cardbus_map.c revision 1.22 1 1.22 itohy /* $NetBSD: cardbus_map.c,v 1.22 2006/06/04 11:49:02 itohy Exp $ */
2 1.1 haya
3 1.1 haya /*
4 1.8 haya * Copyright (c) 1999 and 2000
5 1.1 haya * HAYAKAWA Koichi. All rights reserved.
6 1.1 haya *
7 1.1 haya * Redistribution and use in source and binary forms, with or without
8 1.1 haya * modification, are permitted provided that the following conditions
9 1.1 haya * are met:
10 1.1 haya * 1. Redistributions of source code must retain the above copyright
11 1.1 haya * notice, this list of conditions and the following disclaimer.
12 1.1 haya * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 haya * notice, this list of conditions and the following disclaimer in the
14 1.1 haya * documentation and/or other materials provided with the distribution.
15 1.1 haya * 3. All advertising materials mentioning features or use of this software
16 1.1 haya * must display the following acknowledgement:
17 1.1 haya * This product includes software developed by HAYAKAWA Koichi.
18 1.1 haya * 4. The name of the author may not be used to endorse or promote products
19 1.1 haya * derived from this software without specific prior written permission.
20 1.1 haya *
21 1.1 haya *
22 1.1 haya * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 haya * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 1.1 haya * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 1.1 haya * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 1.1 haya * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 1.1 haya * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 1.1 haya * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 haya * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 1.1 haya * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 1.1 haya * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.1 haya * POSSIBILITY OF SUCH DAMAGE.
33 1.1 haya */
34 1.1 haya
35 1.13 lukem #include <sys/cdefs.h>
36 1.22 itohy __KERNEL_RCSID(0, "$NetBSD: cardbus_map.c,v 1.22 2006/06/04 11:49:02 itohy Exp $");
37 1.1 haya
38 1.1 haya #include <sys/param.h>
39 1.1 haya #include <sys/systm.h>
40 1.1 haya #include <sys/device.h>
41 1.1 haya
42 1.1 haya #include <machine/bus.h>
43 1.1 haya
44 1.1 haya #include <dev/cardbus/cardbusvar.h>
45 1.1 haya
46 1.1 haya #include <dev/pci/pcireg.h> /* XXX */
47 1.1 haya
48 1.1 haya #if defined DEBUG && !defined CARDBUS_MAP_DEBUG
49 1.1 haya #define CARDBUS_MAP_DEBUG
50 1.1 haya #endif
51 1.1 haya
52 1.1 haya #if defined CARDBUS_MAP_DEBUG
53 1.1 haya #define STATIC
54 1.1 haya #define DPRINTF(a) printf a
55 1.1 haya #else
56 1.1 haya #define STATIC static
57 1.1 haya #define DPRINTF(a)
58 1.1 haya #endif
59 1.1 haya
60 1.1 haya
61 1.15 perry static int cardbus_io_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
62 1.1 haya cardbustag_t, int, cardbusreg_t,
63 1.15 perry bus_addr_t *, bus_size_t *, int *);
64 1.15 perry static int cardbus_mem_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
65 1.1 haya cardbustag_t, int, cardbusreg_t,
66 1.15 perry bus_addr_t *, bus_size_t *, int *);
67 1.1 haya
68 1.1 haya /*
69 1.1 haya * static int cardbus_io_find(cardbus_chipset_tag_t cc,
70 1.1 haya * cardbus_function_tag_t cf, cardbustag_t tag,
71 1.1 haya * int reg, cardbusreg_t type, bus_addr_t *basep,
72 1.1 haya * bus_size_t *sizep, int *flagsp)
73 1.11 wiz * This code is stolen from sys/dev/pci_map.c.
74 1.1 haya */
75 1.1 haya static int
76 1.1 haya cardbus_io_find(cc, cf, tag, reg, type, basep, sizep, flagsp)
77 1.8 haya cardbus_chipset_tag_t cc;
78 1.8 haya cardbus_function_tag_t cf;
79 1.8 haya cardbustag_t tag;
80 1.8 haya int reg;
81 1.8 haya cardbusreg_t type;
82 1.8 haya bus_addr_t *basep;
83 1.8 haya bus_size_t *sizep;
84 1.8 haya int *flagsp;
85 1.1 haya {
86 1.8 haya cardbusreg_t address, mask;
87 1.8 haya int s;
88 1.1 haya
89 1.8 haya /* EXT ROM is able to map on memory space ONLY. */
90 1.8 haya if (reg == CARDBUS_ROM_REG) {
91 1.8 haya return 1;
92 1.8 haya }
93 1.8 haya
94 1.8 haya if(reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3)) {
95 1.8 haya panic("cardbus_io_find: bad request");
96 1.8 haya }
97 1.8 haya
98 1.8 haya /*
99 1.8 haya * Section 6.2.5.1, `Address Maps', tells us that:
100 1.8 haya *
101 1.8 haya * 1) The builtin software should have already mapped the device in a
102 1.8 haya * reasonable way.
103 1.8 haya *
104 1.8 haya * 2) A device which wants 2^n bytes of memory will hardwire the bottom
105 1.8 haya * n bits of the address to 0. As recommended, we write all 1s and see
106 1.8 haya * what we get back.
107 1.8 haya */
108 1.8 haya s = splhigh();
109 1.8 haya address = cardbus_conf_read(cc, cf, tag, reg);
110 1.8 haya cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
111 1.8 haya mask = cardbus_conf_read(cc, cf, tag, reg);
112 1.8 haya cardbus_conf_write(cc, cf, tag, reg, address);
113 1.8 haya splx(s);
114 1.8 haya
115 1.8 haya if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
116 1.8 haya printf("cardbus_io_find: expected type i/o, found mem\n");
117 1.8 haya return 1;
118 1.8 haya }
119 1.8 haya
120 1.8 haya if (PCI_MAPREG_IO_SIZE(mask) == 0) {
121 1.8 haya printf("cardbus_io_find: void region\n");
122 1.8 haya return 1;
123 1.8 haya }
124 1.8 haya
125 1.8 haya if (basep != 0) {
126 1.8 haya *basep = PCI_MAPREG_IO_ADDR(address);
127 1.8 haya }
128 1.8 haya if (sizep != 0) {
129 1.8 haya *sizep = PCI_MAPREG_IO_SIZE(mask);
130 1.8 haya }
131 1.8 haya if (flagsp != 0) {
132 1.8 haya *flagsp = 0;
133 1.8 haya }
134 1.1 haya
135 1.8 haya return 0;
136 1.1 haya }
137 1.1 haya
138 1.1 haya
139 1.1 haya
140 1.1 haya /*
141 1.1 haya * static int cardbus_mem_find(cardbus_chipset_tag_t cc,
142 1.1 haya * cardbus_function_tag_t cf, cardbustag_t tag,
143 1.1 haya * int reg, cardbusreg_t type, bus_addr_t *basep,
144 1.1 haya * bus_size_t *sizep, int *flagsp)
145 1.11 wiz * This code is stolen from sys/dev/pci_map.c.
146 1.1 haya */
147 1.1 haya static int
148 1.1 haya cardbus_mem_find(cc, cf, tag, reg, type, basep, sizep, flagsp)
149 1.8 haya cardbus_chipset_tag_t cc;
150 1.8 haya cardbus_function_tag_t cf;
151 1.8 haya cardbustag_t tag;
152 1.8 haya int reg;
153 1.8 haya cardbusreg_t type;
154 1.8 haya bus_addr_t *basep;
155 1.8 haya bus_size_t *sizep;
156 1.8 haya int *flagsp;
157 1.1 haya {
158 1.8 haya cardbusreg_t address, mask;
159 1.8 haya int s;
160 1.1 haya
161 1.16 perry if (reg != CARDBUS_ROM_REG &&
162 1.8 haya (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))) {
163 1.8 haya panic("cardbus_mem_find: bad request");
164 1.8 haya }
165 1.8 haya
166 1.8 haya /*
167 1.8 haya * Section 6.2.5.1, `Address Maps', tells us that:
168 1.8 haya *
169 1.8 haya * 1) The builtin software should have already mapped the device in a
170 1.8 haya * reasonable way.
171 1.8 haya *
172 1.8 haya * 2) A device which wants 2^n bytes of memory will hardwire the bottom
173 1.8 haya * n bits of the address to 0. As recommended, we write all 1s and see
174 1.8 haya * what we get back.
175 1.8 haya */
176 1.8 haya s = splhigh();
177 1.8 haya address = cardbus_conf_read(cc, cf, tag, reg);
178 1.8 haya cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
179 1.8 haya mask = cardbus_conf_read(cc, cf, tag, reg);
180 1.8 haya cardbus_conf_write(cc, cf, tag, reg, address);
181 1.8 haya splx(s);
182 1.8 haya
183 1.8 haya if (reg != CARDBUS_ROM_REG) {
184 1.8 haya /* memory space BAR */
185 1.8 haya
186 1.8 haya if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
187 1.8 haya printf("cardbus_mem_find: expected type mem, found i/o\n");
188 1.8 haya return 1;
189 1.8 haya }
190 1.8 haya if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
191 1.8 haya printf("cardbus_mem_find: expected mem type %08x, found %08x\n",
192 1.8 haya PCI_MAPREG_MEM_TYPE(type),
193 1.8 haya PCI_MAPREG_MEM_TYPE(address));
194 1.8 haya return 1;
195 1.8 haya }
196 1.8 haya }
197 1.8 haya
198 1.8 haya if (PCI_MAPREG_MEM_SIZE(mask) == 0) {
199 1.8 haya printf("cardbus_mem_find: void region\n");
200 1.8 haya return 1;
201 1.8 haya }
202 1.8 haya
203 1.8 haya switch (PCI_MAPREG_MEM_TYPE(address)) {
204 1.8 haya case PCI_MAPREG_MEM_TYPE_32BIT:
205 1.8 haya case PCI_MAPREG_MEM_TYPE_32BIT_1M:
206 1.8 haya break;
207 1.8 haya case PCI_MAPREG_MEM_TYPE_64BIT:
208 1.8 haya printf("cardbus_mem_find: 64-bit memory mapping register\n");
209 1.8 haya return 1;
210 1.8 haya default:
211 1.8 haya printf("cardbus_mem_find: reserved mapping register type\n");
212 1.8 haya return 1;
213 1.8 haya }
214 1.8 haya
215 1.8 haya if (basep != 0) {
216 1.8 haya *basep = PCI_MAPREG_MEM_ADDR(address);
217 1.8 haya }
218 1.8 haya if (sizep != 0) {
219 1.8 haya *sizep = PCI_MAPREG_MEM_SIZE(mask);
220 1.8 haya }
221 1.8 haya if (flagsp != 0) {
222 1.8 haya *flagsp = PCI_MAPREG_MEM_PREFETCHABLE(address) ?
223 1.8 haya BUS_SPACE_MAP_PREFETCHABLE : 0;
224 1.8 haya }
225 1.16 perry
226 1.8 haya return 0;
227 1.1 haya }
228 1.1 haya
229 1.1 haya
230 1.1 haya
231 1.1 haya
232 1.1 haya /*
233 1.4 haya * int cardbus_mapreg_map(struct cardbus_softc *, int, int, cardbusreg_t,
234 1.1 haya * int bus_space_tag_t *, bus_space_handle_t *,
235 1.1 haya * bus_addr_t *, bus_size_t *)
236 1.1 haya * This function maps bus-space on the value of Base Address
237 1.1 haya * Register (BAR) indexed by the argument `reg' (the second argument).
238 1.1 haya * When the value of the BAR is not valid, such as 0x00000000, a new
239 1.1 haya * address should be allocated for the BAR and new address values is
240 1.1 haya * written on the BAR.
241 1.1 haya */
242 1.1 haya int
243 1.4 haya cardbus_mapreg_map(sc, func, reg, type, busflags, tagp, handlep, basep, sizep)
244 1.8 haya struct cardbus_softc *sc;
245 1.8 haya int func, reg, busflags;
246 1.8 haya cardbusreg_t type;
247 1.8 haya bus_space_tag_t *tagp;
248 1.8 haya bus_space_handle_t *handlep;
249 1.8 haya bus_addr_t *basep;
250 1.8 haya bus_size_t *sizep;
251 1.1 haya {
252 1.8 haya cardbus_chipset_tag_t cc = sc->sc_cc;
253 1.8 haya cardbus_function_tag_t cf = sc->sc_cf;
254 1.8 haya bus_space_tag_t bustag;
255 1.1 haya #if rbus
256 1.8 haya rbus_tag_t rbustag;
257 1.1 haya #endif
258 1.20 martin bus_space_handle_t handle;
259 1.8 haya bus_addr_t base;
260 1.8 haya bus_size_t size;
261 1.8 haya int flags;
262 1.8 haya int status = 0;
263 1.22 itohy cardbustag_t tag;
264 1.1 haya
265 1.21 mrg size = 0; /* XXX gcc */
266 1.21 mrg flags = 0; /* XXX gcc */
267 1.21 mrg
268 1.22 itohy tag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
269 1.1 haya
270 1.8 haya DPRINTF(("cardbus_mapreg_map called: %s %x\n", sc->sc_dev.dv_xname,
271 1.1 haya type));
272 1.1 haya
273 1.8 haya if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
274 1.8 haya if (cardbus_io_find(cc, cf, tag, reg, type, &base, &size, &flags)) {
275 1.8 haya status = 1;
276 1.8 haya }
277 1.8 haya bustag = sc->sc_iot;
278 1.1 haya #if rbus
279 1.8 haya rbustag = sc->sc_rbus_iot;
280 1.1 haya #endif
281 1.8 haya } else {
282 1.8 haya if (cardbus_mem_find(cc, cf, tag, reg, type, &base, &size, &flags)){
283 1.8 haya status = 1;
284 1.8 haya }
285 1.8 haya bustag = sc->sc_memt;
286 1.1 haya #if rbus
287 1.8 haya rbustag = sc->sc_rbus_memt;
288 1.1 haya #endif
289 1.8 haya }
290 1.8 haya if (status == 0) {
291 1.1 haya #if rbus
292 1.8 haya bus_addr_t mask = size - 1;
293 1.8 haya if (base != 0) {
294 1.8 haya mask = 0xffffffff;
295 1.8 haya }
296 1.8 haya if ((*cf->cardbus_space_alloc)(cc, rbustag, base, size, mask,
297 1.8 haya size, busflags | flags, &base, &handle)) {
298 1.8 haya panic("io alloc");
299 1.8 haya }
300 1.1 haya #else
301 1.8 haya bus_addr_t start = 0x8300;
302 1.8 haya bus_addr_t end = 0x8400;
303 1.8 haya if (base != 0) {
304 1.8 haya bus_addr_t start = base;
305 1.8 haya bus_addr_t end = base + size;
306 1.8 haya }
307 1.8 haya if (bus_space_alloc(bustag, start, end, size, size, 0, 0, &base, &handle)) {
308 1.8 haya panic("io alloc");
309 1.8 haya }
310 1.1 haya #endif
311 1.8 haya }
312 1.8 haya cardbus_conf_write(cc, cf, tag, reg, base);
313 1.1 haya
314 1.12 jmc DPRINTF(("cardbus_mapreg_map: physaddr %lx\n", (unsigned long)base));
315 1.1 haya
316 1.8 haya if (tagp != 0) {
317 1.8 haya *tagp = bustag;
318 1.8 haya }
319 1.8 haya if (handlep != 0) {
320 1.8 haya *handlep = handle;
321 1.8 haya }
322 1.8 haya if (basep != 0) {
323 1.8 haya *basep = base;
324 1.8 haya }
325 1.8 haya if (sizep != 0) {
326 1.8 haya *sizep = size;
327 1.8 haya }
328 1.8 haya cardbus_free_tag(cc, cf, tag);
329 1.4 haya
330 1.8 haya return 0;
331 1.1 haya }
332 1.1 haya
333 1.1 haya
334 1.1 haya
335 1.1 haya
336 1.1 haya
337 1.8 haya /*
338 1.8 haya * int cardbus_mapreg_unmap(struct cardbus_softc *sc, int func, int reg,
339 1.8 haya * bus_space_tag_t tag, bus_space_handle_t handle,
340 1.8 haya * bus_size_t size)
341 1.8 haya *
342 1.8 haya * This function releases bus-space region and close memory or io
343 1.8 haya * window on the bridge.
344 1.8 haya *
345 1.8 haya * Arguments:
346 1.8 haya * struct cardbus_softc *sc; the pointer to the device structure of cardbus.
347 1.8 haya * int func; the number of function on the device.
348 1.8 haya * int reg; the offset of BAR register.
349 1.8 haya */
350 1.8 haya int
351 1.8 haya cardbus_mapreg_unmap(sc, func, reg, tag, handle, size)
352 1.8 haya struct cardbus_softc *sc;
353 1.8 haya int func, reg;
354 1.8 haya bus_space_tag_t tag;
355 1.8 haya bus_space_handle_t handle;
356 1.8 haya bus_size_t size;
357 1.8 haya {
358 1.8 haya cardbus_chipset_tag_t cc = sc->sc_cc;
359 1.8 haya cardbus_function_tag_t cf = sc->sc_cf;
360 1.8 haya int st = 1;
361 1.8 haya cardbustag_t cardbustag;
362 1.8 haya #if rbus
363 1.10 mycroft rbus_tag_t rbustag;
364 1.8 haya
365 1.8 haya if (sc->sc_iot == tag) {
366 1.8 haya /* bus space is io space */
367 1.9 itohy DPRINTF(("%s: unmap i/o space\n", sc->sc_dev.dv_xname));
368 1.10 mycroft rbustag = sc->sc_rbus_iot;
369 1.10 mycroft } else if (sc->sc_memt == tag) {
370 1.8 haya /* bus space is memory space */
371 1.9 itohy DPRINTF(("%s: unmap mem space\n", sc->sc_dev.dv_xname));
372 1.10 mycroft rbustag = sc->sc_rbus_memt;
373 1.8 haya } else {
374 1.8 haya return 1;
375 1.8 haya }
376 1.8 haya #endif
377 1.8 haya
378 1.17 drochner cardbustag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
379 1.8 haya
380 1.10 mycroft cardbus_conf_write(cc, cf, cardbustag, reg, 0);
381 1.8 haya
382 1.8 haya #if rbus
383 1.10 mycroft (*cf->cardbus_space_free)(cc, rbustag, handle, size);
384 1.8 haya #endif
385 1.8 haya
386 1.8 haya cardbus_free_tag(cc, cf, cardbustag);
387 1.8 haya
388 1.8 haya return st;
389 1.8 haya }
390 1.8 haya
391 1.8 haya
392 1.8 haya
393 1.8 haya
394 1.1 haya
395 1.1 haya /*
396 1.1 haya * int cardbus_save_bar(cardbus_devfunc_t);
397 1.1 haya *
398 1.1 haya * This function saves the Base Address Registers at the CardBus
399 1.1 haya * function denoted by the argument.
400 1.1 haya */
401 1.1 haya int cardbus_save_bar(ct)
402 1.8 haya cardbus_devfunc_t ct;
403 1.1 haya {
404 1.8 haya cardbustag_t tag = Cardbus_make_tag(ct);
405 1.8 haya cardbus_chipset_tag_t cc = ct->ct_cc;
406 1.8 haya cardbus_function_tag_t cf = ct->ct_cf;
407 1.8 haya
408 1.8 haya ct->ct_bar[0] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE0_REG);
409 1.8 haya ct->ct_bar[1] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE1_REG);
410 1.8 haya ct->ct_bar[2] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE2_REG);
411 1.8 haya ct->ct_bar[3] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE3_REG);
412 1.8 haya ct->ct_bar[4] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE4_REG);
413 1.8 haya ct->ct_bar[5] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE5_REG);
414 1.1 haya
415 1.8 haya DPRINTF(("cardbus_save_bar: %x %x\n", ct->ct_bar[0], ct->ct_bar[1]));
416 1.1 haya
417 1.8 haya Cardbus_free_tag(ct, tag);
418 1.1 haya
419 1.8 haya return 0;
420 1.1 haya }
421 1.1 haya
422 1.1 haya
423 1.1 haya
424 1.1 haya /*
425 1.1 haya * int cardbus_restore_bar(cardbus_devfunc_t);
426 1.1 haya *
427 1.1 haya * This function saves the Base Address Registers at the CardBus
428 1.1 haya * function denoted by the argument.
429 1.1 haya */
430 1.1 haya int cardbus_restore_bar(ct)
431 1.8 haya cardbus_devfunc_t ct;
432 1.1 haya {
433 1.8 haya cardbustag_t tag = Cardbus_make_tag(ct);
434 1.8 haya cardbus_chipset_tag_t cc = ct->ct_cc;
435 1.8 haya cardbus_function_tag_t cf = ct->ct_cf;
436 1.8 haya
437 1.8 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, ct->ct_bar[0]);
438 1.8 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, ct->ct_bar[1]);
439 1.8 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, ct->ct_bar[2]);
440 1.8 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, ct->ct_bar[3]);
441 1.8 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, ct->ct_bar[4]);
442 1.8 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, ct->ct_bar[5]);
443 1.8 haya
444 1.8 haya Cardbus_free_tag(ct, tag);
445 1.1 haya
446 1.8 haya return 0;
447 1.1 haya }
448