Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / s3c2410.c
blobffc61389614b62e02cd241e6602f1cc1464298bd
1 /*
2 * linux/drivers/serial/s3c2410.c
4 * Driver for onboard UARTs on the Samsung S3C24XX
6 * Based on drivers/char/serial.c and drivers/char/21285.c
8 * Ben Dooks, (c) 2003-2005 Simtec Electronics
9 * http://www.simtec.co.uk/products/SWLINUX/
11 * Changelog:
13 * 22-Jul-2004 BJD Finished off device rewrite
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
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
29 * 06-Mar-2005 BJD Add s3c2440 fclk clock source
31 * 09-Mar-2005 BJD Add s3c2400 support
33 * 10-Mar-2005 LCVR Changed S3C2410_VA_UART to S3C24XX_VA_UART
36 /* Note on 2440 fclk clock source handling
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.
43 /* Hote on 2410 error handling
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.
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
55 * BJD, 04-Nov-2004
58 #include <linux/config.h>
60 #if defined(CONFIG_SERIAL_S3C2410_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
61 #define SUPPORT_SYSRQ
62 #endif
64 #include <linux/module.h>
65 #include <linux/ioport.h>
66 #include <linux/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>
76 #include <asm/io.h>
77 #include <asm/irq.h>
79 #include <asm/hardware.h>
80 #include <asm/hardware/clock.h>
82 #include <asm/arch/regs-serial.h>
83 #include <asm/arch/regs-gpio.h>
85 #include <asm/mach-types.h>
87 /* structures */
89 struct s3c24xx_uart_info {
90 char *name;
91 unsigned int type;
92 unsigned int fifosize;
93 unsigned long rx_fifomask;
94 unsigned long rx_fifoshift;
95 unsigned long rx_fifofull;
96 unsigned long tx_fifomask;
97 unsigned long tx_fifoshift;
98 unsigned long tx_fifofull;
100 /* clock source control */
102 int (*get_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
103 int (*set_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
105 /* uart controls */
106 int (*reset_port)(struct uart_port *, struct s3c2410_uartcfg *);
109 struct s3c24xx_uart_port {
110 unsigned char rx_claimed;
111 unsigned char tx_claimed;
113 struct s3c24xx_uart_info *info;
114 struct s3c24xx_uart_clksrc *clksrc;
115 struct clk *clk;
116 struct clk *baudclk;
117 struct uart_port port;
121 /* configuration defines */
123 #if 0
124 #if 1
125 /* send debug to the low-level output routines */
127 extern void printascii(const char *);
129 static void
130 s3c24xx_serial_dbg(const char *fmt, ...)
132 va_list va;
133 char buff[256];
135 va_start(va, fmt);
136 vsprintf(buff, fmt, va);
137 va_end(va);
139 printascii(buff);
142 #define dbg(x...) s3c24xx_serial_dbg(x)
144 #else
145 #define dbg(x...) printk(KERN_DEBUG "s3c24xx: ");
146 #endif
147 #else /* no debug */
148 #define dbg(x...) do {} while(0)
149 #endif
151 /* UART name and device definitions */
153 #define S3C24XX_SERIAL_NAME "ttySAC"
154 #define S3C24XX_SERIAL_DEVFS "tts/"
155 #define S3C24XX_SERIAL_MAJOR 204
156 #define S3C24XX_SERIAL_MINOR 64
159 /* conversion functions */
161 #define s3c24xx_dev_to_port(__dev) (struct uart_port *)dev_get_drvdata(__dev)
162 #define s3c24xx_dev_to_cfg(__dev) (struct s3c2410_uartcfg *)((__dev)->platform_data)
164 /* we can support 3 uarts, but not always use them */
166 #define NR_PORTS (3)
168 /* port irq numbers */
170 #define TX_IRQ(port) ((port)->irq + 1)
171 #define RX_IRQ(port) ((port)->irq)
173 /* register access controls */
175 #define portaddr(port, reg) ((port)->membase + (reg))
177 #define rd_regb(port, reg) (__raw_readb(portaddr(port, reg)))
178 #define rd_regl(port, reg) (__raw_readl(portaddr(port, reg)))
180 #define wr_regb(port, reg, val) \
181 do { __raw_writeb(val, portaddr(port, reg)); } while(0)
183 #define wr_regl(port, reg, val) \
184 do { __raw_writel(val, portaddr(port, reg)); } while(0)
186 /* macros to change one thing to another */
188 #define tx_enabled(port) ((port)->unused[0])
189 #define rx_enabled(port) ((port)->unused[1])
191 /* flag to ignore all characters comming in */
192 #define RXSTAT_DUMMY_READ (0x10000000)
194 static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
196 return container_of(port, struct s3c24xx_uart_port, port);
199 /* translate a port to the device name */
201 static inline char *s3c24xx_serial_portname(struct uart_port *port)
203 return to_platform_device(port->dev)->name;
206 static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
208 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
211 static void s3c24xx_serial_rx_enable(struct uart_port *port)
213 unsigned long flags;
214 unsigned int ucon, ufcon;
215 int count = 10000;
217 spin_lock_irqsave(&port->lock, flags);
219 while (--count && !s3c24xx_serial_txempty_nofifo(port))
220 udelay(100);
222 ufcon = rd_regl(port, S3C2410_UFCON);
223 ufcon |= S3C2410_UFCON_RESETRX;
224 wr_regl(port, S3C2410_UFCON, ufcon);
226 ucon = rd_regl(port, S3C2410_UCON);
227 ucon |= S3C2410_UCON_RXIRQMODE;
228 wr_regl(port, S3C2410_UCON, ucon);
230 rx_enabled(port) = 1;
231 spin_unlock_irqrestore(&port->lock, flags);
234 static void s3c24xx_serial_rx_disable(struct uart_port *port)
236 unsigned long flags;
237 unsigned int ucon;
239 spin_lock_irqsave(&port->lock, flags);
241 ucon = rd_regl(port, S3C2410_UCON);
242 ucon &= ~S3C2410_UCON_RXIRQMODE;
243 wr_regl(port, S3C2410_UCON, ucon);
245 rx_enabled(port) = 0;
246 spin_unlock_irqrestore(&port->lock, flags);
249 static void
250 s3c24xx_serial_stop_tx(struct uart_port *port, unsigned int tty_stop)
252 if (tx_enabled(port)) {
253 disable_irq(TX_IRQ(port));
254 tx_enabled(port) = 0;
255 if (port->flags & UPF_CONS_FLOW)
256 s3c24xx_serial_rx_enable(port);
260 static void
261 s3c24xx_serial_start_tx(struct uart_port *port, unsigned int tty_start)
263 if (!tx_enabled(port)) {
264 if (port->flags & UPF_CONS_FLOW)
265 s3c24xx_serial_rx_disable(port);
267 enable_irq(TX_IRQ(port));
268 tx_enabled(port) = 1;
273 static void s3c24xx_serial_stop_rx(struct uart_port *port)
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;
282 static void s3c24xx_serial_enable_ms(struct uart_port *port)
286 static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
288 return to_ourport(port)->info;
291 static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
293 if (port->dev == NULL)
294 return NULL;
296 return (struct s3c2410_uartcfg *)port->dev->platform_data;
299 static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
300 unsigned long ufstat)
302 struct s3c24xx_uart_info *info = ourport->info;
304 if (ufstat & info->rx_fifofull)
305 return info->fifosize;
307 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
311 /* ? - where has parity gone?? */
312 #define S3C2410_UERSTAT_PARITY (0x1000)
314 static irqreturn_t
315 s3c24xx_serial_rx_chars(int irq, void *dev_id, struct pt_regs *regs)
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;
323 while (max_count-- > 0) {
324 ufcon = rd_regl(port, S3C2410_UFCON);
325 ufstat = rd_regl(port, S3C2410_UFSTAT);
327 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
328 break;
330 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
331 if (tty->low_latency)
332 tty_flip_buffer_push(tty);
335 * If this failed then we will throw away the
336 * bytes but must do so to clear interrupts
340 uerstat = rd_regl(port, S3C2410_UERSTAT);
341 ch = rd_regb(port, S3C2410_URXH);
343 if (port->flags & UPF_CONS_FLOW) {
344 int txe = s3c24xx_serial_txempty_nofifo(port);
346 if (rx_enabled(port)) {
347 if (!txe) {
348 rx_enabled(port) = 0;
349 continue;
351 } else {
352 if (txe) {
353 ufcon |= S3C2410_UFCON_RESETRX;
354 wr_regl(port, S3C2410_UFCON, ufcon);
355 rx_enabled(port) = 1;
356 goto out;
358 continue;
362 /* insert the character into the buffer */
364 flag = TTY_NORMAL;
365 port->icount.rx++;
367 if (uerstat & S3C2410_UERSTAT_ANY) {
368 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
369 ch, uerstat);
371 /* check for break */
372 if (uerstat & S3C2410_UERSTAT_BREAK) {
373 dbg("break!\n");
374 port->icount.brk++;
375 if (uart_handle_break(port))
376 goto ignore_char;
379 if (uerstat & S3C2410_UERSTAT_FRAME)
380 port->icount.frame++;
381 if (uerstat & S3C2410_UERSTAT_OVERRUN)
382 port->icount.overrun++;
384 uerstat &= port->read_status_mask;
386 if (uerstat & S3C2410_UERSTAT_BREAK)
387 flag = TTY_BREAK;
388 else if (uerstat & S3C2410_UERSTAT_PARITY)
389 flag = TTY_PARITY;
390 else if (uerstat & ( S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_OVERRUN))
391 flag = TTY_FRAME;
394 if (uart_handle_sysrq_char(port, ch, regs))
395 goto ignore_char;
397 if ((uerstat & port->ignore_status_mask) == 0) {
398 tty_insert_flip_char(tty, ch, flag);
401 if ((uerstat & S3C2410_UERSTAT_OVERRUN) &&
402 tty->flip.count < TTY_FLIPBUF_SIZE) {
404 * Overrun is special, since it's reported
405 * immediately, and doesn't affect the current
406 * character.
409 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
412 ignore_char:
413 continue;
415 tty_flip_buffer_push(tty);
417 out:
418 return IRQ_HANDLED;
421 static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id, struct pt_regs *regs)
423 struct s3c24xx_uart_port *ourport = id;
424 struct uart_port *port = &ourport->port;
425 struct circ_buf *xmit = &port->info->xmit;
426 int count = 256;
428 if (port->x_char) {
429 wr_regb(port, S3C2410_UTXH, port->x_char);
430 port->icount.tx++;
431 port->x_char = 0;
432 goto out;
435 /* if there isnt anything more to transmit, or the uart is now
436 * stopped, disable the uart and exit
439 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
440 s3c24xx_serial_stop_tx(port, 0);
441 goto out;
444 /* try and drain the buffer... */
446 while (!uart_circ_empty(xmit) && count-- > 0) {
447 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
448 break;
450 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
451 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
452 port->icount.tx++;
455 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
456 uart_write_wakeup(port);
458 if (uart_circ_empty(xmit))
459 s3c24xx_serial_stop_tx(port, 0);
461 out:
462 return IRQ_HANDLED;
465 static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
467 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
468 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
469 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
471 if (ufcon & S3C2410_UFCON_FIFOMODE) {
472 if ((ufstat & info->tx_fifomask) != 0 ||
473 (ufstat & info->tx_fifofull))
474 return 0;
476 return 1;
479 return s3c24xx_serial_txempty_nofifo(port);
482 /* no modem control lines */
483 static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
485 unsigned int umstat = rd_regb(port,S3C2410_UMSTAT);
487 if (umstat & S3C2410_UMSTAT_CTS)
488 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
489 else
490 return TIOCM_CAR | TIOCM_DSR;
493 static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
495 /* todo - possibly remove AFC and do manual CTS */
498 static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
500 unsigned long flags;
501 unsigned int ucon;
503 spin_lock_irqsave(&port->lock, flags);
505 ucon = rd_regl(port, S3C2410_UCON);
507 if (break_state)
508 ucon |= S3C2410_UCON_SBREAK;
509 else
510 ucon &= ~S3C2410_UCON_SBREAK;
512 wr_regl(port, S3C2410_UCON, ucon);
514 spin_unlock_irqrestore(&port->lock, flags);
517 static void s3c24xx_serial_shutdown(struct uart_port *port)
519 struct s3c24xx_uart_port *ourport = to_ourport(port);
521 if (ourport->tx_claimed) {
522 free_irq(TX_IRQ(port), ourport);
523 tx_enabled(port) = 0;
524 ourport->tx_claimed = 0;
527 if (ourport->rx_claimed) {
528 free_irq(RX_IRQ(port), ourport);
529 ourport->rx_claimed = 0;
530 rx_enabled(port) = 0;
535 static int s3c24xx_serial_startup(struct uart_port *port)
537 struct s3c24xx_uart_port *ourport = to_ourport(port);
538 unsigned long flags;
539 int ret;
541 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
542 port->mapbase, port->membase);
544 local_irq_save(flags);
546 rx_enabled(port) = 1;
548 ret = request_irq(RX_IRQ(port),
549 s3c24xx_serial_rx_chars, 0,
550 s3c24xx_serial_portname(port), ourport);
552 if (ret != 0) {
553 printk(KERN_ERR "cannot get irq %d\n", RX_IRQ(port));
554 return ret;
557 ourport->rx_claimed = 1;
559 dbg("requesting tx irq...\n");
561 tx_enabled(port) = 1;
563 ret = request_irq(TX_IRQ(port),
564 s3c24xx_serial_tx_chars, 0,
565 s3c24xx_serial_portname(port), ourport);
567 if (ret) {
568 printk(KERN_ERR "cannot get irq %d\n", TX_IRQ(port));
569 goto err;
572 ourport->tx_claimed = 1;
574 dbg("s3c24xx_serial_startup ok\n");
576 /* the port reset code should have done the correct
577 * register setup for the port controls */
579 local_irq_restore(flags);
580 return ret;
582 err:
583 s3c24xx_serial_shutdown(port);
584 local_irq_restore(flags);
585 return ret;
588 /* power power management control */
590 static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
591 unsigned int old)
593 struct s3c24xx_uart_port *ourport = to_ourport(port);
595 switch (level) {
596 case 3:
597 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
598 clk_disable(ourport->baudclk);
600 clk_disable(ourport->clk);
601 break;
603 case 0:
604 clk_enable(ourport->clk);
606 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
607 clk_enable(ourport->baudclk);
609 break;
610 default:
611 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
615 /* baud rate calculation
617 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
618 * of different sources, including the peripheral clock ("pclk") and an
619 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
620 * with a programmable extra divisor.
622 * The following code goes through the clock sources, and calculates the
623 * baud clocks (and the resultant actual baud rates) and then tries to
624 * pick the closest one and select that.
629 #define MAX_CLKS (8)
631 static struct s3c24xx_uart_clksrc tmp_clksrc = {
632 .name = "pclk",
633 .min_baud = 0,
634 .max_baud = 0,
635 .divisor = 1,
638 static inline int
639 s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
641 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
643 return (info->get_clksrc)(port, c);
646 static inline int
647 s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
649 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
651 return (info->set_clksrc)(port, c);
654 struct baud_calc {
655 struct s3c24xx_uart_clksrc *clksrc;
656 unsigned int calc;
657 unsigned int quot;
658 struct clk *src;
661 static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
662 struct uart_port *port,
663 struct s3c24xx_uart_clksrc *clksrc,
664 unsigned int baud)
666 unsigned long rate;
668 calc->src = clk_get(port->dev, clksrc->name);
669 if (calc->src == NULL || IS_ERR(calc->src))
670 return 0;
672 rate = clk_get_rate(calc->src);
673 rate /= clksrc->divisor;
675 calc->clksrc = clksrc;
676 calc->quot = (rate + (8 * baud)) / (16 * baud);
677 calc->calc = (rate / (calc->quot * 16));
679 calc->quot--;
680 return 1;
683 static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
684 struct s3c24xx_uart_clksrc **clksrc,
685 struct clk **clk,
686 unsigned int baud)
688 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
689 struct s3c24xx_uart_clksrc *clkp;
690 struct baud_calc res[MAX_CLKS];
691 struct baud_calc *resptr, *best, *sptr;
692 int i;
694 clkp = cfg->clocks;
695 best = NULL;
697 if (cfg->clocks_size < 2) {
698 if (cfg->clocks_size == 0)
699 clkp = &tmp_clksrc;
701 /* check to see if we're sourcing fclk, and if so we're
702 * going to have to update the clock source
705 if (strcmp(clkp->name, "fclk") == 0) {
706 struct s3c24xx_uart_clksrc src;
708 s3c24xx_serial_getsource(port, &src);
710 /* check that the port already using fclk, and if
711 * not, then re-select fclk
714 if (strcmp(src.name, clkp->name) == 0) {
715 s3c24xx_serial_setsource(port, clkp);
716 s3c24xx_serial_getsource(port, &src);
719 clkp->divisor = src.divisor;
722 s3c24xx_serial_calcbaud(res, port, clkp, baud);
723 best = res;
724 resptr = best + 1;
725 } else {
726 resptr = res;
728 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
729 if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
730 resptr++;
734 /* ok, we now need to select the best clock we found */
736 if (!best) {
737 unsigned int deviation = (1<<30)|((1<<30)-1);
738 int calc_deviation;
740 for (sptr = res; sptr < resptr; sptr++) {
741 printk(KERN_DEBUG
742 "found clk %p (%s) quot %d, calc %d\n",
743 sptr->clksrc, sptr->clksrc->name,
744 sptr->quot, sptr->calc);
746 calc_deviation = baud - sptr->calc;
747 if (calc_deviation < 0)
748 calc_deviation = -calc_deviation;
750 if (calc_deviation < deviation) {
751 best = sptr;
752 deviation = calc_deviation;
756 printk(KERN_DEBUG "best %p (deviation %d)\n", best, deviation);
759 printk(KERN_DEBUG "selected clock %p (%s) quot %d, calc %d\n",
760 best->clksrc, best->clksrc->name, best->quot, best->calc);
762 /* store results to pass back */
764 *clksrc = best->clksrc;
765 *clk = best->src;
767 return best->quot;
770 static void s3c24xx_serial_set_termios(struct uart_port *port,
771 struct termios *termios,
772 struct termios *old)
774 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
775 struct s3c24xx_uart_port *ourport = to_ourport(port);
776 struct s3c24xx_uart_clksrc *clksrc;
777 struct clk *clk;
778 unsigned long flags;
779 unsigned int baud, quot;
780 unsigned int ulcon;
781 unsigned int umcon;
784 * We don't support modem control lines.
786 termios->c_cflag &= ~(HUPCL | CMSPAR);
787 termios->c_cflag |= CLOCAL;
790 * Ask the core to calculate the divisor for us.
793 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
795 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
796 quot = port->custom_divisor;
797 else
798 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
800 /* check to see if we need to change clock source */
802 if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
803 s3c24xx_serial_setsource(port, clksrc);
805 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
806 clk_disable(ourport->baudclk);
807 clk_unuse(ourport->baudclk);
808 ourport->baudclk = NULL;
811 clk_use(clk);
812 clk_enable(clk);
814 ourport->clksrc = clksrc;
815 ourport->baudclk = clk;
818 switch (termios->c_cflag & CSIZE) {
819 case CS5:
820 dbg("config: 5bits/char\n");
821 ulcon = S3C2410_LCON_CS5;
822 break;
823 case CS6:
824 dbg("config: 6bits/char\n");
825 ulcon = S3C2410_LCON_CS6;
826 break;
827 case CS7:
828 dbg("config: 7bits/char\n");
829 ulcon = S3C2410_LCON_CS7;
830 break;
831 case CS8:
832 default:
833 dbg("config: 8bits/char\n");
834 ulcon = S3C2410_LCON_CS8;
835 break;
838 /* preserve original lcon IR settings */
839 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
841 if (termios->c_cflag & CSTOPB)
842 ulcon |= S3C2410_LCON_STOPB;
844 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
846 if (termios->c_cflag & PARENB) {
847 if (termios->c_cflag & PARODD)
848 ulcon |= S3C2410_LCON_PODD;
849 else
850 ulcon |= S3C2410_LCON_PEVEN;
851 } else {
852 ulcon |= S3C2410_LCON_PNONE;
855 spin_lock_irqsave(&port->lock, flags);
857 dbg("setting ulcon to %08x, brddiv to %d\n", ulcon, quot);
859 wr_regl(port, S3C2410_ULCON, ulcon);
860 wr_regl(port, S3C2410_UBRDIV, quot);
861 wr_regl(port, S3C2410_UMCON, umcon);
863 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
864 rd_regl(port, S3C2410_ULCON),
865 rd_regl(port, S3C2410_UCON),
866 rd_regl(port, S3C2410_UFCON));
869 * Update the per-port timeout.
871 uart_update_timeout(port, termios->c_cflag, baud);
874 * Which character status flags are we interested in?
876 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
877 if (termios->c_iflag & INPCK)
878 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
881 * Which character status flags should we ignore?
883 port->ignore_status_mask = 0;
884 if (termios->c_iflag & IGNPAR)
885 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
886 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
887 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
890 * Ignore all characters if CREAD is not set.
892 if ((termios->c_cflag & CREAD) == 0)
893 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
895 spin_unlock_irqrestore(&port->lock, flags);
898 static const char *s3c24xx_serial_type(struct uart_port *port)
900 switch (port->type) {
901 case PORT_S3C2410:
902 return "S3C2410";
903 case PORT_S3C2440:
904 return "S3C2440";
905 default:
906 return NULL;
910 #define MAP_SIZE (0x100)
912 static void s3c24xx_serial_release_port(struct uart_port *port)
914 release_mem_region(port->mapbase, MAP_SIZE);
917 static int s3c24xx_serial_request_port(struct uart_port *port)
919 char *name = s3c24xx_serial_portname(port);
920 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
923 static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
925 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
927 if (flags & UART_CONFIG_TYPE &&
928 s3c24xx_serial_request_port(port) == 0)
929 port->type = info->type;
933 * verify the new serial_struct (for TIOCSSERIAL).
935 static int
936 s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
938 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
940 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
941 return -EINVAL;
943 return 0;
947 #ifdef CONFIG_SERIAL_S3C2410_CONSOLE
949 static struct console s3c24xx_serial_console;
951 #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
952 #else
953 #define S3C24XX_SERIAL_CONSOLE NULL
954 #endif
956 static struct uart_ops s3c24xx_serial_ops = {
957 .pm = s3c24xx_serial_pm,
958 .tx_empty = s3c24xx_serial_tx_empty,
959 .get_mctrl = s3c24xx_serial_get_mctrl,
960 .set_mctrl = s3c24xx_serial_set_mctrl,
961 .stop_tx = s3c24xx_serial_stop_tx,
962 .start_tx = s3c24xx_serial_start_tx,
963 .stop_rx = s3c24xx_serial_stop_rx,
964 .enable_ms = s3c24xx_serial_enable_ms,
965 .break_ctl = s3c24xx_serial_break_ctl,
966 .startup = s3c24xx_serial_startup,
967 .shutdown = s3c24xx_serial_shutdown,
968 .set_termios = s3c24xx_serial_set_termios,
969 .type = s3c24xx_serial_type,
970 .release_port = s3c24xx_serial_release_port,
971 .request_port = s3c24xx_serial_request_port,
972 .config_port = s3c24xx_serial_config_port,
973 .verify_port = s3c24xx_serial_verify_port,
977 static struct uart_driver s3c24xx_uart_drv = {
978 .owner = THIS_MODULE,
979 .dev_name = "s3c2410_serial",
980 .nr = 3,
981 .cons = S3C24XX_SERIAL_CONSOLE,
982 .driver_name = S3C24XX_SERIAL_NAME,
983 .devfs_name = S3C24XX_SERIAL_DEVFS,
984 .major = S3C24XX_SERIAL_MAJOR,
985 .minor = S3C24XX_SERIAL_MINOR,
988 static struct s3c24xx_uart_port s3c24xx_serial_ports[NR_PORTS] = {
989 [0] = {
990 .port = {
991 .lock = SPIN_LOCK_UNLOCKED,
992 .iotype = UPIO_MEM,
993 .irq = IRQ_S3CUART_RX0,
994 .uartclk = 0,
995 .fifosize = 16,
996 .ops = &s3c24xx_serial_ops,
997 .flags = UPF_BOOT_AUTOCONF,
998 .line = 0,
1001 [1] = {
1002 .port = {
1003 .lock = SPIN_LOCK_UNLOCKED,
1004 .iotype = UPIO_MEM,
1005 .irq = IRQ_S3CUART_RX1,
1006 .uartclk = 0,
1007 .fifosize = 16,
1008 .ops = &s3c24xx_serial_ops,
1009 .flags = UPF_BOOT_AUTOCONF,
1010 .line = 1,
1013 #if NR_PORTS > 2
1015 [2] = {
1016 .port = {
1017 .lock = SPIN_LOCK_UNLOCKED,
1018 .iotype = UPIO_MEM,
1019 .irq = IRQ_S3CUART_RX2,
1020 .uartclk = 0,
1021 .fifosize = 16,
1022 .ops = &s3c24xx_serial_ops,
1023 .flags = UPF_BOOT_AUTOCONF,
1024 .line = 2,
1027 #endif
1030 /* s3c24xx_serial_resetport
1032 * wrapper to call the specific reset for this port (reset the fifos
1033 * and the settings)
1036 static inline int s3c24xx_serial_resetport(struct uart_port * port,
1037 struct s3c2410_uartcfg *cfg)
1039 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1041 return (info->reset_port)(port, cfg);
1044 /* s3c24xx_serial_init_port
1046 * initialise a single serial port from the platform device given
1049 static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1050 struct s3c24xx_uart_info *info,
1051 struct platform_device *platdev)
1053 struct uart_port *port = &ourport->port;
1054 struct s3c2410_uartcfg *cfg;
1055 struct resource *res;
1057 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1059 if (platdev == NULL)
1060 return -ENODEV;
1062 cfg = s3c24xx_dev_to_cfg(&platdev->dev);
1064 if (port->mapbase != 0)
1065 return 0;
1067 if (cfg->hwport > 3)
1068 return -EINVAL;
1070 /* setup info for port */
1071 port->dev = &platdev->dev;
1072 ourport->info = info;
1074 /* copy the info in from provided structure */
1075 ourport->port.fifosize = info->fifosize;
1077 dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
1079 port->uartclk = 1;
1081 if (cfg->uart_flags & UPF_CONS_FLOW) {
1082 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1083 port->flags |= UPF_CONS_FLOW;
1086 /* sort our the physical and virtual addresses for each UART */
1088 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1089 if (res == NULL) {
1090 printk(KERN_ERR "failed to find memory resource for uart\n");
1091 return -EINVAL;
1094 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1096 port->mapbase = res->start;
1097 port->membase = S3C24XX_VA_UART + (res->start - S3C2410_PA_UART);
1098 port->irq = platform_get_irq(platdev, 0);
1100 ourport->clk = clk_get(&platdev->dev, "uart");
1102 if (ourport->clk != NULL && !IS_ERR(ourport->clk))
1103 clk_use(ourport->clk);
1105 dbg("port: map=%08x, mem=%08x, irq=%d, clock=%ld\n",
1106 port->mapbase, port->membase, port->irq, port->uartclk);
1108 /* reset the fifos (and setup the uart) */
1109 s3c24xx_serial_resetport(port, cfg);
1110 return 0;
1113 /* Device driver serial port probe */
1115 static int probe_index = 0;
1117 int s3c24xx_serial_probe(struct device *_dev,
1118 struct s3c24xx_uart_info *info)
1120 struct s3c24xx_uart_port *ourport;
1121 struct platform_device *dev = to_platform_device(_dev);
1122 int ret;
1124 dbg("s3c24xx_serial_probe(%p, %p) %d\n", _dev, info, probe_index);
1126 ourport = &s3c24xx_serial_ports[probe_index];
1127 probe_index++;
1129 dbg("%s: initialising port %p...\n", __FUNCTION__, ourport);
1131 ret = s3c24xx_serial_init_port(ourport, info, dev);
1132 if (ret < 0)
1133 goto probe_err;
1135 dbg("%s: adding port\n", __FUNCTION__);
1136 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1137 dev_set_drvdata(_dev, &ourport->port);
1139 return 0;
1141 probe_err:
1142 return ret;
1145 int s3c24xx_serial_remove(struct device *_dev)
1147 struct uart_port *port = s3c24xx_dev_to_port(_dev);
1149 if (port)
1150 uart_remove_one_port(&s3c24xx_uart_drv, port);
1152 return 0;
1155 /* UART power management code */
1157 #ifdef CONFIG_PM
1159 int s3c24xx_serial_suspend(struct device *dev, u32 state, u32 level)
1161 struct uart_port *port = s3c24xx_dev_to_port(dev);
1163 if (port && level == SUSPEND_DISABLE)
1164 uart_suspend_port(&s3c24xx_uart_drv, port);
1166 return 0;
1169 int s3c24xx_serial_resume(struct device *dev, u32 level)
1171 struct uart_port *port = s3c24xx_dev_to_port(dev);
1172 struct s3c24xx_uart_port *ourport = to_ourport(port);
1174 if (port && level == RESUME_ENABLE) {
1175 clk_enable(ourport->clk);
1176 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1177 clk_disable(ourport->clk);
1179 uart_resume_port(&s3c24xx_uart_drv, port);
1182 return 0;
1185 #else
1186 #define s3c24xx_serial_suspend NULL
1187 #define s3c24xx_serial_resume NULL
1188 #endif
1190 int s3c24xx_serial_init(struct device_driver *drv,
1191 struct s3c24xx_uart_info *info)
1193 dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
1194 return driver_register(drv);
1198 /* now comes the code to initialise either the s3c2410 or s3c2440 serial
1199 * port information
1202 /* cpu specific variations on the serial port support */
1204 #ifdef CONFIG_CPU_S3C2400
1206 static int s3c2400_serial_getsource(struct uart_port *port,
1207 struct s3c24xx_uart_clksrc *clk)
1209 clk->divisor = 1;
1210 clk->name = "pclk";
1212 return 0;
1215 static int s3c2400_serial_setsource(struct uart_port *port,
1216 struct s3c24xx_uart_clksrc *clk)
1218 return 0;
1221 static int s3c2400_serial_resetport(struct uart_port *port,
1222 struct s3c2410_uartcfg *cfg)
1224 dbg("s3c2400_serial_resetport: port=%p (%08lx), cfg=%p\n",
1225 port, port->mapbase, cfg);
1227 wr_regl(port, S3C2410_UCON, cfg->ucon);
1228 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
1230 /* reset both fifos */
1232 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1233 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1235 return 0;
1238 static struct s3c24xx_uart_info s3c2400_uart_inf = {
1239 .name = "Samsung S3C2400 UART",
1240 .type = PORT_S3C2400,
1241 .fifosize = 16,
1242 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
1243 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
1244 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
1245 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
1246 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
1247 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
1248 .get_clksrc = s3c2400_serial_getsource,
1249 .set_clksrc = s3c2400_serial_setsource,
1250 .reset_port = s3c2400_serial_resetport,
1253 static int s3c2400_serial_probe(struct device *dev)
1255 return s3c24xx_serial_probe(dev, &s3c2400_uart_inf);
1258 static struct device_driver s3c2400_serial_drv = {
1259 .name = "s3c2400-uart",
1260 .bus = &platform_bus_type,
1261 .probe = s3c2400_serial_probe,
1262 .remove = s3c24xx_serial_remove,
1263 .suspend = s3c24xx_serial_suspend,
1264 .resume = s3c24xx_serial_resume,
1267 static inline int s3c2400_serial_init(void)
1269 return s3c24xx_serial_init(&s3c2400_serial_drv, &s3c2400_uart_inf);
1272 static inline void s3c2400_serial_exit(void)
1274 driver_unregister(&s3c2400_serial_drv);
1277 #define s3c2400_uart_inf_at &s3c2400_uart_inf
1278 #else
1280 static inline int s3c2400_serial_init(void)
1282 return 0;
1285 static inline void s3c2400_serial_exit(void)
1289 #define s3c2400_uart_inf_at NULL
1291 #endif /* CONFIG_CPU_S3C2400 */
1293 /* S3C2410 support */
1295 #ifdef CONFIG_CPU_S3C2410
1297 static int s3c2410_serial_setsource(struct uart_port *port,
1298 struct s3c24xx_uart_clksrc *clk)
1300 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1302 if (strcmp(clk->name, "uclk") == 0)
1303 ucon |= S3C2410_UCON_UCLK;
1304 else
1305 ucon &= ~S3C2410_UCON_UCLK;
1307 wr_regl(port, S3C2410_UCON, ucon);
1308 return 0;
1311 static int s3c2410_serial_getsource(struct uart_port *port,
1312 struct s3c24xx_uart_clksrc *clk)
1314 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1316 clk->divisor = 1;
1317 clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
1319 return 0;
1322 static int s3c2410_serial_resetport(struct uart_port *port,
1323 struct s3c2410_uartcfg *cfg)
1325 dbg("s3c2410_serial_resetport: port=%p (%08lx), cfg=%p\n",
1326 port, port->mapbase, cfg);
1328 wr_regl(port, S3C2410_UCON, cfg->ucon);
1329 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
1331 /* reset both fifos */
1333 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1334 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1336 return 0;
1339 static struct s3c24xx_uart_info s3c2410_uart_inf = {
1340 .name = "Samsung S3C2410 UART",
1341 .type = PORT_S3C2410,
1342 .fifosize = 16,
1343 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
1344 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
1345 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
1346 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
1347 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
1348 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
1349 .get_clksrc = s3c2410_serial_getsource,
1350 .set_clksrc = s3c2410_serial_setsource,
1351 .reset_port = s3c2410_serial_resetport,
1354 /* device management */
1356 static int s3c2410_serial_probe(struct device *dev)
1358 return s3c24xx_serial_probe(dev, &s3c2410_uart_inf);
1361 static struct device_driver s3c2410_serial_drv = {
1362 .name = "s3c2410-uart",
1363 .bus = &platform_bus_type,
1364 .probe = s3c2410_serial_probe,
1365 .remove = s3c24xx_serial_remove,
1366 .suspend = s3c24xx_serial_suspend,
1367 .resume = s3c24xx_serial_resume,
1370 static inline int s3c2410_serial_init(void)
1372 return s3c24xx_serial_init(&s3c2410_serial_drv, &s3c2410_uart_inf);
1375 static inline void s3c2410_serial_exit(void)
1377 driver_unregister(&s3c2410_serial_drv);
1380 #define s3c2410_uart_inf_at &s3c2410_uart_inf
1381 #else
1383 static inline int s3c2410_serial_init(void)
1385 return 0;
1388 static inline void s3c2410_serial_exit(void)
1392 #define s3c2410_uart_inf_at NULL
1394 #endif /* CONFIG_CPU_S3C2410 */
1396 #ifdef CONFIG_CPU_S3C2440
1398 static int s3c2440_serial_setsource(struct uart_port *port,
1399 struct s3c24xx_uart_clksrc *clk)
1401 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1403 // todo - proper fclk<>nonfclk switch //
1405 ucon &= ~S3C2440_UCON_CLKMASK;
1407 if (strcmp(clk->name, "uclk") == 0)
1408 ucon |= S3C2440_UCON_UCLK;
1409 else if (strcmp(clk->name, "pclk") == 0)
1410 ucon |= S3C2440_UCON_PCLK;
1411 else if (strcmp(clk->name, "fclk") == 0)
1412 ucon |= S3C2440_UCON_FCLK;
1413 else {
1414 printk(KERN_ERR "unknown clock source %s\n", clk->name);
1415 return -EINVAL;
1418 wr_regl(port, S3C2410_UCON, ucon);
1419 return 0;
1423 static int s3c2440_serial_getsource(struct uart_port *port,
1424 struct s3c24xx_uart_clksrc *clk)
1426 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1427 unsigned long ucon0, ucon1, ucon2;
1429 switch (ucon & S3C2440_UCON_CLKMASK) {
1430 case S3C2440_UCON_UCLK:
1431 clk->divisor = 1;
1432 clk->name = "uclk";
1433 break;
1435 case S3C2440_UCON_PCLK:
1436 case S3C2440_UCON_PCLK2:
1437 clk->divisor = 1;
1438 clk->name = "pclk";
1439 break;
1441 case S3C2440_UCON_FCLK:
1442 /* the fun of calculating the uart divisors on
1443 * the s3c2440 */
1445 ucon0 = __raw_readl(S3C24XX_VA_UART0 + S3C2410_UCON);
1446 ucon1 = __raw_readl(S3C24XX_VA_UART1 + S3C2410_UCON);
1447 ucon2 = __raw_readl(S3C24XX_VA_UART2 + S3C2410_UCON);
1449 printk("ucons: %08lx, %08lx, %08lx\n", ucon0, ucon1, ucon2);
1451 ucon0 &= S3C2440_UCON0_DIVMASK;
1452 ucon1 &= S3C2440_UCON1_DIVMASK;
1453 ucon2 &= S3C2440_UCON2_DIVMASK;
1455 if (ucon0 != 0) {
1456 clk->divisor = ucon0 >> S3C2440_UCON_DIVSHIFT;
1457 clk->divisor += 6;
1458 } else if (ucon1 != 0) {
1459 clk->divisor = ucon1 >> S3C2440_UCON_DIVSHIFT;
1460 clk->divisor += 21;
1461 } else if (ucon2 != 0) {
1462 clk->divisor = ucon2 >> S3C2440_UCON_DIVSHIFT;
1463 clk->divisor += 36;
1464 } else {
1465 /* manual calims 44, seems to be 9 */
1466 clk->divisor = 9;
1469 clk->name = "fclk";
1470 break;
1473 return 0;
1476 static int s3c2440_serial_resetport(struct uart_port *port,
1477 struct s3c2410_uartcfg *cfg)
1479 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1481 dbg("s3c2440_serial_resetport: port=%p (%08lx), cfg=%p\n",
1482 port, port->mapbase, cfg);
1484 /* ensure we don't change the clock settings... */
1486 ucon &= (S3C2440_UCON0_DIVMASK | (3<<10));
1488 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
1489 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
1491 /* reset both fifos */
1493 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1494 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1496 return 0;
1499 static struct s3c24xx_uart_info s3c2440_uart_inf = {
1500 .name = "Samsung S3C2440 UART",
1501 .type = PORT_S3C2440,
1502 .fifosize = 64,
1503 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1504 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1505 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1506 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1507 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1508 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1509 .get_clksrc = s3c2440_serial_getsource,
1510 .set_clksrc = s3c2440_serial_setsource,
1511 .reset_port = s3c2440_serial_resetport,
1514 /* device management */
1516 static int s3c2440_serial_probe(struct device *dev)
1518 dbg("s3c2440_serial_probe: dev=%p\n", dev);
1519 return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
1522 static struct device_driver s3c2440_serial_drv = {
1523 .name = "s3c2440-uart",
1524 .bus = &platform_bus_type,
1525 .probe = s3c2440_serial_probe,
1526 .remove = s3c24xx_serial_remove,
1527 .suspend = s3c24xx_serial_suspend,
1528 .resume = s3c24xx_serial_resume,
1532 static inline int s3c2440_serial_init(void)
1534 return s3c24xx_serial_init(&s3c2440_serial_drv, &s3c2440_uart_inf);
1537 static inline void s3c2440_serial_exit(void)
1539 driver_unregister(&s3c2440_serial_drv);
1542 #define s3c2440_uart_inf_at &s3c2440_uart_inf
1543 #else
1545 static inline int s3c2440_serial_init(void)
1547 return 0;
1550 static inline void s3c2440_serial_exit(void)
1554 #define s3c2440_uart_inf_at NULL
1555 #endif /* CONFIG_CPU_S3C2440 */
1557 /* module initialisation code */
1559 static int __init s3c24xx_serial_modinit(void)
1561 int ret;
1563 ret = uart_register_driver(&s3c24xx_uart_drv);
1564 if (ret < 0) {
1565 printk(KERN_ERR "failed to register UART driver\n");
1566 return -1;
1569 s3c2400_serial_init();
1570 s3c2410_serial_init();
1571 s3c2440_serial_init();
1573 return 0;
1576 static void __exit s3c24xx_serial_modexit(void)
1578 s3c2400_serial_exit();
1579 s3c2410_serial_exit();
1580 s3c2440_serial_exit();
1582 uart_unregister_driver(&s3c24xx_uart_drv);
1586 module_init(s3c24xx_serial_modinit);
1587 module_exit(s3c24xx_serial_modexit);
1589 /* Console code */
1591 #ifdef CONFIG_SERIAL_S3C2410_CONSOLE
1593 static struct uart_port *cons_uart;
1595 static int
1596 s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1598 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1599 unsigned long ufstat, utrstat;
1601 if (ufcon & S3C2410_UFCON_FIFOMODE) {
1602 /* fifo mode - check ammount of data in fifo registers... */
1604 ufstat = rd_regl(port, S3C2410_UFSTAT);
1605 return (ufstat & info->tx_fifofull) ? 0 : 1;
1608 /* in non-fifo mode, we go and use the tx buffer empty */
1610 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1611 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1614 static void
1615 s3c24xx_serial_console_write(struct console *co, const char *s,
1616 unsigned int count)
1618 int i;
1619 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1621 for (i = 0; i < count; i++) {
1622 while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
1623 barrier();
1625 wr_regb(cons_uart, S3C2410_UTXH, s[i]);
1627 if (s[i] == '\n') {
1628 while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
1629 barrier();
1631 wr_regb(cons_uart, S3C2410_UTXH, '\r');
1636 static void __init
1637 s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1638 int *parity, int *bits)
1640 struct s3c24xx_uart_clksrc clksrc;
1641 struct clk *clk;
1642 unsigned int ulcon;
1643 unsigned int ucon;
1644 unsigned int ubrdiv;
1645 unsigned long rate;
1647 ulcon = rd_regl(port, S3C2410_ULCON);
1648 ucon = rd_regl(port, S3C2410_UCON);
1649 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1651 dbg("s3c24xx_serial_get_options: port=%p\n"
1652 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1653 port, ulcon, ucon, ubrdiv);
1655 if ((ucon & 0xf) != 0) {
1656 /* consider the serial port configured if the tx/rx mode set */
1658 switch (ulcon & S3C2410_LCON_CSMASK) {
1659 case S3C2410_LCON_CS5:
1660 *bits = 5;
1661 break;
1662 case S3C2410_LCON_CS6:
1663 *bits = 6;
1664 break;
1665 case S3C2410_LCON_CS7:
1666 *bits = 7;
1667 break;
1668 default:
1669 case S3C2410_LCON_CS8:
1670 *bits = 8;
1671 break;
1674 switch (ulcon & S3C2410_LCON_PMASK) {
1675 case S3C2410_LCON_PEVEN:
1676 *parity = 'e';
1677 break;
1679 case S3C2410_LCON_PODD:
1680 *parity = 'o';
1681 break;
1683 case S3C2410_LCON_PNONE:
1684 default:
1685 *parity = 'n';
1688 /* now calculate the baud rate */
1690 s3c24xx_serial_getsource(port, &clksrc);
1692 clk = clk_get(port->dev, clksrc.name);
1693 if (!IS_ERR(clk) && clk != NULL)
1694 rate = clk_get_rate(clk) / clksrc.divisor;
1695 else
1696 rate = 1;
1699 *baud = rate / ( 16 * (ubrdiv + 1));
1700 dbg("calculated baud %d\n", *baud);
1705 /* s3c24xx_serial_init_ports
1707 * initialise the serial ports from the machine provided initialisation
1708 * data.
1711 static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info *info)
1713 struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1714 struct platform_device **platdev_ptr;
1715 int i;
1717 dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1719 platdev_ptr = s3c24xx_uart_devs;
1721 for (i = 0; i < NR_PORTS; i++, ptr++, platdev_ptr++) {
1722 s3c24xx_serial_init_port(ptr, info, *platdev_ptr);
1725 return 0;
1728 static int __init
1729 s3c24xx_serial_console_setup(struct console *co, char *options)
1731 struct uart_port *port;
1732 int baud = 9600;
1733 int bits = 8;
1734 int parity = 'n';
1735 int flow = 'n';
1737 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1738 co, co->index, options);
1740 /* is this a valid port */
1742 if (co->index == -1 || co->index >= NR_PORTS)
1743 co->index = 0;
1745 port = &s3c24xx_serial_ports[co->index].port;
1747 /* is the port configured? */
1749 if (port->mapbase == 0x0) {
1750 co->index = 0;
1751 port = &s3c24xx_serial_ports[co->index].port;
1754 cons_uart = port;
1756 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1759 * Check whether an invalid uart number has been specified, and
1760 * if so, search for the first available port that does have
1761 * console support.
1763 if (options)
1764 uart_parse_options(options, &baud, &parity, &bits, &flow);
1765 else
1766 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1768 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1770 return uart_set_options(port, co, baud, parity, bits, flow);
1773 /* s3c24xx_serial_initconsole
1775 * initialise the console from one of the uart drivers
1778 static struct console s3c24xx_serial_console =
1780 .name = S3C24XX_SERIAL_NAME,
1781 .device = uart_console_device,
1782 .flags = CON_PRINTBUFFER,
1783 .index = -1,
1784 .write = s3c24xx_serial_console_write,
1785 .setup = s3c24xx_serial_console_setup
1788 static int s3c24xx_serial_initconsole(void)
1790 struct s3c24xx_uart_info *info;
1791 struct platform_device *dev = s3c24xx_uart_devs[0];
1793 dbg("s3c24xx_serial_initconsole\n");
1795 /* select driver based on the cpu */
1797 if (dev == NULL) {
1798 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1799 return 0;
1802 if (strcmp(dev->name, "s3c2400-uart") == 0) {
1803 info = s3c2400_uart_inf_at;
1804 } else if (strcmp(dev->name, "s3c2410-uart") == 0) {
1805 info = s3c2410_uart_inf_at;
1806 } else if (strcmp(dev->name, "s3c2440-uart") == 0) {
1807 info = s3c2440_uart_inf_at;
1808 } else {
1809 printk(KERN_ERR "s3c24xx: no driver for %s\n", dev->name);
1810 return 0;
1813 if (info == NULL) {
1814 printk(KERN_ERR "s3c24xx: no driver for console\n");
1815 return 0;
1818 s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1819 s3c24xx_serial_init_ports(info);
1821 register_console(&s3c24xx_serial_console);
1822 return 0;
1825 console_initcall(s3c24xx_serial_initconsole);
1827 #endif /* CONFIG_SERIAL_S3C2410_CONSOLE */
1829 MODULE_LICENSE("GPL");
1830 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1831 MODULE_DESCRIPTION("Samsung S3C2410/S3C2440 Serial port driver");