fsl_lbc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. u32 br = in_be32(&lbc->bank[i].br);
  70. u32 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. u32 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 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. unsigned long flags;
  188. spin_lock_irqsave(&fsl_lbc_lock, flags);
  189. status = in_be32(&lbc->ltesr);
  190. if (!status) {
  191. spin_unlock_irqrestore(&fsl_lbc_lock, flags);
  192. return IRQ_NONE;
  193. }
  194. out_be32(&lbc->ltesr, LTESR_CLEAR);
  195. out_be32(&lbc->lteatr, 0);
  196. out_be32(&lbc->ltear, 0);
  197. ctrl->irq_status = status;
  198. if (status & LTESR_BM)
  199. dev_err(ctrl->dev, "Local bus monitor time-out: "
  200. "LTESR 0x%08X\n", status);
  201. if (status & LTESR_WP)
  202. dev_err(ctrl->dev, "Write protect error: "
  203. "LTESR 0x%08X\n", status);
  204. if (status & LTESR_ATMW)
  205. dev_err(ctrl->dev, "Atomic write error: "
  206. "LTESR 0x%08X\n", status);
  207. if (status & LTESR_ATMR)
  208. dev_err(ctrl->dev, "Atomic read error: "
  209. "LTESR 0x%08X\n", status);
  210. if (status & LTESR_CS)
  211. dev_err(ctrl->dev, "Chip select error: "
  212. "LTESR 0x%08X\n", status);
  213. if (status & LTESR_FCT) {
  214. dev_err(ctrl->dev, "FCM command time-out: "
  215. "LTESR 0x%08X\n", status);
  216. smp_wmb();
  217. wake_up(&ctrl->irq_wait);
  218. }
  219. if (status & LTESR_PAR) {
  220. dev_err(ctrl->dev, "Parity or Uncorrectable ECC error: "
  221. "LTESR 0x%08X\n", status);
  222. smp_wmb();
  223. wake_up(&ctrl->irq_wait);
  224. }
  225. if (status & LTESR_CC) {
  226. smp_wmb();
  227. wake_up(&ctrl->irq_wait);
  228. }
  229. if (status & ~LTESR_MASK)
  230. dev_err(ctrl->dev, "Unknown error: "
  231. "LTESR 0x%08X\n", status);
  232. spin_unlock_irqrestore(&fsl_lbc_lock, flags);
  233. return IRQ_HANDLED;
  234. }
  235. /*
  236. * fsl_lbc_ctrl_probe
  237. *
  238. * called by device layer when it finds a device matching
  239. * one our driver can handled. This code allocates all of
  240. * the resources needed for the controller only. The
  241. * resources for the NAND banks themselves are allocated
  242. * in the chip probe function.
  243. */
  244. static int fsl_lbc_ctrl_probe(struct platform_device *dev)
  245. {
  246. int ret;
  247. if (!dev->dev.of_node) {
  248. dev_err(&dev->dev, "Device OF-Node is NULL");
  249. return -EFAULT;
  250. }
  251. fsl_lbc_ctrl_dev = kzalloc(sizeof(*fsl_lbc_ctrl_dev), GFP_KERNEL);
  252. if (!fsl_lbc_ctrl_dev)
  253. return -ENOMEM;
  254. dev_set_drvdata(&dev->dev, fsl_lbc_ctrl_dev);
  255. spin_lock_init(&fsl_lbc_ctrl_dev->lock);
  256. init_waitqueue_head(&fsl_lbc_ctrl_dev->irq_wait);
  257. fsl_lbc_ctrl_dev->regs = of_iomap(dev->dev.of_node, 0);
  258. if (!fsl_lbc_ctrl_dev->regs) {
  259. dev_err(&dev->dev, "failed to get memory region\n");
  260. ret = -ENODEV;
  261. goto err;
  262. }
  263. fsl_lbc_ctrl_dev->irq[0] = irq_of_parse_and_map(dev->dev.of_node, 0);
  264. if (!fsl_lbc_ctrl_dev->irq[0]) {
  265. dev_err(&dev->dev, "failed to get irq resource\n");
  266. ret = -ENODEV;
  267. goto err;
  268. }
  269. fsl_lbc_ctrl_dev->dev = &dev->dev;
  270. ret = fsl_lbc_ctrl_init(fsl_lbc_ctrl_dev, dev->dev.of_node);
  271. if (ret < 0)
  272. goto err;
  273. ret = request_irq(fsl_lbc_ctrl_dev->irq[0], fsl_lbc_ctrl_irq, 0,
  274. "fsl-lbc", fsl_lbc_ctrl_dev);
  275. if (ret != 0) {
  276. dev_err(&dev->dev, "failed to install irq (%d)\n",
  277. fsl_lbc_ctrl_dev->irq[0]);
  278. ret = fsl_lbc_ctrl_dev->irq[0];
  279. goto err;
  280. }
  281. fsl_lbc_ctrl_dev->irq[1] = irq_of_parse_and_map(dev->dev.of_node, 1);
  282. if (fsl_lbc_ctrl_dev->irq[1]) {
  283. ret = request_irq(fsl_lbc_ctrl_dev->irq[1], fsl_lbc_ctrl_irq,
  284. IRQF_SHARED, "fsl-lbc-err", fsl_lbc_ctrl_dev);
  285. if (ret) {
  286. dev_err(&dev->dev, "failed to install irq (%d)\n",
  287. fsl_lbc_ctrl_dev->irq[1]);
  288. ret = fsl_lbc_ctrl_dev->irq[1];
  289. goto err1;
  290. }
  291. }
  292. /* Enable interrupts for any detected events */
  293. out_be32(&fsl_lbc_ctrl_dev->regs->lteir, LTEIR_ENABLE);
  294. return 0;
  295. err1:
  296. free_irq(fsl_lbc_ctrl_dev->irq[0], fsl_lbc_ctrl_dev);
  297. err:
  298. iounmap(fsl_lbc_ctrl_dev->regs);
  299. kfree(fsl_lbc_ctrl_dev);
  300. fsl_lbc_ctrl_dev = NULL;
  301. return ret;
  302. }
  303. #ifdef CONFIG_SUSPEND
  304. /* save lbc registers */
  305. static int fsl_lbc_suspend(struct platform_device *pdev, pm_message_t state)
  306. {
  307. struct fsl_lbc_ctrl *ctrl = dev_get_drvdata(&pdev->dev);
  308. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  309. ctrl->saved_regs = kmalloc(sizeof(struct fsl_lbc_regs), GFP_KERNEL);
  310. if (!ctrl->saved_regs)
  311. return -ENOMEM;
  312. _memcpy_fromio(ctrl->saved_regs, lbc, sizeof(struct fsl_lbc_regs));
  313. return 0;
  314. }
  315. /* restore lbc registers */
  316. static int fsl_lbc_resume(struct platform_device *pdev)
  317. {
  318. struct fsl_lbc_ctrl *ctrl = dev_get_drvdata(&pdev->dev);
  319. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  320. if (ctrl->saved_regs) {
  321. _memcpy_toio(lbc, ctrl->saved_regs,
  322. sizeof(struct fsl_lbc_regs));
  323. kfree(ctrl->saved_regs);
  324. ctrl->saved_regs = NULL;
  325. }
  326. return 0;
  327. }
  328. #endif /* CONFIG_SUSPEND */
  329. static const struct of_device_id fsl_lbc_match[] = {
  330. { .compatible = "fsl,elbc", },
  331. { .compatible = "fsl,pq3-localbus", },
  332. { .compatible = "fsl,pq2-localbus", },
  333. { .compatible = "fsl,pq2pro-localbus", },
  334. {},
  335. };
  336. static struct platform_driver fsl_lbc_ctrl_driver = {
  337. .driver = {
  338. .name = "fsl-lbc",
  339. .of_match_table = fsl_lbc_match,
  340. },
  341. .probe = fsl_lbc_ctrl_probe,
  342. #ifdef CONFIG_SUSPEND
  343. .suspend = fsl_lbc_suspend,
  344. .resume = fsl_lbc_resume,
  345. #endif
  346. };
  347. static int __init fsl_lbc_init(void)
  348. {
  349. return platform_driver_register(&fsl_lbc_ctrl_driver);
  350. }
  351. subsys_initcall(fsl_lbc_init);