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