Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016, The Linux Foundation. All rights reserved. |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/clk.h> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/bitops.h> |
| 9 | #include <linux/dma-mapping.h> |
| 10 | #include <linux/dmaengine.h> |
| 11 | #include <linux/module.h> |
Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 12 | #include <linux/mtd/rawnand.h> |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 13 | #include <linux/mtd/partitions.h> |
| 14 | #include <linux/of.h> |
| 15 | #include <linux/of_device.h> |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 16 | #include <linux/delay.h> |
Abhishek Sahu | 8c4cdce | 2017-09-25 13:21:25 +0530 | [diff] [blame] | 17 | #include <linux/dma/qcom_bam_dma.h> |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 18 | |
| 19 | /* NANDc reg offsets */ |
| 20 | #define NAND_FLASH_CMD 0x00 |
| 21 | #define NAND_ADDR0 0x04 |
| 22 | #define NAND_ADDR1 0x08 |
| 23 | #define NAND_FLASH_CHIP_SELECT 0x0c |
| 24 | #define NAND_EXEC_CMD 0x10 |
| 25 | #define NAND_FLASH_STATUS 0x14 |
| 26 | #define NAND_BUFFER_STATUS 0x18 |
| 27 | #define NAND_DEV0_CFG0 0x20 |
| 28 | #define NAND_DEV0_CFG1 0x24 |
| 29 | #define NAND_DEV0_ECC_CFG 0x28 |
Md Sadre Alam | 0646493 | 2021-01-31 01:37:16 +0530 | [diff] [blame] | 30 | #define NAND_AUTO_STATUS_EN 0x2c |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 31 | #define NAND_DEV1_CFG0 0x30 |
| 32 | #define NAND_DEV1_CFG1 0x34 |
| 33 | #define NAND_READ_ID 0x40 |
| 34 | #define NAND_READ_STATUS 0x44 |
| 35 | #define NAND_DEV_CMD0 0xa0 |
| 36 | #define NAND_DEV_CMD1 0xa4 |
| 37 | #define NAND_DEV_CMD2 0xa8 |
| 38 | #define NAND_DEV_CMD_VLD 0xac |
| 39 | #define SFLASHC_BURST_CFG 0xe0 |
| 40 | #define NAND_ERASED_CW_DETECT_CFG 0xe8 |
| 41 | #define NAND_ERASED_CW_DETECT_STATUS 0xec |
| 42 | #define NAND_EBI2_ECC_BUF_CFG 0xf0 |
| 43 | #define FLASH_BUF_ACC 0x100 |
| 44 | |
| 45 | #define NAND_CTRL 0xf00 |
| 46 | #define NAND_VERSION 0xf08 |
| 47 | #define NAND_READ_LOCATION_0 0xf20 |
| 48 | #define NAND_READ_LOCATION_1 0xf24 |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 49 | #define NAND_READ_LOCATION_2 0xf28 |
| 50 | #define NAND_READ_LOCATION_3 0xf2c |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 51 | #define NAND_READ_LOCATION_LAST_CW_0 0xf40 |
| 52 | #define NAND_READ_LOCATION_LAST_CW_1 0xf44 |
| 53 | #define NAND_READ_LOCATION_LAST_CW_2 0xf48 |
| 54 | #define NAND_READ_LOCATION_LAST_CW_3 0xf4c |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 55 | |
| 56 | /* dummy register offsets, used by write_reg_dma */ |
| 57 | #define NAND_DEV_CMD1_RESTORE 0xdead |
| 58 | #define NAND_DEV_CMD_VLD_RESTORE 0xbeef |
| 59 | |
| 60 | /* NAND_FLASH_CMD bits */ |
| 61 | #define PAGE_ACC BIT(4) |
| 62 | #define LAST_PAGE BIT(5) |
| 63 | |
| 64 | /* NAND_FLASH_CHIP_SELECT bits */ |
| 65 | #define NAND_DEV_SEL 0 |
| 66 | #define DM_EN BIT(2) |
| 67 | |
| 68 | /* NAND_FLASH_STATUS bits */ |
| 69 | #define FS_OP_ERR BIT(4) |
| 70 | #define FS_READY_BSY_N BIT(5) |
| 71 | #define FS_MPU_ERR BIT(8) |
| 72 | #define FS_DEVICE_STS_ERR BIT(16) |
| 73 | #define FS_DEVICE_WP BIT(23) |
| 74 | |
| 75 | /* NAND_BUFFER_STATUS bits */ |
| 76 | #define BS_UNCORRECTABLE_BIT BIT(8) |
| 77 | #define BS_CORRECTABLE_ERR_MSK 0x1f |
| 78 | |
| 79 | /* NAND_DEVn_CFG0 bits */ |
| 80 | #define DISABLE_STATUS_AFTER_WRITE 4 |
| 81 | #define CW_PER_PAGE 6 |
| 82 | #define UD_SIZE_BYTES 9 |
| 83 | #define ECC_PARITY_SIZE_BYTES_RS 19 |
| 84 | #define SPARE_SIZE_BYTES 23 |
| 85 | #define NUM_ADDR_CYCLES 27 |
| 86 | #define STATUS_BFR_READ 30 |
| 87 | #define SET_RD_MODE_AFTER_STATUS 31 |
| 88 | |
| 89 | /* NAND_DEVn_CFG0 bits */ |
| 90 | #define DEV0_CFG1_ECC_DISABLE 0 |
| 91 | #define WIDE_FLASH 1 |
| 92 | #define NAND_RECOVERY_CYCLES 2 |
| 93 | #define CS_ACTIVE_BSY 5 |
| 94 | #define BAD_BLOCK_BYTE_NUM 6 |
| 95 | #define BAD_BLOCK_IN_SPARE_AREA 16 |
| 96 | #define WR_RD_BSY_GAP 17 |
| 97 | #define ENABLE_BCH_ECC 27 |
| 98 | |
| 99 | /* NAND_DEV0_ECC_CFG bits */ |
| 100 | #define ECC_CFG_ECC_DISABLE 0 |
| 101 | #define ECC_SW_RESET 1 |
| 102 | #define ECC_MODE 4 |
| 103 | #define ECC_PARITY_SIZE_BYTES_BCH 8 |
| 104 | #define ECC_NUM_DATA_BYTES 16 |
| 105 | #define ECC_FORCE_CLK_OPEN 30 |
| 106 | |
| 107 | /* NAND_DEV_CMD1 bits */ |
| 108 | #define READ_ADDR 0 |
| 109 | |
| 110 | /* NAND_DEV_CMD_VLD bits */ |
Abhishek Sahu | d8a9b32 | 2017-08-11 17:09:16 +0530 | [diff] [blame] | 111 | #define READ_START_VLD BIT(0) |
| 112 | #define READ_STOP_VLD BIT(1) |
| 113 | #define WRITE_START_VLD BIT(2) |
| 114 | #define ERASE_START_VLD BIT(3) |
| 115 | #define SEQ_READ_START_VLD BIT(4) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 116 | |
| 117 | /* NAND_EBI2_ECC_BUF_CFG bits */ |
| 118 | #define NUM_STEPS 0 |
| 119 | |
| 120 | /* NAND_ERASED_CW_DETECT_CFG bits */ |
| 121 | #define ERASED_CW_ECC_MASK 1 |
| 122 | #define AUTO_DETECT_RES 0 |
| 123 | #define MASK_ECC (1 << ERASED_CW_ECC_MASK) |
| 124 | #define RESET_ERASED_DET (1 << AUTO_DETECT_RES) |
| 125 | #define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES) |
| 126 | #define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC) |
| 127 | #define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC) |
| 128 | |
| 129 | /* NAND_ERASED_CW_DETECT_STATUS bits */ |
| 130 | #define PAGE_ALL_ERASED BIT(7) |
| 131 | #define CODEWORD_ALL_ERASED BIT(6) |
| 132 | #define PAGE_ERASED BIT(5) |
| 133 | #define CODEWORD_ERASED BIT(4) |
| 134 | #define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED) |
| 135 | #define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED) |
| 136 | |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 137 | /* NAND_READ_LOCATION_n bits */ |
| 138 | #define READ_LOCATION_OFFSET 0 |
| 139 | #define READ_LOCATION_SIZE 16 |
| 140 | #define READ_LOCATION_LAST 31 |
| 141 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 142 | /* Version Mask */ |
| 143 | #define NAND_VERSION_MAJOR_MASK 0xf0000000 |
| 144 | #define NAND_VERSION_MAJOR_SHIFT 28 |
| 145 | #define NAND_VERSION_MINOR_MASK 0x0fff0000 |
| 146 | #define NAND_VERSION_MINOR_SHIFT 16 |
| 147 | |
| 148 | /* NAND OP_CMDs */ |
Olof Johansson | 33bf551 | 2018-11-16 19:43:27 -0800 | [diff] [blame] | 149 | #define OP_PAGE_READ 0x2 |
| 150 | #define OP_PAGE_READ_WITH_ECC 0x3 |
| 151 | #define OP_PAGE_READ_WITH_ECC_SPARE 0x4 |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 152 | #define OP_PAGE_READ_ONFI_READ 0x5 |
Olof Johansson | 33bf551 | 2018-11-16 19:43:27 -0800 | [diff] [blame] | 153 | #define OP_PROGRAM_PAGE 0x6 |
| 154 | #define OP_PAGE_PROGRAM_WITH_ECC 0x7 |
| 155 | #define OP_PROGRAM_PAGE_SPARE 0x9 |
| 156 | #define OP_BLOCK_ERASE 0xa |
| 157 | #define OP_FETCH_ID 0xb |
| 158 | #define OP_RESET_DEVICE 0xd |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 159 | |
Abhishek Sahu | d8a9b32 | 2017-08-11 17:09:16 +0530 | [diff] [blame] | 160 | /* Default Value for NAND_DEV_CMD_VLD */ |
| 161 | #define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \ |
| 162 | ERASE_START_VLD | SEQ_READ_START_VLD) |
| 163 | |
Abhishek Sahu | 9d43f91 | 2017-08-17 17:37:45 +0530 | [diff] [blame] | 164 | /* NAND_CTRL bits */ |
| 165 | #define BAM_MODE_EN BIT(0) |
| 166 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 167 | /* |
| 168 | * the NAND controller performs reads/writes with ECC in 516 byte chunks. |
| 169 | * the driver calls the chunks 'step' or 'codeword' interchangeably |
| 170 | */ |
| 171 | #define NANDC_STEP_SIZE 512 |
| 172 | |
| 173 | /* |
| 174 | * the largest page size we support is 8K, this will have 16 steps/codewords |
| 175 | * of 512 bytes each |
| 176 | */ |
| 177 | #define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE) |
| 178 | |
| 179 | /* we read at most 3 registers per codeword scan */ |
| 180 | #define MAX_REG_RD (3 * MAX_NUM_STEPS) |
| 181 | |
| 182 | /* ECC modes supported by the controller */ |
| 183 | #define ECC_NONE BIT(0) |
| 184 | #define ECC_RS_4BIT BIT(1) |
| 185 | #define ECC_BCH_4BIT BIT(2) |
| 186 | #define ECC_BCH_8BIT BIT(3) |
| 187 | |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 188 | #define nandc_set_read_loc_first(chip, reg, cw_offset, read_size, is_last_read_loc) \ |
| 189 | nandc_set_reg(chip, reg, \ |
Md Sadre Alam | 622d3fc | 2021-02-24 01:08:59 +0530 | [diff] [blame] | 190 | ((cw_offset) << READ_LOCATION_OFFSET) | \ |
| 191 | ((read_size) << READ_LOCATION_SIZE) | \ |
| 192 | ((is_last_read_loc) << READ_LOCATION_LAST)) |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 193 | |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 194 | #define nandc_set_read_loc_last(chip, reg, cw_offset, read_size, is_last_read_loc) \ |
| 195 | nandc_set_reg(chip, reg, \ |
| 196 | ((cw_offset) << READ_LOCATION_OFFSET) | \ |
| 197 | ((read_size) << READ_LOCATION_SIZE) | \ |
| 198 | ((is_last_read_loc) << READ_LOCATION_LAST)) |
Abhishek Sahu | cc409b9 | 2017-08-17 17:37:47 +0530 | [diff] [blame] | 199 | /* |
| 200 | * Returns the actual register address for all NAND_DEV_ registers |
| 201 | * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD) |
| 202 | */ |
| 203 | #define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg)) |
| 204 | |
Abhishek Sahu | 8d6b6d7 | 2017-09-25 13:21:26 +0530 | [diff] [blame] | 205 | /* Returns the NAND register physical address */ |
| 206 | #define nandc_reg_phys(chip, offset) ((chip)->base_phys + (offset)) |
| 207 | |
| 208 | /* Returns the dma address for reg read buffer */ |
| 209 | #define reg_buf_dma_addr(chip, vaddr) \ |
| 210 | ((chip)->reg_read_dma + \ |
| 211 | ((uint8_t *)(vaddr) - (uint8_t *)(chip)->reg_read_buf)) |
| 212 | |
Abhishek Sahu | 8c4cdce | 2017-09-25 13:21:25 +0530 | [diff] [blame] | 213 | #define QPIC_PER_CW_CMD_ELEMENTS 32 |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 214 | #define QPIC_PER_CW_CMD_SGL 32 |
| 215 | #define QPIC_PER_CW_DATA_SGL 8 |
| 216 | |
Abhishek Sahu | 6f20070 | 2018-06-20 12:57:33 +0530 | [diff] [blame] | 217 | #define QPIC_NAND_COMPLETION_TIMEOUT msecs_to_jiffies(2000) |
| 218 | |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 219 | /* |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 220 | * Flags used in DMA descriptor preparation helper functions |
| 221 | * (i.e. read_reg_dma/write_reg_dma/read_data_dma/write_data_dma) |
| 222 | */ |
| 223 | /* Don't set the EOT in current tx BAM sgl */ |
| 224 | #define NAND_BAM_NO_EOT BIT(0) |
| 225 | /* Set the NWD flag in current BAM sgl */ |
| 226 | #define NAND_BAM_NWD BIT(1) |
| 227 | /* Finish writing in the current BAM sgl and start writing in another BAM sgl */ |
| 228 | #define NAND_BAM_NEXT_SGL BIT(2) |
Abhishek Sahu | a86b9c4 | 2017-08-17 17:37:44 +0530 | [diff] [blame] | 229 | /* |
| 230 | * Erased codeword status is being used two times in single transfer so this |
| 231 | * flag will determine the current value of erased codeword status register |
| 232 | */ |
| 233 | #define NAND_ERASED_CW_SET BIT(4) |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 234 | |
| 235 | /* |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 236 | * This data type corresponds to the BAM transaction which will be used for all |
| 237 | * NAND transfers. |
Abhishek Sahu | 8c4cdce | 2017-09-25 13:21:25 +0530 | [diff] [blame] | 238 | * @bam_ce - the array of BAM command elements |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 239 | * @cmd_sgl - sgl for NAND BAM command pipe |
| 240 | * @data_sgl - sgl for NAND BAM consumer/producer pipe |
Abhishek Sahu | 8c4cdce | 2017-09-25 13:21:25 +0530 | [diff] [blame] | 241 | * @bam_ce_pos - the index in bam_ce which is available for next sgl |
| 242 | * @bam_ce_start - the index in bam_ce which marks the start position ce |
| 243 | * for current sgl. It will be used for size calculation |
| 244 | * for current sgl |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 245 | * @cmd_sgl_pos - current index in command sgl. |
| 246 | * @cmd_sgl_start - start index in command sgl. |
| 247 | * @tx_sgl_pos - current index in data sgl for tx. |
| 248 | * @tx_sgl_start - start index in data sgl for tx. |
| 249 | * @rx_sgl_pos - current index in data sgl for rx. |
| 250 | * @rx_sgl_start - start index in data sgl for rx. |
Abhishek Sahu | 6f20070 | 2018-06-20 12:57:33 +0530 | [diff] [blame] | 251 | * @wait_second_completion - wait for second DMA desc completion before making |
| 252 | * the NAND transfer completion. |
| 253 | * @txn_done - completion for NAND transfer. |
| 254 | * @last_data_desc - last DMA desc in data channel (tx/rx). |
| 255 | * @last_cmd_desc - last DMA desc in command channel. |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 256 | */ |
| 257 | struct bam_transaction { |
Abhishek Sahu | 8c4cdce | 2017-09-25 13:21:25 +0530 | [diff] [blame] | 258 | struct bam_cmd_element *bam_ce; |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 259 | struct scatterlist *cmd_sgl; |
| 260 | struct scatterlist *data_sgl; |
Abhishek Sahu | 8c4cdce | 2017-09-25 13:21:25 +0530 | [diff] [blame] | 261 | u32 bam_ce_pos; |
| 262 | u32 bam_ce_start; |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 263 | u32 cmd_sgl_pos; |
| 264 | u32 cmd_sgl_start; |
| 265 | u32 tx_sgl_pos; |
| 266 | u32 tx_sgl_start; |
| 267 | u32 rx_sgl_pos; |
| 268 | u32 rx_sgl_start; |
Abhishek Sahu | 6f20070 | 2018-06-20 12:57:33 +0530 | [diff] [blame] | 269 | bool wait_second_completion; |
| 270 | struct completion txn_done; |
| 271 | struct dma_async_tx_descriptor *last_data_desc; |
| 272 | struct dma_async_tx_descriptor *last_cmd_desc; |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 273 | }; |
| 274 | |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 275 | /* |
| 276 | * This data type corresponds to the nand dma descriptor |
| 277 | * @list - list for desc_info |
| 278 | * @dir - DMA transfer direction |
| 279 | * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by |
| 280 | * ADM |
| 281 | * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM |
| 282 | * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM |
| 283 | * @dma_desc - low level DMA engine descriptor |
| 284 | */ |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 285 | struct desc_info { |
| 286 | struct list_head node; |
| 287 | |
| 288 | enum dma_data_direction dir; |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 289 | union { |
| 290 | struct scatterlist adm_sgl; |
| 291 | struct { |
| 292 | struct scatterlist *bam_sgl; |
| 293 | int sgl_cnt; |
| 294 | }; |
| 295 | }; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 296 | struct dma_async_tx_descriptor *dma_desc; |
| 297 | }; |
| 298 | |
| 299 | /* |
| 300 | * holds the current register values that we want to write. acts as a contiguous |
| 301 | * chunk of memory which we use to write the controller registers through DMA. |
| 302 | */ |
| 303 | struct nandc_regs { |
| 304 | __le32 cmd; |
| 305 | __le32 addr0; |
| 306 | __le32 addr1; |
| 307 | __le32 chip_sel; |
| 308 | __le32 exec; |
| 309 | |
| 310 | __le32 cfg0; |
| 311 | __le32 cfg1; |
| 312 | __le32 ecc_bch_cfg; |
| 313 | |
| 314 | __le32 clrflashstatus; |
| 315 | __le32 clrreadstatus; |
| 316 | |
| 317 | __le32 cmd1; |
| 318 | __le32 vld; |
| 319 | |
| 320 | __le32 orig_cmd1; |
| 321 | __le32 orig_vld; |
| 322 | |
| 323 | __le32 ecc_buf_cfg; |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 324 | __le32 read_location0; |
| 325 | __le32 read_location1; |
| 326 | __le32 read_location2; |
| 327 | __le32 read_location3; |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 328 | __le32 read_location_last0; |
| 329 | __le32 read_location_last1; |
| 330 | __le32 read_location_last2; |
| 331 | __le32 read_location_last3; |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 332 | |
Abhishek Sahu | a86b9c4 | 2017-08-17 17:37:44 +0530 | [diff] [blame] | 333 | __le32 erased_cw_detect_cfg_clr; |
| 334 | __le32 erased_cw_detect_cfg_set; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | /* |
| 338 | * NAND controller data struct |
| 339 | * |
| 340 | * @controller: base controller structure |
| 341 | * @host_list: list containing all the chips attached to the |
| 342 | * controller |
| 343 | * @dev: parent device |
| 344 | * @base: MMIO base |
Abhishek Sahu | 8d6b6d7 | 2017-09-25 13:21:26 +0530 | [diff] [blame] | 345 | * @base_phys: physical base address of controller registers |
| 346 | * @base_dma: dma base address of controller registers |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 347 | * @core_clk: controller clock |
| 348 | * @aon_clk: another controller clock |
| 349 | * |
| 350 | * @chan: dma channel |
| 351 | * @cmd_crci: ADM DMA CRCI for command flow control |
| 352 | * @data_crci: ADM DMA CRCI for data flow control |
| 353 | * @desc_list: DMA descriptor list (list of desc_infos) |
| 354 | * |
| 355 | * @data_buffer: our local DMA buffer for page read/writes, |
| 356 | * used when we can't use the buffer provided |
| 357 | * by upper layers directly |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 358 | * @buf_size/count/start: markers for chip->legacy.read_buf/write_buf |
| 359 | * functions |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 360 | * @reg_read_buf: local buffer for reading back registers via DMA |
Abhishek Sahu | 6192ff7 | 2017-08-17 17:37:39 +0530 | [diff] [blame] | 361 | * @reg_read_dma: contains dma address for register read buffer |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 362 | * @reg_read_pos: marker for data read in reg_read_buf |
| 363 | * |
| 364 | * @regs: a contiguous chunk of memory for DMA register |
| 365 | * writes. contains the register values to be |
| 366 | * written to controller |
| 367 | * @cmd1/vld: some fixed controller register values |
Abhishek Sahu | 58f1f22 | 2017-08-11 17:09:17 +0530 | [diff] [blame] | 368 | * @props: properties of current NAND controller, |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 369 | * initialized via DT match data |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 370 | * @max_cwperpage: maximum QPIC codewords required. calculated |
| 371 | * from all connected NAND devices pagesize |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 372 | */ |
| 373 | struct qcom_nand_controller { |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 374 | struct nand_controller controller; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 375 | struct list_head host_list; |
| 376 | |
| 377 | struct device *dev; |
| 378 | |
| 379 | void __iomem *base; |
Abhishek Sahu | 8d6b6d7 | 2017-09-25 13:21:26 +0530 | [diff] [blame] | 380 | phys_addr_t base_phys; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 381 | dma_addr_t base_dma; |
| 382 | |
| 383 | struct clk *core_clk; |
| 384 | struct clk *aon_clk; |
| 385 | |
Abhishek Sahu | 497d7d8 | 2017-08-11 17:09:19 +0530 | [diff] [blame] | 386 | union { |
| 387 | /* will be used only by QPIC for BAM DMA */ |
| 388 | struct { |
| 389 | struct dma_chan *tx_chan; |
| 390 | struct dma_chan *rx_chan; |
| 391 | struct dma_chan *cmd_chan; |
| 392 | }; |
| 393 | |
| 394 | /* will be used only by EBI2 for ADM DMA */ |
| 395 | struct { |
| 396 | struct dma_chan *chan; |
| 397 | unsigned int cmd_crci; |
| 398 | unsigned int data_crci; |
| 399 | }; |
| 400 | }; |
| 401 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 402 | struct list_head desc_list; |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 403 | struct bam_transaction *bam_txn; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 404 | |
| 405 | u8 *data_buffer; |
| 406 | int buf_size; |
| 407 | int buf_count; |
| 408 | int buf_start; |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 409 | unsigned int max_cwperpage; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 410 | |
| 411 | __le32 *reg_read_buf; |
Abhishek Sahu | 6192ff7 | 2017-08-17 17:37:39 +0530 | [diff] [blame] | 412 | dma_addr_t reg_read_dma; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 413 | int reg_read_pos; |
| 414 | |
| 415 | struct nandc_regs *regs; |
| 416 | |
| 417 | u32 cmd1, vld; |
Abhishek Sahu | 58f1f22 | 2017-08-11 17:09:17 +0530 | [diff] [blame] | 418 | const struct qcom_nandc_props *props; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | /* |
| 422 | * NAND chip structure |
| 423 | * |
| 424 | * @chip: base NAND chip structure |
| 425 | * @node: list node to add itself to host_list in |
| 426 | * qcom_nand_controller |
| 427 | * |
| 428 | * @cs: chip select value for this chip |
| 429 | * @cw_size: the number of bytes in a single step/codeword |
| 430 | * of a page, consisting of all data, ecc, spare |
| 431 | * and reserved bytes |
| 432 | * @cw_data: the number of bytes within a codeword protected |
| 433 | * by ECC |
| 434 | * @use_ecc: request the controller to use ECC for the |
| 435 | * upcoming read/write |
| 436 | * @bch_enabled: flag to tell whether BCH ECC mode is used |
| 437 | * @ecc_bytes_hw: ECC bytes used by controller hardware for this |
| 438 | * chip |
| 439 | * @status: value to be returned if NAND_CMD_STATUS command |
| 440 | * is executed |
| 441 | * @last_command: keeps track of last command on this chip. used |
| 442 | * for reading correct status |
| 443 | * |
| 444 | * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for |
| 445 | * ecc/non-ecc mode for the current nand flash |
| 446 | * device |
| 447 | */ |
| 448 | struct qcom_nand_host { |
| 449 | struct nand_chip chip; |
| 450 | struct list_head node; |
| 451 | |
| 452 | int cs; |
| 453 | int cw_size; |
| 454 | int cw_data; |
| 455 | bool use_ecc; |
| 456 | bool bch_enabled; |
| 457 | int ecc_bytes_hw; |
| 458 | int spare_bytes; |
| 459 | int bbm_size; |
| 460 | u8 status; |
| 461 | int last_command; |
| 462 | |
| 463 | u32 cfg0, cfg1; |
| 464 | u32 cfg0_raw, cfg1_raw; |
| 465 | u32 ecc_buf_cfg; |
| 466 | u32 ecc_bch_cfg; |
| 467 | u32 clrflashstatus; |
| 468 | u32 clrreadstatus; |
| 469 | }; |
| 470 | |
Abhishek Sahu | 58f1f22 | 2017-08-11 17:09:17 +0530 | [diff] [blame] | 471 | /* |
| 472 | * This data type corresponds to the NAND controller properties which varies |
| 473 | * among different NAND controllers. |
| 474 | * @ecc_modes - ecc mode for NAND |
Abhishek Sahu | 8c5d5d6 | 2017-08-11 17:09:18 +0530 | [diff] [blame] | 475 | * @is_bam - whether NAND controller is using BAM |
Sivaprakash Murugesan | 443440c | 2020-06-12 13:28:15 +0530 | [diff] [blame] | 476 | * @is_qpic - whether NAND CTRL is part of qpic IP |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 477 | * @qpic_v2 - flag to indicate QPIC IP version 2 |
Abhishek Sahu | cc409b9 | 2017-08-17 17:37:47 +0530 | [diff] [blame] | 478 | * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset |
Abhishek Sahu | 58f1f22 | 2017-08-11 17:09:17 +0530 | [diff] [blame] | 479 | */ |
| 480 | struct qcom_nandc_props { |
| 481 | u32 ecc_modes; |
Abhishek Sahu | 8c5d5d6 | 2017-08-11 17:09:18 +0530 | [diff] [blame] | 482 | bool is_bam; |
Sivaprakash Murugesan | 443440c | 2020-06-12 13:28:15 +0530 | [diff] [blame] | 483 | bool is_qpic; |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 484 | bool qpic_v2; |
Abhishek Sahu | cc409b9 | 2017-08-17 17:37:47 +0530 | [diff] [blame] | 485 | u32 dev_cmd_reg_start; |
Abhishek Sahu | 58f1f22 | 2017-08-11 17:09:17 +0530 | [diff] [blame] | 486 | }; |
| 487 | |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 488 | /* Frees the BAM transaction memory */ |
| 489 | static void free_bam_transaction(struct qcom_nand_controller *nandc) |
| 490 | { |
| 491 | struct bam_transaction *bam_txn = nandc->bam_txn; |
| 492 | |
| 493 | devm_kfree(nandc->dev, bam_txn); |
| 494 | } |
| 495 | |
| 496 | /* Allocates and Initializes the BAM transaction */ |
| 497 | static struct bam_transaction * |
| 498 | alloc_bam_transaction(struct qcom_nand_controller *nandc) |
| 499 | { |
| 500 | struct bam_transaction *bam_txn; |
| 501 | size_t bam_txn_size; |
| 502 | unsigned int num_cw = nandc->max_cwperpage; |
| 503 | void *bam_txn_buf; |
| 504 | |
| 505 | bam_txn_size = |
| 506 | sizeof(*bam_txn) + num_cw * |
Abhishek Sahu | 8c4cdce | 2017-09-25 13:21:25 +0530 | [diff] [blame] | 507 | ((sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS) + |
| 508 | (sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL) + |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 509 | (sizeof(*bam_txn->data_sgl) * QPIC_PER_CW_DATA_SGL)); |
| 510 | |
| 511 | bam_txn_buf = devm_kzalloc(nandc->dev, bam_txn_size, GFP_KERNEL); |
| 512 | if (!bam_txn_buf) |
| 513 | return NULL; |
| 514 | |
| 515 | bam_txn = bam_txn_buf; |
| 516 | bam_txn_buf += sizeof(*bam_txn); |
| 517 | |
Abhishek Sahu | 8c4cdce | 2017-09-25 13:21:25 +0530 | [diff] [blame] | 518 | bam_txn->bam_ce = bam_txn_buf; |
| 519 | bam_txn_buf += |
| 520 | sizeof(*bam_txn->bam_ce) * QPIC_PER_CW_CMD_ELEMENTS * num_cw; |
| 521 | |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 522 | bam_txn->cmd_sgl = bam_txn_buf; |
| 523 | bam_txn_buf += |
| 524 | sizeof(*bam_txn->cmd_sgl) * QPIC_PER_CW_CMD_SGL * num_cw; |
| 525 | |
| 526 | bam_txn->data_sgl = bam_txn_buf; |
| 527 | |
Abhishek Sahu | 6f20070 | 2018-06-20 12:57:33 +0530 | [diff] [blame] | 528 | init_completion(&bam_txn->txn_done); |
| 529 | |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 530 | return bam_txn; |
| 531 | } |
| 532 | |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 533 | /* Clears the BAM transaction indexes */ |
| 534 | static void clear_bam_transaction(struct qcom_nand_controller *nandc) |
| 535 | { |
| 536 | struct bam_transaction *bam_txn = nandc->bam_txn; |
| 537 | |
| 538 | if (!nandc->props->is_bam) |
| 539 | return; |
| 540 | |
Abhishek Sahu | 8c4cdce | 2017-09-25 13:21:25 +0530 | [diff] [blame] | 541 | bam_txn->bam_ce_pos = 0; |
| 542 | bam_txn->bam_ce_start = 0; |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 543 | bam_txn->cmd_sgl_pos = 0; |
| 544 | bam_txn->cmd_sgl_start = 0; |
| 545 | bam_txn->tx_sgl_pos = 0; |
| 546 | bam_txn->tx_sgl_start = 0; |
| 547 | bam_txn->rx_sgl_pos = 0; |
| 548 | bam_txn->rx_sgl_start = 0; |
Abhishek Sahu | 6f20070 | 2018-06-20 12:57:33 +0530 | [diff] [blame] | 549 | bam_txn->last_data_desc = NULL; |
| 550 | bam_txn->wait_second_completion = false; |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 551 | |
| 552 | sg_init_table(bam_txn->cmd_sgl, nandc->max_cwperpage * |
| 553 | QPIC_PER_CW_CMD_SGL); |
| 554 | sg_init_table(bam_txn->data_sgl, nandc->max_cwperpage * |
| 555 | QPIC_PER_CW_DATA_SGL); |
Abhishek Sahu | 6f20070 | 2018-06-20 12:57:33 +0530 | [diff] [blame] | 556 | |
| 557 | reinit_completion(&bam_txn->txn_done); |
| 558 | } |
| 559 | |
| 560 | /* Callback for DMA descriptor completion */ |
| 561 | static void qpic_bam_dma_done(void *data) |
| 562 | { |
| 563 | struct bam_transaction *bam_txn = data; |
| 564 | |
| 565 | /* |
| 566 | * In case of data transfer with NAND, 2 callbacks will be generated. |
| 567 | * One for command channel and another one for data channel. |
| 568 | * If current transaction has data descriptors |
| 569 | * (i.e. wait_second_completion is true), then set this to false |
| 570 | * and wait for second DMA descriptor completion. |
| 571 | */ |
| 572 | if (bam_txn->wait_second_completion) |
| 573 | bam_txn->wait_second_completion = false; |
| 574 | else |
| 575 | complete(&bam_txn->txn_done); |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 576 | } |
| 577 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 578 | static inline struct qcom_nand_host *to_qcom_nand_host(struct nand_chip *chip) |
| 579 | { |
| 580 | return container_of(chip, struct qcom_nand_host, chip); |
| 581 | } |
| 582 | |
| 583 | static inline struct qcom_nand_controller * |
| 584 | get_qcom_nand_controller(struct nand_chip *chip) |
| 585 | { |
| 586 | return container_of(chip->controller, struct qcom_nand_controller, |
| 587 | controller); |
| 588 | } |
| 589 | |
| 590 | static inline u32 nandc_read(struct qcom_nand_controller *nandc, int offset) |
| 591 | { |
| 592 | return ioread32(nandc->base + offset); |
| 593 | } |
| 594 | |
| 595 | static inline void nandc_write(struct qcom_nand_controller *nandc, int offset, |
| 596 | u32 val) |
| 597 | { |
| 598 | iowrite32(val, nandc->base + offset); |
| 599 | } |
| 600 | |
Abhishek Sahu | 6192ff7 | 2017-08-17 17:37:39 +0530 | [diff] [blame] | 601 | static inline void nandc_read_buffer_sync(struct qcom_nand_controller *nandc, |
| 602 | bool is_cpu) |
| 603 | { |
| 604 | if (!nandc->props->is_bam) |
| 605 | return; |
| 606 | |
| 607 | if (is_cpu) |
| 608 | dma_sync_single_for_cpu(nandc->dev, nandc->reg_read_dma, |
| 609 | MAX_REG_RD * |
| 610 | sizeof(*nandc->reg_read_buf), |
| 611 | DMA_FROM_DEVICE); |
| 612 | else |
| 613 | dma_sync_single_for_device(nandc->dev, nandc->reg_read_dma, |
| 614 | MAX_REG_RD * |
| 615 | sizeof(*nandc->reg_read_buf), |
| 616 | DMA_FROM_DEVICE); |
| 617 | } |
| 618 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 619 | static __le32 *offset_to_nandc_reg(struct nandc_regs *regs, int offset) |
| 620 | { |
| 621 | switch (offset) { |
| 622 | case NAND_FLASH_CMD: |
| 623 | return ®s->cmd; |
| 624 | case NAND_ADDR0: |
| 625 | return ®s->addr0; |
| 626 | case NAND_ADDR1: |
| 627 | return ®s->addr1; |
| 628 | case NAND_FLASH_CHIP_SELECT: |
| 629 | return ®s->chip_sel; |
| 630 | case NAND_EXEC_CMD: |
| 631 | return ®s->exec; |
| 632 | case NAND_FLASH_STATUS: |
| 633 | return ®s->clrflashstatus; |
| 634 | case NAND_DEV0_CFG0: |
| 635 | return ®s->cfg0; |
| 636 | case NAND_DEV0_CFG1: |
| 637 | return ®s->cfg1; |
| 638 | case NAND_DEV0_ECC_CFG: |
| 639 | return ®s->ecc_bch_cfg; |
| 640 | case NAND_READ_STATUS: |
| 641 | return ®s->clrreadstatus; |
| 642 | case NAND_DEV_CMD1: |
| 643 | return ®s->cmd1; |
| 644 | case NAND_DEV_CMD1_RESTORE: |
| 645 | return ®s->orig_cmd1; |
| 646 | case NAND_DEV_CMD_VLD: |
| 647 | return ®s->vld; |
| 648 | case NAND_DEV_CMD_VLD_RESTORE: |
| 649 | return ®s->orig_vld; |
| 650 | case NAND_EBI2_ECC_BUF_CFG: |
| 651 | return ®s->ecc_buf_cfg; |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 652 | case NAND_READ_LOCATION_0: |
| 653 | return ®s->read_location0; |
| 654 | case NAND_READ_LOCATION_1: |
| 655 | return ®s->read_location1; |
| 656 | case NAND_READ_LOCATION_2: |
| 657 | return ®s->read_location2; |
| 658 | case NAND_READ_LOCATION_3: |
| 659 | return ®s->read_location3; |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 660 | case NAND_READ_LOCATION_LAST_CW_0: |
| 661 | return ®s->read_location_last0; |
| 662 | case NAND_READ_LOCATION_LAST_CW_1: |
| 663 | return ®s->read_location_last1; |
| 664 | case NAND_READ_LOCATION_LAST_CW_2: |
| 665 | return ®s->read_location_last2; |
| 666 | case NAND_READ_LOCATION_LAST_CW_3: |
| 667 | return ®s->read_location_last3; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 668 | default: |
| 669 | return NULL; |
| 670 | } |
| 671 | } |
| 672 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 673 | static void nandc_set_reg(struct nand_chip *chip, int offset, |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 674 | u32 val) |
| 675 | { |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 676 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 677 | struct nandc_regs *regs = nandc->regs; |
| 678 | __le32 *reg; |
| 679 | |
| 680 | reg = offset_to_nandc_reg(regs, offset); |
| 681 | |
| 682 | if (reg) |
| 683 | *reg = cpu_to_le32(val); |
| 684 | } |
| 685 | |
Md Sadre Alam | b057e49 | 2021-02-24 01:08:58 +0530 | [diff] [blame] | 686 | /* Helper to check the code word, whether it is last cw or not */ |
| 687 | static bool qcom_nandc_is_last_cw(struct nand_ecc_ctrl *ecc, int cw) |
| 688 | { |
| 689 | return cw == (ecc->steps - 1); |
| 690 | } |
| 691 | |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 692 | /* helper to configure location register values */ |
| 693 | static void nandc_set_read_loc(struct nand_chip *chip, int cw, int reg, |
| 694 | int cw_offset, int read_size, int is_last_read_loc) |
| 695 | { |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 696 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 697 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 698 | int reg_base = NAND_READ_LOCATION_0; |
| 699 | |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 700 | if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw)) |
| 701 | reg_base = NAND_READ_LOCATION_LAST_CW_0; |
| 702 | |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 703 | reg_base += reg * 4; |
| 704 | |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 705 | if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw)) |
| 706 | return nandc_set_read_loc_last(chip, reg_base, cw_offset, |
| 707 | read_size, is_last_read_loc); |
| 708 | else |
| 709 | return nandc_set_read_loc_first(chip, reg_base, cw_offset, |
| 710 | read_size, is_last_read_loc); |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 711 | } |
| 712 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 713 | /* helper to configure address register values */ |
| 714 | static void set_address(struct qcom_nand_host *host, u16 column, int page) |
| 715 | { |
| 716 | struct nand_chip *chip = &host->chip; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 717 | |
| 718 | if (chip->options & NAND_BUSWIDTH_16) |
| 719 | column >>= 1; |
| 720 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 721 | nandc_set_reg(chip, NAND_ADDR0, page << 16 | column); |
| 722 | nandc_set_reg(chip, NAND_ADDR1, page >> 16 & 0xff); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /* |
| 726 | * update_rw_regs: set up read/write register values, these will be |
| 727 | * written to the NAND controller registers via DMA |
| 728 | * |
| 729 | * @num_cw: number of steps for the read/write operation |
| 730 | * @read: read or write operation |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 731 | * @cw : which code word |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 732 | */ |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 733 | static void update_rw_regs(struct qcom_nand_host *host, int num_cw, bool read, int cw) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 734 | { |
| 735 | struct nand_chip *chip = &host->chip; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 736 | u32 cmd, cfg0, cfg1, ecc_bch_cfg; |
Md Sadre Alam | bfb34ec | 2021-06-08 12:18:36 +0530 | [diff] [blame] | 737 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 738 | |
| 739 | if (read) { |
| 740 | if (host->use_ecc) |
Olof Johansson | 33bf551 | 2018-11-16 19:43:27 -0800 | [diff] [blame] | 741 | cmd = OP_PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 742 | else |
Olof Johansson | 33bf551 | 2018-11-16 19:43:27 -0800 | [diff] [blame] | 743 | cmd = OP_PAGE_READ | PAGE_ACC | LAST_PAGE; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 744 | } else { |
Olof Johansson | 33bf551 | 2018-11-16 19:43:27 -0800 | [diff] [blame] | 745 | cmd = OP_PROGRAM_PAGE | PAGE_ACC | LAST_PAGE; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | if (host->use_ecc) { |
| 749 | cfg0 = (host->cfg0 & ~(7U << CW_PER_PAGE)) | |
| 750 | (num_cw - 1) << CW_PER_PAGE; |
| 751 | |
| 752 | cfg1 = host->cfg1; |
| 753 | ecc_bch_cfg = host->ecc_bch_cfg; |
| 754 | } else { |
| 755 | cfg0 = (host->cfg0_raw & ~(7U << CW_PER_PAGE)) | |
| 756 | (num_cw - 1) << CW_PER_PAGE; |
| 757 | |
| 758 | cfg1 = host->cfg1_raw; |
| 759 | ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE; |
| 760 | } |
| 761 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 762 | nandc_set_reg(chip, NAND_FLASH_CMD, cmd); |
| 763 | nandc_set_reg(chip, NAND_DEV0_CFG0, cfg0); |
| 764 | nandc_set_reg(chip, NAND_DEV0_CFG1, cfg1); |
| 765 | nandc_set_reg(chip, NAND_DEV0_ECC_CFG, ecc_bch_cfg); |
Md Sadre Alam | bfb34ec | 2021-06-08 12:18:36 +0530 | [diff] [blame] | 766 | if (!nandc->props->qpic_v2) |
| 767 | nandc_set_reg(chip, NAND_EBI2_ECC_BUF_CFG, host->ecc_buf_cfg); |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 768 | nandc_set_reg(chip, NAND_FLASH_STATUS, host->clrflashstatus); |
| 769 | nandc_set_reg(chip, NAND_READ_STATUS, host->clrreadstatus); |
| 770 | nandc_set_reg(chip, NAND_EXEC_CMD, 1); |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 771 | |
| 772 | if (read) |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 773 | nandc_set_read_loc(chip, cw, 0, 0, host->use_ecc ? |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 774 | host->cw_data : host->cw_size, 1); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 775 | } |
| 776 | |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 777 | /* |
| 778 | * Maps the scatter gather list for DMA transfer and forms the DMA descriptor |
| 779 | * for BAM. This descriptor will be added in the NAND DMA descriptor queue |
| 780 | * which will be submitted to DMA engine. |
| 781 | */ |
| 782 | static int prepare_bam_async_desc(struct qcom_nand_controller *nandc, |
| 783 | struct dma_chan *chan, |
| 784 | unsigned long flags) |
| 785 | { |
| 786 | struct desc_info *desc; |
| 787 | struct scatterlist *sgl; |
| 788 | unsigned int sgl_cnt; |
| 789 | int ret; |
| 790 | struct bam_transaction *bam_txn = nandc->bam_txn; |
| 791 | enum dma_transfer_direction dir_eng; |
| 792 | struct dma_async_tx_descriptor *dma_desc; |
| 793 | |
| 794 | desc = kzalloc(sizeof(*desc), GFP_KERNEL); |
| 795 | if (!desc) |
| 796 | return -ENOMEM; |
| 797 | |
| 798 | if (chan == nandc->cmd_chan) { |
| 799 | sgl = &bam_txn->cmd_sgl[bam_txn->cmd_sgl_start]; |
| 800 | sgl_cnt = bam_txn->cmd_sgl_pos - bam_txn->cmd_sgl_start; |
| 801 | bam_txn->cmd_sgl_start = bam_txn->cmd_sgl_pos; |
| 802 | dir_eng = DMA_MEM_TO_DEV; |
| 803 | desc->dir = DMA_TO_DEVICE; |
| 804 | } else if (chan == nandc->tx_chan) { |
| 805 | sgl = &bam_txn->data_sgl[bam_txn->tx_sgl_start]; |
| 806 | sgl_cnt = bam_txn->tx_sgl_pos - bam_txn->tx_sgl_start; |
| 807 | bam_txn->tx_sgl_start = bam_txn->tx_sgl_pos; |
| 808 | dir_eng = DMA_MEM_TO_DEV; |
| 809 | desc->dir = DMA_TO_DEVICE; |
| 810 | } else { |
| 811 | sgl = &bam_txn->data_sgl[bam_txn->rx_sgl_start]; |
| 812 | sgl_cnt = bam_txn->rx_sgl_pos - bam_txn->rx_sgl_start; |
| 813 | bam_txn->rx_sgl_start = bam_txn->rx_sgl_pos; |
| 814 | dir_eng = DMA_DEV_TO_MEM; |
| 815 | desc->dir = DMA_FROM_DEVICE; |
| 816 | } |
| 817 | |
| 818 | sg_mark_end(sgl + sgl_cnt - 1); |
| 819 | ret = dma_map_sg(nandc->dev, sgl, sgl_cnt, desc->dir); |
| 820 | if (ret == 0) { |
| 821 | dev_err(nandc->dev, "failure in mapping desc\n"); |
| 822 | kfree(desc); |
| 823 | return -ENOMEM; |
| 824 | } |
| 825 | |
| 826 | desc->sgl_cnt = sgl_cnt; |
| 827 | desc->bam_sgl = sgl; |
| 828 | |
| 829 | dma_desc = dmaengine_prep_slave_sg(chan, sgl, sgl_cnt, dir_eng, |
| 830 | flags); |
| 831 | |
| 832 | if (!dma_desc) { |
| 833 | dev_err(nandc->dev, "failure in prep desc\n"); |
| 834 | dma_unmap_sg(nandc->dev, sgl, sgl_cnt, desc->dir); |
| 835 | kfree(desc); |
| 836 | return -EINVAL; |
| 837 | } |
| 838 | |
| 839 | desc->dma_desc = dma_desc; |
| 840 | |
Abhishek Sahu | 6f20070 | 2018-06-20 12:57:33 +0530 | [diff] [blame] | 841 | /* update last data/command descriptor */ |
| 842 | if (chan == nandc->cmd_chan) |
| 843 | bam_txn->last_cmd_desc = dma_desc; |
| 844 | else |
| 845 | bam_txn->last_data_desc = dma_desc; |
| 846 | |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 847 | list_add_tail(&desc->node, &nandc->desc_list); |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 852 | /* |
Abhishek Sahu | 8d6b6d7 | 2017-09-25 13:21:26 +0530 | [diff] [blame] | 853 | * Prepares the command descriptor for BAM DMA which will be used for NAND |
| 854 | * register reads and writes. The command descriptor requires the command |
| 855 | * to be formed in command element type so this function uses the command |
| 856 | * element from bam transaction ce array and fills the same with required |
| 857 | * data. A single SGL can contain multiple command elements so |
| 858 | * NAND_BAM_NEXT_SGL will be used for starting the separate SGL |
| 859 | * after the current command element. |
| 860 | */ |
| 861 | static int prep_bam_dma_desc_cmd(struct qcom_nand_controller *nandc, bool read, |
| 862 | int reg_off, const void *vaddr, |
| 863 | int size, unsigned int flags) |
| 864 | { |
| 865 | int bam_ce_size; |
| 866 | int i, ret; |
| 867 | struct bam_cmd_element *bam_ce_buffer; |
| 868 | struct bam_transaction *bam_txn = nandc->bam_txn; |
| 869 | |
| 870 | bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_pos]; |
| 871 | |
| 872 | /* fill the command desc */ |
| 873 | for (i = 0; i < size; i++) { |
| 874 | if (read) |
| 875 | bam_prep_ce(&bam_ce_buffer[i], |
| 876 | nandc_reg_phys(nandc, reg_off + 4 * i), |
| 877 | BAM_READ_COMMAND, |
| 878 | reg_buf_dma_addr(nandc, |
| 879 | (__le32 *)vaddr + i)); |
| 880 | else |
| 881 | bam_prep_ce_le32(&bam_ce_buffer[i], |
| 882 | nandc_reg_phys(nandc, reg_off + 4 * i), |
| 883 | BAM_WRITE_COMMAND, |
| 884 | *((__le32 *)vaddr + i)); |
| 885 | } |
| 886 | |
| 887 | bam_txn->bam_ce_pos += size; |
| 888 | |
| 889 | /* use the separate sgl after this command */ |
| 890 | if (flags & NAND_BAM_NEXT_SGL) { |
| 891 | bam_ce_buffer = &bam_txn->bam_ce[bam_txn->bam_ce_start]; |
| 892 | bam_ce_size = (bam_txn->bam_ce_pos - |
| 893 | bam_txn->bam_ce_start) * |
| 894 | sizeof(struct bam_cmd_element); |
| 895 | sg_set_buf(&bam_txn->cmd_sgl[bam_txn->cmd_sgl_pos], |
| 896 | bam_ce_buffer, bam_ce_size); |
| 897 | bam_txn->cmd_sgl_pos++; |
| 898 | bam_txn->bam_ce_start = bam_txn->bam_ce_pos; |
| 899 | |
| 900 | if (flags & NAND_BAM_NWD) { |
| 901 | ret = prepare_bam_async_desc(nandc, nandc->cmd_chan, |
| 902 | DMA_PREP_FENCE | |
| 903 | DMA_PREP_CMD); |
| 904 | if (ret) |
| 905 | return ret; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | /* |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 913 | * Prepares the data descriptor for BAM DMA which will be used for NAND |
| 914 | * data reads and writes. |
| 915 | */ |
| 916 | static int prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read, |
| 917 | const void *vaddr, |
| 918 | int size, unsigned int flags) |
| 919 | { |
| 920 | int ret; |
| 921 | struct bam_transaction *bam_txn = nandc->bam_txn; |
| 922 | |
| 923 | if (read) { |
| 924 | sg_set_buf(&bam_txn->data_sgl[bam_txn->rx_sgl_pos], |
| 925 | vaddr, size); |
| 926 | bam_txn->rx_sgl_pos++; |
| 927 | } else { |
| 928 | sg_set_buf(&bam_txn->data_sgl[bam_txn->tx_sgl_pos], |
| 929 | vaddr, size); |
| 930 | bam_txn->tx_sgl_pos++; |
| 931 | |
| 932 | /* |
| 933 | * BAM will only set EOT for DMA_PREP_INTERRUPT so if this flag |
| 934 | * is not set, form the DMA descriptor |
| 935 | */ |
| 936 | if (!(flags & NAND_BAM_NO_EOT)) { |
| 937 | ret = prepare_bam_async_desc(nandc, nandc->tx_chan, |
| 938 | DMA_PREP_INTERRUPT); |
| 939 | if (ret) |
| 940 | return ret; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | return 0; |
| 945 | } |
| 946 | |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 947 | static int prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read, |
| 948 | int reg_off, const void *vaddr, int size, |
| 949 | bool flow_control) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 950 | { |
| 951 | struct desc_info *desc; |
| 952 | struct dma_async_tx_descriptor *dma_desc; |
| 953 | struct scatterlist *sgl; |
| 954 | struct dma_slave_config slave_conf; |
| 955 | enum dma_transfer_direction dir_eng; |
| 956 | int ret; |
| 957 | |
| 958 | desc = kzalloc(sizeof(*desc), GFP_KERNEL); |
| 959 | if (!desc) |
| 960 | return -ENOMEM; |
| 961 | |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 962 | sgl = &desc->adm_sgl; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 963 | |
| 964 | sg_init_one(sgl, vaddr, size); |
| 965 | |
| 966 | if (read) { |
| 967 | dir_eng = DMA_DEV_TO_MEM; |
| 968 | desc->dir = DMA_FROM_DEVICE; |
| 969 | } else { |
| 970 | dir_eng = DMA_MEM_TO_DEV; |
| 971 | desc->dir = DMA_TO_DEVICE; |
| 972 | } |
| 973 | |
| 974 | ret = dma_map_sg(nandc->dev, sgl, 1, desc->dir); |
| 975 | if (ret == 0) { |
| 976 | ret = -ENOMEM; |
| 977 | goto err; |
| 978 | } |
| 979 | |
| 980 | memset(&slave_conf, 0x00, sizeof(slave_conf)); |
| 981 | |
| 982 | slave_conf.device_fc = flow_control; |
| 983 | if (read) { |
| 984 | slave_conf.src_maxburst = 16; |
| 985 | slave_conf.src_addr = nandc->base_dma + reg_off; |
| 986 | slave_conf.slave_id = nandc->data_crci; |
| 987 | } else { |
| 988 | slave_conf.dst_maxburst = 16; |
| 989 | slave_conf.dst_addr = nandc->base_dma + reg_off; |
| 990 | slave_conf.slave_id = nandc->cmd_crci; |
| 991 | } |
| 992 | |
| 993 | ret = dmaengine_slave_config(nandc->chan, &slave_conf); |
| 994 | if (ret) { |
| 995 | dev_err(nandc->dev, "failed to configure dma channel\n"); |
| 996 | goto err; |
| 997 | } |
| 998 | |
| 999 | dma_desc = dmaengine_prep_slave_sg(nandc->chan, sgl, 1, dir_eng, 0); |
| 1000 | if (!dma_desc) { |
| 1001 | dev_err(nandc->dev, "failed to prepare desc\n"); |
| 1002 | ret = -EINVAL; |
| 1003 | goto err; |
| 1004 | } |
| 1005 | |
| 1006 | desc->dma_desc = dma_desc; |
| 1007 | |
| 1008 | list_add_tail(&desc->node, &nandc->desc_list); |
| 1009 | |
| 1010 | return 0; |
| 1011 | err: |
| 1012 | kfree(desc); |
| 1013 | |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * read_reg_dma: prepares a descriptor to read a given number of |
| 1019 | * contiguous registers to the reg_read_buf pointer |
| 1020 | * |
| 1021 | * @first: offset of the first register in the contiguous block |
| 1022 | * @num_regs: number of registers to read |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1023 | * @flags: flags to control DMA descriptor preparation |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1024 | */ |
| 1025 | static int read_reg_dma(struct qcom_nand_controller *nandc, int first, |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1026 | int num_regs, unsigned int flags) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1027 | { |
| 1028 | bool flow_control = false; |
| 1029 | void *vaddr; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1030 | |
Abhishek Sahu | 8d6b6d7 | 2017-09-25 13:21:26 +0530 | [diff] [blame] | 1031 | vaddr = nandc->reg_read_buf + nandc->reg_read_pos; |
| 1032 | nandc->reg_read_pos += num_regs; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1033 | |
Abhishek Sahu | cc409b9 | 2017-08-17 17:37:47 +0530 | [diff] [blame] | 1034 | if (first == NAND_DEV_CMD_VLD || first == NAND_DEV_CMD1) |
| 1035 | first = dev_cmd_reg_addr(nandc, first); |
| 1036 | |
Abhishek Sahu | 8d6b6d7 | 2017-09-25 13:21:26 +0530 | [diff] [blame] | 1037 | if (nandc->props->is_bam) |
| 1038 | return prep_bam_dma_desc_cmd(nandc, true, first, vaddr, |
| 1039 | num_regs, flags); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1040 | |
Abhishek Sahu | 8d6b6d7 | 2017-09-25 13:21:26 +0530 | [diff] [blame] | 1041 | if (first == NAND_READ_ID || first == NAND_FLASH_STATUS) |
| 1042 | flow_control = true; |
| 1043 | |
| 1044 | return prep_adm_dma_desc(nandc, true, first, vaddr, |
| 1045 | num_regs * sizeof(u32), flow_control); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * write_reg_dma: prepares a descriptor to write a given number of |
| 1050 | * contiguous registers |
| 1051 | * |
| 1052 | * @first: offset of the first register in the contiguous block |
| 1053 | * @num_regs: number of registers to write |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1054 | * @flags: flags to control DMA descriptor preparation |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1055 | */ |
| 1056 | static int write_reg_dma(struct qcom_nand_controller *nandc, int first, |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1057 | int num_regs, unsigned int flags) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1058 | { |
| 1059 | bool flow_control = false; |
| 1060 | struct nandc_regs *regs = nandc->regs; |
| 1061 | void *vaddr; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1062 | |
| 1063 | vaddr = offset_to_nandc_reg(regs, first); |
| 1064 | |
Abhishek Sahu | a86b9c4 | 2017-08-17 17:37:44 +0530 | [diff] [blame] | 1065 | if (first == NAND_ERASED_CW_DETECT_CFG) { |
| 1066 | if (flags & NAND_ERASED_CW_SET) |
| 1067 | vaddr = ®s->erased_cw_detect_cfg_set; |
| 1068 | else |
| 1069 | vaddr = ®s->erased_cw_detect_cfg_clr; |
| 1070 | } |
| 1071 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1072 | if (first == NAND_EXEC_CMD) |
| 1073 | flags |= NAND_BAM_NWD; |
| 1074 | |
Abhishek Sahu | cc409b9 | 2017-08-17 17:37:47 +0530 | [diff] [blame] | 1075 | if (first == NAND_DEV_CMD1_RESTORE || first == NAND_DEV_CMD1) |
| 1076 | first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD1); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1077 | |
Abhishek Sahu | cc409b9 | 2017-08-17 17:37:47 +0530 | [diff] [blame] | 1078 | if (first == NAND_DEV_CMD_VLD_RESTORE || first == NAND_DEV_CMD_VLD) |
| 1079 | first = dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1080 | |
Abhishek Sahu | 8d6b6d7 | 2017-09-25 13:21:26 +0530 | [diff] [blame] | 1081 | if (nandc->props->is_bam) |
| 1082 | return prep_bam_dma_desc_cmd(nandc, false, first, vaddr, |
| 1083 | num_regs, flags); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1084 | |
Abhishek Sahu | 8d6b6d7 | 2017-09-25 13:21:26 +0530 | [diff] [blame] | 1085 | if (first == NAND_FLASH_CMD) |
| 1086 | flow_control = true; |
| 1087 | |
| 1088 | return prep_adm_dma_desc(nandc, false, first, vaddr, |
| 1089 | num_regs * sizeof(u32), flow_control); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | * read_data_dma: prepares a DMA descriptor to transfer data from the |
| 1094 | * controller's internal buffer to the buffer 'vaddr' |
| 1095 | * |
| 1096 | * @reg_off: offset within the controller's data buffer |
| 1097 | * @vaddr: virtual address of the buffer we want to write to |
| 1098 | * @size: DMA transaction size in bytes |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1099 | * @flags: flags to control DMA descriptor preparation |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1100 | */ |
| 1101 | static int read_data_dma(struct qcom_nand_controller *nandc, int reg_off, |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1102 | const u8 *vaddr, int size, unsigned int flags) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1103 | { |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 1104 | if (nandc->props->is_bam) |
| 1105 | return prep_bam_dma_desc_data(nandc, true, vaddr, size, flags); |
| 1106 | |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 1107 | return prep_adm_dma_desc(nandc, true, reg_off, vaddr, size, false); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | * write_data_dma: prepares a DMA descriptor to transfer data from |
| 1112 | * 'vaddr' to the controller's internal buffer |
| 1113 | * |
| 1114 | * @reg_off: offset within the controller's data buffer |
| 1115 | * @vaddr: virtual address of the buffer we want to read from |
| 1116 | * @size: DMA transaction size in bytes |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1117 | * @flags: flags to control DMA descriptor preparation |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1118 | */ |
| 1119 | static int write_data_dma(struct qcom_nand_controller *nandc, int reg_off, |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1120 | const u8 *vaddr, int size, unsigned int flags) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1121 | { |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 1122 | if (nandc->props->is_bam) |
| 1123 | return prep_bam_dma_desc_data(nandc, false, vaddr, size, flags); |
| 1124 | |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 1125 | return prep_adm_dma_desc(nandc, false, reg_off, vaddr, size, false); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | /* |
Abhishek Sahu | bde4330 | 2017-07-19 17:17:55 +0530 | [diff] [blame] | 1129 | * Helper to prepare DMA descriptors for configuring registers |
| 1130 | * before reading a NAND page. |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1131 | */ |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1132 | static void config_nand_page_read(struct nand_chip *chip) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1133 | { |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1134 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1135 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1136 | write_reg_dma(nandc, NAND_ADDR0, 2, 0); |
| 1137 | write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0); |
Md Sadre Alam | bfb34ec | 2021-06-08 12:18:36 +0530 | [diff] [blame] | 1138 | if (!nandc->props->qpic_v2) |
| 1139 | write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, 0); |
Abhishek Sahu | a86b9c4 | 2017-08-17 17:37:44 +0530 | [diff] [blame] | 1140 | write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, 0); |
| 1141 | write_reg_dma(nandc, NAND_ERASED_CW_DETECT_CFG, 1, |
| 1142 | NAND_ERASED_CW_SET | NAND_BAM_NEXT_SGL); |
Abhishek Sahu | bde4330 | 2017-07-19 17:17:55 +0530 | [diff] [blame] | 1143 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1144 | |
Abhishek Sahu | bde4330 | 2017-07-19 17:17:55 +0530 | [diff] [blame] | 1145 | /* |
| 1146 | * Helper to prepare DMA descriptors for configuring registers |
| 1147 | * before reading each codeword in NAND page. |
| 1148 | */ |
Abhishek Sahu | 5bc36b2 | 2018-06-20 12:57:39 +0530 | [diff] [blame] | 1149 | static void |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 1150 | config_nand_cw_read(struct nand_chip *chip, bool use_ecc, int cw) |
Abhishek Sahu | bde4330 | 2017-07-19 17:17:55 +0530 | [diff] [blame] | 1151 | { |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1152 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 1153 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 1154 | |
| 1155 | int reg = NAND_READ_LOCATION_0; |
| 1156 | |
| 1157 | if (nandc->props->qpic_v2 && qcom_nandc_is_last_cw(ecc, cw)) |
| 1158 | reg = NAND_READ_LOCATION_LAST_CW_0; |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1159 | |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 1160 | if (nandc->props->is_bam) |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 1161 | write_reg_dma(nandc, reg, 4, NAND_BAM_NEXT_SGL); |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 1162 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1163 | write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL); |
| 1164 | write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1165 | |
Abhishek Sahu | 5bc36b2 | 2018-06-20 12:57:39 +0530 | [diff] [blame] | 1166 | if (use_ecc) { |
| 1167 | read_reg_dma(nandc, NAND_FLASH_STATUS, 2, 0); |
| 1168 | read_reg_dma(nandc, NAND_ERASED_CW_DETECT_STATUS, 1, |
| 1169 | NAND_BAM_NEXT_SGL); |
| 1170 | } else { |
| 1171 | read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL); |
| 1172 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /* |
Abhishek Sahu | bde4330 | 2017-07-19 17:17:55 +0530 | [diff] [blame] | 1176 | * Helper to prepare dma descriptors to configure registers needed for reading a |
| 1177 | * single codeword in page |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1178 | */ |
Abhishek Sahu | 5bc36b2 | 2018-06-20 12:57:39 +0530 | [diff] [blame] | 1179 | static void |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1180 | config_nand_single_cw_page_read(struct nand_chip *chip, |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 1181 | bool use_ecc, int cw) |
Abhishek Sahu | bde4330 | 2017-07-19 17:17:55 +0530 | [diff] [blame] | 1182 | { |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1183 | config_nand_page_read(chip); |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 1184 | config_nand_cw_read(chip, use_ecc, cw); |
Abhishek Sahu | bde4330 | 2017-07-19 17:17:55 +0530 | [diff] [blame] | 1185 | } |
| 1186 | |
Abhishek Sahu | 77cc536 | 2017-07-19 17:17:56 +0530 | [diff] [blame] | 1187 | /* |
| 1188 | * Helper to prepare DMA descriptors used to configure registers needed for |
| 1189 | * before writing a NAND page. |
| 1190 | */ |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1191 | static void config_nand_page_write(struct nand_chip *chip) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1192 | { |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1193 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1194 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1195 | write_reg_dma(nandc, NAND_ADDR0, 2, 0); |
| 1196 | write_reg_dma(nandc, NAND_DEV0_CFG0, 3, 0); |
Md Sadre Alam | bfb34ec | 2021-06-08 12:18:36 +0530 | [diff] [blame] | 1197 | if (!nandc->props->qpic_v2) |
| 1198 | write_reg_dma(nandc, NAND_EBI2_ECC_BUF_CFG, 1, |
| 1199 | NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1200 | } |
| 1201 | |
Abhishek Sahu | 77cc536 | 2017-07-19 17:17:56 +0530 | [diff] [blame] | 1202 | /* |
| 1203 | * Helper to prepare DMA descriptors for configuring registers |
| 1204 | * before writing each codeword in NAND page. |
| 1205 | */ |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1206 | static void config_nand_cw_write(struct nand_chip *chip) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1207 | { |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1208 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1209 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1210 | write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL); |
| 1211 | write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1212 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1213 | read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1214 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1215 | write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0); |
| 1216 | write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | /* |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1220 | * the following functions are used within chip->legacy.cmdfunc() to |
| 1221 | * perform different NAND_CMD_* commands |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1222 | */ |
| 1223 | |
| 1224 | /* sets up descriptors for NAND_CMD_PARAM */ |
| 1225 | static int nandc_param(struct qcom_nand_host *host) |
| 1226 | { |
| 1227 | struct nand_chip *chip = &host->chip; |
| 1228 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1229 | |
| 1230 | /* |
| 1231 | * NAND_CMD_PARAM is called before we know much about the FLASH chip |
| 1232 | * in use. we configure the controller to perform a raw read of 512 |
| 1233 | * bytes to read onfi params |
| 1234 | */ |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 1235 | if (nandc->props->qpic_v2) |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1236 | nandc_set_reg(chip, NAND_FLASH_CMD, OP_PAGE_READ_ONFI_READ | |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 1237 | PAGE_ACC | LAST_PAGE); |
| 1238 | else |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1239 | nandc_set_reg(chip, NAND_FLASH_CMD, OP_PAGE_READ | |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 1240 | PAGE_ACC | LAST_PAGE); |
| 1241 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1242 | nandc_set_reg(chip, NAND_ADDR0, 0); |
| 1243 | nandc_set_reg(chip, NAND_ADDR1, 0); |
| 1244 | nandc_set_reg(chip, NAND_DEV0_CFG0, 0 << CW_PER_PAGE |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1245 | | 512 << UD_SIZE_BYTES |
| 1246 | | 5 << NUM_ADDR_CYCLES |
| 1247 | | 0 << SPARE_SIZE_BYTES); |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1248 | nandc_set_reg(chip, NAND_DEV0_CFG1, 7 << NAND_RECOVERY_CYCLES |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1249 | | 0 << CS_ACTIVE_BSY |
| 1250 | | 17 << BAD_BLOCK_BYTE_NUM |
| 1251 | | 1 << BAD_BLOCK_IN_SPARE_AREA |
| 1252 | | 2 << WR_RD_BSY_GAP |
| 1253 | | 0 << WIDE_FLASH |
| 1254 | | 1 << DEV0_CFG1_ECC_DISABLE); |
Md Sadre Alam | bfb34ec | 2021-06-08 12:18:36 +0530 | [diff] [blame] | 1255 | if (!nandc->props->qpic_v2) |
| 1256 | nandc_set_reg(chip, NAND_EBI2_ECC_BUF_CFG, 1 << ECC_CFG_ECC_DISABLE); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1257 | |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 1258 | /* configure CMD1 and VLD for ONFI param probing in QPIC v1 */ |
| 1259 | if (!nandc->props->qpic_v2) { |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1260 | nandc_set_reg(chip, NAND_DEV_CMD_VLD, |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 1261 | (nandc->vld & ~READ_START_VLD)); |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1262 | nandc_set_reg(chip, NAND_DEV_CMD1, |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 1263 | (nandc->cmd1 & ~(0xFF << READ_ADDR)) |
| 1264 | | NAND_CMD_PARAM << READ_ADDR); |
| 1265 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1266 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1267 | nandc_set_reg(chip, NAND_EXEC_CMD, 1); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1268 | |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 1269 | if (!nandc->props->qpic_v2) { |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1270 | nandc_set_reg(chip, NAND_DEV_CMD1_RESTORE, nandc->cmd1); |
| 1271 | nandc_set_reg(chip, NAND_DEV_CMD_VLD_RESTORE, nandc->vld); |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 1272 | } |
| 1273 | |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 1274 | nandc_set_read_loc(chip, 0, 0, 0, 512, 1); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1275 | |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 1276 | if (!nandc->props->qpic_v2) { |
| 1277 | write_reg_dma(nandc, NAND_DEV_CMD_VLD, 1, 0); |
| 1278 | write_reg_dma(nandc, NAND_DEV_CMD1, 1, NAND_BAM_NEXT_SGL); |
| 1279 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1280 | |
| 1281 | nandc->buf_count = 512; |
| 1282 | memset(nandc->data_buffer, 0xff, nandc->buf_count); |
| 1283 | |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 1284 | config_nand_single_cw_page_read(chip, false, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1285 | |
| 1286 | read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1287 | nandc->buf_count, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1288 | |
| 1289 | /* restore CMD1 and VLD regs */ |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 1290 | if (!nandc->props->qpic_v2) { |
| 1291 | write_reg_dma(nandc, NAND_DEV_CMD1_RESTORE, 1, 0); |
| 1292 | write_reg_dma(nandc, NAND_DEV_CMD_VLD_RESTORE, 1, NAND_BAM_NEXT_SGL); |
| 1293 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1294 | |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | /* sets up descriptors for NAND_CMD_ERASE1 */ |
| 1299 | static int erase_block(struct qcom_nand_host *host, int page_addr) |
| 1300 | { |
| 1301 | struct nand_chip *chip = &host->chip; |
| 1302 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1303 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1304 | nandc_set_reg(chip, NAND_FLASH_CMD, |
Olof Johansson | 33bf551 | 2018-11-16 19:43:27 -0800 | [diff] [blame] | 1305 | OP_BLOCK_ERASE | PAGE_ACC | LAST_PAGE); |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1306 | nandc_set_reg(chip, NAND_ADDR0, page_addr); |
| 1307 | nandc_set_reg(chip, NAND_ADDR1, 0); |
| 1308 | nandc_set_reg(chip, NAND_DEV0_CFG0, |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1309 | host->cfg0_raw & ~(7 << CW_PER_PAGE)); |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1310 | nandc_set_reg(chip, NAND_DEV0_CFG1, host->cfg1_raw); |
| 1311 | nandc_set_reg(chip, NAND_EXEC_CMD, 1); |
| 1312 | nandc_set_reg(chip, NAND_FLASH_STATUS, host->clrflashstatus); |
| 1313 | nandc_set_reg(chip, NAND_READ_STATUS, host->clrreadstatus); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1314 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1315 | write_reg_dma(nandc, NAND_FLASH_CMD, 3, NAND_BAM_NEXT_SGL); |
| 1316 | write_reg_dma(nandc, NAND_DEV0_CFG0, 2, NAND_BAM_NEXT_SGL); |
| 1317 | write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1318 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1319 | read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1320 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1321 | write_reg_dma(nandc, NAND_FLASH_STATUS, 1, 0); |
| 1322 | write_reg_dma(nandc, NAND_READ_STATUS, 1, NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1323 | |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | /* sets up descriptors for NAND_CMD_READID */ |
| 1328 | static int read_id(struct qcom_nand_host *host, int column) |
| 1329 | { |
| 1330 | struct nand_chip *chip = &host->chip; |
| 1331 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1332 | |
| 1333 | if (column == -1) |
| 1334 | return 0; |
| 1335 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1336 | nandc_set_reg(chip, NAND_FLASH_CMD, OP_FETCH_ID); |
| 1337 | nandc_set_reg(chip, NAND_ADDR0, column); |
| 1338 | nandc_set_reg(chip, NAND_ADDR1, 0); |
| 1339 | nandc_set_reg(chip, NAND_FLASH_CHIP_SELECT, |
Abhishek Sahu | 9d43f91 | 2017-08-17 17:37:45 +0530 | [diff] [blame] | 1340 | nandc->props->is_bam ? 0 : DM_EN); |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1341 | nandc_set_reg(chip, NAND_EXEC_CMD, 1); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1342 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1343 | write_reg_dma(nandc, NAND_FLASH_CMD, 4, NAND_BAM_NEXT_SGL); |
| 1344 | write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1345 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1346 | read_reg_dma(nandc, NAND_READ_ID, 1, NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1347 | |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | /* sets up descriptors for NAND_CMD_RESET */ |
| 1352 | static int reset(struct qcom_nand_host *host) |
| 1353 | { |
| 1354 | struct nand_chip *chip = &host->chip; |
| 1355 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1356 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1357 | nandc_set_reg(chip, NAND_FLASH_CMD, OP_RESET_DEVICE); |
| 1358 | nandc_set_reg(chip, NAND_EXEC_CMD, 1); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1359 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1360 | write_reg_dma(nandc, NAND_FLASH_CMD, 1, NAND_BAM_NEXT_SGL); |
| 1361 | write_reg_dma(nandc, NAND_EXEC_CMD, 1, NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1362 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1363 | read_reg_dma(nandc, NAND_FLASH_STATUS, 1, NAND_BAM_NEXT_SGL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1364 | |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | /* helpers to submit/free our list of dma descriptors */ |
| 1369 | static int submit_descs(struct qcom_nand_controller *nandc) |
| 1370 | { |
| 1371 | struct desc_info *desc; |
| 1372 | dma_cookie_t cookie = 0; |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 1373 | struct bam_transaction *bam_txn = nandc->bam_txn; |
| 1374 | int r; |
| 1375 | |
| 1376 | if (nandc->props->is_bam) { |
| 1377 | if (bam_txn->rx_sgl_pos > bam_txn->rx_sgl_start) { |
| 1378 | r = prepare_bam_async_desc(nandc, nandc->rx_chan, 0); |
| 1379 | if (r) |
| 1380 | return r; |
| 1381 | } |
| 1382 | |
| 1383 | if (bam_txn->tx_sgl_pos > bam_txn->tx_sgl_start) { |
| 1384 | r = prepare_bam_async_desc(nandc, nandc->tx_chan, |
| 1385 | DMA_PREP_INTERRUPT); |
| 1386 | if (r) |
| 1387 | return r; |
| 1388 | } |
| 1389 | |
| 1390 | if (bam_txn->cmd_sgl_pos > bam_txn->cmd_sgl_start) { |
Abhishek Sahu | 8d6b6d7 | 2017-09-25 13:21:26 +0530 | [diff] [blame] | 1391 | r = prepare_bam_async_desc(nandc, nandc->cmd_chan, |
| 1392 | DMA_PREP_CMD); |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 1393 | if (r) |
| 1394 | return r; |
| 1395 | } |
| 1396 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1397 | |
| 1398 | list_for_each_entry(desc, &nandc->desc_list, node) |
| 1399 | cookie = dmaengine_submit(desc->dma_desc); |
| 1400 | |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 1401 | if (nandc->props->is_bam) { |
Abhishek Sahu | 6f20070 | 2018-06-20 12:57:33 +0530 | [diff] [blame] | 1402 | bam_txn->last_cmd_desc->callback = qpic_bam_dma_done; |
| 1403 | bam_txn->last_cmd_desc->callback_param = bam_txn; |
| 1404 | if (bam_txn->last_data_desc) { |
| 1405 | bam_txn->last_data_desc->callback = qpic_bam_dma_done; |
| 1406 | bam_txn->last_data_desc->callback_param = bam_txn; |
| 1407 | bam_txn->wait_second_completion = true; |
| 1408 | } |
| 1409 | |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 1410 | dma_async_issue_pending(nandc->tx_chan); |
| 1411 | dma_async_issue_pending(nandc->rx_chan); |
Abhishek Sahu | 6f20070 | 2018-06-20 12:57:33 +0530 | [diff] [blame] | 1412 | dma_async_issue_pending(nandc->cmd_chan); |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 1413 | |
Abhishek Sahu | 6f20070 | 2018-06-20 12:57:33 +0530 | [diff] [blame] | 1414 | if (!wait_for_completion_timeout(&bam_txn->txn_done, |
| 1415 | QPIC_NAND_COMPLETION_TIMEOUT)) |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 1416 | return -ETIMEDOUT; |
| 1417 | } else { |
| 1418 | if (dma_sync_wait(nandc->chan, cookie) != DMA_COMPLETE) |
| 1419 | return -ETIMEDOUT; |
| 1420 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1421 | |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
| 1425 | static void free_descs(struct qcom_nand_controller *nandc) |
| 1426 | { |
| 1427 | struct desc_info *desc, *n; |
| 1428 | |
| 1429 | list_for_each_entry_safe(desc, n, &nandc->desc_list, node) { |
| 1430 | list_del(&desc->node); |
Abhishek Sahu | 381dd24 | 2017-08-17 17:37:41 +0530 | [diff] [blame] | 1431 | |
| 1432 | if (nandc->props->is_bam) |
| 1433 | dma_unmap_sg(nandc->dev, desc->bam_sgl, |
| 1434 | desc->sgl_cnt, desc->dir); |
| 1435 | else |
| 1436 | dma_unmap_sg(nandc->dev, &desc->adm_sgl, 1, |
| 1437 | desc->dir); |
| 1438 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1439 | kfree(desc); |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | /* reset the register read buffer for next NAND operation */ |
| 1444 | static void clear_read_regs(struct qcom_nand_controller *nandc) |
| 1445 | { |
| 1446 | nandc->reg_read_pos = 0; |
Abhishek Sahu | 6192ff7 | 2017-08-17 17:37:39 +0530 | [diff] [blame] | 1447 | nandc_read_buffer_sync(nandc, false); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | static void pre_command(struct qcom_nand_host *host, int command) |
| 1451 | { |
| 1452 | struct nand_chip *chip = &host->chip; |
| 1453 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1454 | |
| 1455 | nandc->buf_count = 0; |
| 1456 | nandc->buf_start = 0; |
| 1457 | host->use_ecc = false; |
| 1458 | host->last_command = command; |
| 1459 | |
| 1460 | clear_read_regs(nandc); |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 1461 | |
| 1462 | if (command == NAND_CMD_RESET || command == NAND_CMD_READID || |
| 1463 | command == NAND_CMD_PARAM || command == NAND_CMD_ERASE1) |
| 1464 | clear_bam_transaction(nandc); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our |
| 1469 | * privately maintained status byte, this status byte can be read after |
| 1470 | * NAND_CMD_STATUS is called |
| 1471 | */ |
| 1472 | static void parse_erase_write_errors(struct qcom_nand_host *host, int command) |
| 1473 | { |
| 1474 | struct nand_chip *chip = &host->chip; |
| 1475 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1476 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 1477 | int num_cw; |
| 1478 | int i; |
| 1479 | |
| 1480 | num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1; |
Abhishek Sahu | 6192ff7 | 2017-08-17 17:37:39 +0530 | [diff] [blame] | 1481 | nandc_read_buffer_sync(nandc, true); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1482 | |
| 1483 | for (i = 0; i < num_cw; i++) { |
| 1484 | u32 flash_status = le32_to_cpu(nandc->reg_read_buf[i]); |
| 1485 | |
| 1486 | if (flash_status & FS_MPU_ERR) |
| 1487 | host->status &= ~NAND_STATUS_WP; |
| 1488 | |
| 1489 | if (flash_status & FS_OP_ERR || (i == (num_cw - 1) && |
| 1490 | (flash_status & |
| 1491 | FS_DEVICE_STS_ERR))) |
| 1492 | host->status |= NAND_STATUS_FAIL; |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | static void post_command(struct qcom_nand_host *host, int command) |
| 1497 | { |
| 1498 | struct nand_chip *chip = &host->chip; |
| 1499 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1500 | |
| 1501 | switch (command) { |
| 1502 | case NAND_CMD_READID: |
Abhishek Sahu | 6192ff7 | 2017-08-17 17:37:39 +0530 | [diff] [blame] | 1503 | nandc_read_buffer_sync(nandc, true); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1504 | memcpy(nandc->data_buffer, nandc->reg_read_buf, |
| 1505 | nandc->buf_count); |
| 1506 | break; |
| 1507 | case NAND_CMD_PAGEPROG: |
| 1508 | case NAND_CMD_ERASE1: |
| 1509 | parse_erase_write_errors(host, command); |
| 1510 | break; |
| 1511 | default: |
| 1512 | break; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | /* |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1517 | * Implements chip->legacy.cmdfunc. It's only used for a limited set of |
| 1518 | * commands. The rest of the commands wouldn't be called by upper layers. |
| 1519 | * For example, NAND_CMD_READOOB would never be called because we have our own |
| 1520 | * versions of read_oob ops for nand_ecc_ctrl. |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1521 | */ |
Boris Brezillon | 5295cf2 | 2018-09-06 14:05:28 +0200 | [diff] [blame] | 1522 | static void qcom_nandc_command(struct nand_chip *chip, unsigned int command, |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1523 | int column, int page_addr) |
| 1524 | { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1525 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
| 1526 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 1527 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1528 | bool wait = false; |
| 1529 | int ret = 0; |
| 1530 | |
| 1531 | pre_command(host, command); |
| 1532 | |
| 1533 | switch (command) { |
| 1534 | case NAND_CMD_RESET: |
| 1535 | ret = reset(host); |
| 1536 | wait = true; |
| 1537 | break; |
| 1538 | |
| 1539 | case NAND_CMD_READID: |
| 1540 | nandc->buf_count = 4; |
| 1541 | ret = read_id(host, column); |
| 1542 | wait = true; |
| 1543 | break; |
| 1544 | |
| 1545 | case NAND_CMD_PARAM: |
| 1546 | ret = nandc_param(host); |
| 1547 | wait = true; |
| 1548 | break; |
| 1549 | |
| 1550 | case NAND_CMD_ERASE1: |
| 1551 | ret = erase_block(host, page_addr); |
| 1552 | wait = true; |
| 1553 | break; |
| 1554 | |
| 1555 | case NAND_CMD_READ0: |
| 1556 | /* we read the entire page for now */ |
| 1557 | WARN_ON(column != 0); |
| 1558 | |
| 1559 | host->use_ecc = true; |
| 1560 | set_address(host, 0, page_addr); |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 1561 | update_rw_regs(host, ecc->steps, true, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1562 | break; |
| 1563 | |
| 1564 | case NAND_CMD_SEQIN: |
| 1565 | WARN_ON(column != 0); |
| 1566 | set_address(host, 0, page_addr); |
| 1567 | break; |
| 1568 | |
| 1569 | case NAND_CMD_PAGEPROG: |
| 1570 | case NAND_CMD_STATUS: |
| 1571 | case NAND_CMD_NONE: |
| 1572 | default: |
| 1573 | break; |
| 1574 | } |
| 1575 | |
| 1576 | if (ret) { |
| 1577 | dev_err(nandc->dev, "failure executing command %d\n", |
| 1578 | command); |
| 1579 | free_descs(nandc); |
| 1580 | return; |
| 1581 | } |
| 1582 | |
| 1583 | if (wait) { |
| 1584 | ret = submit_descs(nandc); |
| 1585 | if (ret) |
| 1586 | dev_err(nandc->dev, |
| 1587 | "failure submitting descs for command %d\n", |
| 1588 | command); |
| 1589 | } |
| 1590 | |
| 1591 | free_descs(nandc); |
| 1592 | |
| 1593 | post_command(host, command); |
| 1594 | } |
| 1595 | |
| 1596 | /* |
| 1597 | * when using BCH ECC, the HW flags an error in NAND_FLASH_STATUS if it read |
| 1598 | * an erased CW, and reports an erased CW in NAND_ERASED_CW_DETECT_STATUS. |
| 1599 | * |
| 1600 | * when using RS ECC, the HW reports the same erros when reading an erased CW, |
| 1601 | * but it notifies that it is an erased CW by placing special characters at |
| 1602 | * certain offsets in the buffer. |
| 1603 | * |
| 1604 | * verify if the page is erased or not, and fix up the page for RS ECC by |
| 1605 | * replacing the special characters with 0xff. |
| 1606 | */ |
| 1607 | static bool erased_chunk_check_and_fixup(u8 *data_buf, int data_len) |
| 1608 | { |
| 1609 | u8 empty1, empty2; |
| 1610 | |
| 1611 | /* |
| 1612 | * an erased page flags an error in NAND_FLASH_STATUS, check if the page |
| 1613 | * is erased by looking for 0x54s at offsets 3 and 175 from the |
| 1614 | * beginning of each codeword |
| 1615 | */ |
| 1616 | |
| 1617 | empty1 = data_buf[3]; |
| 1618 | empty2 = data_buf[175]; |
| 1619 | |
| 1620 | /* |
| 1621 | * if the erased codework markers, if they exist override them with |
| 1622 | * 0xffs |
| 1623 | */ |
| 1624 | if ((empty1 == 0x54 && empty2 == 0xff) || |
| 1625 | (empty1 == 0xff && empty2 == 0x54)) { |
| 1626 | data_buf[3] = 0xff; |
| 1627 | data_buf[175] = 0xff; |
| 1628 | } |
| 1629 | |
| 1630 | /* |
| 1631 | * check if the entire chunk contains 0xffs or not. if it doesn't, then |
| 1632 | * restore the original values at the special offsets |
| 1633 | */ |
| 1634 | if (memchr_inv(data_buf, 0xff, data_len)) { |
| 1635 | data_buf[3] = empty1; |
| 1636 | data_buf[175] = empty2; |
| 1637 | |
| 1638 | return false; |
| 1639 | } |
| 1640 | |
| 1641 | return true; |
| 1642 | } |
| 1643 | |
| 1644 | struct read_stats { |
| 1645 | __le32 flash; |
| 1646 | __le32 buffer; |
| 1647 | __le32 erased_cw; |
| 1648 | }; |
| 1649 | |
Abhishek Sahu | 5bc36b2 | 2018-06-20 12:57:39 +0530 | [diff] [blame] | 1650 | /* reads back FLASH_STATUS register set by the controller */ |
| 1651 | static int check_flash_errors(struct qcom_nand_host *host, int cw_cnt) |
| 1652 | { |
| 1653 | struct nand_chip *chip = &host->chip; |
| 1654 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1655 | int i; |
| 1656 | |
Praveenkumar I | bc36860 | 2020-10-09 13:37:52 +0530 | [diff] [blame] | 1657 | nandc_read_buffer_sync(nandc, true); |
| 1658 | |
Abhishek Sahu | 5bc36b2 | 2018-06-20 12:57:39 +0530 | [diff] [blame] | 1659 | for (i = 0; i < cw_cnt; i++) { |
| 1660 | u32 flash = le32_to_cpu(nandc->reg_read_buf[i]); |
| 1661 | |
| 1662 | if (flash & (FS_OP_ERR | FS_MPU_ERR)) |
| 1663 | return -EIO; |
| 1664 | } |
| 1665 | |
| 1666 | return 0; |
| 1667 | } |
| 1668 | |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 1669 | /* performs raw read for one codeword */ |
| 1670 | static int |
| 1671 | qcom_nandc_read_cw_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1672 | u8 *data_buf, u8 *oob_buf, int page, int cw) |
| 1673 | { |
| 1674 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
| 1675 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1676 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 1677 | int data_size1, data_size2, oob_size1, oob_size2; |
| 1678 | int ret, reg_off = FLASH_BUF_ACC, read_loc = 0; |
Md Sadre Alam | f60f574 | 2021-09-07 12:35:57 +0530 | [diff] [blame] | 1679 | int raw_cw = cw; |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 1680 | |
| 1681 | nand_read_page_op(chip, page, 0, NULL, 0); |
| 1682 | host->use_ecc = false; |
| 1683 | |
Md Sadre Alam | f60f574 | 2021-09-07 12:35:57 +0530 | [diff] [blame] | 1684 | if (nandc->props->qpic_v2) |
| 1685 | raw_cw = ecc->steps - 1; |
| 1686 | |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 1687 | clear_bam_transaction(nandc); |
| 1688 | set_address(host, host->cw_size * cw, page); |
Md Sadre Alam | f60f574 | 2021-09-07 12:35:57 +0530 | [diff] [blame] | 1689 | update_rw_regs(host, 1, true, raw_cw); |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1690 | config_nand_page_read(chip); |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 1691 | |
| 1692 | data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1); |
| 1693 | oob_size1 = host->bbm_size; |
| 1694 | |
Md Sadre Alam | b057e49 | 2021-02-24 01:08:58 +0530 | [diff] [blame] | 1695 | if (qcom_nandc_is_last_cw(ecc, cw)) { |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 1696 | data_size2 = ecc->size - data_size1 - |
| 1697 | ((ecc->steps - 1) * 4); |
| 1698 | oob_size2 = (ecc->steps * 4) + host->ecc_bytes_hw + |
| 1699 | host->spare_bytes; |
| 1700 | } else { |
| 1701 | data_size2 = host->cw_data - data_size1; |
| 1702 | oob_size2 = host->ecc_bytes_hw + host->spare_bytes; |
| 1703 | } |
| 1704 | |
| 1705 | if (nandc->props->is_bam) { |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 1706 | nandc_set_read_loc(chip, cw, 0, read_loc, data_size1, 0); |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 1707 | read_loc += data_size1; |
| 1708 | |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 1709 | nandc_set_read_loc(chip, cw, 1, read_loc, oob_size1, 0); |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 1710 | read_loc += oob_size1; |
| 1711 | |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 1712 | nandc_set_read_loc(chip, cw, 2, read_loc, data_size2, 0); |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 1713 | read_loc += data_size2; |
| 1714 | |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 1715 | nandc_set_read_loc(chip, cw, 3, read_loc, oob_size2, 1); |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 1716 | } |
| 1717 | |
Md Sadre Alam | f60f574 | 2021-09-07 12:35:57 +0530 | [diff] [blame] | 1718 | config_nand_cw_read(chip, false, raw_cw); |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 1719 | |
| 1720 | read_data_dma(nandc, reg_off, data_buf, data_size1, 0); |
| 1721 | reg_off += data_size1; |
| 1722 | |
| 1723 | read_data_dma(nandc, reg_off, oob_buf, oob_size1, 0); |
| 1724 | reg_off += oob_size1; |
| 1725 | |
| 1726 | read_data_dma(nandc, reg_off, data_buf + data_size1, data_size2, 0); |
| 1727 | reg_off += data_size2; |
| 1728 | |
| 1729 | read_data_dma(nandc, reg_off, oob_buf + oob_size1, oob_size2, 0); |
| 1730 | |
| 1731 | ret = submit_descs(nandc); |
| 1732 | free_descs(nandc); |
| 1733 | if (ret) { |
| 1734 | dev_err(nandc->dev, "failure to read raw cw %d\n", cw); |
| 1735 | return ret; |
| 1736 | } |
| 1737 | |
| 1738 | return check_flash_errors(host, 1); |
| 1739 | } |
| 1740 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1741 | /* |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1742 | * Bitflips can happen in erased codewords also so this function counts the |
| 1743 | * number of 0 in each CW for which ECC engine returns the uncorrectable |
| 1744 | * error. The page will be assumed as erased if this count is less than or |
| 1745 | * equal to the ecc->strength for each CW. |
| 1746 | * |
| 1747 | * 1. Both DATA and OOB need to be checked for number of 0. The |
| 1748 | * top-level API can be called with only data buf or OOB buf so use |
| 1749 | * chip->data_buf if data buf is null and chip->oob_poi if oob buf |
| 1750 | * is null for copying the raw bytes. |
| 1751 | * 2. Perform raw read for all the CW which has uncorrectable errors. |
| 1752 | * 3. For each CW, check the number of 0 in cw_data and usable OOB bytes. |
| 1753 | * The BBM and spare bytes bit flip won’t affect the ECC so don’t check |
| 1754 | * the number of bitflips in this area. |
| 1755 | */ |
| 1756 | static int |
| 1757 | check_for_erased_page(struct qcom_nand_host *host, u8 *data_buf, |
| 1758 | u8 *oob_buf, unsigned long uncorrectable_cws, |
| 1759 | int page, unsigned int max_bitflips) |
| 1760 | { |
| 1761 | struct nand_chip *chip = &host->chip; |
| 1762 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1763 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 1764 | u8 *cw_data_buf, *cw_oob_buf; |
| 1765 | int cw, data_size, oob_size, ret = 0; |
| 1766 | |
Boris Brezillon | eeab717 | 2018-10-28 15:27:55 +0100 | [diff] [blame] | 1767 | if (!data_buf) |
| 1768 | data_buf = nand_get_data_buf(chip); |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1769 | |
| 1770 | if (!oob_buf) { |
Boris Brezillon | eeab717 | 2018-10-28 15:27:55 +0100 | [diff] [blame] | 1771 | nand_get_data_buf(chip); |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1772 | oob_buf = chip->oob_poi; |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | for_each_set_bit(cw, &uncorrectable_cws, ecc->steps) { |
Md Sadre Alam | b057e49 | 2021-02-24 01:08:58 +0530 | [diff] [blame] | 1776 | if (qcom_nandc_is_last_cw(ecc, cw)) { |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1777 | data_size = ecc->size - ((ecc->steps - 1) * 4); |
| 1778 | oob_size = (ecc->steps * 4) + host->ecc_bytes_hw; |
| 1779 | } else { |
| 1780 | data_size = host->cw_data; |
| 1781 | oob_size = host->ecc_bytes_hw; |
| 1782 | } |
| 1783 | |
| 1784 | /* determine starting buffer address for current CW */ |
| 1785 | cw_data_buf = data_buf + (cw * host->cw_data); |
| 1786 | cw_oob_buf = oob_buf + (cw * ecc->bytes); |
| 1787 | |
| 1788 | ret = qcom_nandc_read_cw_raw(mtd, chip, cw_data_buf, |
| 1789 | cw_oob_buf, page, cw); |
| 1790 | if (ret) |
| 1791 | return ret; |
| 1792 | |
| 1793 | /* |
| 1794 | * make sure it isn't an erased page reported |
| 1795 | * as not-erased by HW because of a few bitflips |
| 1796 | */ |
| 1797 | ret = nand_check_erased_ecc_chunk(cw_data_buf, data_size, |
| 1798 | cw_oob_buf + host->bbm_size, |
| 1799 | oob_size, NULL, |
| 1800 | 0, ecc->strength); |
| 1801 | if (ret < 0) { |
| 1802 | mtd->ecc_stats.failed++; |
| 1803 | } else { |
| 1804 | mtd->ecc_stats.corrected += ret; |
| 1805 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | return max_bitflips; |
| 1810 | } |
| 1811 | |
| 1812 | /* |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1813 | * reads back status registers set by the controller to notify page read |
| 1814 | * errors. this is equivalent to what 'ecc->correct()' would do. |
| 1815 | */ |
| 1816 | static int parse_read_errors(struct qcom_nand_host *host, u8 *data_buf, |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1817 | u8 *oob_buf, int page) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1818 | { |
| 1819 | struct nand_chip *chip = &host->chip; |
| 1820 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1821 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1822 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1823 | unsigned int max_bitflips = 0, uncorrectable_cws = 0; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1824 | struct read_stats *buf; |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1825 | bool flash_op_err = false, erased; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1826 | int i; |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1827 | u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1828 | |
| 1829 | buf = (struct read_stats *)nandc->reg_read_buf; |
Abhishek Sahu | 6192ff7 | 2017-08-17 17:37:39 +0530 | [diff] [blame] | 1830 | nandc_read_buffer_sync(nandc, true); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1831 | |
| 1832 | for (i = 0; i < ecc->steps; i++, buf++) { |
| 1833 | u32 flash, buffer, erased_cw; |
| 1834 | int data_len, oob_len; |
| 1835 | |
Md Sadre Alam | b057e49 | 2021-02-24 01:08:58 +0530 | [diff] [blame] | 1836 | if (qcom_nandc_is_last_cw(ecc, i)) { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1837 | data_len = ecc->size - ((ecc->steps - 1) << 2); |
| 1838 | oob_len = ecc->steps << 2; |
| 1839 | } else { |
| 1840 | data_len = host->cw_data; |
| 1841 | oob_len = 0; |
| 1842 | } |
| 1843 | |
| 1844 | flash = le32_to_cpu(buf->flash); |
| 1845 | buffer = le32_to_cpu(buf->buffer); |
| 1846 | erased_cw = le32_to_cpu(buf->erased_cw); |
| 1847 | |
Abhishek Sahu | 8eab721 | 2018-06-20 12:57:34 +0530 | [diff] [blame] | 1848 | /* |
| 1849 | * Check ECC failure for each codeword. ECC failure can |
| 1850 | * happen in either of the following conditions |
| 1851 | * 1. If number of bitflips are greater than ECC engine |
| 1852 | * capability. |
| 1853 | * 2. If this codeword contains all 0xff for which erased |
| 1854 | * codeword detection check will be done. |
| 1855 | */ |
| 1856 | if ((flash & FS_OP_ERR) && (buffer & BS_UNCORRECTABLE_BIT)) { |
Abhishek Sahu | 2f61038 | 2018-06-20 12:57:35 +0530 | [diff] [blame] | 1857 | /* |
| 1858 | * For BCH ECC, ignore erased codeword errors, if |
| 1859 | * ERASED_CW bits are set. |
| 1860 | */ |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1861 | if (host->bch_enabled) { |
Zhen Lei | 902f332 | 2021-05-10 19:49:44 +0800 | [diff] [blame] | 1862 | erased = (erased_cw & ERASED_CW) == ERASED_CW; |
Abhishek Sahu | 2f61038 | 2018-06-20 12:57:35 +0530 | [diff] [blame] | 1863 | /* |
| 1864 | * For RS ECC, HW reports the erased CW by placing |
| 1865 | * special characters at certain offsets in the buffer. |
| 1866 | * These special characters will be valid only if |
| 1867 | * complete page is read i.e. data_buf is not NULL. |
| 1868 | */ |
| 1869 | } else if (data_buf) { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1870 | erased = erased_chunk_check_and_fixup(data_buf, |
| 1871 | data_len); |
Abhishek Sahu | 2f61038 | 2018-06-20 12:57:35 +0530 | [diff] [blame] | 1872 | } else { |
| 1873 | erased = false; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1874 | } |
| 1875 | |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1876 | if (!erased) |
| 1877 | uncorrectable_cws |= BIT(i); |
Abhishek Sahu | 8eab721 | 2018-06-20 12:57:34 +0530 | [diff] [blame] | 1878 | /* |
| 1879 | * Check if MPU or any other operational error (timeout, |
| 1880 | * device failure, etc.) happened for this codeword and |
| 1881 | * make flash_op_err true. If flash_op_err is set, then |
| 1882 | * EIO will be returned for page read. |
| 1883 | */ |
| 1884 | } else if (flash & (FS_OP_ERR | FS_MPU_ERR)) { |
| 1885 | flash_op_err = true; |
| 1886 | /* |
| 1887 | * No ECC or operational errors happened. Check the number of |
| 1888 | * bits corrected and update the ecc_stats.corrected. |
| 1889 | */ |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1890 | } else { |
| 1891 | unsigned int stat; |
| 1892 | |
| 1893 | stat = buffer & BS_CORRECTABLE_ERR_MSK; |
| 1894 | mtd->ecc_stats.corrected += stat; |
| 1895 | max_bitflips = max(max_bitflips, stat); |
| 1896 | } |
| 1897 | |
Abhishek Sahu | 2f61038 | 2018-06-20 12:57:35 +0530 | [diff] [blame] | 1898 | if (data_buf) |
| 1899 | data_buf += data_len; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1900 | if (oob_buf) |
| 1901 | oob_buf += oob_len + ecc->bytes; |
| 1902 | } |
| 1903 | |
Abhishek Sahu | 8eab721 | 2018-06-20 12:57:34 +0530 | [diff] [blame] | 1904 | if (flash_op_err) |
| 1905 | return -EIO; |
| 1906 | |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1907 | if (!uncorrectable_cws) |
| 1908 | return max_bitflips; |
| 1909 | |
| 1910 | return check_for_erased_page(host, data_buf_start, oob_buf_start, |
| 1911 | uncorrectable_cws, page, |
| 1912 | max_bitflips); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | /* |
| 1916 | * helper to perform the actual page read operation, used by ecc->read_page(), |
| 1917 | * ecc->read_oob() |
| 1918 | */ |
| 1919 | static int read_page_ecc(struct qcom_nand_host *host, u8 *data_buf, |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1920 | u8 *oob_buf, int page) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1921 | { |
| 1922 | struct nand_chip *chip = &host->chip; |
| 1923 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 1924 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Abhishek Sahu | add0cfa | 2018-06-20 12:57:36 +0530 | [diff] [blame] | 1925 | u8 *data_buf_start = data_buf, *oob_buf_start = oob_buf; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1926 | int i, ret; |
| 1927 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 1928 | config_nand_page_read(chip); |
Abhishek Sahu | bde4330 | 2017-07-19 17:17:55 +0530 | [diff] [blame] | 1929 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1930 | /* queue cmd descs for each codeword */ |
| 1931 | for (i = 0; i < ecc->steps; i++) { |
| 1932 | int data_size, oob_size; |
| 1933 | |
Md Sadre Alam | b057e49 | 2021-02-24 01:08:58 +0530 | [diff] [blame] | 1934 | if (qcom_nandc_is_last_cw(ecc, i)) { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1935 | data_size = ecc->size - ((ecc->steps - 1) << 2); |
| 1936 | oob_size = (ecc->steps << 2) + host->ecc_bytes_hw + |
| 1937 | host->spare_bytes; |
| 1938 | } else { |
| 1939 | data_size = host->cw_data; |
| 1940 | oob_size = host->ecc_bytes_hw + host->spare_bytes; |
| 1941 | } |
| 1942 | |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 1943 | if (nandc->props->is_bam) { |
| 1944 | if (data_buf && oob_buf) { |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 1945 | nandc_set_read_loc(chip, i, 0, 0, data_size, 0); |
| 1946 | nandc_set_read_loc(chip, i, 1, data_size, |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 1947 | oob_size, 1); |
| 1948 | } else if (data_buf) { |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 1949 | nandc_set_read_loc(chip, i, 0, 0, data_size, 1); |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 1950 | } else { |
Md Sadre Alam | e7a307f | 2021-02-24 01:09:00 +0530 | [diff] [blame] | 1951 | nandc_set_read_loc(chip, i, 0, data_size, |
Abhishek Sahu | 91af95c | 2017-08-17 17:37:43 +0530 | [diff] [blame] | 1952 | oob_size, 1); |
| 1953 | } |
| 1954 | } |
| 1955 | |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 1956 | config_nand_cw_read(chip, true, i); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1957 | |
| 1958 | if (data_buf) |
| 1959 | read_data_dma(nandc, FLASH_BUF_ACC, data_buf, |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1960 | data_size, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1961 | |
| 1962 | /* |
| 1963 | * when ecc is enabled, the controller doesn't read the real |
| 1964 | * or dummy bad block markers in each chunk. To maintain a |
| 1965 | * consistent layout across RAW and ECC reads, we just |
| 1966 | * leave the real/dummy BBM offsets empty (i.e, filled with |
| 1967 | * 0xffs) |
| 1968 | */ |
| 1969 | if (oob_buf) { |
| 1970 | int j; |
| 1971 | |
| 1972 | for (j = 0; j < host->bbm_size; j++) |
| 1973 | *oob_buf++ = 0xff; |
| 1974 | |
| 1975 | read_data_dma(nandc, FLASH_BUF_ACC + data_size, |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 1976 | oob_buf, oob_size, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | if (data_buf) |
| 1980 | data_buf += data_size; |
| 1981 | if (oob_buf) |
| 1982 | oob_buf += oob_size; |
| 1983 | } |
| 1984 | |
| 1985 | ret = submit_descs(nandc); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1986 | free_descs(nandc); |
| 1987 | |
Abhishek Sahu | add0cfa | 2018-06-20 12:57:36 +0530 | [diff] [blame] | 1988 | if (ret) { |
| 1989 | dev_err(nandc->dev, "failure to read page/oob\n"); |
| 1990 | return ret; |
| 1991 | } |
| 1992 | |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 1993 | return parse_read_errors(host, data_buf_start, oob_buf_start, page); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | /* |
| 1997 | * a helper that copies the last step/codeword of a page (containing free oob) |
| 1998 | * into our local buffer |
| 1999 | */ |
| 2000 | static int copy_last_cw(struct qcom_nand_host *host, int page) |
| 2001 | { |
| 2002 | struct nand_chip *chip = &host->chip; |
| 2003 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2004 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 2005 | int size; |
| 2006 | int ret; |
| 2007 | |
| 2008 | clear_read_regs(nandc); |
| 2009 | |
| 2010 | size = host->use_ecc ? host->cw_data : host->cw_size; |
| 2011 | |
| 2012 | /* prepare a clean read buffer */ |
| 2013 | memset(nandc->data_buffer, 0xff, size); |
| 2014 | |
| 2015 | set_address(host, host->cw_size * (ecc->steps - 1), page); |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 2016 | update_rw_regs(host, 1, true, ecc->steps - 1); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2017 | |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 2018 | config_nand_single_cw_page_read(chip, host->use_ecc, ecc->steps - 1); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2019 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 2020 | read_data_dma(nandc, FLASH_BUF_ACC, nandc->data_buffer, size, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2021 | |
| 2022 | ret = submit_descs(nandc); |
| 2023 | if (ret) |
| 2024 | dev_err(nandc->dev, "failed to copy last codeword\n"); |
| 2025 | |
| 2026 | free_descs(nandc); |
| 2027 | |
| 2028 | return ret; |
| 2029 | } |
| 2030 | |
| 2031 | /* implements ecc->read_page() */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2032 | static int qcom_nandc_read_page(struct nand_chip *chip, uint8_t *buf, |
| 2033 | int oob_required, int page) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2034 | { |
| 2035 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
| 2036 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2037 | u8 *data_buf, *oob_buf = NULL; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2038 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2039 | nand_read_page_op(chip, page, 0, NULL, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2040 | data_buf = buf; |
| 2041 | oob_buf = oob_required ? chip->oob_poi : NULL; |
| 2042 | |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 2043 | clear_bam_transaction(nandc); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2044 | |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 2045 | return read_page_ecc(host, data_buf, oob_buf, page); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | /* implements ecc->read_page_raw() */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2049 | static int qcom_nandc_read_page_raw(struct nand_chip *chip, uint8_t *buf, |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2050 | int oob_required, int page) |
| 2051 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2052 | struct mtd_info *mtd = nand_to_mtd(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2053 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2054 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 2055 | int cw, ret; |
| 2056 | u8 *data_buf = buf, *oob_buf = chip->oob_poi; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2057 | |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 2058 | for (cw = 0; cw < ecc->steps; cw++) { |
| 2059 | ret = qcom_nandc_read_cw_raw(mtd, chip, data_buf, oob_buf, |
| 2060 | page, cw); |
| 2061 | if (ret) |
| 2062 | return ret; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2063 | |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 2064 | data_buf += host->cw_data; |
| 2065 | oob_buf += ecc->bytes; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2066 | } |
| 2067 | |
Abhishek Sahu | 85632c1 | 2018-06-20 12:57:40 +0530 | [diff] [blame] | 2068 | return 0; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | /* implements ecc->read_oob() */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2072 | static int qcom_nandc_read_oob(struct nand_chip *chip, int page) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2073 | { |
| 2074 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
| 2075 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2076 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2077 | |
| 2078 | clear_read_regs(nandc); |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 2079 | clear_bam_transaction(nandc); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2080 | |
| 2081 | host->use_ecc = true; |
| 2082 | set_address(host, 0, page); |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 2083 | update_rw_regs(host, ecc->steps, true, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2084 | |
Abhishek Sahu | 9f43dee | 2018-07-03 17:36:03 +0530 | [diff] [blame] | 2085 | return read_page_ecc(host, NULL, chip->oob_poi, page); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | /* implements ecc->write_page() */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 2089 | static int qcom_nandc_write_page(struct nand_chip *chip, const uint8_t *buf, |
| 2090 | int oob_required, int page) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2091 | { |
| 2092 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
| 2093 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2094 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 2095 | u8 *data_buf, *oob_buf; |
| 2096 | int i, ret; |
| 2097 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2098 | nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 2099 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2100 | clear_read_regs(nandc); |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 2101 | clear_bam_transaction(nandc); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2102 | |
| 2103 | data_buf = (u8 *)buf; |
| 2104 | oob_buf = chip->oob_poi; |
| 2105 | |
| 2106 | host->use_ecc = true; |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 2107 | update_rw_regs(host, ecc->steps, false, 0); |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 2108 | config_nand_page_write(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2109 | |
| 2110 | for (i = 0; i < ecc->steps; i++) { |
| 2111 | int data_size, oob_size; |
| 2112 | |
Md Sadre Alam | b057e49 | 2021-02-24 01:08:58 +0530 | [diff] [blame] | 2113 | if (qcom_nandc_is_last_cw(ecc, i)) { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2114 | data_size = ecc->size - ((ecc->steps - 1) << 2); |
| 2115 | oob_size = (ecc->steps << 2) + host->ecc_bytes_hw + |
| 2116 | host->spare_bytes; |
| 2117 | } else { |
| 2118 | data_size = host->cw_data; |
| 2119 | oob_size = ecc->bytes; |
| 2120 | } |
| 2121 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2122 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 2123 | write_data_dma(nandc, FLASH_BUF_ACC, data_buf, data_size, |
| 2124 | i == (ecc->steps - 1) ? NAND_BAM_NO_EOT : 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2125 | |
| 2126 | /* |
| 2127 | * when ECC is enabled, we don't really need to write anything |
| 2128 | * to oob for the first n - 1 codewords since these oob regions |
| 2129 | * just contain ECC bytes that's written by the controller |
| 2130 | * itself. For the last codeword, we skip the bbm positions and |
| 2131 | * write to the free oob area. |
| 2132 | */ |
Md Sadre Alam | b057e49 | 2021-02-24 01:08:58 +0530 | [diff] [blame] | 2133 | if (qcom_nandc_is_last_cw(ecc, i)) { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2134 | oob_buf += host->bbm_size; |
| 2135 | |
| 2136 | write_data_dma(nandc, FLASH_BUF_ACC + data_size, |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 2137 | oob_buf, oob_size, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2138 | } |
| 2139 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 2140 | config_nand_cw_write(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2141 | |
| 2142 | data_buf += data_size; |
| 2143 | oob_buf += oob_size; |
| 2144 | } |
| 2145 | |
| 2146 | ret = submit_descs(nandc); |
| 2147 | if (ret) |
| 2148 | dev_err(nandc->dev, "failure to write page\n"); |
| 2149 | |
| 2150 | free_descs(nandc); |
| 2151 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2152 | if (!ret) |
| 2153 | ret = nand_prog_page_end_op(chip); |
| 2154 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2155 | return ret; |
| 2156 | } |
| 2157 | |
| 2158 | /* implements ecc->write_page_raw() */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 2159 | static int qcom_nandc_write_page_raw(struct nand_chip *chip, |
| 2160 | const uint8_t *buf, int oob_required, |
| 2161 | int page) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2162 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 2163 | struct mtd_info *mtd = nand_to_mtd(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2164 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
| 2165 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2166 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 2167 | u8 *data_buf, *oob_buf; |
| 2168 | int i, ret; |
| 2169 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2170 | nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2171 | clear_read_regs(nandc); |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 2172 | clear_bam_transaction(nandc); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2173 | |
| 2174 | data_buf = (u8 *)buf; |
| 2175 | oob_buf = chip->oob_poi; |
| 2176 | |
| 2177 | host->use_ecc = false; |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 2178 | update_rw_regs(host, ecc->steps, false, 0); |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 2179 | config_nand_page_write(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2180 | |
| 2181 | for (i = 0; i < ecc->steps; i++) { |
| 2182 | int data_size1, data_size2, oob_size1, oob_size2; |
| 2183 | int reg_off = FLASH_BUF_ACC; |
| 2184 | |
| 2185 | data_size1 = mtd->writesize - host->cw_size * (ecc->steps - 1); |
| 2186 | oob_size1 = host->bbm_size; |
| 2187 | |
Md Sadre Alam | b057e49 | 2021-02-24 01:08:58 +0530 | [diff] [blame] | 2188 | if (qcom_nandc_is_last_cw(ecc, i)) { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2189 | data_size2 = ecc->size - data_size1 - |
| 2190 | ((ecc->steps - 1) << 2); |
| 2191 | oob_size2 = (ecc->steps << 2) + host->ecc_bytes_hw + |
| 2192 | host->spare_bytes; |
| 2193 | } else { |
| 2194 | data_size2 = host->cw_data - data_size1; |
| 2195 | oob_size2 = host->ecc_bytes_hw + host->spare_bytes; |
| 2196 | } |
| 2197 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 2198 | write_data_dma(nandc, reg_off, data_buf, data_size1, |
| 2199 | NAND_BAM_NO_EOT); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2200 | reg_off += data_size1; |
| 2201 | data_buf += data_size1; |
| 2202 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 2203 | write_data_dma(nandc, reg_off, oob_buf, oob_size1, |
| 2204 | NAND_BAM_NO_EOT); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2205 | reg_off += oob_size1; |
| 2206 | oob_buf += oob_size1; |
| 2207 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 2208 | write_data_dma(nandc, reg_off, data_buf, data_size2, |
| 2209 | NAND_BAM_NO_EOT); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2210 | reg_off += data_size2; |
| 2211 | data_buf += data_size2; |
| 2212 | |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 2213 | write_data_dma(nandc, reg_off, oob_buf, oob_size2, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2214 | oob_buf += oob_size2; |
| 2215 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 2216 | config_nand_cw_write(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | ret = submit_descs(nandc); |
| 2220 | if (ret) |
| 2221 | dev_err(nandc->dev, "failure to write raw page\n"); |
| 2222 | |
| 2223 | free_descs(nandc); |
| 2224 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2225 | if (!ret) |
| 2226 | ret = nand_prog_page_end_op(chip); |
| 2227 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2228 | return ret; |
| 2229 | } |
| 2230 | |
| 2231 | /* |
| 2232 | * implements ecc->write_oob() |
| 2233 | * |
Abhishek Sahu | 28eed9f | 2018-06-20 12:57:37 +0530 | [diff] [blame] | 2234 | * the NAND controller cannot write only data or only OOB within a codeword |
| 2235 | * since ECC is calculated for the combined codeword. So update the OOB from |
| 2236 | * chip->oob_poi, and pad the data area with OxFF before writing. |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2237 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 2238 | static int qcom_nandc_write_oob(struct nand_chip *chip, int page) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2239 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 2240 | struct mtd_info *mtd = nand_to_mtd(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2241 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
| 2242 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2243 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 2244 | u8 *oob = chip->oob_poi; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2245 | int data_size, oob_size; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2246 | int ret; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2247 | |
| 2248 | host->use_ecc = true; |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 2249 | clear_bam_transaction(nandc); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2250 | |
| 2251 | /* calculate the data and oob size for the last codeword/step */ |
| 2252 | data_size = ecc->size - ((ecc->steps - 1) << 2); |
Boris Brezillon | aa02fcf | 2016-03-18 17:53:31 +0100 | [diff] [blame] | 2253 | oob_size = mtd->oobavail; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2254 | |
Abhishek Sahu | 28eed9f | 2018-06-20 12:57:37 +0530 | [diff] [blame] | 2255 | memset(nandc->data_buffer, 0xff, host->cw_data); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2256 | /* override new oob content to last codeword */ |
Boris Brezillon | aa02fcf | 2016-03-18 17:53:31 +0100 | [diff] [blame] | 2257 | mtd_ooblayout_get_databytes(mtd, nandc->data_buffer + data_size, oob, |
| 2258 | 0, mtd->oobavail); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2259 | |
| 2260 | set_address(host, host->cw_size * (ecc->steps - 1), page); |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 2261 | update_rw_regs(host, 1, false, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2262 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 2263 | config_nand_page_write(chip); |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 2264 | write_data_dma(nandc, FLASH_BUF_ACC, |
| 2265 | nandc->data_buffer, data_size + oob_size, 0); |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 2266 | config_nand_cw_write(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2267 | |
| 2268 | ret = submit_descs(nandc); |
| 2269 | |
| 2270 | free_descs(nandc); |
| 2271 | |
| 2272 | if (ret) { |
| 2273 | dev_err(nandc->dev, "failure to write oob\n"); |
| 2274 | return -EIO; |
| 2275 | } |
| 2276 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2277 | return nand_prog_page_end_op(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2278 | } |
| 2279 | |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 2280 | static int qcom_nandc_block_bad(struct nand_chip *chip, loff_t ofs) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2281 | { |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 2282 | struct mtd_info *mtd = nand_to_mtd(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2283 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
| 2284 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2285 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 2286 | int page, ret, bbpos, bad = 0; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2287 | |
| 2288 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 2289 | |
| 2290 | /* |
| 2291 | * configure registers for a raw sub page read, the address is set to |
| 2292 | * the beginning of the last codeword, we don't care about reading ecc |
| 2293 | * portion of oob. we just want the first few bytes from this codeword |
| 2294 | * that contains the BBM |
| 2295 | */ |
| 2296 | host->use_ecc = false; |
| 2297 | |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 2298 | clear_bam_transaction(nandc); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2299 | ret = copy_last_cw(host, page); |
| 2300 | if (ret) |
| 2301 | goto err; |
| 2302 | |
Abhishek Sahu | 5bc36b2 | 2018-06-20 12:57:39 +0530 | [diff] [blame] | 2303 | if (check_flash_errors(host, 1)) { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2304 | dev_warn(nandc->dev, "error when trying to read BBM\n"); |
| 2305 | goto err; |
| 2306 | } |
| 2307 | |
| 2308 | bbpos = mtd->writesize - host->cw_size * (ecc->steps - 1); |
| 2309 | |
| 2310 | bad = nandc->data_buffer[bbpos] != 0xff; |
| 2311 | |
| 2312 | if (chip->options & NAND_BUSWIDTH_16) |
| 2313 | bad = bad || (nandc->data_buffer[bbpos + 1] != 0xff); |
| 2314 | err: |
| 2315 | return bad; |
| 2316 | } |
| 2317 | |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 2318 | static int qcom_nandc_block_markbad(struct nand_chip *chip, loff_t ofs) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2319 | { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2320 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
| 2321 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2322 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2323 | int page, ret; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2324 | |
| 2325 | clear_read_regs(nandc); |
Abhishek Sahu | 4e2f6c5 | 2017-08-17 17:37:46 +0530 | [diff] [blame] | 2326 | clear_bam_transaction(nandc); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2327 | |
| 2328 | /* |
| 2329 | * to mark the BBM as bad, we flash the entire last codeword with 0s. |
| 2330 | * we don't care about the rest of the content in the codeword since |
| 2331 | * we aren't going to use this block again |
| 2332 | */ |
| 2333 | memset(nandc->data_buffer, 0x00, host->cw_size); |
| 2334 | |
| 2335 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 2336 | |
| 2337 | /* prepare write */ |
| 2338 | host->use_ecc = false; |
| 2339 | set_address(host, host->cw_size * (ecc->steps - 1), page); |
Md Sadre Alam | 503ee5a | 2021-02-24 01:09:01 +0530 | [diff] [blame] | 2340 | update_rw_regs(host, 1, false, ecc->steps - 1); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2341 | |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 2342 | config_nand_page_write(chip); |
Abhishek Sahu | 67e830a | 2017-08-17 17:37:42 +0530 | [diff] [blame] | 2343 | write_data_dma(nandc, FLASH_BUF_ACC, |
| 2344 | nandc->data_buffer, host->cw_size, 0); |
Md Sadre Alam | 9a7c39e | 2021-02-24 01:08:57 +0530 | [diff] [blame] | 2345 | config_nand_cw_write(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2346 | |
| 2347 | ret = submit_descs(nandc); |
| 2348 | |
| 2349 | free_descs(nandc); |
| 2350 | |
| 2351 | if (ret) { |
| 2352 | dev_err(nandc->dev, "failure to update BBM\n"); |
| 2353 | return -EIO; |
| 2354 | } |
| 2355 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2356 | return nand_prog_page_end_op(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | /* |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 2360 | * the three functions below implement chip->legacy.read_byte(), |
| 2361 | * chip->legacy.read_buf() and chip->legacy.write_buf() respectively. these |
| 2362 | * aren't used for reading/writing page data, they are used for smaller data |
| 2363 | * like reading id, status etc |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2364 | */ |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 2365 | static uint8_t qcom_nandc_read_byte(struct nand_chip *chip) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2366 | { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2367 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
| 2368 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2369 | u8 *buf = nandc->data_buffer; |
| 2370 | u8 ret = 0x0; |
| 2371 | |
| 2372 | if (host->last_command == NAND_CMD_STATUS) { |
| 2373 | ret = host->status; |
| 2374 | |
| 2375 | host->status = NAND_STATUS_READY | NAND_STATUS_WP; |
| 2376 | |
| 2377 | return ret; |
| 2378 | } |
| 2379 | |
| 2380 | if (nandc->buf_start < nandc->buf_count) |
| 2381 | ret = buf[nandc->buf_start++]; |
| 2382 | |
| 2383 | return ret; |
| 2384 | } |
| 2385 | |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 2386 | static void qcom_nandc_read_buf(struct nand_chip *chip, uint8_t *buf, int len) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2387 | { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2388 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2389 | int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start); |
| 2390 | |
| 2391 | memcpy(buf, nandc->data_buffer + nandc->buf_start, real_len); |
| 2392 | nandc->buf_start += real_len; |
| 2393 | } |
| 2394 | |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 2395 | static void qcom_nandc_write_buf(struct nand_chip *chip, const uint8_t *buf, |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2396 | int len) |
| 2397 | { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2398 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2399 | int real_len = min_t(size_t, len, nandc->buf_count - nandc->buf_start); |
| 2400 | |
| 2401 | memcpy(nandc->data_buffer + nandc->buf_start, buf, real_len); |
| 2402 | |
| 2403 | nandc->buf_start += real_len; |
| 2404 | } |
| 2405 | |
| 2406 | /* we support only one external chip for now */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 2407 | static void qcom_nandc_select_chip(struct nand_chip *chip, int chipnr) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2408 | { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2409 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
| 2410 | |
| 2411 | if (chipnr <= 0) |
| 2412 | return; |
| 2413 | |
| 2414 | dev_warn(nandc->dev, "invalid chip select\n"); |
| 2415 | } |
| 2416 | |
| 2417 | /* |
| 2418 | * NAND controller page layout info |
| 2419 | * |
| 2420 | * Layout with ECC enabled: |
| 2421 | * |
| 2422 | * |----------------------| |---------------------------------| |
| 2423 | * | xx.......yy| | *********xx.......yy| |
| 2424 | * | DATA xx..ECC..yy| | DATA **SPARE**xx..ECC..yy| |
| 2425 | * | (516) xx.......yy| | (516-n*4) **(n*4)**xx.......yy| |
| 2426 | * | xx.......yy| | *********xx.......yy| |
| 2427 | * |----------------------| |---------------------------------| |
| 2428 | * codeword 1,2..n-1 codeword n |
| 2429 | * <---(528/532 Bytes)--> <-------(528/532 Bytes)---------> |
| 2430 | * |
| 2431 | * n = Number of codewords in the page |
| 2432 | * . = ECC bytes |
| 2433 | * * = Spare/free bytes |
| 2434 | * x = Unused byte(s) |
| 2435 | * y = Reserved byte(s) |
| 2436 | * |
| 2437 | * 2K page: n = 4, spare = 16 bytes |
| 2438 | * 4K page: n = 8, spare = 32 bytes |
| 2439 | * 8K page: n = 16, spare = 64 bytes |
| 2440 | * |
| 2441 | * the qcom nand controller operates at a sub page/codeword level. each |
| 2442 | * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively. |
| 2443 | * the number of ECC bytes vary based on the ECC strength and the bus width. |
| 2444 | * |
| 2445 | * the first n - 1 codewords contains 516 bytes of user data, the remaining |
| 2446 | * 12/16 bytes consist of ECC and reserved data. The nth codeword contains |
| 2447 | * both user data and spare(oobavail) bytes that sum up to 516 bytes. |
| 2448 | * |
| 2449 | * When we access a page with ECC enabled, the reserved bytes(s) are not |
| 2450 | * accessible at all. When reading, we fill up these unreadable positions |
| 2451 | * with 0xffs. When writing, the controller skips writing the inaccessible |
| 2452 | * bytes. |
| 2453 | * |
| 2454 | * Layout with ECC disabled: |
| 2455 | * |
| 2456 | * |------------------------------| |---------------------------------------| |
| 2457 | * | yy xx.......| | bb *********xx.......| |
| 2458 | * | DATA1 yy DATA2 xx..ECC..| | DATA1 bb DATA2 **SPARE**xx..ECC..| |
| 2459 | * | (size1) yy (size2) xx.......| | (size1) bb (size2) **(n*4)**xx.......| |
| 2460 | * | yy xx.......| | bb *********xx.......| |
| 2461 | * |------------------------------| |---------------------------------------| |
| 2462 | * codeword 1,2..n-1 codeword n |
| 2463 | * <-------(528/532 Bytes)------> <-----------(528/532 Bytes)-----------> |
| 2464 | * |
| 2465 | * n = Number of codewords in the page |
| 2466 | * . = ECC bytes |
| 2467 | * * = Spare/free bytes |
| 2468 | * x = Unused byte(s) |
| 2469 | * y = Dummy Bad Bock byte(s) |
| 2470 | * b = Real Bad Block byte(s) |
| 2471 | * size1/size2 = function of codeword size and 'n' |
| 2472 | * |
| 2473 | * when the ECC block is disabled, one reserved byte (or two for 16 bit bus |
| 2474 | * width) is now accessible. For the first n - 1 codewords, these are dummy Bad |
| 2475 | * Block Markers. In the last codeword, this position contains the real BBM |
| 2476 | * |
| 2477 | * In order to have a consistent layout between RAW and ECC modes, we assume |
| 2478 | * the following OOB layout arrangement: |
| 2479 | * |
| 2480 | * |-----------| |--------------------| |
| 2481 | * |yyxx.......| |bb*********xx.......| |
| 2482 | * |yyxx..ECC..| |bb*FREEOOB*xx..ECC..| |
| 2483 | * |yyxx.......| |bb*********xx.......| |
| 2484 | * |yyxx.......| |bb*********xx.......| |
| 2485 | * |-----------| |--------------------| |
| 2486 | * first n - 1 nth OOB region |
| 2487 | * OOB regions |
| 2488 | * |
| 2489 | * n = Number of codewords in the page |
| 2490 | * . = ECC bytes |
| 2491 | * * = FREE OOB bytes |
| 2492 | * y = Dummy bad block byte(s) (inaccessible when ECC enabled) |
| 2493 | * x = Unused byte(s) |
| 2494 | * b = Real bad block byte(s) (inaccessible when ECC enabled) |
| 2495 | * |
| 2496 | * This layout is read as is when ECC is disabled. When ECC is enabled, the |
| 2497 | * inaccessible Bad Block byte(s) are ignored when we write to a page/oob, |
| 2498 | * and assumed as 0xffs when we read a page/oob. The ECC, unused and |
Boris Brezillon | 421e81c | 2016-03-18 17:54:27 +0100 | [diff] [blame] | 2499 | * dummy/real bad block bytes are grouped as ecc bytes (i.e, ecc->bytes is |
| 2500 | * the sum of the three). |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2501 | */ |
Boris Brezillon | 421e81c | 2016-03-18 17:54:27 +0100 | [diff] [blame] | 2502 | static int qcom_nand_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 2503 | struct mtd_oob_region *oobregion) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2504 | { |
Boris Brezillon | 421e81c | 2016-03-18 17:54:27 +0100 | [diff] [blame] | 2505 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 2506 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2507 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2508 | |
Boris Brezillon | 421e81c | 2016-03-18 17:54:27 +0100 | [diff] [blame] | 2509 | if (section > 1) |
| 2510 | return -ERANGE; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2511 | |
Boris Brezillon | 421e81c | 2016-03-18 17:54:27 +0100 | [diff] [blame] | 2512 | if (!section) { |
| 2513 | oobregion->length = (ecc->bytes * (ecc->steps - 1)) + |
| 2514 | host->bbm_size; |
| 2515 | oobregion->offset = 0; |
| 2516 | } else { |
| 2517 | oobregion->length = host->ecc_bytes_hw + host->spare_bytes; |
| 2518 | oobregion->offset = mtd->oobsize - oobregion->length; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2519 | } |
| 2520 | |
Boris Brezillon | 421e81c | 2016-03-18 17:54:27 +0100 | [diff] [blame] | 2521 | return 0; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2522 | } |
| 2523 | |
Boris Brezillon | 421e81c | 2016-03-18 17:54:27 +0100 | [diff] [blame] | 2524 | static int qcom_nand_ooblayout_free(struct mtd_info *mtd, int section, |
| 2525 | struct mtd_oob_region *oobregion) |
| 2526 | { |
| 2527 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 2528 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
| 2529 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 2530 | |
| 2531 | if (section) |
| 2532 | return -ERANGE; |
| 2533 | |
| 2534 | oobregion->length = ecc->steps * 4; |
| 2535 | oobregion->offset = ((ecc->steps - 1) * ecc->bytes) + host->bbm_size; |
| 2536 | |
| 2537 | return 0; |
| 2538 | } |
| 2539 | |
| 2540 | static const struct mtd_ooblayout_ops qcom_nand_ooblayout_ops = { |
| 2541 | .ecc = qcom_nand_ooblayout_ecc, |
| 2542 | .free = qcom_nand_ooblayout_free, |
| 2543 | }; |
| 2544 | |
Abhishek Sahu | 7ddb937 | 2018-06-20 12:57:32 +0530 | [diff] [blame] | 2545 | static int |
| 2546 | qcom_nandc_calc_ecc_bytes(int step_size, int strength) |
| 2547 | { |
| 2548 | return strength == 4 ? 12 : 16; |
| 2549 | } |
| 2550 | NAND_ECC_CAPS_SINGLE(qcom_nandc_ecc_caps, qcom_nandc_calc_ecc_bytes, |
| 2551 | NANDC_STEP_SIZE, 4, 8); |
| 2552 | |
Miquel Raynal | 6a3cec6 | 2018-07-20 17:15:22 +0200 | [diff] [blame] | 2553 | static int qcom_nand_attach_chip(struct nand_chip *chip) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2554 | { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2555 | struct mtd_info *mtd = nand_to_mtd(chip); |
Miquel Raynal | 6a3cec6 | 2018-07-20 17:15:22 +0200 | [diff] [blame] | 2556 | struct qcom_nand_host *host = to_qcom_nand_host(chip); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2557 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 2558 | struct qcom_nand_controller *nandc = get_qcom_nand_controller(chip); |
Abhishek Sahu | 7ddb937 | 2018-06-20 12:57:32 +0530 | [diff] [blame] | 2559 | int cwperpage, bad_block_byte, ret; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2560 | bool wide_bus; |
| 2561 | int ecc_mode = 1; |
| 2562 | |
Abhishek Sahu | 320bdb5 | 2018-06-20 12:57:31 +0530 | [diff] [blame] | 2563 | /* controller only supports 512 bytes data steps */ |
| 2564 | ecc->size = NANDC_STEP_SIZE; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2565 | wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false; |
Abhishek Sahu | 7ddb937 | 2018-06-20 12:57:32 +0530 | [diff] [blame] | 2566 | cwperpage = mtd->writesize / NANDC_STEP_SIZE; |
| 2567 | |
| 2568 | /* |
| 2569 | * Each CW has 4 available OOB bytes which will be protected with ECC |
| 2570 | * so remaining bytes can be used for ECC. |
| 2571 | */ |
| 2572 | ret = nand_ecc_choose_conf(chip, &qcom_nandc_ecc_caps, |
| 2573 | mtd->oobsize - (cwperpage * 4)); |
| 2574 | if (ret) { |
| 2575 | dev_err(nandc->dev, "No valid ECC settings possible\n"); |
| 2576 | return ret; |
| 2577 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2578 | |
| 2579 | if (ecc->strength >= 8) { |
| 2580 | /* 8 bit ECC defaults to BCH ECC on all platforms */ |
| 2581 | host->bch_enabled = true; |
| 2582 | ecc_mode = 1; |
| 2583 | |
| 2584 | if (wide_bus) { |
| 2585 | host->ecc_bytes_hw = 14; |
| 2586 | host->spare_bytes = 0; |
| 2587 | host->bbm_size = 2; |
| 2588 | } else { |
| 2589 | host->ecc_bytes_hw = 13; |
| 2590 | host->spare_bytes = 2; |
| 2591 | host->bbm_size = 1; |
| 2592 | } |
| 2593 | } else { |
| 2594 | /* |
| 2595 | * if the controller supports BCH for 4 bit ECC, the controller |
| 2596 | * uses lesser bytes for ECC. If RS is used, the ECC bytes is |
| 2597 | * always 10 bytes |
| 2598 | */ |
Abhishek Sahu | 58f1f22 | 2017-08-11 17:09:17 +0530 | [diff] [blame] | 2599 | if (nandc->props->ecc_modes & ECC_BCH_4BIT) { |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2600 | /* BCH */ |
| 2601 | host->bch_enabled = true; |
| 2602 | ecc_mode = 0; |
| 2603 | |
| 2604 | if (wide_bus) { |
| 2605 | host->ecc_bytes_hw = 8; |
| 2606 | host->spare_bytes = 2; |
| 2607 | host->bbm_size = 2; |
| 2608 | } else { |
| 2609 | host->ecc_bytes_hw = 7; |
| 2610 | host->spare_bytes = 4; |
| 2611 | host->bbm_size = 1; |
| 2612 | } |
| 2613 | } else { |
| 2614 | /* RS */ |
| 2615 | host->ecc_bytes_hw = 10; |
| 2616 | |
| 2617 | if (wide_bus) { |
| 2618 | host->spare_bytes = 0; |
| 2619 | host->bbm_size = 2; |
| 2620 | } else { |
| 2621 | host->spare_bytes = 1; |
| 2622 | host->bbm_size = 1; |
| 2623 | } |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | /* |
| 2628 | * we consider ecc->bytes as the sum of all the non-data content in a |
| 2629 | * step. It gives us a clean representation of the oob area (even if |
| 2630 | * all the bytes aren't used for ECC).It is always 16 bytes for 8 bit |
| 2631 | * ECC and 12 bytes for 4 bit ECC |
| 2632 | */ |
| 2633 | ecc->bytes = host->ecc_bytes_hw + host->spare_bytes + host->bbm_size; |
| 2634 | |
| 2635 | ecc->read_page = qcom_nandc_read_page; |
| 2636 | ecc->read_page_raw = qcom_nandc_read_page_raw; |
| 2637 | ecc->read_oob = qcom_nandc_read_oob; |
| 2638 | ecc->write_page = qcom_nandc_write_page; |
| 2639 | ecc->write_page_raw = qcom_nandc_write_page_raw; |
| 2640 | ecc->write_oob = qcom_nandc_write_oob; |
| 2641 | |
Miquel Raynal | bace41f | 2020-08-27 10:51:58 +0200 | [diff] [blame] | 2642 | ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2643 | |
Boris Brezillon | 421e81c | 2016-03-18 17:54:27 +0100 | [diff] [blame] | 2644 | mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2645 | |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 2646 | nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage, |
| 2647 | cwperpage); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2648 | |
| 2649 | /* |
| 2650 | * DATA_UD_BYTES varies based on whether the read/write command protects |
| 2651 | * spare data with ECC too. We protect spare data by default, so we set |
| 2652 | * it to main + spare data, which are 512 and 4 bytes respectively. |
| 2653 | */ |
| 2654 | host->cw_data = 516; |
| 2655 | |
| 2656 | /* |
| 2657 | * total bytes in a step, either 528 bytes for 4 bit ECC, or 532 bytes |
| 2658 | * for 8 bit ECC |
| 2659 | */ |
| 2660 | host->cw_size = host->cw_data + ecc->bytes; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2661 | bad_block_byte = mtd->writesize - host->cw_size * (cwperpage - 1) + 1; |
| 2662 | |
| 2663 | host->cfg0 = (cwperpage - 1) << CW_PER_PAGE |
| 2664 | | host->cw_data << UD_SIZE_BYTES |
| 2665 | | 0 << DISABLE_STATUS_AFTER_WRITE |
| 2666 | | 5 << NUM_ADDR_CYCLES |
| 2667 | | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_RS |
| 2668 | | 0 << STATUS_BFR_READ |
| 2669 | | 1 << SET_RD_MODE_AFTER_STATUS |
| 2670 | | host->spare_bytes << SPARE_SIZE_BYTES; |
| 2671 | |
| 2672 | host->cfg1 = 7 << NAND_RECOVERY_CYCLES |
| 2673 | | 0 << CS_ACTIVE_BSY |
| 2674 | | bad_block_byte << BAD_BLOCK_BYTE_NUM |
| 2675 | | 0 << BAD_BLOCK_IN_SPARE_AREA |
| 2676 | | 2 << WR_RD_BSY_GAP |
| 2677 | | wide_bus << WIDE_FLASH |
| 2678 | | host->bch_enabled << ENABLE_BCH_ECC; |
| 2679 | |
| 2680 | host->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE |
| 2681 | | host->cw_size << UD_SIZE_BYTES |
| 2682 | | 5 << NUM_ADDR_CYCLES |
| 2683 | | 0 << SPARE_SIZE_BYTES; |
| 2684 | |
| 2685 | host->cfg1_raw = 7 << NAND_RECOVERY_CYCLES |
| 2686 | | 0 << CS_ACTIVE_BSY |
| 2687 | | 17 << BAD_BLOCK_BYTE_NUM |
| 2688 | | 1 << BAD_BLOCK_IN_SPARE_AREA |
| 2689 | | 2 << WR_RD_BSY_GAP |
| 2690 | | wide_bus << WIDE_FLASH |
| 2691 | | 1 << DEV0_CFG1_ECC_DISABLE; |
| 2692 | |
Abhishek Sahu | 10777de | 2017-08-03 17:56:39 +0200 | [diff] [blame] | 2693 | host->ecc_bch_cfg = !host->bch_enabled << ECC_CFG_ECC_DISABLE |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2694 | | 0 << ECC_SW_RESET |
| 2695 | | host->cw_data << ECC_NUM_DATA_BYTES |
| 2696 | | 1 << ECC_FORCE_CLK_OPEN |
| 2697 | | ecc_mode << ECC_MODE |
| 2698 | | host->ecc_bytes_hw << ECC_PARITY_SIZE_BYTES_BCH; |
| 2699 | |
Md Sadre Alam | bfb34ec | 2021-06-08 12:18:36 +0530 | [diff] [blame] | 2700 | if (!nandc->props->qpic_v2) |
| 2701 | host->ecc_buf_cfg = 0x203 << NUM_STEPS; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2702 | |
| 2703 | host->clrflashstatus = FS_READY_BSY_N; |
| 2704 | host->clrreadstatus = 0xc0; |
Abhishek Sahu | a86b9c4 | 2017-08-17 17:37:44 +0530 | [diff] [blame] | 2705 | nandc->regs->erased_cw_detect_cfg_clr = |
| 2706 | cpu_to_le32(CLR_ERASED_PAGE_DET); |
| 2707 | nandc->regs->erased_cw_detect_cfg_set = |
| 2708 | cpu_to_le32(SET_ERASED_PAGE_DET); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2709 | |
| 2710 | dev_dbg(nandc->dev, |
| 2711 | "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", |
| 2712 | host->cfg0, host->cfg1, host->ecc_buf_cfg, host->ecc_bch_cfg, |
| 2713 | host->cw_size, host->cw_data, ecc->strength, ecc->bytes, |
| 2714 | cwperpage); |
| 2715 | |
| 2716 | return 0; |
| 2717 | } |
| 2718 | |
Miquel Raynal | 6a3cec6 | 2018-07-20 17:15:22 +0200 | [diff] [blame] | 2719 | static const struct nand_controller_ops qcom_nandc_ops = { |
| 2720 | .attach_chip = qcom_nand_attach_chip, |
| 2721 | }; |
| 2722 | |
Peter Ujfalusi | 80c3012 | 2020-02-27 14:37:47 +0200 | [diff] [blame] | 2723 | static void qcom_nandc_unalloc(struct qcom_nand_controller *nandc) |
| 2724 | { |
| 2725 | if (nandc->props->is_bam) { |
| 2726 | if (!dma_mapping_error(nandc->dev, nandc->reg_read_dma)) |
| 2727 | dma_unmap_single(nandc->dev, nandc->reg_read_dma, |
| 2728 | MAX_REG_RD * |
| 2729 | sizeof(*nandc->reg_read_buf), |
| 2730 | DMA_FROM_DEVICE); |
| 2731 | |
| 2732 | if (nandc->tx_chan) |
| 2733 | dma_release_channel(nandc->tx_chan); |
| 2734 | |
| 2735 | if (nandc->rx_chan) |
| 2736 | dma_release_channel(nandc->rx_chan); |
| 2737 | |
| 2738 | if (nandc->cmd_chan) |
| 2739 | dma_release_channel(nandc->cmd_chan); |
| 2740 | } else { |
| 2741 | if (nandc->chan) |
| 2742 | dma_release_channel(nandc->chan); |
| 2743 | } |
| 2744 | } |
| 2745 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2746 | static int qcom_nandc_alloc(struct qcom_nand_controller *nandc) |
| 2747 | { |
| 2748 | int ret; |
| 2749 | |
| 2750 | ret = dma_set_coherent_mask(nandc->dev, DMA_BIT_MASK(32)); |
| 2751 | if (ret) { |
| 2752 | dev_err(nandc->dev, "failed to set DMA mask\n"); |
| 2753 | return ret; |
| 2754 | } |
| 2755 | |
| 2756 | /* |
| 2757 | * we use the internal buffer for reading ONFI params, reading small |
| 2758 | * data like ID and status, and preforming read-copy-write operations |
| 2759 | * when writing to a codeword partially. 532 is the maximum possible |
| 2760 | * size of a codeword for our nand controller |
| 2761 | */ |
| 2762 | nandc->buf_size = 532; |
| 2763 | |
| 2764 | nandc->data_buffer = devm_kzalloc(nandc->dev, nandc->buf_size, |
| 2765 | GFP_KERNEL); |
| 2766 | if (!nandc->data_buffer) |
| 2767 | return -ENOMEM; |
| 2768 | |
| 2769 | nandc->regs = devm_kzalloc(nandc->dev, sizeof(*nandc->regs), |
| 2770 | GFP_KERNEL); |
| 2771 | if (!nandc->regs) |
| 2772 | return -ENOMEM; |
| 2773 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 2774 | nandc->reg_read_buf = devm_kcalloc(nandc->dev, |
| 2775 | MAX_REG_RD, sizeof(*nandc->reg_read_buf), |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2776 | GFP_KERNEL); |
| 2777 | if (!nandc->reg_read_buf) |
| 2778 | return -ENOMEM; |
| 2779 | |
Abhishek Sahu | 497d7d8 | 2017-08-11 17:09:19 +0530 | [diff] [blame] | 2780 | if (nandc->props->is_bam) { |
Abhishek Sahu | 6192ff7 | 2017-08-17 17:37:39 +0530 | [diff] [blame] | 2781 | nandc->reg_read_dma = |
| 2782 | dma_map_single(nandc->dev, nandc->reg_read_buf, |
| 2783 | MAX_REG_RD * |
| 2784 | sizeof(*nandc->reg_read_buf), |
| 2785 | DMA_FROM_DEVICE); |
| 2786 | if (dma_mapping_error(nandc->dev, nandc->reg_read_dma)) { |
| 2787 | dev_err(nandc->dev, "failed to DMA MAP reg buffer\n"); |
| 2788 | return -EIO; |
| 2789 | } |
| 2790 | |
Peter Ujfalusi | 92f0f8e | 2020-02-27 14:37:48 +0200 | [diff] [blame] | 2791 | nandc->tx_chan = dma_request_chan(nandc->dev, "tx"); |
| 2792 | if (IS_ERR(nandc->tx_chan)) { |
| 2793 | ret = PTR_ERR(nandc->tx_chan); |
| 2794 | nandc->tx_chan = NULL; |
Krzysztof Kozlowski | 07eb014 | 2020-09-01 16:25:35 +0200 | [diff] [blame] | 2795 | dev_err_probe(nandc->dev, ret, |
| 2796 | "tx DMA channel request failed\n"); |
Peter Ujfalusi | 80c3012 | 2020-02-27 14:37:47 +0200 | [diff] [blame] | 2797 | goto unalloc; |
Abhishek Sahu | 497d7d8 | 2017-08-11 17:09:19 +0530 | [diff] [blame] | 2798 | } |
| 2799 | |
Peter Ujfalusi | 92f0f8e | 2020-02-27 14:37:48 +0200 | [diff] [blame] | 2800 | nandc->rx_chan = dma_request_chan(nandc->dev, "rx"); |
| 2801 | if (IS_ERR(nandc->rx_chan)) { |
| 2802 | ret = PTR_ERR(nandc->rx_chan); |
| 2803 | nandc->rx_chan = NULL; |
Krzysztof Kozlowski | 07eb014 | 2020-09-01 16:25:35 +0200 | [diff] [blame] | 2804 | dev_err_probe(nandc->dev, ret, |
| 2805 | "rx DMA channel request failed\n"); |
Peter Ujfalusi | 80c3012 | 2020-02-27 14:37:47 +0200 | [diff] [blame] | 2806 | goto unalloc; |
Abhishek Sahu | 497d7d8 | 2017-08-11 17:09:19 +0530 | [diff] [blame] | 2807 | } |
| 2808 | |
Peter Ujfalusi | 92f0f8e | 2020-02-27 14:37:48 +0200 | [diff] [blame] | 2809 | nandc->cmd_chan = dma_request_chan(nandc->dev, "cmd"); |
| 2810 | if (IS_ERR(nandc->cmd_chan)) { |
| 2811 | ret = PTR_ERR(nandc->cmd_chan); |
| 2812 | nandc->cmd_chan = NULL; |
Krzysztof Kozlowski | 07eb014 | 2020-09-01 16:25:35 +0200 | [diff] [blame] | 2813 | dev_err_probe(nandc->dev, ret, |
| 2814 | "cmd DMA channel request failed\n"); |
Peter Ujfalusi | 80c3012 | 2020-02-27 14:37:47 +0200 | [diff] [blame] | 2815 | goto unalloc; |
Abhishek Sahu | 497d7d8 | 2017-08-11 17:09:19 +0530 | [diff] [blame] | 2816 | } |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 2817 | |
| 2818 | /* |
| 2819 | * Initially allocate BAM transaction to read ONFI param page. |
| 2820 | * After detecting all the devices, this BAM transaction will |
| 2821 | * be freed and the next BAM tranasction will be allocated with |
| 2822 | * maximum codeword size |
| 2823 | */ |
| 2824 | nandc->max_cwperpage = 1; |
| 2825 | nandc->bam_txn = alloc_bam_transaction(nandc); |
| 2826 | if (!nandc->bam_txn) { |
| 2827 | dev_err(nandc->dev, |
| 2828 | "failed to allocate bam transaction\n"); |
Peter Ujfalusi | 80c3012 | 2020-02-27 14:37:47 +0200 | [diff] [blame] | 2829 | ret = -ENOMEM; |
| 2830 | goto unalloc; |
Abhishek Sahu | cb80f11 | 2017-08-17 17:37:40 +0530 | [diff] [blame] | 2831 | } |
Abhishek Sahu | 497d7d8 | 2017-08-11 17:09:19 +0530 | [diff] [blame] | 2832 | } else { |
Peter Ujfalusi | 92f0f8e | 2020-02-27 14:37:48 +0200 | [diff] [blame] | 2833 | nandc->chan = dma_request_chan(nandc->dev, "rxtx"); |
| 2834 | if (IS_ERR(nandc->chan)) { |
| 2835 | ret = PTR_ERR(nandc->chan); |
| 2836 | nandc->chan = NULL; |
Krzysztof Kozlowski | 07eb014 | 2020-09-01 16:25:35 +0200 | [diff] [blame] | 2837 | dev_err_probe(nandc->dev, ret, |
| 2838 | "rxtx DMA channel request failed\n"); |
Peter Ujfalusi | 92f0f8e | 2020-02-27 14:37:48 +0200 | [diff] [blame] | 2839 | return ret; |
Abhishek Sahu | 497d7d8 | 2017-08-11 17:09:19 +0530 | [diff] [blame] | 2840 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | INIT_LIST_HEAD(&nandc->desc_list); |
| 2844 | INIT_LIST_HEAD(&nandc->host_list); |
| 2845 | |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 2846 | nand_controller_init(&nandc->controller); |
Miquel Raynal | 6a3cec6 | 2018-07-20 17:15:22 +0200 | [diff] [blame] | 2847 | nandc->controller.ops = &qcom_nandc_ops; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2848 | |
| 2849 | return 0; |
Peter Ujfalusi | 80c3012 | 2020-02-27 14:37:47 +0200 | [diff] [blame] | 2850 | unalloc: |
| 2851 | qcom_nandc_unalloc(nandc); |
| 2852 | return ret; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | /* one time setup of a few nand controller registers */ |
| 2856 | static int qcom_nandc_setup(struct qcom_nand_controller *nandc) |
| 2857 | { |
Abhishek Sahu | 9d43f91 | 2017-08-17 17:37:45 +0530 | [diff] [blame] | 2858 | u32 nand_ctrl; |
| 2859 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2860 | /* kill onenand */ |
Sivaprakash Murugesan | 443440c | 2020-06-12 13:28:15 +0530 | [diff] [blame] | 2861 | if (!nandc->props->is_qpic) |
| 2862 | nandc_write(nandc, SFLASHC_BURST_CFG, 0); |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 2863 | |
| 2864 | if (!nandc->props->qpic_v2) |
| 2865 | nandc_write(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD_VLD), |
| 2866 | NAND_DEV_CMD_VLD_VAL); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2867 | |
Abhishek Sahu | 9d43f91 | 2017-08-17 17:37:45 +0530 | [diff] [blame] | 2868 | /* enable ADM or BAM DMA */ |
| 2869 | if (nandc->props->is_bam) { |
| 2870 | nand_ctrl = nandc_read(nandc, NAND_CTRL); |
Sivaprakash Murugesan | cb27239 | 2020-06-12 13:28:16 +0530 | [diff] [blame] | 2871 | |
| 2872 | /* |
| 2873 | *NAND_CTRL is an operational registers, and CPU |
| 2874 | * access to operational registers are read only |
| 2875 | * in BAM mode. So update the NAND_CTRL register |
| 2876 | * only if it is not in BAM mode. In most cases BAM |
| 2877 | * mode will be enabled in bootloader |
| 2878 | */ |
| 2879 | if (!(nand_ctrl & BAM_MODE_EN)) |
| 2880 | nandc_write(nandc, NAND_CTRL, nand_ctrl | BAM_MODE_EN); |
Abhishek Sahu | 9d43f91 | 2017-08-17 17:37:45 +0530 | [diff] [blame] | 2881 | } else { |
| 2882 | nandc_write(nandc, NAND_FLASH_CHIP_SELECT, DM_EN); |
| 2883 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2884 | |
| 2885 | /* save the original values of these registers */ |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 2886 | if (!nandc->props->qpic_v2) { |
| 2887 | nandc->cmd1 = nandc_read(nandc, dev_cmd_reg_addr(nandc, NAND_DEV_CMD1)); |
| 2888 | nandc->vld = NAND_DEV_CMD_VLD_VAL; |
| 2889 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2890 | |
| 2891 | return 0; |
| 2892 | } |
| 2893 | |
Baruch Siach | 21020be | 2021-04-21 21:31:09 +0300 | [diff] [blame] | 2894 | static const char * const probes[] = { "cmdlinepart", "ofpart", "qcomsmem", NULL }; |
Manivannan Sadhasivam | 82bfd11 | 2021-01-04 09:41:36 +0530 | [diff] [blame] | 2895 | |
Miquel Raynal | 6a3cec6 | 2018-07-20 17:15:22 +0200 | [diff] [blame] | 2896 | static int qcom_nand_host_init_and_register(struct qcom_nand_controller *nandc, |
| 2897 | struct qcom_nand_host *host, |
| 2898 | struct device_node *dn) |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2899 | { |
| 2900 | struct nand_chip *chip = &host->chip; |
| 2901 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 2902 | struct device *dev = nandc->dev; |
| 2903 | int ret; |
| 2904 | |
| 2905 | ret = of_property_read_u32(dn, "reg", &host->cs); |
| 2906 | if (ret) { |
| 2907 | dev_err(dev, "can't get chip-select\n"); |
| 2908 | return -ENXIO; |
| 2909 | } |
| 2910 | |
| 2911 | nand_set_flash_node(chip, dn); |
| 2912 | mtd->name = devm_kasprintf(dev, GFP_KERNEL, "qcom_nand.%d", host->cs); |
Fabio Estevam | 069f053 | 2018-01-05 18:02:55 -0200 | [diff] [blame] | 2913 | if (!mtd->name) |
| 2914 | return -ENOMEM; |
| 2915 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2916 | mtd->owner = THIS_MODULE; |
| 2917 | mtd->dev.parent = dev; |
| 2918 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 2919 | chip->legacy.cmdfunc = qcom_nandc_command; |
Boris Brezillon | 7d6c37e | 2018-11-11 08:55:22 +0100 | [diff] [blame] | 2920 | chip->legacy.select_chip = qcom_nandc_select_chip; |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 2921 | chip->legacy.read_byte = qcom_nandc_read_byte; |
| 2922 | chip->legacy.read_buf = qcom_nandc_read_buf; |
| 2923 | chip->legacy.write_buf = qcom_nandc_write_buf; |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2924 | chip->legacy.set_features = nand_get_set_features_notsupp; |
| 2925 | chip->legacy.get_features = nand_get_set_features_notsupp; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2926 | |
| 2927 | /* |
| 2928 | * the bad block marker is readable only when we read the last codeword |
| 2929 | * of a page with ECC disabled. currently, the nand_base and nand_bbt |
| 2930 | * helpers don't allow us to read BB from a nand chip with ECC |
| 2931 | * disabled (MTD_OPS_PLACE_OOB is set by default). use the block_bad |
| 2932 | * and block_markbad helpers until we permanently switch to using |
| 2933 | * MTD_OPS_RAW for all drivers (with the help of badblockbits) |
| 2934 | */ |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 2935 | chip->legacy.block_bad = qcom_nandc_block_bad; |
| 2936 | chip->legacy.block_markbad = qcom_nandc_block_markbad; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2937 | |
| 2938 | chip->controller = &nandc->controller; |
Miquel Raynal | ce8148d | 2020-05-07 12:52:31 +0200 | [diff] [blame] | 2939 | chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USES_DMA | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2940 | NAND_SKIP_BBTSCAN; |
| 2941 | |
| 2942 | /* set up initial status value */ |
| 2943 | host->status = NAND_STATUS_READY | NAND_STATUS_WP; |
| 2944 | |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 2945 | ret = nand_scan(chip, 1); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2946 | if (ret) |
| 2947 | return ret; |
| 2948 | |
Christian Lamparter | 81d9bdf | 2018-12-23 01:31:26 +0100 | [diff] [blame] | 2949 | if (nandc->props->is_bam) { |
| 2950 | free_bam_transaction(nandc); |
| 2951 | nandc->bam_txn = alloc_bam_transaction(nandc); |
| 2952 | if (!nandc->bam_txn) { |
| 2953 | dev_err(nandc->dev, |
| 2954 | "failed to allocate bam transaction\n"); |
Manivannan Sadhasivam | ab2c8d3 | 2021-04-02 20:31:28 +0530 | [diff] [blame] | 2955 | nand_cleanup(chip); |
Christian Lamparter | 81d9bdf | 2018-12-23 01:31:26 +0100 | [diff] [blame] | 2956 | return -ENOMEM; |
| 2957 | } |
| 2958 | } |
| 2959 | |
Manivannan Sadhasivam | 82bfd11 | 2021-01-04 09:41:36 +0530 | [diff] [blame] | 2960 | ret = mtd_device_parse_register(mtd, probes, NULL, NULL, 0); |
Abhishek Sahu | 89f5127 | 2017-07-19 17:17:58 +0530 | [diff] [blame] | 2961 | if (ret) |
Miquel Raynal | 6a3cec6 | 2018-07-20 17:15:22 +0200 | [diff] [blame] | 2962 | nand_cleanup(chip); |
Abhishek Sahu | 89f5127 | 2017-07-19 17:17:58 +0530 | [diff] [blame] | 2963 | |
| 2964 | return ret; |
| 2965 | } |
| 2966 | |
| 2967 | static int qcom_probe_nand_devices(struct qcom_nand_controller *nandc) |
| 2968 | { |
| 2969 | struct device *dev = nandc->dev; |
| 2970 | struct device_node *dn = dev->of_node, *child; |
Miquel Raynal | 6a3cec6 | 2018-07-20 17:15:22 +0200 | [diff] [blame] | 2971 | struct qcom_nand_host *host; |
Manivannan Sadhasivam | 55fbb9b | 2021-03-02 18:57:57 +0530 | [diff] [blame] | 2972 | int ret = -ENODEV; |
Abhishek Sahu | 89f5127 | 2017-07-19 17:17:58 +0530 | [diff] [blame] | 2973 | |
Miquel Raynal | 6a3cec6 | 2018-07-20 17:15:22 +0200 | [diff] [blame] | 2974 | for_each_available_child_of_node(dn, child) { |
| 2975 | host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL); |
| 2976 | if (!host) { |
| 2977 | of_node_put(child); |
| 2978 | return -ENOMEM; |
Abhishek Sahu | 89f5127 | 2017-07-19 17:17:58 +0530 | [diff] [blame] | 2979 | } |
Miquel Raynal | 6a3cec6 | 2018-07-20 17:15:22 +0200 | [diff] [blame] | 2980 | |
| 2981 | ret = qcom_nand_host_init_and_register(nandc, host, child); |
| 2982 | if (ret) { |
| 2983 | devm_kfree(dev, host); |
| 2984 | continue; |
| 2985 | } |
| 2986 | |
| 2987 | list_add_tail(&host->node, &nandc->host_list); |
Abhishek Sahu | 89f5127 | 2017-07-19 17:17:58 +0530 | [diff] [blame] | 2988 | } |
| 2989 | |
Manivannan Sadhasivam | 55fbb9b | 2021-03-02 18:57:57 +0530 | [diff] [blame] | 2990 | return ret; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | /* parse custom DT properties here */ |
| 2994 | static int qcom_nandc_parse_dt(struct platform_device *pdev) |
| 2995 | { |
| 2996 | struct qcom_nand_controller *nandc = platform_get_drvdata(pdev); |
| 2997 | struct device_node *np = nandc->dev->of_node; |
| 2998 | int ret; |
| 2999 | |
Abhishek Sahu | 497d7d8 | 2017-08-11 17:09:19 +0530 | [diff] [blame] | 3000 | if (!nandc->props->is_bam) { |
| 3001 | ret = of_property_read_u32(np, "qcom,cmd-crci", |
| 3002 | &nandc->cmd_crci); |
| 3003 | if (ret) { |
| 3004 | dev_err(nandc->dev, "command CRCI unspecified\n"); |
| 3005 | return ret; |
| 3006 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3007 | |
Abhishek Sahu | 497d7d8 | 2017-08-11 17:09:19 +0530 | [diff] [blame] | 3008 | ret = of_property_read_u32(np, "qcom,data-crci", |
| 3009 | &nandc->data_crci); |
| 3010 | if (ret) { |
| 3011 | dev_err(nandc->dev, "data CRCI unspecified\n"); |
| 3012 | return ret; |
| 3013 | } |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | return 0; |
| 3017 | } |
| 3018 | |
| 3019 | static int qcom_nandc_probe(struct platform_device *pdev) |
| 3020 | { |
| 3021 | struct qcom_nand_controller *nandc; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3022 | const void *dev_data; |
| 3023 | struct device *dev = &pdev->dev; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3024 | struct resource *res; |
| 3025 | int ret; |
| 3026 | |
| 3027 | nandc = devm_kzalloc(&pdev->dev, sizeof(*nandc), GFP_KERNEL); |
| 3028 | if (!nandc) |
| 3029 | return -ENOMEM; |
| 3030 | |
| 3031 | platform_set_drvdata(pdev, nandc); |
| 3032 | nandc->dev = dev; |
| 3033 | |
| 3034 | dev_data = of_device_get_match_data(dev); |
| 3035 | if (!dev_data) { |
| 3036 | dev_err(&pdev->dev, "failed to get device data\n"); |
| 3037 | return -ENODEV; |
| 3038 | } |
| 3039 | |
Abhishek Sahu | 58f1f22 | 2017-08-11 17:09:17 +0530 | [diff] [blame] | 3040 | nandc->props = dev_data; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3041 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3042 | nandc->core_clk = devm_clk_get(dev, "core"); |
| 3043 | if (IS_ERR(nandc->core_clk)) |
| 3044 | return PTR_ERR(nandc->core_clk); |
| 3045 | |
| 3046 | nandc->aon_clk = devm_clk_get(dev, "aon"); |
| 3047 | if (IS_ERR(nandc->aon_clk)) |
| 3048 | return PTR_ERR(nandc->aon_clk); |
| 3049 | |
| 3050 | ret = qcom_nandc_parse_dt(pdev); |
| 3051 | if (ret) |
| 3052 | return ret; |
| 3053 | |
Arnd Bergmann | 7330fc5 | 2018-07-17 22:27:42 +0200 | [diff] [blame] | 3054 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 3055 | nandc->base = devm_ioremap_resource(dev, res); |
| 3056 | if (IS_ERR(nandc->base)) |
| 3057 | return PTR_ERR(nandc->base); |
| 3058 | |
| 3059 | nandc->base_phys = res->start; |
| 3060 | nandc->base_dma = dma_map_resource(dev, res->start, |
| 3061 | resource_size(res), |
| 3062 | DMA_BIDIRECTIONAL, 0); |
Manivannan Sadhasivam | 32cbc7c | 2021-04-05 10:39:12 +0530 | [diff] [blame] | 3063 | if (dma_mapping_error(dev, nandc->base_dma)) |
Arnd Bergmann | 7330fc5 | 2018-07-17 22:27:42 +0200 | [diff] [blame] | 3064 | return -ENXIO; |
| 3065 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3066 | ret = qcom_nandc_alloc(nandc); |
| 3067 | if (ret) |
Arnd Bergmann | 7330fc5 | 2018-07-17 22:27:42 +0200 | [diff] [blame] | 3068 | goto err_nandc_alloc; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3069 | |
| 3070 | ret = clk_prepare_enable(nandc->core_clk); |
| 3071 | if (ret) |
| 3072 | goto err_core_clk; |
| 3073 | |
| 3074 | ret = clk_prepare_enable(nandc->aon_clk); |
| 3075 | if (ret) |
| 3076 | goto err_aon_clk; |
| 3077 | |
| 3078 | ret = qcom_nandc_setup(nandc); |
| 3079 | if (ret) |
| 3080 | goto err_setup; |
| 3081 | |
Abhishek Sahu | 89f5127 | 2017-07-19 17:17:58 +0530 | [diff] [blame] | 3082 | ret = qcom_probe_nand_devices(nandc); |
| 3083 | if (ret) |
| 3084 | goto err_setup; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3085 | |
| 3086 | return 0; |
| 3087 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3088 | err_setup: |
| 3089 | clk_disable_unprepare(nandc->aon_clk); |
| 3090 | err_aon_clk: |
| 3091 | clk_disable_unprepare(nandc->core_clk); |
| 3092 | err_core_clk: |
| 3093 | qcom_nandc_unalloc(nandc); |
Arnd Bergmann | 7330fc5 | 2018-07-17 22:27:42 +0200 | [diff] [blame] | 3094 | err_nandc_alloc: |
| 3095 | dma_unmap_resource(dev, res->start, resource_size(res), |
| 3096 | DMA_BIDIRECTIONAL, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3097 | |
| 3098 | return ret; |
| 3099 | } |
| 3100 | |
| 3101 | static int qcom_nandc_remove(struct platform_device *pdev) |
| 3102 | { |
| 3103 | struct qcom_nand_controller *nandc = platform_get_drvdata(pdev); |
Arnd Bergmann | 7330fc5 | 2018-07-17 22:27:42 +0200 | [diff] [blame] | 3104 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3105 | struct qcom_nand_host *host; |
Miquel Raynal | 0a2bc99 | 2020-05-19 15:00:17 +0200 | [diff] [blame] | 3106 | struct nand_chip *chip; |
| 3107 | int ret; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3108 | |
Miquel Raynal | 0a2bc99 | 2020-05-19 15:00:17 +0200 | [diff] [blame] | 3109 | list_for_each_entry(host, &nandc->host_list, node) { |
| 3110 | chip = &host->chip; |
| 3111 | ret = mtd_device_unregister(nand_to_mtd(chip)); |
| 3112 | WARN_ON(ret); |
| 3113 | nand_cleanup(chip); |
| 3114 | } |
Arnd Bergmann | 7330fc5 | 2018-07-17 22:27:42 +0200 | [diff] [blame] | 3115 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3116 | qcom_nandc_unalloc(nandc); |
| 3117 | |
| 3118 | clk_disable_unprepare(nandc->aon_clk); |
| 3119 | clk_disable_unprepare(nandc->core_clk); |
| 3120 | |
Arnd Bergmann | 7330fc5 | 2018-07-17 22:27:42 +0200 | [diff] [blame] | 3121 | dma_unmap_resource(&pdev->dev, nandc->base_dma, resource_size(res), |
| 3122 | DMA_BIDIRECTIONAL, 0); |
| 3123 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3124 | return 0; |
| 3125 | } |
| 3126 | |
Abhishek Sahu | 58f1f22 | 2017-08-11 17:09:17 +0530 | [diff] [blame] | 3127 | static const struct qcom_nandc_props ipq806x_nandc_props = { |
| 3128 | .ecc_modes = (ECC_RS_4BIT | ECC_BCH_8BIT), |
Abhishek Sahu | 8c5d5d6 | 2017-08-11 17:09:18 +0530 | [diff] [blame] | 3129 | .is_bam = false, |
Abhishek Sahu | cc409b9 | 2017-08-17 17:37:47 +0530 | [diff] [blame] | 3130 | .dev_cmd_reg_start = 0x0, |
Abhishek Sahu | 58f1f22 | 2017-08-11 17:09:17 +0530 | [diff] [blame] | 3131 | }; |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3132 | |
Abhishek Sahu | a063783 | 2017-08-17 17:37:53 +0530 | [diff] [blame] | 3133 | static const struct qcom_nandc_props ipq4019_nandc_props = { |
| 3134 | .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT), |
| 3135 | .is_bam = true, |
Sivaprakash Murugesan | 443440c | 2020-06-12 13:28:15 +0530 | [diff] [blame] | 3136 | .is_qpic = true, |
Abhishek Sahu | a063783 | 2017-08-17 17:37:53 +0530 | [diff] [blame] | 3137 | .dev_cmd_reg_start = 0x0, |
| 3138 | }; |
| 3139 | |
Abhishek Sahu | dce8476 | 2017-08-17 17:37:54 +0530 | [diff] [blame] | 3140 | static const struct qcom_nandc_props ipq8074_nandc_props = { |
| 3141 | .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT), |
| 3142 | .is_bam = true, |
Sivaprakash Murugesan | 443440c | 2020-06-12 13:28:15 +0530 | [diff] [blame] | 3143 | .is_qpic = true, |
Abhishek Sahu | dce8476 | 2017-08-17 17:37:54 +0530 | [diff] [blame] | 3144 | .dev_cmd_reg_start = 0x7000, |
| 3145 | }; |
| 3146 | |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 3147 | static const struct qcom_nandc_props sdx55_nandc_props = { |
| 3148 | .ecc_modes = (ECC_BCH_4BIT | ECC_BCH_8BIT), |
| 3149 | .is_bam = true, |
| 3150 | .is_qpic = true, |
| 3151 | .qpic_v2 = true, |
| 3152 | .dev_cmd_reg_start = 0x7000, |
| 3153 | }; |
| 3154 | |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3155 | /* |
| 3156 | * data will hold a struct pointer containing more differences once we support |
| 3157 | * more controller variants |
| 3158 | */ |
| 3159 | static const struct of_device_id qcom_nandc_of_match[] = { |
Abhishek Sahu | 58f1f22 | 2017-08-11 17:09:17 +0530 | [diff] [blame] | 3160 | { |
| 3161 | .compatible = "qcom,ipq806x-nand", |
| 3162 | .data = &ipq806x_nandc_props, |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3163 | }, |
Abhishek Sahu | a063783 | 2017-08-17 17:37:53 +0530 | [diff] [blame] | 3164 | { |
| 3165 | .compatible = "qcom,ipq4019-nand", |
| 3166 | .data = &ipq4019_nandc_props, |
| 3167 | }, |
Abhishek Sahu | dce8476 | 2017-08-17 17:37:54 +0530 | [diff] [blame] | 3168 | { |
Kathiravan T | 6285862 | 2020-10-13 10:45:23 +0530 | [diff] [blame] | 3169 | .compatible = "qcom,ipq6018-nand", |
| 3170 | .data = &ipq8074_nandc_props, |
| 3171 | }, |
| 3172 | { |
Abhishek Sahu | dce8476 | 2017-08-17 17:37:54 +0530 | [diff] [blame] | 3173 | .compatible = "qcom,ipq8074-nand", |
| 3174 | .data = &ipq8074_nandc_props, |
| 3175 | }, |
Manivannan Sadhasivam | b120958 | 2020-11-26 14:27:05 +0530 | [diff] [blame] | 3176 | { |
| 3177 | .compatible = "qcom,sdx55-nand", |
| 3178 | .data = &sdx55_nandc_props, |
| 3179 | }, |
Archit Taneja | c76b78d | 2016-02-03 14:29:50 +0530 | [diff] [blame] | 3180 | {} |
| 3181 | }; |
| 3182 | MODULE_DEVICE_TABLE(of, qcom_nandc_of_match); |
| 3183 | |
| 3184 | static struct platform_driver qcom_nandc_driver = { |
| 3185 | .driver = { |
| 3186 | .name = "qcom-nandc", |
| 3187 | .of_match_table = qcom_nandc_of_match, |
| 3188 | }, |
| 3189 | .probe = qcom_nandc_probe, |
| 3190 | .remove = qcom_nandc_remove, |
| 3191 | }; |
| 3192 | module_platform_driver(qcom_nandc_driver); |
| 3193 | |
| 3194 | MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>"); |
| 3195 | MODULE_DESCRIPTION("Qualcomm NAND Controller driver"); |
| 3196 | MODULE_LICENSE("GPL v2"); |