1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 #include <targetos.h>
12 #include <kernel.h>
13 #include <fsdriver.h>
14 
15 /***********************************************************************/
16 /* Symbol Definitions                                                  */
17 /***********************************************************************/
18 #define NDM_PART_NAME_LEN 15            // partition name size in bytes
19 #define NDM_PART_USER 0                 // number of ui32s in partition for user
20 #define NDM_PART_NAME NDM_PART_NAME_LEN // obsolete symbol
21 
22 // Various NAND device types
23 #define NDM_SLC (1 << 0)
24 #define NDM_MLC (1 << 1)
25 #define NDM_WR1 (1 << 2)
26 
27 // Various function return types
28 #define NDM_INIT_BAD_BLOCK 1
29 #define NDM_CTRL_BLOCK 2
30 #define NDM_REG_BLOCK 3
31 
32 // Various states for a page - used by data_and_spare_check()
33 #define NDM_PAGE_ERASED 0
34 #define NDM_PAGE_VALID 1
35 #define NDM_PAGE_INVALID 2
36 
37 // write_data_and_spare action parameter values
38 #define NDM_NONE 0
39 #define NDM_ECC 1
40 #define NDM_ECC_VAL 2
41 
42 /***********************************************************************/
43 /* Type Declarations                                                   */
44 /***********************************************************************/
45 // NDM Partition Information
46 typedef struct {
47     ui32 first_block; // first virtual block for partition
48     ui32 num_blocks;  // number of virtual blocks in partition
49 #if NDM_PART_USER
50     ui32 user[NDM_PART_USER]; // reserved for the user
51 #endif
52     char name[NDM_PART_NAME_LEN]; // partition name
53     ui8 type;                     // partition type - same as vstat()
54 } NDMPartition;
55 
56 // Driver count statistics for TargetNDM devices
57 typedef struct {
58     ui32 write_page;      // number of write_data_and_spare() calls
59     ui32 write_pages;     // number of write_pages() calls
60     ui32 read_page;       // number of read_decode_data() calls
61     ui32 read_pages;      // number of read_pages() calls
62     ui32 xfr_page;        // number of transfer_page() calls
63     ui32 read_dec_spare;  // number of read_decode_spare() calls
64     ui32 read_spare;      // number of read_spare() calls
65     ui32 page_erased;     // number of data_and_spare_erased() calls
66     ui32 check_page;      // number of data_and_spare_check() calls
67     ui32 erase_block;     // number of erase_block() calls
68     ui32 is_block_bad;    // number of is_block_bad() calls
69 } NdmDvrStats;
70 
71 // Driver Interface Structure
72 typedef struct {
73     ui32 num_blocks;     // total number of blocks on device
74     ui32 max_bad_blocks; // maximum number of bad blocks
75     ui32 block_size;     // block size in bytes
76     ui32 page_size;      // page data area in bytes
77     ui32 eb_size;        // used spare area in bytes
78     ui32 flags;          // option flags
79     ui32 type;           // type of device
80     void* dev;           // optional value set by driver
81 
82     // Driver Functions
83     int (*write_data_and_spare)(ui32 pn, const ui8* data, ui8* spare, int action, void* dev);
84     int (*write_pages)(ui32 pn, ui32 count, const ui8* data, ui8* spare, int action, void* dev);
85     int (*read_decode_data)(ui32 pn, ui8* data, ui8* spare, void* dev);
86     int (*read_pages)(ui32 pn, ui32 count, ui8* data, ui8* spare, void* dev);
87     int (*transfer_page)(ui32 old_pn, ui32 new_pn, ui8* data, ui8* old_spare, ui8* new_spare,
88                          int encode_spare, void* dev);
89 #if INC_FFS_NDM_MLC || INC_FTL_NDM_MLC
90     ui32 (*pair_offset)(ui32 page_offset, void* dev);
91 #endif
92     int (*read_decode_spare)(ui32 pn, ui8* spare, void* dev);
93     int (*read_spare)(ui32 pn, ui8* spare, void* dev);
94     int (*data_and_spare_erased)(ui32 pn, ui8* data, ui8* spare, void* dev);
95     int (*data_and_spare_check)(ui32 pn, ui8* data, ui8* spare, int* status, void* dev);
96     int (*erase_block)(ui32 pn, void* dev);
97     int (*is_block_bad)(ui32 pn, void* dev);
98 #if FS_DVR_TEST
99     ui32 dev_eb_size; /* device spare area size */
100     void (*chip_show)(void* vol);
101     int (*rd_raw_spare)(ui32 p, ui8* spare, void* dev);
102     int (*rd_raw_page)(ui32 p, void* data, void* dev);
103 #endif
104 } NDMDrvr;
105 
106 // NDM Control Block
107 typedef struct ndm* NDM;
108 typedef const struct ndm* CNDM;
109 
110 /***********************************************************************/
111 /* Functions Prototypes                                                */
112 /***********************************************************************/
113 // General API
114 NDM ndmAddDev(const NDMDrvr* drvr);
115 int ndmDelDev(NDM ndm);
116 ui32 ndmGetNumVBlocks(CNDM ndm);
117 int ndmUnformat(NDM ndm);
118 ui32 ndmPastPrevPair(CNDM ndm, ui32 pn);
119 
120 // Partitions API
121 ui32 ndmGetNumPartitions(CNDM ndm);
122 int ndmSetNumPartitions(NDM ndm, ui32 num_partitions);
123 int ndmReadPartition(CNDM ndm, NDMPartition* part, ui32 part_num);
124 const NDMPartition* ndmGetPartition(CNDM ndm, ui32 part_num);
125 int ndmWritePartition(NDM ndm, const NDMPartition* part, ui32 part_num, const char* name);
126 int ndmErasePartition(NDM ndm, ui32 part_num);
127 void ndmDeletePartition(CNDM ndm, ui32 part_num);
128 void ndmDeletePartitionTable(NDM ndm);
129 int ndmSavePartitionTable(NDM ndm);
130 int ndmDelVols(CNDM ndm);
131 int ndmDelVol(CNDM ndm, ui32 part_num);
132 int ndmWrFatPartition(NDM ndm, ui32 part_num);
133 
134 // User Volume API
135 int ndmEraseBlock(ui32 pn, void* ndm_ptr);
136 int ndmReadPages(ui32 start_pn, ui32 count, void* data, void* spare, void* ndm_ptr);
137 int ndmWritePages(ui32 start_pn, ui32 count, const void* data, void* spare, void* ndm_ptr);
138 
139 // FAT/XFS/FFS Volume API
140 int ndmAddVolFatFTL(NDM ndm, ui32 part_no, FtlNdmVol* ftl, FatVol* fat);
141 int ndmAddVolXfsFTL(NDM ndm, ui32 part_no, FtlNdmVol* ftl, XfsVol* xfs);
142 int ndmAddVolFFS(NDM ndm, ui32 part_num, FfsVol* ffs_dvr);
143 
144 // Driver Test/Special Routines
145 int ndmExtractBBL(NDM ndm);
146 int ndmInsertBBL(NDM ndm);
147 int NdmDvrTestAdd(const NDMDrvr* dev);
148 
149 #ifdef __cplusplus
150 }
151 #endif
152