1 /*
2 * linux/arch/sh/io_keywest.c
3 *
4 * IO functions for a Hitachi KeyWest Evaluation Board.
5 *
6 * Copyright (C) 2001 Lineo, Japan
7 *
8 * This file is subject to the terms and conditions of the GNU General Publi
9 c
10 * License. See the file "COPYING" in the main directory of this archive
11 * for more details.
12 *
13 */
14
15 #include <linux/config.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <asm/machvec.h>
19 #include <asm/io.h>
20 #include <asm/keywest.h>
21
22 //#define KEYWEST_DEBUG 3
23 #undef KEYWEST_DEBUG
24
25 #ifdef KEYWEST_DEBUG
26 #define DPRINTK(args...) printk(args)
27 #define DIPRINTK(n, args...) if (KEYWEST_DEBUG>(n)) printk(args)
28 #else
29 #define DPRINTK(args...)
30 #define DIPRINTK(n, args...)
31 #endif
32
33
34 /* Low iomap maps port 0-1K to addresses in 8byte chunks */
35 #define KEYWEST_IOMAP_LO_THRESH 0x400
36 #define KEYWEST_IOMAP_LO_SHIFT 3
37 #define KEYWEST_IOMAP_LO_MASK ((1<<KEYWEST_IOMAP_LO_SHIFT)-1)
38 #define KEYWEST_IOMAP_LO_NMAP (KEYWEST_IOMAP_LO_THRESH>>KEYWEST_IOMAP_LO_SHIFT)
39 static u32 keywest_iomap_lo[KEYWEST_IOMAP_LO_NMAP];
40 static u8 keywest_iomap_lo_shift[KEYWEST_IOMAP_LO_NMAP];
41
42 /* High iomap maps port 1K-64K to addresses in 1K chunks */
43 #define KEYWEST_IOMAP_HI_THRESH 0x10000
44 #define KEYWEST_IOMAP_HI_SHIFT 10
45 #define KEYWEST_IOMAP_HI_MASK ((1<<KEYWEST_IOMAP_HI_SHIFT)-1)
46 #define KEYWEST_IOMAP_HI_NMAP (KEYWEST_IOMAP_HI_THRESH>>KEYWEST_IOMAP_HI_SHIFT)
47 static u32 keywest_iomap_hi[KEYWEST_IOMAP_HI_NMAP];
48 static u8 keywest_iomap_hi_shift[KEYWEST_IOMAP_HI_NMAP];
49
50 #ifndef MAX
51 #define MAX(a,b) ((a)>(b)?(a):(b))
52 #endif
53
54 #define PORT2ADDR(x) keywest_isa_port2addr(x)
55 //#define PORT2ADDR(x) (sh_mv.mv_isa_port2addr(x))
56 //#define PORT2ADDR(x) (x)
57
keywest_port_map(u32 baseport,u32 nports,u32 addr,u8 shift)58 void keywest_port_map(u32 baseport, u32 nports, u32 addr, u8 shift)
59 {
60 u32 port, endport = baseport + nports;
61
62 DPRINTK("keywest_port_map(base=0x%0x, n=0x%0x, addr=0x%08x)\n",
63 baseport, nports, addr);
64
65 for (port = baseport ;
66 port < endport && port < KEYWEST_IOMAP_LO_THRESH ;
67 port += (1<<KEYWEST_IOMAP_LO_SHIFT)) {
68 DPRINTK(" maplo[0x%x] = 0x%08x\n", port, addr);
69 keywest_iomap_lo[port>>KEYWEST_IOMAP_LO_SHIFT] = addr;
70 keywest_iomap_lo_shift[port>>KEYWEST_IOMAP_LO_SHIFT] = shift;
71 addr += (1<<(KEYWEST_IOMAP_LO_SHIFT));
72 }
73
74 for (port = MAX(baseport, KEYWEST_IOMAP_LO_THRESH) ;
75 port < endport && port < KEYWEST_IOMAP_HI_THRESH ;
76 port += (1<<KEYWEST_IOMAP_HI_SHIFT)) {
77 DPRINTK(" maphi[0x%x] = 0x%08x\n", port, addr);
78 keywest_iomap_hi[port>>KEYWEST_IOMAP_HI_SHIFT] = addr;
79 keywest_iomap_hi_shift[port>>KEYWEST_IOMAP_HI_SHIFT] = shift;
80 addr += (1<<(KEYWEST_IOMAP_HI_SHIFT));
81 }
82 }
83 EXPORT_SYMBOL(keywest_port_map);
84
keywest_port_unmap(u32 baseport,u32 nports)85 void keywest_port_unmap(u32 baseport, u32 nports)
86 {
87 u32 port, endport = baseport + nports;
88
89 DPRINTK("keywest_port_unmap(base=0x%0x, n=0x%0x)\n", baseport, nports);
90
91 for (port = baseport ;
92 port < endport && port < KEYWEST_IOMAP_LO_THRESH ;
93 port += (1<<KEYWEST_IOMAP_LO_SHIFT)) {
94 keywest_iomap_lo[port>>KEYWEST_IOMAP_LO_SHIFT] = 0;
95 }
96
97 for (port = MAX(baseport, KEYWEST_IOMAP_LO_THRESH) ;
98 port < endport && port < KEYWEST_IOMAP_HI_THRESH ;
99 port += (1<<KEYWEST_IOMAP_HI_SHIFT)) {
100 keywest_iomap_hi[port>>KEYWEST_IOMAP_HI_SHIFT] = 0;
101 }
102 }
103 EXPORT_SYMBOL(keywest_port_unmap);
104
keywest_isa_port2addr(unsigned long port)105 unsigned long keywest_isa_port2addr(unsigned long port)
106 {
107 unsigned long addr;
108 unsigned char shift;
109
110 /* Physical address not in P0, do nothing */
111 if (PXSEG(port))
112 return(port);
113
114 addr = 0;
115 /* physical address in P0, map to P2 */
116 if (port >= 0x30000)
117 addr = P2SEGADDR(port);
118 /* Key West I/O + HD64465 registers 0x10000-0x30000 */
119 else if (port >= KEYWEST_IOMAP_HI_THRESH)
120 addr = KEYWEST_INTERNAL_BASE + (port - KEYWEST_IOMAP_HI_THRESH);
121 /* Handle remapping of high IO/PCI IO ports */
122 else if (port >= KEYWEST_IOMAP_LO_THRESH) {
123 addr = keywest_iomap_hi[port >> KEYWEST_IOMAP_HI_SHIFT];
124 shift = keywest_iomap_hi_shift[port >> KEYWEST_IOMAP_HI_SHIFT];
125 if (addr != 0)
126 addr += (port & KEYWEST_IOMAP_HI_MASK) << shift;
127 }
128 /* Handle remapping of low IO ports */
129 else {
130 addr = keywest_iomap_lo[port >> KEYWEST_IOMAP_LO_SHIFT];
131 shift = keywest_iomap_lo_shift[port >> KEYWEST_IOMAP_LO_SHIFT];
132 if (addr != 0)
133 addr += (port & KEYWEST_IOMAP_LO_MASK) << shift;
134 }
135
136 DIPRINTK(2, "PORT2ADDR(0x%08lx) = 0x%08lx\n", port, addr);
137
138 return addr;
139 }
140
delay(void)141 static inline void delay(void)
142 {
143 ctrl_inw(0xa0000000);
144 }
145
keywest_inb(unsigned long port)146 unsigned char keywest_inb(unsigned long port)
147 {
148 unsigned long addr = PORT2ADDR(port);
149 unsigned long b = (addr == 0 ? 0 : *(volatile unsigned char*)addr);
150
151 DIPRINTK(0, "inb(%08lx) = %02x\n", addr, (unsigned)b);
152 return b;
153 }
154
keywest_inb_p(unsigned long port)155 unsigned char keywest_inb_p(unsigned long port)
156 {
157 unsigned long v;
158 unsigned long addr = PORT2ADDR(port);
159
160 v = (addr == 0 ? 0 : *(volatile unsigned char*)addr);
161 delay();
162 DIPRINTK(0, "inb_p(%08lx) = %02x\n", addr, (unsigned)v);
163 return v;
164 }
165
keywest_inw(unsigned long port)166 unsigned short keywest_inw(unsigned long port)
167 {
168 unsigned long addr = PORT2ADDR(port);
169 unsigned long b = (addr == 0 ? 0 : *(volatile unsigned short*)addr);
170 DIPRINTK(0, "inw(%08lx) = %04lx\n", addr, b);
171 return b;
172 }
173
keywest_inl(unsigned long port)174 unsigned int keywest_inl(unsigned long port)
175 {
176 unsigned long addr = PORT2ADDR(port);
177 unsigned int b = (addr == 0 ? 0 : *(volatile unsigned long*)addr);
178 DIPRINTK(0, "inl(%08lx) = %08x\n", addr, b);
179 return b;
180 }
181
keywest_insb(unsigned long port,void * buffer,unsigned long count)182 void keywest_insb(unsigned long port, void *buffer, unsigned long count)
183 {
184 unsigned char *buf=buffer;
185 while(count--) *buf++=inb(port);
186 }
187
keywest_insw(unsigned long port,void * buffer,unsigned long count)188 void keywest_insw(unsigned long port, void *buffer, unsigned long count)
189 {
190 unsigned short *buf=buffer;
191 while(count--) *buf++=inw(port);
192 }
193
keywest_insl(unsigned long port,void * buffer,unsigned long count)194 void keywest_insl(unsigned long port, void *buffer, unsigned long count)
195 {
196 #if 0
197 unsigned long *buf=buffer;
198 while(count--) *buf++=inl(port);
199 #else
200 union {
201 unsigned long l;
202 unsigned short s[2];
203 unsigned char c[4];
204 } align;
205 port = (((unsigned long)(port)) & 0xe0000000) ? port :
206 keywest_isa_port2addr(port);
207 switch (((unsigned long) buffer) & 0x3) {
208 case 0: {
209 register unsigned long *buf=buffer, *endp = buf + count;
210 while (buf < endp)
211 *buf++ = * (volatile unsigned long *) port;
212 break;
213 }
214 case 2: {
215 register unsigned short *buf=buffer, *endp = buf + (count << 1);
216 while (buf < endp) {
217 align.l = * (volatile unsigned long *) port;
218 *buf++ = align.s[0];
219 *buf++ = align.s[1];
220 }
221 break;
222 }
223 case 1:
224 case 3: {
225 register unsigned char *buf=buffer, *endp = buf + (count << 2);
226 while (buf < endp) {
227 align.l = * (volatile unsigned long *) port;
228 *buf++ = align.c[0];
229 *buf++ = align.c[1];
230 *buf++ = align.c[2];
231 *buf++ = align.c[3];
232 }
233 break;
234 }
235 }
236 #endif
237 }
238
keywest_outb(unsigned char b,unsigned long port)239 void keywest_outb(unsigned char b, unsigned long port)
240 {
241 unsigned long addr = PORT2ADDR(port);
242
243 DIPRINTK(0, "outb(%02x, %08lx)\n", (unsigned)b, addr);
244 if (addr != 0)
245 *(volatile unsigned char*)addr = b;
246 }
247
keywest_outb_p(unsigned char b,unsigned long port)248 void keywest_outb_p(unsigned char b, unsigned long port)
249 {
250 unsigned long addr = PORT2ADDR(port);
251
252 DIPRINTK(0, "outb_p(%02x, %08lx)\n", (unsigned)b, addr);
253 if (addr != 0)
254 *(volatile unsigned char*)addr = b;
255 delay();
256 }
257
keywest_outw(unsigned short b,unsigned long port)258 void keywest_outw(unsigned short b, unsigned long port)
259 {
260 unsigned long addr = PORT2ADDR(port);
261 DIPRINTK(0, "outw(%04x, %08lx)\n", (unsigned)b, addr);
262 if (addr != 0)
263 *(volatile unsigned short*)addr = b;
264 }
265
keywest_outl(unsigned int b,unsigned long port)266 void keywest_outl(unsigned int b, unsigned long port)
267 {
268 unsigned long addr = PORT2ADDR(port);
269 DIPRINTK(0, "outl(%08x, %08lx)\n", b, addr);
270 if (addr != 0)
271 *(volatile unsigned long*)addr = b;
272 }
273
keywest_outsb(unsigned long port,const void * buffer,unsigned long count)274 void keywest_outsb(unsigned long port, const void *buffer, unsigned long count)
275 {
276 const unsigned char *buf=buffer;
277 while(count--) outb(*buf++, port);
278 }
279
keywest_outsw(unsigned long port,const void * buffer,unsigned long count)280 void keywest_outsw(unsigned long port, const void *buffer, unsigned long count)
281 {
282 const unsigned short *buf=buffer;
283 while(count--) outw(*buf++, port);
284 }
285
keywest_outsl(unsigned long port,const void * buffer,unsigned long count)286 void keywest_outsl(unsigned long port, const void *buffer, unsigned long count)
287 {
288 #if 0
289 const unsigned long *buf=buffer;
290 while(count--) outl(*buf++, port);
291 #else
292 union {
293 unsigned long l;
294 unsigned short s[2];
295 unsigned char c[4];
296 } align;
297 port = (((unsigned long)(port)) & 0xe0000000) ? port :
298 keywest_isa_port2addr(port);
299 switch (((unsigned long) buffer) & 0x3) {
300 case 0: {
301 register unsigned long *buf=buffer, *endp = buf + count;
302 while (buf < endp)
303 * (volatile unsigned long *) port = *buf++;
304 break;
305 }
306 case 2: {
307 register unsigned short *buf=buffer, *endp = buf + (count << 1);
308 while (buf < endp) {
309 align.s[0] = *buf++;
310 align.s[1] = *buf++;
311 * (volatile unsigned long *) port = align.l;
312 }
313 break;
314 }
315 case 1:
316 case 3: {
317 register unsigned char *buf=buffer, *endp = buf + (count << 2);
318 while (buf < endp) {
319 align.c[0] = *buf++;
320 align.c[1] = *buf++;
321 align.c[2] = *buf++;
322 align.c[3] = *buf++;
323 * (volatile unsigned long *) port = align.l;
324 }
325 break;
326 }
327 }
328 #endif
329 }
330
331