xlp_pci.c revision 257338
191094Sdes/*-
2115619Sdes * Copyright (c) 2003-2012 Broadcom Corporation
3228690Sdes * All Rights Reserved
491094Sdes *
591094Sdes * Redistribution and use in source and binary forms, with or without
691094Sdes * modification, are permitted provided that the following conditions
799158Sdes * are met:
899158Sdes *
999158Sdes * 1. Redistributions of source code must retain the above copyright
1091094Sdes *    notice, this list of conditions and the following disclaimer.
1191094Sdes * 2. Redistributions in binary form must reproduce the above copyright
1291094Sdes *    notice, this list of conditions and the following disclaimer in
1391094Sdes *    the documentation and/or other materials provided with the
1491094Sdes *    distribution.
1591094Sdes *
1691094Sdes * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
1791094Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1891094Sdes * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1991094Sdes * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
2091094Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2191094Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2291094Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
2391094Sdes * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2491094Sdes * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
2591094Sdes * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2691094Sdes * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2791094Sdes */
2891094Sdes
2991094Sdes#include <sys/cdefs.h>
3091094Sdes__FBSDID("$FreeBSD: head/sys/mips/nlm/xlp_pci.c 257338 2013-10-29 14:07:31Z nwhitehorn $");
3191094Sdes
3291094Sdes#include <sys/param.h>
3391094Sdes#include <sys/systm.h>
3491094Sdes#include <sys/types.h>
35271947Sdes#include <sys/kernel.h>
3691094Sdes#include <sys/module.h>
3791094Sdes#include <sys/malloc.h>
38228690Sdes#include <sys/bus.h>
39228690Sdes#include <sys/endian.h>
40228690Sdes#include <sys/rman.h>
41228690Sdes#include <sys/pciio.h>
4291094Sdes
43255376Sdes#include <vm/vm.h>
44255376Sdes#include <vm/vm_param.h>
4591094Sdes#include <vm/pmap.h>
46117610Sdes
47255376Sdes#include <dev/pci/pcivar.h>
4891100Sdes#include <dev/pci/pcireg.h>
4991094Sdes#include <dev/pci/pci_private.h>
5091094Sdes
5191094Sdes#include <dev/uart/uart.h>
5291094Sdes#include <dev/uart/uart_bus.h>
5391100Sdes#include <dev/uart/uart_cpu.h>
5491094Sdes
5591094Sdes#include <machine/bus.h>
5691094Sdes#include <machine/md_var.h>
57107937Sdes#include <machine/intr_machdep.h>
58271947Sdes#include <machine/cpuregs.h>
59107937Sdes
6091100Sdes#include <mips/nlm/hal/haldefs.h>
6191100Sdes#include <mips/nlm/interrupt.h>
62255376Sdes#include <mips/nlm/hal/iomap.h>
63255376Sdes#include <mips/nlm/hal/mips-extns.h>
64255376Sdes#include <mips/nlm/hal/pic.h>
65255376Sdes#include <mips/nlm/hal/bridge.h>
66255376Sdes#include <mips/nlm/hal/gbu.h>
6791100Sdes#include <mips/nlm/hal/pcibus.h>
68255376Sdes#include <mips/nlm/hal/uart.h>
6991100Sdes#include <mips/nlm/xlp.h>
70117610Sdes
71255376Sdes#include "pcib_if.h"
72255376Sdes#include "pci_if.h"
73255376Sdes
74255376Sdes#define	EMUL_MEM_START	0x16000000UL
75255376Sdes#define	EMUL_MEM_END	0x18ffffffUL
76255376Sdes
77255376Sdes/* SoC device qurik handling */
7891100Sdesstatic int irt_irq_map[4 * 256];
7991100Sdesstatic int irq_irt_map[64];
80255376Sdes
81255376Sdesstatic void
82255376Sdesxlp_add_irq(int node, int irt, int irq)
83255376Sdes{
84255376Sdes	int nodeirt = node * 256 + irt;
8591100Sdes
86255376Sdes	irt_irq_map[nodeirt] = irq;
87255376Sdes	irq_irt_map[irq] = nodeirt;
88255376Sdes}
89255376Sdes
90255376Sdesint
91255376Sdesxlp_irq_to_irt(int irq)
92255376Sdes{
93255376Sdes	return irq_irt_map[irq];
94255376Sdes}
95117610Sdes
9691100Sdesint
97255376Sdesxlp_irt_to_irq(int nodeirt)
98255376Sdes{
99255376Sdes	return irt_irq_map[nodeirt];
100255376Sdes}
101255376Sdes
102255376Sdes/* Override PCI a bit for SoC devices */
103255376Sdes
104255376Sdesenum {
105255376Sdes	INTERNAL_DEV	= 0x1,	/* internal device, skip on enumeration */
106255376Sdes	MEM_RES_EMUL	= 0x2,	/* no MEM or IO bar, custom res alloc */
107255376Sdes	SHARED_IRQ	= 0x4,
108255376Sdes	DEV_MMIO32	= 0x8,	/* byte access not allowed to mmio */
109255376Sdes};
110255376Sdes
111255376Sdesstruct soc_dev_desc {
112255376Sdes	u_int	devid;		/* device ID */
113255376Sdes	int	irqbase;	/* start IRQ */
114255376Sdes	u_int	flags;		/* flags */
115255376Sdes	int	ndevs;		/* to keep track of number of devices */
116255376Sdes};
117255376Sdes
118255376Sdesstruct soc_dev_desc xlp_dev_desc[] = {
119255376Sdes	{ PCI_DEVICE_ID_NLM_ICI,               0, INTERNAL_DEV },
120255376Sdes	{ PCI_DEVICE_ID_NLM_PIC,               0, INTERNAL_DEV },
12191100Sdes	{ PCI_DEVICE_ID_NLM_FMN,               0, INTERNAL_DEV },
122255376Sdes	{ PCI_DEVICE_ID_NLM_UART, PIC_UART_0_IRQ, MEM_RES_EMUL | DEV_MMIO32},
123255376Sdes	{ PCI_DEVICE_ID_NLM_I2C,               0, MEM_RES_EMUL | DEV_MMIO32 },
124255376Sdes	{ PCI_DEVICE_ID_NLM_NOR,               0, MEM_RES_EMUL },
125255376Sdes	{ PCI_DEVICE_ID_NLM_MMC,     PIC_MMC_IRQ, MEM_RES_EMUL },
126255376Sdes	{ PCI_DEVICE_ID_NLM_EHCI, PIC_EHCI_0_IRQ, 0 }
127255376Sdes};
128255376Sdes
129255376Sdesstruct  xlp_devinfo {
130255376Sdes	struct pci_devinfo pcidev;
131255376Sdes	int	irq;
132255376Sdes	int	flags;
133255376Sdes	u_long	mem_res_start;
134255376Sdes};
135255376Sdes
136255376Sdesstatic __inline struct soc_dev_desc *
137255376Sdesxlp_find_soc_desc(int devid)
138255376Sdes{
139255376Sdes	struct soc_dev_desc *p;
140255376Sdes	int i, n;
141255376Sdes
142255376Sdes	n = sizeof(xlp_dev_desc) / sizeof(xlp_dev_desc[0]);
143255376Sdes	for (i = 0, p = xlp_dev_desc; i < n; i++, p++)
144255376Sdes		if (p->devid == devid)
145255376Sdes			return (p);
146255376Sdes	return (NULL);
147255376Sdes}
148255376Sdes
149255376Sdesstatic struct resource *
150255376Sdesxlp_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
151255376Sdes    u_long start, u_long end, u_long count, u_int flags)
152255376Sdes{
153255376Sdes	struct resource *r;
154255376Sdes	struct xlp_devinfo *xlp_devinfo;
155255376Sdes	int busno;
156255376Sdes
157255376Sdes	/*
158255376Sdes	 * Do custom allocation for MEMORY resource for SoC device if
159255376Sdes	 * MEM_RES_EMUL flag is set
160255376Sdes	 */
161255376Sdes	busno = pci_get_bus(child);
162255376Sdes	if ((type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) && busno == 0) {
163117610Sdes		xlp_devinfo = (struct xlp_devinfo *)device_get_ivars(child);
164255376Sdes		if ((xlp_devinfo->flags & MEM_RES_EMUL) != 0) {
165255376Sdes			/* no emulation for IO ports */
166255376Sdes			if (type == SYS_RES_IOPORT)
167255376Sdes				return (NULL);
168117610Sdes
169255376Sdes			start = xlp_devinfo->mem_res_start;
170255376Sdes			count = XLP_PCIE_CFG_SIZE - XLP_IO_PCI_HDRSZ;
171255376Sdes
172255376Sdes			/* MMC needs to 2 slots with rids 16 and 20 and a
173117610Sdes			 * fixup for size */
174255376Sdes			if (pci_get_device(child) == PCI_DEVICE_ID_NLM_MMC) {
175255376Sdes				count = 0x100;
176255376Sdes				if (*rid == 16)
177255376Sdes					; /* first slot already setup */
178117610Sdes				else if (*rid == 20)
179255376Sdes					start += 0x100; /* second slot */
180255376Sdes				else
181255376Sdes					return (NULL);
182117610Sdes			}
183255376Sdes
184255376Sdes			end = start + count - 1;
185255376Sdes			r = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
186255376Sdes			    type, rid, start, end, count, flags);
187255376Sdes			if (r == NULL)
188255376Sdes				return (NULL);
189255376Sdes			if ((xlp_devinfo->flags & DEV_MMIO32) != 0)
190255376Sdes				rman_set_bustag(r, rmi_uart_bus_space);
191117610Sdes			return (r);
192255376Sdes		}
193255376Sdes	}
194255376Sdes
195255376Sdes	/* Not custom alloc, use PCI code */
196255376Sdes	return (pci_alloc_resource(bus, child, type, rid, start, end, count,
197255376Sdes	    flags));
198255376Sdes}
199255376Sdes
200255376Sdesstatic int
201255376Sdesxlp_pci_release_resource(device_t bus, device_t child, int type, int rid,
202255376Sdes    struct resource *r)
203255376Sdes{
204255376Sdes	u_long start;
205255376Sdes
206255376Sdes	/* If custom alloc, handle that */
207255376Sdes	start = rman_get_start(r);
208255376Sdes	if (type == SYS_RES_MEMORY && pci_get_bus(child) == 0 &&
209255376Sdes	    start >= EMUL_MEM_START && start <= EMUL_MEM_END)
210255376Sdes		return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
211255376Sdes		    type, rid, r));
212255376Sdes
213255376Sdes	/* use default PCI function */
214255376Sdes	return (bus_generic_rl_release_resource(bus, child, type, rid, r));
215255376Sdes}
216255376Sdes
217255376Sdesstatic void
218255376Sdesxlp_add_soc_child(device_t pcib, device_t dev, int b, int s, int f)
219255376Sdes{
220255376Sdes	struct pci_devinfo *dinfo;
221255376Sdes	struct xlp_devinfo *xlp_dinfo;
222255376Sdes	struct soc_dev_desc *si;
223255376Sdes	uint64_t pcibase;
224255376Sdes	int domain, node, irt, irq, flags, devoffset, num;
225255376Sdes	uint16_t devid;
226255376Sdes
227255376Sdes	domain = pcib_get_domain(dev);
228255376Sdes	node = s / 8;
229255376Sdes	devoffset = XLP_HDR_OFFSET(node, 0, s % 8, f);
230255376Sdes	if (!nlm_dev_exists(devoffset))
231255376Sdes		return;
232255376Sdes
233255376Sdes	/* Find if there is a desc for the SoC device */
234255376Sdes	devid = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_DEVICE, 2);
235255376Sdes	si = xlp_find_soc_desc(devid);
236255376Sdes
237255376Sdes	/* update flags and irq from desc if available */
238255376Sdes	irq = 0;
239255376Sdes	flags = 0;
240255376Sdes	if (si != NULL) {
241255376Sdes		if (si->irqbase != 0)
242255376Sdes			irq = si->irqbase + si->ndevs;
243255376Sdes		flags = si->flags;
244255376Sdes		si->ndevs++;
245255376Sdes	}
246255376Sdes
247255376Sdes	/* skip internal devices */
248255376Sdes	if ((flags & INTERNAL_DEV) != 0)
249255376Sdes		return;
250255376Sdes
251255376Sdes	/* PCIe interfaces are special, bug in Ax */
252255376Sdes	if (devid == PCI_DEVICE_ID_NLM_PCIE) {
253255376Sdes		xlp_add_irq(node, xlp_pcie_link_irt(f), PIC_PCIE_0_IRQ + f);
254255376Sdes	} else {
255255376Sdes		/* Stash intline and pin in shadow reg for devices */
256255376Sdes		pcibase = nlm_pcicfg_base(devoffset);
257255376Sdes		irt = nlm_irtstart(pcibase);
258255376Sdes		num = nlm_irtnum(pcibase);
259255376Sdes		if (irq != 0 && num > 0) {
260255376Sdes			xlp_add_irq(node, irt, irq);
261255376Sdes			nlm_write_reg(pcibase, XLP_PCI_DEVSCRATCH_REG0,
262255376Sdes			    (1 << 8) | irq);
263255376Sdes		}
26491100Sdes	}
265255376Sdes	dinfo = pci_read_device(pcib, domain, b, s, f, sizeof(*xlp_dinfo));
266255376Sdes	if (dinfo == NULL)
267255376Sdes		return;
268255376Sdes	xlp_dinfo = (struct xlp_devinfo *)dinfo;
269255376Sdes	xlp_dinfo->irq = irq;
270255376Sdes	xlp_dinfo->flags = flags;
271255376Sdes
272255376Sdes	/* memory resource from ecfg space, if MEM_RES_EMUL is set */
273255376Sdes	if ((flags & MEM_RES_EMUL) != 0)
274255376Sdes		xlp_dinfo->mem_res_start = XLP_DEFAULT_IO_BASE + devoffset +
275255376Sdes		    XLP_IO_PCI_HDRSZ;
276255376Sdes	pci_add_child(dev, dinfo);
277255376Sdes}
278255376Sdes
279255376Sdesstatic int
280255376Sdesxlp_pci_attach(device_t dev)
281255376Sdes{
28291100Sdes	device_t pcib = device_get_parent(dev);
28391100Sdes	int maxslots, s, f, pcifunchigh;
284255376Sdes	int busno;
285255376Sdes	uint8_t hdrtype;
286255376Sdes
287255376Sdes	/*
288255376Sdes	 * The on-chip devices are on a bus that is almost, but not
289255376Sdes	 * quite, completely like PCI. Add those things by hand.
29091100Sdes	 */
291255376Sdes	busno = pcib_get_bus(dev);
29291100Sdes	maxslots = PCIB_MAXSLOTS(pcib);
293255376Sdes	for (s = 0; s <= maxslots; s++) {
294255376Sdes		pcifunchigh = 0;
295255376Sdes		f = 0;
296255376Sdes		hdrtype = PCIB_READ_CONFIG(pcib, busno, s, f, PCIR_HDRTYPE, 1);
297255376Sdes		if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
298255376Sdes			continue;
299255376Sdes		if (hdrtype & PCIM_MFDEV)
300255376Sdes			pcifunchigh = PCI_FUNCMAX;
301255376Sdes		for (f = 0; f <= pcifunchigh; f++)
302255376Sdes			xlp_add_soc_child(pcib, dev, busno, s, f);
303255376Sdes	}
304255376Sdes	return (bus_generic_attach(dev));
30591100Sdes}
306255376Sdes
307255376Sdesstatic int
308255376Sdesxlp_pci_probe(device_t dev)
30991100Sdes{
31091100Sdes	device_t pcib;
31191100Sdes
31291094Sdes	pcib = device_get_parent(dev);
31391100Sdes	/*
31491100Sdes	 * Only the top level bus has SoC devices, leave the rest to
31591100Sdes	 * Generic PCI code
31691094Sdes	 */
31791094Sdes	if (strcmp(device_get_nameunit(pcib), "pcib0") != 0)
31891094Sdes		return (ENXIO);
31991094Sdes	device_set_desc(dev, "XLP SoCbus");
32091094Sdes	return (BUS_PROBE_DEFAULT);
32191094Sdes}
32291094Sdes
32391094Sdesstatic devclass_t pci_devclass;
324255376Sdesstatic device_method_t xlp_pci_methods[] = {
325123394Sdes	/* Device interface */
32691100Sdes	DEVMETHOD(device_probe,		xlp_pci_probe),
32791094Sdes	DEVMETHOD(device_attach,	xlp_pci_attach),
328107937Sdes	DEVMETHOD(bus_alloc_resource,	xlp_pci_alloc_resource),
329107937Sdes	DEVMETHOD(bus_release_resource, xlp_pci_release_resource),
33091094Sdes
331107937Sdes	DEVMETHOD_END
332123394Sdes};
333107937Sdes
33491094SdesDEFINE_CLASS_1(pci, xlp_pci_driver, xlp_pci_methods, sizeof(struct pci_softc),
335123394Sdes    pci_driver);
336123394SdesDRIVER_MODULE(xlp_pci, pcib, xlp_pci_driver, pci_devclass, 0, 0);
33791094Sdes
33891094Sdesstatic devclass_t pcib_devclass;
339255376Sdesstatic struct rman irq_rman, port_rman, mem_rman, emul_rman;
340255376Sdes
34191100Sdesstatic void
34291100Sdesxlp_pcib_init_resources(void)
34391094Sdes{
344255376Sdes	irq_rman.rm_start = 0;
345255376Sdes	irq_rman.rm_end = 255;
34691094Sdes	irq_rman.rm_type = RMAN_ARRAY;
34791094Sdes	irq_rman.rm_descr = "PCI Mapped Interrupts";
34891094Sdes	if (rman_init(&irq_rman)
34991094Sdes	    || rman_manage_region(&irq_rman, 0, 255))
35094735Sdes		panic("pci_init_resources irq_rman");
35194735Sdes
35294735Sdes	port_rman.rm_start = 0;
35391094Sdes	port_rman.rm_end = ~0ul;
35491094Sdes	port_rman.rm_type = RMAN_ARRAY;
35591094Sdes	port_rman.rm_descr = "I/O ports";
35694735Sdes	if (rman_init(&port_rman)
35794735Sdes	    || rman_manage_region(&port_rman, PCIE_IO_BASE, PCIE_IO_LIMIT))
35894735Sdes		panic("pci_init_resources port_rman");
35991094Sdes
36091094Sdes	mem_rman.rm_start = 0;
36191094Sdes	mem_rman.rm_end = ~0ul;
36291094Sdes	mem_rman.rm_type = RMAN_ARRAY;
36391094Sdes	mem_rman.rm_descr = "I/O memory";
364123394Sdes	if (rman_init(&mem_rman)
365255376Sdes	    || rman_manage_region(&mem_rman, PCIE_MEM_BASE, PCIE_MEM_LIMIT))
366107937Sdes		panic("pci_init_resources mem_rman");
367228690Sdes
368122912Sdes	/*
369123394Sdes	 * This includes the GBU (nor flash) memory range and the PCIe
370271947Sdes	 * memory area.
371123394Sdes	 */
372117610Sdes	emul_rman.rm_start = 0;
373122912Sdes	emul_rman.rm_end = ~0ul;
374123394Sdes	emul_rman.rm_type = RMAN_ARRAY;
375123394Sdes	emul_rman.rm_descr = "Emulated MEMIO";
376123394Sdes	if (rman_init(&emul_rman)
377255376Sdes	    || rman_manage_region(&emul_rman, EMUL_MEM_START, EMUL_MEM_END))
378107937Sdes		panic("pci_init_resources emul_rman");
37991094Sdes}
38091100Sdes
38191100Sdesstatic int
38291100Sdesxlp_pcib_probe(device_t dev)
38391100Sdes{
38491100Sdes
38591100Sdes	device_set_desc(dev, "XLP PCI bus");
38691100Sdes	return (BUS_PROBE_NOWILDCARD);
38791100Sdes}
38897241Sdes
38997241Sdesstatic int
39097241Sdesxlp_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
391141098Sdes{
392141098Sdes
393141098Sdes	switch (which) {
39497241Sdes	case PCIB_IVAR_DOMAIN:
39597241Sdes		*result = 0;
396115619Sdes		return (0);
39797241Sdes	case PCIB_IVAR_BUS:
39897241Sdes		*result = 0;
39997241Sdes		return (0);
40097241Sdes	}
40197241Sdes	return (ENOENT);
40297241Sdes}
403
404static int
405xlp_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t result)
406{
407	switch (which) {
408	case PCIB_IVAR_DOMAIN:
409		return (EINVAL);
410	case PCIB_IVAR_BUS:
411		return (EINVAL);
412	}
413	return (ENOENT);
414}
415
416static int
417xlp_pcib_maxslots(device_t dev)
418{
419
420	return (PCI_SLOTMAX);
421}
422
423static u_int32_t
424xlp_pcib_read_config(device_t dev, u_int b, u_int s, u_int f,
425    u_int reg, int width)
426{
427	uint32_t data = 0;
428	uint64_t cfgaddr;
429	int	regindex = reg/sizeof(uint32_t);
430
431	cfgaddr = nlm_pcicfg_base(XLP_HDR_OFFSET(0, b, s, f));
432	if ((width == 2) && (reg & 1))
433		return 0xFFFFFFFF;
434	else if ((width == 4) && (reg & 3))
435		return 0xFFFFFFFF;
436
437	/*
438	 * The intline and int pin of SoC devices are DOA, except
439	 * for bridges (slot %8 == 1).
440	 * use the values we stashed in a writable PCI scratch reg.
441	 */
442	if (b == 0 && regindex == 0xf && s % 8 > 1)
443		regindex = XLP_PCI_DEVSCRATCH_REG0;
444
445	data = nlm_read_pci_reg(cfgaddr, regindex);
446	if (width == 1)
447		return ((data >> ((reg & 3) << 3)) & 0xff);
448	else if (width == 2)
449		return ((data >> ((reg & 3) << 3)) & 0xffff);
450	else
451		return (data);
452}
453
454static void
455xlp_pcib_write_config(device_t dev, u_int b, u_int s, u_int f,
456    u_int reg, u_int32_t val, int width)
457{
458	uint64_t cfgaddr;
459	uint32_t data = 0;
460	int	regindex = reg / sizeof(uint32_t);
461
462	cfgaddr = nlm_pcicfg_base(XLP_HDR_OFFSET(0, b, s, f));
463	if ((width == 2) && (reg & 1))
464		return;
465	else if ((width == 4) && (reg & 3))
466		return;
467
468	if (width == 1) {
469		data = nlm_read_pci_reg(cfgaddr, regindex);
470		data = (data & ~(0xff << ((reg & 3) << 3))) |
471		    (val << ((reg & 3) << 3));
472	} else if (width == 2) {
473		data = nlm_read_pci_reg(cfgaddr, regindex);
474		data = (data & ~(0xffff << ((reg & 3) << 3))) |
475		    (val << ((reg & 3) << 3));
476	} else {
477		data = val;
478	}
479
480	/*
481	 * use shadow reg for intpin/intline which are dead
482	 */
483	if (b == 0 && regindex == 0xf && s % 8 > 1)
484		regindex = XLP_PCI_DEVSCRATCH_REG0;
485	nlm_write_pci_reg(cfgaddr, regindex, data);
486}
487
488/*
489 * Enable byte swap in hardware when compiled big-endian.
490 * Programs a link's PCIe SWAP regions from the link's IO and MEM address
491 * ranges.
492 */
493static void
494xlp_pcib_hardware_swap_enable(int node, int link)
495{
496#if BYTE_ORDER == BIG_ENDIAN
497	uint64_t bbase, linkpcibase;
498	uint32_t bar;
499	int pcieoffset;
500
501	pcieoffset = XLP_IO_PCIE_OFFSET(node, link);
502	if (!nlm_dev_exists(pcieoffset))
503		return;
504
505	bbase = nlm_get_bridge_regbase(node);
506	linkpcibase = nlm_pcicfg_base(pcieoffset);
507	bar = nlm_read_bridge_reg(bbase, BRIDGE_PCIEMEM_BASE0 + link);
508	nlm_write_pci_reg(linkpcibase, PCIE_BYTE_SWAP_MEM_BASE, bar);
509
510	bar = nlm_read_bridge_reg(bbase, BRIDGE_PCIEMEM_LIMIT0 + link);
511	nlm_write_pci_reg(linkpcibase, PCIE_BYTE_SWAP_MEM_LIM, bar | 0xFFF);
512
513	bar = nlm_read_bridge_reg(bbase, BRIDGE_PCIEIO_BASE0 + link);
514	nlm_write_pci_reg(linkpcibase, PCIE_BYTE_SWAP_IO_BASE, bar);
515
516	bar = nlm_read_bridge_reg(bbase, BRIDGE_PCIEIO_LIMIT0 + link);
517	nlm_write_pci_reg(linkpcibase, PCIE_BYTE_SWAP_IO_LIM, bar | 0xFFF);
518#endif
519}
520
521static int
522xlp_pcib_attach(device_t dev)
523{
524	int node, link;
525
526	xlp_pcib_init_resources();
527
528	/* enable hardware swap on all nodes/links */
529	for (node = 0; node < XLP_MAX_NODES; node++)
530		for (link = 0; link < 4; link++)
531			xlp_pcib_hardware_swap_enable(node, link);
532
533	device_add_child(dev, "pci", 0);
534	bus_generic_attach(dev);
535	return (0);
536}
537
538static void
539xlp_pcib_identify(driver_t * driver, device_t parent)
540{
541
542	BUS_ADD_CHILD(parent, 0, "pcib", 0);
543}
544
545/*
546 * XLS PCIe can have upto 4 links, and each link has its on IRQ
547 * Find the link on which the device is on
548 */
549static int
550xlp_pcie_link(device_t pcib, device_t dev)
551{
552	device_t parent, tmp;
553
554	/* find the lane on which the slot is connected to */
555	tmp = dev;
556	while (1) {
557		parent = device_get_parent(tmp);
558		if (parent == NULL || parent == pcib) {
559			device_printf(dev, "Cannot find parent bus\n");
560			return (-1);
561		}
562		if (strcmp(device_get_nameunit(parent), "pci0") == 0)
563			break;
564		tmp = parent;
565	}
566	return (pci_get_function(tmp));
567}
568
569static int
570xlp_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
571{
572	int i, link;
573
574	/*
575	 * Each link has 32 MSIs that can be allocated, but for now
576	 * we only support one device per link.
577	 * msi_alloc() equivalent is needed when we start supporting
578	 * bridges on the PCIe link.
579	 */
580	link = xlp_pcie_link(pcib, dev);
581	if (link == -1)
582		return (ENXIO);
583
584	/*
585	 * encode the irq so that we know it is a MSI interrupt when we
586	 * setup interrupts
587	 */
588	for (i = 0; i < count; i++)
589		irqs[i] = 64 + link * 32 + i;
590
591	return (0);
592}
593
594static int
595xlp_release_msi(device_t pcib, device_t dev, int count, int *irqs)
596{
597	return (0);
598}
599
600static int
601xlp_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
602    uint32_t *data)
603{
604	int msi, irt;
605
606	if (irq >= 64) {
607		msi = irq - 64;
608		*addr = MIPS_MSI_ADDR(0);
609
610		irt = xlp_pcie_link_irt(msi/32);
611		if (irt != -1)
612			*data = MIPS_MSI_DATA(xlp_irt_to_irq(irt));
613		return (0);
614	} else {
615		device_printf(dev, "%s: map_msi for irq %d  - ignored",
616		    device_get_nameunit(pcib), irq);
617		return (ENXIO);
618	}
619}
620
621static void
622bridge_pcie_ack(int irq)
623{
624	uint32_t node,reg;
625	uint64_t base;
626
627	node = nlm_nodeid();
628	reg = PCIE_MSI_STATUS;
629
630	switch (irq) {
631		case PIC_PCIE_0_IRQ:
632			base = nlm_pcicfg_base(XLP_IO_PCIE0_OFFSET(node));
633			break;
634		case PIC_PCIE_1_IRQ:
635			base = nlm_pcicfg_base(XLP_IO_PCIE1_OFFSET(node));
636			break;
637		case PIC_PCIE_2_IRQ:
638			base = nlm_pcicfg_base(XLP_IO_PCIE2_OFFSET(node));
639			break;
640		case PIC_PCIE_3_IRQ:
641			base = nlm_pcicfg_base(XLP_IO_PCIE3_OFFSET(node));
642			break;
643		default:
644			return;
645	}
646
647	nlm_write_pci_reg(base, reg, 0xFFFFFFFF);
648	return;
649}
650
651static int
652mips_platform_pcib_setup_intr(device_t dev, device_t child,
653    struct resource *irq, int flags, driver_filter_t *filt,
654    driver_intr_t *intr, void *arg, void **cookiep)
655{
656	int error = 0;
657	int xlpirq;
658	void *extra_ack;
659
660	error = rman_activate_resource(irq);
661	if (error)
662		return error;
663	if (rman_get_start(irq) != rman_get_end(irq)) {
664		device_printf(dev, "Interrupt allocation %lu != %lu\n",
665		    rman_get_start(irq), rman_get_end(irq));
666		return (EINVAL);
667	}
668	xlpirq = rman_get_start(irq);
669	if (xlpirq == 0)
670		return (0);
671
672	if (strcmp(device_get_name(dev), "pcib") != 0)
673		return (0);
674
675	/*
676	 * temporary hack for MSI, we support just one device per
677	 * link, and assign the link interrupt to the device interrupt
678	 */
679	if (xlpirq >= 64) {
680		int node, val, link;
681		uint64_t base;
682
683		xlpirq -= 64;
684		if (xlpirq % 32 != 0)
685			return (0);
686
687		node = nlm_nodeid();
688		link = xlpirq / 32;
689		base = nlm_pcicfg_base(XLP_IO_PCIE_OFFSET(node,link));
690
691		/* MSI Interrupt Vector enable at bridge's configuration */
692		nlm_write_pci_reg(base, PCIE_MSI_EN, PCIE_MSI_VECTOR_INT_EN);
693
694		val = nlm_read_pci_reg(base, PCIE_INT_EN0);
695		/* MSI Interrupt enable at bridge's configuration */
696		nlm_write_pci_reg(base, PCIE_INT_EN0,
697		    (val | PCIE_MSI_INT_EN));
698
699		/* legacy interrupt disable at bridge */
700		val = nlm_read_pci_reg(base, PCIE_BRIDGE_CMD);
701		nlm_write_pci_reg(base, PCIE_BRIDGE_CMD,
702		    (val | PCIM_CMD_INTxDIS));
703
704		/* MSI address update at bridge */
705		nlm_write_pci_reg(base, PCIE_BRIDGE_MSI_ADDRL,
706		    MSI_MIPS_ADDR_BASE);
707		nlm_write_pci_reg(base, PCIE_BRIDGE_MSI_ADDRH, 0);
708
709		val = nlm_read_pci_reg(base, PCIE_BRIDGE_MSI_CAP);
710		/* MSI capability enable at bridge */
711		nlm_write_pci_reg(base, PCIE_BRIDGE_MSI_CAP,
712		    (val | (PCIM_MSICTRL_MSI_ENABLE << 16) |
713		        (PCIM_MSICTRL_MMC_32 << 16)));
714
715		xlpirq = xlp_pcie_link_irt(xlpirq / 32);
716		if (xlpirq == -1)
717			return (EINVAL);
718		xlpirq = xlp_irt_to_irq(xlpirq);
719	}
720	/* Set all irqs to CPU 0 for now */
721	nlm_pic_write_irt_direct(xlp_pic_base, xlp_irq_to_irt(xlpirq), 1, 0,
722	    PIC_LOCAL_SCHEDULING, xlpirq, 0);
723	extra_ack = NULL;
724	if (xlpirq >= PIC_PCIE_0_IRQ && xlpirq <= PIC_PCIE_3_IRQ)
725		extra_ack = bridge_pcie_ack;
726	xlp_establish_intr(device_get_name(child), filt,
727	    intr, arg, xlpirq, flags, cookiep, extra_ack);
728
729	return (0);
730}
731
732static int
733mips_platform_pcib_teardown_intr(device_t dev, device_t child,
734    struct resource *irq, void *cookie)
735{
736	if (strcmp(device_get_name(child), "pci") == 0) {
737		/* if needed reprogram the pic to clear pcix related entry */
738		device_printf(dev, "teardown intr\n");
739	}
740	return (bus_generic_teardown_intr(dev, child, irq, cookie));
741}
742
743static struct resource *
744xlp_pcib_alloc_resource(device_t bus, device_t child, int type, int *rid,
745    u_long start, u_long end, u_long count, u_int flags)
746{
747	struct rman *rm = NULL;
748	struct resource *rv;
749	void *va;
750	int needactivate = flags & RF_ACTIVE;
751
752	switch (type) {
753	case SYS_RES_IRQ:
754		rm = &irq_rman;
755		break;
756
757	case SYS_RES_IOPORT:
758		rm = &port_rman;
759		break;
760
761	case SYS_RES_MEMORY:
762		if (start >= EMUL_MEM_START && start <= EMUL_MEM_END)
763			rm = &emul_rman;
764		else
765			rm = &mem_rman;
766			break;
767
768	default:
769		return (0);
770	}
771
772	rv = rman_reserve_resource(rm, start, end, count, flags, child);
773	if (rv == NULL)
774		return (NULL);
775
776	rman_set_rid(rv, *rid);
777
778	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
779		va = pmap_mapdev(start, count);
780		rman_set_bushandle(rv, (bus_space_handle_t)va);
781		rman_set_bustag(rv, rmi_bus_space);
782	}
783	if (needactivate) {
784		if (bus_activate_resource(child, type, *rid, rv)) {
785			rman_release_resource(rv);
786			return (NULL);
787		}
788	}
789	return (rv);
790}
791
792static int
793xlp_pcib_release_resource(device_t bus, device_t child, int type, int rid,
794    struct resource *r)
795{
796
797	return (rman_release_resource(r));
798}
799
800static int
801xlp_pcib_activate_resource(device_t bus, device_t child, int type, int rid,
802    struct resource *r)
803{
804
805	return (rman_activate_resource(r));
806}
807
808static int
809xlp_pcib_deactivate_resource(device_t bus, device_t child, int type, int rid,
810    struct resource *r)
811{
812
813	return (rman_deactivate_resource(r));
814}
815
816static int
817mips_pcib_route_interrupt(device_t bus, device_t dev, int pin)
818{
819	int irt, link;
820
821	/*
822	 * Validate requested pin number.
823	 */
824	if ((pin < 1) || (pin > 4))
825		return (255);
826
827	if (pci_get_bus(dev) == 0 &&
828	    pci_get_vendor(dev) == PCI_VENDOR_NETLOGIC) {
829		/* SoC devices */
830		uint64_t pcibase;
831		int f, n, d, num;
832
833		f = pci_get_function(dev);
834		n = pci_get_slot(dev) / 8;
835		d = pci_get_slot(dev) % 8;
836
837		/*
838		 * For PCIe links, return link IRT, for other SoC devices
839		 * get the IRT from its PCIe header
840		 */
841		if (d == 1) {
842			irt = xlp_pcie_link_irt(f);
843		} else {
844			pcibase = nlm_pcicfg_base(XLP_HDR_OFFSET(n, 0, d, f));
845			irt = nlm_irtstart(pcibase);
846			num = nlm_irtnum(pcibase);
847			if (num != 1)
848				device_printf(bus, "[%d:%d:%d] Error %d IRQs\n",
849				    n, d, f, num);
850		}
851	} else {
852		/* Regular PCI devices */
853		link = xlp_pcie_link(bus, dev);
854		irt = xlp_pcie_link_irt(link);
855	}
856
857	if (irt != -1)
858		return (xlp_irt_to_irq(irt));
859
860	return (255);
861}
862
863static device_method_t xlp_pcib_methods[] = {
864	/* Device interface */
865	DEVMETHOD(device_identify, xlp_pcib_identify),
866	DEVMETHOD(device_probe, xlp_pcib_probe),
867	DEVMETHOD(device_attach, xlp_pcib_attach),
868
869	/* Bus interface */
870	DEVMETHOD(bus_read_ivar, xlp_pcib_read_ivar),
871	DEVMETHOD(bus_write_ivar, xlp_pcib_write_ivar),
872	DEVMETHOD(bus_alloc_resource, xlp_pcib_alloc_resource),
873	DEVMETHOD(bus_release_resource, xlp_pcib_release_resource),
874	DEVMETHOD(bus_activate_resource, xlp_pcib_activate_resource),
875	DEVMETHOD(bus_deactivate_resource, xlp_pcib_deactivate_resource),
876	DEVMETHOD(bus_setup_intr, mips_platform_pcib_setup_intr),
877	DEVMETHOD(bus_teardown_intr, mips_platform_pcib_teardown_intr),
878
879	/* pcib interface */
880	DEVMETHOD(pcib_maxslots, xlp_pcib_maxslots),
881	DEVMETHOD(pcib_read_config, xlp_pcib_read_config),
882	DEVMETHOD(pcib_write_config, xlp_pcib_write_config),
883	DEVMETHOD(pcib_route_interrupt, mips_pcib_route_interrupt),
884
885	DEVMETHOD(pcib_alloc_msi, xlp_alloc_msi),
886	DEVMETHOD(pcib_release_msi, xlp_release_msi),
887	DEVMETHOD(pcib_map_msi, xlp_map_msi),
888
889	DEVMETHOD_END
890};
891
892static driver_t xlp_pcib_driver = {
893	"pcib",
894	xlp_pcib_methods,
895	1, /* no softc */
896};
897
898DRIVER_MODULE(pcib, nexus, xlp_pcib_driver, pcib_devclass, 0, 0);
899