Home | History | Annotate | Line # | Download | only in cardbus
      1  1.50   thorpej /*	$NetBSD: rbus_ppb.c,v 1.50 2022/09/25 17:33:19 thorpej Exp $	*/
      2   1.1       mcr 
      3   1.1       mcr /*
      4   1.1       mcr  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.1       mcr  * All rights reserved.
      6   1.1       mcr  *
      7   1.1       mcr  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1       mcr  * by Michael Richardson <mcr (at) sandelman.ottawa.on.ca>
      9   1.1       mcr  *
     10   1.1       mcr  * Redistribution and use in source and binary forms, with or without
     11   1.1       mcr  * modification, are permitted provided that the following conditions
     12   1.1       mcr  * are met:
     13   1.1       mcr  * 1. Redistributions of source code must retain the above copyright
     14   1.1       mcr  *    notice, this list of conditions and the following disclaimer.
     15   1.1       mcr  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       mcr  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       mcr  *    documentation and/or other materials provided with the distribution.
     18   1.1       mcr  *
     19   1.1       mcr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1       mcr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1       mcr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1       mcr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1       mcr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1       mcr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1       mcr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1       mcr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1       mcr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1       mcr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1       mcr  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       mcr  */
     31   1.1       mcr 
     32   1.1       mcr /*
     33   1.1       mcr  * CardBus front-end for the Intel/Digital DECchip 21152 PCI-PCI bridge
     34   1.1       mcr  */
     35   1.2     lukem 
     36   1.2     lukem #include <sys/cdefs.h>
     37  1.50   thorpej __KERNEL_RCSID(0, "$NetBSD: rbus_ppb.c,v 1.50 2022/09/25 17:33:19 thorpej Exp $");
     38   1.1       mcr 
     39   1.1       mcr #include <sys/param.h>
     40   1.1       mcr #include <sys/systm.h>
     41   1.1       mcr #include <sys/mbuf.h>
     42   1.1       mcr #include <sys/kernel.h>
     43   1.1       mcr #include <sys/socket.h>
     44   1.1       mcr #include <sys/ioctl.h>
     45   1.1       mcr #include <sys/errno.h>
     46   1.1       mcr #include <sys/device.h>
     47  1.38    dyoung #include <sys/kmem.h>
     48   1.1       mcr 
     49   1.1       mcr #include <machine/endian.h>
     50   1.1       mcr 
     51  1.20        ad #include <sys/bus.h>
     52  1.20        ad #include <sys/intr.h>
     53   1.1       mcr 
     54   1.1       mcr #include <dev/pci/pcivar.h>
     55   1.1       mcr #include <dev/pci/pcireg.h>
     56   1.1       mcr #include <dev/pci/pcidevs.h>
     57   1.1       mcr #include <dev/pci/ppbreg.h>
     58   1.1       mcr 
     59   1.1       mcr #include <dev/ic/i82365reg.h>
     60   1.1       mcr 
     61  1.41  drochner #include <dev/cardbus/rbus.h>
     62   1.1       mcr #include <dev/pci/pccbbreg.h>
     63   1.1       mcr #include <dev/pci/pccbbvar.h>
     64   1.1       mcr 
     65   1.1       mcr #include <dev/cardbus/cardbusvar.h>
     66  1.12   mycroft #include <dev/pci/pcidevs.h>
     67   1.1       mcr 
     68  1.23  jmcneill #include <x86/pci/pci_addr_fixup.h>
     69  1.23  jmcneill #include <x86/pci/pci_bus_fixup.h>
     70   1.1       mcr #include <i386/pci/pci_intr_fixup.h>
     71   1.1       mcr #include <i386/pci/pcibios.h>
     72   1.1       mcr 
     73   1.1       mcr struct ppb_softc;
     74   1.1       mcr 
     75  1.31    cegger static int  ppb_cardbus_match(device_t, cfdata_t, void *);
     76  1.31    cegger static void ppb_cardbus_attach(device_t, device_t, void *);
     77  1.31    cegger static int  ppb_activate(device_t, enum devact);
     78  1.16  drochner int rppbprint(void *, const char *);
     79  1.16  drochner int rbus_intr_fixup(pci_chipset_tag_t, int, int, int);
     80  1.16  drochner void rbus_do_header_fixup(pci_chipset_tag_t, pcitag_t, void *);
     81   1.1       mcr 
     82  1.16  drochner static void rbus_pci_phys_allocate(pci_chipset_tag_t, pcitag_t, void *);
     83  1.16  drochner 
     84  1.16  drochner static int rbus_do_phys_allocate(pci_chipset_tag_t, pcitag_t, int,
     85  1.16  drochner 				 void *, int, bus_addr_t *, bus_size_t);
     86  1.16  drochner 
     87  1.16  drochner static void rbus_pci_phys_countspace(pci_chipset_tag_t, pcitag_t, void *);
     88  1.16  drochner 
     89  1.16  drochner static int rbus_do_phys_countspace(pci_chipset_tag_t, pcitag_t, int,
     90  1.16  drochner 				   void *, int, bus_addr_t *, bus_size_t);
     91  1.16  drochner 
     92  1.16  drochner unsigned int rbus_round_up(unsigned int, unsigned int);
     93   1.1       mcr 
     94   1.1       mcr 
     95   1.1       mcr struct ppb_cardbus_softc {
     96  1.25     joerg   device_t sc_dev;
     97   1.3   thorpej   pcitag_t sc_tag;
     98   1.1       mcr   int foo;
     99   1.1       mcr };
    100   1.1       mcr 
    101  1.25     joerg CFATTACH_DECL_NEW(rbus_ppb, sizeof(struct ppb_cardbus_softc),
    102  1.39    dyoung     ppb_cardbus_match, ppb_cardbus_attach, NULL, ppb_activate);
    103   1.1       mcr 
    104   1.1       mcr #ifdef  CBB_DEBUG
    105   1.1       mcr int rbus_ppb_debug = 0;   /* hack with kdb */
    106   1.1       mcr #define DPRINTF(X) if(rbus_ppb_debug) printf X
    107   1.1       mcr #else
    108   1.1       mcr #define DPRINTF(X)
    109   1.1       mcr #endif
    110   1.1       mcr 
    111   1.1       mcr static int
    112  1.31    cegger ppb_cardbus_match(device_t parent, cfdata_t match, void *aux)
    113   1.1       mcr {
    114   1.1       mcr 	struct cardbus_attach_args *ca = aux;
    115   1.1       mcr 
    116  1.35    dyoung 	if (PCI_VENDOR(ca->ca_id) ==  PCI_VENDOR_DEC &&
    117  1.35    dyoung 	    PCI_PRODUCT(ca->ca_id) == PCI_PRODUCT_DEC_21152)
    118   1.1       mcr 		return (1);
    119   1.1       mcr 
    120   1.1       mcr 	if(PCI_CLASS(ca->ca_class) == PCI_CLASS_BRIDGE &&
    121   1.1       mcr 	   PCI_SUBCLASS(ca->ca_class) == PCI_SUBCLASS_BRIDGE_PCI) {
    122   1.1       mcr 	  /* XXX */
    123   1.1       mcr 	  printf("recognizing generic bridge chip\n");
    124   1.1       mcr 	}
    125   1.1       mcr 
    126   1.1       mcr 	return (0);
    127   1.1       mcr }
    128   1.1       mcr 
    129   1.1       mcr 
    130   1.1       mcr int
    131  1.27       dsl rppbprint(void *aux, const char *pnp)
    132   1.1       mcr {
    133   1.1       mcr 	struct pcibus_attach_args *pba = aux;
    134   1.1       mcr 
    135   1.1       mcr 	/* only PCIs can attach to PPBs; easy. */
    136   1.1       mcr 	if (pnp)
    137   1.9   thorpej 		aprint_normal("pci at %s", pnp);
    138   1.9   thorpej 	aprint_normal(" bus %d (rbus)", pba->pba_bus);
    139   1.1       mcr 	return (UNCONF);
    140   1.1       mcr }
    141   1.1       mcr 
    142   1.1       mcr int
    143   1.1       mcr rbus_intr_fixup(pci_chipset_tag_t pc,
    144   1.1       mcr 		int minbus,
    145   1.1       mcr 		int maxbus,
    146   1.1       mcr 		int line)
    147   1.1       mcr {
    148   1.1       mcr   pci_device_foreach_min(pc, minbus,
    149   1.1       mcr 			 maxbus, rbus_do_header_fixup, (void *)&line);
    150   1.1       mcr   return 0;
    151   1.1       mcr }
    152   1.1       mcr 
    153   1.1       mcr void
    154  1.27       dsl rbus_do_header_fixup(pci_chipset_tag_t pc, pcitag_t tag, void *context)
    155   1.1       mcr {
    156   1.1       mcr   int bus, device, function;
    157  1.43  uebayasi   pcireg_t intr;
    158   1.1       mcr   int *pline = (int *)context;
    159   1.1       mcr   int line = *pline;
    160   1.1       mcr 
    161   1.1       mcr   pci_decompose_tag(pc, tag, &bus, &device, &function);
    162   1.1       mcr 
    163   1.1       mcr   intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    164   1.1       mcr 
    165   1.1       mcr   intr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
    166   1.1       mcr   intr |= (line << PCI_INTERRUPT_LINE_SHIFT);
    167   1.1       mcr   pci_conf_write(pc, tag, PCI_INTERRUPT_REG, intr);
    168   1.1       mcr 
    169   1.1       mcr }
    170   1.1       mcr 
    171  1.15     perry /*
    172   1.1       mcr  * This function takes a range of PCI bus numbers and
    173   1.1       mcr  * allocates space for all devices found in this space (the BARs) from
    174   1.1       mcr  * the rbus space maps (I/O and memory).
    175   1.1       mcr  *
    176   1.1       mcr  * It assumes that "rbus" is defined. The whole concept does.
    177   1.1       mcr  *
    178   1.1       mcr  * It uses pci_device_foreach_min() to call rbus_pci_phys_allocate.
    179   1.1       mcr  * This function is mostly stolen from
    180  1.15     perry  *     pci_addr_fixup.c:pciaddr_resource_reserve.
    181   1.1       mcr  *
    182   1.1       mcr  */
    183   1.1       mcr struct rbus_pci_addr_fixup_context {
    184   1.1       mcr   struct ppb_cardbus_softc *csc;
    185   1.1       mcr   cardbus_chipset_tag_t ct;
    186   1.1       mcr   struct cardbus_softc *sc;
    187   1.1       mcr   struct cardbus_attach_args *caa;
    188   1.1       mcr   int    minbus;
    189   1.1       mcr   int    maxbus;
    190   1.1       mcr   bus_size_t  *bussize_ioreqs;
    191   1.1       mcr   bus_size_t  *bussize_memreqs;
    192   1.1       mcr   rbus_tag_t   *iobustags;
    193   1.1       mcr   rbus_tag_t   *membustags;
    194  1.15     perry };
    195   1.1       mcr 
    196  1.15     perry unsigned int
    197  1.16  drochner rbus_round_up(unsigned int size, unsigned int minval)
    198   1.1       mcr {
    199   1.1       mcr   unsigned int power2;
    200   1.1       mcr 
    201   1.1       mcr   if(size == 0) {
    202   1.1       mcr     return 0;
    203   1.1       mcr   }
    204   1.1       mcr 
    205  1.16  drochner   power2=minval;
    206   1.1       mcr 
    207   1.1       mcr   while(power2 < (1 << 31) &&
    208   1.1       mcr 	power2 < size) {
    209   1.1       mcr     power2 = power2 << 1;
    210   1.1       mcr   }
    211  1.15     perry 
    212   1.1       mcr   return power2;
    213   1.1       mcr }
    214  1.15     perry 
    215   1.1       mcr static void
    216   1.1       mcr rbus_pci_addr_fixup(struct ppb_cardbus_softc *csc,
    217   1.1       mcr 		    cardbus_chipset_tag_t ct,
    218   1.1       mcr 		    struct cardbus_softc *sc,
    219   1.1       mcr 		    pci_chipset_tag_t     pc,
    220   1.1       mcr 		    struct cardbus_attach_args *caa,
    221  1.38    dyoung 		    int minbus, const int maxbus)
    222   1.1       mcr {
    223   1.1       mcr 	struct rbus_pci_addr_fixup_context rct;
    224  1.38    dyoung 	const size_t size = sizeof(bus_size_t[maxbus+1]);
    225  1.38    dyoung 	int busnum;
    226   1.1       mcr 	bus_addr_t start;
    227   1.1       mcr 	bus_space_handle_t handle;
    228   1.1       mcr 	u_int32_t reg;
    229   1.1       mcr 
    230   1.1       mcr 	rct.csc=csc;
    231   1.1       mcr 	rct.ct=ct;
    232   1.1       mcr 	rct.sc=sc;
    233   1.1       mcr 	rct.caa=caa;
    234   1.1       mcr 	rct.minbus = minbus;
    235   1.1       mcr 	rct.maxbus = maxbus;
    236  1.45       chs 	rct.bussize_ioreqs = kmem_zalloc(size, KM_SLEEP);
    237  1.45       chs 	rct.bussize_memreqs = kmem_zalloc(size, KM_SLEEP);
    238  1.45       chs 	rct.iobustags = kmem_zalloc(maxbus * sizeof(rbus_tag_t), KM_SLEEP);
    239  1.45       chs 	rct.membustags = kmem_zalloc(maxbus * sizeof(rbus_tag_t), KM_SLEEP);
    240   1.1       mcr 
    241   1.1       mcr 	printf("%s: sizing buses %d-%d\n",
    242  1.25     joerg 	       device_xname(rct.csc->sc_dev),
    243   1.1       mcr 	       minbus, maxbus);
    244   1.1       mcr 
    245   1.1       mcr 	pci_device_foreach_min(pc, minbus, maxbus,
    246   1.1       mcr 			       rbus_pci_phys_countspace, &rct);
    247   1.1       mcr 
    248   1.1       mcr 	/*
    249   1.1       mcr 	 * we need to determine amount of address space for each
    250   1.1       mcr 	 * bus. To do this, we have to roll up amounts and then
    251   1.1       mcr 	 * we need to divide up the cardbus's extent to allocate
    252   1.1       mcr 	 * some space to each bus.
    253   1.1       mcr 	 */
    254   1.1       mcr 
    255   1.1       mcr 	for(busnum=maxbus; busnum > minbus; busnum--) {
    256   1.1       mcr 	  if(pci_bus_parent[busnum] != 0) {
    257   1.1       mcr 	    if(pci_bus_parent[busnum] < minbus ||
    258   1.1       mcr 	       pci_bus_parent[busnum] >= maxbus) {
    259   1.1       mcr 	      printf("%s: bus %d has illegal parent %d\n",
    260  1.25     joerg 		     device_xname(rct.csc->sc_dev),
    261   1.1       mcr 		     busnum, pci_bus_parent[busnum]);
    262   1.1       mcr 	      continue;
    263   1.1       mcr 	    }
    264   1.1       mcr 
    265   1.1       mcr 	    /* first round amount of space up */
    266   1.1       mcr 	    rct.bussize_ioreqs[busnum] =
    267  1.46   msaitoh 	      rbus_round_up(rct.bussize_ioreqs[busnum],  PCI_BRIDGE_IO_MIN);
    268   1.1       mcr 	    rct.bussize_ioreqs[pci_bus_parent[busnum]] +=
    269   1.1       mcr 	      rct.bussize_ioreqs[busnum];
    270   1.1       mcr 
    271   1.1       mcr 	    rct.bussize_memreqs[busnum] =
    272  1.46   msaitoh 	      rbus_round_up(rct.bussize_memreqs[busnum], PCI_BRIDGE_MEM_MIN);
    273   1.1       mcr 	    rct.bussize_memreqs[pci_bus_parent[busnum]] +=
    274   1.1       mcr 	      rct.bussize_memreqs[busnum];
    275   1.1       mcr 
    276   1.1       mcr 	  }
    277   1.1       mcr 	}
    278   1.1       mcr 
    279   1.1       mcr 	rct.bussize_ioreqs[minbus] =
    280  1.46   msaitoh 	  rbus_round_up(rct.bussize_ioreqs[minbus], PCI_BRIDGE_IO_MIN);
    281  1.46   msaitoh 	rct.bussize_memreqs[minbus] =  /* XXX Not 8 but PCI_BRIDGE_MEM_MIN ? */
    282   1.1       mcr 	  rbus_round_up(rct.bussize_memreqs[minbus], 8);
    283   1.1       mcr 
    284  1.38    dyoung 	printf("%s: total needs IO %08zx and MEM %08zx\n",
    285  1.25     joerg 	       device_xname(rct.csc->sc_dev),
    286   1.1       mcr 	       rct.bussize_ioreqs[minbus], rct.bussize_memreqs[minbus]);
    287   1.1       mcr 
    288   1.1       mcr 	if(!caa->ca_rbus_iot) {
    289   1.1       mcr 	  panic("no iot bus");
    290   1.1       mcr 	}
    291   1.1       mcr 
    292   1.1       mcr 	if(rct.bussize_ioreqs[minbus]) {
    293   1.1       mcr 	  if(rbus_space_alloc(caa->ca_rbus_iot, 0,
    294   1.1       mcr 			      rct.bussize_ioreqs[minbus],
    295   1.1       mcr 			      rct.bussize_ioreqs[minbus]-1 /* mask  */,
    296   1.1       mcr 			      rct.bussize_ioreqs[minbus] /* align */,
    297   1.1       mcr 			      /* flags */ 0,
    298   1.1       mcr 			      &start,
    299   1.1       mcr 			      &handle) != 0) {
    300  1.38    dyoung 	    panic("rbus_ppb: can not allocate %zu bytes in IO bus %d",
    301   1.1       mcr 		  rct.bussize_ioreqs[minbus], minbus);
    302   1.1       mcr 	  }
    303   1.1       mcr 	  rct.iobustags[minbus]=rbus_new(caa->ca_rbus_iot,
    304  1.15     perry 					 start,
    305   1.1       mcr 					 rct.bussize_ioreqs[minbus],
    306   1.1       mcr 					 0 /* offset to add to physical address
    307   1.1       mcr 					      to make processor address */,
    308   1.1       mcr 					 RBUS_SPACE_DEDICATE);
    309   1.1       mcr 	}
    310   1.1       mcr 
    311   1.1       mcr 	if(rct.bussize_memreqs[minbus]) {
    312   1.1       mcr 	  if(rbus_space_alloc(caa->ca_rbus_memt, 0,
    313   1.1       mcr 			      rct.bussize_memreqs[minbus],
    314   1.1       mcr 			      rct.bussize_memreqs[minbus]-1 /* mask */,
    315   1.1       mcr 			      rct.bussize_memreqs[minbus] /* align */,
    316   1.1       mcr 			      /* flags */ 0,
    317   1.1       mcr 			      &start,
    318   1.1       mcr 			      &handle) != 0) {
    319  1.38    dyoung 	    panic("%s: can not allocate %zu bytes in MEM bus %d",
    320  1.25     joerg 		  device_xname(rct.csc->sc_dev),
    321   1.1       mcr 		  rct.bussize_memreqs[minbus], minbus);
    322   1.1       mcr 	  }
    323   1.1       mcr 	  rct.membustags[minbus]=rbus_new(caa->ca_rbus_memt,
    324   1.1       mcr 					  start,
    325   1.1       mcr 					  rct.bussize_memreqs[minbus],
    326   1.1       mcr 					  0 /* offset to add to physical
    327   1.1       mcr 					       address to make processor
    328   1.1       mcr 					       address */,
    329   1.1       mcr 					  RBUS_SPACE_DEDICATE);
    330   1.1       mcr 	}
    331   1.1       mcr 
    332   1.1       mcr 	for(busnum=minbus+1; busnum <= maxbus; busnum++) {
    333   1.1       mcr 	  int busparent;
    334   1.1       mcr 
    335   1.1       mcr 	  busparent = pci_bus_parent[busnum];
    336   1.1       mcr 
    337  1.38    dyoung 	  printf("%s: bus %d (parent=%d) needs IO %08zx and MEM %08zx\n",
    338  1.25     joerg 		 device_xname(rct.csc->sc_dev),
    339   1.1       mcr 		 busnum,
    340   1.1       mcr 		 busparent,
    341   1.1       mcr 		 rct.bussize_ioreqs[busnum],
    342   1.1       mcr 		 rct.bussize_memreqs[busnum]);
    343   1.1       mcr 
    344   1.1       mcr 	  if(busparent > maxbus) {
    345   1.1       mcr 	    panic("rbus_ppb: illegal parent");
    346   1.1       mcr 	  }
    347   1.1       mcr 
    348   1.1       mcr 	  if(rct.bussize_ioreqs[busnum]) {
    349   1.1       mcr 	    if(rbus_space_alloc(rct.iobustags[busparent],
    350   1.1       mcr 				0,
    351   1.1       mcr 				rct.bussize_ioreqs[busnum],
    352   1.1       mcr 				rct.bussize_ioreqs[busnum]-1 /*mask */,
    353   1.1       mcr 				rct.bussize_ioreqs[busnum] /* align */,
    354   1.1       mcr 				/* flags */ 0,
    355   1.1       mcr 				&start,
    356   1.1       mcr 				&handle) != 0) {
    357  1.38    dyoung 	      panic("rbus_ppb: can not allocate %zu bytes in IO bus %d",
    358   1.1       mcr 		    rct.bussize_ioreqs[busnum], busnum);
    359   1.1       mcr 	    }
    360   1.1       mcr 	    rct.iobustags[busnum]=rbus_new(rct.iobustags[busparent],
    361   1.1       mcr 					   start,
    362   1.1       mcr 					   rct.bussize_ioreqs[busnum],
    363   1.1       mcr 					   0 /* offset to add to physical
    364   1.1       mcr 						address
    365   1.1       mcr 						to make processor address */,
    366   1.1       mcr 					   RBUS_SPACE_DEDICATE);
    367   1.1       mcr 
    368   1.1       mcr 	    /* program the bridge */
    369  1.15     perry 
    370   1.1       mcr 	    /* enable I/O space */
    371   1.1       mcr 	    reg = pci_conf_read(pc, pci_bus_tag[busnum],
    372   1.1       mcr 				PCI_COMMAND_STATUS_REG);
    373   1.1       mcr 	    reg |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    374   1.1       mcr 	    pci_conf_write(pc, pci_bus_tag[busnum],
    375   1.1       mcr 			   PCI_COMMAND_STATUS_REG, reg);
    376   1.1       mcr 
    377   1.1       mcr 	    /* now init the limit register for I/O */
    378  1.46   msaitoh 	    pci_conf_write(pc, pci_bus_tag[busnum], PCI_BRIDGE_STATIO_REG,
    379  1.46   msaitoh 		__SHIFTIN((start >> 8)
    380  1.46   msaitoh 		    & PCI_BRIDGE_STATIO_IOADDR, PCI_BRIDGE_STATIO_IOBASE) |
    381  1.46   msaitoh 		__SHIFTIN(((start + rct.bussize_ioreqs[busnum] + 4095) >> 8)
    382  1.46   msaitoh 		    & PCI_BRIDGE_STATIO_IOADDR, PCI_BRIDGE_STATIO_IOLIMIT));
    383   1.1       mcr 	  }
    384  1.15     perry 
    385   1.1       mcr 	  if(rct.bussize_memreqs[busnum]) {
    386   1.1       mcr 	    if(rbus_space_alloc(rct.membustags[busparent],
    387   1.1       mcr 				0,
    388  1.15     perry 				rct.bussize_memreqs[busnum] /* size  */,
    389  1.15     perry 				rct.bussize_memreqs[busnum]-1 /*mask */,
    390   1.1       mcr 				rct.bussize_memreqs[busnum] /* align */,
    391   1.1       mcr 				/* flags */ 0,
    392   1.1       mcr 				&start,
    393   1.1       mcr 				&handle) != 0) {
    394  1.38    dyoung 	      panic("rbus_ppb: can not allocate %zu bytes in MEM bus %d",
    395   1.1       mcr 		    rct.bussize_memreqs[busnum], busnum);
    396   1.1       mcr 	    }
    397   1.1       mcr 	    rct.membustags[busnum]=rbus_new(rct.membustags[busparent],
    398   1.1       mcr 					    start,
    399   1.1       mcr 					    rct.bussize_memreqs[busnum],
    400   1.1       mcr 					    0 /* offset to add to physical
    401   1.1       mcr 						 address to make processor
    402   1.1       mcr 						 address */,
    403   1.1       mcr 					    RBUS_SPACE_DEDICATE);
    404   1.1       mcr 
    405   1.1       mcr 	    /* program the bridge */
    406   1.1       mcr 	    /* enable memory space */
    407   1.1       mcr 	    reg = pci_conf_read(pc, pci_bus_tag[busnum],
    408   1.1       mcr 				PCI_COMMAND_STATUS_REG);
    409   1.1       mcr 	    reg |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    410   1.1       mcr 	    pci_conf_write(pc, pci_bus_tag[busnum],
    411   1.1       mcr 			   PCI_COMMAND_STATUS_REG, reg);
    412   1.1       mcr 
    413   1.1       mcr 	    /* now init the limit register for memory */
    414  1.46   msaitoh 	    pci_conf_write(pc, pci_bus_tag[busnum], PCI_BRIDGE_MEMORY_REG,
    415  1.46   msaitoh 		__SHIFTIN((start >> 16) & PCI_BRIDGE_MEMORY_ADDR,
    416  1.46   msaitoh 		    PCI_BRIDGE_MEMORY_BASE) |
    417  1.47   msaitoh 		__SHIFTIN(((start + rct.bussize_memreqs[busnum]
    418  1.47   msaitoh 			    + PCI_BRIDGE_MEM_MIN - 1) >> 16)
    419  1.47   msaitoh 		    & PCI_BRIDGE_MEMORY_ADDR, PCI_BRIDGE_MEMORY_LIMIT));
    420   1.1       mcr 
    421   1.1       mcr 	    /* and set the prefetchable limits as well */
    422  1.46   msaitoh 	    pci_conf_write(pc, pci_bus_tag[busnum], PCI_BRIDGE_PREFETCHMEM_REG,
    423  1.46   msaitoh 		__SHIFTIN((start >> 16) & PCI_BRIDGE_PREFETCHMEM_ADDR,
    424  1.46   msaitoh 		    PCI_BRIDGE_PREFETCHMEM_BASE) |
    425  1.47   msaitoh 		__SHIFTIN(((start + rct.bussize_memreqs[busnum]
    426  1.47   msaitoh 			    + PCI_BRIDGE_MEM_MIN - 1) >> 16)
    427  1.47   msaitoh 		    & PCI_BRIDGE_PREFETCHMEM_ADDR,
    428  1.46   msaitoh 		    PCI_BRIDGE_PREFETCHMEM_LIMIT));
    429   1.1       mcr 
    430   1.1       mcr 	    /* pci_conf_print(pc, pci_bus_tag[busnum], NULL); */
    431   1.1       mcr 	  }
    432   1.1       mcr 	}
    433   1.1       mcr 
    434   1.1       mcr 	printf("%s: configuring buses %d-%d\n",
    435  1.25     joerg 		device_xname(rct.csc->sc_dev),
    436   1.1       mcr 	       minbus, maxbus);
    437   1.1       mcr 	pci_device_foreach_min(pc, minbus, maxbus,
    438   1.1       mcr 			       rbus_pci_phys_allocate, &rct);
    439  1.38    dyoung 
    440  1.38    dyoung 	kmem_free(rct.bussize_ioreqs, size);
    441  1.38    dyoung 	kmem_free(rct.bussize_memreqs, size);
    442  1.38    dyoung 	kmem_free(rct.iobustags, maxbus * sizeof(rbus_tag_t));
    443  1.38    dyoung 	kmem_free(rct.membustags, maxbus * sizeof(rbus_tag_t));
    444   1.1       mcr }
    445   1.1       mcr 
    446   1.1       mcr static void
    447  1.27       dsl rbus_pci_phys_countspace(pci_chipset_tag_t pc, pcitag_t tag, void *context)
    448   1.1       mcr {
    449   1.1       mcr         int bus, device, function;
    450   1.1       mcr 	struct  rbus_pci_addr_fixup_context *rct =
    451   1.1       mcr 	  (struct  rbus_pci_addr_fixup_context *)context;
    452   1.1       mcr 
    453   1.1       mcr 	pci_decompose_tag(pc, tag, &bus, &device, &function);
    454   1.1       mcr 
    455   1.1       mcr 	printf("%s: configuring device %02x:%02x:%02x\n",
    456  1.25     joerg 	       device_xname(rct->csc->sc_dev),
    457   1.1       mcr 	       bus, device, function);
    458   1.1       mcr 
    459   1.1       mcr 	pciaddr_resource_manage(pc, tag,
    460   1.1       mcr 				rbus_do_phys_countspace, context);
    461   1.1       mcr }
    462   1.1       mcr 
    463  1.15     perry 
    464   1.1       mcr int
    465  1.28       dsl rbus_do_phys_countspace(pci_chipset_tag_t pc, pcitag_t tag, int mapreg, void *ctx, int type, bus_addr_t *addr, bus_size_t size)
    466   1.1       mcr {
    467   1.1       mcr 	struct  rbus_pci_addr_fixup_context *rct =
    468   1.1       mcr 	  (struct  rbus_pci_addr_fixup_context *)ctx;
    469   1.1       mcr 	int bus, device, function;
    470   1.1       mcr 
    471   1.1       mcr 	pci_decompose_tag(pc, tag, &bus, &device, &function);
    472   1.1       mcr 
    473   1.1       mcr 	if(size > (1<<24)) {
    474   1.1       mcr 	  printf("%s: skipping huge space request of size=%08x\n",
    475  1.25     joerg 		 device_xname(rct->csc->sc_dev), (unsigned int)size);
    476   1.1       mcr 	  return 0;
    477   1.1       mcr 	}
    478   1.1       mcr 
    479   1.1       mcr 	if(PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
    480   1.1       mcr 	  rct->bussize_ioreqs[bus] += size;
    481   1.1       mcr 	} else {
    482   1.1       mcr 	  rct->bussize_memreqs[bus]+= size;
    483   1.1       mcr 	}
    484  1.15     perry 
    485   1.1       mcr 	return 0;
    486   1.1       mcr }
    487   1.1       mcr 
    488   1.1       mcr static void
    489  1.27       dsl rbus_pci_phys_allocate(pci_chipset_tag_t pc, pcitag_t tag, void *context)
    490   1.1       mcr {
    491   1.1       mcr         int bus, device, function, command;
    492   1.1       mcr 	struct rbus_pci_addr_fixup_context *rct =
    493   1.1       mcr 	  (struct rbus_pci_addr_fixup_context *)context;
    494  1.15     perry 
    495   1.1       mcr 	pci_decompose_tag(pc, tag, &bus, &device, &function);
    496   1.1       mcr 
    497   1.1       mcr 	printf("%s: configuring device %02x:%02x:%02x\n",
    498  1.25     joerg 	       device_xname(rct->csc->sc_dev),
    499   1.1       mcr 	       bus, device, function);
    500   1.1       mcr 
    501   1.1       mcr 	pciaddr_resource_manage(pc, tag,
    502   1.1       mcr 				rbus_do_phys_allocate, context);
    503   1.1       mcr 
    504   1.1       mcr 	/* now turn the device's memory and I/O on */
    505   1.1       mcr 	command = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    506   1.1       mcr 	command |= PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE;
    507   1.1       mcr 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, command);
    508   1.1       mcr }
    509   1.1       mcr 
    510   1.1       mcr int
    511  1.28       dsl rbus_do_phys_allocate(pci_chipset_tag_t pc, pcitag_t tag, int mapreg, void *ctx, int type, bus_addr_t *addr, bus_size_t size)
    512   1.1       mcr {
    513   1.1       mcr 	struct  rbus_pci_addr_fixup_context *rct =
    514   1.1       mcr 	  (struct  rbus_pci_addr_fixup_context *)ctx;
    515   1.1       mcr 	cardbus_chipset_tag_t ct     = rct->ct;
    516   1.1       mcr 	struct cardbus_softc *sc     = rct->sc;
    517   1.1       mcr 	cardbus_function_t       *cf = sc->sc_cf;
    518   1.1       mcr 	rbus_tag_t          rbustag;
    519   1.1       mcr 	bus_addr_t mask = size -1;
    520   1.1       mcr 	bus_addr_t base = 0;
    521   1.1       mcr 	bus_space_handle_t handle;
    522   1.1       mcr 	int busflags = 0;
    523   1.1       mcr 	int flags    = 0;
    524  1.16  drochner 	const char *bustype;
    525   1.1       mcr 	int bus, device, function;
    526   1.1       mcr 
    527   1.1       mcr 	pci_decompose_tag(pc, tag, &bus, &device, &function);
    528   1.1       mcr 
    529   1.1       mcr 	/*
    530   1.1       mcr 	 * some devices come up with garbage in them (Tulip?)
    531   1.1       mcr 	 * we are in charge here, so give them address
    532  1.15     perry 	 * space anyway.
    533   1.1       mcr 	 *
    534   1.1       mcr 	 * XXX this may be due to no secondary PCI reset!!!
    535   1.1       mcr 	 */
    536   1.1       mcr #if 0
    537   1.1       mcr 	if (*addr) {
    538   1.1       mcr 		printf("Already allocated space at %08x\n",
    539   1.1       mcr 		       (unsigned int)*addr);
    540   1.1       mcr 		return (0);
    541   1.1       mcr 	}
    542   1.1       mcr #endif
    543   1.1       mcr 
    544   1.1       mcr 	if(size > (1<<24)) {
    545   1.1       mcr 	  printf("%s: skipping huge space request of size=%08x\n",
    546  1.25     joerg 		 device_xname(rct->csc->sc_dev), (unsigned int)size);
    547   1.1       mcr 	  return 0;
    548   1.1       mcr 	}
    549   1.1       mcr 
    550   1.1       mcr 	if(PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
    551   1.1       mcr 	  rbustag = rct->iobustags[bus];
    552   1.1       mcr 	  bustype = "io";
    553   1.1       mcr 	} else {
    554   1.1       mcr 	  rbustag = rct->membustags[bus];
    555   1.1       mcr 	  bustype = "mem";
    556   1.1       mcr 	}
    557   1.1       mcr 
    558   1.1       mcr 	if((*cf->cardbus_space_alloc)(ct, rbustag, base, size,
    559   1.1       mcr 				      mask, size, busflags|flags,
    560   1.1       mcr 				      addr, &handle)) {
    561   1.1       mcr 	  printf("%s: no available resources (size=%08x) for bar %2d. fixup failed\n",
    562  1.25     joerg 		 device_xname(rct->csc->sc_dev), (unsigned int)size, mapreg);
    563   1.1       mcr 
    564   1.1       mcr 	  *addr = 0;
    565   1.1       mcr 	  pci_conf_write(pc, tag, mapreg, *addr);
    566   1.1       mcr 	  return (1);
    567   1.1       mcr 	}
    568   1.1       mcr 
    569   1.1       mcr 	printf("%s: alloc %s space of size %08x for %02d:%02d:%02d -> %08x\n",
    570  1.25     joerg 	       device_xname(rct->csc->sc_dev),
    571  1.15     perry 	       bustype,
    572   1.1       mcr 	       (unsigned int)size,
    573   1.1       mcr 	       bus, device, function, (unsigned int)*addr);
    574   1.1       mcr 
    575   1.1       mcr 	/* write new address to PCI device configuration header */
    576   1.1       mcr 	pci_conf_write(pc, tag, mapreg, *addr);
    577   1.1       mcr 
    578   1.1       mcr 	/* check */
    579   1.1       mcr 	{
    580   1.1       mcr 		DPRINTF(("%s: pci_addr_fixup: ",
    581  1.25     joerg 			 device_xname(rct->csc->sc_dev)));
    582   1.1       mcr #ifdef  CBB_DEBUG
    583   1.1       mcr 		if(rbus_ppb_debug) { pciaddr_print_devid(pc, tag); }
    584   1.1       mcr #endif
    585   1.1       mcr 	}
    586   1.1       mcr 
    587   1.1       mcr 	/* double check that the value got inserted correctly */
    588   1.1       mcr 	if (pciaddr_ioaddr(pci_conf_read(pc, tag, mapreg)) != *addr) {
    589   1.1       mcr 		pci_conf_write(pc, tag, mapreg, 0); /* clear */
    590   1.1       mcr 		printf("%s: fixup failed. (new address=%#x)\n",
    591  1.25     joerg 		       device_xname(rct->csc->sc_dev),
    592   1.1       mcr 		       (unsigned)*addr);
    593   1.1       mcr 		return (1);
    594   1.1       mcr 	}
    595   1.1       mcr 
    596   1.1       mcr 	DPRINTF(("new address 0x%08x\n",
    597   1.1       mcr 		 (unsigned)*addr));
    598   1.1       mcr 
    599   1.1       mcr 	return (0);
    600   1.1       mcr }
    601   1.1       mcr 
    602   1.1       mcr static void
    603  1.25     joerg ppb_cardbus_attach(device_t parent, device_t self, void *aux)
    604   1.1       mcr {
    605  1.19   thorpej 	struct ppb_cardbus_softc *csc = device_private(self);
    606  1.24     joerg 	struct cardbus_softc *parent_sc = device_private(parent);
    607   1.1       mcr 	struct cardbus_attach_args *ca = aux;
    608   1.1       mcr 	cardbus_devfunc_t ct = ca->ca_ct;
    609   1.1       mcr 	cardbus_chipset_tag_t cc = ct->ct_cc;
    610   1.1       mcr 	struct pccbb_softc *psc = (struct pccbb_softc *)cc;
    611   1.1       mcr 	struct pcibus_attach_args pba;
    612   1.1       mcr 	char devinfo[256];
    613   1.1       mcr 	pcireg_t busdata;
    614   1.1       mcr 	int minbus, maxbus;
    615   1.1       mcr 
    616  1.25     joerg 	csc->sc_dev = self;
    617  1.25     joerg 
    618  1.11    itojun 	pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
    619   1.1       mcr 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(ca->ca_class));
    620   1.1       mcr 
    621  1.32    dyoung 	csc->sc_tag = ca->ca_tag;
    622   1.3   thorpej 
    623  1.46   msaitoh 	busdata = Cardbus_conf_read(ct, ca->ca_tag, PCI_BRIDGE_BUS_REG);
    624   1.1       mcr 	minbus = pcibios_max_bus;
    625  1.10     lukem 	maxbus = minbus;		/* XXX; gcc */
    626   1.1       mcr 
    627  1.46   msaitoh 	if (PCI_BRIDGE_BUS_NUM_SECONDARY(busdata) == 0) {
    628  1.21    cegger 		aprint_error_dev(self, "not configured by system firmware calling pci_bus_fixup(%d)\n", 0);
    629   1.1       mcr 
    630   1.1       mcr 	  /*
    631   1.1       mcr 	   * first, pull the reset wire on the secondary bridge
    632   1.1       mcr 	   * to clear all devices
    633   1.1       mcr 	   */
    634  1.46   msaitoh 	  busdata = Cardbus_conf_read(ct, ca->ca_tag, PCI_BRIDGE_CONTROL_REG);
    635  1.46   msaitoh 	  Cardbus_conf_write(ct, ca->ca_tag, PCI_BRIDGE_CONTROL_REG,
    636  1.46   msaitoh 	      busdata | PCI_BRIDGE_CONTROL_SECBR);
    637   1.1       mcr 	  delay(1);
    638  1.46   msaitoh 	  Cardbus_conf_write(ct, ca->ca_tag, PCI_BRIDGE_CONTROL_REG,
    639   1.1       mcr 			     busdata);
    640   1.1       mcr 
    641   1.1       mcr 	  /* then go initialize the bridge control registers */
    642   1.1       mcr 	  maxbus = pci_bus_fixup(psc->sc_pc, 0);
    643   1.1       mcr 	}
    644   1.1       mcr 
    645  1.46   msaitoh 	busdata = Cardbus_conf_read(ct, ca->ca_tag, PCI_BRIDGE_BUS_REG);
    646  1.46   msaitoh 	if(PCI_BRIDGE_BUS_NUM_SECONDARY(busdata) == 0) {
    647  1.21    cegger 		aprint_error_dev(self, "still not configured, not fixable.\n");
    648  1.21    cegger 		return;
    649   1.1       mcr 	}
    650   1.1       mcr 
    651  1.15     perry #if 0
    652  1.46   msaitoh 	minbus = PCI_BRIDGE_BUS_NUM_SECONDARY(busdata);
    653  1.46   msaitoh 	maxbus = PCI_BRIDGE_BUS_NUM_SUBORDINATE(busdata);
    654   1.1       mcr #endif
    655  1.15     perry 
    656   1.1       mcr 	/* now, go and assign addresses for the new devices */
    657   1.1       mcr 	rbus_pci_addr_fixup(csc, cc, parent_sc,
    658   1.1       mcr 			    psc->sc_pc,
    659   1.1       mcr 			    ca,
    660   1.1       mcr 			    minbus, maxbus);
    661   1.1       mcr 
    662   1.1       mcr 	/*
    663   1.1       mcr 	 * now configure all connected devices to the IRQ which
    664   1.1       mcr 	 * was assigned to this slot, as they will all arrive from
    665   1.1       mcr 	 * that IRQ.
    666   1.1       mcr 	 */
    667  1.41  drochner 	rbus_intr_fixup(psc->sc_pc, minbus, maxbus, 0);
    668   1.1       mcr 
    669  1.15     perry 	/*
    670   1.1       mcr 	 * enable direct routing of interrupts. We do this because
    671   1.1       mcr 	 * we can not manage to get pccb_intr_establish() called until
    672   1.1       mcr 	 * PCI subsystem is merged with rbus. The major thing that this
    673   1.1       mcr 	 * routine does is avoid calling the driver's interrupt routine
    674   1.1       mcr 	 * when the card has been removed.
    675   1.1       mcr 	 *
    676   1.1       mcr 	 * The rbus_ppb.c can not cope with card desertions until the merging
    677   1.1       mcr 	 * anyway.
    678   1.1       mcr 	 */
    679   1.1       mcr 	pccbb_intr_route(psc);
    680   1.1       mcr 
    681   1.1       mcr 	/*
    682   1.1       mcr 	 * Attach the PCI bus than hangs off of it.
    683   1.1       mcr 	 *
    684   1.1       mcr 	 * XXX Don't pass-through Memory Read Multiple.  Should we?
    685   1.1       mcr 	 * XXX Consult the spec...
    686  1.15     perry 	 */
    687   1.1       mcr 	pba.pba_iot  = ca->ca_iot;
    688   1.1       mcr 	pba.pba_memt = ca->ca_memt;
    689   1.1       mcr 	pba.pba_dmat = ca->ca_dmat;
    690   1.1       mcr 	pba.pba_pc   = psc->sc_pc;
    691  1.46   msaitoh 	pba.pba_flags    = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY;
    692  1.46   msaitoh 	pba.pba_bus      = PCI_BRIDGE_BUS_NUM_SECONDARY(busdata);
    693   1.4     lukem 	pba.pba_bridgetag = &csc->sc_tag;
    694   1.1       mcr 	/*pba.pba_intrswiz = parent_sc->sc_intrswiz; */
    695   1.1       mcr 	pba.pba_intrtag  = psc->sc_pa.pa_intrtag;
    696   1.1       mcr 
    697  1.49   thorpej 	config_found(self, &pba, rppbprint, CFARGS_NONE);
    698   1.1       mcr }
    699   1.1       mcr 
    700   1.1       mcr int
    701  1.31    cegger ppb_activate(device_t self, enum devact act)
    702   1.1       mcr {
    703   1.1       mcr   printf("ppb_activate called\n");
    704   1.1       mcr   return 0;
    705   1.1       mcr }
    706