wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 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 |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 34 | struct serial_device *default_serial_console (void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 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 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 47 | static int serial_register (struct serial_device *dev) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 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 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 64 | void serial_initialize (void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 65 | { |
| 66 | #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2) |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 67 | serial_register (&serial_smc_device); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 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) |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 71 | serial_register (&serial_scc_device); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 74 | serial_assign (default_serial_console ()->name); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 75 | } |
| 76 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 77 | void serial_devices_init (void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 78 | { |
| 79 | device_t dev; |
| 80 | struct serial_device *s = serial_devices; |
| 81 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 82 | while (s) { |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 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 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 100 | int serial_assign (char *name) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 101 | { |
| 102 | struct serial_device *s; |
| 103 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 104 | for (s = serial_devices; s; s = s->next) { |
| 105 | if (strcmp (s->name, name) == 0) { |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 106 | serial_current = s; |
| 107 | return 0; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return 1; |
| 112 | } |
| 113 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 114 | void serial_reinit_all (void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 115 | { |
| 116 | struct serial_device *s; |
| 117 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 118 | for (s = serial_devices; s; s = s->next) { |
| 119 | s->init (); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 123 | int serial_init (void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 124 | { |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 125 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 17b211b | 2004-10-11 23:10:30 +0000 | [diff] [blame] | 126 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 127 | if (!(gd->flags & GD_FLG_RELOC) || !serial_current) { |
| 128 | struct serial_device *dev = default_serial_console (); |
| 129 | |
| 130 | return dev->init (); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 131 | } |
| 132 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 133 | return serial_current->init (); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 134 | } |
| 135 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 136 | void serial_setbrg (void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 137 | { |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 138 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 17b211b | 2004-10-11 23:10:30 +0000 | [diff] [blame] | 139 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 140 | if (!(gd->flags & GD_FLG_RELOC) || !serial_current) { |
| 141 | struct serial_device *dev = default_serial_console (); |
| 142 | |
| 143 | dev->setbrg (); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 147 | serial_current->setbrg (); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 148 | } |
| 149 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 150 | int serial_getc (void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 151 | { |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 152 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 17b211b | 2004-10-11 23:10:30 +0000 | [diff] [blame] | 153 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 154 | if (!(gd->flags & GD_FLG_RELOC) || !serial_current) { |
| 155 | struct serial_device *dev = default_serial_console (); |
| 156 | |
| 157 | return dev->getc (); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 158 | } |
| 159 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 160 | return serial_current->getc (); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 161 | } |
| 162 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 163 | int serial_tstc (void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 164 | { |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 165 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 17b211b | 2004-10-11 23:10:30 +0000 | [diff] [blame] | 166 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 167 | if (!(gd->flags & GD_FLG_RELOC) || !serial_current) { |
| 168 | struct serial_device *dev = default_serial_console (); |
| 169 | |
| 170 | return dev->tstc (); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 171 | } |
| 172 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 173 | return serial_current->tstc (); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 174 | } |
| 175 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 176 | void serial_putc (const char c) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 177 | { |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 178 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 17b211b | 2004-10-11 23:10:30 +0000 | [diff] [blame] | 179 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 180 | if (!(gd->flags & GD_FLG_RELOC) || !serial_current) { |
| 181 | struct serial_device *dev = default_serial_console (); |
| 182 | |
| 183 | dev->putc (c); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 187 | serial_current->putc (c); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 188 | } |
| 189 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 190 | void serial_puts (const char *s) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 191 | { |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 192 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 17b211b | 2004-10-11 23:10:30 +0000 | [diff] [blame] | 193 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 194 | if (!(gd->flags & GD_FLG_RELOC) || !serial_current) { |
| 195 | struct serial_device *dev = default_serial_console (); |
| 196 | |
| 197 | dev->puts (s); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | |
wdenk | 8f08c5b | 2004-10-11 22:43:02 +0000 | [diff] [blame] | 201 | serial_current->puts (s); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | #endif /* CONFIG_SERIAL_MULTI */ |