blob: a831f9cb458a4658ca023ab5a5ff5d8235587444 [file] [log] [blame]
Archit Tanejac76b78d2016-02-03 14:29:50 +05301/*
2 * Copyright (c) 2016, The Linux Foundation. All rights reserved.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/clk.h>
15#include <linux/slab.h>
16#include <linux/bitops.h>
17#include <linux/dma-mapping.h>
18#include <linux/dmaengine.h>
19#include <linux/module.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020020#include <linux/mtd/rawnand.h>
Archit Tanejac76b78d2016-02-03 14:29:50 +053021#include <linux/mtd/partitions.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
Archit Tanejac76b78d2016-02-03 14:29:50 +053024#include <linux/delay.h>
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +053025#include <linux/dma/qcom_bam_dma.h>
Christoph Hellwigea8c64a2018-01-10 16:21:13 +010026#include <linux/dma-direct.h> /* XXX: drivers shall never use this directly! */
Archit Tanejac76b78d2016-02-03 14:29:50 +053027
28/* NANDc reg offsets */
29#define NAND_FLASH_CMD 0x00
30#define NAND_ADDR0 0x04
31#define NAND_ADDR1 0x08
32#define NAND_FLASH_CHIP_SELECT 0x0c
33#define NAND_EXEC_CMD 0x10
34#define NAND_FLASH_STATUS 0x14
35#define NAND_BUFFER_STATUS 0x18
36#define NAND_DEV0_CFG0 0x20
37#define NAND_DEV0_CFG1 0x24
38#define NAND_DEV0_ECC_CFG 0x28
39#define NAND_DEV1_ECC_CFG 0x2c
40#define NAND_DEV1_CFG0 0x30
41#define NAND_DEV1_CFG1 0x34
42#define NAND_READ_ID 0x40
43#define NAND_READ_STATUS 0x44
44#define NAND_DEV_CMD0 0xa0
45#define NAND_DEV_CMD1 0xa4
46#define NAND_DEV_CMD2 0xa8
47#define NAND_DEV_CMD_VLD 0xac
48#define SFLASHC_BURST_CFG 0xe0
49#define NAND_ERASED_CW_DETECT_CFG 0xe8
50#define NAND_ERASED_CW_DETECT_STATUS 0xec
51#define NAND_EBI2_ECC_BUF_CFG 0xf0
52#define FLASH_BUF_ACC 0x100
53
54#define NAND_CTRL 0xf00
55#define NAND_VERSION 0xf08
56#define NAND_READ_LOCATION_0 0xf20
57#define NAND_READ_LOCATION_1 0xf24
Abhishek Sahu91af95c2017-08-17 17:37:43 +053058#define NAND_READ_LOCATION_2 0xf28
59#define NAND_READ_LOCATION_3 0xf2c
Archit Tanejac76b78d2016-02-03 14:29:50 +053060
61/* dummy register offsets, used by write_reg_dma */
62#define NAND_DEV_CMD1_RESTORE 0xdead
63#define NAND_DEV_CMD_VLD_RESTORE 0xbeef
64
65/* NAND_FLASH_CMD bits */
66#define PAGE_ACC BIT(4)
67#define LAST_PAGE BIT(5)
68
69/* NAND_FLASH_CHIP_SELECT bits */
70#define NAND_DEV_SEL 0
71#define DM_EN BIT(2)
72
73/* NAND_FLASH_STATUS bits */
74#define FS_OP_ERR BIT(4)
75#define FS_READY_BSY_N BIT(5)
76#define FS_MPU_ERR BIT(8)
77#define FS_DEVICE_STS_ERR BIT(16)
78#define FS_DEVICE_WP BIT(23)
79
80/* NAND_BUFFER_STATUS bits */
81#define BS_UNCORRECTABLE_BIT BIT(8)
82#define BS_CORRECTABLE_ERR_MSK 0x1f
83
84/* NAND_DEVn_CFG0 bits */
85#define DISABLE_STATUS_AFTER_WRITE 4
86#define CW_PER_PAGE 6
87#define UD_SIZE_BYTES 9
88#define ECC_PARITY_SIZE_BYTES_RS 19
89#define SPARE_SIZE_BYTES 23
90#define NUM_ADDR_CYCLES 27
91#define STATUS_BFR_READ 30
92#define SET_RD_MODE_AFTER_STATUS 31
93
94/* NAND_DEVn_CFG0 bits */
95#define DEV0_CFG1_ECC_DISABLE 0
96#define WIDE_FLASH 1
97#define NAND_RECOVERY_CYCLES 2
98#define CS_ACTIVE_BSY 5
99#define BAD_BLOCK_BYTE_NUM 6
100#define BAD_BLOCK_IN_SPARE_AREA 16
101#define WR_RD_BSY_GAP 17
102#define ENABLE_BCH_ECC 27
103
104/* NAND_DEV0_ECC_CFG bits */
105#define ECC_CFG_ECC_DISABLE 0
106#define ECC_SW_RESET 1
107#define ECC_MODE 4
108#define ECC_PARITY_SIZE_BYTES_BCH 8
109#define ECC_NUM_DATA_BYTES 16
110#define ECC_FORCE_CLK_OPEN 30
111
112/* NAND_DEV_CMD1 bits */
113#define READ_ADDR 0
114
115/* NAND_DEV_CMD_VLD bits */
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530116#define READ_START_VLD BIT(0)
117#define READ_STOP_VLD BIT(1)
118#define WRITE_START_VLD BIT(2)
119#define ERASE_START_VLD BIT(3)
120#define SEQ_READ_START_VLD BIT(4)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530121
122/* NAND_EBI2_ECC_BUF_CFG bits */
123#define NUM_STEPS 0
124
125/* NAND_ERASED_CW_DETECT_CFG bits */
126#define ERASED_CW_ECC_MASK 1
127#define AUTO_DETECT_RES 0
128#define MASK_ECC (1 << ERASED_CW_ECC_MASK)
129#define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
130#define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
131#define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
132#define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
133
134/* NAND_ERASED_CW_DETECT_STATUS bits */
135#define PAGE_ALL_ERASED BIT(7)
136#define CODEWORD_ALL_ERASED BIT(6)
137#define PAGE_ERASED BIT(5)
138#define CODEWORD_ERASED BIT(4)
139#define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
140#define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
141
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530142/* NAND_READ_LOCATION_n bits */
143#define READ_LOCATION_OFFSET 0
144#define READ_LOCATION_SIZE 16
145#define READ_LOCATION_LAST 31
146
Archit Tanejac76b78d2016-02-03 14:29:50 +0530147/* Version Mask */
148#define NAND_VERSION_MAJOR_MASK 0xf0000000
149#define NAND_VERSION_MAJOR_SHIFT 28
150#define NAND_VERSION_MINOR_MASK 0x0fff0000
151#define NAND_VERSION_MINOR_SHIFT 16
152
153/* NAND OP_CMDs */
154#define PAGE_READ 0x2
155#define PAGE_READ_WITH_ECC 0x3
156#define PAGE_READ_WITH_ECC_SPARE 0x4
157#define PROGRAM_PAGE 0x6
158#define PAGE_PROGRAM_WITH_ECC 0x7
159#define PROGRAM_PAGE_SPARE 0x9
160#define BLOCK_ERASE 0xa
161#define FETCH_ID 0xb
162#define RESET_DEVICE 0xd
163
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530164/* Default Value for NAND_DEV_CMD_VLD */
165#define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \
166 ERASE_START_VLD | SEQ_READ_START_VLD)
167
Abhishek Sahu9d43f912017-08-17 17:37:45 +0530168/* NAND_CTRL bits */
169#define BAM_MODE_EN BIT(0)
170
Archit Tanejac76b78d2016-02-03 14:29:50 +0530171/*
172 * the NAND controller performs reads/writes with ECC in 516 byte chunks.
173 * the driver calls the chunks 'step' or 'codeword' interchangeably
174 */
175#define NANDC_STEP_SIZE 512
176
177/*
178 * the largest page size we support is 8K, this will have 16 steps/codewords
179 * of 512 bytes each
180 */
181#define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
182
183/* we read at most 3 registers per codeword scan */
184#define MAX_REG_RD (3 * MAX_NUM_STEPS)
185
186/* ECC modes supported by the controller */
187#define ECC_NONE BIT(0)
188#define ECC_RS_4BIT BIT(1)
189#define ECC_BCH_4BIT BIT(2)
190#define ECC_BCH_8BIT BIT(3)
191
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530192#define nandc_set_read_loc(nandc, reg, offset, size, is_last) \
193nandc_set_reg(nandc, NAND_READ_LOCATION_##reg, \
194 ((offset) << READ_LOCATION_OFFSET) | \
195 ((size) << READ_LOCATION_SIZE) | \
196 ((is_last) << READ_LOCATION_LAST))
197
Abhishek Sahucc409b92017-08-17 17:37:47 +0530198/*
199 * Returns the actual register address for all NAND_DEV_ registers
200 * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD)
201 */
202#define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg))
203
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530204/* Returns the NAND register physical address */
205#define nandc_reg_phys(chip, offset) ((chip)->base_phys + (offset))
206
207/* Returns the dma address for reg read buffer */
208#define reg_buf_dma_addr(chip, vaddr) \
209 ((chip)->reg_read_dma + \
210 ((uint8_t *)(vaddr) - (uint8_t *)(chip)->reg_read_buf))
211
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530212#define QPIC_PER_CW_CMD_ELEMENTS 32
Abhishek Sahucb80f112017-08-17 17:37:40 +0530213#define QPIC_PER_CW_CMD_SGL 32
214#define QPIC_PER_CW_DATA_SGL 8
215
Abhishek Sahu6f200702018-06-20 12:57:33 +0530216#define QPIC_NAND_COMPLETION_TIMEOUT msecs_to_jiffies(2000)
217
Abhishek Sahucb80f112017-08-17 17:37:40 +0530218/*
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530219 * Flags used in DMA descriptor preparation helper functions
220 * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
221 */
222/* Don't set the EOT in current tx BAM sgl */
223#define NAND_BAM_NO_EOT BIT(0)
224/* Set the NWD flag in current BAM sgl */
225#define NAND_BAM_NWD BIT(1)
226/* Finish writing in the current BAM sgl and start writing in another BAM sgl */
227#define NAND_BAM_NEXT_SGL BIT(2)
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530228/*
229 * Erased codeword status is being used two times in single transfer so this
230 * flag will determine the current value of erased codeword status register
231 */
232#define NAND_ERASED_CW_SET BIT(4)
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530233
234/*
Abhishek Sahucb80f112017-08-17 17:37:40 +0530235 * This data type corresponds to the BAM transaction which will be used for all
236 * NAND transfers.
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530237 * @bam_ce - the array of BAM command elements
Abhishek Sahucb80f112017-08-17 17:37:40 +0530238 * @cmd_sgl - sgl for NAND BAM command pipe
239 * @data_sgl - sgl for NAND BAM consumer/producer pipe
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530240 * @bam_ce_pos - the index in bam_ce which is available for next sgl
241 * @bam_ce_start - the index in bam_ce which marks the start position ce
242 * for current sgl. It will be used for size calculation
243 * for current sgl
Abhishek Sahucb80f112017-08-17 17:37:40 +0530244 * @cmd_sgl_pos - current index in command sgl.
245 * @cmd_sgl_start - start index in command sgl.
246 * @tx_sgl_pos - current index in data sgl for tx.
247 * @tx_sgl_start - start index in data sgl for tx.
248 * @rx_sgl_pos - current index in data sgl for rx.
249 * @rx_sgl_start - start index in data sgl for rx.
Abhishek Sahu6f200702018-06-20 12:57:33 +0530250 * @wait_second_completion - wait for second DMA desc completion before making
251 * the NAND transfer completion.
252 * @txn_done - completion for NAND transfer.
253 * @last_data_desc - last DMA desc in data channel (tx/rx).
254 * @last_cmd_desc - last DMA desc in command channel.
Abhishek Sahucb80f112017-08-17 17:37:40 +0530255 */
256struct bam_transaction {
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530257 struct bam_cmd_element *bam_ce;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530258 struct scatterlist *cmd_sgl;
259 struct scatterlist *data_sgl;
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530260 u32 bam_ce_pos;
261 u32 bam_ce_start;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530262 u32 cmd_sgl_pos;
263 u32 cmd_sgl_start;
264 u32 tx_sgl_pos;
265 u32 tx_sgl_start;
266 u32 rx_sgl_pos;
267 u32 rx_sgl_start;
Abhishek Sahu6f200702018-06-20 12:57:33 +0530268 bool wait_second_completion;
269 struct completion txn_done;
270 struct dma_async_tx_descriptor *last_data_desc;
271 struct dma_async_tx_descriptor *last_cmd_desc;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530272};
273
Abhishek Sahu381dd242017-08-17 17:37:41 +0530274/*
275 * This data type corresponds to the nand dma descriptor
276 * @list - list for desc_info
277 * @dir - DMA transfer direction
278 * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
279 * ADM
280 * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
281 * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
282 * @dma_desc - low level DMA engine descriptor
283 */
Archit Tanejac76b78d2016-02-03 14:29:50 +0530284struct desc_info {
285 struct list_head node;
286
287 enum dma_data_direction dir;
Abhishek Sahu381dd242017-08-17 17:37:41 +0530288 union {
289 struct scatterlist adm_sgl;
290 struct {
291 struct scatterlist *bam_sgl;
292 int sgl_cnt;
293 };
294 };
Archit Tanejac76b78d2016-02-03 14:29:50 +0530295 struct dma_async_tx_descriptor *dma_desc;
296};
297
298/*
299 * holds the current register values that we want to write. acts as a contiguous
300 * chunk of memory which we use to write the controller registers through DMA.
301 */
302struct nandc_regs {
303 __le32 cmd;
304 __le32 addr0;
305 __le32 addr1;
306 __le32 chip_sel;
307 __le32 exec;
308
309 __le32 cfg0;
310 __le32 cfg1;
311 __le32 ecc_bch_cfg;
312
313 __le32 clrflashstatus;
314 __le32 clrreadstatus;
315
316 __le32 cmd1;
317 __le32 vld;
318
319 __le32 orig_cmd1;
320 __le32 orig_vld;
321
322 __le32 ecc_buf_cfg;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530323 __le32 read_location0;
324 __le32 read_location1;
325 __le32 read_location2;
326 __le32 read_location3;
327
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530328 __le32 erased_cw_detect_cfg_clr;
329 __le32 erased_cw_detect_cfg_set;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530330};
331
332/*
333 * NAND controller data struct
334 *
335 * @controller: base controller structure
336 * @host_list: list containing all the chips attached to the
337 * controller
338 * @dev: parent device
339 * @base: MMIO base
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530340 * @base_phys: physical base address of controller registers
341 * @base_dma: dma base address of controller registers
Archit Tanejac76b78d2016-02-03 14:29:50 +0530342 * @core_clk: controller clock
343 * @aon_clk: another controller clock
344 *
345 * @chan: dma channel
346 * @cmd_crci: ADM DMA CRCI for command flow control
347 * @data_crci: ADM DMA CRCI for data flow control
348 * @desc_list: DMA descriptor list (list of desc_infos)
349 *
350 * @data_buffer: our local DMA buffer for page read/writes,
351 * used when we can't use the buffer provided
352 * by upper layers directly
353 * @buf_size/count/start: markers for chip->read_buf/write_buf functions
354 * @reg_read_buf: local buffer for reading back registers via DMA
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530355 * @reg_read_dma: contains dma address for register read buffer
Archit Tanejac76b78d2016-02-03 14:29:50 +0530356 * @reg_read_pos: marker for data read in reg_read_buf
357 *
358 * @regs: a contiguous chunk of memory for DMA register
359 * writes. contains the register values to be
360 * written to controller
361 * @cmd1/vld: some fixed controller register values
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530362 * @props: properties of current NAND controller,
Archit Tanejac76b78d2016-02-03 14:29:50 +0530363 * initialized via DT match data
Abhishek Sahucb80f112017-08-17 17:37:40 +0530364 * @max_cwperpage: maximum QPIC codewords required. calculated
365 * from all connected NAND devices pagesize
Archit Tanejac76b78d2016-02-03 14:29:50 +0530366 */
367struct qcom_nand_controller {
368 struct nand_hw_control controller;
369 struct list_head host_list;
370
371 struct device *dev;
372
373 void __iomem *base;
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530374 phys_addr_t base_phys;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530375 dma_addr_t base_dma;
376
377 struct clk *core_clk;
378 struct clk *aon_clk;
379
Abhishek Sahu497d7d82017-08-11 17:09:19 +0530380 union {
381 /* will be used only by QPIC for BAM DMA */
382 struct {
383 struct dma_chan *tx_chan;
384 struct dma_chan *rx_chan;
385 struct dma_chan *cmd_chan;
386 };
387
388 /* will be used only by EBI2 for ADM DMA */
389 struct {
390 struct dma_chan *chan;
391 unsigned int cmd_crci;
392 unsigned int data_crci;
393 };
394 };
395
Archit Tanejac76b78d2016-02-03 14:29:50 +0530396 struct list_head desc_list;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530397 struct bam_transaction *bam_txn;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530398
399 u8 *data_buffer;
400 int buf_size;
401 int buf_count;
402 int buf_start;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530403 unsigned int max_cwperpage;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530404
405 __le32 *reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530406 dma_addr_t reg_read_dma;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530407 int reg_read_pos;
408
409 struct nandc_regs *regs;
410
411 u32 cmd1, vld;
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530412 const struct qcom_nandc_props *props;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530413};
414
415/*
416 * NAND chip structure
417 *
418 * @chip: base NAND chip structure
419 * @node: list node to add itself to host_list in
420 * qcom_nand_controller
421 *
422 * @cs: chip select value for this chip
423 * @cw_size: the number of bytes in a single step/codeword
424 * of a page, consisting of all data, ecc, spare
425 * and reserved bytes
426 * @cw_data: the number of bytes within a codeword protected
427 * by ECC
428 * @use_ecc: request the controller to use ECC for the
429 * upcoming read/write
430 * @bch_enabled: flag to tell whether BCH ECC mode is used
431 * @ecc_bytes_hw: ECC bytes used by controller hardware for this
432 * chip
433 * @status: value to be returned if NAND_CMD_STATUS command
434 * is executed
435 * @last_command: keeps track of last command on this chip. used
436 * for reading correct status
437 *
438 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
439 * ecc/non-ecc mode for the current nand flash
440 * device
441 */
442struct qcom_nand_host {
443 struct nand_chip chip;
444 struct list_head node;
445
446 int cs;
447 int cw_size;
448 int cw_data;
449 bool use_ecc;
450 bool bch_enabled;
451 int ecc_bytes_hw;
452 int spare_bytes;
453 int bbm_size;
454 u8 status;
455 int last_command;
456
457 u32 cfg0, cfg1;
458 u32 cfg0_raw, cfg1_raw;
459 u32 ecc_buf_cfg;
460 u32 ecc_bch_cfg;
461 u32 clrflashstatus;
462 u32 clrreadstatus;
463};
464
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530465/*
466 * This data type corresponds to the NAND controller properties which varies
467 * among different NAND controllers.
468 * @ecc_modes - ecc mode for NAND
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +0530469 * @is_bam - whether NAND controller is using BAM
Abhishek Sahucc409b92017-08-17 17:37:47 +0530470 * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530471 */
472struct qcom_nandc_props {
473 u32 ecc_modes;
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +0530474 bool is_bam;
Abhishek Sahucc409b92017-08-17 17:37:47 +0530475 u32 dev_cmd_reg_start;
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530476};
477
Abhishek Sahucb80f112017-08-17 17:37:40 +0530478/* Frees the BAM transaction memory */
479static void free_bam_transaction(struct qcom_nand_controller *nandc)
480{
481 struct bam_transaction *bam_txn = nandc->bam_txn;
482
483 devm_kfree(nandc->dev, bam_txn);
484}
485
486/* Allocates and Initializes the BAM transaction */
487static struct bam_transaction *
488alloc_bam_transaction(struct qcom_nand_controller *nandc)
489{
490 struct bam_transaction *bam_txn;
491 size_t bam_txn_size;
492 unsigned int num_cw = nandc->max_cwperpage;
493 void *bam_txn_buf;
494
495 bam_txn_size =
496 sizeof(*bam_txn) + num_cw *
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530497 ((sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS) +
498 (sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) +
Abhishek Sahucb80f112017-08-17 17:37:40 +0530499 (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL));
500
501 bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL);
502 if (!bam_txn_buf)
503 return NULL;
504
505 bam_txn = bam_txn_buf;
506 bam_txn_buf += sizeof(*bam_txn);
507
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530508 bam_txn->bam_ce = bam_txn_buf;
509 bam_txn_buf +=
510 sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS * num_cw;
511
Abhishek Sahucb80f112017-08-17 17:37:40 +0530512 bam_txn->cmd_sgl = bam_txn_buf;
513 bam_txn_buf +=
514 sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw;
515
516 bam_txn->data_sgl = bam_txn_buf;
517
Abhishek Sahu6f200702018-06-20 12:57:33 +0530518 init_completion(&bam_txn->txn_done);
519
Abhishek Sahucb80f112017-08-17 17:37:40 +0530520 return bam_txn;
521}
522
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530523/* Clears the BAM transaction indexes */
524static void clear_bam_transaction(struct qcom_nand_controller *nandc)
525{
526 struct bam_transaction *bam_txn = nandc->bam_txn;
527
528 if (!nandc->props->is_bam)
529 return;
530
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530531 bam_txn->bam_ce_pos = 0;
532 bam_txn->bam_ce_start = 0;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530533 bam_txn->cmd_sgl_pos = 0;
534 bam_txn->cmd_sgl_start = 0;
535 bam_txn->tx_sgl_pos = 0;
536 bam_txn->tx_sgl_start = 0;
537 bam_txn->rx_sgl_pos = 0;
538 bam_txn->rx_sgl_start = 0;
Abhishek Sahu6f200702018-06-20 12:57:33 +0530539 bam_txn->last_data_desc = NULL;
540 bam_txn->wait_second_completion = false;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530541
542 sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage *
543 QPIC_PER_CW_CMD_SGL);
544 sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage *
545 QPIC_PER_CW_DATA_SGL);
Abhishek Sahu6f200702018-06-20 12:57:33 +0530546
547 reinit_completion(&bam_txn->txn_done);
548}
549
550/* Callback for DMA descriptor completion */
551static void qpic_bam_dma_done(void *data)
552{
553 struct bam_transaction *bam_txn = data;
554
555 /*
556 * In case of data transfer with NAND, 2 callbacks will be generated.
557 * One for command channel and another one for data channel.
558 * If current transaction has data descriptors
559 * (i.e. wait_second_completion is true), then set this to false
560 * and wait for second DMA descriptor completion.
561 */
562 if (bam_txn->wait_second_completion)
563 bam_txn->wait_second_completion = false;
564 else
565 complete(&bam_txn->txn_done);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530566}
567
Archit Tanejac76b78d2016-02-03 14:29:50 +0530568static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
569{
570 return container_of(chip, struct qcom_nand_host, chip);
571}
572
573static inline struct qcom_nand_controller *
574get_qcom_nand_controller(struct nand_chip *chip)
575{
576 return container_of(chip->controller, struct qcom_nand_controller,
577 controller);
578}
579
580static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
581{
582 return ioread32(nandc->base + offset);
583}
584
585static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
586 u32 val)
587{
588 iowrite32(val, nandc->base + offset);
589}
590
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530591static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
592 bool is_cpu)
593{
594 if (!nandc->props->is_bam)
595 return;
596
597 if (is_cpu)
598 dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
599 MAX_REG_RD *
600 sizeof(*nandc->reg_read_buf),
601 DMA_FROM_DEVICE);
602 else
603 dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
604 MAX_REG_RD *
605 sizeof(*nandc->reg_read_buf),
606 DMA_FROM_DEVICE);
607}
608
Archit Tanejac76b78d2016-02-03 14:29:50 +0530609static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
610{
611 switch (offset) {
612 case NAND_FLASH_CMD:
613 return &regs->cmd;
614 case NAND_ADDR0:
615 return &regs->addr0;
616 case NAND_ADDR1:
617 return &regs->addr1;
618 case NAND_FLASH_CHIP_SELECT:
619 return &regs->chip_sel;
620 case NAND_EXEC_CMD:
621 return &regs->exec;
622 case NAND_FLASH_STATUS:
623 return &regs->clrflashstatus;
624 case NAND_DEV0_CFG0:
625 return &regs->cfg0;
626 case NAND_DEV0_CFG1:
627 return &regs->cfg1;
628 case NAND_DEV0_ECC_CFG:
629 return &regs->ecc_bch_cfg;
630 case NAND_READ_STATUS:
631 return &regs->clrreadstatus;
632 case NAND_DEV_CMD1:
633 return &regs->cmd1;
634 case NAND_DEV_CMD1_RESTORE:
635 return &regs->orig_cmd1;
636 case NAND_DEV_CMD_VLD:
637 return &regs->vld;
638 case NAND_DEV_CMD_VLD_RESTORE:
639 return &regs->orig_vld;
640 case NAND_EBI2_ECC_BUF_CFG:
641 return &regs->ecc_buf_cfg;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530642 case NAND_READ_LOCATION_0:
643 return &regs->read_location0;
644 case NAND_READ_LOCATION_1:
645 return &regs->read_location1;
646 case NAND_READ_LOCATION_2:
647 return &regs->read_location2;
648 case NAND_READ_LOCATION_3:
649 return &regs->read_location3;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530650 default:
651 return NULL;
652 }
653}
654
655static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
656 u32 val)
657{
658 struct nandc_regs *regs = nandc->regs;
659 __le32 *reg;
660
661 reg = offset_to_nandc_reg(regs, offset);
662
663 if (reg)
664 *reg = cpu_to_le32(val);
665}
666
667/* helper to configure address register values */
668static void set_address(struct qcom_nand_host *host, u16 column, int page)
669{
670 struct nand_chip *chip = &host->chip;
671 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
672
673 if (chip->options & NAND_BUSWIDTH_16)
674 column >>= 1;
675
676 nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
677 nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
678}
679
680/*
681 * update_rw_regs: set up read/write register values, these will be
682 * written to the NAND controller registers via DMA
683 *
684 * @num_cw: number of steps for the read/write operation
685 * @read: read or write operation
686 */
687static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
688{
689 struct nand_chip *chip = &host->chip;
690 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
691 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
692
693 if (read) {
694 if (host->use_ecc)
695 cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
696 else
697 cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
698 } else {
699 cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
700 }
701
702 if (host->use_ecc) {
703 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
704 (num_cw - 1) << CW_PER_PAGE;
705
706 cfg1 = host->cfg1;
707 ecc_bch_cfg = host->ecc_bch_cfg;
708 } else {
709 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
710 (num_cw - 1) << CW_PER_PAGE;
711
712 cfg1 = host->cfg1_raw;
713 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
714 }
715
716 nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
717 nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
718 nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
719 nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
720 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
721 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
722 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
723 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530724
725 if (read)
726 nandc_set_read_loc(nandc, 0, 0, host->use_ecc ?
727 host->cw_data : host->cw_size, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530728}
729
Abhishek Sahu381dd242017-08-17 17:37:41 +0530730/*
731 * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
732 * for BAM. This descriptor will be added in the NAND DMA descriptor queue
733 * which will be submitted to DMA engine.
734 */
735static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
736 struct dma_chan *chan,
737 unsigned long flags)
738{
739 struct desc_info *desc;
740 struct scatterlist *sgl;
741 unsigned int sgl_cnt;
742 int ret;
743 struct bam_transaction *bam_txn = nandc->bam_txn;
744 enum dma_transfer_direction dir_eng;
745 struct dma_async_tx_descriptor *dma_desc;
746
747 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
748 if (!desc)
749 return -ENOMEM;
750
751 if (chan == nandc->cmd_chan) {
752 sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
753 sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
754 bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
755 dir_eng = DMA_MEM_TO_DEV;
756 desc->dir = DMA_TO_DEVICE;
757 } else if (chan == nandc->tx_chan) {
758 sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
759 sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
760 bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
761 dir_eng = DMA_MEM_TO_DEV;
762 desc->dir = DMA_TO_DEVICE;
763 } else {
764 sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
765 sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
766 bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
767 dir_eng = DMA_DEV_TO_MEM;
768 desc->dir = DMA_FROM_DEVICE;
769 }
770
771 sg_mark_end(sgl + sgl_cnt - 1);
772 ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
773 if (ret == 0) {
774 dev_err(nandc->dev, "failure in mapping desc\n");
775 kfree(desc);
776 return -ENOMEM;
777 }
778
779 desc->sgl_cnt = sgl_cnt;
780 desc->bam_sgl = sgl;
781
782 dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
783 flags);
784
785 if (!dma_desc) {
786 dev_err(nandc->dev, "failure in prep desc\n");
787 dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
788 kfree(desc);
789 return -EINVAL;
790 }
791
792 desc->dma_desc = dma_desc;
793
Abhishek Sahu6f200702018-06-20 12:57:33 +0530794 /* update last data/command descriptor */
795 if (chan == nandc->cmd_chan)
796 bam_txn->last_cmd_desc = dma_desc;
797 else
798 bam_txn->last_data_desc = dma_desc;
799
Abhishek Sahu381dd242017-08-17 17:37:41 +0530800 list_add_tail(&desc->node, &nandc->desc_list);
801
802 return 0;
803}
804
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530805/*
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530806 * Prepares the command descriptor for BAM DMA which will be used for NAND
807 * register reads and writes. The command descriptor requires the command
808 * to be formed in command element type so this function uses the command
809 * element from bam transaction ce array and fills the same with required
810 * data. A single SGL can contain multiple command elements so
811 * NAND_BAM_NEXT_SGL will be used for starting the separate SGL
812 * after the current command element.
813 */
814static int prep_bam_dma_desc_cmd(struct qcom_nand_controller *nandc, bool read,
815 int reg_off, const void *vaddr,
816 int size, unsigned int flags)
817{
818 int bam_ce_size;
819 int i, ret;
820 struct bam_cmd_element *bam_ce_buffer;
821 struct bam_transaction *bam_txn = nandc->bam_txn;
822
823 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_pos];
824
825 /* fill the command desc */
826 for (i = 0; i < size; i++) {
827 if (read)
828 bam_prep_ce(&bam_ce_buffer[i],
829 nandc_reg_phys(nandc, reg_off + 4 * i),
830 BAM_READ_COMMAND,
831 reg_buf_dma_addr(nandc,
832 (__le32 *)vaddr + i));
833 else
834 bam_prep_ce_le32(&bam_ce_buffer[i],
835 nandc_reg_phys(nandc, reg_off + 4 * i),
836 BAM_WRITE_COMMAND,
837 *((__le32 *)vaddr + i));
838 }
839
840 bam_txn->bam_ce_pos += size;
841
842 /* use the separate sgl after this command */
843 if (flags & NAND_BAM_NEXT_SGL) {
844 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_start];
845 bam_ce_size = (bam_txn->bam_ce_pos -
846 bam_txn->bam_ce_start) *
847 sizeof(struct bam_cmd_element);
848 sg_set_buf(&bam_txn->cmd_sgl[bam_txn->cmd_sgl_pos],
849 bam_ce_buffer, bam_ce_size);
850 bam_txn->cmd_sgl_pos++;
851 bam_txn->bam_ce_start = bam_txn->bam_ce_pos;
852
853 if (flags & NAND_BAM_NWD) {
854 ret = prepare_bam_async_desc(nandc, nandc->cmd_chan,
855 DMA_PREP_FENCE |
856 DMA_PREP_CMD);
857 if (ret)
858 return ret;
859 }
860 }
861
862 return 0;
863}
864
865/*
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530866 * Prepares the data descriptor for BAM DMA which will be used for NAND
867 * data reads and writes.
868 */
869static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read,
870 const void *vaddr,
871 int size, unsigned int flags)
872{
873 int ret;
874 struct bam_transaction *bam_txn = nandc->bam_txn;
875
876 if (read) {
877 sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos],
878 vaddr, size);
879 bam_txn->rx_sgl_pos++;
880 } else {
881 sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos],
882 vaddr, size);
883 bam_txn->tx_sgl_pos++;
884
885 /*
886 * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
887 * is not set, form the DMA descriptor
888 */
889 if (!(flags & NAND_BAM_NO_EOT)) {
890 ret = prepare_bam_async_desc(nandc, nandc->tx_chan,
891 DMA_PREP_INTERRUPT);
892 if (ret)
893 return ret;
894 }
895 }
896
897 return 0;
898}
899
Abhishek Sahu381dd242017-08-17 17:37:41 +0530900static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
901 int reg_off, const void *vaddr, int size,
902 bool flow_control)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530903{
904 struct desc_info *desc;
905 struct dma_async_tx_descriptor *dma_desc;
906 struct scatterlist *sgl;
907 struct dma_slave_config slave_conf;
908 enum dma_transfer_direction dir_eng;
909 int ret;
910
911 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
912 if (!desc)
913 return -ENOMEM;
914
Abhishek Sahu381dd242017-08-17 17:37:41 +0530915 sgl = &desc->adm_sgl;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530916
917 sg_init_one(sgl, vaddr, size);
918
919 if (read) {
920 dir_eng = DMA_DEV_TO_MEM;
921 desc->dir = DMA_FROM_DEVICE;
922 } else {
923 dir_eng = DMA_MEM_TO_DEV;
924 desc->dir = DMA_TO_DEVICE;
925 }
926
927 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
928 if (ret == 0) {
929 ret = -ENOMEM;
930 goto err;
931 }
932
933 memset(&slave_conf, 0x00, sizeof(slave_conf));
934
935 slave_conf.device_fc = flow_control;
936 if (read) {
937 slave_conf.src_maxburst = 16;
938 slave_conf.src_addr = nandc->base_dma + reg_off;
939 slave_conf.slave_id = nandc->data_crci;
940 } else {
941 slave_conf.dst_maxburst = 16;
942 slave_conf.dst_addr = nandc->base_dma + reg_off;
943 slave_conf.slave_id = nandc->cmd_crci;
944 }
945
946 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
947 if (ret) {
948 dev_err(nandc->dev, "failed to configure dma channel\n");
949 goto err;
950 }
951
952 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
953 if (!dma_desc) {
954 dev_err(nandc->dev, "failed to prepare desc\n");
955 ret = -EINVAL;
956 goto err;
957 }
958
959 desc->dma_desc = dma_desc;
960
961 list_add_tail(&desc->node, &nandc->desc_list);
962
963 return 0;
964err:
965 kfree(desc);
966
967 return ret;
968}
969
970/*
971 * read_reg_dma: prepares a descriptor to read a given number of
972 * contiguous registers to the reg_read_buf pointer
973 *
974 * @first: offset of the first register in the contiguous block
975 * @num_regs: number of registers to read
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530976 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530977 */
978static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530979 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530980{
981 bool flow_control = false;
982 void *vaddr;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530983
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530984 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
985 nandc->reg_read_pos += num_regs;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530986
Abhishek Sahucc409b92017-08-17 17:37:47 +0530987 if (first == NAND_DEV_CMD_VLD || first == NAND_DEV_CMD1)
988 first = dev_cmd_reg_addr(nandc, first);
989
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530990 if (nandc->props->is_bam)
991 return prep_bam_dma_desc_cmd(nandc, true, first, vaddr,
992 num_regs, flags);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530993
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530994 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
995 flow_control = true;
996
997 return prep_adm_dma_desc(nandc, true, first, vaddr,
998 num_regs * sizeof(u32), flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530999}
1000
1001/*
1002 * write_reg_dma: prepares a descriptor to write a given number of
1003 * contiguous registers
1004 *
1005 * @first: offset of the first register in the contiguous block
1006 * @num_regs: number of registers to write
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301007 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +05301008 */
1009static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301010 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301011{
1012 bool flow_control = false;
1013 struct nandc_regs *regs = nandc->regs;
1014 void *vaddr;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301015
1016 vaddr = offset_to_nandc_reg(regs, first);
1017
Abhishek Sahua86b9c42017-08-17 17:37:44 +05301018 if (first == NAND_ERASED_CW_DETECT_CFG) {
1019 if (flags & NAND_ERASED_CW_SET)
1020 vaddr = &regs->erased_cw_detect_cfg_set;
1021 else
1022 vaddr = &regs->erased_cw_detect_cfg_clr;
1023 }
1024
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301025 if (first == NAND_EXEC_CMD)
1026 flags |= NAND_BAM_NWD;
1027
Abhishek Sahucc409b92017-08-17 17:37:47 +05301028 if (first == NAND_DEV_CMD1_RESTORE || first == NAND_DEV_CMD1)
1029 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301030
Abhishek Sahucc409b92017-08-17 17:37:47 +05301031 if (first == NAND_DEV_CMD_VLD_RESTORE || first == NAND_DEV_CMD_VLD)
1032 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301033
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301034 if (nandc->props->is_bam)
1035 return prep_bam_dma_desc_cmd(nandc, false, first, vaddr,
1036 num_regs, flags);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301037
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301038 if (first == NAND_FLASH_CMD)
1039 flow_control = true;
1040
1041 return prep_adm_dma_desc(nandc, false, first, vaddr,
1042 num_regs * sizeof(u32), flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301043}
1044
1045/*
1046 * read_data_dma: prepares a DMA descriptor to transfer data from the
1047 * controller's internal buffer to the buffer 'vaddr'
1048 *
1049 * @reg_off: offset within the controller's data buffer
1050 * @vaddr: virtual address of the buffer we want to write to
1051 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301052 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +05301053 */
1054static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301055 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301056{
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301057 if (nandc->props->is_bam)
1058 return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags);
1059
Abhishek Sahu381dd242017-08-17 17:37:41 +05301060 return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301061}
1062
1063/*
1064 * write_data_dma: prepares a DMA descriptor to transfer data from
1065 * 'vaddr' to the controller's internal buffer
1066 *
1067 * @reg_off: offset within the controller's data buffer
1068 * @vaddr: virtual address of the buffer we want to read from
1069 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301070 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +05301071 */
1072static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301073 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301074{
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301075 if (nandc->props->is_bam)
1076 return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags);
1077
Abhishek Sahu381dd242017-08-17 17:37:41 +05301078 return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301079}
1080
1081/*
Abhishek Sahubde43302017-07-19 17:17:55 +05301082 * Helper to prepare DMA descriptors for configuring registers
1083 * before reading a NAND page.
Archit Tanejac76b78d2016-02-03 14:29:50 +05301084 */
Abhishek Sahubde43302017-07-19 17:17:55 +05301085static void config_nand_page_read(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301086{
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301087 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1088 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1089 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
Abhishek Sahua86b9c42017-08-17 17:37:44 +05301090 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
1091 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
1092 NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
Abhishek Sahubde43302017-07-19 17:17:55 +05301093}
Archit Tanejac76b78d2016-02-03 14:29:50 +05301094
Abhishek Sahubde43302017-07-19 17:17:55 +05301095/*
1096 * Helper to prepare DMA descriptors for configuring registers
1097 * before reading each codeword in NAND page.
1098 */
1099static void config_nand_cw_read(struct qcom_nand_controller *nandc)
1100{
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301101 if (nandc->props->is_bam)
1102 write_reg_dma(nandc, NAND_READ_LOCATION_0, 4,
1103 NAND_BAM_NEXT_SGL);
1104
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301105 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1106 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301107
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301108 read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
1109 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
1110 NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301111}
1112
1113/*
Abhishek Sahubde43302017-07-19 17:17:55 +05301114 * Helper to prepare dma descriptors to configure registers needed for reading a
1115 * single codeword in page
Archit Tanejac76b78d2016-02-03 14:29:50 +05301116 */
Abhishek Sahubde43302017-07-19 17:17:55 +05301117static void config_nand_single_cw_page_read(struct qcom_nand_controller *nandc)
1118{
1119 config_nand_page_read(nandc);
1120 config_nand_cw_read(nandc);
1121}
1122
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301123/*
1124 * Helper to prepare DMA descriptors used to configure registers needed for
1125 * before writing a NAND page.
1126 */
1127static void config_nand_page_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301128{
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301129 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1130 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1131 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
1132 NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301133}
1134
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301135/*
1136 * Helper to prepare DMA descriptors for configuring registers
1137 * before writing each codeword in NAND page.
1138 */
1139static void config_nand_cw_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301140{
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301141 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1142 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301143
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301144 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301145
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301146 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1147 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301148}
1149
1150/*
1151 * the following functions are used within chip->cmdfunc() to perform different
1152 * NAND_CMD_* commands
1153 */
1154
1155/* sets up descriptors for NAND_CMD_PARAM */
1156static int nandc_param(struct qcom_nand_host *host)
1157{
1158 struct nand_chip *chip = &host->chip;
1159 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1160
1161 /*
1162 * NAND_CMD_PARAM is called before we know much about the FLASH chip
1163 * in use. we configure the controller to perform a raw read of 512
1164 * bytes to read onfi params
1165 */
1166 nandc_set_reg(nandc, NAND_FLASH_CMD, PAGE_READ | PAGE_ACC | LAST_PAGE);
1167 nandc_set_reg(nandc, NAND_ADDR0, 0);
1168 nandc_set_reg(nandc, NAND_ADDR1, 0);
1169 nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
1170 | 512 << UD_SIZE_BYTES
1171 | 5 << NUM_ADDR_CYCLES
1172 | 0 << SPARE_SIZE_BYTES);
1173 nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
1174 | 0 << CS_ACTIVE_BSY
1175 | 17 << BAD_BLOCK_BYTE_NUM
1176 | 1 << BAD_BLOCK_IN_SPARE_AREA
1177 | 2 << WR_RD_BSY_GAP
1178 | 0 << WIDE_FLASH
1179 | 1 << DEV0_CFG1_ECC_DISABLE);
1180 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
1181
1182 /* configure CMD1 and VLD for ONFI param probing */
1183 nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
Abhishek Sahud8a9b322017-08-11 17:09:16 +05301184 (nandc->vld & ~READ_START_VLD));
Archit Tanejac76b78d2016-02-03 14:29:50 +05301185 nandc_set_reg(nandc, NAND_DEV_CMD1,
1186 (nandc->cmd1 & ~(0xFF << READ_ADDR))
1187 | NAND_CMD_PARAM << READ_ADDR);
1188
1189 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1190
1191 nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
1192 nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301193 nandc_set_read_loc(nandc, 0, 0, 512, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301194
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301195 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
1196 write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301197
1198 nandc->buf_count = 512;
1199 memset(nandc->data_buffer, 0xff, nandc->buf_count);
1200
Abhishek Sahubde43302017-07-19 17:17:55 +05301201 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301202
1203 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301204 nandc->buf_count, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301205
1206 /* restore CMD1 and VLD regs */
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301207 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
1208 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301209
1210 return 0;
1211}
1212
1213/* sets up descriptors for NAND_CMD_ERASE1 */
1214static int erase_block(struct qcom_nand_host *host, int page_addr)
1215{
1216 struct nand_chip *chip = &host->chip;
1217 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1218
1219 nandc_set_reg(nandc, NAND_FLASH_CMD,
1220 BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
1221 nandc_set_reg(nandc, NAND_ADDR0, page_addr);
1222 nandc_set_reg(nandc, NAND_ADDR1, 0);
1223 nandc_set_reg(nandc, NAND_DEV0_CFG0,
1224 host->cfg0_raw & ~(7 << CW_PER_PAGE));
1225 nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
1226 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1227 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
1228 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
1229
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301230 write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
1231 write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
1232 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301233
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301234 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301235
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301236 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1237 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301238
1239 return 0;
1240}
1241
1242/* sets up descriptors for NAND_CMD_READID */
1243static int read_id(struct qcom_nand_host *host, int column)
1244{
1245 struct nand_chip *chip = &host->chip;
1246 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1247
1248 if (column == -1)
1249 return 0;
1250
1251 nandc_set_reg(nandc, NAND_FLASH_CMD, FETCH_ID);
1252 nandc_set_reg(nandc, NAND_ADDR0, column);
1253 nandc_set_reg(nandc, NAND_ADDR1, 0);
Abhishek Sahu9d43f912017-08-17 17:37:45 +05301254 nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT,
1255 nandc->props->is_bam ? 0 : DM_EN);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301256 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1257
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301258 write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
1259 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301260
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301261 read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301262
1263 return 0;
1264}
1265
1266/* sets up descriptors for NAND_CMD_RESET */
1267static int reset(struct qcom_nand_host *host)
1268{
1269 struct nand_chip *chip = &host->chip;
1270 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1271
1272 nandc_set_reg(nandc, NAND_FLASH_CMD, RESET_DEVICE);
1273 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1274
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301275 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1276 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301277
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301278 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301279
1280 return 0;
1281}
1282
1283/* helpers to submit/free our list of dma descriptors */
1284static int submit_descs(struct qcom_nand_controller *nandc)
1285{
1286 struct desc_info *desc;
1287 dma_cookie_t cookie = 0;
Abhishek Sahu381dd242017-08-17 17:37:41 +05301288 struct bam_transaction *bam_txn = nandc->bam_txn;
1289 int r;
1290
1291 if (nandc->props->is_bam) {
1292 if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
1293 r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
1294 if (r)
1295 return r;
1296 }
1297
1298 if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
1299 r = prepare_bam_async_desc(nandc, nandc->tx_chan,
1300 DMA_PREP_INTERRUPT);
1301 if (r)
1302 return r;
1303 }
1304
1305 if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301306 r = prepare_bam_async_desc(nandc, nandc->cmd_chan,
1307 DMA_PREP_CMD);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301308 if (r)
1309 return r;
1310 }
1311 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301312
1313 list_for_each_entry(desc, &nandc->desc_list, node)
1314 cookie = dmaengine_submit(desc->dma_desc);
1315
Abhishek Sahu381dd242017-08-17 17:37:41 +05301316 if (nandc->props->is_bam) {
Abhishek Sahu6f200702018-06-20 12:57:33 +05301317 bam_txn->last_cmd_desc->callback = qpic_bam_dma_done;
1318 bam_txn->last_cmd_desc->callback_param = bam_txn;
1319 if (bam_txn->last_data_desc) {
1320 bam_txn->last_data_desc->callback = qpic_bam_dma_done;
1321 bam_txn->last_data_desc->callback_param = bam_txn;
1322 bam_txn->wait_second_completion = true;
1323 }
1324
Abhishek Sahu381dd242017-08-17 17:37:41 +05301325 dma_async_issue_pending(nandc->tx_chan);
1326 dma_async_issue_pending(nandc->rx_chan);
Abhishek Sahu6f200702018-06-20 12:57:33 +05301327 dma_async_issue_pending(nandc->cmd_chan);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301328
Abhishek Sahu6f200702018-06-20 12:57:33 +05301329 if (!wait_for_completion_timeout(&bam_txn->txn_done,
1330 QPIC_NAND_COMPLETION_TIMEOUT))
Abhishek Sahu381dd242017-08-17 17:37:41 +05301331 return -ETIMEDOUT;
1332 } else {
1333 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
1334 return -ETIMEDOUT;
1335 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301336
1337 return 0;
1338}
1339
1340static void free_descs(struct qcom_nand_controller *nandc)
1341{
1342 struct desc_info *desc, *n;
1343
1344 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
1345 list_del(&desc->node);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301346
1347 if (nandc->props->is_bam)
1348 dma_unmap_sg(nandc->dev, desc->bam_sgl,
1349 desc->sgl_cnt, desc->dir);
1350 else
1351 dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
1352 desc->dir);
1353
Archit Tanejac76b78d2016-02-03 14:29:50 +05301354 kfree(desc);
1355 }
1356}
1357
1358/* reset the register read buffer for next NAND operation */
1359static void clear_read_regs(struct qcom_nand_controller *nandc)
1360{
1361 nandc->reg_read_pos = 0;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301362 nandc_read_buffer_sync(nandc, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301363}
1364
1365static void pre_command(struct qcom_nand_host *host, int command)
1366{
1367 struct nand_chip *chip = &host->chip;
1368 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1369
1370 nandc->buf_count = 0;
1371 nandc->buf_start = 0;
1372 host->use_ecc = false;
1373 host->last_command = command;
1374
1375 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301376
1377 if (command == NAND_CMD_RESET || command == NAND_CMD_READID ||
1378 command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1)
1379 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301380}
1381
1382/*
1383 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
1384 * privately maintained status byte, this status byte can be read after
1385 * NAND_CMD_STATUS is called
1386 */
1387static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
1388{
1389 struct nand_chip *chip = &host->chip;
1390 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1391 struct nand_ecc_ctrl *ecc = &chip->ecc;
1392 int num_cw;
1393 int i;
1394
1395 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301396 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301397
1398 for (i = 0; i < num_cw; i++) {
1399 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
1400
1401 if (flash_status & FS_MPU_ERR)
1402 host->status &= ~NAND_STATUS_WP;
1403
1404 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
1405 (flash_status &
1406 FS_DEVICE_STS_ERR)))
1407 host->status |= NAND_STATUS_FAIL;
1408 }
1409}
1410
1411static void post_command(struct qcom_nand_host *host, int command)
1412{
1413 struct nand_chip *chip = &host->chip;
1414 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1415
1416 switch (command) {
1417 case NAND_CMD_READID:
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301418 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301419 memcpy(nandc->data_buffer, nandc->reg_read_buf,
1420 nandc->buf_count);
1421 break;
1422 case NAND_CMD_PAGEPROG:
1423 case NAND_CMD_ERASE1:
1424 parse_erase_write_errors(host, command);
1425 break;
1426 default:
1427 break;
1428 }
1429}
1430
1431/*
1432 * Implements chip->cmdfunc. It's only used for a limited set of commands.
1433 * The rest of the commands wouldn't be called by upper layers. For example,
1434 * NAND_CMD_READOOB would never be called because we have our own versions
1435 * of read_oob ops for nand_ecc_ctrl.
1436 */
1437static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
1438 int column, int page_addr)
1439{
1440 struct nand_chip *chip = mtd_to_nand(mtd);
1441 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1442 struct nand_ecc_ctrl *ecc = &chip->ecc;
1443 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1444 bool wait = false;
1445 int ret = 0;
1446
1447 pre_command(host, command);
1448
1449 switch (command) {
1450 case NAND_CMD_RESET:
1451 ret = reset(host);
1452 wait = true;
1453 break;
1454
1455 case NAND_CMD_READID:
1456 nandc->buf_count = 4;
1457 ret = read_id(host, column);
1458 wait = true;
1459 break;
1460
1461 case NAND_CMD_PARAM:
1462 ret = nandc_param(host);
1463 wait = true;
1464 break;
1465
1466 case NAND_CMD_ERASE1:
1467 ret = erase_block(host, page_addr);
1468 wait = true;
1469 break;
1470
1471 case NAND_CMD_READ0:
1472 /* we read the entire page for now */
1473 WARN_ON(column != 0);
1474
1475 host->use_ecc = true;
1476 set_address(host, 0, page_addr);
1477 update_rw_regs(host, ecc->steps, true);
1478 break;
1479
1480 case NAND_CMD_SEQIN:
1481 WARN_ON(column != 0);
1482 set_address(host, 0, page_addr);
1483 break;
1484
1485 case NAND_CMD_PAGEPROG:
1486 case NAND_CMD_STATUS:
1487 case NAND_CMD_NONE:
1488 default:
1489 break;
1490 }
1491
1492 if (ret) {
1493 dev_err(nandc->dev, "failure executing command %d\n",
1494 command);
1495 free_descs(nandc);
1496 return;
1497 }
1498
1499 if (wait) {
1500 ret = submit_descs(nandc);
1501 if (ret)
1502 dev_err(nandc->dev,
1503 "failure submitting descs for command %d\n",
1504 command);
1505 }
1506
1507 free_descs(nandc);
1508
1509 post_command(host, command);
1510}
1511
1512/*
1513 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1514 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1515 *
1516 * when using RS ECC, the HW reports the same erros when reading an erased CW,
1517 * but it notifies that it is an erased CW by placing special characters at
1518 * certain offsets in the buffer.
1519 *
1520 * verify if the page is erased or not, and fix up the page for RS ECC by
1521 * replacing the special characters with 0xff.
1522 */
1523static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
1524{
1525 u8 empty1, empty2;
1526
1527 /*
1528 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1529 * is erased by looking for 0x54s at offsets 3 and 175 from the
1530 * beginning of each codeword
1531 */
1532
1533 empty1 = data_buf[3];
1534 empty2 = data_buf[175];
1535
1536 /*
1537 * if the erased codework markers, if they exist override them with
1538 * 0xffs
1539 */
1540 if ((empty1 == 0x54 && empty2 == 0xff) ||
1541 (empty1 == 0xff && empty2 == 0x54)) {
1542 data_buf[3] = 0xff;
1543 data_buf[175] = 0xff;
1544 }
1545
1546 /*
1547 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1548 * restore the original values at the special offsets
1549 */
1550 if (memchr_inv(data_buf, 0xff, data_len)) {
1551 data_buf[3] = empty1;
1552 data_buf[175] = empty2;
1553
1554 return false;
1555 }
1556
1557 return true;
1558}
1559
1560struct read_stats {
1561 __le32 flash;
1562 __le32 buffer;
1563 __le32 erased_cw;
1564};
1565
1566/*
1567 * reads back status registers set by the controller to notify page read
1568 * errors. this is equivalent to what 'ecc->correct()' would do.
1569 */
1570static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1571 u8 *oob_buf)
1572{
1573 struct nand_chip *chip = &host->chip;
1574 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1575 struct mtd_info *mtd = nand_to_mtd(chip);
1576 struct nand_ecc_ctrl *ecc = &chip->ecc;
1577 unsigned int max_bitflips = 0;
1578 struct read_stats *buf;
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301579 bool flash_op_err = false;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301580 int i;
1581
1582 buf = (struct read_stats *)nandc->reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301583 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301584
1585 for (i = 0; i < ecc->steps; i++, buf++) {
1586 u32 flash, buffer, erased_cw;
1587 int data_len, oob_len;
1588
1589 if (i == (ecc->steps - 1)) {
1590 data_len = ecc->size - ((ecc->steps - 1) << 2);
1591 oob_len = ecc->steps << 2;
1592 } else {
1593 data_len = host->cw_data;
1594 oob_len = 0;
1595 }
1596
1597 flash = le32_to_cpu(buf->flash);
1598 buffer = le32_to_cpu(buf->buffer);
1599 erased_cw = le32_to_cpu(buf->erased_cw);
1600
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301601 /*
1602 * Check ECC failure for each codeword. ECC failure can
1603 * happen in either of the following conditions
1604 * 1. If number of bitflips are greater than ECC engine
1605 * capability.
1606 * 2. If this codeword contains all 0xff for which erased
1607 * codeword detection check will be done.
1608 */
1609 if ((flash & FS_OP_ERR) && (buffer & BS_UNCORRECTABLE_BIT)) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05301610 bool erased;
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301611 int ret, ecclen, extraooblen;
1612 void *eccbuf;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301613
Abhishek Sahu2f610382018-06-20 12:57:35 +05301614 /*
1615 * For BCH ECC, ignore erased codeword errors, if
1616 * ERASED_CW bits are set.
1617 */
Archit Tanejac76b78d2016-02-03 14:29:50 +05301618 if (host->bch_enabled) {
1619 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1620 true : false;
Abhishek Sahu2f610382018-06-20 12:57:35 +05301621 /*
1622 * For RS ECC, HW reports the erased CW by placing
1623 * special characters at certain offsets in the buffer.
1624 * These special characters will be valid only if
1625 * complete page is read i.e. data_buf is not NULL.
1626 */
1627 } else if (data_buf) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05301628 erased = erased_chunk_check_and_fixup(data_buf,
1629 data_len);
Abhishek Sahu2f610382018-06-20 12:57:35 +05301630 } else {
1631 erased = false;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301632 }
1633
1634 if (erased) {
1635 data_buf += data_len;
1636 if (oob_buf)
1637 oob_buf += oob_len + ecc->bytes;
1638 continue;
1639 }
1640
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301641 eccbuf = oob_buf ? oob_buf + oob_len : NULL;
1642 ecclen = oob_buf ? host->ecc_bytes_hw : 0;
1643 extraooblen = oob_buf ? oob_len : 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301644
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301645 /*
1646 * make sure it isn't an erased page reported
1647 * as not-erased by HW because of a few bitflips
1648 */
1649 ret = nand_check_erased_ecc_chunk(data_buf,
1650 data_len, eccbuf, ecclen, oob_buf,
1651 extraooblen, ecc->strength);
1652 if (ret < 0) {
1653 mtd->ecc_stats.failed++;
1654 } else {
1655 mtd->ecc_stats.corrected += ret;
1656 max_bitflips =
1657 max_t(unsigned int, max_bitflips, ret);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301658 }
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301659 /*
1660 * Check if MPU or any other operational error (timeout,
1661 * device failure, etc.) happened for this codeword and
1662 * make flash_op_err true. If flash_op_err is set, then
1663 * EIO will be returned for page read.
1664 */
1665 } else if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1666 flash_op_err = true;
1667 /*
1668 * No ECC or operational errors happened. Check the number of
1669 * bits corrected and update the ecc_stats.corrected.
1670 */
Archit Tanejac76b78d2016-02-03 14:29:50 +05301671 } else {
1672 unsigned int stat;
1673
1674 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1675 mtd->ecc_stats.corrected += stat;
1676 max_bitflips = max(max_bitflips, stat);
1677 }
1678
Abhishek Sahu2f610382018-06-20 12:57:35 +05301679 if (data_buf)
1680 data_buf += data_len;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301681 if (oob_buf)
1682 oob_buf += oob_len + ecc->bytes;
1683 }
1684
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301685 if (flash_op_err)
1686 return -EIO;
1687
Archit Tanejac76b78d2016-02-03 14:29:50 +05301688 return max_bitflips;
1689}
1690
1691/*
1692 * helper to perform the actual page read operation, used by ecc->read_page(),
1693 * ecc->read_oob()
1694 */
1695static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1696 u8 *oob_buf)
1697{
1698 struct nand_chip *chip = &host->chip;
1699 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1700 struct nand_ecc_ctrl *ecc = &chip->ecc;
1701 int i, ret;
1702
Abhishek Sahubde43302017-07-19 17:17:55 +05301703 config_nand_page_read(nandc);
1704
Archit Tanejac76b78d2016-02-03 14:29:50 +05301705 /* queue cmd descs for each codeword */
1706 for (i = 0; i < ecc->steps; i++) {
1707 int data_size, oob_size;
1708
1709 if (i == (ecc->steps - 1)) {
1710 data_size = ecc->size - ((ecc->steps - 1) << 2);
1711 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1712 host->spare_bytes;
1713 } else {
1714 data_size = host->cw_data;
1715 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1716 }
1717
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301718 if (nandc->props->is_bam) {
1719 if (data_buf && oob_buf) {
1720 nandc_set_read_loc(nandc, 0, 0, data_size, 0);
1721 nandc_set_read_loc(nandc, 1, data_size,
1722 oob_size, 1);
1723 } else if (data_buf) {
1724 nandc_set_read_loc(nandc, 0, 0, data_size, 1);
1725 } else {
1726 nandc_set_read_loc(nandc, 0, data_size,
1727 oob_size, 1);
1728 }
1729 }
1730
Abhishek Sahubde43302017-07-19 17:17:55 +05301731 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301732
1733 if (data_buf)
1734 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301735 data_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301736
1737 /*
1738 * when ecc is enabled, the controller doesn't read the real
1739 * or dummy bad block markers in each chunk. To maintain a
1740 * consistent layout across RAW and ECC reads, we just
1741 * leave the real/dummy BBM offsets empty (i.e, filled with
1742 * 0xffs)
1743 */
1744 if (oob_buf) {
1745 int j;
1746
1747 for (j = 0; j < host->bbm_size; j++)
1748 *oob_buf++ = 0xff;
1749
1750 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301751 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301752 }
1753
1754 if (data_buf)
1755 data_buf += data_size;
1756 if (oob_buf)
1757 oob_buf += oob_size;
1758 }
1759
1760 ret = submit_descs(nandc);
1761 if (ret)
1762 dev_err(nandc->dev, "failure to read page/oob\n");
1763
1764 free_descs(nandc);
1765
1766 return ret;
1767}
1768
1769/*
1770 * a helper that copies the last step/codeword of a page (containing free oob)
1771 * into our local buffer
1772 */
1773static int copy_last_cw(struct qcom_nand_host *host, int page)
1774{
1775 struct nand_chip *chip = &host->chip;
1776 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1777 struct nand_ecc_ctrl *ecc = &chip->ecc;
1778 int size;
1779 int ret;
1780
1781 clear_read_regs(nandc);
1782
1783 size = host->use_ecc ? host->cw_data : host->cw_size;
1784
1785 /* prepare a clean read buffer */
1786 memset(nandc->data_buffer, 0xff, size);
1787
1788 set_address(host, host->cw_size * (ecc->steps - 1), page);
1789 update_rw_regs(host, 1, true);
1790
Abhishek Sahubde43302017-07-19 17:17:55 +05301791 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301792
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301793 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301794
1795 ret = submit_descs(nandc);
1796 if (ret)
1797 dev_err(nandc->dev, "failed to copy last codeword\n");
1798
1799 free_descs(nandc);
1800
1801 return ret;
1802}
1803
1804/* implements ecc->read_page() */
1805static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1806 uint8_t *buf, int oob_required, int page)
1807{
1808 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1809 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1810 u8 *data_buf, *oob_buf = NULL;
1811 int ret;
1812
Boris Brezillon25f815f2017-11-30 18:01:30 +01001813 nand_read_page_op(chip, page, 0, NULL, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301814 data_buf = buf;
1815 oob_buf = oob_required ? chip->oob_poi : NULL;
1816
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301817 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301818 ret = read_page_ecc(host, data_buf, oob_buf);
1819 if (ret) {
1820 dev_err(nandc->dev, "failure to read page\n");
1821 return ret;
1822 }
1823
1824 return parse_read_errors(host, data_buf, oob_buf);
1825}
1826
1827/* implements ecc->read_page_raw() */
1828static int qcom_nandc_read_page_raw(struct mtd_info *mtd,
1829 struct nand_chip *chip, uint8_t *buf,
1830 int oob_required, int page)
1831{
1832 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1833 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1834 u8 *data_buf, *oob_buf;
1835 struct nand_ecc_ctrl *ecc = &chip->ecc;
1836 int i, ret;
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301837 int read_loc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301838
Boris Brezillon25f815f2017-11-30 18:01:30 +01001839 nand_read_page_op(chip, page, 0, NULL, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301840 data_buf = buf;
1841 oob_buf = chip->oob_poi;
1842
1843 host->use_ecc = false;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301844
1845 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301846 update_rw_regs(host, ecc->steps, true);
Abhishek Sahubde43302017-07-19 17:17:55 +05301847 config_nand_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301848
1849 for (i = 0; i < ecc->steps; i++) {
1850 int data_size1, data_size2, oob_size1, oob_size2;
1851 int reg_off = FLASH_BUF_ACC;
1852
1853 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1854 oob_size1 = host->bbm_size;
1855
1856 if (i == (ecc->steps - 1)) {
1857 data_size2 = ecc->size - data_size1 -
1858 ((ecc->steps - 1) << 2);
1859 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1860 host->spare_bytes;
1861 } else {
1862 data_size2 = host->cw_data - data_size1;
1863 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1864 }
1865
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301866 if (nandc->props->is_bam) {
1867 read_loc = 0;
1868 nandc_set_read_loc(nandc, 0, read_loc, data_size1, 0);
1869 read_loc += data_size1;
1870
1871 nandc_set_read_loc(nandc, 1, read_loc, oob_size1, 0);
1872 read_loc += oob_size1;
1873
1874 nandc_set_read_loc(nandc, 2, read_loc, data_size2, 0);
1875 read_loc += data_size2;
1876
1877 nandc_set_read_loc(nandc, 3, read_loc, oob_size2, 1);
1878 }
1879
Abhishek Sahubde43302017-07-19 17:17:55 +05301880 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301881
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301882 read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301883 reg_off += data_size1;
1884 data_buf += data_size1;
1885
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301886 read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301887 reg_off += oob_size1;
1888 oob_buf += oob_size1;
1889
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301890 read_data_dma(nandc, reg_off, data_buf, data_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301891 reg_off += data_size2;
1892 data_buf += data_size2;
1893
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301894 read_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301895 oob_buf += oob_size2;
1896 }
1897
1898 ret = submit_descs(nandc);
1899 if (ret)
1900 dev_err(nandc->dev, "failure to read raw page\n");
1901
1902 free_descs(nandc);
1903
1904 return 0;
1905}
1906
1907/* implements ecc->read_oob() */
1908static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1909 int page)
1910{
1911 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1912 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1913 struct nand_ecc_ctrl *ecc = &chip->ecc;
1914 int ret;
1915
1916 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301917 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301918
1919 host->use_ecc = true;
1920 set_address(host, 0, page);
1921 update_rw_regs(host, ecc->steps, true);
1922
1923 ret = read_page_ecc(host, NULL, chip->oob_poi);
1924 if (ret)
1925 dev_err(nandc->dev, "failure to read oob\n");
1926
1927 return ret;
1928}
1929
1930/* implements ecc->write_page() */
1931static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1932 const uint8_t *buf, int oob_required, int page)
1933{
1934 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1935 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1936 struct nand_ecc_ctrl *ecc = &chip->ecc;
1937 u8 *data_buf, *oob_buf;
1938 int i, ret;
1939
Boris Brezillon25f815f2017-11-30 18:01:30 +01001940 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1941
Archit Tanejac76b78d2016-02-03 14:29:50 +05301942 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301943 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301944
1945 data_buf = (u8 *)buf;
1946 oob_buf = chip->oob_poi;
1947
1948 host->use_ecc = true;
1949 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301950 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301951
1952 for (i = 0; i < ecc->steps; i++) {
1953 int data_size, oob_size;
1954
1955 if (i == (ecc->steps - 1)) {
1956 data_size = ecc->size - ((ecc->steps - 1) << 2);
1957 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1958 host->spare_bytes;
1959 } else {
1960 data_size = host->cw_data;
1961 oob_size = ecc->bytes;
1962 }
1963
Archit Tanejac76b78d2016-02-03 14:29:50 +05301964
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301965 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
1966 i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301967
1968 /*
1969 * when ECC is enabled, we don't really need to write anything
1970 * to oob for the first n - 1 codewords since these oob regions
1971 * just contain ECC bytes that's written by the controller
1972 * itself. For the last codeword, we skip the bbm positions and
1973 * write to the free oob area.
1974 */
1975 if (i == (ecc->steps - 1)) {
1976 oob_buf += host->bbm_size;
1977
1978 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301979 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301980 }
1981
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301982 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301983
1984 data_buf += data_size;
1985 oob_buf += oob_size;
1986 }
1987
1988 ret = submit_descs(nandc);
1989 if (ret)
1990 dev_err(nandc->dev, "failure to write page\n");
1991
1992 free_descs(nandc);
1993
Boris Brezillon25f815f2017-11-30 18:01:30 +01001994 if (!ret)
1995 ret = nand_prog_page_end_op(chip);
1996
Archit Tanejac76b78d2016-02-03 14:29:50 +05301997 return ret;
1998}
1999
2000/* implements ecc->write_page_raw() */
2001static int qcom_nandc_write_page_raw(struct mtd_info *mtd,
2002 struct nand_chip *chip, const uint8_t *buf,
2003 int oob_required, int page)
2004{
2005 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2006 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2007 struct nand_ecc_ctrl *ecc = &chip->ecc;
2008 u8 *data_buf, *oob_buf;
2009 int i, ret;
2010
Boris Brezillon25f815f2017-11-30 18:01:30 +01002011 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302012 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302013 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302014
2015 data_buf = (u8 *)buf;
2016 oob_buf = chip->oob_poi;
2017
2018 host->use_ecc = false;
2019 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302020 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302021
2022 for (i = 0; i < ecc->steps; i++) {
2023 int data_size1, data_size2, oob_size1, oob_size2;
2024 int reg_off = FLASH_BUF_ACC;
2025
2026 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
2027 oob_size1 = host->bbm_size;
2028
2029 if (i == (ecc->steps - 1)) {
2030 data_size2 = ecc->size - data_size1 -
2031 ((ecc->steps - 1) << 2);
2032 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
2033 host->spare_bytes;
2034 } else {
2035 data_size2 = host->cw_data - data_size1;
2036 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
2037 }
2038
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302039 write_data_dma(nandc, reg_off, data_buf, data_size1,
2040 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302041 reg_off += data_size1;
2042 data_buf += data_size1;
2043
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302044 write_data_dma(nandc, reg_off, oob_buf, oob_size1,
2045 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302046 reg_off += oob_size1;
2047 oob_buf += oob_size1;
2048
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302049 write_data_dma(nandc, reg_off, data_buf, data_size2,
2050 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302051 reg_off += data_size2;
2052 data_buf += data_size2;
2053
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302054 write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302055 oob_buf += oob_size2;
2056
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302057 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302058 }
2059
2060 ret = submit_descs(nandc);
2061 if (ret)
2062 dev_err(nandc->dev, "failure to write raw page\n");
2063
2064 free_descs(nandc);
2065
Boris Brezillon25f815f2017-11-30 18:01:30 +01002066 if (!ret)
2067 ret = nand_prog_page_end_op(chip);
2068
Archit Tanejac76b78d2016-02-03 14:29:50 +05302069 return ret;
2070}
2071
2072/*
2073 * implements ecc->write_oob()
2074 *
2075 * the NAND controller cannot write only data or only oob within a codeword,
2076 * since ecc is calculated for the combined codeword. we first copy the
2077 * entire contents for the last codeword(data + oob), replace the old oob
2078 * with the new one in chip->oob_poi, and then write the entire codeword.
2079 * this read-copy-write operation results in a slight performance loss.
2080 */
2081static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
2082 int page)
2083{
2084 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2085 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2086 struct nand_ecc_ctrl *ecc = &chip->ecc;
2087 u8 *oob = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302088 int data_size, oob_size;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002089 int ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302090
2091 host->use_ecc = true;
2092
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302093 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302094 ret = copy_last_cw(host, page);
2095 if (ret)
2096 return ret;
2097
2098 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302099 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302100
2101 /* calculate the data and oob size for the last codeword/step */
2102 data_size = ecc->size - ((ecc->steps - 1) << 2);
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01002103 oob_size = mtd->oobavail;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302104
2105 /* override new oob content to last codeword */
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01002106 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
2107 0, mtd->oobavail);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302108
2109 set_address(host, host->cw_size * (ecc->steps - 1), page);
2110 update_rw_regs(host, 1, false);
2111
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302112 config_nand_page_write(nandc);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302113 write_data_dma(nandc, FLASH_BUF_ACC,
2114 nandc->data_buffer, data_size + oob_size, 0);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302115 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302116
2117 ret = submit_descs(nandc);
2118
2119 free_descs(nandc);
2120
2121 if (ret) {
2122 dev_err(nandc->dev, "failure to write oob\n");
2123 return -EIO;
2124 }
2125
Boris Brezillon97d90da2017-11-30 18:01:29 +01002126 return nand_prog_page_end_op(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302127}
2128
2129static int qcom_nandc_block_bad(struct mtd_info *mtd, loff_t ofs)
2130{
2131 struct nand_chip *chip = mtd_to_nand(mtd);
2132 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2133 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2134 struct nand_ecc_ctrl *ecc = &chip->ecc;
2135 int page, ret, bbpos, bad = 0;
2136 u32 flash_status;
2137
2138 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2139
2140 /*
2141 * configure registers for a raw sub page read, the address is set to
2142 * the beginning of the last codeword, we don't care about reading ecc
2143 * portion of oob. we just want the first few bytes from this codeword
2144 * that contains the BBM
2145 */
2146 host->use_ecc = false;
2147
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302148 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302149 ret = copy_last_cw(host, page);
2150 if (ret)
2151 goto err;
2152
2153 flash_status = le32_to_cpu(nandc->reg_read_buf[0]);
2154
2155 if (flash_status & (FS_OP_ERR | FS_MPU_ERR)) {
2156 dev_warn(nandc->dev, "error when trying to read BBM\n");
2157 goto err;
2158 }
2159
2160 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
2161
2162 bad = nandc->data_buffer[bbpos] != 0xff;
2163
2164 if (chip->options & NAND_BUSWIDTH_16)
2165 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
2166err:
2167 return bad;
2168}
2169
2170static int qcom_nandc_block_markbad(struct mtd_info *mtd, loff_t ofs)
2171{
2172 struct nand_chip *chip = mtd_to_nand(mtd);
2173 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2174 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2175 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002176 int page, ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302177
2178 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302179 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302180
2181 /*
2182 * to mark the BBM as bad, we flash the entire last codeword with 0s.
2183 * we don't care about the rest of the content in the codeword since
2184 * we aren't going to use this block again
2185 */
2186 memset(nandc->data_buffer, 0x00, host->cw_size);
2187
2188 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2189
2190 /* prepare write */
2191 host->use_ecc = false;
2192 set_address(host, host->cw_size * (ecc->steps - 1), page);
2193 update_rw_regs(host, 1, false);
2194
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302195 config_nand_page_write(nandc);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302196 write_data_dma(nandc, FLASH_BUF_ACC,
2197 nandc->data_buffer, host->cw_size, 0);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302198 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302199
2200 ret = submit_descs(nandc);
2201
2202 free_descs(nandc);
2203
2204 if (ret) {
2205 dev_err(nandc->dev, "failure to update BBM\n");
2206 return -EIO;
2207 }
2208
Boris Brezillon97d90da2017-11-30 18:01:29 +01002209 return nand_prog_page_end_op(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302210}
2211
2212/*
2213 * the three functions below implement chip->read_byte(), chip->read_buf()
2214 * and chip->write_buf() respectively. these aren't used for
2215 * reading/writing page data, they are used for smaller data like reading
2216 * id, status etc
2217 */
2218static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
2219{
2220 struct nand_chip *chip = mtd_to_nand(mtd);
2221 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2222 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2223 u8 *buf = nandc->data_buffer;
2224 u8 ret = 0x0;
2225
2226 if (host->last_command == NAND_CMD_STATUS) {
2227 ret = host->status;
2228
2229 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2230
2231 return ret;
2232 }
2233
2234 if (nandc->buf_start < nandc->buf_count)
2235 ret = buf[nandc->buf_start++];
2236
2237 return ret;
2238}
2239
2240static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
2241{
2242 struct nand_chip *chip = mtd_to_nand(mtd);
2243 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2244 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2245
2246 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
2247 nandc->buf_start += real_len;
2248}
2249
2250static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
2251 int len)
2252{
2253 struct nand_chip *chip = mtd_to_nand(mtd);
2254 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2255 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2256
2257 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
2258
2259 nandc->buf_start += real_len;
2260}
2261
2262/* we support only one external chip for now */
2263static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
2264{
2265 struct nand_chip *chip = mtd_to_nand(mtd);
2266 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2267
2268 if (chipnr <= 0)
2269 return;
2270
2271 dev_warn(nandc->dev, "invalid chip select\n");
2272}
2273
2274/*
2275 * NAND controller page layout info
2276 *
2277 * Layout with ECC enabled:
2278 *
2279 * |----------------------| |---------------------------------|
2280 * | xx.......yy| | *********xx.......yy|
2281 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
2282 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
2283 * | xx.......yy| | *********xx.......yy|
2284 * |----------------------| |---------------------------------|
2285 * codeword 1,2..n-1 codeword n
2286 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
2287 *
2288 * n = Number of codewords in the page
2289 * . = ECC bytes
2290 * * = Spare/free bytes
2291 * x = Unused byte(s)
2292 * y = Reserved byte(s)
2293 *
2294 * 2K page: n = 4, spare = 16 bytes
2295 * 4K page: n = 8, spare = 32 bytes
2296 * 8K page: n = 16, spare = 64 bytes
2297 *
2298 * the qcom nand controller operates at a sub page/codeword level. each
2299 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2300 * the number of ECC bytes vary based on the ECC strength and the bus width.
2301 *
2302 * the first n - 1 codewords contains 516 bytes of user data, the remaining
2303 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2304 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2305 *
2306 * When we access a page with ECC enabled, the reserved bytes(s) are not
2307 * accessible at all. When reading, we fill up these unreadable positions
2308 * with 0xffs. When writing, the controller skips writing the inaccessible
2309 * bytes.
2310 *
2311 * Layout with ECC disabled:
2312 *
2313 * |------------------------------| |---------------------------------------|
2314 * | yy xx.......| | bb *********xx.......|
2315 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
2316 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
2317 * | yy xx.......| | bb *********xx.......|
2318 * |------------------------------| |---------------------------------------|
2319 * codeword 1,2..n-1 codeword n
2320 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
2321 *
2322 * n = Number of codewords in the page
2323 * . = ECC bytes
2324 * * = Spare/free bytes
2325 * x = Unused byte(s)
2326 * y = Dummy Bad Bock byte(s)
2327 * b = Real Bad Block byte(s)
2328 * size1/size2 = function of codeword size and 'n'
2329 *
2330 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2331 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2332 * Block Markers. In the last codeword, this position contains the real BBM
2333 *
2334 * In order to have a consistent layout between RAW and ECC modes, we assume
2335 * the following OOB layout arrangement:
2336 *
2337 * |-----------| |--------------------|
2338 * |yyxx.......| |bb*********xx.......|
2339 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
2340 * |yyxx.......| |bb*********xx.......|
2341 * |yyxx.......| |bb*********xx.......|
2342 * |-----------| |--------------------|
2343 * first n - 1 nth OOB region
2344 * OOB regions
2345 *
2346 * n = Number of codewords in the page
2347 * . = ECC bytes
2348 * * = FREE OOB bytes
2349 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2350 * x = Unused byte(s)
2351 * b = Real bad block byte(s) (inaccessible when ECC enabled)
2352 *
2353 * This layout is read as is when ECC is disabled. When ECC is enabled, the
2354 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2355 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
Boris Brezillon421e81c2016-03-18 17:54:27 +01002356 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2357 * the sum of the three).
Archit Tanejac76b78d2016-02-03 14:29:50 +05302358 */
Boris Brezillon421e81c2016-03-18 17:54:27 +01002359static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2360 struct mtd_oob_region *oobregion)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302361{
Boris Brezillon421e81c2016-03-18 17:54:27 +01002362 struct nand_chip *chip = mtd_to_nand(mtd);
2363 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302364 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302365
Boris Brezillon421e81c2016-03-18 17:54:27 +01002366 if (section > 1)
2367 return -ERANGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302368
Boris Brezillon421e81c2016-03-18 17:54:27 +01002369 if (!section) {
2370 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
2371 host->bbm_size;
2372 oobregion->offset = 0;
2373 } else {
2374 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
2375 oobregion->offset = mtd->oobsize - oobregion->length;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302376 }
2377
Boris Brezillon421e81c2016-03-18 17:54:27 +01002378 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302379}
2380
Boris Brezillon421e81c2016-03-18 17:54:27 +01002381static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
2382 struct mtd_oob_region *oobregion)
2383{
2384 struct nand_chip *chip = mtd_to_nand(mtd);
2385 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2386 struct nand_ecc_ctrl *ecc = &chip->ecc;
2387
2388 if (section)
2389 return -ERANGE;
2390
2391 oobregion->length = ecc->steps * 4;
2392 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
2393
2394 return 0;
2395}
2396
2397static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
2398 .ecc = qcom_nand_ooblayout_ecc,
2399 .free = qcom_nand_ooblayout_free,
2400};
2401
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302402static int
2403qcom_nandc_calc_ecc_bytes(int step_size, int strength)
2404{
2405 return strength == 4 ? 12 : 16;
2406}
2407NAND_ECC_CAPS_SINGLE(qcom_nandc_ecc_caps, qcom_nandc_calc_ecc_bytes,
2408 NANDC_STEP_SIZE, 4, 8);
2409
Archit Tanejac76b78d2016-02-03 14:29:50 +05302410static int qcom_nand_host_setup(struct qcom_nand_host *host)
2411{
2412 struct nand_chip *chip = &host->chip;
2413 struct mtd_info *mtd = nand_to_mtd(chip);
2414 struct nand_ecc_ctrl *ecc = &chip->ecc;
2415 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302416 int cwperpage, bad_block_byte, ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302417 bool wide_bus;
2418 int ecc_mode = 1;
2419
Abhishek Sahu320bdb52018-06-20 12:57:31 +05302420 /* controller only supports 512 bytes data steps */
2421 ecc->size = NANDC_STEP_SIZE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302422 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302423 cwperpage = mtd->writesize / NANDC_STEP_SIZE;
2424
2425 /*
2426 * Each CW has 4 available OOB bytes which will be protected with ECC
2427 * so remaining bytes can be used for ECC.
2428 */
2429 ret = nand_ecc_choose_conf(chip, &qcom_nandc_ecc_caps,
2430 mtd->oobsize - (cwperpage * 4));
2431 if (ret) {
2432 dev_err(nandc->dev, "No valid ECC settings possible\n");
2433 return ret;
2434 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302435
2436 if (ecc->strength >= 8) {
2437 /* 8 bit ECC defaults to BCH ECC on all platforms */
2438 host->bch_enabled = true;
2439 ecc_mode = 1;
2440
2441 if (wide_bus) {
2442 host->ecc_bytes_hw = 14;
2443 host->spare_bytes = 0;
2444 host->bbm_size = 2;
2445 } else {
2446 host->ecc_bytes_hw = 13;
2447 host->spare_bytes = 2;
2448 host->bbm_size = 1;
2449 }
2450 } else {
2451 /*
2452 * if the controller supports BCH for 4 bit ECC, the controller
2453 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2454 * always 10 bytes
2455 */
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302456 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302457 /* BCH */
2458 host->bch_enabled = true;
2459 ecc_mode = 0;
2460
2461 if (wide_bus) {
2462 host->ecc_bytes_hw = 8;
2463 host->spare_bytes = 2;
2464 host->bbm_size = 2;
2465 } else {
2466 host->ecc_bytes_hw = 7;
2467 host->spare_bytes = 4;
2468 host->bbm_size = 1;
2469 }
2470 } else {
2471 /* RS */
2472 host->ecc_bytes_hw = 10;
2473
2474 if (wide_bus) {
2475 host->spare_bytes = 0;
2476 host->bbm_size = 2;
2477 } else {
2478 host->spare_bytes = 1;
2479 host->bbm_size = 1;
2480 }
2481 }
2482 }
2483
2484 /*
2485 * we consider ecc->bytes as the sum of all the non-data content in a
2486 * step. It gives us a clean representation of the oob area (even if
2487 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2488 * ECC and 12 bytes for 4 bit ECC
2489 */
2490 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
2491
2492 ecc->read_page = qcom_nandc_read_page;
2493 ecc->read_page_raw = qcom_nandc_read_page_raw;
2494 ecc->read_oob = qcom_nandc_read_oob;
2495 ecc->write_page = qcom_nandc_write_page;
2496 ecc->write_page_raw = qcom_nandc_write_page_raw;
2497 ecc->write_oob = qcom_nandc_write_oob;
2498
2499 ecc->mode = NAND_ECC_HW;
2500
Boris Brezillon421e81c2016-03-18 17:54:27 +01002501 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302502
Abhishek Sahucb80f112017-08-17 17:37:40 +05302503 nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
2504 cwperpage);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302505
2506 /*
2507 * DATA_UD_BYTES varies based on whether the read/write command protects
2508 * spare data with ECC too. We protect spare data by default, so we set
2509 * it to main + spare data, which are 512 and 4 bytes respectively.
2510 */
2511 host->cw_data = 516;
2512
2513 /*
2514 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2515 * for 8 bit ECC
2516 */
2517 host->cw_size = host->cw_data + ecc->bytes;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302518 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
2519
2520 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
2521 | host->cw_data << UD_SIZE_BYTES
2522 | 0 << DISABLE_STATUS_AFTER_WRITE
2523 | 5 << NUM_ADDR_CYCLES
2524 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
2525 | 0 << STATUS_BFR_READ
2526 | 1 << SET_RD_MODE_AFTER_STATUS
2527 | host->spare_bytes << SPARE_SIZE_BYTES;
2528
2529 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
2530 | 0 << CS_ACTIVE_BSY
2531 | bad_block_byte << BAD_BLOCK_BYTE_NUM
2532 | 0 << BAD_BLOCK_IN_SPARE_AREA
2533 | 2 << WR_RD_BSY_GAP
2534 | wide_bus << WIDE_FLASH
2535 | host->bch_enabled << ENABLE_BCH_ECC;
2536
2537 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
2538 | host->cw_size << UD_SIZE_BYTES
2539 | 5 << NUM_ADDR_CYCLES
2540 | 0 << SPARE_SIZE_BYTES;
2541
2542 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
2543 | 0 << CS_ACTIVE_BSY
2544 | 17 << BAD_BLOCK_BYTE_NUM
2545 | 1 << BAD_BLOCK_IN_SPARE_AREA
2546 | 2 << WR_RD_BSY_GAP
2547 | wide_bus << WIDE_FLASH
2548 | 1 << DEV0_CFG1_ECC_DISABLE;
2549
Abhishek Sahu10777de2017-08-03 17:56:39 +02002550 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
Archit Tanejac76b78d2016-02-03 14:29:50 +05302551 | 0 << ECC_SW_RESET
2552 | host->cw_data << ECC_NUM_DATA_BYTES
2553 | 1 << ECC_FORCE_CLK_OPEN
2554 | ecc_mode << ECC_MODE
2555 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
2556
2557 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
2558
2559 host->clrflashstatus = FS_READY_BSY_N;
2560 host->clrreadstatus = 0xc0;
Abhishek Sahua86b9c42017-08-17 17:37:44 +05302561 nandc->regs->erased_cw_detect_cfg_clr =
2562 cpu_to_le32(CLR_ERASED_PAGE_DET);
2563 nandc->regs->erased_cw_detect_cfg_set =
2564 cpu_to_le32(SET_ERASED_PAGE_DET);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302565
2566 dev_dbg(nandc->dev,
2567 "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",
2568 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
2569 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
2570 cwperpage);
2571
2572 return 0;
2573}
2574
2575static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
2576{
2577 int ret;
2578
2579 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
2580 if (ret) {
2581 dev_err(nandc->dev, "failed to set DMA mask\n");
2582 return ret;
2583 }
2584
2585 /*
2586 * we use the internal buffer for reading ONFI params, reading small
2587 * data like ID and status, and preforming read-copy-write operations
2588 * when writing to a codeword partially. 532 is the maximum possible
2589 * size of a codeword for our nand controller
2590 */
2591 nandc->buf_size = 532;
2592
2593 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
2594 GFP_KERNEL);
2595 if (!nandc->data_buffer)
2596 return -ENOMEM;
2597
2598 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
2599 GFP_KERNEL);
2600 if (!nandc->regs)
2601 return -ENOMEM;
2602
Kees Cooka86854d2018-06-12 14:07:58 -07002603 nandc->reg_read_buf = devm_kcalloc(nandc->dev,
2604 MAX_REG_RD, sizeof(*nandc->reg_read_buf),
Archit Tanejac76b78d2016-02-03 14:29:50 +05302605 GFP_KERNEL);
2606 if (!nandc->reg_read_buf)
2607 return -ENOMEM;
2608
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302609 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302610 nandc->reg_read_dma =
2611 dma_map_single(nandc->dev, nandc->reg_read_buf,
2612 MAX_REG_RD *
2613 sizeof(*nandc->reg_read_buf),
2614 DMA_FROM_DEVICE);
2615 if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
2616 dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
2617 return -EIO;
2618 }
2619
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302620 nandc->tx_chan = dma_request_slave_channel(nandc->dev, "tx");
2621 if (!nandc->tx_chan) {
2622 dev_err(nandc->dev, "failed to request tx channel\n");
2623 return -ENODEV;
2624 }
2625
2626 nandc->rx_chan = dma_request_slave_channel(nandc->dev, "rx");
2627 if (!nandc->rx_chan) {
2628 dev_err(nandc->dev, "failed to request rx channel\n");
2629 return -ENODEV;
2630 }
2631
2632 nandc->cmd_chan = dma_request_slave_channel(nandc->dev, "cmd");
2633 if (!nandc->cmd_chan) {
2634 dev_err(nandc->dev, "failed to request cmd channel\n");
2635 return -ENODEV;
2636 }
Abhishek Sahucb80f112017-08-17 17:37:40 +05302637
2638 /*
2639 * Initially allocate BAM transaction to read ONFI param page.
2640 * After detecting all the devices, this BAM transaction will
2641 * be freed and the next BAM tranasction will be allocated with
2642 * maximum codeword size
2643 */
2644 nandc->max_cwperpage = 1;
2645 nandc->bam_txn = alloc_bam_transaction(nandc);
2646 if (!nandc->bam_txn) {
2647 dev_err(nandc->dev,
2648 "failed to allocate bam transaction\n");
2649 return -ENOMEM;
2650 }
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302651 } else {
2652 nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
2653 if (!nandc->chan) {
2654 dev_err(nandc->dev,
2655 "failed to request slave channel\n");
2656 return -ENODEV;
2657 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302658 }
2659
2660 INIT_LIST_HEAD(&nandc->desc_list);
2661 INIT_LIST_HEAD(&nandc->host_list);
2662
Marc Gonzalezd45bc582016-07-27 11:23:52 +02002663 nand_hw_control_init(&nandc->controller);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302664
2665 return 0;
2666}
2667
2668static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2669{
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302670 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302671 if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
2672 dma_unmap_single(nandc->dev, nandc->reg_read_dma,
2673 MAX_REG_RD *
2674 sizeof(*nandc->reg_read_buf),
2675 DMA_FROM_DEVICE);
2676
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302677 if (nandc->tx_chan)
2678 dma_release_channel(nandc->tx_chan);
2679
2680 if (nandc->rx_chan)
2681 dma_release_channel(nandc->rx_chan);
2682
2683 if (nandc->cmd_chan)
2684 dma_release_channel(nandc->cmd_chan);
2685 } else {
2686 if (nandc->chan)
2687 dma_release_channel(nandc->chan);
2688 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302689}
2690
2691/* one time setup of a few nand controller registers */
2692static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2693{
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302694 u32 nand_ctrl;
2695
Archit Tanejac76b78d2016-02-03 14:29:50 +05302696 /* kill onenand */
2697 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
Abhishek Sahucc409b92017-08-17 17:37:47 +05302698 nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD),
2699 NAND_DEV_CMD_VLD_VAL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302700
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302701 /* enable ADM or BAM DMA */
2702 if (nandc->props->is_bam) {
2703 nand_ctrl = nandc_read(nandc, NAND_CTRL);
2704 nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
2705 } else {
2706 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2707 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302708
2709 /* save the original values of these registers */
Abhishek Sahucc409b92017-08-17 17:37:47 +05302710 nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1));
Abhishek Sahud8a9b322017-08-11 17:09:16 +05302711 nandc->vld = NAND_DEV_CMD_VLD_VAL;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302712
2713 return 0;
2714}
2715
2716static int qcom_nand_host_init(struct qcom_nand_controller *nandc,
2717 struct qcom_nand_host *host,
2718 struct device_node *dn)
2719{
2720 struct nand_chip *chip = &host->chip;
2721 struct mtd_info *mtd = nand_to_mtd(chip);
2722 struct device *dev = nandc->dev;
2723 int ret;
2724
2725 ret = of_property_read_u32(dn, "reg", &host->cs);
2726 if (ret) {
2727 dev_err(dev, "can't get chip-select\n");
2728 return -ENXIO;
2729 }
2730
2731 nand_set_flash_node(chip, dn);
2732 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
Fabio Estevam069f0532018-01-05 18:02:55 -02002733 if (!mtd->name)
2734 return -ENOMEM;
2735
Archit Tanejac76b78d2016-02-03 14:29:50 +05302736 mtd->owner = THIS_MODULE;
2737 mtd->dev.parent = dev;
2738
2739 chip->cmdfunc = qcom_nandc_command;
2740 chip->select_chip = qcom_nandc_select_chip;
2741 chip->read_byte = qcom_nandc_read_byte;
2742 chip->read_buf = qcom_nandc_read_buf;
2743 chip->write_buf = qcom_nandc_write_buf;
Miquel Raynalb9587582018-03-19 14:47:19 +01002744 chip->set_features = nand_get_set_features_notsupp;
2745 chip->get_features = nand_get_set_features_notsupp;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302746
2747 /*
2748 * the bad block marker is readable only when we read the last codeword
2749 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2750 * helpers don't allow us to read BB from a nand chip with ECC
2751 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2752 * and block_markbad helpers until we permanently switch to using
2753 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2754 */
2755 chip->block_bad = qcom_nandc_block_bad;
2756 chip->block_markbad = qcom_nandc_block_markbad;
2757
2758 chip->controller = &nandc->controller;
2759 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
2760 NAND_SKIP_BBTSCAN;
2761
2762 /* set up initial status value */
2763 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2764
2765 ret = nand_scan_ident(mtd, 1, NULL);
2766 if (ret)
2767 return ret;
2768
2769 ret = qcom_nand_host_setup(host);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302770
2771 return ret;
2772}
2773
2774static int qcom_nand_mtd_register(struct qcom_nand_controller *nandc,
2775 struct qcom_nand_host *host,
2776 struct device_node *dn)
2777{
2778 struct nand_chip *chip = &host->chip;
2779 struct mtd_info *mtd = nand_to_mtd(chip);
2780 int ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302781
2782 ret = nand_scan_tail(mtd);
2783 if (ret)
2784 return ret;
2785
Abhishek Sahu89f51272017-07-19 17:17:58 +05302786 ret = mtd_device_register(mtd, NULL, 0);
2787 if (ret)
2788 nand_cleanup(mtd_to_nand(mtd));
2789
2790 return ret;
2791}
2792
2793static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2794{
2795 struct device *dev = nandc->dev;
2796 struct device_node *dn = dev->of_node, *child;
2797 struct qcom_nand_host *host, *tmp;
2798 int ret;
2799
2800 for_each_available_child_of_node(dn, child) {
2801 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2802 if (!host) {
2803 of_node_put(child);
2804 return -ENOMEM;
2805 }
2806
2807 ret = qcom_nand_host_init(nandc, host, child);
2808 if (ret) {
2809 devm_kfree(dev, host);
2810 continue;
2811 }
2812
2813 list_add_tail(&host->node, &nandc->host_list);
2814 }
2815
2816 if (list_empty(&nandc->host_list))
2817 return -ENODEV;
2818
Abhishek Sahucb80f112017-08-17 17:37:40 +05302819 if (nandc->props->is_bam) {
2820 free_bam_transaction(nandc);
2821 nandc->bam_txn = alloc_bam_transaction(nandc);
2822 if (!nandc->bam_txn) {
2823 dev_err(nandc->dev,
2824 "failed to allocate bam transaction\n");
2825 return -ENOMEM;
2826 }
2827 }
2828
Abhishek Sahu89f51272017-07-19 17:17:58 +05302829 list_for_each_entry_safe(host, tmp, &nandc->host_list, node) {
2830 ret = qcom_nand_mtd_register(nandc, host, child);
2831 if (ret) {
2832 list_del(&host->node);
2833 devm_kfree(dev, host);
2834 }
2835 }
2836
2837 if (list_empty(&nandc->host_list))
2838 return -ENODEV;
2839
2840 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302841}
2842
2843/* parse custom DT properties here */
2844static int qcom_nandc_parse_dt(struct platform_device *pdev)
2845{
2846 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2847 struct device_node *np = nandc->dev->of_node;
2848 int ret;
2849
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302850 if (!nandc->props->is_bam) {
2851 ret = of_property_read_u32(np, "qcom,cmd-crci",
2852 &nandc->cmd_crci);
2853 if (ret) {
2854 dev_err(nandc->dev, "command CRCI unspecified\n");
2855 return ret;
2856 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302857
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302858 ret = of_property_read_u32(np, "qcom,data-crci",
2859 &nandc->data_crci);
2860 if (ret) {
2861 dev_err(nandc->dev, "data CRCI unspecified\n");
2862 return ret;
2863 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302864 }
2865
2866 return 0;
2867}
2868
2869static int qcom_nandc_probe(struct platform_device *pdev)
2870{
2871 struct qcom_nand_controller *nandc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302872 const void *dev_data;
2873 struct device *dev = &pdev->dev;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302874 struct resource *res;
2875 int ret;
2876
2877 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2878 if (!nandc)
2879 return -ENOMEM;
2880
2881 platform_set_drvdata(pdev, nandc);
2882 nandc->dev = dev;
2883
2884 dev_data = of_device_get_match_data(dev);
2885 if (!dev_data) {
2886 dev_err(&pdev->dev, "failed to get device data\n");
2887 return -ENODEV;
2888 }
2889
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302890 nandc->props = dev_data;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302891
2892 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2893 nandc->base = devm_ioremap_resource(dev, res);
2894 if (IS_ERR(nandc->base))
2895 return PTR_ERR(nandc->base);
2896
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05302897 nandc->base_phys = res->start;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302898 nandc->base_dma = phys_to_dma(dev, (phys_addr_t)res->start);
2899
2900 nandc->core_clk = devm_clk_get(dev, "core");
2901 if (IS_ERR(nandc->core_clk))
2902 return PTR_ERR(nandc->core_clk);
2903
2904 nandc->aon_clk = devm_clk_get(dev, "aon");
2905 if (IS_ERR(nandc->aon_clk))
2906 return PTR_ERR(nandc->aon_clk);
2907
2908 ret = qcom_nandc_parse_dt(pdev);
2909 if (ret)
2910 return ret;
2911
2912 ret = qcom_nandc_alloc(nandc);
2913 if (ret)
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302914 goto err_core_clk;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302915
2916 ret = clk_prepare_enable(nandc->core_clk);
2917 if (ret)
2918 goto err_core_clk;
2919
2920 ret = clk_prepare_enable(nandc->aon_clk);
2921 if (ret)
2922 goto err_aon_clk;
2923
2924 ret = qcom_nandc_setup(nandc);
2925 if (ret)
2926 goto err_setup;
2927
Abhishek Sahu89f51272017-07-19 17:17:58 +05302928 ret = qcom_probe_nand_devices(nandc);
2929 if (ret)
2930 goto err_setup;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302931
2932 return 0;
2933
Archit Tanejac76b78d2016-02-03 14:29:50 +05302934err_setup:
2935 clk_disable_unprepare(nandc->aon_clk);
2936err_aon_clk:
2937 clk_disable_unprepare(nandc->core_clk);
2938err_core_clk:
2939 qcom_nandc_unalloc(nandc);
2940
2941 return ret;
2942}
2943
2944static int qcom_nandc_remove(struct platform_device *pdev)
2945{
2946 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2947 struct qcom_nand_host *host;
2948
2949 list_for_each_entry(host, &nandc->host_list, node)
2950 nand_release(nand_to_mtd(&host->chip));
2951
2952 qcom_nandc_unalloc(nandc);
2953
2954 clk_disable_unprepare(nandc->aon_clk);
2955 clk_disable_unprepare(nandc->core_clk);
2956
2957 return 0;
2958}
2959
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302960static const struct qcom_nandc_props ipq806x_nandc_props = {
2961 .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +05302962 .is_bam = false,
Abhishek Sahucc409b92017-08-17 17:37:47 +05302963 .dev_cmd_reg_start = 0x0,
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302964};
Archit Tanejac76b78d2016-02-03 14:29:50 +05302965
Abhishek Sahua0637832017-08-17 17:37:53 +05302966static const struct qcom_nandc_props ipq4019_nandc_props = {
2967 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
2968 .is_bam = true,
2969 .dev_cmd_reg_start = 0x0,
2970};
2971
Abhishek Sahudce84762017-08-17 17:37:54 +05302972static const struct qcom_nandc_props ipq8074_nandc_props = {
2973 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
2974 .is_bam = true,
2975 .dev_cmd_reg_start = 0x7000,
2976};
2977
Archit Tanejac76b78d2016-02-03 14:29:50 +05302978/*
2979 * data will hold a struct pointer containing more differences once we support
2980 * more controller variants
2981 */
2982static const struct of_device_id qcom_nandc_of_match[] = {
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302983 {
2984 .compatible = "qcom,ipq806x-nand",
2985 .data = &ipq806x_nandc_props,
Archit Tanejac76b78d2016-02-03 14:29:50 +05302986 },
Abhishek Sahua0637832017-08-17 17:37:53 +05302987 {
2988 .compatible = "qcom,ipq4019-nand",
2989 .data = &ipq4019_nandc_props,
2990 },
Abhishek Sahudce84762017-08-17 17:37:54 +05302991 {
2992 .compatible = "qcom,ipq8074-nand",
2993 .data = &ipq8074_nandc_props,
2994 },
Archit Tanejac76b78d2016-02-03 14:29:50 +05302995 {}
2996};
2997MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
2998
2999static struct platform_driver qcom_nandc_driver = {
3000 .driver = {
3001 .name = "qcom-nandc",
3002 .of_match_table = qcom_nandc_of_match,
3003 },
3004 .probe = qcom_nandc_probe,
3005 .remove = qcom_nandc_remove,
3006};
3007module_platform_driver(qcom_nandc_driver);
3008
3009MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
3010MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
3011MODULE_LICENSE("GPL v2");