cardbus_map.c revision 1.22 1 /* $NetBSD: cardbus_map.c,v 1.22 2006/06/04 11:49:02 itohy Exp $ */
2
3 /*
4 * Copyright (c) 1999 and 2000
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 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: cardbus_map.c,v 1.22 2006/06/04 11:49:02 itohy Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41
42 #include <machine/bus.h>
43
44 #include <dev/cardbus/cardbusvar.h>
45
46 #include <dev/pci/pcireg.h> /* XXX */
47
48 #if defined DEBUG && !defined CARDBUS_MAP_DEBUG
49 #define CARDBUS_MAP_DEBUG
50 #endif
51
52 #if defined CARDBUS_MAP_DEBUG
53 #define STATIC
54 #define DPRINTF(a) printf a
55 #else
56 #define STATIC static
57 #define DPRINTF(a)
58 #endif
59
60
61 static int cardbus_io_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
62 cardbustag_t, int, cardbusreg_t,
63 bus_addr_t *, bus_size_t *, int *);
64 static int cardbus_mem_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
65 cardbustag_t, int, cardbusreg_t,
66 bus_addr_t *, bus_size_t *, int *);
67
68 /*
69 * static int cardbus_io_find(cardbus_chipset_tag_t cc,
70 * cardbus_function_tag_t cf, cardbustag_t tag,
71 * int reg, cardbusreg_t type, bus_addr_t *basep,
72 * bus_size_t *sizep, int *flagsp)
73 * This code is stolen from sys/dev/pci_map.c.
74 */
75 static int
76 cardbus_io_find(cc, cf, tag, reg, type, basep, sizep, flagsp)
77 cardbus_chipset_tag_t cc;
78 cardbus_function_tag_t cf;
79 cardbustag_t tag;
80 int reg;
81 cardbusreg_t type;
82 bus_addr_t *basep;
83 bus_size_t *sizep;
84 int *flagsp;
85 {
86 cardbusreg_t address, mask;
87 int s;
88
89 /* EXT ROM is able to map on memory space ONLY. */
90 if (reg == CARDBUS_ROM_REG) {
91 return 1;
92 }
93
94 if(reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3)) {
95 panic("cardbus_io_find: bad request");
96 }
97
98 /*
99 * Section 6.2.5.1, `Address Maps', tells us that:
100 *
101 * 1) The builtin software should have already mapped the device in a
102 * reasonable way.
103 *
104 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
105 * n bits of the address to 0. As recommended, we write all 1s and see
106 * what we get back.
107 */
108 s = splhigh();
109 address = cardbus_conf_read(cc, cf, tag, reg);
110 cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
111 mask = cardbus_conf_read(cc, cf, tag, reg);
112 cardbus_conf_write(cc, cf, tag, reg, address);
113 splx(s);
114
115 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
116 printf("cardbus_io_find: expected type i/o, found mem\n");
117 return 1;
118 }
119
120 if (PCI_MAPREG_IO_SIZE(mask) == 0) {
121 printf("cardbus_io_find: void region\n");
122 return 1;
123 }
124
125 if (basep != 0) {
126 *basep = PCI_MAPREG_IO_ADDR(address);
127 }
128 if (sizep != 0) {
129 *sizep = PCI_MAPREG_IO_SIZE(mask);
130 }
131 if (flagsp != 0) {
132 *flagsp = 0;
133 }
134
135 return 0;
136 }
137
138
139
140 /*
141 * static int cardbus_mem_find(cardbus_chipset_tag_t cc,
142 * cardbus_function_tag_t cf, cardbustag_t tag,
143 * int reg, cardbusreg_t type, bus_addr_t *basep,
144 * bus_size_t *sizep, int *flagsp)
145 * This code is stolen from sys/dev/pci_map.c.
146 */
147 static int
148 cardbus_mem_find(cc, cf, tag, reg, type, basep, sizep, flagsp)
149 cardbus_chipset_tag_t cc;
150 cardbus_function_tag_t cf;
151 cardbustag_t tag;
152 int reg;
153 cardbusreg_t type;
154 bus_addr_t *basep;
155 bus_size_t *sizep;
156 int *flagsp;
157 {
158 cardbusreg_t address, mask;
159 int s;
160
161 if (reg != CARDBUS_ROM_REG &&
162 (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))) {
163 panic("cardbus_mem_find: bad request");
164 }
165
166 /*
167 * Section 6.2.5.1, `Address Maps', tells us that:
168 *
169 * 1) The builtin software should have already mapped the device in a
170 * reasonable way.
171 *
172 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
173 * n bits of the address to 0. As recommended, we write all 1s and see
174 * what we get back.
175 */
176 s = splhigh();
177 address = cardbus_conf_read(cc, cf, tag, reg);
178 cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
179 mask = cardbus_conf_read(cc, cf, tag, reg);
180 cardbus_conf_write(cc, cf, tag, reg, address);
181 splx(s);
182
183 if (reg != CARDBUS_ROM_REG) {
184 /* memory space BAR */
185
186 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
187 printf("cardbus_mem_find: expected type mem, found i/o\n");
188 return 1;
189 }
190 if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
191 printf("cardbus_mem_find: expected mem type %08x, found %08x\n",
192 PCI_MAPREG_MEM_TYPE(type),
193 PCI_MAPREG_MEM_TYPE(address));
194 return 1;
195 }
196 }
197
198 if (PCI_MAPREG_MEM_SIZE(mask) == 0) {
199 printf("cardbus_mem_find: void region\n");
200 return 1;
201 }
202
203 switch (PCI_MAPREG_MEM_TYPE(address)) {
204 case PCI_MAPREG_MEM_TYPE_32BIT:
205 case PCI_MAPREG_MEM_TYPE_32BIT_1M:
206 break;
207 case PCI_MAPREG_MEM_TYPE_64BIT:
208 printf("cardbus_mem_find: 64-bit memory mapping register\n");
209 return 1;
210 default:
211 printf("cardbus_mem_find: reserved mapping register type\n");
212 return 1;
213 }
214
215 if (basep != 0) {
216 *basep = PCI_MAPREG_MEM_ADDR(address);
217 }
218 if (sizep != 0) {
219 *sizep = PCI_MAPREG_MEM_SIZE(mask);
220 }
221 if (flagsp != 0) {
222 *flagsp = PCI_MAPREG_MEM_PREFETCHABLE(address) ?
223 BUS_SPACE_MAP_PREFETCHABLE : 0;
224 }
225
226 return 0;
227 }
228
229
230
231
232 /*
233 * int cardbus_mapreg_map(struct cardbus_softc *, int, int, cardbusreg_t,
234 * int bus_space_tag_t *, bus_space_handle_t *,
235 * bus_addr_t *, bus_size_t *)
236 * This function maps bus-space on the value of Base Address
237 * Register (BAR) indexed by the argument `reg' (the second argument).
238 * When the value of the BAR is not valid, such as 0x00000000, a new
239 * address should be allocated for the BAR and new address values is
240 * written on the BAR.
241 */
242 int
243 cardbus_mapreg_map(sc, func, reg, type, busflags, tagp, handlep, basep, sizep)
244 struct cardbus_softc *sc;
245 int func, reg, busflags;
246 cardbusreg_t type;
247 bus_space_tag_t *tagp;
248 bus_space_handle_t *handlep;
249 bus_addr_t *basep;
250 bus_size_t *sizep;
251 {
252 cardbus_chipset_tag_t cc = sc->sc_cc;
253 cardbus_function_tag_t cf = sc->sc_cf;
254 bus_space_tag_t bustag;
255 #if rbus
256 rbus_tag_t rbustag;
257 #endif
258 bus_space_handle_t handle;
259 bus_addr_t base;
260 bus_size_t size;
261 int flags;
262 int status = 0;
263 cardbustag_t tag;
264
265 size = 0; /* XXX gcc */
266 flags = 0; /* XXX gcc */
267
268 tag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
269
270 DPRINTF(("cardbus_mapreg_map called: %s %x\n", sc->sc_dev.dv_xname,
271 type));
272
273 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
274 if (cardbus_io_find(cc, cf, tag, reg, type, &base, &size, &flags)) {
275 status = 1;
276 }
277 bustag = sc->sc_iot;
278 #if rbus
279 rbustag = sc->sc_rbus_iot;
280 #endif
281 } else {
282 if (cardbus_mem_find(cc, cf, tag, reg, type, &base, &size, &flags)){
283 status = 1;
284 }
285 bustag = sc->sc_memt;
286 #if rbus
287 rbustag = sc->sc_rbus_memt;
288 #endif
289 }
290 if (status == 0) {
291 #if rbus
292 bus_addr_t mask = size - 1;
293 if (base != 0) {
294 mask = 0xffffffff;
295 }
296 if ((*cf->cardbus_space_alloc)(cc, rbustag, base, size, mask,
297 size, busflags | flags, &base, &handle)) {
298 panic("io alloc");
299 }
300 #else
301 bus_addr_t start = 0x8300;
302 bus_addr_t end = 0x8400;
303 if (base != 0) {
304 bus_addr_t start = base;
305 bus_addr_t end = base + size;
306 }
307 if (bus_space_alloc(bustag, start, end, size, size, 0, 0, &base, &handle)) {
308 panic("io alloc");
309 }
310 #endif
311 }
312 cardbus_conf_write(cc, cf, tag, reg, base);
313
314 DPRINTF(("cardbus_mapreg_map: physaddr %lx\n", (unsigned long)base));
315
316 if (tagp != 0) {
317 *tagp = bustag;
318 }
319 if (handlep != 0) {
320 *handlep = handle;
321 }
322 if (basep != 0) {
323 *basep = base;
324 }
325 if (sizep != 0) {
326 *sizep = size;
327 }
328 cardbus_free_tag(cc, cf, tag);
329
330 return 0;
331 }
332
333
334
335
336
337 /*
338 * int cardbus_mapreg_unmap(struct cardbus_softc *sc, int func, int reg,
339 * bus_space_tag_t tag, bus_space_handle_t handle,
340 * bus_size_t size)
341 *
342 * This function releases bus-space region and close memory or io
343 * window on the bridge.
344 *
345 * Arguments:
346 * struct cardbus_softc *sc; the pointer to the device structure of cardbus.
347 * int func; the number of function on the device.
348 * int reg; the offset of BAR register.
349 */
350 int
351 cardbus_mapreg_unmap(sc, func, reg, tag, handle, size)
352 struct cardbus_softc *sc;
353 int func, reg;
354 bus_space_tag_t tag;
355 bus_space_handle_t handle;
356 bus_size_t size;
357 {
358 cardbus_chipset_tag_t cc = sc->sc_cc;
359 cardbus_function_tag_t cf = sc->sc_cf;
360 int st = 1;
361 cardbustag_t cardbustag;
362 #if rbus
363 rbus_tag_t rbustag;
364
365 if (sc->sc_iot == tag) {
366 /* bus space is io space */
367 DPRINTF(("%s: unmap i/o space\n", sc->sc_dev.dv_xname));
368 rbustag = sc->sc_rbus_iot;
369 } else if (sc->sc_memt == tag) {
370 /* bus space is memory space */
371 DPRINTF(("%s: unmap mem space\n", sc->sc_dev.dv_xname));
372 rbustag = sc->sc_rbus_memt;
373 } else {
374 return 1;
375 }
376 #endif
377
378 cardbustag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
379
380 cardbus_conf_write(cc, cf, cardbustag, reg, 0);
381
382 #if rbus
383 (*cf->cardbus_space_free)(cc, rbustag, handle, size);
384 #endif
385
386 cardbus_free_tag(cc, cf, cardbustag);
387
388 return st;
389 }
390
391
392
393
394
395 /*
396 * int cardbus_save_bar(cardbus_devfunc_t);
397 *
398 * This function saves the Base Address Registers at the CardBus
399 * function denoted by the argument.
400 */
401 int cardbus_save_bar(ct)
402 cardbus_devfunc_t ct;
403 {
404 cardbustag_t tag = Cardbus_make_tag(ct);
405 cardbus_chipset_tag_t cc = ct->ct_cc;
406 cardbus_function_tag_t cf = ct->ct_cf;
407
408 ct->ct_bar[0] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE0_REG);
409 ct->ct_bar[1] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE1_REG);
410 ct->ct_bar[2] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE2_REG);
411 ct->ct_bar[3] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE3_REG);
412 ct->ct_bar[4] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE4_REG);
413 ct->ct_bar[5] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE5_REG);
414
415 DPRINTF(("cardbus_save_bar: %x %x\n", ct->ct_bar[0], ct->ct_bar[1]));
416
417 Cardbus_free_tag(ct, tag);
418
419 return 0;
420 }
421
422
423
424 /*
425 * int cardbus_restore_bar(cardbus_devfunc_t);
426 *
427 * This function saves the Base Address Registers at the CardBus
428 * function denoted by the argument.
429 */
430 int cardbus_restore_bar(ct)
431 cardbus_devfunc_t ct;
432 {
433 cardbustag_t tag = Cardbus_make_tag(ct);
434 cardbus_chipset_tag_t cc = ct->ct_cc;
435 cardbus_function_tag_t cf = ct->ct_cf;
436
437 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, ct->ct_bar[0]);
438 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, ct->ct_bar[1]);
439 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, ct->ct_bar[2]);
440 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, ct->ct_bar[3]);
441 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, ct->ct_bar[4]);
442 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, ct->ct_bar[5]);
443
444 Cardbus_free_tag(ct, tag);
445
446 return 0;
447 }
448