mtdnand.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. 2018-09-10 heyuanjie87 first version
  9. */
  10. #ifndef _MTDNAND_H_
  11. #define _MTDNAND_H_
  12. #include "mtd.h"
  13. /* Status bits */
  14. #define NAND_STATUS_FAIL 0x01
  15. #define NAND_STATUS_FAIL_N1 0x02
  16. #define NAND_STATUS_WP 0x80
  17. typedef enum
  18. {
  19. NAND_CMD_PAGE_RD, /* read data to chip's page buffer,do WaitBusy after this cmd in low driver */
  20. NAND_CMD_PAGE_WR0, /* write data to chip's page buffer */
  21. NAND_CMD_PAGE_WR1, /* do flash programe */
  22. NAND_CMD_BLK_ERASE, /* erase block */
  23. NAND_CMD_ECC_EN, /* enable gen HWECC */
  24. NAND_CMD_ECC_DIS /* disable gen HWECC */
  25. } nand_cmd_t;
  26. typedef enum
  27. {
  28. NAND_ECCM_NONE,
  29. NAND_ECCM_HW,
  30. } nand_eccmode_t;
  31. struct nand_chip;
  32. struct nand_ops;
  33. /**
  34. * struct nand_buffers - buffer structure for read/write
  35. * @ecccalc: buffer pointer for calculated ECC, size is oobsize.
  36. * @ecccode: buffer pointer for ECC read from flash, size is oobsize.
  37. *
  38. */
  39. struct nand_buffers
  40. {
  41. uint8_t *ecccalc;
  42. uint8_t *ecccode;
  43. };
  44. struct nand_ecc
  45. {
  46. uint8_t mode; /* nand_eccmode_t */
  47. uint8_t bytes; /* gen ecc bytes per ecc step(usually 3) */
  48. uint16_t stepsize:12; /* min 256 */
  49. uint16_t _step:4; /* */
  50. /* driver must set the two interface if HWECC */
  51. void (*calculate)(struct nand_chip *chip, const uint8_t *dat, uint8_t *ecc_code);
  52. /* return max bit flips if can't correct,return -1 ECC is error(0 success) */
  53. int(*correct)(struct nand_chip *chip, uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc);
  54. /* ignore if NONECC */
  55. const struct mtd_oob_region *layout;
  56. };
  57. typedef struct nand_chip
  58. {
  59. rt_mtd_t parent;
  60. /* driver must init these */
  61. const struct nand_ops *ops;
  62. struct nand_ecc ecc;
  63. const struct mtd_oob_region *freelayout;
  64. /* driver do not touch */
  65. struct nand_buffers buffers;
  66. uint8_t *oob_poi;
  67. uint8_t *pagebuf;
  68. uint32_t size;
  69. uint16_t oobsize;
  70. uint8_t pages_pb;
  71. uint16_t page_size;
  72. int(*read_page)(struct nand_chip *chip, uint8_t *buf, int oob_required, int page);
  73. int(*write_page)(struct nand_chip *chip, const uint8_t *buf, int oob_required, int page);
  74. }rt_nand_t;
  75. struct nand_ops
  76. {
  77. int(*cmdfunc)(rt_nand_t *nand, int cmd, int page, int offset); /* send nand operation cmd, return Status bits(0 success),
  78. if nand is busy please wait in low driver */
  79. int(*read_buf)(rt_nand_t *nand, uint8_t *buf, int len); /* read data from nand chip's page buffer */
  80. int(*write_buf)(rt_nand_t *nand, const uint8_t *buf, int len);/* write data to nand chip's page buffer */
  81. int(*isbad)(rt_nand_t *nand, uint32_t blk); /* if NULL OOB[0] used as bad mark(not 0xff is bad) */
  82. int(*markbad)(rt_nand_t *nand, uint32_t blk); /* if NULL OOB[0] used as bad mark(set to 0x00) */
  83. };
  84. int rt_mtd_nand_init(rt_nand_t *nand, int blk_size, int page_size, int oob_size);
  85. #endif /* MTD_NAND_H_ */