bcm_umi_nand.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*****************************************************************************
  2. * Copyright 2004 - 2009 Broadcom Corporation. All rights reserved.
  3. *
  4. * Unless you and Broadcom execute a separate written software license
  5. * agreement governing use of this software, this software is licensed to you
  6. * under the terms of the GNU General Public License version 2, available at
  7. * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  8. *
  9. * Notwithstanding the above, under no circumstances may you combine this
  10. * software in any way with any other Broadcom software provided under a
  11. * license other than the GPL, without Broadcom's express prior written
  12. * consent.
  13. *****************************************************************************/
  14. /* ---- Include Files ---------------------------------------------------- */
  15. #include <linux/version.h>
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <linux/ioport.h>
  23. #include <linux/device.h>
  24. #include <linux/delay.h>
  25. #include <linux/err.h>
  26. #include <linux/io.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/mtd/mtd.h>
  29. #include <linux/mtd/nand.h>
  30. #include <linux/mtd/nand_ecc.h>
  31. #include <linux/mtd/partitions.h>
  32. #include <asm/mach-types.h>
  33. #include <asm/system.h>
  34. #include <mach/reg_nand.h>
  35. #include <mach/reg_umi.h>
  36. #include "nand_bcm_umi.h"
  37. #include <mach/memory_settings.h>
  38. #define USE_DMA 1
  39. #include <mach/dma.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/completion.h>
  42. /* ---- External Variable Declarations ----------------------------------- */
  43. /* ---- External Function Prototypes ------------------------------------- */
  44. /* ---- Public Variables ------------------------------------------------- */
  45. /* ---- Private Constants and Types -------------------------------------- */
  46. static const __devinitconst char gBanner[] = KERN_INFO \
  47. "BCM UMI MTD NAND Driver: 1.00\n";
  48. #ifdef CONFIG_MTD_PARTITIONS
  49. const char *part_probes[] = { "cmdlinepart", NULL };
  50. #endif
  51. #if NAND_ECC_BCH
  52. static uint8_t scan_ff_pattern[] = { 0xff };
  53. static struct nand_bbt_descr largepage_bbt = {
  54. .options = 0,
  55. .offs = 0,
  56. .len = 1,
  57. .pattern = scan_ff_pattern
  58. };
  59. #endif
  60. /*
  61. ** Preallocate a buffer to avoid having to do this every dma operation.
  62. ** This is the size of the preallocated coherent DMA buffer.
  63. */
  64. #if USE_DMA
  65. #define DMA_MIN_BUFLEN 512
  66. #define DMA_MAX_BUFLEN PAGE_SIZE
  67. #define USE_DIRECT_IO(len) (((len) < DMA_MIN_BUFLEN) || \
  68. ((len) > DMA_MAX_BUFLEN))
  69. /*
  70. * The current NAND data space goes from 0x80001900 to 0x80001FFF,
  71. * which is only 0x700 = 1792 bytes long. This is too small for 2K, 4K page
  72. * size NAND flash. Need to break the DMA down to multiple 1Ks.
  73. *
  74. * Need to make sure REG_NAND_DATA_PADDR + DMA_MAX_LEN < 0x80002000
  75. */
  76. #define DMA_MAX_LEN 1024
  77. #else /* !USE_DMA */
  78. #define DMA_MIN_BUFLEN 0
  79. #define DMA_MAX_BUFLEN 0
  80. #define USE_DIRECT_IO(len) 1
  81. #endif
  82. /* ---- Private Function Prototypes -------------------------------------- */
  83. static void bcm_umi_nand_read_buf(struct mtd_info *mtd, u_char * buf, int len);
  84. static void bcm_umi_nand_write_buf(struct mtd_info *mtd, const u_char * buf,
  85. int len);
  86. /* ---- Private Variables ------------------------------------------------ */
  87. static struct mtd_info *board_mtd;
  88. static void __iomem *bcm_umi_io_base;
  89. static void *virtPtr;
  90. static dma_addr_t physPtr;
  91. static struct completion nand_comp;
  92. /* ---- Private Functions ------------------------------------------------ */
  93. #if NAND_ECC_BCH
  94. #include "bcm_umi_bch.c"
  95. #else
  96. #include "bcm_umi_hamming.c"
  97. #endif
  98. #if USE_DMA
  99. /* Handler called when the DMA finishes. */
  100. static void nand_dma_handler(DMA_Device_t dev, int reason, void *userData)
  101. {
  102. complete(&nand_comp);
  103. }
  104. static int nand_dma_init(void)
  105. {
  106. int rc;
  107. rc = dma_set_device_handler(DMA_DEVICE_NAND_MEM_TO_MEM,
  108. nand_dma_handler, NULL);
  109. if (rc != 0) {
  110. printk(KERN_ERR "dma_set_device_handler failed: %d\n", rc);
  111. return rc;
  112. }
  113. virtPtr =
  114. dma_alloc_coherent(NULL, DMA_MAX_BUFLEN, &physPtr, GFP_KERNEL);
  115. if (virtPtr == NULL) {
  116. printk(KERN_ERR "NAND - Failed to allocate memory for DMA buffer\n");
  117. return -ENOMEM;
  118. }
  119. return 0;
  120. }
  121. static void nand_dma_term(void)
  122. {
  123. if (virtPtr != NULL)
  124. dma_free_coherent(NULL, DMA_MAX_BUFLEN, virtPtr, physPtr);
  125. }
  126. static void nand_dma_read(void *buf, int len)
  127. {
  128. int offset = 0;
  129. int tmp_len = 0;
  130. int len_left = len;
  131. DMA_Handle_t hndl;
  132. if (virtPtr == NULL)
  133. panic("nand_dma_read: virtPtr == NULL\n");
  134. if ((void *)physPtr == NULL)
  135. panic("nand_dma_read: physPtr == NULL\n");
  136. hndl = dma_request_channel(DMA_DEVICE_NAND_MEM_TO_MEM);
  137. if (hndl < 0) {
  138. printk(KERN_ERR
  139. "nand_dma_read: unable to allocate dma channel: %d\n",
  140. (int)hndl);
  141. panic("\n");
  142. }
  143. while (len_left > 0) {
  144. if (len_left > DMA_MAX_LEN) {
  145. tmp_len = DMA_MAX_LEN;
  146. len_left -= DMA_MAX_LEN;
  147. } else {
  148. tmp_len = len_left;
  149. len_left = 0;
  150. }
  151. init_completion(&nand_comp);
  152. dma_transfer_mem_to_mem(hndl, REG_NAND_DATA_PADDR,
  153. physPtr + offset, tmp_len);
  154. wait_for_completion(&nand_comp);
  155. offset += tmp_len;
  156. }
  157. dma_free_channel(hndl);
  158. if (buf != NULL)
  159. memcpy(buf, virtPtr, len);
  160. }
  161. static void nand_dma_write(const void *buf, int len)
  162. {
  163. int offset = 0;
  164. int tmp_len = 0;
  165. int len_left = len;
  166. DMA_Handle_t hndl;
  167. if (buf == NULL)
  168. panic("nand_dma_write: buf == NULL\n");
  169. if (virtPtr == NULL)
  170. panic("nand_dma_write: virtPtr == NULL\n");
  171. if ((void *)physPtr == NULL)
  172. panic("nand_dma_write: physPtr == NULL\n");
  173. memcpy(virtPtr, buf, len);
  174. hndl = dma_request_channel(DMA_DEVICE_NAND_MEM_TO_MEM);
  175. if (hndl < 0) {
  176. printk(KERN_ERR
  177. "nand_dma_write: unable to allocate dma channel: %d\n",
  178. (int)hndl);
  179. panic("\n");
  180. }
  181. while (len_left > 0) {
  182. if (len_left > DMA_MAX_LEN) {
  183. tmp_len = DMA_MAX_LEN;
  184. len_left -= DMA_MAX_LEN;
  185. } else {
  186. tmp_len = len_left;
  187. len_left = 0;
  188. }
  189. init_completion(&nand_comp);
  190. dma_transfer_mem_to_mem(hndl, physPtr + offset,
  191. REG_NAND_DATA_PADDR, tmp_len);
  192. wait_for_completion(&nand_comp);
  193. offset += tmp_len;
  194. }
  195. dma_free_channel(hndl);
  196. }
  197. #endif
  198. static int nand_dev_ready(struct mtd_info *mtd)
  199. {
  200. return nand_bcm_umi_dev_ready();
  201. }
  202. /****************************************************************************
  203. *
  204. * bcm_umi_nand_inithw
  205. *
  206. * This routine does the necessary hardware (board-specific)
  207. * initializations. This includes setting up the timings, etc.
  208. *
  209. ***************************************************************************/
  210. int bcm_umi_nand_inithw(void)
  211. {
  212. /* Configure nand timing parameters */
  213. REG_UMI_NAND_TCR &= ~0x7ffff;
  214. REG_UMI_NAND_TCR |= HW_CFG_NAND_TCR;
  215. #if !defined(CONFIG_MTD_NAND_BCM_UMI_HWCS)
  216. /* enable software control of CS */
  217. REG_UMI_NAND_TCR |= REG_UMI_NAND_TCR_CS_SWCTRL;
  218. #endif
  219. /* keep NAND chip select asserted */
  220. REG_UMI_NAND_RCSR |= REG_UMI_NAND_RCSR_CS_ASSERTED;
  221. REG_UMI_NAND_TCR &= ~REG_UMI_NAND_TCR_WORD16;
  222. /* enable writes to flash */
  223. REG_UMI_MMD_ICR |= REG_UMI_MMD_ICR_FLASH_WP;
  224. writel(NAND_CMD_RESET, bcm_umi_io_base + REG_NAND_CMD_OFFSET);
  225. nand_bcm_umi_wait_till_ready();
  226. #if NAND_ECC_BCH
  227. nand_bcm_umi_bch_config_ecc(NAND_ECC_NUM_BYTES);
  228. #endif
  229. return 0;
  230. }
  231. /* Used to turn latch the proper register for access. */
  232. static void bcm_umi_nand_hwcontrol(struct mtd_info *mtd, int cmd,
  233. unsigned int ctrl)
  234. {
  235. /* send command to hardware */
  236. struct nand_chip *chip = mtd->priv;
  237. if (ctrl & NAND_CTRL_CHANGE) {
  238. if (ctrl & NAND_CLE) {
  239. chip->IO_ADDR_W = bcm_umi_io_base + REG_NAND_CMD_OFFSET;
  240. goto CMD;
  241. }
  242. if (ctrl & NAND_ALE) {
  243. chip->IO_ADDR_W =
  244. bcm_umi_io_base + REG_NAND_ADDR_OFFSET;
  245. goto CMD;
  246. }
  247. chip->IO_ADDR_W = bcm_umi_io_base + REG_NAND_DATA8_OFFSET;
  248. }
  249. CMD:
  250. /* Send command to chip directly */
  251. if (cmd != NAND_CMD_NONE)
  252. writeb(cmd, chip->IO_ADDR_W);
  253. }
  254. static void bcm_umi_nand_write_buf(struct mtd_info *mtd, const u_char * buf,
  255. int len)
  256. {
  257. if (USE_DIRECT_IO(len)) {
  258. /* Do it the old way if the buffer is small or too large.
  259. * Probably quicker than starting and checking dma. */
  260. int i;
  261. struct nand_chip *this = mtd->priv;
  262. for (i = 0; i < len; i++)
  263. writeb(buf[i], this->IO_ADDR_W);
  264. }
  265. #if USE_DMA
  266. else
  267. nand_dma_write(buf, len);
  268. #endif
  269. }
  270. static void bcm_umi_nand_read_buf(struct mtd_info *mtd, u_char * buf, int len)
  271. {
  272. if (USE_DIRECT_IO(len)) {
  273. int i;
  274. struct nand_chip *this = mtd->priv;
  275. for (i = 0; i < len; i++)
  276. buf[i] = readb(this->IO_ADDR_R);
  277. }
  278. #if USE_DMA
  279. else
  280. nand_dma_read(buf, len);
  281. #endif
  282. }
  283. static uint8_t readbackbuf[NAND_MAX_PAGESIZE];
  284. static int bcm_umi_nand_verify_buf(struct mtd_info *mtd, const u_char * buf,
  285. int len)
  286. {
  287. /*
  288. * Try to readback page with ECC correction. This is necessary
  289. * for MLC parts which may have permanently stuck bits.
  290. */
  291. struct nand_chip *chip = mtd->priv;
  292. int ret = chip->ecc.read_page(mtd, chip, readbackbuf, 0);
  293. if (ret < 0)
  294. return -EFAULT;
  295. else {
  296. if (memcmp(readbackbuf, buf, len) == 0)
  297. return 0;
  298. return -EFAULT;
  299. }
  300. return 0;
  301. }
  302. static int __devinit bcm_umi_nand_probe(struct platform_device *pdev)
  303. {
  304. struct nand_chip *this;
  305. struct resource *r;
  306. int err = 0;
  307. printk(gBanner);
  308. /* Allocate memory for MTD device structure and private data */
  309. board_mtd =
  310. kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip),
  311. GFP_KERNEL);
  312. if (!board_mtd) {
  313. printk(KERN_WARNING
  314. "Unable to allocate NAND MTD device structure.\n");
  315. return -ENOMEM;
  316. }
  317. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  318. if (!r)
  319. return -ENXIO;
  320. /* map physical address */
  321. bcm_umi_io_base = ioremap(r->start, r->end - r->start + 1);
  322. if (!bcm_umi_io_base) {
  323. printk(KERN_ERR "ioremap to access BCM UMI NAND chip failed\n");
  324. kfree(board_mtd);
  325. return -EIO;
  326. }
  327. /* Get pointer to private data */
  328. this = (struct nand_chip *)(&board_mtd[1]);
  329. /* Initialize structures */
  330. memset((char *)board_mtd, 0, sizeof(struct mtd_info));
  331. memset((char *)this, 0, sizeof(struct nand_chip));
  332. /* Link the private data with the MTD structure */
  333. board_mtd->priv = this;
  334. /* Initialize the NAND hardware. */
  335. if (bcm_umi_nand_inithw() < 0) {
  336. printk(KERN_ERR "BCM UMI NAND chip could not be initialized\n");
  337. iounmap(bcm_umi_io_base);
  338. kfree(board_mtd);
  339. return -EIO;
  340. }
  341. /* Set address of NAND IO lines */
  342. this->IO_ADDR_W = bcm_umi_io_base + REG_NAND_DATA8_OFFSET;
  343. this->IO_ADDR_R = bcm_umi_io_base + REG_NAND_DATA8_OFFSET;
  344. /* Set command delay time, see datasheet for correct value */
  345. this->chip_delay = 0;
  346. /* Assign the device ready function, if available */
  347. this->dev_ready = nand_dev_ready;
  348. this->options = 0;
  349. this->write_buf = bcm_umi_nand_write_buf;
  350. this->read_buf = bcm_umi_nand_read_buf;
  351. this->verify_buf = bcm_umi_nand_verify_buf;
  352. this->cmd_ctrl = bcm_umi_nand_hwcontrol;
  353. this->ecc.mode = NAND_ECC_HW;
  354. this->ecc.size = 512;
  355. this->ecc.bytes = NAND_ECC_NUM_BYTES;
  356. #if NAND_ECC_BCH
  357. this->ecc.read_page = bcm_umi_bch_read_page_hwecc;
  358. this->ecc.write_page = bcm_umi_bch_write_page_hwecc;
  359. #else
  360. this->ecc.correct = nand_correct_data512;
  361. this->ecc.calculate = bcm_umi_hamming_get_hw_ecc;
  362. this->ecc.hwctl = bcm_umi_hamming_enable_hwecc;
  363. #endif
  364. #if USE_DMA
  365. err = nand_dma_init();
  366. if (err != 0)
  367. return err;
  368. #endif
  369. /* Figure out the size of the device that we have.
  370. * We need to do this to figure out which ECC
  371. * layout we'll be using.
  372. */
  373. err = nand_scan_ident(board_mtd, 1);
  374. if (err) {
  375. printk(KERN_ERR "nand_scan failed: %d\n", err);
  376. iounmap(bcm_umi_io_base);
  377. kfree(board_mtd);
  378. return err;
  379. }
  380. /* Now that we know the nand size, we can setup the ECC layout */
  381. switch (board_mtd->writesize) { /* writesize is the pagesize */
  382. case 4096:
  383. this->ecc.layout = &nand_hw_eccoob_4096;
  384. break;
  385. case 2048:
  386. this->ecc.layout = &nand_hw_eccoob_2048;
  387. break;
  388. case 512:
  389. this->ecc.layout = &nand_hw_eccoob_512;
  390. break;
  391. default:
  392. {
  393. printk(KERN_ERR "NAND - Unrecognized pagesize: %d\n",
  394. board_mtd->writesize);
  395. return -EINVAL;
  396. }
  397. }
  398. #if NAND_ECC_BCH
  399. if (board_mtd->writesize > 512) {
  400. if (this->options & NAND_USE_FLASH_BBT)
  401. largepage_bbt.options = NAND_BBT_SCAN2NDPAGE;
  402. this->badblock_pattern = &largepage_bbt;
  403. }
  404. #endif
  405. /* Now finish off the scan, now that ecc.layout has been initialized. */
  406. err = nand_scan_tail(board_mtd);
  407. if (err) {
  408. printk(KERN_ERR "nand_scan failed: %d\n", err);
  409. iounmap(bcm_umi_io_base);
  410. kfree(board_mtd);
  411. return err;
  412. }
  413. /* Register the partitions */
  414. {
  415. int nr_partitions;
  416. struct mtd_partition *partition_info;
  417. board_mtd->name = "bcm_umi-nand";
  418. nr_partitions =
  419. parse_mtd_partitions(board_mtd, part_probes,
  420. &partition_info, 0);
  421. if (nr_partitions <= 0) {
  422. printk(KERN_ERR "BCM UMI NAND: Too few partitions - %d\n",
  423. nr_partitions);
  424. iounmap(bcm_umi_io_base);
  425. kfree(board_mtd);
  426. return -EIO;
  427. }
  428. add_mtd_partitions(board_mtd, partition_info, nr_partitions);
  429. }
  430. /* Return happy */
  431. return 0;
  432. }
  433. static int bcm_umi_nand_remove(struct platform_device *pdev)
  434. {
  435. #if USE_DMA
  436. nand_dma_term();
  437. #endif
  438. /* Release resources, unregister device */
  439. nand_release(board_mtd);
  440. /* unmap physical address */
  441. iounmap(bcm_umi_io_base);
  442. /* Free the MTD device structure */
  443. kfree(board_mtd);
  444. return 0;
  445. }
  446. #ifdef CONFIG_PM
  447. static int bcm_umi_nand_suspend(struct platform_device *pdev,
  448. pm_message_t state)
  449. {
  450. printk(KERN_ERR "MTD NAND suspend is being called\n");
  451. return 0;
  452. }
  453. static int bcm_umi_nand_resume(struct platform_device *pdev)
  454. {
  455. printk(KERN_ERR "MTD NAND resume is being called\n");
  456. return 0;
  457. }
  458. #else
  459. #define bcm_umi_nand_suspend NULL
  460. #define bcm_umi_nand_resume NULL
  461. #endif
  462. static struct platform_driver nand_driver = {
  463. .driver = {
  464. .name = "bcm-nand",
  465. .owner = THIS_MODULE,
  466. },
  467. .probe = bcm_umi_nand_probe,
  468. .remove = bcm_umi_nand_remove,
  469. .suspend = bcm_umi_nand_suspend,
  470. .resume = bcm_umi_nand_resume,
  471. };
  472. static int __init nand_init(void)
  473. {
  474. return platform_driver_register(&nand_driver);
  475. }
  476. static void __exit nand_exit(void)
  477. {
  478. platform_driver_unregister(&nand_driver);
  479. }
  480. module_init(nand_init);
  481. module_exit(nand_exit);
  482. MODULE_LICENSE("GPL");
  483. MODULE_AUTHOR("Broadcom");
  484. MODULE_DESCRIPTION("BCM UMI MTD NAND driver");