blob: 134a19874e49769d2c341409e94683968ff39f36 [file] [log] [blame]
Ian Campbell6efe3692014-05-05 11:52:26 +01001/*
2 * (C) Copyright 2012 Henrik Nordstrom <henrik@henriknordstrom.net>
3 *
4 * (C) Copyright 2007-2011
5 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
6 * Tom Cubie <tangliang@allwinnertech.com>
7 *
8 * Some init for sunxi platform.
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 */
12
13#include <common.h>
Hans de Goede3352b222014-06-13 22:55:49 +020014#include <i2c.h>
Ian Campbellba8311f2014-05-05 11:52:28 +010015#include <netdev.h>
16#include <miiphy.h>
Ian Campbell6efe3692014-05-05 11:52:26 +010017#include <serial.h>
18#ifdef CONFIG_SPL_BUILD
19#include <spl.h>
20#endif
21#include <asm/gpio.h>
22#include <asm/io.h>
23#include <asm/arch/clock.h>
24#include <asm/arch/gpio.h>
25#include <asm/arch/sys_proto.h>
26#include <asm/arch/timer.h>
27
Ian Campbelld41e2f672014-07-06 20:03:20 +010028#include <linux/compiler.h>
29
Ian Campbell6efe3692014-05-05 11:52:26 +010030#ifdef CONFIG_SPL_BUILD
31/* Pointer to the global data structure for SPL */
32DECLARE_GLOBAL_DATA_PTR;
33
34/* The sunxi internal brom will try to loader external bootloader
35 * from mmc0, nand flash, mmc2.
36 * Unfortunately we can't check how SPL was loaded so assume
37 * it's always the first SD/MMC controller
38 */
39u32 spl_boot_device(void)
40{
41 return BOOT_DEVICE_MMC1;
42}
43
44/* No confirmation data available in SPL yet. Hardcode bootmode */
45u32 spl_boot_mode(void)
46{
47 return MMCSD_MODE_RAW;
48}
49#endif
50
51int gpio_init(void)
52{
Chen-Yu Tsaid4ea92b2014-10-22 16:47:42 +080053#if CONFIG_CONS_INDEX == 1 && defined(CONFIG_UART0_PORT_F)
54#if defined(CONFIG_SUN4I) || defined(CONFIG_SUN7I)
55 /* disable GPB22,23 as uart0 tx,rx to avoid conflict */
56 sunxi_gpio_set_cfgpin(SUNXI_GPB(22), SUNXI_GPIO_INPUT);
57 sunxi_gpio_set_cfgpin(SUNXI_GPB(23), SUNXI_GPIO_INPUT);
58#endif
59 sunxi_gpio_set_cfgpin(SUNXI_GPF(2), SUNXI_GPF2_UART0_TX);
60 sunxi_gpio_set_cfgpin(SUNXI_GPF(4), SUNXI_GPF4_UART0_RX);
61 sunxi_gpio_set_pull(SUNXI_GPF(4), 1);
62#elif CONFIG_CONS_INDEX == 1 && (defined(CONFIG_SUN4I) || defined(CONFIG_SUN7I))
Ian Campbell6efe3692014-05-05 11:52:26 +010063 sunxi_gpio_set_cfgpin(SUNXI_GPB(22), SUN4I_GPB22_UART0_TX);
64 sunxi_gpio_set_cfgpin(SUNXI_GPB(23), SUN4I_GPB23_UART0_RX);
Chen-Yu Tsai4e526e22014-10-03 20:16:21 +080065 sunxi_gpio_set_pull(SUNXI_GPB(23), SUNXI_GPIO_PULL_UP);
Hans de Goede8c1c7822014-06-09 11:36:58 +020066#elif CONFIG_CONS_INDEX == 1 && defined(CONFIG_SUN5I)
67 sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN5I_GPB19_UART0_TX);
68 sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN5I_GPB20_UART0_RX);
Chen-Yu Tsai4e526e22014-10-03 20:16:21 +080069 sunxi_gpio_set_pull(SUNXI_GPB(20), SUNXI_GPIO_PULL_UP);
Maxime Ripardf139f1e2014-10-03 20:16:28 +080070#elif CONFIG_CONS_INDEX == 1 && defined(CONFIG_SUN6I)
71 sunxi_gpio_set_cfgpin(SUNXI_GPH(20), SUN6I_GPH20_UART0_TX);
72 sunxi_gpio_set_cfgpin(SUNXI_GPH(21), SUN6I_GPH21_UART0_RX);
73 sunxi_gpio_set_pull(SUNXI_GPH(21), SUNXI_GPIO_PULL_UP);
Hans de Goede8c1c7822014-06-09 11:36:58 +020074#elif CONFIG_CONS_INDEX == 2 && defined(CONFIG_SUN5I)
75 sunxi_gpio_set_cfgpin(SUNXI_GPG(3), SUN5I_GPG3_UART1_TX);
76 sunxi_gpio_set_cfgpin(SUNXI_GPG(4), SUN5I_GPG4_UART1_RX);
Chen-Yu Tsai4e526e22014-10-03 20:16:21 +080077 sunxi_gpio_set_pull(SUNXI_GPG(4), SUNXI_GPIO_PULL_UP);
Hans de Goede8c1c7822014-06-09 11:36:58 +020078#else
79#error Unsupported console port number. Please fix pin mux settings in board.c
80#endif
Ian Campbell6efe3692014-05-05 11:52:26 +010081
82 return 0;
83}
84
85void reset_cpu(ulong addr)
86{
Chen-Yu Tsai1275c482014-10-04 20:37:28 +080087#if defined(CONFIG_SUN4I) || defined(CONFIG_SUN5I) || defined(CONFIG_SUN7I)
Hans de Goede1374e892014-06-09 11:36:56 +020088 static const struct sunxi_wdog *wdog =
89 &((struct sunxi_timer_reg *)SUNXI_TIMER_BASE)->wdog;
90
91 /* Set the watchdog for its shortest interval (.5s) and wait */
92 writel(WDT_MODE_RESET_EN | WDT_MODE_EN, &wdog->mode);
93 writel(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdog->ctl);
Hans de Goedefa43a6e2014-06-13 22:55:52 +020094
95 while (1) {
96 /* sun5i sometimes gets stuck without this */
97 writel(WDT_MODE_RESET_EN | WDT_MODE_EN, &wdog->mode);
98 }
Chen-Yu Tsai1275c482014-10-04 20:37:28 +080099#else /* CONFIG_SUN6I || CONFIG_SUN8I || .. */
100 static const struct sunxi_wdog *wdog =
101 ((struct sunxi_timer_reg *)SUNXI_TIMER_BASE)->wdog;
102
103 /* Set the watchdog for its shortest interval (.5s) and wait */
104 writel(WDT_CFG_RESET, &wdog->cfg);
105 writel(WDT_MODE_EN, &wdog->mode);
106 writel(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdog->ctl);
107#endif
Ian Campbell6efe3692014-05-05 11:52:26 +0100108}
109
110/* do some early init */
111void s_init(void)
112{
Chen-Yu Tsai848c2632014-10-22 16:47:44 +0800113#if !defined CONFIG_SPL_BUILD && (defined CONFIG_SUN7I || \
114 defined CONFIG_SUN6I || defined CONFIG_SUN8I)
Ian Campbell6efe3692014-05-05 11:52:26 +0100115 /* Enable SMP mode for CPU0, by setting bit 6 of Auxiliary Ctl reg */
116 asm volatile(
117 "mrc p15, 0, r0, c1, c0, 1\n"
118 "orr r0, r0, #1 << 6\n"
119 "mcr p15, 0, r0, c1, c0, 1\n");
120#endif
121
122 clock_init();
123 timer_init();
124 gpio_init();
Hans de Goede3352b222014-06-13 22:55:49 +0200125 i2c_init_board();
Ian Campbell6efe3692014-05-05 11:52:26 +0100126
127#ifdef CONFIG_SPL_BUILD
128 gd = &gdata;
129 preloader_console_init();
130
Hans de Goede3352b222014-06-13 22:55:49 +0200131#ifdef CONFIG_SPL_I2C_SUPPORT
132 /* Needed early by sunxi_board_init if PMU is enabled */
133 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
134#endif
Ian Campbell6efe3692014-05-05 11:52:26 +0100135 sunxi_board_init();
136#endif
137}
138
139#ifndef CONFIG_SYS_DCACHE_OFF
140void enable_caches(void)
141{
142 /* Enable D-cache. I-cache is already enabled in start.S */
143 dcache_enable();
144}
145#endif
Ian Campbellba8311f2014-05-05 11:52:28 +0100146
147#ifdef CONFIG_CMD_NET
148/*
149 * Initializes on-chip ethernet controllers.
150 * to override, implement board_eth_init()
151 */
152int cpu_eth_init(bd_t *bis)
153{
Ian Campbelld41e2f672014-07-06 20:03:20 +0100154 __maybe_unused int rc;
Ian Campbellba8311f2014-05-05 11:52:28 +0100155
Hans de Goede9b218722014-07-26 17:09:13 +0200156#ifdef CONFIG_MACPWR
157 gpio_direction_output(CONFIG_MACPWR, 1);
158 mdelay(200);
159#endif
160
Hans de Goede73d7d422014-06-09 11:37:00 +0200161#ifdef CONFIG_SUNXI_EMAC
162 rc = sunxi_emac_initialize(bis);
163 if (rc < 0) {
164 printf("sunxi: failed to initialize emac\n");
165 return rc;
166 }
167#endif
168
Ian Campbellba8311f2014-05-05 11:52:28 +0100169#ifdef CONFIG_SUNXI_GMAC
170 rc = sunxi_gmac_initialize(bis);
171 if (rc < 0) {
172 printf("sunxi: failed to initialize gmac\n");
173 return rc;
174 }
175#endif
176
177 return 0;
178}
179#endif