xref: /netbsd-src/sys/arch/mips/bonito/bonito_pci.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: bonito_pci.c,v 1.11 2014/03/11 08:19:45 mrg 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.11 2014/03/11 08:19:45 mrg 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
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
85 bonito_attach_hook(device_t parent, device_t self,
86     struct pcibus_attach_args *pba)
87 {
88 }
89 
90 int
91 bonito_bus_maxdevs(void *v, int busno)
92 {
93 
94 	return (32);
95 }
96 
97 pcitag_t
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
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
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 	bonito_decompose_tag(bc, tag, &b, &d, &f);
123 
124 	if (b == 0) {
125 		if (d > (31 - bc->bc_adbase))
126 			return true;
127 		*cfgoff = (1UL << (d + bc->bc_adbase)) | (f << 8) | offset;
128 		*pcimap_cfg = 0;
129 	} else {
130 		*cfgoff = tag | offset;
131 		*pcimap_cfg = BONITO_PCIMAPCFG_TYPE1;
132 	}
133 
134 	return false;
135 }
136 
137 pcireg_t
138 bonito_conf_read(void *v, pcitag_t tag, int offset)
139 {
140 	struct bonito_config *bc = v;
141 	pcireg_t data;
142 	u_int32_t cfgoff, pcimap_cfg;
143 	int s;
144 
145 	if (bonito_conf_addr(bc, tag, offset, &cfgoff, &pcimap_cfg))
146 		return ((pcireg_t) -1);
147 
148 	PCI_CONF_LOCK(s);
149 
150 	/* clear aborts */
151 	REGVAL(BONITO_PCICMD) |=
152 	    PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
153 
154 	/* high 16 bits of address go into PciMapCfg register */
155 	REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
156 
157 	wbflush();
158 	/* Issue a read to make sure the write is posted */
159 	(void)REGVAL(BONITO_PCIMAP_CFG);
160 
161 	/* low 16 bits of address are offset into config space */
162 	data = REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc));
163 
164 	/* check for error */
165 	if (REGVAL(BONITO_PCICMD) &
166 	    (PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT))
167 		data = (pcireg_t) -1;
168 
169 	PCI_CONF_UNLOCK(s);
170 
171 	return (data);
172 }
173 
174 void
175 bonito_conf_write(void *v, pcitag_t tag, int offset, pcireg_t data)
176 {
177 	struct bonito_config *vt = v;
178 	u_int32_t cfgoff, pcimap_cfg;
179 	int s;
180 
181 	if (bonito_conf_addr(vt, tag, offset, &cfgoff, &pcimap_cfg))
182 		panic("bonito_conf_write");
183 
184 	PCI_CONF_LOCK(s);
185 
186 	/* clear aborts */
187 	REGVAL(BONITO_PCICMD) |=
188 	    PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
189 
190 	/* high 16 bits of address go into PciMapCfg register */
191 	REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
192 
193 	wbflush();
194 	/* Issue a read to make sure the write is posted */
195 	(void)REGVAL(BONITO_PCIMAP_CFG);
196 
197 	/* low 16 bits of address are offset into config space */
198 	REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc)) = data;
199 
200 	PCI_CONF_UNLOCK(s);
201 }
202