]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - cpu/sa1100/serial.c
68bcd1f2a4061398fdf767172db215c714afdd13
[karo-tx-uboot.git] / cpu / sa1100 / serial.c
1 /*
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>
32 #include <SA-1100.h>
33
34 void serial_setbrg (void)
35 {
36         DECLARE_GLOBAL_DATA_PTR;
37
38         unsigned int reg = 0;
39
40         if (gd->baudrate == 1200)
41                 reg = 191;
42         else if (gd->baudrate == 9600)
43                 reg = 23;
44         else if (gd->baudrate == 19200)
45                 reg = 11;
46         else if (gd->baudrate == 38400)
47                 reg = 5;
48         else if (gd->baudrate == 57600)
49                 reg = 3;
50         else if (gd->baudrate == 115200)
51                 reg = 1;
52         else
53                 hang ();
54
55 #ifdef CONFIG_SERIAL1
56         /* Wait until port is ready ... */
57         while (Ser1UTSR1 & UTSR1_TBY) {
58         }
59
60         /* init serial serial 1 */
61         Ser1UTCR3 = 0x00;
62         Ser1UTSR0 = 0xff;
63         Ser1UTCR0 = (UTCR0_1StpBit | UTCR0_8BitData);
64         Ser1UTCR1 = 0;
65         Ser1UTCR2 = (u32) reg;
66         Ser1UTCR3 = (UTCR3_RXE | UTCR3_TXE);
67 #elif CONFIG_SERIAL3
68         /* Wait until port is ready ... */
69         while (Ser3UTSR1 & UTSR1_TBY) {
70         }
71
72         /* init serial serial 3 */
73         Ser3UTCR3 = 0x00;
74         Ser3UTSR0 = 0xff;
75         Ser3UTCR0 = (UTCR0_1StpBit | UTCR0_8BitData);
76         Ser3UTCR1 = 0;
77         Ser3UTCR2 = (u32) reg;
78         Ser3UTCR3 = (UTCR3_RXE | UTCR3_TXE);
79 #else
80 #error "Bad: you didn't configured serial ..."
81 #endif
82 }
83
84
85 /*
86  * Initialise the serial port with the given baudrate. The settings
87  * are always 8 data bits, no parity, 1 stop bit, no start bits.
88  *
89  */
90 int serial_init (void)
91 {
92         serial_setbrg ();
93
94         return (0);
95 }
96
97
98 /*
99  * Output a single byte to the serial port.
100  */
101 void serial_putc (const char c)
102 {
103 #ifdef CONFIG_SERIAL1
104         /* wait for room in the tx FIFO on SERIAL1 */
105         while ((Ser1UTSR0 & UTSR0_TFS) == 0);
106
107         Ser1UTDR = c;
108 #elif CONFIG_SERIAL3
109         /* wait for room in the tx FIFO on SERIAL3 */
110         while ((Ser3UTSR0 & UTSR0_TFS) == 0);
111
112         Ser3UTDR = c;
113 #endif
114
115         /* If \n, also do \r */
116         if (c == '\n')
117                 serial_putc ('\r');
118 }
119
120 /*
121  * Read a single byte from the serial port. Returns 1 on success, 0
122  * otherwise. When the function is succesfull, the character read is
123  * written into its argument c.
124  */
125 int serial_tstc (void)
126 {
127 #ifdef CONFIG_SERIAL1
128         return Ser1UTSR1 & UTSR1_RNE;
129 #elif CONFIG_SERIAL3
130         return Ser3UTSR1 & UTSR1_RNE;
131 #endif
132 }
133
134 /*
135  * Read a single byte from the serial port. Returns 1 on success, 0
136  * otherwise. When the function is succesfull, the character read is
137  * written into its argument c.
138  */
139 int serial_getc (void)
140 {
141 #ifdef CONFIG_SERIAL1
142         while (!(Ser1UTSR1 & UTSR1_RNE));
143
144         return (char) Ser1UTDR & 0xff;
145 #elif CONFIG_SERIAL3
146         while (!(Ser3UTSR1 & UTSR1_RNE));
147
148         return (char) Ser3UTDR & 0xff;
149 #endif
150 }
151
152 void
153 serial_puts (const char *s)
154 {
155         while (*s) {
156                 serial_putc (*s++);
157         }
158 }