mtdnand.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * File : nand.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-7-8 heyuanjie the first version
  23. */
  24. /*
  25. * COPYRIGHT (C) 2012, Shanghai Real Thread
  26. */
  27. #ifndef _MTDNAND_H_
  28. #define _MTDNAND_H_
  29. #include "mtd.h"
  30. /* Status bits */
  31. #define NAND_STATUS_FAIL 0x01
  32. #define NAND_STATUS_FAIL_N1 0x02
  33. #define NAND_STATUS_WP 0x80
  34. typedef enum
  35. {
  36. NAND_CMD_PAGE_RD, /* read data to chip's page buffer,do WaitBusy after this cmd in low driver */
  37. NAND_CMD_PAGE_WR0, /* write data to chip's page buffer */
  38. NAND_CMD_PAGE_WR1, /* do flash programe */
  39. NAND_CMD_BLK_ERASE, /* erase block */
  40. NAND_CMD_ECC_EN, /* enable gen HWECC */
  41. NAND_CMD_ECC_DIS /* disable gen HWECC */
  42. } nand_cmd_t;
  43. typedef enum
  44. {
  45. NAND_ECCM_NONE,
  46. NAND_ECCM_HW,
  47. } nand_eccmode_t;
  48. struct nand_chip;
  49. struct nand_ops;
  50. /**
  51. * struct nand_buffers - buffer structure for read/write
  52. * @ecccalc: buffer pointer for calculated ECC, size is oobsize.
  53. * @ecccode: buffer pointer for ECC read from flash, size is oobsize.
  54. *
  55. */
  56. struct nand_buffers
  57. {
  58. uint8_t *ecccalc;
  59. uint8_t *ecccode;
  60. };
  61. struct nand_ecc
  62. {
  63. uint8_t mode; /* nand_eccmode_t */
  64. uint8_t bytes; /* gen ecc bytes per ecc step(usually 3) */
  65. uint16_t stepsize:12; /* min 256 */
  66. uint16_t _step:4; /* */
  67. /* driver must set the two interface if HWECC */
  68. void (*calculate)(struct nand_chip *chip, const uint8_t *dat, uint8_t *ecc_code);
  69. /* return max bit flips if can't correct,return -1 ECC is error(0 success) */
  70. int(*correct)(struct nand_chip *chip, uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc);
  71. /* ignore if NONECC */
  72. const struct mtd_oob_region *layout;
  73. };
  74. typedef struct nand_chip
  75. {
  76. rt_mtd_t parent;
  77. /* driver must init these */
  78. const struct nand_ops *ops;
  79. struct nand_ecc ecc;
  80. const struct mtd_oob_region *freelayout;
  81. /* driver do not touch */
  82. struct nand_buffers buffers;
  83. uint8_t *oob_poi;
  84. uint8_t *pagebuf;
  85. uint32_t size;
  86. uint16_t oobsize;
  87. uint8_t pages_pb;
  88. uint16_t page_size;
  89. int(*read_page)(struct nand_chip *chip, uint8_t *buf, int oob_required, int page);
  90. int(*write_page)(struct nand_chip *chip, const uint8_t *buf, int oob_required, int page);
  91. }rt_nand_t;
  92. struct nand_ops
  93. {
  94. int(*cmdfunc)(rt_nand_t *nand, int cmd, int page, int offset); /* send nand operation cmd, return Status bits(0 success),
  95. if nand is busy please wait in low driver */
  96. int(*read_buf)(rt_nand_t *nand, uint8_t *buf, int len); /* read data from nand chip's page buffer */
  97. int(*write_buf)(rt_nand_t *nand, const uint8_t *buf, int len);/* write data to nand chip's page buffer */
  98. int(*isbad)(rt_nand_t *nand, uint32_t blk); /* if NULL OOB[0] used as bad mark(not 0xff is bad) */
  99. int(*markbad)(rt_nand_t *nand, uint32_t blk); /* if NULL OOB[0] used as bad mark(set to 0x00) */
  100. };
  101. int rt_mtd_nand_init(rt_nand_t *nand, int blk_size, int page_size, int blks_pc, int oob_size);
  102. #endif /* MTD_NAND_H_ */