c2fa9b43adae9d05278ebbacae15592c63e9fcc2
[linux-2.6.git] / drivers / serial / s3c2410.c
1 /*
2  * linux/drivers/serial/s3c2410.c
3  *
4  * Driver for onboard UARTs on the Samsung S3C24XX
5  *
6  * Based on drivers/char/serial.c and drivers/char/21285.c
7  *
8  * Ben Dooks, (c) 2003 Simtec Electronics
9  *
10  * Changelog:
11  *
12  * 22-Jul-2004  BJD  Finished off device rewrite
13  *
14  * 21-Jul-2004  BJD  Thanks to <herbet@13thfloor.at> for pointing out
15  *                   problems with baud rate and loss of IR settings. Update
16  *                   to add configuration via platform_device structure
17  *
18  * 28-Sep-2004  BJD  Re-write for the following items
19  *                   - S3C2410 and S3C2440 serial support
20  *                   - Power Management support
21  *                   - Fix console via IrDA devices
22  *                   - SysReq (Herbert Pötzl)
23  *                   - Break character handling (Herbert Pötzl)
24  *                   - spin-lock initialisation (Dimitry Andric)
25  *                   - added clock control
26  *                   - updated init code to use platform_device info
27 */
28
29 /* Hote on 2410 error handling
30  *
31  * The s3c2410 manual has a love/hate affair with the contents of the
32  * UERSTAT register in the UART blocks, and keeps marking some of the
33  * error bits as reserved. Having checked with the s3c2410x01,
34  * it copes with BREAKs properly, so I am happy to ignore the RESERVED
35  * feature from the latter versions of the manual.
36  *
37  * If it becomes aparrent that latter versions of the 2410 remove these
38  * bits, then action will have to be taken to differentiate the versions
39  * and change the policy on BREAK
40  *
41  * BJD, 04-Nov-2004
42 */
43
44 #include <linux/config.h>
45
46 #if defined(CONFIG_SERIAL_S3C2410_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
47 #define SUPPORT_SYSRQ
48 #endif
49
50 #include <linux/module.h>
51 #include <linux/ioport.h>
52 #include <linux/device.h>
53 #include <linux/init.h>
54 #include <linux/sysrq.h>
55 #include <linux/console.h>
56 #include <linux/tty.h>
57 #include <linux/tty_flip.h>
58 #include <linux/serial_core.h>
59 #include <linux/serial.h>
60 #include <linux/delay.h>
61
62 #include <asm/io.h>
63 #include <asm/irq.h>
64
65 #include <asm/hardware.h>
66 #include <asm/hardware/clock.h>
67
68 #include <asm/arch/regs-serial.h>
69 #include <asm/arch/regs-gpio.h>
70
71 #include <asm/mach-types.h>
72
73 /* structures */
74
75 struct s3c24xx_uart_info {
76         char                    *name;
77         unsigned int            type;
78         unsigned int            fifosize;
79         unsigned long           rx_fifomask;
80         unsigned long           rx_fifoshift;
81         unsigned long           rx_fifofull;
82         unsigned long           tx_fifomask;
83         unsigned long           tx_fifoshift;
84         unsigned long           tx_fifofull;
85
86         /* clock source control */
87
88         int (*get_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
89         int (*set_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
90 };
91
92 struct s3c24xx_uart_port {
93         unsigned char                   rx_claimed;
94         unsigned char                   tx_claimed;
95
96         struct s3c24xx_uart_info        *info;
97         struct s3c24xx_uart_clksrc      *clksrc;
98         struct clk                      *clk;
99         struct clk                      *baudclk;
100         struct uart_port                port;
101 };
102
103
104 /* configuration defines */
105
106 #if 0
107 #if 1
108 /* send debug to the low-level output routines */
109
110 extern void printascii(const char *);
111
112 static void
113 s3c24xx_serial_dbg(const char *fmt, ...)
114 {
115         va_list va;
116         char buff[256];
117
118         va_start(va, fmt);
119         vsprintf(buff, fmt, va);
120         va_end(va);
121
122         printascii(buff);
123 }
124
125 #define dbg(x...) s3c24xx_serial_dbg(x)
126
127 #else
128 #define dbg(x...) printk(KERN_DEBUG "s3c24xx: ");
129 #endif
130 #else /* no debug */
131 #define dbg(x...) do {} while(0)
132 #endif
133
134 /* UART name and device definitions */
135
136 #define S3C24XX_SERIAL_NAME     "ttySAC"
137 #define S3C24XX_SERIAL_DEVFS    "tts/"
138 #define S3C24XX_SERIAL_MAJOR    204
139 #define S3C24XX_SERIAL_MINOR    64
140
141
142 /* conversion functions */
143
144 #define s3c24xx_dev_to_port(__dev) (struct uart_port *)dev_get_drvdata(__dev)
145 #define s3c24xx_dev_to_cfg(__dev) (struct s3c2410_uartcfg *)((__dev)->platform_data)
146
147 /* we can support 3 uarts, but not always use them */
148
149 #define NR_PORTS (3)
150
151 /* port irq numbers */
152
153 #define TX_IRQ(port) ((port)->irq + 1)
154 #define RX_IRQ(port) ((port)->irq)
155
156 /* register access controls */
157
158 #define portaddr(port, reg) ((port)->membase + (reg))
159
160 #define rd_regb(port, reg) (__raw_readb(portaddr(port, reg)))
161 #define rd_regl(port, reg) (__raw_readl(portaddr(port, reg)))
162
163 #define wr_regb(port, reg, val) \
164   do { __raw_writeb(val, portaddr(port, reg)); } while(0)
165
166 #define wr_regl(port, reg, val) \
167   do { __raw_writel(val, portaddr(port, reg)); } while(0)
168
169 /* macros to change one thing to another */
170
171 #define tx_enabled(port) ((port)->unused[0])
172 #define rx_enabled(port) ((port)->unused[1])
173
174 /* flag to ignore all characters comming in */
175 #define RXSTAT_DUMMY_READ (0x10000000)
176
177 static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
178 {
179         return container_of(port, struct s3c24xx_uart_port, port);
180 }
181
182 /* translate a port to the device name */
183
184 static inline char *s3c24xx_serial_portname(struct uart_port *port)
185 {
186         return to_platform_device(port->dev)->name;
187 }
188
189 static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
190 {
191         return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
192 }
193
194 static void s3c24xx_serial_rx_enable(struct uart_port *port)
195 {
196         unsigned long flags;
197         unsigned int ucon, ufcon;
198         int count = 10000;
199
200         spin_lock_irqsave(&port->lock, flags);
201
202         while (--count && !s3c24xx_serial_txempty_nofifo(port))
203                 udelay(100);
204
205         ufcon = rd_regl(port, S3C2410_UFCON);
206         ufcon |= S3C2410_UFCON_RESETRX;
207         wr_regl(port, S3C2410_UFCON, ufcon);
208
209         ucon = rd_regl(port, S3C2410_UCON);
210         ucon |= S3C2410_UCON_RXIRQMODE;
211         wr_regl(port, S3C2410_UCON, ucon);
212
213         rx_enabled(port) = 1;
214         spin_unlock_irqrestore(&port->lock, flags);
215 }
216
217 static void s3c24xx_serial_rx_disable(struct uart_port *port)
218 {
219         unsigned long flags;
220         unsigned int ucon;
221
222         spin_lock_irqsave(&port->lock, flags);
223
224         ucon = rd_regl(port, S3C2410_UCON);
225         ucon &= ~S3C2410_UCON_RXIRQMODE;
226         wr_regl(port, S3C2410_UCON, ucon);
227
228         rx_enabled(port) = 0;
229         spin_unlock_irqrestore(&port->lock, flags);
230 }
231
232 static void
233 s3c24xx_serial_stop_tx(struct uart_port *port, unsigned int tty_stop)
234 {
235         if (tx_enabled(port)) {
236                 disable_irq(TX_IRQ(port));
237                 tx_enabled(port) = 0;
238                 if (port->flags & UPF_CONS_FLOW)
239                         s3c24xx_serial_rx_enable(port);
240         }
241 }
242
243 static void
244 s3c24xx_serial_start_tx(struct uart_port *port, unsigned int tty_start)
245 {
246         if (!tx_enabled(port)) {
247                 if (port->flags & UPF_CONS_FLOW)
248                         s3c24xx_serial_rx_disable(port);
249
250                 enable_irq(TX_IRQ(port));
251                 tx_enabled(port) = 1;
252         }
253 }
254
255
256 static void s3c24xx_serial_stop_rx(struct uart_port *port)
257 {
258         if (rx_enabled(port)) {
259                 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
260                 disable_irq(RX_IRQ(port));
261                 rx_enabled(port) = 0;
262         }
263 }
264
265 static void s3c24xx_serial_enable_ms(struct uart_port *port)
266 {
267 }
268
269 static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
270 {
271         return to_ourport(port)->info;
272 }
273
274 static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
275 {
276         if (port->dev == NULL)
277                 return NULL;
278
279         return (struct s3c2410_uartcfg *)port->dev->platform_data;
280 }
281
282 static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
283                                      unsigned long ufstat)
284 {
285         struct s3c24xx_uart_info *info = ourport->info;
286
287         if (ufstat & info->rx_fifofull)
288                 return info->fifosize;
289
290         return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
291 }
292
293
294 /* ? - where has parity gone?? */
295 #define S3C2410_UERSTAT_PARITY (0x1000)
296
297 static irqreturn_t
298 s3c24xx_serial_rx_chars(int irq, void *dev_id, struct pt_regs *regs)
299 {
300         struct s3c24xx_uart_port *ourport = dev_id;
301         struct uart_port *port = &ourport->port;
302         struct tty_struct *tty = port->info->tty;
303         unsigned int ufcon, ch, flag, ufstat, uerstat;
304         int max_count = 64;
305
306         while (max_count-- > 0) {
307                 ufcon = rd_regl(port, S3C2410_UFCON);
308                 ufstat = rd_regl(port, S3C2410_UFSTAT);
309
310                 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
311                         break;
312
313                 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
314                         if (tty->low_latency)
315                                 tty_flip_buffer_push(tty);
316
317                         /*
318                          * If this failed then we will throw away the
319                          * bytes but must do so to clear interrupts
320                          */
321                 }
322
323                 uerstat = rd_regl(port, S3C2410_UERSTAT);
324                 ch = rd_regb(port, S3C2410_URXH);
325
326                 if (port->flags & UPF_CONS_FLOW) {
327                         int txe = s3c24xx_serial_txempty_nofifo(port);
328
329                         if (rx_enabled(port)) {
330                                 if (!txe) {
331                                         rx_enabled(port) = 0;
332                                         continue;
333                                 }
334                         } else {
335                                 if (txe) {
336                                         ufcon |= S3C2410_UFCON_RESETRX;
337                                         wr_regl(port, S3C2410_UFCON, ufcon);
338                                         rx_enabled(port) = 1;
339                                         goto out;
340                                 }
341                                 continue;
342                         }
343                 }
344
345                 /* insert the character into the buffer */
346
347                 flag = TTY_NORMAL;
348                 port->icount.rx++;
349
350                 if (uerstat & S3C2410_UERSTAT_ANY) {
351                         dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
352                             ch, uerstat);
353
354                         /* check for break */
355                         if (uerstat & S3C2410_UERSTAT_BREAK) {
356                                 dbg("break!\n");
357                                 port->icount.brk++;
358                                 if (uart_handle_break(port))
359                                     goto ignore_char;
360                         }
361
362                         if (uerstat & S3C2410_UERSTAT_FRAME)
363                                 port->icount.frame++;
364                         if (uerstat & S3C2410_UERSTAT_OVERRUN)
365                                 port->icount.overrun++;
366
367                         uerstat &= port->read_status_mask;
368
369                         if (uerstat & S3C2410_UERSTAT_BREAK)
370                                 flag = TTY_BREAK;
371                         else if (uerstat & S3C2410_UERSTAT_PARITY)
372                                 flag = TTY_PARITY;
373                         else if (uerstat & ( S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_OVERRUN))
374                                 flag = TTY_FRAME;
375                 }
376
377                 if (uart_handle_sysrq_char(port, ch, regs))
378                         goto ignore_char;
379
380                 if ((uerstat & port->ignore_status_mask) == 0) {
381                         tty_insert_flip_char(tty, ch, flag);
382                 }
383
384                 if ((uerstat & S3C2410_UERSTAT_OVERRUN) &&
385                     tty->flip.count < TTY_FLIPBUF_SIZE) {
386                         /*
387                          * Overrun is special, since it's reported
388                          * immediately, and doesn't affect the current
389                          * character.
390                          */
391
392                         tty_insert_flip_char(tty, 0, TTY_OVERRUN);
393                 }
394
395         ignore_char:
396                 continue;
397         }
398         tty_flip_buffer_push(tty);
399
400  out:
401         return IRQ_HANDLED;
402 }
403
404 static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id, struct pt_regs *regs)
405 {
406         struct s3c24xx_uart_port *ourport = id;
407         struct uart_port *port = &ourport->port;
408         struct circ_buf *xmit = &port->info->xmit;
409         int count = 256;
410
411         if (port->x_char) {
412                 wr_regb(port, S3C2410_UTXH, port->x_char);
413                 port->icount.tx++;
414                 port->x_char = 0;
415                 goto out;
416         }
417
418         /* if there isnt anything more to transmit, or the uart is now
419          * stopped, disable the uart and exit
420         */
421
422         if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
423                 s3c24xx_serial_stop_tx(port, 0);
424                 goto out;
425         }
426
427         /* try and drain the buffer... */
428
429         while (!uart_circ_empty(xmit) && count-- > 0) {
430                 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
431                         break;
432
433                 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
434                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
435                 port->icount.tx++;
436         }
437
438         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
439                 uart_write_wakeup(port);
440
441         if (uart_circ_empty(xmit))
442                 s3c24xx_serial_stop_tx(port, 0);
443
444  out:
445         return IRQ_HANDLED;
446 }
447
448 static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
449 {
450         struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
451         unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
452         unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
453
454         if (ufcon & S3C2410_UFCON_FIFOMODE) {
455                 if ((ufstat & info->tx_fifomask) != 0 ||
456                     (ufstat & info->tx_fifofull))
457                         return 0;
458
459                 return 1;
460         }
461
462         return s3c24xx_serial_txempty_nofifo(port);
463 }
464
465 /* no modem control lines */
466 static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
467 {
468         unsigned int umstat = rd_regb(port,S3C2410_UMSTAT);
469
470         if (umstat & S3C2410_UMSTAT_CTS)
471                 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
472         else
473                 return TIOCM_CAR | TIOCM_DSR;
474 }
475
476 static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
477 {
478         /* todo - possibly remove AFC and do manual CTS */
479 }
480
481 static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
482 {
483         unsigned long flags;
484         unsigned int ucon;
485
486         spin_lock_irqsave(&port->lock, flags);
487
488         ucon = rd_regl(port, S3C2410_UCON);
489
490         if (break_state)
491                 ucon |= S3C2410_UCON_SBREAK;
492         else
493                 ucon &= ~S3C2410_UCON_SBREAK;
494
495         wr_regl(port, S3C2410_UCON, ucon);
496
497         spin_unlock_irqrestore(&port->lock, flags);
498 }
499
500 static void s3c24xx_serial_shutdown(struct uart_port *port)
501 {
502         struct s3c24xx_uart_port *ourport = to_ourport(port);
503
504         if (ourport->tx_claimed) {
505                 free_irq(TX_IRQ(port), ourport);
506                 tx_enabled(port) = 0;
507                 ourport->tx_claimed = 0;
508         }
509
510         if (ourport->rx_claimed) {
511                 free_irq(RX_IRQ(port), ourport);
512                 ourport->rx_claimed = 0;
513                 rx_enabled(port) = 0;
514         }
515 }
516
517
518 static int s3c24xx_serial_startup(struct uart_port *port)
519 {
520         struct s3c24xx_uart_port *ourport = to_ourport(port);
521         unsigned long flags;
522         int ret;
523
524         dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
525             port->mapbase, port->membase);
526
527         local_irq_save(flags);
528
529         rx_enabled(port) = 1;
530
531         ret = request_irq(RX_IRQ(port),
532                           s3c24xx_serial_rx_chars, 0,
533                           s3c24xx_serial_portname(port), ourport);
534
535         if (ret != 0) {
536                 printk(KERN_ERR "cannot get irq %d\n", RX_IRQ(port));
537                 return ret;
538         }
539
540         ourport->rx_claimed = 1;
541
542         dbg("requesting tx irq...\n");
543
544         tx_enabled(port) = 1;
545
546         ret = request_irq(TX_IRQ(port),
547                           s3c24xx_serial_tx_chars, 0,
548                           s3c24xx_serial_portname(port), ourport);
549
550         if (ret) {
551                 printk(KERN_ERR "cannot get irq %d\n", TX_IRQ(port));
552                 goto err;
553         }
554
555         ourport->tx_claimed = 1;
556
557         dbg("s3c24xx_serial_startup ok\n");
558
559         /* the port reset code should have done the correct
560          * register setup for the port controls */
561
562         local_irq_restore(flags);
563         return ret;
564
565  err:
566         s3c24xx_serial_shutdown(port);
567         local_irq_restore(flags);
568         return ret;
569 }
570
571 /* power power management control */
572
573 static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
574                               unsigned int old)
575 {
576         struct s3c24xx_uart_port *ourport = to_ourport(port);
577
578         switch (level) {
579         case 3:
580                 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
581                         clk_disable(ourport->baudclk);
582
583                 clk_disable(ourport->clk);
584                 break;
585
586         case 0:
587                 clk_enable(ourport->clk);
588
589                 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
590                         clk_enable(ourport->baudclk);
591
592                 break;
593         default:
594                 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
595         }
596 }
597
598 /* baud rate calculation
599  *
600  * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
601  * of different sources, including the peripheral clock ("pclk") and an
602  * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
603  * with a programmable extra divisor.
604  *
605  * The following code goes through the clock sources, and calculates the
606  * baud clocks (and the resultant actual baud rates) and then tries to
607  * pick the closest one and select that.
608  *
609  * NOTES:
610  *      1) there is no current code to properly select/deselect FCLK on
611  *         the s3c2440, so only specify FCLK or non-FCLK in the clock
612  *         sources for the UART
613  *
614 */
615
616
617 #define MAX_CLKS (8)
618
619 static struct s3c24xx_uart_clksrc tmp_clksrc = {
620         .name           = "pclk",
621         .min_baud       = 0,
622         .max_baud       = 0,
623         .divisor        = 1,
624 };
625
626 static inline int
627 s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
628 {
629         struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
630
631         return (info->get_clksrc)(port, c);
632 }
633
634 static inline int
635 s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
636 {
637         struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
638
639         return (info->set_clksrc)(port, c);
640 }
641
642 struct baud_calc {
643         struct s3c24xx_uart_clksrc      *clksrc;
644         unsigned int                     calc;
645         unsigned int                     quot;
646         struct clk                      *src;
647 };
648
649 static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
650                                    struct uart_port *port,
651                                    struct s3c24xx_uart_clksrc *clksrc,
652                                    unsigned int baud)
653 {
654         unsigned long rate;
655
656         calc->src = clk_get(port->dev, clksrc->name);
657         if (calc->src == NULL || IS_ERR(calc->src))
658                 return 0;
659
660         rate = clk_get_rate(calc->src);
661
662         calc->clksrc = clksrc;
663         calc->quot = (rate + (8 * baud)) / (16 * baud);
664         calc->calc = (rate / (calc->quot * 16));
665
666         calc->quot--;
667         return 1;
668 }
669
670 static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
671                                           struct s3c24xx_uart_clksrc **clksrc,
672                                           struct clk **clk,
673                                           unsigned int baud)
674 {
675         struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
676         struct s3c24xx_uart_clksrc *clkp;
677         struct baud_calc res[MAX_CLKS];
678         struct baud_calc *resptr, *best, *sptr;
679         int i;
680
681         clkp = cfg->clocks;
682         best = NULL;
683
684         if (cfg->clocks_size < 2) {
685                 if (cfg->clocks_size == 0)
686                         clkp = &tmp_clksrc;
687
688                 s3c24xx_serial_calcbaud(res, port, clkp, baud);
689                 best = res;
690                 resptr = best + 1;
691         } else {
692                 resptr = res;
693
694                 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
695                         if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
696                                 resptr++;
697                 }
698         }
699
700         /* ok, we now need to select the best clock we found */
701
702         if (!best) {
703                 unsigned int deviation = (1<<30)|((1<<30)-1);
704                 int calc_deviation;
705
706                 for (sptr = res; sptr < resptr; sptr++) {
707                         printk(KERN_DEBUG
708                                "found clk %p (%s) quot %d, calc %d\n",
709                                sptr->clksrc, sptr->clksrc->name,
710                                sptr->quot, sptr->calc);
711
712                         calc_deviation = baud - sptr->calc;
713                         if (calc_deviation < 0)
714                                 calc_deviation = -calc_deviation;
715
716                         if (calc_deviation < deviation) {
717                                 best = sptr;
718                                 deviation = calc_deviation;
719                         }
720                 }
721
722                 printk(KERN_DEBUG "best %p (deviation %d)\n", best, deviation);
723         }
724
725         printk(KERN_DEBUG "selected clock %p (%s) quot %d, calc %d\n",
726                best->clksrc, best->clksrc->name, best->quot, best->calc);
727
728         /* store results to pass back */
729
730         *clksrc = best->clksrc;
731         *clk    = best->src;
732
733         return best->quot;
734 }
735
736 static void s3c24xx_serial_set_termios(struct uart_port *port,
737                                        struct termios *termios,
738                                        struct termios *old)
739 {
740         struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
741         struct s3c24xx_uart_port *ourport = to_ourport(port);
742         struct s3c24xx_uart_clksrc *clksrc;
743         struct clk *clk;
744         unsigned long flags;
745         unsigned int baud, quot;
746         unsigned int ulcon;
747
748         /*
749          * We don't support modem control lines.
750          */
751         termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
752         termios->c_cflag |= CLOCAL;
753
754         /*
755          * Ask the core to calculate the divisor for us.
756          */
757
758         baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
759
760         if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
761                 quot = port->custom_divisor;
762         else
763                 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
764
765         /* check to see if we need  to change clock source */
766
767         if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
768                 s3c24xx_serial_setsource(port, clksrc);
769
770                 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
771                         clk_disable(ourport->baudclk);
772                         clk_unuse(ourport->baudclk);
773                         ourport->baudclk  = NULL;
774                 }
775
776                 clk_use(clk);
777                 clk_enable(clk);
778
779                 ourport->clksrc = clksrc;
780                 ourport->baudclk = clk;
781         }
782
783         switch (termios->c_cflag & CSIZE) {
784         case CS5:
785                 dbg("config: 5bits/char\n");
786                 ulcon = S3C2410_LCON_CS5;
787                 break;
788         case CS6:
789                 dbg("config: 6bits/char\n");
790                 ulcon = S3C2410_LCON_CS6;
791                 break;
792         case CS7:
793                 dbg("config: 7bits/char\n");
794                 ulcon = S3C2410_LCON_CS7;
795                 break;
796         case CS8:
797         default:
798                 dbg("config: 8bits/char\n");
799                 ulcon = S3C2410_LCON_CS8;
800                 break;
801         }
802
803         /* preserve original lcon IR settings */
804         ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
805
806         if (termios->c_cflag & CSTOPB)
807                 ulcon |= S3C2410_LCON_STOPB;
808
809         if (termios->c_cflag & PARENB) {
810                 if (!(termios->c_cflag & PARODD))
811                         ulcon |= S3C2410_LCON_PODD;
812                 else
813                         ulcon |= S3C2410_LCON_PEVEN;
814         } else {
815                 ulcon |= S3C2410_LCON_PNONE;
816         }
817
818         spin_lock_irqsave(&port->lock, flags);
819
820         dbg("setting ulcon to %08x, brddiv to %d\n", ulcon, quot);
821
822         wr_regl(port, S3C2410_ULCON, ulcon);
823         wr_regl(port, S3C2410_UBRDIV, quot);
824
825         dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
826             rd_regl(port, S3C2410_ULCON),
827             rd_regl(port, S3C2410_UCON),
828             rd_regl(port, S3C2410_UFCON));
829
830         /*
831          * Update the per-port timeout.
832          */
833         uart_update_timeout(port, termios->c_cflag, baud);
834
835         /*
836          * Which character status flags are we interested in?
837          */
838         port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
839         if (termios->c_iflag & INPCK)
840                 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
841
842         /*
843          * Which character status flags should we ignore?
844          */
845         port->ignore_status_mask = 0;
846         if (termios->c_iflag & IGNPAR)
847                 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
848         if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
849                 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
850
851         /*
852          * Ignore all characters if CREAD is not set.
853          */
854         if ((termios->c_cflag & CREAD) == 0)
855                 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
856
857         spin_unlock_irqrestore(&port->lock, flags);
858 }
859
860 static const char *s3c24xx_serial_type(struct uart_port *port)
861 {
862         switch (port->type) {
863         case PORT_S3C2410:
864                 return "S3C2410";
865         case PORT_S3C2440:
866                 return "S3C2440";
867         default:
868                 return NULL;
869         }
870 }
871
872 #define MAP_SIZE (0x100)
873
874 static void s3c24xx_serial_release_port(struct uart_port *port)
875 {
876         release_mem_region(port->mapbase, MAP_SIZE);
877 }
878
879 static int s3c24xx_serial_request_port(struct uart_port *port)
880 {
881         char *name = s3c24xx_serial_portname(port);
882         return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
883 }
884
885 static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
886 {
887         struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
888
889         if (flags & UART_CONFIG_TYPE &&
890             s3c24xx_serial_request_port(port) == 0)
891                 port->type = info->type;
892 }
893
894 /*
895  * verify the new serial_struct (for TIOCSSERIAL).
896  */
897 static int
898 s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
899 {
900         struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
901
902         if (ser->type != PORT_UNKNOWN && ser->type != info->type)
903                 return -EINVAL;
904
905         return 0;
906 }
907
908
909 #ifdef CONFIG_SERIAL_S3C2410_CONSOLE
910
911 static struct console s3c24xx_serial_console;
912
913 #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
914 #else
915 #define S3C24XX_SERIAL_CONSOLE NULL
916 #endif
917
918 static struct uart_ops s3c24xx_serial_ops = {
919         .pm             = s3c24xx_serial_pm,
920         .tx_empty       = s3c24xx_serial_tx_empty,
921         .get_mctrl      = s3c24xx_serial_get_mctrl,
922         .set_mctrl      = s3c24xx_serial_set_mctrl,
923         .stop_tx        = s3c24xx_serial_stop_tx,
924         .start_tx       = s3c24xx_serial_start_tx,
925         .stop_rx        = s3c24xx_serial_stop_rx,
926         .enable_ms      = s3c24xx_serial_enable_ms,
927         .break_ctl      = s3c24xx_serial_break_ctl,
928         .startup        = s3c24xx_serial_startup,
929         .shutdown       = s3c24xx_serial_shutdown,
930         .set_termios    = s3c24xx_serial_set_termios,
931         .type           = s3c24xx_serial_type,
932         .release_port   = s3c24xx_serial_release_port,
933         .request_port   = s3c24xx_serial_request_port,
934         .config_port    = s3c24xx_serial_config_port,
935         .verify_port    = s3c24xx_serial_verify_port,
936 };
937
938
939 static struct uart_driver s3c24xx_uart_drv = {
940         .owner          = THIS_MODULE,
941         .dev_name       = "s3c2410_serial",
942         .nr             = 3,
943         .cons           = S3C24XX_SERIAL_CONSOLE,
944         .driver_name    = S3C24XX_SERIAL_NAME,
945         .devfs_name     = S3C24XX_SERIAL_DEVFS,
946         .major          = S3C24XX_SERIAL_MAJOR,
947         .minor          = S3C24XX_SERIAL_MINOR,
948 };
949
950 static struct s3c24xx_uart_port s3c24xx_serial_ports[NR_PORTS] = {
951         [0] = {
952                 .port = {
953                         .lock           = SPIN_LOCK_UNLOCKED,
954                         .membase        = 0,
955                         .mapbase        = 0,
956                         .iotype         = UPIO_MEM,
957                         .irq            = IRQ_S3CUART_RX0,
958                         .uartclk        = 0,
959                         .fifosize       = 16,
960                         .ops            = &s3c24xx_serial_ops,
961                         .flags          = UPF_BOOT_AUTOCONF,
962                         .line           = 0,
963                 }
964         },
965         [1] = {
966                 .port = {
967                         .lock           = SPIN_LOCK_UNLOCKED,
968                         .membase        = 0,
969                         .mapbase        = 0,
970                         .iotype         = UPIO_MEM,
971                         .irq            = IRQ_S3CUART_RX1,
972                         .uartclk        = 0,
973                         .fifosize       = 16,
974                         .ops            = &s3c24xx_serial_ops,
975                         .flags          = UPF_BOOT_AUTOCONF,
976                         .line           = 1,
977                 }
978         },
979 #if NR_PORTS > 2
980
981         [2] = {
982                 .port = {
983                         .lock           = SPIN_LOCK_UNLOCKED,
984                         .membase        = 0,
985                         .mapbase        = 0,
986                         .iotype         = UPIO_MEM,
987                         .irq            = IRQ_S3CUART_RX2,
988                         .uartclk        = 0,
989                         .fifosize       = 16,
990                         .ops            = &s3c24xx_serial_ops,
991                         .flags          = UPF_BOOT_AUTOCONF,
992                         .line           = 2,
993                 }
994         }
995 #endif
996 };
997
998
999 static int s3c24xx_serial_resetport(struct uart_port *port,
1000                                     struct s3c2410_uartcfg *cfg)
1001 {
1002         /* ensure registers are setup */
1003
1004         dbg("s3c24xx_serial_resetport: port=%p (%08lx), cfg=%p\n",
1005             port, port->mapbase, cfg);
1006
1007         wr_regl(port, S3C2410_UCON,  cfg->ucon);
1008         wr_regl(port, S3C2410_ULCON, cfg->ulcon);
1009
1010         /* reset both fifos */
1011
1012         wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1013         wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1014
1015         return 0;
1016 }
1017
1018 /* s3c24xx_serial_init_port
1019  *
1020  * initialise a single serial port from the platform device given
1021  */
1022
1023 static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1024                                     struct s3c24xx_uart_info *info,
1025                                     struct platform_device *platdev)
1026 {
1027         struct uart_port *port = &ourport->port;
1028         struct s3c2410_uartcfg *cfg;
1029         struct resource *res;
1030
1031         dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1032
1033         if (platdev == NULL)
1034                 return -ENODEV;
1035
1036         cfg = s3c24xx_dev_to_cfg(&platdev->dev);
1037
1038         if (port->mapbase != 0)
1039                 return 0;
1040
1041         if (cfg->hwport > 3)
1042                 return -EINVAL;
1043
1044         /* setup info for port */
1045         port->dev       = &platdev->dev;
1046         ourport->info   = info;
1047
1048         /* copy the info in from provided structure */
1049         ourport->port.fifosize = info->fifosize;
1050
1051         dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
1052
1053         port->uartclk = 1;
1054
1055         if (cfg->uart_flags & UPF_CONS_FLOW) {
1056                 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1057                 port->flags |= UPF_CONS_FLOW;
1058         }
1059
1060         /* sort our the physical and virtual addresses for each UART */
1061
1062         res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1063         if (res == NULL) {
1064                 printk(KERN_ERR "failed to find memory resource for uart\n");
1065                 return -EINVAL;
1066         }
1067
1068         dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1069
1070         port->mapbase   = res->start;
1071         port->membase   = (void __iomem *)(res->start - S3C2410_PA_UART);
1072         port->membase  += S3C2410_VA_UART;
1073         port->irq       = platform_get_irq(platdev, 0);
1074
1075         ourport->clk    = clk_get(&platdev->dev, "uart");
1076
1077         if (ourport->clk != NULL && !IS_ERR(ourport->clk))
1078                 clk_use(ourport->clk);
1079
1080         dbg("port: map=%08x, mem=%08x, irq=%d, clock=%ld\n",
1081             port->mapbase, port->membase, port->irq, port->uartclk);
1082
1083         /* reset the fifos (and setup the uart) */
1084         s3c24xx_serial_resetport(port, cfg);
1085         return 0;
1086 }
1087
1088 /* Device driver serial port probe */
1089
1090 static int probe_index = 0;
1091
1092 int s3c24xx_serial_probe(struct device *_dev,
1093                          struct s3c24xx_uart_info *info)
1094 {
1095         struct s3c24xx_uart_port *ourport;
1096         struct platform_device *dev = to_platform_device(_dev);
1097         int ret;
1098
1099         dbg("s3c24xx_serial_probe(%p, %p) %d\n", _dev, info, probe_index);
1100
1101         ourport = &s3c24xx_serial_ports[probe_index];
1102         probe_index++;
1103
1104         dbg("%s: initialising port %p...\n", __FUNCTION__, ourport);
1105
1106         ret = s3c24xx_serial_init_port(ourport, info, dev);
1107         if (ret < 0)
1108                 goto probe_err;
1109
1110         dbg("%s: adding port\n", __FUNCTION__);
1111         uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1112         dev_set_drvdata(_dev, &ourport->port);
1113
1114         return 0;
1115
1116  probe_err:
1117         return ret;
1118 }
1119
1120 int s3c24xx_serial_remove(struct device *_dev)
1121 {
1122         struct uart_port *port = s3c24xx_dev_to_port(_dev);
1123
1124         if (port)
1125                 uart_remove_one_port(&s3c24xx_uart_drv, port);
1126
1127         return 0;
1128 }
1129
1130 /* UART power management code */
1131
1132 #ifdef CONFIG_PM
1133
1134 int s3c24xx_serial_suspend(struct device *dev, u32 state, u32 level)
1135 {
1136         struct uart_port *port = s3c24xx_dev_to_port(dev);
1137
1138         if (port && level == SUSPEND_DISABLE)
1139                 uart_suspend_port(&s3c24xx_uart_drv, port);
1140
1141         return 0;
1142 }
1143
1144 int s3c24xx_serial_resume(struct device *dev, u32 level)
1145 {
1146         struct uart_port *port = s3c24xx_dev_to_port(dev);
1147         struct s3c24xx_uart_port *ourport = to_ourport(port);
1148
1149         if (port && level == RESUME_ENABLE) {
1150                 clk_enable(ourport->clk);
1151                 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1152                 clk_disable(ourport->clk);
1153
1154                 uart_resume_port(&s3c24xx_uart_drv, port);
1155         }
1156
1157         return 0;
1158 }
1159
1160 #else
1161 #define s3c24xx_serial_suspend NULL
1162 #define s3c24xx_serial_resume  NULL
1163 #endif
1164
1165 int s3c24xx_serial_init(struct device_driver *drv,
1166                         struct s3c24xx_uart_info *info)
1167 {
1168         dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
1169         return driver_register(drv);
1170 }
1171
1172
1173 /* now comes the code to initialise either the s3c2410 or s3c2440 serial
1174  * port information
1175 */
1176
1177 /* cpu specific variations on the serial port support */
1178
1179 #ifdef CONFIG_CPU_S3C2410
1180
1181 static int s3c2410_serial_setsource(struct uart_port *port,
1182                                     struct s3c24xx_uart_clksrc *clk)
1183 {
1184         unsigned long ucon = rd_regl(port, S3C2410_UCON);
1185
1186         if (strcmp(clk->name, "uclk") == 0)
1187                 ucon |= S3C2410_UCON_UCLK;
1188         else
1189                 ucon &= ~S3C2410_UCON_UCLK;
1190
1191         wr_regl(port, S3C2410_UCON, ucon);
1192         return 0;
1193 }
1194
1195 static int s3c2410_serial_getsource(struct uart_port *port,
1196                                     struct s3c24xx_uart_clksrc *clk)
1197 {
1198         unsigned long ucon = rd_regl(port, S3C2410_UCON);
1199
1200         clk->divisor = 1;
1201         clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
1202
1203         return 0;
1204 }
1205
1206 static struct s3c24xx_uart_info s3c2410_uart_inf = {
1207         .name           = "Samsung S3C2410 UART",
1208         .type           = PORT_S3C2410,
1209         .fifosize       = 16,
1210         .rx_fifomask    = S3C2410_UFSTAT_RXMASK,
1211         .rx_fifoshift   = S3C2410_UFSTAT_RXSHIFT,
1212         .rx_fifofull    = S3C2410_UFSTAT_RXFULL,
1213         .tx_fifofull    = S3C2410_UFSTAT_TXFULL,
1214         .tx_fifomask    = S3C2410_UFSTAT_TXMASK,
1215         .tx_fifoshift   = S3C2410_UFSTAT_TXSHIFT,
1216         .get_clksrc     = s3c2410_serial_getsource,
1217         .set_clksrc     = s3c2410_serial_setsource,
1218 };
1219
1220 /* device management */
1221
1222 static int s3c2410_serial_probe(struct device *dev)
1223 {
1224         return s3c24xx_serial_probe(dev, &s3c2410_uart_inf);
1225 }
1226
1227 static struct device_driver s3c2410_serial_drv = {
1228         .name           = "s3c2410-uart",
1229         .bus            = &platform_bus_type,
1230         .probe          = s3c2410_serial_probe,
1231         .remove         = s3c24xx_serial_remove,
1232         .suspend        = s3c24xx_serial_suspend,
1233         .resume         = s3c24xx_serial_resume,
1234 };
1235
1236 static inline int s3c2410_serial_init(void)
1237 {
1238         return s3c24xx_serial_init(&s3c2410_serial_drv, &s3c2410_uart_inf);
1239 }
1240
1241 static inline void s3c2410_serial_exit(void)
1242 {
1243         driver_unregister(&s3c2410_serial_drv);
1244 }
1245
1246 #define s3c2410_uart_inf_at &s3c2410_uart_inf
1247 #else
1248
1249 static inline int s3c2410_serial_init(void)
1250 {
1251         return 0;
1252 }
1253
1254 static inline void s3c2410_serial_exit(void)
1255 {
1256 }
1257
1258 #define s3c2410_uart_inf_at NULL
1259
1260 #endif /* CONFIG_CPU_S3C2410 */
1261
1262 #ifdef CONFIG_CPU_S3C2440
1263
1264 static int s3c2440_serial_setsource(struct uart_port *port,
1265                                      struct s3c24xx_uart_clksrc *clk)
1266 {
1267         unsigned long ucon = rd_regl(port, S3C2410_UCON);
1268
1269         // todo - proper fclk<>nonfclk switch //
1270
1271         ucon &= ~S3C2440_UCON_CLKMASK;
1272
1273         if (strcmp(clk->name, "uclk") == 0)
1274                 ucon |= S3C2440_UCON_UCLK;
1275         else if (strcmp(clk->name, "pclk") == 0)
1276                 ucon |= S3C2440_UCON_PCLK;
1277         else if (strcmp(clk->name, "fclk") == 0)
1278                 ucon |= S3C2440_UCON_FCLK;
1279         else {
1280                 printk(KERN_ERR "unknown clock source %s\n", clk->name);
1281                 return -EINVAL;
1282         }
1283
1284         wr_regl(port, S3C2410_UCON, ucon);
1285         return 0;
1286 }
1287
1288
1289 static int s3c2440_serial_getsource(struct uart_port *port,
1290                                     struct s3c24xx_uart_clksrc *clk)
1291 {
1292         unsigned long ucon = rd_regl(port, S3C2410_UCON);
1293
1294         switch (ucon & S3C2440_UCON_CLKMASK) {
1295         case S3C2440_UCON_UCLK:
1296                 clk->divisor = 1;
1297                 clk->name = "uclk";
1298                 break;
1299
1300         case S3C2440_UCON_PCLK:
1301         case S3C2440_UCON_PCLK2:
1302                 clk->divisor = 1;
1303                 clk->name = "pclk";
1304                 break;
1305
1306         case S3C2440_UCON_FCLK:
1307                 clk->divisor = 7; /* todo - work out divisor */
1308                 clk->name = "fclk";
1309                 break;
1310         }
1311
1312         return 0;
1313 }
1314
1315
1316 static struct s3c24xx_uart_info s3c2440_uart_inf = {
1317         .name           = "Samsung S3C2440 UART",
1318         .type           = PORT_S3C2440,
1319         .fifosize       = 64,
1320         .rx_fifomask    = S3C2440_UFSTAT_RXMASK,
1321         .rx_fifoshift   = S3C2440_UFSTAT_RXSHIFT,
1322         .rx_fifofull    = S3C2440_UFSTAT_RXFULL,
1323         .tx_fifofull    = S3C2440_UFSTAT_TXFULL,
1324         .tx_fifomask    = S3C2440_UFSTAT_TXMASK,
1325         .tx_fifoshift   = S3C2440_UFSTAT_TXSHIFT,
1326         .get_clksrc     = s3c2440_serial_getsource,
1327         .set_clksrc     = s3c2440_serial_setsource
1328 };
1329
1330 /* device management */
1331
1332 static int s3c2440_serial_probe(struct device *dev)
1333 {
1334         dbg("s3c2440_serial_probe: dev=%p\n", dev);
1335         return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
1336 }
1337
1338 static struct device_driver s3c2440_serial_drv = {
1339         .name           = "s3c2440-uart",
1340         .bus            = &platform_bus_type,
1341         .probe          = s3c2440_serial_probe,
1342         .remove         = s3c24xx_serial_remove,
1343         .suspend        = s3c24xx_serial_suspend,
1344         .resume         = s3c24xx_serial_resume,
1345 };
1346
1347
1348 static inline int s3c2440_serial_init(void)
1349 {
1350         return s3c24xx_serial_init(&s3c2440_serial_drv, &s3c2440_uart_inf);
1351 }
1352
1353 static inline void s3c2440_serial_exit(void)
1354 {
1355         driver_unregister(&s3c2440_serial_drv);
1356 }
1357
1358 #define s3c2440_uart_inf_at &s3c2440_uart_inf
1359 #else
1360
1361 static inline int s3c2440_serial_init(void)
1362 {
1363         return 0;
1364 }
1365
1366 static inline void s3c2440_serial_exit(void)
1367 {
1368 }
1369
1370 #define s3c2440_uart_inf_at NULL
1371 #endif /* CONFIG_CPU_S3C2440 */
1372
1373 /* module initialisation code */
1374
1375 static int __init s3c24xx_serial_modinit(void)
1376 {
1377         int ret;
1378
1379         ret = uart_register_driver(&s3c24xx_uart_drv);
1380         if (ret < 0) {
1381                 printk(KERN_ERR "failed to register UART driver\n");
1382                 return -1;
1383         }
1384
1385         s3c2410_serial_init();
1386         s3c2440_serial_init();
1387
1388         return 0;
1389 }
1390
1391 static void __exit s3c24xx_serial_modexit(void)
1392 {
1393         s3c2410_serial_exit();
1394         s3c2440_serial_exit();
1395
1396         uart_unregister_driver(&s3c24xx_uart_drv);
1397 }
1398
1399
1400 module_init(s3c24xx_serial_modinit);
1401 module_exit(s3c24xx_serial_modexit);
1402
1403 /* Console code */
1404
1405 #ifdef CONFIG_SERIAL_S3C2410_CONSOLE
1406
1407 static struct uart_port *cons_uart;
1408
1409 static int
1410 s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1411 {
1412         struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1413         unsigned long ufstat, utrstat;
1414
1415         if (ufcon & S3C2410_UFCON_FIFOMODE) {
1416                 /* fifo mode - check ammount of data in fifo registers... */
1417
1418                 ufstat = rd_regl(port, S3C2410_UFSTAT);
1419                 return (ufstat & info->tx_fifofull) ? 0 : 1;
1420         }
1421
1422         /* in non-fifo mode, we go and use the tx buffer empty */
1423
1424         utrstat = rd_regl(port, S3C2410_UTRSTAT);
1425         return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1426 }
1427
1428 static void
1429 s3c24xx_serial_console_write(struct console *co, const char *s,
1430                              unsigned int count)
1431 {
1432         int i;
1433         unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1434
1435         for (i = 0; i < count; i++) {
1436                 while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
1437                         barrier();
1438
1439                 wr_regb(cons_uart, S3C2410_UTXH, s[i]);
1440
1441                 if (s[i] == '\n') {
1442                         while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
1443                                 barrier();
1444
1445                         wr_regb(cons_uart, S3C2410_UTXH, '\r');
1446                 }
1447         }
1448 }
1449
1450 static void __init
1451 s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1452                            int *parity, int *bits)
1453 {
1454         struct s3c24xx_uart_clksrc clksrc;
1455         struct clk *clk;
1456         unsigned int ulcon;
1457         unsigned int ucon;
1458         unsigned int ubrdiv;
1459         unsigned long rate;
1460
1461         ulcon  = rd_regl(port, S3C2410_ULCON);
1462         ucon   = rd_regl(port, S3C2410_UCON);
1463         ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1464
1465         dbg("s3c24xx_serial_get_options: port=%p\n"
1466             "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1467             port, ulcon, ucon, ubrdiv);
1468
1469         if ((ucon & 0xf) != 0) {
1470                 /* consider the serial port configured if the tx/rx mode set */
1471
1472                 switch (ulcon & S3C2410_LCON_CSMASK) {
1473                 case S3C2410_LCON_CS5:
1474                         *bits = 5;
1475                         break;
1476                 case S3C2410_LCON_CS6:
1477                         *bits = 6;
1478                         break;
1479                 case S3C2410_LCON_CS7:
1480                         *bits = 7;
1481                         break;
1482                 default:
1483                 case S3C2410_LCON_CS8:
1484                         *bits = 8;
1485                         break;
1486                 }
1487
1488                 switch (ulcon & S3C2410_LCON_PMASK) {
1489                 case S3C2410_LCON_PEVEN:
1490                         *parity = 'e';
1491                         break;
1492
1493                 case S3C2410_LCON_PODD:
1494                         *parity = 'o';
1495                         break;
1496
1497                 case S3C2410_LCON_PNONE:
1498                 default:
1499                         *parity = 'n';
1500                 }
1501
1502                 /* now calculate the baud rate */
1503
1504                 s3c24xx_serial_getsource(port, &clksrc);
1505
1506                 clk = clk_get(port->dev, clksrc.name);
1507                 if (!IS_ERR(clk) && clk != NULL)
1508                         rate = clk_get_rate(clk);
1509                 else
1510                         rate = 1;
1511
1512
1513                 *baud = rate / ( 16 * (ubrdiv + 1));
1514                 dbg("calculated baud %d\n", *baud);
1515         }
1516
1517 }
1518
1519 /* s3c24xx_serial_init_ports
1520  *
1521  * initialise the serial ports from the machine provided initialisation
1522  * data.
1523 */
1524
1525 static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info *info)
1526 {
1527         struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1528         struct platform_device **platdev_ptr;
1529         int i;
1530
1531         dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1532
1533         platdev_ptr = s3c24xx_uart_devs;
1534
1535         for (i = 0; i < NR_PORTS; i++, ptr++, platdev_ptr++) {
1536                 s3c24xx_serial_init_port(ptr, info, *platdev_ptr);
1537         }
1538
1539         return 0;
1540 }
1541
1542 static int __init
1543 s3c24xx_serial_console_setup(struct console *co, char *options)
1544 {
1545         struct uart_port *port;
1546         int baud = 9600;
1547         int bits = 8;
1548         int parity = 'n';
1549         int flow = 'n';
1550
1551         dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1552             co, co->index, options);
1553
1554         /* is this a valid port */
1555
1556         if (co->index == -1 || co->index >= NR_PORTS)
1557                 co->index = 0;
1558
1559         port = &s3c24xx_serial_ports[co->index].port;
1560
1561         /* is the port configured? */
1562
1563         if (port->mapbase == 0x0) {
1564                 co->index = 0;
1565                 port = &s3c24xx_serial_ports[co->index].port;
1566         }
1567
1568         cons_uart = port;
1569
1570         dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1571
1572         /*
1573          * Check whether an invalid uart number has been specified, and
1574          * if so, search for the first available port that does have
1575          * console support.
1576          */
1577         if (options)
1578                 uart_parse_options(options, &baud, &parity, &bits, &flow);
1579         else
1580                 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1581
1582         dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1583
1584         return uart_set_options(port, co, baud, parity, bits, flow);
1585 }
1586
1587 /* s3c24xx_serial_initconsole
1588  *
1589  * initialise the console from one of the uart drivers
1590 */
1591
1592 static struct console s3c24xx_serial_console =
1593 {
1594         .name           = S3C24XX_SERIAL_NAME,
1595         .device         = uart_console_device,
1596         .flags          = CON_PRINTBUFFER,
1597         .index          = -1,
1598         .write          = s3c24xx_serial_console_write,
1599         .setup          = s3c24xx_serial_console_setup
1600 };
1601
1602
1603 static int s3c24xx_serial_initconsole(void)
1604 {
1605         struct s3c24xx_uart_info *info;
1606         struct platform_device *dev = s3c24xx_uart_devs[0];
1607
1608         dbg("s3c24xx_serial_initconsole\n");
1609
1610         /* select driver based on the cpu */
1611
1612         if (dev == NULL) {
1613                 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1614                 return 0;
1615         }
1616
1617         if (strcmp(dev->name, "s3c2410-uart") == 0) {
1618                 info = s3c2410_uart_inf_at;
1619         } else if (strcmp(dev->name, "s3c2440-uart") == 0) {
1620                 info = s3c2440_uart_inf_at;
1621         } else {
1622                 printk(KERN_ERR "s3c24xx: no driver for %s\n", dev->name);
1623                 return 0;
1624         }
1625
1626         if (info == NULL) {
1627                 printk(KERN_ERR "s3c24xx: no driver for console\n");
1628                 return 0;
1629         }
1630
1631         s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1632         s3c24xx_serial_init_ports(info);
1633
1634         register_console(&s3c24xx_serial_console);
1635         return 0;
1636 }
1637
1638 console_initcall(s3c24xx_serial_initconsole);
1639
1640 #endif /* CONFIG_SERIAL_S3C2410_CONSOLE */
1641
1642 MODULE_LICENSE("GPL");
1643 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1644 MODULE_DESCRIPTION("Samsung S3C2410/S3C2440 Serial port driver");