1 /* $NetBSD: bonito_pci.c,v 1.12 2015/10/02 05:22:51 msaitoh Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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 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.
30 */
31
32 /*
33 * PCI configuration space support for the Algorithmics BONITO
34 * MIPS PCI and memory controller.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: bonito_pci.c,v 1.12 2015/10/02 05:22:51 msaitoh Exp $");
39
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/device.h>
43 #include <sys/intr.h>
44 #include <sys/systm.h>
45
46 #include <mips/locore.h>
47
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50
51 #include <mips/bonito/bonitoreg.h>
52 #include <mips/bonito/bonitovar.h>
53
54 /*
55 * Bonito systems are always single-processor, so this is sufficient.
56 */
57 #define PCI_CONF_LOCK(s) (s) = splhigh()
58 #define PCI_CONF_UNLOCK(s) splx((s))
59
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);
67
68 void
bonito_pci_init(pci_chipset_tag_t pc,const struct bonito_config * bc)69 bonito_pci_init(pci_chipset_tag_t pc, const struct bonito_config *bc)
70 {
71
72 pc->pc_conf_v = __UNCONST(bc);
73 if (bc->bc_attach_hook != NULL)
74 pc->pc_attach_hook = bc->bc_attach_hook;
75 else
76 pc->pc_attach_hook = bonito_attach_hook;
77 pc->pc_bus_maxdevs = bonito_bus_maxdevs;
78 pc->pc_make_tag = bonito_make_tag;
79 pc->pc_decompose_tag = bonito_decompose_tag;
80 pc->pc_conf_read = bonito_conf_read;
81 pc->pc_conf_write = bonito_conf_write;
82 }
83
84 void
bonito_attach_hook(device_t parent,device_t self,struct pcibus_attach_args * pba)85 bonito_attach_hook(device_t parent, device_t self,
86 struct pcibus_attach_args *pba)
87 {
88 }
89
90 int
bonito_bus_maxdevs(void * v,int busno)91 bonito_bus_maxdevs(void *v, int busno)
92 {
93
94 return (32);
95 }
96
97 pcitag_t
bonito_make_tag(void * v,int b,int d,int f)98 bonito_make_tag(void *v, int b, int d, int f)
99 {
100
101 return ((b << 16) | (d << 11) | (f << 8));
102 }
103
104 void
bonito_decompose_tag(void * v,pcitag_t tag,int * bp,int * dp,int * fp)105 bonito_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
106 {
107
108 if (bp != NULL)
109 *bp = (tag >> 16) & 0xff;
110 if (dp != NULL)
111 *dp = (tag >> 11) & 0x1f;
112 if (fp != NULL)
113 *fp = (tag >> 8) & 0x7;
114 }
115
116 static bool
bonito_conf_addr(struct bonito_config * bc,pcitag_t tag,int offset,u_int32_t * cfgoff,u_int32_t * pcimap_cfg)117 bonito_conf_addr(struct bonito_config *bc, pcitag_t tag, int offset,
118 u_int32_t *cfgoff, u_int32_t *pcimap_cfg)
119 {
120 int b, d, f;
121
122 if ((unsigned int)offset >= PCI_CONF_SIZE)
123 return true;
124
125 bonito_decompose_tag(bc, tag, &b, &d, &f);
126
127 if (b == 0) {
128 if (d > (31 - bc->bc_adbase))
129 return true;
130 *cfgoff = (1UL << (d + bc->bc_adbase)) | (f << 8) | offset;
131 *pcimap_cfg = 0;
132 } else {
133 *cfgoff = tag | offset;
134 *pcimap_cfg = BONITO_PCIMAPCFG_TYPE1;
135 }
136
137 return false;
138 }
139
140 pcireg_t
bonito_conf_read(void * v,pcitag_t tag,int offset)141 bonito_conf_read(void *v, pcitag_t tag, int offset)
142 {
143 struct bonito_config *bc = v;
144 pcireg_t data;
145 u_int32_t cfgoff, pcimap_cfg;
146 int s;
147
148 if (bonito_conf_addr(bc, tag, offset, &cfgoff, &pcimap_cfg))
149 return ((pcireg_t) -1);
150
151 PCI_CONF_LOCK(s);
152
153 /* clear aborts */
154 REGVAL(BONITO_PCICMD) |=
155 PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
156
157 /* high 16 bits of address go into PciMapCfg register */
158 REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
159
160 wbflush();
161 /* Issue a read to make sure the write is posted */
162 (void)REGVAL(BONITO_PCIMAP_CFG);
163
164 /* low 16 bits of address are offset into config space */
165 data = REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc));
166
167 /* check for error */
168 if (REGVAL(BONITO_PCICMD) &
169 (PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT))
170 data = (pcireg_t) -1;
171
172 PCI_CONF_UNLOCK(s);
173
174 return (data);
175 }
176
177 void
bonito_conf_write(void * v,pcitag_t tag,int offset,pcireg_t data)178 bonito_conf_write(void *v, pcitag_t tag, int offset, pcireg_t data)
179 {
180 struct bonito_config *vt = v;
181 u_int32_t cfgoff, pcimap_cfg;
182 int s;
183
184 if (bonito_conf_addr(vt, tag, offset, &cfgoff, &pcimap_cfg))
185 panic("bonito_conf_write");
186
187 PCI_CONF_LOCK(s);
188
189 /* clear aborts */
190 REGVAL(BONITO_PCICMD) |=
191 PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
192
193 /* high 16 bits of address go into PciMapCfg register */
194 REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
195
196 wbflush();
197 /* Issue a read to make sure the write is posted */
198 (void)REGVAL(BONITO_PCIMAP_CFG);
199
200 /* low 16 bits of address are offset into config space */
201 REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc)) = data;
202
203 PCI_CONF_UNLOCK(s);
204 }
205