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