nand driver: improve comments and docs and shuffle around some define(s).
[bertos.git] / bertos / drv / nand.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 * \brief ONFI 1.0 compliant NAND kblock driver
33 *
34 * Defective blocks are remapped in a reserved area of configurable size
35 * at the bottom of the NAND.
36 * At the moment there is no wear-leveling block translation: kblock's blocks
37 * are mapped directly on NAND erase blocks: when a (k)block is written the
38 * corresponding erase block is erased and all pages within are rewritten.
39 * Partial write is not possible: it's recommended to use buffered mode.
40 *
41 * The driver needs to format the NAND before use. If the initialization code
42 * detects a fresh memory it does a bad block scan and a formatting.
43 * Format info isn't stored in NAND in a global structure: each block has its
44 * info written in the spare area of its first page.  These info contais a tag
45 * to detect formatted blocks and an index for bad block remapping (struct
46 * RemapInfo).
47 *
48 * The ECC for each page is written in the spare area too.
49 *
50 * Works only in 8 bit data mode and NAND parameters are not
51 * detected at run-time, but hand-configured in cfg_nand.h.
52 *
53 * Heap is needed to allocate the tipically large buffer necessary
54 * to erase and write a block.
55 *
56 * \author Stefano Fedrigo <aleph@develer.com>
57 */
58
59 #include "nand.h"
60 #include <cfg/log.h>
61 #include <struct/heap.h>
62 #include <string.h> // memset
63
64
65 /*
66  * Remap info written in the first page of each block.
67  *
68  * This structure is used in blocks of the reserved area to store
69  * which block the block containing the structure is remapping.
70  * It's stored in all other blocks too to mark a formatted block.
71  * In this case the member mapped_blk has non meaning.
72  */
73 struct RemapInfo
74 {
75         uint32_t tag;         // Magic number to detect valid info
76         uint16_t mapped_blk;  // Bad block the block containing this info is remapping
77 };
78
79 // Where RemapInfo is stored in the spare area
80 #define NAND_REMAP_TAG_OFFSET  (CONFIG_NAND_SPARE_SIZE - sizeof(struct RemapInfo))
81
82 // Fixed tag to detect RemapInfo
83 #define NAND_REMAP_TAG         0x3e10c8ed
84
85 /*
86  * Number of ECC words computed for a page.
87  *
88  * For 2048 bytes pages and 1 ECC word each 256 bytes,
89  * 24 bytes of ECC data are stored.
90  */
91 #define NAND_ECC_NWORDS        (CONFIG_NAND_DATA_SIZE / 256)
92
93 // Total page size (user data + spare) in bytes
94 #define NAND_PAGE_SIZE         (CONFIG_NAND_DATA_SIZE + CONFIG_NAND_SPARE_SIZE)
95
96 // Erase block size in bytes
97 #define NAND_BLOCK_SIZE        (CONFIG_NAND_DATA_SIZE * CONFIG_NAND_PAGES_PER_BLOCK)
98
99 // Number of usable blocks, and index of first remapping block
100 #define NAND_NUM_USER_BLOCKS   (CONFIG_NAND_NUM_BLOCK - CONFIG_NAND_NUM_REMAP_BLOCKS)
101
102 // ONFI NAND status codes
103 #define NAND_STATUS_READY  BV(6)
104 #define NAND_STATUS_ERROR  BV(0)
105
106
107 // Get block from page
108 #define PAGE(blk)            ((blk) * CONFIG_NAND_PAGES_PER_BLOCK)
109
110 // Page from block and page in block
111 #define BLOCK(page)          ((uint16_t)((page) / CONFIG_NAND_PAGES_PER_BLOCK))
112 #define PAGE_IN_BLOCK(page)  ((uint16_t)((page) % CONFIG_NAND_PAGES_PER_BLOCK))
113
114
115 /*
116  * Translate page index plus a byte offset
117  * in the five address cycles format needed by NAND.
118  *
119  * Cycles in x8 mode.
120  * CA = column addr, PA = page addr, BA = block addr
121  *
122  * Cycle    I/O7  I/O6  I/O5  I/O4  I/O3  I/O2  I/O1  I/O0
123  * -------------------------------------------------------
124  * First    CA7   CA6   CA5   CA4   CA3   CA2   CA1   CA0
125  * Second   LOW   LOW   LOW   LOW   CA11  CA10  CA9   CA8
126  * Third    BA7   BA6   PA5   PA4   PA3   PA2   PA1   PA0
127  * Fourth   BA15  BA14  BA13  BA12  BA11  BA10  BA9   BA8
128  * Fifth    LOW   LOW   LOW   LOW   LOW   LOW   LOW   BA16
129  */
130 static void getAddrCycles(uint32_t page, uint16_t offset, uint32_t *cycle0, uint32_t *cycle1234)
131 {
132         ASSERT(offset < NAND_PAGE_SIZE);
133
134         *cycle0 = offset & 0xff;
135         *cycle1234 = (page << 8) | ((offset >> 8) & 0xf);
136
137         //LOG_INFO("nand addr: %lx %lx\n", *cycle1234, *cycle0);
138 }
139
140
141 static void chipReset(Nand *chip)
142 {
143         nand_sendCommand(chip, NAND_CMD_RESET, 0, 0, 0, 0);
144         nand_waitReadyBusy(chip, CONFIG_NAND_TMOUT);
145 }
146
147
148 static bool isOperationComplete(Nand *chip)
149 {
150         uint8_t status;
151
152         nand_sendCommand(chip, NAND_CMD_STATUS, 0, 0, 0, 0);
153
154         status = nand_getChipStatus(chip);
155         return (status & NAND_STATUS_READY) && !(status & NAND_STATUS_ERROR);
156 }
157
158
159 /**
160  * Erase the whole block.
161  */
162 int nand_blockErase(Nand *chip, uint16_t block)
163 {
164         uint32_t cycle0;
165         uint32_t cycle1234;
166
167         uint16_t remapped_block = chip->block_map[block];
168         if (block != remapped_block)
169         {
170                 LOG_INFO("nand_blockErase: remapped block: blk %d->%d\n", block, remapped_block);
171                 block = remapped_block;
172         }
173
174         getAddrCycles(PAGE(block), 0, &cycle0, &cycle1234);
175
176         nand_sendCommand(chip, NAND_CMD_ERASE_1, NAND_CMD_ERASE_2, 3, 0, cycle1234 >> 8);
177
178         nand_waitReadyBusy(chip, CONFIG_NAND_TMOUT);
179
180         if (!isOperationComplete(chip))
181         {
182                 LOG_ERR("nand: error erasing block\n");
183                 chip->status |= NAND_ERR_ERASE;
184                 return -1;
185         }
186
187         return 0;
188 }
189
190
191 /**
192  * Read Device ID and configuration codes.
193  */
194 bool nand_getDevId(Nand *chip, uint8_t dev_id[5])
195 {
196         nand_sendCommand(chip, NAND_CMD_READID, 0, 1, 0, 0);
197
198         nand_waitReadyBusy(chip, CONFIG_NAND_TMOUT);
199         if (!nand_waitTransferComplete(chip, CONFIG_NAND_TMOUT))
200         {
201                 LOG_ERR("nand: getDevId timeout\n");
202                 chip->status |= NAND_ERR_RD_TMOUT;
203                 return false;
204         }
205
206         memcpy(dev_id, nand_dataBuffer(chip), sizeof(dev_id));
207         return true;
208 }
209
210
211 static bool nand_readPage(Nand *chip, uint32_t page, uint16_t offset)
212 {
213         uint32_t cycle0;
214         uint32_t cycle1234;
215
216         //LOG_INFO("nand_readPage: page 0x%lx off 0x%x\n", page, offset);
217
218         getAddrCycles(page, offset, &cycle0, &cycle1234);
219
220         nand_sendCommand(chip, NAND_CMD_READ_1, NAND_CMD_READ_2, 5, cycle0, cycle1234);
221
222         nand_waitReadyBusy(chip, CONFIG_NAND_TMOUT);
223         if (!nand_waitTransferComplete(chip, CONFIG_NAND_TMOUT))
224         {
225                 LOG_ERR("nand: read timeout\n");
226                 chip->status |= NAND_ERR_RD_TMOUT;
227                 return false;
228         }
229
230         return true;
231 }
232
233
234 /*
235  * Read page data and ECC, checking for errors.
236  * TODO: fix errors with ECC when possible.
237  */
238 static bool nand_read(Nand *chip, uint32_t page, void *buf, uint16_t offset, uint16_t size)
239 {
240         struct RemapInfo remap_info;
241         uint32_t remapped_page = PAGE(chip->block_map[BLOCK(page)]) + PAGE_IN_BLOCK(page);
242
243         //LOG_INFO("nand_read: page=%ld, offset=%d, size=%d\n", page, offset, size);
244
245         if (page != remapped_page)
246         {
247                 LOG_INFO("nand_read: remapped block: blk %d->%d, pg %ld->%ld\n",
248                                 BLOCK(page), chip->block_map[BLOCK(page)], page, remapped_page);
249                 page = remapped_page;
250         }
251
252         if (!nand_readPage(chip, page, 0))
253                 return false;
254
255         memcpy(buf, (char *)nand_dataBuffer(chip) + offset, size);
256
257         /*
258          * Check for ECC hardware status only if a valid RemapInfo structure is found.
259          * That guarantees the page is written by us and a valid ECC is present.
260          */
261         memcpy(&remap_info, (char *)buf + NAND_REMAP_TAG_OFFSET, sizeof(remap_info));
262         if (remap_info.tag == NAND_REMAP_TAG && !nand_checkEcc(chip))
263         {
264                 chip->status |= NAND_ERR_ECC;
265                 return false;
266         }
267         else
268                 return true;
269 }
270
271
272 /*
273  * Write data stored in nand_dataBuffer() to a NAND page, starting at a given offset.
274  * Usually offset will be 0 to write data or CONFIG_NAND_DATA_SIZE to write the spare
275  * area.
276  */
277 static bool nand_writePage(Nand *chip, uint32_t page, uint16_t offset)
278 {
279         uint32_t cycle0;
280         uint32_t cycle1234;
281
282         //LOG_INFO("nand_writePage: page 0x%lx off 0x%x\n", page, offset);
283
284         getAddrCycles(page, offset, &cycle0, &cycle1234);
285
286         nand_sendCommand(chip, NAND_CMD_WRITE_1, 0, 5, cycle0, cycle1234);
287
288         if (!nand_waitTransferComplete(chip, CONFIG_NAND_TMOUT))
289         {
290                 LOG_ERR("nand: write timeout\n");
291                 chip->status |= NAND_ERR_WR_TMOUT;
292                 return false;
293         }
294
295         nand_sendCommand(chip, NAND_CMD_WRITE_2, 0, 0, 0, 0);
296
297         nand_waitReadyBusy(chip, CONFIG_NAND_TMOUT);
298
299         if (!isOperationComplete(chip))
300         {
301                 LOG_ERR("nand: error writing page\n");
302                 chip->status |= NAND_ERR_WRITE;
303                 return false;
304         }
305
306         return true;
307 }
308
309
310 /*
311  * Write data, ECC and remap block info.
312  *
313  * \param page           the page to be written
314  * \parma original_page  if different from page, it's the page that's being remapped
315  *
316  * Implementation note for SAM3 NFC controller:
317  * according to datasheet to get ECC computed by hardware is sufficient
318  * to write the main area.  But it seems that in that way the last ECC_PR
319  * register is not generated.  The workaround is to write data and dummy (ff)
320  * spare data in one write, at this point the last ECC_PR is correct and
321  * ECC data can be written in the spare area with a second program operation.
322  */
323 static bool nand_write(Nand *chip, uint32_t page, const void *buf, size_t size)
324 {
325         struct RemapInfo remap_info;
326         uint32_t *nand_buf = (uint32_t *)nand_dataBuffer(chip);
327         uint32_t remapped_page = PAGE(chip->block_map[BLOCK(page)]) + PAGE_IN_BLOCK(page);
328
329         ASSERT(size <= CONFIG_NAND_DATA_SIZE);
330
331         if (page != remapped_page)
332                 LOG_INFO("nand_write: remapped block: blk %d->%d, pg %ld->%ld\n",
333                                 BLOCK(page), chip->block_map[BLOCK(page)], page, remapped_page);
334
335         // Data
336         memset(nand_buf, 0xff, NAND_PAGE_SIZE);
337         memcpy(nand_buf, buf, size);
338         if (!nand_writePage(chip, remapped_page, 0))
339                 return false;
340
341         // ECC
342         memset(nand_buf, 0xff, CONFIG_NAND_SPARE_SIZE);
343         nand_computeEcc(chip, buf, size, nand_buf, NAND_ECC_NWORDS);
344
345         // Remap info
346         remap_info.tag = NAND_REMAP_TAG;
347         remap_info.mapped_blk = BLOCK(page);
348         memcpy((char *)nand_buf + NAND_REMAP_TAG_OFFSET, &remap_info, sizeof(remap_info));
349
350         return nand_writePage(chip, remapped_page, CONFIG_NAND_DATA_SIZE);
351 }
352
353
354 /*
355  * Check if the given block is marked bad: ONFI standard mandates
356  * that bad block are marked with "00" bytes on the spare area of the
357  * first page in block.
358  */
359 static bool blockIsGood(Nand *chip, uint16_t blk)
360 {
361         uint8_t *first_byte = (uint8_t *)nand_dataBuffer(chip);
362         bool good;
363
364         // Check first byte in spare area of first page in block
365         nand_readPage(chip, PAGE(blk), CONFIG_NAND_DATA_SIZE);
366         good = *first_byte != 0;
367
368         if (!good)
369                 LOG_INFO("nand: bad block %d\n", blk);
370
371         return good;
372 }
373
374
375 /*
376  * Return the main partition block remapped on given block in the remap
377  * partition (dest_blk).
378  */
379 static int getBadBlockFromRemapBlock(Nand *chip, uint16_t dest_blk)
380 {
381         struct RemapInfo *remap_info = (struct RemapInfo *)nand_dataBuffer(chip);
382
383         if (!nand_readPage(chip, PAGE(dest_blk), CONFIG_NAND_DATA_SIZE + NAND_REMAP_TAG_OFFSET))
384                 return -1;
385
386         if (remap_info->tag == NAND_REMAP_TAG)
387                 return remap_info->mapped_blk;
388         else
389                 return -1;
390 }
391
392
393 /*
394  * Set a block remapping: src_blk (a block in main data partition) is remapped
395  * on dest_blk (block in reserved remapped blocks partition).
396  */
397 static bool setMapping(Nand *chip, uint32_t src_blk, uint32_t dest_blk)
398 {
399         struct RemapInfo *remap_info = (struct RemapInfo *)nand_dataBuffer(chip);
400
401         LOG_INFO("nand, setMapping(): src=%ld dst=%ld\n", src_blk, dest_blk);
402
403         if (!nand_readPage(chip, PAGE(dest_blk), CONFIG_NAND_DATA_SIZE + NAND_REMAP_TAG_OFFSET))
404                 return false;
405
406         remap_info->tag = NAND_REMAP_TAG;
407         remap_info->mapped_blk = src_blk;
408
409         return nand_writePage(chip, PAGE(dest_blk), CONFIG_NAND_DATA_SIZE + NAND_REMAP_TAG_OFFSET);
410 }
411
412
413 /*
414  * Get a new block from the remap partition to use as a substitute
415  * for a bad block.
416  */
417 static uint16_t getFreeRemapBlock(Nand *chip)
418 {
419         int blk;
420
421         for (blk = chip->remap_start; blk < CONFIG_NAND_NUM_BLOCK; blk++)
422         {
423                 if (blockIsGood(chip, blk))
424                 {
425                         chip->remap_start = blk + 1;
426                         return blk;
427                 }
428         }
429
430         LOG_ERR("nand: reserved blocks for bad block remapping exhausted!\n");
431         return 0;
432 }
433
434
435 /*
436  * Check if NAND is initialized.
437  */
438 static bool chipIsMarked(Nand *chip)
439 {
440         return getBadBlockFromRemapBlock(chip, NAND_NUM_USER_BLOCKS) != -1;
441 }
442
443
444 /*
445  * Initialize NAND (format). Scan NAND for factory marked bad blocks.
446  * All found bad blocks are remapped to the remap partition: each
447  * block in the remap partition used to remap bad blocks is marked.
448  */
449 static void initBlockMap(Nand *chip)
450 {
451         int b, last;
452
453         // Default is for each block to not be remapped
454         for (b = 0; b < CONFIG_NAND_NUM_BLOCK; b++)
455                 chip->block_map[b] = b;
456         chip->remap_start = NAND_NUM_USER_BLOCKS;
457
458         if (chipIsMarked(chip))
459         {
460                 LOG_INFO("nand: found initialized NAND, searching for remapped blocks\n");
461
462                 // Scan for assigned blocks in remap area
463                 for (b = last = NAND_NUM_USER_BLOCKS; b < CONFIG_NAND_NUM_BLOCK; b++)
464                 {
465                         int remapped_blk = getBadBlockFromRemapBlock(chip, b);
466                         if (remapped_blk != -1 && remapped_blk != b)
467                         {
468                                 LOG_INFO("nand: found remapped block %d->%d\n", remapped_blk, b);
469                                 chip->block_map[remapped_blk] = b;
470                                 last = b + 1;
471                         }
472                 }
473                 chip->remap_start = last;
474         }
475         else
476         {
477                 bool remapped_anything = false;
478
479                 LOG_INFO("nand: found new NAND, searching for bad blocks\n");
480
481                 for (b = 0; b < NAND_NUM_USER_BLOCKS; b++)
482                 {
483                         if (!blockIsGood(chip, b))
484                         {
485                                 chip->block_map[b] = getFreeRemapBlock(chip);
486                                 setMapping(chip, b, chip->block_map[b]);
487                                 remapped_anything = true;
488                                 LOG_INFO("nand: found new bad block %d, remapped to %d\n", b, chip->block_map[b]);
489                         }
490                 }
491
492                 /*
493              * If no bad blocks are found (we're lucky!) write anyway a dummy
494                  * remap to mark NAND and detect we already scanned it next time.
495                  */
496                 if (!remapped_anything)
497                 {
498                         setMapping(chip, NAND_NUM_USER_BLOCKS, NAND_NUM_USER_BLOCKS);
499                         LOG_INFO("nand: no bad block founds, marked NAND\n");
500                 }
501         }
502 }
503
504
505 /**
506  * Reset bad blocks map and erase all blocks.
507  *
508  * \note DON'T USE on production chips: this function will try to erase
509  *       factory marked bad blocks too.
510  */
511 void nand_format(Nand *chip)
512 {
513         int b;
514
515         for (b = 0; b < CONFIG_NAND_NUM_BLOCK; b++)
516         {
517                 LOG_INFO("nand: erasing block %d\n", b);
518                 chip->block_map[b] = b;
519                 nand_blockErase(chip, b);
520         }
521         chip->remap_start = NAND_NUM_USER_BLOCKS;
522 }
523
524 #ifdef _DEBUG
525
526 /*
527  * Create some bad blocks, erasing them and writing the bad block mark.
528  */
529 void nand_ruinSomeBlocks(Nand *chip)
530 {
531         int bads[] = { 7, 99, 555, 1003, 1004, 1432 };
532         unsigned i;
533
534         LOG_INFO("nand: erasing mark\n");
535         nand_blockErase(chip, NAND_NUM_USER_BLOCKS);
536
537         for (i = 0; i < countof(bads); i++)
538         {
539                 LOG_INFO("nand: erasing block %d\n", bads[i]);
540                 nand_blockErase(chip, bads[i]);
541
542                 LOG_INFO("nand: marking page %d as bad\n", PAGE(bads[i]));
543                 memset(nand_dataBuffer(chip), 0, CONFIG_NAND_SPARE_SIZE);
544                 nand_writePage(chip, PAGE(bads[i]), CONFIG_NAND_DATA_SIZE);
545         }
546 }
547
548 #endif
549
550 static bool commonInit(Nand *chip, struct Heap *heap, unsigned chip_select)
551 {
552         memset(chip, 0, sizeof(Nand));
553
554         DB(chip->fd.priv.type = KBT_NAND);
555         chip->fd.blk_size = NAND_BLOCK_SIZE;
556         chip->fd.blk_cnt  = NAND_NUM_USER_BLOCKS;
557
558         chip->chip_select = chip_select;
559         chip->block_map = heap_allocmem(heap, CONFIG_NAND_NUM_BLOCK * sizeof(*chip->block_map));
560         if (!chip->block_map)
561         {
562                 LOG_ERR("nand: error allocating block map\n");
563                 return false;
564         }
565
566         nand_hwInit(chip);
567         chipReset(chip);
568         initBlockMap(chip);
569
570         return true;
571 }
572
573
574 /**************** Kblock interface ****************/
575
576
577 static size_t nand_writeDirect(struct KBlock *kblk, block_idx_t idx, const void *buf, size_t offset, size_t size)
578 {
579         ASSERT(offset <= NAND_BLOCK_SIZE);
580         ASSERT(offset % CONFIG_NAND_DATA_SIZE == 0);
581         ASSERT(size <= NAND_BLOCK_SIZE);
582         ASSERT(size % CONFIG_NAND_DATA_SIZE == 0);
583
584         //LOG_INFO("nand_writeDirect: idx=%ld offset=%d size=%d\n", idx, offset, size);
585
586         nand_blockErase(NAND_CAST(kblk), idx);
587
588         while (offset < size)
589         {
590                 uint32_t page = PAGE(idx) + (offset / CONFIG_NAND_DATA_SIZE);
591
592                 if (!nand_write(NAND_CAST(kblk), page, buf, CONFIG_NAND_DATA_SIZE))
593                         break;
594
595                 offset += CONFIG_NAND_DATA_SIZE;
596                 buf = (const char *)buf + CONFIG_NAND_DATA_SIZE;
597         }
598
599         return offset;
600 }
601
602
603 static size_t nand_readDirect(struct KBlock *kblk, block_idx_t idx, void *buf, size_t offset, size_t size)
604 {
605         uint32_t page;
606         size_t   read_size;
607         size_t   read_offset;
608         size_t   nread = 0;
609
610         ASSERT(offset < NAND_BLOCK_SIZE);
611         ASSERT(size <= NAND_BLOCK_SIZE);
612
613         //LOG_INFO("nand_readDirect: idx=%ld offset=%d size=%d\n", idx, offset, size);
614
615         while (nread < size)
616         {
617                 page        = PAGE(idx) + (offset / CONFIG_NAND_DATA_SIZE);
618                 read_offset = offset % CONFIG_NAND_DATA_SIZE;
619                 read_size   = MIN(size, CONFIG_NAND_DATA_SIZE - read_offset);
620
621                 if (!nand_read(NAND_CAST(kblk), page, (char *)buf + nread, read_offset, read_size))
622                         break;
623
624                 offset += read_size;
625                 nread  += read_size;
626         }
627
628         return nread;
629 }
630
631
632 static int nand_error(struct KBlock *kblk)
633 {
634         Nand *chip = NAND_CAST(kblk);
635         return chip->status;
636 }
637
638
639 static void nand_clearError(struct KBlock *kblk)
640 {
641         Nand *chip = NAND_CAST(kblk);
642         chip->status = 0;
643 }
644
645
646 static const KBlockVTable nand_buffered_vt =
647 {
648         .readDirect = nand_readDirect,
649         .writeDirect = nand_writeDirect,
650
651         .readBuf = kblock_swReadBuf,
652         .writeBuf = kblock_swWriteBuf,
653         .load = kblock_swLoad,
654         .store = kblock_swStore,
655
656         .error = nand_error,
657         .clearerr = nand_clearError,
658 };
659
660 static const KBlockVTable nand_unbuffered_vt =
661 {
662         .readDirect = nand_readDirect,
663         .writeDirect = nand_writeDirect,
664
665         .error = nand_error,
666         .clearerr = nand_clearError,
667 };
668
669
670 /**
671  * Initialize NAND kblock driver in buffered mode.
672  */
673 bool nand_init(Nand *chip, struct Heap *heap, unsigned chip_select)
674 {
675         if (!commonInit(chip, heap, chip_select))
676                 return false;
677
678         chip->fd.priv.vt = &nand_buffered_vt;
679         chip->fd.priv.flags |= KB_BUFFERED;
680
681         chip->fd.priv.buf = heap_allocmem(heap, NAND_BLOCK_SIZE);
682         if (!chip->fd.priv.buf)
683         {
684                 LOG_ERR("nand: error allocating block buffer\n");
685                 return false;
686         }
687
688         // Load the first block in the cache
689         return nand_readDirect(&chip->fd, 0, chip->fd.priv.buf, 0, chip->fd.blk_size);
690 }
691
692
693 /**
694  * Initialize NAND kblock driver in unbuffered mode.
695  */
696 bool nand_initUnbuffered(Nand *chip, struct Heap *heap, unsigned chip_select)
697 {
698         if (!commonInit(chip, heap, chip_select))
699                 return false;
700
701         chip->fd.priv.vt = &nand_unbuffered_vt;
702         return true;
703 }
704