blob: cedebfe496bc257bffea7f56c781e97be9654802 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
4 *
5 * (C) Copyright 2002
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
8 *
9 * (C) Copyright 2002
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Alex Zuepke <azu@sysgo.de>
12 *
13 * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 */
30
31#include <common.h>
wdenk5f535fe2003-09-18 09:21:33 +000032#include <watchdog.h>
wdenkc6097192002-11-03 00:24:07 +000033#include <asm/arch/pxa-regs.h>
34
35void serial_setbrg (void)
36{
37 DECLARE_GLOBAL_DATA_PTR;
38
39 unsigned int quot = 0;
40
41 if (gd->baudrate == 1200)
wdenk5f535fe2003-09-18 09:21:33 +000042 quot = 768;
wdenkc6097192002-11-03 00:24:07 +000043 else if (gd->baudrate == 9600)
44 quot = 96;
45 else if (gd->baudrate == 19200)
46 quot = 48;
47 else if (gd->baudrate == 38400)
48 quot = 24;
49 else if (gd->baudrate == 57600)
50 quot = 16;
51 else if (gd->baudrate == 115200)
52 quot = 8;
53 else
54 hang ();
55
56#ifdef CONFIG_FFUART
wdenkc6097192002-11-03 00:24:07 +000057 CKEN |= CKEN6_FFUART;
58
59 FFIER = 0; /* Disable for now */
60 FFFCR = 0; /* No fifos enabled */
61
62 /* set baud rate */
63 FFLCR = LCR_WLS0 | LCR_WLS1 | LCR_DLAB;
64 FFDLL = quot & 0xff;
65 FFDLH = quot >> 8;
66 FFLCR = LCR_WLS0 | LCR_WLS1;
67
68 FFIER = IER_UUE; /* Enable FFUART */
69
wdenk3e386912003-04-05 00:53:31 +000070#elif defined(CONFIG_BTUART)
71 CKEN |= CKEN7_BTUART;
72
73 BTIER = 0;
74 BTFCR = 0;
75
76 /* set baud rate */
77 BTLCR = LCR_DLAB;
78 BTDLL = quot & 0xff;
79 BTDLH = quot >> 8;
80 BTLCR = LCR_WLS0 | LCR_WLS1;
81
82 BTIER = IER_UUE; /* Enable BFUART */
83
84#elif defined(CONFIG_STUART)
wdenk5f535fe2003-09-18 09:21:33 +000085 CKEN |= CKEN5_STUART;
86
87 STIER = 0;
88 STFCR = 0;
89
90 /* set baud rate */
91 STLCR = LCR_DLAB;
92 STDLL = quot & 0xff;
93 STDLH = quot >> 8;
94 STLCR = LCR_WLS0 | LCR_WLS1;
95
96 STIER = IER_UUE; /* Enable STUART */
97
wdenkc6097192002-11-03 00:24:07 +000098#else
wdenk5f535fe2003-09-18 09:21:33 +000099#error "Bad: you didn't configure serial ..."
wdenkc6097192002-11-03 00:24:07 +0000100#endif
101}
102
103
104/*
105 * Initialise the serial port with the given baudrate. The settings
106 * are always 8 data bits, no parity, 1 stop bit, no start bits.
107 *
108 */
109int serial_init (void)
110{
111 serial_setbrg ();
112
113 return (0);
114}
115
116
117/*
118 * Output a single byte to the serial port.
119 */
120void serial_putc (const char c)
121{
122#ifdef CONFIG_FFUART
123 /* wait for room in the tx FIFO on FFUART */
wdenk5f535fe2003-09-18 09:21:33 +0000124 while ((FFLSR & LSR_TEMT) == 0)
125 WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
wdenkc6097192002-11-03 00:24:07 +0000126 FFTHR = c;
wdenk3e386912003-04-05 00:53:31 +0000127#elif defined(CONFIG_BTUART)
wdenk5f535fe2003-09-18 09:21:33 +0000128 while ((BTLSR & LSR_TEMT ) == 0 )
129 WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
wdenk3e386912003-04-05 00:53:31 +0000130 BTTHR = c;
131#elif defined(CONFIG_STUART)
wdenk5f535fe2003-09-18 09:21:33 +0000132 while ((STLSR & LSR_TEMT ) == 0 )
133 WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
134 STTHR = c;
wdenkc6097192002-11-03 00:24:07 +0000135#endif
136
137 /* If \n, also do \r */
138 if (c == '\n')
139 serial_putc ('\r');
140}
141
142/*
143 * Read a single byte from the serial port. Returns 1 on success, 0
144 * otherwise. When the function is succesfull, the character read is
145 * written into its argument c.
146 */
147int serial_tstc (void)
148{
149#ifdef CONFIG_FFUART
150 return FFLSR & LSR_DR;
wdenk3e386912003-04-05 00:53:31 +0000151#elif defined(CONFIG_BTUART)
152 return BTLSR & LSR_DR;
153#elif defined(CONFIG_STUART)
wdenk5f535fe2003-09-18 09:21:33 +0000154 return STLSR & LSR_DR;
wdenkc6097192002-11-03 00:24:07 +0000155#endif
156}
157
158/*
159 * Read a single byte from the serial port. Returns 1 on success, 0
160 * otherwise. When the function is succesfull, the character read is
161 * written into its argument c.
162 */
163int serial_getc (void)
164{
165#ifdef CONFIG_FFUART
wdenk5f535fe2003-09-18 09:21:33 +0000166 while (!(FFLSR & LSR_DR))
167 WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
wdenkc6097192002-11-03 00:24:07 +0000168 return (char) FFRBR & 0xff;
wdenk3e386912003-04-05 00:53:31 +0000169#elif defined(CONFIG_BTUART)
wdenk5f535fe2003-09-18 09:21:33 +0000170 while (!(BTLSR & LSR_DR))
171 WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
wdenk3e386912003-04-05 00:53:31 +0000172 return (char) BTRBR & 0xff;
173#elif defined(CONFIG_STUART)
wdenk5f535fe2003-09-18 09:21:33 +0000174 while (!(STLSR & LSR_DR))
175 WATCHDOG_RESET (); /* Reset HW Watchdog, if needed */
176 return (char) STRBR & 0xff;
wdenkc6097192002-11-03 00:24:07 +0000177#endif
178}
179
180void
181serial_puts (const char *s)
182{
183 while (*s) {
184 serial_putc (*s++);
185 }
186}