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)
313 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
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;
550 ctrl_val |= (cflag & PARENB) ? ASC_CTL_MODE_8BIT_PAR :
555 termios->c_cflag = cflag;
558 ctrl_val |= (cflag & CSTOPB) ? ASC_CTL_STOP_2BIT : ASC_CTL_STOP_1BIT;
562 ctrl_val |= ASC_CTL_PARITYODD;
564 /* hardware flow control */
565 if ((cflag & CRTSCTS)) {
566 ctrl_val |= ASC_CTL_CTSENABLE;
568 /* If flow-control selected, stop handling RTS manually */
570 devm_gpiod_put(port->dev, ascport->rts);
573 pinctrl_select_state(ascport->pinctrl,
574 ascport->states[DEFAULT]);
577 /* If flow-control disabled, it's safe to handle RTS manually */
578 if (!ascport->rts && ascport->states[NO_HW_FLOWCTRL]) {
579 pinctrl_select_state(ascport->pinctrl,
580 ascport->states[NO_HW_FLOWCTRL]);
582 gpiod = devm_fwnode_get_gpiod_from_child(port->dev,
588 ascport->rts = gpiod;
592 if ((baud < 19200) && !ascport->force_m1) {
593 asc_out(port, ASC_BAUDRATE, (port->uartclk / (16 * baud)));
596 * MODE 1: recommended for high bit rates (above 19.2K)
598 * baudrate * 16 * 2^16
599 * ASCBaudRate = ------------------------
602 * To keep maths inside 64bits, we divide inputclock by 16.
604 u64 dividend = (u64)baud * (1 << 16);
606 do_div(dividend, port->uartclk / 16);
607 asc_out(port, ASC_BAUDRATE, dividend);
608 ctrl_val |= ASC_CTL_BAUDMODE;
611 uart_update_timeout(port, cflag, baud);
613 ascport->port.read_status_mask = ASC_RXBUF_DUMMY_OE;
614 if (termios->c_iflag & INPCK)
615 ascport->port.read_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
616 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
617 ascport->port.read_status_mask |= ASC_RXBUF_DUMMY_BE;
620 * Characters to ignore
622 ascport->port.ignore_status_mask = 0;
623 if (termios->c_iflag & IGNPAR)
624 ascport->port.ignore_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
625 if (termios->c_iflag & IGNBRK) {
626 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_BE;
628 * If we're ignoring parity and break indicators,
629 * ignore overruns too (for real raw support).
631 if (termios->c_iflag & IGNPAR)
632 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_OE;
636 * Ignore all characters if CREAD is not set.
638 if (!(termios->c_cflag & CREAD))
639 ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_RX;
641 /* Set the timeout */
642 asc_out(port, ASC_TIMEOUT, 20);
644 /* write final value and enable port */
645 asc_out(port, ASC_CTL, (ctrl_val | ASC_CTL_RUN));
647 spin_unlock_irqrestore(&port->lock, flags);
650 static const char *asc_type(struct uart_port *port)
652 return (port->type == PORT_ASC) ? DRIVER_NAME : NULL;
655 static void asc_release_port(struct uart_port *port)
659 static int asc_request_port(struct uart_port *port)
665 * Called when the port is opened, and UPF_BOOT_AUTOCONF flag is set
666 * Set type field if successful
668 static void asc_config_port(struct uart_port *port, int flags)
670 if ((flags & UART_CONFIG_TYPE))
671 port->type = PORT_ASC;
675 asc_verify_port(struct uart_port *port, struct serial_struct *ser)
677 /* No user changeable parameters */
681 #ifdef CONFIG_CONSOLE_POLL
683 * Console polling routines for writing and reading from the uart while
684 * in an interrupt or debug context (i.e. kgdb).
687 static int asc_get_poll_char(struct uart_port *port)
689 if (!(asc_in(port, ASC_STA) & ASC_STA_RBF))
692 return asc_in(port, ASC_RXBUF);
695 static void asc_put_poll_char(struct uart_port *port, unsigned char c)
697 while (!asc_txfifo_is_half_empty(port))
699 asc_out(port, ASC_TXBUF, c);
702 #endif /* CONFIG_CONSOLE_POLL */
704 /*---------------------------------------------------------------------*/
706 static const struct uart_ops asc_uart_ops = {
707 .tx_empty = asc_tx_empty,
708 .set_mctrl = asc_set_mctrl,
709 .get_mctrl = asc_get_mctrl,
710 .start_tx = asc_start_tx,
711 .stop_tx = asc_stop_tx,
712 .stop_rx = asc_stop_rx,
713 .break_ctl = asc_break_ctl,
714 .startup = asc_startup,
715 .shutdown = asc_shutdown,
716 .set_termios = asc_set_termios,
718 .release_port = asc_release_port,
719 .request_port = asc_request_port,
720 .config_port = asc_config_port,
721 .verify_port = asc_verify_port,
723 #ifdef CONFIG_CONSOLE_POLL
724 .poll_get_char = asc_get_poll_char,
725 .poll_put_char = asc_put_poll_char,
726 #endif /* CONFIG_CONSOLE_POLL */
729 static int asc_init_port(struct asc_port *ascport,
730 struct platform_device *pdev)
732 struct uart_port *port = &ascport->port;
733 struct resource *res;
736 port->iotype = UPIO_MEM;
737 port->flags = UPF_BOOT_AUTOCONF;
738 port->ops = &asc_uart_ops;
739 port->fifosize = ASC_FIFO_SIZE;
740 port->dev = &pdev->dev;
741 port->irq = platform_get_irq(pdev, 0);
743 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
744 port->membase = devm_ioremap_resource(&pdev->dev, res);
745 if (IS_ERR(port->membase))
746 return PTR_ERR(port->membase);
747 port->mapbase = res->start;
749 spin_lock_init(&port->lock);
751 ascport->clk = devm_clk_get(&pdev->dev, NULL);
753 if (WARN_ON(IS_ERR(ascport->clk)))
755 /* ensure that clk rate is correct by enabling the clk */
756 clk_prepare_enable(ascport->clk);
757 ascport->port.uartclk = clk_get_rate(ascport->clk);
758 WARN_ON(ascport->port.uartclk == 0);
759 clk_disable_unprepare(ascport->clk);
761 ascport->pinctrl = devm_pinctrl_get(&pdev->dev);
762 if (IS_ERR(ascport->pinctrl)) {
763 ret = PTR_ERR(ascport->pinctrl);
764 dev_err(&pdev->dev, "Failed to get Pinctrl: %d\n", ret);
768 ascport->states[DEFAULT] =
769 pinctrl_lookup_state(ascport->pinctrl, "default");
770 if (IS_ERR(ascport->states[DEFAULT])) {
771 ret = PTR_ERR(ascport->states[DEFAULT]);
773 "Failed to look up Pinctrl state 'default': %d\n", ret);
777 /* "no-hw-flowctrl" state is optional */
778 ascport->states[NO_HW_FLOWCTRL] =
779 pinctrl_lookup_state(ascport->pinctrl, "no-hw-flowctrl");
780 if (IS_ERR(ascport->states[NO_HW_FLOWCTRL]))
781 ascport->states[NO_HW_FLOWCTRL] = NULL;
786 static struct asc_port *asc_of_get_asc_port(struct platform_device *pdev)
788 struct device_node *np = pdev->dev.of_node;
794 id = of_alias_get_id(np, ASC_SERIAL_NAME);
799 if (WARN_ON(id >= ASC_MAX_PORTS))
802 asc_ports[id].hw_flow_control = of_property_read_bool(np,
804 asc_ports[id].force_m1 = of_property_read_bool(np, "st,force_m1");
805 asc_ports[id].port.line = id;
806 asc_ports[id].rts = NULL;
808 return &asc_ports[id];
812 static const struct of_device_id asc_match[] = {
813 { .compatible = "st,asc", },
817 MODULE_DEVICE_TABLE(of, asc_match);
820 static int asc_serial_probe(struct platform_device *pdev)
823 struct asc_port *ascport;
825 ascport = asc_of_get_asc_port(pdev);
829 ret = asc_init_port(ascport, pdev);
833 ret = uart_add_one_port(&asc_uart_driver, &ascport->port);
837 platform_set_drvdata(pdev, &ascport->port);
842 static int asc_serial_remove(struct platform_device *pdev)
844 struct uart_port *port = platform_get_drvdata(pdev);
846 return uart_remove_one_port(&asc_uart_driver, port);
849 #ifdef CONFIG_PM_SLEEP
850 static int asc_serial_suspend(struct device *dev)
852 struct platform_device *pdev = to_platform_device(dev);
853 struct uart_port *port = platform_get_drvdata(pdev);
855 return uart_suspend_port(&asc_uart_driver, port);
858 static int asc_serial_resume(struct device *dev)
860 struct platform_device *pdev = to_platform_device(dev);
861 struct uart_port *port = platform_get_drvdata(pdev);
863 return uart_resume_port(&asc_uart_driver, port);
866 #endif /* CONFIG_PM_SLEEP */
868 /*----------------------------------------------------------------------*/
870 #ifdef CONFIG_SERIAL_ST_ASC_CONSOLE
871 static void asc_console_putchar(struct uart_port *port, int ch)
873 unsigned int timeout = 1000000;
875 /* Wait for upto 1 second in case flow control is stopping us. */
876 while (--timeout && !asc_txfifo_is_half_empty(port))
879 asc_out(port, ASC_TXBUF, ch);
883 * Print a string to the serial port trying not to disturb
884 * any possible real use of the port...
887 static void asc_console_write(struct console *co, const char *s, unsigned count)
889 struct uart_port *port = &asc_ports[co->index].port;
891 unsigned long timeout = 1000000;
896 locked = 0; /* asc_interrupt has already claimed the lock */
897 else if (oops_in_progress)
898 locked = spin_trylock_irqsave(&port->lock, flags);
900 spin_lock_irqsave(&port->lock, flags);
903 * Disable interrupts so we don't get the IRQ line bouncing
904 * up and down while interrupts are disabled.
906 intenable = asc_in(port, ASC_INTEN);
907 asc_out(port, ASC_INTEN, 0);
908 (void)asc_in(port, ASC_INTEN); /* Defeat bus write posting */
910 uart_console_write(port, s, count, asc_console_putchar);
912 while (--timeout && !asc_txfifo_is_empty(port))
915 asc_out(port, ASC_INTEN, intenable);
918 spin_unlock_irqrestore(&port->lock, flags);
921 static int asc_console_setup(struct console *co, char *options)
923 struct asc_port *ascport;
929 if (co->index >= ASC_MAX_PORTS)
932 ascport = &asc_ports[co->index];
935 * This driver does not support early console initialization
936 * (use ARM early printk support instead), so we only expect
937 * this to be called during the uart port registration when the
938 * driver gets probed and the port should be mapped at that point.
940 if (ascport->port.mapbase == 0 || ascport->port.membase == NULL)
944 uart_parse_options(options, &baud, &parity, &bits, &flow);
946 return uart_set_options(&ascport->port, co, baud, parity, bits, flow);
949 static struct console asc_console = {
950 .name = ASC_SERIAL_NAME,
951 .device = uart_console_device,
952 .write = asc_console_write,
953 .setup = asc_console_setup,
954 .flags = CON_PRINTBUFFER,
956 .data = &asc_uart_driver,
959 #define ASC_SERIAL_CONSOLE (&asc_console)
962 #define ASC_SERIAL_CONSOLE NULL
963 #endif /* CONFIG_SERIAL_ST_ASC_CONSOLE */
965 static struct uart_driver asc_uart_driver = {
966 .owner = THIS_MODULE,
967 .driver_name = DRIVER_NAME,
968 .dev_name = ASC_SERIAL_NAME,
972 .cons = ASC_SERIAL_CONSOLE,
975 static const struct dev_pm_ops asc_serial_pm_ops = {
976 SET_SYSTEM_SLEEP_PM_OPS(asc_serial_suspend, asc_serial_resume)
979 static struct platform_driver asc_serial_driver = {
980 .probe = asc_serial_probe,
981 .remove = asc_serial_remove,
984 .pm = &asc_serial_pm_ops,
985 .of_match_table = of_match_ptr(asc_match),
989 static int __init asc_init(void)
992 static const char banner[] __initconst =
993 KERN_INFO "STMicroelectronics ASC driver initialized\n";
997 ret = uart_register_driver(&asc_uart_driver);
1001 ret = platform_driver_register(&asc_serial_driver);
1003 uart_unregister_driver(&asc_uart_driver);
1008 static void __exit asc_exit(void)
1010 platform_driver_unregister(&asc_serial_driver);
1011 uart_unregister_driver(&asc_uart_driver);
1014 module_init(asc_init);
1015 module_exit(asc_exit);
1017 MODULE_ALIAS("platform:" DRIVER_NAME);
1018 MODULE_AUTHOR("STMicroelectronics (R&D) Limited");
1019 MODULE_DESCRIPTION("STMicroelectronics ASC serial port driver");
1020 MODULE_LICENSE("GPL");