blob: 612dd2a051009df23ccae05f5ab8b5ed9c71ce68 [file] [log] [blame]
Heiko Schocher5e51b482008-10-15 09:39:08 +02001/*
2 * (C) Copyright 2008
3 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
4 *
Holger Brunckbf20a882011-06-05 22:22:18 +00005 * (C) Copyright 2011
6 * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
7 *
Heiko Schocher5e51b482008-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 Schocher5e51b482008-10-15 09:39:08 +020028#include <ioports.h>
Holger Brunck30664922011-05-04 01:47:30 +000029#include <command.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020030#include <malloc.h>
Heiko Schochere5b6c2e2008-10-15 09:41:00 +020031#include <hush.h>
Heiko Schocherd19a6ec2008-11-21 08:29:40 +010032#include <net.h>
Heiko Schocher466924f2010-02-18 08:08:25 +010033#include <netdev.h>
Heiko Schocherd19a6ec2008-11-21 08:29:40 +010034#include <asm/io.h>
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +000035#include <linux/ctype.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020036
Holger Brunckbf20a882011-06-05 22:22:18 +000037#include "common.h"
Heiko Schocher5e51b482008-10-15 09:39:08 +020038#if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
39#include <i2c.h>
Holger Brunck14b39b82011-06-05 22:22:20 +000040#endif
Heiko Schocher5e51b482008-10-15 09:39:08 +020041
Heiko Schocherd9e10952011-03-08 10:51:15 +010042static void i2c_write_start_seq(void);
Heiko Schochercfc58042010-04-26 13:07:28 +020043DECLARE_GLOBAL_DATA_PTR;
Heiko Schocherd9e10952011-03-08 10:51:15 +010044
Heiko Schochercfc58042010-04-26 13:07:28 +020045/*
46 * Set Keymile specific environment variables
47 * Currently only some memory layout variables are calculated here
48 * ... ------------------------------------------------
49 * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM
50 * ... |<------------------- pram ------------------->|
51 * ... ------------------------------------------------
52 * @END_OF_RAM: denotes the RAM size
53 * @pnvramaddr: Startadress of pseudo non volatile RAM in hex
54 * @pram : preserved ram size in k
55 * @varaddr : startadress for /var mounted into RAM
56 */
57int set_km_env(void)
58{
59 uchar buf[32];
60 unsigned int pnvramaddr;
61 unsigned int pram;
62 unsigned int varaddr;
Andreas Huber28c63c72011-09-13 23:06:11 +000063 unsigned int kernelmem;
64 char *p;
65 unsigned long rootfssize = 0;
Heiko Schochercfc58042010-04-26 13:07:28 +020066
67 pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
68 - CONFIG_KM_PNVRAM;
69 sprintf((char *)buf, "0x%x", pnvramaddr);
70 setenv("pnvramaddr", (char *)buf);
71
Andreas Huber28c63c72011-09-13 23:06:11 +000072 /* try to read rootfssize (ram image) from envrionment */
73 p = getenv("rootfssize");
74 if (p != NULL)
75 strict_strtoul(p, 16, &rootfssize);
76 pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
77 CONFIG_KM_PNVRAM) / 0x400;
Heiko Schochercfc58042010-04-26 13:07:28 +020078 sprintf((char *)buf, "0x%x", pram);
79 setenv("pram", (char *)buf);
80
81 varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
82 sprintf((char *)buf, "0x%x", varaddr);
83 setenv("varaddr", (char *)buf);
Andreas Huber28c63c72011-09-13 23:06:11 +000084
85 kernelmem = gd->ram_size - 0x400 * pram;
86 sprintf((char *)buf, "0x%x", kernelmem);
87 setenv("kernelmem", (char *)buf);
88
Heiko Schochercfc58042010-04-26 13:07:28 +020089 return 0;
90}
91
Heiko Schocherd9e10952011-03-08 10:51:15 +010092#define DELAY_ABORT_SEQ 62 /* @200kHz 9 clocks = 44us, 62us is ok */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020093#define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
Heiko Schocher5e51b482008-10-15 09:39:08 +020094
Heiko Schocher466924f2010-02-18 08:08:25 +010095#if !defined(CONFIG_MPC83xx)
Heiko Schocherd9e10952011-03-08 10:51:15 +010096static void i2c_write_start_seq(void)
Heiko Schocher5e51b482008-10-15 09:39:08 +020097{
Heiko Schocher8ce3dd52011-03-15 16:52:29 +010098 set_sda(1);
99 udelay(DELAY_HALF_PERIOD);
100 set_scl(1);
101 udelay(DELAY_HALF_PERIOD);
102 set_sda(0);
103 udelay(DELAY_HALF_PERIOD);
104 set_scl(0);
105 udelay(DELAY_HALF_PERIOD);
Heiko Schocher5e51b482008-10-15 09:39:08 +0200106}
107
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100108/*
109 * I2C is a synchronous protocol and resets of the processor in the middle
110 * of an access can block the I2C Bus until a powerdown of the full unit is
111 * done. This function toggles the SCL until the SCL and SCA line are
112 * released, but max. 16 times, after this a I2C start-sequence is sent.
113 * This I2C Deblocking mechanism was developed by Keymile in association
114 * with Anatech and Atmel in 1998.
Heiko Schocher5e51b482008-10-15 09:39:08 +0200115 */
Holger Brunckbf20a882011-06-05 22:22:18 +0000116int i2c_make_abort(void)
Heiko Schocher5e51b482008-10-15 09:39:08 +0200117{
Heiko Schocherd9e10952011-03-08 10:51:15 +0100118
119#if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
120 immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
121 i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
122
123 /*
124 * disable I2C controller first, otherwhise it thinks we want to
125 * talk to the slave port...
126 */
127 clrbits_8(&i2c->i2c_i2mod, 0x01);
128
129 /* Set the PortPins to GPIO */
130 setports(1);
131#endif
132
Heiko Schocher5e51b482008-10-15 09:39:08 +0200133 int scl_state = 0;
134 int sda_state = 0;
135 int i = 0;
136 int ret = 0;
137
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100138 if (!get_sda()) {
Heiko Schocher5e51b482008-10-15 09:39:08 +0200139 ret = -1;
140 while (i < 16) {
141 i++;
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100142 set_scl(0);
143 udelay(DELAY_ABORT_SEQ);
144 set_scl(1);
145 udelay(DELAY_ABORT_SEQ);
146 scl_state = get_scl();
147 sda_state = get_sda();
Heiko Schocher5e51b482008-10-15 09:39:08 +0200148 if (scl_state && sda_state) {
149 ret = 0;
Stefan Bigler5b604ed2011-07-04 22:24:02 +0000150 printf("[INFO] i2c abort after %d clocks\n", i);
Heiko Schocher5e51b482008-10-15 09:39:08 +0200151 break;
152 }
153 }
154 }
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100155 if (ret == 0)
156 for (i = 0; i < 5; i++)
Heiko Schocherd9e10952011-03-08 10:51:15 +0100157 i2c_write_start_seq();
Stefan Bigler5b604ed2011-07-04 22:24:02 +0000158 else
159 printf("[ERROR] i2c abort failed\n");
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100160
Heiko Schocherd9e10952011-03-08 10:51:15 +0100161 /* respect stop setup time */
162 udelay(DELAY_ABORT_SEQ);
163 set_scl(1);
164 udelay(DELAY_ABORT_SEQ);
165 set_sda(1);
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100166 get_sda();
Heiko Schocherd9e10952011-03-08 10:51:15 +0100167
168#if defined(CONFIG_HARD_I2C)
169 /* Set the PortPins back to use for I2C */
170 setports(0);
171#endif
Heiko Schocher5e51b482008-10-15 09:39:08 +0200172 return ret;
173}
Holger Brunck14b39b82011-06-05 22:22:20 +0000174#endif /* !MPC83xx */
Heiko Schocher5e51b482008-10-15 09:39:08 +0200175
Heiko Schocher466924f2010-02-18 08:08:25 +0100176#if defined(CONFIG_MPC83xx)
Heiko Schocherd9e10952011-03-08 10:51:15 +0100177static void i2c_write_start_seq(void)
178{
Heiko Schocherc5ca01f2009-07-09 12:04:26 +0200179 struct fsl_i2c *dev;
180 dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
Heiko Schocherd9e10952011-03-08 10:51:15 +0100181 udelay(DELAY_ABORT_SEQ);
182 out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
183 udelay(DELAY_ABORT_SEQ);
184 out_8(&dev->cr, (I2C_CR_MEN));
185}
186
Holger Brunckbf20a882011-06-05 22:22:18 +0000187int i2c_make_abort(void)
Heiko Schocherd9e10952011-03-08 10:51:15 +0100188{
189 struct fsl_i2c *dev;
190 dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
Heiko Schocherc5ca01f2009-07-09 12:04:26 +0200191 uchar dummy;
Heiko Schocherd9e10952011-03-08 10:51:15 +0100192 uchar last;
193 int nbr_read = 0;
194 int i = 0;
195 int ret = 0;
Heiko Schocherc5ca01f2009-07-09 12:04:26 +0200196
Heiko Schocherd9e10952011-03-08 10:51:15 +0100197 /* wait after each operation to finsh with a delay */
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100198 out_8(&dev->cr, (I2C_CR_MSTA));
Heiko Schocherd9e10952011-03-08 10:51:15 +0100199 udelay(DELAY_ABORT_SEQ);
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100200 out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
Heiko Schocherd9e10952011-03-08 10:51:15 +0100201 udelay(DELAY_ABORT_SEQ);
Heiko Schocherc5ca01f2009-07-09 12:04:26 +0200202 dummy = in_8(&dev->dr);
Heiko Schocherd9e10952011-03-08 10:51:15 +0100203 udelay(DELAY_ABORT_SEQ);
204 last = in_8(&dev->dr);
205 nbr_read++;
Heiko Schocher5e51b482008-10-15 09:39:08 +0200206
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100207 /*
Heiko Schocherd9e10952011-03-08 10:51:15 +0100208 * do read until the last bit is 1, but stop if the full eeprom is
209 * read.
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100210 */
Heiko Schocherd9e10952011-03-08 10:51:15 +0100211 while (((last & 0x01) != 0x01) &&
212 (nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) {
213 udelay(DELAY_ABORT_SEQ);
214 last = in_8(&dev->dr);
215 nbr_read++;
216 }
217 if ((last & 0x01) != 0x01)
218 ret = -2;
219 if ((last != 0xff) || (nbr_read > 1))
220 printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
221 nbr_read, last);
222 udelay(DELAY_ABORT_SEQ);
223 out_8(&dev->cr, (I2C_CR_MEN));
224 udelay(DELAY_ABORT_SEQ);
225 /* clear status reg */
226 out_8(&dev->sr, 0);
Heiko Schocher5e51b482008-10-15 09:39:08 +0200227
Heiko Schocherd9e10952011-03-08 10:51:15 +0100228 for (i = 0; i < 5; i++)
229 i2c_write_start_seq();
230 if (ret != 0)
231 printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
232 nbr_read, last);
233
234 return ret;
235}
Heiko Schocher5e51b482008-10-15 09:39:08 +0200236#endif
237
Heiko Schocherd9e10952011-03-08 10:51:15 +0100238/**
239 * i2c_init_board - reset i2c bus. When the board is powercycled during a
240 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
241 */
242void i2c_init_board(void)
243{
Heiko Schocher5e51b482008-10-15 09:39:08 +0200244 /* Now run the AbortSequence() */
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100245 i2c_make_abort();
Heiko Schocher5e51b482008-10-15 09:39:08 +0200246}
Heiko Schocher1eedd3f2008-10-17 16:11:52 +0200247
Holger Brunck34108f12011-03-14 15:31:19 +0100248#if !defined(MACH_TYPE_KM_KIRKWOOD)
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100249int ethernet_present(void)
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100250{
Heiko Schocher3a8dd212011-03-08 10:47:39 +0100251 struct km_bec_fpga *base =
252 (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100253
254 return in_8(&base->bprth) & PIGGY_PRESENT;
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100255}
Heiko Schocher60301192010-02-22 16:43:02 +0530256#endif
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100257
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100258int board_eth_init(bd_t *bis)
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100259{
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100260 if (ethernet_present())
Heiko Schocher466924f2010-02-18 08:08:25 +0100261 return cpu_eth_init(bis);
262
263 return -1;
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100264}
Holger Brunck30664922011-05-04 01:47:30 +0000265
266/*
267 * do_setboardid command
268 * read out the board id and the hw key from the intventory EEPROM and set
269 * this values as environment variables.
270 */
271static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
272 char *const argv[])
273{
274 unsigned char buf[32];
275 char *p;
276
277 p = get_local_var("IVM_BoardId");
278 if (p == NULL) {
279 printf("can't get the IVM_Boardid\n");
280 return 1;
281 }
282 sprintf((char *)buf, "%s", p);
283 setenv("boardid", (char *)buf);
Holger Brunckde799842011-07-04 22:23:59 +0000284 printf("set boardid=%s\n", buf);
Holger Brunck30664922011-05-04 01:47:30 +0000285
286 p = get_local_var("IVM_HWKey");
287 if (p == NULL) {
288 printf("can't get the IVM_HWKey\n");
289 return 1;
290 }
291 sprintf((char *)buf, "%s", p);
292 setenv("hwkey", (char *)buf);
Holger Brunckde799842011-07-04 22:23:59 +0000293 printf("set hwkey=%s\n", buf);
294 printf("Execute manually saveenv for persistent storage.\n");
Holger Brunck30664922011-05-04 01:47:30 +0000295
296 return 0;
297}
298
299U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
300 "hwkey from IVM and set in environment");
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000301
302/*
303 * command km_checkbidhwk
304 * if "boardid" and "hwkey" are not already set in the environment, do:
305 * if a "boardIdListHex" exists in the environment:
306 * - read ivm data for boardid and hwkey
307 * - compare each entry of the boardIdListHex with the
308 * IVM data:
309 * if match:
310 * set environment variables boardid, boardId,
311 * hwkey, hwKey to the found values
312 * both (boardid and boardId) are set because
313 * they might be used differently in the
314 * application and in the init scripts (?)
315 * return 0 in case of match, 1 if not match or error
316 */
317int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
318 char *const argv[])
319{
320 unsigned long ivmbid = 0, ivmhwkey = 0;
321 unsigned long envbid = 0, envhwkey = 0;
322 char *p;
323 int verbose = argc > 1 && *argv[1] == 'v';
324 int rc = 0;
325
326 /*
327 * first read out the real inventory values, these values are
328 * already stored in the local hush variables
329 */
330 p = get_local_var("IVM_BoardId");
331 if (p == NULL) {
332 printf("can't get the IVM_Boardid\n");
333 return 1;
334 }
335 rc = strict_strtoul(p, 16, &ivmbid);
336
337 p = get_local_var("IVM_HWKey");
338 if (p == NULL) {
339 printf("can't get the IVM_HWKey\n");
340 return 1;
341 }
342 rc = strict_strtoul(p, 16, &ivmhwkey);
343
344 if (!ivmbid || !ivmhwkey) {
345 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
346 return rc;
347 }
348
349 /* now try to read values from environment if available */
350 p = getenv("boardid");
351 if (p != NULL)
352 rc = strict_strtoul(p, 16, &envbid);
353 p = getenv("hwkey");
354 if (p != NULL)
355 rc = strict_strtoul(p, 16, &envhwkey);
356
357 if (rc != 0) {
358 printf("strict_strtoul returns error: %d", rc);
359 return rc;
360 }
361
362 if (!envbid || !envhwkey) {
363 /*
364 * BoardId/HWkey not available in the environment, so try the
365 * environment variable for BoardId/HWkey list
366 */
367 char *bidhwklist = getenv("boardIdListHex");
368
369 if (bidhwklist) {
370 int found = 0;
371 char *rest = bidhwklist;
372 char *endp;
373
374 if (verbose) {
375 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
376 ivmbid, ivmhwkey);
377 printf("boardIdHwKeyList: %s\n",
378 bidhwklist);
379 }
380 while (!found) {
381 /* loop over each bid/hwkey pair in the list */
382 unsigned long bid = 0;
383 unsigned long hwkey = 0;
384
385 while (*rest && !isxdigit(*rest))
386 rest++;
387 /*
388 * use simple_strtoul because we need &end and
389 * we know we got non numeric char at the end
390 */
391 bid = simple_strtoul(rest, &endp, 16);
392 /* BoardId and HWkey are separated with a "_" */
393 if (*endp == '_') {
394 rest = endp + 1;
395 /*
396 * use simple_strtoul because we need
397 * &end
398 */
399 hwkey = simple_strtoul(rest, &endp, 16);
400 rest = endp;
401 while (*rest && !isxdigit(*rest))
402 rest++;
403 }
404 if ((!bid) || (!hwkey)) {
405 /* end of list */
406 break;
407 }
408 if (verbose) {
409 printf("trying bid=0x%lX, hwkey=%ld\n",
410 bid, hwkey);
411 }
412 /*
413 * Compare the values of the found entry in the
414 * list with the valid values which are stored
415 * in the inventory eeprom. If they are equal
Holger Brunckbc4e15f2011-06-05 22:22:17 +0000416 * set the values in environment variables.
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000417 */
418 if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
419 char buf[10];
420
421 found = 1;
422 envbid = bid;
423 envhwkey = hwkey;
424 sprintf(buf, "%lx", bid);
425 setenv("boardid", buf);
426 sprintf(buf, "%lx", hwkey);
427 setenv("hwkey", buf);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000428 }
429 } /* end while( ! found ) */
430 }
431 }
432
433 /* compare now the values */
434 if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
435 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
436 rc = 0; /* match */
437 } else {
Holger Brunckde799842011-07-04 22:23:59 +0000438 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
439 envhwkey);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000440 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
441 rc = 1; /* don't match */
442 }
443 return rc;
444}
445
446U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
447 "check boardid and hwkey",
448 "[v]\n - check environment parameter "\
449 "\"boardIdListHex\" against stored boardid and hwkey "\
450 "from the IVM\n v: verbose output"
451);