blob: 3e85b90cded451b709da8dc795b164e8e8a34ab1 [file] [log] [blame]
wdenk9b7f3842003-10-09 20:09:04 +00001/*
2 * AU1X00 UART support
3 *
4 * Hardcoded to UART 0 for now
5 * Speed and options also hardcoded to 115200 8N1
6 *
7 * Copyright (c) 2003 Thomas.Lange@corelatus.se
8 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <config.h>
wdenk9b7f3842003-10-09 20:09:04 +000029#include <common.h>
30#include <asm/au1x00.h>
Marek Vasut01f9ea82012-09-13 01:16:50 +020031#include <serial.h>
32#include <linux/compiler.h>
wdenk9b7f3842003-10-09 20:09:04 +000033
34/******************************************************************************
35*
36* serial_init - initialize a channel
37*
38* This routine initializes the number of data bits, parity
39* and set the selected baud rate. Interrupts are disabled.
40* Set the modem control signals if the option is selected.
41*
42* RETURNS: N/A
43*/
44
Marek Vasut01f9ea82012-09-13 01:16:50 +020045static int au1x00_serial_init(void)
wdenk9b7f3842003-10-09 20:09:04 +000046{
47 volatile u32 *uart_fifoctl = (volatile u32*)(UART0_ADDR+UART_FCR);
48 volatile u32 *uart_enable = (volatile u32*)(UART0_ADDR+UART_ENABLE);
49
50 /* Enable clocks first */
51 *uart_enable = UART_EN_CE;
52
53 /* Then release reset */
54 /* Must release reset before setting other regs */
55 *uart_enable = UART_EN_CE|UART_EN_E;
56
57 /* Activate fifos, reset tx and rx */
58 /* Set tx trigger level to 12 */
59 *uart_fifoctl = UART_FCR_ENABLE_FIFO|UART_FCR_CLEAR_RCVR|
60 UART_FCR_CLEAR_XMIT|UART_FCR_T_TRIGGER_12;
61
62 serial_setbrg();
63
64 return 0;
65}
66
67
Marek Vasut01f9ea82012-09-13 01:16:50 +020068static void au1x00_serial_setbrg(void)
wdenk9b7f3842003-10-09 20:09:04 +000069{
70 volatile u32 *uart_clk = (volatile u32*)(UART0_ADDR+UART_CLK);
71 volatile u32 *uart_lcr = (volatile u32*)(UART0_ADDR+UART_LCR);
Wolfgang Denk017bc0d2005-09-25 16:50:33 +020072 volatile u32 *sys_powerctrl = (u32 *)SYS_POWERCTRL;
73 int sd;
74 int divisorx2;
75
76 /* sd is system clock divisor */
77 /* see section 10.4.5 in au1550 datasheet */
78 sd = (*sys_powerctrl & 0x03) + 2;
79
80 /* calulate 2x baudrate and round */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020081 divisorx2 = ((CONFIG_SYS_MIPS_TIMER_FREQ/(sd * 16 * CONFIG_BAUDRATE)));
Wolfgang Denk017bc0d2005-09-25 16:50:33 +020082
83 if (divisorx2 & 0x01)
84 divisorx2 = divisorx2 + 1;
wdenk9b7f3842003-10-09 20:09:04 +000085
Wolfgang Denk017bc0d2005-09-25 16:50:33 +020086 *uart_clk = divisorx2 / 2;
wdenk9b7f3842003-10-09 20:09:04 +000087
88 /* Set parity, stop bits and word length to 8N1 */
89 *uart_lcr = UART_LCR_WLEN8;
90}
91
Marek Vasut01f9ea82012-09-13 01:16:50 +020092static void au1x00_serial_putc(const char c)
wdenk9b7f3842003-10-09 20:09:04 +000093{
94 volatile u32 *uart_lsr = (volatile u32*)(UART0_ADDR+UART_LSR);
95 volatile u32 *uart_tx = (volatile u32*)(UART0_ADDR+UART_TX);
96
Marek Vasut01f9ea82012-09-13 01:16:50 +020097 if (c == '\n')
98 au1x00_serial_putc('\r');
wdenk9b7f3842003-10-09 20:09:04 +000099
100 /* Wait for fifo to shift out some bytes */
101 while((*uart_lsr&UART_LSR_THRE)==0);
102
103 *uart_tx = (u32)c;
104}
105
Marek Vasut01f9ea82012-09-13 01:16:50 +0200106static int au1x00_serial_getc(void)
wdenk9b7f3842003-10-09 20:09:04 +0000107{
108 volatile u32 *uart_rx = (volatile u32*)(UART0_ADDR+UART_RX);
109 char c;
110
111 while (!serial_tstc());
112
113 c = (*uart_rx&0xFF);
114 return c;
115}
116
Marek Vasut01f9ea82012-09-13 01:16:50 +0200117static int au1x00_serial_tstc(void)
wdenk9b7f3842003-10-09 20:09:04 +0000118{
119 volatile u32 *uart_lsr = (volatile u32*)(UART0_ADDR+UART_LSR);
120
121 if(*uart_lsr&UART_LSR_DR){
122 /* Data in rfifo */
123 return(1);
124 }
125 return 0;
126}
Marek Vasut01f9ea82012-09-13 01:16:50 +0200127
Marek Vasut01f9ea82012-09-13 01:16:50 +0200128static struct serial_device au1x00_serial_drv = {
129 .name = "au1x00_serial",
130 .start = au1x00_serial_init,
131 .stop = NULL,
132 .setbrg = au1x00_serial_setbrg,
133 .putc = au1x00_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000134 .puts = default_serial_puts,
Marek Vasut01f9ea82012-09-13 01:16:50 +0200135 .getc = au1x00_serial_getc,
136 .tstc = au1x00_serial_tstc,
137};
138
139void au1x00_serial_initialize(void)
140{
141 serial_register(&au1x00_serial_drv);
142}
143
144__weak struct serial_device *default_serial_console(void)
145{
146 return &au1x00_serial_drv;
147}