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