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