Home | History | Annotate | Line # | Download | only in pci
sbbrz_pci.c revision 1.2
      1 /* $NetBSD: sbbrz_pci.c,v 1.2 2011/02/17 14:24:12 matt Exp $ */
      2 
      3 /*
      4  * Copyright 2000, 2001
      5  * Broadcom Corporation. All rights reserved.
      6  *
      7  * This software is furnished under license and may be used and copied only
      8  * in accordance with the following terms and conditions.  Subject to these
      9  * conditions, you may download, copy, install, use, modify and distribute
     10  * modified or unmodified copies of this software in source and/or binary
     11  * form. No title or ownership is transferred hereby.
     12  *
     13  * 1) Any source code used, modified or distributed must reproduce and
     14  *    retain this copyright notice and list of conditions as they appear in
     15  *    the source file.
     16  *
     17  * 2) No right is granted to use any trade name, trademark, or logo of
     18  *    Broadcom Corporation. Neither the "Broadcom Corporation" name nor any
     19  *    trademark or logo of Broadcom Corporation may be used to endorse or
     20  *    promote products derived from this software without the prior written
     21  *    permission of Broadcom Corporation.
     22  *
     23  * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
     24  *    WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
     25  *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
     26  *    NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
     27  *    FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
     28  *    LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     31  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     32  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     33  *    OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /* from: $NetBSD: apecs_pci.c,v 1.18 2000/06/29 08:58:45 mrg Exp */
     37 
     38 /*
     39  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
     40  * All rights reserved.
     41  *
     42  * Author: Chris G. Demetriou
     43  *
     44  * Permission to use, copy, modify and distribute this software and
     45  * its documentation is hereby granted, provided that both the copyright
     46  * notice and this permission notice appear in all copies of the
     47  * software, derivative works or modified versions, and any portions
     48  * thereof, and that both notices appear in supporting documentation.
     49  *
     50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     53  *
     54  * Carnegie Mellon requests users of this software to return to
     55  *
     56  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     57  *  School of Computer Science
     58  *  Carnegie Mellon University
     59  *  Pittsburgh PA 15213-3890
     60  *
     61  * any improvements or extensions that they make and grant Carnegie the
     62  * rights to redistribute these changes.
     63  */
     64 
     65 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     66 
     67 __KERNEL_RCSID(0, "$NetBSD: sbbrz_pci.c,v 1.2 2011/02/17 14:24:12 matt Exp $");
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/kernel.h>
     72 #include <sys/device.h>
     73 #include <uvm/uvm_extern.h>
     74 
     75 #include <dev/pci/pcireg.h>
     76 #include <dev/pci/pcivar.h>
     77 
     78 #include <machine/locore.h>
     79 #include <mips/sibyte/include/sb1250_regs.h>
     80 #include <mips/sibyte/include/sb1250_scd.h>
     81 #include <mips/sibyte/include/sb1250_int.h>
     82 #include <mips/sibyte/pci/sbbrzvar.h>
     83 
     84 void		sbbrz_pci_attach_hook(device_t, device_t,
     85 		    struct pcibus_attach_args *);
     86 static int	sbbrz_pci_bus_maxdevs(void *, int);
     87 static pcitag_t	sbbrz_pci_make_tag(void *, int, int, int);
     88 static void	sbbrz_pci_decompose_tag(void *, pcitag_t, int *, int *, int *);
     89 
     90 static pcireg_t	sbbrz_pci_conf_read(void *, pcitag_t, int);
     91 static void	sbbrz_pci_conf_write(void *, pcitag_t, int, pcireg_t);
     92 #ifdef PCI_NETBSD_CONFIGURE
     93 static void	sbbrz_pci_conf_interrupt(void *, int, int, int, int, int *);
     94 #endif
     95 
     96 static int	sbbrz_pci_intr_map(struct pci_attach_args *,
     97 		    pci_intr_handle_t *);
     98 static const char *
     99 		sbbrz_pci_intr_string(void *, pci_intr_handle_t);
    100 static const struct evcnt *
    101 		sbbrz_pci_intr_evcnt(void *, pci_intr_handle_t);
    102 static void *	sbbrz_pci_intr_establish(void *, pci_intr_handle_t,
    103 		    int, int (*)(void *), void *);
    104 static void	sbbrz_pci_intr_disestablish(void *, void *);
    105 
    106 
    107 void
    108 sbbrz_pci_init(pci_chipset_tag_t pc, void *v)
    109 {
    110 
    111 	pc->pc_conf_v = v;
    112 	pc->pc_attach_hook = sbbrz_pci_attach_hook;
    113 	pc->pc_bus_maxdevs = sbbrz_pci_bus_maxdevs;
    114 	pc->pc_make_tag = sbbrz_pci_make_tag;
    115 	pc->pc_decompose_tag = sbbrz_pci_decompose_tag;
    116 	pc->pc_conf_read = sbbrz_pci_conf_read;
    117 	pc->pc_conf_write = sbbrz_pci_conf_write;
    118 	pc->pc_intr_map = sbbrz_pci_intr_map;
    119 	pc->pc_intr_string = sbbrz_pci_intr_string;
    120 	pc->pc_intr_evcnt = sbbrz_pci_intr_evcnt;
    121 	pc->pc_intr_establish = sbbrz_pci_intr_establish;
    122 	pc->pc_intr_disestablish = sbbrz_pci_intr_disestablish;
    123 #ifdef PCI_NETBSD_CONFIGURE
    124 	pc->pc_conf_interrupt = sbbrz_pci_conf_interrupt;
    125 #endif
    126 #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    127 	pc->pc_pciide_compat_intr_establish = sbbrz_pciide_compat_intr_establish;
    128 #endif
    129 }
    130 
    131 void
    132 sbbrz_pci_attach_hook(device_t parent, device_t self,
    133     struct pcibus_attach_args *pba)
    134 {
    135 }
    136 
    137 int
    138 sbbrz_pci_bus_maxdevs(void *cpv, int busno)
    139 {
    140 	uint64_t regval;
    141 	int host;
    142 
    143 	/* If not the PCI bus directly off the 1250, always up to 32 devs.  */
    144 	if (busno != 0)
    145 		return 32;
    146 
    147 	/* If the PCI on the 1250, 32 devices if host mode, otherwise only 2. */
    148 	regval = mips3_ld((void *)MIPS_PHYS_TO_KSEG1(A_SCD_SYSTEM_CFG));
    149 	host = (regval & M_SYS_PCI_HOST) != 0;
    150 
    151 	return (host ? 32 : 2);
    152 }
    153 
    154 pcitag_t
    155 sbbrz_pci_make_tag(void *cpv, int b, int d, int f)
    156 {
    157 
    158 	return (b << 16) | (d << 11) | (f << 8);
    159 }
    160 
    161 void
    162 sbbrz_pci_decompose_tag(void *cpv, pcitag_t tag,
    163 	int *bp, int *dp, int *fp)
    164 {
    165 
    166 	if (bp != NULL)
    167 		*bp = (tag >> 16) & 0xff;
    168 	if (dp != NULL)
    169 		*dp = (tag >> 11) & 0x1f;
    170 	if (fp != NULL)
    171 		*fp = (tag >> 8) & 0x7;
    172 }
    173 
    174 pcireg_t
    175 sbbrz_pci_conf_read(void *cpv, pcitag_t tag, int offset)
    176 {
    177 	uint64_t addr;
    178 
    179 #ifdef DIAGNOSTIC
    180 	if ((offset & 0x3) != 0)
    181 		panic ("pci_conf_read: misaligned");
    182 #endif
    183 
    184 	addr = A_PHYS_LDTPCI_CFG_MATCH_BITS + tag + offset;
    185 	addr = MIPS_PHYS_TO_XKPHYS(MIPS3_TLB_ATTR_UNCACHED, addr);
    186 
    187 	__asm volatile("sync");
    188 
    189 	if (badaddr64(addr, 4) != 0)
    190 		return 0xffffffff;
    191 
    192 	return mips3_lw_a64(addr);
    193 }
    194 
    195 void
    196 sbbrz_pci_conf_write(void *cpv, pcitag_t tag, int offset, pcireg_t data)
    197 {
    198 	uint64_t addr;
    199 
    200 #ifdef DIAGNOSTIC
    201 	if ((offset & 0x3) != 0)
    202 		panic ("pci_conf_write: misaligned");
    203 #endif
    204 
    205 	addr = A_PHYS_LDTPCI_CFG_MATCH_BITS + tag + offset;
    206 	addr = MIPS_PHYS_TO_XKPHYS(MIPS3_TLB_ATTR_UNCACHED, addr);
    207 
    208 	return mips3_sw_a64(addr, data);
    209 }
    210 
    211 int
    212 sbbrz_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    213 {
    214 	int bus, device, func;
    215 	sbbrz_pci_decompose_tag(NULL, pa->pa_intrtag, &bus, &device, &func);
    216 	*ihp = 0;
    217 	if (pa->pa_intrpin == PCI_INTERRUPT_PIN_NONE)
    218 		return EINVAL;
    219 	if (bus == 0) {
    220 		*ihp = K_INT_PCI_INTA
    221 		    + (((device-5) + pa->pa_intrswiz + pa->pa_intrpin - PCI_INTERRUPT_PIN_A) % 4);
    222 		return 0;
    223 	}
    224 	return EOPNOTSUPP;
    225 }
    226 
    227 const char *
    228 sbbrz_pci_intr_string(void *v, pci_intr_handle_t ih)
    229 {
    230 	switch (ih) {
    231 	default:		return NULL;
    232 	case K_INT_PCI_INTA:	return "pci inta";
    233 	case K_INT_PCI_INTB:	return "pci intb";
    234 	case K_INT_PCI_INTC:	return "pci intc";
    235 	case K_INT_PCI_INTD:	return "pci intd";
    236 	}
    237 }
    238 
    239 const struct evcnt *
    240 sbbrz_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
    241 {
    242 	return NULL;
    243 }
    244 
    245 void *
    246 sbbrz_pci_intr_establish(void *v, pci_intr_handle_t ih, int level,
    247 	int (*handler)(void *), void *arg)
    248 {
    249 	return cpu_intr_establish(ih, level,
    250 	    (void (*)(void *, uint32_t, vaddr_t))handler, arg);
    251 }
    252 
    253 void
    254 sbbrz_pci_intr_disestablish(void *v, void *ih)
    255 {
    256 }
    257