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