fsl_lbc.c 10 KB

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