mtd.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * File : mtd.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, Shanghai Real-Thread Technology Co., Ltd
  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-5 heyuanjie the first version
  23. */
  24. #ifndef __MTD_H__
  25. #define __MTD_H__
  26. #include <rtthread.h>
  27. #include <stddef.h>
  28. #include <stdint.h>
  29. #define MTD_TYPE_NOR 1
  30. #define MTD_TYPE_NAND 2
  31. /**
  32. * MTD operation modes
  33. *
  34. * @MTD_OPM_PLACE_OOB: OOB data are placed at the given offset (default)
  35. * @MTD_OPM_AUTO_OOB: OOB data are automatically placed at the free areas
  36. * @MTD_OPM_RAW: data are transferred as-is, with no error correction;
  37. */
  38. enum mtd_opm
  39. {
  40. MTD_OPM_PLACE_OOB = 0,
  41. MTD_OPM_AUTO_OOB = 1,
  42. MTD_OPM_RAW = 2,
  43. };
  44. #ifndef loff_t
  45. typedef long loff_t;
  46. #endif
  47. struct mtd_oob_region
  48. {
  49. uint8_t offset;
  50. uint8_t length;
  51. };
  52. typedef struct mtd_info
  53. {
  54. struct rt_device parent;
  55. const struct mtd_ops *ops;
  56. uint16_t oob_size;
  57. uint16_t sector_size; /* Minimal writable flash unit size */
  58. uint32_t block_size:28; /* Erase size for the device */
  59. uint32_t type:4;
  60. size_t size; /* Total size of the MTD */
  61. loff_t offset; /* At which this MTD starts, from the beginning of the MEMORY */
  62. struct mtd_info *master;
  63. void *priv;
  64. }rt_mtd_t;
  65. struct mtd_io_desc
  66. {
  67. uint8_t mode; /* operation mode(enum mtd_opm) */
  68. uint8_t ooblen; /* number of oob bytes to write/read */
  69. uint8_t oobretlen; /* number of oob bytes written/read */
  70. uint8_t ooboffs; /* offset in the oob area */
  71. uint8_t *oobbuf;
  72. size_t datlen; /* number of data bytes to write/read */
  73. size_t datretlen; /* number of data bytes written/read */
  74. uint8_t *datbuf; /* if NULL only oob are read/written */
  75. };
  76. struct mtd_ops
  77. {
  78. int(*erase)(rt_mtd_t *mtd, loff_t addr, size_t len); /* return 0 if success */
  79. int(*read) (rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *ops); /* return 0 if success */
  80. int(*write) (rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *ops); /* return 0 if success */
  81. int(*isbad) (rt_mtd_t *mtd, uint32_t block); /* return 1 if bad, 0 not bad */
  82. int(*markbad) (rt_mtd_t *mtd, uint32_t block); /* return 0 if success */
  83. };
  84. struct mtd_part
  85. {
  86. const char *name; /* name of the MTD partion */
  87. loff_t offset; /* start addr of partion */
  88. size_t size; /* size of partion */
  89. };
  90. int rt_mtd_erase(rt_mtd_t *mtd, loff_t addr, size_t size);
  91. int rt_mtd_block_erase(rt_mtd_t *mtd, uint32_t block);
  92. int rt_mtd_read_oob(rt_mtd_t *mtd, loff_t from, struct mtd_io_desc *desc);
  93. int rt_mtd_write_oob(rt_mtd_t *mtd, loff_t to, struct mtd_io_desc *desc);
  94. int rt_mtd_read(rt_mtd_t *mtd, loff_t from, uint8_t *buf, size_t len);
  95. int rt_mtd_write(rt_mtd_t *mtd, loff_t to, const uint8_t *buf, size_t len);
  96. int rt_mtd_block_markbad(rt_mtd_t *mtd, uint32_t block);
  97. int rt_mtd_block_isbad(rt_mtd_t *mtd, uint32_t block);
  98. rt_mtd_t* rt_mtd_get(const char *name);
  99. int rt_mtd_register(rt_mtd_t *master, const struct mtd_part *parts, int np);
  100. #endif