fsl_ifc.c 8.3 KB

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