blob: 97a4c080ce7af81c52344b9a54b5713f08f4e03e [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
Archit Tanejac76b78d2016-02-03 14:29:50 +0530638static void config_cw_write_pre(struct qcom_nand_controller *nandc)
639{
640 write_reg_dma(nandc, NAND_FLASH_CMD, 3);
641 write_reg_dma(nandc, NAND_DEV0_CFG0, 3);
642 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1);
643}
644
645static void config_cw_write_post(struct qcom_nand_controller *nandc)
646{
647 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
648
649 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
650
651 write_reg_dma(nandc, NAND_FLASH_STATUS, 1);
652 write_reg_dma(nandc, NAND_READ_STATUS, 1);
653}
654
655/*
656 * the following functions are used within chip->cmdfunc() to perform different
657 * NAND_CMD_* commands
658 */
659
660/* sets up descriptors for NAND_CMD_PARAM */
661static int nandc_param(struct qcom_nand_host *host)
662{
663 struct nand_chip *chip = &host->chip;
664 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
665
666 /*
667 * NAND_CMD_PARAM is called before we know much about the FLASH chip
668 * in use. we configure the controller to perform a raw read of 512
669 * bytes to read onfi params
670 */
671 nandc_set_reg(nandc, NAND_FLASH_CMD, PAGE_READ | PAGE_ACC | LAST_PAGE);
672 nandc_set_reg(nandc, NAND_ADDR0, 0);
673 nandc_set_reg(nandc, NAND_ADDR1, 0);
674 nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
675 | 512 << UD_SIZE_BYTES
676 | 5 << NUM_ADDR_CYCLES
677 | 0 << SPARE_SIZE_BYTES);
678 nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
679 | 0 << CS_ACTIVE_BSY
680 | 17 << BAD_BLOCK_BYTE_NUM
681 | 1 << BAD_BLOCK_IN_SPARE_AREA
682 | 2 << WR_RD_BSY_GAP
683 | 0 << WIDE_FLASH
684 | 1 << DEV0_CFG1_ECC_DISABLE);
685 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
686
687 /* configure CMD1 and VLD for ONFI param probing */
688 nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
689 (nandc->vld & ~(1 << READ_START_VLD))
690 | 0 << READ_START_VLD);
691 nandc_set_reg(nandc, NAND_DEV_CMD1,
692 (nandc->cmd1 & ~(0xFF << READ_ADDR))
693 | NAND_CMD_PARAM << READ_ADDR);
694
695 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
696
697 nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
698 nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
699
700 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1);
701 write_reg_dma(nandc, NAND_DEV_CMD1, 1);
702
703 nandc->buf_count = 512;
704 memset(nandc->data_buffer, 0xff, nandc->buf_count);
705
Abhishek Sahubde43302017-07-19 17:17:55 +0530706 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530707
708 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
709 nandc->buf_count);
710
711 /* restore CMD1 and VLD regs */
712 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1);
713 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1);
714
715 return 0;
716}
717
718/* sets up descriptors for NAND_CMD_ERASE1 */
719static int erase_block(struct qcom_nand_host *host, int page_addr)
720{
721 struct nand_chip *chip = &host->chip;
722 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
723
724 nandc_set_reg(nandc, NAND_FLASH_CMD,
725 BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
726 nandc_set_reg(nandc, NAND_ADDR0, page_addr);
727 nandc_set_reg(nandc, NAND_ADDR1, 0);
728 nandc_set_reg(nandc, NAND_DEV0_CFG0,
729 host->cfg0_raw & ~(7 << CW_PER_PAGE));
730 nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
731 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
732 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
733 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
734
735 write_reg_dma(nandc, NAND_FLASH_CMD, 3);
736 write_reg_dma(nandc, NAND_DEV0_CFG0, 2);
737 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
738
739 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
740
741 write_reg_dma(nandc, NAND_FLASH_STATUS, 1);
742 write_reg_dma(nandc, NAND_READ_STATUS, 1);
743
744 return 0;
745}
746
747/* sets up descriptors for NAND_CMD_READID */
748static int read_id(struct qcom_nand_host *host, int column)
749{
750 struct nand_chip *chip = &host->chip;
751 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
752
753 if (column == -1)
754 return 0;
755
756 nandc_set_reg(nandc, NAND_FLASH_CMD, FETCH_ID);
757 nandc_set_reg(nandc, NAND_ADDR0, column);
758 nandc_set_reg(nandc, NAND_ADDR1, 0);
759 nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
760 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
761
762 write_reg_dma(nandc, NAND_FLASH_CMD, 4);
763 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
764
765 read_reg_dma(nandc, NAND_READ_ID, 1);
766
767 return 0;
768}
769
770/* sets up descriptors for NAND_CMD_RESET */
771static int reset(struct qcom_nand_host *host)
772{
773 struct nand_chip *chip = &host->chip;
774 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
775
776 nandc_set_reg(nandc, NAND_FLASH_CMD, RESET_DEVICE);
777 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
778
779 write_reg_dma(nandc, NAND_FLASH_CMD, 1);
780 write_reg_dma(nandc, NAND_EXEC_CMD, 1);
781
782 read_reg_dma(nandc, NAND_FLASH_STATUS, 1);
783
784 return 0;
785}
786
787/* helpers to submit/free our list of dma descriptors */
788static int submit_descs(struct qcom_nand_controller *nandc)
789{
790 struct desc_info *desc;
791 dma_cookie_t cookie = 0;
792
793 list_for_each_entry(desc, &nandc->desc_list, node)
794 cookie = dmaengine_submit(desc->dma_desc);
795
796 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
797 return -ETIMEDOUT;
798
799 return 0;
800}
801
802static void free_descs(struct qcom_nand_controller *nandc)
803{
804 struct desc_info *desc, *n;
805
806 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
807 list_del(&desc->node);
808 dma_unmap_sg(nandc->dev, &desc->sgl, 1, desc->dir);
809 kfree(desc);
810 }
811}
812
813/* reset the register read buffer for next NAND operation */
814static void clear_read_regs(struct qcom_nand_controller *nandc)
815{
816 nandc->reg_read_pos = 0;
817 memset(nandc->reg_read_buf, 0,
818 MAX_REG_RD * sizeof(*nandc->reg_read_buf));
819}
820
821static void pre_command(struct qcom_nand_host *host, int command)
822{
823 struct nand_chip *chip = &host->chip;
824 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
825
826 nandc->buf_count = 0;
827 nandc->buf_start = 0;
828 host->use_ecc = false;
829 host->last_command = command;
830
831 clear_read_regs(nandc);
832}
833
834/*
835 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
836 * privately maintained status byte, this status byte can be read after
837 * NAND_CMD_STATUS is called
838 */
839static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
840{
841 struct nand_chip *chip = &host->chip;
842 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
843 struct nand_ecc_ctrl *ecc = &chip->ecc;
844 int num_cw;
845 int i;
846
847 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
848
849 for (i = 0; i < num_cw; i++) {
850 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
851
852 if (flash_status & FS_MPU_ERR)
853 host->status &= ~NAND_STATUS_WP;
854
855 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
856 (flash_status &
857 FS_DEVICE_STS_ERR)))
858 host->status |= NAND_STATUS_FAIL;
859 }
860}
861
862static void post_command(struct qcom_nand_host *host, int command)
863{
864 struct nand_chip *chip = &host->chip;
865 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
866
867 switch (command) {
868 case NAND_CMD_READID:
869 memcpy(nandc->data_buffer, nandc->reg_read_buf,
870 nandc->buf_count);
871 break;
872 case NAND_CMD_PAGEPROG:
873 case NAND_CMD_ERASE1:
874 parse_erase_write_errors(host, command);
875 break;
876 default:
877 break;
878 }
879}
880
881/*
882 * Implements chip->cmdfunc. It's only used for a limited set of commands.
883 * The rest of the commands wouldn't be called by upper layers. For example,
884 * NAND_CMD_READOOB would never be called because we have our own versions
885 * of read_oob ops for nand_ecc_ctrl.
886 */
887static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
888 int column, int page_addr)
889{
890 struct nand_chip *chip = mtd_to_nand(mtd);
891 struct qcom_nand_host *host = to_qcom_nand_host(chip);
892 struct nand_ecc_ctrl *ecc = &chip->ecc;
893 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
894 bool wait = false;
895 int ret = 0;
896
897 pre_command(host, command);
898
899 switch (command) {
900 case NAND_CMD_RESET:
901 ret = reset(host);
902 wait = true;
903 break;
904
905 case NAND_CMD_READID:
906 nandc->buf_count = 4;
907 ret = read_id(host, column);
908 wait = true;
909 break;
910
911 case NAND_CMD_PARAM:
912 ret = nandc_param(host);
913 wait = true;
914 break;
915
916 case NAND_CMD_ERASE1:
917 ret = erase_block(host, page_addr);
918 wait = true;
919 break;
920
921 case NAND_CMD_READ0:
922 /* we read the entire page for now */
923 WARN_ON(column != 0);
924
925 host->use_ecc = true;
926 set_address(host, 0, page_addr);
927 update_rw_regs(host, ecc->steps, true);
928 break;
929
930 case NAND_CMD_SEQIN:
931 WARN_ON(column != 0);
932 set_address(host, 0, page_addr);
933 break;
934
935 case NAND_CMD_PAGEPROG:
936 case NAND_CMD_STATUS:
937 case NAND_CMD_NONE:
938 default:
939 break;
940 }
941
942 if (ret) {
943 dev_err(nandc->dev, "failure executing command %d\n",
944 command);
945 free_descs(nandc);
946 return;
947 }
948
949 if (wait) {
950 ret = submit_descs(nandc);
951 if (ret)
952 dev_err(nandc->dev,
953 "failure submitting descs for command %d\n",
954 command);
955 }
956
957 free_descs(nandc);
958
959 post_command(host, command);
960}
961
962/*
963 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
964 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
965 *
966 * when using RS ECC, the HW reports the same erros when reading an erased CW,
967 * but it notifies that it is an erased CW by placing special characters at
968 * certain offsets in the buffer.
969 *
970 * verify if the page is erased or not, and fix up the page for RS ECC by
971 * replacing the special characters with 0xff.
972 */
973static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
974{
975 u8 empty1, empty2;
976
977 /*
978 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
979 * is erased by looking for 0x54s at offsets 3 and 175 from the
980 * beginning of each codeword
981 */
982
983 empty1 = data_buf[3];
984 empty2 = data_buf[175];
985
986 /*
987 * if the erased codework markers, if they exist override them with
988 * 0xffs
989 */
990 if ((empty1 == 0x54 && empty2 == 0xff) ||
991 (empty1 == 0xff && empty2 == 0x54)) {
992 data_buf[3] = 0xff;
993 data_buf[175] = 0xff;
994 }
995
996 /*
997 * check if the entire chunk contains 0xffs or not. if it doesn't, then
998 * restore the original values at the special offsets
999 */
1000 if (memchr_inv(data_buf, 0xff, data_len)) {
1001 data_buf[3] = empty1;
1002 data_buf[175] = empty2;
1003
1004 return false;
1005 }
1006
1007 return true;
1008}
1009
1010struct read_stats {
1011 __le32 flash;
1012 __le32 buffer;
1013 __le32 erased_cw;
1014};
1015
1016/*
1017 * reads back status registers set by the controller to notify page read
1018 * errors. this is equivalent to what 'ecc->correct()' would do.
1019 */
1020static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1021 u8 *oob_buf)
1022{
1023 struct nand_chip *chip = &host->chip;
1024 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1025 struct mtd_info *mtd = nand_to_mtd(chip);
1026 struct nand_ecc_ctrl *ecc = &chip->ecc;
1027 unsigned int max_bitflips = 0;
1028 struct read_stats *buf;
1029 int i;
1030
1031 buf = (struct read_stats *)nandc->reg_read_buf;
1032
1033 for (i = 0; i < ecc->steps; i++, buf++) {
1034 u32 flash, buffer, erased_cw;
1035 int data_len, oob_len;
1036
1037 if (i == (ecc->steps - 1)) {
1038 data_len = ecc->size - ((ecc->steps - 1) << 2);
1039 oob_len = ecc->steps << 2;
1040 } else {
1041 data_len = host->cw_data;
1042 oob_len = 0;
1043 }
1044
1045 flash = le32_to_cpu(buf->flash);
1046 buffer = le32_to_cpu(buf->buffer);
1047 erased_cw = le32_to_cpu(buf->erased_cw);
1048
1049 if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1050 bool erased;
1051
1052 /* ignore erased codeword errors */
1053 if (host->bch_enabled) {
1054 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1055 true : false;
1056 } else {
1057 erased = erased_chunk_check_and_fixup(data_buf,
1058 data_len);
1059 }
1060
1061 if (erased) {
1062 data_buf += data_len;
1063 if (oob_buf)
1064 oob_buf += oob_len + ecc->bytes;
1065 continue;
1066 }
1067
1068 if (buffer & BS_UNCORRECTABLE_BIT) {
1069 int ret, ecclen, extraooblen;
1070 void *eccbuf;
1071
1072 eccbuf = oob_buf ? oob_buf + oob_len : NULL;
1073 ecclen = oob_buf ? host->ecc_bytes_hw : 0;
1074 extraooblen = oob_buf ? oob_len : 0;
1075
1076 /*
1077 * make sure it isn't an erased page reported
1078 * as not-erased by HW because of a few bitflips
1079 */
1080 ret = nand_check_erased_ecc_chunk(data_buf,
1081 data_len, eccbuf, ecclen, oob_buf,
1082 extraooblen, ecc->strength);
1083 if (ret < 0) {
1084 mtd->ecc_stats.failed++;
1085 } else {
1086 mtd->ecc_stats.corrected += ret;
1087 max_bitflips =
1088 max_t(unsigned int, max_bitflips, ret);
1089 }
1090 }
1091 } else {
1092 unsigned int stat;
1093
1094 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1095 mtd->ecc_stats.corrected += stat;
1096 max_bitflips = max(max_bitflips, stat);
1097 }
1098
1099 data_buf += data_len;
1100 if (oob_buf)
1101 oob_buf += oob_len + ecc->bytes;
1102 }
1103
1104 return max_bitflips;
1105}
1106
1107/*
1108 * helper to perform the actual page read operation, used by ecc->read_page(),
1109 * ecc->read_oob()
1110 */
1111static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1112 u8 *oob_buf)
1113{
1114 struct nand_chip *chip = &host->chip;
1115 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1116 struct nand_ecc_ctrl *ecc = &chip->ecc;
1117 int i, ret;
1118
Abhishek Sahubde43302017-07-19 17:17:55 +05301119 config_nand_page_read(nandc);
1120
Archit Tanejac76b78d2016-02-03 14:29:50 +05301121 /* queue cmd descs for each codeword */
1122 for (i = 0; i < ecc->steps; i++) {
1123 int data_size, oob_size;
1124
1125 if (i == (ecc->steps - 1)) {
1126 data_size = ecc->size - ((ecc->steps - 1) << 2);
1127 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1128 host->spare_bytes;
1129 } else {
1130 data_size = host->cw_data;
1131 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1132 }
1133
Abhishek Sahubde43302017-07-19 17:17:55 +05301134 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301135
1136 if (data_buf)
1137 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
1138 data_size);
1139
1140 /*
1141 * when ecc is enabled, the controller doesn't read the real
1142 * or dummy bad block markers in each chunk. To maintain a
1143 * consistent layout across RAW and ECC reads, we just
1144 * leave the real/dummy BBM offsets empty (i.e, filled with
1145 * 0xffs)
1146 */
1147 if (oob_buf) {
1148 int j;
1149
1150 for (j = 0; j < host->bbm_size; j++)
1151 *oob_buf++ = 0xff;
1152
1153 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
1154 oob_buf, oob_size);
1155 }
1156
1157 if (data_buf)
1158 data_buf += data_size;
1159 if (oob_buf)
1160 oob_buf += oob_size;
1161 }
1162
1163 ret = submit_descs(nandc);
1164 if (ret)
1165 dev_err(nandc->dev, "failure to read page/oob\n");
1166
1167 free_descs(nandc);
1168
1169 return ret;
1170}
1171
1172/*
1173 * a helper that copies the last step/codeword of a page (containing free oob)
1174 * into our local buffer
1175 */
1176static int copy_last_cw(struct qcom_nand_host *host, int page)
1177{
1178 struct nand_chip *chip = &host->chip;
1179 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1180 struct nand_ecc_ctrl *ecc = &chip->ecc;
1181 int size;
1182 int ret;
1183
1184 clear_read_regs(nandc);
1185
1186 size = host->use_ecc ? host->cw_data : host->cw_size;
1187
1188 /* prepare a clean read buffer */
1189 memset(nandc->data_buffer, 0xff, size);
1190
1191 set_address(host, host->cw_size * (ecc->steps - 1), page);
1192 update_rw_regs(host, 1, true);
1193
Abhishek Sahubde43302017-07-19 17:17:55 +05301194 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301195
1196 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size);
1197
1198 ret = submit_descs(nandc);
1199 if (ret)
1200 dev_err(nandc->dev, "failed to copy last codeword\n");
1201
1202 free_descs(nandc);
1203
1204 return ret;
1205}
1206
1207/* implements ecc->read_page() */
1208static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1209 uint8_t *buf, int oob_required, int page)
1210{
1211 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1212 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1213 u8 *data_buf, *oob_buf = NULL;
1214 int ret;
1215
1216 data_buf = buf;
1217 oob_buf = oob_required ? chip->oob_poi : NULL;
1218
1219 ret = read_page_ecc(host, data_buf, oob_buf);
1220 if (ret) {
1221 dev_err(nandc->dev, "failure to read page\n");
1222 return ret;
1223 }
1224
1225 return parse_read_errors(host, data_buf, oob_buf);
1226}
1227
1228/* implements ecc->read_page_raw() */
1229static int qcom_nandc_read_page_raw(struct mtd_info *mtd,
1230 struct nand_chip *chip, uint8_t *buf,
1231 int oob_required, int page)
1232{
1233 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1234 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1235 u8 *data_buf, *oob_buf;
1236 struct nand_ecc_ctrl *ecc = &chip->ecc;
1237 int i, ret;
1238
1239 data_buf = buf;
1240 oob_buf = chip->oob_poi;
1241
1242 host->use_ecc = false;
1243 update_rw_regs(host, ecc->steps, true);
Abhishek Sahubde43302017-07-19 17:17:55 +05301244 config_nand_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301245
1246 for (i = 0; i < ecc->steps; i++) {
1247 int data_size1, data_size2, oob_size1, oob_size2;
1248 int reg_off = FLASH_BUF_ACC;
1249
1250 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1251 oob_size1 = host->bbm_size;
1252
1253 if (i == (ecc->steps - 1)) {
1254 data_size2 = ecc->size - data_size1 -
1255 ((ecc->steps - 1) << 2);
1256 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1257 host->spare_bytes;
1258 } else {
1259 data_size2 = host->cw_data - data_size1;
1260 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1261 }
1262
Abhishek Sahubde43302017-07-19 17:17:55 +05301263 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301264
1265 read_data_dma(nandc, reg_off, data_buf, data_size1);
1266 reg_off += data_size1;
1267 data_buf += data_size1;
1268
1269 read_data_dma(nandc, reg_off, oob_buf, oob_size1);
1270 reg_off += oob_size1;
1271 oob_buf += oob_size1;
1272
1273 read_data_dma(nandc, reg_off, data_buf, data_size2);
1274 reg_off += data_size2;
1275 data_buf += data_size2;
1276
1277 read_data_dma(nandc, reg_off, oob_buf, oob_size2);
1278 oob_buf += oob_size2;
1279 }
1280
1281 ret = submit_descs(nandc);
1282 if (ret)
1283 dev_err(nandc->dev, "failure to read raw page\n");
1284
1285 free_descs(nandc);
1286
1287 return 0;
1288}
1289
1290/* implements ecc->read_oob() */
1291static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1292 int page)
1293{
1294 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1295 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1296 struct nand_ecc_ctrl *ecc = &chip->ecc;
1297 int ret;
1298
1299 clear_read_regs(nandc);
1300
1301 host->use_ecc = true;
1302 set_address(host, 0, page);
1303 update_rw_regs(host, ecc->steps, true);
1304
1305 ret = read_page_ecc(host, NULL, chip->oob_poi);
1306 if (ret)
1307 dev_err(nandc->dev, "failure to read oob\n");
1308
1309 return ret;
1310}
1311
1312/* implements ecc->write_page() */
1313static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1314 const uint8_t *buf, int oob_required, int page)
1315{
1316 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1317 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1318 struct nand_ecc_ctrl *ecc = &chip->ecc;
1319 u8 *data_buf, *oob_buf;
1320 int i, ret;
1321
1322 clear_read_regs(nandc);
1323
1324 data_buf = (u8 *)buf;
1325 oob_buf = chip->oob_poi;
1326
1327 host->use_ecc = true;
1328 update_rw_regs(host, ecc->steps, false);
1329
1330 for (i = 0; i < ecc->steps; i++) {
1331 int data_size, oob_size;
1332
1333 if (i == (ecc->steps - 1)) {
1334 data_size = ecc->size - ((ecc->steps - 1) << 2);
1335 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1336 host->spare_bytes;
1337 } else {
1338 data_size = host->cw_data;
1339 oob_size = ecc->bytes;
1340 }
1341
1342 config_cw_write_pre(nandc);
1343
1344 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size);
1345
1346 /*
1347 * when ECC is enabled, we don't really need to write anything
1348 * to oob for the first n - 1 codewords since these oob regions
1349 * just contain ECC bytes that's written by the controller
1350 * itself. For the last codeword, we skip the bbm positions and
1351 * write to the free oob area.
1352 */
1353 if (i == (ecc->steps - 1)) {
1354 oob_buf += host->bbm_size;
1355
1356 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
1357 oob_buf, oob_size);
1358 }
1359
1360 config_cw_write_post(nandc);
1361
1362 data_buf += data_size;
1363 oob_buf += oob_size;
1364 }
1365
1366 ret = submit_descs(nandc);
1367 if (ret)
1368 dev_err(nandc->dev, "failure to write page\n");
1369
1370 free_descs(nandc);
1371
1372 return ret;
1373}
1374
1375/* implements ecc->write_page_raw() */
1376static int qcom_nandc_write_page_raw(struct mtd_info *mtd,
1377 struct nand_chip *chip, const uint8_t *buf,
1378 int oob_required, int page)
1379{
1380 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1381 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1382 struct nand_ecc_ctrl *ecc = &chip->ecc;
1383 u8 *data_buf, *oob_buf;
1384 int i, ret;
1385
1386 clear_read_regs(nandc);
1387
1388 data_buf = (u8 *)buf;
1389 oob_buf = chip->oob_poi;
1390
1391 host->use_ecc = false;
1392 update_rw_regs(host, ecc->steps, false);
1393
1394 for (i = 0; i < ecc->steps; i++) {
1395 int data_size1, data_size2, oob_size1, oob_size2;
1396 int reg_off = FLASH_BUF_ACC;
1397
1398 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1399 oob_size1 = host->bbm_size;
1400
1401 if (i == (ecc->steps - 1)) {
1402 data_size2 = ecc->size - data_size1 -
1403 ((ecc->steps - 1) << 2);
1404 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1405 host->spare_bytes;
1406 } else {
1407 data_size2 = host->cw_data - data_size1;
1408 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1409 }
1410
1411 config_cw_write_pre(nandc);
1412
1413 write_data_dma(nandc, reg_off, data_buf, data_size1);
1414 reg_off += data_size1;
1415 data_buf += data_size1;
1416
1417 write_data_dma(nandc, reg_off, oob_buf, oob_size1);
1418 reg_off += oob_size1;
1419 oob_buf += oob_size1;
1420
1421 write_data_dma(nandc, reg_off, data_buf, data_size2);
1422 reg_off += data_size2;
1423 data_buf += data_size2;
1424
1425 write_data_dma(nandc, reg_off, oob_buf, oob_size2);
1426 oob_buf += oob_size2;
1427
1428 config_cw_write_post(nandc);
1429 }
1430
1431 ret = submit_descs(nandc);
1432 if (ret)
1433 dev_err(nandc->dev, "failure to write raw page\n");
1434
1435 free_descs(nandc);
1436
1437 return ret;
1438}
1439
1440/*
1441 * implements ecc->write_oob()
1442 *
1443 * the NAND controller cannot write only data or only oob within a codeword,
1444 * since ecc is calculated for the combined codeword. we first copy the
1445 * entire contents for the last codeword(data + oob), replace the old oob
1446 * with the new one in chip->oob_poi, and then write the entire codeword.
1447 * this read-copy-write operation results in a slight performance loss.
1448 */
1449static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1450 int page)
1451{
1452 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1453 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1454 struct nand_ecc_ctrl *ecc = &chip->ecc;
1455 u8 *oob = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301456 int data_size, oob_size;
1457 int ret, status = 0;
1458
1459 host->use_ecc = true;
1460
1461 ret = copy_last_cw(host, page);
1462 if (ret)
1463 return ret;
1464
1465 clear_read_regs(nandc);
1466
1467 /* calculate the data and oob size for the last codeword/step */
1468 data_size = ecc->size - ((ecc->steps - 1) << 2);
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001469 oob_size = mtd->oobavail;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301470
1471 /* override new oob content to last codeword */
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001472 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
1473 0, mtd->oobavail);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301474
1475 set_address(host, host->cw_size * (ecc->steps - 1), page);
1476 update_rw_regs(host, 1, false);
1477
1478 config_cw_write_pre(nandc);
1479 write_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
1480 data_size + oob_size);
1481 config_cw_write_post(nandc);
1482
1483 ret = submit_descs(nandc);
1484
1485 free_descs(nandc);
1486
1487 if (ret) {
1488 dev_err(nandc->dev, "failure to write oob\n");
1489 return -EIO;
1490 }
1491
1492 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1493
1494 status = chip->waitfunc(mtd, chip);
1495
1496 return status & NAND_STATUS_FAIL ? -EIO : 0;
1497}
1498
1499static int qcom_nandc_block_bad(struct mtd_info *mtd, loff_t ofs)
1500{
1501 struct nand_chip *chip = mtd_to_nand(mtd);
1502 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1503 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1504 struct nand_ecc_ctrl *ecc = &chip->ecc;
1505 int page, ret, bbpos, bad = 0;
1506 u32 flash_status;
1507
1508 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1509
1510 /*
1511 * configure registers for a raw sub page read, the address is set to
1512 * the beginning of the last codeword, we don't care about reading ecc
1513 * portion of oob. we just want the first few bytes from this codeword
1514 * that contains the BBM
1515 */
1516 host->use_ecc = false;
1517
1518 ret = copy_last_cw(host, page);
1519 if (ret)
1520 goto err;
1521
1522 flash_status = le32_to_cpu(nandc->reg_read_buf[0]);
1523
1524 if (flash_status & (FS_OP_ERR | FS_MPU_ERR)) {
1525 dev_warn(nandc->dev, "error when trying to read BBM\n");
1526 goto err;
1527 }
1528
1529 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
1530
1531 bad = nandc->data_buffer[bbpos] != 0xff;
1532
1533 if (chip->options & NAND_BUSWIDTH_16)
1534 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
1535err:
1536 return bad;
1537}
1538
1539static int qcom_nandc_block_markbad(struct mtd_info *mtd, loff_t ofs)
1540{
1541 struct nand_chip *chip = mtd_to_nand(mtd);
1542 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1543 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1544 struct nand_ecc_ctrl *ecc = &chip->ecc;
1545 int page, ret, status = 0;
1546
1547 clear_read_regs(nandc);
1548
1549 /*
1550 * to mark the BBM as bad, we flash the entire last codeword with 0s.
1551 * we don't care about the rest of the content in the codeword since
1552 * we aren't going to use this block again
1553 */
1554 memset(nandc->data_buffer, 0x00, host->cw_size);
1555
1556 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1557
1558 /* prepare write */
1559 host->use_ecc = false;
1560 set_address(host, host->cw_size * (ecc->steps - 1), page);
1561 update_rw_regs(host, 1, false);
1562
1563 config_cw_write_pre(nandc);
1564 write_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, host->cw_size);
1565 config_cw_write_post(nandc);
1566
1567 ret = submit_descs(nandc);
1568
1569 free_descs(nandc);
1570
1571 if (ret) {
1572 dev_err(nandc->dev, "failure to update BBM\n");
1573 return -EIO;
1574 }
1575
1576 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1577
1578 status = chip->waitfunc(mtd, chip);
1579
1580 return status & NAND_STATUS_FAIL ? -EIO : 0;
1581}
1582
1583/*
1584 * the three functions below implement chip->read_byte(), chip->read_buf()
1585 * and chip->write_buf() respectively. these aren't used for
1586 * reading/writing page data, they are used for smaller data like reading
1587 * id, status etc
1588 */
1589static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
1590{
1591 struct nand_chip *chip = mtd_to_nand(mtd);
1592 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1593 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1594 u8 *buf = nandc->data_buffer;
1595 u8 ret = 0x0;
1596
1597 if (host->last_command == NAND_CMD_STATUS) {
1598 ret = host->status;
1599
1600 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
1601
1602 return ret;
1603 }
1604
1605 if (nandc->buf_start < nandc->buf_count)
1606 ret = buf[nandc->buf_start++];
1607
1608 return ret;
1609}
1610
1611static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1612{
1613 struct nand_chip *chip = mtd_to_nand(mtd);
1614 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1615 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
1616
1617 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
1618 nandc->buf_start += real_len;
1619}
1620
1621static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
1622 int len)
1623{
1624 struct nand_chip *chip = mtd_to_nand(mtd);
1625 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1626 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
1627
1628 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
1629
1630 nandc->buf_start += real_len;
1631}
1632
1633/* we support only one external chip for now */
1634static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
1635{
1636 struct nand_chip *chip = mtd_to_nand(mtd);
1637 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1638
1639 if (chipnr <= 0)
1640 return;
1641
1642 dev_warn(nandc->dev, "invalid chip select\n");
1643}
1644
1645/*
1646 * NAND controller page layout info
1647 *
1648 * Layout with ECC enabled:
1649 *
1650 * |----------------------| |---------------------------------|
1651 * | xx.......yy| | *********xx.......yy|
1652 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
1653 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
1654 * | xx.......yy| | *********xx.......yy|
1655 * |----------------------| |---------------------------------|
1656 * codeword 1,2..n-1 codeword n
1657 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
1658 *
1659 * n = Number of codewords in the page
1660 * . = ECC bytes
1661 * * = Spare/free bytes
1662 * x = Unused byte(s)
1663 * y = Reserved byte(s)
1664 *
1665 * 2K page: n = 4, spare = 16 bytes
1666 * 4K page: n = 8, spare = 32 bytes
1667 * 8K page: n = 16, spare = 64 bytes
1668 *
1669 * the qcom nand controller operates at a sub page/codeword level. each
1670 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
1671 * the number of ECC bytes vary based on the ECC strength and the bus width.
1672 *
1673 * the first n - 1 codewords contains 516 bytes of user data, the remaining
1674 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
1675 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
1676 *
1677 * When we access a page with ECC enabled, the reserved bytes(s) are not
1678 * accessible at all. When reading, we fill up these unreadable positions
1679 * with 0xffs. When writing, the controller skips writing the inaccessible
1680 * bytes.
1681 *
1682 * Layout with ECC disabled:
1683 *
1684 * |------------------------------| |---------------------------------------|
1685 * | yy xx.......| | bb *********xx.......|
1686 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
1687 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
1688 * | yy xx.......| | bb *********xx.......|
1689 * |------------------------------| |---------------------------------------|
1690 * codeword 1,2..n-1 codeword n
1691 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
1692 *
1693 * n = Number of codewords in the page
1694 * . = ECC bytes
1695 * * = Spare/free bytes
1696 * x = Unused byte(s)
1697 * y = Dummy Bad Bock byte(s)
1698 * b = Real Bad Block byte(s)
1699 * size1/size2 = function of codeword size and 'n'
1700 *
1701 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
1702 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
1703 * Block Markers. In the last codeword, this position contains the real BBM
1704 *
1705 * In order to have a consistent layout between RAW and ECC modes, we assume
1706 * the following OOB layout arrangement:
1707 *
1708 * |-----------| |--------------------|
1709 * |yyxx.......| |bb*********xx.......|
1710 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
1711 * |yyxx.......| |bb*********xx.......|
1712 * |yyxx.......| |bb*********xx.......|
1713 * |-----------| |--------------------|
1714 * first n - 1 nth OOB region
1715 * OOB regions
1716 *
1717 * n = Number of codewords in the page
1718 * . = ECC bytes
1719 * * = FREE OOB bytes
1720 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
1721 * x = Unused byte(s)
1722 * b = Real bad block byte(s) (inaccessible when ECC enabled)
1723 *
1724 * This layout is read as is when ECC is disabled. When ECC is enabled, the
1725 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
1726 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
Boris Brezillon421e81c2016-03-18 17:54:27 +01001727 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
1728 * the sum of the three).
Archit Tanejac76b78d2016-02-03 14:29:50 +05301729 */
Boris Brezillon421e81c2016-03-18 17:54:27 +01001730static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
1731 struct mtd_oob_region *oobregion)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301732{
Boris Brezillon421e81c2016-03-18 17:54:27 +01001733 struct nand_chip *chip = mtd_to_nand(mtd);
1734 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301735 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301736
Boris Brezillon421e81c2016-03-18 17:54:27 +01001737 if (section > 1)
1738 return -ERANGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301739
Boris Brezillon421e81c2016-03-18 17:54:27 +01001740 if (!section) {
1741 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
1742 host->bbm_size;
1743 oobregion->offset = 0;
1744 } else {
1745 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
1746 oobregion->offset = mtd->oobsize - oobregion->length;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301747 }
1748
Boris Brezillon421e81c2016-03-18 17:54:27 +01001749 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301750}
1751
Boris Brezillon421e81c2016-03-18 17:54:27 +01001752static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
1753 struct mtd_oob_region *oobregion)
1754{
1755 struct nand_chip *chip = mtd_to_nand(mtd);
1756 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1757 struct nand_ecc_ctrl *ecc = &chip->ecc;
1758
1759 if (section)
1760 return -ERANGE;
1761
1762 oobregion->length = ecc->steps * 4;
1763 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
1764
1765 return 0;
1766}
1767
1768static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
1769 .ecc = qcom_nand_ooblayout_ecc,
1770 .free = qcom_nand_ooblayout_free,
1771};
1772
Archit Tanejac76b78d2016-02-03 14:29:50 +05301773static int qcom_nand_host_setup(struct qcom_nand_host *host)
1774{
1775 struct nand_chip *chip = &host->chip;
1776 struct mtd_info *mtd = nand_to_mtd(chip);
1777 struct nand_ecc_ctrl *ecc = &chip->ecc;
1778 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1779 int cwperpage, bad_block_byte;
1780 bool wide_bus;
1781 int ecc_mode = 1;
1782
1783 /*
1784 * the controller requires each step consists of 512 bytes of data.
1785 * bail out if DT has populated a wrong step size.
1786 */
1787 if (ecc->size != NANDC_STEP_SIZE) {
1788 dev_err(nandc->dev, "invalid ecc size\n");
1789 return -EINVAL;
1790 }
1791
1792 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
1793
1794 if (ecc->strength >= 8) {
1795 /* 8 bit ECC defaults to BCH ECC on all platforms */
1796 host->bch_enabled = true;
1797 ecc_mode = 1;
1798
1799 if (wide_bus) {
1800 host->ecc_bytes_hw = 14;
1801 host->spare_bytes = 0;
1802 host->bbm_size = 2;
1803 } else {
1804 host->ecc_bytes_hw = 13;
1805 host->spare_bytes = 2;
1806 host->bbm_size = 1;
1807 }
1808 } else {
1809 /*
1810 * if the controller supports BCH for 4 bit ECC, the controller
1811 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
1812 * always 10 bytes
1813 */
1814 if (nandc->ecc_modes & ECC_BCH_4BIT) {
1815 /* BCH */
1816 host->bch_enabled = true;
1817 ecc_mode = 0;
1818
1819 if (wide_bus) {
1820 host->ecc_bytes_hw = 8;
1821 host->spare_bytes = 2;
1822 host->bbm_size = 2;
1823 } else {
1824 host->ecc_bytes_hw = 7;
1825 host->spare_bytes = 4;
1826 host->bbm_size = 1;
1827 }
1828 } else {
1829 /* RS */
1830 host->ecc_bytes_hw = 10;
1831
1832 if (wide_bus) {
1833 host->spare_bytes = 0;
1834 host->bbm_size = 2;
1835 } else {
1836 host->spare_bytes = 1;
1837 host->bbm_size = 1;
1838 }
1839 }
1840 }
1841
1842 /*
1843 * we consider ecc->bytes as the sum of all the non-data content in a
1844 * step. It gives us a clean representation of the oob area (even if
1845 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
1846 * ECC and 12 bytes for 4 bit ECC
1847 */
1848 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
1849
1850 ecc->read_page = qcom_nandc_read_page;
1851 ecc->read_page_raw = qcom_nandc_read_page_raw;
1852 ecc->read_oob = qcom_nandc_read_oob;
1853 ecc->write_page = qcom_nandc_write_page;
1854 ecc->write_page_raw = qcom_nandc_write_page_raw;
1855 ecc->write_oob = qcom_nandc_write_oob;
1856
1857 ecc->mode = NAND_ECC_HW;
1858
Boris Brezillon421e81c2016-03-18 17:54:27 +01001859 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301860
1861 cwperpage = mtd->writesize / ecc->size;
1862
1863 /*
1864 * DATA_UD_BYTES varies based on whether the read/write command protects
1865 * spare data with ECC too. We protect spare data by default, so we set
1866 * it to main + spare data, which are 512 and 4 bytes respectively.
1867 */
1868 host->cw_data = 516;
1869
1870 /*
1871 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
1872 * for 8 bit ECC
1873 */
1874 host->cw_size = host->cw_data + ecc->bytes;
1875
1876 if (ecc->bytes * (mtd->writesize / ecc->size) > mtd->oobsize) {
1877 dev_err(nandc->dev, "ecc data doesn't fit in OOB area\n");
1878 return -EINVAL;
1879 }
1880
1881 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
1882
1883 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
1884 | host->cw_data << UD_SIZE_BYTES
1885 | 0 << DISABLE_STATUS_AFTER_WRITE
1886 | 5 << NUM_ADDR_CYCLES
1887 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
1888 | 0 << STATUS_BFR_READ
1889 | 1 << SET_RD_MODE_AFTER_STATUS
1890 | host->spare_bytes << SPARE_SIZE_BYTES;
1891
1892 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
1893 | 0 << CS_ACTIVE_BSY
1894 | bad_block_byte << BAD_BLOCK_BYTE_NUM
1895 | 0 << BAD_BLOCK_IN_SPARE_AREA
1896 | 2 << WR_RD_BSY_GAP
1897 | wide_bus << WIDE_FLASH
1898 | host->bch_enabled << ENABLE_BCH_ECC;
1899
1900 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
1901 | host->cw_size << UD_SIZE_BYTES
1902 | 5 << NUM_ADDR_CYCLES
1903 | 0 << SPARE_SIZE_BYTES;
1904
1905 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
1906 | 0 << CS_ACTIVE_BSY
1907 | 17 << BAD_BLOCK_BYTE_NUM
1908 | 1 << BAD_BLOCK_IN_SPARE_AREA
1909 | 2 << WR_RD_BSY_GAP
1910 | wide_bus << WIDE_FLASH
1911 | 1 << DEV0_CFG1_ECC_DISABLE;
1912
Abhishek Sahu10777de2017-08-03 17:56:39 +02001913 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
Archit Tanejac76b78d2016-02-03 14:29:50 +05301914 | 0 << ECC_SW_RESET
1915 | host->cw_data << ECC_NUM_DATA_BYTES
1916 | 1 << ECC_FORCE_CLK_OPEN
1917 | ecc_mode << ECC_MODE
1918 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
1919
1920 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
1921
1922 host->clrflashstatus = FS_READY_BSY_N;
1923 host->clrreadstatus = 0xc0;
1924
1925 dev_dbg(nandc->dev,
1926 "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",
1927 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
1928 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
1929 cwperpage);
1930
1931 return 0;
1932}
1933
1934static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
1935{
1936 int ret;
1937
1938 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
1939 if (ret) {
1940 dev_err(nandc->dev, "failed to set DMA mask\n");
1941 return ret;
1942 }
1943
1944 /*
1945 * we use the internal buffer for reading ONFI params, reading small
1946 * data like ID and status, and preforming read-copy-write operations
1947 * when writing to a codeword partially. 532 is the maximum possible
1948 * size of a codeword for our nand controller
1949 */
1950 nandc->buf_size = 532;
1951
1952 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
1953 GFP_KERNEL);
1954 if (!nandc->data_buffer)
1955 return -ENOMEM;
1956
1957 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
1958 GFP_KERNEL);
1959 if (!nandc->regs)
1960 return -ENOMEM;
1961
1962 nandc->reg_read_buf = devm_kzalloc(nandc->dev,
1963 MAX_REG_RD * sizeof(*nandc->reg_read_buf),
1964 GFP_KERNEL);
1965 if (!nandc->reg_read_buf)
1966 return -ENOMEM;
1967
1968 nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
1969 if (!nandc->chan) {
1970 dev_err(nandc->dev, "failed to request slave channel\n");
1971 return -ENODEV;
1972 }
1973
1974 INIT_LIST_HEAD(&nandc->desc_list);
1975 INIT_LIST_HEAD(&nandc->host_list);
1976
Marc Gonzalezd45bc582016-07-27 11:23:52 +02001977 nand_hw_control_init(&nandc->controller);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301978
1979 return 0;
1980}
1981
1982static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
1983{
1984 dma_release_channel(nandc->chan);
1985}
1986
1987/* one time setup of a few nand controller registers */
1988static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
1989{
1990 /* kill onenand */
1991 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
1992
1993 /* enable ADM DMA */
1994 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
1995
1996 /* save the original values of these registers */
1997 nandc->cmd1 = nandc_read(nandc, NAND_DEV_CMD1);
1998 nandc->vld = nandc_read(nandc, NAND_DEV_CMD_VLD);
1999
2000 return 0;
2001}
2002
2003static int qcom_nand_host_init(struct qcom_nand_controller *nandc,
2004 struct qcom_nand_host *host,
2005 struct device_node *dn)
2006{
2007 struct nand_chip *chip = &host->chip;
2008 struct mtd_info *mtd = nand_to_mtd(chip);
2009 struct device *dev = nandc->dev;
2010 int ret;
2011
2012 ret = of_property_read_u32(dn, "reg", &host->cs);
2013 if (ret) {
2014 dev_err(dev, "can't get chip-select\n");
2015 return -ENXIO;
2016 }
2017
2018 nand_set_flash_node(chip, dn);
2019 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
2020 mtd->owner = THIS_MODULE;
2021 mtd->dev.parent = dev;
2022
2023 chip->cmdfunc = qcom_nandc_command;
2024 chip->select_chip = qcom_nandc_select_chip;
2025 chip->read_byte = qcom_nandc_read_byte;
2026 chip->read_buf = qcom_nandc_read_buf;
2027 chip->write_buf = qcom_nandc_write_buf;
Boris Brezillon4a78cc62017-05-26 17:10:15 +02002028 chip->onfi_set_features = nand_onfi_get_set_features_notsupp;
2029 chip->onfi_get_features = nand_onfi_get_set_features_notsupp;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302030
2031 /*
2032 * the bad block marker is readable only when we read the last codeword
2033 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2034 * helpers don't allow us to read BB from a nand chip with ECC
2035 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2036 * and block_markbad helpers until we permanently switch to using
2037 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2038 */
2039 chip->block_bad = qcom_nandc_block_bad;
2040 chip->block_markbad = qcom_nandc_block_markbad;
2041
2042 chip->controller = &nandc->controller;
2043 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
2044 NAND_SKIP_BBTSCAN;
2045
2046 /* set up initial status value */
2047 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2048
2049 ret = nand_scan_ident(mtd, 1, NULL);
2050 if (ret)
2051 return ret;
2052
2053 ret = qcom_nand_host_setup(host);
2054 if (ret)
2055 return ret;
2056
2057 ret = nand_scan_tail(mtd);
2058 if (ret)
2059 return ret;
2060
2061 return mtd_device_register(mtd, NULL, 0);
2062}
2063
2064/* parse custom DT properties here */
2065static int qcom_nandc_parse_dt(struct platform_device *pdev)
2066{
2067 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2068 struct device_node *np = nandc->dev->of_node;
2069 int ret;
2070
2071 ret = of_property_read_u32(np, "qcom,cmd-crci", &nandc->cmd_crci);
2072 if (ret) {
2073 dev_err(nandc->dev, "command CRCI unspecified\n");
2074 return ret;
2075 }
2076
2077 ret = of_property_read_u32(np, "qcom,data-crci", &nandc->data_crci);
2078 if (ret) {
2079 dev_err(nandc->dev, "data CRCI unspecified\n");
2080 return ret;
2081 }
2082
2083 return 0;
2084}
2085
2086static int qcom_nandc_probe(struct platform_device *pdev)
2087{
2088 struct qcom_nand_controller *nandc;
2089 struct qcom_nand_host *host;
2090 const void *dev_data;
2091 struct device *dev = &pdev->dev;
2092 struct device_node *dn = dev->of_node, *child;
2093 struct resource *res;
2094 int ret;
2095
2096 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2097 if (!nandc)
2098 return -ENOMEM;
2099
2100 platform_set_drvdata(pdev, nandc);
2101 nandc->dev = dev;
2102
2103 dev_data = of_device_get_match_data(dev);
2104 if (!dev_data) {
2105 dev_err(&pdev->dev, "failed to get device data\n");
2106 return -ENODEV;
2107 }
2108
2109 nandc->ecc_modes = (unsigned long)dev_data;
2110
2111 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2112 nandc->base = devm_ioremap_resource(dev, res);
2113 if (IS_ERR(nandc->base))
2114 return PTR_ERR(nandc->base);
2115
2116 nandc->base_dma = phys_to_dma(dev, (phys_addr_t)res->start);
2117
2118 nandc->core_clk = devm_clk_get(dev, "core");
2119 if (IS_ERR(nandc->core_clk))
2120 return PTR_ERR(nandc->core_clk);
2121
2122 nandc->aon_clk = devm_clk_get(dev, "aon");
2123 if (IS_ERR(nandc->aon_clk))
2124 return PTR_ERR(nandc->aon_clk);
2125
2126 ret = qcom_nandc_parse_dt(pdev);
2127 if (ret)
2128 return ret;
2129
2130 ret = qcom_nandc_alloc(nandc);
2131 if (ret)
2132 return ret;
2133
2134 ret = clk_prepare_enable(nandc->core_clk);
2135 if (ret)
2136 goto err_core_clk;
2137
2138 ret = clk_prepare_enable(nandc->aon_clk);
2139 if (ret)
2140 goto err_aon_clk;
2141
2142 ret = qcom_nandc_setup(nandc);
2143 if (ret)
2144 goto err_setup;
2145
2146 for_each_available_child_of_node(dn, child) {
Abhishek Sahue8064232017-07-19 17:17:53 +05302147 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2148 if (!host) {
2149 of_node_put(child);
2150 ret = -ENOMEM;
2151 goto err_cs_init;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302152 }
Abhishek Sahue8064232017-07-19 17:17:53 +05302153
2154 ret = qcom_nand_host_init(nandc, host, child);
2155 if (ret) {
2156 devm_kfree(dev, host);
2157 continue;
2158 }
2159
2160 list_add_tail(&host->node, &nandc->host_list);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302161 }
2162
2163 if (list_empty(&nandc->host_list)) {
2164 ret = -ENODEV;
2165 goto err_cs_init;
2166 }
2167
2168 return 0;
2169
2170err_cs_init:
2171 list_for_each_entry(host, &nandc->host_list, node)
2172 nand_release(nand_to_mtd(&host->chip));
2173err_setup:
2174 clk_disable_unprepare(nandc->aon_clk);
2175err_aon_clk:
2176 clk_disable_unprepare(nandc->core_clk);
2177err_core_clk:
2178 qcom_nandc_unalloc(nandc);
2179
2180 return ret;
2181}
2182
2183static int qcom_nandc_remove(struct platform_device *pdev)
2184{
2185 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2186 struct qcom_nand_host *host;
2187
2188 list_for_each_entry(host, &nandc->host_list, node)
2189 nand_release(nand_to_mtd(&host->chip));
2190
2191 qcom_nandc_unalloc(nandc);
2192
2193 clk_disable_unprepare(nandc->aon_clk);
2194 clk_disable_unprepare(nandc->core_clk);
2195
2196 return 0;
2197}
2198
2199#define EBI2_NANDC_ECC_MODES (ECC_RS_4BIT | ECC_BCH_8BIT)
2200
2201/*
2202 * data will hold a struct pointer containing more differences once we support
2203 * more controller variants
2204 */
2205static const struct of_device_id qcom_nandc_of_match[] = {
2206 { .compatible = "qcom,ipq806x-nand",
2207 .data = (void *)EBI2_NANDC_ECC_MODES,
2208 },
2209 {}
2210};
2211MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
2212
2213static struct platform_driver qcom_nandc_driver = {
2214 .driver = {
2215 .name = "qcom-nandc",
2216 .of_match_table = qcom_nandc_of_match,
2217 },
2218 .probe = qcom_nandc_probe,
2219 .remove = qcom_nandc_remove,
2220};
2221module_platform_driver(qcom_nandc_driver);
2222
2223MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
2224MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
2225MODULE_LICENSE("GPL v2");