Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / cardbus / cardbus_map.c
blob376e068b54fa91fad82ee5770e2029f27ddef254
1 /* $NetBSD: cardbus_map.c,v 1.30 2009/03/15 15:45:01 cegger Exp $ */
3 /*
4 * Copyright (c) 1999 and 2000
5 * HAYAKAWA Koichi. All rights reserved.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: cardbus_map.c,v 1.30 2009/03/15 15:45:01 cegger Exp $");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
36 #include <sys/bus.h>
38 #include <dev/cardbus/cardbusvar.h>
40 #include <dev/pci/pcireg.h> /* XXX */
42 #if defined DEBUG && !defined CARDBUS_MAP_DEBUG
43 #define CARDBUS_MAP_DEBUG
44 #endif
46 #if defined CARDBUS_MAP_DEBUG
47 #define STATIC
48 #define DPRINTF(a) printf a
49 #else
50 #define STATIC static
51 #define DPRINTF(a)
52 #endif
55 static int cardbus_io_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
56 cardbustag_t, int, cardbusreg_t,
57 bus_addr_t *, bus_size_t *, int *);
58 static int cardbus_mem_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
59 cardbustag_t, int, cardbusreg_t,
60 bus_addr_t *, bus_size_t *, int *);
63 * static int cardbus_io_find(cardbus_chipset_tag_t cc,
64 * cardbus_function_tag_t cf, cardbustag_t tag,
65 * int reg, cardbusreg_t type, bus_addr_t *basep,
66 * bus_size_t *sizep, int *flagsp)
67 * This code is stolen from sys/dev/pci_map.c.
69 static int
70 cardbus_io_find(
71 cardbus_chipset_tag_t cc,
72 cardbus_function_tag_t cf,
73 cardbustag_t tag,
74 int reg,
75 cardbusreg_t type,
76 bus_addr_t *basep,
77 bus_size_t *sizep,
78 int *flagsp)
80 cardbusreg_t address, mask;
81 int s;
83 /* EXT ROM is able to map on memory space ONLY. */
84 if (reg == CARDBUS_ROM_REG) {
85 return 1;
88 if(reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3)) {
89 panic("cardbus_io_find: bad request");
93 * Section 6.2.5.1, `Address Maps', tells us that:
95 * 1) The builtin software should have already mapped the device in a
96 * reasonable way.
98 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
99 * n bits of the address to 0. As recommended, we write all 1s and see
100 * what we get back.
102 s = splhigh();
103 address = cardbus_conf_read(cc, cf, tag, reg);
104 cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
105 mask = cardbus_conf_read(cc, cf, tag, reg);
106 cardbus_conf_write(cc, cf, tag, reg, address);
107 splx(s);
109 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
110 printf("cardbus_io_find: expected type i/o, found mem\n");
111 return 1;
114 if (PCI_MAPREG_IO_SIZE(mask) == 0) {
115 printf("cardbus_io_find: void region\n");
116 return 1;
119 if (basep != 0) {
120 *basep = PCI_MAPREG_IO_ADDR(address);
122 if (sizep != 0) {
123 *sizep = PCI_MAPREG_IO_SIZE(mask);
125 if (flagsp != 0) {
126 *flagsp = 0;
129 return 0;
135 * static int cardbus_mem_find(cardbus_chipset_tag_t cc,
136 * cardbus_function_tag_t cf, cardbustag_t tag,
137 * int reg, cardbusreg_t type, bus_addr_t *basep,
138 * bus_size_t *sizep, int *flagsp)
139 * This code is stolen from sys/dev/pci_map.c.
141 static int
142 cardbus_mem_find(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf, cardbustag_t tag, int reg, cardbusreg_t type, bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
144 cardbusreg_t address, mask;
145 int s;
147 if (reg != CARDBUS_ROM_REG &&
148 (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))) {
149 panic("cardbus_mem_find: bad request");
153 * Section 6.2.5.1, `Address Maps', tells us that:
155 * 1) The builtin software should have already mapped the device in a
156 * reasonable way.
158 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
159 * n bits of the address to 0. As recommended, we write all 1s and see
160 * what we get back.
162 s = splhigh();
163 address = cardbus_conf_read(cc, cf, tag, reg);
164 cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
165 mask = cardbus_conf_read(cc, cf, tag, reg);
166 cardbus_conf_write(cc, cf, tag, reg, address);
167 splx(s);
169 if (reg != CARDBUS_ROM_REG) {
170 /* memory space BAR */
172 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
173 printf("cardbus_mem_find: expected type mem, found i/o\n");
174 return 1;
176 if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
177 printf("cardbus_mem_find: expected mem type %08x, found %08x\n",
178 PCI_MAPREG_MEM_TYPE(type),
179 PCI_MAPREG_MEM_TYPE(address));
180 return 1;
184 if (PCI_MAPREG_MEM_SIZE(mask) == 0) {
185 printf("cardbus_mem_find: void region\n");
186 return 1;
189 switch (PCI_MAPREG_MEM_TYPE(address)) {
190 case PCI_MAPREG_MEM_TYPE_32BIT:
191 case PCI_MAPREG_MEM_TYPE_32BIT_1M:
192 break;
193 case PCI_MAPREG_MEM_TYPE_64BIT:
194 printf("cardbus_mem_find: 64-bit memory mapping register\n");
195 return 1;
196 default:
197 printf("cardbus_mem_find: reserved mapping register type\n");
198 return 1;
201 if (basep != 0) {
202 *basep = PCI_MAPREG_MEM_ADDR(address);
204 if (sizep != 0) {
205 *sizep = PCI_MAPREG_MEM_SIZE(mask);
207 if (flagsp != 0) {
208 *flagsp = PCI_MAPREG_MEM_PREFETCHABLE(address) ?
209 BUS_SPACE_MAP_PREFETCHABLE : 0;
212 return 0;
219 * int cardbus_mapreg_map(struct cardbus_softc *, int, int, cardbusreg_t,
220 * int bus_space_tag_t *, bus_space_handle_t *,
221 * bus_addr_t *, bus_size_t *)
222 * This function maps bus-space on the value of Base Address
223 * Register (BAR) indexed by the argument `reg' (the second argument).
224 * When the value of the BAR is not valid, such as 0x00000000, a new
225 * address should be allocated for the BAR and new address values is
226 * written on the BAR.
229 cardbus_mapreg_map(struct cardbus_softc *sc, int func, int reg, cardbusreg_t type, int busflags, bus_space_tag_t *tagp, bus_space_handle_t *handlep, bus_addr_t *basep, bus_size_t *sizep)
231 cardbus_chipset_tag_t cc = sc->sc_cc;
232 cardbus_function_tag_t cf = sc->sc_cf;
233 bus_space_tag_t bustag;
234 #if rbus
235 rbus_tag_t rbustag;
236 #endif
237 bus_space_handle_t handle;
238 bus_addr_t base;
239 bus_size_t size;
240 int flags;
241 int status = 0;
242 cardbustag_t tag;
244 size = 0; /* XXX gcc */
245 flags = 0; /* XXX gcc */
247 tag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
249 DPRINTF(("cardbus_mapreg_map called: %s %x\n", device_xname(sc->sc_dev),
250 type));
252 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
253 if (cardbus_io_find(cc, cf, tag, reg, type, &base, &size, &flags)) {
254 status = 1;
256 bustag = sc->sc_iot;
257 #if rbus
258 rbustag = sc->sc_rbus_iot;
259 #endif
260 } else {
261 if (cardbus_mem_find(cc, cf, tag, reg, type, &base, &size, &flags)){
262 status = 1;
264 bustag = sc->sc_memt;
265 #if rbus
266 rbustag = sc->sc_rbus_memt;
267 #endif
269 if (status == 0) {
270 #if rbus
271 bus_addr_t mask = size - 1;
272 if (base != 0) {
273 mask = 0xffffffff;
275 if ((*cf->cardbus_space_alloc)(cc, rbustag, base, size, mask,
276 size, busflags | flags, &base, &handle)) {
277 panic("io alloc");
279 #else
280 bus_addr_t start = 0x8300;
281 bus_addr_t end = 0x8400;
282 if (base != 0) {
283 bus_addr_t start = base;
284 bus_addr_t end = base + size;
286 if (bus_space_alloc(bustag, start, end, size, size, 0, 0, &base, &handle)) {
287 panic("io alloc");
289 #endif
291 cardbus_conf_write(cc, cf, tag, reg, base);
293 DPRINTF(("cardbus_mapreg_map: physaddr %lx\n", (unsigned long)base));
295 if (tagp != 0) {
296 *tagp = bustag;
298 if (handlep != 0) {
299 *handlep = handle;
301 if (basep != 0) {
302 *basep = base;
304 if (sizep != 0) {
305 *sizep = size;
307 cardbus_free_tag(cc, cf, tag);
309 return 0;
317 * int cardbus_mapreg_unmap(struct cardbus_softc *sc, int func, int reg,
318 * bus_space_tag_t tag, bus_space_handle_t handle,
319 * bus_size_t size)
321 * This function releases bus-space region and close memory or io
322 * window on the bridge.
324 * Arguments:
325 * struct cardbus_softc *sc; the pointer to the device structure of cardbus.
326 * int func; the number of function on the device.
327 * int reg; the offset of BAR register.
330 cardbus_mapreg_unmap(struct cardbus_softc *sc, int func, int reg, bus_space_tag_t tag, bus_space_handle_t handle, bus_size_t size)
332 cardbus_chipset_tag_t cc = sc->sc_cc;
333 cardbus_function_tag_t cf = sc->sc_cf;
334 int st = 1;
335 cardbustag_t cardbustag;
336 #if rbus
337 rbus_tag_t rbustag;
339 if (sc->sc_iot == tag) {
340 /* bus space is io space */
341 DPRINTF(("%s: unmap i/o space\n", device_xname(sc->sc_dev)));
342 rbustag = sc->sc_rbus_iot;
343 } else if (sc->sc_memt == tag) {
344 /* bus space is memory space */
345 DPRINTF(("%s: unmap mem space\n", device_xname(sc->sc_dev)));
346 rbustag = sc->sc_rbus_memt;
347 } else {
348 return 1;
350 #endif
352 cardbustag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
354 cardbus_conf_write(cc, cf, cardbustag, reg, 0);
356 #if rbus
357 (*cf->cardbus_space_free)(cc, rbustag, handle, size);
358 #endif
360 cardbus_free_tag(cc, cf, cardbustag);
362 return st;
370 * int cardbus_save_bar(cardbus_devfunc_t);
372 * This function saves the Base Address Registers at the CardBus
373 * function denoted by the argument.
375 int cardbus_save_bar(cardbus_devfunc_t ct)
377 cardbustag_t tag = Cardbus_make_tag(ct);
378 cardbus_chipset_tag_t cc = ct->ct_cc;
379 cardbus_function_tag_t cf = ct->ct_cf;
381 ct->ct_bar[0] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE0_REG);
382 ct->ct_bar[1] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE1_REG);
383 ct->ct_bar[2] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE2_REG);
384 ct->ct_bar[3] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE3_REG);
385 ct->ct_bar[4] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE4_REG);
386 ct->ct_bar[5] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE5_REG);
388 DPRINTF(("cardbus_save_bar: %x %x\n", ct->ct_bar[0], ct->ct_bar[1]));
390 Cardbus_free_tag(ct, tag);
392 return 0;
398 * int cardbus_restore_bar(cardbus_devfunc_t);
400 * This function saves the Base Address Registers at the CardBus
401 * function denoted by the argument.
403 int cardbus_restore_bar(cardbus_devfunc_t ct)
405 cardbustag_t tag = Cardbus_make_tag(ct);
406 cardbus_chipset_tag_t cc = ct->ct_cc;
407 cardbus_function_tag_t cf = ct->ct_cf;
409 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, ct->ct_bar[0]);
410 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, ct->ct_bar[1]);
411 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, ct->ct_bar[2]);
412 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, ct->ct_bar[3]);
413 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, ct->ct_bar[4]);
414 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, ct->ct_bar[5]);
416 Cardbus_free_tag(ct, tag);
418 return 0;