apecs_pci.c revision 1.1 1 1.1 cgd /* $NetBSD: apecs_pci.c,v 1.1 1995/06/28 01:25:30 cgd Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1994, 1995 Carnegie-Mellon University.
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Author: Chris G. Demetriou
8 1.1 cgd *
9 1.1 cgd * Permission to use, copy, modify and distribute this software and
10 1.1 cgd * its documentation is hereby granted, provided that both the copyright
11 1.1 cgd * notice and this permission notice appear in all copies of the
12 1.1 cgd * software, derivative works or modified versions, and any portions
13 1.1 cgd * thereof, and that both notices appear in supporting documentation.
14 1.1 cgd *
15 1.1 cgd * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 1.1 cgd * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 1.1 cgd * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 1.1 cgd *
19 1.1 cgd * Carnegie Mellon requests users of this software to return to
20 1.1 cgd *
21 1.1 cgd * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 1.1 cgd * School of Computer Science
23 1.1 cgd * Carnegie Mellon University
24 1.1 cgd * Pittsburgh PA 15213-3890
25 1.1 cgd *
26 1.1 cgd * any improvements or extensions that they make and grant Carnegie the
27 1.1 cgd * rights to redistribute these changes.
28 1.1 cgd */
29 1.1 cgd
30 1.1 cgd #include <sys/param.h>
31 1.1 cgd #include <sys/systm.h>
32 1.1 cgd #include <sys/kernel.h>
33 1.1 cgd #include <sys/device.h>
34 1.1 cgd #include <vm/vm.h>
35 1.1 cgd
36 1.1 cgd #include <machine/autoconf.h>
37 1.1 cgd #include <machine/pio.h>
38 1.1 cgd
39 1.1 cgd #include <dev/isa/isavar.h>
40 1.1 cgd
41 1.1 cgd #include <dev/pci/pcireg.h>
42 1.1 cgd #include <dev/pci/pcivar.h>
43 1.1 cgd #include <alpha/pci/pci_chipset.h>
44 1.1 cgd #include <alpha/pci/apecsreg.h>
45 1.1 cgd
46 1.1 cgd void apecs_setup __P((void));
47 1.1 cgd pcitag_t apecs_make_tag __P((int, int, int));
48 1.1 cgd pcireg_t apecs_conf_read __P((pcitag_t, int));
49 1.1 cgd void apecs_conf_write __P((pcitag_t, int, pcireg_t));
50 1.1 cgd int apecs_map_mem __P((pcitag_t, int, vm_offset_t *, vm_offset_t *));
51 1.1 cgd int apecs_pcidma_map __P((caddr_t, vm_size_t, vm_offset_t *));
52 1.1 cgd void apecs_pcidma_unmap __P((caddr_t, vm_size_t, int, vm_offset_t *));
53 1.1 cgd
54 1.1 cgd struct pci_cs_fcns apecs_p1e_cs_fcns = { /* XXX WHAT'S DIFFERENT? */
55 1.1 cgd apecs_setup,
56 1.1 cgd apecs_make_tag,
57 1.1 cgd apecs_conf_read,
58 1.1 cgd apecs_conf_write,
59 1.1 cgd apecs_map_mem,
60 1.1 cgd apecs_pcidma_map,
61 1.1 cgd apecs_pcidma_unmap,
62 1.1 cgd };
63 1.1 cgd
64 1.1 cgd struct pci_cs_fcns apecs_p2e_cs_fcns = { /* XXX WHAT'S DIFFERENT? */
65 1.1 cgd apecs_setup,
66 1.1 cgd apecs_make_tag,
67 1.1 cgd apecs_conf_read,
68 1.1 cgd apecs_conf_write,
69 1.1 cgd apecs_map_mem,
70 1.1 cgd apecs_pcidma_map,
71 1.1 cgd apecs_pcidma_unmap,
72 1.1 cgd };
73 1.1 cgd
74 1.1 cgd #define REGVAL(r) (*(u_int32_t *)phystok0seg(r))
75 1.1 cgd
76 1.1 cgd void
77 1.1 cgd apecs_setup()
78 1.1 cgd {
79 1.1 cgd
80 1.1 cgd /*
81 1.1 cgd * Set up PCI bus mastering DMA windows on the APECS chip.
82 1.1 cgd *
83 1.1 cgd * What the PROM wants:
84 1.1 cgd * a 1G direct-mapped window that maps the PCI address
85 1.1 cgd * space from 4G -> 5G to memory addresses 0 -> 1G,
86 1.1 cgd * set up in window two.
87 1.1 cgd *
88 1.1 cgd * What we want:
89 1.1 cgd * a 1G direct-mapped window that maps the PCI address
90 1.1 cgd * space from 0 -> 1G to memory addresses 0 -> 1G.
91 1.1 cgd *
92 1.1 cgd * Unless we satisfy the PROM, we can't live through a reboot.
93 1.1 cgd * If we don't do what we want, I have to write more code.
94 1.1 cgd
95 1.1 cgd * So:
96 1.1 cgd * Leave window two alone, map window 1 the way I want it.
97 1.1 cgd *
98 1.1 cgd * XXX verify that windows don't overlap
99 1.1 cgd * XXX be trickier
100 1.1 cgd * XXX magic numbers
101 1.1 cgd */
102 1.1 cgd
103 1.1 cgd #if 0 /* should be routine to dump regs */
104 1.1 cgd printf("old base1 was 0x%x\n", REGVAL(EPIC_PCI_BASE_1));
105 1.1 cgd printf("old mask1 was 0x%x\n", REGVAL(EPIC_PCI_MASK_1));
106 1.1 cgd printf("old tbase1 was 0x%x\n", REGVAL(EPIC_TBASE_1));
107 1.1 cgd
108 1.1 cgd printf("old base2 was 0x%x\n", REGVAL(EPIC_PCI_BASE_2));
109 1.1 cgd printf("old mask2 was 0x%x\n", REGVAL(EPIC_PCI_MASK_2));
110 1.1 cgd printf("old tbase2 was 0x%x\n", REGVAL(EPIC_TBASE_2));
111 1.1 cgd #endif
112 1.1 cgd
113 1.1 cgd #if 0 /* XXX STUPID PROM; MUST LEAVE WINDOW 2 ALONE. See above */
114 1.1 cgd /* Turn off DMA window enables in PCI Base Reg 2. */
115 1.1 cgd REGVAL(EPIC_PCI_BASE_2) = 0;
116 1.1 cgd
117 1.1 cgd /* Set up Translated Base Register 2; translate to sybBus addr 0. */
118 1.1 cgd REGVAL(EPIC_TBASE_2) = 0;
119 1.1 cgd
120 1.1 cgd /* Set up PCI mask register 2; map 1G space. */
121 1.1 cgd REGVAL(EPIC_PCI_MASK_2) = 0x3ff00000;
122 1.1 cgd
123 1.1 cgd /* Enable window 2; from PCI address 4G, direct mapped. */
124 1.1 cgd REGVAL(EPIC_PCI_BASE_2) = 0x40080000;
125 1.1 cgd #endif /* STUPID PROM */
126 1.1 cgd
127 1.1 cgd /* Turn off DMA window enables in PCI Base Reg 1. */
128 1.1 cgd REGVAL(EPIC_PCI_BASE_1) = 0;
129 1.1 cgd
130 1.1 cgd /* Set up Translated Base Register 1; translate to sybBus addr 0. */
131 1.1 cgd { /* XXX */
132 1.1 cgd extern struct sgmapent *sgmap;
133 1.1 cgd REGVAL(EPIC_TBASE_1) = vtophys(sgmap) >> 1;
134 1.1 cgd } /* XXX */
135 1.1 cgd
136 1.1 cgd /* Set up PCI mask register 1; map 8MB space. */
137 1.1 cgd REGVAL(EPIC_PCI_MASK_1) = 0x00700000;
138 1.1 cgd
139 1.1 cgd /* Enable window 1; from PCI address 8MB, direct mapped. */
140 1.1 cgd REGVAL(EPIC_PCI_BASE_1) = 0x008c0000;
141 1.1 cgd
142 1.1 cgd /*
143 1.1 cgd * Should set up HAXR1 and HAXR2... However, the PROM again
144 1.1 cgd * wants them where they're set to be...
145 1.1 cgd */
146 1.1 cgd #if 0
147 1.1 cgd printf("old haxr0 was 0x%x\n", REGVAL(EPIC_HAXR0));
148 1.1 cgd printf("old haxr1 was 0x%x\n", REGVAL(EPIC_HAXR1));
149 1.1 cgd printf("old haxr2 was 0x%x\n", REGVAL(EPIC_HAXR2));
150 1.1 cgd #endif
151 1.1 cgd
152 1.1 cgd #if 0 /* XXX STUPID PROM */
153 1.1 cgd /* HAXR0 is wired zero; no op. */
154 1.1 cgd REGVAL(EPIC_HAXR0) = 0;
155 1.1 cgd
156 1.1 cgd /* HAXR1: maps PCI memory space above 16M. 16M -> 2G+16M. */
157 1.1 cgd REGVAL(EPIC_HAXR1) = 0x80000000;
158 1.1 cgd
159 1.1 cgd /* HAXR2: maps PCI I/O space above 256K. 256K -> 256k. */
160 1.1 cgd REGVAL(EPIC_HAXR2) = 0;
161 1.1 cgd #endif
162 1.1 cgd }
163 1.1 cgd
164 1.1 cgd pcitag_t
165 1.1 cgd apecs_make_tag(bus, device, function)
166 1.1 cgd int bus, device, function;
167 1.1 cgd {
168 1.1 cgd pcitag_t tag;
169 1.1 cgd
170 1.1 cgd if (bus >= 256 || device >= 32 || function >= 8)
171 1.1 cgd panic("apecs_make_tag: bad request");
172 1.1 cgd
173 1.1 cgd tag = (bus << 21) | (device << 16) | (function << 13);
174 1.1 cgd #if 0
175 1.1 cgd printf("apecs_make_tag: bus %d, device %d, function %d -> 0x%lx\n", bus,
176 1.1 cgd device, function, tag);
177 1.1 cgd #endif
178 1.1 cgd return tag;
179 1.1 cgd }
180 1.1 cgd
181 1.1 cgd pcireg_t
182 1.1 cgd apecs_conf_read(tag, offset)
183 1.1 cgd pcitag_t tag;
184 1.1 cgd int offset; /* XXX */
185 1.1 cgd {
186 1.1 cgd pcireg_t *datap, data;
187 1.1 cgd int reg = offset >> 2; /* XXX */
188 1.1 cgd
189 1.1 cgd if ((tag & 0x1fe00000) != 0) {
190 1.1 cgd panic("apecs_conf_read: bus != 0?");
191 1.1 cgd }
192 1.1 cgd /* XXX FILL IN HAXR2 bits. */
193 1.1 cgd
194 1.1 cgd datap = (pcireg_t *)
195 1.1 cgd phystok0seg(APECS_PCI_CONF | tag | reg << 7 | 0 << 5 | 0x3 << 3);
196 1.1 cgd if (badaddr(datap, sizeof *datap))
197 1.1 cgd return ((pcireg_t)-1);
198 1.1 cgd data = *datap;
199 1.1 cgd #if 0
200 1.1 cgd printf("apecs_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p\n", tag, reg,
201 1.1 cgd data, datap);
202 1.1 cgd #endif
203 1.1 cgd return data;
204 1.1 cgd }
205 1.1 cgd
206 1.1 cgd void
207 1.1 cgd apecs_conf_write(tag, offset, data)
208 1.1 cgd pcitag_t tag;
209 1.1 cgd int offset; /* XXX */
210 1.1 cgd pcireg_t data;
211 1.1 cgd {
212 1.1 cgd pcireg_t *datap;
213 1.1 cgd int reg = offset >> 2; /* XXX */
214 1.1 cgd
215 1.1 cgd if ((tag & 0x1fe00000) != 0) {
216 1.1 cgd panic("apecs_conf_read: bus != 0?");
217 1.1 cgd }
218 1.1 cgd /* XXX FILL IN HAXR2 bits. */
219 1.1 cgd
220 1.1 cgd datap = (pcireg_t *)
221 1.1 cgd phystok0seg(APECS_PCI_CONF | tag | reg << 7 | 0 << 5 | 0x3 << 3);
222 1.1 cgd #if 0
223 1.1 cgd printf("apecs_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
224 1.1 cgd reg, data, datap);
225 1.1 cgd #endif
226 1.1 cgd *datap = data;
227 1.1 cgd }
228 1.1 cgd
229 1.1 cgd #ifndef APECS_PCI_PMEM_START
230 1.1 cgd #define APECS_PCI_PMEM_START 0x00100000 /* WAY XXX */
231 1.1 cgd #endif
232 1.1 cgd
233 1.1 cgd vm_offset_t apecs_pci_paddr = APECS_PCI_PMEM_START;
234 1.1 cgd
235 1.1 cgd int
236 1.1 cgd apecs_map_mem(tag, reg, vap, pap)
237 1.1 cgd pcitag_t tag;
238 1.1 cgd int reg;
239 1.1 cgd vm_offset_t *vap, *pap;
240 1.1 cgd {
241 1.1 cgd pcireg_t data;
242 1.1 cgd vm_offset_t pci_pa, sb_pa;
243 1.1 cgd
244 1.1 cgd if (reg < PCI_MAP_REG_START || reg >= PCI_MAP_REG_END || (reg & 3))
245 1.1 cgd panic("apecs_map_mem: bad request");
246 1.1 cgd
247 1.1 cgd /*
248 1.1 cgd * "HERE WE GO AGAIN!!!"
249 1.1 cgd *
250 1.1 cgd * The PROM has already mapped the device for us. The PROM is
251 1.1 cgd * our friend. We wouldn't want to make the PROM unhappy.
252 1.1 cgd *
253 1.1 cgd * So, we take the address that's been assigned (already) to
254 1.1 cgd * the register, and figure out what physical and virtual addresses
255 1.1 cgd * go with it...
256 1.1 cgd */
257 1.1 cgd /*
258 1.1 cgd * Section 6.2.5.1, `Address Maps', says that a device which wants 2^n
259 1.1 cgd * bytes of memory will hardwire the bottom n bits of the address to 0.
260 1.1 cgd * As recommended, we write all 1s and see what we get back.
261 1.1 cgd */
262 1.1 cgd data = pci_conf_read(tag, reg);
263 1.1 cgd
264 1.1 cgd if (data & PCI_MAP_IO)
265 1.1 cgd panic("apecs_map_mem: attempt to memory map an I/O region");
266 1.1 cgd
267 1.1 cgd switch (data & PCI_MAP_MEMORY_TYPE_MASK) {
268 1.1 cgd case PCI_MAP_MEMORY_TYPE_32BIT:
269 1.1 cgd break;
270 1.1 cgd case PCI_MAP_MEMORY_TYPE_32BIT_1M:
271 1.1 cgd printf("apecs_map_mem: attempt to map restricted 32-bit region\n");
272 1.1 cgd return EOPNOTSUPP;
273 1.1 cgd case PCI_MAP_MEMORY_TYPE_64BIT:
274 1.1 cgd printf("apecs_map_mem: attempt to map 64-bit region\n");
275 1.1 cgd return EOPNOTSUPP;
276 1.1 cgd default:
277 1.1 cgd printf("apecs_map_mem: reserved mapping type\n");
278 1.1 cgd return EINVAL;
279 1.1 cgd }
280 1.1 cgd
281 1.1 cgd if (data & PCI_MAP_MEMORY_CACHABLE) {
282 1.1 cgd /* XXX probably should impact mapping location? */
283 1.1 cgd printf("apecs_map_mem: memory wants to be cacheable.\n");
284 1.1 cgd }
285 1.1 cgd
286 1.1 cgd /* figure out where it was mapped... */
287 1.1 cgd pci_pa = data & PCI_MAP_MEMORY_ADDRESS_MASK; /* PCI bus address */
288 1.1 cgd sb_pa = ((data & 0x7fffffc) << 5) | (2L << 32); /* sysBus address */
289 1.1 cgd
290 1.1 cgd /* and tell the driver. */
291 1.1 cgd *vap = phystok0seg(sb_pa);
292 1.1 cgd *pap = pci_pa;
293 1.1 cgd
294 1.1 cgd #if 0
295 1.1 cgd printf("pci_map_mem: memory mapped at 0x%lx\n", *pap);
296 1.1 cgd printf("pci_map_mem: virtual 0x%lx\n", *vap);
297 1.1 cgd #endif
298 1.1 cgd
299 1.1 cgd return 0;
300 1.1 cgd }
301 1.1 cgd
302 1.1 cgd int
303 1.1 cgd apecs_pcidma_map(addr, size, mappings)
304 1.1 cgd caddr_t addr;
305 1.1 cgd vm_size_t size;
306 1.1 cgd vm_offset_t *mappings;
307 1.1 cgd {
308 1.1 cgd vm_offset_t va;
309 1.1 cgd long todo;
310 1.1 cgd int i;
311 1.1 cgd
312 1.1 cgd i = 0;
313 1.1 cgd va = (vm_offset_t)addr;
314 1.1 cgd todo = size;
315 1.1 cgd
316 1.1 cgd while (todo > 0) {
317 1.1 cgd mappings[i] = vtophys(va) | 0x40000000;
318 1.1 cgd #if 0
319 1.1 cgd printf("a_pd_m mapping %d: %lx -> %lx -> %lx\n", i, va,
320 1.1 cgd vtophys(va), mappings[i]);
321 1.1 cgd #endif
322 1.1 cgd i++;
323 1.1 cgd todo -= PAGE_SIZE - (va - trunc_page(va));
324 1.1 cgd va += PAGE_SIZE - (va - trunc_page(va));
325 1.1 cgd }
326 1.1 cgd return (i);
327 1.1 cgd }
328 1.1 cgd
329 1.1 cgd void
330 1.1 cgd apecs_pcidma_unmap(addr, size, nmappings, mappings)
331 1.1 cgd caddr_t addr;
332 1.1 cgd vm_size_t size;
333 1.1 cgd int nmappings;
334 1.1 cgd vm_offset_t *mappings;
335 1.1 cgd {
336 1.1 cgd
337 1.1 cgd /* maybe XXX if diagnostic, check that mapping happened. */
338 1.1 cgd printf("apecs_pcidma_unmap: nada\n");
339 1.1 cgd }
340