1 // SPDX-License-Identifier: GPL-2.0+
3 * NXP (Philips) SCC+++(SCN+++) serial driver
5 * Copyright (C) 2012 Alexander Shiyan <shc_work@mail.ru>
7 * Based on sc26xx.c, by Thomas Bogendörfer (tsbogend@alpha.franken.de)
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/err.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/device.h>
16 #include <linux/console.h>
17 #include <linux/serial_core.h>
18 #include <linux/serial.h>
20 #include <linux/tty.h>
21 #include <linux/tty_flip.h>
22 #include <linux/spinlock.h>
23 #include <linux/platform_device.h>
24 #include <linux/platform_data/serial-sccnxp.h>
25 #include <linux/regulator/consumer.h>
27 #define SCCNXP_NAME "uart-sccnxp"
28 #define SCCNXP_MAJOR 204
29 #define SCCNXP_MINOR 205
31 #define SCCNXP_MR_REG (0x00)
32 # define MR0_BAUD_NORMAL (0 << 0)
33 # define MR0_BAUD_EXT1 (1 << 0)
34 # define MR0_BAUD_EXT2 (5 << 0)
35 # define MR0_FIFO (1 << 3)
36 # define MR0_TXLVL (1 << 4)
37 # define MR1_BITS_5 (0 << 0)
38 # define MR1_BITS_6 (1 << 0)
39 # define MR1_BITS_7 (2 << 0)
40 # define MR1_BITS_8 (3 << 0)
41 # define MR1_PAR_EVN (0 << 2)
42 # define MR1_PAR_ODD (1 << 2)
43 # define MR1_PAR_NO (4 << 2)
44 # define MR2_STOP1 (7 << 0)
45 # define MR2_STOP2 (0xf << 0)
46 #define SCCNXP_SR_REG (0x01)
47 # define SR_RXRDY (1 << 0)
48 # define SR_FULL (1 << 1)
49 # define SR_TXRDY (1 << 2)
50 # define SR_TXEMT (1 << 3)
51 # define SR_OVR (1 << 4)
52 # define SR_PE (1 << 5)
53 # define SR_FE (1 << 6)
54 # define SR_BRK (1 << 7)
55 #define SCCNXP_CSR_REG (SCCNXP_SR_REG)
56 # define CSR_TIMER_MODE (0x0d)
57 #define SCCNXP_CR_REG (0x02)
58 # define CR_RX_ENABLE (1 << 0)
59 # define CR_RX_DISABLE (1 << 1)
60 # define CR_TX_ENABLE (1 << 2)
61 # define CR_TX_DISABLE (1 << 3)
62 # define CR_CMD_MRPTR1 (0x01 << 4)
63 # define CR_CMD_RX_RESET (0x02 << 4)
64 # define CR_CMD_TX_RESET (0x03 << 4)
65 # define CR_CMD_STATUS_RESET (0x04 << 4)
66 # define CR_CMD_BREAK_RESET (0x05 << 4)
67 # define CR_CMD_START_BREAK (0x06 << 4)
68 # define CR_CMD_STOP_BREAK (0x07 << 4)
69 # define CR_CMD_MRPTR0 (0x0b << 4)
70 #define SCCNXP_RHR_REG (0x03)
71 #define SCCNXP_THR_REG SCCNXP_RHR_REG
72 #define SCCNXP_IPCR_REG (0x04)
73 #define SCCNXP_ACR_REG SCCNXP_IPCR_REG
74 # define ACR_BAUD0 (0 << 7)
75 # define ACR_BAUD1 (1 << 7)
76 # define ACR_TIMER_MODE (6 << 4)
77 #define SCCNXP_ISR_REG (0x05)
78 #define SCCNXP_IMR_REG SCCNXP_ISR_REG
79 # define IMR_TXRDY (1 << 0)
80 # define IMR_RXRDY (1 << 1)
81 # define ISR_TXRDY(x) (1 << ((x * 4) + 0))
82 # define ISR_RXRDY(x) (1 << ((x * 4) + 1))
83 #define SCCNXP_CTPU_REG (0x06)
84 #define SCCNXP_CTPL_REG (0x07)
85 #define SCCNXP_IPR_REG (0x0d)
86 #define SCCNXP_OPCR_REG SCCNXP_IPR_REG
87 #define SCCNXP_SOP_REG (0x0e)
88 #define SCCNXP_START_COUNTER_REG SCCNXP_SOP_REG
89 #define SCCNXP_ROP_REG (0x0f)
92 #define MCTRL_MASK(sig) (0xf << (sig))
93 #define MCTRL_IBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_IP0)
94 #define MCTRL_OBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_OP0)
96 #define SCCNXP_HAVE_IO 0x00000001
97 #define SCCNXP_HAVE_MR0 0x00000002
102 unsigned long freq_min
;
103 unsigned long freq_std
;
104 unsigned long freq_max
;
106 unsigned int fifosize
;
107 /* Time between read/write cycles */
112 struct uart_driver uart
;
113 struct uart_port port
[SCCNXP_MAX_UARTS
];
114 bool opened
[SCCNXP_MAX_UARTS
];
119 struct sccnxp_chip
*chip
;
121 #ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
122 struct console console
;
128 struct timer_list timer
;
130 struct sccnxp_pdata pdata
;
132 struct regulator
*regulator
;
135 static const struct sccnxp_chip sc2681
= {
141 .flags
= SCCNXP_HAVE_IO
,
146 static const struct sccnxp_chip sc2691
= {
157 static const struct sccnxp_chip sc2692
= {
163 .flags
= SCCNXP_HAVE_IO
,
168 static const struct sccnxp_chip sc2891
= {
174 .flags
= SCCNXP_HAVE_IO
| SCCNXP_HAVE_MR0
,
179 static const struct sccnxp_chip sc2892
= {
185 .flags
= SCCNXP_HAVE_IO
| SCCNXP_HAVE_MR0
,
190 static const struct sccnxp_chip sc28202
= {
194 .freq_std
= 14745600,
195 .freq_max
= 50000000,
196 .flags
= SCCNXP_HAVE_IO
| SCCNXP_HAVE_MR0
,
201 static const struct sccnxp_chip sc68681
= {
207 .flags
= SCCNXP_HAVE_IO
,
212 static const struct sccnxp_chip sc68692
= {
218 .flags
= SCCNXP_HAVE_IO
,
223 static u8
sccnxp_read(struct uart_port
*port
, u8 reg
)
225 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
228 ret
= readb(port
->membase
+ (reg
<< port
->regshift
));
230 ndelay(s
->chip
->trwd
);
235 static void sccnxp_write(struct uart_port
*port
, u8 reg
, u8 v
)
237 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
239 writeb(v
, port
->membase
+ (reg
<< port
->regshift
));
241 ndelay(s
->chip
->trwd
);
244 static u8
sccnxp_port_read(struct uart_port
*port
, u8 reg
)
246 return sccnxp_read(port
, (port
->line
<< 3) + reg
);
249 static void sccnxp_port_write(struct uart_port
*port
, u8 reg
, u8 v
)
251 sccnxp_write(port
, (port
->line
<< 3) + reg
, v
);
254 static int sccnxp_update_best_err(int a
, int b
, int *besterr
)
256 int err
= abs(a
- b
);
258 if (*besterr
> err
) {
266 static const struct {
272 { 0, ACR_BAUD0
, MR0_BAUD_NORMAL
, 50, },
273 { 0, ACR_BAUD1
, MR0_BAUD_NORMAL
, 75, },
274 { 1, ACR_BAUD0
, MR0_BAUD_NORMAL
, 110, },
275 { 2, ACR_BAUD0
, MR0_BAUD_NORMAL
, 134, },
276 { 3, ACR_BAUD1
, MR0_BAUD_NORMAL
, 150, },
277 { 3, ACR_BAUD0
, MR0_BAUD_NORMAL
, 200, },
278 { 4, ACR_BAUD0
, MR0_BAUD_NORMAL
, 300, },
279 { 0, ACR_BAUD1
, MR0_BAUD_EXT1
, 450, },
280 { 1, ACR_BAUD0
, MR0_BAUD_EXT2
, 880, },
281 { 3, ACR_BAUD1
, MR0_BAUD_EXT1
, 900, },
282 { 5, ACR_BAUD0
, MR0_BAUD_NORMAL
, 600, },
283 { 7, ACR_BAUD0
, MR0_BAUD_NORMAL
, 1050, },
284 { 2, ACR_BAUD0
, MR0_BAUD_EXT2
, 1076, },
285 { 6, ACR_BAUD0
, MR0_BAUD_NORMAL
, 1200, },
286 { 10, ACR_BAUD1
, MR0_BAUD_NORMAL
, 1800, },
287 { 7, ACR_BAUD1
, MR0_BAUD_NORMAL
, 2000, },
288 { 8, ACR_BAUD0
, MR0_BAUD_NORMAL
, 2400, },
289 { 5, ACR_BAUD1
, MR0_BAUD_EXT1
, 3600, },
290 { 9, ACR_BAUD0
, MR0_BAUD_NORMAL
, 4800, },
291 { 10, ACR_BAUD0
, MR0_BAUD_NORMAL
, 7200, },
292 { 11, ACR_BAUD0
, MR0_BAUD_NORMAL
, 9600, },
293 { 8, ACR_BAUD0
, MR0_BAUD_EXT1
, 14400, },
294 { 12, ACR_BAUD1
, MR0_BAUD_NORMAL
, 19200, },
295 { 9, ACR_BAUD0
, MR0_BAUD_EXT1
, 28800, },
296 { 12, ACR_BAUD0
, MR0_BAUD_NORMAL
, 38400, },
297 { 11, ACR_BAUD0
, MR0_BAUD_EXT1
, 57600, },
298 { 12, ACR_BAUD1
, MR0_BAUD_EXT1
, 115200, },
299 { 12, ACR_BAUD0
, MR0_BAUD_EXT1
, 230400, },
303 static int sccnxp_set_baud(struct uart_port
*port
, int baud
)
305 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
306 int div_std
, tmp_baud
, bestbaud
= INT_MAX
, besterr
= INT_MAX
;
307 struct sccnxp_chip
*chip
= s
->chip
;
308 u8 i
, acr
= 0, csr
= 0, mr0
= 0;
310 /* Find divisor to load to the timer preset registers */
311 div_std
= DIV_ROUND_CLOSEST(port
->uartclk
, 2 * 16 * baud
);
312 if ((div_std
>= 2) && (div_std
<= 0xffff)) {
313 bestbaud
= DIV_ROUND_CLOSEST(port
->uartclk
, 2 * 16 * div_std
);
314 sccnxp_update_best_err(baud
, bestbaud
, &besterr
);
315 csr
= CSR_TIMER_MODE
;
316 sccnxp_port_write(port
, SCCNXP_CTPU_REG
, div_std
>> 8);
317 sccnxp_port_write(port
, SCCNXP_CTPL_REG
, div_std
);
318 /* Issue start timer/counter command */
319 sccnxp_port_read(port
, SCCNXP_START_COUNTER_REG
);
322 /* Find best baud from table */
323 for (i
= 0; baud_std
[i
].baud
&& besterr
; i
++) {
324 if (baud_std
[i
].mr0
&& !(chip
->flags
& SCCNXP_HAVE_MR0
))
326 div_std
= DIV_ROUND_CLOSEST(chip
->freq_std
, baud_std
[i
].baud
);
327 tmp_baud
= DIV_ROUND_CLOSEST(port
->uartclk
, div_std
);
328 if (!sccnxp_update_best_err(baud
, tmp_baud
, &besterr
)) {
329 acr
= baud_std
[i
].acr
;
330 csr
= baud_std
[i
].csr
;
331 mr0
= baud_std
[i
].mr0
;
336 if (chip
->flags
& SCCNXP_HAVE_MR0
) {
337 /* Enable FIFO, set half level for TX */
338 mr0
|= MR0_FIFO
| MR0_TXLVL
;
340 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_MRPTR0
);
341 sccnxp_port_write(port
, SCCNXP_MR_REG
, mr0
);
344 sccnxp_port_write(port
, SCCNXP_ACR_REG
, acr
| ACR_TIMER_MODE
);
345 sccnxp_port_write(port
, SCCNXP_CSR_REG
, (csr
<< 4) | csr
);
347 if (baud
!= bestbaud
)
348 dev_dbg(port
->dev
, "Baudrate desired: %i, calculated: %i\n",
354 static void sccnxp_enable_irq(struct uart_port
*port
, int mask
)
356 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
358 s
->imr
|= mask
<< (port
->line
* 4);
359 sccnxp_write(port
, SCCNXP_IMR_REG
, s
->imr
);
362 static void sccnxp_disable_irq(struct uart_port
*port
, int mask
)
364 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
366 s
->imr
&= ~(mask
<< (port
->line
* 4));
367 sccnxp_write(port
, SCCNXP_IMR_REG
, s
->imr
);
370 static void sccnxp_set_bit(struct uart_port
*port
, int sig
, int state
)
373 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
375 if (s
->pdata
.mctrl_cfg
[port
->line
] & MCTRL_MASK(sig
)) {
376 bitmask
= 1 << MCTRL_OBIT(s
->pdata
.mctrl_cfg
[port
->line
], sig
);
378 sccnxp_write(port
, SCCNXP_SOP_REG
, bitmask
);
380 sccnxp_write(port
, SCCNXP_ROP_REG
, bitmask
);
384 static void sccnxp_handle_rx(struct uart_port
*port
)
389 sr
= sccnxp_port_read(port
, SCCNXP_SR_REG
);
390 if (!(sr
& SR_RXRDY
))
392 sr
&= SR_PE
| SR_FE
| SR_OVR
| SR_BRK
;
394 ch
= sccnxp_port_read(port
, SCCNXP_RHR_REG
);
402 sccnxp_port_write(port
, SCCNXP_CR_REG
,
404 if (uart_handle_break(port
))
406 } else if (sr
& SR_PE
)
407 port
->icount
.parity
++;
409 port
->icount
.frame
++;
410 else if (sr
& SR_OVR
) {
411 port
->icount
.overrun
++;
412 sccnxp_port_write(port
, SCCNXP_CR_REG
,
413 CR_CMD_STATUS_RESET
);
416 sr
&= port
->read_status_mask
;
423 else if (sr
& SR_OVR
)
427 if (uart_handle_sysrq_char(port
, ch
))
430 if (sr
& port
->ignore_status_mask
)
433 uart_insert_char(port
, sr
, SR_OVR
, ch
, flag
);
436 tty_flip_buffer_push(&port
->state
->port
);
439 static void sccnxp_handle_tx(struct uart_port
*port
)
442 struct tty_port
*tport
= &port
->state
->port
;
443 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
445 if (unlikely(port
->x_char
)) {
446 sccnxp_port_write(port
, SCCNXP_THR_REG
, port
->x_char
);
452 if (kfifo_is_empty(&tport
->xmit_fifo
) || uart_tx_stopped(port
)) {
453 /* Disable TX if FIFO is empty */
454 if (sccnxp_port_read(port
, SCCNXP_SR_REG
) & SR_TXEMT
) {
455 sccnxp_disable_irq(port
, IMR_TXRDY
);
457 /* Set direction to input */
458 if (s
->chip
->flags
& SCCNXP_HAVE_IO
)
459 sccnxp_set_bit(port
, DIR_OP
, 0);
467 sr
= sccnxp_port_read(port
, SCCNXP_SR_REG
);
468 if (!(sr
& SR_TXRDY
))
471 if (!uart_fifo_get(port
, &ch
))
474 sccnxp_port_write(port
, SCCNXP_THR_REG
, ch
);
477 if (kfifo_len(&tport
->xmit_fifo
) < WAKEUP_CHARS
)
478 uart_write_wakeup(port
);
481 static void sccnxp_handle_events(struct sccnxp_port
*s
)
487 isr
= sccnxp_read(&s
->port
[0], SCCNXP_ISR_REG
);
492 for (i
= 0; i
< s
->uart
.nr
; i
++) {
493 if (s
->opened
[i
] && (isr
& ISR_RXRDY(i
)))
494 sccnxp_handle_rx(&s
->port
[i
]);
495 if (s
->opened
[i
] && (isr
& ISR_TXRDY(i
)))
496 sccnxp_handle_tx(&s
->port
[i
]);
501 static void sccnxp_timer(struct timer_list
*t
)
503 struct sccnxp_port
*s
= from_timer(s
, t
, timer
);
506 spin_lock_irqsave(&s
->lock
, flags
);
507 sccnxp_handle_events(s
);
508 spin_unlock_irqrestore(&s
->lock
, flags
);
510 mod_timer(&s
->timer
, jiffies
+ usecs_to_jiffies(s
->pdata
.poll_time_us
));
513 static irqreturn_t
sccnxp_ist(int irq
, void *dev_id
)
515 struct sccnxp_port
*s
= (struct sccnxp_port
*)dev_id
;
518 spin_lock_irqsave(&s
->lock
, flags
);
519 sccnxp_handle_events(s
);
520 spin_unlock_irqrestore(&s
->lock
, flags
);
525 static void sccnxp_start_tx(struct uart_port
*port
)
527 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
530 spin_lock_irqsave(&s
->lock
, flags
);
532 /* Set direction to output */
533 if (s
->chip
->flags
& SCCNXP_HAVE_IO
)
534 sccnxp_set_bit(port
, DIR_OP
, 1);
536 sccnxp_enable_irq(port
, IMR_TXRDY
);
538 spin_unlock_irqrestore(&s
->lock
, flags
);
541 static void sccnxp_stop_tx(struct uart_port
*port
)
546 static void sccnxp_stop_rx(struct uart_port
*port
)
548 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
551 spin_lock_irqsave(&s
->lock
, flags
);
552 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_RX_DISABLE
);
553 spin_unlock_irqrestore(&s
->lock
, flags
);
556 static unsigned int sccnxp_tx_empty(struct uart_port
*port
)
560 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
562 spin_lock_irqsave(&s
->lock
, flags
);
563 val
= sccnxp_port_read(port
, SCCNXP_SR_REG
);
564 spin_unlock_irqrestore(&s
->lock
, flags
);
566 return (val
& SR_TXEMT
) ? TIOCSER_TEMT
: 0;
569 static void sccnxp_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
571 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
574 if (!(s
->chip
->flags
& SCCNXP_HAVE_IO
))
577 spin_lock_irqsave(&s
->lock
, flags
);
579 sccnxp_set_bit(port
, DTR_OP
, mctrl
& TIOCM_DTR
);
580 sccnxp_set_bit(port
, RTS_OP
, mctrl
& TIOCM_RTS
);
582 spin_unlock_irqrestore(&s
->lock
, flags
);
585 static unsigned int sccnxp_get_mctrl(struct uart_port
*port
)
589 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
590 unsigned int mctrl
= TIOCM_DSR
| TIOCM_CTS
| TIOCM_CAR
;
592 if (!(s
->chip
->flags
& SCCNXP_HAVE_IO
))
595 spin_lock_irqsave(&s
->lock
, flags
);
597 ipr
= ~sccnxp_read(port
, SCCNXP_IPCR_REG
);
599 if (s
->pdata
.mctrl_cfg
[port
->line
] & MCTRL_MASK(DSR_IP
)) {
600 bitmask
= 1 << MCTRL_IBIT(s
->pdata
.mctrl_cfg
[port
->line
],
603 mctrl
|= (ipr
& bitmask
) ? TIOCM_DSR
: 0;
605 if (s
->pdata
.mctrl_cfg
[port
->line
] & MCTRL_MASK(CTS_IP
)) {
606 bitmask
= 1 << MCTRL_IBIT(s
->pdata
.mctrl_cfg
[port
->line
],
609 mctrl
|= (ipr
& bitmask
) ? TIOCM_CTS
: 0;
611 if (s
->pdata
.mctrl_cfg
[port
->line
] & MCTRL_MASK(DCD_IP
)) {
612 bitmask
= 1 << MCTRL_IBIT(s
->pdata
.mctrl_cfg
[port
->line
],
615 mctrl
|= (ipr
& bitmask
) ? TIOCM_CAR
: 0;
617 if (s
->pdata
.mctrl_cfg
[port
->line
] & MCTRL_MASK(RNG_IP
)) {
618 bitmask
= 1 << MCTRL_IBIT(s
->pdata
.mctrl_cfg
[port
->line
],
621 mctrl
|= (ipr
& bitmask
) ? TIOCM_RNG
: 0;
624 spin_unlock_irqrestore(&s
->lock
, flags
);
629 static void sccnxp_break_ctl(struct uart_port
*port
, int break_state
)
631 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
634 spin_lock_irqsave(&s
->lock
, flags
);
635 sccnxp_port_write(port
, SCCNXP_CR_REG
, break_state
?
636 CR_CMD_START_BREAK
: CR_CMD_STOP_BREAK
);
637 spin_unlock_irqrestore(&s
->lock
, flags
);
640 static void sccnxp_set_termios(struct uart_port
*port
,
641 struct ktermios
*termios
,
642 const struct ktermios
*old
)
644 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
649 spin_lock_irqsave(&s
->lock
, flags
);
651 /* Mask termios capabilities we don't support */
652 termios
->c_cflag
&= ~CMSPAR
;
654 /* Disable RX & TX, reset break condition, status and FIFOs */
655 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_RX_RESET
|
656 CR_RX_DISABLE
| CR_TX_DISABLE
);
657 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_TX_RESET
);
658 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_STATUS_RESET
);
659 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_BREAK_RESET
);
662 switch (termios
->c_cflag
& CSIZE
) {
679 if (termios
->c_cflag
& PARENB
) {
680 if (termios
->c_cflag
& PARODD
)
686 mr2
= (termios
->c_cflag
& CSTOPB
) ? MR2_STOP2
: MR2_STOP1
;
688 /* Update desired format */
689 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_MRPTR1
);
690 sccnxp_port_write(port
, SCCNXP_MR_REG
, mr1
);
691 sccnxp_port_write(port
, SCCNXP_MR_REG
, mr2
);
693 /* Set read status mask */
694 port
->read_status_mask
= SR_OVR
;
695 if (termios
->c_iflag
& INPCK
)
696 port
->read_status_mask
|= SR_PE
| SR_FE
;
697 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
698 port
->read_status_mask
|= SR_BRK
;
700 /* Set status ignore mask */
701 port
->ignore_status_mask
= 0;
702 if (termios
->c_iflag
& IGNBRK
)
703 port
->ignore_status_mask
|= SR_BRK
;
704 if (termios
->c_iflag
& IGNPAR
)
705 port
->ignore_status_mask
|= SR_PE
;
706 if (!(termios
->c_cflag
& CREAD
))
707 port
->ignore_status_mask
|= SR_PE
| SR_OVR
| SR_FE
| SR_BRK
;
710 baud
= uart_get_baud_rate(port
, termios
, old
, 50,
711 (s
->chip
->flags
& SCCNXP_HAVE_MR0
) ?
713 baud
= sccnxp_set_baud(port
, baud
);
715 /* Update timeout according to new baud rate */
716 uart_update_timeout(port
, termios
->c_cflag
, baud
);
718 /* Report actual baudrate back to core */
719 if (tty_termios_baud_rate(termios
))
720 tty_termios_encode_baud_rate(termios
, baud
, baud
);
723 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_RX_ENABLE
| CR_TX_ENABLE
);
725 spin_unlock_irqrestore(&s
->lock
, flags
);
728 static int sccnxp_startup(struct uart_port
*port
)
730 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
733 spin_lock_irqsave(&s
->lock
, flags
);
735 if (s
->chip
->flags
& SCCNXP_HAVE_IO
) {
736 /* Outputs are controlled manually */
737 sccnxp_write(port
, SCCNXP_OPCR_REG
, 0);
740 /* Reset break condition, status and FIFOs */
741 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_RX_RESET
);
742 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_TX_RESET
);
743 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_STATUS_RESET
);
744 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_BREAK_RESET
);
747 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_RX_ENABLE
| CR_TX_ENABLE
);
749 /* Enable RX interrupt */
750 sccnxp_enable_irq(port
, IMR_RXRDY
);
752 s
->opened
[port
->line
] = 1;
754 spin_unlock_irqrestore(&s
->lock
, flags
);
759 static void sccnxp_shutdown(struct uart_port
*port
)
761 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
764 spin_lock_irqsave(&s
->lock
, flags
);
766 s
->opened
[port
->line
] = 0;
768 /* Disable interrupts */
769 sccnxp_disable_irq(port
, IMR_TXRDY
| IMR_RXRDY
);
771 /* Disable TX & RX */
772 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_RX_DISABLE
| CR_TX_DISABLE
);
774 /* Leave direction to input */
775 if (s
->chip
->flags
& SCCNXP_HAVE_IO
)
776 sccnxp_set_bit(port
, DIR_OP
, 0);
778 spin_unlock_irqrestore(&s
->lock
, flags
);
781 static const char *sccnxp_type(struct uart_port
*port
)
783 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
785 return (port
->type
== PORT_SC26XX
) ? s
->chip
->name
: NULL
;
788 static void sccnxp_release_port(struct uart_port
*port
)
793 static int sccnxp_request_port(struct uart_port
*port
)
799 static void sccnxp_config_port(struct uart_port
*port
, int flags
)
801 if (flags
& UART_CONFIG_TYPE
)
802 port
->type
= PORT_SC26XX
;
805 static int sccnxp_verify_port(struct uart_port
*port
, struct serial_struct
*s
)
807 if ((s
->type
== PORT_UNKNOWN
) || (s
->type
== PORT_SC26XX
))
809 if (s
->irq
== port
->irq
)
815 static const struct uart_ops sccnxp_ops
= {
816 .tx_empty
= sccnxp_tx_empty
,
817 .set_mctrl
= sccnxp_set_mctrl
,
818 .get_mctrl
= sccnxp_get_mctrl
,
819 .stop_tx
= sccnxp_stop_tx
,
820 .start_tx
= sccnxp_start_tx
,
821 .stop_rx
= sccnxp_stop_rx
,
822 .break_ctl
= sccnxp_break_ctl
,
823 .startup
= sccnxp_startup
,
824 .shutdown
= sccnxp_shutdown
,
825 .set_termios
= sccnxp_set_termios
,
827 .release_port
= sccnxp_release_port
,
828 .request_port
= sccnxp_request_port
,
829 .config_port
= sccnxp_config_port
,
830 .verify_port
= sccnxp_verify_port
,
833 #ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
834 static void sccnxp_console_putchar(struct uart_port
*port
, unsigned char c
)
839 if (sccnxp_port_read(port
, SCCNXP_SR_REG
) & SR_TXRDY
) {
840 sccnxp_port_write(port
, SCCNXP_THR_REG
, c
);
847 static void sccnxp_console_write(struct console
*co
, const char *c
, unsigned n
)
849 struct sccnxp_port
*s
= (struct sccnxp_port
*)co
->data
;
850 struct uart_port
*port
= &s
->port
[co
->index
];
853 spin_lock_irqsave(&s
->lock
, flags
);
854 uart_console_write(port
, c
, n
, sccnxp_console_putchar
);
855 spin_unlock_irqrestore(&s
->lock
, flags
);
858 static int sccnxp_console_setup(struct console
*co
, char *options
)
860 struct sccnxp_port
*s
= (struct sccnxp_port
*)co
->data
;
861 struct uart_port
*port
= &s
->port
[(co
->index
> 0) ? co
->index
: 0];
862 int baud
= 9600, bits
= 8, parity
= 'n', flow
= 'n';
865 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
867 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
871 static const struct platform_device_id sccnxp_id_table
[] = {
872 { .name
= "sc2681", .driver_data
= (kernel_ulong_t
)&sc2681
, },
873 { .name
= "sc2691", .driver_data
= (kernel_ulong_t
)&sc2691
, },
874 { .name
= "sc2692", .driver_data
= (kernel_ulong_t
)&sc2692
, },
875 { .name
= "sc2891", .driver_data
= (kernel_ulong_t
)&sc2891
, },
876 { .name
= "sc2892", .driver_data
= (kernel_ulong_t
)&sc2892
, },
877 { .name
= "sc28202", .driver_data
= (kernel_ulong_t
)&sc28202
, },
878 { .name
= "sc68681", .driver_data
= (kernel_ulong_t
)&sc68681
, },
879 { .name
= "sc68692", .driver_data
= (kernel_ulong_t
)&sc68692
, },
882 MODULE_DEVICE_TABLE(platform
, sccnxp_id_table
);
884 static int sccnxp_probe(struct platform_device
*pdev
)
886 struct sccnxp_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
887 struct resource
*res
;
889 struct sccnxp_port
*s
;
890 void __iomem
*membase
;
893 membase
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
895 return PTR_ERR(membase
);
897 s
= devm_kzalloc(&pdev
->dev
, sizeof(struct sccnxp_port
), GFP_KERNEL
);
899 dev_err(&pdev
->dev
, "Error allocating port structure\n");
902 platform_set_drvdata(pdev
, s
);
904 spin_lock_init(&s
->lock
);
906 s
->chip
= (struct sccnxp_chip
*)pdev
->id_entry
->driver_data
;
908 s
->regulator
= devm_regulator_get(&pdev
->dev
, "vcc");
909 if (!IS_ERR(s
->regulator
)) {
910 ret
= regulator_enable(s
->regulator
);
913 "Failed to enable regulator: %i\n", ret
);
916 } else if (PTR_ERR(s
->regulator
) == -EPROBE_DEFER
)
917 return -EPROBE_DEFER
;
919 clk
= devm_clk_get_enabled(&pdev
->dev
, NULL
);
922 if (ret
== -EPROBE_DEFER
)
926 uartclk
= clk_get_rate(clk
);
930 dev_notice(&pdev
->dev
, "Using default clock frequency\n");
931 uartclk
= s
->chip
->freq_std
;
934 /* Check input frequency */
935 if ((uartclk
< s
->chip
->freq_min
) || (uartclk
> s
->chip
->freq_max
)) {
936 dev_err(&pdev
->dev
, "Frequency out of bounds\n");
942 memcpy(&s
->pdata
, pdata
, sizeof(struct sccnxp_pdata
));
944 if (s
->pdata
.poll_time_us
) {
945 dev_info(&pdev
->dev
, "Using poll mode, resolution %u usecs\n",
946 s
->pdata
.poll_time_us
);
951 s
->irq
= platform_get_irq(pdev
, 0);
958 s
->uart
.owner
= THIS_MODULE
;
959 s
->uart
.dev_name
= "ttySC";
960 s
->uart
.major
= SCCNXP_MAJOR
;
961 s
->uart
.minor
= SCCNXP_MINOR
;
962 s
->uart
.nr
= s
->chip
->nr
;
963 #ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
964 s
->uart
.cons
= &s
->console
;
965 s
->uart
.cons
->device
= uart_console_device
;
966 s
->uart
.cons
->write
= sccnxp_console_write
;
967 s
->uart
.cons
->setup
= sccnxp_console_setup
;
968 s
->uart
.cons
->flags
= CON_PRINTBUFFER
;
969 s
->uart
.cons
->index
= -1;
970 s
->uart
.cons
->data
= s
;
971 strcpy(s
->uart
.cons
->name
, "ttySC");
973 ret
= uart_register_driver(&s
->uart
);
975 dev_err(&pdev
->dev
, "Registering UART driver failed\n");
979 for (i
= 0; i
< s
->uart
.nr
; i
++) {
981 s
->port
[i
].dev
= &pdev
->dev
;
982 s
->port
[i
].irq
= s
->irq
;
983 s
->port
[i
].type
= PORT_SC26XX
;
984 s
->port
[i
].fifosize
= s
->chip
->fifosize
;
985 s
->port
[i
].flags
= UPF_SKIP_TEST
| UPF_FIXED_TYPE
;
986 s
->port
[i
].iotype
= UPIO_MEM
;
987 s
->port
[i
].mapbase
= res
->start
;
988 s
->port
[i
].membase
= membase
;
989 s
->port
[i
].regshift
= s
->pdata
.reg_shift
;
990 s
->port
[i
].uartclk
= uartclk
;
991 s
->port
[i
].ops
= &sccnxp_ops
;
992 s
->port
[i
].has_sysrq
= IS_ENABLED(CONFIG_SERIAL_SCCNXP_CONSOLE
);
993 uart_add_one_port(&s
->uart
, &s
->port
[i
]);
994 /* Set direction to input */
995 if (s
->chip
->flags
& SCCNXP_HAVE_IO
)
996 sccnxp_set_bit(&s
->port
[i
], DIR_OP
, 0);
999 /* Disable interrupts */
1001 sccnxp_write(&s
->port
[0], SCCNXP_IMR_REG
, 0);
1004 ret
= devm_request_threaded_irq(&pdev
->dev
, s
->irq
, NULL
,
1006 IRQF_TRIGGER_FALLING
|
1008 dev_name(&pdev
->dev
), s
);
1012 dev_err(&pdev
->dev
, "Unable to reguest IRQ %i\n", s
->irq
);
1014 timer_setup(&s
->timer
, sccnxp_timer
, 0);
1015 mod_timer(&s
->timer
, jiffies
+
1016 usecs_to_jiffies(s
->pdata
.poll_time_us
));
1020 uart_unregister_driver(&s
->uart
);
1022 if (!IS_ERR(s
->regulator
))
1023 regulator_disable(s
->regulator
);
1028 static void sccnxp_remove(struct platform_device
*pdev
)
1031 struct sccnxp_port
*s
= platform_get_drvdata(pdev
);
1034 devm_free_irq(&pdev
->dev
, s
->irq
, s
);
1036 del_timer_sync(&s
->timer
);
1038 for (i
= 0; i
< s
->uart
.nr
; i
++)
1039 uart_remove_one_port(&s
->uart
, &s
->port
[i
]);
1041 uart_unregister_driver(&s
->uart
);
1043 if (!IS_ERR(s
->regulator
)) {
1044 int ret
= regulator_disable(s
->regulator
);
1046 dev_err(&pdev
->dev
, "Failed to disable regulator\n");
1050 static struct platform_driver sccnxp_uart_driver
= {
1052 .name
= SCCNXP_NAME
,
1054 .probe
= sccnxp_probe
,
1055 .remove
= sccnxp_remove
,
1056 .id_table
= sccnxp_id_table
,
1058 module_platform_driver(sccnxp_uart_driver
);
1060 MODULE_LICENSE("GPL v2");
1061 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
1062 MODULE_DESCRIPTION("SCCNXP serial driver");