blob: 7883f11b8c53bf3f36a546f074166bc6a11c81fd [file] [log] [blame]
Jovi Zhang99edb3d2011-03-30 05:30:41 -04001/*
Ben Dooksb4975492008-07-03 12:32:51 +01002 * Driver core for Samsung SoC onboard UARTs.
3 *
Ben Dooksccae9412009-11-13 22:54:14 +00004 * Ben Dooks, Copyright (c) 2003-2008 Simtec Electronics
Ben Dooksb4975492008-07-03 12:32:51 +01005 * 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>
Ben Dooks30555472008-10-21 14:06:36 +010044#include <linux/cpufreq.h>
Thomas Abraham26c919e2011-11-06 22:10:44 +053045#include <linux/of.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 Dooksb4975492008-07-03 12:32:51 +010050
Ben Dooksa2b7ba92008-10-07 22:26:09 +010051#include <plat/regs-serial.h>
Thomas Abraham5f5a7a52011-10-24 11:47:46 +020052#include <plat/clock.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
Ben Dooksb4975492008-07-03 12:32:51 +010062/* macros to change one thing to another */
63
64#define tx_enabled(port) ((port)->unused[0])
65#define rx_enabled(port) ((port)->unused[1])
66
Lucas De Marchi25985ed2011-03-30 22:57:33 -030067/* flag to ignore all characters coming in */
Ben Dooksb4975492008-07-03 12:32:51 +010068#define RXSTAT_DUMMY_READ (0x10000000)
69
70static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
71{
72 return container_of(port, struct s3c24xx_uart_port, port);
73}
74
75/* translate a port to the device name */
76
77static inline const char *s3c24xx_serial_portname(struct uart_port *port)
78{
79 return to_platform_device(port->dev)->name;
80}
81
82static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
83{
Sachin Kamat9303ac12012-09-05 10:30:11 +053084 return rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE;
Ben Dooksb4975492008-07-03 12:32:51 +010085}
86
Thomas Abraham88bb4ea2011-08-10 15:51:19 +053087/*
88 * s3c64xx and later SoC's include the interrupt mask and status registers in
89 * the controller itself, unlike the s3c24xx SoC's which have these registers
90 * in the interrupt controller. Check if the port type is s3c64xx or higher.
91 */
92static int s3c24xx_serial_has_interrupt_mask(struct uart_port *port)
93{
94 return to_ourport(port)->info->type == PORT_S3C6400;
95}
96
Ben Dooksb4975492008-07-03 12:32:51 +010097static void s3c24xx_serial_rx_enable(struct uart_port *port)
98{
99 unsigned long flags;
100 unsigned int ucon, ufcon;
101 int count = 10000;
102
103 spin_lock_irqsave(&port->lock, flags);
104
105 while (--count && !s3c24xx_serial_txempty_nofifo(port))
106 udelay(100);
107
108 ufcon = rd_regl(port, S3C2410_UFCON);
109 ufcon |= S3C2410_UFCON_RESETRX;
110 wr_regl(port, S3C2410_UFCON, ufcon);
111
112 ucon = rd_regl(port, S3C2410_UCON);
113 ucon |= S3C2410_UCON_RXIRQMODE;
114 wr_regl(port, S3C2410_UCON, ucon);
115
116 rx_enabled(port) = 1;
117 spin_unlock_irqrestore(&port->lock, flags);
118}
119
120static void s3c24xx_serial_rx_disable(struct uart_port *port)
121{
122 unsigned long flags;
123 unsigned int ucon;
124
125 spin_lock_irqsave(&port->lock, flags);
126
127 ucon = rd_regl(port, S3C2410_UCON);
128 ucon &= ~S3C2410_UCON_RXIRQMODE;
129 wr_regl(port, S3C2410_UCON, ucon);
130
131 rx_enabled(port) = 0;
132 spin_unlock_irqrestore(&port->lock, flags);
133}
134
135static void s3c24xx_serial_stop_tx(struct uart_port *port)
136{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100137 struct s3c24xx_uart_port *ourport = to_ourport(port);
138
Ben Dooksb4975492008-07-03 12:32:51 +0100139 if (tx_enabled(port)) {
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530140 if (s3c24xx_serial_has_interrupt_mask(port))
141 __set_bit(S3C64XX_UINTM_TXD,
142 portaddrl(port, S3C64XX_UINTM));
143 else
144 disable_irq_nosync(ourport->tx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100145 tx_enabled(port) = 0;
146 if (port->flags & UPF_CONS_FLOW)
147 s3c24xx_serial_rx_enable(port);
148 }
149}
150
151static void s3c24xx_serial_start_tx(struct uart_port *port)
152{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100153 struct s3c24xx_uart_port *ourport = to_ourport(port);
154
Ben Dooksb4975492008-07-03 12:32:51 +0100155 if (!tx_enabled(port)) {
156 if (port->flags & UPF_CONS_FLOW)
157 s3c24xx_serial_rx_disable(port);
158
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530159 if (s3c24xx_serial_has_interrupt_mask(port))
160 __clear_bit(S3C64XX_UINTM_TXD,
161 portaddrl(port, S3C64XX_UINTM));
162 else
163 enable_irq(ourport->tx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100164 tx_enabled(port) = 1;
165 }
166}
167
Ben Dooksb4975492008-07-03 12:32:51 +0100168static void s3c24xx_serial_stop_rx(struct uart_port *port)
169{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100170 struct s3c24xx_uart_port *ourport = to_ourport(port);
171
Ben Dooksb4975492008-07-03 12:32:51 +0100172 if (rx_enabled(port)) {
173 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530174 if (s3c24xx_serial_has_interrupt_mask(port))
175 __set_bit(S3C64XX_UINTM_RXD,
176 portaddrl(port, S3C64XX_UINTM));
177 else
178 disable_irq_nosync(ourport->rx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100179 rx_enabled(port) = 0;
180 }
181}
182
183static void s3c24xx_serial_enable_ms(struct uart_port *port)
184{
185}
186
187static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
188{
189 return to_ourport(port)->info;
190}
191
192static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
193{
Thomas Abraham4d84e972011-10-24 11:47:25 +0200194 struct s3c24xx_uart_port *ourport;
195
Ben Dooksb4975492008-07-03 12:32:51 +0100196 if (port->dev == NULL)
197 return NULL;
198
Thomas Abraham4d84e972011-10-24 11:47:25 +0200199 ourport = container_of(port, struct s3c24xx_uart_port, port);
200 return ourport->cfg;
Ben Dooksb4975492008-07-03 12:32:51 +0100201}
202
203static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
204 unsigned long ufstat)
205{
206 struct s3c24xx_uart_info *info = ourport->info;
207
208 if (ufstat & info->rx_fifofull)
Thomas Abrahamda121502011-11-02 19:23:25 +0900209 return ourport->port.fifosize;
Ben Dooksb4975492008-07-03 12:32:51 +0100210
211 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
212}
213
214
215/* ? - where has parity gone?? */
216#define S3C2410_UERSTAT_PARITY (0x1000)
217
218static irqreturn_t
219s3c24xx_serial_rx_chars(int irq, void *dev_id)
220{
221 struct s3c24xx_uart_port *ourport = dev_id;
222 struct uart_port *port = &ourport->port;
Ben Dooksb4975492008-07-03 12:32:51 +0100223 unsigned int ufcon, ch, flag, ufstat, uerstat;
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530224 unsigned long flags;
Ben Dooksb4975492008-07-03 12:32:51 +0100225 int max_count = 64;
226
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530227 spin_lock_irqsave(&port->lock, flags);
228
Ben Dooksb4975492008-07-03 12:32:51 +0100229 while (max_count-- > 0) {
230 ufcon = rd_regl(port, S3C2410_UFCON);
231 ufstat = rd_regl(port, S3C2410_UFSTAT);
232
233 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
234 break;
235
236 uerstat = rd_regl(port, S3C2410_UERSTAT);
237 ch = rd_regb(port, S3C2410_URXH);
238
239 if (port->flags & UPF_CONS_FLOW) {
240 int txe = s3c24xx_serial_txempty_nofifo(port);
241
242 if (rx_enabled(port)) {
243 if (!txe) {
244 rx_enabled(port) = 0;
245 continue;
246 }
247 } else {
248 if (txe) {
249 ufcon |= S3C2410_UFCON_RESETRX;
250 wr_regl(port, S3C2410_UFCON, ufcon);
251 rx_enabled(port) = 1;
252 goto out;
253 }
254 continue;
255 }
256 }
257
258 /* insert the character into the buffer */
259
260 flag = TTY_NORMAL;
261 port->icount.rx++;
262
263 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
264 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
265 ch, uerstat);
266
267 /* check for break */
268 if (uerstat & S3C2410_UERSTAT_BREAK) {
269 dbg("break!\n");
270 port->icount.brk++;
271 if (uart_handle_break(port))
Sachin Kamat9303ac12012-09-05 10:30:11 +0530272 goto ignore_char;
Ben Dooksb4975492008-07-03 12:32:51 +0100273 }
274
275 if (uerstat & S3C2410_UERSTAT_FRAME)
276 port->icount.frame++;
277 if (uerstat & S3C2410_UERSTAT_OVERRUN)
278 port->icount.overrun++;
279
280 uerstat &= port->read_status_mask;
281
282 if (uerstat & S3C2410_UERSTAT_BREAK)
283 flag = TTY_BREAK;
284 else if (uerstat & S3C2410_UERSTAT_PARITY)
285 flag = TTY_PARITY;
286 else if (uerstat & (S3C2410_UERSTAT_FRAME |
287 S3C2410_UERSTAT_OVERRUN))
288 flag = TTY_FRAME;
289 }
290
291 if (uart_handle_sysrq_char(port, ch))
292 goto ignore_char;
293
294 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
295 ch, flag);
296
297 ignore_char:
298 continue;
299 }
Jiri Slaby2e124b42013-01-03 15:53:06 +0100300 tty_flip_buffer_push(&port->state->port);
Ben Dooksb4975492008-07-03 12:32:51 +0100301
302 out:
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530303 spin_unlock_irqrestore(&port->lock, flags);
Ben Dooksb4975492008-07-03 12:32:51 +0100304 return IRQ_HANDLED;
305}
306
307static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
308{
309 struct s3c24xx_uart_port *ourport = id;
310 struct uart_port *port = &ourport->port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700311 struct circ_buf *xmit = &port->state->xmit;
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530312 unsigned long flags;
Ben Dooksb4975492008-07-03 12:32:51 +0100313 int count = 256;
314
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530315 spin_lock_irqsave(&port->lock, flags);
316
Ben Dooksb4975492008-07-03 12:32:51 +0100317 if (port->x_char) {
318 wr_regb(port, S3C2410_UTXH, port->x_char);
319 port->icount.tx++;
320 port->x_char = 0;
321 goto out;
322 }
323
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300324 /* if there isn't anything more to transmit, or the uart is now
Ben Dooksb4975492008-07-03 12:32:51 +0100325 * stopped, disable the uart and exit
326 */
327
328 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
329 s3c24xx_serial_stop_tx(port);
330 goto out;
331 }
332
333 /* try and drain the buffer... */
334
335 while (!uart_circ_empty(xmit) && count-- > 0) {
336 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
337 break;
338
339 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
340 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
341 port->icount.tx++;
342 }
343
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530344 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
345 spin_unlock(&port->lock);
Ben Dooksb4975492008-07-03 12:32:51 +0100346 uart_write_wakeup(port);
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530347 spin_lock(&port->lock);
348 }
Ben Dooksb4975492008-07-03 12:32:51 +0100349
350 if (uart_circ_empty(xmit))
351 s3c24xx_serial_stop_tx(port);
352
353 out:
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530354 spin_unlock_irqrestore(&port->lock, flags);
Ben Dooksb4975492008-07-03 12:32:51 +0100355 return IRQ_HANDLED;
356}
357
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530358/* interrupt handler for s3c64xx and later SoC's.*/
359static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
360{
361 struct s3c24xx_uart_port *ourport = id;
362 struct uart_port *port = &ourport->port;
363 unsigned int pend = rd_regl(port, S3C64XX_UINTP);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530364 irqreturn_t ret = IRQ_HANDLED;
365
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530366 if (pend & S3C64XX_UINTM_RXD_MSK) {
367 ret = s3c24xx_serial_rx_chars(irq, id);
368 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
369 }
370 if (pend & S3C64XX_UINTM_TXD_MSK) {
371 ret = s3c24xx_serial_tx_chars(irq, id);
372 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
373 }
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530374 return ret;
375}
376
Ben Dooksb4975492008-07-03 12:32:51 +0100377static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
378{
379 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
380 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
381 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
382
383 if (ufcon & S3C2410_UFCON_FIFOMODE) {
384 if ((ufstat & info->tx_fifomask) != 0 ||
385 (ufstat & info->tx_fifofull))
386 return 0;
387
388 return 1;
389 }
390
391 return s3c24xx_serial_txempty_nofifo(port);
392}
393
394/* no modem control lines */
395static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
396{
397 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
398
399 if (umstat & S3C2410_UMSTAT_CTS)
400 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
401 else
402 return TIOCM_CAR | TIOCM_DSR;
403}
404
405static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
406{
407 /* todo - possibly remove AFC and do manual CTS */
408}
409
410static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
411{
412 unsigned long flags;
413 unsigned int ucon;
414
415 spin_lock_irqsave(&port->lock, flags);
416
417 ucon = rd_regl(port, S3C2410_UCON);
418
419 if (break_state)
420 ucon |= S3C2410_UCON_SBREAK;
421 else
422 ucon &= ~S3C2410_UCON_SBREAK;
423
424 wr_regl(port, S3C2410_UCON, ucon);
425
426 spin_unlock_irqrestore(&port->lock, flags);
427}
428
429static void s3c24xx_serial_shutdown(struct uart_port *port)
430{
431 struct s3c24xx_uart_port *ourport = to_ourport(port);
432
433 if (ourport->tx_claimed) {
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530434 if (!s3c24xx_serial_has_interrupt_mask(port))
435 free_irq(ourport->tx_irq, ourport);
Ben Dooksb4975492008-07-03 12:32:51 +0100436 tx_enabled(port) = 0;
437 ourport->tx_claimed = 0;
438 }
439
440 if (ourport->rx_claimed) {
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530441 if (!s3c24xx_serial_has_interrupt_mask(port))
442 free_irq(ourport->rx_irq, ourport);
Ben Dooksb4975492008-07-03 12:32:51 +0100443 ourport->rx_claimed = 0;
444 rx_enabled(port) = 0;
445 }
Ben Dooksb4975492008-07-03 12:32:51 +0100446
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530447 /* Clear pending interrupts and mask all interrupts */
448 if (s3c24xx_serial_has_interrupt_mask(port)) {
Tomasz Figab6ad2932013-03-26 15:57:35 +0100449 free_irq(port->irq, ourport);
450
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530451 wr_regl(port, S3C64XX_UINTP, 0xf);
452 wr_regl(port, S3C64XX_UINTM, 0xf);
453 }
454}
Ben Dooksb4975492008-07-03 12:32:51 +0100455
456static int s3c24xx_serial_startup(struct uart_port *port)
457{
458 struct s3c24xx_uart_port *ourport = to_ourport(port);
459 int ret;
460
461 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
462 port->mapbase, port->membase);
463
464 rx_enabled(port) = 1;
465
Ben Dooksb73c289c2008-10-21 14:07:04 +0100466 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
Ben Dooksb4975492008-07-03 12:32:51 +0100467 s3c24xx_serial_portname(port), ourport);
468
469 if (ret != 0) {
Sachin Kamatd20925e2012-09-05 10:30:10 +0530470 dev_err(port->dev, "cannot get irq %d\n", ourport->rx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100471 return ret;
472 }
473
474 ourport->rx_claimed = 1;
475
476 dbg("requesting tx irq...\n");
477
478 tx_enabled(port) = 1;
479
Ben Dooksb73c289c2008-10-21 14:07:04 +0100480 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
Ben Dooksb4975492008-07-03 12:32:51 +0100481 s3c24xx_serial_portname(port), ourport);
482
483 if (ret) {
Sachin Kamatd20925e2012-09-05 10:30:10 +0530484 dev_err(port->dev, "cannot get irq %d\n", ourport->tx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100485 goto err;
486 }
487
488 ourport->tx_claimed = 1;
489
490 dbg("s3c24xx_serial_startup ok\n");
491
492 /* the port reset code should have done the correct
493 * register setup for the port controls */
494
495 return ret;
496
497 err:
498 s3c24xx_serial_shutdown(port);
499 return ret;
500}
501
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530502static int s3c64xx_serial_startup(struct uart_port *port)
503{
504 struct s3c24xx_uart_port *ourport = to_ourport(port);
505 int ret;
506
507 dbg("s3c64xx_serial_startup: port=%p (%08lx,%p)\n",
508 port->mapbase, port->membase);
509
Tomasz Figab6ad2932013-03-26 15:57:35 +0100510 wr_regl(port, S3C64XX_UINTM, 0xf);
511
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530512 ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
513 s3c24xx_serial_portname(port), ourport);
514 if (ret) {
Sachin Kamatd20925e2012-09-05 10:30:10 +0530515 dev_err(port->dev, "cannot get irq %d\n", port->irq);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530516 return ret;
517 }
518
519 /* For compatibility with s3c24xx Soc's */
520 rx_enabled(port) = 1;
521 ourport->rx_claimed = 1;
522 tx_enabled(port) = 0;
523 ourport->tx_claimed = 1;
524
525 /* Enable Rx Interrupt */
526 __clear_bit(S3C64XX_UINTM_RXD, portaddrl(port, S3C64XX_UINTM));
527 dbg("s3c64xx_serial_startup ok\n");
528 return ret;
529}
530
Ben Dooksb4975492008-07-03 12:32:51 +0100531/* power power management control */
532
533static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
534 unsigned int old)
535{
536 struct s3c24xx_uart_port *ourport = to_ourport(port);
537
Ben Dooks30555472008-10-21 14:06:36 +0100538 ourport->pm_level = level;
539
Ben Dooksb4975492008-07-03 12:32:51 +0100540 switch (level) {
541 case 3:
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900542 if (!IS_ERR(ourport->baudclk))
Thomas Abraham9484b002012-10-03 07:40:04 +0900543 clk_disable_unprepare(ourport->baudclk);
Ben Dooksb4975492008-07-03 12:32:51 +0100544
Thomas Abraham9484b002012-10-03 07:40:04 +0900545 clk_disable_unprepare(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +0100546 break;
547
548 case 0:
Thomas Abraham9484b002012-10-03 07:40:04 +0900549 clk_prepare_enable(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +0100550
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900551 if (!IS_ERR(ourport->baudclk))
Thomas Abraham9484b002012-10-03 07:40:04 +0900552 clk_prepare_enable(ourport->baudclk);
Ben Dooksb4975492008-07-03 12:32:51 +0100553
554 break;
555 default:
Sachin Kamatd20925e2012-09-05 10:30:10 +0530556 dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level);
Ben Dooksb4975492008-07-03 12:32:51 +0100557 }
558}
559
560/* baud rate calculation
561 *
562 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
563 * of different sources, including the peripheral clock ("pclk") and an
564 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
565 * with a programmable extra divisor.
566 *
567 * The following code goes through the clock sources, and calculates the
568 * baud clocks (and the resultant actual baud rates) and then tries to
569 * pick the closest one and select that.
570 *
571*/
572
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200573#define MAX_CLK_NAME_LENGTH 15
Ben Dooksb4975492008-07-03 12:32:51 +0100574
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200575static inline int s3c24xx_serial_getsource(struct uart_port *port)
Ben Dooksb4975492008-07-03 12:32:51 +0100576{
577 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200578 unsigned int ucon;
Ben Dooksb4975492008-07-03 12:32:51 +0100579
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200580 if (info->num_clks == 1)
Ben Dooksb4975492008-07-03 12:32:51 +0100581 return 0;
582
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200583 ucon = rd_regl(port, S3C2410_UCON);
584 ucon &= info->clksel_mask;
585 return ucon >> info->clksel_shift;
Ben Dooksb4975492008-07-03 12:32:51 +0100586}
587
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200588static void s3c24xx_serial_setsource(struct uart_port *port,
589 unsigned int clk_sel)
Ben Dooksb4975492008-07-03 12:32:51 +0100590{
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200591 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
592 unsigned int ucon;
Ben Dooksb4975492008-07-03 12:32:51 +0100593
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200594 if (info->num_clks == 1)
595 return;
Ben Dooksb4975492008-07-03 12:32:51 +0100596
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200597 ucon = rd_regl(port, S3C2410_UCON);
598 if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
599 return;
Ben Dooksb4975492008-07-03 12:32:51 +0100600
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200601 ucon &= ~info->clksel_mask;
602 ucon |= clk_sel << info->clksel_shift;
603 wr_regl(port, S3C2410_UCON, ucon);
604}
Ben Dooksb4975492008-07-03 12:32:51 +0100605
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200606static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
607 unsigned int req_baud, struct clk **best_clk,
608 unsigned int *clk_num)
609{
610 struct s3c24xx_uart_info *info = ourport->info;
611 struct clk *clk;
612 unsigned long rate;
613 unsigned int cnt, baud, quot, clk_sel, best_quot = 0;
614 char clkname[MAX_CLK_NAME_LENGTH];
615 int calc_deviation, deviation = (1 << 30) - 1;
616
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200617 clk_sel = (ourport->cfg->clk_sel) ? ourport->cfg->clk_sel :
618 ourport->info->def_clk_sel;
619 for (cnt = 0; cnt < info->num_clks; cnt++) {
620 if (!(clk_sel & (1 << cnt)))
621 continue;
622
623 sprintf(clkname, "clk_uart_baud%d", cnt);
624 clk = clk_get(ourport->port.dev, clkname);
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900625 if (IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200626 continue;
627
628 rate = clk_get_rate(clk);
629 if (!rate)
630 continue;
631
632 if (ourport->info->has_divslot) {
633 unsigned long div = rate / req_baud;
634
635 /* The UDIVSLOT register on the newer UARTs allows us to
636 * get a divisor adjustment of 1/16th on the baud clock.
637 *
638 * We don't keep the UDIVSLOT value (the 16ths we
639 * calculated by not multiplying the baud by 16) as it
640 * is easy enough to recalculate.
641 */
642
643 quot = div / 16;
644 baud = rate / div;
645 } else {
646 quot = (rate + (8 * req_baud)) / (16 * req_baud);
647 baud = rate / (quot * 16);
648 }
649 quot--;
650
651 calc_deviation = req_baud - baud;
652 if (calc_deviation < 0)
653 calc_deviation = -calc_deviation;
654
655 if (calc_deviation < deviation) {
656 *best_clk = clk;
657 best_quot = quot;
658 *clk_num = cnt;
659 deviation = calc_deviation;
Ben Dooksb4975492008-07-03 12:32:51 +0100660 }
661 }
662
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200663 return best_quot;
Ben Dooksb4975492008-07-03 12:32:51 +0100664}
665
Ben Dooks090f8482008-12-12 00:24:21 +0000666/* udivslot_table[]
667 *
668 * This table takes the fractional value of the baud divisor and gives
669 * the recommended setting for the UDIVSLOT register.
670 */
671static u16 udivslot_table[16] = {
672 [0] = 0x0000,
673 [1] = 0x0080,
674 [2] = 0x0808,
675 [3] = 0x0888,
676 [4] = 0x2222,
677 [5] = 0x4924,
678 [6] = 0x4A52,
679 [7] = 0x54AA,
680 [8] = 0x5555,
681 [9] = 0xD555,
682 [10] = 0xD5D5,
683 [11] = 0xDDD5,
684 [12] = 0xDDDD,
685 [13] = 0xDFDD,
686 [14] = 0xDFDF,
687 [15] = 0xFFDF,
688};
689
Ben Dooksb4975492008-07-03 12:32:51 +0100690static void s3c24xx_serial_set_termios(struct uart_port *port,
691 struct ktermios *termios,
692 struct ktermios *old)
693{
694 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
695 struct s3c24xx_uart_port *ourport = to_ourport(port);
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900696 struct clk *clk = ERR_PTR(-EINVAL);
Ben Dooksb4975492008-07-03 12:32:51 +0100697 unsigned long flags;
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200698 unsigned int baud, quot, clk_sel = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100699 unsigned int ulcon;
700 unsigned int umcon;
Ben Dooks090f8482008-12-12 00:24:21 +0000701 unsigned int udivslot = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100702
703 /*
704 * We don't support modem control lines.
705 */
706 termios->c_cflag &= ~(HUPCL | CMSPAR);
707 termios->c_cflag |= CLOCAL;
708
709 /*
710 * Ask the core to calculate the divisor for us.
711 */
712
713 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200714 quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +0100715 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
716 quot = port->custom_divisor;
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900717 if (IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200718 return;
Ben Dooksb4975492008-07-03 12:32:51 +0100719
720 /* check to see if we need to change clock source */
721
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200722 if (ourport->baudclk != clk) {
723 s3c24xx_serial_setsource(port, clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +0100724
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900725 if (!IS_ERR(ourport->baudclk)) {
Thomas Abraham9484b002012-10-03 07:40:04 +0900726 clk_disable_unprepare(ourport->baudclk);
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900727 ourport->baudclk = ERR_PTR(-EINVAL);
Ben Dooksb4975492008-07-03 12:32:51 +0100728 }
729
Thomas Abraham9484b002012-10-03 07:40:04 +0900730 clk_prepare_enable(clk);
Ben Dooksb4975492008-07-03 12:32:51 +0100731
Ben Dooksb4975492008-07-03 12:32:51 +0100732 ourport->baudclk = clk;
Ben Dooks30555472008-10-21 14:06:36 +0100733 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100734 }
735
Ben Dooks090f8482008-12-12 00:24:21 +0000736 if (ourport->info->has_divslot) {
737 unsigned int div = ourport->baudclk_rate / baud;
738
Jongpill Lee8b526ae2010-07-16 10:19:41 +0900739 if (cfg->has_fracval) {
740 udivslot = (div & 15);
741 dbg("fracval = %04x\n", udivslot);
742 } else {
743 udivslot = udivslot_table[div & 15];
744 dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
745 }
Ben Dooks090f8482008-12-12 00:24:21 +0000746 }
747
Ben Dooksb4975492008-07-03 12:32:51 +0100748 switch (termios->c_cflag & CSIZE) {
749 case CS5:
750 dbg("config: 5bits/char\n");
751 ulcon = S3C2410_LCON_CS5;
752 break;
753 case CS6:
754 dbg("config: 6bits/char\n");
755 ulcon = S3C2410_LCON_CS6;
756 break;
757 case CS7:
758 dbg("config: 7bits/char\n");
759 ulcon = S3C2410_LCON_CS7;
760 break;
761 case CS8:
762 default:
763 dbg("config: 8bits/char\n");
764 ulcon = S3C2410_LCON_CS8;
765 break;
766 }
767
768 /* preserve original lcon IR settings */
769 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
770
771 if (termios->c_cflag & CSTOPB)
772 ulcon |= S3C2410_LCON_STOPB;
773
774 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
775
776 if (termios->c_cflag & PARENB) {
777 if (termios->c_cflag & PARODD)
778 ulcon |= S3C2410_LCON_PODD;
779 else
780 ulcon |= S3C2410_LCON_PEVEN;
781 } else {
782 ulcon |= S3C2410_LCON_PNONE;
783 }
784
785 spin_lock_irqsave(&port->lock, flags);
786
Ben Dooks090f8482008-12-12 00:24:21 +0000787 dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
788 ulcon, quot, udivslot);
Ben Dooksb4975492008-07-03 12:32:51 +0100789
790 wr_regl(port, S3C2410_ULCON, ulcon);
791 wr_regl(port, S3C2410_UBRDIV, quot);
792 wr_regl(port, S3C2410_UMCON, umcon);
793
Ben Dooks090f8482008-12-12 00:24:21 +0000794 if (ourport->info->has_divslot)
795 wr_regl(port, S3C2443_DIVSLOT, udivslot);
796
Ben Dooksb4975492008-07-03 12:32:51 +0100797 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
798 rd_regl(port, S3C2410_ULCON),
799 rd_regl(port, S3C2410_UCON),
800 rd_regl(port, S3C2410_UFCON));
801
802 /*
803 * Update the per-port timeout.
804 */
805 uart_update_timeout(port, termios->c_cflag, baud);
806
807 /*
808 * Which character status flags are we interested in?
809 */
810 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
811 if (termios->c_iflag & INPCK)
812 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
813
814 /*
815 * Which character status flags should we ignore?
816 */
817 port->ignore_status_mask = 0;
818 if (termios->c_iflag & IGNPAR)
819 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
820 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
821 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
822
823 /*
824 * Ignore all characters if CREAD is not set.
825 */
826 if ((termios->c_cflag & CREAD) == 0)
827 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
828
829 spin_unlock_irqrestore(&port->lock, flags);
830}
831
832static const char *s3c24xx_serial_type(struct uart_port *port)
833{
834 switch (port->type) {
835 case PORT_S3C2410:
836 return "S3C2410";
837 case PORT_S3C2440:
838 return "S3C2440";
839 case PORT_S3C2412:
840 return "S3C2412";
Ben Dooksb690ace2008-10-21 14:07:03 +0100841 case PORT_S3C6400:
842 return "S3C6400/10";
Ben Dooksb4975492008-07-03 12:32:51 +0100843 default:
844 return NULL;
845 }
846}
847
848#define MAP_SIZE (0x100)
849
850static void s3c24xx_serial_release_port(struct uart_port *port)
851{
852 release_mem_region(port->mapbase, MAP_SIZE);
853}
854
855static int s3c24xx_serial_request_port(struct uart_port *port)
856{
857 const char *name = s3c24xx_serial_portname(port);
858 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
859}
860
861static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
862{
863 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
864
865 if (flags & UART_CONFIG_TYPE &&
866 s3c24xx_serial_request_port(port) == 0)
867 port->type = info->type;
868}
869
870/*
871 * verify the new serial_struct (for TIOCSSERIAL).
872 */
873static int
874s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
875{
876 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
877
878 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
879 return -EINVAL;
880
881 return 0;
882}
883
884
885#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
886
887static struct console s3c24xx_serial_console;
888
Julien Pichon93b5c032012-09-21 23:22:31 -0700889static int __init s3c24xx_serial_console_init(void)
890{
891 register_console(&s3c24xx_serial_console);
892 return 0;
893}
894console_initcall(s3c24xx_serial_console_init);
895
Ben Dooksb4975492008-07-03 12:32:51 +0100896#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
897#else
898#define S3C24XX_SERIAL_CONSOLE NULL
899#endif
900
Julien Pichon93b5c032012-09-21 23:22:31 -0700901#ifdef CONFIG_CONSOLE_POLL
902static int s3c24xx_serial_get_poll_char(struct uart_port *port);
903static void s3c24xx_serial_put_poll_char(struct uart_port *port,
904 unsigned char c);
905#endif
906
Ben Dooksb4975492008-07-03 12:32:51 +0100907static struct uart_ops s3c24xx_serial_ops = {
908 .pm = s3c24xx_serial_pm,
909 .tx_empty = s3c24xx_serial_tx_empty,
910 .get_mctrl = s3c24xx_serial_get_mctrl,
911 .set_mctrl = s3c24xx_serial_set_mctrl,
912 .stop_tx = s3c24xx_serial_stop_tx,
913 .start_tx = s3c24xx_serial_start_tx,
914 .stop_rx = s3c24xx_serial_stop_rx,
915 .enable_ms = s3c24xx_serial_enable_ms,
916 .break_ctl = s3c24xx_serial_break_ctl,
917 .startup = s3c24xx_serial_startup,
918 .shutdown = s3c24xx_serial_shutdown,
919 .set_termios = s3c24xx_serial_set_termios,
920 .type = s3c24xx_serial_type,
921 .release_port = s3c24xx_serial_release_port,
922 .request_port = s3c24xx_serial_request_port,
923 .config_port = s3c24xx_serial_config_port,
924 .verify_port = s3c24xx_serial_verify_port,
Julien Pichon93b5c032012-09-21 23:22:31 -0700925#ifdef CONFIG_CONSOLE_POLL
926 .poll_get_char = s3c24xx_serial_get_poll_char,
927 .poll_put_char = s3c24xx_serial_put_poll_char,
928#endif
Ben Dooksb4975492008-07-03 12:32:51 +0100929};
930
Ben Dooksb4975492008-07-03 12:32:51 +0100931static struct uart_driver s3c24xx_uart_drv = {
932 .owner = THIS_MODULE,
Darius Augulis2cf0c582011-01-12 14:50:51 +0900933 .driver_name = "s3c2410_serial",
Ben Dooksbdd49152008-11-03 19:51:42 +0000934 .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
Ben Dooksb4975492008-07-03 12:32:51 +0100935 .cons = S3C24XX_SERIAL_CONSOLE,
Darius Augulis2cf0c582011-01-12 14:50:51 +0900936 .dev_name = S3C24XX_SERIAL_NAME,
Ben Dooksb4975492008-07-03 12:32:51 +0100937 .major = S3C24XX_SERIAL_MAJOR,
938 .minor = S3C24XX_SERIAL_MINOR,
939};
940
Ben Dooks03d5e772008-11-03 09:21:23 +0000941static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
Ben Dooksb4975492008-07-03 12:32:51 +0100942 [0] = {
943 .port = {
944 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
945 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +0100946 .uartclk = 0,
947 .fifosize = 16,
948 .ops = &s3c24xx_serial_ops,
949 .flags = UPF_BOOT_AUTOCONF,
950 .line = 0,
951 }
952 },
953 [1] = {
954 .port = {
955 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
956 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +0100957 .uartclk = 0,
958 .fifosize = 16,
959 .ops = &s3c24xx_serial_ops,
960 .flags = UPF_BOOT_AUTOCONF,
961 .line = 1,
962 }
963 },
Ben Dooks03d5e772008-11-03 09:21:23 +0000964#if CONFIG_SERIAL_SAMSUNG_UARTS > 2
Ben Dooksb4975492008-07-03 12:32:51 +0100965
966 [2] = {
967 .port = {
968 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
969 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +0100970 .uartclk = 0,
971 .fifosize = 16,
972 .ops = &s3c24xx_serial_ops,
973 .flags = UPF_BOOT_AUTOCONF,
974 .line = 2,
975 }
Ben Dooks03d5e772008-11-03 09:21:23 +0000976 },
977#endif
978#if CONFIG_SERIAL_SAMSUNG_UARTS > 3
979 [3] = {
980 .port = {
981 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
982 .iotype = UPIO_MEM,
Ben Dooks03d5e772008-11-03 09:21:23 +0000983 .uartclk = 0,
984 .fifosize = 16,
985 .ops = &s3c24xx_serial_ops,
986 .flags = UPF_BOOT_AUTOCONF,
987 .line = 3,
988 }
Ben Dooksb4975492008-07-03 12:32:51 +0100989 }
990#endif
991};
992
993/* s3c24xx_serial_resetport
994 *
Thomas Abraham0dfb3b42011-10-24 11:48:21 +0200995 * reset the fifos and other the settings.
Ben Dooksb4975492008-07-03 12:32:51 +0100996*/
997
Thomas Abraham0dfb3b42011-10-24 11:48:21 +0200998static void s3c24xx_serial_resetport(struct uart_port *port,
999 struct s3c2410_uartcfg *cfg)
Ben Dooksb4975492008-07-03 12:32:51 +01001000{
1001 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001002 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1003 unsigned int ucon_mask;
Ben Dooksb4975492008-07-03 12:32:51 +01001004
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001005 ucon_mask = info->clksel_mask;
1006 if (info->type == PORT_S3C2440)
1007 ucon_mask |= S3C2440_UCON0_DIVMASK;
1008
1009 ucon &= ucon_mask;
1010 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
1011
1012 /* reset both fifos */
1013 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1014 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1015
1016 /* some delay is required after fifo reset */
1017 udelay(1);
Ben Dooksb4975492008-07-03 12:32:51 +01001018}
1019
Ben Dooks30555472008-10-21 14:06:36 +01001020
1021#ifdef CONFIG_CPU_FREQ
1022
1023static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
1024 unsigned long val, void *data)
1025{
1026 struct s3c24xx_uart_port *port;
1027 struct uart_port *uport;
1028
1029 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
1030 uport = &port->port;
1031
1032 /* check to see if port is enabled */
1033
1034 if (port->pm_level != 0)
1035 return 0;
1036
1037 /* try and work out if the baudrate is changing, we can detect
1038 * a change in rate, but we do not have support for detecting
1039 * a disturbance in the clock-rate over the change.
1040 */
1041
Kyoungil Kim25f04ad2012-05-20 17:49:31 +09001042 if (IS_ERR(port->baudclk))
Ben Dooks30555472008-10-21 14:06:36 +01001043 goto exit;
1044
Kyoungil Kim25f04ad2012-05-20 17:49:31 +09001045 if (port->baudclk_rate == clk_get_rate(port->baudclk))
Ben Dooks30555472008-10-21 14:06:36 +01001046 goto exit;
1047
1048 if (val == CPUFREQ_PRECHANGE) {
1049 /* we should really shut the port down whilst the
1050 * frequency change is in progress. */
1051
1052 } else if (val == CPUFREQ_POSTCHANGE) {
1053 struct ktermios *termios;
1054 struct tty_struct *tty;
1055
Alan Coxebd2c8f2009-09-19 13:13:28 -07001056 if (uport->state == NULL)
Ben Dooks30555472008-10-21 14:06:36 +01001057 goto exit;
Ben Dooks30555472008-10-21 14:06:36 +01001058
Alan Coxebd2c8f2009-09-19 13:13:28 -07001059 tty = uport->state->port.tty;
Ben Dooks30555472008-10-21 14:06:36 +01001060
Ben Dooks7de40c22008-12-14 23:11:02 +00001061 if (tty == NULL)
Ben Dooks30555472008-10-21 14:06:36 +01001062 goto exit;
Ben Dooks30555472008-10-21 14:06:36 +01001063
Alan Coxadc8d742012-07-14 15:31:47 +01001064 termios = &tty->termios;
Ben Dooks30555472008-10-21 14:06:36 +01001065
1066 if (termios == NULL) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301067 dev_warn(uport->dev, "%s: no termios?\n", __func__);
Ben Dooks30555472008-10-21 14:06:36 +01001068 goto exit;
1069 }
1070
1071 s3c24xx_serial_set_termios(uport, termios, NULL);
1072 }
1073
1074 exit:
1075 return 0;
1076}
1077
1078static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1079{
1080 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1081
1082 return cpufreq_register_notifier(&port->freq_transition,
1083 CPUFREQ_TRANSITION_NOTIFIER);
1084}
1085
1086static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1087{
1088 cpufreq_unregister_notifier(&port->freq_transition,
1089 CPUFREQ_TRANSITION_NOTIFIER);
1090}
1091
1092#else
1093static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1094{
1095 return 0;
1096}
1097
1098static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1099{
1100}
1101#endif
1102
Ben Dooksb4975492008-07-03 12:32:51 +01001103/* s3c24xx_serial_init_port
1104 *
1105 * initialise a single serial port from the platform device given
1106 */
1107
1108static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
Ben Dooksb4975492008-07-03 12:32:51 +01001109 struct platform_device *platdev)
1110{
1111 struct uart_port *port = &ourport->port;
Thomas Abrahamda121502011-11-02 19:23:25 +09001112 struct s3c2410_uartcfg *cfg = ourport->cfg;
Ben Dooksb4975492008-07-03 12:32:51 +01001113 struct resource *res;
1114 int ret;
1115
1116 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1117
1118 if (platdev == NULL)
1119 return -ENODEV;
1120
Ben Dooksb4975492008-07-03 12:32:51 +01001121 if (port->mapbase != 0)
1122 return 0;
1123
Ben Dooksb4975492008-07-03 12:32:51 +01001124 /* setup info for port */
1125 port->dev = &platdev->dev;
Ben Dooksb4975492008-07-03 12:32:51 +01001126
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301127 /* Startup sequence is different for s3c64xx and higher SoC's */
1128 if (s3c24xx_serial_has_interrupt_mask(port))
1129 s3c24xx_serial_ops.startup = s3c64xx_serial_startup;
1130
Ben Dooksb4975492008-07-03 12:32:51 +01001131 port->uartclk = 1;
1132
1133 if (cfg->uart_flags & UPF_CONS_FLOW) {
1134 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1135 port->flags |= UPF_CONS_FLOW;
1136 }
1137
1138 /* sort our the physical and virtual addresses for each UART */
1139
1140 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1141 if (res == NULL) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301142 dev_err(port->dev, "failed to find memory resource for uart\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001143 return -EINVAL;
1144 }
1145
1146 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1147
Thomas Abraham41147bf2013-01-01 00:21:55 -08001148 port->membase = devm_ioremap(port->dev, res->start, resource_size(res));
1149 if (!port->membase) {
1150 dev_err(port->dev, "failed to remap controller address\n");
1151 return -EBUSY;
1152 }
1153
Ben Dooksb690ace2008-10-21 14:07:03 +01001154 port->mapbase = res->start;
Ben Dooksb4975492008-07-03 12:32:51 +01001155 ret = platform_get_irq(platdev, 0);
1156 if (ret < 0)
1157 port->irq = 0;
Ben Dooksb73c289c2008-10-21 14:07:04 +01001158 else {
Ben Dooksb4975492008-07-03 12:32:51 +01001159 port->irq = ret;
Ben Dooksb73c289c2008-10-21 14:07:04 +01001160 ourport->rx_irq = ret;
1161 ourport->tx_irq = ret + 1;
1162 }
Sachin Kamat9303ac12012-09-05 10:30:11 +05301163
Ben Dooksb73c289c2008-10-21 14:07:04 +01001164 ret = platform_get_irq(platdev, 1);
1165 if (ret > 0)
1166 ourport->tx_irq = ret;
Ben Dooksb4975492008-07-03 12:32:51 +01001167
1168 ourport->clk = clk_get(&platdev->dev, "uart");
1169
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301170 /* Keep all interrupts masked and cleared */
1171 if (s3c24xx_serial_has_interrupt_mask(port)) {
1172 wr_regl(port, S3C64XX_UINTM, 0xf);
1173 wr_regl(port, S3C64XX_UINTP, 0xf);
1174 wr_regl(port, S3C64XX_UINTSP, 0xf);
1175 }
1176
Ben Dooksb73c289c2008-10-21 14:07:04 +01001177 dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
1178 port->mapbase, port->membase, port->irq,
1179 ourport->rx_irq, ourport->tx_irq, port->uartclk);
Ben Dooksb4975492008-07-03 12:32:51 +01001180
1181 /* reset the fifos (and setup the uart) */
1182 s3c24xx_serial_resetport(port, cfg);
1183 return 0;
1184}
1185
1186static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
1187 struct device_attribute *attr,
1188 char *buf)
1189{
1190 struct uart_port *port = s3c24xx_dev_to_port(dev);
1191 struct s3c24xx_uart_port *ourport = to_ourport(port);
1192
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001193 if (IS_ERR(ourport->baudclk))
1194 return -EINVAL;
1195
KeyYoung Park7b15e1d2012-05-30 17:29:55 +09001196 return snprintf(buf, PAGE_SIZE, "* %s\n",
1197 ourport->baudclk->name ?: "(null)");
Ben Dooksb4975492008-07-03 12:32:51 +01001198}
1199
1200static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
1201
Thomas Abraham26c919e2011-11-06 22:10:44 +05301202
Ben Dooksb4975492008-07-03 12:32:51 +01001203/* Device driver serial port probe */
1204
Thomas Abraham26c919e2011-11-06 22:10:44 +05301205static const struct of_device_id s3c24xx_uart_dt_match[];
Ben Dooksb4975492008-07-03 12:32:51 +01001206static int probe_index;
1207
Thomas Abraham26c919e2011-11-06 22:10:44 +05301208static inline struct s3c24xx_serial_drv_data *s3c24xx_get_driver_data(
1209 struct platform_device *pdev)
1210{
1211#ifdef CONFIG_OF
1212 if (pdev->dev.of_node) {
1213 const struct of_device_id *match;
1214 match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
1215 return (struct s3c24xx_serial_drv_data *)match->data;
1216 }
1217#endif
1218 return (struct s3c24xx_serial_drv_data *)
1219 platform_get_device_id(pdev)->driver_data;
1220}
1221
Thomas Abrahamda121502011-11-02 19:23:25 +09001222static int s3c24xx_serial_probe(struct platform_device *pdev)
Ben Dooksb4975492008-07-03 12:32:51 +01001223{
1224 struct s3c24xx_uart_port *ourport;
1225 int ret;
1226
Thomas Abrahamda121502011-11-02 19:23:25 +09001227 dbg("s3c24xx_serial_probe(%p) %d\n", pdev, probe_index);
Ben Dooksb4975492008-07-03 12:32:51 +01001228
1229 ourport = &s3c24xx_serial_ports[probe_index];
Thomas Abrahamda121502011-11-02 19:23:25 +09001230
Thomas Abraham26c919e2011-11-06 22:10:44 +05301231 ourport->drv_data = s3c24xx_get_driver_data(pdev);
1232 if (!ourport->drv_data) {
1233 dev_err(&pdev->dev, "could not find driver data\n");
1234 return -ENODEV;
1235 }
Thomas Abrahamda121502011-11-02 19:23:25 +09001236
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001237 ourport->baudclk = ERR_PTR(-EINVAL);
Thomas Abrahamda121502011-11-02 19:23:25 +09001238 ourport->info = ourport->drv_data->info;
1239 ourport->cfg = (pdev->dev.platform_data) ?
1240 (struct s3c2410_uartcfg *)pdev->dev.platform_data :
1241 ourport->drv_data->def_cfg;
1242
1243 ourport->port.fifosize = (ourport->info->fifosize) ?
1244 ourport->info->fifosize :
1245 ourport->drv_data->fifosize[probe_index];
1246
Ben Dooksb4975492008-07-03 12:32:51 +01001247 probe_index++;
1248
1249 dbg("%s: initialising port %p...\n", __func__, ourport);
1250
Thomas Abrahamda121502011-11-02 19:23:25 +09001251 ret = s3c24xx_serial_init_port(ourport, pdev);
Ben Dooksb4975492008-07-03 12:32:51 +01001252 if (ret < 0)
1253 goto probe_err;
1254
1255 dbg("%s: adding port\n", __func__);
1256 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
Thomas Abrahamda121502011-11-02 19:23:25 +09001257 platform_set_drvdata(pdev, &ourport->port);
Ben Dooksb4975492008-07-03 12:32:51 +01001258
Thomas Abrahamda121502011-11-02 19:23:25 +09001259 ret = device_create_file(&pdev->dev, &dev_attr_clock_source);
Ben Dooksb4975492008-07-03 12:32:51 +01001260 if (ret < 0)
Thomas Abrahamda121502011-11-02 19:23:25 +09001261 dev_err(&pdev->dev, "failed to add clock source attr.\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001262
Ben Dooks30555472008-10-21 14:06:36 +01001263 ret = s3c24xx_serial_cpufreq_register(ourport);
1264 if (ret < 0)
Thomas Abrahamda121502011-11-02 19:23:25 +09001265 dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
Ben Dooks30555472008-10-21 14:06:36 +01001266
Ben Dooksb4975492008-07-03 12:32:51 +01001267 return 0;
1268
1269 probe_err:
1270 return ret;
1271}
1272
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001273static int s3c24xx_serial_remove(struct platform_device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01001274{
1275 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1276
1277 if (port) {
Ben Dooks30555472008-10-21 14:06:36 +01001278 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
Ben Dooksb4975492008-07-03 12:32:51 +01001279 device_remove_file(&dev->dev, &dev_attr_clock_source);
1280 uart_remove_one_port(&s3c24xx_uart_drv, port);
1281 }
1282
1283 return 0;
1284}
1285
Ben Dooksb4975492008-07-03 12:32:51 +01001286/* UART power management code */
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001287#ifdef CONFIG_PM_SLEEP
1288static int s3c24xx_serial_suspend(struct device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01001289{
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001290 struct uart_port *port = s3c24xx_dev_to_port(dev);
Ben Dooksb4975492008-07-03 12:32:51 +01001291
1292 if (port)
1293 uart_suspend_port(&s3c24xx_uart_drv, port);
1294
1295 return 0;
1296}
1297
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001298static int s3c24xx_serial_resume(struct device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01001299{
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001300 struct uart_port *port = s3c24xx_dev_to_port(dev);
Ben Dooksb4975492008-07-03 12:32:51 +01001301 struct s3c24xx_uart_port *ourport = to_ourport(port);
1302
1303 if (port) {
Thomas Abraham9484b002012-10-03 07:40:04 +09001304 clk_prepare_enable(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +01001305 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
Thomas Abraham9484b002012-10-03 07:40:04 +09001306 clk_disable_unprepare(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +01001307
1308 uart_resume_port(&s3c24xx_uart_drv, port);
1309 }
1310
1311 return 0;
1312}
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001313
Michael Spangd09a7302013-03-27 19:34:24 -04001314static int s3c24xx_serial_resume_noirq(struct device *dev)
1315{
1316 struct uart_port *port = s3c24xx_dev_to_port(dev);
1317
1318 if (port) {
1319 /* restore IRQ mask */
1320 if (s3c24xx_serial_has_interrupt_mask(port)) {
1321 unsigned int uintm = 0xf;
1322 if (tx_enabled(port))
1323 uintm &= ~S3C64XX_UINTM_TXD_MSK;
1324 if (rx_enabled(port))
1325 uintm &= ~S3C64XX_UINTM_RXD_MSK;
1326 wr_regl(port, S3C64XX_UINTM, uintm);
1327 }
1328 }
1329
1330 return 0;
1331}
1332
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001333static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
1334 .suspend = s3c24xx_serial_suspend,
1335 .resume = s3c24xx_serial_resume,
Michael Spangd09a7302013-03-27 19:34:24 -04001336 .resume_noirq = s3c24xx_serial_resume_noirq,
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001337};
Kukjin Kimb882fc12011-07-28 08:50:38 +09001338#define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
1339
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001340#else /* !CONFIG_PM_SLEEP */
Kukjin Kimb882fc12011-07-28 08:50:38 +09001341
1342#define SERIAL_SAMSUNG_PM_OPS NULL
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001343#endif /* CONFIG_PM_SLEEP */
Ben Dooksb4975492008-07-03 12:32:51 +01001344
Ben Dooksb4975492008-07-03 12:32:51 +01001345/* Console code */
1346
1347#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1348
1349static struct uart_port *cons_uart;
1350
1351static int
1352s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1353{
1354 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1355 unsigned long ufstat, utrstat;
1356
1357 if (ufcon & S3C2410_UFCON_FIFOMODE) {
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001358 /* fifo mode - check amount of data in fifo registers... */
Ben Dooksb4975492008-07-03 12:32:51 +01001359
1360 ufstat = rd_regl(port, S3C2410_UFSTAT);
1361 return (ufstat & info->tx_fifofull) ? 0 : 1;
1362 }
1363
1364 /* in non-fifo mode, we go and use the tx buffer empty */
1365
1366 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1367 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1368}
1369
Michael Spang38adbc52013-03-27 19:34:25 -04001370static bool
1371s3c24xx_port_configured(unsigned int ucon)
1372{
1373 /* consider the serial port configured if the tx/rx mode set */
1374 return (ucon & 0xf) != 0;
1375}
1376
Julien Pichon93b5c032012-09-21 23:22:31 -07001377#ifdef CONFIG_CONSOLE_POLL
1378/*
1379 * Console polling routines for writing and reading from the uart while
1380 * in an interrupt or debug context.
1381 */
1382
1383static int s3c24xx_serial_get_poll_char(struct uart_port *port)
1384{
1385 struct s3c24xx_uart_port *ourport = to_ourport(port);
1386 unsigned int ufstat;
1387
1388 ufstat = rd_regl(port, S3C2410_UFSTAT);
1389 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
1390 return NO_POLL_CHAR;
1391
1392 return rd_regb(port, S3C2410_URXH);
1393}
1394
1395static void s3c24xx_serial_put_poll_char(struct uart_port *port,
1396 unsigned char c)
1397{
1398 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
Michael Spang38adbc52013-03-27 19:34:25 -04001399 unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON);
1400
1401 /* not possible to xmit on unconfigured port */
1402 if (!s3c24xx_port_configured(ucon))
1403 return;
Julien Pichon93b5c032012-09-21 23:22:31 -07001404
1405 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1406 cpu_relax();
1407 wr_regb(cons_uart, S3C2410_UTXH, c);
1408}
1409
1410#endif /* CONFIG_CONSOLE_POLL */
1411
Ben Dooksb4975492008-07-03 12:32:51 +01001412static void
1413s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
1414{
1415 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
Michael Spang38adbc52013-03-27 19:34:25 -04001416 unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON);
1417
1418 /* not possible to xmit on unconfigured port */
1419 if (!s3c24xx_port_configured(ucon))
1420 return;
1421
Ben Dooksb4975492008-07-03 12:32:51 +01001422 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1423 barrier();
1424 wr_regb(cons_uart, S3C2410_UTXH, ch);
1425}
1426
1427static void
1428s3c24xx_serial_console_write(struct console *co, const char *s,
1429 unsigned int count)
1430{
1431 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
1432}
1433
1434static void __init
1435s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1436 int *parity, int *bits)
1437{
Ben Dooksb4975492008-07-03 12:32:51 +01001438 struct clk *clk;
1439 unsigned int ulcon;
1440 unsigned int ucon;
1441 unsigned int ubrdiv;
1442 unsigned long rate;
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001443 unsigned int clk_sel;
1444 char clk_name[MAX_CLK_NAME_LENGTH];
Ben Dooksb4975492008-07-03 12:32:51 +01001445
1446 ulcon = rd_regl(port, S3C2410_ULCON);
1447 ucon = rd_regl(port, S3C2410_UCON);
1448 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1449
1450 dbg("s3c24xx_serial_get_options: port=%p\n"
1451 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1452 port, ulcon, ucon, ubrdiv);
1453
Michael Spang38adbc52013-03-27 19:34:25 -04001454 if (s3c24xx_port_configured(ucon)) {
Ben Dooksb4975492008-07-03 12:32:51 +01001455 switch (ulcon & S3C2410_LCON_CSMASK) {
1456 case S3C2410_LCON_CS5:
1457 *bits = 5;
1458 break;
1459 case S3C2410_LCON_CS6:
1460 *bits = 6;
1461 break;
1462 case S3C2410_LCON_CS7:
1463 *bits = 7;
1464 break;
1465 default:
1466 case S3C2410_LCON_CS8:
1467 *bits = 8;
1468 break;
1469 }
1470
1471 switch (ulcon & S3C2410_LCON_PMASK) {
1472 case S3C2410_LCON_PEVEN:
1473 *parity = 'e';
1474 break;
1475
1476 case S3C2410_LCON_PODD:
1477 *parity = 'o';
1478 break;
1479
1480 case S3C2410_LCON_PNONE:
1481 default:
1482 *parity = 'n';
1483 }
1484
1485 /* now calculate the baud rate */
1486
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001487 clk_sel = s3c24xx_serial_getsource(port);
1488 sprintf(clk_name, "clk_uart_baud%d", clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +01001489
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001490 clk = clk_get(port->dev, clk_name);
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001491 if (!IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001492 rate = clk_get_rate(clk);
Ben Dooksb4975492008-07-03 12:32:51 +01001493 else
1494 rate = 1;
1495
Ben Dooksb4975492008-07-03 12:32:51 +01001496 *baud = rate / (16 * (ubrdiv + 1));
1497 dbg("calculated baud %d\n", *baud);
1498 }
1499
1500}
1501
Ben Dooksb4975492008-07-03 12:32:51 +01001502static int __init
1503s3c24xx_serial_console_setup(struct console *co, char *options)
1504{
1505 struct uart_port *port;
1506 int baud = 9600;
1507 int bits = 8;
1508 int parity = 'n';
1509 int flow = 'n';
1510
1511 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1512 co, co->index, options);
1513
1514 /* is this a valid port */
1515
Ben Dooks03d5e772008-11-03 09:21:23 +00001516 if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
Ben Dooksb4975492008-07-03 12:32:51 +01001517 co->index = 0;
1518
1519 port = &s3c24xx_serial_ports[co->index].port;
1520
1521 /* is the port configured? */
1522
Thomas Abrahamee430f12011-06-14 19:12:26 +09001523 if (port->mapbase == 0x0)
1524 return -ENODEV;
Ben Dooksb4975492008-07-03 12:32:51 +01001525
1526 cons_uart = port;
1527
1528 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1529
1530 /*
1531 * Check whether an invalid uart number has been specified, and
1532 * if so, search for the first available port that does have
1533 * console support.
1534 */
1535 if (options)
1536 uart_parse_options(options, &baud, &parity, &bits, &flow);
1537 else
1538 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1539
1540 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1541
1542 return uart_set_options(port, co, baud, parity, bits, flow);
1543}
1544
Ben Dooksb4975492008-07-03 12:32:51 +01001545static struct console s3c24xx_serial_console = {
1546 .name = S3C24XX_SERIAL_NAME,
1547 .device = uart_console_device,
1548 .flags = CON_PRINTBUFFER,
1549 .index = -1,
1550 .write = s3c24xx_serial_console_write,
Thomas Abraham5822a5d2011-06-14 19:12:26 +09001551 .setup = s3c24xx_serial_console_setup,
1552 .data = &s3c24xx_uart_drv,
Ben Dooksb4975492008-07-03 12:32:51 +01001553};
Ben Dooksb4975492008-07-03 12:32:51 +01001554#endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
1555
Thomas Abrahamda121502011-11-02 19:23:25 +09001556#ifdef CONFIG_CPU_S3C2410
1557static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
1558 .info = &(struct s3c24xx_uart_info) {
1559 .name = "Samsung S3C2410 UART",
1560 .type = PORT_S3C2410,
1561 .fifosize = 16,
1562 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
1563 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
1564 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
1565 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
1566 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
1567 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
1568 .def_clk_sel = S3C2410_UCON_CLKSEL0,
1569 .num_clks = 2,
1570 .clksel_mask = S3C2410_UCON_CLKMASK,
1571 .clksel_shift = S3C2410_UCON_CLKSHIFT,
1572 },
1573 .def_cfg = &(struct s3c2410_uartcfg) {
1574 .ucon = S3C2410_UCON_DEFAULT,
1575 .ufcon = S3C2410_UFCON_DEFAULT,
1576 },
1577};
1578#define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
1579#else
1580#define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1581#endif
1582
1583#ifdef CONFIG_CPU_S3C2412
1584static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
1585 .info = &(struct s3c24xx_uart_info) {
1586 .name = "Samsung S3C2412 UART",
1587 .type = PORT_S3C2412,
1588 .fifosize = 64,
1589 .has_divslot = 1,
1590 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1591 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1592 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1593 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1594 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1595 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1596 .def_clk_sel = S3C2410_UCON_CLKSEL2,
1597 .num_clks = 4,
1598 .clksel_mask = S3C2412_UCON_CLKMASK,
1599 .clksel_shift = S3C2412_UCON_CLKSHIFT,
1600 },
1601 .def_cfg = &(struct s3c2410_uartcfg) {
1602 .ucon = S3C2410_UCON_DEFAULT,
1603 .ufcon = S3C2410_UFCON_DEFAULT,
1604 },
1605};
1606#define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
1607#else
1608#define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1609#endif
1610
1611#if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
Denis 'GNUtoo' Cariklib26469a2012-02-23 08:23:52 +01001612 defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
Thomas Abrahamda121502011-11-02 19:23:25 +09001613static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
1614 .info = &(struct s3c24xx_uart_info) {
1615 .name = "Samsung S3C2440 UART",
1616 .type = PORT_S3C2440,
1617 .fifosize = 64,
1618 .has_divslot = 1,
1619 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1620 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1621 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1622 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1623 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1624 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1625 .def_clk_sel = S3C2410_UCON_CLKSEL2,
1626 .num_clks = 4,
1627 .clksel_mask = S3C2412_UCON_CLKMASK,
1628 .clksel_shift = S3C2412_UCON_CLKSHIFT,
1629 },
1630 .def_cfg = &(struct s3c2410_uartcfg) {
1631 .ucon = S3C2410_UCON_DEFAULT,
1632 .ufcon = S3C2410_UFCON_DEFAULT,
1633 },
1634};
1635#define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
1636#else
1637#define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1638#endif
1639
1640#if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410) || \
1641 defined(CONFIG_CPU_S5P6440) || defined(CONFIG_CPU_S5P6450) || \
1642 defined(CONFIG_CPU_S5PC100)
1643static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
1644 .info = &(struct s3c24xx_uart_info) {
1645 .name = "Samsung S3C6400 UART",
1646 .type = PORT_S3C6400,
1647 .fifosize = 64,
1648 .has_divslot = 1,
1649 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1650 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1651 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1652 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1653 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1654 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1655 .def_clk_sel = S3C2410_UCON_CLKSEL2,
1656 .num_clks = 4,
1657 .clksel_mask = S3C6400_UCON_CLKMASK,
1658 .clksel_shift = S3C6400_UCON_CLKSHIFT,
1659 },
1660 .def_cfg = &(struct s3c2410_uartcfg) {
1661 .ucon = S3C2410_UCON_DEFAULT,
1662 .ufcon = S3C2410_UFCON_DEFAULT,
1663 },
1664};
1665#define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
1666#else
1667#define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1668#endif
1669
1670#ifdef CONFIG_CPU_S5PV210
1671static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
1672 .info = &(struct s3c24xx_uart_info) {
1673 .name = "Samsung S5PV210 UART",
1674 .type = PORT_S3C6400,
1675 .has_divslot = 1,
1676 .rx_fifomask = S5PV210_UFSTAT_RXMASK,
1677 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
1678 .rx_fifofull = S5PV210_UFSTAT_RXFULL,
1679 .tx_fifofull = S5PV210_UFSTAT_TXFULL,
1680 .tx_fifomask = S5PV210_UFSTAT_TXMASK,
1681 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
1682 .def_clk_sel = S3C2410_UCON_CLKSEL0,
1683 .num_clks = 2,
1684 .clksel_mask = S5PV210_UCON_CLKMASK,
1685 .clksel_shift = S5PV210_UCON_CLKSHIFT,
1686 },
1687 .def_cfg = &(struct s3c2410_uartcfg) {
1688 .ucon = S5PV210_UCON_DEFAULT,
1689 .ufcon = S5PV210_UFCON_DEFAULT,
1690 },
1691 .fifosize = { 256, 64, 16, 16 },
1692};
1693#define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
1694#else
1695#define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1696#endif
1697
Kukjin Kim5f7b6d12012-02-01 00:11:23 +09001698#if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212) || \
Kukjin Kim2edb36c2012-11-15 15:48:56 +09001699 defined(CONFIG_SOC_EXYNOS4412) || defined(CONFIG_SOC_EXYNOS5250) || \
1700 defined(CONFIG_SOC_EXYNOS5440)
Thomas Abrahamda121502011-11-02 19:23:25 +09001701static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
1702 .info = &(struct s3c24xx_uart_info) {
1703 .name = "Samsung Exynos4 UART",
1704 .type = PORT_S3C6400,
1705 .has_divslot = 1,
1706 .rx_fifomask = S5PV210_UFSTAT_RXMASK,
1707 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
1708 .rx_fifofull = S5PV210_UFSTAT_RXFULL,
1709 .tx_fifofull = S5PV210_UFSTAT_TXFULL,
1710 .tx_fifomask = S5PV210_UFSTAT_TXMASK,
1711 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
1712 .def_clk_sel = S3C2410_UCON_CLKSEL0,
1713 .num_clks = 1,
1714 .clksel_mask = 0,
1715 .clksel_shift = 0,
1716 },
1717 .def_cfg = &(struct s3c2410_uartcfg) {
1718 .ucon = S5PV210_UCON_DEFAULT,
1719 .ufcon = S5PV210_UFCON_DEFAULT,
1720 .has_fracval = 1,
1721 },
1722 .fifosize = { 256, 64, 16, 16 },
1723};
1724#define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
1725#else
1726#define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1727#endif
1728
1729static struct platform_device_id s3c24xx_serial_driver_ids[] = {
1730 {
1731 .name = "s3c2410-uart",
1732 .driver_data = S3C2410_SERIAL_DRV_DATA,
1733 }, {
1734 .name = "s3c2412-uart",
1735 .driver_data = S3C2412_SERIAL_DRV_DATA,
1736 }, {
1737 .name = "s3c2440-uart",
1738 .driver_data = S3C2440_SERIAL_DRV_DATA,
1739 }, {
1740 .name = "s3c6400-uart",
1741 .driver_data = S3C6400_SERIAL_DRV_DATA,
1742 }, {
1743 .name = "s5pv210-uart",
1744 .driver_data = S5PV210_SERIAL_DRV_DATA,
1745 }, {
1746 .name = "exynos4210-uart",
1747 .driver_data = EXYNOS4210_SERIAL_DRV_DATA,
1748 },
1749 { },
1750};
1751MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
1752
Thomas Abraham26c919e2011-11-06 22:10:44 +05301753#ifdef CONFIG_OF
1754static const struct of_device_id s3c24xx_uart_dt_match[] = {
Heiko Stübner666ca0b2012-11-22 11:37:44 +01001755 { .compatible = "samsung,s3c2410-uart",
1756 .data = (void *)S3C2410_SERIAL_DRV_DATA },
1757 { .compatible = "samsung,s3c2412-uart",
1758 .data = (void *)S3C2412_SERIAL_DRV_DATA },
1759 { .compatible = "samsung,s3c2440-uart",
1760 .data = (void *)S3C2440_SERIAL_DRV_DATA },
1761 { .compatible = "samsung,s3c6400-uart",
1762 .data = (void *)S3C6400_SERIAL_DRV_DATA },
1763 { .compatible = "samsung,s5pv210-uart",
1764 .data = (void *)S5PV210_SERIAL_DRV_DATA },
Thomas Abraham26c919e2011-11-06 22:10:44 +05301765 { .compatible = "samsung,exynos4210-uart",
Mark Browna169a882011-11-08 17:00:14 +09001766 .data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
Thomas Abraham26c919e2011-11-06 22:10:44 +05301767 {},
1768};
1769MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
Thomas Abraham26c919e2011-11-06 22:10:44 +05301770#endif
1771
Thomas Abrahamda121502011-11-02 19:23:25 +09001772static struct platform_driver samsung_serial_driver = {
1773 .probe = s3c24xx_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001774 .remove = s3c24xx_serial_remove,
Thomas Abrahamda121502011-11-02 19:23:25 +09001775 .id_table = s3c24xx_serial_driver_ids,
1776 .driver = {
1777 .name = "samsung-uart",
1778 .owner = THIS_MODULE,
1779 .pm = SERIAL_SAMSUNG_PM_OPS,
Sachin Kamat905f4ba2013-01-07 09:50:42 +05301780 .of_match_table = of_match_ptr(s3c24xx_uart_dt_match),
Thomas Abrahamda121502011-11-02 19:23:25 +09001781 },
1782};
1783
1784/* module initialisation code */
1785
1786static int __init s3c24xx_serial_modinit(void)
1787{
1788 int ret;
1789
1790 ret = uart_register_driver(&s3c24xx_uart_drv);
1791 if (ret < 0) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301792 pr_err("Failed to register Samsung UART driver\n");
Sachin Kamate740d8f2012-09-12 12:00:01 +05301793 return ret;
Thomas Abrahamda121502011-11-02 19:23:25 +09001794 }
1795
1796 return platform_driver_register(&samsung_serial_driver);
1797}
1798
1799static void __exit s3c24xx_serial_modexit(void)
1800{
1801 uart_unregister_driver(&s3c24xx_uart_drv);
1802}
1803
1804module_init(s3c24xx_serial_modinit);
1805module_exit(s3c24xx_serial_modexit);
1806
1807MODULE_ALIAS("platform:samsung-uart");
Ben Dooksb4975492008-07-03 12:32:51 +01001808MODULE_DESCRIPTION("Samsung SoC Serial port driver");
1809MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1810MODULE_LICENSE("GPL v2");