blob: 04e6f7b26706482883fb3fe681200febc23fc92e [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Archit Tanejac76b78d2016-02-03 14:29:50 +05302/*
3 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
Archit Tanejac76b78d2016-02-03 14:29:50 +05304 */
5
6#include <linux/clk.h>
7#include <linux/slab.h>
8#include <linux/bitops.h>
9#include <linux/dma-mapping.h>
10#include <linux/dmaengine.h>
11#include <linux/module.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020012#include <linux/mtd/rawnand.h>
Archit Tanejac76b78d2016-02-03 14:29:50 +053013#include <linux/mtd/partitions.h>
14#include <linux/of.h>
15#include <linux/of_device.h>
Archit Tanejac76b78d2016-02-03 14:29:50 +053016#include <linux/delay.h>
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +053017#include <linux/dma/qcom_bam_dma.h>
Archit Tanejac76b78d2016-02-03 14:29:50 +053018
19/* NANDc reg offsets */
20#define NAND_FLASH_CMD 0x00
21#define NAND_ADDR0 0x04
22#define NAND_ADDR1 0x08
23#define NAND_FLASH_CHIP_SELECT 0x0c
24#define NAND_EXEC_CMD 0x10
25#define NAND_FLASH_STATUS 0x14
26#define NAND_BUFFER_STATUS 0x18
27#define NAND_DEV0_CFG0 0x20
28#define NAND_DEV0_CFG1 0x24
29#define NAND_DEV0_ECC_CFG 0x28
Md Sadre Alam06464932021-01-31 01:37:16 +053030#define NAND_AUTO_STATUS_EN 0x2c
Archit Tanejac76b78d2016-02-03 14:29:50 +053031#define NAND_DEV1_CFG0 0x30
32#define NAND_DEV1_CFG1 0x34
33#define NAND_READ_ID 0x40
34#define NAND_READ_STATUS 0x44
35#define NAND_DEV_CMD0 0xa0
36#define NAND_DEV_CMD1 0xa4
37#define NAND_DEV_CMD2 0xa8
38#define NAND_DEV_CMD_VLD 0xac
39#define SFLASHC_BURST_CFG 0xe0
40#define NAND_ERASED_CW_DETECT_CFG 0xe8
41#define NAND_ERASED_CW_DETECT_STATUS 0xec
42#define NAND_EBI2_ECC_BUF_CFG 0xf0
43#define FLASH_BUF_ACC 0x100
44
45#define NAND_CTRL 0xf00
46#define NAND_VERSION 0xf08
47#define NAND_READ_LOCATION_0 0xf20
48#define NAND_READ_LOCATION_1 0xf24
Abhishek Sahu91af95c2017-08-17 17:37:43 +053049#define NAND_READ_LOCATION_2 0xf28
50#define NAND_READ_LOCATION_3 0xf2c
Md Sadre Alam503ee5a2021-02-24 01:09:01 +053051#define NAND_READ_LOCATION_LAST_CW_0 0xf40
52#define NAND_READ_LOCATION_LAST_CW_1 0xf44
53#define NAND_READ_LOCATION_LAST_CW_2 0xf48
54#define NAND_READ_LOCATION_LAST_CW_3 0xf4c
Archit Tanejac76b78d2016-02-03 14:29:50 +053055
56/* dummy register offsets, used by write_reg_dma */
57#define NAND_DEV_CMD1_RESTORE 0xdead
58#define NAND_DEV_CMD_VLD_RESTORE 0xbeef
59
60/* NAND_FLASH_CMD bits */
61#define PAGE_ACC BIT(4)
62#define LAST_PAGE BIT(5)
63
64/* NAND_FLASH_CHIP_SELECT bits */
65#define NAND_DEV_SEL 0
66#define DM_EN BIT(2)
67
68/* NAND_FLASH_STATUS bits */
69#define FS_OP_ERR BIT(4)
70#define FS_READY_BSY_N BIT(5)
71#define FS_MPU_ERR BIT(8)
72#define FS_DEVICE_STS_ERR BIT(16)
73#define FS_DEVICE_WP BIT(23)
74
75/* NAND_BUFFER_STATUS bits */
76#define BS_UNCORRECTABLE_BIT BIT(8)
77#define BS_CORRECTABLE_ERR_MSK 0x1f
78
79/* NAND_DEVn_CFG0 bits */
80#define DISABLE_STATUS_AFTER_WRITE 4
81#define CW_PER_PAGE 6
82#define UD_SIZE_BYTES 9
83#define ECC_PARITY_SIZE_BYTES_RS 19
84#define SPARE_SIZE_BYTES 23
85#define NUM_ADDR_CYCLES 27
86#define STATUS_BFR_READ 30
87#define SET_RD_MODE_AFTER_STATUS 31
88
89/* NAND_DEVn_CFG0 bits */
90#define DEV0_CFG1_ECC_DISABLE 0
91#define WIDE_FLASH 1
92#define NAND_RECOVERY_CYCLES 2
93#define CS_ACTIVE_BSY 5
94#define BAD_BLOCK_BYTE_NUM 6
95#define BAD_BLOCK_IN_SPARE_AREA 16
96#define WR_RD_BSY_GAP 17
97#define ENABLE_BCH_ECC 27
98
99/* NAND_DEV0_ECC_CFG bits */
100#define ECC_CFG_ECC_DISABLE 0
101#define ECC_SW_RESET 1
102#define ECC_MODE 4
103#define ECC_PARITY_SIZE_BYTES_BCH 8
104#define ECC_NUM_DATA_BYTES 16
105#define ECC_FORCE_CLK_OPEN 30
106
107/* NAND_DEV_CMD1 bits */
108#define READ_ADDR 0
109
110/* NAND_DEV_CMD_VLD bits */
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530111#define READ_START_VLD BIT(0)
112#define READ_STOP_VLD BIT(1)
113#define WRITE_START_VLD BIT(2)
114#define ERASE_START_VLD BIT(3)
115#define SEQ_READ_START_VLD BIT(4)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530116
117/* NAND_EBI2_ECC_BUF_CFG bits */
118#define NUM_STEPS 0
119
120/* NAND_ERASED_CW_DETECT_CFG bits */
121#define ERASED_CW_ECC_MASK 1
122#define AUTO_DETECT_RES 0
123#define MASK_ECC (1 << ERASED_CW_ECC_MASK)
124#define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
125#define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
126#define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
127#define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
128
129/* NAND_ERASED_CW_DETECT_STATUS bits */
130#define PAGE_ALL_ERASED BIT(7)
131#define CODEWORD_ALL_ERASED BIT(6)
132#define PAGE_ERASED BIT(5)
133#define CODEWORD_ERASED BIT(4)
134#define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
135#define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
136
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530137/* NAND_READ_LOCATION_n bits */
138#define READ_LOCATION_OFFSET 0
139#define READ_LOCATION_SIZE 16
140#define READ_LOCATION_LAST 31
141
Archit Tanejac76b78d2016-02-03 14:29:50 +0530142/* Version Mask */
143#define NAND_VERSION_MAJOR_MASK 0xf0000000
144#define NAND_VERSION_MAJOR_SHIFT 28
145#define NAND_VERSION_MINOR_MASK 0x0fff0000
146#define NAND_VERSION_MINOR_SHIFT 16
147
148/* NAND OP_CMDs */
Olof Johansson33bf5512018-11-16 19:43:27 -0800149#define OP_PAGE_READ 0x2
150#define OP_PAGE_READ_WITH_ECC 0x3
151#define OP_PAGE_READ_WITH_ECC_SPARE 0x4
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +0530152#define OP_PAGE_READ_ONFI_READ 0x5
Olof Johansson33bf5512018-11-16 19:43:27 -0800153#define OP_PROGRAM_PAGE 0x6
154#define OP_PAGE_PROGRAM_WITH_ECC 0x7
155#define OP_PROGRAM_PAGE_SPARE 0x9
156#define OP_BLOCK_ERASE 0xa
157#define OP_FETCH_ID 0xb
158#define OP_RESET_DEVICE 0xd
Archit Tanejac76b78d2016-02-03 14:29:50 +0530159
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530160/* Default Value for NAND_DEV_CMD_VLD */
161#define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \
162 ERASE_START_VLD | SEQ_READ_START_VLD)
163
Abhishek Sahu9d43f912017-08-17 17:37:45 +0530164/* NAND_CTRL bits */
165#define BAM_MODE_EN BIT(0)
166
Archit Tanejac76b78d2016-02-03 14:29:50 +0530167/*
168 * the NAND controller performs reads/writes with ECC in 516 byte chunks.
169 * the driver calls the chunks 'step' or 'codeword' interchangeably
170 */
171#define NANDC_STEP_SIZE 512
172
173/*
174 * the largest page size we support is 8K, this will have 16 steps/codewords
175 * of 512 bytes each
176 */
177#define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
178
179/* we read at most 3 registers per codeword scan */
180#define MAX_REG_RD (3 * MAX_NUM_STEPS)
181
182/* ECC modes supported by the controller */
183#define ECC_NONE BIT(0)
184#define ECC_RS_4BIT BIT(1)
185#define ECC_BCH_4BIT BIT(2)
186#define ECC_BCH_8BIT BIT(3)
187
Md Sadre Alame7a307f2021-02-24 01:09:00 +0530188#define nandc_set_read_loc_first(chip, reg, cw_offset, read_size, is_last_read_loc) \
189nandc_set_reg(chip, reg, \
Md Sadre Alam622d3fc2021-02-24 01:08:59 +0530190 ((cw_offset) << READ_LOCATION_OFFSET) | \
191 ((read_size) << READ_LOCATION_SIZE) | \
192 ((is_last_read_loc) << READ_LOCATION_LAST))
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530193
Md Sadre Alam503ee5a2021-02-24 01:09:01 +0530194#define nandc_set_read_loc_last(chip, reg, cw_offset, read_size, is_last_read_loc) \
195nandc_set_reg(chip, reg, \
196 ((cw_offset) << READ_LOCATION_OFFSET) | \
197 ((read_size) << READ_LOCATION_SIZE) | \
198 ((is_last_read_loc) << READ_LOCATION_LAST))
Abhishek Sahucc409b92017-08-17 17:37:47 +0530199/*
200 * Returns the actual register address for all NAND_DEV_ registers
201 * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD)
202 */
203#define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg))
204
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530205/* Returns the NAND register physical address */
206#define nandc_reg_phys(chip, offset) ((chip)->base_phys + (offset))
207
208/* Returns the dma address for reg read buffer */
209#define reg_buf_dma_addr(chip, vaddr) \
210 ((chip)->reg_read_dma + \
211 ((uint8_t *)(vaddr) - (uint8_t *)(chip)->reg_read_buf))
212
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530213#define QPIC_PER_CW_CMD_ELEMENTS 32
Abhishek Sahucb80f112017-08-17 17:37:40 +0530214#define QPIC_PER_CW_CMD_SGL 32
215#define QPIC_PER_CW_DATA_SGL 8
216
Abhishek Sahu6f200702018-06-20 12:57:33 +0530217#define QPIC_NAND_COMPLETION_TIMEOUT msecs_to_jiffies(2000)
218
Abhishek Sahucb80f112017-08-17 17:37:40 +0530219/*
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530220 * Flags used in DMA descriptor preparation helper functions
221 * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
222 */
223/* Don't set the EOT in current tx BAM sgl */
224#define NAND_BAM_NO_EOT BIT(0)
225/* Set the NWD flag in current BAM sgl */
226#define NAND_BAM_NWD BIT(1)
227/* Finish writing in the current BAM sgl and start writing in another BAM sgl */
228#define NAND_BAM_NEXT_SGL BIT(2)
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530229/*
230 * Erased codeword status is being used two times in single transfer so this
231 * flag will determine the current value of erased codeword status register
232 */
233#define NAND_ERASED_CW_SET BIT(4)
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530234
235/*
Abhishek Sahucb80f112017-08-17 17:37:40 +0530236 * This data type corresponds to the BAM transaction which will be used for all
237 * NAND transfers.
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530238 * @bam_ce - the array of BAM command elements
Abhishek Sahucb80f112017-08-17 17:37:40 +0530239 * @cmd_sgl - sgl for NAND BAM command pipe
240 * @data_sgl - sgl for NAND BAM consumer/producer pipe
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530241 * @bam_ce_pos - the index in bam_ce which is available for next sgl
242 * @bam_ce_start - the index in bam_ce which marks the start position ce
243 * for current sgl. It will be used for size calculation
244 * for current sgl
Abhishek Sahucb80f112017-08-17 17:37:40 +0530245 * @cmd_sgl_pos - current index in command sgl.
246 * @cmd_sgl_start - start index in command sgl.
247 * @tx_sgl_pos - current index in data sgl for tx.
248 * @tx_sgl_start - start index in data sgl for tx.
249 * @rx_sgl_pos - current index in data sgl for rx.
250 * @rx_sgl_start - start index in data sgl for rx.
Abhishek Sahu6f200702018-06-20 12:57:33 +0530251 * @wait_second_completion - wait for second DMA desc completion before making
252 * the NAND transfer completion.
253 * @txn_done - completion for NAND transfer.
254 * @last_data_desc - last DMA desc in data channel (tx/rx).
255 * @last_cmd_desc - last DMA desc in command channel.
Abhishek Sahucb80f112017-08-17 17:37:40 +0530256 */
257struct bam_transaction {
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530258 struct bam_cmd_element *bam_ce;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530259 struct scatterlist *cmd_sgl;
260 struct scatterlist *data_sgl;
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530261 u32 bam_ce_pos;
262 u32 bam_ce_start;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530263 u32 cmd_sgl_pos;
264 u32 cmd_sgl_start;
265 u32 tx_sgl_pos;
266 u32 tx_sgl_start;
267 u32 rx_sgl_pos;
268 u32 rx_sgl_start;
Abhishek Sahu6f200702018-06-20 12:57:33 +0530269 bool wait_second_completion;
270 struct completion txn_done;
271 struct dma_async_tx_descriptor *last_data_desc;
272 struct dma_async_tx_descriptor *last_cmd_desc;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530273};
274
Abhishek Sahu381dd242017-08-17 17:37:41 +0530275/*
276 * This data type corresponds to the nand dma descriptor
277 * @list - list for desc_info
278 * @dir - DMA transfer direction
279 * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
280 * ADM
281 * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
282 * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
283 * @dma_desc - low level DMA engine descriptor
284 */
Archit Tanejac76b78d2016-02-03 14:29:50 +0530285struct desc_info {
286 struct list_head node;
287
288 enum dma_data_direction dir;
Abhishek Sahu381dd242017-08-17 17:37:41 +0530289 union {
290 struct scatterlist adm_sgl;
291 struct {
292 struct scatterlist *bam_sgl;
293 int sgl_cnt;
294 };
295 };
Archit Tanejac76b78d2016-02-03 14:29:50 +0530296 struct dma_async_tx_descriptor *dma_desc;
297};
298
299/*
300 * holds the current register values that we want to write. acts as a contiguous
301 * chunk of memory which we use to write the controller registers through DMA.
302 */
303struct nandc_regs {
304 __le32 cmd;
305 __le32 addr0;
306 __le32 addr1;
307 __le32 chip_sel;
308 __le32 exec;
309
310 __le32 cfg0;
311 __le32 cfg1;
312 __le32 ecc_bch_cfg;
313
314 __le32 clrflashstatus;
315 __le32 clrreadstatus;
316
317 __le32 cmd1;
318 __le32 vld;
319
320 __le32 orig_cmd1;
321 __le32 orig_vld;
322
323 __le32 ecc_buf_cfg;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530324 __le32 read_location0;
325 __le32 read_location1;
326 __le32 read_location2;
327 __le32 read_location3;
Md Sadre Alam503ee5a2021-02-24 01:09:01 +0530328 __le32 read_location_last0;
329 __le32 read_location_last1;
330 __le32 read_location_last2;
331 __le32 read_location_last3;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530332
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530333 __le32 erased_cw_detect_cfg_clr;
334 __le32 erased_cw_detect_cfg_set;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530335};
336
337/*
338 * NAND controller data struct
339 *
340 * @controller: base controller structure
341 * @host_list: list containing all the chips attached to the
342 * controller
343 * @dev: parent device
344 * @base: MMIO base
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530345 * @base_phys: physical base address of controller registers
346 * @base_dma: dma base address of controller registers
Archit Tanejac76b78d2016-02-03 14:29:50 +0530347 * @core_clk: controller clock
348 * @aon_clk: another controller clock
349 *
350 * @chan: dma channel
351 * @cmd_crci: ADM DMA CRCI for command flow control
352 * @data_crci: ADM DMA CRCI for data flow control
353 * @desc_list: DMA descriptor list (list of desc_infos)
354 *
355 * @data_buffer: our local DMA buffer for page read/writes,
356 * used when we can't use the buffer provided
357 * by upper layers directly
Boris Brezillon716bbba2018-09-07 00:38:35 +0200358 * @buf_size/count/start: markers for chip->legacy.read_buf/write_buf
359 * functions
Archit Tanejac76b78d2016-02-03 14:29:50 +0530360 * @reg_read_buf: local buffer for reading back registers via DMA
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530361 * @reg_read_dma: contains dma address for register read buffer
Archit Tanejac76b78d2016-02-03 14:29:50 +0530362 * @reg_read_pos: marker for data read in reg_read_buf
363 *
364 * @regs: a contiguous chunk of memory for DMA register
365 * writes. contains the register values to be
366 * written to controller
367 * @cmd1/vld: some fixed controller register values
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530368 * @props: properties of current NAND controller,
Archit Tanejac76b78d2016-02-03 14:29:50 +0530369 * initialized via DT match data
Abhishek Sahucb80f112017-08-17 17:37:40 +0530370 * @max_cwperpage: maximum QPIC codewords required. calculated
371 * from all connected NAND devices pagesize
Archit Tanejac76b78d2016-02-03 14:29:50 +0530372 */
373struct qcom_nand_controller {
Miquel Raynal7da45132018-07-17 09:08:02 +0200374 struct nand_controller controller;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530375 struct list_head host_list;
376
377 struct device *dev;
378
379 void __iomem *base;
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530380 phys_addr_t base_phys;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530381 dma_addr_t base_dma;
382
383 struct clk *core_clk;
384 struct clk *aon_clk;
385
Abhishek Sahu497d7d82017-08-11 17:09:19 +0530386 union {
387 /* will be used only by QPIC for BAM DMA */
388 struct {
389 struct dma_chan *tx_chan;
390 struct dma_chan *rx_chan;
391 struct dma_chan *cmd_chan;
392 };
393
394 /* will be used only by EBI2 for ADM DMA */
395 struct {
396 struct dma_chan *chan;
397 unsigned int cmd_crci;
398 unsigned int data_crci;
399 };
400 };
401
Archit Tanejac76b78d2016-02-03 14:29:50 +0530402 struct list_head desc_list;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530403 struct bam_transaction *bam_txn;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530404
405 u8 *data_buffer;
406 int buf_size;
407 int buf_count;
408 int buf_start;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530409 unsigned int max_cwperpage;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530410
411 __le32 *reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530412 dma_addr_t reg_read_dma;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530413 int reg_read_pos;
414
415 struct nandc_regs *regs;
416
417 u32 cmd1, vld;
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530418 const struct qcom_nandc_props *props;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530419};
420
421/*
422 * NAND chip structure
423 *
424 * @chip: base NAND chip structure
425 * @node: list node to add itself to host_list in
426 * qcom_nand_controller
427 *
428 * @cs: chip select value for this chip
429 * @cw_size: the number of bytes in a single step/codeword
430 * of a page, consisting of all data, ecc, spare
431 * and reserved bytes
432 * @cw_data: the number of bytes within a codeword protected
433 * by ECC
434 * @use_ecc: request the controller to use ECC for the
435 * upcoming read/write
436 * @bch_enabled: flag to tell whether BCH ECC mode is used
437 * @ecc_bytes_hw: ECC bytes used by controller hardware for this
438 * chip
439 * @status: value to be returned if NAND_CMD_STATUS command
440 * is executed
441 * @last_command: keeps track of last command on this chip. used
442 * for reading correct status
443 *
444 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
445 * ecc/non-ecc mode for the current nand flash
446 * device
447 */
448struct qcom_nand_host {
449 struct nand_chip chip;
450 struct list_head node;
451
452 int cs;
453 int cw_size;
454 int cw_data;
455 bool use_ecc;
456 bool bch_enabled;
457 int ecc_bytes_hw;
458 int spare_bytes;
459 int bbm_size;
460 u8 status;
461 int last_command;
462
463 u32 cfg0, cfg1;
464 u32 cfg0_raw, cfg1_raw;
465 u32 ecc_buf_cfg;
466 u32 ecc_bch_cfg;
467 u32 clrflashstatus;
468 u32 clrreadstatus;
469};
470
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530471/*
472 * This data type corresponds to the NAND controller properties which varies
473 * among different NAND controllers.
474 * @ecc_modes - ecc mode for NAND
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +0530475 * @is_bam - whether NAND controller is using BAM
Sivaprakash Murugesan443440c2020-06-12 13:28:15 +0530476 * @is_qpic - whether NAND CTRL is part of qpic IP
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +0530477 * @qpic_v2 - flag to indicate QPIC IP version 2
Abhishek Sahucc409b92017-08-17 17:37:47 +0530478 * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530479 */
480struct qcom_nandc_props {
481 u32 ecc_modes;
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +0530482 bool is_bam;
Sivaprakash Murugesan443440c2020-06-12 13:28:15 +0530483 bool is_qpic;
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +0530484 bool qpic_v2;
Abhishek Sahucc409b92017-08-17 17:37:47 +0530485 u32 dev_cmd_reg_start;
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530486};
487
Abhishek Sahucb80f112017-08-17 17:37:40 +0530488/* Frees the BAM transaction memory */
489static void free_bam_transaction(struct qcom_nand_controller *nandc)
490{
491 struct bam_transaction *bam_txn = nandc->bam_txn;
492
493 devm_kfree(nandc->dev, bam_txn);
494}
495
496/* Allocates and Initializes the BAM transaction */
497static struct bam_transaction *
498alloc_bam_transaction(struct qcom_nand_controller *nandc)
499{
500 struct bam_transaction *bam_txn;
501 size_t bam_txn_size;
502 unsigned int num_cw = nandc->max_cwperpage;
503 void *bam_txn_buf;
504
505 bam_txn_size =
506 sizeof(*bam_txn) + num_cw *
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530507 ((sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS) +
508 (sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) +
Abhishek Sahucb80f112017-08-17 17:37:40 +0530509 (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL));
510
511 bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL);
512 if (!bam_txn_buf)
513 return NULL;
514
515 bam_txn = bam_txn_buf;
516 bam_txn_buf += sizeof(*bam_txn);
517
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530518 bam_txn->bam_ce = bam_txn_buf;
519 bam_txn_buf +=
520 sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS * num_cw;
521
Abhishek Sahucb80f112017-08-17 17:37:40 +0530522 bam_txn->cmd_sgl = bam_txn_buf;
523 bam_txn_buf +=
524 sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw;
525
526 bam_txn->data_sgl = bam_txn_buf;
527
Abhishek Sahu6f200702018-06-20 12:57:33 +0530528 init_completion(&bam_txn->txn_done);
529
Abhishek Sahucb80f112017-08-17 17:37:40 +0530530 return bam_txn;
531}
532
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530533/* Clears the BAM transaction indexes */
534static void clear_bam_transaction(struct qcom_nand_controller *nandc)
535{
536 struct bam_transaction *bam_txn = nandc->bam_txn;
537
538 if (!nandc->props->is_bam)
539 return;
540
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530541 bam_txn->bam_ce_pos = 0;
542 bam_txn->bam_ce_start = 0;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530543 bam_txn->cmd_sgl_pos = 0;
544 bam_txn->cmd_sgl_start = 0;
545 bam_txn->tx_sgl_pos = 0;
546 bam_txn->tx_sgl_start = 0;
547 bam_txn->rx_sgl_pos = 0;
548 bam_txn->rx_sgl_start = 0;
Abhishek Sahu6f200702018-06-20 12:57:33 +0530549 bam_txn->last_data_desc = NULL;
550 bam_txn->wait_second_completion = false;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530551
552 sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage *
553 QPIC_PER_CW_CMD_SGL);
554 sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage *
555 QPIC_PER_CW_DATA_SGL);
Abhishek Sahu6f200702018-06-20 12:57:33 +0530556
557 reinit_completion(&bam_txn->txn_done);
558}
559
560/* Callback for DMA descriptor completion */
561static void qpic_bam_dma_done(void *data)
562{
563 struct bam_transaction *bam_txn = data;
564
565 /*
566 * In case of data transfer with NAND, 2 callbacks will be generated.
567 * One for command channel and another one for data channel.
568 * If current transaction has data descriptors
569 * (i.e. wait_second_completion is true), then set this to false
570 * and wait for second DMA descriptor completion.
571 */
572 if (bam_txn->wait_second_completion)
573 bam_txn->wait_second_completion = false;
574 else
575 complete(&bam_txn->txn_done);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530576}
577
Archit Tanejac76b78d2016-02-03 14:29:50 +0530578static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
579{
580 return container_of(chip, struct qcom_nand_host, chip);
581}
582
583static inline struct qcom_nand_controller *
584get_qcom_nand_controller(struct nand_chip *chip)
585{
586 return container_of(chip->controller, struct qcom_nand_controller,
587 controller);
588}
589
590static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
591{
592 return ioread32(nandc->base + offset);
593}
594
595static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
596 u32 val)
597{
598 iowrite32(val, nandc->base + offset);
599}
600
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530601static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
602 bool is_cpu)
603{
604 if (!nandc->props->is_bam)
605 return;
606
607 if (is_cpu)
608 dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
609 MAX_REG_RD *
610 sizeof(*nandc->reg_read_buf),
611 DMA_FROM_DEVICE);
612 else
613 dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
614 MAX_REG_RD *
615 sizeof(*nandc->reg_read_buf),
616 DMA_FROM_DEVICE);
617}
618
Archit Tanejac76b78d2016-02-03 14:29:50 +0530619static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
620{
621 switch (offset) {
622 case NAND_FLASH_CMD:
623 return &regs->cmd;
624 case NAND_ADDR0:
625 return &regs->addr0;
626 case NAND_ADDR1:
627 return &regs->addr1;
628 case NAND_FLASH_CHIP_SELECT:
629 return &regs->chip_sel;
630 case NAND_EXEC_CMD:
631 return &regs->exec;
632 case NAND_FLASH_STATUS:
633 return &regs->clrflashstatus;
634 case NAND_DEV0_CFG0:
635 return &regs->cfg0;
636 case NAND_DEV0_CFG1:
637 return &regs->cfg1;
638 case NAND_DEV0_ECC_CFG:
639 return &regs->ecc_bch_cfg;
640 case NAND_READ_STATUS:
641 return &regs->clrreadstatus;
642 case NAND_DEV_CMD1:
643 return &regs->cmd1;
644 case NAND_DEV_CMD1_RESTORE:
645 return &regs->orig_cmd1;
646 case NAND_DEV_CMD_VLD:
647 return &regs->vld;
648 case NAND_DEV_CMD_VLD_RESTORE:
649 return &regs->orig_vld;
650 case NAND_EBI2_ECC_BUF_CFG:
651 return &regs->ecc_buf_cfg;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530652 case NAND_READ_LOCATION_0:
653 return &regs->read_location0;
654 case NAND_READ_LOCATION_1:
655 return &regs->read_location1;
656 case NAND_READ_LOCATION_2:
657 return &regs->read_location2;
658 case NAND_READ_LOCATION_3:
659 return &regs->read_location3;
Md Sadre Alam503ee5a2021-02-24 01:09:01 +0530660 case NAND_READ_LOCATION_LAST_CW_0:
661 return &regs->read_location_last0;
662 case NAND_READ_LOCATION_LAST_CW_1:
663 return &regs->read_location_last1;
664 case NAND_READ_LOCATION_LAST_CW_2:
665 return &regs->read_location_last2;
666 case NAND_READ_LOCATION_LAST_CW_3:
667 return &regs->read_location_last3;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530668 default:
669 return NULL;
670 }
671}
672
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +0530673static void nandc_set_reg(struct nand_chip *chip, int offset,
Archit Tanejac76b78d2016-02-03 14:29:50 +0530674 u32 val)
675{
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +0530676 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530677 struct nandc_regs *regs = nandc->regs;
678 __le32 *reg;
679
680 reg = offset_to_nandc_reg(regs, offset);
681
682 if (reg)
683 *reg = cpu_to_le32(val);
684}
685
Md Sadre Alamb057e492021-02-24 01:08:58 +0530686/* Helper to check the code word, whether it is last cw or not */
687static bool qcom_nandc_is_last_cw(struct nand_ecc_ctrl *ecc, int cw)
688{
689 return cw == (ecc->steps - 1);
690}
691
Md Sadre Alame7a307f2021-02-24 01:09:00 +0530692/* helper to configure location register values */
693static void nandc_set_read_loc(struct nand_chip *chip, int cw, int reg,
694 int cw_offset, int read_size, int is_last_read_loc)
695{
Md Sadre Alam503ee5a2021-02-24 01:09:01 +0530696 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
697 struct nand_ecc_ctrl *ecc = &chip->ecc;
Md Sadre Alame7a307f2021-02-24 01:09:00 +0530698 int reg_base = NAND_READ_LOCATION_0;
699
Md Sadre Alam503ee5a2021-02-24 01:09:01 +0530700 if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw))
701 reg_base = NAND_READ_LOCATION_LAST_CW_0;
702
Md Sadre Alame7a307f2021-02-24 01:09:00 +0530703 reg_base += reg * 4;
704
Md Sadre Alam503ee5a2021-02-24 01:09:01 +0530705 if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw))
706 return nandc_set_read_loc_last(chip, reg_base, cw_offset,
707 read_size, is_last_read_loc);
708 else
709 return nandc_set_read_loc_first(chip, reg_base, cw_offset,
710 read_size, is_last_read_loc);
Md Sadre Alame7a307f2021-02-24 01:09:00 +0530711}
712
Archit Tanejac76b78d2016-02-03 14:29:50 +0530713/* helper to configure address register values */
714static void set_address(struct qcom_nand_host *host, u16 column, int page)
715{
716 struct nand_chip *chip = &host->chip;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530717
718 if (chip->options & NAND_BUSWIDTH_16)
719 column >>= 1;
720
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +0530721 nandc_set_reg(chip, NAND_ADDR0, page << 16 | column);
722 nandc_set_reg(chip, NAND_ADDR1, page >> 16 & 0xff);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530723}
724
725/*
726 * update_rw_regs: set up read/write register values, these will be
727 * written to the NAND controller registers via DMA
728 *
729 * @num_cw: number of steps for the read/write operation
730 * @read: read or write operation
Md Sadre Alam503ee5a2021-02-24 01:09:01 +0530731 * @cw : which code word
Archit Tanejac76b78d2016-02-03 14:29:50 +0530732 */
Md Sadre Alam503ee5a2021-02-24 01:09:01 +0530733static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read, int cw)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530734{
735 struct nand_chip *chip = &host->chip;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530736 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
Md Sadre Alambfb34ec2021-06-08 12:18:36 +0530737 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530738
739 if (read) {
740 if (host->use_ecc)
Olof Johansson33bf5512018-11-16 19:43:27 -0800741 cmd = OP_PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530742 else
Olof Johansson33bf5512018-11-16 19:43:27 -0800743 cmd = OP_PAGE_READ | PAGE_ACC | LAST_PAGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530744 } else {
Olof Johansson33bf5512018-11-16 19:43:27 -0800745 cmd = OP_PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530746 }
747
748 if (host->use_ecc) {
749 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
750 (num_cw - 1) << CW_PER_PAGE;
751
752 cfg1 = host->cfg1;
753 ecc_bch_cfg = host->ecc_bch_cfg;
754 } else {
755 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
756 (num_cw - 1) << CW_PER_PAGE;
757
758 cfg1 = host->cfg1_raw;
759 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
760 }
761
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +0530762 nandc_set_reg(chip, NAND_FLASH_CMD, cmd);
763 nandc_set_reg(chip, NAND_DEV0_CFG0, cfg0);
764 nandc_set_reg(chip, NAND_DEV0_CFG1, cfg1);
765 nandc_set_reg(chip, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
Md Sadre Alambfb34ec2021-06-08 12:18:36 +0530766 if (!nandc->props->qpic_v2)
767 nandc_set_reg(chip, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +0530768 nandc_set_reg(chip, NAND_FLASH_STATUS, host->clrflashstatus);
769 nandc_set_reg(chip, NAND_READ_STATUS, host->clrreadstatus);
770 nandc_set_reg(chip, NAND_EXEC_CMD, 1);
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530771
772 if (read)
Md Sadre Alam503ee5a2021-02-24 01:09:01 +0530773 nandc_set_read_loc(chip, cw, 0, 0, host->use_ecc ?
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530774 host->cw_data : host->cw_size, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530775}
776
Abhishek Sahu381dd242017-08-17 17:37:41 +0530777/*
778 * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
779 * for BAM. This descriptor will be added in the NAND DMA descriptor queue
780 * which will be submitted to DMA engine.
781 */
782static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
783 struct dma_chan *chan,
784 unsigned long flags)
785{
786 struct desc_info *desc;
787 struct scatterlist *sgl;
788 unsigned int sgl_cnt;
789 int ret;
790 struct bam_transaction *bam_txn = nandc->bam_txn;
791 enum dma_transfer_direction dir_eng;
792 struct dma_async_tx_descriptor *dma_desc;
793
794 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
795 if (!desc)
796 return -ENOMEM;
797
798 if (chan == nandc->cmd_chan) {
799 sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
800 sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
801 bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
802 dir_eng = DMA_MEM_TO_DEV;
803 desc->dir = DMA_TO_DEVICE;
804 } else if (chan == nandc->tx_chan) {
805 sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
806 sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
807 bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
808 dir_eng = DMA_MEM_TO_DEV;
809 desc->dir = DMA_TO_DEVICE;
810 } else {
811 sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
812 sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
813 bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
814 dir_eng = DMA_DEV_TO_MEM;
815 desc->dir = DMA_FROM_DEVICE;
816 }
817
818 sg_mark_end(sgl + sgl_cnt - 1);
819 ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
820 if (ret == 0) {
821 dev_err(nandc->dev, "failure in mapping desc\n");
822 kfree(desc);
823 return -ENOMEM;
824 }
825
826 desc->sgl_cnt = sgl_cnt;
827 desc->bam_sgl = sgl;
828
829 dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
830 flags);
831
832 if (!dma_desc) {
833 dev_err(nandc->dev, "failure in prep desc\n");
834 dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
835 kfree(desc);
836 return -EINVAL;
837 }
838
839 desc->dma_desc = dma_desc;
840
Abhishek Sahu6f200702018-06-20 12:57:33 +0530841 /* update last data/command descriptor */
842 if (chan == nandc->cmd_chan)
843 bam_txn->last_cmd_desc = dma_desc;
844 else
845 bam_txn->last_data_desc = dma_desc;
846
Abhishek Sahu381dd242017-08-17 17:37:41 +0530847 list_add_tail(&desc->node, &nandc->desc_list);
848
849 return 0;
850}
851
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530852/*
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530853 * Prepares the command descriptor for BAM DMA which will be used for NAND
854 * register reads and writes. The command descriptor requires the command
855 * to be formed in command element type so this function uses the command
856 * element from bam transaction ce array and fills the same with required
857 * data. A single SGL can contain multiple command elements so
858 * NAND_BAM_NEXT_SGL will be used for starting the separate SGL
859 * after the current command element.
860 */
861static int prep_bam_dma_desc_cmd(struct qcom_nand_controller *nandc, bool read,
862 int reg_off, const void *vaddr,
863 int size, unsigned int flags)
864{
865 int bam_ce_size;
866 int i, ret;
867 struct bam_cmd_element *bam_ce_buffer;
868 struct bam_transaction *bam_txn = nandc->bam_txn;
869
870 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_pos];
871
872 /* fill the command desc */
873 for (i = 0; i < size; i++) {
874 if (read)
875 bam_prep_ce(&bam_ce_buffer[i],
876 nandc_reg_phys(nandc, reg_off + 4 * i),
877 BAM_READ_COMMAND,
878 reg_buf_dma_addr(nandc,
879 (__le32 *)vaddr + i));
880 else
881 bam_prep_ce_le32(&bam_ce_buffer[i],
882 nandc_reg_phys(nandc, reg_off + 4 * i),
883 BAM_WRITE_COMMAND,
884 *((__le32 *)vaddr + i));
885 }
886
887 bam_txn->bam_ce_pos += size;
888
889 /* use the separate sgl after this command */
890 if (flags & NAND_BAM_NEXT_SGL) {
891 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_start];
892 bam_ce_size = (bam_txn->bam_ce_pos -
893 bam_txn->bam_ce_start) *
894 sizeof(struct bam_cmd_element);
895 sg_set_buf(&bam_txn->cmd_sgl[bam_txn->cmd_sgl_pos],
896 bam_ce_buffer, bam_ce_size);
897 bam_txn->cmd_sgl_pos++;
898 bam_txn->bam_ce_start = bam_txn->bam_ce_pos;
899
900 if (flags & NAND_BAM_NWD) {
901 ret = prepare_bam_async_desc(nandc, nandc->cmd_chan,
902 DMA_PREP_FENCE |
903 DMA_PREP_CMD);
904 if (ret)
905 return ret;
906 }
907 }
908
909 return 0;
910}
911
912/*
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530913 * Prepares the data descriptor for BAM DMA which will be used for NAND
914 * data reads and writes.
915 */
916static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read,
917 const void *vaddr,
918 int size, unsigned int flags)
919{
920 int ret;
921 struct bam_transaction *bam_txn = nandc->bam_txn;
922
923 if (read) {
924 sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos],
925 vaddr, size);
926 bam_txn->rx_sgl_pos++;
927 } else {
928 sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos],
929 vaddr, size);
930 bam_txn->tx_sgl_pos++;
931
932 /*
933 * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
934 * is not set, form the DMA descriptor
935 */
936 if (!(flags & NAND_BAM_NO_EOT)) {
937 ret = prepare_bam_async_desc(nandc, nandc->tx_chan,
938 DMA_PREP_INTERRUPT);
939 if (ret)
940 return ret;
941 }
942 }
943
944 return 0;
945}
946
Abhishek Sahu381dd242017-08-17 17:37:41 +0530947static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
948 int reg_off, const void *vaddr, int size,
949 bool flow_control)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530950{
951 struct desc_info *desc;
952 struct dma_async_tx_descriptor *dma_desc;
953 struct scatterlist *sgl;
954 struct dma_slave_config slave_conf;
955 enum dma_transfer_direction dir_eng;
956 int ret;
957
958 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
959 if (!desc)
960 return -ENOMEM;
961
Abhishek Sahu381dd242017-08-17 17:37:41 +0530962 sgl = &desc->adm_sgl;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530963
964 sg_init_one(sgl, vaddr, size);
965
966 if (read) {
967 dir_eng = DMA_DEV_TO_MEM;
968 desc->dir = DMA_FROM_DEVICE;
969 } else {
970 dir_eng = DMA_MEM_TO_DEV;
971 desc->dir = DMA_TO_DEVICE;
972 }
973
974 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
975 if (ret == 0) {
976 ret = -ENOMEM;
977 goto err;
978 }
979
980 memset(&slave_conf, 0x00, sizeof(slave_conf));
981
982 slave_conf.device_fc = flow_control;
983 if (read) {
984 slave_conf.src_maxburst = 16;
985 slave_conf.src_addr = nandc->base_dma + reg_off;
986 slave_conf.slave_id = nandc->data_crci;
987 } else {
988 slave_conf.dst_maxburst = 16;
989 slave_conf.dst_addr = nandc->base_dma + reg_off;
990 slave_conf.slave_id = nandc->cmd_crci;
991 }
992
993 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
994 if (ret) {
995 dev_err(nandc->dev, "failed to configure dma channel\n");
996 goto err;
997 }
998
999 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
1000 if (!dma_desc) {
1001 dev_err(nandc->dev, "failed to prepare desc\n");
1002 ret = -EINVAL;
1003 goto err;
1004 }
1005
1006 desc->dma_desc = dma_desc;
1007
1008 list_add_tail(&desc->node, &nandc->desc_list);
1009
1010 return 0;
1011err:
1012 kfree(desc);
1013
1014 return ret;
1015}
1016
1017/*
1018 * read_reg_dma: prepares a descriptor to read a given number of
1019 * contiguous registers to the reg_read_buf pointer
1020 *
1021 * @first: offset of the first register in the contiguous block
1022 * @num_regs: number of registers to read
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301023 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +05301024 */
1025static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301026 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301027{
1028 bool flow_control = false;
1029 void *vaddr;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301030
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301031 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
1032 nandc->reg_read_pos += num_regs;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301033
Abhishek Sahucc409b92017-08-17 17:37:47 +05301034 if (first == NAND_DEV_CMD_VLD || first == NAND_DEV_CMD1)
1035 first = dev_cmd_reg_addr(nandc, first);
1036
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301037 if (nandc->props->is_bam)
1038 return prep_bam_dma_desc_cmd(nandc, true, first, vaddr,
1039 num_regs, flags);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301040
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301041 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
1042 flow_control = true;
1043
1044 return prep_adm_dma_desc(nandc, true, first, vaddr,
1045 num_regs * sizeof(u32), flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301046}
1047
1048/*
1049 * write_reg_dma: prepares a descriptor to write a given number of
1050 * contiguous registers
1051 *
1052 * @first: offset of the first register in the contiguous block
1053 * @num_regs: number of registers to write
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301054 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +05301055 */
1056static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301057 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301058{
1059 bool flow_control = false;
1060 struct nandc_regs *regs = nandc->regs;
1061 void *vaddr;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301062
1063 vaddr = offset_to_nandc_reg(regs, first);
1064
Abhishek Sahua86b9c42017-08-17 17:37:44 +05301065 if (first == NAND_ERASED_CW_DETECT_CFG) {
1066 if (flags & NAND_ERASED_CW_SET)
1067 vaddr = &regs->erased_cw_detect_cfg_set;
1068 else
1069 vaddr = &regs->erased_cw_detect_cfg_clr;
1070 }
1071
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301072 if (first == NAND_EXEC_CMD)
1073 flags |= NAND_BAM_NWD;
1074
Abhishek Sahucc409b92017-08-17 17:37:47 +05301075 if (first == NAND_DEV_CMD1_RESTORE || first == NAND_DEV_CMD1)
1076 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301077
Abhishek Sahucc409b92017-08-17 17:37:47 +05301078 if (first == NAND_DEV_CMD_VLD_RESTORE || first == NAND_DEV_CMD_VLD)
1079 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301080
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301081 if (nandc->props->is_bam)
1082 return prep_bam_dma_desc_cmd(nandc, false, first, vaddr,
1083 num_regs, flags);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301084
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301085 if (first == NAND_FLASH_CMD)
1086 flow_control = true;
1087
1088 return prep_adm_dma_desc(nandc, false, first, vaddr,
1089 num_regs * sizeof(u32), flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301090}
1091
1092/*
1093 * read_data_dma: prepares a DMA descriptor to transfer data from the
1094 * controller's internal buffer to the buffer 'vaddr'
1095 *
1096 * @reg_off: offset within the controller's data buffer
1097 * @vaddr: virtual address of the buffer we want to write to
1098 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301099 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +05301100 */
1101static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301102 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301103{
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301104 if (nandc->props->is_bam)
1105 return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags);
1106
Abhishek Sahu381dd242017-08-17 17:37:41 +05301107 return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301108}
1109
1110/*
1111 * write_data_dma: prepares a DMA descriptor to transfer data from
1112 * 'vaddr' to the controller's internal buffer
1113 *
1114 * @reg_off: offset within the controller's data buffer
1115 * @vaddr: virtual address of the buffer we want to read from
1116 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301117 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +05301118 */
1119static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301120 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301121{
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301122 if (nandc->props->is_bam)
1123 return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags);
1124
Abhishek Sahu381dd242017-08-17 17:37:41 +05301125 return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301126}
1127
1128/*
Abhishek Sahubde43302017-07-19 17:17:55 +05301129 * Helper to prepare DMA descriptors for configuring registers
1130 * before reading a NAND page.
Archit Tanejac76b78d2016-02-03 14:29:50 +05301131 */
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301132static void config_nand_page_read(struct nand_chip *chip)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301133{
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301134 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1135
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301136 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1137 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
Md Sadre Alambfb34ec2021-06-08 12:18:36 +05301138 if (!nandc->props->qpic_v2)
1139 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
Abhishek Sahua86b9c42017-08-17 17:37:44 +05301140 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
1141 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
1142 NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
Abhishek Sahubde43302017-07-19 17:17:55 +05301143}
Archit Tanejac76b78d2016-02-03 14:29:50 +05301144
Abhishek Sahubde43302017-07-19 17:17:55 +05301145/*
1146 * Helper to prepare DMA descriptors for configuring registers
1147 * before reading each codeword in NAND page.
1148 */
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301149static void
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05301150config_nand_cw_read(struct nand_chip *chip, bool use_ecc, int cw)
Abhishek Sahubde43302017-07-19 17:17:55 +05301151{
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301152 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05301153 struct nand_ecc_ctrl *ecc = &chip->ecc;
1154
1155 int reg = NAND_READ_LOCATION_0;
1156
1157 if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw))
1158 reg = NAND_READ_LOCATION_LAST_CW_0;
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301159
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301160 if (nandc->props->is_bam)
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05301161 write_reg_dma(nandc, reg, 4, NAND_BAM_NEXT_SGL);
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301162
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301163 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1164 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301165
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301166 if (use_ecc) {
1167 read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
1168 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
1169 NAND_BAM_NEXT_SGL);
1170 } else {
1171 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1172 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301173}
1174
1175/*
Abhishek Sahubde43302017-07-19 17:17:55 +05301176 * Helper to prepare dma descriptors to configure registers needed for reading a
1177 * single codeword in page
Archit Tanejac76b78d2016-02-03 14:29:50 +05301178 */
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301179static void
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301180config_nand_single_cw_page_read(struct nand_chip *chip,
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05301181 bool use_ecc, int cw)
Abhishek Sahubde43302017-07-19 17:17:55 +05301182{
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301183 config_nand_page_read(chip);
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05301184 config_nand_cw_read(chip, use_ecc, cw);
Abhishek Sahubde43302017-07-19 17:17:55 +05301185}
1186
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301187/*
1188 * Helper to prepare DMA descriptors used to configure registers needed for
1189 * before writing a NAND page.
1190 */
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301191static void config_nand_page_write(struct nand_chip *chip)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301192{
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301193 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1194
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301195 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1196 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
Md Sadre Alambfb34ec2021-06-08 12:18:36 +05301197 if (!nandc->props->qpic_v2)
1198 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
1199 NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301200}
1201
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301202/*
1203 * Helper to prepare DMA descriptors for configuring registers
1204 * before writing each codeword in NAND page.
1205 */
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301206static void config_nand_cw_write(struct nand_chip *chip)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301207{
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301208 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1209
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301210 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1211 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301212
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301213 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301214
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301215 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1216 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301217}
1218
1219/*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001220 * the following functions are used within chip->legacy.cmdfunc() to
1221 * perform different NAND_CMD_* commands
Archit Tanejac76b78d2016-02-03 14:29:50 +05301222 */
1223
1224/* sets up descriptors for NAND_CMD_PARAM */
1225static int nandc_param(struct qcom_nand_host *host)
1226{
1227 struct nand_chip *chip = &host->chip;
1228 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1229
1230 /*
1231 * NAND_CMD_PARAM is called before we know much about the FLASH chip
1232 * in use. we configure the controller to perform a raw read of 512
1233 * bytes to read onfi params
1234 */
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05301235 if (nandc->props->qpic_v2)
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301236 nandc_set_reg(chip, NAND_FLASH_CMD, OP_PAGE_READ_ONFI_READ |
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05301237 PAGE_ACC | LAST_PAGE);
1238 else
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301239 nandc_set_reg(chip, NAND_FLASH_CMD, OP_PAGE_READ |
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05301240 PAGE_ACC | LAST_PAGE);
1241
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301242 nandc_set_reg(chip, NAND_ADDR0, 0);
1243 nandc_set_reg(chip, NAND_ADDR1, 0);
1244 nandc_set_reg(chip, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
Archit Tanejac76b78d2016-02-03 14:29:50 +05301245 | 512 << UD_SIZE_BYTES
1246 | 5 << NUM_ADDR_CYCLES
1247 | 0 << SPARE_SIZE_BYTES);
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301248 nandc_set_reg(chip, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
Archit Tanejac76b78d2016-02-03 14:29:50 +05301249 | 0 << CS_ACTIVE_BSY
1250 | 17 << BAD_BLOCK_BYTE_NUM
1251 | 1 << BAD_BLOCK_IN_SPARE_AREA
1252 | 2 << WR_RD_BSY_GAP
1253 | 0 << WIDE_FLASH
1254 | 1 << DEV0_CFG1_ECC_DISABLE);
Md Sadre Alambfb34ec2021-06-08 12:18:36 +05301255 if (!nandc->props->qpic_v2)
1256 nandc_set_reg(chip, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301257
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05301258 /* configure CMD1 and VLD for ONFI param probing in QPIC v1 */
1259 if (!nandc->props->qpic_v2) {
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301260 nandc_set_reg(chip, NAND_DEV_CMD_VLD,
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05301261 (nandc->vld & ~READ_START_VLD));
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301262 nandc_set_reg(chip, NAND_DEV_CMD1,
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05301263 (nandc->cmd1 & ~(0xFF << READ_ADDR))
1264 | NAND_CMD_PARAM << READ_ADDR);
1265 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301266
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301267 nandc_set_reg(chip, NAND_EXEC_CMD, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301268
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05301269 if (!nandc->props->qpic_v2) {
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301270 nandc_set_reg(chip, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
1271 nandc_set_reg(chip, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05301272 }
1273
Md Sadre Alame7a307f2021-02-24 01:09:00 +05301274 nandc_set_read_loc(chip, 0, 0, 0, 512, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301275
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05301276 if (!nandc->props->qpic_v2) {
1277 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
1278 write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
1279 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301280
1281 nandc->buf_count = 512;
1282 memset(nandc->data_buffer, 0xff, nandc->buf_count);
1283
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05301284 config_nand_single_cw_page_read(chip, false, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301285
1286 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301287 nandc->buf_count, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301288
1289 /* restore CMD1 and VLD regs */
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05301290 if (!nandc->props->qpic_v2) {
1291 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
1292 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
1293 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301294
1295 return 0;
1296}
1297
1298/* sets up descriptors for NAND_CMD_ERASE1 */
1299static int erase_block(struct qcom_nand_host *host, int page_addr)
1300{
1301 struct nand_chip *chip = &host->chip;
1302 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1303
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301304 nandc_set_reg(chip, NAND_FLASH_CMD,
Olof Johansson33bf5512018-11-16 19:43:27 -08001305 OP_BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301306 nandc_set_reg(chip, NAND_ADDR0, page_addr);
1307 nandc_set_reg(chip, NAND_ADDR1, 0);
1308 nandc_set_reg(chip, NAND_DEV0_CFG0,
Archit Tanejac76b78d2016-02-03 14:29:50 +05301309 host->cfg0_raw & ~(7 << CW_PER_PAGE));
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301310 nandc_set_reg(chip, NAND_DEV0_CFG1, host->cfg1_raw);
1311 nandc_set_reg(chip, NAND_EXEC_CMD, 1);
1312 nandc_set_reg(chip, NAND_FLASH_STATUS, host->clrflashstatus);
1313 nandc_set_reg(chip, NAND_READ_STATUS, host->clrreadstatus);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301314
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301315 write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
1316 write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
1317 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301318
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301319 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301320
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301321 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1322 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301323
1324 return 0;
1325}
1326
1327/* sets up descriptors for NAND_CMD_READID */
1328static int read_id(struct qcom_nand_host *host, int column)
1329{
1330 struct nand_chip *chip = &host->chip;
1331 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1332
1333 if (column == -1)
1334 return 0;
1335
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301336 nandc_set_reg(chip, NAND_FLASH_CMD, OP_FETCH_ID);
1337 nandc_set_reg(chip, NAND_ADDR0, column);
1338 nandc_set_reg(chip, NAND_ADDR1, 0);
1339 nandc_set_reg(chip, NAND_FLASH_CHIP_SELECT,
Abhishek Sahu9d43f912017-08-17 17:37:45 +05301340 nandc->props->is_bam ? 0 : DM_EN);
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301341 nandc_set_reg(chip, NAND_EXEC_CMD, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301342
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301343 write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
1344 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301345
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301346 read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301347
1348 return 0;
1349}
1350
1351/* sets up descriptors for NAND_CMD_RESET */
1352static int reset(struct qcom_nand_host *host)
1353{
1354 struct nand_chip *chip = &host->chip;
1355 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1356
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301357 nandc_set_reg(chip, NAND_FLASH_CMD, OP_RESET_DEVICE);
1358 nandc_set_reg(chip, NAND_EXEC_CMD, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301359
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301360 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1361 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301362
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301363 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301364
1365 return 0;
1366}
1367
1368/* helpers to submit/free our list of dma descriptors */
1369static int submit_descs(struct qcom_nand_controller *nandc)
1370{
1371 struct desc_info *desc;
1372 dma_cookie_t cookie = 0;
Abhishek Sahu381dd242017-08-17 17:37:41 +05301373 struct bam_transaction *bam_txn = nandc->bam_txn;
1374 int r;
1375
1376 if (nandc->props->is_bam) {
1377 if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
1378 r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
1379 if (r)
1380 return r;
1381 }
1382
1383 if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
1384 r = prepare_bam_async_desc(nandc, nandc->tx_chan,
1385 DMA_PREP_INTERRUPT);
1386 if (r)
1387 return r;
1388 }
1389
1390 if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301391 r = prepare_bam_async_desc(nandc, nandc->cmd_chan,
1392 DMA_PREP_CMD);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301393 if (r)
1394 return r;
1395 }
1396 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301397
1398 list_for_each_entry(desc, &nandc->desc_list, node)
1399 cookie = dmaengine_submit(desc->dma_desc);
1400
Abhishek Sahu381dd242017-08-17 17:37:41 +05301401 if (nandc->props->is_bam) {
Abhishek Sahu6f200702018-06-20 12:57:33 +05301402 bam_txn->last_cmd_desc->callback = qpic_bam_dma_done;
1403 bam_txn->last_cmd_desc->callback_param = bam_txn;
1404 if (bam_txn->last_data_desc) {
1405 bam_txn->last_data_desc->callback = qpic_bam_dma_done;
1406 bam_txn->last_data_desc->callback_param = bam_txn;
1407 bam_txn->wait_second_completion = true;
1408 }
1409
Abhishek Sahu381dd242017-08-17 17:37:41 +05301410 dma_async_issue_pending(nandc->tx_chan);
1411 dma_async_issue_pending(nandc->rx_chan);
Abhishek Sahu6f200702018-06-20 12:57:33 +05301412 dma_async_issue_pending(nandc->cmd_chan);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301413
Abhishek Sahu6f200702018-06-20 12:57:33 +05301414 if (!wait_for_completion_timeout(&bam_txn->txn_done,
1415 QPIC_NAND_COMPLETION_TIMEOUT))
Abhishek Sahu381dd242017-08-17 17:37:41 +05301416 return -ETIMEDOUT;
1417 } else {
1418 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
1419 return -ETIMEDOUT;
1420 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301421
1422 return 0;
1423}
1424
1425static void free_descs(struct qcom_nand_controller *nandc)
1426{
1427 struct desc_info *desc, *n;
1428
1429 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
1430 list_del(&desc->node);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301431
1432 if (nandc->props->is_bam)
1433 dma_unmap_sg(nandc->dev, desc->bam_sgl,
1434 desc->sgl_cnt, desc->dir);
1435 else
1436 dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
1437 desc->dir);
1438
Archit Tanejac76b78d2016-02-03 14:29:50 +05301439 kfree(desc);
1440 }
1441}
1442
1443/* reset the register read buffer for next NAND operation */
1444static void clear_read_regs(struct qcom_nand_controller *nandc)
1445{
1446 nandc->reg_read_pos = 0;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301447 nandc_read_buffer_sync(nandc, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301448}
1449
1450static void pre_command(struct qcom_nand_host *host, int command)
1451{
1452 struct nand_chip *chip = &host->chip;
1453 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1454
1455 nandc->buf_count = 0;
1456 nandc->buf_start = 0;
1457 host->use_ecc = false;
1458 host->last_command = command;
1459
1460 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301461
1462 if (command == NAND_CMD_RESET || command == NAND_CMD_READID ||
1463 command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1)
1464 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301465}
1466
1467/*
1468 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
1469 * privately maintained status byte, this status byte can be read after
1470 * NAND_CMD_STATUS is called
1471 */
1472static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
1473{
1474 struct nand_chip *chip = &host->chip;
1475 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1476 struct nand_ecc_ctrl *ecc = &chip->ecc;
1477 int num_cw;
1478 int i;
1479
1480 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301481 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301482
1483 for (i = 0; i < num_cw; i++) {
1484 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
1485
1486 if (flash_status & FS_MPU_ERR)
1487 host->status &= ~NAND_STATUS_WP;
1488
1489 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
1490 (flash_status &
1491 FS_DEVICE_STS_ERR)))
1492 host->status |= NAND_STATUS_FAIL;
1493 }
1494}
1495
1496static void post_command(struct qcom_nand_host *host, int command)
1497{
1498 struct nand_chip *chip = &host->chip;
1499 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1500
1501 switch (command) {
1502 case NAND_CMD_READID:
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301503 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301504 memcpy(nandc->data_buffer, nandc->reg_read_buf,
1505 nandc->buf_count);
1506 break;
1507 case NAND_CMD_PAGEPROG:
1508 case NAND_CMD_ERASE1:
1509 parse_erase_write_errors(host, command);
1510 break;
1511 default:
1512 break;
1513 }
1514}
1515
1516/*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001517 * Implements chip->legacy.cmdfunc. It's only used for a limited set of
1518 * commands. The rest of the commands wouldn't be called by upper layers.
1519 * For example, NAND_CMD_READOOB would never be called because we have our own
1520 * versions of read_oob ops for nand_ecc_ctrl.
Archit Tanejac76b78d2016-02-03 14:29:50 +05301521 */
Boris Brezillon5295cf22018-09-06 14:05:28 +02001522static void qcom_nandc_command(struct nand_chip *chip, unsigned int command,
Archit Tanejac76b78d2016-02-03 14:29:50 +05301523 int column, int page_addr)
1524{
Archit Tanejac76b78d2016-02-03 14:29:50 +05301525 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1526 struct nand_ecc_ctrl *ecc = &chip->ecc;
1527 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1528 bool wait = false;
1529 int ret = 0;
1530
1531 pre_command(host, command);
1532
1533 switch (command) {
1534 case NAND_CMD_RESET:
1535 ret = reset(host);
1536 wait = true;
1537 break;
1538
1539 case NAND_CMD_READID:
1540 nandc->buf_count = 4;
1541 ret = read_id(host, column);
1542 wait = true;
1543 break;
1544
1545 case NAND_CMD_PARAM:
1546 ret = nandc_param(host);
1547 wait = true;
1548 break;
1549
1550 case NAND_CMD_ERASE1:
1551 ret = erase_block(host, page_addr);
1552 wait = true;
1553 break;
1554
1555 case NAND_CMD_READ0:
1556 /* we read the entire page for now */
1557 WARN_ON(column != 0);
1558
1559 host->use_ecc = true;
1560 set_address(host, 0, page_addr);
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05301561 update_rw_regs(host, ecc->steps, true, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301562 break;
1563
1564 case NAND_CMD_SEQIN:
1565 WARN_ON(column != 0);
1566 set_address(host, 0, page_addr);
1567 break;
1568
1569 case NAND_CMD_PAGEPROG:
1570 case NAND_CMD_STATUS:
1571 case NAND_CMD_NONE:
1572 default:
1573 break;
1574 }
1575
1576 if (ret) {
1577 dev_err(nandc->dev, "failure executing command %d\n",
1578 command);
1579 free_descs(nandc);
1580 return;
1581 }
1582
1583 if (wait) {
1584 ret = submit_descs(nandc);
1585 if (ret)
1586 dev_err(nandc->dev,
1587 "failure submitting descs for command %d\n",
1588 command);
1589 }
1590
1591 free_descs(nandc);
1592
1593 post_command(host, command);
1594}
1595
1596/*
1597 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1598 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1599 *
1600 * when using RS ECC, the HW reports the same erros when reading an erased CW,
1601 * but it notifies that it is an erased CW by placing special characters at
1602 * certain offsets in the buffer.
1603 *
1604 * verify if the page is erased or not, and fix up the page for RS ECC by
1605 * replacing the special characters with 0xff.
1606 */
1607static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
1608{
1609 u8 empty1, empty2;
1610
1611 /*
1612 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1613 * is erased by looking for 0x54s at offsets 3 and 175 from the
1614 * beginning of each codeword
1615 */
1616
1617 empty1 = data_buf[3];
1618 empty2 = data_buf[175];
1619
1620 /*
1621 * if the erased codework markers, if they exist override them with
1622 * 0xffs
1623 */
1624 if ((empty1 == 0x54 && empty2 == 0xff) ||
1625 (empty1 == 0xff && empty2 == 0x54)) {
1626 data_buf[3] = 0xff;
1627 data_buf[175] = 0xff;
1628 }
1629
1630 /*
1631 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1632 * restore the original values at the special offsets
1633 */
1634 if (memchr_inv(data_buf, 0xff, data_len)) {
1635 data_buf[3] = empty1;
1636 data_buf[175] = empty2;
1637
1638 return false;
1639 }
1640
1641 return true;
1642}
1643
1644struct read_stats {
1645 __le32 flash;
1646 __le32 buffer;
1647 __le32 erased_cw;
1648};
1649
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301650/* reads back FLASH_STATUS register set by the controller */
1651static int check_flash_errors(struct qcom_nand_host *host, int cw_cnt)
1652{
1653 struct nand_chip *chip = &host->chip;
1654 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1655 int i;
1656
Praveenkumar Ibc368602020-10-09 13:37:52 +05301657 nandc_read_buffer_sync(nandc, true);
1658
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301659 for (i = 0; i < cw_cnt; i++) {
1660 u32 flash = le32_to_cpu(nandc->reg_read_buf[i]);
1661
1662 if (flash & (FS_OP_ERR | FS_MPU_ERR))
1663 return -EIO;
1664 }
1665
1666 return 0;
1667}
1668
Abhishek Sahu85632c12018-06-20 12:57:40 +05301669/* performs raw read for one codeword */
1670static int
1671qcom_nandc_read_cw_raw(struct mtd_info *mtd, struct nand_chip *chip,
1672 u8 *data_buf, u8 *oob_buf, int page, int cw)
1673{
1674 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1675 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1676 struct nand_ecc_ctrl *ecc = &chip->ecc;
1677 int data_size1, data_size2, oob_size1, oob_size2;
1678 int ret, reg_off = FLASH_BUF_ACC, read_loc = 0;
Md Sadre Alamf60f5742021-09-07 12:35:57 +05301679 int raw_cw = cw;
Abhishek Sahu85632c12018-06-20 12:57:40 +05301680
1681 nand_read_page_op(chip, page, 0, NULL, 0);
1682 host->use_ecc = false;
1683
Md Sadre Alamf60f5742021-09-07 12:35:57 +05301684 if (nandc->props->qpic_v2)
1685 raw_cw = ecc->steps - 1;
1686
Abhishek Sahu85632c12018-06-20 12:57:40 +05301687 clear_bam_transaction(nandc);
1688 set_address(host, host->cw_size * cw, page);
Md Sadre Alamf60f5742021-09-07 12:35:57 +05301689 update_rw_regs(host, 1, true, raw_cw);
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301690 config_nand_page_read(chip);
Abhishek Sahu85632c12018-06-20 12:57:40 +05301691
1692 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1693 oob_size1 = host->bbm_size;
1694
Md Sadre Alamb057e492021-02-24 01:08:58 +05301695 if (qcom_nandc_is_last_cw(ecc, cw)) {
Abhishek Sahu85632c12018-06-20 12:57:40 +05301696 data_size2 = ecc->size - data_size1 -
1697 ((ecc->steps - 1) * 4);
1698 oob_size2 = (ecc->steps * 4) + host->ecc_bytes_hw +
1699 host->spare_bytes;
1700 } else {
1701 data_size2 = host->cw_data - data_size1;
1702 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1703 }
1704
1705 if (nandc->props->is_bam) {
Md Sadre Alame7a307f2021-02-24 01:09:00 +05301706 nandc_set_read_loc(chip, cw, 0, read_loc, data_size1, 0);
Abhishek Sahu85632c12018-06-20 12:57:40 +05301707 read_loc += data_size1;
1708
Md Sadre Alame7a307f2021-02-24 01:09:00 +05301709 nandc_set_read_loc(chip, cw, 1, read_loc, oob_size1, 0);
Abhishek Sahu85632c12018-06-20 12:57:40 +05301710 read_loc += oob_size1;
1711
Md Sadre Alame7a307f2021-02-24 01:09:00 +05301712 nandc_set_read_loc(chip, cw, 2, read_loc, data_size2, 0);
Abhishek Sahu85632c12018-06-20 12:57:40 +05301713 read_loc += data_size2;
1714
Md Sadre Alame7a307f2021-02-24 01:09:00 +05301715 nandc_set_read_loc(chip, cw, 3, read_loc, oob_size2, 1);
Abhishek Sahu85632c12018-06-20 12:57:40 +05301716 }
1717
Md Sadre Alamf60f5742021-09-07 12:35:57 +05301718 config_nand_cw_read(chip, false, raw_cw);
Abhishek Sahu85632c12018-06-20 12:57:40 +05301719
1720 read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
1721 reg_off += data_size1;
1722
1723 read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
1724 reg_off += oob_size1;
1725
1726 read_data_dma(nandc, reg_off, data_buf + data_size1, data_size2, 0);
1727 reg_off += data_size2;
1728
1729 read_data_dma(nandc, reg_off, oob_buf + oob_size1, oob_size2, 0);
1730
1731 ret = submit_descs(nandc);
1732 free_descs(nandc);
1733 if (ret) {
1734 dev_err(nandc->dev, "failure to read raw cw %d\n", cw);
1735 return ret;
1736 }
1737
1738 return check_flash_errors(host, 1);
1739}
1740
Archit Tanejac76b78d2016-02-03 14:29:50 +05301741/*
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301742 * Bitflips can happen in erased codewords also so this function counts the
1743 * number of 0 in each CW for which ECC engine returns the uncorrectable
1744 * error. The page will be assumed as erased if this count is less than or
1745 * equal to the ecc->strength for each CW.
1746 *
1747 * 1. Both DATA and OOB need to be checked for number of 0. The
1748 * top-level API can be called with only data buf or OOB buf so use
1749 * chip->data_buf if data buf is null and chip->oob_poi if oob buf
1750 * is null for copying the raw bytes.
1751 * 2. Perform raw read for all the CW which has uncorrectable errors.
1752 * 3. For each CW, check the number of 0 in cw_data and usable OOB bytes.
1753 * The BBM and spare bytes bit flip won’t affect the ECC so don’t check
1754 * the number of bitflips in this area.
1755 */
1756static int
1757check_for_erased_page(struct qcom_nand_host *host, u8 *data_buf,
1758 u8 *oob_buf, unsigned long uncorrectable_cws,
1759 int page, unsigned int max_bitflips)
1760{
1761 struct nand_chip *chip = &host->chip;
1762 struct mtd_info *mtd = nand_to_mtd(chip);
1763 struct nand_ecc_ctrl *ecc = &chip->ecc;
1764 u8 *cw_data_buf, *cw_oob_buf;
1765 int cw, data_size, oob_size, ret = 0;
1766
Boris Brezilloneeab7172018-10-28 15:27:55 +01001767 if (!data_buf)
1768 data_buf = nand_get_data_buf(chip);
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301769
1770 if (!oob_buf) {
Boris Brezilloneeab7172018-10-28 15:27:55 +01001771 nand_get_data_buf(chip);
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301772 oob_buf = chip->oob_poi;
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301773 }
1774
1775 for_each_set_bit(cw, &uncorrectable_cws, ecc->steps) {
Md Sadre Alamb057e492021-02-24 01:08:58 +05301776 if (qcom_nandc_is_last_cw(ecc, cw)) {
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301777 data_size = ecc->size - ((ecc->steps - 1) * 4);
1778 oob_size = (ecc->steps * 4) + host->ecc_bytes_hw;
1779 } else {
1780 data_size = host->cw_data;
1781 oob_size = host->ecc_bytes_hw;
1782 }
1783
1784 /* determine starting buffer address for current CW */
1785 cw_data_buf = data_buf + (cw * host->cw_data);
1786 cw_oob_buf = oob_buf + (cw * ecc->bytes);
1787
1788 ret = qcom_nandc_read_cw_raw(mtd, chip, cw_data_buf,
1789 cw_oob_buf, page, cw);
1790 if (ret)
1791 return ret;
1792
1793 /*
1794 * make sure it isn't an erased page reported
1795 * as not-erased by HW because of a few bitflips
1796 */
1797 ret = nand_check_erased_ecc_chunk(cw_data_buf, data_size,
1798 cw_oob_buf + host->bbm_size,
1799 oob_size, NULL,
1800 0, ecc->strength);
1801 if (ret < 0) {
1802 mtd->ecc_stats.failed++;
1803 } else {
1804 mtd->ecc_stats.corrected += ret;
1805 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1806 }
1807 }
1808
1809 return max_bitflips;
1810}
1811
1812/*
Archit Tanejac76b78d2016-02-03 14:29:50 +05301813 * reads back status registers set by the controller to notify page read
1814 * errors. this is equivalent to what 'ecc->correct()' would do.
1815 */
1816static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301817 u8 *oob_buf, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301818{
1819 struct nand_chip *chip = &host->chip;
1820 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1821 struct mtd_info *mtd = nand_to_mtd(chip);
1822 struct nand_ecc_ctrl *ecc = &chip->ecc;
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301823 unsigned int max_bitflips = 0, uncorrectable_cws = 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301824 struct read_stats *buf;
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301825 bool flash_op_err = false, erased;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301826 int i;
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301827 u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301828
1829 buf = (struct read_stats *)nandc->reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301830 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301831
1832 for (i = 0; i < ecc->steps; i++, buf++) {
1833 u32 flash, buffer, erased_cw;
1834 int data_len, oob_len;
1835
Md Sadre Alamb057e492021-02-24 01:08:58 +05301836 if (qcom_nandc_is_last_cw(ecc, i)) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05301837 data_len = ecc->size - ((ecc->steps - 1) << 2);
1838 oob_len = ecc->steps << 2;
1839 } else {
1840 data_len = host->cw_data;
1841 oob_len = 0;
1842 }
1843
1844 flash = le32_to_cpu(buf->flash);
1845 buffer = le32_to_cpu(buf->buffer);
1846 erased_cw = le32_to_cpu(buf->erased_cw);
1847
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301848 /*
1849 * Check ECC failure for each codeword. ECC failure can
1850 * happen in either of the following conditions
1851 * 1. If number of bitflips are greater than ECC engine
1852 * capability.
1853 * 2. If this codeword contains all 0xff for which erased
1854 * codeword detection check will be done.
1855 */
1856 if ((flash & FS_OP_ERR) && (buffer & BS_UNCORRECTABLE_BIT)) {
Abhishek Sahu2f610382018-06-20 12:57:35 +05301857 /*
1858 * For BCH ECC, ignore erased codeword errors, if
1859 * ERASED_CW bits are set.
1860 */
Archit Tanejac76b78d2016-02-03 14:29:50 +05301861 if (host->bch_enabled) {
Zhen Lei902f3322021-05-10 19:49:44 +08001862 erased = (erased_cw & ERASED_CW) == ERASED_CW;
Abhishek Sahu2f610382018-06-20 12:57:35 +05301863 /*
1864 * For RS ECC, HW reports the erased CW by placing
1865 * special characters at certain offsets in the buffer.
1866 * These special characters will be valid only if
1867 * complete page is read i.e. data_buf is not NULL.
1868 */
1869 } else if (data_buf) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05301870 erased = erased_chunk_check_and_fixup(data_buf,
1871 data_len);
Abhishek Sahu2f610382018-06-20 12:57:35 +05301872 } else {
1873 erased = false;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301874 }
1875
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301876 if (!erased)
1877 uncorrectable_cws |= BIT(i);
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301878 /*
1879 * Check if MPU or any other operational error (timeout,
1880 * device failure, etc.) happened for this codeword and
1881 * make flash_op_err true. If flash_op_err is set, then
1882 * EIO will be returned for page read.
1883 */
1884 } else if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1885 flash_op_err = true;
1886 /*
1887 * No ECC or operational errors happened. Check the number of
1888 * bits corrected and update the ecc_stats.corrected.
1889 */
Archit Tanejac76b78d2016-02-03 14:29:50 +05301890 } else {
1891 unsigned int stat;
1892
1893 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1894 mtd->ecc_stats.corrected += stat;
1895 max_bitflips = max(max_bitflips, stat);
1896 }
1897
Abhishek Sahu2f610382018-06-20 12:57:35 +05301898 if (data_buf)
1899 data_buf += data_len;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301900 if (oob_buf)
1901 oob_buf += oob_len + ecc->bytes;
1902 }
1903
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301904 if (flash_op_err)
1905 return -EIO;
1906
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301907 if (!uncorrectable_cws)
1908 return max_bitflips;
1909
1910 return check_for_erased_page(host, data_buf_start, oob_buf_start,
1911 uncorrectable_cws, page,
1912 max_bitflips);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301913}
1914
1915/*
1916 * helper to perform the actual page read operation, used by ecc->read_page(),
1917 * ecc->read_oob()
1918 */
1919static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301920 u8 *oob_buf, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301921{
1922 struct nand_chip *chip = &host->chip;
1923 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1924 struct nand_ecc_ctrl *ecc = &chip->ecc;
Abhishek Sahuadd0cfa2018-06-20 12:57:36 +05301925 u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301926 int i, ret;
1927
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05301928 config_nand_page_read(chip);
Abhishek Sahubde43302017-07-19 17:17:55 +05301929
Archit Tanejac76b78d2016-02-03 14:29:50 +05301930 /* queue cmd descs for each codeword */
1931 for (i = 0; i < ecc->steps; i++) {
1932 int data_size, oob_size;
1933
Md Sadre Alamb057e492021-02-24 01:08:58 +05301934 if (qcom_nandc_is_last_cw(ecc, i)) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05301935 data_size = ecc->size - ((ecc->steps - 1) << 2);
1936 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1937 host->spare_bytes;
1938 } else {
1939 data_size = host->cw_data;
1940 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1941 }
1942
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301943 if (nandc->props->is_bam) {
1944 if (data_buf && oob_buf) {
Md Sadre Alame7a307f2021-02-24 01:09:00 +05301945 nandc_set_read_loc(chip, i, 0, 0, data_size, 0);
1946 nandc_set_read_loc(chip, i, 1, data_size,
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301947 oob_size, 1);
1948 } else if (data_buf) {
Md Sadre Alame7a307f2021-02-24 01:09:00 +05301949 nandc_set_read_loc(chip, i, 0, 0, data_size, 1);
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301950 } else {
Md Sadre Alame7a307f2021-02-24 01:09:00 +05301951 nandc_set_read_loc(chip, i, 0, data_size,
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301952 oob_size, 1);
1953 }
1954 }
1955
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05301956 config_nand_cw_read(chip, true, i);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301957
1958 if (data_buf)
1959 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301960 data_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301961
1962 /*
1963 * when ecc is enabled, the controller doesn't read the real
1964 * or dummy bad block markers in each chunk. To maintain a
1965 * consistent layout across RAW and ECC reads, we just
1966 * leave the real/dummy BBM offsets empty (i.e, filled with
1967 * 0xffs)
1968 */
1969 if (oob_buf) {
1970 int j;
1971
1972 for (j = 0; j < host->bbm_size; j++)
1973 *oob_buf++ = 0xff;
1974
1975 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301976 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301977 }
1978
1979 if (data_buf)
1980 data_buf += data_size;
1981 if (oob_buf)
1982 oob_buf += oob_size;
1983 }
1984
1985 ret = submit_descs(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301986 free_descs(nandc);
1987
Abhishek Sahuadd0cfa2018-06-20 12:57:36 +05301988 if (ret) {
1989 dev_err(nandc->dev, "failure to read page/oob\n");
1990 return ret;
1991 }
1992
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301993 return parse_read_errors(host, data_buf_start, oob_buf_start, page);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301994}
1995
1996/*
1997 * a helper that copies the last step/codeword of a page (containing free oob)
1998 * into our local buffer
1999 */
2000static int copy_last_cw(struct qcom_nand_host *host, int page)
2001{
2002 struct nand_chip *chip = &host->chip;
2003 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2004 struct nand_ecc_ctrl *ecc = &chip->ecc;
2005 int size;
2006 int ret;
2007
2008 clear_read_regs(nandc);
2009
2010 size = host->use_ecc ? host->cw_data : host->cw_size;
2011
2012 /* prepare a clean read buffer */
2013 memset(nandc->data_buffer, 0xff, size);
2014
2015 set_address(host, host->cw_size * (ecc->steps - 1), page);
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05302016 update_rw_regs(host, 1, true, ecc->steps - 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302017
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05302018 config_nand_single_cw_page_read(chip, host->use_ecc, ecc->steps - 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302019
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302020 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302021
2022 ret = submit_descs(nandc);
2023 if (ret)
2024 dev_err(nandc->dev, "failed to copy last codeword\n");
2025
2026 free_descs(nandc);
2027
2028 return ret;
2029}
2030
2031/* implements ecc->read_page() */
Boris Brezillonb9761682018-09-06 14:05:20 +02002032static int qcom_nandc_read_page(struct nand_chip *chip, uint8_t *buf,
2033 int oob_required, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302034{
2035 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2036 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2037 u8 *data_buf, *oob_buf = NULL;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302038
Boris Brezillon25f815f2017-11-30 18:01:30 +01002039 nand_read_page_op(chip, page, 0, NULL, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302040 data_buf = buf;
2041 oob_buf = oob_required ? chip->oob_poi : NULL;
2042
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302043 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302044
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05302045 return read_page_ecc(host, data_buf, oob_buf, page);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302046}
2047
2048/* implements ecc->read_page_raw() */
Boris Brezillonb9761682018-09-06 14:05:20 +02002049static int qcom_nandc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
Archit Tanejac76b78d2016-02-03 14:29:50 +05302050 int oob_required, int page)
2051{
Boris Brezillonb9761682018-09-06 14:05:20 +02002052 struct mtd_info *mtd = nand_to_mtd(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302053 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302054 struct nand_ecc_ctrl *ecc = &chip->ecc;
Abhishek Sahu85632c12018-06-20 12:57:40 +05302055 int cw, ret;
2056 u8 *data_buf = buf, *oob_buf = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302057
Abhishek Sahu85632c12018-06-20 12:57:40 +05302058 for (cw = 0; cw < ecc->steps; cw++) {
2059 ret = qcom_nandc_read_cw_raw(mtd, chip, data_buf, oob_buf,
2060 page, cw);
2061 if (ret)
2062 return ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302063
Abhishek Sahu85632c12018-06-20 12:57:40 +05302064 data_buf += host->cw_data;
2065 oob_buf += ecc->bytes;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302066 }
2067
Abhishek Sahu85632c12018-06-20 12:57:40 +05302068 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302069}
2070
2071/* implements ecc->read_oob() */
Boris Brezillonb9761682018-09-06 14:05:20 +02002072static int qcom_nandc_read_oob(struct nand_chip *chip, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302073{
2074 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2075 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2076 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302077
2078 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302079 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302080
2081 host->use_ecc = true;
2082 set_address(host, 0, page);
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05302083 update_rw_regs(host, ecc->steps, true, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302084
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05302085 return read_page_ecc(host, NULL, chip->oob_poi, page);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302086}
2087
2088/* implements ecc->write_page() */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02002089static int qcom_nandc_write_page(struct nand_chip *chip, const uint8_t *buf,
2090 int oob_required, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302091{
2092 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2093 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2094 struct nand_ecc_ctrl *ecc = &chip->ecc;
2095 u8 *data_buf, *oob_buf;
2096 int i, ret;
2097
Boris Brezillon25f815f2017-11-30 18:01:30 +01002098 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2099
Archit Tanejac76b78d2016-02-03 14:29:50 +05302100 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302101 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302102
2103 data_buf = (u8 *)buf;
2104 oob_buf = chip->oob_poi;
2105
2106 host->use_ecc = true;
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05302107 update_rw_regs(host, ecc->steps, false, 0);
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05302108 config_nand_page_write(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302109
2110 for (i = 0; i < ecc->steps; i++) {
2111 int data_size, oob_size;
2112
Md Sadre Alamb057e492021-02-24 01:08:58 +05302113 if (qcom_nandc_is_last_cw(ecc, i)) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302114 data_size = ecc->size - ((ecc->steps - 1) << 2);
2115 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
2116 host->spare_bytes;
2117 } else {
2118 data_size = host->cw_data;
2119 oob_size = ecc->bytes;
2120 }
2121
Archit Tanejac76b78d2016-02-03 14:29:50 +05302122
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302123 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
2124 i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302125
2126 /*
2127 * when ECC is enabled, we don't really need to write anything
2128 * to oob for the first n - 1 codewords since these oob regions
2129 * just contain ECC bytes that's written by the controller
2130 * itself. For the last codeword, we skip the bbm positions and
2131 * write to the free oob area.
2132 */
Md Sadre Alamb057e492021-02-24 01:08:58 +05302133 if (qcom_nandc_is_last_cw(ecc, i)) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302134 oob_buf += host->bbm_size;
2135
2136 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302137 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302138 }
2139
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05302140 config_nand_cw_write(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302141
2142 data_buf += data_size;
2143 oob_buf += oob_size;
2144 }
2145
2146 ret = submit_descs(nandc);
2147 if (ret)
2148 dev_err(nandc->dev, "failure to write page\n");
2149
2150 free_descs(nandc);
2151
Boris Brezillon25f815f2017-11-30 18:01:30 +01002152 if (!ret)
2153 ret = nand_prog_page_end_op(chip);
2154
Archit Tanejac76b78d2016-02-03 14:29:50 +05302155 return ret;
2156}
2157
2158/* implements ecc->write_page_raw() */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02002159static int qcom_nandc_write_page_raw(struct nand_chip *chip,
2160 const uint8_t *buf, int oob_required,
2161 int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302162{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02002163 struct mtd_info *mtd = nand_to_mtd(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302164 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2165 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2166 struct nand_ecc_ctrl *ecc = &chip->ecc;
2167 u8 *data_buf, *oob_buf;
2168 int i, ret;
2169
Boris Brezillon25f815f2017-11-30 18:01:30 +01002170 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302171 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302172 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302173
2174 data_buf = (u8 *)buf;
2175 oob_buf = chip->oob_poi;
2176
2177 host->use_ecc = false;
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05302178 update_rw_regs(host, ecc->steps, false, 0);
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05302179 config_nand_page_write(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302180
2181 for (i = 0; i < ecc->steps; i++) {
2182 int data_size1, data_size2, oob_size1, oob_size2;
2183 int reg_off = FLASH_BUF_ACC;
2184
2185 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
2186 oob_size1 = host->bbm_size;
2187
Md Sadre Alamb057e492021-02-24 01:08:58 +05302188 if (qcom_nandc_is_last_cw(ecc, i)) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302189 data_size2 = ecc->size - data_size1 -
2190 ((ecc->steps - 1) << 2);
2191 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
2192 host->spare_bytes;
2193 } else {
2194 data_size2 = host->cw_data - data_size1;
2195 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
2196 }
2197
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302198 write_data_dma(nandc, reg_off, data_buf, data_size1,
2199 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302200 reg_off += data_size1;
2201 data_buf += data_size1;
2202
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302203 write_data_dma(nandc, reg_off, oob_buf, oob_size1,
2204 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302205 reg_off += oob_size1;
2206 oob_buf += oob_size1;
2207
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302208 write_data_dma(nandc, reg_off, data_buf, data_size2,
2209 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302210 reg_off += data_size2;
2211 data_buf += data_size2;
2212
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302213 write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302214 oob_buf += oob_size2;
2215
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05302216 config_nand_cw_write(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302217 }
2218
2219 ret = submit_descs(nandc);
2220 if (ret)
2221 dev_err(nandc->dev, "failure to write raw page\n");
2222
2223 free_descs(nandc);
2224
Boris Brezillon25f815f2017-11-30 18:01:30 +01002225 if (!ret)
2226 ret = nand_prog_page_end_op(chip);
2227
Archit Tanejac76b78d2016-02-03 14:29:50 +05302228 return ret;
2229}
2230
2231/*
2232 * implements ecc->write_oob()
2233 *
Abhishek Sahu28eed9f2018-06-20 12:57:37 +05302234 * the NAND controller cannot write only data or only OOB within a codeword
2235 * since ECC is calculated for the combined codeword. So update the OOB from
2236 * chip->oob_poi, and pad the data area with OxFF before writing.
Archit Tanejac76b78d2016-02-03 14:29:50 +05302237 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02002238static int qcom_nandc_write_oob(struct nand_chip *chip, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302239{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02002240 struct mtd_info *mtd = nand_to_mtd(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302241 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2242 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2243 struct nand_ecc_ctrl *ecc = &chip->ecc;
2244 u8 *oob = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302245 int data_size, oob_size;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002246 int ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302247
2248 host->use_ecc = true;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302249 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302250
2251 /* calculate the data and oob size for the last codeword/step */
2252 data_size = ecc->size - ((ecc->steps - 1) << 2);
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01002253 oob_size = mtd->oobavail;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302254
Abhishek Sahu28eed9f2018-06-20 12:57:37 +05302255 memset(nandc->data_buffer, 0xff, host->cw_data);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302256 /* override new oob content to last codeword */
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01002257 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
2258 0, mtd->oobavail);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302259
2260 set_address(host, host->cw_size * (ecc->steps - 1), page);
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05302261 update_rw_regs(host, 1, false, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302262
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05302263 config_nand_page_write(chip);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302264 write_data_dma(nandc, FLASH_BUF_ACC,
2265 nandc->data_buffer, data_size + oob_size, 0);
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05302266 config_nand_cw_write(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302267
2268 ret = submit_descs(nandc);
2269
2270 free_descs(nandc);
2271
2272 if (ret) {
2273 dev_err(nandc->dev, "failure to write oob\n");
2274 return -EIO;
2275 }
2276
Boris Brezillon97d90da2017-11-30 18:01:29 +01002277 return nand_prog_page_end_op(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302278}
2279
Boris Brezillonc17556f2018-09-06 14:05:25 +02002280static int qcom_nandc_block_bad(struct nand_chip *chip, loff_t ofs)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302281{
Boris Brezillonc17556f2018-09-06 14:05:25 +02002282 struct mtd_info *mtd = nand_to_mtd(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302283 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2284 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2285 struct nand_ecc_ctrl *ecc = &chip->ecc;
2286 int page, ret, bbpos, bad = 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302287
2288 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2289
2290 /*
2291 * configure registers for a raw sub page read, the address is set to
2292 * the beginning of the last codeword, we don't care about reading ecc
2293 * portion of oob. we just want the first few bytes from this codeword
2294 * that contains the BBM
2295 */
2296 host->use_ecc = false;
2297
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302298 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302299 ret = copy_last_cw(host, page);
2300 if (ret)
2301 goto err;
2302
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05302303 if (check_flash_errors(host, 1)) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302304 dev_warn(nandc->dev, "error when trying to read BBM\n");
2305 goto err;
2306 }
2307
2308 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
2309
2310 bad = nandc->data_buffer[bbpos] != 0xff;
2311
2312 if (chip->options & NAND_BUSWIDTH_16)
2313 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
2314err:
2315 return bad;
2316}
2317
Boris Brezillonc17556f2018-09-06 14:05:25 +02002318static int qcom_nandc_block_markbad(struct nand_chip *chip, loff_t ofs)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302319{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302320 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2321 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2322 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002323 int page, ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302324
2325 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302326 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302327
2328 /*
2329 * to mark the BBM as bad, we flash the entire last codeword with 0s.
2330 * we don't care about the rest of the content in the codeword since
2331 * we aren't going to use this block again
2332 */
2333 memset(nandc->data_buffer, 0x00, host->cw_size);
2334
2335 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2336
2337 /* prepare write */
2338 host->use_ecc = false;
2339 set_address(host, host->cw_size * (ecc->steps - 1), page);
Md Sadre Alam503ee5a2021-02-24 01:09:01 +05302340 update_rw_regs(host, 1, false, ecc->steps - 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302341
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05302342 config_nand_page_write(chip);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302343 write_data_dma(nandc, FLASH_BUF_ACC,
2344 nandc->data_buffer, host->cw_size, 0);
Md Sadre Alam9a7c39e2021-02-24 01:08:57 +05302345 config_nand_cw_write(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302346
2347 ret = submit_descs(nandc);
2348
2349 free_descs(nandc);
2350
2351 if (ret) {
2352 dev_err(nandc->dev, "failure to update BBM\n");
2353 return -EIO;
2354 }
2355
Boris Brezillon97d90da2017-11-30 18:01:29 +01002356 return nand_prog_page_end_op(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302357}
2358
2359/*
Boris Brezillon716bbba2018-09-07 00:38:35 +02002360 * the three functions below implement chip->legacy.read_byte(),
2361 * chip->legacy.read_buf() and chip->legacy.write_buf() respectively. these
2362 * aren't used for reading/writing page data, they are used for smaller data
2363 * like reading id, status etc
Archit Tanejac76b78d2016-02-03 14:29:50 +05302364 */
Boris Brezillon7e534322018-09-06 14:05:22 +02002365static uint8_t qcom_nandc_read_byte(struct nand_chip *chip)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302366{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302367 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2368 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2369 u8 *buf = nandc->data_buffer;
2370 u8 ret = 0x0;
2371
2372 if (host->last_command == NAND_CMD_STATUS) {
2373 ret = host->status;
2374
2375 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2376
2377 return ret;
2378 }
2379
2380 if (nandc->buf_start < nandc->buf_count)
2381 ret = buf[nandc->buf_start++];
2382
2383 return ret;
2384}
2385
Boris Brezillon7e534322018-09-06 14:05:22 +02002386static void qcom_nandc_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302387{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302388 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2389 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2390
2391 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
2392 nandc->buf_start += real_len;
2393}
2394
Boris Brezillonc0739d82018-09-06 14:05:23 +02002395static void qcom_nandc_write_buf(struct nand_chip *chip, const uint8_t *buf,
Archit Tanejac76b78d2016-02-03 14:29:50 +05302396 int len)
2397{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302398 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2399 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2400
2401 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
2402
2403 nandc->buf_start += real_len;
2404}
2405
2406/* we support only one external chip for now */
Boris Brezillon758b56f2018-09-06 14:05:24 +02002407static void qcom_nandc_select_chip(struct nand_chip *chip, int chipnr)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302408{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302409 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2410
2411 if (chipnr <= 0)
2412 return;
2413
2414 dev_warn(nandc->dev, "invalid chip select\n");
2415}
2416
2417/*
2418 * NAND controller page layout info
2419 *
2420 * Layout with ECC enabled:
2421 *
2422 * |----------------------| |---------------------------------|
2423 * | xx.......yy| | *********xx.......yy|
2424 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
2425 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
2426 * | xx.......yy| | *********xx.......yy|
2427 * |----------------------| |---------------------------------|
2428 * codeword 1,2..n-1 codeword n
2429 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
2430 *
2431 * n = Number of codewords in the page
2432 * . = ECC bytes
2433 * * = Spare/free bytes
2434 * x = Unused byte(s)
2435 * y = Reserved byte(s)
2436 *
2437 * 2K page: n = 4, spare = 16 bytes
2438 * 4K page: n = 8, spare = 32 bytes
2439 * 8K page: n = 16, spare = 64 bytes
2440 *
2441 * the qcom nand controller operates at a sub page/codeword level. each
2442 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2443 * the number of ECC bytes vary based on the ECC strength and the bus width.
2444 *
2445 * the first n - 1 codewords contains 516 bytes of user data, the remaining
2446 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2447 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2448 *
2449 * When we access a page with ECC enabled, the reserved bytes(s) are not
2450 * accessible at all. When reading, we fill up these unreadable positions
2451 * with 0xffs. When writing, the controller skips writing the inaccessible
2452 * bytes.
2453 *
2454 * Layout with ECC disabled:
2455 *
2456 * |------------------------------| |---------------------------------------|
2457 * | yy xx.......| | bb *********xx.......|
2458 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
2459 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
2460 * | yy xx.......| | bb *********xx.......|
2461 * |------------------------------| |---------------------------------------|
2462 * codeword 1,2..n-1 codeword n
2463 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
2464 *
2465 * n = Number of codewords in the page
2466 * . = ECC bytes
2467 * * = Spare/free bytes
2468 * x = Unused byte(s)
2469 * y = Dummy Bad Bock byte(s)
2470 * b = Real Bad Block byte(s)
2471 * size1/size2 = function of codeword size and 'n'
2472 *
2473 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2474 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2475 * Block Markers. In the last codeword, this position contains the real BBM
2476 *
2477 * In order to have a consistent layout between RAW and ECC modes, we assume
2478 * the following OOB layout arrangement:
2479 *
2480 * |-----------| |--------------------|
2481 * |yyxx.......| |bb*********xx.......|
2482 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
2483 * |yyxx.......| |bb*********xx.......|
2484 * |yyxx.......| |bb*********xx.......|
2485 * |-----------| |--------------------|
2486 * first n - 1 nth OOB region
2487 * OOB regions
2488 *
2489 * n = Number of codewords in the page
2490 * . = ECC bytes
2491 * * = FREE OOB bytes
2492 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2493 * x = Unused byte(s)
2494 * b = Real bad block byte(s) (inaccessible when ECC enabled)
2495 *
2496 * This layout is read as is when ECC is disabled. When ECC is enabled, the
2497 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2498 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
Boris Brezillon421e81c2016-03-18 17:54:27 +01002499 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2500 * the sum of the three).
Archit Tanejac76b78d2016-02-03 14:29:50 +05302501 */
Boris Brezillon421e81c2016-03-18 17:54:27 +01002502static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2503 struct mtd_oob_region *oobregion)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302504{
Boris Brezillon421e81c2016-03-18 17:54:27 +01002505 struct nand_chip *chip = mtd_to_nand(mtd);
2506 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302507 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302508
Boris Brezillon421e81c2016-03-18 17:54:27 +01002509 if (section > 1)
2510 return -ERANGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302511
Boris Brezillon421e81c2016-03-18 17:54:27 +01002512 if (!section) {
2513 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
2514 host->bbm_size;
2515 oobregion->offset = 0;
2516 } else {
2517 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
2518 oobregion->offset = mtd->oobsize - oobregion->length;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302519 }
2520
Boris Brezillon421e81c2016-03-18 17:54:27 +01002521 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302522}
2523
Boris Brezillon421e81c2016-03-18 17:54:27 +01002524static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
2525 struct mtd_oob_region *oobregion)
2526{
2527 struct nand_chip *chip = mtd_to_nand(mtd);
2528 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2529 struct nand_ecc_ctrl *ecc = &chip->ecc;
2530
2531 if (section)
2532 return -ERANGE;
2533
2534 oobregion->length = ecc->steps * 4;
2535 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
2536
2537 return 0;
2538}
2539
2540static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
2541 .ecc = qcom_nand_ooblayout_ecc,
2542 .free = qcom_nand_ooblayout_free,
2543};
2544
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302545static int
2546qcom_nandc_calc_ecc_bytes(int step_size, int strength)
2547{
2548 return strength == 4 ? 12 : 16;
2549}
2550NAND_ECC_CAPS_SINGLE(qcom_nandc_ecc_caps, qcom_nandc_calc_ecc_bytes,
2551 NANDC_STEP_SIZE, 4, 8);
2552
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002553static int qcom_nand_attach_chip(struct nand_chip *chip)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302554{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302555 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002556 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302557 struct nand_ecc_ctrl *ecc = &chip->ecc;
2558 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302559 int cwperpage, bad_block_byte, ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302560 bool wide_bus;
2561 int ecc_mode = 1;
2562
Abhishek Sahu320bdb52018-06-20 12:57:31 +05302563 /* controller only supports 512 bytes data steps */
2564 ecc->size = NANDC_STEP_SIZE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302565 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302566 cwperpage = mtd->writesize / NANDC_STEP_SIZE;
2567
2568 /*
2569 * Each CW has 4 available OOB bytes which will be protected with ECC
2570 * so remaining bytes can be used for ECC.
2571 */
2572 ret = nand_ecc_choose_conf(chip, &qcom_nandc_ecc_caps,
2573 mtd->oobsize - (cwperpage * 4));
2574 if (ret) {
2575 dev_err(nandc->dev, "No valid ECC settings possible\n");
2576 return ret;
2577 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302578
2579 if (ecc->strength >= 8) {
2580 /* 8 bit ECC defaults to BCH ECC on all platforms */
2581 host->bch_enabled = true;
2582 ecc_mode = 1;
2583
2584 if (wide_bus) {
2585 host->ecc_bytes_hw = 14;
2586 host->spare_bytes = 0;
2587 host->bbm_size = 2;
2588 } else {
2589 host->ecc_bytes_hw = 13;
2590 host->spare_bytes = 2;
2591 host->bbm_size = 1;
2592 }
2593 } else {
2594 /*
2595 * if the controller supports BCH for 4 bit ECC, the controller
2596 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2597 * always 10 bytes
2598 */
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302599 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302600 /* BCH */
2601 host->bch_enabled = true;
2602 ecc_mode = 0;
2603
2604 if (wide_bus) {
2605 host->ecc_bytes_hw = 8;
2606 host->spare_bytes = 2;
2607 host->bbm_size = 2;
2608 } else {
2609 host->ecc_bytes_hw = 7;
2610 host->spare_bytes = 4;
2611 host->bbm_size = 1;
2612 }
2613 } else {
2614 /* RS */
2615 host->ecc_bytes_hw = 10;
2616
2617 if (wide_bus) {
2618 host->spare_bytes = 0;
2619 host->bbm_size = 2;
2620 } else {
2621 host->spare_bytes = 1;
2622 host->bbm_size = 1;
2623 }
2624 }
2625 }
2626
2627 /*
2628 * we consider ecc->bytes as the sum of all the non-data content in a
2629 * step. It gives us a clean representation of the oob area (even if
2630 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2631 * ECC and 12 bytes for 4 bit ECC
2632 */
2633 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
2634
2635 ecc->read_page = qcom_nandc_read_page;
2636 ecc->read_page_raw = qcom_nandc_read_page_raw;
2637 ecc->read_oob = qcom_nandc_read_oob;
2638 ecc->write_page = qcom_nandc_write_page;
2639 ecc->write_page_raw = qcom_nandc_write_page_raw;
2640 ecc->write_oob = qcom_nandc_write_oob;
2641
Miquel Raynalbace41f2020-08-27 10:51:58 +02002642 ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302643
Boris Brezillon421e81c2016-03-18 17:54:27 +01002644 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302645
Abhishek Sahucb80f112017-08-17 17:37:40 +05302646 nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
2647 cwperpage);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302648
2649 /*
2650 * DATA_UD_BYTES varies based on whether the read/write command protects
2651 * spare data with ECC too. We protect spare data by default, so we set
2652 * it to main + spare data, which are 512 and 4 bytes respectively.
2653 */
2654 host->cw_data = 516;
2655
2656 /*
2657 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2658 * for 8 bit ECC
2659 */
2660 host->cw_size = host->cw_data + ecc->bytes;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302661 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
2662
2663 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
2664 | host->cw_data << UD_SIZE_BYTES
2665 | 0 << DISABLE_STATUS_AFTER_WRITE
2666 | 5 << NUM_ADDR_CYCLES
2667 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
2668 | 0 << STATUS_BFR_READ
2669 | 1 << SET_RD_MODE_AFTER_STATUS
2670 | host->spare_bytes << SPARE_SIZE_BYTES;
2671
2672 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
2673 | 0 << CS_ACTIVE_BSY
2674 | bad_block_byte << BAD_BLOCK_BYTE_NUM
2675 | 0 << BAD_BLOCK_IN_SPARE_AREA
2676 | 2 << WR_RD_BSY_GAP
2677 | wide_bus << WIDE_FLASH
2678 | host->bch_enabled << ENABLE_BCH_ECC;
2679
2680 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
2681 | host->cw_size << UD_SIZE_BYTES
2682 | 5 << NUM_ADDR_CYCLES
2683 | 0 << SPARE_SIZE_BYTES;
2684
2685 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
2686 | 0 << CS_ACTIVE_BSY
2687 | 17 << BAD_BLOCK_BYTE_NUM
2688 | 1 << BAD_BLOCK_IN_SPARE_AREA
2689 | 2 << WR_RD_BSY_GAP
2690 | wide_bus << WIDE_FLASH
2691 | 1 << DEV0_CFG1_ECC_DISABLE;
2692
Abhishek Sahu10777de2017-08-03 17:56:39 +02002693 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
Archit Tanejac76b78d2016-02-03 14:29:50 +05302694 | 0 << ECC_SW_RESET
2695 | host->cw_data << ECC_NUM_DATA_BYTES
2696 | 1 << ECC_FORCE_CLK_OPEN
2697 | ecc_mode << ECC_MODE
2698 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
2699
Md Sadre Alambfb34ec2021-06-08 12:18:36 +05302700 if (!nandc->props->qpic_v2)
2701 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302702
2703 host->clrflashstatus = FS_READY_BSY_N;
2704 host->clrreadstatus = 0xc0;
Abhishek Sahua86b9c42017-08-17 17:37:44 +05302705 nandc->regs->erased_cw_detect_cfg_clr =
2706 cpu_to_le32(CLR_ERASED_PAGE_DET);
2707 nandc->regs->erased_cw_detect_cfg_set =
2708 cpu_to_le32(SET_ERASED_PAGE_DET);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302709
2710 dev_dbg(nandc->dev,
2711 "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",
2712 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
2713 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
2714 cwperpage);
2715
2716 return 0;
2717}
2718
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002719static const struct nand_controller_ops qcom_nandc_ops = {
2720 .attach_chip = qcom_nand_attach_chip,
2721};
2722
Peter Ujfalusi80c30122020-02-27 14:37:47 +02002723static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2724{
2725 if (nandc->props->is_bam) {
2726 if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
2727 dma_unmap_single(nandc->dev, nandc->reg_read_dma,
2728 MAX_REG_RD *
2729 sizeof(*nandc->reg_read_buf),
2730 DMA_FROM_DEVICE);
2731
2732 if (nandc->tx_chan)
2733 dma_release_channel(nandc->tx_chan);
2734
2735 if (nandc->rx_chan)
2736 dma_release_channel(nandc->rx_chan);
2737
2738 if (nandc->cmd_chan)
2739 dma_release_channel(nandc->cmd_chan);
2740 } else {
2741 if (nandc->chan)
2742 dma_release_channel(nandc->chan);
2743 }
2744}
2745
Archit Tanejac76b78d2016-02-03 14:29:50 +05302746static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
2747{
2748 int ret;
2749
2750 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
2751 if (ret) {
2752 dev_err(nandc->dev, "failed to set DMA mask\n");
2753 return ret;
2754 }
2755
2756 /*
2757 * we use the internal buffer for reading ONFI params, reading small
2758 * data like ID and status, and preforming read-copy-write operations
2759 * when writing to a codeword partially. 532 is the maximum possible
2760 * size of a codeword for our nand controller
2761 */
2762 nandc->buf_size = 532;
2763
2764 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
2765 GFP_KERNEL);
2766 if (!nandc->data_buffer)
2767 return -ENOMEM;
2768
2769 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
2770 GFP_KERNEL);
2771 if (!nandc->regs)
2772 return -ENOMEM;
2773
Kees Cooka86854d2018-06-12 14:07:58 -07002774 nandc->reg_read_buf = devm_kcalloc(nandc->dev,
2775 MAX_REG_RD, sizeof(*nandc->reg_read_buf),
Archit Tanejac76b78d2016-02-03 14:29:50 +05302776 GFP_KERNEL);
2777 if (!nandc->reg_read_buf)
2778 return -ENOMEM;
2779
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302780 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302781 nandc->reg_read_dma =
2782 dma_map_single(nandc->dev, nandc->reg_read_buf,
2783 MAX_REG_RD *
2784 sizeof(*nandc->reg_read_buf),
2785 DMA_FROM_DEVICE);
2786 if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
2787 dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
2788 return -EIO;
2789 }
2790
Peter Ujfalusi92f0f8e2020-02-27 14:37:48 +02002791 nandc->tx_chan = dma_request_chan(nandc->dev, "tx");
2792 if (IS_ERR(nandc->tx_chan)) {
2793 ret = PTR_ERR(nandc->tx_chan);
2794 nandc->tx_chan = NULL;
Krzysztof Kozlowski07eb0142020-09-01 16:25:35 +02002795 dev_err_probe(nandc->dev, ret,
2796 "tx DMA channel request failed\n");
Peter Ujfalusi80c30122020-02-27 14:37:47 +02002797 goto unalloc;
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302798 }
2799
Peter Ujfalusi92f0f8e2020-02-27 14:37:48 +02002800 nandc->rx_chan = dma_request_chan(nandc->dev, "rx");
2801 if (IS_ERR(nandc->rx_chan)) {
2802 ret = PTR_ERR(nandc->rx_chan);
2803 nandc->rx_chan = NULL;
Krzysztof Kozlowski07eb0142020-09-01 16:25:35 +02002804 dev_err_probe(nandc->dev, ret,
2805 "rx DMA channel request failed\n");
Peter Ujfalusi80c30122020-02-27 14:37:47 +02002806 goto unalloc;
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302807 }
2808
Peter Ujfalusi92f0f8e2020-02-27 14:37:48 +02002809 nandc->cmd_chan = dma_request_chan(nandc->dev, "cmd");
2810 if (IS_ERR(nandc->cmd_chan)) {
2811 ret = PTR_ERR(nandc->cmd_chan);
2812 nandc->cmd_chan = NULL;
Krzysztof Kozlowski07eb0142020-09-01 16:25:35 +02002813 dev_err_probe(nandc->dev, ret,
2814 "cmd DMA channel request failed\n");
Peter Ujfalusi80c30122020-02-27 14:37:47 +02002815 goto unalloc;
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302816 }
Abhishek Sahucb80f112017-08-17 17:37:40 +05302817
2818 /*
2819 * Initially allocate BAM transaction to read ONFI param page.
2820 * After detecting all the devices, this BAM transaction will
2821 * be freed and the next BAM tranasction will be allocated with
2822 * maximum codeword size
2823 */
2824 nandc->max_cwperpage = 1;
2825 nandc->bam_txn = alloc_bam_transaction(nandc);
2826 if (!nandc->bam_txn) {
2827 dev_err(nandc->dev,
2828 "failed to allocate bam transaction\n");
Peter Ujfalusi80c30122020-02-27 14:37:47 +02002829 ret = -ENOMEM;
2830 goto unalloc;
Abhishek Sahucb80f112017-08-17 17:37:40 +05302831 }
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302832 } else {
Peter Ujfalusi92f0f8e2020-02-27 14:37:48 +02002833 nandc->chan = dma_request_chan(nandc->dev, "rxtx");
2834 if (IS_ERR(nandc->chan)) {
2835 ret = PTR_ERR(nandc->chan);
2836 nandc->chan = NULL;
Krzysztof Kozlowski07eb0142020-09-01 16:25:35 +02002837 dev_err_probe(nandc->dev, ret,
2838 "rxtx DMA channel request failed\n");
Peter Ujfalusi92f0f8e2020-02-27 14:37:48 +02002839 return ret;
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302840 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302841 }
2842
2843 INIT_LIST_HEAD(&nandc->desc_list);
2844 INIT_LIST_HEAD(&nandc->host_list);
2845
Miquel Raynal7da45132018-07-17 09:08:02 +02002846 nand_controller_init(&nandc->controller);
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002847 nandc->controller.ops = &qcom_nandc_ops;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302848
2849 return 0;
Peter Ujfalusi80c30122020-02-27 14:37:47 +02002850unalloc:
2851 qcom_nandc_unalloc(nandc);
2852 return ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302853}
2854
2855/* one time setup of a few nand controller registers */
2856static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2857{
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302858 u32 nand_ctrl;
2859
Archit Tanejac76b78d2016-02-03 14:29:50 +05302860 /* kill onenand */
Sivaprakash Murugesan443440c2020-06-12 13:28:15 +05302861 if (!nandc->props->is_qpic)
2862 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05302863
2864 if (!nandc->props->qpic_v2)
2865 nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD),
2866 NAND_DEV_CMD_VLD_VAL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302867
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302868 /* enable ADM or BAM DMA */
2869 if (nandc->props->is_bam) {
2870 nand_ctrl = nandc_read(nandc, NAND_CTRL);
Sivaprakash Murugesancb272392020-06-12 13:28:16 +05302871
2872 /*
2873 *NAND_CTRL is an operational registers, and CPU
2874 * access to operational registers are read only
2875 * in BAM mode. So update the NAND_CTRL register
2876 * only if it is not in BAM mode. In most cases BAM
2877 * mode will be enabled in bootloader
2878 */
2879 if (!(nand_ctrl & BAM_MODE_EN))
2880 nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302881 } else {
2882 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2883 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302884
2885 /* save the original values of these registers */
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05302886 if (!nandc->props->qpic_v2) {
2887 nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1));
2888 nandc->vld = NAND_DEV_CMD_VLD_VAL;
2889 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302890
2891 return 0;
2892}
2893
Baruch Siach21020be2021-04-21 21:31:09 +03002894static const char * const probes[] = { "cmdlinepart", "ofpart", "qcomsmem", NULL };
Manivannan Sadhasivam82bfd112021-01-04 09:41:36 +05302895
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002896static int qcom_nand_host_init_and_register(struct qcom_nand_controller *nandc,
2897 struct qcom_nand_host *host,
2898 struct device_node *dn)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302899{
2900 struct nand_chip *chip = &host->chip;
2901 struct mtd_info *mtd = nand_to_mtd(chip);
2902 struct device *dev = nandc->dev;
2903 int ret;
2904
2905 ret = of_property_read_u32(dn, "reg", &host->cs);
2906 if (ret) {
2907 dev_err(dev, "can't get chip-select\n");
2908 return -ENXIO;
2909 }
2910
2911 nand_set_flash_node(chip, dn);
2912 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
Fabio Estevam069f0532018-01-05 18:02:55 -02002913 if (!mtd->name)
2914 return -ENOMEM;
2915
Archit Tanejac76b78d2016-02-03 14:29:50 +05302916 mtd->owner = THIS_MODULE;
2917 mtd->dev.parent = dev;
2918
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002919 chip->legacy.cmdfunc = qcom_nandc_command;
Boris Brezillon7d6c37e2018-11-11 08:55:22 +01002920 chip->legacy.select_chip = qcom_nandc_select_chip;
Boris Brezillon716bbba2018-09-07 00:38:35 +02002921 chip->legacy.read_byte = qcom_nandc_read_byte;
2922 chip->legacy.read_buf = qcom_nandc_read_buf;
2923 chip->legacy.write_buf = qcom_nandc_write_buf;
Boris Brezillon45240362018-09-07 00:38:40 +02002924 chip->legacy.set_features = nand_get_set_features_notsupp;
2925 chip->legacy.get_features = nand_get_set_features_notsupp;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302926
2927 /*
2928 * the bad block marker is readable only when we read the last codeword
2929 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2930 * helpers don't allow us to read BB from a nand chip with ECC
2931 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2932 * and block_markbad helpers until we permanently switch to using
2933 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2934 */
Boris Brezilloncdc784c2018-09-07 00:38:38 +02002935 chip->legacy.block_bad = qcom_nandc_block_bad;
2936 chip->legacy.block_markbad = qcom_nandc_block_markbad;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302937
2938 chip->controller = &nandc->controller;
Miquel Raynalce8148d2020-05-07 12:52:31 +02002939 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USES_DMA |
Archit Tanejac76b78d2016-02-03 14:29:50 +05302940 NAND_SKIP_BBTSCAN;
2941
2942 /* set up initial status value */
2943 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2944
Boris Brezillon00ad3782018-09-06 14:05:14 +02002945 ret = nand_scan(chip, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302946 if (ret)
2947 return ret;
2948
Christian Lamparter81d9bdf2018-12-23 01:31:26 +01002949 if (nandc->props->is_bam) {
2950 free_bam_transaction(nandc);
2951 nandc->bam_txn = alloc_bam_transaction(nandc);
2952 if (!nandc->bam_txn) {
2953 dev_err(nandc->dev,
2954 "failed to allocate bam transaction\n");
Manivannan Sadhasivamab2c8d32021-04-02 20:31:28 +05302955 nand_cleanup(chip);
Christian Lamparter81d9bdf2018-12-23 01:31:26 +01002956 return -ENOMEM;
2957 }
2958 }
2959
Manivannan Sadhasivam82bfd112021-01-04 09:41:36 +05302960 ret = mtd_device_parse_register(mtd, probes, NULL, NULL, 0);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302961 if (ret)
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002962 nand_cleanup(chip);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302963
2964 return ret;
2965}
2966
2967static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2968{
2969 struct device *dev = nandc->dev;
2970 struct device_node *dn = dev->of_node, *child;
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002971 struct qcom_nand_host *host;
Manivannan Sadhasivam55fbb9b2021-03-02 18:57:57 +05302972 int ret = -ENODEV;
Abhishek Sahu89f51272017-07-19 17:17:58 +05302973
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002974 for_each_available_child_of_node(dn, child) {
2975 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2976 if (!host) {
2977 of_node_put(child);
2978 return -ENOMEM;
Abhishek Sahu89f51272017-07-19 17:17:58 +05302979 }
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002980
2981 ret = qcom_nand_host_init_and_register(nandc, host, child);
2982 if (ret) {
2983 devm_kfree(dev, host);
2984 continue;
2985 }
2986
2987 list_add_tail(&host->node, &nandc->host_list);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302988 }
2989
Manivannan Sadhasivam55fbb9b2021-03-02 18:57:57 +05302990 return ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302991}
2992
2993/* parse custom DT properties here */
2994static int qcom_nandc_parse_dt(struct platform_device *pdev)
2995{
2996 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2997 struct device_node *np = nandc->dev->of_node;
2998 int ret;
2999
Abhishek Sahu497d7d82017-08-11 17:09:19 +05303000 if (!nandc->props->is_bam) {
3001 ret = of_property_read_u32(np, "qcom,cmd-crci",
3002 &nandc->cmd_crci);
3003 if (ret) {
3004 dev_err(nandc->dev, "command CRCI unspecified\n");
3005 return ret;
3006 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05303007
Abhishek Sahu497d7d82017-08-11 17:09:19 +05303008 ret = of_property_read_u32(np, "qcom,data-crci",
3009 &nandc->data_crci);
3010 if (ret) {
3011 dev_err(nandc->dev, "data CRCI unspecified\n");
3012 return ret;
3013 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05303014 }
3015
3016 return 0;
3017}
3018
3019static int qcom_nandc_probe(struct platform_device *pdev)
3020{
3021 struct qcom_nand_controller *nandc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05303022 const void *dev_data;
3023 struct device *dev = &pdev->dev;
Archit Tanejac76b78d2016-02-03 14:29:50 +05303024 struct resource *res;
3025 int ret;
3026
3027 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
3028 if (!nandc)
3029 return -ENOMEM;
3030
3031 platform_set_drvdata(pdev, nandc);
3032 nandc->dev = dev;
3033
3034 dev_data = of_device_get_match_data(dev);
3035 if (!dev_data) {
3036 dev_err(&pdev->dev, "failed to get device data\n");
3037 return -ENODEV;
3038 }
3039
Abhishek Sahu58f1f222017-08-11 17:09:17 +05303040 nandc->props = dev_data;
Archit Tanejac76b78d2016-02-03 14:29:50 +05303041
Archit Tanejac76b78d2016-02-03 14:29:50 +05303042 nandc->core_clk = devm_clk_get(dev, "core");
3043 if (IS_ERR(nandc->core_clk))
3044 return PTR_ERR(nandc->core_clk);
3045
3046 nandc->aon_clk = devm_clk_get(dev, "aon");
3047 if (IS_ERR(nandc->aon_clk))
3048 return PTR_ERR(nandc->aon_clk);
3049
3050 ret = qcom_nandc_parse_dt(pdev);
3051 if (ret)
3052 return ret;
3053
Arnd Bergmann7330fc52018-07-17 22:27:42 +02003054 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3055 nandc->base = devm_ioremap_resource(dev, res);
3056 if (IS_ERR(nandc->base))
3057 return PTR_ERR(nandc->base);
3058
3059 nandc->base_phys = res->start;
3060 nandc->base_dma = dma_map_resource(dev, res->start,
3061 resource_size(res),
3062 DMA_BIDIRECTIONAL, 0);
Manivannan Sadhasivam32cbc7c2021-04-05 10:39:12 +05303063 if (dma_mapping_error(dev, nandc->base_dma))
Arnd Bergmann7330fc52018-07-17 22:27:42 +02003064 return -ENXIO;
3065
Archit Tanejac76b78d2016-02-03 14:29:50 +05303066 ret = qcom_nandc_alloc(nandc);
3067 if (ret)
Arnd Bergmann7330fc52018-07-17 22:27:42 +02003068 goto err_nandc_alloc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05303069
3070 ret = clk_prepare_enable(nandc->core_clk);
3071 if (ret)
3072 goto err_core_clk;
3073
3074 ret = clk_prepare_enable(nandc->aon_clk);
3075 if (ret)
3076 goto err_aon_clk;
3077
3078 ret = qcom_nandc_setup(nandc);
3079 if (ret)
3080 goto err_setup;
3081
Abhishek Sahu89f51272017-07-19 17:17:58 +05303082 ret = qcom_probe_nand_devices(nandc);
3083 if (ret)
3084 goto err_setup;
Archit Tanejac76b78d2016-02-03 14:29:50 +05303085
3086 return 0;
3087
Archit Tanejac76b78d2016-02-03 14:29:50 +05303088err_setup:
3089 clk_disable_unprepare(nandc->aon_clk);
3090err_aon_clk:
3091 clk_disable_unprepare(nandc->core_clk);
3092err_core_clk:
3093 qcom_nandc_unalloc(nandc);
Arnd Bergmann7330fc52018-07-17 22:27:42 +02003094err_nandc_alloc:
3095 dma_unmap_resource(dev, res->start, resource_size(res),
3096 DMA_BIDIRECTIONAL, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05303097
3098 return ret;
3099}
3100
3101static int qcom_nandc_remove(struct platform_device *pdev)
3102{
3103 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
Arnd Bergmann7330fc52018-07-17 22:27:42 +02003104 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05303105 struct qcom_nand_host *host;
Miquel Raynal0a2bc992020-05-19 15:00:17 +02003106 struct nand_chip *chip;
3107 int ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05303108
Miquel Raynal0a2bc992020-05-19 15:00:17 +02003109 list_for_each_entry(host, &nandc->host_list, node) {
3110 chip = &host->chip;
3111 ret = mtd_device_unregister(nand_to_mtd(chip));
3112 WARN_ON(ret);
3113 nand_cleanup(chip);
3114 }
Arnd Bergmann7330fc52018-07-17 22:27:42 +02003115
Archit Tanejac76b78d2016-02-03 14:29:50 +05303116 qcom_nandc_unalloc(nandc);
3117
3118 clk_disable_unprepare(nandc->aon_clk);
3119 clk_disable_unprepare(nandc->core_clk);
3120
Arnd Bergmann7330fc52018-07-17 22:27:42 +02003121 dma_unmap_resource(&pdev->dev, nandc->base_dma, resource_size(res),
3122 DMA_BIDIRECTIONAL, 0);
3123
Archit Tanejac76b78d2016-02-03 14:29:50 +05303124 return 0;
3125}
3126
Abhishek Sahu58f1f222017-08-11 17:09:17 +05303127static const struct qcom_nandc_props ipq806x_nandc_props = {
3128 .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +05303129 .is_bam = false,
Abhishek Sahucc409b92017-08-17 17:37:47 +05303130 .dev_cmd_reg_start = 0x0,
Abhishek Sahu58f1f222017-08-11 17:09:17 +05303131};
Archit Tanejac76b78d2016-02-03 14:29:50 +05303132
Abhishek Sahua0637832017-08-17 17:37:53 +05303133static const struct qcom_nandc_props ipq4019_nandc_props = {
3134 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3135 .is_bam = true,
Sivaprakash Murugesan443440c2020-06-12 13:28:15 +05303136 .is_qpic = true,
Abhishek Sahua0637832017-08-17 17:37:53 +05303137 .dev_cmd_reg_start = 0x0,
3138};
3139
Abhishek Sahudce84762017-08-17 17:37:54 +05303140static const struct qcom_nandc_props ipq8074_nandc_props = {
3141 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3142 .is_bam = true,
Sivaprakash Murugesan443440c2020-06-12 13:28:15 +05303143 .is_qpic = true,
Abhishek Sahudce84762017-08-17 17:37:54 +05303144 .dev_cmd_reg_start = 0x7000,
3145};
3146
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05303147static const struct qcom_nandc_props sdx55_nandc_props = {
3148 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3149 .is_bam = true,
3150 .is_qpic = true,
3151 .qpic_v2 = true,
3152 .dev_cmd_reg_start = 0x7000,
3153};
3154
Archit Tanejac76b78d2016-02-03 14:29:50 +05303155/*
3156 * data will hold a struct pointer containing more differences once we support
3157 * more controller variants
3158 */
3159static const struct of_device_id qcom_nandc_of_match[] = {
Abhishek Sahu58f1f222017-08-11 17:09:17 +05303160 {
3161 .compatible = "qcom,ipq806x-nand",
3162 .data = &ipq806x_nandc_props,
Archit Tanejac76b78d2016-02-03 14:29:50 +05303163 },
Abhishek Sahua0637832017-08-17 17:37:53 +05303164 {
3165 .compatible = "qcom,ipq4019-nand",
3166 .data = &ipq4019_nandc_props,
3167 },
Abhishek Sahudce84762017-08-17 17:37:54 +05303168 {
Kathiravan T62858622020-10-13 10:45:23 +05303169 .compatible = "qcom,ipq6018-nand",
3170 .data = &ipq8074_nandc_props,
3171 },
3172 {
Abhishek Sahudce84762017-08-17 17:37:54 +05303173 .compatible = "qcom,ipq8074-nand",
3174 .data = &ipq8074_nandc_props,
3175 },
Manivannan Sadhasivamb1209582020-11-26 14:27:05 +05303176 {
3177 .compatible = "qcom,sdx55-nand",
3178 .data = &sdx55_nandc_props,
3179 },
Archit Tanejac76b78d2016-02-03 14:29:50 +05303180 {}
3181};
3182MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
3183
3184static struct platform_driver qcom_nandc_driver = {
3185 .driver = {
3186 .name = "qcom-nandc",
3187 .of_match_table = qcom_nandc_of_match,
3188 },
3189 .probe = qcom_nandc_probe,
3190 .remove = qcom_nandc_remove,
3191};
3192module_platform_driver(qcom_nandc_driver);
3193
3194MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
3195MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
3196MODULE_LICENSE("GPL v2");