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