1 /*
2  * Copyright (C) 2011 OMICRON electronics GmbH
3  *
4  * Based on da850evm.c. Original Copyrights follow:
5  *
6  * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
7  * Copyright (C) 2009 Nick Thompson, GE Fanuc, Ltd. <nick.thompson@gefanuc.com>
8  * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 
25 #include <common.h>
26 #include <i2c.h>
27 #include <net.h>
28 #include <netdev.h>
29 #include <watchdog.h>
30 #include <asm/io.h>
31 #include <asm/arch/hardware.h>
32 #include <asm/arch/gpio.h>
33 #include <asm/arch/emif_defs.h>
34 #include <asm/arch/emac_defs.h>
35 #include <asm/arch/pinmux_defs.h>
36 #include <asm/arch/davinci_misc.h>
37 #include <asm/arch/timer_defs.h>
38 
39 DECLARE_GLOBAL_DATA_PTR;
40 
41 #define CALIMAIN_HWVERSION_MASK    0x7f000000
42 #define CALIMAIN_HWVERSION_SHIFT   24
43 
44 /* Hardware version pinmux settings */
45 const struct pinmux_config hwversion_pins[] = {
46 	{ pinmux(16), 8, 2 }, /* GP7[15] */
47 	{ pinmux(16), 8, 3 }, /* GP7[14] */
48 	{ pinmux(16), 8, 4 }, /* GP7[13] */
49 	{ pinmux(16), 8, 5 }, /* GP7[12] */
50 	{ pinmux(16), 8, 6 }, /* GP7[11] */
51 	{ pinmux(16), 8, 7 }, /* GP7[10] */
52 	{ pinmux(17), 8, 0 }, /* GP7[9] */
53 	{ pinmux(17), 8, 1 }  /* GP7[8] */
54 };
55 
56 const struct pinmux_resource pinmuxes[] = {
57 	PINMUX_ITEM(uart2_pins_txrx),
58 	PINMUX_ITEM(emac_pins_mii),
59 	PINMUX_ITEM(emac_pins_mdio),
60 	PINMUX_ITEM(emifa_pins_nor),
61 	PINMUX_ITEM(emifa_pins_cs2),
62 	PINMUX_ITEM(emifa_pins_cs3),
63 };
64 
65 const int pinmuxes_size = ARRAY_SIZE(pinmuxes);
66 
67 const struct lpsc_resource lpsc[] = {
68 	{ DAVINCI_LPSC_AEMIF },	/* NAND, NOR */
69 	{ DAVINCI_LPSC_EMAC },	/* image download */
70 	{ DAVINCI_LPSC_UART2 },	/* console */
71 	{ DAVINCI_LPSC_GPIO },
72 };
73 
74 const int lpsc_size = ARRAY_SIZE(lpsc);
75 
76 /* read board revision from GPIO7[8..14] */
77 u32 get_board_rev(void)
78 {
79 	lpsc_on(DAVINCI_LPSC_GPIO);
80 	if (davinci_configure_pin_mux(hwversion_pins,
81 				      ARRAY_SIZE(hwversion_pins)) != 0)
82 		return 0xffffffff;
83 
84 	return (davinci_gpio_bank67->in_data & CALIMAIN_HWVERSION_MASK)
85 		>> CALIMAIN_HWVERSION_SHIFT;
86 }
87 
88 /*
89  * determine the oscillator frequency depending on the board revision
90  *
91  * rev 0x00  ... 25 MHz oscillator
92  * rev 0x01  ... 24 MHz oscillator
93  */
94 int calimain_get_osc_freq(void)
95 {
96 	u32 rev;
97 	int freq;
98 
99 	rev = get_board_rev();
100 	switch (rev) {
101 	case 0x00:
102 		freq = 25000000;
103 		break;
104 	default:
105 		freq = 24000000;
106 		break;
107 	}
108 	return freq;
109 }
110 
111 int board_init(void)
112 {
113 	int val;
114 
115 #ifndef CONFIG_USE_IRQ
116 	irq_init();
117 #endif
118 
119 	/* address of boot parameters */
120 	gd->bd->bi_boot_params = LINUX_BOOT_PARAM_ADDR;
121 
122 #ifdef CONFIG_DRIVER_TI_EMAC
123 	/* select emac MII mode */
124 	val = readl(&davinci_syscfg_regs->cfgchip3);
125 	val &= ~(1 << 8);
126 	writel(val, &davinci_syscfg_regs->cfgchip3);
127 #endif /* CONFIG_DRIVER_TI_EMAC */
128 
129 #ifdef CONFIG_HW_WATCHDOG
130 	davinci_hw_watchdog_enable();
131 #endif
132 
133 	printf("Input clock frequency: %d Hz\n", calimain_get_osc_freq());
134 	printf("Board revision:        %d\n", get_board_rev());
135 
136 	return 0;
137 }
138 
139 #ifdef CONFIG_DRIVER_TI_EMAC
140 /*
141  * Initializes on-board ethernet controllers.
142  */
143 int board_eth_init(bd_t *bis)
144 {
145 	if (!davinci_emac_initialize()) {
146 		printf("Error: Ethernet init failed!\n");
147 		return -1;
148 	}
149 
150 	return 0;
151 }
152 #endif /* CONFIG_DRIVER_TI_EMAC */
153 
154 #ifdef CONFIG_HW_WATCHDOG
155 void hw_watchdog_reset(void)
156 {
157 	davinci_hw_watchdog_reset();
158 }
159 #endif
160 
161 #if defined(CONFIG_BOOTCOUNT_LIMIT)
162 void bootcount_store(ulong a)
163 {
164 	struct davinci_rtc *reg =
165 		(struct davinci_rtc *)CONFIG_SYS_BOOTCOUNT_ADDR;
166 
167 	/*
168 	 * write RTC kick register to enable write
169 	 * for RTC Scratch registers. Scratch0 and 1 are
170 	 * used for bootcount values.
171 	 */
172 	writel(RTC_KICK0R_WE, &reg->kick0r);
173 	writel(RTC_KICK1R_WE, &reg->kick1r);
174 	writel(a, &reg->scratch0);
175 	writel(BOOTCOUNT_MAGIC, &reg->scratch1);
176 }
177 
178 ulong bootcount_load(void)
179 {
180 	struct davinci_rtc *reg =
181 		(struct davinci_rtc *)CONFIG_SYS_BOOTCOUNT_ADDR;
182 
183 	if (readl(&reg->scratch1) != BOOTCOUNT_MAGIC)
184 		return 0;
185 	else
186 		return readl(&reg->scratch0);
187 }
188 #endif
189