blob: 0e727d79f2ce13868d9907161bf3f7531e29d0bb [file] [log] [blame]
Archit Tanejac76b78d2016-02-03 14:29:50 +05301/*
2 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/clk.h>
15#include <linux/slab.h>
16#include <linux/bitops.h>
17#include <linux/dma-mapping.h>
18#include <linux/dmaengine.h>
19#include <linux/module.h>
20#include <linux/mtd/nand.h>
21#include <linux/mtd/partitions.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
Archit Tanejac76b78d2016-02-03 14:29:50 +053024#include <linux/delay.h>
25
26/* NANDc reg offsets */
27#define NAND_FLASH_CMD 0x00
28#define NAND_ADDR0 0x04
29#define NAND_ADDR1 0x08
30#define NAND_FLASH_CHIP_SELECT 0x0c
31#define NAND_EXEC_CMD 0x10
32#define NAND_FLASH_STATUS 0x14
33#define NAND_BUFFER_STATUS 0x18
34#define NAND_DEV0_CFG0 0x20
35#define NAND_DEV0_CFG1 0x24
36#define NAND_DEV0_ECC_CFG 0x28
37#define NAND_DEV1_ECC_CFG 0x2c
38#define NAND_DEV1_CFG0 0x30
39#define NAND_DEV1_CFG1 0x34
40#define NAND_READ_ID 0x40
41#define NAND_READ_STATUS 0x44
42#define NAND_DEV_CMD0 0xa0
43#define NAND_DEV_CMD1 0xa4
44#define NAND_DEV_CMD2 0xa8
45#define NAND_DEV_CMD_VLD 0xac
46#define SFLASHC_BURST_CFG 0xe0
47#define NAND_ERASED_CW_DETECT_CFG 0xe8
48#define NAND_ERASED_CW_DETECT_STATUS 0xec
49#define NAND_EBI2_ECC_BUF_CFG 0xf0
50#define FLASH_BUF_ACC 0x100
51
52#define NAND_CTRL 0xf00
53#define NAND_VERSION 0xf08
54#define NAND_READ_LOCATION_0 0xf20
55#define NAND_READ_LOCATION_1 0xf24
56
57/* dummy register offsets, used by write_reg_dma */
58#define NAND_DEV_CMD1_RESTORE 0xdead
59#define NAND_DEV_CMD_VLD_RESTORE 0xbeef
60
61/* NAND_FLASH_CMD bits */
62#define PAGE_ACC BIT(4)
63#define LAST_PAGE BIT(5)
64
65/* NAND_FLASH_CHIP_SELECT bits */
66#define NAND_DEV_SEL 0
67#define DM_EN BIT(2)
68
69/* NAND_FLASH_STATUS bits */
70#define FS_OP_ERR BIT(4)
71#define FS_READY_BSY_N BIT(5)
72#define FS_MPU_ERR BIT(8)
73#define FS_DEVICE_STS_ERR BIT(16)
74#define FS_DEVICE_WP BIT(23)
75
76/* NAND_BUFFER_STATUS bits */
77#define BS_UNCORRECTABLE_BIT BIT(8)
78#define BS_CORRECTABLE_ERR_MSK 0x1f
79
80/* NAND_DEVn_CFG0 bits */
81#define DISABLE_STATUS_AFTER_WRITE 4
82#define CW_PER_PAGE 6
83#define UD_SIZE_BYTES 9
84#define ECC_PARITY_SIZE_BYTES_RS 19
85#define SPARE_SIZE_BYTES 23
86#define NUM_ADDR_CYCLES 27
87#define STATUS_BFR_READ 30
88#define SET_RD_MODE_AFTER_STATUS 31
89
90/* NAND_DEVn_CFG0 bits */
91#define DEV0_CFG1_ECC_DISABLE 0
92#define WIDE_FLASH 1
93#define NAND_RECOVERY_CYCLES 2
94#define CS_ACTIVE_BSY 5
95#define BAD_BLOCK_BYTE_NUM 6
96#define BAD_BLOCK_IN_SPARE_AREA 16
97#define WR_RD_BSY_GAP 17
98#define ENABLE_BCH_ECC 27
99
100/* NAND_DEV0_ECC_CFG bits */
101#define ECC_CFG_ECC_DISABLE 0
102#define ECC_SW_RESET 1
103#define ECC_MODE 4
104#define ECC_PARITY_SIZE_BYTES_BCH 8
105#define ECC_NUM_DATA_BYTES 16
106#define ECC_FORCE_CLK_OPEN 30
107
108/* NAND_DEV_CMD1 bits */
109#define READ_ADDR 0
110
111/* NAND_DEV_CMD_VLD bits */
112#define READ_START_VLD 0
113
114/* NAND_EBI2_ECC_BUF_CFG bits */
115#define NUM_STEPS 0
116
117/* NAND_ERASED_CW_DETECT_CFG bits */
118#define ERASED_CW_ECC_MASK 1
119#define AUTO_DETECT_RES 0
120#define MASK_ECC (1 << ERASED_CW_ECC_MASK)
121#define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
122#define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
123#define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
124#define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
125
126/* NAND_ERASED_CW_DETECT_STATUS bits */
127#define PAGE_ALL_ERASED BIT(7)
128#define CODEWORD_ALL_ERASED BIT(6)
129#define PAGE_ERASED BIT(5)
130#define CODEWORD_ERASED BIT(4)
131#define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
132#define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
133
134/* Version Mask */
135#define NAND_VERSION_MAJOR_MASK 0xf0000000
136#define NAND_VERSION_MAJOR_SHIFT 28
137#define NAND_VERSION_MINOR_MASK 0x0fff0000
138#define NAND_VERSION_MINOR_SHIFT 16
139
140/* NAND OP_CMDs */
141#define PAGE_READ 0x2
142#define PAGE_READ_WITH_ECC 0x3
143#define PAGE_READ_WITH_ECC_SPARE 0x4
144#define PROGRAM_PAGE 0x6
145#define PAGE_PROGRAM_WITH_ECC 0x7
146#define PROGRAM_PAGE_SPARE 0x9
147#define BLOCK_ERASE 0xa
148#define FETCH_ID 0xb
149#define RESET_DEVICE 0xd
150
151/*
152 * the NAND controller performs reads/writes with ECC in 516 byte chunks.
153 * the driver calls the chunks 'step' or 'codeword' interchangeably
154 */
155#define NANDC_STEP_SIZE 512
156
157/*
158 * the largest page size we support is 8K, this will have 16 steps/codewords
159 * of 512 bytes each
160 */
161#define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
162
163/* we read at most 3 registers per codeword scan */
164#define MAX_REG_RD (3 * MAX_NUM_STEPS)
165
166/* ECC modes supported by the controller */
167#define ECC_NONE BIT(0)
168#define ECC_RS_4BIT BIT(1)
169#define ECC_BCH_4BIT BIT(2)
170#define ECC_BCH_8BIT BIT(3)
171
172struct desc_info {
173 struct list_head node;
174
175 enum dma_data_direction dir;
176 struct scatterlist sgl;
177 struct dma_async_tx_descriptor *dma_desc;
178};
179
180/*
181 * holds the current register values that we want to write. acts as a contiguous
182 * chunk of memory which we use to write the controller registers through DMA.
183 */
184struct nandc_regs {
185 __le32 cmd;
186 __le32 addr0;
187 __le32 addr1;
188 __le32 chip_sel;
189 __le32 exec;
190
191 __le32 cfg0;
192 __le32 cfg1;
193 __le32 ecc_bch_cfg;
194
195 __le32 clrflashstatus;
196 __le32 clrreadstatus;
197
198 __le32 cmd1;
199 __le32 vld;
200
201 __le32 orig_cmd1;
202 __le32 orig_vld;
203
204 __le32 ecc_buf_cfg;
205};
206
207/*
208 * NAND controller data struct
209 *
210 * @controller: base controller structure
211 * @host_list: list containing all the chips attached to the
212 * controller
213 * @dev: parent device
214 * @base: MMIO base
215 * @base_dma: physical base address of controller registers
216 * @core_clk: controller clock
217 * @aon_clk: another controller clock
218 *
219 * @chan: dma channel
220 * @cmd_crci: ADM DMA CRCI for command flow control
221 * @data_crci: ADM DMA CRCI for data flow control
222 * @desc_list: DMA descriptor list (list of desc_infos)
223 *
224 * @data_buffer: our local DMA buffer for page read/writes,
225 * used when we can't use the buffer provided
226 * by upper layers directly
227 * @buf_size/count/start: markers for chip->read_buf/write_buf functions
228 * @reg_read_buf: local buffer for reading back registers via DMA
229 * @reg_read_pos: marker for data read in reg_read_buf
230 *
231 * @regs: a contiguous chunk of memory for DMA register
232 * writes. contains the register values to be
233 * written to controller
234 * @cmd1/vld: some fixed controller register values
235 * @ecc_modes: supported ECC modes by the current controller,
236 * initialized via DT match data
237 */
238struct qcom_nand_controller {
239 struct nand_hw_control controller;
240 struct list_head host_list;
241
242 struct device *dev;
243
244 void __iomem *base;
245 dma_addr_t base_dma;
246
247 struct clk *core_clk;
248 struct clk *aon_clk;
249
250 struct dma_chan *chan;
251 unsigned int cmd_crci;
252 unsigned int data_crci;
253 struct list_head desc_list;
254
255 u8 *data_buffer;
256 int buf_size;
257 int buf_count;
258 int buf_start;
259
260 __le32 *reg_read_buf;
261 int reg_read_pos;
262
263 struct nandc_regs *regs;
264
265 u32 cmd1, vld;
266 u32 ecc_modes;
267};
268
269/*
270 * NAND chip structure
271 *
272 * @chip: base NAND chip structure
273 * @node: list node to add itself to host_list in
274 * qcom_nand_controller
275 *
276 * @cs: chip select value for this chip
277 * @cw_size: the number of bytes in a single step/codeword
278 * of a page, consisting of all data, ecc, spare
279 * and reserved bytes
280 * @cw_data: the number of bytes within a codeword protected
281 * by ECC
282 * @use_ecc: request the controller to use ECC for the
283 * upcoming read/write
284 * @bch_enabled: flag to tell whether BCH ECC mode is used
285 * @ecc_bytes_hw: ECC bytes used by controller hardware for this
286 * chip
287 * @status: value to be returned if NAND_CMD_STATUS command
288 * is executed
289 * @last_command: keeps track of last command on this chip. used
290 * for reading correct status
291 *
292 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
293 * ecc/non-ecc mode for the current nand flash
294 * device
295 */
296struct qcom_nand_host {
297 struct nand_chip chip;
298 struct list_head node;
299
300 int cs;
301 int cw_size;
302 int cw_data;
303 bool use_ecc;
304 bool bch_enabled;
305 int ecc_bytes_hw;
306 int spare_bytes;
307 int bbm_size;
308 u8 status;
309 int last_command;
310
311 u32 cfg0, cfg1;
312 u32 cfg0_raw, cfg1_raw;
313 u32 ecc_buf_cfg;
314 u32 ecc_bch_cfg;
315 u32 clrflashstatus;
316 u32 clrreadstatus;
317};
318
319static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
320{
321 return container_of(chip, struct qcom_nand_host, chip);
322}
323
324static inline struct qcom_nand_controller *
325get_qcom_nand_controller(struct nand_chip *chip)
326{
327 return container_of(chip->controller, struct qcom_nand_controller,
328 controller);
329}
330
331static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
332{
333 return ioread32(nandc->base + offset);
334}
335
336static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
337 u32 val)
338{
339 iowrite32(val, nandc->base + offset);
340}
341
342static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
343{
344 switch (offset) {
345 case NAND_FLASH_CMD:
346 return &regs->cmd;
347 case NAND_ADDR0:
348 return &regs->addr0;
349 case NAND_ADDR1:
350 return &regs->addr1;
351 case NAND_FLASH_CHIP_SELECT:
352 return &regs->chip_sel;
353 case NAND_EXEC_CMD:
354 return &regs->exec;
355 case NAND_FLASH_STATUS:
356 return &regs->clrflashstatus;
357 case NAND_DEV0_CFG0:
358 return &regs->cfg0;
359 case NAND_DEV0_CFG1:
360 return &regs->cfg1;
361 case NAND_DEV0_ECC_CFG:
362 return &regs->ecc_bch_cfg;
363 case NAND_READ_STATUS:
364 return &regs->clrreadstatus;
365 case NAND_DEV_CMD1:
366 return &regs->cmd1;
367 case NAND_DEV_CMD1_RESTORE:
368 return &regs->orig_cmd1;
369 case NAND_DEV_CMD_VLD:
370 return &regs->vld;
371 case NAND_DEV_CMD_VLD_RESTORE:
372 return &regs->orig_vld;
373 case NAND_EBI2_ECC_BUF_CFG:
374 return &regs->ecc_buf_cfg;
375 default:
376 return NULL;
377 }
378}
379
380static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
381 u32 val)
382{
383 struct nandc_regs *regs = nandc->regs;
384 __le32 *reg;
385
386 reg = offset_to_nandc_reg(regs, offset);
387
388 if (reg)
389 *reg = cpu_to_le32(val);
390}
391
392/* helper to configure address register values */
393static void set_address(struct qcom_nand_host *host, u16 column, int page)
394{
395 struct nand_chip *chip = &host->chip;
396 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
397
398 if (chip->options & NAND_BUSWIDTH_16)
399 column >>= 1;
400
401 nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
402 nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
403}
404
405/*
406 * update_rw_regs: set up read/write register values, these will be
407 * written to the NAND controller registers via DMA
408 *
409 * @num_cw: number of steps for the read/write operation
410 * @read: read or write operation
411 */
412static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
413{
414 struct nand_chip *chip = &host->chip;
415 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
416 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
417
418 if (read) {
419 if (host->use_ecc)
420 cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
421 else
422 cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
423 } else {
424 cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
425 }
426
427 if (host->use_ecc) {
428 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
429 (num_cw - 1) << CW_PER_PAGE;
430
431 cfg1 = host->cfg1;
432 ecc_bch_cfg = host->ecc_bch_cfg;
433 } else {
434 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
435 (num_cw - 1) << CW_PER_PAGE;
436
437 cfg1 = host->cfg1_raw;
438 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
439 }
440
441 nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
442 nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
443 nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
444 nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
445 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
446 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
447 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
448 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
449}
450
451static int prep_dma_desc(struct qcom_nand_controller *nandc, bool read,
452 int reg_off, const void *vaddr, int size,
453 bool flow_control)
454{
455 struct desc_info *desc;
456 struct dma_async_tx_descriptor *dma_desc;
457 struct scatterlist *sgl;
458 struct dma_slave_config slave_conf;
459 enum dma_transfer_direction dir_eng;
460 int ret;
461
462 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
463 if (!desc)
464 return -ENOMEM;
465
466 sgl = &desc->sgl;
467
468 sg_init_one(sgl, vaddr, size);
469
470 if (read) {
471 dir_eng = DMA_DEV_TO_MEM;
472 desc->dir = DMA_FROM_DEVICE;
473 } else {
474 dir_eng = DMA_MEM_TO_DEV;
475 desc->dir = DMA_TO_DEVICE;
476 }
477
478 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
479 if (ret == 0) {
480 ret = -ENOMEM;
481 goto err;
482 }
483
484 memset(&slave_conf, 0x00, sizeof(slave_conf));
485
486 slave_conf.device_fc = flow_control;
487 if (read) {
488 slave_conf.src_maxburst = 16;
489 slave_conf.src_addr = nandc->base_dma + reg_off;
490 slave_conf.slave_id = nandc->data_crci;
491 } else {
492 slave_conf.dst_maxburst = 16;
493 slave_conf.dst_addr = nandc->base_dma + reg_off;
494 slave_conf.slave_id = nandc->cmd_crci;
495 }
496
497 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
498 if (ret) {
499 dev_err(nandc->dev, "failed to configure dma channel\n");
500 goto err;
501 }
502
503 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
504 if (!dma_desc) {
505 dev_err(nandc->dev, "failed to prepare desc\n");
506 ret = -EINVAL;
507 goto err;
508 }
509
510 desc->dma_desc = dma_desc;
511
512 list_add_tail(&desc->node, &nandc->desc_list);
513
514 return 0;
515err:
516 kfree(desc);
517
518 return ret;
519}
520
521/*
522 * read_reg_dma: prepares a descriptor to read a given number of
523 * contiguous registers to the reg_read_buf pointer
524 *
525 * @first: offset of the first register in the contiguous block
526 * @num_regs: number of registers to read
527 */
528static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
529 int num_regs)
530{
531 bool flow_control = false;
532 void *vaddr;
533 int size;
534
535 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
536 flow_control = true;
537
538 size = num_regs * sizeof(u32);
539 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
540 nandc->reg_read_pos += num_regs;
541
542 return prep_dma_desc(nandc, true, first, vaddr, size, flow_control);
543}
544
545/*
546 * write_reg_dma: prepares a descriptor to write a given number of
547 * contiguous registers
548 *
549 * @first: offset of the first register in the contiguous block
550 * @num_regs: number of registers to write
551 */
552static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
553 int num_regs)
554{
555 bool flow_control = false;
556 struct nandc_regs *regs = nandc->regs;
557 void *vaddr;
558 int size;
559
560 vaddr = offset_to_nandc_reg(regs, first);
561
562 if (first == NAND_FLASH_CMD)
563 flow_control = true;
564
565 if (first == NAND_DEV_CMD1_RESTORE)
566 first = NAND_DEV_CMD1;
567
568 if (first == NAND_DEV_CMD_VLD_RESTORE)
569 first = NAND_DEV_CMD_VLD;
570
571 size = num_regs * sizeof(u32);
572
573 return prep_dma_desc(nandc, false, first, vaddr, size, flow_control);
574}
575
576/*
577 * read_data_dma: prepares a DMA descriptor to transfer data from the
578 * controller's internal buffer to the buffer 'vaddr'
579 *
580 * @reg_off: offset within the controller's data buffer
581 * @vaddr: virtual address of the buffer we want to write to
582 * @size: DMA transaction size in bytes
583 */
584static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
585 const u8 *vaddr, int size)
586{
587 return prep_dma_desc(nandc, true, reg_off, vaddr, size, false);
588}
589
590/*
591 * write_data_dma: prepares a DMA descriptor to transfer data from
592 * 'vaddr' to the controller's internal buffer
593 *
594 * @reg_off: offset within the controller's data buffer
595 * @vaddr: virtual address of the buffer we want to read from
596 * @size: DMA transaction size in bytes
597 */
598static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
599 const u8 *vaddr, int size)
600{
601 return prep_dma_desc(nandc, false, reg_off, vaddr, size, false);
602}
603
604/*
Abhishek Sahubde43302017-07-19 17:17:55 +0530605 * Helper to prepare DMA descriptors for configuring registers
606 * before reading a NAND page.
Archit Tanejac76b78d2016-02-03 14:29:50 +0530607 */
Abhishek Sahubde43302017-07-19 17:17:55 +0530608static void config_nand_page_read(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530609{
Abhishek Sahubde43302017-07-19 17:17:55 +0530610 write_reg_dma(nandc, NAND_ADDR0, 2);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530611 write_reg_dma(nandc, NAND_DEV0_CFG0, 3);
612 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1);
Abhishek Sahubde43302017-07-19 17:17:55 +0530613}
Archit Tanejac76b78d2016-02-03 14:29:50 +0530614
Abhishek Sahubde43302017-07-19 17:17:55 +0530615/*
616 * Helper to prepare DMA descriptors for configuring registers
617 * before reading each codeword in NAND page.
618 */
619static void config_nand_cw_read(struct qcom_nand_controller *nandc)
620{
621 write_reg_dma(nandc, NAND_FLASH_CMD, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530622 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
623
624 read_reg_dma(nandc, NAND_FLASH_STATUS, 2);
625 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1);
626}
627
628/*
Abhishek Sahubde43302017-07-19 17:17:55 +0530629 * Helper to prepare dma descriptors to configure registers needed for reading a
630 * single codeword in page
Archit Tanejac76b78d2016-02-03 14:29:50 +0530631 */
Abhishek Sahubde43302017-07-19 17:17:55 +0530632static void config_nand_single_cw_page_read(struct qcom_nand_controller *nandc)
633{
634 config_nand_page_read(nandc);
635 config_nand_cw_read(nandc);
636}
637
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530638/*
639 * Helper to prepare DMA descriptors used to configure registers needed for
640 * before writing a NAND page.
641 */
642static void config_nand_page_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530643{
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530644 write_reg_dma(nandc, NAND_ADDR0, 2);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530645 write_reg_dma(nandc, NAND_DEV0_CFG0, 3);
646 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1);
647}
648
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530649/*
650 * Helper to prepare DMA descriptors for configuring registers
651 * before writing each codeword in NAND page.
652 */
653static void config_nand_cw_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530654{
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530655 write_reg_dma(nandc, NAND_FLASH_CMD, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530656 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
657
658 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
659
660 write_reg_dma(nandc, NAND_FLASH_STATUS, 1);
661 write_reg_dma(nandc, NAND_READ_STATUS, 1);
662}
663
664/*
665 * the following functions are used within chip->cmdfunc() to perform different
666 * NAND_CMD_* commands
667 */
668
669/* sets up descriptors for NAND_CMD_PARAM */
670static int nandc_param(struct qcom_nand_host *host)
671{
672 struct nand_chip *chip = &host->chip;
673 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
674
675 /*
676 * NAND_CMD_PARAM is called before we know much about the FLASH chip
677 * in use. we configure the controller to perform a raw read of 512
678 * bytes to read onfi params
679 */
680 nandc_set_reg(nandc, NAND_FLASH_CMD, PAGE_READ | PAGE_ACC | LAST_PAGE);
681 nandc_set_reg(nandc, NAND_ADDR0, 0);
682 nandc_set_reg(nandc, NAND_ADDR1, 0);
683 nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
684 | 512 << UD_SIZE_BYTES
685 | 5 << NUM_ADDR_CYCLES
686 | 0 << SPARE_SIZE_BYTES);
687 nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
688 | 0 << CS_ACTIVE_BSY
689 | 17 << BAD_BLOCK_BYTE_NUM
690 | 1 << BAD_BLOCK_IN_SPARE_AREA
691 | 2 << WR_RD_BSY_GAP
692 | 0 << WIDE_FLASH
693 | 1 << DEV0_CFG1_ECC_DISABLE);
694 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
695
696 /* configure CMD1 and VLD for ONFI param probing */
697 nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
698 (nandc->vld & ~(1 << READ_START_VLD))
699 | 0 << READ_START_VLD);
700 nandc_set_reg(nandc, NAND_DEV_CMD1,
701 (nandc->cmd1 & ~(0xFF << READ_ADDR))
702 | NAND_CMD_PARAM << READ_ADDR);
703
704 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
705
706 nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
707 nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
708
709 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1);
710 write_reg_dma(nandc, NAND_DEV_CMD1, 1);
711
712 nandc->buf_count = 512;
713 memset(nandc->data_buffer, 0xff, nandc->buf_count);
714
Abhishek Sahubde43302017-07-19 17:17:55 +0530715 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530716
717 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
718 nandc->buf_count);
719
720 /* restore CMD1 and VLD regs */
721 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1);
722 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1);
723
724 return 0;
725}
726
727/* sets up descriptors for NAND_CMD_ERASE1 */
728static int erase_block(struct qcom_nand_host *host, int page_addr)
729{
730 struct nand_chip *chip = &host->chip;
731 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
732
733 nandc_set_reg(nandc, NAND_FLASH_CMD,
734 BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
735 nandc_set_reg(nandc, NAND_ADDR0, page_addr);
736 nandc_set_reg(nandc, NAND_ADDR1, 0);
737 nandc_set_reg(nandc, NAND_DEV0_CFG0,
738 host->cfg0_raw & ~(7 << CW_PER_PAGE));
739 nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
740 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
741 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
742 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
743
744 write_reg_dma(nandc, NAND_FLASH_CMD, 3);
745 write_reg_dma(nandc, NAND_DEV0_CFG0, 2);
746 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
747
748 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
749
750 write_reg_dma(nandc, NAND_FLASH_STATUS, 1);
751 write_reg_dma(nandc, NAND_READ_STATUS, 1);
752
753 return 0;
754}
755
756/* sets up descriptors for NAND_CMD_READID */
757static int read_id(struct qcom_nand_host *host, int column)
758{
759 struct nand_chip *chip = &host->chip;
760 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
761
762 if (column == -1)
763 return 0;
764
765 nandc_set_reg(nandc, NAND_FLASH_CMD, FETCH_ID);
766 nandc_set_reg(nandc, NAND_ADDR0, column);
767 nandc_set_reg(nandc, NAND_ADDR1, 0);
768 nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
769 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
770
771 write_reg_dma(nandc, NAND_FLASH_CMD, 4);
772 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
773
774 read_reg_dma(nandc, NAND_READ_ID, 1);
775
776 return 0;
777}
778
779/* sets up descriptors for NAND_CMD_RESET */
780static int reset(struct qcom_nand_host *host)
781{
782 struct nand_chip *chip = &host->chip;
783 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
784
785 nandc_set_reg(nandc, NAND_FLASH_CMD, RESET_DEVICE);
786 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
787
788 write_reg_dma(nandc, NAND_FLASH_CMD, 1);
789 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
790
791 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
792
793 return 0;
794}
795
796/* helpers to submit/free our list of dma descriptors */
797static int submit_descs(struct qcom_nand_controller *nandc)
798{
799 struct desc_info *desc;
800 dma_cookie_t cookie = 0;
801
802 list_for_each_entry(desc, &nandc->desc_list, node)
803 cookie = dmaengine_submit(desc->dma_desc);
804
805 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
806 return -ETIMEDOUT;
807
808 return 0;
809}
810
811static void free_descs(struct qcom_nand_controller *nandc)
812{
813 struct desc_info *desc, *n;
814
815 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
816 list_del(&desc->node);
817 dma_unmap_sg(nandc->dev, &desc->sgl, 1, desc->dir);
818 kfree(desc);
819 }
820}
821
822/* reset the register read buffer for next NAND operation */
823static void clear_read_regs(struct qcom_nand_controller *nandc)
824{
825 nandc->reg_read_pos = 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530826}
827
828static void pre_command(struct qcom_nand_host *host, int command)
829{
830 struct nand_chip *chip = &host->chip;
831 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
832
833 nandc->buf_count = 0;
834 nandc->buf_start = 0;
835 host->use_ecc = false;
836 host->last_command = command;
837
838 clear_read_regs(nandc);
839}
840
841/*
842 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
843 * privately maintained status byte, this status byte can be read after
844 * NAND_CMD_STATUS is called
845 */
846static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
847{
848 struct nand_chip *chip = &host->chip;
849 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
850 struct nand_ecc_ctrl *ecc = &chip->ecc;
851 int num_cw;
852 int i;
853
854 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
855
856 for (i = 0; i < num_cw; i++) {
857 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
858
859 if (flash_status & FS_MPU_ERR)
860 host->status &= ~NAND_STATUS_WP;
861
862 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
863 (flash_status &
864 FS_DEVICE_STS_ERR)))
865 host->status |= NAND_STATUS_FAIL;
866 }
867}
868
869static void post_command(struct qcom_nand_host *host, int command)
870{
871 struct nand_chip *chip = &host->chip;
872 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
873
874 switch (command) {
875 case NAND_CMD_READID:
876 memcpy(nandc->data_buffer, nandc->reg_read_buf,
877 nandc->buf_count);
878 break;
879 case NAND_CMD_PAGEPROG:
880 case NAND_CMD_ERASE1:
881 parse_erase_write_errors(host, command);
882 break;
883 default:
884 break;
885 }
886}
887
888/*
889 * Implements chip->cmdfunc. It's only used for a limited set of commands.
890 * The rest of the commands wouldn't be called by upper layers. For example,
891 * NAND_CMD_READOOB would never be called because we have our own versions
892 * of read_oob ops for nand_ecc_ctrl.
893 */
894static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
895 int column, int page_addr)
896{
897 struct nand_chip *chip = mtd_to_nand(mtd);
898 struct qcom_nand_host *host = to_qcom_nand_host(chip);
899 struct nand_ecc_ctrl *ecc = &chip->ecc;
900 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
901 bool wait = false;
902 int ret = 0;
903
904 pre_command(host, command);
905
906 switch (command) {
907 case NAND_CMD_RESET:
908 ret = reset(host);
909 wait = true;
910 break;
911
912 case NAND_CMD_READID:
913 nandc->buf_count = 4;
914 ret = read_id(host, column);
915 wait = true;
916 break;
917
918 case NAND_CMD_PARAM:
919 ret = nandc_param(host);
920 wait = true;
921 break;
922
923 case NAND_CMD_ERASE1:
924 ret = erase_block(host, page_addr);
925 wait = true;
926 break;
927
928 case NAND_CMD_READ0:
929 /* we read the entire page for now */
930 WARN_ON(column != 0);
931
932 host->use_ecc = true;
933 set_address(host, 0, page_addr);
934 update_rw_regs(host, ecc->steps, true);
935 break;
936
937 case NAND_CMD_SEQIN:
938 WARN_ON(column != 0);
939 set_address(host, 0, page_addr);
940 break;
941
942 case NAND_CMD_PAGEPROG:
943 case NAND_CMD_STATUS:
944 case NAND_CMD_NONE:
945 default:
946 break;
947 }
948
949 if (ret) {
950 dev_err(nandc->dev, "failure executing command %d\n",
951 command);
952 free_descs(nandc);
953 return;
954 }
955
956 if (wait) {
957 ret = submit_descs(nandc);
958 if (ret)
959 dev_err(nandc->dev,
960 "failure submitting descs for command %d\n",
961 command);
962 }
963
964 free_descs(nandc);
965
966 post_command(host, command);
967}
968
969/*
970 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
971 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
972 *
973 * when using RS ECC, the HW reports the same erros when reading an erased CW,
974 * but it notifies that it is an erased CW by placing special characters at
975 * certain offsets in the buffer.
976 *
977 * verify if the page is erased or not, and fix up the page for RS ECC by
978 * replacing the special characters with 0xff.
979 */
980static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
981{
982 u8 empty1, empty2;
983
984 /*
985 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
986 * is erased by looking for 0x54s at offsets 3 and 175 from the
987 * beginning of each codeword
988 */
989
990 empty1 = data_buf[3];
991 empty2 = data_buf[175];
992
993 /*
994 * if the erased codework markers, if they exist override them with
995 * 0xffs
996 */
997 if ((empty1 == 0x54 && empty2 == 0xff) ||
998 (empty1 == 0xff && empty2 == 0x54)) {
999 data_buf[3] = 0xff;
1000 data_buf[175] = 0xff;
1001 }
1002
1003 /*
1004 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1005 * restore the original values at the special offsets
1006 */
1007 if (memchr_inv(data_buf, 0xff, data_len)) {
1008 data_buf[3] = empty1;
1009 data_buf[175] = empty2;
1010
1011 return false;
1012 }
1013
1014 return true;
1015}
1016
1017struct read_stats {
1018 __le32 flash;
1019 __le32 buffer;
1020 __le32 erased_cw;
1021};
1022
1023/*
1024 * reads back status registers set by the controller to notify page read
1025 * errors. this is equivalent to what 'ecc->correct()' would do.
1026 */
1027static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1028 u8 *oob_buf)
1029{
1030 struct nand_chip *chip = &host->chip;
1031 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1032 struct mtd_info *mtd = nand_to_mtd(chip);
1033 struct nand_ecc_ctrl *ecc = &chip->ecc;
1034 unsigned int max_bitflips = 0;
1035 struct read_stats *buf;
1036 int i;
1037
1038 buf = (struct read_stats *)nandc->reg_read_buf;
1039
1040 for (i = 0; i < ecc->steps; i++, buf++) {
1041 u32 flash, buffer, erased_cw;
1042 int data_len, oob_len;
1043
1044 if (i == (ecc->steps - 1)) {
1045 data_len = ecc->size - ((ecc->steps - 1) << 2);
1046 oob_len = ecc->steps << 2;
1047 } else {
1048 data_len = host->cw_data;
1049 oob_len = 0;
1050 }
1051
1052 flash = le32_to_cpu(buf->flash);
1053 buffer = le32_to_cpu(buf->buffer);
1054 erased_cw = le32_to_cpu(buf->erased_cw);
1055
1056 if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1057 bool erased;
1058
1059 /* ignore erased codeword errors */
1060 if (host->bch_enabled) {
1061 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1062 true : false;
1063 } else {
1064 erased = erased_chunk_check_and_fixup(data_buf,
1065 data_len);
1066 }
1067
1068 if (erased) {
1069 data_buf += data_len;
1070 if (oob_buf)
1071 oob_buf += oob_len + ecc->bytes;
1072 continue;
1073 }
1074
1075 if (buffer & BS_UNCORRECTABLE_BIT) {
1076 int ret, ecclen, extraooblen;
1077 void *eccbuf;
1078
1079 eccbuf = oob_buf ? oob_buf + oob_len : NULL;
1080 ecclen = oob_buf ? host->ecc_bytes_hw : 0;
1081 extraooblen = oob_buf ? oob_len : 0;
1082
1083 /*
1084 * make sure it isn't an erased page reported
1085 * as not-erased by HW because of a few bitflips
1086 */
1087 ret = nand_check_erased_ecc_chunk(data_buf,
1088 data_len, eccbuf, ecclen, oob_buf,
1089 extraooblen, ecc->strength);
1090 if (ret < 0) {
1091 mtd->ecc_stats.failed++;
1092 } else {
1093 mtd->ecc_stats.corrected += ret;
1094 max_bitflips =
1095 max_t(unsigned int, max_bitflips, ret);
1096 }
1097 }
1098 } else {
1099 unsigned int stat;
1100
1101 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1102 mtd->ecc_stats.corrected += stat;
1103 max_bitflips = max(max_bitflips, stat);
1104 }
1105
1106 data_buf += data_len;
1107 if (oob_buf)
1108 oob_buf += oob_len + ecc->bytes;
1109 }
1110
1111 return max_bitflips;
1112}
1113
1114/*
1115 * helper to perform the actual page read operation, used by ecc->read_page(),
1116 * ecc->read_oob()
1117 */
1118static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1119 u8 *oob_buf)
1120{
1121 struct nand_chip *chip = &host->chip;
1122 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1123 struct nand_ecc_ctrl *ecc = &chip->ecc;
1124 int i, ret;
1125
Abhishek Sahubde43302017-07-19 17:17:55 +05301126 config_nand_page_read(nandc);
1127
Archit Tanejac76b78d2016-02-03 14:29:50 +05301128 /* queue cmd descs for each codeword */
1129 for (i = 0; i < ecc->steps; i++) {
1130 int data_size, oob_size;
1131
1132 if (i == (ecc->steps - 1)) {
1133 data_size = ecc->size - ((ecc->steps - 1) << 2);
1134 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1135 host->spare_bytes;
1136 } else {
1137 data_size = host->cw_data;
1138 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1139 }
1140
Abhishek Sahubde43302017-07-19 17:17:55 +05301141 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301142
1143 if (data_buf)
1144 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
1145 data_size);
1146
1147 /*
1148 * when ecc is enabled, the controller doesn't read the real
1149 * or dummy bad block markers in each chunk. To maintain a
1150 * consistent layout across RAW and ECC reads, we just
1151 * leave the real/dummy BBM offsets empty (i.e, filled with
1152 * 0xffs)
1153 */
1154 if (oob_buf) {
1155 int j;
1156
1157 for (j = 0; j < host->bbm_size; j++)
1158 *oob_buf++ = 0xff;
1159
1160 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
1161 oob_buf, oob_size);
1162 }
1163
1164 if (data_buf)
1165 data_buf += data_size;
1166 if (oob_buf)
1167 oob_buf += oob_size;
1168 }
1169
1170 ret = submit_descs(nandc);
1171 if (ret)
1172 dev_err(nandc->dev, "failure to read page/oob\n");
1173
1174 free_descs(nandc);
1175
1176 return ret;
1177}
1178
1179/*
1180 * a helper that copies the last step/codeword of a page (containing free oob)
1181 * into our local buffer
1182 */
1183static int copy_last_cw(struct qcom_nand_host *host, int page)
1184{
1185 struct nand_chip *chip = &host->chip;
1186 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1187 struct nand_ecc_ctrl *ecc = &chip->ecc;
1188 int size;
1189 int ret;
1190
1191 clear_read_regs(nandc);
1192
1193 size = host->use_ecc ? host->cw_data : host->cw_size;
1194
1195 /* prepare a clean read buffer */
1196 memset(nandc->data_buffer, 0xff, size);
1197
1198 set_address(host, host->cw_size * (ecc->steps - 1), page);
1199 update_rw_regs(host, 1, true);
1200
Abhishek Sahubde43302017-07-19 17:17:55 +05301201 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301202
1203 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size);
1204
1205 ret = submit_descs(nandc);
1206 if (ret)
1207 dev_err(nandc->dev, "failed to copy last codeword\n");
1208
1209 free_descs(nandc);
1210
1211 return ret;
1212}
1213
1214/* implements ecc->read_page() */
1215static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1216 uint8_t *buf, int oob_required, int page)
1217{
1218 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1219 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1220 u8 *data_buf, *oob_buf = NULL;
1221 int ret;
1222
1223 data_buf = buf;
1224 oob_buf = oob_required ? chip->oob_poi : NULL;
1225
1226 ret = read_page_ecc(host, data_buf, oob_buf);
1227 if (ret) {
1228 dev_err(nandc->dev, "failure to read page\n");
1229 return ret;
1230 }
1231
1232 return parse_read_errors(host, data_buf, oob_buf);
1233}
1234
1235/* implements ecc->read_page_raw() */
1236static int qcom_nandc_read_page_raw(struct mtd_info *mtd,
1237 struct nand_chip *chip, uint8_t *buf,
1238 int oob_required, int page)
1239{
1240 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1241 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1242 u8 *data_buf, *oob_buf;
1243 struct nand_ecc_ctrl *ecc = &chip->ecc;
1244 int i, ret;
1245
1246 data_buf = buf;
1247 oob_buf = chip->oob_poi;
1248
1249 host->use_ecc = false;
1250 update_rw_regs(host, ecc->steps, true);
Abhishek Sahubde43302017-07-19 17:17:55 +05301251 config_nand_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301252
1253 for (i = 0; i < ecc->steps; i++) {
1254 int data_size1, data_size2, oob_size1, oob_size2;
1255 int reg_off = FLASH_BUF_ACC;
1256
1257 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1258 oob_size1 = host->bbm_size;
1259
1260 if (i == (ecc->steps - 1)) {
1261 data_size2 = ecc->size - data_size1 -
1262 ((ecc->steps - 1) << 2);
1263 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1264 host->spare_bytes;
1265 } else {
1266 data_size2 = host->cw_data - data_size1;
1267 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1268 }
1269
Abhishek Sahubde43302017-07-19 17:17:55 +05301270 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301271
1272 read_data_dma(nandc, reg_off, data_buf, data_size1);
1273 reg_off += data_size1;
1274 data_buf += data_size1;
1275
1276 read_data_dma(nandc, reg_off, oob_buf, oob_size1);
1277 reg_off += oob_size1;
1278 oob_buf += oob_size1;
1279
1280 read_data_dma(nandc, reg_off, data_buf, data_size2);
1281 reg_off += data_size2;
1282 data_buf += data_size2;
1283
1284 read_data_dma(nandc, reg_off, oob_buf, oob_size2);
1285 oob_buf += oob_size2;
1286 }
1287
1288 ret = submit_descs(nandc);
1289 if (ret)
1290 dev_err(nandc->dev, "failure to read raw page\n");
1291
1292 free_descs(nandc);
1293
1294 return 0;
1295}
1296
1297/* implements ecc->read_oob() */
1298static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1299 int page)
1300{
1301 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1302 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1303 struct nand_ecc_ctrl *ecc = &chip->ecc;
1304 int ret;
1305
1306 clear_read_regs(nandc);
1307
1308 host->use_ecc = true;
1309 set_address(host, 0, page);
1310 update_rw_regs(host, ecc->steps, true);
1311
1312 ret = read_page_ecc(host, NULL, chip->oob_poi);
1313 if (ret)
1314 dev_err(nandc->dev, "failure to read oob\n");
1315
1316 return ret;
1317}
1318
1319/* implements ecc->write_page() */
1320static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1321 const uint8_t *buf, int oob_required, int page)
1322{
1323 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1324 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1325 struct nand_ecc_ctrl *ecc = &chip->ecc;
1326 u8 *data_buf, *oob_buf;
1327 int i, ret;
1328
1329 clear_read_regs(nandc);
1330
1331 data_buf = (u8 *)buf;
1332 oob_buf = chip->oob_poi;
1333
1334 host->use_ecc = true;
1335 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301336 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301337
1338 for (i = 0; i < ecc->steps; i++) {
1339 int data_size, oob_size;
1340
1341 if (i == (ecc->steps - 1)) {
1342 data_size = ecc->size - ((ecc->steps - 1) << 2);
1343 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1344 host->spare_bytes;
1345 } else {
1346 data_size = host->cw_data;
1347 oob_size = ecc->bytes;
1348 }
1349
Archit Tanejac76b78d2016-02-03 14:29:50 +05301350
1351 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size);
1352
1353 /*
1354 * when ECC is enabled, we don't really need to write anything
1355 * to oob for the first n - 1 codewords since these oob regions
1356 * just contain ECC bytes that's written by the controller
1357 * itself. For the last codeword, we skip the bbm positions and
1358 * write to the free oob area.
1359 */
1360 if (i == (ecc->steps - 1)) {
1361 oob_buf += host->bbm_size;
1362
1363 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
1364 oob_buf, oob_size);
1365 }
1366
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301367 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301368
1369 data_buf += data_size;
1370 oob_buf += oob_size;
1371 }
1372
1373 ret = submit_descs(nandc);
1374 if (ret)
1375 dev_err(nandc->dev, "failure to write page\n");
1376
1377 free_descs(nandc);
1378
1379 return ret;
1380}
1381
1382/* implements ecc->write_page_raw() */
1383static int qcom_nandc_write_page_raw(struct mtd_info *mtd,
1384 struct nand_chip *chip, const uint8_t *buf,
1385 int oob_required, int page)
1386{
1387 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1388 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1389 struct nand_ecc_ctrl *ecc = &chip->ecc;
1390 u8 *data_buf, *oob_buf;
1391 int i, ret;
1392
1393 clear_read_regs(nandc);
1394
1395 data_buf = (u8 *)buf;
1396 oob_buf = chip->oob_poi;
1397
1398 host->use_ecc = false;
1399 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301400 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301401
1402 for (i = 0; i < ecc->steps; i++) {
1403 int data_size1, data_size2, oob_size1, oob_size2;
1404 int reg_off = FLASH_BUF_ACC;
1405
1406 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1407 oob_size1 = host->bbm_size;
1408
1409 if (i == (ecc->steps - 1)) {
1410 data_size2 = ecc->size - data_size1 -
1411 ((ecc->steps - 1) << 2);
1412 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1413 host->spare_bytes;
1414 } else {
1415 data_size2 = host->cw_data - data_size1;
1416 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1417 }
1418
Archit Tanejac76b78d2016-02-03 14:29:50 +05301419 write_data_dma(nandc, reg_off, data_buf, data_size1);
1420 reg_off += data_size1;
1421 data_buf += data_size1;
1422
1423 write_data_dma(nandc, reg_off, oob_buf, oob_size1);
1424 reg_off += oob_size1;
1425 oob_buf += oob_size1;
1426
1427 write_data_dma(nandc, reg_off, data_buf, data_size2);
1428 reg_off += data_size2;
1429 data_buf += data_size2;
1430
1431 write_data_dma(nandc, reg_off, oob_buf, oob_size2);
1432 oob_buf += oob_size2;
1433
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301434 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301435 }
1436
1437 ret = submit_descs(nandc);
1438 if (ret)
1439 dev_err(nandc->dev, "failure to write raw page\n");
1440
1441 free_descs(nandc);
1442
1443 return ret;
1444}
1445
1446/*
1447 * implements ecc->write_oob()
1448 *
1449 * the NAND controller cannot write only data or only oob within a codeword,
1450 * since ecc is calculated for the combined codeword. we first copy the
1451 * entire contents for the last codeword(data + oob), replace the old oob
1452 * with the new one in chip->oob_poi, and then write the entire codeword.
1453 * this read-copy-write operation results in a slight performance loss.
1454 */
1455static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1456 int page)
1457{
1458 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1459 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1460 struct nand_ecc_ctrl *ecc = &chip->ecc;
1461 u8 *oob = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301462 int data_size, oob_size;
1463 int ret, status = 0;
1464
1465 host->use_ecc = true;
1466
1467 ret = copy_last_cw(host, page);
1468 if (ret)
1469 return ret;
1470
1471 clear_read_regs(nandc);
1472
1473 /* calculate the data and oob size for the last codeword/step */
1474 data_size = ecc->size - ((ecc->steps - 1) << 2);
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001475 oob_size = mtd->oobavail;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301476
1477 /* override new oob content to last codeword */
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001478 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
1479 0, mtd->oobavail);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301480
1481 set_address(host, host->cw_size * (ecc->steps - 1), page);
1482 update_rw_regs(host, 1, false);
1483
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301484 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301485 write_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
1486 data_size + oob_size);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301487 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301488
1489 ret = submit_descs(nandc);
1490
1491 free_descs(nandc);
1492
1493 if (ret) {
1494 dev_err(nandc->dev, "failure to write oob\n");
1495 return -EIO;
1496 }
1497
1498 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1499
1500 status = chip->waitfunc(mtd, chip);
1501
1502 return status & NAND_STATUS_FAIL ? -EIO : 0;
1503}
1504
1505static int qcom_nandc_block_bad(struct mtd_info *mtd, loff_t ofs)
1506{
1507 struct nand_chip *chip = mtd_to_nand(mtd);
1508 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1509 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1510 struct nand_ecc_ctrl *ecc = &chip->ecc;
1511 int page, ret, bbpos, bad = 0;
1512 u32 flash_status;
1513
1514 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1515
1516 /*
1517 * configure registers for a raw sub page read, the address is set to
1518 * the beginning of the last codeword, we don't care about reading ecc
1519 * portion of oob. we just want the first few bytes from this codeword
1520 * that contains the BBM
1521 */
1522 host->use_ecc = false;
1523
1524 ret = copy_last_cw(host, page);
1525 if (ret)
1526 goto err;
1527
1528 flash_status = le32_to_cpu(nandc->reg_read_buf[0]);
1529
1530 if (flash_status & (FS_OP_ERR | FS_MPU_ERR)) {
1531 dev_warn(nandc->dev, "error when trying to read BBM\n");
1532 goto err;
1533 }
1534
1535 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
1536
1537 bad = nandc->data_buffer[bbpos] != 0xff;
1538
1539 if (chip->options & NAND_BUSWIDTH_16)
1540 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
1541err:
1542 return bad;
1543}
1544
1545static int qcom_nandc_block_markbad(struct mtd_info *mtd, loff_t ofs)
1546{
1547 struct nand_chip *chip = mtd_to_nand(mtd);
1548 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1549 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1550 struct nand_ecc_ctrl *ecc = &chip->ecc;
1551 int page, ret, status = 0;
1552
1553 clear_read_regs(nandc);
1554
1555 /*
1556 * to mark the BBM as bad, we flash the entire last codeword with 0s.
1557 * we don't care about the rest of the content in the codeword since
1558 * we aren't going to use this block again
1559 */
1560 memset(nandc->data_buffer, 0x00, host->cw_size);
1561
1562 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1563
1564 /* prepare write */
1565 host->use_ecc = false;
1566 set_address(host, host->cw_size * (ecc->steps - 1), page);
1567 update_rw_regs(host, 1, false);
1568
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301569 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301570 write_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, host->cw_size);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301571 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301572
1573 ret = submit_descs(nandc);
1574
1575 free_descs(nandc);
1576
1577 if (ret) {
1578 dev_err(nandc->dev, "failure to update BBM\n");
1579 return -EIO;
1580 }
1581
1582 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1583
1584 status = chip->waitfunc(mtd, chip);
1585
1586 return status & NAND_STATUS_FAIL ? -EIO : 0;
1587}
1588
1589/*
1590 * the three functions below implement chip->read_byte(), chip->read_buf()
1591 * and chip->write_buf() respectively. these aren't used for
1592 * reading/writing page data, they are used for smaller data like reading
1593 * id, status etc
1594 */
1595static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
1596{
1597 struct nand_chip *chip = mtd_to_nand(mtd);
1598 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1599 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1600 u8 *buf = nandc->data_buffer;
1601 u8 ret = 0x0;
1602
1603 if (host->last_command == NAND_CMD_STATUS) {
1604 ret = host->status;
1605
1606 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
1607
1608 return ret;
1609 }
1610
1611 if (nandc->buf_start < nandc->buf_count)
1612 ret = buf[nandc->buf_start++];
1613
1614 return ret;
1615}
1616
1617static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1618{
1619 struct nand_chip *chip = mtd_to_nand(mtd);
1620 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1621 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
1622
1623 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
1624 nandc->buf_start += real_len;
1625}
1626
1627static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
1628 int len)
1629{
1630 struct nand_chip *chip = mtd_to_nand(mtd);
1631 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1632 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
1633
1634 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
1635
1636 nandc->buf_start += real_len;
1637}
1638
1639/* we support only one external chip for now */
1640static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
1641{
1642 struct nand_chip *chip = mtd_to_nand(mtd);
1643 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1644
1645 if (chipnr <= 0)
1646 return;
1647
1648 dev_warn(nandc->dev, "invalid chip select\n");
1649}
1650
1651/*
1652 * NAND controller page layout info
1653 *
1654 * Layout with ECC enabled:
1655 *
1656 * |----------------------| |---------------------------------|
1657 * | xx.......yy| | *********xx.......yy|
1658 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
1659 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
1660 * | xx.......yy| | *********xx.......yy|
1661 * |----------------------| |---------------------------------|
1662 * codeword 1,2..n-1 codeword n
1663 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
1664 *
1665 * n = Number of codewords in the page
1666 * . = ECC bytes
1667 * * = Spare/free bytes
1668 * x = Unused byte(s)
1669 * y = Reserved byte(s)
1670 *
1671 * 2K page: n = 4, spare = 16 bytes
1672 * 4K page: n = 8, spare = 32 bytes
1673 * 8K page: n = 16, spare = 64 bytes
1674 *
1675 * the qcom nand controller operates at a sub page/codeword level. each
1676 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
1677 * the number of ECC bytes vary based on the ECC strength and the bus width.
1678 *
1679 * the first n - 1 codewords contains 516 bytes of user data, the remaining
1680 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
1681 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
1682 *
1683 * When we access a page with ECC enabled, the reserved bytes(s) are not
1684 * accessible at all. When reading, we fill up these unreadable positions
1685 * with 0xffs. When writing, the controller skips writing the inaccessible
1686 * bytes.
1687 *
1688 * Layout with ECC disabled:
1689 *
1690 * |------------------------------| |---------------------------------------|
1691 * | yy xx.......| | bb *********xx.......|
1692 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
1693 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
1694 * | yy xx.......| | bb *********xx.......|
1695 * |------------------------------| |---------------------------------------|
1696 * codeword 1,2..n-1 codeword n
1697 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
1698 *
1699 * n = Number of codewords in the page
1700 * . = ECC bytes
1701 * * = Spare/free bytes
1702 * x = Unused byte(s)
1703 * y = Dummy Bad Bock byte(s)
1704 * b = Real Bad Block byte(s)
1705 * size1/size2 = function of codeword size and 'n'
1706 *
1707 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
1708 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
1709 * Block Markers. In the last codeword, this position contains the real BBM
1710 *
1711 * In order to have a consistent layout between RAW and ECC modes, we assume
1712 * the following OOB layout arrangement:
1713 *
1714 * |-----------| |--------------------|
1715 * |yyxx.......| |bb*********xx.......|
1716 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
1717 * |yyxx.......| |bb*********xx.......|
1718 * |yyxx.......| |bb*********xx.......|
1719 * |-----------| |--------------------|
1720 * first n - 1 nth OOB region
1721 * OOB regions
1722 *
1723 * n = Number of codewords in the page
1724 * . = ECC bytes
1725 * * = FREE OOB bytes
1726 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
1727 * x = Unused byte(s)
1728 * b = Real bad block byte(s) (inaccessible when ECC enabled)
1729 *
1730 * This layout is read as is when ECC is disabled. When ECC is enabled, the
1731 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
1732 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
Boris Brezillon421e81c2016-03-18 17:54:27 +01001733 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
1734 * the sum of the three).
Archit Tanejac76b78d2016-02-03 14:29:50 +05301735 */
Boris Brezillon421e81c2016-03-18 17:54:27 +01001736static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
1737 struct mtd_oob_region *oobregion)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301738{
Boris Brezillon421e81c2016-03-18 17:54:27 +01001739 struct nand_chip *chip = mtd_to_nand(mtd);
1740 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301741 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301742
Boris Brezillon421e81c2016-03-18 17:54:27 +01001743 if (section > 1)
1744 return -ERANGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301745
Boris Brezillon421e81c2016-03-18 17:54:27 +01001746 if (!section) {
1747 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
1748 host->bbm_size;
1749 oobregion->offset = 0;
1750 } else {
1751 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
1752 oobregion->offset = mtd->oobsize - oobregion->length;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301753 }
1754
Boris Brezillon421e81c2016-03-18 17:54:27 +01001755 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301756}
1757
Boris Brezillon421e81c2016-03-18 17:54:27 +01001758static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
1759 struct mtd_oob_region *oobregion)
1760{
1761 struct nand_chip *chip = mtd_to_nand(mtd);
1762 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1763 struct nand_ecc_ctrl *ecc = &chip->ecc;
1764
1765 if (section)
1766 return -ERANGE;
1767
1768 oobregion->length = ecc->steps * 4;
1769 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
1770
1771 return 0;
1772}
1773
1774static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
1775 .ecc = qcom_nand_ooblayout_ecc,
1776 .free = qcom_nand_ooblayout_free,
1777};
1778
Archit Tanejac76b78d2016-02-03 14:29:50 +05301779static int qcom_nand_host_setup(struct qcom_nand_host *host)
1780{
1781 struct nand_chip *chip = &host->chip;
1782 struct mtd_info *mtd = nand_to_mtd(chip);
1783 struct nand_ecc_ctrl *ecc = &chip->ecc;
1784 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1785 int cwperpage, bad_block_byte;
1786 bool wide_bus;
1787 int ecc_mode = 1;
1788
1789 /*
1790 * the controller requires each step consists of 512 bytes of data.
1791 * bail out if DT has populated a wrong step size.
1792 */
1793 if (ecc->size != NANDC_STEP_SIZE) {
1794 dev_err(nandc->dev, "invalid ecc size\n");
1795 return -EINVAL;
1796 }
1797
1798 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
1799
1800 if (ecc->strength >= 8) {
1801 /* 8 bit ECC defaults to BCH ECC on all platforms */
1802 host->bch_enabled = true;
1803 ecc_mode = 1;
1804
1805 if (wide_bus) {
1806 host->ecc_bytes_hw = 14;
1807 host->spare_bytes = 0;
1808 host->bbm_size = 2;
1809 } else {
1810 host->ecc_bytes_hw = 13;
1811 host->spare_bytes = 2;
1812 host->bbm_size = 1;
1813 }
1814 } else {
1815 /*
1816 * if the controller supports BCH for 4 bit ECC, the controller
1817 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
1818 * always 10 bytes
1819 */
1820 if (nandc->ecc_modes & ECC_BCH_4BIT) {
1821 /* BCH */
1822 host->bch_enabled = true;
1823 ecc_mode = 0;
1824
1825 if (wide_bus) {
1826 host->ecc_bytes_hw = 8;
1827 host->spare_bytes = 2;
1828 host->bbm_size = 2;
1829 } else {
1830 host->ecc_bytes_hw = 7;
1831 host->spare_bytes = 4;
1832 host->bbm_size = 1;
1833 }
1834 } else {
1835 /* RS */
1836 host->ecc_bytes_hw = 10;
1837
1838 if (wide_bus) {
1839 host->spare_bytes = 0;
1840 host->bbm_size = 2;
1841 } else {
1842 host->spare_bytes = 1;
1843 host->bbm_size = 1;
1844 }
1845 }
1846 }
1847
1848 /*
1849 * we consider ecc->bytes as the sum of all the non-data content in a
1850 * step. It gives us a clean representation of the oob area (even if
1851 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
1852 * ECC and 12 bytes for 4 bit ECC
1853 */
1854 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
1855
1856 ecc->read_page = qcom_nandc_read_page;
1857 ecc->read_page_raw = qcom_nandc_read_page_raw;
1858 ecc->read_oob = qcom_nandc_read_oob;
1859 ecc->write_page = qcom_nandc_write_page;
1860 ecc->write_page_raw = qcom_nandc_write_page_raw;
1861 ecc->write_oob = qcom_nandc_write_oob;
1862
1863 ecc->mode = NAND_ECC_HW;
1864
Boris Brezillon421e81c2016-03-18 17:54:27 +01001865 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301866
1867 cwperpage = mtd->writesize / ecc->size;
1868
1869 /*
1870 * DATA_UD_BYTES varies based on whether the read/write command protects
1871 * spare data with ECC too. We protect spare data by default, so we set
1872 * it to main + spare data, which are 512 and 4 bytes respectively.
1873 */
1874 host->cw_data = 516;
1875
1876 /*
1877 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
1878 * for 8 bit ECC
1879 */
1880 host->cw_size = host->cw_data + ecc->bytes;
1881
1882 if (ecc->bytes * (mtd->writesize / ecc->size) > mtd->oobsize) {
1883 dev_err(nandc->dev, "ecc data doesn't fit in OOB area\n");
1884 return -EINVAL;
1885 }
1886
1887 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
1888
1889 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
1890 | host->cw_data << UD_SIZE_BYTES
1891 | 0 << DISABLE_STATUS_AFTER_WRITE
1892 | 5 << NUM_ADDR_CYCLES
1893 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
1894 | 0 << STATUS_BFR_READ
1895 | 1 << SET_RD_MODE_AFTER_STATUS
1896 | host->spare_bytes << SPARE_SIZE_BYTES;
1897
1898 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
1899 | 0 << CS_ACTIVE_BSY
1900 | bad_block_byte << BAD_BLOCK_BYTE_NUM
1901 | 0 << BAD_BLOCK_IN_SPARE_AREA
1902 | 2 << WR_RD_BSY_GAP
1903 | wide_bus << WIDE_FLASH
1904 | host->bch_enabled << ENABLE_BCH_ECC;
1905
1906 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
1907 | host->cw_size << UD_SIZE_BYTES
1908 | 5 << NUM_ADDR_CYCLES
1909 | 0 << SPARE_SIZE_BYTES;
1910
1911 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
1912 | 0 << CS_ACTIVE_BSY
1913 | 17 << BAD_BLOCK_BYTE_NUM
1914 | 1 << BAD_BLOCK_IN_SPARE_AREA
1915 | 2 << WR_RD_BSY_GAP
1916 | wide_bus << WIDE_FLASH
1917 | 1 << DEV0_CFG1_ECC_DISABLE;
1918
Abhishek Sahu10777de2017-08-03 17:56:39 +02001919 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
Archit Tanejac76b78d2016-02-03 14:29:50 +05301920 | 0 << ECC_SW_RESET
1921 | host->cw_data << ECC_NUM_DATA_BYTES
1922 | 1 << ECC_FORCE_CLK_OPEN
1923 | ecc_mode << ECC_MODE
1924 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
1925
1926 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
1927
1928 host->clrflashstatus = FS_READY_BSY_N;
1929 host->clrreadstatus = 0xc0;
1930
1931 dev_dbg(nandc->dev,
1932 "cfg0 %x cfg1 %x ecc_buf_cfg %x ecc_bch cfg %x cw_size %d cw_data %d strength %d parity_bytes %d steps %d\n",
1933 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
1934 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
1935 cwperpage);
1936
1937 return 0;
1938}
1939
1940static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
1941{
1942 int ret;
1943
1944 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
1945 if (ret) {
1946 dev_err(nandc->dev, "failed to set DMA mask\n");
1947 return ret;
1948 }
1949
1950 /*
1951 * we use the internal buffer for reading ONFI params, reading small
1952 * data like ID and status, and preforming read-copy-write operations
1953 * when writing to a codeword partially. 532 is the maximum possible
1954 * size of a codeword for our nand controller
1955 */
1956 nandc->buf_size = 532;
1957
1958 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
1959 GFP_KERNEL);
1960 if (!nandc->data_buffer)
1961 return -ENOMEM;
1962
1963 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
1964 GFP_KERNEL);
1965 if (!nandc->regs)
1966 return -ENOMEM;
1967
1968 nandc->reg_read_buf = devm_kzalloc(nandc->dev,
1969 MAX_REG_RD * sizeof(*nandc->reg_read_buf),
1970 GFP_KERNEL);
1971 if (!nandc->reg_read_buf)
1972 return -ENOMEM;
1973
1974 nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
1975 if (!nandc->chan) {
1976 dev_err(nandc->dev, "failed to request slave channel\n");
1977 return -ENODEV;
1978 }
1979
1980 INIT_LIST_HEAD(&nandc->desc_list);
1981 INIT_LIST_HEAD(&nandc->host_list);
1982
Marc Gonzalezd45bc582016-07-27 11:23:52 +02001983 nand_hw_control_init(&nandc->controller);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301984
1985 return 0;
1986}
1987
1988static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
1989{
1990 dma_release_channel(nandc->chan);
1991}
1992
1993/* one time setup of a few nand controller registers */
1994static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
1995{
1996 /* kill onenand */
1997 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
1998
1999 /* enable ADM DMA */
2000 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2001
2002 /* save the original values of these registers */
2003 nandc->cmd1 = nandc_read(nandc, NAND_DEV_CMD1);
2004 nandc->vld = nandc_read(nandc, NAND_DEV_CMD_VLD);
2005
2006 return 0;
2007}
2008
2009static int qcom_nand_host_init(struct qcom_nand_controller *nandc,
2010 struct qcom_nand_host *host,
2011 struct device_node *dn)
2012{
2013 struct nand_chip *chip = &host->chip;
2014 struct mtd_info *mtd = nand_to_mtd(chip);
2015 struct device *dev = nandc->dev;
2016 int ret;
2017
2018 ret = of_property_read_u32(dn, "reg", &host->cs);
2019 if (ret) {
2020 dev_err(dev, "can't get chip-select\n");
2021 return -ENXIO;
2022 }
2023
2024 nand_set_flash_node(chip, dn);
2025 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
2026 mtd->owner = THIS_MODULE;
2027 mtd->dev.parent = dev;
2028
2029 chip->cmdfunc = qcom_nandc_command;
2030 chip->select_chip = qcom_nandc_select_chip;
2031 chip->read_byte = qcom_nandc_read_byte;
2032 chip->read_buf = qcom_nandc_read_buf;
2033 chip->write_buf = qcom_nandc_write_buf;
Boris Brezillon4a78cc62017-05-26 17:10:15 +02002034 chip->onfi_set_features = nand_onfi_get_set_features_notsupp;
2035 chip->onfi_get_features = nand_onfi_get_set_features_notsupp;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302036
2037 /*
2038 * the bad block marker is readable only when we read the last codeword
2039 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2040 * helpers don't allow us to read BB from a nand chip with ECC
2041 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2042 * and block_markbad helpers until we permanently switch to using
2043 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2044 */
2045 chip->block_bad = qcom_nandc_block_bad;
2046 chip->block_markbad = qcom_nandc_block_markbad;
2047
2048 chip->controller = &nandc->controller;
2049 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
2050 NAND_SKIP_BBTSCAN;
2051
2052 /* set up initial status value */
2053 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2054
2055 ret = nand_scan_ident(mtd, 1, NULL);
2056 if (ret)
2057 return ret;
2058
2059 ret = qcom_nand_host_setup(host);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302060
2061 return ret;
2062}
2063
2064static int qcom_nand_mtd_register(struct qcom_nand_controller *nandc,
2065 struct qcom_nand_host *host,
2066 struct device_node *dn)
2067{
2068 struct nand_chip *chip = &host->chip;
2069 struct mtd_info *mtd = nand_to_mtd(chip);
2070 int ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302071
2072 ret = nand_scan_tail(mtd);
2073 if (ret)
2074 return ret;
2075
Abhishek Sahu89f51272017-07-19 17:17:58 +05302076 ret = mtd_device_register(mtd, NULL, 0);
2077 if (ret)
2078 nand_cleanup(mtd_to_nand(mtd));
2079
2080 return ret;
2081}
2082
2083static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2084{
2085 struct device *dev = nandc->dev;
2086 struct device_node *dn = dev->of_node, *child;
2087 struct qcom_nand_host *host, *tmp;
2088 int ret;
2089
2090 for_each_available_child_of_node(dn, child) {
2091 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2092 if (!host) {
2093 of_node_put(child);
2094 return -ENOMEM;
2095 }
2096
2097 ret = qcom_nand_host_init(nandc, host, child);
2098 if (ret) {
2099 devm_kfree(dev, host);
2100 continue;
2101 }
2102
2103 list_add_tail(&host->node, &nandc->host_list);
2104 }
2105
2106 if (list_empty(&nandc->host_list))
2107 return -ENODEV;
2108
2109 list_for_each_entry_safe(host, tmp, &nandc->host_list, node) {
2110 ret = qcom_nand_mtd_register(nandc, host, child);
2111 if (ret) {
2112 list_del(&host->node);
2113 devm_kfree(dev, host);
2114 }
2115 }
2116
2117 if (list_empty(&nandc->host_list))
2118 return -ENODEV;
2119
2120 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302121}
2122
2123/* parse custom DT properties here */
2124static int qcom_nandc_parse_dt(struct platform_device *pdev)
2125{
2126 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2127 struct device_node *np = nandc->dev->of_node;
2128 int ret;
2129
2130 ret = of_property_read_u32(np, "qcom,cmd-crci", &nandc->cmd_crci);
2131 if (ret) {
2132 dev_err(nandc->dev, "command CRCI unspecified\n");
2133 return ret;
2134 }
2135
2136 ret = of_property_read_u32(np, "qcom,data-crci", &nandc->data_crci);
2137 if (ret) {
2138 dev_err(nandc->dev, "data CRCI unspecified\n");
2139 return ret;
2140 }
2141
2142 return 0;
2143}
2144
2145static int qcom_nandc_probe(struct platform_device *pdev)
2146{
2147 struct qcom_nand_controller *nandc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302148 const void *dev_data;
2149 struct device *dev = &pdev->dev;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302150 struct resource *res;
2151 int ret;
2152
2153 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2154 if (!nandc)
2155 return -ENOMEM;
2156
2157 platform_set_drvdata(pdev, nandc);
2158 nandc->dev = dev;
2159
2160 dev_data = of_device_get_match_data(dev);
2161 if (!dev_data) {
2162 dev_err(&pdev->dev, "failed to get device data\n");
2163 return -ENODEV;
2164 }
2165
2166 nandc->ecc_modes = (unsigned long)dev_data;
2167
2168 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2169 nandc->base = devm_ioremap_resource(dev, res);
2170 if (IS_ERR(nandc->base))
2171 return PTR_ERR(nandc->base);
2172
2173 nandc->base_dma = phys_to_dma(dev, (phys_addr_t)res->start);
2174
2175 nandc->core_clk = devm_clk_get(dev, "core");
2176 if (IS_ERR(nandc->core_clk))
2177 return PTR_ERR(nandc->core_clk);
2178
2179 nandc->aon_clk = devm_clk_get(dev, "aon");
2180 if (IS_ERR(nandc->aon_clk))
2181 return PTR_ERR(nandc->aon_clk);
2182
2183 ret = qcom_nandc_parse_dt(pdev);
2184 if (ret)
2185 return ret;
2186
2187 ret = qcom_nandc_alloc(nandc);
2188 if (ret)
2189 return ret;
2190
2191 ret = clk_prepare_enable(nandc->core_clk);
2192 if (ret)
2193 goto err_core_clk;
2194
2195 ret = clk_prepare_enable(nandc->aon_clk);
2196 if (ret)
2197 goto err_aon_clk;
2198
2199 ret = qcom_nandc_setup(nandc);
2200 if (ret)
2201 goto err_setup;
2202
Abhishek Sahu89f51272017-07-19 17:17:58 +05302203 ret = qcom_probe_nand_devices(nandc);
2204 if (ret)
2205 goto err_setup;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302206
2207 return 0;
2208
Archit Tanejac76b78d2016-02-03 14:29:50 +05302209err_setup:
2210 clk_disable_unprepare(nandc->aon_clk);
2211err_aon_clk:
2212 clk_disable_unprepare(nandc->core_clk);
2213err_core_clk:
2214 qcom_nandc_unalloc(nandc);
2215
2216 return ret;
2217}
2218
2219static int qcom_nandc_remove(struct platform_device *pdev)
2220{
2221 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2222 struct qcom_nand_host *host;
2223
2224 list_for_each_entry(host, &nandc->host_list, node)
2225 nand_release(nand_to_mtd(&host->chip));
2226
2227 qcom_nandc_unalloc(nandc);
2228
2229 clk_disable_unprepare(nandc->aon_clk);
2230 clk_disable_unprepare(nandc->core_clk);
2231
2232 return 0;
2233}
2234
2235#define EBI2_NANDC_ECC_MODES (ECC_RS_4BIT | ECC_BCH_8BIT)
2236
2237/*
2238 * data will hold a struct pointer containing more differences once we support
2239 * more controller variants
2240 */
2241static const struct of_device_id qcom_nandc_of_match[] = {
2242 { .compatible = "qcom,ipq806x-nand",
2243 .data = (void *)EBI2_NANDC_ECC_MODES,
2244 },
2245 {}
2246};
2247MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
2248
2249static struct platform_driver qcom_nandc_driver = {
2250 .driver = {
2251 .name = "qcom-nandc",
2252 .of_match_table = qcom_nandc_of_match,
2253 },
2254 .probe = qcom_nandc_probe,
2255 .remove = qcom_nandc_remove,
2256};
2257module_platform_driver(qcom_nandc_driver);
2258
2259MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
2260MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
2261MODULE_LICENSE("GPL v2");