blob: 73321fb4cbf50f4cf8947932f1d034fbe23c5671 [file] [log] [blame]
Andy Wallsb1526422008-08-30 16:03:44 -03001/*
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#ifndef CX18_IO_H
24#define CX18_IO_H
25
26#include "cx18-driver.h"
27
Andy Wallsd267d852008-09-28 21:46:02 -030028/*
29 * Readback and retry of MMIO access for reliability:
30 * The concept was suggested by Steve Toth <stoth@linuxtv.org>.
31 * The implmentation is the fault of Andy Walls <awalls@radix.net>.
Andy Walls3f75c612008-11-16 23:33:41 -030032 *
33 * *write* functions are implied to retry the mmio unless suffixed with _noretry
34 * *read* functions never retry the mmio (it never helps to do so)
Andy Wallsd267d852008-09-28 21:46:02 -030035 */
36
37/* Statistics gathering */
Andy Wallsd267d852008-09-28 21:46:02 -030038
39void cx18_log_statistics(struct cx18 *cx);
40
Andy Wallsc641d092008-09-01 00:40:41 -030041/* Non byteswapping memory mapped IO */
Andy Walls3f75c612008-11-16 23:33:41 -030042static inline u32 cx18_raw_readl(struct cx18 *cx, const void __iomem *addr)
43{
44 return __raw_readl(addr);
45}
46
Andy Wallsd267d852008-09-28 21:46:02 -030047static inline
48void cx18_raw_writel_noretry(struct cx18 *cx, u32 val, void __iomem *addr)
Andy Wallsc641d092008-09-01 00:40:41 -030049{
50 __raw_writel(val, addr);
Andy Wallsc641d092008-09-01 00:40:41 -030051}
Andy Wallsb1526422008-08-30 16:03:44 -030052
Andy Wallsd267d852008-09-28 21:46:02 -030053static inline void cx18_raw_writel(struct cx18 *cx, u32 val, void __iomem *addr)
54{
Andy Walls3f75c612008-11-16 23:33:41 -030055 int i;
56 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
Andy Wallsd267d852008-09-28 21:46:02 -030057 cx18_raw_writel_noretry(cx, val, addr);
Andy Walls3f75c612008-11-16 23:33:41 -030058 if (val == cx18_raw_readl(cx, addr))
59 break;
60 }
Andy Wallsd267d852008-09-28 21:46:02 -030061}
62
Andy Wallsc641d092008-09-01 00:40:41 -030063/* Normal memory mapped IO */
Andy Walls3f75c612008-11-16 23:33:41 -030064static inline u32 cx18_readl(struct cx18 *cx, const void __iomem *addr)
65{
66 return readl(addr);
67}
68
Andy Wallsd267d852008-09-28 21:46:02 -030069static inline
70void cx18_writel_noretry(struct cx18 *cx, u32 val, void __iomem *addr)
Andy Wallsc641d092008-09-01 00:40:41 -030071{
72 writel(val, addr);
Andy Wallsc641d092008-09-01 00:40:41 -030073}
Andy Wallsb1526422008-08-30 16:03:44 -030074
Andy Wallsd267d852008-09-28 21:46:02 -030075static inline void cx18_writel(struct cx18 *cx, u32 val, void __iomem *addr)
76{
Andy Walls3f75c612008-11-16 23:33:41 -030077 int i;
78 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
Andy Wallsd267d852008-09-28 21:46:02 -030079 cx18_writel_noretry(cx, val, addr);
Andy Walls3f75c612008-11-16 23:33:41 -030080 if (val == cx18_readl(cx, addr))
81 break;
82 }
Andy Wallsd267d852008-09-28 21:46:02 -030083}
84
Andy Walls3f75c612008-11-16 23:33:41 -030085static inline
86void 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(cx, addr) & mask))
94 break;
95 }
96}
97
98static inline u16 cx18_readw(struct cx18 *cx, const void __iomem *addr)
99{
100 return readw(addr);
101}
Andy Wallsd267d852008-09-28 21:46:02 -0300102
103static inline
104void cx18_writew_noretry(struct cx18 *cx, u16 val, void __iomem *addr)
Andy Wallsc641d092008-09-01 00:40:41 -0300105{
106 writew(val, addr);
Andy Wallsc641d092008-09-01 00:40:41 -0300107}
108
Andy Wallsd267d852008-09-28 21:46:02 -0300109static inline void cx18_writew(struct cx18 *cx, u16 val, void __iomem *addr)
110{
Andy Walls3f75c612008-11-16 23:33:41 -0300111 int i;
112 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
Andy Wallsd267d852008-09-28 21:46:02 -0300113 cx18_writew_noretry(cx, val, addr);
Andy Walls3f75c612008-11-16 23:33:41 -0300114 if (val == cx18_readw(cx, addr))
115 break;
116 }
Andy Wallsd267d852008-09-28 21:46:02 -0300117}
118
Andy Walls3f75c612008-11-16 23:33:41 -0300119static inline u8 cx18_readb(struct cx18 *cx, const void __iomem *addr)
120{
121 return readb(addr);
122}
Andy Wallsd267d852008-09-28 21:46:02 -0300123
124static inline
125void cx18_writeb_noretry(struct cx18 *cx, u8 val, void __iomem *addr)
Andy Wallsc641d092008-09-01 00:40:41 -0300126{
127 writeb(val, addr);
Andy Wallsc641d092008-09-01 00:40:41 -0300128}
129
Andy Wallsd267d852008-09-28 21:46:02 -0300130static inline void cx18_writeb(struct cx18 *cx, u8 val, void __iomem *addr)
131{
Andy Walls3f75c612008-11-16 23:33:41 -0300132 int i;
133 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
Andy Wallsd267d852008-09-28 21:46:02 -0300134 cx18_writeb_noretry(cx, val, addr);
Andy Walls3f75c612008-11-16 23:33:41 -0300135 if (val == cx18_readb(cx, addr))
136 break;
137 }
Andy Wallsd267d852008-09-28 21:46:02 -0300138}
139
Andy Wallsee2d64f2008-11-16 01:38:19 -0300140static inline
Andy Wallsb1526422008-08-30 16:03:44 -0300141void cx18_memcpy_fromio(struct cx18 *cx, void *to,
Andy Wallsee2d64f2008-11-16 01:38:19 -0300142 const void __iomem *from, unsigned int len)
143{
144 memcpy_fromio(to, from, len);
145}
146
Andy Wallsb1526422008-08-30 16:03:44 -0300147void cx18_memset_io(struct cx18 *cx, void __iomem *addr, int val, size_t count);
148
Andy Wallsd267d852008-09-28 21:46:02 -0300149
Andy Wallsc641d092008-09-01 00:40:41 -0300150/* Access "register" region of CX23418 memory mapped I/O */
Andy Wallsd267d852008-09-28 21:46:02 -0300151static inline void cx18_write_reg_noretry(struct cx18 *cx, u32 val, u32 reg)
152{
153 cx18_writel_noretry(cx, val, cx->reg_mem + reg);
154}
155
Andy Wallsc641d092008-09-01 00:40:41 -0300156static inline void cx18_write_reg(struct cx18 *cx, u32 val, u32 reg)
157{
Andy Walls3f75c612008-11-16 23:33:41 -0300158 cx18_writel(cx, val, cx->reg_mem + reg);
Andy Wallsf056d292008-10-31 20:49:12 -0300159}
160
161static inline void cx18_write_reg_expect(struct cx18 *cx, u32 val, u32 reg,
162 u32 eval, u32 mask)
163{
Andy Walls3f75c612008-11-16 23:33:41 -0300164 cx18_writel_expect(cx, val, cx->reg_mem + reg, eval, mask);
Andy Wallsc641d092008-09-01 00:40:41 -0300165}
166
167static inline u32 cx18_read_reg(struct cx18 *cx, u32 reg)
168{
Andy Walls3f75c612008-11-16 23:33:41 -0300169 return cx18_readl(cx, cx->reg_mem + reg);
Andy Wallsc641d092008-09-01 00:40:41 -0300170}
171
Andy Wallsd267d852008-09-28 21:46:02 -0300172
Andy Wallsc641d092008-09-01 00:40:41 -0300173/* Access "encoder memory" region of CX23418 memory mapped I/O */
174static inline void cx18_write_enc(struct cx18 *cx, u32 val, u32 addr)
175{
Andy Walls3f75c612008-11-16 23:33:41 -0300176 cx18_writel(cx, val, cx->enc_mem + addr);
Andy Wallsc641d092008-09-01 00:40:41 -0300177}
178
179static inline u32 cx18_read_enc(struct cx18 *cx, u32 addr)
180{
Andy Walls3f75c612008-11-16 23:33:41 -0300181 return cx18_readl(cx, cx->enc_mem + addr);
Andy Wallsd267d852008-09-28 21:46:02 -0300182}
Andy Wallsc641d092008-09-01 00:40:41 -0300183
Andy Wallsb1526422008-08-30 16:03:44 -0300184void cx18_sw1_irq_enable(struct cx18 *cx, u32 val);
185void cx18_sw1_irq_disable(struct cx18 *cx, u32 val);
186void cx18_sw2_irq_enable(struct cx18 *cx, u32 val);
187void cx18_sw2_irq_disable(struct cx18 *cx, u32 val);
Andy Wallsd20ceec2008-11-09 18:14:07 -0300188void cx18_sw2_irq_disable_cpu(struct cx18 *cx, u32 val);
Andy Wallsb1526422008-08-30 16:03:44 -0300189void cx18_setup_page(struct cx18 *cx, u32 addr);
190
Andy Wallsb1526422008-08-30 16:03:44 -0300191#endif /* CX18_IO_H */