haldefs.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 2003-2011 NetLogic Microsystems, Inc. (NetLogic). All rights
  3. * reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the NetLogic
  9. * license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY NETLOGIC ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  29. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  31. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  32. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef __NLM_HAL_HALDEFS_H__
  35. #define __NLM_HAL_HALDEFS_H__
  36. #include <linux/irqflags.h> /* for local_irq_disable */
  37. /*
  38. * This file contains platform specific memory mapped IO implementation
  39. * and will provide a way to read 32/64 bit memory mapped registers in
  40. * all ABIs
  41. */
  42. static inline uint32_t
  43. nlm_read_reg(uint64_t base, uint32_t reg)
  44. {
  45. volatile uint32_t *addr = (volatile uint32_t *)(long)base + reg;
  46. return *addr;
  47. }
  48. static inline void
  49. nlm_write_reg(uint64_t base, uint32_t reg, uint32_t val)
  50. {
  51. volatile uint32_t *addr = (volatile uint32_t *)(long)base + reg;
  52. *addr = val;
  53. }
  54. /*
  55. * For o32 compilation, we have to disable interrupts to access 64 bit
  56. * registers
  57. *
  58. * We need to disable interrupts because we save just the lower 32 bits of
  59. * registers in interrupt handling. So if we get hit by an interrupt while
  60. * using the upper 32 bits of a register, we lose.
  61. */
  62. static inline uint64_t
  63. nlm_read_reg64(uint64_t base, uint32_t reg)
  64. {
  65. uint64_t addr = base + (reg >> 1) * sizeof(uint64_t);
  66. volatile uint64_t *ptr = (volatile uint64_t *)(long)addr;
  67. uint64_t val;
  68. if (sizeof(unsigned long) == 4) {
  69. unsigned long flags;
  70. local_irq_save(flags);
  71. __asm__ __volatile__(
  72. ".set push" "\n\t"
  73. ".set mips64" "\n\t"
  74. "ld %L0, %1" "\n\t"
  75. "dsra32 %M0, %L0, 0" "\n\t"
  76. "sll %L0, %L0, 0" "\n\t"
  77. ".set pop" "\n"
  78. : "=r" (val)
  79. : "m" (*ptr));
  80. local_irq_restore(flags);
  81. } else
  82. val = *ptr;
  83. return val;
  84. }
  85. static inline void
  86. nlm_write_reg64(uint64_t base, uint32_t reg, uint64_t val)
  87. {
  88. uint64_t addr = base + (reg >> 1) * sizeof(uint64_t);
  89. volatile uint64_t *ptr = (volatile uint64_t *)(long)addr;
  90. if (sizeof(unsigned long) == 4) {
  91. unsigned long flags;
  92. uint64_t tmp;
  93. local_irq_save(flags);
  94. __asm__ __volatile__(
  95. ".set push" "\n\t"
  96. ".set mips64" "\n\t"
  97. "dsll32 %L0, %L0, 0" "\n\t"
  98. "dsrl32 %L0, %L0, 0" "\n\t"
  99. "dsll32 %M0, %M0, 0" "\n\t"
  100. "or %L0, %L0, %M0" "\n\t"
  101. "sd %L0, %2" "\n\t"
  102. ".set pop" "\n"
  103. : "=r" (tmp)
  104. : "0" (val), "m" (*ptr));
  105. local_irq_restore(flags);
  106. } else
  107. *ptr = val;
  108. }
  109. /*
  110. * Routines to store 32/64 bit values to 64 bit addresses,
  111. * used when going thru XKPHYS to access registers
  112. */
  113. static inline uint32_t
  114. nlm_read_reg_xkphys(uint64_t base, uint32_t reg)
  115. {
  116. return nlm_read_reg(base, reg);
  117. }
  118. static inline void
  119. nlm_write_reg_xkphys(uint64_t base, uint32_t reg, uint32_t val)
  120. {
  121. nlm_write_reg(base, reg, val);
  122. }
  123. static inline uint64_t
  124. nlm_read_reg64_xkphys(uint64_t base, uint32_t reg)
  125. {
  126. return nlm_read_reg64(base, reg);
  127. }
  128. static inline void
  129. nlm_write_reg64_xkphys(uint64_t base, uint32_t reg, uint64_t val)
  130. {
  131. nlm_write_reg64(base, reg, val);
  132. }
  133. /* Location where IO base is mapped */
  134. extern uint64_t nlm_io_base;
  135. #if defined(CONFIG_CPU_XLP)
  136. static inline uint64_t
  137. nlm_pcicfg_base(uint32_t devoffset)
  138. {
  139. return nlm_io_base + devoffset;
  140. }
  141. #elif defined(CONFIG_CPU_XLR)
  142. static inline uint64_t
  143. nlm_mmio_base(uint32_t devoffset)
  144. {
  145. return nlm_io_base + devoffset;
  146. }
  147. #endif
  148. #endif