nand.h

Go to the documentation of this file.
00001 
00042 #ifndef DRV_NAND_H
00043 #define DRV_NAND_H
00044 
00045 #include "cfg/cfg_nand.h"
00046 #include <io/kblock.h>
00047 
00048 
00049 // Define log settings for cfg/log.h
00050 #define LOG_LEVEL    CONFIG_NAND_LOG_LEVEL
00051 #define LOG_FORMAT   CONFIG_NAND_LOG_FORMAT
00052 
00057 #define NAND_ERR_ERASE     BV(1)   ///< Error erasing a block
00058 #define NAND_ERR_WRITE     BV(2)   ///< Error writing a page
00059 #define NAND_ERR_RD_TMOUT  BV(3)   ///< Read timeout
00060 #define NAND_ERR_WR_TMOUT  BV(4)   ///< Write timeout
00061 #define NAND_ERR_ECC       BV(5)   ///< Unrecoverable ECC error
00062 
00065 // NAND commands
00066 #define NAND_CMD_READ_1               0x00
00067 #define NAND_CMD_READ_2               0x30
00068 #define NAND_CMD_COPYBACK_READ_1      0x00
00069 #define NAND_CMD_COPYBACK_READ_2      0x35
00070 #define NAND_CMD_COPYBACK_PROGRAM_1   0x85
00071 #define NAND_CMD_COPYBACK_PROGRAM_2   0x10
00072 #define NAND_CMD_RANDOM_OUT           0x05
00073 #define NAND_CMD_RANDOM_OUT_2         0xE0
00074 #define NAND_CMD_RANDOM_IN            0x85
00075 #define NAND_CMD_READID               0x90
00076 #define NAND_CMD_WRITE_1              0x80
00077 #define NAND_CMD_WRITE_2              0x10
00078 #define NAND_CMD_ERASE_1              0x60
00079 #define NAND_CMD_ERASE_2              0xD0
00080 #define NAND_CMD_STATUS               0x70
00081 #define NAND_CMD_RESET                0xFF
00082 
00083 
00087 typedef struct Nand
00088 {
00089     KBlock    fd;           // KBlock descriptor
00090 
00091     uint8_t   chip_select;  // Chip select where NAND is connected
00092     uint8_t   status;       // Status bitmap
00093 
00094     uint16_t *block_map;    // For bad blocks remapping
00095     uint16_t  remap_start;  // First unused remap block
00096 } Nand;
00097 
00098 /*
00099  * Kblock id.
00100  */
00101 #define KBT_NAND  MAKE_ID('N', 'A', 'N', 'D')
00102 
00106 INLINE Nand *NAND_CAST(KBlock *kb)
00107 {
00108     ASSERT(kb->priv.type == KBT_NAND);
00109     return (Nand *)kb;
00110 }
00111 
00112 struct Heap;
00113 
00114 // Kblock interface
00115 bool nand_init(Nand *chip, struct Heap *heap, unsigned chip_select);
00116 bool nand_initUnbuffered(Nand *chip, struct Heap *heap, unsigned chip_select);
00117 
00118 // NAND specific functions
00119 bool nand_getDevId(Nand *chip, uint8_t dev_id[5]);
00120 int nand_blockErase(Nand *chip, uint16_t block);
00121 void nand_format(Nand *chip);
00122 
00123 #ifdef _DEBUG
00124 void nand_ruinSomeBlocks(Nand *chip);
00125 #endif
00126 
00127 // Hardware specific functions, implemented by cpu specific module
00128 bool nand_waitReadyBusy(Nand *chip, time_t timeout);
00129 bool nand_waitTransferComplete(Nand *chip, time_t timeout);
00130 void nand_sendCommand(Nand *chip, uint32_t cmd1, uint32_t cmd2,
00131         int num_cycles, uint32_t cycle0, uint32_t cycle1234);
00132 uint8_t nand_getChipStatus(Nand *chip);
00133 void *nand_dataBuffer(Nand *chip);
00134 bool nand_checkEcc(Nand *chip);
00135 void nand_computeEcc(Nand *chip, const void *buf, size_t size, uint32_t *ecc, size_t ecc_size);
00136 void nand_hwInit(Nand *chip);
00137 
00138 #endif /* DRV_NAND_H */