3 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
6 * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
35 #include <linux/ctype.h>
37 #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
42 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
46 static void i2c_write_start_seq(void);
47 DECLARE_GLOBAL_DATA_PTR;
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
64 unsigned int pnvramaddr;
68 pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
70 sprintf((char *)buf, "0x%x", pnvramaddr);
71 setenv("pnvramaddr", (char *)buf);
73 pram = (CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM + CONFIG_KM_PNVRAM) /
75 sprintf((char *)buf, "0x%x", pram);
76 setenv("pram", (char *)buf);
78 varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
79 sprintf((char *)buf, "0x%x", varaddr);
80 setenv("varaddr", (char *)buf);
84 #define DELAY_ABORT_SEQ 62 /* @200kHz 9 clocks = 44us, 62us is ok */
85 #define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
87 #if !defined(CONFIG_MPC83xx)
88 static void i2c_write_start_seq(void)
91 udelay(DELAY_HALF_PERIOD);
93 udelay(DELAY_HALF_PERIOD);
95 udelay(DELAY_HALF_PERIOD);
97 udelay(DELAY_HALF_PERIOD);
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.
108 int i2c_make_abort(void)
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;
116 * disable I2C controller first, otherwhise it thinks we want to
117 * talk to the slave port...
119 clrbits_8(&i2c->i2c_i2mod, 0x01);
121 /* Set the PortPins to GPIO */
135 udelay(DELAY_ABORT_SEQ);
137 udelay(DELAY_ABORT_SEQ);
138 scl_state = get_scl();
139 sda_state = get_sda();
140 if (scl_state && sda_state) {
142 printf("[INFO] i2c abort after %d clocks\n", i);
148 for (i = 0; i < 5; i++)
149 i2c_write_start_seq();
151 printf("[ERROR] i2c abort failed\n");
153 /* respect stop setup time */
154 udelay(DELAY_ABORT_SEQ);
156 udelay(DELAY_ABORT_SEQ);
160 #if defined(CONFIG_HARD_I2C)
161 /* Set the PortPins back to use for I2C */
166 #endif /* !MPC83xx */
168 #if defined(CONFIG_MPC83xx)
169 static void i2c_write_start_seq(void)
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));
179 int i2c_make_abort(void)
182 dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
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);
200 * do read until the last bit is 1, but stop if the full eeprom is
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);
209 if ((last & 0x01) != 0x01)
211 if ((last != 0xff) || (nbr_read > 1))
212 printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
214 udelay(DELAY_ABORT_SEQ);
215 out_8(&dev->cr, (I2C_CR_MEN));
216 udelay(DELAY_ABORT_SEQ);
217 /* clear status reg */
220 for (i = 0; i < 5; i++)
221 i2c_write_start_seq();
223 printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
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.
234 void i2c_init_board(void)
236 /* Now run the AbortSequence() */
240 #if !defined(MACH_TYPE_KM_KIRKWOOD)
241 int ethernet_present(void)
243 struct km_bec_fpga *base =
244 (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
246 return in_8(&base->bprth) & PIGGY_PRESENT;
250 int board_eth_init(bd_t *bis)
252 if (ethernet_present())
253 return cpu_eth_init(bis);
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.
263 static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
266 unsigned char buf[32];
269 p = get_local_var("IVM_BoardId");
271 printf("can't get the IVM_Boardid\n");
274 sprintf((char *)buf, "%s", p);
275 setenv("boardid", (char *)buf);
276 printf("set boardid=%s\n", buf);
278 p = get_local_var("IVM_HWKey");
280 printf("can't get the IVM_HWKey\n");
283 sprintf((char *)buf, "%s", p);
284 setenv("hwkey", (char *)buf);
285 printf("set hwkey=%s\n", buf);
286 printf("Execute manually saveenv for persistent storage.\n");
291 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
292 "hwkey from IVM and set in environment");
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
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
309 int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
312 unsigned long ivmbid = 0, ivmhwkey = 0;
313 unsigned long envbid = 0, envhwkey = 0;
315 int verbose = argc > 1 && *argv[1] == 'v';
319 * first read out the real inventory values, these values are
320 * already stored in the local hush variables
322 p = get_local_var("IVM_BoardId");
324 printf("can't get the IVM_Boardid\n");
327 rc = strict_strtoul(p, 16, &ivmbid);
329 p = get_local_var("IVM_HWKey");
331 printf("can't get the IVM_HWKey\n");
334 rc = strict_strtoul(p, 16, &ivmhwkey);
336 if (!ivmbid || !ivmhwkey) {
337 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
341 /* now try to read values from environment if available */
342 p = getenv("boardid");
344 rc = strict_strtoul(p, 16, &envbid);
347 rc = strict_strtoul(p, 16, &envhwkey);
350 printf("strict_strtoul returns error: %d", rc);
354 if (!envbid || !envhwkey) {
356 * BoardId/HWkey not available in the environment, so try the
357 * environment variable for BoardId/HWkey list
359 char *bidhwklist = getenv("boardIdListHex");
363 char *rest = bidhwklist;
367 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
369 printf("boardIdHwKeyList: %s\n",
373 /* loop over each bid/hwkey pair in the list */
374 unsigned long bid = 0;
375 unsigned long hwkey = 0;
377 while (*rest && !isxdigit(*rest))
380 * use simple_strtoul because we need &end and
381 * we know we got non numeric char at the end
383 bid = simple_strtoul(rest, &endp, 16);
384 /* BoardId and HWkey are separated with a "_" */
388 * use simple_strtoul because we need
391 hwkey = simple_strtoul(rest, &endp, 16);
393 while (*rest && !isxdigit(*rest))
396 if ((!bid) || (!hwkey)) {
401 printf("trying bid=0x%lX, hwkey=%ld\n",
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
408 * set the values in environment variables.
410 if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
416 sprintf(buf, "%lx", bid);
417 setenv("boardid", buf);
418 sprintf(buf, "%lx", hwkey);
419 setenv("hwkey", buf);
421 } /* end while( ! found ) */
425 /* compare now the values */
426 if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
427 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
430 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
432 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
433 rc = 1; /* don't match */
438 U_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"