bcm_umi_nand.c 14 KB

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