Home | History | Annotate | Line # | Download | only in pci
mppb.c revision 1.10.4.1
      1 /*	$NetBSD: mppb.c,v 1.10.4.1 2021/03/23 07:14:42 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Radoslaw Kujawa.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /* Matay Prometheus Zorro-PCI bridge driver. */
     33 
     34 #include <sys/types.h>
     35 #include <sys/param.h>
     36 #include <sys/time.h>
     37 #include <sys/systm.h>
     38 #include <sys/errno.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/kmem.h>
     42 
     43 #include <uvm/uvm_extern.h>
     44 
     45 #include <machine/bus.h>
     46 #include <machine/cpu.h>
     47 
     48 #include <m68k/bus_dma.h>
     49 #include <amiga/dev/zbusvar.h>
     50 #include <amiga/pci/mppbreg.h>
     51 
     52 #include <dev/pci/pcivar.h>
     53 #include <dev/pci/pcireg.h>
     54 #include <dev/pci/pcidevs.h>
     55 #include <dev/pci/pciconf.h>
     56 
     57 #include "opt_pci.h"
     58 
     59 /* Zorro IDs */
     60 #define ZORRO_MANID_MATAY	44359
     61 #define ZORRO_PRODID_PROMETHEUS	1
     62 
     63 struct mppb_softc {
     64 	device_t sc_dev;
     65 	volatile char *ba;
     66 	struct bus_space_tag pci_conf_area;
     67 	struct bus_space_tag pci_io_area;
     68 	struct bus_space_tag pci_mem_area;
     69 	struct amiga_pci_chipset apc;
     70 };
     71 
     72 static int	mppb_match(device_t, cfdata_t, void *);
     73 static void	mppb_attach(device_t, device_t, void *);
     74 pcireg_t	mppb_pci_conf_read(pci_chipset_tag_t, pcitag_t, int);
     75 void		mppb_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
     76 int		mppb_pci_bus_maxdevs(pci_chipset_tag_t, int);
     77 void		mppb_pci_attach_hook (device_t, device_t,
     78 		    struct pcibus_attach_args *pba);
     79 pcitag_t	mppb_pci_make_tag(pci_chipset_tag_t, int, int, int);
     80 void		mppb_pci_decompose_tag(pci_chipset_tag_t, pcitag_t,
     81 		    int *, int *, int *);
     82 int		mppb_pci_intr_map(const struct pci_attach_args *,
     83 		    pci_intr_handle_t *);
     84 const struct evcnt * mppb_pci_intr_evcnt(pci_chipset_tag_t,
     85 		    pci_intr_handle_t);
     86 
     87 CFATTACH_DECL_NEW(mppb, sizeof(struct mppb_softc),
     88     mppb_match, mppb_attach, NULL, NULL);
     89 
     90 static int
     91 mppb_match(device_t parent, cfdata_t cf, void *aux)
     92 {
     93 	struct zbus_args *zap;
     94 
     95 	zap = aux;
     96 
     97 	if (zap->manid != ZORRO_MANID_MATAY)
     98 		return 0;
     99 
    100 	if (zap->prodid != ZORRO_PRODID_PROMETHEUS)
    101 		return 0;
    102 
    103 	return 1;
    104 }
    105 
    106 
    107 static void
    108 mppb_attach(device_t parent, device_t self, void *aux)
    109 {
    110 	struct mppb_softc *sc;
    111 	struct pcibus_attach_args pba;
    112 	struct zbus_args *zap;
    113 	pci_chipset_tag_t pc;
    114 
    115 	zap = aux;
    116 	sc = device_private(self);
    117 	pc = &sc->apc;
    118 	sc->sc_dev = self;
    119 	sc->ba = zap->va;
    120 
    121 	aprint_normal(": Matay Prometheus PCI bridge\n");
    122 
    123 	/* Setup bus space mappings. */
    124 	sc->pci_conf_area.base = (bus_addr_t) sc->ba + MPPB_CONF_BASE;
    125 	sc->pci_conf_area.absm = &amiga_bus_stride_1swap;
    126 
    127 	sc->pci_mem_area.base = (bus_addr_t) sc->ba + MPPB_MEM_BASE;
    128 	sc->pci_mem_area.absm = &amiga_bus_stride_1;
    129 
    130 	sc->pci_io_area.base = (bus_addr_t) sc->ba + MPPB_IO_BASE;
    131 	sc->pci_io_area.absm = &amiga_bus_stride_1;
    132 
    133 #ifdef MPPB_DEBUG
    134 	aprint_normal("mppb mapped conf %x->%x, mem %x->%x\n, io %x->%x\n",
    135 	    kvtop((void*) sc->pci_conf_area.base), sc->pci_conf_area.base,
    136 	    kvtop((void*) sc->pci_mem_area.base), sc->pci_mem_area.base,
    137 	    kvtop((void*) sc->pci_io_area.base), sc->pci_io_area.base);
    138 #endif
    139 
    140 	sc->apc.pci_conf_datat = &(sc->pci_conf_area);
    141 
    142 	if (bus_space_map(sc->apc.pci_conf_datat, 0, MPPB_CONF_SIZE, 0,
    143 	    &sc->apc.pci_conf_datah))
    144 		aprint_error_dev(self,
    145 		    "couldn't map PCI configuration data space\n");
    146 
    147 	/* Initialize the PCI chipset tag. */
    148 	sc->apc.pc_conf_v = (void*) pc;
    149 	sc->apc.pc_bus_maxdevs = mppb_pci_bus_maxdevs;
    150 	sc->apc.pc_make_tag = amiga_pci_make_tag;
    151 	sc->apc.pc_decompose_tag = amiga_pci_decompose_tag;
    152 	sc->apc.pc_conf_read = mppb_pci_conf_read;
    153 	sc->apc.pc_conf_write = mppb_pci_conf_write;
    154 	sc->apc.pc_attach_hook = mppb_pci_attach_hook;
    155 
    156 	sc->apc.pc_intr_map = mppb_pci_intr_map;
    157 	sc->apc.pc_intr_string = amiga_pci_intr_string;
    158 	sc->apc.pc_intr_establish = amiga_pci_intr_establish;
    159 	sc->apc.pc_intr_disestablish = amiga_pci_intr_disestablish;
    160 
    161 	sc->apc.pc_conf_hook = amiga_pci_conf_hook;
    162 	sc->apc.pc_conf_interrupt = amiga_pci_conf_interrupt;
    163 
    164 #ifdef PCI_NETBSD_CONFIGURE
    165 	struct pciconf_resources *pcires = pciconf_resource_init();
    166 
    167 	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
    168 	    MPPB_IO_BASE, MPPB_IO_SIZE);
    169 	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
    170 	    MPPB_MEM_BASE, MPPB_MEM_SIZE);
    171 
    172 #ifdef MPPB_DEBUG
    173 	aprint_normal("mppb: reconfiguring the bus!\n");
    174 #endif /* MPPB_DEBUG */
    175 	pci_configure_bus(pc, pcires, 0, CACHELINE_SIZE);
    176 
    177 	pciconf_resource_fini(pcires);
    178 #endif /* PCI_NETBSD_CONFIGURE */
    179 
    180 	pba.pba_iot = &(sc->pci_io_area);
    181 	pba.pba_memt = &(sc->pci_mem_area);
    182 	pba.pba_dmat = NULL;
    183 	pba.pba_dmat64 = NULL;
    184 	pba.pba_pc = pc;
    185 	pba.pba_flags = PCI_FLAGS_MEM_OKAY | PCI_FLAGS_IO_OKAY;
    186 	pba.pba_bus = 0;
    187 	pba.pba_bridgetag = NULL;
    188 
    189 	config_found(self, &pba, pcibusprint, CFARG_EOL);
    190 }
    191 
    192 pcireg_t
    193 mppb_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    194 {
    195 	uint32_t data;
    196 	uint32_t bus, dev, func;
    197 
    198 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    199 		return (pcireg_t) -1;
    200 
    201 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    202 
    203 	data = bus_space_read_4(pc->pci_conf_datat, pc->pci_conf_datah,
    204 	    (MPPB_CONF_STRIDE*dev) + reg);
    205 #ifdef MPPB_DEBUG_CONF
    206 	aprint_normal("mppb conf read va: %lx, bus: %d, dev: %d, "
    207 	    "func: %d, reg: %d -r-> data %x\n",
    208 	    pc->pci_conf_datah, bus, dev, func, reg, data);
    209 #endif /* MPPB_DEBUG_CONF */
    210 	return data;
    211 }
    212 
    213 void
    214 mppb_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val)
    215 {
    216 	uint32_t bus, dev, func;
    217 
    218 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    219 		return;
    220 
    221 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    222 
    223 	bus_space_write_4(pc->pci_conf_datat, pc->pci_conf_datah,
    224 	    (MPPB_CONF_STRIDE*dev) + reg, val);
    225 #ifdef MPPB_DEBUG_CONF
    226 	aprint_normal("mppb conf write va: %lx, bus: %d, dev: %d, "
    227 	    "func: %d, reg: %d -w-> data %x\n",
    228 	    pc->pci_conf_datah, bus, dev, func, reg, val);
    229 #endif /* MPPB_DEBUG_CONF */
    230 
    231 }
    232 
    233 int
    234 mppb_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    235 {
    236 	return 4; /* Prometheus has 4 slots */
    237 }
    238 
    239 void
    240 mppb_pci_attach_hook(device_t parent, device_t self,
    241     struct pcibus_attach_args *pba)
    242 {
    243 }
    244 
    245 int
    246 mppb_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    247 {
    248 	/* TODO: add sanity checking */
    249 
    250 	*ihp = MPPB_INT;
    251 	return 0;
    252 }
    253 
    254 const struct evcnt *
    255 mppb_pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
    256 {
    257 	/* TODO: implement */
    258 	return NULL;
    259 }
    260