blob: 8564b9c757565dadfd30c3542c7420d7da890e8a [file] [log] [blame]
Heiko Schocherc2485362008-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 Schocheraf895e42011-02-22 08:58:19 +010025#if defined(CONFIG_MGCOGE) || defined(CONFIG_MGCOGE2NE)
Heiko Schocherc2485362008-10-15 09:39:08 +020026#include <mpc8260.h>
Heiko Schocher210c8c02008-11-21 08:29:40 +010027#endif
Heiko Schocherc2485362008-10-15 09:39:08 +020028#include <ioports.h>
29#include <malloc.h>
Heiko Schocher8f64da72008-10-15 09:41:00 +020030#include <hush.h>
Heiko Schocher210c8c02008-11-21 08:29:40 +010031#include <net.h>
Heiko Schocher62ddcf02010-02-18 08:08:25 +010032#include <netdev.h>
Heiko Schocher210c8c02008-11-21 08:29:40 +010033#include <asm/io.h>
Heiko Schocherc2485362008-10-15 09:39:08 +020034
35#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
36#include <libfdt.h>
37#endif
38
Heiko Schocher67fa8c22010-02-22 16:43:02 +053039#include "../common/common.h"
Heiko Schocherc2485362008-10-15 09:39:08 +020040#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
41#include <i2c.h>
Heiko Schocherc2485362008-10-15 09:39:08 +020042
Heiko Schocherb11f53f2011-03-15 16:52:29 +010043int ivm_calc_crc(unsigned char *buf, int len)
Heiko Schocher8f64da72008-10-15 09:41:00 +020044{
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
Heiko Schocherb11f53f2011-03-15 16:52:29 +010073static int ivm_set_value(char *name, char *value)
Heiko Schocher8f64da72008-10-15 09:41:00 +020074{
75 char tempbuf[256];
76
77 if (value != NULL) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +010078 sprintf(tempbuf, "%s=%s", name, value);
79 return set_local_var(tempbuf, 0);
Heiko Schocher8f64da72008-10-15 09:41:00 +020080 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +010081 unset_local_var(name);
Heiko Schocher8f64da72008-10-15 09:41:00 +020082 }
83 return 0;
84}
85
Heiko Schocherb11f53f2011-03-15 16:52:29 +010086static int ivm_get_value(unsigned char *buf, int len, char *name, int off,
Heiko Schocher8f64da72008-10-15 09:41:00 +020087 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])) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +010094 printf("%s Error corrupted %s\n", __func__, name);
Heiko Schocher8f64da72008-10-15 09:41:00 +020095 val = -1;
96 } else {
97 val = buf[off + 0] + (buf[off + 1] << 8);
98 if ((val == 0) && (check == 1))
99 val = -1;
100 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100101 sprintf((char *)valbuf, "%x", val);
102 ivm_set_value(name, (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200103 return val;
104}
105
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100106#define INV_BLOCKSIZE 0x100
107#define INV_DATAADDRESS 0x21
108#define INVENTORYDATASIZE (INV_BLOCKSIZE - INV_DATAADDRESS - 3)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200109
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
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100123static char convert_char(char c)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200124{
125 return (c < ' ' || c > '~') ? '.' : c;
126}
127
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100128static int ivm_findinventorystring(int type,
Heiko Schocher8f64da72008-10-15 09:41:00 +0200129 unsigned char* const string,
130 unsigned long maxlen,
131 unsigned char *buf)
132{
133 int xcode = 0;
134 unsigned long cr = 0;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100135 unsigned long addr = INV_DATAADDRESS;
Heiko Schocher8f64da72008-10-15 09:41:00 +0200136 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
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100159 /*
160 * the expected number of CR was found until the end of the IVM
161 * content --> fill string
162 */
Heiko Schocher8f64da72008-10-15 09:41:00 +0200163 if (addr < INVENTORYDATASIZE) {
164 /* Copy the IVM string in the corresponding string */
165 for (; (buf[addr] != '\r') &&
166 ((buf[addr] != ';') || (!stop)) &&
167 (size < (maxlen - 1) &&
168 (addr < INVENTORYDATASIZE)); addr++)
169 {
170 size += sprintf((char *)string + size, "%c",
171 convert_char (buf[addr]));
172 }
173
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100174 /*
175 * copy phase is done: check if everything is ok. If not,
Heiko Schocher8f64da72008-10-15 09:41:00 +0200176 * the inventory data is most probably corrupted: tell
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100177 * the world there is a problem!
178 */
Heiko Schocher8f64da72008-10-15 09:41:00 +0200179 if (addr == INVENTORYDATASIZE) {
180 xcode = -1;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100181 printf("Error end of string not found\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200182 } else if ((size >= (maxlen - 1)) &&
183 (buf[addr] != '\r')) {
184 xcode = -1;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100185 printf("string too long till next CR\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200186 }
187 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100188 /*
189 * some CR are missing...
190 * the inventory data is most probably corrupted
191 */
Heiko Schocher8f64da72008-10-15 09:41:00 +0200192 xcode = -1;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100193 printf("not enough cr found\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200194 }
195 return xcode;
196}
197
198#define GET_STRING(name, which, len) \
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100199 if (ivm_findinventorystring(which, valbuf, len, buf) == 0) { \
200 ivm_set_value(name, (char *)valbuf); \
Heiko Schocher8f64da72008-10-15 09:41:00 +0200201 }
202
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100203static int ivm_check_crc(unsigned char *buf, int block)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200204{
205 unsigned long crc;
206 unsigned long crceeprom;
207
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100208 crc = ivm_calc_crc(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200209 crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + \
210 buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200211 if (crc != crceeprom) {
Heiko Schocherdc71b242009-07-09 12:04:18 +0200212 if (block == 0)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100213 printf("Error CRC Block: %d EEprom: calculated: \
Heiko Schocherdc71b242009-07-09 12:04:18 +0200214 %lx EEprom: %lx\n", block, crc, crceeprom);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200215 return -1;
216 }
217 return 0;
218}
219
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100220static int ivm_analyze_block2(unsigned char *buf, int len)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200221{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200222 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
Heiko Schocher8f64da72008-10-15 09:41:00 +0200223 unsigned long count;
224
225 /* IVM_MacAddress */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100226 sprintf((char *)valbuf, "%pM", buf);
227 ivm_set_value("IVM_MacAddress", (char *)valbuf);
228 if (getenv("ethaddr") == NULL)
229 setenv((char *)"ethaddr", (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200230 /* IVM_MacCount */
231 count = (buf[10] << 24) +
232 (buf[11] << 16) +
233 (buf[12] << 8) +
234 buf[13];
235 if (count == 0xffffffff)
236 count = 1;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100237 sprintf((char *)valbuf, "%lx", count);
238 ivm_set_value("IVM_MacCount", (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200239 return 0;
240}
241
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100242int ivm_analyze_eeprom(unsigned char *buf, int len)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200243{
244 unsigned short val;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200245 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
Heiko Schocher8f64da72008-10-15 09:41:00 +0200246 unsigned char *tmp;
247
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100248 if (ivm_check_crc(buf, 0) != 0)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200249 return -1;
250
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100251 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
252 "IVM_BoardId", 0, 1);
253 val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
254 "IVM_HWKey", 6, 1);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200255 if (val != 0xffff) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100256 sprintf((char *)valbuf, "%x", ((val / 100) % 10));
257 ivm_set_value("IVM_HWVariant", (char *)valbuf);
258 sprintf((char *)valbuf, "%x", (val % 100));
259 ivm_set_value("IVM_HWVersion", (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200260 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100261 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
262 "IVM_Functions", 12, 0);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200263
264 GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
265 GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
266 tmp = (unsigned char *) getenv("IVM_DeviceName");
267 if (tmp) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100268 int len = strlen((char *)tmp);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200269 int i = 0;
270
271 while (i < len) {
272 if (tmp[i] == ';') {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100273 ivm_set_value("IVM_ShortText",
274 (char *)&tmp[i + 1]);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200275 break;
276 }
277 i++;
278 }
279 if (i >= len)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100280 ivm_set_value("IVM_ShortText", NULL);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200281 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100282 ivm_set_value("IVM_ShortText", NULL);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200283 }
284 GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
285 GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
286 GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
287 GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
288 GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
289 GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
290 GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
291 GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
292 GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
293
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100294 if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
Heiko Schocherdc71b242009-07-09 12:04:18 +0200295 return 0;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100296 ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2],
297 CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200298
299 return 0;
300}
301
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100302int ivm_read_eeprom(void)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200303{
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100304#if defined(CONFIG_I2C_MUX)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200305 I2C_MUX_DEVICE *dev = NULL;
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100306#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200307 uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
Heiko Schocher8f64da72008-10-15 09:41:00 +0200308 uchar *buf;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200309 unsigned dev_addr = CONFIG_SYS_IVM_EEPROM_ADR;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100310 int ret;
Heiko Schocher8f64da72008-10-15 09:41:00 +0200311
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100312#if defined(CONFIG_I2C_MUX)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200313 /* First init the Bus, select the Bus */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200314#if defined(CONFIG_SYS_I2C_IVM_BUS)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100315 dev = i2c_mux_ident_muxstring((uchar *)CONFIG_SYS_I2C_IVM_BUS);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200316#else
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100317 buf = (unsigned char *) getenv("EEprom_ivm");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200318 if (buf != NULL)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100319 dev = i2c_mux_ident_muxstring(buf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200320#endif
321 if (dev == NULL) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100322 printf("Error couldnt add Bus for IVM\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200323 return -1;
324 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100325 i2c_set_bus_num(dev->busid);
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100326#endif
Heiko Schocher8f64da72008-10-15 09:41:00 +0200327
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100328 buf = (unsigned char *) getenv("EEprom_ivm_addr");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200329 if (buf != NULL)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100330 dev_addr = simple_strtoul((char *)buf, NULL, 16);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200331
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100332 ret = i2c_read(dev_addr, 0, 1, i2c_buffer,
333 CONFIG_SYS_IVM_EEPROM_MAX_LEN);
334 if (ret != 0) {
Heiko Schocher8f64da72008-10-15 09:41:00 +0200335 printf ("Error reading EEprom\n");
336 return -2;
337 }
338
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100339 return ivm_analyze_eeprom(i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200340}
341
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200342#if defined(CONFIG_SYS_I2C_INIT_BOARD)
Heiko Schocherc2485362008-10-15 09:39:08 +0200343#define DELAY_ABORT_SEQ 62
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200344#define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
Heiko Schocherc2485362008-10-15 09:39:08 +0200345
Heiko Schocheraf895e42011-02-22 08:58:19 +0100346#if defined(CONFIG_MGCOGE) || defined(CONFIG_MGCOGE2NE)
Heiko Schocherc2485362008-10-15 09:39:08 +0200347#define SDA_MASK 0x00010000
348#define SCL_MASK 0x00020000
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100349static void set_pin(int state, unsigned long mask)
Heiko Schocherc2485362008-10-15 09:39:08 +0200350{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100351 ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocherc2485362008-10-15 09:39:08 +0200352
353 if (state)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100354 setbits_be32(&iop->pdat, mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200355 else
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100356 clrbits_be32(&iop->pdat, mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200357
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100358 setbits_be32(&iop->pdir, mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200359}
360
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100361static int get_pin(unsigned long mask)
Heiko Schocherc2485362008-10-15 09:39:08 +0200362{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100363 ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocherc2485362008-10-15 09:39:08 +0200364
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100365 clrbits_be32(&iop->pdir, mask);
366 return 0 != (in_be32(&iop->pdat) & mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200367}
368
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100369static void set_sda(int state)
Heiko Schocherc2485362008-10-15 09:39:08 +0200370{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100371 set_pin(state, SDA_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200372}
373
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100374static void set_scl(int state)
Heiko Schocherc2485362008-10-15 09:39:08 +0200375{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100376 set_pin(state, SCL_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200377}
378
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100379static int get_sda(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200380{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100381 return get_pin(SDA_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200382}
383
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100384static int get_scl(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200385{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100386 return get_pin(SCL_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200387}
388
389#if defined(CONFIG_HARD_I2C)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100390static void setports(int gpio)
Heiko Schocherc2485362008-10-15 09:39:08 +0200391{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100392 ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocherc2485362008-10-15 09:39:08 +0200393
394 if (gpio) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100395 clrbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
396 clrbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
Heiko Schocherc2485362008-10-15 09:39:08 +0200397 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100398 setbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
399 clrbits_be32(&iop->pdir, (SDA_MASK | SCL_MASK));
400 setbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
Heiko Schocherc2485362008-10-15 09:39:08 +0200401 }
402}
403#endif
404#endif
405
Heiko Schocher62ddcf02010-02-18 08:08:25 +0100406#if !defined(CONFIG_MPC83xx)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100407static void writeStartSeq(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200408{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100409 set_sda(1);
410 udelay(DELAY_HALF_PERIOD);
411 set_scl(1);
412 udelay(DELAY_HALF_PERIOD);
413 set_sda(0);
414 udelay(DELAY_HALF_PERIOD);
415 set_scl(0);
416 udelay(DELAY_HALF_PERIOD);
Heiko Schocherc2485362008-10-15 09:39:08 +0200417}
418
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100419/*
420 * I2C is a synchronous protocol and resets of the processor in the middle
421 * of an access can block the I2C Bus until a powerdown of the full unit is
422 * done. This function toggles the SCL until the SCL and SCA line are
423 * released, but max. 16 times, after this a I2C start-sequence is sent.
424 * This I2C Deblocking mechanism was developed by Keymile in association
425 * with Anatech and Atmel in 1998.
Heiko Schocherc2485362008-10-15 09:39:08 +0200426 */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100427static int i2c_make_abort(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200428{
429 int scl_state = 0;
430 int sda_state = 0;
431 int i = 0;
432 int ret = 0;
433
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100434 if (!get_sda()) {
Heiko Schocherc2485362008-10-15 09:39:08 +0200435 ret = -1;
436 while (i < 16) {
437 i++;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100438 set_scl(0);
439 udelay(DELAY_ABORT_SEQ);
440 set_scl(1);
441 udelay(DELAY_ABORT_SEQ);
442 scl_state = get_scl();
443 sda_state = get_sda();
Heiko Schocherc2485362008-10-15 09:39:08 +0200444 if (scl_state && sda_state) {
445 ret = 0;
446 break;
447 }
448 }
449 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100450 if (ret == 0)
451 for (i = 0; i < 5; i++)
452 writeStartSeq();
453
454 get_sda();
Heiko Schocherc2485362008-10-15 09:39:08 +0200455 return ret;
456}
Heiko Schocher39df00d2009-07-09 12:04:26 +0200457#endif
Heiko Schocherc2485362008-10-15 09:39:08 +0200458
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100459/*
Heiko Schocherc2485362008-10-15 09:39:08 +0200460 * i2c_init_board - reset i2c bus. When the board is powercycled during a
461 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
462 */
463void i2c_init_board(void)
464{
Heiko Schocher62ddcf02010-02-18 08:08:25 +0100465#if defined(CONFIG_MPC83xx)
Heiko Schocher39df00d2009-07-09 12:04:26 +0200466 struct fsl_i2c *dev;
467 dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
468 uchar dummy;
469
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100470 out_8(&dev->cr, (I2C_CR_MSTA));
471 out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
Heiko Schocher39df00d2009-07-09 12:04:26 +0200472 dummy = in_8(&dev->dr);
473 dummy = in_8(&dev->dr);
474 if (dummy != 0xff) {
475 dummy = in_8(&dev->dr);
476 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100477 out_8(&dev->cr, (I2C_CR_MEN));
478 out_8(&dev->cr, 0x00);
479 out_8(&dev->cr, (I2C_CR_MEN));
Heiko Schocher39df00d2009-07-09 12:04:26 +0200480#else
Holger Brunck802d9962011-03-14 15:31:19 +0100481#if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100482 immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
483 i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
Heiko Schocherc2485362008-10-15 09:39:08 +0200484
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100485 /*
486 * disable I2C controller first, otherwhise it thinks we want to
487 * talk to the slave port...
488 */
489 clrbits_8(&i2c->i2c_i2mod, 0x01);
Heiko Schocherc2485362008-10-15 09:39:08 +0200490
491 /* Set the PortPins to GPIO */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100492 setports(1);
Heiko Schocherc2485362008-10-15 09:39:08 +0200493#endif
494
495 /* Now run the AbortSequence() */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100496 i2c_make_abort();
Heiko Schocherc2485362008-10-15 09:39:08 +0200497
498#if defined(CONFIG_HARD_I2C)
499 /* Set the PortPins back to use for I2C */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100500 setports(0);
Heiko Schocherc2485362008-10-15 09:39:08 +0200501#endif
Heiko Schocher39df00d2009-07-09 12:04:26 +0200502#endif
Heiko Schocherc2485362008-10-15 09:39:08 +0200503}
504#endif
Heiko Schocher210c8c02008-11-21 08:29:40 +0100505#endif
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200506
507#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100508int fdt_set_node_and_value(void *blob,
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200509 char *nodename,
510 char *regname,
511 void *var,
512 int size)
513{
514 int ret = 0;
515 int nodeoffset = 0;
516
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100517 nodeoffset = fdt_path_offset(blob, nodename);
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200518 if (nodeoffset >= 0) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100519 ret = fdt_setprop(blob, nodeoffset, regname, var,
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200520 size);
521 if (ret < 0)
522 printf("ft_blob_update(): cannot set %s/%s "
523 "property err:%s\n", nodename, regname,
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100524 fdt_strerror(ret));
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200525 } else {
526 printf("ft_blob_update(): cannot find %s node "
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100527 "err:%s\n", nodename, fdt_strerror(nodeoffset));
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200528 }
529 return ret;
530}
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100531
532int fdt_get_node_and_value(void *blob,
Heiko Schocherdc71b242009-07-09 12:04:18 +0200533 char *nodename,
534 char *propname,
535 void **var)
536{
537 int len;
538 int nodeoffset = 0;
539
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100540 nodeoffset = fdt_path_offset(blob, nodename);
Heiko Schocherdc71b242009-07-09 12:04:18 +0200541 if (nodeoffset >= 0) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100542 *var = (void *)fdt_getprop(blob, nodeoffset, propname, &len);
Heiko Schocherdc71b242009-07-09 12:04:18 +0200543 if (len == 0) {
544 /* no value */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100545 printf("%s no value\n", __func__);
Heiko Schocherdc71b242009-07-09 12:04:18 +0200546 return -1;
547 } else if (len > 0) {
548 return len;
549 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100550 printf("libfdt fdt_getprop(): %s\n",
Heiko Schocherdc71b242009-07-09 12:04:18 +0200551 fdt_strerror(len));
552 return -2;
553 }
554 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100555 printf("%s: cannot find %s node err:%s\n", __func__,
556 nodename, fdt_strerror(nodeoffset));
Heiko Schocherdc71b242009-07-09 12:04:18 +0200557 return -3;
558 }
559}
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200560#endif
Heiko Schocher210c8c02008-11-21 08:29:40 +0100561
Holger Brunck802d9962011-03-14 15:31:19 +0100562#if !defined(MACH_TYPE_KM_KIRKWOOD)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100563int ethernet_present(void)
Heiko Schocher210c8c02008-11-21 08:29:40 +0100564{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100565 struct km_bec_fpga *base = (struct km_bec_fpga *)CONFIG_SYS_PIGGY_BASE;
566
567 return in_8(&base->bprth) & PIGGY_PRESENT;
Heiko Schocher210c8c02008-11-21 08:29:40 +0100568}
Heiko Schocher67fa8c22010-02-22 16:43:02 +0530569#endif
Heiko Schocher210c8c02008-11-21 08:29:40 +0100570
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100571int board_eth_init(bd_t *bis)
Heiko Schocher210c8c02008-11-21 08:29:40 +0100572{
573#ifdef CONFIG_KEYMILE_HDLC_ENET
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100574 (void)keymile_hdlc_enet_initialize(bis);
Heiko Schocher210c8c02008-11-21 08:29:40 +0100575#endif
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100576 if (ethernet_present())
Heiko Schocher62ddcf02010-02-18 08:08:25 +0100577 return cpu_eth_init(bis);
578
579 return -1;
Heiko Schocher210c8c02008-11-21 08:29:40 +0100580}