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