blob: 1b7d2c0abd7eb5c59346a9373a3f2c270d9b8f3e [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * Driver core for Samsung SoC onboard UARTs.
3 *
4 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
5 * http://armlinux.simtec.co.uk/
6 *
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.
10*/
11
12/* Hote on 2410 error handling
13 *
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.
19 *
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
23 *
24 * BJD, 04-Nov-2004
25*/
26
27#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
28#define SUPPORT_SYSRQ
29#endif
30
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>
45#include <linux/of.h>
46
47#include <asm/irq.h>
48
49#include <mach/hardware.h>
50#include <mach/map.h>
51
52#include <plat/regs-serial.h>
53#include <plat/clock.h>
54
55#include "samsung.h"
56
57/* UART name and device definitions */
58
59#define S3C24XX_SERIAL_NAME "ttySAC"
60#define S3C24XX_SERIAL_MAJOR 204
61#define S3C24XX_SERIAL_MINOR 64
62
63/* macros to change one thing to another */
64
65#define tx_enabled(port) ((port)->unused[0])
66#define rx_enabled(port) ((port)->unused[1])
67
68/* flag to ignore all characters coming in */
69#define RXSTAT_DUMMY_READ (0x10000000)
70
71static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
72{
73 return container_of(port, struct s3c24xx_uart_port, port);
74}
75
76/* translate a port to the device name */
77
78static inline const char *s3c24xx_serial_portname(struct uart_port *port)
79{
80 return to_platform_device(port->dev)->name;
81}
82
83static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
84{
85 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
86}
87
88/*
89 * s3c64xx and later SoC's include the interrupt mask and status registers in
90 * the controller itself, unlike the s3c24xx SoC's which have these registers
91 * in the interrupt controller. Check if the port type is s3c64xx or higher.
92 */
93static int s3c24xx_serial_has_interrupt_mask(struct uart_port *port)
94{
95 return to_ourport(port)->info->type == PORT_S3C6400;
96}
97
98static void s3c24xx_serial_rx_enable(struct uart_port *port)
99{
100 unsigned long flags;
101 unsigned int ucon, ufcon;
102 int count = 10000;
103
104 spin_lock_irqsave(&port->lock, flags);
105
106 while (--count && !s3c24xx_serial_txempty_nofifo(port))
107 udelay(100);
108
109 ufcon = rd_regl(port, S3C2410_UFCON);
110 ufcon |= S3C2410_UFCON_RESETRX;
111 wr_regl(port, S3C2410_UFCON, ufcon);
112
113 ucon = rd_regl(port, S3C2410_UCON);
114 ucon |= S3C2410_UCON_RXIRQMODE;
115 wr_regl(port, S3C2410_UCON, ucon);
116
117 rx_enabled(port) = 1;
118 spin_unlock_irqrestore(&port->lock, flags);
119}
120
121static void s3c24xx_serial_rx_disable(struct uart_port *port)
122{
123 unsigned long flags;
124 unsigned int ucon;
125
126 spin_lock_irqsave(&port->lock, flags);
127
128 ucon = rd_regl(port, S3C2410_UCON);
129 ucon &= ~S3C2410_UCON_RXIRQMODE;
130 wr_regl(port, S3C2410_UCON, ucon);
131
132 rx_enabled(port) = 0;
133 spin_unlock_irqrestore(&port->lock, flags);
134}
135
136static void s3c24xx_serial_stop_tx(struct uart_port *port)
137{
138 struct s3c24xx_uart_port *ourport = to_ourport(port);
139
140 if (tx_enabled(port)) {
141 if (s3c24xx_serial_has_interrupt_mask(port))
142 __set_bit(S3C64XX_UINTM_TXD,
143 portaddrl(port, S3C64XX_UINTM));
144 else
145 disable_irq_nosync(ourport->tx_irq);
146 tx_enabled(port) = 0;
147 if (port->flags & UPF_CONS_FLOW)
148 s3c24xx_serial_rx_enable(port);
149 }
150}
151
152static void s3c24xx_serial_start_tx(struct uart_port *port)
153{
154 struct s3c24xx_uart_port *ourport = to_ourport(port);
155
156 if (!tx_enabled(port)) {
157 if (port->flags & UPF_CONS_FLOW)
158 s3c24xx_serial_rx_disable(port);
159
160 if (s3c24xx_serial_has_interrupt_mask(port))
161 __clear_bit(S3C64XX_UINTM_TXD,
162 portaddrl(port, S3C64XX_UINTM));
163 else
164 enable_irq(ourport->tx_irq);
165 tx_enabled(port) = 1;
166 }
167}
168
169static void s3c24xx_serial_stop_rx(struct uart_port *port)
170{
171 struct s3c24xx_uart_port *ourport = to_ourport(port);
172
173 if (rx_enabled(port)) {
174 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
175 if (s3c24xx_serial_has_interrupt_mask(port))
176 __set_bit(S3C64XX_UINTM_RXD,
177 portaddrl(port, S3C64XX_UINTM));
178 else
179 disable_irq_nosync(ourport->rx_irq);
180 rx_enabled(port) = 0;
181 }
182}
183
184static void s3c24xx_serial_enable_ms(struct uart_port *port)
185{
186}
187
188static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
189{
190 return to_ourport(port)->info;
191}
192
193static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
194{
195 struct s3c24xx_uart_port *ourport;
196
197 if (port->dev == NULL)
198 return NULL;
199
200 ourport = container_of(port, struct s3c24xx_uart_port, port);
201 return ourport->cfg;
202}
203
204static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
205 unsigned long ufstat)
206{
207 struct s3c24xx_uart_info *info = ourport->info;
208
209 if (ufstat & info->rx_fifofull)
210 return ourport->port.fifosize;
211
212 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
213}
214
215
216/* ? - where has parity gone?? */
217#define S3C2410_UERSTAT_PARITY (0x1000)
218
219static irqreturn_t
220s3c24xx_serial_rx_chars(int irq, void *dev_id)
221{
222 struct s3c24xx_uart_port *ourport = dev_id;
223 struct uart_port *port = &ourport->port;
224 struct tty_struct *tty = port->state->port.tty;
225 unsigned int ufcon, ch, flag, ufstat, uerstat;
226 int max_count = 64;
227
228 while (max_count-- > 0) {
229 ufcon = rd_regl(port, S3C2410_UFCON);
230 ufstat = rd_regl(port, S3C2410_UFSTAT);
231
232 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
233 break;
234
235 uerstat = rd_regl(port, S3C2410_UERSTAT);
236 ch = rd_regb(port, S3C2410_URXH);
237
238 if (port->flags & UPF_CONS_FLOW) {
239 int txe = s3c24xx_serial_txempty_nofifo(port);
240
241 if (rx_enabled(port)) {
242 if (!txe) {
243 rx_enabled(port) = 0;
244 continue;
245 }
246 } else {
247 if (txe) {
248 ufcon |= S3C2410_UFCON_RESETRX;
249 wr_regl(port, S3C2410_UFCON, ufcon);
250 rx_enabled(port) = 1;
251 goto out;
252 }
253 continue;
254 }
255 }
256
257 /* insert the character into the buffer */
258
259 flag = TTY_NORMAL;
260 port->icount.rx++;
261
262 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
263 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
264 ch, uerstat);
265
266 /* check for break */
267 if (uerstat & S3C2410_UERSTAT_BREAK) {
268 dbg("break!\n");
269 port->icount.brk++;
270 if (uart_handle_break(port))
271 goto ignore_char;
272 }
273
274 if (uerstat & S3C2410_UERSTAT_FRAME)
275 port->icount.frame++;
276 if (uerstat & S3C2410_UERSTAT_OVERRUN)
277 port->icount.overrun++;
278
279 uerstat &= port->read_status_mask;
280
281 if (uerstat & S3C2410_UERSTAT_BREAK)
282 flag = TTY_BREAK;
283 else if (uerstat & S3C2410_UERSTAT_PARITY)
284 flag = TTY_PARITY;
285 else if (uerstat & (S3C2410_UERSTAT_FRAME |
286 S3C2410_UERSTAT_OVERRUN))
287 flag = TTY_FRAME;
288 }
289
290 if (uart_handle_sysrq_char(port, ch))
291 goto ignore_char;
292
293 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
294 ch, flag);
295
296 ignore_char:
297 continue;
298 }
299 tty_flip_buffer_push(tty);
300
301 out:
302 return IRQ_HANDLED;
303}
304
305static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
306{
307 struct s3c24xx_uart_port *ourport = id;
308 struct uart_port *port = &ourport->port;
309 struct circ_buf *xmit = &port->state->xmit;
310 int count = 256;
311
312 if (port->x_char) {
313 wr_regb(port, S3C2410_UTXH, port->x_char);
314 port->icount.tx++;
315 port->x_char = 0;
316 goto out;
317 }
318
319 /* if there isn't anything more to transmit, or the uart is now
320 * stopped, disable the uart and exit
321 */
322
323 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
324 s3c24xx_serial_stop_tx(port);
325 goto out;
326 }
327
328 /* try and drain the buffer... */
329
330 while (!uart_circ_empty(xmit) && count-- > 0) {
331 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
332 break;
333
334 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
335 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
336 port->icount.tx++;
337 }
338
339 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
340 uart_write_wakeup(port);
341
342 if (uart_circ_empty(xmit))
343 s3c24xx_serial_stop_tx(port);
344
345 out:
346 return IRQ_HANDLED;
347}
348
349/* interrupt handler for s3c64xx and later SoC's.*/
350static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
351{
352 struct s3c24xx_uart_port *ourport = id;
353 struct uart_port *port = &ourport->port;
354 unsigned int pend = rd_regl(port, S3C64XX_UINTP);
355 unsigned long flags;
356 irqreturn_t ret = IRQ_HANDLED;
357
358 spin_lock_irqsave(&port->lock, flags);
359 if (pend & S3C64XX_UINTM_RXD_MSK) {
360 ret = s3c24xx_serial_rx_chars(irq, id);
361 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
362 }
363 if (pend & S3C64XX_UINTM_TXD_MSK) {
364 ret = s3c24xx_serial_tx_chars(irq, id);
365 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
366 }
367 spin_unlock_irqrestore(&port->lock, flags);
368 return ret;
369}
370
371static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
372{
373 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
374 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
375 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
376
377 if (ufcon & S3C2410_UFCON_FIFOMODE) {
378 if ((ufstat & info->tx_fifomask) != 0 ||
379 (ufstat & info->tx_fifofull))
380 return 0;
381
382 return 1;
383 }
384
385 return s3c24xx_serial_txempty_nofifo(port);
386}
387
388/* no modem control lines */
389static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
390{
391 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
392
393 if (umstat & S3C2410_UMSTAT_CTS)
394 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
395 else
396 return TIOCM_CAR | TIOCM_DSR;
397}
398
399static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
400{
401 /* todo - possibly remove AFC and do manual CTS */
402}
403
404static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
405{
406 unsigned long flags;
407 unsigned int ucon;
408
409 spin_lock_irqsave(&port->lock, flags);
410
411 ucon = rd_regl(port, S3C2410_UCON);
412
413 if (break_state)
414 ucon |= S3C2410_UCON_SBREAK;
415 else
416 ucon &= ~S3C2410_UCON_SBREAK;
417
418 wr_regl(port, S3C2410_UCON, ucon);
419
420 spin_unlock_irqrestore(&port->lock, flags);
421}
422
423static void s3c24xx_serial_shutdown(struct uart_port *port)
424{
425 struct s3c24xx_uart_port *ourport = to_ourport(port);
426
427 if (ourport->tx_claimed) {
428 if (!s3c24xx_serial_has_interrupt_mask(port))
429 free_irq(ourport->tx_irq, ourport);
430 tx_enabled(port) = 0;
431 ourport->tx_claimed = 0;
432 }
433
434 if (ourport->rx_claimed) {
435 if (!s3c24xx_serial_has_interrupt_mask(port))
436 free_irq(ourport->rx_irq, ourport);
437 ourport->rx_claimed = 0;
438 rx_enabled(port) = 0;
439 }
440
441 /* Clear pending interrupts and mask all interrupts */
442 if (s3c24xx_serial_has_interrupt_mask(port)) {
443 wr_regl(port, S3C64XX_UINTP, 0xf);
444 wr_regl(port, S3C64XX_UINTM, 0xf);
445 }
446}
447
448static int s3c24xx_serial_startup(struct uart_port *port)
449{
450 struct s3c24xx_uart_port *ourport = to_ourport(port);
451 int ret;
452
453 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
454 port->mapbase, port->membase);
455
456 rx_enabled(port) = 1;
457
458 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
459 s3c24xx_serial_portname(port), ourport);
460
461 if (ret != 0) {
462 printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
463 return ret;
464 }
465
466 ourport->rx_claimed = 1;
467
468 dbg("requesting tx irq...\n");
469
470 tx_enabled(port) = 1;
471
472 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
473 s3c24xx_serial_portname(port), ourport);
474
475 if (ret) {
476 printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
477 goto err;
478 }
479
480 ourport->tx_claimed = 1;
481
482 dbg("s3c24xx_serial_startup ok\n");
483
484 /* the port reset code should have done the correct
485 * register setup for the port controls */
486
487 return ret;
488
489 err:
490 s3c24xx_serial_shutdown(port);
491 return ret;
492}
493
494static int s3c64xx_serial_startup(struct uart_port *port)
495{
496 struct s3c24xx_uart_port *ourport = to_ourport(port);
497 int ret;
498
499 dbg("s3c64xx_serial_startup: port=%p (%08lx,%p)\n",
500 port->mapbase, port->membase);
501
502 ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
503 s3c24xx_serial_portname(port), ourport);
504 if (ret) {
505 printk(KERN_ERR "cannot get irq %d\n", port->irq);
506 return ret;
507 }
508
509 /* For compatibility with s3c24xx Soc's */
510 rx_enabled(port) = 1;
511 ourport->rx_claimed = 1;
512 tx_enabled(port) = 0;
513 ourport->tx_claimed = 1;
514
515 /* Enable Rx Interrupt */
516 __clear_bit(S3C64XX_UINTM_RXD, portaddrl(port, S3C64XX_UINTM));
517 dbg("s3c64xx_serial_startup ok\n");
518 return ret;
519}
520
521/* power power management control */
522
523static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
524 unsigned int old)
525{
526 struct s3c24xx_uart_port *ourport = to_ourport(port);
527 int timeout = 10000;
528
529 ourport->pm_level = level;
530
531 switch (level) {
532 case 3:
533 while (--timeout && !s3c24xx_serial_txempty_nofifo(port))
534 udelay(100);
535
536 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
537 clk_disable(ourport->baudclk);
538
539 clk_disable(ourport->clk);
540 break;
541
542 case 0:
543 clk_enable(ourport->clk);
544
545 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
546 clk_enable(ourport->baudclk);
547
548 break;
549 default:
550 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
551 }
552}
553
554/* baud rate calculation
555 *
556 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
557 * of different sources, including the peripheral clock ("pclk") and an
558 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
559 * with a programmable extra divisor.
560 *
561 * The following code goes through the clock sources, and calculates the
562 * baud clocks (and the resultant actual baud rates) and then tries to
563 * pick the closest one and select that.
564 *
565*/
566
567#define MAX_CLK_NAME_LENGTH 15
568
569static inline int s3c24xx_serial_getsource(struct uart_port *port)
570{
571 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
572 unsigned int ucon;
573
574 if (info->num_clks == 1)
575 return 0;
576
577 ucon = rd_regl(port, S3C2410_UCON);
578 ucon &= info->clksel_mask;
579 return ucon >> info->clksel_shift;
580}
581
582static void s3c24xx_serial_setsource(struct uart_port *port,
583 unsigned int clk_sel)
584{
585 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
586 unsigned int ucon;
587
588 if (info->num_clks == 1)
589 return;
590
591 ucon = rd_regl(port, S3C2410_UCON);
592 if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
593 return;
594
595 ucon &= ~info->clksel_mask;
596 ucon |= clk_sel << info->clksel_shift;
597 wr_regl(port, S3C2410_UCON, ucon);
598}
599
600static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
601 unsigned int req_baud, struct clk **best_clk,
602 unsigned int *clk_num)
603{
604 struct s3c24xx_uart_info *info = ourport->info;
605 struct clk *clk;
606 unsigned long rate;
607 unsigned int cnt, baud, quot, clk_sel, best_quot = 0;
608 char clkname[MAX_CLK_NAME_LENGTH];
609 int calc_deviation, deviation = (1 << 30) - 1;
610
611 *best_clk = NULL;
612 clk_sel = (ourport->cfg->clk_sel) ? ourport->cfg->clk_sel :
613 ourport->info->def_clk_sel;
614 for (cnt = 0; cnt < info->num_clks; cnt++) {
615 if (!(clk_sel & (1 << cnt)))
616 continue;
617
618 sprintf(clkname, "clk_uart_baud%d", cnt);
619 clk = clk_get(ourport->port.dev, clkname);
620 if (IS_ERR_OR_NULL(clk))
621 continue;
622
623 rate = clk_get_rate(clk);
624 if (!rate)
625 continue;
626
627 if (ourport->info->has_divslot) {
628 unsigned long div = rate / req_baud;
629
630 /* The UDIVSLOT register on the newer UARTs allows us to
631 * get a divisor adjustment of 1/16th on the baud clock.
632 *
633 * We don't keep the UDIVSLOT value (the 16ths we
634 * calculated by not multiplying the baud by 16) as it
635 * is easy enough to recalculate.
636 */
637
638 quot = div / 16;
639 baud = rate / div;
640 } else {
641 quot = (rate + (8 * req_baud)) / (16 * req_baud);
642 baud = rate / (quot * 16);
643 }
644 quot--;
645
646 calc_deviation = req_baud - baud;
647 if (calc_deviation < 0)
648 calc_deviation = -calc_deviation;
649
650 if (calc_deviation < deviation) {
651 *best_clk = clk;
652 best_quot = quot;
653 *clk_num = cnt;
654 deviation = calc_deviation;
655 }
656 }
657
658 return best_quot;
659}
660
661/* udivslot_table[]
662 *
663 * This table takes the fractional value of the baud divisor and gives
664 * the recommended setting for the UDIVSLOT register.
665 */
666static u16 udivslot_table[16] = {
667 [0] = 0x0000,
668 [1] = 0x0080,
669 [2] = 0x0808,
670 [3] = 0x0888,
671 [4] = 0x2222,
672 [5] = 0x4924,
673 [6] = 0x4A52,
674 [7] = 0x54AA,
675 [8] = 0x5555,
676 [9] = 0xD555,
677 [10] = 0xD5D5,
678 [11] = 0xDDD5,
679 [12] = 0xDDDD,
680 [13] = 0xDFDD,
681 [14] = 0xDFDF,
682 [15] = 0xFFDF,
683};
684
685static void s3c24xx_serial_set_termios(struct uart_port *port,
686 struct ktermios *termios,
687 struct ktermios *old)
688{
689 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
690 struct s3c24xx_uart_port *ourport = to_ourport(port);
691 struct clk *clk = NULL;
692 unsigned long flags;
693 unsigned int baud, quot, clk_sel = 0;
694 unsigned int ulcon;
695 unsigned int umcon;
696 unsigned int udivslot = 0;
697
698 /*
699 * We don't support modem control lines.
700 */
701 termios->c_cflag &= ~(HUPCL | CMSPAR);
702 termios->c_cflag |= CLOCAL;
703
704 /*
705 * Ask the core to calculate the divisor for us.
706 */
707
708 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
709 quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
710 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
711 quot = port->custom_divisor;
712 if (!clk)
713 return;
714
715 /* check to see if we need to change clock source */
716
717 if (ourport->baudclk != clk) {
718 s3c24xx_serial_setsource(port, clk_sel);
719
720 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
721 clk_disable(ourport->baudclk);
722 ourport->baudclk = NULL;
723 }
724
725 clk_enable(clk);
726
727 ourport->baudclk = clk;
728 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
729 }
730
731 if (ourport->info->has_divslot) {
732 unsigned int div = ourport->baudclk_rate / baud;
733
734 if (cfg->has_fracval) {
735 udivslot = (div & 15);
736 dbg("fracval = %04x\n", udivslot);
737 } else {
738 udivslot = udivslot_table[div & 15];
739 dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
740 }
741 }
742
743 switch (termios->c_cflag & CSIZE) {
744 case CS5:
745 dbg("config: 5bits/char\n");
746 ulcon = S3C2410_LCON_CS5;
747 break;
748 case CS6:
749 dbg("config: 6bits/char\n");
750 ulcon = S3C2410_LCON_CS6;
751 break;
752 case CS7:
753 dbg("config: 7bits/char\n");
754 ulcon = S3C2410_LCON_CS7;
755 break;
756 case CS8:
757 default:
758 dbg("config: 8bits/char\n");
759 ulcon = S3C2410_LCON_CS8;
760 break;
761 }
762
763 /* preserve original lcon IR settings */
764 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
765
766 if (termios->c_cflag & CSTOPB)
767 ulcon |= S3C2410_LCON_STOPB;
768
769 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
770
771 if (termios->c_cflag & PARENB) {
772 if (termios->c_cflag & PARODD)
773 ulcon |= S3C2410_LCON_PODD;
774 else
775 ulcon |= S3C2410_LCON_PEVEN;
776 } else {
777 ulcon |= S3C2410_LCON_PNONE;
778 }
779
780 spin_lock_irqsave(&port->lock, flags);
781
782 dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
783 ulcon, quot, udivslot);
784
785 wr_regl(port, S3C2410_ULCON, ulcon);
786 wr_regl(port, S3C2410_UBRDIV, quot);
787 wr_regl(port, S3C2410_UMCON, umcon);
788
789 if (ourport->info->has_divslot)
790 wr_regl(port, S3C2443_DIVSLOT, udivslot);
791
792 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
793 rd_regl(port, S3C2410_ULCON),
794 rd_regl(port, S3C2410_UCON),
795 rd_regl(port, S3C2410_UFCON));
796
797 /*
798 * Update the per-port timeout.
799 */
800 uart_update_timeout(port, termios->c_cflag, baud);
801
802 /*
803 * Which character status flags are we interested in?
804 */
805 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
806 if (termios->c_iflag & INPCK)
807 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
808
809 /*
810 * Which character status flags should we ignore?
811 */
812 port->ignore_status_mask = 0;
813 if (termios->c_iflag & IGNPAR)
814 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
815 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
816 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
817
818 /*
819 * Ignore all characters if CREAD is not set.
820 */
821 if ((termios->c_cflag & CREAD) == 0)
822 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
823
824 spin_unlock_irqrestore(&port->lock, flags);
825}
826
827static const char *s3c24xx_serial_type(struct uart_port *port)
828{
829 switch (port->type) {
830 case PORT_S3C2410:
831 return "S3C2410";
832 case PORT_S3C2440:
833 return "S3C2440";
834 case PORT_S3C2412:
835 return "S3C2412";
836 case PORT_S3C6400:
837 return "S3C6400/10";
838 default:
839 return NULL;
840 }
841}
842
843#define MAP_SIZE (0x100)
844
845static void s3c24xx_serial_release_port(struct uart_port *port)
846{
847 release_mem_region(port->mapbase, MAP_SIZE);
848}
849
850static int s3c24xx_serial_request_port(struct uart_port *port)
851{
852 const char *name = s3c24xx_serial_portname(port);
853 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
854}
855
856static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
857{
858 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
859
860 if (flags & UART_CONFIG_TYPE &&
861 s3c24xx_serial_request_port(port) == 0)
862 port->type = info->type;
863}
864
865/*
866 * verify the new serial_struct (for TIOCSSERIAL).
867 */
868static int
869s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
870{
871 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
872
873 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
874 return -EINVAL;
875
876 return 0;
877}
878
879
880#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
881
882static struct console s3c24xx_serial_console;
883
884#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
885#else
886#define S3C24XX_SERIAL_CONSOLE NULL
887#endif
888
889static struct uart_ops s3c24xx_serial_ops = {
890 .pm = s3c24xx_serial_pm,
891 .tx_empty = s3c24xx_serial_tx_empty,
892 .get_mctrl = s3c24xx_serial_get_mctrl,
893 .set_mctrl = s3c24xx_serial_set_mctrl,
894 .stop_tx = s3c24xx_serial_stop_tx,
895 .start_tx = s3c24xx_serial_start_tx,
896 .stop_rx = s3c24xx_serial_stop_rx,
897 .enable_ms = s3c24xx_serial_enable_ms,
898 .break_ctl = s3c24xx_serial_break_ctl,
899 .startup = s3c24xx_serial_startup,
900 .shutdown = s3c24xx_serial_shutdown,
901 .set_termios = s3c24xx_serial_set_termios,
902 .type = s3c24xx_serial_type,
903 .release_port = s3c24xx_serial_release_port,
904 .request_port = s3c24xx_serial_request_port,
905 .config_port = s3c24xx_serial_config_port,
906 .verify_port = s3c24xx_serial_verify_port,
907};
908
909static struct uart_driver s3c24xx_uart_drv = {
910 .owner = THIS_MODULE,
911 .driver_name = "s3c2410_serial",
912 .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
913 .cons = S3C24XX_SERIAL_CONSOLE,
914 .dev_name = S3C24XX_SERIAL_NAME,
915 .major = S3C24XX_SERIAL_MAJOR,
916 .minor = S3C24XX_SERIAL_MINOR,
917};
918
919static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
920 [0] = {
921 .port = {
922 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
923 .iotype = UPIO_MEM,
924 .uartclk = 0,
925 .fifosize = 16,
926 .ops = &s3c24xx_serial_ops,
927 .flags = UPF_BOOT_AUTOCONF,
928 .line = 0,
929 }
930 },
931 [1] = {
932 .port = {
933 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
934 .iotype = UPIO_MEM,
935 .uartclk = 0,
936 .fifosize = 16,
937 .ops = &s3c24xx_serial_ops,
938 .flags = UPF_BOOT_AUTOCONF,
939 .line = 1,
940 }
941 },
942#if CONFIG_SERIAL_SAMSUNG_UARTS > 2
943
944 [2] = {
945 .port = {
946 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
947 .iotype = UPIO_MEM,
948 .uartclk = 0,
949 .fifosize = 16,
950 .ops = &s3c24xx_serial_ops,
951 .flags = UPF_BOOT_AUTOCONF,
952 .line = 2,
953 }
954 },
955#endif
956#if CONFIG_SERIAL_SAMSUNG_UARTS > 3
957 [3] = {
958 .port = {
959 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
960 .iotype = UPIO_MEM,
961 .uartclk = 0,
962 .fifosize = 16,
963 .ops = &s3c24xx_serial_ops,
964 .flags = UPF_BOOT_AUTOCONF,
965 .line = 3,
966 }
967 }
968#endif
969};
970
971/* s3c24xx_serial_resetport
972 *
973 * reset the fifos and other the settings.
974*/
975
976static void s3c24xx_serial_resetport(struct uart_port *port,
977 struct s3c2410_uartcfg *cfg)
978{
979 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
980 unsigned long ucon = rd_regl(port, S3C2410_UCON);
981 unsigned int ucon_mask;
982
983 ucon_mask = info->clksel_mask;
984 if (info->type == PORT_S3C2440)
985 ucon_mask |= S3C2440_UCON0_DIVMASK;
986
987 ucon &= ucon_mask;
988 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
989 wr_regl(port, S3C2410_ULCON, cfg->ulcon);
990
991 /* reset both fifos */
992 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
993 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
994
995 /* some delay is required after fifo reset */
996 udelay(1);
997}
998
999
1000#ifdef CONFIG_CPU_FREQ
1001
1002static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
1003 unsigned long val, void *data)
1004{
1005 struct s3c24xx_uart_port *port;
1006 struct uart_port *uport;
1007
1008 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
1009 uport = &port->port;
1010
1011 /* check to see if port is enabled */
1012
1013 if (port->pm_level != 0)
1014 return 0;
1015
1016 /* try and work out if the baudrate is changing, we can detect
1017 * a change in rate, but we do not have support for detecting
1018 * a disturbance in the clock-rate over the change.
1019 */
1020
1021 if (IS_ERR(port->clk))
1022 goto exit;
1023
1024 if (port->baudclk_rate == clk_get_rate(port->clk))
1025 goto exit;
1026
1027 if (val == CPUFREQ_PRECHANGE) {
1028 /* we should really shut the port down whilst the
1029 * frequency change is in progress. */
1030
1031 } else if (val == CPUFREQ_POSTCHANGE) {
1032 struct ktermios *termios;
1033 struct tty_struct *tty;
1034
1035 if (uport->state == NULL)
1036 goto exit;
1037
1038 tty = uport->state->port.tty;
1039
1040 if (tty == NULL)
1041 goto exit;
1042
1043 termios = tty->termios;
1044
1045 if (termios == NULL) {
1046 printk(KERN_WARNING "%s: no termios?\n", __func__);
1047 goto exit;
1048 }
1049
1050 s3c24xx_serial_set_termios(uport, termios, NULL);
1051 }
1052
1053 exit:
1054 return 0;
1055}
1056
1057static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1058{
1059 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1060
1061 return cpufreq_register_notifier(&port->freq_transition,
1062 CPUFREQ_TRANSITION_NOTIFIER);
1063}
1064
1065static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1066{
1067 cpufreq_unregister_notifier(&port->freq_transition,
1068 CPUFREQ_TRANSITION_NOTIFIER);
1069}
1070
1071#else
1072static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1073{
1074 return 0;
1075}
1076
1077static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1078{
1079}
1080#endif
1081
1082/* s3c24xx_serial_init_port
1083 *
1084 * initialise a single serial port from the platform device given
1085 */
1086
1087static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1088 struct platform_device *platdev)
1089{
1090 struct uart_port *port = &ourport->port;
1091 struct s3c2410_uartcfg *cfg = ourport->cfg;
1092 struct resource *res;
1093 int ret;
1094
1095 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1096
1097 if (platdev == NULL)
1098 return -ENODEV;
1099
1100 if (port->mapbase != 0)
1101 return 0;
1102
1103 /* setup info for port */
1104 port->dev = &platdev->dev;
1105
1106 /* Startup sequence is different for s3c64xx and higher SoC's */
1107 if (s3c24xx_serial_has_interrupt_mask(port))
1108 s3c24xx_serial_ops.startup = s3c64xx_serial_startup;
1109
1110 port->uartclk = 1;
1111
1112 if (cfg->uart_flags & UPF_CONS_FLOW) {
1113 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1114 port->flags |= UPF_CONS_FLOW;
1115 }
1116
1117 /* sort our the physical and virtual addresses for each UART */
1118
1119 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1120 if (res == NULL) {
1121 printk(KERN_ERR "failed to find memory resource for uart\n");
1122 return -EINVAL;
1123 }
1124
1125 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1126
1127 port->mapbase = res->start;
1128 port->membase = S3C_VA_UART + (res->start & 0xfffff);
1129 ret = platform_get_irq(platdev, 0);
1130 if (ret < 0)
1131 port->irq = 0;
1132 else {
1133 port->irq = ret;
1134 ourport->rx_irq = ret;
1135 ourport->tx_irq = ret + 1;
1136 }
1137
1138 ret = platform_get_irq(platdev, 1);
1139 if (ret > 0)
1140 ourport->tx_irq = ret;
1141
1142 ourport->clk = clk_get(&platdev->dev, "uart");
1143
1144 /* Keep all interrupts masked and cleared */
1145 if (s3c24xx_serial_has_interrupt_mask(port)) {
1146 wr_regl(port, S3C64XX_UINTM, 0xf);
1147 wr_regl(port, S3C64XX_UINTP, 0xf);
1148 wr_regl(port, S3C64XX_UINTSP, 0xf);
1149 }
1150
1151 dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
1152 port->mapbase, port->membase, port->irq,
1153 ourport->rx_irq, ourport->tx_irq, port->uartclk);
1154
1155 /* reset the fifos (and setup the uart) */
1156 s3c24xx_serial_resetport(port, cfg);
1157 return 0;
1158}
1159
1160static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
1161 struct device_attribute *attr,
1162 char *buf)
1163{
1164 struct uart_port *port = s3c24xx_dev_to_port(dev);
1165 struct s3c24xx_uart_port *ourport = to_ourport(port);
1166
1167 return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->baudclk->name);
1168}
1169
1170static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
1171
1172
1173/* Device driver serial port probe */
1174
1175static const struct of_device_id s3c24xx_uart_dt_match[];
1176static int probe_index;
1177
1178static inline struct s3c24xx_serial_drv_data *s3c24xx_get_driver_data(
1179 struct platform_device *pdev)
1180{
1181#ifdef CONFIG_OF
1182 if (pdev->dev.of_node) {
1183 const struct of_device_id *match;
1184 match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
1185 return (struct s3c24xx_serial_drv_data *)match->data;
1186 }
1187#endif
1188 return (struct s3c24xx_serial_drv_data *)
1189 platform_get_device_id(pdev)->driver_data;
1190}
1191
1192static int s3c24xx_serial_probe(struct platform_device *pdev)
1193{
1194 struct s3c24xx_uart_port *ourport;
1195 int ret;
1196
1197 dbg("s3c24xx_serial_probe(%p) %d\n", pdev, probe_index);
1198
1199 ourport = &s3c24xx_serial_ports[probe_index];
1200
1201 ourport->drv_data = s3c24xx_get_driver_data(pdev);
1202 if (!ourport->drv_data) {
1203 dev_err(&pdev->dev, "could not find driver data\n");
1204 return -ENODEV;
1205 }
1206
1207 ourport->info = ourport->drv_data->info;
1208 ourport->cfg = (pdev->dev.platform_data) ?
1209 (struct s3c2410_uartcfg *)pdev->dev.platform_data :
1210 ourport->drv_data->def_cfg;
1211
1212 ourport->port.fifosize = (ourport->info->fifosize) ?
1213 ourport->info->fifosize :
1214 ourport->drv_data->fifosize[probe_index];
1215
1216 probe_index++;
1217
1218 dbg("%s: initialising port %p...\n", __func__, ourport);
1219
1220 ret = s3c24xx_serial_init_port(ourport, pdev);
1221 if (ret < 0)
1222 goto probe_err;
1223
1224 dbg("%s: adding port\n", __func__);
1225 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1226 platform_set_drvdata(pdev, &ourport->port);
1227
1228 ret = device_create_file(&pdev->dev, &dev_attr_clock_source);
1229 if (ret < 0)
1230 dev_err(&pdev->dev, "failed to add clock source attr.\n");
1231
1232 ret = s3c24xx_serial_cpufreq_register(ourport);
1233 if (ret < 0)
1234 dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
1235
1236 return 0;
1237
1238 probe_err:
1239 return ret;
1240}
1241
1242static int __devexit s3c24xx_serial_remove(struct platform_device *dev)
1243{
1244 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1245
1246 if (port) {
1247 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
1248 device_remove_file(&dev->dev, &dev_attr_clock_source);
1249 uart_remove_one_port(&s3c24xx_uart_drv, port);
1250 }
1251
1252 return 0;
1253}
1254
1255/* UART power management code */
1256#ifdef CONFIG_PM_SLEEP
1257static int s3c24xx_serial_suspend(struct device *dev)
1258{
1259 struct uart_port *port = s3c24xx_dev_to_port(dev);
1260
1261 if (port)
1262 uart_suspend_port(&s3c24xx_uart_drv, port);
1263
1264 return 0;
1265}
1266
1267static int s3c24xx_serial_resume(struct device *dev)
1268{
1269 struct uart_port *port = s3c24xx_dev_to_port(dev);
1270 struct s3c24xx_uart_port *ourport = to_ourport(port);
1271
1272 if (port) {
1273 clk_enable(ourport->clk);
1274 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1275 clk_disable(ourport->clk);
1276
1277 uart_resume_port(&s3c24xx_uart_drv, port);
1278 }
1279
1280 return 0;
1281}
1282
1283static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
1284 .suspend = s3c24xx_serial_suspend,
1285 .resume = s3c24xx_serial_resume,
1286};
1287#define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
1288
1289#else /* !CONFIG_PM_SLEEP */
1290
1291#define SERIAL_SAMSUNG_PM_OPS NULL
1292#endif /* CONFIG_PM_SLEEP */
1293
1294/* Console code */
1295
1296#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1297
1298static struct uart_port *cons_uart;
1299
1300static int
1301s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1302{
1303 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1304 unsigned long ufstat, utrstat;
1305
1306 if (ufcon & S3C2410_UFCON_FIFOMODE) {
1307 /* fifo mode - check amount of data in fifo registers... */
1308
1309 ufstat = rd_regl(port, S3C2410_UFSTAT);
1310 return (ufstat & info->tx_fifofull) ? 0 : 1;
1311 }
1312
1313 /* in non-fifo mode, we go and use the tx buffer empty */
1314
1315 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1316 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1317}
1318
1319static void
1320s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
1321{
1322 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1323 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1324 barrier();
1325 wr_regb(cons_uart, S3C2410_UTXH, ch);
1326}
1327
1328static void
1329s3c24xx_serial_console_write(struct console *co, const char *s,
1330 unsigned int count)
1331{
1332 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
1333}
1334
1335static void __init
1336s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1337 int *parity, int *bits)
1338{
1339 struct clk *clk;
1340 unsigned int ulcon;
1341 unsigned int ucon;
1342 unsigned int ubrdiv;
1343 unsigned long rate;
1344 unsigned int clk_sel;
1345 char clk_name[MAX_CLK_NAME_LENGTH];
1346
1347 ulcon = rd_regl(port, S3C2410_ULCON);
1348 ucon = rd_regl(port, S3C2410_UCON);
1349 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1350
1351 dbg("s3c24xx_serial_get_options: port=%p\n"
1352 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1353 port, ulcon, ucon, ubrdiv);
1354
1355 if ((ucon & 0xf) != 0) {
1356 /* consider the serial port configured if the tx/rx mode set */
1357
1358 switch (ulcon & S3C2410_LCON_CSMASK) {
1359 case S3C2410_LCON_CS5:
1360 *bits = 5;
1361 break;
1362 case S3C2410_LCON_CS6:
1363 *bits = 6;
1364 break;
1365 case S3C2410_LCON_CS7:
1366 *bits = 7;
1367 break;
1368 default:
1369 case S3C2410_LCON_CS8:
1370 *bits = 8;
1371 break;
1372 }
1373
1374 switch (ulcon & S3C2410_LCON_PMASK) {
1375 case S3C2410_LCON_PEVEN:
1376 *parity = 'e';
1377 break;
1378
1379 case S3C2410_LCON_PODD:
1380 *parity = 'o';
1381 break;
1382
1383 case S3C2410_LCON_PNONE:
1384 default:
1385 *parity = 'n';
1386 }
1387
1388 /* now calculate the baud rate */
1389
1390 clk_sel = s3c24xx_serial_getsource(port);
1391 sprintf(clk_name, "clk_uart_baud%d", clk_sel);
1392
1393 clk = clk_get(port->dev, clk_name);
1394 if (!IS_ERR(clk) && clk != NULL)
1395 rate = clk_get_rate(clk);
1396 else
1397 rate = 1;
1398
1399 *baud = rate / (16 * (ubrdiv + 1));
1400 dbg("calculated baud %d\n", *baud);
1401 }
1402
1403}
1404
1405static int __init
1406s3c24xx_serial_console_setup(struct console *co, char *options)
1407{
1408 struct uart_port *port;
1409 int baud = 9600;
1410 int bits = 8;
1411 int parity = 'n';
1412 int flow = 'n';
1413
1414 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1415 co, co->index, options);
1416
1417 /* is this a valid port */
1418
1419 if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
1420 co->index = 0;
1421
1422 port = &s3c24xx_serial_ports[co->index].port;
1423
1424 /* is the port configured? */
1425
1426 if (port->mapbase == 0x0)
1427 return -ENODEV;
1428
1429 cons_uart = port;
1430
1431 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1432
1433 /*
1434 * Check whether an invalid uart number has been specified, and
1435 * if so, search for the first available port that does have
1436 * console support.
1437 */
1438 if (options)
1439 uart_parse_options(options, &baud, &parity, &bits, &flow);
1440 else
1441 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1442
1443 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1444
1445 return uart_set_options(port, co, baud, parity, bits, flow);
1446}
1447
1448static 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,
1455 .data = &s3c24xx_uart_drv,
1456};
1457#endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
1458
1459#ifdef CONFIG_CPU_S3C2410
1460static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
1461 .info = &(struct s3c24xx_uart_info) {
1462 .name = "Samsung S3C2410 UART",
1463 .type = PORT_S3C2410,
1464 .fifosize = 16,
1465 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
1466 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
1467 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
1468 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
1469 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
1470 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
1471 .def_clk_sel = S3C2410_UCON_CLKSEL0,
1472 .num_clks = 2,
1473 .clksel_mask = S3C2410_UCON_CLKMASK,
1474 .clksel_shift = S3C2410_UCON_CLKSHIFT,
1475 },
1476 .def_cfg = &(struct s3c2410_uartcfg) {
1477 .ucon = S3C2410_UCON_DEFAULT,
1478 .ufcon = S3C2410_UFCON_DEFAULT,
1479 },
1480};
1481#define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
1482#else
1483#define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1484#endif
1485
1486#ifdef CONFIG_CPU_S3C2412
1487static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
1488 .info = &(struct s3c24xx_uart_info) {
1489 .name = "Samsung S3C2412 UART",
1490 .type = PORT_S3C2412,
1491 .fifosize = 64,
1492 .has_divslot = 1,
1493 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1494 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1495 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1496 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1497 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1498 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1499 .def_clk_sel = S3C2410_UCON_CLKSEL2,
1500 .num_clks = 4,
1501 .clksel_mask = S3C2412_UCON_CLKMASK,
1502 .clksel_shift = S3C2412_UCON_CLKSHIFT,
1503 },
1504 .def_cfg = &(struct s3c2410_uartcfg) {
1505 .ucon = S3C2410_UCON_DEFAULT,
1506 .ufcon = S3C2410_UFCON_DEFAULT,
1507 },
1508};
1509#define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
1510#else
1511#define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1512#endif
1513
1514#if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
1515 defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
1516static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
1517 .info = &(struct s3c24xx_uart_info) {
1518 .name = "Samsung S3C2440 UART",
1519 .type = PORT_S3C2440,
1520 .fifosize = 64,
1521 .has_divslot = 1,
1522 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1523 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1524 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1525 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1526 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1527 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1528 .def_clk_sel = S3C2410_UCON_CLKSEL2,
1529 .num_clks = 4,
1530 .clksel_mask = S3C2412_UCON_CLKMASK,
1531 .clksel_shift = S3C2412_UCON_CLKSHIFT,
1532 },
1533 .def_cfg = &(struct s3c2410_uartcfg) {
1534 .ucon = S3C2410_UCON_DEFAULT,
1535 .ufcon = S3C2410_UFCON_DEFAULT,
1536 },
1537};
1538#define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
1539#else
1540#define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1541#endif
1542
1543#if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410) || \
1544 defined(CONFIG_CPU_S5P6440) || defined(CONFIG_CPU_S5P6450) || \
1545 defined(CONFIG_CPU_S5PC100)
1546static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
1547 .info = &(struct s3c24xx_uart_info) {
1548 .name = "Samsung S3C6400 UART",
1549 .type = PORT_S3C6400,
1550 .fifosize = 64,
1551 .has_divslot = 1,
1552 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1553 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1554 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1555 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1556 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1557 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1558 .def_clk_sel = S3C2410_UCON_CLKSEL2,
1559 .num_clks = 4,
1560 .clksel_mask = S3C6400_UCON_CLKMASK,
1561 .clksel_shift = S3C6400_UCON_CLKSHIFT,
1562 },
1563 .def_cfg = &(struct s3c2410_uartcfg) {
1564 .ucon = S3C2410_UCON_DEFAULT,
1565 .ufcon = S3C2410_UFCON_DEFAULT,
1566 },
1567};
1568#define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
1569#else
1570#define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1571#endif
1572
1573#ifdef CONFIG_CPU_S5PV210
1574static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
1575 .info = &(struct s3c24xx_uart_info) {
1576 .name = "Samsung S5PV210 UART",
1577 .type = PORT_S3C6400,
1578 .has_divslot = 1,
1579 .rx_fifomask = S5PV210_UFSTAT_RXMASK,
1580 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
1581 .rx_fifofull = S5PV210_UFSTAT_RXFULL,
1582 .tx_fifofull = S5PV210_UFSTAT_TXFULL,
1583 .tx_fifomask = S5PV210_UFSTAT_TXMASK,
1584 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
1585 .def_clk_sel = S3C2410_UCON_CLKSEL0,
1586 .num_clks = 2,
1587 .clksel_mask = S5PV210_UCON_CLKMASK,
1588 .clksel_shift = S5PV210_UCON_CLKSHIFT,
1589 },
1590 .def_cfg = &(struct s3c2410_uartcfg) {
1591 .ucon = S5PV210_UCON_DEFAULT,
1592 .ufcon = S5PV210_UFCON_DEFAULT,
1593 },
1594 .fifosize = { 256, 64, 16, 16 },
1595};
1596#define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
1597#else
1598#define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1599#endif
1600
1601#if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212) || \
1602 defined(CONFIG_SOC_EXYNOS4412) || defined(CONFIG_SOC_EXYNOS5250)
1603static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
1604 .info = &(struct s3c24xx_uart_info) {
1605 .name = "Samsung Exynos4 UART",
1606 .type = PORT_S3C6400,
1607 .has_divslot = 1,
1608 .rx_fifomask = S5PV210_UFSTAT_RXMASK,
1609 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
1610 .rx_fifofull = S5PV210_UFSTAT_RXFULL,
1611 .tx_fifofull = S5PV210_UFSTAT_TXFULL,
1612 .tx_fifomask = S5PV210_UFSTAT_TXMASK,
1613 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
1614 .def_clk_sel = S3C2410_UCON_CLKSEL0,
1615 .num_clks = 1,
1616 .clksel_mask = 0,
1617 .clksel_shift = 0,
1618 },
1619 .def_cfg = &(struct s3c2410_uartcfg) {
1620 .ucon = S5PV210_UCON_DEFAULT,
1621 .ufcon = S5PV210_UFCON_DEFAULT,
1622 .has_fracval = 1,
1623 },
1624 .fifosize = { 256, 64, 16, 16 },
1625};
1626#define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
1627#else
1628#define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1629#endif
1630
1631static struct platform_device_id s3c24xx_serial_driver_ids[] = {
1632 {
1633 .name = "s3c2410-uart",
1634 .driver_data = S3C2410_SERIAL_DRV_DATA,
1635 }, {
1636 .name = "s3c2412-uart",
1637 .driver_data = S3C2412_SERIAL_DRV_DATA,
1638 }, {
1639 .name = "s3c2440-uart",
1640 .driver_data = S3C2440_SERIAL_DRV_DATA,
1641 }, {
1642 .name = "s3c6400-uart",
1643 .driver_data = S3C6400_SERIAL_DRV_DATA,
1644 }, {
1645 .name = "s5pv210-uart",
1646 .driver_data = S5PV210_SERIAL_DRV_DATA,
1647 }, {
1648 .name = "exynos4210-uart",
1649 .driver_data = EXYNOS4210_SERIAL_DRV_DATA,
1650 },
1651 { },
1652};
1653MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
1654
1655#ifdef CONFIG_OF
1656static const struct of_device_id s3c24xx_uart_dt_match[] = {
1657 { .compatible = "samsung,exynos4210-uart",
1658 .data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
1659 {},
1660};
1661MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
1662#else
1663#define s3c24xx_uart_dt_match NULL
1664#endif
1665
1666static struct platform_driver samsung_serial_driver = {
1667 .probe = s3c24xx_serial_probe,
1668 .remove = __devexit_p(s3c24xx_serial_remove),
1669 .id_table = s3c24xx_serial_driver_ids,
1670 .driver = {
1671 .name = "samsung-uart",
1672 .owner = THIS_MODULE,
1673 .pm = SERIAL_SAMSUNG_PM_OPS,
1674 .of_match_table = s3c24xx_uart_dt_match,
1675 },
1676};
1677
1678/* module initialisation code */
1679
1680static int __init s3c24xx_serial_modinit(void)
1681{
1682 int ret;
1683
1684 ret = uart_register_driver(&s3c24xx_uart_drv);
1685 if (ret < 0) {
1686 printk(KERN_ERR "failed to register UART driver\n");
1687 return -1;
1688 }
1689
1690 return platform_driver_register(&samsung_serial_driver);
1691}
1692
1693static void __exit s3c24xx_serial_modexit(void)
1694{
1695 uart_unregister_driver(&s3c24xx_uart_drv);
1696}
1697
1698module_init(s3c24xx_serial_modinit);
1699module_exit(s3c24xx_serial_modexit);
1700
1701MODULE_ALIAS("platform:samsung-uart");
1702MODULE_DESCRIPTION("Samsung SoC Serial port driver");
1703MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1704MODULE_LICENSE("GPL v2");