1 // SPDX-License-Identifier: GPL-2.0-only
3 // HiSilicon SPI NOR V3XX Flash Controller Driver for hi16xx chipsets
5 // Copyright (c) 2019 HiSilicon Technologies Co., Ltd.
6 // Author: John Garry <john.garry@huawei.com>
8 #include <linux/acpi.h>
9 #include <linux/bitops.h>
10 #include <linux/iopoll.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
15 #include <linux/spi/spi-mem.h>
17 #define HISI_SFC_V3XX_VERSION (0x1f8)
19 #define HISI_SFC_V3XX_CMD_CFG (0x300)
20 #define HISI_SFC_V3XX_CMD_CFG_DUAL_IN_DUAL_OUT (1 << 17)
21 #define HISI_SFC_V3XX_CMD_CFG_DUAL_IO (2 << 17)
22 #define HISI_SFC_V3XX_CMD_CFG_FULL_DIO (3 << 17)
23 #define HISI_SFC_V3XX_CMD_CFG_QUAD_IN_QUAD_OUT (5 << 17)
24 #define HISI_SFC_V3XX_CMD_CFG_QUAD_IO (6 << 17)
25 #define HISI_SFC_V3XX_CMD_CFG_FULL_QIO (7 << 17)
26 #define HISI_SFC_V3XX_CMD_CFG_DATA_CNT_OFF 9
27 #define HISI_SFC_V3XX_CMD_CFG_RW_MSK BIT(8)
28 #define HISI_SFC_V3XX_CMD_CFG_DATA_EN_MSK BIT(7)
29 #define HISI_SFC_V3XX_CMD_CFG_DUMMY_CNT_OFF 4
30 #define HISI_SFC_V3XX_CMD_CFG_ADDR_EN_MSK BIT(3)
31 #define HISI_SFC_V3XX_CMD_CFG_CS_SEL_OFF 1
32 #define HISI_SFC_V3XX_CMD_CFG_START_MSK BIT(0)
33 #define HISI_SFC_V3XX_CMD_INS (0x308)
34 #define HISI_SFC_V3XX_CMD_ADDR (0x30c)
35 #define HISI_SFC_V3XX_CMD_DATABUF0 (0x400)
37 struct hisi_sfc_v3xx_host {
39 void __iomem *regbase;
43 #define HISI_SFC_V3XX_WAIT_TIMEOUT_US 1000000
44 #define HISI_SFC_V3XX_WAIT_POLL_INTERVAL_US 10
46 static int hisi_sfc_v3xx_wait_cmd_idle(struct hisi_sfc_v3xx_host *host)
50 return readl_poll_timeout(host->regbase + HISI_SFC_V3XX_CMD_CFG, reg,
51 !(reg & HISI_SFC_V3XX_CMD_CFG_START_MSK),
52 HISI_SFC_V3XX_WAIT_POLL_INTERVAL_US,
53 HISI_SFC_V3XX_WAIT_TIMEOUT_US);
56 static int hisi_sfc_v3xx_adjust_op_size(struct spi_mem *mem,
57 struct spi_mem_op *op)
59 struct spi_device *spi = mem->spi;
60 struct hisi_sfc_v3xx_host *host;
61 uintptr_t addr = (uintptr_t)op->data.buf.in;
64 host = spi_controller_get_devdata(spi->master);
66 max_byte_count = host->max_cmd_dword * 4;
68 if (!IS_ALIGNED(addr, 4) && op->data.nbytes >= 4)
69 op->data.nbytes = 4 - (addr % 4);
70 else if (op->data.nbytes > max_byte_count)
71 op->data.nbytes = max_byte_count;
77 * memcpy_{to,from}io doesn't gurantee 32b accesses - which we require for the
78 * DATABUF registers -so use __io{read,write}32_copy when possible. For
79 * trailing bytes, copy them byte-by-byte from the DATABUF register, as we
80 * can't clobber outside the source/dest buffer.
82 * For efficient data read/write, we try to put any start 32b unaligned data
83 * into a separate transaction in hisi_sfc_v3xx_adjust_op_size().
85 static void hisi_sfc_v3xx_read_databuf(struct hisi_sfc_v3xx_host *host,
86 u8 *to, unsigned int len)
91 from = host->regbase + HISI_SFC_V3XX_CMD_DATABUF0;
93 if (IS_ALIGNED((uintptr_t)to, 4)) {
96 __ioread32_copy(to, from, words);
105 val = __raw_readl(from);
107 for (i = 0; i < len; i++, val >>= 8, to++)
111 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, from += 4) {
112 u32 val = __raw_readl(from);
115 for (j = 0; j < 4 && (j + (i * 4) < len);
116 to++, val >>= 8, j++)
122 static void hisi_sfc_v3xx_write_databuf(struct hisi_sfc_v3xx_host *host,
123 const u8 *from, unsigned int len)
128 to = host->regbase + HISI_SFC_V3XX_CMD_DATABUF0;
130 if (IS_ALIGNED((uintptr_t)from, 4)) {
133 __iowrite32_copy(to, from, words);
142 for (i = 0; i < len; i++, from++)
143 val |= *from << i * 8;
144 __raw_writel(val, to);
148 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, to += 4) {
152 for (j = 0; j < 4 && (j + (i * 4) < len);
154 val |= *from << j * 8;
155 __raw_writel(val, to);
160 static int hisi_sfc_v3xx_generic_exec_op(struct hisi_sfc_v3xx_host *host,
161 const struct spi_mem_op *op,
164 int ret, len = op->data.nbytes;
168 config |= HISI_SFC_V3XX_CMD_CFG_ADDR_EN_MSK;
170 switch (op->data.buswidth) {
174 if (op->addr.buswidth <= 1) {
175 config |= HISI_SFC_V3XX_CMD_CFG_DUAL_IN_DUAL_OUT;
176 } else if (op->addr.buswidth == 2) {
177 if (op->cmd.buswidth <= 1) {
178 config |= HISI_SFC_V3XX_CMD_CFG_DUAL_IO;
179 } else if (op->cmd.buswidth == 2) {
180 config |= HISI_SFC_V3XX_CMD_CFG_FULL_DIO;
189 if (op->addr.buswidth <= 1) {
190 config |= HISI_SFC_V3XX_CMD_CFG_QUAD_IN_QUAD_OUT;
191 } else if (op->addr.buswidth == 4) {
192 if (op->cmd.buswidth <= 1) {
193 config |= HISI_SFC_V3XX_CMD_CFG_QUAD_IO;
194 } else if (op->cmd.buswidth == 4) {
195 config |= HISI_SFC_V3XX_CMD_CFG_FULL_QIO;
207 if (op->data.dir != SPI_MEM_NO_DATA) {
208 config |= (len - 1) << HISI_SFC_V3XX_CMD_CFG_DATA_CNT_OFF;
209 config |= HISI_SFC_V3XX_CMD_CFG_DATA_EN_MSK;
212 if (op->data.dir == SPI_MEM_DATA_OUT)
213 hisi_sfc_v3xx_write_databuf(host, op->data.buf.out, len);
214 else if (op->data.dir == SPI_MEM_DATA_IN)
215 config |= HISI_SFC_V3XX_CMD_CFG_RW_MSK;
217 config |= op->dummy.nbytes << HISI_SFC_V3XX_CMD_CFG_DUMMY_CNT_OFF |
218 chip_select << HISI_SFC_V3XX_CMD_CFG_CS_SEL_OFF |
219 HISI_SFC_V3XX_CMD_CFG_START_MSK;
221 writel(op->addr.val, host->regbase + HISI_SFC_V3XX_CMD_ADDR);
222 writel(op->cmd.opcode, host->regbase + HISI_SFC_V3XX_CMD_INS);
224 writel(config, host->regbase + HISI_SFC_V3XX_CMD_CFG);
226 ret = hisi_sfc_v3xx_wait_cmd_idle(host);
230 if (op->data.dir == SPI_MEM_DATA_IN)
231 hisi_sfc_v3xx_read_databuf(host, op->data.buf.in, len);
236 static int hisi_sfc_v3xx_exec_op(struct spi_mem *mem,
237 const struct spi_mem_op *op)
239 struct hisi_sfc_v3xx_host *host;
240 struct spi_device *spi = mem->spi;
241 u8 chip_select = spi->chip_select;
243 host = spi_controller_get_devdata(spi->master);
245 return hisi_sfc_v3xx_generic_exec_op(host, op, chip_select);
248 static const struct spi_controller_mem_ops hisi_sfc_v3xx_mem_ops = {
249 .adjust_op_size = hisi_sfc_v3xx_adjust_op_size,
250 .exec_op = hisi_sfc_v3xx_exec_op,
253 static int hisi_sfc_v3xx_probe(struct platform_device *pdev)
255 struct device *dev = &pdev->dev;
256 struct hisi_sfc_v3xx_host *host;
257 struct spi_controller *ctlr;
261 ctlr = spi_alloc_master(&pdev->dev, sizeof(*host));
265 ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD |
266 SPI_TX_DUAL | SPI_TX_QUAD;
268 host = spi_controller_get_devdata(ctlr);
271 platform_set_drvdata(pdev, host);
273 host->regbase = devm_platform_ioremap_resource(pdev, 0);
274 if (IS_ERR(host->regbase)) {
275 ret = PTR_ERR(host->regbase);
280 ctlr->num_chipselect = 1;
281 ctlr->mem_ops = &hisi_sfc_v3xx_mem_ops;
283 version = readl(host->regbase + HISI_SFC_V3XX_VERSION);
287 host->max_cmd_dword = 64;
290 host->max_cmd_dword = 16;
294 ret = devm_spi_register_controller(dev, ctlr);
298 dev_info(&pdev->dev, "hw version 0x%x\n", version);
303 spi_master_put(ctlr);
307 #if IS_ENABLED(CONFIG_ACPI)
308 static const struct acpi_device_id hisi_sfc_v3xx_acpi_ids[] = {
312 MODULE_DEVICE_TABLE(acpi, hisi_sfc_v3xx_acpi_ids);
315 static struct platform_driver hisi_sfc_v3xx_spi_driver = {
317 .name = "hisi-sfc-v3xx",
318 .acpi_match_table = ACPI_PTR(hisi_sfc_v3xx_acpi_ids),
320 .probe = hisi_sfc_v3xx_probe,
323 module_platform_driver(hisi_sfc_v3xx_spi_driver);
325 MODULE_LICENSE("GPL");
326 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
327 MODULE_DESCRIPTION("HiSilicon SPI NOR V3XX Flash Controller Driver for hi16xx chipsets");