mtd_nand.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. struct rt_mtd_nand_device
  40. {
  41. struct rt_device parent;
  42. rt_uint16_t page_size; /* The Page size in the flash */
  43. rt_uint16_t oob_size; /* Out of bank size */
  44. rt_uint16_t oob_free; /* the free area in oob that flash driver not use */
  45. rt_uint16_t plane_num; /* the number of plane in the NAND Flash */
  46. rt_uint32_t pages_per_block; /* The number of page a block */
  47. rt_uint16_t block_total;
  48. rt_uint32_t block_start; /* The start of available block*/
  49. rt_uint32_t block_end; /* The end of available block */
  50. /* operations interface */
  51. const struct rt_mtd_nand_driver_ops* ops;
  52. };
  53. struct rt_mtd_nand_driver_ops
  54. {
  55. rt_err_t (*read_id) (struct rt_mtd_nand_device* device);
  56. rt_err_t (*read_page)(struct rt_mtd_nand_device* device,
  57. rt_off_t page,
  58. rt_uint8_t* data, rt_uint32_t data_len,
  59. rt_uint8_t * spare, rt_uint32_t spare_len);
  60. rt_err_t (*write_page)(struct rt_mtd_nand_device * device,
  61. rt_off_t page,
  62. const rt_uint8_t * data, rt_uint32_t data_len,
  63. const rt_uint8_t * spare, rt_uint32_t spare_len);
  64. rt_err_t (*move_page) (struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page);
  65. rt_err_t (*erase_block)(struct rt_mtd_nand_device* device, rt_uint32_t block);
  66. rt_err_t (*check_block)(struct rt_mtd_nand_device* device, rt_uint32_t block);
  67. rt_err_t (*mark_badblock)(struct rt_mtd_nand_device* device, rt_uint32_t block);
  68. };
  69. rt_err_t rt_mtd_nand_register_device(const char* name, struct rt_mtd_nand_device* device);
  70. rt_inline rt_uint32_t rt_mtd_nand_read_id(struct rt_mtd_nand_device* device)
  71. {
  72. return device->ops->read_id(device);
  73. }
  74. rt_inline rt_err_t rt_mtd_nand_read(
  75. struct rt_mtd_nand_device* device,
  76. rt_off_t page,
  77. rt_uint8_t* data, rt_uint32_t data_len,
  78. rt_uint8_t * spare, rt_uint32_t spare_len)
  79. {
  80. return device->ops->read_page(device, page, data, data_len, spare, spare_len);
  81. }
  82. rt_inline rt_err_t rt_mtd_nand_write(
  83. struct rt_mtd_nand_device* device,
  84. rt_off_t page,
  85. const rt_uint8_t* data, rt_uint32_t data_len,
  86. const rt_uint8_t * spare, rt_uint32_t spare_len)
  87. {
  88. return device->ops->write_page(device, page, data, data_len, spare, spare_len);
  89. }
  90. rt_inline rt_err_t rt_mtd_nand_move_page(struct rt_mtd_nand_device* device,
  91. rt_off_t src_page, rt_off_t dst_page)
  92. {
  93. return device->ops->move_page(device, src_page, dst_page);
  94. }
  95. rt_inline rt_err_t rt_mtd_nand_erase_block(struct rt_mtd_nand_device* device, rt_uint32_t block)
  96. {
  97. return device->ops->erase_block(device, block);
  98. }
  99. rt_inline rt_err_t rt_mtd_nand_check_block(struct rt_mtd_nand_device* device, rt_uint32_t block)
  100. {
  101. return device->ops->check_block(device, block);
  102. }
  103. rt_inline rt_err_t rt_mtd_nand_mark_badblock(struct rt_mtd_nand_device* device, rt_uint32_t block)
  104. {
  105. return device->ops->mark_badblock(device, block);
  106. }
  107. #endif /* MTD_NAND_H_ */