7d529f1ccc0a918694f687dd55d3de48520fafec
[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 #ifndef DRV_NAND_H
42 #define DRV_NAND_H
43
44 #include "cfg/cfg_nand.h"
45 #include <io/kblock.h>
46
47
48 // Define log settings for cfg/log.h
49 #define LOG_LEVEL    CONFIG_NAND_LOG_LEVEL
50 #define LOG_FORMAT   CONFIG_NAND_LOG_FORMAT
51
52 /**
53  * \name Error codes.
54  * \{
55  */
56 #define NAND_ERR_ERASE     BV(1)   ///< Error erasing a block
57 #define NAND_ERR_WRITE     BV(2)   ///< Error writing a page
58 #define NAND_ERR_RD_TMOUT  BV(3)   ///< Read timeout
59 #define NAND_ERR_WR_TMOUT  BV(4)   ///< Write timeout
60 #define NAND_ERR_ECC       BV(5)   ///< Unrecoverable ECC error
61 /** \} */
62
63
64 // NAND commands
65 #define NAND_CMD_READ_1               0x00
66 #define NAND_CMD_READ_2               0x30
67 #define NAND_CMD_COPYBACK_READ_1      0x00
68 #define NAND_CMD_COPYBACK_READ_2      0x35
69 #define NAND_CMD_COPYBACK_PROGRAM_1   0x85
70 #define NAND_CMD_COPYBACK_PROGRAM_2   0x10
71 #define NAND_CMD_RANDOM_OUT           0x05
72 #define NAND_CMD_RANDOM_OUT_2         0xE0
73 #define NAND_CMD_RANDOM_IN            0x85
74 #define NAND_CMD_READID               0x90
75 #define NAND_CMD_WRITE_1              0x80
76 #define NAND_CMD_WRITE_2              0x10
77 #define NAND_CMD_ERASE_1              0x60
78 #define NAND_CMD_ERASE_2              0xD0
79 #define NAND_CMD_STATUS               0x70
80 #define NAND_CMD_RESET                0xFF
81
82
83 /**
84  * NAND context.
85  */
86 typedef struct Nand
87 {
88         KBlock    fd;           // KBlock descriptor
89
90         uint8_t   chip_select;  // Chip select where NAND is connected
91         uint8_t   status;       // Status bitmap
92
93         uint16_t *block_map;    // For bad blocks remapping
94         uint16_t  remap_start;  // First unused remap block
95 } Nand;
96
97 /*
98  * Kblock id.
99  */
100 #define KBT_NAND  MAKE_ID('N', 'A', 'N', 'D')
101
102 /**
103 * Convert + ASSERT from generic KBlock to NAND context.
104 */
105 INLINE Nand *NAND_CAST(KBlock *kb)
106 {
107         ASSERT(kb->priv.type == KBT_NAND);
108         return (Nand *)kb;
109 }
110
111 struct Heap;
112
113 // Kblock interface
114 bool nand_init(Nand *chip, struct Heap *heap, unsigned chip_select);
115 bool nand_initUnbuffered(Nand *chip, struct Heap *heap, unsigned chip_select);
116
117 // NAND specific functions
118 bool nand_getDevId(Nand *chip, uint8_t dev_id[5]);
119 int nand_blockErase(Nand *chip, uint16_t block);
120 void nand_format(Nand *chip);
121
122 #ifdef _DEBUG
123 void nand_ruinSomeBlocks(Nand *chip);
124 #endif
125
126 // Hardware specific functions, implemented by cpu specific module
127 bool nand_waitReadyBusy(Nand *chip, time_t timeout);
128 bool nand_waitTransferComplete(Nand *chip, time_t timeout);
129 void nand_sendCommand(Nand *chip, uint32_t cmd1, uint32_t cmd2,
130                 int num_cycles, uint32_t cycle0, uint32_t cycle1234);
131 uint8_t nand_getChipStatus(Nand *chip);
132 void *nand_dataBuffer(Nand *chip);
133 bool nand_checkEcc(Nand *chip);
134 void nand_computeEcc(Nand *chip, const void *buf, size_t size, uint32_t *ecc, size_t ecc_size);
135 void nand_hwInit(Nand *chip);
136
137 #endif /* DRV_NAND_H */