ixdp425_pci.c revision 1.6 1 1.6 rmind /* $NetBSD: ixdp425_pci.c,v 1.6 2009/10/21 14:15:51 rmind 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.6 rmind __KERNEL_RCSID(0, "$NetBSD: ixdp425_pci.c,v 1.6 2009/10/21 14:15:51 rmind 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.1 ichiro #include <machine/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.3 scw static int ixdp425_pci_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
54 1.3 scw static const char *ixdp425_pci_intr_string(void *, pci_intr_handle_t);
55 1.3 scw static const struct evcnt *ixdp425_pci_intr_evcnt(void *, pci_intr_handle_t);
56 1.3 scw static void *ixdp425_pci_intr_establish(void *, pci_intr_handle_t, int,
57 1.1 ichiro int (*func)(void *), void *);
58 1.3 scw static void ixdp425_pci_intr_disestablish(void *, void *);
59 1.1 ichiro
60 1.1 ichiro void
61 1.3 scw ixp425_md_pci_init(struct ixp425_softc *sc)
62 1.1 ichiro {
63 1.3 scw pci_chipset_tag_t pc = &sc->ia_pci_chipset;
64 1.3 scw u_int32_t reg;
65 1.3 scw
66 1.3 scw /*
67 1.3 scw * PCI initialization
68 1.3 scw */
69 1.3 scw pc->pc_intr_v = sc;
70 1.1 ichiro pc->pc_intr_map = ixdp425_pci_intr_map;
71 1.1 ichiro pc->pc_intr_string = ixdp425_pci_intr_string;
72 1.1 ichiro pc->pc_intr_evcnt = ixdp425_pci_intr_evcnt;
73 1.1 ichiro pc->pc_intr_establish = ixdp425_pci_intr_establish;
74 1.1 ichiro pc->pc_intr_disestablish = ixdp425_pci_intr_disestablish;
75 1.3 scw
76 1.3 scw /* PCI Reset Assert */
77 1.3 scw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOUTR);
78 1.3 scw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOUTR, reg & ~(1U << GPIO_PCI_RESET));
79 1.3 scw
80 1.3 scw /* PCI Clock Disable */
81 1.3 scw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
82 1.3 scw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg & ~GPCLKR_MUX14);
83 1.3 scw
84 1.3 scw /*
85 1.3 scw * set GPIO Direction
86 1.3 scw * Output: PCI_CLK, PCI_RESET
87 1.3 scw * Input: PCI_INTA, PCI_INTB, PCI_INTC, PCI_INTD
88 1.3 scw */
89 1.3 scw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOER);
90 1.3 scw reg &= ~(1U << GPIO_PCI_CLK);
91 1.3 scw reg &= ~(1U << GPIO_PCI_RESET);
92 1.3 scw reg |= ((1U << GPIO_PCI_INTA) | (1U << GPIO_PCI_INTB) |
93 1.3 scw (1U << GPIO_PCI_INTC) | (1U << GPIO_PCI_INTD));
94 1.3 scw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOER, reg);
95 1.3 scw
96 1.3 scw /* clear ISR */
97 1.3 scw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPISR,
98 1.3 scw (1U << GPIO_PCI_INTA) | (1U << GPIO_PCI_INTB) |
99 1.3 scw (1U << GPIO_PCI_INTC) | (1U << GPIO_PCI_INTD));
100 1.3 scw
101 1.3 scw /* wait 1ms to satisfy "minimum reset assertion time" of the PCI spec */
102 1.3 scw DELAY(1000);
103 1.3 scw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
104 1.3 scw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg |
105 1.3 scw (0xf << GPCLKR_CLK0DC_SHIFT) | (0xf << GPCLKR_CLK0TC_SHIFT));
106 1.3 scw
107 1.3 scw /* PCI Clock Enable */
108 1.3 scw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPCLKR);
109 1.3 scw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPCLKR, reg | GPCLKR_MUX14);
110 1.3 scw
111 1.3 scw /*
112 1.3 scw * wait 100us to satisfy "minimum reset assertion time from clock stable
113 1.3 scw * requirement of the PCI spec
114 1.3 scw */
115 1.3 scw DELAY(100);
116 1.3 scw /* PCI Reset deassert */
117 1.3 scw reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPOUTR);
118 1.3 scw GPIO_CONF_WRITE_4(sc, IXP425_GPIO_GPOUTR, reg | (1U << GPIO_PCI_RESET));
119 1.3 scw
120 1.3 scw /*
121 1.3 scw * AHB->PCI address translation
122 1.3 scw * PCI Memory Map allocation in 0x48000000 (64MB)
123 1.3 scw * see. IXP425_PCI_MEM_HWBASE
124 1.3 scw */
125 1.3 scw PCI_CSR_WRITE_4(sc, PCI_PCIMEMBASE, 0x48494a4b);
126 1.3 scw
127 1.3 scw /*
128 1.3 scw * PCI->AHB address translation
129 1.3 scw * begin at the physical memory start + OFFSET
130 1.3 scw */
131 1.3 scw #define AHB_OFFSET 0x10000000UL
132 1.3 scw PCI_CSR_WRITE_4(sc, PCI_AHBMEMBASE,
133 1.3 scw (AHB_OFFSET & 0xFF000000) +
134 1.3 scw ((AHB_OFFSET & 0xFF000000) >> 8) +
135 1.3 scw ((AHB_OFFSET & 0xFF000000) >> 16) +
136 1.3 scw ((AHB_OFFSET & 0xFF000000) >> 24) +
137 1.3 scw 0x00010203);
138 1.3 scw
139 1.3 scw /* write Mapping registers PCI Configuration Registers */
140 1.3 scw /* Base Address 0 - 3 */
141 1.3 scw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR0, AHB_OFFSET + 0x00000000);
142 1.3 scw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR1, AHB_OFFSET + 0x01000000);
143 1.3 scw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR2, AHB_OFFSET + 0x02000000);
144 1.3 scw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR3, AHB_OFFSET + 0x03000000);
145 1.3 scw
146 1.3 scw /* Base Address 4 */
147 1.3 scw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR4, 0xffffffff);
148 1.3 scw
149 1.3 scw /* Base Address 5 */
150 1.3 scw ixp425_pci_conf_reg_write(sc, PCI_MAPREG_BAR5, 0x00000000);
151 1.3 scw
152 1.3 scw /* assert some PCI errors */
153 1.3 scw PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_AHBE | ISR_PPE | ISR_PFE | ISR_PSE);
154 1.3 scw
155 1.3 scw /*
156 1.3 scw * Set up byte lane swapping between little-endian PCI
157 1.3 scw * and the big-endian AHB bus
158 1.3 scw */
159 1.3 scw PCI_CSR_WRITE_4(sc, PCI_CSR, CSR_IC | CSR_ABE | CSR_PDS);
160 1.3 scw
161 1.3 scw /*
162 1.3 scw * Enable bus mastering and I/O,memory access
163 1.3 scw */
164 1.3 scw ixp425_pci_conf_reg_write(sc, PCI_COMMAND_STATUS_REG,
165 1.3 scw PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
166 1.3 scw PCI_COMMAND_MASTER_ENABLE);
167 1.3 scw }
168 1.3 scw
169 1.3 scw void
170 1.3 scw ixp425_md_pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
171 1.3 scw int swiz, int *ilinep)
172 1.3 scw {
173 1.3 scw
174 1.3 scw if (bus == 0)
175 1.3 scw *ilinep = ((swiz + (dev + pin - 1)) & 3);
176 1.3 scw else
177 1.3 scw panic("ixp425_md_pci_conf_interrupt: unsupported bus number");
178 1.1 ichiro }
179 1.1 ichiro
180 1.4 ichiro #define IXP425_MAX_DEV 4
181 1.4 ichiro #define IXP425_MAX_LINE 4
182 1.3 scw static int
183 1.1 ichiro ixdp425_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
184 1.1 ichiro {
185 1.4 ichiro static int ixp425_pci_table[IXP425_MAX_DEV][IXP425_MAX_LINE] =
186 1.4 ichiro {
187 1.4 ichiro {PCI_INT_A, PCI_INT_B, PCI_INT_C, PCI_INT_D},
188 1.4 ichiro {PCI_INT_B, PCI_INT_C, PCI_INT_D, PCI_INT_A},
189 1.4 ichiro {PCI_INT_C, PCI_INT_D, PCI_INT_A, PCI_INT_B},
190 1.4 ichiro {PCI_INT_D, PCI_INT_A, PCI_INT_B, PCI_INT_C},
191 1.4 ichiro };
192 1.4 ichiro
193 1.1 ichiro int pin = pa->pa_intrpin;
194 1.4 ichiro int dev = pa->pa_device;
195 1.1 ichiro
196 1.1 ichiro #ifdef PCI_DEBUG
197 1.1 ichiro void *v = pa->pa_pc;
198 1.1 ichiro int line = pa->pa_intrline;
199 1.1 ichiro pcitag_t intrtag = pa->pa_intrtag;
200 1.1 ichiro
201 1.1 ichiro printf("ixdp425_pci_intr_map: v=%p, tag=%08lx intrpin=%d line=%d dev=%d\n",
202 1.1 ichiro v, intrtag, pin, line, dev);
203 1.1 ichiro #endif
204 1.2 ichiro
205 1.4 ichiro if (pin >= 1 && pin <= IXP425_MAX_LINE &&
206 1.4 ichiro dev >= 1 && dev <= IXP425_MAX_DEV) {
207 1.4 ichiro *ihp = ixp425_pci_table[dev - 1][pin - 1];
208 1.1 ichiro return (0);
209 1.4 ichiro } else {
210 1.1 ichiro printf("ixdp425_pci_intr_map: no mapping for %d/%d/%d\n",
211 1.1 ichiro pa->pa_bus, pa->pa_device, pa->pa_function);
212 1.1 ichiro return (1);
213 1.1 ichiro }
214 1.1 ichiro }
215 1.1 ichiro
216 1.3 scw static const char *
217 1.1 ichiro ixdp425_pci_intr_string(void *v, pci_intr_handle_t ih)
218 1.1 ichiro {
219 1.1 ichiro static char irqstr[IRQNAMESIZE];
220 1.1 ichiro
221 1.1 ichiro sprintf(irqstr, "ixp425 irq %ld", ih);
222 1.1 ichiro return (irqstr);
223 1.1 ichiro }
224 1.1 ichiro
225 1.3 scw static const struct evcnt *
226 1.1 ichiro ixdp425_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
227 1.1 ichiro {
228 1.1 ichiro return (NULL);
229 1.1 ichiro }
230 1.1 ichiro
231 1.3 scw static void *
232 1.1 ichiro ixdp425_pci_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
233 1.1 ichiro int (*func)(void *), void *arg)
234 1.1 ichiro {
235 1.1 ichiro #ifdef PCI_DEBUG
236 1.1 ichiro printf("ixdp425_pci_intr_establish(v=%p, irq=%d, ipl=%d, func=%p, arg=%p)\n",
237 1.1 ichiro v, (int) ih, ipl, func, arg);
238 1.1 ichiro #endif
239 1.1 ichiro
240 1.1 ichiro return (ixp425_intr_establish(ih, ipl, func, arg));
241 1.1 ichiro }
242 1.1 ichiro
243 1.3 scw static void
244 1.1 ichiro ixdp425_pci_intr_disestablish(void *v, void *cookie)
245 1.1 ichiro {
246 1.1 ichiro #ifdef PCI_DEBUG
247 1.1 ichiro printf("ixdp425_pci_intr_disestablish(v=%p, cookie=%p)\n",
248 1.1 ichiro v, cookie);
249 1.1 ichiro #endif
250 1.1 ichiro
251 1.1 ichiro ixp425_intr_disestablish(cookie);
252 1.1 ichiro }
253