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