1 // SPDX-License-Identifier: GPL-2.0+
3 * NXP (Philips) SCC+++(SCN+++) serial driver
10 #if defined(CONFIG_SERIAL_SCCNXP_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
14 #include <linux/clk.h>
15 #include <linux/err.h>
16 #include <linux/module.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/device.h>
19 #include <linux/console.h>
20 #include <linux/serial_core.h>
21 #include <linux/serial.h>
23 #include <linux/tty.h>
24 #include <linux/tty_flip.h>
25 #include <linux/spinlock.h>
26 #include <linux/platform_device.h>
27 #include <linux/platform_data/serial-sccnxp.h>
28 #include <linux/regulator/consumer.h>
30 #define SCCNXP_NAME "uart-sccnxp"
31 #define SCCNXP_MAJOR 204
32 #define SCCNXP_MINOR 205
34 #define SCCNXP_MR_REG (0x00)
35 # define MR0_BAUD_NORMAL (0 << 0)
36 # define MR0_BAUD_EXT1 (1 << 0)
37 # define MR0_BAUD_EXT2 (5 << 0)
38 # define MR0_FIFO (1 << 3)
39 # define MR0_TXLVL (1 << 4)
40 # define MR1_BITS_5 (0 << 0)
41 # define MR1_BITS_6 (1 << 0)
42 # define MR1_BITS_7 (2 << 0)
43 # define MR1_BITS_8 (3 << 0)
44 # define MR1_PAR_EVN (0 << 2)
45 # define MR1_PAR_ODD (1 << 2)
46 # define MR1_PAR_NO (4 << 2)
47 # define MR2_STOP1 (7 << 0)
48 # define MR2_STOP2 (0xf << 0)
49 #define SCCNXP_SR_REG (0x01)
50 #define SCCNXP_CSR_REG SCCNXP_SR_REG
51 # define SR_RXRDY (1 << 0)
52 # define SR_FULL (1 << 1)
53 # define SR_TXRDY (1 << 2)
54 # define SR_TXEMT (1 << 3)
55 # define SR_OVR (1 << 4)
56 # define SR_PE (1 << 5)
57 # define SR_FE (1 << 6)
58 # define SR_BRK (1 << 7)
59 #define SCCNXP_CR_REG (0x02)
60 # define CR_RX_ENABLE (1 << 0)
61 # define CR_RX_DISABLE (1 << 1)
62 # define CR_TX_ENABLE (1 << 2)
63 # define CR_TX_DISABLE (1 << 3)
64 # define CR_CMD_MRPTR1 (0x01 << 4)
65 # define CR_CMD_RX_RESET (0x02 << 4)
66 # define CR_CMD_TX_RESET (0x03 << 4)
67 # define CR_CMD_STATUS_RESET (0x04 << 4)
68 # define CR_CMD_BREAK_RESET (0x05 << 4)
69 # define CR_CMD_START_BREAK (0x06 << 4)
70 # define CR_CMD_STOP_BREAK (0x07 << 4)
71 # define CR_CMD_MRPTR0 (0x0b << 4)
72 #define SCCNXP_RHR_REG (0x03)
73 #define SCCNXP_THR_REG SCCNXP_RHR_REG
74 #define SCCNXP_IPCR_REG (0x04)
75 #define SCCNXP_ACR_REG SCCNXP_IPCR_REG
76 # define ACR_BAUD0 (0 << 7)
77 # define ACR_BAUD1 (1 << 7)
78 # define ACR_TIMER_MODE (6 << 4)
79 #define SCCNXP_ISR_REG (0x05)
80 #define SCCNXP_IMR_REG SCCNXP_ISR_REG
81 # define IMR_TXRDY (1 << 0)
82 # define IMR_RXRDY (1 << 1)
83 # define ISR_TXRDY(x) (1 << ((x * 4) + 0))
84 # define ISR_RXRDY(x) (1 << ((x * 4) + 1))
85 #define SCCNXP_IPR_REG (0x0d)
86 #define SCCNXP_OPCR_REG SCCNXP_IPR_REG
87 #define SCCNXP_SOP_REG (0x0e)
88 #define SCCNXP_ROP_REG (0x0f)
91 #define MCTRL_MASK(sig) (0xf << (sig))
92 #define MCTRL_IBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_IP0)
93 #define MCTRL_OBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_OP0)
95 #define SCCNXP_HAVE_IO 0x00000001
96 #define SCCNXP_HAVE_MR0 0x00000002
101 unsigned long freq_min;
102 unsigned long freq_std;
103 unsigned long freq_max;
105 unsigned int fifosize;
109 struct uart_driver uart;
110 struct uart_port port[SCCNXP_MAX_UARTS];
111 bool opened[SCCNXP_MAX_UARTS];
116 struct sccnxp_chip *chip;
118 #ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
119 struct console console;
125 struct timer_list timer;
127 struct sccnxp_pdata pdata;
129 struct regulator *regulator;
132 static const struct sccnxp_chip sc2681 = {
138 .flags = SCCNXP_HAVE_IO,
142 static const struct sccnxp_chip sc2691 = {
152 static const struct sccnxp_chip sc2692 = {
158 .flags = SCCNXP_HAVE_IO,
162 static const struct sccnxp_chip sc2891 = {
168 .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
172 static const struct sccnxp_chip sc2892 = {
178 .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
182 static const struct sccnxp_chip sc28202 = {
186 .freq_std = 14745600,
187 .freq_max = 50000000,
188 .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
192 static const struct sccnxp_chip sc68681 = {
198 .flags = SCCNXP_HAVE_IO,
202 static const struct sccnxp_chip sc68692 = {
208 .flags = SCCNXP_HAVE_IO,
212 static inline u8 sccnxp_read(struct uart_port *port, u8 reg)
214 return readb(port->membase + (reg << port->regshift));
217 static inline void sccnxp_write(struct uart_port *port, u8 reg, u8 v)
219 writeb(v, port->membase + (reg << port->regshift));
222 static inline u8 sccnxp_port_read(struct uart_port *port, u8 reg)
224 return sccnxp_read(port, (port->line << 3) + reg);
227 static inline void sccnxp_port_write(struct uart_port *port, u8 reg, u8 v)
229 sccnxp_write(port, (port->line << 3) + reg, v);
232 static int sccnxp_update_best_err(int a, int b, int *besterr)
234 int err = abs(a - b);
236 if ((*besterr < 0) || (*besterr > err)) {
244 static const struct {
250 { 0, ACR_BAUD0, MR0_BAUD_NORMAL, 50, },
251 { 0, ACR_BAUD1, MR0_BAUD_NORMAL, 75, },
252 { 1, ACR_BAUD0, MR0_BAUD_NORMAL, 110, },
253 { 2, ACR_BAUD0, MR0_BAUD_NORMAL, 134, },
254 { 3, ACR_BAUD1, MR0_BAUD_NORMAL, 150, },
255 { 3, ACR_BAUD0, MR0_BAUD_NORMAL, 200, },
256 { 4, ACR_BAUD0, MR0_BAUD_NORMAL, 300, },
257 { 0, ACR_BAUD1, MR0_BAUD_EXT1, 450, },
258 { 1, ACR_BAUD0, MR0_BAUD_EXT2, 880, },
259 { 3, ACR_BAUD1, MR0_BAUD_EXT1, 900, },
260 { 5, ACR_BAUD0, MR0_BAUD_NORMAL, 600, },
261 { 7, ACR_BAUD0, MR0_BAUD_NORMAL, 1050, },
262 { 2, ACR_BAUD0, MR0_BAUD_EXT2, 1076, },
263 { 6, ACR_BAUD0, MR0_BAUD_NORMAL, 1200, },
264 { 10, ACR_BAUD1, MR0_BAUD_NORMAL, 1800, },
265 { 7, ACR_BAUD1, MR0_BAUD_NORMAL, 2000, },
266 { 8, ACR_BAUD0, MR0_BAUD_NORMAL, 2400, },
267 { 5, ACR_BAUD1, MR0_BAUD_EXT1, 3600, },
268 { 9, ACR_BAUD0, MR0_BAUD_NORMAL, 4800, },
269 { 10, ACR_BAUD0, MR0_BAUD_NORMAL, 7200, },
270 { 11, ACR_BAUD0, MR0_BAUD_NORMAL, 9600, },
271 { 8, ACR_BAUD0, MR0_BAUD_EXT1, 14400, },
272 { 12, ACR_BAUD1, MR0_BAUD_NORMAL, 19200, },
273 { 9, ACR_BAUD0, MR0_BAUD_EXT1, 28800, },
274 { 12, ACR_BAUD0, MR0_BAUD_NORMAL, 38400, },
275 { 11, ACR_BAUD0, MR0_BAUD_EXT1, 57600, },
276 { 12, ACR_BAUD1, MR0_BAUD_EXT1, 115200, },
277 { 12, ACR_BAUD0, MR0_BAUD_EXT1, 230400, },
281 static int sccnxp_set_baud(struct uart_port *port, int baud)
283 struct sccnxp_port *s = dev_get_drvdata(port->dev);
284 int div_std, tmp_baud, bestbaud = baud, besterr = -1;
285 struct sccnxp_chip *chip = s->chip;
286 u8 i, acr = 0, csr = 0, mr0 = 0;
288 /* Find best baud from table */
289 for (i = 0; baud_std[i].baud && besterr; i++) {
290 if (baud_std[i].mr0 && !(chip->flags & SCCNXP_HAVE_MR0))
292 div_std = DIV_ROUND_CLOSEST(chip->freq_std, baud_std[i].baud);
293 tmp_baud = DIV_ROUND_CLOSEST(port->uartclk, div_std);
294 if (!sccnxp_update_best_err(baud, tmp_baud, &besterr)) {
295 acr = baud_std[i].acr;
296 csr = baud_std[i].csr;
297 mr0 = baud_std[i].mr0;
302 if (chip->flags & SCCNXP_HAVE_MR0) {
303 /* Enable FIFO, set half level for TX */
304 mr0 |= MR0_FIFO | MR0_TXLVL;
306 sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_MRPTR0);
307 sccnxp_port_write(port, SCCNXP_MR_REG, mr0);
310 sccnxp_port_write(port, SCCNXP_ACR_REG, acr | ACR_TIMER_MODE);
311 sccnxp_port_write(port, SCCNXP_CSR_REG, (csr << 4) | csr);
313 if (baud != bestbaud)
314 dev_dbg(port->dev, "Baudrate desired: %i, calculated: %i\n",
320 static void sccnxp_enable_irq(struct uart_port *port, int mask)
322 struct sccnxp_port *s = dev_get_drvdata(port->dev);
324 s->imr |= mask << (port->line * 4);
325 sccnxp_write(port, SCCNXP_IMR_REG, s->imr);
328 static void sccnxp_disable_irq(struct uart_port *port, int mask)
330 struct sccnxp_port *s = dev_get_drvdata(port->dev);
332 s->imr &= ~(mask << (port->line * 4));
333 sccnxp_write(port, SCCNXP_IMR_REG, s->imr);
336 static void sccnxp_set_bit(struct uart_port *port, int sig, int state)
339 struct sccnxp_port *s = dev_get_drvdata(port->dev);
341 if (s->pdata.mctrl_cfg[port->line] & MCTRL_MASK(sig)) {
342 bitmask = 1 << MCTRL_OBIT(s->pdata.mctrl_cfg[port->line], sig);
344 sccnxp_write(port, SCCNXP_SOP_REG, bitmask);
346 sccnxp_write(port, SCCNXP_ROP_REG, bitmask);
350 static void sccnxp_handle_rx(struct uart_port *port)
353 unsigned int ch, flag;
356 sr = sccnxp_port_read(port, SCCNXP_SR_REG);
357 if (!(sr & SR_RXRDY))
359 sr &= SR_PE | SR_FE | SR_OVR | SR_BRK;
361 ch = sccnxp_port_read(port, SCCNXP_RHR_REG);
369 sccnxp_port_write(port, SCCNXP_CR_REG,
371 if (uart_handle_break(port))
373 } else if (sr & SR_PE)
374 port->icount.parity++;
376 port->icount.frame++;
377 else if (sr & SR_OVR) {
378 port->icount.overrun++;
379 sccnxp_port_write(port, SCCNXP_CR_REG,
380 CR_CMD_STATUS_RESET);
383 sr &= port->read_status_mask;
390 else if (sr & SR_OVR)
394 if (uart_handle_sysrq_char(port, ch))
397 if (sr & port->ignore_status_mask)
400 uart_insert_char(port, sr, SR_OVR, ch, flag);
403 tty_flip_buffer_push(&port->state->port);
406 static void sccnxp_handle_tx(struct uart_port *port)
409 struct circ_buf *xmit = &port->state->xmit;
410 struct sccnxp_port *s = dev_get_drvdata(port->dev);
412 if (unlikely(port->x_char)) {
413 sccnxp_port_write(port, SCCNXP_THR_REG, port->x_char);
419 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
420 /* Disable TX if FIFO is empty */
421 if (sccnxp_port_read(port, SCCNXP_SR_REG) & SR_TXEMT) {
422 sccnxp_disable_irq(port, IMR_TXRDY);
424 /* Set direction to input */
425 if (s->chip->flags & SCCNXP_HAVE_IO)
426 sccnxp_set_bit(port, DIR_OP, 0);
431 while (!uart_circ_empty(xmit)) {
432 sr = sccnxp_port_read(port, SCCNXP_SR_REG);
433 if (!(sr & SR_TXRDY))
436 sccnxp_port_write(port, SCCNXP_THR_REG, xmit->buf[xmit->tail]);
437 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
441 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
442 uart_write_wakeup(port);
445 static void sccnxp_handle_events(struct sccnxp_port *s)
451 isr = sccnxp_read(&s->port[0], SCCNXP_ISR_REG);
456 for (i = 0; i < s->uart.nr; i++) {
457 if (s->opened[i] && (isr & ISR_RXRDY(i)))
458 sccnxp_handle_rx(&s->port[i]);
459 if (s->opened[i] && (isr & ISR_TXRDY(i)))
460 sccnxp_handle_tx(&s->port[i]);
465 static void sccnxp_timer(struct timer_list *t)
467 struct sccnxp_port *s = from_timer(s, t, timer);
470 spin_lock_irqsave(&s->lock, flags);
471 sccnxp_handle_events(s);
472 spin_unlock_irqrestore(&s->lock, flags);
474 mod_timer(&s->timer, jiffies + usecs_to_jiffies(s->pdata.poll_time_us));
477 static irqreturn_t sccnxp_ist(int irq, void *dev_id)
479 struct sccnxp_port *s = (struct sccnxp_port *)dev_id;
482 spin_lock_irqsave(&s->lock, flags);
483 sccnxp_handle_events(s);
484 spin_unlock_irqrestore(&s->lock, flags);
489 static void sccnxp_start_tx(struct uart_port *port)
491 struct sccnxp_port *s = dev_get_drvdata(port->dev);
494 spin_lock_irqsave(&s->lock, flags);
496 /* Set direction to output */
497 if (s->chip->flags & SCCNXP_HAVE_IO)
498 sccnxp_set_bit(port, DIR_OP, 1);
500 sccnxp_enable_irq(port, IMR_TXRDY);
502 spin_unlock_irqrestore(&s->lock, flags);
505 static void sccnxp_stop_tx(struct uart_port *port)
510 static void sccnxp_stop_rx(struct uart_port *port)
512 struct sccnxp_port *s = dev_get_drvdata(port->dev);
515 spin_lock_irqsave(&s->lock, flags);
516 sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_DISABLE);
517 spin_unlock_irqrestore(&s->lock, flags);
520 static unsigned int sccnxp_tx_empty(struct uart_port *port)
524 struct sccnxp_port *s = dev_get_drvdata(port->dev);
526 spin_lock_irqsave(&s->lock, flags);
527 val = sccnxp_port_read(port, SCCNXP_SR_REG);
528 spin_unlock_irqrestore(&s->lock, flags);
530 return (val & SR_TXEMT) ? TIOCSER_TEMT : 0;
533 static void sccnxp_set_mctrl(struct uart_port *port, unsigned int mctrl)
535 struct sccnxp_port *s = dev_get_drvdata(port->dev);
538 if (!(s->chip->flags & SCCNXP_HAVE_IO))
541 spin_lock_irqsave(&s->lock, flags);
543 sccnxp_set_bit(port, DTR_OP, mctrl & TIOCM_DTR);
544 sccnxp_set_bit(port, RTS_OP, mctrl & TIOCM_RTS);
546 spin_unlock_irqrestore(&s->lock, flags);
549 static unsigned int sccnxp_get_mctrl(struct uart_port *port)
553 struct sccnxp_port *s = dev_get_drvdata(port->dev);
554 unsigned int mctrl = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
556 if (!(s->chip->flags & SCCNXP_HAVE_IO))
559 spin_lock_irqsave(&s->lock, flags);
561 ipr = ~sccnxp_read(port, SCCNXP_IPCR_REG);
563 if (s->pdata.mctrl_cfg[port->line] & MCTRL_MASK(DSR_IP)) {
564 bitmask = 1 << MCTRL_IBIT(s->pdata.mctrl_cfg[port->line],
567 mctrl |= (ipr & bitmask) ? TIOCM_DSR : 0;
569 if (s->pdata.mctrl_cfg[port->line] & MCTRL_MASK(CTS_IP)) {
570 bitmask = 1 << MCTRL_IBIT(s->pdata.mctrl_cfg[port->line],
573 mctrl |= (ipr & bitmask) ? TIOCM_CTS : 0;
575 if (s->pdata.mctrl_cfg[port->line] & MCTRL_MASK(DCD_IP)) {
576 bitmask = 1 << MCTRL_IBIT(s->pdata.mctrl_cfg[port->line],
579 mctrl |= (ipr & bitmask) ? TIOCM_CAR : 0;
581 if (s->pdata.mctrl_cfg[port->line] & MCTRL_MASK(RNG_IP)) {
582 bitmask = 1 << MCTRL_IBIT(s->pdata.mctrl_cfg[port->line],
585 mctrl |= (ipr & bitmask) ? TIOCM_RNG : 0;
588 spin_unlock_irqrestore(&s->lock, flags);
593 static void sccnxp_break_ctl(struct uart_port *port, int break_state)
595 struct sccnxp_port *s = dev_get_drvdata(port->dev);
598 spin_lock_irqsave(&s->lock, flags);
599 sccnxp_port_write(port, SCCNXP_CR_REG, break_state ?
600 CR_CMD_START_BREAK : CR_CMD_STOP_BREAK);
601 spin_unlock_irqrestore(&s->lock, flags);
604 static void sccnxp_set_termios(struct uart_port *port,
605 struct ktermios *termios, struct ktermios *old)
607 struct sccnxp_port *s = dev_get_drvdata(port->dev);
612 spin_lock_irqsave(&s->lock, flags);
614 /* Mask termios capabilities we don't support */
615 termios->c_cflag &= ~CMSPAR;
617 /* Disable RX & TX, reset break condition, status and FIFOs */
618 sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_RX_RESET |
619 CR_RX_DISABLE | CR_TX_DISABLE);
620 sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_TX_RESET);
621 sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_STATUS_RESET);
622 sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_BREAK_RESET);
625 switch (termios->c_cflag & CSIZE) {
642 if (termios->c_cflag & PARENB) {
643 if (termios->c_cflag & PARODD)
649 mr2 = (termios->c_cflag & CSTOPB) ? MR2_STOP2 : MR2_STOP1;
651 /* Update desired format */
652 sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_MRPTR1);
653 sccnxp_port_write(port, SCCNXP_MR_REG, mr1);
654 sccnxp_port_write(port, SCCNXP_MR_REG, mr2);
656 /* Set read status mask */
657 port->read_status_mask = SR_OVR;
658 if (termios->c_iflag & INPCK)
659 port->read_status_mask |= SR_PE | SR_FE;
660 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
661 port->read_status_mask |= SR_BRK;
663 /* Set status ignore mask */
664 port->ignore_status_mask = 0;
665 if (termios->c_iflag & IGNBRK)
666 port->ignore_status_mask |= SR_BRK;
667 if (termios->c_iflag & IGNPAR)
668 port->ignore_status_mask |= SR_PE;
669 if (!(termios->c_cflag & CREAD))
670 port->ignore_status_mask |= SR_PE | SR_OVR | SR_FE | SR_BRK;
673 baud = uart_get_baud_rate(port, termios, old, 50,
674 (s->chip->flags & SCCNXP_HAVE_MR0) ?
676 baud = sccnxp_set_baud(port, baud);
678 /* Update timeout according to new baud rate */
679 uart_update_timeout(port, termios->c_cflag, baud);
681 /* Report actual baudrate back to core */
682 if (tty_termios_baud_rate(termios))
683 tty_termios_encode_baud_rate(termios, baud, baud);
686 sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_ENABLE | CR_TX_ENABLE);
688 spin_unlock_irqrestore(&s->lock, flags);
691 static int sccnxp_startup(struct uart_port *port)
693 struct sccnxp_port *s = dev_get_drvdata(port->dev);
696 spin_lock_irqsave(&s->lock, flags);
698 if (s->chip->flags & SCCNXP_HAVE_IO) {
699 /* Outputs are controlled manually */
700 sccnxp_write(port, SCCNXP_OPCR_REG, 0);
703 /* Reset break condition, status and FIFOs */
704 sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_RX_RESET);
705 sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_TX_RESET);
706 sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_STATUS_RESET);
707 sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_BREAK_RESET);
710 sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_ENABLE | CR_TX_ENABLE);
712 /* Enable RX interrupt */
713 sccnxp_enable_irq(port, IMR_RXRDY);
715 s->opened[port->line] = 1;
717 spin_unlock_irqrestore(&s->lock, flags);
722 static void sccnxp_shutdown(struct uart_port *port)
724 struct sccnxp_port *s = dev_get_drvdata(port->dev);
727 spin_lock_irqsave(&s->lock, flags);
729 s->opened[port->line] = 0;
731 /* Disable interrupts */
732 sccnxp_disable_irq(port, IMR_TXRDY | IMR_RXRDY);
734 /* Disable TX & RX */
735 sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_DISABLE | CR_TX_DISABLE);
737 /* Leave direction to input */
738 if (s->chip->flags & SCCNXP_HAVE_IO)
739 sccnxp_set_bit(port, DIR_OP, 0);
741 spin_unlock_irqrestore(&s->lock, flags);
744 static const char *sccnxp_type(struct uart_port *port)
746 struct sccnxp_port *s = dev_get_drvdata(port->dev);
748 return (port->type == PORT_SC26XX) ? s->chip->name : NULL;
751 static void sccnxp_release_port(struct uart_port *port)
756 static int sccnxp_request_port(struct uart_port *port)
762 static void sccnxp_config_port(struct uart_port *port, int flags)
764 if (flags & UART_CONFIG_TYPE)
765 port->type = PORT_SC26XX;
768 static int sccnxp_verify_port(struct uart_port *port, struct serial_struct *s)
770 if ((s->type == PORT_UNKNOWN) || (s->type == PORT_SC26XX))
772 if (s->irq == port->irq)
778 static const struct uart_ops sccnxp_ops = {
779 .tx_empty = sccnxp_tx_empty,
780 .set_mctrl = sccnxp_set_mctrl,
781 .get_mctrl = sccnxp_get_mctrl,
782 .stop_tx = sccnxp_stop_tx,
783 .start_tx = sccnxp_start_tx,
784 .stop_rx = sccnxp_stop_rx,
785 .break_ctl = sccnxp_break_ctl,
786 .startup = sccnxp_startup,
787 .shutdown = sccnxp_shutdown,
788 .set_termios = sccnxp_set_termios,
790 .release_port = sccnxp_release_port,
791 .request_port = sccnxp_request_port,
792 .config_port = sccnxp_config_port,
793 .verify_port = sccnxp_verify_port,
796 #ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
797 static void sccnxp_console_putchar(struct uart_port *port, int c)
802 if (sccnxp_port_read(port, SCCNXP_SR_REG) & SR_TXRDY) {
803 sccnxp_port_write(port, SCCNXP_THR_REG, c);
810 static void sccnxp_console_write(struct console *co, const char *c, unsigned n)
812 struct sccnxp_port *s = (struct sccnxp_port *)co->data;
813 struct uart_port *port = &s->port[co->index];
816 spin_lock_irqsave(&s->lock, flags);
817 uart_console_write(port, c, n, sccnxp_console_putchar);
818 spin_unlock_irqrestore(&s->lock, flags);
821 static int sccnxp_console_setup(struct console *co, char *options)
823 struct sccnxp_port *s = (struct sccnxp_port *)co->data;
824 struct uart_port *port = &s->port[(co->index > 0) ? co->index : 0];
825 int baud = 9600, bits = 8, parity = 'n', flow = 'n';
828 uart_parse_options(options, &baud, &parity, &bits, &flow);
830 return uart_set_options(port, co, baud, parity, bits, flow);
834 static const struct platform_device_id sccnxp_id_table[] = {
835 { .name = "sc2681", .driver_data = (kernel_ulong_t)&sc2681, },
836 { .name = "sc2691", .driver_data = (kernel_ulong_t)&sc2691, },
837 { .name = "sc2692", .driver_data = (kernel_ulong_t)&sc2692, },
838 { .name = "sc2891", .driver_data = (kernel_ulong_t)&sc2891, },
839 { .name = "sc2892", .driver_data = (kernel_ulong_t)&sc2892, },
840 { .name = "sc28202", .driver_data = (kernel_ulong_t)&sc28202, },
841 { .name = "sc68681", .driver_data = (kernel_ulong_t)&sc68681, },
842 { .name = "sc68692", .driver_data = (kernel_ulong_t)&sc68692, },
845 MODULE_DEVICE_TABLE(platform, sccnxp_id_table);
847 static int sccnxp_probe(struct platform_device *pdev)
849 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
850 struct sccnxp_pdata *pdata = dev_get_platdata(&pdev->dev);
852 struct sccnxp_port *s;
853 void __iomem *membase;
856 membase = devm_ioremap_resource(&pdev->dev, res);
858 return PTR_ERR(membase);
860 s = devm_kzalloc(&pdev->dev, sizeof(struct sccnxp_port), GFP_KERNEL);
862 dev_err(&pdev->dev, "Error allocating port structure\n");
865 platform_set_drvdata(pdev, s);
867 spin_lock_init(&s->lock);
869 s->chip = (struct sccnxp_chip *)pdev->id_entry->driver_data;
871 s->regulator = devm_regulator_get(&pdev->dev, "vcc");
872 if (!IS_ERR(s->regulator)) {
873 ret = regulator_enable(s->regulator);
876 "Failed to enable regulator: %i\n", ret);
879 } else if (PTR_ERR(s->regulator) == -EPROBE_DEFER)
880 return -EPROBE_DEFER;
882 clk = devm_clk_get(&pdev->dev, NULL);
885 if (ret == -EPROBE_DEFER)
889 ret = clk_prepare_enable(clk);
893 ret = devm_add_action_or_reset(&pdev->dev,
894 (void(*)(void *))clk_disable_unprepare,
899 uartclk = clk_get_rate(clk);
903 dev_notice(&pdev->dev, "Using default clock frequency\n");
904 uartclk = s->chip->freq_std;
907 /* Check input frequency */
908 if ((uartclk < s->chip->freq_min) || (uartclk > s->chip->freq_max)) {
909 dev_err(&pdev->dev, "Frequency out of bounds\n");
915 memcpy(&s->pdata, pdata, sizeof(struct sccnxp_pdata));
917 if (s->pdata.poll_time_us) {
918 dev_info(&pdev->dev, "Using poll mode, resolution %u usecs\n",
919 s->pdata.poll_time_us);
924 s->irq = platform_get_irq(pdev, 0);
926 dev_err(&pdev->dev, "Missing irq resource data\n");
932 s->uart.owner = THIS_MODULE;
933 s->uart.dev_name = "ttySC";
934 s->uart.major = SCCNXP_MAJOR;
935 s->uart.minor = SCCNXP_MINOR;
936 s->uart.nr = s->chip->nr;
937 #ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
938 s->uart.cons = &s->console;
939 s->uart.cons->device = uart_console_device;
940 s->uart.cons->write = sccnxp_console_write;
941 s->uart.cons->setup = sccnxp_console_setup;
942 s->uart.cons->flags = CON_PRINTBUFFER;
943 s->uart.cons->index = -1;
944 s->uart.cons->data = s;
945 strcpy(s->uart.cons->name, "ttySC");
947 ret = uart_register_driver(&s->uart);
949 dev_err(&pdev->dev, "Registering UART driver failed\n");
953 for (i = 0; i < s->uart.nr; i++) {
955 s->port[i].dev = &pdev->dev;
956 s->port[i].irq = s->irq;
957 s->port[i].type = PORT_SC26XX;
958 s->port[i].fifosize = s->chip->fifosize;
959 s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
960 s->port[i].iotype = UPIO_MEM;
961 s->port[i].mapbase = res->start;
962 s->port[i].membase = membase;
963 s->port[i].regshift = s->pdata.reg_shift;
964 s->port[i].uartclk = uartclk;
965 s->port[i].ops = &sccnxp_ops;
966 uart_add_one_port(&s->uart, &s->port[i]);
967 /* Set direction to input */
968 if (s->chip->flags & SCCNXP_HAVE_IO)
969 sccnxp_set_bit(&s->port[i], DIR_OP, 0);
972 /* Disable interrupts */
974 sccnxp_write(&s->port[0], SCCNXP_IMR_REG, 0);
977 ret = devm_request_threaded_irq(&pdev->dev, s->irq, NULL,
979 IRQF_TRIGGER_FALLING |
981 dev_name(&pdev->dev), s);
985 dev_err(&pdev->dev, "Unable to reguest IRQ %i\n", s->irq);
987 timer_setup(&s->timer, sccnxp_timer, 0);
988 mod_timer(&s->timer, jiffies +
989 usecs_to_jiffies(s->pdata.poll_time_us));
993 uart_unregister_driver(&s->uart);
995 if (!IS_ERR(s->regulator))
996 regulator_disable(s->regulator);
1001 static int sccnxp_remove(struct platform_device *pdev)
1004 struct sccnxp_port *s = platform_get_drvdata(pdev);
1007 devm_free_irq(&pdev->dev, s->irq, s);
1009 del_timer_sync(&s->timer);
1011 for (i = 0; i < s->uart.nr; i++)
1012 uart_remove_one_port(&s->uart, &s->port[i]);
1014 uart_unregister_driver(&s->uart);
1016 if (!IS_ERR(s->regulator))
1017 return regulator_disable(s->regulator);
1022 static struct platform_driver sccnxp_uart_driver = {
1024 .name = SCCNXP_NAME,
1026 .probe = sccnxp_probe,
1027 .remove = sccnxp_remove,
1028 .id_table = sccnxp_id_table,
1030 module_platform_driver(sccnxp_uart_driver);
1032 MODULE_LICENSE("GPL v2");
1034 MODULE_DESCRIPTION("SCCNXP serial driver");