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