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