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 | |
Simon Glass | 8735682 | 2014-12-23 12:04:52 -0700 | [diff] [blame] | 30 | static int gpio_init(void) |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 31 | { |
Chen-Yu Tsai | d4ea92b | 2014-10-22 16:47:42 +0800 | [diff] [blame] | 32 | #if CONFIG_CONS_INDEX == 1 && defined(CONFIG_UART0_PORT_F) |
Ian Campbell | 8f32aaa | 2014-10-24 21:20:47 +0100 | [diff] [blame] | 33 | #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I) |
Chen-Yu Tsai | d4ea92b | 2014-10-22 16:47:42 +0800 | [diff] [blame] | 34 | /* disable GPB22,23 as uart0 tx,rx to avoid conflict */ |
| 35 | sunxi_gpio_set_cfgpin(SUNXI_GPB(22), SUNXI_GPIO_INPUT); |
| 36 | sunxi_gpio_set_cfgpin(SUNXI_GPB(23), SUNXI_GPIO_INPUT); |
| 37 | #endif |
| 38 | sunxi_gpio_set_cfgpin(SUNXI_GPF(2), SUNXI_GPF2_UART0_TX); |
| 39 | sunxi_gpio_set_cfgpin(SUNXI_GPF(4), SUNXI_GPF4_UART0_RX); |
| 40 | sunxi_gpio_set_pull(SUNXI_GPF(4), 1); |
Ian Campbell | 8f32aaa | 2014-10-24 21:20:47 +0100 | [diff] [blame] | 41 | #elif CONFIG_CONS_INDEX == 1 && (defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)) |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 42 | sunxi_gpio_set_cfgpin(SUNXI_GPB(22), SUN4I_GPB22_UART0_TX); |
| 43 | sunxi_gpio_set_cfgpin(SUNXI_GPB(23), SUN4I_GPB23_UART0_RX); |
Chen-Yu Tsai | 4e526e2 | 2014-10-03 20:16:21 +0800 | [diff] [blame] | 44 | sunxi_gpio_set_pull(SUNXI_GPB(23), SUNXI_GPIO_PULL_UP); |
Ian Campbell | 8f32aaa | 2014-10-24 21:20:47 +0100 | [diff] [blame] | 45 | #elif CONFIG_CONS_INDEX == 1 && defined(CONFIG_MACH_SUN5I) |
Hans de Goede | 8c1c782 | 2014-06-09 11:36:58 +0200 | [diff] [blame] | 46 | sunxi_gpio_set_cfgpin(SUNXI_GPB(19), SUN5I_GPB19_UART0_TX); |
| 47 | sunxi_gpio_set_cfgpin(SUNXI_GPB(20), SUN5I_GPB20_UART0_RX); |
Chen-Yu Tsai | 4e526e2 | 2014-10-03 20:16:21 +0800 | [diff] [blame] | 48 | sunxi_gpio_set_pull(SUNXI_GPB(20), SUNXI_GPIO_PULL_UP); |
Ian Campbell | 8f32aaa | 2014-10-24 21:20:47 +0100 | [diff] [blame] | 49 | #elif CONFIG_CONS_INDEX == 1 && defined(CONFIG_MACH_SUN6I) |
Maxime Ripard | f139f1e | 2014-10-03 20:16:28 +0800 | [diff] [blame] | 50 | sunxi_gpio_set_cfgpin(SUNXI_GPH(20), SUN6I_GPH20_UART0_TX); |
| 51 | sunxi_gpio_set_cfgpin(SUNXI_GPH(21), SUN6I_GPH21_UART0_RX); |
| 52 | sunxi_gpio_set_pull(SUNXI_GPH(21), SUNXI_GPIO_PULL_UP); |
Ian Campbell | 8f32aaa | 2014-10-24 21:20:47 +0100 | [diff] [blame] | 53 | #elif CONFIG_CONS_INDEX == 2 && defined(CONFIG_MACH_SUN5I) |
Hans de Goede | 8c1c782 | 2014-06-09 11:36:58 +0200 | [diff] [blame] | 54 | sunxi_gpio_set_cfgpin(SUNXI_GPG(3), SUN5I_GPG3_UART1_TX); |
| 55 | sunxi_gpio_set_cfgpin(SUNXI_GPG(4), SUN5I_GPG4_UART1_RX); |
Chen-Yu Tsai | 4e526e2 | 2014-10-03 20:16:21 +0800 | [diff] [blame] | 56 | sunxi_gpio_set_pull(SUNXI_GPG(4), SUNXI_GPIO_PULL_UP); |
Ian Campbell | 8f32aaa | 2014-10-24 21:20:47 +0100 | [diff] [blame] | 57 | #elif CONFIG_CONS_INDEX == 5 && defined(CONFIG_MACH_SUN8I) |
Chen-Yu Tsai | 6ee6388 | 2014-10-22 16:47:47 +0800 | [diff] [blame] | 58 | sunxi_gpio_set_cfgpin(SUNXI_GPL(2), SUN8I_GPL2_R_UART_TX); |
| 59 | sunxi_gpio_set_cfgpin(SUNXI_GPL(3), SUN8I_GPL3_R_UART_RX); |
| 60 | sunxi_gpio_set_pull(SUNXI_GPL(3), SUNXI_GPIO_PULL_UP); |
Hans de Goede | 8c1c782 | 2014-06-09 11:36:58 +0200 | [diff] [blame] | 61 | #else |
| 62 | #error Unsupported console port number. Please fix pin mux settings in board.c |
| 63 | #endif |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
Simon Glass | 8735682 | 2014-12-23 12:04:52 -0700 | [diff] [blame] | 67 | |
Hans de Goede | b42b04d | 2015-01-21 16:24:05 +0100 | [diff] [blame] | 68 | void s_init(void) |
Simon Glass | 8735682 | 2014-12-23 12:04:52 -0700 | [diff] [blame] | 69 | { |
| 70 | #if defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I |
| 71 | /* Magic (undocmented) value taken from boot0, without this DRAM |
| 72 | * access gets messed up (seems cache related) */ |
| 73 | setbits_le32(SUNXI_SRAMC_BASE + 0x44, 0x1800); |
| 74 | #endif |
| 75 | #if !defined CONFIG_SPL_BUILD && (defined CONFIG_MACH_SUN7I || \ |
| 76 | defined CONFIG_MACH_SUN6I || defined CONFIG_MACH_SUN8I) |
| 77 | /* Enable SMP mode for CPU0, by setting bit 6 of Auxiliary Ctl reg */ |
| 78 | asm volatile( |
| 79 | "mrc p15, 0, r0, c1, c0, 1\n" |
| 80 | "orr r0, r0, #1 << 6\n" |
| 81 | "mcr p15, 0, r0, c1, c0, 1\n"); |
| 82 | #endif |
| 83 | |
| 84 | clock_init(); |
| 85 | timer_init(); |
| 86 | gpio_init(); |
| 87 | i2c_init_board(); |
Hans de Goede | b42b04d | 2015-01-21 16:24:05 +0100 | [diff] [blame] | 88 | } |
Simon Glass | 8735682 | 2014-12-23 12:04:52 -0700 | [diff] [blame] | 89 | |
Hans de Goede | b42b04d | 2015-01-21 16:24:05 +0100 | [diff] [blame] | 90 | #ifdef CONFIG_SPL_BUILD |
| 91 | /* The sunxi internal brom will try to loader external bootloader |
| 92 | * from mmc0, nand flash, mmc2. |
| 93 | * Unfortunately we can't check how SPL was loaded so assume |
| 94 | * it's always the first SD/MMC controller |
| 95 | */ |
| 96 | u32 spl_boot_device(void) |
| 97 | { |
| 98 | return BOOT_DEVICE_MMC1; |
| 99 | } |
| 100 | |
| 101 | /* No confirmation data available in SPL yet. Hardcode bootmode */ |
| 102 | u32 spl_boot_mode(void) |
| 103 | { |
| 104 | return MMCSD_MODE_RAW; |
| 105 | } |
| 106 | |
| 107 | void board_init_f(ulong dummy) |
| 108 | { |
Simon Glass | 8735682 | 2014-12-23 12:04:52 -0700 | [diff] [blame] | 109 | preloader_console_init(); |
| 110 | |
| 111 | #ifdef CONFIG_SPL_I2C_SUPPORT |
| 112 | /* Needed early by sunxi_board_init if PMU is enabled */ |
| 113 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
| 114 | #endif |
| 115 | sunxi_board_init(); |
| 116 | |
| 117 | /* Clear the BSS. */ |
| 118 | memset(__bss_start, 0, __bss_end - __bss_start); |
| 119 | |
| 120 | board_init_r(NULL, 0); |
| 121 | } |
| 122 | #endif |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 123 | |
| 124 | void reset_cpu(ulong addr) |
| 125 | { |
Ian Campbell | 8f32aaa | 2014-10-24 21:20:47 +0100 | [diff] [blame] | 126 | #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I) |
Hans de Goede | 1374e89 | 2014-06-09 11:36:56 +0200 | [diff] [blame] | 127 | static const struct sunxi_wdog *wdog = |
| 128 | &((struct sunxi_timer_reg *)SUNXI_TIMER_BASE)->wdog; |
| 129 | |
| 130 | /* Set the watchdog for its shortest interval (.5s) and wait */ |
| 131 | writel(WDT_MODE_RESET_EN | WDT_MODE_EN, &wdog->mode); |
| 132 | writel(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdog->ctl); |
Hans de Goede | fa43a6e | 2014-06-13 22:55:52 +0200 | [diff] [blame] | 133 | |
| 134 | while (1) { |
| 135 | /* sun5i sometimes gets stuck without this */ |
| 136 | writel(WDT_MODE_RESET_EN | WDT_MODE_EN, &wdog->mode); |
| 137 | } |
Ian Campbell | 8f32aaa | 2014-10-24 21:20:47 +0100 | [diff] [blame] | 138 | #else /* CONFIG_MACH_SUN6I || CONFIG_MACH_SUN8I || .. */ |
Chen-Yu Tsai | 1275c48 | 2014-10-04 20:37:28 +0800 | [diff] [blame] | 139 | static const struct sunxi_wdog *wdog = |
| 140 | ((struct sunxi_timer_reg *)SUNXI_TIMER_BASE)->wdog; |
| 141 | |
| 142 | /* Set the watchdog for its shortest interval (.5s) and wait */ |
| 143 | writel(WDT_CFG_RESET, &wdog->cfg); |
| 144 | writel(WDT_MODE_EN, &wdog->mode); |
| 145 | writel(WDT_CTRL_KEY | WDT_CTRL_RESTART, &wdog->ctl); |
| 146 | #endif |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Ian Campbell | 6efe369 | 2014-05-05 11:52:26 +0100 | [diff] [blame] | 149 | #ifndef CONFIG_SYS_DCACHE_OFF |
| 150 | void enable_caches(void) |
| 151 | { |
| 152 | /* Enable D-cache. I-cache is already enabled in start.S */ |
| 153 | dcache_enable(); |
| 154 | } |
| 155 | #endif |
Ian Campbell | ba8311f | 2014-05-05 11:52:28 +0100 | [diff] [blame] | 156 | |
| 157 | #ifdef CONFIG_CMD_NET |
| 158 | /* |
| 159 | * Initializes on-chip ethernet controllers. |
| 160 | * to override, implement board_eth_init() |
| 161 | */ |
| 162 | int cpu_eth_init(bd_t *bis) |
| 163 | { |
Ian Campbell | d41e2f67 | 2014-07-06 20:03:20 +0100 | [diff] [blame] | 164 | __maybe_unused int rc; |
Ian Campbell | ba8311f | 2014-05-05 11:52:28 +0100 | [diff] [blame] | 165 | |
Hans de Goede | 9b21872 | 2014-07-26 17:09:13 +0200 | [diff] [blame] | 166 | #ifdef CONFIG_MACPWR |
| 167 | gpio_direction_output(CONFIG_MACPWR, 1); |
| 168 | mdelay(200); |
| 169 | #endif |
| 170 | |
Hans de Goede | 73d7d42 | 2014-06-09 11:37:00 +0200 | [diff] [blame] | 171 | #ifdef CONFIG_SUNXI_EMAC |
| 172 | rc = sunxi_emac_initialize(bis); |
| 173 | if (rc < 0) { |
| 174 | printf("sunxi: failed to initialize emac\n"); |
| 175 | return rc; |
| 176 | } |
| 177 | #endif |
| 178 | |
Ian Campbell | ba8311f | 2014-05-05 11:52:28 +0100 | [diff] [blame] | 179 | #ifdef CONFIG_SUNXI_GMAC |
| 180 | rc = sunxi_gmac_initialize(bis); |
| 181 | if (rc < 0) { |
| 182 | printf("sunxi: failed to initialize gmac\n"); |
| 183 | return rc; |
| 184 | } |
| 185 | #endif |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | #endif |