Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 1 | /* |
| 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 Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 14 | #include <i2c.h> |
Ian Campbell | ba8311f | 2014-05-05 11:52:28 +0100 | [diff] [blame] | 15 | #include <netdev.h> |
| 16 | #include <miiphy.h> |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 17 | #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 Campbell | d41e2f67 | 2014-07-06 20:03:20 +0100 | [diff] [blame] | 28 | #include <linux/compiler.h> |
| 29 | |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 30 | #ifdef CONFIG_SPL_BUILD |
| 31 | /* Pointer to the global data structure for SPL */ |
| 32 | DECLARE_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 | */ |
| 39 | u32 spl_boot_device(void) |
| 40 | { |
| 41 | return BOOT_DEVICE_MMC1; |
| 42 | } |
| 43 | |
| 44 | /* No confirmation data available in SPL yet. Hardcode bootmode */ |
| 45 | u32 spl_boot_mode(void) |
| 46 | { |
| 47 | return MMCSD_MODE_RAW; |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | int gpio_init(void) |
| 52 | { |
Chen-Yu Tsai | d4ea92b | 2014-10-22 16:47:42 +0800 | [diff] [blame] | 53 | #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 Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 63 | 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 Tsai | 4e526e2 | 2014-10-03 20:16:21 +0800 | [diff] [blame] | 65 | sunxi_gpio_set_pull(SUNXI_GPB(23), SUNXI_GPIO_PULL_UP); |
Hans de Goede | 8c1c782 | 2014-06-09 11:36:58 +0200 | [diff] [blame] | 66 | #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 Tsai | 4e526e2 | 2014-10-03 20:16:21 +0800 | [diff] [blame] | 69 | sunxi_gpio_set_pull(SUNXI_GPB(20), SUNXI_GPIO_PULL_UP); |
Maxime Ripard | f139f1e | 2014-10-03 20:16:28 +0800 | [diff] [blame] | 70 | #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 Goede | 8c1c782 | 2014-06-09 11:36:58 +0200 | [diff] [blame] | 74 | #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 Tsai | 4e526e2 | 2014-10-03 20:16:21 +0800 | [diff] [blame] | 77 | sunxi_gpio_set_pull(SUNXI_GPG(4), SUNXI_GPIO_PULL_UP); |
Chen-Yu Tsai | 6ee6388 | 2014-10-22 16:47:47 +0800 | [diff] [blame] | 78 | #elif CONFIG_CONS_INDEX == 5 && defined(CONFIG_SUN8I) |
| 79 | sunxi_gpio_set_cfgpin(SUNXI_GPL(2), SUN8I_GPL2_R_UART_TX); |
| 80 | sunxi_gpio_set_cfgpin(SUNXI_GPL(3), SUN8I_GPL3_R_UART_RX); |
| 81 | sunxi_gpio_set_pull(SUNXI_GPL(3), SUNXI_GPIO_PULL_UP); |
Hans de Goede | 8c1c782 | 2014-06-09 11:36:58 +0200 | [diff] [blame] | 82 | #else |
| 83 | #error Unsupported console port number. Please fix pin mux settings in board.c |
| 84 | #endif |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | void reset_cpu(ulong addr) |
| 90 | { |
Chen-Yu Tsai | 1275c48 | 2014-10-04 20:37:28 +0800 | [diff] [blame] | 91 | #if defined(CONFIG_SUN4I) || defined(CONFIG_SUN5I) || defined(CONFIG_SUN7I) |
Hans de Goede | 1374e89 | 2014-06-09 11:36:56 +0200 | [diff] [blame] | 92 | static const struct sunxi_wdog *wdog = |
| 93 | &((struct sunxi_timer_reg *)SUNXI_TIMER_BASE)->wdog; |
| 94 | |
| 95 | /* Set the watchdog for its shortest interval (.5s) and wait */ |
| 96 | writel(WDT_MODE_RESET_EN | WDT_MODE_EN, &wdog->mode); |
| 97 | writel(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdog->ctl); |
Hans de Goede | fa43a6e | 2014-06-13 22:55:52 +0200 | [diff] [blame] | 98 | |
| 99 | while (1) { |
| 100 | /* sun5i sometimes gets stuck without this */ |
| 101 | writel(WDT_MODE_RESET_EN | WDT_MODE_EN, &wdog->mode); |
| 102 | } |
Chen-Yu Tsai | 1275c48 | 2014-10-04 20:37:28 +0800 | [diff] [blame] | 103 | #else /* CONFIG_SUN6I || CONFIG_SUN8I || .. */ |
| 104 | static const struct sunxi_wdog *wdog = |
| 105 | ((struct sunxi_timer_reg *)SUNXI_TIMER_BASE)->wdog; |
| 106 | |
| 107 | /* Set the watchdog for its shortest interval (.5s) and wait */ |
| 108 | writel(WDT_CFG_RESET, &wdog->cfg); |
| 109 | writel(WDT_MODE_EN, &wdog->mode); |
| 110 | writel(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdog->ctl); |
| 111 | #endif |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* do some early init */ |
| 115 | void s_init(void) |
| 116 | { |
Chen-Yu Tsai | 848c263 | 2014-10-22 16:47:44 +0800 | [diff] [blame] | 117 | #if !defined CONFIG_SPL_BUILD && (defined CONFIG_SUN7I || \ |
| 118 | defined CONFIG_SUN6I || defined CONFIG_SUN8I) |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 119 | /* Enable SMP mode for CPU0, by setting bit 6 of Auxiliary Ctl reg */ |
| 120 | asm volatile( |
| 121 | "mrc p15, 0, r0, c1, c0, 1\n" |
| 122 | "orr r0, r0, #1 << 6\n" |
| 123 | "mcr p15, 0, r0, c1, c0, 1\n"); |
| 124 | #endif |
| 125 | |
| 126 | clock_init(); |
| 127 | timer_init(); |
| 128 | gpio_init(); |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 129 | i2c_init_board(); |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 130 | |
| 131 | #ifdef CONFIG_SPL_BUILD |
| 132 | gd = &gdata; |
| 133 | preloader_console_init(); |
| 134 | |
Hans de Goede | 3352b22 | 2014-06-13 22:55:49 +0200 | [diff] [blame] | 135 | #ifdef CONFIG_SPL_I2C_SUPPORT |
| 136 | /* Needed early by sunxi_board_init if PMU is enabled */ |
| 137 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 138 | #endif |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 139 | sunxi_board_init(); |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | #ifndef CONFIG_SYS_DCACHE_OFF |
| 144 | void enable_caches(void) |
| 145 | { |
| 146 | /* Enable D-cache. I-cache is already enabled in start.S */ |
| 147 | dcache_enable(); |
| 148 | } |
| 149 | #endif |
Ian Campbell | ba8311f | 2014-05-05 11:52:28 +0100 | [diff] [blame] | 150 | |
| 151 | #ifdef CONFIG_CMD_NET |
| 152 | /* |
| 153 | * Initializes on-chip ethernet controllers. |
| 154 | * to override, implement board_eth_init() |
| 155 | */ |
| 156 | int cpu_eth_init(bd_t *bis) |
| 157 | { |
Ian Campbell | d41e2f67 | 2014-07-06 20:03:20 +0100 | [diff] [blame] | 158 | __maybe_unused int rc; |
Ian Campbell | ba8311f | 2014-05-05 11:52:28 +0100 | [diff] [blame] | 159 | |
Hans de Goede | 9b21872 | 2014-07-26 17:09:13 +0200 | [diff] [blame] | 160 | #ifdef CONFIG_MACPWR |
| 161 | gpio_direction_output(CONFIG_MACPWR, 1); |
| 162 | mdelay(200); |
| 163 | #endif |
| 164 | |
Hans de Goede | 73d7d42 | 2014-06-09 11:37:00 +0200 | [diff] [blame] | 165 | #ifdef CONFIG_SUNXI_EMAC |
| 166 | rc = sunxi_emac_initialize(bis); |
| 167 | if (rc < 0) { |
| 168 | printf("sunxi: failed to initialize emac\n"); |
| 169 | return rc; |
| 170 | } |
| 171 | #endif |
| 172 | |
Ian Campbell | ba8311f | 2014-05-05 11:52:28 +0100 | [diff] [blame] | 173 | #ifdef CONFIG_SUNXI_GMAC |
| 174 | rc = sunxi_gmac_initialize(bis); |
| 175 | if (rc < 0) { |
| 176 | printf("sunxi: failed to initialize gmac\n"); |
| 177 | return rc; |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | #endif |