blob: 9037dddff99a91dd0f9b6886f0347e4154b51d0e [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 */
Boris Brezillon5295cf22018-09-06 14:05:28 +02001443static void qcom_nandc_command(struct nand_chip *chip, unsigned int command,
Archit Tanejac76b78d2016-02-03 14:29:50 +05301444 int column, int page_addr)
1445{
Archit Tanejac76b78d2016-02-03 14:29:50 +05301446 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1447 struct nand_ecc_ctrl *ecc = &chip->ecc;
1448 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1449 bool wait = false;
1450 int ret = 0;
1451
1452 pre_command(host, command);
1453
1454 switch (command) {
1455 case NAND_CMD_RESET:
1456 ret = reset(host);
1457 wait = true;
1458 break;
1459
1460 case NAND_CMD_READID:
1461 nandc->buf_count = 4;
1462 ret = read_id(host, column);
1463 wait = true;
1464 break;
1465
1466 case NAND_CMD_PARAM:
1467 ret = nandc_param(host);
1468 wait = true;
1469 break;
1470
1471 case NAND_CMD_ERASE1:
1472 ret = erase_block(host, page_addr);
1473 wait = true;
1474 break;
1475
1476 case NAND_CMD_READ0:
1477 /* we read the entire page for now */
1478 WARN_ON(column != 0);
1479
1480 host->use_ecc = true;
1481 set_address(host, 0, page_addr);
1482 update_rw_regs(host, ecc->steps, true);
1483 break;
1484
1485 case NAND_CMD_SEQIN:
1486 WARN_ON(column != 0);
1487 set_address(host, 0, page_addr);
1488 break;
1489
1490 case NAND_CMD_PAGEPROG:
1491 case NAND_CMD_STATUS:
1492 case NAND_CMD_NONE:
1493 default:
1494 break;
1495 }
1496
1497 if (ret) {
1498 dev_err(nandc->dev, "failure executing command %d\n",
1499 command);
1500 free_descs(nandc);
1501 return;
1502 }
1503
1504 if (wait) {
1505 ret = submit_descs(nandc);
1506 if (ret)
1507 dev_err(nandc->dev,
1508 "failure submitting descs for command %d\n",
1509 command);
1510 }
1511
1512 free_descs(nandc);
1513
1514 post_command(host, command);
1515}
1516
1517/*
1518 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1519 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1520 *
1521 * when using RS ECC, the HW reports the same erros when reading an erased CW,
1522 * but it notifies that it is an erased CW by placing special characters at
1523 * certain offsets in the buffer.
1524 *
1525 * verify if the page is erased or not, and fix up the page for RS ECC by
1526 * replacing the special characters with 0xff.
1527 */
1528static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
1529{
1530 u8 empty1, empty2;
1531
1532 /*
1533 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1534 * is erased by looking for 0x54s at offsets 3 and 175 from the
1535 * beginning of each codeword
1536 */
1537
1538 empty1 = data_buf[3];
1539 empty2 = data_buf[175];
1540
1541 /*
1542 * if the erased codework markers, if they exist override them with
1543 * 0xffs
1544 */
1545 if ((empty1 == 0x54 && empty2 == 0xff) ||
1546 (empty1 == 0xff && empty2 == 0x54)) {
1547 data_buf[3] = 0xff;
1548 data_buf[175] = 0xff;
1549 }
1550
1551 /*
1552 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1553 * restore the original values at the special offsets
1554 */
1555 if (memchr_inv(data_buf, 0xff, data_len)) {
1556 data_buf[3] = empty1;
1557 data_buf[175] = empty2;
1558
1559 return false;
1560 }
1561
1562 return true;
1563}
1564
1565struct read_stats {
1566 __le32 flash;
1567 __le32 buffer;
1568 __le32 erased_cw;
1569};
1570
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301571/* reads back FLASH_STATUS register set by the controller */
1572static int check_flash_errors(struct qcom_nand_host *host, int cw_cnt)
1573{
1574 struct nand_chip *chip = &host->chip;
1575 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1576 int i;
1577
1578 for (i = 0; i < cw_cnt; i++) {
1579 u32 flash = le32_to_cpu(nandc->reg_read_buf[i]);
1580
1581 if (flash & (FS_OP_ERR | FS_MPU_ERR))
1582 return -EIO;
1583 }
1584
1585 return 0;
1586}
1587
Abhishek Sahu85632c12018-06-20 12:57:40 +05301588/* performs raw read for one codeword */
1589static int
1590qcom_nandc_read_cw_raw(struct mtd_info *mtd, struct nand_chip *chip,
1591 u8 *data_buf, u8 *oob_buf, int page, int cw)
1592{
1593 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1594 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1595 struct nand_ecc_ctrl *ecc = &chip->ecc;
1596 int data_size1, data_size2, oob_size1, oob_size2;
1597 int ret, reg_off = FLASH_BUF_ACC, read_loc = 0;
1598
1599 nand_read_page_op(chip, page, 0, NULL, 0);
1600 host->use_ecc = false;
1601
1602 clear_bam_transaction(nandc);
1603 set_address(host, host->cw_size * cw, page);
1604 update_rw_regs(host, 1, true);
1605 config_nand_page_read(nandc);
1606
1607 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1608 oob_size1 = host->bbm_size;
1609
1610 if (cw == (ecc->steps - 1)) {
1611 data_size2 = ecc->size - data_size1 -
1612 ((ecc->steps - 1) * 4);
1613 oob_size2 = (ecc->steps * 4) + host->ecc_bytes_hw +
1614 host->spare_bytes;
1615 } else {
1616 data_size2 = host->cw_data - data_size1;
1617 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1618 }
1619
1620 if (nandc->props->is_bam) {
1621 nandc_set_read_loc(nandc, 0, read_loc, data_size1, 0);
1622 read_loc += data_size1;
1623
1624 nandc_set_read_loc(nandc, 1, read_loc, oob_size1, 0);
1625 read_loc += oob_size1;
1626
1627 nandc_set_read_loc(nandc, 2, read_loc, data_size2, 0);
1628 read_loc += data_size2;
1629
1630 nandc_set_read_loc(nandc, 3, read_loc, oob_size2, 1);
1631 }
1632
1633 config_nand_cw_read(nandc, false);
1634
1635 read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
1636 reg_off += data_size1;
1637
1638 read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
1639 reg_off += oob_size1;
1640
1641 read_data_dma(nandc, reg_off, data_buf + data_size1, data_size2, 0);
1642 reg_off += data_size2;
1643
1644 read_data_dma(nandc, reg_off, oob_buf + oob_size1, oob_size2, 0);
1645
1646 ret = submit_descs(nandc);
1647 free_descs(nandc);
1648 if (ret) {
1649 dev_err(nandc->dev, "failure to read raw cw %d\n", cw);
1650 return ret;
1651 }
1652
1653 return check_flash_errors(host, 1);
1654}
1655
Archit Tanejac76b78d2016-02-03 14:29:50 +05301656/*
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301657 * Bitflips can happen in erased codewords also so this function counts the
1658 * number of 0 in each CW for which ECC engine returns the uncorrectable
1659 * error. The page will be assumed as erased if this count is less than or
1660 * equal to the ecc->strength for each CW.
1661 *
1662 * 1. Both DATA and OOB need to be checked for number of 0. The
1663 * top-level API can be called with only data buf or OOB buf so use
1664 * chip->data_buf if data buf is null and chip->oob_poi if oob buf
1665 * is null for copying the raw bytes.
1666 * 2. Perform raw read for all the CW which has uncorrectable errors.
1667 * 3. For each CW, check the number of 0 in cw_data and usable OOB bytes.
1668 * The BBM and spare bytes bit flip won’t affect the ECC so don’t check
1669 * the number of bitflips in this area.
1670 */
1671static int
1672check_for_erased_page(struct qcom_nand_host *host, u8 *data_buf,
1673 u8 *oob_buf, unsigned long uncorrectable_cws,
1674 int page, unsigned int max_bitflips)
1675{
1676 struct nand_chip *chip = &host->chip;
1677 struct mtd_info *mtd = nand_to_mtd(chip);
1678 struct nand_ecc_ctrl *ecc = &chip->ecc;
1679 u8 *cw_data_buf, *cw_oob_buf;
1680 int cw, data_size, oob_size, ret = 0;
1681
1682 if (!data_buf) {
1683 data_buf = chip->data_buf;
1684 chip->pagebuf = -1;
1685 }
1686
1687 if (!oob_buf) {
1688 oob_buf = chip->oob_poi;
1689 chip->pagebuf = -1;
1690 }
1691
1692 for_each_set_bit(cw, &uncorrectable_cws, ecc->steps) {
1693 if (cw == (ecc->steps - 1)) {
1694 data_size = ecc->size - ((ecc->steps - 1) * 4);
1695 oob_size = (ecc->steps * 4) + host->ecc_bytes_hw;
1696 } else {
1697 data_size = host->cw_data;
1698 oob_size = host->ecc_bytes_hw;
1699 }
1700
1701 /* determine starting buffer address for current CW */
1702 cw_data_buf = data_buf + (cw * host->cw_data);
1703 cw_oob_buf = oob_buf + (cw * ecc->bytes);
1704
1705 ret = qcom_nandc_read_cw_raw(mtd, chip, cw_data_buf,
1706 cw_oob_buf, page, cw);
1707 if (ret)
1708 return ret;
1709
1710 /*
1711 * make sure it isn't an erased page reported
1712 * as not-erased by HW because of a few bitflips
1713 */
1714 ret = nand_check_erased_ecc_chunk(cw_data_buf, data_size,
1715 cw_oob_buf + host->bbm_size,
1716 oob_size, NULL,
1717 0, ecc->strength);
1718 if (ret < 0) {
1719 mtd->ecc_stats.failed++;
1720 } else {
1721 mtd->ecc_stats.corrected += ret;
1722 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1723 }
1724 }
1725
1726 return max_bitflips;
1727}
1728
1729/*
Archit Tanejac76b78d2016-02-03 14:29:50 +05301730 * reads back status registers set by the controller to notify page read
1731 * errors. this is equivalent to what 'ecc->correct()' would do.
1732 */
1733static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301734 u8 *oob_buf, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301735{
1736 struct nand_chip *chip = &host->chip;
1737 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1738 struct mtd_info *mtd = nand_to_mtd(chip);
1739 struct nand_ecc_ctrl *ecc = &chip->ecc;
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301740 unsigned int max_bitflips = 0, uncorrectable_cws = 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301741 struct read_stats *buf;
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301742 bool flash_op_err = false, erased;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301743 int i;
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301744 u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301745
1746 buf = (struct read_stats *)nandc->reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301747 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301748
1749 for (i = 0; i < ecc->steps; i++, buf++) {
1750 u32 flash, buffer, erased_cw;
1751 int data_len, oob_len;
1752
1753 if (i == (ecc->steps - 1)) {
1754 data_len = ecc->size - ((ecc->steps - 1) << 2);
1755 oob_len = ecc->steps << 2;
1756 } else {
1757 data_len = host->cw_data;
1758 oob_len = 0;
1759 }
1760
1761 flash = le32_to_cpu(buf->flash);
1762 buffer = le32_to_cpu(buf->buffer);
1763 erased_cw = le32_to_cpu(buf->erased_cw);
1764
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301765 /*
1766 * Check ECC failure for each codeword. ECC failure can
1767 * happen in either of the following conditions
1768 * 1. If number of bitflips are greater than ECC engine
1769 * capability.
1770 * 2. If this codeword contains all 0xff for which erased
1771 * codeword detection check will be done.
1772 */
1773 if ((flash & FS_OP_ERR) && (buffer & BS_UNCORRECTABLE_BIT)) {
Abhishek Sahu2f610382018-06-20 12:57:35 +05301774 /*
1775 * For BCH ECC, ignore erased codeword errors, if
1776 * ERASED_CW bits are set.
1777 */
Archit Tanejac76b78d2016-02-03 14:29:50 +05301778 if (host->bch_enabled) {
1779 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1780 true : false;
Abhishek Sahu2f610382018-06-20 12:57:35 +05301781 /*
1782 * For RS ECC, HW reports the erased CW by placing
1783 * special characters at certain offsets in the buffer.
1784 * These special characters will be valid only if
1785 * complete page is read i.e. data_buf is not NULL.
1786 */
1787 } else if (data_buf) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05301788 erased = erased_chunk_check_and_fixup(data_buf,
1789 data_len);
Abhishek Sahu2f610382018-06-20 12:57:35 +05301790 } else {
1791 erased = false;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301792 }
1793
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301794 if (!erased)
1795 uncorrectable_cws |= BIT(i);
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301796 /*
1797 * Check if MPU or any other operational error (timeout,
1798 * device failure, etc.) happened for this codeword and
1799 * make flash_op_err true. If flash_op_err is set, then
1800 * EIO will be returned for page read.
1801 */
1802 } else if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1803 flash_op_err = true;
1804 /*
1805 * No ECC or operational errors happened. Check the number of
1806 * bits corrected and update the ecc_stats.corrected.
1807 */
Archit Tanejac76b78d2016-02-03 14:29:50 +05301808 } else {
1809 unsigned int stat;
1810
1811 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1812 mtd->ecc_stats.corrected += stat;
1813 max_bitflips = max(max_bitflips, stat);
1814 }
1815
Abhishek Sahu2f610382018-06-20 12:57:35 +05301816 if (data_buf)
1817 data_buf += data_len;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301818 if (oob_buf)
1819 oob_buf += oob_len + ecc->bytes;
1820 }
1821
Abhishek Sahu8eab7212018-06-20 12:57:34 +05301822 if (flash_op_err)
1823 return -EIO;
1824
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301825 if (!uncorrectable_cws)
1826 return max_bitflips;
1827
1828 return check_for_erased_page(host, data_buf_start, oob_buf_start,
1829 uncorrectable_cws, page,
1830 max_bitflips);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301831}
1832
1833/*
1834 * helper to perform the actual page read operation, used by ecc->read_page(),
1835 * ecc->read_oob()
1836 */
1837static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301838 u8 *oob_buf, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301839{
1840 struct nand_chip *chip = &host->chip;
1841 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1842 struct nand_ecc_ctrl *ecc = &chip->ecc;
Abhishek Sahuadd0cfa2018-06-20 12:57:36 +05301843 u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301844 int i, ret;
1845
Abhishek Sahubde43302017-07-19 17:17:55 +05301846 config_nand_page_read(nandc);
1847
Archit Tanejac76b78d2016-02-03 14:29:50 +05301848 /* queue cmd descs for each codeword */
1849 for (i = 0; i < ecc->steps; i++) {
1850 int data_size, oob_size;
1851
1852 if (i == (ecc->steps - 1)) {
1853 data_size = ecc->size - ((ecc->steps - 1) << 2);
1854 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1855 host->spare_bytes;
1856 } else {
1857 data_size = host->cw_data;
1858 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1859 }
1860
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301861 if (nandc->props->is_bam) {
1862 if (data_buf && oob_buf) {
1863 nandc_set_read_loc(nandc, 0, 0, data_size, 0);
1864 nandc_set_read_loc(nandc, 1, data_size,
1865 oob_size, 1);
1866 } else if (data_buf) {
1867 nandc_set_read_loc(nandc, 0, 0, data_size, 1);
1868 } else {
1869 nandc_set_read_loc(nandc, 0, data_size,
1870 oob_size, 1);
1871 }
1872 }
1873
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301874 config_nand_cw_read(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301875
1876 if (data_buf)
1877 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301878 data_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301879
1880 /*
1881 * when ecc is enabled, the controller doesn't read the real
1882 * or dummy bad block markers in each chunk. To maintain a
1883 * consistent layout across RAW and ECC reads, we just
1884 * leave the real/dummy BBM offsets empty (i.e, filled with
1885 * 0xffs)
1886 */
1887 if (oob_buf) {
1888 int j;
1889
1890 for (j = 0; j < host->bbm_size; j++)
1891 *oob_buf++ = 0xff;
1892
1893 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301894 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301895 }
1896
1897 if (data_buf)
1898 data_buf += data_size;
1899 if (oob_buf)
1900 oob_buf += oob_size;
1901 }
1902
1903 ret = submit_descs(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301904 free_descs(nandc);
1905
Abhishek Sahuadd0cfa2018-06-20 12:57:36 +05301906 if (ret) {
1907 dev_err(nandc->dev, "failure to read page/oob\n");
1908 return ret;
1909 }
1910
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301911 return parse_read_errors(host, data_buf_start, oob_buf_start, page);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301912}
1913
1914/*
1915 * a helper that copies the last step/codeword of a page (containing free oob)
1916 * into our local buffer
1917 */
1918static int copy_last_cw(struct qcom_nand_host *host, int page)
1919{
1920 struct nand_chip *chip = &host->chip;
1921 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1922 struct nand_ecc_ctrl *ecc = &chip->ecc;
1923 int size;
1924 int ret;
1925
1926 clear_read_regs(nandc);
1927
1928 size = host->use_ecc ? host->cw_data : host->cw_size;
1929
1930 /* prepare a clean read buffer */
1931 memset(nandc->data_buffer, 0xff, size);
1932
1933 set_address(host, host->cw_size * (ecc->steps - 1), page);
1934 update_rw_regs(host, 1, true);
1935
Abhishek Sahu5bc36b22018-06-20 12:57:39 +05301936 config_nand_single_cw_page_read(nandc, host->use_ecc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301937
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301938 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301939
1940 ret = submit_descs(nandc);
1941 if (ret)
1942 dev_err(nandc->dev, "failed to copy last codeword\n");
1943
1944 free_descs(nandc);
1945
1946 return ret;
1947}
1948
1949/* implements ecc->read_page() */
Boris Brezillonb9761682018-09-06 14:05:20 +02001950static int qcom_nandc_read_page(struct nand_chip *chip, uint8_t *buf,
1951 int oob_required, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301952{
1953 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1954 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1955 u8 *data_buf, *oob_buf = NULL;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301956
Boris Brezillon25f815f2017-11-30 18:01:30 +01001957 nand_read_page_op(chip, page, 0, NULL, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301958 data_buf = buf;
1959 oob_buf = oob_required ? chip->oob_poi : NULL;
1960
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301961 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301962
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05301963 return read_page_ecc(host, data_buf, oob_buf, page);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301964}
1965
1966/* implements ecc->read_page_raw() */
Boris Brezillonb9761682018-09-06 14:05:20 +02001967static int qcom_nandc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
Archit Tanejac76b78d2016-02-03 14:29:50 +05301968 int oob_required, int page)
1969{
Boris Brezillonb9761682018-09-06 14:05:20 +02001970 struct mtd_info *mtd = nand_to_mtd(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301971 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301972 struct nand_ecc_ctrl *ecc = &chip->ecc;
Abhishek Sahu85632c12018-06-20 12:57:40 +05301973 int cw, ret;
1974 u8 *data_buf = buf, *oob_buf = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301975
Abhishek Sahu85632c12018-06-20 12:57:40 +05301976 for (cw = 0; cw < ecc->steps; cw++) {
1977 ret = qcom_nandc_read_cw_raw(mtd, chip, data_buf, oob_buf,
1978 page, cw);
1979 if (ret)
1980 return ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301981
Abhishek Sahu85632c12018-06-20 12:57:40 +05301982 data_buf += host->cw_data;
1983 oob_buf += ecc->bytes;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301984 }
1985
Abhishek Sahu85632c12018-06-20 12:57:40 +05301986 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301987}
1988
1989/* implements ecc->read_oob() */
Boris Brezillonb9761682018-09-06 14:05:20 +02001990static int qcom_nandc_read_oob(struct nand_chip *chip, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301991{
1992 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1993 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1994 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301995
1996 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301997 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301998
1999 host->use_ecc = true;
2000 set_address(host, 0, page);
2001 update_rw_regs(host, ecc->steps, true);
2002
Abhishek Sahu9f43dee2018-07-03 17:36:03 +05302003 return read_page_ecc(host, NULL, chip->oob_poi, page);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302004}
2005
2006/* implements ecc->write_page() */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02002007static int qcom_nandc_write_page(struct nand_chip *chip, const uint8_t *buf,
2008 int oob_required, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302009{
2010 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2011 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2012 struct nand_ecc_ctrl *ecc = &chip->ecc;
2013 u8 *data_buf, *oob_buf;
2014 int i, ret;
2015
Boris Brezillon25f815f2017-11-30 18:01:30 +01002016 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
2017
Archit Tanejac76b78d2016-02-03 14:29:50 +05302018 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302019 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302020
2021 data_buf = (u8 *)buf;
2022 oob_buf = chip->oob_poi;
2023
2024 host->use_ecc = true;
2025 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302026 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302027
2028 for (i = 0; i < ecc->steps; i++) {
2029 int data_size, oob_size;
2030
2031 if (i == (ecc->steps - 1)) {
2032 data_size = ecc->size - ((ecc->steps - 1) << 2);
2033 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
2034 host->spare_bytes;
2035 } else {
2036 data_size = host->cw_data;
2037 oob_size = ecc->bytes;
2038 }
2039
Archit Tanejac76b78d2016-02-03 14:29:50 +05302040
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302041 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
2042 i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302043
2044 /*
2045 * when ECC is enabled, we don't really need to write anything
2046 * to oob for the first n - 1 codewords since these oob regions
2047 * just contain ECC bytes that's written by the controller
2048 * itself. For the last codeword, we skip the bbm positions and
2049 * write to the free oob area.
2050 */
2051 if (i == (ecc->steps - 1)) {
2052 oob_buf += host->bbm_size;
2053
2054 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302055 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302056 }
2057
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302058 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302059
2060 data_buf += data_size;
2061 oob_buf += oob_size;
2062 }
2063
2064 ret = submit_descs(nandc);
2065 if (ret)
2066 dev_err(nandc->dev, "failure to write page\n");
2067
2068 free_descs(nandc);
2069
Boris Brezillon25f815f2017-11-30 18:01:30 +01002070 if (!ret)
2071 ret = nand_prog_page_end_op(chip);
2072
Archit Tanejac76b78d2016-02-03 14:29:50 +05302073 return ret;
2074}
2075
2076/* implements ecc->write_page_raw() */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02002077static int qcom_nandc_write_page_raw(struct nand_chip *chip,
2078 const uint8_t *buf, int oob_required,
2079 int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302080{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02002081 struct mtd_info *mtd = nand_to_mtd(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302082 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 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02002156static int qcom_nandc_write_oob(struct nand_chip *chip, int page)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302157{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02002158 struct mtd_info *mtd = nand_to_mtd(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302159 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
Boris Brezillonc17556f2018-09-06 14:05:25 +02002198static int qcom_nandc_block_bad(struct nand_chip *chip, loff_t ofs)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302199{
Boris Brezillonc17556f2018-09-06 14:05:25 +02002200 struct mtd_info *mtd = nand_to_mtd(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302201 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
Boris Brezillonc17556f2018-09-06 14:05:25 +02002236static int qcom_nandc_block_markbad(struct nand_chip *chip, loff_t ofs)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302237{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302238 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2239 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2240 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002241 int page, ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302242
2243 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302244 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302245
2246 /*
2247 * to mark the BBM as bad, we flash the entire last codeword with 0s.
2248 * we don't care about the rest of the content in the codeword since
2249 * we aren't going to use this block again
2250 */
2251 memset(nandc->data_buffer, 0x00, host->cw_size);
2252
2253 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2254
2255 /* prepare write */
2256 host->use_ecc = false;
2257 set_address(host, host->cw_size * (ecc->steps - 1), page);
2258 update_rw_regs(host, 1, false);
2259
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302260 config_nand_page_write(nandc);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302261 write_data_dma(nandc, FLASH_BUF_ACC,
2262 nandc->data_buffer, host->cw_size, 0);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302263 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302264
2265 ret = submit_descs(nandc);
2266
2267 free_descs(nandc);
2268
2269 if (ret) {
2270 dev_err(nandc->dev, "failure to update BBM\n");
2271 return -EIO;
2272 }
2273
Boris Brezillon97d90da2017-11-30 18:01:29 +01002274 return nand_prog_page_end_op(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302275}
2276
2277/*
2278 * the three functions below implement chip->read_byte(), chip->read_buf()
2279 * and chip->write_buf() respectively. these aren't used for
2280 * reading/writing page data, they are used for smaller data like reading
2281 * id, status etc
2282 */
Boris Brezillon7e534322018-09-06 14:05:22 +02002283static uint8_t qcom_nandc_read_byte(struct nand_chip *chip)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302284{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302285 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2286 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2287 u8 *buf = nandc->data_buffer;
2288 u8 ret = 0x0;
2289
2290 if (host->last_command == NAND_CMD_STATUS) {
2291 ret = host->status;
2292
2293 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2294
2295 return ret;
2296 }
2297
2298 if (nandc->buf_start < nandc->buf_count)
2299 ret = buf[nandc->buf_start++];
2300
2301 return ret;
2302}
2303
Boris Brezillon7e534322018-09-06 14:05:22 +02002304static void qcom_nandc_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302305{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302306 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2307 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2308
2309 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
2310 nandc->buf_start += real_len;
2311}
2312
Boris Brezillonc0739d82018-09-06 14:05:23 +02002313static void qcom_nandc_write_buf(struct nand_chip *chip, const uint8_t *buf,
Archit Tanejac76b78d2016-02-03 14:29:50 +05302314 int len)
2315{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302316 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2317 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2318
2319 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
2320
2321 nandc->buf_start += real_len;
2322}
2323
2324/* we support only one external chip for now */
Boris Brezillon758b56f2018-09-06 14:05:24 +02002325static void qcom_nandc_select_chip(struct nand_chip *chip, int chipnr)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302326{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302327 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2328
2329 if (chipnr <= 0)
2330 return;
2331
2332 dev_warn(nandc->dev, "invalid chip select\n");
2333}
2334
2335/*
2336 * NAND controller page layout info
2337 *
2338 * Layout with ECC enabled:
2339 *
2340 * |----------------------| |---------------------------------|
2341 * | xx.......yy| | *********xx.......yy|
2342 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
2343 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
2344 * | xx.......yy| | *********xx.......yy|
2345 * |----------------------| |---------------------------------|
2346 * codeword 1,2..n-1 codeword n
2347 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
2348 *
2349 * n = Number of codewords in the page
2350 * . = ECC bytes
2351 * * = Spare/free bytes
2352 * x = Unused byte(s)
2353 * y = Reserved byte(s)
2354 *
2355 * 2K page: n = 4, spare = 16 bytes
2356 * 4K page: n = 8, spare = 32 bytes
2357 * 8K page: n = 16, spare = 64 bytes
2358 *
2359 * the qcom nand controller operates at a sub page/codeword level. each
2360 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2361 * the number of ECC bytes vary based on the ECC strength and the bus width.
2362 *
2363 * the first n - 1 codewords contains 516 bytes of user data, the remaining
2364 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2365 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2366 *
2367 * When we access a page with ECC enabled, the reserved bytes(s) are not
2368 * accessible at all. When reading, we fill up these unreadable positions
2369 * with 0xffs. When writing, the controller skips writing the inaccessible
2370 * bytes.
2371 *
2372 * Layout with ECC disabled:
2373 *
2374 * |------------------------------| |---------------------------------------|
2375 * | yy xx.......| | bb *********xx.......|
2376 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
2377 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
2378 * | yy xx.......| | bb *********xx.......|
2379 * |------------------------------| |---------------------------------------|
2380 * codeword 1,2..n-1 codeword n
2381 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
2382 *
2383 * n = Number of codewords in the page
2384 * . = ECC bytes
2385 * * = Spare/free bytes
2386 * x = Unused byte(s)
2387 * y = Dummy Bad Bock byte(s)
2388 * b = Real Bad Block byte(s)
2389 * size1/size2 = function of codeword size and 'n'
2390 *
2391 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2392 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2393 * Block Markers. In the last codeword, this position contains the real BBM
2394 *
2395 * In order to have a consistent layout between RAW and ECC modes, we assume
2396 * the following OOB layout arrangement:
2397 *
2398 * |-----------| |--------------------|
2399 * |yyxx.......| |bb*********xx.......|
2400 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
2401 * |yyxx.......| |bb*********xx.......|
2402 * |yyxx.......| |bb*********xx.......|
2403 * |-----------| |--------------------|
2404 * first n - 1 nth OOB region
2405 * OOB regions
2406 *
2407 * n = Number of codewords in the page
2408 * . = ECC bytes
2409 * * = FREE OOB bytes
2410 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2411 * x = Unused byte(s)
2412 * b = Real bad block byte(s) (inaccessible when ECC enabled)
2413 *
2414 * This layout is read as is when ECC is disabled. When ECC is enabled, the
2415 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2416 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
Boris Brezillon421e81c2016-03-18 17:54:27 +01002417 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2418 * the sum of the three).
Archit Tanejac76b78d2016-02-03 14:29:50 +05302419 */
Boris Brezillon421e81c2016-03-18 17:54:27 +01002420static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2421 struct mtd_oob_region *oobregion)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302422{
Boris Brezillon421e81c2016-03-18 17:54:27 +01002423 struct nand_chip *chip = mtd_to_nand(mtd);
2424 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302425 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302426
Boris Brezillon421e81c2016-03-18 17:54:27 +01002427 if (section > 1)
2428 return -ERANGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302429
Boris Brezillon421e81c2016-03-18 17:54:27 +01002430 if (!section) {
2431 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
2432 host->bbm_size;
2433 oobregion->offset = 0;
2434 } else {
2435 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
2436 oobregion->offset = mtd->oobsize - oobregion->length;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302437 }
2438
Boris Brezillon421e81c2016-03-18 17:54:27 +01002439 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302440}
2441
Boris Brezillon421e81c2016-03-18 17:54:27 +01002442static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
2443 struct mtd_oob_region *oobregion)
2444{
2445 struct nand_chip *chip = mtd_to_nand(mtd);
2446 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2447 struct nand_ecc_ctrl *ecc = &chip->ecc;
2448
2449 if (section)
2450 return -ERANGE;
2451
2452 oobregion->length = ecc->steps * 4;
2453 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
2454
2455 return 0;
2456}
2457
2458static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
2459 .ecc = qcom_nand_ooblayout_ecc,
2460 .free = qcom_nand_ooblayout_free,
2461};
2462
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302463static int
2464qcom_nandc_calc_ecc_bytes(int step_size, int strength)
2465{
2466 return strength == 4 ? 12 : 16;
2467}
2468NAND_ECC_CAPS_SINGLE(qcom_nandc_ecc_caps, qcom_nandc_calc_ecc_bytes,
2469 NANDC_STEP_SIZE, 4, 8);
2470
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002471static int qcom_nand_attach_chip(struct nand_chip *chip)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302472{
Archit Tanejac76b78d2016-02-03 14:29:50 +05302473 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002474 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302475 struct nand_ecc_ctrl *ecc = &chip->ecc;
2476 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302477 int cwperpage, bad_block_byte, ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302478 bool wide_bus;
2479 int ecc_mode = 1;
2480
Abhishek Sahu320bdb52018-06-20 12:57:31 +05302481 /* controller only supports 512 bytes data steps */
2482 ecc->size = NANDC_STEP_SIZE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302483 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
Abhishek Sahu7ddb9372018-06-20 12:57:32 +05302484 cwperpage = mtd->writesize / NANDC_STEP_SIZE;
2485
2486 /*
2487 * Each CW has 4 available OOB bytes which will be protected with ECC
2488 * so remaining bytes can be used for ECC.
2489 */
2490 ret = nand_ecc_choose_conf(chip, &qcom_nandc_ecc_caps,
2491 mtd->oobsize - (cwperpage * 4));
2492 if (ret) {
2493 dev_err(nandc->dev, "No valid ECC settings possible\n");
2494 return ret;
2495 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302496
2497 if (ecc->strength >= 8) {
2498 /* 8 bit ECC defaults to BCH ECC on all platforms */
2499 host->bch_enabled = true;
2500 ecc_mode = 1;
2501
2502 if (wide_bus) {
2503 host->ecc_bytes_hw = 14;
2504 host->spare_bytes = 0;
2505 host->bbm_size = 2;
2506 } else {
2507 host->ecc_bytes_hw = 13;
2508 host->spare_bytes = 2;
2509 host->bbm_size = 1;
2510 }
2511 } else {
2512 /*
2513 * if the controller supports BCH for 4 bit ECC, the controller
2514 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2515 * always 10 bytes
2516 */
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302517 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302518 /* BCH */
2519 host->bch_enabled = true;
2520 ecc_mode = 0;
2521
2522 if (wide_bus) {
2523 host->ecc_bytes_hw = 8;
2524 host->spare_bytes = 2;
2525 host->bbm_size = 2;
2526 } else {
2527 host->ecc_bytes_hw = 7;
2528 host->spare_bytes = 4;
2529 host->bbm_size = 1;
2530 }
2531 } else {
2532 /* RS */
2533 host->ecc_bytes_hw = 10;
2534
2535 if (wide_bus) {
2536 host->spare_bytes = 0;
2537 host->bbm_size = 2;
2538 } else {
2539 host->spare_bytes = 1;
2540 host->bbm_size = 1;
2541 }
2542 }
2543 }
2544
2545 /*
2546 * we consider ecc->bytes as the sum of all the non-data content in a
2547 * step. It gives us a clean representation of the oob area (even if
2548 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2549 * ECC and 12 bytes for 4 bit ECC
2550 */
2551 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
2552
2553 ecc->read_page = qcom_nandc_read_page;
2554 ecc->read_page_raw = qcom_nandc_read_page_raw;
2555 ecc->read_oob = qcom_nandc_read_oob;
2556 ecc->write_page = qcom_nandc_write_page;
2557 ecc->write_page_raw = qcom_nandc_write_page_raw;
2558 ecc->write_oob = qcom_nandc_write_oob;
2559
2560 ecc->mode = NAND_ECC_HW;
2561
Boris Brezillon421e81c2016-03-18 17:54:27 +01002562 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302563
Abhishek Sahucb80f112017-08-17 17:37:40 +05302564 nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
2565 cwperpage);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302566
2567 /*
2568 * DATA_UD_BYTES varies based on whether the read/write command protects
2569 * spare data with ECC too. We protect spare data by default, so we set
2570 * it to main + spare data, which are 512 and 4 bytes respectively.
2571 */
2572 host->cw_data = 516;
2573
2574 /*
2575 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2576 * for 8 bit ECC
2577 */
2578 host->cw_size = host->cw_data + ecc->bytes;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302579 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
2580
2581 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
2582 | host->cw_data << UD_SIZE_BYTES
2583 | 0 << DISABLE_STATUS_AFTER_WRITE
2584 | 5 << NUM_ADDR_CYCLES
2585 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
2586 | 0 << STATUS_BFR_READ
2587 | 1 << SET_RD_MODE_AFTER_STATUS
2588 | host->spare_bytes << SPARE_SIZE_BYTES;
2589
2590 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
2591 | 0 << CS_ACTIVE_BSY
2592 | bad_block_byte << BAD_BLOCK_BYTE_NUM
2593 | 0 << BAD_BLOCK_IN_SPARE_AREA
2594 | 2 << WR_RD_BSY_GAP
2595 | wide_bus << WIDE_FLASH
2596 | host->bch_enabled << ENABLE_BCH_ECC;
2597
2598 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
2599 | host->cw_size << UD_SIZE_BYTES
2600 | 5 << NUM_ADDR_CYCLES
2601 | 0 << SPARE_SIZE_BYTES;
2602
2603 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
2604 | 0 << CS_ACTIVE_BSY
2605 | 17 << BAD_BLOCK_BYTE_NUM
2606 | 1 << BAD_BLOCK_IN_SPARE_AREA
2607 | 2 << WR_RD_BSY_GAP
2608 | wide_bus << WIDE_FLASH
2609 | 1 << DEV0_CFG1_ECC_DISABLE;
2610
Abhishek Sahu10777de2017-08-03 17:56:39 +02002611 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
Archit Tanejac76b78d2016-02-03 14:29:50 +05302612 | 0 << ECC_SW_RESET
2613 | host->cw_data << ECC_NUM_DATA_BYTES
2614 | 1 << ECC_FORCE_CLK_OPEN
2615 | ecc_mode << ECC_MODE
2616 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
2617
2618 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
2619
2620 host->clrflashstatus = FS_READY_BSY_N;
2621 host->clrreadstatus = 0xc0;
Abhishek Sahua86b9c42017-08-17 17:37:44 +05302622 nandc->regs->erased_cw_detect_cfg_clr =
2623 cpu_to_le32(CLR_ERASED_PAGE_DET);
2624 nandc->regs->erased_cw_detect_cfg_set =
2625 cpu_to_le32(SET_ERASED_PAGE_DET);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302626
2627 dev_dbg(nandc->dev,
2628 "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",
2629 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
2630 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
2631 cwperpage);
2632
2633 return 0;
2634}
2635
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002636static const struct nand_controller_ops qcom_nandc_ops = {
2637 .attach_chip = qcom_nand_attach_chip,
2638};
2639
Archit Tanejac76b78d2016-02-03 14:29:50 +05302640static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
2641{
2642 int ret;
2643
2644 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
2645 if (ret) {
2646 dev_err(nandc->dev, "failed to set DMA mask\n");
2647 return ret;
2648 }
2649
2650 /*
2651 * we use the internal buffer for reading ONFI params, reading small
2652 * data like ID and status, and preforming read-copy-write operations
2653 * when writing to a codeword partially. 532 is the maximum possible
2654 * size of a codeword for our nand controller
2655 */
2656 nandc->buf_size = 532;
2657
2658 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
2659 GFP_KERNEL);
2660 if (!nandc->data_buffer)
2661 return -ENOMEM;
2662
2663 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
2664 GFP_KERNEL);
2665 if (!nandc->regs)
2666 return -ENOMEM;
2667
Kees Cooka86854d2018-06-12 14:07:58 -07002668 nandc->reg_read_buf = devm_kcalloc(nandc->dev,
2669 MAX_REG_RD, sizeof(*nandc->reg_read_buf),
Archit Tanejac76b78d2016-02-03 14:29:50 +05302670 GFP_KERNEL);
2671 if (!nandc->reg_read_buf)
2672 return -ENOMEM;
2673
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302674 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302675 nandc->reg_read_dma =
2676 dma_map_single(nandc->dev, nandc->reg_read_buf,
2677 MAX_REG_RD *
2678 sizeof(*nandc->reg_read_buf),
2679 DMA_FROM_DEVICE);
2680 if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
2681 dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
2682 return -EIO;
2683 }
2684
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302685 nandc->tx_chan = dma_request_slave_channel(nandc->dev, "tx");
2686 if (!nandc->tx_chan) {
2687 dev_err(nandc->dev, "failed to request tx channel\n");
2688 return -ENODEV;
2689 }
2690
2691 nandc->rx_chan = dma_request_slave_channel(nandc->dev, "rx");
2692 if (!nandc->rx_chan) {
2693 dev_err(nandc->dev, "failed to request rx channel\n");
2694 return -ENODEV;
2695 }
2696
2697 nandc->cmd_chan = dma_request_slave_channel(nandc->dev, "cmd");
2698 if (!nandc->cmd_chan) {
2699 dev_err(nandc->dev, "failed to request cmd channel\n");
2700 return -ENODEV;
2701 }
Abhishek Sahucb80f112017-08-17 17:37:40 +05302702
2703 /*
2704 * Initially allocate BAM transaction to read ONFI param page.
2705 * After detecting all the devices, this BAM transaction will
2706 * be freed and the next BAM tranasction will be allocated with
2707 * maximum codeword size
2708 */
2709 nandc->max_cwperpage = 1;
2710 nandc->bam_txn = alloc_bam_transaction(nandc);
2711 if (!nandc->bam_txn) {
2712 dev_err(nandc->dev,
2713 "failed to allocate bam transaction\n");
2714 return -ENOMEM;
2715 }
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302716 } else {
2717 nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
2718 if (!nandc->chan) {
2719 dev_err(nandc->dev,
2720 "failed to request slave channel\n");
2721 return -ENODEV;
2722 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302723 }
2724
2725 INIT_LIST_HEAD(&nandc->desc_list);
2726 INIT_LIST_HEAD(&nandc->host_list);
2727
Miquel Raynal7da45132018-07-17 09:08:02 +02002728 nand_controller_init(&nandc->controller);
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002729 nandc->controller.ops = &qcom_nandc_ops;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302730
2731 return 0;
2732}
2733
2734static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2735{
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302736 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302737 if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
2738 dma_unmap_single(nandc->dev, nandc->reg_read_dma,
2739 MAX_REG_RD *
2740 sizeof(*nandc->reg_read_buf),
2741 DMA_FROM_DEVICE);
2742
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302743 if (nandc->tx_chan)
2744 dma_release_channel(nandc->tx_chan);
2745
2746 if (nandc->rx_chan)
2747 dma_release_channel(nandc->rx_chan);
2748
2749 if (nandc->cmd_chan)
2750 dma_release_channel(nandc->cmd_chan);
2751 } else {
2752 if (nandc->chan)
2753 dma_release_channel(nandc->chan);
2754 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302755}
2756
2757/* one time setup of a few nand controller registers */
2758static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2759{
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302760 u32 nand_ctrl;
2761
Archit Tanejac76b78d2016-02-03 14:29:50 +05302762 /* kill onenand */
2763 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
Abhishek Sahucc409b92017-08-17 17:37:47 +05302764 nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD),
2765 NAND_DEV_CMD_VLD_VAL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302766
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302767 /* enable ADM or BAM DMA */
2768 if (nandc->props->is_bam) {
2769 nand_ctrl = nandc_read(nandc, NAND_CTRL);
2770 nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
2771 } else {
2772 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2773 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302774
2775 /* save the original values of these registers */
Abhishek Sahucc409b92017-08-17 17:37:47 +05302776 nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1));
Abhishek Sahud8a9b322017-08-11 17:09:16 +05302777 nandc->vld = NAND_DEV_CMD_VLD_VAL;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302778
2779 return 0;
2780}
2781
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002782static int qcom_nand_host_init_and_register(struct qcom_nand_controller *nandc,
2783 struct qcom_nand_host *host,
2784 struct device_node *dn)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302785{
2786 struct nand_chip *chip = &host->chip;
2787 struct mtd_info *mtd = nand_to_mtd(chip);
2788 struct device *dev = nandc->dev;
2789 int ret;
2790
2791 ret = of_property_read_u32(dn, "reg", &host->cs);
2792 if (ret) {
2793 dev_err(dev, "can't get chip-select\n");
2794 return -ENXIO;
2795 }
2796
2797 nand_set_flash_node(chip, dn);
2798 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
Fabio Estevam069f0532018-01-05 18:02:55 -02002799 if (!mtd->name)
2800 return -ENOMEM;
2801
Archit Tanejac76b78d2016-02-03 14:29:50 +05302802 mtd->owner = THIS_MODULE;
2803 mtd->dev.parent = dev;
2804
2805 chip->cmdfunc = qcom_nandc_command;
2806 chip->select_chip = qcom_nandc_select_chip;
2807 chip->read_byte = qcom_nandc_read_byte;
2808 chip->read_buf = qcom_nandc_read_buf;
2809 chip->write_buf = qcom_nandc_write_buf;
Miquel Raynalb9587582018-03-19 14:47:19 +01002810 chip->set_features = nand_get_set_features_notsupp;
2811 chip->get_features = nand_get_set_features_notsupp;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302812
2813 /*
2814 * the bad block marker is readable only when we read the last codeword
2815 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2816 * helpers don't allow us to read BB from a nand chip with ECC
2817 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2818 * and block_markbad helpers until we permanently switch to using
2819 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2820 */
2821 chip->block_bad = qcom_nandc_block_bad;
2822 chip->block_markbad = qcom_nandc_block_markbad;
2823
2824 chip->controller = &nandc->controller;
2825 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
2826 NAND_SKIP_BBTSCAN;
2827
2828 /* set up initial status value */
2829 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2830
Boris Brezillon00ad3782018-09-06 14:05:14 +02002831 ret = nand_scan(chip, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302832 if (ret)
2833 return ret;
2834
Abhishek Sahu89f51272017-07-19 17:17:58 +05302835 ret = mtd_device_register(mtd, NULL, 0);
2836 if (ret)
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002837 nand_cleanup(chip);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302838
2839 return ret;
2840}
2841
2842static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2843{
2844 struct device *dev = nandc->dev;
2845 struct device_node *dn = dev->of_node, *child;
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002846 struct qcom_nand_host *host;
Abhishek Sahu89f51272017-07-19 17:17:58 +05302847 int ret;
2848
Abhishek Sahucb80f112017-08-17 17:37:40 +05302849 if (nandc->props->is_bam) {
2850 free_bam_transaction(nandc);
2851 nandc->bam_txn = alloc_bam_transaction(nandc);
2852 if (!nandc->bam_txn) {
2853 dev_err(nandc->dev,
2854 "failed to allocate bam transaction\n");
2855 return -ENOMEM;
2856 }
2857 }
2858
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002859 for_each_available_child_of_node(dn, child) {
2860 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2861 if (!host) {
2862 of_node_put(child);
2863 return -ENOMEM;
Abhishek Sahu89f51272017-07-19 17:17:58 +05302864 }
Miquel Raynal6a3cec62018-07-20 17:15:22 +02002865
2866 ret = qcom_nand_host_init_and_register(nandc, host, child);
2867 if (ret) {
2868 devm_kfree(dev, host);
2869 continue;
2870 }
2871
2872 list_add_tail(&host->node, &nandc->host_list);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302873 }
2874
2875 if (list_empty(&nandc->host_list))
2876 return -ENODEV;
2877
2878 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302879}
2880
2881/* parse custom DT properties here */
2882static int qcom_nandc_parse_dt(struct platform_device *pdev)
2883{
2884 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2885 struct device_node *np = nandc->dev->of_node;
2886 int ret;
2887
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302888 if (!nandc->props->is_bam) {
2889 ret = of_property_read_u32(np, "qcom,cmd-crci",
2890 &nandc->cmd_crci);
2891 if (ret) {
2892 dev_err(nandc->dev, "command CRCI unspecified\n");
2893 return ret;
2894 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302895
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302896 ret = of_property_read_u32(np, "qcom,data-crci",
2897 &nandc->data_crci);
2898 if (ret) {
2899 dev_err(nandc->dev, "data CRCI unspecified\n");
2900 return ret;
2901 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302902 }
2903
2904 return 0;
2905}
2906
2907static int qcom_nandc_probe(struct platform_device *pdev)
2908{
2909 struct qcom_nand_controller *nandc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302910 const void *dev_data;
2911 struct device *dev = &pdev->dev;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302912 struct resource *res;
2913 int ret;
2914
2915 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2916 if (!nandc)
2917 return -ENOMEM;
2918
2919 platform_set_drvdata(pdev, nandc);
2920 nandc->dev = dev;
2921
2922 dev_data = of_device_get_match_data(dev);
2923 if (!dev_data) {
2924 dev_err(&pdev->dev, "failed to get device data\n");
2925 return -ENODEV;
2926 }
2927
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302928 nandc->props = dev_data;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302929
Archit Tanejac76b78d2016-02-03 14:29:50 +05302930 nandc->core_clk = devm_clk_get(dev, "core");
2931 if (IS_ERR(nandc->core_clk))
2932 return PTR_ERR(nandc->core_clk);
2933
2934 nandc->aon_clk = devm_clk_get(dev, "aon");
2935 if (IS_ERR(nandc->aon_clk))
2936 return PTR_ERR(nandc->aon_clk);
2937
2938 ret = qcom_nandc_parse_dt(pdev);
2939 if (ret)
2940 return ret;
2941
Arnd Bergmann7330fc52018-07-17 22:27:42 +02002942 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2943 nandc->base = devm_ioremap_resource(dev, res);
2944 if (IS_ERR(nandc->base))
2945 return PTR_ERR(nandc->base);
2946
2947 nandc->base_phys = res->start;
2948 nandc->base_dma = dma_map_resource(dev, res->start,
2949 resource_size(res),
2950 DMA_BIDIRECTIONAL, 0);
2951 if (!nandc->base_dma)
2952 return -ENXIO;
2953
Archit Tanejac76b78d2016-02-03 14:29:50 +05302954 ret = qcom_nandc_alloc(nandc);
2955 if (ret)
Arnd Bergmann7330fc52018-07-17 22:27:42 +02002956 goto err_nandc_alloc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302957
2958 ret = clk_prepare_enable(nandc->core_clk);
2959 if (ret)
2960 goto err_core_clk;
2961
2962 ret = clk_prepare_enable(nandc->aon_clk);
2963 if (ret)
2964 goto err_aon_clk;
2965
2966 ret = qcom_nandc_setup(nandc);
2967 if (ret)
2968 goto err_setup;
2969
Abhishek Sahu89f51272017-07-19 17:17:58 +05302970 ret = qcom_probe_nand_devices(nandc);
2971 if (ret)
2972 goto err_setup;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302973
2974 return 0;
2975
Archit Tanejac76b78d2016-02-03 14:29:50 +05302976err_setup:
2977 clk_disable_unprepare(nandc->aon_clk);
2978err_aon_clk:
2979 clk_disable_unprepare(nandc->core_clk);
2980err_core_clk:
2981 qcom_nandc_unalloc(nandc);
Arnd Bergmann7330fc52018-07-17 22:27:42 +02002982err_nandc_alloc:
2983 dma_unmap_resource(dev, res->start, resource_size(res),
2984 DMA_BIDIRECTIONAL, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302985
2986 return ret;
2987}
2988
2989static int qcom_nandc_remove(struct platform_device *pdev)
2990{
2991 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
Arnd Bergmann7330fc52018-07-17 22:27:42 +02002992 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302993 struct qcom_nand_host *host;
2994
2995 list_for_each_entry(host, &nandc->host_list, node)
Boris Brezillon59ac2762018-09-06 14:05:15 +02002996 nand_release(&host->chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302997
Arnd Bergmann7330fc52018-07-17 22:27:42 +02002998
Archit Tanejac76b78d2016-02-03 14:29:50 +05302999 qcom_nandc_unalloc(nandc);
3000
3001 clk_disable_unprepare(nandc->aon_clk);
3002 clk_disable_unprepare(nandc->core_clk);
3003
Arnd Bergmann7330fc52018-07-17 22:27:42 +02003004 dma_unmap_resource(&pdev->dev, nandc->base_dma, resource_size(res),
3005 DMA_BIDIRECTIONAL, 0);
3006
Archit Tanejac76b78d2016-02-03 14:29:50 +05303007 return 0;
3008}
3009
Abhishek Sahu58f1f222017-08-11 17:09:17 +05303010static const struct qcom_nandc_props ipq806x_nandc_props = {
3011 .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +05303012 .is_bam = false,
Abhishek Sahucc409b92017-08-17 17:37:47 +05303013 .dev_cmd_reg_start = 0x0,
Abhishek Sahu58f1f222017-08-11 17:09:17 +05303014};
Archit Tanejac76b78d2016-02-03 14:29:50 +05303015
Abhishek Sahua0637832017-08-17 17:37:53 +05303016static const struct qcom_nandc_props ipq4019_nandc_props = {
3017 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3018 .is_bam = true,
3019 .dev_cmd_reg_start = 0x0,
3020};
3021
Abhishek Sahudce84762017-08-17 17:37:54 +05303022static const struct qcom_nandc_props ipq8074_nandc_props = {
3023 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
3024 .is_bam = true,
3025 .dev_cmd_reg_start = 0x7000,
3026};
3027
Archit Tanejac76b78d2016-02-03 14:29:50 +05303028/*
3029 * data will hold a struct pointer containing more differences once we support
3030 * more controller variants
3031 */
3032static const struct of_device_id qcom_nandc_of_match[] = {
Abhishek Sahu58f1f222017-08-11 17:09:17 +05303033 {
3034 .compatible = "qcom,ipq806x-nand",
3035 .data = &ipq806x_nandc_props,
Archit Tanejac76b78d2016-02-03 14:29:50 +05303036 },
Abhishek Sahua0637832017-08-17 17:37:53 +05303037 {
3038 .compatible = "qcom,ipq4019-nand",
3039 .data = &ipq4019_nandc_props,
3040 },
Abhishek Sahudce84762017-08-17 17:37:54 +05303041 {
3042 .compatible = "qcom,ipq8074-nand",
3043 .data = &ipq8074_nandc_props,
3044 },
Archit Tanejac76b78d2016-02-03 14:29:50 +05303045 {}
3046};
3047MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
3048
3049static struct platform_driver qcom_nandc_driver = {
3050 .driver = {
3051 .name = "qcom-nandc",
3052 .of_match_table = qcom_nandc_of_match,
3053 },
3054 .probe = qcom_nandc_probe,
3055 .remove = qcom_nandc_remove,
3056};
3057module_platform_driver(qcom_nandc_driver);
3058
3059MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
3060MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
3061MODULE_LICENSE("GPL v2");