Home | History | Annotate | Line # | Download | only in footbridge
footbridge_pci.c revision 1.5
      1  1.5  thorpej /*	$NetBSD: footbridge_pci.c,v 1.5 2002/08/17 20:46:26 thorpej Exp $	*/
      2  1.1    chris 
      3  1.1    chris /*
      4  1.1    chris  * Copyright (c) 1997,1998 Mark Brinicombe.
      5  1.1    chris  * Copyright (c) 1997,1998 Causality Limited
      6  1.1    chris  * All rights reserved.
      7  1.1    chris  *
      8  1.1    chris  * Redistribution and use in source and binary forms, with or without
      9  1.1    chris  * modification, are permitted provided that the following conditions
     10  1.1    chris  * are met:
     11  1.1    chris  * 1. Redistributions of source code must retain the above copyright
     12  1.1    chris  *    notice, this list of conditions and the following disclaimer.
     13  1.1    chris  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1    chris  *    notice, this list of conditions and the following disclaimer in the
     15  1.1    chris  *    documentation and/or other materials provided with the distribution.
     16  1.1    chris  * 3. All advertising materials mentioning features or use of this software
     17  1.1    chris  *    must display the following acknowledgement:
     18  1.1    chris  *	This product includes software developed by Mark Brinicombe
     19  1.1    chris  *	for the NetBSD Project.
     20  1.1    chris  * 4. The name of the company nor the name of the author may be used to
     21  1.1    chris  *    endorse or promote products derived from this software without specific
     22  1.1    chris  *    prior written permission.
     23  1.1    chris  *
     24  1.1    chris  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     25  1.1    chris  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     26  1.1    chris  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  1.1    chris  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     28  1.1    chris  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     29  1.1    chris  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     30  1.1    chris  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1    chris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1    chris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1    chris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1    chris  * SUCH DAMAGE.
     35  1.1    chris  */
     36  1.1    chris 
     37  1.1    chris #include <sys/param.h>
     38  1.1    chris #include <sys/systm.h>
     39  1.1    chris #include <sys/conf.h>
     40  1.1    chris #include <sys/malloc.h>
     41  1.1    chris #include <sys/device.h>
     42  1.1    chris 
     43  1.1    chris #define _ARM32_BUS_DMA_PRIVATE
     44  1.1    chris #include <machine/bus.h>
     45  1.4     matt #include <machine/intr.h>
     46  1.1    chris 
     47  1.1    chris #include <dev/pci/pcireg.h>
     48  1.1    chris #include <dev/pci/pcivar.h>
     49  1.1    chris 
     50  1.1    chris #include <arm/footbridge/dc21285reg.h>
     51  1.1    chris #include <arm/footbridge/dc21285mem.h>
     52  1.1    chris 
     53  1.1    chris #include "isa.h"
     54  1.1    chris #if NISA > 0
     55  1.1    chris #include <dev/isa/isavar.h>
     56  1.1    chris #endif
     57  1.1    chris 
     58  1.2     matt #ifdef netwinder
     59  1.1    chris void		netwinder_pci_attach_hook __P((struct device *,
     60  1.1    chris 		    struct device *, struct pcibus_attach_args *));
     61  1.1    chris #endif
     62  1.1    chris void		footbridge_pci_attach_hook __P((struct device *,
     63  1.1    chris 		    struct device *, struct pcibus_attach_args *));
     64  1.1    chris int		footbridge_pci_bus_maxdevs __P((void *, int));
     65  1.1    chris pcitag_t	footbridge_pci_make_tag __P((void *, int, int, int));
     66  1.1    chris void		footbridge_pci_decompose_tag __P((void *, pcitag_t, int *,
     67  1.1    chris 		    int *, int *));
     68  1.1    chris pcireg_t	footbridge_pci_conf_read __P((void *, pcitag_t, int));
     69  1.1    chris void		footbridge_pci_conf_write __P((void *, pcitag_t, int,
     70  1.1    chris 		    pcireg_t));
     71  1.1    chris int		footbridge_pci_intr_map __P((struct pci_attach_args *,
     72  1.1    chris 		    pci_intr_handle_t *));
     73  1.1    chris const char	*footbridge_pci_intr_string __P((void *, pci_intr_handle_t));
     74  1.1    chris const struct evcnt *footbridge_pci_intr_evcnt __P((void *, pci_intr_handle_t));
     75  1.1    chris void		*footbridge_pci_intr_establish __P((void *, pci_intr_handle_t,
     76  1.1    chris 		    int, int (*)(void *), void *));
     77  1.1    chris void		footbridge_pci_intr_disestablish __P((void *, void *));
     78  1.1    chris 
     79  1.1    chris 
     80  1.1    chris struct arm32_pci_chipset footbridge_pci_chipset = {
     81  1.1    chris 	NULL,	/* conf_v */
     82  1.2     matt #ifdef netwinder
     83  1.1    chris 	netwinder_pci_attach_hook,
     84  1.1    chris #else
     85  1.1    chris 	footbridge_pci_attach_hook,
     86  1.1    chris #endif
     87  1.1    chris 	footbridge_pci_bus_maxdevs,
     88  1.1    chris 	footbridge_pci_make_tag,
     89  1.1    chris 	footbridge_pci_decompose_tag,
     90  1.1    chris 	footbridge_pci_conf_read,
     91  1.1    chris 	footbridge_pci_conf_write,
     92  1.1    chris 	NULL,	/* intr_v */
     93  1.1    chris 	footbridge_pci_intr_map,
     94  1.1    chris 	footbridge_pci_intr_string,
     95  1.1    chris 	footbridge_pci_intr_evcnt,
     96  1.1    chris 	footbridge_pci_intr_establish,
     97  1.1    chris 	footbridge_pci_intr_disestablish
     98  1.1    chris };
     99  1.1    chris 
    100  1.1    chris /*
    101  1.1    chris  * PCI doesn't have any special needs; just use the generic versions
    102  1.1    chris  * of these functions.
    103  1.1    chris  */
    104  1.1    chris struct arm32_bus_dma_tag footbridge_pci_bus_dma_tag = {
    105  1.1    chris 	0,
    106  1.1    chris 	0,
    107  1.1    chris 	_bus_dmamap_create,
    108  1.1    chris 	_bus_dmamap_destroy,
    109  1.1    chris 	_bus_dmamap_load,
    110  1.1    chris 	_bus_dmamap_load_mbuf,
    111  1.1    chris 	_bus_dmamap_load_uio,
    112  1.1    chris 	_bus_dmamap_load_raw,
    113  1.1    chris 	_bus_dmamap_unload,
    114  1.5  thorpej 	_bus_dmamap_sync,	/* pre */
    115  1.5  thorpej 	NULL,			/* post */
    116  1.1    chris 	_bus_dmamem_alloc,
    117  1.1    chris 	_bus_dmamem_free,
    118  1.1    chris 	_bus_dmamem_map,
    119  1.1    chris 	_bus_dmamem_unmap,
    120  1.1    chris 	_bus_dmamem_mmap,
    121  1.1    chris };
    122  1.1    chris 
    123  1.1    chris /*
    124  1.1    chris  * Currently we only support 12 devices as we select directly in the
    125  1.1    chris  * type 0 config cycle
    126  1.1    chris  * (See conf_{read,write} for more detail
    127  1.1    chris  */
    128  1.1    chris #define MAX_PCI_DEVICES	21
    129  1.1    chris 
    130  1.1    chris /*static int
    131  1.1    chris pci_intr(void *arg)
    132  1.1    chris {
    133  1.1    chris 	printf("pci int %x\n", (int)arg);
    134  1.1    chris 	return(0);
    135  1.1    chris }*/
    136  1.1    chris 
    137  1.1    chris 
    138  1.1    chris void
    139  1.1    chris footbridge_pci_attach_hook(parent, self, pba)
    140  1.1    chris 	struct device *parent, *self;
    141  1.1    chris 	struct pcibus_attach_args *pba;
    142  1.1    chris {
    143  1.1    chris #ifdef PCI_DEBUG
    144  1.1    chris 	printf("footbridge_pci_attach_hook()\n");
    145  1.1    chris #endif
    146  1.1    chris 
    147  1.1    chris /*	intr_claim(18, IPL_NONE, "pci int 0", pci_intr, (void *)0x10000);
    148  1.1    chris 	intr_claim(8, IPL_NONE, "pci int 1", pci_intr, (void *)0x10001);
    149  1.1    chris 	intr_claim(9, IPL_NONE, "pci int 2", pci_intr, (void *)0x10002);
    150  1.1    chris 	intr_claim(11, IPL_NONE, "pci int 3", pci_intr, (void *)0x10003);*/
    151  1.1    chris }
    152  1.1    chris 
    153  1.1    chris int
    154  1.1    chris footbridge_pci_bus_maxdevs(pcv, busno)
    155  1.1    chris 	void *pcv;
    156  1.1    chris 	int busno;
    157  1.1    chris {
    158  1.1    chris #ifdef PCI_DEBUG
    159  1.1    chris 	printf("footbridge_pci_bus_maxdevs(pcv=%p, busno=%d)\n", pcv, busno);
    160  1.1    chris #endif
    161  1.1    chris 	return(MAX_PCI_DEVICES);
    162  1.1    chris }
    163  1.1    chris 
    164  1.1    chris pcitag_t
    165  1.1    chris footbridge_pci_make_tag(pcv, bus, device, function)
    166  1.1    chris 	void *pcv;
    167  1.1    chris 	int bus, device, function;
    168  1.1    chris {
    169  1.1    chris #ifdef PCI_DEBUG
    170  1.1    chris 	printf("footbridge_pci_make_tag(pcv=%p, bus=%d, device=%d, function=%d)\n",
    171  1.1    chris 	    pcv, bus, device, function);
    172  1.1    chris #endif
    173  1.1    chris 	return ((bus << 16) | (device << 11) | (function << 8));
    174  1.1    chris }
    175  1.1    chris 
    176  1.1    chris void
    177  1.1    chris footbridge_pci_decompose_tag(pcv, tag, busp, devicep, functionp)
    178  1.1    chris 	void *pcv;
    179  1.1    chris 	pcitag_t tag;
    180  1.1    chris 	int *busp, *devicep, *functionp;
    181  1.1    chris {
    182  1.1    chris #ifdef PCI_DEBUG
    183  1.1    chris 	printf("footbridge_pci_decompose_tag(pcv=%p, tag=0x%08x, bp=%x, dp=%x, fp=%x)\n",
    184  1.1    chris 	    pcv, tag, busp, devicep, functionp);
    185  1.1    chris #endif
    186  1.1    chris 
    187  1.1    chris 	if (busp != NULL)
    188  1.1    chris 		*busp = (tag >> 16) & 0xff;
    189  1.1    chris 	if (devicep != NULL)
    190  1.1    chris 		*devicep = (tag >> 11) & 0x1f;
    191  1.1    chris 	if (functionp != NULL)
    192  1.1    chris 		*functionp = (tag >> 8) & 0x7;
    193  1.1    chris }
    194  1.1    chris 
    195  1.1    chris pcireg_t
    196  1.1    chris footbridge_pci_conf_read(pcv, tag, reg)
    197  1.1    chris 	void *pcv;
    198  1.1    chris 	pcitag_t tag;
    199  1.1    chris 	int reg;
    200  1.1    chris {
    201  1.1    chris 	int bus, device, function;
    202  1.1    chris 	u_int address;
    203  1.1    chris 	pcireg_t data;
    204  1.1    chris 
    205  1.1    chris 	footbridge_pci_decompose_tag(pcv, tag, &bus, &device, &function);
    206  1.1    chris 	if (bus == 0)
    207  1.1    chris 		/* Limited to 12 devices or we exceed type 0 config space */
    208  1.1    chris 		address = DC21285_PCI_TYPE_0_CONFIG_VBASE | (3 << 22) | (device << 11);
    209  1.1    chris 	else
    210  1.1    chris 		address = DC21285_PCI_TYPE_1_CONFIG_VBASE | (device << 11) |
    211  1.1    chris 		    (bus << 16);
    212  1.1    chris 
    213  1.1    chris 	address |= (function << 8) | reg;
    214  1.1    chris 
    215  1.1    chris 	data = *((unsigned int *)address);
    216  1.1    chris #ifdef PCI_DEBUG
    217  1.1    chris 	printf("footbridge_pci_conf_read(pcv=%p tag=0x%08x reg=0x%02x)=0x%08x\n",
    218  1.1    chris 	    pcv, tag, reg, data);
    219  1.1    chris #endif
    220  1.1    chris 	return(data);
    221  1.1    chris }
    222  1.1    chris 
    223  1.1    chris void
    224  1.1    chris footbridge_pci_conf_write(pcv, tag, reg, data)
    225  1.1    chris 	void *pcv;
    226  1.1    chris 	pcitag_t tag;
    227  1.1    chris 	int reg;
    228  1.1    chris 	pcireg_t data;
    229  1.1    chris {
    230  1.1    chris 	int bus, device, function;
    231  1.1    chris 	u_int address;
    232  1.1    chris 
    233  1.1    chris 	footbridge_pci_decompose_tag(pcv, tag, &bus, &device, &function);
    234  1.1    chris 	if (bus == 0)
    235  1.1    chris 		address = DC21285_PCI_TYPE_0_CONFIG_VBASE | (3 << 22) | (device << 11);
    236  1.1    chris 	else
    237  1.1    chris 		address = DC21285_PCI_TYPE_1_CONFIG_VBASE | (device << 11) |
    238  1.1    chris 		    (bus << 16);
    239  1.1    chris 
    240  1.1    chris 	address |= (function << 8) | reg;
    241  1.1    chris 
    242  1.1    chris #ifdef PCI_DEBUG
    243  1.1    chris 	printf("footbridge_pci_conf_write(pcv=%p tag=0x%08x reg=0x%02x, 0x%08x)\n",
    244  1.1    chris 	    pcv, tag, reg, data);
    245  1.1    chris #endif
    246  1.1    chris 
    247  1.1    chris 	*((unsigned int *)address) = data;
    248  1.1    chris }
    249  1.1    chris 
    250  1.1    chris int
    251  1.1    chris footbridge_pci_intr_map(pa, ihp)
    252  1.1    chris 	struct pci_attach_args *pa;
    253  1.1    chris 	pci_intr_handle_t *ihp;
    254  1.1    chris {
    255  1.1    chris 	int pin = pa->pa_intrpin, line = pa->pa_intrline;
    256  1.1    chris 	int intr = -1;
    257  1.1    chris 
    258  1.1    chris #ifdef PCI_DEBUG
    259  1.1    chris 	void *pcv = pa->pa_pc;
    260  1.1    chris 	pcitag_t intrtag = pa->pa_intrtag;
    261  1.1    chris 	int bus, device, function;
    262  1.1    chris 
    263  1.1    chris 	footbridge_pci_decompose_tag(pcv, intrtag, &bus, &device, &function);
    264  1.1    chris 	printf("footbride_pci_intr_map: pcv=%p, tag=%08lx pin=%d line=%d dev=%d\n",
    265  1.1    chris 	    pcv, intrtag, pin, line, device);
    266  1.1    chris #endif
    267  1.1    chris 
    268  1.1    chris 	/*
    269  1.1    chris 	 * Only the line is used to map the interrupt.
    270  1.1    chris 	 * The firmware is expected to setup up the interrupt
    271  1.1    chris 	 * line as seen from the CPU
    272  1.1    chris 	 * This means the firmware deals with the interrupt rotation
    273  1.1    chris 	 * between slots etc.
    274  1.1    chris 	 *
    275  1.1    chris 	 * Perhaps the firmware should also to the final mapping
    276  1.1    chris 	 * to a 21285 interrupt bit so the code below would be
    277  1.1    chris 	 * completely MI.
    278  1.1    chris 	 */
    279  1.1    chris 
    280  1.1    chris 	switch (line) {
    281  1.1    chris 	case PCI_INTERRUPT_PIN_NONE:
    282  1.1    chris 	case 0xff:
    283  1.1    chris 		/* No IRQ */
    284  1.1    chris 		printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
    285  1.1    chris 		*ihp = -1;
    286  1.1    chris 		return(1);
    287  1.1    chris 		break;
    288  1.3    chris #ifdef cats
    289  1.1    chris 	/* This is machine dependant and needs to be moved */
    290  1.1    chris 	case PCI_INTERRUPT_PIN_A:
    291  1.1    chris 		intr = IRQ_PCI;
    292  1.1    chris 		break;
    293  1.1    chris 	case PCI_INTERRUPT_PIN_B:
    294  1.1    chris 		intr = IRQ_IN_L0;
    295  1.1    chris 		break;
    296  1.1    chris 	case PCI_INTERRUPT_PIN_C:
    297  1.1    chris 		intr = IRQ_IN_L1;
    298  1.1    chris 		break;
    299  1.1    chris 	case PCI_INTERRUPT_PIN_D:
    300  1.1    chris 		intr = IRQ_IN_L3;
    301  1.1    chris 		break;
    302  1.1    chris #endif
    303  1.1    chris 	default:
    304  1.1    chris 		/*
    305  1.1    chris 		 * Experimental firmware feature ...
    306  1.1    chris 		 *
    307  1.1    chris 		 * If the interrupt line is in the range 0x80 to 0x8F
    308  1.1    chris 		 * then the lower 4 bits indicate the ISA interrupt
    309  1.1    chris 		 * bit that should be used.
    310  1.1    chris 		 * If the interrupt line is in the range 0x40 to 0x5F
    311  1.1    chris 		 * then the lower 5 bits indicate the actual DC21285
    312  1.1    chris 		 * interrupt bit that should be used.
    313  1.1    chris 		 */
    314  1.1    chris 
    315  1.1    chris 		if (line >= 0x40 && line <= 0x5f)
    316  1.1    chris 			intr = line & 0x1f;
    317  1.1    chris 		else if (line >= 0x80 && line <= 0x8f)
    318  1.1    chris 			intr = line;
    319  1.1    chris 		else {
    320  1.1    chris 	                printf("footbridge_pci_intr_map: out of range interrupt"
    321  1.1    chris 			       "pin %d line %d (%#x)\n", pin, line, line);
    322  1.1    chris 			*ihp = -1;
    323  1.1    chris 			return(1);
    324  1.1    chris 		}
    325  1.1    chris 		break;
    326  1.1    chris 	}
    327  1.1    chris 
    328  1.1    chris #ifdef PCI_DEBUG
    329  1.1    chris 	printf("pin %d, line %d mapped to int %d\n", pin, line, intr);
    330  1.1    chris #endif
    331  1.1    chris 
    332  1.1    chris 	*ihp = intr;
    333  1.1    chris 	return(0);
    334  1.1    chris }
    335  1.1    chris 
    336  1.1    chris const char *
    337  1.1    chris footbridge_pci_intr_string(pcv, ih)
    338  1.1    chris 	void *pcv;
    339  1.1    chris 	pci_intr_handle_t ih;
    340  1.1    chris {
    341  1.1    chris 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
    342  1.1    chris 
    343  1.1    chris #ifdef PCI_DEBUG
    344  1.1    chris 	printf("footbridge_pci_intr_string(pcv=0x%p, ih=0x%lx)\n", pcv, ih);
    345  1.1    chris #endif
    346  1.1    chris 	if (ih == 0)
    347  1.1    chris 		panic("footbridge_pci_intr_string: bogus handle 0x%lx\n", ih);
    348  1.1    chris 
    349  1.1    chris #if NISA > 0
    350  1.1    chris 	if (ih >= 0x80 && ih <= 0x8f) {
    351  1.1    chris 		sprintf(irqstr, "isairq %ld", (ih & 0x0f));
    352  1.1    chris 		return(irqstr);
    353  1.1    chris 	}
    354  1.1    chris #endif
    355  1.1    chris 	sprintf(irqstr, "irq %ld", ih);
    356  1.1    chris 	return(irqstr);
    357  1.1    chris }
    358  1.1    chris 
    359  1.1    chris const struct evcnt *
    360  1.1    chris footbridge_pci_intr_evcnt(pcv, ih)
    361  1.1    chris 	void *pcv;
    362  1.1    chris 	pci_intr_handle_t ih;
    363  1.1    chris {
    364  1.1    chris 
    365  1.1    chris 	/* XXX for now, no evcnt parent reported */
    366  1.1    chris 	return NULL;
    367  1.1    chris }
    368  1.1    chris 
    369  1.1    chris void *
    370  1.1    chris footbridge_pci_intr_establish(pcv, ih, level, func, arg)
    371  1.1    chris 	void *pcv;
    372  1.1    chris 	pci_intr_handle_t ih;
    373  1.1    chris 	int level, (*func) __P((void *));
    374  1.1    chris 	void *arg;
    375  1.1    chris {
    376  1.1    chris 	void *intr;
    377  1.1    chris 	int length;
    378  1.1    chris 	char *string;
    379  1.1    chris 
    380  1.1    chris #ifdef PCI_DEBUG
    381  1.1    chris 	printf("footbridge_pci_intr_establish(pcv=%p, ih=0x%lx, level=%d, func=%p, arg=%p)\n",
    382  1.1    chris 	    pcv, ih, level, func, arg);
    383  1.1    chris #endif
    384  1.1    chris 
    385  1.1    chris 	/* Copy the interrupt string to a private buffer */
    386  1.1    chris 	length = strlen(footbridge_pci_intr_string(pcv, ih));
    387  1.1    chris 	string = malloc(length + 1, M_DEVBUF, M_WAITOK);
    388  1.1    chris 	strcpy(string, footbridge_pci_intr_string(pcv, ih));
    389  1.1    chris #if NISA > 0
    390  1.1    chris 	/*
    391  1.1    chris 	 * XXX the IDE driver will attach the interrupts in compat mode and
    392  1.1    chris 	 * thus we need to fail this here.
    393  1.1    chris 	 * This assumes that the interrupts are 14 and 15 which they are for
    394  1.1    chris 	 * IDE compat mode.
    395  1.1    chris 	 * Really the firmware should make this clear in the interrupt reg.
    396  1.1    chris 	 */
    397  1.1    chris 	if (ih >= 0x80 && ih <= 0x8d) {
    398  1.1    chris 		intr = isa_intr_establish(NULL, (ih & 0x0f), IST_EDGE,
    399  1.1    chris 		    level, func, arg);
    400  1.1    chris 	} else
    401  1.1    chris #endif
    402  1.1    chris 	intr = intr_claim(ih, level, string, func, arg);
    403  1.1    chris 
    404  1.1    chris 	return(intr);
    405  1.1    chris }
    406  1.1    chris 
    407  1.1    chris void
    408  1.1    chris footbridge_pci_intr_disestablish(pcv, cookie)
    409  1.1    chris 	void *pcv;
    410  1.1    chris 	void *cookie;
    411  1.1    chris {
    412  1.1    chris #ifdef PCI_DEBUG
    413  1.1    chris 	printf("footbridge_pci_intr_disestablish(pcv=%p, cookie=0x%x)\n",
    414  1.1    chris 	    pcv, cookie);
    415  1.1    chris #endif
    416  1.1    chris 	/* XXXX Need to free the string */
    417  1.1    chris 
    418  1.1    chris 	intr_release(cookie);
    419  1.1    chris }
    420