5514c9e53088b75e0d6708bc05b4b453a1016360
[bertos.git] / bertos / drv / nand.h
1 /**
2 * \file
3 * <!--
4 * This file is part of BeRTOS.
5 *
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 *
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction.  Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License.  This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
28 *
29 * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30 * -->
31 *
32 * \brief NAND driver
33 *
34 * \author Stefano Fedrigo <aleph@develer.com>
35 *
36 * $WIZ$ module_name = "nand"
37 * $WIZ$ module_depends = "timer", "kblock", "heap"
38 * $WIZ$ module_configuration = "bertos/cfg/cfg_nand.h"
39 */
40
41 #ifndef DRV_NAND_H
42 #define DRV_NAND_H
43
44 #include "cfg/cfg_nand.h"
45 #include <cfg/macros.h>
46 #include <io/kblock.h>
47
48
49 // Define log settings for cfg/log.h
50 #define LOG_LEVEL    CONFIG_NAND_LOG_LEVEL
51 #define LOG_FORMAT   CONFIG_NAND_LOG_FORMAT
52
53 /**
54  * \name Error codes.
55  * \{
56  */
57 #define NAND_ERR_ERASE     BV(1)   ///< Error erasing a block
58 #define NAND_ERR_WRITE     BV(2)   ///< Error writing a page
59 #define NAND_ERR_RD_TMOUT  BV(3)   ///< Read timeout
60 #define NAND_ERR_WR_TMOUT  BV(4)   ///< Write timeout
61 #define NAND_ERR_ECC       BV(5)   ///< Unrecoverable ECC error
62 /** \} */
63
64 #define NAND_PAGE_SIZE         (CONFIG_NAND_DATA_SIZE + CONFIG_NAND_SPARE_SIZE)
65 #define NAND_BLOCK_SIZE        (CONFIG_NAND_DATA_SIZE * CONFIG_NAND_PAGES_PER_BLOCK)
66
67 // Number of usable blocks, and index of first remapping block
68 #define NAND_NUM_USER_BLOCKS   (CONFIG_NAND_NUM_BLOCK - CONFIG_NAND_NUM_REMAP_BLOCKS)
69
70
71 // NAND commands
72 #define NAND_CMD_READ_1               0x00
73 #define NAND_CMD_READ_2               0x30
74 #define NAND_CMD_COPYBACK_READ_1      0x00
75 #define NAND_CMD_COPYBACK_READ_2      0x35
76 #define NAND_CMD_COPYBACK_PROGRAM_1   0x85
77 #define NAND_CMD_COPYBACK_PROGRAM_2   0x10
78 #define NAND_CMD_RANDOM_OUT           0x05
79 #define NAND_CMD_RANDOM_OUT_2         0xE0
80 #define NAND_CMD_RANDOM_IN            0x85
81 #define NAND_CMD_READID               0x90
82 #define NAND_CMD_WRITE_1              0x80
83 #define NAND_CMD_WRITE_2              0x10
84 #define NAND_CMD_ERASE_1              0x60
85 #define NAND_CMD_ERASE_2              0xD0
86 #define NAND_CMD_STATUS               0x70
87 #define NAND_CMD_RESET                0xFF
88
89
90 // Get block from page
91 #define PAGE(blk)            ((blk) * CONFIG_NAND_PAGES_PER_BLOCK)
92
93 // Page from block and page in block
94 #define BLOCK(page)          ((uint16_t)((page) / CONFIG_NAND_PAGES_PER_BLOCK))
95 #define PAGE_IN_BLOCK(page)  ((uint16_t)((page) % CONFIG_NAND_PAGES_PER_BLOCK))
96
97
98 /**
99  * NAND context.
100  */
101 typedef struct Nand
102 {
103         KBlock    fd;           // KBlock descriptor
104
105         uint8_t   chip_select;  // Chip select where NAND is connected
106         uint8_t   status;       // Status bitmap
107
108         uint16_t *block_map;    // For bad blocks remapping
109         uint16_t  remap_start;  // First unused remap block
110 } Nand;
111
112 /*
113  * Kblock id.
114  */
115 #define KBT_NAND  MAKE_ID('N', 'A', 'N', 'D')
116
117 /**
118 * Convert + ASSERT from generic KBlock to NAND context.
119 */
120 INLINE Nand *NAND_CAST(KBlock *kb)
121 {
122         ASSERT(kb->priv.type == KBT_NAND);
123         return (Nand *)kb;
124 }
125
126 struct Heap;
127
128 // Kblock interface
129 bool nand_init(Nand *chip, struct Heap *heap, unsigned chip_select);
130 bool nand_initUnbuffered(Nand *chip, struct Heap *heap, unsigned chip_select);
131
132 // NAND specific functions
133 bool nand_getDevId(Nand *chip, uint8_t dev_id[5]);
134 int nand_blockErase(Nand *chip, uint16_t block);
135 void nand_format(Nand *chip);
136
137 #ifdef _DEBUG
138 void nand_ruinSomeBlocks(Nand *chip);
139 #endif
140
141 // Hardware specific functions, implemented by cpu specific module
142 bool nand_waitReadyBusy(Nand *chip, time_t timeout);
143 bool nand_waitTransferComplete(Nand *chip, time_t timeout);
144 void nand_sendCommand(Nand *chip, uint32_t cmd1, uint32_t cmd2,
145                 int num_cycles, uint32_t cycle0, uint32_t cycle1234);
146 uint8_t nand_getChipStatus(Nand *chip);
147 void *nand_dataBuffer(Nand *chip);
148 bool nand_checkEcc(Nand *chip);
149 void nand_computeEcc(Nand *chip, const void *buf, size_t size, uint32_t *ecc, size_t ecc_size);
150 void nand_hwInit(Nand *chip);
151
152 #endif /* DRV_NAND_H */