spi: HiSilicon v3xx: Properly set CMD_CONFIG for Dual/Quad modes
[linux-2.6-microblaze.git] / drivers / spi / spi-hisi-sfc-v3xx.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // HiSilicon SPI NOR V3XX Flash Controller Driver for hi16xx chipsets
4 //
5 // Copyright (c) 2019 HiSilicon Technologies Co., Ltd.
6 // Author: John Garry <john.garry@huawei.com>
7
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>
16
17 #define HISI_SFC_V3XX_VERSION (0x1f8)
18
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)
36
37 struct hisi_sfc_v3xx_host {
38         struct device *dev;
39         void __iomem *regbase;
40         int max_cmd_dword;
41 };
42
43 #define HISI_SFC_V3XX_WAIT_TIMEOUT_US           1000000
44 #define HISI_SFC_V3XX_WAIT_POLL_INTERVAL_US     10
45
46 static int hisi_sfc_v3xx_wait_cmd_idle(struct hisi_sfc_v3xx_host *host)
47 {
48         u32 reg;
49
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);
54 }
55
56 static int hisi_sfc_v3xx_adjust_op_size(struct spi_mem *mem,
57                                         struct spi_mem_op *op)
58 {
59         struct spi_device *spi = mem->spi;
60         struct hisi_sfc_v3xx_host *host;
61         uintptr_t addr = (uintptr_t)op->data.buf.in;
62         int max_byte_count;
63
64         host = spi_controller_get_devdata(spi->master);
65
66         max_byte_count = host->max_cmd_dword * 4;
67
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;
72
73         return 0;
74 }
75
76 /*
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.
81  *
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().
84  */
85 static void hisi_sfc_v3xx_read_databuf(struct hisi_sfc_v3xx_host *host,
86                                        u8 *to, unsigned int len)
87 {
88         void __iomem *from;
89         int i;
90
91         from = host->regbase + HISI_SFC_V3XX_CMD_DATABUF0;
92
93         if (IS_ALIGNED((uintptr_t)to, 4)) {
94                 int words = len / 4;
95
96                 __ioread32_copy(to, from, words);
97
98                 len -= words * 4;
99                 if (len) {
100                         u32 val;
101
102                         to += words * 4;
103                         from += words * 4;
104
105                         val = __raw_readl(from);
106
107                         for (i = 0; i < len; i++, val >>= 8, to++)
108                                 *to = (u8)val;
109                 }
110         } else {
111                 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, from += 4) {
112                         u32 val = __raw_readl(from);
113                         int j;
114
115                         for (j = 0; j < 4 && (j + (i * 4) < len);
116                              to++, val >>= 8, j++)
117                                 *to = (u8)val;
118                 }
119         }
120 }
121
122 static void hisi_sfc_v3xx_write_databuf(struct hisi_sfc_v3xx_host *host,
123                                         const u8 *from, unsigned int len)
124 {
125         void __iomem *to;
126         int i;
127
128         to = host->regbase + HISI_SFC_V3XX_CMD_DATABUF0;
129
130         if (IS_ALIGNED((uintptr_t)from, 4)) {
131                 int words = len / 4;
132
133                 __iowrite32_copy(to, from, words);
134
135                 len -= words * 4;
136                 if (len) {
137                         u32 val = 0;
138
139                         to += words * 4;
140                         from += words * 4;
141
142                         for (i = 0; i < len; i++, from++)
143                                 val |= *from << i * 8;
144                         __raw_writel(val, to);
145                 }
146
147         } else {
148                 for (i = 0; i < DIV_ROUND_UP(len, 4); i++, to += 4) {
149                         u32 val = 0;
150                         int j;
151
152                         for (j = 0; j < 4 && (j + (i * 4) < len);
153                              from++, j++)
154                                 val |= *from << j * 8;
155                         __raw_writel(val, to);
156                 }
157         }
158 }
159
160 static int hisi_sfc_v3xx_generic_exec_op(struct hisi_sfc_v3xx_host *host,
161                                          const struct spi_mem_op *op,
162                                          u8 chip_select)
163 {
164         int ret, len = op->data.nbytes;
165         u32 config = 0;
166
167         if (op->addr.nbytes)
168                 config |= HISI_SFC_V3XX_CMD_CFG_ADDR_EN_MSK;
169
170         switch (op->data.buswidth) {
171         case 0 ... 1:
172                 break;
173         case 2:
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;
181                         } else {
182                                 return -EIO;
183                         }
184                 } else {
185                         return -EIO;
186                 }
187                 break;
188         case 4:
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;
196                         } else {
197                                 return -EIO;
198                         }
199                 } else {
200                         return -EIO;
201                 }
202                 break;
203         default:
204                 return -EOPNOTSUPP;
205         }
206
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;
210         }
211
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;
216
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;
220
221         writel(op->addr.val, host->regbase + HISI_SFC_V3XX_CMD_ADDR);
222         writel(op->cmd.opcode, host->regbase + HISI_SFC_V3XX_CMD_INS);
223
224         writel(config, host->regbase + HISI_SFC_V3XX_CMD_CFG);
225
226         ret = hisi_sfc_v3xx_wait_cmd_idle(host);
227         if (ret)
228                 return ret;
229
230         if (op->data.dir == SPI_MEM_DATA_IN)
231                 hisi_sfc_v3xx_read_databuf(host, op->data.buf.in, len);
232
233         return 0;
234 }
235
236 static int hisi_sfc_v3xx_exec_op(struct spi_mem *mem,
237                                  const struct spi_mem_op *op)
238 {
239         struct hisi_sfc_v3xx_host *host;
240         struct spi_device *spi = mem->spi;
241         u8 chip_select = spi->chip_select;
242
243         host = spi_controller_get_devdata(spi->master);
244
245         return hisi_sfc_v3xx_generic_exec_op(host, op, chip_select);
246 }
247
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,
251 };
252
253 static int hisi_sfc_v3xx_probe(struct platform_device *pdev)
254 {
255         struct device *dev = &pdev->dev;
256         struct hisi_sfc_v3xx_host *host;
257         struct spi_controller *ctlr;
258         u32 version;
259         int ret;
260
261         ctlr = spi_alloc_master(&pdev->dev, sizeof(*host));
262         if (!ctlr)
263                 return -ENOMEM;
264
265         ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD |
266                           SPI_TX_DUAL | SPI_TX_QUAD;
267
268         host = spi_controller_get_devdata(ctlr);
269         host->dev = dev;
270
271         platform_set_drvdata(pdev, host);
272
273         host->regbase = devm_platform_ioremap_resource(pdev, 0);
274         if (IS_ERR(host->regbase)) {
275                 ret = PTR_ERR(host->regbase);
276                 goto err_put_master;
277         }
278
279         ctlr->bus_num = -1;
280         ctlr->num_chipselect = 1;
281         ctlr->mem_ops = &hisi_sfc_v3xx_mem_ops;
282
283         version = readl(host->regbase + HISI_SFC_V3XX_VERSION);
284
285         switch (version) {
286         case 0x351:
287                 host->max_cmd_dword = 64;
288                 break;
289         default:
290                 host->max_cmd_dword = 16;
291                 break;
292         }
293
294         ret = devm_spi_register_controller(dev, ctlr);
295         if (ret)
296                 goto err_put_master;
297
298         dev_info(&pdev->dev, "hw version 0x%x\n", version);
299
300         return 0;
301
302 err_put_master:
303         spi_master_put(ctlr);
304         return ret;
305 }
306
307 #if IS_ENABLED(CONFIG_ACPI)
308 static const struct acpi_device_id hisi_sfc_v3xx_acpi_ids[] = {
309         {"HISI0341", 0},
310         {}
311 };
312 MODULE_DEVICE_TABLE(acpi, hisi_sfc_v3xx_acpi_ids);
313 #endif
314
315 static struct platform_driver hisi_sfc_v3xx_spi_driver = {
316         .driver = {
317                 .name   = "hisi-sfc-v3xx",
318                 .acpi_match_table = ACPI_PTR(hisi_sfc_v3xx_acpi_ids),
319         },
320         .probe  = hisi_sfc_v3xx_probe,
321 };
322
323 module_platform_driver(hisi_sfc_v3xx_spi_driver);
324
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");