cardbus_map.c revision 1.4 1 /* $NetBSD: cardbus_map.c,v 1.4 1999/10/27 10:04:41 haya Exp $ */
2
3 /*
4 * Copyright (c) 1999
5 * HAYAKAWA Koichi. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by HAYAKAWA Koichi.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35
36 /* #define CARDBUS_MAP_DEBUG 1 */
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42
43 #include <machine/bus.h>
44
45 #include <dev/cardbus/cardbusvar.h>
46
47 #include <dev/pci/pcireg.h> /* XXX */
48
49 #if defined DEBUG && !defined CARDBUS_MAP_DEBUG
50 #define CARDBUS_MAP_DEBUG
51 #endif
52
53 #if defined CARDBUS_MAP_DEBUG
54 #define STATIC
55 #define DPRINTF(a) printf a
56 #else
57 #define STATIC static
58 #define DPRINTF(a)
59 #endif
60
61
62 static int cardbus_io_find __P((cardbus_chipset_tag_t, cardbus_function_tag_t,
63 cardbustag_t, int, cardbusreg_t,
64 bus_addr_t *, bus_size_t *, int *));
65 static int cardbus_mem_find __P((cardbus_chipset_tag_t, cardbus_function_tag_t,
66 cardbustag_t, int, cardbusreg_t,
67 bus_addr_t *, bus_size_t *, int *));
68
69 /*
70 * static int cardbus_io_find(cardbus_chipset_tag_t cc,
71 * cardbus_function_tag_t cf, cardbustag_t tag,
72 * int reg, cardbusreg_t type, bus_addr_t *basep,
73 * bus_size_t *sizep, int *flagsp)
74 * This code is stallen from sys/dev/pci_map.c.
75 */
76 static int
77 cardbus_io_find(cc, cf, tag, reg, type, basep, sizep, flagsp)
78 cardbus_chipset_tag_t cc;
79 cardbus_function_tag_t cf;
80 cardbustag_t tag;
81 int reg;
82 cardbusreg_t type;
83 bus_addr_t *basep;
84 bus_size_t *sizep;
85 int *flagsp;
86 {
87 cardbusreg_t address, mask;
88 int s;
89
90 if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3)) {
91 panic("cardbus_io_find: bad request");
92 }
93
94 /*
95 * Section 6.2.5.1, `Address Maps', tells us that:
96 *
97 * 1) The builtin software should have already mapped the device in a
98 * reasonable way.
99 *
100 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
101 * n bits of the address to 0. As recommended, we write all 1s and see
102 * what we get back.
103 */
104 s = splhigh();
105 address = cardbus_conf_read(cc, cf, tag, reg);
106 cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
107 mask = cardbus_conf_read(cc, cf, tag, reg);
108 cardbus_conf_write(cc, cf, tag, reg, address);
109 splx(s);
110
111 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
112 printf("pci_io_find: expected type i/o, found mem\n");
113 return 1;
114 }
115
116 if (PCI_MAPREG_IO_SIZE(mask) == 0) {
117 printf("pci_io_find: void region\n");
118 return (1);
119 }
120
121 if (basep != 0) {
122 *basep = PCI_MAPREG_IO_ADDR(address);
123 }
124 if (sizep != 0) {
125 *sizep = PCI_MAPREG_IO_SIZE(mask);
126 }
127 if (flagsp != 0) {
128 *flagsp = 0;
129 }
130
131 return 0;
132 }
133
134
135
136 /*
137 * static int cardbus_mem_find(cardbus_chipset_tag_t cc,
138 * cardbus_function_tag_t cf, cardbustag_t tag,
139 * int reg, cardbusreg_t type, bus_addr_t *basep,
140 * bus_size_t *sizep, int *flagsp)
141 * This code is stallen from sys/dev/pci_map.c.
142 */
143 static int
144 cardbus_mem_find(cc, cf, tag, reg, type, basep, sizep, flagsp)
145 cardbus_chipset_tag_t cc;
146 cardbus_function_tag_t cf;
147 cardbustag_t tag;
148 int reg;
149 cardbusreg_t type;
150 bus_addr_t *basep;
151 bus_size_t *sizep;
152 int *flagsp;
153 {
154 cardbusreg_t address, mask;
155 int s;
156
157 if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3)) {
158 panic("cardbus_find_mem: bad request");
159 }
160
161 /*
162 * Section 6.2.5.1, `Address Maps', tells us that:
163 *
164 * 1) The builtin software should have already mapped the device in a
165 * reasonable way.
166 *
167 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
168 * n bits of the address to 0. As recommended, we write all 1s and see
169 * what we get back.
170 */
171 s = splhigh();
172 address = cardbus_conf_read(cc, cf, tag, reg);
173 cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
174 mask = cardbus_conf_read(cc, cf, tag, reg);
175 cardbus_conf_write(cc, cf, tag, reg, address);
176 splx(s);
177
178 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
179 printf("cardbus_mem_find: expected type mem, found i/o\n");
180 return 1;
181 }
182 if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
183 printf("cardbus_mem_find: expected mem type %08x, found %08x\n",
184 PCI_MAPREG_MEM_TYPE(type),
185 PCI_MAPREG_MEM_TYPE(address));
186 return 1;
187 }
188
189 if (PCI_MAPREG_MEM_SIZE(mask) == 0) {
190 printf("cardbus_mem_find: void region\n");
191 return 1;
192 }
193
194 switch (PCI_MAPREG_MEM_TYPE(address)) {
195 case PCI_MAPREG_MEM_TYPE_32BIT:
196 case PCI_MAPREG_MEM_TYPE_32BIT_1M:
197 break;
198 case PCI_MAPREG_MEM_TYPE_64BIT:
199 printf("pci_mem_find: 64-bit memory mapping register\n");
200 return 1;
201 default:
202 printf("cardbus_mem_find: reserved mapping register type\n");
203 return 1;
204 }
205
206 if (basep != 0) {
207 *basep = PCI_MAPREG_MEM_ADDR(address);
208 }
209 if (sizep != 0) {
210 *sizep = PCI_MAPREG_MEM_SIZE(mask);
211 }
212 if (flagsp != 0) {
213 *flagsp = PCI_MAPREG_MEM_CACHEABLE(address) ? BUS_SPACE_MAP_CACHEABLE : 0;
214 }
215
216 return 0;
217 }
218
219
220
221
222 /*
223 * int cardbus_mapreg_map(struct cardbus_softc *, int, int, cardbusreg_t,
224 * int bus_space_tag_t *, bus_space_handle_t *,
225 * bus_addr_t *, bus_size_t *)
226 * This function maps bus-space on the value of Base Address
227 * Register (BAR) indexed by the argument `reg' (the second argument).
228 * When the value of the BAR is not valid, such as 0x00000000, a new
229 * address should be allocated for the BAR and new address values is
230 * written on the BAR.
231 */
232 int
233 cardbus_mapreg_map(sc, func, reg, type, busflags, tagp, handlep, basep, sizep)
234 struct cardbus_softc *sc;
235 int func, reg, busflags;
236 cardbusreg_t type;
237 bus_space_tag_t *tagp;
238 bus_space_handle_t *handlep;
239 bus_addr_t *basep;
240 bus_size_t *sizep;
241 {
242 cardbus_chipset_tag_t cc = sc->sc_cc;
243 cardbus_function_tag_t cf = sc->sc_cf;
244 bus_space_tag_t bustag;
245 #if rbus
246 rbus_tag_t rbustag;
247 #endif
248 bus_space_handle_t handle;
249 bus_addr_t base;
250 bus_size_t size;
251 int flags;
252 int status = 0;
253
254 cardbustag_t tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
255
256 DPRINTF(("cardbus_mapreg_map called: %s %x\n", sc->sc_dev.dv_xname,
257 type));
258
259 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
260 if (cardbus_io_find(cc, cf, tag, reg, type, &base, &size, &flags)) {
261 status = 1;
262 }
263 bustag = sc->sc_iot;
264 #if rbus
265 rbustag = sc->sc_rbus_iot;
266 #endif
267 } else {
268 if (cardbus_mem_find(cc, cf, tag, reg, type, &base, &size, &flags)){
269 status = 1;
270 }
271 bustag = sc->sc_memt;
272 #if rbus
273 rbustag = sc->sc_rbus_memt;
274 #endif
275 }
276 if (status == 0) {
277 #if rbus
278 bus_addr_t mask = size - 1;
279 if (base != 0) {
280 mask = 0xffffffff;
281 }
282 if ((*cf->cardbus_space_alloc)(cc, rbustag, base, size, mask, size,
283 busflags | flags, &base, &handle)) {
284 panic("io alloc");
285 }
286 #else
287 bus_addr_t start = 0x8300;
288 bus_addr_t end = 0x8400;
289 if (base != 0) {
290 bus_addr_t start = base;
291 bus_addr_t end = base + size;
292 }
293 if (bus_space_alloc(bustag, start, end, size, size, 0, 0, &base, &handle)) {
294 panic("io alloc");
295 }
296 #endif
297 }
298 cardbus_conf_write(cc, cf, tag, reg, base);
299
300 DPRINTF(("cardbus_mapreg_map: physaddr %lx\n", base));
301
302 if (tagp != 0) {
303 *tagp = bustag;
304 }
305 if (handlep != 0) {
306 *handlep = handle;
307 }
308 if (basep != 0) {
309 *basep = base;
310 }
311 if (sizep != 0) {
312 *sizep = size;
313 }
314 cardbus_free_tag(cc, cf, tag);
315
316 return 0;
317 }
318
319
320
321
322
323
324 /*
325 * int cardbus_save_bar(cardbus_devfunc_t);
326 *
327 * This function saves the Base Address Registers at the CardBus
328 * function denoted by the argument.
329 */
330 int cardbus_save_bar(ct)
331 cardbus_devfunc_t ct;
332 {
333 cardbustag_t tag = Cardbus_make_tag(ct);
334 cardbus_chipset_tag_t cc = ct->ct_cc;
335 cardbus_function_tag_t cf = ct->ct_cf;
336
337 ct->ct_bar[0] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE0_REG);
338 ct->ct_bar[1] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE1_REG);
339 ct->ct_bar[2] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE2_REG);
340 ct->ct_bar[3] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE3_REG);
341 ct->ct_bar[4] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE4_REG);
342 ct->ct_bar[5] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE5_REG);
343
344 DPRINTF(("cardbus_save_bar: %x %x\n", ct->ct_bar[0], ct->ct_bar[1]));
345
346 Cardbus_free_tag(ct, tag);
347
348 return 0;
349 }
350
351
352
353 /*
354 * int cardbus_restore_bar(cardbus_devfunc_t);
355 *
356 * This function saves the Base Address Registers at the CardBus
357 * function denoted by the argument.
358 */
359 int cardbus_restore_bar(ct)
360 cardbus_devfunc_t ct;
361 {
362 cardbustag_t tag = Cardbus_make_tag(ct);
363 cardbus_chipset_tag_t cc = ct->ct_cc;
364 cardbus_function_tag_t cf = ct->ct_cf;
365
366 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, ct->ct_bar[0]);
367 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, ct->ct_bar[1]);
368 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, ct->ct_bar[2]);
369 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, ct->ct_bar[3]);
370 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, ct->ct_bar[4]);
371 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, ct->ct_bar[5]);
372
373 Cardbus_free_tag(ct, tag);
374 return 0;
375 }
376