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