#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: cardbus_map.c,v 1.36 2010/03/15 19:48:31 dyoung Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/bus.h>
#include <dev/cardbus/cardbusvar.h>
#include <dev/pci/pcireg.h>
#if defined DEBUG && !defined CARDBUS_MAP_DEBUG
#define CARDBUS_MAP_DEBUG
#endif
#if defined CARDBUS_MAP_DEBUG
#define STATIC
#define DPRINTF(a) printf a
#else
#define STATIC static
#define DPRINTF(a)
#endif
static int cardbus_io_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
pcitag_t, int, pcireg_t,
bus_addr_t *, bus_size_t *, int *);
static int cardbus_mem_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
pcitag_t, int, pcireg_t,
bus_addr_t *, bus_size_t *, int *);
static int
cardbus_io_find(
cardbus_chipset_tag_t cc,
cardbus_function_tag_t cf,
pcitag_t tag,
int reg,
pcireg_t type,
bus_addr_t *basep,
bus_size_t *sizep,
int *flagsp)
{
pcireg_t address, mask;
int s;
if (reg == CARDBUS_ROM_REG) {
return 1;
}
if(reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3)) {
panic("cardbus_io_find: bad request");
}
s = splhigh();
address = cardbus_conf_read(cc, cf, tag, reg);
cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
mask = cardbus_conf_read(cc, cf, tag, reg);
cardbus_conf_write(cc, cf, tag, reg, address);
splx(s);
if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
printf("cardbus_io_find: expected type i/o, found mem\n");
return 1;
}
if (PCI_MAPREG_IO_SIZE(mask) == 0) {
printf("cardbus_io_find: void region\n");
return 1;
}
if (basep != 0) {
*basep = PCI_MAPREG_IO_ADDR(address);
}
if (sizep != 0) {
*sizep = PCI_MAPREG_IO_SIZE(mask);
}
if (flagsp != 0) {
*flagsp = 0;
}
return 0;
}
static int
cardbus_mem_find(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf, pcitag_t tag, int reg, pcireg_t type, bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
{
pcireg_t address, mask;
int s;
if (reg != CARDBUS_ROM_REG &&
(reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))) {
panic("cardbus_mem_find: bad request");
}
s = splhigh();
address = cardbus_conf_read(cc, cf, tag, reg);
cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
mask = cardbus_conf_read(cc, cf, tag, reg);
cardbus_conf_write(cc, cf, tag, reg, address);
splx(s);
if (reg != CARDBUS_ROM_REG) {
if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
printf("cardbus_mem_find: expected type mem, found i/o\n");
return 1;
}
if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
printf("cardbus_mem_find: expected mem type %08x, found %08x\n",
PCI_MAPREG_MEM_TYPE(type),
PCI_MAPREG_MEM_TYPE(address));
return 1;
}
}
if (PCI_MAPREG_MEM_SIZE(mask) == 0) {
printf("cardbus_mem_find: void region\n");
return 1;
}
switch (PCI_MAPREG_MEM_TYPE(address)) {
case PCI_MAPREG_MEM_TYPE_32BIT:
case PCI_MAPREG_MEM_TYPE_32BIT_1M:
break;
case PCI_MAPREG_MEM_TYPE_64BIT:
printf("cardbus_mem_find: 64-bit memory mapping register\n");
return 1;
default:
printf("cardbus_mem_find: reserved mapping register type\n");
return 1;
}
if (basep != 0) {
*basep = PCI_MAPREG_MEM_ADDR(address);
}
if (sizep != 0) {
*sizep = PCI_MAPREG_MEM_SIZE(mask);
}
if (flagsp != 0) {
*flagsp = PCI_MAPREG_MEM_PREFETCHABLE(address) ?
BUS_SPACE_MAP_PREFETCHABLE : 0;
}
return 0;
}
int
cardbus_mapreg_map(struct cardbus_softc *sc, int func, int reg, pcireg_t type, int busflags, bus_space_tag_t *tagp, bus_space_handle_t *handlep, bus_addr_t *basep, bus_size_t *sizep)
{
cardbus_chipset_tag_t cc = sc->sc_cc;
cardbus_function_tag_t cf = sc->sc_cf;
bus_space_tag_t bustag;
rbus_tag_t rbustag;
bus_space_handle_t handle;
bus_addr_t base;
bus_size_t size;
int flags;
int status = 0;
pcitag_t tag;
size = 0;
flags = 0;
tag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
DPRINTF(("cardbus_mapreg_map called: %s %x\n", device_xname(sc->sc_dev),
type));
if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
if (cardbus_io_find(cc, cf, tag, reg, type, &base, &size, &flags)) {
status = 1;
}
bustag = sc->sc_iot;
rbustag = sc->sc_rbus_iot;
} else {
if (cardbus_mem_find(cc, cf, tag, reg, type, &base, &size, &flags)){
status = 1;
}
bustag = sc->sc_memt;
rbustag = sc->sc_rbus_memt;
}
if (status == 0) {
bus_addr_t mask = size - 1;
if (base != 0) {
mask = 0xffffffff;
}
if ((*cf->cardbus_space_alloc)(cc, rbustag, base, size, mask,
size, busflags | flags, &base, &handle)) {
panic("io alloc");
}
}
cardbus_conf_write(cc, cf, tag, reg, base);
DPRINTF(("cardbus_mapreg_map: physaddr %lx\n", (unsigned long)base));
if (tagp != 0) {
*tagp = bustag;
}
if (handlep != 0) {
*handlep = handle;
}
if (basep != 0) {
*basep = base;
}
if (sizep != 0) {
*sizep = size;
}
return 0;
}
int
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)
{
cardbus_chipset_tag_t cc = sc->sc_cc;
cardbus_function_tag_t cf = sc->sc_cf;
int st = 1;
pcitag_t cardbustag;
rbus_tag_t rbustag;
if (sc->sc_iot == tag) {
DPRINTF(("%s: unmap i/o space\n", device_xname(sc->sc_dev)));
rbustag = sc->sc_rbus_iot;
} else if (sc->sc_memt == tag) {
DPRINTF(("%s: unmap mem space\n", device_xname(sc->sc_dev)));
rbustag = sc->sc_rbus_memt;
} else {
return 1;
}
cardbustag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
cardbus_conf_write(cc, cf, cardbustag, reg, 0);
(*cf->cardbus_space_free)(cc, rbustag, handle, size);
return st;
}