mt29f on SAM3: handle read/write timeout; fix SMC configuration;
[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 // Timeout for read/write transfers in ms
60 #define MT29F_XFER_TMOUT  1000
61
62 // NAND flash status codes
63 #define MT29F_STATUS_READY             BV(6)
64 #define MT29F_STATUS_ERROR             BV(0)
65
66 // NAND flash commands
67 #define MT29F_CMD_READ_1               0x00
68 #define MT29F_CMD_READ_2               0x30
69 #define MT29F_CMD_COPYBACK_READ_1      0x00
70 #define MT29F_CMD_COPYBACK_READ_2      0x35
71 #define MT29F_CMD_COPYBACK_PROGRAM_1   0x85
72 #define MT29F_CMD_COPYBACK_PROGRAM_2   0x10
73 #define MT29F_CMD_RANDOM_OUT           0x05
74 #define MT29F_CMD_RANDOM_OUT_2         0xE0
75 #define MT29F_CMD_RANDOM_IN            0x85
76 #define MT29F_CMD_READID               0x90
77 #define MT29F_CMD_WRITE_1              0x80
78 #define MT29F_CMD_WRITE_2              0x10
79 #define MT29F_CMD_ERASE_1              0x60
80 #define MT29F_CMD_ERASE_2              0xD0
81 #define MT29F_CMD_STATUS               0x70
82 #define MT29F_CMD_RESET                0xFF
83
84
85 struct Mt29fHardware
86 {
87         uint8_t status;
88 };
89
90
91 /*
92  * Translate flash page index plus a byte offset
93  * in the five address cycles format needed by NAND.
94  *
95  * Cycles in x8 mode as the MT29F2G08AAD
96  * CA = column addr, PA = page addr, BA = block addr
97  *
98  * Cycle    I/O7  I/O6  I/O5  I/O4  I/O3  I/O2  I/O1  I/O0
99  * -------------------------------------------------------
100  * First    CA7   CA6   CA5   CA4   CA3   CA2   CA1   CA0
101  * Second   LOW   LOW   LOW   LOW   CA11  CA10  CA9   CA8
102  * Third    BA7   BA6   PA5   PA4   PA3   PA2   PA1   PA0
103  * Fourth   BA15  BA14  BA13  BA12  BA11  BA10  BA9   BA8
104  * Fifth    LOW   LOW   LOW   LOW   LOW   LOW   LOW   BA16
105  */
106 static void mt29f_getAddrCycles(block_idx_t page, size_t offset, uint32_t *cycle0, uint32_t *cycle1234)
107 {
108         uint32_t addr = (page * MT29F_PAGE_SIZE) + offset;
109
110         /*
111          * offset nibbles  77776666 55554444 33332222 11110000
112          * cycle1234       -------7 66665555 ----4444 33332222
113          * cycle0          11110000
114          */
115         *cycle0 = addr & 0xff;
116         *cycle1234 = ((addr >> 8) & 0x00000fff) | ((addr >> 4) & 0x01ff0000);
117 }
118
119
120 INLINE bool mt29f_isBusy(void)
121 {
122         return HWREG(NFC_CMD_BASE_ADDR + NFC_CMD_NFCCMD) & 0x8000000;
123 }
124
125 INLINE bool mt29f_isCmdDone(void)
126 {
127     return SMC_SR & SMC_SR_CMDDONE;
128 }
129
130 INLINE bool mt29f_isReadyBusy(void)
131 {
132     return SMC_SR & SMC_SR_RB_EDGE0;
133 }
134
135 /*
136  * Wait for transfer to complete until timeout.
137  * If transfer completes return true, false in case of timeout.
138  */
139 INLINE bool mt29f_waitTransferComplete(void)
140 {
141         time_t start = timer_clock();
142
143         while (!(SMC_SR & SMC_SR_XFRDONE))
144         {
145                 cpu_relax();
146                 if (timer_clock() - start > MT29F_XFER_TMOUT)
147                         return false;
148         }
149
150         kprintf("xfer ok!!\n");
151         return true;
152 }
153
154
155 /*
156  * Send command to NAND and wait for completion.
157  */
158 static void mt29f_sendCommand(uint32_t cmd, uint32_t cycle0, uint32_t cycle1234)
159 {
160         reg32_t *cmd_addr;
161
162         while (mt29f_isBusy());
163
164         SMC_ADDR = cycle0;
165
166         cmd_addr = (reg32_t *)(NFC_CMD_BASE_ADDR + cmd);
167         *cmd_addr = cycle1234;
168
169         while (!mt29f_isCmdDone());
170 }
171
172
173 static bool mt29f_isOperationComplete(void)
174 {
175         uint8_t status;
176
177         mt29f_sendCommand(
178                 NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
179                 MT29F_CMD_STATUS << 2, 0, 0);
180
181         status = (uint8_t)HWREG(MT29F_DATA_ADDR);
182         return (status & MT29F_STATUS_READY) && !(status & MT29F_STATUS_ERROR);
183 }
184
185
186 static void mt29f_reset(void)
187 {
188         mt29f_sendCommand(
189                 NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
190                 MT29F_CMD_RESET << 2,
191                 0, 0);
192
193         while (!mt29f_isReadyBusy());
194 }
195
196
197 /**
198  * Erase the whole block containing given page.
199  */
200 int mt29f_blockErase(Mt29f *fls, block_idx_t page)
201 {
202         uint32_t cycle0;
203         uint32_t cycle1234;
204
205         mt29f_getAddrCycles(page, 0, &cycle0, &cycle1234);
206
207         mt29f_sendCommand(
208                 NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_THREE | NFC_CMD_VCMD2 |
209                 (MT29F_CMD_ERASE_2 << 10) | (MT29F_CMD_ERASE_1 << 2),
210                 0, cycle1234);
211
212         while (!mt29f_isReadyBusy());
213
214         if (!mt29f_isOperationComplete())
215         {
216                 LOG_ERR("mt29f: error erasing block\n");
217                 fls->hw->status |= MT29F_ERR_ERASE;
218                 return -1;
219         }
220
221         return 0;
222 }
223
224
225 static size_t mt29f_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
226 {
227         Mt29f *fls = FLASH_CAST(blk);
228         uint32_t cycle0;
229         uint32_t cycle1234;
230
231         ASSERT(offset == 0);
232         ASSERT(size == blk->blk_size);
233
234         kprintf("read direct\n");
235
236         mt29f_getAddrCycles(idx, 0, &cycle0, &cycle1234);
237
238         mt29f_sendCommand(
239                 NFC_CMD_NFCCMD | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_FIVE | NFC_CMD_VCMD2 |
240                 (MT29F_CMD_READ_2 << 10) | (MT29F_CMD_READ_1 << 2),
241                 cycle0, cycle1234);
242
243         while (!mt29f_isReadyBusy());
244         if (!mt29f_waitTransferComplete())
245         {
246                 LOG_ERR("mt29f: read timeout\n");
247                 fls->hw->status |= MT29F_ERR_RD_TMOUT;
248                 return 0;
249         }
250
251         if (!kblock_buffered(blk) && (buf != (void *)NFC_SRAM_BASE_ADDR))
252                 memcpy(buf, (void *)NFC_SRAM_BASE_ADDR, size);
253
254         return size;
255 }
256
257
258 static size_t mt29f_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
259 {
260         Mt29f *fls = FLASH_CAST(blk);
261         uint32_t cycle0;
262         uint32_t cycle1234;
263
264         ASSERT(offset == 0);
265         ASSERT(size == blk->blk_size);
266
267         kprintf("write direct\n");
268
269         if (!kblock_buffered(blk) && (_buf != (void *)NFC_SRAM_BASE_ADDR))
270                 memcpy((void *)NFC_SRAM_BASE_ADDR, _buf, size);
271
272         mt29f_getAddrCycles(idx, 0, &cycle0, &cycle1234);
273
274         mt29f_sendCommand(
275                         NFC_CMD_NFCCMD | NFC_CMD_NFCWR | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_FIVE |
276                         MT29F_CMD_WRITE_1 << 2,
277                         cycle0, cycle1234);
278
279         if (!mt29f_waitTransferComplete())
280         {
281                 LOG_ERR("mt29f: write timeout\n");
282                 fls->hw->status |= MT29F_ERR_WR_TMOUT;
283                 return 0;
284         }
285
286         mt29f_sendCommand(
287                         NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
288                         MT29F_CMD_WRITE_2 << 2,
289                         0, 0);
290
291         while (!mt29f_isReadyBusy());
292
293         if (!mt29f_isOperationComplete())
294         {
295                 LOG_ERR("mt29f: error writing page\n");
296                 fls->hw->status |= MT29F_ERR_WRITE;
297                 return 0;
298         }
299
300         return size;
301 }
302
303
304 static int mt29f_error(struct KBlock *blk)
305 {
306         Mt29f *fls = FLASH_CAST(blk);
307         return fls->hw->status;
308 }
309
310
311 static void mt29f_clearerror(struct KBlock *blk)
312 {
313         Mt29f *fls = FLASH_CAST(blk);
314         fls->hw->status = 0;
315 }
316
317
318 static const KBlockVTable mt29f_buffered_vt =
319 {
320         .readDirect = mt29f_readDirect,
321         .writeDirect = mt29f_writeDirect,
322
323         .readBuf = kblock_swReadBuf,
324         .writeBuf = kblock_swWriteBuf,
325         .load = kblock_swLoad,
326         .store = kblock_swStore,
327
328         .close = kblock_swClose,
329
330         .error = mt29f_error,
331         .clearerr = mt29f_clearerror,
332 };
333
334
335 static const KBlockVTable mt29f_unbuffered_vt =
336 {
337         .readDirect = mt29f_readDirect,
338         .writeDirect = mt29f_writeDirect,
339
340         .close = kblock_swClose,
341
342         .error = mt29f_error,
343         .clearerr = mt29f_clearerror,
344 };
345
346
347 static struct Mt29fHardware mt29f_hw;
348
349
350 static void common_init(Mt29f *fls)
351 {
352         memset(fls, 0, sizeof(*fls));
353         DB(fls->blk.priv.type = KBT_MT29F);
354
355         fls->hw = &mt29f_hw;
356
357         fls->blk.blk_size = MT29F_PAGE_SIZE;
358         fls->blk.blk_cnt =  MT29F_SIZE / MT29F_PAGE_SIZE;
359
360         /*
361          * TODO: put following stuff in hw_ file dependent (and configurable cs?)
362          * Parameters for MT29F8G08AAD
363          */
364         pmc_periphEnable(PIOA_ID);
365         pmc_periphEnable(PIOC_ID);
366         pmc_periphEnable(PIOD_ID);
367
368         PIO_PERIPH_SEL(PIOA_BASE, MT29F_PINS_PORTA, MT29F_PERIPH_PORTA);
369         PIOA_PDR = MT29F_PINS_PORTA;
370         PIOA_PUER = MT29F_PINS_PORTA;
371
372         PIO_PERIPH_SEL(PIOC_BASE, MT29F_PINS_PORTC, MT29F_PERIPH_PORTC);
373         PIOC_PDR = MT29F_PINS_PORTC;
374         PIOC_PUER = MT29F_PINS_PORTC;
375
376         PIO_PERIPH_SEL(PIOD_BASE, MT29F_PINS_PORTD, MT29F_PERIPH_PORTD);
377         PIOD_PDR = MT29F_PINS_PORTD;
378         PIOD_PUER = MT29F_PINS_PORTD;
379
380     pmc_periphEnable(SMC_SDRAMC_ID);
381
382     SMC_SETUP0 = SMC_SETUP_NWE_SETUP(0)
383                 | SMC_SETUP_NCS_WR_SETUP(0)
384                 | SMC_SETUP_NRD_SETUP(0)
385                 | SMC_SETUP_NCS_RD_SETUP(0);
386
387     SMC_PULSE0 = SMC_PULSE_NWE_PULSE(2)
388                 | SMC_PULSE_NCS_WR_PULSE(3)
389                 | SMC_PULSE_NRD_PULSE(2)
390                 | SMC_PULSE_NCS_RD_PULSE(3);
391
392     SMC_CYCLE0 = SMC_CYCLE_NWE_CYCLE(3)
393                 | SMC_CYCLE_NRD_CYCLE(3);
394
395     SMC_TIMINGS0 = SMC_TIMINGS_TCLR(1)
396                 | SMC_TIMINGS_TADL(6)
397                 | SMC_TIMINGS_TAR(4)
398                 | SMC_TIMINGS_TRR(2)
399                 | SMC_TIMINGS_TWB(9)
400                 | SMC_TIMINGS_RBNSEL(7)
401                 | SMC_TIMINGS_NFSEL;
402
403     SMC_MODE0 = SMC_MODE_READ_MODE
404                 | SMC_MODE_WRITE_MODE;
405
406         SMC_CFG = SMC_CFG_PAGESIZE_PS2048_64
407                 | SMC_CFG_EDGECTRL
408                 | SMC_CFG_DTOMUL_X1048576
409                 | SMC_CFG_DTOCYC(0xF);
410
411         // Disable SMC interrupts, reset and enable NFC controller
412         SMC_IDR = ~0;
413         SMC_CTRL = 0;
414         SMC_CTRL = SMC_CTRL_NFCEN;
415
416         mt29f_reset();
417 }
418
419
420 void mt29f_init(Mt29f *fls)
421 {
422         common_init(fls);
423         fls->blk.priv.vt = &mt29f_buffered_vt;
424         fls->blk.priv.flags |= KB_BUFFERED;
425         fls->blk.priv.buf = (void *)NFC_SRAM_BASE_ADDR;
426
427         // Load the first block in the cache
428         //mt29f_readDirect(&fls->blk, 0, (void *)NFC_SRAM_BASE_ADDR, 0, MT29F_PAGE_SIZE);
429         //debug
430         memset((void *)NFC_SRAM_BASE_ADDR, 0xbb, MT29F_PAGE_SIZE);
431 }
432
433
434 void mt29f_initUnbuffered(Mt29f *fls)
435 {
436         common_init(fls);
437         fls->blk.priv.vt = &mt29f_unbuffered_vt;
438 }
439