mtd.h 3.0 KB

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