cx18-io.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * cx18 driver PCI memory mapped IO access routines
  3. *
  4. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  5. * Copyright (C) 2008 Andy Walls <awalls@radix.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  20. * 02111-1307 USA
  21. */
  22. #include "cx18-driver.h"
  23. #include "cx18-io.h"
  24. #include "cx18-irq.h"
  25. void cx18_log_statistics(struct cx18 *cx)
  26. {
  27. int i;
  28. if (!(cx18_debug & CX18_DBGFLG_INFO))
  29. return;
  30. for (i = 0; i <= CX18_MAX_MMIO_WR_RETRIES; i++)
  31. CX18_DEBUG_INFO("retried_write[%d] = %d\n", i,
  32. atomic_read(&cx->mmio_stats.retried_write[i]));
  33. for (i = 0; i <= CX18_MAX_MMIO_RD_RETRIES; i++)
  34. CX18_DEBUG_INFO("retried_read[%d] = %d\n", i,
  35. atomic_read(&cx->mmio_stats.retried_read[i]));
  36. for (i = 0; i <= CX18_MAX_MB_ACK_DELAY; i++)
  37. if (atomic_read(&cx->mbox_stats.mb_ack_delay[i]))
  38. CX18_DEBUG_INFO("mb_ack_delay[%d] = %d\n", i,
  39. atomic_read(&cx->mbox_stats.mb_ack_delay[i]));
  40. return;
  41. }
  42. void cx18_raw_writel_retry(struct cx18 *cx, u32 val, void __iomem *addr)
  43. {
  44. int i;
  45. for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
  46. cx18_raw_writel_noretry(cx, val, addr);
  47. if (val == cx18_raw_readl_noretry(cx, addr))
  48. break;
  49. }
  50. cx18_log_write_retries(cx, i, addr);
  51. }
  52. u32 cx18_raw_readl_retry(struct cx18 *cx, const void __iomem *addr)
  53. {
  54. int i;
  55. u32 val;
  56. for (i = 0; i < CX18_MAX_MMIO_RD_RETRIES; i++) {
  57. val = cx18_raw_readl_noretry(cx, addr);
  58. if (val != 0xffffffff) /* PCI bus read error */
  59. break;
  60. }
  61. cx18_log_read_retries(cx, i, addr);
  62. return val;
  63. }
  64. u16 cx18_raw_readw_retry(struct cx18 *cx, const void __iomem *addr)
  65. {
  66. int i;
  67. u16 val;
  68. for (i = 0; i < CX18_MAX_MMIO_RD_RETRIES; i++) {
  69. val = cx18_raw_readw_noretry(cx, addr);
  70. if (val != 0xffff) /* PCI bus read error */
  71. break;
  72. }
  73. cx18_log_read_retries(cx, i, addr);
  74. return val;
  75. }
  76. void cx18_writel_retry(struct cx18 *cx, u32 val, void __iomem *addr)
  77. {
  78. int i;
  79. for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
  80. cx18_writel_noretry(cx, val, addr);
  81. if (val == cx18_readl_noretry(cx, addr))
  82. break;
  83. }
  84. cx18_log_write_retries(cx, i, addr);
  85. }
  86. void _cx18_writel_expect(struct cx18 *cx, u32 val, void __iomem *addr,
  87. u32 eval, u32 mask)
  88. {
  89. int i;
  90. eval &= mask;
  91. for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
  92. cx18_writel_noretry(cx, val, addr);
  93. if (eval == (cx18_readl_noretry(cx, addr) & mask))
  94. break;
  95. }
  96. cx18_log_write_retries(cx, i, addr);
  97. }
  98. void cx18_writew_retry(struct cx18 *cx, u16 val, void __iomem *addr)
  99. {
  100. int i;
  101. for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
  102. cx18_writew_noretry(cx, val, addr);
  103. if (val == cx18_readw_noretry(cx, addr))
  104. break;
  105. }
  106. cx18_log_write_retries(cx, i, addr);
  107. }
  108. void cx18_writeb_retry(struct cx18 *cx, u8 val, void __iomem *addr)
  109. {
  110. int i;
  111. for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
  112. cx18_writeb_noretry(cx, val, addr);
  113. if (val == cx18_readb_noretry(cx, addr))
  114. break;
  115. }
  116. cx18_log_write_retries(cx, i, addr);
  117. }
  118. u32 cx18_readl_retry(struct cx18 *cx, const void __iomem *addr)
  119. {
  120. int i;
  121. u32 val;
  122. for (i = 0; i < CX18_MAX_MMIO_RD_RETRIES; i++) {
  123. val = cx18_readl_noretry(cx, addr);
  124. if (val != 0xffffffff) /* PCI bus read error */
  125. break;
  126. }
  127. cx18_log_read_retries(cx, i, addr);
  128. return val;
  129. }
  130. u16 cx18_readw_retry(struct cx18 *cx, const void __iomem *addr)
  131. {
  132. int i;
  133. u16 val;
  134. for (i = 0; i < CX18_MAX_MMIO_RD_RETRIES; i++) {
  135. val = cx18_readw_noretry(cx, addr);
  136. if (val != 0xffff) /* PCI bus read error */
  137. break;
  138. }
  139. cx18_log_read_retries(cx, i, addr);
  140. return val;
  141. }
  142. u8 cx18_readb_retry(struct cx18 *cx, const void __iomem *addr)
  143. {
  144. int i;
  145. u8 val;
  146. for (i = 0; i < CX18_MAX_MMIO_RD_RETRIES; i++) {
  147. val = cx18_readb_noretry(cx, addr);
  148. if (val != 0xff) /* PCI bus read error */
  149. break;
  150. }
  151. cx18_log_read_retries(cx, i, addr);
  152. return val;
  153. }
  154. void cx18_memset_io(struct cx18 *cx, void __iomem *addr, int val, size_t count)
  155. {
  156. u8 __iomem *dst = addr;
  157. u16 val2 = val | (val << 8);
  158. u32 val4 = val2 | (val2 << 16);
  159. /* Align writes on the CX23418's addresses */
  160. if ((count > 0) && ((unsigned long)dst & 1)) {
  161. cx18_writeb(cx, (u8) val, dst);
  162. count--;
  163. dst++;
  164. }
  165. if ((count > 1) && ((unsigned long)dst & 2)) {
  166. cx18_writew(cx, val2, dst);
  167. count -= 2;
  168. dst += 2;
  169. }
  170. while (count > 3) {
  171. cx18_writel(cx, val4, dst);
  172. count -= 4;
  173. dst += 4;
  174. }
  175. if (count > 1) {
  176. cx18_writew(cx, val2, dst);
  177. count -= 2;
  178. dst += 2;
  179. }
  180. if (count > 0)
  181. cx18_writeb(cx, (u8) val, dst);
  182. }
  183. void cx18_sw1_irq_enable(struct cx18 *cx, u32 val)
  184. {
  185. u32 r;
  186. cx18_write_reg_expect(cx, val, SW1_INT_STATUS, ~val, val);
  187. r = cx18_read_reg(cx, SW1_INT_ENABLE_PCI);
  188. cx18_write_reg(cx, r | val, SW1_INT_ENABLE_PCI);
  189. }
  190. void cx18_sw1_irq_disable(struct cx18 *cx, u32 val)
  191. {
  192. u32 r;
  193. r = cx18_read_reg(cx, SW1_INT_ENABLE_PCI);
  194. cx18_write_reg(cx, r & ~val, SW1_INT_ENABLE_PCI);
  195. }
  196. void cx18_sw2_irq_enable(struct cx18 *cx, u32 val)
  197. {
  198. u32 r;
  199. cx18_write_reg_expect(cx, val, SW2_INT_STATUS, ~val, val);
  200. r = cx18_read_reg(cx, SW2_INT_ENABLE_PCI);
  201. cx18_write_reg(cx, r | val, SW2_INT_ENABLE_PCI);
  202. }
  203. void cx18_sw2_irq_disable(struct cx18 *cx, u32 val)
  204. {
  205. u32 r;
  206. r = cx18_read_reg(cx, SW2_INT_ENABLE_PCI);
  207. cx18_write_reg(cx, r & ~val, SW2_INT_ENABLE_PCI);
  208. }
  209. void cx18_sw2_irq_disable_cpu(struct cx18 *cx, u32 val)
  210. {
  211. u32 r;
  212. r = cx18_read_reg(cx, SW2_INT_ENABLE_CPU);
  213. cx18_write_reg(cx, r & ~val, SW2_INT_ENABLE_CPU);
  214. }
  215. void cx18_setup_page(struct cx18 *cx, u32 addr)
  216. {
  217. u32 val;
  218. val = cx18_read_reg(cx, 0xD000F8);
  219. val = (val & ~0x1f00) | ((addr >> 17) & 0x1f00);
  220. cx18_write_reg(cx, val, 0xD000F8);
  221. }