4 * Qi Hardware, Xiangfu Liu <xiangfu@sharism.cc>
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/jz4740.h>
14 #include <linux/compiler.h>
17 * serial_init - initialize a channel
19 * This routine initializes the number of data bits, parity
20 * and set the selected baud rate. Interrupts are disabled.
21 * Set the modem control signals if the option is selected.
25 struct jz4740_uart
*uart
= (struct jz4740_uart
*)CONFIG_SYS_UART_BASE
;
27 static int jz_serial_init(void)
29 /* Disable port interrupts while changing hardware */
30 writeb(0, &uart
->dlhr_ier
);
32 /* Disable UART unit function */
33 writeb(~UART_FCR_UUE
, &uart
->iir_fcr
);
35 /* Set both receiver and transmitter in UART mode (not SIR) */
36 writeb(~(SIRCR_RSIRE
| SIRCR_TSIRE
), &uart
->isr
);
39 * Set databits, stopbits and parity.
40 * (8-bit data, 1 stopbit, no parity)
42 writeb(UART_LCR_WLEN_8
| UART_LCR_STOP_1
, &uart
->lcr
);
47 /* Enable UART unit, enable and clear FIFO */
48 writeb(UART_FCR_UUE
| UART_FCR_FE
| UART_FCR_TFLS
| UART_FCR_RFLS
,
54 static void jz_serial_setbrg(void)
58 baud_div
= CONFIG_SYS_EXTAL
/ 16 / CONFIG_BAUDRATE
;
60 tmp
= readb(&uart
->lcr
);
62 writeb(tmp
, &uart
->lcr
);
64 writeb((baud_div
>> 8) & 0xff, &uart
->dlhr_ier
);
65 writeb(baud_div
& 0xff, &uart
->rbr_thr_dllr
);
67 tmp
&= ~UART_LCR_DLAB
;
68 writeb(tmp
, &uart
->lcr
);
71 static int jz_serial_tstc(void)
73 if (readb(&uart
->lsr
) & UART_LSR_DR
)
79 static void jz_serial_putc(const char c
)
84 /* Wait for fifo to shift out some bytes */
85 while (!((readb(&uart
->lsr
) & (UART_LSR_TDRQ
| UART_LSR_TEMT
)) == 0x60))
88 writeb((u8
)c
, &uart
->rbr_thr_dllr
);
91 static int jz_serial_getc(void)
93 while (!serial_tstc())
96 return readb(&uart
->rbr_thr_dllr
);
99 static struct serial_device jz_serial_drv
= {
101 .start
= jz_serial_init
,
103 .setbrg
= jz_serial_setbrg
,
104 .putc
= jz_serial_putc
,
105 .puts
= default_serial_puts
,
106 .getc
= jz_serial_getc
,
107 .tstc
= jz_serial_tstc
,
110 void jz_serial_initialize(void)
112 serial_register(&jz_serial_drv
);
115 __weak
struct serial_device
*default_serial_console(void)
117 return &jz_serial_drv
;