Rex-BC Chen | 73e6b8e | 2021-11-02 10:31:53 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | /* |
| 4 | * This file is created based on MT8186 Functional Specification |
| 5 | * Chapter number: 5.6, 5.8 |
| 6 | */ |
| 7 | |
Rex-BC Chen | 0d50892 | 2021-11-04 13:56:56 +0800 | [diff] [blame] | 8 | #include <assert.h> |
Rex-BC Chen | 73e6b8e | 2021-11-02 10:31:53 +0800 | [diff] [blame] | 9 | #include <device/mmio.h> |
Ruwen Liu | 0480a19 | 2021-11-04 13:30:23 +0800 | [diff] [blame^] | 10 | #include <spi_flash.h> |
Rex-BC Chen | 73e6b8e | 2021-11-02 10:31:53 +0800 | [diff] [blame] | 11 | #include <soc/addressmap.h> |
Rex-BC Chen | a6b3af9 | 2021-09-23 20:21:18 +0800 | [diff] [blame] | 12 | #include <soc/flash_controller_common.h> |
Rex-BC Chen | 0d50892 | 2021-11-04 13:56:56 +0800 | [diff] [blame] | 13 | #include <soc/gpio.h> |
Rex-BC Chen | 73e6b8e | 2021-11-02 10:31:53 +0800 | [diff] [blame] | 14 | #include <soc/spi.h> |
| 15 | |
Ruwen Liu | 0480a19 | 2021-11-04 13:30:23 +0800 | [diff] [blame^] | 16 | struct mtk_spi_bus spi_bus[SPI_BUS_NUMBER] = { |
| 17 | { |
| 18 | .regs = (void *)SPI0_BASE, |
| 19 | .cs_gpio = GPIO(SPI0_CSB), |
| 20 | }, |
| 21 | { |
| 22 | .regs = (void *)SPI1_BASE, |
| 23 | .cs_gpio = GPIO(SPI1_CSB), |
| 24 | }, |
| 25 | { |
| 26 | .regs = (void *)SPI2_BASE, |
| 27 | .cs_gpio = GPIO(SPI2_CSB), |
| 28 | }, |
| 29 | { |
| 30 | .regs = (void *)SPI3_BASE, |
| 31 | .cs_gpio = GPIO(SPI3_CSB), |
| 32 | }, |
| 33 | { |
| 34 | .regs = (void *)SPI4_BASE, |
| 35 | .cs_gpio = GPIO(EINT11), |
| 36 | }, |
| 37 | { |
| 38 | .regs = (void *)SPI5_BASE, |
| 39 | .cs_gpio = GPIO(SPI5_CSB), |
| 40 | } |
| 41 | }; |
| 42 | |
Rex-BC Chen | 0d50892 | 2021-11-04 13:56:56 +0800 | [diff] [blame] | 43 | struct pad_func { |
| 44 | gpio_t gpio; |
| 45 | u8 func; |
| 46 | }; |
| 47 | |
| 48 | #define PAD_FUNC(name, func) {GPIO(name), PAD_##name##_FUNC_##func} |
Ruwen Liu | 0480a19 | 2021-11-04 13:30:23 +0800 | [diff] [blame^] | 49 | #define PAD_FUNC_GPIO(name) {GPIO(name), 0} |
| 50 | |
| 51 | static const struct pad_func pad0_funcs[SPI_BUS_NUMBER][4] = { |
| 52 | { |
| 53 | PAD_FUNC(SPI0_MI, SPI0_MI_A), |
| 54 | PAD_FUNC_GPIO(SPI0_CSB), |
| 55 | PAD_FUNC(SPI0_MO, SPI0_MO_A), |
| 56 | PAD_FUNC(SPI0_CLK, SPI0_CLK_A), |
| 57 | }, |
| 58 | { |
| 59 | PAD_FUNC(SPI1_MI, SPI1_MI_A), |
| 60 | PAD_FUNC_GPIO(SPI1_CSB), |
| 61 | PAD_FUNC(SPI1_MO, SPI1_MO_A), |
| 62 | PAD_FUNC(SPI1_CLK, SPI1_CLK_A), |
| 63 | }, |
| 64 | { |
| 65 | PAD_FUNC(SPI2_MI, SPI2_MI_A), |
| 66 | PAD_FUNC_GPIO(SPI2_CSB), |
| 67 | PAD_FUNC(SPI2_MO, SPI2_MO_A), |
| 68 | PAD_FUNC(SPI2_CK, SPI2_CLK_A), |
| 69 | }, |
| 70 | { |
| 71 | PAD_FUNC(SPI3_MI, SPI3_MI), |
| 72 | PAD_FUNC_GPIO(SPI3_CSB), |
| 73 | PAD_FUNC(SPI3_MO, SPI3_MO), |
| 74 | PAD_FUNC(SPI3_CLK, SPI3_CLK), |
| 75 | }, |
| 76 | { |
| 77 | PAD_FUNC(EINT13, SPI4_MI_A), |
| 78 | PAD_FUNC_GPIO(EINT11), |
| 79 | PAD_FUNC(EINT12, SPI4_MO_A), |
| 80 | PAD_FUNC(EINT10, SPI4_CLK_A), |
| 81 | }, |
| 82 | { |
| 83 | PAD_FUNC(SPI5_MI, SPI5_MI), |
| 84 | PAD_FUNC_GPIO(SPI5_CSB), |
| 85 | PAD_FUNC(SPI5_MO, SPI5_MO), |
| 86 | PAD_FUNC(SPI5_CLK, SPI5_CLK), |
| 87 | }, |
| 88 | }; |
| 89 | |
| 90 | static const struct pad_func pad1_funcs[SPI_BUS_NUMBER][4] = { |
| 91 | { |
| 92 | PAD_FUNC(EINT3, SPI0_MI_B), |
| 93 | PAD_FUNC_GPIO(EINT1), |
| 94 | PAD_FUNC(EINT2, SPI0_MO_B), |
| 95 | PAD_FUNC(EINT0, SPI0_CLK_B), |
| 96 | }, |
| 97 | { |
| 98 | PAD_FUNC(EINT9, SPI1_MI_B), |
| 99 | PAD_FUNC_GPIO(EINT7), |
| 100 | PAD_FUNC(EINT8, SPI1_MO_B), |
| 101 | PAD_FUNC(EINT6, SPI1_CLK_B), |
| 102 | }, |
| 103 | { |
| 104 | PAD_FUNC(CAM_PDN1, SPI2_MI_B), |
| 105 | PAD_FUNC_GPIO(CAM_PDN0), |
| 106 | PAD_FUNC(CAM_RST0, SPI2_MO_B), |
| 107 | PAD_FUNC(EINT18, SPI2_CLK_B), |
| 108 | }, |
| 109 | { |
| 110 | }, |
| 111 | { |
| 112 | PAD_FUNC(I2S2_DI, SPI4_MI_B), |
| 113 | PAD_FUNC_GPIO(I2S2_BCK), |
| 114 | PAD_FUNC(I2S2_LRCK, SPI4_MO_B), |
| 115 | PAD_FUNC(I2S2_MCK, SPI4_CLK_B), |
| 116 | }, |
| 117 | { |
| 118 | }, |
| 119 | }; |
Rex-BC Chen | 0d50892 | 2021-11-04 13:56:56 +0800 | [diff] [blame] | 120 | |
| 121 | static const struct pad_func nor_pinmux[SPI_NOR_GPIO_SET_NUM][4] = { |
| 122 | /* GPIO 36 ~ 39 */ |
| 123 | [SPI_NOR_GPIO_SET0] = { |
| 124 | PAD_FUNC(SPI0_CLK, SPINOR_CK), |
| 125 | PAD_FUNC(SPI0_CSB, SPINOR_CS), |
| 126 | PAD_FUNC(SPI0_MO, SPINOR_IO0), |
| 127 | PAD_FUNC(SPI0_MI, SPINOR_IO1), |
| 128 | }, |
| 129 | /* GPIO 61 ~ 64 */ |
| 130 | [SPI_NOR_GPIO_SET1] = { |
| 131 | PAD_FUNC(TDM_RX_BCK, SPINOR_CK), |
| 132 | PAD_FUNC(TDM_RX_MCLK, SPINOR_CS), |
| 133 | PAD_FUNC(TDM_RX_DATA0, SPINOR_IO0), |
| 134 | PAD_FUNC(TDM_RX_DATA1, SPINOR_IO1), |
| 135 | }, |
| 136 | }; |
| 137 | |
| 138 | void mtk_snfc_init(int gpio_set) |
| 139 | { |
| 140 | const struct pad_func *ptr = NULL; |
| 141 | |
| 142 | assert(gpio_set < SPI_NOR_GPIO_SET_NUM); |
| 143 | |
| 144 | ptr = nor_pinmux[gpio_set]; |
| 145 | for (size_t i = 0; i < ARRAY_SIZE(nor_pinmux[gpio_set]); i++) { |
| 146 | gpio_set_pull(ptr[i].gpio, GPIO_PULL_ENABLE, GPIO_PULL_UP); |
| 147 | gpio_set_mode(ptr[i].gpio, ptr[i].func); |
| 148 | } |
| 149 | } |
| 150 | |
Ruwen Liu | 0480a19 | 2021-11-04 13:30:23 +0800 | [diff] [blame^] | 151 | void mtk_spi_set_gpio_pinmux(unsigned int bus, enum spi_pad_mask pad_select) |
| 152 | { |
| 153 | assert(bus < SPI_BUS_NUMBER); |
| 154 | const struct pad_func *ptr; |
| 155 | |
| 156 | if (pad_select == SPI_PAD0_MASK) { |
| 157 | ptr = pad0_funcs[bus]; |
| 158 | } else { |
| 159 | assert((bus == 0 || bus == 1 || bus == 2 || bus == 4) && |
| 160 | pad_select == SPI_PAD1_MASK); |
| 161 | ptr = pad1_funcs[bus]; |
| 162 | } |
| 163 | for (int i = 0; i < 4; i++) |
| 164 | gpio_set_mode(ptr[i].gpio, ptr[i].func); |
| 165 | } |
| 166 | |
Rex-BC Chen | 73e6b8e | 2021-11-02 10:31:53 +0800 | [diff] [blame] | 167 | static const struct spi_ctrlr spi_flash_ctrlr = { |
| 168 | .max_xfer_size = 65535, |
Rex-BC Chen | a6b3af9 | 2021-09-23 20:21:18 +0800 | [diff] [blame] | 169 | .flash_probe = mtk_spi_flash_probe, |
Rex-BC Chen | 73e6b8e | 2021-11-02 10:31:53 +0800 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = { |
| 173 | { |
Ruwen Liu | 0480a19 | 2021-11-04 13:30:23 +0800 | [diff] [blame^] | 174 | .ctrlr = &spi_ctrlr, |
| 175 | .bus_start = 0, |
| 176 | .bus_end = SPI_BUS_NUMBER - 1, |
| 177 | }, |
| 178 | { |
Rex-BC Chen | 73e6b8e | 2021-11-02 10:31:53 +0800 | [diff] [blame] | 179 | .ctrlr = &spi_flash_ctrlr, |
Rex-BC Chen | a6b3af9 | 2021-09-23 20:21:18 +0800 | [diff] [blame] | 180 | .bus_start = CONFIG_BOOT_DEVICE_SPI_FLASH_BUS, |
| 181 | .bus_end = CONFIG_BOOT_DEVICE_SPI_FLASH_BUS, |
Rex-BC Chen | 73e6b8e | 2021-11-02 10:31:53 +0800 | [diff] [blame] | 182 | }, |
| 183 | }; |
| 184 | |
| 185 | const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map); |