PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/mtd/nand/bcm_umi_nand.c

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