blob: 44fc38afa22847c2a3de82af56fbb3e90468eba7 [file] [log] [blame]
Ben Dooksb4975492008-07-03 12:32:51 +01001/* linux/drivers/serial/samsuing.c
2 *
3 * Driver core for Samsung SoC onboard UARTs.
4 *
5 * Ben Dooks, Copyright (c) 2003-2005,2008 Simtec Electronics
6 * http://armlinux.simtec.co.uk/
7 *
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.
11*/
12
13/* Hote on 2410 error handling
14 *
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.
20 *
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
24 *
25 * BJD, 04-Nov-2004
26*/
27
28#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
29#define SUPPORT_SYSRQ
30#endif
31
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>
Ben Dooks30555472008-10-21 14:06:36 +010045#include <linux/cpufreq.h>
Ben Dooksb4975492008-07-03 12:32:51 +010046
47#include <asm/irq.h>
48
Russell Kinga09e64f2008-08-05 16:14:15 +010049#include <mach/hardware.h>
Ben Dooksb690ace2008-10-21 14:07:03 +010050#include <mach/map.h>
Ben Dooksb4975492008-07-03 12:32:51 +010051
Ben Dooksa2b7ba92008-10-07 22:26:09 +010052#include <plat/regs-serial.h>
Ben Dooksb4975492008-07-03 12:32:51 +010053
54#include "samsung.h"
55
56/* UART name and device definitions */
57
58#define S3C24XX_SERIAL_NAME "ttySAC"
59#define S3C24XX_SERIAL_MAJOR 204
60#define S3C24XX_SERIAL_MINOR 64
61
62/* we can support 3 uarts, but not always use them */
63
Sandeep Patil1d4bab02008-10-21 14:06:30 +010064#if defined(CONFIG_CPU_S3C2400) || defined(CONFIG_CPU_S3C24A0)
Ben Dooksb4975492008-07-03 12:32:51 +010065#define NR_PORTS (2)
66#else
67#define NR_PORTS (3)
68#endif
69
70/* port irq numbers */
71
72#define TX_IRQ(port) ((port)->irq + 1)
73#define RX_IRQ(port) ((port)->irq)
74
75/* macros to change one thing to another */
76
77#define tx_enabled(port) ((port)->unused[0])
78#define rx_enabled(port) ((port)->unused[1])
79
80/* flag to ignore all characters comming in */
81#define RXSTAT_DUMMY_READ (0x10000000)
82
83static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
84{
85 return container_of(port, struct s3c24xx_uart_port, port);
86}
87
88/* translate a port to the device name */
89
90static inline const char *s3c24xx_serial_portname(struct uart_port *port)
91{
92 return to_platform_device(port->dev)->name;
93}
94
95static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
96{
97 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
98}
99
100static void s3c24xx_serial_rx_enable(struct uart_port *port)
101{
102 unsigned long flags;
103 unsigned int ucon, ufcon;
104 int count = 10000;
105
106 spin_lock_irqsave(&port->lock, flags);
107
108 while (--count && !s3c24xx_serial_txempty_nofifo(port))
109 udelay(100);
110
111 ufcon = rd_regl(port, S3C2410_UFCON);
112 ufcon |= S3C2410_UFCON_RESETRX;
113 wr_regl(port, S3C2410_UFCON, ufcon);
114
115 ucon = rd_regl(port, S3C2410_UCON);
116 ucon |= S3C2410_UCON_RXIRQMODE;
117 wr_regl(port, S3C2410_UCON, ucon);
118
119 rx_enabled(port) = 1;
120 spin_unlock_irqrestore(&port->lock, flags);
121}
122
123static void s3c24xx_serial_rx_disable(struct uart_port *port)
124{
125 unsigned long flags;
126 unsigned int ucon;
127
128 spin_lock_irqsave(&port->lock, flags);
129
130 ucon = rd_regl(port, S3C2410_UCON);
131 ucon &= ~S3C2410_UCON_RXIRQMODE;
132 wr_regl(port, S3C2410_UCON, ucon);
133
134 rx_enabled(port) = 0;
135 spin_unlock_irqrestore(&port->lock, flags);
136}
137
138static void s3c24xx_serial_stop_tx(struct uart_port *port)
139{
140 if (tx_enabled(port)) {
141 disable_irq(TX_IRQ(port));
142 tx_enabled(port) = 0;
143 if (port->flags & UPF_CONS_FLOW)
144 s3c24xx_serial_rx_enable(port);
145 }
146}
147
148static void s3c24xx_serial_start_tx(struct uart_port *port)
149{
150 if (!tx_enabled(port)) {
151 if (port->flags & UPF_CONS_FLOW)
152 s3c24xx_serial_rx_disable(port);
153
154 enable_irq(TX_IRQ(port));
155 tx_enabled(port) = 1;
156 }
157}
158
159
160static void s3c24xx_serial_stop_rx(struct uart_port *port)
161{
162 if (rx_enabled(port)) {
163 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
164 disable_irq(RX_IRQ(port));
165 rx_enabled(port) = 0;
166 }
167}
168
169static void s3c24xx_serial_enable_ms(struct uart_port *port)
170{
171}
172
173static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
174{
175 return to_ourport(port)->info;
176}
177
178static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
179{
180 if (port->dev == NULL)
181 return NULL;
182
183 return (struct s3c2410_uartcfg *)port->dev->platform_data;
184}
185
186static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
187 unsigned long ufstat)
188{
189 struct s3c24xx_uart_info *info = ourport->info;
190
191 if (ufstat & info->rx_fifofull)
192 return info->fifosize;
193
194 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
195}
196
197
198/* ? - where has parity gone?? */
199#define S3C2410_UERSTAT_PARITY (0x1000)
200
201static irqreturn_t
202s3c24xx_serial_rx_chars(int irq, void *dev_id)
203{
204 struct s3c24xx_uart_port *ourport = dev_id;
205 struct uart_port *port = &ourport->port;
Alan Coxf10140f2008-07-22 15:25:07 +0100206 struct tty_struct *tty = port->info->port.tty;
Ben Dooksb4975492008-07-03 12:32:51 +0100207 unsigned int ufcon, ch, flag, ufstat, uerstat;
208 int max_count = 64;
209
210 while (max_count-- > 0) {
211 ufcon = rd_regl(port, S3C2410_UFCON);
212 ufstat = rd_regl(port, S3C2410_UFSTAT);
213
214 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
215 break;
216
217 uerstat = rd_regl(port, S3C2410_UERSTAT);
218 ch = rd_regb(port, S3C2410_URXH);
219
220 if (port->flags & UPF_CONS_FLOW) {
221 int txe = s3c24xx_serial_txempty_nofifo(port);
222
223 if (rx_enabled(port)) {
224 if (!txe) {
225 rx_enabled(port) = 0;
226 continue;
227 }
228 } else {
229 if (txe) {
230 ufcon |= S3C2410_UFCON_RESETRX;
231 wr_regl(port, S3C2410_UFCON, ufcon);
232 rx_enabled(port) = 1;
233 goto out;
234 }
235 continue;
236 }
237 }
238
239 /* insert the character into the buffer */
240
241 flag = TTY_NORMAL;
242 port->icount.rx++;
243
244 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
245 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
246 ch, uerstat);
247
248 /* check for break */
249 if (uerstat & S3C2410_UERSTAT_BREAK) {
250 dbg("break!\n");
251 port->icount.brk++;
252 if (uart_handle_break(port))
253 goto ignore_char;
254 }
255
256 if (uerstat & S3C2410_UERSTAT_FRAME)
257 port->icount.frame++;
258 if (uerstat & S3C2410_UERSTAT_OVERRUN)
259 port->icount.overrun++;
260
261 uerstat &= port->read_status_mask;
262
263 if (uerstat & S3C2410_UERSTAT_BREAK)
264 flag = TTY_BREAK;
265 else if (uerstat & S3C2410_UERSTAT_PARITY)
266 flag = TTY_PARITY;
267 else if (uerstat & (S3C2410_UERSTAT_FRAME |
268 S3C2410_UERSTAT_OVERRUN))
269 flag = TTY_FRAME;
270 }
271
272 if (uart_handle_sysrq_char(port, ch))
273 goto ignore_char;
274
275 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
276 ch, flag);
277
278 ignore_char:
279 continue;
280 }
281 tty_flip_buffer_push(tty);
282
283 out:
284 return IRQ_HANDLED;
285}
286
287static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
288{
289 struct s3c24xx_uart_port *ourport = id;
290 struct uart_port *port = &ourport->port;
291 struct circ_buf *xmit = &port->info->xmit;
292 int count = 256;
293
294 if (port->x_char) {
295 wr_regb(port, S3C2410_UTXH, port->x_char);
296 port->icount.tx++;
297 port->x_char = 0;
298 goto out;
299 }
300
301 /* if there isnt anything more to transmit, or the uart is now
302 * stopped, disable the uart and exit
303 */
304
305 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
306 s3c24xx_serial_stop_tx(port);
307 goto out;
308 }
309
310 /* try and drain the buffer... */
311
312 while (!uart_circ_empty(xmit) && count-- > 0) {
313 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
314 break;
315
316 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
317 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
318 port->icount.tx++;
319 }
320
321 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
322 uart_write_wakeup(port);
323
324 if (uart_circ_empty(xmit))
325 s3c24xx_serial_stop_tx(port);
326
327 out:
328 return IRQ_HANDLED;
329}
330
331static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
332{
333 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
334 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
335 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
336
337 if (ufcon & S3C2410_UFCON_FIFOMODE) {
338 if ((ufstat & info->tx_fifomask) != 0 ||
339 (ufstat & info->tx_fifofull))
340 return 0;
341
342 return 1;
343 }
344
345 return s3c24xx_serial_txempty_nofifo(port);
346}
347
348/* no modem control lines */
349static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
350{
351 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
352
353 if (umstat & S3C2410_UMSTAT_CTS)
354 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
355 else
356 return TIOCM_CAR | TIOCM_DSR;
357}
358
359static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
360{
361 /* todo - possibly remove AFC and do manual CTS */
362}
363
364static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
365{
366 unsigned long flags;
367 unsigned int ucon;
368
369 spin_lock_irqsave(&port->lock, flags);
370
371 ucon = rd_regl(port, S3C2410_UCON);
372
373 if (break_state)
374 ucon |= S3C2410_UCON_SBREAK;
375 else
376 ucon &= ~S3C2410_UCON_SBREAK;
377
378 wr_regl(port, S3C2410_UCON, ucon);
379
380 spin_unlock_irqrestore(&port->lock, flags);
381}
382
383static void s3c24xx_serial_shutdown(struct uart_port *port)
384{
385 struct s3c24xx_uart_port *ourport = to_ourport(port);
386
387 if (ourport->tx_claimed) {
388 free_irq(TX_IRQ(port), ourport);
389 tx_enabled(port) = 0;
390 ourport->tx_claimed = 0;
391 }
392
393 if (ourport->rx_claimed) {
394 free_irq(RX_IRQ(port), ourport);
395 ourport->rx_claimed = 0;
396 rx_enabled(port) = 0;
397 }
398}
399
400
401static int s3c24xx_serial_startup(struct uart_port *port)
402{
403 struct s3c24xx_uart_port *ourport = to_ourport(port);
404 int ret;
405
406 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
407 port->mapbase, port->membase);
408
409 rx_enabled(port) = 1;
410
411 ret = request_irq(RX_IRQ(port),
412 s3c24xx_serial_rx_chars, 0,
413 s3c24xx_serial_portname(port), ourport);
414
415 if (ret != 0) {
416 printk(KERN_ERR "cannot get irq %d\n", RX_IRQ(port));
417 return ret;
418 }
419
420 ourport->rx_claimed = 1;
421
422 dbg("requesting tx irq...\n");
423
424 tx_enabled(port) = 1;
425
426 ret = request_irq(TX_IRQ(port),
427 s3c24xx_serial_tx_chars, 0,
428 s3c24xx_serial_portname(port), ourport);
429
430 if (ret) {
431 printk(KERN_ERR "cannot get irq %d\n", TX_IRQ(port));
432 goto err;
433 }
434
435 ourport->tx_claimed = 1;
436
437 dbg("s3c24xx_serial_startup ok\n");
438
439 /* the port reset code should have done the correct
440 * register setup for the port controls */
441
442 return ret;
443
444 err:
445 s3c24xx_serial_shutdown(port);
446 return ret;
447}
448
449/* power power management control */
450
451static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
452 unsigned int old)
453{
454 struct s3c24xx_uart_port *ourport = to_ourport(port);
455
Ben Dooks30555472008-10-21 14:06:36 +0100456 ourport->pm_level = level;
457
Ben Dooksb4975492008-07-03 12:32:51 +0100458 switch (level) {
459 case 3:
460 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
461 clk_disable(ourport->baudclk);
462
463 clk_disable(ourport->clk);
464 break;
465
466 case 0:
467 clk_enable(ourport->clk);
468
469 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
470 clk_enable(ourport->baudclk);
471
472 break;
473 default:
474 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
475 }
476}
477
478/* baud rate calculation
479 *
480 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
481 * of different sources, including the peripheral clock ("pclk") and an
482 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
483 * with a programmable extra divisor.
484 *
485 * The following code goes through the clock sources, and calculates the
486 * baud clocks (and the resultant actual baud rates) and then tries to
487 * pick the closest one and select that.
488 *
489*/
490
491
492#define MAX_CLKS (8)
493
494static struct s3c24xx_uart_clksrc tmp_clksrc = {
495 .name = "pclk",
496 .min_baud = 0,
497 .max_baud = 0,
498 .divisor = 1,
499};
500
501static inline int
502s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
503{
504 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
505
506 return (info->get_clksrc)(port, c);
507}
508
509static inline int
510s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
511{
512 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
513
514 return (info->set_clksrc)(port, c);
515}
516
517struct baud_calc {
518 struct s3c24xx_uart_clksrc *clksrc;
519 unsigned int calc;
520 unsigned int quot;
521 struct clk *src;
522};
523
524static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
525 struct uart_port *port,
526 struct s3c24xx_uart_clksrc *clksrc,
527 unsigned int baud)
528{
529 unsigned long rate;
530
531 calc->src = clk_get(port->dev, clksrc->name);
532 if (calc->src == NULL || IS_ERR(calc->src))
533 return 0;
534
535 rate = clk_get_rate(calc->src);
536 rate /= clksrc->divisor;
537
538 calc->clksrc = clksrc;
539 calc->quot = (rate + (8 * baud)) / (16 * baud);
540 calc->calc = (rate / (calc->quot * 16));
541
542 calc->quot--;
543 return 1;
544}
545
546static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
547 struct s3c24xx_uart_clksrc **clksrc,
548 struct clk **clk,
549 unsigned int baud)
550{
551 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
552 struct s3c24xx_uart_clksrc *clkp;
553 struct baud_calc res[MAX_CLKS];
554 struct baud_calc *resptr, *best, *sptr;
555 int i;
556
557 clkp = cfg->clocks;
558 best = NULL;
559
560 if (cfg->clocks_size < 2) {
561 if (cfg->clocks_size == 0)
562 clkp = &tmp_clksrc;
563
564 /* check to see if we're sourcing fclk, and if so we're
565 * going to have to update the clock source
566 */
567
568 if (strcmp(clkp->name, "fclk") == 0) {
569 struct s3c24xx_uart_clksrc src;
570
571 s3c24xx_serial_getsource(port, &src);
572
573 /* check that the port already using fclk, and if
574 * not, then re-select fclk
575 */
576
577 if (strcmp(src.name, clkp->name) == 0) {
578 s3c24xx_serial_setsource(port, clkp);
579 s3c24xx_serial_getsource(port, &src);
580 }
581
582 clkp->divisor = src.divisor;
583 }
584
585 s3c24xx_serial_calcbaud(res, port, clkp, baud);
586 best = res;
587 resptr = best + 1;
588 } else {
589 resptr = res;
590
591 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
592 if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
593 resptr++;
594 }
595 }
596
597 /* ok, we now need to select the best clock we found */
598
599 if (!best) {
600 unsigned int deviation = (1<<30)|((1<<30)-1);
601 int calc_deviation;
602
603 for (sptr = res; sptr < resptr; sptr++) {
604 calc_deviation = baud - sptr->calc;
605 if (calc_deviation < 0)
606 calc_deviation = -calc_deviation;
607
608 if (calc_deviation < deviation) {
609 best = sptr;
610 deviation = calc_deviation;
611 }
612 }
613 }
614
615 /* store results to pass back */
616
617 *clksrc = best->clksrc;
618 *clk = best->src;
619
620 return best->quot;
621}
622
623static void s3c24xx_serial_set_termios(struct uart_port *port,
624 struct ktermios *termios,
625 struct ktermios *old)
626{
627 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
628 struct s3c24xx_uart_port *ourport = to_ourport(port);
629 struct s3c24xx_uart_clksrc *clksrc = NULL;
630 struct clk *clk = NULL;
631 unsigned long flags;
632 unsigned int baud, quot;
633 unsigned int ulcon;
634 unsigned int umcon;
635
636 /*
637 * We don't support modem control lines.
638 */
639 termios->c_cflag &= ~(HUPCL | CMSPAR);
640 termios->c_cflag |= CLOCAL;
641
642 /*
643 * Ask the core to calculate the divisor for us.
644 */
645
646 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
647
648 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
649 quot = port->custom_divisor;
650 else
651 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
652
653 /* check to see if we need to change clock source */
654
655 if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
656 s3c24xx_serial_setsource(port, clksrc);
657
658 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
659 clk_disable(ourport->baudclk);
660 ourport->baudclk = NULL;
661 }
662
663 clk_enable(clk);
664
665 ourport->clksrc = clksrc;
666 ourport->baudclk = clk;
Ben Dooks30555472008-10-21 14:06:36 +0100667 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100668 }
669
670 switch (termios->c_cflag & CSIZE) {
671 case CS5:
672 dbg("config: 5bits/char\n");
673 ulcon = S3C2410_LCON_CS5;
674 break;
675 case CS6:
676 dbg("config: 6bits/char\n");
677 ulcon = S3C2410_LCON_CS6;
678 break;
679 case CS7:
680 dbg("config: 7bits/char\n");
681 ulcon = S3C2410_LCON_CS7;
682 break;
683 case CS8:
684 default:
685 dbg("config: 8bits/char\n");
686 ulcon = S3C2410_LCON_CS8;
687 break;
688 }
689
690 /* preserve original lcon IR settings */
691 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
692
693 if (termios->c_cflag & CSTOPB)
694 ulcon |= S3C2410_LCON_STOPB;
695
696 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
697
698 if (termios->c_cflag & PARENB) {
699 if (termios->c_cflag & PARODD)
700 ulcon |= S3C2410_LCON_PODD;
701 else
702 ulcon |= S3C2410_LCON_PEVEN;
703 } else {
704 ulcon |= S3C2410_LCON_PNONE;
705 }
706
707 spin_lock_irqsave(&port->lock, flags);
708
709 dbg("setting ulcon to %08x, brddiv to %d\n", ulcon, quot);
710
711 wr_regl(port, S3C2410_ULCON, ulcon);
712 wr_regl(port, S3C2410_UBRDIV, quot);
713 wr_regl(port, S3C2410_UMCON, umcon);
714
715 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
716 rd_regl(port, S3C2410_ULCON),
717 rd_regl(port, S3C2410_UCON),
718 rd_regl(port, S3C2410_UFCON));
719
720 /*
721 * Update the per-port timeout.
722 */
723 uart_update_timeout(port, termios->c_cflag, baud);
724
725 /*
726 * Which character status flags are we interested in?
727 */
728 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
729 if (termios->c_iflag & INPCK)
730 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
731
732 /*
733 * Which character status flags should we ignore?
734 */
735 port->ignore_status_mask = 0;
736 if (termios->c_iflag & IGNPAR)
737 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
738 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
739 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
740
741 /*
742 * Ignore all characters if CREAD is not set.
743 */
744 if ((termios->c_cflag & CREAD) == 0)
745 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
746
747 spin_unlock_irqrestore(&port->lock, flags);
748}
749
750static const char *s3c24xx_serial_type(struct uart_port *port)
751{
752 switch (port->type) {
753 case PORT_S3C2410:
754 return "S3C2410";
755 case PORT_S3C2440:
756 return "S3C2440";
757 case PORT_S3C2412:
758 return "S3C2412";
Ben Dooksb690ace2008-10-21 14:07:03 +0100759 case PORT_S3C6400:
760 return "S3C6400/10";
Ben Dooksb4975492008-07-03 12:32:51 +0100761 default:
762 return NULL;
763 }
764}
765
766#define MAP_SIZE (0x100)
767
768static void s3c24xx_serial_release_port(struct uart_port *port)
769{
770 release_mem_region(port->mapbase, MAP_SIZE);
771}
772
773static int s3c24xx_serial_request_port(struct uart_port *port)
774{
775 const char *name = s3c24xx_serial_portname(port);
776 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
777}
778
779static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
780{
781 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
782
783 if (flags & UART_CONFIG_TYPE &&
784 s3c24xx_serial_request_port(port) == 0)
785 port->type = info->type;
786}
787
788/*
789 * verify the new serial_struct (for TIOCSSERIAL).
790 */
791static int
792s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
793{
794 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
795
796 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
797 return -EINVAL;
798
799 return 0;
800}
801
802
803#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
804
805static struct console s3c24xx_serial_console;
806
807#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
808#else
809#define S3C24XX_SERIAL_CONSOLE NULL
810#endif
811
812static struct uart_ops s3c24xx_serial_ops = {
813 .pm = s3c24xx_serial_pm,
814 .tx_empty = s3c24xx_serial_tx_empty,
815 .get_mctrl = s3c24xx_serial_get_mctrl,
816 .set_mctrl = s3c24xx_serial_set_mctrl,
817 .stop_tx = s3c24xx_serial_stop_tx,
818 .start_tx = s3c24xx_serial_start_tx,
819 .stop_rx = s3c24xx_serial_stop_rx,
820 .enable_ms = s3c24xx_serial_enable_ms,
821 .break_ctl = s3c24xx_serial_break_ctl,
822 .startup = s3c24xx_serial_startup,
823 .shutdown = s3c24xx_serial_shutdown,
824 .set_termios = s3c24xx_serial_set_termios,
825 .type = s3c24xx_serial_type,
826 .release_port = s3c24xx_serial_release_port,
827 .request_port = s3c24xx_serial_request_port,
828 .config_port = s3c24xx_serial_config_port,
829 .verify_port = s3c24xx_serial_verify_port,
830};
831
832
833static struct uart_driver s3c24xx_uart_drv = {
834 .owner = THIS_MODULE,
835 .dev_name = "s3c2410_serial",
836 .nr = 3,
837 .cons = S3C24XX_SERIAL_CONSOLE,
838 .driver_name = S3C24XX_SERIAL_NAME,
839 .major = S3C24XX_SERIAL_MAJOR,
840 .minor = S3C24XX_SERIAL_MINOR,
841};
842
843static struct s3c24xx_uart_port s3c24xx_serial_ports[NR_PORTS] = {
844 [0] = {
845 .port = {
846 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
847 .iotype = UPIO_MEM,
848 .irq = IRQ_S3CUART_RX0,
849 .uartclk = 0,
850 .fifosize = 16,
851 .ops = &s3c24xx_serial_ops,
852 .flags = UPF_BOOT_AUTOCONF,
853 .line = 0,
854 }
855 },
856 [1] = {
857 .port = {
858 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
859 .iotype = UPIO_MEM,
860 .irq = IRQ_S3CUART_RX1,
861 .uartclk = 0,
862 .fifosize = 16,
863 .ops = &s3c24xx_serial_ops,
864 .flags = UPF_BOOT_AUTOCONF,
865 .line = 1,
866 }
867 },
868#if NR_PORTS > 2
869
870 [2] = {
871 .port = {
872 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
873 .iotype = UPIO_MEM,
874 .irq = IRQ_S3CUART_RX2,
875 .uartclk = 0,
876 .fifosize = 16,
877 .ops = &s3c24xx_serial_ops,
878 .flags = UPF_BOOT_AUTOCONF,
879 .line = 2,
880 }
881 }
882#endif
883};
884
885/* s3c24xx_serial_resetport
886 *
887 * wrapper to call the specific reset for this port (reset the fifos
888 * and the settings)
889*/
890
891static inline int s3c24xx_serial_resetport(struct uart_port *port,
892 struct s3c2410_uartcfg *cfg)
893{
894 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
895
896 return (info->reset_port)(port, cfg);
897}
898
Ben Dooks30555472008-10-21 14:06:36 +0100899
900#ifdef CONFIG_CPU_FREQ
901
902static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
903 unsigned long val, void *data)
904{
905 struct s3c24xx_uart_port *port;
906 struct uart_port *uport;
907
908 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
909 uport = &port->port;
910
911 /* check to see if port is enabled */
912
913 if (port->pm_level != 0)
914 return 0;
915
916 /* try and work out if the baudrate is changing, we can detect
917 * a change in rate, but we do not have support for detecting
918 * a disturbance in the clock-rate over the change.
919 */
920
921 if (IS_ERR(port->clk))
922 goto exit;
923
924 if (port->baudclk_rate == clk_get_rate(port->clk))
925 goto exit;
926
927 if (val == CPUFREQ_PRECHANGE) {
928 /* we should really shut the port down whilst the
929 * frequency change is in progress. */
930
931 } else if (val == CPUFREQ_POSTCHANGE) {
932 struct ktermios *termios;
933 struct tty_struct *tty;
934
935 if (uport->info == NULL) {
936 printk(KERN_WARNING "%s: info NULL\n", __func__);
937 goto exit;
938 }
939
940 tty = uport->info->port.tty;
941
942 if (tty == NULL) {
943 printk(KERN_WARNING "%s: tty is NULL\n", __func__);
944 goto exit;
945 }
946
947 termios = tty->termios;
948
949 if (termios == NULL) {
950 printk(KERN_WARNING "%s: no termios?\n", __func__);
951 goto exit;
952 }
953
954 s3c24xx_serial_set_termios(uport, termios, NULL);
955 }
956
957 exit:
958 return 0;
959}
960
961static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
962{
963 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
964
965 return cpufreq_register_notifier(&port->freq_transition,
966 CPUFREQ_TRANSITION_NOTIFIER);
967}
968
969static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
970{
971 cpufreq_unregister_notifier(&port->freq_transition,
972 CPUFREQ_TRANSITION_NOTIFIER);
973}
974
975#else
976static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
977{
978 return 0;
979}
980
981static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
982{
983}
984#endif
985
Ben Dooksb4975492008-07-03 12:32:51 +0100986/* s3c24xx_serial_init_port
987 *
988 * initialise a single serial port from the platform device given
989 */
990
991static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
992 struct s3c24xx_uart_info *info,
993 struct platform_device *platdev)
994{
995 struct uart_port *port = &ourport->port;
996 struct s3c2410_uartcfg *cfg;
997 struct resource *res;
998 int ret;
999
1000 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1001
1002 if (platdev == NULL)
1003 return -ENODEV;
1004
1005 cfg = s3c24xx_dev_to_cfg(&platdev->dev);
1006
1007 if (port->mapbase != 0)
1008 return 0;
1009
1010 if (cfg->hwport > 3)
1011 return -EINVAL;
1012
1013 /* setup info for port */
1014 port->dev = &platdev->dev;
1015 ourport->info = info;
1016
1017 /* copy the info in from provided structure */
1018 ourport->port.fifosize = info->fifosize;
1019
1020 dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
1021
1022 port->uartclk = 1;
1023
1024 if (cfg->uart_flags & UPF_CONS_FLOW) {
1025 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1026 port->flags |= UPF_CONS_FLOW;
1027 }
1028
1029 /* sort our the physical and virtual addresses for each UART */
1030
1031 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1032 if (res == NULL) {
1033 printk(KERN_ERR "failed to find memory resource for uart\n");
1034 return -EINVAL;
1035 }
1036
1037 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1038
Ben Dooksb690ace2008-10-21 14:07:03 +01001039 port->mapbase = res->start;
1040 port->membase = S3C_VA_UART + res->start - (S3C_PA_UART & 0xfff00000);
Ben Dooksb4975492008-07-03 12:32:51 +01001041 ret = platform_get_irq(platdev, 0);
1042 if (ret < 0)
1043 port->irq = 0;
1044 else
1045 port->irq = ret;
1046
1047 ourport->clk = clk_get(&platdev->dev, "uart");
1048
1049 dbg("port: map=%08x, mem=%08x, irq=%d, clock=%ld\n",
1050 port->mapbase, port->membase, port->irq, port->uartclk);
1051
1052 /* reset the fifos (and setup the uart) */
1053 s3c24xx_serial_resetport(port, cfg);
1054 return 0;
1055}
1056
1057static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
1058 struct device_attribute *attr,
1059 char *buf)
1060{
1061 struct uart_port *port = s3c24xx_dev_to_port(dev);
1062 struct s3c24xx_uart_port *ourport = to_ourport(port);
1063
1064 return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->clksrc->name);
1065}
1066
1067static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
1068
1069/* Device driver serial port probe */
1070
1071static int probe_index;
1072
1073int s3c24xx_serial_probe(struct platform_device *dev,
1074 struct s3c24xx_uart_info *info)
1075{
1076 struct s3c24xx_uart_port *ourport;
1077 int ret;
1078
1079 dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
1080
1081 ourport = &s3c24xx_serial_ports[probe_index];
1082 probe_index++;
1083
1084 dbg("%s: initialising port %p...\n", __func__, ourport);
1085
1086 ret = s3c24xx_serial_init_port(ourport, info, dev);
1087 if (ret < 0)
1088 goto probe_err;
1089
1090 dbg("%s: adding port\n", __func__);
1091 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1092 platform_set_drvdata(dev, &ourport->port);
1093
1094 ret = device_create_file(&dev->dev, &dev_attr_clock_source);
1095 if (ret < 0)
1096 printk(KERN_ERR "%s: failed to add clksrc attr.\n", __func__);
1097
Ben Dooks30555472008-10-21 14:06:36 +01001098 ret = s3c24xx_serial_cpufreq_register(ourport);
1099 if (ret < 0)
1100 dev_err(&dev->dev, "failed to add cpufreq notifier\n");
1101
Ben Dooksb4975492008-07-03 12:32:51 +01001102 return 0;
1103
1104 probe_err:
1105 return ret;
1106}
1107
1108EXPORT_SYMBOL_GPL(s3c24xx_serial_probe);
1109
1110int s3c24xx_serial_remove(struct platform_device *dev)
1111{
1112 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1113
1114 if (port) {
Ben Dooks30555472008-10-21 14:06:36 +01001115 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
Ben Dooksb4975492008-07-03 12:32:51 +01001116 device_remove_file(&dev->dev, &dev_attr_clock_source);
1117 uart_remove_one_port(&s3c24xx_uart_drv, port);
1118 }
1119
1120 return 0;
1121}
1122
1123EXPORT_SYMBOL_GPL(s3c24xx_serial_remove);
1124
1125/* UART power management code */
1126
1127#ifdef CONFIG_PM
1128
1129static int s3c24xx_serial_suspend(struct platform_device *dev, pm_message_t state)
1130{
1131 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1132
1133 if (port)
1134 uart_suspend_port(&s3c24xx_uart_drv, port);
1135
1136 return 0;
1137}
1138
1139static int s3c24xx_serial_resume(struct platform_device *dev)
1140{
1141 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1142 struct s3c24xx_uart_port *ourport = to_ourport(port);
1143
1144 if (port) {
1145 clk_enable(ourport->clk);
1146 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1147 clk_disable(ourport->clk);
1148
1149 uart_resume_port(&s3c24xx_uart_drv, port);
1150 }
1151
1152 return 0;
1153}
1154#endif
1155
1156int s3c24xx_serial_init(struct platform_driver *drv,
1157 struct s3c24xx_uart_info *info)
1158{
1159 dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
1160
1161#ifdef CONFIG_PM
1162 drv->suspend = s3c24xx_serial_suspend;
1163 drv->resume = s3c24xx_serial_resume;
1164#endif
1165
1166 return platform_driver_register(drv);
1167}
1168
1169EXPORT_SYMBOL_GPL(s3c24xx_serial_init);
1170
1171/* module initialisation code */
1172
1173static int __init s3c24xx_serial_modinit(void)
1174{
1175 int ret;
1176
1177 ret = uart_register_driver(&s3c24xx_uart_drv);
1178 if (ret < 0) {
1179 printk(KERN_ERR "failed to register UART driver\n");
1180 return -1;
1181 }
1182
1183 return 0;
1184}
1185
1186static void __exit s3c24xx_serial_modexit(void)
1187{
1188 uart_unregister_driver(&s3c24xx_uart_drv);
1189}
1190
1191module_init(s3c24xx_serial_modinit);
1192module_exit(s3c24xx_serial_modexit);
1193
1194/* Console code */
1195
1196#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1197
1198static struct uart_port *cons_uart;
1199
1200static int
1201s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1202{
1203 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1204 unsigned long ufstat, utrstat;
1205
1206 if (ufcon & S3C2410_UFCON_FIFOMODE) {
1207 /* fifo mode - check ammount of data in fifo registers... */
1208
1209 ufstat = rd_regl(port, S3C2410_UFSTAT);
1210 return (ufstat & info->tx_fifofull) ? 0 : 1;
1211 }
1212
1213 /* in non-fifo mode, we go and use the tx buffer empty */
1214
1215 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1216 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1217}
1218
1219static void
1220s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
1221{
1222 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1223 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1224 barrier();
1225 wr_regb(cons_uart, S3C2410_UTXH, ch);
1226}
1227
1228static void
1229s3c24xx_serial_console_write(struct console *co, const char *s,
1230 unsigned int count)
1231{
1232 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
1233}
1234
1235static void __init
1236s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1237 int *parity, int *bits)
1238{
1239 struct s3c24xx_uart_clksrc clksrc;
1240 struct clk *clk;
1241 unsigned int ulcon;
1242 unsigned int ucon;
1243 unsigned int ubrdiv;
1244 unsigned long rate;
1245
1246 ulcon = rd_regl(port, S3C2410_ULCON);
1247 ucon = rd_regl(port, S3C2410_UCON);
1248 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1249
1250 dbg("s3c24xx_serial_get_options: port=%p\n"
1251 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1252 port, ulcon, ucon, ubrdiv);
1253
1254 if ((ucon & 0xf) != 0) {
1255 /* consider the serial port configured if the tx/rx mode set */
1256
1257 switch (ulcon & S3C2410_LCON_CSMASK) {
1258 case S3C2410_LCON_CS5:
1259 *bits = 5;
1260 break;
1261 case S3C2410_LCON_CS6:
1262 *bits = 6;
1263 break;
1264 case S3C2410_LCON_CS7:
1265 *bits = 7;
1266 break;
1267 default:
1268 case S3C2410_LCON_CS8:
1269 *bits = 8;
1270 break;
1271 }
1272
1273 switch (ulcon & S3C2410_LCON_PMASK) {
1274 case S3C2410_LCON_PEVEN:
1275 *parity = 'e';
1276 break;
1277
1278 case S3C2410_LCON_PODD:
1279 *parity = 'o';
1280 break;
1281
1282 case S3C2410_LCON_PNONE:
1283 default:
1284 *parity = 'n';
1285 }
1286
1287 /* now calculate the baud rate */
1288
1289 s3c24xx_serial_getsource(port, &clksrc);
1290
1291 clk = clk_get(port->dev, clksrc.name);
1292 if (!IS_ERR(clk) && clk != NULL)
1293 rate = clk_get_rate(clk) / clksrc.divisor;
1294 else
1295 rate = 1;
1296
1297
1298 *baud = rate / (16 * (ubrdiv + 1));
1299 dbg("calculated baud %d\n", *baud);
1300 }
1301
1302}
1303
1304/* s3c24xx_serial_init_ports
1305 *
1306 * initialise the serial ports from the machine provided initialisation
1307 * data.
1308*/
1309
1310static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info *info)
1311{
1312 struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1313 struct platform_device **platdev_ptr;
1314 int i;
1315
1316 dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1317
1318 platdev_ptr = s3c24xx_uart_devs;
1319
1320 for (i = 0; i < NR_PORTS; i++, ptr++, platdev_ptr++) {
1321 s3c24xx_serial_init_port(ptr, info, *platdev_ptr);
1322 }
1323
1324 return 0;
1325}
1326
1327static int __init
1328s3c24xx_serial_console_setup(struct console *co, char *options)
1329{
1330 struct uart_port *port;
1331 int baud = 9600;
1332 int bits = 8;
1333 int parity = 'n';
1334 int flow = 'n';
1335
1336 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1337 co, co->index, options);
1338
1339 /* is this a valid port */
1340
1341 if (co->index == -1 || co->index >= NR_PORTS)
1342 co->index = 0;
1343
1344 port = &s3c24xx_serial_ports[co->index].port;
1345
1346 /* is the port configured? */
1347
1348 if (port->mapbase == 0x0) {
1349 co->index = 0;
1350 port = &s3c24xx_serial_ports[co->index].port;
1351 }
1352
1353 cons_uart = port;
1354
1355 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1356
1357 /*
1358 * Check whether an invalid uart number has been specified, and
1359 * if so, search for the first available port that does have
1360 * console support.
1361 */
1362 if (options)
1363 uart_parse_options(options, &baud, &parity, &bits, &flow);
1364 else
1365 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1366
1367 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1368
1369 return uart_set_options(port, co, baud, parity, bits, flow);
1370}
1371
1372/* s3c24xx_serial_initconsole
1373 *
1374 * initialise the console from one of the uart drivers
1375*/
1376
1377static struct console s3c24xx_serial_console = {
1378 .name = S3C24XX_SERIAL_NAME,
1379 .device = uart_console_device,
1380 .flags = CON_PRINTBUFFER,
1381 .index = -1,
1382 .write = s3c24xx_serial_console_write,
1383 .setup = s3c24xx_serial_console_setup
1384};
1385
1386int s3c24xx_serial_initconsole(struct platform_driver *drv,
1387 struct s3c24xx_uart_info *info)
1388
1389{
1390 struct platform_device *dev = s3c24xx_uart_devs[0];
1391
1392 dbg("s3c24xx_serial_initconsole\n");
1393
1394 /* select driver based on the cpu */
1395
1396 if (dev == NULL) {
1397 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1398 return 0;
1399 }
1400
1401 if (strcmp(dev->name, drv->driver.name) != 0)
1402 return 0;
1403
1404 s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1405 s3c24xx_serial_init_ports(info);
1406
1407 register_console(&s3c24xx_serial_console);
1408 return 0;
1409}
1410
1411#endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
1412
1413MODULE_DESCRIPTION("Samsung SoC Serial port driver");
1414MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1415MODULE_LICENSE("GPL v2");