blob: 6600e08711ea4252bb2306cbf4730b1444a0761f [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 Schocher6c11aea2011-03-08 10:51:15 +010043static void i2c_write_start_seq(void);
44static int i2c_make_abort(void);
45
Heiko Schocherb11f53f2011-03-15 16:52:29 +010046int ivm_calc_crc(unsigned char *buf, int len)
Heiko Schocher8f64da72008-10-15 09:41:00 +020047{
48 const unsigned short crc_tab[16] = {
49 0x0000, 0xCC01, 0xD801, 0x1400,
50 0xF001, 0x3C00, 0x2800, 0xE401,
51 0xA001, 0x6C00, 0x7800, 0xB401,
52 0x5000, 0x9C01, 0x8801, 0x4400};
53
54 unsigned short crc = 0; /* final result */
55 unsigned short r1 = 0; /* temp */
56 unsigned char byte = 0; /* input buffer */
57 int i;
58
59 /* calculate CRC from array data */
60 for (i = 0; i < len; i++) {
61 byte = buf[i];
62
63 /* lower 4 bits */
64 r1 = crc_tab[crc & 0xF];
65 crc = ((crc) >> 4) & 0x0FFF;
66 crc = crc ^ r1 ^ crc_tab[byte & 0xF];
67
68 /* upper 4 bits */
69 r1 = crc_tab[crc & 0xF];
70 crc = (crc >> 4) & 0x0FFF;
71 crc = crc ^ r1 ^ crc_tab[(byte >> 4) & 0xF];
72 }
73 return crc;
74}
75
Heiko Schocherb11f53f2011-03-15 16:52:29 +010076static int ivm_set_value(char *name, char *value)
Heiko Schocher8f64da72008-10-15 09:41:00 +020077{
78 char tempbuf[256];
79
80 if (value != NULL) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +010081 sprintf(tempbuf, "%s=%s", name, value);
82 return set_local_var(tempbuf, 0);
Heiko Schocher8f64da72008-10-15 09:41:00 +020083 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +010084 unset_local_var(name);
Heiko Schocher8f64da72008-10-15 09:41:00 +020085 }
86 return 0;
87}
88
Heiko Schocherb11f53f2011-03-15 16:52:29 +010089static int ivm_get_value(unsigned char *buf, int len, char *name, int off,
Heiko Schocher8f64da72008-10-15 09:41:00 +020090 int check)
91{
92 unsigned short val;
93 unsigned char valbuf[30];
94
95 if ((buf[off + 0] != buf[off + 2]) &&
96 (buf[off + 2] != buf[off + 4])) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +010097 printf("%s Error corrupted %s\n", __func__, name);
Heiko Schocher8f64da72008-10-15 09:41:00 +020098 val = -1;
99 } else {
100 val = buf[off + 0] + (buf[off + 1] << 8);
101 if ((val == 0) && (check == 1))
102 val = -1;
103 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100104 sprintf((char *)valbuf, "%x", val);
105 ivm_set_value(name, (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200106 return val;
107}
108
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100109#define INV_BLOCKSIZE 0x100
110#define INV_DATAADDRESS 0x21
111#define INVENTORYDATASIZE (INV_BLOCKSIZE - INV_DATAADDRESS - 3)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200112
113#define IVM_POS_SHORT_TEXT 0
114#define IVM_POS_MANU_ID 1
115#define IVM_POS_MANU_SERIAL 2
116#define IVM_POS_PART_NUMBER 3
117#define IVM_POS_BUILD_STATE 4
118#define IVM_POS_SUPPLIER_PART_NUMBER 5
119#define IVM_POS_DELIVERY_DATE 6
120#define IVM_POS_SUPPLIER_BUILD_STATE 7
121#define IVM_POS_CUSTOMER_ID 8
122#define IVM_POS_CUSTOMER_PROD_ID 9
123#define IVM_POS_HISTORY 10
124#define IVM_POS_SYMBOL_ONLY 11
125
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100126static char convert_char(char c)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200127{
128 return (c < ' ' || c > '~') ? '.' : c;
129}
130
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100131static int ivm_findinventorystring(int type,
Heiko Schocher8f64da72008-10-15 09:41:00 +0200132 unsigned char* const string,
133 unsigned long maxlen,
134 unsigned char *buf)
135{
136 int xcode = 0;
137 unsigned long cr = 0;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100138 unsigned long addr = INV_DATAADDRESS;
Heiko Schocher8f64da72008-10-15 09:41:00 +0200139 unsigned long size = 0;
140 unsigned long nr = type;
141 int stop = 0; /* stop on semicolon */
142
143 memset(string, '\0', maxlen);
144 switch (type) {
145 case IVM_POS_SYMBOL_ONLY:
146 nr = 0;
147 stop= 1;
148 break;
149 default:
150 nr = type;
151 stop = 0;
152 }
153
154 /* Look for the requested number of CR. */
155 while ((cr != nr) && (addr < INVENTORYDATASIZE)) {
156 if ((buf[addr] == '\r')) {
157 cr++;
158 }
159 addr++;
160 }
161
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100162 /*
163 * the expected number of CR was found until the end of the IVM
164 * content --> fill string
165 */
Heiko Schocher8f64da72008-10-15 09:41:00 +0200166 if (addr < INVENTORYDATASIZE) {
167 /* Copy the IVM string in the corresponding string */
168 for (; (buf[addr] != '\r') &&
169 ((buf[addr] != ';') || (!stop)) &&
170 (size < (maxlen - 1) &&
171 (addr < INVENTORYDATASIZE)); addr++)
172 {
173 size += sprintf((char *)string + size, "%c",
174 convert_char (buf[addr]));
175 }
176
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100177 /*
178 * copy phase is done: check if everything is ok. If not,
Heiko Schocher8f64da72008-10-15 09:41:00 +0200179 * the inventory data is most probably corrupted: tell
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100180 * the world there is a problem!
181 */
Heiko Schocher8f64da72008-10-15 09:41:00 +0200182 if (addr == INVENTORYDATASIZE) {
183 xcode = -1;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100184 printf("Error end of string not found\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200185 } else if ((size >= (maxlen - 1)) &&
186 (buf[addr] != '\r')) {
187 xcode = -1;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100188 printf("string too long till next CR\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200189 }
190 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100191 /*
192 * some CR are missing...
193 * the inventory data is most probably corrupted
194 */
Heiko Schocher8f64da72008-10-15 09:41:00 +0200195 xcode = -1;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100196 printf("not enough cr found\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200197 }
198 return xcode;
199}
200
201#define GET_STRING(name, which, len) \
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100202 if (ivm_findinventorystring(which, valbuf, len, buf) == 0) { \
203 ivm_set_value(name, (char *)valbuf); \
Heiko Schocher8f64da72008-10-15 09:41:00 +0200204 }
205
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100206static int ivm_check_crc(unsigned char *buf, int block)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200207{
208 unsigned long crc;
209 unsigned long crceeprom;
210
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100211 crc = ivm_calc_crc(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200212 crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + \
213 buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200214 if (crc != crceeprom) {
Heiko Schocherdc71b242009-07-09 12:04:18 +0200215 if (block == 0)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100216 printf("Error CRC Block: %d EEprom: calculated: \
Heiko Schocherdc71b242009-07-09 12:04:18 +0200217 %lx EEprom: %lx\n", block, crc, crceeprom);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200218 return -1;
219 }
220 return 0;
221}
222
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100223static int ivm_analyze_block2(unsigned char *buf, int len)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200224{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200225 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
Heiko Schocher8f64da72008-10-15 09:41:00 +0200226 unsigned long count;
227
228 /* IVM_MacAddress */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100229 sprintf((char *)valbuf, "%pM", buf);
230 ivm_set_value("IVM_MacAddress", (char *)valbuf);
231 if (getenv("ethaddr") == NULL)
232 setenv((char *)"ethaddr", (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200233 /* IVM_MacCount */
234 count = (buf[10] << 24) +
235 (buf[11] << 16) +
236 (buf[12] << 8) +
237 buf[13];
238 if (count == 0xffffffff)
239 count = 1;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100240 sprintf((char *)valbuf, "%lx", count);
241 ivm_set_value("IVM_MacCount", (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200242 return 0;
243}
244
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100245int ivm_analyze_eeprom(unsigned char *buf, int len)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200246{
247 unsigned short val;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200248 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
Heiko Schocher8f64da72008-10-15 09:41:00 +0200249 unsigned char *tmp;
250
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100251 if (ivm_check_crc(buf, 0) != 0)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200252 return -1;
253
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100254 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
255 "IVM_BoardId", 0, 1);
256 val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
257 "IVM_HWKey", 6, 1);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200258 if (val != 0xffff) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100259 sprintf((char *)valbuf, "%x", ((val / 100) % 10));
260 ivm_set_value("IVM_HWVariant", (char *)valbuf);
261 sprintf((char *)valbuf, "%x", (val % 100));
262 ivm_set_value("IVM_HWVersion", (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200263 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100264 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
265 "IVM_Functions", 12, 0);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200266
267 GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
268 GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
269 tmp = (unsigned char *) getenv("IVM_DeviceName");
270 if (tmp) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100271 int len = strlen((char *)tmp);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200272 int i = 0;
273
274 while (i < len) {
275 if (tmp[i] == ';') {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100276 ivm_set_value("IVM_ShortText",
277 (char *)&tmp[i + 1]);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200278 break;
279 }
280 i++;
281 }
282 if (i >= len)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100283 ivm_set_value("IVM_ShortText", NULL);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200284 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100285 ivm_set_value("IVM_ShortText", NULL);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200286 }
287 GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
288 GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
289 GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
290 GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
291 GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
292 GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
293 GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
294 GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
295 GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
296
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100297 if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
Heiko Schocherdc71b242009-07-09 12:04:18 +0200298 return 0;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100299 ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2],
300 CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200301
302 return 0;
303}
304
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100305int ivm_read_eeprom(void)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200306{
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100307#if defined(CONFIG_I2C_MUX)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200308 I2C_MUX_DEVICE *dev = NULL;
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100309#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200310 uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
Heiko Schocher8f64da72008-10-15 09:41:00 +0200311 uchar *buf;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200312 unsigned dev_addr = CONFIG_SYS_IVM_EEPROM_ADR;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100313 int ret;
Heiko Schocher8f64da72008-10-15 09:41:00 +0200314
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100315#if defined(CONFIG_I2C_MUX)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200316 /* First init the Bus, select the Bus */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200317#if defined(CONFIG_SYS_I2C_IVM_BUS)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100318 dev = i2c_mux_ident_muxstring((uchar *)CONFIG_SYS_I2C_IVM_BUS);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200319#else
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100320 buf = (unsigned char *) getenv("EEprom_ivm");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200321 if (buf != NULL)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100322 dev = i2c_mux_ident_muxstring(buf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200323#endif
324 if (dev == NULL) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100325 printf("Error couldnt add Bus for IVM\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200326 return -1;
327 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100328 i2c_set_bus_num(dev->busid);
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100329#endif
Heiko Schocher8f64da72008-10-15 09:41:00 +0200330
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100331 buf = (unsigned char *) getenv("EEprom_ivm_addr");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200332 if (buf != NULL)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100333 dev_addr = simple_strtoul((char *)buf, NULL, 16);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200334
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100335 /* add deblocking here */
336 i2c_make_abort();
337
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100338 ret = i2c_read(dev_addr, 0, 1, i2c_buffer,
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100339 CONFIG_SYS_IVM_EEPROM_MAX_LEN);
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100340 if (ret != 0) {
Heiko Schocher8f64da72008-10-15 09:41:00 +0200341 printf ("Error reading EEprom\n");
342 return -2;
343 }
344
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100345 return ivm_analyze_eeprom(i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200346}
347
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200348#if defined(CONFIG_SYS_I2C_INIT_BOARD)
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100349#define DELAY_ABORT_SEQ 62 /* @200kHz 9 clocks = 44us, 62us is ok */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200350#define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
Heiko Schocherc2485362008-10-15 09:39:08 +0200351
Heiko Schocheraf895e42011-02-22 08:58:19 +0100352#if defined(CONFIG_MGCOGE) || defined(CONFIG_MGCOGE2NE)
Heiko Schocherc2485362008-10-15 09:39:08 +0200353#define SDA_MASK 0x00010000
354#define SCL_MASK 0x00020000
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100355static void set_pin(int state, unsigned long mask)
Heiko Schocherc2485362008-10-15 09:39:08 +0200356{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100357 ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocherc2485362008-10-15 09:39:08 +0200358
359 if (state)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100360 setbits_be32(&iop->pdat, mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200361 else
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100362 clrbits_be32(&iop->pdat, mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200363
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100364 setbits_be32(&iop->pdir, mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200365}
366
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100367static int get_pin(unsigned long mask)
Heiko Schocherc2485362008-10-15 09:39:08 +0200368{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100369 ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocherc2485362008-10-15 09:39:08 +0200370
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100371 clrbits_be32(&iop->pdir, mask);
372 return 0 != (in_be32(&iop->pdat) & mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200373}
374
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100375static void set_sda(int state)
Heiko Schocherc2485362008-10-15 09:39:08 +0200376{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100377 set_pin(state, SDA_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200378}
379
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100380static void set_scl(int state)
Heiko Schocherc2485362008-10-15 09:39:08 +0200381{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100382 set_pin(state, SCL_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200383}
384
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100385static int get_sda(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200386{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100387 return get_pin(SDA_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200388}
389
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100390static int get_scl(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200391{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100392 return get_pin(SCL_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200393}
394
395#if defined(CONFIG_HARD_I2C)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100396static void setports(int gpio)
Heiko Schocherc2485362008-10-15 09:39:08 +0200397{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100398 ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocherc2485362008-10-15 09:39:08 +0200399
400 if (gpio) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100401 clrbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
402 clrbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
Heiko Schocherc2485362008-10-15 09:39:08 +0200403 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100404 setbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
405 clrbits_be32(&iop->pdir, (SDA_MASK | SCL_MASK));
406 setbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
Heiko Schocherc2485362008-10-15 09:39:08 +0200407 }
408}
409#endif
410#endif
411
Heiko Schocher62ddcf02010-02-18 08:08:25 +0100412#if !defined(CONFIG_MPC83xx)
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100413static void i2c_write_start_seq(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200414{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100415 set_sda(1);
416 udelay(DELAY_HALF_PERIOD);
417 set_scl(1);
418 udelay(DELAY_HALF_PERIOD);
419 set_sda(0);
420 udelay(DELAY_HALF_PERIOD);
421 set_scl(0);
422 udelay(DELAY_HALF_PERIOD);
Heiko Schocherc2485362008-10-15 09:39:08 +0200423}
424
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100425/*
426 * I2C is a synchronous protocol and resets of the processor in the middle
427 * of an access can block the I2C Bus until a powerdown of the full unit is
428 * done. This function toggles the SCL until the SCL and SCA line are
429 * released, but max. 16 times, after this a I2C start-sequence is sent.
430 * This I2C Deblocking mechanism was developed by Keymile in association
431 * with Anatech and Atmel in 1998.
Heiko Schocherc2485362008-10-15 09:39:08 +0200432 */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100433static int i2c_make_abort(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200434{
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100435
436#if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
437 immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
438 i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
439
440 /*
441 * disable I2C controller first, otherwhise it thinks we want to
442 * talk to the slave port...
443 */
444 clrbits_8(&i2c->i2c_i2mod, 0x01);
445
446 /* Set the PortPins to GPIO */
447 setports(1);
448#endif
449
Heiko Schocherc2485362008-10-15 09:39:08 +0200450 int scl_state = 0;
451 int sda_state = 0;
452 int i = 0;
453 int ret = 0;
454
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100455 if (!get_sda()) {
Heiko Schocherc2485362008-10-15 09:39:08 +0200456 ret = -1;
457 while (i < 16) {
458 i++;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100459 set_scl(0);
460 udelay(DELAY_ABORT_SEQ);
461 set_scl(1);
462 udelay(DELAY_ABORT_SEQ);
463 scl_state = get_scl();
464 sda_state = get_sda();
Heiko Schocherc2485362008-10-15 09:39:08 +0200465 if (scl_state && sda_state) {
466 ret = 0;
467 break;
468 }
469 }
470 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100471 if (ret == 0)
472 for (i = 0; i < 5; i++)
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100473 i2c_write_start_seq();
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100474
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100475 /* respect stop setup time */
476 udelay(DELAY_ABORT_SEQ);
477 set_scl(1);
478 udelay(DELAY_ABORT_SEQ);
479 set_sda(1);
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100480 get_sda();
Heiko Schocherc2485362008-10-15 09:39:08 +0200481
482#if defined(CONFIG_HARD_I2C)
483 /* Set the PortPins back to use for I2C */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100484 setports(0);
Heiko Schocherc2485362008-10-15 09:39:08 +0200485#endif
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100486 return ret;
487}
Heiko Schocher39df00d2009-07-09 12:04:26 +0200488#endif
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100489
490#if defined(CONFIG_MPC83xx)
491static void i2c_write_start_seq(void)
492{
493 struct fsl_i2c *dev;
494 dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
495 udelay(DELAY_ABORT_SEQ);
496 out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
497 udelay(DELAY_ABORT_SEQ);
498 out_8(&dev->cr, (I2C_CR_MEN));
499}
500
501static int i2c_make_abort(void)
502{
503 struct fsl_i2c *dev;
504 dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
505 uchar dummy;
506 uchar last;
507 int nbr_read = 0;
508 int i = 0;
509 int ret = 0;
510
511 /* wait after each operation to finsh with a delay */
512 out_8(&dev->cr, (I2C_CR_MSTA));
513 udelay(DELAY_ABORT_SEQ);
514 out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
515 udelay(DELAY_ABORT_SEQ);
516 dummy = in_8(&dev->dr);
517 udelay(DELAY_ABORT_SEQ);
518 last = in_8(&dev->dr);
519 nbr_read++;
520
521 /*
522 * do read until the last bit is 1, but stop if the full eeprom is
523 * read.
524 */
525 while (((last & 0x01) != 0x01) &&
526 (nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) {
527 udelay(DELAY_ABORT_SEQ);
528 last = in_8(&dev->dr);
529 nbr_read++;
530 }
531 if ((last & 0x01) != 0x01)
532 ret = -2;
533 if ((last != 0xff) || (nbr_read > 1))
534 printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
535 nbr_read, last);
536 udelay(DELAY_ABORT_SEQ);
537 out_8(&dev->cr, (I2C_CR_MEN));
538 udelay(DELAY_ABORT_SEQ);
539 /* clear status reg */
540 out_8(&dev->sr, 0);
541
542 for (i = 0; i < 5; i++)
543 i2c_write_start_seq();
544 if (ret != 0)
545 printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
546 nbr_read, last);
547
548 return ret;
549}
550#endif
551
552/**
553 * i2c_init_board - reset i2c bus. When the board is powercycled during a
554 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
555 */
556void i2c_init_board(void)
557{
558 /* Now run the AbortSequence() */
559 i2c_make_abort();
Heiko Schocherc2485362008-10-15 09:39:08 +0200560}
561#endif
Heiko Schocher210c8c02008-11-21 08:29:40 +0100562#endif
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200563
564#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100565int fdt_set_node_and_value(void *blob,
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200566 char *nodename,
567 char *regname,
568 void *var,
569 int size)
570{
571 int ret = 0;
572 int nodeoffset = 0;
573
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100574 nodeoffset = fdt_path_offset(blob, nodename);
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200575 if (nodeoffset >= 0) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100576 ret = fdt_setprop(blob, nodeoffset, regname, var,
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200577 size);
578 if (ret < 0)
579 printf("ft_blob_update(): cannot set %s/%s "
580 "property err:%s\n", nodename, regname,
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100581 fdt_strerror(ret));
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200582 } else {
583 printf("ft_blob_update(): cannot find %s node "
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100584 "err:%s\n", nodename, fdt_strerror(nodeoffset));
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200585 }
586 return ret;
587}
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100588
589int fdt_get_node_and_value(void *blob,
Heiko Schocherdc71b242009-07-09 12:04:18 +0200590 char *nodename,
591 char *propname,
592 void **var)
593{
594 int len;
595 int nodeoffset = 0;
596
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100597 nodeoffset = fdt_path_offset(blob, nodename);
Heiko Schocherdc71b242009-07-09 12:04:18 +0200598 if (nodeoffset >= 0) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100599 *var = (void *)fdt_getprop(blob, nodeoffset, propname, &len);
Heiko Schocherdc71b242009-07-09 12:04:18 +0200600 if (len == 0) {
601 /* no value */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100602 printf("%s no value\n", __func__);
Heiko Schocherdc71b242009-07-09 12:04:18 +0200603 return -1;
604 } else if (len > 0) {
605 return len;
606 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100607 printf("libfdt fdt_getprop(): %s\n",
Heiko Schocherdc71b242009-07-09 12:04:18 +0200608 fdt_strerror(len));
609 return -2;
610 }
611 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100612 printf("%s: cannot find %s node err:%s\n", __func__,
613 nodename, fdt_strerror(nodeoffset));
Heiko Schocherdc71b242009-07-09 12:04:18 +0200614 return -3;
615 }
616}
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200617#endif
Heiko Schocher210c8c02008-11-21 08:29:40 +0100618
Holger Brunck802d9962011-03-14 15:31:19 +0100619#if !defined(MACH_TYPE_KM_KIRKWOOD)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100620int ethernet_present(void)
Heiko Schocher210c8c02008-11-21 08:29:40 +0100621{
Heiko Schocher8ed74342011-03-08 10:47:39 +0100622 struct km_bec_fpga *base =
623 (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100624
625 return in_8(&base->bprth) & PIGGY_PRESENT;
Heiko Schocher210c8c02008-11-21 08:29:40 +0100626}
Heiko Schocher67fa8c22010-02-22 16:43:02 +0530627#endif
Heiko Schocher210c8c02008-11-21 08:29:40 +0100628
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100629int board_eth_init(bd_t *bis)
Heiko Schocher210c8c02008-11-21 08:29:40 +0100630{
631#ifdef CONFIG_KEYMILE_HDLC_ENET
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100632 (void)keymile_hdlc_enet_initialize(bis);
Heiko Schocher210c8c02008-11-21 08:29:40 +0100633#endif
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100634 if (ethernet_present())
Heiko Schocher62ddcf02010-02-18 08:08:25 +0100635 return cpu_eth_init(bis);
636
637 return -1;
Heiko Schocher210c8c02008-11-21 08:29:40 +0100638}