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>
38 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
42 static void i2c_write_start_seq(void);
43 DECLARE_GLOBAL_DATA_PTR
;
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
60 unsigned int pnvramaddr
;
63 unsigned int kernelmem
;
65 unsigned long rootfssize
= 0;
67 pnvramaddr
= gd
->ram_size
- CONFIG_KM_RESERVED_PRAM
- CONFIG_KM_PHRAM
69 sprintf((char *)buf
, "0x%x", pnvramaddr
);
70 setenv("pnvramaddr", (char *)buf
);
72 /* try to read rootfssize (ram image) from envrionment */
73 p
= getenv("rootfssize");
75 strict_strtoul(p
, 16, &rootfssize
);
76 pram
= (rootfssize
+ CONFIG_KM_RESERVED_PRAM
+ CONFIG_KM_PHRAM
+
77 CONFIG_KM_PNVRAM
) / 0x400;
78 sprintf((char *)buf
, "0x%x", pram
);
79 setenv("pram", (char *)buf
);
81 varaddr
= gd
->ram_size
- CONFIG_KM_RESERVED_PRAM
- CONFIG_KM_PHRAM
;
82 sprintf((char *)buf
, "0x%x", varaddr
);
83 setenv("varaddr", (char *)buf
);
85 kernelmem
= gd
->ram_size
- 0x400 * pram
;
86 sprintf((char *)buf
, "0x%x", kernelmem
);
87 setenv("kernelmem", (char *)buf
);
92 #define DELAY_ABORT_SEQ 62 /* @200kHz 9 clocks = 44us, 62us is ok */
93 #define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000))
95 #if !defined(CONFIG_MPC83xx)
96 static void i2c_write_start_seq(void)
99 udelay(DELAY_HALF_PERIOD
);
101 udelay(DELAY_HALF_PERIOD
);
103 udelay(DELAY_HALF_PERIOD
);
105 udelay(DELAY_HALF_PERIOD
);
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.
116 int i2c_make_abort(void)
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
;
124 * disable I2C controller first, otherwhise it thinks we want to
125 * talk to the slave port...
127 clrbits_8(&i2c
->i2c_i2mod
, 0x01);
129 /* Set the PortPins to GPIO */
143 udelay(DELAY_ABORT_SEQ
);
145 udelay(DELAY_ABORT_SEQ
);
146 scl_state
= get_scl();
147 sda_state
= get_sda();
148 if (scl_state
&& sda_state
) {
150 printf("[INFO] i2c abort after %d clocks\n", i
);
156 for (i
= 0; i
< 5; i
++)
157 i2c_write_start_seq();
159 printf("[ERROR] i2c abort failed\n");
161 /* respect stop setup time */
162 udelay(DELAY_ABORT_SEQ
);
164 udelay(DELAY_ABORT_SEQ
);
168 #if defined(CONFIG_HARD_I2C)
169 /* Set the PortPins back to use for I2C */
174 #endif /* !MPC83xx */
176 #if defined(CONFIG_MPC83xx)
177 static void i2c_write_start_seq(void)
180 dev
= (struct fsl_i2c
*) (CONFIG_SYS_IMMR
+ CONFIG_SYS_I2C_OFFSET
);
181 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
));
187 int i2c_make_abort(void)
190 dev
= (struct fsl_i2c
*) (CONFIG_SYS_IMMR
+ CONFIG_SYS_I2C_OFFSET
);
197 /* wait after each operation to finsh with a delay */
198 out_8(&dev
->cr
, (I2C_CR_MSTA
));
199 udelay(DELAY_ABORT_SEQ
);
200 out_8(&dev
->cr
, (I2C_CR_MEN
| I2C_CR_MSTA
));
201 udelay(DELAY_ABORT_SEQ
);
202 dummy
= in_8(&dev
->dr
);
203 udelay(DELAY_ABORT_SEQ
);
204 last
= in_8(&dev
->dr
);
208 * do read until the last bit is 1, but stop if the full eeprom is
211 while (((last
& 0x01) != 0x01) &&
212 (nbr_read
< CONFIG_SYS_IVM_EEPROM_MAX_LEN
)) {
213 udelay(DELAY_ABORT_SEQ
);
214 last
= in_8(&dev
->dr
);
217 if ((last
& 0x01) != 0x01)
219 if ((last
!= 0xff) || (nbr_read
> 1))
220 printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
222 udelay(DELAY_ABORT_SEQ
);
223 out_8(&dev
->cr
, (I2C_CR_MEN
));
224 udelay(DELAY_ABORT_SEQ
);
225 /* clear status reg */
228 for (i
= 0; i
< 5; i
++)
229 i2c_write_start_seq();
231 printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
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.
242 void i2c_init_board(void)
244 /* Now run the AbortSequence() */
248 #if !defined(MACH_TYPE_KM_KIRKWOOD)
249 int ethernet_present(void)
251 struct km_bec_fpga
*base
=
252 (struct km_bec_fpga
*)CONFIG_SYS_KMBEC_FPGA_BASE
;
254 return in_8(&base
->bprth
) & PIGGY_PRESENT
;
258 int board_eth_init(bd_t
*bis
)
260 if (ethernet_present())
261 return cpu_eth_init(bis
);
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.
271 static int do_setboardid(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
274 unsigned char buf
[32];
277 p
= get_local_var("IVM_BoardId");
279 printf("can't get the IVM_Boardid\n");
282 sprintf((char *)buf
, "%s", p
);
283 setenv("boardid", (char *)buf
);
284 printf("set boardid=%s\n", buf
);
286 p
= get_local_var("IVM_HWKey");
288 printf("can't get the IVM_HWKey\n");
291 sprintf((char *)buf
, "%s", p
);
292 setenv("hwkey", (char *)buf
);
293 printf("set hwkey=%s\n", buf
);
294 printf("Execute manually saveenv for persistent storage.\n");
299 U_BOOT_CMD(km_setboardid
, 1, 0, do_setboardid
, "setboardid", "read out bid and "
300 "hwkey from IVM and set in environment");
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
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
317 int do_checkboardidhwk(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
320 unsigned long ivmbid
= 0, ivmhwkey
= 0;
321 unsigned long envbid
= 0, envhwkey
= 0;
323 int verbose
= argc
> 1 && *argv
[1] == 'v';
327 * first read out the real inventory values, these values are
328 * already stored in the local hush variables
330 p
= get_local_var("IVM_BoardId");
332 printf("can't get the IVM_Boardid\n");
335 rc
= strict_strtoul(p
, 16, &ivmbid
);
337 p
= get_local_var("IVM_HWKey");
339 printf("can't get the IVM_HWKey\n");
342 rc
= strict_strtoul(p
, 16, &ivmhwkey
);
344 if (!ivmbid
|| !ivmhwkey
) {
345 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
349 /* now try to read values from environment if available */
350 p
= getenv("boardid");
352 rc
= strict_strtoul(p
, 16, &envbid
);
355 rc
= strict_strtoul(p
, 16, &envhwkey
);
358 printf("strict_strtoul returns error: %d", rc
);
362 if (!envbid
|| !envhwkey
) {
364 * BoardId/HWkey not available in the environment, so try the
365 * environment variable for BoardId/HWkey list
367 char *bidhwklist
= getenv("boardIdListHex");
371 char *rest
= bidhwklist
;
375 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
377 printf("boardIdHwKeyList: %s\n",
381 /* loop over each bid/hwkey pair in the list */
382 unsigned long bid
= 0;
383 unsigned long hwkey
= 0;
385 while (*rest
&& !isxdigit(*rest
))
388 * use simple_strtoul because we need &end and
389 * we know we got non numeric char at the end
391 bid
= simple_strtoul(rest
, &endp
, 16);
392 /* BoardId and HWkey are separated with a "_" */
396 * use simple_strtoul because we need
399 hwkey
= simple_strtoul(rest
, &endp
, 16);
401 while (*rest
&& !isxdigit(*rest
))
404 if ((!bid
) || (!hwkey
)) {
409 printf("trying bid=0x%lX, hwkey=%ld\n",
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
416 * set the values in environment variables.
418 if ((bid
== ivmbid
) && (hwkey
== ivmhwkey
)) {
424 sprintf(buf
, "%lx", bid
);
425 setenv("boardid", buf
);
426 sprintf(buf
, "%lx", hwkey
);
427 setenv("hwkey", buf
);
429 } /* end while( ! found ) */
433 /* compare now the values */
434 if ((ivmbid
== envbid
) && (ivmhwkey
== envhwkey
)) {
435 printf("boardid=0x%3lX, hwkey=%ld\n", envbid
, envhwkey
);
438 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid
,
440 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid
, ivmhwkey
);
441 rc
= 1; /* don't match */
446 U_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"