blob: fff312346460e375fc4cea64483c7138a508e4a8 [file] [log] [blame]
Mike Frysingerad2d1632008-12-22 02:56:07 -05001/*
2 * smc911x_eeprom.c - EEPROM interface to SMC911x parts.
3 * Only tested on SMSC9118 though ...
4 *
Mike Frysingerc44efcf2009-11-12 22:26:02 -05005 * Copyright 2004-2009 Analog Devices Inc.
Mike Frysingerad2d1632008-12-22 02:56:07 -05006 *
7 * Licensed under the GPL-2 or later.
8 *
9 * Based on smc91111_eeprom.c which:
10 * Heavily borrowed from the following peoples GPL'ed software:
11 * - Wolfgang Denk, DENX Software Engineering, wd@denx.de
12 * Das U-boot
13 * - Ladislav Michl ladis@linux-mips.org
14 * A rejected patch on the U-Boot mailing list
15 */
16
17#include <common.h>
18#include <exports.h>
19
Mike Frysingerc44efcf2009-11-12 22:26:02 -050020/* the smc911x.h gets base addr through eth_device' iobase */
21struct eth_device {
22 const char *name;
23 unsigned long iobase;
24 void *priv;
25};
Mike Frysingerad2d1632008-12-22 02:56:07 -050026#include "../drivers/net/smc911x.h"
27
28/**
29 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
30 */
31static int smsc_ctrlc(void)
32{
33 return (tstc() && getc() == 0x03);
34}
35
36/**
37 * usage - dump usage information
38 */
39static void usage(void)
40{
41 puts(
42 "MAC/EEPROM Commands:\n"
43 " P : Print the MAC addresses\n"
44 " D : Dump the EEPROM contents\n"
45 " M : Dump the MAC contents\n"
46 " C : Copy the MAC address from the EEPROM to the MAC\n"
47 " W : Write a register in the EEPROM or in the MAC\n"
48 " Q : Quit\n"
49 "\n"
50 "Some commands take arguments:\n"
51 " W <E|M> <register> <value>\n"
52 " E: EEPROM M: MAC\n"
53 );
54}
55
56/**
57 * dump_regs - dump the MAC registers
58 *
59 * Registers 0x00 - 0x50 are FIFOs. The 0x50+ are the control registers
60 * and they're all 32bits long. 0xB8+ are reserved, so don't bother.
61 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -050062static void dump_regs(struct eth_device *dev)
Mike Frysingerad2d1632008-12-22 02:56:07 -050063{
64 u8 i, j = 0;
65 for (i = 0x50; i < 0xB8; i += sizeof(u32))
Mike Frysinger069f4362009-02-25 17:29:40 -050066 printf("%02x: 0x%08x %c", i,
Mike Frysingerc44efcf2009-11-12 22:26:02 -050067 smc911x_reg_read(dev, i),
Mike Frysingerad2d1632008-12-22 02:56:07 -050068 (j++ % 2 ? '\n' : ' '));
69}
70
71/**
72 * do_eeprom_cmd - handle eeprom communication
73 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -050074static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
Mike Frysingerad2d1632008-12-22 02:56:07 -050075{
Mike Frysingerc44efcf2009-11-12 22:26:02 -050076 if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
Mike Frysingerad2d1632008-12-22 02:56:07 -050077 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
Mike Frysingerc44efcf2009-11-12 22:26:02 -050078 smc911x_reg_read(dev, E2P_CMD));
Mike Frysingerad2d1632008-12-22 02:56:07 -050079 return -1;
80 }
81
Mike Frysingerc44efcf2009-11-12 22:26:02 -050082 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -050083
Mike Frysingerc44efcf2009-11-12 22:26:02 -050084 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
Mike Frysingerad2d1632008-12-22 02:56:07 -050085 if (smsc_ctrlc()) {
86 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
Mike Frysingerc44efcf2009-11-12 22:26:02 -050087 smc911x_reg_read(dev, E2P_CMD));
Mike Frysingerad2d1632008-12-22 02:56:07 -050088 return -1;
89 }
90
91 return 0;
92}
93
94/**
95 * read_eeprom_reg - read specified register in EEPROM
96 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -050097static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
Mike Frysingerad2d1632008-12-22 02:56:07 -050098{
Mike Frysingerc44efcf2009-11-12 22:26:02 -050099 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
100 return (ret ? : smc911x_reg_read(dev, E2P_DATA));
Mike Frysingerad2d1632008-12-22 02:56:07 -0500101}
102
103/**
104 * write_eeprom_reg - write specified value into specified register in EEPROM
105 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500106static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500107{
108 int ret;
109
110 /* enable erasing/writing */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500111 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500112 if (ret)
113 goto done;
114
115 /* erase the eeprom reg */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500116 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500117 if (ret)
118 goto done;
119
120 /* write the eeprom reg */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500121 smc911x_reg_write(dev, E2P_DATA, value);
122 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500123 if (ret)
124 goto done;
125
126 /* disable erasing/writing */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500127 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500128
129 done:
130 return ret;
131}
132
133/**
134 * skip_space - find first non-whitespace in given pointer
135 */
136static char *skip_space(char *buf)
137{
138 while (buf[0] == ' ' || buf[0] == '\t')
139 ++buf;
140 return buf;
141}
142
143/**
144 * write_stuff - handle writing of MAC registers / eeprom
145 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500146static void write_stuff(struct eth_device *dev, char *line)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500147{
148 char dest;
149 char *endp;
150 u8 reg;
151 u32 value;
152
153 /* Skip over the "W " part of the command */
154 line = skip_space(line + 1);
155
156 /* Figure out destination */
157 switch (line[0]) {
158 case 'E':
159 case 'M':
160 dest = line[0];
161 break;
162 default:
163 invalid_usage:
164 printf("ERROR: Invalid write usage\n");
165 usage();
166 return;
167 }
168
169 /* Get the register to write */
170 line = skip_space(line + 1);
171 reg = simple_strtoul(line, &endp, 16);
172 if (line == endp)
173 goto invalid_usage;
174
175 /* Get the value to write */
176 line = skip_space(endp);
177 value = simple_strtoul(line, &endp, 16);
178 if (line == endp)
179 goto invalid_usage;
180
181 /* Check for trailing cruft */
182 line = skip_space(endp);
183 if (line[0])
184 goto invalid_usage;
185
186 /* Finally, execute the command */
187 if (dest == 'E') {
188 printf("Writing EEPROM register %02x with %02x\n", reg, value);
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500189 write_eeprom_reg(dev, value, reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500190 } else {
191 printf("Writing MAC register %02x with %08x\n", reg, value);
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500192 smc911x_reg_write(dev, reg, value);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500193 }
194}
195
196/**
197 * copy_from_eeprom - copy MAC address in eeprom to address registers
198 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500199static void copy_from_eeprom(struct eth_device *dev)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500200{
201 ulong addrl =
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500202 read_eeprom_reg(dev, 0x01) |
203 read_eeprom_reg(dev, 0x02) << 8 |
204 read_eeprom_reg(dev, 0x03) << 16 |
205 read_eeprom_reg(dev, 0x04) << 24;
Mike Frysingerad2d1632008-12-22 02:56:07 -0500206 ulong addrh =
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500207 read_eeprom_reg(dev, 0x05) |
208 read_eeprom_reg(dev, 0x06) << 8;
209 smc911x_set_mac_csr(dev, ADDRL, addrl);
210 smc911x_set_mac_csr(dev, ADDRH, addrh);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500211 puts("EEPROM contents copied to MAC\n");
212}
213
214/**
215 * print_macaddr - print MAC address registers and MAC address in eeprom
216 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500217static void print_macaddr(struct eth_device *dev)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500218{
219 puts("Current MAC Address in MAC: ");
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500220 ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
221 ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500222 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
223 (u8)(addrl), (u8)(addrl >> 8), (u8)(addrl >> 16),
224 (u8)(addrl >> 24), (u8)(addrh), (u8)(addrh >> 8));
225
226 puts("Current MAC Address in EEPROM: ");
227 int i;
228 for (i = 1; i < 6; ++i)
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500229 printf("%02x:", read_eeprom_reg(dev, i));
230 printf("%02x\n", read_eeprom_reg(dev, i));
Mike Frysingerad2d1632008-12-22 02:56:07 -0500231}
232
233/**
234 * dump_eeprom - dump the whole content of the EEPROM
235 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500236static void dump_eeprom(struct eth_device *dev)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500237{
238 int i;
239 puts("EEPROM:\n");
240 for (i = 0; i < 7; ++i)
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500241 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
Mike Frysingerad2d1632008-12-22 02:56:07 -0500242}
243
244/**
245 * smc911x_init - get the MAC/EEPROM up and ready for use
246 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500247static int smc911x_init(struct eth_device *dev)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500248{
249 /* See if there is anything there */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500250 if (!smc911x_detect_chip(dev))
Mike Frysingerad2d1632008-12-22 02:56:07 -0500251 return 1;
252
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500253 smc911x_reset(dev);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500254
255 /* Make sure we set EEDIO/EECLK to the EEPROM */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500256 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
257 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500258 if (smsc_ctrlc()) {
Mike Frysinger069f4362009-02-25 17:29:40 -0500259 printf("init: timeout (E2P_CMD = 0x%08x)\n",
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500260 smc911x_reg_read(dev, E2P_CMD));
Mike Frysingerad2d1632008-12-22 02:56:07 -0500261 return 1;
262 }
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500263 smc911x_reg_write(dev, GPIO_CFG,
264 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500265 }
266
267 return 0;
268}
269
270/**
271 * getline - consume a line of input and handle some escape sequences
272 */
273static char *getline(void)
274{
275 static char buffer[100];
276 char c;
277 size_t i;
278
279 i = 0;
280 while (1) {
281 buffer[i] = '\0';
282 while (!tstc())
283 continue;
284
285 c = getc();
286 /* Convert to uppercase */
287 if (c >= 'a' && c <= 'z')
288 c -= ('a' - 'A');
289
290 switch (c) {
291 case '\r': /* Enter/Return key */
292 case '\n':
293 puts("\n");
294 return buffer;
295
296 case 0x03: /* ^C - break */
297 return NULL;
298
299 case 0x5F:
300 case 0x08: /* ^H - backspace */
301 case 0x7F: /* DEL - backspace */
302 if (i) {
303 puts("\b \b");
304 i--;
305 }
306 break;
307
308 default:
309 /* Ignore control characters */
310 if (c < 0x20)
311 break;
312 /* Queue up all other characters */
313 buffer[i++] = c;
314 printf("%c", c);
315 break;
316 }
317 }
318}
319
320/**
321 * smc911x_eeprom - our application's main() function
322 */
323int smc911x_eeprom(int argc, char *argv[])
324{
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500325 /* Avoid initializing on stack as gcc likes to call memset() */
326 struct eth_device dev;
327 dev.name = __func__;
328 dev.iobase = CONFIG_SMC911X_BASE;
329
Mike Frysingerad2d1632008-12-22 02:56:07 -0500330 /* Print the ABI version */
331 app_startup(argv);
332 if (XF_VERSION != get_version()) {
333 printf("Expects ABI version %d\n", XF_VERSION);
334 printf("Actual U-Boot ABI version %lu\n", get_version());
335 printf("Can't run\n\n");
336 return 1;
337 }
338
339 /* Initialize the MAC/EEPROM somewhat */
340 puts("\n");
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500341 if (smc911x_init(&dev))
Mike Frysingerad2d1632008-12-22 02:56:07 -0500342 return 1;
343
344 /* Dump helpful usage information */
345 puts("\n");
346 usage();
347 puts("\n");
348
349 while (1) {
350 char *line;
351
352 /* Send the prompt and wait for a line */
353 puts("eeprom> ");
354 line = getline();
355
356 /* Got a ctrl+c */
357 if (!line)
358 return 0;
359
360 /* Eat leading space */
361 line = skip_space(line);
362
363 /* Empty line, try again */
364 if (!line[0])
365 continue;
366
367 /* Only accept 1 letter commands */
368 if (line[0] && line[1] && line[1] != ' ' && line[1] != '\t')
369 goto unknown_cmd;
370
371 /* Now parse the command */
372 switch (line[0]) {
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500373 case 'W': write_stuff(&dev, line); break;
374 case 'D': dump_eeprom(&dev); break;
375 case 'M': dump_regs(&dev); break;
376 case 'C': copy_from_eeprom(&dev); break;
377 case 'P': print_macaddr(&dev); break;
Mike Frysingerad2d1632008-12-22 02:56:07 -0500378 unknown_cmd:
379 default: puts("ERROR: Unknown command!\n\n");
380 case '?':
381 case 'H': usage(); break;
382 case 'Q': return 0;
383 }
384 }
385}