Home | History | Annotate | Line # | Download | only in pci
empb.c revision 1.1
      1 /*	$NetBSD: empb.c,v 1.1 2012/05/30 18:01:51 rkujawa Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2012 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 /* Elbox Mediator PCI bridge driver. Currently supports Mediator 1200 models.*/
     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/empbreg.h>
     52 #include <amiga/pci/emmemvar.h>
     53 
     54 #include <dev/pci/pcivar.h>
     55 #include <dev/pci/pcireg.h>
     56 #include <dev/pci/pcidevs.h>
     57 #include <dev/pci/pciconf.h>
     58 
     59 #include "opt_pci.h"
     60 
     61 /* #define EMPB_DEBUG 1 */
     62 
     63 #define	PCI_CONF_LOCK(s)	(s) = splhigh()
     64 #define	PCI_CONF_UNLOCK(s)	splx((s))
     65 
     66 struct empb_softc {
     67 	device_t			sc_dev;
     68 
     69 	struct bus_space_tag		setup_area;
     70 	bus_space_tag_t			setup_area_t;
     71 	bus_space_handle_t		setup_area_h;
     72 
     73 	struct bus_space_tag		pci_confio_area;
     74 	bus_space_tag_t			pci_confio_t;
     75 	bus_space_handle_t		pci_confio_h;
     76 	uint8_t				pci_confio_mode;
     77 
     78 	struct bus_space_tag		pci_mem_window;
     79 	uint32_t			pci_mem_window_size;
     80 
     81 	struct amiga_pci_chipset	apc;
     82 
     83 };
     84 
     85 static int	empb_match(struct device *, struct cfdata *, void *);
     86 static void	empb_attach(struct device *, struct device *, void *);
     87 static void	empb_callback(device_t self);
     88 
     89 /*static bool	empb_find_mem(struct empb_softc *sc);*/
     90 void		empb_switch_bridge(struct empb_softc *sc, uint8_t mode);
     91 
     92 pcireg_t	empb_pci_conf_read(pci_chipset_tag_t, pcitag_t, int);
     93 void		empb_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
     94 int		empb_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno);
     95 void		empb_pci_attach_hook(struct device *parent,
     96 		    struct device *self, struct pcibus_attach_args *pba);
     97 pcitag_t	empb_pci_make_tag(pci_chipset_tag_t pc, int bus, int device,
     98 		    int function);
     99 void		empb_pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag,
    100 		    int *bp, int *dp, int *fp);
    101 int		empb_pci_intr_map(const struct pci_attach_args *pa,
    102 		    pci_intr_handle_t *ihp);
    103 const struct evcnt * empb_pci_intr_evcnt(pci_chipset_tag_t pc,
    104 		    pci_intr_handle_t ih);
    105 
    106 CFATTACH_DECL_NEW(empb, sizeof(struct empb_softc),
    107     empb_match, empb_attach, NULL, NULL);
    108 
    109 static int
    110 empb_match(device_t parent, cfdata_t cf, void *aux)
    111 {
    112 	struct zbus_args *zap;
    113 
    114 	zap = aux;
    115 
    116 	if (zap->manid != ZORRO_MANID_ELBOX)
    117 		return 0;
    118 
    119 	switch (zap->prodid) {
    120 	case ZORRO_PRODID_MED1K2:
    121 	case ZORRO_PRODID_MED1K2SX:
    122 	case ZORRO_PRODID_MED1K2LT2:
    123 	case ZORRO_PRODID_MED1K2LT4:
    124 	case ZORRO_PRODID_MED1K2TX:
    125 		return 1;
    126 	}
    127 
    128 	return 0;
    129 }
    130 
    131 
    132 static void
    133 empb_attach(device_t parent, device_t self, void *aux)
    134 {
    135 	struct empb_softc *sc;
    136 	struct zbus_args *zap;
    137 
    138 	volatile char *ba;
    139 
    140 	zap = aux;
    141 	sc = device_private(self);
    142 	sc->sc_dev = self;
    143 	ba = zap->va;
    144 
    145 	switch (zap->prodid) {
    146 	case ZORRO_PRODID_MED1K2:
    147 		aprint_normal(": ELBOX Mediator PCI 1200\n");
    148 		break;
    149 	case ZORRO_PRODID_MED1K2SX:
    150 		aprint_normal(": ELBOX Mediator PCI 1200 SX\n");
    151 		break;
    152 	case ZORRO_PRODID_MED1K2LT2:
    153 		aprint_normal(": ELBOX Mediator PCI 1200 LT2\n");
    154 		break;
    155 	case ZORRO_PRODID_MED1K2LT4:
    156 		aprint_normal(": ELBOX Mediator PCI 1200 LT4\n");
    157 		break;
    158 	case ZORRO_PRODID_MED1K2TX:
    159 		aprint_normal(": ELBOX Mediator PCI 1200 TX\n");
    160 		break;
    161 	default:
    162 		aprint_normal(": ELBOX Mediator PCI (unknown)\n");
    163 		break;
    164 	}
    165 
    166 	/* Setup bus space mappings. */
    167 	sc->pci_confio_area.base = (bus_addr_t) ba + EMPB_BRIDGE_OFF;
    168 	sc->pci_confio_area.absm = &amiga_bus_stride_1swap;
    169 
    170 	sc->setup_area.base = (bus_addr_t) ba + EMPB_SETUP_OFF;
    171 	sc->setup_area.absm = &amiga_bus_stride_1;
    172 
    173 	/*
    174 	 * Defer everything until later, we need to wait for possible
    175 	 * emmem attachments.
    176 	 */
    177 
    178 	config_defer(self, empb_callback);
    179 }
    180 
    181 static void
    182 empb_callback(device_t self) {
    183 
    184 	struct empb_softc *sc;
    185 	pci_chipset_tag_t pc;
    186 	struct pcibus_attach_args pba;
    187 
    188 	sc = device_private(self);
    189 	pc = &sc->apc;
    190 
    191 #ifdef EMPB_DEBUG
    192 	aprint_normal("empb: mapped setup %x->%x, conf/io %x->%x\n",
    193 	    kvtop((void*) sc->setup_area.base), sc->setup_area.base,
    194 	    kvtop((void*) sc->pci_confio_area.base), sc->pci_confio_area.base);
    195 #endif
    196 
    197 	sc->pci_confio_t = &(sc->pci_confio_area);
    198 
    199 	if (bus_space_map(sc->pci_confio_t, 0, EMPB_BRIDGE_SIZE, 0,
    200 	    &sc->pci_confio_h))
    201 		aprint_error_dev(self,
    202 		    "couldn't map PCI configuration & I/O space\n");
    203 
    204 	sc->apc.pci_conf_datat = sc->pci_confio_t;
    205 	sc->apc.pci_conf_datah = sc->pci_confio_h;
    206 
    207 	sc->setup_area_t = &(sc->setup_area);
    208 
    209 	if (bus_space_map(sc->setup_area_t, 0, EMPB_SETUP_SIZE, 0,
    210 	    &sc->setup_area_h))
    211 		aprint_error_dev(self,
    212 		    "couldn't map Mediator setup space\n");
    213 
    214 	/* Initialize the PCI chipset tag. */
    215 	sc->apc.pc_conf_v = (void*) pc;
    216 	sc->apc.pc_bus_maxdevs = empb_pci_bus_maxdevs;
    217 	sc->apc.pc_make_tag = amiga_pci_make_tag;
    218 	sc->apc.pc_decompose_tag = amiga_pci_decompose_tag;
    219 	sc->apc.pc_conf_read = empb_pci_conf_read;
    220 	sc->apc.pc_conf_write = empb_pci_conf_write;
    221 	sc->apc.pc_attach_hook = empb_pci_attach_hook;
    222 
    223 	sc->apc.pc_intr_map = empb_pci_intr_map;
    224 	sc->apc.pc_intr_string = amiga_pci_intr_string;
    225 	sc->apc.pc_intr_establish = amiga_pci_intr_establish;
    226 	sc->apc.pc_intr_disestablish = amiga_pci_intr_disestablish;
    227 
    228 	sc->apc.pc_conf_hook = amiga_pci_conf_hook;
    229 	sc->apc.pc_conf_interrupt = amiga_pci_conf_interrupt;
    230 
    231 	sc->apc.cookie = sc;
    232 
    233 	pba.pba_iot = &(sc->pci_confio_area);
    234 	pba.pba_dmat = NULL;
    235 	pba.pba_dmat64 = NULL;
    236 	pba.pba_pc = pc;
    237 	pba.pba_flags = PCI_FLAGS_IO_OKAY;
    238 
    239 	/*if(sc->pci_mem_window_size > 0) {
    240 		pba.pba_memt = &(sc->pci_mem_window);
    241 		pba.pba_flags |= PCI_FLAGS_MEM_OKAY;
    242 	} else */
    243 		pba.pba_memt = NULL;
    244 
    245 	pba.pba_bus = 0;
    246 	pba.pba_bridgetag = NULL;
    247 
    248 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    249 }
    250 
    251 /*
    252  * Switch between configuration space and I/O space.
    253  */
    254 void
    255 empb_switch_bridge(struct empb_softc *sc, uint8_t mode)
    256 {
    257 	bus_space_write_1(sc->setup_area_t, sc->setup_area_h,
    258 	    EMPB_SETUP_BRIDGE_OFF, mode);
    259 }
    260 
    261 
    262 /*
    263  * Try to find a (optional) memory window board.
    264  */
    265 /*bool
    266 empb_find_mem(struct empb_softc *sc)
    267 {
    268 	device_t memdev;
    269 	struct emmem_softc *mem_sc;
    270 
    271 	memdev = device_find_by_xname("emmem0");
    272 
    273 	if(memdev == NULL)
    274 		return false;
    275 
    276 	mem_sc = device_private(memdev);
    277 
    278 	sc->pci_mem_window.base = (bus_addr_t) mem_sc->sc_base;
    279 	sc->pci_mem_window.absm = &amiga_bus_stride_1;
    280 
    281 	sc->pci_mem_window_size = mem_sc->sc_size;
    282 }*/
    283 
    284 pcireg_t
    285 empb_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    286 {
    287 	uint32_t data;
    288 	uint32_t bus, dev, func;
    289 	struct empb_softc *sc;
    290 	int s;
    291 
    292 	sc = pc->cookie;
    293 
    294 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    295 
    296 	PCI_CONF_LOCK(s);
    297 
    298 	empb_switch_bridge(sc, BRIDGE_CONF);
    299 
    300 	data = bus_space_read_4(pc->pci_conf_datat, pc->pci_conf_datah,
    301 	    EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg);
    302 #ifdef EMPB_DEBUG_CONF
    303 	aprint_normal("empb conf read va: %lx, bus: %d, dev: %d, "
    304 	    "func: %d, reg: %d -r-> data %x\n",
    305 	    pc->pci_conf_datah, bus, dev, func, reg, data);
    306 #endif /* EMPB_DEBUG_CONF */
    307 
    308 	empb_switch_bridge(sc, BRIDGE_IO);
    309 
    310 	PCI_CONF_UNLOCK(s);
    311 
    312 	return data;
    313 }
    314 
    315 void
    316 empb_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val)
    317 {
    318 	uint32_t bus, dev, func;
    319 	struct empb_softc *sc;
    320 	int s;
    321 
    322 	sc = pc->cookie;
    323 
    324 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    325 
    326 	PCI_CONF_LOCK(s);
    327 
    328 	empb_switch_bridge(sc, BRIDGE_CONF);
    329 
    330 	bus_space_write_4(pc->pci_conf_datat, pc->pci_conf_datah,
    331 	    EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg, val);
    332 #ifdef EMPB_DEBUG_CONF
    333 	aprint_normal("empb conf write va: %lx, bus: %d, dev: %d, "
    334 	    "func: %d, reg: %d -w-> data %x\n",
    335 	    pc->pci_conf_datah, bus, dev, func, reg, val);
    336 #endif /* EMPB_DEBUG_CONF */
    337 	empb_switch_bridge(sc, BRIDGE_IO);
    338 
    339 	PCI_CONF_UNLOCK(s);
    340 }
    341 
    342 int
    343 empb_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    344 {
    345 	return 6; /* no Mediator with more than 6 slots? */
    346 }
    347 
    348 void
    349 empb_pci_attach_hook(struct device *parent, struct device *self,
    350     struct pcibus_attach_args *pba)
    351 {
    352 }
    353 
    354 int
    355 empb_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    356 {
    357 	/* TODO: add sanity checking */
    358 
    359 	*ihp = EMPB_INT;
    360 	return 0;
    361 }
    362 
    363 const struct evcnt *
    364 empb_pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
    365 {
    366 	/* TODO: implement */
    367 	return NULL;
    368 }
    369 
    370