bcm_umi_nand.c 13 KB

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