blob: 6a4626a8bf95b82d7d587eb1932ce7edb2d03680 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Zhou Wang54f531f2015-01-25 18:53:13 +08002/*
3 * Hisilicon NAND Flash controller driver
4 *
5 * Copyright © 2012-2014 HiSilicon Technologies Co., Ltd.
6 * http://www.hisilicon.com
7 *
8 * Author: Zhou Wang <wangzhou.bry@gmail.com>
9 * The initial developer of the original code is Zhiyong Cai
10 * <caizhiyong@huawei.com>
Zhou Wang54f531f2015-01-25 18:53:13 +080011 */
12#include <linux/of.h>
Zhou Wang54f531f2015-01-25 18:53:13 +080013#include <linux/mtd/mtd.h>
14#include <linux/sizes.h>
15#include <linux/clk.h>
16#include <linux/slab.h>
17#include <linux/module.h>
18#include <linux/delay.h>
19#include <linux/interrupt.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020020#include <linux/mtd/rawnand.h>
Zhou Wang54f531f2015-01-25 18:53:13 +080021#include <linux/dma-mapping.h>
22#include <linux/platform_device.h>
23#include <linux/mtd/partitions.h>
24
25#define HINFC504_MAX_CHIP (4)
26#define HINFC504_W_LATCH (5)
27#define HINFC504_R_LATCH (7)
28#define HINFC504_RW_LATCH (3)
29
30#define HINFC504_NFC_TIMEOUT (2 * HZ)
31#define HINFC504_NFC_PM_TIMEOUT (1 * HZ)
32#define HINFC504_NFC_DMA_TIMEOUT (5 * HZ)
33#define HINFC504_CHIP_DELAY (25)
34
35#define HINFC504_REG_BASE_ADDRESS_LEN (0x100)
36#define HINFC504_BUFFER_BASE_ADDRESS_LEN (2048 + 128)
37
38#define HINFC504_ADDR_CYCLE_MASK 0x4
39
40#define HINFC504_CON 0x00
41#define HINFC504_CON_OP_MODE_NORMAL BIT(0)
42#define HINFC504_CON_PAGEISZE_SHIFT (1)
43#define HINFC504_CON_PAGESIZE_MASK (0x07)
44#define HINFC504_CON_BUS_WIDTH BIT(4)
45#define HINFC504_CON_READY_BUSY_SEL BIT(8)
46#define HINFC504_CON_ECCTYPE_SHIFT (9)
47#define HINFC504_CON_ECCTYPE_MASK (0x07)
48
49#define HINFC504_PWIDTH 0x04
50#define SET_HINFC504_PWIDTH(_w_lcnt, _r_lcnt, _rw_hcnt) \
51 ((_w_lcnt) | (((_r_lcnt) & 0x0F) << 4) | (((_rw_hcnt) & 0x0F) << 8))
52
53#define HINFC504_CMD 0x0C
54#define HINFC504_ADDRL 0x10
55#define HINFC504_ADDRH 0x14
56#define HINFC504_DATA_NUM 0x18
57
58#define HINFC504_OP 0x1C
59#define HINFC504_OP_READ_DATA_EN BIT(1)
60#define HINFC504_OP_WAIT_READY_EN BIT(2)
61#define HINFC504_OP_CMD2_EN BIT(3)
62#define HINFC504_OP_WRITE_DATA_EN BIT(4)
63#define HINFC504_OP_ADDR_EN BIT(5)
64#define HINFC504_OP_CMD1_EN BIT(6)
65#define HINFC504_OP_NF_CS_SHIFT (7)
66#define HINFC504_OP_NF_CS_MASK (3)
67#define HINFC504_OP_ADDR_CYCLE_SHIFT (9)
68#define HINFC504_OP_ADDR_CYCLE_MASK (7)
69
70#define HINFC504_STATUS 0x20
71#define HINFC504_READY BIT(0)
72
73#define HINFC504_INTEN 0x24
74#define HINFC504_INTEN_DMA BIT(9)
75#define HINFC504_INTEN_UE BIT(6)
76#define HINFC504_INTEN_CE BIT(5)
77
78#define HINFC504_INTS 0x28
79#define HINFC504_INTS_DMA BIT(9)
80#define HINFC504_INTS_UE BIT(6)
81#define HINFC504_INTS_CE BIT(5)
82
83#define HINFC504_INTCLR 0x2C
84#define HINFC504_INTCLR_DMA BIT(9)
85#define HINFC504_INTCLR_UE BIT(6)
86#define HINFC504_INTCLR_CE BIT(5)
87
88#define HINFC504_ECC_STATUS 0x5C
89#define HINFC504_ECC_16_BIT_SHIFT 12
90
91#define HINFC504_DMA_CTRL 0x60
92#define HINFC504_DMA_CTRL_DMA_START BIT(0)
93#define HINFC504_DMA_CTRL_WE BIT(1)
94#define HINFC504_DMA_CTRL_DATA_AREA_EN BIT(2)
95#define HINFC504_DMA_CTRL_OOB_AREA_EN BIT(3)
96#define HINFC504_DMA_CTRL_BURST4_EN BIT(4)
97#define HINFC504_DMA_CTRL_BURST8_EN BIT(5)
98#define HINFC504_DMA_CTRL_BURST16_EN BIT(6)
99#define HINFC504_DMA_CTRL_ADDR_NUM_SHIFT (7)
100#define HINFC504_DMA_CTRL_ADDR_NUM_MASK (1)
101#define HINFC504_DMA_CTRL_CS_SHIFT (8)
102#define HINFC504_DMA_CTRL_CS_MASK (0x03)
103
104#define HINFC504_DMA_ADDR_DATA 0x64
105#define HINFC504_DMA_ADDR_OOB 0x68
106
107#define HINFC504_DMA_LEN 0x6C
108#define HINFC504_DMA_LEN_OOB_SHIFT (16)
109#define HINFC504_DMA_LEN_OOB_MASK (0xFFF)
110
111#define HINFC504_DMA_PARA 0x70
112#define HINFC504_DMA_PARA_DATA_RW_EN BIT(0)
113#define HINFC504_DMA_PARA_OOB_RW_EN BIT(1)
114#define HINFC504_DMA_PARA_DATA_EDC_EN BIT(2)
115#define HINFC504_DMA_PARA_OOB_EDC_EN BIT(3)
116#define HINFC504_DMA_PARA_DATA_ECC_EN BIT(4)
117#define HINFC504_DMA_PARA_OOB_ECC_EN BIT(5)
118
119#define HINFC_VERSION 0x74
120#define HINFC504_LOG_READ_ADDR 0x7C
121#define HINFC504_LOG_READ_LEN 0x80
122
123#define HINFC504_NANDINFO_LEN 0x10
124
125struct hinfc_host {
126 struct nand_chip chip;
Zhou Wang54f531f2015-01-25 18:53:13 +0800127 struct device *dev;
128 void __iomem *iobase;
129 void __iomem *mmio;
130 struct completion cmd_complete;
131 unsigned int offset;
132 unsigned int command;
133 int chipselect;
134 unsigned int addr_cycle;
135 u32 addr_value[2];
136 u32 cache_addr_value[2];
137 char *buffer;
138 dma_addr_t dma_buffer;
139 dma_addr_t dma_oob;
140 int version;
141 unsigned int irq_status; /* interrupt status */
142};
143
144static inline unsigned int hinfc_read(struct hinfc_host *host, unsigned int reg)
145{
146 return readl(host->iobase + reg);
147}
148
149static inline void hinfc_write(struct hinfc_host *host, unsigned int value,
150 unsigned int reg)
151{
152 writel(value, host->iobase + reg);
153}
154
155static void wait_controller_finished(struct hinfc_host *host)
156{
157 unsigned long timeout = jiffies + HINFC504_NFC_TIMEOUT;
158 int val;
159
160 while (time_before(jiffies, timeout)) {
161 val = hinfc_read(host, HINFC504_STATUS);
162 if (host->command == NAND_CMD_ERASE2) {
163 /* nfc is ready */
164 while (!(val & HINFC504_READY)) {
165 usleep_range(500, 1000);
166 val = hinfc_read(host, HINFC504_STATUS);
167 }
168 return;
169 }
170
171 if (val & HINFC504_READY)
172 return;
173 }
174
175 /* wait cmd timeout */
176 dev_err(host->dev, "Wait NAND controller exec cmd timeout.\n");
177}
178
179static void hisi_nfc_dma_transfer(struct hinfc_host *host, int todev)
180{
Boris BREZILLONfa100162015-12-10 09:00:08 +0100181 struct nand_chip *chip = &host->chip;
182 struct mtd_info *mtd = nand_to_mtd(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800183 unsigned long val;
184 int ret;
185
186 hinfc_write(host, host->dma_buffer, HINFC504_DMA_ADDR_DATA);
187 hinfc_write(host, host->dma_oob, HINFC504_DMA_ADDR_OOB);
188
189 if (chip->ecc.mode == NAND_ECC_NONE) {
190 hinfc_write(host, ((mtd->oobsize & HINFC504_DMA_LEN_OOB_MASK)
191 << HINFC504_DMA_LEN_OOB_SHIFT), HINFC504_DMA_LEN);
192
193 hinfc_write(host, HINFC504_DMA_PARA_DATA_RW_EN
194 | HINFC504_DMA_PARA_OOB_RW_EN, HINFC504_DMA_PARA);
195 } else {
196 if (host->command == NAND_CMD_READOOB)
197 hinfc_write(host, HINFC504_DMA_PARA_OOB_RW_EN
198 | HINFC504_DMA_PARA_OOB_EDC_EN
199 | HINFC504_DMA_PARA_OOB_ECC_EN, HINFC504_DMA_PARA);
200 else
201 hinfc_write(host, HINFC504_DMA_PARA_DATA_RW_EN
202 | HINFC504_DMA_PARA_OOB_RW_EN
203 | HINFC504_DMA_PARA_DATA_EDC_EN
204 | HINFC504_DMA_PARA_OOB_EDC_EN
205 | HINFC504_DMA_PARA_DATA_ECC_EN
206 | HINFC504_DMA_PARA_OOB_ECC_EN, HINFC504_DMA_PARA);
207
208 }
209
210 val = (HINFC504_DMA_CTRL_DMA_START | HINFC504_DMA_CTRL_BURST4_EN
211 | HINFC504_DMA_CTRL_BURST8_EN | HINFC504_DMA_CTRL_BURST16_EN
212 | HINFC504_DMA_CTRL_DATA_AREA_EN | HINFC504_DMA_CTRL_OOB_AREA_EN
213 | ((host->addr_cycle == 4 ? 1 : 0)
214 << HINFC504_DMA_CTRL_ADDR_NUM_SHIFT)
215 | ((host->chipselect & HINFC504_DMA_CTRL_CS_MASK)
216 << HINFC504_DMA_CTRL_CS_SHIFT));
217
218 if (todev)
219 val |= HINFC504_DMA_CTRL_WE;
220
221 init_completion(&host->cmd_complete);
222
223 hinfc_write(host, val, HINFC504_DMA_CTRL);
224 ret = wait_for_completion_timeout(&host->cmd_complete,
225 HINFC504_NFC_DMA_TIMEOUT);
226
227 if (!ret) {
228 dev_err(host->dev, "DMA operation(irq) timeout!\n");
229 /* sanity check */
230 val = hinfc_read(host, HINFC504_DMA_CTRL);
231 if (!(val & HINFC504_DMA_CTRL_DMA_START))
232 dev_err(host->dev, "DMA is already done but without irq ACK!\n");
233 else
234 dev_err(host->dev, "DMA is really timeout!\n");
235 }
236}
237
238static int hisi_nfc_send_cmd_pageprog(struct hinfc_host *host)
239{
240 host->addr_value[0] &= 0xffff0000;
241
242 hinfc_write(host, host->addr_value[0], HINFC504_ADDRL);
243 hinfc_write(host, host->addr_value[1], HINFC504_ADDRH);
244 hinfc_write(host, NAND_CMD_PAGEPROG << 8 | NAND_CMD_SEQIN,
245 HINFC504_CMD);
246
247 hisi_nfc_dma_transfer(host, 1);
248
249 return 0;
250}
251
252static int hisi_nfc_send_cmd_readstart(struct hinfc_host *host)
253{
Boris BREZILLONfa100162015-12-10 09:00:08 +0100254 struct mtd_info *mtd = nand_to_mtd(&host->chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800255
256 if ((host->addr_value[0] == host->cache_addr_value[0]) &&
257 (host->addr_value[1] == host->cache_addr_value[1]))
258 return 0;
259
260 host->addr_value[0] &= 0xffff0000;
261
262 hinfc_write(host, host->addr_value[0], HINFC504_ADDRL);
263 hinfc_write(host, host->addr_value[1], HINFC504_ADDRH);
264 hinfc_write(host, NAND_CMD_READSTART << 8 | NAND_CMD_READ0,
265 HINFC504_CMD);
266
267 hinfc_write(host, 0, HINFC504_LOG_READ_ADDR);
268 hinfc_write(host, mtd->writesize + mtd->oobsize,
269 HINFC504_LOG_READ_LEN);
270
271 hisi_nfc_dma_transfer(host, 0);
272
273 host->cache_addr_value[0] = host->addr_value[0];
274 host->cache_addr_value[1] = host->addr_value[1];
275
276 return 0;
277}
278
279static int hisi_nfc_send_cmd_erase(struct hinfc_host *host)
280{
281 hinfc_write(host, host->addr_value[0], HINFC504_ADDRL);
282 hinfc_write(host, (NAND_CMD_ERASE2 << 8) | NAND_CMD_ERASE1,
283 HINFC504_CMD);
284
285 hinfc_write(host, HINFC504_OP_WAIT_READY_EN
286 | HINFC504_OP_CMD2_EN
287 | HINFC504_OP_CMD1_EN
288 | HINFC504_OP_ADDR_EN
289 | ((host->chipselect & HINFC504_OP_NF_CS_MASK)
290 << HINFC504_OP_NF_CS_SHIFT)
291 | ((host->addr_cycle & HINFC504_OP_ADDR_CYCLE_MASK)
292 << HINFC504_OP_ADDR_CYCLE_SHIFT),
293 HINFC504_OP);
294
295 wait_controller_finished(host);
296
297 return 0;
298}
299
300static int hisi_nfc_send_cmd_readid(struct hinfc_host *host)
301{
302 hinfc_write(host, HINFC504_NANDINFO_LEN, HINFC504_DATA_NUM);
303 hinfc_write(host, NAND_CMD_READID, HINFC504_CMD);
304 hinfc_write(host, 0, HINFC504_ADDRL);
305
306 hinfc_write(host, HINFC504_OP_CMD1_EN | HINFC504_OP_ADDR_EN
307 | HINFC504_OP_READ_DATA_EN
308 | ((host->chipselect & HINFC504_OP_NF_CS_MASK)
309 << HINFC504_OP_NF_CS_SHIFT)
310 | 1 << HINFC504_OP_ADDR_CYCLE_SHIFT, HINFC504_OP);
311
312 wait_controller_finished(host);
313
314 return 0;
315}
316
317static int hisi_nfc_send_cmd_status(struct hinfc_host *host)
318{
319 hinfc_write(host, HINFC504_NANDINFO_LEN, HINFC504_DATA_NUM);
320 hinfc_write(host, NAND_CMD_STATUS, HINFC504_CMD);
321 hinfc_write(host, HINFC504_OP_CMD1_EN
322 | HINFC504_OP_READ_DATA_EN
323 | ((host->chipselect & HINFC504_OP_NF_CS_MASK)
324 << HINFC504_OP_NF_CS_SHIFT),
325 HINFC504_OP);
326
327 wait_controller_finished(host);
328
329 return 0;
330}
331
332static int hisi_nfc_send_cmd_reset(struct hinfc_host *host, int chipselect)
333{
334 hinfc_write(host, NAND_CMD_RESET, HINFC504_CMD);
335
336 hinfc_write(host, HINFC504_OP_CMD1_EN
337 | ((chipselect & HINFC504_OP_NF_CS_MASK)
338 << HINFC504_OP_NF_CS_SHIFT)
339 | HINFC504_OP_WAIT_READY_EN,
340 HINFC504_OP);
341
342 wait_controller_finished(host);
343
344 return 0;
345}
346
Boris Brezillon758b56f2018-09-06 14:05:24 +0200347static void hisi_nfc_select_chip(struct nand_chip *chip, int chipselect)
Zhou Wang54f531f2015-01-25 18:53:13 +0800348{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100349 struct hinfc_host *host = nand_get_controller_data(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800350
351 if (chipselect < 0)
352 return;
353
354 host->chipselect = chipselect;
355}
356
Boris Brezillon7e534322018-09-06 14:05:22 +0200357static uint8_t hisi_nfc_read_byte(struct nand_chip *chip)
Zhou Wang54f531f2015-01-25 18:53:13 +0800358{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100359 struct hinfc_host *host = nand_get_controller_data(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800360
361 if (host->command == NAND_CMD_STATUS)
362 return *(uint8_t *)(host->mmio);
363
364 host->offset++;
365
366 if (host->command == NAND_CMD_READID)
367 return *(uint8_t *)(host->mmio + host->offset - 1);
368
369 return *(uint8_t *)(host->buffer + host->offset - 1);
370}
371
Zhou Wang54f531f2015-01-25 18:53:13 +0800372static void
Boris Brezillonc0739d82018-09-06 14:05:23 +0200373hisi_nfc_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
Zhou Wang54f531f2015-01-25 18:53:13 +0800374{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100375 struct hinfc_host *host = nand_get_controller_data(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800376
377 memcpy(host->buffer + host->offset, buf, len);
378 host->offset += len;
379}
380
Boris Brezillon7e534322018-09-06 14:05:22 +0200381static void hisi_nfc_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
Zhou Wang54f531f2015-01-25 18:53:13 +0800382{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100383 struct hinfc_host *host = nand_get_controller_data(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800384
385 memcpy(buf, host->buffer + host->offset, len);
386 host->offset += len;
387}
388
389static void set_addr(struct mtd_info *mtd, int column, int page_addr)
390{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100391 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100392 struct hinfc_host *host = nand_get_controller_data(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800393 unsigned int command = host->command;
394
395 host->addr_cycle = 0;
396 host->addr_value[0] = 0;
397 host->addr_value[1] = 0;
398
399 /* Serially input address */
400 if (column != -1) {
401 /* Adjust columns for 16 bit buswidth */
402 if (chip->options & NAND_BUSWIDTH_16 &&
403 !nand_opcode_8bits(command))
404 column >>= 1;
405
406 host->addr_value[0] = column & 0xffff;
407 host->addr_cycle = 2;
408 }
409 if (page_addr != -1) {
410 host->addr_value[0] |= (page_addr & 0xffff)
411 << (host->addr_cycle * 8);
412 host->addr_cycle += 2;
Masahiro Yamada14157f82017-09-13 11:05:50 +0900413 if (chip->options & NAND_ROW_ADDR_3) {
Zhou Wang54f531f2015-01-25 18:53:13 +0800414 host->addr_cycle += 1;
415 if (host->command == NAND_CMD_ERASE1)
416 host->addr_value[0] |= ((page_addr >> 16) & 0xff) << 16;
417 else
418 host->addr_value[1] |= ((page_addr >> 16) & 0xff);
419 }
420 }
421}
422
Boris Brezillon5295cf22018-09-06 14:05:28 +0200423static void hisi_nfc_cmdfunc(struct nand_chip *chip, unsigned command,
424 int column, int page_addr)
Zhou Wang54f531f2015-01-25 18:53:13 +0800425{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200426 struct mtd_info *mtd = nand_to_mtd(chip);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100427 struct hinfc_host *host = nand_get_controller_data(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800428 int is_cache_invalid = 1;
429 unsigned int flag = 0;
430
431 host->command = command;
432
433 switch (command) {
434 case NAND_CMD_READ0:
435 case NAND_CMD_READOOB:
436 if (command == NAND_CMD_READ0)
437 host->offset = column;
438 else
439 host->offset = column + mtd->writesize;
440
441 is_cache_invalid = 0;
442 set_addr(mtd, column, page_addr);
443 hisi_nfc_send_cmd_readstart(host);
444 break;
445
446 case NAND_CMD_SEQIN:
447 host->offset = column;
448 set_addr(mtd, column, page_addr);
449 break;
450
451 case NAND_CMD_ERASE1:
452 set_addr(mtd, column, page_addr);
453 break;
454
455 case NAND_CMD_PAGEPROG:
456 hisi_nfc_send_cmd_pageprog(host);
457 break;
458
459 case NAND_CMD_ERASE2:
460 hisi_nfc_send_cmd_erase(host);
461 break;
462
463 case NAND_CMD_READID:
464 host->offset = column;
465 memset(host->mmio, 0, 0x10);
466 hisi_nfc_send_cmd_readid(host);
467 break;
468
469 case NAND_CMD_STATUS:
470 flag = hinfc_read(host, HINFC504_CON);
471 if (chip->ecc.mode == NAND_ECC_HW)
472 hinfc_write(host,
Dan Carpenterdd58d382015-02-11 13:25:09 +0300473 flag & ~(HINFC504_CON_ECCTYPE_MASK <<
Zhou Wang54f531f2015-01-25 18:53:13 +0800474 HINFC504_CON_ECCTYPE_SHIFT), HINFC504_CON);
475
476 host->offset = 0;
477 memset(host->mmio, 0, 0x10);
478 hisi_nfc_send_cmd_status(host);
479 hinfc_write(host, flag, HINFC504_CON);
480 break;
481
482 case NAND_CMD_RESET:
483 hisi_nfc_send_cmd_reset(host, host->chipselect);
484 break;
485
486 default:
487 dev_err(host->dev, "Error: unsupported cmd(cmd=%x, col=%x, page=%x)\n",
488 command, column, page_addr);
489 }
490
491 if (is_cache_invalid) {
492 host->cache_addr_value[0] = ~0;
493 host->cache_addr_value[1] = ~0;
494 }
495}
496
497static irqreturn_t hinfc_irq_handle(int irq, void *devid)
498{
499 struct hinfc_host *host = devid;
500 unsigned int flag;
501
502 flag = hinfc_read(host, HINFC504_INTS);
503 /* store interrupts state */
504 host->irq_status |= flag;
505
506 if (flag & HINFC504_INTS_DMA) {
507 hinfc_write(host, HINFC504_INTCLR_DMA, HINFC504_INTCLR);
508 complete(&host->cmd_complete);
509 } else if (flag & HINFC504_INTS_CE) {
510 hinfc_write(host, HINFC504_INTCLR_CE, HINFC504_INTCLR);
511 } else if (flag & HINFC504_INTS_UE) {
512 hinfc_write(host, HINFC504_INTCLR_UE, HINFC504_INTCLR);
513 }
514
515 return IRQ_HANDLED;
516}
517
Boris Brezillonb9761682018-09-06 14:05:20 +0200518static int hisi_nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
519 int oob_required, int page)
Zhou Wang54f531f2015-01-25 18:53:13 +0800520{
Boris Brezillonb9761682018-09-06 14:05:20 +0200521 struct mtd_info *mtd = nand_to_mtd(chip);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100522 struct hinfc_host *host = nand_get_controller_data(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800523 int max_bitflips = 0, stat = 0, stat_max = 0, status_ecc;
524 int stat_1, stat_2;
525
Boris Brezillon25f815f2017-11-30 18:01:30 +0100526 nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon716bbba2018-09-07 00:38:35 +0200527 chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize);
Zhou Wang54f531f2015-01-25 18:53:13 +0800528
529 /* errors which can not be corrected by ECC */
530 if (host->irq_status & HINFC504_INTS_UE) {
531 mtd->ecc_stats.failed++;
532 } else if (host->irq_status & HINFC504_INTS_CE) {
533 /* TODO: need add other ECC modes! */
534 switch (chip->ecc.strength) {
535 case 16:
536 status_ecc = hinfc_read(host, HINFC504_ECC_STATUS) >>
537 HINFC504_ECC_16_BIT_SHIFT & 0x0fff;
538 stat_2 = status_ecc & 0x3f;
539 stat_1 = status_ecc >> 6 & 0x3f;
540 stat = stat_1 + stat_2;
541 stat_max = max_t(int, stat_1, stat_2);
542 }
543 mtd->ecc_stats.corrected += stat;
544 max_bitflips = max_t(int, max_bitflips, stat_max);
545 }
546 host->irq_status = 0;
547
548 return max_bitflips;
549}
550
Boris Brezillonb9761682018-09-06 14:05:20 +0200551static int hisi_nand_read_oob(struct nand_chip *chip, int page)
Zhou Wang54f531f2015-01-25 18:53:13 +0800552{
Boris Brezillonb9761682018-09-06 14:05:20 +0200553 struct mtd_info *mtd = nand_to_mtd(chip);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100554 struct hinfc_host *host = nand_get_controller_data(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800555
Boris Brezillon97d90da2017-11-30 18:01:29 +0100556 nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Zhou Wang54f531f2015-01-25 18:53:13 +0800557
558 if (host->irq_status & HINFC504_INTS_UE) {
559 host->irq_status = 0;
560 return -EBADMSG;
561 }
562
563 host->irq_status = 0;
564 return 0;
565}
566
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200567static int hisi_nand_write_page_hwecc(struct nand_chip *chip,
568 const uint8_t *buf, int oob_required,
569 int page)
Zhou Wang54f531f2015-01-25 18:53:13 +0800570{
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200571 struct mtd_info *mtd = nand_to_mtd(chip);
572
Boris Brezillon25f815f2017-11-30 18:01:30 +0100573 nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Zhou Wang54f531f2015-01-25 18:53:13 +0800574 if (oob_required)
Boris Brezillon716bbba2018-09-07 00:38:35 +0200575 chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize);
Zhou Wang54f531f2015-01-25 18:53:13 +0800576
Boris Brezillon25f815f2017-11-30 18:01:30 +0100577 return nand_prog_page_end_op(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800578}
579
580static void hisi_nfc_host_init(struct hinfc_host *host)
581{
582 struct nand_chip *chip = &host->chip;
583 unsigned int flag = 0;
584
585 host->version = hinfc_read(host, HINFC_VERSION);
586 host->addr_cycle = 0;
587 host->addr_value[0] = 0;
588 host->addr_value[1] = 0;
589 host->cache_addr_value[0] = ~0;
590 host->cache_addr_value[1] = ~0;
591 host->chipselect = 0;
592
593 /* default page size: 2K, ecc_none. need modify */
594 flag = HINFC504_CON_OP_MODE_NORMAL | HINFC504_CON_READY_BUSY_SEL
595 | ((0x001 & HINFC504_CON_PAGESIZE_MASK)
596 << HINFC504_CON_PAGEISZE_SHIFT)
597 | ((0x0 & HINFC504_CON_ECCTYPE_MASK)
598 << HINFC504_CON_ECCTYPE_SHIFT)
599 | ((chip->options & NAND_BUSWIDTH_16) ?
600 HINFC504_CON_BUS_WIDTH : 0);
601 hinfc_write(host, flag, HINFC504_CON);
602
603 memset(host->mmio, 0xff, HINFC504_BUFFER_BASE_ADDRESS_LEN);
604
605 hinfc_write(host, SET_HINFC504_PWIDTH(HINFC504_W_LATCH,
606 HINFC504_R_LATCH, HINFC504_RW_LATCH), HINFC504_PWIDTH);
607
608 /* enable DMA irq */
609 hinfc_write(host, HINFC504_INTEN_DMA, HINFC504_INTEN);
610}
611
Boris Brezillon2ca9ec92016-02-03 20:02:09 +0100612static int hisi_ooblayout_ecc(struct mtd_info *mtd, int section,
613 struct mtd_oob_region *oobregion)
614{
615 /* FIXME: add ECC bytes position */
616 return -ENOTSUPP;
617}
618
619static int hisi_ooblayout_free(struct mtd_info *mtd, int section,
620 struct mtd_oob_region *oobregion)
621{
622 if (section)
623 return -ERANGE;
624
625 oobregion->offset = 2;
626 oobregion->length = 6;
627
628 return 0;
629}
630
631static const struct mtd_ooblayout_ops hisi_ooblayout_ops = {
632 .ecc = hisi_ooblayout_ecc,
633 .free = hisi_ooblayout_free,
Zhou Wang54f531f2015-01-25 18:53:13 +0800634};
635
636static int hisi_nfc_ecc_probe(struct hinfc_host *host)
637{
638 unsigned int flag;
639 int size, strength, ecc_bits;
640 struct device *dev = host->dev;
641 struct nand_chip *chip = &host->chip;
Boris BREZILLONfa100162015-12-10 09:00:08 +0100642 struct mtd_info *mtd = nand_to_mtd(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800643
Boris Brezillon2d019222016-04-01 14:54:27 +0200644 size = chip->ecc.size;
645 strength = chip->ecc.strength;
Zhou Wang54f531f2015-01-25 18:53:13 +0800646 if (size != 1024) {
647 dev_err(dev, "error ecc size: %d\n", size);
648 return -EINVAL;
649 }
650
651 if ((size == 1024) && ((strength != 8) && (strength != 16) &&
652 (strength != 24) && (strength != 40))) {
653 dev_err(dev, "ecc size and strength do not match\n");
654 return -EINVAL;
655 }
656
657 chip->ecc.size = size;
658 chip->ecc.strength = strength;
659
660 chip->ecc.read_page = hisi_nand_read_page_hwecc;
661 chip->ecc.read_oob = hisi_nand_read_oob;
662 chip->ecc.write_page = hisi_nand_write_page_hwecc;
663
664 switch (chip->ecc.strength) {
665 case 16:
666 ecc_bits = 6;
667 if (mtd->writesize == 2048)
Boris Brezillon2ca9ec92016-02-03 20:02:09 +0100668 mtd_set_ooblayout(mtd, &hisi_ooblayout_ops);
Zhou Wang54f531f2015-01-25 18:53:13 +0800669
670 /* TODO: add more page size support */
671 break;
672
673 /* TODO: add more ecc strength support */
674 default:
675 dev_err(dev, "not support strength: %d\n", chip->ecc.strength);
676 return -EINVAL;
677 }
678
679 flag = hinfc_read(host, HINFC504_CON);
680 /* add ecc type configure */
681 flag |= ((ecc_bits & HINFC504_CON_ECCTYPE_MASK)
682 << HINFC504_CON_ECCTYPE_SHIFT);
683 hinfc_write(host, flag, HINFC504_CON);
684
685 /* enable ecc irq */
686 flag = hinfc_read(host, HINFC504_INTEN) & 0xfff;
687 hinfc_write(host, flag | HINFC504_INTEN_UE | HINFC504_INTEN_CE,
688 HINFC504_INTEN);
689
690 return 0;
691}
692
Miquel Raynal07c1a4b2018-07-20 17:15:01 +0200693static int hisi_nfc_attach_chip(struct nand_chip *chip)
694{
695 struct mtd_info *mtd = nand_to_mtd(chip);
696 struct hinfc_host *host = nand_get_controller_data(chip);
697 int flag;
698
699 host->buffer = dmam_alloc_coherent(host->dev,
700 mtd->writesize + mtd->oobsize,
701 &host->dma_buffer, GFP_KERNEL);
702 if (!host->buffer)
703 return -ENOMEM;
704
705 host->dma_oob = host->dma_buffer + mtd->writesize;
706 memset(host->buffer, 0xff, mtd->writesize + mtd->oobsize);
707
708 flag = hinfc_read(host, HINFC504_CON);
709 flag &= ~(HINFC504_CON_PAGESIZE_MASK << HINFC504_CON_PAGEISZE_SHIFT);
710 switch (mtd->writesize) {
711 case 2048:
712 flag |= (0x001 << HINFC504_CON_PAGEISZE_SHIFT);
713 break;
714 /*
715 * TODO: add more pagesize support,
716 * default pagesize has been set in hisi_nfc_host_init
717 */
718 default:
719 dev_err(host->dev, "NON-2KB page size nand flash\n");
720 return -EINVAL;
721 }
722 hinfc_write(host, flag, HINFC504_CON);
723
724 if (chip->ecc.mode == NAND_ECC_HW)
725 hisi_nfc_ecc_probe(host);
726
727 return 0;
728}
729
730static const struct nand_controller_ops hisi_nfc_controller_ops = {
731 .attach_chip = hisi_nfc_attach_chip,
732};
733
Zhou Wang54f531f2015-01-25 18:53:13 +0800734static int hisi_nfc_probe(struct platform_device *pdev)
735{
Miquel Raynal07c1a4b2018-07-20 17:15:01 +0200736 int ret = 0, irq, max_chips = HINFC504_MAX_CHIP;
Zhou Wang54f531f2015-01-25 18:53:13 +0800737 struct device *dev = &pdev->dev;
738 struct hinfc_host *host;
739 struct nand_chip *chip;
740 struct mtd_info *mtd;
741 struct resource *res;
742 struct device_node *np = dev->of_node;
Zhou Wang54f531f2015-01-25 18:53:13 +0800743
744 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
745 if (!host)
746 return -ENOMEM;
747 host->dev = dev;
748
749 platform_set_drvdata(pdev, host);
750 chip = &host->chip;
Boris BREZILLONfa100162015-12-10 09:00:08 +0100751 mtd = nand_to_mtd(chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800752
753 irq = platform_get_irq(pdev, 0);
754 if (irq < 0) {
755 dev_err(dev, "no IRQ resource defined\n");
Miquel Raynal9326dc72018-04-21 20:00:38 +0200756 return -ENXIO;
Zhou Wang54f531f2015-01-25 18:53:13 +0800757 }
758
759 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
760 host->iobase = devm_ioremap_resource(dev, res);
Miquel Raynal9326dc72018-04-21 20:00:38 +0200761 if (IS_ERR(host->iobase))
762 return PTR_ERR(host->iobase);
Zhou Wang54f531f2015-01-25 18:53:13 +0800763
764 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
765 host->mmio = devm_ioremap_resource(dev, res);
766 if (IS_ERR(host->mmio)) {
Zhou Wang54f531f2015-01-25 18:53:13 +0800767 dev_err(dev, "devm_ioremap_resource[1] fail\n");
Miquel Raynal9326dc72018-04-21 20:00:38 +0200768 return PTR_ERR(host->mmio);
Zhou Wang54f531f2015-01-25 18:53:13 +0800769 }
770
Zhou Wang54f531f2015-01-25 18:53:13 +0800771 mtd->name = "hisi_nand";
772 mtd->dev.parent = &pdev->dev;
773
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100774 nand_set_controller_data(chip, host);
Brian Norrisa61ae812015-10-30 20:33:25 -0700775 nand_set_flash_node(chip, np);
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200776 chip->legacy.cmdfunc = hisi_nfc_cmdfunc;
Boris Brezillon7d6c37e2018-11-11 08:55:22 +0100777 chip->legacy.select_chip = hisi_nfc_select_chip;
Boris Brezillon716bbba2018-09-07 00:38:35 +0200778 chip->legacy.read_byte = hisi_nfc_read_byte;
779 chip->legacy.write_buf = hisi_nfc_write_buf;
780 chip->legacy.read_buf = hisi_nfc_read_buf;
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200781 chip->legacy.chip_delay = HINFC504_CHIP_DELAY;
Boris Brezillon45240362018-09-07 00:38:40 +0200782 chip->legacy.set_features = nand_get_set_features_notsupp;
783 chip->legacy.get_features = nand_get_set_features_notsupp;
Zhou Wang54f531f2015-01-25 18:53:13 +0800784
Zhou Wang54f531f2015-01-25 18:53:13 +0800785 hisi_nfc_host_init(host);
786
Valentin Rothbergd8bf368d2015-03-05 15:23:08 +0100787 ret = devm_request_irq(dev, irq, hinfc_irq_handle, 0x0, "nandc", host);
Zhou Wang54f531f2015-01-25 18:53:13 +0800788 if (ret) {
789 dev_err(dev, "failed to request IRQ\n");
Miquel Raynal9326dc72018-04-21 20:00:38 +0200790 return ret;
Zhou Wang54f531f2015-01-25 18:53:13 +0800791 }
792
Boris Brezillon7b6a9b22018-11-20 10:02:39 +0100793 chip->legacy.dummy_controller.ops = &hisi_nfc_controller_ops;
Boris Brezillon00ad3782018-09-06 14:05:14 +0200794 ret = nand_scan(chip, max_chips);
Masahiro Yamadac8cae352016-11-04 19:43:07 +0900795 if (ret)
Miquel Raynal9326dc72018-04-21 20:00:38 +0200796 return ret;
Zhou Wang54f531f2015-01-25 18:53:13 +0800797
Brian Norrisa61ae812015-10-30 20:33:25 -0700798 ret = mtd_device_register(mtd, NULL, 0);
Zhou Wang54f531f2015-01-25 18:53:13 +0800799 if (ret) {
800 dev_err(dev, "Err MTD partition=%d\n", ret);
Miquel Raynal6f533c42018-04-21 20:00:39 +0200801 nand_cleanup(chip);
Miquel Raynal9326dc72018-04-21 20:00:38 +0200802 return ret;
Zhou Wang54f531f2015-01-25 18:53:13 +0800803 }
804
805 return 0;
Zhou Wang54f531f2015-01-25 18:53:13 +0800806}
807
808static int hisi_nfc_remove(struct platform_device *pdev)
809{
810 struct hinfc_host *host = platform_get_drvdata(pdev);
Zhou Wang54f531f2015-01-25 18:53:13 +0800811
Boris Brezillon59ac2762018-09-06 14:05:15 +0200812 nand_release(&host->chip);
Zhou Wang54f531f2015-01-25 18:53:13 +0800813
814 return 0;
815}
816
817#ifdef CONFIG_PM_SLEEP
818static int hisi_nfc_suspend(struct device *dev)
819{
820 struct hinfc_host *host = dev_get_drvdata(dev);
821 unsigned long timeout = jiffies + HINFC504_NFC_PM_TIMEOUT;
822
823 while (time_before(jiffies, timeout)) {
824 if (((hinfc_read(host, HINFC504_STATUS) & 0x1) == 0x0) &&
825 (hinfc_read(host, HINFC504_DMA_CTRL) &
826 HINFC504_DMA_CTRL_DMA_START)) {
827 cond_resched();
828 return 0;
829 }
830 }
831
832 dev_err(host->dev, "nand controller suspend timeout.\n");
833
834 return -EAGAIN;
835}
836
837static int hisi_nfc_resume(struct device *dev)
838{
839 int cs;
840 struct hinfc_host *host = dev_get_drvdata(dev);
841 struct nand_chip *chip = &host->chip;
842
Boris Brezillon32813e22018-10-29 11:58:29 +0100843 for (cs = 0; cs < nanddev_ntargets(&chip->base); cs++)
Zhou Wang54f531f2015-01-25 18:53:13 +0800844 hisi_nfc_send_cmd_reset(host, cs);
845 hinfc_write(host, SET_HINFC504_PWIDTH(HINFC504_W_LATCH,
846 HINFC504_R_LATCH, HINFC504_RW_LATCH), HINFC504_PWIDTH);
847
848 return 0;
849}
850#endif
851static SIMPLE_DEV_PM_OPS(hisi_nfc_pm_ops, hisi_nfc_suspend, hisi_nfc_resume);
852
853static const struct of_device_id nfc_id_table[] = {
854 { .compatible = "hisilicon,504-nfc" },
855 {}
856};
857MODULE_DEVICE_TABLE(of, nfc_id_table);
858
859static struct platform_driver hisi_nfc_driver = {
860 .driver = {
861 .name = "hisi_nand",
862 .of_match_table = nfc_id_table,
863 .pm = &hisi_nfc_pm_ops,
864 },
865 .probe = hisi_nfc_probe,
866 .remove = hisi_nfc_remove,
867};
868
869module_platform_driver(hisi_nfc_driver);
870
871MODULE_LICENSE("GPL");
872MODULE_AUTHOR("Zhou Wang");
873MODULE_AUTHOR("Zhiyong Cai");
874MODULE_DESCRIPTION("Hisilicon Nand Flash Controller Driver");