2 * NXP (Philips) SCC+++(SCN+++) serial driver
4 * Copyright (C) 2012 Alexander Shiyan <shc_work@mail.ru>
6 * Based on sc26xx.c, by Thomas Bogendörfer (tsbogend@alpha.franken.de)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #if defined(CONFIG_SERIAL_SCCNXP_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18 #include <linux/clk.h>
19 #include <linux/err.h>
20 #include <linux/module.h>
21 #include <linux/device.h>
22 #include <linux/console.h>
23 #include <linux/serial_core.h>
24 #include <linux/serial.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 #include <linux/spinlock.h>
29 #include <linux/platform_device.h>
30 #include <linux/platform_data/serial-sccnxp.h>
31 #include <linux/regulator/consumer.h>
33 #define SCCNXP_NAME "uart-sccnxp"
34 #define SCCNXP_MAJOR 204
35 #define SCCNXP_MINOR 205
37 #define SCCNXP_MR_REG (0x00)
38 # define MR0_BAUD_NORMAL (0 << 0)
39 # define MR0_BAUD_EXT1 (1 << 0)
40 # define MR0_BAUD_EXT2 (5 << 0)
41 # define MR0_FIFO (1 << 3)
42 # define MR0_TXLVL (1 << 4)
43 # define MR1_BITS_5 (0 << 0)
44 # define MR1_BITS_6 (1 << 0)
45 # define MR1_BITS_7 (2 << 0)
46 # define MR1_BITS_8 (3 << 0)
47 # define MR1_PAR_EVN (0 << 2)
48 # define MR1_PAR_ODD (1 << 2)
49 # define MR1_PAR_NO (4 << 2)
50 # define MR2_STOP1 (7 << 0)
51 # define MR2_STOP2 (0xf << 0)
52 #define SCCNXP_SR_REG (0x01)
53 #define SCCNXP_CSR_REG SCCNXP_SR_REG
54 # define SR_RXRDY (1 << 0)
55 # define SR_FULL (1 << 1)
56 # define SR_TXRDY (1 << 2)
57 # define SR_TXEMT (1 << 3)
58 # define SR_OVR (1 << 4)
59 # define SR_PE (1 << 5)
60 # define SR_FE (1 << 6)
61 # define SR_BRK (1 << 7)
62 #define SCCNXP_CR_REG (0x02)
63 # define CR_RX_ENABLE (1 << 0)
64 # define CR_RX_DISABLE (1 << 1)
65 # define CR_TX_ENABLE (1 << 2)
66 # define CR_TX_DISABLE (1 << 3)
67 # define CR_CMD_MRPTR1 (0x01 << 4)
68 # define CR_CMD_RX_RESET (0x02 << 4)
69 # define CR_CMD_TX_RESET (0x03 << 4)
70 # define CR_CMD_STATUS_RESET (0x04 << 4)
71 # define CR_CMD_BREAK_RESET (0x05 << 4)
72 # define CR_CMD_START_BREAK (0x06 << 4)
73 # define CR_CMD_STOP_BREAK (0x07 << 4)
74 # define CR_CMD_MRPTR0 (0x0b << 4)
75 #define SCCNXP_RHR_REG (0x03)
76 #define SCCNXP_THR_REG SCCNXP_RHR_REG
77 #define SCCNXP_IPCR_REG (0x04)
78 #define SCCNXP_ACR_REG SCCNXP_IPCR_REG
79 # define ACR_BAUD0 (0 << 7)
80 # define ACR_BAUD1 (1 << 7)
81 # define ACR_TIMER_MODE (6 << 4)
82 #define SCCNXP_ISR_REG (0x05)
83 #define SCCNXP_IMR_REG SCCNXP_ISR_REG
84 # define IMR_TXRDY (1 << 0)
85 # define IMR_RXRDY (1 << 1)
86 # define ISR_TXRDY(x) (1 << ((x * 4) + 0))
87 # define ISR_RXRDY(x) (1 << ((x * 4) + 1))
88 #define SCCNXP_IPR_REG (0x0d)
89 #define SCCNXP_OPCR_REG SCCNXP_IPR_REG
90 #define SCCNXP_SOP_REG (0x0e)
91 #define SCCNXP_ROP_REG (0x0f)
94 #define MCTRL_MASK(sig) (0xf << (sig))
95 #define MCTRL_IBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_IP0)
96 #define MCTRL_OBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_OP0)
98 #define SCCNXP_HAVE_IO 0x00000001
99 #define SCCNXP_HAVE_MR0 0x00000002
104 unsigned long freq_min
;
105 unsigned long freq_std
;
106 unsigned long freq_max
;
108 unsigned int fifosize
;
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
,
145 static const struct sccnxp_chip sc2691
= {
155 static const struct sccnxp_chip sc2692
= {
161 .flags
= SCCNXP_HAVE_IO
,
165 static const struct sccnxp_chip sc2891
= {
171 .flags
= SCCNXP_HAVE_IO
| SCCNXP_HAVE_MR0
,
175 static const struct sccnxp_chip sc2892
= {
181 .flags
= SCCNXP_HAVE_IO
| SCCNXP_HAVE_MR0
,
185 static const struct sccnxp_chip sc28202
= {
189 .freq_std
= 14745600,
190 .freq_max
= 50000000,
191 .flags
= SCCNXP_HAVE_IO
| SCCNXP_HAVE_MR0
,
195 static const struct sccnxp_chip sc68681
= {
201 .flags
= SCCNXP_HAVE_IO
,
205 static const struct sccnxp_chip sc68692
= {
211 .flags
= SCCNXP_HAVE_IO
,
215 static inline u8
sccnxp_read(struct uart_port
*port
, u8 reg
)
217 return readb(port
->membase
+ (reg
<< port
->regshift
));
220 static inline void sccnxp_write(struct uart_port
*port
, u8 reg
, u8 v
)
222 writeb(v
, port
->membase
+ (reg
<< port
->regshift
));
225 static inline u8
sccnxp_port_read(struct uart_port
*port
, u8 reg
)
227 return sccnxp_read(port
, (port
->line
<< 3) + reg
);
230 static inline void sccnxp_port_write(struct uart_port
*port
, u8 reg
, u8 v
)
232 sccnxp_write(port
, (port
->line
<< 3) + reg
, v
);
235 static int sccnxp_update_best_err(int a
, int b
, int *besterr
)
237 int err
= abs(a
- b
);
239 if ((*besterr
< 0) || (*besterr
> err
)) {
247 static const struct {
253 { 0, ACR_BAUD0
, MR0_BAUD_NORMAL
, 50, },
254 { 0, ACR_BAUD1
, MR0_BAUD_NORMAL
, 75, },
255 { 1, ACR_BAUD0
, MR0_BAUD_NORMAL
, 110, },
256 { 2, ACR_BAUD0
, MR0_BAUD_NORMAL
, 134, },
257 { 3, ACR_BAUD1
, MR0_BAUD_NORMAL
, 150, },
258 { 3, ACR_BAUD0
, MR0_BAUD_NORMAL
, 200, },
259 { 4, ACR_BAUD0
, MR0_BAUD_NORMAL
, 300, },
260 { 0, ACR_BAUD1
, MR0_BAUD_EXT1
, 450, },
261 { 1, ACR_BAUD0
, MR0_BAUD_EXT2
, 880, },
262 { 3, ACR_BAUD1
, MR0_BAUD_EXT1
, 900, },
263 { 5, ACR_BAUD0
, MR0_BAUD_NORMAL
, 600, },
264 { 7, ACR_BAUD0
, MR0_BAUD_NORMAL
, 1050, },
265 { 2, ACR_BAUD0
, MR0_BAUD_EXT2
, 1076, },
266 { 6, ACR_BAUD0
, MR0_BAUD_NORMAL
, 1200, },
267 { 10, ACR_BAUD1
, MR0_BAUD_NORMAL
, 1800, },
268 { 7, ACR_BAUD1
, MR0_BAUD_NORMAL
, 2000, },
269 { 8, ACR_BAUD0
, MR0_BAUD_NORMAL
, 2400, },
270 { 5, ACR_BAUD1
, MR0_BAUD_EXT1
, 3600, },
271 { 9, ACR_BAUD0
, MR0_BAUD_NORMAL
, 4800, },
272 { 10, ACR_BAUD0
, MR0_BAUD_NORMAL
, 7200, },
273 { 11, ACR_BAUD0
, MR0_BAUD_NORMAL
, 9600, },
274 { 8, ACR_BAUD0
, MR0_BAUD_EXT1
, 14400, },
275 { 12, ACR_BAUD1
, MR0_BAUD_NORMAL
, 19200, },
276 { 9, ACR_BAUD0
, MR0_BAUD_EXT1
, 28800, },
277 { 12, ACR_BAUD0
, MR0_BAUD_NORMAL
, 38400, },
278 { 11, ACR_BAUD0
, MR0_BAUD_EXT1
, 57600, },
279 { 12, ACR_BAUD1
, MR0_BAUD_EXT1
, 115200, },
280 { 12, ACR_BAUD0
, MR0_BAUD_EXT1
, 230400, },
284 static int sccnxp_set_baud(struct uart_port
*port
, int baud
)
286 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
287 int div_std
, tmp_baud
, bestbaud
= baud
, besterr
= -1;
288 struct sccnxp_chip
*chip
= s
->chip
;
289 u8 i
, acr
= 0, csr
= 0, mr0
= 0;
291 /* Find best baud from table */
292 for (i
= 0; baud_std
[i
].baud
&& besterr
; i
++) {
293 if (baud_std
[i
].mr0
&& !(chip
->flags
& SCCNXP_HAVE_MR0
))
295 div_std
= DIV_ROUND_CLOSEST(chip
->freq_std
, baud_std
[i
].baud
);
296 tmp_baud
= DIV_ROUND_CLOSEST(port
->uartclk
, div_std
);
297 if (!sccnxp_update_best_err(baud
, tmp_baud
, &besterr
)) {
298 acr
= baud_std
[i
].acr
;
299 csr
= baud_std
[i
].csr
;
300 mr0
= baud_std
[i
].mr0
;
305 if (chip
->flags
& SCCNXP_HAVE_MR0
) {
306 /* Enable FIFO, set half level for TX */
307 mr0
|= MR0_FIFO
| MR0_TXLVL
;
309 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_MRPTR0
);
310 sccnxp_port_write(port
, SCCNXP_MR_REG
, mr0
);
313 sccnxp_port_write(port
, SCCNXP_ACR_REG
, acr
| ACR_TIMER_MODE
);
314 sccnxp_port_write(port
, SCCNXP_CSR_REG
, (csr
<< 4) | csr
);
316 if (baud
!= bestbaud
)
317 dev_dbg(port
->dev
, "Baudrate desired: %i, calculated: %i\n",
323 static void sccnxp_enable_irq(struct uart_port
*port
, int mask
)
325 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
327 s
->imr
|= mask
<< (port
->line
* 4);
328 sccnxp_write(port
, SCCNXP_IMR_REG
, s
->imr
);
331 static void sccnxp_disable_irq(struct uart_port
*port
, int mask
)
333 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
335 s
->imr
&= ~(mask
<< (port
->line
* 4));
336 sccnxp_write(port
, SCCNXP_IMR_REG
, s
->imr
);
339 static void sccnxp_set_bit(struct uart_port
*port
, int sig
, int state
)
342 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
344 if (s
->pdata
.mctrl_cfg
[port
->line
] & MCTRL_MASK(sig
)) {
345 bitmask
= 1 << MCTRL_OBIT(s
->pdata
.mctrl_cfg
[port
->line
], sig
);
347 sccnxp_write(port
, SCCNXP_SOP_REG
, bitmask
);
349 sccnxp_write(port
, SCCNXP_ROP_REG
, bitmask
);
353 static void sccnxp_handle_rx(struct uart_port
*port
)
356 unsigned int ch
, flag
;
359 sr
= sccnxp_port_read(port
, SCCNXP_SR_REG
);
360 if (!(sr
& SR_RXRDY
))
362 sr
&= SR_PE
| SR_FE
| SR_OVR
| SR_BRK
;
364 ch
= sccnxp_port_read(port
, SCCNXP_RHR_REG
);
372 sccnxp_port_write(port
, SCCNXP_CR_REG
,
374 if (uart_handle_break(port
))
376 } else if (sr
& SR_PE
)
377 port
->icount
.parity
++;
379 port
->icount
.frame
++;
380 else if (sr
& SR_OVR
) {
381 port
->icount
.overrun
++;
382 sccnxp_port_write(port
, SCCNXP_CR_REG
,
383 CR_CMD_STATUS_RESET
);
386 sr
&= port
->read_status_mask
;
393 else if (sr
& SR_OVR
)
397 if (uart_handle_sysrq_char(port
, ch
))
400 if (sr
& port
->ignore_status_mask
)
403 uart_insert_char(port
, sr
, SR_OVR
, ch
, flag
);
406 tty_flip_buffer_push(&port
->state
->port
);
409 static void sccnxp_handle_tx(struct uart_port
*port
)
412 struct circ_buf
*xmit
= &port
->state
->xmit
;
413 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
415 if (unlikely(port
->x_char
)) {
416 sccnxp_port_write(port
, SCCNXP_THR_REG
, port
->x_char
);
422 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
423 /* Disable TX if FIFO is empty */
424 if (sccnxp_port_read(port
, SCCNXP_SR_REG
) & SR_TXEMT
) {
425 sccnxp_disable_irq(port
, IMR_TXRDY
);
427 /* Set direction to input */
428 if (s
->chip
->flags
& SCCNXP_HAVE_IO
)
429 sccnxp_set_bit(port
, DIR_OP
, 0);
434 while (!uart_circ_empty(xmit
)) {
435 sr
= sccnxp_port_read(port
, SCCNXP_SR_REG
);
436 if (!(sr
& SR_TXRDY
))
439 sccnxp_port_write(port
, SCCNXP_THR_REG
, xmit
->buf
[xmit
->tail
]);
440 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
444 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
445 uart_write_wakeup(port
);
448 static void sccnxp_handle_events(struct sccnxp_port
*s
)
454 isr
= sccnxp_read(&s
->port
[0], SCCNXP_ISR_REG
);
459 for (i
= 0; i
< s
->uart
.nr
; i
++) {
460 if (s
->opened
[i
] && (isr
& ISR_RXRDY(i
)))
461 sccnxp_handle_rx(&s
->port
[i
]);
462 if (s
->opened
[i
] && (isr
& ISR_TXRDY(i
)))
463 sccnxp_handle_tx(&s
->port
[i
]);
468 static void sccnxp_timer(unsigned long data
)
470 struct sccnxp_port
*s
= (struct sccnxp_port
*)data
;
473 spin_lock_irqsave(&s
->lock
, flags
);
474 sccnxp_handle_events(s
);
475 spin_unlock_irqrestore(&s
->lock
, flags
);
477 if (!timer_pending(&s
->timer
))
478 mod_timer(&s
->timer
, jiffies
+
479 usecs_to_jiffies(s
->pdata
.poll_time_us
));
482 static irqreturn_t
sccnxp_ist(int irq
, void *dev_id
)
484 struct sccnxp_port
*s
= (struct sccnxp_port
*)dev_id
;
487 spin_lock_irqsave(&s
->lock
, flags
);
488 sccnxp_handle_events(s
);
489 spin_unlock_irqrestore(&s
->lock
, flags
);
494 static void sccnxp_start_tx(struct uart_port
*port
)
496 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
499 spin_lock_irqsave(&s
->lock
, flags
);
501 /* Set direction to output */
502 if (s
->chip
->flags
& SCCNXP_HAVE_IO
)
503 sccnxp_set_bit(port
, DIR_OP
, 1);
505 sccnxp_enable_irq(port
, IMR_TXRDY
);
507 spin_unlock_irqrestore(&s
->lock
, flags
);
510 static void sccnxp_stop_tx(struct uart_port
*port
)
515 static void sccnxp_stop_rx(struct uart_port
*port
)
517 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
520 spin_lock_irqsave(&s
->lock
, flags
);
521 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_RX_DISABLE
);
522 spin_unlock_irqrestore(&s
->lock
, flags
);
525 static unsigned int sccnxp_tx_empty(struct uart_port
*port
)
529 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
531 spin_lock_irqsave(&s
->lock
, flags
);
532 val
= sccnxp_port_read(port
, SCCNXP_SR_REG
);
533 spin_unlock_irqrestore(&s
->lock
, flags
);
535 return (val
& SR_TXEMT
) ? TIOCSER_TEMT
: 0;
538 static void sccnxp_enable_ms(struct uart_port
*port
)
543 static void sccnxp_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
545 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
548 if (!(s
->chip
->flags
& SCCNXP_HAVE_IO
))
551 spin_lock_irqsave(&s
->lock
, flags
);
553 sccnxp_set_bit(port
, DTR_OP
, mctrl
& TIOCM_DTR
);
554 sccnxp_set_bit(port
, RTS_OP
, mctrl
& TIOCM_RTS
);
556 spin_unlock_irqrestore(&s
->lock
, flags
);
559 static unsigned int sccnxp_get_mctrl(struct uart_port
*port
)
563 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
564 unsigned int mctrl
= TIOCM_DSR
| TIOCM_CTS
| TIOCM_CAR
;
566 if (!(s
->chip
->flags
& SCCNXP_HAVE_IO
))
569 spin_lock_irqsave(&s
->lock
, flags
);
571 ipr
= ~sccnxp_read(port
, SCCNXP_IPCR_REG
);
573 if (s
->pdata
.mctrl_cfg
[port
->line
] & MCTRL_MASK(DSR_IP
)) {
574 bitmask
= 1 << MCTRL_IBIT(s
->pdata
.mctrl_cfg
[port
->line
],
577 mctrl
|= (ipr
& bitmask
) ? TIOCM_DSR
: 0;
579 if (s
->pdata
.mctrl_cfg
[port
->line
] & MCTRL_MASK(CTS_IP
)) {
580 bitmask
= 1 << MCTRL_IBIT(s
->pdata
.mctrl_cfg
[port
->line
],
583 mctrl
|= (ipr
& bitmask
) ? TIOCM_CTS
: 0;
585 if (s
->pdata
.mctrl_cfg
[port
->line
] & MCTRL_MASK(DCD_IP
)) {
586 bitmask
= 1 << MCTRL_IBIT(s
->pdata
.mctrl_cfg
[port
->line
],
589 mctrl
|= (ipr
& bitmask
) ? TIOCM_CAR
: 0;
591 if (s
->pdata
.mctrl_cfg
[port
->line
] & MCTRL_MASK(RNG_IP
)) {
592 bitmask
= 1 << MCTRL_IBIT(s
->pdata
.mctrl_cfg
[port
->line
],
595 mctrl
|= (ipr
& bitmask
) ? TIOCM_RNG
: 0;
598 spin_unlock_irqrestore(&s
->lock
, flags
);
603 static void sccnxp_break_ctl(struct uart_port
*port
, int break_state
)
605 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
608 spin_lock_irqsave(&s
->lock
, flags
);
609 sccnxp_port_write(port
, SCCNXP_CR_REG
, break_state
?
610 CR_CMD_START_BREAK
: CR_CMD_STOP_BREAK
);
611 spin_unlock_irqrestore(&s
->lock
, flags
);
614 static void sccnxp_set_termios(struct uart_port
*port
,
615 struct ktermios
*termios
, struct ktermios
*old
)
617 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
622 spin_lock_irqsave(&s
->lock
, flags
);
624 /* Mask termios capabilities we don't support */
625 termios
->c_cflag
&= ~CMSPAR
;
627 /* Disable RX & TX, reset break condition, status and FIFOs */
628 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_RX_RESET
|
629 CR_RX_DISABLE
| CR_TX_DISABLE
);
630 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_TX_RESET
);
631 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_STATUS_RESET
);
632 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_BREAK_RESET
);
635 switch (termios
->c_cflag
& CSIZE
) {
652 if (termios
->c_cflag
& PARENB
) {
653 if (termios
->c_cflag
& PARODD
)
659 mr2
= (termios
->c_cflag
& CSTOPB
) ? MR2_STOP2
: MR2_STOP1
;
661 /* Update desired format */
662 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_MRPTR1
);
663 sccnxp_port_write(port
, SCCNXP_MR_REG
, mr1
);
664 sccnxp_port_write(port
, SCCNXP_MR_REG
, mr2
);
666 /* Set read status mask */
667 port
->read_status_mask
= SR_OVR
;
668 if (termios
->c_iflag
& INPCK
)
669 port
->read_status_mask
|= SR_PE
| SR_FE
;
670 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
671 port
->read_status_mask
|= SR_BRK
;
673 /* Set status ignore mask */
674 port
->ignore_status_mask
= 0;
675 if (termios
->c_iflag
& IGNBRK
)
676 port
->ignore_status_mask
|= SR_BRK
;
677 if (!(termios
->c_cflag
& CREAD
))
678 port
->ignore_status_mask
|= SR_PE
| SR_OVR
| SR_FE
| SR_BRK
;
681 baud
= uart_get_baud_rate(port
, termios
, old
, 50,
682 (s
->chip
->flags
& SCCNXP_HAVE_MR0
) ?
684 baud
= sccnxp_set_baud(port
, baud
);
686 /* Update timeout according to new baud rate */
687 uart_update_timeout(port
, termios
->c_cflag
, baud
);
689 /* Report actual baudrate back to core */
690 if (tty_termios_baud_rate(termios
))
691 tty_termios_encode_baud_rate(termios
, baud
, baud
);
694 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_RX_ENABLE
| CR_TX_ENABLE
);
696 spin_unlock_irqrestore(&s
->lock
, flags
);
699 static int sccnxp_startup(struct uart_port
*port
)
701 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
704 spin_lock_irqsave(&s
->lock
, flags
);
706 if (s
->chip
->flags
& SCCNXP_HAVE_IO
) {
707 /* Outputs are controlled manually */
708 sccnxp_write(port
, SCCNXP_OPCR_REG
, 0);
711 /* Reset break condition, status and FIFOs */
712 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_RX_RESET
);
713 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_TX_RESET
);
714 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_STATUS_RESET
);
715 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_CMD_BREAK_RESET
);
718 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_RX_ENABLE
| CR_TX_ENABLE
);
720 /* Enable RX interrupt */
721 sccnxp_enable_irq(port
, IMR_RXRDY
);
723 s
->opened
[port
->line
] = 1;
725 spin_unlock_irqrestore(&s
->lock
, flags
);
730 static void sccnxp_shutdown(struct uart_port
*port
)
732 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
735 spin_lock_irqsave(&s
->lock
, flags
);
737 s
->opened
[port
->line
] = 0;
739 /* Disable interrupts */
740 sccnxp_disable_irq(port
, IMR_TXRDY
| IMR_RXRDY
);
742 /* Disable TX & RX */
743 sccnxp_port_write(port
, SCCNXP_CR_REG
, CR_RX_DISABLE
| CR_TX_DISABLE
);
745 /* Leave direction to input */
746 if (s
->chip
->flags
& SCCNXP_HAVE_IO
)
747 sccnxp_set_bit(port
, DIR_OP
, 0);
749 spin_unlock_irqrestore(&s
->lock
, flags
);
752 static const char *sccnxp_type(struct uart_port
*port
)
754 struct sccnxp_port
*s
= dev_get_drvdata(port
->dev
);
756 return (port
->type
== PORT_SC26XX
) ? s
->chip
->name
: NULL
;
759 static void sccnxp_release_port(struct uart_port
*port
)
764 static int sccnxp_request_port(struct uart_port
*port
)
770 static void sccnxp_config_port(struct uart_port
*port
, int flags
)
772 if (flags
& UART_CONFIG_TYPE
)
773 port
->type
= PORT_SC26XX
;
776 static int sccnxp_verify_port(struct uart_port
*port
, struct serial_struct
*s
)
778 if ((s
->type
== PORT_UNKNOWN
) || (s
->type
== PORT_SC26XX
))
780 if (s
->irq
== port
->irq
)
786 static const struct uart_ops sccnxp_ops
= {
787 .tx_empty
= sccnxp_tx_empty
,
788 .set_mctrl
= sccnxp_set_mctrl
,
789 .get_mctrl
= sccnxp_get_mctrl
,
790 .stop_tx
= sccnxp_stop_tx
,
791 .start_tx
= sccnxp_start_tx
,
792 .stop_rx
= sccnxp_stop_rx
,
793 .enable_ms
= sccnxp_enable_ms
,
794 .break_ctl
= sccnxp_break_ctl
,
795 .startup
= sccnxp_startup
,
796 .shutdown
= sccnxp_shutdown
,
797 .set_termios
= sccnxp_set_termios
,
799 .release_port
= sccnxp_release_port
,
800 .request_port
= sccnxp_request_port
,
801 .config_port
= sccnxp_config_port
,
802 .verify_port
= sccnxp_verify_port
,
805 #ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
806 static void sccnxp_console_putchar(struct uart_port
*port
, int c
)
811 if (sccnxp_port_read(port
, SCCNXP_SR_REG
) & SR_TXRDY
) {
812 sccnxp_port_write(port
, SCCNXP_THR_REG
, c
);
819 static void sccnxp_console_write(struct console
*co
, const char *c
, unsigned n
)
821 struct sccnxp_port
*s
= (struct sccnxp_port
*)co
->data
;
822 struct uart_port
*port
= &s
->port
[co
->index
];
825 spin_lock_irqsave(&s
->lock
, flags
);
826 uart_console_write(port
, c
, n
, sccnxp_console_putchar
);
827 spin_unlock_irqrestore(&s
->lock
, flags
);
830 static int sccnxp_console_setup(struct console
*co
, char *options
)
832 struct sccnxp_port
*s
= (struct sccnxp_port
*)co
->data
;
833 struct uart_port
*port
= &s
->port
[(co
->index
> 0) ? co
->index
: 0];
834 int baud
= 9600, bits
= 8, parity
= 'n', flow
= 'n';
837 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
839 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
843 static const struct platform_device_id sccnxp_id_table
[] = {
844 { .name
= "sc2681", .driver_data
= (kernel_ulong_t
)&sc2681
, },
845 { .name
= "sc2691", .driver_data
= (kernel_ulong_t
)&sc2691
, },
846 { .name
= "sc2692", .driver_data
= (kernel_ulong_t
)&sc2692
, },
847 { .name
= "sc2891", .driver_data
= (kernel_ulong_t
)&sc2891
, },
848 { .name
= "sc2892", .driver_data
= (kernel_ulong_t
)&sc2892
, },
849 { .name
= "sc28202", .driver_data
= (kernel_ulong_t
)&sc28202
, },
850 { .name
= "sc68681", .driver_data
= (kernel_ulong_t
)&sc68681
, },
851 { .name
= "sc68692", .driver_data
= (kernel_ulong_t
)&sc68692
, },
854 MODULE_DEVICE_TABLE(platform
, sccnxp_id_table
);
856 static int sccnxp_probe(struct platform_device
*pdev
)
858 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
859 struct sccnxp_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
861 struct sccnxp_port
*s
;
862 void __iomem
*membase
;
865 membase
= devm_ioremap_resource(&pdev
->dev
, res
);
867 return PTR_ERR(membase
);
869 s
= devm_kzalloc(&pdev
->dev
, sizeof(struct sccnxp_port
), GFP_KERNEL
);
871 dev_err(&pdev
->dev
, "Error allocating port structure\n");
874 platform_set_drvdata(pdev
, s
);
876 spin_lock_init(&s
->lock
);
878 s
->chip
= (struct sccnxp_chip
*)pdev
->id_entry
->driver_data
;
880 s
->regulator
= devm_regulator_get(&pdev
->dev
, "vcc");
881 if (!IS_ERR(s
->regulator
)) {
882 ret
= regulator_enable(s
->regulator
);
885 "Failed to enable regulator: %i\n", ret
);
888 } else if (PTR_ERR(s
->regulator
) == -EPROBE_DEFER
)
889 return -EPROBE_DEFER
;
891 clk
= devm_clk_get(&pdev
->dev
, NULL
);
893 if (PTR_ERR(clk
) == -EPROBE_DEFER
) {
897 dev_notice(&pdev
->dev
, "Using default clock frequency\n");
898 uartclk
= s
->chip
->freq_std
;
900 uartclk
= clk_get_rate(clk
);
902 /* Check input frequency */
903 if ((uartclk
< s
->chip
->freq_min
) || (uartclk
> s
->chip
->freq_max
)) {
904 dev_err(&pdev
->dev
, "Frequency out of bounds\n");
910 memcpy(&s
->pdata
, pdata
, sizeof(struct sccnxp_pdata
));
912 if (s
->pdata
.poll_time_us
) {
913 dev_info(&pdev
->dev
, "Using poll mode, resolution %u usecs\n",
914 s
->pdata
.poll_time_us
);
919 s
->irq
= platform_get_irq(pdev
, 0);
921 dev_err(&pdev
->dev
, "Missing irq resource data\n");
927 s
->uart
.owner
= THIS_MODULE
;
928 s
->uart
.dev_name
= "ttySC";
929 s
->uart
.major
= SCCNXP_MAJOR
;
930 s
->uart
.minor
= SCCNXP_MINOR
;
931 s
->uart
.nr
= s
->chip
->nr
;
932 #ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
933 s
->uart
.cons
= &s
->console
;
934 s
->uart
.cons
->device
= uart_console_device
;
935 s
->uart
.cons
->write
= sccnxp_console_write
;
936 s
->uart
.cons
->setup
= sccnxp_console_setup
;
937 s
->uart
.cons
->flags
= CON_PRINTBUFFER
;
938 s
->uart
.cons
->index
= -1;
939 s
->uart
.cons
->data
= s
;
940 strcpy(s
->uart
.cons
->name
, "ttySC");
942 ret
= uart_register_driver(&s
->uart
);
944 dev_err(&pdev
->dev
, "Registering UART driver failed\n");
948 for (i
= 0; i
< s
->uart
.nr
; i
++) {
950 s
->port
[i
].dev
= &pdev
->dev
;
951 s
->port
[i
].irq
= s
->irq
;
952 s
->port
[i
].type
= PORT_SC26XX
;
953 s
->port
[i
].fifosize
= s
->chip
->fifosize
;
954 s
->port
[i
].flags
= UPF_SKIP_TEST
| UPF_FIXED_TYPE
;
955 s
->port
[i
].iotype
= UPIO_MEM
;
956 s
->port
[i
].mapbase
= res
->start
;
957 s
->port
[i
].membase
= membase
;
958 s
->port
[i
].regshift
= s
->pdata
.reg_shift
;
959 s
->port
[i
].uartclk
= uartclk
;
960 s
->port
[i
].ops
= &sccnxp_ops
;
961 uart_add_one_port(&s
->uart
, &s
->port
[i
]);
962 /* Set direction to input */
963 if (s
->chip
->flags
& SCCNXP_HAVE_IO
)
964 sccnxp_set_bit(&s
->port
[i
], DIR_OP
, 0);
967 /* Disable interrupts */
969 sccnxp_write(&s
->port
[0], SCCNXP_IMR_REG
, 0);
972 ret
= devm_request_threaded_irq(&pdev
->dev
, s
->irq
, NULL
,
974 IRQF_TRIGGER_FALLING
|
976 dev_name(&pdev
->dev
), s
);
980 dev_err(&pdev
->dev
, "Unable to reguest IRQ %i\n", s
->irq
);
982 init_timer(&s
->timer
);
983 setup_timer(&s
->timer
, sccnxp_timer
, (unsigned long)s
);
984 mod_timer(&s
->timer
, jiffies
+
985 usecs_to_jiffies(s
->pdata
.poll_time_us
));
990 if (!IS_ERR(s
->regulator
))
991 return regulator_disable(s
->regulator
);
996 static int sccnxp_remove(struct platform_device
*pdev
)
999 struct sccnxp_port
*s
= platform_get_drvdata(pdev
);
1002 devm_free_irq(&pdev
->dev
, s
->irq
, s
);
1004 del_timer_sync(&s
->timer
);
1006 for (i
= 0; i
< s
->uart
.nr
; i
++)
1007 uart_remove_one_port(&s
->uart
, &s
->port
[i
]);
1009 uart_unregister_driver(&s
->uart
);
1011 if (!IS_ERR(s
->regulator
))
1012 return regulator_disable(s
->regulator
);
1017 static struct platform_driver sccnxp_uart_driver
= {
1019 .name
= SCCNXP_NAME
,
1020 .owner
= THIS_MODULE
,
1022 .probe
= sccnxp_probe
,
1023 .remove
= sccnxp_remove
,
1024 .id_table
= sccnxp_id_table
,
1026 module_platform_driver(sccnxp_uart_driver
);
1028 MODULE_LICENSE("GPL v2");
1029 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
1030 MODULE_DESCRIPTION("SCCNXP serial driver");