]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/xscale/ixp425/ixp425_pci.c
Upgrade to OpenSSH 7.6p1. This will be followed shortly by 7.7p1.
[FreeBSD/FreeBSD.git] / sys / arm / xscale / ixp425 / ixp425_pci.c
1 /*      $NetBSD: ixp425_pci.c,v 1.6 2009/10/21 14:15:50 rmind Exp $ */
2
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2003
7  *      Ichiro FUKUHARA <ichiro@ichiro.org>.
8  * All rights reserved.
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 ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #define _ARM32_BUS_DMA_PRIVATE
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/rman.h>
43
44 #include <dev/pci/pcivar.h>
45
46 #include <machine/armreg.h>
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49 #include <machine/pcb.h>
50
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 #include <vm/vm_extern.h>
54
55 #include <arm/xscale/ixp425/ixp425reg.h>
56 #include <arm/xscale/ixp425/ixp425var.h>
57
58 #include <dev/pci/pcib_private.h>
59 #include "pcib_if.h"
60
61 #include <dev/pci/pcireg.h>
62 extern struct ixp425_softc *ixp425_softc;
63
64 #define PCI_CSR_WRITE_4(sc, reg, data)  \
65         bus_write_4(sc->sc_csr, reg, data)
66
67 #define PCI_CSR_READ_4(sc, reg) \
68         bus_read_4(sc->sc_csr, reg)
69
70 #define PCI_CONF_LOCK(s)        (s) = disable_interrupts(PSR_I)
71 #define PCI_CONF_UNLOCK(s)      restore_interrupts((s))
72
73 static device_probe_t ixppcib_probe;
74 static device_attach_t ixppcib_attach;
75 static bus_read_ivar_t ixppcib_read_ivar;
76 static bus_write_ivar_t ixppcib_write_ivar;
77 static bus_setup_intr_t ixppcib_setup_intr;
78 static bus_teardown_intr_t ixppcib_teardown_intr;
79 static bus_alloc_resource_t ixppcib_alloc_resource;
80 static bus_activate_resource_t ixppcib_activate_resource;
81 static bus_deactivate_resource_t ixppcib_deactivate_resource;
82 static bus_release_resource_t ixppcib_release_resource;
83 static pcib_maxslots_t ixppcib_maxslots;
84 static pcib_read_config_t ixppcib_read_config;
85 static pcib_write_config_t ixppcib_write_config;
86 static pcib_route_interrupt_t ixppcib_route_interrupt;
87
88 static int
89 ixppcib_probe(device_t dev)
90 {
91         device_set_desc(dev, "IXP4XX PCI Bus");
92         return (0);
93 }
94
95 static void
96 ixp425_pci_conf_reg_write(struct ixppcib_softc *sc, uint32_t reg,
97     uint32_t data)
98 {
99         PCI_CSR_WRITE_4(sc, PCI_CRP_AD_CBE, ((reg & ~3) | COMMAND_CRP_WRITE));
100         PCI_CSR_WRITE_4(sc, PCI_CRP_AD_WDATA, data);
101 }
102
103 static int
104 ixppcib_attach(device_t dev)
105 {
106         int rid;
107         struct ixppcib_softc *sc;
108
109         sc = device_get_softc(dev);
110
111         rid = 0;
112         sc->sc_csr = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
113             IXP425_PCI_HWBASE, IXP425_PCI_HWBASE + IXP425_PCI_SIZE,
114             IXP425_PCI_SIZE, RF_ACTIVE);
115         if (sc->sc_csr == NULL)
116                 panic("cannot allocate PCI CSR registers");
117
118         ixp425_md_attach(dev);
119         /* always setup the base, incase another OS messes w/ it */
120         PCI_CSR_WRITE_4(sc, PCI_PCIMEMBASE, 0x48494a4b);
121
122         rid = 0;
123         sc->sc_mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
124             IXP425_PCI_MEM_HWBASE, IXP425_PCI_MEM_HWBASE + IXP425_PCI_MEM_SIZE,
125             IXP425_PCI_MEM_SIZE, RF_ACTIVE);
126         if (sc->sc_mem == NULL)
127                 panic("cannot allocate PCI MEM space");
128
129         /* NB: PCI dma window is 64M so anything above must be bounced */
130         if (bus_dma_tag_create(NULL, 1, 0, IXP425_AHB_OFFSET + 64 * 1024 * 1024,
131             BUS_SPACE_MAXADDR, NULL, NULL,  0xffffffff, 0xff, 0xffffffff, 0,
132             NULL, NULL, &sc->sc_dmat))
133                 panic("couldn't create the PCI dma tag !");
134         /*
135          * Initialize the bus space tags.
136          */
137         ixp425_io_bs_init(&sc->sc_pci_iot, sc);
138         ixp425_mem_bs_init(&sc->sc_pci_memt, sc);
139
140         sc->sc_dev = dev;
141
142         /* Initialize memory and i/o rmans. */
143         sc->sc_io_rman.rm_type = RMAN_ARRAY;
144         sc->sc_io_rman.rm_descr = "IXP4XX PCI I/O Ports";
145         if (rman_init(&sc->sc_io_rman) != 0 ||
146                 rman_manage_region(&sc->sc_io_rman, 0,
147                     IXP425_PCI_IO_SIZE) != 0) {
148                 panic("ixppcib_probe: failed to set up I/O rman");
149         }
150
151         sc->sc_mem_rman.rm_type = RMAN_ARRAY;
152         sc->sc_mem_rman.rm_descr = "IXP4XX PCI Memory";
153         if (rman_init(&sc->sc_mem_rman) != 0 ||
154                 rman_manage_region(&sc->sc_mem_rman, IXP425_PCI_MEM_HWBASE,
155                     IXP425_PCI_MEM_HWBASE + IXP425_PCI_MEM_SIZE) != 0) {
156                 panic("ixppcib_probe: failed to set up memory rman");
157         }
158
159         /*
160          * PCI->AHB address translation
161          *      begin at the physical memory start + OFFSET
162          */
163         PCI_CSR_WRITE_4(sc, PCI_AHBMEMBASE,
164             (IXP425_AHB_OFFSET & 0xFF000000) +
165             ((IXP425_AHB_OFFSET & 0xFF000000) >> 8) +
166             ((IXP425_AHB_OFFSET & 0xFF000000) >> 16) +
167             ((IXP425_AHB_OFFSET & 0xFF000000) >> 24) +
168             0x00010203);
169         
170 #define IXPPCIB_WRITE_CONF(sc, reg, val) \
171         ixp425_pci_conf_reg_write(sc, reg, val)
172         /* Write Mapping registers PCI Configuration Registers */
173         /* Base Address 0 - 3 */
174         IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR0, IXP425_AHB_OFFSET + 0x00000000);
175         IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR1, IXP425_AHB_OFFSET + 0x01000000);
176         IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR2, IXP425_AHB_OFFSET + 0x02000000);
177         IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR3, IXP425_AHB_OFFSET + 0x03000000);
178         
179         /* Base Address 4 */
180         IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR4, 0xffffffff);
181         
182         /* Base Address 5 */
183         IXPPCIB_WRITE_CONF(sc, PCI_MAPREG_BAR5, 0x00000000);
184         
185         /* Assert some PCI errors */
186         PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_AHBE | ISR_PPE | ISR_PFE | ISR_PSE);
187         
188 #ifdef __ARMEB__
189         /*
190          * Set up byte lane swapping between little-endian PCI
191          * and the big-endian AHB bus
192          */
193         PCI_CSR_WRITE_4(sc, PCI_CSR, CSR_IC | CSR_ABE | CSR_PDS);
194 #else
195         PCI_CSR_WRITE_4(sc, PCI_CSR, CSR_IC | CSR_ABE);
196 #endif
197         
198         /*
199          * Enable bus mastering and I/O,memory access
200          */
201         IXPPCIB_WRITE_CONF(sc, PCIR_COMMAND,
202             PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
203         
204         /*
205          * Wait some more to ensure PCI devices have stabilised.
206          */
207         DELAY(50000);
208
209         device_add_child(dev, "pci", -1);
210         return (bus_generic_attach(dev));
211 }
212
213 static int
214 ixppcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
215 {
216         struct ixppcib_softc *sc;
217
218         sc = device_get_softc(dev);
219         switch (which) {
220         case PCIB_IVAR_DOMAIN:
221                 *result = 0;
222                 return (0);
223         case PCIB_IVAR_BUS:
224                 *result = sc->sc_bus;
225                 return (0);
226         }
227
228         return (ENOENT);
229 }
230
231 static int
232 ixppcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
233 {
234         struct ixppcib_softc *sc;
235
236         sc = device_get_softc(dev);
237         switch (which) {
238         case PCIB_IVAR_DOMAIN:
239                 return (EINVAL);
240         case PCIB_IVAR_BUS:
241                 sc->sc_bus = value;
242                 return (0);
243         }
244
245         return (ENOENT);
246 }
247
248 static int
249 ixppcib_setup_intr(device_t dev, device_t child, struct resource *ires,
250     int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
251     void **cookiep)
252 {
253
254         return (BUS_SETUP_INTR(device_get_parent(dev), child, ires, flags,
255             filt, intr, arg, cookiep));
256 }
257
258 static int
259 ixppcib_teardown_intr(device_t dev, device_t child, struct resource *vec,
260      void *cookie)
261 {
262
263         return (BUS_TEARDOWN_INTR(device_get_parent(dev), child, vec, cookie));
264 }
265
266 static struct resource *
267 ixppcib_alloc_resource(device_t bus, device_t child, int type, int *rid,
268     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
269 {
270         struct ixppcib_softc *sc = device_get_softc(bus);
271         struct rman *rmanp;
272         struct resource *rv;
273
274         rv = NULL;
275         switch (type) {
276         case SYS_RES_IRQ:
277                 rmanp = &sc->sc_irq_rman;
278                 break;
279
280         case SYS_RES_IOPORT:
281                 rmanp = &sc->sc_io_rman;
282                 break;
283
284         case SYS_RES_MEMORY:
285                 rmanp = &sc->sc_mem_rman;
286                 break;
287
288         default:
289                 return (rv);
290         }
291
292         rv = rman_reserve_resource(rmanp, start, end, count, flags & ~RF_ACTIVE,
293             child);
294         if (rv == NULL)
295                 return (NULL);
296         rman_set_rid(rv, *rid);
297         if (flags & RF_ACTIVE) {
298                 if (bus_activate_resource(child, type, *rid, rv)) {
299                         rman_release_resource(rv);
300                         return (NULL);
301                 }
302         }
303
304         return (rv);
305 }
306
307 static int
308 ixppcib_activate_resource(device_t bus, device_t child, int type, int rid,
309     struct resource *r)
310 {
311         struct ixppcib_softc *sc = device_get_softc(bus);
312         int error;
313
314         error = rman_activate_resource(r);
315         if (error)
316                 return (error);
317         switch (type) {
318         case SYS_RES_IOPORT:
319                 rman_set_bustag(r, &sc->sc_pci_iot);
320                 rman_set_bushandle(r, rman_get_start(r));
321                 break;
322         case SYS_RES_MEMORY:
323                 rman_set_bustag(r, &sc->sc_pci_memt);
324                 rman_set_bushandle(r, rman_get_bushandle(sc->sc_mem) +
325                     (rman_get_start(r) - IXP425_PCI_MEM_HWBASE));
326                 break;
327         }
328                 
329         return (0);
330 }
331
332 static int
333 ixppcib_deactivate_resource(device_t bus, device_t child, int type, int rid,
334     struct resource *r)
335 {
336
337         device_printf(bus, "%s called deactivate_resource (unexpected)\n",
338             device_get_nameunit(child));
339         return (ENXIO);
340 }
341
342 static int
343 ixppcib_release_resource(device_t bus, device_t child, int type, int rid,
344     struct resource *r)
345 {
346
347         device_printf(bus, "%s called release_resource (unexpected)\n",
348             device_get_nameunit(child));
349         return (ENXIO);
350 }
351
352 static bus_dma_tag_t
353 ixppcib_get_dma_tag(device_t bus, device_t child)
354 {
355         struct ixppcib_softc *sc = device_get_softc(bus);
356
357         return (sc->sc_dmat);
358 }
359
360 static void
361 ixppcib_conf_setup(struct ixppcib_softc *sc, int bus, int slot, int func,
362     int reg)
363 {
364         if (bus == 0) {
365                 /* configuration type 0 */
366                 PCI_CSR_WRITE_4(sc, PCI_NP_AD,
367                     (1U << (32 - (slot & 0x1f))) |
368                     ((func & 0x7) << 8) | (reg & ~3));
369         } else {
370                 /* configuration type 1 */
371                 PCI_CSR_WRITE_4(sc, PCI_NP_AD,
372                     (bus << 16) | (slot << 11) |
373                     (func << 8) | (reg & ~3) | 1);
374         }
375
376 }
377
378 static int
379 ixppcib_maxslots(device_t dev)
380 {
381
382         return (PCI_SLOTMAX);
383 }
384
385 static u_int32_t
386 ixppcib_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
387     int bytes)
388 {
389         struct ixppcib_softc *sc = device_get_softc(dev);
390         u_int32_t data, ret;
391
392         ixppcib_conf_setup(sc, bus, slot, func, reg & ~3);
393
394         PCI_CSR_WRITE_4(sc, PCI_NP_CBE, COMMAND_NP_CONF_READ);
395         ret = PCI_CSR_READ_4(sc, PCI_NP_RDATA);
396         ret >>= (reg & 3) * 8;
397         ret &= 0xffffffff >> ((4 - bytes) * 8);
398 #if 0
399         device_printf(dev, "%s: %u:%u:%u %#x(%d) = %#x\n",
400             __func__, bus, slot, func, reg, bytes, ret);
401 #endif
402         /* check & clear PCI abort */
403         data = PCI_CSR_READ_4(sc, PCI_ISR);
404         if (data & ISR_PFE) {
405                 PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_PFE);
406                 return (-1);
407         }
408         return (ret);
409 }
410
411 static const int byteenables[] = { 0, 0x10, 0x30, 0x70, 0xf0 };
412
413 static void
414 ixppcib_write_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
415     u_int32_t val, int bytes)
416 {
417         struct ixppcib_softc *sc = device_get_softc(dev);
418         u_int32_t data;
419
420 #if 0
421         device_printf(dev, "%s: %u:%u:%u %#x(%d) = %#x\n",
422             __func__, bus, slot, func, reg, bytes, val);
423 #endif
424         ixppcib_conf_setup(sc, bus, slot, func, reg & ~3);
425
426         /* Byte enables are active low, so not them first */
427         PCI_CSR_WRITE_4(sc, PCI_NP_CBE, COMMAND_NP_CONF_WRITE |
428             (~(byteenables[bytes] << (reg & 3)) & 0xf0));
429         PCI_CSR_WRITE_4(sc, PCI_NP_WDATA, val << ((reg & 3) * 8));
430
431         /* check & clear PCI abort */
432         data = PCI_CSR_READ_4(sc, PCI_ISR);
433         if (data & ISR_PFE)
434                 PCI_CSR_WRITE_4(sc, PCI_ISR, ISR_PFE);
435 }
436
437 static int
438 ixppcib_route_interrupt(device_t bridge, device_t device, int pin)
439 {
440
441         return (ixp425_md_route_interrupt(bridge, device, pin));
442 }
443
444 static device_method_t ixppcib_methods[] = {
445         /* Device interface */
446         DEVMETHOD(device_probe,                 ixppcib_probe),
447         DEVMETHOD(device_attach,                ixppcib_attach),
448
449         /* Bus interface */
450         DEVMETHOD(bus_read_ivar,                ixppcib_read_ivar),
451         DEVMETHOD(bus_write_ivar,               ixppcib_write_ivar),
452         DEVMETHOD(bus_setup_intr,               ixppcib_setup_intr),
453         DEVMETHOD(bus_teardown_intr,            ixppcib_teardown_intr),
454         DEVMETHOD(bus_alloc_resource,           ixppcib_alloc_resource),
455         DEVMETHOD(bus_activate_resource,        ixppcib_activate_resource),
456         DEVMETHOD(bus_deactivate_resource,      ixppcib_deactivate_resource),
457         DEVMETHOD(bus_release_resource,         ixppcib_release_resource),
458         DEVMETHOD(bus_get_dma_tag,              ixppcib_get_dma_tag),
459
460         /* pcib interface */
461         DEVMETHOD(pcib_maxslots,                ixppcib_maxslots),
462         DEVMETHOD(pcib_read_config,             ixppcib_read_config),
463         DEVMETHOD(pcib_write_config,            ixppcib_write_config),
464         DEVMETHOD(pcib_route_interrupt,         ixppcib_route_interrupt),
465         DEVMETHOD(pcib_request_feature,         pcib_request_feature_allow),
466
467         DEVMETHOD_END
468 };
469
470 static driver_t ixppcib_driver = {
471         "pcib",
472         ixppcib_methods,
473         sizeof(struct ixppcib_softc),
474 };
475 static devclass_t ixppcib_devclass;
476
477 DRIVER_MODULE(ixppcib, ixp, ixppcib_driver, ixppcib_devclass, 0, 0);