blob: 3d1d506d3d18bd3c9a217789970a388f00e0c9ad [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 Sahu8c4cdce2017-09-25 13:21:25 +0530203#define QPIC_PER_CW_CMD_ELEMENTS 32
Abhishek Sahucb80f112017-08-17 17:37:40 +0530204#define QPIC_PER_CW_CMD_SGL 32
205#define QPIC_PER_CW_DATA_SGL 8
206
207/*
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530208 * Flags used in DMA descriptor preparation helper functions
209 * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma)
210 */
211/* Don't set the EOT in current tx BAM sgl */
212#define NAND_BAM_NO_EOT BIT(0)
213/* Set the NWD flag in current BAM sgl */
214#define NAND_BAM_NWD BIT(1)
215/* Finish writing in the current BAM sgl and start writing in another BAM sgl */
216#define NAND_BAM_NEXT_SGL BIT(2)
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530217/*
218 * Erased codeword status is being used two times in single transfer so this
219 * flag will determine the current value of erased codeword status register
220 */
221#define NAND_ERASED_CW_SET BIT(4)
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530222
223/*
Abhishek Sahucb80f112017-08-17 17:37:40 +0530224 * This data type corresponds to the BAM transaction which will be used for all
225 * NAND transfers.
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530226 * @bam_ce - the array of BAM command elements
Abhishek Sahucb80f112017-08-17 17:37:40 +0530227 * @cmd_sgl - sgl for NAND BAM command pipe
228 * @data_sgl - sgl for NAND BAM consumer/producer pipe
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530229 * @bam_ce_pos - the index in bam_ce which is available for next sgl
230 * @bam_ce_start - the index in bam_ce which marks the start position ce
231 * for current sgl. It will be used for size calculation
232 * for current sgl
Abhishek Sahucb80f112017-08-17 17:37:40 +0530233 * @cmd_sgl_pos - current index in command sgl.
234 * @cmd_sgl_start - start index in command sgl.
235 * @tx_sgl_pos - current index in data sgl for tx.
236 * @tx_sgl_start - start index in data sgl for tx.
237 * @rx_sgl_pos - current index in data sgl for rx.
238 * @rx_sgl_start - start index in data sgl for rx.
239 */
240struct bam_transaction {
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530241 struct bam_cmd_element *bam_ce;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530242 struct scatterlist *cmd_sgl;
243 struct scatterlist *data_sgl;
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530244 u32 bam_ce_pos;
245 u32 bam_ce_start;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530246 u32 cmd_sgl_pos;
247 u32 cmd_sgl_start;
248 u32 tx_sgl_pos;
249 u32 tx_sgl_start;
250 u32 rx_sgl_pos;
251 u32 rx_sgl_start;
252};
253
Abhishek Sahu381dd242017-08-17 17:37:41 +0530254/*
255 * This data type corresponds to the nand dma descriptor
256 * @list - list for desc_info
257 * @dir - DMA transfer direction
258 * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by
259 * ADM
260 * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM
261 * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM
262 * @dma_desc - low level DMA engine descriptor
263 */
Archit Tanejac76b78d2016-02-03 14:29:50 +0530264struct desc_info {
265 struct list_head node;
266
267 enum dma_data_direction dir;
Abhishek Sahu381dd242017-08-17 17:37:41 +0530268 union {
269 struct scatterlist adm_sgl;
270 struct {
271 struct scatterlist *bam_sgl;
272 int sgl_cnt;
273 };
274 };
Archit Tanejac76b78d2016-02-03 14:29:50 +0530275 struct dma_async_tx_descriptor *dma_desc;
276};
277
278/*
279 * holds the current register values that we want to write. acts as a contiguous
280 * chunk of memory which we use to write the controller registers through DMA.
281 */
282struct nandc_regs {
283 __le32 cmd;
284 __le32 addr0;
285 __le32 addr1;
286 __le32 chip_sel;
287 __le32 exec;
288
289 __le32 cfg0;
290 __le32 cfg1;
291 __le32 ecc_bch_cfg;
292
293 __le32 clrflashstatus;
294 __le32 clrreadstatus;
295
296 __le32 cmd1;
297 __le32 vld;
298
299 __le32 orig_cmd1;
300 __le32 orig_vld;
301
302 __le32 ecc_buf_cfg;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530303 __le32 read_location0;
304 __le32 read_location1;
305 __le32 read_location2;
306 __le32 read_location3;
307
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530308 __le32 erased_cw_detect_cfg_clr;
309 __le32 erased_cw_detect_cfg_set;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530310};
311
312/*
313 * NAND controller data struct
314 *
315 * @controller: base controller structure
316 * @host_list: list containing all the chips attached to the
317 * controller
318 * @dev: parent device
319 * @base: MMIO base
320 * @base_dma: physical base address of controller registers
321 * @core_clk: controller clock
322 * @aon_clk: another controller clock
323 *
324 * @chan: dma channel
325 * @cmd_crci: ADM DMA CRCI for command flow control
326 * @data_crci: ADM DMA CRCI for data flow control
327 * @desc_list: DMA descriptor list (list of desc_infos)
328 *
329 * @data_buffer: our local DMA buffer for page read/writes,
330 * used when we can't use the buffer provided
331 * by upper layers directly
332 * @buf_size/count/start: markers for chip->read_buf/write_buf functions
333 * @reg_read_buf: local buffer for reading back registers via DMA
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530334 * @reg_read_dma: contains dma address for register read buffer
Archit Tanejac76b78d2016-02-03 14:29:50 +0530335 * @reg_read_pos: marker for data read in reg_read_buf
336 *
337 * @regs: a contiguous chunk of memory for DMA register
338 * writes. contains the register values to be
339 * written to controller
340 * @cmd1/vld: some fixed controller register values
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530341 * @props: properties of current NAND controller,
Archit Tanejac76b78d2016-02-03 14:29:50 +0530342 * initialized via DT match data
Abhishek Sahucb80f112017-08-17 17:37:40 +0530343 * @max_cwperpage: maximum QPIC codewords required. calculated
344 * from all connected NAND devices pagesize
Archit Tanejac76b78d2016-02-03 14:29:50 +0530345 */
346struct qcom_nand_controller {
347 struct nand_hw_control controller;
348 struct list_head host_list;
349
350 struct device *dev;
351
352 void __iomem *base;
353 dma_addr_t base_dma;
354
355 struct clk *core_clk;
356 struct clk *aon_clk;
357
Abhishek Sahu497d7d82017-08-11 17:09:19 +0530358 union {
359 /* will be used only by QPIC for BAM DMA */
360 struct {
361 struct dma_chan *tx_chan;
362 struct dma_chan *rx_chan;
363 struct dma_chan *cmd_chan;
364 };
365
366 /* will be used only by EBI2 for ADM DMA */
367 struct {
368 struct dma_chan *chan;
369 unsigned int cmd_crci;
370 unsigned int data_crci;
371 };
372 };
373
Archit Tanejac76b78d2016-02-03 14:29:50 +0530374 struct list_head desc_list;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530375 struct bam_transaction *bam_txn;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530376
377 u8 *data_buffer;
378 int buf_size;
379 int buf_count;
380 int buf_start;
Abhishek Sahucb80f112017-08-17 17:37:40 +0530381 unsigned int max_cwperpage;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530382
383 __le32 *reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530384 dma_addr_t reg_read_dma;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530385 int reg_read_pos;
386
387 struct nandc_regs *regs;
388
389 u32 cmd1, vld;
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530390 const struct qcom_nandc_props *props;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530391};
392
393/*
394 * NAND chip structure
395 *
396 * @chip: base NAND chip structure
397 * @node: list node to add itself to host_list in
398 * qcom_nand_controller
399 *
400 * @cs: chip select value for this chip
401 * @cw_size: the number of bytes in a single step/codeword
402 * of a page, consisting of all data, ecc, spare
403 * and reserved bytes
404 * @cw_data: the number of bytes within a codeword protected
405 * by ECC
406 * @use_ecc: request the controller to use ECC for the
407 * upcoming read/write
408 * @bch_enabled: flag to tell whether BCH ECC mode is used
409 * @ecc_bytes_hw: ECC bytes used by controller hardware for this
410 * chip
411 * @status: value to be returned if NAND_CMD_STATUS command
412 * is executed
413 * @last_command: keeps track of last command on this chip. used
414 * for reading correct status
415 *
416 * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
417 * ecc/non-ecc mode for the current nand flash
418 * device
419 */
420struct qcom_nand_host {
421 struct nand_chip chip;
422 struct list_head node;
423
424 int cs;
425 int cw_size;
426 int cw_data;
427 bool use_ecc;
428 bool bch_enabled;
429 int ecc_bytes_hw;
430 int spare_bytes;
431 int bbm_size;
432 u8 status;
433 int last_command;
434
435 u32 cfg0, cfg1;
436 u32 cfg0_raw, cfg1_raw;
437 u32 ecc_buf_cfg;
438 u32 ecc_bch_cfg;
439 u32 clrflashstatus;
440 u32 clrreadstatus;
441};
442
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530443/*
444 * This data type corresponds to the NAND controller properties which varies
445 * among different NAND controllers.
446 * @ecc_modes - ecc mode for NAND
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +0530447 * @is_bam - whether NAND controller is using BAM
Abhishek Sahucc409b92017-08-17 17:37:47 +0530448 * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530449 */
450struct qcom_nandc_props {
451 u32 ecc_modes;
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +0530452 bool is_bam;
Abhishek Sahucc409b92017-08-17 17:37:47 +0530453 u32 dev_cmd_reg_start;
Abhishek Sahu58f1f222017-08-11 17:09:17 +0530454};
455
Abhishek Sahucb80f112017-08-17 17:37:40 +0530456/* Frees the BAM transaction memory */
457static void free_bam_transaction(struct qcom_nand_controller *nandc)
458{
459 struct bam_transaction *bam_txn = nandc->bam_txn;
460
461 devm_kfree(nandc->dev, bam_txn);
462}
463
464/* Allocates and Initializes the BAM transaction */
465static struct bam_transaction *
466alloc_bam_transaction(struct qcom_nand_controller *nandc)
467{
468 struct bam_transaction *bam_txn;
469 size_t bam_txn_size;
470 unsigned int num_cw = nandc->max_cwperpage;
471 void *bam_txn_buf;
472
473 bam_txn_size =
474 sizeof(*bam_txn) + num_cw *
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530475 ((sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS) +
476 (sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) +
Abhishek Sahucb80f112017-08-17 17:37:40 +0530477 (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL));
478
479 bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL);
480 if (!bam_txn_buf)
481 return NULL;
482
483 bam_txn = bam_txn_buf;
484 bam_txn_buf += sizeof(*bam_txn);
485
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530486 bam_txn->bam_ce = bam_txn_buf;
487 bam_txn_buf +=
488 sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS * num_cw;
489
Abhishek Sahucb80f112017-08-17 17:37:40 +0530490 bam_txn->cmd_sgl = bam_txn_buf;
491 bam_txn_buf +=
492 sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw;
493
494 bam_txn->data_sgl = bam_txn_buf;
495
496 return bam_txn;
497}
498
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530499/* Clears the BAM transaction indexes */
500static void clear_bam_transaction(struct qcom_nand_controller *nandc)
501{
502 struct bam_transaction *bam_txn = nandc->bam_txn;
503
504 if (!nandc->props->is_bam)
505 return;
506
Abhishek Sahu8c4cdce2017-09-25 13:21:25 +0530507 bam_txn->bam_ce_pos = 0;
508 bam_txn->bam_ce_start = 0;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530509 bam_txn->cmd_sgl_pos = 0;
510 bam_txn->cmd_sgl_start = 0;
511 bam_txn->tx_sgl_pos = 0;
512 bam_txn->tx_sgl_start = 0;
513 bam_txn->rx_sgl_pos = 0;
514 bam_txn->rx_sgl_start = 0;
515
516 sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage *
517 QPIC_PER_CW_CMD_SGL);
518 sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage *
519 QPIC_PER_CW_DATA_SGL);
520}
521
Archit Tanejac76b78d2016-02-03 14:29:50 +0530522static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip)
523{
524 return container_of(chip, struct qcom_nand_host, chip);
525}
526
527static inline struct qcom_nand_controller *
528get_qcom_nand_controller(struct nand_chip *chip)
529{
530 return container_of(chip->controller, struct qcom_nand_controller,
531 controller);
532}
533
534static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset)
535{
536 return ioread32(nandc->base + offset);
537}
538
539static inline void nandc_write(struct qcom_nand_controller *nandc, int offset,
540 u32 val)
541{
542 iowrite32(val, nandc->base + offset);
543}
544
Abhishek Sahu6192ff72017-08-17 17:37:39 +0530545static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc,
546 bool is_cpu)
547{
548 if (!nandc->props->is_bam)
549 return;
550
551 if (is_cpu)
552 dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma,
553 MAX_REG_RD *
554 sizeof(*nandc->reg_read_buf),
555 DMA_FROM_DEVICE);
556 else
557 dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma,
558 MAX_REG_RD *
559 sizeof(*nandc->reg_read_buf),
560 DMA_FROM_DEVICE);
561}
562
Archit Tanejac76b78d2016-02-03 14:29:50 +0530563static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset)
564{
565 switch (offset) {
566 case NAND_FLASH_CMD:
567 return &regs->cmd;
568 case NAND_ADDR0:
569 return &regs->addr0;
570 case NAND_ADDR1:
571 return &regs->addr1;
572 case NAND_FLASH_CHIP_SELECT:
573 return &regs->chip_sel;
574 case NAND_EXEC_CMD:
575 return &regs->exec;
576 case NAND_FLASH_STATUS:
577 return &regs->clrflashstatus;
578 case NAND_DEV0_CFG0:
579 return &regs->cfg0;
580 case NAND_DEV0_CFG1:
581 return &regs->cfg1;
582 case NAND_DEV0_ECC_CFG:
583 return &regs->ecc_bch_cfg;
584 case NAND_READ_STATUS:
585 return &regs->clrreadstatus;
586 case NAND_DEV_CMD1:
587 return &regs->cmd1;
588 case NAND_DEV_CMD1_RESTORE:
589 return &regs->orig_cmd1;
590 case NAND_DEV_CMD_VLD:
591 return &regs->vld;
592 case NAND_DEV_CMD_VLD_RESTORE:
593 return &regs->orig_vld;
594 case NAND_EBI2_ECC_BUF_CFG:
595 return &regs->ecc_buf_cfg;
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530596 case NAND_READ_LOCATION_0:
597 return &regs->read_location0;
598 case NAND_READ_LOCATION_1:
599 return &regs->read_location1;
600 case NAND_READ_LOCATION_2:
601 return &regs->read_location2;
602 case NAND_READ_LOCATION_3:
603 return &regs->read_location3;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530604 default:
605 return NULL;
606 }
607}
608
609static void nandc_set_reg(struct qcom_nand_controller *nandc, int offset,
610 u32 val)
611{
612 struct nandc_regs *regs = nandc->regs;
613 __le32 *reg;
614
615 reg = offset_to_nandc_reg(regs, offset);
616
617 if (reg)
618 *reg = cpu_to_le32(val);
619}
620
621/* helper to configure address register values */
622static void set_address(struct qcom_nand_host *host, u16 column, int page)
623{
624 struct nand_chip *chip = &host->chip;
625 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
626
627 if (chip->options & NAND_BUSWIDTH_16)
628 column >>= 1;
629
630 nandc_set_reg(nandc, NAND_ADDR0, page << 16 | column);
631 nandc_set_reg(nandc, NAND_ADDR1, page >> 16 & 0xff);
632}
633
634/*
635 * update_rw_regs: set up read/write register values, these will be
636 * written to the NAND controller registers via DMA
637 *
638 * @num_cw: number of steps for the read/write operation
639 * @read: read or write operation
640 */
641static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read)
642{
643 struct nand_chip *chip = &host->chip;
644 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
645 u32 cmd, cfg0, cfg1, ecc_bch_cfg;
646
647 if (read) {
648 if (host->use_ecc)
649 cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
650 else
651 cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
652 } else {
653 cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
654 }
655
656 if (host->use_ecc) {
657 cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) |
658 (num_cw - 1) << CW_PER_PAGE;
659
660 cfg1 = host->cfg1;
661 ecc_bch_cfg = host->ecc_bch_cfg;
662 } else {
663 cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) |
664 (num_cw - 1) << CW_PER_PAGE;
665
666 cfg1 = host->cfg1_raw;
667 ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
668 }
669
670 nandc_set_reg(nandc, NAND_FLASH_CMD, cmd);
671 nandc_set_reg(nandc, NAND_DEV0_CFG0, cfg0);
672 nandc_set_reg(nandc, NAND_DEV0_CFG1, cfg1);
673 nandc_set_reg(nandc, NAND_DEV0_ECC_CFG, ecc_bch_cfg);
674 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg);
675 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
676 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
677 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530678
679 if (read)
680 nandc_set_read_loc(nandc, 0, 0, host->use_ecc ?
681 host->cw_data : host->cw_size, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530682}
683
Abhishek Sahu381dd242017-08-17 17:37:41 +0530684/*
685 * Maps the scatter gather list for DMA transfer and forms the DMA descriptor
686 * for BAM. This descriptor will be added in the NAND DMA descriptor queue
687 * which will be submitted to DMA engine.
688 */
689static int prepare_bam_async_desc(struct qcom_nand_controller *nandc,
690 struct dma_chan *chan,
691 unsigned long flags)
692{
693 struct desc_info *desc;
694 struct scatterlist *sgl;
695 unsigned int sgl_cnt;
696 int ret;
697 struct bam_transaction *bam_txn = nandc->bam_txn;
698 enum dma_transfer_direction dir_eng;
699 struct dma_async_tx_descriptor *dma_desc;
700
701 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
702 if (!desc)
703 return -ENOMEM;
704
705 if (chan == nandc->cmd_chan) {
706 sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start];
707 sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start;
708 bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos;
709 dir_eng = DMA_MEM_TO_DEV;
710 desc->dir = DMA_TO_DEVICE;
711 } else if (chan == nandc->tx_chan) {
712 sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start];
713 sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start;
714 bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos;
715 dir_eng = DMA_MEM_TO_DEV;
716 desc->dir = DMA_TO_DEVICE;
717 } else {
718 sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start];
719 sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start;
720 bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos;
721 dir_eng = DMA_DEV_TO_MEM;
722 desc->dir = DMA_FROM_DEVICE;
723 }
724
725 sg_mark_end(sgl + sgl_cnt - 1);
726 ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
727 if (ret == 0) {
728 dev_err(nandc->dev, "failure in mapping desc\n");
729 kfree(desc);
730 return -ENOMEM;
731 }
732
733 desc->sgl_cnt = sgl_cnt;
734 desc->bam_sgl = sgl;
735
736 dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng,
737 flags);
738
739 if (!dma_desc) {
740 dev_err(nandc->dev, "failure in prep desc\n");
741 dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir);
742 kfree(desc);
743 return -EINVAL;
744 }
745
746 desc->dma_desc = dma_desc;
747
748 list_add_tail(&desc->node, &nandc->desc_list);
749
750 return 0;
751}
752
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530753/*
754 * Prepares the data descriptor for BAM DMA which will be used for NAND
755 * data reads and writes.
756 */
757static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read,
758 const void *vaddr,
759 int size, unsigned int flags)
760{
761 int ret;
762 struct bam_transaction *bam_txn = nandc->bam_txn;
763
764 if (read) {
765 sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos],
766 vaddr, size);
767 bam_txn->rx_sgl_pos++;
768 } else {
769 sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos],
770 vaddr, size);
771 bam_txn->tx_sgl_pos++;
772
773 /*
774 * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag
775 * is not set, form the DMA descriptor
776 */
777 if (!(flags & NAND_BAM_NO_EOT)) {
778 ret = prepare_bam_async_desc(nandc, nandc->tx_chan,
779 DMA_PREP_INTERRUPT);
780 if (ret)
781 return ret;
782 }
783 }
784
785 return 0;
786}
787
Abhishek Sahu381dd242017-08-17 17:37:41 +0530788static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read,
789 int reg_off, const void *vaddr, int size,
790 bool flow_control)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530791{
792 struct desc_info *desc;
793 struct dma_async_tx_descriptor *dma_desc;
794 struct scatterlist *sgl;
795 struct dma_slave_config slave_conf;
796 enum dma_transfer_direction dir_eng;
797 int ret;
798
799 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
800 if (!desc)
801 return -ENOMEM;
802
Abhishek Sahu381dd242017-08-17 17:37:41 +0530803 sgl = &desc->adm_sgl;
Archit Tanejac76b78d2016-02-03 14:29:50 +0530804
805 sg_init_one(sgl, vaddr, size);
806
807 if (read) {
808 dir_eng = DMA_DEV_TO_MEM;
809 desc->dir = DMA_FROM_DEVICE;
810 } else {
811 dir_eng = DMA_MEM_TO_DEV;
812 desc->dir = DMA_TO_DEVICE;
813 }
814
815 ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir);
816 if (ret == 0) {
817 ret = -ENOMEM;
818 goto err;
819 }
820
821 memset(&slave_conf, 0x00, sizeof(slave_conf));
822
823 slave_conf.device_fc = flow_control;
824 if (read) {
825 slave_conf.src_maxburst = 16;
826 slave_conf.src_addr = nandc->base_dma + reg_off;
827 slave_conf.slave_id = nandc->data_crci;
828 } else {
829 slave_conf.dst_maxburst = 16;
830 slave_conf.dst_addr = nandc->base_dma + reg_off;
831 slave_conf.slave_id = nandc->cmd_crci;
832 }
833
834 ret = dmaengine_slave_config(nandc->chan, &slave_conf);
835 if (ret) {
836 dev_err(nandc->dev, "failed to configure dma channel\n");
837 goto err;
838 }
839
840 dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0);
841 if (!dma_desc) {
842 dev_err(nandc->dev, "failed to prepare desc\n");
843 ret = -EINVAL;
844 goto err;
845 }
846
847 desc->dma_desc = dma_desc;
848
849 list_add_tail(&desc->node, &nandc->desc_list);
850
851 return 0;
852err:
853 kfree(desc);
854
855 return ret;
856}
857
858/*
859 * read_reg_dma: prepares a descriptor to read a given number of
860 * contiguous registers to the reg_read_buf pointer
861 *
862 * @first: offset of the first register in the contiguous block
863 * @num_regs: number of registers to read
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530864 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530865 */
866static int read_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530867 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530868{
869 bool flow_control = false;
870 void *vaddr;
871 int size;
872
873 if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
874 flow_control = true;
875
Abhishek Sahucc409b92017-08-17 17:37:47 +0530876 if (first == NAND_DEV_CMD_VLD || first == NAND_DEV_CMD1)
877 first = dev_cmd_reg_addr(nandc, first);
878
Archit Tanejac76b78d2016-02-03 14:29:50 +0530879 size = num_regs * sizeof(u32);
880 vaddr = nandc->reg_read_buf + nandc->reg_read_pos;
881 nandc->reg_read_pos += num_regs;
882
Abhishek Sahu381dd242017-08-17 17:37:41 +0530883 return prep_adm_dma_desc(nandc, true, first, vaddr, size, flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530884}
885
886/*
887 * write_reg_dma: prepares a descriptor to write a given number of
888 * contiguous registers
889 *
890 * @first: offset of the first register in the contiguous block
891 * @num_regs: number of registers to write
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530892 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530893 */
894static int write_reg_dma(struct qcom_nand_controller *nandc, int first,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530895 int num_regs, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530896{
897 bool flow_control = false;
898 struct nandc_regs *regs = nandc->regs;
899 void *vaddr;
900 int size;
901
902 vaddr = offset_to_nandc_reg(regs, first);
903
904 if (first == NAND_FLASH_CMD)
905 flow_control = true;
906
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530907 if (first == NAND_ERASED_CW_DETECT_CFG) {
908 if (flags & NAND_ERASED_CW_SET)
909 vaddr = &regs->erased_cw_detect_cfg_set;
910 else
911 vaddr = &regs->erased_cw_detect_cfg_clr;
912 }
913
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530914 if (first == NAND_EXEC_CMD)
915 flags |= NAND_BAM_NWD;
916
Abhishek Sahucc409b92017-08-17 17:37:47 +0530917 if (first == NAND_DEV_CMD1_RESTORE || first == NAND_DEV_CMD1)
918 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD1);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530919
Abhishek Sahucc409b92017-08-17 17:37:47 +0530920 if (first == NAND_DEV_CMD_VLD_RESTORE || first == NAND_DEV_CMD_VLD)
921 first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530922
923 size = num_regs * sizeof(u32);
924
Abhishek Sahu381dd242017-08-17 17:37:41 +0530925 return prep_adm_dma_desc(nandc, false, first, vaddr, size,
926 flow_control);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530927}
928
929/*
930 * read_data_dma: prepares a DMA descriptor to transfer data from the
931 * controller's internal buffer to the buffer 'vaddr'
932 *
933 * @reg_off: offset within the controller's data buffer
934 * @vaddr: virtual address of the buffer we want to write to
935 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530936 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530937 */
938static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530939 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530940{
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530941 if (nandc->props->is_bam)
942 return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags);
943
Abhishek Sahu381dd242017-08-17 17:37:41 +0530944 return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530945}
946
947/*
948 * write_data_dma: prepares a DMA descriptor to transfer data from
949 * 'vaddr' to the controller's internal buffer
950 *
951 * @reg_off: offset within the controller's data buffer
952 * @vaddr: virtual address of the buffer we want to read from
953 * @size: DMA transaction size in bytes
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530954 * @flags: flags to control DMA descriptor preparation
Archit Tanejac76b78d2016-02-03 14:29:50 +0530955 */
956static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off,
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530957 const u8 *vaddr, int size, unsigned int flags)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530958{
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +0530959 if (nandc->props->is_bam)
960 return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags);
961
Abhishek Sahu381dd242017-08-17 17:37:41 +0530962 return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530963}
964
965/*
Abhishek Sahubde43302017-07-19 17:17:55 +0530966 * Helper to prepare DMA descriptors for configuring registers
967 * before reading a NAND page.
Archit Tanejac76b78d2016-02-03 14:29:50 +0530968 */
Abhishek Sahubde43302017-07-19 17:17:55 +0530969static void config_nand_page_read(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +0530970{
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530971 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
972 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
973 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0);
Abhishek Sahua86b9c42017-08-17 17:37:44 +0530974 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0);
975 write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1,
976 NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
Abhishek Sahubde43302017-07-19 17:17:55 +0530977}
Archit Tanejac76b78d2016-02-03 14:29:50 +0530978
Abhishek Sahubde43302017-07-19 17:17:55 +0530979/*
980 * Helper to prepare DMA descriptors for configuring registers
981 * before reading each codeword in NAND page.
982 */
983static void config_nand_cw_read(struct qcom_nand_controller *nandc)
984{
Abhishek Sahu91af95c2017-08-17 17:37:43 +0530985 if (nandc->props->is_bam)
986 write_reg_dma(nandc, NAND_READ_LOCATION_0, 4,
987 NAND_BAM_NEXT_SGL);
988
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530989 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
990 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530991
Abhishek Sahu67e830a2017-08-17 17:37:42 +0530992 read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0);
993 read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1,
994 NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +0530995}
996
997/*
Abhishek Sahubde43302017-07-19 17:17:55 +0530998 * Helper to prepare dma descriptors to configure registers needed for reading a
999 * single codeword in page
Archit Tanejac76b78d2016-02-03 14:29:50 +05301000 */
Abhishek Sahubde43302017-07-19 17:17:55 +05301001static void config_nand_single_cw_page_read(struct qcom_nand_controller *nandc)
1002{
1003 config_nand_page_read(nandc);
1004 config_nand_cw_read(nandc);
1005}
1006
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301007/*
1008 * Helper to prepare DMA descriptors used to configure registers needed for
1009 * before writing a NAND page.
1010 */
1011static void config_nand_page_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301012{
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301013 write_reg_dma(nandc, NAND_ADDR0, 2, 0);
1014 write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0);
1015 write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1,
1016 NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301017}
1018
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301019/*
1020 * Helper to prepare DMA descriptors for configuring registers
1021 * before writing each codeword in NAND page.
1022 */
1023static void config_nand_cw_write(struct qcom_nand_controller *nandc)
Archit Tanejac76b78d2016-02-03 14:29:50 +05301024{
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301025 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1026 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301027
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301028 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301029
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301030 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1031 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301032}
1033
1034/*
1035 * the following functions are used within chip->cmdfunc() to perform different
1036 * NAND_CMD_* commands
1037 */
1038
1039/* sets up descriptors for NAND_CMD_PARAM */
1040static int nandc_param(struct qcom_nand_host *host)
1041{
1042 struct nand_chip *chip = &host->chip;
1043 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1044
1045 /*
1046 * NAND_CMD_PARAM is called before we know much about the FLASH chip
1047 * in use. we configure the controller to perform a raw read of 512
1048 * bytes to read onfi params
1049 */
1050 nandc_set_reg(nandc, NAND_FLASH_CMD, PAGE_READ | PAGE_ACC | LAST_PAGE);
1051 nandc_set_reg(nandc, NAND_ADDR0, 0);
1052 nandc_set_reg(nandc, NAND_ADDR1, 0);
1053 nandc_set_reg(nandc, NAND_DEV0_CFG0, 0 << CW_PER_PAGE
1054 | 512 << UD_SIZE_BYTES
1055 | 5 << NUM_ADDR_CYCLES
1056 | 0 << SPARE_SIZE_BYTES);
1057 nandc_set_reg(nandc, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES
1058 | 0 << CS_ACTIVE_BSY
1059 | 17 << BAD_BLOCK_BYTE_NUM
1060 | 1 << BAD_BLOCK_IN_SPARE_AREA
1061 | 2 << WR_RD_BSY_GAP
1062 | 0 << WIDE_FLASH
1063 | 1 << DEV0_CFG1_ECC_DISABLE);
1064 nandc_set_reg(nandc, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE);
1065
1066 /* configure CMD1 and VLD for ONFI param probing */
1067 nandc_set_reg(nandc, NAND_DEV_CMD_VLD,
Abhishek Sahud8a9b322017-08-11 17:09:16 +05301068 (nandc->vld & ~READ_START_VLD));
Archit Tanejac76b78d2016-02-03 14:29:50 +05301069 nandc_set_reg(nandc, NAND_DEV_CMD1,
1070 (nandc->cmd1 & ~(0xFF << READ_ADDR))
1071 | NAND_CMD_PARAM << READ_ADDR);
1072
1073 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1074
1075 nandc_set_reg(nandc, NAND_DEV_CMD1_RESTORE, nandc->cmd1);
1076 nandc_set_reg(nandc, NAND_DEV_CMD_VLD_RESTORE, nandc->vld);
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301077 nandc_set_read_loc(nandc, 0, 0, 512, 1);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301078
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301079 write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0);
1080 write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301081
1082 nandc->buf_count = 512;
1083 memset(nandc->data_buffer, 0xff, nandc->buf_count);
1084
Abhishek Sahubde43302017-07-19 17:17:55 +05301085 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301086
1087 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301088 nandc->buf_count, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301089
1090 /* restore CMD1 and VLD regs */
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301091 write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0);
1092 write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301093
1094 return 0;
1095}
1096
1097/* sets up descriptors for NAND_CMD_ERASE1 */
1098static int erase_block(struct qcom_nand_host *host, int page_addr)
1099{
1100 struct nand_chip *chip = &host->chip;
1101 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1102
1103 nandc_set_reg(nandc, NAND_FLASH_CMD,
1104 BLOCK_ERASE | PAGE_ACC | LAST_PAGE);
1105 nandc_set_reg(nandc, NAND_ADDR0, page_addr);
1106 nandc_set_reg(nandc, NAND_ADDR1, 0);
1107 nandc_set_reg(nandc, NAND_DEV0_CFG0,
1108 host->cfg0_raw & ~(7 << CW_PER_PAGE));
1109 nandc_set_reg(nandc, NAND_DEV0_CFG1, host->cfg1_raw);
1110 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1111 nandc_set_reg(nandc, NAND_FLASH_STATUS, host->clrflashstatus);
1112 nandc_set_reg(nandc, NAND_READ_STATUS, host->clrreadstatus);
1113
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301114 write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
1115 write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
1116 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301117
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301118 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301119
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301120 write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0);
1121 write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301122
1123 return 0;
1124}
1125
1126/* sets up descriptors for NAND_CMD_READID */
1127static int read_id(struct qcom_nand_host *host, int column)
1128{
1129 struct nand_chip *chip = &host->chip;
1130 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1131
1132 if (column == -1)
1133 return 0;
1134
1135 nandc_set_reg(nandc, NAND_FLASH_CMD, FETCH_ID);
1136 nandc_set_reg(nandc, NAND_ADDR0, column);
1137 nandc_set_reg(nandc, NAND_ADDR1, 0);
Abhishek Sahu9d43f912017-08-17 17:37:45 +05301138 nandc_set_reg(nandc, NAND_FLASH_CHIP_SELECT,
1139 nandc->props->is_bam ? 0 : DM_EN);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301140 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1141
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301142 write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL);
1143 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301144
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301145 read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301146
1147 return 0;
1148}
1149
1150/* sets up descriptors for NAND_CMD_RESET */
1151static int reset(struct qcom_nand_host *host)
1152{
1153 struct nand_chip *chip = &host->chip;
1154 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1155
1156 nandc_set_reg(nandc, NAND_FLASH_CMD, RESET_DEVICE);
1157 nandc_set_reg(nandc, NAND_EXEC_CMD, 1);
1158
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301159 write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1160 write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301161
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301162 read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301163
1164 return 0;
1165}
1166
1167/* helpers to submit/free our list of dma descriptors */
1168static int submit_descs(struct qcom_nand_controller *nandc)
1169{
1170 struct desc_info *desc;
1171 dma_cookie_t cookie = 0;
Abhishek Sahu381dd242017-08-17 17:37:41 +05301172 struct bam_transaction *bam_txn = nandc->bam_txn;
1173 int r;
1174
1175 if (nandc->props->is_bam) {
1176 if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) {
1177 r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0);
1178 if (r)
1179 return r;
1180 }
1181
1182 if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) {
1183 r = prepare_bam_async_desc(nandc, nandc->tx_chan,
1184 DMA_PREP_INTERRUPT);
1185 if (r)
1186 return r;
1187 }
1188
1189 if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) {
1190 r = prepare_bam_async_desc(nandc, nandc->cmd_chan, 0);
1191 if (r)
1192 return r;
1193 }
1194 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301195
1196 list_for_each_entry(desc, &nandc->desc_list, node)
1197 cookie = dmaengine_submit(desc->dma_desc);
1198
Abhishek Sahu381dd242017-08-17 17:37:41 +05301199 if (nandc->props->is_bam) {
1200 dma_async_issue_pending(nandc->tx_chan);
1201 dma_async_issue_pending(nandc->rx_chan);
1202
1203 if (dma_sync_wait(nandc->cmd_chan, cookie) != DMA_COMPLETE)
1204 return -ETIMEDOUT;
1205 } else {
1206 if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE)
1207 return -ETIMEDOUT;
1208 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05301209
1210 return 0;
1211}
1212
1213static void free_descs(struct qcom_nand_controller *nandc)
1214{
1215 struct desc_info *desc, *n;
1216
1217 list_for_each_entry_safe(desc, n, &nandc->desc_list, node) {
1218 list_del(&desc->node);
Abhishek Sahu381dd242017-08-17 17:37:41 +05301219
1220 if (nandc->props->is_bam)
1221 dma_unmap_sg(nandc->dev, desc->bam_sgl,
1222 desc->sgl_cnt, desc->dir);
1223 else
1224 dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1,
1225 desc->dir);
1226
Archit Tanejac76b78d2016-02-03 14:29:50 +05301227 kfree(desc);
1228 }
1229}
1230
1231/* reset the register read buffer for next NAND operation */
1232static void clear_read_regs(struct qcom_nand_controller *nandc)
1233{
1234 nandc->reg_read_pos = 0;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301235 nandc_read_buffer_sync(nandc, false);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301236}
1237
1238static void pre_command(struct qcom_nand_host *host, int command)
1239{
1240 struct nand_chip *chip = &host->chip;
1241 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1242
1243 nandc->buf_count = 0;
1244 nandc->buf_start = 0;
1245 host->use_ecc = false;
1246 host->last_command = command;
1247
1248 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301249
1250 if (command == NAND_CMD_RESET || command == NAND_CMD_READID ||
1251 command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1)
1252 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301253}
1254
1255/*
1256 * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
1257 * privately maintained status byte, this status byte can be read after
1258 * NAND_CMD_STATUS is called
1259 */
1260static void parse_erase_write_errors(struct qcom_nand_host *host, int command)
1261{
1262 struct nand_chip *chip = &host->chip;
1263 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1264 struct nand_ecc_ctrl *ecc = &chip->ecc;
1265 int num_cw;
1266 int i;
1267
1268 num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301269 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301270
1271 for (i = 0; i < num_cw; i++) {
1272 u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]);
1273
1274 if (flash_status & FS_MPU_ERR)
1275 host->status &= ~NAND_STATUS_WP;
1276
1277 if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
1278 (flash_status &
1279 FS_DEVICE_STS_ERR)))
1280 host->status |= NAND_STATUS_FAIL;
1281 }
1282}
1283
1284static void post_command(struct qcom_nand_host *host, int command)
1285{
1286 struct nand_chip *chip = &host->chip;
1287 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1288
1289 switch (command) {
1290 case NAND_CMD_READID:
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301291 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301292 memcpy(nandc->data_buffer, nandc->reg_read_buf,
1293 nandc->buf_count);
1294 break;
1295 case NAND_CMD_PAGEPROG:
1296 case NAND_CMD_ERASE1:
1297 parse_erase_write_errors(host, command);
1298 break;
1299 default:
1300 break;
1301 }
1302}
1303
1304/*
1305 * Implements chip->cmdfunc. It's only used for a limited set of commands.
1306 * The rest of the commands wouldn't be called by upper layers. For example,
1307 * NAND_CMD_READOOB would never be called because we have our own versions
1308 * of read_oob ops for nand_ecc_ctrl.
1309 */
1310static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
1311 int column, int page_addr)
1312{
1313 struct nand_chip *chip = mtd_to_nand(mtd);
1314 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1315 struct nand_ecc_ctrl *ecc = &chip->ecc;
1316 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1317 bool wait = false;
1318 int ret = 0;
1319
1320 pre_command(host, command);
1321
1322 switch (command) {
1323 case NAND_CMD_RESET:
1324 ret = reset(host);
1325 wait = true;
1326 break;
1327
1328 case NAND_CMD_READID:
1329 nandc->buf_count = 4;
1330 ret = read_id(host, column);
1331 wait = true;
1332 break;
1333
1334 case NAND_CMD_PARAM:
1335 ret = nandc_param(host);
1336 wait = true;
1337 break;
1338
1339 case NAND_CMD_ERASE1:
1340 ret = erase_block(host, page_addr);
1341 wait = true;
1342 break;
1343
1344 case NAND_CMD_READ0:
1345 /* we read the entire page for now */
1346 WARN_ON(column != 0);
1347
1348 host->use_ecc = true;
1349 set_address(host, 0, page_addr);
1350 update_rw_regs(host, ecc->steps, true);
1351 break;
1352
1353 case NAND_CMD_SEQIN:
1354 WARN_ON(column != 0);
1355 set_address(host, 0, page_addr);
1356 break;
1357
1358 case NAND_CMD_PAGEPROG:
1359 case NAND_CMD_STATUS:
1360 case NAND_CMD_NONE:
1361 default:
1362 break;
1363 }
1364
1365 if (ret) {
1366 dev_err(nandc->dev, "failure executing command %d\n",
1367 command);
1368 free_descs(nandc);
1369 return;
1370 }
1371
1372 if (wait) {
1373 ret = submit_descs(nandc);
1374 if (ret)
1375 dev_err(nandc->dev,
1376 "failure submitting descs for command %d\n",
1377 command);
1378 }
1379
1380 free_descs(nandc);
1381
1382 post_command(host, command);
1383}
1384
1385/*
1386 * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read
1387 * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS.
1388 *
1389 * when using RS ECC, the HW reports the same erros when reading an erased CW,
1390 * but it notifies that it is an erased CW by placing special characters at
1391 * certain offsets in the buffer.
1392 *
1393 * verify if the page is erased or not, and fix up the page for RS ECC by
1394 * replacing the special characters with 0xff.
1395 */
1396static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len)
1397{
1398 u8 empty1, empty2;
1399
1400 /*
1401 * an erased page flags an error in NAND_FLASH_STATUS, check if the page
1402 * is erased by looking for 0x54s at offsets 3 and 175 from the
1403 * beginning of each codeword
1404 */
1405
1406 empty1 = data_buf[3];
1407 empty2 = data_buf[175];
1408
1409 /*
1410 * if the erased codework markers, if they exist override them with
1411 * 0xffs
1412 */
1413 if ((empty1 == 0x54 && empty2 == 0xff) ||
1414 (empty1 == 0xff && empty2 == 0x54)) {
1415 data_buf[3] = 0xff;
1416 data_buf[175] = 0xff;
1417 }
1418
1419 /*
1420 * check if the entire chunk contains 0xffs or not. if it doesn't, then
1421 * restore the original values at the special offsets
1422 */
1423 if (memchr_inv(data_buf, 0xff, data_len)) {
1424 data_buf[3] = empty1;
1425 data_buf[175] = empty2;
1426
1427 return false;
1428 }
1429
1430 return true;
1431}
1432
1433struct read_stats {
1434 __le32 flash;
1435 __le32 buffer;
1436 __le32 erased_cw;
1437};
1438
1439/*
1440 * reads back status registers set by the controller to notify page read
1441 * errors. this is equivalent to what 'ecc->correct()' would do.
1442 */
1443static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf,
1444 u8 *oob_buf)
1445{
1446 struct nand_chip *chip = &host->chip;
1447 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1448 struct mtd_info *mtd = nand_to_mtd(chip);
1449 struct nand_ecc_ctrl *ecc = &chip->ecc;
1450 unsigned int max_bitflips = 0;
1451 struct read_stats *buf;
1452 int i;
1453
1454 buf = (struct read_stats *)nandc->reg_read_buf;
Abhishek Sahu6192ff72017-08-17 17:37:39 +05301455 nandc_read_buffer_sync(nandc, true);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301456
1457 for (i = 0; i < ecc->steps; i++, buf++) {
1458 u32 flash, buffer, erased_cw;
1459 int data_len, oob_len;
1460
1461 if (i == (ecc->steps - 1)) {
1462 data_len = ecc->size - ((ecc->steps - 1) << 2);
1463 oob_len = ecc->steps << 2;
1464 } else {
1465 data_len = host->cw_data;
1466 oob_len = 0;
1467 }
1468
1469 flash = le32_to_cpu(buf->flash);
1470 buffer = le32_to_cpu(buf->buffer);
1471 erased_cw = le32_to_cpu(buf->erased_cw);
1472
1473 if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
1474 bool erased;
1475
1476 /* ignore erased codeword errors */
1477 if (host->bch_enabled) {
1478 erased = (erased_cw & ERASED_CW) == ERASED_CW ?
1479 true : false;
1480 } else {
1481 erased = erased_chunk_check_and_fixup(data_buf,
1482 data_len);
1483 }
1484
1485 if (erased) {
1486 data_buf += data_len;
1487 if (oob_buf)
1488 oob_buf += oob_len + ecc->bytes;
1489 continue;
1490 }
1491
1492 if (buffer & BS_UNCORRECTABLE_BIT) {
1493 int ret, ecclen, extraooblen;
1494 void *eccbuf;
1495
1496 eccbuf = oob_buf ? oob_buf + oob_len : NULL;
1497 ecclen = oob_buf ? host->ecc_bytes_hw : 0;
1498 extraooblen = oob_buf ? oob_len : 0;
1499
1500 /*
1501 * make sure it isn't an erased page reported
1502 * as not-erased by HW because of a few bitflips
1503 */
1504 ret = nand_check_erased_ecc_chunk(data_buf,
1505 data_len, eccbuf, ecclen, oob_buf,
1506 extraooblen, ecc->strength);
1507 if (ret < 0) {
1508 mtd->ecc_stats.failed++;
1509 } else {
1510 mtd->ecc_stats.corrected += ret;
1511 max_bitflips =
1512 max_t(unsigned int, max_bitflips, ret);
1513 }
1514 }
1515 } else {
1516 unsigned int stat;
1517
1518 stat = buffer & BS_CORRECTABLE_ERR_MSK;
1519 mtd->ecc_stats.corrected += stat;
1520 max_bitflips = max(max_bitflips, stat);
1521 }
1522
1523 data_buf += data_len;
1524 if (oob_buf)
1525 oob_buf += oob_len + ecc->bytes;
1526 }
1527
1528 return max_bitflips;
1529}
1530
1531/*
1532 * helper to perform the actual page read operation, used by ecc->read_page(),
1533 * ecc->read_oob()
1534 */
1535static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf,
1536 u8 *oob_buf)
1537{
1538 struct nand_chip *chip = &host->chip;
1539 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1540 struct nand_ecc_ctrl *ecc = &chip->ecc;
1541 int i, ret;
1542
Abhishek Sahubde43302017-07-19 17:17:55 +05301543 config_nand_page_read(nandc);
1544
Archit Tanejac76b78d2016-02-03 14:29:50 +05301545 /* queue cmd descs for each codeword */
1546 for (i = 0; i < ecc->steps; i++) {
1547 int data_size, oob_size;
1548
1549 if (i == (ecc->steps - 1)) {
1550 data_size = ecc->size - ((ecc->steps - 1) << 2);
1551 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1552 host->spare_bytes;
1553 } else {
1554 data_size = host->cw_data;
1555 oob_size = host->ecc_bytes_hw + host->spare_bytes;
1556 }
1557
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301558 if (nandc->props->is_bam) {
1559 if (data_buf && oob_buf) {
1560 nandc_set_read_loc(nandc, 0, 0, data_size, 0);
1561 nandc_set_read_loc(nandc, 1, data_size,
1562 oob_size, 1);
1563 } else if (data_buf) {
1564 nandc_set_read_loc(nandc, 0, 0, data_size, 1);
1565 } else {
1566 nandc_set_read_loc(nandc, 0, data_size,
1567 oob_size, 1);
1568 }
1569 }
1570
Abhishek Sahubde43302017-07-19 17:17:55 +05301571 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301572
1573 if (data_buf)
1574 read_data_dma(nandc, FLASH_BUF_ACC, data_buf,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301575 data_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301576
1577 /*
1578 * when ecc is enabled, the controller doesn't read the real
1579 * or dummy bad block markers in each chunk. To maintain a
1580 * consistent layout across RAW and ECC reads, we just
1581 * leave the real/dummy BBM offsets empty (i.e, filled with
1582 * 0xffs)
1583 */
1584 if (oob_buf) {
1585 int j;
1586
1587 for (j = 0; j < host->bbm_size; j++)
1588 *oob_buf++ = 0xff;
1589
1590 read_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301591 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301592 }
1593
1594 if (data_buf)
1595 data_buf += data_size;
1596 if (oob_buf)
1597 oob_buf += oob_size;
1598 }
1599
1600 ret = submit_descs(nandc);
1601 if (ret)
1602 dev_err(nandc->dev, "failure to read page/oob\n");
1603
1604 free_descs(nandc);
1605
1606 return ret;
1607}
1608
1609/*
1610 * a helper that copies the last step/codeword of a page (containing free oob)
1611 * into our local buffer
1612 */
1613static int copy_last_cw(struct qcom_nand_host *host, int page)
1614{
1615 struct nand_chip *chip = &host->chip;
1616 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1617 struct nand_ecc_ctrl *ecc = &chip->ecc;
1618 int size;
1619 int ret;
1620
1621 clear_read_regs(nandc);
1622
1623 size = host->use_ecc ? host->cw_data : host->cw_size;
1624
1625 /* prepare a clean read buffer */
1626 memset(nandc->data_buffer, 0xff, size);
1627
1628 set_address(host, host->cw_size * (ecc->steps - 1), page);
1629 update_rw_regs(host, 1, true);
1630
Abhishek Sahubde43302017-07-19 17:17:55 +05301631 config_nand_single_cw_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301632
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301633 read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301634
1635 ret = submit_descs(nandc);
1636 if (ret)
1637 dev_err(nandc->dev, "failed to copy last codeword\n");
1638
1639 free_descs(nandc);
1640
1641 return ret;
1642}
1643
1644/* implements ecc->read_page() */
1645static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1646 uint8_t *buf, int oob_required, int page)
1647{
1648 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1649 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1650 u8 *data_buf, *oob_buf = NULL;
1651 int ret;
1652
1653 data_buf = buf;
1654 oob_buf = oob_required ? chip->oob_poi : NULL;
1655
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301656 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301657 ret = read_page_ecc(host, data_buf, oob_buf);
1658 if (ret) {
1659 dev_err(nandc->dev, "failure to read page\n");
1660 return ret;
1661 }
1662
1663 return parse_read_errors(host, data_buf, oob_buf);
1664}
1665
1666/* implements ecc->read_page_raw() */
1667static int qcom_nandc_read_page_raw(struct mtd_info *mtd,
1668 struct nand_chip *chip, uint8_t *buf,
1669 int oob_required, int page)
1670{
1671 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1672 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1673 u8 *data_buf, *oob_buf;
1674 struct nand_ecc_ctrl *ecc = &chip->ecc;
1675 int i, ret;
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301676 int read_loc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301677
1678 data_buf = buf;
1679 oob_buf = chip->oob_poi;
1680
1681 host->use_ecc = false;
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301682
1683 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301684 update_rw_regs(host, ecc->steps, true);
Abhishek Sahubde43302017-07-19 17:17:55 +05301685 config_nand_page_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301686
1687 for (i = 0; i < ecc->steps; i++) {
1688 int data_size1, data_size2, oob_size1, oob_size2;
1689 int reg_off = FLASH_BUF_ACC;
1690
1691 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1692 oob_size1 = host->bbm_size;
1693
1694 if (i == (ecc->steps - 1)) {
1695 data_size2 = ecc->size - data_size1 -
1696 ((ecc->steps - 1) << 2);
1697 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1698 host->spare_bytes;
1699 } else {
1700 data_size2 = host->cw_data - data_size1;
1701 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1702 }
1703
Abhishek Sahu91af95c2017-08-17 17:37:43 +05301704 if (nandc->props->is_bam) {
1705 read_loc = 0;
1706 nandc_set_read_loc(nandc, 0, read_loc, data_size1, 0);
1707 read_loc += data_size1;
1708
1709 nandc_set_read_loc(nandc, 1, read_loc, oob_size1, 0);
1710 read_loc += oob_size1;
1711
1712 nandc_set_read_loc(nandc, 2, read_loc, data_size2, 0);
1713 read_loc += data_size2;
1714
1715 nandc_set_read_loc(nandc, 3, read_loc, oob_size2, 1);
1716 }
1717
Abhishek Sahubde43302017-07-19 17:17:55 +05301718 config_nand_cw_read(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301719
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301720 read_data_dma(nandc, reg_off, data_buf, data_size1, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301721 reg_off += data_size1;
1722 data_buf += data_size1;
1723
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301724 read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301725 reg_off += oob_size1;
1726 oob_buf += oob_size1;
1727
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301728 read_data_dma(nandc, reg_off, data_buf, data_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301729 reg_off += data_size2;
1730 data_buf += data_size2;
1731
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301732 read_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301733 oob_buf += oob_size2;
1734 }
1735
1736 ret = submit_descs(nandc);
1737 if (ret)
1738 dev_err(nandc->dev, "failure to read raw page\n");
1739
1740 free_descs(nandc);
1741
1742 return 0;
1743}
1744
1745/* implements ecc->read_oob() */
1746static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1747 int page)
1748{
1749 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1750 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1751 struct nand_ecc_ctrl *ecc = &chip->ecc;
1752 int ret;
1753
1754 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301755 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301756
1757 host->use_ecc = true;
1758 set_address(host, 0, page);
1759 update_rw_regs(host, ecc->steps, true);
1760
1761 ret = read_page_ecc(host, NULL, chip->oob_poi);
1762 if (ret)
1763 dev_err(nandc->dev, "failure to read oob\n");
1764
1765 return ret;
1766}
1767
1768/* implements ecc->write_page() */
1769static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1770 const uint8_t *buf, int oob_required, int page)
1771{
1772 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1773 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1774 struct nand_ecc_ctrl *ecc = &chip->ecc;
1775 u8 *data_buf, *oob_buf;
1776 int i, ret;
1777
1778 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301779 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301780
1781 data_buf = (u8 *)buf;
1782 oob_buf = chip->oob_poi;
1783
1784 host->use_ecc = true;
1785 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301786 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301787
1788 for (i = 0; i < ecc->steps; i++) {
1789 int data_size, oob_size;
1790
1791 if (i == (ecc->steps - 1)) {
1792 data_size = ecc->size - ((ecc->steps - 1) << 2);
1793 oob_size = (ecc->steps << 2) + host->ecc_bytes_hw +
1794 host->spare_bytes;
1795 } else {
1796 data_size = host->cw_data;
1797 oob_size = ecc->bytes;
1798 }
1799
Archit Tanejac76b78d2016-02-03 14:29:50 +05301800
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301801 write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size,
1802 i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301803
1804 /*
1805 * when ECC is enabled, we don't really need to write anything
1806 * to oob for the first n - 1 codewords since these oob regions
1807 * just contain ECC bytes that's written by the controller
1808 * itself. For the last codeword, we skip the bbm positions and
1809 * write to the free oob area.
1810 */
1811 if (i == (ecc->steps - 1)) {
1812 oob_buf += host->bbm_size;
1813
1814 write_data_dma(nandc, FLASH_BUF_ACC + data_size,
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301815 oob_buf, oob_size, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301816 }
1817
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301818 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301819
1820 data_buf += data_size;
1821 oob_buf += oob_size;
1822 }
1823
1824 ret = submit_descs(nandc);
1825 if (ret)
1826 dev_err(nandc->dev, "failure to write page\n");
1827
1828 free_descs(nandc);
1829
1830 return ret;
1831}
1832
1833/* implements ecc->write_page_raw() */
1834static int qcom_nandc_write_page_raw(struct mtd_info *mtd,
1835 struct nand_chip *chip, const uint8_t *buf,
1836 int oob_required, int page)
1837{
1838 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1839 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1840 struct nand_ecc_ctrl *ecc = &chip->ecc;
1841 u8 *data_buf, *oob_buf;
1842 int i, ret;
1843
1844 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301845 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301846
1847 data_buf = (u8 *)buf;
1848 oob_buf = chip->oob_poi;
1849
1850 host->use_ecc = false;
1851 update_rw_regs(host, ecc->steps, false);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301852 config_nand_page_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301853
1854 for (i = 0; i < ecc->steps; i++) {
1855 int data_size1, data_size2, oob_size1, oob_size2;
1856 int reg_off = FLASH_BUF_ACC;
1857
1858 data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1);
1859 oob_size1 = host->bbm_size;
1860
1861 if (i == (ecc->steps - 1)) {
1862 data_size2 = ecc->size - data_size1 -
1863 ((ecc->steps - 1) << 2);
1864 oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw +
1865 host->spare_bytes;
1866 } else {
1867 data_size2 = host->cw_data - data_size1;
1868 oob_size2 = host->ecc_bytes_hw + host->spare_bytes;
1869 }
1870
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301871 write_data_dma(nandc, reg_off, data_buf, data_size1,
1872 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301873 reg_off += data_size1;
1874 data_buf += data_size1;
1875
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301876 write_data_dma(nandc, reg_off, oob_buf, oob_size1,
1877 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301878 reg_off += oob_size1;
1879 oob_buf += oob_size1;
1880
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301881 write_data_dma(nandc, reg_off, data_buf, data_size2,
1882 NAND_BAM_NO_EOT);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301883 reg_off += data_size2;
1884 data_buf += data_size2;
1885
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301886 write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301887 oob_buf += oob_size2;
1888
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301889 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301890 }
1891
1892 ret = submit_descs(nandc);
1893 if (ret)
1894 dev_err(nandc->dev, "failure to write raw page\n");
1895
1896 free_descs(nandc);
1897
1898 return ret;
1899}
1900
1901/*
1902 * implements ecc->write_oob()
1903 *
1904 * the NAND controller cannot write only data or only oob within a codeword,
1905 * since ecc is calculated for the combined codeword. we first copy the
1906 * entire contents for the last codeword(data + oob), replace the old oob
1907 * with the new one in chip->oob_poi, and then write the entire codeword.
1908 * this read-copy-write operation results in a slight performance loss.
1909 */
1910static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1911 int page)
1912{
1913 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1914 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1915 struct nand_ecc_ctrl *ecc = &chip->ecc;
1916 u8 *oob = chip->oob_poi;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301917 int data_size, oob_size;
1918 int ret, status = 0;
1919
1920 host->use_ecc = true;
1921
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301922 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301923 ret = copy_last_cw(host, page);
1924 if (ret)
1925 return ret;
1926
1927 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301928 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301929
1930 /* calculate the data and oob size for the last codeword/step */
1931 data_size = ecc->size - ((ecc->steps - 1) << 2);
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001932 oob_size = mtd->oobavail;
Archit Tanejac76b78d2016-02-03 14:29:50 +05301933
1934 /* override new oob content to last codeword */
Boris Brezillonaa02fcf2016-03-18 17:53:31 +01001935 mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob,
1936 0, mtd->oobavail);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301937
1938 set_address(host, host->cw_size * (ecc->steps - 1), page);
1939 update_rw_regs(host, 1, false);
1940
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301941 config_nand_page_write(nandc);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05301942 write_data_dma(nandc, FLASH_BUF_ACC,
1943 nandc->data_buffer, data_size + oob_size, 0);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05301944 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301945
1946 ret = submit_descs(nandc);
1947
1948 free_descs(nandc);
1949
1950 if (ret) {
1951 dev_err(nandc->dev, "failure to write oob\n");
1952 return -EIO;
1953 }
1954
1955 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1956
1957 status = chip->waitfunc(mtd, chip);
1958
1959 return status & NAND_STATUS_FAIL ? -EIO : 0;
1960}
1961
1962static int qcom_nandc_block_bad(struct mtd_info *mtd, loff_t ofs)
1963{
1964 struct nand_chip *chip = mtd_to_nand(mtd);
1965 struct qcom_nand_host *host = to_qcom_nand_host(chip);
1966 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
1967 struct nand_ecc_ctrl *ecc = &chip->ecc;
1968 int page, ret, bbpos, bad = 0;
1969 u32 flash_status;
1970
1971 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
1972
1973 /*
1974 * configure registers for a raw sub page read, the address is set to
1975 * the beginning of the last codeword, we don't care about reading ecc
1976 * portion of oob. we just want the first few bytes from this codeword
1977 * that contains the BBM
1978 */
1979 host->use_ecc = false;
1980
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05301981 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05301982 ret = copy_last_cw(host, page);
1983 if (ret)
1984 goto err;
1985
1986 flash_status = le32_to_cpu(nandc->reg_read_buf[0]);
1987
1988 if (flash_status & (FS_OP_ERR | FS_MPU_ERR)) {
1989 dev_warn(nandc->dev, "error when trying to read BBM\n");
1990 goto err;
1991 }
1992
1993 bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1);
1994
1995 bad = nandc->data_buffer[bbpos] != 0xff;
1996
1997 if (chip->options & NAND_BUSWIDTH_16)
1998 bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff);
1999err:
2000 return bad;
2001}
2002
2003static int qcom_nandc_block_markbad(struct mtd_info *mtd, loff_t ofs)
2004{
2005 struct nand_chip *chip = mtd_to_nand(mtd);
2006 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2007 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2008 struct nand_ecc_ctrl *ecc = &chip->ecc;
2009 int page, ret, status = 0;
2010
2011 clear_read_regs(nandc);
Abhishek Sahu4e2f6c52017-08-17 17:37:46 +05302012 clear_bam_transaction(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302013
2014 /*
2015 * to mark the BBM as bad, we flash the entire last codeword with 0s.
2016 * we don't care about the rest of the content in the codeword since
2017 * we aren't going to use this block again
2018 */
2019 memset(nandc->data_buffer, 0x00, host->cw_size);
2020
2021 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
2022
2023 /* prepare write */
2024 host->use_ecc = false;
2025 set_address(host, host->cw_size * (ecc->steps - 1), page);
2026 update_rw_regs(host, 1, false);
2027
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302028 config_nand_page_write(nandc);
Abhishek Sahu67e830a2017-08-17 17:37:42 +05302029 write_data_dma(nandc, FLASH_BUF_ACC,
2030 nandc->data_buffer, host->cw_size, 0);
Abhishek Sahu77cc5362017-07-19 17:17:56 +05302031 config_nand_cw_write(nandc);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302032
2033 ret = submit_descs(nandc);
2034
2035 free_descs(nandc);
2036
2037 if (ret) {
2038 dev_err(nandc->dev, "failure to update BBM\n");
2039 return -EIO;
2040 }
2041
2042 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2043
2044 status = chip->waitfunc(mtd, chip);
2045
2046 return status & NAND_STATUS_FAIL ? -EIO : 0;
2047}
2048
2049/*
2050 * the three functions below implement chip->read_byte(), chip->read_buf()
2051 * and chip->write_buf() respectively. these aren't used for
2052 * reading/writing page data, they are used for smaller data like reading
2053 * id, status etc
2054 */
2055static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
2056{
2057 struct nand_chip *chip = mtd_to_nand(mtd);
2058 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2059 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2060 u8 *buf = nandc->data_buffer;
2061 u8 ret = 0x0;
2062
2063 if (host->last_command == NAND_CMD_STATUS) {
2064 ret = host->status;
2065
2066 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2067
2068 return ret;
2069 }
2070
2071 if (nandc->buf_start < nandc->buf_count)
2072 ret = buf[nandc->buf_start++];
2073
2074 return ret;
2075}
2076
2077static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
2078{
2079 struct nand_chip *chip = mtd_to_nand(mtd);
2080 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2081 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2082
2083 memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len);
2084 nandc->buf_start += real_len;
2085}
2086
2087static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
2088 int len)
2089{
2090 struct nand_chip *chip = mtd_to_nand(mtd);
2091 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2092 int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start);
2093
2094 memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len);
2095
2096 nandc->buf_start += real_len;
2097}
2098
2099/* we support only one external chip for now */
2100static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
2101{
2102 struct nand_chip *chip = mtd_to_nand(mtd);
2103 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2104
2105 if (chipnr <= 0)
2106 return;
2107
2108 dev_warn(nandc->dev, "invalid chip select\n");
2109}
2110
2111/*
2112 * NAND controller page layout info
2113 *
2114 * Layout with ECC enabled:
2115 *
2116 * |----------------------| |---------------------------------|
2117 * | xx.......yy| | *********xx.......yy|
2118 * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy|
2119 * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy|
2120 * | xx.......yy| | *********xx.......yy|
2121 * |----------------------| |---------------------------------|
2122 * codeword 1,2..n-1 codeword n
2123 * <---(528/532 Bytes)--> <-------(528/532 Bytes)--------->
2124 *
2125 * n = Number of codewords in the page
2126 * . = ECC bytes
2127 * * = Spare/free bytes
2128 * x = Unused byte(s)
2129 * y = Reserved byte(s)
2130 *
2131 * 2K page: n = 4, spare = 16 bytes
2132 * 4K page: n = 8, spare = 32 bytes
2133 * 8K page: n = 16, spare = 64 bytes
2134 *
2135 * the qcom nand controller operates at a sub page/codeword level. each
2136 * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
2137 * the number of ECC bytes vary based on the ECC strength and the bus width.
2138 *
2139 * the first n - 1 codewords contains 516 bytes of user data, the remaining
2140 * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
2141 * both user data and spare(oobavail) bytes that sum up to 516 bytes.
2142 *
2143 * When we access a page with ECC enabled, the reserved bytes(s) are not
2144 * accessible at all. When reading, we fill up these unreadable positions
2145 * with 0xffs. When writing, the controller skips writing the inaccessible
2146 * bytes.
2147 *
2148 * Layout with ECC disabled:
2149 *
2150 * |------------------------------| |---------------------------------------|
2151 * | yy xx.......| | bb *********xx.......|
2152 * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..|
2153 * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......|
2154 * | yy xx.......| | bb *********xx.......|
2155 * |------------------------------| |---------------------------------------|
2156 * codeword 1,2..n-1 codeword n
2157 * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)----------->
2158 *
2159 * n = Number of codewords in the page
2160 * . = ECC bytes
2161 * * = Spare/free bytes
2162 * x = Unused byte(s)
2163 * y = Dummy Bad Bock byte(s)
2164 * b = Real Bad Block byte(s)
2165 * size1/size2 = function of codeword size and 'n'
2166 *
2167 * when the ECC block is disabled, one reserved byte (or two for 16 bit bus
2168 * width) is now accessible. For the first n - 1 codewords, these are dummy Bad
2169 * Block Markers. In the last codeword, this position contains the real BBM
2170 *
2171 * In order to have a consistent layout between RAW and ECC modes, we assume
2172 * the following OOB layout arrangement:
2173 *
2174 * |-----------| |--------------------|
2175 * |yyxx.......| |bb*********xx.......|
2176 * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..|
2177 * |yyxx.......| |bb*********xx.......|
2178 * |yyxx.......| |bb*********xx.......|
2179 * |-----------| |--------------------|
2180 * first n - 1 nth OOB region
2181 * OOB regions
2182 *
2183 * n = Number of codewords in the page
2184 * . = ECC bytes
2185 * * = FREE OOB bytes
2186 * y = Dummy bad block byte(s) (inaccessible when ECC enabled)
2187 * x = Unused byte(s)
2188 * b = Real bad block byte(s) (inaccessible when ECC enabled)
2189 *
2190 * This layout is read as is when ECC is disabled. When ECC is enabled, the
2191 * inaccessible Bad Block byte(s) are ignored when we write to a page/oob,
2192 * and assumed as 0xffs when we read a page/oob. The ECC, unused and
Boris Brezillon421e81c2016-03-18 17:54:27 +01002193 * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is
2194 * the sum of the three).
Archit Tanejac76b78d2016-02-03 14:29:50 +05302195 */
Boris Brezillon421e81c2016-03-18 17:54:27 +01002196static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2197 struct mtd_oob_region *oobregion)
Archit Tanejac76b78d2016-02-03 14:29:50 +05302198{
Boris Brezillon421e81c2016-03-18 17:54:27 +01002199 struct nand_chip *chip = mtd_to_nand(mtd);
2200 struct qcom_nand_host *host = to_qcom_nand_host(chip);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302201 struct nand_ecc_ctrl *ecc = &chip->ecc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302202
Boris Brezillon421e81c2016-03-18 17:54:27 +01002203 if (section > 1)
2204 return -ERANGE;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302205
Boris Brezillon421e81c2016-03-18 17:54:27 +01002206 if (!section) {
2207 oobregion->length = (ecc->bytes * (ecc->steps - 1)) +
2208 host->bbm_size;
2209 oobregion->offset = 0;
2210 } else {
2211 oobregion->length = host->ecc_bytes_hw + host->spare_bytes;
2212 oobregion->offset = mtd->oobsize - oobregion->length;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302213 }
2214
Boris Brezillon421e81c2016-03-18 17:54:27 +01002215 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302216}
2217
Boris Brezillon421e81c2016-03-18 17:54:27 +01002218static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section,
2219 struct mtd_oob_region *oobregion)
2220{
2221 struct nand_chip *chip = mtd_to_nand(mtd);
2222 struct qcom_nand_host *host = to_qcom_nand_host(chip);
2223 struct nand_ecc_ctrl *ecc = &chip->ecc;
2224
2225 if (section)
2226 return -ERANGE;
2227
2228 oobregion->length = ecc->steps * 4;
2229 oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size;
2230
2231 return 0;
2232}
2233
2234static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = {
2235 .ecc = qcom_nand_ooblayout_ecc,
2236 .free = qcom_nand_ooblayout_free,
2237};
2238
Archit Tanejac76b78d2016-02-03 14:29:50 +05302239static int qcom_nand_host_setup(struct qcom_nand_host *host)
2240{
2241 struct nand_chip *chip = &host->chip;
2242 struct mtd_info *mtd = nand_to_mtd(chip);
2243 struct nand_ecc_ctrl *ecc = &chip->ecc;
2244 struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip);
2245 int cwperpage, bad_block_byte;
2246 bool wide_bus;
2247 int ecc_mode = 1;
2248
2249 /*
2250 * the controller requires each step consists of 512 bytes of data.
2251 * bail out if DT has populated a wrong step size.
2252 */
2253 if (ecc->size != NANDC_STEP_SIZE) {
2254 dev_err(nandc->dev, "invalid ecc size\n");
2255 return -EINVAL;
2256 }
2257
2258 wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
2259
2260 if (ecc->strength >= 8) {
2261 /* 8 bit ECC defaults to BCH ECC on all platforms */
2262 host->bch_enabled = true;
2263 ecc_mode = 1;
2264
2265 if (wide_bus) {
2266 host->ecc_bytes_hw = 14;
2267 host->spare_bytes = 0;
2268 host->bbm_size = 2;
2269 } else {
2270 host->ecc_bytes_hw = 13;
2271 host->spare_bytes = 2;
2272 host->bbm_size = 1;
2273 }
2274 } else {
2275 /*
2276 * if the controller supports BCH for 4 bit ECC, the controller
2277 * uses lesser bytes for ECC. If RS is used, the ECC bytes is
2278 * always 10 bytes
2279 */
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302280 if (nandc->props->ecc_modes & ECC_BCH_4BIT) {
Archit Tanejac76b78d2016-02-03 14:29:50 +05302281 /* BCH */
2282 host->bch_enabled = true;
2283 ecc_mode = 0;
2284
2285 if (wide_bus) {
2286 host->ecc_bytes_hw = 8;
2287 host->spare_bytes = 2;
2288 host->bbm_size = 2;
2289 } else {
2290 host->ecc_bytes_hw = 7;
2291 host->spare_bytes = 4;
2292 host->bbm_size = 1;
2293 }
2294 } else {
2295 /* RS */
2296 host->ecc_bytes_hw = 10;
2297
2298 if (wide_bus) {
2299 host->spare_bytes = 0;
2300 host->bbm_size = 2;
2301 } else {
2302 host->spare_bytes = 1;
2303 host->bbm_size = 1;
2304 }
2305 }
2306 }
2307
2308 /*
2309 * we consider ecc->bytes as the sum of all the non-data content in a
2310 * step. It gives us a clean representation of the oob area (even if
2311 * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit
2312 * ECC and 12 bytes for 4 bit ECC
2313 */
2314 ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size;
2315
2316 ecc->read_page = qcom_nandc_read_page;
2317 ecc->read_page_raw = qcom_nandc_read_page_raw;
2318 ecc->read_oob = qcom_nandc_read_oob;
2319 ecc->write_page = qcom_nandc_write_page;
2320 ecc->write_page_raw = qcom_nandc_write_page_raw;
2321 ecc->write_oob = qcom_nandc_write_oob;
2322
2323 ecc->mode = NAND_ECC_HW;
2324
Boris Brezillon421e81c2016-03-18 17:54:27 +01002325 mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302326
2327 cwperpage = mtd->writesize / ecc->size;
Abhishek Sahucb80f112017-08-17 17:37:40 +05302328 nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
2329 cwperpage);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302330
2331 /*
2332 * DATA_UD_BYTES varies based on whether the read/write command protects
2333 * spare data with ECC too. We protect spare data by default, so we set
2334 * it to main + spare data, which are 512 and 4 bytes respectively.
2335 */
2336 host->cw_data = 516;
2337
2338 /*
2339 * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes
2340 * for 8 bit ECC
2341 */
2342 host->cw_size = host->cw_data + ecc->bytes;
2343
2344 if (ecc->bytes * (mtd->writesize / ecc->size) > mtd->oobsize) {
2345 dev_err(nandc->dev, "ecc data doesn't fit in OOB area\n");
2346 return -EINVAL;
2347 }
2348
2349 bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1;
2350
2351 host->cfg0 = (cwperpage - 1) << CW_PER_PAGE
2352 | host->cw_data << UD_SIZE_BYTES
2353 | 0 << DISABLE_STATUS_AFTER_WRITE
2354 | 5 << NUM_ADDR_CYCLES
2355 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS
2356 | 0 << STATUS_BFR_READ
2357 | 1 << SET_RD_MODE_AFTER_STATUS
2358 | host->spare_bytes << SPARE_SIZE_BYTES;
2359
2360 host->cfg1 = 7 << NAND_RECOVERY_CYCLES
2361 | 0 << CS_ACTIVE_BSY
2362 | bad_block_byte << BAD_BLOCK_BYTE_NUM
2363 | 0 << BAD_BLOCK_IN_SPARE_AREA
2364 | 2 << WR_RD_BSY_GAP
2365 | wide_bus << WIDE_FLASH
2366 | host->bch_enabled << ENABLE_BCH_ECC;
2367
2368 host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
2369 | host->cw_size << UD_SIZE_BYTES
2370 | 5 << NUM_ADDR_CYCLES
2371 | 0 << SPARE_SIZE_BYTES;
2372
2373 host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
2374 | 0 << CS_ACTIVE_BSY
2375 | 17 << BAD_BLOCK_BYTE_NUM
2376 | 1 << BAD_BLOCK_IN_SPARE_AREA
2377 | 2 << WR_RD_BSY_GAP
2378 | wide_bus << WIDE_FLASH
2379 | 1 << DEV0_CFG1_ECC_DISABLE;
2380
Abhishek Sahu10777de2017-08-03 17:56:39 +02002381 host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE
Archit Tanejac76b78d2016-02-03 14:29:50 +05302382 | 0 << ECC_SW_RESET
2383 | host->cw_data << ECC_NUM_DATA_BYTES
2384 | 1 << ECC_FORCE_CLK_OPEN
2385 | ecc_mode << ECC_MODE
2386 | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH;
2387
2388 host->ecc_buf_cfg = 0x203 << NUM_STEPS;
2389
2390 host->clrflashstatus = FS_READY_BSY_N;
2391 host->clrreadstatus = 0xc0;
Abhishek Sahua86b9c42017-08-17 17:37:44 +05302392 nandc->regs->erased_cw_detect_cfg_clr =
2393 cpu_to_le32(CLR_ERASED_PAGE_DET);
2394 nandc->regs->erased_cw_detect_cfg_set =
2395 cpu_to_le32(SET_ERASED_PAGE_DET);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302396
2397 dev_dbg(nandc->dev,
2398 "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",
2399 host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg,
2400 host->cw_size, host->cw_data, ecc->strength, ecc->bytes,
2401 cwperpage);
2402
2403 return 0;
2404}
2405
2406static int qcom_nandc_alloc(struct qcom_nand_controller *nandc)
2407{
2408 int ret;
2409
2410 ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32));
2411 if (ret) {
2412 dev_err(nandc->dev, "failed to set DMA mask\n");
2413 return ret;
2414 }
2415
2416 /*
2417 * we use the internal buffer for reading ONFI params, reading small
2418 * data like ID and status, and preforming read-copy-write operations
2419 * when writing to a codeword partially. 532 is the maximum possible
2420 * size of a codeword for our nand controller
2421 */
2422 nandc->buf_size = 532;
2423
2424 nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size,
2425 GFP_KERNEL);
2426 if (!nandc->data_buffer)
2427 return -ENOMEM;
2428
2429 nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs),
2430 GFP_KERNEL);
2431 if (!nandc->regs)
2432 return -ENOMEM;
2433
2434 nandc->reg_read_buf = devm_kzalloc(nandc->dev,
2435 MAX_REG_RD * sizeof(*nandc->reg_read_buf),
2436 GFP_KERNEL);
2437 if (!nandc->reg_read_buf)
2438 return -ENOMEM;
2439
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302440 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302441 nandc->reg_read_dma =
2442 dma_map_single(nandc->dev, nandc->reg_read_buf,
2443 MAX_REG_RD *
2444 sizeof(*nandc->reg_read_buf),
2445 DMA_FROM_DEVICE);
2446 if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) {
2447 dev_err(nandc->dev, "failed to DMA MAP reg buffer\n");
2448 return -EIO;
2449 }
2450
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302451 nandc->tx_chan = dma_request_slave_channel(nandc->dev, "tx");
2452 if (!nandc->tx_chan) {
2453 dev_err(nandc->dev, "failed to request tx channel\n");
2454 return -ENODEV;
2455 }
2456
2457 nandc->rx_chan = dma_request_slave_channel(nandc->dev, "rx");
2458 if (!nandc->rx_chan) {
2459 dev_err(nandc->dev, "failed to request rx channel\n");
2460 return -ENODEV;
2461 }
2462
2463 nandc->cmd_chan = dma_request_slave_channel(nandc->dev, "cmd");
2464 if (!nandc->cmd_chan) {
2465 dev_err(nandc->dev, "failed to request cmd channel\n");
2466 return -ENODEV;
2467 }
Abhishek Sahucb80f112017-08-17 17:37:40 +05302468
2469 /*
2470 * Initially allocate BAM transaction to read ONFI param page.
2471 * After detecting all the devices, this BAM transaction will
2472 * be freed and the next BAM tranasction will be allocated with
2473 * maximum codeword size
2474 */
2475 nandc->max_cwperpage = 1;
2476 nandc->bam_txn = alloc_bam_transaction(nandc);
2477 if (!nandc->bam_txn) {
2478 dev_err(nandc->dev,
2479 "failed to allocate bam transaction\n");
2480 return -ENOMEM;
2481 }
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302482 } else {
2483 nandc->chan = dma_request_slave_channel(nandc->dev, "rxtx");
2484 if (!nandc->chan) {
2485 dev_err(nandc->dev,
2486 "failed to request slave channel\n");
2487 return -ENODEV;
2488 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302489 }
2490
2491 INIT_LIST_HEAD(&nandc->desc_list);
2492 INIT_LIST_HEAD(&nandc->host_list);
2493
Marc Gonzalezd45bc582016-07-27 11:23:52 +02002494 nand_hw_control_init(&nandc->controller);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302495
2496 return 0;
2497}
2498
2499static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc)
2500{
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302501 if (nandc->props->is_bam) {
Abhishek Sahu6192ff72017-08-17 17:37:39 +05302502 if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma))
2503 dma_unmap_single(nandc->dev, nandc->reg_read_dma,
2504 MAX_REG_RD *
2505 sizeof(*nandc->reg_read_buf),
2506 DMA_FROM_DEVICE);
2507
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302508 if (nandc->tx_chan)
2509 dma_release_channel(nandc->tx_chan);
2510
2511 if (nandc->rx_chan)
2512 dma_release_channel(nandc->rx_chan);
2513
2514 if (nandc->cmd_chan)
2515 dma_release_channel(nandc->cmd_chan);
2516 } else {
2517 if (nandc->chan)
2518 dma_release_channel(nandc->chan);
2519 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302520}
2521
2522/* one time setup of a few nand controller registers */
2523static int qcom_nandc_setup(struct qcom_nand_controller *nandc)
2524{
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302525 u32 nand_ctrl;
2526
Archit Tanejac76b78d2016-02-03 14:29:50 +05302527 /* kill onenand */
2528 nandc_write(nandc, SFLASHC_BURST_CFG, 0);
Abhishek Sahucc409b92017-08-17 17:37:47 +05302529 nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD),
2530 NAND_DEV_CMD_VLD_VAL);
Archit Tanejac76b78d2016-02-03 14:29:50 +05302531
Abhishek Sahu9d43f912017-08-17 17:37:45 +05302532 /* enable ADM or BAM DMA */
2533 if (nandc->props->is_bam) {
2534 nand_ctrl = nandc_read(nandc, NAND_CTRL);
2535 nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN);
2536 } else {
2537 nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN);
2538 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302539
2540 /* save the original values of these registers */
Abhishek Sahucc409b92017-08-17 17:37:47 +05302541 nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1));
Abhishek Sahud8a9b322017-08-11 17:09:16 +05302542 nandc->vld = NAND_DEV_CMD_VLD_VAL;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302543
2544 return 0;
2545}
2546
2547static int qcom_nand_host_init(struct qcom_nand_controller *nandc,
2548 struct qcom_nand_host *host,
2549 struct device_node *dn)
2550{
2551 struct nand_chip *chip = &host->chip;
2552 struct mtd_info *mtd = nand_to_mtd(chip);
2553 struct device *dev = nandc->dev;
2554 int ret;
2555
2556 ret = of_property_read_u32(dn, "reg", &host->cs);
2557 if (ret) {
2558 dev_err(dev, "can't get chip-select\n");
2559 return -ENXIO;
2560 }
2561
2562 nand_set_flash_node(chip, dn);
2563 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs);
2564 mtd->owner = THIS_MODULE;
2565 mtd->dev.parent = dev;
2566
2567 chip->cmdfunc = qcom_nandc_command;
2568 chip->select_chip = qcom_nandc_select_chip;
2569 chip->read_byte = qcom_nandc_read_byte;
2570 chip->read_buf = qcom_nandc_read_buf;
2571 chip->write_buf = qcom_nandc_write_buf;
Boris Brezillon4a78cc62017-05-26 17:10:15 +02002572 chip->onfi_set_features = nand_onfi_get_set_features_notsupp;
2573 chip->onfi_get_features = nand_onfi_get_set_features_notsupp;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302574
2575 /*
2576 * the bad block marker is readable only when we read the last codeword
2577 * of a page with ECC disabled. currently, the nand_base and nand_bbt
2578 * helpers don't allow us to read BB from a nand chip with ECC
2579 * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad
2580 * and block_markbad helpers until we permanently switch to using
2581 * MTD_OPS_RAW for all drivers (with the help of badblockbits)
2582 */
2583 chip->block_bad = qcom_nandc_block_bad;
2584 chip->block_markbad = qcom_nandc_block_markbad;
2585
2586 chip->controller = &nandc->controller;
2587 chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER |
2588 NAND_SKIP_BBTSCAN;
2589
2590 /* set up initial status value */
2591 host->status = NAND_STATUS_READY | NAND_STATUS_WP;
2592
2593 ret = nand_scan_ident(mtd, 1, NULL);
2594 if (ret)
2595 return ret;
2596
2597 ret = qcom_nand_host_setup(host);
Abhishek Sahu89f51272017-07-19 17:17:58 +05302598
2599 return ret;
2600}
2601
2602static int qcom_nand_mtd_register(struct qcom_nand_controller *nandc,
2603 struct qcom_nand_host *host,
2604 struct device_node *dn)
2605{
2606 struct nand_chip *chip = &host->chip;
2607 struct mtd_info *mtd = nand_to_mtd(chip);
2608 int ret;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302609
2610 ret = nand_scan_tail(mtd);
2611 if (ret)
2612 return ret;
2613
Abhishek Sahu89f51272017-07-19 17:17:58 +05302614 ret = mtd_device_register(mtd, NULL, 0);
2615 if (ret)
2616 nand_cleanup(mtd_to_nand(mtd));
2617
2618 return ret;
2619}
2620
2621static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc)
2622{
2623 struct device *dev = nandc->dev;
2624 struct device_node *dn = dev->of_node, *child;
2625 struct qcom_nand_host *host, *tmp;
2626 int ret;
2627
2628 for_each_available_child_of_node(dn, child) {
2629 host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
2630 if (!host) {
2631 of_node_put(child);
2632 return -ENOMEM;
2633 }
2634
2635 ret = qcom_nand_host_init(nandc, host, child);
2636 if (ret) {
2637 devm_kfree(dev, host);
2638 continue;
2639 }
2640
2641 list_add_tail(&host->node, &nandc->host_list);
2642 }
2643
2644 if (list_empty(&nandc->host_list))
2645 return -ENODEV;
2646
Abhishek Sahucb80f112017-08-17 17:37:40 +05302647 if (nandc->props->is_bam) {
2648 free_bam_transaction(nandc);
2649 nandc->bam_txn = alloc_bam_transaction(nandc);
2650 if (!nandc->bam_txn) {
2651 dev_err(nandc->dev,
2652 "failed to allocate bam transaction\n");
2653 return -ENOMEM;
2654 }
2655 }
2656
Abhishek Sahu89f51272017-07-19 17:17:58 +05302657 list_for_each_entry_safe(host, tmp, &nandc->host_list, node) {
2658 ret = qcom_nand_mtd_register(nandc, host, child);
2659 if (ret) {
2660 list_del(&host->node);
2661 devm_kfree(dev, host);
2662 }
2663 }
2664
2665 if (list_empty(&nandc->host_list))
2666 return -ENODEV;
2667
2668 return 0;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302669}
2670
2671/* parse custom DT properties here */
2672static int qcom_nandc_parse_dt(struct platform_device *pdev)
2673{
2674 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2675 struct device_node *np = nandc->dev->of_node;
2676 int ret;
2677
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302678 if (!nandc->props->is_bam) {
2679 ret = of_property_read_u32(np, "qcom,cmd-crci",
2680 &nandc->cmd_crci);
2681 if (ret) {
2682 dev_err(nandc->dev, "command CRCI unspecified\n");
2683 return ret;
2684 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302685
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302686 ret = of_property_read_u32(np, "qcom,data-crci",
2687 &nandc->data_crci);
2688 if (ret) {
2689 dev_err(nandc->dev, "data CRCI unspecified\n");
2690 return ret;
2691 }
Archit Tanejac76b78d2016-02-03 14:29:50 +05302692 }
2693
2694 return 0;
2695}
2696
2697static int qcom_nandc_probe(struct platform_device *pdev)
2698{
2699 struct qcom_nand_controller *nandc;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302700 const void *dev_data;
2701 struct device *dev = &pdev->dev;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302702 struct resource *res;
2703 int ret;
2704
2705 nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL);
2706 if (!nandc)
2707 return -ENOMEM;
2708
2709 platform_set_drvdata(pdev, nandc);
2710 nandc->dev = dev;
2711
2712 dev_data = of_device_get_match_data(dev);
2713 if (!dev_data) {
2714 dev_err(&pdev->dev, "failed to get device data\n");
2715 return -ENODEV;
2716 }
2717
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302718 nandc->props = dev_data;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302719
2720 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2721 nandc->base = devm_ioremap_resource(dev, res);
2722 if (IS_ERR(nandc->base))
2723 return PTR_ERR(nandc->base);
2724
2725 nandc->base_dma = phys_to_dma(dev, (phys_addr_t)res->start);
2726
2727 nandc->core_clk = devm_clk_get(dev, "core");
2728 if (IS_ERR(nandc->core_clk))
2729 return PTR_ERR(nandc->core_clk);
2730
2731 nandc->aon_clk = devm_clk_get(dev, "aon");
2732 if (IS_ERR(nandc->aon_clk))
2733 return PTR_ERR(nandc->aon_clk);
2734
2735 ret = qcom_nandc_parse_dt(pdev);
2736 if (ret)
2737 return ret;
2738
2739 ret = qcom_nandc_alloc(nandc);
2740 if (ret)
Abhishek Sahu497d7d82017-08-11 17:09:19 +05302741 goto err_core_clk;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302742
2743 ret = clk_prepare_enable(nandc->core_clk);
2744 if (ret)
2745 goto err_core_clk;
2746
2747 ret = clk_prepare_enable(nandc->aon_clk);
2748 if (ret)
2749 goto err_aon_clk;
2750
2751 ret = qcom_nandc_setup(nandc);
2752 if (ret)
2753 goto err_setup;
2754
Abhishek Sahu89f51272017-07-19 17:17:58 +05302755 ret = qcom_probe_nand_devices(nandc);
2756 if (ret)
2757 goto err_setup;
Archit Tanejac76b78d2016-02-03 14:29:50 +05302758
2759 return 0;
2760
Archit Tanejac76b78d2016-02-03 14:29:50 +05302761err_setup:
2762 clk_disable_unprepare(nandc->aon_clk);
2763err_aon_clk:
2764 clk_disable_unprepare(nandc->core_clk);
2765err_core_clk:
2766 qcom_nandc_unalloc(nandc);
2767
2768 return ret;
2769}
2770
2771static int qcom_nandc_remove(struct platform_device *pdev)
2772{
2773 struct qcom_nand_controller *nandc = platform_get_drvdata(pdev);
2774 struct qcom_nand_host *host;
2775
2776 list_for_each_entry(host, &nandc->host_list, node)
2777 nand_release(nand_to_mtd(&host->chip));
2778
2779 qcom_nandc_unalloc(nandc);
2780
2781 clk_disable_unprepare(nandc->aon_clk);
2782 clk_disable_unprepare(nandc->core_clk);
2783
2784 return 0;
2785}
2786
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302787static const struct qcom_nandc_props ipq806x_nandc_props = {
2788 .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT),
Abhishek Sahu8c5d5d62017-08-11 17:09:18 +05302789 .is_bam = false,
Abhishek Sahucc409b92017-08-17 17:37:47 +05302790 .dev_cmd_reg_start = 0x0,
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302791};
Archit Tanejac76b78d2016-02-03 14:29:50 +05302792
Abhishek Sahua0637832017-08-17 17:37:53 +05302793static const struct qcom_nandc_props ipq4019_nandc_props = {
2794 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
2795 .is_bam = true,
2796 .dev_cmd_reg_start = 0x0,
2797};
2798
Abhishek Sahudce84762017-08-17 17:37:54 +05302799static const struct qcom_nandc_props ipq8074_nandc_props = {
2800 .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT),
2801 .is_bam = true,
2802 .dev_cmd_reg_start = 0x7000,
2803};
2804
Archit Tanejac76b78d2016-02-03 14:29:50 +05302805/*
2806 * data will hold a struct pointer containing more differences once we support
2807 * more controller variants
2808 */
2809static const struct of_device_id qcom_nandc_of_match[] = {
Abhishek Sahu58f1f222017-08-11 17:09:17 +05302810 {
2811 .compatible = "qcom,ipq806x-nand",
2812 .data = &ipq806x_nandc_props,
Archit Tanejac76b78d2016-02-03 14:29:50 +05302813 },
Abhishek Sahua0637832017-08-17 17:37:53 +05302814 {
2815 .compatible = "qcom,ipq4019-nand",
2816 .data = &ipq4019_nandc_props,
2817 },
Abhishek Sahudce84762017-08-17 17:37:54 +05302818 {
2819 .compatible = "qcom,ipq8074-nand",
2820 .data = &ipq8074_nandc_props,
2821 },
Archit Tanejac76b78d2016-02-03 14:29:50 +05302822 {}
2823};
2824MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
2825
2826static struct platform_driver qcom_nandc_driver = {
2827 .driver = {
2828 .name = "qcom-nandc",
2829 .of_match_table = qcom_nandc_of_match,
2830 },
2831 .probe = qcom_nandc_probe,
2832 .remove = qcom_nandc_remove,
2833};
2834module_platform_driver(qcom_nandc_driver);
2835
2836MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
2837MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
2838MODULE_LICENSE("GPL v2");