fsl_ifc.c 8.1 KB

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