1 // SPDX-License-Identifier: GPL-2.0+
3 * Support for the asynchronous serial interface (DUART) included
4 * in the BCM1250 and derived System-On-a-Chip (SOC) devices.
6 * Copyright (c) 2007 Maciej W. Rozycki
8 * Derived from drivers/char/sb1250_duart.c for which the following
11 * Copyright (c) 2000, 2001, 2002, 2003, 2004 Broadcom Corporation
15 * "BCM1250/BCM1125/BCM1125H User Manual", Broadcom Corporation
18 #include <linux/compiler.h>
19 #include <linux/console.h>
20 #include <linux/delay.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/ioport.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/major.h>
28 #include <linux/serial.h>
29 #include <linux/serial_core.h>
30 #include <linux/spinlock.h>
31 #include <linux/sysrq.h>
32 #include <linux/tty.h>
33 #include <linux/tty_flip.h>
34 #include <linux/types.h>
36 #include <linux/refcount.h>
39 #include <asm/sibyte/sb1250.h>
40 #include <asm/sibyte/sb1250_uart.h>
41 #include <asm/sibyte/swarm.h>
44 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
45 #include <asm/sibyte/bcm1480_regs.h>
46 #include <asm/sibyte/bcm1480_int.h>
48 #define SBD_CHANREGS(line) A_BCM1480_DUART_CHANREG((line), 0)
49 #define SBD_CTRLREGS(line) A_BCM1480_DUART_CTRLREG((line), 0)
50 #define SBD_INT(line) (K_BCM1480_INT_UART_0 + (line))
52 #define DUART_CHANREG_SPACING BCM1480_DUART_CHANREG_SPACING
54 #define R_DUART_IMRREG(line) R_BCM1480_DUART_IMRREG(line)
55 #define R_DUART_INCHREG(line) R_BCM1480_DUART_INCHREG(line)
56 #define R_DUART_ISRREG(line) R_BCM1480_DUART_ISRREG(line)
58 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
59 #include <asm/sibyte/sb1250_regs.h>
60 #include <asm/sibyte/sb1250_int.h>
62 #define SBD_CHANREGS(line) A_DUART_CHANREG((line), 0)
63 #define SBD_CTRLREGS(line) A_DUART_CTRLREG(0)
64 #define SBD_INT(line) (K_INT_UART_0 + (line))
67 #error invalid SB1250 UART configuration
72 MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
73 MODULE_DESCRIPTION("BCM1xxx on-chip DUART serial driver");
74 MODULE_LICENSE("GPL");
77 #define DUART_MAX_CHIP 2
78 #define DUART_MAX_SIDE 2
84 struct sbd_duart
*duart
;
85 struct uart_port port
;
86 unsigned char __iomem
*memctrl
;
92 * Per-DUART state for the shared register space.
95 struct sbd_port sport
[2];
96 unsigned long mapctrl
;
100 #define to_sport(uport) container_of(uport, struct sbd_port, port)
102 static struct sbd_duart sbd_duarts
[DUART_MAX_CHIP
];
106 * Reading and writing SB1250 DUART registers.
108 * There are three register spaces: two per-channel ones and
109 * a shared one. We have to define accessors appropriately.
110 * All registers are 64-bit and all but the Baud Rate Clock
111 * registers only define 8 least significant bits. There is
112 * also a workaround to take into account. Raw accessors use
113 * the full register width, but cooked ones truncate it
114 * intentionally so that the rest of the driver does not care.
116 static u64
__read_sbdchn(struct sbd_port
*sport
, int reg
)
118 void __iomem
*csr
= sport
->port
.membase
+ reg
;
120 return __raw_readq(csr
);
123 static u64
__read_sbdshr(struct sbd_port
*sport
, int reg
)
125 void __iomem
*csr
= sport
->memctrl
+ reg
;
127 return __raw_readq(csr
);
130 static void __write_sbdchn(struct sbd_port
*sport
, int reg
, u64 value
)
132 void __iomem
*csr
= sport
->port
.membase
+ reg
;
134 __raw_writeq(value
, csr
);
137 static void __write_sbdshr(struct sbd_port
*sport
, int reg
, u64 value
)
139 void __iomem
*csr
= sport
->memctrl
+ reg
;
141 __raw_writeq(value
, csr
);
145 * In bug 1956, we get glitches that can mess up uart registers. This
146 * "read-mode-reg after any register access" is an accepted workaround.
148 static void __war_sbd1956(struct sbd_port
*sport
)
150 __read_sbdchn(sport
, R_DUART_MODE_REG_1
);
151 __read_sbdchn(sport
, R_DUART_MODE_REG_2
);
154 static unsigned char read_sbdchn(struct sbd_port
*sport
, int reg
)
156 unsigned char retval
;
158 retval
= __read_sbdchn(sport
, reg
);
159 if (IS_ENABLED(CONFIG_SB1_PASS_2_WORKAROUNDS
))
160 __war_sbd1956(sport
);
164 static unsigned char read_sbdshr(struct sbd_port
*sport
, int reg
)
166 unsigned char retval
;
168 retval
= __read_sbdshr(sport
, reg
);
169 if (IS_ENABLED(CONFIG_SB1_PASS_2_WORKAROUNDS
))
170 __war_sbd1956(sport
);
174 static void write_sbdchn(struct sbd_port
*sport
, int reg
, unsigned int value
)
176 __write_sbdchn(sport
, reg
, value
);
177 if (IS_ENABLED(CONFIG_SB1_PASS_2_WORKAROUNDS
))
178 __war_sbd1956(sport
);
181 static void write_sbdshr(struct sbd_port
*sport
, int reg
, unsigned int value
)
183 __write_sbdshr(sport
, reg
, value
);
184 if (IS_ENABLED(CONFIG_SB1_PASS_2_WORKAROUNDS
))
185 __war_sbd1956(sport
);
189 static int sbd_receive_ready(struct sbd_port
*sport
)
191 return read_sbdchn(sport
, R_DUART_STATUS
) & M_DUART_RX_RDY
;
194 static int sbd_receive_drain(struct sbd_port
*sport
)
198 while (sbd_receive_ready(sport
) && --loops
)
199 read_sbdchn(sport
, R_DUART_RX_HOLD
);
203 static int __maybe_unused
sbd_transmit_ready(struct sbd_port
*sport
)
205 return read_sbdchn(sport
, R_DUART_STATUS
) & M_DUART_TX_RDY
;
208 static int __maybe_unused
sbd_transmit_drain(struct sbd_port
*sport
)
212 while (!sbd_transmit_ready(sport
) && --loops
)
217 static int sbd_transmit_empty(struct sbd_port
*sport
)
219 return read_sbdchn(sport
, R_DUART_STATUS
) & M_DUART_TX_EMT
;
222 static int sbd_line_drain(struct sbd_port
*sport
)
226 while (!sbd_transmit_empty(sport
) && --loops
)
232 static unsigned int sbd_tx_empty(struct uart_port
*uport
)
234 struct sbd_port
*sport
= to_sport(uport
);
236 return sbd_transmit_empty(sport
) ? TIOCSER_TEMT
: 0;
239 static unsigned int sbd_get_mctrl(struct uart_port
*uport
)
241 struct sbd_port
*sport
= to_sport(uport
);
242 unsigned int mctrl
, status
;
244 status
= read_sbdshr(sport
, R_DUART_IN_PORT
);
245 status
>>= (uport
->line
) % 2;
246 mctrl
= (!(status
& M_DUART_IN_PIN0_VAL
) ? TIOCM_CTS
: 0) |
247 (!(status
& M_DUART_IN_PIN4_VAL
) ? TIOCM_CAR
: 0) |
248 (!(status
& M_DUART_RIN0_PIN
) ? TIOCM_RNG
: 0) |
249 (!(status
& M_DUART_IN_PIN2_VAL
) ? TIOCM_DSR
: 0);
253 static void sbd_set_mctrl(struct uart_port
*uport
, unsigned int mctrl
)
255 struct sbd_port
*sport
= to_sport(uport
);
256 unsigned int clr
= 0, set
= 0, mode2
;
258 if (mctrl
& TIOCM_DTR
)
259 set
|= M_DUART_SET_OPR2
;
261 clr
|= M_DUART_CLR_OPR2
;
262 if (mctrl
& TIOCM_RTS
)
263 set
|= M_DUART_SET_OPR0
;
265 clr
|= M_DUART_CLR_OPR0
;
266 clr
<<= (uport
->line
) % 2;
267 set
<<= (uport
->line
) % 2;
269 mode2
= read_sbdchn(sport
, R_DUART_MODE_REG_2
);
270 mode2
&= ~M_DUART_CHAN_MODE
;
271 if (mctrl
& TIOCM_LOOP
)
272 mode2
|= V_DUART_CHAN_MODE_LCL_LOOP
;
274 mode2
|= V_DUART_CHAN_MODE_NORMAL
;
276 write_sbdshr(sport
, R_DUART_CLEAR_OPR
, clr
);
277 write_sbdshr(sport
, R_DUART_SET_OPR
, set
);
278 write_sbdchn(sport
, R_DUART_MODE_REG_2
, mode2
);
281 static void sbd_stop_tx(struct uart_port
*uport
)
283 struct sbd_port
*sport
= to_sport(uport
);
285 write_sbdchn(sport
, R_DUART_CMD
, M_DUART_TX_DIS
);
286 sport
->tx_stopped
= 1;
289 static void sbd_start_tx(struct uart_port
*uport
)
291 struct sbd_port
*sport
= to_sport(uport
);
294 /* Enable tx interrupts. */
295 mask
= read_sbdshr(sport
, R_DUART_IMRREG((uport
->line
) % 2));
296 mask
|= M_DUART_IMR_TX
;
297 write_sbdshr(sport
, R_DUART_IMRREG((uport
->line
) % 2), mask
);
299 /* Go!, go!, go!... */
300 write_sbdchn(sport
, R_DUART_CMD
, M_DUART_TX_EN
);
301 sport
->tx_stopped
= 0;
304 static void sbd_stop_rx(struct uart_port
*uport
)
306 struct sbd_port
*sport
= to_sport(uport
);
308 write_sbdshr(sport
, R_DUART_IMRREG((uport
->line
) % 2), 0);
311 static void sbd_enable_ms(struct uart_port
*uport
)
313 struct sbd_port
*sport
= to_sport(uport
);
315 write_sbdchn(sport
, R_DUART_AUXCTL_X
,
316 M_DUART_CIN_CHNG_ENA
| M_DUART_CTS_CHNG_ENA
);
319 static void sbd_break_ctl(struct uart_port
*uport
, int break_state
)
321 struct sbd_port
*sport
= to_sport(uport
);
323 if (break_state
== -1)
324 write_sbdchn(sport
, R_DUART_CMD
, V_DUART_MISC_CMD_START_BREAK
);
326 write_sbdchn(sport
, R_DUART_CMD
, V_DUART_MISC_CMD_STOP_BREAK
);
330 static void sbd_receive_chars(struct sbd_port
*sport
)
332 struct uart_port
*uport
= &sport
->port
;
333 struct uart_icount
*icount
;
334 unsigned int status
, ch
, flag
;
337 for (count
= 16; count
; count
--) {
338 status
= read_sbdchn(sport
, R_DUART_STATUS
);
339 if (!(status
& M_DUART_RX_RDY
))
342 ch
= read_sbdchn(sport
, R_DUART_RX_HOLD
);
346 icount
= &uport
->icount
;
349 if (unlikely(status
&
350 (M_DUART_RCVD_BRK
| M_DUART_FRM_ERR
|
351 M_DUART_PARITY_ERR
| M_DUART_OVRUN_ERR
))) {
352 if (status
& M_DUART_RCVD_BRK
) {
354 if (uart_handle_break(uport
))
356 } else if (status
& M_DUART_FRM_ERR
)
358 else if (status
& M_DUART_PARITY_ERR
)
360 if (status
& M_DUART_OVRUN_ERR
)
363 status
&= uport
->read_status_mask
;
364 if (status
& M_DUART_RCVD_BRK
)
366 else if (status
& M_DUART_FRM_ERR
)
368 else if (status
& M_DUART_PARITY_ERR
)
372 if (uart_handle_sysrq_char(uport
, ch
))
375 uart_insert_char(uport
, status
, M_DUART_OVRUN_ERR
, ch
, flag
);
378 tty_flip_buffer_push(&uport
->state
->port
);
381 static void sbd_transmit_chars(struct sbd_port
*sport
)
383 struct uart_port
*uport
= &sport
->port
;
384 struct circ_buf
*xmit
= &sport
->port
.state
->xmit
;
388 /* XON/XOFF chars. */
389 if (sport
->port
.x_char
) {
390 write_sbdchn(sport
, R_DUART_TX_HOLD
, sport
->port
.x_char
);
391 sport
->port
.icount
.tx
++;
392 sport
->port
.x_char
= 0;
396 /* If nothing to do or stopped or hardware stopped. */
397 stop_tx
= (uart_circ_empty(xmit
) || uart_tx_stopped(&sport
->port
));
401 write_sbdchn(sport
, R_DUART_TX_HOLD
, xmit
->buf
[xmit
->tail
]);
402 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
403 sport
->port
.icount
.tx
++;
405 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
406 uart_write_wakeup(&sport
->port
);
409 /* Are we are done? */
410 if (stop_tx
|| uart_circ_empty(xmit
)) {
411 /* Disable tx interrupts. */
412 mask
= read_sbdshr(sport
, R_DUART_IMRREG((uport
->line
) % 2));
413 mask
&= ~M_DUART_IMR_TX
;
414 write_sbdshr(sport
, R_DUART_IMRREG((uport
->line
) % 2), mask
);
418 static void sbd_status_handle(struct sbd_port
*sport
)
420 struct uart_port
*uport
= &sport
->port
;
423 delta
= read_sbdshr(sport
, R_DUART_INCHREG((uport
->line
) % 2));
424 delta
>>= (uport
->line
) % 2;
426 if (delta
& (M_DUART_IN_PIN0_VAL
<< S_DUART_IN_PIN_CHNG
))
427 uart_handle_cts_change(uport
, !(delta
& M_DUART_IN_PIN0_VAL
));
429 if (delta
& (M_DUART_IN_PIN2_VAL
<< S_DUART_IN_PIN_CHNG
))
432 if (delta
& ((M_DUART_IN_PIN2_VAL
| M_DUART_IN_PIN0_VAL
) <<
433 S_DUART_IN_PIN_CHNG
))
434 wake_up_interruptible(&uport
->state
->port
.delta_msr_wait
);
437 static irqreturn_t
sbd_interrupt(int irq
, void *dev_id
)
439 struct sbd_port
*sport
= dev_id
;
440 struct uart_port
*uport
= &sport
->port
;
441 irqreturn_t status
= IRQ_NONE
;
442 unsigned int intstat
;
445 for (count
= 16; count
; count
--) {
446 intstat
= read_sbdshr(sport
,
447 R_DUART_ISRREG((uport
->line
) % 2));
448 intstat
&= read_sbdshr(sport
,
449 R_DUART_IMRREG((uport
->line
) % 2));
450 intstat
&= M_DUART_ISR_ALL
;
454 if (intstat
& M_DUART_ISR_RX
)
455 sbd_receive_chars(sport
);
456 if (intstat
& M_DUART_ISR_IN
)
457 sbd_status_handle(sport
);
458 if (intstat
& M_DUART_ISR_TX
)
459 sbd_transmit_chars(sport
);
461 status
= IRQ_HANDLED
;
468 static int sbd_startup(struct uart_port
*uport
)
470 struct sbd_port
*sport
= to_sport(uport
);
474 ret
= request_irq(sport
->port
.irq
, sbd_interrupt
,
475 IRQF_SHARED
, "sb1250-duart", sport
);
479 /* Clear the receive FIFO. */
480 sbd_receive_drain(sport
);
482 /* Clear the interrupt registers. */
483 write_sbdchn(sport
, R_DUART_CMD
, V_DUART_MISC_CMD_RESET_BREAK_INT
);
484 read_sbdshr(sport
, R_DUART_INCHREG((uport
->line
) % 2));
486 /* Set rx/tx interrupt to FIFO available. */
487 mode1
= read_sbdchn(sport
, R_DUART_MODE_REG_1
);
488 mode1
&= ~(M_DUART_RX_IRQ_SEL_RXFULL
| M_DUART_TX_IRQ_SEL_TXEMPT
);
489 write_sbdchn(sport
, R_DUART_MODE_REG_1
, mode1
);
491 /* Disable tx, enable rx. */
492 write_sbdchn(sport
, R_DUART_CMD
, M_DUART_TX_DIS
| M_DUART_RX_EN
);
493 sport
->tx_stopped
= 1;
495 /* Enable interrupts. */
496 write_sbdshr(sport
, R_DUART_IMRREG((uport
->line
) % 2),
497 M_DUART_IMR_IN
| M_DUART_IMR_RX
);
502 static void sbd_shutdown(struct uart_port
*uport
)
504 struct sbd_port
*sport
= to_sport(uport
);
506 write_sbdchn(sport
, R_DUART_CMD
, M_DUART_TX_DIS
| M_DUART_RX_DIS
);
507 sport
->tx_stopped
= 1;
508 free_irq(sport
->port
.irq
, sport
);
512 static void sbd_init_port(struct sbd_port
*sport
)
514 struct uart_port
*uport
= &sport
->port
;
516 if (sport
->initialised
)
519 /* There is no DUART reset feature, so just set some sane defaults. */
520 write_sbdchn(sport
, R_DUART_CMD
, V_DUART_MISC_CMD_RESET_TX
);
521 write_sbdchn(sport
, R_DUART_CMD
, V_DUART_MISC_CMD_RESET_RX
);
522 write_sbdchn(sport
, R_DUART_MODE_REG_1
, V_DUART_BITS_PER_CHAR_8
);
523 write_sbdchn(sport
, R_DUART_MODE_REG_2
, 0);
524 write_sbdchn(sport
, R_DUART_FULL_CTL
,
525 V_DUART_INT_TIME(0) | V_DUART_SIG_FULL(15));
526 write_sbdchn(sport
, R_DUART_OPCR_X
, 0);
527 write_sbdchn(sport
, R_DUART_AUXCTL_X
, 0);
528 write_sbdshr(sport
, R_DUART_IMRREG((uport
->line
) % 2), 0);
530 sport
->initialised
= 1;
533 static void sbd_set_termios(struct uart_port
*uport
, struct ktermios
*termios
,
534 struct ktermios
*old_termios
)
536 struct sbd_port
*sport
= to_sport(uport
);
537 unsigned int mode1
= 0, mode2
= 0, aux
= 0;
538 unsigned int mode1mask
= 0, mode2mask
= 0, auxmask
= 0;
539 unsigned int oldmode1
, oldmode2
, oldaux
;
540 unsigned int baud
, brg
;
541 unsigned int command
;
543 mode1mask
|= ~(M_DUART_PARITY_MODE
| M_DUART_PARITY_TYPE_ODD
|
544 M_DUART_BITS_PER_CHAR
);
545 mode2mask
|= ~M_DUART_STOP_BIT_LEN_2
;
546 auxmask
|= ~M_DUART_CTS_CHNG_ENA
;
549 switch (termios
->c_cflag
& CSIZE
) {
552 /* Unsupported, leave unchanged. */
553 mode1mask
|= M_DUART_PARITY_MODE
;
556 mode1
|= V_DUART_BITS_PER_CHAR_7
;
560 mode1
|= V_DUART_BITS_PER_CHAR_8
;
564 /* Parity and stop bits. */
565 if (termios
->c_cflag
& CSTOPB
)
566 mode2
|= M_DUART_STOP_BIT_LEN_2
;
568 mode2
|= M_DUART_STOP_BIT_LEN_1
;
569 if (termios
->c_cflag
& PARENB
)
570 mode1
|= V_DUART_PARITY_MODE_ADD
;
572 mode1
|= V_DUART_PARITY_MODE_NONE
;
573 if (termios
->c_cflag
& PARODD
)
574 mode1
|= M_DUART_PARITY_TYPE_ODD
;
576 mode1
|= M_DUART_PARITY_TYPE_EVEN
;
578 baud
= uart_get_baud_rate(uport
, termios
, old_termios
, 1200, 5000000);
579 brg
= V_DUART_BAUD_RATE(baud
);
580 /* The actual lower bound is 1221bps, so compensate. */
581 if (brg
> M_DUART_CLK_COUNTER
)
582 brg
= M_DUART_CLK_COUNTER
;
584 uart_update_timeout(uport
, termios
->c_cflag
, baud
);
586 uport
->read_status_mask
= M_DUART_OVRUN_ERR
;
587 if (termios
->c_iflag
& INPCK
)
588 uport
->read_status_mask
|= M_DUART_FRM_ERR
|
590 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
591 uport
->read_status_mask
|= M_DUART_RCVD_BRK
;
593 uport
->ignore_status_mask
= 0;
594 if (termios
->c_iflag
& IGNPAR
)
595 uport
->ignore_status_mask
|= M_DUART_FRM_ERR
|
597 if (termios
->c_iflag
& IGNBRK
) {
598 uport
->ignore_status_mask
|= M_DUART_RCVD_BRK
;
599 if (termios
->c_iflag
& IGNPAR
)
600 uport
->ignore_status_mask
|= M_DUART_OVRUN_ERR
;
603 if (termios
->c_cflag
& CREAD
)
604 command
= M_DUART_RX_EN
;
606 command
= M_DUART_RX_DIS
;
608 if (termios
->c_cflag
& CRTSCTS
)
609 aux
|= M_DUART_CTS_CHNG_ENA
;
611 aux
&= ~M_DUART_CTS_CHNG_ENA
;
613 spin_lock(&uport
->lock
);
615 if (sport
->tx_stopped
)
616 command
|= M_DUART_TX_DIS
;
618 command
|= M_DUART_TX_EN
;
620 oldmode1
= read_sbdchn(sport
, R_DUART_MODE_REG_1
) & mode1mask
;
621 oldmode2
= read_sbdchn(sport
, R_DUART_MODE_REG_2
) & mode2mask
;
622 oldaux
= read_sbdchn(sport
, R_DUART_AUXCTL_X
) & auxmask
;
624 if (!sport
->tx_stopped
)
625 sbd_line_drain(sport
);
626 write_sbdchn(sport
, R_DUART_CMD
, M_DUART_TX_DIS
| M_DUART_RX_DIS
);
628 write_sbdchn(sport
, R_DUART_MODE_REG_1
, mode1
| oldmode1
);
629 write_sbdchn(sport
, R_DUART_MODE_REG_2
, mode2
| oldmode2
);
630 write_sbdchn(sport
, R_DUART_CLK_SEL
, brg
);
631 write_sbdchn(sport
, R_DUART_AUXCTL_X
, aux
| oldaux
);
633 write_sbdchn(sport
, R_DUART_CMD
, command
);
635 spin_unlock(&uport
->lock
);
639 static const char *sbd_type(struct uart_port
*uport
)
641 return "SB1250 DUART";
644 static void sbd_release_port(struct uart_port
*uport
)
646 struct sbd_port
*sport
= to_sport(uport
);
647 struct sbd_duart
*duart
= sport
->duart
;
649 iounmap(sport
->memctrl
);
650 sport
->memctrl
= NULL
;
651 iounmap(uport
->membase
);
652 uport
->membase
= NULL
;
654 if(refcount_dec_and_test(&duart
->map_guard
))
655 release_mem_region(duart
->mapctrl
, DUART_CHANREG_SPACING
);
656 release_mem_region(uport
->mapbase
, DUART_CHANREG_SPACING
);
659 static int sbd_map_port(struct uart_port
*uport
)
661 const char *err
= KERN_ERR
"sbd: Cannot map MMIO\n";
662 struct sbd_port
*sport
= to_sport(uport
);
663 struct sbd_duart
*duart
= sport
->duart
;
666 uport
->membase
= ioremap(uport
->mapbase
,
667 DUART_CHANREG_SPACING
);
668 if (!uport
->membase
) {
674 sport
->memctrl
= ioremap(duart
->mapctrl
,
675 DUART_CHANREG_SPACING
);
676 if (!sport
->memctrl
) {
678 iounmap(uport
->membase
);
679 uport
->membase
= NULL
;
686 static int sbd_request_port(struct uart_port
*uport
)
688 const char *err
= KERN_ERR
"sbd: Unable to reserve MMIO resource\n";
689 struct sbd_duart
*duart
= to_sport(uport
)->duart
;
692 if (!request_mem_region(uport
->mapbase
, DUART_CHANREG_SPACING
,
697 refcount_inc(&duart
->map_guard
);
698 if (refcount_read(&duart
->map_guard
) == 1) {
699 if (!request_mem_region(duart
->mapctrl
, DUART_CHANREG_SPACING
,
701 refcount_dec(&duart
->map_guard
);
707 ret
= sbd_map_port(uport
);
709 if (refcount_dec_and_test(&duart
->map_guard
))
710 release_mem_region(duart
->mapctrl
,
711 DUART_CHANREG_SPACING
);
715 release_mem_region(uport
->mapbase
, DUART_CHANREG_SPACING
);
721 static void sbd_config_port(struct uart_port
*uport
, int flags
)
723 struct sbd_port
*sport
= to_sport(uport
);
725 if (flags
& UART_CONFIG_TYPE
) {
726 if (sbd_request_port(uport
))
729 uport
->type
= PORT_SB1250_DUART
;
731 sbd_init_port(sport
);
735 static int sbd_verify_port(struct uart_port
*uport
, struct serial_struct
*ser
)
739 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_SB1250_DUART
)
741 if (ser
->irq
!= uport
->irq
)
743 if (ser
->baud_base
!= uport
->uartclk
/ 16)
749 static const struct uart_ops sbd_ops
= {
750 .tx_empty
= sbd_tx_empty
,
751 .set_mctrl
= sbd_set_mctrl
,
752 .get_mctrl
= sbd_get_mctrl
,
753 .stop_tx
= sbd_stop_tx
,
754 .start_tx
= sbd_start_tx
,
755 .stop_rx
= sbd_stop_rx
,
756 .enable_ms
= sbd_enable_ms
,
757 .break_ctl
= sbd_break_ctl
,
758 .startup
= sbd_startup
,
759 .shutdown
= sbd_shutdown
,
760 .set_termios
= sbd_set_termios
,
762 .release_port
= sbd_release_port
,
763 .request_port
= sbd_request_port
,
764 .config_port
= sbd_config_port
,
765 .verify_port
= sbd_verify_port
,
768 /* Initialize SB1250 DUART port structures. */
769 static void __init
sbd_probe_duarts(void)
778 /* Set the number of available units based on the SOC type. */
780 case K_SYS_SOC_TYPE_BCM1x55
:
781 case K_SYS_SOC_TYPE_BCM1x80
:
785 /* Assume at least two serial ports at the normal address. */
792 for (chip
= 0, line
= 0; chip
< DUART_MAX_CHIP
&& line
< max_lines
;
794 sbd_duarts
[chip
].mapctrl
= SBD_CTRLREGS(line
);
796 for (side
= 0; side
< DUART_MAX_SIDE
&& line
< max_lines
;
798 struct sbd_port
*sport
= &sbd_duarts
[chip
].sport
[side
];
799 struct uart_port
*uport
= &sport
->port
;
801 sport
->duart
= &sbd_duarts
[chip
];
803 uport
->irq
= SBD_INT(line
);
804 uport
->uartclk
= 100000000 / 20 * 16;
805 uport
->fifosize
= 16;
806 uport
->iotype
= UPIO_MEM
;
807 uport
->flags
= UPF_BOOT_AUTOCONF
;
808 uport
->ops
= &sbd_ops
;
810 uport
->mapbase
= SBD_CHANREGS(line
);
811 uport
->has_sysrq
= IS_ENABLED(CONFIG_SERIAL_SB1250_DUART_CONSOLE
);
817 #ifdef CONFIG_SERIAL_SB1250_DUART_CONSOLE
819 * Serial console stuff. Very basic, polling driver for doing serial
820 * console output. The console_lock is held by the caller, so we
821 * shouldn't be interrupted for more console activity.
823 static void sbd_console_putchar(struct uart_port
*uport
, int ch
)
825 struct sbd_port
*sport
= to_sport(uport
);
827 sbd_transmit_drain(sport
);
828 write_sbdchn(sport
, R_DUART_TX_HOLD
, ch
);
831 static void sbd_console_write(struct console
*co
, const char *s
,
834 int chip
= co
->index
/ DUART_MAX_SIDE
;
835 int side
= co
->index
% DUART_MAX_SIDE
;
836 struct sbd_port
*sport
= &sbd_duarts
[chip
].sport
[side
];
837 struct uart_port
*uport
= &sport
->port
;
841 /* Disable transmit interrupts and enable the transmitter. */
842 spin_lock_irqsave(&uport
->lock
, flags
);
843 mask
= read_sbdshr(sport
, R_DUART_IMRREG((uport
->line
) % 2));
844 write_sbdshr(sport
, R_DUART_IMRREG((uport
->line
) % 2),
845 mask
& ~M_DUART_IMR_TX
);
846 write_sbdchn(sport
, R_DUART_CMD
, M_DUART_TX_EN
);
847 spin_unlock_irqrestore(&uport
->lock
, flags
);
849 uart_console_write(&sport
->port
, s
, count
, sbd_console_putchar
);
851 /* Restore transmit interrupts and the transmitter enable. */
852 spin_lock_irqsave(&uport
->lock
, flags
);
853 sbd_line_drain(sport
);
854 if (sport
->tx_stopped
)
855 write_sbdchn(sport
, R_DUART_CMD
, M_DUART_TX_DIS
);
856 write_sbdshr(sport
, R_DUART_IMRREG((uport
->line
) % 2), mask
);
857 spin_unlock_irqrestore(&uport
->lock
, flags
);
860 static int __init
sbd_console_setup(struct console
*co
, char *options
)
862 int chip
= co
->index
/ DUART_MAX_SIDE
;
863 int side
= co
->index
% DUART_MAX_SIDE
;
864 struct sbd_port
*sport
= &sbd_duarts
[chip
].sport
[side
];
865 struct uart_port
*uport
= &sport
->port
;
875 ret
= sbd_map_port(uport
);
879 sbd_init_port(sport
);
882 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
883 return uart_set_options(uport
, co
, baud
, parity
, bits
, flow
);
886 static struct uart_driver sbd_reg
;
887 static struct console sbd_console
= {
889 .write
= sbd_console_write
,
890 .device
= uart_console_device
,
891 .setup
= sbd_console_setup
,
892 .flags
= CON_PRINTBUFFER
,
897 static int __init
sbd_serial_console_init(void)
900 register_console(&sbd_console
);
905 console_initcall(sbd_serial_console_init
);
907 #define SERIAL_SB1250_DUART_CONSOLE &sbd_console
909 #define SERIAL_SB1250_DUART_CONSOLE NULL
910 #endif /* CONFIG_SERIAL_SB1250_DUART_CONSOLE */
913 static struct uart_driver sbd_reg
= {
914 .owner
= THIS_MODULE
,
915 .driver_name
= "sb1250_duart",
918 .minor
= SB1250_DUART_MINOR_BASE
,
919 .nr
= DUART_MAX_CHIP
* DUART_MAX_SIDE
,
920 .cons
= SERIAL_SB1250_DUART_CONSOLE
,
923 /* Set up the driver and register it. */
924 static int __init
sbd_init(void)
930 ret
= uart_register_driver(&sbd_reg
);
934 for (i
= 0; i
< DUART_MAX_CHIP
* DUART_MAX_SIDE
; i
++) {
935 struct sbd_duart
*duart
= &sbd_duarts
[i
/ DUART_MAX_SIDE
];
936 struct sbd_port
*sport
= &duart
->sport
[i
% DUART_MAX_SIDE
];
937 struct uart_port
*uport
= &sport
->port
;
940 uart_add_one_port(&sbd_reg
, uport
);
946 /* Unload the driver. Unregister stuff, get ready to go away. */
947 static void __exit
sbd_exit(void)
951 for (i
= DUART_MAX_CHIP
* DUART_MAX_SIDE
- 1; i
>= 0; i
--) {
952 struct sbd_duart
*duart
= &sbd_duarts
[i
/ DUART_MAX_SIDE
];
953 struct sbd_port
*sport
= &duart
->sport
[i
% DUART_MAX_SIDE
];
954 struct uart_port
*uport
= &sport
->port
;
957 uart_remove_one_port(&sbd_reg
, uport
);
960 uart_unregister_driver(&sbd_reg
);
963 module_init(sbd_init
);
964 module_exit(sbd_exit
);