blob: 544784a7d90a75fd7186e14985b0c1db86411913 [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
Thomas Herzmannb2c31602012-05-04 10:55:58 +020037#if defined(CONFIG_POST)
38#include "post.h"
39#endif
Holger Brunckbf20a882011-06-05 22:22:18 +000040#include "common.h"
Heiko Schocher5e51b482008-10-15 09:39:08 +020041#include <i2c.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020042
Holger Brunck015a60f2011-09-14 10:54:12 +020043#if !defined(CONFIG_MPC83xx)
Heiko Schocherd9e10952011-03-08 10:51:15 +010044static void i2c_write_start_seq(void);
Holger Brunck015a60f2011-09-14 10:54:12 +020045#endif
46
Heiko Schochercfc58042010-04-26 13:07:28 +020047DECLARE_GLOBAL_DATA_PTR;
Heiko Schocherd9e10952011-03-08 10:51:15 +010048
Heiko Schochercfc58042010-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;
Andreas Huber28c63c72011-09-13 23:06:11 +000067 unsigned int kernelmem;
68 char *p;
69 unsigned long rootfssize = 0;
Heiko Schochercfc58042010-04-26 13:07:28 +020070
71 pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
72 - CONFIG_KM_PNVRAM;
73 sprintf((char *)buf, "0x%x", pnvramaddr);
74 setenv("pnvramaddr", (char *)buf);
75
Andreas Huber28c63c72011-09-13 23:06:11 +000076 /* try to read rootfssize (ram image) from envrionment */
77 p = getenv("rootfssize");
78 if (p != NULL)
79 strict_strtoul(p, 16, &rootfssize);
80 pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
81 CONFIG_KM_PNVRAM) / 0x400;
Heiko Schochercfc58042010-04-26 13:07:28 +020082 sprintf((char *)buf, "0x%x", pram);
83 setenv("pram", (char *)buf);
84
85 varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
86 sprintf((char *)buf, "0x%x", varaddr);
87 setenv("varaddr", (char *)buf);
Andreas Huber28c63c72011-09-13 23:06:11 +000088
89 kernelmem = gd->ram_size - 0x400 * pram;
90 sprintf((char *)buf, "0x%x", kernelmem);
91 setenv("kernelmem", (char *)buf);
92
Heiko Schochercfc58042010-04-26 13:07:28 +020093 return 0;
94}
95
Holger Brunck015a60f2011-09-14 10:54:12 +020096#if defined(CONFIG_SYS_I2C_INIT_BOARD)
Heiko Schocher466924f2010-02-18 08:08:25 +010097#if !defined(CONFIG_MPC83xx)
Heiko Schocherd9e10952011-03-08 10:51:15 +010098static void i2c_write_start_seq(void)
Heiko Schocher5e51b482008-10-15 09:39:08 +020099{
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100100 set_sda(1);
101 udelay(DELAY_HALF_PERIOD);
102 set_scl(1);
103 udelay(DELAY_HALF_PERIOD);
104 set_sda(0);
105 udelay(DELAY_HALF_PERIOD);
106 set_scl(0);
107 udelay(DELAY_HALF_PERIOD);
Heiko Schocher5e51b482008-10-15 09:39:08 +0200108}
109
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100110/*
111 * I2C is a synchronous protocol and resets of the processor in the middle
112 * of an access can block the I2C Bus until a powerdown of the full unit is
113 * done. This function toggles the SCL until the SCL and SCA line are
114 * released, but max. 16 times, after this a I2C start-sequence is sent.
115 * This I2C Deblocking mechanism was developed by Keymile in association
116 * with Anatech and Atmel in 1998.
Heiko Schocher5e51b482008-10-15 09:39:08 +0200117 */
Holger Brunckbf20a882011-06-05 22:22:18 +0000118int i2c_make_abort(void)
Heiko Schocher5e51b482008-10-15 09:39:08 +0200119{
Heiko Schocherd9e10952011-03-08 10:51:15 +0100120
121#if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
Holger Brunckb8205f92012-11-02 00:15:09 +0000122 immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
Heiko Schocherd9e10952011-03-08 10:51:15 +0100123 i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
124
125 /*
126 * disable I2C controller first, otherwhise it thinks we want to
127 * talk to the slave port...
128 */
129 clrbits_8(&i2c->i2c_i2mod, 0x01);
130
131 /* Set the PortPins to GPIO */
132 setports(1);
133#endif
134
Heiko Schocher5e51b482008-10-15 09:39:08 +0200135 int scl_state = 0;
136 int sda_state = 0;
137 int i = 0;
138 int ret = 0;
139
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100140 if (!get_sda()) {
Heiko Schocher5e51b482008-10-15 09:39:08 +0200141 ret = -1;
142 while (i < 16) {
143 i++;
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100144 set_scl(0);
145 udelay(DELAY_ABORT_SEQ);
146 set_scl(1);
147 udelay(DELAY_ABORT_SEQ);
148 scl_state = get_scl();
149 sda_state = get_sda();
Heiko Schocher5e51b482008-10-15 09:39:08 +0200150 if (scl_state && sda_state) {
151 ret = 0;
152 break;
153 }
154 }
155 }
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100156 if (ret == 0)
157 for (i = 0; i < 5; i++)
Heiko Schocherd9e10952011-03-08 10:51:15 +0100158 i2c_write_start_seq();
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100159
Heiko Schocherd9e10952011-03-08 10:51:15 +0100160 /* respect stop setup time */
161 udelay(DELAY_ABORT_SEQ);
162 set_scl(1);
163 udelay(DELAY_ABORT_SEQ);
164 set_sda(1);
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100165 get_sda();
Heiko Schocherd9e10952011-03-08 10:51:15 +0100166
167#if defined(CONFIG_HARD_I2C)
168 /* Set the PortPins back to use for I2C */
169 setports(0);
170#endif
Heiko Schocher5e51b482008-10-15 09:39:08 +0200171 return ret;
172}
Heiko Schocher5e51b482008-10-15 09:39:08 +0200173#endif
174
Heiko Schocherd9e10952011-03-08 10:51:15 +0100175/**
176 * i2c_init_board - reset i2c bus. When the board is powercycled during a
177 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
178 */
179void i2c_init_board(void)
180{
Heiko Schocher5e51b482008-10-15 09:39:08 +0200181 /* Now run the AbortSequence() */
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100182 i2c_make_abort();
Heiko Schocher5e51b482008-10-15 09:39:08 +0200183}
Holger Brunck015a60f2011-09-14 10:54:12 +0200184#endif
185
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100186int board_eth_init(bd_t *bis)
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100187{
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100188 if (ethernet_present())
Heiko Schocher466924f2010-02-18 08:08:25 +0100189 return cpu_eth_init(bis);
190
191 return -1;
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100192}
Holger Brunck30664922011-05-04 01:47:30 +0000193
194/*
195 * do_setboardid command
196 * read out the board id and the hw key from the intventory EEPROM and set
197 * this values as environment variables.
198 */
199static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
200 char *const argv[])
201{
202 unsigned char buf[32];
203 char *p;
204
205 p = get_local_var("IVM_BoardId");
206 if (p == NULL) {
207 printf("can't get the IVM_Boardid\n");
208 return 1;
209 }
210 sprintf((char *)buf, "%s", p);
211 setenv("boardid", (char *)buf);
Holger Brunckde799842011-07-04 22:23:59 +0000212 printf("set boardid=%s\n", buf);
Holger Brunck30664922011-05-04 01:47:30 +0000213
214 p = get_local_var("IVM_HWKey");
215 if (p == NULL) {
216 printf("can't get the IVM_HWKey\n");
217 return 1;
218 }
219 sprintf((char *)buf, "%s", p);
220 setenv("hwkey", (char *)buf);
Holger Brunckde799842011-07-04 22:23:59 +0000221 printf("set hwkey=%s\n", buf);
222 printf("Execute manually saveenv for persistent storage.\n");
Holger Brunck30664922011-05-04 01:47:30 +0000223
224 return 0;
225}
226
227U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
228 "hwkey from IVM and set in environment");
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000229
230/*
231 * command km_checkbidhwk
232 * if "boardid" and "hwkey" are not already set in the environment, do:
233 * if a "boardIdListHex" exists in the environment:
234 * - read ivm data for boardid and hwkey
235 * - compare each entry of the boardIdListHex with the
236 * IVM data:
237 * if match:
238 * set environment variables boardid, boardId,
239 * hwkey, hwKey to the found values
240 * both (boardid and boardId) are set because
241 * they might be used differently in the
242 * application and in the init scripts (?)
243 * return 0 in case of match, 1 if not match or error
244 */
Holger Brunck828411f2013-05-06 15:02:40 +0200245static int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000246 char *const argv[])
247{
248 unsigned long ivmbid = 0, ivmhwkey = 0;
249 unsigned long envbid = 0, envhwkey = 0;
250 char *p;
251 int verbose = argc > 1 && *argv[1] == 'v';
252 int rc = 0;
253
254 /*
255 * first read out the real inventory values, these values are
256 * already stored in the local hush variables
257 */
258 p = get_local_var("IVM_BoardId");
259 if (p == NULL) {
260 printf("can't get the IVM_Boardid\n");
261 return 1;
262 }
263 rc = strict_strtoul(p, 16, &ivmbid);
264
265 p = get_local_var("IVM_HWKey");
266 if (p == NULL) {
267 printf("can't get the IVM_HWKey\n");
268 return 1;
269 }
270 rc = strict_strtoul(p, 16, &ivmhwkey);
271
272 if (!ivmbid || !ivmhwkey) {
273 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
274 return rc;
275 }
276
277 /* now try to read values from environment if available */
278 p = getenv("boardid");
279 if (p != NULL)
280 rc = strict_strtoul(p, 16, &envbid);
281 p = getenv("hwkey");
282 if (p != NULL)
283 rc = strict_strtoul(p, 16, &envhwkey);
284
285 if (rc != 0) {
286 printf("strict_strtoul returns error: %d", rc);
287 return rc;
288 }
289
290 if (!envbid || !envhwkey) {
291 /*
292 * BoardId/HWkey not available in the environment, so try the
293 * environment variable for BoardId/HWkey list
294 */
295 char *bidhwklist = getenv("boardIdListHex");
296
297 if (bidhwklist) {
298 int found = 0;
299 char *rest = bidhwklist;
300 char *endp;
301
302 if (verbose) {
303 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
304 ivmbid, ivmhwkey);
305 printf("boardIdHwKeyList: %s\n",
306 bidhwklist);
307 }
308 while (!found) {
309 /* loop over each bid/hwkey pair in the list */
310 unsigned long bid = 0;
311 unsigned long hwkey = 0;
312
313 while (*rest && !isxdigit(*rest))
314 rest++;
315 /*
316 * use simple_strtoul because we need &end and
317 * we know we got non numeric char at the end
318 */
319 bid = simple_strtoul(rest, &endp, 16);
320 /* BoardId and HWkey are separated with a "_" */
321 if (*endp == '_') {
322 rest = endp + 1;
323 /*
324 * use simple_strtoul because we need
325 * &end
326 */
327 hwkey = simple_strtoul(rest, &endp, 16);
328 rest = endp;
329 while (*rest && !isxdigit(*rest))
330 rest++;
331 }
332 if ((!bid) || (!hwkey)) {
333 /* end of list */
334 break;
335 }
336 if (verbose) {
337 printf("trying bid=0x%lX, hwkey=%ld\n",
338 bid, hwkey);
339 }
340 /*
341 * Compare the values of the found entry in the
342 * list with the valid values which are stored
343 * in the inventory eeprom. If they are equal
Holger Brunckbc4e15f2011-06-05 22:22:17 +0000344 * set the values in environment variables.
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000345 */
346 if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
347 char buf[10];
348
349 found = 1;
350 envbid = bid;
351 envhwkey = hwkey;
352 sprintf(buf, "%lx", bid);
353 setenv("boardid", buf);
354 sprintf(buf, "%lx", hwkey);
355 setenv("hwkey", buf);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000356 }
357 } /* end while( ! found ) */
358 }
359 }
360
361 /* compare now the values */
362 if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
363 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
364 rc = 0; /* match */
365 } else {
Holger Brunckde799842011-07-04 22:23:59 +0000366 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
367 envhwkey);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000368 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
369 rc = 1; /* don't match */
370 }
371 return rc;
372}
373
374U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
375 "check boardid and hwkey",
376 "[v]\n - check environment parameter "\
377 "\"boardIdListHex\" against stored boardid and hwkey "\
378 "from the IVM\n v: verbose output"
379);
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200380
381/*
382 * command km_checktestboot
383 * if the testpin of the board is asserted, return 1
384 * * else return 0
385 */
Holger Brunck828411f2013-05-06 15:02:40 +0200386static int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc,
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200387 char *const argv[])
388{
389 int testpin = 0;
390 char *s = NULL;
391 int testboot = 0;
392 int verbose = argc > 1 && *argv[1] == 'v';
393
394#if defined(CONFIG_POST)
395 testpin = post_hotkeys_pressed();
396 s = getenv("test_bank");
397#endif
398 /* when test_bank is not set, act as if testpin is not asserted */
399 testboot = (testpin != 0) && (s);
400 if (verbose) {
401 printf("testpin = %d\n", testpin);
402 printf("test_bank = %s\n", s ? s : "not set");
403 printf("boot test app : %s\n", (testboot) ? "yes" : "no");
404 }
405 /* return 0 means: testboot, therefore we need the inversion */
406 return !testboot;
407}
408
409U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
410 "check if testpin is asserted",
411 "[v]\n v - verbose output"
412);