blob: 49113d4cee10c0dbb34c757caa489411b4f4b7e4 [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>
Archit Tanejac76b78d2016-02-03 14:29:50 +053026
27/* NANDc reg offsets */
28#define NAND_FLASH_CMD 0x00
29#define NAND_ADDR0 0x04
30#define NAND_ADDR1 0x08
31#define NAND_FLASH_CHIP_SELECT 0x0c
32#define NAND_EXEC_CMD 0x10
33#define NAND_FLASH_STATUS 0x14
34#define NAND_BUFFER_STATUS 0x18
35#define NAND_DEV0_CFG0 0x20
36#define NAND_DEV0_CFG1 0x24
37#define NAND_DEV0_ECC_CFG 0x28
38#define NAND_DEV1_ECC_CFG 0x2c
39#define NAND_DEV1_CFG0 0x30
40#define NAND_DEV1_CFG1 0x34
41#define NAND_READ_ID 0x40
42#define NAND_READ_STATUS 0x44
43#define NAND_DEV_CMD0 0xa0
44#define NAND_DEV_CMD1 0xa4
45#define NAND_DEV_CMD2 0xa8
46#define NAND_DEV_CMD_VLD 0xac
47#define SFLASHC_BURST_CFG 0xe0
48#define NAND_ERASED_CW_DETECT_CFG 0xe8
49#define NAND_ERASED_CW_DETECT_STATUS 0xec
50#define NAND_EBI2_ECC_BUF_CFG 0xf0
51#define FLASH_BUF_ACC 0x100
52
53#define NAND_CTRL 0xf00
54#define NAND_VERSION 0xf08
55#define NAND_READ_LOCATION_0 0xf20
56#define NAND_READ_LOCATION_1 0xf24
Abhishek Sahu91af95c2017-08-17 17:37:43 +053057#define NAND_READ_LOCATION_2 0xf28
58#define NAND_READ_LOCATION_3 0xf2c
Archit Tanejac76b78d2016-02-03 14:29:50 +053059
60/* dummy register offsets, used by write_reg_dma */
61#define NAND_DEV_CMD1_RESTORE 0xdead
62#define NAND_DEV_CMD_VLD_RESTORE 0xbeef
63
64/* NAND_FLASH_CMD bits */
65#define PAGE_ACC BIT(4)
66#define LAST_PAGE BIT(5)
67
68/* NAND_FLASH_CHIP_SELECT bits */
69#define NAND_DEV_SEL 0
70#define DM_EN BIT(2)
71
72/* NAND_FLASH_STATUS bits */
73#define FS_OP_ERR BIT(4)
74#define FS_READY_BSY_N BIT(5)
75#define FS_MPU_ERR BIT(8)
76#define FS_DEVICE_STS_ERR BIT(16)
77#define FS_DEVICE_WP BIT(23)
78
79/* NAND_BUFFER_STATUS bits */
80#define BS_UNCORRECTABLE_BIT BIT(8)
81#define BS_CORRECTABLE_ERR_MSK 0x1f
82
83/* NAND_DEVn_CFG0 bits */
84#define DISABLE_STATUS_AFTER_WRITE 4
85#define CW_PER_PAGE 6
86#define UD_SIZE_BYTES 9
87#define ECC_PARITY_SIZE_BYTES_RS 19
88#define SPARE_SIZE_BYTES 23
89#define NUM_ADDR_CYCLES 27
90#define STATUS_BFR_READ 30
91#define SET_RD_MODE_AFTER_STATUS 31
92
93/* NAND_DEVn_CFG0 bits */
94#define DEV0_CFG1_ECC_DISABLE 0
95#define WIDE_FLASH 1
96#define NAND_RECOVERY_CYCLES 2
97#define CS_ACTIVE_BSY 5
98#define BAD_BLOCK_BYTE_NUM 6
99#define BAD_BLOCK_IN_SPARE_AREA 16
100#define WR_RD_BSY_GAP 17
101#define ENABLE_BCH_ECC 27
102
103/* NAND_DEV0_ECC_CFG bits */
104#define ECC_CFG_ECC_DISABLE 0
105#define ECC_SW_RESET 1
106#define ECC_MODE 4
107#define ECC_PARITY_SIZE_BYTES_BCH 8
108#define ECC_NUM_DATA_BYTES 16
109#define ECC_FORCE_CLK_OPEN 30
110
111/* NAND_DEV_CMD1 bits */
112#define READ_ADDR 0
113
114/* NAND_DEV_CMD_VLD bits */
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530115#define READ_START_VLD BIT(0)
116#define READ_STOP_VLD BIT(1)
117#define WRITE_START_VLD BIT(2)
118#define ERASE_START_VLD BIT(3)
119#define SEQ_READ_START_VLD BIT(4)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530120
121/* NAND_EBI2_ECC_BUF_CFG bits */
122#define NUM_STEPS 0
123
124/* NAND_ERASED_CW_DETECT_CFG bits */
125#define ERASED_CW_ECC_MASK 1
126#define AUTO_DETECT_RES 0
127#define MASK_ECC (1 << ERASED_CW_ECC_MASK)
128#define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
129#define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
130#define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
131#define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
132
133/* NAND_ERASED_CW_DETECT_STATUS bits */
134#define PAGE_ALL_ERASED BIT(7)
135#define CODEWORD_ALL_ERASED BIT(6)
136#define PAGE_ERASED BIT(5)
137#define CODEWORD_ERASED BIT(4)
138#define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
139#define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
140
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530141/* NAND_READ_LOCATION_n bits */
142#define READ_LOCATION_OFFSET 0
143#define READ_LOCATION_SIZE 16
144#define READ_LOCATION_LAST 31
145
Archit Tanejac76b78d2016-02-03 14:29:50 +0530146/* Version Mask */
147#define NAND_VERSION_MAJOR_MASK 0xf0000000
148#define NAND_VERSION_MAJOR_SHIFT 28
149#define NAND_VERSION_MINOR_MASK 0x0fff0000
150#define NAND_VERSION_MINOR_SHIFT 16
151
152/* NAND OP_CMDs */
153#define PAGE_READ 0x2
154#define PAGE_READ_WITH_ECC 0x3
155#define PAGE_READ_WITH_ECC_SPARE 0x4
156#define PROGRAM_PAGE 0x6
157#define PAGE_PROGRAM_WITH_ECC 0x7
158#define PROGRAM_PAGE_SPARE 0x9
159#define BLOCK_ERASE 0xa
160#define FETCH_ID 0xb
161#define RESET_DEVICE 0xd
162
Abhishek Sahud8a9b322017-08-11 17:09:16 +0530163/* Default Value for NAND_DEV_CMD_VLD */
164#define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \
165 ERASE_START_VLD | SEQ_READ_START_VLD)
166
Abhishek Sahu9d43f912017-08-17 17:37:45 +0530167/* NAND_CTRL bits */
168#define BAM_MODE_EN BIT(0)
169
Archit Tanejac76b78d2016-02-03 14:29:50 +0530170/*
171 * the NAND controller performs reads/writes with ECC in 516 byte chunks.
172 * the driver calls the chunks 'step' or 'codeword' interchangeably
173 */
174#define NANDC_STEP_SIZE 512
175
176/*
177 * the largest page size we support is 8K, this will have 16 steps/codewords
178 * of 512 bytes each
179 */
180#define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
181
182/* we read at most 3 registers per codeword scan */
183#define MAX_REG_RD (3 * MAX_NUM_STEPS)
184
185/* ECC modes supported by the controller */
186#define ECC_NONE BIT(0)
187#define ECC_RS_4BIT BIT(1)
188#define ECC_BCH_4BIT BIT(2)
189#define ECC_BCH_8BIT BIT(3)
190
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530191#define nandc_set_read_loc(nandc, reg, offset, size, is_last) \
192nandc_set_reg(nandc, NAND_READ_LOCATION_##reg, \
193 ((offset) << READ_LOCATION_OFFSET) | \
194 ((size) << READ_LOCATION_SIZE) | \
195 ((is_last) << READ_LOCATION_LAST))
196
Abhishek Sahucc409b92017-08-17 17:37:47 +0530197/*
198 * Returns the actual register address for all NAND_DEV_ registers
199 * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD)
200 */
201#define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg))
202
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530203/* Returns the NAND register physical address */
204#define nandc_reg_phys(chip, offset) ((chip)->base_phys + (offset))
205
206/* Returns the dma address for reg read buffer */
207#define reg_buf_dma_addr(chip, vaddr) \
208 ((chip)->reg_read_dma + \
209 ((uint8_t *)(vaddr) - (uint8_t *)(chip)->reg_read_buf))
210
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530211#define QPIC_PER_CW_CMD_ELEMENTS 32
Abhishek Sahucb80f112017-08-17 17:37:40 +0530212#define QPIC_PER_CW_CMD_SGL 32
213#define QPIC_PER_CW_DATA_SGL 8
214
Abhishek Sahu6f200702018-06-20 12:57:33 +0530215#define QPIC_NAND_COMPLETION_TIMEOUT msecs_to_jiffies(2000)
216
Abhishek Sahucb80f112017-08-17 17:37:40 +0530217/*
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530218 * Flags used in DMA descriptor preparation helper functions
219 * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
220 */
221/* Don't set the EOT in current tx BAM sgl */
222#define NAND_BAM_NO_EOT BIT(0)
223/* Set the NWD flag in current BAM sgl */
224#define NAND_BAM_NWD BIT(1)
225/* Finish writing in the current BAM sgl and start writing in another BAM sgl */
226#define NAND_BAM_NEXT_SGL BIT(2)
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530227/*
228 * Erased codeword status is being used two times in single transfer so this
229 * flag will determine the current value of erased codeword status register
230 */
231#define NAND_ERASED_CW_SET BIT(4)
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530232
233/*
Abhishek Sahucb80f112017-08-17 17:37:40 +0530234 * This data type corresponds to the BAM transaction which will be used for all
235 * NAND transfers.
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530236 * @bam_ce - the array of BAM command elements
Abhishek Sahucb80f112017-08-17 17:37:40 +0530237 * @cmd_sgl - sgl for NAND BAM command pipe
238 * @data_sgl - sgl for NAND BAM consumer/producer pipe
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530239 * @bam_ce_pos - the index in bam_ce which is available for next sgl
240 * @bam_ce_start - the index in bam_ce which marks the start position ce
241 * for current sgl. It will be used for size calculation
242 * for current sgl
Abhishek Sahucb80f112017-08-17 17:37:40 +0530243 * @cmd_sgl_pos - current index in command sgl.
244 * @cmd_sgl_start - start index in command sgl.
245 * @tx_sgl_pos - current index in data sgl for tx.
246 * @tx_sgl_start - start index in data sgl for tx.
247 * @rx_sgl_pos - current index in data sgl for rx.
248 * @rx_sgl_start - start index in data sgl for rx.
Abhishek Sahu6f200702018-06-20 12:57:33 +0530249 * @wait_second_completion - wait for second DMA desc completion before making
250 * the NAND transfer completion.
251 * @txn_done - completion for NAND transfer.
252 * @last_data_desc - last DMA desc in data channel (tx/rx).
253 * @last_cmd_desc - last DMA desc in command channel.
Abhishek Sahucb80f112017-08-17 17:37:40 +0530254 */
255struct bam_transaction {
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530256 struct bam_cmd_element *bam_ce;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530257 struct scatterlist *cmd_sgl;
258 struct scatterlist *data_sgl;
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530259 u32 bam_ce_pos;
260 u32 bam_ce_start;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530261 u32 cmd_sgl_pos;
262 u32 cmd_sgl_start;
263 u32 tx_sgl_pos;
264 u32 tx_sgl_start;
265 u32 rx_sgl_pos;
266 u32 rx_sgl_start;
Abhishek Sahu6f200702018-06-20 12:57:33 +0530267 bool wait_second_completion;
268 struct completion txn_done;
269 struct dma_async_tx_descriptor *last_data_desc;
270 struct dma_async_tx_descriptor *last_cmd_desc;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530271};
272
Abhishek Sahu381dd242017-08-17 17:37:41 +0530273/*
274 * This data type corresponds to the nand dma descriptor
275 * @list - list for desc_info
276 * @dir - DMA transfer direction
277 * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
278 * ADM
279 * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
280 * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
281 * @dma_desc - low level DMA engine descriptor
282 */
Archit Tanejac76b78d2016-02-03 14:29:50 +0530283struct desc_info {
284 struct list_head node;
285
286 enum dma_data_direction dir;
Abhishek Sahu381dd242017-08-17 17:37:41 +0530287 union {
288 struct scatterlist adm_sgl;
289 struct {
290 struct scatterlist *bam_sgl;
291 int sgl_cnt;
292 };
293 };
Archit Tanejac76b78d2016-02-03 14:29:50 +0530294 struct dma_async_tx_descriptor *dma_desc;
295};
296
297/*
298 * holds the current register values that we want to write. acts as a contiguous
299 * chunk of memory which we use to write the controller registers through DMA.
300 */
301struct nandc_regs {
302 __le32 cmd;
303 __le32 addr0;
304 __le32 addr1;
305 __le32 chip_sel;
306 __le32 exec;
307
308 __le32 cfg0;
309 __le32 cfg1;
310 __le32 ecc_bch_cfg;
311
312 __le32 clrflashstatus;
313 __le32 clrreadstatus;
314
315 __le32 cmd1;
316 __le32 vld;
317
318 __le32 orig_cmd1;
319 __le32 orig_vld;
320
321 __le32 ecc_buf_cfg;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530322 __le32 read_location0;
323 __le32 read_location1;
324 __le32 read_location2;
325 __le32 read_location3;
326
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530327 __le32 erased_cw_detect_cfg_clr;
328 __le32 erased_cw_detect_cfg_set;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530329};
330
331/*
332 * NAND controller data struct
333 *
334 * @controller: base controller structure
335 * @host_list: list containing all the chips attached to the
336 * controller
337 * @dev: parent device
338 * @base: MMIO base
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530339 * @base_phys: physical base address of controller registers
340 * @base_dma: dma base address of controller registers
Archit Tanejac76b78d2016-02-03 14:29:50 +0530341 * @core_clk: controller clock
342 * @aon_clk: another controller clock
343 *
344 * @chan: dma channel
345 * @cmd_crci: ADM DMA CRCI for command flow control
346 * @data_crci: ADM DMA CRCI for data flow control
347 * @desc_list: DMA descriptor list (list of desc_infos)
348 *
349 * @data_buffer: our local DMA buffer for page read/writes,
350 * used when we can't use the buffer provided
351 * by upper layers directly
352 * @buf_size/count/start: markers for chip->read_buf/write_buf functions
353 * @reg_read_buf: local buffer for reading back registers via DMA
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530354 * @reg_read_dma: contains dma address for register read buffer
Archit Tanejac76b78d2016-02-03 14:29:50 +0530355 * @reg_read_pos: marker for data read in reg_read_buf
356 *
357 * @regs: a contiguous chunk of memory for DMA register
358 * writes. contains the register values to be
359 * written to controller
360 * @cmd1/vld: some fixed controller register values
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530361 * @props: properties of current NAND controller,
Archit Tanejac76b78d2016-02-03 14:29:50 +0530362 * initialized via DT match data
Abhishek Sahucb80f112017-08-17 17:37:40 +0530363 * @max_cwperpage: maximum QPIC codewords required. calculated
364 * from all connected NAND devices pagesize
Archit Tanejac76b78d2016-02-03 14:29:50 +0530365 */
366struct qcom_nand_controller {
Miquel Raynal7da45132018-07-17 09:08:02 +0200367 struct nand_controller controller;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530368 struct list_head host_list;
369
370 struct device *dev;
371
372 void __iomem *base;
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530373 phys_addr_t base_phys;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530374 dma_addr_t base_dma;
375
376 struct clk *core_clk;
377 struct clk *aon_clk;
378
Abhishek Sahu497d7d82017-08-11 17:09:19 +0530379 union {
380 /* will be used only by QPIC for BAM DMA */
381 struct {
382 struct dma_chan *tx_chan;
383 struct dma_chan *rx_chan;
384 struct dma_chan *cmd_chan;
385 };
386
387 /* will be used only by EBI2 for ADM DMA */
388 struct {
389 struct dma_chan *chan;
390 unsigned int cmd_crci;
391 unsigned int data_crci;
392 };
393 };
394
Archit Tanejac76b78d2016-02-03 14:29:50 +0530395 struct list_head desc_list;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530396 struct bam_transaction *bam_txn;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530397
398 u8 *data_buffer;
399 int buf_size;
400 int buf_count;
401 int buf_start;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530402 unsigned int max_cwperpage;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530403
404 __le32 *reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530405 dma_addr_t reg_read_dma;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530406 int reg_read_pos;
407
408 struct nandc_regs *regs;
409
410 u32 cmd1, vld;
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530411 const struct qcom_nandc_props *props;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530412};
413
414/*
415 * NAND chip structure
416 *
417 * @chip: base NAND chip structure
418 * @node: list node to add itself to host_list in
419 * qcom_nand_controller
420 *
421 * @cs: chip select value for this chip
422 * @cw_size: the number of bytes in a single step/codeword
423 * of a page, consisting of all data, ecc, spare
424 * and reserved bytes
425 * @cw_data: the number of bytes within a codeword protected
426 * by ECC
427 * @use_ecc: request the controller to use ECC for the
428 * upcoming read/write
429 * @bch_enabled: flag to tell whether BCH ECC mode is used
430 * @ecc_bytes_hw: ECC bytes used by controller hardware for this
431 * chip
432 * @status: value to be returned if NAND_CMD_STATUS command
433 * is executed
434 * @last_command: keeps track of last command on this chip. used
435 * for reading correct status
436 *
437 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
438 * ecc/non-ecc mode for the current nand flash
439 * device
440 */
441struct qcom_nand_host {
442 struct nand_chip chip;
443 struct list_head node;
444
445 int cs;
446 int cw_size;
447 int cw_data;
448 bool use_ecc;
449 bool bch_enabled;
450 int ecc_bytes_hw;
451 int spare_bytes;
452 int bbm_size;
453 u8 status;
454 int last_command;
455
456 u32 cfg0, cfg1;
457 u32 cfg0_raw, cfg1_raw;
458 u32 ecc_buf_cfg;
459 u32 ecc_bch_cfg;
460 u32 clrflashstatus;
461 u32 clrreadstatus;
462};
463
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530464/*
465 * This data type corresponds to the NAND controller properties which varies
466 * among different NAND controllers.
467 * @ecc_modes - ecc mode for NAND
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +0530468 * @is_bam - whether NAND controller is using BAM
Abhishek Sahucc409b92017-08-17 17:37:47 +0530469 * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530470 */
471struct qcom_nandc_props {
472 u32 ecc_modes;
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +0530473 bool is_bam;
Abhishek Sahucc409b92017-08-17 17:37:47 +0530474 u32 dev_cmd_reg_start;
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530475};
476
Abhishek Sahucb80f112017-08-17 17:37:40 +0530477/* Frees the BAM transaction memory */
478static void free_bam_transaction(struct qcom_nand_controller *nandc)
479{
480 struct bam_transaction *bam_txn = nandc->bam_txn;
481
482 devm_kfree(nandc->dev, bam_txn);
483}
484
485/* Allocates and Initializes the BAM transaction */
486static struct bam_transaction *
487alloc_bam_transaction(struct qcom_nand_controller *nandc)
488{
489 struct bam_transaction *bam_txn;
490 size_t bam_txn_size;
491 unsigned int num_cw = nandc->max_cwperpage;
492 void *bam_txn_buf;
493
494 bam_txn_size =
495 sizeof(*bam_txn) + num_cw *
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530496 ((sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS) +
497 (sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) +
Abhishek Sahucb80f112017-08-17 17:37:40 +0530498 (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL));
499
500 bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL);
501 if (!bam_txn_buf)
502 return NULL;
503
504 bam_txn = bam_txn_buf;
505 bam_txn_buf += sizeof(*bam_txn);
506
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530507 bam_txn->bam_ce = bam_txn_buf;
508 bam_txn_buf +=
509 sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS * num_cw;
510
Abhishek Sahucb80f112017-08-17 17:37:40 +0530511 bam_txn->cmd_sgl = bam_txn_buf;
512 bam_txn_buf +=
513 sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw;
514
515 bam_txn->data_sgl = bam_txn_buf;
516
Abhishek Sahu6f200702018-06-20 12:57:33 +0530517 init_completion(&bam_txn->txn_done);
518
Abhishek Sahucb80f112017-08-17 17:37:40 +0530519 return bam_txn;
520}
521
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530522/* Clears the BAM transaction indexes */
523static void clear_bam_transaction(struct qcom_nand_controller *nandc)
524{
525 struct bam_transaction *bam_txn = nandc->bam_txn;
526
527 if (!nandc->props->is_bam)
528 return;
529
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530530 bam_txn->bam_ce_pos = 0;
531 bam_txn->bam_ce_start = 0;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530532 bam_txn->cmd_sgl_pos = 0;
533 bam_txn->cmd_sgl_start = 0;
534 bam_txn->tx_sgl_pos = 0;
535 bam_txn->tx_sgl_start = 0;
536 bam_txn->rx_sgl_pos = 0;
537 bam_txn->rx_sgl_start = 0;
Abhishek Sahu6f200702018-06-20 12:57:33 +0530538 bam_txn->last_data_desc = NULL;
539 bam_txn->wait_second_completion = false;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530540
541 sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage *
542 QPIC_PER_CW_CMD_SGL);
543 sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage *
544 QPIC_PER_CW_DATA_SGL);
Abhishek Sahu6f200702018-06-20 12:57:33 +0530545
546 reinit_completion(&bam_txn->txn_done);
547}
548
549/* Callback for DMA descriptor completion */
550static void qpic_bam_dma_done(void *data)
551{
552 struct bam_transaction *bam_txn = data;
553
554 /*
555 * In case of data transfer with NAND, 2 callbacks will be generated.
556 * One for command channel and another one for data channel.
557 * If current transaction has data descriptors
558 * (i.e. wait_second_completion is true), then set this to false
559 * and wait for second DMA descriptor completion.
560 */
561 if (bam_txn->wait_second_completion)
562 bam_txn->wait_second_completion = false;
563 else
564 complete(&bam_txn->txn_done);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530565}
566
Archit Tanejac76b78d2016-02-03 14:29:50 +0530567static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
568{
569 return container_of(chip, struct qcom_nand_host, chip);
570}
571
572static inline struct qcom_nand_controller *
573get_qcom_nand_controller(struct nand_chip *chip)
574{
575 return container_of(chip->controller, struct qcom_nand_controller,
576 controller);
577}
578
579static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
580{
581 return ioread32(nandc->base + offset);
582}
583
584static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
585 u32 val)
586{
587 iowrite32(val, nandc->base + offset);
588}
589
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530590static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
591 bool is_cpu)
592{
593 if (!nandc->props->is_bam)
594 return;
595
596 if (is_cpu)
597 dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
598 MAX_REG_RD *
599 sizeof(*nandc->reg_read_buf),
600 DMA_FROM_DEVICE);
601 else
602 dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
603 MAX_REG_RD *
604 sizeof(*nandc->reg_read_buf),
605 DMA_FROM_DEVICE);
606}
607
Archit Tanejac76b78d2016-02-03 14:29:50 +0530608static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
609{
610 switch (offset) {
611 case NAND_FLASH_CMD:
612 return &regs->cmd;
613 case NAND_ADDR0:
614 return &regs->addr0;
615 case NAND_ADDR1:
616 return &regs->addr1;
617 case NAND_FLASH_CHIP_SELECT:
618 return &regs->chip_sel;
619 case NAND_EXEC_CMD:
620 return &regs->exec;
621 case NAND_FLASH_STATUS:
622 return &regs->clrflashstatus;
623 case NAND_DEV0_CFG0:
624 return &regs->cfg0;
625 case NAND_DEV0_CFG1:
626 return &regs->cfg1;
627 case NAND_DEV0_ECC_CFG:
628 return &regs->ecc_bch_cfg;
629 case NAND_READ_STATUS:
630 return &regs->clrreadstatus;
631 case NAND_DEV_CMD1:
632 return &regs->cmd1;
633 case NAND_DEV_CMD1_RESTORE:
634 return &regs->orig_cmd1;
635 case NAND_DEV_CMD_VLD:
636 return &regs->vld;
637 case NAND_DEV_CMD_VLD_RESTORE:
638 return &regs->orig_vld;
639 case NAND_EBI2_ECC_BUF_CFG:
640 return &regs->ecc_buf_cfg;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530641 case NAND_READ_LOCATION_0:
642 return &regs->read_location0;
643 case NAND_READ_LOCATION_1:
644 return &regs->read_location1;
645 case NAND_READ_LOCATION_2:
646 return &regs->read_location2;
647 case NAND_READ_LOCATION_3:
648 return &regs->read_location3;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530649 default:
650 return NULL;
651 }
652}
653
654static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
655 u32 val)
656{
657 struct nandc_regs *regs = nandc->regs;
658 __le32 *reg;
659
660 reg = offset_to_nandc_reg(regs, offset);
661
662 if (reg)
663 *reg = cpu_to_le32(val);
664}
665
666/* helper to configure address register values */
667static void set_address(struct qcom_nand_host *host, u16 column, int page)
668{
669 struct nand_chip *chip = &host->chip;
670 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
671
672 if (chip->options & NAND_BUSWIDTH_16)
673 column >>= 1;
674
675 nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
676 nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
677}
678
679/*
680 * update_rw_regs: set up read/write register values, these will be
681 * written to the NAND controller registers via DMA
682 *
683 * @num_cw: number of steps for the read/write operation
684 * @read: read or write operation
685 */
686static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
687{
688 struct nand_chip *chip = &host->chip;
689 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
690 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
691
692 if (read) {
693 if (host->use_ecc)
694 cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
695 else
696 cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
697 } else {
698 cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
699 }
700
701 if (host->use_ecc) {
702 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
703 (num_cw - 1) << CW_PER_PAGE;
704
705 cfg1 = host->cfg1;
706 ecc_bch_cfg = host->ecc_bch_cfg;
707 } else {
708 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
709 (num_cw - 1) << CW_PER_PAGE;
710
711 cfg1 = host->cfg1_raw;
712 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
713 }
714
715 nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
716 nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
717 nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
718 nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
719 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
720 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
721 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
722 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530723
724 if (read)
725 nandc_set_read_loc(nandc, 0, 0, host->use_ecc ?
726 host->cw_data : host->cw_size, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530727}
728
Abhishek Sahu381dd242017-08-17 17:37:41 +0530729/*
730 * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
731 * for BAM. This descriptor will be added in the NAND DMA descriptor queue
732 * which will be submitted to DMA engine.
733 */
734static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
735 struct dma_chan *chan,
736 unsigned long flags)
737{
738 struct desc_info *desc;
739 struct scatterlist *sgl;
740 unsigned int sgl_cnt;
741 int ret;
742 struct bam_transaction *bam_txn = nandc->bam_txn;
743 enum dma_transfer_direction dir_eng;
744 struct dma_async_tx_descriptor *dma_desc;
745
746 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
747 if (!desc)
748 return -ENOMEM;
749
750 if (chan == nandc->cmd_chan) {
751 sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
752 sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
753 bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
754 dir_eng = DMA_MEM_TO_DEV;
755 desc->dir = DMA_TO_DEVICE;
756 } else if (chan == nandc->tx_chan) {
757 sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
758 sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
759 bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
760 dir_eng = DMA_MEM_TO_DEV;
761 desc->dir = DMA_TO_DEVICE;
762 } else {
763 sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
764 sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
765 bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
766 dir_eng = DMA_DEV_TO_MEM;
767 desc->dir = DMA_FROM_DEVICE;
768 }
769
770 sg_mark_end(sgl + sgl_cnt - 1);
771 ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
772 if (ret == 0) {
773 dev_err(nandc->dev, "failure in mapping desc\n");
774 kfree(desc);
775 return -ENOMEM;
776 }
777
778 desc->sgl_cnt = sgl_cnt;
779 desc->bam_sgl = sgl;
780
781 dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
782 flags);
783
784 if (!dma_desc) {
785 dev_err(nandc->dev, "failure in prep desc\n");
786 dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
787 kfree(desc);
788 return -EINVAL;
789 }
790
791 desc->dma_desc = dma_desc;
792
Abhishek Sahu6f200702018-06-20 12:57:33 +0530793 /* update last data/command descriptor */
794 if (chan == nandc->cmd_chan)
795 bam_txn->last_cmd_desc = dma_desc;
796 else
797 bam_txn->last_data_desc = dma_desc;
798
Abhishek Sahu381dd242017-08-17 17:37:41 +0530799 list_add_tail(&desc->node, &nandc->desc_list);
800
801 return 0;
802}
803
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530804/*
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530805 * Prepares the command descriptor for BAM DMA which will be used for NAND
806 * register reads and writes. The command descriptor requires the command
807 * to be formed in command element type so this function uses the command
808 * element from bam transaction ce array and fills the same with required
809 * data. A single SGL can contain multiple command elements so
810 * NAND_BAM_NEXT_SGL will be used for starting the separate SGL
811 * after the current command element.
812 */
813static int prep_bam_dma_desc_cmd(struct qcom_nand_controller *nandc, bool read,
814 int reg_off, const void *vaddr,
815 int size, unsigned int flags)
816{
817 int bam_ce_size;
818 int i, ret;
819 struct bam_cmd_element *bam_ce_buffer;
820 struct bam_transaction *bam_txn = nandc->bam_txn;
821
822 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_pos];
823
824 /* fill the command desc */
825 for (i = 0; i < size; i++) {
826 if (read)
827 bam_prep_ce(&bam_ce_buffer[i],
828 nandc_reg_phys(nandc, reg_off + 4 * i),
829 BAM_READ_COMMAND,
830 reg_buf_dma_addr(nandc,
831 (__le32 *)vaddr + i));
832 else
833 bam_prep_ce_le32(&bam_ce_buffer[i],
834 nandc_reg_phys(nandc, reg_off + 4 * i),
835 BAM_WRITE_COMMAND,
836 *((__le32 *)vaddr + i));
837 }
838
839 bam_txn->bam_ce_pos += size;
840
841 /* use the separate sgl after this command */
842 if (flags & NAND_BAM_NEXT_SGL) {
843 bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_start];
844 bam_ce_size = (bam_txn->bam_ce_pos -
845 bam_txn->bam_ce_start) *
846 sizeof(struct bam_cmd_element);
847 sg_set_buf(&bam_txn->cmd_sgl[bam_txn->cmd_sgl_pos],
848 bam_ce_buffer, bam_ce_size);
849 bam_txn->cmd_sgl_pos++;
850 bam_txn->bam_ce_start = bam_txn->bam_ce_pos;
851
852 if (flags & NAND_BAM_NWD) {
853 ret = prepare_bam_async_desc(nandc, nandc->cmd_chan,
854 DMA_PREP_FENCE |
855 DMA_PREP_CMD);
856 if (ret)
857 return ret;
858 }
859 }
860
861 return 0;
862}
863
864/*
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530865 * Prepares the data descriptor for BAM DMA which will be used for NAND
866 * data reads and writes.
867 */
868static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read,
869 const void *vaddr,
870 int size, unsigned int flags)
871{
872 int ret;
873 struct bam_transaction *bam_txn = nandc->bam_txn;
874
875 if (read) {
876 sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos],
877 vaddr, size);
878 bam_txn->rx_sgl_pos++;
879 } else {
880 sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos],
881 vaddr, size);
882 bam_txn->tx_sgl_pos++;
883
884 /*
885 * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
886 * is not set, form the DMA descriptor
887 */
888 if (!(flags & NAND_BAM_NO_EOT)) {
889 ret = prepare_bam_async_desc(nandc, nandc->tx_chan,
890 DMA_PREP_INTERRUPT);
891 if (ret)
892 return ret;
893 }
894 }
895
896 return 0;
897}
898
Abhishek Sahu381dd242017-08-17 17:37:41 +0530899static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
900 int reg_off, const void *vaddr, int size,
901 bool flow_control)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530902{
903 struct desc_info *desc;
904 struct dma_async_tx_descriptor *dma_desc;
905 struct scatterlist *sgl;
906 struct dma_slave_config slave_conf;
907 enum dma_transfer_direction dir_eng;
908 int ret;
909
910 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
911 if (!desc)
912 return -ENOMEM;
913
Abhishek Sahu381dd242017-08-17 17:37:41 +0530914 sgl = &desc->adm_sgl;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530915
916 sg_init_one(sgl, vaddr, size);
917
918 if (read) {
919 dir_eng = DMA_DEV_TO_MEM;
920 desc->dir = DMA_FROM_DEVICE;
921 } else {
922 dir_eng = DMA_MEM_TO_DEV;
923 desc->dir = DMA_TO_DEVICE;
924 }
925
926 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
927 if (ret == 0) {
928 ret = -ENOMEM;
929 goto err;
930 }
931
932 memset(&slave_conf, 0x00, sizeof(slave_conf));
933
934 slave_conf.device_fc = flow_control;
935 if (read) {
936 slave_conf.src_maxburst = 16;
937 slave_conf.src_addr = nandc->base_dma + reg_off;
938 slave_conf.slave_id = nandc->data_crci;
939 } else {
940 slave_conf.dst_maxburst = 16;
941 slave_conf.dst_addr = nandc->base_dma + reg_off;
942 slave_conf.slave_id = nandc->cmd_crci;
943 }
944
945 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
946 if (ret) {
947 dev_err(nandc->dev, "failed to configure dma channel\n");
948 goto err;
949 }
950
951 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
952 if (!dma_desc) {
953 dev_err(nandc->dev, "failed to prepare desc\n");
954 ret = -EINVAL;
955 goto err;
956 }
957
958 desc->dma_desc = dma_desc;
959
960 list_add_tail(&desc->node, &nandc->desc_list);
961
962 return 0;
963err:
964 kfree(desc);
965
966 return ret;
967}
968
969/*
970 * read_reg_dma: prepares a descriptor to read a given number of
971 * contiguous registers to the reg_read_buf pointer
972 *
973 * @first: offset of the first register in the contiguous block
974 * @num_regs: number of registers to read
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530975 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530976 */
977static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530978 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530979{
980 bool flow_control = false;
981 void *vaddr;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530982
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530983 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
984 nandc->reg_read_pos += num_regs;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530985
Abhishek Sahucc409b92017-08-17 17:37:47 +0530986 if (first == NAND_DEV_CMD_VLD || first == NAND_DEV_CMD1)
987 first = dev_cmd_reg_addr(nandc, first);
988
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530989 if (nandc->props->is_bam)
990 return prep_bam_dma_desc_cmd(nandc, true, first, vaddr,
991 num_regs, flags);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530992
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +0530993 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
994 flow_control = true;
995
996 return prep_adm_dma_desc(nandc, true, first, vaddr,
997 num_regs * sizeof(u32), flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530998}
999
1000/*
1001 * write_reg_dma: prepares a descriptor to write a given number of
1002 * contiguous registers
1003 *
1004 * @first: offset of the first register in the contiguous block
1005 * @num_regs: number of registers to write
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301006 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +05301007 */
1008static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301009 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301010{
1011 bool flow_control = false;
1012 struct nandc_regs *regs = nandc->regs;
1013 void *vaddr;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301014
1015 vaddr = offset_to_nandc_reg(regs, first);
1016
Abhishek Sahua86b9c42017-08-17 17:37:44 +05301017 if (first == NAND_ERASED_CW_DETECT_CFG) {
1018 if (flags & NAND_ERASED_CW_SET)
1019 vaddr = &regs->erased_cw_detect_cfg_set;
1020 else
1021 vaddr = &regs->erased_cw_detect_cfg_clr;
1022 }
1023
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301024 if (first == NAND_EXEC_CMD)
1025 flags |= NAND_BAM_NWD;
1026
Abhishek Sahucc409b92017-08-17 17:37:47 +05301027 if (first == NAND_DEV_CMD1_RESTORE || first == NAND_DEV_CMD1)
1028 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301029
Abhishek Sahucc409b92017-08-17 17:37:47 +05301030 if (first == NAND_DEV_CMD_VLD_RESTORE || first == NAND_DEV_CMD_VLD)
1031 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301032
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301033 if (nandc->props->is_bam)
1034 return prep_bam_dma_desc_cmd(nandc, false, first, vaddr,
1035 num_regs, flags);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301036
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301037 if (first == NAND_FLASH_CMD)
1038 flow_control = true;
1039
1040 return prep_adm_dma_desc(nandc, false, first, vaddr,
1041 num_regs * sizeof(u32), flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301042}
1043
1044/*
1045 * read_data_dma: prepares a DMA descriptor to transfer data from the
1046 * controller's internal buffer to the buffer 'vaddr'
1047 *
1048 * @reg_off: offset within the controller's data buffer
1049 * @vaddr: virtual address of the buffer we want to write to
1050 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301051 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +05301052 */
1053static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301054 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301055{
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301056 if (nandc->props->is_bam)
1057 return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags);
1058
Abhishek Sahu381dd242017-08-17 17:37:41 +05301059 return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301060}
1061
1062/*
1063 * write_data_dma: prepares a DMA descriptor to transfer data from
1064 * 'vaddr' to the controller's internal buffer
1065 *
1066 * @reg_off: offset within the controller's data buffer
1067 * @vaddr: virtual address of the buffer we want to read from
1068 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301069 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +05301070 */
1071static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301072 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301073{
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301074 if (nandc->props->is_bam)
1075 return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags);
1076
Abhishek Sahu381dd242017-08-17 17:37:41 +05301077 return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301078}
1079
1080/*
Abhishek Sahubde43302017-07-19 17:17:55 +05301081 * Helper to prepare DMA descriptors for configuring registers
1082 * before reading a NAND page.
Archit Tanejac76b78d2016-02-03 14:29:50 +05301083 */
Abhishek Sahubde43302017-07-19 17:17:55 +05301084static void config_nand_page_read(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301085{
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301086 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1087 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1088 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
Abhishek Sahua86b9c42017-08-17 17:37:44 +05301089 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
1090 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
1091 NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
Abhishek Sahubde43302017-07-19 17:17:55 +05301092}
Archit Tanejac76b78d2016-02-03 14:29:50 +05301093
Abhishek Sahubde43302017-07-19 17:17:55 +05301094/*
1095 * Helper to prepare DMA descriptors for configuring registers
1096 * before reading each codeword in NAND page.
1097 */
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301098static void
1099config_nand_cw_read(struct qcom_nand_controller *nandc, bool use_ecc)
Abhishek Sahubde43302017-07-19 17:17:55 +05301100{
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 Sahu5bc36b22018-06-20 12:57:39 +05301108 if (use_ecc) {
1109 read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
1110 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
1111 NAND_BAM_NEXT_SGL);
1112 } else {
1113 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1114 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301115}
1116
1117/*
Abhishek Sahubde43302017-07-19 17:17:55 +05301118 * Helper to prepare dma descriptors to configure registers needed for reading a
1119 * single codeword in page
Archit Tanejac76b78d2016-02-03 14:29:50 +05301120 */
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301121static void
1122config_nand_single_cw_page_read(struct qcom_nand_controller *nandc,
1123 bool use_ecc)
Abhishek Sahubde43302017-07-19 17:17:55 +05301124{
1125 config_nand_page_read(nandc);
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301126 config_nand_cw_read(nandc, use_ecc);
Abhishek Sahubde43302017-07-19 17:17:55 +05301127}
1128
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301129/*
1130 * Helper to prepare DMA descriptors used to configure registers needed for
1131 * before writing a NAND page.
1132 */
1133static void config_nand_page_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301134{
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301135 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1136 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1137 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
1138 NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301139}
1140
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301141/*
1142 * Helper to prepare DMA descriptors for configuring registers
1143 * before writing each codeword in NAND page.
1144 */
1145static void config_nand_cw_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301146{
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301147 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1148 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301149
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301150 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301151
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301152 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1153 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301154}
1155
1156/*
1157 * the following functions are used within chip->cmdfunc() to perform different
1158 * NAND_CMD_* commands
1159 */
1160
1161/* sets up descriptors for NAND_CMD_PARAM */
1162static int nandc_param(struct qcom_nand_host *host)
1163{
1164 struct nand_chip *chip = &host->chip;
1165 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1166
1167 /*
1168 * NAND_CMD_PARAM is called before we know much about the FLASH chip
1169 * in use. we configure the controller to perform a raw read of 512
1170 * bytes to read onfi params
1171 */
1172 nandc_set_reg(nandc, NAND_FLASH_CMD, PAGE_READ | PAGE_ACC | LAST_PAGE);
1173 nandc_set_reg(nandc, NAND_ADDR0, 0);
1174 nandc_set_reg(nandc, NAND_ADDR1, 0);
1175 nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
1176 | 512 << UD_SIZE_BYTES
1177 | 5 << NUM_ADDR_CYCLES
1178 | 0 << SPARE_SIZE_BYTES);
1179 nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
1180 | 0 << CS_ACTIVE_BSY
1181 | 17 << BAD_BLOCK_BYTE_NUM
1182 | 1 << BAD_BLOCK_IN_SPARE_AREA
1183 | 2 << WR_RD_BSY_GAP
1184 | 0 << WIDE_FLASH
1185 | 1 << DEV0_CFG1_ECC_DISABLE);
1186 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
1187
1188 /* configure CMD1 and VLD for ONFI param probing */
1189 nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
Abhishek Sahud8a9b322017-08-11 17:09:16 +05301190 (nandc->vld & ~READ_START_VLD));
Archit Tanejac76b78d2016-02-03 14:29:50 +05301191 nandc_set_reg(nandc, NAND_DEV_CMD1,
1192 (nandc->cmd1 & ~(0xFF << READ_ADDR))
1193 | NAND_CMD_PARAM << READ_ADDR);
1194
1195 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1196
1197 nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
1198 nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301199 nandc_set_read_loc(nandc, 0, 0, 512, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301200
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301201 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
1202 write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301203
1204 nandc->buf_count = 512;
1205 memset(nandc->data_buffer, 0xff, nandc->buf_count);
1206
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301207 config_nand_single_cw_page_read(nandc, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301208
1209 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301210 nandc->buf_count, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301211
1212 /* restore CMD1 and VLD regs */
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301213 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
1214 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301215
1216 return 0;
1217}
1218
1219/* sets up descriptors for NAND_CMD_ERASE1 */
1220static int erase_block(struct qcom_nand_host *host, int page_addr)
1221{
1222 struct nand_chip *chip = &host->chip;
1223 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1224
1225 nandc_set_reg(nandc, NAND_FLASH_CMD,
1226 BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
1227 nandc_set_reg(nandc, NAND_ADDR0, page_addr);
1228 nandc_set_reg(nandc, NAND_ADDR1, 0);
1229 nandc_set_reg(nandc, NAND_DEV0_CFG0,
1230 host->cfg0_raw & ~(7 << CW_PER_PAGE));
1231 nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
1232 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1233 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
1234 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
1235
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301236 write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
1237 write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
1238 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301239
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301240 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301241
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301242 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1243 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301244
1245 return 0;
1246}
1247
1248/* sets up descriptors for NAND_CMD_READID */
1249static int read_id(struct qcom_nand_host *host, int column)
1250{
1251 struct nand_chip *chip = &host->chip;
1252 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1253
1254 if (column == -1)
1255 return 0;
1256
1257 nandc_set_reg(nandc, NAND_FLASH_CMD, FETCH_ID);
1258 nandc_set_reg(nandc, NAND_ADDR0, column);
1259 nandc_set_reg(nandc, NAND_ADDR1, 0);
Abhishek Sahu9d43f912017-08-17 17:37:45 +05301260 nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT,
1261 nandc->props->is_bam ? 0 : DM_EN);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301262 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1263
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301264 write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
1265 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301266
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301267 read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301268
1269 return 0;
1270}
1271
1272/* sets up descriptors for NAND_CMD_RESET */
1273static int reset(struct qcom_nand_host *host)
1274{
1275 struct nand_chip *chip = &host->chip;
1276 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1277
1278 nandc_set_reg(nandc, NAND_FLASH_CMD, RESET_DEVICE);
1279 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1280
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301281 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1282 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301283
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301284 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301285
1286 return 0;
1287}
1288
1289/* helpers to submit/free our list of dma descriptors */
1290static int submit_descs(struct qcom_nand_controller *nandc)
1291{
1292 struct desc_info *desc;
1293 dma_cookie_t cookie = 0;
Abhishek Sahu381dd242017-08-17 17:37:41 +05301294 struct bam_transaction *bam_txn = nandc->bam_txn;
1295 int r;
1296
1297 if (nandc->props->is_bam) {
1298 if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
1299 r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
1300 if (r)
1301 return r;
1302 }
1303
1304 if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
1305 r = prepare_bam_async_desc(nandc, nandc->tx_chan,
1306 DMA_PREP_INTERRUPT);
1307 if (r)
1308 return r;
1309 }
1310
1311 if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
Abhishek Sahu8d6b6d72017-09-25 13:21:26 +05301312 r = prepare_bam_async_desc(nandc, nandc->cmd_chan,
1313 DMA_PREP_CMD);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301314 if (r)
1315 return r;
1316 }
1317 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301318
1319 list_for_each_entry(desc, &nandc->desc_list, node)
1320 cookie = dmaengine_submit(desc->dma_desc);
1321
Abhishek Sahu381dd242017-08-17 17:37:41 +05301322 if (nandc->props->is_bam) {
Abhishek Sahu6f200702018-06-20 12:57:33 +05301323 bam_txn->last_cmd_desc->callback = qpic_bam_dma_done;
1324 bam_txn->last_cmd_desc->callback_param = bam_txn;
1325 if (bam_txn->last_data_desc) {
1326 bam_txn->last_data_desc->callback = qpic_bam_dma_done;
1327 bam_txn->last_data_desc->callback_param = bam_txn;
1328 bam_txn->wait_second_completion = true;
1329 }
1330
Abhishek Sahu381dd242017-08-17 17:37:41 +05301331 dma_async_issue_pending(nandc->tx_chan);
1332 dma_async_issue_pending(nandc->rx_chan);
Abhishek Sahu6f200702018-06-20 12:57:33 +05301333 dma_async_issue_pending(nandc->cmd_chan);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301334
Abhishek Sahu6f200702018-06-20 12:57:33 +05301335 if (!wait_for_completion_timeout(&bam_txn->txn_done,
1336 QPIC_NAND_COMPLETION_TIMEOUT))
Abhishek Sahu381dd242017-08-17 17:37:41 +05301337 return -ETIMEDOUT;
1338 } else {
1339 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
1340 return -ETIMEDOUT;
1341 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301342
1343 return 0;
1344}
1345
1346static void free_descs(struct qcom_nand_controller *nandc)
1347{
1348 struct desc_info *desc, *n;
1349
1350 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
1351 list_del(&desc->node);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301352
1353 if (nandc->props->is_bam)
1354 dma_unmap_sg(nandc->dev, desc->bam_sgl,
1355 desc->sgl_cnt, desc->dir);
1356 else
1357 dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
1358 desc->dir);
1359
Archit Tanejac76b78d2016-02-03 14:29:50 +05301360 kfree(desc);
1361 }
1362}
1363
1364/* reset the register read buffer for next NAND operation */
1365static void clear_read_regs(struct qcom_nand_controller *nandc)
1366{
1367 nandc->reg_read_pos = 0;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301368 nandc_read_buffer_sync(nandc, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301369}
1370
1371static void pre_command(struct qcom_nand_host *host, int command)
1372{
1373 struct nand_chip *chip = &host->chip;
1374 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1375
1376 nandc->buf_count = 0;
1377 nandc->buf_start = 0;
1378 host->use_ecc = false;
1379 host->last_command = command;
1380
1381 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301382
1383 if (command == NAND_CMD_RESET || command == NAND_CMD_READID ||
1384 command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1)
1385 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301386}
1387
1388/*
1389 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
1390 * privately maintained status byte, this status byte can be read after
1391 * NAND_CMD_STATUS is called
1392 */
1393static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
1394{
1395 struct nand_chip *chip = &host->chip;
1396 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1397 struct nand_ecc_ctrl *ecc = &chip->ecc;
1398 int num_cw;
1399 int i;
1400
1401 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301402 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301403
1404 for (i = 0; i < num_cw; i++) {
1405 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
1406
1407 if (flash_status & FS_MPU_ERR)
1408 host->status &= ~NAND_STATUS_WP;
1409
1410 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
1411 (flash_status &
1412 FS_DEVICE_STS_ERR)))
1413 host->status |= NAND_STATUS_FAIL;
1414 }
1415}
1416
1417static void post_command(struct qcom_nand_host *host, int command)
1418{
1419 struct nand_chip *chip = &host->chip;
1420 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1421
1422 switch (command) {
1423 case NAND_CMD_READID:
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301424 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301425 memcpy(nandc->data_buffer, nandc->reg_read_buf,
1426 nandc->buf_count);
1427 break;
1428 case NAND_CMD_PAGEPROG:
1429 case NAND_CMD_ERASE1:
1430 parse_erase_write_errors(host, command);
1431 break;
1432 default:
1433 break;
1434 }
1435}
1436
1437/*
1438 * Implements chip->cmdfunc. It's only used for a limited set of commands.
1439 * The rest of the commands wouldn't be called by upper layers. For example,
1440 * NAND_CMD_READOOB would never be called because we have our own versions
1441 * of read_oob ops for nand_ecc_ctrl.
1442 */
1443static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
1444 int column, int page_addr)
1445{
1446 struct nand_chip *chip = mtd_to_nand(mtd);
1447 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1448 struct nand_ecc_ctrl *ecc = &chip->ecc;
1449 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1450 bool wait = false;
1451 int ret = 0;
1452
1453 pre_command(host, command);
1454
1455 switch (command) {
1456 case NAND_CMD_RESET:
1457 ret = reset(host);
1458 wait = true;
1459 break;
1460
1461 case NAND_CMD_READID:
1462 nandc->buf_count = 4;
1463 ret = read_id(host, column);
1464 wait = true;
1465 break;
1466
1467 case NAND_CMD_PARAM:
1468 ret = nandc_param(host);
1469 wait = true;
1470 break;
1471
1472 case NAND_CMD_ERASE1:
1473 ret = erase_block(host, page_addr);
1474 wait = true;
1475 break;
1476
1477 case NAND_CMD_READ0:
1478 /* we read the entire page for now */
1479 WARN_ON(column != 0);
1480
1481 host->use_ecc = true;
1482 set_address(host, 0, page_addr);
1483 update_rw_regs(host, ecc->steps, true);
1484 break;
1485
1486 case NAND_CMD_SEQIN:
1487 WARN_ON(column != 0);
1488 set_address(host, 0, page_addr);
1489 break;
1490
1491 case NAND_CMD_PAGEPROG:
1492 case NAND_CMD_STATUS:
1493 case NAND_CMD_NONE:
1494 default:
1495 break;
1496 }
1497
1498 if (ret) {
1499 dev_err(nandc->dev, "failure executing command %d\n",
1500 command);
1501 free_descs(nandc);
1502 return;
1503 }
1504
1505 if (wait) {
1506 ret = submit_descs(nandc);
1507 if (ret)
1508 dev_err(nandc->dev,
1509 "failure submitting descs for command %d\n",
1510 command);
1511 }
1512
1513 free_descs(nandc);
1514
1515 post_command(host, command);
1516}
1517
1518/*
1519 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1520 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1521 *
1522 * when using RS ECC, the HW reports the same erros when reading an erased CW,
1523 * but it notifies that it is an erased CW by placing special characters at
1524 * certain offsets in the buffer.
1525 *
1526 * verify if the page is erased or not, and fix up the page for RS ECC by
1527 * replacing the special characters with 0xff.
1528 */
1529static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
1530{
1531 u8 empty1, empty2;
1532
1533 /*
1534 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1535 * is erased by looking for 0x54s at offsets 3 and 175 from the
1536 * beginning of each codeword
1537 */
1538
1539 empty1 = data_buf[3];
1540 empty2 = data_buf[175];
1541
1542 /*
1543 * if the erased codework markers, if they exist override them with
1544 * 0xffs
1545 */
1546 if ((empty1 == 0x54 && empty2 == 0xff) ||
1547 (empty1 == 0xff && empty2 == 0x54)) {
1548 data_buf[3] = 0xff;
1549 data_buf[175] = 0xff;
1550 }
1551
1552 /*
1553 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1554 * restore the original values at the special offsets
1555 */
1556 if (memchr_inv(data_buf, 0xff, data_len)) {
1557 data_buf[3] = empty1;
1558 data_buf[175] = empty2;
1559
1560 return false;
1561 }
1562
1563 return true;
1564}
1565
1566struct read_stats {
1567 __le32 flash;
1568 __le32 buffer;
1569 __le32 erased_cw;
1570};
1571
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301572/* reads back FLASH_STATUS register set by the controller */
1573static int check_flash_errors(struct qcom_nand_host *host, int cw_cnt)
1574{
1575 struct nand_chip *chip = &host->chip;
1576 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1577 int i;
1578
1579 for (i = 0; i < cw_cnt; i++) {
1580 u32 flash = le32_to_cpu(nandc->reg_read_buf[i]);
1581
1582 if (flash & (FS_OP_ERR | FS_MPU_ERR))
1583 return -EIO;
1584 }
1585
1586 return 0;
1587}
1588
Abhishek Sahu85632c12018-06-20 12:57:40 +05301589/* performs raw read for one codeword */
1590static int
1591qcom_nandc_read_cw_raw(struct mtd_info *mtd, struct nand_chip *chip,
1592 u8 *data_buf, u8 *oob_buf, int page, int cw)
1593{
1594 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1595 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1596 struct nand_ecc_ctrl *ecc = &chip->ecc;
1597 int data_size1, data_size2, oob_size1, oob_size2;
1598 int ret, reg_off = FLASH_BUF_ACC, read_loc = 0;
1599
1600 nand_read_page_op(chip, page, 0, NULL, 0);
1601 host->use_ecc = false;
1602
1603 clear_bam_transaction(nandc);
1604 set_address(host, host->cw_size * cw, page);
1605 update_rw_regs(host, 1, true);
1606 config_nand_page_read(nandc);
1607
1608 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1609 oob_size1 = host->bbm_size;
1610
1611 if (cw == (ecc->steps - 1)) {
1612 data_size2 = ecc->size - data_size1 -
1613 ((ecc->steps - 1) * 4);
1614 oob_size2 = (ecc->steps * 4) + host->ecc_bytes_hw +
1615 host->spare_bytes;
1616 } else {
1617 data_size2 = host->cw_data - data_size1;
1618 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1619 }
1620
1621 if (nandc->props->is_bam) {
1622 nandc_set_read_loc(nandc, 0, read_loc, data_size1, 0);
1623 read_loc += data_size1;
1624
1625 nandc_set_read_loc(nandc, 1, read_loc, oob_size1, 0);
1626 read_loc += oob_size1;
1627
1628 nandc_set_read_loc(nandc, 2, read_loc, data_size2, 0);
1629 read_loc += data_size2;
1630
1631 nandc_set_read_loc(nandc, 3, read_loc, oob_size2, 1);
1632 }
1633
1634 config_nand_cw_read(nandc, false);
1635
1636 read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
1637 reg_off += data_size1;
1638
1639 read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
1640 reg_off += oob_size1;
1641
1642 read_data_dma(nandc, reg_off, data_buf + data_size1, data_size2, 0);
1643 reg_off += data_size2;
1644
1645 read_data_dma(nandc, reg_off, oob_buf + oob_size1, oob_size2, 0);
1646
1647 ret = submit_descs(nandc);
1648 free_descs(nandc);
1649 if (ret) {
1650 dev_err(nandc->dev, "failure to read raw cw %d\n", cw);
1651 return ret;
1652 }
1653
1654 return check_flash_errors(host, 1);
1655}
1656
Archit Tanejac76b78d2016-02-03 14:29:50 +05301657/*
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301658 * Bitflips can happen in erased codewords also so this function counts the
1659 * number of 0 in each CW for which ECC engine returns the uncorrectable
1660 * error. The page will be assumed as erased if this count is less than or
1661 * equal to the ecc->strength for each CW.
1662 *
1663 * 1. Both DATA and OOB need to be checked for number of 0. The
1664 * top-level API can be called with only data buf or OOB buf so use
1665 * chip->data_buf if data buf is null and chip->oob_poi if oob buf
1666 * is null for copying the raw bytes.
1667 * 2. Perform raw read for all the CW which has uncorrectable errors.
1668 * 3. For each CW, check the number of 0 in cw_data and usable OOB bytes.
1669 * The BBM and spare bytes bit flip won’t affect the ECC so don’t check
1670 * the number of bitflips in this area.
1671 */
1672static int
1673check_for_erased_page(struct qcom_nand_host *host, u8 *data_buf,
1674 u8 *oob_buf, unsigned long uncorrectable_cws,
1675 int page, unsigned int max_bitflips)
1676{
1677 struct nand_chip *chip = &host->chip;
1678 struct mtd_info *mtd = nand_to_mtd(chip);
1679 struct nand_ecc_ctrl *ecc = &chip->ecc;
1680 u8 *cw_data_buf, *cw_oob_buf;
1681 int cw, data_size, oob_size, ret = 0;
1682
1683 if (!data_buf) {
1684 data_buf = chip->data_buf;
1685 chip->pagebuf = -1;
1686 }
1687
1688 if (!oob_buf) {
1689 oob_buf = chip->oob_poi;
1690 chip->pagebuf = -1;
1691 }
1692
1693 for_each_set_bit(cw, &uncorrectable_cws, ecc->steps) {
1694 if (cw == (ecc->steps - 1)) {
1695 data_size = ecc->size - ((ecc->steps - 1) * 4);
1696 oob_size = (ecc->steps * 4) + host->ecc_bytes_hw;
1697 } else {
1698 data_size = host->cw_data;
1699 oob_size = host->ecc_bytes_hw;
1700 }
1701
1702 /* determine starting buffer address for current CW */
1703 cw_data_buf = data_buf + (cw * host->cw_data);
1704 cw_oob_buf = oob_buf + (cw * ecc->bytes);
1705
1706 ret = qcom_nandc_read_cw_raw(mtd, chip, cw_data_buf,
1707 cw_oob_buf, page, cw);
1708 if (ret)
1709 return ret;
1710
1711 /*
1712 * make sure it isn't an erased page reported
1713 * as not-erased by HW because of a few bitflips
1714 */
1715 ret = nand_check_erased_ecc_chunk(cw_data_buf, data_size,
1716 cw_oob_buf + host->bbm_size,
1717 oob_size, NULL,
1718 0, ecc->strength);
1719 if (ret < 0) {
1720 mtd->ecc_stats.failed++;
1721 } else {
1722 mtd->ecc_stats.corrected += ret;
1723 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1724 }
1725 }
1726
1727 return max_bitflips;
1728}
1729
1730/*
Archit Tanejac76b78d2016-02-03 14:29:50 +05301731 * reads back status registers set by the controller to notify page read
1732 * errors. this is equivalent to what 'ecc->correct()' would do.
1733 */
1734static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301735 u8 *oob_buf, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301736{
1737 struct nand_chip *chip = &host->chip;
1738 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1739 struct mtd_info *mtd = nand_to_mtd(chip);
1740 struct nand_ecc_ctrl *ecc = &chip->ecc;
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301741 unsigned int max_bitflips = 0, uncorrectable_cws = 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301742 struct read_stats *buf;
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301743 bool flash_op_err = false, erased;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301744 int i;
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301745 u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301746
1747 buf = (struct read_stats *)nandc->reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301748 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301749
1750 for (i = 0; i < ecc->steps; i++, buf++) {
1751 u32 flash, buffer, erased_cw;
1752 int data_len, oob_len;
1753
1754 if (i == (ecc->steps - 1)) {
1755 data_len = ecc->size - ((ecc->steps - 1) << 2);
1756 oob_len = ecc->steps << 2;
1757 } else {
1758 data_len = host->cw_data;
1759 oob_len = 0;
1760 }
1761
1762 flash = le32_to_cpu(buf->flash);
1763 buffer = le32_to_cpu(buf->buffer);
1764 erased_cw = le32_to_cpu(buf->erased_cw);
1765
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301766 /*
1767 * Check ECC failure for each codeword. ECC failure can
1768 * happen in either of the following conditions
1769 * 1. If number of bitflips are greater than ECC engine
1770 * capability.
1771 * 2. If this codeword contains all 0xff for which erased
1772 * codeword detection check will be done.
1773 */
1774 if ((flash & FS_OP_ERR) && (buffer & BS_UNCORRECTABLE_BIT)) {
Abhishek Sahu2f610382018-06-20 12:57:35 +05301775 /*
1776 * For BCH ECC, ignore erased codeword errors, if
1777 * ERASED_CW bits are set.
1778 */
Archit Tanejac76b78d2016-02-03 14:29:50 +05301779 if (host->bch_enabled) {
1780 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1781 true : false;
Abhishek Sahu2f610382018-06-20 12:57:35 +05301782 /*
1783 * For RS ECC, HW reports the erased CW by placing
1784 * special characters at certain offsets in the buffer.
1785 * These special characters will be valid only if
1786 * complete page is read i.e. data_buf is not NULL.
1787 */
1788 } else if (data_buf) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05301789 erased = erased_chunk_check_and_fixup(data_buf,
1790 data_len);
Abhishek Sahu2f610382018-06-20 12:57:35 +05301791 } else {
1792 erased = false;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301793 }
1794
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301795 if (!erased)
1796 uncorrectable_cws |= BIT(i);
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301797 /*
1798 * Check if MPU or any other operational error (timeout,
1799 * device failure, etc.) happened for this codeword and
1800 * make flash_op_err true. If flash_op_err is set, then
1801 * EIO will be returned for page read.
1802 */
1803 } else if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1804 flash_op_err = true;
1805 /*
1806 * No ECC or operational errors happened. Check the number of
1807 * bits corrected and update the ecc_stats.corrected.
1808 */
Archit Tanejac76b78d2016-02-03 14:29:50 +05301809 } else {
1810 unsigned int stat;
1811
1812 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1813 mtd->ecc_stats.corrected += stat;
1814 max_bitflips = max(max_bitflips, stat);
1815 }
1816
Abhishek Sahu2f610382018-06-20 12:57:35 +05301817 if (data_buf)
1818 data_buf += data_len;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301819 if (oob_buf)
1820 oob_buf += oob_len + ecc->bytes;
1821 }
1822
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301823 if (flash_op_err)
1824 return -EIO;
1825
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301826 if (!uncorrectable_cws)
1827 return max_bitflips;
1828
1829 return check_for_erased_page(host, data_buf_start, oob_buf_start,
1830 uncorrectable_cws, page,
1831 max_bitflips);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301832}
1833
1834/*
1835 * helper to perform the actual page read operation, used by ecc->read_page(),
1836 * ecc->read_oob()
1837 */
1838static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301839 u8 *oob_buf, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301840{
1841 struct nand_chip *chip = &host->chip;
1842 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1843 struct nand_ecc_ctrl *ecc = &chip->ecc;
Abhishek Sahuadd0cfa2018-06-20 12:57:36 +05301844 u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301845 int i, ret;
1846
Abhishek Sahubde43302017-07-19 17:17:55 +05301847 config_nand_page_read(nandc);
1848
Archit Tanejac76b78d2016-02-03 14:29:50 +05301849 /* queue cmd descs for each codeword */
1850 for (i = 0; i < ecc->steps; i++) {
1851 int data_size, oob_size;
1852
1853 if (i == (ecc->steps - 1)) {
1854 data_size = ecc->size - ((ecc->steps - 1) << 2);
1855 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1856 host->spare_bytes;
1857 } else {
1858 data_size = host->cw_data;
1859 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1860 }
1861
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301862 if (nandc->props->is_bam) {
1863 if (data_buf && oob_buf) {
1864 nandc_set_read_loc(nandc, 0, 0, data_size, 0);
1865 nandc_set_read_loc(nandc, 1, data_size,
1866 oob_size, 1);
1867 } else if (data_buf) {
1868 nandc_set_read_loc(nandc, 0, 0, data_size, 1);
1869 } else {
1870 nandc_set_read_loc(nandc, 0, data_size,
1871 oob_size, 1);
1872 }
1873 }
1874
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301875 config_nand_cw_read(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301876
1877 if (data_buf)
1878 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301879 data_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301880
1881 /*
1882 * when ecc is enabled, the controller doesn't read the real
1883 * or dummy bad block markers in each chunk. To maintain a
1884 * consistent layout across RAW and ECC reads, we just
1885 * leave the real/dummy BBM offsets empty (i.e, filled with
1886 * 0xffs)
1887 */
1888 if (oob_buf) {
1889 int j;
1890
1891 for (j = 0; j < host->bbm_size; j++)
1892 *oob_buf++ = 0xff;
1893
1894 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301895 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301896 }
1897
1898 if (data_buf)
1899 data_buf += data_size;
1900 if (oob_buf)
1901 oob_buf += oob_size;
1902 }
1903
1904 ret = submit_descs(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301905 free_descs(nandc);
1906
Abhishek Sahuadd0cfa2018-06-20 12:57:36 +05301907 if (ret) {
1908 dev_err(nandc->dev, "failure to read page/oob\n");
1909 return ret;
1910 }
1911
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301912 return parse_read_errors(host, data_buf_start, oob_buf_start, page);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301913}
1914
1915/*
1916 * a helper that copies the last step/codeword of a page (containing free oob)
1917 * into our local buffer
1918 */
1919static int copy_last_cw(struct qcom_nand_host *host, int page)
1920{
1921 struct nand_chip *chip = &host->chip;
1922 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1923 struct nand_ecc_ctrl *ecc = &chip->ecc;
1924 int size;
1925 int ret;
1926
1927 clear_read_regs(nandc);
1928
1929 size = host->use_ecc ? host->cw_data : host->cw_size;
1930
1931 /* prepare a clean read buffer */
1932 memset(nandc->data_buffer, 0xff, size);
1933
1934 set_address(host, host->cw_size * (ecc->steps - 1), page);
1935 update_rw_regs(host, 1, true);
1936
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301937 config_nand_single_cw_page_read(nandc, host->use_ecc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301938
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301939 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301940
1941 ret = submit_descs(nandc);
1942 if (ret)
1943 dev_err(nandc->dev, "failed to copy last codeword\n");
1944
1945 free_descs(nandc);
1946
1947 return ret;
1948}
1949
1950/* implements ecc->read_page() */
Boris Brezillonb9761682018-09-06 14:05:20 +02001951static int qcom_nandc_read_page(struct nand_chip *chip, uint8_t *buf,
1952 int oob_required, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301953{
1954 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1955 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1956 u8 *data_buf, *oob_buf = NULL;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301957
Boris Brezillon25f815f2017-11-30 18:01:30 +01001958 nand_read_page_op(chip, page, 0, NULL, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301959 data_buf = buf;
1960 oob_buf = oob_required ? chip->oob_poi : NULL;
1961
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301962 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301963
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301964 return read_page_ecc(host, data_buf, oob_buf, page);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301965}
1966
1967/* implements ecc->read_page_raw() */
Boris Brezillonb9761682018-09-06 14:05:20 +02001968static int qcom_nandc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
Archit Tanejac76b78d2016-02-03 14:29:50 +05301969 int oob_required, int page)
1970{
Boris Brezillonb9761682018-09-06 14:05:20 +02001971 struct mtd_info *mtd = nand_to_mtd(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301972 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301973 struct nand_ecc_ctrl *ecc = &chip->ecc;
Abhishek Sahu85632c12018-06-20 12:57:40 +05301974 int cw, ret;
1975 u8 *data_buf = buf, *oob_buf = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301976
Abhishek Sahu85632c12018-06-20 12:57:40 +05301977 for (cw = 0; cw < ecc->steps; cw++) {
1978 ret = qcom_nandc_read_cw_raw(mtd, chip, data_buf, oob_buf,
1979 page, cw);
1980 if (ret)
1981 return ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301982
Abhishek Sahu85632c12018-06-20 12:57:40 +05301983 data_buf += host->cw_data;
1984 oob_buf += ecc->bytes;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301985 }
1986
Abhishek Sahu85632c12018-06-20 12:57:40 +05301987 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301988}
1989
1990/* implements ecc->read_oob() */
Boris Brezillonb9761682018-09-06 14:05:20 +02001991static int qcom_nandc_read_oob(struct nand_chip *chip, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301992{
1993 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1994 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1995 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301996
1997 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301998 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301999
2000 host->use_ecc = true;
2001 set_address(host, 0, page);
2002 update_rw_regs(host, ecc->steps, true);
2003
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05302004 return read_page_ecc(host, NULL, chip->oob_poi, page);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302005}
2006
2007/* implements ecc->write_page() */
2008static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
2009 const uint8_t *buf, int oob_required, int page)
2010{
2011 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2012 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2013 struct nand_ecc_ctrl *ecc = &chip->ecc;
2014 u8 *data_buf, *oob_buf;
2015 int i, ret;
2016
Boris Brezillon25f815f2017-11-30 18:01:30 +01002017 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2018
Archit Tanejac76b78d2016-02-03 14:29:50 +05302019 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302020 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302021
2022 data_buf = (u8 *)buf;
2023 oob_buf = chip->oob_poi;
2024
2025 host->use_ecc = true;
2026 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302027 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302028
2029 for (i = 0; i < ecc->steps; i++) {
2030 int data_size, oob_size;
2031
2032 if (i == (ecc->steps - 1)) {
2033 data_size = ecc->size - ((ecc->steps - 1) << 2);
2034 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
2035 host->spare_bytes;
2036 } else {
2037 data_size = host->cw_data;
2038 oob_size = ecc->bytes;
2039 }
2040
Archit Tanejac76b78d2016-02-03 14:29:50 +05302041
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302042 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
2043 i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302044
2045 /*
2046 * when ECC is enabled, we don't really need to write anything
2047 * to oob for the first n - 1 codewords since these oob regions
2048 * just contain ECC bytes that's written by the controller
2049 * itself. For the last codeword, we skip the bbm positions and
2050 * write to the free oob area.
2051 */
2052 if (i == (ecc->steps - 1)) {
2053 oob_buf += host->bbm_size;
2054
2055 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302056 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302057 }
2058
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302059 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302060
2061 data_buf += data_size;
2062 oob_buf += oob_size;
2063 }
2064
2065 ret = submit_descs(nandc);
2066 if (ret)
2067 dev_err(nandc->dev, "failure to write page\n");
2068
2069 free_descs(nandc);
2070
Boris Brezillon25f815f2017-11-30 18:01:30 +01002071 if (!ret)
2072 ret = nand_prog_page_end_op(chip);
2073
Archit Tanejac76b78d2016-02-03 14:29:50 +05302074 return ret;
2075}
2076
2077/* implements ecc->write_page_raw() */
2078static int qcom_nandc_write_page_raw(struct mtd_info *mtd,
2079 struct nand_chip *chip, const uint8_t *buf,
2080 int oob_required, int page)
2081{
2082 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2083 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2084 struct nand_ecc_ctrl *ecc = &chip->ecc;
2085 u8 *data_buf, *oob_buf;
2086 int i, ret;
2087
Boris Brezillon25f815f2017-11-30 18:01:30 +01002088 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302089 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302090 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302091
2092 data_buf = (u8 *)buf;
2093 oob_buf = chip->oob_poi;
2094
2095 host->use_ecc = false;
2096 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302097 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302098
2099 for (i = 0; i < ecc->steps; i++) {
2100 int data_size1, data_size2, oob_size1, oob_size2;
2101 int reg_off = FLASH_BUF_ACC;
2102
2103 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
2104 oob_size1 = host->bbm_size;
2105
2106 if (i == (ecc->steps - 1)) {
2107 data_size2 = ecc->size - data_size1 -
2108 ((ecc->steps - 1) << 2);
2109 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
2110 host->spare_bytes;
2111 } else {
2112 data_size2 = host->cw_data - data_size1;
2113 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
2114 }
2115
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302116 write_data_dma(nandc, reg_off, data_buf, data_size1,
2117 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302118 reg_off += data_size1;
2119 data_buf += data_size1;
2120
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302121 write_data_dma(nandc, reg_off, oob_buf, oob_size1,
2122 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302123 reg_off += oob_size1;
2124 oob_buf += oob_size1;
2125
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302126 write_data_dma(nandc, reg_off, data_buf, data_size2,
2127 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302128 reg_off += data_size2;
2129 data_buf += data_size2;
2130
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302131 write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302132 oob_buf += oob_size2;
2133
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302134 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302135 }
2136
2137 ret = submit_descs(nandc);
2138 if (ret)
2139 dev_err(nandc->dev, "failure to write raw page\n");
2140
2141 free_descs(nandc);
2142
Boris Brezillon25f815f2017-11-30 18:01:30 +01002143 if (!ret)
2144 ret = nand_prog_page_end_op(chip);
2145
Archit Tanejac76b78d2016-02-03 14:29:50 +05302146 return ret;
2147}
2148
2149/*
2150 * implements ecc->write_oob()
2151 *
Abhishek Sahu28eed9f2018-06-20 12:57:37 +05302152 * the NAND controller cannot write only data or only OOB within a codeword
2153 * since ECC is calculated for the combined codeword. So update the OOB from
2154 * chip->oob_poi, and pad the data area with OxFF before writing.
Archit Tanejac76b78d2016-02-03 14:29:50 +05302155 */
2156static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
2157 int page)
2158{
2159 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2160 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2161 struct nand_ecc_ctrl *ecc = &chip->ecc;
2162 u8 *oob = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302163 int data_size, oob_size;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002164 int ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302165
2166 host->use_ecc = true;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302167 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302168
2169 /* calculate the data and oob size for the last codeword/step */
2170 data_size = ecc->size - ((ecc->steps - 1) << 2);
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01002171 oob_size = mtd->oobavail;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302172
Abhishek Sahu28eed9f2018-06-20 12:57:37 +05302173 memset(nandc->data_buffer, 0xff, host->cw_data);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302174 /* override new oob content to last codeword */
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01002175 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
2176 0, mtd->oobavail);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302177
2178 set_address(host, host->cw_size * (ecc->steps - 1), page);
2179 update_rw_regs(host, 1, false);
2180
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302181 config_nand_page_write(nandc);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302182 write_data_dma(nandc, FLASH_BUF_ACC,
2183 nandc->data_buffer, data_size + oob_size, 0);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302184 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302185
2186 ret = submit_descs(nandc);
2187
2188 free_descs(nandc);
2189
2190 if (ret) {
2191 dev_err(nandc->dev, "failure to write oob\n");
2192 return -EIO;
2193 }
2194
Boris Brezillon97d90da2017-11-30 18:01:29 +01002195 return nand_prog_page_end_op(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302196}
2197
2198static int qcom_nandc_block_bad(struct mtd_info *mtd, loff_t ofs)
2199{
2200 struct nand_chip *chip = mtd_to_nand(mtd);
2201 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2202 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2203 struct nand_ecc_ctrl *ecc = &chip->ecc;
2204 int page, ret, bbpos, bad = 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302205
2206 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2207
2208 /*
2209 * configure registers for a raw sub page read, the address is set to
2210 * the beginning of the last codeword, we don't care about reading ecc
2211 * portion of oob. we just want the first few bytes from this codeword
2212 * that contains the BBM
2213 */
2214 host->use_ecc = false;
2215
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302216 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302217 ret = copy_last_cw(host, page);
2218 if (ret)
2219 goto err;
2220
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05302221 if (check_flash_errors(host, 1)) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302222 dev_warn(nandc->dev, "error when trying to read BBM\n");
2223 goto err;
2224 }
2225
2226 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
2227
2228 bad = nandc->data_buffer[bbpos] != 0xff;
2229
2230 if (chip->options & NAND_BUSWIDTH_16)
2231 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
2232err:
2233 return bad;
2234}
2235
2236static int qcom_nandc_block_markbad(struct mtd_info *mtd, loff_t ofs)
2237{
2238 struct nand_chip *chip = mtd_to_nand(mtd);
2239 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2240 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2241 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002242 int page, ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302243
2244 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302245 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302246
2247 /*
2248 * to mark the BBM as bad, we flash the entire last codeword with 0s.
2249 * we don't care about the rest of the content in the codeword since
2250 * we aren't going to use this block again
2251 */
2252 memset(nandc->data_buffer, 0x00, host->cw_size);
2253
2254 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2255
2256 /* prepare write */
2257 host->use_ecc = false;
2258 set_address(host, host->cw_size * (ecc->steps - 1), page);
2259 update_rw_regs(host, 1, false);
2260
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302261 config_nand_page_write(nandc);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302262 write_data_dma(nandc, FLASH_BUF_ACC,
2263 nandc->data_buffer, host->cw_size, 0);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302264 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302265
2266 ret = submit_descs(nandc);
2267
2268 free_descs(nandc);
2269
2270 if (ret) {
2271 dev_err(nandc->dev, "failure to update BBM\n");
2272 return -EIO;
2273 }
2274
Boris Brezillon97d90da2017-11-30 18:01:29 +01002275 return nand_prog_page_end_op(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302276}
2277
2278/*
2279 * the three functions below implement chip->read_byte(), chip->read_buf()
2280 * and chip->write_buf() respectively. these aren't used for
2281 * reading/writing page data, they are used for smaller data like reading
2282 * id, status etc
2283 */
2284static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
2285{
2286 struct nand_chip *chip = mtd_to_nand(mtd);
2287 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2288 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2289 u8 *buf = nandc->data_buffer;
2290 u8 ret = 0x0;
2291
2292 if (host->last_command == NAND_CMD_STATUS) {
2293 ret = host->status;
2294
2295 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2296
2297 return ret;
2298 }
2299
2300 if (nandc->buf_start < nandc->buf_count)
2301 ret = buf[nandc->buf_start++];
2302
2303 return ret;
2304}
2305
2306static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
2307{
2308 struct nand_chip *chip = mtd_to_nand(mtd);
2309 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2310 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2311
2312 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
2313 nandc->buf_start += real_len;
2314}
2315
2316static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
2317 int len)
2318{
2319 struct nand_chip *chip = mtd_to_nand(mtd);
2320 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2321 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2322
2323 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
2324
2325 nandc->buf_start += real_len;
2326}
2327
2328/* we support only one external chip for now */
2329static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
2330{
2331 struct nand_chip *chip = mtd_to_nand(mtd);
2332 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2333
2334 if (chipnr <= 0)
2335 return;
2336
2337 dev_warn(nandc->dev, "invalid chip select\n");
2338}
2339
2340/*
2341 * NAND controller page layout info
2342 *
2343 * Layout with ECC enabled:
2344 *
2345 * |----------------------| |---------------------------------|
2346 * | xx.......yy| | *********xx.......yy|
2347 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
2348 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
2349 * | xx.......yy| | *********xx.......yy|
2350 * |----------------------| |---------------------------------|
2351 * codeword 1,2..n-1 codeword n
2352 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
2353 *
2354 * n = Number of codewords in the page
2355 * . = ECC bytes
2356 * * = Spare/free bytes
2357 * x = Unused byte(s)
2358 * y = Reserved byte(s)
2359 *
2360 * 2K page: n = 4, spare = 16 bytes
2361 * 4K page: n = 8, spare = 32 bytes
2362 * 8K page: n = 16, spare = 64 bytes
2363 *
2364 * the qcom nand controller operates at a sub page/codeword level. each
2365 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2366 * the number of ECC bytes vary based on the ECC strength and the bus width.
2367 *
2368 * the first n - 1 codewords contains 516 bytes of user data, the remaining
2369 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2370 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2371 *
2372 * When we access a page with ECC enabled, the reserved bytes(s) are not
2373 * accessible at all. When reading, we fill up these unreadable positions
2374 * with 0xffs. When writing, the controller skips writing the inaccessible
2375 * bytes.
2376 *
2377 * Layout with ECC disabled:
2378 *
2379 * |------------------------------| |---------------------------------------|
2380 * | yy xx.......| | bb *********xx.......|
2381 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
2382 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
2383 * | yy xx.......| | bb *********xx.......|
2384 * |------------------------------| |---------------------------------------|
2385 * codeword 1,2..n-1 codeword n
2386 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
2387 *
2388 * n = Number of codewords in the page
2389 * . = ECC bytes
2390 * * = Spare/free bytes
2391 * x = Unused byte(s)
2392 * y = Dummy Bad Bock byte(s)
2393 * b = Real Bad Block byte(s)
2394 * size1/size2 = function of codeword size and 'n'
2395 *
2396 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2397 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2398 * Block Markers. In the last codeword, this position contains the real BBM
2399 *
2400 * In order to have a consistent layout between RAW and ECC modes, we assume
2401 * the following OOB layout arrangement:
2402 *
2403 * |-----------| |--------------------|
2404 * |yyxx.......| |bb*********xx.......|
2405 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
2406 * |yyxx.......| |bb*********xx.......|
2407 * |yyxx.......| |bb*********xx.......|
2408 * |-----------| |--------------------|
2409 * first n - 1 nth OOB region
2410 * OOB regions
2411 *
2412 * n = Number of codewords in the page
2413 * . = ECC bytes
2414 * * = FREE OOB bytes
2415 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2416 * x = Unused byte(s)
2417 * b = Real bad block byte(s) (inaccessible when ECC enabled)
2418 *
2419 * This layout is read as is when ECC is disabled. When ECC is enabled, the
2420 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2421 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
Boris Brezillon421e81c2016-03-18 17:54:27 +01002422 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2423 * the sum of the three).
Archit Tanejac76b78d2016-02-03 14:29:50 +05302424 */
Boris Brezillon421e81c2016-03-18 17:54:27 +01002425static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2426 struct mtd_oob_region *oobregion)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302427{
Boris Brezillon421e81c2016-03-18 17:54:27 +01002428 struct nand_chip *chip = mtd_to_nand(mtd);
2429 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302430 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302431
Boris Brezillon421e81c2016-03-18 17:54:27 +01002432 if (section > 1)
2433 return -ERANGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302434
Boris Brezillon421e81c2016-03-18 17:54:27 +01002435 if (!section) {
2436 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
2437 host->bbm_size;
2438 oobregion->offset = 0;
2439 } else {
2440 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
2441 oobregion->offset = mtd->oobsize - oobregion->length;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302442 }
2443
Boris Brezillon421e81c2016-03-18 17:54:27 +01002444 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302445}
2446
Boris Brezillon421e81c2016-03-18 17:54:27 +01002447static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
2448 struct mtd_oob_region *oobregion)
2449{
2450 struct nand_chip *chip = mtd_to_nand(mtd);
2451 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2452 struct nand_ecc_ctrl *ecc = &chip->ecc;
2453
2454 if (section)
2455 return -ERANGE;
2456
2457 oobregion->length = ecc->steps * 4;
2458 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
2459
2460 return 0;
2461}
2462
2463static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
2464 .ecc = qcom_nand_ooblayout_ecc,
2465 .free = qcom_nand_ooblayout_free,
2466};
2467
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302468static int
2469qcom_nandc_calc_ecc_bytes(int step_size, int strength)
2470{
2471 return strength == 4 ? 12 : 16;
2472}
2473NAND_ECC_CAPS_SINGLE(qcom_nandc_ecc_caps, qcom_nandc_calc_ecc_bytes,
2474 NANDC_STEP_SIZE, 4, 8);
2475
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002476static int qcom_nand_attach_chip(struct nand_chip *chip)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302477{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302478 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002479 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302480 struct nand_ecc_ctrl *ecc = &chip->ecc;
2481 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302482 int cwperpage, bad_block_byte, ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302483 bool wide_bus;
2484 int ecc_mode = 1;
2485
Abhishek Sahu320bdb52018-06-20 12:57:31 +05302486 /* controller only supports 512 bytes data steps */
2487 ecc->size = NANDC_STEP_SIZE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302488 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302489 cwperpage = mtd->writesize / NANDC_STEP_SIZE;
2490
2491 /*
2492 * Each CW has 4 available OOB bytes which will be protected with ECC
2493 * so remaining bytes can be used for ECC.
2494 */
2495 ret = nand_ecc_choose_conf(chip, &qcom_nandc_ecc_caps,
2496 mtd->oobsize - (cwperpage * 4));
2497 if (ret) {
2498 dev_err(nandc->dev, "No valid ECC settings possible\n");
2499 return ret;
2500 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302501
2502 if (ecc->strength >= 8) {
2503 /* 8 bit ECC defaults to BCH ECC on all platforms */
2504 host->bch_enabled = true;
2505 ecc_mode = 1;
2506
2507 if (wide_bus) {
2508 host->ecc_bytes_hw = 14;
2509 host->spare_bytes = 0;
2510 host->bbm_size = 2;
2511 } else {
2512 host->ecc_bytes_hw = 13;
2513 host->spare_bytes = 2;
2514 host->bbm_size = 1;
2515 }
2516 } else {
2517 /*
2518 * if the controller supports BCH for 4 bit ECC, the controller
2519 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2520 * always 10 bytes
2521 */
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302522 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302523 /* BCH */
2524 host->bch_enabled = true;
2525 ecc_mode = 0;
2526
2527 if (wide_bus) {
2528 host->ecc_bytes_hw = 8;
2529 host->spare_bytes = 2;
2530 host->bbm_size = 2;
2531 } else {
2532 host->ecc_bytes_hw = 7;
2533 host->spare_bytes = 4;
2534 host->bbm_size = 1;
2535 }
2536 } else {
2537 /* RS */
2538 host->ecc_bytes_hw = 10;
2539
2540 if (wide_bus) {
2541 host->spare_bytes = 0;
2542 host->bbm_size = 2;
2543 } else {
2544 host->spare_bytes = 1;
2545 host->bbm_size = 1;
2546 }
2547 }
2548 }
2549
2550 /*
2551 * we consider ecc->bytes as the sum of all the non-data content in a
2552 * step. It gives us a clean representation of the oob area (even if
2553 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2554 * ECC and 12 bytes for 4 bit ECC
2555 */
2556 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
2557
2558 ecc->read_page = qcom_nandc_read_page;
2559 ecc->read_page_raw = qcom_nandc_read_page_raw;
2560 ecc->read_oob = qcom_nandc_read_oob;
2561 ecc->write_page = qcom_nandc_write_page;
2562 ecc->write_page_raw = qcom_nandc_write_page_raw;
2563 ecc->write_oob = qcom_nandc_write_oob;
2564
2565 ecc->mode = NAND_ECC_HW;
2566
Boris Brezillon421e81c2016-03-18 17:54:27 +01002567 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302568
Abhishek Sahucb80f112017-08-17 17:37:40 +05302569 nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
2570 cwperpage);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302571
2572 /*
2573 * DATA_UD_BYTES varies based on whether the read/write command protects
2574 * spare data with ECC too. We protect spare data by default, so we set
2575 * it to main + spare data, which are 512 and 4 bytes respectively.
2576 */
2577 host->cw_data = 516;
2578
2579 /*
2580 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2581 * for 8 bit ECC
2582 */
2583 host->cw_size = host->cw_data + ecc->bytes;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302584 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
2585
2586 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
2587 | host->cw_data << UD_SIZE_BYTES
2588 | 0 << DISABLE_STATUS_AFTER_WRITE
2589 | 5 << NUM_ADDR_CYCLES
2590 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
2591 | 0 << STATUS_BFR_READ
2592 | 1 << SET_RD_MODE_AFTER_STATUS
2593 | host->spare_bytes << SPARE_SIZE_BYTES;
2594
2595 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
2596 | 0 << CS_ACTIVE_BSY
2597 | bad_block_byte << BAD_BLOCK_BYTE_NUM
2598 | 0 << BAD_BLOCK_IN_SPARE_AREA
2599 | 2 << WR_RD_BSY_GAP
2600 | wide_bus << WIDE_FLASH
2601 | host->bch_enabled << ENABLE_BCH_ECC;
2602
2603 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
2604 | host->cw_size << UD_SIZE_BYTES
2605 | 5 << NUM_ADDR_CYCLES
2606 | 0 << SPARE_SIZE_BYTES;
2607
2608 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
2609 | 0 << CS_ACTIVE_BSY
2610 | 17 << BAD_BLOCK_BYTE_NUM
2611 | 1 << BAD_BLOCK_IN_SPARE_AREA
2612 | 2 << WR_RD_BSY_GAP
2613 | wide_bus << WIDE_FLASH
2614 | 1 << DEV0_CFG1_ECC_DISABLE;
2615
Abhishek Sahu10777de2017-08-03 17:56:39 +02002616 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
Archit Tanejac76b78d2016-02-03 14:29:50 +05302617 | 0 << ECC_SW_RESET
2618 | host->cw_data << ECC_NUM_DATA_BYTES
2619 | 1 << ECC_FORCE_CLK_OPEN
2620 | ecc_mode << ECC_MODE
2621 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
2622
2623 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
2624
2625 host->clrflashstatus = FS_READY_BSY_N;
2626 host->clrreadstatus = 0xc0;
Abhishek Sahua86b9c42017-08-17 17:37:44 +05302627 nandc->regs->erased_cw_detect_cfg_clr =
2628 cpu_to_le32(CLR_ERASED_PAGE_DET);
2629 nandc->regs->erased_cw_detect_cfg_set =
2630 cpu_to_le32(SET_ERASED_PAGE_DET);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302631
2632 dev_dbg(nandc->dev,
2633 "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",
2634 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
2635 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
2636 cwperpage);
2637
2638 return 0;
2639}
2640
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002641static const struct nand_controller_ops qcom_nandc_ops = {
2642 .attach_chip = qcom_nand_attach_chip,
2643};
2644
Archit Tanejac76b78d2016-02-03 14:29:50 +05302645static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
2646{
2647 int ret;
2648
2649 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
2650 if (ret) {
2651 dev_err(nandc->dev, "failed to set DMA mask\n");
2652 return ret;
2653 }
2654
2655 /*
2656 * we use the internal buffer for reading ONFI params, reading small
2657 * data like ID and status, and preforming read-copy-write operations
2658 * when writing to a codeword partially. 532 is the maximum possible
2659 * size of a codeword for our nand controller
2660 */
2661 nandc->buf_size = 532;
2662
2663 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
2664 GFP_KERNEL);
2665 if (!nandc->data_buffer)
2666 return -ENOMEM;
2667
2668 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
2669 GFP_KERNEL);
2670 if (!nandc->regs)
2671 return -ENOMEM;
2672
Kees Cooka86854d2018-06-12 14:07:58 -07002673 nandc->reg_read_buf = devm_kcalloc(nandc->dev,
2674 MAX_REG_RD, sizeof(*nandc->reg_read_buf),
Archit Tanejac76b78d2016-02-03 14:29:50 +05302675 GFP_KERNEL);
2676 if (!nandc->reg_read_buf)
2677 return -ENOMEM;
2678
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302679 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302680 nandc->reg_read_dma =
2681 dma_map_single(nandc->dev, nandc->reg_read_buf,
2682 MAX_REG_RD *
2683 sizeof(*nandc->reg_read_buf),
2684 DMA_FROM_DEVICE);
2685 if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
2686 dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
2687 return -EIO;
2688 }
2689
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302690 nandc->tx_chan = dma_request_slave_channel(nandc->dev, "tx");
2691 if (!nandc->tx_chan) {
2692 dev_err(nandc->dev, "failed to request tx channel\n");
2693 return -ENODEV;
2694 }
2695
2696 nandc->rx_chan = dma_request_slave_channel(nandc->dev, "rx");
2697 if (!nandc->rx_chan) {
2698 dev_err(nandc->dev, "failed to request rx channel\n");
2699 return -ENODEV;
2700 }
2701
2702 nandc->cmd_chan = dma_request_slave_channel(nandc->dev, "cmd");
2703 if (!nandc->cmd_chan) {
2704 dev_err(nandc->dev, "failed to request cmd channel\n");
2705 return -ENODEV;
2706 }
Abhishek Sahucb80f112017-08-17 17:37:40 +05302707
2708 /*
2709 * Initially allocate BAM transaction to read ONFI param page.
2710 * After detecting all the devices, this BAM transaction will
2711 * be freed and the next BAM tranasction will be allocated with
2712 * maximum codeword size
2713 */
2714 nandc->max_cwperpage = 1;
2715 nandc->bam_txn = alloc_bam_transaction(nandc);
2716 if (!nandc->bam_txn) {
2717 dev_err(nandc->dev,
2718 "failed to allocate bam transaction\n");
2719 return -ENOMEM;
2720 }
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302721 } else {
2722 nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
2723 if (!nandc->chan) {
2724 dev_err(nandc->dev,
2725 "failed to request slave channel\n");
2726 return -ENODEV;
2727 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302728 }
2729
2730 INIT_LIST_HEAD(&nandc->desc_list);
2731 INIT_LIST_HEAD(&nandc->host_list);
2732
Miquel Raynal7da45132018-07-17 09:08:02 +02002733 nand_controller_init(&nandc->controller);
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002734 nandc->controller.ops = &qcom_nandc_ops;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302735
2736 return 0;
2737}
2738
2739static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2740{
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302741 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302742 if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
2743 dma_unmap_single(nandc->dev, nandc->reg_read_dma,
2744 MAX_REG_RD *
2745 sizeof(*nandc->reg_read_buf),
2746 DMA_FROM_DEVICE);
2747
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302748 if (nandc->tx_chan)
2749 dma_release_channel(nandc->tx_chan);
2750
2751 if (nandc->rx_chan)
2752 dma_release_channel(nandc->rx_chan);
2753
2754 if (nandc->cmd_chan)
2755 dma_release_channel(nandc->cmd_chan);
2756 } else {
2757 if (nandc->chan)
2758 dma_release_channel(nandc->chan);
2759 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302760}
2761
2762/* one time setup of a few nand controller registers */
2763static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2764{
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302765 u32 nand_ctrl;
2766
Archit Tanejac76b78d2016-02-03 14:29:50 +05302767 /* kill onenand */
2768 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
Abhishek Sahucc409b92017-08-17 17:37:47 +05302769 nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD),
2770 NAND_DEV_CMD_VLD_VAL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302771
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302772 /* enable ADM or BAM DMA */
2773 if (nandc->props->is_bam) {
2774 nand_ctrl = nandc_read(nandc, NAND_CTRL);
2775 nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
2776 } else {
2777 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2778 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302779
2780 /* save the original values of these registers */
Abhishek Sahucc409b92017-08-17 17:37:47 +05302781 nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1));
Abhishek Sahud8a9b322017-08-11 17:09:16 +05302782 nandc->vld = NAND_DEV_CMD_VLD_VAL;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302783
2784 return 0;
2785}
2786
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002787static int qcom_nand_host_init_and_register(struct qcom_nand_controller *nandc,
2788 struct qcom_nand_host *host,
2789 struct device_node *dn)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302790{
2791 struct nand_chip *chip = &host->chip;
2792 struct mtd_info *mtd = nand_to_mtd(chip);
2793 struct device *dev = nandc->dev;
2794 int ret;
2795
2796 ret = of_property_read_u32(dn, "reg", &host->cs);
2797 if (ret) {
2798 dev_err(dev, "can't get chip-select\n");
2799 return -ENXIO;
2800 }
2801
2802 nand_set_flash_node(chip, dn);
2803 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
Fabio Estevam069f0532018-01-05 18:02:55 -02002804 if (!mtd->name)
2805 return -ENOMEM;
2806
Archit Tanejac76b78d2016-02-03 14:29:50 +05302807 mtd->owner = THIS_MODULE;
2808 mtd->dev.parent = dev;
2809
2810 chip->cmdfunc = qcom_nandc_command;
2811 chip->select_chip = qcom_nandc_select_chip;
2812 chip->read_byte = qcom_nandc_read_byte;
2813 chip->read_buf = qcom_nandc_read_buf;
2814 chip->write_buf = qcom_nandc_write_buf;
Miquel Raynalb9587582018-03-19 14:47:19 +01002815 chip->set_features = nand_get_set_features_notsupp;
2816 chip->get_features = nand_get_set_features_notsupp;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302817
2818 /*
2819 * the bad block marker is readable only when we read the last codeword
2820 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2821 * helpers don't allow us to read BB from a nand chip with ECC
2822 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2823 * and block_markbad helpers until we permanently switch to using
2824 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2825 */
2826 chip->block_bad = qcom_nandc_block_bad;
2827 chip->block_markbad = qcom_nandc_block_markbad;
2828
2829 chip->controller = &nandc->controller;
2830 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
2831 NAND_SKIP_BBTSCAN;
2832
2833 /* set up initial status value */
2834 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2835
Boris Brezillon00ad3782018-09-06 14:05:14 +02002836 ret = nand_scan(chip, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302837 if (ret)
2838 return ret;
2839
Abhishek Sahu89f51272017-07-19 17:17:58 +05302840 ret = mtd_device_register(mtd, NULL, 0);
2841 if (ret)
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002842 nand_cleanup(chip);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302843
2844 return ret;
2845}
2846
2847static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2848{
2849 struct device *dev = nandc->dev;
2850 struct device_node *dn = dev->of_node, *child;
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002851 struct qcom_nand_host *host;
Abhishek Sahu89f51272017-07-19 17:17:58 +05302852 int ret;
2853
Abhishek Sahucb80f112017-08-17 17:37:40 +05302854 if (nandc->props->is_bam) {
2855 free_bam_transaction(nandc);
2856 nandc->bam_txn = alloc_bam_transaction(nandc);
2857 if (!nandc->bam_txn) {
2858 dev_err(nandc->dev,
2859 "failed to allocate bam transaction\n");
2860 return -ENOMEM;
2861 }
2862 }
2863
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002864 for_each_available_child_of_node(dn, child) {
2865 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2866 if (!host) {
2867 of_node_put(child);
2868 return -ENOMEM;
Abhishek Sahu89f51272017-07-19 17:17:58 +05302869 }
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002870
2871 ret = qcom_nand_host_init_and_register(nandc, host, child);
2872 if (ret) {
2873 devm_kfree(dev, host);
2874 continue;
2875 }
2876
2877 list_add_tail(&host->node, &nandc->host_list);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302878 }
2879
2880 if (list_empty(&nandc->host_list))
2881 return -ENODEV;
2882
2883 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302884}
2885
2886/* parse custom DT properties here */
2887static int qcom_nandc_parse_dt(struct platform_device *pdev)
2888{
2889 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2890 struct device_node *np = nandc->dev->of_node;
2891 int ret;
2892
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302893 if (!nandc->props->is_bam) {
2894 ret = of_property_read_u32(np, "qcom,cmd-crci",
2895 &nandc->cmd_crci);
2896 if (ret) {
2897 dev_err(nandc->dev, "command CRCI unspecified\n");
2898 return ret;
2899 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302900
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302901 ret = of_property_read_u32(np, "qcom,data-crci",
2902 &nandc->data_crci);
2903 if (ret) {
2904 dev_err(nandc->dev, "data CRCI unspecified\n");
2905 return ret;
2906 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302907 }
2908
2909 return 0;
2910}
2911
2912static int qcom_nandc_probe(struct platform_device *pdev)
2913{
2914 struct qcom_nand_controller *nandc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302915 const void *dev_data;
2916 struct device *dev = &pdev->dev;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302917 struct resource *res;
2918 int ret;
2919
2920 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2921 if (!nandc)
2922 return -ENOMEM;
2923
2924 platform_set_drvdata(pdev, nandc);
2925 nandc->dev = dev;
2926
2927 dev_data = of_device_get_match_data(dev);
2928 if (!dev_data) {
2929 dev_err(&pdev->dev, "failed to get device data\n");
2930 return -ENODEV;
2931 }
2932
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302933 nandc->props = dev_data;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302934
Archit Tanejac76b78d2016-02-03 14:29:50 +05302935 nandc->core_clk = devm_clk_get(dev, "core");
2936 if (IS_ERR(nandc->core_clk))
2937 return PTR_ERR(nandc->core_clk);
2938
2939 nandc->aon_clk = devm_clk_get(dev, "aon");
2940 if (IS_ERR(nandc->aon_clk))
2941 return PTR_ERR(nandc->aon_clk);
2942
2943 ret = qcom_nandc_parse_dt(pdev);
2944 if (ret)
2945 return ret;
2946
Arnd Bergmann7330fc52018-07-17 22:27:42 +02002947 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2948 nandc->base = devm_ioremap_resource(dev, res);
2949 if (IS_ERR(nandc->base))
2950 return PTR_ERR(nandc->base);
2951
2952 nandc->base_phys = res->start;
2953 nandc->base_dma = dma_map_resource(dev, res->start,
2954 resource_size(res),
2955 DMA_BIDIRECTIONAL, 0);
2956 if (!nandc->base_dma)
2957 return -ENXIO;
2958
Archit Tanejac76b78d2016-02-03 14:29:50 +05302959 ret = qcom_nandc_alloc(nandc);
2960 if (ret)
Arnd Bergmann7330fc52018-07-17 22:27:42 +02002961 goto err_nandc_alloc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302962
2963 ret = clk_prepare_enable(nandc->core_clk);
2964 if (ret)
2965 goto err_core_clk;
2966
2967 ret = clk_prepare_enable(nandc->aon_clk);
2968 if (ret)
2969 goto err_aon_clk;
2970
2971 ret = qcom_nandc_setup(nandc);
2972 if (ret)
2973 goto err_setup;
2974
Abhishek Sahu89f51272017-07-19 17:17:58 +05302975 ret = qcom_probe_nand_devices(nandc);
2976 if (ret)
2977 goto err_setup;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302978
2979 return 0;
2980
Archit Tanejac76b78d2016-02-03 14:29:50 +05302981err_setup:
2982 clk_disable_unprepare(nandc->aon_clk);
2983err_aon_clk:
2984 clk_disable_unprepare(nandc->core_clk);
2985err_core_clk:
2986 qcom_nandc_unalloc(nandc);
Arnd Bergmann7330fc52018-07-17 22:27:42 +02002987err_nandc_alloc:
2988 dma_unmap_resource(dev, res->start, resource_size(res),
2989 DMA_BIDIRECTIONAL, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302990
2991 return ret;
2992}
2993
2994static int qcom_nandc_remove(struct platform_device *pdev)
2995{
2996 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
Arnd Bergmann7330fc52018-07-17 22:27:42 +02002997 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302998 struct qcom_nand_host *host;
2999
3000 list_for_each_entry(host, &nandc->host_list, node)
Boris Brezillon59ac2762018-09-06 14:05:15 +02003001 nand_release(&host->chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05303002
Arnd Bergmann7330fc52018-07-17 22:27:42 +02003003
Archit Tanejac76b78d2016-02-03 14:29:50 +05303004 qcom_nandc_unalloc(nandc);
3005
3006 clk_disable_unprepare(nandc->aon_clk);
3007 clk_disable_unprepare(nandc->core_clk);
3008
Arnd Bergmann7330fc52018-07-17 22:27:42 +02003009 dma_unmap_resource(&pdev->dev, nandc->base_dma, resource_size(res),
3010 DMA_BIDIRECTIONAL, 0);
3011
Archit Tanejac76b78d2016-02-03 14:29:50 +05303012 return 0;
3013}
3014
Abhishek Sahu58f1f222017-08-11 17:09:17 +05303015static const struct qcom_nandc_props ipq806x_nandc_props = {
3016 .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +05303017 .is_bam = false,
Abhishek Sahucc409b92017-08-17 17:37:47 +05303018 .dev_cmd_reg_start = 0x0,
Abhishek Sahu58f1f222017-08-11 17:09:17 +05303019};
Archit Tanejac76b78d2016-02-03 14:29:50 +05303020
Abhishek Sahua0637832017-08-17 17:37:53 +05303021static const struct qcom_nandc_props ipq4019_nandc_props = {
3022 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3023 .is_bam = true,
3024 .dev_cmd_reg_start = 0x0,
3025};
3026
Abhishek Sahudce84762017-08-17 17:37:54 +05303027static const struct qcom_nandc_props ipq8074_nandc_props = {
3028 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3029 .is_bam = true,
3030 .dev_cmd_reg_start = 0x7000,
3031};
3032
Archit Tanejac76b78d2016-02-03 14:29:50 +05303033/*
3034 * data will hold a struct pointer containing more differences once we support
3035 * more controller variants
3036 */
3037static const struct of_device_id qcom_nandc_of_match[] = {
Abhishek Sahu58f1f222017-08-11 17:09:17 +05303038 {
3039 .compatible = "qcom,ipq806x-nand",
3040 .data = &ipq806x_nandc_props,
Archit Tanejac76b78d2016-02-03 14:29:50 +05303041 },
Abhishek Sahua0637832017-08-17 17:37:53 +05303042 {
3043 .compatible = "qcom,ipq4019-nand",
3044 .data = &ipq4019_nandc_props,
3045 },
Abhishek Sahudce84762017-08-17 17:37:54 +05303046 {
3047 .compatible = "qcom,ipq8074-nand",
3048 .data = &ipq8074_nandc_props,
3049 },
Archit Tanejac76b78d2016-02-03 14:29:50 +05303050 {}
3051};
3052MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
3053
3054static struct platform_driver qcom_nandc_driver = {
3055 .driver = {
3056 .name = "qcom-nandc",
3057 .of_match_table = qcom_nandc_of_match,
3058 },
3059 .probe = qcom_nandc_probe,
3060 .remove = qcom_nandc_remove,
3061};
3062module_platform_driver(qcom_nandc_driver);
3063
3064MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
3065MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
3066MODULE_LICENSE("GPL v2");