mt29f nand driver: use context the right way, storing and using right chip select...
[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 // Get chip select mask for command register
89 #define MT29F_CSID(chip)  (((chip)->chip_select << NFC_CMD_CSID_SHIFT) & NFC_CMD_CSID_MASK)
90
91
92 /*
93  * Translate flash page index plus a byte offset
94  * in the five address cycles format needed by NAND.
95  *
96  * Cycles in x8 mode as the MT29F2G08AAD
97  * CA = column addr, PA = page addr, BA = block addr
98  *
99  * Cycle    I/O7  I/O6  I/O5  I/O4  I/O3  I/O2  I/O1  I/O0
100  * -------------------------------------------------------
101  * First    CA7   CA6   CA5   CA4   CA3   CA2   CA1   CA0
102  * Second   LOW   LOW   LOW   LOW   CA11  CA10  CA9   CA8
103  * Third    BA7   BA6   PA5   PA4   PA3   PA2   PA1   PA0
104  * Fourth   BA15  BA14  BA13  BA12  BA11  BA10  BA9   BA8
105  * Fifth    LOW   LOW   LOW   LOW   LOW   LOW   LOW   BA16
106  */
107 static void getAddrCycles(uint32_t page, uint16_t offset, uint32_t *cycle0, uint32_t *cycle1234)
108 {
109         ASSERT(offset < MT29F_PAGE_SIZE);
110
111         *cycle0 = offset & 0xff;
112         *cycle1234 = (page << 8) | ((offset >> 8) & 0xf);
113
114         LOG_INFO("mt29f addr: %lx %lx\n", *cycle1234, *cycle0);
115 }
116
117
118 INLINE bool nfcIsBusy(void)
119 {
120         return HWREG(NFC_CMD_BASE_ADDR + NFC_CMD_NFCCMD) & 0x8000000;
121 }
122
123 INLINE bool isCmdDone(void)
124 {
125     return SMC_SR & SMC_SR_CMDDONE;
126 }
127
128 static bool waitReadyBusy(void)
129 {
130         time_t start = timer_clock();
131
132         while (!(SMC_SR & SMC_SR_RB_EDGE0))
133         {
134                 cpu_relax();
135                 if (timer_clock() - start > MT29F_TMOUT)
136                 {
137                         LOG_INFO("mt29f: R/B timeout\n");
138                         return false;
139                 }
140         }
141
142         return true;
143 }
144
145 /*
146  * Wait for transfer to complete until timeout.
147  * If transfer completes return true, false in case of timeout.
148  */
149 static bool waitTransferComplete(void)
150 {
151         time_t start = timer_clock();
152
153         while (!(SMC_SR & SMC_SR_XFRDONE))
154         {
155                 cpu_relax();
156                 if (timer_clock() - start > MT29F_TMOUT)
157                 {
158                         LOG_INFO("mt29f: xfer complete timeout\n");
159                         return false;
160                 }
161         }
162
163         return true;
164 }
165
166
167 /*
168  * Send command to NAND and wait for completion.
169  */
170 static void sendCommand(uint32_t cmd,
171                 int num_cycles, uint32_t cycle0, uint32_t cycle1234)
172 {
173         reg32_t *cmd_addr;
174
175         while (nfcIsBusy());
176
177         if (num_cycles == 5)
178                 SMC_ADDR = cycle0;
179
180         cmd_addr = (reg32_t *)(NFC_CMD_BASE_ADDR + cmd);
181         *cmd_addr = cycle1234;
182
183         while (!isCmdDone());
184 }
185
186
187 static bool isOperationComplete(Mt29f *chip)
188 {
189         uint8_t status;
190
191         sendCommand(MT29F_CSID(chip) |
192                 NFC_CMD_NFCCMD | NFC_CMD_ACYCLE_NONE |
193                 MT29F_CMD_STATUS << 2,
194                 0, 0, 0);
195
196         status = (uint8_t)HWREG(MT29F_DATA_ADDR);
197         return (status & MT29F_STATUS_READY) && !(status & MT29F_STATUS_ERROR);
198 }
199
200
201 static void chipReset(Mt29f *chip)
202 {
203         sendCommand(MT29F_CSID(chip) |
204                 NFC_CMD_NFCCMD | NFC_CMD_ACYCLE_NONE |
205                 MT29F_CMD_RESET << 2,
206                 0, 0, 0);
207
208         waitReadyBusy();
209 }
210
211
212 /**
213  * Erase the whole block containing given page.
214  */
215 int mt29f_blockErase(Mt29f *chip, uint32_t page)
216 {
217         uint32_t cycle0;
218         uint32_t cycle1234;
219
220         getAddrCycles(page, 0, &cycle0, &cycle1234);
221
222         sendCommand(MT29F_CSID(chip) |
223                 NFC_CMD_NFCCMD | NFC_CMD_ACYCLE_THREE | NFC_CMD_VCMD2 |
224                 (MT29F_CMD_ERASE_2 << 10) | (MT29F_CMD_ERASE_1 << 2),
225                 3, 0, cycle1234 >> 8);
226
227         waitReadyBusy();
228
229         if (!isOperationComplete(chip))
230         {
231                 LOG_ERR("mt29f: error erasing block\n");
232                 chip->status |= MT29F_ERR_ERASE;
233                 return -1;
234         }
235
236         return 0;
237 }
238
239
240 /**
241  * Read Device ID and configuration codes.
242  */
243 bool mt29f_getDevId(Mt29f *chip, uint8_t dev_id[5])
244 {
245         sendCommand(MT29F_CSID(chip) |
246                 NFC_CMD_NFCCMD | NFC_CMD_NFCEN | NFC_CMD_ACYCLE_ONE |
247                 MT29F_CMD_READID << 2,
248                 1, 0, 0);
249
250         waitReadyBusy();
251         if (!waitTransferComplete())
252         {
253                 LOG_ERR("mt29f: getDevId timeout\n");
254                 chip->status |= MT29F_ERR_RD_TMOUT;
255                 return false;
256         }
257
258         memcpy(dev_id, (void *)NFC_SRAM_BASE_ADDR, 5);
259         return true;
260 }
261
262
263 static bool checkEcc(void)
264 {
265         uint32_t sr1 = SMC_ECC_SR1;
266
267         if (sr1)
268         {
269                 LOG_INFO("ECC error, ECC_SR1=0x%lx\n", sr1);
270                 return false;
271         }
272         else
273                 return true;
274 }
275
276
277 static bool mt29f_readPage(Mt29f *chip, uint32_t page, uint16_t offset)
278 {
279         uint32_t cycle0;
280         uint32_t cycle1234;
281
282         LOG_INFO("mt29f_readPage: page 0x%lx off 0x%x\n", page, offset);
283
284         getAddrCycles(page, offset, &cycle0, &cycle1234);
285
286         sendCommand(MT29F_CSID(chip) |
287                 NFC_CMD_NFCCMD | NFC_CMD_NFCEN | NFC_CMD_ACYCLE_FIVE | NFC_CMD_VCMD2 |
288                 (MT29F_CMD_READ_2 << 10) | (MT29F_CMD_READ_1 << 2),
289                 5, cycle0, cycle1234);
290
291         waitReadyBusy();
292         if (!waitTransferComplete())
293         {
294                 LOG_ERR("mt29f: read timeout\n");
295                 chip->status |= MT29F_ERR_RD_TMOUT;
296                 return false;
297         }
298
299         return true;
300 }
301
302
303 /*
304  * Read page data and ECC, checking for errors.
305  * TODO: fix errors with ECC when possible.
306  */
307 bool mt29f_read(Mt29f *chip, uint32_t page, void *buf, uint16_t size)
308 {
309         ASSERT(size <= MT29F_DATA_SIZE);
310
311         if (!mt29f_readPage(chip, page, 0))
312                 return false;
313
314         memcpy(buf, (void *)NFC_SRAM_BASE_ADDR, size);
315
316         return checkEcc();
317 }
318
319
320 /*
321  * Write data in NFC SRAM buffer to a NAND page, starting at a given offset.
322  * Usually offset will be 0 to write data or MT29F_DATA_SIZE to write the spare
323  * area.
324  *
325  * According to datasheet to get ECC computed by hardware is sufficient
326  * to write the main area.  But it seems that in that way the last ECC_PR
327  * register is not generated.  The workaround is to write data and dummy (ff)
328  * spare data in one write, at this point the last ECC_PR is correct and
329  * ECC data can be written in the spare area with a second program operation.
330  */
331 static bool mt29f_writePage(Mt29f *chip, uint32_t page, uint16_t offset)
332 {
333         uint32_t cycle0;
334         uint32_t cycle1234;
335
336         LOG_INFO("mt29f_writePage: page 0x%lx off 0x%x\n", page, offset);
337
338         getAddrCycles(page, offset, &cycle0, &cycle1234);
339
340         sendCommand(MT29F_CSID(chip) |
341                         NFC_CMD_NFCCMD | NFC_CMD_NFCWR | NFC_CMD_NFCEN | NFC_CMD_ACYCLE_FIVE |
342                         MT29F_CMD_WRITE_1 << 2,
343                         5, cycle0, cycle1234);
344
345         if (!waitTransferComplete())
346         {
347                 LOG_ERR("mt29f: write timeout\n");
348                 chip->status |= MT29F_ERR_WR_TMOUT;
349                 return false;
350         }
351
352         sendCommand(MT29F_CSID(chip) |
353                         NFC_CMD_NFCCMD | NFC_CMD_ACYCLE_NONE |
354                         MT29F_CMD_WRITE_2 << 2,
355                         0, 0, 0);
356
357         waitReadyBusy();
358
359         if (!isOperationComplete(chip))
360         {
361                 LOG_ERR("mt29f: error writing page\n");
362                 chip->status |= MT29F_ERR_WRITE;
363                 return false;
364         }
365
366         return true;
367 }
368
369
370 /*
371  * Write data in a page.
372  */
373 static bool mt29f_writePageData(Mt29f *chip, uint32_t page, const void *buf, uint16_t size)
374 {
375         ASSERT(size <= MT29F_DATA_SIZE);
376
377         memset((void *)NFC_SRAM_BASE_ADDR, 0xff, MT29F_PAGE_SIZE);
378         memcpy((void *)NFC_SRAM_BASE_ADDR, buf, size);
379
380         return mt29f_writePage(chip, page, 0);
381 }
382
383
384 /*
385  * Write the ECC for a page.
386  *
387  * ECC data are extracted from ECC_PRx registers and written
388  * in the page's spare area.
389  * For 2048 bytes pages and 1 ECC word each 256 bytes,
390  * 24 bytes of ECC data are stored.
391  */
392 static bool mt29f_writePageEcc(Mt29f *chip, uint32_t page)
393 {
394         int i;
395         uint32_t *buf = (uint32_t *)NFC_SRAM_BASE_ADDR;
396
397         memset((void *)NFC_SRAM_BASE_ADDR, 0xff, MT29F_SPARE_SIZE);
398
399         for (i = 0; i < MT29F_ECC_NWORDS; i++)
400                 buf[i] = *((reg32_t *)(SMC_BASE + SMC_ECC_PR0_OFF) + i);
401
402         return mt29f_writePage(chip, page, MT29F_DATA_SIZE);
403 }
404
405
406 bool mt29f_write(Mt29f *chip, uint32_t page, const void *buf, uint16_t size)
407 {
408         return
409                 mt29f_writePageData(chip, page, buf, size) &&
410                 mt29f_writePageEcc(chip, page);
411 }
412
413
414 int mt29f_error(Mt29f *chip)
415 {
416         return chip->status;
417 }
418
419
420 void mt29f_clearError(Mt29f *chip)
421 {
422         chip->status = 0;
423 }
424
425
426 static void initPio(void)
427 {
428         /*
429          * TODO: put following stuff in hw_ file dependent
430          * Parameters for MT29F8G08AAD
431          */
432         pmc_periphEnable(PIOA_ID);
433         pmc_periphEnable(PIOC_ID);
434         pmc_periphEnable(PIOD_ID);
435
436         PIO_PERIPH_SEL(PIOA_BASE, MT29F_PINS_PORTA, MT29F_PERIPH_PORTA);
437         PIOA_PDR = MT29F_PINS_PORTA;
438         PIOA_PUER = MT29F_PINS_PORTA;
439
440         PIO_PERIPH_SEL(PIOC_BASE, MT29F_PINS_PORTC, MT29F_PERIPH_PORTC);
441         PIOC_PDR = MT29F_PINS_PORTC;
442         PIOC_PUER = MT29F_PINS_PORTC;
443
444         PIO_PERIPH_SEL(PIOD_BASE, MT29F_PINS_PORTD, MT29F_PERIPH_PORTD);
445         PIOD_PDR = MT29F_PINS_PORTD;
446         PIOD_PUER = MT29F_PINS_PORTD;
447
448     pmc_periphEnable(SMC_SDRAMC_ID);
449 }
450
451
452 static void initSmc(void)
453 {
454     SMC_SETUP0 = SMC_SETUP_NWE_SETUP(0)
455                 | SMC_SETUP_NCS_WR_SETUP(0)
456                 | SMC_SETUP_NRD_SETUP(0)
457                 | SMC_SETUP_NCS_RD_SETUP(0);
458
459     SMC_PULSE0 = SMC_PULSE_NWE_PULSE(2)
460                 | SMC_PULSE_NCS_WR_PULSE(3)
461                 | SMC_PULSE_NRD_PULSE(2)
462                 | SMC_PULSE_NCS_RD_PULSE(3);
463
464     SMC_CYCLE0 = SMC_CYCLE_NWE_CYCLE(3)
465                 | SMC_CYCLE_NRD_CYCLE(3);
466
467     SMC_TIMINGS0 = SMC_TIMINGS_TCLR(1)
468                 | SMC_TIMINGS_TADL(6)
469                 | SMC_TIMINGS_TAR(4)
470                 | SMC_TIMINGS_TRR(2)
471                 | SMC_TIMINGS_TWB(9)
472                 | SMC_TIMINGS_RBNSEL(7)
473                 | SMC_TIMINGS_NFSEL;
474
475     SMC_MODE0 = SMC_MODE_READ_MODE
476                 | SMC_MODE_WRITE_MODE;
477
478         SMC_CFG = SMC_CFG_PAGESIZE_PS2048_64
479                 | SMC_CFG_EDGECTRL
480                 | SMC_CFG_DTOMUL_X1048576
481                 | SMC_CFG_DTOCYC(0xF)
482                 | SMC_CFG_WSPARE
483                 | SMC_CFG_RSPARE;
484
485         // Disable SMC interrupts, reset and enable NFC controller
486         SMC_IDR = ~0;
487         SMC_CTRL = 0;
488         SMC_CTRL = SMC_CTRL_NFCEN;
489
490         // Enable ECC, 1 ECC per 256 bytes
491         SMC_ECC_CTRL = SMC_ECC_CTRL_SWRST;
492         SMC_ECC_MD = SMC_ECC_MD_ECC_PAGESIZE_PS2048_64 | SMC_ECC_MD_TYPCORREC_C256B;
493 }
494
495
496 void mt29f_init(Mt29f *chip, uint8_t chip_select)
497 {
498         memset(chip, 0, sizeof(Mt29f));
499
500         chip->chip_select = chip_select;
501
502         initPio();
503         initSmc();
504         chipReset(chip);
505 }
506