Home | History | Annotate | Line # | Download | only in pci
mppb.c revision 1.1
      1 /*	$NetBSD: mppb.c,v 1.1 2011/09/17 16:55:34 rkujawa 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/extent.h>
     42 #include <sys/kmem.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <machine/bus.h>
     47 #include <machine/cpu.h>
     48 
     49 #include <m68k/bus_dma.h>
     50 #include <amiga/dev/zbusvar.h>
     51 #include <amiga/pci/mppbreg.h>
     52 
     53 #include <dev/pci/pcivar.h>
     54 #include <dev/pci/pcireg.h>
     55 #include <dev/pci/pcidevs.h>
     56 #include <dev/pci/pciconf.h>
     57 
     58 /* Zorro IDs */
     59 #define ZORRO_MANID_MATAY	44359
     60 #define ZORRO_PRODID_PROMETHEUS	1
     61 
     62 struct mppb_softc {
     63 	device_t sc_dev;
     64 	volatile char *ba;
     65 	struct bus_space_tag pci_conf_area;
     66 	struct bus_space_tag pci_io_area;
     67 	struct bus_space_tag pci_mem_area;
     68 	struct amiga_pci_chipset apc;
     69 };
     70 
     71 static int	mppb_match(struct device *, struct cfdata *, void *);
     72 static void	mppb_attach(struct device *, struct device *, void *);
     73 pcireg_t	mppb_pci_conf_read(pci_chipset_tag_t, pcitag_t, int);
     74 void		mppb_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
     75 int		mppb_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno);
     76 int		mppb_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev,
     77 		    int func, pcireg_t id);
     78 void		mppb_pci_attach_hook (struct device *parent,
     79 		    struct device *self, struct pcibus_attach_args *pba);
     80 pcitag_t	mppb_pci_make_tag(pci_chipset_tag_t pc, int bus, int device,
     81 		    int function);
     82 void		mppb_pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag,
     83 		    int *bp, int *dp, int *fp);
     84 
     85 void *		mppb_pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t
     86 		    ih, int level, int (*ih_fun)(void *), void *ih_arg);
     87 void		mppb_pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie);
     88 int		mppb_pci_intr_map(const struct pci_attach_args *pa,
     89 		    pci_intr_handle_t *ihp);
     90 const char *	mppb_pci_intr_string(pci_chipset_tag_t pc,
     91 		    pci_intr_handle_t ih);
     92 const struct evcnt * mppb_pci_intr_evcnt(pci_chipset_tag_t pc,
     93 		    pci_intr_handle_t ih);
     94 
     95 CFATTACH_DECL_NEW(mppb, sizeof(struct mppb_softc),
     96     mppb_match, mppb_attach, NULL, NULL);
     97 
     98 static int
     99 mppb_match(device_t parent, cfdata_t cf, void *aux)
    100 {
    101 	struct zbus_args *zap;
    102 
    103 	zap = aux;
    104 
    105 	if (zap->manid != ZORRO_MANID_MATAY)
    106 		return 0;
    107 
    108 	if (zap->prodid != ZORRO_PRODID_PROMETHEUS)
    109 		return 0;
    110 
    111 #ifdef MPPB_DEBUG
    112 	aprint_normal("mppb matched by Zorro ID %d, %d\n", zap->manid,
    113 	    zap->prodid);
    114 #endif
    115 
    116 	return 1;
    117 }
    118 
    119 
    120 static void
    121 mppb_attach(device_t parent, device_t self, void *aux)
    122 {
    123 	struct mppb_softc *sc;
    124 	struct pcibus_attach_args pba;
    125 	struct zbus_args *zap;
    126 	pci_chipset_tag_t pc;
    127 
    128 	zap = aux;
    129 	sc = device_private(self);
    130 	pc = &sc->apc;
    131 	sc->sc_dev = self;
    132 	sc->ba = zap->va;
    133 
    134 	aprint_normal(": Matay Prometheus PCI bridge\n");
    135 
    136 	/* Setup bus space mappings. */
    137 	sc->pci_conf_area.base = (bus_addr_t) sc->ba + MPPB_CONF_BASE;
    138 	sc->pci_conf_area.absm = &amiga_bus_stride_1swap;
    139 
    140 	sc->pci_mem_area.base = (bus_addr_t) sc->ba + MPPB_MEM_BASE;
    141 	sc->pci_mem_area.absm = &amiga_bus_stride_1;
    142 
    143 	sc->pci_io_area.base = (bus_addr_t) sc->ba + MPPB_IO_BASE;
    144 	sc->pci_io_area.absm = &amiga_bus_stride_1;
    145 
    146 #ifdef MPPB_DEBUG
    147 	aprint_normal("mppb mapped conf %x->%x, mem %x->%x\n, io %x->%x",
    148 	    (zap->pa) + MPPB_CONF_BASE, sc->pci_conf_area.base,
    149 	    (zap->pa) + MPPB_MEM_BASE, sc->pci_mem_area.base,
    150 	    (zap->pa) + MPPB_IO_BASE, sc->pci_io_area.base);
    151 #endif
    152 
    153 	sc->apc.pci_conf_iot = &(sc->pci_conf_area);
    154 
    155 	if (bus_space_map(sc->apc.pci_conf_iot, 0, MPPB_CONF_SIZE, 0,
    156 	    &sc->apc.pci_conf_ioh))
    157 		aprint_error_dev(self,
    158 		    "couldn't map PCI configuration data space\n");
    159 
    160 	/* Initialize the PCI chipset tag. */
    161 	sc->apc.pc_conf_v = (void*) pc;
    162 	sc->apc.pc_bus_maxdevs = mppb_pci_bus_maxdevs;
    163 	sc->apc.pc_make_tag = mppb_pci_make_tag;
    164 	sc->apc.pc_decompose_tag = mppb_pci_decompose_tag;
    165 	sc->apc.pc_conf_read = mppb_pci_conf_read;
    166 	sc->apc.pc_conf_write = mppb_pci_conf_write;
    167 	sc->apc.pc_attach_hook = mppb_pci_attach_hook;
    168 
    169 	sc->apc.pc_intr_map = mppb_pci_intr_map;
    170 	sc->apc.pc_intr_string = mppb_pci_intr_string;
    171 	sc->apc.pc_intr_establish = mppb_pci_intr_establish;
    172 	sc->apc.pc_intr_disestablish = mppb_pci_intr_disestablish;
    173 	/* XXX: pc_conf_interrupt */
    174 
    175 	pba.pba_iot = &(sc->pci_io_area);
    176 	pba.pba_memt = &(sc->pci_mem_area);
    177 	pba.pba_dmat = NULL;
    178 	pba.pba_dmat64 = NULL;
    179 	pba.pba_pc = pc;
    180 	pba.pba_flags = PCI_FLAGS_MEM_OKAY | PCI_FLAGS_IO_OKAY;
    181 	pba.pba_bus = 0;
    182 	pba.pba_bridgetag = NULL;
    183 
    184 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    185 }
    186 
    187 pcireg_t
    188 mppb_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    189 {
    190 	uint32_t data;
    191 	uint32_t bus, dev, func;
    192 
    193 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    194 
    195 	data = bus_space_read_4(pc->pci_conf_iot, pc->pci_conf_ioh,
    196 	    (MPPB_CONF_STRIDE*dev) + reg);
    197 #ifdef MPPB_DEBUG
    198 	aprint_normal("mppb conf read va: %lx, bus: %d, dev: %d, "
    199 	    "func: %d, reg: %d -r-> data %x\n",
    200 	    pc->pci_conf_ioh, bus, dev, func, reg, data);
    201 #endif
    202 	return data;
    203 }
    204 
    205 void
    206 mppb_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val)
    207 {
    208 	uint32_t bus, dev, func;
    209 
    210 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    211 
    212 	bus_space_write_4(pc->pci_conf_iot, pc->pci_conf_ioh,
    213 	    (MPPB_CONF_STRIDE*dev) + reg, val);
    214 #ifdef MPPB_DEBUG
    215 	aprint_normal("mppb conf write va: %lx, bus: %d, dev: %d, "
    216 	    "func: %d, reg: %d -w-> data %x\n",
    217 	    pc->pci_conf_ioh, bus, dev, func, reg, val);
    218 #endif
    219 
    220 }
    221 
    222 int
    223 mppb_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    224 {
    225 	return 4; /* Prometheus has 4 slots */
    226 }
    227 
    228 pcitag_t
    229 mppb_pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
    230 {
    231 	return (bus << 16) | (device << 11) | (function << 8);
    232 }
    233 
    234 void
    235 mppb_pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp,
    236     int *dp, int *fp)
    237 {
    238 	if (bp != NULL)
    239 		*bp = (tag >> 16) & 0xff;
    240 	if (dp != NULL)
    241 		*dp = (tag >> 11) & 0x1f;
    242 	if (fp != NULL)
    243 		*fp = (tag >> 8) & 0x07;
    244 }
    245 
    246 void
    247 mppb_pci_attach_hook(struct device *parent, struct device *self,
    248     struct pcibus_attach_args *pba)
    249 {
    250 }
    251 
    252 void *
    253 mppb_pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
    254    int (*ih_fun)(void *), void *ih_arg)
    255 {
    256 	struct isr* pci_isr;
    257 	pci_isr = kmem_zalloc(sizeof(struct isr), KM_SLEEP);
    258 
    259 	/* TODO: check for bogus handle */
    260 
    261 	pci_isr->isr_intr = ih_fun;
    262 	pci_isr->isr_arg = ih_arg;
    263 	pci_isr->isr_ipl = MPPB_INT;
    264 	add_isr(pci_isr);
    265 	return pci_isr;
    266 }
    267 
    268 void
    269 mppb_pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
    270 {
    271 	remove_isr(cookie);
    272 	kmem_free(cookie, sizeof(struct isr));
    273 }
    274 
    275 int
    276 mppb_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    277 {
    278 	/* TODO: add sanity checking */
    279 
    280 	*ihp = MPPB_INT;
    281 	return 0;
    282 }
    283 
    284 const char *
    285 mppb_pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih)
    286 {
    287 	static char str[10];
    288 
    289 	sprintf(str, "INT%d", (int) ih);
    290 	return str;
    291 }
    292 
    293 const struct evcnt *
    294 mppb_pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
    295 {
    296 	/* TODO: implement */
    297 	return NULL;
    298 }
    299 
    300