dfc17a28a06b97be11776ee85c2e71c089b12d11
[platform/kernel/linux-rpi.git] / drivers / mtd / nand / raw / qcom_nandc.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2016, The Linux Foundation. All rights reserved.
4  */
5
6 #include <linux/clk.h>
7 #include <linux/slab.h>
8 #include <linux/bitops.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/dmaengine.h>
11 #include <linux/module.h>
12 #include <linux/mtd/rawnand.h>
13 #include <linux/mtd/partitions.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/delay.h>
17 #include <linux/dma/qcom_bam_dma.h>
18
19 /* NANDc reg offsets */
20 #define NAND_FLASH_CMD                  0x00
21 #define NAND_ADDR0                      0x04
22 #define NAND_ADDR1                      0x08
23 #define NAND_FLASH_CHIP_SELECT          0x0c
24 #define NAND_EXEC_CMD                   0x10
25 #define NAND_FLASH_STATUS               0x14
26 #define NAND_BUFFER_STATUS              0x18
27 #define NAND_DEV0_CFG0                  0x20
28 #define NAND_DEV0_CFG1                  0x24
29 #define NAND_DEV0_ECC_CFG               0x28
30 #define NAND_DEV1_ECC_CFG               0x2c
31 #define NAND_DEV1_CFG0                  0x30
32 #define NAND_DEV1_CFG1                  0x34
33 #define NAND_READ_ID                    0x40
34 #define NAND_READ_STATUS                0x44
35 #define NAND_DEV_CMD0                   0xa0
36 #define NAND_DEV_CMD1                   0xa4
37 #define NAND_DEV_CMD2                   0xa8
38 #define NAND_DEV_CMD_VLD                0xac
39 #define SFLASHC_BURST_CFG               0xe0
40 #define NAND_ERASED_CW_DETECT_CFG       0xe8
41 #define NAND_ERASED_CW_DETECT_STATUS    0xec
42 #define NAND_EBI2_ECC_BUF_CFG           0xf0
43 #define FLASH_BUF_ACC                   0x100
44
45 #define NAND_CTRL                       0xf00
46 #define NAND_VERSION                    0xf08
47 #define NAND_READ_LOCATION_0            0xf20
48 #define NAND_READ_LOCATION_1            0xf24
49 #define NAND_READ_LOCATION_2            0xf28
50 #define NAND_READ_LOCATION_3            0xf2c
51
52 /* dummy register offsets, used by write_reg_dma */
53 #define NAND_DEV_CMD1_RESTORE           0xdead
54 #define NAND_DEV_CMD_VLD_RESTORE        0xbeef
55
56 /* NAND_FLASH_CMD bits */
57 #define PAGE_ACC                        BIT(4)
58 #define LAST_PAGE                       BIT(5)
59
60 /* NAND_FLASH_CHIP_SELECT bits */
61 #define NAND_DEV_SEL                    0
62 #define DM_EN                           BIT(2)
63
64 /* NAND_FLASH_STATUS bits */
65 #define FS_OP_ERR                       BIT(4)
66 #define FS_READY_BSY_N                  BIT(5)
67 #define FS_MPU_ERR                      BIT(8)
68 #define FS_DEVICE_STS_ERR               BIT(16)
69 #define FS_DEVICE_WP                    BIT(23)
70
71 /* NAND_BUFFER_STATUS bits */
72 #define BS_UNCORRECTABLE_BIT            BIT(8)
73 #define BS_CORRECTABLE_ERR_MSK          0x1f
74
75 /* NAND_DEVn_CFG0 bits */
76 #define DISABLE_STATUS_AFTER_WRITE      4
77 #define CW_PER_PAGE                     6
78 #define UD_SIZE_BYTES                   9
79 #define ECC_PARITY_SIZE_BYTES_RS        19
80 #define SPARE_SIZE_BYTES                23
81 #define NUM_ADDR_CYCLES                 27
82 #define STATUS_BFR_READ                 30
83 #define SET_RD_MODE_AFTER_STATUS        31
84
85 /* NAND_DEVn_CFG0 bits */
86 #define DEV0_CFG1_ECC_DISABLE           0
87 #define WIDE_FLASH                      1
88 #define NAND_RECOVERY_CYCLES            2
89 #define CS_ACTIVE_BSY                   5
90 #define BAD_BLOCK_BYTE_NUM              6
91 #define BAD_BLOCK_IN_SPARE_AREA         16
92 #define WR_RD_BSY_GAP                   17
93 #define ENABLE_BCH_ECC                  27
94
95 /* NAND_DEV0_ECC_CFG bits */
96 #define ECC_CFG_ECC_DISABLE             0
97 #define ECC_SW_RESET                    1
98 #define ECC_MODE                        4
99 #define ECC_PARITY_SIZE_BYTES_BCH       8
100 #define ECC_NUM_DATA_BYTES              16
101 #define ECC_FORCE_CLK_OPEN              30
102
103 /* NAND_DEV_CMD1 bits */
104 #define READ_ADDR                       0
105
106 /* NAND_DEV_CMD_VLD bits */
107 #define READ_START_VLD                  BIT(0)
108 #define READ_STOP_VLD                   BIT(1)
109 #define WRITE_START_VLD                 BIT(2)
110 #define ERASE_START_VLD                 BIT(3)
111 #define SEQ_READ_START_VLD              BIT(4)
112
113 /* NAND_EBI2_ECC_BUF_CFG bits */
114 #define NUM_STEPS                       0
115
116 /* NAND_ERASED_CW_DETECT_CFG bits */
117 #define ERASED_CW_ECC_MASK              1
118 #define AUTO_DETECT_RES                 0
119 #define MASK_ECC                        (1 << ERASED_CW_ECC_MASK)
120 #define RESET_ERASED_DET                (1 << AUTO_DETECT_RES)
121 #define ACTIVE_ERASED_DET               (0 << AUTO_DETECT_RES)
122 #define CLR_ERASED_PAGE_DET             (RESET_ERASED_DET | MASK_ECC)
123 #define SET_ERASED_PAGE_DET             (ACTIVE_ERASED_DET | MASK_ECC)
124
125 /* NAND_ERASED_CW_DETECT_STATUS bits */
126 #define PAGE_ALL_ERASED                 BIT(7)
127 #define CODEWORD_ALL_ERASED             BIT(6)
128 #define PAGE_ERASED                     BIT(5)
129 #define CODEWORD_ERASED                 BIT(4)
130 #define ERASED_PAGE                     (PAGE_ALL_ERASED | PAGE_ERASED)
131 #define ERASED_CW                       (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
132
133 /* NAND_READ_LOCATION_n bits */
134 #define READ_LOCATION_OFFSET            0
135 #define READ_LOCATION_SIZE              16
136 #define READ_LOCATION_LAST              31
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 OP_PAGE_READ                    0x2
146 #define OP_PAGE_READ_WITH_ECC           0x3
147 #define OP_PAGE_READ_WITH_ECC_SPARE     0x4
148 #define OP_PROGRAM_PAGE                 0x6
149 #define OP_PAGE_PROGRAM_WITH_ECC        0x7
150 #define OP_PROGRAM_PAGE_SPARE           0x9
151 #define OP_BLOCK_ERASE                  0xa
152 #define OP_FETCH_ID                     0xb
153 #define OP_RESET_DEVICE                 0xd
154
155 /* 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
159 /* NAND_CTRL bits */
160 #define BAM_MODE_EN                     BIT(0)
161
162 /*
163  * the NAND controller performs reads/writes with ECC in 516 byte chunks.
164  * the driver calls the chunks 'step' or 'codeword' interchangeably
165  */
166 #define NANDC_STEP_SIZE                 512
167
168 /*
169  * the largest page size we support is 8K, this will have 16 steps/codewords
170  * of 512 bytes each
171  */
172 #define MAX_NUM_STEPS                   (SZ_8K / NANDC_STEP_SIZE)
173
174 /* we read at most 3 registers per codeword scan */
175 #define MAX_REG_RD                      (3 * MAX_NUM_STEPS)
176
177 /* ECC modes supported by the controller */
178 #define ECC_NONE        BIT(0)
179 #define ECC_RS_4BIT     BIT(1)
180 #define ECC_BCH_4BIT    BIT(2)
181 #define ECC_BCH_8BIT    BIT(3)
182
183 #define nandc_set_read_loc(nandc, reg, offset, size, is_last)   \
184 nandc_set_reg(nandc, NAND_READ_LOCATION_##reg,                  \
185               ((offset) << READ_LOCATION_OFFSET) |              \
186               ((size) << READ_LOCATION_SIZE) |                  \
187               ((is_last) << READ_LOCATION_LAST))
188
189 /*
190  * Returns the actual register address for all NAND_DEV_ registers
191  * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD)
192  */
193 #define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg))
194
195 /* Returns the NAND register physical address */
196 #define nandc_reg_phys(chip, offset) ((chip)->base_phys + (offset))
197
198 /* Returns the dma address for reg read buffer */
199 #define reg_buf_dma_addr(chip, vaddr) \
200         ((chip)->reg_read_dma + \
201         ((uint8_t *)(vaddr) - (uint8_t *)(chip)->reg_read_buf))
202
203 #define QPIC_PER_CW_CMD_ELEMENTS        32
204 #define QPIC_PER_CW_CMD_SGL             32
205 #define QPIC_PER_CW_DATA_SGL            8
206
207 #define QPIC_NAND_COMPLETION_TIMEOUT    msecs_to_jiffies(2000)
208
209 /*
210  * Flags used in DMA descriptor preparation helper functions
211  * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
212  */
213 /* Don't set the EOT in current tx BAM sgl */
214 #define NAND_BAM_NO_EOT                 BIT(0)
215 /* Set the NWD flag in current BAM sgl */
216 #define NAND_BAM_NWD                    BIT(1)
217 /* Finish writing in the current BAM sgl and start writing in another BAM sgl */
218 #define NAND_BAM_NEXT_SGL               BIT(2)
219 /*
220  * Erased codeword status is being used two times in single transfer so this
221  * flag will determine the current value of erased codeword status register
222  */
223 #define NAND_ERASED_CW_SET              BIT(4)
224
225 /*
226  * This data type corresponds to the BAM transaction which will be used for all
227  * NAND transfers.
228  * @bam_ce - the array of BAM command elements
229  * @cmd_sgl - sgl for NAND BAM command pipe
230  * @data_sgl - sgl for NAND BAM consumer/producer pipe
231  * @bam_ce_pos - the index in bam_ce which is available for next sgl
232  * @bam_ce_start - the index in bam_ce which marks the start position ce
233  *                 for current sgl. It will be used for size calculation
234  *                 for current sgl
235  * @cmd_sgl_pos - current index in command sgl.
236  * @cmd_sgl_start - start index in command sgl.
237  * @tx_sgl_pos - current index in data sgl for tx.
238  * @tx_sgl_start - start index in data sgl for tx.
239  * @rx_sgl_pos - current index in data sgl for rx.
240  * @rx_sgl_start - start index in data sgl for rx.
241  * @wait_second_completion - wait for second DMA desc completion before making
242  *                           the NAND transfer completion.
243  * @txn_done - completion for NAND transfer.
244  * @last_data_desc - last DMA desc in data channel (tx/rx).
245  * @last_cmd_desc - last DMA desc in command channel.
246  */
247 struct bam_transaction {
248         struct bam_cmd_element *bam_ce;
249         struct scatterlist *cmd_sgl;
250         struct scatterlist *data_sgl;
251         u32 bam_ce_pos;
252         u32 bam_ce_start;
253         u32 cmd_sgl_pos;
254         u32 cmd_sgl_start;
255         u32 tx_sgl_pos;
256         u32 tx_sgl_start;
257         u32 rx_sgl_pos;
258         u32 rx_sgl_start;
259         bool wait_second_completion;
260         struct completion txn_done;
261         struct dma_async_tx_descriptor *last_data_desc;
262         struct dma_async_tx_descriptor *last_cmd_desc;
263 };
264
265 /*
266  * This data type corresponds to the nand dma descriptor
267  * @list - list for desc_info
268  * @dir - DMA transfer direction
269  * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
270  *            ADM
271  * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
272  * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
273  * @dma_desc - low level DMA engine descriptor
274  */
275 struct desc_info {
276         struct list_head node;
277
278         enum dma_data_direction dir;
279         union {
280                 struct scatterlist adm_sgl;
281                 struct {
282                         struct scatterlist *bam_sgl;
283                         int sgl_cnt;
284                 };
285         };
286         struct dma_async_tx_descriptor *dma_desc;
287 };
288
289 /*
290  * holds the current register values that we want to write. acts as a contiguous
291  * chunk of memory which we use to write the controller registers through DMA.
292  */
293 struct nandc_regs {
294         __le32 cmd;
295         __le32 addr0;
296         __le32 addr1;
297         __le32 chip_sel;
298         __le32 exec;
299
300         __le32 cfg0;
301         __le32 cfg1;
302         __le32 ecc_bch_cfg;
303
304         __le32 clrflashstatus;
305         __le32 clrreadstatus;
306
307         __le32 cmd1;
308         __le32 vld;
309
310         __le32 orig_cmd1;
311         __le32 orig_vld;
312
313         __le32 ecc_buf_cfg;
314         __le32 read_location0;
315         __le32 read_location1;
316         __le32 read_location2;
317         __le32 read_location3;
318
319         __le32 erased_cw_detect_cfg_clr;
320         __le32 erased_cw_detect_cfg_set;
321 };
322
323 /*
324  * NAND controller data struct
325  *
326  * @controller:                 base controller structure
327  * @host_list:                  list containing all the chips attached to the
328  *                              controller
329  * @dev:                        parent device
330  * @base:                       MMIO base
331  * @base_phys:                  physical base address of controller registers
332  * @base_dma:                   dma base address of controller registers
333  * @core_clk:                   controller clock
334  * @aon_clk:                    another controller clock
335  *
336  * @chan:                       dma channel
337  * @cmd_crci:                   ADM DMA CRCI for command flow control
338  * @data_crci:                  ADM DMA CRCI for data flow control
339  * @desc_list:                  DMA descriptor list (list of desc_infos)
340  *
341  * @data_buffer:                our local DMA buffer for page read/writes,
342  *                              used when we can't use the buffer provided
343  *                              by upper layers directly
344  * @buf_size/count/start:       markers for chip->legacy.read_buf/write_buf
345  *                              functions
346  * @reg_read_buf:               local buffer for reading back registers via DMA
347  * @reg_read_dma:               contains dma address for register read buffer
348  * @reg_read_pos:               marker for data read in reg_read_buf
349  *
350  * @regs:                       a contiguous chunk of memory for DMA register
351  *                              writes. contains the register values to be
352  *                              written to controller
353  * @cmd1/vld:                   some fixed controller register values
354  * @props:                      properties of current NAND controller,
355  *                              initialized via DT match data
356  * @max_cwperpage:              maximum QPIC codewords required. calculated
357  *                              from all connected NAND devices pagesize
358  */
359 struct qcom_nand_controller {
360         struct nand_controller controller;
361         struct list_head host_list;
362
363         struct device *dev;
364
365         void __iomem *base;
366         phys_addr_t base_phys;
367         dma_addr_t base_dma;
368
369         struct clk *core_clk;
370         struct clk *aon_clk;
371
372         union {
373                 /* will be used only by QPIC for BAM DMA */
374                 struct {
375                         struct dma_chan *tx_chan;
376                         struct dma_chan *rx_chan;
377                         struct dma_chan *cmd_chan;
378                 };
379
380                 /* will be used only by EBI2 for ADM DMA */
381                 struct {
382                         struct dma_chan *chan;
383                         unsigned int cmd_crci;
384                         unsigned int data_crci;
385                 };
386         };
387
388         struct list_head desc_list;
389         struct bam_transaction *bam_txn;
390
391         u8              *data_buffer;
392         int             buf_size;
393         int             buf_count;
394         int             buf_start;
395         unsigned int    max_cwperpage;
396
397         __le32 *reg_read_buf;
398         dma_addr_t reg_read_dma;
399         int reg_read_pos;
400
401         struct nandc_regs *regs;
402
403         u32 cmd1, vld;
404         const struct qcom_nandc_props *props;
405 };
406
407 /*
408  * NAND chip structure
409  *
410  * @chip:                       base NAND chip structure
411  * @node:                       list node to add itself to host_list in
412  *                              qcom_nand_controller
413  *
414  * @cs:                         chip select value for this chip
415  * @cw_size:                    the number of bytes in a single step/codeword
416  *                              of a page, consisting of all data, ecc, spare
417  *                              and reserved bytes
418  * @cw_data:                    the number of bytes within a codeword protected
419  *                              by ECC
420  * @use_ecc:                    request the controller to use ECC for the
421  *                              upcoming read/write
422  * @bch_enabled:                flag to tell whether BCH ECC mode is used
423  * @ecc_bytes_hw:               ECC bytes used by controller hardware for this
424  *                              chip
425  * @status:                     value to be returned if NAND_CMD_STATUS command
426  *                              is executed
427  * @last_command:               keeps track of last command on this chip. used
428  *                              for reading correct status
429  *
430  * @cfg0, cfg1, cfg0_raw..:     NANDc register configurations needed for
431  *                              ecc/non-ecc mode for the current nand flash
432  *                              device
433  */
434 struct qcom_nand_host {
435         struct nand_chip chip;
436         struct list_head node;
437
438         int cs;
439         int cw_size;
440         int cw_data;
441         bool use_ecc;
442         bool bch_enabled;
443         int ecc_bytes_hw;
444         int spare_bytes;
445         int bbm_size;
446         u8 status;
447         int last_command;
448
449         u32 cfg0, cfg1;
450         u32 cfg0_raw, cfg1_raw;
451         u32 ecc_buf_cfg;
452         u32 ecc_bch_cfg;
453         u32 clrflashstatus;
454         u32 clrreadstatus;
455 };
456
457 /*
458  * This data type corresponds to the NAND controller properties which varies
459  * among different NAND controllers.
460  * @ecc_modes - ecc mode for NAND
461  * @is_bam - whether NAND controller is using BAM
462  * @is_qpic - whether NAND CTRL is part of qpic IP
463  * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset
464  */
465 struct qcom_nandc_props {
466         u32 ecc_modes;
467         bool is_bam;
468         bool is_qpic;
469         u32 dev_cmd_reg_start;
470 };
471
472 /* Frees the BAM transaction memory */
473 static void free_bam_transaction(struct qcom_nand_controller *nandc)
474 {
475         struct bam_transaction *bam_txn = nandc->bam_txn;
476
477         devm_kfree(nandc->dev, bam_txn);
478 }
479
480 /* Allocates and Initializes the BAM transaction */
481 static struct bam_transaction *
482 alloc_bam_transaction(struct qcom_nand_controller *nandc)
483 {
484         struct bam_transaction *bam_txn;
485         size_t bam_txn_size;
486         unsigned int num_cw = nandc->max_cwperpage;
487         void *bam_txn_buf;
488
489         bam_txn_size =
490                 sizeof(*bam_txn) + num_cw *
491                 ((sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS) +
492                 (sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) +
493                 (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL));
494
495         bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL);
496         if (!bam_txn_buf)
497                 return NULL;
498
499         bam_txn = bam_txn_buf;
500         bam_txn_buf += sizeof(*bam_txn);
501
502         bam_txn->bam_ce = bam_txn_buf;
503         bam_txn_buf +=
504                 sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS * num_cw;
505
506         bam_txn->cmd_sgl = bam_txn_buf;
507         bam_txn_buf +=
508                 sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw;
509
510         bam_txn->data_sgl = bam_txn_buf;
511
512         init_completion(&bam_txn->txn_done);
513
514         return bam_txn;
515 }
516
517 /* Clears the BAM transaction indexes */
518 static void clear_bam_transaction(struct qcom_nand_controller *nandc)
519 {
520         struct bam_transaction *bam_txn = nandc->bam_txn;
521
522         if (!nandc->props->is_bam)
523                 return;
524
525         bam_txn->bam_ce_pos = 0;
526         bam_txn->bam_ce_start = 0;
527         bam_txn->cmd_sgl_pos = 0;
528         bam_txn->cmd_sgl_start = 0;
529         bam_txn->tx_sgl_pos = 0;
530         bam_txn->tx_sgl_start = 0;
531         bam_txn->rx_sgl_pos = 0;
532         bam_txn->rx_sgl_start = 0;
533         bam_txn->last_data_desc = NULL;
534         bam_txn->wait_second_completion = false;
535
536         sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage *
537                       QPIC_PER_CW_CMD_SGL);
538         sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage *
539                       QPIC_PER_CW_DATA_SGL);
540
541         reinit_completion(&bam_txn->txn_done);
542 }
543
544 /* Callback for DMA descriptor completion */
545 static void qpic_bam_dma_done(void *data)
546 {
547         struct bam_transaction *bam_txn = data;
548
549         /*
550          * In case of data transfer with NAND, 2 callbacks will be generated.
551          * One for command channel and another one for data channel.
552          * If current transaction has data descriptors
553          * (i.e. wait_second_completion is true), then set this to false
554          * and wait for second DMA descriptor completion.
555          */
556         if (bam_txn->wait_second_completion)
557                 bam_txn->wait_second_completion = false;
558         else
559                 complete(&bam_txn->txn_done);
560 }
561
562 static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
563 {
564         return container_of(chip, struct qcom_nand_host, chip);
565 }
566
567 static inline struct qcom_nand_controller *
568 get_qcom_nand_controller(struct nand_chip *chip)
569 {
570         return container_of(chip->controller, struct qcom_nand_controller,
571                             controller);
572 }
573
574 static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
575 {
576         return ioread32(nandc->base + offset);
577 }
578
579 static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
580                                u32 val)
581 {
582         iowrite32(val, nandc->base + offset);
583 }
584
585 static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
586                                           bool is_cpu)
587 {
588         if (!nandc->props->is_bam)
589                 return;
590
591         if (is_cpu)
592                 dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
593                                         MAX_REG_RD *
594                                         sizeof(*nandc->reg_read_buf),
595                                         DMA_FROM_DEVICE);
596         else
597                 dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
598                                            MAX_REG_RD *
599                                            sizeof(*nandc->reg_read_buf),
600                                            DMA_FROM_DEVICE);
601 }
602
603 static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
604 {
605         switch (offset) {
606         case NAND_FLASH_CMD:
607                 return &regs->cmd;
608         case NAND_ADDR0:
609                 return &regs->addr0;
610         case NAND_ADDR1:
611                 return &regs->addr1;
612         case NAND_FLASH_CHIP_SELECT:
613                 return &regs->chip_sel;
614         case NAND_EXEC_CMD:
615                 return &regs->exec;
616         case NAND_FLASH_STATUS:
617                 return &regs->clrflashstatus;
618         case NAND_DEV0_CFG0:
619                 return &regs->cfg0;
620         case NAND_DEV0_CFG1:
621                 return &regs->cfg1;
622         case NAND_DEV0_ECC_CFG:
623                 return &regs->ecc_bch_cfg;
624         case NAND_READ_STATUS:
625                 return &regs->clrreadstatus;
626         case NAND_DEV_CMD1:
627                 return &regs->cmd1;
628         case NAND_DEV_CMD1_RESTORE:
629                 return &regs->orig_cmd1;
630         case NAND_DEV_CMD_VLD:
631                 return &regs->vld;
632         case NAND_DEV_CMD_VLD_RESTORE:
633                 return &regs->orig_vld;
634         case NAND_EBI2_ECC_BUF_CFG:
635                 return &regs->ecc_buf_cfg;
636         case NAND_READ_LOCATION_0:
637                 return &regs->read_location0;
638         case NAND_READ_LOCATION_1:
639                 return &regs->read_location1;
640         case NAND_READ_LOCATION_2:
641                 return &regs->read_location2;
642         case NAND_READ_LOCATION_3:
643                 return &regs->read_location3;
644         default:
645                 return NULL;
646         }
647 }
648
649 static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
650                           u32 val)
651 {
652         struct nandc_regs *regs = nandc->regs;
653         __le32 *reg;
654
655         reg = offset_to_nandc_reg(regs, offset);
656
657         if (reg)
658                 *reg = cpu_to_le32(val);
659 }
660
661 /* helper to configure address register values */
662 static void set_address(struct qcom_nand_host *host, u16 column, int page)
663 {
664         struct nand_chip *chip = &host->chip;
665         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
666
667         if (chip->options & NAND_BUSWIDTH_16)
668                 column >>= 1;
669
670         nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
671         nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
672 }
673
674 /*
675  * update_rw_regs:      set up read/write register values, these will be
676  *                      written to the NAND controller registers via DMA
677  *
678  * @num_cw:             number of steps for the read/write operation
679  * @read:               read or write operation
680  */
681 static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
682 {
683         struct nand_chip *chip = &host->chip;
684         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
685         u32 cmd, cfg0, cfg1, ecc_bch_cfg;
686
687         if (read) {
688                 if (host->use_ecc)
689                         cmd = OP_PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
690                 else
691                         cmd = OP_PAGE_READ | PAGE_ACC | LAST_PAGE;
692         } else {
693                 cmd = OP_PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
694         }
695
696         if (host->use_ecc) {
697                 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
698                                 (num_cw - 1) << CW_PER_PAGE;
699
700                 cfg1 = host->cfg1;
701                 ecc_bch_cfg = host->ecc_bch_cfg;
702         } else {
703                 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
704                                 (num_cw - 1) << CW_PER_PAGE;
705
706                 cfg1 = host->cfg1_raw;
707                 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
708         }
709
710         nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
711         nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
712         nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
713         nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
714         nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
715         nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
716         nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
717         nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
718
719         if (read)
720                 nandc_set_read_loc(nandc, 0, 0, host->use_ecc ?
721                                    host->cw_data : host->cw_size, 1);
722 }
723
724 /*
725  * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
726  * for BAM. This descriptor will be added in the NAND DMA descriptor queue
727  * which will be submitted to DMA engine.
728  */
729 static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
730                                   struct dma_chan *chan,
731                                   unsigned long flags)
732 {
733         struct desc_info *desc;
734         struct scatterlist *sgl;
735         unsigned int sgl_cnt;
736         int ret;
737         struct bam_transaction *bam_txn = nandc->bam_txn;
738         enum dma_transfer_direction dir_eng;
739         struct dma_async_tx_descriptor *dma_desc;
740
741         desc = kzalloc(sizeof(*desc), GFP_KERNEL);
742         if (!desc)
743                 return -ENOMEM;
744
745         if (chan == nandc->cmd_chan) {
746                 sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
747                 sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
748                 bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
749                 dir_eng = DMA_MEM_TO_DEV;
750                 desc->dir = DMA_TO_DEVICE;
751         } else if (chan == nandc->tx_chan) {
752                 sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
753                 sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
754                 bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
755                 dir_eng = DMA_MEM_TO_DEV;
756                 desc->dir = DMA_TO_DEVICE;
757         } else {
758                 sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
759                 sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
760                 bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
761                 dir_eng = DMA_DEV_TO_MEM;
762                 desc->dir = DMA_FROM_DEVICE;
763         }
764
765         sg_mark_end(sgl + sgl_cnt - 1);
766         ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
767         if (ret == 0) {
768                 dev_err(nandc->dev, "failure in mapping desc\n");
769                 kfree(desc);
770                 return -ENOMEM;
771         }
772
773         desc->sgl_cnt = sgl_cnt;
774         desc->bam_sgl = sgl;
775
776         dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
777                                            flags);
778
779         if (!dma_desc) {
780                 dev_err(nandc->dev, "failure in prep desc\n");
781                 dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
782                 kfree(desc);
783                 return -EINVAL;
784         }
785
786         desc->dma_desc = dma_desc;
787
788         /* update last data/command descriptor */
789         if (chan == nandc->cmd_chan)
790                 bam_txn->last_cmd_desc = dma_desc;
791         else
792                 bam_txn->last_data_desc = dma_desc;
793
794         list_add_tail(&desc->node, &nandc->desc_list);
795
796         return 0;
797 }
798
799 /*
800  * Prepares the command descriptor for BAM DMA which will be used for NAND
801  * register reads and writes. The command descriptor requires the command
802  * to be formed in command element type so this function uses the command
803  * element from bam transaction ce array and fills the same with required
804  * data. A single SGL can contain multiple command elements so
805  * NAND_BAM_NEXT_SGL will be used for starting the separate SGL
806  * after the current command element.
807  */
808 static int prep_bam_dma_desc_cmd(struct qcom_nand_controller *nandc, bool read,
809                                  int reg_off, const void *vaddr,
810                                  int size, unsigned int flags)
811 {
812         int bam_ce_size;
813         int i, ret;
814         struct bam_cmd_element *bam_ce_buffer;
815         struct bam_transaction *bam_txn = nandc->bam_txn;
816
817         bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_pos];
818
819         /* fill the command desc */
820         for (i = 0; i < size; i++) {
821                 if (read)
822                         bam_prep_ce(&bam_ce_buffer[i],
823                                     nandc_reg_phys(nandc, reg_off + 4 * i),
824                                     BAM_READ_COMMAND,
825                                     reg_buf_dma_addr(nandc,
826                                                      (__le32 *)vaddr + i));
827                 else
828                         bam_prep_ce_le32(&bam_ce_buffer[i],
829                                          nandc_reg_phys(nandc, reg_off + 4 * i),
830                                          BAM_WRITE_COMMAND,
831                                          *((__le32 *)vaddr + i));
832         }
833
834         bam_txn->bam_ce_pos += size;
835
836         /* use the separate sgl after this command */
837         if (flags & NAND_BAM_NEXT_SGL) {
838                 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_start];
839                 bam_ce_size = (bam_txn->bam_ce_pos -
840                                 bam_txn->bam_ce_start) *
841                                 sizeof(struct bam_cmd_element);
842                 sg_set_buf(&bam_txn->cmd_sgl[bam_txn->cmd_sgl_pos],
843                            bam_ce_buffer, bam_ce_size);
844                 bam_txn->cmd_sgl_pos++;
845                 bam_txn->bam_ce_start = bam_txn->bam_ce_pos;
846
847                 if (flags & NAND_BAM_NWD) {
848                         ret = prepare_bam_async_desc(nandc, nandc->cmd_chan,
849                                                      DMA_PREP_FENCE |
850                                                      DMA_PREP_CMD);
851                         if (ret)
852                                 return ret;
853                 }
854         }
855
856         return 0;
857 }
858
859 /*
860  * Prepares the data descriptor for BAM DMA which will be used for NAND
861  * data reads and writes.
862  */
863 static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read,
864                                   const void *vaddr,
865                                   int size, unsigned int flags)
866 {
867         int ret;
868         struct bam_transaction *bam_txn = nandc->bam_txn;
869
870         if (read) {
871                 sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos],
872                            vaddr, size);
873                 bam_txn->rx_sgl_pos++;
874         } else {
875                 sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos],
876                            vaddr, size);
877                 bam_txn->tx_sgl_pos++;
878
879                 /*
880                  * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
881                  * is not set, form the DMA descriptor
882                  */
883                 if (!(flags & NAND_BAM_NO_EOT)) {
884                         ret = prepare_bam_async_desc(nandc, nandc->tx_chan,
885                                                      DMA_PREP_INTERRUPT);
886                         if (ret)
887                                 return ret;
888                 }
889         }
890
891         return 0;
892 }
893
894 static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
895                              int reg_off, const void *vaddr, int size,
896                              bool flow_control)
897 {
898         struct desc_info *desc;
899         struct dma_async_tx_descriptor *dma_desc;
900         struct scatterlist *sgl;
901         struct dma_slave_config slave_conf;
902         enum dma_transfer_direction dir_eng;
903         int ret;
904
905         desc = kzalloc(sizeof(*desc), GFP_KERNEL);
906         if (!desc)
907                 return -ENOMEM;
908
909         sgl = &desc->adm_sgl;
910
911         sg_init_one(sgl, vaddr, size);
912
913         if (read) {
914                 dir_eng = DMA_DEV_TO_MEM;
915                 desc->dir = DMA_FROM_DEVICE;
916         } else {
917                 dir_eng = DMA_MEM_TO_DEV;
918                 desc->dir = DMA_TO_DEVICE;
919         }
920
921         ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
922         if (ret == 0) {
923                 ret = -ENOMEM;
924                 goto err;
925         }
926
927         memset(&slave_conf, 0x00, sizeof(slave_conf));
928
929         slave_conf.device_fc = flow_control;
930         if (read) {
931                 slave_conf.src_maxburst = 16;
932                 slave_conf.src_addr = nandc->base_dma + reg_off;
933                 slave_conf.slave_id = nandc->data_crci;
934         } else {
935                 slave_conf.dst_maxburst = 16;
936                 slave_conf.dst_addr = nandc->base_dma + reg_off;
937                 slave_conf.slave_id = nandc->cmd_crci;
938         }
939
940         ret = dmaengine_slave_config(nandc->chan, &slave_conf);
941         if (ret) {
942                 dev_err(nandc->dev, "failed to configure dma channel\n");
943                 goto err;
944         }
945
946         dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
947         if (!dma_desc) {
948                 dev_err(nandc->dev, "failed to prepare desc\n");
949                 ret = -EINVAL;
950                 goto err;
951         }
952
953         desc->dma_desc = dma_desc;
954
955         list_add_tail(&desc->node, &nandc->desc_list);
956
957         return 0;
958 err:
959         kfree(desc);
960
961         return ret;
962 }
963
964 /*
965  * read_reg_dma:        prepares a descriptor to read a given number of
966  *                      contiguous registers to the reg_read_buf pointer
967  *
968  * @first:              offset of the first register in the contiguous block
969  * @num_regs:           number of registers to read
970  * @flags:              flags to control DMA descriptor preparation
971  */
972 static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
973                         int num_regs, unsigned int flags)
974 {
975         bool flow_control = false;
976         void *vaddr;
977
978         vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
979         nandc->reg_read_pos += num_regs;
980
981         if (first == NAND_DEV_CMD_VLD || first == NAND_DEV_CMD1)
982                 first = dev_cmd_reg_addr(nandc, first);
983
984         if (nandc->props->is_bam)
985                 return prep_bam_dma_desc_cmd(nandc, true, first, vaddr,
986                                              num_regs, flags);
987
988         if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
989                 flow_control = true;
990
991         return prep_adm_dma_desc(nandc, true, first, vaddr,
992                                  num_regs * sizeof(u32), flow_control);
993 }
994
995 /*
996  * write_reg_dma:       prepares a descriptor to write a given number of
997  *                      contiguous registers
998  *
999  * @first:              offset of the first register in the contiguous block
1000  * @num_regs:           number of registers to write
1001  * @flags:              flags to control DMA descriptor preparation
1002  */
1003 static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
1004                          int num_regs, unsigned int flags)
1005 {
1006         bool flow_control = false;
1007         struct nandc_regs *regs = nandc->regs;
1008         void *vaddr;
1009
1010         vaddr = offset_to_nandc_reg(regs, first);
1011
1012         if (first == NAND_ERASED_CW_DETECT_CFG) {
1013                 if (flags & NAND_ERASED_CW_SET)
1014                         vaddr = &regs->erased_cw_detect_cfg_set;
1015                 else
1016                         vaddr = &regs->erased_cw_detect_cfg_clr;
1017         }
1018
1019         if (first == NAND_EXEC_CMD)
1020                 flags |= NAND_BAM_NWD;
1021
1022         if (first == NAND_DEV_CMD1_RESTORE || first == NAND_DEV_CMD1)
1023                 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD1);
1024
1025         if (first == NAND_DEV_CMD_VLD_RESTORE || first == NAND_DEV_CMD_VLD)
1026                 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD);
1027
1028         if (nandc->props->is_bam)
1029                 return prep_bam_dma_desc_cmd(nandc, false, first, vaddr,
1030                                              num_regs, flags);
1031
1032         if (first == NAND_FLASH_CMD)
1033                 flow_control = true;
1034
1035         return prep_adm_dma_desc(nandc, false, first, vaddr,
1036                                  num_regs * sizeof(u32), flow_control);
1037 }
1038
1039 /*
1040  * read_data_dma:       prepares a DMA descriptor to transfer data from the
1041  *                      controller's internal buffer to the buffer 'vaddr'
1042  *
1043  * @reg_off:            offset within the controller's data buffer
1044  * @vaddr:              virtual address of the buffer we want to write to
1045  * @size:               DMA transaction size in bytes
1046  * @flags:              flags to control DMA descriptor preparation
1047  */
1048 static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
1049                          const u8 *vaddr, int size, unsigned int flags)
1050 {
1051         if (nandc->props->is_bam)
1052                 return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags);
1053
1054         return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
1055 }
1056
1057 /*
1058  * write_data_dma:      prepares a DMA descriptor to transfer data from
1059  *                      'vaddr' to the controller's internal buffer
1060  *
1061  * @reg_off:            offset within the controller's data buffer
1062  * @vaddr:              virtual address of the buffer we want to read from
1063  * @size:               DMA transaction size in bytes
1064  * @flags:              flags to control DMA descriptor preparation
1065  */
1066 static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
1067                           const u8 *vaddr, int size, unsigned int flags)
1068 {
1069         if (nandc->props->is_bam)
1070                 return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags);
1071
1072         return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
1073 }
1074
1075 /*
1076  * Helper to prepare DMA descriptors for configuring registers
1077  * before reading a NAND page.
1078  */
1079 static void config_nand_page_read(struct qcom_nand_controller *nandc)
1080 {
1081         write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1082         write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1083         write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
1084         write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
1085         write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
1086                       NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
1087 }
1088
1089 /*
1090  * Helper to prepare DMA descriptors for configuring registers
1091  * before reading each codeword in NAND page.
1092  */
1093 static void
1094 config_nand_cw_read(struct qcom_nand_controller *nandc, bool use_ecc)
1095 {
1096         if (nandc->props->is_bam)
1097                 write_reg_dma(nandc, NAND_READ_LOCATION_0, 4,
1098                               NAND_BAM_NEXT_SGL);
1099
1100         write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1101         write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1102
1103         if (use_ecc) {
1104                 read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
1105                 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
1106                              NAND_BAM_NEXT_SGL);
1107         } else {
1108                 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1109         }
1110 }
1111
1112 /*
1113  * Helper to prepare dma descriptors to configure registers needed for reading a
1114  * single codeword in page
1115  */
1116 static void
1117 config_nand_single_cw_page_read(struct qcom_nand_controller *nandc,
1118                                 bool use_ecc)
1119 {
1120         config_nand_page_read(nandc);
1121         config_nand_cw_read(nandc, use_ecc);
1122 }
1123
1124 /*
1125  * Helper to prepare DMA descriptors used to configure registers needed for
1126  * before writing a NAND page.
1127  */
1128 static void config_nand_page_write(struct qcom_nand_controller *nandc)
1129 {
1130         write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1131         write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1132         write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
1133                       NAND_BAM_NEXT_SGL);
1134 }
1135
1136 /*
1137  * Helper to prepare DMA descriptors for configuring registers
1138  * before writing each codeword in NAND page.
1139  */
1140 static void config_nand_cw_write(struct qcom_nand_controller *nandc)
1141 {
1142         write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1143         write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1144
1145         read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1146
1147         write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1148         write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
1149 }
1150
1151 /*
1152  * the following functions are used within chip->legacy.cmdfunc() to
1153  * perform different NAND_CMD_* commands
1154  */
1155
1156 /* sets up descriptors for NAND_CMD_PARAM */
1157 static int nandc_param(struct qcom_nand_host *host)
1158 {
1159         struct nand_chip *chip = &host->chip;
1160         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1161
1162         /*
1163          * NAND_CMD_PARAM is called before we know much about the FLASH chip
1164          * in use. we configure the controller to perform a raw read of 512
1165          * bytes to read onfi params
1166          */
1167         nandc_set_reg(nandc, NAND_FLASH_CMD, OP_PAGE_READ | PAGE_ACC | LAST_PAGE);
1168         nandc_set_reg(nandc, NAND_ADDR0, 0);
1169         nandc_set_reg(nandc, NAND_ADDR1, 0);
1170         nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
1171                                         | 512 << UD_SIZE_BYTES
1172                                         | 5 << NUM_ADDR_CYCLES
1173                                         | 0 << SPARE_SIZE_BYTES);
1174         nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
1175                                         | 0 << CS_ACTIVE_BSY
1176                                         | 17 << BAD_BLOCK_BYTE_NUM
1177                                         | 1 << BAD_BLOCK_IN_SPARE_AREA
1178                                         | 2 << WR_RD_BSY_GAP
1179                                         | 0 << WIDE_FLASH
1180                                         | 1 << DEV0_CFG1_ECC_DISABLE);
1181         nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
1182
1183         /* configure CMD1 and VLD for ONFI param probing */
1184         nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
1185                       (nandc->vld & ~READ_START_VLD));
1186         nandc_set_reg(nandc, NAND_DEV_CMD1,
1187                       (nandc->cmd1 & ~(0xFF << READ_ADDR))
1188                       | NAND_CMD_PARAM << READ_ADDR);
1189
1190         nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1191
1192         nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
1193         nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
1194         nandc_set_read_loc(nandc, 0, 0, 512, 1);
1195
1196         write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
1197         write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
1198
1199         nandc->buf_count = 512;
1200         memset(nandc->data_buffer, 0xff, nandc->buf_count);
1201
1202         config_nand_single_cw_page_read(nandc, false);
1203
1204         read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
1205                       nandc->buf_count, 0);
1206
1207         /* restore CMD1 and VLD regs */
1208         write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
1209         write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
1210
1211         return 0;
1212 }
1213
1214 /* sets up descriptors for NAND_CMD_ERASE1 */
1215 static int erase_block(struct qcom_nand_host *host, int page_addr)
1216 {
1217         struct nand_chip *chip = &host->chip;
1218         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1219
1220         nandc_set_reg(nandc, NAND_FLASH_CMD,
1221                       OP_BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
1222         nandc_set_reg(nandc, NAND_ADDR0, page_addr);
1223         nandc_set_reg(nandc, NAND_ADDR1, 0);
1224         nandc_set_reg(nandc, NAND_DEV0_CFG0,
1225                       host->cfg0_raw & ~(7 << CW_PER_PAGE));
1226         nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
1227         nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1228         nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
1229         nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
1230
1231         write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
1232         write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
1233         write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1234
1235         read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1236
1237         write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1238         write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
1239
1240         return 0;
1241 }
1242
1243 /* sets up descriptors for NAND_CMD_READID */
1244 static int read_id(struct qcom_nand_host *host, int column)
1245 {
1246         struct nand_chip *chip = &host->chip;
1247         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1248
1249         if (column == -1)
1250                 return 0;
1251
1252         nandc_set_reg(nandc, NAND_FLASH_CMD, OP_FETCH_ID);
1253         nandc_set_reg(nandc, NAND_ADDR0, column);
1254         nandc_set_reg(nandc, NAND_ADDR1, 0);
1255         nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT,
1256                       nandc->props->is_bam ? 0 : DM_EN);
1257         nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1258
1259         write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
1260         write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1261
1262         read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
1263
1264         return 0;
1265 }
1266
1267 /* sets up descriptors for NAND_CMD_RESET */
1268 static int reset(struct qcom_nand_host *host)
1269 {
1270         struct nand_chip *chip = &host->chip;
1271         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1272
1273         nandc_set_reg(nandc, NAND_FLASH_CMD, OP_RESET_DEVICE);
1274         nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1275
1276         write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1277         write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1278
1279         read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1280
1281         return 0;
1282 }
1283
1284 /* helpers to submit/free our list of dma descriptors */
1285 static int submit_descs(struct qcom_nand_controller *nandc)
1286 {
1287         struct desc_info *desc;
1288         dma_cookie_t cookie = 0;
1289         struct bam_transaction *bam_txn = nandc->bam_txn;
1290         int r;
1291
1292         if (nandc->props->is_bam) {
1293                 if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
1294                         r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
1295                         if (r)
1296                                 return r;
1297                 }
1298
1299                 if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
1300                         r = prepare_bam_async_desc(nandc, nandc->tx_chan,
1301                                                    DMA_PREP_INTERRUPT);
1302                         if (r)
1303                                 return r;
1304                 }
1305
1306                 if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
1307                         r = prepare_bam_async_desc(nandc, nandc->cmd_chan,
1308                                                    DMA_PREP_CMD);
1309                         if (r)
1310                                 return r;
1311                 }
1312         }
1313
1314         list_for_each_entry(desc, &nandc->desc_list, node)
1315                 cookie = dmaengine_submit(desc->dma_desc);
1316
1317         if (nandc->props->is_bam) {
1318                 bam_txn->last_cmd_desc->callback = qpic_bam_dma_done;
1319                 bam_txn->last_cmd_desc->callback_param = bam_txn;
1320                 if (bam_txn->last_data_desc) {
1321                         bam_txn->last_data_desc->callback = qpic_bam_dma_done;
1322                         bam_txn->last_data_desc->callback_param = bam_txn;
1323                         bam_txn->wait_second_completion = true;
1324                 }
1325
1326                 dma_async_issue_pending(nandc->tx_chan);
1327                 dma_async_issue_pending(nandc->rx_chan);
1328                 dma_async_issue_pending(nandc->cmd_chan);
1329
1330                 if (!wait_for_completion_timeout(&bam_txn->txn_done,
1331                                                  QPIC_NAND_COMPLETION_TIMEOUT))
1332                         return -ETIMEDOUT;
1333         } else {
1334                 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
1335                         return -ETIMEDOUT;
1336         }
1337
1338         return 0;
1339 }
1340
1341 static void free_descs(struct qcom_nand_controller *nandc)
1342 {
1343         struct desc_info *desc, *n;
1344
1345         list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
1346                 list_del(&desc->node);
1347
1348                 if (nandc->props->is_bam)
1349                         dma_unmap_sg(nandc->dev, desc->bam_sgl,
1350                                      desc->sgl_cnt, desc->dir);
1351                 else
1352                         dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
1353                                      desc->dir);
1354
1355                 kfree(desc);
1356         }
1357 }
1358
1359 /* reset the register read buffer for next NAND operation */
1360 static void clear_read_regs(struct qcom_nand_controller *nandc)
1361 {
1362         nandc->reg_read_pos = 0;
1363         nandc_read_buffer_sync(nandc, false);
1364 }
1365
1366 static void pre_command(struct qcom_nand_host *host, int command)
1367 {
1368         struct nand_chip *chip = &host->chip;
1369         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1370
1371         nandc->buf_count = 0;
1372         nandc->buf_start = 0;
1373         host->use_ecc = false;
1374         host->last_command = command;
1375
1376         clear_read_regs(nandc);
1377
1378         if (command == NAND_CMD_RESET || command == NAND_CMD_READID ||
1379             command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1)
1380                 clear_bam_transaction(nandc);
1381 }
1382
1383 /*
1384  * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
1385  * privately maintained status byte, this status byte can be read after
1386  * NAND_CMD_STATUS is called
1387  */
1388 static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
1389 {
1390         struct nand_chip *chip = &host->chip;
1391         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1392         struct nand_ecc_ctrl *ecc = &chip->ecc;
1393         int num_cw;
1394         int i;
1395
1396         num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
1397         nandc_read_buffer_sync(nandc, true);
1398
1399         for (i = 0; i < num_cw; i++) {
1400                 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
1401
1402                 if (flash_status & FS_MPU_ERR)
1403                         host->status &= ~NAND_STATUS_WP;
1404
1405                 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
1406                                                  (flash_status &
1407                                                   FS_DEVICE_STS_ERR)))
1408                         host->status |= NAND_STATUS_FAIL;
1409         }
1410 }
1411
1412 static void post_command(struct qcom_nand_host *host, int command)
1413 {
1414         struct nand_chip *chip = &host->chip;
1415         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1416
1417         switch (command) {
1418         case NAND_CMD_READID:
1419                 nandc_read_buffer_sync(nandc, true);
1420                 memcpy(nandc->data_buffer, nandc->reg_read_buf,
1421                        nandc->buf_count);
1422                 break;
1423         case NAND_CMD_PAGEPROG:
1424         case NAND_CMD_ERASE1:
1425                 parse_erase_write_errors(host, command);
1426                 break;
1427         default:
1428                 break;
1429         }
1430 }
1431
1432 /*
1433  * Implements chip->legacy.cmdfunc. It's  only used for a limited set of
1434  * commands. The rest of the commands wouldn't be called by upper layers.
1435  * For example, NAND_CMD_READOOB would never be called because we have our own
1436  * versions of read_oob ops for nand_ecc_ctrl.
1437  */
1438 static void qcom_nandc_command(struct nand_chip *chip, unsigned int command,
1439                                int column, int page_addr)
1440 {
1441         struct qcom_nand_host *host = to_qcom_nand_host(chip);
1442         struct nand_ecc_ctrl *ecc = &chip->ecc;
1443         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1444         bool wait = false;
1445         int ret = 0;
1446
1447         pre_command(host, command);
1448
1449         switch (command) {
1450         case NAND_CMD_RESET:
1451                 ret = reset(host);
1452                 wait = true;
1453                 break;
1454
1455         case NAND_CMD_READID:
1456                 nandc->buf_count = 4;
1457                 ret = read_id(host, column);
1458                 wait = true;
1459                 break;
1460
1461         case NAND_CMD_PARAM:
1462                 ret = nandc_param(host);
1463                 wait = true;
1464                 break;
1465
1466         case NAND_CMD_ERASE1:
1467                 ret = erase_block(host, page_addr);
1468                 wait = true;
1469                 break;
1470
1471         case NAND_CMD_READ0:
1472                 /* we read the entire page for now */
1473                 WARN_ON(column != 0);
1474
1475                 host->use_ecc = true;
1476                 set_address(host, 0, page_addr);
1477                 update_rw_regs(host, ecc->steps, true);
1478                 break;
1479
1480         case NAND_CMD_SEQIN:
1481                 WARN_ON(column != 0);
1482                 set_address(host, 0, page_addr);
1483                 break;
1484
1485         case NAND_CMD_PAGEPROG:
1486         case NAND_CMD_STATUS:
1487         case NAND_CMD_NONE:
1488         default:
1489                 break;
1490         }
1491
1492         if (ret) {
1493                 dev_err(nandc->dev, "failure executing command %d\n",
1494                         command);
1495                 free_descs(nandc);
1496                 return;
1497         }
1498
1499         if (wait) {
1500                 ret = submit_descs(nandc);
1501                 if (ret)
1502                         dev_err(nandc->dev,
1503                                 "failure submitting descs for command %d\n",
1504                                 command);
1505         }
1506
1507         free_descs(nandc);
1508
1509         post_command(host, command);
1510 }
1511
1512 /*
1513  * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1514  * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1515  *
1516  * when using RS ECC, the HW reports the same erros when reading an erased CW,
1517  * but it notifies that it is an erased CW by placing special characters at
1518  * certain offsets in the buffer.
1519  *
1520  * verify if the page is erased or not, and fix up the page for RS ECC by
1521  * replacing the special characters with 0xff.
1522  */
1523 static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
1524 {
1525         u8 empty1, empty2;
1526
1527         /*
1528          * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1529          * is erased by looking for 0x54s at offsets 3 and 175 from the
1530          * beginning of each codeword
1531          */
1532
1533         empty1 = data_buf[3];
1534         empty2 = data_buf[175];
1535
1536         /*
1537          * if the erased codework markers, if they exist override them with
1538          * 0xffs
1539          */
1540         if ((empty1 == 0x54 && empty2 == 0xff) ||
1541             (empty1 == 0xff && empty2 == 0x54)) {
1542                 data_buf[3] = 0xff;
1543                 data_buf[175] = 0xff;
1544         }
1545
1546         /*
1547          * check if the entire chunk contains 0xffs or not. if it doesn't, then
1548          * restore the original values at the special offsets
1549          */
1550         if (memchr_inv(data_buf, 0xff, data_len)) {
1551                 data_buf[3] = empty1;
1552                 data_buf[175] = empty2;
1553
1554                 return false;
1555         }
1556
1557         return true;
1558 }
1559
1560 struct read_stats {
1561         __le32 flash;
1562         __le32 buffer;
1563         __le32 erased_cw;
1564 };
1565
1566 /* reads back FLASH_STATUS register set by the controller */
1567 static int check_flash_errors(struct qcom_nand_host *host, int cw_cnt)
1568 {
1569         struct nand_chip *chip = &host->chip;
1570         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1571         int i;
1572
1573         nandc_read_buffer_sync(nandc, true);
1574
1575         for (i = 0; i < cw_cnt; i++) {
1576                 u32 flash = le32_to_cpu(nandc->reg_read_buf[i]);
1577
1578                 if (flash & (FS_OP_ERR | FS_MPU_ERR))
1579                         return -EIO;
1580         }
1581
1582         return 0;
1583 }
1584
1585 /* performs raw read for one codeword */
1586 static int
1587 qcom_nandc_read_cw_raw(struct mtd_info *mtd, struct nand_chip *chip,
1588                        u8 *data_buf, u8 *oob_buf, int page, int cw)
1589 {
1590         struct qcom_nand_host *host = to_qcom_nand_host(chip);
1591         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1592         struct nand_ecc_ctrl *ecc = &chip->ecc;
1593         int data_size1, data_size2, oob_size1, oob_size2;
1594         int ret, reg_off = FLASH_BUF_ACC, read_loc = 0;
1595
1596         nand_read_page_op(chip, page, 0, NULL, 0);
1597         host->use_ecc = false;
1598
1599         clear_bam_transaction(nandc);
1600         set_address(host, host->cw_size * cw, page);
1601         update_rw_regs(host, 1, true);
1602         config_nand_page_read(nandc);
1603
1604         data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1605         oob_size1 = host->bbm_size;
1606
1607         if (cw == (ecc->steps - 1)) {
1608                 data_size2 = ecc->size - data_size1 -
1609                              ((ecc->steps - 1) * 4);
1610                 oob_size2 = (ecc->steps * 4) + host->ecc_bytes_hw +
1611                             host->spare_bytes;
1612         } else {
1613                 data_size2 = host->cw_data - data_size1;
1614                 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1615         }
1616
1617         if (nandc->props->is_bam) {
1618                 nandc_set_read_loc(nandc, 0, read_loc, data_size1, 0);
1619                 read_loc += data_size1;
1620
1621                 nandc_set_read_loc(nandc, 1, read_loc, oob_size1, 0);
1622                 read_loc += oob_size1;
1623
1624                 nandc_set_read_loc(nandc, 2, read_loc, data_size2, 0);
1625                 read_loc += data_size2;
1626
1627                 nandc_set_read_loc(nandc, 3, read_loc, oob_size2, 1);
1628         }
1629
1630         config_nand_cw_read(nandc, false);
1631
1632         read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
1633         reg_off += data_size1;
1634
1635         read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
1636         reg_off += oob_size1;
1637
1638         read_data_dma(nandc, reg_off, data_buf + data_size1, data_size2, 0);
1639         reg_off += data_size2;
1640
1641         read_data_dma(nandc, reg_off, oob_buf + oob_size1, oob_size2, 0);
1642
1643         ret = submit_descs(nandc);
1644         free_descs(nandc);
1645         if (ret) {
1646                 dev_err(nandc->dev, "failure to read raw cw %d\n", cw);
1647                 return ret;
1648         }
1649
1650         return check_flash_errors(host, 1);
1651 }
1652
1653 /*
1654  * Bitflips can happen in erased codewords also so this function counts the
1655  * number of 0 in each CW for which ECC engine returns the uncorrectable
1656  * error. The page will be assumed as erased if this count is less than or
1657  * equal to the ecc->strength for each CW.
1658  *
1659  * 1. Both DATA and OOB need to be checked for number of 0. The
1660  *    top-level API can be called with only data buf or OOB buf so use
1661  *    chip->data_buf if data buf is null and chip->oob_poi if oob buf
1662  *    is null for copying the raw bytes.
1663  * 2. Perform raw read for all the CW which has uncorrectable errors.
1664  * 3. For each CW, check the number of 0 in cw_data and usable OOB bytes.
1665  *    The BBM and spare bytes bit flip won’t affect the ECC so don’t check
1666  *    the number of bitflips in this area.
1667  */
1668 static int
1669 check_for_erased_page(struct qcom_nand_host *host, u8 *data_buf,
1670                       u8 *oob_buf, unsigned long uncorrectable_cws,
1671                       int page, unsigned int max_bitflips)
1672 {
1673         struct nand_chip *chip = &host->chip;
1674         struct mtd_info *mtd = nand_to_mtd(chip);
1675         struct nand_ecc_ctrl *ecc = &chip->ecc;
1676         u8 *cw_data_buf, *cw_oob_buf;
1677         int cw, data_size, oob_size, ret = 0;
1678
1679         if (!data_buf)
1680                 data_buf = nand_get_data_buf(chip);
1681
1682         if (!oob_buf) {
1683                 nand_get_data_buf(chip);
1684                 oob_buf = chip->oob_poi;
1685         }
1686
1687         for_each_set_bit(cw, &uncorrectable_cws, ecc->steps) {
1688                 if (cw == (ecc->steps - 1)) {
1689                         data_size = ecc->size - ((ecc->steps - 1) * 4);
1690                         oob_size = (ecc->steps * 4) + host->ecc_bytes_hw;
1691                 } else {
1692                         data_size = host->cw_data;
1693                         oob_size = host->ecc_bytes_hw;
1694                 }
1695
1696                 /* determine starting buffer address for current CW */
1697                 cw_data_buf = data_buf + (cw * host->cw_data);
1698                 cw_oob_buf = oob_buf + (cw * ecc->bytes);
1699
1700                 ret = qcom_nandc_read_cw_raw(mtd, chip, cw_data_buf,
1701                                              cw_oob_buf, page, cw);
1702                 if (ret)
1703                         return ret;
1704
1705                 /*
1706                  * make sure it isn't an erased page reported
1707                  * as not-erased by HW because of a few bitflips
1708                  */
1709                 ret = nand_check_erased_ecc_chunk(cw_data_buf, data_size,
1710                                                   cw_oob_buf + host->bbm_size,
1711                                                   oob_size, NULL,
1712                                                   0, ecc->strength);
1713                 if (ret < 0) {
1714                         mtd->ecc_stats.failed++;
1715                 } else {
1716                         mtd->ecc_stats.corrected += ret;
1717                         max_bitflips = max_t(unsigned int, max_bitflips, ret);
1718                 }
1719         }
1720
1721         return max_bitflips;
1722 }
1723
1724 /*
1725  * reads back status registers set by the controller to notify page read
1726  * errors. this is equivalent to what 'ecc->correct()' would do.
1727  */
1728 static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1729                              u8 *oob_buf, int page)
1730 {
1731         struct nand_chip *chip = &host->chip;
1732         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1733         struct mtd_info *mtd = nand_to_mtd(chip);
1734         struct nand_ecc_ctrl *ecc = &chip->ecc;
1735         unsigned int max_bitflips = 0, uncorrectable_cws = 0;
1736         struct read_stats *buf;
1737         bool flash_op_err = false, erased;
1738         int i;
1739         u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
1740
1741         buf = (struct read_stats *)nandc->reg_read_buf;
1742         nandc_read_buffer_sync(nandc, true);
1743
1744         for (i = 0; i < ecc->steps; i++, buf++) {
1745                 u32 flash, buffer, erased_cw;
1746                 int data_len, oob_len;
1747
1748                 if (i == (ecc->steps - 1)) {
1749                         data_len = ecc->size - ((ecc->steps - 1) << 2);
1750                         oob_len = ecc->steps << 2;
1751                 } else {
1752                         data_len = host->cw_data;
1753                         oob_len = 0;
1754                 }
1755
1756                 flash = le32_to_cpu(buf->flash);
1757                 buffer = le32_to_cpu(buf->buffer);
1758                 erased_cw = le32_to_cpu(buf->erased_cw);
1759
1760                 /*
1761                  * Check ECC failure for each codeword. ECC failure can
1762                  * happen in either of the following conditions
1763                  * 1. If number of bitflips are greater than ECC engine
1764                  *    capability.
1765                  * 2. If this codeword contains all 0xff for which erased
1766                  *    codeword detection check will be done.
1767                  */
1768                 if ((flash & FS_OP_ERR) && (buffer & BS_UNCORRECTABLE_BIT)) {
1769                         /*
1770                          * For BCH ECC, ignore erased codeword errors, if
1771                          * ERASED_CW bits are set.
1772                          */
1773                         if (host->bch_enabled) {
1774                                 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1775                                          true : false;
1776                         /*
1777                          * For RS ECC, HW reports the erased CW by placing
1778                          * special characters at certain offsets in the buffer.
1779                          * These special characters will be valid only if
1780                          * complete page is read i.e. data_buf is not NULL.
1781                          */
1782                         } else if (data_buf) {
1783                                 erased = erased_chunk_check_and_fixup(data_buf,
1784                                                                       data_len);
1785                         } else {
1786                                 erased = false;
1787                         }
1788
1789                         if (!erased)
1790                                 uncorrectable_cws |= BIT(i);
1791                 /*
1792                  * Check if MPU or any other operational error (timeout,
1793                  * device failure, etc.) happened for this codeword and
1794                  * make flash_op_err true. If flash_op_err is set, then
1795                  * EIO will be returned for page read.
1796                  */
1797                 } else if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1798                         flash_op_err = true;
1799                 /*
1800                  * No ECC or operational errors happened. Check the number of
1801                  * bits corrected and update the ecc_stats.corrected.
1802                  */
1803                 } else {
1804                         unsigned int stat;
1805
1806                         stat = buffer & BS_CORRECTABLE_ERR_MSK;
1807                         mtd->ecc_stats.corrected += stat;
1808                         max_bitflips = max(max_bitflips, stat);
1809                 }
1810
1811                 if (data_buf)
1812                         data_buf += data_len;
1813                 if (oob_buf)
1814                         oob_buf += oob_len + ecc->bytes;
1815         }
1816
1817         if (flash_op_err)
1818                 return -EIO;
1819
1820         if (!uncorrectable_cws)
1821                 return max_bitflips;
1822
1823         return check_for_erased_page(host, data_buf_start, oob_buf_start,
1824                                      uncorrectable_cws, page,
1825                                      max_bitflips);
1826 }
1827
1828 /*
1829  * helper to perform the actual page read operation, used by ecc->read_page(),
1830  * ecc->read_oob()
1831  */
1832 static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1833                          u8 *oob_buf, int page)
1834 {
1835         struct nand_chip *chip = &host->chip;
1836         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1837         struct nand_ecc_ctrl *ecc = &chip->ecc;
1838         u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
1839         int i, ret;
1840
1841         config_nand_page_read(nandc);
1842
1843         /* queue cmd descs for each codeword */
1844         for (i = 0; i < ecc->steps; i++) {
1845                 int data_size, oob_size;
1846
1847                 if (i == (ecc->steps - 1)) {
1848                         data_size = ecc->size - ((ecc->steps - 1) << 2);
1849                         oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1850                                    host->spare_bytes;
1851                 } else {
1852                         data_size = host->cw_data;
1853                         oob_size = host->ecc_bytes_hw + host->spare_bytes;
1854                 }
1855
1856                 if (nandc->props->is_bam) {
1857                         if (data_buf && oob_buf) {
1858                                 nandc_set_read_loc(nandc, 0, 0, data_size, 0);
1859                                 nandc_set_read_loc(nandc, 1, data_size,
1860                                                    oob_size, 1);
1861                         } else if (data_buf) {
1862                                 nandc_set_read_loc(nandc, 0, 0, data_size, 1);
1863                         } else {
1864                                 nandc_set_read_loc(nandc, 0, data_size,
1865                                                    oob_size, 1);
1866                         }
1867                 }
1868
1869                 config_nand_cw_read(nandc, true);
1870
1871                 if (data_buf)
1872                         read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
1873                                       data_size, 0);
1874
1875                 /*
1876                  * when ecc is enabled, the controller doesn't read the real
1877                  * or dummy bad block markers in each chunk. To maintain a
1878                  * consistent layout across RAW and ECC reads, we just
1879                  * leave the real/dummy BBM offsets empty (i.e, filled with
1880                  * 0xffs)
1881                  */
1882                 if (oob_buf) {
1883                         int j;
1884
1885                         for (j = 0; j < host->bbm_size; j++)
1886                                 *oob_buf++ = 0xff;
1887
1888                         read_data_dma(nandc, FLASH_BUF_ACC + data_size,
1889                                       oob_buf, oob_size, 0);
1890                 }
1891
1892                 if (data_buf)
1893                         data_buf += data_size;
1894                 if (oob_buf)
1895                         oob_buf += oob_size;
1896         }
1897
1898         ret = submit_descs(nandc);
1899         free_descs(nandc);
1900
1901         if (ret) {
1902                 dev_err(nandc->dev, "failure to read page/oob\n");
1903                 return ret;
1904         }
1905
1906         return parse_read_errors(host, data_buf_start, oob_buf_start, page);
1907 }
1908
1909 /*
1910  * a helper that copies the last step/codeword of a page (containing free oob)
1911  * into our local buffer
1912  */
1913 static int copy_last_cw(struct qcom_nand_host *host, int page)
1914 {
1915         struct nand_chip *chip = &host->chip;
1916         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1917         struct nand_ecc_ctrl *ecc = &chip->ecc;
1918         int size;
1919         int ret;
1920
1921         clear_read_regs(nandc);
1922
1923         size = host->use_ecc ? host->cw_data : host->cw_size;
1924
1925         /* prepare a clean read buffer */
1926         memset(nandc->data_buffer, 0xff, size);
1927
1928         set_address(host, host->cw_size * (ecc->steps - 1), page);
1929         update_rw_regs(host, 1, true);
1930
1931         config_nand_single_cw_page_read(nandc, host->use_ecc);
1932
1933         read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
1934
1935         ret = submit_descs(nandc);
1936         if (ret)
1937                 dev_err(nandc->dev, "failed to copy last codeword\n");
1938
1939         free_descs(nandc);
1940
1941         return ret;
1942 }
1943
1944 /* implements ecc->read_page() */
1945 static int qcom_nandc_read_page(struct nand_chip *chip, uint8_t *buf,
1946                                 int oob_required, int page)
1947 {
1948         struct qcom_nand_host *host = to_qcom_nand_host(chip);
1949         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1950         u8 *data_buf, *oob_buf = NULL;
1951
1952         nand_read_page_op(chip, page, 0, NULL, 0);
1953         data_buf = buf;
1954         oob_buf = oob_required ? chip->oob_poi : NULL;
1955
1956         clear_bam_transaction(nandc);
1957
1958         return read_page_ecc(host, data_buf, oob_buf, page);
1959 }
1960
1961 /* implements ecc->read_page_raw() */
1962 static int qcom_nandc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
1963                                     int oob_required, int page)
1964 {
1965         struct mtd_info *mtd = nand_to_mtd(chip);
1966         struct qcom_nand_host *host = to_qcom_nand_host(chip);
1967         struct nand_ecc_ctrl *ecc = &chip->ecc;
1968         int cw, ret;
1969         u8 *data_buf = buf, *oob_buf = chip->oob_poi;
1970
1971         for (cw = 0; cw < ecc->steps; cw++) {
1972                 ret = qcom_nandc_read_cw_raw(mtd, chip, data_buf, oob_buf,
1973                                              page, cw);
1974                 if (ret)
1975                         return ret;
1976
1977                 data_buf += host->cw_data;
1978                 oob_buf += ecc->bytes;
1979         }
1980
1981         return 0;
1982 }
1983
1984 /* implements ecc->read_oob() */
1985 static int qcom_nandc_read_oob(struct nand_chip *chip, int page)
1986 {
1987         struct qcom_nand_host *host = to_qcom_nand_host(chip);
1988         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1989         struct nand_ecc_ctrl *ecc = &chip->ecc;
1990
1991         clear_read_regs(nandc);
1992         clear_bam_transaction(nandc);
1993
1994         host->use_ecc = true;
1995         set_address(host, 0, page);
1996         update_rw_regs(host, ecc->steps, true);
1997
1998         return read_page_ecc(host, NULL, chip->oob_poi, page);
1999 }
2000
2001 /* implements ecc->write_page() */
2002 static int qcom_nandc_write_page(struct nand_chip *chip, const uint8_t *buf,
2003                                  int oob_required, int page)
2004 {
2005         struct qcom_nand_host *host = to_qcom_nand_host(chip);
2006         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2007         struct nand_ecc_ctrl *ecc = &chip->ecc;
2008         u8 *data_buf, *oob_buf;
2009         int i, ret;
2010
2011         nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2012
2013         clear_read_regs(nandc);
2014         clear_bam_transaction(nandc);
2015
2016         data_buf = (u8 *)buf;
2017         oob_buf = chip->oob_poi;
2018
2019         host->use_ecc = true;
2020         update_rw_regs(host, ecc->steps, false);
2021         config_nand_page_write(nandc);
2022
2023         for (i = 0; i < ecc->steps; i++) {
2024                 int data_size, oob_size;
2025
2026                 if (i == (ecc->steps - 1)) {
2027                         data_size = ecc->size - ((ecc->steps - 1) << 2);
2028                         oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
2029                                    host->spare_bytes;
2030                 } else {
2031                         data_size = host->cw_data;
2032                         oob_size = ecc->bytes;
2033                 }
2034
2035
2036                 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
2037                                i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
2038
2039                 /*
2040                  * when ECC is enabled, we don't really need to write anything
2041                  * to oob for the first n - 1 codewords since these oob regions
2042                  * just contain ECC bytes that's written by the controller
2043                  * itself. For the last codeword, we skip the bbm positions and
2044                  * write to the free oob area.
2045                  */
2046                 if (i == (ecc->steps - 1)) {
2047                         oob_buf += host->bbm_size;
2048
2049                         write_data_dma(nandc, FLASH_BUF_ACC + data_size,
2050                                        oob_buf, oob_size, 0);
2051                 }
2052
2053                 config_nand_cw_write(nandc);
2054
2055                 data_buf += data_size;
2056                 oob_buf += oob_size;
2057         }
2058
2059         ret = submit_descs(nandc);
2060         if (ret)
2061                 dev_err(nandc->dev, "failure to write page\n");
2062
2063         free_descs(nandc);
2064
2065         if (!ret)
2066                 ret = nand_prog_page_end_op(chip);
2067
2068         return ret;
2069 }
2070
2071 /* implements ecc->write_page_raw() */
2072 static int qcom_nandc_write_page_raw(struct nand_chip *chip,
2073                                      const uint8_t *buf, int oob_required,
2074                                      int page)
2075 {
2076         struct mtd_info *mtd = nand_to_mtd(chip);
2077         struct qcom_nand_host *host = to_qcom_nand_host(chip);
2078         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2079         struct nand_ecc_ctrl *ecc = &chip->ecc;
2080         u8 *data_buf, *oob_buf;
2081         int i, ret;
2082
2083         nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2084         clear_read_regs(nandc);
2085         clear_bam_transaction(nandc);
2086
2087         data_buf = (u8 *)buf;
2088         oob_buf = chip->oob_poi;
2089
2090         host->use_ecc = false;
2091         update_rw_regs(host, ecc->steps, false);
2092         config_nand_page_write(nandc);
2093
2094         for (i = 0; i < ecc->steps; i++) {
2095                 int data_size1, data_size2, oob_size1, oob_size2;
2096                 int reg_off = FLASH_BUF_ACC;
2097
2098                 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
2099                 oob_size1 = host->bbm_size;
2100
2101                 if (i == (ecc->steps - 1)) {
2102                         data_size2 = ecc->size - data_size1 -
2103                                      ((ecc->steps - 1) << 2);
2104                         oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
2105                                     host->spare_bytes;
2106                 } else {
2107                         data_size2 = host->cw_data - data_size1;
2108                         oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
2109                 }
2110
2111                 write_data_dma(nandc, reg_off, data_buf, data_size1,
2112                                NAND_BAM_NO_EOT);
2113                 reg_off += data_size1;
2114                 data_buf += data_size1;
2115
2116                 write_data_dma(nandc, reg_off, oob_buf, oob_size1,
2117                                NAND_BAM_NO_EOT);
2118                 reg_off += oob_size1;
2119                 oob_buf += oob_size1;
2120
2121                 write_data_dma(nandc, reg_off, data_buf, data_size2,
2122                                NAND_BAM_NO_EOT);
2123                 reg_off += data_size2;
2124                 data_buf += data_size2;
2125
2126                 write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
2127                 oob_buf += oob_size2;
2128
2129                 config_nand_cw_write(nandc);
2130         }
2131
2132         ret = submit_descs(nandc);
2133         if (ret)
2134                 dev_err(nandc->dev, "failure to write raw page\n");
2135
2136         free_descs(nandc);
2137
2138         if (!ret)
2139                 ret = nand_prog_page_end_op(chip);
2140
2141         return ret;
2142 }
2143
2144 /*
2145  * implements ecc->write_oob()
2146  *
2147  * the NAND controller cannot write only data or only OOB within a codeword
2148  * since ECC is calculated for the combined codeword. So update the OOB from
2149  * chip->oob_poi, and pad the data area with OxFF before writing.
2150  */
2151 static int qcom_nandc_write_oob(struct nand_chip *chip, int page)
2152 {
2153         struct mtd_info *mtd = nand_to_mtd(chip);
2154         struct qcom_nand_host *host = to_qcom_nand_host(chip);
2155         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2156         struct nand_ecc_ctrl *ecc = &chip->ecc;
2157         u8 *oob = chip->oob_poi;
2158         int data_size, oob_size;
2159         int ret;
2160
2161         host->use_ecc = true;
2162         clear_bam_transaction(nandc);
2163
2164         /* calculate the data and oob size for the last codeword/step */
2165         data_size = ecc->size - ((ecc->steps - 1) << 2);
2166         oob_size = mtd->oobavail;
2167
2168         memset(nandc->data_buffer, 0xff, host->cw_data);
2169         /* override new oob content to last codeword */
2170         mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
2171                                     0, mtd->oobavail);
2172
2173         set_address(host, host->cw_size * (ecc->steps - 1), page);
2174         update_rw_regs(host, 1, false);
2175
2176         config_nand_page_write(nandc);
2177         write_data_dma(nandc, FLASH_BUF_ACC,
2178                        nandc->data_buffer, data_size + oob_size, 0);
2179         config_nand_cw_write(nandc);
2180
2181         ret = submit_descs(nandc);
2182
2183         free_descs(nandc);
2184
2185         if (ret) {
2186                 dev_err(nandc->dev, "failure to write oob\n");
2187                 return -EIO;
2188         }
2189
2190         return nand_prog_page_end_op(chip);
2191 }
2192
2193 static int qcom_nandc_block_bad(struct nand_chip *chip, loff_t ofs)
2194 {
2195         struct mtd_info *mtd = nand_to_mtd(chip);
2196         struct qcom_nand_host *host = to_qcom_nand_host(chip);
2197         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2198         struct nand_ecc_ctrl *ecc = &chip->ecc;
2199         int page, ret, bbpos, bad = 0;
2200
2201         page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2202
2203         /*
2204          * configure registers for a raw sub page read, the address is set to
2205          * the beginning of the last codeword, we don't care about reading ecc
2206          * portion of oob. we just want the first few bytes from this codeword
2207          * that contains the BBM
2208          */
2209         host->use_ecc = false;
2210
2211         clear_bam_transaction(nandc);
2212         ret = copy_last_cw(host, page);
2213         if (ret)
2214                 goto err;
2215
2216         if (check_flash_errors(host, 1)) {
2217                 dev_warn(nandc->dev, "error when trying to read BBM\n");
2218                 goto err;
2219         }
2220
2221         bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
2222
2223         bad = nandc->data_buffer[bbpos] != 0xff;
2224
2225         if (chip->options & NAND_BUSWIDTH_16)
2226                 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
2227 err:
2228         return bad;
2229 }
2230
2231 static int qcom_nandc_block_markbad(struct nand_chip *chip, loff_t ofs)
2232 {
2233         struct qcom_nand_host *host = to_qcom_nand_host(chip);
2234         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2235         struct nand_ecc_ctrl *ecc = &chip->ecc;
2236         int page, ret;
2237
2238         clear_read_regs(nandc);
2239         clear_bam_transaction(nandc);
2240
2241         /*
2242          * to mark the BBM as bad, we flash the entire last codeword with 0s.
2243          * we don't care about the rest of the content in the codeword since
2244          * we aren't going to use this block again
2245          */
2246         memset(nandc->data_buffer, 0x00, host->cw_size);
2247
2248         page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2249
2250         /* prepare write */
2251         host->use_ecc = false;
2252         set_address(host, host->cw_size * (ecc->steps - 1), page);
2253         update_rw_regs(host, 1, false);
2254
2255         config_nand_page_write(nandc);
2256         write_data_dma(nandc, FLASH_BUF_ACC,
2257                        nandc->data_buffer, host->cw_size, 0);
2258         config_nand_cw_write(nandc);
2259
2260         ret = submit_descs(nandc);
2261
2262         free_descs(nandc);
2263
2264         if (ret) {
2265                 dev_err(nandc->dev, "failure to update BBM\n");
2266                 return -EIO;
2267         }
2268
2269         return nand_prog_page_end_op(chip);
2270 }
2271
2272 /*
2273  * the three functions below implement chip->legacy.read_byte(),
2274  * chip->legacy.read_buf() and chip->legacy.write_buf() respectively. these
2275  * aren't used for reading/writing page data, they are used for smaller data
2276  * like reading id, status etc
2277  */
2278 static uint8_t qcom_nandc_read_byte(struct nand_chip *chip)
2279 {
2280         struct qcom_nand_host *host = to_qcom_nand_host(chip);
2281         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2282         u8 *buf = nandc->data_buffer;
2283         u8 ret = 0x0;
2284
2285         if (host->last_command == NAND_CMD_STATUS) {
2286                 ret = host->status;
2287
2288                 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2289
2290                 return ret;
2291         }
2292
2293         if (nandc->buf_start < nandc->buf_count)
2294                 ret = buf[nandc->buf_start++];
2295
2296         return ret;
2297 }
2298
2299 static void qcom_nandc_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
2300 {
2301         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2302         int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2303
2304         memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
2305         nandc->buf_start += real_len;
2306 }
2307
2308 static void qcom_nandc_write_buf(struct nand_chip *chip, const uint8_t *buf,
2309                                  int len)
2310 {
2311         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2312         int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2313
2314         memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
2315
2316         nandc->buf_start += real_len;
2317 }
2318
2319 /* we support only one external chip for now */
2320 static void qcom_nandc_select_chip(struct nand_chip *chip, int chipnr)
2321 {
2322         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2323
2324         if (chipnr <= 0)
2325                 return;
2326
2327         dev_warn(nandc->dev, "invalid chip select\n");
2328 }
2329
2330 /*
2331  * NAND controller page layout info
2332  *
2333  * Layout with ECC enabled:
2334  *
2335  * |----------------------|  |---------------------------------|
2336  * |           xx.......yy|  |             *********xx.......yy|
2337  * |    DATA   xx..ECC..yy|  |    DATA     **SPARE**xx..ECC..yy|
2338  * |   (516)   xx.......yy|  |  (516-n*4)  **(n*4)**xx.......yy|
2339  * |           xx.......yy|  |             *********xx.......yy|
2340  * |----------------------|  |---------------------------------|
2341  *     codeword 1,2..n-1                  codeword n
2342  *  <---(528/532 Bytes)-->    <-------(528/532 Bytes)--------->
2343  *
2344  * n = Number of codewords in the page
2345  * . = ECC bytes
2346  * * = Spare/free bytes
2347  * x = Unused byte(s)
2348  * y = Reserved byte(s)
2349  *
2350  * 2K page: n = 4, spare = 16 bytes
2351  * 4K page: n = 8, spare = 32 bytes
2352  * 8K page: n = 16, spare = 64 bytes
2353  *
2354  * the qcom nand controller operates at a sub page/codeword level. each
2355  * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2356  * the number of ECC bytes vary based on the ECC strength and the bus width.
2357  *
2358  * the first n - 1 codewords contains 516 bytes of user data, the remaining
2359  * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2360  * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2361  *
2362  * When we access a page with ECC enabled, the reserved bytes(s) are not
2363  * accessible at all. When reading, we fill up these unreadable positions
2364  * with 0xffs. When writing, the controller skips writing the inaccessible
2365  * bytes.
2366  *
2367  * Layout with ECC disabled:
2368  *
2369  * |------------------------------|  |---------------------------------------|
2370  * |         yy          xx.......|  |         bb          *********xx.......|
2371  * |  DATA1  yy  DATA2   xx..ECC..|  |  DATA1  bb  DATA2   **SPARE**xx..ECC..|
2372  * | (size1) yy (size2)  xx.......|  | (size1) bb (size2)  **(n*4)**xx.......|
2373  * |         yy          xx.......|  |         bb          *********xx.......|
2374  * |------------------------------|  |---------------------------------------|
2375  *         codeword 1,2..n-1                        codeword n
2376  *  <-------(528/532 Bytes)------>    <-----------(528/532 Bytes)----------->
2377  *
2378  * n = Number of codewords in the page
2379  * . = ECC bytes
2380  * * = Spare/free bytes
2381  * x = Unused byte(s)
2382  * y = Dummy Bad Bock byte(s)
2383  * b = Real Bad Block byte(s)
2384  * size1/size2 = function of codeword size and 'n'
2385  *
2386  * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2387  * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2388  * Block Markers. In the last codeword, this position contains the real BBM
2389  *
2390  * In order to have a consistent layout between RAW and ECC modes, we assume
2391  * the following OOB layout arrangement:
2392  *
2393  * |-----------|  |--------------------|
2394  * |yyxx.......|  |bb*********xx.......|
2395  * |yyxx..ECC..|  |bb*FREEOOB*xx..ECC..|
2396  * |yyxx.......|  |bb*********xx.......|
2397  * |yyxx.......|  |bb*********xx.......|
2398  * |-----------|  |--------------------|
2399  *  first n - 1       nth OOB region
2400  *  OOB regions
2401  *
2402  * n = Number of codewords in the page
2403  * . = ECC bytes
2404  * * = FREE OOB bytes
2405  * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2406  * x = Unused byte(s)
2407  * b = Real bad block byte(s) (inaccessible when ECC enabled)
2408  *
2409  * This layout is read as is when ECC is disabled. When ECC is enabled, the
2410  * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2411  * and assumed as 0xffs when we read a page/oob. The ECC, unused and
2412  * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2413  * the sum of the three).
2414  */
2415 static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2416                                    struct mtd_oob_region *oobregion)
2417 {
2418         struct nand_chip *chip = mtd_to_nand(mtd);
2419         struct qcom_nand_host *host = to_qcom_nand_host(chip);
2420         struct nand_ecc_ctrl *ecc = &chip->ecc;
2421
2422         if (section > 1)
2423                 return -ERANGE;
2424
2425         if (!section) {
2426                 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
2427                                     host->bbm_size;
2428                 oobregion->offset = 0;
2429         } else {
2430                 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
2431                 oobregion->offset = mtd->oobsize - oobregion->length;
2432         }
2433
2434         return 0;
2435 }
2436
2437 static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
2438                                      struct mtd_oob_region *oobregion)
2439 {
2440         struct nand_chip *chip = mtd_to_nand(mtd);
2441         struct qcom_nand_host *host = to_qcom_nand_host(chip);
2442         struct nand_ecc_ctrl *ecc = &chip->ecc;
2443
2444         if (section)
2445                 return -ERANGE;
2446
2447         oobregion->length = ecc->steps * 4;
2448         oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
2449
2450         return 0;
2451 }
2452
2453 static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
2454         .ecc = qcom_nand_ooblayout_ecc,
2455         .free = qcom_nand_ooblayout_free,
2456 };
2457
2458 static int
2459 qcom_nandc_calc_ecc_bytes(int step_size, int strength)
2460 {
2461         return strength == 4 ? 12 : 16;
2462 }
2463 NAND_ECC_CAPS_SINGLE(qcom_nandc_ecc_caps, qcom_nandc_calc_ecc_bytes,
2464                      NANDC_STEP_SIZE, 4, 8);
2465
2466 static int qcom_nand_attach_chip(struct nand_chip *chip)
2467 {
2468         struct mtd_info *mtd = nand_to_mtd(chip);
2469         struct qcom_nand_host *host = to_qcom_nand_host(chip);
2470         struct nand_ecc_ctrl *ecc = &chip->ecc;
2471         struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2472         int cwperpage, bad_block_byte, ret;
2473         bool wide_bus;
2474         int ecc_mode = 1;
2475
2476         /* controller only supports 512 bytes data steps */
2477         ecc->size = NANDC_STEP_SIZE;
2478         wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
2479         cwperpage = mtd->writesize / NANDC_STEP_SIZE;
2480
2481         /*
2482          * Each CW has 4 available OOB bytes which will be protected with ECC
2483          * so remaining bytes can be used for ECC.
2484          */
2485         ret = nand_ecc_choose_conf(chip, &qcom_nandc_ecc_caps,
2486                                    mtd->oobsize - (cwperpage * 4));
2487         if (ret) {
2488                 dev_err(nandc->dev, "No valid ECC settings possible\n");
2489                 return ret;
2490         }
2491
2492         if (ecc->strength >= 8) {
2493                 /* 8 bit ECC defaults to BCH ECC on all platforms */
2494                 host->bch_enabled = true;
2495                 ecc_mode = 1;
2496
2497                 if (wide_bus) {
2498                         host->ecc_bytes_hw = 14;
2499                         host->spare_bytes = 0;
2500                         host->bbm_size = 2;
2501                 } else {
2502                         host->ecc_bytes_hw = 13;
2503                         host->spare_bytes = 2;
2504                         host->bbm_size = 1;
2505                 }
2506         } else {
2507                 /*
2508                  * if the controller supports BCH for 4 bit ECC, the controller
2509                  * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2510                  * always 10 bytes
2511                  */
2512                 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
2513                         /* BCH */
2514                         host->bch_enabled = true;
2515                         ecc_mode = 0;
2516
2517                         if (wide_bus) {
2518                                 host->ecc_bytes_hw = 8;
2519                                 host->spare_bytes = 2;
2520                                 host->bbm_size = 2;
2521                         } else {
2522                                 host->ecc_bytes_hw = 7;
2523                                 host->spare_bytes = 4;
2524                                 host->bbm_size = 1;
2525                         }
2526                 } else {
2527                         /* RS */
2528                         host->ecc_bytes_hw = 10;
2529
2530                         if (wide_bus) {
2531                                 host->spare_bytes = 0;
2532                                 host->bbm_size = 2;
2533                         } else {
2534                                 host->spare_bytes = 1;
2535                                 host->bbm_size = 1;
2536                         }
2537                 }
2538         }
2539
2540         /*
2541          * we consider ecc->bytes as the sum of all the non-data content in a
2542          * step. It gives us a clean representation of the oob area (even if
2543          * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2544          * ECC and 12 bytes for 4 bit ECC
2545          */
2546         ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
2547
2548         ecc->read_page          = qcom_nandc_read_page;
2549         ecc->read_page_raw      = qcom_nandc_read_page_raw;
2550         ecc->read_oob           = qcom_nandc_read_oob;
2551         ecc->write_page         = qcom_nandc_write_page;
2552         ecc->write_page_raw     = qcom_nandc_write_page_raw;
2553         ecc->write_oob          = qcom_nandc_write_oob;
2554
2555         ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
2556
2557         mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
2558
2559         nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
2560                                      cwperpage);
2561
2562         /*
2563          * DATA_UD_BYTES varies based on whether the read/write command protects
2564          * spare data with ECC too. We protect spare data by default, so we set
2565          * it to main + spare data, which are 512 and 4 bytes respectively.
2566          */
2567         host->cw_data = 516;
2568
2569         /*
2570          * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2571          * for 8 bit ECC
2572          */
2573         host->cw_size = host->cw_data + ecc->bytes;
2574         bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
2575
2576         host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
2577                                 | host->cw_data << UD_SIZE_BYTES
2578                                 | 0 << DISABLE_STATUS_AFTER_WRITE
2579                                 | 5 << NUM_ADDR_CYCLES
2580                                 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
2581                                 | 0 << STATUS_BFR_READ
2582                                 | 1 << SET_RD_MODE_AFTER_STATUS
2583                                 | host->spare_bytes << SPARE_SIZE_BYTES;
2584
2585         host->cfg1 = 7 << NAND_RECOVERY_CYCLES
2586                                 | 0 <<  CS_ACTIVE_BSY
2587                                 | bad_block_byte << BAD_BLOCK_BYTE_NUM
2588                                 | 0 << BAD_BLOCK_IN_SPARE_AREA
2589                                 | 2 << WR_RD_BSY_GAP
2590                                 | wide_bus << WIDE_FLASH
2591                                 | host->bch_enabled << ENABLE_BCH_ECC;
2592
2593         host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
2594                                 | host->cw_size << UD_SIZE_BYTES
2595                                 | 5 << NUM_ADDR_CYCLES
2596                                 | 0 << SPARE_SIZE_BYTES;
2597
2598         host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
2599                                 | 0 << CS_ACTIVE_BSY
2600                                 | 17 << BAD_BLOCK_BYTE_NUM
2601                                 | 1 << BAD_BLOCK_IN_SPARE_AREA
2602                                 | 2 << WR_RD_BSY_GAP
2603                                 | wide_bus << WIDE_FLASH
2604                                 | 1 << DEV0_CFG1_ECC_DISABLE;
2605
2606         host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
2607                                 | 0 << ECC_SW_RESET
2608                                 | host->cw_data << ECC_NUM_DATA_BYTES
2609                                 | 1 << ECC_FORCE_CLK_OPEN
2610                                 | ecc_mode << ECC_MODE
2611                                 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
2612
2613         host->ecc_buf_cfg = 0x203 << NUM_STEPS;
2614
2615         host->clrflashstatus = FS_READY_BSY_N;
2616         host->clrreadstatus = 0xc0;
2617         nandc->regs->erased_cw_detect_cfg_clr =
2618                 cpu_to_le32(CLR_ERASED_PAGE_DET);
2619         nandc->regs->erased_cw_detect_cfg_set =
2620                 cpu_to_le32(SET_ERASED_PAGE_DET);
2621
2622         dev_dbg(nandc->dev,
2623                 "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",
2624                 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
2625                 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
2626                 cwperpage);
2627
2628         return 0;
2629 }
2630
2631 static const struct nand_controller_ops qcom_nandc_ops = {
2632         .attach_chip = qcom_nand_attach_chip,
2633 };
2634
2635 static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2636 {
2637         if (nandc->props->is_bam) {
2638                 if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
2639                         dma_unmap_single(nandc->dev, nandc->reg_read_dma,
2640                                          MAX_REG_RD *
2641                                          sizeof(*nandc->reg_read_buf),
2642                                          DMA_FROM_DEVICE);
2643
2644                 if (nandc->tx_chan)
2645                         dma_release_channel(nandc->tx_chan);
2646
2647                 if (nandc->rx_chan)
2648                         dma_release_channel(nandc->rx_chan);
2649
2650                 if (nandc->cmd_chan)
2651                         dma_release_channel(nandc->cmd_chan);
2652         } else {
2653                 if (nandc->chan)
2654                         dma_release_channel(nandc->chan);
2655         }
2656 }
2657
2658 static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
2659 {
2660         int ret;
2661
2662         ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
2663         if (ret) {
2664                 dev_err(nandc->dev, "failed to set DMA mask\n");
2665                 return ret;
2666         }
2667
2668         /*
2669          * we use the internal buffer for reading ONFI params, reading small
2670          * data like ID and status, and preforming read-copy-write operations
2671          * when writing to a codeword partially. 532 is the maximum possible
2672          * size of a codeword for our nand controller
2673          */
2674         nandc->buf_size = 532;
2675
2676         nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
2677                                         GFP_KERNEL);
2678         if (!nandc->data_buffer)
2679                 return -ENOMEM;
2680
2681         nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
2682                                         GFP_KERNEL);
2683         if (!nandc->regs)
2684                 return -ENOMEM;
2685
2686         nandc->reg_read_buf = devm_kcalloc(nandc->dev,
2687                                 MAX_REG_RD, sizeof(*nandc->reg_read_buf),
2688                                 GFP_KERNEL);
2689         if (!nandc->reg_read_buf)
2690                 return -ENOMEM;
2691
2692         if (nandc->props->is_bam) {
2693                 nandc->reg_read_dma =
2694                         dma_map_single(nandc->dev, nandc->reg_read_buf,
2695                                        MAX_REG_RD *
2696                                        sizeof(*nandc->reg_read_buf),
2697                                        DMA_FROM_DEVICE);
2698                 if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
2699                         dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
2700                         return -EIO;
2701                 }
2702
2703                 nandc->tx_chan = dma_request_chan(nandc->dev, "tx");
2704                 if (IS_ERR(nandc->tx_chan)) {
2705                         ret = PTR_ERR(nandc->tx_chan);
2706                         nandc->tx_chan = NULL;
2707                         dev_err_probe(nandc->dev, ret,
2708                                       "tx DMA channel request failed\n");
2709                         goto unalloc;
2710                 }
2711
2712                 nandc->rx_chan = dma_request_chan(nandc->dev, "rx");
2713                 if (IS_ERR(nandc->rx_chan)) {
2714                         ret = PTR_ERR(nandc->rx_chan);
2715                         nandc->rx_chan = NULL;
2716                         dev_err_probe(nandc->dev, ret,
2717                                       "rx DMA channel request failed\n");
2718                         goto unalloc;
2719                 }
2720
2721                 nandc->cmd_chan = dma_request_chan(nandc->dev, "cmd");
2722                 if (IS_ERR(nandc->cmd_chan)) {
2723                         ret = PTR_ERR(nandc->cmd_chan);
2724                         nandc->cmd_chan = NULL;
2725                         dev_err_probe(nandc->dev, ret,
2726                                       "cmd DMA channel request failed\n");
2727                         goto unalloc;
2728                 }
2729
2730                 /*
2731                  * Initially allocate BAM transaction to read ONFI param page.
2732                  * After detecting all the devices, this BAM transaction will
2733                  * be freed and the next BAM tranasction will be allocated with
2734                  * maximum codeword size
2735                  */
2736                 nandc->max_cwperpage = 1;
2737                 nandc->bam_txn = alloc_bam_transaction(nandc);
2738                 if (!nandc->bam_txn) {
2739                         dev_err(nandc->dev,
2740                                 "failed to allocate bam transaction\n");
2741                         ret = -ENOMEM;
2742                         goto unalloc;
2743                 }
2744         } else {
2745                 nandc->chan = dma_request_chan(nandc->dev, "rxtx");
2746                 if (IS_ERR(nandc->chan)) {
2747                         ret = PTR_ERR(nandc->chan);
2748                         nandc->chan = NULL;
2749                         dev_err_probe(nandc->dev, ret,
2750                                       "rxtx DMA channel request failed\n");
2751                         return ret;
2752                 }
2753         }
2754
2755         INIT_LIST_HEAD(&nandc->desc_list);
2756         INIT_LIST_HEAD(&nandc->host_list);
2757
2758         nand_controller_init(&nandc->controller);
2759         nandc->controller.ops = &qcom_nandc_ops;
2760
2761         return 0;
2762 unalloc:
2763         qcom_nandc_unalloc(nandc);
2764         return ret;
2765 }
2766
2767 /* one time setup of a few nand controller registers */
2768 static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2769 {
2770         u32 nand_ctrl;
2771
2772         /* kill onenand */
2773         if (!nandc->props->is_qpic)
2774                 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
2775         nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD),
2776                     NAND_DEV_CMD_VLD_VAL);
2777
2778         /* enable ADM or BAM DMA */
2779         if (nandc->props->is_bam) {
2780                 nand_ctrl = nandc_read(nandc, NAND_CTRL);
2781
2782                 /*
2783                  *NAND_CTRL is an operational registers, and CPU
2784                  * access to operational registers are read only
2785                  * in BAM mode. So update the NAND_CTRL register
2786                  * only if it is not in BAM mode. In most cases BAM
2787                  * mode will be enabled in bootloader
2788                  */
2789                 if (!(nand_ctrl & BAM_MODE_EN))
2790                         nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
2791         } else {
2792                 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2793         }
2794
2795         /* save the original values of these registers */
2796         nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1));
2797         nandc->vld = NAND_DEV_CMD_VLD_VAL;
2798
2799         return 0;
2800 }
2801
2802 static int qcom_nand_host_init_and_register(struct qcom_nand_controller *nandc,
2803                                             struct qcom_nand_host *host,
2804                                             struct device_node *dn)
2805 {
2806         struct nand_chip *chip = &host->chip;
2807         struct mtd_info *mtd = nand_to_mtd(chip);
2808         struct device *dev = nandc->dev;
2809         int ret;
2810
2811         ret = of_property_read_u32(dn, "reg", &host->cs);
2812         if (ret) {
2813                 dev_err(dev, "can't get chip-select\n");
2814                 return -ENXIO;
2815         }
2816
2817         nand_set_flash_node(chip, dn);
2818         mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
2819         if (!mtd->name)
2820                 return -ENOMEM;
2821
2822         mtd->owner = THIS_MODULE;
2823         mtd->dev.parent = dev;
2824
2825         chip->legacy.cmdfunc    = qcom_nandc_command;
2826         chip->legacy.select_chip        = qcom_nandc_select_chip;
2827         chip->legacy.read_byte  = qcom_nandc_read_byte;
2828         chip->legacy.read_buf   = qcom_nandc_read_buf;
2829         chip->legacy.write_buf  = qcom_nandc_write_buf;
2830         chip->legacy.set_features       = nand_get_set_features_notsupp;
2831         chip->legacy.get_features       = nand_get_set_features_notsupp;
2832
2833         /*
2834          * the bad block marker is readable only when we read the last codeword
2835          * of a page with ECC disabled. currently, the nand_base and nand_bbt
2836          * helpers don't allow us to read BB from a nand chip with ECC
2837          * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2838          * and block_markbad helpers until we permanently switch to using
2839          * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2840          */
2841         chip->legacy.block_bad          = qcom_nandc_block_bad;
2842         chip->legacy.block_markbad      = qcom_nandc_block_markbad;
2843
2844         chip->controller = &nandc->controller;
2845         chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USES_DMA |
2846                          NAND_SKIP_BBTSCAN;
2847
2848         /* set up initial status value */
2849         host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2850
2851         ret = nand_scan(chip, 1);
2852         if (ret)
2853                 return ret;
2854
2855         if (nandc->props->is_bam) {
2856                 free_bam_transaction(nandc);
2857                 nandc->bam_txn = alloc_bam_transaction(nandc);
2858                 if (!nandc->bam_txn) {
2859                         dev_err(nandc->dev,
2860                                 "failed to allocate bam transaction\n");
2861                         return -ENOMEM;
2862                 }
2863         }
2864
2865         ret = mtd_device_register(mtd, NULL, 0);
2866         if (ret)
2867                 nand_cleanup(chip);
2868
2869         return ret;
2870 }
2871
2872 static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2873 {
2874         struct device *dev = nandc->dev;
2875         struct device_node *dn = dev->of_node, *child;
2876         struct qcom_nand_host *host;
2877         int ret;
2878
2879         for_each_available_child_of_node(dn, child) {
2880                 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2881                 if (!host) {
2882                         of_node_put(child);
2883                         return -ENOMEM;
2884                 }
2885
2886                 ret = qcom_nand_host_init_and_register(nandc, host, child);
2887                 if (ret) {
2888                         devm_kfree(dev, host);
2889                         continue;
2890                 }
2891
2892                 list_add_tail(&host->node, &nandc->host_list);
2893         }
2894
2895         if (list_empty(&nandc->host_list))
2896                 return -ENODEV;
2897
2898         return 0;
2899 }
2900
2901 /* parse custom DT properties here */
2902 static int qcom_nandc_parse_dt(struct platform_device *pdev)
2903 {
2904         struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2905         struct device_node *np = nandc->dev->of_node;
2906         int ret;
2907
2908         if (!nandc->props->is_bam) {
2909                 ret = of_property_read_u32(np, "qcom,cmd-crci",
2910                                            &nandc->cmd_crci);
2911                 if (ret) {
2912                         dev_err(nandc->dev, "command CRCI unspecified\n");
2913                         return ret;
2914                 }
2915
2916                 ret = of_property_read_u32(np, "qcom,data-crci",
2917                                            &nandc->data_crci);
2918                 if (ret) {
2919                         dev_err(nandc->dev, "data CRCI unspecified\n");
2920                         return ret;
2921                 }
2922         }
2923
2924         return 0;
2925 }
2926
2927 static int qcom_nandc_probe(struct platform_device *pdev)
2928 {
2929         struct qcom_nand_controller *nandc;
2930         const void *dev_data;
2931         struct device *dev = &pdev->dev;
2932         struct resource *res;
2933         int ret;
2934
2935         nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2936         if (!nandc)
2937                 return -ENOMEM;
2938
2939         platform_set_drvdata(pdev, nandc);
2940         nandc->dev = dev;
2941
2942         dev_data = of_device_get_match_data(dev);
2943         if (!dev_data) {
2944                 dev_err(&pdev->dev, "failed to get device data\n");
2945                 return -ENODEV;
2946         }
2947
2948         nandc->props = dev_data;
2949
2950         nandc->core_clk = devm_clk_get(dev, "core");
2951         if (IS_ERR(nandc->core_clk))
2952                 return PTR_ERR(nandc->core_clk);
2953
2954         nandc->aon_clk = devm_clk_get(dev, "aon");
2955         if (IS_ERR(nandc->aon_clk))
2956                 return PTR_ERR(nandc->aon_clk);
2957
2958         ret = qcom_nandc_parse_dt(pdev);
2959         if (ret)
2960                 return ret;
2961
2962         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2963         nandc->base = devm_ioremap_resource(dev, res);
2964         if (IS_ERR(nandc->base))
2965                 return PTR_ERR(nandc->base);
2966
2967         nandc->base_phys = res->start;
2968         nandc->base_dma = dma_map_resource(dev, res->start,
2969                                            resource_size(res),
2970                                            DMA_BIDIRECTIONAL, 0);
2971         if (!nandc->base_dma)
2972                 return -ENXIO;
2973
2974         ret = qcom_nandc_alloc(nandc);
2975         if (ret)
2976                 goto err_nandc_alloc;
2977
2978         ret = clk_prepare_enable(nandc->core_clk);
2979         if (ret)
2980                 goto err_core_clk;
2981
2982         ret = clk_prepare_enable(nandc->aon_clk);
2983         if (ret)
2984                 goto err_aon_clk;
2985
2986         ret = qcom_nandc_setup(nandc);
2987         if (ret)
2988                 goto err_setup;
2989
2990         ret = qcom_probe_nand_devices(nandc);
2991         if (ret)
2992                 goto err_setup;
2993
2994         return 0;
2995
2996 err_setup:
2997         clk_disable_unprepare(nandc->aon_clk);
2998 err_aon_clk:
2999         clk_disable_unprepare(nandc->core_clk);
3000 err_core_clk:
3001         qcom_nandc_unalloc(nandc);
3002 err_nandc_alloc:
3003         dma_unmap_resource(dev, res->start, resource_size(res),
3004                            DMA_BIDIRECTIONAL, 0);
3005
3006         return ret;
3007 }
3008
3009 static int qcom_nandc_remove(struct platform_device *pdev)
3010 {
3011         struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
3012         struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3013         struct qcom_nand_host *host;
3014         struct nand_chip *chip;
3015         int ret;
3016
3017         list_for_each_entry(host, &nandc->host_list, node) {
3018                 chip = &host->chip;
3019                 ret = mtd_device_unregister(nand_to_mtd(chip));
3020                 WARN_ON(ret);
3021                 nand_cleanup(chip);
3022         }
3023
3024         qcom_nandc_unalloc(nandc);
3025
3026         clk_disable_unprepare(nandc->aon_clk);
3027         clk_disable_unprepare(nandc->core_clk);
3028
3029         dma_unmap_resource(&pdev->dev, nandc->base_dma, resource_size(res),
3030                            DMA_BIDIRECTIONAL, 0);
3031
3032         return 0;
3033 }
3034
3035 static const struct qcom_nandc_props ipq806x_nandc_props = {
3036         .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
3037         .is_bam = false,
3038         .dev_cmd_reg_start = 0x0,
3039 };
3040
3041 static const struct qcom_nandc_props ipq4019_nandc_props = {
3042         .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3043         .is_bam = true,
3044         .is_qpic = true,
3045         .dev_cmd_reg_start = 0x0,
3046 };
3047
3048 static const struct qcom_nandc_props ipq8074_nandc_props = {
3049         .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3050         .is_bam = true,
3051         .is_qpic = true,
3052         .dev_cmd_reg_start = 0x7000,
3053 };
3054
3055 /*
3056  * data will hold a struct pointer containing more differences once we support
3057  * more controller variants
3058  */
3059 static const struct of_device_id qcom_nandc_of_match[] = {
3060         {
3061                 .compatible = "qcom,ipq806x-nand",
3062                 .data = &ipq806x_nandc_props,
3063         },
3064         {
3065                 .compatible = "qcom,ipq4019-nand",
3066                 .data = &ipq4019_nandc_props,
3067         },
3068         {
3069                 .compatible = "qcom,ipq8074-nand",
3070                 .data = &ipq8074_nandc_props,
3071         },
3072         {}
3073 };
3074 MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
3075
3076 static struct platform_driver qcom_nandc_driver = {
3077         .driver = {
3078                 .name = "qcom-nandc",
3079                 .of_match_table = qcom_nandc_of_match,
3080         },
3081         .probe   = qcom_nandc_probe,
3082         .remove  = qcom_nandc_remove,
3083 };
3084 module_platform_driver(qcom_nandc_driver);
3085
3086 MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
3087 MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
3088 MODULE_LICENSE("GPL v2");