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_fwnode_get_gpiod_from_child(port
->dev
,
584 ascport
->rts
= gpiod
;
588 if ((baud
< 19200) && !ascport
->force_m1
) {
589 asc_out(port
, ASC_BAUDRATE
, (port
->uartclk
/ (16 * baud
)));
592 * MODE 1: recommended for high bit rates (above 19.2K)
594 * baudrate * 16 * 2^16
595 * ASCBaudRate = ------------------------
598 * To keep maths inside 64bits, we divide inputclock by 16.
600 u64 dividend
= (u64
)baud
* (1 << 16);
602 do_div(dividend
, port
->uartclk
/ 16);
603 asc_out(port
, ASC_BAUDRATE
, dividend
);
604 ctrl_val
|= ASC_CTL_BAUDMODE
;
607 uart_update_timeout(port
, cflag
, baud
);
609 ascport
->port
.read_status_mask
= ASC_RXBUF_DUMMY_OE
;
610 if (termios
->c_iflag
& INPCK
)
611 ascport
->port
.read_status_mask
|= ASC_RXBUF_FE
| ASC_RXBUF_PE
;
612 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
613 ascport
->port
.read_status_mask
|= ASC_RXBUF_DUMMY_BE
;
616 * Characters to ignore
618 ascport
->port
.ignore_status_mask
= 0;
619 if (termios
->c_iflag
& IGNPAR
)
620 ascport
->port
.ignore_status_mask
|= ASC_RXBUF_FE
| ASC_RXBUF_PE
;
621 if (termios
->c_iflag
& IGNBRK
) {
622 ascport
->port
.ignore_status_mask
|= ASC_RXBUF_DUMMY_BE
;
624 * If we're ignoring parity and break indicators,
625 * ignore overruns too (for real raw support).
627 if (termios
->c_iflag
& IGNPAR
)
628 ascport
->port
.ignore_status_mask
|= ASC_RXBUF_DUMMY_OE
;
632 * Ignore all characters if CREAD is not set.
634 if (!(termios
->c_cflag
& CREAD
))
635 ascport
->port
.ignore_status_mask
|= ASC_RXBUF_DUMMY_RX
;
637 /* Set the timeout */
638 asc_out(port
, ASC_TIMEOUT
, 20);
640 /* write final value and enable port */
641 asc_out(port
, ASC_CTL
, (ctrl_val
| ASC_CTL_RUN
));
643 spin_unlock_irqrestore(&port
->lock
, flags
);
646 static const char *asc_type(struct uart_port
*port
)
648 return (port
->type
== PORT_ASC
) ? DRIVER_NAME
: NULL
;
651 static void asc_release_port(struct uart_port
*port
)
655 static int asc_request_port(struct uart_port
*port
)
661 * Called when the port is opened, and UPF_BOOT_AUTOCONF flag is set
662 * Set type field if successful
664 static void asc_config_port(struct uart_port
*port
, int flags
)
666 if ((flags
& UART_CONFIG_TYPE
))
667 port
->type
= PORT_ASC
;
671 asc_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
673 /* No user changeable parameters */
677 #ifdef CONFIG_CONSOLE_POLL
679 * Console polling routines for writing and reading from the uart while
680 * in an interrupt or debug context (i.e. kgdb).
683 static int asc_get_poll_char(struct uart_port
*port
)
685 if (!(asc_in(port
, ASC_STA
) & ASC_STA_RBF
))
688 return asc_in(port
, ASC_RXBUF
);
691 static void asc_put_poll_char(struct uart_port
*port
, unsigned char c
)
693 while (!asc_txfifo_is_half_empty(port
))
695 asc_out(port
, ASC_TXBUF
, c
);
698 #endif /* CONFIG_CONSOLE_POLL */
700 /*---------------------------------------------------------------------*/
702 static const struct uart_ops asc_uart_ops
= {
703 .tx_empty
= asc_tx_empty
,
704 .set_mctrl
= asc_set_mctrl
,
705 .get_mctrl
= asc_get_mctrl
,
706 .start_tx
= asc_start_tx
,
707 .stop_tx
= asc_stop_tx
,
708 .stop_rx
= asc_stop_rx
,
709 .break_ctl
= asc_break_ctl
,
710 .startup
= asc_startup
,
711 .shutdown
= asc_shutdown
,
712 .set_termios
= asc_set_termios
,
714 .release_port
= asc_release_port
,
715 .request_port
= asc_request_port
,
716 .config_port
= asc_config_port
,
717 .verify_port
= asc_verify_port
,
719 #ifdef CONFIG_CONSOLE_POLL
720 .poll_get_char
= asc_get_poll_char
,
721 .poll_put_char
= asc_put_poll_char
,
722 #endif /* CONFIG_CONSOLE_POLL */
725 static int asc_init_port(struct asc_port
*ascport
,
726 struct platform_device
*pdev
)
728 struct uart_port
*port
= &ascport
->port
;
729 struct resource
*res
;
732 port
->iotype
= UPIO_MEM
;
733 port
->flags
= UPF_BOOT_AUTOCONF
;
734 port
->ops
= &asc_uart_ops
;
735 port
->fifosize
= ASC_FIFO_SIZE
;
736 port
->dev
= &pdev
->dev
;
737 port
->irq
= platform_get_irq(pdev
, 0);
739 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
740 port
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
741 if (IS_ERR(port
->membase
))
742 return PTR_ERR(port
->membase
);
743 port
->mapbase
= res
->start
;
745 spin_lock_init(&port
->lock
);
747 ascport
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
749 if (WARN_ON(IS_ERR(ascport
->clk
)))
751 /* ensure that clk rate is correct by enabling the clk */
752 clk_prepare_enable(ascport
->clk
);
753 ascport
->port
.uartclk
= clk_get_rate(ascport
->clk
);
754 WARN_ON(ascport
->port
.uartclk
== 0);
755 clk_disable_unprepare(ascport
->clk
);
757 ascport
->pinctrl
= devm_pinctrl_get(&pdev
->dev
);
758 if (IS_ERR(ascport
->pinctrl
)) {
759 ret
= PTR_ERR(ascport
->pinctrl
);
760 dev_err(&pdev
->dev
, "Failed to get Pinctrl: %d\n", ret
);
763 ascport
->states
[DEFAULT
] =
764 pinctrl_lookup_state(ascport
->pinctrl
, "default");
765 if (IS_ERR(ascport
->states
[DEFAULT
])) {
766 ret
= PTR_ERR(ascport
->states
[DEFAULT
]);
768 "Failed to look up Pinctrl state 'default': %d\n", ret
);
772 /* "no-hw-flowctrl" state is optional */
773 ascport
->states
[NO_HW_FLOWCTRL
] =
774 pinctrl_lookup_state(ascport
->pinctrl
, "no-hw-flowctrl");
775 if (IS_ERR(ascport
->states
[NO_HW_FLOWCTRL
]))
776 ascport
->states
[NO_HW_FLOWCTRL
] = NULL
;
781 static struct asc_port
*asc_of_get_asc_port(struct platform_device
*pdev
)
783 struct device_node
*np
= pdev
->dev
.of_node
;
789 id
= of_alias_get_id(np
, ASC_SERIAL_NAME
);
794 if (WARN_ON(id
>= ASC_MAX_PORTS
))
797 asc_ports
[id
].hw_flow_control
= of_property_read_bool(np
,
799 asc_ports
[id
].force_m1
= of_property_read_bool(np
, "st,force_m1");
800 asc_ports
[id
].port
.line
= id
;
801 asc_ports
[id
].rts
= NULL
;
803 return &asc_ports
[id
];
807 static const struct of_device_id asc_match
[] = {
808 { .compatible
= "st,asc", },
812 MODULE_DEVICE_TABLE(of
, asc_match
);
815 static int asc_serial_probe(struct platform_device
*pdev
)
818 struct asc_port
*ascport
;
820 ascport
= asc_of_get_asc_port(pdev
);
824 ret
= asc_init_port(ascport
, pdev
);
828 ret
= uart_add_one_port(&asc_uart_driver
, &ascport
->port
);
832 platform_set_drvdata(pdev
, &ascport
->port
);
837 static int asc_serial_remove(struct platform_device
*pdev
)
839 struct uart_port
*port
= platform_get_drvdata(pdev
);
841 return uart_remove_one_port(&asc_uart_driver
, port
);
844 #ifdef CONFIG_PM_SLEEP
845 static int asc_serial_suspend(struct device
*dev
)
847 struct platform_device
*pdev
= to_platform_device(dev
);
848 struct uart_port
*port
= platform_get_drvdata(pdev
);
850 return uart_suspend_port(&asc_uart_driver
, port
);
853 static int asc_serial_resume(struct device
*dev
)
855 struct platform_device
*pdev
= to_platform_device(dev
);
856 struct uart_port
*port
= platform_get_drvdata(pdev
);
858 return uart_resume_port(&asc_uart_driver
, port
);
861 #endif /* CONFIG_PM_SLEEP */
863 /*----------------------------------------------------------------------*/
865 #ifdef CONFIG_SERIAL_ST_ASC_CONSOLE
866 static void asc_console_putchar(struct uart_port
*port
, int ch
)
868 unsigned int timeout
= 1000000;
870 /* Wait for upto 1 second in case flow control is stopping us. */
871 while (--timeout
&& !asc_txfifo_is_half_empty(port
))
874 asc_out(port
, ASC_TXBUF
, ch
);
878 * Print a string to the serial port trying not to disturb
879 * any possible real use of the port...
882 static void asc_console_write(struct console
*co
, const char *s
, unsigned count
)
884 struct uart_port
*port
= &asc_ports
[co
->index
].port
;
886 unsigned long timeout
= 1000000;
891 locked
= 0; /* asc_interrupt has already claimed the lock */
892 else if (oops_in_progress
)
893 locked
= spin_trylock_irqsave(&port
->lock
, flags
);
895 spin_lock_irqsave(&port
->lock
, flags
);
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_irqrestore(&port
->lock
, flags
);
916 static int asc_console_setup(struct console
*co
, char *options
)
918 struct asc_port
*ascport
;
924 if (co
->index
>= ASC_MAX_PORTS
)
927 ascport
= &asc_ports
[co
->index
];
930 * This driver does not support early console initialization
931 * (use ARM early printk support instead), so we only expect
932 * this to be called during the uart port registration when the
933 * driver gets probed and the port should be mapped at that point.
935 if (ascport
->port
.mapbase
== 0 || ascport
->port
.membase
== NULL
)
939 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
941 return uart_set_options(&ascport
->port
, co
, baud
, parity
, bits
, flow
);
944 static struct console asc_console
= {
945 .name
= ASC_SERIAL_NAME
,
946 .device
= uart_console_device
,
947 .write
= asc_console_write
,
948 .setup
= asc_console_setup
,
949 .flags
= CON_PRINTBUFFER
,
951 .data
= &asc_uart_driver
,
954 #define ASC_SERIAL_CONSOLE (&asc_console)
957 #define ASC_SERIAL_CONSOLE NULL
958 #endif /* CONFIG_SERIAL_ST_ASC_CONSOLE */
960 static struct uart_driver asc_uart_driver
= {
961 .owner
= THIS_MODULE
,
962 .driver_name
= DRIVER_NAME
,
963 .dev_name
= ASC_SERIAL_NAME
,
967 .cons
= ASC_SERIAL_CONSOLE
,
970 static const struct dev_pm_ops asc_serial_pm_ops
= {
971 SET_SYSTEM_SLEEP_PM_OPS(asc_serial_suspend
, asc_serial_resume
)
974 static struct platform_driver asc_serial_driver
= {
975 .probe
= asc_serial_probe
,
976 .remove
= asc_serial_remove
,
979 .pm
= &asc_serial_pm_ops
,
980 .of_match_table
= of_match_ptr(asc_match
),
984 static int __init
asc_init(void)
987 static const char banner
[] __initconst
=
988 KERN_INFO
"STMicroelectronics ASC driver initialized\n";
992 ret
= uart_register_driver(&asc_uart_driver
);
996 ret
= platform_driver_register(&asc_serial_driver
);
998 uart_unregister_driver(&asc_uart_driver
);
1003 static void __exit
asc_exit(void)
1005 platform_driver_unregister(&asc_serial_driver
);
1006 uart_unregister_driver(&asc_uart_driver
);
1009 module_init(asc_init
);
1010 module_exit(asc_exit
);
1012 MODULE_ALIAS("platform:" DRIVER_NAME
);
1013 MODULE_AUTHOR("STMicroelectronics (R&D) Limited");
1014 MODULE_DESCRIPTION("STMicroelectronics ASC serial port driver");
1015 MODULE_LICENSE("GPL");