pasemi_nand.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/module.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/rawnand.h>
  27. #include <linux/mtd/nand_ecc.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/pci.h>
  33. #include <asm/io.h>
  34. #define LBICTRL_LPCCTL_NR 0x00004000
  35. #define CLE_PIN_CTL 15
  36. #define ALE_PIN_CTL 14
  37. static unsigned int lpcctl;
  38. static struct mtd_info *pasemi_nand_mtd;
  39. static const char driver_name[] = "pasemi-nand";
  40. static void pasemi_read_buf(struct nand_chip *chip, u_char *buf, int len)
  41. {
  42. while (len > 0x800) {
  43. memcpy_fromio(buf, chip->legacy.IO_ADDR_R, 0x800);
  44. buf += 0x800;
  45. len -= 0x800;
  46. }
  47. memcpy_fromio(buf, chip->legacy.IO_ADDR_R, len);
  48. }
  49. static void pasemi_write_buf(struct nand_chip *chip, const u_char *buf,
  50. int len)
  51. {
  52. while (len > 0x800) {
  53. memcpy_toio(chip->legacy.IO_ADDR_R, buf, 0x800);
  54. buf += 0x800;
  55. len -= 0x800;
  56. }
  57. memcpy_toio(chip->legacy.IO_ADDR_R, buf, len);
  58. }
  59. static void pasemi_hwcontrol(struct nand_chip *chip, int cmd,
  60. unsigned int ctrl)
  61. {
  62. if (cmd == NAND_CMD_NONE)
  63. return;
  64. if (ctrl & NAND_CLE)
  65. out_8(chip->legacy.IO_ADDR_W + (1 << CLE_PIN_CTL), cmd);
  66. else
  67. out_8(chip->legacy.IO_ADDR_W + (1 << ALE_PIN_CTL), cmd);
  68. /* Push out posted writes */
  69. eieio();
  70. inl(lpcctl);
  71. }
  72. int pasemi_device_ready(struct nand_chip *chip)
  73. {
  74. return !!(inl(lpcctl) & LBICTRL_LPCCTL_NR);
  75. }
  76. static int pasemi_nand_probe(struct platform_device *ofdev)
  77. {
  78. struct device *dev = &ofdev->dev;
  79. struct pci_dev *pdev;
  80. struct device_node *np = dev->of_node;
  81. struct resource res;
  82. struct nand_chip *chip;
  83. int err = 0;
  84. err = of_address_to_resource(np, 0, &res);
  85. if (err)
  86. return -EINVAL;
  87. /* We only support one device at the moment */
  88. if (pasemi_nand_mtd)
  89. return -ENODEV;
  90. dev_dbg(dev, "pasemi_nand at %pR\n", &res);
  91. /* Allocate memory for MTD device structure and private data */
  92. chip = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
  93. if (!chip) {
  94. err = -ENOMEM;
  95. goto out;
  96. }
  97. pasemi_nand_mtd = nand_to_mtd(chip);
  98. /* Link the private data with the MTD structure */
  99. pasemi_nand_mtd->dev.parent = dev;
  100. chip->legacy.IO_ADDR_R = of_iomap(np, 0);
  101. chip->legacy.IO_ADDR_W = chip->legacy.IO_ADDR_R;
  102. if (!chip->legacy.IO_ADDR_R) {
  103. err = -EIO;
  104. goto out_mtd;
  105. }
  106. pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa008, NULL);
  107. if (!pdev) {
  108. err = -ENODEV;
  109. goto out_ior;
  110. }
  111. lpcctl = pci_resource_start(pdev, 0);
  112. pci_dev_put(pdev);
  113. if (!request_region(lpcctl, 4, driver_name)) {
  114. err = -EBUSY;
  115. goto out_ior;
  116. }
  117. chip->legacy.cmd_ctrl = pasemi_hwcontrol;
  118. chip->legacy.dev_ready = pasemi_device_ready;
  119. chip->legacy.read_buf = pasemi_read_buf;
  120. chip->legacy.write_buf = pasemi_write_buf;
  121. chip->legacy.chip_delay = 0;
  122. chip->ecc.mode = NAND_ECC_SOFT;
  123. chip->ecc.algo = NAND_ECC_HAMMING;
  124. /* Enable the following for a flash based bad block table */
  125. chip->bbt_options = NAND_BBT_USE_FLASH;
  126. /* Scan to find existence of the device */
  127. err = nand_scan(chip, 1);
  128. if (err)
  129. goto out_lpc;
  130. if (mtd_device_register(pasemi_nand_mtd, NULL, 0)) {
  131. dev_err(dev, "Unable to register MTD device\n");
  132. err = -ENODEV;
  133. goto out_lpc;
  134. }
  135. dev_info(dev, "PA Semi NAND flash at %pR, control at I/O %x\n", &res,
  136. lpcctl);
  137. return 0;
  138. out_lpc:
  139. release_region(lpcctl, 4);
  140. out_ior:
  141. iounmap(chip->legacy.IO_ADDR_R);
  142. out_mtd:
  143. kfree(chip);
  144. out:
  145. return err;
  146. }
  147. static int pasemi_nand_remove(struct platform_device *ofdev)
  148. {
  149. struct nand_chip *chip;
  150. if (!pasemi_nand_mtd)
  151. return 0;
  152. chip = mtd_to_nand(pasemi_nand_mtd);
  153. /* Release resources, unregister device */
  154. nand_release(chip);
  155. release_region(lpcctl, 4);
  156. iounmap(chip->legacy.IO_ADDR_R);
  157. /* Free the MTD device structure */
  158. kfree(chip);
  159. pasemi_nand_mtd = NULL;
  160. return 0;
  161. }
  162. static const struct of_device_id pasemi_nand_match[] =
  163. {
  164. {
  165. .compatible = "pasemi,localbus-nand",
  166. },
  167. {},
  168. };
  169. MODULE_DEVICE_TABLE(of, pasemi_nand_match);
  170. static struct platform_driver pasemi_nand_driver =
  171. {
  172. .driver = {
  173. .name = driver_name,
  174. .of_match_table = pasemi_nand_match,
  175. },
  176. .probe = pasemi_nand_probe,
  177. .remove = pasemi_nand_remove,
  178. };
  179. module_platform_driver(pasemi_nand_driver);
  180. MODULE_LICENSE("GPL");
  181. MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
  182. MODULE_DESCRIPTION("NAND flash interface driver for PA Semi PWRficient");