cardbus_map.c revision 1.5 1 1.5 joda /* $NetBSD: cardbus_map.c,v 1.5 1999/10/27 14:14:18 joda Exp $ */
2 1.1 haya
3 1.1 haya /*
4 1.1 haya * Copyright (c) 1999
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.1 haya
36 1.3 augustss /* #define CARDBUS_MAP_DEBUG 1 */
37 1.1 haya
38 1.1 haya #include <sys/types.h>
39 1.1 haya #include <sys/param.h>
40 1.1 haya #include <sys/systm.h>
41 1.1 haya #include <sys/device.h>
42 1.1 haya
43 1.1 haya #include <machine/bus.h>
44 1.1 haya
45 1.1 haya #include <dev/cardbus/cardbusvar.h>
46 1.1 haya
47 1.1 haya #include <dev/pci/pcireg.h> /* XXX */
48 1.1 haya
49 1.1 haya #if defined DEBUG && !defined CARDBUS_MAP_DEBUG
50 1.1 haya #define CARDBUS_MAP_DEBUG
51 1.1 haya #endif
52 1.1 haya
53 1.1 haya #if defined CARDBUS_MAP_DEBUG
54 1.1 haya #define STATIC
55 1.1 haya #define DPRINTF(a) printf a
56 1.1 haya #else
57 1.1 haya #define STATIC static
58 1.1 haya #define DPRINTF(a)
59 1.1 haya #endif
60 1.1 haya
61 1.1 haya
62 1.1 haya static int cardbus_io_find __P((cardbus_chipset_tag_t, cardbus_function_tag_t,
63 1.1 haya cardbustag_t, int, cardbusreg_t,
64 1.1 haya bus_addr_t *, bus_size_t *, int *));
65 1.1 haya static int cardbus_mem_find __P((cardbus_chipset_tag_t, cardbus_function_tag_t,
66 1.1 haya cardbustag_t, int, cardbusreg_t,
67 1.1 haya bus_addr_t *, bus_size_t *, int *));
68 1.1 haya
69 1.1 haya /*
70 1.1 haya * static int cardbus_io_find(cardbus_chipset_tag_t cc,
71 1.1 haya * cardbus_function_tag_t cf, cardbustag_t tag,
72 1.1 haya * int reg, cardbusreg_t type, bus_addr_t *basep,
73 1.1 haya * bus_size_t *sizep, int *flagsp)
74 1.1 haya * This code is stallen from sys/dev/pci_map.c.
75 1.1 haya */
76 1.1 haya static int
77 1.1 haya cardbus_io_find(cc, cf, tag, reg, type, basep, sizep, flagsp)
78 1.1 haya cardbus_chipset_tag_t cc;
79 1.1 haya cardbus_function_tag_t cf;
80 1.1 haya cardbustag_t tag;
81 1.1 haya int reg;
82 1.1 haya cardbusreg_t type;
83 1.1 haya bus_addr_t *basep;
84 1.1 haya bus_size_t *sizep;
85 1.1 haya int *flagsp;
86 1.1 haya {
87 1.1 haya cardbusreg_t address, mask;
88 1.1 haya int s;
89 1.1 haya
90 1.5 joda if (reg != CARDBUS_ROM_REG
91 1.5 joda && (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))) {
92 1.1 haya panic("cardbus_io_find: bad request");
93 1.1 haya }
94 1.1 haya
95 1.1 haya /*
96 1.1 haya * Section 6.2.5.1, `Address Maps', tells us that:
97 1.1 haya *
98 1.1 haya * 1) The builtin software should have already mapped the device in a
99 1.1 haya * reasonable way.
100 1.1 haya *
101 1.1 haya * 2) A device which wants 2^n bytes of memory will hardwire the bottom
102 1.1 haya * n bits of the address to 0. As recommended, we write all 1s and see
103 1.1 haya * what we get back.
104 1.1 haya */
105 1.1 haya s = splhigh();
106 1.1 haya address = cardbus_conf_read(cc, cf, tag, reg);
107 1.1 haya cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
108 1.1 haya mask = cardbus_conf_read(cc, cf, tag, reg);
109 1.1 haya cardbus_conf_write(cc, cf, tag, reg, address);
110 1.1 haya splx(s);
111 1.1 haya
112 1.1 haya if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
113 1.1 haya printf("pci_io_find: expected type i/o, found mem\n");
114 1.1 haya return 1;
115 1.1 haya }
116 1.1 haya
117 1.1 haya if (PCI_MAPREG_IO_SIZE(mask) == 0) {
118 1.1 haya printf("pci_io_find: void region\n");
119 1.1 haya return (1);
120 1.1 haya }
121 1.1 haya
122 1.1 haya if (basep != 0) {
123 1.1 haya *basep = PCI_MAPREG_IO_ADDR(address);
124 1.1 haya }
125 1.1 haya if (sizep != 0) {
126 1.1 haya *sizep = PCI_MAPREG_IO_SIZE(mask);
127 1.1 haya }
128 1.1 haya if (flagsp != 0) {
129 1.1 haya *flagsp = 0;
130 1.1 haya }
131 1.1 haya
132 1.1 haya return 0;
133 1.1 haya }
134 1.1 haya
135 1.1 haya
136 1.1 haya
137 1.1 haya /*
138 1.1 haya * static int cardbus_mem_find(cardbus_chipset_tag_t cc,
139 1.1 haya * cardbus_function_tag_t cf, cardbustag_t tag,
140 1.1 haya * int reg, cardbusreg_t type, bus_addr_t *basep,
141 1.1 haya * bus_size_t *sizep, int *flagsp)
142 1.1 haya * This code is stallen from sys/dev/pci_map.c.
143 1.1 haya */
144 1.1 haya static int
145 1.1 haya cardbus_mem_find(cc, cf, tag, reg, type, basep, sizep, flagsp)
146 1.1 haya cardbus_chipset_tag_t cc;
147 1.1 haya cardbus_function_tag_t cf;
148 1.1 haya cardbustag_t tag;
149 1.1 haya int reg;
150 1.1 haya cardbusreg_t type;
151 1.1 haya bus_addr_t *basep;
152 1.1 haya bus_size_t *sizep;
153 1.1 haya int *flagsp;
154 1.1 haya {
155 1.1 haya cardbusreg_t address, mask;
156 1.1 haya int s;
157 1.1 haya
158 1.5 joda if (reg != CARDBUS_ROM_REG &&
159 1.5 joda (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))) {
160 1.1 haya panic("cardbus_find_mem: bad request");
161 1.1 haya }
162 1.1 haya
163 1.1 haya /*
164 1.1 haya * Section 6.2.5.1, `Address Maps', tells us that:
165 1.1 haya *
166 1.1 haya * 1) The builtin software should have already mapped the device in a
167 1.1 haya * reasonable way.
168 1.1 haya *
169 1.1 haya * 2) A device which wants 2^n bytes of memory will hardwire the bottom
170 1.1 haya * n bits of the address to 0. As recommended, we write all 1s and see
171 1.1 haya * what we get back.
172 1.1 haya */
173 1.1 haya s = splhigh();
174 1.1 haya address = cardbus_conf_read(cc, cf, tag, reg);
175 1.1 haya cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
176 1.1 haya mask = cardbus_conf_read(cc, cf, tag, reg);
177 1.1 haya cardbus_conf_write(cc, cf, tag, reg, address);
178 1.1 haya splx(s);
179 1.1 haya
180 1.1 haya if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
181 1.1 haya printf("cardbus_mem_find: expected type mem, found i/o\n");
182 1.1 haya return 1;
183 1.1 haya }
184 1.1 haya if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
185 1.1 haya printf("cardbus_mem_find: expected mem type %08x, found %08x\n",
186 1.1 haya PCI_MAPREG_MEM_TYPE(type),
187 1.1 haya PCI_MAPREG_MEM_TYPE(address));
188 1.1 haya return 1;
189 1.1 haya }
190 1.1 haya
191 1.1 haya if (PCI_MAPREG_MEM_SIZE(mask) == 0) {
192 1.1 haya printf("cardbus_mem_find: void region\n");
193 1.1 haya return 1;
194 1.1 haya }
195 1.1 haya
196 1.1 haya switch (PCI_MAPREG_MEM_TYPE(address)) {
197 1.1 haya case PCI_MAPREG_MEM_TYPE_32BIT:
198 1.1 haya case PCI_MAPREG_MEM_TYPE_32BIT_1M:
199 1.1 haya break;
200 1.1 haya case PCI_MAPREG_MEM_TYPE_64BIT:
201 1.1 haya printf("pci_mem_find: 64-bit memory mapping register\n");
202 1.1 haya return 1;
203 1.1 haya default:
204 1.1 haya printf("cardbus_mem_find: reserved mapping register type\n");
205 1.1 haya return 1;
206 1.1 haya }
207 1.1 haya
208 1.1 haya if (basep != 0) {
209 1.1 haya *basep = PCI_MAPREG_MEM_ADDR(address);
210 1.1 haya }
211 1.1 haya if (sizep != 0) {
212 1.1 haya *sizep = PCI_MAPREG_MEM_SIZE(mask);
213 1.1 haya }
214 1.1 haya if (flagsp != 0) {
215 1.1 haya *flagsp = PCI_MAPREG_MEM_CACHEABLE(address) ? BUS_SPACE_MAP_CACHEABLE : 0;
216 1.1 haya }
217 1.1 haya
218 1.1 haya return 0;
219 1.1 haya }
220 1.1 haya
221 1.1 haya
222 1.1 haya
223 1.1 haya
224 1.1 haya /*
225 1.4 haya * int cardbus_mapreg_map(struct cardbus_softc *, int, int, cardbusreg_t,
226 1.1 haya * int bus_space_tag_t *, bus_space_handle_t *,
227 1.1 haya * bus_addr_t *, bus_size_t *)
228 1.1 haya * This function maps bus-space on the value of Base Address
229 1.1 haya * Register (BAR) indexed by the argument `reg' (the second argument).
230 1.1 haya * When the value of the BAR is not valid, such as 0x00000000, a new
231 1.1 haya * address should be allocated for the BAR and new address values is
232 1.1 haya * written on the BAR.
233 1.1 haya */
234 1.1 haya int
235 1.4 haya cardbus_mapreg_map(sc, func, reg, type, busflags, tagp, handlep, basep, sizep)
236 1.4 haya struct cardbus_softc *sc;
237 1.4 haya int func, reg, busflags;
238 1.1 haya cardbusreg_t type;
239 1.1 haya bus_space_tag_t *tagp;
240 1.1 haya bus_space_handle_t *handlep;
241 1.1 haya bus_addr_t *basep;
242 1.1 haya bus_size_t *sizep;
243 1.1 haya {
244 1.4 haya cardbus_chipset_tag_t cc = sc->sc_cc;
245 1.4 haya cardbus_function_tag_t cf = sc->sc_cf;
246 1.1 haya bus_space_tag_t bustag;
247 1.1 haya #if rbus
248 1.1 haya rbus_tag_t rbustag;
249 1.1 haya #endif
250 1.1 haya bus_space_handle_t handle;
251 1.1 haya bus_addr_t base;
252 1.1 haya bus_size_t size;
253 1.1 haya int flags;
254 1.1 haya int status = 0;
255 1.1 haya
256 1.4 haya cardbustag_t tag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
257 1.1 haya
258 1.4 haya DPRINTF(("cardbus_mapreg_map called: %s %x\n", sc->sc_dev.dv_xname,
259 1.1 haya type));
260 1.1 haya
261 1.1 haya if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
262 1.1 haya if (cardbus_io_find(cc, cf, tag, reg, type, &base, &size, &flags)) {
263 1.1 haya status = 1;
264 1.1 haya }
265 1.4 haya bustag = sc->sc_iot;
266 1.1 haya #if rbus
267 1.4 haya rbustag = sc->sc_rbus_iot;
268 1.1 haya #endif
269 1.1 haya } else {
270 1.1 haya if (cardbus_mem_find(cc, cf, tag, reg, type, &base, &size, &flags)){
271 1.1 haya status = 1;
272 1.1 haya }
273 1.4 haya bustag = sc->sc_memt;
274 1.1 haya #if rbus
275 1.4 haya rbustag = sc->sc_rbus_memt;
276 1.1 haya #endif
277 1.1 haya }
278 1.1 haya if (status == 0) {
279 1.1 haya #if rbus
280 1.1 haya bus_addr_t mask = size - 1;
281 1.1 haya if (base != 0) {
282 1.1 haya mask = 0xffffffff;
283 1.1 haya }
284 1.1 haya if ((*cf->cardbus_space_alloc)(cc, rbustag, base, size, mask, size,
285 1.1 haya busflags | flags, &base, &handle)) {
286 1.1 haya panic("io alloc");
287 1.1 haya }
288 1.1 haya #else
289 1.1 haya bus_addr_t start = 0x8300;
290 1.1 haya bus_addr_t end = 0x8400;
291 1.1 haya if (base != 0) {
292 1.1 haya bus_addr_t start = base;
293 1.1 haya bus_addr_t end = base + size;
294 1.1 haya }
295 1.1 haya if (bus_space_alloc(bustag, start, end, size, size, 0, 0, &base, &handle)) {
296 1.1 haya panic("io alloc");
297 1.1 haya }
298 1.1 haya #endif
299 1.1 haya }
300 1.1 haya cardbus_conf_write(cc, cf, tag, reg, base);
301 1.1 haya
302 1.1 haya DPRINTF(("cardbus_mapreg_map: physaddr %lx\n", base));
303 1.1 haya
304 1.1 haya if (tagp != 0) {
305 1.1 haya *tagp = bustag;
306 1.1 haya }
307 1.1 haya if (handlep != 0) {
308 1.1 haya *handlep = handle;
309 1.1 haya }
310 1.1 haya if (basep != 0) {
311 1.1 haya *basep = base;
312 1.1 haya }
313 1.1 haya if (sizep != 0) {
314 1.1 haya *sizep = size;
315 1.1 haya }
316 1.4 haya cardbus_free_tag(cc, cf, tag);
317 1.4 haya
318 1.1 haya return 0;
319 1.1 haya }
320 1.1 haya
321 1.1 haya
322 1.1 haya
323 1.1 haya
324 1.1 haya
325 1.1 haya
326 1.1 haya /*
327 1.1 haya * int cardbus_save_bar(cardbus_devfunc_t);
328 1.1 haya *
329 1.1 haya * This function saves the Base Address Registers at the CardBus
330 1.1 haya * function denoted by the argument.
331 1.1 haya */
332 1.1 haya int cardbus_save_bar(ct)
333 1.1 haya cardbus_devfunc_t ct;
334 1.1 haya {
335 1.1 haya cardbustag_t tag = Cardbus_make_tag(ct);
336 1.1 haya cardbus_chipset_tag_t cc = ct->ct_cc;
337 1.1 haya cardbus_function_tag_t cf = ct->ct_cf;
338 1.1 haya
339 1.1 haya ct->ct_bar[0] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE0_REG);
340 1.1 haya ct->ct_bar[1] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE1_REG);
341 1.1 haya ct->ct_bar[2] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE2_REG);
342 1.1 haya ct->ct_bar[3] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE3_REG);
343 1.1 haya ct->ct_bar[4] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE4_REG);
344 1.1 haya ct->ct_bar[5] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE5_REG);
345 1.1 haya
346 1.1 haya DPRINTF(("cardbus_save_bar: %x %x\n", ct->ct_bar[0], ct->ct_bar[1]));
347 1.1 haya
348 1.1 haya Cardbus_free_tag(ct, tag);
349 1.1 haya
350 1.1 haya return 0;
351 1.1 haya }
352 1.1 haya
353 1.1 haya
354 1.1 haya
355 1.1 haya /*
356 1.1 haya * int cardbus_restore_bar(cardbus_devfunc_t);
357 1.1 haya *
358 1.1 haya * This function saves the Base Address Registers at the CardBus
359 1.1 haya * function denoted by the argument.
360 1.1 haya */
361 1.1 haya int cardbus_restore_bar(ct)
362 1.1 haya cardbus_devfunc_t ct;
363 1.1 haya {
364 1.1 haya cardbustag_t tag = Cardbus_make_tag(ct);
365 1.1 haya cardbus_chipset_tag_t cc = ct->ct_cc;
366 1.1 haya cardbus_function_tag_t cf = ct->ct_cf;
367 1.1 haya
368 1.1 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, ct->ct_bar[0]);
369 1.1 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, ct->ct_bar[1]);
370 1.1 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, ct->ct_bar[2]);
371 1.1 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, ct->ct_bar[3]);
372 1.1 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, ct->ct_bar[4]);
373 1.1 haya cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, ct->ct_bar[5]);
374 1.1 haya
375 1.1 haya Cardbus_free_tag(ct, tag);
376 1.1 haya return 0;
377 1.1 haya }
378