1 /*
2 * include/asm-sh/io_bigsur.c
3 *
4 * By Dustin McIntire (dustin@sensoria.com) (c)2001
5 * Derived from io_hd64465.h, which bore the message:
6 * By Greg Banks <gbanks@pocketpenguins.com>
7 * (c) 2000 PocketPenguins Inc.
8 * and from io_hd64461.h, which bore the message:
9 * Copyright 2000 Stuart Menefy (stuart.menefy@st.com)
10 *
11 * May be copied or modified under the terms of the GNU General Public
12 * License. See linux/COPYING for more information.
13 *
14 * IO functions for a Hitachi Big Sur Evaluation Board.
15 */
16
17 #include <linux/config.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <asm/machvec.h>
21 #include <asm/io.h>
22 #include <asm/bigsur.h>
23
24 //#define BIGSUR_DEBUG 2
25 #undef BIGSUR_DEBUG
26
27 #ifdef BIGSUR_DEBUG
28 #define DPRINTK(args...) printk(args)
29 #define DIPRINTK(n, args...) if (BIGSUR_DEBUG>(n)) printk(args)
30 #else
31 #define DPRINTK(args...)
32 #define DIPRINTK(n, args...)
33 #endif
34
35
36 /* Low iomap maps port 0-1K to addresses in 8byte chunks */
37 #define BIGSUR_IOMAP_LO_THRESH 0x400
38 #define BIGSUR_IOMAP_LO_SHIFT 3
39 #define BIGSUR_IOMAP_LO_MASK ((1<<BIGSUR_IOMAP_LO_SHIFT)-1)
40 #define BIGSUR_IOMAP_LO_NMAP (BIGSUR_IOMAP_LO_THRESH>>BIGSUR_IOMAP_LO_SHIFT)
41 static u32 bigsur_iomap_lo[BIGSUR_IOMAP_LO_NMAP];
42 static u8 bigsur_iomap_lo_shift[BIGSUR_IOMAP_LO_NMAP];
43
44 /* High iomap maps port 1K-64K to addresses in 1K chunks */
45 #define BIGSUR_IOMAP_HI_THRESH 0x10000
46 #define BIGSUR_IOMAP_HI_SHIFT 10
47 #define BIGSUR_IOMAP_HI_MASK ((1<<BIGSUR_IOMAP_HI_SHIFT)-1)
48 #define BIGSUR_IOMAP_HI_NMAP (BIGSUR_IOMAP_HI_THRESH>>BIGSUR_IOMAP_HI_SHIFT)
49 static u32 bigsur_iomap_hi[BIGSUR_IOMAP_HI_NMAP];
50 static u8 bigsur_iomap_hi_shift[BIGSUR_IOMAP_HI_NMAP];
51
52 #ifndef MAX
53 #define MAX(a,b) ((a)>(b)?(a):(b))
54 #endif
55
56 #define PORT2ADDR(x) (sh_mv.mv_isa_port2addr(x))
57
bigsur_port_map(u32 baseport,u32 nports,u32 addr,u8 shift)58 void bigsur_port_map(u32 baseport, u32 nports, u32 addr, u8 shift)
59 {
60 u32 port, endport = baseport + nports;
61
62 DPRINTK("bigsur_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 < BIGSUR_IOMAP_LO_THRESH ;
67 port += (1<<BIGSUR_IOMAP_LO_SHIFT)) {
68 DPRINTK(" maplo[0x%x] = 0x%08x\n", port, addr);
69 bigsur_iomap_lo[port>>BIGSUR_IOMAP_LO_SHIFT] = addr;
70 bigsur_iomap_lo_shift[port>>BIGSUR_IOMAP_LO_SHIFT] = shift;
71 addr += (1<<(BIGSUR_IOMAP_LO_SHIFT));
72 }
73
74 for (port = MAX(baseport, BIGSUR_IOMAP_LO_THRESH) ;
75 port < endport && port < BIGSUR_IOMAP_HI_THRESH ;
76 port += (1<<BIGSUR_IOMAP_HI_SHIFT)) {
77 DPRINTK(" maphi[0x%x] = 0x%08x\n", port, addr);
78 bigsur_iomap_hi[port>>BIGSUR_IOMAP_HI_SHIFT] = addr;
79 bigsur_iomap_hi_shift[port>>BIGSUR_IOMAP_HI_SHIFT] = shift;
80 addr += (1<<(BIGSUR_IOMAP_HI_SHIFT));
81 }
82 }
83 EXPORT_SYMBOL(bigsur_port_map);
84
bigsur_port_unmap(u32 baseport,u32 nports)85 void bigsur_port_unmap(u32 baseport, u32 nports)
86 {
87 u32 port, endport = baseport + nports;
88
89 DPRINTK("bigsur_port_unmap(base=0x%0x, n=0x%0x)\n", baseport, nports);
90
91 for (port = baseport ;
92 port < endport && port < BIGSUR_IOMAP_LO_THRESH ;
93 port += (1<<BIGSUR_IOMAP_LO_SHIFT)) {
94 bigsur_iomap_lo[port>>BIGSUR_IOMAP_LO_SHIFT] = 0;
95 }
96
97 for (port = MAX(baseport, BIGSUR_IOMAP_LO_THRESH) ;
98 port < endport && port < BIGSUR_IOMAP_HI_THRESH ;
99 port += (1<<BIGSUR_IOMAP_HI_SHIFT)) {
100 bigsur_iomap_hi[port>>BIGSUR_IOMAP_HI_SHIFT] = 0;
101 }
102 }
103 EXPORT_SYMBOL(bigsur_port_unmap);
104
bigsur_isa_port2addr(unsigned long port)105 unsigned long bigsur_isa_port2addr(unsigned long port)
106 {
107 unsigned long addr = 0;
108 unsigned char shift;
109
110 /* Physical address not in P0, do nothing */
111 if (PXSEG(port)) addr = port;
112 /* physical address in P0, map to P2 */
113 else if (port >= 0x30000)
114 addr = P2SEGADDR(port);
115 /* Big Sur I/O + HD64465 registers 0x10000-0x30000 */
116 else if (port >= BIGSUR_IOMAP_HI_THRESH)
117 addr = BIGSUR_INTERNAL_BASE + (port - BIGSUR_IOMAP_HI_THRESH);
118 /* Handle remapping of high IO/PCI IO ports */
119 else if (port >= BIGSUR_IOMAP_LO_THRESH) {
120 addr = bigsur_iomap_hi[port >> BIGSUR_IOMAP_HI_SHIFT];
121 shift = bigsur_iomap_hi_shift[port >> BIGSUR_IOMAP_HI_SHIFT];
122 if (addr != 0)
123 addr += (port & BIGSUR_IOMAP_HI_MASK) << shift;
124 }
125 /* Handle remapping of low IO ports */
126 else {
127 addr = bigsur_iomap_lo[port >> BIGSUR_IOMAP_LO_SHIFT];
128 shift = bigsur_iomap_lo_shift[port >> BIGSUR_IOMAP_LO_SHIFT];
129 if (addr != 0)
130 addr += (port & BIGSUR_IOMAP_LO_MASK) << shift;
131 }
132
133 DIPRINTK(2, "PORT2ADDR(0x%08lx) = 0x%08lx\n", port, addr);
134
135 return addr;
136 }
137
delay(void)138 static inline void delay(void)
139 {
140 ctrl_inw(0xa0000000);
141 }
142
bigsur_inb(unsigned long port)143 unsigned char bigsur_inb(unsigned long port)
144 {
145 unsigned long addr = PORT2ADDR(port);
146 unsigned long b = (addr == 0 ? 0 : *(volatile unsigned char*)addr);
147
148 DIPRINTK(0, "inb(%08lx) = %02x\n", addr, (unsigned)b);
149 return b;
150 }
151
bigsur_inb_p(unsigned long port)152 unsigned char bigsur_inb_p(unsigned long port)
153 {
154 unsigned long v;
155 unsigned long addr = PORT2ADDR(port);
156
157 v = (addr == 0 ? 0 : *(volatile unsigned char*)addr);
158 delay();
159 DIPRINTK(0, "inb_p(%08lx) = %02x\n", addr, (unsigned)v);
160 return v;
161 }
162
bigsur_inw(unsigned long port)163 unsigned short bigsur_inw(unsigned long port)
164 {
165 unsigned long addr = PORT2ADDR(port);
166 unsigned long b = (addr == 0 ? 0 : *(volatile unsigned short*)addr);
167 DIPRINTK(0, "inw(%08lx) = %04lx\n", addr, b);
168 return b;
169 }
170
bigsur_inl(unsigned long port)171 unsigned int bigsur_inl(unsigned long port)
172 {
173 unsigned long addr = PORT2ADDR(port);
174 unsigned int b = (addr == 0 ? 0 : *(volatile unsigned long*)addr);
175 DIPRINTK(0, "inl(%08lx) = %08x\n", addr, b);
176 return b;
177 }
178
bigsur_insb(unsigned long port,void * buffer,unsigned long count)179 void bigsur_insb(unsigned long port, void *buffer, unsigned long count)
180 {
181 unsigned char *buf=buffer;
182 while(count--) *buf++=inb(port);
183 }
184
bigsur_insw(unsigned long port,void * buffer,unsigned long count)185 void bigsur_insw(unsigned long port, void *buffer, unsigned long count)
186 {
187 unsigned short *buf=buffer;
188 while(count--) *buf++=inw(port);
189 }
190
bigsur_insl(unsigned long port,void * buffer,unsigned long count)191 void bigsur_insl(unsigned long port, void *buffer, unsigned long count)
192 {
193 unsigned long *buf=buffer;
194 while(count--) *buf++=inl(port);
195 }
196
bigsur_outb(unsigned char b,unsigned long port)197 void bigsur_outb(unsigned char b, unsigned long port)
198 {
199 unsigned long addr = PORT2ADDR(port);
200
201 DIPRINTK(0, "outb(%02x, %08lx)\n", (unsigned)b, addr);
202 if (addr != 0)
203 *(volatile unsigned char*)addr = b;
204 }
205
bigsur_outb_p(unsigned char b,unsigned long port)206 void bigsur_outb_p(unsigned char b, unsigned long port)
207 {
208 unsigned long addr = PORT2ADDR(port);
209
210 DIPRINTK(0, "outb_p(%02x, %08lx)\n", (unsigned)b, addr);
211 if (addr != 0)
212 *(volatile unsigned char*)addr = b;
213 delay();
214 }
215
bigsur_outw(unsigned short b,unsigned long port)216 void bigsur_outw(unsigned short b, unsigned long port)
217 {
218 unsigned long addr = PORT2ADDR(port);
219 DIPRINTK(0, "outw(%04x, %08lx)\n", (unsigned)b, addr);
220 if (addr != 0)
221 *(volatile unsigned short*)addr = b;
222 }
223
bigsur_outl(unsigned int b,unsigned long port)224 void bigsur_outl(unsigned int b, unsigned long port)
225 {
226 unsigned long addr = PORT2ADDR(port);
227 DIPRINTK(0, "outl(%08x, %08lx)\n", b, addr);
228 if (addr != 0)
229 *(volatile unsigned long*)addr = b;
230 }
231
bigsur_outsb(unsigned long port,const void * buffer,unsigned long count)232 void bigsur_outsb(unsigned long port, const void *buffer, unsigned long count)
233 {
234 const unsigned char *buf=buffer;
235 while(count--) outb(*buf++, port);
236 }
237
bigsur_outsw(unsigned long port,const void * buffer,unsigned long count)238 void bigsur_outsw(unsigned long port, const void *buffer, unsigned long count)
239 {
240 const unsigned short *buf=buffer;
241 while(count--) outw(*buf++, port);
242 }
243
bigsur_outsl(unsigned long port,const void * buffer,unsigned long count)244 void bigsur_outsl(unsigned long port, const void *buffer, unsigned long count)
245 {
246 const unsigned long *buf=buffer;
247 while(count--) outl(*buf++, port);
248 }
249
250