Home | History | Annotate | Line # | Download | only in nslu2
nslu2_pci.c revision 1.1.76.1
      1 /*      $NetBSD: nslu2_pci.c,v 1.1.76.1 2008/06/02 13:22:02 mjf Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Steve C. Woodford.
      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  * Copyright (c) 2003
     33  *      Ichiro FUKUHARA <ichiro (at) ichiro.org>.
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *      This product includes software developed by Ichiro FUKUHARA.
     47  * 4. The name of the company nor the name of the author may be used to
     48  *    endorse or promote products derived from this software without specific
     49  *    prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     52  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     55  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: nslu2_pci.c,v 1.1.76.1 2008/06/02 13:22:02 mjf Exp $");
     66 
     67 /*
     68  * Linksys NSLU2 PCI support.
     69  */
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/device.h>
     74 
     75 #include <arm/xscale/ixp425reg.h>
     76 #include <arm/xscale/ixp425var.h>
     77 
     78 #include <dev/pci/pcivar.h>
     79 
     80 #include <evbarm/nslu2/nslu2reg.h>
     81 
     82 static int
     83 nslu2_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
     84 {
     85 
     86 	KASSERT(pa->pa_bus == 0 && pa->pa_device == 1);
     87 
     88 	switch (pa->pa_function) {
     89 	case 0:
     90 		*ihp = PCI_INT_A;
     91 		break;
     92 
     93 	case 1:
     94 		*ihp = PCI_INT_B;
     95 		break;
     96 
     97 	case 2:
     98 		*ihp = PCI_INT_C;
     99 		break;
    100 
    101 	default:
    102 		return (1);
    103 	}
    104 
    105 	return (0);
    106 }
    107 
    108 static const char *
    109 nslu2_pci_intr_string(void *v, pci_intr_handle_t ih)
    110 {
    111 
    112 	switch (ih) {
    113 	case PCI_INT_A:
    114 		return ("INTA");
    115 
    116 	case PCI_INT_B:
    117 		return ("INTB");
    118 
    119 	case PCI_INT_C:
    120 		return ("INTC");
    121 	}
    122 
    123 	return (NULL);
    124 }
    125 
    126 static const struct evcnt *
    127 nslu2_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
    128 {
    129 
    130 	return (NULL);
    131 }
    132 
    133 static void *
    134 nslu2_pci_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
    135     int (*func)(void *), void *arg)
    136 {
    137 
    138 	return (ixp425_intr_establish(ih, ipl, func, arg));
    139 }
    140 
    141 static void
    142 nslu2_pci_intr_disestablish(void *v, void *cookie)
    143 {
    144 
    145 	ixp425_intr_disestablish(cookie);
    146 }
    147 
    148 void
    149 ixp425_md_pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
    150     int swiz, int *ilinep)
    151 {
    152 
    153 	KASSERT(bus == 0 && dev == 1);
    154 
    155 	*ilinep = ((swiz + pin - 1) & 3);
    156 }
    157 
    158 void
    159 ixp425_md_pci_init(struct ixp425_softc *sc)
    160 {
    161 	pci_chipset_tag_t pc = &sc->ia_pci_chipset;
    162 	u_int32_t reg;
    163 
    164 	pc->pc_intr_v = sc;
    165 	pc->pc_intr_map = nslu2_pci_intr_map;
    166 	pc->pc_intr_string = nslu2_pci_intr_string;
    167 	pc->pc_intr_evcnt = nslu2_pci_intr_evcnt;
    168 	pc->pc_intr_establish = nslu2_pci_intr_establish;
    169 	pc->pc_intr_disestablish = nslu2_pci_intr_disestablish;
    170 
    171 	/* PCI Reset Assert */
    172 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOUTR);
    173 	reg &= ~(1u << GPIO_PCI_RESET);
    174 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOUTR, reg);
    175 
    176 	/* PCI Clock Disable */
    177 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
    178 	reg &= ~GPCLKR_MUX14;
    179 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg);
    180 
    181 	/*
    182 	 * Set GPIO Direction
    183 	 *	Output: PCI_CLK, PCI_RESET
    184 	 *	Input:  PCI_INTA, PCI_INTB, PCI_INTC
    185 	 */
    186 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOER);
    187 	reg &= ~((1u << GPIO_PCI_CLK) | (1u << GPIO_PCI_RESET));
    188 	reg |= (1u << GPIO_PCI_INTA) | (1u << GPIO_PCI_INTB) |
    189 	    (1u << GPIO_PCI_INTC);
    190 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOER, reg);
    191 
    192 	/*
    193 	 * Set GPIO interrupt type
    194 	 *      PCI_INT_A, PCI_INTB, PCI_INT_C: Active Low
    195 	 */
    196 	reg = GPIO_CONF_READ_4(sc, GPIO_TYPE_REG(GPIO_PCI_INTA));
    197 	reg &= ~GPIO_TYPE(GPIO_PCI_INTA, GPIO_TYPE_MASK);
    198 	reg |= GPIO_TYPE(GPIO_PCI_INTA, GPIO_TYPE_ACT_LOW);
    199 	GPIO_CONF_WRITE_4(sc, GPIO_TYPE_REG(GPIO_PCI_INTA), reg);
    200 
    201 	reg = GPIO_CONF_READ_4(sc, GPIO_TYPE_REG(GPIO_PCI_INTB));
    202 	reg &= ~GPIO_TYPE(GPIO_PCI_INTB, GPIO_TYPE_MASK);
    203 	reg |= GPIO_TYPE(GPIO_PCI_INTB, GPIO_TYPE_ACT_LOW);
    204 	GPIO_CONF_WRITE_4(sc, GPIO_TYPE_REG(GPIO_PCI_INTB), reg);
    205 
    206 	reg = GPIO_CONF_READ_4(sc, GPIO_TYPE_REG(GPIO_PCI_INTC));
    207 	reg &= ~GPIO_TYPE(GPIO_PCI_INTC, GPIO_TYPE_MASK);
    208 	reg |= GPIO_TYPE(GPIO_PCI_INTC, GPIO_TYPE_ACT_LOW);
    209 	GPIO_CONF_WRITE_4(sc, GPIO_TYPE_REG(GPIO_PCI_INTC), reg);
    210 
    211 	/* Clear ISR */
    212 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPISR, (1u << GPIO_PCI_INTA) |
    213 	    (1u << GPIO_PCI_INTB) | (1u << GPIO_PCI_INTC));
    214 
    215 	/* Wait 1ms to satisfy "minimum reset assertion time" of the PCI spec */
    216 	DELAY(1000);
    217 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
    218 	reg |= (0xf << GPCLKR_CLK0DC_SHIFT) | (0xf << GPCLKR_CLK0TC_SHIFT);
    219 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg);
    220 
    221 	/* PCI Clock Enable */
    222 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
    223 	reg |= GPCLKR_MUX14;
    224 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg);
    225 
    226 	/*
    227 	 * Wait 100us to satisfy "minimum reset assertion time from clock stable
    228 	 * requirement of the PCI spec
    229 	 */
    230 	DELAY(100);
    231         /* PCI Reset deassert */
    232 	reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOUTR);
    233 	reg |= 1u << GPIO_PCI_RESET;
    234 	GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOUTR, reg);
    235 
    236 	/*
    237 	 * AHB->PCI address translation
    238 	 *	PCI Memory Map allocation in 0x48000000 (64MB)
    239 	 *	see. IXP425_PCI_MEM_HWBASE
    240 	 */
    241 	PCI_CSR_WRITE_4(sc, PCI_PCIMEMBASE, 0x48494a4b);
    242 
    243 	/*
    244 	 * PCI->AHB address translation
    245 	 * 	begin at the physical memory start + OFFSET
    246 	 */
    247 #define	AHB_OFFSET	0x10000000UL
    248 	reg  = (AHB_OFFSET + 0x00000000) >> 0;
    249 	reg |= (AHB_OFFSET + 0x01000000) >> 8;
    250 	reg |= (AHB_OFFSET + 0x02000000) >> 16;
    251 	reg |= (AHB_OFFSET + 0x03000000) >> 24;
    252 	PCI_CSR_WRITE_4(sc, PCI_AHBMEMBASE, reg);
    253 
    254 	/* Write Mapping registers PCI Configuration Registers */
    255 	/* Base Address 0 - 3 */
    256 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR0, AHB_OFFSET + 0x00000000);
    257 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR1, AHB_OFFSET + 0x01000000);
    258 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR2, AHB_OFFSET + 0x02000000);
    259 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR3, AHB_OFFSET + 0x03000000);
    260 
    261 	/* Base Address 4 */
    262 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR4, 0xffffffff);
    263 
    264 	/* Base Address 5 */
    265 	ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR5, 0x00000000);
    266 
    267 	/* Assert some PCI errors */
    268 	PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_AHBE | ISR_PPE | ISR_PFE | ISR_PSE);
    269 
    270 	/*
    271 	 * Set up byte lane swapping between little-endian PCI
    272 	 * and the big-endian AHB bus
    273 	 */
    274 	PCI_CSR_WRITE_4(sc, PCI_CSR, CSR_IC | CSR_ABE | CSR_PDS);
    275 
    276 	/*
    277 	 * Enable bus mastering and I/O,memory access
    278 	 */
    279 	ixp425_pci_conf_reg_write(sc, PCI_COMMAND_STATUS_REG,
    280 		PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
    281 		PCI_COMMAND_MASTER_ENABLE);
    282 
    283 	/*
    284 	 * Wait some more to ensure PCI devices have stabilised.
    285 	 */
    286 	DELAY(50000);
    287 }
    288