blob: 1ff5daf9d6190c458f9bba70f55c78fd04fedc68 [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
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530476/* Clears the BAM transaction indexes */
477static void clear_bam_transaction(struct qcom_nand_controller *nandc)
478{
479 struct bam_transaction *bam_txn = nandc->bam_txn;
480
481 if (!nandc->props->is_bam)
482 return;
483
484 bam_txn->cmd_sgl_pos = 0;
485 bam_txn->cmd_sgl_start = 0;
486 bam_txn->tx_sgl_pos = 0;
487 bam_txn->tx_sgl_start = 0;
488 bam_txn->rx_sgl_pos = 0;
489 bam_txn->rx_sgl_start = 0;
490
491 sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage *
492 QPIC_PER_CW_CMD_SGL);
493 sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage *
494 QPIC_PER_CW_DATA_SGL);
495}
496
Archit Tanejac76b78d2016-02-03 14:29:50 +0530497static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
498{
499 return container_of(chip, struct qcom_nand_host, chip);
500}
501
502static inline struct qcom_nand_controller *
503get_qcom_nand_controller(struct nand_chip *chip)
504{
505 return container_of(chip->controller, struct qcom_nand_controller,
506 controller);
507}
508
509static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
510{
511 return ioread32(nandc->base + offset);
512}
513
514static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
515 u32 val)
516{
517 iowrite32(val, nandc->base + offset);
518}
519
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530520static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
521 bool is_cpu)
522{
523 if (!nandc->props->is_bam)
524 return;
525
526 if (is_cpu)
527 dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
528 MAX_REG_RD *
529 sizeof(*nandc->reg_read_buf),
530 DMA_FROM_DEVICE);
531 else
532 dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
533 MAX_REG_RD *
534 sizeof(*nandc->reg_read_buf),
535 DMA_FROM_DEVICE);
536}
537
Archit Tanejac76b78d2016-02-03 14:29:50 +0530538static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
539{
540 switch (offset) {
541 case NAND_FLASH_CMD:
542 return &regs->cmd;
543 case NAND_ADDR0:
544 return &regs->addr0;
545 case NAND_ADDR1:
546 return &regs->addr1;
547 case NAND_FLASH_CHIP_SELECT:
548 return &regs->chip_sel;
549 case NAND_EXEC_CMD:
550 return &regs->exec;
551 case NAND_FLASH_STATUS:
552 return &regs->clrflashstatus;
553 case NAND_DEV0_CFG0:
554 return &regs->cfg0;
555 case NAND_DEV0_CFG1:
556 return &regs->cfg1;
557 case NAND_DEV0_ECC_CFG:
558 return &regs->ecc_bch_cfg;
559 case NAND_READ_STATUS:
560 return &regs->clrreadstatus;
561 case NAND_DEV_CMD1:
562 return &regs->cmd1;
563 case NAND_DEV_CMD1_RESTORE:
564 return &regs->orig_cmd1;
565 case NAND_DEV_CMD_VLD:
566 return &regs->vld;
567 case NAND_DEV_CMD_VLD_RESTORE:
568 return &regs->orig_vld;
569 case NAND_EBI2_ECC_BUF_CFG:
570 return &regs->ecc_buf_cfg;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530571 case NAND_READ_LOCATION_0:
572 return &regs->read_location0;
573 case NAND_READ_LOCATION_1:
574 return &regs->read_location1;
575 case NAND_READ_LOCATION_2:
576 return &regs->read_location2;
577 case NAND_READ_LOCATION_3:
578 return &regs->read_location3;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530579 default:
580 return NULL;
581 }
582}
583
584static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
585 u32 val)
586{
587 struct nandc_regs *regs = nandc->regs;
588 __le32 *reg;
589
590 reg = offset_to_nandc_reg(regs, offset);
591
592 if (reg)
593 *reg = cpu_to_le32(val);
594}
595
596/* helper to configure address register values */
597static void set_address(struct qcom_nand_host *host, u16 column, int page)
598{
599 struct nand_chip *chip = &host->chip;
600 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
601
602 if (chip->options & NAND_BUSWIDTH_16)
603 column >>= 1;
604
605 nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
606 nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
607}
608
609/*
610 * update_rw_regs: set up read/write register values, these will be
611 * written to the NAND controller registers via DMA
612 *
613 * @num_cw: number of steps for the read/write operation
614 * @read: read or write operation
615 */
616static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
617{
618 struct nand_chip *chip = &host->chip;
619 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
620 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
621
622 if (read) {
623 if (host->use_ecc)
624 cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
625 else
626 cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
627 } else {
628 cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
629 }
630
631 if (host->use_ecc) {
632 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
633 (num_cw - 1) << CW_PER_PAGE;
634
635 cfg1 = host->cfg1;
636 ecc_bch_cfg = host->ecc_bch_cfg;
637 } else {
638 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
639 (num_cw - 1) << CW_PER_PAGE;
640
641 cfg1 = host->cfg1_raw;
642 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
643 }
644
645 nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
646 nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
647 nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
648 nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
649 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
650 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
651 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
652 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530653
654 if (read)
655 nandc_set_read_loc(nandc, 0, 0, host->use_ecc ?
656 host->cw_data : host->cw_size, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530657}
658
Abhishek Sahu381dd242017-08-17 17:37:41 +0530659/*
660 * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
661 * for BAM. This descriptor will be added in the NAND DMA descriptor queue
662 * which will be submitted to DMA engine.
663 */
664static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
665 struct dma_chan *chan,
666 unsigned long flags)
667{
668 struct desc_info *desc;
669 struct scatterlist *sgl;
670 unsigned int sgl_cnt;
671 int ret;
672 struct bam_transaction *bam_txn = nandc->bam_txn;
673 enum dma_transfer_direction dir_eng;
674 struct dma_async_tx_descriptor *dma_desc;
675
676 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
677 if (!desc)
678 return -ENOMEM;
679
680 if (chan == nandc->cmd_chan) {
681 sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
682 sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
683 bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
684 dir_eng = DMA_MEM_TO_DEV;
685 desc->dir = DMA_TO_DEVICE;
686 } else if (chan == nandc->tx_chan) {
687 sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
688 sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
689 bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
690 dir_eng = DMA_MEM_TO_DEV;
691 desc->dir = DMA_TO_DEVICE;
692 } else {
693 sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
694 sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
695 bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
696 dir_eng = DMA_DEV_TO_MEM;
697 desc->dir = DMA_FROM_DEVICE;
698 }
699
700 sg_mark_end(sgl + sgl_cnt - 1);
701 ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
702 if (ret == 0) {
703 dev_err(nandc->dev, "failure in mapping desc\n");
704 kfree(desc);
705 return -ENOMEM;
706 }
707
708 desc->sgl_cnt = sgl_cnt;
709 desc->bam_sgl = sgl;
710
711 dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
712 flags);
713
714 if (!dma_desc) {
715 dev_err(nandc->dev, "failure in prep desc\n");
716 dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
717 kfree(desc);
718 return -EINVAL;
719 }
720
721 desc->dma_desc = dma_desc;
722
723 list_add_tail(&desc->node, &nandc->desc_list);
724
725 return 0;
726}
727
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530728/*
729 * Prepares the data descriptor for BAM DMA which will be used for NAND
730 * data reads and writes.
731 */
732static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read,
733 const void *vaddr,
734 int size, unsigned int flags)
735{
736 int ret;
737 struct bam_transaction *bam_txn = nandc->bam_txn;
738
739 if (read) {
740 sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos],
741 vaddr, size);
742 bam_txn->rx_sgl_pos++;
743 } else {
744 sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos],
745 vaddr, size);
746 bam_txn->tx_sgl_pos++;
747
748 /*
749 * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
750 * is not set, form the DMA descriptor
751 */
752 if (!(flags & NAND_BAM_NO_EOT)) {
753 ret = prepare_bam_async_desc(nandc, nandc->tx_chan,
754 DMA_PREP_INTERRUPT);
755 if (ret)
756 return ret;
757 }
758 }
759
760 return 0;
761}
762
Abhishek Sahu381dd242017-08-17 17:37:41 +0530763static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
764 int reg_off, const void *vaddr, int size,
765 bool flow_control)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530766{
767 struct desc_info *desc;
768 struct dma_async_tx_descriptor *dma_desc;
769 struct scatterlist *sgl;
770 struct dma_slave_config slave_conf;
771 enum dma_transfer_direction dir_eng;
772 int ret;
773
774 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
775 if (!desc)
776 return -ENOMEM;
777
Abhishek Sahu381dd242017-08-17 17:37:41 +0530778 sgl = &desc->adm_sgl;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530779
780 sg_init_one(sgl, vaddr, size);
781
782 if (read) {
783 dir_eng = DMA_DEV_TO_MEM;
784 desc->dir = DMA_FROM_DEVICE;
785 } else {
786 dir_eng = DMA_MEM_TO_DEV;
787 desc->dir = DMA_TO_DEVICE;
788 }
789
790 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
791 if (ret == 0) {
792 ret = -ENOMEM;
793 goto err;
794 }
795
796 memset(&slave_conf, 0x00, sizeof(slave_conf));
797
798 slave_conf.device_fc = flow_control;
799 if (read) {
800 slave_conf.src_maxburst = 16;
801 slave_conf.src_addr = nandc->base_dma + reg_off;
802 slave_conf.slave_id = nandc->data_crci;
803 } else {
804 slave_conf.dst_maxburst = 16;
805 slave_conf.dst_addr = nandc->base_dma + reg_off;
806 slave_conf.slave_id = nandc->cmd_crci;
807 }
808
809 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
810 if (ret) {
811 dev_err(nandc->dev, "failed to configure dma channel\n");
812 goto err;
813 }
814
815 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
816 if (!dma_desc) {
817 dev_err(nandc->dev, "failed to prepare desc\n");
818 ret = -EINVAL;
819 goto err;
820 }
821
822 desc->dma_desc = dma_desc;
823
824 list_add_tail(&desc->node, &nandc->desc_list);
825
826 return 0;
827err:
828 kfree(desc);
829
830 return ret;
831}
832
833/*
834 * read_reg_dma: prepares a descriptor to read a given number of
835 * contiguous registers to the reg_read_buf pointer
836 *
837 * @first: offset of the first register in the contiguous block
838 * @num_regs: number of registers to read
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530839 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530840 */
841static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530842 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530843{
844 bool flow_control = false;
845 void *vaddr;
846 int size;
847
848 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
849 flow_control = true;
850
851 size = num_regs * sizeof(u32);
852 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
853 nandc->reg_read_pos += num_regs;
854
Abhishek Sahu381dd242017-08-17 17:37:41 +0530855 return prep_adm_dma_desc(nandc, true, first, vaddr, size, flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530856}
857
858/*
859 * write_reg_dma: prepares a descriptor to write a given number of
860 * contiguous registers
861 *
862 * @first: offset of the first register in the contiguous block
863 * @num_regs: number of registers to write
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530864 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530865 */
866static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530867 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530868{
869 bool flow_control = false;
870 struct nandc_regs *regs = nandc->regs;
871 void *vaddr;
872 int size;
873
874 vaddr = offset_to_nandc_reg(regs, first);
875
876 if (first == NAND_FLASH_CMD)
877 flow_control = true;
878
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530879 if (first == NAND_ERASED_CW_DETECT_CFG) {
880 if (flags & NAND_ERASED_CW_SET)
881 vaddr = &regs->erased_cw_detect_cfg_set;
882 else
883 vaddr = &regs->erased_cw_detect_cfg_clr;
884 }
885
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530886 if (first == NAND_EXEC_CMD)
887 flags |= NAND_BAM_NWD;
888
Archit Tanejac76b78d2016-02-03 14:29:50 +0530889 if (first == NAND_DEV_CMD1_RESTORE)
890 first = NAND_DEV_CMD1;
891
892 if (first == NAND_DEV_CMD_VLD_RESTORE)
893 first = NAND_DEV_CMD_VLD;
894
895 size = num_regs * sizeof(u32);
896
Abhishek Sahu381dd242017-08-17 17:37:41 +0530897 return prep_adm_dma_desc(nandc, false, first, vaddr, size,
898 flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530899}
900
901/*
902 * read_data_dma: prepares a DMA descriptor to transfer data from the
903 * controller's internal buffer to the buffer 'vaddr'
904 *
905 * @reg_off: offset within the controller's data buffer
906 * @vaddr: virtual address of the buffer we want to write to
907 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530908 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530909 */
910static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530911 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530912{
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530913 if (nandc->props->is_bam)
914 return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags);
915
Abhishek Sahu381dd242017-08-17 17:37:41 +0530916 return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530917}
918
919/*
920 * write_data_dma: prepares a DMA descriptor to transfer data from
921 * 'vaddr' to the controller's internal buffer
922 *
923 * @reg_off: offset within the controller's data buffer
924 * @vaddr: virtual address of the buffer we want to read from
925 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530926 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530927 */
928static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530929 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530930{
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530931 if (nandc->props->is_bam)
932 return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags);
933
Abhishek Sahu381dd242017-08-17 17:37:41 +0530934 return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530935}
936
937/*
Abhishek Sahubde43302017-07-19 17:17:55 +0530938 * Helper to prepare DMA descriptors for configuring registers
939 * before reading a NAND page.
Archit Tanejac76b78d2016-02-03 14:29:50 +0530940 */
Abhishek Sahubde43302017-07-19 17:17:55 +0530941static void config_nand_page_read(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530942{
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530943 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
944 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
945 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530946 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
947 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
948 NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
Abhishek Sahubde43302017-07-19 17:17:55 +0530949}
Archit Tanejac76b78d2016-02-03 14:29:50 +0530950
Abhishek Sahubde43302017-07-19 17:17:55 +0530951/*
952 * Helper to prepare DMA descriptors for configuring registers
953 * before reading each codeword in NAND page.
954 */
955static void config_nand_cw_read(struct qcom_nand_controller *nandc)
956{
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530957 if (nandc->props->is_bam)
958 write_reg_dma(nandc, NAND_READ_LOCATION_0, 4,
959 NAND_BAM_NEXT_SGL);
960
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530961 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
962 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530963
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530964 read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
965 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
966 NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530967}
968
969/*
Abhishek Sahubde43302017-07-19 17:17:55 +0530970 * Helper to prepare dma descriptors to configure registers needed for reading a
971 * single codeword in page
Archit Tanejac76b78d2016-02-03 14:29:50 +0530972 */
Abhishek Sahubde43302017-07-19 17:17:55 +0530973static void config_nand_single_cw_page_read(struct qcom_nand_controller *nandc)
974{
975 config_nand_page_read(nandc);
976 config_nand_cw_read(nandc);
977}
978
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530979/*
980 * Helper to prepare DMA descriptors used to configure registers needed for
981 * before writing a NAND page.
982 */
983static void config_nand_page_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530984{
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530985 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
986 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
987 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
988 NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530989}
990
Abhishek Sahu77cc5362017-07-19 17:17:56 +0530991/*
992 * Helper to prepare DMA descriptors for configuring registers
993 * before writing each codeword in NAND page.
994 */
995static void config_nand_cw_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530996{
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530997 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
998 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530999
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301000 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301001
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301002 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1003 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301004}
1005
1006/*
1007 * the following functions are used within chip->cmdfunc() to perform different
1008 * NAND_CMD_* commands
1009 */
1010
1011/* sets up descriptors for NAND_CMD_PARAM */
1012static int nandc_param(struct qcom_nand_host *host)
1013{
1014 struct nand_chip *chip = &host->chip;
1015 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1016
1017 /*
1018 * NAND_CMD_PARAM is called before we know much about the FLASH chip
1019 * in use. we configure the controller to perform a raw read of 512
1020 * bytes to read onfi params
1021 */
1022 nandc_set_reg(nandc, NAND_FLASH_CMD, PAGE_READ | PAGE_ACC | LAST_PAGE);
1023 nandc_set_reg(nandc, NAND_ADDR0, 0);
1024 nandc_set_reg(nandc, NAND_ADDR1, 0);
1025 nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
1026 | 512 << UD_SIZE_BYTES
1027 | 5 << NUM_ADDR_CYCLES
1028 | 0 << SPARE_SIZE_BYTES);
1029 nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
1030 | 0 << CS_ACTIVE_BSY
1031 | 17 << BAD_BLOCK_BYTE_NUM
1032 | 1 << BAD_BLOCK_IN_SPARE_AREA
1033 | 2 << WR_RD_BSY_GAP
1034 | 0 << WIDE_FLASH
1035 | 1 << DEV0_CFG1_ECC_DISABLE);
1036 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
1037
1038 /* configure CMD1 and VLD for ONFI param probing */
1039 nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
Abhishek Sahud8a9b322017-08-11 17:09:16 +05301040 (nandc->vld & ~READ_START_VLD));
Archit Tanejac76b78d2016-02-03 14:29:50 +05301041 nandc_set_reg(nandc, NAND_DEV_CMD1,
1042 (nandc->cmd1 & ~(0xFF << READ_ADDR))
1043 | NAND_CMD_PARAM << READ_ADDR);
1044
1045 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1046
1047 nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
1048 nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301049 nandc_set_read_loc(nandc, 0, 0, 512, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301050
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301051 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
1052 write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301053
1054 nandc->buf_count = 512;
1055 memset(nandc->data_buffer, 0xff, nandc->buf_count);
1056
Abhishek Sahubde43302017-07-19 17:17:55 +05301057 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301058
1059 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301060 nandc->buf_count, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301061
1062 /* restore CMD1 and VLD regs */
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301063 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
1064 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301065
1066 return 0;
1067}
1068
1069/* sets up descriptors for NAND_CMD_ERASE1 */
1070static int erase_block(struct qcom_nand_host *host, int page_addr)
1071{
1072 struct nand_chip *chip = &host->chip;
1073 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1074
1075 nandc_set_reg(nandc, NAND_FLASH_CMD,
1076 BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
1077 nandc_set_reg(nandc, NAND_ADDR0, page_addr);
1078 nandc_set_reg(nandc, NAND_ADDR1, 0);
1079 nandc_set_reg(nandc, NAND_DEV0_CFG0,
1080 host->cfg0_raw & ~(7 << CW_PER_PAGE));
1081 nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
1082 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1083 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
1084 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
1085
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301086 write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
1087 write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
1088 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301089
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301090 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301091
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301092 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1093 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301094
1095 return 0;
1096}
1097
1098/* sets up descriptors for NAND_CMD_READID */
1099static int read_id(struct qcom_nand_host *host, int column)
1100{
1101 struct nand_chip *chip = &host->chip;
1102 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1103
1104 if (column == -1)
1105 return 0;
1106
1107 nandc_set_reg(nandc, NAND_FLASH_CMD, FETCH_ID);
1108 nandc_set_reg(nandc, NAND_ADDR0, column);
1109 nandc_set_reg(nandc, NAND_ADDR1, 0);
Abhishek Sahu9d43f912017-08-17 17:37:45 +05301110 nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT,
1111 nandc->props->is_bam ? 0 : DM_EN);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301112 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1113
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301114 write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
1115 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301116
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301117 read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301118
1119 return 0;
1120}
1121
1122/* sets up descriptors for NAND_CMD_RESET */
1123static int reset(struct qcom_nand_host *host)
1124{
1125 struct nand_chip *chip = &host->chip;
1126 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1127
1128 nandc_set_reg(nandc, NAND_FLASH_CMD, RESET_DEVICE);
1129 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1130
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301131 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1132 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301133
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301134 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301135
1136 return 0;
1137}
1138
1139/* helpers to submit/free our list of dma descriptors */
1140static int submit_descs(struct qcom_nand_controller *nandc)
1141{
1142 struct desc_info *desc;
1143 dma_cookie_t cookie = 0;
Abhishek Sahu381dd242017-08-17 17:37:41 +05301144 struct bam_transaction *bam_txn = nandc->bam_txn;
1145 int r;
1146
1147 if (nandc->props->is_bam) {
1148 if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
1149 r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
1150 if (r)
1151 return r;
1152 }
1153
1154 if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
1155 r = prepare_bam_async_desc(nandc, nandc->tx_chan,
1156 DMA_PREP_INTERRUPT);
1157 if (r)
1158 return r;
1159 }
1160
1161 if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
1162 r = prepare_bam_async_desc(nandc, nandc->cmd_chan, 0);
1163 if (r)
1164 return r;
1165 }
1166 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301167
1168 list_for_each_entry(desc, &nandc->desc_list, node)
1169 cookie = dmaengine_submit(desc->dma_desc);
1170
Abhishek Sahu381dd242017-08-17 17:37:41 +05301171 if (nandc->props->is_bam) {
1172 dma_async_issue_pending(nandc->tx_chan);
1173 dma_async_issue_pending(nandc->rx_chan);
1174
1175 if (dma_sync_wait(nandc->cmd_chan, cookie) != DMA_COMPLETE)
1176 return -ETIMEDOUT;
1177 } else {
1178 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
1179 return -ETIMEDOUT;
1180 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301181
1182 return 0;
1183}
1184
1185static void free_descs(struct qcom_nand_controller *nandc)
1186{
1187 struct desc_info *desc, *n;
1188
1189 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
1190 list_del(&desc->node);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301191
1192 if (nandc->props->is_bam)
1193 dma_unmap_sg(nandc->dev, desc->bam_sgl,
1194 desc->sgl_cnt, desc->dir);
1195 else
1196 dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
1197 desc->dir);
1198
Archit Tanejac76b78d2016-02-03 14:29:50 +05301199 kfree(desc);
1200 }
1201}
1202
1203/* reset the register read buffer for next NAND operation */
1204static void clear_read_regs(struct qcom_nand_controller *nandc)
1205{
1206 nandc->reg_read_pos = 0;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301207 nandc_read_buffer_sync(nandc, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301208}
1209
1210static void pre_command(struct qcom_nand_host *host, int command)
1211{
1212 struct nand_chip *chip = &host->chip;
1213 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1214
1215 nandc->buf_count = 0;
1216 nandc->buf_start = 0;
1217 host->use_ecc = false;
1218 host->last_command = command;
1219
1220 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301221
1222 if (command == NAND_CMD_RESET || command == NAND_CMD_READID ||
1223 command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1)
1224 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301225}
1226
1227/*
1228 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
1229 * privately maintained status byte, this status byte can be read after
1230 * NAND_CMD_STATUS is called
1231 */
1232static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
1233{
1234 struct nand_chip *chip = &host->chip;
1235 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1236 struct nand_ecc_ctrl *ecc = &chip->ecc;
1237 int num_cw;
1238 int i;
1239
1240 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301241 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301242
1243 for (i = 0; i < num_cw; i++) {
1244 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
1245
1246 if (flash_status & FS_MPU_ERR)
1247 host->status &= ~NAND_STATUS_WP;
1248
1249 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
1250 (flash_status &
1251 FS_DEVICE_STS_ERR)))
1252 host->status |= NAND_STATUS_FAIL;
1253 }
1254}
1255
1256static void post_command(struct qcom_nand_host *host, int command)
1257{
1258 struct nand_chip *chip = &host->chip;
1259 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1260
1261 switch (command) {
1262 case NAND_CMD_READID:
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301263 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301264 memcpy(nandc->data_buffer, nandc->reg_read_buf,
1265 nandc->buf_count);
1266 break;
1267 case NAND_CMD_PAGEPROG:
1268 case NAND_CMD_ERASE1:
1269 parse_erase_write_errors(host, command);
1270 break;
1271 default:
1272 break;
1273 }
1274}
1275
1276/*
1277 * Implements chip->cmdfunc. It's only used for a limited set of commands.
1278 * The rest of the commands wouldn't be called by upper layers. For example,
1279 * NAND_CMD_READOOB would never be called because we have our own versions
1280 * of read_oob ops for nand_ecc_ctrl.
1281 */
1282static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
1283 int column, int page_addr)
1284{
1285 struct nand_chip *chip = mtd_to_nand(mtd);
1286 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1287 struct nand_ecc_ctrl *ecc = &chip->ecc;
1288 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1289 bool wait = false;
1290 int ret = 0;
1291
1292 pre_command(host, command);
1293
1294 switch (command) {
1295 case NAND_CMD_RESET:
1296 ret = reset(host);
1297 wait = true;
1298 break;
1299
1300 case NAND_CMD_READID:
1301 nandc->buf_count = 4;
1302 ret = read_id(host, column);
1303 wait = true;
1304 break;
1305
1306 case NAND_CMD_PARAM:
1307 ret = nandc_param(host);
1308 wait = true;
1309 break;
1310
1311 case NAND_CMD_ERASE1:
1312 ret = erase_block(host, page_addr);
1313 wait = true;
1314 break;
1315
1316 case NAND_CMD_READ0:
1317 /* we read the entire page for now */
1318 WARN_ON(column != 0);
1319
1320 host->use_ecc = true;
1321 set_address(host, 0, page_addr);
1322 update_rw_regs(host, ecc->steps, true);
1323 break;
1324
1325 case NAND_CMD_SEQIN:
1326 WARN_ON(column != 0);
1327 set_address(host, 0, page_addr);
1328 break;
1329
1330 case NAND_CMD_PAGEPROG:
1331 case NAND_CMD_STATUS:
1332 case NAND_CMD_NONE:
1333 default:
1334 break;
1335 }
1336
1337 if (ret) {
1338 dev_err(nandc->dev, "failure executing command %d\n",
1339 command);
1340 free_descs(nandc);
1341 return;
1342 }
1343
1344 if (wait) {
1345 ret = submit_descs(nandc);
1346 if (ret)
1347 dev_err(nandc->dev,
1348 "failure submitting descs for command %d\n",
1349 command);
1350 }
1351
1352 free_descs(nandc);
1353
1354 post_command(host, command);
1355}
1356
1357/*
1358 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1359 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1360 *
1361 * when using RS ECC, the HW reports the same erros when reading an erased CW,
1362 * but it notifies that it is an erased CW by placing special characters at
1363 * certain offsets in the buffer.
1364 *
1365 * verify if the page is erased or not, and fix up the page for RS ECC by
1366 * replacing the special characters with 0xff.
1367 */
1368static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
1369{
1370 u8 empty1, empty2;
1371
1372 /*
1373 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1374 * is erased by looking for 0x54s at offsets 3 and 175 from the
1375 * beginning of each codeword
1376 */
1377
1378 empty1 = data_buf[3];
1379 empty2 = data_buf[175];
1380
1381 /*
1382 * if the erased codework markers, if they exist override them with
1383 * 0xffs
1384 */
1385 if ((empty1 == 0x54 && empty2 == 0xff) ||
1386 (empty1 == 0xff && empty2 == 0x54)) {
1387 data_buf[3] = 0xff;
1388 data_buf[175] = 0xff;
1389 }
1390
1391 /*
1392 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1393 * restore the original values at the special offsets
1394 */
1395 if (memchr_inv(data_buf, 0xff, data_len)) {
1396 data_buf[3] = empty1;
1397 data_buf[175] = empty2;
1398
1399 return false;
1400 }
1401
1402 return true;
1403}
1404
1405struct read_stats {
1406 __le32 flash;
1407 __le32 buffer;
1408 __le32 erased_cw;
1409};
1410
1411/*
1412 * reads back status registers set by the controller to notify page read
1413 * errors. this is equivalent to what 'ecc->correct()' would do.
1414 */
1415static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1416 u8 *oob_buf)
1417{
1418 struct nand_chip *chip = &host->chip;
1419 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1420 struct mtd_info *mtd = nand_to_mtd(chip);
1421 struct nand_ecc_ctrl *ecc = &chip->ecc;
1422 unsigned int max_bitflips = 0;
1423 struct read_stats *buf;
1424 int i;
1425
1426 buf = (struct read_stats *)nandc->reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301427 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301428
1429 for (i = 0; i < ecc->steps; i++, buf++) {
1430 u32 flash, buffer, erased_cw;
1431 int data_len, oob_len;
1432
1433 if (i == (ecc->steps - 1)) {
1434 data_len = ecc->size - ((ecc->steps - 1) << 2);
1435 oob_len = ecc->steps << 2;
1436 } else {
1437 data_len = host->cw_data;
1438 oob_len = 0;
1439 }
1440
1441 flash = le32_to_cpu(buf->flash);
1442 buffer = le32_to_cpu(buf->buffer);
1443 erased_cw = le32_to_cpu(buf->erased_cw);
1444
1445 if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1446 bool erased;
1447
1448 /* ignore erased codeword errors */
1449 if (host->bch_enabled) {
1450 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1451 true : false;
1452 } else {
1453 erased = erased_chunk_check_and_fixup(data_buf,
1454 data_len);
1455 }
1456
1457 if (erased) {
1458 data_buf += data_len;
1459 if (oob_buf)
1460 oob_buf += oob_len + ecc->bytes;
1461 continue;
1462 }
1463
1464 if (buffer & BS_UNCORRECTABLE_BIT) {
1465 int ret, ecclen, extraooblen;
1466 void *eccbuf;
1467
1468 eccbuf = oob_buf ? oob_buf + oob_len : NULL;
1469 ecclen = oob_buf ? host->ecc_bytes_hw : 0;
1470 extraooblen = oob_buf ? oob_len : 0;
1471
1472 /*
1473 * make sure it isn't an erased page reported
1474 * as not-erased by HW because of a few bitflips
1475 */
1476 ret = nand_check_erased_ecc_chunk(data_buf,
1477 data_len, eccbuf, ecclen, oob_buf,
1478 extraooblen, ecc->strength);
1479 if (ret < 0) {
1480 mtd->ecc_stats.failed++;
1481 } else {
1482 mtd->ecc_stats.corrected += ret;
1483 max_bitflips =
1484 max_t(unsigned int, max_bitflips, ret);
1485 }
1486 }
1487 } else {
1488 unsigned int stat;
1489
1490 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1491 mtd->ecc_stats.corrected += stat;
1492 max_bitflips = max(max_bitflips, stat);
1493 }
1494
1495 data_buf += data_len;
1496 if (oob_buf)
1497 oob_buf += oob_len + ecc->bytes;
1498 }
1499
1500 return max_bitflips;
1501}
1502
1503/*
1504 * helper to perform the actual page read operation, used by ecc->read_page(),
1505 * ecc->read_oob()
1506 */
1507static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1508 u8 *oob_buf)
1509{
1510 struct nand_chip *chip = &host->chip;
1511 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1512 struct nand_ecc_ctrl *ecc = &chip->ecc;
1513 int i, ret;
1514
Abhishek Sahubde43302017-07-19 17:17:55 +05301515 config_nand_page_read(nandc);
1516
Archit Tanejac76b78d2016-02-03 14:29:50 +05301517 /* queue cmd descs for each codeword */
1518 for (i = 0; i < ecc->steps; i++) {
1519 int data_size, oob_size;
1520
1521 if (i == (ecc->steps - 1)) {
1522 data_size = ecc->size - ((ecc->steps - 1) << 2);
1523 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1524 host->spare_bytes;
1525 } else {
1526 data_size = host->cw_data;
1527 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1528 }
1529
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301530 if (nandc->props->is_bam) {
1531 if (data_buf && oob_buf) {
1532 nandc_set_read_loc(nandc, 0, 0, data_size, 0);
1533 nandc_set_read_loc(nandc, 1, data_size,
1534 oob_size, 1);
1535 } else if (data_buf) {
1536 nandc_set_read_loc(nandc, 0, 0, data_size, 1);
1537 } else {
1538 nandc_set_read_loc(nandc, 0, data_size,
1539 oob_size, 1);
1540 }
1541 }
1542
Abhishek Sahubde43302017-07-19 17:17:55 +05301543 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301544
1545 if (data_buf)
1546 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301547 data_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301548
1549 /*
1550 * when ecc is enabled, the controller doesn't read the real
1551 * or dummy bad block markers in each chunk. To maintain a
1552 * consistent layout across RAW and ECC reads, we just
1553 * leave the real/dummy BBM offsets empty (i.e, filled with
1554 * 0xffs)
1555 */
1556 if (oob_buf) {
1557 int j;
1558
1559 for (j = 0; j < host->bbm_size; j++)
1560 *oob_buf++ = 0xff;
1561
1562 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301563 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301564 }
1565
1566 if (data_buf)
1567 data_buf += data_size;
1568 if (oob_buf)
1569 oob_buf += oob_size;
1570 }
1571
1572 ret = submit_descs(nandc);
1573 if (ret)
1574 dev_err(nandc->dev, "failure to read page/oob\n");
1575
1576 free_descs(nandc);
1577
1578 return ret;
1579}
1580
1581/*
1582 * a helper that copies the last step/codeword of a page (containing free oob)
1583 * into our local buffer
1584 */
1585static int copy_last_cw(struct qcom_nand_host *host, int page)
1586{
1587 struct nand_chip *chip = &host->chip;
1588 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1589 struct nand_ecc_ctrl *ecc = &chip->ecc;
1590 int size;
1591 int ret;
1592
1593 clear_read_regs(nandc);
1594
1595 size = host->use_ecc ? host->cw_data : host->cw_size;
1596
1597 /* prepare a clean read buffer */
1598 memset(nandc->data_buffer, 0xff, size);
1599
1600 set_address(host, host->cw_size * (ecc->steps - 1), page);
1601 update_rw_regs(host, 1, true);
1602
Abhishek Sahubde43302017-07-19 17:17:55 +05301603 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301604
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301605 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301606
1607 ret = submit_descs(nandc);
1608 if (ret)
1609 dev_err(nandc->dev, "failed to copy last codeword\n");
1610
1611 free_descs(nandc);
1612
1613 return ret;
1614}
1615
1616/* implements ecc->read_page() */
1617static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1618 uint8_t *buf, int oob_required, int page)
1619{
1620 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1621 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1622 u8 *data_buf, *oob_buf = NULL;
1623 int ret;
1624
1625 data_buf = buf;
1626 oob_buf = oob_required ? chip->oob_poi : NULL;
1627
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301628 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301629 ret = read_page_ecc(host, data_buf, oob_buf);
1630 if (ret) {
1631 dev_err(nandc->dev, "failure to read page\n");
1632 return ret;
1633 }
1634
1635 return parse_read_errors(host, data_buf, oob_buf);
1636}
1637
1638/* implements ecc->read_page_raw() */
1639static int qcom_nandc_read_page_raw(struct mtd_info *mtd,
1640 struct nand_chip *chip, uint8_t *buf,
1641 int oob_required, int page)
1642{
1643 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1644 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1645 u8 *data_buf, *oob_buf;
1646 struct nand_ecc_ctrl *ecc = &chip->ecc;
1647 int i, ret;
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301648 int read_loc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301649
1650 data_buf = buf;
1651 oob_buf = chip->oob_poi;
1652
1653 host->use_ecc = false;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301654
1655 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301656 update_rw_regs(host, ecc->steps, true);
Abhishek Sahubde43302017-07-19 17:17:55 +05301657 config_nand_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301658
1659 for (i = 0; i < ecc->steps; i++) {
1660 int data_size1, data_size2, oob_size1, oob_size2;
1661 int reg_off = FLASH_BUF_ACC;
1662
1663 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1664 oob_size1 = host->bbm_size;
1665
1666 if (i == (ecc->steps - 1)) {
1667 data_size2 = ecc->size - data_size1 -
1668 ((ecc->steps - 1) << 2);
1669 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1670 host->spare_bytes;
1671 } else {
1672 data_size2 = host->cw_data - data_size1;
1673 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1674 }
1675
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301676 if (nandc->props->is_bam) {
1677 read_loc = 0;
1678 nandc_set_read_loc(nandc, 0, read_loc, data_size1, 0);
1679 read_loc += data_size1;
1680
1681 nandc_set_read_loc(nandc, 1, read_loc, oob_size1, 0);
1682 read_loc += oob_size1;
1683
1684 nandc_set_read_loc(nandc, 2, read_loc, data_size2, 0);
1685 read_loc += data_size2;
1686
1687 nandc_set_read_loc(nandc, 3, read_loc, oob_size2, 1);
1688 }
1689
Abhishek Sahubde43302017-07-19 17:17:55 +05301690 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301691
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301692 read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301693 reg_off += data_size1;
1694 data_buf += data_size1;
1695
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301696 read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301697 reg_off += oob_size1;
1698 oob_buf += oob_size1;
1699
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301700 read_data_dma(nandc, reg_off, data_buf, data_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301701 reg_off += data_size2;
1702 data_buf += data_size2;
1703
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301704 read_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301705 oob_buf += oob_size2;
1706 }
1707
1708 ret = submit_descs(nandc);
1709 if (ret)
1710 dev_err(nandc->dev, "failure to read raw page\n");
1711
1712 free_descs(nandc);
1713
1714 return 0;
1715}
1716
1717/* implements ecc->read_oob() */
1718static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1719 int page)
1720{
1721 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1722 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1723 struct nand_ecc_ctrl *ecc = &chip->ecc;
1724 int ret;
1725
1726 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301727 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301728
1729 host->use_ecc = true;
1730 set_address(host, 0, page);
1731 update_rw_regs(host, ecc->steps, true);
1732
1733 ret = read_page_ecc(host, NULL, chip->oob_poi);
1734 if (ret)
1735 dev_err(nandc->dev, "failure to read oob\n");
1736
1737 return ret;
1738}
1739
1740/* implements ecc->write_page() */
1741static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1742 const uint8_t *buf, int oob_required, int page)
1743{
1744 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1745 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1746 struct nand_ecc_ctrl *ecc = &chip->ecc;
1747 u8 *data_buf, *oob_buf;
1748 int i, ret;
1749
1750 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301751 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301752
1753 data_buf = (u8 *)buf;
1754 oob_buf = chip->oob_poi;
1755
1756 host->use_ecc = true;
1757 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301758 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301759
1760 for (i = 0; i < ecc->steps; i++) {
1761 int data_size, oob_size;
1762
1763 if (i == (ecc->steps - 1)) {
1764 data_size = ecc->size - ((ecc->steps - 1) << 2);
1765 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1766 host->spare_bytes;
1767 } else {
1768 data_size = host->cw_data;
1769 oob_size = ecc->bytes;
1770 }
1771
Archit Tanejac76b78d2016-02-03 14:29:50 +05301772
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301773 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
1774 i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301775
1776 /*
1777 * when ECC is enabled, we don't really need to write anything
1778 * to oob for the first n - 1 codewords since these oob regions
1779 * just contain ECC bytes that's written by the controller
1780 * itself. For the last codeword, we skip the bbm positions and
1781 * write to the free oob area.
1782 */
1783 if (i == (ecc->steps - 1)) {
1784 oob_buf += host->bbm_size;
1785
1786 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301787 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301788 }
1789
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301790 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301791
1792 data_buf += data_size;
1793 oob_buf += oob_size;
1794 }
1795
1796 ret = submit_descs(nandc);
1797 if (ret)
1798 dev_err(nandc->dev, "failure to write page\n");
1799
1800 free_descs(nandc);
1801
1802 return ret;
1803}
1804
1805/* implements ecc->write_page_raw() */
1806static int qcom_nandc_write_page_raw(struct mtd_info *mtd,
1807 struct nand_chip *chip, const uint8_t *buf,
1808 int oob_required, int page)
1809{
1810 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1811 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1812 struct nand_ecc_ctrl *ecc = &chip->ecc;
1813 u8 *data_buf, *oob_buf;
1814 int i, ret;
1815
1816 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301817 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301818
1819 data_buf = (u8 *)buf;
1820 oob_buf = chip->oob_poi;
1821
1822 host->use_ecc = false;
1823 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301824 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301825
1826 for (i = 0; i < ecc->steps; i++) {
1827 int data_size1, data_size2, oob_size1, oob_size2;
1828 int reg_off = FLASH_BUF_ACC;
1829
1830 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1831 oob_size1 = host->bbm_size;
1832
1833 if (i == (ecc->steps - 1)) {
1834 data_size2 = ecc->size - data_size1 -
1835 ((ecc->steps - 1) << 2);
1836 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1837 host->spare_bytes;
1838 } else {
1839 data_size2 = host->cw_data - data_size1;
1840 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1841 }
1842
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301843 write_data_dma(nandc, reg_off, data_buf, data_size1,
1844 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301845 reg_off += data_size1;
1846 data_buf += data_size1;
1847
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301848 write_data_dma(nandc, reg_off, oob_buf, oob_size1,
1849 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301850 reg_off += oob_size1;
1851 oob_buf += oob_size1;
1852
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301853 write_data_dma(nandc, reg_off, data_buf, data_size2,
1854 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301855 reg_off += data_size2;
1856 data_buf += data_size2;
1857
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301858 write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301859 oob_buf += oob_size2;
1860
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301861 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301862 }
1863
1864 ret = submit_descs(nandc);
1865 if (ret)
1866 dev_err(nandc->dev, "failure to write raw page\n");
1867
1868 free_descs(nandc);
1869
1870 return ret;
1871}
1872
1873/*
1874 * implements ecc->write_oob()
1875 *
1876 * the NAND controller cannot write only data or only oob within a codeword,
1877 * since ecc is calculated for the combined codeword. we first copy the
1878 * entire contents for the last codeword(data + oob), replace the old oob
1879 * with the new one in chip->oob_poi, and then write the entire codeword.
1880 * this read-copy-write operation results in a slight performance loss.
1881 */
1882static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1883 int page)
1884{
1885 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1886 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1887 struct nand_ecc_ctrl *ecc = &chip->ecc;
1888 u8 *oob = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301889 int data_size, oob_size;
1890 int ret, status = 0;
1891
1892 host->use_ecc = true;
1893
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301894 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301895 ret = copy_last_cw(host, page);
1896 if (ret)
1897 return ret;
1898
1899 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301900 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301901
1902 /* calculate the data and oob size for the last codeword/step */
1903 data_size = ecc->size - ((ecc->steps - 1) << 2);
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001904 oob_size = mtd->oobavail;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301905
1906 /* override new oob content to last codeword */
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001907 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
1908 0, mtd->oobavail);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301909
1910 set_address(host, host->cw_size * (ecc->steps - 1), page);
1911 update_rw_regs(host, 1, false);
1912
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301913 config_nand_page_write(nandc);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301914 write_data_dma(nandc, FLASH_BUF_ACC,
1915 nandc->data_buffer, data_size + oob_size, 0);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301916 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301917
1918 ret = submit_descs(nandc);
1919
1920 free_descs(nandc);
1921
1922 if (ret) {
1923 dev_err(nandc->dev, "failure to write oob\n");
1924 return -EIO;
1925 }
1926
1927 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1928
1929 status = chip->waitfunc(mtd, chip);
1930
1931 return status & NAND_STATUS_FAIL ? -EIO : 0;
1932}
1933
1934static int qcom_nandc_block_bad(struct mtd_info *mtd, loff_t ofs)
1935{
1936 struct nand_chip *chip = mtd_to_nand(mtd);
1937 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1938 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1939 struct nand_ecc_ctrl *ecc = &chip->ecc;
1940 int page, ret, bbpos, bad = 0;
1941 u32 flash_status;
1942
1943 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1944
1945 /*
1946 * configure registers for a raw sub page read, the address is set to
1947 * the beginning of the last codeword, we don't care about reading ecc
1948 * portion of oob. we just want the first few bytes from this codeword
1949 * that contains the BBM
1950 */
1951 host->use_ecc = false;
1952
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301953 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301954 ret = copy_last_cw(host, page);
1955 if (ret)
1956 goto err;
1957
1958 flash_status = le32_to_cpu(nandc->reg_read_buf[0]);
1959
1960 if (flash_status & (FS_OP_ERR | FS_MPU_ERR)) {
1961 dev_warn(nandc->dev, "error when trying to read BBM\n");
1962 goto err;
1963 }
1964
1965 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
1966
1967 bad = nandc->data_buffer[bbpos] != 0xff;
1968
1969 if (chip->options & NAND_BUSWIDTH_16)
1970 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
1971err:
1972 return bad;
1973}
1974
1975static int qcom_nandc_block_markbad(struct mtd_info *mtd, loff_t ofs)
1976{
1977 struct nand_chip *chip = mtd_to_nand(mtd);
1978 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1979 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1980 struct nand_ecc_ctrl *ecc = &chip->ecc;
1981 int page, ret, status = 0;
1982
1983 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301984 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301985
1986 /*
1987 * to mark the BBM as bad, we flash the entire last codeword with 0s.
1988 * we don't care about the rest of the content in the codeword since
1989 * we aren't going to use this block again
1990 */
1991 memset(nandc->data_buffer, 0x00, host->cw_size);
1992
1993 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1994
1995 /* prepare write */
1996 host->use_ecc = false;
1997 set_address(host, host->cw_size * (ecc->steps - 1), page);
1998 update_rw_regs(host, 1, false);
1999
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302000 config_nand_page_write(nandc);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302001 write_data_dma(nandc, FLASH_BUF_ACC,
2002 nandc->data_buffer, host->cw_size, 0);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302003 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302004
2005 ret = submit_descs(nandc);
2006
2007 free_descs(nandc);
2008
2009 if (ret) {
2010 dev_err(nandc->dev, "failure to update BBM\n");
2011 return -EIO;
2012 }
2013
2014 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2015
2016 status = chip->waitfunc(mtd, chip);
2017
2018 return status & NAND_STATUS_FAIL ? -EIO : 0;
2019}
2020
2021/*
2022 * the three functions below implement chip->read_byte(), chip->read_buf()
2023 * and chip->write_buf() respectively. these aren't used for
2024 * reading/writing page data, they are used for smaller data like reading
2025 * id, status etc
2026 */
2027static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
2028{
2029 struct nand_chip *chip = mtd_to_nand(mtd);
2030 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2031 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2032 u8 *buf = nandc->data_buffer;
2033 u8 ret = 0x0;
2034
2035 if (host->last_command == NAND_CMD_STATUS) {
2036 ret = host->status;
2037
2038 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2039
2040 return ret;
2041 }
2042
2043 if (nandc->buf_start < nandc->buf_count)
2044 ret = buf[nandc->buf_start++];
2045
2046 return ret;
2047}
2048
2049static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
2050{
2051 struct nand_chip *chip = mtd_to_nand(mtd);
2052 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2053 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2054
2055 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
2056 nandc->buf_start += real_len;
2057}
2058
2059static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
2060 int len)
2061{
2062 struct nand_chip *chip = mtd_to_nand(mtd);
2063 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2064 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2065
2066 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
2067
2068 nandc->buf_start += real_len;
2069}
2070
2071/* we support only one external chip for now */
2072static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
2073{
2074 struct nand_chip *chip = mtd_to_nand(mtd);
2075 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2076
2077 if (chipnr <= 0)
2078 return;
2079
2080 dev_warn(nandc->dev, "invalid chip select\n");
2081}
2082
2083/*
2084 * NAND controller page layout info
2085 *
2086 * Layout with ECC enabled:
2087 *
2088 * |----------------------| |---------------------------------|
2089 * | xx.......yy| | *********xx.......yy|
2090 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
2091 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
2092 * | xx.......yy| | *********xx.......yy|
2093 * |----------------------| |---------------------------------|
2094 * codeword 1,2..n-1 codeword n
2095 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
2096 *
2097 * n = Number of codewords in the page
2098 * . = ECC bytes
2099 * * = Spare/free bytes
2100 * x = Unused byte(s)
2101 * y = Reserved byte(s)
2102 *
2103 * 2K page: n = 4, spare = 16 bytes
2104 * 4K page: n = 8, spare = 32 bytes
2105 * 8K page: n = 16, spare = 64 bytes
2106 *
2107 * the qcom nand controller operates at a sub page/codeword level. each
2108 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2109 * the number of ECC bytes vary based on the ECC strength and the bus width.
2110 *
2111 * the first n - 1 codewords contains 516 bytes of user data, the remaining
2112 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2113 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2114 *
2115 * When we access a page with ECC enabled, the reserved bytes(s) are not
2116 * accessible at all. When reading, we fill up these unreadable positions
2117 * with 0xffs. When writing, the controller skips writing the inaccessible
2118 * bytes.
2119 *
2120 * Layout with ECC disabled:
2121 *
2122 * |------------------------------| |---------------------------------------|
2123 * | yy xx.......| | bb *********xx.......|
2124 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
2125 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
2126 * | yy xx.......| | bb *********xx.......|
2127 * |------------------------------| |---------------------------------------|
2128 * codeword 1,2..n-1 codeword n
2129 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
2130 *
2131 * n = Number of codewords in the page
2132 * . = ECC bytes
2133 * * = Spare/free bytes
2134 * x = Unused byte(s)
2135 * y = Dummy Bad Bock byte(s)
2136 * b = Real Bad Block byte(s)
2137 * size1/size2 = function of codeword size and 'n'
2138 *
2139 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2140 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2141 * Block Markers. In the last codeword, this position contains the real BBM
2142 *
2143 * In order to have a consistent layout between RAW and ECC modes, we assume
2144 * the following OOB layout arrangement:
2145 *
2146 * |-----------| |--------------------|
2147 * |yyxx.......| |bb*********xx.......|
2148 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
2149 * |yyxx.......| |bb*********xx.......|
2150 * |yyxx.......| |bb*********xx.......|
2151 * |-----------| |--------------------|
2152 * first n - 1 nth OOB region
2153 * OOB regions
2154 *
2155 * n = Number of codewords in the page
2156 * . = ECC bytes
2157 * * = FREE OOB bytes
2158 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2159 * x = Unused byte(s)
2160 * b = Real bad block byte(s) (inaccessible when ECC enabled)
2161 *
2162 * This layout is read as is when ECC is disabled. When ECC is enabled, the
2163 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2164 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
Boris Brezillon421e81c2016-03-18 17:54:27 +01002165 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2166 * the sum of the three).
Archit Tanejac76b78d2016-02-03 14:29:50 +05302167 */
Boris Brezillon421e81c2016-03-18 17:54:27 +01002168static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2169 struct mtd_oob_region *oobregion)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302170{
Boris Brezillon421e81c2016-03-18 17:54:27 +01002171 struct nand_chip *chip = mtd_to_nand(mtd);
2172 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302173 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302174
Boris Brezillon421e81c2016-03-18 17:54:27 +01002175 if (section > 1)
2176 return -ERANGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302177
Boris Brezillon421e81c2016-03-18 17:54:27 +01002178 if (!section) {
2179 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
2180 host->bbm_size;
2181 oobregion->offset = 0;
2182 } else {
2183 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
2184 oobregion->offset = mtd->oobsize - oobregion->length;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302185 }
2186
Boris Brezillon421e81c2016-03-18 17:54:27 +01002187 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302188}
2189
Boris Brezillon421e81c2016-03-18 17:54:27 +01002190static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
2191 struct mtd_oob_region *oobregion)
2192{
2193 struct nand_chip *chip = mtd_to_nand(mtd);
2194 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2195 struct nand_ecc_ctrl *ecc = &chip->ecc;
2196
2197 if (section)
2198 return -ERANGE;
2199
2200 oobregion->length = ecc->steps * 4;
2201 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
2202
2203 return 0;
2204}
2205
2206static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
2207 .ecc = qcom_nand_ooblayout_ecc,
2208 .free = qcom_nand_ooblayout_free,
2209};
2210
Archit Tanejac76b78d2016-02-03 14:29:50 +05302211static int qcom_nand_host_setup(struct qcom_nand_host *host)
2212{
2213 struct nand_chip *chip = &host->chip;
2214 struct mtd_info *mtd = nand_to_mtd(chip);
2215 struct nand_ecc_ctrl *ecc = &chip->ecc;
2216 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2217 int cwperpage, bad_block_byte;
2218 bool wide_bus;
2219 int ecc_mode = 1;
2220
2221 /*
2222 * the controller requires each step consists of 512 bytes of data.
2223 * bail out if DT has populated a wrong step size.
2224 */
2225 if (ecc->size != NANDC_STEP_SIZE) {
2226 dev_err(nandc->dev, "invalid ecc size\n");
2227 return -EINVAL;
2228 }
2229
2230 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
2231
2232 if (ecc->strength >= 8) {
2233 /* 8 bit ECC defaults to BCH ECC on all platforms */
2234 host->bch_enabled = true;
2235 ecc_mode = 1;
2236
2237 if (wide_bus) {
2238 host->ecc_bytes_hw = 14;
2239 host->spare_bytes = 0;
2240 host->bbm_size = 2;
2241 } else {
2242 host->ecc_bytes_hw = 13;
2243 host->spare_bytes = 2;
2244 host->bbm_size = 1;
2245 }
2246 } else {
2247 /*
2248 * if the controller supports BCH for 4 bit ECC, the controller
2249 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2250 * always 10 bytes
2251 */
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302252 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302253 /* BCH */
2254 host->bch_enabled = true;
2255 ecc_mode = 0;
2256
2257 if (wide_bus) {
2258 host->ecc_bytes_hw = 8;
2259 host->spare_bytes = 2;
2260 host->bbm_size = 2;
2261 } else {
2262 host->ecc_bytes_hw = 7;
2263 host->spare_bytes = 4;
2264 host->bbm_size = 1;
2265 }
2266 } else {
2267 /* RS */
2268 host->ecc_bytes_hw = 10;
2269
2270 if (wide_bus) {
2271 host->spare_bytes = 0;
2272 host->bbm_size = 2;
2273 } else {
2274 host->spare_bytes = 1;
2275 host->bbm_size = 1;
2276 }
2277 }
2278 }
2279
2280 /*
2281 * we consider ecc->bytes as the sum of all the non-data content in a
2282 * step. It gives us a clean representation of the oob area (even if
2283 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2284 * ECC and 12 bytes for 4 bit ECC
2285 */
2286 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
2287
2288 ecc->read_page = qcom_nandc_read_page;
2289 ecc->read_page_raw = qcom_nandc_read_page_raw;
2290 ecc->read_oob = qcom_nandc_read_oob;
2291 ecc->write_page = qcom_nandc_write_page;
2292 ecc->write_page_raw = qcom_nandc_write_page_raw;
2293 ecc->write_oob = qcom_nandc_write_oob;
2294
2295 ecc->mode = NAND_ECC_HW;
2296
Boris Brezillon421e81c2016-03-18 17:54:27 +01002297 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302298
2299 cwperpage = mtd->writesize / ecc->size;
Abhishek Sahucb80f112017-08-17 17:37:40 +05302300 nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
2301 cwperpage);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302302
2303 /*
2304 * DATA_UD_BYTES varies based on whether the read/write command protects
2305 * spare data with ECC too. We protect spare data by default, so we set
2306 * it to main + spare data, which are 512 and 4 bytes respectively.
2307 */
2308 host->cw_data = 516;
2309
2310 /*
2311 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2312 * for 8 bit ECC
2313 */
2314 host->cw_size = host->cw_data + ecc->bytes;
2315
2316 if (ecc->bytes * (mtd->writesize / ecc->size) > mtd->oobsize) {
2317 dev_err(nandc->dev, "ecc data doesn't fit in OOB area\n");
2318 return -EINVAL;
2319 }
2320
2321 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
2322
2323 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
2324 | host->cw_data << UD_SIZE_BYTES
2325 | 0 << DISABLE_STATUS_AFTER_WRITE
2326 | 5 << NUM_ADDR_CYCLES
2327 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
2328 | 0 << STATUS_BFR_READ
2329 | 1 << SET_RD_MODE_AFTER_STATUS
2330 | host->spare_bytes << SPARE_SIZE_BYTES;
2331
2332 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
2333 | 0 << CS_ACTIVE_BSY
2334 | bad_block_byte << BAD_BLOCK_BYTE_NUM
2335 | 0 << BAD_BLOCK_IN_SPARE_AREA
2336 | 2 << WR_RD_BSY_GAP
2337 | wide_bus << WIDE_FLASH
2338 | host->bch_enabled << ENABLE_BCH_ECC;
2339
2340 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
2341 | host->cw_size << UD_SIZE_BYTES
2342 | 5 << NUM_ADDR_CYCLES
2343 | 0 << SPARE_SIZE_BYTES;
2344
2345 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
2346 | 0 << CS_ACTIVE_BSY
2347 | 17 << BAD_BLOCK_BYTE_NUM
2348 | 1 << BAD_BLOCK_IN_SPARE_AREA
2349 | 2 << WR_RD_BSY_GAP
2350 | wide_bus << WIDE_FLASH
2351 | 1 << DEV0_CFG1_ECC_DISABLE;
2352
Abhishek Sahu10777de2017-08-03 17:56:39 +02002353 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
Archit Tanejac76b78d2016-02-03 14:29:50 +05302354 | 0 << ECC_SW_RESET
2355 | host->cw_data << ECC_NUM_DATA_BYTES
2356 | 1 << ECC_FORCE_CLK_OPEN
2357 | ecc_mode << ECC_MODE
2358 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
2359
2360 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
2361
2362 host->clrflashstatus = FS_READY_BSY_N;
2363 host->clrreadstatus = 0xc0;
Abhishek Sahua86b9c42017-08-17 17:37:44 +05302364 nandc->regs->erased_cw_detect_cfg_clr =
2365 cpu_to_le32(CLR_ERASED_PAGE_DET);
2366 nandc->regs->erased_cw_detect_cfg_set =
2367 cpu_to_le32(SET_ERASED_PAGE_DET);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302368
2369 dev_dbg(nandc->dev,
2370 "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",
2371 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
2372 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
2373 cwperpage);
2374
2375 return 0;
2376}
2377
2378static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
2379{
2380 int ret;
2381
2382 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
2383 if (ret) {
2384 dev_err(nandc->dev, "failed to set DMA mask\n");
2385 return ret;
2386 }
2387
2388 /*
2389 * we use the internal buffer for reading ONFI params, reading small
2390 * data like ID and status, and preforming read-copy-write operations
2391 * when writing to a codeword partially. 532 is the maximum possible
2392 * size of a codeword for our nand controller
2393 */
2394 nandc->buf_size = 532;
2395
2396 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
2397 GFP_KERNEL);
2398 if (!nandc->data_buffer)
2399 return -ENOMEM;
2400
2401 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
2402 GFP_KERNEL);
2403 if (!nandc->regs)
2404 return -ENOMEM;
2405
2406 nandc->reg_read_buf = devm_kzalloc(nandc->dev,
2407 MAX_REG_RD * sizeof(*nandc->reg_read_buf),
2408 GFP_KERNEL);
2409 if (!nandc->reg_read_buf)
2410 return -ENOMEM;
2411
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302412 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302413 nandc->reg_read_dma =
2414 dma_map_single(nandc->dev, nandc->reg_read_buf,
2415 MAX_REG_RD *
2416 sizeof(*nandc->reg_read_buf),
2417 DMA_FROM_DEVICE);
2418 if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
2419 dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
2420 return -EIO;
2421 }
2422
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302423 nandc->tx_chan = dma_request_slave_channel(nandc->dev, "tx");
2424 if (!nandc->tx_chan) {
2425 dev_err(nandc->dev, "failed to request tx channel\n");
2426 return -ENODEV;
2427 }
2428
2429 nandc->rx_chan = dma_request_slave_channel(nandc->dev, "rx");
2430 if (!nandc->rx_chan) {
2431 dev_err(nandc->dev, "failed to request rx channel\n");
2432 return -ENODEV;
2433 }
2434
2435 nandc->cmd_chan = dma_request_slave_channel(nandc->dev, "cmd");
2436 if (!nandc->cmd_chan) {
2437 dev_err(nandc->dev, "failed to request cmd channel\n");
2438 return -ENODEV;
2439 }
Abhishek Sahucb80f112017-08-17 17:37:40 +05302440
2441 /*
2442 * Initially allocate BAM transaction to read ONFI param page.
2443 * After detecting all the devices, this BAM transaction will
2444 * be freed and the next BAM tranasction will be allocated with
2445 * maximum codeword size
2446 */
2447 nandc->max_cwperpage = 1;
2448 nandc->bam_txn = alloc_bam_transaction(nandc);
2449 if (!nandc->bam_txn) {
2450 dev_err(nandc->dev,
2451 "failed to allocate bam transaction\n");
2452 return -ENOMEM;
2453 }
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302454 } else {
2455 nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
2456 if (!nandc->chan) {
2457 dev_err(nandc->dev,
2458 "failed to request slave channel\n");
2459 return -ENODEV;
2460 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302461 }
2462
2463 INIT_LIST_HEAD(&nandc->desc_list);
2464 INIT_LIST_HEAD(&nandc->host_list);
2465
Marc Gonzalezd45bc582016-07-27 11:23:52 +02002466 nand_hw_control_init(&nandc->controller);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302467
2468 return 0;
2469}
2470
2471static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2472{
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302473 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302474 if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
2475 dma_unmap_single(nandc->dev, nandc->reg_read_dma,
2476 MAX_REG_RD *
2477 sizeof(*nandc->reg_read_buf),
2478 DMA_FROM_DEVICE);
2479
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302480 if (nandc->tx_chan)
2481 dma_release_channel(nandc->tx_chan);
2482
2483 if (nandc->rx_chan)
2484 dma_release_channel(nandc->rx_chan);
2485
2486 if (nandc->cmd_chan)
2487 dma_release_channel(nandc->cmd_chan);
2488 } else {
2489 if (nandc->chan)
2490 dma_release_channel(nandc->chan);
2491 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302492}
2493
2494/* one time setup of a few nand controller registers */
2495static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2496{
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302497 u32 nand_ctrl;
2498
Archit Tanejac76b78d2016-02-03 14:29:50 +05302499 /* kill onenand */
2500 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
Abhishek Sahud8a9b322017-08-11 17:09:16 +05302501 nandc_write(nandc, NAND_DEV_CMD_VLD, NAND_DEV_CMD_VLD_VAL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302502
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302503 /* enable ADM or BAM DMA */
2504 if (nandc->props->is_bam) {
2505 nand_ctrl = nandc_read(nandc, NAND_CTRL);
2506 nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
2507 } else {
2508 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2509 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302510
2511 /* save the original values of these registers */
2512 nandc->cmd1 = nandc_read(nandc, NAND_DEV_CMD1);
Abhishek Sahud8a9b322017-08-11 17:09:16 +05302513 nandc->vld = NAND_DEV_CMD_VLD_VAL;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302514
2515 return 0;
2516}
2517
2518static int qcom_nand_host_init(struct qcom_nand_controller *nandc,
2519 struct qcom_nand_host *host,
2520 struct device_node *dn)
2521{
2522 struct nand_chip *chip = &host->chip;
2523 struct mtd_info *mtd = nand_to_mtd(chip);
2524 struct device *dev = nandc->dev;
2525 int ret;
2526
2527 ret = of_property_read_u32(dn, "reg", &host->cs);
2528 if (ret) {
2529 dev_err(dev, "can't get chip-select\n");
2530 return -ENXIO;
2531 }
2532
2533 nand_set_flash_node(chip, dn);
2534 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
2535 mtd->owner = THIS_MODULE;
2536 mtd->dev.parent = dev;
2537
2538 chip->cmdfunc = qcom_nandc_command;
2539 chip->select_chip = qcom_nandc_select_chip;
2540 chip->read_byte = qcom_nandc_read_byte;
2541 chip->read_buf = qcom_nandc_read_buf;
2542 chip->write_buf = qcom_nandc_write_buf;
Boris Brezillon4a78cc62017-05-26 17:10:15 +02002543 chip->onfi_set_features = nand_onfi_get_set_features_notsupp;
2544 chip->onfi_get_features = nand_onfi_get_set_features_notsupp;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302545
2546 /*
2547 * the bad block marker is readable only when we read the last codeword
2548 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2549 * helpers don't allow us to read BB from a nand chip with ECC
2550 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2551 * and block_markbad helpers until we permanently switch to using
2552 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2553 */
2554 chip->block_bad = qcom_nandc_block_bad;
2555 chip->block_markbad = qcom_nandc_block_markbad;
2556
2557 chip->controller = &nandc->controller;
2558 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
2559 NAND_SKIP_BBTSCAN;
2560
2561 /* set up initial status value */
2562 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2563
2564 ret = nand_scan_ident(mtd, 1, NULL);
2565 if (ret)
2566 return ret;
2567
2568 ret = qcom_nand_host_setup(host);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302569
2570 return ret;
2571}
2572
2573static int qcom_nand_mtd_register(struct qcom_nand_controller *nandc,
2574 struct qcom_nand_host *host,
2575 struct device_node *dn)
2576{
2577 struct nand_chip *chip = &host->chip;
2578 struct mtd_info *mtd = nand_to_mtd(chip);
2579 int ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302580
2581 ret = nand_scan_tail(mtd);
2582 if (ret)
2583 return ret;
2584
Abhishek Sahu89f51272017-07-19 17:17:58 +05302585 ret = mtd_device_register(mtd, NULL, 0);
2586 if (ret)
2587 nand_cleanup(mtd_to_nand(mtd));
2588
2589 return ret;
2590}
2591
2592static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2593{
2594 struct device *dev = nandc->dev;
2595 struct device_node *dn = dev->of_node, *child;
2596 struct qcom_nand_host *host, *tmp;
2597 int ret;
2598
2599 for_each_available_child_of_node(dn, child) {
2600 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2601 if (!host) {
2602 of_node_put(child);
2603 return -ENOMEM;
2604 }
2605
2606 ret = qcom_nand_host_init(nandc, host, child);
2607 if (ret) {
2608 devm_kfree(dev, host);
2609 continue;
2610 }
2611
2612 list_add_tail(&host->node, &nandc->host_list);
2613 }
2614
2615 if (list_empty(&nandc->host_list))
2616 return -ENODEV;
2617
Abhishek Sahucb80f112017-08-17 17:37:40 +05302618 if (nandc->props->is_bam) {
2619 free_bam_transaction(nandc);
2620 nandc->bam_txn = alloc_bam_transaction(nandc);
2621 if (!nandc->bam_txn) {
2622 dev_err(nandc->dev,
2623 "failed to allocate bam transaction\n");
2624 return -ENOMEM;
2625 }
2626 }
2627
Abhishek Sahu89f51272017-07-19 17:17:58 +05302628 list_for_each_entry_safe(host, tmp, &nandc->host_list, node) {
2629 ret = qcom_nand_mtd_register(nandc, host, child);
2630 if (ret) {
2631 list_del(&host->node);
2632 devm_kfree(dev, host);
2633 }
2634 }
2635
2636 if (list_empty(&nandc->host_list))
2637 return -ENODEV;
2638
2639 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302640}
2641
2642/* parse custom DT properties here */
2643static int qcom_nandc_parse_dt(struct platform_device *pdev)
2644{
2645 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2646 struct device_node *np = nandc->dev->of_node;
2647 int ret;
2648
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302649 if (!nandc->props->is_bam) {
2650 ret = of_property_read_u32(np, "qcom,cmd-crci",
2651 &nandc->cmd_crci);
2652 if (ret) {
2653 dev_err(nandc->dev, "command CRCI unspecified\n");
2654 return ret;
2655 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302656
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302657 ret = of_property_read_u32(np, "qcom,data-crci",
2658 &nandc->data_crci);
2659 if (ret) {
2660 dev_err(nandc->dev, "data CRCI unspecified\n");
2661 return ret;
2662 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302663 }
2664
2665 return 0;
2666}
2667
2668static int qcom_nandc_probe(struct platform_device *pdev)
2669{
2670 struct qcom_nand_controller *nandc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302671 const void *dev_data;
2672 struct device *dev = &pdev->dev;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302673 struct resource *res;
2674 int ret;
2675
2676 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2677 if (!nandc)
2678 return -ENOMEM;
2679
2680 platform_set_drvdata(pdev, nandc);
2681 nandc->dev = dev;
2682
2683 dev_data = of_device_get_match_data(dev);
2684 if (!dev_data) {
2685 dev_err(&pdev->dev, "failed to get device data\n");
2686 return -ENODEV;
2687 }
2688
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302689 nandc->props = dev_data;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302690
2691 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2692 nandc->base = devm_ioremap_resource(dev, res);
2693 if (IS_ERR(nandc->base))
2694 return PTR_ERR(nandc->base);
2695
2696 nandc->base_dma = phys_to_dma(dev, (phys_addr_t)res->start);
2697
2698 nandc->core_clk = devm_clk_get(dev, "core");
2699 if (IS_ERR(nandc->core_clk))
2700 return PTR_ERR(nandc->core_clk);
2701
2702 nandc->aon_clk = devm_clk_get(dev, "aon");
2703 if (IS_ERR(nandc->aon_clk))
2704 return PTR_ERR(nandc->aon_clk);
2705
2706 ret = qcom_nandc_parse_dt(pdev);
2707 if (ret)
2708 return ret;
2709
2710 ret = qcom_nandc_alloc(nandc);
2711 if (ret)
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302712 goto err_core_clk;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302713
2714 ret = clk_prepare_enable(nandc->core_clk);
2715 if (ret)
2716 goto err_core_clk;
2717
2718 ret = clk_prepare_enable(nandc->aon_clk);
2719 if (ret)
2720 goto err_aon_clk;
2721
2722 ret = qcom_nandc_setup(nandc);
2723 if (ret)
2724 goto err_setup;
2725
Abhishek Sahu89f51272017-07-19 17:17:58 +05302726 ret = qcom_probe_nand_devices(nandc);
2727 if (ret)
2728 goto err_setup;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302729
2730 return 0;
2731
Archit Tanejac76b78d2016-02-03 14:29:50 +05302732err_setup:
2733 clk_disable_unprepare(nandc->aon_clk);
2734err_aon_clk:
2735 clk_disable_unprepare(nandc->core_clk);
2736err_core_clk:
2737 qcom_nandc_unalloc(nandc);
2738
2739 return ret;
2740}
2741
2742static int qcom_nandc_remove(struct platform_device *pdev)
2743{
2744 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2745 struct qcom_nand_host *host;
2746
2747 list_for_each_entry(host, &nandc->host_list, node)
2748 nand_release(nand_to_mtd(&host->chip));
2749
2750 qcom_nandc_unalloc(nandc);
2751
2752 clk_disable_unprepare(nandc->aon_clk);
2753 clk_disable_unprepare(nandc->core_clk);
2754
2755 return 0;
2756}
2757
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302758static const struct qcom_nandc_props ipq806x_nandc_props = {
2759 .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +05302760 .is_bam = false,
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302761};
Archit Tanejac76b78d2016-02-03 14:29:50 +05302762
2763/*
2764 * data will hold a struct pointer containing more differences once we support
2765 * more controller variants
2766 */
2767static const struct of_device_id qcom_nandc_of_match[] = {
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302768 {
2769 .compatible = "qcom,ipq806x-nand",
2770 .data = &ipq806x_nandc_props,
Archit Tanejac76b78d2016-02-03 14:29:50 +05302771 },
2772 {}
2773};
2774MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
2775
2776static struct platform_driver qcom_nandc_driver = {
2777 .driver = {
2778 .name = "qcom-nandc",
2779 .of_match_table = qcom_nandc_of_match,
2780 },
2781 .probe = qcom_nandc_probe,
2782 .remove = qcom_nandc_remove,
2783};
2784module_platform_driver(qcom_nandc_driver);
2785
2786MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
2787MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
2788MODULE_LICENSE("GPL v2");