1/*	$NetBSD: pci_machdep.c,v 1.4 2011/06/22 18:06:32 matt Exp $	*/
2
3/*
4 * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
5 * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Charles M. Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Machine-specific functions for PCI autoconfiguration.
35 *
36 * On PCs, there are two methods of generating PCI configuration cycles.
37 * We try to detect the appropriate mechanism for this machine and set
38 * up a few function pointers to access the correct method directly.
39 *
40 * The configuration method can be hard-coded in the config file by
41 * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
42 * as defined section 3.6.4.1, `Generating Configuration Cycles'.
43 */
44
45#include <sys/cdefs.h>
46__KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.4 2011/06/22 18:06:32 matt Exp $");
47
48#include <sys/param.h>
49#include <sys/bus.h>
50#include <sys/device.h>
51#include <sys/errno.h>
52#include <sys/extent.h>
53#include <sys/intr.h>
54#include <sys/malloc.h>
55#include <sys/queue.h>
56#include <sys/systm.h>
57#include <sys/time.h>
58
59#include <uvm/uvm.h>
60
61#define _POWERPC_BUS_DMA_PRIVATE
62
63#include <dev/ic/cpc700reg.h>
64
65#include <machine/pmppc.h>
66#include <machine/pio.h>
67
68#include <dev/pci/pcivar.h>
69#include <dev/pci/pcireg.h>
70#include <dev/pci/pcidevs.h>
71#include <dev/pci/pciconf.h>
72
73#include <evbppc/pmppc/dev/mainbus.h>
74
75/*
76 * Address conversion as seen from a PCI master.
77 * XXX Shouldn't use 0x80000000, the actual value
78 * should come from the BAR.
79 */
80#define PHYS_TO_PCI_MEM(x)	((x) + 0x80000000)
81#define PCI_MEM_TO_PHYS(x)	((x) - 0x80000000)
82
83static bus_addr_t phys_to_pci(bus_dma_tag_t, bus_addr_t);
84static bus_addr_t pci_to_phys(bus_dma_tag_t, bus_addr_t);
85
86extern struct powerpc_bus_dma_tag pci_bus_dma_tag;
87
88void
89pmppc_pci_get_chipset_tag(pci_chipset_tag_t pc)
90{
91	pc->pc_conf_v = (void *)pc;
92
93	pc->pc_attach_hook = genppc_pci_indirect_attach_hook;
94	pc->pc_bus_maxdevs = genppc_pci_bus_maxdevs;
95	pc->pc_make_tag = genppc_pci_indirect_make_tag;
96	pc->pc_conf_read = genppc_pci_indirect_conf_read;
97	pc->pc_conf_write = genppc_pci_indirect_conf_write;
98
99	pc->pc_intr_v = (void *)pc;
100
101	pc->pc_intr_map = pmppc_pci_intr_map;
102	pc->pc_intr_string = genppc_pci_intr_string;
103	pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
104	pc->pc_intr_establish = genppc_pci_intr_establish;
105	pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
106	pc->pc_intr_setattr = genppc_pci_intr_setattr;
107
108	pc->pc_conf_interrupt = pmppc_pci_conf_interrupt;
109	pc->pc_decompose_tag = genppc_pci_indirect_decompose_tag;
110	pc->pc_conf_hook = genppc_pci_conf_hook;
111
112	pc->pc_addr = mapiodev(CPC_PCICFGADR, 4, false);
113	pc->pc_data = mapiodev(CPC_PCICFGDATA, 4, false);
114	pc->pc_bus = 0;
115	pc->pc_node = 0;
116	pc->pc_memt = 0;
117	pc->pc_iot = 0;
118
119	/* the following two lines are required because unlike other ports,
120	 * we cannot just add PHYS_TO_BUS_MEM/BUS_MEM_TO_PHYS defines to
121	 * bus.h, because it would impact other evbppc ports.
122	 */
123	pci_bus_dma_tag._dma_phys_to_bus_mem = phys_to_pci;
124	pci_bus_dma_tag._dma_bus_mem_to_phys = pci_to_phys;
125}
126
127
128static bus_addr_t
129phys_to_pci(bus_dma_tag_t t, bus_addr_t a)
130{
131	return PHYS_TO_PCI_MEM(a);
132}
133
134static bus_addr_t pci_to_phys(bus_dma_tag_t t, bus_addr_t a)
135{
136	return PCI_MEM_TO_PHYS(a);
137}
138
139int
140pmppc_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
141{
142	int	pin = pa->pa_intrpin;
143	int	line = pa->pa_intrline;
144
145	if (pin == 0) {
146		/* No IRQ used. */
147		goto bad;
148	}
149
150	if (pin > 4) {
151		printf("pci_intr_map: bad interrupt pin %d\n", pin);
152		goto bad;
153	}
154
155	if (line == 255) {
156		printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
157		goto bad;
158	}
159	/*printf("pci_intr_map pin=%d line=%d\n", pin, line);*/
160
161	switch (line & 3) {	/* XXX what should this be? */
162	case 0: *ihp = PMPPC_I_BPMC_INTA; break;
163	case 1: *ihp = PMPPC_I_BPMC_INTB; break;
164	case 2: *ihp = PMPPC_I_BPMC_INTC; break;
165	case 3: *ihp = PMPPC_I_BPMC_INTD; break;
166	}
167	return 0;
168
169bad:
170	*ihp = -1;
171	return 1;
172}
173
174void
175pmppc_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz,
176    int *iline)
177{
178	int line;
179
180	line = (swiz + dev) & 3;
181	/* XXX UGLY UGLY, figure out the real interrupt mapping */
182	if (bus==3&&dev==2&&pin==1&&swiz==3) line=2;
183/*
184	printf("pci_conf_interrupt: bus=%d dev=%d pin=%d swiz=%d => line=%d\n",
185		bus, dev, pin, swiz, line);
186*/
187	*iline = line;
188}
189