2 * cx18 driver PCI memory mapped IO access routines
4 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
5 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
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.
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.
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
23 #include "cx18-driver.h"
27 void cx18_log_statistics(struct cx18 *cx)
31 if (!(cx18_debug & CX18_DBGFLG_INFO))
34 for (i = 0; i <= CX18_MAX_MMIO_WR_RETRIES; i++)
35 CX18_DEBUG_INFO("retried_write[%d] = %d\n", i,
36 atomic_read(&cx->mmio_stats.retried_write[i]));
37 for (i = 0; i <= CX18_MAX_MMIO_RD_RETRIES; i++)
38 CX18_DEBUG_INFO("retried_read[%d] = %d\n", i,
39 atomic_read(&cx->mmio_stats.retried_read[i]));
40 for (i = 0; i <= CX18_MAX_MB_ACK_DELAY; i++)
41 if (atomic_read(&cx->mbox_stats.mb_ack_delay[i]))
42 CX18_DEBUG_INFO("mb_ack_delay[%d] = %d\n", i,
43 atomic_read(&cx->mbox_stats.mb_ack_delay[i]));
47 void cx18_raw_writel_retry(struct cx18 *cx, u32 val, void __iomem *addr)
50 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
51 cx18_raw_writel_noretry(cx, val, addr);
52 if (val == cx18_raw_readl_noretry(cx, addr))
55 cx18_log_write_retries(cx, i, addr);
58 u32 cx18_raw_readl_retry(struct cx18 *cx, const void __iomem *addr)
62 for (i = 0; i < CX18_MAX_MMIO_RD_RETRIES; i++) {
63 val = cx18_raw_readl_noretry(cx, addr);
64 if (val != 0xffffffff) /* PCI bus read error */
67 cx18_log_read_retries(cx, i, addr);
71 u16 cx18_raw_readw_retry(struct cx18 *cx, const void __iomem *addr)
75 for (i = 0; i < CX18_MAX_MMIO_RD_RETRIES; i++) {
76 val = cx18_raw_readw_noretry(cx, addr);
77 if (val != 0xffff) /* PCI bus read error */
80 cx18_log_read_retries(cx, i, addr);
84 void cx18_writel_retry(struct cx18 *cx, u32 val, void __iomem *addr)
87 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
88 cx18_writel_noretry(cx, val, addr);
89 if (val == cx18_readl_noretry(cx, addr))
92 cx18_log_write_retries(cx, i, addr);
95 void _cx18_writel_expect(struct cx18 *cx, u32 val, void __iomem *addr,
100 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
101 cx18_writel_noretry(cx, val, addr);
102 if (eval == (cx18_readl_noretry(cx, addr) & mask))
105 cx18_log_write_retries(cx, i, addr);
108 void cx18_writew_retry(struct cx18 *cx, u16 val, void __iomem *addr)
111 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
112 cx18_writew_noretry(cx, val, addr);
113 if (val == cx18_readw_noretry(cx, addr))
116 cx18_log_write_retries(cx, i, addr);
119 void cx18_writeb_retry(struct cx18 *cx, u8 val, void __iomem *addr)
122 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
123 cx18_writeb_noretry(cx, val, addr);
124 if (val == cx18_readb_noretry(cx, addr))
127 cx18_log_write_retries(cx, i, addr);
130 u32 cx18_readl_retry(struct cx18 *cx, const void __iomem *addr)
134 for (i = 0; i < CX18_MAX_MMIO_RD_RETRIES; i++) {
135 val = cx18_readl_noretry(cx, addr);
136 if (val != 0xffffffff) /* PCI bus read error */
139 cx18_log_read_retries(cx, i, addr);
143 u16 cx18_readw_retry(struct cx18 *cx, const void __iomem *addr)
147 for (i = 0; i < CX18_MAX_MMIO_RD_RETRIES; i++) {
148 val = cx18_readw_noretry(cx, addr);
149 if (val != 0xffff) /* PCI bus read error */
152 cx18_log_read_retries(cx, i, addr);
156 u8 cx18_readb_retry(struct cx18 *cx, const void __iomem *addr)
160 for (i = 0; i < CX18_MAX_MMIO_RD_RETRIES; i++) {
161 val = cx18_readb_noretry(cx, addr);
162 if (val != 0xff) /* PCI bus read error */
165 cx18_log_read_retries(cx, i, addr);
169 void cx18_memcpy_fromio(struct cx18 *cx, void *to,
170 const void __iomem *from, unsigned int len)
172 const u8 __iomem *src = from;
175 /* Align reads on the CX23418's addresses */
176 if ((len > 0) && ((unsigned long) src & 1)) {
177 *dst = cx18_readb(cx, src);
182 if ((len > 1) && ((unsigned long) src & 2)) {
183 *((u16 *)dst) = cx18_raw_readw(cx, src);
189 *((u32 *)dst) = cx18_raw_readl(cx, src);
195 *((u16 *)dst) = cx18_raw_readw(cx, src);
201 *dst = cx18_readb(cx, src);
204 void cx18_memset_io(struct cx18 *cx, void __iomem *addr, int val, size_t count)
206 u8 __iomem *dst = addr;
207 u16 val2 = val | (val << 8);
208 u32 val4 = val2 | (val2 << 16);
210 /* Align writes on the CX23418's addresses */
211 if ((count > 0) && ((unsigned long)dst & 1)) {
212 cx18_writeb(cx, (u8) val, dst);
216 if ((count > 1) && ((unsigned long)dst & 2)) {
217 cx18_writew(cx, val2, dst);
222 cx18_writel(cx, val4, dst);
227 cx18_writew(cx, val2, dst);
232 cx18_writeb(cx, (u8) val, dst);
235 void cx18_sw1_irq_enable(struct cx18 *cx, u32 val)
238 cx18_write_reg_expect(cx, val, SW1_INT_STATUS, ~val, val);
239 r = cx18_read_reg(cx, SW1_INT_ENABLE_PCI);
240 cx18_write_reg(cx, r | val, SW1_INT_ENABLE_PCI);
243 void cx18_sw1_irq_disable(struct cx18 *cx, u32 val)
246 r = cx18_read_reg(cx, SW1_INT_ENABLE_PCI);
247 cx18_write_reg(cx, r & ~val, SW1_INT_ENABLE_PCI);
250 void cx18_sw2_irq_enable(struct cx18 *cx, u32 val)
253 cx18_write_reg_expect(cx, val, SW2_INT_STATUS, ~val, val);
254 r = cx18_read_reg(cx, SW2_INT_ENABLE_PCI);
255 cx18_write_reg(cx, r | val, SW2_INT_ENABLE_PCI);
258 void cx18_sw2_irq_disable(struct cx18 *cx, u32 val)
261 r = cx18_read_reg(cx, SW2_INT_ENABLE_PCI);
262 cx18_write_reg(cx, r & ~val, SW2_INT_ENABLE_PCI);
265 void cx18_sw2_irq_disable_cpu(struct cx18 *cx, u32 val)
268 r = cx18_read_reg(cx, SW2_INT_ENABLE_CPU);
269 cx18_write_reg(cx, r & ~val, SW2_INT_ENABLE_CPU);
272 void cx18_setup_page(struct cx18 *cx, u32 addr)
275 val = cx18_read_reg(cx, 0xD000F8);
276 val = (val & ~0x1f00) | ((addr >> 17) & 0x1f00);
277 cx18_write_reg(cx, val, 0xD000F8);