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 Simtec Electronics
12 * 22-Jul-2004 BJD Finished off device rewrite
14 * 21-Jul-2004 BJD Thanks to <herbet@13thfloor.at> for pointing out
15 * problems with baud rate and loss of IR settings. Update
16 * to add configuration via platform_device structure
18 * 28-Sep-2004 BJD Re-write for the following items
19 * - S3C2410 and S3C2440 serial support
20 * - Power Management support
21 * - Fix console via IrDA devices
22 * - SysReq (Herbert Pötzl)
23 * - Break character handling (Herbert Pötzl)
24 * - spin-lock initialisation (Dimitry Andric)
25 * - added clock control
26 * - updated init code to use platform_device info
29 /* Hote on 2410 error handling
31 * The s3c2410 manual has a love/hate affair with the contents of the
32 * UERSTAT register in the UART blocks, and keeps marking some of the
33 * error bits as reserved. Having checked with the s3c2410x01,
34 * it copes with BREAKs properly, so I am happy to ignore the RESERVED
35 * feature from the latter versions of the manual.
37 * If it becomes aparrent that latter versions of the 2410 remove these
38 * bits, then action will have to be taken to differentiate the versions
39 * and change the policy on BREAK
44 #include <linux/config.h>
46 #if defined(CONFIG_SERIAL_S3C2410_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
50 #include <linux/module.h>
51 #include <linux/ioport.h>
52 #include <linux/device.h>
53 #include <linux/init.h>
54 #include <linux/sysrq.h>
55 #include <linux/console.h>
56 #include <linux/tty.h>
57 #include <linux/tty_flip.h>
58 #include <linux/serial_core.h>
59 #include <linux/serial.h>
60 #include <linux/delay.h>
65 #include <asm/hardware.h>
66 #include <asm/hardware/clock.h>
68 #include <asm/arch/regs-serial.h>
69 #include <asm/arch/regs-gpio.h>
71 #include <asm/mach-types.h>
75 struct s3c24xx_uart_info {
78 unsigned int fifosize;
79 unsigned long rx_fifomask;
80 unsigned long rx_fifoshift;
81 unsigned long rx_fifofull;
82 unsigned long tx_fifomask;
83 unsigned long tx_fifoshift;
84 unsigned long tx_fifofull;
86 /* clock source control */
88 int (*get_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
89 int (*set_clksrc)(struct uart_port *, struct s3c24xx_uart_clksrc *clk);
92 struct s3c24xx_uart_port {
93 unsigned char rx_claimed;
94 unsigned char tx_claimed;
96 struct s3c24xx_uart_info *info;
97 struct s3c24xx_uart_clksrc *clksrc;
100 struct uart_port port;
104 /* configuration defines */
108 /* send debug to the low-level output routines */
110 extern void printascii(const char *);
113 s3c24xx_serial_dbg(const char *fmt, ...)
119 vsprintf(buff, fmt, va);
125 #define dbg(x...) s3c24xx_serial_dbg(x)
128 #define dbg(x...) printk(KERN_DEBUG "s3c24xx: ");
131 #define dbg(x...) do {} while(0)
134 /* UART name and device definitions */
136 #define S3C24XX_SERIAL_NAME "ttySAC"
137 #define S3C24XX_SERIAL_DEVFS "tts/"
138 #define S3C24XX_SERIAL_MAJOR 204
139 #define S3C24XX_SERIAL_MINOR 64
142 /* conversion functions */
144 #define s3c24xx_dev_to_port(__dev) (struct uart_port *)dev_get_drvdata(__dev)
145 #define s3c24xx_dev_to_cfg(__dev) (struct s3c2410_uartcfg *)((__dev)->platform_data)
147 /* we can support 3 uarts, but not always use them */
151 /* port irq numbers */
153 #define TX_IRQ(port) ((port)->irq + 1)
154 #define RX_IRQ(port) ((port)->irq)
156 /* register access controls */
158 #define portaddr(port, reg) ((port)->membase + (reg))
160 #define rd_regb(port, reg) (__raw_readb(portaddr(port, reg)))
161 #define rd_regl(port, reg) (__raw_readl(portaddr(port, reg)))
163 #define wr_regb(port, reg, val) \
164 do { __raw_writeb(val, portaddr(port, reg)); } while(0)
166 #define wr_regl(port, reg, val) \
167 do { __raw_writel(val, portaddr(port, reg)); } while(0)
169 /* macros to change one thing to another */
171 #define tx_enabled(port) ((port)->unused[0])
172 #define rx_enabled(port) ((port)->unused[1])
174 /* flag to ignore all characters comming in */
175 #define RXSTAT_DUMMY_READ (0x10000000)
177 static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
179 return container_of(port, struct s3c24xx_uart_port, port);
182 /* translate a port to the device name */
184 static inline char *s3c24xx_serial_portname(struct uart_port *port)
186 return to_platform_device(port->dev)->name;
189 static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
191 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
194 static void s3c24xx_serial_rx_enable(struct uart_port *port)
197 unsigned int ucon, ufcon;
200 spin_lock_irqsave(&port->lock, flags);
202 while (--count && !s3c24xx_serial_txempty_nofifo(port))
205 ufcon = rd_regl(port, S3C2410_UFCON);
206 ufcon |= S3C2410_UFCON_RESETRX;
207 wr_regl(port, S3C2410_UFCON, ufcon);
209 ucon = rd_regl(port, S3C2410_UCON);
210 ucon |= S3C2410_UCON_RXIRQMODE;
211 wr_regl(port, S3C2410_UCON, ucon);
213 rx_enabled(port) = 1;
214 spin_unlock_irqrestore(&port->lock, flags);
217 static void s3c24xx_serial_rx_disable(struct uart_port *port)
222 spin_lock_irqsave(&port->lock, flags);
224 ucon = rd_regl(port, S3C2410_UCON);
225 ucon &= ~S3C2410_UCON_RXIRQMODE;
226 wr_regl(port, S3C2410_UCON, ucon);
228 rx_enabled(port) = 0;
229 spin_unlock_irqrestore(&port->lock, flags);
233 s3c24xx_serial_stop_tx(struct uart_port *port, unsigned int tty_stop)
235 if (tx_enabled(port)) {
236 disable_irq(TX_IRQ(port));
237 tx_enabled(port) = 0;
238 if (port->flags & UPF_CONS_FLOW)
239 s3c24xx_serial_rx_enable(port);
244 s3c24xx_serial_start_tx(struct uart_port *port, unsigned int tty_start)
246 if (!tx_enabled(port)) {
247 if (port->flags & UPF_CONS_FLOW)
248 s3c24xx_serial_rx_disable(port);
250 enable_irq(TX_IRQ(port));
251 tx_enabled(port) = 1;
256 static void s3c24xx_serial_stop_rx(struct uart_port *port)
258 if (rx_enabled(port)) {
259 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
260 disable_irq(RX_IRQ(port));
261 rx_enabled(port) = 0;
265 static void s3c24xx_serial_enable_ms(struct uart_port *port)
269 static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
271 return to_ourport(port)->info;
274 static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
276 if (port->dev == NULL)
279 return (struct s3c2410_uartcfg *)port->dev->platform_data;
282 static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
283 unsigned long ufstat)
285 struct s3c24xx_uart_info *info = ourport->info;
287 if (ufstat & info->rx_fifofull)
288 return info->fifosize;
290 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
294 /* ? - where has parity gone?? */
295 #define S3C2410_UERSTAT_PARITY (0x1000)
298 s3c24xx_serial_rx_chars(int irq, void *dev_id, struct pt_regs *regs)
300 struct s3c24xx_uart_port *ourport = dev_id;
301 struct uart_port *port = &ourport->port;
302 struct tty_struct *tty = port->info->tty;
303 unsigned int ufcon, ch, flag, ufstat, uerstat;
306 while (max_count-- > 0) {
307 ufcon = rd_regl(port, S3C2410_UFCON);
308 ufstat = rd_regl(port, S3C2410_UFSTAT);
310 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
313 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
314 if (tty->low_latency)
315 tty_flip_buffer_push(tty);
318 * If this failed then we will throw away the
319 * bytes but must do so to clear interrupts
323 uerstat = rd_regl(port, S3C2410_UERSTAT);
324 ch = rd_regb(port, S3C2410_URXH);
326 if (port->flags & UPF_CONS_FLOW) {
327 int txe = s3c24xx_serial_txempty_nofifo(port);
329 if (rx_enabled(port)) {
331 rx_enabled(port) = 0;
336 ufcon |= S3C2410_UFCON_RESETRX;
337 wr_regl(port, S3C2410_UFCON, ufcon);
338 rx_enabled(port) = 1;
345 /* insert the character into the buffer */
350 if (uerstat & S3C2410_UERSTAT_ANY) {
351 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
354 /* check for break */
355 if (uerstat & S3C2410_UERSTAT_BREAK) {
358 if (uart_handle_break(port))
362 if (uerstat & S3C2410_UERSTAT_FRAME)
363 port->icount.frame++;
364 if (uerstat & S3C2410_UERSTAT_OVERRUN)
365 port->icount.overrun++;
367 uerstat &= port->read_status_mask;
369 if (uerstat & S3C2410_UERSTAT_BREAK)
371 else if (uerstat & S3C2410_UERSTAT_PARITY)
373 else if (uerstat & ( S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_OVERRUN))
377 if (uart_handle_sysrq_char(port, ch, regs))
380 if ((uerstat & port->ignore_status_mask) == 0) {
381 tty_insert_flip_char(tty, ch, flag);
384 if ((uerstat & S3C2410_UERSTAT_OVERRUN) &&
385 tty->flip.count < TTY_FLIPBUF_SIZE) {
387 * Overrun is special, since it's reported
388 * immediately, and doesn't affect the current
392 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
398 tty_flip_buffer_push(tty);
404 static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id, struct pt_regs *regs)
406 struct s3c24xx_uart_port *ourport = id;
407 struct uart_port *port = &ourport->port;
408 struct circ_buf *xmit = &port->info->xmit;
412 wr_regb(port, S3C2410_UTXH, port->x_char);
418 /* if there isnt anything more to transmit, or the uart is now
419 * stopped, disable the uart and exit
422 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
423 s3c24xx_serial_stop_tx(port, 0);
427 /* try and drain the buffer... */
429 while (!uart_circ_empty(xmit) && count-- > 0) {
430 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
433 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
434 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
438 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
439 uart_write_wakeup(port);
441 if (uart_circ_empty(xmit))
442 s3c24xx_serial_stop_tx(port, 0);
448 static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
450 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
451 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
452 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
454 if (ufcon & S3C2410_UFCON_FIFOMODE) {
455 if ((ufstat & info->tx_fifomask) != 0 ||
456 (ufstat & info->tx_fifofull))
462 return s3c24xx_serial_txempty_nofifo(port);
465 /* no modem control lines */
466 static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
468 unsigned int umstat = rd_regb(port,S3C2410_UMSTAT);
470 if (umstat & S3C2410_UMSTAT_CTS)
471 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
473 return TIOCM_CAR | TIOCM_DSR;
476 static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
478 /* todo - possibly remove AFC and do manual CTS */
481 static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
486 spin_lock_irqsave(&port->lock, flags);
488 ucon = rd_regl(port, S3C2410_UCON);
491 ucon |= S3C2410_UCON_SBREAK;
493 ucon &= ~S3C2410_UCON_SBREAK;
495 wr_regl(port, S3C2410_UCON, ucon);
497 spin_unlock_irqrestore(&port->lock, flags);
500 static void s3c24xx_serial_shutdown(struct uart_port *port)
502 struct s3c24xx_uart_port *ourport = to_ourport(port);
504 if (ourport->tx_claimed) {
505 free_irq(TX_IRQ(port), ourport);
506 tx_enabled(port) = 0;
507 ourport->tx_claimed = 0;
510 if (ourport->rx_claimed) {
511 free_irq(RX_IRQ(port), ourport);
512 ourport->rx_claimed = 0;
513 rx_enabled(port) = 0;
518 static int s3c24xx_serial_startup(struct uart_port *port)
520 struct s3c24xx_uart_port *ourport = to_ourport(port);
524 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
525 port->mapbase, port->membase);
527 local_irq_save(flags);
529 rx_enabled(port) = 1;
531 ret = request_irq(RX_IRQ(port),
532 s3c24xx_serial_rx_chars, 0,
533 s3c24xx_serial_portname(port), ourport);
536 printk(KERN_ERR "cannot get irq %d\n", RX_IRQ(port));
540 ourport->rx_claimed = 1;
542 dbg("requesting tx irq...\n");
544 tx_enabled(port) = 1;
546 ret = request_irq(TX_IRQ(port),
547 s3c24xx_serial_tx_chars, 0,
548 s3c24xx_serial_portname(port), ourport);
551 printk(KERN_ERR "cannot get irq %d\n", TX_IRQ(port));
555 ourport->tx_claimed = 1;
557 dbg("s3c24xx_serial_startup ok\n");
559 /* the port reset code should have done the correct
560 * register setup for the port controls */
562 local_irq_restore(flags);
566 s3c24xx_serial_shutdown(port);
567 local_irq_restore(flags);
571 /* power power management control */
573 static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
576 struct s3c24xx_uart_port *ourport = to_ourport(port);
580 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
581 clk_disable(ourport->baudclk);
583 clk_disable(ourport->clk);
587 clk_enable(ourport->clk);
589 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
590 clk_enable(ourport->baudclk);
594 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
598 /* baud rate calculation
600 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
601 * of different sources, including the peripheral clock ("pclk") and an
602 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
603 * with a programmable extra divisor.
605 * The following code goes through the clock sources, and calculates the
606 * baud clocks (and the resultant actual baud rates) and then tries to
607 * pick the closest one and select that.
610 * 1) there is no current code to properly select/deselect FCLK on
611 * the s3c2440, so only specify FCLK or non-FCLK in the clock
612 * sources for the UART
619 static struct s3c24xx_uart_clksrc tmp_clksrc = {
627 s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
629 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
631 return (info->get_clksrc)(port, c);
635 s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
637 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
639 return (info->set_clksrc)(port, c);
643 struct s3c24xx_uart_clksrc *clksrc;
649 static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
650 struct uart_port *port,
651 struct s3c24xx_uart_clksrc *clksrc,
656 calc->src = clk_get(port->dev, clksrc->name);
657 if (calc->src == NULL || IS_ERR(calc->src))
660 rate = clk_get_rate(calc->src);
662 calc->clksrc = clksrc;
663 calc->quot = (rate + (8 * baud)) / (16 * baud);
664 calc->calc = (rate / (calc->quot * 16));
670 static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
671 struct s3c24xx_uart_clksrc **clksrc,
675 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
676 struct s3c24xx_uart_clksrc *clkp;
677 struct baud_calc res[MAX_CLKS];
678 struct baud_calc *resptr, *best, *sptr;
684 if (cfg->clocks_size < 2) {
685 if (cfg->clocks_size == 0)
688 s3c24xx_serial_calcbaud(res, port, clkp, baud);
694 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
695 if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
700 /* ok, we now need to select the best clock we found */
703 unsigned int deviation = (1<<30)|((1<<30)-1);
706 for (sptr = res; sptr < resptr; sptr++) {
708 "found clk %p (%s) quot %d, calc %d\n",
709 sptr->clksrc, sptr->clksrc->name,
710 sptr->quot, sptr->calc);
712 calc_deviation = baud - sptr->calc;
713 if (calc_deviation < 0)
714 calc_deviation = -calc_deviation;
716 if (calc_deviation < deviation) {
718 deviation = calc_deviation;
722 printk(KERN_DEBUG "best %p (deviation %d)\n", best, deviation);
725 printk(KERN_DEBUG "selected clock %p (%s) quot %d, calc %d\n",
726 best->clksrc, best->clksrc->name, best->quot, best->calc);
728 /* store results to pass back */
730 *clksrc = best->clksrc;
736 static void s3c24xx_serial_set_termios(struct uart_port *port,
737 struct termios *termios,
740 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
741 struct s3c24xx_uart_port *ourport = to_ourport(port);
742 struct s3c24xx_uart_clksrc *clksrc;
745 unsigned int baud, quot;
749 * We don't support modem control lines.
751 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
752 termios->c_cflag |= CLOCAL;
755 * Ask the core to calculate the divisor for us.
758 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
760 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
761 quot = port->custom_divisor;
763 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
765 /* check to see if we need to change clock source */
767 if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
768 s3c24xx_serial_setsource(port, clksrc);
770 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
771 clk_disable(ourport->baudclk);
772 clk_unuse(ourport->baudclk);
773 ourport->baudclk = NULL;
779 ourport->clksrc = clksrc;
780 ourport->baudclk = clk;
783 switch (termios->c_cflag & CSIZE) {
785 dbg("config: 5bits/char\n");
786 ulcon = S3C2410_LCON_CS5;
789 dbg("config: 6bits/char\n");
790 ulcon = S3C2410_LCON_CS6;
793 dbg("config: 7bits/char\n");
794 ulcon = S3C2410_LCON_CS7;
798 dbg("config: 8bits/char\n");
799 ulcon = S3C2410_LCON_CS8;
803 /* preserve original lcon IR settings */
804 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
806 if (termios->c_cflag & CSTOPB)
807 ulcon |= S3C2410_LCON_STOPB;
809 if (termios->c_cflag & PARENB) {
810 if (!(termios->c_cflag & PARODD))
811 ulcon |= S3C2410_LCON_PODD;
813 ulcon |= S3C2410_LCON_PEVEN;
815 ulcon |= S3C2410_LCON_PNONE;
818 spin_lock_irqsave(&port->lock, flags);
820 dbg("setting ulcon to %08x, brddiv to %d\n", ulcon, quot);
822 wr_regl(port, S3C2410_ULCON, ulcon);
823 wr_regl(port, S3C2410_UBRDIV, quot);
825 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
826 rd_regl(port, S3C2410_ULCON),
827 rd_regl(port, S3C2410_UCON),
828 rd_regl(port, S3C2410_UFCON));
831 * Update the per-port timeout.
833 uart_update_timeout(port, termios->c_cflag, baud);
836 * Which character status flags are we interested in?
838 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
839 if (termios->c_iflag & INPCK)
840 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
843 * Which character status flags should we ignore?
845 port->ignore_status_mask = 0;
846 if (termios->c_iflag & IGNPAR)
847 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
848 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
849 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
852 * Ignore all characters if CREAD is not set.
854 if ((termios->c_cflag & CREAD) == 0)
855 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
857 spin_unlock_irqrestore(&port->lock, flags);
860 static const char *s3c24xx_serial_type(struct uart_port *port)
862 switch (port->type) {
872 #define MAP_SIZE (0x100)
874 static void s3c24xx_serial_release_port(struct uart_port *port)
876 release_mem_region(port->mapbase, MAP_SIZE);
879 static int s3c24xx_serial_request_port(struct uart_port *port)
881 char *name = s3c24xx_serial_portname(port);
882 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
885 static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
887 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
889 if (flags & UART_CONFIG_TYPE &&
890 s3c24xx_serial_request_port(port) == 0)
891 port->type = info->type;
895 * verify the new serial_struct (for TIOCSSERIAL).
898 s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
900 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
902 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
909 #ifdef CONFIG_SERIAL_S3C2410_CONSOLE
911 static struct console s3c24xx_serial_console;
913 #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
915 #define S3C24XX_SERIAL_CONSOLE NULL
918 static struct uart_ops s3c24xx_serial_ops = {
919 .pm = s3c24xx_serial_pm,
920 .tx_empty = s3c24xx_serial_tx_empty,
921 .get_mctrl = s3c24xx_serial_get_mctrl,
922 .set_mctrl = s3c24xx_serial_set_mctrl,
923 .stop_tx = s3c24xx_serial_stop_tx,
924 .start_tx = s3c24xx_serial_start_tx,
925 .stop_rx = s3c24xx_serial_stop_rx,
926 .enable_ms = s3c24xx_serial_enable_ms,
927 .break_ctl = s3c24xx_serial_break_ctl,
928 .startup = s3c24xx_serial_startup,
929 .shutdown = s3c24xx_serial_shutdown,
930 .set_termios = s3c24xx_serial_set_termios,
931 .type = s3c24xx_serial_type,
932 .release_port = s3c24xx_serial_release_port,
933 .request_port = s3c24xx_serial_request_port,
934 .config_port = s3c24xx_serial_config_port,
935 .verify_port = s3c24xx_serial_verify_port,
939 static struct uart_driver s3c24xx_uart_drv = {
940 .owner = THIS_MODULE,
941 .dev_name = "s3c2410_serial",
943 .cons = S3C24XX_SERIAL_CONSOLE,
944 .driver_name = S3C24XX_SERIAL_NAME,
945 .devfs_name = S3C24XX_SERIAL_DEVFS,
946 .major = S3C24XX_SERIAL_MAJOR,
947 .minor = S3C24XX_SERIAL_MINOR,
950 static struct s3c24xx_uart_port s3c24xx_serial_ports[NR_PORTS] = {
953 .lock = SPIN_LOCK_UNLOCKED,
957 .irq = IRQ_S3CUART_RX0,
960 .ops = &s3c24xx_serial_ops,
961 .flags = UPF_BOOT_AUTOCONF,
967 .lock = SPIN_LOCK_UNLOCKED,
971 .irq = IRQ_S3CUART_RX1,
974 .ops = &s3c24xx_serial_ops,
975 .flags = UPF_BOOT_AUTOCONF,
983 .lock = SPIN_LOCK_UNLOCKED,
987 .irq = IRQ_S3CUART_RX2,
990 .ops = &s3c24xx_serial_ops,
991 .flags = UPF_BOOT_AUTOCONF,
999 static int s3c24xx_serial_resetport(struct uart_port *port,
1000 struct s3c2410_uartcfg *cfg)
1002 /* ensure registers are setup */
1004 dbg("s3c24xx_serial_resetport: port=%p (%08lx), cfg=%p\n",
1005 port, port->mapbase, cfg);
1007 wr_regl(port, S3C2410_UCON, cfg->ucon);
1008 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
1010 /* reset both fifos */
1012 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1013 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1018 /* s3c24xx_serial_init_port
1020 * initialise a single serial port from the platform device given
1023 static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1024 struct s3c24xx_uart_info *info,
1025 struct platform_device *platdev)
1027 struct uart_port *port = &ourport->port;
1028 struct s3c2410_uartcfg *cfg;
1029 struct resource *res;
1031 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1033 if (platdev == NULL)
1036 cfg = s3c24xx_dev_to_cfg(&platdev->dev);
1038 if (port->mapbase != 0)
1041 if (cfg->hwport > 3)
1044 /* setup info for port */
1045 port->dev = &platdev->dev;
1046 ourport->info = info;
1048 /* copy the info in from provided structure */
1049 ourport->port.fifosize = info->fifosize;
1051 dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
1055 if (cfg->uart_flags & UPF_CONS_FLOW) {
1056 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1057 port->flags |= UPF_CONS_FLOW;
1060 /* sort our the physical and virtual addresses for each UART */
1062 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1064 printk(KERN_ERR "failed to find memory resource for uart\n");
1068 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1070 port->mapbase = res->start;
1071 port->membase = (void __iomem *)(res->start - S3C2410_PA_UART);
1072 port->membase += S3C2410_VA_UART;
1073 port->irq = platform_get_irq(platdev, 0);
1075 ourport->clk = clk_get(&platdev->dev, "uart");
1077 if (ourport->clk != NULL && !IS_ERR(ourport->clk))
1078 clk_use(ourport->clk);
1080 dbg("port: map=%08x, mem=%08x, irq=%d, clock=%ld\n",
1081 port->mapbase, port->membase, port->irq, port->uartclk);
1083 /* reset the fifos (and setup the uart) */
1084 s3c24xx_serial_resetport(port, cfg);
1088 /* Device driver serial port probe */
1090 static int probe_index = 0;
1092 int s3c24xx_serial_probe(struct device *_dev,
1093 struct s3c24xx_uart_info *info)
1095 struct s3c24xx_uart_port *ourport;
1096 struct platform_device *dev = to_platform_device(_dev);
1099 dbg("s3c24xx_serial_probe(%p, %p) %d\n", _dev, info, probe_index);
1101 ourport = &s3c24xx_serial_ports[probe_index];
1104 dbg("%s: initialising port %p...\n", __FUNCTION__, ourport);
1106 ret = s3c24xx_serial_init_port(ourport, info, dev);
1110 dbg("%s: adding port\n", __FUNCTION__);
1111 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1112 dev_set_drvdata(_dev, &ourport->port);
1120 int s3c24xx_serial_remove(struct device *_dev)
1122 struct uart_port *port = s3c24xx_dev_to_port(_dev);
1125 uart_remove_one_port(&s3c24xx_uart_drv, port);
1130 /* UART power management code */
1134 int s3c24xx_serial_suspend(struct device *dev, u32 state, u32 level)
1136 struct uart_port *port = s3c24xx_dev_to_port(dev);
1138 if (port && level == SUSPEND_DISABLE)
1139 uart_suspend_port(&s3c24xx_uart_drv, port);
1144 int s3c24xx_serial_resume(struct device *dev, u32 level)
1146 struct uart_port *port = s3c24xx_dev_to_port(dev);
1147 struct s3c24xx_uart_port *ourport = to_ourport(port);
1149 if (port && level == RESUME_ENABLE) {
1150 clk_enable(ourport->clk);
1151 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1152 clk_disable(ourport->clk);
1154 uart_resume_port(&s3c24xx_uart_drv, port);
1161 #define s3c24xx_serial_suspend NULL
1162 #define s3c24xx_serial_resume NULL
1165 int s3c24xx_serial_init(struct device_driver *drv,
1166 struct s3c24xx_uart_info *info)
1168 dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
1169 return driver_register(drv);
1173 /* now comes the code to initialise either the s3c2410 or s3c2440 serial
1177 /* cpu specific variations on the serial port support */
1179 #ifdef CONFIG_CPU_S3C2410
1181 static int s3c2410_serial_setsource(struct uart_port *port,
1182 struct s3c24xx_uart_clksrc *clk)
1184 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1186 if (strcmp(clk->name, "uclk") == 0)
1187 ucon |= S3C2410_UCON_UCLK;
1189 ucon &= ~S3C2410_UCON_UCLK;
1191 wr_regl(port, S3C2410_UCON, ucon);
1195 static int s3c2410_serial_getsource(struct uart_port *port,
1196 struct s3c24xx_uart_clksrc *clk)
1198 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1201 clk->name = (ucon & S3C2410_UCON_UCLK) ? "uclk" : "pclk";
1206 static struct s3c24xx_uart_info s3c2410_uart_inf = {
1207 .name = "Samsung S3C2410 UART",
1208 .type = PORT_S3C2410,
1210 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
1211 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
1212 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
1213 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
1214 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
1215 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
1216 .get_clksrc = s3c2410_serial_getsource,
1217 .set_clksrc = s3c2410_serial_setsource,
1220 /* device management */
1222 static int s3c2410_serial_probe(struct device *dev)
1224 return s3c24xx_serial_probe(dev, &s3c2410_uart_inf);
1227 static struct device_driver s3c2410_serial_drv = {
1228 .name = "s3c2410-uart",
1229 .bus = &platform_bus_type,
1230 .probe = s3c2410_serial_probe,
1231 .remove = s3c24xx_serial_remove,
1232 .suspend = s3c24xx_serial_suspend,
1233 .resume = s3c24xx_serial_resume,
1236 static inline int s3c2410_serial_init(void)
1238 return s3c24xx_serial_init(&s3c2410_serial_drv, &s3c2410_uart_inf);
1241 static inline void s3c2410_serial_exit(void)
1243 driver_unregister(&s3c2410_serial_drv);
1246 #define s3c2410_uart_inf_at &s3c2410_uart_inf
1249 static inline int s3c2410_serial_init(void)
1254 static inline void s3c2410_serial_exit(void)
1258 #define s3c2410_uart_inf_at NULL
1260 #endif /* CONFIG_CPU_S3C2410 */
1262 #ifdef CONFIG_CPU_S3C2440
1264 static int s3c2440_serial_setsource(struct uart_port *port,
1265 struct s3c24xx_uart_clksrc *clk)
1267 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1269 // todo - proper fclk<>nonfclk switch //
1271 ucon &= ~S3C2440_UCON_CLKMASK;
1273 if (strcmp(clk->name, "uclk") == 0)
1274 ucon |= S3C2440_UCON_UCLK;
1275 else if (strcmp(clk->name, "pclk") == 0)
1276 ucon |= S3C2440_UCON_PCLK;
1277 else if (strcmp(clk->name, "fclk") == 0)
1278 ucon |= S3C2440_UCON_FCLK;
1280 printk(KERN_ERR "unknown clock source %s\n", clk->name);
1284 wr_regl(port, S3C2410_UCON, ucon);
1289 static int s3c2440_serial_getsource(struct uart_port *port,
1290 struct s3c24xx_uart_clksrc *clk)
1292 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1294 switch (ucon & S3C2440_UCON_CLKMASK) {
1295 case S3C2440_UCON_UCLK:
1300 case S3C2440_UCON_PCLK:
1301 case S3C2440_UCON_PCLK2:
1306 case S3C2440_UCON_FCLK:
1307 clk->divisor = 7; /* todo - work out divisor */
1316 static struct s3c24xx_uart_info s3c2440_uart_inf = {
1317 .name = "Samsung S3C2440 UART",
1318 .type = PORT_S3C2440,
1320 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1321 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1322 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1323 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1324 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1325 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1326 .get_clksrc = s3c2440_serial_getsource,
1327 .set_clksrc = s3c2440_serial_setsource
1330 /* device management */
1332 static int s3c2440_serial_probe(struct device *dev)
1334 dbg("s3c2440_serial_probe: dev=%p\n", dev);
1335 return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
1338 static struct device_driver s3c2440_serial_drv = {
1339 .name = "s3c2440-uart",
1340 .bus = &platform_bus_type,
1341 .probe = s3c2440_serial_probe,
1342 .remove = s3c24xx_serial_remove,
1343 .suspend = s3c24xx_serial_suspend,
1344 .resume = s3c24xx_serial_resume,
1348 static inline int s3c2440_serial_init(void)
1350 return s3c24xx_serial_init(&s3c2440_serial_drv, &s3c2440_uart_inf);
1353 static inline void s3c2440_serial_exit(void)
1355 driver_unregister(&s3c2440_serial_drv);
1358 #define s3c2440_uart_inf_at &s3c2440_uart_inf
1361 static inline int s3c2440_serial_init(void)
1366 static inline void s3c2440_serial_exit(void)
1370 #define s3c2440_uart_inf_at NULL
1371 #endif /* CONFIG_CPU_S3C2440 */
1373 /* module initialisation code */
1375 static int __init s3c24xx_serial_modinit(void)
1379 ret = uart_register_driver(&s3c24xx_uart_drv);
1381 printk(KERN_ERR "failed to register UART driver\n");
1385 s3c2410_serial_init();
1386 s3c2440_serial_init();
1391 static void __exit s3c24xx_serial_modexit(void)
1393 s3c2410_serial_exit();
1394 s3c2440_serial_exit();
1396 uart_unregister_driver(&s3c24xx_uart_drv);
1400 module_init(s3c24xx_serial_modinit);
1401 module_exit(s3c24xx_serial_modexit);
1405 #ifdef CONFIG_SERIAL_S3C2410_CONSOLE
1407 static struct uart_port *cons_uart;
1410 s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1412 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1413 unsigned long ufstat, utrstat;
1415 if (ufcon & S3C2410_UFCON_FIFOMODE) {
1416 /* fifo mode - check ammount of data in fifo registers... */
1418 ufstat = rd_regl(port, S3C2410_UFSTAT);
1419 return (ufstat & info->tx_fifofull) ? 0 : 1;
1422 /* in non-fifo mode, we go and use the tx buffer empty */
1424 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1425 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1429 s3c24xx_serial_console_write(struct console *co, const char *s,
1433 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1435 for (i = 0; i < count; i++) {
1436 while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
1439 wr_regb(cons_uart, S3C2410_UTXH, s[i]);
1442 while (!s3c24xx_serial_console_txrdy(cons_uart, ufcon))
1445 wr_regb(cons_uart, S3C2410_UTXH, '\r');
1451 s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1452 int *parity, int *bits)
1454 struct s3c24xx_uart_clksrc clksrc;
1458 unsigned int ubrdiv;
1461 ulcon = rd_regl(port, S3C2410_ULCON);
1462 ucon = rd_regl(port, S3C2410_UCON);
1463 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1465 dbg("s3c24xx_serial_get_options: port=%p\n"
1466 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1467 port, ulcon, ucon, ubrdiv);
1469 if ((ucon & 0xf) != 0) {
1470 /* consider the serial port configured if the tx/rx mode set */
1472 switch (ulcon & S3C2410_LCON_CSMASK) {
1473 case S3C2410_LCON_CS5:
1476 case S3C2410_LCON_CS6:
1479 case S3C2410_LCON_CS7:
1483 case S3C2410_LCON_CS8:
1488 switch (ulcon & S3C2410_LCON_PMASK) {
1489 case S3C2410_LCON_PEVEN:
1493 case S3C2410_LCON_PODD:
1497 case S3C2410_LCON_PNONE:
1502 /* now calculate the baud rate */
1504 s3c24xx_serial_getsource(port, &clksrc);
1506 clk = clk_get(port->dev, clksrc.name);
1507 if (!IS_ERR(clk) && clk != NULL)
1508 rate = clk_get_rate(clk);
1513 *baud = rate / ( 16 * (ubrdiv + 1));
1514 dbg("calculated baud %d\n", *baud);
1519 /* s3c24xx_serial_init_ports
1521 * initialise the serial ports from the machine provided initialisation
1525 static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info *info)
1527 struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1528 struct platform_device **platdev_ptr;
1531 dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1533 platdev_ptr = s3c24xx_uart_devs;
1535 for (i = 0; i < NR_PORTS; i++, ptr++, platdev_ptr++) {
1536 s3c24xx_serial_init_port(ptr, info, *platdev_ptr);
1543 s3c24xx_serial_console_setup(struct console *co, char *options)
1545 struct uart_port *port;
1551 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1552 co, co->index, options);
1554 /* is this a valid port */
1556 if (co->index == -1 || co->index >= NR_PORTS)
1559 port = &s3c24xx_serial_ports[co->index].port;
1561 /* is the port configured? */
1563 if (port->mapbase == 0x0) {
1565 port = &s3c24xx_serial_ports[co->index].port;
1570 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1573 * Check whether an invalid uart number has been specified, and
1574 * if so, search for the first available port that does have
1578 uart_parse_options(options, &baud, &parity, &bits, &flow);
1580 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1582 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1584 return uart_set_options(port, co, baud, parity, bits, flow);
1587 /* s3c24xx_serial_initconsole
1589 * initialise the console from one of the uart drivers
1592 static struct console s3c24xx_serial_console =
1594 .name = S3C24XX_SERIAL_NAME,
1595 .device = uart_console_device,
1596 .flags = CON_PRINTBUFFER,
1598 .write = s3c24xx_serial_console_write,
1599 .setup = s3c24xx_serial_console_setup
1603 static int s3c24xx_serial_initconsole(void)
1605 struct s3c24xx_uart_info *info;
1606 struct platform_device *dev = s3c24xx_uart_devs[0];
1608 dbg("s3c24xx_serial_initconsole\n");
1610 /* select driver based on the cpu */
1613 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1617 if (strcmp(dev->name, "s3c2410-uart") == 0) {
1618 info = s3c2410_uart_inf_at;
1619 } else if (strcmp(dev->name, "s3c2440-uart") == 0) {
1620 info = s3c2440_uart_inf_at;
1622 printk(KERN_ERR "s3c24xx: no driver for %s\n", dev->name);
1627 printk(KERN_ERR "s3c24xx: no driver for console\n");
1631 s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1632 s3c24xx_serial_init_ports(info);
1634 register_console(&s3c24xx_serial_console);
1638 console_initcall(s3c24xx_serial_initconsole);
1640 #endif /* CONFIG_SERIAL_S3C2410_CONSOLE */
1642 MODULE_LICENSE("GPL");
1643 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1644 MODULE_DESCRIPTION("Samsung S3C2410/S3C2440 Serial port driver");