]> git.sur5r.net Git - u-boot/blob - board/keymile/common/common.c
km/common: remove unneeded ifdefs for I2C
[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  * 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>
28 #include <ioports.h>
29 #include <command.h>
30 #include <malloc.h>
31 #include <hush.h>
32 #include <net.h>
33 #include <netdev.h>
34 #include <asm/io.h>
35 #include <linux/ctype.h>
36
37 #if defined(CONFIG_POST)
38 #include "post.h"
39 #endif
40 #include "common.h"
41 #include <i2c.h>
42
43 #if !defined(CONFIG_MPC83xx)
44 static void i2c_write_start_seq(void);
45 #endif
46
47 DECLARE_GLOBAL_DATA_PTR;
48
49 /*
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  */
61 int set_km_env(void)
62 {
63         uchar buf[32];
64         unsigned int pnvramaddr;
65         unsigned int pram;
66         unsigned int varaddr;
67         unsigned int kernelmem;
68         char *p;
69         unsigned long rootfssize = 0;
70
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
76         /* 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;
82         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);
88
89         kernelmem = gd->ram_size - 0x400 * pram;
90         sprintf((char *)buf, "0x%x", kernelmem);
91         setenv("kernelmem", (char *)buf);
92
93         return 0;
94 }
95
96 #if defined(CONFIG_SYS_I2C_INIT_BOARD)
97 #if !defined(CONFIG_MPC83xx)
98 static void i2c_write_start_seq(void)
99 {
100         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);
108 }
109
110 /*
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.
117  */
118 int i2c_make_abort(void)
119 {
120
121 #if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
122         immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
123         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
135         int     scl_state = 0;
136         int     sda_state = 0;
137         int     i = 0;
138         int     ret = 0;
139
140         if (!get_sda()) {
141                 ret = -1;
142                 while (i < 16) {
143                         i++;
144                         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();
150                         if (scl_state && sda_state) {
151                                 ret = 0;
152                                 break;
153                         }
154                 }
155         }
156         if (ret == 0)
157                 for (i = 0; i < 5; i++)
158                         i2c_write_start_seq();
159
160         /* respect stop setup time */
161         udelay(DELAY_ABORT_SEQ);
162         set_scl(1);
163         udelay(DELAY_ABORT_SEQ);
164         set_sda(1);
165         get_sda();
166
167 #if defined(CONFIG_HARD_I2C)
168         /* Set the PortPins back to use for I2C */
169         setports(0);
170 #endif
171         return ret;
172 }
173 #endif
174
175 /**
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  */
179 void i2c_init_board(void)
180 {
181         /* Now run the AbortSequence() */
182         i2c_make_abort();
183 }
184 #endif
185
186
187 #if !defined(MACH_TYPE_KM_KIRKWOOD)
188 int ethernet_present(void)
189 {
190         struct km_bec_fpga *base =
191                 (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
192
193         return in_8(&base->bprth) & PIGGY_PRESENT;
194 }
195 #endif
196
197 int board_eth_init(bd_t *bis)
198 {
199         if (ethernet_present())
200                 return cpu_eth_init(bis);
201
202         return -1;
203 }
204
205 /*
206  * do_setboardid command
207  * read out the board id and the hw key from the intventory EEPROM and set
208  * this values as environment variables.
209  */
210 static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
211                                 char *const argv[])
212 {
213         unsigned char buf[32];
214         char *p;
215
216         p = get_local_var("IVM_BoardId");
217         if (p == NULL) {
218                 printf("can't get the IVM_Boardid\n");
219                 return 1;
220         }
221         sprintf((char *)buf, "%s", p);
222         setenv("boardid", (char *)buf);
223         printf("set boardid=%s\n", buf);
224
225         p = get_local_var("IVM_HWKey");
226         if (p == NULL) {
227                 printf("can't get the IVM_HWKey\n");
228                 return 1;
229         }
230         sprintf((char *)buf, "%s", p);
231         setenv("hwkey", (char *)buf);
232         printf("set hwkey=%s\n", buf);
233         printf("Execute manually saveenv for persistent storage.\n");
234
235         return 0;
236 }
237
238 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
239                                  "hwkey from IVM and set in environment");
240
241 /*
242  * command km_checkbidhwk
243  *      if "boardid" and "hwkey" are not already set in the environment, do:
244  *              if a "boardIdListHex" exists in the environment:
245  *                      - read ivm data for boardid and hwkey
246  *                      - compare each entry of the boardIdListHex with the
247  *                              IVM data:
248  *                      if match:
249  *                              set environment variables boardid, boardId,
250  *                              hwkey, hwKey to the found values
251  *                              both (boardid and boardId) are set because
252  *                              they might be used differently in the
253  *                              application and in the init scripts (?)
254  *      return 0 in case of match, 1 if not match or error
255  */
256 int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
257                         char *const argv[])
258 {
259         unsigned long ivmbid = 0, ivmhwkey = 0;
260         unsigned long envbid = 0, envhwkey = 0;
261         char *p;
262         int verbose = argc > 1 && *argv[1] == 'v';
263         int rc = 0;
264
265         /*
266          * first read out the real inventory values, these values are
267          * already stored in the local hush variables
268          */
269         p = get_local_var("IVM_BoardId");
270         if (p == NULL) {
271                 printf("can't get the IVM_Boardid\n");
272                 return 1;
273         }
274         rc = strict_strtoul(p, 16, &ivmbid);
275
276         p = get_local_var("IVM_HWKey");
277         if (p == NULL) {
278                 printf("can't get the IVM_HWKey\n");
279                 return 1;
280         }
281         rc = strict_strtoul(p, 16, &ivmhwkey);
282
283         if (!ivmbid || !ivmhwkey) {
284                 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
285                 return rc;
286         }
287
288         /* now try to read values from environment if available */
289         p = getenv("boardid");
290         if (p != NULL)
291                 rc = strict_strtoul(p, 16, &envbid);
292         p = getenv("hwkey");
293         if (p != NULL)
294                 rc = strict_strtoul(p, 16, &envhwkey);
295
296         if (rc != 0) {
297                 printf("strict_strtoul returns error: %d", rc);
298                 return rc;
299         }
300
301         if (!envbid || !envhwkey) {
302                 /*
303                  * BoardId/HWkey not available in the environment, so try the
304                  * environment variable for BoardId/HWkey list
305                  */
306                 char *bidhwklist = getenv("boardIdListHex");
307
308                 if (bidhwklist) {
309                         int found = 0;
310                         char *rest = bidhwklist;
311                         char *endp;
312
313                         if (verbose) {
314                                 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
315                                         ivmbid, ivmhwkey);
316                                 printf("boardIdHwKeyList: %s\n",
317                                         bidhwklist);
318                         }
319                         while (!found) {
320                                 /* loop over each bid/hwkey pair in the list */
321                                 unsigned long bid   = 0;
322                                 unsigned long hwkey = 0;
323
324                                 while (*rest && !isxdigit(*rest))
325                                         rest++;
326                                 /*
327                                  * use simple_strtoul because we need &end and
328                                  * we know we got non numeric char at the end
329                                  */
330                                 bid = simple_strtoul(rest, &endp, 16);
331                                 /* BoardId and HWkey are separated with a "_" */
332                                 if (*endp == '_') {
333                                         rest  = endp + 1;
334                                         /*
335                                          * use simple_strtoul because we need
336                                          * &end
337                                          */
338                                         hwkey = simple_strtoul(rest, &endp, 16);
339                                         rest  = endp;
340                                         while (*rest && !isxdigit(*rest))
341                                                 rest++;
342                                 }
343                                 if ((!bid) || (!hwkey)) {
344                                         /* end of list */
345                                         break;
346                                 }
347                                 if (verbose) {
348                                         printf("trying bid=0x%lX, hwkey=%ld\n",
349                                                 bid, hwkey);
350                                 }
351                                 /*
352                                  * Compare the values of the found entry in the
353                                  * list with the valid values which are stored
354                                  * in the inventory eeprom. If they are equal
355                                  * set the values in environment variables.
356                                  */
357                                 if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
358                                         char buf[10];
359
360                                         found = 1;
361                                         envbid   = bid;
362                                         envhwkey = hwkey;
363                                         sprintf(buf, "%lx", bid);
364                                         setenv("boardid", buf);
365                                         sprintf(buf, "%lx", hwkey);
366                                         setenv("hwkey", buf);
367                                 }
368                         } /* end while( ! found ) */
369                 }
370         }
371
372         /* compare now the values */
373         if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
374                 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
375                 rc = 0; /* match */
376         } else {
377                 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
378                         envhwkey);
379                 printf("       IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
380                 rc = 1; /* don't match */
381         }
382         return rc;
383 }
384
385 U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
386                 "check boardid and hwkey",
387                 "[v]\n  - check environment parameter "\
388                 "\"boardIdListHex\" against stored boardid and hwkey "\
389                 "from the IVM\n    v: verbose output"
390 );
391
392 /*
393  * command km_checktestboot
394  *  if the testpin of the board is asserted, return 1
395  *  *   else return 0
396  */
397 int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc,
398                         char *const argv[])
399 {
400         int testpin = 0;
401         char *s = NULL;
402         int testboot = 0;
403         int verbose = argc > 1 && *argv[1] == 'v';
404
405 #if defined(CONFIG_POST)
406         testpin = post_hotkeys_pressed();
407         s = getenv("test_bank");
408 #endif
409         /* when test_bank is not set, act as if testpin is not asserted */
410         testboot = (testpin != 0) && (s);
411         if (verbose) {
412                 printf("testpin   = %d\n", testpin);
413                 printf("test_bank = %s\n", s ? s : "not set");
414                 printf("boot test app : %s\n", (testboot) ? "yes" : "no");
415         }
416         /* return 0 means: testboot, therefore we need the inversion */
417         return !testboot;
418 }
419
420 U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
421                 "check if testpin is asserted",
422                 "[v]\n  v - verbose output"
423 );