pasemi_nand.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2006-2007 PA Semi, Inc
  4. *
  5. * Author: Egor Martovetsky <[email protected]>
  6. * Maintained by: Olof Johansson <[email protected]>
  7. *
  8. * Driver for the PWRficient onchip NAND flash interface
  9. */
  10. #undef DEBUG
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/mtd/rawnand.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pci.h>
  20. #include <asm/io.h>
  21. #define LBICTRL_LPCCTL_NR 0x00004000
  22. #define CLE_PIN_CTL 15
  23. #define ALE_PIN_CTL 14
  24. static unsigned int lpcctl;
  25. static struct mtd_info *pasemi_nand_mtd;
  26. static struct nand_controller controller;
  27. static const char driver_name[] = "pasemi-nand";
  28. static void pasemi_read_buf(struct nand_chip *chip, u_char *buf, int len)
  29. {
  30. while (len > 0x800) {
  31. memcpy_fromio(buf, chip->legacy.IO_ADDR_R, 0x800);
  32. buf += 0x800;
  33. len -= 0x800;
  34. }
  35. memcpy_fromio(buf, chip->legacy.IO_ADDR_R, len);
  36. }
  37. static void pasemi_write_buf(struct nand_chip *chip, const u_char *buf,
  38. int len)
  39. {
  40. while (len > 0x800) {
  41. memcpy_toio(chip->legacy.IO_ADDR_R, buf, 0x800);
  42. buf += 0x800;
  43. len -= 0x800;
  44. }
  45. memcpy_toio(chip->legacy.IO_ADDR_R, buf, len);
  46. }
  47. static void pasemi_hwcontrol(struct nand_chip *chip, int cmd,
  48. unsigned int ctrl)
  49. {
  50. if (cmd == NAND_CMD_NONE)
  51. return;
  52. if (ctrl & NAND_CLE)
  53. out_8(chip->legacy.IO_ADDR_W + (1 << CLE_PIN_CTL), cmd);
  54. else
  55. out_8(chip->legacy.IO_ADDR_W + (1 << ALE_PIN_CTL), cmd);
  56. /* Push out posted writes */
  57. eieio();
  58. inl(lpcctl);
  59. }
  60. static int pasemi_device_ready(struct nand_chip *chip)
  61. {
  62. return !!(inl(lpcctl) & LBICTRL_LPCCTL_NR);
  63. }
  64. static int pasemi_attach_chip(struct nand_chip *chip)
  65. {
  66. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
  67. chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
  68. chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
  69. return 0;
  70. }
  71. static const struct nand_controller_ops pasemi_ops = {
  72. .attach_chip = pasemi_attach_chip,
  73. };
  74. static int pasemi_nand_probe(struct platform_device *ofdev)
  75. {
  76. struct device *dev = &ofdev->dev;
  77. struct pci_dev *pdev;
  78. struct device_node *np = dev->of_node;
  79. struct resource res;
  80. struct nand_chip *chip;
  81. int err = 0;
  82. err = of_address_to_resource(np, 0, &res);
  83. if (err)
  84. return -EINVAL;
  85. /* We only support one device at the moment */
  86. if (pasemi_nand_mtd)
  87. return -ENODEV;
  88. dev_dbg(dev, "pasemi_nand at %pR\n", &res);
  89. /* Allocate memory for MTD device structure and private data */
  90. chip = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
  91. if (!chip) {
  92. err = -ENOMEM;
  93. goto out;
  94. }
  95. controller.ops = &pasemi_ops;
  96. nand_controller_init(&controller);
  97. chip->controller = &controller;
  98. pasemi_nand_mtd = nand_to_mtd(chip);
  99. /* Link the private data with the MTD structure */
  100. pasemi_nand_mtd->dev.parent = dev;
  101. chip->legacy.IO_ADDR_R = of_iomap(np, 0);
  102. chip->legacy.IO_ADDR_W = chip->legacy.IO_ADDR_R;
  103. if (!chip->legacy.IO_ADDR_R) {
  104. err = -EIO;
  105. goto out_mtd;
  106. }
  107. pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa008, NULL);
  108. if (!pdev) {
  109. err = -ENODEV;
  110. goto out_ior;
  111. }
  112. lpcctl = pci_resource_start(pdev, 0);
  113. pci_dev_put(pdev);
  114. if (!request_region(lpcctl, 4, driver_name)) {
  115. err = -EBUSY;
  116. goto out_ior;
  117. }
  118. chip->legacy.cmd_ctrl = pasemi_hwcontrol;
  119. chip->legacy.dev_ready = pasemi_device_ready;
  120. chip->legacy.read_buf = pasemi_read_buf;
  121. chip->legacy.write_buf = pasemi_write_buf;
  122. chip->legacy.chip_delay = 0;
  123. /* Enable the following for a flash based bad block table */
  124. chip->bbt_options = NAND_BBT_USE_FLASH;
  125. /*
  126. * This driver assumes that the default ECC engine should be TYPE_SOFT.
  127. * Set ->engine_type before registering the NAND devices in order to
  128. * provide a driver specific default value.
  129. */
  130. chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
  131. /* Scan to find existence of the device */
  132. err = nand_scan(chip, 1);
  133. if (err)
  134. goto out_lpc;
  135. if (mtd_device_register(pasemi_nand_mtd, NULL, 0)) {
  136. dev_err(dev, "Unable to register MTD device\n");
  137. err = -ENODEV;
  138. goto out_cleanup_nand;
  139. }
  140. dev_info(dev, "PA Semi NAND flash at %pR, control at I/O %x\n", &res,
  141. lpcctl);
  142. return 0;
  143. out_cleanup_nand:
  144. nand_cleanup(chip);
  145. out_lpc:
  146. release_region(lpcctl, 4);
  147. out_ior:
  148. iounmap(chip->legacy.IO_ADDR_R);
  149. out_mtd:
  150. kfree(chip);
  151. out:
  152. return err;
  153. }
  154. static int pasemi_nand_remove(struct platform_device *ofdev)
  155. {
  156. struct nand_chip *chip;
  157. int ret;
  158. if (!pasemi_nand_mtd)
  159. return 0;
  160. chip = mtd_to_nand(pasemi_nand_mtd);
  161. /* Release resources, unregister device */
  162. ret = mtd_device_unregister(pasemi_nand_mtd);
  163. WARN_ON(ret);
  164. nand_cleanup(chip);
  165. release_region(lpcctl, 4);
  166. iounmap(chip->legacy.IO_ADDR_R);
  167. /* Free the MTD device structure */
  168. kfree(chip);
  169. pasemi_nand_mtd = NULL;
  170. return 0;
  171. }
  172. static const struct of_device_id pasemi_nand_match[] =
  173. {
  174. {
  175. .compatible = "pasemi,localbus-nand",
  176. },
  177. {},
  178. };
  179. MODULE_DEVICE_TABLE(of, pasemi_nand_match);
  180. static struct platform_driver pasemi_nand_driver =
  181. {
  182. .driver = {
  183. .name = driver_name,
  184. .of_match_table = pasemi_nand_match,
  185. },
  186. .probe = pasemi_nand_probe,
  187. .remove = pasemi_nand_remove,
  188. };
  189. module_platform_driver(pasemi_nand_driver);
  190. MODULE_LICENSE("GPL");
  191. MODULE_AUTHOR("Egor Martovetsky <[email protected]>");
  192. MODULE_DESCRIPTION("NAND flash interface driver for PA Semi PWRficient");