bcm_umi_nand.c 13 KB

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