Home | History | Annotate | Line # | Download | only in ixdp425
ixdp425_pci.c revision 1.8.12.2
      1  1.8.12.2     tls /*      $NetBSD: ixdp425_pci.c,v 1.8.12.2 2014/08/20 00:02:55 tls Exp $ */
      2       1.2  ichiro #define PCI_DEBUG
      3       1.1  ichiro /*
      4       1.1  ichiro  * Copyright (c) 2003
      5       1.1  ichiro  *      Ichiro FUKUHARA <ichiro (at) ichiro.org>.
      6       1.1  ichiro  * All rights reserved.
      7       1.1  ichiro  *
      8       1.1  ichiro  * Redistribution and use in source and binary forms, with or without
      9       1.1  ichiro  * modification, are permitted provided that the following conditions
     10       1.1  ichiro  * are met:
     11       1.1  ichiro  * 1. Redistributions of source code must retain the above copyright
     12       1.1  ichiro  *    notice, this list of conditions and the following disclaimer.
     13       1.1  ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  ichiro  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  ichiro  *    documentation and/or other materials provided with the distribution.
     16       1.1  ichiro  *
     17       1.1  ichiro  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     18       1.1  ichiro  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19       1.1  ichiro  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20       1.1  ichiro  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     21       1.1  ichiro  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22       1.1  ichiro  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23       1.1  ichiro  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24       1.1  ichiro  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25       1.1  ichiro  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26       1.1  ichiro  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27       1.1  ichiro  * SUCH DAMAGE.
     28       1.1  ichiro  */
     29       1.1  ichiro 
     30       1.1  ichiro #include <sys/cdefs.h>
     31  1.8.12.2     tls __KERNEL_RCSID(0, "$NetBSD: ixdp425_pci.c,v 1.8.12.2 2014/08/20 00:02:55 tls Exp $");
     32       1.1  ichiro 
     33       1.1  ichiro /*
     34       1.1  ichiro  * IXDP425 PCI interrupt support.
     35       1.1  ichiro  */
     36       1.1  ichiro 
     37       1.1  ichiro #include <sys/param.h>
     38       1.1  ichiro #include <sys/systm.h>
     39       1.1  ichiro #include <sys/device.h>
     40       1.1  ichiro 
     41       1.1  ichiro #include <machine/autoconf.h>
     42       1.8  dyoung #include <sys/bus.h>
     43       1.1  ichiro 
     44       1.1  ichiro #include <evbarm/ixdp425/ixdp425reg.h>
     45       1.1  ichiro #include <evbarm/ixdp425/ixdp425var.h>
     46       1.1  ichiro 
     47       1.1  ichiro #include <arm/xscale/ixp425reg.h>
     48       1.1  ichiro #include <arm/xscale/ixp425var.h>
     49       1.1  ichiro 
     50       1.1  ichiro #include <dev/pci/pcidevs.h>
     51       1.1  ichiro #include <dev/pci/ppbreg.h>
     52       1.1  ichiro 
     53       1.7  dyoung static int ixdp425_pci_intr_map(const struct pci_attach_args *,
     54       1.7  dyoung     pci_intr_handle_t *);
     55  1.8.12.2     tls static const char *ixdp425_pci_intr_string(void *, pci_intr_handle_t, char *, size_t);
     56       1.3     scw static const struct evcnt *ixdp425_pci_intr_evcnt(void *, pci_intr_handle_t);
     57       1.3     scw static void *ixdp425_pci_intr_establish(void *, pci_intr_handle_t, int,
     58       1.1  ichiro 	int (*func)(void *), void *);
     59       1.3     scw static void ixdp425_pci_intr_disestablish(void *, void *);
     60       1.1  ichiro 
     61       1.1  ichiro void
     62       1.3     scw ixp425_md_pci_init(struct ixp425_softc *sc)
     63       1.1  ichiro {
     64       1.3     scw 	pci_chipset_tag_t pc = &sc->ia_pci_chipset;
     65  1.8.12.1     tls 	uint32_t reg;
     66       1.3     scw 
     67       1.3     scw 	/*
     68       1.3     scw 	 * PCI initialization
     69       1.3     scw 	 */
     70       1.3     scw 	pc->pc_intr_v = sc;
     71       1.1  ichiro 	pc->pc_intr_map = ixdp425_pci_intr_map;
     72       1.1  ichiro 	pc->pc_intr_string = ixdp425_pci_intr_string;
     73       1.1  ichiro 	pc->pc_intr_evcnt = ixdp425_pci_intr_evcnt;
     74       1.1  ichiro 	pc->pc_intr_establish = ixdp425_pci_intr_establish;
     75       1.1  ichiro 	pc->pc_intr_disestablish = ixdp425_pci_intr_disestablish;
     76       1.3     scw 
     77       1.3     scw 	/* PCI Reset Assert */
     78       1.3     scw 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOUTR);
     79       1.3     scw 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOUTR, reg & ~(1U << GPIO_PCI_RESET));
     80       1.3     scw 
     81       1.3     scw 	/* PCI Clock Disable */
     82       1.3     scw 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
     83       1.3     scw 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg & ~GPCLKR_MUX14);
     84       1.3     scw 
     85       1.3     scw 	/*
     86       1.3     scw 	 * set GPIO Direction
     87       1.3     scw 	 *	Output: PCI_CLK, PCI_RESET
     88       1.3     scw 	 *	Input:  PCI_INTA, PCI_INTB, PCI_INTC, PCI_INTD
     89       1.3     scw 	 */
     90       1.3     scw 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOER);
     91       1.3     scw 	reg &= ~(1U << GPIO_PCI_CLK);
     92       1.3     scw 	reg &= ~(1U << GPIO_PCI_RESET);
     93       1.3     scw 	reg |= ((1U << GPIO_PCI_INTA) | (1U << GPIO_PCI_INTB) |
     94       1.3     scw 		(1U << GPIO_PCI_INTC) | (1U << GPIO_PCI_INTD));
     95       1.3     scw 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOER, reg);
     96       1.3     scw 
     97       1.3     scw 	/* clear ISR */
     98       1.3     scw 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPISR,
     99       1.3     scw 			  (1U << GPIO_PCI_INTA) | (1U << GPIO_PCI_INTB) |
    100       1.3     scw 			  (1U << GPIO_PCI_INTC) | (1U << GPIO_PCI_INTD));
    101       1.3     scw 
    102       1.3     scw 	/* wait 1ms to satisfy "minimum reset assertion time" of the PCI spec */
    103       1.3     scw 	DELAY(1000);
    104       1.3     scw 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
    105       1.3     scw 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg |
    106       1.3     scw 		(0xf << GPCLKR_CLK0DC_SHIFT) | (0xf << GPCLKR_CLK0TC_SHIFT));
    107       1.3     scw 
    108       1.3     scw 	/* PCI Clock Enable */
    109       1.3     scw 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
    110       1.3     scw 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg | GPCLKR_MUX14);
    111       1.3     scw 
    112       1.3     scw 	/*
    113       1.3     scw 	 * wait 100us to satisfy "minimum reset assertion time from clock stable
    114       1.3     scw 	 * requirement of the PCI spec
    115       1.3     scw 	 */
    116       1.3     scw 	DELAY(100);
    117       1.3     scw         /* PCI Reset deassert */
    118       1.3     scw 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOUTR);
    119       1.3     scw 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOUTR, reg | (1U << GPIO_PCI_RESET));
    120       1.3     scw 
    121       1.3     scw 	/*
    122       1.3     scw 	 * AHB->PCI address translation
    123       1.3     scw 	 *	PCI Memory Map allocation in 0x48000000 (64MB)
    124       1.3     scw 	 *	see. IXP425_PCI_MEM_HWBASE
    125       1.3     scw 	 */
    126       1.3     scw 	PCI_CSR_WRITE_4(sc, PCI_PCIMEMBASE, 0x48494a4b);
    127       1.3     scw 
    128       1.3     scw 	/*
    129       1.3     scw 	 * PCI->AHB address translation
    130       1.3     scw 	 * 	begin at the physical memory start + OFFSET
    131       1.3     scw 	 */
    132       1.3     scw #define	AHB_OFFSET	0x10000000UL
    133       1.3     scw 	PCI_CSR_WRITE_4(sc, PCI_AHBMEMBASE,
    134       1.3     scw 			 (AHB_OFFSET & 0xFF000000) +
    135       1.3     scw 			((AHB_OFFSET & 0xFF000000) >> 8) +
    136       1.3     scw 			((AHB_OFFSET & 0xFF000000) >> 16) +
    137       1.3     scw 			((AHB_OFFSET & 0xFF000000) >> 24) +
    138       1.3     scw 			  0x00010203);
    139       1.3     scw 
    140       1.3     scw 	/* write Mapping registers PCI Configuration Registers */
    141       1.3     scw 	/* Base Address 0 - 3 */
    142       1.3     scw 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR0, AHB_OFFSET + 0x00000000);
    143       1.3     scw 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR1, AHB_OFFSET + 0x01000000);
    144       1.3     scw 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR2, AHB_OFFSET + 0x02000000);
    145       1.3     scw 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR3, AHB_OFFSET + 0x03000000);
    146       1.3     scw 
    147       1.3     scw 	/* Base Address 4 */
    148       1.3     scw 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR4, 0xffffffff);
    149       1.3     scw 
    150       1.3     scw 	/* Base Address 5 */
    151       1.3     scw 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR5, 0x00000000);
    152       1.3     scw 
    153       1.3     scw 	/* assert some PCI errors */
    154       1.3     scw 	PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_AHBE | ISR_PPE | ISR_PFE | ISR_PSE);
    155       1.3     scw 
    156       1.3     scw 	/*
    157       1.3     scw 	 * Set up byte lane swapping between little-endian PCI
    158       1.3     scw 	 * and the big-endian AHB bus
    159       1.3     scw 	 */
    160       1.3     scw 	PCI_CSR_WRITE_4(sc, PCI_CSR, CSR_IC | CSR_ABE | CSR_PDS);
    161       1.3     scw 
    162       1.3     scw 	/*
    163       1.3     scw 	 * Enable bus mastering and I/O,memory access
    164       1.3     scw 	 */
    165       1.3     scw 	ixp425_pci_conf_reg_write(sc, PCI_COMMAND_STATUS_REG,
    166       1.3     scw 		PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
    167       1.3     scw 		PCI_COMMAND_MASTER_ENABLE);
    168       1.3     scw }
    169       1.3     scw 
    170       1.3     scw void
    171       1.3     scw ixp425_md_pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
    172       1.3     scw     int swiz, int *ilinep)
    173       1.3     scw {
    174       1.3     scw 
    175       1.3     scw 	if (bus == 0)
    176       1.3     scw 		*ilinep = ((swiz + (dev + pin - 1)) & 3);
    177       1.3     scw 	else
    178       1.3     scw 		panic("ixp425_md_pci_conf_interrupt: unsupported bus number");
    179       1.1  ichiro }
    180       1.1  ichiro 
    181       1.4  ichiro #define	IXP425_MAX_DEV	4
    182       1.4  ichiro #define	IXP425_MAX_LINE	4
    183       1.3     scw static int
    184       1.7  dyoung ixdp425_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    185       1.1  ichiro {
    186       1.4  ichiro 	static int ixp425_pci_table[IXP425_MAX_DEV][IXP425_MAX_LINE] =
    187       1.4  ichiro 	{
    188       1.4  ichiro 		{PCI_INT_A, PCI_INT_B, PCI_INT_C, PCI_INT_D},
    189       1.4  ichiro 		{PCI_INT_B, PCI_INT_C, PCI_INT_D, PCI_INT_A},
    190       1.4  ichiro 		{PCI_INT_C, PCI_INT_D, PCI_INT_A, PCI_INT_B},
    191       1.4  ichiro 		{PCI_INT_D, PCI_INT_A, PCI_INT_B, PCI_INT_C},
    192       1.4  ichiro 	};
    193       1.4  ichiro 
    194       1.1  ichiro 	int pin = pa->pa_intrpin;
    195       1.4  ichiro 	int dev = pa->pa_device;
    196       1.1  ichiro 
    197       1.1  ichiro #ifdef PCI_DEBUG
    198       1.1  ichiro 	void *v = pa->pa_pc;
    199       1.1  ichiro 	int line = pa->pa_intrline;
    200       1.1  ichiro 	pcitag_t intrtag = pa->pa_intrtag;
    201       1.1  ichiro 
    202       1.1  ichiro 	printf("ixdp425_pci_intr_map: v=%p, tag=%08lx intrpin=%d line=%d dev=%d\n",
    203       1.1  ichiro 		v, intrtag, pin, line, dev);
    204       1.1  ichiro #endif
    205       1.2  ichiro 
    206       1.4  ichiro 	if (pin >= 1 && pin <= IXP425_MAX_LINE &&
    207       1.4  ichiro 	    dev >= 1 && dev <= IXP425_MAX_DEV) {
    208       1.4  ichiro 		*ihp = ixp425_pci_table[dev - 1][pin - 1];
    209       1.1  ichiro 		return (0);
    210       1.4  ichiro 	} else {
    211       1.1  ichiro 		printf("ixdp425_pci_intr_map: no mapping for %d/%d/%d\n",
    212       1.1  ichiro 			pa->pa_bus, pa->pa_device, pa->pa_function);
    213       1.1  ichiro 		return (1);
    214       1.1  ichiro 	}
    215       1.1  ichiro }
    216       1.1  ichiro 
    217       1.3     scw static const char *
    218  1.8.12.2     tls ixdp425_pci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
    219       1.1  ichiro {
    220  1.8.12.2     tls 	snprintf(buf, len, "ixp425 irq %ld", ih);
    221  1.8.12.2     tls 	return buf;
    222       1.1  ichiro }
    223       1.1  ichiro 
    224       1.3     scw static const struct evcnt *
    225       1.1  ichiro ixdp425_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
    226       1.1  ichiro {
    227       1.1  ichiro 	return (NULL);
    228       1.1  ichiro }
    229       1.1  ichiro 
    230       1.3     scw static void *
    231       1.1  ichiro ixdp425_pci_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
    232       1.1  ichiro     int (*func)(void *), void *arg)
    233       1.1  ichiro {
    234       1.1  ichiro #ifdef PCI_DEBUG
    235       1.1  ichiro 	printf("ixdp425_pci_intr_establish(v=%p, irq=%d, ipl=%d, func=%p, arg=%p)\n",
    236       1.1  ichiro 		v, (int) ih, ipl, func, arg);
    237       1.1  ichiro #endif
    238       1.1  ichiro 
    239       1.1  ichiro 	return (ixp425_intr_establish(ih, ipl, func, arg));
    240       1.1  ichiro }
    241       1.1  ichiro 
    242       1.3     scw static void
    243       1.1  ichiro ixdp425_pci_intr_disestablish(void *v, void *cookie)
    244       1.1  ichiro {
    245       1.1  ichiro #ifdef PCI_DEBUG
    246       1.1  ichiro 	printf("ixdp425_pci_intr_disestablish(v=%p, cookie=%p)\n",
    247       1.1  ichiro 		v, cookie);
    248       1.1  ichiro #endif
    249       1.1  ichiro 
    250       1.1  ichiro 	ixp425_intr_disestablish(cookie);
    251       1.1  ichiro }
    252