Update preset.
[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 ONFI 1.0 compliant NAND kblock 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
42 #ifndef DRV_NAND_H
43 #define DRV_NAND_H
44
45 #include "cfg/cfg_nand.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
65 // NAND commands
66 #define NAND_CMD_READ_1               0x00
67 #define NAND_CMD_READ_2               0x30
68 #define NAND_CMD_COPYBACK_READ_1      0x00
69 #define NAND_CMD_COPYBACK_READ_2      0x35
70 #define NAND_CMD_COPYBACK_PROGRAM_1   0x85
71 #define NAND_CMD_COPYBACK_PROGRAM_2   0x10
72 #define NAND_CMD_RANDOM_OUT           0x05
73 #define NAND_CMD_RANDOM_OUT_2         0xE0
74 #define NAND_CMD_RANDOM_IN            0x85
75 #define NAND_CMD_READID               0x90
76 #define NAND_CMD_WRITE_1              0x80
77 #define NAND_CMD_WRITE_2              0x10
78 #define NAND_CMD_ERASE_1              0x60
79 #define NAND_CMD_ERASE_2              0xD0
80 #define NAND_CMD_STATUS               0x70
81 #define NAND_CMD_RESET                0xFF
82
83
84 /**
85  * NAND context.
86  */
87 typedef struct Nand
88 {
89         KBlock    fd;           // KBlock descriptor
90
91         uint8_t   chip_select;  // Chip select where NAND is connected
92         uint8_t   status;       // Status bitmap
93
94         uint16_t *block_map;    // For bad blocks remapping
95         uint16_t  remap_start;  // First unused remap block
96 } Nand;
97
98 /*
99  * Kblock id.
100  */
101 #define KBT_NAND  MAKE_ID('N', 'A', 'N', 'D')
102
103 /**
104 * Convert + ASSERT from generic KBlock to NAND context.
105 */
106 INLINE Nand *NAND_CAST(KBlock *kb)
107 {
108         ASSERT(kb->priv.type == KBT_NAND);
109         return (Nand *)kb;
110 }
111
112 struct Heap;
113
114 // Kblock interface
115 bool nand_init(Nand *chip, struct Heap *heap, unsigned chip_select);
116 bool nand_initUnbuffered(Nand *chip, struct Heap *heap, unsigned chip_select);
117
118 // NAND specific functions
119 bool nand_getDevId(Nand *chip, uint8_t dev_id[5]);
120 int nand_blockErase(Nand *chip, uint16_t block);
121 void nand_format(Nand *chip);
122
123 #ifdef _DEBUG
124 void nand_ruinSomeBlocks(Nand *chip);
125 #endif
126
127 // Hardware specific functions, implemented by cpu specific module
128 bool nand_waitReadyBusy(Nand *chip, time_t timeout);
129 bool nand_waitTransferComplete(Nand *chip, time_t timeout);
130 void nand_sendCommand(Nand *chip, uint32_t cmd1, uint32_t cmd2,
131                 int num_cycles, uint32_t cycle0, uint32_t cycle1234);
132 uint8_t nand_getChipStatus(Nand *chip);
133 void *nand_dataBuffer(Nand *chip);
134 bool nand_checkEcc(Nand *chip);
135 void nand_computeEcc(Nand *chip, const void *buf, size_t size, uint32_t *ecc, size_t ecc_size);
136 void nand_hwInit(Nand *chip);
137
138 #endif /* DRV_NAND_H */