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