blob: 18974d886cf729e01d777f10a3f2226a936b397d [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>
Andy Walls6afdeaf2010-05-23 18:53:35 -03005 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
Andy Wallsb1526422008-08-30 16:03:44 -03006 *
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>.
Andy Walls6afdeaf2010-05-23 18:53:35 -030031 * The implmentation is the fault of Andy Walls <awalls@md.metrocast.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
Andy Wallsc641d092008-09-01 00:40:41 -030037/* Non byteswapping memory mapped IO */
Andy Walls3f75c612008-11-16 23:33:41 -030038static inline u32 cx18_raw_readl(struct cx18 *cx, const void __iomem *addr)
39{
40 return __raw_readl(addr);
41}
42
Andy Wallsd267d852008-09-28 21:46:02 -030043static inline
44void cx18_raw_writel_noretry(struct cx18 *cx, u32 val, void __iomem *addr)
Andy Wallsc641d092008-09-01 00:40:41 -030045{
46 __raw_writel(val, addr);
Andy Wallsc641d092008-09-01 00:40:41 -030047}
Andy Wallsb1526422008-08-30 16:03:44 -030048
Andy Wallsd267d852008-09-28 21:46:02 -030049static inline void cx18_raw_writel(struct cx18 *cx, u32 val, void __iomem *addr)
50{
Andy Walls3f75c612008-11-16 23:33:41 -030051 int i;
52 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
Andy Wallsd267d852008-09-28 21:46:02 -030053 cx18_raw_writel_noretry(cx, val, addr);
Andy Walls3f75c612008-11-16 23:33:41 -030054 if (val == cx18_raw_readl(cx, addr))
55 break;
56 }
Andy Wallsd267d852008-09-28 21:46:02 -030057}
58
Andy Wallsc641d092008-09-01 00:40:41 -030059/* Normal memory mapped IO */
Andy Walls3f75c612008-11-16 23:33:41 -030060static inline u32 cx18_readl(struct cx18 *cx, const void __iomem *addr)
61{
62 return readl(addr);
63}
64
Andy Wallsd267d852008-09-28 21:46:02 -030065static inline
66void cx18_writel_noretry(struct cx18 *cx, u32 val, void __iomem *addr)
Andy Wallsc641d092008-09-01 00:40:41 -030067{
68 writel(val, addr);
Andy Wallsc641d092008-09-01 00:40:41 -030069}
Andy Wallsb1526422008-08-30 16:03:44 -030070
Andy Wallsd267d852008-09-28 21:46:02 -030071static inline void cx18_writel(struct cx18 *cx, u32 val, void __iomem *addr)
72{
Andy Walls3f75c612008-11-16 23:33:41 -030073 int i;
74 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
Andy Wallsd267d852008-09-28 21:46:02 -030075 cx18_writel_noretry(cx, val, addr);
Andy Walls3f75c612008-11-16 23:33:41 -030076 if (val == cx18_readl(cx, addr))
77 break;
78 }
Andy Wallsd267d852008-09-28 21:46:02 -030079}
80
Andy Walls3f75c612008-11-16 23:33:41 -030081static inline
82void cx18_writel_expect(struct cx18 *cx, u32 val, void __iomem *addr,
83 u32 eval, u32 mask)
84{
85 int i;
Andy Wallsdaa1c162008-11-30 10:01:21 -030086 u32 r;
Andy Walls3f75c612008-11-16 23:33:41 -030087 eval &= mask;
88 for (i = 0; i < CX18_MAX_MMIO_WR_RETRIES; i++) {
89 cx18_writel_noretry(cx, val, addr);
Andy Wallsdaa1c162008-11-30 10:01:21 -030090 r = cx18_readl(cx, addr);
91 if (r == 0xffffffff && eval != 0xffffffff)
92 continue;
93 if (eval == (r & mask))
Andy Walls3f75c612008-11-16 23:33:41 -030094 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 */