* Code cleanup
[platform/kernel/u-boot.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         {
84                 memset (&dev, 0, sizeof (dev));
85
86                 strcpy (dev.name, s->name);
87                 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
88
89                 dev.start = s->init;
90                 dev.putc = s->putc;
91                 dev.puts = s->puts;
92                 dev.getc = s->getc;
93                 dev.tstc = s->tstc;
94
95                 device_register (&dev);
96
97                 s = s->next;
98         }
99 }
100
101 int serial_assign(char * name)
102 {
103         struct serial_device *s;
104
105         for (s = serial_devices; s; s = s->next)
106         {
107                 if (strcmp(s->name, name) == 0)
108                 {
109                         serial_current = s;
110                         return 0;
111                 }
112         }
113
114         return 1;
115 }
116
117 void serial_reinit_all(void)
118 {
119         struct serial_device *s;
120
121         for (s = serial_devices; s; s = s->next)
122         {
123                 s->init();
124         }
125 }
126
127 int serial_init(void)
128 {
129         if (!serial_current)
130         {
131                 struct serial_device *dev = default_serial_console();
132                 return dev->init();
133         }
134
135         return serial_current->init();
136 }
137
138 void serial_setbrg(void)
139 {
140         if (!serial_current)
141         {
142                 struct serial_device *dev = default_serial_console();
143                 dev->setbrg();
144                 return;
145         }
146
147         serial_current->setbrg();
148 }
149
150 int serial_getc(void)
151 {
152         if (!serial_current)
153         {
154                 struct serial_device *dev = default_serial_console();
155                 return dev->getc();
156         }
157
158         return serial_current->getc();
159 }
160
161 int serial_tstc(void)
162 {
163         if (!serial_current)
164         {
165                 struct serial_device *dev = default_serial_console();
166                 return dev->tstc();
167         }
168
169         return serial_current->tstc();
170 }
171
172 void serial_putc(const char c)
173 {
174         if (!serial_current)
175         {
176                 struct serial_device *dev = default_serial_console();
177                 dev->putc(c);
178                 return;
179         }
180
181         serial_current->putc(c);
182 }
183
184 void serial_puts(const char *s)
185 {
186         if (!serial_current)
187         {
188                 struct serial_device *dev = default_serial_console();
189                 dev->puts(s);
190                 return;
191         }
192
193         serial_current->puts(s);
194 }
195
196 #endif /* CONFIG_SERIAL_MULTI */