fsl_lbc.c 10 KB

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