]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/mtd/spi-nor/mtk-quadspi.c
scsi: qedi: Fix return code in qedi_ep_connect()
[karo-tx-linux.git] / drivers / mtd / spi-nor / mtk-quadspi.c
1 /*
2  * Copyright (c) 2015 MediaTek Inc.
3  * Author: Bayi Cheng <bayi.cheng@mediatek.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/init.h>
19 #include <linux/io.h>
20 #include <linux/iopoll.h>
21 #include <linux/ioport.h>
22 #include <linux/math64.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/partitions.h>
32 #include <linux/mtd/spi-nor.h>
33
34 #define MTK_NOR_CMD_REG                 0x00
35 #define MTK_NOR_CNT_REG                 0x04
36 #define MTK_NOR_RDSR_REG                0x08
37 #define MTK_NOR_RDATA_REG               0x0c
38 #define MTK_NOR_RADR0_REG               0x10
39 #define MTK_NOR_RADR1_REG               0x14
40 #define MTK_NOR_RADR2_REG               0x18
41 #define MTK_NOR_WDATA_REG               0x1c
42 #define MTK_NOR_PRGDATA0_REG            0x20
43 #define MTK_NOR_PRGDATA1_REG            0x24
44 #define MTK_NOR_PRGDATA2_REG            0x28
45 #define MTK_NOR_PRGDATA3_REG            0x2c
46 #define MTK_NOR_PRGDATA4_REG            0x30
47 #define MTK_NOR_PRGDATA5_REG            0x34
48 #define MTK_NOR_SHREG0_REG              0x38
49 #define MTK_NOR_SHREG1_REG              0x3c
50 #define MTK_NOR_SHREG2_REG              0x40
51 #define MTK_NOR_SHREG3_REG              0x44
52 #define MTK_NOR_SHREG4_REG              0x48
53 #define MTK_NOR_SHREG5_REG              0x4c
54 #define MTK_NOR_SHREG6_REG              0x50
55 #define MTK_NOR_SHREG7_REG              0x54
56 #define MTK_NOR_SHREG8_REG              0x58
57 #define MTK_NOR_SHREG9_REG              0x5c
58 #define MTK_NOR_CFG1_REG                0x60
59 #define MTK_NOR_CFG2_REG                0x64
60 #define MTK_NOR_CFG3_REG                0x68
61 #define MTK_NOR_STATUS0_REG             0x70
62 #define MTK_NOR_STATUS1_REG             0x74
63 #define MTK_NOR_STATUS2_REG             0x78
64 #define MTK_NOR_STATUS3_REG             0x7c
65 #define MTK_NOR_FLHCFG_REG              0x84
66 #define MTK_NOR_TIME_REG                0x94
67 #define MTK_NOR_PP_DATA_REG             0x98
68 #define MTK_NOR_PREBUF_STUS_REG         0x9c
69 #define MTK_NOR_DELSEL0_REG             0xa0
70 #define MTK_NOR_DELSEL1_REG             0xa4
71 #define MTK_NOR_INTRSTUS_REG            0xa8
72 #define MTK_NOR_INTREN_REG              0xac
73 #define MTK_NOR_CHKSUM_CTL_REG          0xb8
74 #define MTK_NOR_CHKSUM_REG              0xbc
75 #define MTK_NOR_CMD2_REG                0xc0
76 #define MTK_NOR_WRPROT_REG              0xc4
77 #define MTK_NOR_RADR3_REG               0xc8
78 #define MTK_NOR_DUAL_REG                0xcc
79 #define MTK_NOR_DELSEL2_REG             0xd0
80 #define MTK_NOR_DELSEL3_REG             0xd4
81 #define MTK_NOR_DELSEL4_REG             0xd8
82
83 /* commands for mtk nor controller */
84 #define MTK_NOR_READ_CMD                0x0
85 #define MTK_NOR_RDSR_CMD                0x2
86 #define MTK_NOR_PRG_CMD                 0x4
87 #define MTK_NOR_WR_CMD                  0x10
88 #define MTK_NOR_PIO_WR_CMD              0x90
89 #define MTK_NOR_WRSR_CMD                0x20
90 #define MTK_NOR_PIO_READ_CMD            0x81
91 #define MTK_NOR_WR_BUF_ENABLE           0x1
92 #define MTK_NOR_WR_BUF_DISABLE          0x0
93 #define MTK_NOR_ENABLE_SF_CMD           0x30
94 #define MTK_NOR_DUAD_ADDR_EN            0x8
95 #define MTK_NOR_QUAD_READ_EN            0x4
96 #define MTK_NOR_DUAL_ADDR_EN            0x2
97 #define MTK_NOR_DUAL_READ_EN            0x1
98 #define MTK_NOR_DUAL_DISABLE            0x0
99 #define MTK_NOR_FAST_READ               0x1
100
101 #define SFLASH_WRBUF_SIZE               128
102
103 /* Can shift up to 48 bits (6 bytes) of TX/RX */
104 #define MTK_NOR_MAX_RX_TX_SHIFT         6
105 /* can shift up to 56 bits (7 bytes) transfer by MTK_NOR_PRG_CMD */
106 #define MTK_NOR_MAX_SHIFT               7
107 /* nor controller 4-byte address mode enable bit */
108 #define MTK_NOR_4B_ADDR_EN              BIT(4)
109
110 /* Helpers for accessing the program data / shift data registers */
111 #define MTK_NOR_PRG_REG(n)              (MTK_NOR_PRGDATA0_REG + 4 * (n))
112 #define MTK_NOR_SHREG(n)                (MTK_NOR_SHREG0_REG + 4 * (n))
113
114 struct mt8173_nor {
115         struct spi_nor nor;
116         struct device *dev;
117         void __iomem *base;     /* nor flash base address */
118         struct clk *spi_clk;
119         struct clk *nor_clk;
120 };
121
122 static void mt8173_nor_set_read_mode(struct mt8173_nor *mt8173_nor)
123 {
124         struct spi_nor *nor = &mt8173_nor->nor;
125
126         switch (nor->flash_read) {
127         case SPI_NOR_FAST:
128                 writeb(nor->read_opcode, mt8173_nor->base +
129                        MTK_NOR_PRGDATA3_REG);
130                 writeb(MTK_NOR_FAST_READ, mt8173_nor->base +
131                        MTK_NOR_CFG1_REG);
132                 break;
133         case SPI_NOR_DUAL:
134                 writeb(nor->read_opcode, mt8173_nor->base +
135                        MTK_NOR_PRGDATA3_REG);
136                 writeb(MTK_NOR_DUAL_READ_EN, mt8173_nor->base +
137                        MTK_NOR_DUAL_REG);
138                 break;
139         case SPI_NOR_QUAD:
140                 writeb(nor->read_opcode, mt8173_nor->base +
141                        MTK_NOR_PRGDATA4_REG);
142                 writeb(MTK_NOR_QUAD_READ_EN, mt8173_nor->base +
143                        MTK_NOR_DUAL_REG);
144                 break;
145         default:
146                 writeb(MTK_NOR_DUAL_DISABLE, mt8173_nor->base +
147                        MTK_NOR_DUAL_REG);
148                 break;
149         }
150 }
151
152 static int mt8173_nor_execute_cmd(struct mt8173_nor *mt8173_nor, u8 cmdval)
153 {
154         int reg;
155         u8 val = cmdval & 0x1f;
156
157         writeb(cmdval, mt8173_nor->base + MTK_NOR_CMD_REG);
158         return readl_poll_timeout(mt8173_nor->base + MTK_NOR_CMD_REG, reg,
159                                   !(reg & val), 100, 10000);
160 }
161
162 static int mt8173_nor_do_tx_rx(struct mt8173_nor *mt8173_nor, u8 op,
163                                u8 *tx, int txlen, u8 *rx, int rxlen)
164 {
165         int len = 1 + txlen + rxlen;
166         int i, ret, idx;
167
168         if (len > MTK_NOR_MAX_SHIFT)
169                 return -EINVAL;
170
171         writeb(len * 8, mt8173_nor->base + MTK_NOR_CNT_REG);
172
173         /* start at PRGDATA5, go down to PRGDATA0 */
174         idx = MTK_NOR_MAX_RX_TX_SHIFT - 1;
175
176         /* opcode */
177         writeb(op, mt8173_nor->base + MTK_NOR_PRG_REG(idx));
178         idx--;
179
180         /* program TX data */
181         for (i = 0; i < txlen; i++, idx--)
182                 writeb(tx[i], mt8173_nor->base + MTK_NOR_PRG_REG(idx));
183
184         /* clear out rest of TX registers */
185         while (idx >= 0) {
186                 writeb(0, mt8173_nor->base + MTK_NOR_PRG_REG(idx));
187                 idx--;
188         }
189
190         ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PRG_CMD);
191         if (ret)
192                 return ret;
193
194         /* restart at first RX byte */
195         idx = rxlen - 1;
196
197         /* read out RX data */
198         for (i = 0; i < rxlen; i++, idx--)
199                 rx[i] = readb(mt8173_nor->base + MTK_NOR_SHREG(idx));
200
201         return 0;
202 }
203
204 /* Do a WRSR (Write Status Register) command */
205 static int mt8173_nor_wr_sr(struct mt8173_nor *mt8173_nor, u8 sr)
206 {
207         writeb(sr, mt8173_nor->base + MTK_NOR_PRGDATA5_REG);
208         writeb(8, mt8173_nor->base + MTK_NOR_CNT_REG);
209         return mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_WRSR_CMD);
210 }
211
212 static int mt8173_nor_write_buffer_enable(struct mt8173_nor *mt8173_nor)
213 {
214         u8 reg;
215
216         /* the bit0 of MTK_NOR_CFG2_REG is pre-fetch buffer
217          * 0: pre-fetch buffer use for read
218          * 1: pre-fetch buffer use for page program
219          */
220         writel(MTK_NOR_WR_BUF_ENABLE, mt8173_nor->base + MTK_NOR_CFG2_REG);
221         return readb_poll_timeout(mt8173_nor->base + MTK_NOR_CFG2_REG, reg,
222                                   0x01 == (reg & 0x01), 100, 10000);
223 }
224
225 static int mt8173_nor_write_buffer_disable(struct mt8173_nor *mt8173_nor)
226 {
227         u8 reg;
228
229         writel(MTK_NOR_WR_BUF_DISABLE, mt8173_nor->base + MTK_NOR_CFG2_REG);
230         return readb_poll_timeout(mt8173_nor->base + MTK_NOR_CFG2_REG, reg,
231                                   MTK_NOR_WR_BUF_DISABLE == (reg & 0x1), 100,
232                                   10000);
233 }
234
235 static void mt8173_nor_set_addr_width(struct mt8173_nor *mt8173_nor)
236 {
237         u8 val;
238         struct spi_nor *nor = &mt8173_nor->nor;
239
240         val = readb(mt8173_nor->base + MTK_NOR_DUAL_REG);
241
242         switch (nor->addr_width) {
243         case 3:
244                 val &= ~MTK_NOR_4B_ADDR_EN;
245                 break;
246         case 4:
247                 val |= MTK_NOR_4B_ADDR_EN;
248                 break;
249         default:
250                 dev_warn(mt8173_nor->dev, "Unexpected address width %u.\n",
251                          nor->addr_width);
252                 break;
253         }
254
255         writeb(val, mt8173_nor->base + MTK_NOR_DUAL_REG);
256 }
257
258 static void mt8173_nor_set_addr(struct mt8173_nor *mt8173_nor, u32 addr)
259 {
260         int i;
261
262         mt8173_nor_set_addr_width(mt8173_nor);
263
264         for (i = 0; i < 3; i++) {
265                 writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR0_REG + i * 4);
266                 addr >>= 8;
267         }
268         /* Last register is non-contiguous */
269         writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR3_REG);
270 }
271
272 static ssize_t mt8173_nor_read(struct spi_nor *nor, loff_t from, size_t length,
273                                u_char *buffer)
274 {
275         int i, ret;
276         int addr = (int)from;
277         u8 *buf = (u8 *)buffer;
278         struct mt8173_nor *mt8173_nor = nor->priv;
279
280         /* set mode for fast read mode ,dual mode or quad mode */
281         mt8173_nor_set_read_mode(mt8173_nor);
282         mt8173_nor_set_addr(mt8173_nor, addr);
283
284         for (i = 0; i < length; i++) {
285                 ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_READ_CMD);
286                 if (ret < 0)
287                         return ret;
288                 buf[i] = readb(mt8173_nor->base + MTK_NOR_RDATA_REG);
289         }
290         return length;
291 }
292
293 static int mt8173_nor_write_single_byte(struct mt8173_nor *mt8173_nor,
294                                         int addr, int length, u8 *data)
295 {
296         int i, ret;
297
298         mt8173_nor_set_addr(mt8173_nor, addr);
299
300         for (i = 0; i < length; i++) {
301                 writeb(*data++, mt8173_nor->base + MTK_NOR_WDATA_REG);
302                 ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_WR_CMD);
303                 if (ret < 0)
304                         return ret;
305         }
306         return 0;
307 }
308
309 static int mt8173_nor_write_buffer(struct mt8173_nor *mt8173_nor, int addr,
310                                    const u8 *buf)
311 {
312         int i, bufidx, data;
313
314         mt8173_nor_set_addr(mt8173_nor, addr);
315
316         bufidx = 0;
317         for (i = 0; i < SFLASH_WRBUF_SIZE; i += 4) {
318                 data = buf[bufidx + 3]<<24 | buf[bufidx + 2]<<16 |
319                        buf[bufidx + 1]<<8 | buf[bufidx];
320                 bufidx += 4;
321                 writel(data, mt8173_nor->base + MTK_NOR_PP_DATA_REG);
322         }
323         return mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_WR_CMD);
324 }
325
326 static ssize_t mt8173_nor_write(struct spi_nor *nor, loff_t to, size_t len,
327                                 const u_char *buf)
328 {
329         int ret;
330         struct mt8173_nor *mt8173_nor = nor->priv;
331         size_t i;
332
333         ret = mt8173_nor_write_buffer_enable(mt8173_nor);
334         if (ret < 0) {
335                 dev_warn(mt8173_nor->dev, "write buffer enable failed!\n");
336                 return ret;
337         }
338
339         for (i = 0; i + SFLASH_WRBUF_SIZE <= len; i += SFLASH_WRBUF_SIZE) {
340                 ret = mt8173_nor_write_buffer(mt8173_nor, to, buf);
341                 if (ret < 0) {
342                         dev_err(mt8173_nor->dev, "write buffer failed!\n");
343                         return ret;
344                 }
345                 to += SFLASH_WRBUF_SIZE;
346                 buf += SFLASH_WRBUF_SIZE;
347         }
348         ret = mt8173_nor_write_buffer_disable(mt8173_nor);
349         if (ret < 0) {
350                 dev_warn(mt8173_nor->dev, "write buffer disable failed!\n");
351                 return ret;
352         }
353
354         if (i < len) {
355                 ret = mt8173_nor_write_single_byte(mt8173_nor, to,
356                                                    (int)(len - i), (u8 *)buf);
357                 if (ret < 0) {
358                         dev_err(mt8173_nor->dev, "write single byte failed!\n");
359                         return ret;
360                 }
361         }
362
363         return len;
364 }
365
366 static int mt8173_nor_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
367 {
368         int ret;
369         struct mt8173_nor *mt8173_nor = nor->priv;
370
371         switch (opcode) {
372         case SPINOR_OP_RDSR:
373                 ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_RDSR_CMD);
374                 if (ret < 0)
375                         return ret;
376                 if (len == 1)
377                         *buf = readb(mt8173_nor->base + MTK_NOR_RDSR_REG);
378                 else
379                         dev_err(mt8173_nor->dev, "len should be 1 for read status!\n");
380                 break;
381         default:
382                 ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, NULL, 0, buf, len);
383                 break;
384         }
385         return ret;
386 }
387
388 static int mt8173_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
389                                 int len)
390 {
391         int ret;
392         struct mt8173_nor *mt8173_nor = nor->priv;
393
394         switch (opcode) {
395         case SPINOR_OP_WRSR:
396                 /* We only handle 1 byte */
397                 ret = mt8173_nor_wr_sr(mt8173_nor, *buf);
398                 break;
399         default:
400                 ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, buf, len, NULL, 0);
401                 if (ret)
402                         dev_warn(mt8173_nor->dev, "write reg failure!\n");
403                 break;
404         }
405         return ret;
406 }
407
408 static int mtk_nor_init(struct mt8173_nor *mt8173_nor,
409                         struct device_node *flash_node)
410 {
411         int ret;
412         struct spi_nor *nor;
413
414         /* initialize controller to accept commands */
415         writel(MTK_NOR_ENABLE_SF_CMD, mt8173_nor->base + MTK_NOR_WRPROT_REG);
416
417         nor = &mt8173_nor->nor;
418         nor->dev = mt8173_nor->dev;
419         nor->priv = mt8173_nor;
420         spi_nor_set_flash_node(nor, flash_node);
421
422         /* fill the hooks to spi nor */
423         nor->read = mt8173_nor_read;
424         nor->read_reg = mt8173_nor_read_reg;
425         nor->write = mt8173_nor_write;
426         nor->write_reg = mt8173_nor_write_reg;
427         nor->mtd.name = "mtk_nor";
428         /* initialized with NULL */
429         ret = spi_nor_scan(nor, NULL, SPI_NOR_DUAL);
430         if (ret)
431                 return ret;
432
433         return mtd_device_register(&nor->mtd, NULL, 0);
434 }
435
436 static int mtk_nor_drv_probe(struct platform_device *pdev)
437 {
438         struct device_node *flash_np;
439         struct resource *res;
440         int ret;
441         struct mt8173_nor *mt8173_nor;
442
443         if (!pdev->dev.of_node) {
444                 dev_err(&pdev->dev, "No DT found\n");
445                 return -EINVAL;
446         }
447
448         mt8173_nor = devm_kzalloc(&pdev->dev, sizeof(*mt8173_nor), GFP_KERNEL);
449         if (!mt8173_nor)
450                 return -ENOMEM;
451         platform_set_drvdata(pdev, mt8173_nor);
452
453         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
454         mt8173_nor->base = devm_ioremap_resource(&pdev->dev, res);
455         if (IS_ERR(mt8173_nor->base))
456                 return PTR_ERR(mt8173_nor->base);
457
458         mt8173_nor->spi_clk = devm_clk_get(&pdev->dev, "spi");
459         if (IS_ERR(mt8173_nor->spi_clk))
460                 return PTR_ERR(mt8173_nor->spi_clk);
461
462         mt8173_nor->nor_clk = devm_clk_get(&pdev->dev, "sf");
463         if (IS_ERR(mt8173_nor->nor_clk))
464                 return PTR_ERR(mt8173_nor->nor_clk);
465
466         mt8173_nor->dev = &pdev->dev;
467         ret = clk_prepare_enable(mt8173_nor->spi_clk);
468         if (ret)
469                 return ret;
470
471         ret = clk_prepare_enable(mt8173_nor->nor_clk);
472         if (ret) {
473                 clk_disable_unprepare(mt8173_nor->spi_clk);
474                 return ret;
475         }
476         /* only support one attached flash */
477         flash_np = of_get_next_available_child(pdev->dev.of_node, NULL);
478         if (!flash_np) {
479                 dev_err(&pdev->dev, "no SPI flash device to configure\n");
480                 ret = -ENODEV;
481                 goto nor_free;
482         }
483         ret = mtk_nor_init(mt8173_nor, flash_np);
484
485 nor_free:
486         if (ret) {
487                 clk_disable_unprepare(mt8173_nor->spi_clk);
488                 clk_disable_unprepare(mt8173_nor->nor_clk);
489         }
490         return ret;
491 }
492
493 static int mtk_nor_drv_remove(struct platform_device *pdev)
494 {
495         struct mt8173_nor *mt8173_nor = platform_get_drvdata(pdev);
496
497         clk_disable_unprepare(mt8173_nor->spi_clk);
498         clk_disable_unprepare(mt8173_nor->nor_clk);
499         return 0;
500 }
501
502 static const struct of_device_id mtk_nor_of_ids[] = {
503         { .compatible = "mediatek,mt8173-nor"},
504         { /* sentinel */ }
505 };
506 MODULE_DEVICE_TABLE(of, mtk_nor_of_ids);
507
508 static struct platform_driver mtk_nor_driver = {
509         .probe = mtk_nor_drv_probe,
510         .remove = mtk_nor_drv_remove,
511         .driver = {
512                 .name = "mtk-nor",
513                 .of_match_table = mtk_nor_of_ids,
514         },
515 };
516
517 module_platform_driver(mtk_nor_driver);
518 MODULE_LICENSE("GPL v2");
519 MODULE_DESCRIPTION("MediaTek SPI NOR Flash Driver");