sam3 nand driver: initial ECC implementation.
[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
50 #include <drv/timer.h>
51 #include <drv/mt29f.h>
52
53 #include <cpu/power.h> /* cpu_relax() */
54 #include <cpu/types.h>
55
56 #include <string.h> /* memcpy() */
57
58 // Timeout for NAND operations in ms
59 #define MT29F_TMOUT  100
60
61 // NAND flash status codes
62 #define MT29F_STATUS_READY             BV(6)
63 #define MT29F_STATUS_ERROR             BV(0)
64
65 // NAND flash commands
66 #define MT29F_CMD_READ_1               0x00
67 #define MT29F_CMD_READ_2               0x30
68 #define MT29F_CMD_COPYBACK_READ_1      0x00
69 #define MT29F_CMD_COPYBACK_READ_2      0x35
70 #define MT29F_CMD_COPYBACK_PROGRAM_1   0x85
71 #define MT29F_CMD_COPYBACK_PROGRAM_2   0x10
72 #define MT29F_CMD_RANDOM_OUT           0x05
73 #define MT29F_CMD_RANDOM_OUT_2         0xE0
74 #define MT29F_CMD_RANDOM_IN            0x85
75 #define MT29F_CMD_READID               0x90
76 #define MT29F_CMD_WRITE_1              0x80
77 #define MT29F_CMD_WRITE_2              0x10
78 #define MT29F_CMD_ERASE_1              0x60
79 #define MT29F_CMD_ERASE_2              0xD0
80 #define MT29F_CMD_STATUS               0x70
81 #define MT29F_CMD_RESET                0xFF
82
83 // Addresses for sending command, addresses and data bytes to flash
84 #define MT29F_CMD_ADDR    0x60400000
85 #define MT29F_ADDR_ADDR   0x60200000
86 #define MT29F_DATA_ADDR   0x60000000
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 getAddrCycles(uint32_t page, uint16_t offset, uint32_t *cycle0, uint32_t *cycle1234)
105 {
106         ASSERT(offset < MT29F_PAGE_SIZE);
107
108         *cycle0 = offset & 0xff;
109         *cycle1234 = (page << 8) | ((offset >> 8) & 0xf);
110
111         LOG_INFO("mt29f addr: %lx %lx\n", *cycle1234, *cycle0);
112 }
113
114
115 INLINE bool nfcIsBusy(void)
116 {
117         return HWREG(NFC_CMD_BASE_ADDR + NFC_CMD_NFCCMD) & 0x8000000;
118 }
119
120 INLINE bool isCmdDone(void)
121 {
122     return SMC_SR & SMC_SR_CMDDONE;
123 }
124
125 static bool waitReadyBusy(void)
126 {
127         time_t start = timer_clock();
128
129         while (!(SMC_SR & SMC_SR_RB_EDGE0))
130         {
131                 cpu_relax();
132                 if (timer_clock() - start > MT29F_TMOUT)
133                 {
134                         LOG_INFO("mt29f: R/B timeout\n");
135                         return false;
136                 }
137         }
138
139         return true;
140 }
141
142 /*
143  * Wait for transfer to complete until timeout.
144  * If transfer completes return true, false in case of timeout.
145  */
146 static bool waitTransferComplete(void)
147 {
148         time_t start = timer_clock();
149
150         while (!(SMC_SR & SMC_SR_XFRDONE))
151         {
152                 cpu_relax();
153                 if (timer_clock() - start > MT29F_TMOUT)
154                 {
155                         LOG_INFO("mt29f: xfer complete timeout\n");
156                         return false;
157                 }
158         }
159
160         return true;
161 }
162
163
164 /*
165  * Send command to NAND and wait for completion.
166  */
167 static void sendCommand(uint32_t cmd,
168                 int num_cycles, uint32_t cycle0, uint32_t cycle1234)
169 {
170         reg32_t *cmd_addr;
171
172         while (nfcIsBusy());
173
174         if (num_cycles == 5)
175                 SMC_ADDR = cycle0;
176
177         cmd_addr = (reg32_t *)(NFC_CMD_BASE_ADDR + cmd);
178         *cmd_addr = cycle1234;
179
180         while (!isCmdDone());
181 }
182
183
184 static bool isOperationComplete(void)
185 {
186         uint8_t status;
187
188         sendCommand(
189                 NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
190                 MT29F_CMD_STATUS << 2,
191                 0, 0, 0);
192
193         status = (uint8_t)HWREG(MT29F_DATA_ADDR);
194         return (status & MT29F_STATUS_READY) && !(status & MT29F_STATUS_ERROR);
195 }
196
197
198 static void chipReset(void)
199 {
200         sendCommand(
201                 NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
202                 MT29F_CMD_RESET << 2,
203                 0, 0, 0);
204
205         waitReadyBusy();
206 }
207
208
209 /**
210  * Erase the whole block containing given page.
211  */
212 int mt29f_blockErase(Mt29f *chip, uint32_t page)
213 {
214         uint32_t cycle0;
215         uint32_t cycle1234;
216
217         getAddrCycles(page, 0, &cycle0, &cycle1234);
218
219         sendCommand(
220                 NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_THREE | NFC_CMD_VCMD2 |
221                 (MT29F_CMD_ERASE_2 << 10) | (MT29F_CMD_ERASE_1 << 2),
222                 3, 0, cycle1234 >> 8);
223
224         waitReadyBusy();
225
226         if (!isOperationComplete())
227         {
228                 LOG_ERR("mt29f: error erasing block\n");
229                 chip->status |= MT29F_ERR_ERASE;
230                 return -1;
231         }
232
233         return 0;
234 }
235
236
237 /**
238  * Read Device ID and configuration codes.
239  */
240 bool mt29f_getDevId(Mt29f *chip, uint8_t dev_id[5])
241 {
242         sendCommand(
243                 NFC_CMD_NFCCMD | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_ONE |
244                 MT29F_CMD_READID << 2,
245                 1, 0, 0);
246
247         waitReadyBusy();
248         if (!waitTransferComplete())
249         {
250                 LOG_ERR("mt29f: getDevId timeout\n");
251                 chip->status |= MT29F_ERR_RD_TMOUT;
252                 return false;
253         }
254
255         memcpy(dev_id, (void *)NFC_SRAM_BASE_ADDR, 5);
256         return true;
257 }
258
259
260 static bool checkEcc(void)
261 {
262         // TODO: implement it actually...
263         LOG_INFO("ECC_SR1: 0x%lx\n", SMC_ECC_SR1);
264         return true;
265 }
266
267
268 static bool mt29f_readPage(Mt29f *chip, uint32_t page, uint16_t offset)
269 {
270         uint32_t cycle0;
271         uint32_t cycle1234;
272
273         LOG_INFO("mt29f_readPage: page 0x%lx off 0x%x\n", page, offset);
274
275         getAddrCycles(page, offset, &cycle0, &cycle1234);
276
277         sendCommand(
278                 NFC_CMD_NFCCMD | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_FIVE | NFC_CMD_VCMD2 |
279                 (MT29F_CMD_READ_2 << 10) | (MT29F_CMD_READ_1 << 2),
280                 5, cycle0, cycle1234);
281
282         waitReadyBusy();
283         if (!waitTransferComplete())
284         {
285                 LOG_ERR("mt29f: read timeout\n");
286                 chip->status |= MT29F_ERR_RD_TMOUT;
287                 return false;
288         }
289
290         return true;
291 }
292
293
294 /*
295  * Read page data and ECC, checking for errors and fixing them if
296  * possible.
297  */
298 bool mt29f_read(Mt29f *chip, uint32_t page, void *buf, uint16_t size)
299 {
300         ASSERT(size <= MT29F_DATA_SIZE);
301
302         if (!mt29f_readPage(chip, page, 0))
303                 return false;
304
305         memcpy(buf, (void *)NFC_SRAM_BASE_ADDR, size);
306
307         checkEcc();
308
309         return true;
310 }
311
312
313 /*
314  * Write data in NFC SRAM buffer to a NAND page, starting at a given offset.
315  * Usually offset will be 0 to write data or MT29F_DATA_SIZE to write the spare
316  * area.
317  *
318  * According to datasheet to get ECC computed by hardware is sufficient
319  * to write the main area.  But it seems that in that way the last ECC_PR
320  * register is not generated.  The workaround is to write data and dummy (ff)
321  * spare data in one write, at this point the last ECC_PR is correct and
322  * ECC data can be written in the spare area with a second program operation.
323  */
324 static bool mt29f_writePage(Mt29f *chip, uint32_t page, uint16_t offset)
325 {
326         uint32_t cycle0;
327         uint32_t cycle1234;
328
329         LOG_INFO("mt29f_writePage: page 0x%lx off 0x%x\n", page, offset);
330
331         getAddrCycles(page, offset, &cycle0, &cycle1234);
332
333         sendCommand(
334                         NFC_CMD_NFCCMD | NFC_CMD_NFCWR | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_FIVE |
335                         MT29F_CMD_WRITE_1 << 2,
336                         5, cycle0, cycle1234);
337
338         if (!waitTransferComplete())
339         {
340                 LOG_ERR("mt29f: write timeout\n");
341                 chip->status |= MT29F_ERR_WR_TMOUT;
342                 return false;
343         }
344
345         sendCommand(
346                         NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
347                         MT29F_CMD_WRITE_2 << 2,
348                         0, 0, 0);
349
350         waitReadyBusy();
351
352         if (!isOperationComplete())
353         {
354                 LOG_ERR("mt29f: error writing page\n");
355                 chip->status |= MT29F_ERR_WRITE;
356                 return false;
357         }
358
359         return true;
360 }
361
362
363 /*
364  * Write data in a page.
365  */
366 static bool mt29f_writePageData(Mt29f *chip, uint32_t page, const void *buf, uint16_t size)
367 {
368         ASSERT(size <= MT29F_DATA_SIZE);
369
370         memset((void *)NFC_SRAM_BASE_ADDR, 0xff, MT29F_PAGE_SIZE);
371         memcpy((void *)NFC_SRAM_BASE_ADDR, buf, size);
372
373         return mt29f_writePage(chip, page, 0);
374 }
375
376
377 /*
378  * Write the ECC for a page.
379  *
380  * ECC data are extracted from ECC_PRx registers and written
381  * in the page's spare area.
382  * For 2048 bytes pages and 1 ECC word each 256 bytes,
383  * 24 bytes of ECC data are stored.
384  */
385 static bool mt29f_writePageEcc(Mt29f *chip, uint32_t page)
386 {
387         int i;
388         uint32_t *buf = (uint32_t *)NFC_SRAM_BASE_ADDR;
389
390         memset((void *)NFC_SRAM_BASE_ADDR, 0xff, MT29F_SPARE_SIZE);
391
392         for (i = 0; i < MT29F_ECC_NWORDS; i++)
393                 buf[i] = *((reg32_t *)(SMC_BASE + SMC_ECC_PR0_OFF) + i);
394
395         return mt29f_writePage(chip, page, MT29F_DATA_SIZE);
396 }
397
398
399 bool mt29f_write(Mt29f *chip, uint32_t page, const void *buf, uint16_t size)
400 {
401         return
402                 mt29f_writePageData(chip, page, buf, size) &&
403                 mt29f_writePageEcc(chip, page);
404 }
405
406
407 int mt29f_error(Mt29f *chip)
408 {
409         return chip->status;
410 }
411
412
413 void mt29f_clearError(Mt29f *chip)
414 {
415         chip->status = 0;
416 }
417
418
419 static void initPio(void)
420 {
421         /*
422          * TODO: put following stuff in hw_ file dependent (and configurable cs?)
423          * Parameters for MT29F8G08AAD
424          */
425         pmc_periphEnable(PIOA_ID);
426         pmc_periphEnable(PIOC_ID);
427         pmc_periphEnable(PIOD_ID);
428
429         PIO_PERIPH_SEL(PIOA_BASE, MT29F_PINS_PORTA, MT29F_PERIPH_PORTA);
430         PIOA_PDR = MT29F_PINS_PORTA;
431         PIOA_PUER = MT29F_PINS_PORTA;
432
433         PIO_PERIPH_SEL(PIOC_BASE, MT29F_PINS_PORTC, MT29F_PERIPH_PORTC);
434         PIOC_PDR = MT29F_PINS_PORTC;
435         PIOC_PUER = MT29F_PINS_PORTC;
436
437         PIO_PERIPH_SEL(PIOD_BASE, MT29F_PINS_PORTD, MT29F_PERIPH_PORTD);
438         PIOD_PDR = MT29F_PINS_PORTD;
439         PIOD_PUER = MT29F_PINS_PORTD;
440
441     pmc_periphEnable(SMC_SDRAMC_ID);
442 }
443
444
445 static void initSmc(void)
446 {
447     SMC_SETUP0 = SMC_SETUP_NWE_SETUP(0)
448                 | SMC_SETUP_NCS_WR_SETUP(0)
449                 | SMC_SETUP_NRD_SETUP(0)
450                 | SMC_SETUP_NCS_RD_SETUP(0);
451
452     SMC_PULSE0 = SMC_PULSE_NWE_PULSE(2)
453                 | SMC_PULSE_NCS_WR_PULSE(3)
454                 | SMC_PULSE_NRD_PULSE(2)
455                 | SMC_PULSE_NCS_RD_PULSE(3);
456
457     SMC_CYCLE0 = SMC_CYCLE_NWE_CYCLE(3)
458                 | SMC_CYCLE_NRD_CYCLE(3);
459
460     SMC_TIMINGS0 = SMC_TIMINGS_TCLR(1)
461                 | SMC_TIMINGS_TADL(6)
462                 | SMC_TIMINGS_TAR(4)
463                 | SMC_TIMINGS_TRR(2)
464                 | SMC_TIMINGS_TWB(9)
465                 | SMC_TIMINGS_RBNSEL(7)
466                 | SMC_TIMINGS_NFSEL;
467
468     SMC_MODE0 = SMC_MODE_READ_MODE
469                 | SMC_MODE_WRITE_MODE;
470
471         SMC_CFG = SMC_CFG_PAGESIZE_PS2048_64
472                 | SMC_CFG_EDGECTRL
473                 | SMC_CFG_DTOMUL_X1048576
474                 | SMC_CFG_DTOCYC(0xF)
475                 | SMC_CFG_WSPARE
476                 | SMC_CFG_RSPARE;
477
478         // Disable SMC interrupts, reset and enable NFC controller
479         SMC_IDR = ~0;
480         SMC_CTRL = 0;
481         SMC_CTRL = SMC_CTRL_NFCEN;
482
483         // Enable ECC, 1 ECC per 256 bytes
484         SMC_ECC_CTRL = SMC_ECC_CTRL_SWRST;
485         SMC_ECC_MD = SMC_ECC_MD_ECC_PAGESIZE_PS2048_64 | SMC_ECC_MD_TYPCORREC_C256B;
486 }
487
488
489 void mt29f_init(Mt29f *chip)
490 {
491         initPio();
492         initSmc();
493
494         mt29f_clearError(chip);
495
496         chipReset();
497 }
498