Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Siarhei Siamashka <siarhei.siamashka@gmail.com> |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 7 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 9 | #include <spl.h> |
Andre Przywara | 05ebd89 | 2021-07-06 00:04:43 +0100 | [diff] [blame] | 10 | #include <asm/arch/spl.h> |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 11 | #include <asm/gpio.h> |
| 12 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 15 | #include <linux/libfdt.h> |
Andre Przywara | f944a61 | 2022-09-06 10:36:38 +0100 | [diff] [blame^] | 16 | #include <sunxi_gpio.h> |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 17 | |
| 18 | #ifdef CONFIG_SPL_OS_BOOT |
| 19 | #error CONFIG_SPL_OS_BOOT is not supported yet |
| 20 | #endif |
| 21 | |
| 22 | /* |
| 23 | * This is a very simple U-Boot image loading implementation, trying to |
| 24 | * replicate what the boot ROM is doing when loading the SPL. Because we |
| 25 | * know the exact pins where the SPI Flash is connected and also know |
| 26 | * that the Read Data Bytes (03h) command is supported, the hardware |
| 27 | * configuration is very simple and we don't need the extra flexibility |
| 28 | * of the SPI framework. Moreover, we rely on the default settings of |
| 29 | * the SPI controler hardware registers and only adjust what needs to |
| 30 | * be changed. This is good for the code size and this implementation |
| 31 | * adds less than 400 bytes to the SPL. |
| 32 | * |
| 33 | * There are two variants of the SPI controller in Allwinner SoCs: |
| 34 | * A10/A13/A20 (sun4i variant) and everything else (sun6i variant). |
| 35 | * Both of them are supported. |
| 36 | * |
| 37 | * The pin mixing part is SoC specific and only A10/A13/A20/H3/A64 are |
| 38 | * supported at the moment. |
| 39 | */ |
| 40 | |
| 41 | /*****************************************************************************/ |
| 42 | /* SUN4I variant of the SPI controller */ |
| 43 | /*****************************************************************************/ |
| 44 | |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 45 | #define SUN4I_SPI0_CCTL 0x1C |
| 46 | #define SUN4I_SPI0_CTL 0x08 |
| 47 | #define SUN4I_SPI0_RX 0x00 |
| 48 | #define SUN4I_SPI0_TX 0x04 |
| 49 | #define SUN4I_SPI0_FIFO_STA 0x28 |
| 50 | #define SUN4I_SPI0_BC 0x20 |
| 51 | #define SUN4I_SPI0_TC 0x24 |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 52 | |
| 53 | #define SUN4I_CTL_ENABLE BIT(0) |
| 54 | #define SUN4I_CTL_MASTER BIT(1) |
| 55 | #define SUN4I_CTL_TF_RST BIT(8) |
| 56 | #define SUN4I_CTL_RF_RST BIT(9) |
| 57 | #define SUN4I_CTL_XCH BIT(10) |
| 58 | |
| 59 | /*****************************************************************************/ |
| 60 | /* SUN6I variant of the SPI controller */ |
| 61 | /*****************************************************************************/ |
| 62 | |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 63 | #define SUN6I_SPI0_CCTL 0x24 |
| 64 | #define SUN6I_SPI0_GCR 0x04 |
| 65 | #define SUN6I_SPI0_TCR 0x08 |
| 66 | #define SUN6I_SPI0_FIFO_STA 0x1C |
| 67 | #define SUN6I_SPI0_MBC 0x30 |
| 68 | #define SUN6I_SPI0_MTC 0x34 |
| 69 | #define SUN6I_SPI0_BCC 0x38 |
| 70 | #define SUN6I_SPI0_TXD 0x200 |
| 71 | #define SUN6I_SPI0_RXD 0x300 |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 72 | |
| 73 | #define SUN6I_CTL_ENABLE BIT(0) |
| 74 | #define SUN6I_CTL_MASTER BIT(1) |
| 75 | #define SUN6I_CTL_SRST BIT(31) |
| 76 | #define SUN6I_TCR_XCH BIT(31) |
| 77 | |
| 78 | /*****************************************************************************/ |
| 79 | |
| 80 | #define CCM_AHB_GATING0 (0x01C20000 + 0x60) |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 81 | #define CCM_H6_SPI_BGR_REG (0x03001000 + 0x96c) |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 82 | #ifdef CONFIG_SUN50I_GEN_H6 |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 83 | #define CCM_SPI0_CLK (0x03001000 + 0x940) |
| 84 | #else |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 85 | #define CCM_SPI0_CLK (0x01C20000 + 0xA0) |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 86 | #endif |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 87 | #define SUN6I_BUS_SOFT_RST_REG0 (0x01C20000 + 0x2C0) |
| 88 | |
| 89 | #define AHB_RESET_SPI0_SHIFT 20 |
| 90 | #define AHB_GATE_OFFSET_SPI0 20 |
| 91 | |
| 92 | #define SPI0_CLK_DIV_BY_2 0x1000 |
| 93 | #define SPI0_CLK_DIV_BY_4 0x1001 |
Jesse Taube | ea3cbc6 | 2022-02-11 19:32:34 -0500 | [diff] [blame] | 94 | #define SPI0_CLK_DIV_BY_32 0x100f |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 95 | |
| 96 | /*****************************************************************************/ |
| 97 | |
| 98 | /* |
| 99 | * Allwinner A10/A20 SoCs were using pins PC0,PC1,PC2,PC23 for booting |
| 100 | * from SPI Flash, everything else is using pins PC0,PC1,PC2,PC3. |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 101 | * The H6 uses PC0, PC2, PC3, PC5, the H616 PC0, PC2, PC3, PC4. |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 102 | */ |
| 103 | static void spi0_pinmux_setup(unsigned int pin_function) |
| 104 | { |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 105 | /* All chips use PC0 and PC2. */ |
| 106 | sunxi_gpio_set_cfgpin(SUNXI_GPC(0), pin_function); |
| 107 | sunxi_gpio_set_cfgpin(SUNXI_GPC(2), pin_function); |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 108 | |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 109 | /* All chips except H6 and H616 use PC1. */ |
| 110 | if (!IS_ENABLED(CONFIG_SUN50I_GEN_H6)) |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 111 | sunxi_gpio_set_cfgpin(SUNXI_GPC(1), pin_function); |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 112 | |
| 113 | if (IS_ENABLED(CONFIG_MACH_SUN50I_H6)) |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 114 | sunxi_gpio_set_cfgpin(SUNXI_GPC(5), pin_function); |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 115 | if (IS_ENABLED(CONFIG_MACH_SUN50I_H616)) |
| 116 | sunxi_gpio_set_cfgpin(SUNXI_GPC(4), pin_function); |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 117 | |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 118 | /* Older generations use PC23 for CS, newer ones use PC3. */ |
Andre Przywara | da3bd45 | 2020-01-28 00:46:42 +0000 | [diff] [blame] | 119 | if (IS_ENABLED(CONFIG_MACH_SUN4I) || IS_ENABLED(CONFIG_MACH_SUN7I) || |
| 120 | IS_ENABLED(CONFIG_MACH_SUN8I_R40)) |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 121 | sunxi_gpio_set_cfgpin(SUNXI_GPC(23), pin_function); |
| 122 | else |
| 123 | sunxi_gpio_set_cfgpin(SUNXI_GPC(3), pin_function); |
| 124 | } |
| 125 | |
Andre Przywara | 382dab2 | 2020-01-28 00:46:41 +0000 | [diff] [blame] | 126 | static bool is_sun6i_gen_spi(void) |
| 127 | { |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 128 | return IS_ENABLED(CONFIG_SUNXI_GEN_SUN6I) || |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 129 | IS_ENABLED(CONFIG_SUN50I_GEN_H6); |
Andre Przywara | 382dab2 | 2020-01-28 00:46:41 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 132 | static uintptr_t spi0_base_address(void) |
| 133 | { |
Andre Przywara | da3bd45 | 2020-01-28 00:46:42 +0000 | [diff] [blame] | 134 | if (IS_ENABLED(CONFIG_MACH_SUN8I_R40)) |
| 135 | return 0x01C05000; |
| 136 | |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 137 | if (IS_ENABLED(CONFIG_SUN50I_GEN_H6)) |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 138 | return 0x05010000; |
| 139 | |
Jesse Taube | ea3cbc6 | 2022-02-11 19:32:34 -0500 | [diff] [blame] | 140 | if (!is_sun6i_gen_spi() || |
| 141 | IS_ENABLED(CONFIG_MACH_SUNIV)) |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 142 | return 0x01C05000; |
| 143 | |
| 144 | return 0x01C68000; |
| 145 | } |
| 146 | |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 147 | /* |
| 148 | * Setup 6 MHz from OSC24M (because the BROM is doing the same). |
| 149 | */ |
| 150 | static void spi0_enable_clock(void) |
| 151 | { |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 152 | uintptr_t base = spi0_base_address(); |
| 153 | |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 154 | /* Deassert SPI0 reset on SUN6I */ |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 155 | if (IS_ENABLED(CONFIG_SUN50I_GEN_H6)) |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 156 | setbits_le32(CCM_H6_SPI_BGR_REG, (1U << 16) | 0x1); |
| 157 | else if (is_sun6i_gen_spi()) |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 158 | setbits_le32(SUN6I_BUS_SOFT_RST_REG0, |
| 159 | (1 << AHB_RESET_SPI0_SHIFT)); |
| 160 | |
| 161 | /* Open the SPI0 gate */ |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 162 | if (!IS_ENABLED(CONFIG_SUN50I_GEN_H6)) |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 163 | setbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0)); |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 164 | |
Jesse Taube | ea3cbc6 | 2022-02-11 19:32:34 -0500 | [diff] [blame] | 165 | if (IS_ENABLED(CONFIG_MACH_SUNIV)) { |
| 166 | /* Divide by 32, clock source is AHB clock 200MHz */ |
| 167 | writel(SPI0_CLK_DIV_BY_32, base + SUN6I_SPI0_CCTL); |
| 168 | } else { |
| 169 | /* Divide by 4 */ |
| 170 | writel(SPI0_CLK_DIV_BY_4, base + (is_sun6i_gen_spi() ? |
| 171 | SUN6I_SPI0_CCTL : SUN4I_SPI0_CCTL)); |
| 172 | /* 24MHz from OSC24M */ |
| 173 | writel((1 << 31), CCM_SPI0_CLK); |
| 174 | } |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 175 | |
Andre Przywara | 382dab2 | 2020-01-28 00:46:41 +0000 | [diff] [blame] | 176 | if (is_sun6i_gen_spi()) { |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 177 | /* Enable SPI in the master mode and do a soft reset */ |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 178 | setbits_le32(base + SUN6I_SPI0_GCR, SUN6I_CTL_MASTER | |
| 179 | SUN6I_CTL_ENABLE | SUN6I_CTL_SRST); |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 180 | /* Wait for completion */ |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 181 | while (readl(base + SUN6I_SPI0_GCR) & SUN6I_CTL_SRST) |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 182 | ; |
| 183 | } else { |
| 184 | /* Enable SPI in the master mode and reset FIFO */ |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 185 | setbits_le32(base + SUN4I_SPI0_CTL, SUN4I_CTL_MASTER | |
| 186 | SUN4I_CTL_ENABLE | |
| 187 | SUN4I_CTL_TF_RST | |
| 188 | SUN4I_CTL_RF_RST); |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | static void spi0_disable_clock(void) |
| 193 | { |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 194 | uintptr_t base = spi0_base_address(); |
| 195 | |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 196 | /* Disable the SPI0 controller */ |
Andre Przywara | 382dab2 | 2020-01-28 00:46:41 +0000 | [diff] [blame] | 197 | if (is_sun6i_gen_spi()) |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 198 | clrbits_le32(base + SUN6I_SPI0_GCR, SUN6I_CTL_MASTER | |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 199 | SUN6I_CTL_ENABLE); |
| 200 | else |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 201 | clrbits_le32(base + SUN4I_SPI0_CTL, SUN4I_CTL_MASTER | |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 202 | SUN4I_CTL_ENABLE); |
| 203 | |
| 204 | /* Disable the SPI0 clock */ |
Jesse Taube | ea3cbc6 | 2022-02-11 19:32:34 -0500 | [diff] [blame] | 205 | if (!IS_ENABLED(CONFIG_MACH_SUNIV)) |
| 206 | writel(0, CCM_SPI0_CLK); |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 207 | |
| 208 | /* Close the SPI0 gate */ |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 209 | if (!IS_ENABLED(CONFIG_SUN50I_GEN_H6)) |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 210 | clrbits_le32(CCM_AHB_GATING0, (1 << AHB_GATE_OFFSET_SPI0)); |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 211 | |
| 212 | /* Assert SPI0 reset on SUN6I */ |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 213 | if (IS_ENABLED(CONFIG_SUN50I_GEN_H6)) |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 214 | clrbits_le32(CCM_H6_SPI_BGR_REG, (1U << 16) | 0x1); |
| 215 | else if (is_sun6i_gen_spi()) |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 216 | clrbits_le32(SUN6I_BUS_SOFT_RST_REG0, |
| 217 | (1 << AHB_RESET_SPI0_SHIFT)); |
| 218 | } |
| 219 | |
Andre Przywara | 90895f6 | 2016-11-20 14:56:55 +0000 | [diff] [blame] | 220 | static void spi0_init(void) |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 221 | { |
| 222 | unsigned int pin_function = SUNXI_GPC_SPI0; |
Andre Przywara | 90895f6 | 2016-11-20 14:56:55 +0000 | [diff] [blame] | 223 | |
Andre Przywara | 0c882df | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 224 | if (IS_ENABLED(CONFIG_MACH_SUN50I) || |
Andre Przywara | b2b4ff2 | 2020-12-13 20:19:43 +0000 | [diff] [blame] | 225 | IS_ENABLED(CONFIG_SUN50I_GEN_H6)) |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 226 | pin_function = SUN50I_GPC_SPI0; |
Jesse Taube | ea3cbc6 | 2022-02-11 19:32:34 -0500 | [diff] [blame] | 227 | else if (IS_ENABLED(CONFIG_MACH_SUNIV)) |
| 228 | pin_function = SUNIV_GPC_SPI0; |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 229 | |
| 230 | spi0_pinmux_setup(pin_function); |
| 231 | spi0_enable_clock(); |
| 232 | } |
| 233 | |
| 234 | static void spi0_deinit(void) |
| 235 | { |
| 236 | /* New SoCs can disable pins, older could only set them as input */ |
| 237 | unsigned int pin_function = SUNXI_GPIO_INPUT; |
Andre Przywara | 382dab2 | 2020-01-28 00:46:41 +0000 | [diff] [blame] | 238 | |
| 239 | if (is_sun6i_gen_spi()) |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 240 | pin_function = SUNXI_GPIO_DISABLE; |
| 241 | |
| 242 | spi0_disable_clock(); |
| 243 | spi0_pinmux_setup(pin_function); |
| 244 | } |
| 245 | |
| 246 | /*****************************************************************************/ |
| 247 | |
| 248 | #define SPI_READ_MAX_SIZE 60 /* FIFO size, minus 4 bytes of the header */ |
| 249 | |
| 250 | static void sunxi_spi0_read_data(u8 *buf, u32 addr, u32 bufsize, |
Andre Przywara | c10848d | 2017-02-16 01:20:25 +0000 | [diff] [blame] | 251 | ulong spi_ctl_reg, |
| 252 | ulong spi_ctl_xch_bitmask, |
| 253 | ulong spi_fifo_reg, |
| 254 | ulong spi_tx_reg, |
| 255 | ulong spi_rx_reg, |
| 256 | ulong spi_bc_reg, |
| 257 | ulong spi_tc_reg, |
| 258 | ulong spi_bcc_reg) |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 259 | { |
| 260 | writel(4 + bufsize, spi_bc_reg); /* Burst counter (total bytes) */ |
| 261 | writel(4, spi_tc_reg); /* Transfer counter (bytes to send) */ |
| 262 | if (spi_bcc_reg) |
| 263 | writel(4, spi_bcc_reg); /* SUN6I also needs this */ |
| 264 | |
| 265 | /* Send the Read Data Bytes (03h) command header */ |
| 266 | writeb(0x03, spi_tx_reg); |
| 267 | writeb((u8)(addr >> 16), spi_tx_reg); |
| 268 | writeb((u8)(addr >> 8), spi_tx_reg); |
| 269 | writeb((u8)(addr), spi_tx_reg); |
| 270 | |
| 271 | /* Start the data transfer */ |
| 272 | setbits_le32(spi_ctl_reg, spi_ctl_xch_bitmask); |
| 273 | |
| 274 | /* Wait until everything is received in the RX FIFO */ |
| 275 | while ((readl(spi_fifo_reg) & 0x7F) < 4 + bufsize) |
| 276 | ; |
| 277 | |
| 278 | /* Skip 4 bytes */ |
| 279 | readl(spi_rx_reg); |
| 280 | |
| 281 | /* Read the data */ |
| 282 | while (bufsize-- > 0) |
| 283 | *buf++ = readb(spi_rx_reg); |
| 284 | |
| 285 | /* tSHSL time is up to 100 ns in various SPI flash datasheets */ |
| 286 | udelay(1); |
| 287 | } |
| 288 | |
| 289 | static void spi0_read_data(void *buf, u32 addr, u32 len) |
| 290 | { |
| 291 | u8 *buf8 = buf; |
| 292 | u32 chunk_len; |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 293 | uintptr_t base = spi0_base_address(); |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 294 | |
| 295 | while (len > 0) { |
| 296 | chunk_len = len; |
| 297 | if (chunk_len > SPI_READ_MAX_SIZE) |
| 298 | chunk_len = SPI_READ_MAX_SIZE; |
| 299 | |
Andre Przywara | 382dab2 | 2020-01-28 00:46:41 +0000 | [diff] [blame] | 300 | if (is_sun6i_gen_spi()) { |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 301 | sunxi_spi0_read_data(buf8, addr, chunk_len, |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 302 | base + SUN6I_SPI0_TCR, |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 303 | SUN6I_TCR_XCH, |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 304 | base + SUN6I_SPI0_FIFO_STA, |
| 305 | base + SUN6I_SPI0_TXD, |
| 306 | base + SUN6I_SPI0_RXD, |
| 307 | base + SUN6I_SPI0_MBC, |
| 308 | base + SUN6I_SPI0_MTC, |
| 309 | base + SUN6I_SPI0_BCC); |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 310 | } else { |
| 311 | sunxi_spi0_read_data(buf8, addr, chunk_len, |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 312 | base + SUN4I_SPI0_CTL, |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 313 | SUN4I_CTL_XCH, |
Andre Przywara | 5c7624d | 2020-01-28 00:46:40 +0000 | [diff] [blame] | 314 | base + SUN4I_SPI0_FIFO_STA, |
| 315 | base + SUN4I_SPI0_TX, |
| 316 | base + SUN4I_SPI0_RX, |
| 317 | base + SUN4I_SPI0_BC, |
| 318 | base + SUN4I_SPI0_TC, |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 319 | 0); |
| 320 | } |
| 321 | |
| 322 | len -= chunk_len; |
| 323 | buf8 += chunk_len; |
| 324 | addr += chunk_len; |
| 325 | } |
| 326 | } |
| 327 | |
Andre Przywara | 230fed7 | 2017-09-22 22:57:22 +0100 | [diff] [blame] | 328 | static ulong spi_load_read(struct spl_load_info *load, ulong sector, |
| 329 | ulong count, void *buf) |
| 330 | { |
| 331 | spi0_read_data(buf, sector, count); |
| 332 | |
| 333 | return count; |
| 334 | } |
| 335 | |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 336 | /*****************************************************************************/ |
| 337 | |
Simon Glass | 0649e91 | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 338 | static int spl_spi_load_image(struct spl_image_info *spl_image, |
| 339 | struct spl_boot_device *bootdev) |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 340 | { |
Andre Przywara | 230fed7 | 2017-09-22 22:57:22 +0100 | [diff] [blame] | 341 | int ret = 0; |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 342 | struct legacy_img_hdr *header; |
Samuel Holland | 784fcf6 | 2022-03-18 00:00:44 -0500 | [diff] [blame] | 343 | uint32_t load_offset = sunxi_get_spl_size(); |
Andre Przywara | 05ebd89 | 2021-07-06 00:04:43 +0100 | [diff] [blame] | 344 | |
Simon Glass | 72cc538 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 345 | header = (struct legacy_img_hdr *)CONFIG_TEXT_BASE; |
Samuel Holland | 784fcf6 | 2022-03-18 00:00:44 -0500 | [diff] [blame] | 346 | load_offset = max_t(uint32_t, load_offset, CONFIG_SYS_SPI_U_BOOT_OFFS); |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 347 | |
| 348 | spi0_init(); |
| 349 | |
Andre Przywara | 05ebd89 | 2021-07-06 00:04:43 +0100 | [diff] [blame] | 350 | spi0_read_data((void *)header, load_offset, 0x40); |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 351 | |
Andre Przywara | 230fed7 | 2017-09-22 22:57:22 +0100 | [diff] [blame] | 352 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 353 | image_get_magic(header) == FDT_MAGIC) { |
| 354 | struct spl_load_info load; |
| 355 | |
| 356 | debug("Found FIT image\n"); |
| 357 | load.dev = NULL; |
| 358 | load.priv = NULL; |
| 359 | load.filename = NULL; |
| 360 | load.bl_len = 1; |
| 361 | load.read = spi_load_read; |
| 362 | ret = spl_load_simple_fit(spl_image, &load, |
Andre Przywara | 05ebd89 | 2021-07-06 00:04:43 +0100 | [diff] [blame] | 363 | load_offset, header); |
Andre Przywara | 230fed7 | 2017-09-22 22:57:22 +0100 | [diff] [blame] | 364 | } else { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 365 | ret = spl_parse_image_header(spl_image, bootdev, header); |
Andre Przywara | 230fed7 | 2017-09-22 22:57:22 +0100 | [diff] [blame] | 366 | if (ret) |
| 367 | return ret; |
| 368 | |
| 369 | spi0_read_data((void *)spl_image->load_addr, |
Andre Przywara | 05ebd89 | 2021-07-06 00:04:43 +0100 | [diff] [blame] | 370 | load_offset, spl_image->size); |
Andre Przywara | 230fed7 | 2017-09-22 22:57:22 +0100 | [diff] [blame] | 371 | } |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 372 | |
| 373 | spi0_deinit(); |
Andre Przywara | 230fed7 | 2017-09-22 22:57:22 +0100 | [diff] [blame] | 374 | |
| 375 | return ret; |
Siarhei Siamashka | 6f3ea20 | 2016-06-07 14:28:34 +0300 | [diff] [blame] | 376 | } |
Simon Glass | b9f6d89 | 2016-09-24 18:20:09 -0600 | [diff] [blame] | 377 | /* Use priorty 0 to override the default if it happens to be linked in */ |
Priit Laes | 19d39fc | 2017-01-02 20:24:50 +0200 | [diff] [blame] | 378 | SPL_LOAD_IMAGE_METHOD("sunxi SPI", 0, BOOT_DEVICE_SPI, spl_spi_load_image); |