1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * QCOM QPIC common APIs header file 4 * 5 * Copyright (c) 2023 Qualcomm Inc. 6 * Authors: Md sadre Alam <quic_mdalam@quicinc.com> 7 * 8 */ 9 #ifndef __MTD_NAND_QPIC_COMMON_H__ 10 #define __MTD_NAND_QPIC_COMMON_H__ 11 12 /* NANDc reg offsets */ 13 #define NAND_FLASH_CMD 0x00 14 #define NAND_ADDR0 0x04 15 #define NAND_ADDR1 0x08 16 #define NAND_FLASH_CHIP_SELECT 0x0c 17 #define NAND_EXEC_CMD 0x10 18 #define NAND_FLASH_STATUS 0x14 19 #define NAND_BUFFER_STATUS 0x18 20 #define NAND_DEV0_CFG0 0x20 21 #define NAND_DEV0_CFG1 0x24 22 #define NAND_DEV0_ECC_CFG 0x28 23 #define NAND_AUTO_STATUS_EN 0x2c 24 #define NAND_DEV1_CFG0 0x30 25 #define NAND_DEV1_CFG1 0x34 26 #define NAND_READ_ID 0x40 27 #define NAND_READ_STATUS 0x44 28 #define NAND_DEV_CMD0 0xa0 29 #define NAND_DEV_CMD1 0xa4 30 #define NAND_DEV_CMD2 0xa8 31 #define NAND_DEV_CMD_VLD 0xac 32 #define SFLASHC_BURST_CFG 0xe0 33 #define NAND_ERASED_CW_DETECT_CFG 0xe8 34 #define NAND_ERASED_CW_DETECT_STATUS 0xec 35 #define NAND_EBI2_ECC_BUF_CFG 0xf0 36 #define FLASH_BUF_ACC 0x100 37 38 #define NAND_CTRL 0xf00 39 #define NAND_VERSION 0xf08 40 #define NAND_READ_LOCATION_0 0xf20 41 #define NAND_READ_LOCATION_1 0xf24 42 #define NAND_READ_LOCATION_2 0xf28 43 #define NAND_READ_LOCATION_3 0xf2c 44 #define NAND_READ_LOCATION_LAST_CW_0 0xf40 45 #define NAND_READ_LOCATION_LAST_CW_1 0xf44 46 #define NAND_READ_LOCATION_LAST_CW_2 0xf48 47 #define NAND_READ_LOCATION_LAST_CW_3 0xf4c 48 49 /* dummy register offsets, used by qcom_write_reg_dma */ 50 #define NAND_DEV_CMD1_RESTORE 0xdead 51 #define NAND_DEV_CMD_VLD_RESTORE 0xbeef 52 53 /* NAND_FLASH_CMD bits */ 54 #define PAGE_ACC BIT(4) 55 #define LAST_PAGE BIT(5) 56 57 /* NAND_FLASH_CHIP_SELECT bits */ 58 #define NAND_DEV_SEL 0 59 #define DM_EN BIT(2) 60 61 /* NAND_FLASH_STATUS bits */ 62 #define FS_OP_ERR BIT(4) 63 #define FS_READY_BSY_N BIT(5) 64 #define FS_MPU_ERR BIT(8) 65 #define FS_DEVICE_STS_ERR BIT(16) 66 #define FS_DEVICE_WP BIT(23) 67 68 /* NAND_BUFFER_STATUS bits */ 69 #define BS_UNCORRECTABLE_BIT BIT(8) 70 #define BS_CORRECTABLE_ERR_MSK 0x1f 71 72 /* NAND_DEVn_CFG0 bits */ 73 #define DISABLE_STATUS_AFTER_WRITE BIT(4) 74 #define CW_PER_PAGE 6 75 #define CW_PER_PAGE_MASK GENMASK(8, 6) 76 #define UD_SIZE_BYTES 9 77 #define UD_SIZE_BYTES_MASK GENMASK(18, 9) 78 #define ECC_PARITY_SIZE_BYTES_RS GENMASK(22, 19) 79 #define SPARE_SIZE_BYTES 23 80 #define SPARE_SIZE_BYTES_MASK GENMASK(26, 23) 81 #define NUM_ADDR_CYCLES 27 82 #define NUM_ADDR_CYCLES_MASK GENMASK(29, 27) 83 #define STATUS_BFR_READ BIT(30) 84 #define SET_RD_MODE_AFTER_STATUS BIT(31) 85 86 /* NAND_DEVn_CFG0 bits */ 87 #define DEV0_CFG1_ECC_DISABLE BIT(0) 88 #define WIDE_FLASH BIT(1) 89 #define NAND_RECOVERY_CYCLES 2 90 #define NAND_RECOVERY_CYCLES_MASK GENMASK(4, 2) 91 #define CS_ACTIVE_BSY BIT(5) 92 #define BAD_BLOCK_BYTE_NUM 6 93 #define BAD_BLOCK_BYTE_NUM_MASK GENMASK(15, 6) 94 #define BAD_BLOCK_IN_SPARE_AREA BIT(16) 95 #define WR_RD_BSY_GAP 17 96 #define WR_RD_BSY_GAP_MASK GENMASK(22, 17) 97 #define ENABLE_BCH_ECC BIT(27) 98 99 /* NAND_DEV0_ECC_CFG bits */ 100 #define ECC_CFG_ECC_DISABLE BIT(0) 101 #define ECC_SW_RESET BIT(1) 102 #define ECC_MODE 4 103 #define ECC_MODE_MASK GENMASK(5, 4) 104 #define ECC_MODE_4BIT 0 105 #define ECC_MODE_8BIT 1 106 #define ECC_PARITY_SIZE_BYTES_BCH 8 107 #define ECC_PARITY_SIZE_BYTES_BCH_MASK GENMASK(12, 8) 108 #define ECC_NUM_DATA_BYTES 16 109 #define ECC_NUM_DATA_BYTES_MASK GENMASK(25, 16) 110 #define ECC_FORCE_CLK_OPEN BIT(30) 111 112 /* NAND_DEV_CMD1 bits */ 113 #define READ_ADDR_MASK GENMASK(7, 0) 114 115 /* NAND_DEV_CMD_VLD bits */ 116 #define READ_START_VLD BIT(0) 117 #define READ_STOP_VLD BIT(1) 118 #define WRITE_START_VLD BIT(2) 119 #define ERASE_START_VLD BIT(3) 120 #define SEQ_READ_START_VLD BIT(4) 121 122 /* NAND_EBI2_ECC_BUF_CFG bits */ 123 #define NUM_STEPS 0 124 #define NUM_STEPS_MASK GENMASK(9, 0) 125 126 /* NAND_ERASED_CW_DETECT_CFG bits */ 127 #define ERASED_CW_ECC_MASK 1 128 #define AUTO_DETECT_RES 0 129 #define MASK_ECC BIT(ERASED_CW_ECC_MASK) 130 #define RESET_ERASED_DET BIT(AUTO_DETECT_RES) 131 #define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES) 132 #define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC) 133 #define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC) 134 135 /* NAND_ERASED_CW_DETECT_STATUS bits */ 136 #define PAGE_ALL_ERASED BIT(7) 137 #define CODEWORD_ALL_ERASED BIT(6) 138 #define PAGE_ERASED BIT(5) 139 #define CODEWORD_ERASED BIT(4) 140 #define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED) 141 #define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED) 142 143 /* NAND_READ_LOCATION_n bits */ 144 #define READ_LOCATION_OFFSET 0 145 #define READ_LOCATION_OFFSET_MASK GENMASK(9, 0) 146 #define READ_LOCATION_SIZE 16 147 #define READ_LOCATION_SIZE_MASK GENMASK(25, 16) 148 #define READ_LOCATION_LAST 31 149 #define READ_LOCATION_LAST_MASK BIT(31) 150 151 /* Version Mask */ 152 #define NAND_VERSION_MAJOR_MASK 0xf0000000 153 #define NAND_VERSION_MAJOR_SHIFT 28 154 #define NAND_VERSION_MINOR_MASK 0x0fff0000 155 #define NAND_VERSION_MINOR_SHIFT 16 156 157 /* NAND OP_CMDs */ 158 #define OP_PAGE_READ 0x2 159 #define OP_PAGE_READ_WITH_ECC 0x3 160 #define OP_PAGE_READ_WITH_ECC_SPARE 0x4 161 #define OP_PAGE_READ_ONFI_READ 0x5 162 #define OP_PROGRAM_PAGE 0x6 163 #define OP_PAGE_PROGRAM_WITH_ECC 0x7 164 #define OP_PROGRAM_PAGE_SPARE 0x9 165 #define OP_BLOCK_ERASE 0xa 166 #define OP_CHECK_STATUS 0xc 167 #define OP_FETCH_ID 0xb 168 #define OP_RESET_DEVICE 0xd 169 170 /* Default Value for NAND_DEV_CMD_VLD */ 171 #define NAND_DEV_CMD_VLD_VAL (READ_START_VLD | WRITE_START_VLD | \ 172 ERASE_START_VLD | SEQ_READ_START_VLD) 173 174 /* NAND_CTRL bits */ 175 #define BAM_MODE_EN BIT(0) 176 177 /* 178 * the NAND controller performs reads/writes with ECC in 516 byte chunks. 179 * the driver calls the chunks 'step' or 'codeword' interchangeably 180 */ 181 #define NANDC_STEP_SIZE 512 182 183 /* 184 * the largest page size we support is 8K, this will have 16 steps/codewords 185 * of 512 bytes each 186 */ 187 #define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE) 188 189 /* we read at most 3 registers per codeword scan */ 190 #define MAX_REG_RD (3 * MAX_NUM_STEPS) 191 192 /* ECC modes supported by the controller */ 193 #define ECC_NONE BIT(0) 194 #define ECC_RS_4BIT BIT(1) 195 #define ECC_BCH_4BIT BIT(2) 196 #define ECC_BCH_8BIT BIT(3) 197 198 /* 199 * Returns the actual register address for all NAND_DEV_ registers 200 * (i.e. NAND_DEV_CMD0, NAND_DEV_CMD1, NAND_DEV_CMD2 and NAND_DEV_CMD_VLD) 201 */ 202 #define dev_cmd_reg_addr(nandc, reg) ((nandc)->props->dev_cmd_reg_start + (reg)) 203 204 /* Returns the dma address for reg read buffer */ 205 #define reg_buf_dma_addr(chip, vaddr) \ 206 ((chip)->reg_read_dma + \ 207 ((u8 *)(vaddr) - (u8 *)(chip)->reg_read_buf)) 208 209 #define QPIC_PER_CW_CMD_ELEMENTS 32 210 #define QPIC_PER_CW_CMD_SGL 32 211 #define QPIC_PER_CW_DATA_SGL 8 212 213 #define QPIC_NAND_COMPLETION_TIMEOUT msecs_to_jiffies(2000) 214 215 /* 216 * Flags used in DMA descriptor preparation helper functions 217 * (i.e. qcom_read_reg_dma/qcom_write_reg_dma/qcom_read_data_dma/qcom_write_data_dma) 218 */ 219 /* Don't set the EOT in current tx BAM sgl */ 220 #define NAND_BAM_NO_EOT BIT(0) 221 /* Set the NWD flag in current BAM sgl */ 222 #define NAND_BAM_NWD BIT(1) 223 /* Finish writing in the current BAM sgl and start writing in another BAM sgl */ 224 #define NAND_BAM_NEXT_SGL BIT(2) 225 /* 226 * Erased codeword status is being used two times in single transfer so this 227 * flag will determine the current value of erased codeword status register 228 */ 229 #define NAND_ERASED_CW_SET BIT(4) 230 231 #define MAX_ADDRESS_CYCLE 5 232 233 /* 234 * This data type corresponds to the BAM transaction which will be used for all 235 * NAND transfers. 236 * @bam_ce - the array of BAM command elements 237 * @cmd_sgl - sgl for NAND BAM command pipe 238 * @data_sgl - sgl for NAND BAM consumer/producer pipe 239 * @last_data_desc - last DMA desc in data channel (tx/rx). 240 * @last_cmd_desc - last DMA desc in command channel. 241 * @txn_done - completion for NAND transfer. 242 * @bam_ce_nitems - the number of elements in the @bam_ce array 243 * @cmd_sgl_nitems - the number of elements in the @cmd_sgl array 244 * @data_sgl_nitems - the number of elements in the @data_sgl array 245 * @bam_ce_pos - the index in bam_ce which is available for next sgl 246 * @bam_ce_start - the index in bam_ce which marks the start position ce 247 * for current sgl. It will be used for size calculation 248 * for current sgl 249 * @cmd_sgl_pos - current index in command sgl. 250 * @cmd_sgl_start - start index in command sgl. 251 * @tx_sgl_pos - current index in data sgl for tx. 252 * @tx_sgl_start - start index in data sgl for tx. 253 * @rx_sgl_pos - current index in data sgl for rx. 254 * @rx_sgl_start - start index in data sgl for rx. 255 */ 256 struct bam_transaction { 257 struct bam_cmd_element *bam_ce; 258 struct scatterlist *cmd_sgl; 259 struct scatterlist *data_sgl; 260 struct dma_async_tx_descriptor *last_data_desc; 261 struct dma_async_tx_descriptor *last_cmd_desc; 262 struct completion txn_done; 263 264 unsigned int bam_ce_nitems; 265 unsigned int cmd_sgl_nitems; 266 unsigned int data_sgl_nitems; 267 268 struct_group(bam_positions, 269 u32 bam_ce_pos; 270 u32 bam_ce_start; 271 u32 cmd_sgl_pos; 272 u32 cmd_sgl_start; 273 u32 tx_sgl_pos; 274 u32 tx_sgl_start; 275 u32 rx_sgl_pos; 276 u32 rx_sgl_start; 277 278 ); 279 }; 280 281 /* 282 * This data type corresponds to the nand dma descriptor 283 * @dma_desc - low level DMA engine descriptor 284 * @list - list for desc_info 285 * 286 * @adm_sgl - sgl which will be used for single sgl dma descriptor. Only used by 287 * ADM 288 * @bam_sgl - sgl which will be used for dma descriptor. Only used by BAM 289 * @sgl_cnt - number of SGL in bam_sgl. Only used by BAM 290 * @dir - DMA transfer direction 291 */ 292 struct desc_info { 293 struct dma_async_tx_descriptor *dma_desc; 294 struct list_head node; 295 296 union { 297 struct scatterlist adm_sgl; 298 struct { 299 struct scatterlist *bam_sgl; 300 int sgl_cnt; 301 }; 302 }; 303 enum dma_data_direction dir; 304 }; 305 306 /* 307 * holds the current register values that we want to write. acts as a contiguous 308 * chunk of memory which we use to write the controller registers through DMA. 309 */ 310 struct nandc_regs { 311 __le32 cmd; 312 __le32 addr0; 313 __le32 addr1; 314 __le32 chip_sel; 315 __le32 exec; 316 317 __le32 cfg0; 318 __le32 cfg1; 319 __le32 ecc_bch_cfg; 320 321 __le32 clrflashstatus; 322 __le32 clrreadstatus; 323 324 __le32 cmd1; 325 __le32 vld; 326 327 __le32 orig_cmd1; 328 __le32 orig_vld; 329 330 __le32 ecc_buf_cfg; 331 __le32 read_location0; 332 __le32 read_location1; 333 __le32 read_location2; 334 __le32 read_location3; 335 __le32 read_location_last0; 336 __le32 read_location_last1; 337 __le32 read_location_last2; 338 __le32 read_location_last3; 339 __le32 spi_cfg; 340 __le32 num_addr_cycle; 341 __le32 busy_wait_cnt; 342 __le32 flash_feature; 343 344 __le32 erased_cw_detect_cfg_clr; 345 __le32 erased_cw_detect_cfg_set; 346 }; 347 348 /* 349 * NAND controller data struct 350 * 351 * @dev: parent device 352 * 353 * @base: MMIO base 354 * 355 * @core_clk: controller clock 356 * @aon_clk: another controller clock 357 * @iomacro_clk: io macro clock 358 * 359 * @regs: a contiguous chunk of memory for DMA register 360 * writes. contains the register values to be 361 * written to controller 362 * 363 * @props: properties of current NAND controller, 364 * initialized via DT match data 365 * 366 * @controller: base controller structure 367 * @qspi: qpic spi structure 368 * @host_list: list containing all the chips attached to the 369 * controller 370 * 371 * @chan: dma channel 372 * @cmd_crci: ADM DMA CRCI for command flow control 373 * @data_crci: ADM DMA CRCI for data flow control 374 * 375 * @desc_list: DMA descriptor list (list of desc_infos) 376 * 377 * @data_buffer: our local DMA buffer for page read/writes, 378 * used when we can't use the buffer provided 379 * by upper layers directly 380 * @reg_read_buf: local buffer for reading back registers via DMA 381 * 382 * @base_phys: physical base address of controller registers 383 * @base_dma: dma base address of controller registers 384 * @reg_read_dma: contains dma address for register read buffer 385 * 386 * @buf_size/count/start: markers for chip->legacy.read_buf/write_buf 387 * functions 388 * @max_cwperpage: maximum QPIC codewords required. calculated 389 * from all connected NAND devices pagesize 390 * 391 * @reg_read_pos: marker for data read in reg_read_buf 392 * 393 * @cmd1/vld: some fixed controller register values 394 * 395 * @exec_opwrite: flag to select correct number of code word 396 * while reading status 397 */ 398 struct qcom_nand_controller { 399 struct device *dev; 400 401 void __iomem *base; 402 403 struct clk *core_clk; 404 struct clk *aon_clk; 405 406 struct nandc_regs *regs; 407 struct bam_transaction *bam_txn; 408 409 const struct qcom_nandc_props *props; 410 411 struct nand_controller *controller; 412 struct qpic_spi_nand *qspi; 413 struct list_head host_list; 414 415 union { 416 /* will be used only by QPIC for BAM DMA */ 417 struct { 418 struct dma_chan *tx_chan; 419 struct dma_chan *rx_chan; 420 struct dma_chan *cmd_chan; 421 }; 422 423 /* will be used only by EBI2 for ADM DMA */ 424 struct { 425 struct dma_chan *chan; 426 unsigned int cmd_crci; 427 unsigned int data_crci; 428 }; 429 }; 430 431 struct list_head desc_list; 432 433 u8 *data_buffer; 434 __le32 *reg_read_buf; 435 436 phys_addr_t base_phys; 437 dma_addr_t base_dma; 438 dma_addr_t reg_read_dma; 439 440 int buf_size; 441 int buf_count; 442 int buf_start; 443 unsigned int max_cwperpage; 444 445 int reg_read_pos; 446 447 u32 cmd1, vld; 448 bool exec_opwrite; 449 }; 450 451 /* 452 * This data type corresponds to the NAND controller properties which varies 453 * among different NAND controllers. 454 * @ecc_modes - ecc mode for NAND 455 * @dev_cmd_reg_start - NAND_DEV_CMD_* registers starting offset 456 * @supports_bam - whether NAND controller is using BAM 457 * @nandc_part_of_qpic - whether NAND controller is part of qpic IP 458 * @qpic_version2 - flag to indicate QPIC IP version 2 459 * @use_codeword_fixup - whether NAND has different layout for boot partitions 460 */ 461 struct qcom_nandc_props { 462 u32 ecc_modes; 463 u32 dev_cmd_reg_start; 464 u32 bam_offset; 465 bool supports_bam; 466 bool nandc_part_of_qpic; 467 bool qpic_version2; 468 bool use_codeword_fixup; 469 }; 470 471 void qcom_free_bam_transaction(struct qcom_nand_controller *nandc); 472 struct bam_transaction *qcom_alloc_bam_transaction(struct qcom_nand_controller *nandc); 473 void qcom_clear_bam_transaction(struct qcom_nand_controller *nandc); 474 void qcom_qpic_bam_dma_done(void *data); 475 void qcom_nandc_dev_to_mem(struct qcom_nand_controller *nandc, bool is_cpu); 476 int qcom_prepare_bam_async_desc(struct qcom_nand_controller *nandc, 477 struct dma_chan *chan, unsigned long flags); 478 int qcom_prep_bam_dma_desc_cmd(struct qcom_nand_controller *nandc, bool read, 479 int reg_off, const void *vaddr, int size, unsigned int flags); 480 int qcom_prep_bam_dma_desc_data(struct qcom_nand_controller *nandc, bool read, 481 const void *vaddr, int size, unsigned int flags); 482 int qcom_prep_adm_dma_desc(struct qcom_nand_controller *nandc, bool read, int reg_off, 483 const void *vaddr, int size, bool flow_control); 484 int qcom_read_reg_dma(struct qcom_nand_controller *nandc, int first, int num_regs, 485 unsigned int flags); 486 int qcom_write_reg_dma(struct qcom_nand_controller *nandc, __le32 *vaddr, int first, 487 int num_regs, unsigned int flags); 488 int qcom_read_data_dma(struct qcom_nand_controller *nandc, int reg_off, const u8 *vaddr, 489 int size, unsigned int flags); 490 int qcom_write_data_dma(struct qcom_nand_controller *nandc, int reg_off, const u8 *vaddr, 491 int size, unsigned int flags); 492 int qcom_submit_descs(struct qcom_nand_controller *nandc); 493 void qcom_clear_read_regs(struct qcom_nand_controller *nandc); 494 void qcom_nandc_unalloc(struct qcom_nand_controller *nandc); 495 int qcom_nandc_alloc(struct qcom_nand_controller *nandc); 496 #endif 497 498