blob: fd77d592c4ee21894e9a29b5810108bdb19fc695 [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
Abhishek Sahu91af95c2017-08-17 17:37:43 +053056#define NAND_READ_LOCATION_2 0xf28
57#define NAND_READ_LOCATION_3 0xf2c
Archit Tanejac76b78d2016-02-03 14:29:50 +053058
59/* dummy register offsets, used by write_reg_dma */
60#define NAND_DEV_CMD1_RESTORE 0xdead
61#define NAND_DEV_CMD_VLD_RESTORE 0xbeef
62
63/* NAND_FLASH_CMD bits */
64#define PAGE_ACC BIT(4)
65#define LAST_PAGE BIT(5)
66
67/* NAND_FLASH_CHIP_SELECT bits */
68#define NAND_DEV_SEL 0
69#define DM_EN BIT(2)
70
71/* NAND_FLASH_STATUS bits */
72#define FS_OP_ERR BIT(4)
73#define FS_READY_BSY_N BIT(5)
74#define FS_MPU_ERR BIT(8)
75#define FS_DEVICE_STS_ERR BIT(16)
76#define FS_DEVICE_WP BIT(23)
77
78/* NAND_BUFFER_STATUS bits */
79#define BS_UNCORRECTABLE_BIT BIT(8)
80#define BS_CORRECTABLE_ERR_MSK 0x1f
81
82/* NAND_DEVn_CFG0 bits */
83#define DISABLE_STATUS_AFTER_WRITE 4
84#define CW_PER_PAGE 6
85#define UD_SIZE_BYTES 9
86#define ECC_PARITY_SIZE_BYTES_RS 19
87#define SPARE_SIZE_BYTES 23
88#define NUM_ADDR_CYCLES 27
89#define STATUS_BFR_READ 30
90#define SET_RD_MODE_AFTER_STATUS 31
91
92/* NAND_DEVn_CFG0 bits */
93#define DEV0_CFG1_ECC_DISABLE 0
94#define WIDE_FLASH 1
95#define NAND_RECOVERY_CYCLES 2
96#define CS_ACTIVE_BSY 5
97#define BAD_BLOCK_BYTE_NUM 6
98#define BAD_BLOCK_IN_SPARE_AREA 16
99#define WR_RD_BSY_GAP 17
100#define ENABLE_BCH_ECC 27
101
102/* NAND_DEV0_ECC_CFG bits */
103#define ECC_CFG_ECC_DISABLE 0
104#define ECC_SW_RESET 1
105#define ECC_MODE 4
106#define ECC_PARITY_SIZE_BYTES_BCH 8
107#define ECC_NUM_DATA_BYTES 16
108#define ECC_FORCE_CLK_OPEN 30
109
110/* NAND_DEV_CMD1 bits */
111#define READ_ADDR 0
112
113/* NAND_DEV_CMD_VLD bits */
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530114#define READ_START_VLD BIT(0)
115#define READ_STOP_VLD BIT(1)
116#define WRITE_START_VLD BIT(2)
117#define ERASE_START_VLD BIT(3)
118#define SEQ_READ_START_VLD BIT(4)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530119
120/* NAND_EBI2_ECC_BUF_CFG bits */
121#define NUM_STEPS 0
122
123/* NAND_ERASED_CW_DETECT_CFG bits */
124#define ERASED_CW_ECC_MASK 1
125#define AUTO_DETECT_RES 0
126#define MASK_ECC (1 << ERASED_CW_ECC_MASK)
127#define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
128#define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
129#define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
130#define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
131
132/* NAND_ERASED_CW_DETECT_STATUS bits */
133#define PAGE_ALL_ERASED BIT(7)
134#define CODEWORD_ALL_ERASED BIT(6)
135#define PAGE_ERASED BIT(5)
136#define CODEWORD_ERASED BIT(4)
137#define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
138#define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
139
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530140/* NAND_READ_LOCATION_n bits */
141#define READ_LOCATION_OFFSET 0
142#define READ_LOCATION_SIZE 16
143#define READ_LOCATION_LAST 31
144
Archit Tanejac76b78d2016-02-03 14:29:50 +0530145/* Version Mask */
146#define NAND_VERSION_MAJOR_MASK 0xf0000000
147#define NAND_VERSION_MAJOR_SHIFT 28
148#define NAND_VERSION_MINOR_MASK 0x0fff0000
149#define NAND_VERSION_MINOR_SHIFT 16
150
151/* NAND OP_CMDs */
152#define PAGE_READ 0x2
153#define PAGE_READ_WITH_ECC 0x3
154#define PAGE_READ_WITH_ECC_SPARE 0x4
155#define PROGRAM_PAGE 0x6
156#define PAGE_PROGRAM_WITH_ECC 0x7
157#define PROGRAM_PAGE_SPARE 0x9
158#define BLOCK_ERASE 0xa
159#define FETCH_ID 0xb
160#define RESET_DEVICE 0xd
161
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530162/* Default Value for NAND_DEV_CMD_VLD */
163#define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \
164 ERASE_START_VLD | SEQ_READ_START_VLD)
165
Abhishek Sahu9d43f912017-08-17 17:37:45 +0530166/* NAND_CTRL bits */
167#define BAM_MODE_EN BIT(0)
168
Archit Tanejac76b78d2016-02-03 14:29:50 +0530169/*
170 * the NAND controller performs reads/writes with ECC in 516 byte chunks.
171 * the driver calls the chunks 'step' or 'codeword' interchangeably
172 */
173#define NANDC_STEP_SIZE 512
174
175/*
176 * the largest page size we support is 8K, this will have 16 steps/codewords
177 * of 512 bytes each
178 */
179#define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
180
181/* we read at most 3 registers per codeword scan */
182#define MAX_REG_RD (3 * MAX_NUM_STEPS)
183
184/* ECC modes supported by the controller */
185#define ECC_NONE BIT(0)
186#define ECC_RS_4BIT BIT(1)
187#define ECC_BCH_4BIT BIT(2)
188#define ECC_BCH_8BIT BIT(3)
189
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530190#define nandc_set_read_loc(nandc, reg, offset, size, is_last) \
191nandc_set_reg(nandc, NAND_READ_LOCATION_##reg, \
192 ((offset) << READ_LOCATION_OFFSET) | \
193 ((size) << READ_LOCATION_SIZE) | \
194 ((is_last) << READ_LOCATION_LAST))
195
Abhishek Sahucb80f112017-08-17 17:37:40 +0530196#define QPIC_PER_CW_CMD_SGL 32
197#define QPIC_PER_CW_DATA_SGL 8
198
199/*
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530200 * Flags used in DMA descriptor preparation helper functions
201 * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
202 */
203/* Don't set the EOT in current tx BAM sgl */
204#define NAND_BAM_NO_EOT BIT(0)
205/* Set the NWD flag in current BAM sgl */
206#define NAND_BAM_NWD BIT(1)
207/* Finish writing in the current BAM sgl and start writing in another BAM sgl */
208#define NAND_BAM_NEXT_SGL BIT(2)
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530209/*
210 * Erased codeword status is being used two times in single transfer so this
211 * flag will determine the current value of erased codeword status register
212 */
213#define NAND_ERASED_CW_SET BIT(4)
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530214
215/*
Abhishek Sahucb80f112017-08-17 17:37:40 +0530216 * This data type corresponds to the BAM transaction which will be used for all
217 * NAND transfers.
218 * @cmd_sgl - sgl for NAND BAM command pipe
219 * @data_sgl - sgl for NAND BAM consumer/producer pipe
220 * @cmd_sgl_pos - current index in command sgl.
221 * @cmd_sgl_start - start index in command sgl.
222 * @tx_sgl_pos - current index in data sgl for tx.
223 * @tx_sgl_start - start index in data sgl for tx.
224 * @rx_sgl_pos - current index in data sgl for rx.
225 * @rx_sgl_start - start index in data sgl for rx.
226 */
227struct bam_transaction {
228 struct scatterlist *cmd_sgl;
229 struct scatterlist *data_sgl;
230 u32 cmd_sgl_pos;
231 u32 cmd_sgl_start;
232 u32 tx_sgl_pos;
233 u32 tx_sgl_start;
234 u32 rx_sgl_pos;
235 u32 rx_sgl_start;
236};
237
Abhishek Sahu381dd242017-08-17 17:37:41 +0530238/*
239 * This data type corresponds to the nand dma descriptor
240 * @list - list for desc_info
241 * @dir - DMA transfer direction
242 * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
243 * ADM
244 * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
245 * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
246 * @dma_desc - low level DMA engine descriptor
247 */
Archit Tanejac76b78d2016-02-03 14:29:50 +0530248struct desc_info {
249 struct list_head node;
250
251 enum dma_data_direction dir;
Abhishek Sahu381dd242017-08-17 17:37:41 +0530252 union {
253 struct scatterlist adm_sgl;
254 struct {
255 struct scatterlist *bam_sgl;
256 int sgl_cnt;
257 };
258 };
Archit Tanejac76b78d2016-02-03 14:29:50 +0530259 struct dma_async_tx_descriptor *dma_desc;
260};
261
262/*
263 * holds the current register values that we want to write. acts as a contiguous
264 * chunk of memory which we use to write the controller registers through DMA.
265 */
266struct nandc_regs {
267 __le32 cmd;
268 __le32 addr0;
269 __le32 addr1;
270 __le32 chip_sel;
271 __le32 exec;
272
273 __le32 cfg0;
274 __le32 cfg1;
275 __le32 ecc_bch_cfg;
276
277 __le32 clrflashstatus;
278 __le32 clrreadstatus;
279
280 __le32 cmd1;
281 __le32 vld;
282
283 __le32 orig_cmd1;
284 __le32 orig_vld;
285
286 __le32 ecc_buf_cfg;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530287 __le32 read_location0;
288 __le32 read_location1;
289 __le32 read_location2;
290 __le32 read_location3;
291
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530292 __le32 erased_cw_detect_cfg_clr;
293 __le32 erased_cw_detect_cfg_set;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530294};
295
296/*
297 * NAND controller data struct
298 *
299 * @controller: base controller structure
300 * @host_list: list containing all the chips attached to the
301 * controller
302 * @dev: parent device
303 * @base: MMIO base
304 * @base_dma: physical base address of controller registers
305 * @core_clk: controller clock
306 * @aon_clk: another controller clock
307 *
308 * @chan: dma channel
309 * @cmd_crci: ADM DMA CRCI for command flow control
310 * @data_crci: ADM DMA CRCI for data flow control
311 * @desc_list: DMA descriptor list (list of desc_infos)
312 *
313 * @data_buffer: our local DMA buffer for page read/writes,
314 * used when we can't use the buffer provided
315 * by upper layers directly
316 * @buf_size/count/start: markers for chip->read_buf/write_buf functions
317 * @reg_read_buf: local buffer for reading back registers via DMA
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530318 * @reg_read_dma: contains dma address for register read buffer
Archit Tanejac76b78d2016-02-03 14:29:50 +0530319 * @reg_read_pos: marker for data read in reg_read_buf
320 *
321 * @regs: a contiguous chunk of memory for DMA register
322 * writes. contains the register values to be
323 * written to controller
324 * @cmd1/vld: some fixed controller register values
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530325 * @props: properties of current NAND controller,
Archit Tanejac76b78d2016-02-03 14:29:50 +0530326 * initialized via DT match data
Abhishek Sahucb80f112017-08-17 17:37:40 +0530327 * @max_cwperpage: maximum QPIC codewords required. calculated
328 * from all connected NAND devices pagesize
Archit Tanejac76b78d2016-02-03 14:29:50 +0530329 */
330struct qcom_nand_controller {
331 struct nand_hw_control controller;
332 struct list_head host_list;
333
334 struct device *dev;
335
336 void __iomem *base;
337 dma_addr_t base_dma;
338
339 struct clk *core_clk;
340 struct clk *aon_clk;
341
Abhishek Sahu497d7d82017-08-11 17:09:19 +0530342 union {
343 /* will be used only by QPIC for BAM DMA */
344 struct {
345 struct dma_chan *tx_chan;
346 struct dma_chan *rx_chan;
347 struct dma_chan *cmd_chan;
348 };
349
350 /* will be used only by EBI2 for ADM DMA */
351 struct {
352 struct dma_chan *chan;
353 unsigned int cmd_crci;
354 unsigned int data_crci;
355 };
356 };
357
Archit Tanejac76b78d2016-02-03 14:29:50 +0530358 struct list_head desc_list;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530359 struct bam_transaction *bam_txn;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530360
361 u8 *data_buffer;
362 int buf_size;
363 int buf_count;
364 int buf_start;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530365 unsigned int max_cwperpage;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530366
367 __le32 *reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530368 dma_addr_t reg_read_dma;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530369 int reg_read_pos;
370
371 struct nandc_regs *regs;
372
373 u32 cmd1, vld;
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530374 const struct qcom_nandc_props *props;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530375};
376
377/*
378 * NAND chip structure
379 *
380 * @chip: base NAND chip structure
381 * @node: list node to add itself to host_list in
382 * qcom_nand_controller
383 *
384 * @cs: chip select value for this chip
385 * @cw_size: the number of bytes in a single step/codeword
386 * of a page, consisting of all data, ecc, spare
387 * and reserved bytes
388 * @cw_data: the number of bytes within a codeword protected
389 * by ECC
390 * @use_ecc: request the controller to use ECC for the
391 * upcoming read/write
392 * @bch_enabled: flag to tell whether BCH ECC mode is used
393 * @ecc_bytes_hw: ECC bytes used by controller hardware for this
394 * chip
395 * @status: value to be returned if NAND_CMD_STATUS command
396 * is executed
397 * @last_command: keeps track of last command on this chip. used
398 * for reading correct status
399 *
400 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
401 * ecc/non-ecc mode for the current nand flash
402 * device
403 */
404struct qcom_nand_host {
405 struct nand_chip chip;
406 struct list_head node;
407
408 int cs;
409 int cw_size;
410 int cw_data;
411 bool use_ecc;
412 bool bch_enabled;
413 int ecc_bytes_hw;
414 int spare_bytes;
415 int bbm_size;
416 u8 status;
417 int last_command;
418
419 u32 cfg0, cfg1;
420 u32 cfg0_raw, cfg1_raw;
421 u32 ecc_buf_cfg;
422 u32 ecc_bch_cfg;
423 u32 clrflashstatus;
424 u32 clrreadstatus;
425};
426
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530427/*
428 * This data type corresponds to the NAND controller properties which varies
429 * among different NAND controllers.
430 * @ecc_modes - ecc mode for NAND
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +0530431 * @is_bam - whether NAND controller is using BAM
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530432 */
433struct qcom_nandc_props {
434 u32 ecc_modes;
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +0530435 bool is_bam;
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530436};
437
Abhishek Sahucb80f112017-08-17 17:37:40 +0530438/* Frees the BAM transaction memory */
439static void free_bam_transaction(struct qcom_nand_controller *nandc)
440{
441 struct bam_transaction *bam_txn = nandc->bam_txn;
442
443 devm_kfree(nandc->dev, bam_txn);
444}
445
446/* Allocates and Initializes the BAM transaction */
447static struct bam_transaction *
448alloc_bam_transaction(struct qcom_nand_controller *nandc)
449{
450 struct bam_transaction *bam_txn;
451 size_t bam_txn_size;
452 unsigned int num_cw = nandc->max_cwperpage;
453 void *bam_txn_buf;
454
455 bam_txn_size =
456 sizeof(*bam_txn) + num_cw *
457 ((sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) +
458 (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL));
459
460 bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL);
461 if (!bam_txn_buf)
462 return NULL;
463
464 bam_txn = bam_txn_buf;
465 bam_txn_buf += sizeof(*bam_txn);
466
467 bam_txn->cmd_sgl = bam_txn_buf;
468 bam_txn_buf +=
469 sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw;
470
471 bam_txn->data_sgl = bam_txn_buf;
472
473 return bam_txn;
474}
475
Archit Tanejac76b78d2016-02-03 14:29:50 +0530476static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
477{
478 return container_of(chip, struct qcom_nand_host, chip);
479}
480
481static inline struct qcom_nand_controller *
482get_qcom_nand_controller(struct nand_chip *chip)
483{
484 return container_of(chip->controller, struct qcom_nand_controller,
485 controller);
486}
487
488static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
489{
490 return ioread32(nandc->base + offset);
491}
492
493static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
494 u32 val)
495{
496 iowrite32(val, nandc->base + offset);
497}
498
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530499static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
500 bool is_cpu)
501{
502 if (!nandc->props->is_bam)
503 return;
504
505 if (is_cpu)
506 dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
507 MAX_REG_RD *
508 sizeof(*nandc->reg_read_buf),
509 DMA_FROM_DEVICE);
510 else
511 dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
512 MAX_REG_RD *
513 sizeof(*nandc->reg_read_buf),
514 DMA_FROM_DEVICE);
515}
516
Archit Tanejac76b78d2016-02-03 14:29:50 +0530517static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
518{
519 switch (offset) {
520 case NAND_FLASH_CMD:
521 return &regs->cmd;
522 case NAND_ADDR0:
523 return &regs->addr0;
524 case NAND_ADDR1:
525 return &regs->addr1;
526 case NAND_FLASH_CHIP_SELECT:
527 return &regs->chip_sel;
528 case NAND_EXEC_CMD:
529 return &regs->exec;
530 case NAND_FLASH_STATUS:
531 return &regs->clrflashstatus;
532 case NAND_DEV0_CFG0:
533 return &regs->cfg0;
534 case NAND_DEV0_CFG1:
535 return &regs->cfg1;
536 case NAND_DEV0_ECC_CFG:
537 return &regs->ecc_bch_cfg;
538 case NAND_READ_STATUS:
539 return &regs->clrreadstatus;
540 case NAND_DEV_CMD1:
541 return &regs->cmd1;
542 case NAND_DEV_CMD1_RESTORE:
543 return &regs->orig_cmd1;
544 case NAND_DEV_CMD_VLD:
545 return &regs->vld;
546 case NAND_DEV_CMD_VLD_RESTORE:
547 return &regs->orig_vld;
548 case NAND_EBI2_ECC_BUF_CFG:
549 return &regs->ecc_buf_cfg;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530550 case NAND_READ_LOCATION_0:
551 return &regs->read_location0;
552 case NAND_READ_LOCATION_1:
553 return &regs->read_location1;
554 case NAND_READ_LOCATION_2:
555 return &regs->read_location2;
556 case NAND_READ_LOCATION_3:
557 return &regs->read_location3;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530558 default:
559 return NULL;
560 }
561}
562
563static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
564 u32 val)
565{
566 struct nandc_regs *regs = nandc->regs;
567 __le32 *reg;
568
569 reg = offset_to_nandc_reg(regs, offset);
570
571 if (reg)
572 *reg = cpu_to_le32(val);
573}
574
575/* helper to configure address register values */
576static void set_address(struct qcom_nand_host *host, u16 column, int page)
577{
578 struct nand_chip *chip = &host->chip;
579 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
580
581 if (chip->options & NAND_BUSWIDTH_16)
582 column >>= 1;
583
584 nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
585 nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
586}
587
588/*
589 * update_rw_regs: set up read/write register values, these will be
590 * written to the NAND controller registers via DMA
591 *
592 * @num_cw: number of steps for the read/write operation
593 * @read: read or write operation
594 */
595static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
596{
597 struct nand_chip *chip = &host->chip;
598 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
599 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
600
601 if (read) {
602 if (host->use_ecc)
603 cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
604 else
605 cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
606 } else {
607 cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
608 }
609
610 if (host->use_ecc) {
611 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
612 (num_cw - 1) << CW_PER_PAGE;
613
614 cfg1 = host->cfg1;
615 ecc_bch_cfg = host->ecc_bch_cfg;
616 } else {
617 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
618 (num_cw - 1) << CW_PER_PAGE;
619
620 cfg1 = host->cfg1_raw;
621 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
622 }
623
624 nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
625 nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
626 nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
627 nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
628 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
629 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
630 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
631 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530632
633 if (read)
634 nandc_set_read_loc(nandc, 0, 0, host->use_ecc ?
635 host->cw_data : host->cw_size, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530636}
637
Abhishek Sahu381dd242017-08-17 17:37:41 +0530638/*
639 * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
640 * for BAM. This descriptor will be added in the NAND DMA descriptor queue
641 * which will be submitted to DMA engine.
642 */
643static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
644 struct dma_chan *chan,
645 unsigned long flags)
646{
647 struct desc_info *desc;
648 struct scatterlist *sgl;
649 unsigned int sgl_cnt;
650 int ret;
651 struct bam_transaction *bam_txn = nandc->bam_txn;
652 enum dma_transfer_direction dir_eng;
653 struct dma_async_tx_descriptor *dma_desc;
654
655 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
656 if (!desc)
657 return -ENOMEM;
658
659 if (chan == nandc->cmd_chan) {
660 sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
661 sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
662 bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
663 dir_eng = DMA_MEM_TO_DEV;
664 desc->dir = DMA_TO_DEVICE;
665 } else if (chan == nandc->tx_chan) {
666 sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
667 sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
668 bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
669 dir_eng = DMA_MEM_TO_DEV;
670 desc->dir = DMA_TO_DEVICE;
671 } else {
672 sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
673 sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
674 bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
675 dir_eng = DMA_DEV_TO_MEM;
676 desc->dir = DMA_FROM_DEVICE;
677 }
678
679 sg_mark_end(sgl + sgl_cnt - 1);
680 ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
681 if (ret == 0) {
682 dev_err(nandc->dev, "failure in mapping desc\n");
683 kfree(desc);
684 return -ENOMEM;
685 }
686
687 desc->sgl_cnt = sgl_cnt;
688 desc->bam_sgl = sgl;
689
690 dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
691 flags);
692
693 if (!dma_desc) {
694 dev_err(nandc->dev, "failure in prep desc\n");
695 dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
696 kfree(desc);
697 return -EINVAL;
698 }
699
700 desc->dma_desc = dma_desc;
701
702 list_add_tail(&desc->node, &nandc->desc_list);
703
704 return 0;
705}
706
707static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
708 int reg_off, const void *vaddr, int size,
709 bool flow_control)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530710{
711 struct desc_info *desc;
712 struct dma_async_tx_descriptor *dma_desc;
713 struct scatterlist *sgl;
714 struct dma_slave_config slave_conf;
715 enum dma_transfer_direction dir_eng;
716 int ret;
717
718 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
719 if (!desc)
720 return -ENOMEM;
721
Abhishek Sahu381dd242017-08-17 17:37:41 +0530722 sgl = &desc->adm_sgl;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530723
724 sg_init_one(sgl, vaddr, size);
725
726 if (read) {
727 dir_eng = DMA_DEV_TO_MEM;
728 desc->dir = DMA_FROM_DEVICE;
729 } else {
730 dir_eng = DMA_MEM_TO_DEV;
731 desc->dir = DMA_TO_DEVICE;
732 }
733
734 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
735 if (ret == 0) {
736 ret = -ENOMEM;
737 goto err;
738 }
739
740 memset(&slave_conf, 0x00, sizeof(slave_conf));
741
742 slave_conf.device_fc = flow_control;
743 if (read) {
744 slave_conf.src_maxburst = 16;
745 slave_conf.src_addr = nandc->base_dma + reg_off;
746 slave_conf.slave_id = nandc->data_crci;
747 } else {
748 slave_conf.dst_maxburst = 16;
749 slave_conf.dst_addr = nandc->base_dma + reg_off;
750 slave_conf.slave_id = nandc->cmd_crci;
751 }
752
753 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
754 if (ret) {
755 dev_err(nandc->dev, "failed to configure dma channel\n");
756 goto err;
757 }
758
759 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
760 if (!dma_desc) {
761 dev_err(nandc->dev, "failed to prepare desc\n");
762 ret = -EINVAL;
763 goto err;
764 }
765
766 desc->dma_desc = dma_desc;
767
768 list_add_tail(&desc->node, &nandc->desc_list);
769
770 return 0;
771err:
772 kfree(desc);
773
774 return ret;
775}
776
777/*
778 * read_reg_dma: prepares a descriptor to read a given number of
779 * contiguous registers to the reg_read_buf pointer
780 *
781 * @first: offset of the first register in the contiguous block
782 * @num_regs: number of registers to read
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530783 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530784 */
785static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530786 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530787{
788 bool flow_control = false;
789 void *vaddr;
790 int size;
791
792 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
793 flow_control = true;
794
795 size = num_regs * sizeof(u32);
796 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
797 nandc->reg_read_pos += num_regs;
798
Abhishek Sahu381dd242017-08-17 17:37:41 +0530799 return prep_adm_dma_desc(nandc, true, first, vaddr, size, flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530800}
801
802/*
803 * write_reg_dma: prepares a descriptor to write a given number of
804 * contiguous registers
805 *
806 * @first: offset of the first register in the contiguous block
807 * @num_regs: number of registers to write
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530808 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530809 */
810static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530811 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530812{
813 bool flow_control = false;
814 struct nandc_regs *regs = nandc->regs;
815 void *vaddr;
816 int size;
817
818 vaddr = offset_to_nandc_reg(regs, first);
819
820 if (first == NAND_FLASH_CMD)
821 flow_control = true;
822
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530823 if (first == NAND_ERASED_CW_DETECT_CFG) {
824 if (flags & NAND_ERASED_CW_SET)
825 vaddr = &regs->erased_cw_detect_cfg_set;
826 else
827 vaddr = &regs->erased_cw_detect_cfg_clr;
828 }
829
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530830 if (first == NAND_EXEC_CMD)
831 flags |= NAND_BAM_NWD;
832
Archit Tanejac76b78d2016-02-03 14:29:50 +0530833 if (first == NAND_DEV_CMD1_RESTORE)
834 first = NAND_DEV_CMD1;
835
836 if (first == NAND_DEV_CMD_VLD_RESTORE)
837 first = NAND_DEV_CMD_VLD;
838
839 size = num_regs * sizeof(u32);
840
Abhishek Sahu381dd242017-08-17 17:37:41 +0530841 return prep_adm_dma_desc(nandc, false, first, vaddr, size,
842 flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530843}
844
845/*
846 * read_data_dma: prepares a DMA descriptor to transfer data from the
847 * controller's internal buffer to the buffer 'vaddr'
848 *
849 * @reg_off: offset within the controller's data buffer
850 * @vaddr: virtual address of the buffer we want to write to
851 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530852 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530853 */
854static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530855 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530856{
Abhishek Sahu381dd242017-08-17 17:37:41 +0530857 return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530858}
859
860/*
861 * write_data_dma: prepares a DMA descriptor to transfer data from
862 * 'vaddr' to the controller's internal buffer
863 *
864 * @reg_off: offset within the controller's data buffer
865 * @vaddr: virtual address of the buffer we want to read from
866 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530867 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530868 */
869static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530870 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530871{
Abhishek Sahu381dd242017-08-17 17:37:41 +0530872 return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530873}
874
875/*
Abhishek Sahubde43302017-07-19 17:17:55 +0530876 * Helper to prepare DMA descriptors for configuring registers
877 * before reading a NAND page.
Archit Tanejac76b78d2016-02-03 14:29:50 +0530878 */
Abhishek Sahubde43302017-07-19 17:17:55 +0530879static void config_nand_page_read(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530880{
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530881 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
882 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
883 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530884 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
885 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
886 NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
Abhishek Sahubde43302017-07-19 17:17:55 +0530887}
Archit Tanejac76b78d2016-02-03 14:29:50 +0530888
Abhishek Sahubde43302017-07-19 17:17:55 +0530889/*
890 * Helper to prepare DMA descriptors for configuring registers
891 * before reading each codeword in NAND page.
892 */
893static void config_nand_cw_read(struct qcom_nand_controller *nandc)
894{
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530895 if (nandc->props->is_bam)
896 write_reg_dma(nandc, NAND_READ_LOCATION_0, 4,
897 NAND_BAM_NEXT_SGL);
898
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530899 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
900 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530901
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530902 read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
903 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
904 NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530905}
906
907/*
Abhishek Sahubde43302017-07-19 17:17:55 +0530908 * Helper to prepare dma descriptors to configure registers needed for reading a
909 * single codeword in page
Archit Tanejac76b78d2016-02-03 14:29:50 +0530910 */
Abhishek Sahubde43302017-07-19 17:17:55 +0530911static void config_nand_single_cw_page_read(struct qcom_nand_controller *nandc)
912{
913 config_nand_page_read(nandc);
914 config_nand_cw_read(nandc);
915}
916
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530917/*
918 * Helper to prepare DMA descriptors used to configure registers needed for
919 * before writing a NAND page.
920 */
921static void config_nand_page_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530922{
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530923 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
924 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
925 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
926 NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530927}
928
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530929/*
930 * Helper to prepare DMA descriptors for configuring registers
931 * before writing each codeword in NAND page.
932 */
933static void config_nand_cw_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530934{
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530935 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
936 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530937
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530938 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530939
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530940 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
941 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530942}
943
944/*
945 * the following functions are used within chip->cmdfunc() to perform different
946 * NAND_CMD_* commands
947 */
948
949/* sets up descriptors for NAND_CMD_PARAM */
950static int nandc_param(struct qcom_nand_host *host)
951{
952 struct nand_chip *chip = &host->chip;
953 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
954
955 /*
956 * NAND_CMD_PARAM is called before we know much about the FLASH chip
957 * in use. we configure the controller to perform a raw read of 512
958 * bytes to read onfi params
959 */
960 nandc_set_reg(nandc, NAND_FLASH_CMD, PAGE_READ | PAGE_ACC | LAST_PAGE);
961 nandc_set_reg(nandc, NAND_ADDR0, 0);
962 nandc_set_reg(nandc, NAND_ADDR1, 0);
963 nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
964 | 512 << UD_SIZE_BYTES
965 | 5 << NUM_ADDR_CYCLES
966 | 0 << SPARE_SIZE_BYTES);
967 nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
968 | 0 << CS_ACTIVE_BSY
969 | 17 << BAD_BLOCK_BYTE_NUM
970 | 1 << BAD_BLOCK_IN_SPARE_AREA
971 | 2 << WR_RD_BSY_GAP
972 | 0 << WIDE_FLASH
973 | 1 << DEV0_CFG1_ECC_DISABLE);
974 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
975
976 /* configure CMD1 and VLD for ONFI param probing */
977 nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530978 (nandc->vld & ~READ_START_VLD));
Archit Tanejac76b78d2016-02-03 14:29:50 +0530979 nandc_set_reg(nandc, NAND_DEV_CMD1,
980 (nandc->cmd1 & ~(0xFF << READ_ADDR))
981 | NAND_CMD_PARAM << READ_ADDR);
982
983 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
984
985 nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
986 nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530987 nandc_set_read_loc(nandc, 0, 0, 512, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530988
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530989 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
990 write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530991
992 nandc->buf_count = 512;
993 memset(nandc->data_buffer, 0xff, nandc->buf_count);
994
Abhishek Sahubde43302017-07-19 17:17:55 +0530995 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530996
997 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530998 nandc->buf_count, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530999
1000 /* restore CMD1 and VLD regs */
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301001 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
1002 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301003
1004 return 0;
1005}
1006
1007/* sets up descriptors for NAND_CMD_ERASE1 */
1008static int erase_block(struct qcom_nand_host *host, int page_addr)
1009{
1010 struct nand_chip *chip = &host->chip;
1011 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1012
1013 nandc_set_reg(nandc, NAND_FLASH_CMD,
1014 BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
1015 nandc_set_reg(nandc, NAND_ADDR0, page_addr);
1016 nandc_set_reg(nandc, NAND_ADDR1, 0);
1017 nandc_set_reg(nandc, NAND_DEV0_CFG0,
1018 host->cfg0_raw & ~(7 << CW_PER_PAGE));
1019 nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
1020 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1021 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
1022 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
1023
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301024 write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
1025 write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
1026 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301027
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301028 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301029
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301030 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1031 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301032
1033 return 0;
1034}
1035
1036/* sets up descriptors for NAND_CMD_READID */
1037static int read_id(struct qcom_nand_host *host, int column)
1038{
1039 struct nand_chip *chip = &host->chip;
1040 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1041
1042 if (column == -1)
1043 return 0;
1044
1045 nandc_set_reg(nandc, NAND_FLASH_CMD, FETCH_ID);
1046 nandc_set_reg(nandc, NAND_ADDR0, column);
1047 nandc_set_reg(nandc, NAND_ADDR1, 0);
Abhishek Sahu9d43f912017-08-17 17:37:45 +05301048 nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT,
1049 nandc->props->is_bam ? 0 : DM_EN);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301050 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1051
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301052 write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
1053 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301054
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301055 read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301056
1057 return 0;
1058}
1059
1060/* sets up descriptors for NAND_CMD_RESET */
1061static int reset(struct qcom_nand_host *host)
1062{
1063 struct nand_chip *chip = &host->chip;
1064 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1065
1066 nandc_set_reg(nandc, NAND_FLASH_CMD, RESET_DEVICE);
1067 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1068
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301069 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1070 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301071
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301072 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301073
1074 return 0;
1075}
1076
1077/* helpers to submit/free our list of dma descriptors */
1078static int submit_descs(struct qcom_nand_controller *nandc)
1079{
1080 struct desc_info *desc;
1081 dma_cookie_t cookie = 0;
Abhishek Sahu381dd242017-08-17 17:37:41 +05301082 struct bam_transaction *bam_txn = nandc->bam_txn;
1083 int r;
1084
1085 if (nandc->props->is_bam) {
1086 if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
1087 r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
1088 if (r)
1089 return r;
1090 }
1091
1092 if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
1093 r = prepare_bam_async_desc(nandc, nandc->tx_chan,
1094 DMA_PREP_INTERRUPT);
1095 if (r)
1096 return r;
1097 }
1098
1099 if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
1100 r = prepare_bam_async_desc(nandc, nandc->cmd_chan, 0);
1101 if (r)
1102 return r;
1103 }
1104 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301105
1106 list_for_each_entry(desc, &nandc->desc_list, node)
1107 cookie = dmaengine_submit(desc->dma_desc);
1108
Abhishek Sahu381dd242017-08-17 17:37:41 +05301109 if (nandc->props->is_bam) {
1110 dma_async_issue_pending(nandc->tx_chan);
1111 dma_async_issue_pending(nandc->rx_chan);
1112
1113 if (dma_sync_wait(nandc->cmd_chan, cookie) != DMA_COMPLETE)
1114 return -ETIMEDOUT;
1115 } else {
1116 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
1117 return -ETIMEDOUT;
1118 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301119
1120 return 0;
1121}
1122
1123static void free_descs(struct qcom_nand_controller *nandc)
1124{
1125 struct desc_info *desc, *n;
1126
1127 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
1128 list_del(&desc->node);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301129
1130 if (nandc->props->is_bam)
1131 dma_unmap_sg(nandc->dev, desc->bam_sgl,
1132 desc->sgl_cnt, desc->dir);
1133 else
1134 dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
1135 desc->dir);
1136
Archit Tanejac76b78d2016-02-03 14:29:50 +05301137 kfree(desc);
1138 }
1139}
1140
1141/* reset the register read buffer for next NAND operation */
1142static void clear_read_regs(struct qcom_nand_controller *nandc)
1143{
1144 nandc->reg_read_pos = 0;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301145 nandc_read_buffer_sync(nandc, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301146}
1147
1148static void pre_command(struct qcom_nand_host *host, int command)
1149{
1150 struct nand_chip *chip = &host->chip;
1151 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1152
1153 nandc->buf_count = 0;
1154 nandc->buf_start = 0;
1155 host->use_ecc = false;
1156 host->last_command = command;
1157
1158 clear_read_regs(nandc);
1159}
1160
1161/*
1162 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
1163 * privately maintained status byte, this status byte can be read after
1164 * NAND_CMD_STATUS is called
1165 */
1166static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
1167{
1168 struct nand_chip *chip = &host->chip;
1169 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1170 struct nand_ecc_ctrl *ecc = &chip->ecc;
1171 int num_cw;
1172 int i;
1173
1174 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301175 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301176
1177 for (i = 0; i < num_cw; i++) {
1178 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
1179
1180 if (flash_status & FS_MPU_ERR)
1181 host->status &= ~NAND_STATUS_WP;
1182
1183 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
1184 (flash_status &
1185 FS_DEVICE_STS_ERR)))
1186 host->status |= NAND_STATUS_FAIL;
1187 }
1188}
1189
1190static void post_command(struct qcom_nand_host *host, int command)
1191{
1192 struct nand_chip *chip = &host->chip;
1193 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1194
1195 switch (command) {
1196 case NAND_CMD_READID:
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301197 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301198 memcpy(nandc->data_buffer, nandc->reg_read_buf,
1199 nandc->buf_count);
1200 break;
1201 case NAND_CMD_PAGEPROG:
1202 case NAND_CMD_ERASE1:
1203 parse_erase_write_errors(host, command);
1204 break;
1205 default:
1206 break;
1207 }
1208}
1209
1210/*
1211 * Implements chip->cmdfunc. It's only used for a limited set of commands.
1212 * The rest of the commands wouldn't be called by upper layers. For example,
1213 * NAND_CMD_READOOB would never be called because we have our own versions
1214 * of read_oob ops for nand_ecc_ctrl.
1215 */
1216static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
1217 int column, int page_addr)
1218{
1219 struct nand_chip *chip = mtd_to_nand(mtd);
1220 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1221 struct nand_ecc_ctrl *ecc = &chip->ecc;
1222 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1223 bool wait = false;
1224 int ret = 0;
1225
1226 pre_command(host, command);
1227
1228 switch (command) {
1229 case NAND_CMD_RESET:
1230 ret = reset(host);
1231 wait = true;
1232 break;
1233
1234 case NAND_CMD_READID:
1235 nandc->buf_count = 4;
1236 ret = read_id(host, column);
1237 wait = true;
1238 break;
1239
1240 case NAND_CMD_PARAM:
1241 ret = nandc_param(host);
1242 wait = true;
1243 break;
1244
1245 case NAND_CMD_ERASE1:
1246 ret = erase_block(host, page_addr);
1247 wait = true;
1248 break;
1249
1250 case NAND_CMD_READ0:
1251 /* we read the entire page for now */
1252 WARN_ON(column != 0);
1253
1254 host->use_ecc = true;
1255 set_address(host, 0, page_addr);
1256 update_rw_regs(host, ecc->steps, true);
1257 break;
1258
1259 case NAND_CMD_SEQIN:
1260 WARN_ON(column != 0);
1261 set_address(host, 0, page_addr);
1262 break;
1263
1264 case NAND_CMD_PAGEPROG:
1265 case NAND_CMD_STATUS:
1266 case NAND_CMD_NONE:
1267 default:
1268 break;
1269 }
1270
1271 if (ret) {
1272 dev_err(nandc->dev, "failure executing command %d\n",
1273 command);
1274 free_descs(nandc);
1275 return;
1276 }
1277
1278 if (wait) {
1279 ret = submit_descs(nandc);
1280 if (ret)
1281 dev_err(nandc->dev,
1282 "failure submitting descs for command %d\n",
1283 command);
1284 }
1285
1286 free_descs(nandc);
1287
1288 post_command(host, command);
1289}
1290
1291/*
1292 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1293 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1294 *
1295 * when using RS ECC, the HW reports the same erros when reading an erased CW,
1296 * but it notifies that it is an erased CW by placing special characters at
1297 * certain offsets in the buffer.
1298 *
1299 * verify if the page is erased or not, and fix up the page for RS ECC by
1300 * replacing the special characters with 0xff.
1301 */
1302static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
1303{
1304 u8 empty1, empty2;
1305
1306 /*
1307 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1308 * is erased by looking for 0x54s at offsets 3 and 175 from the
1309 * beginning of each codeword
1310 */
1311
1312 empty1 = data_buf[3];
1313 empty2 = data_buf[175];
1314
1315 /*
1316 * if the erased codework markers, if they exist override them with
1317 * 0xffs
1318 */
1319 if ((empty1 == 0x54 && empty2 == 0xff) ||
1320 (empty1 == 0xff && empty2 == 0x54)) {
1321 data_buf[3] = 0xff;
1322 data_buf[175] = 0xff;
1323 }
1324
1325 /*
1326 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1327 * restore the original values at the special offsets
1328 */
1329 if (memchr_inv(data_buf, 0xff, data_len)) {
1330 data_buf[3] = empty1;
1331 data_buf[175] = empty2;
1332
1333 return false;
1334 }
1335
1336 return true;
1337}
1338
1339struct read_stats {
1340 __le32 flash;
1341 __le32 buffer;
1342 __le32 erased_cw;
1343};
1344
1345/*
1346 * reads back status registers set by the controller to notify page read
1347 * errors. this is equivalent to what 'ecc->correct()' would do.
1348 */
1349static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1350 u8 *oob_buf)
1351{
1352 struct nand_chip *chip = &host->chip;
1353 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1354 struct mtd_info *mtd = nand_to_mtd(chip);
1355 struct nand_ecc_ctrl *ecc = &chip->ecc;
1356 unsigned int max_bitflips = 0;
1357 struct read_stats *buf;
1358 int i;
1359
1360 buf = (struct read_stats *)nandc->reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301361 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301362
1363 for (i = 0; i < ecc->steps; i++, buf++) {
1364 u32 flash, buffer, erased_cw;
1365 int data_len, oob_len;
1366
1367 if (i == (ecc->steps - 1)) {
1368 data_len = ecc->size - ((ecc->steps - 1) << 2);
1369 oob_len = ecc->steps << 2;
1370 } else {
1371 data_len = host->cw_data;
1372 oob_len = 0;
1373 }
1374
1375 flash = le32_to_cpu(buf->flash);
1376 buffer = le32_to_cpu(buf->buffer);
1377 erased_cw = le32_to_cpu(buf->erased_cw);
1378
1379 if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1380 bool erased;
1381
1382 /* ignore erased codeword errors */
1383 if (host->bch_enabled) {
1384 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1385 true : false;
1386 } else {
1387 erased = erased_chunk_check_and_fixup(data_buf,
1388 data_len);
1389 }
1390
1391 if (erased) {
1392 data_buf += data_len;
1393 if (oob_buf)
1394 oob_buf += oob_len + ecc->bytes;
1395 continue;
1396 }
1397
1398 if (buffer & BS_UNCORRECTABLE_BIT) {
1399 int ret, ecclen, extraooblen;
1400 void *eccbuf;
1401
1402 eccbuf = oob_buf ? oob_buf + oob_len : NULL;
1403 ecclen = oob_buf ? host->ecc_bytes_hw : 0;
1404 extraooblen = oob_buf ? oob_len : 0;
1405
1406 /*
1407 * make sure it isn't an erased page reported
1408 * as not-erased by HW because of a few bitflips
1409 */
1410 ret = nand_check_erased_ecc_chunk(data_buf,
1411 data_len, eccbuf, ecclen, oob_buf,
1412 extraooblen, ecc->strength);
1413 if (ret < 0) {
1414 mtd->ecc_stats.failed++;
1415 } else {
1416 mtd->ecc_stats.corrected += ret;
1417 max_bitflips =
1418 max_t(unsigned int, max_bitflips, ret);
1419 }
1420 }
1421 } else {
1422 unsigned int stat;
1423
1424 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1425 mtd->ecc_stats.corrected += stat;
1426 max_bitflips = max(max_bitflips, stat);
1427 }
1428
1429 data_buf += data_len;
1430 if (oob_buf)
1431 oob_buf += oob_len + ecc->bytes;
1432 }
1433
1434 return max_bitflips;
1435}
1436
1437/*
1438 * helper to perform the actual page read operation, used by ecc->read_page(),
1439 * ecc->read_oob()
1440 */
1441static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1442 u8 *oob_buf)
1443{
1444 struct nand_chip *chip = &host->chip;
1445 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1446 struct nand_ecc_ctrl *ecc = &chip->ecc;
1447 int i, ret;
1448
Abhishek Sahubde43302017-07-19 17:17:55 +05301449 config_nand_page_read(nandc);
1450
Archit Tanejac76b78d2016-02-03 14:29:50 +05301451 /* queue cmd descs for each codeword */
1452 for (i = 0; i < ecc->steps; i++) {
1453 int data_size, oob_size;
1454
1455 if (i == (ecc->steps - 1)) {
1456 data_size = ecc->size - ((ecc->steps - 1) << 2);
1457 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1458 host->spare_bytes;
1459 } else {
1460 data_size = host->cw_data;
1461 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1462 }
1463
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301464 if (nandc->props->is_bam) {
1465 if (data_buf && oob_buf) {
1466 nandc_set_read_loc(nandc, 0, 0, data_size, 0);
1467 nandc_set_read_loc(nandc, 1, data_size,
1468 oob_size, 1);
1469 } else if (data_buf) {
1470 nandc_set_read_loc(nandc, 0, 0, data_size, 1);
1471 } else {
1472 nandc_set_read_loc(nandc, 0, data_size,
1473 oob_size, 1);
1474 }
1475 }
1476
Abhishek Sahubde43302017-07-19 17:17:55 +05301477 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301478
1479 if (data_buf)
1480 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301481 data_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301482
1483 /*
1484 * when ecc is enabled, the controller doesn't read the real
1485 * or dummy bad block markers in each chunk. To maintain a
1486 * consistent layout across RAW and ECC reads, we just
1487 * leave the real/dummy BBM offsets empty (i.e, filled with
1488 * 0xffs)
1489 */
1490 if (oob_buf) {
1491 int j;
1492
1493 for (j = 0; j < host->bbm_size; j++)
1494 *oob_buf++ = 0xff;
1495
1496 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301497 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301498 }
1499
1500 if (data_buf)
1501 data_buf += data_size;
1502 if (oob_buf)
1503 oob_buf += oob_size;
1504 }
1505
1506 ret = submit_descs(nandc);
1507 if (ret)
1508 dev_err(nandc->dev, "failure to read page/oob\n");
1509
1510 free_descs(nandc);
1511
1512 return ret;
1513}
1514
1515/*
1516 * a helper that copies the last step/codeword of a page (containing free oob)
1517 * into our local buffer
1518 */
1519static int copy_last_cw(struct qcom_nand_host *host, int page)
1520{
1521 struct nand_chip *chip = &host->chip;
1522 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1523 struct nand_ecc_ctrl *ecc = &chip->ecc;
1524 int size;
1525 int ret;
1526
1527 clear_read_regs(nandc);
1528
1529 size = host->use_ecc ? host->cw_data : host->cw_size;
1530
1531 /* prepare a clean read buffer */
1532 memset(nandc->data_buffer, 0xff, size);
1533
1534 set_address(host, host->cw_size * (ecc->steps - 1), page);
1535 update_rw_regs(host, 1, true);
1536
Abhishek Sahubde43302017-07-19 17:17:55 +05301537 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301538
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301539 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301540
1541 ret = submit_descs(nandc);
1542 if (ret)
1543 dev_err(nandc->dev, "failed to copy last codeword\n");
1544
1545 free_descs(nandc);
1546
1547 return ret;
1548}
1549
1550/* implements ecc->read_page() */
1551static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1552 uint8_t *buf, int oob_required, int page)
1553{
1554 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1555 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1556 u8 *data_buf, *oob_buf = NULL;
1557 int ret;
1558
1559 data_buf = buf;
1560 oob_buf = oob_required ? chip->oob_poi : NULL;
1561
1562 ret = read_page_ecc(host, data_buf, oob_buf);
1563 if (ret) {
1564 dev_err(nandc->dev, "failure to read page\n");
1565 return ret;
1566 }
1567
1568 return parse_read_errors(host, data_buf, oob_buf);
1569}
1570
1571/* implements ecc->read_page_raw() */
1572static int qcom_nandc_read_page_raw(struct mtd_info *mtd,
1573 struct nand_chip *chip, uint8_t *buf,
1574 int oob_required, int page)
1575{
1576 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1577 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1578 u8 *data_buf, *oob_buf;
1579 struct nand_ecc_ctrl *ecc = &chip->ecc;
1580 int i, ret;
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301581 int read_loc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301582
1583 data_buf = buf;
1584 oob_buf = chip->oob_poi;
1585
1586 host->use_ecc = false;
1587 update_rw_regs(host, ecc->steps, true);
Abhishek Sahubde43302017-07-19 17:17:55 +05301588 config_nand_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301589
1590 for (i = 0; i < ecc->steps; i++) {
1591 int data_size1, data_size2, oob_size1, oob_size2;
1592 int reg_off = FLASH_BUF_ACC;
1593
1594 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1595 oob_size1 = host->bbm_size;
1596
1597 if (i == (ecc->steps - 1)) {
1598 data_size2 = ecc->size - data_size1 -
1599 ((ecc->steps - 1) << 2);
1600 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1601 host->spare_bytes;
1602 } else {
1603 data_size2 = host->cw_data - data_size1;
1604 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1605 }
1606
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301607 if (nandc->props->is_bam) {
1608 read_loc = 0;
1609 nandc_set_read_loc(nandc, 0, read_loc, data_size1, 0);
1610 read_loc += data_size1;
1611
1612 nandc_set_read_loc(nandc, 1, read_loc, oob_size1, 0);
1613 read_loc += oob_size1;
1614
1615 nandc_set_read_loc(nandc, 2, read_loc, data_size2, 0);
1616 read_loc += data_size2;
1617
1618 nandc_set_read_loc(nandc, 3, read_loc, oob_size2, 1);
1619 }
1620
Abhishek Sahubde43302017-07-19 17:17:55 +05301621 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301622
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301623 read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301624 reg_off += data_size1;
1625 data_buf += data_size1;
1626
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301627 read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301628 reg_off += oob_size1;
1629 oob_buf += oob_size1;
1630
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301631 read_data_dma(nandc, reg_off, data_buf, data_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301632 reg_off += data_size2;
1633 data_buf += data_size2;
1634
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301635 read_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301636 oob_buf += oob_size2;
1637 }
1638
1639 ret = submit_descs(nandc);
1640 if (ret)
1641 dev_err(nandc->dev, "failure to read raw page\n");
1642
1643 free_descs(nandc);
1644
1645 return 0;
1646}
1647
1648/* implements ecc->read_oob() */
1649static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1650 int page)
1651{
1652 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1653 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1654 struct nand_ecc_ctrl *ecc = &chip->ecc;
1655 int ret;
1656
1657 clear_read_regs(nandc);
1658
1659 host->use_ecc = true;
1660 set_address(host, 0, page);
1661 update_rw_regs(host, ecc->steps, true);
1662
1663 ret = read_page_ecc(host, NULL, chip->oob_poi);
1664 if (ret)
1665 dev_err(nandc->dev, "failure to read oob\n");
1666
1667 return ret;
1668}
1669
1670/* implements ecc->write_page() */
1671static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1672 const uint8_t *buf, int oob_required, int page)
1673{
1674 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1675 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1676 struct nand_ecc_ctrl *ecc = &chip->ecc;
1677 u8 *data_buf, *oob_buf;
1678 int i, ret;
1679
1680 clear_read_regs(nandc);
1681
1682 data_buf = (u8 *)buf;
1683 oob_buf = chip->oob_poi;
1684
1685 host->use_ecc = true;
1686 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301687 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301688
1689 for (i = 0; i < ecc->steps; i++) {
1690 int data_size, oob_size;
1691
1692 if (i == (ecc->steps - 1)) {
1693 data_size = ecc->size - ((ecc->steps - 1) << 2);
1694 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1695 host->spare_bytes;
1696 } else {
1697 data_size = host->cw_data;
1698 oob_size = ecc->bytes;
1699 }
1700
Archit Tanejac76b78d2016-02-03 14:29:50 +05301701
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301702 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
1703 i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301704
1705 /*
1706 * when ECC is enabled, we don't really need to write anything
1707 * to oob for the first n - 1 codewords since these oob regions
1708 * just contain ECC bytes that's written by the controller
1709 * itself. For the last codeword, we skip the bbm positions and
1710 * write to the free oob area.
1711 */
1712 if (i == (ecc->steps - 1)) {
1713 oob_buf += host->bbm_size;
1714
1715 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301716 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301717 }
1718
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301719 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301720
1721 data_buf += data_size;
1722 oob_buf += oob_size;
1723 }
1724
1725 ret = submit_descs(nandc);
1726 if (ret)
1727 dev_err(nandc->dev, "failure to write page\n");
1728
1729 free_descs(nandc);
1730
1731 return ret;
1732}
1733
1734/* implements ecc->write_page_raw() */
1735static int qcom_nandc_write_page_raw(struct mtd_info *mtd,
1736 struct nand_chip *chip, const uint8_t *buf,
1737 int oob_required, int page)
1738{
1739 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1740 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1741 struct nand_ecc_ctrl *ecc = &chip->ecc;
1742 u8 *data_buf, *oob_buf;
1743 int i, ret;
1744
1745 clear_read_regs(nandc);
1746
1747 data_buf = (u8 *)buf;
1748 oob_buf = chip->oob_poi;
1749
1750 host->use_ecc = false;
1751 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301752 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301753
1754 for (i = 0; i < ecc->steps; i++) {
1755 int data_size1, data_size2, oob_size1, oob_size2;
1756 int reg_off = FLASH_BUF_ACC;
1757
1758 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1759 oob_size1 = host->bbm_size;
1760
1761 if (i == (ecc->steps - 1)) {
1762 data_size2 = ecc->size - data_size1 -
1763 ((ecc->steps - 1) << 2);
1764 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1765 host->spare_bytes;
1766 } else {
1767 data_size2 = host->cw_data - data_size1;
1768 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1769 }
1770
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301771 write_data_dma(nandc, reg_off, data_buf, data_size1,
1772 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301773 reg_off += data_size1;
1774 data_buf += data_size1;
1775
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301776 write_data_dma(nandc, reg_off, oob_buf, oob_size1,
1777 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301778 reg_off += oob_size1;
1779 oob_buf += oob_size1;
1780
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301781 write_data_dma(nandc, reg_off, data_buf, data_size2,
1782 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301783 reg_off += data_size2;
1784 data_buf += data_size2;
1785
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301786 write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301787 oob_buf += oob_size2;
1788
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301789 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301790 }
1791
1792 ret = submit_descs(nandc);
1793 if (ret)
1794 dev_err(nandc->dev, "failure to write raw page\n");
1795
1796 free_descs(nandc);
1797
1798 return ret;
1799}
1800
1801/*
1802 * implements ecc->write_oob()
1803 *
1804 * the NAND controller cannot write only data or only oob within a codeword,
1805 * since ecc is calculated for the combined codeword. we first copy the
1806 * entire contents for the last codeword(data + oob), replace the old oob
1807 * with the new one in chip->oob_poi, and then write the entire codeword.
1808 * this read-copy-write operation results in a slight performance loss.
1809 */
1810static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1811 int page)
1812{
1813 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1814 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1815 struct nand_ecc_ctrl *ecc = &chip->ecc;
1816 u8 *oob = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301817 int data_size, oob_size;
1818 int ret, status = 0;
1819
1820 host->use_ecc = true;
1821
1822 ret = copy_last_cw(host, page);
1823 if (ret)
1824 return ret;
1825
1826 clear_read_regs(nandc);
1827
1828 /* calculate the data and oob size for the last codeword/step */
1829 data_size = ecc->size - ((ecc->steps - 1) << 2);
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001830 oob_size = mtd->oobavail;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301831
1832 /* override new oob content to last codeword */
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001833 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
1834 0, mtd->oobavail);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301835
1836 set_address(host, host->cw_size * (ecc->steps - 1), page);
1837 update_rw_regs(host, 1, false);
1838
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301839 config_nand_page_write(nandc);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301840 write_data_dma(nandc, FLASH_BUF_ACC,
1841 nandc->data_buffer, data_size + oob_size, 0);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301842 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301843
1844 ret = submit_descs(nandc);
1845
1846 free_descs(nandc);
1847
1848 if (ret) {
1849 dev_err(nandc->dev, "failure to write oob\n");
1850 return -EIO;
1851 }
1852
1853 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1854
1855 status = chip->waitfunc(mtd, chip);
1856
1857 return status & NAND_STATUS_FAIL ? -EIO : 0;
1858}
1859
1860static int qcom_nandc_block_bad(struct mtd_info *mtd, loff_t ofs)
1861{
1862 struct nand_chip *chip = mtd_to_nand(mtd);
1863 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1864 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1865 struct nand_ecc_ctrl *ecc = &chip->ecc;
1866 int page, ret, bbpos, bad = 0;
1867 u32 flash_status;
1868
1869 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1870
1871 /*
1872 * configure registers for a raw sub page read, the address is set to
1873 * the beginning of the last codeword, we don't care about reading ecc
1874 * portion of oob. we just want the first few bytes from this codeword
1875 * that contains the BBM
1876 */
1877 host->use_ecc = false;
1878
1879 ret = copy_last_cw(host, page);
1880 if (ret)
1881 goto err;
1882
1883 flash_status = le32_to_cpu(nandc->reg_read_buf[0]);
1884
1885 if (flash_status & (FS_OP_ERR | FS_MPU_ERR)) {
1886 dev_warn(nandc->dev, "error when trying to read BBM\n");
1887 goto err;
1888 }
1889
1890 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
1891
1892 bad = nandc->data_buffer[bbpos] != 0xff;
1893
1894 if (chip->options & NAND_BUSWIDTH_16)
1895 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
1896err:
1897 return bad;
1898}
1899
1900static int qcom_nandc_block_markbad(struct mtd_info *mtd, loff_t ofs)
1901{
1902 struct nand_chip *chip = mtd_to_nand(mtd);
1903 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1904 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1905 struct nand_ecc_ctrl *ecc = &chip->ecc;
1906 int page, ret, status = 0;
1907
1908 clear_read_regs(nandc);
1909
1910 /*
1911 * to mark the BBM as bad, we flash the entire last codeword with 0s.
1912 * we don't care about the rest of the content in the codeword since
1913 * we aren't going to use this block again
1914 */
1915 memset(nandc->data_buffer, 0x00, host->cw_size);
1916
1917 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1918
1919 /* prepare write */
1920 host->use_ecc = false;
1921 set_address(host, host->cw_size * (ecc->steps - 1), page);
1922 update_rw_regs(host, 1, false);
1923
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301924 config_nand_page_write(nandc);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301925 write_data_dma(nandc, FLASH_BUF_ACC,
1926 nandc->data_buffer, host->cw_size, 0);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301927 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301928
1929 ret = submit_descs(nandc);
1930
1931 free_descs(nandc);
1932
1933 if (ret) {
1934 dev_err(nandc->dev, "failure to update BBM\n");
1935 return -EIO;
1936 }
1937
1938 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1939
1940 status = chip->waitfunc(mtd, chip);
1941
1942 return status & NAND_STATUS_FAIL ? -EIO : 0;
1943}
1944
1945/*
1946 * the three functions below implement chip->read_byte(), chip->read_buf()
1947 * and chip->write_buf() respectively. these aren't used for
1948 * reading/writing page data, they are used for smaller data like reading
1949 * id, status etc
1950 */
1951static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
1952{
1953 struct nand_chip *chip = mtd_to_nand(mtd);
1954 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1955 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1956 u8 *buf = nandc->data_buffer;
1957 u8 ret = 0x0;
1958
1959 if (host->last_command == NAND_CMD_STATUS) {
1960 ret = host->status;
1961
1962 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
1963
1964 return ret;
1965 }
1966
1967 if (nandc->buf_start < nandc->buf_count)
1968 ret = buf[nandc->buf_start++];
1969
1970 return ret;
1971}
1972
1973static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1974{
1975 struct nand_chip *chip = mtd_to_nand(mtd);
1976 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1977 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
1978
1979 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
1980 nandc->buf_start += real_len;
1981}
1982
1983static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
1984 int len)
1985{
1986 struct nand_chip *chip = mtd_to_nand(mtd);
1987 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1988 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
1989
1990 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
1991
1992 nandc->buf_start += real_len;
1993}
1994
1995/* we support only one external chip for now */
1996static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
1997{
1998 struct nand_chip *chip = mtd_to_nand(mtd);
1999 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2000
2001 if (chipnr <= 0)
2002 return;
2003
2004 dev_warn(nandc->dev, "invalid chip select\n");
2005}
2006
2007/*
2008 * NAND controller page layout info
2009 *
2010 * Layout with ECC enabled:
2011 *
2012 * |----------------------| |---------------------------------|
2013 * | xx.......yy| | *********xx.......yy|
2014 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
2015 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
2016 * | xx.......yy| | *********xx.......yy|
2017 * |----------------------| |---------------------------------|
2018 * codeword 1,2..n-1 codeword n
2019 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
2020 *
2021 * n = Number of codewords in the page
2022 * . = ECC bytes
2023 * * = Spare/free bytes
2024 * x = Unused byte(s)
2025 * y = Reserved byte(s)
2026 *
2027 * 2K page: n = 4, spare = 16 bytes
2028 * 4K page: n = 8, spare = 32 bytes
2029 * 8K page: n = 16, spare = 64 bytes
2030 *
2031 * the qcom nand controller operates at a sub page/codeword level. each
2032 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2033 * the number of ECC bytes vary based on the ECC strength and the bus width.
2034 *
2035 * the first n - 1 codewords contains 516 bytes of user data, the remaining
2036 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2037 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2038 *
2039 * When we access a page with ECC enabled, the reserved bytes(s) are not
2040 * accessible at all. When reading, we fill up these unreadable positions
2041 * with 0xffs. When writing, the controller skips writing the inaccessible
2042 * bytes.
2043 *
2044 * Layout with ECC disabled:
2045 *
2046 * |------------------------------| |---------------------------------------|
2047 * | yy xx.......| | bb *********xx.......|
2048 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
2049 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
2050 * | yy xx.......| | bb *********xx.......|
2051 * |------------------------------| |---------------------------------------|
2052 * codeword 1,2..n-1 codeword n
2053 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
2054 *
2055 * n = Number of codewords in the page
2056 * . = ECC bytes
2057 * * = Spare/free bytes
2058 * x = Unused byte(s)
2059 * y = Dummy Bad Bock byte(s)
2060 * b = Real Bad Block byte(s)
2061 * size1/size2 = function of codeword size and 'n'
2062 *
2063 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2064 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2065 * Block Markers. In the last codeword, this position contains the real BBM
2066 *
2067 * In order to have a consistent layout between RAW and ECC modes, we assume
2068 * the following OOB layout arrangement:
2069 *
2070 * |-----------| |--------------------|
2071 * |yyxx.......| |bb*********xx.......|
2072 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
2073 * |yyxx.......| |bb*********xx.......|
2074 * |yyxx.......| |bb*********xx.......|
2075 * |-----------| |--------------------|
2076 * first n - 1 nth OOB region
2077 * OOB regions
2078 *
2079 * n = Number of codewords in the page
2080 * . = ECC bytes
2081 * * = FREE OOB bytes
2082 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2083 * x = Unused byte(s)
2084 * b = Real bad block byte(s) (inaccessible when ECC enabled)
2085 *
2086 * This layout is read as is when ECC is disabled. When ECC is enabled, the
2087 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2088 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
Boris Brezillon421e81c2016-03-18 17:54:27 +01002089 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2090 * the sum of the three).
Archit Tanejac76b78d2016-02-03 14:29:50 +05302091 */
Boris Brezillon421e81c2016-03-18 17:54:27 +01002092static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2093 struct mtd_oob_region *oobregion)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302094{
Boris Brezillon421e81c2016-03-18 17:54:27 +01002095 struct nand_chip *chip = mtd_to_nand(mtd);
2096 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302097 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302098
Boris Brezillon421e81c2016-03-18 17:54:27 +01002099 if (section > 1)
2100 return -ERANGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302101
Boris Brezillon421e81c2016-03-18 17:54:27 +01002102 if (!section) {
2103 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
2104 host->bbm_size;
2105 oobregion->offset = 0;
2106 } else {
2107 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
2108 oobregion->offset = mtd->oobsize - oobregion->length;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302109 }
2110
Boris Brezillon421e81c2016-03-18 17:54:27 +01002111 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302112}
2113
Boris Brezillon421e81c2016-03-18 17:54:27 +01002114static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
2115 struct mtd_oob_region *oobregion)
2116{
2117 struct nand_chip *chip = mtd_to_nand(mtd);
2118 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2119 struct nand_ecc_ctrl *ecc = &chip->ecc;
2120
2121 if (section)
2122 return -ERANGE;
2123
2124 oobregion->length = ecc->steps * 4;
2125 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
2126
2127 return 0;
2128}
2129
2130static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
2131 .ecc = qcom_nand_ooblayout_ecc,
2132 .free = qcom_nand_ooblayout_free,
2133};
2134
Archit Tanejac76b78d2016-02-03 14:29:50 +05302135static int qcom_nand_host_setup(struct qcom_nand_host *host)
2136{
2137 struct nand_chip *chip = &host->chip;
2138 struct mtd_info *mtd = nand_to_mtd(chip);
2139 struct nand_ecc_ctrl *ecc = &chip->ecc;
2140 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2141 int cwperpage, bad_block_byte;
2142 bool wide_bus;
2143 int ecc_mode = 1;
2144
2145 /*
2146 * the controller requires each step consists of 512 bytes of data.
2147 * bail out if DT has populated a wrong step size.
2148 */
2149 if (ecc->size != NANDC_STEP_SIZE) {
2150 dev_err(nandc->dev, "invalid ecc size\n");
2151 return -EINVAL;
2152 }
2153
2154 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
2155
2156 if (ecc->strength >= 8) {
2157 /* 8 bit ECC defaults to BCH ECC on all platforms */
2158 host->bch_enabled = true;
2159 ecc_mode = 1;
2160
2161 if (wide_bus) {
2162 host->ecc_bytes_hw = 14;
2163 host->spare_bytes = 0;
2164 host->bbm_size = 2;
2165 } else {
2166 host->ecc_bytes_hw = 13;
2167 host->spare_bytes = 2;
2168 host->bbm_size = 1;
2169 }
2170 } else {
2171 /*
2172 * if the controller supports BCH for 4 bit ECC, the controller
2173 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2174 * always 10 bytes
2175 */
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302176 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302177 /* BCH */
2178 host->bch_enabled = true;
2179 ecc_mode = 0;
2180
2181 if (wide_bus) {
2182 host->ecc_bytes_hw = 8;
2183 host->spare_bytes = 2;
2184 host->bbm_size = 2;
2185 } else {
2186 host->ecc_bytes_hw = 7;
2187 host->spare_bytes = 4;
2188 host->bbm_size = 1;
2189 }
2190 } else {
2191 /* RS */
2192 host->ecc_bytes_hw = 10;
2193
2194 if (wide_bus) {
2195 host->spare_bytes = 0;
2196 host->bbm_size = 2;
2197 } else {
2198 host->spare_bytes = 1;
2199 host->bbm_size = 1;
2200 }
2201 }
2202 }
2203
2204 /*
2205 * we consider ecc->bytes as the sum of all the non-data content in a
2206 * step. It gives us a clean representation of the oob area (even if
2207 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2208 * ECC and 12 bytes for 4 bit ECC
2209 */
2210 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
2211
2212 ecc->read_page = qcom_nandc_read_page;
2213 ecc->read_page_raw = qcom_nandc_read_page_raw;
2214 ecc->read_oob = qcom_nandc_read_oob;
2215 ecc->write_page = qcom_nandc_write_page;
2216 ecc->write_page_raw = qcom_nandc_write_page_raw;
2217 ecc->write_oob = qcom_nandc_write_oob;
2218
2219 ecc->mode = NAND_ECC_HW;
2220
Boris Brezillon421e81c2016-03-18 17:54:27 +01002221 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302222
2223 cwperpage = mtd->writesize / ecc->size;
Abhishek Sahucb80f112017-08-17 17:37:40 +05302224 nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
2225 cwperpage);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302226
2227 /*
2228 * DATA_UD_BYTES varies based on whether the read/write command protects
2229 * spare data with ECC too. We protect spare data by default, so we set
2230 * it to main + spare data, which are 512 and 4 bytes respectively.
2231 */
2232 host->cw_data = 516;
2233
2234 /*
2235 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2236 * for 8 bit ECC
2237 */
2238 host->cw_size = host->cw_data + ecc->bytes;
2239
2240 if (ecc->bytes * (mtd->writesize / ecc->size) > mtd->oobsize) {
2241 dev_err(nandc->dev, "ecc data doesn't fit in OOB area\n");
2242 return -EINVAL;
2243 }
2244
2245 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
2246
2247 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
2248 | host->cw_data << UD_SIZE_BYTES
2249 | 0 << DISABLE_STATUS_AFTER_WRITE
2250 | 5 << NUM_ADDR_CYCLES
2251 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
2252 | 0 << STATUS_BFR_READ
2253 | 1 << SET_RD_MODE_AFTER_STATUS
2254 | host->spare_bytes << SPARE_SIZE_BYTES;
2255
2256 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
2257 | 0 << CS_ACTIVE_BSY
2258 | bad_block_byte << BAD_BLOCK_BYTE_NUM
2259 | 0 << BAD_BLOCK_IN_SPARE_AREA
2260 | 2 << WR_RD_BSY_GAP
2261 | wide_bus << WIDE_FLASH
2262 | host->bch_enabled << ENABLE_BCH_ECC;
2263
2264 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
2265 | host->cw_size << UD_SIZE_BYTES
2266 | 5 << NUM_ADDR_CYCLES
2267 | 0 << SPARE_SIZE_BYTES;
2268
2269 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
2270 | 0 << CS_ACTIVE_BSY
2271 | 17 << BAD_BLOCK_BYTE_NUM
2272 | 1 << BAD_BLOCK_IN_SPARE_AREA
2273 | 2 << WR_RD_BSY_GAP
2274 | wide_bus << WIDE_FLASH
2275 | 1 << DEV0_CFG1_ECC_DISABLE;
2276
Abhishek Sahu10777de2017-08-03 17:56:39 +02002277 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
Archit Tanejac76b78d2016-02-03 14:29:50 +05302278 | 0 << ECC_SW_RESET
2279 | host->cw_data << ECC_NUM_DATA_BYTES
2280 | 1 << ECC_FORCE_CLK_OPEN
2281 | ecc_mode << ECC_MODE
2282 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
2283
2284 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
2285
2286 host->clrflashstatus = FS_READY_BSY_N;
2287 host->clrreadstatus = 0xc0;
Abhishek Sahua86b9c42017-08-17 17:37:44 +05302288 nandc->regs->erased_cw_detect_cfg_clr =
2289 cpu_to_le32(CLR_ERASED_PAGE_DET);
2290 nandc->regs->erased_cw_detect_cfg_set =
2291 cpu_to_le32(SET_ERASED_PAGE_DET);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302292
2293 dev_dbg(nandc->dev,
2294 "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",
2295 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
2296 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
2297 cwperpage);
2298
2299 return 0;
2300}
2301
2302static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
2303{
2304 int ret;
2305
2306 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
2307 if (ret) {
2308 dev_err(nandc->dev, "failed to set DMA mask\n");
2309 return ret;
2310 }
2311
2312 /*
2313 * we use the internal buffer for reading ONFI params, reading small
2314 * data like ID and status, and preforming read-copy-write operations
2315 * when writing to a codeword partially. 532 is the maximum possible
2316 * size of a codeword for our nand controller
2317 */
2318 nandc->buf_size = 532;
2319
2320 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
2321 GFP_KERNEL);
2322 if (!nandc->data_buffer)
2323 return -ENOMEM;
2324
2325 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
2326 GFP_KERNEL);
2327 if (!nandc->regs)
2328 return -ENOMEM;
2329
2330 nandc->reg_read_buf = devm_kzalloc(nandc->dev,
2331 MAX_REG_RD * sizeof(*nandc->reg_read_buf),
2332 GFP_KERNEL);
2333 if (!nandc->reg_read_buf)
2334 return -ENOMEM;
2335
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302336 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302337 nandc->reg_read_dma =
2338 dma_map_single(nandc->dev, nandc->reg_read_buf,
2339 MAX_REG_RD *
2340 sizeof(*nandc->reg_read_buf),
2341 DMA_FROM_DEVICE);
2342 if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
2343 dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
2344 return -EIO;
2345 }
2346
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302347 nandc->tx_chan = dma_request_slave_channel(nandc->dev, "tx");
2348 if (!nandc->tx_chan) {
2349 dev_err(nandc->dev, "failed to request tx channel\n");
2350 return -ENODEV;
2351 }
2352
2353 nandc->rx_chan = dma_request_slave_channel(nandc->dev, "rx");
2354 if (!nandc->rx_chan) {
2355 dev_err(nandc->dev, "failed to request rx channel\n");
2356 return -ENODEV;
2357 }
2358
2359 nandc->cmd_chan = dma_request_slave_channel(nandc->dev, "cmd");
2360 if (!nandc->cmd_chan) {
2361 dev_err(nandc->dev, "failed to request cmd channel\n");
2362 return -ENODEV;
2363 }
Abhishek Sahucb80f112017-08-17 17:37:40 +05302364
2365 /*
2366 * Initially allocate BAM transaction to read ONFI param page.
2367 * After detecting all the devices, this BAM transaction will
2368 * be freed and the next BAM tranasction will be allocated with
2369 * maximum codeword size
2370 */
2371 nandc->max_cwperpage = 1;
2372 nandc->bam_txn = alloc_bam_transaction(nandc);
2373 if (!nandc->bam_txn) {
2374 dev_err(nandc->dev,
2375 "failed to allocate bam transaction\n");
2376 return -ENOMEM;
2377 }
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302378 } else {
2379 nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
2380 if (!nandc->chan) {
2381 dev_err(nandc->dev,
2382 "failed to request slave channel\n");
2383 return -ENODEV;
2384 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302385 }
2386
2387 INIT_LIST_HEAD(&nandc->desc_list);
2388 INIT_LIST_HEAD(&nandc->host_list);
2389
Marc Gonzalezd45bc582016-07-27 11:23:52 +02002390 nand_hw_control_init(&nandc->controller);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302391
2392 return 0;
2393}
2394
2395static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2396{
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302397 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302398 if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
2399 dma_unmap_single(nandc->dev, nandc->reg_read_dma,
2400 MAX_REG_RD *
2401 sizeof(*nandc->reg_read_buf),
2402 DMA_FROM_DEVICE);
2403
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302404 if (nandc->tx_chan)
2405 dma_release_channel(nandc->tx_chan);
2406
2407 if (nandc->rx_chan)
2408 dma_release_channel(nandc->rx_chan);
2409
2410 if (nandc->cmd_chan)
2411 dma_release_channel(nandc->cmd_chan);
2412 } else {
2413 if (nandc->chan)
2414 dma_release_channel(nandc->chan);
2415 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302416}
2417
2418/* one time setup of a few nand controller registers */
2419static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2420{
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302421 u32 nand_ctrl;
2422
Archit Tanejac76b78d2016-02-03 14:29:50 +05302423 /* kill onenand */
2424 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
Abhishek Sahud8a9b322017-08-11 17:09:16 +05302425 nandc_write(nandc, NAND_DEV_CMD_VLD, NAND_DEV_CMD_VLD_VAL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302426
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302427 /* enable ADM or BAM DMA */
2428 if (nandc->props->is_bam) {
2429 nand_ctrl = nandc_read(nandc, NAND_CTRL);
2430 nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
2431 } else {
2432 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2433 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302434
2435 /* save the original values of these registers */
2436 nandc->cmd1 = nandc_read(nandc, NAND_DEV_CMD1);
Abhishek Sahud8a9b322017-08-11 17:09:16 +05302437 nandc->vld = NAND_DEV_CMD_VLD_VAL;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302438
2439 return 0;
2440}
2441
2442static int qcom_nand_host_init(struct qcom_nand_controller *nandc,
2443 struct qcom_nand_host *host,
2444 struct device_node *dn)
2445{
2446 struct nand_chip *chip = &host->chip;
2447 struct mtd_info *mtd = nand_to_mtd(chip);
2448 struct device *dev = nandc->dev;
2449 int ret;
2450
2451 ret = of_property_read_u32(dn, "reg", &host->cs);
2452 if (ret) {
2453 dev_err(dev, "can't get chip-select\n");
2454 return -ENXIO;
2455 }
2456
2457 nand_set_flash_node(chip, dn);
2458 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
2459 mtd->owner = THIS_MODULE;
2460 mtd->dev.parent = dev;
2461
2462 chip->cmdfunc = qcom_nandc_command;
2463 chip->select_chip = qcom_nandc_select_chip;
2464 chip->read_byte = qcom_nandc_read_byte;
2465 chip->read_buf = qcom_nandc_read_buf;
2466 chip->write_buf = qcom_nandc_write_buf;
Boris Brezillon4a78cc62017-05-26 17:10:15 +02002467 chip->onfi_set_features = nand_onfi_get_set_features_notsupp;
2468 chip->onfi_get_features = nand_onfi_get_set_features_notsupp;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302469
2470 /*
2471 * the bad block marker is readable only when we read the last codeword
2472 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2473 * helpers don't allow us to read BB from a nand chip with ECC
2474 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2475 * and block_markbad helpers until we permanently switch to using
2476 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2477 */
2478 chip->block_bad = qcom_nandc_block_bad;
2479 chip->block_markbad = qcom_nandc_block_markbad;
2480
2481 chip->controller = &nandc->controller;
2482 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
2483 NAND_SKIP_BBTSCAN;
2484
2485 /* set up initial status value */
2486 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2487
2488 ret = nand_scan_ident(mtd, 1, NULL);
2489 if (ret)
2490 return ret;
2491
2492 ret = qcom_nand_host_setup(host);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302493
2494 return ret;
2495}
2496
2497static int qcom_nand_mtd_register(struct qcom_nand_controller *nandc,
2498 struct qcom_nand_host *host,
2499 struct device_node *dn)
2500{
2501 struct nand_chip *chip = &host->chip;
2502 struct mtd_info *mtd = nand_to_mtd(chip);
2503 int ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302504
2505 ret = nand_scan_tail(mtd);
2506 if (ret)
2507 return ret;
2508
Abhishek Sahu89f51272017-07-19 17:17:58 +05302509 ret = mtd_device_register(mtd, NULL, 0);
2510 if (ret)
2511 nand_cleanup(mtd_to_nand(mtd));
2512
2513 return ret;
2514}
2515
2516static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2517{
2518 struct device *dev = nandc->dev;
2519 struct device_node *dn = dev->of_node, *child;
2520 struct qcom_nand_host *host, *tmp;
2521 int ret;
2522
2523 for_each_available_child_of_node(dn, child) {
2524 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2525 if (!host) {
2526 of_node_put(child);
2527 return -ENOMEM;
2528 }
2529
2530 ret = qcom_nand_host_init(nandc, host, child);
2531 if (ret) {
2532 devm_kfree(dev, host);
2533 continue;
2534 }
2535
2536 list_add_tail(&host->node, &nandc->host_list);
2537 }
2538
2539 if (list_empty(&nandc->host_list))
2540 return -ENODEV;
2541
Abhishek Sahucb80f112017-08-17 17:37:40 +05302542 if (nandc->props->is_bam) {
2543 free_bam_transaction(nandc);
2544 nandc->bam_txn = alloc_bam_transaction(nandc);
2545 if (!nandc->bam_txn) {
2546 dev_err(nandc->dev,
2547 "failed to allocate bam transaction\n");
2548 return -ENOMEM;
2549 }
2550 }
2551
Abhishek Sahu89f51272017-07-19 17:17:58 +05302552 list_for_each_entry_safe(host, tmp, &nandc->host_list, node) {
2553 ret = qcom_nand_mtd_register(nandc, host, child);
2554 if (ret) {
2555 list_del(&host->node);
2556 devm_kfree(dev, host);
2557 }
2558 }
2559
2560 if (list_empty(&nandc->host_list))
2561 return -ENODEV;
2562
2563 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302564}
2565
2566/* parse custom DT properties here */
2567static int qcom_nandc_parse_dt(struct platform_device *pdev)
2568{
2569 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2570 struct device_node *np = nandc->dev->of_node;
2571 int ret;
2572
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302573 if (!nandc->props->is_bam) {
2574 ret = of_property_read_u32(np, "qcom,cmd-crci",
2575 &nandc->cmd_crci);
2576 if (ret) {
2577 dev_err(nandc->dev, "command CRCI unspecified\n");
2578 return ret;
2579 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302580
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302581 ret = of_property_read_u32(np, "qcom,data-crci",
2582 &nandc->data_crci);
2583 if (ret) {
2584 dev_err(nandc->dev, "data CRCI unspecified\n");
2585 return ret;
2586 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302587 }
2588
2589 return 0;
2590}
2591
2592static int qcom_nandc_probe(struct platform_device *pdev)
2593{
2594 struct qcom_nand_controller *nandc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302595 const void *dev_data;
2596 struct device *dev = &pdev->dev;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302597 struct resource *res;
2598 int ret;
2599
2600 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2601 if (!nandc)
2602 return -ENOMEM;
2603
2604 platform_set_drvdata(pdev, nandc);
2605 nandc->dev = dev;
2606
2607 dev_data = of_device_get_match_data(dev);
2608 if (!dev_data) {
2609 dev_err(&pdev->dev, "failed to get device data\n");
2610 return -ENODEV;
2611 }
2612
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302613 nandc->props = dev_data;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302614
2615 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2616 nandc->base = devm_ioremap_resource(dev, res);
2617 if (IS_ERR(nandc->base))
2618 return PTR_ERR(nandc->base);
2619
2620 nandc->base_dma = phys_to_dma(dev, (phys_addr_t)res->start);
2621
2622 nandc->core_clk = devm_clk_get(dev, "core");
2623 if (IS_ERR(nandc->core_clk))
2624 return PTR_ERR(nandc->core_clk);
2625
2626 nandc->aon_clk = devm_clk_get(dev, "aon");
2627 if (IS_ERR(nandc->aon_clk))
2628 return PTR_ERR(nandc->aon_clk);
2629
2630 ret = qcom_nandc_parse_dt(pdev);
2631 if (ret)
2632 return ret;
2633
2634 ret = qcom_nandc_alloc(nandc);
2635 if (ret)
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302636 goto err_core_clk;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302637
2638 ret = clk_prepare_enable(nandc->core_clk);
2639 if (ret)
2640 goto err_core_clk;
2641
2642 ret = clk_prepare_enable(nandc->aon_clk);
2643 if (ret)
2644 goto err_aon_clk;
2645
2646 ret = qcom_nandc_setup(nandc);
2647 if (ret)
2648 goto err_setup;
2649
Abhishek Sahu89f51272017-07-19 17:17:58 +05302650 ret = qcom_probe_nand_devices(nandc);
2651 if (ret)
2652 goto err_setup;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302653
2654 return 0;
2655
Archit Tanejac76b78d2016-02-03 14:29:50 +05302656err_setup:
2657 clk_disable_unprepare(nandc->aon_clk);
2658err_aon_clk:
2659 clk_disable_unprepare(nandc->core_clk);
2660err_core_clk:
2661 qcom_nandc_unalloc(nandc);
2662
2663 return ret;
2664}
2665
2666static int qcom_nandc_remove(struct platform_device *pdev)
2667{
2668 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2669 struct qcom_nand_host *host;
2670
2671 list_for_each_entry(host, &nandc->host_list, node)
2672 nand_release(nand_to_mtd(&host->chip));
2673
2674 qcom_nandc_unalloc(nandc);
2675
2676 clk_disable_unprepare(nandc->aon_clk);
2677 clk_disable_unprepare(nandc->core_clk);
2678
2679 return 0;
2680}
2681
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302682static const struct qcom_nandc_props ipq806x_nandc_props = {
2683 .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +05302684 .is_bam = false,
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302685};
Archit Tanejac76b78d2016-02-03 14:29:50 +05302686
2687/*
2688 * data will hold a struct pointer containing more differences once we support
2689 * more controller variants
2690 */
2691static const struct of_device_id qcom_nandc_of_match[] = {
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302692 {
2693 .compatible = "qcom,ipq806x-nand",
2694 .data = &ipq806x_nandc_props,
Archit Tanejac76b78d2016-02-03 14:29:50 +05302695 },
2696 {}
2697};
2698MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
2699
2700static struct platform_driver qcom_nandc_driver = {
2701 .driver = {
2702 .name = "qcom-nandc",
2703 .of_match_table = qcom_nandc_of_match,
2704 },
2705 .probe = qcom_nandc_probe,
2706 .remove = qcom_nandc_remove,
2707};
2708module_platform_driver(qcom_nandc_driver);
2709
2710MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
2711MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
2712MODULE_LICENSE("GPL v2");