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