fsl_lbc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Freescale LBC and UPM routines.
  3. *
  4. * Copyright (c) 2007-2008 MontaVista Software, Inc.
  5. *
  6. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/compiler.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/types.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <asm/prom.h>
  22. #include <asm/fsl_lbc.h>
  23. static spinlock_t fsl_lbc_lock = __SPIN_LOCK_UNLOCKED(fsl_lbc_lock);
  24. static struct fsl_lbc_regs __iomem *fsl_lbc_regs;
  25. static char __initdata *compat_lbc[] = {
  26. "fsl,pq2-localbus",
  27. "fsl,pq2pro-localbus",
  28. "fsl,pq3-localbus",
  29. "fsl,elbc",
  30. };
  31. static int __init fsl_lbc_init(void)
  32. {
  33. struct device_node *lbus;
  34. int i;
  35. for (i = 0; i < ARRAY_SIZE(compat_lbc); i++) {
  36. lbus = of_find_compatible_node(NULL, NULL, compat_lbc[i]);
  37. if (lbus)
  38. goto found;
  39. }
  40. return -ENODEV;
  41. found:
  42. fsl_lbc_regs = of_iomap(lbus, 0);
  43. of_node_put(lbus);
  44. if (!fsl_lbc_regs)
  45. return -ENOMEM;
  46. return 0;
  47. }
  48. arch_initcall(fsl_lbc_init);
  49. /**
  50. * fsl_lbc_find - find Localbus bank
  51. * @addr_base: base address of the memory bank
  52. *
  53. * This function walks LBC banks comparing "Base address" field of the BR
  54. * registers with the supplied addr_base argument. When bases match this
  55. * function returns bank number (starting with 0), otherwise it returns
  56. * appropriate errno value.
  57. */
  58. int fsl_lbc_find(phys_addr_t addr_base)
  59. {
  60. int i;
  61. if (!fsl_lbc_regs)
  62. return -ENODEV;
  63. for (i = 0; i < ARRAY_SIZE(fsl_lbc_regs->bank); i++) {
  64. __be32 br = in_be32(&fsl_lbc_regs->bank[i].br);
  65. __be32 or = in_be32(&fsl_lbc_regs->bank[i].or);
  66. if (br & BR_V && (br & or & BR_BA) == addr_base)
  67. return i;
  68. }
  69. return -ENOENT;
  70. }
  71. EXPORT_SYMBOL(fsl_lbc_find);
  72. /**
  73. * fsl_upm_find - find pre-programmed UPM via base address
  74. * @addr_base: base address of the memory bank controlled by the UPM
  75. * @upm: pointer to the allocated fsl_upm structure
  76. *
  77. * This function fills fsl_upm structure so you can use it with the rest of
  78. * UPM API. On success this function returns 0, otherwise it returns
  79. * appropriate errno value.
  80. */
  81. int fsl_upm_find(phys_addr_t addr_base, struct fsl_upm *upm)
  82. {
  83. int bank;
  84. __be32 br;
  85. bank = fsl_lbc_find(addr_base);
  86. if (bank < 0)
  87. return bank;
  88. br = in_be32(&fsl_lbc_regs->bank[bank].br);
  89. switch (br & BR_MSEL) {
  90. case BR_MS_UPMA:
  91. upm->mxmr = &fsl_lbc_regs->mamr;
  92. break;
  93. case BR_MS_UPMB:
  94. upm->mxmr = &fsl_lbc_regs->mbmr;
  95. break;
  96. case BR_MS_UPMC:
  97. upm->mxmr = &fsl_lbc_regs->mcmr;
  98. break;
  99. default:
  100. return -EINVAL;
  101. }
  102. switch (br & BR_PS) {
  103. case BR_PS_8:
  104. upm->width = 8;
  105. break;
  106. case BR_PS_16:
  107. upm->width = 16;
  108. break;
  109. case BR_PS_32:
  110. upm->width = 32;
  111. break;
  112. default:
  113. return -EINVAL;
  114. }
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(fsl_upm_find);
  118. /**
  119. * fsl_upm_run_pattern - actually run an UPM pattern
  120. * @upm: pointer to the fsl_upm structure obtained via fsl_upm_find
  121. * @io_base: remapped pointer to where memory access should happen
  122. * @mar: MAR register content during pattern execution
  123. *
  124. * This function triggers dummy write to the memory specified by the io_base,
  125. * thus UPM pattern actually executed. Note that mar usage depends on the
  126. * pre-programmed AMX bits in the UPM RAM.
  127. */
  128. int fsl_upm_run_pattern(struct fsl_upm *upm, void __iomem *io_base, u32 mar)
  129. {
  130. int ret = 0;
  131. unsigned long flags;
  132. spin_lock_irqsave(&fsl_lbc_lock, flags);
  133. out_be32(&fsl_lbc_regs->mar, mar);
  134. switch (upm->width) {
  135. case 8:
  136. out_8(io_base, 0x0);
  137. break;
  138. case 16:
  139. out_be16(io_base, 0x0);
  140. break;
  141. case 32:
  142. out_be32(io_base, 0x0);
  143. break;
  144. default:
  145. ret = -EINVAL;
  146. break;
  147. }
  148. spin_unlock_irqrestore(&fsl_lbc_lock, flags);
  149. return ret;
  150. }
  151. EXPORT_SYMBOL(fsl_upm_run_pattern);