MT29F driver: first implementation of read/write functions.
[bertos.git] / bertos / cpu / cortex-m3 / drv / mt29f_sam3.c
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  *
33  * \brief Micron MT29F serial NAND driver for SAM3's static memory controller.
34  *
35  * \author Stefano Fedrigo <aleph@develer.com>
36  */
37
38 #include "mt29f_sam3.h"
39 #include "cfg/cfg_mt29f.h"
40
41 // Define log settings for cfg/log.h
42 #define LOG_LEVEL    CONFIG_MT29F_LOG_LEVEL
43 #define LOG_FORMAT   CONFIG_MT29F_LOG_FORMAT
44
45 #include <cfg/log.h>
46 #include <cfg/macros.h>
47
48 #include <io/sam3.h>
49 #include <io/kblock.h>
50
51 #include <drv/timer.h>
52 #include <drv/mt29f.h>
53
54 #include <cpu/power.h> /* cpu_relax() */
55 #include <cpu/types.h>
56
57 #include <string.h> /* memcpy() */
58
59
60 // NAND flash status codes
61 #define MT29F_STATUS_READY             BV(6)
62 #define MT29F_STATUS_ERROR             BV(0)
63
64 // NAND flash commands
65 #define MT29F_CMD_READ_1               0x00
66 #define MT29F_CMD_READ_2               0x30
67 #define MT29F_CMD_COPYBACK_READ_1      0x00
68 #define MT29F_CMD_COPYBACK_READ_2      0x35
69 #define MT29F_CMD_COPYBACK_PROGRAM_1   0x85
70 #define MT29F_CMD_COPYBACK_PROGRAM_2   0x10
71 #define MT29F_CMD_RANDOM_OUT           0x05
72 #define MT29F_CMD_RANDOM_OUT_2         0xE0
73 #define MT29F_CMD_RANDOM_IN            0x85
74 #define MT29F_CMD_READID               0x90
75 #define MT29F_CMD_WRITE_1              0x80
76 #define MT29F_CMD_WRITE_2              0x10
77 #define MT29F_CMD_ERASE_1              0x60
78 #define MT29F_CMD_ERASE_2              0xD0
79 #define MT29F_CMD_STATUS               0x70
80 #define MT29F_CMD_RESET                0xFF
81
82
83 struct Mt29fHardware
84 {
85         uint8_t status;
86 };
87
88
89 /*
90  * Translate flash page index plus a byte offset
91  * in the five address cycles format needed by NAND.
92  *
93  * Cycles in x8 mode as the MT29F2G08AAD
94  * CA = column addr, PA = page addr, BA = block addr
95  *
96  * Cycle    I/O7  I/O6  I/O5  I/O4  I/O3  I/O2  I/O1  I/O0
97  * -------------------------------------------------------
98  * First    CA7   CA6   CA5   CA4   CA3   CA2   CA1   CA0
99  * Second   LOW   LOW   LOW   LOW   CA11  CA10  CA9   CA8
100  * Third    BA7   BA6   PA5   PA4   PA3   PA2   PA1   PA0
101  * Fourth   BA15  BA14  BA13  BA12  BA11  BA10  BA9   BA8
102  * Fifth    LOW   LOW   LOW   LOW   LOW   LOW   LOW   BA16
103  */
104 static void mt29f_getAddrCycles(block_idx_t page, size_t offset, uint32_t *cycle0, uint32_t *cycle1234)
105 {
106         uint32_t addr = (page * MT29F_PAGE_SIZE) + offset;
107
108         /*
109          * offset nibbles  77776666 55554444 33332222 11110000
110          * cycle1234       -------7 66665555 ----4444 33332222
111          * cycle0          11110000
112          */
113         *cycle0 = addr & 0xff;
114         *cycle1234 = ((addr >> 8) & 0x00000fff) | ((addr >> 4) & 0x01ff0000);
115 }
116
117
118 INLINE bool mt29f_isBusy(void)
119 {
120         return HWREG(NFC_CMD_BASE_ADDR + NFC_CMD_NFCCMD) & 0x8000000;
121 }
122
123 INLINE bool mt29f_isCmdDone(void)
124 {
125     return SMC_SR & SMC_SR_CMDDONE;
126 }
127
128 INLINE bool mt29f_isReadyBusy(void)
129 {
130     return SMC_SR & SMC_SR_RB_EDGE0;
131 }
132
133 INLINE bool mt29f_isTransferComplete(void)
134 {
135         return SMC_SR & SMC_SR_XFRDONE;
136 }
137
138
139 /*
140  * Send command to NAND and wait for completion.
141  */
142 static void mt29f_sendCommand(uint32_t cmd, uint32_t cycle0, uint32_t cycle1234)
143 {
144         reg32_t *cmd_addr;
145
146         while (mt29f_isBusy());
147
148         SMC_ADDR = cycle0;
149
150         cmd_addr = (reg32_t *)(NFC_CMD_BASE_ADDR + cmd);
151         *cmd_addr = cycle1234;
152
153         while (!mt29f_isCmdDone());
154 }
155
156
157 static bool mt29f_isOperationComplete(void)
158 {
159         uint8_t status;
160
161         mt29f_sendCommand(
162                 NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
163                 MT29F_CMD_STATUS << 2, 0, 0);
164
165         status = (uint8_t)HWREG(MT29F_DATA_ADDR);
166         return (status & MT29F_STATUS_READY) && !(status & MT29F_STATUS_ERROR);
167 }
168
169
170 #if 0 //reset
171         mt29f_sendCommand(
172                         NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
173                         MT29F_CMD_RESET << 2,
174                         0,                                     /* Dummy address cylce 1,2,3,4.*/
175                         0                                      /* Dummy address cylce 0.*/
176 #endif
177
178 /**
179  * Erase block at given offset.
180  */
181 int mt29f_blockErase(Mt29f *fls, block_idx_t page)
182 {
183         uint32_t cycle0;
184         uint32_t cycle1234;
185
186         mt29f_getAddrCycles(page, 0, &cycle0, &cycle1234);
187
188         mt29f_sendCommand(
189                 NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_THREE | NFC_CMD_VCMD2 |
190                 (MT29F_CMD_ERASE_2 << 10) | (MT29F_CMD_ERASE_1 << 2),
191                 0, cycle1234);
192
193         while (!mt29f_isReadyBusy());
194
195         if (!mt29f_isOperationComplete())
196         {
197                 LOG_ERR("mt29f: error erasing block\n");
198                 fls->hw->status |= MT29F_ERR_ERASE;
199                 return -1;
200         }
201
202         return 0;
203 }
204
205
206 static size_t mt29f_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
207 {
208         uint32_t cycle0;
209         uint32_t cycle1234;
210
211         ASSERT(offset == 0);
212         ASSERT(size == blk->blk_size);
213
214         mt29f_getAddrCycles(idx, 0, &cycle0, &cycle1234);
215
216         mt29f_sendCommand(
217                 NFC_CMD_NFCCMD | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_FIVE | NFC_CMD_VCMD2 |
218                 (MT29F_CMD_READ_2 << 10) | (MT29F_CMD_READ_1 << 2),
219                 cycle0, cycle1234);
220
221         while (!mt29f_isReadyBusy());
222         while (!mt29f_isTransferComplete());
223
224         if (!kblock_buffered(blk))
225         {
226                 // Make sure user is not buffering just in NFC controller SRAM
227                 ASSERT((buf < (void *)NFC_CMD_BASE_ADDR) || (buf > (void *)(NFC_CMD_BASE_ADDR + MT29F_PAGE_SIZE)));
228                 memcpy(buf, (void *)NFC_CMD_BASE_ADDR, size);
229         }
230
231         return size;
232 }
233
234
235 static size_t mt29f_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
236 {
237         Mt29f *fls = FLASH_CAST(blk);
238         uint32_t cycle0;
239         uint32_t cycle1234;
240
241         ASSERT(offset == 0);
242         ASSERT(size == blk->blk_size);
243
244         if (!kblock_buffered(blk))
245         {
246                 // Make sure user is not buffering just in NFC controller SRAM
247                 ASSERT((_buf < (void *)NFC_CMD_BASE_ADDR) || (_buf > (void *)(NFC_CMD_BASE_ADDR + MT29F_PAGE_SIZE)));
248                 memcpy((void *)NFC_CMD_BASE_ADDR, _buf, size);
249         }
250
251         mt29f_getAddrCycles(idx, 0, &cycle0, &cycle1234);
252
253         mt29f_sendCommand(
254                         NFC_CMD_NFCCMD | NFC_CMD_NFCWR | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_FIVE |
255                         MT29F_CMD_WRITE_1 << 2,
256                         cycle0, cycle1234);
257
258         while (!mt29f_isTransferComplete());
259
260         mt29f_sendCommand(
261                         NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
262                         MT29F_CMD_WRITE_2 << 2,
263                         0, 0);
264
265         while (!mt29f_isReadyBusy());
266
267         if (!mt29f_isOperationComplete())
268         {
269                 LOG_ERR("mt29f: error writing page\n");
270                 fls->hw->status |= MT29F_ERR_WRITE;
271                 return 0;
272         }
273
274         return size;
275 }
276
277
278 static int mt29f_error(struct KBlock *blk)
279 {
280         Mt29f *fls = FLASH_CAST(blk);
281         return fls->hw->status;
282 }
283
284
285 static void mt29f_clearerror(struct KBlock *blk)
286 {
287         Mt29f *fls = FLASH_CAST(blk);
288         fls->hw->status = 0;
289 }
290
291
292 static const KBlockVTable mt29f_buffered_vt =
293 {
294         .readDirect = mt29f_readDirect,
295         .writeDirect = mt29f_writeDirect,
296
297         .readBuf = kblock_swReadBuf,
298         .writeBuf = kblock_swWriteBuf,
299         .load = kblock_swLoad,
300         .store = kblock_swStore,
301
302         .close = kblock_swClose,
303
304         .error = mt29f_error,
305         .clearerr = mt29f_clearerror,
306 };
307
308
309 static const KBlockVTable mt29f_unbuffered_vt =
310 {
311         .readDirect = mt29f_readDirect,
312         .writeDirect = mt29f_writeDirect,
313
314         .close = kblock_swClose,
315
316         .error = mt29f_error,
317         .clearerr = mt29f_clearerror,
318 };
319
320
321 static struct Mt29fHardware mt29f_hw;
322
323
324 static void common_init(Mt29f *fls)
325 {
326         memset(fls, 0, sizeof(*fls));
327         DB(fls->blk.priv.type = KBT_MT29F);
328
329         fls->hw = &mt29f_hw;
330
331         fls->blk.blk_size = MT29F_PAGE_SIZE;
332         fls->blk.blk_cnt =  MT29F_SIZE / MT29F_PAGE_SIZE;
333
334         /*
335          * TODO: put following stuff in hw_ file dependent (and configurable cs?)
336          * Parameters for MT29F8G08AAD
337          */
338         pmc_periphEnable(PIOA_ID);
339         pmc_periphEnable(PIOC_ID);
340         pmc_periphEnable(PIOD_ID);
341
342         PIO_PERIPH_SEL(PIOA_BASE, MT29F_PINS_PORTA, MT29F_PERIPH_PORTA);
343         PIOA_PDR = MT29F_PINS_PORTA;
344         PIOA_PUER = MT29F_PINS_PORTA;
345
346         PIO_PERIPH_SEL(PIOC_BASE, MT29F_PINS_PORTC, MT29F_PERIPH_PORTC);
347         PIOC_PDR = MT29F_PINS_PORTC;
348         PIOC_PUER = MT29F_PINS_PORTC;
349
350         PIO_PERIPH_SEL(PIOD_BASE, MT29F_PINS_PORTD, MT29F_PERIPH_PORTD);
351         PIOD_PDR = MT29F_PINS_PORTD;
352         PIOD_PUER = MT29F_PINS_PORTD;
353
354     pmc_periphEnable(SMC_SDRAMC_ID);
355
356     SMC_SETUP0 = SMC_SETUP_NWE_SETUP(0)
357                 | SMC_SETUP_NCS_WR_SETUP(0)
358                 | SMC_SETUP_NRD_SETUP(0)
359                 | SMC_SETUP_NCS_RD_SETUP(0);
360
361     SMC_PULSE0 = SMC_PULSE_NWE_PULSE(2)
362                 | SMC_PULSE_NCS_WR_PULSE(3)
363                 | SMC_PULSE_NRD_PULSE(2)
364                 | SMC_PULSE_NCS_RD_PULSE(3);
365
366     SMC_CYCLE0 = SMC_CYCLE_NWE_CYCLE(3)
367                 | SMC_CYCLE_NRD_CYCLE(3);
368
369     SMC_TIMINGS0 = SMC_TIMINGS_TCLR(1)
370                 | SMC_TIMINGS_TADL(6)
371                 | SMC_TIMINGS_TAR(4)
372                 | SMC_TIMINGS_TRR(2)
373                 | SMC_TIMINGS_TWB(9)
374                 | SMC_TIMINGS_RBNSEL(7)
375                 | SMC_TIMINGS_NFSEL;
376
377     SMC_MODE0 = SMC_MODE_READ_MODE
378                 | SMC_MODE_WRITE_MODE;
379 }
380
381
382 void mt29f_hw_init(Mt29f *fls)
383 {
384         common_init(fls);
385         fls->blk.priv.vt = &mt29f_buffered_vt;
386         fls->blk.priv.flags |= KB_BUFFERED;
387         fls->blk.priv.buf = (void *)NFC_CMD_BASE_ADDR;
388
389         // Load the first block in the cache
390         void *start = 0x0;
391         memcpy(fls->blk.priv.buf, start, fls->blk.blk_size);
392 }
393
394
395 void mt29f_hw_initUnbuffered(Mt29f *fls)
396 {
397         common_init(fls);
398         fls->blk.priv.vt = &mt29f_unbuffered_vt;
399 }
400