1 /* 2 * (C) Copyright 2008 3 * Heiko Schocher, DENX Software Engineering, hs@denx.de. 4 * 5 * (C) Copyright 2011 6 * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com 7 * 8 * SPDX-License-Identifier: GPL-2.0+ 9 */ 10 11 #include <common.h> 12 #include <ioports.h> 13 #include <command.h> 14 #include <malloc.h> 15 #include <hush.h> 16 #include <net.h> 17 #include <netdev.h> 18 #include <asm/io.h> 19 #include <linux/ctype.h> 20 21 #if defined(CONFIG_POST) 22 #include "post.h" 23 #endif 24 #include "common.h" 25 #include <i2c.h> 26 27 #if !defined(CONFIG_MPC83xx) 28 static void i2c_write_start_seq(void); 29 #endif 30 31 DECLARE_GLOBAL_DATA_PTR; 32 33 /* 34 * Set Keymile specific environment variables 35 * Currently only some memory layout variables are calculated here 36 * ... ------------------------------------------------ 37 * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM 38 * ... |<------------------- pram ------------------->| 39 * ... ------------------------------------------------ 40 * @END_OF_RAM: denotes the RAM size 41 * @pnvramaddr: Startadress of pseudo non volatile RAM in hex 42 * @pram : preserved ram size in k 43 * @varaddr : startadress for /var mounted into RAM 44 */ 45 int set_km_env(void) 46 { 47 uchar buf[32]; 48 unsigned int pnvramaddr; 49 unsigned int pram; 50 unsigned int varaddr; 51 unsigned int kernelmem; 52 char *p; 53 unsigned long rootfssize = 0; 54 55 pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM 56 - CONFIG_KM_PNVRAM; 57 sprintf((char *)buf, "0x%x", pnvramaddr); 58 setenv("pnvramaddr", (char *)buf); 59 60 /* try to read rootfssize (ram image) from envrionment */ 61 p = getenv("rootfssize"); 62 if (p != NULL) 63 strict_strtoul(p, 16, &rootfssize); 64 pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM + 65 CONFIG_KM_PNVRAM) / 0x400; 66 sprintf((char *)buf, "0x%x", pram); 67 setenv("pram", (char *)buf); 68 69 varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM; 70 sprintf((char *)buf, "0x%x", varaddr); 71 setenv("varaddr", (char *)buf); 72 73 kernelmem = gd->ram_size - 0x400 * pram; 74 sprintf((char *)buf, "0x%x", kernelmem); 75 setenv("kernelmem", (char *)buf); 76 77 return 0; 78 } 79 80 #if defined(CONFIG_SYS_I2C_INIT_BOARD) 81 #if !defined(CONFIG_MPC83xx) 82 static void i2c_write_start_seq(void) 83 { 84 set_sda(1); 85 udelay(DELAY_HALF_PERIOD); 86 set_scl(1); 87 udelay(DELAY_HALF_PERIOD); 88 set_sda(0); 89 udelay(DELAY_HALF_PERIOD); 90 set_scl(0); 91 udelay(DELAY_HALF_PERIOD); 92 } 93 94 /* 95 * I2C is a synchronous protocol and resets of the processor in the middle 96 * of an access can block the I2C Bus until a powerdown of the full unit is 97 * done. This function toggles the SCL until the SCL and SCA line are 98 * released, but max. 16 times, after this a I2C start-sequence is sent. 99 * This I2C Deblocking mechanism was developed by Keymile in association 100 * with Anatech and Atmel in 1998. 101 */ 102 int i2c_make_abort(void) 103 { 104 105 #if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD) 106 immap_t *immap = (immap_t *)CONFIG_SYS_IMMR; 107 i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c; 108 109 /* 110 * disable I2C controller first, otherwhise it thinks we want to 111 * talk to the slave port... 112 */ 113 clrbits_8(&i2c->i2c_i2mod, 0x01); 114 115 /* Set the PortPins to GPIO */ 116 setports(1); 117 #endif 118 119 int scl_state = 0; 120 int sda_state = 0; 121 int i = 0; 122 int ret = 0; 123 124 if (!get_sda()) { 125 ret = -1; 126 while (i < 16) { 127 i++; 128 set_scl(0); 129 udelay(DELAY_ABORT_SEQ); 130 set_scl(1); 131 udelay(DELAY_ABORT_SEQ); 132 scl_state = get_scl(); 133 sda_state = get_sda(); 134 if (scl_state && sda_state) { 135 ret = 0; 136 break; 137 } 138 } 139 } 140 if (ret == 0) 141 for (i = 0; i < 5; i++) 142 i2c_write_start_seq(); 143 144 /* respect stop setup time */ 145 udelay(DELAY_ABORT_SEQ); 146 set_scl(1); 147 udelay(DELAY_ABORT_SEQ); 148 set_sda(1); 149 get_sda(); 150 151 #if defined(CONFIG_HARD_I2C) 152 /* Set the PortPins back to use for I2C */ 153 setports(0); 154 #endif 155 return ret; 156 } 157 #endif 158 159 /** 160 * i2c_init_board - reset i2c bus. When the board is powercycled during a 161 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions. 162 */ 163 void i2c_init_board(void) 164 { 165 /* Now run the AbortSequence() */ 166 i2c_make_abort(); 167 } 168 #endif 169 170 int board_eth_init(bd_t *bis) 171 { 172 if (ethernet_present()) 173 return cpu_eth_init(bis); 174 175 return -1; 176 } 177 178 /* 179 * do_setboardid command 180 * read out the board id and the hw key from the intventory EEPROM and set 181 * this values as environment variables. 182 */ 183 static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc, 184 char *const argv[]) 185 { 186 unsigned char buf[32]; 187 char *p; 188 189 p = get_local_var("IVM_BoardId"); 190 if (p == NULL) { 191 printf("can't get the IVM_Boardid\n"); 192 return 1; 193 } 194 sprintf((char *)buf, "%s", p); 195 setenv("boardid", (char *)buf); 196 printf("set boardid=%s\n", buf); 197 198 p = get_local_var("IVM_HWKey"); 199 if (p == NULL) { 200 printf("can't get the IVM_HWKey\n"); 201 return 1; 202 } 203 sprintf((char *)buf, "%s", p); 204 setenv("hwkey", (char *)buf); 205 printf("set hwkey=%s\n", buf); 206 printf("Execute manually saveenv for persistent storage.\n"); 207 208 return 0; 209 } 210 211 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and " 212 "hwkey from IVM and set in environment"); 213 214 /* 215 * command km_checkbidhwk 216 * if "boardid" and "hwkey" are not already set in the environment, do: 217 * if a "boardIdListHex" exists in the environment: 218 * - read ivm data for boardid and hwkey 219 * - compare each entry of the boardIdListHex with the 220 * IVM data: 221 * if match: 222 * set environment variables boardid, boardId, 223 * hwkey, hwKey to the found values 224 * both (boardid and boardId) are set because 225 * they might be used differently in the 226 * application and in the init scripts (?) 227 * return 0 in case of match, 1 if not match or error 228 */ 229 int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc, 230 char *const argv[]) 231 { 232 unsigned long ivmbid = 0, ivmhwkey = 0; 233 unsigned long envbid = 0, envhwkey = 0; 234 char *p; 235 int verbose = argc > 1 && *argv[1] == 'v'; 236 int rc = 0; 237 238 /* 239 * first read out the real inventory values, these values are 240 * already stored in the local hush variables 241 */ 242 p = get_local_var("IVM_BoardId"); 243 if (p == NULL) { 244 printf("can't get the IVM_Boardid\n"); 245 return 1; 246 } 247 rc = strict_strtoul(p, 16, &ivmbid); 248 249 p = get_local_var("IVM_HWKey"); 250 if (p == NULL) { 251 printf("can't get the IVM_HWKey\n"); 252 return 1; 253 } 254 rc = strict_strtoul(p, 16, &ivmhwkey); 255 256 if (!ivmbid || !ivmhwkey) { 257 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n"); 258 return rc; 259 } 260 261 /* now try to read values from environment if available */ 262 p = getenv("boardid"); 263 if (p != NULL) 264 rc = strict_strtoul(p, 16, &envbid); 265 p = getenv("hwkey"); 266 if (p != NULL) 267 rc = strict_strtoul(p, 16, &envhwkey); 268 269 if (rc != 0) { 270 printf("strict_strtoul returns error: %d", rc); 271 return rc; 272 } 273 274 if (!envbid || !envhwkey) { 275 /* 276 * BoardId/HWkey not available in the environment, so try the 277 * environment variable for BoardId/HWkey list 278 */ 279 char *bidhwklist = getenv("boardIdListHex"); 280 281 if (bidhwklist) { 282 int found = 0; 283 char *rest = bidhwklist; 284 char *endp; 285 286 if (verbose) { 287 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n", 288 ivmbid, ivmhwkey); 289 printf("boardIdHwKeyList: %s\n", 290 bidhwklist); 291 } 292 while (!found) { 293 /* loop over each bid/hwkey pair in the list */ 294 unsigned long bid = 0; 295 unsigned long hwkey = 0; 296 297 while (*rest && !isxdigit(*rest)) 298 rest++; 299 /* 300 * use simple_strtoul because we need &end and 301 * we know we got non numeric char at the end 302 */ 303 bid = simple_strtoul(rest, &endp, 16); 304 /* BoardId and HWkey are separated with a "_" */ 305 if (*endp == '_') { 306 rest = endp + 1; 307 /* 308 * use simple_strtoul because we need 309 * &end 310 */ 311 hwkey = simple_strtoul(rest, &endp, 16); 312 rest = endp; 313 while (*rest && !isxdigit(*rest)) 314 rest++; 315 } 316 if ((!bid) || (!hwkey)) { 317 /* end of list */ 318 break; 319 } 320 if (verbose) { 321 printf("trying bid=0x%lX, hwkey=%ld\n", 322 bid, hwkey); 323 } 324 /* 325 * Compare the values of the found entry in the 326 * list with the valid values which are stored 327 * in the inventory eeprom. If they are equal 328 * set the values in environment variables. 329 */ 330 if ((bid == ivmbid) && (hwkey == ivmhwkey)) { 331 char buf[10]; 332 333 found = 1; 334 envbid = bid; 335 envhwkey = hwkey; 336 sprintf(buf, "%lx", bid); 337 setenv("boardid", buf); 338 sprintf(buf, "%lx", hwkey); 339 setenv("hwkey", buf); 340 } 341 } /* end while( ! found ) */ 342 } 343 } 344 345 /* compare now the values */ 346 if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) { 347 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey); 348 rc = 0; /* match */ 349 } else { 350 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid, 351 envhwkey); 352 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey); 353 rc = 1; /* don't match */ 354 } 355 return rc; 356 } 357 358 U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk, 359 "check boardid and hwkey", 360 "[v]\n - check environment parameter "\ 361 "\"boardIdListHex\" against stored boardid and hwkey "\ 362 "from the IVM\n v: verbose output" 363 ); 364 365 /* 366 * command km_checktestboot 367 * if the testpin of the board is asserted, return 1 368 * * else return 0 369 */ 370 int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc, 371 char *const argv[]) 372 { 373 int testpin = 0; 374 char *s = NULL; 375 int testboot = 0; 376 int verbose = argc > 1 && *argv[1] == 'v'; 377 378 #if defined(CONFIG_POST) 379 testpin = post_hotkeys_pressed(); 380 s = getenv("test_bank"); 381 #endif 382 /* when test_bank is not set, act as if testpin is not asserted */ 383 testboot = (testpin != 0) && (s); 384 if (verbose) { 385 printf("testpin = %d\n", testpin); 386 printf("test_bank = %s\n", s ? s : "not set"); 387 printf("boot test app : %s\n", (testboot) ? "yes" : "no"); 388 } 389 /* return 0 means: testboot, therefore we need the inversion */ 390 return !testboot; 391 } 392 393 U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot, 394 "check if testpin is asserted", 395 "[v]\n v - verbose output" 396 ); 397