fsl_lbc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Freescale LBC and UPM routines.
  3. *
  4. * Copyright © 2007-2008 MontaVista Software, Inc.
  5. * Copyright © 2010 Freescale Semiconductor
  6. *
  7. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  8. * Author: Jack Lan <Jack.Lan@freescale.com>
  9. * Author: Roy Zang <tie-fei.zang@freescale.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/export.h>
  18. #include <linux/kernel.h>
  19. #include <linux/compiler.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/types.h>
  22. #include <linux/io.h>
  23. #include <linux/of.h>
  24. #include <linux/slab.h>
  25. #include <linux/sched.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/mod_devicetable.h>
  29. #include <asm/prom.h>
  30. #include <asm/fsl_lbc.h>
  31. static spinlock_t fsl_lbc_lock = __SPIN_LOCK_UNLOCKED(fsl_lbc_lock);
  32. struct fsl_lbc_ctrl *fsl_lbc_ctrl_dev;
  33. EXPORT_SYMBOL(fsl_lbc_ctrl_dev);
  34. /**
  35. * fsl_lbc_addr - convert the base address
  36. * @addr_base: base address of the memory bank
  37. *
  38. * This function converts a base address of lbc into the right format for the
  39. * BR register. If the SOC has eLBC then it returns 32bit physical address
  40. * else it convers a 34bit local bus physical address to correct format of
  41. * 32bit address for BR register (Example: MPC8641).
  42. */
  43. u32 fsl_lbc_addr(phys_addr_t addr_base)
  44. {
  45. struct device_node *np = fsl_lbc_ctrl_dev->dev->of_node;
  46. u32 addr = addr_base & 0xffff8000;
  47. if (of_device_is_compatible(np, "fsl,elbc"))
  48. return addr;
  49. return addr | ((addr_base & 0x300000000ull) >> 19);
  50. }
  51. EXPORT_SYMBOL(fsl_lbc_addr);
  52. /**
  53. * fsl_lbc_find - find Localbus bank
  54. * @addr_base: base address of the memory bank
  55. *
  56. * This function walks LBC banks comparing "Base address" field of the BR
  57. * registers with the supplied addr_base argument. When bases match this
  58. * function returns bank number (starting with 0), otherwise it returns
  59. * appropriate errno value.
  60. */
  61. int fsl_lbc_find(phys_addr_t addr_base)
  62. {
  63. int i;
  64. struct fsl_lbc_regs __iomem *lbc;
  65. if (!fsl_lbc_ctrl_dev || !fsl_lbc_ctrl_dev->regs)
  66. return -ENODEV;
  67. lbc = fsl_lbc_ctrl_dev->regs;
  68. for (i = 0; i < ARRAY_SIZE(lbc->bank); i++) {
  69. __be32 br = in_be32(&lbc->bank[i].br);
  70. __be32 or = in_be32(&lbc->bank[i].or);
  71. if (br & BR_V && (br & or & BR_BA) == fsl_lbc_addr(addr_base))
  72. return i;
  73. }
  74. return -ENOENT;
  75. }
  76. EXPORT_SYMBOL(fsl_lbc_find);
  77. /**
  78. * fsl_upm_find - find pre-programmed UPM via base address
  79. * @addr_base: base address of the memory bank controlled by the UPM
  80. * @upm: pointer to the allocated fsl_upm structure
  81. *
  82. * This function fills fsl_upm structure so you can use it with the rest of
  83. * UPM API. On success this function returns 0, otherwise it returns
  84. * appropriate errno value.
  85. */
  86. int fsl_upm_find(phys_addr_t addr_base, struct fsl_upm *upm)
  87. {
  88. int bank;
  89. __be32 br;
  90. struct fsl_lbc_regs __iomem *lbc;
  91. bank = fsl_lbc_find(addr_base);
  92. if (bank < 0)
  93. return bank;
  94. if (!fsl_lbc_ctrl_dev || !fsl_lbc_ctrl_dev->regs)
  95. return -ENODEV;
  96. lbc = fsl_lbc_ctrl_dev->regs;
  97. br = in_be32(&lbc->bank[bank].br);
  98. switch (br & BR_MSEL) {
  99. case BR_MS_UPMA:
  100. upm->mxmr = &lbc->mamr;
  101. break;
  102. case BR_MS_UPMB:
  103. upm->mxmr = &lbc->mbmr;
  104. break;
  105. case BR_MS_UPMC:
  106. upm->mxmr = &lbc->mcmr;
  107. break;
  108. default:
  109. return -EINVAL;
  110. }
  111. switch (br & BR_PS) {
  112. case BR_PS_8:
  113. upm->width = 8;
  114. break;
  115. case BR_PS_16:
  116. upm->width = 16;
  117. break;
  118. case BR_PS_32:
  119. upm->width = 32;
  120. break;
  121. default:
  122. return -EINVAL;
  123. }
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(fsl_upm_find);
  127. /**
  128. * fsl_upm_run_pattern - actually run an UPM pattern
  129. * @upm: pointer to the fsl_upm structure obtained via fsl_upm_find
  130. * @io_base: remapped pointer to where memory access should happen
  131. * @mar: MAR register content during pattern execution
  132. *
  133. * This function triggers dummy write to the memory specified by the io_base,
  134. * thus UPM pattern actually executed. Note that mar usage depends on the
  135. * pre-programmed AMX bits in the UPM RAM.
  136. */
  137. int fsl_upm_run_pattern(struct fsl_upm *upm, void __iomem *io_base, u32 mar)
  138. {
  139. int ret = 0;
  140. unsigned long flags;
  141. if (!fsl_lbc_ctrl_dev || !fsl_lbc_ctrl_dev->regs)
  142. return -ENODEV;
  143. spin_lock_irqsave(&fsl_lbc_lock, flags);
  144. out_be32(&fsl_lbc_ctrl_dev->regs->mar, mar);
  145. switch (upm->width) {
  146. case 8:
  147. out_8(io_base, 0x0);
  148. break;
  149. case 16:
  150. out_be16(io_base, 0x0);
  151. break;
  152. case 32:
  153. out_be32(io_base, 0x0);
  154. break;
  155. default:
  156. ret = -EINVAL;
  157. break;
  158. }
  159. spin_unlock_irqrestore(&fsl_lbc_lock, flags);
  160. return ret;
  161. }
  162. EXPORT_SYMBOL(fsl_upm_run_pattern);
  163. static int __devinit fsl_lbc_ctrl_init(struct fsl_lbc_ctrl *ctrl,
  164. struct device_node *node)
  165. {
  166. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  167. /* clear event registers */
  168. setbits32(&lbc->ltesr, LTESR_CLEAR);
  169. out_be32(&lbc->lteatr, 0);
  170. out_be32(&lbc->ltear, 0);
  171. out_be32(&lbc->lteccr, LTECCR_CLEAR);
  172. out_be32(&lbc->ltedr, LTEDR_ENABLE);
  173. /* Set the monitor timeout value to the maximum for erratum A001 */
  174. if (of_device_is_compatible(node, "fsl,elbc"))
  175. clrsetbits_be32(&lbc->lbcr, LBCR_BMT, LBCR_BMTPS);
  176. return 0;
  177. }
  178. /*
  179. * NOTE: This interrupt is used to report localbus events of various kinds,
  180. * such as transaction errors on the chipselects.
  181. */
  182. static irqreturn_t fsl_lbc_ctrl_irq(int irqno, void *data)
  183. {
  184. struct fsl_lbc_ctrl *ctrl = data;
  185. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  186. u32 status;
  187. status = in_be32(&lbc->ltesr);
  188. if (!status)
  189. return IRQ_NONE;
  190. out_be32(&lbc->ltesr, LTESR_CLEAR);
  191. out_be32(&lbc->lteatr, 0);
  192. out_be32(&lbc->ltear, 0);
  193. ctrl->irq_status = status;
  194. if (status & LTESR_BM)
  195. dev_err(ctrl->dev, "Local bus monitor time-out: "
  196. "LTESR 0x%08X\n", status);
  197. if (status & LTESR_WP)
  198. dev_err(ctrl->dev, "Write protect error: "
  199. "LTESR 0x%08X\n", status);
  200. if (status & LTESR_ATMW)
  201. dev_err(ctrl->dev, "Atomic write error: "
  202. "LTESR 0x%08X\n", status);
  203. if (status & LTESR_ATMR)
  204. dev_err(ctrl->dev, "Atomic read error: "
  205. "LTESR 0x%08X\n", status);
  206. if (status & LTESR_CS)
  207. dev_err(ctrl->dev, "Chip select error: "
  208. "LTESR 0x%08X\n", status);
  209. if (status & LTESR_UPM)
  210. ;
  211. if (status & LTESR_FCT) {
  212. dev_err(ctrl->dev, "FCM command time-out: "
  213. "LTESR 0x%08X\n", status);
  214. smp_wmb();
  215. wake_up(&ctrl->irq_wait);
  216. }
  217. if (status & LTESR_PAR) {
  218. dev_err(ctrl->dev, "Parity or Uncorrectable ECC error: "
  219. "LTESR 0x%08X\n", status);
  220. smp_wmb();
  221. wake_up(&ctrl->irq_wait);
  222. }
  223. if (status & LTESR_CC) {
  224. smp_wmb();
  225. wake_up(&ctrl->irq_wait);
  226. }
  227. if (status & ~LTESR_MASK)
  228. dev_err(ctrl->dev, "Unknown error: "
  229. "LTESR 0x%08X\n", status);
  230. return IRQ_HANDLED;
  231. }
  232. /*
  233. * fsl_lbc_ctrl_probe
  234. *
  235. * called by device layer when it finds a device matching
  236. * one our driver can handled. This code allocates all of
  237. * the resources needed for the controller only. The
  238. * resources for the NAND banks themselves are allocated
  239. * in the chip probe function.
  240. */
  241. static int __devinit fsl_lbc_ctrl_probe(struct platform_device *dev)
  242. {
  243. int ret;
  244. if (!dev->dev.of_node) {
  245. dev_err(&dev->dev, "Device OF-Node is NULL");
  246. return -EFAULT;
  247. }
  248. fsl_lbc_ctrl_dev = kzalloc(sizeof(*fsl_lbc_ctrl_dev), GFP_KERNEL);
  249. if (!fsl_lbc_ctrl_dev)
  250. return -ENOMEM;
  251. dev_set_drvdata(&dev->dev, fsl_lbc_ctrl_dev);
  252. spin_lock_init(&fsl_lbc_ctrl_dev->lock);
  253. init_waitqueue_head(&fsl_lbc_ctrl_dev->irq_wait);
  254. fsl_lbc_ctrl_dev->regs = of_iomap(dev->dev.of_node, 0);
  255. if (!fsl_lbc_ctrl_dev->regs) {
  256. dev_err(&dev->dev, "failed to get memory region\n");
  257. ret = -ENODEV;
  258. goto err;
  259. }
  260. fsl_lbc_ctrl_dev->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
  261. if (fsl_lbc_ctrl_dev->irq == NO_IRQ) {
  262. dev_err(&dev->dev, "failed to get irq resource\n");
  263. ret = -ENODEV;
  264. goto err;
  265. }
  266. fsl_lbc_ctrl_dev->dev = &dev->dev;
  267. ret = fsl_lbc_ctrl_init(fsl_lbc_ctrl_dev, dev->dev.of_node);
  268. if (ret < 0)
  269. goto err;
  270. ret = request_irq(fsl_lbc_ctrl_dev->irq, fsl_lbc_ctrl_irq, 0,
  271. "fsl-lbc", fsl_lbc_ctrl_dev);
  272. if (ret != 0) {
  273. dev_err(&dev->dev, "failed to install irq (%d)\n",
  274. fsl_lbc_ctrl_dev->irq);
  275. ret = fsl_lbc_ctrl_dev->irq;
  276. goto err;
  277. }
  278. /* Enable interrupts for any detected events */
  279. out_be32(&fsl_lbc_ctrl_dev->regs->lteir, LTEIR_ENABLE);
  280. return 0;
  281. err:
  282. iounmap(fsl_lbc_ctrl_dev->regs);
  283. kfree(fsl_lbc_ctrl_dev);
  284. fsl_lbc_ctrl_dev = NULL;
  285. return ret;
  286. }
  287. #ifdef CONFIG_SUSPEND
  288. /* save lbc registers */
  289. static int fsl_lbc_suspend(struct platform_device *pdev, pm_message_t state)
  290. {
  291. struct fsl_lbc_ctrl *ctrl = dev_get_drvdata(&pdev->dev);
  292. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  293. ctrl->saved_regs = kmalloc(sizeof(struct fsl_lbc_regs), GFP_KERNEL);
  294. if (!ctrl->saved_regs)
  295. return -ENOMEM;
  296. _memcpy_fromio(ctrl->saved_regs, lbc, sizeof(struct fsl_lbc_regs));
  297. return 0;
  298. }
  299. /* restore lbc registers */
  300. static int fsl_lbc_resume(struct platform_device *pdev)
  301. {
  302. struct fsl_lbc_ctrl *ctrl = dev_get_drvdata(&pdev->dev);
  303. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  304. if (ctrl->saved_regs) {
  305. _memcpy_toio(lbc, ctrl->saved_regs,
  306. sizeof(struct fsl_lbc_regs));
  307. kfree(ctrl->saved_regs);
  308. ctrl->saved_regs = NULL;
  309. }
  310. return 0;
  311. }
  312. #endif /* CONFIG_SUSPEND */
  313. static const struct of_device_id fsl_lbc_match[] = {
  314. { .compatible = "fsl,elbc", },
  315. { .compatible = "fsl,pq3-localbus", },
  316. { .compatible = "fsl,pq2-localbus", },
  317. { .compatible = "fsl,pq2pro-localbus", },
  318. {},
  319. };
  320. static struct platform_driver fsl_lbc_ctrl_driver = {
  321. .driver = {
  322. .name = "fsl-lbc",
  323. .of_match_table = fsl_lbc_match,
  324. },
  325. .probe = fsl_lbc_ctrl_probe,
  326. #ifdef CONFIG_SUSPEND
  327. .suspend = fsl_lbc_suspend,
  328. .resume = fsl_lbc_resume,
  329. #endif
  330. };
  331. static int __init fsl_lbc_init(void)
  332. {
  333. return platform_driver_register(&fsl_lbc_ctrl_driver);
  334. }
  335. module_init(fsl_lbc_init);