xref: /netbsd-src/sys/arch/alpha/pci/lca.c (revision 7663c1deeb1a1eb7d0b8d5910c6bd0391fee5692)
1 /* $NetBSD: lca.c,v 1.58 2023/12/04 00:32:10 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2000 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  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
34  * All rights reserved.
35  *
36  * Authors: Jeffrey Hsu and Chris G. Demetriou
37  *
38  * Permission to use, copy, modify and distribute this software and
39  * its documentation is hereby granted, provided that both the copyright
40  * notice and this permission notice appear in all copies of the
41  * software, derivative works or modified versions, and any portions
42  * thereof, and that both notices appear in supporting documentation.
43  *
44  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
46  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47  *
48  * Carnegie Mellon requests users of this software to return to
49  *
50  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
51  *  School of Computer Science
52  *  Carnegie Mellon University
53  *  Pittsburgh PA 15213-3890
54  *
55  * any improvements or extensions that they make and grant Carnegie the
56  * rights to redistribute these changes.
57  */
58 
59 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
60 
61 __KERNEL_RCSID(0, "$NetBSD: lca.c,v 1.58 2023/12/04 00:32:10 thorpej Exp $");
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/device.h>
67 
68 #include <uvm/uvm_extern.h>
69 
70 #include <machine/autoconf.h>
71 #include <machine/rpb.h>
72 #include <machine/sysarch.h>
73 
74 #include <dev/isa/isareg.h>
75 #include <dev/isa/isavar.h>
76 
77 #include <dev/pci/pcireg.h>
78 #include <dev/pci/pcivar.h>
79 #include <alpha/pci/lcareg.h>
80 #include <alpha/pci/lcavar.h>
81 
82 static int	lcamatch(device_t, cfdata_t, void *);
83 static void	lcaattach(device_t, device_t, void *);
84 
85 CFATTACH_DECL_NEW(lca, sizeof(struct lca_softc),
86     lcamatch, lcaattach, NULL, NULL);
87 
88 extern struct cfdriver lca_cd;
89 
90 static int	lca_bus_get_window(int, int,
91 		    struct alpha_bus_space_translation *);
92 
93 /* There can be only one. */
94 struct lca_config lca_configuration;
95 static int lcafound;
96 
97 static const u_int lca_bcache_sizes[__SHIFTOUT(MEMC_CAR_BCS, MEMC_CAR_BCS)] = {
98 [BCS_64K]	=	64 * 1024,
99 [BCS_128K]	=	128 * 1024,
100 [BCS_256K]	=	256 * 1024,
101 [BCS_512K]	=	512 * 1024,
102 [BCS_1M]	=	1024 * 1024,
103 [BCS_2M]	=	2048 * 1024,
104 /* Other values are reserved and we treat them as 0. */
105 };
106 
107 static int
lcamatch(device_t parent,cfdata_t match,void * aux)108 lcamatch(device_t parent, cfdata_t match, void *aux)
109 {
110 	struct mainbus_attach_args *ma = aux;
111 
112 	/* Make sure that we're looking for a LCA. */
113 	if (strcmp(ma->ma_name, lca_cd.cd_name) != 0)
114 		return (0);
115 
116 	if (lcafound)
117 		return (0);
118 
119 	return (1);
120 }
121 
122 /*
123  * Probe the memory controller's Bcache configuration.
124  */
125 void
lca_probe_bcache(void)126 lca_probe_bcache(void)
127 {
128 	const uint64_t car = REGVAL64(LCA_MEMC_CAR);
129 
130 	if (lca_configuration.lc_bcache_size != 0) {
131 		/* Already done. */
132 		return;
133 	}
134 
135 	if (car & MEMC_CAR_BCE) {
136 		lca_configuration.lc_bcache_size =
137 		    lca_bcache_sizes[__SHIFTOUT(car, MEMC_CAR_BCS)];
138 	} else {
139 		lca_configuration.lc_bcache_size = 0;
140 	}
141 
142 	if (lca_configuration.lc_bcache_size) {
143 		uvmexp.ncolors = atop(lca_configuration.lc_bcache_size);
144 	}
145 }
146 
147 /*
148  * Set up the chipset's function pointers.
149  */
150 void
lca_init(struct lca_config * lcp)151 lca_init(struct lca_config *lcp)
152 {
153 
154 	/*
155 	 * The LCA HAE register is WRITE-ONLY, so we can't tell where
156 	 * the second sparse window is actually mapped.  Therefore,
157 	 * we have to guess where it is.  This seems to be the normal
158 	 * address.
159 	 */
160 	lcp->lc_s_mem_w2_masked_base = 0x80000000;
161 
162 	if (!lcp->lc_initted) {
163 		/* don't do these twice since they set up extents */
164 		lca_bus_io_init(&lcp->lc_iot, lcp);
165 		lca_bus_mem_init(&lcp->lc_memt, lcp);
166 
167 		/*
168 		 * We have 1 I/O window and 3 MEM windows.
169 		 */
170 		alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_IO] = 1;
171 		alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_MEM] = 3;
172 		alpha_bus_get_window = lca_bus_get_window;
173 	}
174 
175 	lca_pci_init(&lcp->lc_pc, lcp);
176 	alpha_pci_chipset = &lcp->lc_pc;
177 
178 	/*
179 	 * Refer to ``DECchip 21066 and DECchip 21068 Alpha AXP Microprocessors
180 	 * Hardware Reference Manual''.
181 	 * ...
182 	 */
183 
184 	/*
185 	 * According to section 6.4.1, all bits of the IOC_HAE register are
186 	 * undefined after reset.  Bits <31:27> are write-only.  However, we
187 	 * cannot blindly set it to zero.  The serial ROM code that initializes
188 	 * the PCI devices' address spaces, allocates sparse memory blocks in
189 	 * the range that must use the IOC_HAE register for address translation,
190 	 * and sets this register accordingly (see section 6.4.14).
191 	 *
192 	 *	IOC_HAE left AS IS.
193 	 */
194 
195 	/* According to section 6.4.2, all bits of the IOC_CONF register are
196 	 * undefined after reset.  Bits <1:0> are write-only.  Set them to
197 	 * 0x00 for PCI Type 0 configuration access.
198 	 *
199 	 *	IOC_CONF set to ZERO.
200 	 */
201 	REGVAL64(LCA_IOC_CONF) = 0;
202 
203 	lcp->lc_initted = 1;
204 }
205 
206 static void
lcaattach(device_t parent,device_t self,void * aux)207 lcaattach(device_t parent, device_t self, void *aux)
208 {
209 	struct lca_softc *sc = device_private(self);
210 	struct lca_config *lcp;
211 	struct pcibus_attach_args pba;
212 
213 	/* note that we've attached the chipset; can't have 2 LCAs. */
214 	lcafound = 1;
215 	sc->sc_dev = self;
216 
217 	/*
218 	 * set up the chipset's info; done once at console init time
219 	 * (maybe), but we must do it twice to take care of things
220 	 * that need to use memory allocation.
221 	 */
222 	lcp = sc->sc_lcp = &lca_configuration;
223 	lca_init(lcp);
224 
225 	/* XXX print chipset information */
226 	aprint_normal("\n");
227 	if (lcp->lc_bcache_size != 0) {
228 		char buf[sizeof("256 KB")];
229 		if (format_bytes(buf, sizeof(buf), lcp->lc_bcache_size) > 0) {
230 			aprint_normal_dev(self, "%s Bcache detected\n", buf);
231 		}
232 	}
233 
234 	lca_dma_init(lcp);
235 
236 	alpha_pci_intr_init(lcp, &lcp->lc_iot, &lcp->lc_memt, &lcp->lc_pc);
237 
238 	pba.pba_iot = &lcp->lc_iot;
239 	pba.pba_memt = &lcp->lc_memt;
240 	pba.pba_dmat =
241 	    alphabus_dma_get_tag(&lcp->lc_dmat_direct, ALPHA_BUS_PCI);
242 	pba.pba_dmat64 = NULL;
243 	pba.pba_pc = &lcp->lc_pc;
244 	pba.pba_bus = 0;
245 	pba.pba_bridgetag = NULL;
246 	pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY |
247 	    PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
248 	config_found(self, &pba, pcibusprint, CFARGS_NONE);
249 }
250 
251 static int
lca_bus_get_window(int type,int window,struct alpha_bus_space_translation * abst)252 lca_bus_get_window(int type, int window,
253     struct alpha_bus_space_translation *abst)
254 {
255 	struct lca_config *lcp = &lca_configuration;
256 	bus_space_tag_t st;
257 
258 	switch (type) {
259 	case ALPHA_BUS_TYPE_PCI_IO:
260 		st = &lcp->lc_iot;
261 		break;
262 
263 	case ALPHA_BUS_TYPE_PCI_MEM:
264 		st = &lcp->lc_memt;
265 		break;
266 
267 	default:
268 		panic("lca_bus_get_window");
269 	}
270 
271 	return (alpha_bus_space_get_window(st, window, abst));
272 }
273