mtd_nand.h 4.6 KB

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