cx18-io.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_memcpy_fromio(struct cx18 *cx, void *to,
  155. const void __iomem *from, unsigned int len)
  156. {
  157. const u8 __iomem *src = from;
  158. u8 *dst = to;
  159. /* Align reads on the CX23418's addresses */
  160. if ((len > 0) && ((unsigned long) src & 1)) {
  161. *dst = cx18_readb(cx, src);
  162. len--;
  163. dst++;
  164. src++;
  165. }
  166. if ((len > 1) && ((unsigned long) src & 2)) {
  167. *((u16 *)dst) = cx18_raw_readw(cx, src);
  168. len -= 2;
  169. dst += 2;
  170. src += 2;
  171. }
  172. while (len > 3) {
  173. *((u32 *)dst) = cx18_raw_readl(cx, src);
  174. len -= 4;
  175. dst += 4;
  176. src += 4;
  177. }
  178. if (len > 1) {
  179. *((u16 *)dst) = cx18_raw_readw(cx, src);
  180. len -= 2;
  181. dst += 2;
  182. src += 2;
  183. }
  184. if (len > 0)
  185. *dst = cx18_readb(cx, src);
  186. }
  187. void cx18_memset_io(struct cx18 *cx, void __iomem *addr, int val, size_t count)
  188. {
  189. u8 __iomem *dst = addr;
  190. u16 val2 = val | (val << 8);
  191. u32 val4 = val2 | (val2 << 16);
  192. /* Align writes on the CX23418's addresses */
  193. if ((count > 0) && ((unsigned long)dst & 1)) {
  194. cx18_writeb(cx, (u8) val, dst);
  195. count--;
  196. dst++;
  197. }
  198. if ((count > 1) && ((unsigned long)dst & 2)) {
  199. cx18_writew(cx, val2, dst);
  200. count -= 2;
  201. dst += 2;
  202. }
  203. while (count > 3) {
  204. cx18_writel(cx, val4, dst);
  205. count -= 4;
  206. dst += 4;
  207. }
  208. if (count > 1) {
  209. cx18_writew(cx, val2, dst);
  210. count -= 2;
  211. dst += 2;
  212. }
  213. if (count > 0)
  214. cx18_writeb(cx, (u8) val, dst);
  215. }
  216. void cx18_sw1_irq_enable(struct cx18 *cx, u32 val)
  217. {
  218. u32 r;
  219. cx18_write_reg_expect(cx, val, SW1_INT_STATUS, ~val, val);
  220. r = cx18_read_reg(cx, SW1_INT_ENABLE_PCI);
  221. cx18_write_reg(cx, r | val, SW1_INT_ENABLE_PCI);
  222. }
  223. void cx18_sw1_irq_disable(struct cx18 *cx, u32 val)
  224. {
  225. u32 r;
  226. r = cx18_read_reg(cx, SW1_INT_ENABLE_PCI);
  227. cx18_write_reg(cx, r & ~val, SW1_INT_ENABLE_PCI);
  228. }
  229. void cx18_sw2_irq_enable(struct cx18 *cx, u32 val)
  230. {
  231. u32 r;
  232. cx18_write_reg_expect(cx, val, SW2_INT_STATUS, ~val, val);
  233. r = cx18_read_reg(cx, SW2_INT_ENABLE_PCI);
  234. cx18_write_reg(cx, r | val, SW2_INT_ENABLE_PCI);
  235. }
  236. void cx18_sw2_irq_disable(struct cx18 *cx, u32 val)
  237. {
  238. u32 r;
  239. r = cx18_read_reg(cx, SW2_INT_ENABLE_PCI);
  240. cx18_write_reg(cx, r & ~val, SW2_INT_ENABLE_PCI);
  241. }
  242. void cx18_setup_page(struct cx18 *cx, u32 addr)
  243. {
  244. u32 val;
  245. val = cx18_read_reg(cx, 0xD000F8);
  246. val = (val & ~0x1f00) | ((addr >> 17) & 0x1f00);
  247. cx18_write_reg(cx, val, 0xD000F8);
  248. }