1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 MediaTek Inc. All Rights Reserved.
5 * Author: Weijie Gao <weijie.gao@mediatek.com>
16 #include <dm/pinctrl.h>
17 #include <linux/bitops.h>
19 #include <linux/iopoll.h>
21 #define SNFI_MAC_CTL 0x500
22 #define MAC_XIO_SEL BIT(4)
23 #define SF_MAC_EN BIT(3)
24 #define SF_TRIG BIT(2)
25 #define WIP_READY BIT(1)
28 #define SNFI_MAC_OUTL 0x504
29 #define SNFI_MAC_INL 0x508
31 #define SNFI_MISC_CTL 0x538
32 #define SW_RST BIT(28)
33 #define FIFO_RD_LTC_SHIFT 25
34 #define FIFO_RD_LTC GENMASK(26, 25)
35 #define LATCH_LAT_SHIFT 8
36 #define LATCH_LAT GENMASK(9, 8)
37 #define CS_DESELECT_CYC_SHIFT 0
38 #define CS_DESELECT_CYC GENMASK(4, 0)
40 #define SNF_STA_CTL1 0x550
41 #define SPI_STATE GENMASK(3, 0)
43 #define SNFI_GPRAM_OFFSET 0x800
44 #define SNFI_GPRAM_SIZE 0x80
46 #define SNFI_POLL_INTERVAL 500000
47 #define SNFI_RST_POLL_INTERVAL 1000000
49 struct mtk_snfi_priv {
56 static int mtk_snfi_adjust_op_size(struct spi_slave *slave,
57 struct spi_mem_op *op)
62 * When there is input data, it will be appended after the output
63 * data in the GPRAM. So the total size of either pure output data
64 * or the output+input data must not exceed the GPRAM size.
67 nbytes = sizeof(op->cmd.opcode) + op->addr.nbytes +
70 if (nbytes + op->data.nbytes <= SNFI_GPRAM_SIZE)
73 if (nbytes >= SNFI_GPRAM_SIZE)
76 op->data.nbytes = SNFI_GPRAM_SIZE - nbytes;
81 static bool mtk_snfi_supports_op(struct spi_slave *slave,
82 const struct spi_mem_op *op)
84 if (op->cmd.buswidth > 1 || op->addr.buswidth > 1 ||
85 op->dummy.buswidth > 1 || op->data.buswidth > 1)
91 static int mtk_snfi_mac_trigger(struct mtk_snfi_priv *priv,
92 struct udevice *bus, u32 outlen, u32 inlen)
98 pinctrl_select_state(bus, "snfi");
101 writel(SF_MAC_EN, priv->base + SNFI_MAC_CTL);
102 writel(outlen, priv->base + SNFI_MAC_OUTL);
103 writel(inlen, priv->base + SNFI_MAC_INL);
105 writel(SF_MAC_EN | SF_TRIG, priv->base + SNFI_MAC_CTL);
107 ret = readl_poll_timeout(priv->base + SNFI_MAC_CTL, val,
108 val & WIP_READY, SNFI_POLL_INTERVAL);
110 printf("%s: timed out waiting for WIP_READY\n", __func__);
114 ret = readl_poll_timeout(priv->base + SNFI_MAC_CTL, val,
115 !(val & WIP), SNFI_POLL_INTERVAL);
117 printf("%s: timed out waiting for WIP cleared\n", __func__);
119 writel(0, priv->base + SNFI_MAC_CTL);
122 #ifdef CONFIG_PINCTRL
123 pinctrl_select_state(bus, "default");
129 static int mtk_snfi_mac_reset(struct mtk_snfi_priv *priv)
134 setbits_32(priv->base + SNFI_MISC_CTL, SW_RST);
136 ret = readl_poll_timeout(priv->base + SNF_STA_CTL1, val,
137 !(val & SPI_STATE), SNFI_POLL_INTERVAL);
139 printf("%s: failed to reset snfi mac\n", __func__);
141 writel((2 << FIFO_RD_LTC_SHIFT) |
142 (10 << CS_DESELECT_CYC_SHIFT),
143 priv->base + SNFI_MISC_CTL);
148 static void mtk_snfi_copy_to_gpram(struct mtk_snfi_priv *priv,
149 const void *data, size_t len)
151 void __iomem *gpram = priv->base + SNFI_GPRAM_OFFSET;
152 size_t i, n = (len + sizeof(u32) - 1) / sizeof(u32);
153 const u32 *buff = data;
156 * The output data will always be copied to the beginning of
157 * the GPRAM. Uses word write for better performace.
159 * Trailing bytes in the last word are not cared.
162 for (i = 0; i < n; i++)
163 writel(buff[i], gpram + i * sizeof(u32));
166 static void mtk_snfi_copy_from_gpram(struct mtk_snfi_priv *priv, u8 *cache,
167 void *data, size_t pos, size_t len)
169 void __iomem *gpram = priv->base + SNFI_GPRAM_OFFSET;
170 u32 *buff = (u32 *)cache;
173 /* Start position in the buffer */
174 off = pos & (sizeof(u32) - 1);
176 /* End position for copy */
177 end = (len + pos + sizeof(u32) - 1) & (~(sizeof(u32) - 1));
179 /* Start position for copy */
180 pos &= ~(sizeof(u32) - 1);
183 * Read aligned data from GPRAM to buffer first.
184 * Uses word read for better performace.
188 buff[i++] = readl(gpram + pos);
193 memcpy(data, cache + off, len);
196 static int mtk_snfi_exec_op(struct spi_slave *slave,
197 const struct spi_mem_op *op)
199 struct udevice *bus = dev_get_parent(slave->dev);
200 struct mtk_snfi_priv *priv = dev_get_priv(bus);
201 u8 gpram_cache[SNFI_GPRAM_SIZE];
202 u32 i, len = 0, inlen = 0;
208 ret = mtk_snfi_mac_reset(priv);
213 gpram_cache[len++] = op->cmd.opcode;
216 addr_sh = (op->addr.nbytes - 1) * 8;
217 while (addr_sh >= 0) {
218 gpram_cache[len++] = (op->addr.val >> addr_sh) & 0xff;
222 /* Put dummy bytes */
223 for (i = 0; i < op->dummy.nbytes; i++)
224 gpram_cache[len++] = 0;
226 /* Put output data */
227 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT) {
228 memcpy(gpram_cache + len, op->data.buf.out, op->data.nbytes);
229 len += op->data.nbytes;
232 /* Copy final output data to GPRAM */
233 mtk_snfi_copy_to_gpram(priv, gpram_cache, len);
235 /* Start one SPI transaction */
236 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
237 inlen = op->data.nbytes;
239 ret = mtk_snfi_mac_trigger(priv, bus, len, inlen);
243 /* Copy input data from GPRAM */
245 mtk_snfi_copy_from_gpram(priv, gpram_cache, op->data.buf.in,
251 static int mtk_snfi_spi_probe(struct udevice *bus)
253 struct mtk_snfi_priv *priv = dev_get_priv(bus);
256 priv->base = (void __iomem *)devfdt_get_addr(bus);
260 ret = clk_get_by_name(bus, "nfi_clk", &priv->nfi_clk);
264 ret = clk_get_by_name(bus, "pad_clk", &priv->pad_clk);
268 clk_enable(&priv->nfi_clk);
269 clk_enable(&priv->pad_clk);
274 static int mtk_snfi_set_speed(struct udevice *bus, uint speed)
277 * The SNFI does not have a bus clock divider.
278 * The bus clock is set in dts (pad_clk, UNIVPLL2_D8 = 50MHz).
284 static int mtk_snfi_set_mode(struct udevice *bus, uint mode)
286 /* The SNFI supports only mode 0 */
294 static const struct spi_controller_mem_ops mtk_snfi_mem_ops = {
295 .adjust_op_size = mtk_snfi_adjust_op_size,
296 .supports_op = mtk_snfi_supports_op,
297 .exec_op = mtk_snfi_exec_op,
300 static const struct dm_spi_ops mtk_snfi_spi_ops = {
301 .mem_ops = &mtk_snfi_mem_ops,
302 .set_speed = mtk_snfi_set_speed,
303 .set_mode = mtk_snfi_set_mode,
306 static const struct udevice_id mtk_snfi_spi_ids[] = {
307 { .compatible = "mediatek,mtk-snfi-spi" },
311 U_BOOT_DRIVER(mtk_snfi_spi) = {
312 .name = "mtk_snfi_spi",
314 .of_match = mtk_snfi_spi_ids,
315 .ops = &mtk_snfi_spi_ops,
316 .priv_auto_alloc_size = sizeof(struct mtk_snfi_priv),
317 .probe = mtk_snfi_spi_probe,