1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 MediaTek Inc. All Rights Reserved.
5 * Author: Weijie Gao <weijie.gao@mediatek.com>
15 #include <dm/pinctrl.h>
16 #include <linux/bitops.h>
18 #include <linux/iopoll.h>
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)
27 #define SNFI_MAC_OUTL 0x504
28 #define SNFI_MAC_INL 0x508
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)
39 #define SNF_STA_CTL1 0x550
40 #define SPI_STATE GENMASK(3, 0)
42 #define SNFI_GPRAM_OFFSET 0x800
43 #define SNFI_GPRAM_SIZE 0x80
45 #define SNFI_POLL_INTERVAL 500000
46 #define SNFI_RST_POLL_INTERVAL 1000000
48 struct mtk_snfi_priv {
55 static int mtk_snfi_adjust_op_size(struct spi_slave *slave,
56 struct spi_mem_op *op)
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.
66 nbytes = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
68 if (nbytes + op->data.nbytes <= SNFI_GPRAM_SIZE)
71 if (nbytes >= SNFI_GPRAM_SIZE)
74 op->data.nbytes = SNFI_GPRAM_SIZE - nbytes;
79 static bool mtk_snfi_supports_op(struct spi_slave *slave,
80 const struct spi_mem_op *op)
82 if (op->cmd.buswidth > 1 || op->addr.buswidth > 1 ||
83 op->dummy.buswidth > 1 || op->data.buswidth > 1)
89 static int mtk_snfi_mac_trigger(struct mtk_snfi_priv *priv,
90 struct udevice *bus, u32 outlen, u32 inlen)
96 pinctrl_select_state(bus, "snfi");
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);
103 writel(SF_MAC_EN | SF_TRIG, priv->base + SNFI_MAC_CTL);
105 ret = readl_poll_timeout(priv->base + SNFI_MAC_CTL, val,
106 val & WIP_READY, SNFI_POLL_INTERVAL);
108 printf("%s: timed out waiting for WIP_READY\n", __func__);
112 ret = readl_poll_timeout(priv->base + SNFI_MAC_CTL, val,
113 !(val & WIP), SNFI_POLL_INTERVAL);
115 printf("%s: timed out waiting for WIP cleared\n", __func__);
117 writel(0, priv->base + SNFI_MAC_CTL);
120 #ifdef CONFIG_PINCTRL
121 pinctrl_select_state(bus, "default");
127 static int mtk_snfi_mac_reset(struct mtk_snfi_priv *priv)
132 setbits_32(priv->base + SNFI_MISC_CTL, SW_RST);
134 ret = readl_poll_timeout(priv->base + SNF_STA_CTL1, val,
135 !(val & SPI_STATE), SNFI_POLL_INTERVAL);
137 printf("%s: failed to reset snfi mac\n", __func__);
139 writel((2 << FIFO_RD_LTC_SHIFT) |
140 (10 << CS_DESELECT_CYC_SHIFT),
141 priv->base + SNFI_MISC_CTL);
146 static void mtk_snfi_copy_to_gpram(struct mtk_snfi_priv *priv,
147 const void *data, size_t len)
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;
154 * The output data will always be copied to the beginning of
155 * the GPRAM. Uses word write for better performance.
157 * Trailing bytes in the last word are not cared.
160 for (i = 0; i < n; i++)
161 writel(buff[i], gpram + i * sizeof(u32));
164 static void mtk_snfi_copy_from_gpram(struct mtk_snfi_priv *priv, u8 *cache,
165 void *data, size_t pos, size_t len)
167 void __iomem *gpram = priv->base + SNFI_GPRAM_OFFSET;
168 u32 *buff = (u32 *)cache;
171 /* Start position in the buffer */
172 off = pos & (sizeof(u32) - 1);
174 /* End position for copy */
175 end = (len + pos + sizeof(u32) - 1) & (~(sizeof(u32) - 1));
177 /* Start position for copy */
178 pos &= ~(sizeof(u32) - 1);
181 * Read aligned data from GPRAM to buffer first.
182 * Uses word read for better performance.
186 buff[i++] = readl(gpram + pos);
191 memcpy(data, cache + off, len);
194 static int mtk_snfi_exec_op(struct spi_slave *slave,
195 const struct spi_mem_op *op)
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;
206 ret = mtk_snfi_mac_reset(priv);
211 gpram_cache[len++] = op->cmd.opcode;
214 addr_sh = (op->addr.nbytes - 1) * 8;
215 while (addr_sh >= 0) {
216 gpram_cache[len++] = (op->addr.val >> addr_sh) & 0xff;
220 /* Put dummy bytes */
221 for (i = 0; i < op->dummy.nbytes; i++)
222 gpram_cache[len++] = 0;
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;
230 /* Copy final output data to GPRAM */
231 mtk_snfi_copy_to_gpram(priv, gpram_cache, len);
233 /* Start one SPI transaction */
234 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
235 inlen = op->data.nbytes;
237 ret = mtk_snfi_mac_trigger(priv, bus, len, inlen);
241 /* Copy input data from GPRAM */
243 mtk_snfi_copy_from_gpram(priv, gpram_cache, op->data.buf.in,
249 static int mtk_snfi_spi_probe(struct udevice *bus)
251 struct mtk_snfi_priv *priv = dev_get_priv(bus);
254 priv->base = dev_read_addr_ptr(bus);
258 ret = clk_get_by_name(bus, "nfi_clk", &priv->nfi_clk);
262 ret = clk_get_by_name(bus, "pad_clk", &priv->pad_clk);
266 clk_enable(&priv->nfi_clk);
267 clk_enable(&priv->pad_clk);
272 static int mtk_snfi_set_speed(struct udevice *bus, uint speed)
275 * The SNFI does not have a bus clock divider.
276 * The bus clock is set in dts (pad_clk, UNIVPLL2_D8 = 50MHz).
282 static int mtk_snfi_set_mode(struct udevice *bus, uint mode)
284 /* The SNFI supports only mode 0 */
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,
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,
304 static const struct udevice_id mtk_snfi_spi_ids[] = {
305 { .compatible = "mediatek,mtk-snfi-spi" },
309 U_BOOT_DRIVER(mtk_snfi_spi) = {
310 .name = "mtk_snfi_spi",
312 .of_match = mtk_snfi_spi_ids,
313 .ops = &mtk_snfi_spi_ops,
314 .priv_auto = sizeof(struct mtk_snfi_priv),
315 .probe = mtk_snfi_spi_probe,