dcr-native.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
  3. * <benh@kernel.crashing.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef _ASM_POWERPC_DCR_NATIVE_H
  20. #define _ASM_POWERPC_DCR_NATIVE_H
  21. #ifdef __KERNEL__
  22. #ifndef __ASSEMBLY__
  23. #include <linux/spinlock.h>
  24. #include <asm/cputable.h>
  25. typedef struct {
  26. unsigned int base;
  27. } dcr_host_native_t;
  28. static inline bool dcr_map_ok_native(dcr_host_native_t host)
  29. {
  30. return true;
  31. }
  32. #define dcr_map_native(dev, dcr_n, dcr_c) \
  33. ((dcr_host_native_t){ .base = (dcr_n) })
  34. #define dcr_unmap_native(host, dcr_c) do {} while (0)
  35. #define dcr_read_native(host, dcr_n) mfdcr(dcr_n + host.base)
  36. #define dcr_write_native(host, dcr_n, value) mtdcr(dcr_n + host.base, value)
  37. /* Table based DCR accessors */
  38. extern void __mtdcr(unsigned int reg, unsigned int val);
  39. extern unsigned int __mfdcr(unsigned int reg);
  40. /* mfdcrx/mtdcrx instruction based accessors. We hand code
  41. * the opcodes in order not to depend on newer binutils
  42. */
  43. static inline unsigned int mfdcrx(unsigned int reg)
  44. {
  45. unsigned int ret;
  46. asm volatile(".long 0x7c000206 | (%0 << 21) | (%1 << 16)"
  47. : "=r" (ret) : "r" (reg));
  48. return ret;
  49. }
  50. static inline void mtdcrx(unsigned int reg, unsigned int val)
  51. {
  52. asm volatile(".long 0x7c000306 | (%0 << 21) | (%1 << 16)"
  53. : : "r" (val), "r" (reg));
  54. }
  55. #define mfdcr(rn) \
  56. ({unsigned int rval; \
  57. if (__builtin_constant_p(rn) && rn < 1024) \
  58. asm volatile("mfdcr %0," __stringify(rn) \
  59. : "=r" (rval)); \
  60. else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR))) \
  61. rval = mfdcrx(rn); \
  62. else \
  63. rval = __mfdcr(rn); \
  64. rval;})
  65. #define mtdcr(rn, v) \
  66. do { \
  67. if (__builtin_constant_p(rn) && rn < 1024) \
  68. asm volatile("mtdcr " __stringify(rn) ",%0" \
  69. : : "r" (v)); \
  70. else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR))) \
  71. mtdcrx(rn, v); \
  72. else \
  73. __mtdcr(rn, v); \
  74. } while (0)
  75. /* R/W of indirect DCRs make use of standard naming conventions for DCRs */
  76. extern spinlock_t dcr_ind_lock;
  77. static inline unsigned __mfdcri(int base_addr, int base_data, int reg)
  78. {
  79. unsigned long flags;
  80. unsigned int val;
  81. spin_lock_irqsave(&dcr_ind_lock, flags);
  82. if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
  83. mtdcrx(base_addr, reg);
  84. val = mfdcrx(base_data);
  85. } else {
  86. __mtdcr(base_addr, reg);
  87. val = __mfdcr(base_data);
  88. }
  89. spin_unlock_irqrestore(&dcr_ind_lock, flags);
  90. return val;
  91. }
  92. static inline void __mtdcri(int base_addr, int base_data, int reg,
  93. unsigned val)
  94. {
  95. unsigned long flags;
  96. spin_lock_irqsave(&dcr_ind_lock, flags);
  97. if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
  98. mtdcrx(base_addr, reg);
  99. mtdcrx(base_data, val);
  100. } else {
  101. __mtdcr(base_addr, reg);
  102. __mtdcr(base_data, val);
  103. }
  104. spin_unlock_irqrestore(&dcr_ind_lock, flags);
  105. }
  106. static inline void __dcri_clrset(int base_addr, int base_data, int reg,
  107. unsigned clr, unsigned set)
  108. {
  109. unsigned long flags;
  110. unsigned int val;
  111. spin_lock_irqsave(&dcr_ind_lock, flags);
  112. if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
  113. mtdcrx(base_addr, reg);
  114. val = (mfdcrx(base_data) & ~clr) | set;
  115. mtdcrx(base_data, val);
  116. } else {
  117. __mtdcr(base_addr, reg);
  118. val = (__mfdcr(base_data) & ~clr) | set;
  119. __mtdcr(base_data, val);
  120. }
  121. spin_unlock_irqrestore(&dcr_ind_lock, flags);
  122. }
  123. #define mfdcri(base, reg) __mfdcri(DCRN_ ## base ## _CONFIG_ADDR, \
  124. DCRN_ ## base ## _CONFIG_DATA, \
  125. reg)
  126. #define mtdcri(base, reg, data) __mtdcri(DCRN_ ## base ## _CONFIG_ADDR, \
  127. DCRN_ ## base ## _CONFIG_DATA, \
  128. reg, data)
  129. #define dcri_clrset(base, reg, clr, set) __dcri_clrset(DCRN_ ## base ## _CONFIG_ADDR, \
  130. DCRN_ ## base ## _CONFIG_DATA, \
  131. reg, clr, set)
  132. #endif /* __ASSEMBLY__ */
  133. #endif /* __KERNEL__ */
  134. #endif /* _ASM_POWERPC_DCR_NATIVE_H */