blob: b2bd7fd8433450b7d983c84f360632cca04ad77d [file] [log] [blame]
Heiko Schocher5e51b482008-10-15 09:39:08 +02001/*
2 * (C) Copyright 2008
3 * Heiko Schocher, DENX Software Engineering, hs@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>
Heiko Schocherd19a6ec2008-11-21 08:29:40 +010025#if defined(CONFIG_MGCOGE)
Heiko Schocher5e51b482008-10-15 09:39:08 +020026#include <mpc8260.h>
Heiko Schocherd19a6ec2008-11-21 08:29:40 +010027#endif
Heiko Schocher5e51b482008-10-15 09:39:08 +020028#include <ioports.h>
29#include <malloc.h>
Heiko Schochere5b6c2e2008-10-15 09:41:00 +020030#include <hush.h>
Heiko Schocherd19a6ec2008-11-21 08:29:40 +010031#include <net.h>
32#include <asm/io.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020033
34#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
35#include <libfdt.h>
36#endif
37
38#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
39#include <i2c.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020040
41extern int i2c_soft_read_pin (void);
42
Heiko Schochere5b6c2e2008-10-15 09:41:00 +020043int ivm_calc_crc (unsigned char *buf, int len)
44{
45 const unsigned short crc_tab[16] = {
46 0x0000, 0xCC01, 0xD801, 0x1400,
47 0xF001, 0x3C00, 0x2800, 0xE401,
48 0xA001, 0x6C00, 0x7800, 0xB401,
49 0x5000, 0x9C01, 0x8801, 0x4400};
50
51 unsigned short crc = 0; /* final result */
52 unsigned short r1 = 0; /* temp */
53 unsigned char byte = 0; /* input buffer */
54 int i;
55
56 /* calculate CRC from array data */
57 for (i = 0; i < len; i++) {
58 byte = buf[i];
59
60 /* lower 4 bits */
61 r1 = crc_tab[crc & 0xF];
62 crc = ((crc) >> 4) & 0x0FFF;
63 crc = crc ^ r1 ^ crc_tab[byte & 0xF];
64
65 /* upper 4 bits */
66 r1 = crc_tab[crc & 0xF];
67 crc = (crc >> 4) & 0x0FFF;
68 crc = crc ^ r1 ^ crc_tab[(byte >> 4) & 0xF];
69 }
70 return crc;
71}
72
73static int ivm_set_value (char *name, char *value)
74{
75 char tempbuf[256];
76
77 if (value != NULL) {
78 sprintf (tempbuf, "%s=%s", name, value);
79 return set_local_var (tempbuf, 0);
80 } else {
81 unset_local_var (name);
82 }
83 return 0;
84}
85
86static int ivm_get_value (unsigned char *buf, int len, char *name, int off,
87 int check)
88{
89 unsigned short val;
90 unsigned char valbuf[30];
91
92 if ((buf[off + 0] != buf[off + 2]) &&
93 (buf[off + 2] != buf[off + 4])) {
94 printf ("%s Error corrupted %s\n", __FUNCTION__, name);
95 val = -1;
96 } else {
97 val = buf[off + 0] + (buf[off + 1] << 8);
98 if ((val == 0) && (check == 1))
99 val = -1;
100 }
101 sprintf ((char *)valbuf, "%x", val);
102 ivm_set_value (name, (char *)valbuf);
103 return val;
104}
105
106#define INVENTORYBLOCKSIZE 0x100
107#define INVENTORYDATAADDRESS 0x21
108#define INVENTORYDATASIZE (INVENTORYBLOCKSIZE - INVENTORYDATAADDRESS - 3)
109
110#define IVM_POS_SHORT_TEXT 0
111#define IVM_POS_MANU_ID 1
112#define IVM_POS_MANU_SERIAL 2
113#define IVM_POS_PART_NUMBER 3
114#define IVM_POS_BUILD_STATE 4
115#define IVM_POS_SUPPLIER_PART_NUMBER 5
116#define IVM_POS_DELIVERY_DATE 6
117#define IVM_POS_SUPPLIER_BUILD_STATE 7
118#define IVM_POS_CUSTOMER_ID 8
119#define IVM_POS_CUSTOMER_PROD_ID 9
120#define IVM_POS_HISTORY 10
121#define IVM_POS_SYMBOL_ONLY 11
122
123static char convert_char (char c)
124{
125 return (c < ' ' || c > '~') ? '.' : c;
126}
127
128static int ivm_findinventorystring (int type,
129 unsigned char* const string,
130 unsigned long maxlen,
131 unsigned char *buf)
132{
133 int xcode = 0;
134 unsigned long cr = 0;
135 unsigned long addr = INVENTORYDATAADDRESS;
136 unsigned long size = 0;
137 unsigned long nr = type;
138 int stop = 0; /* stop on semicolon */
139
140 memset(string, '\0', maxlen);
141 switch (type) {
142 case IVM_POS_SYMBOL_ONLY:
143 nr = 0;
144 stop= 1;
145 break;
146 default:
147 nr = type;
148 stop = 0;
149 }
150
151 /* Look for the requested number of CR. */
152 while ((cr != nr) && (addr < INVENTORYDATASIZE)) {
153 if ((buf[addr] == '\r')) {
154 cr++;
155 }
156 addr++;
157 }
158
159 /* the expected number of CR was found until the end of the IVM
160 * content --> fill string */
161 if (addr < INVENTORYDATASIZE) {
162 /* Copy the IVM string in the corresponding string */
163 for (; (buf[addr] != '\r') &&
164 ((buf[addr] != ';') || (!stop)) &&
165 (size < (maxlen - 1) &&
166 (addr < INVENTORYDATASIZE)); addr++)
167 {
168 size += sprintf((char *)string + size, "%c",
169 convert_char (buf[addr]));
170 }
171
172 /* copy phase is done: check if everything is ok. If not,
173 * the inventory data is most probably corrupted: tell
174 * the world there is a problem! */
175 if (addr == INVENTORYDATASIZE) {
176 xcode = -1;
177 printf ("Error end of string not found\n");
178 } else if ((size >= (maxlen - 1)) &&
179 (buf[addr] != '\r')) {
180 xcode = -1;
181 printf ("string too long till next CR\n");
182 }
183 } else {
184 /* some CR are missing...
185 * the inventory data is most probably corrupted */
186 xcode = -1;
187 printf ("not enough cr found\n");
188 }
189 return xcode;
190}
191
192#define GET_STRING(name, which, len) \
193 if (ivm_findinventorystring (which, valbuf, len, buf) == 0) { \
194 ivm_set_value (name, (char *)valbuf); \
195 }
196
197static int ivm_check_crc (unsigned char *buf, int block)
198{
199 unsigned long crc;
200 unsigned long crceeprom;
201
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200202 crc = ivm_calc_crc (buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2);
203 crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + \
204 buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256);
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200205 if (crc != crceeprom) {
206 printf ("Error CRC Block: %d EEprom: calculated: %lx EEprom: %lx\n",
207 block, crc, crceeprom);
208 return -1;
209 }
210 return 0;
211}
212
213static int ivm_analyze_block2 (unsigned char *buf, int len)
214{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200215 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200216 unsigned long count;
217
218 /* IVM_MacAddress */
219 sprintf ((char *)valbuf, "%02X:%02X:%02X:%02X:%02X:%02X",
220 buf[1],
221 buf[2],
222 buf[3],
223 buf[4],
224 buf[5],
225 buf[6]);
226 ivm_set_value ("IVM_MacAddress", (char *)valbuf);
227 if (getenv ("ethaddr") == NULL)
228 setenv ((char *)"ethaddr", (char *)valbuf);
229 /* IVM_MacCount */
230 count = (buf[10] << 24) +
231 (buf[11] << 16) +
232 (buf[12] << 8) +
233 buf[13];
234 if (count == 0xffffffff)
235 count = 1;
236 sprintf ((char *)valbuf, "%lx", count);
237 ivm_set_value ("IVM_MacCount", (char *)valbuf);
238 return 0;
239}
240
241int ivm_analyze_eeprom (unsigned char *buf, int len)
242{
243 unsigned short val;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200244 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200245 unsigned char *tmp;
246
247 if (ivm_check_crc (buf, 0) != 0)
248 return -1;
249
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200250 ivm_get_value (buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN, "IVM_BoardId", 0, 1);
251 val = ivm_get_value (buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN, "IVM_HWKey", 6, 1);
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200252 if (val != 0xffff) {
253 sprintf ((char *)valbuf, "%x", ((val /100) % 10));
254 ivm_set_value ("IVM_HWVariant", (char *)valbuf);
255 sprintf ((char *)valbuf, "%x", (val % 100));
256 ivm_set_value ("IVM_HWVersion", (char *)valbuf);
257 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200258 ivm_get_value (buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN, "IVM_Functions", 12, 0);
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200259
260 GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
261 GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
262 tmp = (unsigned char *) getenv("IVM_DeviceName");
263 if (tmp) {
264 int len = strlen ((char *)tmp);
265 int i = 0;
266
267 while (i < len) {
268 if (tmp[i] == ';') {
269 ivm_set_value ("IVM_ShortText", (char *)&tmp[i + 1]);
270 break;
271 }
272 i++;
273 }
274 if (i >= len)
275 ivm_set_value ("IVM_ShortText", NULL);
276 } else {
277 ivm_set_value ("IVM_ShortText", NULL);
278 }
279 GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
280 GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
281 GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
282 GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
283 GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
284 GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
285 GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
286 GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
287 GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
288
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200289 if (ivm_check_crc (&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200290 return -2;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200291 ivm_analyze_block2 (&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200292
293 return 0;
294}
295
296int ivm_read_eeprom (void)
297{
Heiko Schocher643e7c02009-03-12 07:37:34 +0100298#if defined(CONFIG_I2C_MUX)
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200299 I2C_MUX_DEVICE *dev = NULL;
Heiko Schocher643e7c02009-03-12 07:37:34 +0100300#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200301 uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200302 uchar *buf;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200303 unsigned dev_addr = CONFIG_SYS_IVM_EEPROM_ADR;
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200304
Heiko Schocher643e7c02009-03-12 07:37:34 +0100305#if defined(CONFIG_I2C_MUX)
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200306 /* First init the Bus, select the Bus */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200307#if defined(CONFIG_SYS_I2C_IVM_BUS)
308 dev = i2c_mux_ident_muxstring ((uchar *)CONFIG_SYS_I2C_IVM_BUS);
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200309#else
310 buf = (unsigned char *) getenv ("EEprom_ivm");
311 if (buf != NULL)
312 dev = i2c_mux_ident_muxstring (buf);
313#endif
314 if (dev == NULL) {
315 printf ("Error couldnt add Bus for IVM\n");
316 return -1;
317 }
318 i2c_set_bus_num (dev->busid);
Heiko Schocher643e7c02009-03-12 07:37:34 +0100319#endif
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200320
321 buf = (unsigned char *) getenv ("EEprom_ivm_addr");
322 if (buf != NULL)
323 dev_addr = simple_strtoul ((char *)buf, NULL, 16);
324
Heiko Schocher46743182009-02-24 11:30:34 +0100325 if (i2c_read(dev_addr, 0, 1, i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN) != 0) {
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200326 printf ("Error reading EEprom\n");
327 return -2;
328 }
329
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200330 return ivm_analyze_eeprom (i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
Heiko Schochere5b6c2e2008-10-15 09:41:00 +0200331}
332
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200333#if defined(CONFIG_SYS_I2C_INIT_BOARD)
Heiko Schocher5e51b482008-10-15 09:39:08 +0200334#define DELAY_ABORT_SEQ 62
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200335#define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
Heiko Schocher5e51b482008-10-15 09:39:08 +0200336
337#if defined(CONFIG_MGCOGE)
338#define SDA_MASK 0x00010000
339#define SCL_MASK 0x00020000
340static void set_pin (int state, unsigned long mask)
341{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200342 volatile ioport_t *iop = ioport_addr ((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocher5e51b482008-10-15 09:39:08 +0200343
344 if (state)
345 iop->pdat |= (mask);
346 else
347 iop->pdat &= ~(mask);
348
349 iop->pdir |= (mask);
350}
351
352static int get_pin (unsigned long mask)
353{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200354 volatile ioport_t *iop = ioport_addr ((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocher5e51b482008-10-15 09:39:08 +0200355
356 iop->pdir &= ~(mask);
357 return (0 != (iop->pdat & (mask)));
358}
359
360static void set_sda (int state)
361{
362 set_pin (state, SDA_MASK);
363}
364
365static void set_scl (int state)
366{
367 set_pin (state, SCL_MASK);
368}
369
370static int get_sda (void)
371{
372 return get_pin (SDA_MASK);
373}
374
375static int get_scl (void)
376{
377 return get_pin (SCL_MASK);
378}
379
380#if defined(CONFIG_HARD_I2C)
381static void setports (int gpio)
382{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200383 volatile ioport_t *iop = ioport_addr ((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocher5e51b482008-10-15 09:39:08 +0200384
385 if (gpio) {
386 iop->ppar &= ~(SDA_MASK | SCL_MASK);
387 iop->podr &= ~(SDA_MASK | SCL_MASK);
388 } else {
389 iop->ppar |= (SDA_MASK | SCL_MASK);
390 iop->pdir &= ~(SDA_MASK | SCL_MASK);
391 iop->podr |= (SDA_MASK | SCL_MASK);
392 }
393}
394#endif
395#endif
396
Heiko Schocher4bb15632009-03-12 07:37:28 +0100397#if defined(CONFIG_KM8XX)
Heiko Schocher5e51b482008-10-15 09:39:08 +0200398static void set_sda (int state)
399{
400 I2C_SDA(state);
401}
402
403static void set_scl (int state)
404{
405 I2C_SCL(state);
406}
407
408static int get_sda (void)
409{
Heiko Schocher7f3afd62008-10-17 13:52:51 +0200410 return I2C_READ;
Heiko Schocher5e51b482008-10-15 09:39:08 +0200411}
412
413static int get_scl (void)
414{
415 int val;
416
417 *(unsigned short *)(I2C_BASE_DIR) &= ~SCL_CONF;
418 udelay (1);
419 val = *(unsigned char *)(I2C_BASE_PORT);
420
421 return ((val & SCL_BIT) == SCL_BIT);
422}
423
424#endif
425
426static void writeStartSeq (void)
427{
428 set_sda (1);
429 udelay (DELAY_HALF_PERIOD);
430 set_scl (1);
431 udelay (DELAY_HALF_PERIOD);
432 set_sda (0);
433 udelay (DELAY_HALF_PERIOD);
434 set_scl (0);
435 udelay (DELAY_HALF_PERIOD);
436}
437
438/* I2C is a synchronous protocol and resets of the processor in the middle
439 of an access can block the I2C Bus until a powerdown of the full unit is
440 done. This function toggles the SCL until the SCL and SCA line are
441 released, but max. 16 times, after this a I2C start-sequence is sent.
442 This I2C Deblocking mechanism was developed by Keymile in association
443 with Anatech and Atmel in 1998.
444 */
445static int i2c_make_abort (void)
446{
447 int scl_state = 0;
448 int sda_state = 0;
449 int i = 0;
450 int ret = 0;
451
452 if (!get_sda ()) {
453 ret = -1;
454 while (i < 16) {
455 i++;
456 set_scl (0);
457 udelay (DELAY_ABORT_SEQ);
458 set_scl (1);
459 udelay (DELAY_ABORT_SEQ);
460 scl_state = get_scl ();
461 sda_state = get_sda ();
462 if (scl_state && sda_state) {
463 ret = 0;
464 break;
465 }
466 }
467 }
468 if (ret == 0) {
469 for (i =0; i < 5; i++) {
470 writeStartSeq ();
471 }
472 }
473 get_sda ();
474 return ret;
475}
476
477/**
478 * i2c_init_board - reset i2c bus. When the board is powercycled during a
479 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
480 */
481void i2c_init_board(void)
482{
483#if defined(CONFIG_HARD_I2C)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200484 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
Heiko Schocher5e51b482008-10-15 09:39:08 +0200485 volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
486
487 /* disable I2C controller first, otherwhise it thinks we want to */
488 /* talk to the slave port... */
489 i2c->i2c_i2mod &= ~0x01;
490
491 /* Set the PortPins to GPIO */
492 setports (1);
493#endif
494
495 /* Now run the AbortSequence() */
496 i2c_make_abort ();
497
498#if defined(CONFIG_HARD_I2C)
499 /* Set the PortPins back to use for I2C */
500 setports (0);
501#endif
502}
503#endif
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100504#endif
Heiko Schocher1eedd3f2008-10-17 16:11:52 +0200505
506#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
507int fdt_set_node_and_value (void *blob,
508 char *nodename,
509 char *regname,
510 void *var,
511 int size)
512{
513 int ret = 0;
514 int nodeoffset = 0;
515
516 nodeoffset = fdt_path_offset (blob, nodename);
517 if (nodeoffset >= 0) {
518 ret = fdt_setprop (blob, nodeoffset, regname, var,
519 size);
520 if (ret < 0)
521 printf("ft_blob_update(): cannot set %s/%s "
522 "property err:%s\n", nodename, regname,
523 fdt_strerror (ret));
524 } else {
525 printf("ft_blob_update(): cannot find %s node "
526 "err:%s\n", nodename, fdt_strerror (nodeoffset));
527 }
528 return ret;
529}
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100530#endif
531
532int ethernet_present (void)
533{
534 return (in_8((u8 *)CONFIG_SYS_PIGGY_BASE + CONFIG_SYS_SLOT_ID_OFF) & 0x80);
535}
536
537int board_eth_init (bd_t *bis)
538{
539#ifdef CONFIG_KEYMILE_HDLC_ENET
540 (void)keymile_hdlc_enet_initialize (bis);
Heiko Schocher1eedd3f2008-10-17 16:11:52 +0200541#endif
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100542 if (ethernet_present ()) {
543 return -1;
544 }
545 return 0;
546}