tty/serial: Add explicit PORT_TEGRA type
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / tty / serial / samsung.c
blobf66f6482930363a9cbd2b69af11588021970cbfe
1 /*
2 * Driver core for Samsung SoC onboard UARTs.
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 /* Hote on 2410 error handling
14 * The s3c2410 manual has a love/hate affair with the contents of the
15 * UERSTAT register in the UART blocks, and keeps marking some of the
16 * error bits as reserved. Having checked with the s3c2410x01,
17 * it copes with BREAKs properly, so I am happy to ignore the RESERVED
18 * feature from the latter versions of the manual.
20 * If it becomes aparrent that latter versions of the 2410 remove these
21 * bits, then action will have to be taken to differentiate the versions
22 * and change the policy on BREAK
24 * BJD, 04-Nov-2004
27 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
28 #define SUPPORT_SYSRQ
29 #endif
31 #include <linux/module.h>
32 #include <linux/ioport.h>
33 #include <linux/io.h>
34 #include <linux/platform_device.h>
35 #include <linux/init.h>
36 #include <linux/sysrq.h>
37 #include <linux/console.h>
38 #include <linux/tty.h>
39 #include <linux/tty_flip.h>
40 #include <linux/serial_core.h>
41 #include <linux/serial.h>
42 #include <linux/delay.h>
43 #include <linux/clk.h>
44 #include <linux/cpufreq.h>
46 #include <asm/irq.h>
48 #include <mach/hardware.h>
49 #include <mach/map.h>
51 #include <plat/regs-serial.h>
53 #include "samsung.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 /* macros to change one thing to another */
63 #define tx_enabled(port) ((port)->unused[0])
64 #define rx_enabled(port) ((port)->unused[1])
66 /* flag to ignore all characters coming in */
67 #define RXSTAT_DUMMY_READ (0x10000000)
69 static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
71 return container_of(port, struct s3c24xx_uart_port, port);
74 /* translate a port to the device name */
76 static inline const char *s3c24xx_serial_portname(struct uart_port *port)
78 return to_platform_device(port->dev)->name;
81 static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
83 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
86 static void s3c24xx_serial_rx_enable(struct uart_port *port)
88 unsigned long flags;
89 unsigned int ucon, ufcon;
90 int count = 10000;
92 spin_lock_irqsave(&port->lock, flags);
94 while (--count && !s3c24xx_serial_txempty_nofifo(port))
95 udelay(100);
97 ufcon = rd_regl(port, S3C2410_UFCON);
98 ufcon |= S3C2410_UFCON_RESETRX;
99 wr_regl(port, S3C2410_UFCON, ufcon);
101 ucon = rd_regl(port, S3C2410_UCON);
102 ucon |= S3C2410_UCON_RXIRQMODE;
103 wr_regl(port, S3C2410_UCON, ucon);
105 rx_enabled(port) = 1;
106 spin_unlock_irqrestore(&port->lock, flags);
109 static void s3c24xx_serial_rx_disable(struct uart_port *port)
111 unsigned long flags;
112 unsigned int ucon;
114 spin_lock_irqsave(&port->lock, flags);
116 ucon = rd_regl(port, S3C2410_UCON);
117 ucon &= ~S3C2410_UCON_RXIRQMODE;
118 wr_regl(port, S3C2410_UCON, ucon);
120 rx_enabled(port) = 0;
121 spin_unlock_irqrestore(&port->lock, flags);
124 static void s3c24xx_serial_stop_tx(struct uart_port *port)
126 struct s3c24xx_uart_port *ourport = to_ourport(port);
128 if (tx_enabled(port)) {
129 disable_irq_nosync(ourport->tx_irq);
130 tx_enabled(port) = 0;
131 if (port->flags & UPF_CONS_FLOW)
132 s3c24xx_serial_rx_enable(port);
136 static void s3c24xx_serial_start_tx(struct uart_port *port)
138 struct s3c24xx_uart_port *ourport = to_ourport(port);
140 if (!tx_enabled(port)) {
141 if (port->flags & UPF_CONS_FLOW)
142 s3c24xx_serial_rx_disable(port);
144 enable_irq(ourport->tx_irq);
145 tx_enabled(port) = 1;
150 static void s3c24xx_serial_stop_rx(struct uart_port *port)
152 struct s3c24xx_uart_port *ourport = to_ourport(port);
154 if (rx_enabled(port)) {
155 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
156 disable_irq_nosync(ourport->rx_irq);
157 rx_enabled(port) = 0;
161 static void s3c24xx_serial_enable_ms(struct uart_port *port)
165 static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
167 return to_ourport(port)->info;
170 static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
172 if (port->dev == NULL)
173 return NULL;
175 return (struct s3c2410_uartcfg *)port->dev->platform_data;
178 static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
179 unsigned long ufstat)
181 struct s3c24xx_uart_info *info = ourport->info;
183 if (ufstat & info->rx_fifofull)
184 return info->fifosize;
186 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
190 /* ? - where has parity gone?? */
191 #define S3C2410_UERSTAT_PARITY (0x1000)
193 static irqreturn_t
194 s3c24xx_serial_rx_chars(int irq, void *dev_id)
196 struct s3c24xx_uart_port *ourport = dev_id;
197 struct uart_port *port = &ourport->port;
198 struct tty_struct *tty = port->state->port.tty;
199 unsigned int ufcon, ch, flag, ufstat, uerstat;
200 int max_count = 64;
202 while (max_count-- > 0) {
203 ufcon = rd_regl(port, S3C2410_UFCON);
204 ufstat = rd_regl(port, S3C2410_UFSTAT);
206 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
207 break;
209 uerstat = rd_regl(port, S3C2410_UERSTAT);
210 ch = rd_regb(port, S3C2410_URXH);
212 if (port->flags & UPF_CONS_FLOW) {
213 int txe = s3c24xx_serial_txempty_nofifo(port);
215 if (rx_enabled(port)) {
216 if (!txe) {
217 rx_enabled(port) = 0;
218 continue;
220 } else {
221 if (txe) {
222 ufcon |= S3C2410_UFCON_RESETRX;
223 wr_regl(port, S3C2410_UFCON, ufcon);
224 rx_enabled(port) = 1;
225 goto out;
227 continue;
231 /* insert the character into the buffer */
233 flag = TTY_NORMAL;
234 port->icount.rx++;
236 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
237 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
238 ch, uerstat);
240 /* check for break */
241 if (uerstat & S3C2410_UERSTAT_BREAK) {
242 dbg("break!\n");
243 port->icount.brk++;
244 if (uart_handle_break(port))
245 goto ignore_char;
248 if (uerstat & S3C2410_UERSTAT_FRAME)
249 port->icount.frame++;
250 if (uerstat & S3C2410_UERSTAT_OVERRUN)
251 port->icount.overrun++;
253 uerstat &= port->read_status_mask;
255 if (uerstat & S3C2410_UERSTAT_BREAK)
256 flag = TTY_BREAK;
257 else if (uerstat & S3C2410_UERSTAT_PARITY)
258 flag = TTY_PARITY;
259 else if (uerstat & (S3C2410_UERSTAT_FRAME |
260 S3C2410_UERSTAT_OVERRUN))
261 flag = TTY_FRAME;
264 if (uart_handle_sysrq_char(port, ch))
265 goto ignore_char;
267 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
268 ch, flag);
270 ignore_char:
271 continue;
273 tty_flip_buffer_push(tty);
275 out:
276 return IRQ_HANDLED;
279 static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
281 struct s3c24xx_uart_port *ourport = id;
282 struct uart_port *port = &ourport->port;
283 struct circ_buf *xmit = &port->state->xmit;
284 int count = 256;
286 if (port->x_char) {
287 wr_regb(port, S3C2410_UTXH, port->x_char);
288 port->icount.tx++;
289 port->x_char = 0;
290 goto out;
293 /* if there isn't anything more to transmit, or the uart is now
294 * stopped, disable the uart and exit
297 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
298 s3c24xx_serial_stop_tx(port);
299 goto out;
302 /* try and drain the buffer... */
304 while (!uart_circ_empty(xmit) && count-- > 0) {
305 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
306 break;
308 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
309 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
310 port->icount.tx++;
313 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
314 uart_write_wakeup(port);
316 if (uart_circ_empty(xmit))
317 s3c24xx_serial_stop_tx(port);
319 out:
320 return IRQ_HANDLED;
323 static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
325 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
326 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
327 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
329 if (ufcon & S3C2410_UFCON_FIFOMODE) {
330 if ((ufstat & info->tx_fifomask) != 0 ||
331 (ufstat & info->tx_fifofull))
332 return 0;
334 return 1;
337 return s3c24xx_serial_txempty_nofifo(port);
340 /* no modem control lines */
341 static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
343 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
345 if (umstat & S3C2410_UMSTAT_CTS)
346 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
347 else
348 return TIOCM_CAR | TIOCM_DSR;
351 static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
353 /* todo - possibly remove AFC and do manual CTS */
356 static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
358 unsigned long flags;
359 unsigned int ucon;
361 spin_lock_irqsave(&port->lock, flags);
363 ucon = rd_regl(port, S3C2410_UCON);
365 if (break_state)
366 ucon |= S3C2410_UCON_SBREAK;
367 else
368 ucon &= ~S3C2410_UCON_SBREAK;
370 wr_regl(port, S3C2410_UCON, ucon);
372 spin_unlock_irqrestore(&port->lock, flags);
375 static void s3c24xx_serial_shutdown(struct uart_port *port)
377 struct s3c24xx_uart_port *ourport = to_ourport(port);
379 if (ourport->tx_claimed) {
380 free_irq(ourport->tx_irq, ourport);
381 tx_enabled(port) = 0;
382 ourport->tx_claimed = 0;
385 if (ourport->rx_claimed) {
386 free_irq(ourport->rx_irq, ourport);
387 ourport->rx_claimed = 0;
388 rx_enabled(port) = 0;
393 static int s3c24xx_serial_startup(struct uart_port *port)
395 struct s3c24xx_uart_port *ourport = to_ourport(port);
396 int ret;
398 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
399 port->mapbase, port->membase);
401 rx_enabled(port) = 1;
403 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
404 s3c24xx_serial_portname(port), ourport);
406 if (ret != 0) {
407 printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
408 return ret;
411 ourport->rx_claimed = 1;
413 dbg("requesting tx irq...\n");
415 tx_enabled(port) = 1;
417 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
418 s3c24xx_serial_portname(port), ourport);
420 if (ret) {
421 printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
422 goto err;
425 ourport->tx_claimed = 1;
427 dbg("s3c24xx_serial_startup ok\n");
429 /* the port reset code should have done the correct
430 * register setup for the port controls */
432 return ret;
434 err:
435 s3c24xx_serial_shutdown(port);
436 return ret;
439 /* power power management control */
441 static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
442 unsigned int old)
444 struct s3c24xx_uart_port *ourport = to_ourport(port);
446 ourport->pm_level = level;
448 switch (level) {
449 case 3:
450 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
451 clk_disable(ourport->baudclk);
453 clk_disable(ourport->clk);
454 break;
456 case 0:
457 clk_enable(ourport->clk);
459 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
460 clk_enable(ourport->baudclk);
462 break;
463 default:
464 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
468 /* baud rate calculation
470 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
471 * of different sources, including the peripheral clock ("pclk") and an
472 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
473 * with a programmable extra divisor.
475 * The following code goes through the clock sources, and calculates the
476 * baud clocks (and the resultant actual baud rates) and then tries to
477 * pick the closest one and select that.
482 #define MAX_CLKS (8)
484 static struct s3c24xx_uart_clksrc tmp_clksrc = {
485 .name = "pclk",
486 .min_baud = 0,
487 .max_baud = 0,
488 .divisor = 1,
491 static inline int
492 s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
494 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
496 return (info->get_clksrc)(port, c);
499 static inline int
500 s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
502 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
504 return (info->set_clksrc)(port, c);
507 struct baud_calc {
508 struct s3c24xx_uart_clksrc *clksrc;
509 unsigned int calc;
510 unsigned int divslot;
511 unsigned int quot;
512 struct clk *src;
515 static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
516 struct uart_port *port,
517 struct s3c24xx_uart_clksrc *clksrc,
518 unsigned int baud)
520 struct s3c24xx_uart_port *ourport = to_ourport(port);
521 unsigned long rate;
523 calc->src = clk_get(port->dev, clksrc->name);
524 if (calc->src == NULL || IS_ERR(calc->src))
525 return 0;
527 rate = clk_get_rate(calc->src);
528 rate /= clksrc->divisor;
530 calc->clksrc = clksrc;
532 if (ourport->info->has_divslot) {
533 unsigned long div = rate / baud;
535 /* The UDIVSLOT register on the newer UARTs allows us to
536 * get a divisor adjustment of 1/16th on the baud clock.
538 * We don't keep the UDIVSLOT value (the 16ths we calculated
539 * by not multiplying the baud by 16) as it is easy enough
540 * to recalculate.
543 calc->quot = div / 16;
544 calc->calc = rate / div;
545 } else {
546 calc->quot = (rate + (8 * baud)) / (16 * baud);
547 calc->calc = (rate / (calc->quot * 16));
550 calc->quot--;
551 return 1;
554 static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
555 struct s3c24xx_uart_clksrc **clksrc,
556 struct clk **clk,
557 unsigned int baud)
559 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
560 struct s3c24xx_uart_clksrc *clkp;
561 struct baud_calc res[MAX_CLKS];
562 struct baud_calc *resptr, *best, *sptr;
563 int i;
565 clkp = cfg->clocks;
566 best = NULL;
568 if (cfg->clocks_size < 2) {
569 if (cfg->clocks_size == 0)
570 clkp = &tmp_clksrc;
572 /* check to see if we're sourcing fclk, and if so we're
573 * going to have to update the clock source
576 if (strcmp(clkp->name, "fclk") == 0) {
577 struct s3c24xx_uart_clksrc src;
579 s3c24xx_serial_getsource(port, &src);
581 /* check that the port already using fclk, and if
582 * not, then re-select fclk
585 if (strcmp(src.name, clkp->name) == 0) {
586 s3c24xx_serial_setsource(port, clkp);
587 s3c24xx_serial_getsource(port, &src);
590 clkp->divisor = src.divisor;
593 s3c24xx_serial_calcbaud(res, port, clkp, baud);
594 best = res;
595 resptr = best + 1;
596 } else {
597 resptr = res;
599 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
600 if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
601 resptr++;
605 /* ok, we now need to select the best clock we found */
607 if (!best) {
608 unsigned int deviation = (1<<30)|((1<<30)-1);
609 int calc_deviation;
611 for (sptr = res; sptr < resptr; sptr++) {
612 calc_deviation = baud - sptr->calc;
613 if (calc_deviation < 0)
614 calc_deviation = -calc_deviation;
616 if (calc_deviation < deviation) {
617 best = sptr;
618 deviation = calc_deviation;
623 /* store results to pass back */
625 *clksrc = best->clksrc;
626 *clk = best->src;
628 return best->quot;
631 /* udivslot_table[]
633 * This table takes the fractional value of the baud divisor and gives
634 * the recommended setting for the UDIVSLOT register.
636 static u16 udivslot_table[16] = {
637 [0] = 0x0000,
638 [1] = 0x0080,
639 [2] = 0x0808,
640 [3] = 0x0888,
641 [4] = 0x2222,
642 [5] = 0x4924,
643 [6] = 0x4A52,
644 [7] = 0x54AA,
645 [8] = 0x5555,
646 [9] = 0xD555,
647 [10] = 0xD5D5,
648 [11] = 0xDDD5,
649 [12] = 0xDDDD,
650 [13] = 0xDFDD,
651 [14] = 0xDFDF,
652 [15] = 0xFFDF,
655 static void s3c24xx_serial_set_termios(struct uart_port *port,
656 struct ktermios *termios,
657 struct ktermios *old)
659 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
660 struct s3c24xx_uart_port *ourport = to_ourport(port);
661 struct s3c24xx_uart_clksrc *clksrc = NULL;
662 struct clk *clk = NULL;
663 unsigned long flags;
664 unsigned int baud, quot;
665 unsigned int ulcon;
666 unsigned int umcon;
667 unsigned int udivslot = 0;
670 * We don't support modem control lines.
672 termios->c_cflag &= ~(HUPCL | CMSPAR);
673 termios->c_cflag |= CLOCAL;
676 * Ask the core to calculate the divisor for us.
679 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
681 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
682 quot = port->custom_divisor;
683 else
684 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
686 /* check to see if we need to change clock source */
688 if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
689 dbg("selecting clock %p\n", clk);
690 s3c24xx_serial_setsource(port, clksrc);
692 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
693 clk_disable(ourport->baudclk);
694 ourport->baudclk = NULL;
697 clk_enable(clk);
699 ourport->clksrc = clksrc;
700 ourport->baudclk = clk;
701 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
704 if (ourport->info->has_divslot) {
705 unsigned int div = ourport->baudclk_rate / baud;
707 if (cfg->has_fracval) {
708 udivslot = (div & 15);
709 dbg("fracval = %04x\n", udivslot);
710 } else {
711 udivslot = udivslot_table[div & 15];
712 dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
716 switch (termios->c_cflag & CSIZE) {
717 case CS5:
718 dbg("config: 5bits/char\n");
719 ulcon = S3C2410_LCON_CS5;
720 break;
721 case CS6:
722 dbg("config: 6bits/char\n");
723 ulcon = S3C2410_LCON_CS6;
724 break;
725 case CS7:
726 dbg("config: 7bits/char\n");
727 ulcon = S3C2410_LCON_CS7;
728 break;
729 case CS8:
730 default:
731 dbg("config: 8bits/char\n");
732 ulcon = S3C2410_LCON_CS8;
733 break;
736 /* preserve original lcon IR settings */
737 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
739 if (termios->c_cflag & CSTOPB)
740 ulcon |= S3C2410_LCON_STOPB;
742 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
744 if (termios->c_cflag & PARENB) {
745 if (termios->c_cflag & PARODD)
746 ulcon |= S3C2410_LCON_PODD;
747 else
748 ulcon |= S3C2410_LCON_PEVEN;
749 } else {
750 ulcon |= S3C2410_LCON_PNONE;
753 spin_lock_irqsave(&port->lock, flags);
755 dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
756 ulcon, quot, udivslot);
758 wr_regl(port, S3C2410_ULCON, ulcon);
759 wr_regl(port, S3C2410_UBRDIV, quot);
760 wr_regl(port, S3C2410_UMCON, umcon);
762 if (ourport->info->has_divslot)
763 wr_regl(port, S3C2443_DIVSLOT, udivslot);
765 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
766 rd_regl(port, S3C2410_ULCON),
767 rd_regl(port, S3C2410_UCON),
768 rd_regl(port, S3C2410_UFCON));
771 * Update the per-port timeout.
773 uart_update_timeout(port, termios->c_cflag, baud);
776 * Which character status flags are we interested in?
778 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
779 if (termios->c_iflag & INPCK)
780 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
783 * Which character status flags should we ignore?
785 port->ignore_status_mask = 0;
786 if (termios->c_iflag & IGNPAR)
787 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
788 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
789 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
792 * Ignore all characters if CREAD is not set.
794 if ((termios->c_cflag & CREAD) == 0)
795 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
797 spin_unlock_irqrestore(&port->lock, flags);
800 static const char *s3c24xx_serial_type(struct uart_port *port)
802 switch (port->type) {
803 case PORT_S3C2410:
804 return "S3C2410";
805 case PORT_S3C2440:
806 return "S3C2440";
807 case PORT_S3C2412:
808 return "S3C2412";
809 case PORT_S3C6400:
810 return "S3C6400/10";
811 default:
812 return NULL;
816 #define MAP_SIZE (0x100)
818 static void s3c24xx_serial_release_port(struct uart_port *port)
820 release_mem_region(port->mapbase, MAP_SIZE);
823 static int s3c24xx_serial_request_port(struct uart_port *port)
825 const char *name = s3c24xx_serial_portname(port);
826 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
829 static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
831 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
833 if (flags & UART_CONFIG_TYPE &&
834 s3c24xx_serial_request_port(port) == 0)
835 port->type = info->type;
839 * verify the new serial_struct (for TIOCSSERIAL).
841 static int
842 s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
844 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
846 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
847 return -EINVAL;
849 return 0;
853 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
855 static struct console s3c24xx_serial_console;
857 #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
858 #else
859 #define S3C24XX_SERIAL_CONSOLE NULL
860 #endif
862 static struct uart_ops s3c24xx_serial_ops = {
863 .pm = s3c24xx_serial_pm,
864 .tx_empty = s3c24xx_serial_tx_empty,
865 .get_mctrl = s3c24xx_serial_get_mctrl,
866 .set_mctrl = s3c24xx_serial_set_mctrl,
867 .stop_tx = s3c24xx_serial_stop_tx,
868 .start_tx = s3c24xx_serial_start_tx,
869 .stop_rx = s3c24xx_serial_stop_rx,
870 .enable_ms = s3c24xx_serial_enable_ms,
871 .break_ctl = s3c24xx_serial_break_ctl,
872 .startup = s3c24xx_serial_startup,
873 .shutdown = s3c24xx_serial_shutdown,
874 .set_termios = s3c24xx_serial_set_termios,
875 .type = s3c24xx_serial_type,
876 .release_port = s3c24xx_serial_release_port,
877 .request_port = s3c24xx_serial_request_port,
878 .config_port = s3c24xx_serial_config_port,
879 .verify_port = s3c24xx_serial_verify_port,
883 static struct uart_driver s3c24xx_uart_drv = {
884 .owner = THIS_MODULE,
885 .driver_name = "s3c2410_serial",
886 .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
887 .cons = S3C24XX_SERIAL_CONSOLE,
888 .dev_name = S3C24XX_SERIAL_NAME,
889 .major = S3C24XX_SERIAL_MAJOR,
890 .minor = S3C24XX_SERIAL_MINOR,
893 static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
894 [0] = {
895 .port = {
896 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
897 .iotype = UPIO_MEM,
898 .irq = IRQ_S3CUART_RX0,
899 .uartclk = 0,
900 .fifosize = 16,
901 .ops = &s3c24xx_serial_ops,
902 .flags = UPF_BOOT_AUTOCONF,
903 .line = 0,
906 [1] = {
907 .port = {
908 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
909 .iotype = UPIO_MEM,
910 .irq = IRQ_S3CUART_RX1,
911 .uartclk = 0,
912 .fifosize = 16,
913 .ops = &s3c24xx_serial_ops,
914 .flags = UPF_BOOT_AUTOCONF,
915 .line = 1,
918 #if CONFIG_SERIAL_SAMSUNG_UARTS > 2
920 [2] = {
921 .port = {
922 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
923 .iotype = UPIO_MEM,
924 .irq = IRQ_S3CUART_RX2,
925 .uartclk = 0,
926 .fifosize = 16,
927 .ops = &s3c24xx_serial_ops,
928 .flags = UPF_BOOT_AUTOCONF,
929 .line = 2,
932 #endif
933 #if CONFIG_SERIAL_SAMSUNG_UARTS > 3
934 [3] = {
935 .port = {
936 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
937 .iotype = UPIO_MEM,
938 .irq = IRQ_S3CUART_RX3,
939 .uartclk = 0,
940 .fifosize = 16,
941 .ops = &s3c24xx_serial_ops,
942 .flags = UPF_BOOT_AUTOCONF,
943 .line = 3,
946 #endif
949 /* s3c24xx_serial_resetport
951 * wrapper to call the specific reset for this port (reset the fifos
952 * and the settings)
955 static inline int s3c24xx_serial_resetport(struct uart_port *port,
956 struct s3c2410_uartcfg *cfg)
958 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
960 return (info->reset_port)(port, cfg);
964 #ifdef CONFIG_CPU_FREQ
966 static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
967 unsigned long val, void *data)
969 struct s3c24xx_uart_port *port;
970 struct uart_port *uport;
972 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
973 uport = &port->port;
975 /* check to see if port is enabled */
977 if (port->pm_level != 0)
978 return 0;
980 /* try and work out if the baudrate is changing, we can detect
981 * a change in rate, but we do not have support for detecting
982 * a disturbance in the clock-rate over the change.
985 if (IS_ERR(port->clk))
986 goto exit;
988 if (port->baudclk_rate == clk_get_rate(port->clk))
989 goto exit;
991 if (val == CPUFREQ_PRECHANGE) {
992 /* we should really shut the port down whilst the
993 * frequency change is in progress. */
995 } else if (val == CPUFREQ_POSTCHANGE) {
996 struct ktermios *termios;
997 struct tty_struct *tty;
999 if (uport->state == NULL)
1000 goto exit;
1002 tty = uport->state->port.tty;
1004 if (tty == NULL)
1005 goto exit;
1007 termios = tty->termios;
1009 if (termios == NULL) {
1010 printk(KERN_WARNING "%s: no termios?\n", __func__);
1011 goto exit;
1014 s3c24xx_serial_set_termios(uport, termios, NULL);
1017 exit:
1018 return 0;
1021 static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1023 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1025 return cpufreq_register_notifier(&port->freq_transition,
1026 CPUFREQ_TRANSITION_NOTIFIER);
1029 static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1031 cpufreq_unregister_notifier(&port->freq_transition,
1032 CPUFREQ_TRANSITION_NOTIFIER);
1035 #else
1036 static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1038 return 0;
1041 static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1044 #endif
1046 /* s3c24xx_serial_init_port
1048 * initialise a single serial port from the platform device given
1051 static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1052 struct s3c24xx_uart_info *info,
1053 struct platform_device *platdev)
1055 struct uart_port *port = &ourport->port;
1056 struct s3c2410_uartcfg *cfg;
1057 struct resource *res;
1058 int ret;
1060 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1062 if (platdev == NULL)
1063 return -ENODEV;
1065 cfg = s3c24xx_dev_to_cfg(&platdev->dev);
1067 if (port->mapbase != 0)
1068 return 0;
1070 if (cfg->hwport > CONFIG_SERIAL_SAMSUNG_UARTS) {
1071 printk(KERN_ERR "%s: port %d bigger than %d\n", __func__,
1072 cfg->hwport, CONFIG_SERIAL_SAMSUNG_UARTS);
1073 return -ERANGE;
1076 /* setup info for port */
1077 port->dev = &platdev->dev;
1078 ourport->info = info;
1080 /* copy the info in from provided structure */
1081 ourport->port.fifosize = info->fifosize;
1083 dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
1085 port->uartclk = 1;
1087 if (cfg->uart_flags & UPF_CONS_FLOW) {
1088 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1089 port->flags |= UPF_CONS_FLOW;
1092 /* sort our the physical and virtual addresses for each UART */
1094 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1095 if (res == NULL) {
1096 printk(KERN_ERR "failed to find memory resource for uart\n");
1097 return -EINVAL;
1100 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1102 port->mapbase = res->start;
1103 port->membase = S3C_VA_UART + (res->start & 0xfffff);
1104 ret = platform_get_irq(platdev, 0);
1105 if (ret < 0)
1106 port->irq = 0;
1107 else {
1108 port->irq = ret;
1109 ourport->rx_irq = ret;
1110 ourport->tx_irq = ret + 1;
1113 ret = platform_get_irq(platdev, 1);
1114 if (ret > 0)
1115 ourport->tx_irq = ret;
1117 ourport->clk = clk_get(&platdev->dev, "uart");
1119 dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
1120 port->mapbase, port->membase, port->irq,
1121 ourport->rx_irq, ourport->tx_irq, port->uartclk);
1123 /* reset the fifos (and setup the uart) */
1124 s3c24xx_serial_resetport(port, cfg);
1125 return 0;
1128 static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
1129 struct device_attribute *attr,
1130 char *buf)
1132 struct uart_port *port = s3c24xx_dev_to_port(dev);
1133 struct s3c24xx_uart_port *ourport = to_ourport(port);
1135 return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->clksrc->name);
1138 static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
1140 /* Device driver serial port probe */
1142 static int probe_index;
1144 int s3c24xx_serial_probe(struct platform_device *dev,
1145 struct s3c24xx_uart_info *info)
1147 struct s3c24xx_uart_port *ourport;
1148 int ret;
1150 dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
1152 ourport = &s3c24xx_serial_ports[probe_index];
1153 probe_index++;
1155 dbg("%s: initialising port %p...\n", __func__, ourport);
1157 ret = s3c24xx_serial_init_port(ourport, info, dev);
1158 if (ret < 0)
1159 goto probe_err;
1161 dbg("%s: adding port\n", __func__);
1162 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1163 platform_set_drvdata(dev, &ourport->port);
1165 ret = device_create_file(&dev->dev, &dev_attr_clock_source);
1166 if (ret < 0)
1167 printk(KERN_ERR "%s: failed to add clksrc attr.\n", __func__);
1169 ret = s3c24xx_serial_cpufreq_register(ourport);
1170 if (ret < 0)
1171 dev_err(&dev->dev, "failed to add cpufreq notifier\n");
1173 return 0;
1175 probe_err:
1176 return ret;
1179 EXPORT_SYMBOL_GPL(s3c24xx_serial_probe);
1181 int __devexit s3c24xx_serial_remove(struct platform_device *dev)
1183 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1185 if (port) {
1186 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
1187 device_remove_file(&dev->dev, &dev_attr_clock_source);
1188 uart_remove_one_port(&s3c24xx_uart_drv, port);
1191 return 0;
1194 EXPORT_SYMBOL_GPL(s3c24xx_serial_remove);
1196 /* UART power management code */
1198 #ifdef CONFIG_PM
1200 static int s3c24xx_serial_suspend(struct platform_device *dev, pm_message_t state)
1202 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1204 if (port)
1205 uart_suspend_port(&s3c24xx_uart_drv, port);
1207 return 0;
1210 static int s3c24xx_serial_resume(struct platform_device *dev)
1212 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1213 struct s3c24xx_uart_port *ourport = to_ourport(port);
1215 if (port) {
1216 clk_enable(ourport->clk);
1217 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1218 clk_disable(ourport->clk);
1220 uart_resume_port(&s3c24xx_uart_drv, port);
1223 return 0;
1225 #endif
1227 int s3c24xx_serial_init(struct platform_driver *drv,
1228 struct s3c24xx_uart_info *info)
1230 dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
1232 #ifdef CONFIG_PM
1233 drv->suspend = s3c24xx_serial_suspend;
1234 drv->resume = s3c24xx_serial_resume;
1235 #endif
1237 return platform_driver_register(drv);
1240 EXPORT_SYMBOL_GPL(s3c24xx_serial_init);
1242 /* module initialisation code */
1244 static int __init s3c24xx_serial_modinit(void)
1246 int ret;
1248 ret = uart_register_driver(&s3c24xx_uart_drv);
1249 if (ret < 0) {
1250 printk(KERN_ERR "failed to register UART driver\n");
1251 return -1;
1254 return 0;
1257 static void __exit s3c24xx_serial_modexit(void)
1259 uart_unregister_driver(&s3c24xx_uart_drv);
1262 module_init(s3c24xx_serial_modinit);
1263 module_exit(s3c24xx_serial_modexit);
1265 /* Console code */
1267 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1269 static struct uart_port *cons_uart;
1271 static int
1272 s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1274 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1275 unsigned long ufstat, utrstat;
1277 if (ufcon & S3C2410_UFCON_FIFOMODE) {
1278 /* fifo mode - check amount of data in fifo registers... */
1280 ufstat = rd_regl(port, S3C2410_UFSTAT);
1281 return (ufstat & info->tx_fifofull) ? 0 : 1;
1284 /* in non-fifo mode, we go and use the tx buffer empty */
1286 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1287 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1290 static void
1291 s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
1293 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1294 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1295 barrier();
1296 wr_regb(cons_uart, S3C2410_UTXH, ch);
1299 static void
1300 s3c24xx_serial_console_write(struct console *co, const char *s,
1301 unsigned int count)
1303 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
1306 static void __init
1307 s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1308 int *parity, int *bits)
1310 struct s3c24xx_uart_clksrc clksrc;
1311 struct clk *clk;
1312 unsigned int ulcon;
1313 unsigned int ucon;
1314 unsigned int ubrdiv;
1315 unsigned long rate;
1317 ulcon = rd_regl(port, S3C2410_ULCON);
1318 ucon = rd_regl(port, S3C2410_UCON);
1319 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1321 dbg("s3c24xx_serial_get_options: port=%p\n"
1322 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1323 port, ulcon, ucon, ubrdiv);
1325 if ((ucon & 0xf) != 0) {
1326 /* consider the serial port configured if the tx/rx mode set */
1328 switch (ulcon & S3C2410_LCON_CSMASK) {
1329 case S3C2410_LCON_CS5:
1330 *bits = 5;
1331 break;
1332 case S3C2410_LCON_CS6:
1333 *bits = 6;
1334 break;
1335 case S3C2410_LCON_CS7:
1336 *bits = 7;
1337 break;
1338 default:
1339 case S3C2410_LCON_CS8:
1340 *bits = 8;
1341 break;
1344 switch (ulcon & S3C2410_LCON_PMASK) {
1345 case S3C2410_LCON_PEVEN:
1346 *parity = 'e';
1347 break;
1349 case S3C2410_LCON_PODD:
1350 *parity = 'o';
1351 break;
1353 case S3C2410_LCON_PNONE:
1354 default:
1355 *parity = 'n';
1358 /* now calculate the baud rate */
1360 s3c24xx_serial_getsource(port, &clksrc);
1362 clk = clk_get(port->dev, clksrc.name);
1363 if (!IS_ERR(clk) && clk != NULL)
1364 rate = clk_get_rate(clk) / clksrc.divisor;
1365 else
1366 rate = 1;
1369 *baud = rate / (16 * (ubrdiv + 1));
1370 dbg("calculated baud %d\n", *baud);
1375 /* s3c24xx_serial_init_ports
1377 * initialise the serial ports from the machine provided initialisation
1378 * data.
1381 static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info **info)
1383 struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1384 struct platform_device **platdev_ptr;
1385 int i;
1387 dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1389 platdev_ptr = s3c24xx_uart_devs;
1391 for (i = 0; i < CONFIG_SERIAL_SAMSUNG_UARTS; i++, ptr++, platdev_ptr++) {
1392 s3c24xx_serial_init_port(ptr, info[i], *platdev_ptr);
1395 return 0;
1398 static int __init
1399 s3c24xx_serial_console_setup(struct console *co, char *options)
1401 struct uart_port *port;
1402 int baud = 9600;
1403 int bits = 8;
1404 int parity = 'n';
1405 int flow = 'n';
1407 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1408 co, co->index, options);
1410 /* is this a valid port */
1412 if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
1413 co->index = 0;
1415 port = &s3c24xx_serial_ports[co->index].port;
1417 /* is the port configured? */
1419 if (port->mapbase == 0x0) {
1420 co->index = 0;
1421 port = &s3c24xx_serial_ports[co->index].port;
1424 cons_uart = port;
1426 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1429 * Check whether an invalid uart number has been specified, and
1430 * if so, search for the first available port that does have
1431 * console support.
1433 if (options)
1434 uart_parse_options(options, &baud, &parity, &bits, &flow);
1435 else
1436 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1438 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1440 return uart_set_options(port, co, baud, parity, bits, flow);
1443 /* s3c24xx_serial_initconsole
1445 * initialise the console from one of the uart drivers
1448 static struct console s3c24xx_serial_console = {
1449 .name = S3C24XX_SERIAL_NAME,
1450 .device = uart_console_device,
1451 .flags = CON_PRINTBUFFER,
1452 .index = -1,
1453 .write = s3c24xx_serial_console_write,
1454 .setup = s3c24xx_serial_console_setup
1457 int s3c24xx_serial_initconsole(struct platform_driver *drv,
1458 struct s3c24xx_uart_info **info)
1461 struct platform_device *dev = s3c24xx_uart_devs[0];
1463 dbg("s3c24xx_serial_initconsole\n");
1465 /* select driver based on the cpu */
1467 if (dev == NULL) {
1468 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1469 return 0;
1472 if (strcmp(dev->name, drv->driver.name) != 0)
1473 return 0;
1475 s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1476 s3c24xx_serial_init_ports(info);
1478 register_console(&s3c24xx_serial_console);
1479 return 0;
1482 #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
1484 MODULE_DESCRIPTION("Samsung SoC Serial port driver");
1485 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1486 MODULE_LICENSE("GPL v2");