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