mt29f NAND driver: remove kblock interface. A very simple FTL layer
[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, size_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 size_t mt29f_pageRead(Mt29f *chip, uint32_t page, void *buf, size_t offset, size_t size)
261 {
262         uint32_t cycle0;
263         uint32_t cycle1234;
264
265         ASSERT(offset == 0);
266
267         LOG_INFO("mt29f_pageRead\n");
268
269         getAddrCycles(page, 0, &cycle0, &cycle1234);
270
271         sendCommand(
272                 NFC_CMD_NFCCMD | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_FIVE | NFC_CMD_VCMD2 |
273                 (MT29F_CMD_READ_2 << 10) | (MT29F_CMD_READ_1 << 2),
274                 5, cycle0, cycle1234);
275
276         waitReadyBusy();
277         if (!waitTransferComplete())
278         {
279                 LOG_ERR("mt29f: read timeout\n");
280                 chip->status |= MT29F_ERR_RD_TMOUT;
281                 return 0;
282         }
283
284         memcpy(buf, (void *)NFC_SRAM_BASE_ADDR, size);
285
286         return size;
287 }
288
289
290 size_t mt29f_pageWrite(Mt29f *chip, uint32_t page, const void *buf, size_t offset, size_t size)
291 {
292         uint32_t cycle0;
293         uint32_t cycle1234;
294
295         ASSERT(offset == 0);
296
297         LOG_INFO("mt29f_pageWrite\n");
298
299         memcpy((void *)NFC_SRAM_BASE_ADDR, buf, size);
300
301         getAddrCycles(page, 0, &cycle0, &cycle1234);
302
303         sendCommand(
304                         NFC_CMD_NFCCMD | NFC_CMD_NFCWR | NFC_CMD_NFCEN | MT29F_CSID | NFC_CMD_ACYCLE_FIVE |
305                         MT29F_CMD_WRITE_1 << 2,
306                         5, cycle0, cycle1234);
307
308         if (!waitTransferComplete())
309         {
310                 LOG_ERR("mt29f: write timeout\n");
311                 chip->status |= MT29F_ERR_WR_TMOUT;
312                 return 0;
313         }
314
315         sendCommand(
316                         NFC_CMD_NFCCMD | MT29F_CSID | NFC_CMD_ACYCLE_NONE |
317                         MT29F_CMD_WRITE_2 << 2,
318                         0, 0, 0);
319
320         waitReadyBusy();
321
322         if (!isOperationComplete())
323         {
324                 LOG_ERR("mt29f: error writing page\n");
325                 chip->status |= MT29F_ERR_WRITE;
326                 return 0;
327         }
328
329         return size;
330 }
331
332
333 int mt29f_error(Mt29f *chip)
334 {
335         return chip->status;
336 }
337
338
339 void mt29f_clearError(Mt29f *chip)
340 {
341         chip->status = 0;
342 }
343
344
345 static void initPio(Mt29f *chip)
346 {
347         /*
348          * TODO: put following stuff in hw_ file dependent (and configurable cs?)
349          * Parameters for MT29F8G08AAD
350          */
351         pmc_periphEnable(PIOA_ID);
352         pmc_periphEnable(PIOC_ID);
353         pmc_periphEnable(PIOD_ID);
354
355         PIO_PERIPH_SEL(PIOA_BASE, MT29F_PINS_PORTA, MT29F_PERIPH_PORTA);
356         PIOA_PDR = MT29F_PINS_PORTA;
357         PIOA_PUER = MT29F_PINS_PORTA;
358
359         PIO_PERIPH_SEL(PIOC_BASE, MT29F_PINS_PORTC, MT29F_PERIPH_PORTC);
360         PIOC_PDR = MT29F_PINS_PORTC;
361         PIOC_PUER = MT29F_PINS_PORTC;
362
363         PIO_PERIPH_SEL(PIOD_BASE, MT29F_PINS_PORTD, MT29F_PERIPH_PORTD);
364         PIOD_PDR = MT29F_PINS_PORTD;
365         PIOD_PUER = MT29F_PINS_PORTD;
366
367     pmc_periphEnable(SMC_SDRAMC_ID);
368 }
369
370
371 static void initSmc(Mt29f *chip)
372 {
373     SMC_SETUP0 = SMC_SETUP_NWE_SETUP(0)
374                 | SMC_SETUP_NCS_WR_SETUP(0)
375                 | SMC_SETUP_NRD_SETUP(0)
376                 | SMC_SETUP_NCS_RD_SETUP(0);
377
378     SMC_PULSE0 = SMC_PULSE_NWE_PULSE(2)
379                 | SMC_PULSE_NCS_WR_PULSE(3)
380                 | SMC_PULSE_NRD_PULSE(2)
381                 | SMC_PULSE_NCS_RD_PULSE(3);
382
383     SMC_CYCLE0 = SMC_CYCLE_NWE_CYCLE(3)
384                 | SMC_CYCLE_NRD_CYCLE(3);
385
386     SMC_TIMINGS0 = SMC_TIMINGS_TCLR(1)
387                 | SMC_TIMINGS_TADL(6)
388                 | SMC_TIMINGS_TAR(4)
389                 | SMC_TIMINGS_TRR(2)
390                 | SMC_TIMINGS_TWB(9)
391                 | SMC_TIMINGS_RBNSEL(7)
392                 | SMC_TIMINGS_NFSEL;
393
394     SMC_MODE0 = SMC_MODE_READ_MODE
395                 | SMC_MODE_WRITE_MODE;
396
397         SMC_CFG = SMC_CFG_PAGESIZE_PS2048_64
398                 | SMC_CFG_EDGECTRL
399                 | SMC_CFG_DTOMUL_X1048576
400                 | SMC_CFG_DTOCYC(0xF);
401
402         // Disable SMC interrupts, reset and enable NFC controller
403         SMC_IDR = ~0;
404         SMC_CTRL = 0;
405         SMC_CTRL = SMC_CTRL_NFCEN;
406
407         // Enable ECC, 1 ECC per 256 bytes
408         SMC_ECC_CTRL = SMC_ECC_CTRL_SWRST;
409         SMC_ECC_MD = SMC_ECC_MD_ECC_PAGESIZE_PS2048_64 | SMC_ECC_MD_TYPCORREC_C256B;
410 }
411
412
413 void mt29f_init(Mt29f *chip)
414 {
415         initPio(chip);
416         initSmc(chip);
417
418         mt29f_clearError(chip);
419
420         chipReset();
421 }
422