Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2008 |
| 3 | * Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 4 | * |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame^] | 5 | * (C) Copyright 2011 |
| 6 | * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com |
| 7 | * |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 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> |
Holger Brunck | 489337f | 2011-05-02 22:56:55 +0000 | [diff] [blame] | 28 | #if defined(CONFIG_KM82XX) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 29 | #include <mpc8260.h> |
Heiko Schocher | 210c8c0 | 2008-11-21 08:29:40 +0100 | [diff] [blame] | 30 | #endif |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 31 | #include <ioports.h> |
Holger Brunck | a9417ce | 2011-05-04 01:47:30 +0000 | [diff] [blame] | 32 | #include <command.h> |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 33 | #include <malloc.h> |
Heiko Schocher | 8f64da7 | 2008-10-15 09:41:00 +0200 | [diff] [blame] | 34 | #include <hush.h> |
Heiko Schocher | 210c8c0 | 2008-11-21 08:29:40 +0100 | [diff] [blame] | 35 | #include <net.h> |
Heiko Schocher | 62ddcf0 | 2010-02-18 08:08:25 +0100 | [diff] [blame] | 36 | #include <netdev.h> |
Heiko Schocher | 210c8c0 | 2008-11-21 08:29:40 +0100 | [diff] [blame] | 37 | #include <asm/io.h> |
Thomas Herzmann | 92c9108 | 2011-05-12 19:59:22 +0000 | [diff] [blame] | 38 | #include <linux/ctype.h> |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 39 | |
| 40 | #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT) |
| 41 | #include <libfdt.h> |
| 42 | #endif |
| 43 | |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame^] | 44 | #include "common.h" |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 45 | #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C) |
| 46 | #include <i2c.h> |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 47 | |
Heiko Schocher | 6c11aea | 2011-03-08 10:51:15 +0100 | [diff] [blame] | 48 | static void i2c_write_start_seq(void); |
Heiko Schocher | f1fef1d | 2010-04-26 13:07:28 +0200 | [diff] [blame] | 49 | DECLARE_GLOBAL_DATA_PTR; |
Heiko Schocher | 6c11aea | 2011-03-08 10:51:15 +0100 | [diff] [blame] | 50 | |
Heiko Schocher | f1fef1d | 2010-04-26 13:07:28 +0200 | [diff] [blame] | 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 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 86 | #if defined(CONFIG_SYS_I2C_INIT_BOARD) |
Heiko Schocher | 6c11aea | 2011-03-08 10:51:15 +0100 | [diff] [blame] | 87 | #define DELAY_ABORT_SEQ 62 /* @200kHz 9 clocks = 44us, 62us is ok */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 88 | #define DELAY_HALF_PERIOD (500 / (CONFIG_SYS_I2C_SPEED / 1000)) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 89 | |
Holger Brunck | 489337f | 2011-05-02 22:56:55 +0000 | [diff] [blame] | 90 | #if defined(CONFIG_KM_82XX) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 91 | #define SDA_MASK 0x00010000 |
| 92 | #define SCL_MASK 0x00020000 |
Holger Brunck | 489337f | 2011-05-02 22:56:55 +0000 | [diff] [blame] | 93 | void set_pin(int state, unsigned long mask) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 94 | { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 95 | ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 96 | |
| 97 | if (state) |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 98 | setbits_be32(&iop->pdat, mask); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 99 | else |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 100 | clrbits_be32(&iop->pdat, mask); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 101 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 102 | setbits_be32(&iop->pdir, mask); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 105 | static int get_pin(unsigned long mask) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 106 | { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 107 | ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 108 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 109 | clrbits_be32(&iop->pdir, mask); |
| 110 | return 0 != (in_be32(&iop->pdat) & mask); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 113 | static void set_sda(int state) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 114 | { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 115 | set_pin(state, SDA_MASK); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 118 | static void set_scl(int state) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 119 | { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 120 | set_pin(state, SCL_MASK); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 123 | static int get_sda(void) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 124 | { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 125 | return get_pin(SDA_MASK); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 128 | static int get_scl(void) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 129 | { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 130 | return get_pin(SCL_MASK); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | #if defined(CONFIG_HARD_I2C) |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 134 | static void setports(int gpio) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 135 | { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 136 | ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, 3); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 137 | |
| 138 | if (gpio) { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 139 | clrbits_be32(&iop->ppar, (SDA_MASK | SCL_MASK)); |
| 140 | clrbits_be32(&iop->podr, (SDA_MASK | SCL_MASK)); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 141 | } else { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 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)); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | #endif |
| 148 | #endif |
| 149 | |
Heiko Schocher | 62ddcf0 | 2010-02-18 08:08:25 +0100 | [diff] [blame] | 150 | #if !defined(CONFIG_MPC83xx) |
Heiko Schocher | 6c11aea | 2011-03-08 10:51:15 +0100 | [diff] [blame] | 151 | static void i2c_write_start_seq(void) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 152 | { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 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); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 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. |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 170 | */ |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame^] | 171 | int i2c_make_abort(void) |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 172 | { |
Heiko Schocher | 6c11aea | 2011-03-08 10:51:15 +0100 | [diff] [blame] | 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 | |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 188 | int scl_state = 0; |
| 189 | int sda_state = 0; |
| 190 | int i = 0; |
| 191 | int ret = 0; |
| 192 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 193 | if (!get_sda()) { |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 194 | ret = -1; |
| 195 | while (i < 16) { |
| 196 | i++; |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 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(); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 203 | if (scl_state && sda_state) { |
| 204 | ret = 0; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | } |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 209 | if (ret == 0) |
| 210 | for (i = 0; i < 5; i++) |
Heiko Schocher | 6c11aea | 2011-03-08 10:51:15 +0100 | [diff] [blame] | 211 | i2c_write_start_seq(); |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 212 | |
Heiko Schocher | 6c11aea | 2011-03-08 10:51:15 +0100 | [diff] [blame] | 213 | /* respect stop setup time */ |
| 214 | udelay(DELAY_ABORT_SEQ); |
| 215 | set_scl(1); |
| 216 | udelay(DELAY_ABORT_SEQ); |
| 217 | set_sda(1); |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 218 | get_sda(); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 219 | |
| 220 | #if defined(CONFIG_HARD_I2C) |
| 221 | /* Set the PortPins back to use for I2C */ |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 222 | setports(0); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 223 | #endif |
Heiko Schocher | 6c11aea | 2011-03-08 10:51:15 +0100 | [diff] [blame] | 224 | return ret; |
| 225 | } |
Heiko Schocher | 39df00d | 2009-07-09 12:04:26 +0200 | [diff] [blame] | 226 | #endif |
Heiko Schocher | 6c11aea | 2011-03-08 10:51:15 +0100 | [diff] [blame] | 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 | |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame^] | 239 | int i2c_make_abort(void) |
Heiko Schocher | 6c11aea | 2011-03-08 10:51:15 +0100 | [diff] [blame] | 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(); |
Heiko Schocher | c248536 | 2008-10-15 09:39:08 +0200 | [diff] [blame] | 298 | } |
| 299 | #endif |
Heiko Schocher | 210c8c0 | 2008-11-21 08:29:40 +0100 | [diff] [blame] | 300 | #endif |
Heiko Schocher | 6250f0f | 2008-10-17 16:11:52 +0200 | [diff] [blame] | 301 | |
| 302 | #if defined(CONFIG_OF_BOARD_SETUP) && defined(CONFIG_OF_LIBFDT) |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 303 | int fdt_set_node_and_value(void *blob, |
Heiko Schocher | 6250f0f | 2008-10-17 16:11:52 +0200 | [diff] [blame] | 304 | char *nodename, |
| 305 | char *regname, |
| 306 | void *var, |
| 307 | int size) |
| 308 | { |
| 309 | int ret = 0; |
| 310 | int nodeoffset = 0; |
| 311 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 312 | nodeoffset = fdt_path_offset(blob, nodename); |
Heiko Schocher | 6250f0f | 2008-10-17 16:11:52 +0200 | [diff] [blame] | 313 | if (nodeoffset >= 0) { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 314 | ret = fdt_setprop(blob, nodeoffset, regname, var, |
Heiko Schocher | 6250f0f | 2008-10-17 16:11:52 +0200 | [diff] [blame] | 315 | size); |
| 316 | if (ret < 0) |
| 317 | printf("ft_blob_update(): cannot set %s/%s " |
| 318 | "property err:%s\n", nodename, regname, |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 319 | fdt_strerror(ret)); |
Heiko Schocher | 6250f0f | 2008-10-17 16:11:52 +0200 | [diff] [blame] | 320 | } else { |
| 321 | printf("ft_blob_update(): cannot find %s node " |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 322 | "err:%s\n", nodename, fdt_strerror(nodeoffset)); |
Heiko Schocher | 6250f0f | 2008-10-17 16:11:52 +0200 | [diff] [blame] | 323 | } |
| 324 | return ret; |
| 325 | } |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 326 | |
| 327 | int fdt_get_node_and_value(void *blob, |
Heiko Schocher | dc71b24 | 2009-07-09 12:04:18 +0200 | [diff] [blame] | 328 | char *nodename, |
| 329 | char *propname, |
| 330 | void **var) |
| 331 | { |
| 332 | int len; |
| 333 | int nodeoffset = 0; |
| 334 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 335 | nodeoffset = fdt_path_offset(blob, nodename); |
Heiko Schocher | dc71b24 | 2009-07-09 12:04:18 +0200 | [diff] [blame] | 336 | if (nodeoffset >= 0) { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 337 | *var = (void *)fdt_getprop(blob, nodeoffset, propname, &len); |
Heiko Schocher | dc71b24 | 2009-07-09 12:04:18 +0200 | [diff] [blame] | 338 | if (len == 0) { |
| 339 | /* no value */ |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 340 | printf("%s no value\n", __func__); |
Heiko Schocher | dc71b24 | 2009-07-09 12:04:18 +0200 | [diff] [blame] | 341 | return -1; |
| 342 | } else if (len > 0) { |
| 343 | return len; |
| 344 | } else { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 345 | printf("libfdt fdt_getprop(): %s\n", |
Heiko Schocher | dc71b24 | 2009-07-09 12:04:18 +0200 | [diff] [blame] | 346 | fdt_strerror(len)); |
| 347 | return -2; |
| 348 | } |
| 349 | } else { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 350 | printf("%s: cannot find %s node err:%s\n", __func__, |
| 351 | nodename, fdt_strerror(nodeoffset)); |
Heiko Schocher | dc71b24 | 2009-07-09 12:04:18 +0200 | [diff] [blame] | 352 | return -3; |
| 353 | } |
| 354 | } |
Heiko Schocher | 6250f0f | 2008-10-17 16:11:52 +0200 | [diff] [blame] | 355 | #endif |
Heiko Schocher | 210c8c0 | 2008-11-21 08:29:40 +0100 | [diff] [blame] | 356 | |
Holger Brunck | 802d996 | 2011-03-14 15:31:19 +0100 | [diff] [blame] | 357 | #if !defined(MACH_TYPE_KM_KIRKWOOD) |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 358 | int ethernet_present(void) |
Heiko Schocher | 210c8c0 | 2008-11-21 08:29:40 +0100 | [diff] [blame] | 359 | { |
Heiko Schocher | 8ed7434 | 2011-03-08 10:47:39 +0100 | [diff] [blame] | 360 | struct km_bec_fpga *base = |
| 361 | (struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE; |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 362 | |
| 363 | return in_8(&base->bprth) & PIGGY_PRESENT; |
Heiko Schocher | 210c8c0 | 2008-11-21 08:29:40 +0100 | [diff] [blame] | 364 | } |
Heiko Schocher | 67fa8c2 | 2010-02-22 16:43:02 +0530 | [diff] [blame] | 365 | #endif |
Heiko Schocher | 210c8c0 | 2008-11-21 08:29:40 +0100 | [diff] [blame] | 366 | |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 367 | int board_eth_init(bd_t *bis) |
Heiko Schocher | 210c8c0 | 2008-11-21 08:29:40 +0100 | [diff] [blame] | 368 | { |
Heiko Schocher | b11f53f | 2011-03-15 16:52:29 +0100 | [diff] [blame] | 369 | if (ethernet_present()) |
Heiko Schocher | 62ddcf0 | 2010-02-18 08:08:25 +0100 | [diff] [blame] | 370 | return cpu_eth_init(bis); |
| 371 | |
| 372 | return -1; |
Heiko Schocher | 210c8c0 | 2008-11-21 08:29:40 +0100 | [diff] [blame] | 373 | } |
Holger Brunck | a9417ce | 2011-05-04 01:47:30 +0000 | [diff] [blame] | 374 | |
| 375 | /* |
| 376 | * do_setboardid command |
| 377 | * read out the board id and the hw key from the intventory EEPROM and set |
| 378 | * this values as environment variables. |
| 379 | */ |
| 380 | static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc, |
| 381 | char *const argv[]) |
| 382 | { |
| 383 | unsigned char buf[32]; |
| 384 | char *p; |
| 385 | |
| 386 | p = get_local_var("IVM_BoardId"); |
| 387 | if (p == NULL) { |
| 388 | printf("can't get the IVM_Boardid\n"); |
| 389 | return 1; |
| 390 | } |
| 391 | sprintf((char *)buf, "%s", p); |
| 392 | setenv("boardid", (char *)buf); |
| 393 | |
| 394 | p = get_local_var("IVM_HWKey"); |
| 395 | if (p == NULL) { |
| 396 | printf("can't get the IVM_HWKey\n"); |
| 397 | return 1; |
| 398 | } |
| 399 | sprintf((char *)buf, "%s", p); |
| 400 | setenv("hwkey", (char *)buf); |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and " |
| 406 | "hwkey from IVM and set in environment"); |
Thomas Herzmann | 92c9108 | 2011-05-12 19:59:22 +0000 | [diff] [blame] | 407 | |
| 408 | /* |
| 409 | * command km_checkbidhwk |
| 410 | * if "boardid" and "hwkey" are not already set in the environment, do: |
| 411 | * if a "boardIdListHex" exists in the environment: |
| 412 | * - read ivm data for boardid and hwkey |
| 413 | * - compare each entry of the boardIdListHex with the |
| 414 | * IVM data: |
| 415 | * if match: |
| 416 | * set environment variables boardid, boardId, |
| 417 | * hwkey, hwKey to the found values |
| 418 | * both (boardid and boardId) are set because |
| 419 | * they might be used differently in the |
| 420 | * application and in the init scripts (?) |
| 421 | * return 0 in case of match, 1 if not match or error |
| 422 | */ |
| 423 | int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc, |
| 424 | char *const argv[]) |
| 425 | { |
| 426 | unsigned long ivmbid = 0, ivmhwkey = 0; |
| 427 | unsigned long envbid = 0, envhwkey = 0; |
| 428 | char *p; |
| 429 | int verbose = argc > 1 && *argv[1] == 'v'; |
| 430 | int rc = 0; |
| 431 | |
| 432 | /* |
| 433 | * first read out the real inventory values, these values are |
| 434 | * already stored in the local hush variables |
| 435 | */ |
| 436 | p = get_local_var("IVM_BoardId"); |
| 437 | if (p == NULL) { |
| 438 | printf("can't get the IVM_Boardid\n"); |
| 439 | return 1; |
| 440 | } |
| 441 | rc = strict_strtoul(p, 16, &ivmbid); |
| 442 | |
| 443 | p = get_local_var("IVM_HWKey"); |
| 444 | if (p == NULL) { |
| 445 | printf("can't get the IVM_HWKey\n"); |
| 446 | return 1; |
| 447 | } |
| 448 | rc = strict_strtoul(p, 16, &ivmhwkey); |
| 449 | |
| 450 | if (!ivmbid || !ivmhwkey) { |
| 451 | printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n"); |
| 452 | return rc; |
| 453 | } |
| 454 | |
| 455 | /* now try to read values from environment if available */ |
| 456 | p = getenv("boardid"); |
| 457 | if (p != NULL) |
| 458 | rc = strict_strtoul(p, 16, &envbid); |
| 459 | p = getenv("hwkey"); |
| 460 | if (p != NULL) |
| 461 | rc = strict_strtoul(p, 16, &envhwkey); |
| 462 | |
| 463 | if (rc != 0) { |
| 464 | printf("strict_strtoul returns error: %d", rc); |
| 465 | return rc; |
| 466 | } |
| 467 | |
| 468 | if (!envbid || !envhwkey) { |
| 469 | /* |
| 470 | * BoardId/HWkey not available in the environment, so try the |
| 471 | * environment variable for BoardId/HWkey list |
| 472 | */ |
| 473 | char *bidhwklist = getenv("boardIdListHex"); |
| 474 | |
| 475 | if (bidhwklist) { |
| 476 | int found = 0; |
| 477 | char *rest = bidhwklist; |
| 478 | char *endp; |
| 479 | |
| 480 | if (verbose) { |
| 481 | printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n", |
| 482 | ivmbid, ivmhwkey); |
| 483 | printf("boardIdHwKeyList: %s\n", |
| 484 | bidhwklist); |
| 485 | } |
| 486 | while (!found) { |
| 487 | /* loop over each bid/hwkey pair in the list */ |
| 488 | unsigned long bid = 0; |
| 489 | unsigned long hwkey = 0; |
| 490 | |
| 491 | while (*rest && !isxdigit(*rest)) |
| 492 | rest++; |
| 493 | /* |
| 494 | * use simple_strtoul because we need &end and |
| 495 | * we know we got non numeric char at the end |
| 496 | */ |
| 497 | bid = simple_strtoul(rest, &endp, 16); |
| 498 | /* BoardId and HWkey are separated with a "_" */ |
| 499 | if (*endp == '_') { |
| 500 | rest = endp + 1; |
| 501 | /* |
| 502 | * use simple_strtoul because we need |
| 503 | * &end |
| 504 | */ |
| 505 | hwkey = simple_strtoul(rest, &endp, 16); |
| 506 | rest = endp; |
| 507 | while (*rest && !isxdigit(*rest)) |
| 508 | rest++; |
| 509 | } |
| 510 | if ((!bid) || (!hwkey)) { |
| 511 | /* end of list */ |
| 512 | break; |
| 513 | } |
| 514 | if (verbose) { |
| 515 | printf("trying bid=0x%lX, hwkey=%ld\n", |
| 516 | bid, hwkey); |
| 517 | } |
| 518 | /* |
| 519 | * Compare the values of the found entry in the |
| 520 | * list with the valid values which are stored |
| 521 | * in the inventory eeprom. If they are equal |
Holger Brunck | ba8be32 | 2011-06-05 22:22:17 +0000 | [diff] [blame] | 522 | * set the values in environment variables. |
Thomas Herzmann | 92c9108 | 2011-05-12 19:59:22 +0000 | [diff] [blame] | 523 | */ |
| 524 | if ((bid == ivmbid) && (hwkey == ivmhwkey)) { |
| 525 | char buf[10]; |
| 526 | |
| 527 | found = 1; |
| 528 | envbid = bid; |
| 529 | envhwkey = hwkey; |
| 530 | sprintf(buf, "%lx", bid); |
| 531 | setenv("boardid", buf); |
| 532 | sprintf(buf, "%lx", hwkey); |
| 533 | setenv("hwkey", buf); |
Thomas Herzmann | 92c9108 | 2011-05-12 19:59:22 +0000 | [diff] [blame] | 534 | } |
| 535 | } /* end while( ! found ) */ |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /* compare now the values */ |
| 540 | if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) { |
| 541 | printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey); |
| 542 | rc = 0; /* match */ |
| 543 | } else { |
| 544 | printf("Error: env bId=0x%3lX, hwKey=%ld\n", envbid, envhwkey); |
| 545 | printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey); |
| 546 | rc = 1; /* don't match */ |
| 547 | } |
| 548 | return rc; |
| 549 | } |
| 550 | |
| 551 | U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk, |
| 552 | "check boardid and hwkey", |
| 553 | "[v]\n - check environment parameter "\ |
| 554 | "\"boardIdListHex\" against stored boardid and hwkey "\ |
| 555 | "from the IVM\n v: verbose output" |
| 556 | ); |