serial.c 6.15 KB
Newer Older
wdenk's avatar
wdenk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * (C) Copyright 2004
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <serial.h>
#include <devices.h>

28 29
DECLARE_GLOBAL_DATA_PTR;

wdenk's avatar
wdenk committed
30 31 32
static struct serial_device *serial_devices = NULL;
static struct serial_device *serial_current = NULL;

33
#if !defined(CONFIG_LWMON) && !defined(CONFIG_PXA27X)
34
struct serial_device *__default_serial_console (void)
wdenk's avatar
wdenk committed
35 36 37 38 39 40
{
#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
	return &serial_smc_device;
#elif defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
   || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
	return &serial_scc_device;
wdenk's avatar
wdenk committed
41
#elif defined(CONFIG_405GP) || defined(CONFIG_405CR) || defined(CONFIG_440) \
Stefan Roese's avatar
Stefan Roese committed
42
   || defined(CONFIG_405EP) || defined(CONFIG_405EZ) || defined(CONFIG_405EX) \
43 44
   || defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC83xx) \
   || defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
45
#if defined(CONFIG_CONS_INDEX) && defined(CONFIG_SYS_NS16550_SERIAL)
46 47 48 49 50 51 52 53 54 55 56 57
#if (CONFIG_CONS_INDEX==1)
	return &eserial1_device;
#elif (CONFIG_CONS_INDEX==2)
	return &eserial2_device;
#elif (CONFIG_CONS_INDEX==3)
	return &eserial3_device;
#elif (CONFIG_CONS_INDEX==4)
	return &eserial4_device;
#else
#error "Bad CONFIG_CONS_INDEX."
#endif
#elif defined(CONFIG_UART1_CONSOLE)
58 59 60 61
		return &serial1_device;
#else
		return &serial0_device;
#endif
62 63 64 65 66 67 68 69 70 71
#elif defined(CONFIG_S3C2410)
#if defined(CONFIG_SERIAL1)
	return &s3c24xx_serial0_device;
#elif defined(CONFIG_SERIAL2)
	return &s3c24xx_serial1_device;
#elif defined(CONFIG_SERIAL3)
	return &s3c24xx_serial2_device;
#else
#error "CONFIG_SERIAL? missing."
#endif
Tom Rix's avatar
Tom Rix committed
72 73
#elif defined(CONFIG_OMAP3_ZOOM2)
		return ZOOM2_DEFAULT_SERIAL_DEVICE;
wdenk's avatar
wdenk committed
74 75 76 77
#else
#error No default console
#endif
}
78 79

struct serial_device *default_serial_console(void) __attribute__((weak, alias("__default_serial_console")));
wdenk's avatar
wdenk committed
80 81
#endif

82
int serial_register (struct serial_device *dev)
wdenk's avatar
wdenk committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96
{
	dev->init += gd->reloc_off;
	dev->setbrg += gd->reloc_off;
	dev->getc += gd->reloc_off;
	dev->tstc += gd->reloc_off;
	dev->putc += gd->reloc_off;
	dev->puts += gd->reloc_off;

	dev->next = serial_devices;
	serial_devices = dev;

	return 0;
}

97
void serial_initialize (void)
wdenk's avatar
wdenk committed
98 99
{
#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
100
	serial_register (&serial_smc_device);
wdenk's avatar
wdenk committed
101 102 103
#endif
#if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) \
 || defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
104
	serial_register (&serial_scc_device);
wdenk's avatar
wdenk committed
105 106
#endif

wdenk's avatar
wdenk committed
107
#if defined(CONFIG_405GP) || defined(CONFIG_405CR) || defined(CONFIG_440) \
Stefan Roese's avatar
Stefan Roese committed
108 109
 || defined(CONFIG_405EP) || defined(CONFIG_405EZ) || defined(CONFIG_405EX) \
 || defined(CONFIG_MPC5xxx)
wdenk's avatar
wdenk committed
110 111 112 113
	serial_register(&serial0_device);
	serial_register(&serial1_device);
#endif

114 115
#if defined(CONFIG_SYS_NS16550_SERIAL)
#if defined(CONFIG_SYS_NS16550_COM1)
116 117
	serial_register(&eserial1_device);
#endif
118
#if defined(CONFIG_SYS_NS16550_COM2)
119 120
	serial_register(&eserial2_device);
#endif
121
#if defined(CONFIG_SYS_NS16550_COM3)
122 123
	serial_register(&eserial3_device);
#endif
124
#if defined(CONFIG_SYS_NS16550_COM4)
125 126
	serial_register(&eserial4_device);
#endif
127
#endif /* CONFIG_SYS_NS16550_SERIAL */
128 129 130 131 132 133 134 135
#if defined (CONFIG_FFUART)
	serial_register(&serial_ffuart_device);
#endif
#if defined (CONFIG_BTUART)
	serial_register(&serial_btuart_device);
#endif
#if defined (CONFIG_STUART)
	serial_register(&serial_stuart_device);
136 137 138 139 140
#endif
#if defined(CONFIG_S3C2410)
	serial_register(&s3c24xx_serial0_device);
	serial_register(&s3c24xx_serial1_device);
	serial_register(&s3c24xx_serial2_device);
141
#endif
142
	serial_assign (default_serial_console ()->name);
wdenk's avatar
wdenk committed
143 144
}

145
void serial_devices_init (void)
wdenk's avatar
wdenk committed
146 147 148 149
{
	device_t dev;
	struct serial_device *s = serial_devices;

150
	while (s) {
wdenk's avatar
wdenk committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		memset (&dev, 0, sizeof (dev));

		strcpy (dev.name, s->name);
		dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;

		dev.start = s->init;
		dev.putc = s->putc;
		dev.puts = s->puts;
		dev.getc = s->getc;
		dev.tstc = s->tstc;

		device_register (&dev);

		s = s->next;
	}
}

168
int serial_assign (char *name)
wdenk's avatar
wdenk committed
169 170 171
{
	struct serial_device *s;

172 173
	for (s = serial_devices; s; s = s->next) {
		if (strcmp (s->name, name) == 0) {
wdenk's avatar
wdenk committed
174 175 176 177 178 179 180 181
			serial_current = s;
			return 0;
		}
	}

	return 1;
}

182
void serial_reinit_all (void)
wdenk's avatar
wdenk committed
183 184 185
{
	struct serial_device *s;

186 187
	for (s = serial_devices; s; s = s->next) {
		s->init ();
wdenk's avatar
wdenk committed
188 189 190
	}
}

191
int serial_init (void)
wdenk's avatar
wdenk committed
192
{
193 194 195 196
	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
		struct serial_device *dev = default_serial_console ();

		return dev->init ();
wdenk's avatar
wdenk committed
197 198
	}

199
	return serial_current->init ();
wdenk's avatar
wdenk committed
200 201
}

202
void serial_setbrg (void)
wdenk's avatar
wdenk committed
203
{
204 205 206 207
	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
		struct serial_device *dev = default_serial_console ();

		dev->setbrg ();
wdenk's avatar
wdenk committed
208 209 210
		return;
	}

211
	serial_current->setbrg ();
wdenk's avatar
wdenk committed
212 213
}

214
int serial_getc (void)
wdenk's avatar
wdenk committed
215
{
216 217 218 219
	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
		struct serial_device *dev = default_serial_console ();

		return dev->getc ();
wdenk's avatar
wdenk committed
220 221
	}

222
	return serial_current->getc ();
wdenk's avatar
wdenk committed
223 224
}

225
int serial_tstc (void)
wdenk's avatar
wdenk committed
226
{
227 228 229 230
	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
		struct serial_device *dev = default_serial_console ();

		return dev->tstc ();
wdenk's avatar
wdenk committed
231 232
	}

233
	return serial_current->tstc ();
wdenk's avatar
wdenk committed
234 235
}

236
void serial_putc (const char c)
wdenk's avatar
wdenk committed
237
{
238 239 240 241
	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
		struct serial_device *dev = default_serial_console ();

		dev->putc (c);
wdenk's avatar
wdenk committed
242 243 244
		return;
	}

245
	serial_current->putc (c);
wdenk's avatar
wdenk committed
246 247
}

248
void serial_puts (const char *s)
wdenk's avatar
wdenk committed
249
{
250 251 252 253
	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
		struct serial_device *dev = default_serial_console ();

		dev->puts (s);
wdenk's avatar
wdenk committed
254 255 256
		return;
	}

257
	serial_current->puts (s);
wdenk's avatar
wdenk committed
258
}