Home | History | Annotate | Line # | Download | only in pci
em4k.c revision 1.1
      1 /*	$NetBSD: em4k.c,v 1.1 2013/01/29 00:49:42 rkujawa Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2013 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 4000 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 <amiga/dev/zbusvar.h>
     50 #include <amiga/pci/empbreg.h>
     51 #include <amiga/pci/em4kvar.h>
     52 #include <amiga/pci/emmemvar.h>
     53 
     54 #include <dev/pci/pciconf.h>
     55 
     56 #include "opt_pci.h"
     57 
     58 static int	em4k_match(device_t, cfdata_t, void *);
     59 static void	em4k_attach(device_t, device_t, void *);
     60 static void	em4k_callback(device_t);
     61 
     62 static void	em4k_find_mem(struct em4k_softc *);
     63 static void	em4k_intr_enable(struct em4k_softc *);
     64 
     65 pcireg_t	em4k_pci_conf_read(pci_chipset_tag_t, pcitag_t, int);
     66 void		em4k_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
     67 int		em4k_pci_bus_maxdevs(pci_chipset_tag_t, int);
     68 void		em4k_pci_attach_hook(device_t, device_t,
     69 		    struct pcibus_attach_args *);
     70 pcitag_t	em4k_pci_make_tag(pci_chipset_tag_t, int, int, int);
     71 void		em4k_pci_decompose_tag(pci_chipset_tag_t, pcitag_t,
     72 		    int *, int *, int *);
     73 int		em4k_pci_intr_map(const struct pci_attach_args *,
     74 		    pci_intr_handle_t *);
     75 const struct evcnt * em4k_pci_intr_evcnt(pci_chipset_tag_t,
     76 		    pci_intr_handle_t);
     77 int		em4k_pci_conf_hook(pci_chipset_tag_t, int, int, int, pcireg_t);
     78 
     79 CFATTACH_DECL_NEW(em4k, sizeof(struct em4k_softc),
     80     em4k_match, em4k_attach, NULL, NULL);
     81 
     82 static int
     83 em4k_match(device_t parent, cfdata_t cf, void *aux)
     84 {
     85 	struct zbus_args *zap;
     86 
     87 	zap = aux;
     88 
     89 	if (zap->manid != ZORRO_MANID_ELBOX)
     90 		return 0;
     91 
     92 	switch (zap->prodid) {
     93 	case ZORRO_PRODID_MED4K:
     94 	case ZORRO_PRODID_MED4KMKII:
     95 		return 1;
     96 	}
     97 
     98 	return 0;
     99 }
    100 
    101 
    102 static void
    103 em4k_attach(device_t parent, device_t self, void *aux)
    104 {
    105 	struct em4k_softc *sc;
    106 	struct zbus_args *zap;
    107 
    108 	volatile char *ba;
    109 
    110 	zap = aux;
    111 	sc = device_private(self);
    112 	sc->sc_dev = self;
    113 	ba = zap->va;
    114 
    115 	sc->model = zap->prodid;
    116 
    117 	switch (sc->model) {
    118 	case ZORRO_PRODID_MED4K:
    119 		aprint_normal(": ELBOX Mediator PCI 4000\n");
    120 		break;
    121 	case ZORRO_PRODID_MED4KMKII:
    122 		aprint_normal(": ELBOX Mediator PCI 4000 MK-II\n");
    123 		break;
    124 	default:
    125 		aprint_normal(": ELBOX Mediator PCI (unknown)\n");
    126 		break;
    127 	}
    128 
    129 	/* Setup bus space mappings. */
    130 	sc->pci_conf_area.base = (bus_addr_t) ba + EM4K_CONF_OFF;
    131 	sc->pci_conf_area.absm = &amiga_bus_stride_1swap;
    132 
    133 	sc->pci_io_area.base = (bus_addr_t) ba + EM4K_IO_OFF;
    134 	sc->pci_io_area.absm = &amiga_bus_stride_1swap;
    135 
    136 	sc->setup_area.base = (bus_addr_t) ba + EM4K_SETUP_OFF;
    137 	sc->setup_area.absm = &amiga_bus_stride_1;
    138 
    139 	/*
    140 	 * Defer everything until later, we need to wait for possible
    141 	 * emmem attachments.
    142 	 */
    143 
    144 	config_defer(self, em4k_callback);
    145 }
    146 
    147 static void
    148 em4k_callback(device_t self) {
    149 
    150 	struct em4k_softc *sc;
    151 	pci_chipset_tag_t pc;
    152 	struct pcibus_attach_args pba;
    153 #ifdef PCI_NETBSD_CONFIGURE
    154 	/* struct extent *ioext, *memext; */
    155 #endif /* PCI_NETBSD_CONFIGURE */
    156 
    157 	sc = device_private(self);
    158 	pc = &sc->apc;
    159 
    160 #ifdef EM4K_DEBUG
    161 	aprint_normal("em4k: mapped setup %x->%x, conf %x->%x, io %x->%x\n",
    162 	    kvtop((void*) sc->setup_area.base), sc->setup_area.base,
    163 	    kvtop((void*) sc->pci_conf_area.base), sc->pci_conf_area.base,
    164 	    kvtop((void*) sc->pci_io_area.base), sc->pci_io_area.base);
    165 #endif
    166 
    167 	sc->pci_conf_t = &(sc->pci_conf_area);
    168 	sc->pci_io_t = &(sc->pci_io_area);
    169 
    170 	if (bus_space_map(sc->pci_conf_t, 0, EM4K_CONF_SIZE, 0,
    171 	    &sc->pci_conf_h))
    172 		aprint_error_dev(self, "couldn't map PCI config space\n");
    173 
    174 	if (bus_space_map(sc->pci_io_t, 0, EM4K_IO_SIZE, 0,
    175 	    &sc->pci_io_h))
    176 		aprint_error_dev(self, "couldn't map PCI I/O space\n");
    177 
    178 	sc->apc.pci_conf_datat = sc->pci_conf_t;
    179 	sc->apc.pci_conf_datah = sc->pci_conf_h;
    180 
    181 	sc->setup_area_t = &(sc->setup_area);
    182 
    183 	if (bus_space_map(sc->setup_area_t, 0, EM4K_SETUP_SIZE, 0,
    184 	    &sc->setup_area_h))
    185 		aprint_error_dev(self,
    186 		    "couldn't map Mediator setup space\n");
    187 
    188 	em4k_find_mem(sc);
    189 	if (sc->pci_mem_win_size == 0)
    190 		aprint_error_dev(self,
    191 		    "couldn't find memory space, this shouldn't happen!\n");
    192 
    193 	/* Initialize the PCI chipset tag. */
    194 	sc->apc.pc_conf_v = (void*) pc;
    195 	sc->apc.pc_bus_maxdevs = em4k_pci_bus_maxdevs;
    196 	sc->apc.pc_make_tag = amiga_pci_make_tag;
    197 	sc->apc.pc_decompose_tag = amiga_pci_decompose_tag;
    198 	sc->apc.pc_conf_read = em4k_pci_conf_read;
    199 	sc->apc.pc_conf_write = em4k_pci_conf_write;
    200 	sc->apc.pc_attach_hook = em4k_pci_attach_hook;
    201 
    202 	sc->apc.pc_intr_map = em4k_pci_intr_map;
    203 	sc->apc.pc_intr_string = amiga_pci_intr_string;
    204 	sc->apc.pc_intr_establish = amiga_pci_intr_establish;
    205 	sc->apc.pc_intr_disestablish = amiga_pci_intr_disestablish;
    206 
    207 	sc->apc.pc_conf_hook = em4k_pci_conf_hook;
    208 	sc->apc.pc_conf_interrupt = amiga_pci_conf_interrupt;
    209 
    210 	sc->apc.cookie = sc;
    211 
    212 #ifdef PCI_NETBSD_CONFIGURE
    213 	/* XXX: not yet
    214 	ioext = extent_create("em4kio", 0, EMPB_BRIDGE_SIZE,
    215 	    NULL, 0, EX_NOWAIT);
    216 
    217 	memext = extent_create("em4kmem", EMPB_MEM_BASE, EMPB_MEM_END,
    218 	    NULL, 0, EX_NOWAIT);
    219 
    220 	pci_configure_bus(pc, ioext, memext, NULL, 0, CACHELINE_SIZE);
    221 
    222 	extent_destroy(ioext);
    223 	extent_destroy(memext);
    224 	*/
    225 
    226 #endif /* PCI_NETBSD_CONFIGURE */
    227 
    228 	pba.pba_iot = &(sc->pci_io_area);
    229 	pba.pba_dmat = NULL;
    230 	pba.pba_dmat64 = NULL;
    231 	pba.pba_pc = pc;
    232 	pba.pba_flags = PCI_FLAGS_IO_OKAY;
    233 
    234 	if (sc->pci_mem_win_size > 0) {
    235 		pba.pba_memt = &(sc->pci_mem_win);
    236 		pba.pba_flags |= PCI_FLAGS_MEM_OKAY;
    237 	} else
    238 		pba.pba_memt = NULL;
    239 
    240 	pba.pba_bus = 0;
    241 	pba.pba_bridgetag = NULL;
    242 
    243 	/* XXX: set correct window pos */
    244 	/*bus_space_write_2(sc->setup_area_t, sc->setup_area_h,
    245 	    EMPB_SETUP_WINDOW_OFF, XXX);*/
    246 
    247 	em4k_intr_enable(sc);
    248 
    249 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    250 }
    251 
    252 static void
    253 em4k_intr_enable(struct em4k_softc *sc)
    254 {
    255 	bus_space_write_1(sc->setup_area_t, sc->setup_area_h,
    256 	    EM4K_SETUP_INTR_OFF, EMPB_INTR_ENABLE);
    257 }
    258 
    259 /*
    260  * Try to find a (optional) memory window board.
    261  */
    262 static void
    263 em4k_find_mem(struct em4k_softc *sc)
    264 {
    265 	device_t memdev;
    266 	struct emmem_softc *mem_sc;
    267 
    268 	memdev = device_find_by_xname("emmem0");
    269 	sc->pci_mem_win_size = 0;
    270 
    271 	if (memdev == NULL) {
    272 		return;
    273 	}
    274 
    275 	mem_sc = device_private(memdev);
    276 
    277 	sc->pci_mem_win.base = (bus_addr_t) mem_sc->sc_base;
    278 	sc->pci_mem_win.absm = &amiga_bus_stride_1swap_abs;
    279 	sc->pci_mem_win_size = mem_sc->sc_size;
    280 	sc->pci_mem_win_t = &sc->pci_mem_win;
    281 
    282 	if (sc->pci_mem_win_size == 512*1024*1024)
    283 		sc->pci_mem_win_mask = EM4K_WINDOW_MASK_512M;
    284 	else if (sc->pci_mem_win_size == 256*1024*1024)
    285 		sc->pci_mem_win_mask = EM4K_WINDOW_MASK_256M;
    286 	else /* disable anyway */
    287 		sc->pci_mem_win_size = 0;
    288 
    289 #ifdef EM4K_DEBUG
    290 	aprint_normal("em4k: found %x b window at %p, switch mask %x\n",
    291 	    sc->pci_mem_win_size, (void*) sc->pci_mem_win.base,
    292 	    sc->pci_mem_win_mask);
    293 #endif /* EM4K_DEBUG */
    294 
    295 }
    296 
    297 pcireg_t
    298 em4k_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    299 {
    300 	uint32_t data;
    301 	uint32_t bus, dev, func;
    302 	struct em4k_softc *sc;
    303 
    304 	sc = pc->cookie;
    305 
    306 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    307 
    308 	data = bus_space_read_4(pc->pci_conf_datat, pc->pci_conf_datah,
    309 	    EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg);
    310 #ifdef EM4K_DEBUG_CONF
    311 	aprint_normal("em4k conf read va: %lx, bus: %d, dev: %d, "
    312 	    "func: %d, reg: %d -r-> data %x\n",
    313 	    pc->pci_conf_datah, bus, dev, func, reg, data);
    314 #endif /* EM4K_DEBUG_CONF */
    315 
    316 	return data;
    317 }
    318 
    319 void
    320 em4k_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val)
    321 {
    322 	uint32_t bus, dev, func;
    323 	struct em4k_softc *sc;
    324 
    325 	sc = pc->cookie;
    326 
    327 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    328 
    329 	bus_space_write_4(pc->pci_conf_datat, pc->pci_conf_datah,
    330 	    EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg, val);
    331 #ifdef EM4K_DEBUG_CONF
    332 	aprint_normal("em4k conf write va: %lx, bus: %d, dev: %d, "
    333 	    "func: %d, reg: %d -w-> data %x\n",
    334 	    pc->pci_conf_datah, bus, dev, func, reg, val);
    335 #endif /* EM4K_DEBUG_CONF */
    336 }
    337 
    338 int
    339 em4k_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    340 {
    341 	return 6; /* no Mediator with more than 6 slots? */
    342 }
    343 
    344 void
    345 em4k_pci_attach_hook(device_t parent, device_t self,
    346     struct pcibus_attach_args *pba)
    347 {
    348 }
    349 
    350 int
    351 em4k_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    352 {
    353 	/* TODO: add sanity checking */
    354 
    355 	*ihp = EMPB_INT;
    356 	return 0;
    357 }
    358 
    359 const struct evcnt *
    360 em4k_pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
    361 {
    362 	/* TODO: implement */
    363 	return NULL;
    364 }
    365 
    366 int
    367 em4k_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func,
    368     pcireg_t id)
    369 {
    370 	return PCI_CONF_DEFAULT;
    371 }
    372