V4L/DVB (8914): cx18: Throttle mmio to/from the CX23418 so boards work in older systems
[safe/jmp/linux-2.6] / drivers / media / video / cx18 / cx18-io.c
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
23 #include "cx18-driver.h"
24 #include "cx18-io.h"
25 #include "cx18-irq.h"
26
27 void cx18_memcpy_fromio(struct cx18 *cx, void *to,
28                         const void __iomem *from, unsigned int len)
29 {
30         /* Align reads on the CX23418's addresses */
31         if ((len > 0) && ((unsigned)from & 1)) {
32                 *((u8 *)to) = cx18_readb(cx, from);
33                 len--;
34                 to++;
35                 from++;
36         }
37         if ((len > 1) && ((unsigned)from & 2)) {
38                 *((u16 *)to) = cx18_raw_readw(cx, from);
39                 len -= 2;
40                 to += 2;
41                 from += 2;
42         }
43         while (len > 3) {
44                 *((u32 *)to) = cx18_raw_readl(cx, from);
45                 len -= 4;
46                 to += 4;
47                 from += 4;
48         }
49         if (len > 1) {
50                 *((u16 *)to) = cx18_raw_readw(cx, from);
51                 len -= 2;
52                 to += 2;
53                 from += 2;
54         }
55         if (len > 0)
56                 *((u8 *)to) = cx18_readb(cx, from);
57 }
58
59 void cx18_memset_io(struct cx18 *cx, void __iomem *addr, int val, size_t count)
60 {
61         u16 val2 = val | (val << 8);
62         u32 val4 = val2 | (val2 << 16);
63
64         /* Align writes on the CX23418's addresses */
65         if ((count > 0) && ((unsigned)addr & 1)) {
66                 cx18_writeb(cx, (u8) val, addr);
67                 count--;
68                 addr++;
69         }
70         if ((count > 1) && ((unsigned)addr & 2)) {
71                 cx18_writew(cx, val2, addr);
72                 count -= 2;
73                 addr += 2;
74         }
75         while (count > 3) {
76                 cx18_writel(cx, val4, addr);
77                 count -= 4;
78                 addr += 4;
79         }
80         if (count > 1) {
81                 cx18_writew(cx, val2, addr);
82                 count -= 2;
83                 addr += 2;
84         }
85         if (count > 0)
86                 cx18_writeb(cx, (u8) val, addr);
87 }
88
89 void cx18_sw1_irq_enable(struct cx18 *cx, u32 val)
90 {
91         u32 r;
92         cx18_write_reg(cx, val, SW1_INT_STATUS);
93         r = cx18_read_reg(cx, SW1_INT_ENABLE_PCI);
94         cx18_write_reg(cx, r | val, SW1_INT_ENABLE_PCI);
95 }
96
97 void cx18_sw1_irq_disable(struct cx18 *cx, u32 val)
98 {
99         u32 r;
100         r = cx18_read_reg(cx, SW1_INT_ENABLE_PCI);
101         cx18_write_reg(cx, r & ~val, SW1_INT_ENABLE_PCI);
102 }
103
104 void cx18_sw2_irq_enable(struct cx18 *cx, u32 val)
105 {
106         u32 r;
107         cx18_write_reg(cx, val, SW2_INT_STATUS);
108         r = cx18_read_reg(cx, SW2_INT_ENABLE_PCI);
109         cx18_write_reg(cx, r | val, SW2_INT_ENABLE_PCI);
110 }
111
112 void cx18_sw2_irq_disable(struct cx18 *cx, u32 val)
113 {
114         u32 r;
115         r = cx18_read_reg(cx, SW2_INT_ENABLE_PCI);
116         cx18_write_reg(cx, r & ~val, SW2_INT_ENABLE_PCI);
117 }
118
119 void cx18_setup_page(struct cx18 *cx, u32 addr)
120 {
121         u32 val;
122         val = cx18_read_reg(cx, 0xD000F8);
123         val = (val & ~0x1f00) | ((addr >> 17) & 0x1f00);
124         cx18_write_reg(cx, val, 0xD000F8);
125 }
126
127 /* Tries to recover from the CX23418 responding improperly on the PCI bus */
128 int cx18_pci_try_recover(struct cx18 *cx)
129 {
130         u16 status;
131
132         pci_read_config_word(cx->dev, PCI_STATUS, &status);
133         pci_write_config_word(cx->dev, PCI_STATUS, status);
134         return 0;
135 }