1 From dc12953941ed3b8bc9eb8d47f8c7e74f54b47049 Mon Sep 17 00:00:00 2001
2 From: Md Sadre Alam <quic_mdalam@quicinc.com>
3 Date: Mon, 19 Aug 2024 11:05:18 +0530
4 Subject: [PATCH v10 6/8] spi: spi-qpic: add driver for QCOM SPI NAND flash
7 This driver implements support for the SPI-NAND mode of QCOM NAND Flash
8 Interface as a SPI-MEM controller with pipelined ECC capability.
10 Co-developed-by: Sricharan Ramabadhran <quic_srichara@quicinc.com>
11 Signed-off-by: Sricharan Ramabadhran <quic_srichara@quicinc.com>
12 Co-developed-by: Varadarajan Narayanan <quic_varada@quicinc.com>
13 Signed-off-by: Varadarajan Narayanan <quic_varada@quicinc.com>
14 Signed-off-by: Md Sadre Alam <quic_mdalam@quicinc.com>
19 * Fixed compilation warnings reported by kernel test robot.
20 * Added depends on CONFIG_MTD
21 * removed extra bracket from statement if (i == (num_cw - 1)) in
22 qcom_spi_program_raw() api.
26 * Changed data type of addr1, addr2, cmd, to __le32 in qpic_spi_nand
28 * In qcom_spi_set_read_loc_first() api added cpu_to_le32() macro to fix
30 * In qcom_spi_set_read_loc_last() api added cpu_to_le32() macro to fix
32 * In qcom_spi_init() api added cpu_to_le32() macro to fix compilation
34 * In qcom_spi_ecc_init_ctx_pipelined() api removed unused variables
35 reqs, user, step_size, strength and added cpu_to_le32() macro as well
36 to fix compilation warning
37 * In qcom_spi_read_last_cw() api added cpu_to_le32() macro to fix compilation
39 * In qcom_spi_check_error() api added cpu_to_le32() macro to fix compilation
41 * In qcom_spi_read_page_ecc() api added cpu_to_le32() macro to fix compilation
43 * In qcom_spi_read_page_oob() api added cpu_to_le32() macro to fix compilation
45 * In qcom_spi_program_raw() api added cpu_to_le32() macro to fix compilation
47 * In qcom_spi_program_ecc() api added cpu_to_le32() macro to fix compilation
49 * In qcom_spi_program_oob() api added cpu_to_le32() macro to fix compilation
51 * In qcom_spi_send_cmdaddr() api added cpu_to_le32() macro to fix compilation
53 * In qcom_spi_io_op() api added cpu_to_le32() macro to fix compilation
58 * Included "bitfield.h" file to /spi-qpic-snand.c
59 to fix compilation warning reported by kernel test robot
60 * Removed unused variable "steps" in
61 qcom_spi_ecc_init_ctx_pipelined() to fix compilation warning
65 * Added read_oob() and write_oob() api
67 * Handled offset value for oob layout
69 * Made CONFIG_SPI_QPIC_SNAND as bool
71 * Added macro ecceng_to_qspi()
73 * Added FIELD_PREP() Macro in spi init
75 * Added else condition in
76 qcom_spi_ecc_finish_io_req_pipelined()
79 * Handled multiple error condition for api
80 qcom_spi_cmd_mapping()
82 * Fix typo for printing debug message
86 * Added separate qpic_spi_nand{...} struct
88 * moved qpic_ecc and qcom_ecc_stats struct to
89 spi-qpic-snand.c file, since its spi nand
92 * Added FIELD_PREP() and GENMASK() macro
94 * Removed rawnand.h and partition.h from
97 * Removed oob_buff assignment form
98 qcom_spi_write_page_cache
100 * Added qcom_nand_unalloc() in remove() path
102 * Fixes all all comments
106 * Added raw_read() and raw_write() api
108 * Updated commit message
110 * Removed register indirection
112 * Added qcom_spi_ prefix to all the api
114 * Removed snand_set_reg() api.
116 * Fixed nandbiterr issue
118 * Removed hardcoded num_cw and made it variable
120 * Removed hardcoded value for mtd pagesize
122 * Added -ENOSUPPORT in cmd mapping for unsupported
125 * Replace if..else with switch..case statement
133 * Set SPI_QPIC_SNAND to n and added COMPILE_TEST in Kconfig
135 * Made driver name sorted in Make file
137 * Made comment like c++
139 * Changed macro to functions, snandc_set_read_loc_last()
140 and snandc_set_read_loc_first()
142 * Added error handling in snandc_set_reg()
144 * Changed into normal conditional statement for
145 return snandc->ecc_stats.failed ? -EBADMSG :
146 snandc->ecc_stats.bitflips;
148 * Remove cast of wbuf in qpic_snand_program_execute()
151 * Made num_cw variable instead hardcoded value
153 * changed if..else condition of function qpic_snand_io_op()
154 to switch..case statement
156 * Added __devm_spi_alloc_controller() api instead of
157 devm_spi_alloc_master()
159 * Disabling clock in remove path
163 * Added initial support for SPI-NAND driver
167 * Added RFC patch for design review
169 drivers/mtd/nand/Makefile | 5 +-
170 drivers/spi/Kconfig | 9 +
171 drivers/spi/Makefile | 1 +
172 drivers/spi/spi-qpic-snand.c | 1634 ++++++++++++++++++++++++++
173 include/linux/mtd/nand-qpic-common.h | 7 +
174 5 files changed, 1655 insertions(+), 1 deletion(-)
175 create mode 100644 drivers/spi/spi-qpic-snand.c
177 --- a/drivers/mtd/nand/Makefile
178 +++ b/drivers/mtd/nand/Makefile
179 @@ -7,8 +7,11 @@ obj-$(CONFIG_MTD_NAND_MTK_BMT) += mtk_bm
181 ifeq ($(CONFIG_MTD_NAND_QCOM),y)
182 obj-y += qpic_common.o
184 +ifeq ($(CONFIG_SPI_QPIC_SNAND),y)
185 +obj-y += qpic_common.o
192 --- a/drivers/spi/Kconfig
193 +++ b/drivers/spi/Kconfig
194 @@ -870,6 +870,15 @@ config SPI_QCOM_QSPI
196 QSPI(Quad SPI) driver for Qualcomm QSPI controller.
198 +config SPI_QPIC_SNAND
199 + bool "QPIC SNAND controller"
200 + depends on ARCH_QCOM || COMPILE_TEST
203 + QPIC_SNAND (QPIC SPI NAND) driver for Qualcomm QPIC controller.
204 + QPIC controller supports both parallel nand and serial nand.
205 + This config will enable serial nand driver for QPIC controller.
208 tristate "Qualcomm SPI controller with QUP interface"
209 depends on ARCH_QCOM || COMPILE_TEST
210 --- a/drivers/spi/Makefile
211 +++ b/drivers/spi/Makefile
212 @@ -110,6 +110,7 @@ obj-$(CONFIG_SPI_PXA2XX) += spi-pxa2xx-
213 obj-$(CONFIG_SPI_PXA2XX_PCI) += spi-pxa2xx-pci.o
214 obj-$(CONFIG_SPI_QCOM_GENI) += spi-geni-qcom.o
215 obj-$(CONFIG_SPI_QCOM_QSPI) += spi-qcom-qspi.o
216 +obj-$(CONFIG_SPI_QPIC_SNAND) += spi-qpic-snand.o
217 obj-$(CONFIG_SPI_QUP) += spi-qup.o
218 obj-$(CONFIG_SPI_ROCKCHIP) += spi-rockchip.o
219 obj-$(CONFIG_SPI_ROCKCHIP_SFC) += spi-rockchip-sfc.o
221 +++ b/drivers/spi/spi-qpic-snand.c
224 + * SPDX-License-Identifier: GPL-2.0
226 + * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
229 + * Md Sadre Alam <quic_mdalam@quicinc.com>
230 + * Sricharan R <quic_srichara@quicinc.com>
231 + * Varadarajan Narayanan <quic_varada@quicinc.com>
233 +#include <linux/bitops.h>
234 +#include <linux/clk.h>
235 +#include <linux/delay.h>
236 +#include <linux/dmaengine.h>
237 +#include <linux/dma-mapping.h>
238 +#include <linux/dma/qcom_adm.h>
239 +#include <linux/dma/qcom_bam_dma.h>
240 +#include <linux/module.h>
241 +#include <linux/of.h>
242 +#include <linux/platform_device.h>
243 +#include <linux/slab.h>
244 +#include <linux/mtd/nand-qpic-common.h>
245 +#include <linux/mtd/spinand.h>
246 +#include <linux/bitfield.h>
248 +#define NAND_FLASH_SPI_CFG 0xc0
249 +#define NAND_NUM_ADDR_CYCLES 0xc4
250 +#define NAND_BUSY_CHECK_WAIT_CNT 0xc8
251 +#define NAND_FLASH_FEATURES 0xf64
253 +/* QSPI NAND config reg bits */
254 +#define LOAD_CLK_CNTR_INIT_EN BIT(28)
255 +#define CLK_CNTR_INIT_VAL_VEC 0x924
256 +#define CLK_CNTR_INIT_VAL_VEC_MASK GENMASK(27, 16)
257 +#define FEA_STATUS_DEV_ADDR 0xc0
258 +#define FEA_STATUS_DEV_ADDR_MASK GENMASK(15, 8)
259 +#define SPI_CFG BIT(0)
260 +#define SPI_NUM_ADDR 0xDA4DB
261 +#define SPI_WAIT_CNT 0x10
262 +#define QPIC_QSPI_NUM_CS 1
263 +#define SPI_TRANSFER_MODE_x1 BIT(29)
264 +#define SPI_TRANSFER_MODE_x4 (3 << 29)
265 +#define SPI_WP BIT(28)
266 +#define SPI_HOLD BIT(27)
267 +#define QPIC_SET_FEATURE BIT(31)
269 +#define SPINAND_RESET 0xff
270 +#define SPINAND_READID 0x9f
271 +#define SPINAND_GET_FEATURE 0x0f
272 +#define SPINAND_SET_FEATURE 0x1f
273 +#define SPINAND_READ 0x13
274 +#define SPINAND_ERASE 0xd8
275 +#define SPINAND_WRITE_EN 0x06
276 +#define SPINAND_PROGRAM_EXECUTE 0x10
277 +#define SPINAND_PROGRAM_LOAD 0x84
279 +#define ACC_FEATURE 0xe
280 +#define BAD_BLOCK_MARKER_SIZE 0x2
281 +#define OOB_BUF_SIZE 128
282 +#define ecceng_to_qspi(eng) container_of(eng, struct qpic_spi_nand, ecc_eng)
283 +struct qpic_snand_op {
289 +struct snandc_read_status {
290 + __le32 snandc_flash;
291 + __le32 snandc_buffer;
292 + __le32 snandc_erased_cw;
297 + * @corrected: ECC corrected
298 + * @bitflips: Max bit flip
299 + * @failed: ECC failed
301 +struct qcom_ecc_stats {
308 + struct device *dev;
325 + u32 clrflashstatus;
330 +struct qpic_spi_nand {
331 + struct qcom_nand_controller *snandc;
332 + struct spi_controller *ctlr;
333 + struct mtd_info *mtd;
334 + struct clk *iomacro_clk;
335 + struct qpic_ecc *ecc;
336 + struct qcom_ecc_stats ecc_stats;
337 + struct nand_ecc_engine ecc_eng;
350 +static void qcom_spi_set_read_loc_first(struct qcom_nand_controller *snandc,
351 + int reg, int cw_offset, int read_size,
352 + int is_last_read_loc)
355 + u32 val = (((cw_offset) << READ_LOCATION_OFFSET) |
356 + ((read_size) << READ_LOCATION_SIZE) | ((is_last_read_loc)
357 + << READ_LOCATION_LAST));
359 + locreg_val = cpu_to_le32(val);
361 + if (reg == NAND_READ_LOCATION_0)
362 + snandc->regs->read_location0 = locreg_val;
363 + else if (reg == NAND_READ_LOCATION_1)
364 + snandc->regs->read_location1 = locreg_val;
365 + else if (reg == NAND_READ_LOCATION_2)
366 + snandc->regs->read_location1 = locreg_val;
367 + else if (reg == NAND_READ_LOCATION_3)
368 + snandc->regs->read_location3 = locreg_val;
371 +static void qcom_spi_set_read_loc_last(struct qcom_nand_controller *snandc,
372 + int reg, int cw_offset, int read_size,
373 + int is_last_read_loc)
376 + u32 val = (((cw_offset) << READ_LOCATION_OFFSET) |
377 + ((read_size) << READ_LOCATION_SIZE) | ((is_last_read_loc)
378 + << READ_LOCATION_LAST));
380 + locreg_val = cpu_to_le32(val);
382 + if (reg == NAND_READ_LOCATION_LAST_CW_0)
383 + snandc->regs->read_location_last0 = locreg_val;
384 + else if (reg == NAND_READ_LOCATION_LAST_CW_1)
385 + snandc->regs->read_location_last1 = locreg_val;
386 + else if (reg == NAND_READ_LOCATION_LAST_CW_2)
387 + snandc->regs->read_location_last2 = locreg_val;
388 + else if (reg == NAND_READ_LOCATION_LAST_CW_3)
389 + snandc->regs->read_location_last3 = locreg_val;
392 +static struct qcom_nand_controller *nand_to_qcom_snand(struct nand_device *nand)
394 + struct nand_ecc_engine *eng = nand->ecc.engine;
395 + struct qpic_spi_nand *qspi = ecceng_to_qspi(eng);
397 + return qspi->snandc;
400 +static int qcom_spi_init(struct qcom_nand_controller *snandc)
402 + u32 snand_cfg_val = 0x0;
405 + snand_cfg_val = FIELD_PREP(CLK_CNTR_INIT_VAL_VEC_MASK, CLK_CNTR_INIT_VAL_VEC) |
406 + FIELD_PREP(LOAD_CLK_CNTR_INIT_EN, 0) |
407 + FIELD_PREP(FEA_STATUS_DEV_ADDR_MASK, FEA_STATUS_DEV_ADDR) |
408 + FIELD_PREP(SPI_CFG, 0);
410 + snandc->regs->spi_cfg = cpu_to_le32(snand_cfg_val);
411 + snandc->regs->num_addr_cycle = cpu_to_le32(SPI_NUM_ADDR);
412 + snandc->regs->busy_wait_cnt = cpu_to_le32(SPI_WAIT_CNT);
414 + qcom_write_reg_dma(snandc, &snandc->regs->spi_cfg, NAND_FLASH_SPI_CFG, 1, 0);
416 + snand_cfg_val &= ~LOAD_CLK_CNTR_INIT_EN;
417 + snandc->regs->spi_cfg = cpu_to_le32(snand_cfg_val);
419 + qcom_write_reg_dma(snandc, &snandc->regs->spi_cfg, NAND_FLASH_SPI_CFG, 1, 0);
421 + qcom_write_reg_dma(snandc, &snandc->regs->num_addr_cycle, NAND_NUM_ADDR_CYCLES, 1, 0);
422 + qcom_write_reg_dma(snandc, &snandc->regs->busy_wait_cnt, NAND_BUSY_CHECK_WAIT_CNT, 1,
423 + NAND_BAM_NEXT_SGL);
425 + ret = qcom_submit_descs(snandc);
427 + dev_err(snandc->dev, "failure in submitting spi init descriptor\n");
434 +static int qcom_spi_ooblayout_ecc(struct mtd_info *mtd, int section,
435 + struct mtd_oob_region *oobregion)
437 + struct nand_device *nand = mtd_to_nanddev(mtd);
438 + struct qcom_nand_controller *snandc = nand_to_qcom_snand(nand);
439 + struct qpic_ecc *qecc = snandc->qspi->ecc;
444 + oobregion->length = qecc->ecc_bytes_hw + qecc->spare_bytes;
445 + oobregion->offset = mtd->oobsize - oobregion->length;
450 +static int qcom_spi_ooblayout_free(struct mtd_info *mtd, int section,
451 + struct mtd_oob_region *oobregion)
453 + struct nand_device *nand = mtd_to_nanddev(mtd);
454 + struct qcom_nand_controller *snandc = nand_to_qcom_snand(nand);
455 + struct qpic_ecc *qecc = snandc->qspi->ecc;
460 + oobregion->length = qecc->steps * 4;
461 + oobregion->offset = ((qecc->steps - 1) * qecc->bytes) + qecc->bbm_size;
466 +static const struct mtd_ooblayout_ops qcom_spi_ooblayout = {
467 + .ecc = qcom_spi_ooblayout_ecc,
468 + .free = qcom_spi_ooblayout_free,
471 +static int qcom_spi_ecc_init_ctx_pipelined(struct nand_device *nand)
473 + struct qcom_nand_controller *snandc = nand_to_qcom_snand(nand);
474 + struct nand_ecc_props *conf = &nand->ecc.ctx.conf;
475 + struct mtd_info *mtd = nanddev_to_mtd(nand);
476 + int cwperpage, bad_block_byte;
477 + struct qpic_ecc *ecc_cfg;
479 + cwperpage = mtd->writesize / NANDC_STEP_SIZE;
480 + snandc->qspi->num_cw = cwperpage;
482 + ecc_cfg = kzalloc(sizeof(*ecc_cfg), GFP_KERNEL);
485 + snandc->qspi->oob_buf = kzalloc(mtd->writesize + mtd->oobsize,
487 + if (!snandc->qspi->oob_buf)
490 + memset(snandc->qspi->oob_buf, 0xff, mtd->writesize + mtd->oobsize);
492 + nand->ecc.ctx.priv = ecc_cfg;
493 + snandc->qspi->mtd = mtd;
495 + ecc_cfg->ecc_bytes_hw = 7;
496 + ecc_cfg->spare_bytes = 4;
497 + ecc_cfg->bbm_size = 1;
498 + ecc_cfg->bch_enabled = true;
499 + ecc_cfg->bytes = ecc_cfg->ecc_bytes_hw + ecc_cfg->spare_bytes + ecc_cfg->bbm_size;
501 + ecc_cfg->steps = 4;
502 + ecc_cfg->strength = 4;
503 + ecc_cfg->step_size = 512;
504 + ecc_cfg->cw_data = 516;
505 + ecc_cfg->cw_size = ecc_cfg->cw_data + ecc_cfg->bytes;
506 + bad_block_byte = mtd->writesize - ecc_cfg->cw_size * (cwperpage - 1) + 1;
508 + mtd_set_ooblayout(mtd, &qcom_spi_ooblayout);
510 + ecc_cfg->cfg0 = FIELD_PREP(CW_PER_PAGE_MASK, (cwperpage - 1)) |
511 + FIELD_PREP(UD_SIZE_BYTES_MASK, ecc_cfg->cw_data) |
512 + FIELD_PREP(DISABLE_STATUS_AFTER_WRITE, 1) |
513 + FIELD_PREP(NUM_ADDR_CYCLES_MASK, 3) |
514 + FIELD_PREP(ECC_PARITY_SIZE_BYTES_RS, ecc_cfg->ecc_bytes_hw) |
515 + FIELD_PREP(STATUS_BFR_READ, 0) |
516 + FIELD_PREP(SET_RD_MODE_AFTER_STATUS, 1) |
517 + FIELD_PREP(SPARE_SIZE_BYTES_MASK, ecc_cfg->spare_bytes);
519 + ecc_cfg->cfg1 = FIELD_PREP(NAND_RECOVERY_CYCLES_MASK, 0) |
520 + FIELD_PREP(CS_ACTIVE_BSY, 0) |
521 + FIELD_PREP(BAD_BLOCK_BYTE_NUM_MASK, bad_block_byte) |
522 + FIELD_PREP(BAD_BLOCK_IN_SPARE_AREA, 0) |
523 + FIELD_PREP(WR_RD_BSY_GAP_MASK, 20) |
524 + FIELD_PREP(WIDE_FLASH, 0) |
525 + FIELD_PREP(ENABLE_BCH_ECC, ecc_cfg->bch_enabled);
527 + ecc_cfg->cfg0_raw = FIELD_PREP(CW_PER_PAGE_MASK, (cwperpage - 1)) |
528 + FIELD_PREP(NUM_ADDR_CYCLES_MASK, 3) |
529 + FIELD_PREP(UD_SIZE_BYTES_MASK, ecc_cfg->cw_size) |
530 + FIELD_PREP(SPARE_SIZE_BYTES_MASK, 0);
532 + ecc_cfg->cfg1_raw = FIELD_PREP(NAND_RECOVERY_CYCLES_MASK, 0) |
533 + FIELD_PREP(CS_ACTIVE_BSY, 0) |
534 + FIELD_PREP(BAD_BLOCK_BYTE_NUM_MASK, 17) |
535 + FIELD_PREP(BAD_BLOCK_IN_SPARE_AREA, 1) |
536 + FIELD_PREP(WR_RD_BSY_GAP_MASK, 20) |
537 + FIELD_PREP(WIDE_FLASH, 0) |
538 + FIELD_PREP(DEV0_CFG1_ECC_DISABLE, 1);
540 + ecc_cfg->ecc_bch_cfg = FIELD_PREP(ECC_CFG_ECC_DISABLE, !ecc_cfg->bch_enabled) |
541 + FIELD_PREP(ECC_SW_RESET, 0) |
542 + FIELD_PREP(ECC_NUM_DATA_BYTES_MASK, ecc_cfg->cw_data) |
543 + FIELD_PREP(ECC_FORCE_CLK_OPEN, 1) |
544 + FIELD_PREP(ECC_MODE_MASK, 0) |
545 + FIELD_PREP(ECC_PARITY_SIZE_BYTES_BCH_MASK, ecc_cfg->ecc_bytes_hw);
547 + ecc_cfg->ecc_buf_cfg = 0x203 << NUM_STEPS;
548 + ecc_cfg->clrflashstatus = FS_READY_BSY_N;
549 + ecc_cfg->clrreadstatus = 0xc0;
551 + conf->step_size = ecc_cfg->step_size;
552 + conf->strength = ecc_cfg->strength;
554 + snandc->regs->erased_cw_detect_cfg_clr = cpu_to_le32(CLR_ERASED_PAGE_DET);
555 + snandc->regs->erased_cw_detect_cfg_set = cpu_to_le32(SET_ERASED_PAGE_DET);
557 + dev_dbg(snandc->dev, "ECC strength: %u bits per %u bytes\n",
558 + ecc_cfg->strength, ecc_cfg->step_size);
563 +static void qcom_spi_ecc_cleanup_ctx_pipelined(struct nand_device *nand)
565 + struct qpic_ecc *ecc_cfg = nand_to_ecc_ctx(nand);
570 +static int qcom_spi_ecc_prepare_io_req_pipelined(struct nand_device *nand,
571 + struct nand_page_io_req *req)
573 + struct qcom_nand_controller *snandc = nand_to_qcom_snand(nand);
574 + struct qpic_ecc *ecc_cfg = nand_to_ecc_ctx(nand);
576 + snandc->qspi->ecc = ecc_cfg;
577 + snandc->qspi->raw_rw = false;
578 + snandc->qspi->oob_rw = false;
579 + snandc->qspi->page_rw = false;
582 + snandc->qspi->page_rw = true;
585 + snandc->qspi->oob_rw = true;
587 + if (req->mode == MTD_OPS_RAW)
588 + snandc->qspi->raw_rw = true;
593 +static int qcom_spi_ecc_finish_io_req_pipelined(struct nand_device *nand,
594 + struct nand_page_io_req *req)
596 + struct qcom_nand_controller *snandc = nand_to_qcom_snand(nand);
597 + struct mtd_info *mtd = nanddev_to_mtd(nand);
599 + if (req->mode == MTD_OPS_RAW || req->type != NAND_PAGE_READ)
602 + if (snandc->qspi->ecc_stats.failed)
603 + mtd->ecc_stats.failed += snandc->qspi->ecc_stats.failed;
605 + mtd->ecc_stats.corrected += snandc->qspi->ecc_stats.corrected;
607 + if (snandc->qspi->ecc_stats.failed)
610 + return snandc->qspi->ecc_stats.bitflips;
613 +static struct nand_ecc_engine_ops qcom_spi_ecc_engine_ops_pipelined = {
614 + .init_ctx = qcom_spi_ecc_init_ctx_pipelined,
615 + .cleanup_ctx = qcom_spi_ecc_cleanup_ctx_pipelined,
616 + .prepare_io_req = qcom_spi_ecc_prepare_io_req_pipelined,
617 + .finish_io_req = qcom_spi_ecc_finish_io_req_pipelined,
620 +/* helper to configure location register values */
621 +static void qcom_spi_set_read_loc(struct qcom_nand_controller *snandc, int cw, int reg,
622 + int cw_offset, int read_size, int is_last_read_loc)
624 + int reg_base = NAND_READ_LOCATION_0;
625 + int num_cw = snandc->qspi->num_cw;
627 + if (cw == (num_cw - 1))
628 + reg_base = NAND_READ_LOCATION_LAST_CW_0;
630 + reg_base += reg * 4;
632 + if (cw == (num_cw - 1))
633 + return qcom_spi_set_read_loc_last(snandc, reg_base, cw_offset,
634 + read_size, is_last_read_loc);
636 + return qcom_spi_set_read_loc_first(snandc, reg_base, cw_offset,
637 + read_size, is_last_read_loc);
641 +qcom_spi_config_cw_read(struct qcom_nand_controller *snandc, bool use_ecc, int cw)
643 + __le32 *reg = &snandc->regs->read_location0;
644 + int num_cw = snandc->qspi->num_cw;
646 + qcom_write_reg_dma(snandc, reg, NAND_READ_LOCATION_0, 4, NAND_BAM_NEXT_SGL);
647 + if (cw == (num_cw - 1)) {
648 + reg = &snandc->regs->read_location_last0;
649 + qcom_write_reg_dma(snandc, reg, NAND_READ_LOCATION_LAST_CW_0, 4,
650 + NAND_BAM_NEXT_SGL);
653 + qcom_write_reg_dma(snandc, &snandc->regs->cmd, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
654 + qcom_write_reg_dma(snandc, &snandc->regs->exec, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
656 + qcom_read_reg_dma(snandc, NAND_FLASH_STATUS, 2, 0);
657 + qcom_read_reg_dma(snandc, NAND_ERASED_CW_DETECT_STATUS, 1,
658 + NAND_BAM_NEXT_SGL);
661 +static int qcom_spi_block_erase(struct qcom_nand_controller *snandc)
663 + struct qpic_ecc *ecc_cfg = snandc->qspi->ecc;
666 + snandc->buf_count = 0;
667 + snandc->buf_start = 0;
668 + qcom_clear_read_regs(snandc);
669 + qcom_clear_bam_transaction(snandc);
671 + snandc->regs->cmd = snandc->qspi->cmd;
672 + snandc->regs->addr0 = snandc->qspi->addr1;
673 + snandc->regs->addr1 = snandc->qspi->addr2;
674 + snandc->regs->cfg0 = cpu_to_le32(ecc_cfg->cfg0_raw & ~(7 << CW_PER_PAGE));
675 + snandc->regs->cfg1 = cpu_to_le32(ecc_cfg->cfg1_raw);
676 + snandc->regs->exec = cpu_to_le32(1);
678 + qcom_write_reg_dma(snandc, &snandc->regs->cmd, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
679 + qcom_write_reg_dma(snandc, &snandc->regs->cfg0, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL);
680 + qcom_write_reg_dma(snandc, &snandc->regs->exec, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
682 + ret = qcom_submit_descs(snandc);
684 + dev_err(snandc->dev, "failure to erase block\n");
691 +static void qcom_spi_config_single_cw_page_read(struct qcom_nand_controller *snandc,
692 + bool use_ecc, int cw)
694 + __le32 *reg = &snandc->regs->read_location0;
695 + int num_cw = snandc->qspi->num_cw;
697 + qcom_write_reg_dma(snandc, &snandc->regs->addr0, NAND_ADDR0, 2, 0);
698 + qcom_write_reg_dma(snandc, &snandc->regs->cfg0, NAND_DEV0_CFG0, 3, 0);
699 + qcom_write_reg_dma(snandc, &snandc->regs->erased_cw_detect_cfg_clr,
700 + NAND_ERASED_CW_DETECT_CFG, 1, 0);
701 + qcom_write_reg_dma(snandc, &snandc->regs->erased_cw_detect_cfg_set,
702 + NAND_ERASED_CW_DETECT_CFG, 1,
703 + NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
705 + if (cw == (num_cw - 1)) {
706 + reg = &snandc->regs->read_location_last0;
707 + qcom_write_reg_dma(snandc, reg, NAND_READ_LOCATION_LAST_CW_0, 4, NAND_BAM_NEXT_SGL);
709 + qcom_write_reg_dma(snandc, &snandc->regs->cmd, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
710 + qcom_write_reg_dma(snandc, &snandc->regs->exec, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
712 + qcom_read_reg_dma(snandc, NAND_FLASH_STATUS, 1, 0);
715 +static int qcom_spi_read_last_cw(struct qcom_nand_controller *snandc,
716 + const struct spi_mem_op *op)
718 + struct qpic_ecc *ecc_cfg = snandc->qspi->ecc;
719 + struct mtd_info *mtd = snandc->qspi->mtd;
722 + u32 cfg0, cfg1, ecc_bch_cfg;
723 + u32 num_cw = snandc->qspi->num_cw;
725 + qcom_clear_bam_transaction(snandc);
726 + qcom_clear_read_regs(snandc);
728 + size = ecc_cfg->cw_size;
729 + col = ecc_cfg->cw_size * (num_cw - 1);
731 + memset(snandc->data_buffer, 0xff, size);
732 + snandc->regs->addr0 = (snandc->qspi->addr1 | cpu_to_le32(col));
733 + snandc->regs->addr1 = snandc->qspi->addr2;
735 + cfg0 = (ecc_cfg->cfg0_raw & ~(7U << CW_PER_PAGE)) |
737 + cfg1 = ecc_cfg->cfg1_raw;
738 + ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
740 + snandc->regs->cmd = snandc->qspi->cmd;
741 + snandc->regs->cfg0 = cpu_to_le32(cfg0);
742 + snandc->regs->cfg1 = cpu_to_le32(cfg1);
743 + snandc->regs->ecc_bch_cfg = cpu_to_le32(ecc_bch_cfg);
744 + snandc->regs->clrflashstatus = cpu_to_le32(ecc_cfg->clrflashstatus);
745 + snandc->regs->clrreadstatus = cpu_to_le32(ecc_cfg->clrreadstatus);
746 + snandc->regs->exec = cpu_to_le32(1);
748 + qcom_spi_set_read_loc(snandc, num_cw - 1, 0, 0, ecc_cfg->cw_size, 1);
750 + qcom_spi_config_single_cw_page_read(snandc, false, num_cw - 1);
752 + qcom_read_data_dma(snandc, FLASH_BUF_ACC, snandc->data_buffer, size, 0);
754 + ret = qcom_submit_descs(snandc);
756 + dev_err(snandc->dev, "failed to read last cw\n");
760 + qcom_nandc_dev_to_mem(snandc, true);
761 + u32 flash = le32_to_cpu(snandc->reg_read_buf[0]);
763 + if (flash & (FS_OP_ERR | FS_MPU_ERR))
766 + bbpos = mtd->writesize - ecc_cfg->cw_size * (num_cw - 1);
768 + if (snandc->data_buffer[bbpos] == 0xff)
769 + snandc->data_buffer[bbpos + 1] = 0xff;
770 + if (snandc->data_buffer[bbpos] != 0xff)
771 + snandc->data_buffer[bbpos + 1] = snandc->data_buffer[bbpos];
773 + memcpy(op->data.buf.in, snandc->data_buffer + bbpos, op->data.nbytes);
778 +static int qcom_spi_check_error(struct qcom_nand_controller *snandc, u8 *data_buf, u8 *oob_buf)
780 + struct snandc_read_status *buf;
781 + struct qpic_ecc *ecc_cfg = snandc->qspi->ecc;
782 + int i, num_cw = snandc->qspi->num_cw;
783 + bool flash_op_err = false, erased;
784 + unsigned int max_bitflips = 0;
785 + unsigned int uncorrectable_cws = 0;
787 + snandc->qspi->ecc_stats.failed = 0;
788 + snandc->qspi->ecc_stats.corrected = 0;
790 + qcom_nandc_dev_to_mem(snandc, true);
791 + buf = (struct snandc_read_status *)snandc->reg_read_buf;
793 + for (i = 0; i < num_cw; i++, buf++) {
794 + u32 flash, buffer, erased_cw;
795 + int data_len, oob_len;
797 + if (i == (num_cw - 1)) {
798 + data_len = NANDC_STEP_SIZE - ((num_cw - 1) << 2);
799 + oob_len = num_cw << 2;
801 + data_len = ecc_cfg->cw_data;
805 + flash = le32_to_cpu(buf->snandc_flash);
806 + buffer = le32_to_cpu(buf->snandc_buffer);
807 + erased_cw = le32_to_cpu(buf->snandc_erased_cw);
809 + if ((flash & FS_OP_ERR) && (buffer & BS_UNCORRECTABLE_BIT)) {
810 + if (ecc_cfg->bch_enabled)
811 + erased = (erased_cw & ERASED_CW) == ERASED_CW;
816 + uncorrectable_cws |= BIT(i);
818 + } else if (flash & (FS_OP_ERR | FS_MPU_ERR)) {
819 + flash_op_err = true;
823 + stat = buffer & BS_CORRECTABLE_ERR_MSK;
824 + snandc->qspi->ecc_stats.corrected += stat;
825 + max_bitflips = max(max_bitflips, stat);
829 + data_buf += data_len;
831 + oob_buf += oob_len + ecc_cfg->bytes;
837 + if (!uncorrectable_cws)
838 + snandc->qspi->ecc_stats.bitflips = max_bitflips;
840 + snandc->qspi->ecc_stats.failed++;
845 +static int qcom_spi_check_raw_flash_errors(struct qcom_nand_controller *snandc, int cw_cnt)
849 + qcom_nandc_dev_to_mem(snandc, true);
851 + for (i = 0; i < cw_cnt; i++) {
852 + u32 flash = le32_to_cpu(snandc->reg_read_buf[i]);
854 + if (flash & (FS_OP_ERR | FS_MPU_ERR))
861 +static int qcom_spi_read_cw_raw(struct qcom_nand_controller *snandc, u8 *data_buf,
862 + u8 *oob_buf, int cw)
864 + struct qpic_ecc *ecc_cfg = snandc->qspi->ecc;
865 + struct mtd_info *mtd = snandc->qspi->mtd;
866 + int data_size1, data_size2, oob_size1, oob_size2;
867 + int ret, reg_off = FLASH_BUF_ACC, read_loc = 0;
869 + u32 cfg0, cfg1, ecc_bch_cfg, num_cw = snandc->qspi->num_cw;
872 + snandc->buf_count = 0;
873 + snandc->buf_start = 0;
874 + qcom_clear_read_regs(snandc);
875 + qcom_clear_bam_transaction(snandc);
876 + raw_cw = num_cw - 1;
878 + cfg0 = (ecc_cfg->cfg0_raw & ~(7U << CW_PER_PAGE)) |
880 + cfg1 = ecc_cfg->cfg1_raw;
881 + ecc_bch_cfg = ECC_CFG_ECC_DISABLE;
883 + col = ecc_cfg->cw_size * cw;
885 + snandc->regs->addr0 = (snandc->qspi->addr1 | cpu_to_le32(col));
886 + snandc->regs->addr1 = snandc->qspi->addr2;
887 + snandc->regs->cmd = snandc->qspi->cmd;
888 + snandc->regs->cfg0 = cpu_to_le32(cfg0);
889 + snandc->regs->cfg1 = cpu_to_le32(cfg1);
890 + snandc->regs->ecc_bch_cfg = cpu_to_le32(ecc_bch_cfg);
891 + snandc->regs->clrflashstatus = cpu_to_le32(ecc_cfg->clrflashstatus);
892 + snandc->regs->clrreadstatus = cpu_to_le32(ecc_cfg->clrreadstatus);
893 + snandc->regs->exec = cpu_to_le32(1);
895 + qcom_spi_set_read_loc(snandc, raw_cw, 0, 0, ecc_cfg->cw_size, 1);
897 + qcom_write_reg_dma(snandc, &snandc->regs->addr0, NAND_ADDR0, 2, 0);
898 + qcom_write_reg_dma(snandc, &snandc->regs->cfg0, NAND_DEV0_CFG0, 3, 0);
899 + qcom_write_reg_dma(snandc, &snandc->regs->ecc_buf_cfg, NAND_EBI2_ECC_BUF_CFG, 1, 0);
901 + qcom_write_reg_dma(snandc, &snandc->regs->erased_cw_detect_cfg_clr,
902 + NAND_ERASED_CW_DETECT_CFG, 1, 0);
903 + qcom_write_reg_dma(snandc, &snandc->regs->erased_cw_detect_cfg_set,
904 + NAND_ERASED_CW_DETECT_CFG, 1,
905 + NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
907 + data_size1 = mtd->writesize - ecc_cfg->cw_size * (num_cw - 1);
908 + oob_size1 = ecc_cfg->bbm_size;
910 + if (cw == (num_cw - 1)) {
911 + data_size2 = NANDC_STEP_SIZE - data_size1 -
912 + ((num_cw - 1) * 4);
913 + oob_size2 = (num_cw * 4) + ecc_cfg->ecc_bytes_hw +
914 + ecc_cfg->spare_bytes;
916 + data_size2 = ecc_cfg->cw_data - data_size1;
917 + oob_size2 = ecc_cfg->ecc_bytes_hw + ecc_cfg->spare_bytes;
920 + qcom_spi_set_read_loc(snandc, cw, 0, read_loc, data_size1, 0);
921 + read_loc += data_size1;
923 + qcom_spi_set_read_loc(snandc, cw, 1, read_loc, oob_size1, 0);
924 + read_loc += oob_size1;
926 + qcom_spi_set_read_loc(snandc, cw, 2, read_loc, data_size2, 0);
927 + read_loc += data_size2;
929 + qcom_spi_set_read_loc(snandc, cw, 3, read_loc, oob_size2, 1);
931 + qcom_spi_config_cw_read(snandc, false, raw_cw);
933 + qcom_read_data_dma(snandc, reg_off, data_buf, data_size1, 0);
934 + reg_off += data_size1;
936 + qcom_read_data_dma(snandc, reg_off, oob_buf, oob_size1, 0);
937 + reg_off += oob_size1;
939 + qcom_read_data_dma(snandc, reg_off, data_buf + data_size1, data_size2, 0);
940 + reg_off += data_size2;
942 + qcom_read_data_dma(snandc, reg_off, oob_buf + oob_size1, oob_size2, 0);
944 + ret = qcom_submit_descs(snandc);
946 + dev_err(snandc->dev, "failure to read raw cw %d\n", cw);
950 + return qcom_spi_check_raw_flash_errors(snandc, 1);
953 +static int qcom_spi_read_page_raw(struct qcom_nand_controller *snandc,
954 + const struct spi_mem_op *op)
956 + struct qpic_ecc *ecc_cfg = snandc->qspi->ecc;
957 + u8 *data_buf = NULL, *oob_buf = NULL;
959 + u32 num_cw = snandc->qspi->num_cw;
961 + if (snandc->qspi->page_rw)
962 + data_buf = op->data.buf.in;
964 + oob_buf = snandc->qspi->oob_buf;
965 + memset(oob_buf, 0xff, OOB_BUF_SIZE);
967 + for (cw = 0; cw < num_cw; cw++) {
968 + ret = qcom_spi_read_cw_raw(snandc, data_buf, oob_buf, cw);
973 + data_buf += ecc_cfg->cw_data;
975 + oob_buf += ecc_cfg->bytes;
981 +static int qcom_spi_read_page_ecc(struct qcom_nand_controller *snandc,
982 + const struct spi_mem_op *op)
984 + struct qpic_ecc *ecc_cfg = snandc->qspi->ecc;
985 + u8 *data_buf = NULL, *data_buf_start, *oob_buf = NULL, *oob_buf_start;
987 + u32 cfg0, cfg1, ecc_bch_cfg, num_cw = snandc->qspi->num_cw;
989 + data_buf = op->data.buf.in;
990 + data_buf_start = data_buf;
992 + oob_buf = snandc->qspi->oob_buf;
993 + oob_buf_start = oob_buf;
995 + snandc->buf_count = 0;
996 + snandc->buf_start = 0;
997 + qcom_clear_read_regs(snandc);
999 + cfg0 = (ecc_cfg->cfg0 & ~(7U << CW_PER_PAGE)) |
1000 + (num_cw - 1) << CW_PER_PAGE;
1001 + cfg1 = ecc_cfg->cfg1;
1002 + ecc_bch_cfg = ecc_cfg->ecc_bch_cfg;
1004 + snandc->regs->addr0 = snandc->qspi->addr1;
1005 + snandc->regs->addr1 = snandc->qspi->addr2;
1006 + snandc->regs->cmd = snandc->qspi->cmd;
1007 + snandc->regs->cfg0 = cpu_to_le32(cfg0);
1008 + snandc->regs->cfg1 = cpu_to_le32(cfg1);
1009 + snandc->regs->ecc_bch_cfg = cpu_to_le32(ecc_bch_cfg);
1010 + snandc->regs->clrflashstatus = cpu_to_le32(ecc_cfg->clrflashstatus);
1011 + snandc->regs->clrreadstatus = cpu_to_le32(ecc_cfg->clrreadstatus);
1012 + snandc->regs->exec = cpu_to_le32(1);
1014 + qcom_spi_set_read_loc(snandc, 0, 0, 0, ecc_cfg->cw_data, 1);
1016 + qcom_clear_bam_transaction(snandc);
1018 + qcom_write_reg_dma(snandc, &snandc->regs->addr0, NAND_ADDR0, 2, 0);
1019 + qcom_write_reg_dma(snandc, &snandc->regs->cfg0, NAND_DEV0_CFG0, 3, 0);
1020 + qcom_write_reg_dma(snandc, &snandc->regs->erased_cw_detect_cfg_clr,
1021 + NAND_ERASED_CW_DETECT_CFG, 1, 0);
1022 + qcom_write_reg_dma(snandc, &snandc->regs->erased_cw_detect_cfg_set,
1023 + NAND_ERASED_CW_DETECT_CFG, 1,
1024 + NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
1026 + for (i = 0; i < num_cw; i++) {
1027 + int data_size, oob_size;
1029 + if (i == (num_cw - 1)) {
1030 + data_size = 512 - ((num_cw - 1) << 2);
1031 + oob_size = (num_cw << 2) + ecc_cfg->ecc_bytes_hw +
1032 + ecc_cfg->spare_bytes;
1034 + data_size = ecc_cfg->cw_data;
1035 + oob_size = ecc_cfg->ecc_bytes_hw + ecc_cfg->spare_bytes;
1038 + if (data_buf && oob_buf) {
1039 + qcom_spi_set_read_loc(snandc, i, 0, 0, data_size, 0);
1040 + qcom_spi_set_read_loc(snandc, i, 1, data_size, oob_size, 1);
1041 + } else if (data_buf) {
1042 + qcom_spi_set_read_loc(snandc, i, 0, 0, data_size, 1);
1044 + qcom_spi_set_read_loc(snandc, i, 0, data_size, oob_size, 1);
1047 + qcom_spi_config_cw_read(snandc, true, i);
1050 + qcom_read_data_dma(snandc, FLASH_BUF_ACC, data_buf,
1055 + for (j = 0; j < ecc_cfg->bbm_size; j++)
1056 + *oob_buf++ = 0xff;
1058 + qcom_read_data_dma(snandc, FLASH_BUF_ACC + data_size,
1059 + oob_buf, oob_size, 0);
1063 + data_buf += data_size;
1065 + oob_buf += oob_size;
1068 + ret = qcom_submit_descs(snandc);
1070 + dev_err(snandc->dev, "failure to read page\n");
1074 + return qcom_spi_check_error(snandc, data_buf_start, oob_buf_start);
1077 +static int qcom_spi_read_page_oob(struct qcom_nand_controller *snandc,
1078 + const struct spi_mem_op *op)
1080 + struct qpic_ecc *ecc_cfg = snandc->qspi->ecc;
1081 + u8 *data_buf = NULL, *data_buf_start, *oob_buf = NULL, *oob_buf_start;
1083 + u32 cfg0, cfg1, ecc_bch_cfg, num_cw = snandc->qspi->num_cw;
1085 + oob_buf = op->data.buf.in;
1086 + oob_buf_start = oob_buf;
1088 + data_buf_start = data_buf;
1090 + snandc->buf_count = 0;
1091 + snandc->buf_start = 0;
1092 + qcom_clear_read_regs(snandc);
1093 + qcom_clear_bam_transaction(snandc);
1095 + cfg0 = (ecc_cfg->cfg0 & ~(7U << CW_PER_PAGE)) |
1096 + (num_cw - 1) << CW_PER_PAGE;
1097 + cfg1 = ecc_cfg->cfg1;
1098 + ecc_bch_cfg = ecc_cfg->ecc_bch_cfg;
1100 + snandc->regs->addr0 = snandc->qspi->addr1;
1101 + snandc->regs->addr1 = snandc->qspi->addr2;
1102 + snandc->regs->cmd = snandc->qspi->cmd;
1103 + snandc->regs->cfg0 = cpu_to_le32(cfg0);
1104 + snandc->regs->cfg1 = cpu_to_le32(cfg1);
1105 + snandc->regs->ecc_bch_cfg = cpu_to_le32(ecc_bch_cfg);
1106 + snandc->regs->clrflashstatus = cpu_to_le32(ecc_cfg->clrflashstatus);
1107 + snandc->regs->clrreadstatus = cpu_to_le32(ecc_cfg->clrreadstatus);
1108 + snandc->regs->exec = cpu_to_le32(1);
1110 + qcom_spi_set_read_loc(snandc, 0, 0, 0, ecc_cfg->cw_data, 1);
1112 + qcom_write_reg_dma(snandc, &snandc->regs->addr0, NAND_ADDR0, 2, 0);
1113 + qcom_write_reg_dma(snandc, &snandc->regs->cfg0, NAND_DEV0_CFG0, 3, 0);
1114 + qcom_write_reg_dma(snandc, &snandc->regs->erased_cw_detect_cfg_clr,
1115 + NAND_ERASED_CW_DETECT_CFG, 1, 0);
1116 + qcom_write_reg_dma(snandc, &snandc->regs->erased_cw_detect_cfg_set,
1117 + NAND_ERASED_CW_DETECT_CFG, 1,
1118 + NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL);
1120 + for (i = 0; i < num_cw; i++) {
1121 + int data_size, oob_size;
1123 + if (i == (num_cw - 1)) {
1124 + data_size = NANDC_STEP_SIZE - ((num_cw - 1) << 2);
1125 + oob_size = (num_cw << 2) + ecc_cfg->ecc_bytes_hw +
1126 + ecc_cfg->spare_bytes;
1128 + data_size = ecc_cfg->cw_data;
1129 + oob_size = ecc_cfg->ecc_bytes_hw + ecc_cfg->spare_bytes;
1132 + qcom_spi_set_read_loc(snandc, i, 0, data_size, oob_size, 1);
1134 + qcom_spi_config_cw_read(snandc, true, i);
1139 + for (j = 0; j < ecc_cfg->bbm_size; j++)
1140 + *oob_buf++ = 0xff;
1142 + qcom_read_data_dma(snandc, FLASH_BUF_ACC + data_size,
1143 + oob_buf, oob_size, 0);
1147 + oob_buf += oob_size;
1150 + ret = qcom_submit_descs(snandc);
1152 + dev_err(snandc->dev, "failure to read oob\n");
1156 + return qcom_spi_check_error(snandc, data_buf_start, oob_buf_start);
1159 +static int qcom_spi_read_page(struct qcom_nand_controller *snandc,
1160 + const struct spi_mem_op *op)
1162 + if (snandc->qspi->page_rw && snandc->qspi->raw_rw)
1163 + return qcom_spi_read_page_raw(snandc, op);
1165 + if (snandc->qspi->page_rw)
1166 + return qcom_spi_read_page_ecc(snandc, op);
1168 + if (snandc->qspi->oob_rw && snandc->qspi->raw_rw)
1169 + return qcom_spi_read_last_cw(snandc, op);
1171 + if (snandc->qspi->oob_rw)
1172 + return qcom_spi_read_page_oob(snandc, op);
1177 +static void qcom_spi_config_page_write(struct qcom_nand_controller *snandc)
1179 + qcom_write_reg_dma(snandc, &snandc->regs->addr0, NAND_ADDR0, 2, 0);
1180 + qcom_write_reg_dma(snandc, &snandc->regs->cfg0, NAND_DEV0_CFG0, 3, 0);
1181 + qcom_write_reg_dma(snandc, &snandc->regs->ecc_buf_cfg, NAND_EBI2_ECC_BUF_CFG,
1182 + 1, NAND_BAM_NEXT_SGL);
1185 +static void qcom_spi_config_cw_write(struct qcom_nand_controller *snandc)
1187 + qcom_write_reg_dma(snandc, &snandc->regs->cmd, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL);
1188 + qcom_write_reg_dma(snandc, &snandc->regs->exec, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1189 + qcom_read_reg_dma(snandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL);
1191 + qcom_write_reg_dma(snandc, &snandc->regs->clrflashstatus, NAND_FLASH_STATUS, 1, 0);
1192 + qcom_write_reg_dma(snandc, &snandc->regs->clrreadstatus, NAND_READ_STATUS, 1,
1193 + NAND_BAM_NEXT_SGL);
1196 +static int qcom_spi_program_raw(struct qcom_nand_controller *snandc,
1197 + const struct spi_mem_op *op)
1199 + struct qpic_ecc *ecc_cfg = snandc->qspi->ecc;
1200 + struct mtd_info *mtd = snandc->qspi->mtd;
1201 + u8 *data_buf = NULL, *oob_buf = NULL;
1203 + int num_cw = snandc->qspi->num_cw;
1204 + u32 cfg0, cfg1, ecc_bch_cfg;
1206 + cfg0 = (ecc_cfg->cfg0_raw & ~(7U << CW_PER_PAGE)) |
1207 + (num_cw - 1) << CW_PER_PAGE;
1208 + cfg1 = ecc_cfg->cfg1_raw;
1209 + ecc_bch_cfg = ECC_CFG_ECC_DISABLE;
1211 + data_buf = snandc->qspi->data_buf;
1213 + oob_buf = snandc->qspi->oob_buf;
1214 + memset(oob_buf, 0xff, OOB_BUF_SIZE);
1216 + snandc->buf_count = 0;
1217 + snandc->buf_start = 0;
1218 + qcom_clear_read_regs(snandc);
1219 + qcom_clear_bam_transaction(snandc);
1221 + snandc->regs->addr0 = snandc->qspi->addr1;
1222 + snandc->regs->addr1 = snandc->qspi->addr2;
1223 + snandc->regs->cmd = snandc->qspi->cmd;
1224 + snandc->regs->cfg0 = cpu_to_le32(cfg0);
1225 + snandc->regs->cfg1 = cpu_to_le32(cfg1);
1226 + snandc->regs->ecc_bch_cfg = cpu_to_le32(ecc_bch_cfg);
1227 + snandc->regs->clrflashstatus = cpu_to_le32(ecc_cfg->clrflashstatus);
1228 + snandc->regs->clrreadstatus = cpu_to_le32(ecc_cfg->clrreadstatus);
1229 + snandc->regs->exec = cpu_to_le32(1);
1231 + qcom_spi_config_page_write(snandc);
1233 + for (i = 0; i < num_cw; i++) {
1234 + int data_size1, data_size2, oob_size1, oob_size2;
1235 + int reg_off = FLASH_BUF_ACC;
1237 + data_size1 = mtd->writesize - ecc_cfg->cw_size * (num_cw - 1);
1238 + oob_size1 = ecc_cfg->bbm_size;
1240 + if (i == (num_cw - 1)) {
1241 + data_size2 = NANDC_STEP_SIZE - data_size1 -
1242 + ((num_cw - 1) << 2);
1243 + oob_size2 = (num_cw << 2) + ecc_cfg->ecc_bytes_hw +
1244 + ecc_cfg->spare_bytes;
1246 + data_size2 = ecc_cfg->cw_data - data_size1;
1247 + oob_size2 = ecc_cfg->ecc_bytes_hw + ecc_cfg->spare_bytes;
1250 + qcom_write_data_dma(snandc, reg_off, data_buf, data_size1,
1252 + reg_off += data_size1;
1253 + data_buf += data_size1;
1255 + qcom_write_data_dma(snandc, reg_off, oob_buf, oob_size1,
1257 + oob_buf += oob_size1;
1258 + reg_off += oob_size1;
1260 + qcom_write_data_dma(snandc, reg_off, data_buf, data_size2,
1262 + reg_off += data_size2;
1263 + data_buf += data_size2;
1265 + qcom_write_data_dma(snandc, reg_off, oob_buf, oob_size2, 0);
1266 + oob_buf += oob_size2;
1268 + qcom_spi_config_cw_write(snandc);
1271 + ret = qcom_submit_descs(snandc);
1273 + dev_err(snandc->dev, "failure to write raw page\n");
1280 +static int qcom_spi_program_ecc(struct qcom_nand_controller *snandc,
1281 + const struct spi_mem_op *op)
1283 + struct qpic_ecc *ecc_cfg = snandc->qspi->ecc;
1284 + u8 *data_buf = NULL, *oob_buf = NULL;
1286 + int num_cw = snandc->qspi->num_cw;
1287 + u32 cfg0, cfg1, ecc_bch_cfg, ecc_buf_cfg;
1289 + cfg0 = (ecc_cfg->cfg0 & ~(7U << CW_PER_PAGE)) |
1290 + (num_cw - 1) << CW_PER_PAGE;
1291 + cfg1 = ecc_cfg->cfg1;
1292 + ecc_bch_cfg = ecc_cfg->ecc_bch_cfg;
1293 + ecc_buf_cfg = ecc_cfg->ecc_buf_cfg;
1295 + if (snandc->qspi->data_buf)
1296 + data_buf = snandc->qspi->data_buf;
1298 + oob_buf = snandc->qspi->oob_buf;
1300 + snandc->buf_count = 0;
1301 + snandc->buf_start = 0;
1302 + qcom_clear_read_regs(snandc);
1303 + qcom_clear_bam_transaction(snandc);
1305 + snandc->regs->addr0 = snandc->qspi->addr1;
1306 + snandc->regs->addr1 = snandc->qspi->addr2;
1307 + snandc->regs->cmd = snandc->qspi->cmd;
1308 + snandc->regs->cfg0 = cpu_to_le32(cfg0);
1309 + snandc->regs->cfg1 = cpu_to_le32(cfg1);
1310 + snandc->regs->ecc_bch_cfg = cpu_to_le32(ecc_bch_cfg);
1311 + snandc->regs->ecc_buf_cfg = cpu_to_le32(ecc_buf_cfg);
1312 + snandc->regs->exec = cpu_to_le32(1);
1314 + qcom_spi_config_page_write(snandc);
1316 + for (i = 0; i < num_cw; i++) {
1317 + int data_size, oob_size;
1319 + if (i == (num_cw - 1)) {
1320 + data_size = NANDC_STEP_SIZE - ((num_cw - 1) << 2);
1321 + oob_size = (num_cw << 2) + ecc_cfg->ecc_bytes_hw +
1322 + ecc_cfg->spare_bytes;
1324 + data_size = ecc_cfg->cw_data;
1325 + oob_size = ecc_cfg->bytes;
1329 + qcom_write_data_dma(snandc, FLASH_BUF_ACC, data_buf, data_size,
1330 + i == (num_cw - 1) ? NAND_BAM_NO_EOT : 0);
1332 + if (i == (num_cw - 1)) {
1334 + oob_buf += ecc_cfg->bbm_size;
1335 + qcom_write_data_dma(snandc, FLASH_BUF_ACC + data_size,
1336 + oob_buf, oob_size, 0);
1340 + qcom_spi_config_cw_write(snandc);
1343 + data_buf += data_size;
1345 + oob_buf += oob_size;
1348 + ret = qcom_submit_descs(snandc);
1350 + dev_err(snandc->dev, "failure to write page\n");
1357 +static int qcom_spi_program_oob(struct qcom_nand_controller *snandc,
1358 + const struct spi_mem_op *op)
1360 + struct qpic_ecc *ecc_cfg = snandc->qspi->ecc;
1361 + u8 *oob_buf = NULL;
1362 + int ret, col, data_size, oob_size;
1363 + int num_cw = snandc->qspi->num_cw;
1364 + u32 cfg0, cfg1, ecc_bch_cfg, ecc_buf_cfg;
1366 + cfg0 = (ecc_cfg->cfg0 & ~(7U << CW_PER_PAGE)) |
1367 + (num_cw - 1) << CW_PER_PAGE;
1368 + cfg1 = ecc_cfg->cfg1;
1369 + ecc_bch_cfg = ecc_cfg->ecc_bch_cfg;
1370 + ecc_buf_cfg = ecc_cfg->ecc_buf_cfg;
1372 + col = ecc_cfg->cw_size * (num_cw - 1);
1374 + oob_buf = snandc->qspi->data_buf;
1376 + snandc->buf_count = 0;
1377 + snandc->buf_start = 0;
1378 + qcom_clear_read_regs(snandc);
1379 + qcom_clear_bam_transaction(snandc);
1380 + snandc->regs->addr0 = (snandc->qspi->addr1 | cpu_to_le32(col));
1381 + snandc->regs->addr1 = snandc->qspi->addr2;
1382 + snandc->regs->cmd = snandc->qspi->cmd;
1383 + snandc->regs->cfg0 = cpu_to_le32(cfg0);
1384 + snandc->regs->cfg1 = cpu_to_le32(cfg1);
1385 + snandc->regs->ecc_bch_cfg = cpu_to_le32(ecc_bch_cfg);
1386 + snandc->regs->ecc_buf_cfg = cpu_to_le32(ecc_buf_cfg);
1387 + snandc->regs->exec = cpu_to_le32(1);
1389 + /* calculate the data and oob size for the last codeword/step */
1390 + data_size = NANDC_STEP_SIZE - ((num_cw - 1) << 2);
1391 + oob_size = snandc->qspi->mtd->oobavail;
1393 + memset(snandc->data_buffer, 0xff, ecc_cfg->cw_data);
1394 + /* override new oob content to last codeword */
1395 + mtd_ooblayout_get_databytes(snandc->qspi->mtd, snandc->data_buffer + data_size,
1396 + oob_buf, 0, snandc->qspi->mtd->oobavail);
1397 + qcom_spi_config_page_write(snandc);
1398 + qcom_write_data_dma(snandc, FLASH_BUF_ACC, snandc->data_buffer, data_size + oob_size, 0);
1399 + qcom_spi_config_cw_write(snandc);
1401 + ret = qcom_submit_descs(snandc);
1403 + dev_err(snandc->dev, "failure to write oob\n");
1410 +static int qcom_spi_program_execute(struct qcom_nand_controller *snandc,
1411 + const struct spi_mem_op *op)
1413 + if (snandc->qspi->page_rw && snandc->qspi->raw_rw)
1414 + return qcom_spi_program_raw(snandc, op);
1416 + if (snandc->qspi->page_rw)
1417 + return qcom_spi_program_ecc(snandc, op);
1419 + if (snandc->qspi->oob_rw)
1420 + return qcom_spi_program_oob(snandc, op);
1425 +static u32 qcom_spi_cmd_mapping(struct qcom_nand_controller *snandc, u32 opcode)
1430 + case SPINAND_RESET:
1431 + cmd = (SPI_WP | SPI_HOLD | SPI_TRANSFER_MODE_x1 | OP_RESET_DEVICE);
1433 + case SPINAND_READID:
1434 + cmd = (SPI_WP | SPI_HOLD | SPI_TRANSFER_MODE_x1 | OP_FETCH_ID);
1436 + case SPINAND_GET_FEATURE:
1437 + cmd = (SPI_TRANSFER_MODE_x1 | SPI_WP | SPI_HOLD | ACC_FEATURE);
1439 + case SPINAND_SET_FEATURE:
1440 + cmd = (SPI_TRANSFER_MODE_x1 | SPI_WP | SPI_HOLD | ACC_FEATURE |
1441 + QPIC_SET_FEATURE);
1443 + case SPINAND_READ:
1444 + if (snandc->qspi->raw_rw) {
1445 + cmd = (PAGE_ACC | LAST_PAGE | SPI_TRANSFER_MODE_x1 |
1446 + SPI_WP | SPI_HOLD | OP_PAGE_READ);
1448 + cmd = (PAGE_ACC | LAST_PAGE | SPI_TRANSFER_MODE_x1 |
1449 + SPI_WP | SPI_HOLD | OP_PAGE_READ_WITH_ECC);
1453 + case SPINAND_ERASE:
1454 + cmd = OP_BLOCK_ERASE | PAGE_ACC | LAST_PAGE | SPI_WP |
1455 + SPI_HOLD | SPI_TRANSFER_MODE_x1;
1457 + case SPINAND_WRITE_EN:
1458 + cmd = SPINAND_WRITE_EN;
1460 + case SPINAND_PROGRAM_EXECUTE:
1461 + cmd = (PAGE_ACC | LAST_PAGE | SPI_TRANSFER_MODE_x1 |
1462 + SPI_WP | SPI_HOLD | OP_PROGRAM_PAGE);
1464 + case SPINAND_PROGRAM_LOAD:
1465 + cmd = SPINAND_PROGRAM_LOAD;
1468 + dev_err(snandc->dev, "Opcode not supported: %u\n", opcode);
1469 + return -EOPNOTSUPP;
1475 +static int qcom_spi_write_page(struct qcom_nand_controller *snandc,
1476 + const struct spi_mem_op *op)
1478 + struct qpic_snand_op s_op = {};
1481 + cmd = qcom_spi_cmd_mapping(snandc, op->cmd.opcode);
1485 + s_op.cmd_reg = cmd;
1487 + if (op->cmd.opcode == SPINAND_PROGRAM_LOAD)
1488 + snandc->qspi->data_buf = (u8 *)op->data.buf.out;
1493 +static int qcom_spi_send_cmdaddr(struct qcom_nand_controller *snandc,
1494 + const struct spi_mem_op *op)
1496 + struct qpic_snand_op s_op = {};
1500 + cmd = qcom_spi_cmd_mapping(snandc, op->cmd.opcode);
1504 + s_op.cmd_reg = cmd;
1505 + s_op.addr1_reg = op->addr.val;
1506 + s_op.addr2_reg = 0;
1508 + opcode = op->cmd.opcode;
1511 + case SPINAND_WRITE_EN:
1513 + case SPINAND_PROGRAM_EXECUTE:
1514 + s_op.addr1_reg = op->addr.val << 16;
1515 + s_op.addr2_reg = op->addr.val >> 16 & 0xff;
1516 + snandc->qspi->addr1 = cpu_to_le32(s_op.addr1_reg);
1517 + snandc->qspi->addr2 = cpu_to_le32(s_op.addr2_reg);
1518 + snandc->qspi->cmd = cpu_to_le32(cmd);
1519 + return qcom_spi_program_execute(snandc, op);
1520 + case SPINAND_READ:
1521 + s_op.addr1_reg = (op->addr.val << 16);
1522 + s_op.addr2_reg = op->addr.val >> 16 & 0xff;
1523 + snandc->qspi->addr1 = cpu_to_le32(s_op.addr1_reg);
1524 + snandc->qspi->addr2 = cpu_to_le32(s_op.addr2_reg);
1525 + snandc->qspi->cmd = cpu_to_le32(cmd);
1527 + case SPINAND_ERASE:
1528 + s_op.addr2_reg = (op->addr.val >> 16) & 0xffff;
1529 + s_op.addr1_reg = op->addr.val;
1530 + snandc->qspi->addr1 = cpu_to_le32(s_op.addr1_reg << 16);
1531 + snandc->qspi->addr2 = cpu_to_le32(s_op.addr2_reg);
1532 + snandc->qspi->cmd = cpu_to_le32(cmd);
1533 + qcom_spi_block_erase(snandc);
1539 + snandc->buf_count = 0;
1540 + snandc->buf_start = 0;
1541 + qcom_clear_read_regs(snandc);
1542 + qcom_clear_bam_transaction(snandc);
1544 + snandc->regs->cmd = cpu_to_le32(s_op.cmd_reg);
1545 + snandc->regs->exec = cpu_to_le32(1);
1546 + snandc->regs->addr0 = cpu_to_le32(s_op.addr1_reg);
1547 + snandc->regs->addr1 = cpu_to_le32(s_op.addr2_reg);
1549 + qcom_write_reg_dma(snandc, &snandc->regs->cmd, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL);
1550 + qcom_write_reg_dma(snandc, &snandc->regs->exec, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL);
1552 + ret = qcom_submit_descs(snandc);
1554 + dev_err(snandc->dev, "failure in submitting cmd descriptor\n");
1559 +static int qcom_spi_io_op(struct qcom_nand_controller *snandc, const struct spi_mem_op *op)
1561 + int ret, val, opcode;
1562 + bool copy = false, copy_ftr = false;
1564 + ret = qcom_spi_send_cmdaddr(snandc, op);
1568 + snandc->buf_count = 0;
1569 + snandc->buf_start = 0;
1570 + qcom_clear_read_regs(snandc);
1571 + qcom_clear_bam_transaction(snandc);
1572 + opcode = op->cmd.opcode;
1575 + case SPINAND_READID:
1576 + snandc->buf_count = 4;
1577 + qcom_read_reg_dma(snandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL);
1580 + case SPINAND_GET_FEATURE:
1581 + snandc->buf_count = 4;
1582 + qcom_read_reg_dma(snandc, NAND_FLASH_FEATURES, 1, NAND_BAM_NEXT_SGL);
1585 + case SPINAND_SET_FEATURE:
1586 + snandc->regs->flash_feature = cpu_to_le32(*(u32 *)op->data.buf.out);
1587 + qcom_write_reg_dma(snandc, &snandc->regs->flash_feature,
1588 + NAND_FLASH_FEATURES, 1, NAND_BAM_NEXT_SGL);
1590 + case SPINAND_PROGRAM_EXECUTE:
1591 + case SPINAND_WRITE_EN:
1592 + case SPINAND_RESET:
1593 + case SPINAND_ERASE:
1594 + case SPINAND_READ:
1597 + return -EOPNOTSUPP;
1600 + ret = qcom_submit_descs(snandc);
1602 + dev_err(snandc->dev, "failure in submitting descriptor for:%d\n", opcode);
1605 + qcom_nandc_dev_to_mem(snandc, true);
1606 + memcpy(op->data.buf.in, snandc->reg_read_buf, snandc->buf_count);
1610 + qcom_nandc_dev_to_mem(snandc, true);
1611 + val = le32_to_cpu(*(__le32 *)snandc->reg_read_buf);
1613 + memcpy(op->data.buf.in, &val, snandc->buf_count);
1619 +static bool qcom_spi_is_page_op(const struct spi_mem_op *op)
1621 + if (op->addr.buswidth != 1 && op->addr.buswidth != 2 && op->addr.buswidth != 4)
1624 + if (op->data.dir == SPI_MEM_DATA_IN) {
1625 + if (op->addr.buswidth == 4 && op->data.buswidth == 4)
1628 + if (op->addr.nbytes == 2 && op->addr.buswidth == 1)
1631 + } else if (op->data.dir == SPI_MEM_DATA_OUT) {
1632 + if (op->data.buswidth == 4)
1634 + if (op->addr.nbytes == 2 && op->addr.buswidth == 1)
1641 +static bool qcom_spi_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
1643 + if (!spi_mem_default_supports_op(mem, op))
1646 + if (op->cmd.nbytes != 1 || op->cmd.buswidth != 1)
1649 + if (qcom_spi_is_page_op(op))
1652 + return ((!op->addr.nbytes || op->addr.buswidth == 1) &&
1653 + (!op->dummy.nbytes || op->dummy.buswidth == 1) &&
1654 + (!op->data.nbytes || op->data.buswidth == 1));
1657 +static int qcom_spi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
1659 + struct qcom_nand_controller *snandc = spi_controller_get_devdata(mem->spi->controller);
1661 + dev_dbg(snandc->dev, "OP %02x ADDR %08llX@%d:%u DATA %d:%u", op->cmd.opcode,
1662 + op->addr.val, op->addr.buswidth, op->addr.nbytes,
1663 + op->data.buswidth, op->data.nbytes);
1665 + if (qcom_spi_is_page_op(op)) {
1666 + if (op->data.dir == SPI_MEM_DATA_IN)
1667 + return qcom_spi_read_page(snandc, op);
1668 + if (op->data.dir == SPI_MEM_DATA_OUT)
1669 + return qcom_spi_write_page(snandc, op);
1671 + return qcom_spi_io_op(snandc, op);
1677 +static const struct spi_controller_mem_ops qcom_spi_mem_ops = {
1678 + .supports_op = qcom_spi_supports_op,
1679 + .exec_op = qcom_spi_exec_op,
1682 +static const struct spi_controller_mem_caps qcom_spi_mem_caps = {
1686 +static int qcom_spi_probe(struct platform_device *pdev)
1688 + struct device *dev = &pdev->dev;
1689 + struct spi_controller *ctlr;
1690 + struct qcom_nand_controller *snandc;
1691 + struct qpic_spi_nand *qspi;
1692 + struct qpic_ecc *ecc;
1693 + struct resource *res;
1694 + const void *dev_data;
1697 + ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
1701 + qspi = devm_kzalloc(dev, sizeof(*qspi), GFP_KERNEL);
1705 + ctlr = __devm_spi_alloc_controller(dev, sizeof(*snandc), false);
1709 + platform_set_drvdata(pdev, ctlr);
1711 + snandc = spi_controller_get_devdata(ctlr);
1712 + qspi->snandc = snandc;
1714 + snandc->dev = dev;
1715 + snandc->qspi = qspi;
1716 + snandc->qspi->ctlr = ctlr;
1717 + snandc->qspi->ecc = ecc;
1719 + dev_data = of_device_get_match_data(dev);
1721 + dev_err(&pdev->dev, "failed to get device data\n");
1725 + snandc->props = dev_data;
1726 + snandc->dev = &pdev->dev;
1728 + snandc->core_clk = devm_clk_get(dev, "core");
1729 + if (IS_ERR(snandc->core_clk))
1730 + return PTR_ERR(snandc->core_clk);
1732 + snandc->aon_clk = devm_clk_get(dev, "aon");
1733 + if (IS_ERR(snandc->aon_clk))
1734 + return PTR_ERR(snandc->aon_clk);
1736 + snandc->qspi->iomacro_clk = devm_clk_get(dev, "iom");
1737 + if (IS_ERR(snandc->qspi->iomacro_clk))
1738 + return PTR_ERR(snandc->qspi->iomacro_clk);
1740 + snandc->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1741 + if (IS_ERR(snandc->base))
1742 + return PTR_ERR(snandc->base);
1744 + snandc->base_phys = res->start;
1745 + snandc->base_dma = dma_map_resource(dev, res->start, resource_size(res),
1746 + DMA_BIDIRECTIONAL, 0);
1747 + if (dma_mapping_error(dev, snandc->base_dma))
1750 + ret = clk_prepare_enable(snandc->core_clk);
1752 + goto err_dis_core_clk;
1754 + ret = clk_prepare_enable(snandc->aon_clk);
1756 + goto err_dis_aon_clk;
1758 + ret = clk_prepare_enable(snandc->qspi->iomacro_clk);
1760 + goto err_dis_iom_clk;
1762 + ret = qcom_nandc_alloc(snandc);
1764 + goto err_snand_alloc;
1766 + ret = qcom_spi_init(snandc);
1768 + goto err_spi_init;
1770 + /* setup ECC engine */
1771 + snandc->qspi->ecc_eng.dev = &pdev->dev;
1772 + snandc->qspi->ecc_eng.integration = NAND_ECC_ENGINE_INTEGRATION_PIPELINED;
1773 + snandc->qspi->ecc_eng.ops = &qcom_spi_ecc_engine_ops_pipelined;
1774 + snandc->qspi->ecc_eng.priv = snandc;
1776 + ret = nand_ecc_register_on_host_hw_engine(&snandc->qspi->ecc_eng);
1778 + dev_err(&pdev->dev, "failed to register ecc engine:%d\n", ret);
1779 + goto err_spi_init;
1782 + ctlr->num_chipselect = QPIC_QSPI_NUM_CS;
1783 + ctlr->mem_ops = &qcom_spi_mem_ops;
1784 + ctlr->mem_caps = &qcom_spi_mem_caps;
1785 + ctlr->dev.of_node = pdev->dev.of_node;
1786 + ctlr->mode_bits = SPI_TX_DUAL | SPI_RX_DUAL |
1787 + SPI_TX_QUAD | SPI_RX_QUAD;
1789 + ret = spi_register_controller(ctlr);
1791 + dev_err(&pdev->dev, "spi_register_controller failed.\n");
1792 + goto err_spi_init;
1798 + qcom_nandc_unalloc(snandc);
1800 + clk_disable_unprepare(snandc->qspi->iomacro_clk);
1802 + clk_disable_unprepare(snandc->aon_clk);
1804 + clk_disable_unprepare(snandc->core_clk);
1806 + dma_unmap_resource(dev, res->start, resource_size(res),
1807 + DMA_BIDIRECTIONAL, 0);
1811 +static void qcom_spi_remove(struct platform_device *pdev)
1813 + struct spi_controller *ctlr = platform_get_drvdata(pdev);
1814 + struct qcom_nand_controller *snandc = spi_controller_get_devdata(ctlr);
1815 + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1817 + spi_unregister_controller(ctlr);
1819 + qcom_nandc_unalloc(snandc);
1821 + clk_disable_unprepare(snandc->aon_clk);
1822 + clk_disable_unprepare(snandc->core_clk);
1823 + clk_disable_unprepare(snandc->qspi->iomacro_clk);
1825 + dma_unmap_resource(&pdev->dev, snandc->base_dma, resource_size(res),
1826 + DMA_BIDIRECTIONAL, 0);
1829 +static const struct qcom_nandc_props ipq9574_snandc_props = {
1830 + .dev_cmd_reg_start = 0x7000,
1831 + .supports_bam = true,
1834 +static const struct of_device_id qcom_snandc_of_match[] = {
1836 + .compatible = "qcom,spi-qpic-snand",
1837 + .data = &ipq9574_snandc_props,
1841 +MODULE_DEVICE_TABLE(of, qcom_snandc_of_match);
1843 +static struct platform_driver qcom_spi_driver = {
1845 + .name = "qcom_snand",
1846 + .of_match_table = qcom_snandc_of_match,
1848 + .probe = qcom_spi_probe,
1849 + .remove_new = qcom_spi_remove,
1851 +module_platform_driver(qcom_spi_driver);
1853 +MODULE_DESCRIPTION("SPI driver for QPIC QSPI cores");
1854 +MODULE_AUTHOR("Md Sadre Alam <quic_mdalam@quicinc.com>");
1855 +MODULE_LICENSE("GPL");
1857 --- a/include/linux/mtd/nand-qpic-common.h
1858 +++ b/include/linux/mtd/nand-qpic-common.h
1859 @@ -322,6 +322,10 @@ struct nandc_regs {
1860 __le32 read_location_last1;
1861 __le32 read_location_last2;
1862 __le32 read_location_last3;
1864 + __le32 num_addr_cycle;
1865 + __le32 busy_wait_cnt;
1866 + __le32 flash_feature;
1868 __le32 erased_cw_detect_cfg_clr;
1869 __le32 erased_cw_detect_cfg_set;
1870 @@ -336,6 +340,7 @@ struct nandc_regs {
1872 * @core_clk: controller clock
1873 * @aon_clk: another controller clock
1874 + * @iomacro_clk: io macro clock
1876 * @regs: a contiguous chunk of memory for DMA register
1877 * writes. contains the register values to be
1878 @@ -345,6 +350,7 @@ struct nandc_regs {
1879 * initialized via DT match data
1881 * @controller: base controller structure
1882 + * @qspi: qpic spi structure
1883 * @host_list: list containing all the chips attached to the
1886 @@ -389,6 +395,7 @@ struct qcom_nand_controller {
1887 const struct qcom_nandc_props *props;
1889 struct nand_controller *controller;
1890 + struct qpic_spi_nand *qspi;
1891 struct list_head host_list;