2 * NAND Flash Controller Device Driver
3 * Copyright (c) 2009, Intel Corporation and its suppliers.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <linux/slab.h>
22 #include <linux/mtd/mtd.h>
31 #define GLOB_LLD_PAGES 64
32 #define GLOB_LLD_PAGE_SIZE (512+16)
33 #define GLOB_LLD_PAGE_DATA_SIZE 512
34 #define GLOB_LLD_BLOCKS 2048
39 u32 valid_banks[MAX_CHANS];
42 static struct mtd_info *spectra_mtd;
43 static int mtddev = -1;
44 module_param(mtddev, int, 0);
46 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
47 * Function: mtd_Flash_Init
49 * Outputs: PASS=0 (notice 0=ok here)
50 * Description: Creates & initializes the flash RAM array.
52 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
53 u16 mtd_Flash_Init(void)
56 printk(KERN_ERR "No MTD device specified. Give mtddev parameter\n");
60 spectra_mtd = get_mtd_device(NULL, mtddev);
62 printk(KERN_ERR "Failed to obtain MTD device #%d\n", mtddev);
66 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
67 __FILE__, __LINE__, __func__);
72 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
73 * Function: mtd_Flash_Release
75 * Outputs: PASS=0 (notice 0=ok here)
76 * Description: Releases the flash.
78 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
79 int mtd_Flash_Release(void)
81 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
82 __FILE__, __LINE__, __func__);
86 put_mtd_device(spectra_mtd);
92 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
93 * Function: mtd_Read_Device_ID
95 * Outputs: PASS=1 FAIL=0
96 * Description: Reads the info from the controller registers.
97 * Sets up DeviceInfo structure with device parameters
98 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
100 u16 mtd_Read_Device_ID(void)
103 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
104 __FILE__, __LINE__, __func__);
109 DeviceInfo.wDeviceMaker = 0;
110 DeviceInfo.wDeviceType = 8;
111 DeviceInfo.wSpectraStartBlock = SPECTRA_START_BLOCK;
112 tmp = spectra_mtd->size;
113 do_div(tmp, spectra_mtd->erasesize);
114 DeviceInfo.wTotalBlocks = tmp;
115 DeviceInfo.wSpectraEndBlock = DeviceInfo.wTotalBlocks - 1;
116 DeviceInfo.wPagesPerBlock = spectra_mtd->erasesize / spectra_mtd->writesize;
117 DeviceInfo.wPageSize = spectra_mtd->writesize + spectra_mtd->oobsize;
118 DeviceInfo.wPageDataSize = spectra_mtd->writesize;
119 DeviceInfo.wPageSpareSize = spectra_mtd->oobsize;
120 DeviceInfo.wBlockSize = DeviceInfo.wPageSize * DeviceInfo.wPagesPerBlock;
121 DeviceInfo.wBlockDataSize = DeviceInfo.wPageDataSize * DeviceInfo.wPagesPerBlock;
122 DeviceInfo.wDataBlockNum = (u32) (DeviceInfo.wSpectraEndBlock -
123 DeviceInfo.wSpectraStartBlock
125 DeviceInfo.MLCDevice = 0;//spectra_mtd->celltype & NAND_CI_CELLTYPE_MSK;
126 DeviceInfo.nBitsInPageNumber =
127 (u8)GLOB_Calc_Used_Bits(DeviceInfo.wPagesPerBlock);
128 DeviceInfo.nBitsInPageDataSize =
129 (u8)GLOB_Calc_Used_Bits(DeviceInfo.wPageDataSize);
130 DeviceInfo.nBitsInBlockDataSize =
131 (u8)GLOB_Calc_Used_Bits(DeviceInfo.wBlockDataSize);
144 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
145 * Function: mtd_Flash_Reset
147 * Outputs: PASS=0 (notice 0=ok here)
148 * Description: Reset the flash
150 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
151 u16 mtd_Flash_Reset(void)
153 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
154 __FILE__, __LINE__, __func__);
159 void erase_callback(struct erase_info *e)
161 complete((void *)e->priv);
164 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
165 * Function: mtd_Erase_Block
167 * Outputs: PASS=0 (notice 0=ok here)
168 * Description: Erase a block
170 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
171 u16 mtd_Erase_Block(u32 block_add)
173 struct erase_info erase;
174 DECLARE_COMPLETION_ONSTACK(comp);
177 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
178 __FILE__, __LINE__, __func__);
180 if (block_add >= DeviceInfo.wTotalBlocks) {
181 printk(KERN_ERR "mtd_Erase_Block error! "
182 "Too big block address: %d\n", block_add);
186 nand_dbg_print(NAND_DBG_DEBUG, "Erasing block %d\n",
189 erase.mtd = spectra_mtd;
190 erase.callback = erase_callback;
191 erase.addr = block_add * spectra_mtd->erasesize;
192 erase.len = spectra_mtd->erasesize;
193 erase.priv = (unsigned long)∁
195 ret = spectra_mtd->erase(spectra_mtd, &erase);
197 wait_for_completion(&comp);
198 if (erase.state != MTD_ERASE_DONE)
202 printk(KERN_WARNING "mtd_Erase_Block error! "
203 "erase of region [0x%llx, 0x%llx] failed\n",
204 erase.addr, erase.len);
211 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
212 * Function: mtd_Write_Page_Main
213 * Inputs: Write buffer address pointer
216 * Number of pages to process
217 * Outputs: PASS=0 (notice 0=ok here)
218 * Description: Write the data in the buffer to main area of flash
220 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
221 u16 mtd_Write_Page_Main(u8 *write_data, u32 Block,
222 u16 Page, u16 PageCount)
227 if (Block >= DeviceInfo.wTotalBlocks)
230 if (Page + PageCount > DeviceInfo.wPagesPerBlock)
233 nand_dbg_print(NAND_DBG_DEBUG, "mtd_Write_Page_Main: "
234 "lba %u Page %u PageCount %u\n",
236 (unsigned int)Page, (unsigned int)PageCount);
240 ret = spectra_mtd->write(spectra_mtd,
241 (Block * spectra_mtd->erasesize) + (Page * spectra_mtd->writesize),
242 DeviceInfo.wPageDataSize, &retlen, write_data);
244 printk(KERN_ERR "%s failed %d\n", __func__, ret);
247 write_data += DeviceInfo.wPageDataSize;
252 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
253 __FILE__, __LINE__, __func__);
258 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
259 * Function: mtd_Read_Page_Main
260 * Inputs: Read buffer address pointer
263 * Number of pages to process
264 * Outputs: PASS=0 (notice 0=ok here)
265 * Description: Read the data from the flash main area to the buffer
267 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
268 u16 mtd_Read_Page_Main(u8 *read_data, u32 Block,
269 u16 Page, u16 PageCount)
274 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
275 __FILE__, __LINE__, __func__);
277 if (Block >= DeviceInfo.wTotalBlocks)
280 if (Page + PageCount > DeviceInfo.wPagesPerBlock)
283 nand_dbg_print(NAND_DBG_DEBUG, "mtd_Read_Page_Main: "
284 "lba %u Page %u PageCount %u\n",
286 (unsigned int)Page, (unsigned int)PageCount);
290 ret = spectra_mtd->read(spectra_mtd,
291 (Block * spectra_mtd->erasesize) + (Page * spectra_mtd->writesize),
292 DeviceInfo.wPageDataSize, &retlen, read_data);
294 printk(KERN_ERR "%s failed %d\n", __func__, ret);
297 read_data += DeviceInfo.wPageDataSize;
302 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
303 __FILE__, __LINE__, __func__);
309 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
310 * Function: mtd_Read_Page_Main_Spare
311 * Inputs: Write Buffer
314 * Outputs: PASS=0 (notice 0=ok here)
315 * Description: Read from flash main+spare area
317 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
318 u16 mtd_Read_Page_Main_Spare(u8 *read_data, u32 Block,
319 u16 Page, u16 PageCount)
321 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
322 __FILE__, __LINE__, __func__);
324 if (Block >= DeviceInfo.wTotalBlocks) {
325 printk(KERN_ERR "Read Page Main+Spare "
326 "Error: Block Address too big\n");
330 if (Page + PageCount > DeviceInfo.wPagesPerBlock) {
331 printk(KERN_ERR "Read Page Main+Spare "
332 "Error: Page number %d+%d too big in block %d\n",
333 Page, PageCount, Block);
337 nand_dbg_print(NAND_DBG_DEBUG, "Read Page Main + Spare - "
338 "No. of pages %u block %u start page %u\n",
339 (unsigned int)PageCount,
340 (unsigned int)Block, (unsigned int)Page);
344 struct mtd_oob_ops ops;
347 ops.mode = MTD_OOB_AUTO;
348 ops.datbuf = read_data;
349 ops.len = DeviceInfo.wPageDataSize;
350 ops.oobbuf = read_data + DeviceInfo.wPageDataSize + BTSIG_OFFSET;
351 ops.ooblen = BTSIG_BYTES;
354 ret = spectra_mtd->read_oob(spectra_mtd,
355 (Block * spectra_mtd->erasesize) + (Page * spectra_mtd->writesize),
358 printk(KERN_ERR "%s failed %d\n", __func__, ret);
361 read_data += DeviceInfo.wPageSize;
369 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
370 * Function: mtd_Write_Page_Main_Spare
371 * Inputs: Write buffer
374 * Outputs: PASS=0 (notice 0=ok here)
375 * Description: Write the buffer to main+spare area of flash
377 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
378 u16 mtd_Write_Page_Main_Spare(u8 *write_data, u32 Block,
379 u16 Page, u16 page_count)
381 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
382 __FILE__, __LINE__, __func__);
384 if (Block >= DeviceInfo.wTotalBlocks) {
385 printk(KERN_ERR "Write Page Main + Spare "
386 "Error: Block Address too big\n");
390 if (Page + page_count > DeviceInfo.wPagesPerBlock) {
391 printk(KERN_ERR "Write Page Main + Spare "
392 "Error: Page number %d+%d too big in block %d\n",
393 Page, page_count, Block);
398 nand_dbg_print(NAND_DBG_DEBUG, "Write Page Main+Spare - "
399 "No. of pages %u block %u start page %u\n",
400 (unsigned int)page_count,
401 (unsigned int)Block, (unsigned int)Page);
404 struct mtd_oob_ops ops;
407 ops.mode = MTD_OOB_AUTO;
408 ops.datbuf = write_data;
409 ops.len = DeviceInfo.wPageDataSize;
410 ops.oobbuf = write_data + DeviceInfo.wPageDataSize + BTSIG_OFFSET;
411 ops.ooblen = BTSIG_BYTES;
414 ret = spectra_mtd->write_oob(spectra_mtd,
415 (Block * spectra_mtd->erasesize) + (Page * spectra_mtd->writesize),
418 printk(KERN_ERR "%s failed %d\n", __func__, ret);
421 write_data += DeviceInfo.wPageSize;
429 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
430 * Function: mtd_Write_Page_Spare
431 * Inputs: Write buffer
434 * Outputs: PASS=0 (notice 0=ok here)
435 * Description: Write the buffer in the spare area
437 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
438 u16 mtd_Write_Page_Spare(u8 *write_data, u32 Block,
439 u16 Page, u16 PageCount)
445 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
446 * Function: mtd_Read_Page_Spare
447 * Inputs: Write Buffer
450 * Outputs: PASS=0 (notice 0=ok here)
451 * Description: Read data from the spare area
453 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
454 u16 mtd_Read_Page_Spare(u8 *read_data, u32 Block,
455 u16 Page, u16 PageCount)
457 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
458 __FILE__, __LINE__, __func__);
460 if (Block >= DeviceInfo.wTotalBlocks) {
461 printk(KERN_ERR "Read Page Spare "
462 "Error: Block Address too big\n");
466 if (Page + PageCount > DeviceInfo.wPagesPerBlock) {
467 printk(KERN_ERR "Read Page Spare "
468 "Error: Page number too big\n");
472 nand_dbg_print(NAND_DBG_DEBUG, "Read Page Spare- "
473 "block %u page %u (%u pages)\n",
474 (unsigned int)Block, (unsigned int)Page, PageCount);
477 struct mtd_oob_ops ops;
480 ops.mode = MTD_OOB_AUTO;
483 ops.oobbuf = read_data;
484 ops.ooblen = BTSIG_BYTES;
487 ret = spectra_mtd->read_oob(spectra_mtd,
488 (Block * spectra_mtd->erasesize) + (Page * spectra_mtd->writesize),
491 printk(KERN_ERR "%s failed %d\n", __func__, ret);
495 read_data += DeviceInfo.wPageSize;
503 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
504 * Function: mtd_Enable_Disable_Interrupts
505 * Inputs: enable or disable
508 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
509 void mtd_Enable_Disable_Interrupts(u16 INT_ENABLE)
511 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
512 __FILE__, __LINE__, __func__);
515 u16 mtd_Get_Bad_Block(u32 block)
521 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
522 * Support for CDMA functions
523 ************************************
524 * mtd_CDMA_Flash_Init
525 * CDMA_process_data command (use LLD_CDMA)
526 * CDMA_MemCopy_CMD (use LLD_CDMA)
527 * mtd_CDMA_execute all commands
528 * mtd_CDMA_Event_Status
529 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
530 u16 mtd_CDMA_Flash_Init(void)
534 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
535 __FILE__, __LINE__, __func__);
537 for (i = 0; i < MAX_DESCS + MAX_CHANS; i++) {
538 PendingCMD[i].CMD = 0;
539 PendingCMD[i].Tag = 0;
540 PendingCMD[i].DataAddr = 0;
541 PendingCMD[i].Block = 0;
542 PendingCMD[i].Page = 0;
543 PendingCMD[i].PageCount = 0;
544 PendingCMD[i].DataDestAddr = 0;
545 PendingCMD[i].DataSrcAddr = 0;
546 PendingCMD[i].MemCopyByteCnt = 0;
547 PendingCMD[i].ChanSync[0] = 0;
548 PendingCMD[i].ChanSync[1] = 0;
549 PendingCMD[i].ChanSync[2] = 0;
550 PendingCMD[i].ChanSync[3] = 0;
551 PendingCMD[i].ChanSync[4] = 0;
552 PendingCMD[i].Status = 3;
558 static void mtd_isr(int irq, void *dev_id)
563 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
564 * Function: CDMA_Execute_CMDs
565 * Inputs: tag_count: the number of pending cmds to do
567 * Description: execute each command in the pending CMD array
568 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
569 u16 mtd_CDMA_Execute_CMDs(u16 tag_count)
572 u8 CMD; /* cmd parameter */
579 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
580 __FILE__, __LINE__, __func__);
582 nand_dbg_print(NAND_DBG_TRACE, "At start of Execute CMDs: "
583 "Tag Count %u\n", tag_count);
585 for (i = 0; i < totalUsedBanks; i++) {
586 PendingCMD[i].CMD = DUMMY_CMD;
587 PendingCMD[i].Tag = 0xFF;
588 PendingCMD[i].Block =
589 (DeviceInfo.wTotalBlocks / totalUsedBanks) * i;
591 for (j = 0; j <= MAX_CHANS; j++)
592 PendingCMD[i].ChanSync[j] = 0;
595 CDMA_Execute_CMDs(tag_count);
598 print_pending_cmds(tag_count);
606 i < tag_count + MAX_CHANS; i++) {
607 CMD = PendingCMD[i].CMD;
608 data = PendingCMD[i].DataAddr;
609 block = PendingCMD[i].Block;
610 page = PendingCMD[i].Page;
611 count = PendingCMD[i].PageCount;
615 mtd_Erase_Block(block);
616 PendingCMD[i].Status = PASS;
619 mtd_Write_Page_Main(data, block, page, count);
620 PendingCMD[i].Status = PASS;
622 case WRITE_MAIN_SPARE_CMD:
623 mtd_Write_Page_Main_Spare(data, block, page, count);
624 PendingCMD[i].Status = PASS;
627 mtd_Read_Page_Main(data, block, page, count);
628 PendingCMD[i].Status = PASS;
631 memcpy(PendingCMD[i].DataDestAddr,
632 PendingCMD[i].DataSrcAddr,
633 PendingCMD[i].MemCopyByteCnt);
635 PendingCMD[i].Status = PASS;
638 PendingCMD[i].Status = FAIL;
644 * Temperory adding code to reset PendingCMD array for basic testing.
645 * It should be done at the end of event status function.
647 for (i = tag_count + MAX_CHANS; i < MAX_DESCS; i++) {
648 PendingCMD[i].CMD = 0;
649 PendingCMD[i].Tag = 0;
650 PendingCMD[i].DataAddr = 0;
651 PendingCMD[i].Block = 0;
652 PendingCMD[i].Page = 0;
653 PendingCMD[i].PageCount = 0;
654 PendingCMD[i].DataDestAddr = 0;
655 PendingCMD[i].DataSrcAddr = 0;
656 PendingCMD[i].MemCopyByteCnt = 0;
657 PendingCMD[i].ChanSync[0] = 0;
658 PendingCMD[i].ChanSync[1] = 0;
659 PendingCMD[i].ChanSync[2] = 0;
660 PendingCMD[i].ChanSync[3] = 0;
661 PendingCMD[i].ChanSync[4] = 0;
662 PendingCMD[i].Status = CMD_NOT_DONE;
665 nand_dbg_print(NAND_DBG_TRACE, "At end of Execute CMDs.\n");
667 mtd_isr(0, 0); /* This is a null isr now. Need fill it in future */
672 /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
673 * Function: mtd_Event_Status
675 * Outputs: Event_Status code
676 * Description: This function can also be used to force errors
677 *&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
678 u16 mtd_CDMA_Event_Status(void)
680 nand_dbg_print(NAND_DBG_TRACE, "%s, Line %d, Function: %s\n",
681 __FILE__, __LINE__, __func__);