4 * Qi Hardware, Xiangfu Liu <xiangfu@sharism.cc>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include <asm/jz4740.h>
27 #include <linux/compiler.h>
30 * serial_init - initialize a channel
32 * This routine initializes the number of data bits, parity
33 * and set the selected baud rate. Interrupts are disabled.
34 * Set the modem control signals if the option is selected.
38 struct jz4740_uart *uart = (struct jz4740_uart *)CONFIG_SYS_UART_BASE;
40 static int jz_serial_init(void)
42 /* Disable port interrupts while changing hardware */
43 writeb(0, &uart->dlhr_ier);
45 /* Disable UART unit function */
46 writeb(~UART_FCR_UUE, &uart->iir_fcr);
48 /* Set both receiver and transmitter in UART mode (not SIR) */
49 writeb(~(SIRCR_RSIRE | SIRCR_TSIRE), &uart->isr);
52 * Set databits, stopbits and parity.
53 * (8-bit data, 1 stopbit, no parity)
55 writeb(UART_LCR_WLEN_8 | UART_LCR_STOP_1, &uart->lcr);
60 /* Enable UART unit, enable and clear FIFO */
61 writeb(UART_FCR_UUE | UART_FCR_FE | UART_FCR_TFLS | UART_FCR_RFLS,
67 static void jz_serial_setbrg(void)
71 baud_div = CONFIG_SYS_EXTAL / 16 / CONFIG_BAUDRATE;
73 tmp = readb(&uart->lcr);
75 writeb(tmp, &uart->lcr);
77 writeb((baud_div >> 8) & 0xff, &uart->dlhr_ier);
78 writeb(baud_div & 0xff, &uart->rbr_thr_dllr);
80 tmp &= ~UART_LCR_DLAB;
81 writeb(tmp, &uart->lcr);
84 static int jz_serial_tstc(void)
86 if (readb(&uart->lsr) & UART_LSR_DR)
92 static void jz_serial_putc(const char c)
97 /* Wait for fifo to shift out some bytes */
98 while (!((readb(&uart->lsr) & (UART_LSR_TDRQ | UART_LSR_TEMT)) == 0x60))
101 writeb((u8)c, &uart->rbr_thr_dllr);
104 static int jz_serial_getc(void)
106 while (!serial_tstc())
109 return readb(&uart->rbr_thr_dllr);
112 static void jz_serial_puts(const char *s)
118 static struct serial_device jz_serial_drv = {
120 .start = jz_serial_init,
122 .setbrg = jz_serial_setbrg,
123 .putc = jz_serial_putc,
124 .puts = jz_serial_puts,
125 .getc = jz_serial_getc,
126 .tstc = jz_serial_tstc,
129 void jz_serial_initialize(void)
131 serial_register(&jz_serial_drv);
134 __weak struct serial_device *default_serial_console(void)
136 return &jz_serial_drv;