1 /* $NetBSD: cardbus_map.c,v 1.30 2009/03/15 15:45:01 cegger Exp $ */
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
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>
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
46 #if defined CARDBUS_MAP_DEBUG
48 #define DPRINTF(a) printf a
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.
71 cardbus_chipset_tag_t cc
,
72 cardbus_function_tag_t cf
,
80 cardbusreg_t address
, mask
;
83 /* EXT ROM is able to map on memory space ONLY. */
84 if (reg
== CARDBUS_ROM_REG
) {
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
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
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
);
109 if (PCI_MAPREG_TYPE(address
) != PCI_MAPREG_TYPE_IO
) {
110 printf("cardbus_io_find: expected type i/o, found mem\n");
114 if (PCI_MAPREG_IO_SIZE(mask
) == 0) {
115 printf("cardbus_io_find: void region\n");
120 *basep
= PCI_MAPREG_IO_ADDR(address
);
123 *sizep
= PCI_MAPREG_IO_SIZE(mask
);
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.
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
;
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
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
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
);
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");
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
));
184 if (PCI_MAPREG_MEM_SIZE(mask
) == 0) {
185 printf("cardbus_mem_find: void region\n");
189 switch (PCI_MAPREG_MEM_TYPE(address
)) {
190 case PCI_MAPREG_MEM_TYPE_32BIT
:
191 case PCI_MAPREG_MEM_TYPE_32BIT_1M
:
193 case PCI_MAPREG_MEM_TYPE_64BIT
:
194 printf("cardbus_mem_find: 64-bit memory mapping register\n");
197 printf("cardbus_mem_find: reserved mapping register type\n");
202 *basep
= PCI_MAPREG_MEM_ADDR(address
);
205 *sizep
= PCI_MAPREG_MEM_SIZE(mask
);
208 *flagsp
= PCI_MAPREG_MEM_PREFETCHABLE(address
) ?
209 BUS_SPACE_MAP_PREFETCHABLE
: 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
;
237 bus_space_handle_t handle
;
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
),
252 if (PCI_MAPREG_TYPE(type
) == PCI_MAPREG_TYPE_IO
) {
253 if (cardbus_io_find(cc
, cf
, tag
, reg
, type
, &base
, &size
, &flags
)) {
258 rbustag
= sc
->sc_rbus_iot
;
261 if (cardbus_mem_find(cc
, cf
, tag
, reg
, type
, &base
, &size
, &flags
)){
264 bustag
= sc
->sc_memt
;
266 rbustag
= sc
->sc_rbus_memt
;
271 bus_addr_t mask
= size
- 1;
275 if ((*cf
->cardbus_space_alloc
)(cc
, rbustag
, base
, size
, mask
,
276 size
, busflags
| flags
, &base
, &handle
)) {
280 bus_addr_t start
= 0x8300;
281 bus_addr_t end
= 0x8400;
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
)) {
291 cardbus_conf_write(cc
, cf
, tag
, reg
, base
);
293 DPRINTF(("cardbus_mapreg_map: physaddr %lx\n", (unsigned long)base
));
307 cardbus_free_tag(cc
, cf
, tag
);
317 * int cardbus_mapreg_unmap(struct cardbus_softc *sc, int func, int reg,
318 * bus_space_tag_t tag, bus_space_handle_t handle,
321 * This function releases bus-space region and close memory or io
322 * window on the bridge.
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
;
335 cardbustag_t cardbustag
;
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
;
352 cardbustag
= cardbus_make_tag(cc
, cf
, sc
->sc_bus
, func
);
354 cardbus_conf_write(cc
, cf
, cardbustag
, reg
, 0);
357 (*cf
->cardbus_space_free
)(cc
, rbustag
, handle
, size
);
360 cardbus_free_tag(cc
, cf
, cardbustag
);
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
);
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
);