blob: e49a9451976e34e015af0eaca6235324d9b2cd6b [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>
Arnd Bergmann9ee51f02013-04-11 02:04:48 +020042#include <linux/serial_s3c.h>
Ben Dooksb4975492008-07-03 12:32:51 +010043#include <linux/delay.h>
44#include <linux/clk.h>
Ben Dooks30555472008-10-21 14:06:36 +010045#include <linux/cpufreq.h>
Thomas Abraham26c919e2011-11-06 22:10:44 +053046#include <linux/of.h>
Ben Dooksb4975492008-07-03 12:32:51 +010047
48#include <asm/irq.h>
49
Arnd Bergmann9ee51f02013-04-11 02:04:48 +020050#ifdef CONFIG_SAMSUNG_CLOCK
Thomas Abraham5f5a7a52011-10-24 11:47:46 +020051#include <plat/clock.h>
Arnd Bergmann9ee51f02013-04-11 02:04:48 +020052#endif
Ben Dooksb4975492008-07-03 12:32:51 +010053
54#include "samsung.h"
55
Joe Perchese4ac92d2014-05-20 14:05:50 -070056#if defined(CONFIG_SERIAL_SAMSUNG_DEBUG) && \
57 defined(CONFIG_DEBUG_LL) && \
58 !defined(MODULE)
59
60extern void printascii(const char *);
61
62__printf(1, 2)
63static void dbg(const char *fmt, ...)
64{
65 va_list va;
66 char buff[256];
67
68 va_start(va, fmt);
Sachin Kamata859c8b2014-06-03 11:56:25 +053069 vscnprintf(buff, sizeof(buff), fmt, va);
Joe Perchese4ac92d2014-05-20 14:05:50 -070070 va_end(va);
71
72 printascii(buff);
73}
74
75#else
76#define dbg(fmt, ...) do { if (0) no_printk(fmt, ##__VA_ARGS__); } while (0)
77#endif
78
Ben Dooksb4975492008-07-03 12:32:51 +010079/* UART name and device definitions */
80
81#define S3C24XX_SERIAL_NAME "ttySAC"
82#define S3C24XX_SERIAL_MAJOR 204
83#define S3C24XX_SERIAL_MINOR 64
84
Ben Dooksb4975492008-07-03 12:32:51 +010085/* macros to change one thing to another */
86
87#define tx_enabled(port) ((port)->unused[0])
88#define rx_enabled(port) ((port)->unused[1])
89
Lucas De Marchi25985ed2011-03-30 22:57:33 -030090/* flag to ignore all characters coming in */
Ben Dooksb4975492008-07-03 12:32:51 +010091#define RXSTAT_DUMMY_READ (0x10000000)
92
93static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
94{
95 return container_of(port, struct s3c24xx_uart_port, port);
96}
97
98/* translate a port to the device name */
99
100static inline const char *s3c24xx_serial_portname(struct uart_port *port)
101{
102 return to_platform_device(port->dev)->name;
103}
104
105static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
106{
Sachin Kamat9303ac12012-09-05 10:30:11 +0530107 return rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE;
Ben Dooksb4975492008-07-03 12:32:51 +0100108}
109
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530110/*
111 * s3c64xx and later SoC's include the interrupt mask and status registers in
112 * the controller itself, unlike the s3c24xx SoC's which have these registers
113 * in the interrupt controller. Check if the port type is s3c64xx or higher.
114 */
115static int s3c24xx_serial_has_interrupt_mask(struct uart_port *port)
116{
117 return to_ourport(port)->info->type == PORT_S3C6400;
118}
119
Ben Dooksb4975492008-07-03 12:32:51 +0100120static void s3c24xx_serial_rx_enable(struct uart_port *port)
121{
122 unsigned long flags;
123 unsigned int ucon, ufcon;
124 int count = 10000;
125
126 spin_lock_irqsave(&port->lock, flags);
127
128 while (--count && !s3c24xx_serial_txempty_nofifo(port))
129 udelay(100);
130
131 ufcon = rd_regl(port, S3C2410_UFCON);
132 ufcon |= S3C2410_UFCON_RESETRX;
133 wr_regl(port, S3C2410_UFCON, ufcon);
134
135 ucon = rd_regl(port, S3C2410_UCON);
136 ucon |= S3C2410_UCON_RXIRQMODE;
137 wr_regl(port, S3C2410_UCON, ucon);
138
139 rx_enabled(port) = 1;
140 spin_unlock_irqrestore(&port->lock, flags);
141}
142
143static void s3c24xx_serial_rx_disable(struct uart_port *port)
144{
145 unsigned long flags;
146 unsigned int ucon;
147
148 spin_lock_irqsave(&port->lock, flags);
149
150 ucon = rd_regl(port, S3C2410_UCON);
151 ucon &= ~S3C2410_UCON_RXIRQMODE;
152 wr_regl(port, S3C2410_UCON, ucon);
153
154 rx_enabled(port) = 0;
155 spin_unlock_irqrestore(&port->lock, flags);
156}
157
158static void s3c24xx_serial_stop_tx(struct uart_port *port)
159{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100160 struct s3c24xx_uart_port *ourport = to_ourport(port);
161
Ben Dooksb4975492008-07-03 12:32:51 +0100162 if (tx_enabled(port)) {
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530163 if (s3c24xx_serial_has_interrupt_mask(port))
164 __set_bit(S3C64XX_UINTM_TXD,
165 portaddrl(port, S3C64XX_UINTM));
166 else
167 disable_irq_nosync(ourport->tx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100168 tx_enabled(port) = 0;
169 if (port->flags & UPF_CONS_FLOW)
170 s3c24xx_serial_rx_enable(port);
171 }
172}
173
174static void s3c24xx_serial_start_tx(struct uart_port *port)
175{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100176 struct s3c24xx_uart_port *ourport = to_ourport(port);
177
Ben Dooksb4975492008-07-03 12:32:51 +0100178 if (!tx_enabled(port)) {
179 if (port->flags & UPF_CONS_FLOW)
180 s3c24xx_serial_rx_disable(port);
181
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530182 if (s3c24xx_serial_has_interrupt_mask(port))
183 __clear_bit(S3C64XX_UINTM_TXD,
184 portaddrl(port, S3C64XX_UINTM));
185 else
186 enable_irq(ourport->tx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100187 tx_enabled(port) = 1;
188 }
189}
190
Ben Dooksb4975492008-07-03 12:32:51 +0100191static void s3c24xx_serial_stop_rx(struct uart_port *port)
192{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100193 struct s3c24xx_uart_port *ourport = to_ourport(port);
194
Ben Dooksb4975492008-07-03 12:32:51 +0100195 if (rx_enabled(port)) {
196 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530197 if (s3c24xx_serial_has_interrupt_mask(port))
198 __set_bit(S3C64XX_UINTM_RXD,
199 portaddrl(port, S3C64XX_UINTM));
200 else
201 disable_irq_nosync(ourport->rx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100202 rx_enabled(port) = 0;
203 }
204}
205
Ben Dooksb4975492008-07-03 12:32:51 +0100206static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
207{
208 return to_ourport(port)->info;
209}
210
211static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
212{
Thomas Abraham4d84e972011-10-24 11:47:25 +0200213 struct s3c24xx_uart_port *ourport;
214
Ben Dooksb4975492008-07-03 12:32:51 +0100215 if (port->dev == NULL)
216 return NULL;
217
Thomas Abraham4d84e972011-10-24 11:47:25 +0200218 ourport = container_of(port, struct s3c24xx_uart_port, port);
219 return ourport->cfg;
Ben Dooksb4975492008-07-03 12:32:51 +0100220}
221
222static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
223 unsigned long ufstat)
224{
225 struct s3c24xx_uart_info *info = ourport->info;
226
227 if (ufstat & info->rx_fifofull)
Thomas Abrahamda121502011-11-02 19:23:25 +0900228 return ourport->port.fifosize;
Ben Dooksb4975492008-07-03 12:32:51 +0100229
230 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
231}
232
233
234/* ? - where has parity gone?? */
235#define S3C2410_UERSTAT_PARITY (0x1000)
236
237static irqreturn_t
238s3c24xx_serial_rx_chars(int irq, void *dev_id)
239{
240 struct s3c24xx_uart_port *ourport = dev_id;
241 struct uart_port *port = &ourport->port;
Ben Dooksb4975492008-07-03 12:32:51 +0100242 unsigned int ufcon, ch, flag, ufstat, uerstat;
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530243 unsigned long flags;
Ben Dooksb4975492008-07-03 12:32:51 +0100244 int max_count = 64;
245
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530246 spin_lock_irqsave(&port->lock, flags);
247
Ben Dooksb4975492008-07-03 12:32:51 +0100248 while (max_count-- > 0) {
249 ufcon = rd_regl(port, S3C2410_UFCON);
250 ufstat = rd_regl(port, S3C2410_UFSTAT);
251
252 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
253 break;
254
255 uerstat = rd_regl(port, S3C2410_UERSTAT);
256 ch = rd_regb(port, S3C2410_URXH);
257
258 if (port->flags & UPF_CONS_FLOW) {
259 int txe = s3c24xx_serial_txempty_nofifo(port);
260
261 if (rx_enabled(port)) {
262 if (!txe) {
263 rx_enabled(port) = 0;
264 continue;
265 }
266 } else {
267 if (txe) {
268 ufcon |= S3C2410_UFCON_RESETRX;
269 wr_regl(port, S3C2410_UFCON, ufcon);
270 rx_enabled(port) = 1;
Viresh Kumarf5693ea2013-08-19 20:14:26 +0530271 spin_unlock_irqrestore(&port->lock,
272 flags);
Ben Dooksb4975492008-07-03 12:32:51 +0100273 goto out;
274 }
275 continue;
276 }
277 }
278
279 /* insert the character into the buffer */
280
281 flag = TTY_NORMAL;
282 port->icount.rx++;
283
284 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
285 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
286 ch, uerstat);
287
288 /* check for break */
289 if (uerstat & S3C2410_UERSTAT_BREAK) {
290 dbg("break!\n");
291 port->icount.brk++;
292 if (uart_handle_break(port))
Sachin Kamat9303ac12012-09-05 10:30:11 +0530293 goto ignore_char;
Ben Dooksb4975492008-07-03 12:32:51 +0100294 }
295
296 if (uerstat & S3C2410_UERSTAT_FRAME)
297 port->icount.frame++;
298 if (uerstat & S3C2410_UERSTAT_OVERRUN)
299 port->icount.overrun++;
300
301 uerstat &= port->read_status_mask;
302
303 if (uerstat & S3C2410_UERSTAT_BREAK)
304 flag = TTY_BREAK;
305 else if (uerstat & S3C2410_UERSTAT_PARITY)
306 flag = TTY_PARITY;
307 else if (uerstat & (S3C2410_UERSTAT_FRAME |
308 S3C2410_UERSTAT_OVERRUN))
309 flag = TTY_FRAME;
310 }
311
312 if (uart_handle_sysrq_char(port, ch))
313 goto ignore_char;
314
315 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
316 ch, flag);
317
318 ignore_char:
319 continue;
320 }
Viresh Kumarf5693ea2013-08-19 20:14:26 +0530321
322 spin_unlock_irqrestore(&port->lock, flags);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100323 tty_flip_buffer_push(&port->state->port);
Ben Dooksb4975492008-07-03 12:32:51 +0100324
325 out:
326 return IRQ_HANDLED;
327}
328
329static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
330{
331 struct s3c24xx_uart_port *ourport = id;
332 struct uart_port *port = &ourport->port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700333 struct circ_buf *xmit = &port->state->xmit;
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530334 unsigned long flags;
Ben Dooksb4975492008-07-03 12:32:51 +0100335 int count = 256;
336
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530337 spin_lock_irqsave(&port->lock, flags);
338
Ben Dooksb4975492008-07-03 12:32:51 +0100339 if (port->x_char) {
340 wr_regb(port, S3C2410_UTXH, port->x_char);
341 port->icount.tx++;
342 port->x_char = 0;
343 goto out;
344 }
345
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300346 /* if there isn't anything more to transmit, or the uart is now
Ben Dooksb4975492008-07-03 12:32:51 +0100347 * stopped, disable the uart and exit
348 */
349
350 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
351 s3c24xx_serial_stop_tx(port);
352 goto out;
353 }
354
355 /* try and drain the buffer... */
356
357 while (!uart_circ_empty(xmit) && count-- > 0) {
358 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
359 break;
360
361 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
362 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
363 port->icount.tx++;
364 }
365
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530366 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
367 spin_unlock(&port->lock);
Ben Dooksb4975492008-07-03 12:32:51 +0100368 uart_write_wakeup(port);
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530369 spin_lock(&port->lock);
370 }
Ben Dooksb4975492008-07-03 12:32:51 +0100371
372 if (uart_circ_empty(xmit))
373 s3c24xx_serial_stop_tx(port);
374
375 out:
Thomas Abrahamc15c3742012-11-22 18:06:28 +0530376 spin_unlock_irqrestore(&port->lock, flags);
Ben Dooksb4975492008-07-03 12:32:51 +0100377 return IRQ_HANDLED;
378}
379
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530380/* interrupt handler for s3c64xx and later SoC's.*/
381static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
382{
383 struct s3c24xx_uart_port *ourport = id;
384 struct uart_port *port = &ourport->port;
385 unsigned int pend = rd_regl(port, S3C64XX_UINTP);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530386 irqreturn_t ret = IRQ_HANDLED;
387
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530388 if (pend & S3C64XX_UINTM_RXD_MSK) {
389 ret = s3c24xx_serial_rx_chars(irq, id);
390 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
391 }
392 if (pend & S3C64XX_UINTM_TXD_MSK) {
393 ret = s3c24xx_serial_tx_chars(irq, id);
394 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
395 }
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530396 return ret;
397}
398
Ben Dooksb4975492008-07-03 12:32:51 +0100399static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
400{
401 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
402 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
403 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
404
405 if (ufcon & S3C2410_UFCON_FIFOMODE) {
406 if ((ufstat & info->tx_fifomask) != 0 ||
407 (ufstat & info->tx_fifofull))
408 return 0;
409
410 return 1;
411 }
412
413 return s3c24xx_serial_txempty_nofifo(port);
414}
415
416/* no modem control lines */
417static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
418{
419 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
420
421 if (umstat & S3C2410_UMSTAT_CTS)
422 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
423 else
424 return TIOCM_CAR | TIOCM_DSR;
425}
426
427static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
428{
José Miguel Gonçalves2d1e5a42013-09-18 16:52:49 +0100429 unsigned int umcon = rd_regl(port, S3C2410_UMCON);
430
431 if (mctrl & TIOCM_RTS)
432 umcon |= S3C2410_UMCOM_RTS_LOW;
433 else
434 umcon &= ~S3C2410_UMCOM_RTS_LOW;
435
436 wr_regl(port, S3C2410_UMCON, umcon);
Ben Dooksb4975492008-07-03 12:32:51 +0100437}
438
439static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
440{
441 unsigned long flags;
442 unsigned int ucon;
443
444 spin_lock_irqsave(&port->lock, flags);
445
446 ucon = rd_regl(port, S3C2410_UCON);
447
448 if (break_state)
449 ucon |= S3C2410_UCON_SBREAK;
450 else
451 ucon &= ~S3C2410_UCON_SBREAK;
452
453 wr_regl(port, S3C2410_UCON, ucon);
454
455 spin_unlock_irqrestore(&port->lock, flags);
456}
457
458static void s3c24xx_serial_shutdown(struct uart_port *port)
459{
460 struct s3c24xx_uart_port *ourport = to_ourport(port);
461
462 if (ourport->tx_claimed) {
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530463 if (!s3c24xx_serial_has_interrupt_mask(port))
464 free_irq(ourport->tx_irq, ourport);
Ben Dooksb4975492008-07-03 12:32:51 +0100465 tx_enabled(port) = 0;
466 ourport->tx_claimed = 0;
467 }
468
469 if (ourport->rx_claimed) {
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530470 if (!s3c24xx_serial_has_interrupt_mask(port))
471 free_irq(ourport->rx_irq, ourport);
Ben Dooksb4975492008-07-03 12:32:51 +0100472 ourport->rx_claimed = 0;
473 rx_enabled(port) = 0;
474 }
Ben Dooksb4975492008-07-03 12:32:51 +0100475
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530476 /* Clear pending interrupts and mask all interrupts */
477 if (s3c24xx_serial_has_interrupt_mask(port)) {
Tomasz Figab6ad2932013-03-26 15:57:35 +0100478 free_irq(port->irq, ourport);
479
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530480 wr_regl(port, S3C64XX_UINTP, 0xf);
481 wr_regl(port, S3C64XX_UINTM, 0xf);
482 }
483}
Ben Dooksb4975492008-07-03 12:32:51 +0100484
485static int s3c24xx_serial_startup(struct uart_port *port)
486{
487 struct s3c24xx_uart_port *ourport = to_ourport(port);
488 int ret;
489
Joe Perchese4ac92d2014-05-20 14:05:50 -0700490 dbg("s3c24xx_serial_startup: port=%p (%08llx,%p)\n",
491 port, (unsigned long long)port->mapbase, port->membase);
Ben Dooksb4975492008-07-03 12:32:51 +0100492
493 rx_enabled(port) = 1;
494
Ben Dooksb73c289c2008-10-21 14:07:04 +0100495 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
Ben Dooksb4975492008-07-03 12:32:51 +0100496 s3c24xx_serial_portname(port), ourport);
497
498 if (ret != 0) {
Sachin Kamatd20925e2012-09-05 10:30:10 +0530499 dev_err(port->dev, "cannot get irq %d\n", ourport->rx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100500 return ret;
501 }
502
503 ourport->rx_claimed = 1;
504
505 dbg("requesting tx irq...\n");
506
507 tx_enabled(port) = 1;
508
Ben Dooksb73c289c2008-10-21 14:07:04 +0100509 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
Ben Dooksb4975492008-07-03 12:32:51 +0100510 s3c24xx_serial_portname(port), ourport);
511
512 if (ret) {
Sachin Kamatd20925e2012-09-05 10:30:10 +0530513 dev_err(port->dev, "cannot get irq %d\n", ourport->tx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100514 goto err;
515 }
516
517 ourport->tx_claimed = 1;
518
519 dbg("s3c24xx_serial_startup ok\n");
520
521 /* the port reset code should have done the correct
522 * register setup for the port controls */
523
524 return ret;
525
526 err:
527 s3c24xx_serial_shutdown(port);
528 return ret;
529}
530
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530531static int s3c64xx_serial_startup(struct uart_port *port)
532{
533 struct s3c24xx_uart_port *ourport = to_ourport(port);
534 int ret;
535
Joe Perchese4ac92d2014-05-20 14:05:50 -0700536 dbg("s3c64xx_serial_startup: port=%p (%08llx,%p)\n",
537 port, (unsigned long long)port->mapbase, port->membase);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530538
Tomasz Figab6ad2932013-03-26 15:57:35 +0100539 wr_regl(port, S3C64XX_UINTM, 0xf);
540
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530541 ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
542 s3c24xx_serial_portname(port), ourport);
543 if (ret) {
Sachin Kamatd20925e2012-09-05 10:30:10 +0530544 dev_err(port->dev, "cannot get irq %d\n", port->irq);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530545 return ret;
546 }
547
548 /* For compatibility with s3c24xx Soc's */
549 rx_enabled(port) = 1;
550 ourport->rx_claimed = 1;
551 tx_enabled(port) = 0;
552 ourport->tx_claimed = 1;
553
554 /* Enable Rx Interrupt */
555 __clear_bit(S3C64XX_UINTM_RXD, portaddrl(port, S3C64XX_UINTM));
556 dbg("s3c64xx_serial_startup ok\n");
557 return ret;
558}
559
Ben Dooksb4975492008-07-03 12:32:51 +0100560/* power power management control */
561
562static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
563 unsigned int old)
564{
565 struct s3c24xx_uart_port *ourport = to_ourport(port);
566
Ben Dooks30555472008-10-21 14:06:36 +0100567 ourport->pm_level = level;
568
Ben Dooksb4975492008-07-03 12:32:51 +0100569 switch (level) {
570 case 3:
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900571 if (!IS_ERR(ourport->baudclk))
Thomas Abraham9484b002012-10-03 07:40:04 +0900572 clk_disable_unprepare(ourport->baudclk);
Ben Dooksb4975492008-07-03 12:32:51 +0100573
Thomas Abraham9484b002012-10-03 07:40:04 +0900574 clk_disable_unprepare(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +0100575 break;
576
577 case 0:
Thomas Abraham9484b002012-10-03 07:40:04 +0900578 clk_prepare_enable(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +0100579
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900580 if (!IS_ERR(ourport->baudclk))
Thomas Abraham9484b002012-10-03 07:40:04 +0900581 clk_prepare_enable(ourport->baudclk);
Ben Dooksb4975492008-07-03 12:32:51 +0100582
583 break;
584 default:
Sachin Kamatd20925e2012-09-05 10:30:10 +0530585 dev_err(port->dev, "s3c24xx_serial: unknown pm %d\n", level);
Ben Dooksb4975492008-07-03 12:32:51 +0100586 }
587}
588
589/* baud rate calculation
590 *
591 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
592 * of different sources, including the peripheral clock ("pclk") and an
593 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
594 * with a programmable extra divisor.
595 *
596 * The following code goes through the clock sources, and calculates the
597 * baud clocks (and the resultant actual baud rates) and then tries to
598 * pick the closest one and select that.
599 *
600*/
601
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200602#define MAX_CLK_NAME_LENGTH 15
Ben Dooksb4975492008-07-03 12:32:51 +0100603
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200604static inline int s3c24xx_serial_getsource(struct uart_port *port)
Ben Dooksb4975492008-07-03 12:32:51 +0100605{
606 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200607 unsigned int ucon;
Ben Dooksb4975492008-07-03 12:32:51 +0100608
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200609 if (info->num_clks == 1)
Ben Dooksb4975492008-07-03 12:32:51 +0100610 return 0;
611
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200612 ucon = rd_regl(port, S3C2410_UCON);
613 ucon &= info->clksel_mask;
614 return ucon >> info->clksel_shift;
Ben Dooksb4975492008-07-03 12:32:51 +0100615}
616
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200617static void s3c24xx_serial_setsource(struct uart_port *port,
618 unsigned int clk_sel)
Ben Dooksb4975492008-07-03 12:32:51 +0100619{
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200620 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
621 unsigned int ucon;
Ben Dooksb4975492008-07-03 12:32:51 +0100622
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200623 if (info->num_clks == 1)
624 return;
Ben Dooksb4975492008-07-03 12:32:51 +0100625
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200626 ucon = rd_regl(port, S3C2410_UCON);
627 if ((ucon & info->clksel_mask) >> info->clksel_shift == clk_sel)
628 return;
Ben Dooksb4975492008-07-03 12:32:51 +0100629
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200630 ucon &= ~info->clksel_mask;
631 ucon |= clk_sel << info->clksel_shift;
632 wr_regl(port, S3C2410_UCON, ucon);
633}
Ben Dooksb4975492008-07-03 12:32:51 +0100634
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200635static unsigned int s3c24xx_serial_getclk(struct s3c24xx_uart_port *ourport,
636 unsigned int req_baud, struct clk **best_clk,
637 unsigned int *clk_num)
638{
639 struct s3c24xx_uart_info *info = ourport->info;
640 struct clk *clk;
641 unsigned long rate;
642 unsigned int cnt, baud, quot, clk_sel, best_quot = 0;
643 char clkname[MAX_CLK_NAME_LENGTH];
644 int calc_deviation, deviation = (1 << 30) - 1;
645
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200646 clk_sel = (ourport->cfg->clk_sel) ? ourport->cfg->clk_sel :
647 ourport->info->def_clk_sel;
648 for (cnt = 0; cnt < info->num_clks; cnt++) {
649 if (!(clk_sel & (1 << cnt)))
650 continue;
651
652 sprintf(clkname, "clk_uart_baud%d", cnt);
653 clk = clk_get(ourport->port.dev, clkname);
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900654 if (IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200655 continue;
656
657 rate = clk_get_rate(clk);
658 if (!rate)
659 continue;
660
661 if (ourport->info->has_divslot) {
662 unsigned long div = rate / req_baud;
663
664 /* The UDIVSLOT register on the newer UARTs allows us to
665 * get a divisor adjustment of 1/16th on the baud clock.
666 *
667 * We don't keep the UDIVSLOT value (the 16ths we
668 * calculated by not multiplying the baud by 16) as it
669 * is easy enough to recalculate.
670 */
671
672 quot = div / 16;
673 baud = rate / div;
674 } else {
675 quot = (rate + (8 * req_baud)) / (16 * req_baud);
676 baud = rate / (quot * 16);
677 }
678 quot--;
679
680 calc_deviation = req_baud - baud;
681 if (calc_deviation < 0)
682 calc_deviation = -calc_deviation;
683
684 if (calc_deviation < deviation) {
685 *best_clk = clk;
686 best_quot = quot;
687 *clk_num = cnt;
688 deviation = calc_deviation;
Ben Dooksb4975492008-07-03 12:32:51 +0100689 }
690 }
691
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200692 return best_quot;
Ben Dooksb4975492008-07-03 12:32:51 +0100693}
694
Ben Dooks090f8482008-12-12 00:24:21 +0000695/* udivslot_table[]
696 *
697 * This table takes the fractional value of the baud divisor and gives
698 * the recommended setting for the UDIVSLOT register.
699 */
700static u16 udivslot_table[16] = {
701 [0] = 0x0000,
702 [1] = 0x0080,
703 [2] = 0x0808,
704 [3] = 0x0888,
705 [4] = 0x2222,
706 [5] = 0x4924,
707 [6] = 0x4A52,
708 [7] = 0x54AA,
709 [8] = 0x5555,
710 [9] = 0xD555,
711 [10] = 0xD5D5,
712 [11] = 0xDDD5,
713 [12] = 0xDDDD,
714 [13] = 0xDFDD,
715 [14] = 0xDFDF,
716 [15] = 0xFFDF,
717};
718
Ben Dooksb4975492008-07-03 12:32:51 +0100719static void s3c24xx_serial_set_termios(struct uart_port *port,
720 struct ktermios *termios,
721 struct ktermios *old)
722{
723 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
724 struct s3c24xx_uart_port *ourport = to_ourport(port);
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900725 struct clk *clk = ERR_PTR(-EINVAL);
Ben Dooksb4975492008-07-03 12:32:51 +0100726 unsigned long flags;
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200727 unsigned int baud, quot, clk_sel = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100728 unsigned int ulcon;
729 unsigned int umcon;
Ben Dooks090f8482008-12-12 00:24:21 +0000730 unsigned int udivslot = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100731
732 /*
733 * We don't support modem control lines.
734 */
735 termios->c_cflag &= ~(HUPCL | CMSPAR);
736 termios->c_cflag |= CLOCAL;
737
738 /*
739 * Ask the core to calculate the divisor for us.
740 */
741
742 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200743 quot = s3c24xx_serial_getclk(ourport, baud, &clk, &clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +0100744 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
745 quot = port->custom_divisor;
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900746 if (IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200747 return;
Ben Dooksb4975492008-07-03 12:32:51 +0100748
749 /* check to see if we need to change clock source */
750
Thomas Abraham5f5a7a52011-10-24 11:47:46 +0200751 if (ourport->baudclk != clk) {
752 s3c24xx_serial_setsource(port, clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +0100753
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900754 if (!IS_ERR(ourport->baudclk)) {
Thomas Abraham9484b002012-10-03 07:40:04 +0900755 clk_disable_unprepare(ourport->baudclk);
Kyoungil Kim7cd88832012-05-20 17:45:54 +0900756 ourport->baudclk = ERR_PTR(-EINVAL);
Ben Dooksb4975492008-07-03 12:32:51 +0100757 }
758
Thomas Abraham9484b002012-10-03 07:40:04 +0900759 clk_prepare_enable(clk);
Ben Dooksb4975492008-07-03 12:32:51 +0100760
Ben Dooksb4975492008-07-03 12:32:51 +0100761 ourport->baudclk = clk;
Ben Dooks30555472008-10-21 14:06:36 +0100762 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100763 }
764
Ben Dooks090f8482008-12-12 00:24:21 +0000765 if (ourport->info->has_divslot) {
766 unsigned int div = ourport->baudclk_rate / baud;
767
Jongpill Lee8b526ae2010-07-16 10:19:41 +0900768 if (cfg->has_fracval) {
769 udivslot = (div & 15);
770 dbg("fracval = %04x\n", udivslot);
771 } else {
772 udivslot = udivslot_table[div & 15];
773 dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
774 }
Ben Dooks090f8482008-12-12 00:24:21 +0000775 }
776
Ben Dooksb4975492008-07-03 12:32:51 +0100777 switch (termios->c_cflag & CSIZE) {
778 case CS5:
779 dbg("config: 5bits/char\n");
780 ulcon = S3C2410_LCON_CS5;
781 break;
782 case CS6:
783 dbg("config: 6bits/char\n");
784 ulcon = S3C2410_LCON_CS6;
785 break;
786 case CS7:
787 dbg("config: 7bits/char\n");
788 ulcon = S3C2410_LCON_CS7;
789 break;
790 case CS8:
791 default:
792 dbg("config: 8bits/char\n");
793 ulcon = S3C2410_LCON_CS8;
794 break;
795 }
796
797 /* preserve original lcon IR settings */
798 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
799
800 if (termios->c_cflag & CSTOPB)
801 ulcon |= S3C2410_LCON_STOPB;
802
Ben Dooksb4975492008-07-03 12:32:51 +0100803 if (termios->c_cflag & PARENB) {
804 if (termios->c_cflag & PARODD)
805 ulcon |= S3C2410_LCON_PODD;
806 else
807 ulcon |= S3C2410_LCON_PEVEN;
808 } else {
809 ulcon |= S3C2410_LCON_PNONE;
810 }
811
812 spin_lock_irqsave(&port->lock, flags);
813
Ben Dooks090f8482008-12-12 00:24:21 +0000814 dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
815 ulcon, quot, udivslot);
Ben Dooksb4975492008-07-03 12:32:51 +0100816
817 wr_regl(port, S3C2410_ULCON, ulcon);
818 wr_regl(port, S3C2410_UBRDIV, quot);
José Miguel Gonçalves2d1e5a42013-09-18 16:52:49 +0100819
820 umcon = rd_regl(port, S3C2410_UMCON);
821 if (termios->c_cflag & CRTSCTS) {
822 umcon |= S3C2410_UMCOM_AFC;
823 /* Disable RTS when RX FIFO contains 63 bytes */
824 umcon &= ~S3C2412_UMCON_AFC_8;
825 } else {
826 umcon &= ~S3C2410_UMCOM_AFC;
827 }
Ben Dooksb4975492008-07-03 12:32:51 +0100828 wr_regl(port, S3C2410_UMCON, umcon);
829
Ben Dooks090f8482008-12-12 00:24:21 +0000830 if (ourport->info->has_divslot)
831 wr_regl(port, S3C2443_DIVSLOT, udivslot);
832
Ben Dooksb4975492008-07-03 12:32:51 +0100833 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
834 rd_regl(port, S3C2410_ULCON),
835 rd_regl(port, S3C2410_UCON),
836 rd_regl(port, S3C2410_UFCON));
837
838 /*
839 * Update the per-port timeout.
840 */
841 uart_update_timeout(port, termios->c_cflag, baud);
842
843 /*
844 * Which character status flags are we interested in?
845 */
846 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
847 if (termios->c_iflag & INPCK)
848 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
849
850 /*
851 * Which character status flags should we ignore?
852 */
853 port->ignore_status_mask = 0;
854 if (termios->c_iflag & IGNPAR)
855 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
856 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
857 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
858
859 /*
860 * Ignore all characters if CREAD is not set.
861 */
862 if ((termios->c_cflag & CREAD) == 0)
863 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
864
865 spin_unlock_irqrestore(&port->lock, flags);
866}
867
868static const char *s3c24xx_serial_type(struct uart_port *port)
869{
870 switch (port->type) {
871 case PORT_S3C2410:
872 return "S3C2410";
873 case PORT_S3C2440:
874 return "S3C2440";
875 case PORT_S3C2412:
876 return "S3C2412";
Ben Dooksb690ace2008-10-21 14:07:03 +0100877 case PORT_S3C6400:
878 return "S3C6400/10";
Ben Dooksb4975492008-07-03 12:32:51 +0100879 default:
880 return NULL;
881 }
882}
883
884#define MAP_SIZE (0x100)
885
886static void s3c24xx_serial_release_port(struct uart_port *port)
887{
888 release_mem_region(port->mapbase, MAP_SIZE);
889}
890
891static int s3c24xx_serial_request_port(struct uart_port *port)
892{
893 const char *name = s3c24xx_serial_portname(port);
894 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
895}
896
897static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
898{
899 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
900
901 if (flags & UART_CONFIG_TYPE &&
902 s3c24xx_serial_request_port(port) == 0)
903 port->type = info->type;
904}
905
906/*
907 * verify the new serial_struct (for TIOCSSERIAL).
908 */
909static int
910s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
911{
912 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
913
914 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
915 return -EINVAL;
916
917 return 0;
918}
919
920
921#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
922
923static struct console s3c24xx_serial_console;
924
Julien Pichon93b5c032012-09-21 23:22:31 -0700925static int __init s3c24xx_serial_console_init(void)
926{
927 register_console(&s3c24xx_serial_console);
928 return 0;
929}
930console_initcall(s3c24xx_serial_console_init);
931
Ben Dooksb4975492008-07-03 12:32:51 +0100932#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
933#else
934#define S3C24XX_SERIAL_CONSOLE NULL
935#endif
936
Arnd Bergmann84f57d92013-04-11 02:04:49 +0200937#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
Julien Pichon93b5c032012-09-21 23:22:31 -0700938static int s3c24xx_serial_get_poll_char(struct uart_port *port);
939static void s3c24xx_serial_put_poll_char(struct uart_port *port,
940 unsigned char c);
941#endif
942
Ben Dooksb4975492008-07-03 12:32:51 +0100943static struct uart_ops s3c24xx_serial_ops = {
944 .pm = s3c24xx_serial_pm,
945 .tx_empty = s3c24xx_serial_tx_empty,
946 .get_mctrl = s3c24xx_serial_get_mctrl,
947 .set_mctrl = s3c24xx_serial_set_mctrl,
948 .stop_tx = s3c24xx_serial_stop_tx,
949 .start_tx = s3c24xx_serial_start_tx,
950 .stop_rx = s3c24xx_serial_stop_rx,
Ben Dooksb4975492008-07-03 12:32:51 +0100951 .break_ctl = s3c24xx_serial_break_ctl,
952 .startup = s3c24xx_serial_startup,
953 .shutdown = s3c24xx_serial_shutdown,
954 .set_termios = s3c24xx_serial_set_termios,
955 .type = s3c24xx_serial_type,
956 .release_port = s3c24xx_serial_release_port,
957 .request_port = s3c24xx_serial_request_port,
958 .config_port = s3c24xx_serial_config_port,
959 .verify_port = s3c24xx_serial_verify_port,
Arnd Bergmann84f57d92013-04-11 02:04:49 +0200960#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_CONSOLE_POLL)
Julien Pichon93b5c032012-09-21 23:22:31 -0700961 .poll_get_char = s3c24xx_serial_get_poll_char,
962 .poll_put_char = s3c24xx_serial_put_poll_char,
963#endif
Ben Dooksb4975492008-07-03 12:32:51 +0100964};
965
Ben Dooksb4975492008-07-03 12:32:51 +0100966static struct uart_driver s3c24xx_uart_drv = {
967 .owner = THIS_MODULE,
Darius Augulis2cf0c582011-01-12 14:50:51 +0900968 .driver_name = "s3c2410_serial",
Ben Dooksbdd49152008-11-03 19:51:42 +0000969 .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
Ben Dooksb4975492008-07-03 12:32:51 +0100970 .cons = S3C24XX_SERIAL_CONSOLE,
Darius Augulis2cf0c582011-01-12 14:50:51 +0900971 .dev_name = S3C24XX_SERIAL_NAME,
Ben Dooksb4975492008-07-03 12:32:51 +0100972 .major = S3C24XX_SERIAL_MAJOR,
973 .minor = S3C24XX_SERIAL_MINOR,
974};
975
Ben Dooks03d5e772008-11-03 09:21:23 +0000976static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
Ben Dooksb4975492008-07-03 12:32:51 +0100977 [0] = {
978 .port = {
979 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
980 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +0100981 .uartclk = 0,
982 .fifosize = 16,
983 .ops = &s3c24xx_serial_ops,
984 .flags = UPF_BOOT_AUTOCONF,
985 .line = 0,
986 }
987 },
988 [1] = {
989 .port = {
990 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
991 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +0100992 .uartclk = 0,
993 .fifosize = 16,
994 .ops = &s3c24xx_serial_ops,
995 .flags = UPF_BOOT_AUTOCONF,
996 .line = 1,
997 }
998 },
Ben Dooks03d5e772008-11-03 09:21:23 +0000999#if CONFIG_SERIAL_SAMSUNG_UARTS > 2
Ben Dooksb4975492008-07-03 12:32:51 +01001000
1001 [2] = {
1002 .port = {
1003 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
1004 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +01001005 .uartclk = 0,
1006 .fifosize = 16,
1007 .ops = &s3c24xx_serial_ops,
1008 .flags = UPF_BOOT_AUTOCONF,
1009 .line = 2,
1010 }
Ben Dooks03d5e772008-11-03 09:21:23 +00001011 },
1012#endif
1013#if CONFIG_SERIAL_SAMSUNG_UARTS > 3
1014 [3] = {
1015 .port = {
1016 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
1017 .iotype = UPIO_MEM,
Ben Dooks03d5e772008-11-03 09:21:23 +00001018 .uartclk = 0,
1019 .fifosize = 16,
1020 .ops = &s3c24xx_serial_ops,
1021 .flags = UPF_BOOT_AUTOCONF,
1022 .line = 3,
1023 }
Ben Dooksb4975492008-07-03 12:32:51 +01001024 }
1025#endif
1026};
1027
1028/* s3c24xx_serial_resetport
1029 *
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001030 * reset the fifos and other the settings.
Ben Dooksb4975492008-07-03 12:32:51 +01001031*/
1032
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001033static void s3c24xx_serial_resetport(struct uart_port *port,
1034 struct s3c2410_uartcfg *cfg)
Ben Dooksb4975492008-07-03 12:32:51 +01001035{
1036 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001037 unsigned long ucon = rd_regl(port, S3C2410_UCON);
1038 unsigned int ucon_mask;
Ben Dooksb4975492008-07-03 12:32:51 +01001039
Thomas Abraham0dfb3b42011-10-24 11:48:21 +02001040 ucon_mask = info->clksel_mask;
1041 if (info->type == PORT_S3C2440)
1042 ucon_mask |= S3C2440_UCON0_DIVMASK;
1043
1044 ucon &= ucon_mask;
1045 wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
1046
1047 /* reset both fifos */
1048 wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH);
1049 wr_regl(port, S3C2410_UFCON, cfg->ufcon);
1050
1051 /* some delay is required after fifo reset */
1052 udelay(1);
Ben Dooksb4975492008-07-03 12:32:51 +01001053}
1054
Ben Dooks30555472008-10-21 14:06:36 +01001055
1056#ifdef CONFIG_CPU_FREQ
1057
1058static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
1059 unsigned long val, void *data)
1060{
1061 struct s3c24xx_uart_port *port;
1062 struct uart_port *uport;
1063
1064 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
1065 uport = &port->port;
1066
1067 /* check to see if port is enabled */
1068
1069 if (port->pm_level != 0)
1070 return 0;
1071
1072 /* try and work out if the baudrate is changing, we can detect
1073 * a change in rate, but we do not have support for detecting
1074 * a disturbance in the clock-rate over the change.
1075 */
1076
Kyoungil Kim25f04ad2012-05-20 17:49:31 +09001077 if (IS_ERR(port->baudclk))
Ben Dooks30555472008-10-21 14:06:36 +01001078 goto exit;
1079
Kyoungil Kim25f04ad2012-05-20 17:49:31 +09001080 if (port->baudclk_rate == clk_get_rate(port->baudclk))
Ben Dooks30555472008-10-21 14:06:36 +01001081 goto exit;
1082
1083 if (val == CPUFREQ_PRECHANGE) {
1084 /* we should really shut the port down whilst the
1085 * frequency change is in progress. */
1086
1087 } else if (val == CPUFREQ_POSTCHANGE) {
1088 struct ktermios *termios;
1089 struct tty_struct *tty;
1090
Alan Coxebd2c8f2009-09-19 13:13:28 -07001091 if (uport->state == NULL)
Ben Dooks30555472008-10-21 14:06:36 +01001092 goto exit;
Ben Dooks30555472008-10-21 14:06:36 +01001093
Alan Coxebd2c8f2009-09-19 13:13:28 -07001094 tty = uport->state->port.tty;
Ben Dooks30555472008-10-21 14:06:36 +01001095
Ben Dooks7de40c22008-12-14 23:11:02 +00001096 if (tty == NULL)
Ben Dooks30555472008-10-21 14:06:36 +01001097 goto exit;
Ben Dooks30555472008-10-21 14:06:36 +01001098
Alan Coxadc8d742012-07-14 15:31:47 +01001099 termios = &tty->termios;
Ben Dooks30555472008-10-21 14:06:36 +01001100
1101 if (termios == NULL) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301102 dev_warn(uport->dev, "%s: no termios?\n", __func__);
Ben Dooks30555472008-10-21 14:06:36 +01001103 goto exit;
1104 }
1105
1106 s3c24xx_serial_set_termios(uport, termios, NULL);
1107 }
1108
1109 exit:
1110 return 0;
1111}
1112
1113static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1114{
1115 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1116
1117 return cpufreq_register_notifier(&port->freq_transition,
1118 CPUFREQ_TRANSITION_NOTIFIER);
1119}
1120
1121static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1122{
1123 cpufreq_unregister_notifier(&port->freq_transition,
1124 CPUFREQ_TRANSITION_NOTIFIER);
1125}
1126
1127#else
1128static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1129{
1130 return 0;
1131}
1132
1133static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1134{
1135}
1136#endif
1137
Ben Dooksb4975492008-07-03 12:32:51 +01001138/* s3c24xx_serial_init_port
1139 *
1140 * initialise a single serial port from the platform device given
1141 */
1142
1143static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
Ben Dooksb4975492008-07-03 12:32:51 +01001144 struct platform_device *platdev)
1145{
1146 struct uart_port *port = &ourport->port;
Thomas Abrahamda121502011-11-02 19:23:25 +09001147 struct s3c2410_uartcfg *cfg = ourport->cfg;
Ben Dooksb4975492008-07-03 12:32:51 +01001148 struct resource *res;
1149 int ret;
1150
1151 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1152
1153 if (platdev == NULL)
1154 return -ENODEV;
1155
Ben Dooksb4975492008-07-03 12:32:51 +01001156 if (port->mapbase != 0)
1157 return 0;
1158
Ben Dooksb4975492008-07-03 12:32:51 +01001159 /* setup info for port */
1160 port->dev = &platdev->dev;
Ben Dooksb4975492008-07-03 12:32:51 +01001161
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301162 /* Startup sequence is different for s3c64xx and higher SoC's */
1163 if (s3c24xx_serial_has_interrupt_mask(port))
1164 s3c24xx_serial_ops.startup = s3c64xx_serial_startup;
1165
Ben Dooksb4975492008-07-03 12:32:51 +01001166 port->uartclk = 1;
1167
1168 if (cfg->uart_flags & UPF_CONS_FLOW) {
1169 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1170 port->flags |= UPF_CONS_FLOW;
1171 }
1172
1173 /* sort our the physical and virtual addresses for each UART */
1174
1175 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1176 if (res == NULL) {
Sachin Kamatd20925e2012-09-05 10:30:10 +05301177 dev_err(port->dev, "failed to find memory resource for uart\n");
Ben Dooksb4975492008-07-03 12:32:51 +01001178 return -EINVAL;
1179 }
1180
Joe Perchese4ac92d2014-05-20 14:05:50 -07001181 dbg("resource %pR)\n", res);
Ben Dooksb4975492008-07-03 12:32:51 +01001182
Thomas Abraham41147bf2013-01-01 00:21:55 -08001183 port->membase = devm_ioremap(port->dev, res->start, resource_size(res));
1184 if (!port->membase) {
1185 dev_err(port->dev, "failed to remap controller address\n");
1186 return -EBUSY;
1187 }
1188
Ben Dooksb690ace2008-10-21 14:07:03 +01001189 port->mapbase = res->start;
Ben Dooksb4975492008-07-03 12:32:51 +01001190 ret = platform_get_irq(platdev, 0);
1191 if (ret < 0)
1192 port->irq = 0;
Ben Dooksb73c289c2008-10-21 14:07:04 +01001193 else {
Ben Dooksb4975492008-07-03 12:32:51 +01001194 port->irq = ret;
Ben Dooksb73c289c2008-10-21 14:07:04 +01001195 ourport->rx_irq = ret;
1196 ourport->tx_irq = ret + 1;
1197 }
Sachin Kamat9303ac12012-09-05 10:30:11 +05301198
Ben Dooksb73c289c2008-10-21 14:07:04 +01001199 ret = platform_get_irq(platdev, 1);
1200 if (ret > 0)
1201 ourport->tx_irq = ret;
Ben Dooksb4975492008-07-03 12:32:51 +01001202
1203 ourport->clk = clk_get(&platdev->dev, "uart");
Chander Kashyap60e93572013-05-28 18:32:07 +05301204 if (IS_ERR(ourport->clk)) {
1205 pr_err("%s: Controller clock not found\n",
1206 dev_name(&platdev->dev));
1207 return PTR_ERR(ourport->clk);
1208 }
1209
1210 ret = clk_prepare_enable(ourport->clk);
1211 if (ret) {
1212 pr_err("uart: clock failed to prepare+enable: %d\n", ret);
1213 clk_put(ourport->clk);
1214 return ret;
1215 }
Ben Dooksb4975492008-07-03 12:32:51 +01001216
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301217 /* Keep all interrupts masked and cleared */
1218 if (s3c24xx_serial_has_interrupt_mask(port)) {
1219 wr_regl(port, S3C64XX_UINTM, 0xf);
1220 wr_regl(port, S3C64XX_UINTP, 0xf);
1221 wr_regl(port, S3C64XX_UINTSP, 0xf);
1222 }
1223
Fabio Estevam1ff5b642014-06-04 20:06:41 -03001224 dbg("port: map=%pa, mem=%p, irq=%d (%d,%d), clock=%u\n",
1225 &port->mapbase, port->membase, port->irq,
Ben Dooksb73c289c2008-10-21 14:07:04 +01001226 ourport->rx_irq, ourport->tx_irq, port->uartclk);
Ben Dooksb4975492008-07-03 12:32:51 +01001227
1228 /* reset the fifos (and setup the uart) */
1229 s3c24xx_serial_resetport(port, cfg);
1230 return 0;
1231}
1232
Arnd Bergmann17efd2b2013-04-11 02:04:47 +02001233#ifdef CONFIG_SAMSUNG_CLOCK
Ben Dooksb4975492008-07-03 12:32:51 +01001234static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
1235 struct device_attribute *attr,
1236 char *buf)
1237{
1238 struct uart_port *port = s3c24xx_dev_to_port(dev);
1239 struct s3c24xx_uart_port *ourport = to_ourport(port);
1240
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001241 if (IS_ERR(ourport->baudclk))
1242 return -EINVAL;
1243
KeyYoung Park7b15e1d2012-05-30 17:29:55 +09001244 return snprintf(buf, PAGE_SIZE, "* %s\n",
1245 ourport->baudclk->name ?: "(null)");
Ben Dooksb4975492008-07-03 12:32:51 +01001246}
1247
1248static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
Arnd Bergmann17efd2b2013-04-11 02:04:47 +02001249#endif
Thomas Abraham26c919e2011-11-06 22:10:44 +05301250
Ben Dooksb4975492008-07-03 12:32:51 +01001251/* Device driver serial port probe */
1252
Thomas Abraham26c919e2011-11-06 22:10:44 +05301253static const struct of_device_id s3c24xx_uart_dt_match[];
Ben Dooksb4975492008-07-03 12:32:51 +01001254static int probe_index;
1255
Thomas Abraham26c919e2011-11-06 22:10:44 +05301256static inline struct s3c24xx_serial_drv_data *s3c24xx_get_driver_data(
1257 struct platform_device *pdev)
1258{
1259#ifdef CONFIG_OF
1260 if (pdev->dev.of_node) {
1261 const struct of_device_id *match;
1262 match = of_match_node(s3c24xx_uart_dt_match, pdev->dev.of_node);
1263 return (struct s3c24xx_serial_drv_data *)match->data;
1264 }
1265#endif
1266 return (struct s3c24xx_serial_drv_data *)
1267 platform_get_device_id(pdev)->driver_data;
1268}
1269
Thomas Abrahamda121502011-11-02 19:23:25 +09001270static int s3c24xx_serial_probe(struct platform_device *pdev)
Ben Dooksb4975492008-07-03 12:32:51 +01001271{
1272 struct s3c24xx_uart_port *ourport;
Tomasz Figa13a9f6c62014-06-26 13:24:34 +02001273 int index = probe_index;
Ben Dooksb4975492008-07-03 12:32:51 +01001274 int ret;
1275
Tomasz Figa13a9f6c62014-06-26 13:24:34 +02001276 if (pdev->dev.of_node) {
1277 ret = of_alias_get_id(pdev->dev.of_node, "serial");
1278 if (ret >= 0)
1279 index = ret;
1280 }
Ben Dooksb4975492008-07-03 12:32:51 +01001281
Tomasz Figa13a9f6c62014-06-26 13:24:34 +02001282 dbg("s3c24xx_serial_probe(%p) %d\n", pdev, index);
1283
1284 ourport = &s3c24xx_serial_ports[index];
Thomas Abrahamda121502011-11-02 19:23:25 +09001285
Thomas Abraham26c919e2011-11-06 22:10:44 +05301286 ourport->drv_data = s3c24xx_get_driver_data(pdev);
1287 if (!ourport->drv_data) {
1288 dev_err(&pdev->dev, "could not find driver data\n");
1289 return -ENODEV;
1290 }
Thomas Abrahamda121502011-11-02 19:23:25 +09001291
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001292 ourport->baudclk = ERR_PTR(-EINVAL);
Thomas Abrahamda121502011-11-02 19:23:25 +09001293 ourport->info = ourport->drv_data->info;
Jingoo Han574de552013-07-30 17:06:57 +09001294 ourport->cfg = (dev_get_platdata(&pdev->dev)) ?
Jingoo Hand4aab202013-09-09 14:10:30 +09001295 dev_get_platdata(&pdev->dev) :
Thomas Abrahamda121502011-11-02 19:23:25 +09001296 ourport->drv_data->def_cfg;
1297
Naveen Krishna Chatradhi135f07c2014-07-14 17:07:16 +05301298 if (pdev->dev.of_node)
1299 of_property_read_u32(pdev->dev.of_node,
1300 "samsung,uart-fifosize", &ourport->port.fifosize);
1301
1302 if (!ourport->port.fifosize) {
1303 ourport->port.fifosize = (ourport->info->fifosize) ?
1304 ourport->info->fifosize :
1305 ourport->drv_data->fifosize[index];
1306 }
Thomas Abrahamda121502011-11-02 19:23:25 +09001307
Ben Dooksb4975492008-07-03 12:32:51 +01001308 probe_index++;
1309
1310 dbg("%s: initialising port %p...\n", __func__, ourport);
1311
Thomas Abrahamda121502011-11-02 19:23:25 +09001312 ret = s3c24xx_serial_init_port(ourport, pdev);
Ben Dooksb4975492008-07-03 12:32:51 +01001313 if (ret < 0)
Tushar Behera8ad711a2014-06-23 11:32:14 +05301314 return ret;
Ben Dooksb4975492008-07-03 12:32:51 +01001315
Tushar Behera6f134c3c2014-01-20 14:32:34 +05301316 if (!s3c24xx_uart_drv.state) {
1317 ret = uart_register_driver(&s3c24xx_uart_drv);
1318 if (ret < 0) {
1319 pr_err("Failed to register Samsung UART driver\n");
1320 return ret;
1321 }
1322 }
1323
Ben Dooksb4975492008-07-03 12:32:51 +01001324 dbg("%s: adding port\n", __func__);
1325 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
Thomas Abrahamda121502011-11-02 19:23:25 +09001326 platform_set_drvdata(pdev, &ourport->port);
Ben Dooksb4975492008-07-03 12:32:51 +01001327
Heiko Stübner0da33362013-12-05 00:54:38 +01001328 /*
1329 * Deactivate the clock enabled in s3c24xx_serial_init_port here,
1330 * so that a potential re-enablement through the pm-callback overlaps
1331 * and keeps the clock enabled in this case.
1332 */
1333 clk_disable_unprepare(ourport->clk);
1334
Arnd Bergmann17efd2b2013-04-11 02:04:47 +02001335#ifdef CONFIG_SAMSUNG_CLOCK
Thomas Abrahamda121502011-11-02 19:23:25 +09001336 ret = device_create_file(&pdev->dev, &dev_attr_clock_source);
Ben Dooksb4975492008-07-03 12:32:51 +01001337 if (ret < 0)
Thomas Abrahamda121502011-11-02 19:23:25 +09001338 dev_err(&pdev->dev, "failed to add clock source attr.\n");
Arnd Bergmann17efd2b2013-04-11 02:04:47 +02001339#endif
Ben Dooksb4975492008-07-03 12:32:51 +01001340
Ben Dooks30555472008-10-21 14:06:36 +01001341 ret = s3c24xx_serial_cpufreq_register(ourport);
1342 if (ret < 0)
Thomas Abrahamda121502011-11-02 19:23:25 +09001343 dev_err(&pdev->dev, "failed to add cpufreq notifier\n");
Ben Dooks30555472008-10-21 14:06:36 +01001344
Ben Dooksb4975492008-07-03 12:32:51 +01001345 return 0;
Ben Dooksb4975492008-07-03 12:32:51 +01001346}
1347
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001348static int s3c24xx_serial_remove(struct platform_device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01001349{
1350 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1351
1352 if (port) {
Ben Dooks30555472008-10-21 14:06:36 +01001353 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
Arnd Bergmann17efd2b2013-04-11 02:04:47 +02001354#ifdef CONFIG_SAMSUNG_CLOCK
Ben Dooksb4975492008-07-03 12:32:51 +01001355 device_remove_file(&dev->dev, &dev_attr_clock_source);
Arnd Bergmann17efd2b2013-04-11 02:04:47 +02001356#endif
Ben Dooksb4975492008-07-03 12:32:51 +01001357 uart_remove_one_port(&s3c24xx_uart_drv, port);
1358 }
1359
Tushar Behera6f134c3c2014-01-20 14:32:34 +05301360 uart_unregister_driver(&s3c24xx_uart_drv);
1361
Ben Dooksb4975492008-07-03 12:32:51 +01001362 return 0;
1363}
1364
Ben Dooksb4975492008-07-03 12:32:51 +01001365/* UART power management code */
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001366#ifdef CONFIG_PM_SLEEP
1367static int s3c24xx_serial_suspend(struct device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01001368{
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001369 struct uart_port *port = s3c24xx_dev_to_port(dev);
Ben Dooksb4975492008-07-03 12:32:51 +01001370
1371 if (port)
1372 uart_suspend_port(&s3c24xx_uart_drv, port);
1373
1374 return 0;
1375}
1376
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001377static int s3c24xx_serial_resume(struct device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01001378{
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001379 struct uart_port *port = s3c24xx_dev_to_port(dev);
Ben Dooksb4975492008-07-03 12:32:51 +01001380 struct s3c24xx_uart_port *ourport = to_ourport(port);
1381
1382 if (port) {
Thomas Abraham9484b002012-10-03 07:40:04 +09001383 clk_prepare_enable(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +01001384 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
Thomas Abraham9484b002012-10-03 07:40:04 +09001385 clk_disable_unprepare(ourport->clk);
Ben Dooksb4975492008-07-03 12:32:51 +01001386
1387 uart_resume_port(&s3c24xx_uart_drv, port);
1388 }
1389
1390 return 0;
1391}
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001392
Michael Spangd09a7302013-03-27 19:34:24 -04001393static int s3c24xx_serial_resume_noirq(struct device *dev)
1394{
1395 struct uart_port *port = s3c24xx_dev_to_port(dev);
1396
1397 if (port) {
1398 /* restore IRQ mask */
1399 if (s3c24xx_serial_has_interrupt_mask(port)) {
1400 unsigned int uintm = 0xf;
1401 if (tx_enabled(port))
1402 uintm &= ~S3C64XX_UINTM_TXD_MSK;
1403 if (rx_enabled(port))
1404 uintm &= ~S3C64XX_UINTM_RXD_MSK;
1405 wr_regl(port, S3C64XX_UINTM, uintm);
1406 }
1407 }
1408
1409 return 0;
1410}
1411
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001412static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
1413 .suspend = s3c24xx_serial_suspend,
1414 .resume = s3c24xx_serial_resume,
Michael Spangd09a7302013-03-27 19:34:24 -04001415 .resume_noirq = s3c24xx_serial_resume_noirq,
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001416};
Kukjin Kimb882fc12011-07-28 08:50:38 +09001417#define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
1418
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001419#else /* !CONFIG_PM_SLEEP */
Kukjin Kimb882fc12011-07-28 08:50:38 +09001420
1421#define SERIAL_SAMSUNG_PM_OPS NULL
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001422#endif /* CONFIG_PM_SLEEP */
Ben Dooksb4975492008-07-03 12:32:51 +01001423
Ben Dooksb4975492008-07-03 12:32:51 +01001424/* Console code */
1425
1426#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1427
1428static struct uart_port *cons_uart;
1429
1430static int
1431s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1432{
1433 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1434 unsigned long ufstat, utrstat;
1435
1436 if (ufcon & S3C2410_UFCON_FIFOMODE) {
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001437 /* fifo mode - check amount of data in fifo registers... */
Ben Dooksb4975492008-07-03 12:32:51 +01001438
1439 ufstat = rd_regl(port, S3C2410_UFSTAT);
1440 return (ufstat & info->tx_fifofull) ? 0 : 1;
1441 }
1442
1443 /* in non-fifo mode, we go and use the tx buffer empty */
1444
1445 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1446 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1447}
1448
Michael Spang38adbc52013-03-27 19:34:25 -04001449static bool
1450s3c24xx_port_configured(unsigned int ucon)
1451{
1452 /* consider the serial port configured if the tx/rx mode set */
1453 return (ucon & 0xf) != 0;
1454}
1455
Julien Pichon93b5c032012-09-21 23:22:31 -07001456#ifdef CONFIG_CONSOLE_POLL
1457/*
1458 * Console polling routines for writing and reading from the uart while
1459 * in an interrupt or debug context.
1460 */
1461
1462static int s3c24xx_serial_get_poll_char(struct uart_port *port)
1463{
1464 struct s3c24xx_uart_port *ourport = to_ourport(port);
1465 unsigned int ufstat;
1466
1467 ufstat = rd_regl(port, S3C2410_UFSTAT);
1468 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
1469 return NO_POLL_CHAR;
1470
1471 return rd_regb(port, S3C2410_URXH);
1472}
1473
1474static void s3c24xx_serial_put_poll_char(struct uart_port *port,
1475 unsigned char c)
1476{
Doug Andersonbb7f09b2014-04-21 09:40:34 -07001477 unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
1478 unsigned int ucon = rd_regl(port, S3C2410_UCON);
Michael Spang38adbc52013-03-27 19:34:25 -04001479
1480 /* not possible to xmit on unconfigured port */
1481 if (!s3c24xx_port_configured(ucon))
1482 return;
Julien Pichon93b5c032012-09-21 23:22:31 -07001483
1484 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1485 cpu_relax();
Doug Andersonbb7f09b2014-04-21 09:40:34 -07001486 wr_regb(port, S3C2410_UTXH, c);
Julien Pichon93b5c032012-09-21 23:22:31 -07001487}
1488
1489#endif /* CONFIG_CONSOLE_POLL */
1490
Ben Dooksb4975492008-07-03 12:32:51 +01001491static void
1492s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
1493{
Doug Andersonbb7f09b2014-04-21 09:40:34 -07001494 unsigned int ufcon = rd_regl(port, S3C2410_UFCON);
Michael Spang38adbc52013-03-27 19:34:25 -04001495
Ben Dooksb4975492008-07-03 12:32:51 +01001496 while (!s3c24xx_serial_console_txrdy(port, ufcon))
Doug Andersonf94b0572014-04-21 09:40:36 -07001497 cpu_relax();
Doug Andersonbb7f09b2014-04-21 09:40:34 -07001498 wr_regb(port, S3C2410_UTXH, ch);
Ben Dooksb4975492008-07-03 12:32:51 +01001499}
1500
1501static void
1502s3c24xx_serial_console_write(struct console *co, const char *s,
1503 unsigned int count)
1504{
Doug Andersonab88c8d2014-04-21 09:40:35 -07001505 unsigned int ucon = rd_regl(cons_uart, S3C2410_UCON);
1506
1507 /* not possible to xmit on unconfigured port */
1508 if (!s3c24xx_port_configured(ucon))
1509 return;
1510
Ben Dooksb4975492008-07-03 12:32:51 +01001511 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
1512}
1513
1514static void __init
1515s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1516 int *parity, int *bits)
1517{
Ben Dooksb4975492008-07-03 12:32:51 +01001518 struct clk *clk;
1519 unsigned int ulcon;
1520 unsigned int ucon;
1521 unsigned int ubrdiv;
1522 unsigned long rate;
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001523 unsigned int clk_sel;
1524 char clk_name[MAX_CLK_NAME_LENGTH];
Ben Dooksb4975492008-07-03 12:32:51 +01001525
1526 ulcon = rd_regl(port, S3C2410_ULCON);
1527 ucon = rd_regl(port, S3C2410_UCON);
1528 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1529
1530 dbg("s3c24xx_serial_get_options: port=%p\n"
1531 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1532 port, ulcon, ucon, ubrdiv);
1533
Michael Spang38adbc52013-03-27 19:34:25 -04001534 if (s3c24xx_port_configured(ucon)) {
Ben Dooksb4975492008-07-03 12:32:51 +01001535 switch (ulcon & S3C2410_LCON_CSMASK) {
1536 case S3C2410_LCON_CS5:
1537 *bits = 5;
1538 break;
1539 case S3C2410_LCON_CS6:
1540 *bits = 6;
1541 break;
1542 case S3C2410_LCON_CS7:
1543 *bits = 7;
1544 break;
1545 default:
1546 case S3C2410_LCON_CS8:
1547 *bits = 8;
1548 break;
1549 }
1550
1551 switch (ulcon & S3C2410_LCON_PMASK) {
1552 case S3C2410_LCON_PEVEN:
1553 *parity = 'e';
1554 break;
1555
1556 case S3C2410_LCON_PODD:
1557 *parity = 'o';
1558 break;
1559
1560 case S3C2410_LCON_PNONE:
1561 default:
1562 *parity = 'n';
1563 }
1564
1565 /* now calculate the baud rate */
1566
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001567 clk_sel = s3c24xx_serial_getsource(port);
1568 sprintf(clk_name, "clk_uart_baud%d", clk_sel);
Ben Dooksb4975492008-07-03 12:32:51 +01001569
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001570 clk = clk_get(port->dev, clk_name);
Kyoungil Kim7cd88832012-05-20 17:45:54 +09001571 if (!IS_ERR(clk))
Thomas Abraham5f5a7a52011-10-24 11:47:46 +02001572 rate = clk_get_rate(clk);
Ben Dooksb4975492008-07-03 12:32:51 +01001573 else
1574 rate = 1;
1575
Ben Dooksb4975492008-07-03 12:32:51 +01001576 *baud = rate / (16 * (ubrdiv + 1));
1577 dbg("calculated baud %d\n", *baud);
1578 }
1579
1580}
1581
Ben Dooksb4975492008-07-03 12:32:51 +01001582static int __init
1583s3c24xx_serial_console_setup(struct console *co, char *options)
1584{
1585 struct uart_port *port;
1586 int baud = 9600;
1587 int bits = 8;
1588 int parity = 'n';
1589 int flow = 'n';
1590
1591 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1592 co, co->index, options);
1593
1594 /* is this a valid port */
1595
Ben Dooks03d5e772008-11-03 09:21:23 +00001596 if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
Ben Dooksb4975492008-07-03 12:32:51 +01001597 co->index = 0;
1598
1599 port = &s3c24xx_serial_ports[co->index].port;
1600
1601 /* is the port configured? */
1602
Thomas Abrahamee430f12011-06-14 19:12:26 +09001603 if (port->mapbase == 0x0)
1604 return -ENODEV;
Ben Dooksb4975492008-07-03 12:32:51 +01001605
1606 cons_uart = port;
1607
1608 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1609
1610 /*
1611 * Check whether an invalid uart number has been specified, and
1612 * if so, search for the first available port that does have
1613 * console support.
1614 */
1615 if (options)
1616 uart_parse_options(options, &baud, &parity, &bits, &flow);
1617 else
1618 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1619
1620 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1621
1622 return uart_set_options(port, co, baud, parity, bits, flow);
1623}
1624
Ben Dooksb4975492008-07-03 12:32:51 +01001625static struct console s3c24xx_serial_console = {
1626 .name = S3C24XX_SERIAL_NAME,
1627 .device = uart_console_device,
1628 .flags = CON_PRINTBUFFER,
1629 .index = -1,
1630 .write = s3c24xx_serial_console_write,
Thomas Abraham5822a5d2011-06-14 19:12:26 +09001631 .setup = s3c24xx_serial_console_setup,
1632 .data = &s3c24xx_uart_drv,
Ben Dooksb4975492008-07-03 12:32:51 +01001633};
Ben Dooksb4975492008-07-03 12:32:51 +01001634#endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
1635
Thomas Abrahamda121502011-11-02 19:23:25 +09001636#ifdef CONFIG_CPU_S3C2410
1637static struct s3c24xx_serial_drv_data s3c2410_serial_drv_data = {
1638 .info = &(struct s3c24xx_uart_info) {
1639 .name = "Samsung S3C2410 UART",
1640 .type = PORT_S3C2410,
1641 .fifosize = 16,
1642 .rx_fifomask = S3C2410_UFSTAT_RXMASK,
1643 .rx_fifoshift = S3C2410_UFSTAT_RXSHIFT,
1644 .rx_fifofull = S3C2410_UFSTAT_RXFULL,
1645 .tx_fifofull = S3C2410_UFSTAT_TXFULL,
1646 .tx_fifomask = S3C2410_UFSTAT_TXMASK,
1647 .tx_fifoshift = S3C2410_UFSTAT_TXSHIFT,
1648 .def_clk_sel = S3C2410_UCON_CLKSEL0,
1649 .num_clks = 2,
1650 .clksel_mask = S3C2410_UCON_CLKMASK,
1651 .clksel_shift = S3C2410_UCON_CLKSHIFT,
1652 },
1653 .def_cfg = &(struct s3c2410_uartcfg) {
1654 .ucon = S3C2410_UCON_DEFAULT,
1655 .ufcon = S3C2410_UFCON_DEFAULT,
1656 },
1657};
1658#define S3C2410_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2410_serial_drv_data)
1659#else
1660#define S3C2410_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1661#endif
1662
1663#ifdef CONFIG_CPU_S3C2412
1664static struct s3c24xx_serial_drv_data s3c2412_serial_drv_data = {
1665 .info = &(struct s3c24xx_uart_info) {
1666 .name = "Samsung S3C2412 UART",
1667 .type = PORT_S3C2412,
1668 .fifosize = 64,
1669 .has_divslot = 1,
1670 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1671 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1672 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1673 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1674 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1675 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1676 .def_clk_sel = S3C2410_UCON_CLKSEL2,
1677 .num_clks = 4,
1678 .clksel_mask = S3C2412_UCON_CLKMASK,
1679 .clksel_shift = S3C2412_UCON_CLKSHIFT,
1680 },
1681 .def_cfg = &(struct s3c2410_uartcfg) {
1682 .ucon = S3C2410_UCON_DEFAULT,
1683 .ufcon = S3C2410_UFCON_DEFAULT,
1684 },
1685};
1686#define S3C2412_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2412_serial_drv_data)
1687#else
1688#define S3C2412_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1689#endif
1690
1691#if defined(CONFIG_CPU_S3C2440) || defined(CONFIG_CPU_S3C2416) || \
Denis 'GNUtoo' Cariklib26469a2012-02-23 08:23:52 +01001692 defined(CONFIG_CPU_S3C2443) || defined(CONFIG_CPU_S3C2442)
Thomas Abrahamda121502011-11-02 19:23:25 +09001693static struct s3c24xx_serial_drv_data s3c2440_serial_drv_data = {
1694 .info = &(struct s3c24xx_uart_info) {
1695 .name = "Samsung S3C2440 UART",
1696 .type = PORT_S3C2440,
1697 .fifosize = 64,
1698 .has_divslot = 1,
1699 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1700 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1701 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1702 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1703 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1704 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1705 .def_clk_sel = S3C2410_UCON_CLKSEL2,
1706 .num_clks = 4,
1707 .clksel_mask = S3C2412_UCON_CLKMASK,
1708 .clksel_shift = S3C2412_UCON_CLKSHIFT,
1709 },
1710 .def_cfg = &(struct s3c2410_uartcfg) {
1711 .ucon = S3C2410_UCON_DEFAULT,
1712 .ufcon = S3C2410_UFCON_DEFAULT,
1713 },
1714};
1715#define S3C2440_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c2440_serial_drv_data)
1716#else
1717#define S3C2440_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1718#endif
1719
Kukjin Kim953b53a2014-07-01 06:32:22 +09001720#if defined(CONFIG_CPU_S3C6400) || defined(CONFIG_CPU_S3C6410)
Thomas Abrahamda121502011-11-02 19:23:25 +09001721static struct s3c24xx_serial_drv_data s3c6400_serial_drv_data = {
1722 .info = &(struct s3c24xx_uart_info) {
1723 .name = "Samsung S3C6400 UART",
1724 .type = PORT_S3C6400,
1725 .fifosize = 64,
1726 .has_divslot = 1,
1727 .rx_fifomask = S3C2440_UFSTAT_RXMASK,
1728 .rx_fifoshift = S3C2440_UFSTAT_RXSHIFT,
1729 .rx_fifofull = S3C2440_UFSTAT_RXFULL,
1730 .tx_fifofull = S3C2440_UFSTAT_TXFULL,
1731 .tx_fifomask = S3C2440_UFSTAT_TXMASK,
1732 .tx_fifoshift = S3C2440_UFSTAT_TXSHIFT,
1733 .def_clk_sel = S3C2410_UCON_CLKSEL2,
1734 .num_clks = 4,
1735 .clksel_mask = S3C6400_UCON_CLKMASK,
1736 .clksel_shift = S3C6400_UCON_CLKSHIFT,
1737 },
1738 .def_cfg = &(struct s3c2410_uartcfg) {
1739 .ucon = S3C2410_UCON_DEFAULT,
1740 .ufcon = S3C2410_UFCON_DEFAULT,
1741 },
1742};
1743#define S3C6400_SERIAL_DRV_DATA ((kernel_ulong_t)&s3c6400_serial_drv_data)
1744#else
1745#define S3C6400_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1746#endif
1747
1748#ifdef CONFIG_CPU_S5PV210
1749static struct s3c24xx_serial_drv_data s5pv210_serial_drv_data = {
1750 .info = &(struct s3c24xx_uart_info) {
1751 .name = "Samsung S5PV210 UART",
1752 .type = PORT_S3C6400,
1753 .has_divslot = 1,
1754 .rx_fifomask = S5PV210_UFSTAT_RXMASK,
1755 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
1756 .rx_fifofull = S5PV210_UFSTAT_RXFULL,
1757 .tx_fifofull = S5PV210_UFSTAT_TXFULL,
1758 .tx_fifomask = S5PV210_UFSTAT_TXMASK,
1759 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
1760 .def_clk_sel = S3C2410_UCON_CLKSEL0,
1761 .num_clks = 2,
1762 .clksel_mask = S5PV210_UCON_CLKMASK,
1763 .clksel_shift = S5PV210_UCON_CLKSHIFT,
1764 },
1765 .def_cfg = &(struct s3c2410_uartcfg) {
1766 .ucon = S5PV210_UCON_DEFAULT,
1767 .ufcon = S5PV210_UFCON_DEFAULT,
1768 },
1769 .fifosize = { 256, 64, 16, 16 },
1770};
1771#define S5PV210_SERIAL_DRV_DATA ((kernel_ulong_t)&s5pv210_serial_drv_data)
1772#else
1773#define S5PV210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1774#endif
1775
Chander Kashyap33f88132013-06-19 00:29:34 +09001776#if defined(CONFIG_ARCH_EXYNOS)
Thomas Abrahamda121502011-11-02 19:23:25 +09001777static struct s3c24xx_serial_drv_data exynos4210_serial_drv_data = {
1778 .info = &(struct s3c24xx_uart_info) {
1779 .name = "Samsung Exynos4 UART",
1780 .type = PORT_S3C6400,
1781 .has_divslot = 1,
1782 .rx_fifomask = S5PV210_UFSTAT_RXMASK,
1783 .rx_fifoshift = S5PV210_UFSTAT_RXSHIFT,
1784 .rx_fifofull = S5PV210_UFSTAT_RXFULL,
1785 .tx_fifofull = S5PV210_UFSTAT_TXFULL,
1786 .tx_fifomask = S5PV210_UFSTAT_TXMASK,
1787 .tx_fifoshift = S5PV210_UFSTAT_TXSHIFT,
1788 .def_clk_sel = S3C2410_UCON_CLKSEL0,
1789 .num_clks = 1,
1790 .clksel_mask = 0,
1791 .clksel_shift = 0,
1792 },
1793 .def_cfg = &(struct s3c2410_uartcfg) {
1794 .ucon = S5PV210_UCON_DEFAULT,
1795 .ufcon = S5PV210_UFCON_DEFAULT,
1796 .has_fracval = 1,
1797 },
1798 .fifosize = { 256, 64, 16, 16 },
1799};
1800#define EXYNOS4210_SERIAL_DRV_DATA ((kernel_ulong_t)&exynos4210_serial_drv_data)
1801#else
1802#define EXYNOS4210_SERIAL_DRV_DATA (kernel_ulong_t)NULL
1803#endif
1804
1805static struct platform_device_id s3c24xx_serial_driver_ids[] = {
1806 {
1807 .name = "s3c2410-uart",
1808 .driver_data = S3C2410_SERIAL_DRV_DATA,
1809 }, {
1810 .name = "s3c2412-uart",
1811 .driver_data = S3C2412_SERIAL_DRV_DATA,
1812 }, {
1813 .name = "s3c2440-uart",
1814 .driver_data = S3C2440_SERIAL_DRV_DATA,
1815 }, {
1816 .name = "s3c6400-uart",
1817 .driver_data = S3C6400_SERIAL_DRV_DATA,
1818 }, {
1819 .name = "s5pv210-uart",
1820 .driver_data = S5PV210_SERIAL_DRV_DATA,
1821 }, {
1822 .name = "exynos4210-uart",
1823 .driver_data = EXYNOS4210_SERIAL_DRV_DATA,
1824 },
1825 { },
1826};
1827MODULE_DEVICE_TABLE(platform, s3c24xx_serial_driver_ids);
1828
Thomas Abraham26c919e2011-11-06 22:10:44 +05301829#ifdef CONFIG_OF
1830static const struct of_device_id s3c24xx_uart_dt_match[] = {
Heiko Stübner666ca0b2012-11-22 11:37:44 +01001831 { .compatible = "samsung,s3c2410-uart",
1832 .data = (void *)S3C2410_SERIAL_DRV_DATA },
1833 { .compatible = "samsung,s3c2412-uart",
1834 .data = (void *)S3C2412_SERIAL_DRV_DATA },
1835 { .compatible = "samsung,s3c2440-uart",
1836 .data = (void *)S3C2440_SERIAL_DRV_DATA },
1837 { .compatible = "samsung,s3c6400-uart",
1838 .data = (void *)S3C6400_SERIAL_DRV_DATA },
1839 { .compatible = "samsung,s5pv210-uart",
1840 .data = (void *)S5PV210_SERIAL_DRV_DATA },
Thomas Abraham26c919e2011-11-06 22:10:44 +05301841 { .compatible = "samsung,exynos4210-uart",
Mark Browna169a882011-11-08 17:00:14 +09001842 .data = (void *)EXYNOS4210_SERIAL_DRV_DATA },
Thomas Abraham26c919e2011-11-06 22:10:44 +05301843 {},
1844};
1845MODULE_DEVICE_TABLE(of, s3c24xx_uart_dt_match);
Thomas Abraham26c919e2011-11-06 22:10:44 +05301846#endif
1847
Thomas Abrahamda121502011-11-02 19:23:25 +09001848static struct platform_driver samsung_serial_driver = {
1849 .probe = s3c24xx_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001850 .remove = s3c24xx_serial_remove,
Thomas Abrahamda121502011-11-02 19:23:25 +09001851 .id_table = s3c24xx_serial_driver_ids,
1852 .driver = {
1853 .name = "samsung-uart",
1854 .owner = THIS_MODULE,
1855 .pm = SERIAL_SAMSUNG_PM_OPS,
Sachin Kamat905f4ba2013-01-07 09:50:42 +05301856 .of_match_table = of_match_ptr(s3c24xx_uart_dt_match),
Thomas Abrahamda121502011-11-02 19:23:25 +09001857 },
1858};
1859
Tushar Behera6f134c3c2014-01-20 14:32:34 +05301860module_platform_driver(samsung_serial_driver);
Thomas Abrahamda121502011-11-02 19:23:25 +09001861
1862MODULE_ALIAS("platform:samsung-uart");
Ben Dooksb4975492008-07-03 12:32:51 +01001863MODULE_DESCRIPTION("Samsung SoC Serial port driver");
1864MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1865MODULE_LICENSE("GPL v2");