397df507579e943edb72e53358b7f473b5a6a9bf
[sfrench/cifs-2.6.git] / drivers / tty / serial / st-asc.c
1 /*
2  * st-asc.c: ST Asynchronous serial controller (ASC) driver
3  *
4  * Copyright (C) 2003-2013 STMicroelectronics (R&D) Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  */
12
13 #if defined(CONFIG_SERIAL_ST_ASC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
14 #define SUPPORT_SYSRQ
15 #endif
16
17 #include <linux/module.h>
18 #include <linux/serial.h>
19 #include <linux/console.h>
20 #include <linux/sysrq.h>
21 #include <linux/platform_device.h>
22 #include <linux/io.h>
23 #include <linux/irq.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/delay.h>
27 #include <linux/spinlock.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/of.h>
30 #include <linux/of_platform.h>
31 #include <linux/serial_core.h>
32 #include <linux/clk.h>
33 #include <linux/gpio/consumer.h>
34
35 #define DRIVER_NAME "st-asc"
36 #define ASC_SERIAL_NAME "ttyAS"
37 #define ASC_FIFO_SIZE 16
38 #define ASC_MAX_PORTS 8
39
40 struct asc_port {
41         struct uart_port port;
42         struct gpio_desc *rts;
43         struct clk *clk;
44         unsigned int hw_flow_control:1;
45         unsigned int force_m1:1;
46 };
47
48 static struct asc_port asc_ports[ASC_MAX_PORTS];
49 static struct uart_driver asc_uart_driver;
50
51 /*---- UART Register definitions ------------------------------*/
52
53 /* Register offsets */
54
55 #define ASC_BAUDRATE                    0x00
56 #define ASC_TXBUF                       0x04
57 #define ASC_RXBUF                       0x08
58 #define ASC_CTL                         0x0C
59 #define ASC_INTEN                       0x10
60 #define ASC_STA                         0x14
61 #define ASC_GUARDTIME                   0x18
62 #define ASC_TIMEOUT                     0x1C
63 #define ASC_TXRESET                     0x20
64 #define ASC_RXRESET                     0x24
65 #define ASC_RETRIES                     0x28
66
67 /* ASC_RXBUF */
68 #define ASC_RXBUF_PE                    0x100
69 #define ASC_RXBUF_FE                    0x200
70 /**
71  * Some of status comes from higher bits of the character and some come from
72  * the status register. Combining both of them in to single status using dummy
73  * bits.
74  */
75 #define ASC_RXBUF_DUMMY_RX              0x10000
76 #define ASC_RXBUF_DUMMY_BE              0x20000
77 #define ASC_RXBUF_DUMMY_OE              0x40000
78
79 /* ASC_CTL */
80
81 #define ASC_CTL_MODE_MSK                0x0007
82 #define  ASC_CTL_MODE_8BIT              0x0001
83 #define  ASC_CTL_MODE_7BIT_PAR          0x0003
84 #define  ASC_CTL_MODE_9BIT              0x0004
85 #define  ASC_CTL_MODE_8BIT_WKUP         0x0005
86 #define  ASC_CTL_MODE_8BIT_PAR          0x0007
87 #define ASC_CTL_STOP_MSK                0x0018
88 #define  ASC_CTL_STOP_HALFBIT           0x0000
89 #define  ASC_CTL_STOP_1BIT              0x0008
90 #define  ASC_CTL_STOP_1_HALFBIT         0x0010
91 #define  ASC_CTL_STOP_2BIT              0x0018
92 #define ASC_CTL_PARITYODD               0x0020
93 #define ASC_CTL_LOOPBACK                0x0040
94 #define ASC_CTL_RUN                     0x0080
95 #define ASC_CTL_RXENABLE                0x0100
96 #define ASC_CTL_SCENABLE                0x0200
97 #define ASC_CTL_FIFOENABLE              0x0400
98 #define ASC_CTL_CTSENABLE               0x0800
99 #define ASC_CTL_BAUDMODE                0x1000
100
101 /* ASC_GUARDTIME */
102
103 #define ASC_GUARDTIME_MSK               0x00FF
104
105 /* ASC_INTEN */
106
107 #define ASC_INTEN_RBE                   0x0001
108 #define ASC_INTEN_TE                    0x0002
109 #define ASC_INTEN_THE                   0x0004
110 #define ASC_INTEN_PE                    0x0008
111 #define ASC_INTEN_FE                    0x0010
112 #define ASC_INTEN_OE                    0x0020
113 #define ASC_INTEN_TNE                   0x0040
114 #define ASC_INTEN_TOI                   0x0080
115 #define ASC_INTEN_RHF                   0x0100
116
117 /* ASC_RETRIES */
118
119 #define ASC_RETRIES_MSK                 0x00FF
120
121 /* ASC_RXBUF */
122
123 #define ASC_RXBUF_MSK                   0x03FF
124
125 /* ASC_STA */
126
127 #define ASC_STA_RBF                     0x0001
128 #define ASC_STA_TE                      0x0002
129 #define ASC_STA_THE                     0x0004
130 #define ASC_STA_PE                      0x0008
131 #define ASC_STA_FE                      0x0010
132 #define ASC_STA_OE                      0x0020
133 #define ASC_STA_TNE                     0x0040
134 #define ASC_STA_TOI                     0x0080
135 #define ASC_STA_RHF                     0x0100
136 #define ASC_STA_TF                      0x0200
137 #define ASC_STA_NKD                     0x0400
138
139 /* ASC_TIMEOUT */
140
141 #define ASC_TIMEOUT_MSK                 0x00FF
142
143 /* ASC_TXBUF */
144
145 #define ASC_TXBUF_MSK                   0x01FF
146
147 /*---- Inline function definitions ---------------------------*/
148
149 static inline struct asc_port *to_asc_port(struct uart_port *port)
150 {
151         return container_of(port, struct asc_port, port);
152 }
153
154 static inline u32 asc_in(struct uart_port *port, u32 offset)
155 {
156 #ifdef readl_relaxed
157         return readl_relaxed(port->membase + offset);
158 #else
159         return readl(port->membase + offset);
160 #endif
161 }
162
163 static inline void asc_out(struct uart_port *port, u32 offset, u32 value)
164 {
165 #ifdef writel_relaxed
166         writel_relaxed(value, port->membase + offset);
167 #else
168         writel(value, port->membase + offset);
169 #endif
170 }
171
172 /*
173  * Some simple utility functions to enable and disable interrupts.
174  * Note that these need to be called with interrupts disabled.
175  */
176 static inline void asc_disable_tx_interrupts(struct uart_port *port)
177 {
178         u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_THE;
179         asc_out(port, ASC_INTEN, intenable);
180         (void)asc_in(port, ASC_INTEN);  /* Defeat bus write posting */
181 }
182
183 static inline void asc_enable_tx_interrupts(struct uart_port *port)
184 {
185         u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_THE;
186         asc_out(port, ASC_INTEN, intenable);
187 }
188
189 static inline void asc_disable_rx_interrupts(struct uart_port *port)
190 {
191         u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_RBE;
192         asc_out(port, ASC_INTEN, intenable);
193         (void)asc_in(port, ASC_INTEN);  /* Defeat bus write posting */
194 }
195
196 static inline void asc_enable_rx_interrupts(struct uart_port *port)
197 {
198         u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_RBE;
199         asc_out(port, ASC_INTEN, intenable);
200 }
201
202 static inline u32 asc_txfifo_is_empty(struct uart_port *port)
203 {
204         return asc_in(port, ASC_STA) & ASC_STA_TE;
205 }
206
207 static inline u32 asc_txfifo_is_half_empty(struct uart_port *port)
208 {
209         return asc_in(port, ASC_STA) & ASC_STA_THE;
210 }
211
212 static inline const char *asc_port_name(struct uart_port *port)
213 {
214         return to_platform_device(port->dev)->name;
215 }
216
217 /*----------------------------------------------------------------------*/
218
219 /*
220  * This section contains code to support the use of the ASC as a
221  * generic serial port.
222  */
223
224 static inline unsigned asc_hw_txroom(struct uart_port *port)
225 {
226         u32 status = asc_in(port, ASC_STA);
227
228         if (status & ASC_STA_THE)
229                 return port->fifosize / 2;
230         else if (!(status & ASC_STA_TF))
231                 return 1;
232
233         return 0;
234 }
235
236 /*
237  * Start transmitting chars.
238  * This is called from both interrupt and task level.
239  * Either way interrupts are disabled.
240  */
241 static void asc_transmit_chars(struct uart_port *port)
242 {
243         struct circ_buf *xmit = &port->state->xmit;
244         int txroom;
245         unsigned char c;
246
247         txroom = asc_hw_txroom(port);
248
249         if ((txroom != 0) && port->x_char) {
250                 c = port->x_char;
251                 port->x_char = 0;
252                 asc_out(port, ASC_TXBUF, c);
253                 port->icount.tx++;
254                 txroom = asc_hw_txroom(port);
255         }
256
257         if (uart_tx_stopped(port)) {
258                 /*
259                  * We should try and stop the hardware here, but I
260                  * don't think the ASC has any way to do that.
261                  */
262                 asc_disable_tx_interrupts(port);
263                 return;
264         }
265
266         if (uart_circ_empty(xmit)) {
267                 asc_disable_tx_interrupts(port);
268                 return;
269         }
270
271         if (txroom == 0)
272                 return;
273
274         do {
275                 c = xmit->buf[xmit->tail];
276                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
277                 asc_out(port, ASC_TXBUF, c);
278                 port->icount.tx++;
279                 txroom--;
280         } while ((txroom > 0) && (!uart_circ_empty(xmit)));
281
282         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
283                 uart_write_wakeup(port);
284
285         if (uart_circ_empty(xmit))
286                 asc_disable_tx_interrupts(port);
287 }
288
289 static void asc_receive_chars(struct uart_port *port)
290 {
291         struct tty_port *tport = &port->state->port;
292         unsigned long status, mode;
293         unsigned long c = 0;
294         char flag;
295         bool ignore_pe = false;
296
297         /*
298          * Datasheet states: If the MODE field selects an 8-bit frame then
299          * this [parity error] bit is undefined. Software should ignore this
300          * bit when reading 8-bit frames.
301          */
302         mode = asc_in(port, ASC_CTL) & ASC_CTL_MODE_MSK;
303         if (mode == ASC_CTL_MODE_8BIT || mode == ASC_CTL_MODE_8BIT_PAR)
304                 ignore_pe = true;
305
306         if (port->irq_wake)
307                 pm_wakeup_event(tport->tty->dev, 0);
308
309         while ((status = asc_in(port, ASC_STA)) & ASC_STA_RBF) {
310                 c = asc_in(port, ASC_RXBUF) | ASC_RXBUF_DUMMY_RX;
311                 flag = TTY_NORMAL;
312                 port->icount.rx++;
313
314                 if (status & ASC_STA_OE || c & ASC_RXBUF_FE ||
315                     (c & ASC_RXBUF_PE && !ignore_pe)) {
316
317                         if (c & ASC_RXBUF_FE) {
318                                 if (c == (ASC_RXBUF_FE | ASC_RXBUF_DUMMY_RX)) {
319                                         port->icount.brk++;
320                                         if (uart_handle_break(port))
321                                                 continue;
322                                         c |= ASC_RXBUF_DUMMY_BE;
323                                 } else {
324                                         port->icount.frame++;
325                                 }
326                         } else if (c & ASC_RXBUF_PE) {
327                                 port->icount.parity++;
328                         }
329                         /*
330                          * Reading any data from the RX FIFO clears the
331                          * overflow error condition.
332                          */
333                         if (status & ASC_STA_OE) {
334                                 port->icount.overrun++;
335                                 c |= ASC_RXBUF_DUMMY_OE;
336                         }
337
338                         c &= port->read_status_mask;
339
340                         if (c & ASC_RXBUF_DUMMY_BE)
341                                 flag = TTY_BREAK;
342                         else if (c & ASC_RXBUF_PE)
343                                 flag = TTY_PARITY;
344                         else if (c & ASC_RXBUF_FE)
345                                 flag = TTY_FRAME;
346                 }
347
348                 if (uart_handle_sysrq_char(port, c & 0xff))
349                         continue;
350
351                 uart_insert_char(port, c, ASC_RXBUF_DUMMY_OE, c & 0xff, flag);
352         }
353
354         /* Tell the rest of the system the news. New characters! */
355         tty_flip_buffer_push(tport);
356 }
357
358 static irqreturn_t asc_interrupt(int irq, void *ptr)
359 {
360         struct uart_port *port = ptr;
361         u32 status;
362
363         spin_lock(&port->lock);
364
365         status = asc_in(port, ASC_STA);
366
367         if (status & ASC_STA_RBF) {
368                 /* Receive FIFO not empty */
369                 asc_receive_chars(port);
370         }
371
372         if ((status & ASC_STA_THE) &&
373             (asc_in(port, ASC_INTEN) & ASC_INTEN_THE)) {
374                 /* Transmitter FIFO at least half empty */
375                 asc_transmit_chars(port);
376         }
377
378         spin_unlock(&port->lock);
379
380         return IRQ_HANDLED;
381 }
382
383 /*----------------------------------------------------------------------*/
384
385 /*
386  * UART Functions
387  */
388
389 static unsigned int asc_tx_empty(struct uart_port *port)
390 {
391         return asc_txfifo_is_empty(port) ? TIOCSER_TEMT : 0;
392 }
393
394 static void asc_set_mctrl(struct uart_port *port, unsigned int mctrl)
395 {
396         struct asc_port *ascport = to_asc_port(port);
397
398         /*
399          * This routine is used for seting signals of: DTR, DCD, CTS and RTS.
400          * We use ASC's hardware for CTS/RTS when hardware flow-control is
401          * enabled, however if the RTS line is required for another purpose,
402          * commonly controlled using HUP from userspace, then we need to toggle
403          * it manually, using GPIO.
404          *
405          * Some boards also have DTR and DCD implemented using PIO pins, code to
406          * do this should be hooked in here.
407          */
408
409         if (!ascport->rts)
410                 return;
411
412         /* If HW flow-control is enabled, we can't fiddle with the RTS line */
413         if (asc_in(port, ASC_CTL) & ASC_CTL_CTSENABLE)
414                 return;
415
416         gpiod_set_value(ascport->rts, mctrl & TIOCM_RTS);
417 }
418
419 static unsigned int asc_get_mctrl(struct uart_port *port)
420 {
421         /*
422          * This routine is used for geting signals of: DTR, DCD, DSR, RI,
423          * and CTS/RTS
424          */
425         return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
426 }
427
428 /* There are probably characters waiting to be transmitted. */
429 static void asc_start_tx(struct uart_port *port)
430 {
431         struct circ_buf *xmit = &port->state->xmit;
432
433         if (!uart_circ_empty(xmit))
434                 asc_enable_tx_interrupts(port);
435 }
436
437 /* Transmit stop */
438 static void asc_stop_tx(struct uart_port *port)
439 {
440         asc_disable_tx_interrupts(port);
441 }
442
443 /* Receive stop */
444 static void asc_stop_rx(struct uart_port *port)
445 {
446         asc_disable_rx_interrupts(port);
447 }
448
449 /* Handle breaks - ignored by us */
450 static void asc_break_ctl(struct uart_port *port, int break_state)
451 {
452         /* Nothing here yet .. */
453 }
454
455 /*
456  * Enable port for reception.
457  */
458 static int asc_startup(struct uart_port *port)
459 {
460         if (request_irq(port->irq, asc_interrupt, 0,
461                         asc_port_name(port), port)) {
462                 dev_err(port->dev, "cannot allocate irq.\n");
463                 return -ENODEV;
464         }
465
466         asc_transmit_chars(port);
467         asc_enable_rx_interrupts(port);
468
469         return 0;
470 }
471
472 static void asc_shutdown(struct uart_port *port)
473 {
474         asc_disable_tx_interrupts(port);
475         asc_disable_rx_interrupts(port);
476         free_irq(port->irq, port);
477 }
478
479 static void asc_pm(struct uart_port *port, unsigned int state,
480                 unsigned int oldstate)
481 {
482         struct asc_port *ascport = to_asc_port(port);
483         unsigned long flags = 0;
484         u32 ctl;
485
486         switch (state) {
487         case UART_PM_STATE_ON:
488                 clk_prepare_enable(ascport->clk);
489                 break;
490         case UART_PM_STATE_OFF:
491                 /*
492                  * Disable the ASC baud rate generator, which is as close as
493                  * we can come to turning it off. Note this is not called with
494                  * the port spinlock held.
495                  */
496                 spin_lock_irqsave(&port->lock, flags);
497                 ctl = asc_in(port, ASC_CTL) & ~ASC_CTL_RUN;
498                 asc_out(port, ASC_CTL, ctl);
499                 spin_unlock_irqrestore(&port->lock, flags);
500                 clk_disable_unprepare(ascport->clk);
501                 break;
502         }
503 }
504
505 static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
506                             struct ktermios *old)
507 {
508         struct asc_port *ascport = to_asc_port(port);
509         unsigned int baud;
510         u32 ctrl_val;
511         tcflag_t cflag;
512         unsigned long flags;
513
514         /* Update termios to reflect hardware capabilities */
515         termios->c_cflag &= ~(CMSPAR |
516                          (ascport->hw_flow_control ? 0 : CRTSCTS));
517
518         port->uartclk = clk_get_rate(ascport->clk);
519
520         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
521         cflag = termios->c_cflag;
522
523         spin_lock_irqsave(&port->lock, flags);
524
525         /* read control register */
526         ctrl_val = asc_in(port, ASC_CTL);
527
528         /* stop serial port and reset value */
529         asc_out(port, ASC_CTL, (ctrl_val & ~ASC_CTL_RUN));
530         ctrl_val = ASC_CTL_RXENABLE | ASC_CTL_FIFOENABLE;
531
532         /* reset fifo rx & tx */
533         asc_out(port, ASC_TXRESET, 1);
534         asc_out(port, ASC_RXRESET, 1);
535
536         /* set character length */
537         if ((cflag & CSIZE) == CS7) {
538                 ctrl_val |= ASC_CTL_MODE_7BIT_PAR;
539         } else {
540                 ctrl_val |= (cflag & PARENB) ?  ASC_CTL_MODE_8BIT_PAR :
541                                                 ASC_CTL_MODE_8BIT;
542         }
543
544         /* set stop bit */
545         ctrl_val |= (cflag & CSTOPB) ? ASC_CTL_STOP_2BIT : ASC_CTL_STOP_1BIT;
546
547         /* odd parity */
548         if (cflag & PARODD)
549                 ctrl_val |= ASC_CTL_PARITYODD;
550
551         /* hardware flow control */
552         if ((cflag & CRTSCTS))
553                 ctrl_val |= ASC_CTL_CTSENABLE;
554
555         if ((baud < 19200) && !ascport->force_m1) {
556                 asc_out(port, ASC_BAUDRATE, (port->uartclk / (16 * baud)));
557         } else {
558                 /*
559                  * MODE 1: recommended for high bit rates (above 19.2K)
560                  *
561                  *                   baudrate * 16 * 2^16
562                  * ASCBaudRate =   ------------------------
563                  *                          inputclock
564                  *
565                  * To keep maths inside 64bits, we divide inputclock by 16.
566                  */
567                 u64 dividend = (u64)baud * (1 << 16);
568
569                 do_div(dividend, port->uartclk / 16);
570                 asc_out(port, ASC_BAUDRATE, dividend);
571                 ctrl_val |= ASC_CTL_BAUDMODE;
572         }
573
574         uart_update_timeout(port, cflag, baud);
575
576         ascport->port.read_status_mask = ASC_RXBUF_DUMMY_OE;
577         if (termios->c_iflag & INPCK)
578                 ascport->port.read_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
579         if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
580                 ascport->port.read_status_mask |= ASC_RXBUF_DUMMY_BE;
581
582         /*
583          * Characters to ignore
584          */
585         ascport->port.ignore_status_mask = 0;
586         if (termios->c_iflag & IGNPAR)
587                 ascport->port.ignore_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
588         if (termios->c_iflag & IGNBRK) {
589                 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_BE;
590                 /*
591                  * If we're ignoring parity and break indicators,
592                  * ignore overruns too (for real raw support).
593                  */
594                 if (termios->c_iflag & IGNPAR)
595                         ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_OE;
596         }
597
598         /*
599          * Ignore all characters if CREAD is not set.
600          */
601         if (!(termios->c_cflag & CREAD))
602                 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_RX;
603
604         /* Set the timeout */
605         asc_out(port, ASC_TIMEOUT, 20);
606
607         /* write final value and enable port */
608         asc_out(port, ASC_CTL, (ctrl_val | ASC_CTL_RUN));
609
610         spin_unlock_irqrestore(&port->lock, flags);
611 }
612
613 static const char *asc_type(struct uart_port *port)
614 {
615         return (port->type == PORT_ASC) ? DRIVER_NAME : NULL;
616 }
617
618 static void asc_release_port(struct uart_port *port)
619 {
620 }
621
622 static int asc_request_port(struct uart_port *port)
623 {
624         return 0;
625 }
626
627 /*
628  * Called when the port is opened, and UPF_BOOT_AUTOCONF flag is set
629  * Set type field if successful
630  */
631 static void asc_config_port(struct uart_port *port, int flags)
632 {
633         if ((flags & UART_CONFIG_TYPE))
634                 port->type = PORT_ASC;
635 }
636
637 static int
638 asc_verify_port(struct uart_port *port, struct serial_struct *ser)
639 {
640         /* No user changeable parameters */
641         return -EINVAL;
642 }
643
644 #ifdef CONFIG_CONSOLE_POLL
645 /*
646  * Console polling routines for writing and reading from the uart while
647  * in an interrupt or debug context (i.e. kgdb).
648  */
649
650 static int asc_get_poll_char(struct uart_port *port)
651 {
652         if (!(asc_in(port, ASC_STA) & ASC_STA_RBF))
653                 return NO_POLL_CHAR;
654
655         return asc_in(port, ASC_RXBUF);
656 }
657
658 static void asc_put_poll_char(struct uart_port *port, unsigned char c)
659 {
660         while (!asc_txfifo_is_half_empty(port))
661                 cpu_relax();
662         asc_out(port, ASC_TXBUF, c);
663 }
664
665 #endif /* CONFIG_CONSOLE_POLL */
666
667 /*---------------------------------------------------------------------*/
668
669 static const struct uart_ops asc_uart_ops = {
670         .tx_empty       = asc_tx_empty,
671         .set_mctrl      = asc_set_mctrl,
672         .get_mctrl      = asc_get_mctrl,
673         .start_tx       = asc_start_tx,
674         .stop_tx        = asc_stop_tx,
675         .stop_rx        = asc_stop_rx,
676         .break_ctl      = asc_break_ctl,
677         .startup        = asc_startup,
678         .shutdown       = asc_shutdown,
679         .set_termios    = asc_set_termios,
680         .type           = asc_type,
681         .release_port   = asc_release_port,
682         .request_port   = asc_request_port,
683         .config_port    = asc_config_port,
684         .verify_port    = asc_verify_port,
685         .pm             = asc_pm,
686 #ifdef CONFIG_CONSOLE_POLL
687         .poll_get_char = asc_get_poll_char,
688         .poll_put_char = asc_put_poll_char,
689 #endif /* CONFIG_CONSOLE_POLL */
690 };
691
692 static int asc_init_port(struct asc_port *ascport,
693                           struct platform_device *pdev)
694 {
695         struct uart_port *port = &ascport->port;
696         struct resource *res;
697
698         port->iotype    = UPIO_MEM;
699         port->flags     = UPF_BOOT_AUTOCONF;
700         port->ops       = &asc_uart_ops;
701         port->fifosize  = ASC_FIFO_SIZE;
702         port->dev       = &pdev->dev;
703         port->irq       = platform_get_irq(pdev, 0);
704
705         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
706         port->membase = devm_ioremap_resource(&pdev->dev, res);
707         if (IS_ERR(port->membase))
708                 return PTR_ERR(port->membase);
709         port->mapbase = res->start;
710
711         spin_lock_init(&port->lock);
712
713         ascport->clk = devm_clk_get(&pdev->dev, NULL);
714
715         if (WARN_ON(IS_ERR(ascport->clk)))
716                 return -EINVAL;
717         /* ensure that clk rate is correct by enabling the clk */
718         clk_prepare_enable(ascport->clk);
719         ascport->port.uartclk = clk_get_rate(ascport->clk);
720         WARN_ON(ascport->port.uartclk == 0);
721         clk_disable_unprepare(ascport->clk);
722
723         return 0;
724 }
725
726 static struct asc_port *asc_of_get_asc_port(struct platform_device *pdev)
727 {
728         struct device_node *np = pdev->dev.of_node;
729         int id;
730
731         if (!np)
732                 return NULL;
733
734         id = of_alias_get_id(np, ASC_SERIAL_NAME);
735
736         if (id < 0)
737                 id = 0;
738
739         if (WARN_ON(id >= ASC_MAX_PORTS))
740                 return NULL;
741
742         asc_ports[id].hw_flow_control = of_property_read_bool(np,
743                                                         "st,hw-flow-control");
744         asc_ports[id].force_m1 =  of_property_read_bool(np, "st,force_m1");
745         asc_ports[id].port.line = id;
746         asc_ports[id].rts = NULL;
747
748         return &asc_ports[id];
749 }
750
751 #ifdef CONFIG_OF
752 static const struct of_device_id asc_match[] = {
753         { .compatible = "st,asc", },
754         {},
755 };
756
757 MODULE_DEVICE_TABLE(of, asc_match);
758 #endif
759
760 static int asc_serial_probe(struct platform_device *pdev)
761 {
762         int ret;
763         struct asc_port *ascport;
764
765         ascport = asc_of_get_asc_port(pdev);
766         if (!ascport)
767                 return -ENODEV;
768
769         ret = asc_init_port(ascport, pdev);
770         if (ret)
771                 return ret;
772
773         ret = uart_add_one_port(&asc_uart_driver, &ascport->port);
774         if (ret)
775                 return ret;
776
777         platform_set_drvdata(pdev, &ascport->port);
778
779         return 0;
780 }
781
782 static int asc_serial_remove(struct platform_device *pdev)
783 {
784         struct uart_port *port = platform_get_drvdata(pdev);
785
786         return uart_remove_one_port(&asc_uart_driver, port);
787 }
788
789 #ifdef CONFIG_PM_SLEEP
790 static int asc_serial_suspend(struct device *dev)
791 {
792         struct platform_device *pdev = to_platform_device(dev);
793         struct uart_port *port = platform_get_drvdata(pdev);
794
795         return uart_suspend_port(&asc_uart_driver, port);
796 }
797
798 static int asc_serial_resume(struct device *dev)
799 {
800         struct platform_device *pdev = to_platform_device(dev);
801         struct uart_port *port = platform_get_drvdata(pdev);
802
803         return uart_resume_port(&asc_uart_driver, port);
804 }
805
806 #endif /* CONFIG_PM_SLEEP */
807
808 /*----------------------------------------------------------------------*/
809
810 #ifdef CONFIG_SERIAL_ST_ASC_CONSOLE
811 static void asc_console_putchar(struct uart_port *port, int ch)
812 {
813         unsigned int timeout = 1000000;
814
815         /* Wait for upto 1 second in case flow control is stopping us. */
816         while (--timeout && !asc_txfifo_is_half_empty(port))
817                 udelay(1);
818
819         asc_out(port, ASC_TXBUF, ch);
820 }
821
822 /*
823  *  Print a string to the serial port trying not to disturb
824  *  any possible real use of the port...
825  */
826
827 static void asc_console_write(struct console *co, const char *s, unsigned count)
828 {
829         struct uart_port *port = &asc_ports[co->index].port;
830         unsigned long flags;
831         unsigned long timeout = 1000000;
832         int locked = 1;
833         u32 intenable;
834
835         local_irq_save(flags);
836         if (port->sysrq)
837                 locked = 0; /* asc_interrupt has already claimed the lock */
838         else if (oops_in_progress)
839                 locked = spin_trylock(&port->lock);
840         else
841                 spin_lock(&port->lock);
842
843         /*
844          * Disable interrupts so we don't get the IRQ line bouncing
845          * up and down while interrupts are disabled.
846          */
847         intenable = asc_in(port, ASC_INTEN);
848         asc_out(port, ASC_INTEN, 0);
849         (void)asc_in(port, ASC_INTEN);  /* Defeat bus write posting */
850
851         uart_console_write(port, s, count, asc_console_putchar);
852
853         while (--timeout && !asc_txfifo_is_empty(port))
854                 udelay(1);
855
856         asc_out(port, ASC_INTEN, intenable);
857
858         if (locked)
859                 spin_unlock(&port->lock);
860         local_irq_restore(flags);
861 }
862
863 static int asc_console_setup(struct console *co, char *options)
864 {
865         struct asc_port *ascport;
866         int baud = 9600;
867         int bits = 8;
868         int parity = 'n';
869         int flow = 'n';
870
871         if (co->index >= ASC_MAX_PORTS)
872                 return -ENODEV;
873
874         ascport = &asc_ports[co->index];
875
876         /*
877          * This driver does not support early console initialization
878          * (use ARM early printk support instead), so we only expect
879          * this to be called during the uart port registration when the
880          * driver gets probed and the port should be mapped at that point.
881          */
882         if (ascport->port.mapbase == 0 || ascport->port.membase == NULL)
883                 return -ENXIO;
884
885         if (options)
886                 uart_parse_options(options, &baud, &parity, &bits, &flow);
887
888         return uart_set_options(&ascport->port, co, baud, parity, bits, flow);
889 }
890
891 static struct console asc_console = {
892         .name           = ASC_SERIAL_NAME,
893         .device         = uart_console_device,
894         .write          = asc_console_write,
895         .setup          = asc_console_setup,
896         .flags          = CON_PRINTBUFFER,
897         .index          = -1,
898         .data           = &asc_uart_driver,
899 };
900
901 #define ASC_SERIAL_CONSOLE (&asc_console)
902
903 #else
904 #define ASC_SERIAL_CONSOLE NULL
905 #endif /* CONFIG_SERIAL_ST_ASC_CONSOLE */
906
907 static struct uart_driver asc_uart_driver = {
908         .owner          = THIS_MODULE,
909         .driver_name    = DRIVER_NAME,
910         .dev_name       = ASC_SERIAL_NAME,
911         .major          = 0,
912         .minor          = 0,
913         .nr             = ASC_MAX_PORTS,
914         .cons           = ASC_SERIAL_CONSOLE,
915 };
916
917 static const struct dev_pm_ops asc_serial_pm_ops = {
918         SET_SYSTEM_SLEEP_PM_OPS(asc_serial_suspend, asc_serial_resume)
919 };
920
921 static struct platform_driver asc_serial_driver = {
922         .probe          = asc_serial_probe,
923         .remove         = asc_serial_remove,
924         .driver = {
925                 .name   = DRIVER_NAME,
926                 .pm     = &asc_serial_pm_ops,
927                 .of_match_table = of_match_ptr(asc_match),
928         },
929 };
930
931 static int __init asc_init(void)
932 {
933         int ret;
934         static char banner[] __initdata =
935                 KERN_INFO "STMicroelectronics ASC driver initialized\n";
936
937         printk(banner);
938
939         ret = uart_register_driver(&asc_uart_driver);
940         if (ret)
941                 return ret;
942
943         ret = platform_driver_register(&asc_serial_driver);
944         if (ret)
945                 uart_unregister_driver(&asc_uart_driver);
946
947         return ret;
948 }
949
950 static void __exit asc_exit(void)
951 {
952         platform_driver_unregister(&asc_serial_driver);
953         uart_unregister_driver(&asc_uart_driver);
954 }
955
956 module_init(asc_init);
957 module_exit(asc_exit);
958
959 MODULE_ALIAS("platform:" DRIVER_NAME);
960 MODULE_AUTHOR("STMicroelectronics (R&D) Limited");
961 MODULE_DESCRIPTION("STMicroelectronics ASC serial port driver");
962 MODULE_LICENSE("GPL");