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