mtd.h 3.0 KB

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