mtd_nand.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * 2011-12-05 Bernard the first version
  9. * 2011-04-02 prife add mark_badblock and check_block
  10. */
  11. /*
  12. * COPYRIGHT (C) 2012, Shanghai Real Thread
  13. */
  14. #ifndef __MTD_NAND_H__
  15. #define __MTD_NAND_H__
  16. #include <rtdevice.h>
  17. struct rt_mtd_nand_driver_ops;
  18. #define RT_MTD_NAND_DEVICE(device) ((struct rt_mtd_nand_device*)(device))
  19. #define RT_MTD_EOK 0 /* NO error */
  20. #define RT_MTD_EECC 1 /* ECC error */
  21. #define RT_MTD_EBUSY 2 /* hardware busy */
  22. #define RT_MTD_EIO 3 /* generic IO issue */
  23. #define RT_MTD_ENOMEM 4 /* out of memory */
  24. #define RT_MTD_ESRC 5 /* source issue */
  25. #define RT_MTD_EECC_CORRECT 6 /* ECC error but correct */
  26. struct rt_mtd_nand_device
  27. {
  28. struct rt_device parent;
  29. rt_uint16_t page_size; /* The Page size in the flash */
  30. rt_uint16_t oob_size; /* Out of bank size */
  31. rt_uint16_t oob_free; /* the free area in oob that flash driver not use */
  32. rt_uint16_t plane_num; /* the number of plane in the NAND Flash */
  33. rt_uint32_t pages_per_block; /* The number of page a block */
  34. rt_uint16_t block_total;
  35. rt_uint32_t block_start; /* The start of available block*/
  36. rt_uint32_t block_end; /* The end of available block */
  37. /* operations interface */
  38. const struct rt_mtd_nand_driver_ops* ops;
  39. };
  40. struct rt_mtd_nand_driver_ops
  41. {
  42. rt_err_t (*read_id) (struct rt_mtd_nand_device* device);
  43. rt_err_t (*read_page)(struct rt_mtd_nand_device* device,
  44. rt_off_t page,
  45. rt_uint8_t* data, rt_uint32_t data_len,
  46. rt_uint8_t * spare, rt_uint32_t spare_len);
  47. rt_err_t (*write_page)(struct rt_mtd_nand_device * device,
  48. rt_off_t page,
  49. const rt_uint8_t * data, rt_uint32_t data_len,
  50. const rt_uint8_t * spare, rt_uint32_t spare_len);
  51. rt_err_t (*move_page) (struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page);
  52. rt_err_t (*erase_block)(struct rt_mtd_nand_device* device, rt_uint32_t block);
  53. rt_err_t (*check_block)(struct rt_mtd_nand_device* device, rt_uint32_t block);
  54. rt_err_t (*mark_badblock)(struct rt_mtd_nand_device* device, rt_uint32_t block);
  55. };
  56. rt_err_t rt_mtd_nand_register_device(const char* name, struct rt_mtd_nand_device* device);
  57. rt_inline rt_uint32_t rt_mtd_nand_read_id(struct rt_mtd_nand_device* device)
  58. {
  59. return device->ops->read_id(device);
  60. }
  61. rt_inline rt_err_t rt_mtd_nand_read(
  62. struct rt_mtd_nand_device* device,
  63. rt_off_t page,
  64. rt_uint8_t* data, rt_uint32_t data_len,
  65. rt_uint8_t * spare, rt_uint32_t spare_len)
  66. {
  67. return device->ops->read_page(device, page, data, data_len, spare, spare_len);
  68. }
  69. rt_inline rt_err_t rt_mtd_nand_write(
  70. struct rt_mtd_nand_device* device,
  71. rt_off_t page,
  72. const rt_uint8_t* data, rt_uint32_t data_len,
  73. const rt_uint8_t * spare, rt_uint32_t spare_len)
  74. {
  75. return device->ops->write_page(device, page, data, data_len, spare, spare_len);
  76. }
  77. rt_inline rt_err_t rt_mtd_nand_move_page(struct rt_mtd_nand_device* device,
  78. rt_off_t src_page, rt_off_t dst_page)
  79. {
  80. return device->ops->move_page(device, src_page, dst_page);
  81. }
  82. rt_inline rt_err_t rt_mtd_nand_erase_block(struct rt_mtd_nand_device* device, rt_uint32_t block)
  83. {
  84. return device->ops->erase_block(device, block);
  85. }
  86. rt_inline rt_err_t rt_mtd_nand_check_block(struct rt_mtd_nand_device* device, rt_uint32_t block)
  87. {
  88. return device->ops->check_block(device, block);
  89. }
  90. rt_inline rt_err_t rt_mtd_nand_mark_badblock(struct rt_mtd_nand_device* device, rt_uint32_t block)
  91. {
  92. return device->ops->mark_badblock(device, block);
  93. }
  94. #endif /* MTD_NAND_H_ */