blob: 51cfb9f1166587c7e4848663dee685e3ceb786ff [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>
Ben Dooksb4975492008-07-03 12:32:51 +010045
46#include <asm/irq.h>
47
Russell Kinga09e64f2008-08-05 16:14:15 +010048#include <mach/hardware.h>
Ben Dooksb690ace2008-10-21 14:07:03 +010049#include <mach/map.h>
Ben Dooksb4975492008-07-03 12:32:51 +010050
Ben Dooksa2b7ba92008-10-07 22:26:09 +010051#include <plat/regs-serial.h>
Ben Dooksb4975492008-07-03 12:32:51 +010052
53#include "samsung.h"
54
55/* UART name and device definitions */
56
57#define S3C24XX_SERIAL_NAME "ttySAC"
58#define S3C24XX_SERIAL_MAJOR 204
59#define S3C24XX_SERIAL_MINOR 64
60
Ben Dooksb4975492008-07-03 12:32:51 +010061/* macros to change one thing to another */
62
63#define tx_enabled(port) ((port)->unused[0])
64#define rx_enabled(port) ((port)->unused[1])
65
Lucas De Marchi25985ed2011-03-30 22:57:33 -030066/* flag to ignore all characters coming in */
Ben Dooksb4975492008-07-03 12:32:51 +010067#define RXSTAT_DUMMY_READ (0x10000000)
68
69static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
70{
71 return container_of(port, struct s3c24xx_uart_port, port);
72}
73
74/* translate a port to the device name */
75
76static inline const char *s3c24xx_serial_portname(struct uart_port *port)
77{
78 return to_platform_device(port->dev)->name;
79}
80
81static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
82{
83 return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);
84}
85
Thomas Abraham88bb4ea2011-08-10 15:51:19 +053086/*
87 * s3c64xx and later SoC's include the interrupt mask and status registers in
88 * the controller itself, unlike the s3c24xx SoC's which have these registers
89 * in the interrupt controller. Check if the port type is s3c64xx or higher.
90 */
91static int s3c24xx_serial_has_interrupt_mask(struct uart_port *port)
92{
93 return to_ourport(port)->info->type == PORT_S3C6400;
94}
95
Ben Dooksb4975492008-07-03 12:32:51 +010096static void s3c24xx_serial_rx_enable(struct uart_port *port)
97{
98 unsigned long flags;
99 unsigned int ucon, ufcon;
100 int count = 10000;
101
102 spin_lock_irqsave(&port->lock, flags);
103
104 while (--count && !s3c24xx_serial_txempty_nofifo(port))
105 udelay(100);
106
107 ufcon = rd_regl(port, S3C2410_UFCON);
108 ufcon |= S3C2410_UFCON_RESETRX;
109 wr_regl(port, S3C2410_UFCON, ufcon);
110
111 ucon = rd_regl(port, S3C2410_UCON);
112 ucon |= S3C2410_UCON_RXIRQMODE;
113 wr_regl(port, S3C2410_UCON, ucon);
114
115 rx_enabled(port) = 1;
116 spin_unlock_irqrestore(&port->lock, flags);
117}
118
119static void s3c24xx_serial_rx_disable(struct uart_port *port)
120{
121 unsigned long flags;
122 unsigned int ucon;
123
124 spin_lock_irqsave(&port->lock, flags);
125
126 ucon = rd_regl(port, S3C2410_UCON);
127 ucon &= ~S3C2410_UCON_RXIRQMODE;
128 wr_regl(port, S3C2410_UCON, ucon);
129
130 rx_enabled(port) = 0;
131 spin_unlock_irqrestore(&port->lock, flags);
132}
133
134static void s3c24xx_serial_stop_tx(struct uart_port *port)
135{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100136 struct s3c24xx_uart_port *ourport = to_ourport(port);
137
Ben Dooksb4975492008-07-03 12:32:51 +0100138 if (tx_enabled(port)) {
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530139 if (s3c24xx_serial_has_interrupt_mask(port))
140 __set_bit(S3C64XX_UINTM_TXD,
141 portaddrl(port, S3C64XX_UINTM));
142 else
143 disable_irq_nosync(ourport->tx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100144 tx_enabled(port) = 0;
145 if (port->flags & UPF_CONS_FLOW)
146 s3c24xx_serial_rx_enable(port);
147 }
148}
149
150static void s3c24xx_serial_start_tx(struct uart_port *port)
151{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100152 struct s3c24xx_uart_port *ourport = to_ourport(port);
153
Ben Dooksb4975492008-07-03 12:32:51 +0100154 if (!tx_enabled(port)) {
155 if (port->flags & UPF_CONS_FLOW)
156 s3c24xx_serial_rx_disable(port);
157
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530158 if (s3c24xx_serial_has_interrupt_mask(port))
159 __clear_bit(S3C64XX_UINTM_TXD,
160 portaddrl(port, S3C64XX_UINTM));
161 else
162 enable_irq(ourport->tx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100163 tx_enabled(port) = 1;
164 }
165}
166
Ben Dooksb4975492008-07-03 12:32:51 +0100167static void s3c24xx_serial_stop_rx(struct uart_port *port)
168{
Ben Dooksb73c289c2008-10-21 14:07:04 +0100169 struct s3c24xx_uart_port *ourport = to_ourport(port);
170
Ben Dooksb4975492008-07-03 12:32:51 +0100171 if (rx_enabled(port)) {
172 dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530173 if (s3c24xx_serial_has_interrupt_mask(port))
174 __set_bit(S3C64XX_UINTM_RXD,
175 portaddrl(port, S3C64XX_UINTM));
176 else
177 disable_irq_nosync(ourport->rx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100178 rx_enabled(port) = 0;
179 }
180}
181
182static void s3c24xx_serial_enable_ms(struct uart_port *port)
183{
184}
185
186static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
187{
188 return to_ourport(port)->info;
189}
190
191static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
192{
Thomas Abraham4d84e972011-10-24 11:47:25 +0200193 struct s3c24xx_uart_port *ourport;
194
Ben Dooksb4975492008-07-03 12:32:51 +0100195 if (port->dev == NULL)
196 return NULL;
197
Thomas Abraham4d84e972011-10-24 11:47:25 +0200198 ourport = container_of(port, struct s3c24xx_uart_port, port);
199 return ourport->cfg;
Ben Dooksb4975492008-07-03 12:32:51 +0100200}
201
202static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
203 unsigned long ufstat)
204{
205 struct s3c24xx_uart_info *info = ourport->info;
206
207 if (ufstat & info->rx_fifofull)
208 return info->fifosize;
209
210 return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
211}
212
213
214/* ? - where has parity gone?? */
215#define S3C2410_UERSTAT_PARITY (0x1000)
216
217static irqreturn_t
218s3c24xx_serial_rx_chars(int irq, void *dev_id)
219{
220 struct s3c24xx_uart_port *ourport = dev_id;
221 struct uart_port *port = &ourport->port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700222 struct tty_struct *tty = port->state->port.tty;
Ben Dooksb4975492008-07-03 12:32:51 +0100223 unsigned int ufcon, ch, flag, ufstat, uerstat;
224 int max_count = 64;
225
226 while (max_count-- > 0) {
227 ufcon = rd_regl(port, S3C2410_UFCON);
228 ufstat = rd_regl(port, S3C2410_UFSTAT);
229
230 if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
231 break;
232
233 uerstat = rd_regl(port, S3C2410_UERSTAT);
234 ch = rd_regb(port, S3C2410_URXH);
235
236 if (port->flags & UPF_CONS_FLOW) {
237 int txe = s3c24xx_serial_txempty_nofifo(port);
238
239 if (rx_enabled(port)) {
240 if (!txe) {
241 rx_enabled(port) = 0;
242 continue;
243 }
244 } else {
245 if (txe) {
246 ufcon |= S3C2410_UFCON_RESETRX;
247 wr_regl(port, S3C2410_UFCON, ufcon);
248 rx_enabled(port) = 1;
249 goto out;
250 }
251 continue;
252 }
253 }
254
255 /* insert the character into the buffer */
256
257 flag = TTY_NORMAL;
258 port->icount.rx++;
259
260 if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
261 dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
262 ch, uerstat);
263
264 /* check for break */
265 if (uerstat & S3C2410_UERSTAT_BREAK) {
266 dbg("break!\n");
267 port->icount.brk++;
268 if (uart_handle_break(port))
269 goto ignore_char;
270 }
271
272 if (uerstat & S3C2410_UERSTAT_FRAME)
273 port->icount.frame++;
274 if (uerstat & S3C2410_UERSTAT_OVERRUN)
275 port->icount.overrun++;
276
277 uerstat &= port->read_status_mask;
278
279 if (uerstat & S3C2410_UERSTAT_BREAK)
280 flag = TTY_BREAK;
281 else if (uerstat & S3C2410_UERSTAT_PARITY)
282 flag = TTY_PARITY;
283 else if (uerstat & (S3C2410_UERSTAT_FRAME |
284 S3C2410_UERSTAT_OVERRUN))
285 flag = TTY_FRAME;
286 }
287
288 if (uart_handle_sysrq_char(port, ch))
289 goto ignore_char;
290
291 uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN,
292 ch, flag);
293
294 ignore_char:
295 continue;
296 }
297 tty_flip_buffer_push(tty);
298
299 out:
300 return IRQ_HANDLED;
301}
302
303static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
304{
305 struct s3c24xx_uart_port *ourport = id;
306 struct uart_port *port = &ourport->port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700307 struct circ_buf *xmit = &port->state->xmit;
Ben Dooksb4975492008-07-03 12:32:51 +0100308 int count = 256;
309
310 if (port->x_char) {
311 wr_regb(port, S3C2410_UTXH, port->x_char);
312 port->icount.tx++;
313 port->x_char = 0;
314 goto out;
315 }
316
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300317 /* if there isn't anything more to transmit, or the uart is now
Ben Dooksb4975492008-07-03 12:32:51 +0100318 * stopped, disable the uart and exit
319 */
320
321 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
322 s3c24xx_serial_stop_tx(port);
323 goto out;
324 }
325
326 /* try and drain the buffer... */
327
328 while (!uart_circ_empty(xmit) && count-- > 0) {
329 if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
330 break;
331
332 wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
333 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
334 port->icount.tx++;
335 }
336
337 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
338 uart_write_wakeup(port);
339
340 if (uart_circ_empty(xmit))
341 s3c24xx_serial_stop_tx(port);
342
343 out:
344 return IRQ_HANDLED;
345}
346
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530347/* interrupt handler for s3c64xx and later SoC's.*/
348static irqreturn_t s3c64xx_serial_handle_irq(int irq, void *id)
349{
350 struct s3c24xx_uart_port *ourport = id;
351 struct uart_port *port = &ourport->port;
352 unsigned int pend = rd_regl(port, S3C64XX_UINTP);
353 unsigned long flags;
354 irqreturn_t ret = IRQ_HANDLED;
355
356 spin_lock_irqsave(&port->lock, flags);
357 if (pend & S3C64XX_UINTM_RXD_MSK) {
358 ret = s3c24xx_serial_rx_chars(irq, id);
359 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_RXD_MSK);
360 }
361 if (pend & S3C64XX_UINTM_TXD_MSK) {
362 ret = s3c24xx_serial_tx_chars(irq, id);
363 wr_regl(port, S3C64XX_UINTP, S3C64XX_UINTM_TXD_MSK);
364 }
365 spin_unlock_irqrestore(&port->lock, flags);
366 return ret;
367}
368
Ben Dooksb4975492008-07-03 12:32:51 +0100369static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)
370{
371 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
372 unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
373 unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
374
375 if (ufcon & S3C2410_UFCON_FIFOMODE) {
376 if ((ufstat & info->tx_fifomask) != 0 ||
377 (ufstat & info->tx_fifofull))
378 return 0;
379
380 return 1;
381 }
382
383 return s3c24xx_serial_txempty_nofifo(port);
384}
385
386/* no modem control lines */
387static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
388{
389 unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
390
391 if (umstat & S3C2410_UMSTAT_CTS)
392 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
393 else
394 return TIOCM_CAR | TIOCM_DSR;
395}
396
397static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
398{
399 /* todo - possibly remove AFC and do manual CTS */
400}
401
402static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
403{
404 unsigned long flags;
405 unsigned int ucon;
406
407 spin_lock_irqsave(&port->lock, flags);
408
409 ucon = rd_regl(port, S3C2410_UCON);
410
411 if (break_state)
412 ucon |= S3C2410_UCON_SBREAK;
413 else
414 ucon &= ~S3C2410_UCON_SBREAK;
415
416 wr_regl(port, S3C2410_UCON, ucon);
417
418 spin_unlock_irqrestore(&port->lock, flags);
419}
420
421static void s3c24xx_serial_shutdown(struct uart_port *port)
422{
423 struct s3c24xx_uart_port *ourport = to_ourport(port);
424
425 if (ourport->tx_claimed) {
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530426 if (!s3c24xx_serial_has_interrupt_mask(port))
427 free_irq(ourport->tx_irq, ourport);
Ben Dooksb4975492008-07-03 12:32:51 +0100428 tx_enabled(port) = 0;
429 ourport->tx_claimed = 0;
430 }
431
432 if (ourport->rx_claimed) {
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530433 if (!s3c24xx_serial_has_interrupt_mask(port))
434 free_irq(ourport->rx_irq, ourport);
Ben Dooksb4975492008-07-03 12:32:51 +0100435 ourport->rx_claimed = 0;
436 rx_enabled(port) = 0;
437 }
Ben Dooksb4975492008-07-03 12:32:51 +0100438
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530439 /* Clear pending interrupts and mask all interrupts */
440 if (s3c24xx_serial_has_interrupt_mask(port)) {
441 wr_regl(port, S3C64XX_UINTP, 0xf);
442 wr_regl(port, S3C64XX_UINTM, 0xf);
443 }
444}
Ben Dooksb4975492008-07-03 12:32:51 +0100445
446static int s3c24xx_serial_startup(struct uart_port *port)
447{
448 struct s3c24xx_uart_port *ourport = to_ourport(port);
449 int ret;
450
451 dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
452 port->mapbase, port->membase);
453
454 rx_enabled(port) = 1;
455
Ben Dooksb73c289c2008-10-21 14:07:04 +0100456 ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
Ben Dooksb4975492008-07-03 12:32:51 +0100457 s3c24xx_serial_portname(port), ourport);
458
459 if (ret != 0) {
Ben Dooksb73c289c2008-10-21 14:07:04 +0100460 printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100461 return ret;
462 }
463
464 ourport->rx_claimed = 1;
465
466 dbg("requesting tx irq...\n");
467
468 tx_enabled(port) = 1;
469
Ben Dooksb73c289c2008-10-21 14:07:04 +0100470 ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
Ben Dooksb4975492008-07-03 12:32:51 +0100471 s3c24xx_serial_portname(port), ourport);
472
473 if (ret) {
Ben Dooksb73c289c2008-10-21 14:07:04 +0100474 printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
Ben Dooksb4975492008-07-03 12:32:51 +0100475 goto err;
476 }
477
478 ourport->tx_claimed = 1;
479
480 dbg("s3c24xx_serial_startup ok\n");
481
482 /* the port reset code should have done the correct
483 * register setup for the port controls */
484
485 return ret;
486
487 err:
488 s3c24xx_serial_shutdown(port);
489 return ret;
490}
491
Thomas Abraham88bb4ea2011-08-10 15:51:19 +0530492static int s3c64xx_serial_startup(struct uart_port *port)
493{
494 struct s3c24xx_uart_port *ourport = to_ourport(port);
495 int ret;
496
497 dbg("s3c64xx_serial_startup: port=%p (%08lx,%p)\n",
498 port->mapbase, port->membase);
499
500 ret = request_irq(port->irq, s3c64xx_serial_handle_irq, IRQF_SHARED,
501 s3c24xx_serial_portname(port), ourport);
502 if (ret) {
503 printk(KERN_ERR "cannot get irq %d\n", port->irq);
504 return ret;
505 }
506
507 /* For compatibility with s3c24xx Soc's */
508 rx_enabled(port) = 1;
509 ourport->rx_claimed = 1;
510 tx_enabled(port) = 0;
511 ourport->tx_claimed = 1;
512
513 /* Enable Rx Interrupt */
514 __clear_bit(S3C64XX_UINTM_RXD, portaddrl(port, S3C64XX_UINTM));
515 dbg("s3c64xx_serial_startup ok\n");
516 return ret;
517}
518
Ben Dooksb4975492008-07-03 12:32:51 +0100519/* power power management control */
520
521static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
522 unsigned int old)
523{
524 struct s3c24xx_uart_port *ourport = to_ourport(port);
525
Ben Dooks30555472008-10-21 14:06:36 +0100526 ourport->pm_level = level;
527
Ben Dooksb4975492008-07-03 12:32:51 +0100528 switch (level) {
529 case 3:
530 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
531 clk_disable(ourport->baudclk);
532
533 clk_disable(ourport->clk);
534 break;
535
536 case 0:
537 clk_enable(ourport->clk);
538
539 if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
540 clk_enable(ourport->baudclk);
541
542 break;
543 default:
544 printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
545 }
546}
547
548/* baud rate calculation
549 *
550 * The UARTs on the S3C2410/S3C2440 can take their clocks from a number
551 * of different sources, including the peripheral clock ("pclk") and an
552 * external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
553 * with a programmable extra divisor.
554 *
555 * The following code goes through the clock sources, and calculates the
556 * baud clocks (and the resultant actual baud rates) and then tries to
557 * pick the closest one and select that.
558 *
559*/
560
561
562#define MAX_CLKS (8)
563
564static struct s3c24xx_uart_clksrc tmp_clksrc = {
565 .name = "pclk",
566 .min_baud = 0,
567 .max_baud = 0,
568 .divisor = 1,
569};
570
571static inline int
572s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
573{
574 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
575
576 return (info->get_clksrc)(port, c);
577}
578
579static inline int
580s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
581{
582 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
583
584 return (info->set_clksrc)(port, c);
585}
586
587struct baud_calc {
588 struct s3c24xx_uart_clksrc *clksrc;
589 unsigned int calc;
Ben Dooks090f8482008-12-12 00:24:21 +0000590 unsigned int divslot;
Ben Dooksb4975492008-07-03 12:32:51 +0100591 unsigned int quot;
592 struct clk *src;
593};
594
595static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
596 struct uart_port *port,
597 struct s3c24xx_uart_clksrc *clksrc,
598 unsigned int baud)
599{
Ben Dooks090f8482008-12-12 00:24:21 +0000600 struct s3c24xx_uart_port *ourport = to_ourport(port);
Ben Dooksb4975492008-07-03 12:32:51 +0100601 unsigned long rate;
602
603 calc->src = clk_get(port->dev, clksrc->name);
604 if (calc->src == NULL || IS_ERR(calc->src))
605 return 0;
606
607 rate = clk_get_rate(calc->src);
608 rate /= clksrc->divisor;
609
610 calc->clksrc = clksrc;
Ben Dooks090f8482008-12-12 00:24:21 +0000611
612 if (ourport->info->has_divslot) {
613 unsigned long div = rate / baud;
614
615 /* The UDIVSLOT register on the newer UARTs allows us to
616 * get a divisor adjustment of 1/16th on the baud clock.
617 *
618 * We don't keep the UDIVSLOT value (the 16ths we calculated
619 * by not multiplying the baud by 16) as it is easy enough
620 * to recalculate.
621 */
622
623 calc->quot = div / 16;
624 calc->calc = rate / div;
625 } else {
626 calc->quot = (rate + (8 * baud)) / (16 * baud);
627 calc->calc = (rate / (calc->quot * 16));
628 }
Ben Dooksb4975492008-07-03 12:32:51 +0100629
630 calc->quot--;
631 return 1;
632}
633
634static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
635 struct s3c24xx_uart_clksrc **clksrc,
636 struct clk **clk,
637 unsigned int baud)
638{
639 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
640 struct s3c24xx_uart_clksrc *clkp;
641 struct baud_calc res[MAX_CLKS];
642 struct baud_calc *resptr, *best, *sptr;
643 int i;
644
645 clkp = cfg->clocks;
646 best = NULL;
647
648 if (cfg->clocks_size < 2) {
649 if (cfg->clocks_size == 0)
650 clkp = &tmp_clksrc;
651
652 /* check to see if we're sourcing fclk, and if so we're
653 * going to have to update the clock source
654 */
655
656 if (strcmp(clkp->name, "fclk") == 0) {
657 struct s3c24xx_uart_clksrc src;
658
659 s3c24xx_serial_getsource(port, &src);
660
661 /* check that the port already using fclk, and if
662 * not, then re-select fclk
663 */
664
665 if (strcmp(src.name, clkp->name) == 0) {
666 s3c24xx_serial_setsource(port, clkp);
667 s3c24xx_serial_getsource(port, &src);
668 }
669
670 clkp->divisor = src.divisor;
671 }
672
673 s3c24xx_serial_calcbaud(res, port, clkp, baud);
674 best = res;
675 resptr = best + 1;
676 } else {
677 resptr = res;
678
679 for (i = 0; i < cfg->clocks_size; i++, clkp++) {
680 if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
681 resptr++;
682 }
683 }
684
685 /* ok, we now need to select the best clock we found */
686
687 if (!best) {
688 unsigned int deviation = (1<<30)|((1<<30)-1);
689 int calc_deviation;
690
691 for (sptr = res; sptr < resptr; sptr++) {
692 calc_deviation = baud - sptr->calc;
693 if (calc_deviation < 0)
694 calc_deviation = -calc_deviation;
695
696 if (calc_deviation < deviation) {
697 best = sptr;
698 deviation = calc_deviation;
699 }
700 }
701 }
702
703 /* store results to pass back */
704
705 *clksrc = best->clksrc;
706 *clk = best->src;
707
708 return best->quot;
709}
710
Ben Dooks090f8482008-12-12 00:24:21 +0000711/* udivslot_table[]
712 *
713 * This table takes the fractional value of the baud divisor and gives
714 * the recommended setting for the UDIVSLOT register.
715 */
716static u16 udivslot_table[16] = {
717 [0] = 0x0000,
718 [1] = 0x0080,
719 [2] = 0x0808,
720 [3] = 0x0888,
721 [4] = 0x2222,
722 [5] = 0x4924,
723 [6] = 0x4A52,
724 [7] = 0x54AA,
725 [8] = 0x5555,
726 [9] = 0xD555,
727 [10] = 0xD5D5,
728 [11] = 0xDDD5,
729 [12] = 0xDDDD,
730 [13] = 0xDFDD,
731 [14] = 0xDFDF,
732 [15] = 0xFFDF,
733};
734
Ben Dooksb4975492008-07-03 12:32:51 +0100735static void s3c24xx_serial_set_termios(struct uart_port *port,
736 struct ktermios *termios,
737 struct ktermios *old)
738{
739 struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
740 struct s3c24xx_uart_port *ourport = to_ourport(port);
741 struct s3c24xx_uart_clksrc *clksrc = NULL;
742 struct clk *clk = NULL;
743 unsigned long flags;
744 unsigned int baud, quot;
745 unsigned int ulcon;
746 unsigned int umcon;
Ben Dooks090f8482008-12-12 00:24:21 +0000747 unsigned int udivslot = 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100748
749 /*
750 * We don't support modem control lines.
751 */
752 termios->c_cflag &= ~(HUPCL | CMSPAR);
753 termios->c_cflag |= CLOCAL;
754
755 /*
756 * Ask the core to calculate the divisor for us.
757 */
758
759 baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);
760
761 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
762 quot = port->custom_divisor;
763 else
764 quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
765
766 /* check to see if we need to change clock source */
767
768 if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
Ben Dooks090f8482008-12-12 00:24:21 +0000769 dbg("selecting clock %p\n", clk);
Ben Dooksb4975492008-07-03 12:32:51 +0100770 s3c24xx_serial_setsource(port, clksrc);
771
772 if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
773 clk_disable(ourport->baudclk);
774 ourport->baudclk = NULL;
775 }
776
777 clk_enable(clk);
778
779 ourport->clksrc = clksrc;
780 ourport->baudclk = clk;
Ben Dooks30555472008-10-21 14:06:36 +0100781 ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
Ben Dooksb4975492008-07-03 12:32:51 +0100782 }
783
Ben Dooks090f8482008-12-12 00:24:21 +0000784 if (ourport->info->has_divslot) {
785 unsigned int div = ourport->baudclk_rate / baud;
786
Jongpill Lee8b526ae2010-07-16 10:19:41 +0900787 if (cfg->has_fracval) {
788 udivslot = (div & 15);
789 dbg("fracval = %04x\n", udivslot);
790 } else {
791 udivslot = udivslot_table[div & 15];
792 dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
793 }
Ben Dooks090f8482008-12-12 00:24:21 +0000794 }
795
Ben Dooksb4975492008-07-03 12:32:51 +0100796 switch (termios->c_cflag & CSIZE) {
797 case CS5:
798 dbg("config: 5bits/char\n");
799 ulcon = S3C2410_LCON_CS5;
800 break;
801 case CS6:
802 dbg("config: 6bits/char\n");
803 ulcon = S3C2410_LCON_CS6;
804 break;
805 case CS7:
806 dbg("config: 7bits/char\n");
807 ulcon = S3C2410_LCON_CS7;
808 break;
809 case CS8:
810 default:
811 dbg("config: 8bits/char\n");
812 ulcon = S3C2410_LCON_CS8;
813 break;
814 }
815
816 /* preserve original lcon IR settings */
817 ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
818
819 if (termios->c_cflag & CSTOPB)
820 ulcon |= S3C2410_LCON_STOPB;
821
822 umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
823
824 if (termios->c_cflag & PARENB) {
825 if (termios->c_cflag & PARODD)
826 ulcon |= S3C2410_LCON_PODD;
827 else
828 ulcon |= S3C2410_LCON_PEVEN;
829 } else {
830 ulcon |= S3C2410_LCON_PNONE;
831 }
832
833 spin_lock_irqsave(&port->lock, flags);
834
Ben Dooks090f8482008-12-12 00:24:21 +0000835 dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
836 ulcon, quot, udivslot);
Ben Dooksb4975492008-07-03 12:32:51 +0100837
838 wr_regl(port, S3C2410_ULCON, ulcon);
839 wr_regl(port, S3C2410_UBRDIV, quot);
840 wr_regl(port, S3C2410_UMCON, umcon);
841
Ben Dooks090f8482008-12-12 00:24:21 +0000842 if (ourport->info->has_divslot)
843 wr_regl(port, S3C2443_DIVSLOT, udivslot);
844
Ben Dooksb4975492008-07-03 12:32:51 +0100845 dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
846 rd_regl(port, S3C2410_ULCON),
847 rd_regl(port, S3C2410_UCON),
848 rd_regl(port, S3C2410_UFCON));
849
850 /*
851 * Update the per-port timeout.
852 */
853 uart_update_timeout(port, termios->c_cflag, baud);
854
855 /*
856 * Which character status flags are we interested in?
857 */
858 port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
859 if (termios->c_iflag & INPCK)
860 port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
861
862 /*
863 * Which character status flags should we ignore?
864 */
865 port->ignore_status_mask = 0;
866 if (termios->c_iflag & IGNPAR)
867 port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
868 if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
869 port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
870
871 /*
872 * Ignore all characters if CREAD is not set.
873 */
874 if ((termios->c_cflag & CREAD) == 0)
875 port->ignore_status_mask |= RXSTAT_DUMMY_READ;
876
877 spin_unlock_irqrestore(&port->lock, flags);
878}
879
880static const char *s3c24xx_serial_type(struct uart_port *port)
881{
882 switch (port->type) {
883 case PORT_S3C2410:
884 return "S3C2410";
885 case PORT_S3C2440:
886 return "S3C2440";
887 case PORT_S3C2412:
888 return "S3C2412";
Ben Dooksb690ace2008-10-21 14:07:03 +0100889 case PORT_S3C6400:
890 return "S3C6400/10";
Ben Dooksb4975492008-07-03 12:32:51 +0100891 default:
892 return NULL;
893 }
894}
895
896#define MAP_SIZE (0x100)
897
898static void s3c24xx_serial_release_port(struct uart_port *port)
899{
900 release_mem_region(port->mapbase, MAP_SIZE);
901}
902
903static int s3c24xx_serial_request_port(struct uart_port *port)
904{
905 const char *name = s3c24xx_serial_portname(port);
906 return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
907}
908
909static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
910{
911 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
912
913 if (flags & UART_CONFIG_TYPE &&
914 s3c24xx_serial_request_port(port) == 0)
915 port->type = info->type;
916}
917
918/*
919 * verify the new serial_struct (for TIOCSSERIAL).
920 */
921static int
922s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
923{
924 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
925
926 if (ser->type != PORT_UNKNOWN && ser->type != info->type)
927 return -EINVAL;
928
929 return 0;
930}
931
932
933#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
934
935static struct console s3c24xx_serial_console;
936
937#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
938#else
939#define S3C24XX_SERIAL_CONSOLE NULL
940#endif
941
942static struct uart_ops s3c24xx_serial_ops = {
943 .pm = s3c24xx_serial_pm,
944 .tx_empty = s3c24xx_serial_tx_empty,
945 .get_mctrl = s3c24xx_serial_get_mctrl,
946 .set_mctrl = s3c24xx_serial_set_mctrl,
947 .stop_tx = s3c24xx_serial_stop_tx,
948 .start_tx = s3c24xx_serial_start_tx,
949 .stop_rx = s3c24xx_serial_stop_rx,
950 .enable_ms = s3c24xx_serial_enable_ms,
951 .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,
960};
961
Ben Dooksb4975492008-07-03 12:32:51 +0100962static struct uart_driver s3c24xx_uart_drv = {
963 .owner = THIS_MODULE,
Darius Augulis2cf0c582011-01-12 14:50:51 +0900964 .driver_name = "s3c2410_serial",
Ben Dooksbdd49152008-11-03 19:51:42 +0000965 .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
Ben Dooksb4975492008-07-03 12:32:51 +0100966 .cons = S3C24XX_SERIAL_CONSOLE,
Darius Augulis2cf0c582011-01-12 14:50:51 +0900967 .dev_name = S3C24XX_SERIAL_NAME,
Ben Dooksb4975492008-07-03 12:32:51 +0100968 .major = S3C24XX_SERIAL_MAJOR,
969 .minor = S3C24XX_SERIAL_MINOR,
970};
971
Ben Dooks03d5e772008-11-03 09:21:23 +0000972static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
Ben Dooksb4975492008-07-03 12:32:51 +0100973 [0] = {
974 .port = {
975 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
976 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +0100977 .uartclk = 0,
978 .fifosize = 16,
979 .ops = &s3c24xx_serial_ops,
980 .flags = UPF_BOOT_AUTOCONF,
981 .line = 0,
982 }
983 },
984 [1] = {
985 .port = {
986 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
987 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +0100988 .uartclk = 0,
989 .fifosize = 16,
990 .ops = &s3c24xx_serial_ops,
991 .flags = UPF_BOOT_AUTOCONF,
992 .line = 1,
993 }
994 },
Ben Dooks03d5e772008-11-03 09:21:23 +0000995#if CONFIG_SERIAL_SAMSUNG_UARTS > 2
Ben Dooksb4975492008-07-03 12:32:51 +0100996
997 [2] = {
998 .port = {
999 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
1000 .iotype = UPIO_MEM,
Ben Dooksb4975492008-07-03 12:32:51 +01001001 .uartclk = 0,
1002 .fifosize = 16,
1003 .ops = &s3c24xx_serial_ops,
1004 .flags = UPF_BOOT_AUTOCONF,
1005 .line = 2,
1006 }
Ben Dooks03d5e772008-11-03 09:21:23 +00001007 },
1008#endif
1009#if CONFIG_SERIAL_SAMSUNG_UARTS > 3
1010 [3] = {
1011 .port = {
1012 .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
1013 .iotype = UPIO_MEM,
Ben Dooks03d5e772008-11-03 09:21:23 +00001014 .uartclk = 0,
1015 .fifosize = 16,
1016 .ops = &s3c24xx_serial_ops,
1017 .flags = UPF_BOOT_AUTOCONF,
1018 .line = 3,
1019 }
Ben Dooksb4975492008-07-03 12:32:51 +01001020 }
1021#endif
1022};
1023
1024/* s3c24xx_serial_resetport
1025 *
1026 * wrapper to call the specific reset for this port (reset the fifos
1027 * and the settings)
1028*/
1029
1030static inline int s3c24xx_serial_resetport(struct uart_port *port,
1031 struct s3c2410_uartcfg *cfg)
1032{
1033 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1034
1035 return (info->reset_port)(port, cfg);
1036}
1037
Ben Dooks30555472008-10-21 14:06:36 +01001038
1039#ifdef CONFIG_CPU_FREQ
1040
1041static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
1042 unsigned long val, void *data)
1043{
1044 struct s3c24xx_uart_port *port;
1045 struct uart_port *uport;
1046
1047 port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
1048 uport = &port->port;
1049
1050 /* check to see if port is enabled */
1051
1052 if (port->pm_level != 0)
1053 return 0;
1054
1055 /* try and work out if the baudrate is changing, we can detect
1056 * a change in rate, but we do not have support for detecting
1057 * a disturbance in the clock-rate over the change.
1058 */
1059
1060 if (IS_ERR(port->clk))
1061 goto exit;
1062
1063 if (port->baudclk_rate == clk_get_rate(port->clk))
1064 goto exit;
1065
1066 if (val == CPUFREQ_PRECHANGE) {
1067 /* we should really shut the port down whilst the
1068 * frequency change is in progress. */
1069
1070 } else if (val == CPUFREQ_POSTCHANGE) {
1071 struct ktermios *termios;
1072 struct tty_struct *tty;
1073
Alan Coxebd2c8f2009-09-19 13:13:28 -07001074 if (uport->state == NULL)
Ben Dooks30555472008-10-21 14:06:36 +01001075 goto exit;
Ben Dooks30555472008-10-21 14:06:36 +01001076
Alan Coxebd2c8f2009-09-19 13:13:28 -07001077 tty = uport->state->port.tty;
Ben Dooks30555472008-10-21 14:06:36 +01001078
Ben Dooks7de40c22008-12-14 23:11:02 +00001079 if (tty == NULL)
Ben Dooks30555472008-10-21 14:06:36 +01001080 goto exit;
Ben Dooks30555472008-10-21 14:06:36 +01001081
1082 termios = tty->termios;
1083
1084 if (termios == NULL) {
1085 printk(KERN_WARNING "%s: no termios?\n", __func__);
1086 goto exit;
1087 }
1088
1089 s3c24xx_serial_set_termios(uport, termios, NULL);
1090 }
1091
1092 exit:
1093 return 0;
1094}
1095
1096static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1097{
1098 port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
1099
1100 return cpufreq_register_notifier(&port->freq_transition,
1101 CPUFREQ_TRANSITION_NOTIFIER);
1102}
1103
1104static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1105{
1106 cpufreq_unregister_notifier(&port->freq_transition,
1107 CPUFREQ_TRANSITION_NOTIFIER);
1108}
1109
1110#else
1111static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
1112{
1113 return 0;
1114}
1115
1116static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
1117{
1118}
1119#endif
1120
Ben Dooksb4975492008-07-03 12:32:51 +01001121/* s3c24xx_serial_init_port
1122 *
1123 * initialise a single serial port from the platform device given
1124 */
1125
1126static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
1127 struct s3c24xx_uart_info *info,
1128 struct platform_device *platdev)
1129{
1130 struct uart_port *port = &ourport->port;
Thomas Abraham4d84e972011-10-24 11:47:25 +02001131 struct s3c2410_uartcfg *cfg = platdev->dev.platform_data;
Ben Dooksb4975492008-07-03 12:32:51 +01001132 struct resource *res;
1133 int ret;
1134
1135 dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
1136
1137 if (platdev == NULL)
1138 return -ENODEV;
1139
Ben Dooksb4975492008-07-03 12:32:51 +01001140 if (port->mapbase != 0)
1141 return 0;
1142
Thomas Abraham4d84e972011-10-24 11:47:25 +02001143 /*
1144 * If platform data is supplied, keep a copy of the location of
1145 * platform data in the driver's private data.
1146 */
1147 if (cfg)
1148 ourport->cfg = cfg;
1149
Ben Dooksbdd49152008-11-03 19:51:42 +00001150 if (cfg->hwport > CONFIG_SERIAL_SAMSUNG_UARTS) {
1151 printk(KERN_ERR "%s: port %d bigger than %d\n", __func__,
1152 cfg->hwport, CONFIG_SERIAL_SAMSUNG_UARTS);
1153 return -ERANGE;
1154 }
Ben Dooksb4975492008-07-03 12:32:51 +01001155
1156 /* setup info for port */
1157 port->dev = &platdev->dev;
1158 ourport->info = info;
1159
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301160 /* Startup sequence is different for s3c64xx and higher SoC's */
1161 if (s3c24xx_serial_has_interrupt_mask(port))
1162 s3c24xx_serial_ops.startup = s3c64xx_serial_startup;
1163
Ben Dooksb4975492008-07-03 12:32:51 +01001164 /* copy the info in from provided structure */
1165 ourport->port.fifosize = info->fifosize;
1166
1167 dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
1168
1169 port->uartclk = 1;
1170
1171 if (cfg->uart_flags & UPF_CONS_FLOW) {
1172 dbg("s3c24xx_serial_init_port: enabling flow control\n");
1173 port->flags |= UPF_CONS_FLOW;
1174 }
1175
1176 /* sort our the physical and virtual addresses for each UART */
1177
1178 res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
1179 if (res == NULL) {
1180 printk(KERN_ERR "failed to find memory resource for uart\n");
1181 return -EINVAL;
1182 }
1183
1184 dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
1185
Ben Dooksb690ace2008-10-21 14:07:03 +01001186 port->mapbase = res->start;
Kukjin Kim2555e662010-09-01 15:13:44 +09001187 port->membase = S3C_VA_UART + (res->start & 0xfffff);
Ben Dooksb4975492008-07-03 12:32:51 +01001188 ret = platform_get_irq(platdev, 0);
1189 if (ret < 0)
1190 port->irq = 0;
Ben Dooksb73c289c2008-10-21 14:07:04 +01001191 else {
Ben Dooksb4975492008-07-03 12:32:51 +01001192 port->irq = ret;
Ben Dooksb73c289c2008-10-21 14:07:04 +01001193 ourport->rx_irq = ret;
1194 ourport->tx_irq = ret + 1;
1195 }
1196
1197 ret = platform_get_irq(platdev, 1);
1198 if (ret > 0)
1199 ourport->tx_irq = ret;
Ben Dooksb4975492008-07-03 12:32:51 +01001200
1201 ourport->clk = clk_get(&platdev->dev, "uart");
1202
Thomas Abraham88bb4ea2011-08-10 15:51:19 +05301203 /* Keep all interrupts masked and cleared */
1204 if (s3c24xx_serial_has_interrupt_mask(port)) {
1205 wr_regl(port, S3C64XX_UINTM, 0xf);
1206 wr_regl(port, S3C64XX_UINTP, 0xf);
1207 wr_regl(port, S3C64XX_UINTSP, 0xf);
1208 }
1209
Ben Dooksb73c289c2008-10-21 14:07:04 +01001210 dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
1211 port->mapbase, port->membase, port->irq,
1212 ourport->rx_irq, ourport->tx_irq, port->uartclk);
Ben Dooksb4975492008-07-03 12:32:51 +01001213
1214 /* reset the fifos (and setup the uart) */
1215 s3c24xx_serial_resetport(port, cfg);
1216 return 0;
1217}
1218
1219static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
1220 struct device_attribute *attr,
1221 char *buf)
1222{
1223 struct uart_port *port = s3c24xx_dev_to_port(dev);
1224 struct s3c24xx_uart_port *ourport = to_ourport(port);
1225
1226 return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->clksrc->name);
1227}
1228
1229static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL);
1230
1231/* Device driver serial port probe */
1232
1233static int probe_index;
1234
1235int s3c24xx_serial_probe(struct platform_device *dev,
1236 struct s3c24xx_uart_info *info)
1237{
1238 struct s3c24xx_uart_port *ourport;
1239 int ret;
1240
1241 dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
1242
1243 ourport = &s3c24xx_serial_ports[probe_index];
1244 probe_index++;
1245
1246 dbg("%s: initialising port %p...\n", __func__, ourport);
1247
1248 ret = s3c24xx_serial_init_port(ourport, info, dev);
1249 if (ret < 0)
1250 goto probe_err;
1251
1252 dbg("%s: adding port\n", __func__);
1253 uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
1254 platform_set_drvdata(dev, &ourport->port);
1255
1256 ret = device_create_file(&dev->dev, &dev_attr_clock_source);
1257 if (ret < 0)
1258 printk(KERN_ERR "%s: failed to add clksrc attr.\n", __func__);
1259
Ben Dooks30555472008-10-21 14:06:36 +01001260 ret = s3c24xx_serial_cpufreq_register(ourport);
1261 if (ret < 0)
1262 dev_err(&dev->dev, "failed to add cpufreq notifier\n");
1263
Ben Dooksb4975492008-07-03 12:32:51 +01001264 return 0;
1265
1266 probe_err:
1267 return ret;
1268}
1269
1270EXPORT_SYMBOL_GPL(s3c24xx_serial_probe);
1271
Peter Korsgaard90ceb9642009-06-22 18:42:49 +01001272int __devexit s3c24xx_serial_remove(struct platform_device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01001273{
1274 struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
1275
1276 if (port) {
Ben Dooks30555472008-10-21 14:06:36 +01001277 s3c24xx_serial_cpufreq_deregister(to_ourport(port));
Ben Dooksb4975492008-07-03 12:32:51 +01001278 device_remove_file(&dev->dev, &dev_attr_clock_source);
1279 uart_remove_one_port(&s3c24xx_uart_drv, port);
1280 }
1281
1282 return 0;
1283}
1284
1285EXPORT_SYMBOL_GPL(s3c24xx_serial_remove);
1286
1287/* UART power management code */
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001288#ifdef CONFIG_PM_SLEEP
1289static int s3c24xx_serial_suspend(struct device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01001290{
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001291 struct uart_port *port = s3c24xx_dev_to_port(dev);
Ben Dooksb4975492008-07-03 12:32:51 +01001292
1293 if (port)
1294 uart_suspend_port(&s3c24xx_uart_drv, port);
1295
1296 return 0;
1297}
1298
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001299static int s3c24xx_serial_resume(struct device *dev)
Ben Dooksb4975492008-07-03 12:32:51 +01001300{
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001301 struct uart_port *port = s3c24xx_dev_to_port(dev);
Ben Dooksb4975492008-07-03 12:32:51 +01001302 struct s3c24xx_uart_port *ourport = to_ourport(port);
1303
1304 if (port) {
1305 clk_enable(ourport->clk);
1306 s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
1307 clk_disable(ourport->clk);
1308
1309 uart_resume_port(&s3c24xx_uart_drv, port);
1310 }
1311
1312 return 0;
1313}
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001314
1315static const struct dev_pm_ops s3c24xx_serial_pm_ops = {
1316 .suspend = s3c24xx_serial_suspend,
1317 .resume = s3c24xx_serial_resume,
1318};
Kukjin Kimb882fc12011-07-28 08:50:38 +09001319#define SERIAL_SAMSUNG_PM_OPS (&s3c24xx_serial_pm_ops)
1320
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001321#else /* !CONFIG_PM_SLEEP */
Kukjin Kimb882fc12011-07-28 08:50:38 +09001322
1323#define SERIAL_SAMSUNG_PM_OPS NULL
MyungJoo Hamaef7fe52011-06-29 15:28:24 +09001324#endif /* CONFIG_PM_SLEEP */
Ben Dooksb4975492008-07-03 12:32:51 +01001325
1326int s3c24xx_serial_init(struct platform_driver *drv,
1327 struct s3c24xx_uart_info *info)
1328{
1329 dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
Kukjin Kimb882fc12011-07-28 08:50:38 +09001330
1331 drv->driver.pm = SERIAL_SAMSUNG_PM_OPS;
Ben Dooksb4975492008-07-03 12:32:51 +01001332
1333 return platform_driver_register(drv);
1334}
1335
1336EXPORT_SYMBOL_GPL(s3c24xx_serial_init);
1337
1338/* module initialisation code */
1339
1340static int __init s3c24xx_serial_modinit(void)
1341{
1342 int ret;
1343
1344 ret = uart_register_driver(&s3c24xx_uart_drv);
1345 if (ret < 0) {
1346 printk(KERN_ERR "failed to register UART driver\n");
1347 return -1;
1348 }
1349
1350 return 0;
1351}
1352
1353static void __exit s3c24xx_serial_modexit(void)
1354{
1355 uart_unregister_driver(&s3c24xx_uart_drv);
1356}
1357
1358module_init(s3c24xx_serial_modinit);
1359module_exit(s3c24xx_serial_modexit);
1360
1361/* Console code */
1362
1363#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
1364
1365static struct uart_port *cons_uart;
1366
1367static int
1368s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
1369{
1370 struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
1371 unsigned long ufstat, utrstat;
1372
1373 if (ufcon & S3C2410_UFCON_FIFOMODE) {
Uwe Kleine-König9ddc5b62010-01-20 17:02:24 +01001374 /* fifo mode - check amount of data in fifo registers... */
Ben Dooksb4975492008-07-03 12:32:51 +01001375
1376 ufstat = rd_regl(port, S3C2410_UFSTAT);
1377 return (ufstat & info->tx_fifofull) ? 0 : 1;
1378 }
1379
1380 /* in non-fifo mode, we go and use the tx buffer empty */
1381
1382 utrstat = rd_regl(port, S3C2410_UTRSTAT);
1383 return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
1384}
1385
1386static void
1387s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
1388{
1389 unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
1390 while (!s3c24xx_serial_console_txrdy(port, ufcon))
1391 barrier();
1392 wr_regb(cons_uart, S3C2410_UTXH, ch);
1393}
1394
1395static void
1396s3c24xx_serial_console_write(struct console *co, const char *s,
1397 unsigned int count)
1398{
1399 uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
1400}
1401
1402static void __init
1403s3c24xx_serial_get_options(struct uart_port *port, int *baud,
1404 int *parity, int *bits)
1405{
1406 struct s3c24xx_uart_clksrc clksrc;
1407 struct clk *clk;
1408 unsigned int ulcon;
1409 unsigned int ucon;
1410 unsigned int ubrdiv;
1411 unsigned long rate;
1412
1413 ulcon = rd_regl(port, S3C2410_ULCON);
1414 ucon = rd_regl(port, S3C2410_UCON);
1415 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
1416
1417 dbg("s3c24xx_serial_get_options: port=%p\n"
1418 "registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
1419 port, ulcon, ucon, ubrdiv);
1420
1421 if ((ucon & 0xf) != 0) {
1422 /* consider the serial port configured if the tx/rx mode set */
1423
1424 switch (ulcon & S3C2410_LCON_CSMASK) {
1425 case S3C2410_LCON_CS5:
1426 *bits = 5;
1427 break;
1428 case S3C2410_LCON_CS6:
1429 *bits = 6;
1430 break;
1431 case S3C2410_LCON_CS7:
1432 *bits = 7;
1433 break;
1434 default:
1435 case S3C2410_LCON_CS8:
1436 *bits = 8;
1437 break;
1438 }
1439
1440 switch (ulcon & S3C2410_LCON_PMASK) {
1441 case S3C2410_LCON_PEVEN:
1442 *parity = 'e';
1443 break;
1444
1445 case S3C2410_LCON_PODD:
1446 *parity = 'o';
1447 break;
1448
1449 case S3C2410_LCON_PNONE:
1450 default:
1451 *parity = 'n';
1452 }
1453
1454 /* now calculate the baud rate */
1455
1456 s3c24xx_serial_getsource(port, &clksrc);
1457
1458 clk = clk_get(port->dev, clksrc.name);
1459 if (!IS_ERR(clk) && clk != NULL)
1460 rate = clk_get_rate(clk) / clksrc.divisor;
1461 else
1462 rate = 1;
1463
1464
1465 *baud = rate / (16 * (ubrdiv + 1));
1466 dbg("calculated baud %d\n", *baud);
1467 }
1468
1469}
1470
1471/* s3c24xx_serial_init_ports
1472 *
1473 * initialise the serial ports from the machine provided initialisation
1474 * data.
1475*/
1476
Thomas Abraham51fe5222010-01-14 15:05:38 +09001477static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info **info)
Ben Dooksb4975492008-07-03 12:32:51 +01001478{
1479 struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
1480 struct platform_device **platdev_ptr;
1481 int i;
1482
1483 dbg("s3c24xx_serial_init_ports: initialising ports...\n");
1484
1485 platdev_ptr = s3c24xx_uart_devs;
1486
Ben Dooks03d5e772008-11-03 09:21:23 +00001487 for (i = 0; i < CONFIG_SERIAL_SAMSUNG_UARTS; i++, ptr++, platdev_ptr++) {
Thomas Abraham51fe5222010-01-14 15:05:38 +09001488 s3c24xx_serial_init_port(ptr, info[i], *platdev_ptr);
Ben Dooksb4975492008-07-03 12:32:51 +01001489 }
1490
1491 return 0;
1492}
1493
1494static int __init
1495s3c24xx_serial_console_setup(struct console *co, char *options)
1496{
1497 struct uart_port *port;
1498 int baud = 9600;
1499 int bits = 8;
1500 int parity = 'n';
1501 int flow = 'n';
1502
1503 dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
1504 co, co->index, options);
1505
1506 /* is this a valid port */
1507
Ben Dooks03d5e772008-11-03 09:21:23 +00001508 if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
Ben Dooksb4975492008-07-03 12:32:51 +01001509 co->index = 0;
1510
1511 port = &s3c24xx_serial_ports[co->index].port;
1512
1513 /* is the port configured? */
1514
Thomas Abrahamee430f12011-06-14 19:12:26 +09001515 if (port->mapbase == 0x0)
1516 return -ENODEV;
Ben Dooksb4975492008-07-03 12:32:51 +01001517
1518 cons_uart = port;
1519
1520 dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
1521
1522 /*
1523 * Check whether an invalid uart number has been specified, and
1524 * if so, search for the first available port that does have
1525 * console support.
1526 */
1527 if (options)
1528 uart_parse_options(options, &baud, &parity, &bits, &flow);
1529 else
1530 s3c24xx_serial_get_options(port, &baud, &parity, &bits);
1531
1532 dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
1533
1534 return uart_set_options(port, co, baud, parity, bits, flow);
1535}
1536
1537/* s3c24xx_serial_initconsole
1538 *
1539 * initialise the console from one of the uart drivers
1540*/
1541
1542static struct console s3c24xx_serial_console = {
1543 .name = S3C24XX_SERIAL_NAME,
1544 .device = uart_console_device,
1545 .flags = CON_PRINTBUFFER,
1546 .index = -1,
1547 .write = s3c24xx_serial_console_write,
Thomas Abraham5822a5d2011-06-14 19:12:26 +09001548 .setup = s3c24xx_serial_console_setup,
1549 .data = &s3c24xx_uart_drv,
Ben Dooksb4975492008-07-03 12:32:51 +01001550};
1551
1552int s3c24xx_serial_initconsole(struct platform_driver *drv,
Thomas Abraham51fe5222010-01-14 15:05:38 +09001553 struct s3c24xx_uart_info **info)
Ben Dooksb4975492008-07-03 12:32:51 +01001554
1555{
1556 struct platform_device *dev = s3c24xx_uart_devs[0];
1557
1558 dbg("s3c24xx_serial_initconsole\n");
1559
1560 /* select driver based on the cpu */
1561
1562 if (dev == NULL) {
1563 printk(KERN_ERR "s3c24xx: no devices for console init\n");
1564 return 0;
1565 }
1566
1567 if (strcmp(dev->name, drv->driver.name) != 0)
1568 return 0;
1569
1570 s3c24xx_serial_console.data = &s3c24xx_uart_drv;
1571 s3c24xx_serial_init_ports(info);
1572
1573 register_console(&s3c24xx_serial_console);
1574 return 0;
1575}
1576
1577#endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
1578
1579MODULE_DESCRIPTION("Samsung SoC Serial port driver");
1580MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1581MODULE_LICENSE("GPL v2");