1 /* linux/drivers/serial/samsuing.c
3 * Driver core for Samsung SoC onboard UARTs.
5 * Ben Dooks, Copyright (c) 2003-2005,2008 Simtec Electronics
6 * http://armlinux.simtec.co.uk/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 /* Hote on 2410 error handling
15 * The s3c2410 manual has a love/hate affair with the contents of the
16 * UERSTAT register in the UART blocks, and keeps marking some of the
17 * error bits as reserved. Having checked with the s3c2410x01,
18 * it copes with BREAKs properly, so I am happy to ignore the RESERVED
19 * feature from the latter versions of the manual.
21 * If it becomes aparrent that latter versions of the 2410 remove these
22 * bits, then action will have to be taken to differentiate the versions
23 * and change the policy on BREAK
28 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
32 #include <linux/module.h>
33 #include <linux/ioport.h>
35 #include <linux/platform_device.h>
36 #include <linux/init.h>
37 #include <linux/sysrq.h>
38 #include <linux/console.h>
39 #include <linux/tty.h>
40 #include <linux/tty_flip.h>
41 #include <linux/serial_core.h>
42 #include <linux/serial.h>
43 #include <linux/delay.h>
44 #include <linux/clk.h>
48 #include <mach/hardware.h>
50 #include <plat/regs-serial.h>
51 #include <mach/regs-gpio.h>
55 /* UART name and device definitions */
57 #define S3C24XX_SERIAL_NAME "ttySAC"
58 #define S3C24XX_SERIAL_MAJOR 204
59 #define S3C24XX_SERIAL_MINOR 64
61 /* we can support 3 uarts, but not always use them */
63 #ifdef CONFIG_CPU_S3C2400
69 /* port irq numbers */
71 #define TX_IRQ(port) ((port)->irq + 1)
72 #define RX_IRQ(port) ((port)->irq)
74 /* macros to change one thing to another */
76 #define tx_enabled(port) ((port)->unused[0])
77 #define rx_enabled(port) ((port)->unused[1])
79 /* flag to ignore all characters comming in */
80 #define RXSTAT_DUMMY_READ (0x10000000)
82 static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
84 return container_of(port, struct s3c24xx_uart_port, port);
87 /* translate a port to the device name */
89 static inline const char *s3c24xx_serial_portname(struct uart_port *port)
91 return to_platform_device(port->dev)->name;
94 static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
96 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
99 static void s3c24xx_serial_rx_enable(struct uart_port *port)
102 unsigned int ucon, ufcon;
105 spin_lock_irqsave(&port->lock, flags);
107 while (--count && !s3c24xx_serial_txempty_nofifo(port))
110 ufcon = rd_regl(port, S3C2410_UFCON);
111 ufcon |= S3C2410_UFCON_RESETRX;
112 wr_regl(port, S3C2410_UFCON, ufcon);
114 ucon = rd_regl(port, S3C2410_UCON);
115 ucon |= S3C2410_UCON_RXIRQMODE;
116 wr_regl(port, S3C2410_UCON, ucon);
118 rx_enabled(port) = 1;
119 spin_unlock_irqrestore(&port->lock, flags);
122 static void s3c24xx_serial_rx_disable(struct uart_port *port)
127 spin_lock_irqsave(&port->lock, flags);
129 ucon = rd_regl(port, S3C2410_UCON);
130 ucon &= ~S3C2410_UCON_RXIRQMODE;
131 wr_regl(port, S3C2410_UCON, ucon);
133 rx_enabled(port) = 0;
134 spin_unlock_irqrestore(&port->lock, flags);
137 static void s3c24xx_serial_stop_tx(struct uart_port *port)
139 if (tx_enabled(port)) {
140 disable_irq(TX_IRQ(port));
141 tx_enabled(port) = 0;
142 if (port->flags & UPF_CONS_FLOW)
143 s3c24xx_serial_rx_enable(port);
147 static void s3c24xx_serial_start_tx(struct uart_port *port)
149 if (!tx_enabled(port)) {
150 if (port->flags & UPF_CONS_FLOW)
151 s3c24xx_serial_rx_disable(port);
153 enable_irq(TX_IRQ(port));
154 tx_enabled(port) = 1;
159 static void s3c24xx_serial_stop_rx(struct uart_port *port)
161 if (rx_enabled(port)) {
162 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
163 disable_irq(RX_IRQ(port));
164 rx_enabled(port) = 0;
168 static void s3c24xx_serial_enable_ms(struct uart_port *port)
172 static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
174 return to_ourport(port)->info;
177 static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
179 if (port->dev == NULL)
182 return (struct s3c2410_uartcfg *)port->dev->platform_data;
185 static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
186 unsigned long ufstat)
188 struct s3c24xx_uart_info *info = ourport->info;
190 if (ufstat & info->rx_fifofull)
191 return info->fifosize;
193 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
197 /* ? - where has parity gone?? */
198 #define S3C2410_UERSTAT_PARITY (0x1000)
201 s3c24xx_serial_rx_chars(int irq, void *dev_id)
203 struct s3c24xx_uart_port *ourport = dev_id;
204 struct uart_port *port = &ourport->port;
205 struct tty_struct *tty = port->info->port.tty;
206 unsigned int ufcon, ch, flag, ufstat, uerstat;
209 while (max_count-- > 0) {
210 ufcon = rd_regl(port, S3C2410_UFCON);
211 ufstat = rd_regl(port, S3C2410_UFSTAT);
213 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
216 uerstat = rd_regl(port, S3C2410_UERSTAT);
217 ch = rd_regb(port, S3C2410_URXH);
219 if (port->flags & UPF_CONS_FLOW) {
220 int txe = s3c24xx_serial_txempty_nofifo(port);
222 if (rx_enabled(port)) {
224 rx_enabled(port) = 0;
229 ufcon |= S3C2410_UFCON_RESETRX;
230 wr_regl(port, S3C2410_UFCON, ufcon);
231 rx_enabled(port) = 1;
238 /* insert the character into the buffer */
243 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
244 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
247 /* check for break */
248 if (uerstat & S3C2410_UERSTAT_BREAK) {
251 if (uart_handle_break(port))
255 if (uerstat & S3C2410_UERSTAT_FRAME)
256 port->icount.frame++;
257 if (uerstat & S3C2410_UERSTAT_OVERRUN)
258 port->icount.overrun++;
260 uerstat &= port->read_status_mask;
262 if (uerstat & S3C2410_UERSTAT_BREAK)
264 else if (uerstat & S3C2410_UERSTAT_PARITY)
266 else if (uerstat & (S3C2410_UERSTAT_FRAME |
267 S3C2410_UERSTAT_OVERRUN))
271 if (uart_handle_sysrq_char(port, ch))
274 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
280 tty_flip_buffer_push(tty);
286 static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
288 struct s3c24xx_uart_port *ourport = id;
289 struct uart_port *port = &ourport->port;
290 struct circ_buf *xmit = &port->info->xmit;
294 wr_regb(port, S3C2410_UTXH, port->x_char);
300 /* if there isnt anything more to transmit, or the uart is now
301 * stopped, disable the uart and exit
304 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
305 s3c24xx_serial_stop_tx(port);
309 /* try and drain the buffer... */
311 while (!uart_circ_empty(xmit) && count-- > 0) {
312 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
315 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
316 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
320 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
321 uart_write_wakeup(port);
323 if (uart_circ_empty(xmit))
324 s3c24xx_serial_stop_tx(port);
330 static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
332 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
333 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
334 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
336 if (ufcon & S3C2410_UFCON_FIFOMODE) {
337 if ((ufstat & info->tx_fifomask) != 0 ||
338 (ufstat & info->tx_fifofull))
344 return s3c24xx_serial_txempty_nofifo(port);
347 /* no modem control lines */
348 static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
350 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
352 if (umstat & S3C2410_UMSTAT_CTS)
353 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
355 return TIOCM_CAR | TIOCM_DSR;
358 static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
360 /* todo - possibly remove AFC and do manual CTS */
363 static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
368 spin_lock_irqsave(&port->lock, flags);
370 ucon = rd_regl(port, S3C2410_UCON);
373 ucon |= S3C2410_UCON_SBREAK;
375 ucon &= ~S3C2410_UCON_SBREAK;
377 wr_regl(port, S3C2410_UCON, ucon);
379 spin_unlock_irqrestore(&port->lock, flags);
382 static void s3c24xx_serial_shutdown(struct uart_port *port)
384 struct s3c24xx_uart_port *ourport = to_ourport(port);
386 if (ourport->tx_claimed) {
387 free_irq(TX_IRQ(port), ourport);
388 tx_enabled(port) = 0;
389 ourport->tx_claimed = 0;
392 if (ourport->rx_claimed) {
393 free_irq(RX_IRQ(port), ourport);
394 ourport->rx_claimed = 0;
395 rx_enabled(port) = 0;
400 static int s3c24xx_serial_startup(struct uart_port *port)
402 struct s3c24xx_uart_port *ourport = to_ourport(port);
405 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
406 port->mapbase, port->membase);
408 rx_enabled(port) = 1;
410 ret = request_irq(RX_IRQ(port),
411 s3c24xx_serial_rx_chars, 0,
412 s3c24xx_serial_portname(port), ourport);
415 printk(KERN_ERR "cannot get irq %d\n", RX_IRQ(port));
419 ourport->rx_claimed = 1;
421 dbg("requesting tx irq...\n");
423 tx_enabled(port) = 1;
425 ret = request_irq(TX_IRQ(port),
426 s3c24xx_serial_tx_chars, 0,
427 s3c24xx_serial_portname(port), ourport);
430 printk(KERN_ERR "cannot get irq %d\n", TX_IRQ(port));
434 ourport->tx_claimed = 1;
436 dbg("s3c24xx_serial_startup ok\n");
438 /* the port reset code should have done the correct
439 * register setup for the port controls */
444 s3c24xx_serial_shutdown(port);
448 /* power power management control */
450 static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
453 struct s3c24xx_uart_port *ourport = to_ourport(port);
457 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
458 clk_disable(ourport->baudclk);
460 clk_disable(ourport->clk);
464 clk_enable(ourport->clk);
466 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
467 clk_enable(ourport->baudclk);
471 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
475 /* baud rate calculation
477 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
478 * of different sources, including the peripheral clock ("pclk") and an
479 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
480 * with a programmable extra divisor.
482 * The following code goes through the clock sources, and calculates the
483 * baud clocks (and the resultant actual baud rates) and then tries to
484 * pick the closest one and select that.
491 static struct s3c24xx_uart_clksrc tmp_clksrc = {
499 s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
501 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
503 return (info->get_clksrc)(port, c);
507 s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
509 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
511 return (info->set_clksrc)(port, c);
515 struct s3c24xx_uart_clksrc *clksrc;
521 static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
522 struct uart_port *port,
523 struct s3c24xx_uart_clksrc *clksrc,
528 calc->src = clk_get(port->dev, clksrc->name);
529 if (calc->src == NULL || IS_ERR(calc->src))
532 rate = clk_get_rate(calc->src);
533 rate /= clksrc->divisor;
535 calc->clksrc = clksrc;
536 calc->quot = (rate + (8 * baud)) / (16 * baud);
537 calc->calc = (rate / (calc->quot * 16));
543 static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
544 struct s3c24xx_uart_clksrc **clksrc,
548 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
549 struct s3c24xx_uart_clksrc *clkp;
550 struct baud_calc res[MAX_CLKS];
551 struct baud_calc *resptr, *best, *sptr;
557 if (cfg->clocks_size < 2) {
558 if (cfg->clocks_size == 0)
561 /* check to see if we're sourcing fclk, and if so we're
562 * going to have to update the clock source
565 if (strcmp(clkp->name, "fclk") == 0) {
566 struct s3c24xx_uart_clksrc src;
568 s3c24xx_serial_getsource(port, &src);
570 /* check that the port already using fclk, and if
571 * not, then re-select fclk
574 if (strcmp(src.name, clkp->name) == 0) {
575 s3c24xx_serial_setsource(port, clkp);
576 s3c24xx_serial_getsource(port, &src);
579 clkp->divisor = src.divisor;
582 s3c24xx_serial_calcbaud(res, port, clkp, baud);
588 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
589 if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
594 /* ok, we now need to select the best clock we found */
597 unsigned int deviation = (1<<30)|((1<<30)-1);
600 for (sptr = res; sptr < resptr; sptr++) {
601 calc_deviation = baud - sptr->calc;
602 if (calc_deviation < 0)
603 calc_deviation = -calc_deviation;
605 if (calc_deviation < deviation) {
607 deviation = calc_deviation;
612 /* store results to pass back */
614 *clksrc = best->clksrc;
620 static void s3c24xx_serial_set_termios(struct uart_port *port,
621 struct ktermios *termios,
622 struct ktermios *old)
624 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
625 struct s3c24xx_uart_port *ourport = to_ourport(port);
626 struct s3c24xx_uart_clksrc *clksrc = NULL;
627 struct clk *clk = NULL;
629 unsigned int baud, quot;
634 * We don't support modem control lines.
636 termios->c_cflag &= ~(HUPCL | CMSPAR);
637 termios->c_cflag |= CLOCAL;
640 * Ask the core to calculate the divisor for us.
643 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
645 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
646 quot = port->custom_divisor;
648 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
650 /* check to see if we need to change clock source */
652 if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
653 s3c24xx_serial_setsource(port, clksrc);
655 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
656 clk_disable(ourport->baudclk);
657 ourport->baudclk = NULL;
662 ourport->clksrc = clksrc;
663 ourport->baudclk = clk;
666 switch (termios->c_cflag & CSIZE) {
668 dbg("config: 5bits/char\n");
669 ulcon = S3C2410_LCON_CS5;
672 dbg("config: 6bits/char\n");
673 ulcon = S3C2410_LCON_CS6;
676 dbg("config: 7bits/char\n");
677 ulcon = S3C2410_LCON_CS7;
681 dbg("config: 8bits/char\n");
682 ulcon = S3C2410_LCON_CS8;
686 /* preserve original lcon IR settings */
687 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
689 if (termios->c_cflag & CSTOPB)
690 ulcon |= S3C2410_LCON_STOPB;
692 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
694 if (termios->c_cflag & PARENB) {
695 if (termios->c_cflag & PARODD)
696 ulcon |= S3C2410_LCON_PODD;
698 ulcon |= S3C2410_LCON_PEVEN;
700 ulcon |= S3C2410_LCON_PNONE;
703 spin_lock_irqsave(&port->lock, flags);
705 dbg("setting ulcon to %08x, brddiv to %d\n", ulcon, quot);
707 wr_regl(port, S3C2410_ULCON, ulcon);
708 wr_regl(port, S3C2410_UBRDIV, quot);
709 wr_regl(port, S3C2410_UMCON, umcon);
711 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
712 rd_regl(port, S3C2410_ULCON),
713 rd_regl(port, S3C2410_UCON),
714 rd_regl(port, S3C2410_UFCON));
717 * Update the per-port timeout.
719 uart_update_timeout(port, termios->c_cflag, baud);
722 * Which character status flags are we interested in?
724 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
725 if (termios->c_iflag & INPCK)
726 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
729 * Which character status flags should we ignore?
731 port->ignore_status_mask = 0;
732 if (termios->c_iflag & IGNPAR)
733 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
734 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
735 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
738 * Ignore all characters if CREAD is not set.
740 if ((termios->c_cflag & CREAD) == 0)
741 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
743 spin_unlock_irqrestore(&port->lock, flags);
746 static const char *s3c24xx_serial_type(struct uart_port *port)
748 switch (port->type) {
760 #define MAP_SIZE (0x100)
762 static void s3c24xx_serial_release_port(struct uart_port *port)
764 release_mem_region(port->mapbase, MAP_SIZE);
767 static int s3c24xx_serial_request_port(struct uart_port *port)
769 const char *name = s3c24xx_serial_portname(port);
770 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
773 static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
775 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
777 if (flags & UART_CONFIG_TYPE &&
778 s3c24xx_serial_request_port(port) == 0)
779 port->type = info->type;
783 * verify the new serial_struct (for TIOCSSERIAL).
786 s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
788 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
790 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
797 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
799 static struct console s3c24xx_serial_console;
801 #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
803 #define S3C24XX_SERIAL_CONSOLE NULL
806 static struct uart_ops s3c24xx_serial_ops = {
807 .pm = s3c24xx_serial_pm,
808 .tx_empty = s3c24xx_serial_tx_empty,
809 .get_mctrl = s3c24xx_serial_get_mctrl,
810 .set_mctrl = s3c24xx_serial_set_mctrl,
811 .stop_tx = s3c24xx_serial_stop_tx,
812 .start_tx = s3c24xx_serial_start_tx,
813 .stop_rx = s3c24xx_serial_stop_rx,
814 .enable_ms = s3c24xx_serial_enable_ms,
815 .break_ctl = s3c24xx_serial_break_ctl,
816 .startup = s3c24xx_serial_startup,
817 .shutdown = s3c24xx_serial_shutdown,
818 .set_termios = s3c24xx_serial_set_termios,
819 .type = s3c24xx_serial_type,
820 .release_port = s3c24xx_serial_release_port,
821 .request_port = s3c24xx_serial_request_port,
822 .config_port = s3c24xx_serial_config_port,
823 .verify_port = s3c24xx_serial_verify_port,
827 static struct uart_driver s3c24xx_uart_drv = {
828 .owner = THIS_MODULE,
829 .dev_name = "s3c2410_serial",
831 .cons = S3C24XX_SERIAL_CONSOLE,
832 .driver_name = S3C24XX_SERIAL_NAME,
833 .major = S3C24XX_SERIAL_MAJOR,
834 .minor = S3C24XX_SERIAL_MINOR,
837 static struct s3c24xx_uart_port s3c24xx_serial_ports[NR_PORTS] = {
840 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
842 .irq = IRQ_S3CUART_RX0,
845 .ops = &s3c24xx_serial_ops,
846 .flags = UPF_BOOT_AUTOCONF,
852 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
854 .irq = IRQ_S3CUART_RX1,
857 .ops = &s3c24xx_serial_ops,
858 .flags = UPF_BOOT_AUTOCONF,
866 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
868 .irq = IRQ_S3CUART_RX2,
871 .ops = &s3c24xx_serial_ops,
872 .flags = UPF_BOOT_AUTOCONF,
879 /* s3c24xx_serial_resetport
881 * wrapper to call the specific reset for this port (reset the fifos
885 static inline int s3c24xx_serial_resetport(struct uart_port *port,
886 struct s3c2410_uartcfg *cfg)
888 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
890 return (info->reset_port)(port, cfg);
893 /* s3c24xx_serial_init_port
895 * initialise a single serial port from the platform device given
898 static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
899 struct s3c24xx_uart_info *info,
900 struct platform_device *platdev)
902 struct uart_port *port = &ourport->port;
903 struct s3c2410_uartcfg *cfg;
904 struct resource *res;
907 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
912 cfg = s3c24xx_dev_to_cfg(&platdev->dev);
914 if (port->mapbase != 0)
920 /* setup info for port */
921 port->dev = &platdev->dev;
922 ourport->info = info;
924 /* copy the info in from provided structure */
925 ourport->port.fifosize = info->fifosize;
927 dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
931 if (cfg->uart_flags & UPF_CONS_FLOW) {
932 dbg("s3c24xx_serial_init_port: enabling flow control\n");
933 port->flags |= UPF_CONS_FLOW;
936 /* sort our the physical and virtual addresses for each UART */
938 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
940 printk(KERN_ERR "failed to find memory resource for uart\n");
944 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
946 port->mapbase = res->start;
947 port->membase = S3C24XX_VA_UART + (res->start - S3C24XX_PA_UART);
948 ret = platform_get_irq(platdev, 0);
954 ourport->clk = clk_get(&platdev->dev, "uart");
956 dbg("port: map=%08x, mem=%08x, irq=%d, clock=%ld\n",
957 port->mapbase, port->membase, port->irq, port->uartclk);
959 /* reset the fifos (and setup the uart) */
960 s3c24xx_serial_resetport(port, cfg);
964 static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
965 struct device_attribute *attr,
968 struct uart_port *port = s3c24xx_dev_to_port(dev);
969 struct s3c24xx_uart_port *ourport = to_ourport(port);
971 return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->clksrc->name);
974 static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
976 /* Device driver serial port probe */
978 static int probe_index;
980 int s3c24xx_serial_probe(struct platform_device *dev,
981 struct s3c24xx_uart_info *info)
983 struct s3c24xx_uart_port *ourport;
986 dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
988 ourport = &s3c24xx_serial_ports[probe_index];
991 dbg("%s: initialising port %p...\n", __func__, ourport);
993 ret = s3c24xx_serial_init_port(ourport, info, dev);
997 dbg("%s: adding port\n", __func__);
998 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
999 platform_set_drvdata(dev, &ourport->port);
1001 ret = device_create_file(&dev->dev, &dev_attr_clock_source);
1003 printk(KERN_ERR "%s: failed to add clksrc attr.\n", __func__);
1011 EXPORT_SYMBOL_GPL(s3c24xx_serial_probe);
1013 int s3c24xx_serial_remove(struct platform_device *dev)
1015 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1018 device_remove_file(&dev->dev, &dev_attr_clock_source);
1019 uart_remove_one_port(&s3c24xx_uart_drv, port);
1025 EXPORT_SYMBOL_GPL(s3c24xx_serial_remove);
1027 /* UART power management code */
1031 static int s3c24xx_serial_suspend(struct platform_device *dev, pm_message_t state)
1033 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1036 uart_suspend_port(&s3c24xx_uart_drv, port);
1041 static int s3c24xx_serial_resume(struct platform_device *dev)
1043 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1044 struct s3c24xx_uart_port *ourport = to_ourport(port);
1047 clk_enable(ourport->clk);
1048 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1049 clk_disable(ourport->clk);
1051 uart_resume_port(&s3c24xx_uart_drv, port);
1058 int s3c24xx_serial_init(struct platform_driver *drv,
1059 struct s3c24xx_uart_info *info)
1061 dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
1064 drv->suspend = s3c24xx_serial_suspend;
1065 drv->resume = s3c24xx_serial_resume;
1068 return platform_driver_register(drv);
1071 EXPORT_SYMBOL_GPL(s3c24xx_serial_init);
1073 /* module initialisation code */
1075 static int __init s3c24xx_serial_modinit(void)
1079 ret = uart_register_driver(&s3c24xx_uart_drv);
1081 printk(KERN_ERR "failed to register UART driver\n");
1088 static void __exit s3c24xx_serial_modexit(void)
1090 uart_unregister_driver(&s3c24xx_uart_drv);
1093 module_init(s3c24xx_serial_modinit);
1094 module_exit(s3c24xx_serial_modexit);
1098 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1100 static struct uart_port *cons_uart;
1103 s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1105 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1106 unsigned long ufstat, utrstat;
1108 if (ufcon & S3C2410_UFCON_FIFOMODE) {
1109 /* fifo mode - check ammount of data in fifo registers... */
1111 ufstat = rd_regl(port, S3C2410_UFSTAT);
1112 return (ufstat & info->tx_fifofull) ? 0 : 1;
1115 /* in non-fifo mode, we go and use the tx buffer empty */
1117 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1118 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1122 s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
1124 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1125 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1127 wr_regb(cons_uart, S3C2410_UTXH, ch);
1131 s3c24xx_serial_console_write(struct console *co, const char *s,
1134 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
1138 s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1139 int *parity, int *bits)
1141 struct s3c24xx_uart_clksrc clksrc;
1145 unsigned int ubrdiv;
1148 ulcon = rd_regl(port, S3C2410_ULCON);
1149 ucon = rd_regl(port, S3C2410_UCON);
1150 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1152 dbg("s3c24xx_serial_get_options: port=%p\n"
1153 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1154 port, ulcon, ucon, ubrdiv);
1156 if ((ucon & 0xf) != 0) {
1157 /* consider the serial port configured if the tx/rx mode set */
1159 switch (ulcon & S3C2410_LCON_CSMASK) {
1160 case S3C2410_LCON_CS5:
1163 case S3C2410_LCON_CS6:
1166 case S3C2410_LCON_CS7:
1170 case S3C2410_LCON_CS8:
1175 switch (ulcon & S3C2410_LCON_PMASK) {
1176 case S3C2410_LCON_PEVEN:
1180 case S3C2410_LCON_PODD:
1184 case S3C2410_LCON_PNONE:
1189 /* now calculate the baud rate */
1191 s3c24xx_serial_getsource(port, &clksrc);
1193 clk = clk_get(port->dev, clksrc.name);
1194 if (!IS_ERR(clk) && clk != NULL)
1195 rate = clk_get_rate(clk) / clksrc.divisor;
1200 *baud = rate / (16 * (ubrdiv + 1));
1201 dbg("calculated baud %d\n", *baud);
1206 /* s3c24xx_serial_init_ports
1208 * initialise the serial ports from the machine provided initialisation
1212 static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info *info)
1214 struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1215 struct platform_device **platdev_ptr;
1218 dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1220 platdev_ptr = s3c24xx_uart_devs;
1222 for (i = 0; i < NR_PORTS; i++, ptr++, platdev_ptr++) {
1223 s3c24xx_serial_init_port(ptr, info, *platdev_ptr);
1230 s3c24xx_serial_console_setup(struct console *co, char *options)
1232 struct uart_port *port;
1238 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1239 co, co->index, options);
1241 /* is this a valid port */
1243 if (co->index == -1 || co->index >= NR_PORTS)
1246 port = &s3c24xx_serial_ports[co->index].port;
1248 /* is the port configured? */
1250 if (port->mapbase == 0x0) {
1252 port = &s3c24xx_serial_ports[co->index].port;
1257 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1260 * Check whether an invalid uart number has been specified, and
1261 * if so, search for the first available port that does have
1265 uart_parse_options(options, &baud, &parity, &bits, &flow);
1267 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1269 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1271 return uart_set_options(port, co, baud, parity, bits, flow);
1274 /* s3c24xx_serial_initconsole
1276 * initialise the console from one of the uart drivers
1279 static struct console s3c24xx_serial_console = {
1280 .name = S3C24XX_SERIAL_NAME,
1281 .device = uart_console_device,
1282 .flags = CON_PRINTBUFFER,
1284 .write = s3c24xx_serial_console_write,
1285 .setup = s3c24xx_serial_console_setup
1288 int s3c24xx_serial_initconsole(struct platform_driver *drv,
1289 struct s3c24xx_uart_info *info)
1292 struct platform_device *dev = s3c24xx_uart_devs[0];
1294 dbg("s3c24xx_serial_initconsole\n");
1296 /* select driver based on the cpu */
1299 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1303 if (strcmp(dev->name, drv->driver.name) != 0)
1306 s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1307 s3c24xx_serial_init_ports(info);
1309 register_console(&s3c24xx_serial_console);
1313 #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
1315 MODULE_DESCRIPTION("Samsung SoC Serial port driver");
1316 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1317 MODULE_LICENSE("GPL v2");