Home | History | Annotate | Line # | Download | only in pci
empb.c revision 1.5
      1 /*	$NetBSD: empb.c,v 1.5 2012/06/04 12:56:48 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 <amiga/dev/zbusvar.h>
     50 #include <amiga/pci/empbreg.h>
     51 #include <amiga/pci/empbvar.h>
     52 #include <amiga/pci/emmemvar.h>
     53 
     54 #include <dev/pci/pciconf.h>
     55 
     56 #include "opt_pci.h"
     57 
     58 /* #define EMPB_DEBUG 1  */
     59 
     60 #define	PCI_CONF_LOCK(s)	(s) = splhigh()
     61 #define	PCI_CONF_UNLOCK(s)	splx((s))
     62 
     63 #define WINDOW_LOCK(s)		(s) = splhigh()
     64 #define WINDOW_UNLOCK(s)	splx((s))
     65 
     66 static int	empb_match(struct device *, struct cfdata *, void *);
     67 static void	empb_attach(struct device *, struct device *, void *);
     68 
     69 static void	empb_callback(device_t self);
     70 
     71 static void	empb_find_mem(struct empb_softc *sc);
     72 static void	empb_switch_bridge(struct empb_softc *sc, uint8_t mode);
     73 static void	empb_intr_enable(struct empb_softc *sc);
     74 
     75 pcireg_t	empb_pci_conf_read(pci_chipset_tag_t, pcitag_t, int);
     76 void		empb_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
     77 int		empb_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno);
     78 void		empb_pci_attach_hook(struct device *parent,
     79 		    struct device *self, struct pcibus_attach_args *pba);
     80 pcitag_t	empb_pci_make_tag(pci_chipset_tag_t pc, int bus, int device,
     81 		    int function);
     82 void		empb_pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag,
     83 		    int *bp, int *dp, int *fp);
     84 int		empb_pci_intr_map(const struct pci_attach_args *pa,
     85 		    pci_intr_handle_t *ihp);
     86 const struct evcnt * empb_pci_intr_evcnt(pci_chipset_tag_t pc,
     87 		    pci_intr_handle_t ih);
     88 int		empb_pci_conf_hook(pci_chipset_tag_t pct, int bus,
     89 		    int dev, int func, pcireg_t id);
     90 
     91 CFATTACH_DECL_NEW(empb, sizeof(struct empb_softc),
     92     empb_match, empb_attach, NULL, NULL);
     93 
     94 static int
     95 empb_match(device_t parent, cfdata_t cf, void *aux)
     96 {
     97 	struct zbus_args *zap;
     98 
     99 	zap = aux;
    100 
    101 	if (zap->manid != ZORRO_MANID_ELBOX)
    102 		return 0;
    103 
    104 	switch (zap->prodid) {
    105 	case ZORRO_PRODID_MED1K2:
    106 	case ZORRO_PRODID_MED1K2SX:
    107 	case ZORRO_PRODID_MED1K2LT2:
    108 	case ZORRO_PRODID_MED1K2LT4:
    109 	case ZORRO_PRODID_MED1K2TX:
    110 		return 1;
    111 	}
    112 
    113 	return 0;
    114 }
    115 
    116 
    117 static void
    118 empb_attach(device_t parent, device_t self, void *aux)
    119 {
    120 	struct empb_softc *sc;
    121 	struct zbus_args *zap;
    122 
    123 	volatile char *ba;
    124 
    125 	zap = aux;
    126 	sc = device_private(self);
    127 	sc->sc_dev = self;
    128 	ba = zap->va;
    129 
    130 	switch (zap->prodid) {
    131 	case ZORRO_PRODID_MED1K2:
    132 		aprint_normal(": ELBOX Mediator PCI 1200\n");
    133 		break;
    134 	case ZORRO_PRODID_MED1K2SX:
    135 		aprint_normal(": ELBOX Mediator PCI 1200 SX\n");
    136 		break;
    137 	case ZORRO_PRODID_MED1K2LT2:
    138 		aprint_normal(": ELBOX Mediator PCI 1200 LT2\n");
    139 		break;
    140 	case ZORRO_PRODID_MED1K2LT4:
    141 		aprint_normal(": ELBOX Mediator PCI 1200 LT4\n");
    142 		break;
    143 	case ZORRO_PRODID_MED1K2TX:
    144 		aprint_normal(": ELBOX Mediator PCI 1200 TX\n");
    145 		break;
    146 	default:
    147 		aprint_normal(": ELBOX Mediator PCI (unknown)\n");
    148 		break;
    149 	}
    150 
    151 	/* Setup bus space mappings. */
    152 	sc->pci_confio_area.base = (bus_addr_t) ba + EMPB_BRIDGE_OFF;
    153 	sc->pci_confio_area.absm = &amiga_bus_stride_1swap;
    154 
    155 	sc->setup_area.base = (bus_addr_t) ba + EMPB_SETUP_OFF;
    156 	sc->setup_area.absm = &amiga_bus_stride_1;
    157 
    158 	/*
    159 	 * Defer everything until later, we need to wait for possible
    160 	 * emmem attachments.
    161 	 */
    162 
    163 	config_defer(self, empb_callback);
    164 }
    165 
    166 static void
    167 empb_callback(device_t self) {
    168 
    169 	struct empb_softc *sc;
    170 	pci_chipset_tag_t pc;
    171 	struct pcibus_attach_args pba;
    172 #ifdef PCI_NETBSD_CONFIGURE
    173 	struct extent *ioext, *memext;
    174 #endif /* PCI_NETBSD_CONFIGURE */
    175 
    176 	sc = device_private(self);
    177 	pc = &sc->apc;
    178 
    179 #ifdef EMPB_DEBUG
    180 	aprint_normal("empb: mapped setup %x->%x, conf/io %x->%x\n",
    181 	    kvtop((void*) sc->setup_area.base), sc->setup_area.base,
    182 	    kvtop((void*) sc->pci_confio_area.base), sc->pci_confio_area.base);
    183 #endif
    184 
    185 	sc->pci_confio_t = &(sc->pci_confio_area);
    186 
    187 	/*
    188 	 * We should not map I/O space here, however we have no choice
    189 	 * since these addresses are shared between configuration space and
    190 	 * I/O space. Not really a problem on m68k, however on PPC...
    191 	 */
    192 	if (bus_space_map(sc->pci_confio_t, 0, EMPB_BRIDGE_SIZE, 0,
    193 	    &sc->pci_confio_h))
    194 		aprint_error_dev(self,
    195 		    "couldn't map PCI configuration & I/O space\n");
    196 
    197 	sc->apc.pci_conf_datat = sc->pci_confio_t;
    198 	sc->apc.pci_conf_datah = sc->pci_confio_h;
    199 
    200 	sc->setup_area_t = &(sc->setup_area);
    201 
    202 	if (bus_space_map(sc->setup_area_t, 0, EMPB_SETUP_SIZE, 0,
    203 	    &sc->setup_area_h))
    204 		aprint_error_dev(self,
    205 		    "couldn't map Mediator setup space\n");
    206 
    207 	empb_find_mem(sc);
    208 	if (sc->pci_mem_win_size == 0)
    209 		aprint_error_dev(self,
    210 		    "couldn't find memory space, check your WINDOW jumper\n");
    211 
    212 	/* just a test */
    213 	empb_switch_window(sc, 0x80000000);
    214 	empb_switch_window(sc, 0x82F00000);
    215 	empb_switch_window(sc, 0x82000000);
    216 
    217 	/* Initialize the PCI chipset tag. */
    218 	sc->apc.pc_conf_v = (void*) pc;
    219 	sc->apc.pc_bus_maxdevs = empb_pci_bus_maxdevs;
    220 	sc->apc.pc_make_tag = amiga_pci_make_tag;
    221 	sc->apc.pc_decompose_tag = amiga_pci_decompose_tag;
    222 	sc->apc.pc_conf_read = empb_pci_conf_read;
    223 	sc->apc.pc_conf_write = empb_pci_conf_write;
    224 	sc->apc.pc_attach_hook = empb_pci_attach_hook;
    225 
    226 	sc->apc.pc_intr_map = empb_pci_intr_map;
    227 	sc->apc.pc_intr_string = amiga_pci_intr_string;
    228 	sc->apc.pc_intr_establish = amiga_pci_intr_establish;
    229 	sc->apc.pc_intr_disestablish = amiga_pci_intr_disestablish;
    230 
    231 	sc->apc.pc_conf_hook = empb_pci_conf_hook;
    232 	sc->apc.pc_conf_interrupt = amiga_pci_conf_interrupt;
    233 
    234 	sc->apc.cookie = sc;
    235 
    236 #ifdef PCI_NETBSD_CONFIGURE
    237 	ioext = extent_create("empbio", 0, EMPB_BRIDGE_SIZE,
    238 	    NULL, 0, EX_NOWAIT);
    239 
    240 	memext = extent_create("empbmem", EMPB_MEM_BASE, EMPB_MEM_END,
    241 	    NULL, 0, EX_NOWAIT);
    242 
    243 	pci_configure_bus(pc, ioext, memext, NULL, 0, CACHELINE_SIZE);
    244 
    245 	extent_destroy(ioext);
    246 	extent_destroy(memext);
    247 
    248 #endif /* PCI_NETBSD_CONFIGURE */
    249 
    250 	pba.pba_iot = &(sc->pci_confio_area);
    251 	pba.pba_dmat = NULL;
    252 	pba.pba_dmat64 = NULL;
    253 	pba.pba_pc = pc;
    254 	pba.pba_flags = PCI_FLAGS_IO_OKAY;
    255 
    256 	if(sc->pci_mem_win_size > 0) {
    257 		pba.pba_memt = &(sc->pci_mem_win);
    258 		pba.pba_flags |= PCI_FLAGS_MEM_OKAY;
    259 	} else
    260 		pba.pba_memt = NULL;
    261 
    262 	pba.pba_bus = 0;
    263 	pba.pba_bridgetag = NULL;
    264 
    265 	empb_intr_enable(sc);
    266 
    267 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    268 }
    269 
    270 static void
    271 empb_intr_enable(struct empb_softc *sc)
    272 {
    273 	bus_space_write_1(sc->setup_area_t, sc->setup_area_h,
    274 	    EMPB_SETUP_INTR_OFF, EMPB_INTR_ENABLE);
    275 }
    276 
    277 /*
    278  * Switch between configuration space and I/O space.
    279  */
    280 static void
    281 empb_switch_bridge(struct empb_softc *sc, uint8_t mode)
    282 {
    283 	bus_space_write_1(sc->setup_area_t, sc->setup_area_h,
    284 	    EMPB_SETUP_BRIDGE_OFF, mode);
    285 }
    286 
    287 
    288 /*
    289  * Try to find a (optional) memory window board.
    290  */
    291 static void
    292 empb_find_mem(struct empb_softc *sc)
    293 {
    294 	device_t memdev;
    295 	struct emmem_softc *mem_sc;
    296 
    297 	memdev = device_find_by_xname("emmem0");
    298 	sc->pci_mem_win_size = 0;
    299 
    300 	if(memdev == NULL) {
    301 		return;
    302 	}
    303 
    304 	mem_sc = device_private(memdev);
    305 
    306 	sc->pci_mem_win.base = (bus_addr_t) mem_sc->sc_base;
    307 	sc->pci_mem_win.absm = &empb_bus_swap;
    308 	sc->pci_mem_win_size = mem_sc->sc_size;
    309 	sc->pci_mem_win_t = &sc->pci_mem_win;
    310 
    311 	if(sc->pci_mem_win_size == 8*1024*1024)
    312 		sc->pci_mem_win_mask = EMPB_WINDOW_MASK_8M;
    313 	else if(sc->pci_mem_win_size == 4*1024*1024)
    314 		sc->pci_mem_win_mask = EMPB_WINDOW_MASK_4M;
    315 	else /* disable anyway */
    316 		sc->pci_mem_win_size = 0;
    317 
    318 #ifdef EMPB_DEBUG
    319 	aprint_normal("empb: found %x b window at %p, switch mask %x\n",
    320 	    sc->pci_mem_win_size, (void*) sc->pci_mem_win.base,
    321 	    sc->pci_mem_win_mask);
    322 #endif /* EMPB_DEBUG */
    323 
    324 }
    325 
    326 /*
    327  * Switch memory window position. Return PCI mem address seen at the beginning
    328  * of window.
    329  */
    330 bus_addr_t
    331 empb_switch_window(struct empb_softc *sc, bus_addr_t address)
    332 {
    333 	int s;
    334 	uint16_t win_reg;
    335 #ifdef EMPB_DEBUG
    336 	uint16_t rwin_reg;
    337 #endif /* EMPB_DEBUG */
    338 
    339 	WINDOW_LOCK(s);
    340 
    341 	win_reg = bswap16((address >> EMPB_WINDOW_SHIFT)
    342 	    & sc->pci_mem_win_mask);
    343 
    344 	bus_space_write_2(sc->setup_area_t, sc->setup_area_h,
    345 	    EMPB_SETUP_WINDOW_OFF, win_reg);
    346 
    347 	/* store window pos, like: sc->pci_mem_win_pos = win_reg ? */
    348 
    349 #ifdef EMPB_DEBUG
    350 	rwin_reg = bus_space_read_2(sc->setup_area_t, sc->setup_area_h,
    351 	    EMPB_SETUP_WINDOW_OFF);
    352 
    353 	aprint_normal("empb: access to %p window switch to %x => reg now %x\n",
    354 	    (void*) address, win_reg, rwin_reg);
    355 #endif /* EMPB_DEBUG */
    356 
    357 	WINDOW_UNLOCK(s);
    358 
    359 	return (bus_addr_t)((bswap16(win_reg)) << EMPB_WINDOW_SHIFT);
    360 }
    361 
    362 
    363 pcireg_t
    364 empb_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    365 {
    366 	uint32_t data;
    367 	uint32_t bus, dev, func;
    368 	struct empb_softc *sc;
    369 	int s;
    370 
    371 	sc = pc->cookie;
    372 
    373 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    374 
    375 	PCI_CONF_LOCK(s);
    376 
    377 	empb_switch_bridge(sc, BRIDGE_CONF);
    378 
    379 	data = bus_space_read_4(pc->pci_conf_datat, pc->pci_conf_datah,
    380 	    EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg);
    381 #ifdef EMPB_DEBUG_CONF
    382 	aprint_normal("empb conf read va: %lx, bus: %d, dev: %d, "
    383 	    "func: %d, reg: %d -r-> data %x\n",
    384 	    pc->pci_conf_datah, bus, dev, func, reg, data);
    385 #endif /* EMPB_DEBUG_CONF */
    386 
    387 	empb_switch_bridge(sc, BRIDGE_IO);
    388 
    389 	PCI_CONF_UNLOCK(s);
    390 
    391 	return data;
    392 }
    393 
    394 void
    395 empb_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val)
    396 {
    397 	uint32_t bus, dev, func;
    398 	struct empb_softc *sc;
    399 	int s;
    400 
    401 	sc = pc->cookie;
    402 
    403 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    404 
    405 	PCI_CONF_LOCK(s);
    406 
    407 	empb_switch_bridge(sc, BRIDGE_CONF);
    408 
    409 	bus_space_write_4(pc->pci_conf_datat, pc->pci_conf_datah,
    410 	    EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg, val);
    411 #ifdef EMPB_DEBUG_CONF
    412 	aprint_normal("empb conf write va: %lx, bus: %d, dev: %d, "
    413 	    "func: %d, reg: %d -w-> data %x\n",
    414 	    pc->pci_conf_datah, bus, dev, func, reg, val);
    415 #endif /* EMPB_DEBUG_CONF */
    416 	empb_switch_bridge(sc, BRIDGE_IO);
    417 
    418 	PCI_CONF_UNLOCK(s);
    419 }
    420 
    421 int
    422 empb_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    423 {
    424 	return 6; /* no Mediator with more than 6 slots? */
    425 }
    426 
    427 void
    428 empb_pci_attach_hook(struct device *parent, struct device *self,
    429     struct pcibus_attach_args *pba)
    430 {
    431 }
    432 
    433 int
    434 empb_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    435 {
    436 	/* TODO: add sanity checking */
    437 
    438 	*ihp = EMPB_INT;
    439 	return 0;
    440 }
    441 
    442 const struct evcnt *
    443 empb_pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
    444 {
    445 	/* TODO: implement */
    446 	return NULL;
    447 }
    448 
    449 int
    450 empb_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func,
    451     pcireg_t id)
    452 {
    453 
    454 	/*
    455 	 * Register information about some known PCI devices with
    456 	 * DMA-able memory.
    457 	 */
    458 	/*if ((PCI_VENDOR(id) == PCI_VENDOR_3DFX) &&
    459 		(PCI_PRODUCT(id) >= PCI_PRODUCT_3DFX_VOODOO3))*/
    460 
    461 
    462         return PCI_CONF_DEFAULT;
    463 }
    464