Home | History | Annotate | Line # | Download | only in pci
universe_pci.c revision 1.11.22.1
      1  1.11.22.1       tls /* $NetBSD: universe_pci.c,v 1.11.22.1 2014/08/20 00:03:48 tls Exp $ */
      2        1.1  drochner 
      3        1.1  drochner /*
      4        1.1  drochner  * Copyright (c) 1999
      5        1.1  drochner  * 	Matthias Drochner.  All rights reserved.
      6        1.1  drochner  *
      7        1.1  drochner  * Redistribution and use in source and binary forms, with or without
      8        1.1  drochner  * modification, are permitted provided that the following conditions
      9        1.1  drochner  * are met:
     10        1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     11        1.1  drochner  *    notice, this list of conditions, and the following disclaimer.
     12        1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     14        1.1  drochner  *    documentation and/or other materials provided with the distribution.
     15        1.1  drochner  *
     16        1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17        1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18        1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19        1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20        1.1  drochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21        1.1  drochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22        1.1  drochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23        1.1  drochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24        1.1  drochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25        1.1  drochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26        1.1  drochner  * SUCH DAMAGE.
     27        1.1  drochner  */
     28        1.1  drochner 
     29        1.1  drochner /*
     30        1.1  drochner  * Common functions for PCI-VME-interfaces using the
     31        1.1  drochner  * Newbridge/Tundra Universe II chip (CA91C142).
     32        1.1  drochner  */
     33        1.4     lukem 
     34        1.4     lukem #include <sys/cdefs.h>
     35  1.11.22.1       tls __KERNEL_RCSID(0, "$NetBSD: universe_pci.c,v 1.11.22.1 2014/08/20 00:03:48 tls Exp $");
     36        1.1  drochner 
     37        1.1  drochner #include <sys/param.h>
     38        1.1  drochner #include <sys/systm.h>
     39        1.1  drochner #include <sys/device.h>
     40        1.1  drochner 
     41        1.1  drochner #include <dev/pci/pcireg.h>
     42        1.1  drochner #include <dev/pci/pcivar.h>
     43        1.1  drochner /*#include <dev/pci/pcidevs.h>*/
     44        1.1  drochner 
     45        1.8        ad #include <sys/bus.h>
     46        1.1  drochner 
     47        1.1  drochner #include <dev/vme/vmereg.h>
     48        1.1  drochner #include <dev/vme/vmevar.h>
     49        1.1  drochner 
     50        1.1  drochner #include <dev/ic/universereg.h>
     51        1.1  drochner #include <dev/pci/universe_pci_var.h>
     52        1.1  drochner 
     53        1.6     perry int univ_pci_intr(void *);
     54        1.1  drochner 
     55        1.1  drochner #define read_csr_4(d, reg) \
     56        1.1  drochner   bus_space_read_4(d->csrt, d->csrh, offsetof(struct universereg, reg))
     57        1.1  drochner #define write_csr_4(d, reg, val) \
     58        1.1  drochner   bus_space_write_4(d->csrt, d->csrh, offsetof(struct universereg, reg), val)
     59        1.1  drochner 
     60        1.1  drochner #define _pso(i) offsetof(struct universereg, __CONCAT(pcislv, i))
     61        1.1  drochner static int pcislvoffsets[8] = {
     62        1.1  drochner 	_pso(0), _pso(1), _pso(2), _pso(3),
     63        1.1  drochner 	_pso(4), _pso(5), _pso(6), _pso(7)
     64        1.1  drochner };
     65        1.1  drochner #undef _pso
     66        1.1  drochner 
     67        1.1  drochner #define read_pcislv(d, idx, reg) \
     68        1.1  drochner   bus_space_read_4(d->csrt, d->csrh, \
     69        1.1  drochner    pcislvoffsets[idx] + offsetof(struct universe_pcislvimg, reg))
     70        1.1  drochner #define write_pcislv(d, idx, reg, val) \
     71        1.1  drochner   bus_space_write_4(d->csrt, d->csrh, \
     72        1.1  drochner    pcislvoffsets[idx] + offsetof(struct universe_pcislvimg, reg), val)
     73        1.1  drochner 
     74        1.2  drochner 
     75        1.2  drochner #define _vso(i) offsetof(struct universereg, __CONCAT(vmeslv, i))
     76        1.2  drochner static int vmeslvoffsets[8] = {
     77        1.2  drochner 	_vso(0), _vso(1), _vso(2), _vso(3),
     78        1.2  drochner 	_vso(4), _vso(5), _vso(6), _vso(7)
     79        1.2  drochner };
     80        1.2  drochner #undef _vso
     81        1.2  drochner 
     82        1.2  drochner #define read_vmeslv(d, idx, reg) \
     83        1.2  drochner   bus_space_read_4(d->csrt, d->csrh, \
     84        1.2  drochner    vmeslvoffsets[idx] + offsetof(struct universe_vmeslvimg, reg))
     85        1.2  drochner #define write_vmeslv(d, idx, reg, val) \
     86        1.2  drochner   bus_space_write_4(d->csrt, d->csrh, \
     87        1.2  drochner    vmeslvoffsets[idx] + offsetof(struct universe_vmeslvimg, reg), val)
     88        1.2  drochner 
     89        1.1  drochner int
     90       1.10       dsl univ_pci_attach(struct univ_pci_data *d, struct pci_attach_args *pa, const char *name, void (*inthdl)(void *, int, int), void *intcookie)
     91        1.1  drochner {
     92        1.1  drochner 	pci_chipset_tag_t pc = pa->pa_pc;
     93        1.1  drochner 	pci_intr_handle_t ih;
     94        1.1  drochner 	const char *intrstr = NULL;
     95        1.1  drochner 	u_int32_t reg;
     96        1.2  drochner 	int i;
     97  1.11.22.1       tls 	char intrbuf[PCI_INTRSTR_LEN];
     98        1.1  drochner 
     99        1.1  drochner 	d->pc = pc;
    100        1.2  drochner 	strncpy(d->devname, name, sizeof(d->devname));
    101        1.2  drochner 	d->devname[sizeof(d->devname) - 1] = '\0';
    102        1.1  drochner 
    103        1.1  drochner 	if (pci_mapreg_map(pa, 0x10,
    104        1.1  drochner 			   PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    105        1.1  drochner 			   0, &d->csrt, &d->csrh, NULL, NULL) &&
    106        1.1  drochner 	    pci_mapreg_map(pa, 0x14,
    107        1.1  drochner 			   PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    108        1.1  drochner 			   0, &d->csrt, &d->csrh, NULL, NULL) &&
    109        1.1  drochner 	    pci_mapreg_map(pa, 0x10,
    110        1.1  drochner 			   PCI_MAPREG_TYPE_IO,
    111        1.1  drochner 			   0, &d->csrt, &d->csrh, NULL, NULL) &&
    112        1.1  drochner 	    pci_mapreg_map(pa, 0x14,
    113        1.1  drochner 			   PCI_MAPREG_TYPE_IO,
    114        1.1  drochner 			   0, &d->csrt, &d->csrh, NULL, NULL))
    115        1.1  drochner 		return (-1);
    116        1.1  drochner 
    117        1.2  drochner 	/* name sure the chip is in a sane state */
    118        1.2  drochner 	write_csr_4(d, lint_en, 0); /* mask all PCI interrupts */
    119        1.2  drochner 	write_csr_4(d, vint_en, 0); /* mask all VME interrupts */
    120        1.2  drochner 	write_csr_4(d, dgcs, 0x40000000); /* stop DMA activity */
    121        1.2  drochner 	for (i = 0; i < 8; i++) {
    122        1.2  drochner 		univ_pci_unmapvme(d, i);
    123        1.2  drochner 		univ_pci_unmappci(d, i);
    124        1.2  drochner 	}
    125        1.2  drochner 	write_csr_4(d, slsi, 0); /* disable "special PCI slave image" */
    126        1.1  drochner 
    127        1.1  drochner 	/* enable DMA */
    128        1.1  drochner 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    129        1.1  drochner 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    130        1.1  drochner 	    PCI_COMMAND_MASTER_ENABLE);
    131        1.1  drochner 
    132        1.2  drochner 	reg = read_csr_4(d, misc_ctl);
    133        1.5   thorpej 	aprint_normal("%s: ", name);
    134        1.2  drochner 	if (reg & 0x00020000) /* SYSCON */
    135        1.5   thorpej 		aprint_normal("VME bus controller, ");
    136        1.2  drochner 	reg = read_csr_4(d, mast_ctl);
    137        1.5   thorpej 	aprint_normal("requesting at VME bus level %d\n", (reg >> 22) & 3);
    138        1.2  drochner 
    139        1.2  drochner 	/* Map and establish the PCI interrupt. */
    140        1.3  sommerfe 	if (pci_intr_map(pa, &ih)) {
    141        1.5   thorpej 		aprint_error("%s: couldn't map interrupt\n", name);
    142        1.1  drochner 		return (-1);
    143        1.1  drochner 	}
    144  1.11.22.1       tls 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    145        1.1  drochner 	/*
    146        1.1  drochner 	 * Use a low interrupt level (the lowest?).
    147        1.1  drochner 	 * We will raise before calling a subdevice's handler.
    148        1.1  drochner 	 */
    149        1.1  drochner 	d->ih = pci_intr_establish(pc, ih, IPL_BIO, univ_pci_intr, d);
    150        1.1  drochner 	if (d->ih == NULL) {
    151        1.5   thorpej 		aprint_error("%s: couldn't establish interrupt", name);
    152        1.1  drochner 		if (intrstr != NULL)
    153       1.11     njoly 			aprint_error(" at %s", intrstr);
    154       1.11     njoly 		aprint_error("\n");
    155        1.1  drochner 		return (-1);
    156        1.1  drochner 	}
    157        1.5   thorpej 	aprint_normal("%s: interrupting at %s\n", name, intrstr);
    158        1.2  drochner 
    159        1.2  drochner 	/* handle all VME interrupts (XXX should be configurable) */
    160        1.2  drochner 	d->vmeinthandler = inthdl;
    161        1.2  drochner 	d->vmeintcookie = intcookie;
    162        1.2  drochner 	write_csr_4(d, lint_stat, 0x00ff37ff); /* ack all pending IRQs */
    163        1.2  drochner 	write_csr_4(d, lint_en, 0x000000fe); /* enable VME IRQ 1..7 */
    164        1.1  drochner 
    165        1.1  drochner 	return (0);
    166        1.1  drochner }
    167        1.1  drochner 
    168        1.1  drochner int
    169        1.9       dsl univ_pci_mapvme(struct univ_pci_data *d, int wnd, vme_addr_t vmebase, u_int32_t len, vme_am_t am, vme_datasize_t datawidth, u_int32_t pcibase)
    170        1.1  drochner {
    171        1.1  drochner 	u_int32_t ctl = 0x80000000;
    172        1.1  drochner 
    173        1.1  drochner 	switch (am & VME_AM_ADRSIZEMASK) {
    174        1.1  drochner 	case VME_AM_A32:
    175        1.1  drochner 		ctl |= 0x00020000;
    176        1.1  drochner 		break;
    177        1.1  drochner 	case VME_AM_A24:
    178        1.1  drochner 		ctl |= 0x00010000;
    179        1.1  drochner 		break;
    180        1.1  drochner 	case VME_AM_A16:
    181        1.1  drochner 		break;
    182        1.1  drochner 	default:
    183        1.1  drochner 		return (EINVAL);
    184        1.1  drochner 	}
    185        1.1  drochner 	if (am & VME_AM_SUPER)
    186        1.1  drochner 		ctl |= 0x00001000;
    187        1.1  drochner 	if ((am & VME_AM_MODEMASK) == VME_AM_PRG)
    188        1.1  drochner 		ctl |= 0x00004000;
    189        1.1  drochner 	if (datawidth & VME_D32)
    190        1.1  drochner 		ctl |= 0x00800000;
    191        1.1  drochner 	else if (datawidth & VME_D16)
    192        1.1  drochner 		ctl |= 0x00400000;
    193        1.1  drochner 	else if (!(datawidth & VME_D8))
    194        1.1  drochner 		return (EINVAL);
    195        1.1  drochner 
    196        1.1  drochner #ifdef UNIV_DEBUG
    197        1.2  drochner 	printf("%s: wnd %d, map VME %x-%x to %x, ctl=%x\n",
    198        1.2  drochner 	       d->devname, wnd, vmebase, vmebase + len, pcibase, ctl);
    199        1.1  drochner #endif
    200        1.1  drochner 
    201        1.1  drochner 	write_pcislv(d, wnd, lsi_bs, pcibase);
    202        1.1  drochner 	write_pcislv(d, wnd, lsi_bd, pcibase + len);
    203        1.1  drochner 	write_pcislv(d, wnd, lsi_to, vmebase - pcibase);
    204        1.1  drochner 	write_pcislv(d, wnd, lsi_ctl, ctl);
    205        1.1  drochner 	return (0);
    206        1.1  drochner }
    207        1.1  drochner 
    208        1.1  drochner void
    209        1.9       dsl univ_pci_unmapvme(struct univ_pci_data *d, int wnd)
    210        1.1  drochner {
    211        1.1  drochner #ifdef UNIV_DEBUG
    212        1.2  drochner 	printf("%s: unmap VME wnd %d\n", d->devname, wnd);
    213        1.1  drochner #endif
    214        1.1  drochner 	write_pcislv(d, wnd, lsi_ctl, 0);
    215        1.1  drochner }
    216        1.1  drochner 
    217        1.2  drochner 
    218        1.2  drochner int
    219        1.9       dsl univ_pci_mappci(struct univ_pci_data *d, int wnd, u_int32_t pcibase, u_int32_t len, vme_addr_t vmebase, vme_am_t am)
    220        1.2  drochner {
    221        1.2  drochner 	u_int32_t ctl = 0x80000000;
    222        1.2  drochner 
    223        1.2  drochner 	switch (am & VME_AM_ADRSIZEMASK) {
    224        1.2  drochner 	case VME_AM_A32:
    225        1.2  drochner 		ctl |= 0x00020000;
    226        1.2  drochner 		break;
    227        1.2  drochner 	case VME_AM_A24:
    228        1.2  drochner 		ctl |= 0x00010000;
    229        1.2  drochner 		break;
    230        1.2  drochner 	case VME_AM_A16:
    231        1.2  drochner 		break;
    232        1.2  drochner 	default:
    233        1.2  drochner 		return (EINVAL);
    234        1.2  drochner 	}
    235        1.2  drochner 	if (am & VME_AM_SUPER)
    236        1.2  drochner 		ctl |= 0x00200000;
    237        1.2  drochner 	else
    238        1.2  drochner 		ctl |= 0x00300000; /* both */
    239        1.2  drochner 	if ((am & VME_AM_MODEMASK) == VME_AM_PRG)
    240        1.2  drochner 		ctl |= 0x00800000;
    241        1.2  drochner 	else
    242        1.2  drochner 		ctl |= 0x00c00000; /* both */
    243        1.2  drochner 
    244        1.2  drochner #ifdef UNIV_DEBUG
    245        1.2  drochner 	printf("%s: wnd %d, map PCI %x-%x to %x, ctl=%x\n",
    246        1.2  drochner 	       d->devname, wnd, pcibase, pcibase + len, vmebase, ctl);
    247        1.2  drochner #endif
    248        1.2  drochner 
    249        1.2  drochner 	write_vmeslv(d, wnd, vsi_bs, vmebase);
    250        1.2  drochner 	write_vmeslv(d, wnd, vsi_bd, vmebase + len);
    251        1.2  drochner 	write_vmeslv(d, wnd, vsi_to, pcibase - vmebase);
    252        1.2  drochner 	write_vmeslv(d, wnd, vsi_ctl, ctl);
    253        1.2  drochner 	return (0);
    254        1.2  drochner }
    255        1.2  drochner 
    256        1.2  drochner void
    257        1.9       dsl univ_pci_unmappci(struct univ_pci_data *d, int wnd)
    258        1.2  drochner {
    259        1.2  drochner #ifdef UNIV_DEBUG
    260        1.2  drochner 	printf("%s: unmap PCI wnd %d\n", d->devname, wnd);
    261        1.2  drochner #endif
    262        1.2  drochner 	write_vmeslv(d, wnd, vsi_ctl, 0);
    263        1.2  drochner }
    264        1.2  drochner 
    265        1.2  drochner int
    266        1.9       dsl univ_pci_vmebuserr(struct univ_pci_data *d, int clear)
    267        1.2  drochner {
    268        1.2  drochner 	u_int32_t pcicsr;
    269        1.2  drochner 
    270        1.2  drochner 	pcicsr = read_csr_4(d, pci_csr);
    271        1.2  drochner 	if ((pcicsr & 0xf8000000) && clear)
    272        1.2  drochner 		write_csr_4(d, pci_csr, pcicsr | 0xf8000000);
    273        1.2  drochner 	return (pcicsr & 0x08000000); /* target abort */
    274        1.2  drochner }
    275        1.2  drochner 
    276        1.1  drochner int
    277        1.9       dsl univ_pci_intr(void *v)
    278        1.1  drochner {
    279        1.1  drochner 	struct univ_pci_data *d = v;
    280        1.1  drochner 	u_int32_t intcsr;
    281        1.2  drochner 	int i, vec;
    282        1.1  drochner 
    283        1.1  drochner 	intcsr = read_csr_4(d, lint_stat) & 0xffffff;
    284        1.1  drochner 	if (!intcsr)
    285        1.1  drochner 		return (0);
    286        1.1  drochner 
    287        1.1  drochner 	/* ack everything */
    288        1.1  drochner 	write_csr_4(d, lint_stat, intcsr);
    289        1.2  drochner #ifdef UNIV_DEBUG
    290        1.2  drochner 	printf("%s: intr, lint_stat=%x\n", d->devname, intcsr);
    291        1.2  drochner #endif
    292        1.2  drochner 	if (intcsr & 0x000000fe) { /* VME interrupt */
    293        1.2  drochner 		for (i = 7; i >= 1; i--) {
    294        1.2  drochner 			if (!(intcsr & (1 << i)))
    295        1.2  drochner 				continue;
    296        1.2  drochner 			vec = read_csr_4(d, v_statid[i - 1]);
    297        1.2  drochner 			if (vec & 0x100) {
    298        1.2  drochner 				printf("%s: err irq %d\n", d->devname, i);
    299        1.2  drochner 				continue;
    300        1.2  drochner 			}
    301        1.2  drochner 			if (d->vmeinthandler)
    302        1.2  drochner 				(*d->vmeinthandler)(d->vmeintcookie, i, vec);
    303        1.2  drochner 		}
    304        1.2  drochner 	}
    305        1.1  drochner 
    306        1.1  drochner 	return (1);
    307        1.1  drochner }
    308