]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/samsung/universal_c210/universal.c
exynos4: universal_C210: use software SPI
[karo-tx-uboot.git] / board / samsung / universal_c210 / universal.c
1 /*
2  *  Copyright (C) 2010 Samsung Electronics
3  *  Minkyu Kang <mk7.kang@samsung.com>
4  *  Kyungmin Park <kyungmin.park@samsung.com>
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24
25 #include <common.h>
26 #include <spi.h>
27 #include <asm/io.h>
28 #include <asm/gpio.h>
29 #include <asm/arch/adc.h>
30 #include <asm/arch/gpio.h>
31 #include <asm/arch/mmc.h>
32 #include <asm/arch/pinmux.h>
33 #include <pmic.h>
34 #include <usb/s3c_udc.h>
35 #include <asm/arch/cpu.h>
36 #include <max8998_pmic.h>
37 #include <asm/arch/watchdog.h>
38
39 DECLARE_GLOBAL_DATA_PTR;
40
41 struct exynos4_gpio_part1 *gpio1;
42 struct exynos4_gpio_part2 *gpio2;
43 unsigned int board_rev;
44
45 u32 get_board_rev(void)
46 {
47         return board_rev;
48 }
49
50 static int get_hwrev(void)
51 {
52         return board_rev & 0xFF;
53 }
54
55 static void check_hw_revision(void);
56
57 int dram_init(void)
58 {
59         gd->ram_size = get_ram_size((long *)PHYS_SDRAM_1, PHYS_SDRAM_1_SIZE) +
60                 get_ram_size((long *)PHYS_SDRAM_2, PHYS_SDRAM_2_SIZE);
61
62         return 0;
63 }
64
65 void dram_init_banksize(void)
66 {
67         gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
68         gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
69         gd->bd->bi_dram[1].start = PHYS_SDRAM_2;
70         gd->bd->bi_dram[1].size = PHYS_SDRAM_2_SIZE;
71 }
72
73 static unsigned short get_adc_value(int channel)
74 {
75         struct s5p_adc *adc = (struct s5p_adc *)samsung_get_base_adc();
76         unsigned short ret = 0;
77         unsigned int reg;
78         unsigned int loop = 0;
79
80         writel(channel & 0xF, &adc->adcmux);
81         writel((1 << 14) | (49 << 6), &adc->adccon);
82         writel(1000 & 0xffff, &adc->adcdly);
83         writel(readl(&adc->adccon) | (1 << 16), &adc->adccon); /* 12 bit */
84         udelay(10);
85         writel(readl(&adc->adccon) | (1 << 0), &adc->adccon); /* Enable */
86         udelay(10);
87
88         do {
89                 udelay(1);
90                 reg = readl(&adc->adccon);
91         } while (!(reg & (1 << 15)) && (loop++ < 1000));
92
93         ret = readl(&adc->adcdat0) & 0xFFF;
94
95         return ret;
96 }
97
98 static int adc_power_control(int on)
99 {
100         int ret;
101         struct pmic *p = get_pmic();
102
103         if (pmic_probe(p))
104                 return -1;
105
106         ret = pmic_set_output(p,
107                               MAX8998_REG_ONOFF1,
108                               MAX8998_LDO4, !!on);
109
110         return ret;
111 }
112
113 static unsigned int get_hw_revision(void)
114 {
115         int hwrev, mode0, mode1;
116
117         adc_power_control(1);
118
119         mode0 = get_adc_value(1);               /* HWREV_MODE0 */
120         mode1 = get_adc_value(2);               /* HWREV_MODE1 */
121
122         /*
123          * XXX Always set the default hwrev as the latest board
124          * ADC = (voltage) / 3.3 * 4096
125          */
126         hwrev = 3;
127
128 #define IS_RANGE(x, min, max)   ((x) > (min) && (x) < (max))
129         if (IS_RANGE(mode0, 80, 200) && IS_RANGE(mode1, 80, 200))
130                 hwrev = 0x0;            /* 0.01V        0.01V */
131         if (IS_RANGE(mode0, 750, 1000) && IS_RANGE(mode1, 80, 200))
132                 hwrev = 0x1;            /* 610mV        0.01V */
133         if (IS_RANGE(mode0, 1300, 1700) && IS_RANGE(mode1, 80, 200))
134                 hwrev = 0x2;            /* 1.16V        0.01V */
135         if (IS_RANGE(mode0, 2000, 2400) && IS_RANGE(mode1, 80, 200))
136                 hwrev = 0x3;            /* 1.79V        0.01V */
137 #undef IS_RANGE
138
139         debug("mode0: %d, mode1: %d, hwrev 0x%x\n", mode0, mode1, hwrev);
140
141         adc_power_control(0);
142
143         return hwrev;
144 }
145
146 static void check_hw_revision(void)
147 {
148         int hwrev;
149
150         hwrev = get_hw_revision();
151
152         board_rev |= hwrev;
153 }
154
155 #ifdef CONFIG_DISPLAY_BOARDINFO
156 int checkboard(void)
157 {
158         puts("Board:\tUniversal C210\n");
159         return 0;
160 }
161 #endif
162
163 #ifdef CONFIG_GENERIC_MMC
164 int board_mmc_init(bd_t *bis)
165 {
166         int err;
167
168         switch (get_hwrev()) {
169         case 0:
170                 /*
171                  * Set the low to enable LDO_EN
172                  * But when you use the test board for eMMC booting
173                  * you should set it HIGH since it removes the inverter
174                  */
175                 /* MASSMEMORY_EN: XMDMDATA_6: GPE3[6] */
176                 s5p_gpio_direction_output(&gpio1->e3, 6, 0);
177                 break;
178         default:
179                 /*
180                  * Default reset state is High and there's no inverter
181                  * But set it as HIGH to ensure
182                  */
183                 /* MASSMEMORY_EN: XMDMADDR_3: GPE1[3] */
184                 s5p_gpio_direction_output(&gpio1->e1, 3, 1);
185                 break;
186         }
187
188         /*
189          * MMC device init
190          * mmc0  : eMMC (8-bit buswidth)
191          * mmc2  : SD card (4-bit buswidth)
192          */
193         err = exynos_pinmux_config(PERIPH_ID_SDMMC0, PINMUX_FLAG_8BIT_MODE);
194         if (err)
195                 debug("SDMMC0 not configured\n");
196         else
197                 err = s5p_mmc_init(0, 8);
198
199         /* T-flash detect */
200         s5p_gpio_cfg_pin(&gpio2->x3, 4, 0xf);
201         s5p_gpio_set_pull(&gpio2->x3, 4, GPIO_PULL_UP);
202
203         /*
204          * Check the T-flash  detect pin
205          * GPX3[4] T-flash detect pin
206          */
207         if (!s5p_gpio_get_value(&gpio2->x3, 4)) {
208                 err = exynos_pinmux_config(PERIPH_ID_SDMMC2, PINMUX_FLAG_NONE);
209                 if (err)
210                         debug("SDMMC2 not configured\n");
211                 else
212                         err = s5p_mmc_init(2, 4);
213         }
214
215         return err;
216
217 }
218 #endif
219
220 #ifdef CONFIG_USB_GADGET
221 static int s5pc210_phy_control(int on)
222 {
223         int ret = 0;
224         struct pmic *p = get_pmic();
225
226         if (pmic_probe(p))
227                 return -1;
228
229         if (on) {
230                 ret |= pmic_set_output(p,
231                                        MAX8998_REG_BUCK_ACTIVE_DISCHARGE3,
232                                        MAX8998_SAFEOUT1, LDO_ON);
233                 ret |= pmic_set_output(p, MAX8998_REG_ONOFF1,
234                                       MAX8998_LDO3, LDO_ON);
235                 ret |= pmic_set_output(p, MAX8998_REG_ONOFF2,
236                                       MAX8998_LDO8, LDO_ON);
237
238         } else {
239                 ret |= pmic_set_output(p, MAX8998_REG_ONOFF2,
240                                       MAX8998_LDO8, LDO_OFF);
241                 ret |= pmic_set_output(p, MAX8998_REG_ONOFF1,
242                                       MAX8998_LDO3, LDO_OFF);
243                 ret |= pmic_set_output(p,
244                                        MAX8998_REG_BUCK_ACTIVE_DISCHARGE3,
245                                        MAX8998_SAFEOUT1, LDO_OFF);
246         }
247
248         if (ret) {
249                 puts("MAX8998 LDO setting error!\n");
250                 return -1;
251         }
252
253         return 0;
254 }
255
256 struct s3c_plat_otg_data s5pc210_otg_data = {
257         .phy_control = s5pc210_phy_control,
258         .regs_phy = EXYNOS4_USBPHY_BASE,
259         .regs_otg = EXYNOS4_USBOTG_BASE,
260         .usb_phy_ctrl = EXYNOS4_USBPHY_CONTROL,
261         .usb_flags = PHY0_SLEEP,
262 };
263 #endif
264
265 int board_early_init_f(void)
266 {
267         wdt_stop();
268
269         return 0;
270 }
271
272 #ifdef CONFIG_SOFT_SPI
273 static void soft_spi_init(void)
274 {
275         gpio_direction_output(CONFIG_SOFT_SPI_GPIO_SCLK,
276                 CONFIG_SOFT_SPI_MODE & SPI_CPOL);
277         gpio_direction_output(CONFIG_SOFT_SPI_GPIO_MOSI, 1);
278         gpio_direction_input(CONFIG_SOFT_SPI_GPIO_MISO);
279         gpio_direction_output(CONFIG_SOFT_SPI_GPIO_CS,
280                 !(CONFIG_SOFT_SPI_MODE & SPI_CS_HIGH));
281 }
282
283 void spi_cs_activate(struct spi_slave *slave)
284 {
285         gpio_set_value(CONFIG_SOFT_SPI_GPIO_CS,
286                 !(CONFIG_SOFT_SPI_MODE & SPI_CS_HIGH));
287         SPI_SCL(1);
288         gpio_set_value(CONFIG_SOFT_SPI_GPIO_CS,
289                 CONFIG_SOFT_SPI_MODE & SPI_CS_HIGH);
290 }
291
292 void spi_cs_deactivate(struct spi_slave *slave)
293 {
294         gpio_set_value(CONFIG_SOFT_SPI_GPIO_CS,
295                 !(CONFIG_SOFT_SPI_MODE & SPI_CS_HIGH));
296 }
297
298 int  spi_cs_is_valid(unsigned int bus, unsigned int cs)
299 {
300         return bus == 0 && cs == 0;
301 }
302
303 void universal_spi_scl(int bit)
304 {
305         gpio_set_value(CONFIG_SOFT_SPI_GPIO_SCLK, bit);
306 }
307
308 void universal_spi_sda(int bit)
309 {
310         gpio_set_value(CONFIG_SOFT_SPI_GPIO_MOSI, bit);
311 }
312
313 int universal_spi_read(void)
314 {
315         return gpio_get_value(CONFIG_SOFT_SPI_GPIO_MISO);
316 }
317 #endif
318
319 int board_init(void)
320 {
321         gpio1 = (struct exynos4_gpio_part1 *) EXYNOS4_GPIO_PART1_BASE;
322         gpio2 = (struct exynos4_gpio_part2 *) EXYNOS4_GPIO_PART2_BASE;
323
324         gd->bd->bi_arch_number = MACH_TYPE_UNIVERSAL_C210;
325         gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x100;
326
327 #if defined(CONFIG_PMIC)
328         pmic_init();
329 #endif
330 #ifdef CONFIG_SOFT_SPI
331         soft_spi_init();
332 #endif
333         check_hw_revision();
334         printf("HW Revision:\t0x%x\n", board_rev);
335
336         return 0;
337 }