nomadik_nand.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * drivers/mtd/nand/nomadik_nand.c
  3. *
  4. * Overview:
  5. * Driver for on-board NAND flash on Nomadik Platforms
  6. *
  7. * Copyright © 2007 STMicroelectronics Pvt. Ltd.
  8. * Author: Sachin Verma <sachin.verma@st.com>
  9. *
  10. * Copyright © 2009 Alessandro Rubini
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. */
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/nand.h>
  28. #include <linux/mtd/nand_ecc.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/io.h>
  32. #include <linux/slab.h>
  33. #include <mach/nand.h>
  34. #include <mach/fsmc.h>
  35. #include <mtd/mtd-abi.h>
  36. struct nomadik_nand_host {
  37. struct mtd_info mtd;
  38. struct nand_chip nand;
  39. void __iomem *data_va;
  40. void __iomem *cmd_va;
  41. void __iomem *addr_va;
  42. struct nand_bbt_descr *bbt_desc;
  43. };
  44. static struct nand_ecclayout nomadik_ecc_layout = {
  45. .eccbytes = 3 * 4,
  46. .eccpos = { /* each subpage has 16 bytes: pos 2,3,4 hosts ECC */
  47. 0x02, 0x03, 0x04,
  48. 0x12, 0x13, 0x14,
  49. 0x22, 0x23, 0x24,
  50. 0x32, 0x33, 0x34},
  51. /* let's keep bytes 5,6,7 for us, just in case we change ECC algo */
  52. .oobfree = { {0x08, 0x08}, {0x18, 0x08}, {0x28, 0x08}, {0x38, 0x08} },
  53. };
  54. static void nomadik_ecc_control(struct mtd_info *mtd, int mode)
  55. {
  56. /* No need to enable hw ecc, it's on by default */
  57. }
  58. static void nomadik_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  59. {
  60. struct nand_chip *nand = mtd->priv;
  61. struct nomadik_nand_host *host = nand->priv;
  62. if (cmd == NAND_CMD_NONE)
  63. return;
  64. if (ctrl & NAND_CLE)
  65. writeb(cmd, host->cmd_va);
  66. else
  67. writeb(cmd, host->addr_va);
  68. }
  69. static int nomadik_nand_probe(struct platform_device *pdev)
  70. {
  71. struct nomadik_nand_platform_data *pdata = pdev->dev.platform_data;
  72. struct nomadik_nand_host *host;
  73. struct mtd_info *mtd;
  74. struct nand_chip *nand;
  75. struct resource *res;
  76. int ret = 0;
  77. /* Allocate memory for the device structure (and zero it) */
  78. host = kzalloc(sizeof(struct nomadik_nand_host), GFP_KERNEL);
  79. if (!host) {
  80. dev_err(&pdev->dev, "Failed to allocate device structure.\n");
  81. return -ENOMEM;
  82. }
  83. /* Call the client's init function, if any */
  84. if (pdata->init)
  85. ret = pdata->init();
  86. if (ret < 0) {
  87. dev_err(&pdev->dev, "Init function failed\n");
  88. goto err;
  89. }
  90. /* ioremap three regions */
  91. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
  92. if (!res) {
  93. ret = -EIO;
  94. goto err_unmap;
  95. }
  96. host->addr_va = ioremap(res->start, resource_size(res));
  97. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
  98. if (!res) {
  99. ret = -EIO;
  100. goto err_unmap;
  101. }
  102. host->data_va = ioremap(res->start, resource_size(res));
  103. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
  104. if (!res) {
  105. ret = -EIO;
  106. goto err_unmap;
  107. }
  108. host->cmd_va = ioremap(res->start, resource_size(res));
  109. if (!host->addr_va || !host->data_va || !host->cmd_va) {
  110. ret = -ENOMEM;
  111. goto err_unmap;
  112. }
  113. /* Link all private pointers */
  114. mtd = &host->mtd;
  115. nand = &host->nand;
  116. mtd->priv = nand;
  117. nand->priv = host;
  118. host->mtd.owner = THIS_MODULE;
  119. nand->IO_ADDR_R = host->data_va;
  120. nand->IO_ADDR_W = host->data_va;
  121. nand->cmd_ctrl = nomadik_cmd_ctrl;
  122. /*
  123. * This stanza declares ECC_HW but uses soft routines. It's because
  124. * HW claims to make the calculation but not the correction. However,
  125. * I haven't managed to get the desired data out of it until now.
  126. */
  127. nand->ecc.mode = NAND_ECC_SOFT;
  128. nand->ecc.layout = &nomadik_ecc_layout;
  129. nand->ecc.hwctl = nomadik_ecc_control;
  130. nand->ecc.size = 512;
  131. nand->ecc.bytes = 3;
  132. nand->options = pdata->options;
  133. /*
  134. * Scan to find existance of the device
  135. */
  136. if (nand_scan(&host->mtd, 1)) {
  137. ret = -ENXIO;
  138. goto err_unmap;
  139. }
  140. #ifdef CONFIG_MTD_PARTITIONS
  141. add_mtd_partitions(&host->mtd, pdata->parts, pdata->nparts);
  142. #else
  143. pr_info("Registering %s as whole device\n", mtd->name);
  144. add_mtd_device(mtd);
  145. #endif
  146. platform_set_drvdata(pdev, host);
  147. return 0;
  148. err_unmap:
  149. if (host->cmd_va)
  150. iounmap(host->cmd_va);
  151. if (host->data_va)
  152. iounmap(host->data_va);
  153. if (host->addr_va)
  154. iounmap(host->addr_va);
  155. err:
  156. kfree(host);
  157. return ret;
  158. }
  159. /*
  160. * Clean up routine
  161. */
  162. static int nomadik_nand_remove(struct platform_device *pdev)
  163. {
  164. struct nomadik_nand_host *host = platform_get_drvdata(pdev);
  165. struct nomadik_nand_platform_data *pdata = pdev->dev.platform_data;
  166. if (pdata->exit)
  167. pdata->exit();
  168. if (host) {
  169. iounmap(host->cmd_va);
  170. iounmap(host->data_va);
  171. iounmap(host->addr_va);
  172. kfree(host);
  173. }
  174. return 0;
  175. }
  176. static int nomadik_nand_suspend(struct device *dev)
  177. {
  178. struct nomadik_nand_host *host = dev_get_drvdata(dev);
  179. int ret = 0;
  180. if (host)
  181. ret = host->mtd.suspend(&host->mtd);
  182. return ret;
  183. }
  184. static int nomadik_nand_resume(struct device *dev)
  185. {
  186. struct nomadik_nand_host *host = dev_get_drvdata(dev);
  187. if (host)
  188. host->mtd.resume(&host->mtd);
  189. return 0;
  190. }
  191. static const struct dev_pm_ops nomadik_nand_pm_ops = {
  192. .suspend = nomadik_nand_suspend,
  193. .resume = nomadik_nand_resume,
  194. };
  195. static struct platform_driver nomadik_nand_driver = {
  196. .probe = nomadik_nand_probe,
  197. .remove = nomadik_nand_remove,
  198. .driver = {
  199. .owner = THIS_MODULE,
  200. .name = "nomadik_nand",
  201. .pm = &nomadik_nand_pm_ops,
  202. },
  203. };
  204. static int __init nand_nomadik_init(void)
  205. {
  206. pr_info("Nomadik NAND driver\n");
  207. return platform_driver_register(&nomadik_nand_driver);
  208. }
  209. static void __exit nand_nomadik_exit(void)
  210. {
  211. platform_driver_unregister(&nomadik_nand_driver);
  212. }
  213. module_init(nand_nomadik_init);
  214. module_exit(nand_nomadik_exit);
  215. MODULE_LICENSE("GPL");
  216. MODULE_AUTHOR("ST Microelectronics (sachin.verma@st.com)");
  217. MODULE_DESCRIPTION("NAND driver for Nomadik Platform");