nomadik_nand.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 existence of the device
  135. */
  136. if (nand_scan(&host->mtd, 1)) {
  137. ret = -ENXIO;
  138. goto err_unmap;
  139. }
  140. mtd_device_register(&host->mtd, pdata->parts, pdata->nparts);
  141. platform_set_drvdata(pdev, host);
  142. return 0;
  143. err_unmap:
  144. if (host->cmd_va)
  145. iounmap(host->cmd_va);
  146. if (host->data_va)
  147. iounmap(host->data_va);
  148. if (host->addr_va)
  149. iounmap(host->addr_va);
  150. err:
  151. kfree(host);
  152. return ret;
  153. }
  154. /*
  155. * Clean up routine
  156. */
  157. static int nomadik_nand_remove(struct platform_device *pdev)
  158. {
  159. struct nomadik_nand_host *host = platform_get_drvdata(pdev);
  160. struct nomadik_nand_platform_data *pdata = pdev->dev.platform_data;
  161. if (pdata->exit)
  162. pdata->exit();
  163. if (host) {
  164. nand_release(&host->mtd);
  165. iounmap(host->cmd_va);
  166. iounmap(host->data_va);
  167. iounmap(host->addr_va);
  168. kfree(host);
  169. }
  170. return 0;
  171. }
  172. static int nomadik_nand_suspend(struct device *dev)
  173. {
  174. struct nomadik_nand_host *host = dev_get_drvdata(dev);
  175. int ret = 0;
  176. if (host)
  177. ret = mtd_suspend(&host->mtd);
  178. return ret;
  179. }
  180. static int nomadik_nand_resume(struct device *dev)
  181. {
  182. struct nomadik_nand_host *host = dev_get_drvdata(dev);
  183. if (host)
  184. mtd_resume(&host->mtd);
  185. return 0;
  186. }
  187. static const struct dev_pm_ops nomadik_nand_pm_ops = {
  188. .suspend = nomadik_nand_suspend,
  189. .resume = nomadik_nand_resume,
  190. };
  191. static struct platform_driver nomadik_nand_driver = {
  192. .probe = nomadik_nand_probe,
  193. .remove = nomadik_nand_remove,
  194. .driver = {
  195. .owner = THIS_MODULE,
  196. .name = "nomadik_nand",
  197. .pm = &nomadik_nand_pm_ops,
  198. },
  199. };
  200. module_platform_driver(nomadik_nand_driver);
  201. MODULE_LICENSE("GPL");
  202. MODULE_AUTHOR("ST Microelectronics (sachin.verma@st.com)");
  203. MODULE_DESCRIPTION("NAND driver for Nomadik Platform");