]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/keymile/common/common.c
km/common: remove fdt_(gs)et_node_and_value
[karo-tx-uboot.git] / 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 #if defined(CONFIG_KM82XX)
29 #include <mpc8260.h>
30 #endif
31 #include <ioports.h>
32 #include <command.h>
33 #include <malloc.h>
34 #include <hush.h>
35 #include <net.h>
36 #include <netdev.h>
37 #include <asm/io.h>
38 #include <linux/ctype.h>
39
40 #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT)
41 #include <libfdt.h>
42 #endif
43
44 #include "common.h"
45 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
46 #include <i2c.h>
47
48 static void i2c_write_start_seq(void);
49 DECLARE_GLOBAL_DATA_PTR;
50
51 /*
52  * Set Keymile specific environment variables
53  * Currently only some memory layout variables are calculated here
54  * ... ------------------------------------------------
55  * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM
56  * ... |<------------------- pram ------------------->|
57  * ... ------------------------------------------------
58  * @END_OF_RAM: denotes the RAM size
59  * @pnvramaddr: Startadress of pseudo non volatile RAM in hex
60  * @pram      : preserved ram size in k
61  * @varaddr   : startadress for /var mounted into RAM
62  */
63 int set_km_env(void)
64 {
65         uchar buf[32];
66         unsigned int pnvramaddr;
67         unsigned int pram;
68         unsigned int varaddr;
69
70         pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
71                         - CONFIG_KM_PNVRAM;
72         sprintf((char *)buf, "0x%x", pnvramaddr);
73         setenv("pnvramaddr", (char *)buf);
74
75         pram = (CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM + CONFIG_KM_PNVRAM) /
76                 0x400;
77         sprintf((char *)buf, "0x%x", pram);
78         setenv("pram", (char *)buf);
79
80         varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
81         sprintf((char *)buf, "0x%x", varaddr);
82         setenv("varaddr", (char *)buf);
83         return 0;
84 }
85
86 #if defined(CONFIG_SYS_I2C_INIT_BOARD)
87 #define DELAY_ABORT_SEQ         62  /* @200kHz 9 clocks = 44us, 62us is ok */
88 #define DELAY_HALF_PERIOD       (500 / (CONFIG_SYS_I2C_SPEED / 1000))
89
90 #if defined(CONFIG_KM_82XX)
91 #define SDA_MASK        0x00010000
92 #define SCL_MASK        0x00020000
93 void set_pin(int state, unsigned long mask)
94 {
95         ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
96
97         if (state)
98                 setbits_be32(&iop->pdat, mask);
99         else
100                 clrbits_be32(&iop->pdat, mask);
101
102         setbits_be32(&iop->pdir, mask);
103 }
104
105 static int get_pin(unsigned long mask)
106 {
107         ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
108
109         clrbits_be32(&iop->pdir, mask);
110         return 0 != (in_be32(&iop->pdat) & mask);
111 }
112
113 static void set_sda(int state)
114 {
115         set_pin(state, SDA_MASK);
116 }
117
118 static void set_scl(int state)
119 {
120         set_pin(state, SCL_MASK);
121 }
122
123 static int get_sda(void)
124 {
125         return get_pin(SDA_MASK);
126 }
127
128 static int get_scl(void)
129 {
130         return get_pin(SCL_MASK);
131 }
132
133 #if defined(CONFIG_HARD_I2C)
134 static void setports(int gpio)
135 {
136         ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3);
137
138         if (gpio) {
139                 clrbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
140                 clrbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
141         } else {
142                 setbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK));
143                 clrbits_be32(&iop->pdir, (SDA_MASK | SCL_MASK));
144                 setbits_be32(&iop->podr, (SDA_MASK | SCL_MASK));
145         }
146 }
147 #endif
148 #endif
149
150 #if !defined(CONFIG_MPC83xx)
151 static void i2c_write_start_seq(void)
152 {
153         set_sda(1);
154         udelay(DELAY_HALF_PERIOD);
155         set_scl(1);
156         udelay(DELAY_HALF_PERIOD);
157         set_sda(0);
158         udelay(DELAY_HALF_PERIOD);
159         set_scl(0);
160         udelay(DELAY_HALF_PERIOD);
161 }
162
163 /*
164  * I2C is a synchronous protocol and resets of the processor in the middle
165  * of an access can block the I2C Bus until a powerdown of the full unit is
166  * done. This function toggles the SCL until the SCL and SCA line are
167  * released, but max. 16 times, after this a I2C start-sequence is sent.
168  * This I2C Deblocking mechanism was developed by Keymile in association
169  * with Anatech and Atmel in 1998.
170  */
171 int i2c_make_abort(void)
172 {
173
174 #if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
175         immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
176         i2c8260_t *i2c  = (i2c8260_t *)&immap->im_i2c;
177
178         /*
179          * disable I2C controller first, otherwhise it thinks we want to
180          * talk to the slave port...
181          */
182         clrbits_8(&i2c->i2c_i2mod, 0x01);
183
184         /* Set the PortPins to GPIO */
185         setports(1);
186 #endif
187
188         int     scl_state = 0;
189         int     sda_state = 0;
190         int     i = 0;
191         int     ret = 0;
192
193         if (!get_sda()) {
194                 ret = -1;
195                 while (i < 16) {
196                         i++;
197                         set_scl(0);
198                         udelay(DELAY_ABORT_SEQ);
199                         set_scl(1);
200                         udelay(DELAY_ABORT_SEQ);
201                         scl_state = get_scl();
202                         sda_state = get_sda();
203                         if (scl_state && sda_state) {
204                                 ret = 0;
205                                 break;
206                         }
207                 }
208         }
209         if (ret == 0)
210                 for (i = 0; i < 5; i++)
211                         i2c_write_start_seq();
212
213         /* respect stop setup time */
214         udelay(DELAY_ABORT_SEQ);
215         set_scl(1);
216         udelay(DELAY_ABORT_SEQ);
217         set_sda(1);
218         get_sda();
219
220 #if defined(CONFIG_HARD_I2C)
221         /* Set the PortPins back to use for I2C */
222         setports(0);
223 #endif
224         return ret;
225 }
226 #endif
227
228 #if defined(CONFIG_MPC83xx)
229 static void i2c_write_start_seq(void)
230 {
231         struct fsl_i2c *dev;
232         dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
233         udelay(DELAY_ABORT_SEQ);
234         out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
235         udelay(DELAY_ABORT_SEQ);
236         out_8(&dev->cr, (I2C_CR_MEN));
237 }
238
239 int i2c_make_abort(void)
240 {
241         struct fsl_i2c *dev;
242         dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
243         uchar   dummy;
244         uchar   last;
245         int     nbr_read = 0;
246         int     i = 0;
247         int         ret = 0;
248
249         /* wait after each operation to finsh with a delay */
250         out_8(&dev->cr, (I2C_CR_MSTA));
251         udelay(DELAY_ABORT_SEQ);
252         out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
253         udelay(DELAY_ABORT_SEQ);
254         dummy = in_8(&dev->dr);
255         udelay(DELAY_ABORT_SEQ);
256         last = in_8(&dev->dr);
257         nbr_read++;
258
259         /*
260          * do read until the last bit is 1, but stop if the full eeprom is
261          * read.
262          */
263         while (((last & 0x01) != 0x01) &&
264                 (nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) {
265                 udelay(DELAY_ABORT_SEQ);
266                 last = in_8(&dev->dr);
267                 nbr_read++;
268         }
269         if ((last & 0x01) != 0x01)
270                 ret = -2;
271         if ((last != 0xff) || (nbr_read > 1))
272                 printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
273                         nbr_read, last);
274         udelay(DELAY_ABORT_SEQ);
275         out_8(&dev->cr, (I2C_CR_MEN));
276         udelay(DELAY_ABORT_SEQ);
277         /* clear status reg */
278         out_8(&dev->sr, 0);
279
280         for (i = 0; i < 5; i++)
281                 i2c_write_start_seq();
282         if (ret != 0)
283                 printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
284                         nbr_read, last);
285
286         return ret;
287 }
288 #endif
289
290 /**
291  * i2c_init_board - reset i2c bus. When the board is powercycled during a
292  * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
293  */
294 void i2c_init_board(void)
295 {
296         /* Now run the AbortSequence() */
297         i2c_make_abort();
298 }
299 #endif
300 #endif
301
302 #if !defined(MACH_TYPE_KM_KIRKWOOD)
303 int ethernet_present(void)
304 {
305         struct km_bec_fpga *base =
306                 (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
307
308         return in_8(&base->bprth) & PIGGY_PRESENT;
309 }
310 #endif
311
312 int board_eth_init(bd_t *bis)
313 {
314         if (ethernet_present())
315                 return cpu_eth_init(bis);
316
317         return -1;
318 }
319
320 /*
321  * do_setboardid command
322  * read out the board id and the hw key from the intventory EEPROM and set
323  * this values as environment variables.
324  */
325 static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
326                                 char *const argv[])
327 {
328         unsigned char buf[32];
329         char *p;
330
331         p = get_local_var("IVM_BoardId");
332         if (p == NULL) {
333                 printf("can't get the IVM_Boardid\n");
334                 return 1;
335         }
336         sprintf((char *)buf, "%s", p);
337         setenv("boardid", (char *)buf);
338
339         p = get_local_var("IVM_HWKey");
340         if (p == NULL) {
341                 printf("can't get the IVM_HWKey\n");
342                 return 1;
343         }
344         sprintf((char *)buf, "%s", p);
345         setenv("hwkey", (char *)buf);
346
347         return 0;
348 }
349
350 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
351                                  "hwkey from IVM and set in environment");
352
353 /*
354  * command km_checkbidhwk
355  *      if "boardid" and "hwkey" are not already set in the environment, do:
356  *              if a "boardIdListHex" exists in the environment:
357  *                      - read ivm data for boardid and hwkey
358  *                      - compare each entry of the boardIdListHex with the
359  *                              IVM data:
360  *                      if match:
361  *                              set environment variables boardid, boardId,
362  *                              hwkey, hwKey to the found values
363  *                              both (boardid and boardId) are set because
364  *                              they might be used differently in the
365  *                              application and in the init scripts (?)
366  *      return 0 in case of match, 1 if not match or error
367  */
368 int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
369                         char *const argv[])
370 {
371         unsigned long ivmbid = 0, ivmhwkey = 0;
372         unsigned long envbid = 0, envhwkey = 0;
373         char *p;
374         int verbose = argc > 1 && *argv[1] == 'v';
375         int rc = 0;
376
377         /*
378          * first read out the real inventory values, these values are
379          * already stored in the local hush variables
380          */
381         p = get_local_var("IVM_BoardId");
382         if (p == NULL) {
383                 printf("can't get the IVM_Boardid\n");
384                 return 1;
385         }
386         rc = strict_strtoul(p, 16, &ivmbid);
387
388         p = get_local_var("IVM_HWKey");
389         if (p == NULL) {
390                 printf("can't get the IVM_HWKey\n");
391                 return 1;
392         }
393         rc = strict_strtoul(p, 16, &ivmhwkey);
394
395         if (!ivmbid || !ivmhwkey) {
396                 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
397                 return rc;
398         }
399
400         /* now try to read values from environment if available */
401         p = getenv("boardid");
402         if (p != NULL)
403                 rc = strict_strtoul(p, 16, &envbid);
404         p = getenv("hwkey");
405         if (p != NULL)
406                 rc = strict_strtoul(p, 16, &envhwkey);
407
408         if (rc != 0) {
409                 printf("strict_strtoul returns error: %d", rc);
410                 return rc;
411         }
412
413         if (!envbid || !envhwkey) {
414                 /*
415                  * BoardId/HWkey not available in the environment, so try the
416                  * environment variable for BoardId/HWkey list
417                  */
418                 char *bidhwklist = getenv("boardIdListHex");
419
420                 if (bidhwklist) {
421                         int found = 0;
422                         char *rest = bidhwklist;
423                         char *endp;
424
425                         if (verbose) {
426                                 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
427                                         ivmbid, ivmhwkey);
428                                 printf("boardIdHwKeyList: %s\n",
429                                         bidhwklist);
430                         }
431                         while (!found) {
432                                 /* loop over each bid/hwkey pair in the list */
433                                 unsigned long bid   = 0;
434                                 unsigned long hwkey = 0;
435
436                                 while (*rest && !isxdigit(*rest))
437                                         rest++;
438                                 /*
439                                  * use simple_strtoul because we need &end and
440                                  * we know we got non numeric char at the end
441                                  */
442                                 bid = simple_strtoul(rest, &endp, 16);
443                                 /* BoardId and HWkey are separated with a "_" */
444                                 if (*endp == '_') {
445                                         rest  = endp + 1;
446                                         /*
447                                          * use simple_strtoul because we need
448                                          * &end
449                                          */
450                                         hwkey = simple_strtoul(rest, &endp, 16);
451                                         rest  = endp;
452                                         while (*rest && !isxdigit(*rest))
453                                                 rest++;
454                                 }
455                                 if ((!bid) || (!hwkey)) {
456                                         /* end of list */
457                                         break;
458                                 }
459                                 if (verbose) {
460                                         printf("trying bid=0x%lX, hwkey=%ld\n",
461                                                 bid, hwkey);
462                                 }
463                                 /*
464                                  * Compare the values of the found entry in the
465                                  * list with the valid values which are stored
466                                  * in the inventory eeprom. If they are equal
467                                  * set the values in environment variables.
468                                  */
469                                 if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
470                                         char buf[10];
471
472                                         found = 1;
473                                         envbid   = bid;
474                                         envhwkey = hwkey;
475                                         sprintf(buf, "%lx", bid);
476                                         setenv("boardid", buf);
477                                         sprintf(buf, "%lx", hwkey);
478                                         setenv("hwkey", buf);
479                                 }
480                         } /* end while( ! found ) */
481                 }
482         }
483
484         /* compare now the values */
485         if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
486                 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
487                 rc = 0; /* match */
488         } else {
489                 printf("Error: env bId=0x%3lX, hwKey=%ld\n", envbid, envhwkey);
490                 printf("       IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
491                 rc = 1; /* don't match */
492         }
493         return rc;
494 }
495
496 U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
497                 "check boardid and hwkey",
498                 "[v]\n  - check environment parameter "\
499                 "\"boardIdListHex\" against stored boardid and hwkey "\
500                 "from the IVM\n    v: verbose output"
501 );