Home | History | Annotate | Line # | Download | only in pci
      1  1.10   nonaka /*	$NetBSD: prep_pciconf_direct.c,v 1.10 2016/10/19 00:08:42 nonaka Exp $	*/
      2   1.1   kleink 
      3   1.1   kleink /*
      4   1.1   kleink  * Copyright (c) 2002 Klaus J. Klein.  All rights reserved.
      5   1.1   kleink  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      6   1.1   kleink  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      7   1.1   kleink  *
      8   1.1   kleink  * Redistribution and use in source and binary forms, with or without
      9   1.1   kleink  * modification, are permitted provided that the following conditions
     10   1.1   kleink  * are met:
     11   1.1   kleink  * 1. Redistributions of source code must retain the above copyright
     12   1.1   kleink  *    notice, this list of conditions and the following disclaimer.
     13   1.1   kleink  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1   kleink  *    notice, this list of conditions and the following disclaimer in the
     15   1.1   kleink  *    documentation and/or other materials provided with the distribution.
     16   1.1   kleink  * 3. All advertising materials mentioning features or use of this software
     17   1.1   kleink  *    must display the following acknowledgement:
     18   1.1   kleink  *	This product includes software developed by Charles M. Hannum.
     19   1.1   kleink  * 4. The name of the author may not be used to endorse or promote products
     20   1.1   kleink  *    derived from this software without specific prior written permission.
     21   1.1   kleink  *
     22   1.1   kleink  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1   kleink  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1   kleink  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1   kleink  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1   kleink  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1   kleink  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1   kleink  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1   kleink  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1   kleink  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1   kleink  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1   kleink  */
     33   1.1   kleink 
     34   1.1   kleink /*
     35   1.1   kleink  * Machine-specific functions for PCI autoconfiguration.
     36   1.1   kleink  *
     37   1.1   kleink  * This version is specific to the direct-mapped configuration space access
     38   1.1   kleink  * such as implemented on the IBM 27-82650 PCI Bridge/Memory Controller.
     39   1.1   kleink  */
     40   1.3    lukem 
     41   1.3    lukem #include <sys/cdefs.h>
     42  1.10   nonaka __KERNEL_RCSID(0, "$NetBSD: prep_pciconf_direct.c,v 1.10 2016/10/19 00:08:42 nonaka Exp $");
     43   1.1   kleink 
     44   1.1   kleink #include <sys/types.h>
     45   1.1   kleink #include <sys/param.h>
     46   1.1   kleink #include <sys/time.h>
     47   1.1   kleink #include <sys/systm.h>
     48   1.1   kleink #include <sys/errno.h>
     49   1.1   kleink #include <sys/device.h>
     50   1.1   kleink 
     51   1.1   kleink #include <uvm/uvm_extern.h>
     52   1.1   kleink 
     53   1.1   kleink #define _POWERPC_BUS_DMA_PRIVATE
     54   1.8   dyoung #include <sys/bus.h>
     55   1.1   kleink #include <machine/intr.h>
     56   1.1   kleink #include <machine/platform.h>
     57   1.2     matt 
     58   1.2     matt #include <powerpc/pio.h>
     59   1.1   kleink 
     60   1.1   kleink #include <dev/isa/isavar.h>
     61   1.1   kleink #include <dev/pci/pcivar.h>
     62   1.1   kleink #include <dev/pci/pcireg.h>
     63   1.1   kleink #include <dev/pci/pcidevs.h>
     64   1.1   kleink 
     65   1.5  garbled #include <prop/proplib.h>
     66   1.5  garbled 
     67   1.1   kleink #ifdef DEBUG
     68   1.6  garbled #define        DPRINTF(x) aprint_debug x
     69   1.1   kleink #else
     70   1.1   kleink #define        DPRINTF(x)
     71   1.1   kleink #endif
     72   1.1   kleink 
     73   1.1   kleink /* Physical base address and size of PCI configuration space. */
     74   1.1   kleink #define PCI_DCONF_BASE		0x80800000
     75   1.1   kleink #define	PCI_DCONF_SIZE		0x00800000
     76   1.1   kleink /* Device function selection. */
     77   1.1   kleink #define	PCI_DCONF_FUNC		0x00000700
     78   1.1   kleink #define	PCI_DCONF_FUNC_SHIFT	8
     79   1.1   kleink /* Configuration space register selection. */
     80   1.1   kleink #define	PCI_DCONF_REG		0x000000ff
     81   1.1   kleink #define	PCI_DCONF_REG_SHIFT	0
     82   1.1   kleink /* Device selection; those bits still available. */
     83   1.1   kleink #define	PCI_DCONF_DEV  \
     84   1.1   kleink     (~(~(PCI_DCONF_SIZE - 1) | PCI_DCONF_FUNC | PCI_DCONF_REG))
     85   1.1   kleink 
     86   1.7     matt void prep_pci_direct_attach_hook(device_t, device_t,
     87   1.1   kleink     struct pcibus_attach_args *);
     88   1.1   kleink pcitag_t prep_pci_direct_make_tag(void *, int, int, int);
     89   1.1   kleink pcireg_t prep_pci_direct_conf_read(void *, pcitag_t, int);
     90   1.1   kleink void prep_pci_direct_conf_write(void *, pcitag_t, int, pcireg_t);
     91   1.1   kleink void prep_pci_direct_decompose_tag(void *, pcitag_t, int *, int *, int *);
     92   1.1   kleink 
     93   1.1   kleink void
     94   1.1   kleink prep_pci_get_chipset_tag_direct(pci_chipset_tag_t pc)
     95   1.1   kleink {
     96   1.1   kleink 
     97   1.1   kleink 	pc->pc_conf_v = NULL;
     98   1.1   kleink 
     99   1.1   kleink 	pc->pc_attach_hook = prep_pci_direct_attach_hook;
    100   1.1   kleink 	pc->pc_bus_maxdevs = prep_pci_bus_maxdevs;
    101   1.1   kleink 	pc->pc_make_tag = prep_pci_direct_make_tag;
    102   1.1   kleink 	pc->pc_conf_read = prep_pci_direct_conf_read;
    103   1.1   kleink 	pc->pc_conf_write = prep_pci_direct_conf_write;
    104   1.1   kleink 
    105   1.1   kleink 	pc->pc_intr_v = NULL;
    106   1.1   kleink 
    107   1.1   kleink 	pc->pc_intr_map = prep_pci_intr_map;
    108   1.6  garbled 	pc->pc_intr_string = genppc_pci_intr_string;
    109   1.6  garbled 	pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
    110   1.6  garbled 	pc->pc_intr_establish = genppc_pci_intr_establish;
    111   1.6  garbled 	pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
    112  1.10   nonaka 	pc->pc_intr_setattr = genppc_pci_intr_setattr;
    113  1.10   nonaka 	pc->pc_intr_type = genppc_pci_intr_type;
    114  1.10   nonaka 	pc->pc_intr_alloc = genppc_pci_intr_alloc;
    115  1.10   nonaka 	pc->pc_intr_release = genppc_pci_intr_release;
    116  1.10   nonaka 	pc->pc_intx_alloc = genppc_pci_intx_alloc;
    117  1.10   nonaka 
    118  1.10   nonaka 	pc->pc_msi_v = NULL;
    119  1.10   nonaka 	genppc_pci_chipset_msi_init(pc);
    120  1.10   nonaka 
    121  1.10   nonaka 	pc->pc_msix_v = NULL;
    122  1.10   nonaka 	genppc_pci_chipset_msix_init(pc);
    123   1.1   kleink 
    124   1.6  garbled 	pc->pc_conf_interrupt = genppc_pci_conf_interrupt;
    125   1.1   kleink 	pc->pc_decompose_tag = prep_pci_direct_decompose_tag;
    126   1.1   kleink 	pc->pc_conf_hook = prep_pci_conf_hook;
    127   1.1   kleink }
    128   1.1   kleink 
    129   1.1   kleink void
    130   1.7     matt prep_pci_direct_attach_hook(device_t parent, device_t self,
    131   1.1   kleink     struct pcibus_attach_args *pba)
    132   1.1   kleink {
    133   1.1   kleink 
    134   1.1   kleink 	if (pba->pba_bus == 0)
    135   1.6  garbled 		aprint_normal(": direct-mapped configuration space access");
    136   1.1   kleink }
    137   1.1   kleink 
    138   1.1   kleink pcitag_t
    139   1.1   kleink prep_pci_direct_make_tag(void *v, int bus, int device, int function)
    140   1.1   kleink {
    141   1.1   kleink 	pcitag_t tag;
    142   1.1   kleink 
    143   1.1   kleink 	if (bus >= 256 || device >= 32 || function >= 8)
    144   1.1   kleink 		panic("prep_pci_direct_make_tag: bad request");
    145   1.1   kleink 
    146   1.1   kleink 	tag = (bus << 16) | (device << 11) | (function << 8);
    147   1.1   kleink 
    148   1.1   kleink 	return tag;
    149   1.1   kleink }
    150   1.1   kleink 
    151   1.1   kleink void
    152   1.1   kleink prep_pci_direct_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp,
    153   1.1   kleink     int *fp)
    154   1.1   kleink {
    155   1.1   kleink 
    156   1.1   kleink 	if (bp != NULL)
    157   1.1   kleink 		*bp = (tag >> 16) & 0xff;
    158   1.1   kleink 	if (dp != NULL)
    159   1.1   kleink 		*dp = (tag >> 11) & 0x1f;
    160   1.1   kleink 	if (fp != NULL)
    161   1.1   kleink 		*fp = (tag >> 8) & 0x7;
    162   1.1   kleink 	return;
    163   1.1   kleink }
    164   1.1   kleink 
    165   1.1   kleink pcireg_t
    166   1.1   kleink prep_pci_direct_conf_read(void *v, pcitag_t tag, int reg)
    167   1.1   kleink {
    168   1.1   kleink 	pcireg_t data;
    169   1.1   kleink 	int bus, device, function;
    170   1.1   kleink 	int s;
    171   1.1   kleink 
    172   1.9  msaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    173   1.9  msaitoh 		return (pcireg_t) ~0;
    174   1.9  msaitoh 
    175   1.1   kleink 	prep_pci_direct_decompose_tag(v, tag, &bus, &device, &function);
    176   1.1   kleink 
    177   1.1   kleink 	if (bus == 0) {
    178   1.1   kleink 		/* Check if device selector is within selector range. */
    179   1.1   kleink 		if (1 << device & ~PCI_DCONF_DEV) {
    180   1.1   kleink 			data = ~0;
    181   1.1   kleink 			goto out;
    182   1.1   kleink 		}
    183   1.1   kleink 		tag = (1 << device) | (function << PCI_DCONF_FUNC_SHIFT);
    184   1.1   kleink 	}
    185   1.1   kleink 
    186   1.1   kleink 	s = splhigh();
    187   1.1   kleink 	data = in32rb(PCI_DCONF_BASE | tag | reg);
    188   1.1   kleink 	splx(s);
    189   1.1   kleink 
    190   1.1   kleink out:
    191   1.1   kleink 	return data;
    192   1.1   kleink }
    193   1.1   kleink 
    194   1.1   kleink void
    195   1.1   kleink prep_pci_direct_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
    196   1.1   kleink {
    197   1.1   kleink 	int bus, device, function;
    198   1.1   kleink 	int s;
    199   1.1   kleink 
    200   1.1   kleink 	DPRINTF(("prep_pci_direct_conf_write(0x%lx, 0x%x, 0x%02x, 0x%08lx) ",
    201   1.1   kleink 	    (unsigned long)v, tag, reg, (unsigned long)data));
    202   1.1   kleink 
    203   1.9  msaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    204   1.9  msaitoh 		return;
    205   1.9  msaitoh 
    206   1.1   kleink 	prep_pci_direct_decompose_tag(v, tag, &bus, &device, &function);
    207   1.1   kleink 
    208   1.1   kleink 	if (bus == 0) {
    209   1.1   kleink 		/* Check if device selector is within selector range. */
    210   1.1   kleink 		if (1 << device & ~PCI_DCONF_DEV) {
    211   1.1   kleink 			/* Should never happen. */
    212   1.1   kleink 			panic("prep_pci_direct_conf_write: bad device %d",
    213   1.1   kleink 			    device);
    214   1.1   kleink 		}
    215   1.1   kleink 		tag = (1 << device) | (function << PCI_DCONF_FUNC_SHIFT);
    216   1.1   kleink 	}
    217   1.1   kleink 
    218   1.1   kleink 	s = splhigh();
    219   1.1   kleink 	out32rb(PCI_DCONF_BASE | tag | reg, data);
    220   1.1   kleink 	splx(s);
    221   1.1   kleink }
    222