fsl_lbc.c 8.7 KB

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