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