wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * Robin Getz rgetz@blacfin.uclinux.org |
| 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 | * Heavily borrowed from the following peoples GPL'ed software: |
| 24 | * - Wolfgang Denk, DENX Software Engineering, wd@denx.de |
| 25 | * Das U-boot |
| 26 | * - Ladislav Michl ladis@linux-mips.org |
| 27 | * A rejected patch on the U-Boot mailing list |
| 28 | */ |
| 29 | |
| 30 | #include <common.h> |
| 31 | #include <exports.h> |
Jean-Christophe PLAGNIOL-VILLARD | ed0ea1f | 2007-11-21 21:19:24 +0100 | [diff] [blame] | 32 | #include "../drivers/net/smc91111.h" |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 33 | |
Mike Frysinger | 034bcce | 2009-03-22 22:18:01 -0400 | [diff] [blame] | 34 | #ifndef SMC91111_EEPROM_INIT |
| 35 | # define SMC91111_EEPROM_INIT() |
Mike Frysinger | ecb196d | 2008-02-04 19:26:56 -0500 | [diff] [blame] | 36 | #endif |
| 37 | |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 38 | #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE |
Mike Frysinger | 034bcce | 2009-03-22 22:18:01 -0400 | [diff] [blame] | 39 | #define EEPROM 0x1 |
| 40 | #define MAC 0x2 |
| 41 | #define UNKNOWN 0x4 |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 42 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 43 | void dump_reg (struct eth_device *dev); |
| 44 | void dump_eeprom (struct eth_device *dev); |
| 45 | int write_eeprom_reg (struct eth_device *dev, int value, int reg); |
| 46 | void copy_from_eeprom (struct eth_device *dev); |
| 47 | void print_MAC (struct eth_device *dev); |
| 48 | int read_eeprom_reg (struct eth_device *dev, int reg); |
| 49 | void print_macaddr (struct eth_device *dev); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 50 | |
| 51 | int smc91111_eeprom (int argc, char *argv[]) |
| 52 | { |
| 53 | int c, i, j, done, line, reg, value, start, what; |
| 54 | char input[50]; |
| 55 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 56 | struct eth_device dev = { |
| 57 | .iobase = CONFIG_SMC91111_BASE |
| 58 | }; |
| 59 | |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 60 | /* Print the ABI version */ |
| 61 | app_startup (argv); |
| 62 | if (XF_VERSION != (int) get_version ()) { |
| 63 | printf ("Expects ABI version %d\n", XF_VERSION); |
| 64 | printf ("Actual U-Boot ABI version %d\n", |
| 65 | (int) get_version ()); |
| 66 | printf ("Can't run\n\n"); |
| 67 | return (0); |
| 68 | } |
| 69 | |
Mike Frysinger | 034bcce | 2009-03-22 22:18:01 -0400 | [diff] [blame] | 70 | SMC91111_EEPROM_INIT(); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 71 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 72 | if ((SMC_inw (&dev, BANK_SELECT) & 0xFF00) != 0x3300) { |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 73 | printf ("Can't find SMSC91111\n"); |
| 74 | return (0); |
| 75 | } |
| 76 | |
| 77 | done = 0; |
| 78 | what = UNKNOWN; |
| 79 | printf ("\n"); |
| 80 | while (!done) { |
| 81 | /* print the prompt */ |
| 82 | printf ("SMC91111> "); |
| 83 | line = 0; |
| 84 | i = 0; |
| 85 | start = 1; |
| 86 | while (!line) { |
| 87 | /* Wait for a keystroke */ |
| 88 | while (!tstc ()); |
| 89 | |
| 90 | c = getc (); |
| 91 | /* Make Uppercase */ |
| 92 | if (c >= 'Z') |
| 93 | c -= ('a' - 'A'); |
| 94 | /* printf(" |%02x| ",c); */ |
| 95 | |
| 96 | switch (c) { |
| 97 | case '\r': /* Enter */ |
| 98 | case '\n': |
| 99 | input[i] = 0; |
| 100 | puts ("\r\n"); |
| 101 | line = 1; |
| 102 | break; |
| 103 | case '\0': /* nul */ |
| 104 | continue; |
| 105 | |
| 106 | case 0x03: /* ^C - break */ |
| 107 | input[0] = 0; |
| 108 | i = 0; |
| 109 | line = 1; |
| 110 | done = 1; |
| 111 | break; |
| 112 | |
| 113 | case 0x5F: |
| 114 | case 0x08: /* ^H - backspace */ |
| 115 | case 0x7F: /* DEL - backspace */ |
| 116 | if (i > 0) { |
| 117 | puts ("\b \b"); |
| 118 | i--; |
| 119 | } |
| 120 | break; |
| 121 | default: |
| 122 | if (start) { |
| 123 | if ((c == 'W') || (c == 'D') |
| 124 | || (c == 'M') || (c == 'C') |
| 125 | || (c == 'P')) { |
| 126 | putc (c); |
| 127 | input[i] = c; |
| 128 | if (i <= 45) |
| 129 | i++; |
| 130 | start = 0; |
| 131 | } |
| 132 | } else { |
| 133 | if ((c >= '0' && c <= '9') |
| 134 | || (c >= 'A' && c <= 'F') |
| 135 | || (c == 'E') || (c == 'M') |
| 136 | || (c == ' ')) { |
| 137 | putc (c); |
| 138 | input[i] = c; |
| 139 | if (i <= 45) |
| 140 | i++; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | for (; i < 49; i++) |
| 149 | input[i] = 0; |
| 150 | |
| 151 | switch (input[0]) { |
| 152 | case ('W'): |
| 153 | /* Line should be w reg value */ |
| 154 | i = 0; |
| 155 | reg = 0; |
| 156 | value = 0; |
| 157 | /* Skip to the next space or end) */ |
| 158 | while ((input[i] != ' ') && (input[i] != 0)) |
| 159 | i++; |
| 160 | |
| 161 | if (input[i] != 0) |
| 162 | i++; |
| 163 | |
| 164 | /* Are we writing to EEPROM or MAC */ |
| 165 | switch (input[i]) { |
| 166 | case ('E'): |
| 167 | what = EEPROM; |
| 168 | break; |
| 169 | case ('M'): |
| 170 | what = MAC; |
| 171 | break; |
| 172 | default: |
| 173 | what = UNKNOWN; |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | /* skip to the next space or end */ |
| 178 | while ((input[i] != ' ') && (input[i] != 0)) |
| 179 | i++; |
| 180 | if (input[i] != 0) |
| 181 | i++; |
| 182 | |
| 183 | /* Find register to write into */ |
| 184 | j = 0; |
| 185 | while ((input[i] != ' ') && (input[i] != 0)) { |
| 186 | j = input[i] - 0x30; |
| 187 | if (j >= 0xA) { |
| 188 | j -= 0x07; |
| 189 | } |
| 190 | reg = (reg * 0x10) + j; |
| 191 | i++; |
| 192 | } |
| 193 | |
| 194 | while ((input[i] != ' ') && (input[i] != 0)) |
| 195 | i++; |
| 196 | |
| 197 | if (input[i] != 0) |
| 198 | i++; |
| 199 | else |
| 200 | what = UNKNOWN; |
| 201 | |
| 202 | /* Get the value to write */ |
| 203 | j = 0; |
| 204 | while ((input[i] != ' ') && (input[i] != 0)) { |
| 205 | j = input[i] - 0x30; |
| 206 | if (j >= 0xA) { |
| 207 | j -= 0x07; |
| 208 | } |
| 209 | value = (value * 0x10) + j; |
| 210 | i++; |
| 211 | } |
| 212 | |
| 213 | switch (what) { |
| 214 | case 1: |
Wolfgang Denk | 46d2b52 | 2006-03-12 02:10:00 +0100 | [diff] [blame] | 215 | printf ("Writing EEPROM register %02x with %04x\n", reg, value); |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 216 | write_eeprom_reg (&dev, value, reg); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 217 | break; |
| 218 | case 2: |
Wolfgang Denk | 46d2b52 | 2006-03-12 02:10:00 +0100 | [diff] [blame] | 219 | printf ("Writing MAC register bank %i, reg %02x with %04x\n", reg >> 4, reg & 0xE, value); |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 220 | SMC_SELECT_BANK (&dev, reg >> 4); |
| 221 | SMC_outw (&dev, value, reg & 0xE); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 222 | break; |
| 223 | default: |
| 224 | printf ("Wrong\n"); |
| 225 | break; |
| 226 | } |
| 227 | break; |
| 228 | case ('D'): |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 229 | dump_eeprom (&dev); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 230 | break; |
| 231 | case ('M'): |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 232 | dump_reg (&dev); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 233 | break; |
| 234 | case ('C'): |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 235 | copy_from_eeprom (&dev); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 236 | break; |
| 237 | case ('P'): |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 238 | print_macaddr (&dev); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 239 | break; |
| 240 | default: |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | } |
| 245 | |
| 246 | return (0); |
| 247 | } |
| 248 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 249 | void copy_from_eeprom (struct eth_device *dev) |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 250 | { |
| 251 | int i; |
| 252 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 253 | SMC_SELECT_BANK (dev, 1); |
| 254 | SMC_outw (dev, (SMC_inw (dev, CTL_REG) & !CTL_EEPROM_SELECT) | |
| 255 | CTL_RELOAD, CTL_REG); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 256 | i = 100; |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 257 | while ((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --i) |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 258 | udelay (100); |
| 259 | if (i == 0) { |
| 260 | printf ("Timeout Refreshing EEPROM registers\n"); |
| 261 | } else { |
| 262 | printf ("EEPROM contents copied to MAC\n"); |
| 263 | } |
| 264 | |
| 265 | } |
| 266 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 267 | void print_macaddr (struct eth_device *dev) |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 268 | { |
| 269 | int i, j, k, mac[6]; |
| 270 | |
| 271 | printf ("Current MAC Address in SMSC91111 "); |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 272 | SMC_SELECT_BANK (dev, 1); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 273 | for (i = 0; i < 5; i++) { |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 274 | printf ("%02x:", SMC_inb (dev, ADDR0_REG + i)); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 277 | printf ("%02x\n", SMC_inb (dev, ADDR0_REG + 5)); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 278 | |
| 279 | i = 0; |
| 280 | for (j = 0x20; j < 0x23; j++) { |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 281 | k = read_eeprom_reg (dev, j); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 282 | mac[i] = k & 0xFF; |
| 283 | i++; |
| 284 | mac[i] = k >> 8; |
| 285 | i++; |
| 286 | } |
| 287 | |
| 288 | printf ("Current MAC Address in EEPROM "); |
| 289 | for (i = 0; i < 5; i++) |
| 290 | printf ("%02x:", mac[i]); |
| 291 | printf ("%02x\n", mac[5]); |
| 292 | |
| 293 | } |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 294 | void dump_eeprom (struct eth_device *dev) |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 295 | { |
| 296 | int j, k; |
| 297 | |
| 298 | printf ("IOS2-0 "); |
| 299 | for (j = 0; j < 8; j++) { |
| 300 | printf ("%03x ", j); |
| 301 | } |
| 302 | printf ("\n"); |
| 303 | |
| 304 | for (k = 0; k < 4; k++) { |
| 305 | if (k == 0) |
| 306 | printf ("CONFIG "); |
| 307 | if (k == 1) |
| 308 | printf ("BASE "); |
| 309 | if ((k == 2) || (k == 3)) |
| 310 | printf (" "); |
| 311 | for (j = 0; j < 0x20; j += 4) { |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 312 | printf ("%02x:%04x ", j + k, |
| 313 | read_eeprom_reg (dev, j + k)); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 314 | } |
| 315 | printf ("\n"); |
| 316 | } |
| 317 | |
| 318 | for (j = 0x20; j < 0x40; j++) { |
| 319 | if ((j & 0x07) == 0) |
| 320 | printf ("\n"); |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 321 | printf ("%02x:%04x ", j, read_eeprom_reg (dev, j)); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 322 | } |
| 323 | printf ("\n"); |
| 324 | |
| 325 | } |
| 326 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 327 | int read_eeprom_reg (struct eth_device *dev, int reg) |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 328 | { |
| 329 | int timeout; |
| 330 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 331 | SMC_SELECT_BANK (dev, 2); |
| 332 | SMC_outw (dev, reg, PTR_REG); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 333 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 334 | SMC_SELECT_BANK (dev, 1); |
| 335 | SMC_outw (dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT | |
| 336 | CTL_RELOAD, CTL_REG); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 337 | timeout = 100; |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 338 | while ((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --timeout) |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 339 | udelay (100); |
| 340 | if (timeout == 0) { |
| 341 | printf ("Timeout Reading EEPROM register %02x\n", reg); |
| 342 | return 0; |
| 343 | } |
| 344 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 345 | return SMC_inw (dev, GP_REG); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 346 | |
| 347 | } |
| 348 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 349 | int write_eeprom_reg (struct eth_device *dev, int value, int reg) |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 350 | { |
| 351 | int timeout; |
| 352 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 353 | SMC_SELECT_BANK (dev, 2); |
| 354 | SMC_outw (dev, reg, PTR_REG); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 355 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 356 | SMC_SELECT_BANK (dev, 1); |
| 357 | SMC_outw (dev, value, GP_REG); |
| 358 | SMC_outw (dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT | |
| 359 | CTL_STORE, CTL_REG); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 360 | timeout = 100; |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 361 | while ((SMC_inw (dev, CTL_REG) & CTL_STORE) && --timeout) |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 362 | udelay (100); |
| 363 | if (timeout == 0) { |
| 364 | printf ("Timeout Writing EEPROM register %02x\n", reg); |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | return 1; |
| 369 | |
| 370 | } |
| 371 | |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 372 | void dump_reg (struct eth_device *dev) |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 373 | { |
| 374 | int i, j; |
| 375 | |
| 376 | printf (" "); |
| 377 | for (j = 0; j < 4; j++) { |
| 378 | printf ("Bank%i ", j); |
| 379 | } |
| 380 | printf ("\n"); |
| 381 | for (i = 0; i < 0xF; i += 2) { |
| 382 | printf ("%02x ", i); |
| 383 | for (j = 0; j < 4; j++) { |
Ben Warren | 0fd6aae | 2009-10-04 22:37:03 -0700 | [diff] [blame] | 384 | SMC_SELECT_BANK (dev, j); |
| 385 | printf ("%04x ", SMC_inw (dev, i)); |
wdenk | c86cdb9 | 2005-01-12 00:38:03 +0000 | [diff] [blame] | 386 | } |
| 387 | printf ("\n"); |
| 388 | } |
| 389 | } |