4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
32 * \brief Micron MT29F serial NAND driver
34 * \author Stefano Fedrigo <aleph@develer.com>
36 * $WIZ$ module_name = "mt29f"
37 * $WIZ$ module_depends = "timer", "kblock", "heap"
38 * $WIZ$ module_configuration = "bertos/cfg/cfg_mt29f.h"
44 #include "cfg/cfg_mt29f.h"
45 #include <cfg/macros.h>
46 #include <io/kblock.h>
49 // Define log settings for cfg/log.h
50 #define LOG_LEVEL CONFIG_MT29F_LOG_LEVEL
51 #define LOG_FORMAT CONFIG_MT29F_LOG_FORMAT
57 #define MT29F_ERR_ERASE BV(1) ///< Error erasing a block
58 #define MT29F_ERR_WRITE BV(2) ///< Error writing a page
59 #define MT29F_ERR_RD_TMOUT BV(3) ///< Read timeout
60 #define MT29F_ERR_WR_TMOUT BV(4) ///< Write timeout
61 #define MT29F_ERR_ECC BV(5) ///< Unrecoverable ECC error
64 #define MT29F_PAGE_SIZE (CONFIG_MT29F_DATA_SIZE + CONFIG_MT29F_SPARE_SIZE)
65 #define MT29F_BLOCK_SIZE (CONFIG_MT29F_DATA_SIZE * CONFIG_MT29F_PAGES_PER_BLOCK)
67 // Number of usable blocks, and index of first remapping block
68 #define MT29F_NUM_USER_BLOCKS (CONFIG_MT29F_NUM_BLOCK - CONFIG_MT29F_NUM_REMAP_BLOCKS)
72 #define MT29F_CMD_READ_1 0x00
73 #define MT29F_CMD_READ_2 0x30
74 #define MT29F_CMD_COPYBACK_READ_1 0x00
75 #define MT29F_CMD_COPYBACK_READ_2 0x35
76 #define MT29F_CMD_COPYBACK_PROGRAM_1 0x85
77 #define MT29F_CMD_COPYBACK_PROGRAM_2 0x10
78 #define MT29F_CMD_RANDOM_OUT 0x05
79 #define MT29F_CMD_RANDOM_OUT_2 0xE0
80 #define MT29F_CMD_RANDOM_IN 0x85
81 #define MT29F_CMD_READID 0x90
82 #define MT29F_CMD_WRITE_1 0x80
83 #define MT29F_CMD_WRITE_2 0x10
84 #define MT29F_CMD_ERASE_1 0x60
85 #define MT29F_CMD_ERASE_2 0xD0
86 #define MT29F_CMD_STATUS 0x70
87 #define MT29F_CMD_RESET 0xFF
90 // Get block from page
91 #define PAGE(blk) ((blk) * CONFIG_MT29F_PAGES_PER_BLOCK)
93 // Page from block and page in block
94 #define BLOCK(page) ((uint16_t)((page) / CONFIG_MT29F_PAGES_PER_BLOCK))
95 #define PAGE_IN_BLOCK(page) ((uint16_t)((page) % CONFIG_MT29F_PAGES_PER_BLOCK))
103 KBlock fd; // KBlock descriptor
105 uint8_t chip_select; // Chip select where NAND is connected
106 uint8_t status; // Status bitmap
108 uint16_t *block_map; // For bad blocks remapping
109 uint16_t remap_start; // First unused remap block
115 #define KBT_NAND MAKE_ID('N', 'A', 'N', 'D')
118 * Convert + ASSERT from generic KBlock to NAND context.
120 INLINE Mt29f *MT29F_CAST(KBlock *kb)
122 ASSERT(kb->priv.type == KBT_NAND);
129 bool mt29f_init(Mt29f *chip, struct Heap *heap, unsigned chip_select);
130 bool mt29f_initUnbuffered(Mt29f *chip, struct Heap *heap, unsigned chip_select);
132 // NAND specific functions
133 bool mt29f_getDevId(Mt29f *chip, uint8_t dev_id[5]);
134 int mt29f_blockErase(Mt29f *chip, uint16_t block);
135 void mt29f_format(Mt29f *chip);
138 void mt29f_ruinSomeBlocks(Mt29f *chip);
141 // Hardware specific functions, implemented by cpu specific module
142 bool mt29f_waitReadyBusy(Mt29f *chip, time_t timeout);
143 bool mt29f_waitTransferComplete(Mt29f *chip, time_t timeout);
144 void mt29f_sendCommand(Mt29f *chip, uint32_t cmd1, uint32_t cmd2,
145 int num_cycles, uint32_t cycle0, uint32_t cycle1234);
146 uint8_t mt29f_getChipStatus(Mt29f *chip);
147 void *mt29f_dataBuffer(Mt29f *chip);
148 bool mt29f_checkEcc(Mt29f *chip);
149 void mt29f_computeEcc(Mt29f *chip, const void *buf, size_t size, uint32_t *ecc, size_t ecc_size);
150 void mt29f_hwInit(Mt29f *chip);
152 #endif /* DRV_MT29F_H */