blob: fce9d582f23c66ba5542ed2e94b1c95ec94cf42f [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 *
Holger Brunck4f745bf2011-06-05 22:22:18 +00005 * (C) Copyright 2011
6 * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
7 *
Heiko Schocherc2485362008-10-15 09:39:08 +02008 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
Heiko Schocherc2485362008-10-15 09:39:08 +020028#include <ioports.h>
Holger Bruncka9417ce2011-05-04 01:47:30 +000029#include <command.h>
Heiko Schocherc2485362008-10-15 09:39:08 +020030#include <malloc.h>
Heiko Schocher8f64da72008-10-15 09:41:00 +020031#include <hush.h>
Heiko Schocher210c8c02008-11-21 08:29:40 +010032#include <net.h>
Heiko Schocher62ddcf02010-02-18 08:08:25 +010033#include <netdev.h>
Heiko Schocher210c8c02008-11-21 08:29:40 +010034#include <asm/io.h>
Thomas Herzmann92c91082011-05-12 19:59:22 +000035#include <linux/ctype.h>
Heiko Schocherc2485362008-10-15 09:39:08 +020036
37#if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
38#include <libfdt.h>
39#endif
40
Holger Brunck4f745bf2011-06-05 22:22:18 +000041#include "common.h"
Heiko Schocherc2485362008-10-15 09:39:08 +020042#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
43#include <i2c.h>
Holger Brunck1adfd9d2011-06-05 22:22:20 +000044#endif
Heiko Schocherc2485362008-10-15 09:39:08 +020045
Heiko Schocher6c11aea2011-03-08 10:51:15 +010046static void i2c_write_start_seq(void);
Heiko Schocherf1fef1d2010-04-26 13:07:28 +020047DECLARE_GLOBAL_DATA_PTR;
Heiko Schocher6c11aea2011-03-08 10:51:15 +010048
Heiko Schocherf1fef1d2010-04-26 13:07:28 +020049/*
50 * Set Keymile specific environment variables
51 * Currently only some memory layout variables are calculated here
52 * ... ------------------------------------------------
53 * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM
54 * ... |<------------------- pram ------------------->|
55 * ... ------------------------------------------------
56 * @END_OF_RAM: denotes the RAM size
57 * @pnvramaddr: Startadress of pseudo non volatile RAM in hex
58 * @pram : preserved ram size in k
59 * @varaddr : startadress for /var mounted into RAM
60 */
61int set_km_env(void)
62{
63 uchar buf[32];
64 unsigned int pnvramaddr;
65 unsigned int pram;
66 unsigned int varaddr;
67
68 pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
69 - CONFIG_KM_PNVRAM;
70 sprintf((char *)buf, "0x%x", pnvramaddr);
71 setenv("pnvramaddr", (char *)buf);
72
73 pram = (CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM + CONFIG_KM_PNVRAM) /
74 0x400;
75 sprintf((char *)buf, "0x%x", pram);
76 setenv("pram", (char *)buf);
77
78 varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
79 sprintf((char *)buf, "0x%x", varaddr);
80 setenv("varaddr", (char *)buf);
81 return 0;
82}
83
Heiko Schocher6c11aea2011-03-08 10:51:15 +010084#define DELAY_ABORT_SEQ 62 /* @200kHz 9 clocks = 44us, 62us is ok */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020085#define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
Heiko Schocherc2485362008-10-15 09:39:08 +020086
Heiko Schocher62ddcf02010-02-18 08:08:25 +010087#if !defined(CONFIG_MPC83xx)
Heiko Schocher6c11aea2011-03-08 10:51:15 +010088static void i2c_write_start_seq(void)
Heiko Schocherc2485362008-10-15 09:39:08 +020089{
Heiko Schocherb11f53f2011-03-15 16:52:29 +010090 set_sda(1);
91 udelay(DELAY_HALF_PERIOD);
92 set_scl(1);
93 udelay(DELAY_HALF_PERIOD);
94 set_sda(0);
95 udelay(DELAY_HALF_PERIOD);
96 set_scl(0);
97 udelay(DELAY_HALF_PERIOD);
Heiko Schocherc2485362008-10-15 09:39:08 +020098}
99
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100100/*
101 * I2C is a synchronous protocol and resets of the processor in the middle
102 * of an access can block the I2C Bus until a powerdown of the full unit is
103 * done. This function toggles the SCL until the SCL and SCA line are
104 * released, but max. 16 times, after this a I2C start-sequence is sent.
105 * This I2C Deblocking mechanism was developed by Keymile in association
106 * with Anatech and Atmel in 1998.
Heiko Schocherc2485362008-10-15 09:39:08 +0200107 */
Holger Brunck4f745bf2011-06-05 22:22:18 +0000108int i2c_make_abort(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200109{
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100110
111#if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
112 immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
113 i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
114
115 /*
116 * disable I2C controller first, otherwhise it thinks we want to
117 * talk to the slave port...
118 */
119 clrbits_8(&i2c->i2c_i2mod, 0x01);
120
121 /* Set the PortPins to GPIO */
122 setports(1);
123#endif
124
Heiko Schocherc2485362008-10-15 09:39:08 +0200125 int scl_state = 0;
126 int sda_state = 0;
127 int i = 0;
128 int ret = 0;
129
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100130 if (!get_sda()) {
Heiko Schocherc2485362008-10-15 09:39:08 +0200131 ret = -1;
132 while (i < 16) {
133 i++;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100134 set_scl(0);
135 udelay(DELAY_ABORT_SEQ);
136 set_scl(1);
137 udelay(DELAY_ABORT_SEQ);
138 scl_state = get_scl();
139 sda_state = get_sda();
Heiko Schocherc2485362008-10-15 09:39:08 +0200140 if (scl_state && sda_state) {
141 ret = 0;
Stefan Biglerc7506c22011-07-04 22:24:02 +0000142 printf("[INFO] i2c abort after %d clocks\n", i);
Heiko Schocherc2485362008-10-15 09:39:08 +0200143 break;
144 }
145 }
146 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100147 if (ret == 0)
148 for (i = 0; i < 5; i++)
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100149 i2c_write_start_seq();
Stefan Biglerc7506c22011-07-04 22:24:02 +0000150 else
151 printf("[ERROR] i2c abort failed\n");
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100152
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100153 /* respect stop setup time */
154 udelay(DELAY_ABORT_SEQ);
155 set_scl(1);
156 udelay(DELAY_ABORT_SEQ);
157 set_sda(1);
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100158 get_sda();
Heiko Schocherc2485362008-10-15 09:39:08 +0200159
160#if defined(CONFIG_HARD_I2C)
161 /* Set the PortPins back to use for I2C */
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100162 setports(0);
Heiko Schocherc2485362008-10-15 09:39:08 +0200163#endif
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100164 return ret;
165}
Holger Brunck1adfd9d2011-06-05 22:22:20 +0000166#endif /* !MPC83xx */
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100167
168#if defined(CONFIG_MPC83xx)
169static void i2c_write_start_seq(void)
170{
171 struct fsl_i2c *dev;
172 dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
173 udelay(DELAY_ABORT_SEQ);
174 out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
175 udelay(DELAY_ABORT_SEQ);
176 out_8(&dev->cr, (I2C_CR_MEN));
177}
178
Holger Brunck4f745bf2011-06-05 22:22:18 +0000179int i2c_make_abort(void)
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100180{
181 struct fsl_i2c *dev;
182 dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
183 uchar dummy;
184 uchar last;
185 int nbr_read = 0;
186 int i = 0;
187 int ret = 0;
188
189 /* wait after each operation to finsh with a delay */
190 out_8(&dev->cr, (I2C_CR_MSTA));
191 udelay(DELAY_ABORT_SEQ);
192 out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
193 udelay(DELAY_ABORT_SEQ);
194 dummy = in_8(&dev->dr);
195 udelay(DELAY_ABORT_SEQ);
196 last = in_8(&dev->dr);
197 nbr_read++;
198
199 /*
200 * do read until the last bit is 1, but stop if the full eeprom is
201 * read.
202 */
203 while (((last & 0x01) != 0x01) &&
204 (nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) {
205 udelay(DELAY_ABORT_SEQ);
206 last = in_8(&dev->dr);
207 nbr_read++;
208 }
209 if ((last & 0x01) != 0x01)
210 ret = -2;
211 if ((last != 0xff) || (nbr_read > 1))
212 printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
213 nbr_read, last);
214 udelay(DELAY_ABORT_SEQ);
215 out_8(&dev->cr, (I2C_CR_MEN));
216 udelay(DELAY_ABORT_SEQ);
217 /* clear status reg */
218 out_8(&dev->sr, 0);
219
220 for (i = 0; i < 5; i++)
221 i2c_write_start_seq();
222 if (ret != 0)
223 printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
224 nbr_read, last);
225
226 return ret;
227}
228#endif
229
230/**
231 * i2c_init_board - reset i2c bus. When the board is powercycled during a
232 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
233 */
234void i2c_init_board(void)
235{
236 /* Now run the AbortSequence() */
237 i2c_make_abort();
Heiko Schocherc2485362008-10-15 09:39:08 +0200238}
Heiko Schocher6250f0f2008-10-17 16:11:52 +0200239
Holger Brunck802d9962011-03-14 15:31:19 +0100240#if !defined(MACH_TYPE_KM_KIRKWOOD)
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100241int ethernet_present(void)
Heiko Schocher210c8c02008-11-21 08:29:40 +0100242{
Heiko Schocher8ed74342011-03-08 10:47:39 +0100243 struct km_bec_fpga *base =
244 (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100245
246 return in_8(&base->bprth) & PIGGY_PRESENT;
Heiko Schocher210c8c02008-11-21 08:29:40 +0100247}
Heiko Schocher67fa8c22010-02-22 16:43:02 +0530248#endif
Heiko Schocher210c8c02008-11-21 08:29:40 +0100249
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100250int board_eth_init(bd_t *bis)
Heiko Schocher210c8c02008-11-21 08:29:40 +0100251{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100252 if (ethernet_present())
Heiko Schocher62ddcf02010-02-18 08:08:25 +0100253 return cpu_eth_init(bis);
254
255 return -1;
Heiko Schocher210c8c02008-11-21 08:29:40 +0100256}
Holger Bruncka9417ce2011-05-04 01:47:30 +0000257
258/*
259 * do_setboardid command
260 * read out the board id and the hw key from the intventory EEPROM and set
261 * this values as environment variables.
262 */
263static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
264 char *const argv[])
265{
266 unsigned char buf[32];
267 char *p;
268
269 p = get_local_var("IVM_BoardId");
270 if (p == NULL) {
271 printf("can't get the IVM_Boardid\n");
272 return 1;
273 }
274 sprintf((char *)buf, "%s", p);
275 setenv("boardid", (char *)buf);
Holger Brunck9485e772011-07-04 22:23:59 +0000276 printf("set boardid=%s\n", buf);
Holger Bruncka9417ce2011-05-04 01:47:30 +0000277
278 p = get_local_var("IVM_HWKey");
279 if (p == NULL) {
280 printf("can't get the IVM_HWKey\n");
281 return 1;
282 }
283 sprintf((char *)buf, "%s", p);
284 setenv("hwkey", (char *)buf);
Holger Brunck9485e772011-07-04 22:23:59 +0000285 printf("set hwkey=%s\n", buf);
286 printf("Execute manually saveenv for persistent storage.\n");
Holger Bruncka9417ce2011-05-04 01:47:30 +0000287
288 return 0;
289}
290
291U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
292 "hwkey from IVM and set in environment");
Thomas Herzmann92c91082011-05-12 19:59:22 +0000293
294/*
295 * command km_checkbidhwk
296 * if "boardid" and "hwkey" are not already set in the environment, do:
297 * if a "boardIdListHex" exists in the environment:
298 * - read ivm data for boardid and hwkey
299 * - compare each entry of the boardIdListHex with the
300 * IVM data:
301 * if match:
302 * set environment variables boardid, boardId,
303 * hwkey, hwKey to the found values
304 * both (boardid and boardId) are set because
305 * they might be used differently in the
306 * application and in the init scripts (?)
307 * return 0 in case of match, 1 if not match or error
308 */
309int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
310 char *const argv[])
311{
312 unsigned long ivmbid = 0, ivmhwkey = 0;
313 unsigned long envbid = 0, envhwkey = 0;
314 char *p;
315 int verbose = argc > 1 && *argv[1] == 'v';
316 int rc = 0;
317
318 /*
319 * first read out the real inventory values, these values are
320 * already stored in the local hush variables
321 */
322 p = get_local_var("IVM_BoardId");
323 if (p == NULL) {
324 printf("can't get the IVM_Boardid\n");
325 return 1;
326 }
327 rc = strict_strtoul(p, 16, &ivmbid);
328
329 p = get_local_var("IVM_HWKey");
330 if (p == NULL) {
331 printf("can't get the IVM_HWKey\n");
332 return 1;
333 }
334 rc = strict_strtoul(p, 16, &ivmhwkey);
335
336 if (!ivmbid || !ivmhwkey) {
337 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
338 return rc;
339 }
340
341 /* now try to read values from environment if available */
342 p = getenv("boardid");
343 if (p != NULL)
344 rc = strict_strtoul(p, 16, &envbid);
345 p = getenv("hwkey");
346 if (p != NULL)
347 rc = strict_strtoul(p, 16, &envhwkey);
348
349 if (rc != 0) {
350 printf("strict_strtoul returns error: %d", rc);
351 return rc;
352 }
353
354 if (!envbid || !envhwkey) {
355 /*
356 * BoardId/HWkey not available in the environment, so try the
357 * environment variable for BoardId/HWkey list
358 */
359 char *bidhwklist = getenv("boardIdListHex");
360
361 if (bidhwklist) {
362 int found = 0;
363 char *rest = bidhwklist;
364 char *endp;
365
366 if (verbose) {
367 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
368 ivmbid, ivmhwkey);
369 printf("boardIdHwKeyList: %s\n",
370 bidhwklist);
371 }
372 while (!found) {
373 /* loop over each bid/hwkey pair in the list */
374 unsigned long bid = 0;
375 unsigned long hwkey = 0;
376
377 while (*rest && !isxdigit(*rest))
378 rest++;
379 /*
380 * use simple_strtoul because we need &end and
381 * we know we got non numeric char at the end
382 */
383 bid = simple_strtoul(rest, &endp, 16);
384 /* BoardId and HWkey are separated with a "_" */
385 if (*endp == '_') {
386 rest = endp + 1;
387 /*
388 * use simple_strtoul because we need
389 * &end
390 */
391 hwkey = simple_strtoul(rest, &endp, 16);
392 rest = endp;
393 while (*rest && !isxdigit(*rest))
394 rest++;
395 }
396 if ((!bid) || (!hwkey)) {
397 /* end of list */
398 break;
399 }
400 if (verbose) {
401 printf("trying bid=0x%lX, hwkey=%ld\n",
402 bid, hwkey);
403 }
404 /*
405 * Compare the values of the found entry in the
406 * list with the valid values which are stored
407 * in the inventory eeprom. If they are equal
Holger Brunckba8be322011-06-05 22:22:17 +0000408 * set the values in environment variables.
Thomas Herzmann92c91082011-05-12 19:59:22 +0000409 */
410 if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
411 char buf[10];
412
413 found = 1;
414 envbid = bid;
415 envhwkey = hwkey;
416 sprintf(buf, "%lx", bid);
417 setenv("boardid", buf);
418 sprintf(buf, "%lx", hwkey);
419 setenv("hwkey", buf);
Thomas Herzmann92c91082011-05-12 19:59:22 +0000420 }
421 } /* end while( ! found ) */
422 }
423 }
424
425 /* compare now the values */
426 if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
427 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
428 rc = 0; /* match */
429 } else {
Holger Brunck9485e772011-07-04 22:23:59 +0000430 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
431 envhwkey);
Thomas Herzmann92c91082011-05-12 19:59:22 +0000432 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
433 rc = 1; /* don't match */
434 }
435 return rc;
436}
437
438U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
439 "check boardid and hwkey",
440 "[v]\n - check environment parameter "\
441 "\"boardIdListHex\" against stored boardid and hwkey "\
442 "from the IVM\n v: verbose output"
443);