am33xx/board.c: make wdtimer/uart_base static
[platform/kernel/u-boot.git] / arch / arm / cpu / armv7 / am33xx / board.c
1 /*
2  * board.c
3  *
4  * Common board functions for AM33XX based boards
5  *
6  * Copyright (C) 2011, Texas Instruments, Incorporated - http://www.ti.com/
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR /PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18
19 #include <common.h>
20 #include <errno.h>
21 #include <spl.h>
22 #include <asm/arch/cpu.h>
23 #include <asm/arch/hardware.h>
24 #include <asm/arch/omap.h>
25 #include <asm/arch/ddr_defs.h>
26 #include <asm/arch/clock.h>
27 #include <asm/arch/gpio.h>
28 #include <asm/arch/mmc_host_def.h>
29 #include <asm/arch/sys_proto.h>
30 #include <asm/io.h>
31 #include <asm/emif.h>
32 #include <asm/gpio.h>
33 #include <i2c.h>
34 #include <miiphy.h>
35 #include <cpsw.h>
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 static struct wd_timer *wdtimer = (struct wd_timer *)WDT_BASE;
40 #ifdef CONFIG_SPL_BUILD
41 static struct uart_sys *uart_base = (struct uart_sys *)DEFAULT_UART_BASE;
42 #endif
43
44 static const struct gpio_bank gpio_bank_am33xx[4] = {
45         { (void *)AM33XX_GPIO0_BASE, METHOD_GPIO_24XX },
46         { (void *)AM33XX_GPIO1_BASE, METHOD_GPIO_24XX },
47         { (void *)AM33XX_GPIO2_BASE, METHOD_GPIO_24XX },
48         { (void *)AM33XX_GPIO3_BASE, METHOD_GPIO_24XX },
49 };
50
51 const struct gpio_bank *const omap_gpio_bank = gpio_bank_am33xx;
52
53 /* MII mode defines */
54 #define MII_MODE_ENABLE         0x0
55 #define RGMII_MODE_ENABLE       0xA
56
57 /* GPIO that controls power to DDR on EVM-SK */
58 #define GPIO_DDR_VTT_EN         7
59
60 static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
61
62 static struct am335x_baseboard_id __attribute__((section (".data"))) header;
63
64 static inline int board_is_bone(void)
65 {
66         return !strncmp(header.name, "A335BONE", HDR_NAME_LEN);
67 }
68
69 static inline int board_is_bone_lt(void)
70 {
71         return !strncmp(header.name, "A335BNLT", HDR_NAME_LEN);
72 }
73
74 static inline int board_is_evm_sk(void)
75 {
76         return !strncmp("A335X_SK", header.name, HDR_NAME_LEN);
77 }
78
79 /*
80  * Read header information from EEPROM into global structure.
81  */
82 static int read_eeprom(void)
83 {
84         /* Check if baseboard eeprom is available */
85         if (i2c_probe(CONFIG_SYS_I2C_EEPROM_ADDR)) {
86                 puts("Could not probe the EEPROM; something fundamentally "
87                         "wrong on the I2C bus.\n");
88                 return -ENODEV;
89         }
90
91         /* read the eeprom using i2c */
92         if (i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 2, (uchar *)&header,
93                                                         sizeof(header))) {
94                 puts("Could not read the EEPROM; something fundamentally"
95                         " wrong on the I2C bus.\n");
96                 return -EIO;
97         }
98
99         if (header.magic != 0xEE3355AA) {
100                 /*
101                  * read the eeprom using i2c again,
102                  * but use only a 1 byte address
103                  */
104                 if (i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, 1,
105                                         (uchar *)&header, sizeof(header))) {
106                         puts("Could not read the EEPROM; something "
107                                 "fundamentally wrong on the I2C bus.\n");
108                         return -EIO;
109                 }
110
111                 if (header.magic != 0xEE3355AA) {
112                         printf("Incorrect magic number (0x%x) in EEPROM\n",
113                                         header.magic);
114                         return -EINVAL;
115                 }
116         }
117
118         return 0;
119 }
120
121 #ifdef CONFIG_SPL_BUILD
122 /* UART Defines */
123 #define UART_RESET              (0x1 << 1)
124 #define UART_CLK_RUNNING_MASK   0x1
125 #define UART_SMART_IDLE_EN      (0x1 << 0x3)
126
127 static void rtc32k_enable(void)
128 {
129         struct rtc_regs *rtc = (struct rtc_regs *)AM335X_RTC_BASE;
130
131         /*
132          * Unlock the RTC's registers.  For more details please see the
133          * RTC_SS section of the TRM.  In order to unlock we need to
134          * write these specific values (keys) in this order.
135          */
136         writel(0x83e70b13, &rtc->kick0r);
137         writel(0x95a4f1e0, &rtc->kick1r);
138
139         /* Enable the RTC 32K OSC by setting bits 3 and 6. */
140         writel((1 << 3) | (1 << 6), &rtc->osc);
141 }
142 #endif
143
144 /*
145  * Determine what type of DDR we have.
146  */
147 static short inline board_memory_type(void)
148 {
149         /* The following boards are known to use DDR3. */
150         if (board_is_evm_sk() || board_is_bone_lt())
151                 return EMIF_REG_SDRAM_TYPE_DDR3;
152
153         return EMIF_REG_SDRAM_TYPE_DDR2;
154 }
155
156 /*
157  * early system init of muxing and clocks.
158  */
159 void s_init(void)
160 {
161         /* WDT1 is already running when the bootloader gets control
162          * Disable it to avoid "random" resets
163          */
164         writel(0xAAAA, &wdtimer->wdtwspr);
165         while (readl(&wdtimer->wdtwwps) != 0x0)
166                 ;
167         writel(0x5555, &wdtimer->wdtwspr);
168         while (readl(&wdtimer->wdtwwps) != 0x0)
169                 ;
170
171 #ifdef CONFIG_SPL_BUILD
172         /* Setup the PLLs and the clocks for the peripherals */
173         pll_init();
174
175         /* Enable RTC32K clock */
176         rtc32k_enable();
177
178         /* UART softreset */
179         u32 regVal;
180
181         enable_uart0_pin_mux();
182
183         regVal = readl(&uart_base->uartsyscfg);
184         regVal |= UART_RESET;
185         writel(regVal, &uart_base->uartsyscfg);
186         while ((readl(&uart_base->uartsyssts) &
187                 UART_CLK_RUNNING_MASK) != UART_CLK_RUNNING_MASK)
188                 ;
189
190         /* Disable smart idle */
191         regVal = readl(&uart_base->uartsyscfg);
192         regVal |= UART_SMART_IDLE_EN;
193         writel(regVal, &uart_base->uartsyscfg);
194
195         gd = &gdata;
196
197         preloader_console_init();
198
199         /* Initalize the board header */
200         enable_i2c0_pin_mux();
201         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
202         if (read_eeprom() < 0)
203                 puts("Could not get board ID.\n");
204
205         enable_board_pin_mux(&header);
206         if (board_is_evm_sk()) {
207                 /*
208                  * EVM SK 1.2A and later use gpio0_7 to enable DDR3.
209                  * This is safe enough to do on older revs.
210                  */
211                 gpio_request(GPIO_DDR_VTT_EN, "ddr_vtt_en");
212                 gpio_direction_output(GPIO_DDR_VTT_EN, 1);
213         }
214
215         config_ddr(board_memory_type());
216 #endif
217 }
218
219 #if defined(CONFIG_OMAP_HSMMC) && !defined(CONFIG_SPL_BUILD)
220 int board_mmc_init(bd_t *bis)
221 {
222         int ret;
223         
224         ret = omap_mmc_init(0, 0, 0);
225         if (ret)
226                 return ret;
227
228         return omap_mmc_init(1, 0, 0);
229 }
230 #endif
231
232 void setup_clocks_for_console(void)
233 {
234         /* Not yet implemented */
235         return;
236 }
237
238 /*
239  * Basic board specific setup.  Pinmux has been handled already.
240  */
241 int board_init(void)
242 {
243         i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
244         if (read_eeprom() < 0)
245                 puts("Could not get board ID.\n");
246
247         gd->bd->bi_boot_params = PHYS_DRAM_1 + 0x100;
248
249         return 0;
250 }
251
252 #ifdef CONFIG_DRIVER_TI_CPSW
253 static void cpsw_control(int enabled)
254 {
255         /* VTP can be added here */
256
257         return;
258 }
259
260 static struct cpsw_slave_data cpsw_slaves[] = {
261         {
262                 .slave_reg_ofs  = 0x208,
263                 .sliver_reg_ofs = 0xd80,
264                 .phy_id         = 0,
265         },
266         {
267                 .slave_reg_ofs  = 0x308,
268                 .sliver_reg_ofs = 0xdc0,
269                 .phy_id         = 1,
270         },
271 };
272
273 static struct cpsw_platform_data cpsw_data = {
274         .mdio_base              = AM335X_CPSW_MDIO_BASE,
275         .cpsw_base              = AM335X_CPSW_BASE,
276         .mdio_div               = 0xff,
277         .channels               = 8,
278         .cpdma_reg_ofs          = 0x800,
279         .slaves                 = 1,
280         .slave_data             = cpsw_slaves,
281         .ale_reg_ofs            = 0xd00,
282         .ale_entries            = 1024,
283         .host_port_reg_ofs      = 0x108,
284         .hw_stats_reg_ofs       = 0x900,
285         .mac_control            = (1 << 5),
286         .control                = cpsw_control,
287         .host_port_num          = 0,
288         .version                = CPSW_CTRL_VERSION_2,
289 };
290
291 int board_eth_init(bd_t *bis)
292 {
293         uint8_t mac_addr[6];
294         uint32_t mac_hi, mac_lo;
295
296         if (!eth_getenv_enetaddr("ethaddr", mac_addr)) {
297                 debug("<ethaddr> not set. Reading from E-fuse\n");
298                 /* try reading mac address from efuse */
299                 mac_lo = readl(&cdev->macid0l);
300                 mac_hi = readl(&cdev->macid0h);
301                 mac_addr[0] = mac_hi & 0xFF;
302                 mac_addr[1] = (mac_hi & 0xFF00) >> 8;
303                 mac_addr[2] = (mac_hi & 0xFF0000) >> 16;
304                 mac_addr[3] = (mac_hi & 0xFF000000) >> 24;
305                 mac_addr[4] = mac_lo & 0xFF;
306                 mac_addr[5] = (mac_lo & 0xFF00) >> 8;
307
308                 if (is_valid_ether_addr(mac_addr))
309                         eth_setenv_enetaddr("ethaddr", mac_addr);
310                 else
311                         return -1;
312         }
313
314         if (board_is_bone() || board_is_bone_lt()) {
315                 writel(MII_MODE_ENABLE, &cdev->miisel);
316                 cpsw_slaves[0].phy_if = cpsw_slaves[1].phy_if =
317                                 PHY_INTERFACE_MODE_MII;
318         } else {
319                 writel(RGMII_MODE_ENABLE, &cdev->miisel);
320                 cpsw_slaves[0].phy_if = cpsw_slaves[1].phy_if =
321                                 PHY_INTERFACE_MODE_RGMII;
322         }
323
324         return cpsw_register(&cpsw_data);
325 }
326 #endif