developer | 8a78b4d | 2019-07-22 17:09:01 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2019 MediaTek Inc. All Rights Reserved. |
| 4 | * |
| 5 | * Author: Weijie Gao <weijie.gao@mediatek.com> |
| 6 | */ |
| 7 | |
developer | 8a78b4d | 2019-07-22 17:09:01 +0530 | [diff] [blame] | 8 | #include <clk.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <spi.h> |
| 12 | #include <spi-mem.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <watchdog.h> |
| 15 | #include <dm/pinctrl.h> |
| 16 | #include <linux/bitops.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/iopoll.h> |
| 19 | |
| 20 | #define SNFI_MAC_CTL 0x500 |
| 21 | #define MAC_XIO_SEL BIT(4) |
| 22 | #define SF_MAC_EN BIT(3) |
| 23 | #define SF_TRIG BIT(2) |
| 24 | #define WIP_READY BIT(1) |
| 25 | #define WIP BIT(0) |
| 26 | |
| 27 | #define SNFI_MAC_OUTL 0x504 |
| 28 | #define SNFI_MAC_INL 0x508 |
| 29 | |
| 30 | #define SNFI_MISC_CTL 0x538 |
| 31 | #define SW_RST BIT(28) |
| 32 | #define FIFO_RD_LTC_SHIFT 25 |
| 33 | #define FIFO_RD_LTC GENMASK(26, 25) |
| 34 | #define LATCH_LAT_SHIFT 8 |
| 35 | #define LATCH_LAT GENMASK(9, 8) |
| 36 | #define CS_DESELECT_CYC_SHIFT 0 |
| 37 | #define CS_DESELECT_CYC GENMASK(4, 0) |
| 38 | |
| 39 | #define SNF_STA_CTL1 0x550 |
| 40 | #define SPI_STATE GENMASK(3, 0) |
| 41 | |
| 42 | #define SNFI_GPRAM_OFFSET 0x800 |
| 43 | #define SNFI_GPRAM_SIZE 0x80 |
| 44 | |
| 45 | #define SNFI_POLL_INTERVAL 500000 |
| 46 | #define SNFI_RST_POLL_INTERVAL 1000000 |
| 47 | |
| 48 | struct mtk_snfi_priv { |
| 49 | void __iomem *base; |
| 50 | |
| 51 | struct clk nfi_clk; |
| 52 | struct clk pad_clk; |
| 53 | }; |
| 54 | |
| 55 | static int mtk_snfi_adjust_op_size(struct spi_slave *slave, |
| 56 | struct spi_mem_op *op) |
| 57 | { |
| 58 | u32 nbytes; |
| 59 | |
| 60 | /* |
| 61 | * When there is input data, it will be appended after the output |
| 62 | * data in the GPRAM. So the total size of either pure output data |
| 63 | * or the output+input data must not exceed the GPRAM size. |
| 64 | */ |
| 65 | |
Pratyush Yadav | ed08485 | 2021-06-26 00:47:04 +0530 | [diff] [blame] | 66 | nbytes = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; |
developer | 8a78b4d | 2019-07-22 17:09:01 +0530 | [diff] [blame] | 67 | |
| 68 | if (nbytes + op->data.nbytes <= SNFI_GPRAM_SIZE) |
| 69 | return 0; |
| 70 | |
| 71 | if (nbytes >= SNFI_GPRAM_SIZE) |
| 72 | return -ENOTSUPP; |
| 73 | |
| 74 | op->data.nbytes = SNFI_GPRAM_SIZE - nbytes; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static bool mtk_snfi_supports_op(struct spi_slave *slave, |
| 80 | const struct spi_mem_op *op) |
| 81 | { |
| 82 | if (op->cmd.buswidth > 1 || op->addr.buswidth > 1 || |
| 83 | op->dummy.buswidth > 1 || op->data.buswidth > 1) |
| 84 | return false; |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | static int mtk_snfi_mac_trigger(struct mtk_snfi_priv *priv, |
| 90 | struct udevice *bus, u32 outlen, u32 inlen) |
| 91 | { |
| 92 | int ret; |
| 93 | u32 val; |
| 94 | |
| 95 | #ifdef CONFIG_PINCTRL |
| 96 | pinctrl_select_state(bus, "snfi"); |
| 97 | #endif |
| 98 | |
| 99 | writel(SF_MAC_EN, priv->base + SNFI_MAC_CTL); |
| 100 | writel(outlen, priv->base + SNFI_MAC_OUTL); |
| 101 | writel(inlen, priv->base + SNFI_MAC_INL); |
| 102 | |
| 103 | writel(SF_MAC_EN | SF_TRIG, priv->base + SNFI_MAC_CTL); |
| 104 | |
| 105 | ret = readl_poll_timeout(priv->base + SNFI_MAC_CTL, val, |
| 106 | val & WIP_READY, SNFI_POLL_INTERVAL); |
| 107 | if (ret) { |
| 108 | printf("%s: timed out waiting for WIP_READY\n", __func__); |
| 109 | goto cleanup; |
| 110 | } |
| 111 | |
| 112 | ret = readl_poll_timeout(priv->base + SNFI_MAC_CTL, val, |
| 113 | !(val & WIP), SNFI_POLL_INTERVAL); |
| 114 | if (ret) |
| 115 | printf("%s: timed out waiting for WIP cleared\n", __func__); |
| 116 | |
| 117 | writel(0, priv->base + SNFI_MAC_CTL); |
| 118 | |
| 119 | cleanup: |
| 120 | #ifdef CONFIG_PINCTRL |
| 121 | pinctrl_select_state(bus, "default"); |
| 122 | #endif |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static int mtk_snfi_mac_reset(struct mtk_snfi_priv *priv) |
| 128 | { |
| 129 | int ret; |
| 130 | u32 val; |
| 131 | |
| 132 | setbits_32(priv->base + SNFI_MISC_CTL, SW_RST); |
| 133 | |
| 134 | ret = readl_poll_timeout(priv->base + SNF_STA_CTL1, val, |
| 135 | !(val & SPI_STATE), SNFI_POLL_INTERVAL); |
| 136 | if (ret) |
| 137 | printf("%s: failed to reset snfi mac\n", __func__); |
| 138 | |
| 139 | writel((2 << FIFO_RD_LTC_SHIFT) | |
| 140 | (10 << CS_DESELECT_CYC_SHIFT), |
| 141 | priv->base + SNFI_MISC_CTL); |
| 142 | |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static void mtk_snfi_copy_to_gpram(struct mtk_snfi_priv *priv, |
| 147 | const void *data, size_t len) |
| 148 | { |
| 149 | void __iomem *gpram = priv->base + SNFI_GPRAM_OFFSET; |
| 150 | size_t i, n = (len + sizeof(u32) - 1) / sizeof(u32); |
| 151 | const u32 *buff = data; |
| 152 | |
| 153 | /* |
| 154 | * The output data will always be copied to the beginning of |
Pengfei Fan | 746271d | 2022-12-09 09:39:50 +0800 | [diff] [blame] | 155 | * the GPRAM. Uses word write for better performance. |
developer | 8a78b4d | 2019-07-22 17:09:01 +0530 | [diff] [blame] | 156 | * |
| 157 | * Trailing bytes in the last word are not cared. |
| 158 | */ |
| 159 | |
| 160 | for (i = 0; i < n; i++) |
| 161 | writel(buff[i], gpram + i * sizeof(u32)); |
| 162 | } |
| 163 | |
| 164 | static void mtk_snfi_copy_from_gpram(struct mtk_snfi_priv *priv, u8 *cache, |
| 165 | void *data, size_t pos, size_t len) |
| 166 | { |
| 167 | void __iomem *gpram = priv->base + SNFI_GPRAM_OFFSET; |
| 168 | u32 *buff = (u32 *)cache; |
| 169 | size_t i, off, end; |
| 170 | |
| 171 | /* Start position in the buffer */ |
| 172 | off = pos & (sizeof(u32) - 1); |
| 173 | |
| 174 | /* End position for copy */ |
| 175 | end = (len + pos + sizeof(u32) - 1) & (~(sizeof(u32) - 1)); |
| 176 | |
| 177 | /* Start position for copy */ |
| 178 | pos &= ~(sizeof(u32) - 1); |
| 179 | |
| 180 | /* |
| 181 | * Read aligned data from GPRAM to buffer first. |
Pengfei Fan | 746271d | 2022-12-09 09:39:50 +0800 | [diff] [blame] | 182 | * Uses word read for better performance. |
developer | 8a78b4d | 2019-07-22 17:09:01 +0530 | [diff] [blame] | 183 | */ |
| 184 | i = 0; |
| 185 | while (pos < end) { |
| 186 | buff[i++] = readl(gpram + pos); |
| 187 | pos += sizeof(u32); |
| 188 | } |
| 189 | |
| 190 | /* Copy rx data */ |
| 191 | memcpy(data, cache + off, len); |
| 192 | } |
| 193 | |
| 194 | static int mtk_snfi_exec_op(struct spi_slave *slave, |
| 195 | const struct spi_mem_op *op) |
| 196 | { |
| 197 | struct udevice *bus = dev_get_parent(slave->dev); |
| 198 | struct mtk_snfi_priv *priv = dev_get_priv(bus); |
| 199 | u8 gpram_cache[SNFI_GPRAM_SIZE]; |
| 200 | u32 i, len = 0, inlen = 0; |
| 201 | int addr_sh; |
| 202 | int ret; |
| 203 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 204 | schedule(); |
developer | 8a78b4d | 2019-07-22 17:09:01 +0530 | [diff] [blame] | 205 | |
| 206 | ret = mtk_snfi_mac_reset(priv); |
| 207 | if (ret) |
| 208 | return ret; |
| 209 | |
| 210 | /* Put opcode */ |
| 211 | gpram_cache[len++] = op->cmd.opcode; |
| 212 | |
| 213 | /* Put address */ |
| 214 | addr_sh = (op->addr.nbytes - 1) * 8; |
| 215 | while (addr_sh >= 0) { |
| 216 | gpram_cache[len++] = (op->addr.val >> addr_sh) & 0xff; |
| 217 | addr_sh -= 8; |
| 218 | } |
| 219 | |
| 220 | /* Put dummy bytes */ |
| 221 | for (i = 0; i < op->dummy.nbytes; i++) |
| 222 | gpram_cache[len++] = 0; |
| 223 | |
| 224 | /* Put output data */ |
| 225 | if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT) { |
| 226 | memcpy(gpram_cache + len, op->data.buf.out, op->data.nbytes); |
| 227 | len += op->data.nbytes; |
| 228 | } |
| 229 | |
| 230 | /* Copy final output data to GPRAM */ |
| 231 | mtk_snfi_copy_to_gpram(priv, gpram_cache, len); |
| 232 | |
| 233 | /* Start one SPI transaction */ |
| 234 | if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN) |
| 235 | inlen = op->data.nbytes; |
| 236 | |
| 237 | ret = mtk_snfi_mac_trigger(priv, bus, len, inlen); |
| 238 | if (ret) |
| 239 | return ret; |
| 240 | |
| 241 | /* Copy input data from GPRAM */ |
| 242 | if (inlen) |
| 243 | mtk_snfi_copy_from_gpram(priv, gpram_cache, op->data.buf.in, |
| 244 | len, inlen); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int mtk_snfi_spi_probe(struct udevice *bus) |
| 250 | { |
| 251 | struct mtk_snfi_priv *priv = dev_get_priv(bus); |
| 252 | int ret; |
| 253 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 254 | priv->base = dev_read_addr_ptr(bus); |
developer | 8a78b4d | 2019-07-22 17:09:01 +0530 | [diff] [blame] | 255 | if (!priv->base) |
| 256 | return -EINVAL; |
| 257 | |
| 258 | ret = clk_get_by_name(bus, "nfi_clk", &priv->nfi_clk); |
| 259 | if (ret < 0) |
| 260 | return ret; |
| 261 | |
| 262 | ret = clk_get_by_name(bus, "pad_clk", &priv->pad_clk); |
| 263 | if (ret < 0) |
| 264 | return ret; |
| 265 | |
| 266 | clk_enable(&priv->nfi_clk); |
| 267 | clk_enable(&priv->pad_clk); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int mtk_snfi_set_speed(struct udevice *bus, uint speed) |
| 273 | { |
| 274 | /* |
| 275 | * The SNFI does not have a bus clock divider. |
| 276 | * The bus clock is set in dts (pad_clk, UNIVPLL2_D8 = 50MHz). |
| 277 | */ |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int mtk_snfi_set_mode(struct udevice *bus, uint mode) |
| 283 | { |
| 284 | /* The SNFI supports only mode 0 */ |
| 285 | |
| 286 | if (mode) |
| 287 | return -EINVAL; |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static const struct spi_controller_mem_ops mtk_snfi_mem_ops = { |
| 293 | .adjust_op_size = mtk_snfi_adjust_op_size, |
| 294 | .supports_op = mtk_snfi_supports_op, |
| 295 | .exec_op = mtk_snfi_exec_op, |
| 296 | }; |
| 297 | |
| 298 | static const struct dm_spi_ops mtk_snfi_spi_ops = { |
| 299 | .mem_ops = &mtk_snfi_mem_ops, |
| 300 | .set_speed = mtk_snfi_set_speed, |
| 301 | .set_mode = mtk_snfi_set_mode, |
| 302 | }; |
| 303 | |
| 304 | static const struct udevice_id mtk_snfi_spi_ids[] = { |
| 305 | { .compatible = "mediatek,mtk-snfi-spi" }, |
| 306 | { } |
| 307 | }; |
| 308 | |
| 309 | U_BOOT_DRIVER(mtk_snfi_spi) = { |
| 310 | .name = "mtk_snfi_spi", |
| 311 | .id = UCLASS_SPI, |
| 312 | .of_match = mtk_snfi_spi_ids, |
| 313 | .ops = &mtk_snfi_spi_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 314 | .priv_auto = sizeof(struct mtk_snfi_priv), |
developer | 8a78b4d | 2019-07-22 17:09:01 +0530 | [diff] [blame] | 315 | .probe = mtk_snfi_spi_probe, |
| 316 | }; |