Home | History | Annotate | Line # | Download | only in pci
em4k.c revision 1.8
      1 /*	$NetBSD: em4k.c,v 1.8 2021/08/07 16:18:42 thorpej 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/kmem.h>
     42 
     43 #include <uvm/uvm_extern.h>
     44 
     45 #include <machine/bus.h>
     46 #include <machine/cpu.h>
     47 
     48 #include <amiga/dev/zbusvar.h>
     49 #include <amiga/pci/empbreg.h>
     50 #include <amiga/pci/em4kvar.h>
     51 #include <amiga/pci/emmemvar.h>
     52 
     53 #include <dev/pci/pciconf.h>
     54 
     55 #include "opt_pci.h"
     56 
     57 static int	em4k_match(device_t, cfdata_t, void *);
     58 static void	em4k_attach(device_t, device_t, void *);
     59 static void	em4k_callback(device_t);
     60 
     61 static void	em4k_find_mem(struct em4k_softc *);
     62 static void	em4k_intr_enable(struct em4k_softc *);
     63 
     64 pcireg_t	em4k_pci_conf_read(pci_chipset_tag_t, pcitag_t, int);
     65 void		em4k_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
     66 int		em4k_pci_bus_maxdevs(pci_chipset_tag_t, int);
     67 void		em4k_pci_attach_hook(device_t, device_t,
     68 		    struct pcibus_attach_args *);
     69 pcitag_t	em4k_pci_make_tag(pci_chipset_tag_t, int, int, int);
     70 void		em4k_pci_decompose_tag(pci_chipset_tag_t, pcitag_t,
     71 		    int *, int *, int *);
     72 int		em4k_pci_intr_map(const struct pci_attach_args *,
     73 		    pci_intr_handle_t *);
     74 const struct evcnt * em4k_pci_intr_evcnt(pci_chipset_tag_t,
     75 		    pci_intr_handle_t);
     76 int		em4k_pci_conf_hook(pci_chipset_tag_t, int, int, int, pcireg_t);
     77 
     78 #ifdef PCI_NETBSD_CONFIGURE
     79 static void	em4k_pci_configure(struct em4k_softc *sc);
     80 #endif /* PCI_NETBSD_CONFIGURE */
     81 
     82 CFATTACH_DECL_NEW(em4k, sizeof(struct em4k_softc),
     83     em4k_match, em4k_attach, NULL, NULL);
     84 
     85 static int
     86 em4k_match(device_t parent, cfdata_t cf, void *aux)
     87 {
     88 	struct zbus_args *zap;
     89 
     90 	zap = aux;
     91 
     92 	if (zap->manid != ZORRO_MANID_ELBOX)
     93 		return 0;
     94 
     95 	switch (zap->prodid) {
     96 	case ZORRO_PRODID_MED4K:
     97 	case ZORRO_PRODID_MED4KMKII:
     98 		return 1;
     99 	}
    100 
    101 	return 0;
    102 }
    103 
    104 
    105 static void
    106 em4k_attach(device_t parent, device_t self, void *aux)
    107 {
    108 	struct em4k_softc *sc;
    109 	struct zbus_args *zap;
    110 
    111 	volatile char *ba;
    112 
    113 	zap = aux;
    114 	sc = device_private(self);
    115 	sc->sc_dev = self;
    116 	ba = zap->va;
    117 
    118 	sc->model = zap->prodid;
    119 
    120 	switch (sc->model) {
    121 	case ZORRO_PRODID_MED4K:
    122 		aprint_normal(": ELBOX Mediator PCI 4000\n");
    123 		break;
    124 	case ZORRO_PRODID_MED4KMKII:
    125 		aprint_normal(": ELBOX Mediator PCI 4000 MK-II\n");
    126 		break;
    127 	default:
    128 		aprint_normal(": ELBOX Mediator PCI (unknown)\n");
    129 		break;
    130 	}
    131 
    132 	/* Setup bus space mappings. */
    133 	sc->pci_conf_area.base = (bus_addr_t) ba + EM4K_CONF_OFF;
    134 	sc->pci_conf_area.absm = &amiga_bus_stride_1swap;
    135 
    136 	sc->pci_io_area.base = (bus_addr_t) ba + EM4K_IO_OFF;
    137 	sc->pci_io_area.absm = &amiga_bus_stride_1swap;
    138 
    139 	sc->setup_area.base = (bus_addr_t) ba + EM4K_SETUP_OFF;
    140 	sc->setup_area.absm = &amiga_bus_stride_1;
    141 
    142 	/*
    143 	 * Defer everything until later, we need to wait for possible
    144 	 * emmem attachments.
    145 	 */
    146 
    147 	config_defer(self, em4k_callback);
    148 }
    149 
    150 static void
    151 em4k_callback(device_t self) {
    152 
    153 	struct em4k_softc *sc;
    154 	pci_chipset_tag_t pc;
    155 	struct pcibus_attach_args pba;
    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 	em4k_pci_configure(sc);
    214 #endif /* PCI_NETBSD_CONFIGURE */
    215 
    216 	pba.pba_iot = &(sc->pci_io_area);
    217 	pba.pba_dmat = NULL;
    218 	pba.pba_dmat64 = NULL;
    219 	pba.pba_pc = pc;
    220 	pba.pba_flags = PCI_FLAGS_IO_OKAY;
    221 
    222 	if (sc->pci_mem_win_size > 0) {
    223 		pba.pba_memt = &(sc->pci_mem_win);
    224 		pba.pba_flags |= PCI_FLAGS_MEM_OKAY;
    225 	} else
    226 		pba.pba_memt = NULL;
    227 
    228 	pba.pba_bus = 0;
    229 	pba.pba_bridgetag = NULL;
    230 
    231 	/* Set correct window position. */
    232 	bus_space_write_1(sc->setup_area_t, sc->setup_area_h,
    233 	    EM4K_SETUP_WINDOW_OFF, kvtop((void*) sc->pci_mem_win.base) >>
    234 	    EM4K_WINDOW_SHIFT);
    235 
    236 	em4k_intr_enable(sc);
    237 
    238 	config_found(self, &pba, pcibusprint, CFARGS_NONE);
    239 }
    240 
    241 #ifdef PCI_NETBSD_CONFIGURE
    242 static void
    243 em4k_pci_configure(struct em4k_softc *sc)
    244 {
    245 	struct pciconf_resources *pcires;
    246 
    247 	pcires = pciconf_resource_init();
    248 
    249 	/* I/O addresses are relative to I/O space address. */
    250 	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO, 0, EM4K_IO_SIZE);
    251 
    252 	/*
    253 	 * Memory space addresses are absolute (and keep in mind that
    254 	 * they are in a separate address space.
    255 	 */
    256 	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
    257 	    kvtop((void*) sc->pci_mem_win.base), sc->pci_mem_win_size);
    258 
    259 	pci_configure_bus(&sc->apc, pcires, 0, CACHELINE_SIZE);
    260 
    261 	pciconf_resource_fini(pcires);
    262 }
    263 #endif /* PCI_NETBSD_CONFIGURE */
    264 
    265 static void
    266 em4k_intr_enable(struct em4k_softc *sc)
    267 {
    268 	bus_space_write_1(sc->setup_area_t, sc->setup_area_h,
    269 	    EM4K_SETUP_INTR_OFF, EMPB_INTR_ENABLE);
    270 }
    271 
    272 /*
    273  * Try to find a (optional) memory window board.
    274  */
    275 static void
    276 em4k_find_mem(struct em4k_softc *sc)
    277 {
    278 	device_t memdev;
    279 	struct emmem_softc *mem_sc;
    280 
    281 	memdev = device_find_by_xname("emmem0");
    282 	sc->pci_mem_win_size = 0;
    283 
    284 	if (memdev == NULL) {
    285 		return;
    286 	}
    287 
    288 	mem_sc = device_private(memdev);
    289 
    290 	sc->pci_mem_win.base = (bus_addr_t) mem_sc->sc_base;
    291 	sc->pci_mem_win.absm = &amiga_bus_stride_1swap_abs;
    292 	sc->pci_mem_win_size = mem_sc->sc_size;
    293 	sc->pci_mem_win_t = &sc->pci_mem_win;
    294 
    295 	if (sc->pci_mem_win_size == 512*1024*1024)
    296 		sc->pci_mem_win_mask = EM4K_WINDOW_MASK_512M;
    297 	else if (sc->pci_mem_win_size == 256*1024*1024)
    298 		sc->pci_mem_win_mask = EM4K_WINDOW_MASK_256M;
    299 	else /* disable anyway */
    300 		sc->pci_mem_win_size = 0;
    301 
    302 #ifdef EM4K_DEBUG
    303 	aprint_normal("em4k: found %x b window at %p, switch mask %x\n",
    304 	    sc->pci_mem_win_size, (void*) sc->pci_mem_win.base,
    305 	    sc->pci_mem_win_mask);
    306 #endif /* EM4K_DEBUG */
    307 
    308 }
    309 
    310 pcireg_t
    311 em4k_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    312 {
    313 	uint32_t data;
    314 	uint32_t bus, dev, func;
    315 
    316 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    317 		return (pcireg_t) -1;
    318 
    319 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    320 
    321 	data = bus_space_read_4(pc->pci_conf_datat, pc->pci_conf_datah,
    322 	    EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg);
    323 #ifdef EM4K_DEBUG_CONF
    324 	aprint_normal("em4k conf read va: %lx, bus: %d, dev: %d, "
    325 	    "func: %d, reg: %d -r-> data %x\n",
    326 	    pc->pci_conf_datah, bus, dev, func, reg, data);
    327 #endif /* EM4K_DEBUG_CONF */
    328 
    329 	return data;
    330 }
    331 
    332 void
    333 em4k_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val)
    334 {
    335 	uint32_t bus, dev, func;
    336 
    337 	if ((unsigned int)reg >= PCI_CONF_SIZE)
    338 		return;
    339 
    340 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    341 
    342 	bus_space_write_4(pc->pci_conf_datat, pc->pci_conf_datah,
    343 	    EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg, val);
    344 #ifdef EM4K_DEBUG_CONF
    345 	aprint_normal("em4k conf write va: %lx, bus: %d, dev: %d, "
    346 	    "func: %d, reg: %d -w-> data %x\n",
    347 	    pc->pci_conf_datah, bus, dev, func, reg, val);
    348 #endif /* EM4K_DEBUG_CONF */
    349 }
    350 
    351 int
    352 em4k_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    353 {
    354 	return 6; /* no Mediator with more than 6 slots? */
    355 }
    356 
    357 void
    358 em4k_pci_attach_hook(device_t parent, device_t self,
    359     struct pcibus_attach_args *pba)
    360 {
    361 }
    362 
    363 int
    364 em4k_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    365 {
    366 	/* TODO: add sanity checking */
    367 
    368 	*ihp = EMPB_INT;
    369 	return 0;
    370 }
    371 
    372 const struct evcnt *
    373 em4k_pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
    374 {
    375 	/* TODO: implement */
    376 	return NULL;
    377 }
    378 
    379 int
    380 em4k_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func,
    381     pcireg_t id)
    382 {
    383 	return PCI_CONF_DEFAULT;
    384 }
    385