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