1 /* $NetBSD: em4k.c,v 1.9 2023/12/20 00:40:42 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2013 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 4000 driver. */
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/em4kvar.h>
50 #include <amiga/pci/emmemvar.h>
51
52 #include <dev/pci/pciconf.h>
53
54 #include "opt_pci.h"
55
56 static int em4k_match(device_t, cfdata_t, void *);
57 static void em4k_attach(device_t, device_t, void *);
58 static void em4k_callback(device_t);
59
60 static void em4k_find_mem(struct em4k_softc *);
61 static void em4k_intr_enable(struct em4k_softc *);
62
63 pcireg_t em4k_pci_conf_read(pci_chipset_tag_t, pcitag_t, int);
64 void em4k_pci_conf_write(pci_chipset_tag_t, pcitag_t, int, pcireg_t);
65 int em4k_pci_bus_maxdevs(pci_chipset_tag_t, int);
66 void em4k_pci_attach_hook(device_t, device_t,
67 struct pcibus_attach_args *);
68 pcitag_t em4k_pci_make_tag(pci_chipset_tag_t, int, int, int);
69 void em4k_pci_decompose_tag(pci_chipset_tag_t, pcitag_t,
70 int *, int *, int *);
71 int em4k_pci_intr_map(const struct pci_attach_args *,
72 pci_intr_handle_t *);
73 const struct evcnt * em4k_pci_intr_evcnt(pci_chipset_tag_t,
74 pci_intr_handle_t);
75 int em4k_pci_conf_hook(pci_chipset_tag_t, int, int, int, pcireg_t);
76
77 #ifdef PCI_NETBSD_CONFIGURE
78 static void em4k_pci_configure(struct em4k_softc *sc);
79 #endif /* PCI_NETBSD_CONFIGURE */
80
81 CFATTACH_DECL_NEW(em4k, sizeof(struct em4k_softc),
82 em4k_match, em4k_attach, NULL, NULL);
83
84 static int
em4k_match(device_t parent,cfdata_t cf,void * aux)85 em4k_match(device_t parent, cfdata_t cf, void *aux)
86 {
87 struct zbus_args *zap;
88
89 zap = aux;
90
91 if (zap->manid != ZORRO_MANID_ELBOX)
92 return 0;
93
94 switch (zap->prodid) {
95 case ZORRO_PRODID_MED4K:
96 case ZORRO_PRODID_MED4KMKII:
97 return 1;
98 }
99
100 return 0;
101 }
102
103
104 static void
em4k_attach(device_t parent,device_t self,void * aux)105 em4k_attach(device_t parent, device_t self, void *aux)
106 {
107 struct em4k_softc *sc;
108 struct zbus_args *zap;
109
110 volatile char *ba;
111
112 zap = aux;
113 sc = device_private(self);
114 sc->sc_dev = self;
115 ba = zap->va;
116
117 sc->model = zap->prodid;
118
119 switch (sc->model) {
120 case ZORRO_PRODID_MED4K:
121 aprint_normal(": ELBOX Mediator PCI 4000\n");
122 break;
123 case ZORRO_PRODID_MED4KMKII:
124 aprint_normal(": ELBOX Mediator PCI 4000 MK-II\n");
125 break;
126 default:
127 aprint_normal(": ELBOX Mediator PCI (unknown)\n");
128 break;
129 }
130
131 /* Setup bus space mappings. */
132 sc->pci_conf_area.base = (bus_addr_t) ba + EM4K_CONF_OFF;
133 sc->pci_conf_area.absm = &amiga_bus_stride_1swap;
134
135 sc->pci_io_area.base = (bus_addr_t) ba + EM4K_IO_OFF;
136 sc->pci_io_area.absm = &amiga_bus_stride_1swap;
137
138 sc->setup_area.base = (bus_addr_t) ba + EM4K_SETUP_OFF;
139 sc->setup_area.absm = &amiga_bus_stride_1;
140
141 /*
142 * Defer everything until later, we need to wait for possible
143 * emmem attachments.
144 */
145
146 config_defer(self, em4k_callback);
147 }
148
149 static void
em4k_callback(device_t self)150 em4k_callback(device_t self) {
151
152 struct em4k_softc *sc;
153 pci_chipset_tag_t pc;
154 struct pcibus_attach_args pba;
155
156 sc = device_private(self);
157 pc = &sc->apc;
158
159 #ifdef EM4K_DEBUG
160 aprint_normal("em4k: mapped setup %x->%x, conf %x->%x, io %x->%x\n",
161 kvtop((void*) sc->setup_area.base), sc->setup_area.base,
162 kvtop((void*) sc->pci_conf_area.base), sc->pci_conf_area.base,
163 kvtop((void*) sc->pci_io_area.base), sc->pci_io_area.base);
164 #endif
165
166 sc->pci_conf_t = &(sc->pci_conf_area);
167 sc->pci_io_t = &(sc->pci_io_area);
168
169 if (bus_space_map(sc->pci_conf_t, 0, EM4K_CONF_SIZE, 0,
170 &sc->pci_conf_h))
171 aprint_error_dev(self, "couldn't map PCI config space\n");
172
173 if (bus_space_map(sc->pci_io_t, 0, EM4K_IO_SIZE, 0,
174 &sc->pci_io_h))
175 aprint_error_dev(self, "couldn't map PCI I/O space\n");
176
177 sc->apc.pci_conf_datat = sc->pci_conf_t;
178 sc->apc.pci_conf_datah = sc->pci_conf_h;
179
180 sc->setup_area_t = &(sc->setup_area);
181
182 if (bus_space_map(sc->setup_area_t, 0, EM4K_SETUP_SIZE, 0,
183 &sc->setup_area_h))
184 aprint_error_dev(self,
185 "couldn't map Mediator setup space\n");
186
187 em4k_find_mem(sc);
188 if (sc->pci_mem_win_size == 0)
189 aprint_error_dev(self,
190 "couldn't find memory space, this shouldn't happen!\n");
191
192 /* Initialize the PCI chipset tag. */
193 sc->apc.pc_conf_v = (void*) pc;
194 sc->apc.pc_bus_maxdevs = em4k_pci_bus_maxdevs;
195 sc->apc.pc_make_tag = amiga_pci_make_tag;
196 sc->apc.pc_decompose_tag = amiga_pci_decompose_tag;
197 sc->apc.pc_conf_read = em4k_pci_conf_read;
198 sc->apc.pc_conf_write = em4k_pci_conf_write;
199 sc->apc.pc_attach_hook = em4k_pci_attach_hook;
200
201 sc->apc.pc_intr_map = em4k_pci_intr_map;
202 sc->apc.pc_intr_string = amiga_pci_intr_string;
203 sc->apc.pc_intr_establish = amiga_pci_intr_establish;
204 sc->apc.pc_intr_disestablish = amiga_pci_intr_disestablish;
205
206 sc->apc.pc_conf_hook = em4k_pci_conf_hook;
207 sc->apc.pc_conf_interrupt = amiga_pci_conf_interrupt;
208
209 sc->apc.cookie = sc;
210
211 #ifdef PCI_NETBSD_CONFIGURE
212 em4k_pci_configure(sc);
213 #endif /* PCI_NETBSD_CONFIGURE */
214
215 pba.pba_iot = &(sc->pci_io_area);
216 pba.pba_dmat = NULL;
217 pba.pba_dmat64 = NULL;
218 pba.pba_pc = pc;
219 pba.pba_flags = PCI_FLAGS_IO_OKAY;
220
221 if (sc->pci_mem_win_size > 0) {
222 pba.pba_memt = &(sc->pci_mem_win);
223 pba.pba_flags |= PCI_FLAGS_MEM_OKAY;
224 } else
225 pba.pba_memt = NULL;
226
227 pba.pba_bus = 0;
228 pba.pba_bridgetag = NULL;
229
230 /* Set correct window position. */
231 bus_space_write_1(sc->setup_area_t, sc->setup_area_h,
232 EM4K_SETUP_WINDOW_OFF, kvtop((void*) sc->pci_mem_win.base) >>
233 EM4K_WINDOW_SHIFT);
234
235 em4k_intr_enable(sc);
236
237 config_found(self, &pba, pcibusprint, CFARGS_NONE);
238 }
239
240 #ifdef PCI_NETBSD_CONFIGURE
241 static void
em4k_pci_configure(struct em4k_softc * sc)242 em4k_pci_configure(struct em4k_softc *sc)
243 {
244 struct pciconf_resources *pcires;
245
246 pcires = pciconf_resource_init();
247
248 /* I/O addresses are relative to I/O space address. */
249 pciconf_resource_add(pcires, PCICONF_RESOURCE_IO, 0, EM4K_IO_SIZE);
250
251 /*
252 * Memory space addresses are absolute (and keep in mind that
253 * they are in a separate address space.
254 */
255 pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
256 kvtop((void*) sc->pci_mem_win.base), sc->pci_mem_win_size);
257
258 pci_configure_bus(&sc->apc, pcires, 0, CACHELINE_SIZE);
259
260 pciconf_resource_fini(pcires);
261 }
262 #endif /* PCI_NETBSD_CONFIGURE */
263
264 static void
em4k_intr_enable(struct em4k_softc * sc)265 em4k_intr_enable(struct em4k_softc *sc)
266 {
267 bus_space_write_1(sc->setup_area_t, sc->setup_area_h,
268 EM4K_SETUP_INTR_OFF, EMPB_INTR_ENABLE);
269 }
270
271 /*
272 * Try to find a (optional) memory window board.
273 */
274 static void
em4k_find_mem(struct em4k_softc * sc)275 em4k_find_mem(struct em4k_softc *sc)
276 {
277 device_t memdev;
278 struct emmem_softc *mem_sc;
279
280 memdev = device_find_by_xname("emmem0");
281 sc->pci_mem_win_size = 0;
282
283 if (memdev == NULL) {
284 return;
285 }
286
287 mem_sc = device_private(memdev);
288
289 sc->pci_mem_win.base = (bus_addr_t) mem_sc->sc_base;
290 sc->pci_mem_win.absm = &amiga_bus_stride_1swap_abs;
291 sc->pci_mem_win_size = mem_sc->sc_size;
292 sc->pci_mem_win_t = &sc->pci_mem_win;
293
294 if (sc->pci_mem_win_size == 512*1024*1024)
295 sc->pci_mem_win_mask = EM4K_WINDOW_MASK_512M;
296 else if (sc->pci_mem_win_size == 256*1024*1024)
297 sc->pci_mem_win_mask = EM4K_WINDOW_MASK_256M;
298 else /* disable anyway */
299 sc->pci_mem_win_size = 0;
300
301 #ifdef EM4K_DEBUG
302 aprint_normal("em4k: found %x b window at %p, switch mask %x\n",
303 sc->pci_mem_win_size, (void*) sc->pci_mem_win.base,
304 sc->pci_mem_win_mask);
305 #endif /* EM4K_DEBUG */
306
307 }
308
309 pcireg_t
em4k_pci_conf_read(pci_chipset_tag_t pc,pcitag_t tag,int reg)310 em4k_pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
311 {
312 uint32_t data;
313 uint32_t bus, dev, func;
314
315 if ((unsigned int)reg >= PCI_CONF_SIZE)
316 return (pcireg_t) -1;
317
318 pci_decompose_tag(pc, tag, &bus, &dev, &func);
319
320 data = bus_space_read_4(pc->pci_conf_datat, pc->pci_conf_datah,
321 EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg);
322 #ifdef EM4K_DEBUG_CONF
323 aprint_normal("em4k conf read va: %lx, bus: %d, dev: %d, "
324 "func: %d, reg: %d -r-> data %x\n",
325 pc->pci_conf_datah, bus, dev, func, reg, data);
326 #endif /* EM4K_DEBUG_CONF */
327
328 return data;
329 }
330
331 void
em4k_pci_conf_write(pci_chipset_tag_t pc,pcitag_t tag,int reg,pcireg_t val)332 em4k_pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t val)
333 {
334 uint32_t bus, dev, func;
335
336 if ((unsigned int)reg >= PCI_CONF_SIZE)
337 return;
338
339 pci_decompose_tag(pc, tag, &bus, &dev, &func);
340
341 bus_space_write_4(pc->pci_conf_datat, pc->pci_conf_datah,
342 EMPB_CONF_DEV_STRIDE*dev + EMPB_CONF_FUNC_STRIDE*func + reg, val);
343 #ifdef EM4K_DEBUG_CONF
344 aprint_normal("em4k conf write va: %lx, bus: %d, dev: %d, "
345 "func: %d, reg: %d -w-> data %x\n",
346 pc->pci_conf_datah, bus, dev, func, reg, val);
347 #endif /* EM4K_DEBUG_CONF */
348 }
349
350 int
em4k_pci_bus_maxdevs(pci_chipset_tag_t pc,int busno)351 em4k_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
352 {
353 return 6; /* no Mediator with more than 6 slots? */
354 }
355
356 void
em4k_pci_attach_hook(device_t parent,device_t self,struct pcibus_attach_args * pba)357 em4k_pci_attach_hook(device_t parent, device_t self,
358 struct pcibus_attach_args *pba)
359 {
360 }
361
362 int
em4k_pci_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)363 em4k_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
364 {
365 /* TODO: add sanity checking */
366
367 *ihp = EMPB_INT;
368 return 0;
369 }
370
371 const struct evcnt *
em4k_pci_intr_evcnt(pci_chipset_tag_t pc,pci_intr_handle_t ih)372 em4k_pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
373 {
374 /* TODO: implement */
375 return NULL;
376 }
377
378 int
em4k_pci_conf_hook(pci_chipset_tag_t pct,int bus,int dev,int func,pcireg_t id)379 em4k_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func,
380 pcireg_t id)
381 {
382 return PCI_CONF_DEFAULT;
383 }
384