]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - common/serial.c
Cleanup
[karo-tx-uboot.git] / common / serial.c
1 /*
2  * (C) Copyright 2004
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
24 #include <common.h>
25 #include <serial.h>
26 #include <devices.h>
27
28 #if defined(CONFIG_SERIAL_MULTI)
29
30 static struct serial_device *serial_devices = NULL;
31 static struct serial_device *serial_current = NULL;
32
33 #ifndef CONFIG_LWMON
34 struct serial_device *default_serial_console (void)
35 {
36 #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
37         return &serial_smc_device;
38 #elif defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
39    || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
40         return &serial_scc_device;
41 #else
42 #error No default console
43 #endif
44 }
45 #endif
46
47 static int serial_register (struct serial_device *dev)
48 {
49         DECLARE_GLOBAL_DATA_PTR;
50
51         dev->init += gd->reloc_off;
52         dev->setbrg += gd->reloc_off;
53         dev->getc += gd->reloc_off;
54         dev->tstc += gd->reloc_off;
55         dev->putc += gd->reloc_off;
56         dev->puts += gd->reloc_off;
57
58         dev->next = serial_devices;
59         serial_devices = dev;
60
61         return 0;
62 }
63
64 void serial_initialize (void)
65 {
66 #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
67         serial_register (&serial_smc_device);
68 #endif
69 #if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
70  || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
71         serial_register (&serial_scc_device);
72 #endif
73
74         serial_assign (default_serial_console ()->name);
75 }
76
77 void serial_devices_init (void)
78 {
79         device_t dev;
80         struct serial_device *s = serial_devices;
81
82         while (s) {
83                 memset (&dev, 0, sizeof (dev));
84
85                 strcpy (dev.name, s->name);
86                 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
87
88                 dev.start = s->init;
89                 dev.putc = s->putc;
90                 dev.puts = s->puts;
91                 dev.getc = s->getc;
92                 dev.tstc = s->tstc;
93
94                 device_register (&dev);
95
96                 s = s->next;
97         }
98 }
99
100 int serial_assign (char *name)
101 {
102         struct serial_device *s;
103
104         for (s = serial_devices; s; s = s->next) {
105                 if (strcmp (s->name, name) == 0) {
106                         serial_current = s;
107                         return 0;
108                 }
109         }
110
111         return 1;
112 }
113
114 void serial_reinit_all (void)
115 {
116         struct serial_device *s;
117
118         for (s = serial_devices; s; s = s->next) {
119                 s->init ();
120         }
121 }
122
123 int serial_init (void)
124 {
125         DECLARE_GLOBAL_DATA_PTR;
126
127         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
128                 struct serial_device *dev = default_serial_console ();
129
130                 return dev->init ();
131         }
132
133         return serial_current->init ();
134 }
135
136 void serial_setbrg (void)
137 {
138         DECLARE_GLOBAL_DATA_PTR;
139
140         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
141                 struct serial_device *dev = default_serial_console ();
142
143                 dev->setbrg ();
144                 return;
145         }
146
147         serial_current->setbrg ();
148 }
149
150 int serial_getc (void)
151 {
152         DECLARE_GLOBAL_DATA_PTR;
153
154         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
155                 struct serial_device *dev = default_serial_console ();
156
157                 return dev->getc ();
158         }
159
160         return serial_current->getc ();
161 }
162
163 int serial_tstc (void)
164 {
165         DECLARE_GLOBAL_DATA_PTR;
166
167         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
168                 struct serial_device *dev = default_serial_console ();
169
170                 return dev->tstc ();
171         }
172
173         return serial_current->tstc ();
174 }
175
176 void serial_putc (const char c)
177 {
178         DECLARE_GLOBAL_DATA_PTR;
179
180         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
181                 struct serial_device *dev = default_serial_console ();
182
183                 dev->putc (c);
184                 return;
185         }
186
187         serial_current->putc (c);
188 }
189
190 void serial_puts (const char *s)
191 {
192         DECLARE_GLOBAL_DATA_PTR;
193
194         if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
195                 struct serial_device *dev = default_serial_console ();
196
197                 dev->puts (s);
198                 return;
199         }
200
201         serial_current->puts (s);
202 }
203
204 #endif /* CONFIG_SERIAL_MULTI */