dcr-native.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include <asm/cpu_has_feature.h>
  26. #include <linux/stringify.h>
  27. typedef struct {
  28. unsigned int base;
  29. } dcr_host_native_t;
  30. static inline bool dcr_map_ok_native(dcr_host_native_t host)
  31. {
  32. return true;
  33. }
  34. #define dcr_map_native(dev, dcr_n, dcr_c) \
  35. ((dcr_host_native_t){ .base = (dcr_n) })
  36. #define dcr_unmap_native(host, dcr_c) do {} while (0)
  37. #define dcr_read_native(host, dcr_n) mfdcr(dcr_n + host.base)
  38. #define dcr_write_native(host, dcr_n, value) mtdcr(dcr_n + host.base, value)
  39. /* Table based DCR accessors */
  40. extern void __mtdcr(unsigned int reg, unsigned int val);
  41. extern unsigned int __mfdcr(unsigned int reg);
  42. /* mfdcrx/mtdcrx instruction based accessors. We hand code
  43. * the opcodes in order not to depend on newer binutils
  44. */
  45. static inline unsigned int mfdcrx(unsigned int reg)
  46. {
  47. unsigned int ret;
  48. asm volatile(".long 0x7c000206 | (%0 << 21) | (%1 << 16)"
  49. : "=r" (ret) : "r" (reg));
  50. return ret;
  51. }
  52. static inline void mtdcrx(unsigned int reg, unsigned int val)
  53. {
  54. asm volatile(".long 0x7c000306 | (%0 << 21) | (%1 << 16)"
  55. : : "r" (val), "r" (reg));
  56. }
  57. #define mfdcr(rn) \
  58. ({unsigned int rval; \
  59. if (__builtin_constant_p(rn) && rn < 1024) \
  60. asm volatile("mfdcr %0," __stringify(rn) \
  61. : "=r" (rval)); \
  62. else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR))) \
  63. rval = mfdcrx(rn); \
  64. else \
  65. rval = __mfdcr(rn); \
  66. rval;})
  67. #define mtdcr(rn, v) \
  68. do { \
  69. if (__builtin_constant_p(rn) && rn < 1024) \
  70. asm volatile("mtdcr " __stringify(rn) ",%0" \
  71. : : "r" (v)); \
  72. else if (likely(cpu_has_feature(CPU_FTR_INDEXED_DCR))) \
  73. mtdcrx(rn, v); \
  74. else \
  75. __mtdcr(rn, v); \
  76. } while (0)
  77. /* R/W of indirect DCRs make use of standard naming conventions for DCRs */
  78. extern spinlock_t dcr_ind_lock;
  79. static inline unsigned __mfdcri(int base_addr, int base_data, int reg)
  80. {
  81. unsigned long flags;
  82. unsigned int val;
  83. spin_lock_irqsave(&dcr_ind_lock, flags);
  84. if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
  85. mtdcrx(base_addr, reg);
  86. val = mfdcrx(base_data);
  87. } else {
  88. __mtdcr(base_addr, reg);
  89. val = __mfdcr(base_data);
  90. }
  91. spin_unlock_irqrestore(&dcr_ind_lock, flags);
  92. return val;
  93. }
  94. static inline void __mtdcri(int base_addr, int base_data, int reg,
  95. unsigned val)
  96. {
  97. unsigned long flags;
  98. spin_lock_irqsave(&dcr_ind_lock, flags);
  99. if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
  100. mtdcrx(base_addr, reg);
  101. mtdcrx(base_data, val);
  102. } else {
  103. __mtdcr(base_addr, reg);
  104. __mtdcr(base_data, val);
  105. }
  106. spin_unlock_irqrestore(&dcr_ind_lock, flags);
  107. }
  108. static inline void __dcri_clrset(int base_addr, int base_data, int reg,
  109. unsigned clr, unsigned set)
  110. {
  111. unsigned long flags;
  112. unsigned int val;
  113. spin_lock_irqsave(&dcr_ind_lock, flags);
  114. if (cpu_has_feature(CPU_FTR_INDEXED_DCR)) {
  115. mtdcrx(base_addr, reg);
  116. val = (mfdcrx(base_data) & ~clr) | set;
  117. mtdcrx(base_data, val);
  118. } else {
  119. __mtdcr(base_addr, reg);
  120. val = (__mfdcr(base_data) & ~clr) | set;
  121. __mtdcr(base_data, val);
  122. }
  123. spin_unlock_irqrestore(&dcr_ind_lock, flags);
  124. }
  125. #define mfdcri(base, reg) __mfdcri(DCRN_ ## base ## _CONFIG_ADDR, \
  126. DCRN_ ## base ## _CONFIG_DATA, \
  127. reg)
  128. #define mtdcri(base, reg, data) __mtdcri(DCRN_ ## base ## _CONFIG_ADDR, \
  129. DCRN_ ## base ## _CONFIG_DATA, \
  130. reg, data)
  131. #define dcri_clrset(base, reg, clr, set) __dcri_clrset(DCRN_ ## base ## _CONFIG_ADDR, \
  132. DCRN_ ## base ## _CONFIG_DATA, \
  133. reg, clr, set)
  134. #endif /* __ASSEMBLY__ */
  135. #endif /* __KERNEL__ */
  136. #endif /* _ASM_POWERPC_DCR_NATIVE_H */