2 * st-asc.c: ST Asynchronous serial controller (ASC) driver
4 * Copyright (C) 2003-2013 STMicroelectronics (R&D) Limited
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.
13 #if defined(CONFIG_SERIAL_ST_ASC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
17 #include <linux/module.h>
18 #include <linux/serial.h>
19 #include <linux/console.h>
20 #include <linux/sysrq.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/platform_device.h>
24 #include <linux/irq.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/delay.h>
28 #include <linux/spinlock.h>
29 #include <linux/pm_runtime.h>
31 #include <linux/of_platform.h>
32 #include <linux/serial_core.h>
33 #include <linux/clk.h>
34 #include <linux/gpio/consumer.h>
36 #define DRIVER_NAME "st-asc"
37 #define ASC_SERIAL_NAME "ttyAS"
38 #define ASC_FIFO_SIZE 16
39 #define ASC_MAX_PORTS 8
43 #define NO_HW_FLOWCTRL 1
46 struct uart_port port;
47 struct gpio_desc *rts;
49 struct pinctrl *pinctrl;
50 struct pinctrl_state *states[2];
51 unsigned int hw_flow_control:1;
52 unsigned int force_m1:1;
55 static struct asc_port asc_ports[ASC_MAX_PORTS];
56 static struct uart_driver asc_uart_driver;
58 /*---- UART Register definitions ------------------------------*/
60 /* Register offsets */
62 #define ASC_BAUDRATE 0x00
63 #define ASC_TXBUF 0x04
64 #define ASC_RXBUF 0x08
66 #define ASC_INTEN 0x10
68 #define ASC_GUARDTIME 0x18
69 #define ASC_TIMEOUT 0x1C
70 #define ASC_TXRESET 0x20
71 #define ASC_RXRESET 0x24
72 #define ASC_RETRIES 0x28
75 #define ASC_RXBUF_PE 0x100
76 #define ASC_RXBUF_FE 0x200
78 * Some of status comes from higher bits of the character and some come from
79 * the status register. Combining both of them in to single status using dummy
82 #define ASC_RXBUF_DUMMY_RX 0x10000
83 #define ASC_RXBUF_DUMMY_BE 0x20000
84 #define ASC_RXBUF_DUMMY_OE 0x40000
88 #define ASC_CTL_MODE_MSK 0x0007
89 #define ASC_CTL_MODE_8BIT 0x0001
90 #define ASC_CTL_MODE_7BIT_PAR 0x0003
91 #define ASC_CTL_MODE_9BIT 0x0004
92 #define ASC_CTL_MODE_8BIT_WKUP 0x0005
93 #define ASC_CTL_MODE_8BIT_PAR 0x0007
94 #define ASC_CTL_STOP_MSK 0x0018
95 #define ASC_CTL_STOP_HALFBIT 0x0000
96 #define ASC_CTL_STOP_1BIT 0x0008
97 #define ASC_CTL_STOP_1_HALFBIT 0x0010
98 #define ASC_CTL_STOP_2BIT 0x0018
99 #define ASC_CTL_PARITYODD 0x0020
100 #define ASC_CTL_LOOPBACK 0x0040
101 #define ASC_CTL_RUN 0x0080
102 #define ASC_CTL_RXENABLE 0x0100
103 #define ASC_CTL_SCENABLE 0x0200
104 #define ASC_CTL_FIFOENABLE 0x0400
105 #define ASC_CTL_CTSENABLE 0x0800
106 #define ASC_CTL_BAUDMODE 0x1000
110 #define ASC_GUARDTIME_MSK 0x00FF
114 #define ASC_INTEN_RBE 0x0001
115 #define ASC_INTEN_TE 0x0002
116 #define ASC_INTEN_THE 0x0004
117 #define ASC_INTEN_PE 0x0008
118 #define ASC_INTEN_FE 0x0010
119 #define ASC_INTEN_OE 0x0020
120 #define ASC_INTEN_TNE 0x0040
121 #define ASC_INTEN_TOI 0x0080
122 #define ASC_INTEN_RHF 0x0100
126 #define ASC_RETRIES_MSK 0x00FF
130 #define ASC_RXBUF_MSK 0x03FF
134 #define ASC_STA_RBF 0x0001
135 #define ASC_STA_TE 0x0002
136 #define ASC_STA_THE 0x0004
137 #define ASC_STA_PE 0x0008
138 #define ASC_STA_FE 0x0010
139 #define ASC_STA_OE 0x0020
140 #define ASC_STA_TNE 0x0040
141 #define ASC_STA_TOI 0x0080
142 #define ASC_STA_RHF 0x0100
143 #define ASC_STA_TF 0x0200
144 #define ASC_STA_NKD 0x0400
148 #define ASC_TIMEOUT_MSK 0x00FF
152 #define ASC_TXBUF_MSK 0x01FF
154 /*---- Inline function definitions ---------------------------*/
156 static inline struct asc_port *to_asc_port(struct uart_port *port)
158 return container_of(port, struct asc_port, port);
161 static inline u32 asc_in(struct uart_port *port, u32 offset)
164 return readl_relaxed(port->membase + offset);
166 return readl(port->membase + offset);
170 static inline void asc_out(struct uart_port *port, u32 offset, u32 value)
172 #ifdef writel_relaxed
173 writel_relaxed(value, port->membase + offset);
175 writel(value, port->membase + offset);
180 * Some simple utility functions to enable and disable interrupts.
181 * Note that these need to be called with interrupts disabled.
183 static inline void asc_disable_tx_interrupts(struct uart_port *port)
185 u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_THE;
186 asc_out(port, ASC_INTEN, intenable);
187 (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
190 static inline void asc_enable_tx_interrupts(struct uart_port *port)
192 u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_THE;
193 asc_out(port, ASC_INTEN, intenable);
196 static inline void asc_disable_rx_interrupts(struct uart_port *port)
198 u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_RBE;
199 asc_out(port, ASC_INTEN, intenable);
200 (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
203 static inline void asc_enable_rx_interrupts(struct uart_port *port)
205 u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_RBE;
206 asc_out(port, ASC_INTEN, intenable);
209 static inline u32 asc_txfifo_is_empty(struct uart_port *port)
211 return asc_in(port, ASC_STA) & ASC_STA_TE;
214 static inline u32 asc_txfifo_is_half_empty(struct uart_port *port)
216 return asc_in(port, ASC_STA) & ASC_STA_THE;
219 static inline const char *asc_port_name(struct uart_port *port)
221 return to_platform_device(port->dev)->name;
224 /*----------------------------------------------------------------------*/
227 * This section contains code to support the use of the ASC as a
228 * generic serial port.
231 static inline unsigned asc_hw_txroom(struct uart_port *port)
233 u32 status = asc_in(port, ASC_STA);
235 if (status & ASC_STA_THE)
236 return port->fifosize / 2;
237 else if (!(status & ASC_STA_TF))
244 * Start transmitting chars.
245 * This is called from both interrupt and task level.
246 * Either way interrupts are disabled.
248 static void asc_transmit_chars(struct uart_port *port)
250 struct circ_buf *xmit = &port->state->xmit;
254 txroom = asc_hw_txroom(port);
256 if ((txroom != 0) && port->x_char) {
259 asc_out(port, ASC_TXBUF, c);
261 txroom = asc_hw_txroom(port);
264 if (uart_tx_stopped(port)) {
266 * We should try and stop the hardware here, but I
267 * don't think the ASC has any way to do that.
269 asc_disable_tx_interrupts(port);
273 if (uart_circ_empty(xmit)) {
274 asc_disable_tx_interrupts(port);
282 c = xmit->buf[xmit->tail];
283 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
284 asc_out(port, ASC_TXBUF, c);
287 } while ((txroom > 0) && (!uart_circ_empty(xmit)));
289 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
290 uart_write_wakeup(port);
292 if (uart_circ_empty(xmit))
293 asc_disable_tx_interrupts(port);
296 static void asc_receive_chars(struct uart_port *port)
298 struct tty_port *tport = &port->state->port;
299 unsigned long status, mode;
302 bool ignore_pe = false;
305 * Datasheet states: If the MODE field selects an 8-bit frame then
306 * this [parity error] bit is undefined. Software should ignore this
307 * bit when reading 8-bit frames.
309 mode = asc_in(port, ASC_CTL) & ASC_CTL_MODE_MSK;
310 if (mode == ASC_CTL_MODE_8BIT || mode == ASC_CTL_MODE_8BIT_PAR)
314 pm_wakeup_event(tport->tty->dev, 0);
316 while ((status = asc_in(port, ASC_STA)) & ASC_STA_RBF) {
317 c = asc_in(port, ASC_RXBUF) | ASC_RXBUF_DUMMY_RX;
321 if (status & ASC_STA_OE || c & ASC_RXBUF_FE ||
322 (c & ASC_RXBUF_PE && !ignore_pe)) {
324 if (c & ASC_RXBUF_FE) {
325 if (c == (ASC_RXBUF_FE | ASC_RXBUF_DUMMY_RX)) {
327 if (uart_handle_break(port))
329 c |= ASC_RXBUF_DUMMY_BE;
331 port->icount.frame++;
333 } else if (c & ASC_RXBUF_PE) {
334 port->icount.parity++;
337 * Reading any data from the RX FIFO clears the
338 * overflow error condition.
340 if (status & ASC_STA_OE) {
341 port->icount.overrun++;
342 c |= ASC_RXBUF_DUMMY_OE;
345 c &= port->read_status_mask;
347 if (c & ASC_RXBUF_DUMMY_BE)
349 else if (c & ASC_RXBUF_PE)
351 else if (c & ASC_RXBUF_FE)
355 if (uart_handle_sysrq_char(port, c & 0xff))
358 uart_insert_char(port, c, ASC_RXBUF_DUMMY_OE, c & 0xff, flag);
361 /* Tell the rest of the system the news. New characters! */
362 tty_flip_buffer_push(tport);
365 static irqreturn_t asc_interrupt(int irq, void *ptr)
367 struct uart_port *port = ptr;
370 spin_lock(&port->lock);
372 status = asc_in(port, ASC_STA);
374 if (status & ASC_STA_RBF) {
375 /* Receive FIFO not empty */
376 asc_receive_chars(port);
379 if ((status & ASC_STA_THE) &&
380 (asc_in(port, ASC_INTEN) & ASC_INTEN_THE)) {
381 /* Transmitter FIFO at least half empty */
382 asc_transmit_chars(port);
385 spin_unlock(&port->lock);
390 /*----------------------------------------------------------------------*/
396 static unsigned int asc_tx_empty(struct uart_port *port)
398 return asc_txfifo_is_empty(port) ? TIOCSER_TEMT : 0;
401 static void asc_set_mctrl(struct uart_port *port, unsigned int mctrl)
403 struct asc_port *ascport = to_asc_port(port);
406 * This routine is used for seting signals of: DTR, DCD, CTS and RTS.
407 * We use ASC's hardware for CTS/RTS when hardware flow-control is
408 * enabled, however if the RTS line is required for another purpose,
409 * commonly controlled using HUP from userspace, then we need to toggle
410 * it manually, using GPIO.
412 * Some boards also have DTR and DCD implemented using PIO pins, code to
413 * do this should be hooked in here.
419 /* If HW flow-control is enabled, we can't fiddle with the RTS line */
420 if (asc_in(port, ASC_CTL) & ASC_CTL_CTSENABLE)
423 gpiod_set_value(ascport->rts, mctrl & TIOCM_RTS);
426 static unsigned int asc_get_mctrl(struct uart_port *port)
429 * This routine is used for geting signals of: DTR, DCD, DSR, RI,
432 return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
435 /* There are probably characters waiting to be transmitted. */
436 static void asc_start_tx(struct uart_port *port)
438 struct circ_buf *xmit = &port->state->xmit;
440 if (!uart_circ_empty(xmit))
441 asc_enable_tx_interrupts(port);
445 static void asc_stop_tx(struct uart_port *port)
447 asc_disable_tx_interrupts(port);
451 static void asc_stop_rx(struct uart_port *port)
453 asc_disable_rx_interrupts(port);
456 /* Handle breaks - ignored by us */
457 static void asc_break_ctl(struct uart_port *port, int break_state)
459 /* Nothing here yet .. */
463 * Enable port for reception.
465 static int asc_startup(struct uart_port *port)
467 if (request_irq(port->irq, asc_interrupt, 0,
468 asc_port_name(port), port)) {
469 dev_err(port->dev, "cannot allocate irq.\n");
473 asc_transmit_chars(port);
474 asc_enable_rx_interrupts(port);
479 static void asc_shutdown(struct uart_port *port)
481 asc_disable_tx_interrupts(port);
482 asc_disable_rx_interrupts(port);
483 free_irq(port->irq, port);
486 static void asc_pm(struct uart_port *port, unsigned int state,
487 unsigned int oldstate)
489 struct asc_port *ascport = to_asc_port(port);
490 unsigned long flags = 0;
494 case UART_PM_STATE_ON:
495 clk_prepare_enable(ascport->clk);
497 case UART_PM_STATE_OFF:
499 * Disable the ASC baud rate generator, which is as close as
500 * we can come to turning it off. Note this is not called with
501 * the port spinlock held.
503 spin_lock_irqsave(&port->lock, flags);
504 ctl = asc_in(port, ASC_CTL) & ~ASC_CTL_RUN;
505 asc_out(port, ASC_CTL, ctl);
506 spin_unlock_irqrestore(&port->lock, flags);
507 clk_disable_unprepare(ascport->clk);
512 static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
513 struct ktermios *old)
515 struct asc_port *ascport = to_asc_port(port);
516 struct device_node *np = port->dev->of_node;
517 struct gpio_desc *gpiod;
523 /* Update termios to reflect hardware capabilities */
524 termios->c_cflag &= ~(CMSPAR |
525 (ascport->hw_flow_control ? 0 : CRTSCTS));
527 port->uartclk = clk_get_rate(ascport->clk);
529 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
530 cflag = termios->c_cflag;
532 spin_lock_irqsave(&port->lock, flags);
534 /* read control register */
535 ctrl_val = asc_in(port, ASC_CTL);
537 /* stop serial port and reset value */
538 asc_out(port, ASC_CTL, (ctrl_val & ~ASC_CTL_RUN));
539 ctrl_val = ASC_CTL_RXENABLE | ASC_CTL_FIFOENABLE;
541 /* reset fifo rx & tx */
542 asc_out(port, ASC_TXRESET, 1);
543 asc_out(port, ASC_RXRESET, 1);
545 /* set character length */
546 if ((cflag & CSIZE) == CS7) {
547 ctrl_val |= ASC_CTL_MODE_7BIT_PAR;
549 ctrl_val |= (cflag & PARENB) ? ASC_CTL_MODE_8BIT_PAR :
554 ctrl_val |= (cflag & CSTOPB) ? ASC_CTL_STOP_2BIT : ASC_CTL_STOP_1BIT;
558 ctrl_val |= ASC_CTL_PARITYODD;
560 /* hardware flow control */
561 if ((cflag & CRTSCTS)) {
562 ctrl_val |= ASC_CTL_CTSENABLE;
564 /* If flow-control selected, stop handling RTS manually */
566 devm_gpiod_put(port->dev, ascport->rts);
569 pinctrl_select_state(ascport->pinctrl,
570 ascport->states[DEFAULT]);
573 /* If flow-control disabled, it's safe to handle RTS manually */
574 if (!ascport->rts && ascport->states[NO_HW_FLOWCTRL]) {
575 pinctrl_select_state(ascport->pinctrl,
576 ascport->states[NO_HW_FLOWCTRL]);
578 gpiod = devm_get_gpiod_from_child(port->dev, "rts",
580 if (!IS_ERR(gpiod)) {
581 gpiod_direction_output(gpiod, 0);
582 ascport->rts = gpiod;
587 if ((baud < 19200) && !ascport->force_m1) {
588 asc_out(port, ASC_BAUDRATE, (port->uartclk / (16 * baud)));
591 * MODE 1: recommended for high bit rates (above 19.2K)
593 * baudrate * 16 * 2^16
594 * ASCBaudRate = ------------------------
597 * To keep maths inside 64bits, we divide inputclock by 16.
599 u64 dividend = (u64)baud * (1 << 16);
601 do_div(dividend, port->uartclk / 16);
602 asc_out(port, ASC_BAUDRATE, dividend);
603 ctrl_val |= ASC_CTL_BAUDMODE;
606 uart_update_timeout(port, cflag, baud);
608 ascport->port.read_status_mask = ASC_RXBUF_DUMMY_OE;
609 if (termios->c_iflag & INPCK)
610 ascport->port.read_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
611 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
612 ascport->port.read_status_mask |= ASC_RXBUF_DUMMY_BE;
615 * Characters to ignore
617 ascport->port.ignore_status_mask = 0;
618 if (termios->c_iflag & IGNPAR)
619 ascport->port.ignore_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
620 if (termios->c_iflag & IGNBRK) {
621 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_BE;
623 * If we're ignoring parity and break indicators,
624 * ignore overruns too (for real raw support).
626 if (termios->c_iflag & IGNPAR)
627 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_OE;
631 * Ignore all characters if CREAD is not set.
633 if (!(termios->c_cflag & CREAD))
634 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_RX;
636 /* Set the timeout */
637 asc_out(port, ASC_TIMEOUT, 20);
639 /* write final value and enable port */
640 asc_out(port, ASC_CTL, (ctrl_val | ASC_CTL_RUN));
642 spin_unlock_irqrestore(&port->lock, flags);
645 static const char *asc_type(struct uart_port *port)
647 return (port->type == PORT_ASC) ? DRIVER_NAME : NULL;
650 static void asc_release_port(struct uart_port *port)
654 static int asc_request_port(struct uart_port *port)
660 * Called when the port is opened, and UPF_BOOT_AUTOCONF flag is set
661 * Set type field if successful
663 static void asc_config_port(struct uart_port *port, int flags)
665 if ((flags & UART_CONFIG_TYPE))
666 port->type = PORT_ASC;
670 asc_verify_port(struct uart_port *port, struct serial_struct *ser)
672 /* No user changeable parameters */
676 #ifdef CONFIG_CONSOLE_POLL
678 * Console polling routines for writing and reading from the uart while
679 * in an interrupt or debug context (i.e. kgdb).
682 static int asc_get_poll_char(struct uart_port *port)
684 if (!(asc_in(port, ASC_STA) & ASC_STA_RBF))
687 return asc_in(port, ASC_RXBUF);
690 static void asc_put_poll_char(struct uart_port *port, unsigned char c)
692 while (!asc_txfifo_is_half_empty(port))
694 asc_out(port, ASC_TXBUF, c);
697 #endif /* CONFIG_CONSOLE_POLL */
699 /*---------------------------------------------------------------------*/
701 static const struct uart_ops asc_uart_ops = {
702 .tx_empty = asc_tx_empty,
703 .set_mctrl = asc_set_mctrl,
704 .get_mctrl = asc_get_mctrl,
705 .start_tx = asc_start_tx,
706 .stop_tx = asc_stop_tx,
707 .stop_rx = asc_stop_rx,
708 .break_ctl = asc_break_ctl,
709 .startup = asc_startup,
710 .shutdown = asc_shutdown,
711 .set_termios = asc_set_termios,
713 .release_port = asc_release_port,
714 .request_port = asc_request_port,
715 .config_port = asc_config_port,
716 .verify_port = asc_verify_port,
718 #ifdef CONFIG_CONSOLE_POLL
719 .poll_get_char = asc_get_poll_char,
720 .poll_put_char = asc_put_poll_char,
721 #endif /* CONFIG_CONSOLE_POLL */
724 static int asc_init_port(struct asc_port *ascport,
725 struct platform_device *pdev)
727 struct uart_port *port = &ascport->port;
728 struct resource *res;
731 port->iotype = UPIO_MEM;
732 port->flags = UPF_BOOT_AUTOCONF;
733 port->ops = &asc_uart_ops;
734 port->fifosize = ASC_FIFO_SIZE;
735 port->dev = &pdev->dev;
736 port->irq = platform_get_irq(pdev, 0);
738 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
739 port->membase = devm_ioremap_resource(&pdev->dev, res);
740 if (IS_ERR(port->membase))
741 return PTR_ERR(port->membase);
742 port->mapbase = res->start;
744 spin_lock_init(&port->lock);
746 ascport->clk = devm_clk_get(&pdev->dev, NULL);
748 if (WARN_ON(IS_ERR(ascport->clk)))
750 /* ensure that clk rate is correct by enabling the clk */
751 clk_prepare_enable(ascport->clk);
752 ascport->port.uartclk = clk_get_rate(ascport->clk);
753 WARN_ON(ascport->port.uartclk == 0);
754 clk_disable_unprepare(ascport->clk);
756 ascport->pinctrl = devm_pinctrl_get(&pdev->dev);
757 if (IS_ERR(ascport->pinctrl)) {
758 ret = PTR_ERR(ascport->pinctrl);
759 dev_err(&pdev->dev, "Failed to get Pinctrl: %d\n", ret);
762 ascport->states[DEFAULT] =
763 pinctrl_lookup_state(ascport->pinctrl, "default");
764 if (IS_ERR(ascport->states[DEFAULT])) {
765 ret = PTR_ERR(ascport->states[DEFAULT]);
767 "Failed to look up Pinctrl state 'default': %d\n", ret);
771 /* "no-hw-flowctrl" state is optional */
772 ascport->states[NO_HW_FLOWCTRL] =
773 pinctrl_lookup_state(ascport->pinctrl, "no-hw-flowctrl");
774 if (IS_ERR(ascport->states[NO_HW_FLOWCTRL]))
775 ascport->states[NO_HW_FLOWCTRL] = NULL;
780 static struct asc_port *asc_of_get_asc_port(struct platform_device *pdev)
782 struct device_node *np = pdev->dev.of_node;
788 id = of_alias_get_id(np, ASC_SERIAL_NAME);
793 if (WARN_ON(id >= ASC_MAX_PORTS))
796 asc_ports[id].hw_flow_control = of_property_read_bool(np,
798 asc_ports[id].force_m1 = of_property_read_bool(np, "st,force_m1");
799 asc_ports[id].port.line = id;
800 asc_ports[id].rts = NULL;
802 return &asc_ports[id];
806 static const struct of_device_id asc_match[] = {
807 { .compatible = "st,asc", },
811 MODULE_DEVICE_TABLE(of, asc_match);
814 static int asc_serial_probe(struct platform_device *pdev)
817 struct asc_port *ascport;
819 ascport = asc_of_get_asc_port(pdev);
823 ret = asc_init_port(ascport, pdev);
827 ret = uart_add_one_port(&asc_uart_driver, &ascport->port);
831 platform_set_drvdata(pdev, &ascport->port);
836 static int asc_serial_remove(struct platform_device *pdev)
838 struct uart_port *port = platform_get_drvdata(pdev);
840 return uart_remove_one_port(&asc_uart_driver, port);
843 #ifdef CONFIG_PM_SLEEP
844 static int asc_serial_suspend(struct device *dev)
846 struct platform_device *pdev = to_platform_device(dev);
847 struct uart_port *port = platform_get_drvdata(pdev);
849 return uart_suspend_port(&asc_uart_driver, port);
852 static int asc_serial_resume(struct device *dev)
854 struct platform_device *pdev = to_platform_device(dev);
855 struct uart_port *port = platform_get_drvdata(pdev);
857 return uart_resume_port(&asc_uart_driver, port);
860 #endif /* CONFIG_PM_SLEEP */
862 /*----------------------------------------------------------------------*/
864 #ifdef CONFIG_SERIAL_ST_ASC_CONSOLE
865 static void asc_console_putchar(struct uart_port *port, int ch)
867 unsigned int timeout = 1000000;
869 /* Wait for upto 1 second in case flow control is stopping us. */
870 while (--timeout && !asc_txfifo_is_half_empty(port))
873 asc_out(port, ASC_TXBUF, ch);
877 * Print a string to the serial port trying not to disturb
878 * any possible real use of the port...
881 static void asc_console_write(struct console *co, const char *s, unsigned count)
883 struct uart_port *port = &asc_ports[co->index].port;
885 unsigned long timeout = 1000000;
889 local_irq_save(flags);
891 locked = 0; /* asc_interrupt has already claimed the lock */
892 else if (oops_in_progress)
893 locked = spin_trylock(&port->lock);
895 spin_lock(&port->lock);
898 * Disable interrupts so we don't get the IRQ line bouncing
899 * up and down while interrupts are disabled.
901 intenable = asc_in(port, ASC_INTEN);
902 asc_out(port, ASC_INTEN, 0);
903 (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
905 uart_console_write(port, s, count, asc_console_putchar);
907 while (--timeout && !asc_txfifo_is_empty(port))
910 asc_out(port, ASC_INTEN, intenable);
913 spin_unlock(&port->lock);
914 local_irq_restore(flags);
917 static int asc_console_setup(struct console *co, char *options)
919 struct asc_port *ascport;
925 if (co->index >= ASC_MAX_PORTS)
928 ascport = &asc_ports[co->index];
931 * This driver does not support early console initialization
932 * (use ARM early printk support instead), so we only expect
933 * this to be called during the uart port registration when the
934 * driver gets probed and the port should be mapped at that point.
936 if (ascport->port.mapbase == 0 || ascport->port.membase == NULL)
940 uart_parse_options(options, &baud, &parity, &bits, &flow);
942 return uart_set_options(&ascport->port, co, baud, parity, bits, flow);
945 static struct console asc_console = {
946 .name = ASC_SERIAL_NAME,
947 .device = uart_console_device,
948 .write = asc_console_write,
949 .setup = asc_console_setup,
950 .flags = CON_PRINTBUFFER,
952 .data = &asc_uart_driver,
955 #define ASC_SERIAL_CONSOLE (&asc_console)
958 #define ASC_SERIAL_CONSOLE NULL
959 #endif /* CONFIG_SERIAL_ST_ASC_CONSOLE */
961 static struct uart_driver asc_uart_driver = {
962 .owner = THIS_MODULE,
963 .driver_name = DRIVER_NAME,
964 .dev_name = ASC_SERIAL_NAME,
968 .cons = ASC_SERIAL_CONSOLE,
971 static const struct dev_pm_ops asc_serial_pm_ops = {
972 SET_SYSTEM_SLEEP_PM_OPS(asc_serial_suspend, asc_serial_resume)
975 static struct platform_driver asc_serial_driver = {
976 .probe = asc_serial_probe,
977 .remove = asc_serial_remove,
980 .pm = &asc_serial_pm_ops,
981 .of_match_table = of_match_ptr(asc_match),
985 static int __init asc_init(void)
988 static char banner[] __initdata =
989 KERN_INFO "STMicroelectronics ASC driver initialized\n";
993 ret = uart_register_driver(&asc_uart_driver);
997 ret = platform_driver_register(&asc_serial_driver);
999 uart_unregister_driver(&asc_uart_driver);
1004 static void __exit asc_exit(void)
1006 platform_driver_unregister(&asc_serial_driver);
1007 uart_unregister_driver(&asc_uart_driver);
1010 module_init(asc_init);
1011 module_exit(asc_exit);
1013 MODULE_ALIAS("platform:" DRIVER_NAME);
1014 MODULE_AUTHOR("STMicroelectronics (R&D) Limited");
1015 MODULE_DESCRIPTION("STMicroelectronics ASC serial port driver");
1016 MODULE_LICENSE("GPL");