fsl_ifc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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/sched.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 <linux/fsl_ifc.h>
  34. #include <asm/prom.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 < fsl_ifc_ctrl_dev->banks; i++) {
  61. u32 cspr = ifc_in32(&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 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 (ifc_in32(&ifc->cm_evter_stat) & IFC_CM_EVTER_STAT_CSER)
  76. ifc_out32(IFC_CM_EVTER_STAT_CSER, &ifc->cm_evter_stat);
  77. /* enable all error and events */
  78. ifc_out32(IFC_CM_EVTER_EN_CSEREN, &ifc->cm_evter_en);
  79. /* enable all error and event interrupts */
  80. ifc_out32(IFC_CM_EVTER_INTR_EN_CSERIREN, &ifc->cm_evter_intr_en);
  81. ifc_out32(0x0, &ifc->cm_erattr0);
  82. ifc_out32(0x0, &ifc->cm_erattr1);
  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 = ifc_in32(&ifc->ifc_nand.nand_evter_stat);
  111. if (stat) {
  112. ifc_out32(stat, &ifc->ifc_nand.nand_evter_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 = ifc_in32(&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. ifc_out32(IFC_CM_EVTER_STAT_CSER, &ifc->cm_evter_stat);
  143. /* read error attribute registers print the error information */
  144. status = ifc_in32(&ifc->cm_erattr0);
  145. err_addr = ifc_in32(&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 fsl_ifc_ctrl_probe(struct platform_device *dev)
  178. {
  179. int ret = 0;
  180. int version, banks;
  181. dev_info(&dev->dev, "Freescale Integrated Flash Controller\n");
  182. fsl_ifc_ctrl_dev = kzalloc(sizeof(*fsl_ifc_ctrl_dev), GFP_KERNEL);
  183. if (!fsl_ifc_ctrl_dev)
  184. return -ENOMEM;
  185. dev_set_drvdata(&dev->dev, fsl_ifc_ctrl_dev);
  186. /* IOMAP the entire IFC region */
  187. fsl_ifc_ctrl_dev->regs = of_iomap(dev->dev.of_node, 0);
  188. if (!fsl_ifc_ctrl_dev->regs) {
  189. dev_err(&dev->dev, "failed to get memory region\n");
  190. ret = -ENODEV;
  191. goto err;
  192. }
  193. version = ifc_in32(&fsl_ifc_ctrl_dev->regs->ifc_rev) &
  194. FSL_IFC_VERSION_MASK;
  195. banks = (version == FSL_IFC_VERSION_1_0_0) ? 4 : 8;
  196. dev_info(&dev->dev, "IFC version %d.%d, %d banks\n",
  197. version >> 24, (version >> 16) & 0xf, banks);
  198. fsl_ifc_ctrl_dev->version = version;
  199. fsl_ifc_ctrl_dev->banks = banks;
  200. if (of_property_read_bool(dev->dev.of_node, "little-endian")) {
  201. fsl_ifc_ctrl_dev->little_endian = true;
  202. dev_dbg(&dev->dev, "IFC REGISTERS are LITTLE endian\n");
  203. } else {
  204. fsl_ifc_ctrl_dev->little_endian = false;
  205. dev_dbg(&dev->dev, "IFC REGISTERS are BIG endian\n");
  206. }
  207. version = ioread32be(&fsl_ifc_ctrl_dev->regs->ifc_rev) &
  208. FSL_IFC_VERSION_MASK;
  209. banks = (version == FSL_IFC_VERSION_1_0_0) ? 4 : 8;
  210. dev_info(&dev->dev, "IFC version %d.%d, %d banks\n",
  211. version >> 24, (version >> 16) & 0xf, banks);
  212. fsl_ifc_ctrl_dev->version = version;
  213. fsl_ifc_ctrl_dev->banks = banks;
  214. /* get the Controller level irq */
  215. fsl_ifc_ctrl_dev->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
  216. if (fsl_ifc_ctrl_dev->irq == NO_IRQ) {
  217. dev_err(&dev->dev, "failed to get irq resource "
  218. "for IFC\n");
  219. ret = -ENODEV;
  220. goto err;
  221. }
  222. /* get the nand machine irq */
  223. fsl_ifc_ctrl_dev->nand_irq =
  224. irq_of_parse_and_map(dev->dev.of_node, 1);
  225. fsl_ifc_ctrl_dev->dev = &dev->dev;
  226. ret = fsl_ifc_ctrl_init(fsl_ifc_ctrl_dev);
  227. if (ret < 0)
  228. goto err;
  229. init_waitqueue_head(&fsl_ifc_ctrl_dev->nand_wait);
  230. ret = request_irq(fsl_ifc_ctrl_dev->irq, fsl_ifc_ctrl_irq, IRQF_SHARED,
  231. "fsl-ifc", fsl_ifc_ctrl_dev);
  232. if (ret != 0) {
  233. dev_err(&dev->dev, "failed to install irq (%d)\n",
  234. fsl_ifc_ctrl_dev->irq);
  235. goto err_irq;
  236. }
  237. if (fsl_ifc_ctrl_dev->nand_irq) {
  238. ret = request_irq(fsl_ifc_ctrl_dev->nand_irq, fsl_ifc_nand_irq,
  239. 0, "fsl-ifc-nand", fsl_ifc_ctrl_dev);
  240. if (ret != 0) {
  241. dev_err(&dev->dev, "failed to install irq (%d)\n",
  242. fsl_ifc_ctrl_dev->nand_irq);
  243. goto err_nandirq;
  244. }
  245. }
  246. return 0;
  247. err_nandirq:
  248. free_irq(fsl_ifc_ctrl_dev->nand_irq, fsl_ifc_ctrl_dev);
  249. irq_dispose_mapping(fsl_ifc_ctrl_dev->nand_irq);
  250. err_irq:
  251. free_irq(fsl_ifc_ctrl_dev->irq, fsl_ifc_ctrl_dev);
  252. irq_dispose_mapping(fsl_ifc_ctrl_dev->irq);
  253. err:
  254. return ret;
  255. }
  256. static const struct of_device_id fsl_ifc_match[] = {
  257. {
  258. .compatible = "fsl,ifc",
  259. },
  260. {},
  261. };
  262. static struct platform_driver fsl_ifc_ctrl_driver = {
  263. .driver = {
  264. .name = "fsl-ifc",
  265. .of_match_table = fsl_ifc_match,
  266. },
  267. .probe = fsl_ifc_ctrl_probe,
  268. .remove = fsl_ifc_ctrl_remove,
  269. };
  270. static int __init fsl_ifc_init(void)
  271. {
  272. return platform_driver_register(&fsl_ifc_ctrl_driver);
  273. }
  274. subsys_initcall(fsl_ifc_init);
  275. MODULE_LICENSE("GPL");
  276. MODULE_AUTHOR("Freescale Semiconductor");
  277. MODULE_DESCRIPTION("Freescale Integrated Flash Controller driver");