nomadik_nand.c 6.0 KB

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