blob: ea3202898da4cc6b3e43770d37ab1d99dc2825c4 [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 Schocher210c8c02008-11-21 08:29:40 +010025#if defined(CONFIG_MGCOGE)
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>
32#include <asm/io.h>
Heiko Schocherc2485362008-10-15 09:39:08 +020033
34#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
35#include <libfdt.h>
36#endif
37
Heiko Schocher67fa8c22010-02-22 16:43:02 +053038#include "../common/common.h"
Heiko Schocherc2485362008-10-15 09:39:08 +020039#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
40#include <i2c.h>
Heiko Schocherc2485362008-10-15 09:39:08 +020041
Heiko Schocherb11f53f2011-03-15 16:52:29 +010042int ivm_calc_crc(unsigned char *buf, int len)
Heiko Schocher8f64da72008-10-15 09:41:00 +020043{
44 const unsigned short crc_tab[16] = {
45 0x0000, 0xCC01, 0xD801, 0x1400,
46 0xF001, 0x3C00, 0x2800, 0xE401,
47 0xA001, 0x6C00, 0x7800, 0xB401,
48 0x5000, 0x9C01, 0x8801, 0x4400};
49
50 unsigned short crc = 0; /* final result */
51 unsigned short r1 = 0; /* temp */
52 unsigned char byte = 0; /* input buffer */
53 int i;
54
55 /* calculate CRC from array data */
56 for (i = 0; i < len; i++) {
57 byte = buf[i];
58
59 /* lower 4 bits */
60 r1 = crc_tab[crc & 0xF];
61 crc = ((crc) >> 4) & 0x0FFF;
62 crc = crc ^ r1 ^ crc_tab[byte & 0xF];
63
64 /* upper 4 bits */
65 r1 = crc_tab[crc & 0xF];
66 crc = (crc >> 4) & 0x0FFF;
67 crc = crc ^ r1 ^ crc_tab[(byte >> 4) & 0xF];
68 }
69 return crc;
70}
71
Heiko Schocherb11f53f2011-03-15 16:52:29 +010072static int ivm_set_value(char *name, char *value)
Heiko Schocher8f64da72008-10-15 09:41:00 +020073{
74 char tempbuf[256];
75
76 if (value != NULL) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +010077 sprintf(tempbuf, "%s=%s", name, value);
78 return set_local_var(tempbuf, 0);
Heiko Schocher8f64da72008-10-15 09:41:00 +020079 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +010080 unset_local_var(name);
Heiko Schocher8f64da72008-10-15 09:41:00 +020081 }
82 return 0;
83}
84
Heiko Schocherb11f53f2011-03-15 16:52:29 +010085static int ivm_get_value(unsigned char *buf, int len, char *name, int off,
Heiko Schocher8f64da72008-10-15 09:41:00 +020086 int check)
87{
88 unsigned short val;
89 unsigned char valbuf[30];
90
91 if ((buf[off + 0] != buf[off + 2]) &&
92 (buf[off + 2] != buf[off + 4])) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +010093 printf("%s Error corrupted %s\n", __func__, name);
Heiko Schocher8f64da72008-10-15 09:41:00 +020094 val = -1;
95 } else {
96 val = buf[off + 0] + (buf[off + 1] << 8);
97 if ((val == 0) && (check == 1))
98 val = -1;
99 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100100 sprintf((char *)valbuf, "%x", val);
101 ivm_set_value(name, (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200102 return val;
103}
104
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100105#define INV_BLOCKSIZE 0x100
106#define INV_DATAADDRESS 0x21
107#define INVENTORYDATASIZE (INV_BLOCKSIZE - INV_DATAADDRESS - 3)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200108
109#define IVM_POS_SHORT_TEXT 0
110#define IVM_POS_MANU_ID 1
111#define IVM_POS_MANU_SERIAL 2
112#define IVM_POS_PART_NUMBER 3
113#define IVM_POS_BUILD_STATE 4
114#define IVM_POS_SUPPLIER_PART_NUMBER 5
115#define IVM_POS_DELIVERY_DATE 6
116#define IVM_POS_SUPPLIER_BUILD_STATE 7
117#define IVM_POS_CUSTOMER_ID 8
118#define IVM_POS_CUSTOMER_PROD_ID 9
119#define IVM_POS_HISTORY 10
120#define IVM_POS_SYMBOL_ONLY 11
121
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100122static char convert_char(char c)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200123{
124 return (c < ' ' || c > '~') ? '.' : c;
125}
126
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100127static int ivm_findinventorystring(int type,
Heiko Schocher8f64da72008-10-15 09:41:00 +0200128 unsigned char* const string,
129 unsigned long maxlen,
130 unsigned char *buf)
131{
132 int xcode = 0;
133 unsigned long cr = 0;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100134 unsigned long addr = INV_DATAADDRESS;
Heiko Schocher8f64da72008-10-15 09:41:00 +0200135 unsigned long size = 0;
136 unsigned long nr = type;
137 int stop = 0; /* stop on semicolon */
138
139 memset(string, '\0', maxlen);
140 switch (type) {
141 case IVM_POS_SYMBOL_ONLY:
142 nr = 0;
143 stop= 1;
144 break;
145 default:
146 nr = type;
147 stop = 0;
148 }
149
150 /* Look for the requested number of CR. */
151 while ((cr != nr) && (addr < INVENTORYDATASIZE)) {
152 if ((buf[addr] == '\r')) {
153 cr++;
154 }
155 addr++;
156 }
157
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100158 /*
159 * the expected number of CR was found until the end of the IVM
160 * content --> fill string
161 */
Heiko Schocher8f64da72008-10-15 09:41:00 +0200162 if (addr < INVENTORYDATASIZE) {
163 /* Copy the IVM string in the corresponding string */
164 for (; (buf[addr] != '\r') &&
165 ((buf[addr] != ';') || (!stop)) &&
166 (size < (maxlen - 1) &&
167 (addr < INVENTORYDATASIZE)); addr++)
168 {
169 size += sprintf((char *)string + size, "%c",
170 convert_char (buf[addr]));
171 }
172
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100173 /*
174 * copy phase is done: check if everything is ok. If not,
Heiko Schocher8f64da72008-10-15 09:41:00 +0200175 * the inventory data is most probably corrupted: tell
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100176 * the world there is a problem!
177 */
Heiko Schocher8f64da72008-10-15 09:41:00 +0200178 if (addr == INVENTORYDATASIZE) {
179 xcode = -1;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100180 printf("Error end of string not found\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200181 } else if ((size >= (maxlen - 1)) &&
182 (buf[addr] != '\r')) {
183 xcode = -1;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100184 printf("string too long till next CR\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200185 }
186 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100187 /*
188 * some CR are missing...
189 * the inventory data is most probably corrupted
190 */
Heiko Schocher8f64da72008-10-15 09:41:00 +0200191 xcode = -1;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100192 printf("not enough cr found\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200193 }
194 return xcode;
195}
196
197#define GET_STRING(name, which, len) \
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100198 if (ivm_findinventorystring(which, valbuf, len, buf) == 0) { \
199 ivm_set_value(name, (char *)valbuf); \
Heiko Schocher8f64da72008-10-15 09:41:00 +0200200 }
201
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100202static int ivm_check_crc(unsigned char *buf, int block)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200203{
204 unsigned long crc;
205 unsigned long crceeprom;
206
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100207 crc = ivm_calc_crc(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200208 crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + \
209 buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200210 if (crc != crceeprom) {
Heiko Schocherdc71b242009-07-09 12:04:18 +0200211 if (block == 0)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100212 printf("Error CRC Block: %d EEprom: calculated: \
Heiko Schocherdc71b242009-07-09 12:04:18 +0200213 %lx EEprom: %lx\n", block, crc, crceeprom);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200214 return -1;
215 }
216 return 0;
217}
218
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100219static int ivm_analyze_block2(unsigned char *buf, int len)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200220{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200221 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
Heiko Schocher8f64da72008-10-15 09:41:00 +0200222 unsigned long count;
223
224 /* IVM_MacAddress */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100225 sprintf((char *)valbuf, "%pM", buf);
226 ivm_set_value("IVM_MacAddress", (char *)valbuf);
227 if (getenv("ethaddr") == NULL)
228 setenv((char *)"ethaddr", (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200229 /* 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;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100236 sprintf((char *)valbuf, "%lx", count);
237 ivm_set_value("IVM_MacCount", (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200238 return 0;
239}
240
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100241int ivm_analyze_eeprom(unsigned char *buf, int len)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200242{
243 unsigned short val;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200244 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
Heiko Schocher8f64da72008-10-15 09:41:00 +0200245 unsigned char *tmp;
246
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100247 if (ivm_check_crc(buf, 0) != 0)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200248 return -1;
249
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100250 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
251 "IVM_BoardId", 0, 1);
252 val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
253 "IVM_HWKey", 6, 1);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200254 if (val != 0xffff) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100255 sprintf((char *)valbuf, "%x", ((val / 100) % 10));
256 ivm_set_value("IVM_HWVariant", (char *)valbuf);
257 sprintf((char *)valbuf, "%x", (val % 100));
258 ivm_set_value("IVM_HWVersion", (char *)valbuf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200259 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100260 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
261 "IVM_Functions", 12, 0);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200262
263 GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
264 GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
265 tmp = (unsigned char *) getenv("IVM_DeviceName");
266 if (tmp) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100267 int len = strlen((char *)tmp);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200268 int i = 0;
269
270 while (i < len) {
271 if (tmp[i] == ';') {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100272 ivm_set_value("IVM_ShortText",
273 (char *)&tmp[i + 1]);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200274 break;
275 }
276 i++;
277 }
278 if (i >= len)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100279 ivm_set_value("IVM_ShortText", NULL);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200280 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100281 ivm_set_value("IVM_ShortText", NULL);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200282 }
283 GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
284 GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
285 GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
286 GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
287 GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
288 GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
289 GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
290 GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
291 GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
292
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100293 if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
Heiko Schocherdc71b242009-07-09 12:04:18 +0200294 return 0;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100295 ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2],
296 CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200297
298 return 0;
299}
300
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100301int ivm_read_eeprom(void)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200302{
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100303#if defined(CONFIG_I2C_MUX)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200304 I2C_MUX_DEVICE *dev = NULL;
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100305#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200306 uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
Heiko Schocher8f64da72008-10-15 09:41:00 +0200307 uchar *buf;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200308 unsigned dev_addr = CONFIG_SYS_IVM_EEPROM_ADR;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100309 int ret;
Heiko Schocher8f64da72008-10-15 09:41:00 +0200310
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100311#if defined(CONFIG_I2C_MUX)
Heiko Schocher8f64da72008-10-15 09:41:00 +0200312 /* First init the Bus, select the Bus */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200313#if defined(CONFIG_SYS_I2C_IVM_BUS)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100314 dev = i2c_mux_ident_muxstring((uchar *)CONFIG_SYS_I2C_IVM_BUS);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200315#else
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100316 buf = (unsigned char *) getenv("EEprom_ivm");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200317 if (buf != NULL)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100318 dev = i2c_mux_ident_muxstring(buf);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200319#endif
320 if (dev == NULL) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100321 printf("Error couldnt add Bus for IVM\n");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200322 return -1;
323 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100324 i2c_set_bus_num(dev->busid);
Heiko Schocher1b6275d2009-03-12 07:37:34 +0100325#endif
Heiko Schocher8f64da72008-10-15 09:41:00 +0200326
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100327 buf = (unsigned char *) getenv("EEprom_ivm_addr");
Heiko Schocher8f64da72008-10-15 09:41:00 +0200328 if (buf != NULL)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100329 dev_addr = simple_strtoul((char *)buf, NULL, 16);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200330
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100331 ret = i2c_read(dev_addr, 0, 1, i2c_buffer,
332 CONFIG_SYS_IVM_EEPROM_MAX_LEN);
333 if (ret != 0) {
Heiko Schocher8f64da72008-10-15 09:41:00 +0200334 printf ("Error reading EEprom\n");
335 return -2;
336 }
337
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100338 return ivm_analyze_eeprom(i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
Heiko Schocher8f64da72008-10-15 09:41:00 +0200339}
340
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200341#if defined(CONFIG_SYS_I2C_INIT_BOARD)
Heiko Schocherc2485362008-10-15 09:39:08 +0200342#define DELAY_ABORT_SEQ 62
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200343#define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
Heiko Schocherc2485362008-10-15 09:39:08 +0200344
345#if defined(CONFIG_MGCOGE)
346#define SDA_MASK 0x00010000
347#define SCL_MASK 0x00020000
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100348static void set_pin(int state, unsigned long mask)
Heiko Schocherc2485362008-10-15 09:39:08 +0200349{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100350 ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocherc2485362008-10-15 09:39:08 +0200351
352 if (state)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100353 setbits_be32(&iop->pdat, mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200354 else
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100355 clrbits_be32(&iop->pdat, mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200356
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100357 setbits_be32(&iop->pdir, mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200358}
359
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100360static int get_pin(unsigned long mask)
Heiko Schocherc2485362008-10-15 09:39:08 +0200361{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100362 ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocherc2485362008-10-15 09:39:08 +0200363
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100364 clrbits_be32(&iop->pdir, mask);
365 return 0 != (in_be32(&iop->pdat) & mask);
Heiko Schocherc2485362008-10-15 09:39:08 +0200366}
367
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100368static void set_sda(int state)
Heiko Schocherc2485362008-10-15 09:39:08 +0200369{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100370 set_pin(state, SDA_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200371}
372
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100373static void set_scl(int state)
Heiko Schocherc2485362008-10-15 09:39:08 +0200374{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100375 set_pin(state, SCL_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200376}
377
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100378static int get_sda(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200379{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100380 return get_pin(SDA_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200381}
382
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100383static int get_scl(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200384{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100385 return get_pin(SCL_MASK);
Heiko Schocherc2485362008-10-15 09:39:08 +0200386}
387
388#if defined(CONFIG_HARD_I2C)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100389static void setports(int gpio)
Heiko Schocherc2485362008-10-15 09:39:08 +0200390{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100391 ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
Heiko Schocherc2485362008-10-15 09:39:08 +0200392
393 if (gpio) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100394 clrbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
395 clrbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
Heiko Schocherc2485362008-10-15 09:39:08 +0200396 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100397 setbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
398 clrbits_be32(&iop->pdir, (SDA_MASK | SCL_MASK));
399 setbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
Heiko Schocherc2485362008-10-15 09:39:08 +0200400 }
401}
402#endif
403#endif
404
Heiko Schocher39df00d2009-07-09 12:04:26 +0200405#if !defined(CONFIG_KMETER1)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100406static void writeStartSeq(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200407{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100408 set_sda(1);
409 udelay(DELAY_HALF_PERIOD);
410 set_scl(1);
411 udelay(DELAY_HALF_PERIOD);
412 set_sda(0);
413 udelay(DELAY_HALF_PERIOD);
414 set_scl(0);
415 udelay(DELAY_HALF_PERIOD);
Heiko Schocherc2485362008-10-15 09:39:08 +0200416}
417
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100418/*
419 * I2C is a synchronous protocol and resets of the processor in the middle
420 * of an access can block the I2C Bus until a powerdown of the full unit is
421 * done. This function toggles the SCL until the SCL and SCA line are
422 * released, but max. 16 times, after this a I2C start-sequence is sent.
423 * This I2C Deblocking mechanism was developed by Keymile in association
424 * with Anatech and Atmel in 1998.
Heiko Schocherc2485362008-10-15 09:39:08 +0200425 */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100426static int i2c_make_abort(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200427{
428 int scl_state = 0;
429 int sda_state = 0;
430 int i = 0;
431 int ret = 0;
432
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100433 if (!get_sda()) {
Heiko Schocherc2485362008-10-15 09:39:08 +0200434 ret = -1;
435 while (i < 16) {
436 i++;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100437 set_scl(0);
438 udelay(DELAY_ABORT_SEQ);
439 set_scl(1);
440 udelay(DELAY_ABORT_SEQ);
441 scl_state = get_scl();
442 sda_state = get_sda();
Heiko Schocherc2485362008-10-15 09:39:08 +0200443 if (scl_state && sda_state) {
444 ret = 0;
445 break;
446 }
447 }
448 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100449 if (ret == 0)
450 for (i = 0; i < 5; i++)
451 writeStartSeq();
452
453 get_sda();
Heiko Schocherc2485362008-10-15 09:39:08 +0200454 return ret;
455}
Heiko Schocher39df00d2009-07-09 12:04:26 +0200456#endif
Heiko Schocherc2485362008-10-15 09:39:08 +0200457
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100458/*
Heiko Schocherc2485362008-10-15 09:39:08 +0200459 * i2c_init_board - reset i2c bus. When the board is powercycled during a
460 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
461 */
462void i2c_init_board(void)
463{
Heiko Schocher39df00d2009-07-09 12:04:26 +0200464#if defined(CONFIG_KMETER1)
465 struct fsl_i2c *dev;
466 dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
467 uchar dummy;
468
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100469 out_8(&dev->cr, (I2C_CR_MSTA));
470 out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
Heiko Schocher39df00d2009-07-09 12:04:26 +0200471 dummy = in_8(&dev->dr);
472 dummy = in_8(&dev->dr);
473 if (dummy != 0xff) {
474 dummy = in_8(&dev->dr);
475 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100476 out_8(&dev->cr, (I2C_CR_MEN));
477 out_8(&dev->cr, 0x00);
478 out_8(&dev->cr, (I2C_CR_MEN));
Heiko Schocher39df00d2009-07-09 12:04:26 +0200479#else
Holger Brunck802d9962011-03-14 15:31:19 +0100480#if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100481 immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
482 i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
Heiko Schocherc2485362008-10-15 09:39:08 +0200483
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100484 /*
485 * disable I2C controller first, otherwhise it thinks we want to
486 * talk to the slave port...
487 */
488 clrbits_8(&i2c->i2c_i2mod, 0x01);
Heiko Schocherc2485362008-10-15 09:39:08 +0200489
490 /* Set the PortPins to GPIO */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100491 setports(1);
Heiko Schocherc2485362008-10-15 09:39:08 +0200492#endif
493
494 /* Now run the AbortSequence() */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100495 i2c_make_abort();
Heiko Schocherc2485362008-10-15 09:39:08 +0200496
497#if defined(CONFIG_HARD_I2C)
498 /* Set the PortPins back to use for I2C */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100499 setports(0);
Heiko Schocherc2485362008-10-15 09:39:08 +0200500#endif
Heiko Schocher39df00d2009-07-09 12:04:26 +0200501#endif
Heiko Schocherc2485362008-10-15 09:39:08 +0200502}
503#endif
Heiko Schocher210c8c02008-11-21 08:29:40 +0100504#endif
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200505
506#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100507int fdt_set_node_and_value(void *blob,
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200508 char *nodename,
509 char *regname,
510 void *var,
511 int size)
512{
513 int ret = 0;
514 int nodeoffset = 0;
515
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100516 nodeoffset = fdt_path_offset(blob, nodename);
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200517 if (nodeoffset >= 0) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100518 ret = fdt_setprop(blob, nodeoffset, regname, var,
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200519 size);
520 if (ret < 0)
521 printf("ft_blob_update(): cannot set %s/%s "
522 "property err:%s\n", nodename, regname,
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100523 fdt_strerror(ret));
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200524 } else {
525 printf("ft_blob_update(): cannot find %s node "
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100526 "err:%s\n", nodename, fdt_strerror(nodeoffset));
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200527 }
528 return ret;
529}
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100530
531int fdt_get_node_and_value(void *blob,
Heiko Schocherdc71b242009-07-09 12:04:18 +0200532 char *nodename,
533 char *propname,
534 void **var)
535{
536 int len;
537 int nodeoffset = 0;
538
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100539 nodeoffset = fdt_path_offset(blob, nodename);
Heiko Schocherdc71b242009-07-09 12:04:18 +0200540 if (nodeoffset >= 0) {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100541 *var = (void *)fdt_getprop(blob, nodeoffset, propname, &len);
Heiko Schocherdc71b242009-07-09 12:04:18 +0200542 if (len == 0) {
543 /* no value */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100544 printf("%s no value\n", __func__);
Heiko Schocherdc71b242009-07-09 12:04:18 +0200545 return -1;
546 } else if (len > 0) {
547 return len;
548 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100549 printf("libfdt fdt_getprop(): %s\n",
Heiko Schocherdc71b242009-07-09 12:04:18 +0200550 fdt_strerror(len));
551 return -2;
552 }
553 } else {
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100554 printf("%s: cannot find %s node err:%s\n", __func__,
555 nodename, fdt_strerror(nodeoffset));
Heiko Schocherdc71b242009-07-09 12:04:18 +0200556 return -3;
557 }
558}
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200559#endif
Heiko Schocher210c8c02008-11-21 08:29:40 +0100560
Holger Brunck802d9962011-03-14 15:31:19 +0100561#if !defined(MACH_TYPE_KM_KIRKWOOD)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100562int ethernet_present(void)
Heiko Schocher210c8c02008-11-21 08:29:40 +0100563{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100564 struct km_bec_fpga *base = (struct km_bec_fpga *)CONFIG_SYS_PIGGY_BASE;
565
566 return in_8(&base->bprth) & PIGGY_PRESENT;
Heiko Schocher210c8c02008-11-21 08:29:40 +0100567}
Heiko Schocher67fa8c22010-02-22 16:43:02 +0530568#endif
Heiko Schocher210c8c02008-11-21 08:29:40 +0100569
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100570int board_eth_init(bd_t *bis)
Heiko Schocher210c8c02008-11-21 08:29:40 +0100571{
572#ifdef CONFIG_KEYMILE_HDLC_ENET
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100573 (void)keymile_hdlc_enet_initialize(bis);
Heiko Schocher210c8c02008-11-21 08:29:40 +0100574#endif
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100575 if (ethernet_present())
Heiko Schocher210c8c02008-11-21 08:29:40 +0100576 return -1;
Heiko Schocher210c8c02008-11-21 08:29:40 +0100577 return 0;
578}