1/*	$NetBSD: prep_pciconf_direct.c,v 1.10 2016/10/19 00:08:42 nonaka Exp $	*/
2
3/*
4 * Copyright (c) 2002 Klaus J. Klein.  All rights reserved.
5 * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
6 * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by Charles M. Hannum.
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*
35 * Machine-specific functions for PCI autoconfiguration.
36 *
37 * This version is specific to the direct-mapped configuration space access
38 * such as implemented on the IBM 27-82650 PCI Bridge/Memory Controller.
39 */
40
41#include <sys/cdefs.h>
42__KERNEL_RCSID(0, "$NetBSD: prep_pciconf_direct.c,v 1.10 2016/10/19 00:08:42 nonaka Exp $");
43
44#include <sys/types.h>
45#include <sys/param.h>
46#include <sys/time.h>
47#include <sys/systm.h>
48#include <sys/errno.h>
49#include <sys/device.h>
50
51#include <uvm/uvm_extern.h>
52
53#define _POWERPC_BUS_DMA_PRIVATE
54#include <sys/bus.h>
55#include <machine/intr.h>
56#include <machine/platform.h>
57
58#include <powerpc/pio.h>
59
60#include <dev/isa/isavar.h>
61#include <dev/pci/pcivar.h>
62#include <dev/pci/pcireg.h>
63#include <dev/pci/pcidevs.h>
64
65#include <prop/proplib.h>
66
67#ifdef DEBUG
68#define        DPRINTF(x) aprint_debug x
69#else
70#define        DPRINTF(x)
71#endif
72
73/* Physical base address and size of PCI configuration space. */
74#define PCI_DCONF_BASE		0x80800000
75#define	PCI_DCONF_SIZE		0x00800000
76/* Device function selection. */
77#define	PCI_DCONF_FUNC		0x00000700
78#define	PCI_DCONF_FUNC_SHIFT	8
79/* Configuration space register selection. */
80#define	PCI_DCONF_REG		0x000000ff
81#define	PCI_DCONF_REG_SHIFT	0
82/* Device selection; those bits still available. */
83#define	PCI_DCONF_DEV  \
84    (~(~(PCI_DCONF_SIZE - 1) | PCI_DCONF_FUNC | PCI_DCONF_REG))
85
86void prep_pci_direct_attach_hook(device_t, device_t,
87    struct pcibus_attach_args *);
88pcitag_t prep_pci_direct_make_tag(void *, int, int, int);
89pcireg_t prep_pci_direct_conf_read(void *, pcitag_t, int);
90void prep_pci_direct_conf_write(void *, pcitag_t, int, pcireg_t);
91void prep_pci_direct_decompose_tag(void *, pcitag_t, int *, int *, int *);
92
93void
94prep_pci_get_chipset_tag_direct(pci_chipset_tag_t pc)
95{
96
97	pc->pc_conf_v = NULL;
98
99	pc->pc_attach_hook = prep_pci_direct_attach_hook;
100	pc->pc_bus_maxdevs = prep_pci_bus_maxdevs;
101	pc->pc_make_tag = prep_pci_direct_make_tag;
102	pc->pc_conf_read = prep_pci_direct_conf_read;
103	pc->pc_conf_write = prep_pci_direct_conf_write;
104
105	pc->pc_intr_v = NULL;
106
107	pc->pc_intr_map = prep_pci_intr_map;
108	pc->pc_intr_string = genppc_pci_intr_string;
109	pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
110	pc->pc_intr_establish = genppc_pci_intr_establish;
111	pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
112	pc->pc_intr_setattr = genppc_pci_intr_setattr;
113	pc->pc_intr_type = genppc_pci_intr_type;
114	pc->pc_intr_alloc = genppc_pci_intr_alloc;
115	pc->pc_intr_release = genppc_pci_intr_release;
116	pc->pc_intx_alloc = genppc_pci_intx_alloc;
117
118	pc->pc_msi_v = NULL;
119	genppc_pci_chipset_msi_init(pc);
120
121	pc->pc_msix_v = NULL;
122	genppc_pci_chipset_msix_init(pc);
123
124	pc->pc_conf_interrupt = genppc_pci_conf_interrupt;
125	pc->pc_decompose_tag = prep_pci_direct_decompose_tag;
126	pc->pc_conf_hook = prep_pci_conf_hook;
127}
128
129void
130prep_pci_direct_attach_hook(device_t parent, device_t self,
131    struct pcibus_attach_args *pba)
132{
133
134	if (pba->pba_bus == 0)
135		aprint_normal(": direct-mapped configuration space access");
136}
137
138pcitag_t
139prep_pci_direct_make_tag(void *v, int bus, int device, int function)
140{
141	pcitag_t tag;
142
143	if (bus >= 256 || device >= 32 || function >= 8)
144		panic("prep_pci_direct_make_tag: bad request");
145
146	tag = (bus << 16) | (device << 11) | (function << 8);
147
148	return tag;
149}
150
151void
152prep_pci_direct_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp,
153    int *fp)
154{
155
156	if (bp != NULL)
157		*bp = (tag >> 16) & 0xff;
158	if (dp != NULL)
159		*dp = (tag >> 11) & 0x1f;
160	if (fp != NULL)
161		*fp = (tag >> 8) & 0x7;
162	return;
163}
164
165pcireg_t
166prep_pci_direct_conf_read(void *v, pcitag_t tag, int reg)
167{
168	pcireg_t data;
169	int bus, device, function;
170	int s;
171
172	if ((unsigned int)reg >= PCI_CONF_SIZE)
173		return (pcireg_t) ~0;
174
175	prep_pci_direct_decompose_tag(v, tag, &bus, &device, &function);
176
177	if (bus == 0) {
178		/* Check if device selector is within selector range. */
179		if (1 << device & ~PCI_DCONF_DEV) {
180			data = ~0;
181			goto out;
182		}
183		tag = (1 << device) | (function << PCI_DCONF_FUNC_SHIFT);
184	}
185
186	s = splhigh();
187	data = in32rb(PCI_DCONF_BASE | tag | reg);
188	splx(s);
189
190out:
191	return data;
192}
193
194void
195prep_pci_direct_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
196{
197	int bus, device, function;
198	int s;
199
200	DPRINTF(("prep_pci_direct_conf_write(0x%lx, 0x%x, 0x%02x, 0x%08lx) ",
201	    (unsigned long)v, tag, reg, (unsigned long)data));
202
203	if ((unsigned int)reg >= PCI_CONF_SIZE)
204		return;
205
206	prep_pci_direct_decompose_tag(v, tag, &bus, &device, &function);
207
208	if (bus == 0) {
209		/* Check if device selector is within selector range. */
210		if (1 << device & ~PCI_DCONF_DEV) {
211			/* Should never happen. */
212			panic("prep_pci_direct_conf_write: bad device %d",
213			    device);
214		}
215		tag = (1 << device) | (function << PCI_DCONF_FUNC_SHIFT);
216	}
217
218	s = splhigh();
219	out32rb(PCI_DCONF_BASE | tag | reg, data);
220	splx(s);
221}
222