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