fsl_ifc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <Dipen.Dudhat@freescale.com>
  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_platform.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 void 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. }
  85. /*
  86. * NAND events are split between an operational interrupt which only
  87. * receives OPC, and an error interrupt that receives everything else,
  88. * including non-NAND errors. Whichever interrupt gets to it first
  89. * records the status and wakes the wait queue.
  90. */
  91. static DEFINE_SPINLOCK(nand_irq_lock);
  92. static u32 check_nand_stat(struct fsl_ifc_ctrl *ctrl)
  93. {
  94. struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
  95. unsigned long flags;
  96. u32 stat;
  97. spin_lock_irqsave(&nand_irq_lock, flags);
  98. stat = ifc_in32(&ifc->ifc_nand.nand_evter_stat);
  99. if (stat) {
  100. ifc_out32(stat, &ifc->ifc_nand.nand_evter_stat);
  101. ctrl->nand_stat = stat;
  102. wake_up(&ctrl->nand_wait);
  103. }
  104. spin_unlock_irqrestore(&nand_irq_lock, flags);
  105. return stat;
  106. }
  107. static irqreturn_t fsl_ifc_nand_irq(int irqno, void *data)
  108. {
  109. struct fsl_ifc_ctrl *ctrl = data;
  110. if (check_nand_stat(ctrl))
  111. return IRQ_HANDLED;
  112. return IRQ_NONE;
  113. }
  114. /*
  115. * NOTE: This interrupt is used to report ifc events of various kinds,
  116. * such as transaction errors on the chipselects.
  117. */
  118. static irqreturn_t fsl_ifc_ctrl_irq(int irqno, void *data)
  119. {
  120. struct fsl_ifc_ctrl *ctrl = data;
  121. struct fsl_ifc_global __iomem *ifc = ctrl->gregs;
  122. u32 err_axiid, err_srcid, status, cs_err, err_addr;
  123. irqreturn_t ret = IRQ_NONE;
  124. /* read for chip select error */
  125. cs_err = ifc_in32(&ifc->cm_evter_stat);
  126. if (cs_err) {
  127. dev_err(ctrl->dev, "transaction sent to IFC is not mapped to any memory bank 0x%08X\n",
  128. cs_err);
  129. /* clear the chip select error */
  130. ifc_out32(IFC_CM_EVTER_STAT_CSER, &ifc->cm_evter_stat);
  131. /* read error attribute registers print the error information */
  132. status = ifc_in32(&ifc->cm_erattr0);
  133. err_addr = ifc_in32(&ifc->cm_erattr1);
  134. if (status & IFC_CM_ERATTR0_ERTYP_READ)
  135. dev_err(ctrl->dev, "Read transaction error CM_ERATTR0 0x%08X\n",
  136. status);
  137. else
  138. dev_err(ctrl->dev, "Write transaction error CM_ERATTR0 0x%08X\n",
  139. status);
  140. err_axiid = (status & IFC_CM_ERATTR0_ERAID) >>
  141. IFC_CM_ERATTR0_ERAID_SHIFT;
  142. dev_err(ctrl->dev, "AXI ID of the error transaction 0x%08X\n",
  143. err_axiid);
  144. err_srcid = (status & IFC_CM_ERATTR0_ESRCID) >>
  145. IFC_CM_ERATTR0_ESRCID_SHIFT;
  146. dev_err(ctrl->dev, "SRC ID of the error transaction 0x%08X\n",
  147. err_srcid);
  148. dev_err(ctrl->dev, "Transaction Address corresponding to error ERADDR 0x%08X\n",
  149. err_addr);
  150. ret = IRQ_HANDLED;
  151. }
  152. if (check_nand_stat(ctrl))
  153. ret = IRQ_HANDLED;
  154. return ret;
  155. }
  156. /*
  157. * fsl_ifc_ctrl_probe
  158. *
  159. * called by device layer when it finds a device matching
  160. * one our driver can handled. This code allocates all of
  161. * the resources needed for the controller only. The
  162. * resources for the NAND banks themselves are allocated
  163. * in the chip probe function.
  164. */
  165. static int fsl_ifc_ctrl_probe(struct platform_device *dev)
  166. {
  167. int ret = 0;
  168. int version, banks;
  169. void __iomem *addr;
  170. dev_info(&dev->dev, "Freescale Integrated Flash Controller\n");
  171. fsl_ifc_ctrl_dev = devm_kzalloc(&dev->dev, sizeof(*fsl_ifc_ctrl_dev),
  172. GFP_KERNEL);
  173. if (!fsl_ifc_ctrl_dev)
  174. return -ENOMEM;
  175. dev_set_drvdata(&dev->dev, fsl_ifc_ctrl_dev);
  176. /* IOMAP the entire IFC region */
  177. fsl_ifc_ctrl_dev->gregs = of_iomap(dev->dev.of_node, 0);
  178. if (!fsl_ifc_ctrl_dev->gregs) {
  179. dev_err(&dev->dev, "failed to get memory region\n");
  180. return -ENODEV;
  181. }
  182. if (of_property_read_bool(dev->dev.of_node, "little-endian")) {
  183. fsl_ifc_ctrl_dev->little_endian = true;
  184. dev_dbg(&dev->dev, "IFC REGISTERS are LITTLE endian\n");
  185. } else {
  186. fsl_ifc_ctrl_dev->little_endian = false;
  187. dev_dbg(&dev->dev, "IFC REGISTERS are BIG endian\n");
  188. }
  189. version = ifc_in32(&fsl_ifc_ctrl_dev->gregs->ifc_rev) &
  190. FSL_IFC_VERSION_MASK;
  191. banks = (version == FSL_IFC_VERSION_1_0_0) ? 4 : 8;
  192. dev_info(&dev->dev, "IFC version %d.%d, %d banks\n",
  193. version >> 24, (version >> 16) & 0xf, banks);
  194. fsl_ifc_ctrl_dev->version = version;
  195. fsl_ifc_ctrl_dev->banks = banks;
  196. addr = fsl_ifc_ctrl_dev->gregs;
  197. if (version >= FSL_IFC_VERSION_2_0_0)
  198. addr += PGOFFSET_64K;
  199. else
  200. addr += PGOFFSET_4K;
  201. fsl_ifc_ctrl_dev->rregs = addr;
  202. /* get the Controller level irq */
  203. fsl_ifc_ctrl_dev->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
  204. if (fsl_ifc_ctrl_dev->irq == 0) {
  205. dev_err(&dev->dev, "failed to get irq resource for IFC\n");
  206. ret = -ENODEV;
  207. goto err;
  208. }
  209. /* get the nand machine irq */
  210. fsl_ifc_ctrl_dev->nand_irq =
  211. irq_of_parse_and_map(dev->dev.of_node, 1);
  212. fsl_ifc_ctrl_dev->dev = &dev->dev;
  213. ret = fsl_ifc_ctrl_init(fsl_ifc_ctrl_dev);
  214. if (ret < 0)
  215. goto err_unmap_nandirq;
  216. init_waitqueue_head(&fsl_ifc_ctrl_dev->nand_wait);
  217. ret = request_irq(fsl_ifc_ctrl_dev->irq, fsl_ifc_ctrl_irq, IRQF_SHARED,
  218. "fsl-ifc", fsl_ifc_ctrl_dev);
  219. if (ret != 0) {
  220. dev_err(&dev->dev, "failed to install irq (%d)\n",
  221. fsl_ifc_ctrl_dev->irq);
  222. goto err_unmap_nandirq;
  223. }
  224. if (fsl_ifc_ctrl_dev->nand_irq) {
  225. ret = request_irq(fsl_ifc_ctrl_dev->nand_irq, fsl_ifc_nand_irq,
  226. 0, "fsl-ifc-nand", fsl_ifc_ctrl_dev);
  227. if (ret != 0) {
  228. dev_err(&dev->dev, "failed to install irq (%d)\n",
  229. fsl_ifc_ctrl_dev->nand_irq);
  230. goto err_free_irq;
  231. }
  232. }
  233. /* legacy dts may still use "simple-bus" compatible */
  234. ret = of_platform_default_populate(dev->dev.of_node, NULL, &dev->dev);
  235. if (ret)
  236. goto err_free_nandirq;
  237. return 0;
  238. err_free_nandirq:
  239. free_irq(fsl_ifc_ctrl_dev->nand_irq, fsl_ifc_ctrl_dev);
  240. err_free_irq:
  241. free_irq(fsl_ifc_ctrl_dev->irq, fsl_ifc_ctrl_dev);
  242. err_unmap_nandirq:
  243. irq_dispose_mapping(fsl_ifc_ctrl_dev->nand_irq);
  244. irq_dispose_mapping(fsl_ifc_ctrl_dev->irq);
  245. err:
  246. iounmap(fsl_ifc_ctrl_dev->gregs);
  247. return ret;
  248. }
  249. static const struct of_device_id fsl_ifc_match[] = {
  250. {
  251. .compatible = "fsl,ifc",
  252. },
  253. {},
  254. };
  255. static struct platform_driver fsl_ifc_ctrl_driver = {
  256. .driver = {
  257. .name = "fsl-ifc",
  258. .of_match_table = fsl_ifc_match,
  259. },
  260. .probe = fsl_ifc_ctrl_probe,
  261. .remove_new = fsl_ifc_ctrl_remove,
  262. };
  263. static int __init fsl_ifc_init(void)
  264. {
  265. return platform_driver_register(&fsl_ifc_ctrl_driver);
  266. }
  267. subsys_initcall(fsl_ifc_init);
  268. MODULE_AUTHOR("Freescale Semiconductor");
  269. MODULE_DESCRIPTION("Freescale Integrated Flash Controller driver");