]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/ppc/cpu/mpc5xxx/serial.c
Merge branch 'master' of git://git.denx.de/u-boot-arm
[karo-tx-uboot.git] / arch / ppc / cpu / mpc5xxx / serial.c
1 /*
2  * (C) Copyright 2000 - 2003
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  *
23  * Hacked for MPC8260 by Murray.Jensen@cmst.csiro.au, 19-Oct-00, with
24  * changes based on the file arch/ppc/mbxboot/m8260_tty.c from the
25  * Linux/PPC sources (m8260_tty.c had no copyright info in it).
26  *
27  * Martin Krause, 8 Jun 2006
28  * Added CONFIG_SERIAL_MULTI support
29  */
30
31 /*
32  * Minimal serial functions needed to use one of the PSC ports
33  * as serial console interface.
34  */
35
36 #include <common.h>
37 #include <mpc5xxx.h>
38
39 #if defined (CONFIG_SERIAL_MULTI)
40 #include <serial.h>
41 #endif
42
43 DECLARE_GLOBAL_DATA_PTR;
44
45 #if defined(CONFIG_PSC_CONSOLE)
46
47 #if CONFIG_PSC_CONSOLE == 1
48 #define PSC_BASE MPC5XXX_PSC1
49 #elif CONFIG_PSC_CONSOLE == 2
50 #define PSC_BASE MPC5XXX_PSC2
51 #elif CONFIG_PSC_CONSOLE == 3
52 #define PSC_BASE MPC5XXX_PSC3
53 #elif CONFIG_PSC_CONSOLE == 4
54 #define PSC_BASE MPC5XXX_PSC4
55 #elif CONFIG_PSC_CONSOLE == 5
56 #define PSC_BASE MPC5XXX_PSC5
57 #elif CONFIG_PSC_CONSOLE == 6
58 #define PSC_BASE MPC5XXX_PSC6
59 #else
60 #error CONFIG_PSC_CONSOLE must be in 1 ... 6
61 #endif
62
63 #if defined(CONFIG_SERIAL_MULTI) && !defined(CONFIG_PSC_CONSOLE2)
64 #error you must define CONFIG_PSC_CONSOLE2 if CONFIG_SERIAL_MULTI is set
65 #endif
66
67 #if defined(CONFIG_SERIAL_MULTI)
68 #if CONFIG_PSC_CONSOLE2 == 1
69 #define PSC_BASE2 MPC5XXX_PSC1
70 #elif CONFIG_PSC_CONSOLE2 == 2
71 #define PSC_BASE2 MPC5XXX_PSC2
72 #elif CONFIG_PSC_CONSOLE2 == 3
73 #define PSC_BASE2 MPC5XXX_PSC3
74 #elif CONFIG_PSC_CONSOLE2 == 4
75 #define PSC_BASE2 MPC5XXX_PSC4
76 #elif CONFIG_PSC_CONSOLE2 == 5
77 #define PSC_BASE2 MPC5XXX_PSC5
78 #elif CONFIG_PSC_CONSOLE2 == 6
79 #define PSC_BASE2 MPC5XXX_PSC6
80 #else
81 #error CONFIG_PSC_CONSOLE2 must be in 1 ... 6
82 #endif
83 #endif /* CONFIG_SERIAL_MULTI */
84
85 #if defined(CONFIG_SERIAL_MULTI)
86 int serial_init_dev (unsigned long dev_base)
87 #else
88 int serial_init (void)
89 #endif
90 {
91 #if defined(CONFIG_SERIAL_MULTI)
92         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
93 #else
94         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
95 #endif
96         unsigned long baseclk;
97         int div;
98
99         /* reset PSC */
100         psc->command = PSC_SEL_MODE_REG_1;
101
102         /* select clock sources */
103         psc->psc_clock_select = 0;
104         baseclk = (gd->ipb_clk + 16) / 32;
105
106         /* switch to UART mode */
107         psc->sicr = 0;
108
109         /* configure parity, bit length and so on */
110         psc->mode = PSC_MODE_8_BITS | PSC_MODE_PARNONE;
111         psc->mode = PSC_MODE_ONE_STOP;
112
113         /* set up UART divisor */
114         div = (baseclk + (gd->baudrate/2)) / gd->baudrate;
115         psc->ctur = (div >> 8) & 0xff;
116         psc->ctlr = div & 0xff;
117
118         /* disable all interrupts */
119         psc->psc_imr = 0;
120
121         /* reset and enable Rx/Tx */
122         psc->command = PSC_RST_RX;
123         psc->command = PSC_RST_TX;
124         psc->command = PSC_RX_ENABLE | PSC_TX_ENABLE;
125
126         return (0);
127 }
128
129 #if defined(CONFIG_SERIAL_MULTI)
130 void serial_putc_dev (unsigned long dev_base, const char c)
131 #else
132 void serial_putc(const char c)
133 #endif
134 {
135 #if defined(CONFIG_SERIAL_MULTI)
136         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
137 #else
138         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
139 #endif
140
141         if (c == '\n')
142 #if defined(CONFIG_SERIAL_MULTI)
143                 serial_putc_dev (dev_base, '\r');
144 #else
145                 serial_putc('\r');
146 #endif
147
148         /* Wait for last character to go. */
149         while (!(psc->psc_status & PSC_SR_TXEMP))
150                 ;
151
152         psc->psc_buffer_8 = c;
153 }
154
155 #if defined(CONFIG_SERIAL_MULTI)
156 void serial_putc_raw_dev(unsigned long dev_base, const char c)
157 #else
158 void serial_putc_raw(const char c)
159 #endif
160 {
161 #if defined(CONFIG_SERIAL_MULTI)
162         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
163 #else
164         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
165 #endif
166         /* Wait for last character to go. */
167         while (!(psc->psc_status & PSC_SR_TXEMP))
168                 ;
169
170         psc->psc_buffer_8 = c;
171 }
172
173
174 #if defined(CONFIG_SERIAL_MULTI)
175 void serial_puts_dev (unsigned long dev_base, const char *s)
176 #else
177 void serial_puts (const char *s)
178 #endif
179 {
180         while (*s) {
181 #if defined(CONFIG_SERIAL_MULTI)
182                 serial_putc_dev (dev_base, *s++);
183 #else
184                 serial_putc (*s++);
185 #endif
186         }
187 }
188
189 #if defined(CONFIG_SERIAL_MULTI)
190 int serial_getc_dev (unsigned long dev_base)
191 #else
192 int serial_getc(void)
193 #endif
194 {
195 #if defined(CONFIG_SERIAL_MULTI)
196         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
197 #else
198         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
199 #endif
200
201         /* Wait for a character to arrive. */
202         while (!(psc->psc_status & PSC_SR_RXRDY))
203                 ;
204
205         return psc->psc_buffer_8;
206 }
207
208 #if defined(CONFIG_SERIAL_MULTI)
209 int serial_tstc_dev (unsigned long dev_base)
210 #else
211 int serial_tstc(void)
212 #endif
213 {
214 #if defined(CONFIG_SERIAL_MULTI)
215         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
216 #else
217         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
218 #endif
219
220         return (psc->psc_status & PSC_SR_RXRDY);
221 }
222
223 #if defined(CONFIG_SERIAL_MULTI)
224 void serial_setbrg_dev (unsigned long dev_base)
225 #else
226 void serial_setbrg(void)
227 #endif
228 {
229 #if defined(CONFIG_SERIAL_MULTI)
230         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
231 #else
232         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
233 #endif
234         unsigned long baseclk, div;
235
236         baseclk = (gd->ipb_clk + 16) / 32;
237
238         /* set up UART divisor */
239         div = (baseclk + (gd->baudrate/2)) / gd->baudrate;
240         psc->ctur = (div >> 8) & 0xFF;
241         psc->ctlr =  div & 0xff;
242 }
243
244 #if defined(CONFIG_SERIAL_MULTI)
245 void serial_setrts_dev (unsigned long dev_base, int s)
246 #else
247 void serial_setrts(int s)
248 #endif
249 {
250 #if defined(CONFIG_SERIAL_MULTI)
251         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
252 #else
253         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
254 #endif
255
256         if (s) {
257                 /* Assert RTS (become LOW) */
258                 psc->op1 = 0x1;
259         }
260         else {
261                 /* Negate RTS (become HIGH) */
262                 psc->op0 = 0x1;
263         }
264 }
265
266 #if defined(CONFIG_SERIAL_MULTI)
267 int serial_getcts_dev (unsigned long dev_base)
268 #else
269 int serial_getcts(void)
270 #endif
271 {
272 #if defined(CONFIG_SERIAL_MULTI)
273         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)dev_base;
274 #else
275         volatile struct mpc5xxx_psc *psc = (struct mpc5xxx_psc *)PSC_BASE;
276 #endif
277
278         return (psc->ip & 0x1) ? 0 : 1;
279 }
280
281 #if defined(CONFIG_SERIAL_MULTI)
282 int serial0_init(void)
283 {
284         return (serial_init_dev(PSC_BASE));
285 }
286
287 int serial1_init(void)
288 {
289         return (serial_init_dev(PSC_BASE2));
290 }
291 void serial0_setbrg (void)
292 {
293         serial_setbrg_dev(PSC_BASE);
294 }
295 void serial1_setbrg (void)
296 {
297         serial_setbrg_dev(PSC_BASE2);
298 }
299
300 void serial0_putc(const char c)
301 {
302         serial_putc_dev(PSC_BASE,c);
303 }
304
305 void serial1_putc(const char c)
306 {
307         serial_putc_dev(PSC_BASE2, c);
308 }
309 void serial0_puts(const char *s)
310 {
311         serial_puts_dev(PSC_BASE, s);
312 }
313
314 void serial1_puts(const char *s)
315 {
316         serial_puts_dev(PSC_BASE2, s);
317 }
318
319 int serial0_getc(void)
320 {
321         return(serial_getc_dev(PSC_BASE));
322 }
323
324 int serial1_getc(void)
325 {
326         return(serial_getc_dev(PSC_BASE2));
327 }
328 int serial0_tstc(void)
329 {
330         return (serial_tstc_dev(PSC_BASE));
331 }
332
333 int serial1_tstc(void)
334 {
335         return (serial_tstc_dev(PSC_BASE2));
336 }
337
338 struct serial_device serial0_device =
339 {
340         "serial0",
341         "UART0",
342         serial0_init,
343         serial0_setbrg,
344         serial0_getc,
345         serial0_tstc,
346         serial0_putc,
347         serial0_puts,
348 };
349
350 struct serial_device serial1_device =
351 {
352         "serial1",
353         "UART1",
354         serial1_init,
355         serial1_setbrg,
356         serial1_getc,
357         serial1_tstc,
358         serial1_putc,
359         serial1_puts,
360 };
361 #endif /* CONFIG_SERIAL_MULTI */
362
363 #endif /* CONFIG_PSC_CONSOLE */