fsl_lbc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. {
  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. /* Enable interrupts for any detected events */
  172. out_be32(&lbc->lteir, LTEIR_ENABLE);
  173. return 0;
  174. }
  175. /*
  176. * NOTE: This interrupt is used to report localbus events of various kinds,
  177. * such as transaction errors on the chipselects.
  178. */
  179. static irqreturn_t fsl_lbc_ctrl_irq(int irqno, void *data)
  180. {
  181. struct fsl_lbc_ctrl *ctrl = data;
  182. struct fsl_lbc_regs __iomem *lbc = ctrl->regs;
  183. u32 status;
  184. status = in_be32(&lbc->ltesr);
  185. if (!status)
  186. return IRQ_NONE;
  187. out_be32(&lbc->ltesr, LTESR_CLEAR);
  188. out_be32(&lbc->lteatr, 0);
  189. out_be32(&lbc->ltear, 0);
  190. ctrl->irq_status = status;
  191. if (status & LTESR_BM)
  192. dev_err(ctrl->dev, "Local bus monitor time-out: "
  193. "LTESR 0x%08X\n", status);
  194. if (status & LTESR_WP)
  195. dev_err(ctrl->dev, "Write protect error: "
  196. "LTESR 0x%08X\n", status);
  197. if (status & LTESR_ATMW)
  198. dev_err(ctrl->dev, "Atomic write error: "
  199. "LTESR 0x%08X\n", status);
  200. if (status & LTESR_ATMR)
  201. dev_err(ctrl->dev, "Atomic read error: "
  202. "LTESR 0x%08X\n", status);
  203. if (status & LTESR_CS)
  204. dev_err(ctrl->dev, "Chip select error: "
  205. "LTESR 0x%08X\n", status);
  206. if (status & LTESR_UPM)
  207. ;
  208. if (status & LTESR_FCT) {
  209. dev_err(ctrl->dev, "FCM command time-out: "
  210. "LTESR 0x%08X\n", status);
  211. smp_wmb();
  212. wake_up(&ctrl->irq_wait);
  213. }
  214. if (status & LTESR_PAR) {
  215. dev_err(ctrl->dev, "Parity or Uncorrectable ECC error: "
  216. "LTESR 0x%08X\n", status);
  217. smp_wmb();
  218. wake_up(&ctrl->irq_wait);
  219. }
  220. if (status & LTESR_CC) {
  221. smp_wmb();
  222. wake_up(&ctrl->irq_wait);
  223. }
  224. if (status & ~LTESR_MASK)
  225. dev_err(ctrl->dev, "Unknown error: "
  226. "LTESR 0x%08X\n", status);
  227. return IRQ_HANDLED;
  228. }
  229. /*
  230. * fsl_lbc_ctrl_probe
  231. *
  232. * called by device layer when it finds a device matching
  233. * one our driver can handled. This code allocates all of
  234. * the resources needed for the controller only. The
  235. * resources for the NAND banks themselves are allocated
  236. * in the chip probe function.
  237. */
  238. static int __devinit fsl_lbc_ctrl_probe(struct platform_device *dev)
  239. {
  240. int ret;
  241. if (!dev->dev.of_node) {
  242. dev_err(&dev->dev, "Device OF-Node is NULL");
  243. return -EFAULT;
  244. }
  245. fsl_lbc_ctrl_dev = kzalloc(sizeof(*fsl_lbc_ctrl_dev), GFP_KERNEL);
  246. if (!fsl_lbc_ctrl_dev)
  247. return -ENOMEM;
  248. dev_set_drvdata(&dev->dev, fsl_lbc_ctrl_dev);
  249. spin_lock_init(&fsl_lbc_ctrl_dev->lock);
  250. init_waitqueue_head(&fsl_lbc_ctrl_dev->irq_wait);
  251. fsl_lbc_ctrl_dev->regs = of_iomap(dev->dev.of_node, 0);
  252. if (!fsl_lbc_ctrl_dev->regs) {
  253. dev_err(&dev->dev, "failed to get memory region\n");
  254. ret = -ENODEV;
  255. goto err;
  256. }
  257. fsl_lbc_ctrl_dev->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
  258. if (fsl_lbc_ctrl_dev->irq == NO_IRQ) {
  259. dev_err(&dev->dev, "failed to get irq resource\n");
  260. ret = -ENODEV;
  261. goto err;
  262. }
  263. fsl_lbc_ctrl_dev->dev = &dev->dev;
  264. ret = fsl_lbc_ctrl_init(fsl_lbc_ctrl_dev);
  265. if (ret < 0)
  266. goto err;
  267. ret = request_irq(fsl_lbc_ctrl_dev->irq, fsl_lbc_ctrl_irq, 0,
  268. "fsl-lbc", fsl_lbc_ctrl_dev);
  269. if (ret != 0) {
  270. dev_err(&dev->dev, "failed to install irq (%d)\n",
  271. fsl_lbc_ctrl_dev->irq);
  272. ret = fsl_lbc_ctrl_dev->irq;
  273. goto err;
  274. }
  275. return 0;
  276. err:
  277. iounmap(fsl_lbc_ctrl_dev->regs);
  278. kfree(fsl_lbc_ctrl_dev);
  279. return ret;
  280. }
  281. static const struct of_device_id fsl_lbc_match[] = {
  282. { .compatible = "fsl,elbc", },
  283. { .compatible = "fsl,pq3-localbus", },
  284. { .compatible = "fsl,pq2-localbus", },
  285. { .compatible = "fsl,pq2pro-localbus", },
  286. {},
  287. };
  288. static struct platform_driver fsl_lbc_ctrl_driver = {
  289. .driver = {
  290. .name = "fsl-lbc",
  291. .of_match_table = fsl_lbc_match,
  292. },
  293. .probe = fsl_lbc_ctrl_probe,
  294. };
  295. static int __init fsl_lbc_init(void)
  296. {
  297. return platform_driver_register(&fsl_lbc_ctrl_driver);
  298. }
  299. module_init(fsl_lbc_init);