1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2011-12-05     Bernard      the first version
9  * 2011-04-02     prife        add mark_badblock and check_block
10  */
11 
12 /*
13  * COPYRIGHT (C) 2012, Shanghai Real Thread
14  */
15 
16 #ifndef __MTD_NAND_H__
17 #define __MTD_NAND_H__
18 
19 #include <rtthread.h>
20 
21 struct rt_mtd_nand_driver_ops;
22 #define RT_MTD_NAND_DEVICE(device)  ((struct rt_mtd_nand_device*)(device))
23 
24 #define RT_MTD_EOK          0     /* NO error */
25 #define RT_MTD_EECC         101   /* ECC error */
26 #define RT_MTD_EBUSY        102   /* hardware busy */
27 #define RT_MTD_EIO          103   /* generic IO issue */
28 #define RT_MTD_ENOMEM       104   /* out of memory */
29 #define RT_MTD_ESRC         105   /* source issue */
30 #define RT_MTD_EECC_CORRECT 106   /* ECC error but correct */
31 
32 struct rt_mtd_nand_device
33 {
34     struct rt_device parent;
35 
36     rt_uint16_t page_size;          /* The Page size in the flash */
37     rt_uint16_t oob_size;           /* Out of bank size */
38     rt_uint16_t oob_free;           /* the free area in oob that flash driver not use */
39     rt_uint16_t plane_num;          /* the number of plane in the NAND Flash */
40 
41     rt_uint32_t pages_per_block;    /* The number of page a block */
42     rt_uint16_t block_total;
43 
44     /* Only be touched by driver */
45     rt_uint32_t block_start;        /* The start of available block*/
46     rt_uint32_t block_end;          /* The end of available block */
47 
48     /* operations interface */
49     const struct rt_mtd_nand_driver_ops *ops;
50 
51     void *priv;
52 };
53 typedef struct rt_mtd_nand_device* rt_mtd_nand_t;
54 
55 struct rt_mtd_nand_driver_ops
56 {
57     rt_err_t (*read_id)(struct rt_mtd_nand_device *device);
58 
59     rt_err_t (*read_page)(struct rt_mtd_nand_device *device,
60                           rt_off_t page,
61                           rt_uint8_t *data, rt_uint32_t data_len,
62                           rt_uint8_t *spare, rt_uint32_t spare_len);
63 
64     rt_err_t (*write_page)(struct rt_mtd_nand_device *device,
65                            rt_off_t page,
66                            const rt_uint8_t *data, rt_uint32_t data_len,
67                            const rt_uint8_t *spare, rt_uint32_t spare_len);
68     rt_err_t (*move_page)(struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page);
69 
70     rt_err_t (*erase_block)(struct rt_mtd_nand_device *device, rt_uint32_t block);
71     rt_err_t (*check_block)(struct rt_mtd_nand_device *device, rt_uint32_t block);
72     rt_err_t (*mark_badblock)(struct rt_mtd_nand_device *device, rt_uint32_t block);
73 };
74 
75 rt_err_t rt_mtd_nand_register_device(const char *name, struct rt_mtd_nand_device *device);
76 rt_uint32_t rt_mtd_nand_read_id(struct rt_mtd_nand_device *device);
77 rt_err_t rt_mtd_nand_read(
78     struct rt_mtd_nand_device *device,
79     rt_off_t page,
80     rt_uint8_t *data, rt_uint32_t data_len,
81     rt_uint8_t *spare, rt_uint32_t spare_len);
82 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 rt_err_t rt_mtd_nand_move_page(struct rt_mtd_nand_device *device,
88         rt_off_t src_page, rt_off_t dst_page);
89 rt_err_t rt_mtd_nand_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block);
90 rt_err_t rt_mtd_nand_check_block(struct rt_mtd_nand_device *device, rt_uint32_t block);
91 rt_err_t rt_mtd_nand_mark_badblock(struct rt_mtd_nand_device *device, rt_uint32_t block);
92 
93 #endif /* MTD_NAND_H_ */
94