fsl_ifc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2011 Freescale Semiconductor, Inc
  4. *
  5. * Freescale Integrated Flash Controller
  6. *
  7. * Author: Dipen Dudhat <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/compiler.h>
  12. #include <linux/sched.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/types.h>
  15. #include <linux/slab.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/fsl_ifc.h>
  21. #include <linux/irqdomain.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. struct fsl_ifc_ctrl *fsl_ifc_ctrl_dev;
  25. EXPORT_SYMBOL(fsl_ifc_ctrl_dev);
  26. /*
  27. * convert_ifc_address - convert the base address
  28. * @addr_base: base address of the memory bank
  29. */
  30. unsigned int convert_ifc_address(phys_addr_t addr_base)
  31. {
  32. return addr_base & CSPR_BA;
  33. }
  34. EXPORT_SYMBOL(convert_ifc_address);
  35. /*
  36. * fsl_ifc_find - find IFC bank
  37. * @addr_base: base address of the memory bank
  38. *
  39. * This function walks IFC banks comparing "Base address" field of the CSPR
  40. * registers with the supplied addr_base argument. When bases match this
  41. * function returns bank number (starting with 0), otherwise it returns
  42. * appropriate errno value.
  43. */
  44. int fsl_ifc_find(phys_addr_t addr_base)
  45. {
  46. int i = 0;
  47. if (!fsl_ifc_ctrl_dev || !fsl_ifc_ctrl_dev->gregs)
  48. return -ENODEV;
  49. for (i = 0; i < fsl_ifc_ctrl_dev->banks; i++) {
  50. u32 cspr = ifc_in32(&fsl_ifc_ctrl_dev->gregs->cspr_cs[i].cspr);
  51. if (cspr & CSPR_V && (cspr & CSPR_BA) ==
  52. convert_ifc_address(addr_base))
  53. return i;
  54. }
  55. return -ENOENT;
  56. }
  57. EXPORT_SYMBOL(fsl_ifc_find);
  58. static int fsl_ifc_ctrl_init(struct fsl_ifc_ctrl *ctrl)
  59. {
  60. struct fsl_ifc_global __iomem *ifc = ctrl->gregs;
  61. /*
  62. * Clear all the common status and event registers
  63. */
  64. if (ifc_in32(&ifc->cm_evter_stat) & IFC_CM_EVTER_STAT_CSER)
  65. ifc_out32(IFC_CM_EVTER_STAT_CSER, &ifc->cm_evter_stat);
  66. /* enable all error and events */
  67. ifc_out32(IFC_CM_EVTER_EN_CSEREN, &ifc->cm_evter_en);
  68. /* enable all error and event interrupts */
  69. ifc_out32(IFC_CM_EVTER_INTR_EN_CSERIREN, &ifc->cm_evter_intr_en);
  70. ifc_out32(0x0, &ifc->cm_erattr0);
  71. ifc_out32(0x0, &ifc->cm_erattr1);
  72. return 0;
  73. }
  74. static int fsl_ifc_ctrl_remove(struct platform_device *dev)
  75. {
  76. struct fsl_ifc_ctrl *ctrl = dev_get_drvdata(&dev->dev);
  77. of_platform_depopulate(&dev->dev);
  78. free_irq(ctrl->nand_irq, ctrl);
  79. free_irq(ctrl->irq, ctrl);
  80. irq_dispose_mapping(ctrl->nand_irq);
  81. irq_dispose_mapping(ctrl->irq);
  82. iounmap(ctrl->gregs);
  83. dev_set_drvdata(&dev->dev, NULL);
  84. return 0;
  85. }
  86. /*
  87. * NAND events are split between an operational interrupt which only
  88. * receives OPC, and an error interrupt that receives everything else,
  89. * including non-NAND errors. Whichever interrupt gets to it first
  90. * records the status and wakes the wait queue.
  91. */
  92. static DEFINE_SPINLOCK(nand_irq_lock);
  93. static u32 check_nand_stat(struct fsl_ifc_ctrl *ctrl)
  94. {
  95. struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
  96. unsigned long flags;
  97. u32 stat;
  98. spin_lock_irqsave(&nand_irq_lock, flags);
  99. stat = ifc_in32(&ifc->ifc_nand.nand_evter_stat);
  100. if (stat) {
  101. ifc_out32(stat, &ifc->ifc_nand.nand_evter_stat);
  102. ctrl->nand_stat = stat;
  103. wake_up(&ctrl->nand_wait);
  104. }
  105. spin_unlock_irqrestore(&nand_irq_lock, flags);
  106. return stat;
  107. }
  108. static irqreturn_t fsl_ifc_nand_irq(int irqno, void *data)
  109. {
  110. struct fsl_ifc_ctrl *ctrl = data;
  111. if (check_nand_stat(ctrl))
  112. return IRQ_HANDLED;
  113. return IRQ_NONE;
  114. }
  115. /*
  116. * NOTE: This interrupt is used to report ifc events of various kinds,
  117. * such as transaction errors on the chipselects.
  118. */
  119. static irqreturn_t fsl_ifc_ctrl_irq(int irqno, void *data)
  120. {
  121. struct fsl_ifc_ctrl *ctrl = data;
  122. struct fsl_ifc_global __iomem *ifc = ctrl->gregs;
  123. u32 err_axiid, err_srcid, status, cs_err, err_addr;
  124. irqreturn_t ret = IRQ_NONE;
  125. /* read for chip select error */
  126. cs_err = ifc_in32(&ifc->cm_evter_stat);
  127. if (cs_err) {
  128. dev_err(ctrl->dev, "transaction sent to IFC is not mapped to any memory bank 0x%08X\n",
  129. cs_err);
  130. /* clear the chip select error */
  131. ifc_out32(IFC_CM_EVTER_STAT_CSER, &ifc->cm_evter_stat);
  132. /* read error attribute registers print the error information */
  133. status = ifc_in32(&ifc->cm_erattr0);
  134. err_addr = ifc_in32(&ifc->cm_erattr1);
  135. if (status & IFC_CM_ERATTR0_ERTYP_READ)
  136. dev_err(ctrl->dev, "Read transaction error CM_ERATTR0 0x%08X\n",
  137. status);
  138. else
  139. dev_err(ctrl->dev, "Write transaction error CM_ERATTR0 0x%08X\n",
  140. status);
  141. err_axiid = (status & IFC_CM_ERATTR0_ERAID) >>
  142. IFC_CM_ERATTR0_ERAID_SHIFT;
  143. dev_err(ctrl->dev, "AXI ID of the error transaction 0x%08X\n",
  144. err_axiid);
  145. err_srcid = (status & IFC_CM_ERATTR0_ESRCID) >>
  146. IFC_CM_ERATTR0_ESRCID_SHIFT;
  147. dev_err(ctrl->dev, "SRC ID of the error transaction 0x%08X\n",
  148. err_srcid);
  149. dev_err(ctrl->dev, "Transaction Address corresponding to error ERADDR 0x%08X\n",
  150. err_addr);
  151. ret = IRQ_HANDLED;
  152. }
  153. if (check_nand_stat(ctrl))
  154. ret = IRQ_HANDLED;
  155. return ret;
  156. }
  157. /*
  158. * fsl_ifc_ctrl_probe
  159. *
  160. * called by device layer when it finds a device matching
  161. * one our driver can handled. This code allocates all of
  162. * the resources needed for the controller only. The
  163. * resources for the NAND banks themselves are allocated
  164. * in the chip probe function.
  165. */
  166. static int fsl_ifc_ctrl_probe(struct platform_device *dev)
  167. {
  168. int ret = 0;
  169. int version, banks;
  170. void __iomem *addr;
  171. dev_info(&dev->dev, "Freescale Integrated Flash Controller\n");
  172. fsl_ifc_ctrl_dev = devm_kzalloc(&dev->dev, sizeof(*fsl_ifc_ctrl_dev),
  173. GFP_KERNEL);
  174. if (!fsl_ifc_ctrl_dev)
  175. return -ENOMEM;
  176. dev_set_drvdata(&dev->dev, fsl_ifc_ctrl_dev);
  177. /* IOMAP the entire IFC region */
  178. fsl_ifc_ctrl_dev->gregs = of_iomap(dev->dev.of_node, 0);
  179. if (!fsl_ifc_ctrl_dev->gregs) {
  180. dev_err(&dev->dev, "failed to get memory region\n");
  181. return -ENODEV;
  182. }
  183. if (of_property_read_bool(dev->dev.of_node, "little-endian")) {
  184. fsl_ifc_ctrl_dev->little_endian = true;
  185. dev_dbg(&dev->dev, "IFC REGISTERS are LITTLE endian\n");
  186. } else {
  187. fsl_ifc_ctrl_dev->little_endian = false;
  188. dev_dbg(&dev->dev, "IFC REGISTERS are BIG endian\n");
  189. }
  190. version = ifc_in32(&fsl_ifc_ctrl_dev->gregs->ifc_rev) &
  191. FSL_IFC_VERSION_MASK;
  192. banks = (version == FSL_IFC_VERSION_1_0_0) ? 4 : 8;
  193. dev_info(&dev->dev, "IFC version %d.%d, %d banks\n",
  194. version >> 24, (version >> 16) & 0xf, banks);
  195. fsl_ifc_ctrl_dev->version = version;
  196. fsl_ifc_ctrl_dev->banks = banks;
  197. addr = fsl_ifc_ctrl_dev->gregs;
  198. if (version >= FSL_IFC_VERSION_2_0_0)
  199. addr += PGOFFSET_64K;
  200. else
  201. addr += PGOFFSET_4K;
  202. fsl_ifc_ctrl_dev->rregs = addr;
  203. /* get the Controller level irq */
  204. fsl_ifc_ctrl_dev->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
  205. if (fsl_ifc_ctrl_dev->irq == 0) {
  206. dev_err(&dev->dev, "failed to get irq resource for IFC\n");
  207. ret = -ENODEV;
  208. goto err;
  209. }
  210. /* get the nand machine irq */
  211. fsl_ifc_ctrl_dev->nand_irq =
  212. irq_of_parse_and_map(dev->dev.of_node, 1);
  213. fsl_ifc_ctrl_dev->dev = &dev->dev;
  214. ret = fsl_ifc_ctrl_init(fsl_ifc_ctrl_dev);
  215. if (ret < 0)
  216. goto err_unmap_nandirq;
  217. init_waitqueue_head(&fsl_ifc_ctrl_dev->nand_wait);
  218. ret = request_irq(fsl_ifc_ctrl_dev->irq, fsl_ifc_ctrl_irq, IRQF_SHARED,
  219. "fsl-ifc", fsl_ifc_ctrl_dev);
  220. if (ret != 0) {
  221. dev_err(&dev->dev, "failed to install irq (%d)\n",
  222. fsl_ifc_ctrl_dev->irq);
  223. goto err_unmap_nandirq;
  224. }
  225. if (fsl_ifc_ctrl_dev->nand_irq) {
  226. ret = request_irq(fsl_ifc_ctrl_dev->nand_irq, fsl_ifc_nand_irq,
  227. 0, "fsl-ifc-nand", fsl_ifc_ctrl_dev);
  228. if (ret != 0) {
  229. dev_err(&dev->dev, "failed to install irq (%d)\n",
  230. fsl_ifc_ctrl_dev->nand_irq);
  231. goto err_free_irq;
  232. }
  233. }
  234. /* legacy dts may still use "simple-bus" compatible */
  235. ret = of_platform_default_populate(dev->dev.of_node, NULL, &dev->dev);
  236. if (ret)
  237. goto err_free_nandirq;
  238. return 0;
  239. err_free_nandirq:
  240. free_irq(fsl_ifc_ctrl_dev->nand_irq, fsl_ifc_ctrl_dev);
  241. err_free_irq:
  242. free_irq(fsl_ifc_ctrl_dev->irq, fsl_ifc_ctrl_dev);
  243. err_unmap_nandirq:
  244. irq_dispose_mapping(fsl_ifc_ctrl_dev->nand_irq);
  245. irq_dispose_mapping(fsl_ifc_ctrl_dev->irq);
  246. err:
  247. iounmap(fsl_ifc_ctrl_dev->gregs);
  248. return ret;
  249. }
  250. static const struct of_device_id fsl_ifc_match[] = {
  251. {
  252. .compatible = "fsl,ifc",
  253. },
  254. {},
  255. };
  256. static struct platform_driver fsl_ifc_ctrl_driver = {
  257. .driver = {
  258. .name = "fsl-ifc",
  259. .of_match_table = fsl_ifc_match,
  260. },
  261. .probe = fsl_ifc_ctrl_probe,
  262. .remove = fsl_ifc_ctrl_remove,
  263. };
  264. static int __init fsl_ifc_init(void)
  265. {
  266. return platform_driver_register(&fsl_ifc_ctrl_driver);
  267. }
  268. subsys_initcall(fsl_ifc_init);
  269. MODULE_LICENSE("GPL");
  270. MODULE_AUTHOR("Freescale Semiconductor");
  271. MODULE_DESCRIPTION("Freescale Integrated Flash Controller driver");