This source file includes following definitions.
- cardbus_mapreg_probe
- cardbus_io_find
- cardbus_mem_find
- cardbus_mapreg_map
- cardbus_mapreg_unmap
- cardbus_save_bar
- cardbus_restore_bar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40
41 #include <machine/bus.h>
42
43 #include <dev/cardbus/cardbusvar.h>
44
45 #include <dev/pci/pcireg.h>
46
47 #if defined DEBUG && !defined CARDBUS_MAP_DEBUG
48 #define CARDBUS_MAP_DEBUG
49 #endif
50
51 #if defined CARDBUS_MAP_DEBUG
52 #define STATIC
53 #define DPRINTF(a) printf a
54 #else
55 #define STATIC static
56 #define DPRINTF(a)
57 #endif
58
59
60 static int cardbus_io_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
61 cardbustag_t, int, cardbusreg_t, bus_addr_t *, bus_size_t *,
62 int *);
63 static int cardbus_mem_find(cardbus_chipset_tag_t, cardbus_function_tag_t,
64 cardbustag_t, int, cardbusreg_t, bus_addr_t *, bus_size_t *,
65 int *);
66
67 int
68 cardbus_mapreg_probe(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
69 cardbustag_t tag, int reg, pcireg_t *typep)
70 {
71 pcireg_t address, mask;
72 int s;
73
74 s = splhigh();
75 address = cardbus_conf_read(cc, cf, tag, reg);
76 cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
77 mask = cardbus_conf_read(cc, cf, tag, reg);
78 cardbus_conf_write(cc, cf, tag, reg, address);
79 splx(s);
80
81 if (mask == 0)
82 return (0);
83
84 if (typep)
85 *typep = _PCI_MAPREG_TYPEBITS(address);
86 return (1);
87 }
88
89
90
91
92
93
94
95
96 static int
97 cardbus_io_find(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
98 cardbustag_t tag, int reg, cardbusreg_t type, bus_addr_t *basep,
99 bus_size_t *sizep, int *flagsp)
100 {
101 cardbusreg_t address, mask;
102 int s;
103
104
105 if (reg == CARDBUS_ROM_REG)
106 return (1);
107
108 if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3)) {
109 panic("cardbus_io_find: bad request");
110 }
111
112
113
114
115
116
117
118
119
120
121
122 s = splhigh();
123 address = cardbus_conf_read(cc, cf, tag, reg);
124 cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
125 mask = cardbus_conf_read(cc, cf, tag, reg);
126 cardbus_conf_write(cc, cf, tag, reg, address);
127 splx(s);
128
129 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
130 printf("cardbus_io_find: expected type i/o, found mem\n");
131 return (1);
132 }
133
134 if (PCI_MAPREG_IO_SIZE(mask) == 0) {
135 printf("cardbus_io_find: void region\n");
136 return (1);
137 }
138
139 if (basep != 0)
140 *basep = PCI_MAPREG_IO_ADDR(address);
141 if (sizep != 0)
142 *sizep = PCI_MAPREG_IO_SIZE(mask);
143 if (flagsp != 0)
144 *flagsp = 0;
145
146 return (0);
147 }
148
149
150
151
152
153
154
155
156 static int
157 cardbus_mem_find(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
158 cardbustag_t tag, int reg, cardbusreg_t type, bus_addr_t *basep,
159 bus_size_t *sizep, int *flagsp)
160 {
161 cardbusreg_t address, mask;
162 int s;
163
164 if (reg != CARDBUS_ROM_REG &&
165 (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))) {
166 panic("cardbus_mem_find: bad request");
167 }
168
169
170
171
172
173
174
175
176
177
178
179 s = splhigh();
180 address = cardbus_conf_read(cc, cf, tag, reg);
181 cardbus_conf_write(cc, cf, tag, reg, 0xffffffff);
182 mask = cardbus_conf_read(cc, cf, tag, reg);
183 cardbus_conf_write(cc, cf, tag, reg, address);
184 splx(s);
185
186 if (reg != CARDBUS_ROM_REG) {
187
188
189 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
190 printf("cardbus_mem_find: expected type mem, "
191 "found i/o\n");
192 return (1);
193 }
194 if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
195 printf("cardbus_mem_find: expected mem type %08x, "
196 "found %08x\n", PCI_MAPREG_MEM_TYPE(type),
197 PCI_MAPREG_MEM_TYPE(address));
198 return (1);
199 }
200 }
201
202 if (PCI_MAPREG_MEM_SIZE(mask) == 0) {
203 printf("cardbus_mem_find: void region\n");
204 return (1);
205 }
206
207 switch (PCI_MAPREG_MEM_TYPE(address)) {
208 case PCI_MAPREG_MEM_TYPE_32BIT:
209 case PCI_MAPREG_MEM_TYPE_32BIT_1M:
210 break;
211 case PCI_MAPREG_MEM_TYPE_64BIT:
212 printf("cardbus_mem_find: 64-bit memory mapping register\n");
213 return (1);
214 default:
215 printf("cardbus_mem_find: reserved mapping register type\n");
216 return (1);
217 }
218
219 if (basep != 0)
220 *basep = PCI_MAPREG_MEM_ADDR(address);
221 if (sizep != 0)
222 *sizep = PCI_MAPREG_MEM_SIZE(mask);
223 if (flagsp != 0) {
224 *flagsp =
225 #ifdef BUS_SPACE_MAP_PREFETCHABLE
226 PCI_MAPREG_MEM_PREFETCHABLE(address) ?
227 BUS_SPACE_MAP_PREFETCHABLE :
228 #endif
229 0;
230 }
231
232 return (0);
233 }
234
235
236
237
238
239
240
241
242
243
244
245 int
246 cardbus_mapreg_map(struct cardbus_softc *sc, int func, int reg,
247 cardbusreg_t type, int busflags, bus_space_tag_t *tagp,
248 bus_space_handle_t *handlep, bus_addr_t *basep, bus_size_t *sizep)
249 {
250 cardbus_chipset_tag_t cc = sc->sc_cc;
251 cardbus_function_tag_t cf = sc->sc_cf;
252 bus_space_tag_t bustag;
253 rbus_tag_t rbustag;
254 bus_space_handle_t handle;
255 bus_addr_t base;
256 bus_size_t size;
257 int flags;
258 int status = 0;
259
260 cardbustag_t tag = cardbus_make_tag(cc, cf, sc->sc_bus,
261 sc->sc_device, func);
262
263 DPRINTF(("cardbus_mapreg_map called: %s %x\n", sc->sc_dev.dv_xname,
264 type));
265
266 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
267 if (cardbus_io_find(cc, cf, tag, reg, type, &base, &size,
268 &flags))
269 status = 1;
270 bustag = sc->sc_iot;
271 rbustag = sc->sc_rbus_iot;
272 } else {
273 if (cardbus_mem_find(cc, cf, tag, reg, type, &base, &size,
274 &flags))
275 status = 1;
276 bustag = sc->sc_memt;
277 rbustag = sc->sc_rbus_memt;
278 }
279 if (status == 0) {
280 bus_addr_t mask = size - 1;
281 if (base != 0)
282 mask = 0xffffffff;
283 if ((*cf->cardbus_space_alloc)(cc, rbustag, base, size, mask,
284 size, busflags | flags, &base, &handle)) {
285 panic("io alloc");
286 }
287 }
288 cardbus_conf_write(cc, cf, tag, reg, base);
289
290 DPRINTF(("cardbus_mapreg_map: physaddr %lx\n", (unsigned long)base));
291
292 if (tagp != 0)
293 *tagp = bustag;
294 if (handlep != 0)
295 *handlep = handle;
296 if (basep != 0)
297 *basep = base;
298 if (sizep != 0)
299 *sizep = size;
300 cardbus_free_tag(cc, cf, tag);
301
302 return (0);
303 }
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318 int
319 cardbus_mapreg_unmap(struct cardbus_softc *sc, int func, int reg,
320 bus_space_tag_t tag, bus_space_handle_t handle, bus_size_t size)
321 {
322 cardbus_chipset_tag_t cc = sc->sc_cc;
323 cardbus_function_tag_t cf = sc->sc_cf;
324 int st = 1;
325 cardbustag_t cardbustag;
326 rbus_tag_t rbustag;
327
328 if (sc->sc_iot == tag) {
329
330 DPRINTF(("%s: unmap i/o space\n", sc->sc_dev.dv_xname));
331 rbustag = sc->sc_rbus_iot;
332 } else if (sc->sc_memt == tag) {
333
334 DPRINTF(("%s: unmap mem space\n", sc->sc_dev.dv_xname));
335 rbustag = sc->sc_rbus_memt;
336 } else
337 return (1);
338
339 cardbustag = cardbus_make_tag(cc, cf, sc->sc_bus, sc->sc_device, func);
340
341 cardbus_conf_write(cc, cf, cardbustag, reg, 0);
342
343 (*cf->cardbus_space_free)(cc, rbustag, handle, size);
344
345 cardbus_free_tag(cc, cf, cardbustag);
346
347 return (st);
348 }
349
350
351
352
353
354
355
356 int
357 cardbus_save_bar(cardbus_devfunc_t ct)
358 {
359 cardbustag_t tag = Cardbus_make_tag(ct);
360 cardbus_chipset_tag_t cc = ct->ct_cc;
361 cardbus_function_tag_t cf = ct->ct_cf;
362
363 ct->ct_bar[0] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE0_REG);
364 ct->ct_bar[1] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE1_REG);
365 ct->ct_bar[2] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE2_REG);
366 ct->ct_bar[3] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE3_REG);
367 ct->ct_bar[4] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE4_REG);
368 ct->ct_bar[5] = cardbus_conf_read(cc, cf, tag, CARDBUS_BASE5_REG);
369
370 DPRINTF(("cardbus_save_bar: %x %x\n", ct->ct_bar[0], ct->ct_bar[1]));
371
372 Cardbus_free_tag(ct, tag);
373
374 return (0);
375 }
376
377
378
379
380
381
382
383 int
384 cardbus_restore_bar(cardbus_devfunc_t ct)
385 {
386 cardbustag_t tag = Cardbus_make_tag(ct);
387 cardbus_chipset_tag_t cc = ct->ct_cc;
388 cardbus_function_tag_t cf = ct->ct_cf;
389
390 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, ct->ct_bar[0]);
391 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, ct->ct_bar[1]);
392 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, ct->ct_bar[2]);
393 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, ct->ct_bar[3]);
394 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, ct->ct_bar[4]);
395 cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, ct->ct_bar[5]);
396
397 Cardbus_free_tag(ct, tag);
398
399 return (0);
400 }