Home | History | Annotate | Line # | Download | only in cardbus
cardbus_map.c revision 1.34
      1 /*	$NetBSD: cardbus_map.c,v 1.34 2010/02/26 00:57:01 dyoung 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  *
     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.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: cardbus_map.c,v 1.34 2010/02/26 00:57:01 dyoung Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 
     36 #include <sys/bus.h>
     37 
     38 #include <dev/cardbus/cardbusvar.h>
     39 
     40 #include <dev/pci/pcireg.h>	/* XXX */
     41 
     42 #if defined DEBUG && !defined CARDBUS_MAP_DEBUG
     43 #define CARDBUS_MAP_DEBUG
     44 #endif
     45 
     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
     53 
     54 
     55 static int cardbus_io_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
     56 				pcitag_t, int, pcireg_t,
     57 				bus_addr_t *, bus_size_t *, int *);
     58 static int cardbus_mem_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
     59 				 pcitag_t, int, pcireg_t,
     60 				 bus_addr_t *, bus_size_t *, int *);
     61 
     62 /*
     63  * static int cardbus_io_find(cardbus_chipset_tag_t cc,
     64  *			      cardbus_function_tag_t cf, pcitag_t tag,
     65  *			      int reg, pcireg_t type, bus_addr_t *basep,
     66  *			      bus_size_t *sizep, int *flagsp)
     67  * This code is stolen from sys/dev/pci_map.c.
     68  */
     69 static int
     70 cardbus_io_find(
     71     cardbus_chipset_tag_t cc,
     72     cardbus_function_tag_t cf,
     73     pcitag_t tag,
     74     int reg,
     75     pcireg_t type,
     76     bus_addr_t *basep,
     77     bus_size_t *sizep,
     78     int *flagsp)
     79 {
     80 	pcireg_t address, mask;
     81 	int s;
     82 
     83 	/* EXT ROM is able to map on memory space ONLY. */
     84 	if (reg == CARDBUS_ROM_REG) {
     85 		return 1;
     86 	}
     87 
     88 	if(reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3)) {
     89 		panic("cardbus_io_find: bad request");
     90 	}
     91 
     92 	/*
     93 	 * Section 6.2.5.1, `Address Maps', tells us that:
     94 	 *
     95 	 * 1) The builtin software should have already mapped the device in a
     96 	 * reasonable way.
     97 	 *
     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.
    101 	 */
    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);
    108 
    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;
    112 	}
    113 
    114 	if (PCI_MAPREG_IO_SIZE(mask) == 0) {
    115 		printf("cardbus_io_find: void region\n");
    116 		return 1;
    117 	}
    118 
    119 	if (basep != 0) {
    120 		*basep = PCI_MAPREG_IO_ADDR(address);
    121 	}
    122 	if (sizep != 0) {
    123 		*sizep = PCI_MAPREG_IO_SIZE(mask);
    124 	}
    125 	if (flagsp != 0) {
    126 		*flagsp = 0;
    127 	}
    128 
    129 	return 0;
    130 }
    131 
    132 
    133 
    134 /*
    135  * static int cardbus_mem_find(cardbus_chipset_tag_t cc,
    136  *			       cardbus_function_tag_t cf, pcitag_t tag,
    137  *			       int reg, pcireg_t type, bus_addr_t *basep,
    138  *			       bus_size_t *sizep, int *flagsp)
    139  * This code is stolen from sys/dev/pci_map.c.
    140  */
    141 static int
    142 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)
    143 {
    144 	pcireg_t address, mask;
    145 	int s;
    146 
    147 	if (reg != CARDBUS_ROM_REG &&
    148 	    (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))) {
    149 		panic("cardbus_mem_find: bad request");
    150 	}
    151 
    152 	/*
    153 	 * Section 6.2.5.1, `Address Maps', tells us that:
    154 	 *
    155 	 * 1) The builtin software should have already mapped the device in a
    156 	 * reasonable way.
    157 	 *
    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.
    161 	 */
    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);
    168 
    169 	if (reg != CARDBUS_ROM_REG) {
    170 		/* memory space BAR */
    171 
    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;
    175 		}
    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;
    181 		}
    182 	}
    183 
    184 	if (PCI_MAPREG_MEM_SIZE(mask) == 0) {
    185 		printf("cardbus_mem_find: void region\n");
    186 		return 1;
    187 	}
    188 
    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;
    199 	}
    200 
    201 	if (basep != 0) {
    202 		*basep = PCI_MAPREG_MEM_ADDR(address);
    203 	}
    204 	if (sizep != 0) {
    205 		*sizep = PCI_MAPREG_MEM_SIZE(mask);
    206 	}
    207 	if (flagsp != 0) {
    208 		*flagsp = PCI_MAPREG_MEM_PREFETCHABLE(address) ?
    209 		    BUS_SPACE_MAP_PREFETCHABLE : 0;
    210 	}
    211 
    212 	return 0;
    213 }
    214 
    215 
    216 
    217 
    218 /*
    219  * int cardbus_mapreg_map(struct cardbus_softc *, int, int, pcireg_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.
    227  */
    228 int
    229 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)
    230 {
    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 	pcitag_t tag;
    243 
    244 	size = 0;	/* XXX gcc */
    245 	flags = 0;	/* XXX gcc */
    246 
    247 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
    248 
    249 	DPRINTF(("cardbus_mapreg_map called: %s %x\n", device_xname(sc->sc_dev),
    250 	   type));
    251 
    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;
    255 		}
    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;
    263 		}
    264 		bustag = sc->sc_memt;
    265 #if rbus
    266 		rbustag = sc->sc_rbus_memt;
    267 #endif
    268 	}
    269 	if (status == 0) {
    270 #if rbus
    271 		bus_addr_t mask = size - 1;
    272 		if (base != 0) {
    273 			mask = 0xffffffff;
    274 		}
    275 		if ((*cf->cardbus_space_alloc)(cc, rbustag, base, size, mask,
    276 		    size, busflags | flags, &base, &handle)) {
    277 			panic("io alloc");
    278 		}
    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;
    285 		}
    286 		if (bus_space_alloc(bustag, start, end, size, size, 0, 0, &base, &handle)) {
    287 			panic("io alloc");
    288 		}
    289 #endif
    290 	}
    291 	cardbus_conf_write(cc, cf, tag, reg, base);
    292 
    293 	DPRINTF(("cardbus_mapreg_map: physaddr %lx\n", (unsigned long)base));
    294 
    295 	if (tagp != 0) {
    296 		*tagp = bustag;
    297 	}
    298 	if (handlep != 0) {
    299 		*handlep = handle;
    300 	}
    301 	if (basep != 0) {
    302 		*basep = base;
    303 	}
    304 	if (sizep != 0) {
    305 		*sizep = size;
    306 	}
    307 
    308 	return 0;
    309 }
    310 
    311 
    312 
    313 
    314 
    315 /*
    316  * int cardbus_mapreg_unmap(struct cardbus_softc *sc, int func, int reg,
    317  *			    bus_space_tag_t tag, bus_space_handle_t handle,
    318  *			    bus_size_t size)
    319  *
    320  *   This function releases bus-space region and close memory or io
    321  *   window on the bridge.
    322  *
    323  *  Arguments:
    324  *   struct cardbus_softc *sc; the pointer to the device structure of cardbus.
    325  *   int func; the number of function on the device.
    326  *   int reg; the offset of BAR register.
    327  */
    328 int
    329 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)
    330 {
    331 	cardbus_chipset_tag_t cc = sc->sc_cc;
    332 	cardbus_function_tag_t cf = sc->sc_cf;
    333 	int st = 1;
    334 	pcitag_t cardbustag;
    335 #if rbus
    336 	rbus_tag_t rbustag;
    337 
    338 	if (sc->sc_iot == tag) {
    339 		/* bus space is io space */
    340 		DPRINTF(("%s: unmap i/o space\n", device_xname(sc->sc_dev)));
    341 		rbustag = sc->sc_rbus_iot;
    342 	} else if (sc->sc_memt == tag) {
    343 		/* bus space is memory space */
    344 		DPRINTF(("%s: unmap mem space\n", device_xname(sc->sc_dev)));
    345 		rbustag = sc->sc_rbus_memt;
    346 	} else {
    347 		return 1;
    348 	}
    349 #endif
    350 
    351 	cardbustag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
    352 
    353 	cardbus_conf_write(cc, cf, cardbustag, reg, 0);
    354 
    355 #if rbus
    356 	(*cf->cardbus_space_free)(cc, rbustag, handle, size);
    357 #endif
    358 
    359 	return st;
    360 }
    361 
    362 
    363 
    364 
    365 
    366 /*
    367  * int cardbus_save_bar(cardbus_devfunc_t);
    368  *
    369  *   This function saves the Base Address Registers at the CardBus
    370  *   function denoted by the argument.
    371  */
    372 int cardbus_save_bar(cardbus_devfunc_t ct)
    373 {
    374 	pcitag_t tag = Cardbus_make_tag(ct);
    375 	cardbus_chipset_tag_t cc = ct->ct_cc;
    376 	cardbus_function_tag_t cf = ct->ct_cf;
    377 
    378 	ct->ct_bar[0] = cardbus_conf_read(cc, cf, tag, PCI_BAR0);
    379 	ct->ct_bar[1] = cardbus_conf_read(cc, cf, tag, PCI_BAR1);
    380 	ct->ct_bar[2] = cardbus_conf_read(cc, cf, tag, PCI_BAR2);
    381 	ct->ct_bar[3] = cardbus_conf_read(cc, cf, tag, PCI_BAR3);
    382 	ct->ct_bar[4] = cardbus_conf_read(cc, cf, tag, PCI_BAR4);
    383 	ct->ct_bar[5] = cardbus_conf_read(cc, cf, tag, PCI_BAR5);
    384 
    385 	DPRINTF(("cardbus_save_bar: %x %x\n", ct->ct_bar[0], ct->ct_bar[1]));
    386 
    387 	return 0;
    388 }
    389 
    390 
    391 
    392 /*
    393  * int cardbus_restore_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_restore_bar(cardbus_devfunc_t ct)
    399 {
    400 	pcitag_t tag = Cardbus_make_tag(ct);
    401 	cardbus_chipset_tag_t cc = ct->ct_cc;
    402 	cardbus_function_tag_t cf = ct->ct_cf;
    403 
    404 	cardbus_conf_write(cc, cf, tag, PCI_BAR0, ct->ct_bar[0]);
    405 	cardbus_conf_write(cc, cf, tag, PCI_BAR1, ct->ct_bar[1]);
    406 	cardbus_conf_write(cc, cf, tag, PCI_BAR2, ct->ct_bar[2]);
    407 	cardbus_conf_write(cc, cf, tag, PCI_BAR3, ct->ct_bar[3]);
    408 	cardbus_conf_write(cc, cf, tag, PCI_BAR4, ct->ct_bar[4]);
    409 	cardbus_conf_write(cc, cf, tag, PCI_BAR5, ct->ct_bar[5]);
    410 
    411 	return 0;
    412 }
    413