empb.c revision 1.15 1 /* $NetBSD: empb.c,v 1.15 2021/08/07 16:18:42 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Radoslaw Kujawa.
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 /* Elbox Mediator PCI bridge driver. Currently supports Mediator 1200 models.*/
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 #include <sys/kmem.h>
42
43 #include <uvm/uvm_extern.h>
44
45 #include <machine/bus.h>
46 #include <machine/cpu.h>
47
48 #include <amiga/dev/zbusvar.h>
49 #include <amiga/pci/empbreg.h>
50 #include <amiga/pci/empbvar.h>
51 #include <amiga/pci/emmemvar.h>
52 #include <amiga/pci/empmvar.h>
53
54 #include <dev/pci/pciconf.h>
55
56 #include "opt_pci.h"
57
58 /*#define EMPB_DEBUG 1 */
59
60 #define PCI_CONF_LOCK(s) (s) = splhigh()
61 #define PCI_CONF_UNLOCK(s) splx((s))
62
63 #define WINDOW_LOCK(s) (s) = splhigh()
64 #define WINDOW_UNLOCK(s) splx((s))
65
66 static int empb_match(device_t, cfdata_t, void *);
67 static void empb_attach(device_t, device_t, void *);
68 static void empb_callback(device_t);
69
70 static void empb_empm_attach(struct empb_softc *sc);
71 static int empb_empm_print(void *aux, const char *pnp);
72
73 static void empb_find_mem(struct empb_softc *);
74 static void empb_switch_bridge(struct empb_softc *, uint8_t);
75 static void empb_intr_enable(struct empb_softc *);
76
77 pcireg_t empb_pci_conf_read(pci_chipset_tag_t, pcitag_t, int);
78 void empb_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
79 int empb_pci_bus_maxdevs(pci_chipset_tag_t, int);
80 void empb_pci_attach_hook(device_t, device_t,
81 struct pcibus_attach_args *);
82 pcitag_t empb_pci_make_tag(pci_chipset_tag_t, int, int, int);
83 void empb_pci_decompose_tag(pci_chipset_tag_t, pcitag_t,
84 int *, int *, int *);
85 int empb_pci_intr_map(const struct pci_attach_args *,
86 pci_intr_handle_t *);
87 const struct evcnt * empb_pci_intr_evcnt(pci_chipset_tag_t,
88 pci_intr_handle_t);
89 int empb_pci_conf_hook(pci_chipset_tag_t, int, int, int, pcireg_t);
90
91 CFATTACH_DECL_NEW(empb, sizeof(struct empb_softc),
92 empb_match, empb_attach, NULL, NULL);
93
94 static int
95 empb_match(device_t parent, cfdata_t cf, void *aux)
96 {
97 struct zbus_args *zap;
98
99 zap = aux;
100
101 if (zap->manid != ZORRO_MANID_ELBOX)
102 return 0;
103
104 switch (zap->prodid) {
105 case ZORRO_PRODID_MED1K2:
106 case ZORRO_PRODID_MED1K2SX:
107 case ZORRO_PRODID_MED1K2LT2:
108 case ZORRO_PRODID_MED1K2LT4:
109 case ZORRO_PRODID_MED1K2TX:
110 case ZORRO_PRODID_MEDZIV: /* ZIV untested! */
111 return 1;
112 }
113
114 return 0;
115 }
116
117
118 static void
119 empb_attach(device_t parent, device_t self, void *aux)
120 {
121 struct empb_softc *sc;
122 struct zbus_args *zap;
123
124 volatile char *ba;
125
126 zap = aux;
127 sc = device_private(self);
128 sc->sc_dev = self;
129 ba = zap->va;
130
131 sc->model = zap->prodid;
132
133 switch (sc->model) {
134 case ZORRO_PRODID_MED1K2:
135 aprint_normal(": ELBOX Mediator PCI 1200\n");
136 break;
137 case ZORRO_PRODID_MED1K2SX:
138 aprint_normal(": ELBOX Mediator PCI 1200 SX\n");
139 break;
140 case ZORRO_PRODID_MED1K2LT2:
141 aprint_normal(": ELBOX Mediator PCI 1200 LT2\n");
142 break;
143 case ZORRO_PRODID_MED1K2LT4:
144 aprint_normal(": ELBOX Mediator PCI 1200 LT4\n");
145 break;
146 case ZORRO_PRODID_MED1K2TX:
147 aprint_normal(": ELBOX Mediator PCI 1200 TX\n");
148 break;
149 default:
150 aprint_normal(": ELBOX Mediator PCI (unknown)\n");
151 break;
152 }
153
154 /* Setup bus space mappings. */
155 sc->pci_confio_area.base = (bus_addr_t) ba + EMPB_BRIDGE_OFF;
156 sc->pci_confio_area.absm = &amiga_bus_stride_1swap;
157
158 sc->setup_area.base = (bus_addr_t) ba + EMPB_SETUP_OFF;
159 sc->setup_area.absm = &amiga_bus_stride_1;
160
161 /*
162 * Defer everything until later, we need to wait for possible
163 * emmem attachments.
164 */
165
166 config_defer(self, empb_callback);
167 }
168
169 static void
170 empb_callback(device_t self) {
171
172 struct empb_softc *sc;
173 pci_chipset_tag_t pc;
174 struct pcibus_attach_args pba;
175
176 sc = device_private(self);
177 pc = &sc->apc;
178
179 #ifdef EMPB_DEBUG
180 aprint_normal("empb: mapped setup %x->%x, conf/io %x->%x\n",
181 kvtop((void*) sc->setup_area.base), sc->setup_area.base,
182 kvtop((void*) sc->pci_confio_area.base), sc->pci_confio_area.base);
183 #endif
184
185 sc->pci_confio_t = &(sc->pci_confio_area);
186
187 /*
188 * We should not map I/O space here, however we have no choice
189 * since these addresses are shared between configuration space and
190 * I/O space. Not really a problem on m68k, however on PPC...
191 */
192 if (bus_space_map(sc->pci_confio_t, 0, EMPB_BRIDGE_SIZE, 0,
193 &sc->pci_confio_h))
194 aprint_error_dev(self,
195 "couldn't map PCI configuration & I/O space\n");
196
197 sc->apc.pci_conf_datat = sc->pci_confio_t;
198 sc->apc.pci_conf_datah = sc->pci_confio_h;
199
200 sc->setup_area_t = &(sc->setup_area);
201
202 if (bus_space_map(sc->setup_area_t, 0, EMPB_SETUP_SIZE, 0,
203 &sc->setup_area_h))
204 aprint_error_dev(self,
205 "couldn't map Mediator setup space\n");
206
207 empb_find_mem(sc);
208 if (sc->pci_mem_win_size == 0)
209 aprint_error_dev(self,
210 "couldn't find memory space, check your WINDOW jumper\n");
211
212 /* Initialize the PCI chipset tag. */
213 sc->apc.pc_conf_v = (void*) pc;
214 sc->apc.pc_bus_maxdevs = empb_pci_bus_maxdevs;
215 sc->apc.pc_make_tag = amiga_pci_make_tag;
216 sc->apc.pc_decompose_tag = amiga_pci_decompose_tag;
217 sc->apc.pc_conf_read = empb_pci_conf_read;
218 sc->apc.pc_conf_write = empb_pci_conf_write;
219 sc->apc.pc_attach_hook = empb_pci_attach_hook;
220
221 sc->apc.pc_intr_map = empb_pci_intr_map;
222 sc->apc.pc_intr_string = amiga_pci_intr_string;
223 sc->apc.pc_intr_establish = amiga_pci_intr_establish;
224 sc->apc.pc_intr_disestablish = amiga_pci_intr_disestablish;
225
226 sc->apc.pc_conf_hook = empb_pci_conf_hook;
227 sc->apc.pc_conf_interrupt = amiga_pci_conf_interrupt;
228
229 sc->apc.cookie = sc;
230
231 #ifdef PCI_NETBSD_CONFIGURE
232 struct pciconf_resources *pcires = pciconf_resource_init();
233
234 pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
235 0, EMPB_BRIDGE_SIZE);
236 pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
237 EMPB_MEM_BASE, EMPB_MEM_SIZE);
238
239 pci_configure_bus(pc, pcires, 0, CACHELINE_SIZE);
240
241 pciconf_resource_fini(pcires);
242
243 #endif /* PCI_NETBSD_CONFIGURE */
244
245 pba.pba_iot = &(sc->pci_confio_area);
246 pba.pba_dmat = NULL;
247 pba.pba_dmat64 = NULL;
248 pba.pba_pc = pc;
249 pba.pba_flags = PCI_FLAGS_IO_OKAY;
250
251 if(sc->pci_mem_win_size > 0) {
252 pba.pba_memt = &(sc->pci_mem_win);
253 pba.pba_flags |= PCI_FLAGS_MEM_OKAY;
254 } else
255 pba.pba_memt = NULL;
256
257 pba.pba_bus = 0;
258 pba.pba_bridgetag = NULL;
259
260 /* Attach power management on SX and TX models. */
261 switch (sc->model) {
262 case ZORRO_PRODID_MED1K2SX:
263 case ZORRO_PRODID_MED1K2TX:
264 empb_empm_attach(sc);
265 default:
266 break;
267 }
268
269 empb_intr_enable(sc);
270
271 config_found(self, &pba, pcibusprint,
272 CFARGS(.iattr = "pcibus"));
273 }
274
275 static void
276 empb_empm_attach(struct empb_softc *sc)
277 {
278 struct empm_attach_args aa;
279 aa.setup_area_t = sc->setup_area_t;
280
281 config_found(sc->sc_dev, &aa, empb_empm_print,
282 CFARGS(.iattr = "empmdev"));
283 }
284
285 static int
286 empb_empm_print(void *aux, const char *pnp)
287 {
288 if (pnp)
289 aprint_normal("empm at %s", pnp);
290
291 return UNCONF;
292 }
293
294 static void
295 empb_intr_enable(struct empb_softc *sc)
296 {
297 bus_space_write_1(sc->setup_area_t, sc->setup_area_h,
298 EMPB_SETUP_INTR_OFF, EMPB_INTR_ENABLE);
299 }
300
301 /*
302 * Switch between configuration space and I/O space.
303 */
304 static void
305 empb_switch_bridge(struct empb_softc *sc, uint8_t mode)
306 {
307 bus_space_write_1(sc->setup_area_t, sc->setup_area_h,
308 EMPB_SETUP_BRIDGE_OFF, mode);
309 }
310
311
312 /*
313 * Try to find a (optional) memory window board.
314 */
315 static void
316 empb_find_mem(struct empb_softc *sc)
317 {
318 device_t memdev;
319 struct emmem_softc *mem_sc;
320
321 memdev = device_find_by_xname("emmem0");
322 sc->pci_mem_win_size = 0;
323
324 if(memdev == NULL) {
325 return;
326 }
327
328 mem_sc = device_private(memdev);
329
330 sc->pci_mem_win.base = (bus_addr_t) mem_sc->sc_base;
331 sc->pci_mem_win.absm = &empb_bus_swap;
332 sc->pci_mem_win_size = mem_sc->sc_size;
333 sc->pci_mem_win_t = &sc->pci_mem_win;
334
335 if(sc->pci_mem_win_size == 8*1024*1024)
336 sc->pci_mem_win_mask = EMPB_WINDOW_MASK_8M;
337 else if(sc->pci_mem_win_size == 4*1024*1024)
338 sc->pci_mem_win_mask = EMPB_WINDOW_MASK_4M;
339 else /* disable anyway */
340 sc->pci_mem_win_size = 0;
341
342 #ifdef EMPB_DEBUG
343 aprint_normal("empb: found %x b window at %p, switch mask %x\n",
344 sc->pci_mem_win_size, (void*) sc->pci_mem_win.base,
345 sc->pci_mem_win_mask);
346 #endif /* EMPB_DEBUG */
347
348 }
349
350 /*
351 * Switch memory window position. Return PCI mem address seen at the beginning
352 * of window.
353 */
354 bus_addr_t
355 empb_switch_window(struct empb_softc *sc, bus_addr_t address)
356 {
357 int s;
358 uint16_t win_reg;
359 #ifdef EMPB_DEBUG
360 uint16_t rwin_reg;
361 #endif /* EMPB_DEBUG */
362
363 WINDOW_LOCK(s);
364
365 win_reg = bswap16((address >> EMPB_WINDOW_SHIFT)
366 & sc->pci_mem_win_mask);
367
368 bus_space_write_2(sc->setup_area_t, sc->setup_area_h,
369 EMPB_SETUP_WINDOW_OFF, win_reg);
370
371 /* store window pos, like: sc->pci_mem_win_pos = win_reg ? */
372
373 #ifdef EMPB_DEBUG
374 rwin_reg = bus_space_read_2(sc->setup_area_t, sc->setup_area_h,
375 EMPB_SETUP_WINDOW_OFF);
376
377 aprint_normal("empb: access to %p window switch to %x => reg now %x\n",
378 (void*) address, win_reg, rwin_reg);
379 #endif /* EMPB_DEBUG */
380
381 WINDOW_UNLOCK(s);
382
383 return (bus_addr_t)((bswap16(win_reg)) << EMPB_WINDOW_SHIFT);
384 }
385
386
387 pcireg_t
388 empb_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
389 {
390 uint32_t data;
391 uint32_t bus, dev, func;
392 struct empb_softc *sc;
393 int s;
394
395 if ((unsigned int)reg >= PCI_CONF_SIZE)
396 return (pcireg_t) -1;
397
398 sc = pc->cookie;
399
400 pci_decompose_tag(pc, tag, &bus, &dev, &func);
401
402 PCI_CONF_LOCK(s);
403
404 empb_switch_bridge(sc, BRIDGE_CONF);
405
406 data = bus_space_read_4(pc->pci_conf_datat, pc->pci_conf_datah,
407 EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg);
408 #ifdef EMPB_DEBUG_CONF
409 aprint_normal("empb conf read va: %lx, bus: %d, dev: %d, "
410 "func: %d, reg: %d -r-> data %x\n",
411 pc->pci_conf_datah, bus, dev, func, reg, data);
412 #endif /* EMPB_DEBUG_CONF */
413
414 empb_switch_bridge(sc, BRIDGE_IO);
415
416 PCI_CONF_UNLOCK(s);
417
418 return data;
419 }
420
421 void
422 empb_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val)
423 {
424 uint32_t bus, dev, func;
425 struct empb_softc *sc;
426 int s;
427
428 if ((unsigned int)reg >= PCI_CONF_SIZE)
429 return;
430
431 sc = pc->cookie;
432
433 pci_decompose_tag(pc, tag, &bus, &dev, &func);
434
435 PCI_CONF_LOCK(s);
436
437 empb_switch_bridge(sc, BRIDGE_CONF);
438
439 bus_space_write_4(pc->pci_conf_datat, pc->pci_conf_datah,
440 EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg, val);
441 #ifdef EMPB_DEBUG_CONF
442 aprint_normal("empb conf write va: %lx, bus: %d, dev: %d, "
443 "func: %d, reg: %d -w-> data %x\n",
444 pc->pci_conf_datah, bus, dev, func, reg, val);
445 #endif /* EMPB_DEBUG_CONF */
446 empb_switch_bridge(sc, BRIDGE_IO);
447
448 PCI_CONF_UNLOCK(s);
449 }
450
451 int
452 empb_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
453 {
454 return 6; /* no Mediator with more than 6 slots? */
455 }
456
457 void
458 empb_pci_attach_hook(device_t parent, device_t self,
459 struct pcibus_attach_args *pba)
460 {
461 }
462
463 int
464 empb_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
465 {
466 /* TODO: add sanity checking */
467
468 *ihp = EMPB_INT;
469 return 0;
470 }
471
472 const struct evcnt *
473 empb_pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
474 {
475 /* TODO: implement */
476 return NULL;
477 }
478
479 int
480 empb_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func,
481 pcireg_t id)
482 {
483
484 /*
485 * Register information about some known PCI devices with
486 * DMA-able memory.
487 */
488 /*if ((PCI_VENDOR(id) == PCI_VENDOR_3DFX) &&
489 (PCI_PRODUCT(id) >= PCI_PRODUCT_3DFX_VOODOO3))*/
490
491
492 return PCI_CONF_DEFAULT;
493 }
494