No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / mips / bonito / bonito_pci.c
blob869476e7846103520716b5a70715b9b5dda85207
1 /* $NetBSD: bonito_pci.c,v 1.6 2009/08/06 00:42:22 matt Exp $ */
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * PCI configuration space support for the Algorithmics BONITO
34 * MIPS PCI and memory controller.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: bonito_pci.c,v 1.6 2009/08/06 00:42:22 matt Exp $");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46 #include <machine/locore.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
51 #include <mips/bonito/bonitoreg.h>
52 #include <mips/bonito/bonitovar.h>
55 * Bonito systems are always single-processor, so this is sufficient.
57 #define PCI_CONF_LOCK(s) (s) = splhigh()
58 #define PCI_CONF_UNLOCK(s) splx((s))
60 void bonito_attach_hook(device_t, device_t,
61 struct pcibus_attach_args *);
62 int bonito_bus_maxdevs(void *, int);
63 pcitag_t bonito_make_tag(void *, int, int, int);
64 void bonito_decompose_tag(void *, pcitag_t, int *, int *, int *);
65 pcireg_t bonito_conf_read(void *, pcitag_t, int);
66 void bonito_conf_write(void *, pcitag_t, int, pcireg_t);
68 void
69 bonito_pci_init(pci_chipset_tag_t pc, struct bonito_config *bc)
72 pc->pc_conf_v = bc;
73 pc->pc_attach_hook = bonito_attach_hook;
74 pc->pc_bus_maxdevs = bonito_bus_maxdevs;
75 pc->pc_make_tag = bonito_make_tag;
76 pc->pc_decompose_tag = bonito_decompose_tag;
77 pc->pc_conf_read = bonito_conf_read;
78 pc->pc_conf_write = bonito_conf_write;
81 void
82 bonito_attach_hook(device_t parent, device_t self,
83 struct pcibus_attach_args *pba)
87 int
88 bonito_bus_maxdevs(void *v, int busno)
91 return (32);
94 pcitag_t
95 bonito_make_tag(void *v, int b, int d, int f)
98 return ((b << 16) | (d << 11) | (f << 8));
101 void
102 bonito_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
105 if (bp != NULL)
106 *bp = (tag >> 16) & 0xff;
107 if (dp != NULL)
108 *dp = (tag >> 11) & 0x1f;
109 if (fp != NULL)
110 *fp = (tag >> 8) & 0x7;
113 static bool
114 bonito_conf_addr(struct bonito_config *bc, pcitag_t tag, int offset,
115 u_int32_t *cfgoff, u_int32_t *pcimap_cfg)
117 int b, d, f;
119 bonito_decompose_tag(bc, tag, &b, &d, &f);
121 if (b == 0) {
122 if (d > (31 - bc->bc_adbase))
123 return true;
124 *cfgoff = (1UL << (d + bc->bc_adbase)) | (f << 8) | offset;
125 *pcimap_cfg = 0;
126 } else {
127 *cfgoff = tag | offset;
128 *pcimap_cfg = BONITO_PCIMAPCFG_TYPE1;
131 return false;
134 pcireg_t
135 bonito_conf_read(void *v, pcitag_t tag, int offset)
137 struct bonito_config *bc = v;
138 pcireg_t data;
139 u_int32_t cfgoff, dummy, pcimap_cfg;
140 int s;
142 if (bonito_conf_addr(bc, tag, offset, &cfgoff, &pcimap_cfg))
143 return ((pcireg_t) -1);
145 PCI_CONF_LOCK(s);
147 /* clear aborts */
148 REGVAL(BONITO_PCICMD) |=
149 PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
151 /* high 16 bits of address go into PciMapCfg register */
152 REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
154 wbflush();
155 /* Issue a read to make sure the write is posted */
156 dummy = REGVAL(BONITO_PCIMAP_CFG);
158 /* low 16 bits of address are offset into config space */
159 data = REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc));
161 /* check for error */
162 if (REGVAL(BONITO_PCICMD) &
163 (PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT))
164 data = (pcireg_t) -1;
166 PCI_CONF_UNLOCK(s);
168 return (data);
171 void
172 bonito_conf_write(void *v, pcitag_t tag, int offset, pcireg_t data)
174 struct bonito_config *vt = v;
175 u_int32_t cfgoff, dummy, pcimap_cfg;
176 int s;
178 if (bonito_conf_addr(vt, tag, offset, &cfgoff, &pcimap_cfg))
179 panic("bonito_conf_write");
181 PCI_CONF_LOCK(s);
183 /* clear aborts */
184 REGVAL(BONITO_PCICMD) |=
185 PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
187 /* high 16 bits of address go into PciMapCfg register */
188 REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
190 wbflush();
191 /* Issue a read to make sure the write is posted */
192 dummy = REGVAL(BONITO_PCIMAP_CFG);
194 /* low 16 bits of address are offset into config space */
195 REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc)) = data;
197 PCI_CONF_UNLOCK(s);