arm/km: introduce bootcount env variable and clean km_arm
[oweals/u-boot.git] / board / keymile / km_arm / km_arm.c
1 /*
2  * (C) Copyright 2009
3  * Marvell Semiconductor <www.marvell.com>
4  * Prafulla Wadaskar <prafulla@marvell.com>
5  *
6  * (C) Copyright 2009
7  * Stefan Roese, DENX Software Engineering, sr@denx.de.
8  *
9  * (C) Copyright 2010
10  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
11  *
12  * See file CREDITS for list of people who contributed to this
13  * project.
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License as
17  * published by the Free Software Foundation; either version 2 of
18  * the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
28  * MA 02110-1301 USA
29  */
30
31 #include <common.h>
32 #include <i2c.h>
33 #include <nand.h>
34 #include <netdev.h>
35 #include <miiphy.h>
36 #include <asm/io.h>
37 #include <asm/arch/kirkwood.h>
38 #include <asm/arch/mpp.h>
39
40 #include "../common/common.h"
41
42 DECLARE_GLOBAL_DATA_PTR;
43
44 /* Multi-Purpose Pins Functionality configuration */
45 u32 kwmpp_config[] = {
46         MPP0_NF_IO2,
47         MPP1_NF_IO3,
48         MPP2_NF_IO4,
49         MPP3_NF_IO5,
50         MPP4_NF_IO6,
51         MPP5_NF_IO7,
52         MPP6_SYSRST_OUTn,
53         MPP7_PEX_RST_OUTn,
54 #if defined(CONFIG_SOFT_I2C)
55         MPP8_GPIO,              /* SDA */
56         MPP9_GPIO,              /* SCL */
57 #endif
58 #if defined(CONFIG_HARD_I2C)
59         MPP8_TW_SDA,
60         MPP9_TW_SCK,
61 #endif
62         MPP10_UART0_TXD,
63         MPP11_UART0_RXD,
64         MPP12_GPO,              /* Reserved */
65         MPP13_UART1_TXD,
66         MPP14_UART1_RXD,
67         MPP15_GPIO,             /* Not used */
68         MPP16_GPIO,             /* Not used */
69         MPP17_GPIO,             /* Reserved */
70         MPP18_NF_IO0,
71         MPP19_NF_IO1,
72         MPP20_GPIO,
73         MPP21_GPIO,
74         MPP22_GPIO,
75         MPP23_GPIO,
76         MPP24_GPIO,
77         MPP25_GPIO,
78         MPP26_GPIO,
79         MPP27_GPIO,
80         MPP28_GPIO,
81         MPP29_GPIO,
82         MPP30_GPIO,
83         MPP31_GPIO,
84         MPP32_GPIO,
85         MPP33_GPIO,
86         MPP34_GPIO,             /* CDL1 (input) */
87         MPP35_GPIO,             /* CDL2 (input) */
88         MPP36_GPIO,             /* MAIN_IRQ (input) */
89         MPP37_GPIO,             /* BOARD_LED */
90         MPP38_GPIO,             /* Piggy3 LED[1] */
91         MPP39_GPIO,             /* Piggy3 LED[2] */
92         MPP40_GPIO,             /* Piggy3 LED[3] */
93         MPP41_GPIO,             /* Piggy3 LED[4] */
94         MPP42_GPIO,             /* Piggy3 LED[5] */
95         MPP43_GPIO,             /* Piggy3 LED[6] */
96         MPP44_GPIO,             /* Piggy3 LED[7], BIST_EN_L */
97         MPP45_GPIO,             /* Piggy3 LED[8] */
98         MPP46_GPIO,             /* Reserved */
99         MPP47_GPIO,             /* Reserved */
100         MPP48_GPIO,             /* Reserved */
101         MPP49_GPIO,             /* SW_INTOUTn */
102         0
103 };
104
105 int ethernet_present(void)
106 {
107         uchar   buf;
108         int     ret = 0;
109
110         if (i2c_read(0x10, 2, 1, &buf, 1) != 0) {
111                 printf("%s: Error reading Boco\n", __func__);
112                 return -1;
113         }
114         if ((buf & 0x40) == 0x40)
115                 ret = 1;
116
117         return ret;
118 }
119
120 int initialize_unit_leds(void)
121 {
122         /*
123          * init the unit LEDs
124          * per default they all are
125          * ok apart from bootstat
126          * LED connected through BOCO
127          * BOCO lies at the address  0x10
128          * LEDs are in the block CTRL_H (addr 0x02)
129          * BOOTSTAT LED is the first 0x01
130          */
131         #define BOCO        0x10
132         #define CTRL_H      0x02
133         #define APPLEDMASK  0x01
134         uchar buf;
135
136         if (i2c_read(BOCO, CTRL_H, 1, &buf, 1) != 0) {
137                 printf("%s: Error reading Boco\n", __func__);
138                 return -1;
139         }
140         buf |= APPLEDMASK;
141         if (i2c_write(BOCO, CTRL_H, 1, &buf, 1) != 0) {
142                 printf("%s: Error writing Boco\n", __func__);
143                 return -1;
144         }
145         return 0;
146 }
147
148 #if defined(CONFIG_BOOTCOUNT_LIMIT)
149 void set_bootcount_addr(void)
150 {
151         uchar buf[32];
152         unsigned int bootcountaddr;
153         bootcountaddr = gd->ram_size - BOOTCOUNT_ADDR;
154         sprintf((char *)buf, "0x%x", bootcountaddr);
155         setenv("bootcountaddr", (char *)buf);
156 }
157 #endif
158
159 int misc_init_r(void)
160 {
161         char *str;
162         int mach_type;
163
164         str = getenv("mach_type");
165         if (str != NULL) {
166                 mach_type = simple_strtoul(str, NULL, 10);
167                 printf("Overwriting MACH_TYPE with %d!!!\n", mach_type);
168                 gd->bd->bi_arch_number = mach_type;
169         }
170
171         initialize_unit_leds();
172         set_km_env();
173 #if defined(CONFIG_BOOTCOUNT_LIMIT)
174         set_bootcount_addr();
175 #endif
176         return 0;
177 }
178
179 int board_early_init_f(void)
180 {
181         u32 tmp;
182
183         kirkwood_mpp_conf(kwmpp_config);
184
185         /*
186          * The FLASH_GPIO_PIN switches between using a
187          * NAND or a SPI FLASH. Set this pin on start
188          * to NAND mode.
189          */
190         tmp = readl(KW_GPIO0_BASE);
191         writel(tmp | FLASH_GPIO_PIN , KW_GPIO0_BASE);
192         tmp = readl(KW_GPIO0_BASE + 4);
193         writel(tmp & (~FLASH_GPIO_PIN) , KW_GPIO0_BASE + 4);
194
195 #if defined(CONFIG_SOFT_I2C)
196         /* init the GPIO for I2C Bitbang driver */
197         kw_gpio_set_valid(KM_KIRKWOOD_SDA_PIN, 1);
198         kw_gpio_set_valid(KM_KIRKWOOD_SCL_PIN, 1);
199         kw_gpio_direction_output(KM_KIRKWOOD_SDA_PIN, 0);
200         kw_gpio_direction_output(KM_KIRKWOOD_SCL_PIN, 0);
201 #endif
202 #if defined(CONFIG_SYS_EEPROM_WREN)
203         kw_gpio_set_valid(KM_KIRKWOOD_ENV_WP, 38);
204         kw_gpio_direction_output(KM_KIRKWOOD_ENV_WP, 1);
205 #endif
206
207         return 0;
208 }
209
210 int board_init(void)
211 {
212         /*
213          * arch number of board
214          */
215         gd->bd->bi_arch_number = MACH_TYPE_KM_KIRKWOOD;
216
217         /* address of boot parameters */
218         gd->bd->bi_boot_params = kw_sdram_bar(0) + 0x100;
219
220         return 0;
221 }
222
223 #if defined(CONFIG_CMD_SF)
224 int do_spi_toggle(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
225 {
226         u32 tmp;
227         if (argc < 2)
228                 return cmd_usage(cmdtp);
229
230         if ((strcmp(argv[1], "off") == 0)) {
231                 printf("SPI FLASH disabled, NAND enabled\n");
232                 /* Multi-Purpose Pins Functionality configuration */
233                 kwmpp_config[0] = MPP0_NF_IO2;
234                 kwmpp_config[1] = MPP1_NF_IO3;
235                 kwmpp_config[2] = MPP2_NF_IO4;
236                 kwmpp_config[3] = MPP3_NF_IO5;
237
238                 kirkwood_mpp_conf(kwmpp_config);
239                 tmp = readl(KW_GPIO0_BASE);
240                 writel(tmp | FLASH_GPIO_PIN , KW_GPIO0_BASE);
241         } else if ((strcmp(argv[1], "on") == 0)) {
242                 printf("SPI FLASH enabled, NAND disabled\n");
243                 /* Multi-Purpose Pins Functionality configuration */
244                 kwmpp_config[0] = MPP0_SPI_SCn;
245                 kwmpp_config[1] = MPP1_SPI_MOSI;
246                 kwmpp_config[2] = MPP2_SPI_SCK;
247                 kwmpp_config[3] = MPP3_SPI_MISO;
248
249                 kirkwood_mpp_conf(kwmpp_config);
250                 tmp = readl(KW_GPIO0_BASE);
251                 writel(tmp & (~FLASH_GPIO_PIN) , KW_GPIO0_BASE);
252         } else {
253                 return cmd_usage(cmdtp);
254         }
255
256         return 0;
257 }
258
259 U_BOOT_CMD(
260         spitoggle,      2,      0,      do_spi_toggle,
261         "En-/disable SPI FLASH access",
262         "<on|off> - Enable (on) or disable (off) SPI FLASH access\n"
263         );
264 #endif
265
266 int dram_init(void)
267 {
268         /* dram_init must store complete ramsize in gd->ram_size */
269         /* Fix this */
270         gd->ram_size = get_ram_size((volatile void *)kw_sdram_bar(0),
271                                 kw_sdram_bs(0));
272         return 0;
273 }
274
275 void dram_init_banksize(void)
276 {
277         int i;
278
279         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
280                 gd->bd->bi_dram[i].start = kw_sdram_bar(i);
281                 gd->bd->bi_dram[i].size = get_ram_size((long *)kw_sdram_bar(i),
282                                                        kw_sdram_bs(i));
283         }
284 }
285
286 /* Configure and enable MV88E1118 PHY */
287 void reset_phy(void)
288 {
289         char *name = "egiga0";
290
291         if (miiphy_set_current_dev(name))
292                 return;
293
294         /* reset the phy */
295         miiphy_reset(name, CONFIG_PHY_BASE_ADR);
296 }
297
298 #if defined(CONFIG_HUSH_INIT_VAR)
299 int hush_init_var(void)
300 {
301         ivm_read_eeprom();
302         return 0;
303 }
304 #endif
305
306 #if defined(CONFIG_BOOTCOUNT_LIMIT)
307 void bootcount_store(ulong a)
308 {
309         volatile ulong *save_addr;
310         volatile ulong size = 0;
311         int i;
312         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
313                 size += gd->bd->bi_dram[i].size;
314         }
315         save_addr = (ulong*)(size - BOOTCOUNT_ADDR);
316         writel(a, save_addr);
317         writel(BOOTCOUNT_MAGIC, &save_addr[1]);
318 }
319
320 ulong bootcount_load(void)
321 {
322         volatile ulong *save_addr;
323         volatile ulong size = 0;
324         int i;
325         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
326                 size += gd->bd->bi_dram[i].size;
327         }
328         save_addr = (ulong*)(size - BOOTCOUNT_ADDR);
329         if (readl(&save_addr[1]) != BOOTCOUNT_MAGIC)
330                 return 0;
331         else
332                 return readl(save_addr);
333 }
334 #endif
335
336 #if defined(CONFIG_SOFT_I2C)
337 void set_sda(int state)
338 {
339         I2C_ACTIVE;
340         I2C_SDA(state);
341 }
342
343 void set_scl(int state)
344 {
345         I2C_SCL(state);
346 }
347
348 int get_sda(void)
349 {
350         I2C_TRISTATE;
351         return I2C_READ;
352 }
353
354 int get_scl(void)
355 {
356         return kw_gpio_get_value(KM_KIRKWOOD_SCL_PIN) ? 1 : 0;
357 }
358 #endif
359
360 #if defined(CONFIG_SYS_EEPROM_WREN)
361 int eeprom_write_enable(unsigned dev_addr, int state)
362 {
363         kw_gpio_set_value(KM_KIRKWOOD_ENV_WP, !state);
364
365         return !kw_gpio_get_value(KM_KIRKWOOD_ENV_WP);
366 }
367 #endif