GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / serial / samsung.c
blobb1156ba8ad1452c113fff20740a1d01596ab3cb2
1 /* linux/drivers/serial/samsuing.c
3 * Driver core for Samsung SoC onboard UARTs.
5 * Ben Dooks, Copyright (c) 2003-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
25 * BJD, 04-Nov-2004
28 #if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
29 #define SUPPORT_SYSRQ
30 #endif
32 #include <linux/module.h>
33 #include <linux/ioport.h>
34 #include <linux/io.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>
45 #include <linux/cpufreq.h>
47 #include <asm/irq.h>
49 #include <mach/hardware.h>
50 #include <mach/map.h>
52 #include <plat/regs-serial.h>
54 #include "samsung.h"
56 /* UART name and device definitions */
58 #define S3C24XX_SERIAL_NAME "ttySAC"
59 #define S3C24XX_SERIAL_MAJOR 204
60 #define S3C24XX_SERIAL_MINOR 64
62 /* macros to change one thing to another */
64 #define tx_enabled(port) ((port)->unused[0])
65 #define rx_enabled(port) ((port)->unused[1])
67 /* flag to ignore all characters comming in */
68 #define RXSTAT_DUMMY_READ (0x10000000)
70 static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
72 return container_of(port, struct s3c24xx_uart_port, port);
75 /* translate a port to the device name */
77 static inline const char *s3c24xx_serial_portname(struct uart_port *port)
79 return to_platform_device(port->dev)->name;
82 static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
84 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
87 static void s3c24xx_serial_rx_enable(struct uart_port *port)
89 unsigned long flags;
90 unsigned int ucon, ufcon;
91 int count = 10000;
93 spin_lock_irqsave(&port->lock, flags);
95 while (--count && !s3c24xx_serial_txempty_nofifo(port))
96 udelay(100);
98 ufcon = rd_regl(port, S3C2410_UFCON);
99 ufcon |= S3C2410_UFCON_RESETRX;
100 wr_regl(port, S3C2410_UFCON, ufcon);
102 ucon = rd_regl(port, S3C2410_UCON);
103 ucon |= S3C2410_UCON_RXIRQMODE;
104 wr_regl(port, S3C2410_UCON, ucon);
106 rx_enabled(port) = 1;
107 spin_unlock_irqrestore(&port->lock, flags);
110 static void s3c24xx_serial_rx_disable(struct uart_port *port)
112 unsigned long flags;
113 unsigned int ucon;
115 spin_lock_irqsave(&port->lock, flags);
117 ucon = rd_regl(port, S3C2410_UCON);
118 ucon &= ~S3C2410_UCON_RXIRQMODE;
119 wr_regl(port, S3C2410_UCON, ucon);
121 rx_enabled(port) = 0;
122 spin_unlock_irqrestore(&port->lock, flags);
125 static void s3c24xx_serial_stop_tx(struct uart_port *port)
127 struct s3c24xx_uart_port *ourport = to_ourport(port);
129 if (tx_enabled(port)) {
130 disable_irq_nosync(ourport->tx_irq);
131 tx_enabled(port) = 0;
132 if (port->flags & UPF_CONS_FLOW)
133 s3c24xx_serial_rx_enable(port);
137 static void s3c24xx_serial_start_tx(struct uart_port *port)
139 struct s3c24xx_uart_port *ourport = to_ourport(port);
141 if (!tx_enabled(port)) {
142 if (port->flags & UPF_CONS_FLOW)
143 s3c24xx_serial_rx_disable(port);
145 enable_irq(ourport->tx_irq);
146 tx_enabled(port) = 1;
151 static void s3c24xx_serial_stop_rx(struct uart_port *port)
153 struct s3c24xx_uart_port *ourport = to_ourport(port);
155 if (rx_enabled(port)) {
156 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
157 disable_irq_nosync(ourport->rx_irq);
158 rx_enabled(port) = 0;
162 static void s3c24xx_serial_enable_ms(struct uart_port *port)
166 static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
168 return to_ourport(port)->info;
171 static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
173 if (port->dev == NULL)
174 return NULL;
176 return (struct s3c2410_uartcfg *)port->dev->platform_data;
179 static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
180 unsigned long ufstat)
182 struct s3c24xx_uart_info *info = ourport->info;
184 if (ufstat & info->rx_fifofull)
185 return info->fifosize;
187 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
191 /* ? - where has parity gone?? */
192 #define S3C2410_UERSTAT_PARITY (0x1000)
194 static irqreturn_t
195 s3c24xx_serial_rx_chars(int irq, void *dev_id)
197 struct s3c24xx_uart_port *ourport = dev_id;
198 struct uart_port *port = &ourport->port;
199 struct tty_struct *tty = port->state->port.tty;
200 unsigned int ufcon, ch, flag, ufstat, uerstat;
201 int max_count = 64;
203 while (max_count-- > 0) {
204 ufcon = rd_regl(port, S3C2410_UFCON);
205 ufstat = rd_regl(port, S3C2410_UFSTAT);
207 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
208 break;
210 uerstat = rd_regl(port, S3C2410_UERSTAT);
211 ch = rd_regb(port, S3C2410_URXH);
213 if (port->flags & UPF_CONS_FLOW) {
214 int txe = s3c24xx_serial_txempty_nofifo(port);
216 if (rx_enabled(port)) {
217 if (!txe) {
218 rx_enabled(port) = 0;
219 continue;
221 } else {
222 if (txe) {
223 ufcon |= S3C2410_UFCON_RESETRX;
224 wr_regl(port, S3C2410_UFCON, ufcon);
225 rx_enabled(port) = 1;
226 goto out;
228 continue;
232 /* insert the character into the buffer */
234 flag = TTY_NORMAL;
235 port->icount.rx++;
237 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
238 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
239 ch, uerstat);
241 /* check for break */
242 if (uerstat & S3C2410_UERSTAT_BREAK) {
243 dbg("break!\n");
244 port->icount.brk++;
245 if (uart_handle_break(port))
246 goto ignore_char;
249 if (uerstat & S3C2410_UERSTAT_FRAME)
250 port->icount.frame++;
251 if (uerstat & S3C2410_UERSTAT_OVERRUN)
252 port->icount.overrun++;
254 uerstat &= port->read_status_mask;
256 if (uerstat & S3C2410_UERSTAT_BREAK)
257 flag = TTY_BREAK;
258 else if (uerstat & S3C2410_UERSTAT_PARITY)
259 flag = TTY_PARITY;
260 else if (uerstat & (S3C2410_UERSTAT_FRAME |
261 S3C2410_UERSTAT_OVERRUN))
262 flag = TTY_FRAME;
265 if (uart_handle_sysrq_char(port, ch))
266 goto ignore_char;
268 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
269 ch, flag);
271 ignore_char:
272 continue;
274 tty_flip_buffer_push(tty);
276 out:
277 return IRQ_HANDLED;
280 static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
282 struct s3c24xx_uart_port *ourport = id;
283 struct uart_port *port = &ourport->port;
284 struct circ_buf *xmit = &port->state->xmit;
285 int count = 256;
287 if (port->x_char) {
288 wr_regb(port, S3C2410_UTXH, port->x_char);
289 port->icount.tx++;
290 port->x_char = 0;
291 goto out;
294 /* if there isnt anything more to transmit, or the uart is now
295 * stopped, disable the uart and exit
298 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
299 s3c24xx_serial_stop_tx(port);
300 goto out;
303 /* try and drain the buffer... */
305 while (!uart_circ_empty(xmit) && count-- > 0) {
306 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
307 break;
309 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
310 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
311 port->icount.tx++;
314 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
315 uart_write_wakeup(port);
317 if (uart_circ_empty(xmit))
318 s3c24xx_serial_stop_tx(port);
320 out:
321 return IRQ_HANDLED;
324 static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
326 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
327 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
328 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
330 if (ufcon & S3C2410_UFCON_FIFOMODE) {
331 if ((ufstat & info->tx_fifomask) != 0 ||
332 (ufstat & info->tx_fifofull))
333 return 0;
335 return 1;
338 return s3c24xx_serial_txempty_nofifo(port);
341 /* no modem control lines */
342 static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
344 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
346 if (umstat & S3C2410_UMSTAT_CTS)
347 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
348 else
349 return TIOCM_CAR | TIOCM_DSR;
352 static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
354 /* todo - possibly remove AFC and do manual CTS */
357 static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
359 unsigned long flags;
360 unsigned int ucon;
362 spin_lock_irqsave(&port->lock, flags);
364 ucon = rd_regl(port, S3C2410_UCON);
366 if (break_state)
367 ucon |= S3C2410_UCON_SBREAK;
368 else
369 ucon &= ~S3C2410_UCON_SBREAK;
371 wr_regl(port, S3C2410_UCON, ucon);
373 spin_unlock_irqrestore(&port->lock, flags);
376 static void s3c24xx_serial_shutdown(struct uart_port *port)
378 struct s3c24xx_uart_port *ourport = to_ourport(port);
380 if (ourport->tx_claimed) {
381 free_irq(ourport->tx_irq, ourport);
382 tx_enabled(port) = 0;
383 ourport->tx_claimed = 0;
386 if (ourport->rx_claimed) {
387 free_irq(ourport->rx_irq, ourport);
388 ourport->rx_claimed = 0;
389 rx_enabled(port) = 0;
394 static int s3c24xx_serial_startup(struct uart_port *port)
396 struct s3c24xx_uart_port *ourport = to_ourport(port);
397 int ret;
399 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
400 port->mapbase, port->membase);
402 rx_enabled(port) = 1;
404 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
405 s3c24xx_serial_portname(port), ourport);
407 if (ret != 0) {
408 printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
409 return ret;
412 ourport->rx_claimed = 1;
414 dbg("requesting tx irq...\n");
416 tx_enabled(port) = 1;
418 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
419 s3c24xx_serial_portname(port), ourport);
421 if (ret) {
422 printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
423 goto err;
426 ourport->tx_claimed = 1;
428 dbg("s3c24xx_serial_startup ok\n");
430 /* the port reset code should have done the correct
431 * register setup for the port controls */
433 return ret;
435 err:
436 s3c24xx_serial_shutdown(port);
437 return ret;
440 /* power power management control */
442 static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
443 unsigned int old)
445 struct s3c24xx_uart_port *ourport = to_ourport(port);
447 ourport->pm_level = level;
449 switch (level) {
450 case 3:
451 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
452 clk_disable(ourport->baudclk);
454 clk_disable(ourport->clk);
455 break;
457 case 0:
458 clk_enable(ourport->clk);
460 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
461 clk_enable(ourport->baudclk);
463 break;
464 default:
465 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
469 /* baud rate calculation
471 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
472 * of different sources, including the peripheral clock ("pclk") and an
473 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
474 * with a programmable extra divisor.
476 * The following code goes through the clock sources, and calculates the
477 * baud clocks (and the resultant actual baud rates) and then tries to
478 * pick the closest one and select that.
483 #define MAX_CLKS (8)
485 static struct s3c24xx_uart_clksrc tmp_clksrc = {
486 .name = "pclk",
487 .min_baud = 0,
488 .max_baud = 0,
489 .divisor = 1,
492 static inline int
493 s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
495 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
497 return (info->get_clksrc)(port, c);
500 static inline int
501 s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
503 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
505 return (info->set_clksrc)(port, c);
508 struct baud_calc {
509 struct s3c24xx_uart_clksrc *clksrc;
510 unsigned int calc;
511 unsigned int divslot;
512 unsigned int quot;
513 struct clk *src;
516 static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
517 struct uart_port *port,
518 struct s3c24xx_uart_clksrc *clksrc,
519 unsigned int baud)
521 struct s3c24xx_uart_port *ourport = to_ourport(port);
522 unsigned long rate;
524 calc->src = clk_get(port->dev, clksrc->name);
525 if (calc->src == NULL || IS_ERR(calc->src))
526 return 0;
528 rate = clk_get_rate(calc->src);
529 rate /= clksrc->divisor;
531 calc->clksrc = clksrc;
533 if (ourport->info->has_divslot) {
534 unsigned long div = rate / baud;
536 /* The UDIVSLOT register on the newer UARTs allows us to
537 * get a divisor adjustment of 1/16th on the baud clock.
539 * We don't keep the UDIVSLOT value (the 16ths we calculated
540 * by not multiplying the baud by 16) as it is easy enough
541 * to recalculate.
544 calc->quot = div / 16;
545 calc->calc = rate / div;
546 } else {
547 calc->quot = (rate + (8 * baud)) / (16 * baud);
548 calc->calc = (rate / (calc->quot * 16));
551 calc->quot--;
552 return 1;
555 static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
556 struct s3c24xx_uart_clksrc **clksrc,
557 struct clk **clk,
558 unsigned int baud)
560 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
561 struct s3c24xx_uart_clksrc *clkp;
562 struct baud_calc res[MAX_CLKS];
563 struct baud_calc *resptr, *best, *sptr;
564 int i;
566 clkp = cfg->clocks;
567 best = NULL;
569 if (cfg->clocks_size < 2) {
570 if (cfg->clocks_size == 0)
571 clkp = &tmp_clksrc;
573 /* check to see if we're sourcing fclk, and if so we're
574 * going to have to update the clock source
577 if (strcmp(clkp->name, "fclk") == 0) {
578 struct s3c24xx_uart_clksrc src;
580 s3c24xx_serial_getsource(port, &src);
582 /* check that the port already using fclk, and if
583 * not, then re-select fclk
586 if (strcmp(src.name, clkp->name) == 0) {
587 s3c24xx_serial_setsource(port, clkp);
588 s3c24xx_serial_getsource(port, &src);
591 clkp->divisor = src.divisor;
594 s3c24xx_serial_calcbaud(res, port, clkp, baud);
595 best = res;
596 resptr = best + 1;
597 } else {
598 resptr = res;
600 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
601 if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
602 resptr++;
606 /* ok, we now need to select the best clock we found */
608 if (!best) {
609 unsigned int deviation = (1<<30)|((1<<30)-1);
610 int calc_deviation;
612 for (sptr = res; sptr < resptr; sptr++) {
613 calc_deviation = baud - sptr->calc;
614 if (calc_deviation < 0)
615 calc_deviation = -calc_deviation;
617 if (calc_deviation < deviation) {
618 best = sptr;
619 deviation = calc_deviation;
624 /* store results to pass back */
626 *clksrc = best->clksrc;
627 *clk = best->src;
629 return best->quot;
632 /* udivslot_table[]
634 * This table takes the fractional value of the baud divisor and gives
635 * the recommended setting for the UDIVSLOT register.
637 static u16 udivslot_table[16] = {
638 [0] = 0x0000,
639 [1] = 0x0080,
640 [2] = 0x0808,
641 [3] = 0x0888,
642 [4] = 0x2222,
643 [5] = 0x4924,
644 [6] = 0x4A52,
645 [7] = 0x54AA,
646 [8] = 0x5555,
647 [9] = 0xD555,
648 [10] = 0xD5D5,
649 [11] = 0xDDD5,
650 [12] = 0xDDDD,
651 [13] = 0xDFDD,
652 [14] = 0xDFDF,
653 [15] = 0xFFDF,
656 static void s3c24xx_serial_set_termios(struct uart_port *port,
657 struct ktermios *termios,
658 struct ktermios *old)
660 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
661 struct s3c24xx_uart_port *ourport = to_ourport(port);
662 struct s3c24xx_uart_clksrc *clksrc = NULL;
663 struct clk *clk = NULL;
664 unsigned long flags;
665 unsigned int baud, quot;
666 unsigned int ulcon;
667 unsigned int umcon;
668 unsigned int udivslot = 0;
671 * We don't support modem control lines.
673 termios->c_cflag &= ~(HUPCL | CMSPAR);
674 termios->c_cflag |= CLOCAL;
677 * Ask the core to calculate the divisor for us.
680 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
682 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
683 quot = port->custom_divisor;
684 else
685 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
687 /* check to see if we need to change clock source */
689 if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
690 dbg("selecting clock %p\n", clk);
691 s3c24xx_serial_setsource(port, clksrc);
693 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
694 clk_disable(ourport->baudclk);
695 ourport->baudclk = NULL;
698 clk_enable(clk);
700 ourport->clksrc = clksrc;
701 ourport->baudclk = clk;
702 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
705 if (ourport->info->has_divslot) {
706 unsigned int div = ourport->baudclk_rate / baud;
708 if (cfg->has_fracval) {
709 udivslot = (div & 15);
710 dbg("fracval = %04x\n", udivslot);
711 } else {
712 udivslot = udivslot_table[div & 15];
713 dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
717 switch (termios->c_cflag & CSIZE) {
718 case CS5:
719 dbg("config: 5bits/char\n");
720 ulcon = S3C2410_LCON_CS5;
721 break;
722 case CS6:
723 dbg("config: 6bits/char\n");
724 ulcon = S3C2410_LCON_CS6;
725 break;
726 case CS7:
727 dbg("config: 7bits/char\n");
728 ulcon = S3C2410_LCON_CS7;
729 break;
730 case CS8:
731 default:
732 dbg("config: 8bits/char\n");
733 ulcon = S3C2410_LCON_CS8;
734 break;
737 /* preserve original lcon IR settings */
738 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
740 if (termios->c_cflag & CSTOPB)
741 ulcon |= S3C2410_LCON_STOPB;
743 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
745 if (termios->c_cflag & PARENB) {
746 if (termios->c_cflag & PARODD)
747 ulcon |= S3C2410_LCON_PODD;
748 else
749 ulcon |= S3C2410_LCON_PEVEN;
750 } else {
751 ulcon |= S3C2410_LCON_PNONE;
754 spin_lock_irqsave(&port->lock, flags);
756 dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
757 ulcon, quot, udivslot);
759 wr_regl(port, S3C2410_ULCON, ulcon);
760 wr_regl(port, S3C2410_UBRDIV, quot);
761 wr_regl(port, S3C2410_UMCON, umcon);
763 if (ourport->info->has_divslot)
764 wr_regl(port, S3C2443_DIVSLOT, udivslot);
766 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
767 rd_regl(port, S3C2410_ULCON),
768 rd_regl(port, S3C2410_UCON),
769 rd_regl(port, S3C2410_UFCON));
772 * Update the per-port timeout.
774 uart_update_timeout(port, termios->c_cflag, baud);
777 * Which character status flags are we interested in?
779 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
780 if (termios->c_iflag & INPCK)
781 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
784 * Which character status flags should we ignore?
786 port->ignore_status_mask = 0;
787 if (termios->c_iflag & IGNPAR)
788 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
789 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
790 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
793 * Ignore all characters if CREAD is not set.
795 if ((termios->c_cflag & CREAD) == 0)
796 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
798 spin_unlock_irqrestore(&port->lock, flags);
801 static const char *s3c24xx_serial_type(struct uart_port *port)
803 switch (port->type) {
804 case PORT_S3C2410:
805 return "S3C2410";
806 case PORT_S3C2440:
807 return "S3C2440";
808 case PORT_S3C2412:
809 return "S3C2412";
810 case PORT_S3C6400:
811 return "S3C6400/10";
812 default:
813 return NULL;
817 #define MAP_SIZE (0x100)
819 static void s3c24xx_serial_release_port(struct uart_port *port)
821 release_mem_region(port->mapbase, MAP_SIZE);
824 static int s3c24xx_serial_request_port(struct uart_port *port)
826 const char *name = s3c24xx_serial_portname(port);
827 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
830 static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
832 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
834 if (flags & UART_CONFIG_TYPE &&
835 s3c24xx_serial_request_port(port) == 0)
836 port->type = info->type;
840 * verify the new serial_struct (for TIOCSSERIAL).
842 static int
843 s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
845 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
847 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
848 return -EINVAL;
850 return 0;
854 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
856 static struct console s3c24xx_serial_console;
858 #define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
859 #else
860 #define S3C24XX_SERIAL_CONSOLE NULL
861 #endif
863 static struct uart_ops s3c24xx_serial_ops = {
864 .pm = s3c24xx_serial_pm,
865 .tx_empty = s3c24xx_serial_tx_empty,
866 .get_mctrl = s3c24xx_serial_get_mctrl,
867 .set_mctrl = s3c24xx_serial_set_mctrl,
868 .stop_tx = s3c24xx_serial_stop_tx,
869 .start_tx = s3c24xx_serial_start_tx,
870 .stop_rx = s3c24xx_serial_stop_rx,
871 .enable_ms = s3c24xx_serial_enable_ms,
872 .break_ctl = s3c24xx_serial_break_ctl,
873 .startup = s3c24xx_serial_startup,
874 .shutdown = s3c24xx_serial_shutdown,
875 .set_termios = s3c24xx_serial_set_termios,
876 .type = s3c24xx_serial_type,
877 .release_port = s3c24xx_serial_release_port,
878 .request_port = s3c24xx_serial_request_port,
879 .config_port = s3c24xx_serial_config_port,
880 .verify_port = s3c24xx_serial_verify_port,
884 static struct uart_driver s3c24xx_uart_drv = {
885 .owner = THIS_MODULE,
886 .dev_name = "s3c2410_serial",
887 .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
888 .cons = S3C24XX_SERIAL_CONSOLE,
889 .driver_name = S3C24XX_SERIAL_NAME,
890 .major = S3C24XX_SERIAL_MAJOR,
891 .minor = S3C24XX_SERIAL_MINOR,
894 static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
895 [0] = {
896 .port = {
897 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
898 .iotype = UPIO_MEM,
899 .irq = IRQ_S3CUART_RX0,
900 .uartclk = 0,
901 .fifosize = 16,
902 .ops = &s3c24xx_serial_ops,
903 .flags = UPF_BOOT_AUTOCONF,
904 .line = 0,
907 [1] = {
908 .port = {
909 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
910 .iotype = UPIO_MEM,
911 .irq = IRQ_S3CUART_RX1,
912 .uartclk = 0,
913 .fifosize = 16,
914 .ops = &s3c24xx_serial_ops,
915 .flags = UPF_BOOT_AUTOCONF,
916 .line = 1,
919 #if CONFIG_SERIAL_SAMSUNG_UARTS > 2
921 [2] = {
922 .port = {
923 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
924 .iotype = UPIO_MEM,
925 .irq = IRQ_S3CUART_RX2,
926 .uartclk = 0,
927 .fifosize = 16,
928 .ops = &s3c24xx_serial_ops,
929 .flags = UPF_BOOT_AUTOCONF,
930 .line = 2,
933 #endif
934 #if CONFIG_SERIAL_SAMSUNG_UARTS > 3
935 [3] = {
936 .port = {
937 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
938 .iotype = UPIO_MEM,
939 .irq = IRQ_S3CUART_RX3,
940 .uartclk = 0,
941 .fifosize = 16,
942 .ops = &s3c24xx_serial_ops,
943 .flags = UPF_BOOT_AUTOCONF,
944 .line = 3,
947 #endif
950 /* s3c24xx_serial_resetport
952 * wrapper to call the specific reset for this port (reset the fifos
953 * and the settings)
956 static inline int s3c24xx_serial_resetport(struct uart_port *port,
957 struct s3c2410_uartcfg *cfg)
959 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
961 return (info->reset_port)(port, cfg);
965 #ifdef CONFIG_CPU_FREQ
967 static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
968 unsigned long val, void *data)
970 struct s3c24xx_uart_port *port;
971 struct uart_port *uport;
973 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
974 uport = &port->port;
976 /* check to see if port is enabled */
978 if (port->pm_level != 0)
979 return 0;
981 /* try and work out if the baudrate is changing, we can detect
982 * a change in rate, but we do not have support for detecting
983 * a disturbance in the clock-rate over the change.
986 if (IS_ERR(port->clk))
987 goto exit;
989 if (port->baudclk_rate == clk_get_rate(port->clk))
990 goto exit;
992 if (val == CPUFREQ_PRECHANGE) {
993 /* we should really shut the port down whilst the
994 * frequency change is in progress. */
996 } else if (val == CPUFREQ_POSTCHANGE) {
997 struct ktermios *termios;
998 struct tty_struct *tty;
1000 if (uport->state == NULL)
1001 goto exit;
1003 tty = uport->state->port.tty;
1005 if (tty == NULL)
1006 goto exit;
1008 termios = tty->termios;
1010 if (termios == NULL) {
1011 printk(KERN_WARNING "%s: no termios?\n", __func__);
1012 goto exit;
1015 s3c24xx_serial_set_termios(uport, termios, NULL);
1018 exit:
1019 return 0;
1022 static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1024 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1026 return cpufreq_register_notifier(&port->freq_transition,
1027 CPUFREQ_TRANSITION_NOTIFIER);
1030 static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1032 cpufreq_unregister_notifier(&port->freq_transition,
1033 CPUFREQ_TRANSITION_NOTIFIER);
1036 #else
1037 static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1039 return 0;
1042 static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1045 #endif
1047 /* s3c24xx_serial_init_port
1049 * initialise a single serial port from the platform device given
1052 static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1053 struct s3c24xx_uart_info *info,
1054 struct platform_device *platdev)
1056 struct uart_port *port = &ourport->port;
1057 struct s3c2410_uartcfg *cfg;
1058 struct resource *res;
1059 int ret;
1061 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1063 if (platdev == NULL)
1064 return -ENODEV;
1066 cfg = s3c24xx_dev_to_cfg(&platdev->dev);
1068 if (port->mapbase != 0)
1069 return 0;
1071 if (cfg->hwport > CONFIG_SERIAL_SAMSUNG_UARTS) {
1072 printk(KERN_ERR "%s: port %d bigger than %d\n", __func__,
1073 cfg->hwport, CONFIG_SERIAL_SAMSUNG_UARTS);
1074 return -ERANGE;
1077 /* setup info for port */
1078 port->dev = &platdev->dev;
1079 ourport->info = info;
1081 /* copy the info in from provided structure */
1082 ourport->port.fifosize = info->fifosize;
1084 dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
1086 port->uartclk = 1;
1088 if (cfg->uart_flags & UPF_CONS_FLOW) {
1089 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1090 port->flags |= UPF_CONS_FLOW;
1093 /* sort our the physical and virtual addresses for each UART */
1095 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1096 if (res == NULL) {
1097 printk(KERN_ERR "failed to find memory resource for uart\n");
1098 return -EINVAL;
1101 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1103 port->mapbase = res->start;
1104 port->membase = S3C_VA_UART + res->start - (S3C_PA_UART & 0xfff00000);
1105 ret = platform_get_irq(platdev, 0);
1106 if (ret < 0)
1107 port->irq = 0;
1108 else {
1109 port->irq = ret;
1110 ourport->rx_irq = ret;
1111 ourport->tx_irq = ret + 1;
1114 ret = platform_get_irq(platdev, 1);
1115 if (ret > 0)
1116 ourport->tx_irq = ret;
1118 ourport->clk = clk_get(&platdev->dev, "uart");
1120 dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
1121 port->mapbase, port->membase, port->irq,
1122 ourport->rx_irq, ourport->tx_irq, port->uartclk);
1124 /* reset the fifos (and setup the uart) */
1125 s3c24xx_serial_resetport(port, cfg);
1126 return 0;
1129 static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
1130 struct device_attribute *attr,
1131 char *buf)
1133 struct uart_port *port = s3c24xx_dev_to_port(dev);
1134 struct s3c24xx_uart_port *ourport = to_ourport(port);
1136 return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->clksrc->name);
1139 static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
1141 /* Device driver serial port probe */
1143 static int probe_index;
1145 int s3c24xx_serial_probe(struct platform_device *dev,
1146 struct s3c24xx_uart_info *info)
1148 struct s3c24xx_uart_port *ourport;
1149 int ret;
1151 dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
1153 ourport = &s3c24xx_serial_ports[probe_index];
1154 probe_index++;
1156 dbg("%s: initialising port %p...\n", __func__, ourport);
1158 ret = s3c24xx_serial_init_port(ourport, info, dev);
1159 if (ret < 0)
1160 goto probe_err;
1162 dbg("%s: adding port\n", __func__);
1163 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1164 platform_set_drvdata(dev, &ourport->port);
1166 ret = device_create_file(&dev->dev, &dev_attr_clock_source);
1167 if (ret < 0)
1168 printk(KERN_ERR "%s: failed to add clksrc attr.\n", __func__);
1170 ret = s3c24xx_serial_cpufreq_register(ourport);
1171 if (ret < 0)
1172 dev_err(&dev->dev, "failed to add cpufreq notifier\n");
1174 return 0;
1176 probe_err:
1177 return ret;
1180 EXPORT_SYMBOL_GPL(s3c24xx_serial_probe);
1182 int __devexit s3c24xx_serial_remove(struct platform_device *dev)
1184 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1186 if (port) {
1187 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
1188 device_remove_file(&dev->dev, &dev_attr_clock_source);
1189 uart_remove_one_port(&s3c24xx_uart_drv, port);
1192 return 0;
1195 EXPORT_SYMBOL_GPL(s3c24xx_serial_remove);
1197 /* UART power management code */
1199 #ifdef CONFIG_PM
1201 static int s3c24xx_serial_suspend(struct platform_device *dev, pm_message_t state)
1203 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1205 if (port)
1206 uart_suspend_port(&s3c24xx_uart_drv, port);
1208 return 0;
1211 static int s3c24xx_serial_resume(struct platform_device *dev)
1213 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1214 struct s3c24xx_uart_port *ourport = to_ourport(port);
1216 if (port) {
1217 clk_enable(ourport->clk);
1218 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1219 clk_disable(ourport->clk);
1221 uart_resume_port(&s3c24xx_uart_drv, port);
1224 return 0;
1226 #endif
1228 int s3c24xx_serial_init(struct platform_driver *drv,
1229 struct s3c24xx_uart_info *info)
1231 dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
1233 #ifdef CONFIG_PM
1234 drv->suspend = s3c24xx_serial_suspend;
1235 drv->resume = s3c24xx_serial_resume;
1236 #endif
1238 return platform_driver_register(drv);
1241 EXPORT_SYMBOL_GPL(s3c24xx_serial_init);
1243 /* module initialisation code */
1245 static int __init s3c24xx_serial_modinit(void)
1247 int ret;
1249 ret = uart_register_driver(&s3c24xx_uart_drv);
1250 if (ret < 0) {
1251 printk(KERN_ERR "failed to register UART driver\n");
1252 return -1;
1255 return 0;
1258 static void __exit s3c24xx_serial_modexit(void)
1260 uart_unregister_driver(&s3c24xx_uart_drv);
1263 module_init(s3c24xx_serial_modinit);
1264 module_exit(s3c24xx_serial_modexit);
1266 /* Console code */
1268 #ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1270 static struct uart_port *cons_uart;
1272 static int
1273 s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1275 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1276 unsigned long ufstat, utrstat;
1278 if (ufcon & S3C2410_UFCON_FIFOMODE) {
1279 /* fifo mode - check amount of data in fifo registers... */
1281 ufstat = rd_regl(port, S3C2410_UFSTAT);
1282 return (ufstat & info->tx_fifofull) ? 0 : 1;
1285 /* in non-fifo mode, we go and use the tx buffer empty */
1287 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1288 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1291 static void
1292 s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
1294 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1295 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1296 barrier();
1297 wr_regb(cons_uart, S3C2410_UTXH, ch);
1300 static void
1301 s3c24xx_serial_console_write(struct console *co, const char *s,
1302 unsigned int count)
1304 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
1307 static void __init
1308 s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1309 int *parity, int *bits)
1311 struct s3c24xx_uart_clksrc clksrc;
1312 struct clk *clk;
1313 unsigned int ulcon;
1314 unsigned int ucon;
1315 unsigned int ubrdiv;
1316 unsigned long rate;
1318 ulcon = rd_regl(port, S3C2410_ULCON);
1319 ucon = rd_regl(port, S3C2410_UCON);
1320 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1322 dbg("s3c24xx_serial_get_options: port=%p\n"
1323 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1324 port, ulcon, ucon, ubrdiv);
1326 if ((ucon & 0xf) != 0) {
1327 /* consider the serial port configured if the tx/rx mode set */
1329 switch (ulcon & S3C2410_LCON_CSMASK) {
1330 case S3C2410_LCON_CS5:
1331 *bits = 5;
1332 break;
1333 case S3C2410_LCON_CS6:
1334 *bits = 6;
1335 break;
1336 case S3C2410_LCON_CS7:
1337 *bits = 7;
1338 break;
1339 default:
1340 case S3C2410_LCON_CS8:
1341 *bits = 8;
1342 break;
1345 switch (ulcon & S3C2410_LCON_PMASK) {
1346 case S3C2410_LCON_PEVEN:
1347 *parity = 'e';
1348 break;
1350 case S3C2410_LCON_PODD:
1351 *parity = 'o';
1352 break;
1354 case S3C2410_LCON_PNONE:
1355 default:
1356 *parity = 'n';
1359 /* now calculate the baud rate */
1361 s3c24xx_serial_getsource(port, &clksrc);
1363 clk = clk_get(port->dev, clksrc.name);
1364 if (!IS_ERR(clk) && clk != NULL)
1365 rate = clk_get_rate(clk) / clksrc.divisor;
1366 else
1367 rate = 1;
1370 *baud = rate / (16 * (ubrdiv + 1));
1371 dbg("calculated baud %d\n", *baud);
1376 /* s3c24xx_serial_init_ports
1378 * initialise the serial ports from the machine provided initialisation
1379 * data.
1382 static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info **info)
1384 struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1385 struct platform_device **platdev_ptr;
1386 int i;
1388 dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1390 platdev_ptr = s3c24xx_uart_devs;
1392 for (i = 0; i < CONFIG_SERIAL_SAMSUNG_UARTS; i++, ptr++, platdev_ptr++) {
1393 s3c24xx_serial_init_port(ptr, info[i], *platdev_ptr);
1396 return 0;
1399 static int __init
1400 s3c24xx_serial_console_setup(struct console *co, char *options)
1402 struct uart_port *port;
1403 int baud = 9600;
1404 int bits = 8;
1405 int parity = 'n';
1406 int flow = 'n';
1408 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1409 co, co->index, options);
1411 /* is this a valid port */
1413 if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
1414 co->index = 0;
1416 port = &s3c24xx_serial_ports[co->index].port;
1418 /* is the port configured? */
1420 if (port->mapbase == 0x0) {
1421 co->index = 0;
1422 port = &s3c24xx_serial_ports[co->index].port;
1425 cons_uart = port;
1427 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1430 * Check whether an invalid uart number has been specified, and
1431 * if so, search for the first available port that does have
1432 * console support.
1434 if (options)
1435 uart_parse_options(options, &baud, &parity, &bits, &flow);
1436 else
1437 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1439 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1441 return uart_set_options(port, co, baud, parity, bits, flow);
1444 /* s3c24xx_serial_initconsole
1446 * initialise the console from one of the uart drivers
1449 static struct console s3c24xx_serial_console = {
1450 .name = S3C24XX_SERIAL_NAME,
1451 .device = uart_console_device,
1452 .flags = CON_PRINTBUFFER,
1453 .index = -1,
1454 .write = s3c24xx_serial_console_write,
1455 .setup = s3c24xx_serial_console_setup
1458 int s3c24xx_serial_initconsole(struct platform_driver *drv,
1459 struct s3c24xx_uart_info **info)
1462 struct platform_device *dev = s3c24xx_uart_devs[0];
1464 dbg("s3c24xx_serial_initconsole\n");
1466 /* select driver based on the cpu */
1468 if (dev == NULL) {
1469 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1470 return 0;
1473 if (strcmp(dev->name, drv->driver.name) != 0)
1474 return 0;
1476 s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1477 s3c24xx_serial_init_ports(info);
1479 register_console(&s3c24xx_serial_console);
1480 return 0;
1483 #endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
1485 MODULE_DESCRIPTION("Samsung SoC Serial port driver");
1486 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1487 MODULE_LICENSE("GPL v2");