pasemi_nand.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2006-2007 PA Semi, Inc
  3. *
  4. * Author: Egor Martovetsky <egor@pasemi.com>
  5. * Maintained by: Olof Johansson <olof@lixom.net>
  6. *
  7. * Driver for the PWRficient onchip NAND flash interface
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #undef DEBUG
  23. #include <linux/slab.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/nand.h>
  28. #include <linux/mtd/nand_ecc.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/pci.h>
  32. #include <asm/io.h>
  33. #define LBICTRL_LPCCTL_NR 0x00004000
  34. #define CLE_PIN_CTL 15
  35. #define ALE_PIN_CTL 14
  36. static unsigned int lpcctl;
  37. static struct mtd_info *pasemi_nand_mtd;
  38. static const char driver_name[] = "pasemi-nand";
  39. static void pasemi_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  40. {
  41. struct nand_chip *chip = mtd->priv;
  42. while (len > 0x800) {
  43. memcpy_fromio(buf, chip->IO_ADDR_R, 0x800);
  44. buf += 0x800;
  45. len -= 0x800;
  46. }
  47. memcpy_fromio(buf, chip->IO_ADDR_R, len);
  48. }
  49. static void pasemi_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  50. {
  51. struct nand_chip *chip = mtd->priv;
  52. while (len > 0x800) {
  53. memcpy_toio(chip->IO_ADDR_R, buf, 0x800);
  54. buf += 0x800;
  55. len -= 0x800;
  56. }
  57. memcpy_toio(chip->IO_ADDR_R, buf, len);
  58. }
  59. static void pasemi_hwcontrol(struct mtd_info *mtd, int cmd,
  60. unsigned int ctrl)
  61. {
  62. struct nand_chip *chip = mtd->priv;
  63. if (cmd == NAND_CMD_NONE)
  64. return;
  65. if (ctrl & NAND_CLE)
  66. out_8(chip->IO_ADDR_W + (1 << CLE_PIN_CTL), cmd);
  67. else
  68. out_8(chip->IO_ADDR_W + (1 << ALE_PIN_CTL), cmd);
  69. /* Push out posted writes */
  70. eieio();
  71. inl(lpcctl);
  72. }
  73. int pasemi_device_ready(struct mtd_info *mtd)
  74. {
  75. return !!(inl(lpcctl) & LBICTRL_LPCCTL_NR);
  76. }
  77. static int __devinit pasemi_nand_probe(struct of_device *ofdev,
  78. const struct of_device_id *match)
  79. {
  80. struct pci_dev *pdev;
  81. struct device_node *np = ofdev->node;
  82. struct resource res;
  83. struct nand_chip *chip;
  84. int err = 0;
  85. err = of_address_to_resource(np, 0, &res);
  86. if (err)
  87. return -EINVAL;
  88. /* We only support one device at the moment */
  89. if (pasemi_nand_mtd)
  90. return -ENODEV;
  91. pr_debug("pasemi_nand at %llx-%llx\n", res.start, res.end);
  92. /* Allocate memory for MTD device structure and private data */
  93. pasemi_nand_mtd = kzalloc(sizeof(struct mtd_info) +
  94. sizeof(struct nand_chip), GFP_KERNEL);
  95. if (!pasemi_nand_mtd) {
  96. printk(KERN_WARNING
  97. "Unable to allocate PASEMI NAND MTD device structure\n");
  98. err = -ENOMEM;
  99. goto out;
  100. }
  101. /* Get pointer to private data */
  102. chip = (struct nand_chip *)&pasemi_nand_mtd[1];
  103. /* Link the private data with the MTD structure */
  104. pasemi_nand_mtd->priv = chip;
  105. pasemi_nand_mtd->owner = THIS_MODULE;
  106. chip->IO_ADDR_R = of_iomap(np, 0);
  107. chip->IO_ADDR_W = chip->IO_ADDR_R;
  108. if (!chip->IO_ADDR_R) {
  109. err = -EIO;
  110. goto out_mtd;
  111. }
  112. pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa008, NULL);
  113. if (!pdev) {
  114. err = -ENODEV;
  115. goto out_ior;
  116. }
  117. lpcctl = pci_resource_start(pdev, 0);
  118. pci_dev_put(pdev);
  119. if (!request_region(lpcctl, 4, driver_name)) {
  120. err = -EBUSY;
  121. goto out_ior;
  122. }
  123. chip->cmd_ctrl = pasemi_hwcontrol;
  124. chip->dev_ready = pasemi_device_ready;
  125. chip->read_buf = pasemi_read_buf;
  126. chip->write_buf = pasemi_write_buf;
  127. chip->chip_delay = 0;
  128. chip->ecc.mode = NAND_ECC_SOFT;
  129. /* Enable the following for a flash based bad block table */
  130. chip->options = NAND_USE_FLASH_BBT | NAND_NO_AUTOINCR;
  131. /* Scan to find existance of the device */
  132. if (nand_scan(pasemi_nand_mtd, 1)) {
  133. err = -ENXIO;
  134. goto out_lpc;
  135. }
  136. if (add_mtd_device(pasemi_nand_mtd)) {
  137. printk(KERN_ERR "pasemi_nand: Unable to register MTD device\n");
  138. err = -ENODEV;
  139. goto out_lpc;
  140. }
  141. printk(KERN_INFO "PA Semi NAND flash at %08llx, control at I/O %x\n",
  142. res.start, lpcctl);
  143. return 0;
  144. out_lpc:
  145. release_region(lpcctl, 4);
  146. out_ior:
  147. iounmap(chip->IO_ADDR_R);
  148. out_mtd:
  149. kfree(pasemi_nand_mtd);
  150. out:
  151. return err;
  152. }
  153. static int __devexit pasemi_nand_remove(struct of_device *ofdev)
  154. {
  155. struct nand_chip *chip;
  156. if (!pasemi_nand_mtd)
  157. return 0;
  158. chip = pasemi_nand_mtd->priv;
  159. /* Release resources, unregister device */
  160. nand_release(pasemi_nand_mtd);
  161. release_region(lpcctl, 4);
  162. iounmap(chip->IO_ADDR_R);
  163. /* Free the MTD device structure */
  164. kfree(pasemi_nand_mtd);
  165. pasemi_nand_mtd = NULL;
  166. return 0;
  167. }
  168. static struct of_device_id pasemi_nand_match[] =
  169. {
  170. {
  171. .compatible = "pasemi,localbus-nand",
  172. },
  173. {},
  174. };
  175. MODULE_DEVICE_TABLE(of, pasemi_nand_match);
  176. static struct of_platform_driver pasemi_nand_driver =
  177. {
  178. .name = (char*)driver_name,
  179. .match_table = pasemi_nand_match,
  180. .probe = pasemi_nand_probe,
  181. .remove = pasemi_nand_remove,
  182. };
  183. static int __init pasemi_nand_init(void)
  184. {
  185. return of_register_platform_driver(&pasemi_nand_driver);
  186. }
  187. module_init(pasemi_nand_init);
  188. static void __exit pasemi_nand_exit(void)
  189. {
  190. of_unregister_platform_driver(&pasemi_nand_driver);
  191. }
  192. module_exit(pasemi_nand_exit);
  193. MODULE_LICENSE("GPL");
  194. MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
  195. MODULE_DESCRIPTION("NAND flash interface driver for PA Semi PWRficient");