Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pci / pci_map.c
blob43d0e2438e4642481ade9c54668f5976f4873b60
1 /* $NetBSD: pci_map.c,v 1.23 2008/04/28 20:23:55 martin Exp $ */
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum; by William R. Studenmund; by Jason R. Thorpe.
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.
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.
33 * PCI device mapping.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: pci_map.c,v 1.23 2008/04/28 20:23:55 martin Exp $");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
46 static int
47 pci_io_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
48 bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
50 pcireg_t address, mask;
51 int s;
53 if (reg < PCI_MAPREG_START ||
54 #if 0
56 * Can't do this check; some devices have mapping registers
57 * way out in left field.
59 reg >= PCI_MAPREG_END ||
60 #endif
61 (reg & 3))
62 panic("pci_io_find: bad request");
65 * Section 6.2.5.1, `Address Maps', tells us that:
67 * 1) The builtin software should have already mapped the device in a
68 * reasonable way.
70 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
71 * n bits of the address to 0. As recommended, we write all 1s and see
72 * what we get back.
74 s = splhigh();
75 address = pci_conf_read(pc, tag, reg);
76 pci_conf_write(pc, tag, reg, 0xffffffff);
77 mask = pci_conf_read(pc, tag, reg);
78 pci_conf_write(pc, tag, reg, address);
79 splx(s);
81 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
82 aprint_debug("pci_io_find: expected type i/o, found mem\n");
83 return (1);
86 if (PCI_MAPREG_IO_SIZE(mask) == 0) {
87 aprint_debug("pci_io_find: void region\n");
88 return (1);
91 if (basep != 0)
92 *basep = PCI_MAPREG_IO_ADDR(address);
93 if (sizep != 0)
94 *sizep = PCI_MAPREG_IO_SIZE(mask);
95 if (flagsp != 0)
96 *flagsp = 0;
98 return (0);
101 static int
102 pci_mem_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
103 bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
105 pcireg_t address, mask, address1 = 0, mask1 = 0xffffffff;
106 u_int64_t waddress, wmask;
107 int s, is64bit, isrom;
109 is64bit = (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT);
110 isrom = (reg == PCI_MAPREG_ROM);
112 if ((!isrom) && (reg < PCI_MAPREG_START ||
113 #if 0
115 * Can't do this check; some devices have mapping registers
116 * way out in left field.
118 reg >= PCI_MAPREG_END ||
119 #endif
120 (reg & 3)))
121 panic("pci_mem_find: bad request");
123 if (is64bit && (reg + 4) >= PCI_MAPREG_END)
124 panic("pci_mem_find: bad 64-bit request");
127 * Section 6.2.5.1, `Address Maps', tells us that:
129 * 1) The builtin software should have already mapped the device in a
130 * reasonable way.
132 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
133 * n bits of the address to 0. As recommended, we write all 1s and see
134 * what we get back.
136 s = splhigh();
137 address = pci_conf_read(pc, tag, reg);
138 pci_conf_write(pc, tag, reg, 0xffffffff);
139 mask = pci_conf_read(pc, tag, reg);
140 pci_conf_write(pc, tag, reg, address);
141 if (is64bit) {
142 address1 = pci_conf_read(pc, tag, reg + 4);
143 pci_conf_write(pc, tag, reg + 4, 0xffffffff);
144 mask1 = pci_conf_read(pc, tag, reg + 4);
145 pci_conf_write(pc, tag, reg + 4, address1);
147 splx(s);
149 if (!isrom) {
151 * roms should have an enable bit instead of a memory
152 * type decoder bit. For normal BARs, make sure that
153 * the address decoder type matches what we asked for.
155 if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
156 printf("pci_mem_find: expected type mem, found i/o\n");
157 return (1);
159 /* XXX Allow 64bit bars for 32bit requests.*/
160 if (PCI_MAPREG_MEM_TYPE(address) !=
161 PCI_MAPREG_MEM_TYPE(type) &&
162 PCI_MAPREG_MEM_TYPE(address) !=
163 PCI_MAPREG_MEM_TYPE_64BIT) {
164 printf("pci_mem_find: "
165 "expected mem type %08x, found %08x\n",
166 PCI_MAPREG_MEM_TYPE(type),
167 PCI_MAPREG_MEM_TYPE(address));
168 return (1);
172 waddress = (u_int64_t)address1 << 32UL | address;
173 wmask = (u_int64_t)mask1 << 32UL | mask;
175 if ((is64bit && PCI_MAPREG_MEM64_SIZE(wmask) == 0) ||
176 (!is64bit && PCI_MAPREG_MEM_SIZE(mask) == 0)) {
177 aprint_debug("pci_mem_find: void region\n");
178 return (1);
181 switch (PCI_MAPREG_MEM_TYPE(address)) {
182 case PCI_MAPREG_MEM_TYPE_32BIT:
183 case PCI_MAPREG_MEM_TYPE_32BIT_1M:
184 break;
185 case PCI_MAPREG_MEM_TYPE_64BIT:
187 * Handle the case of a 64-bit memory register on a
188 * platform with 32-bit addressing. Make sure that
189 * the address assigned and the device's memory size
190 * fit in 32 bits. We implicitly assume that if
191 * bus_addr_t is 64-bit, then so is bus_size_t.
193 if (sizeof(u_int64_t) > sizeof(bus_addr_t) &&
194 (address1 != 0 || mask1 != 0xffffffff)) {
195 printf("pci_mem_find: 64-bit memory map which is "
196 "inaccessible on a 32-bit platform\n");
197 return (1);
199 break;
200 default:
201 printf("pci_mem_find: reserved mapping register type\n");
202 return (1);
205 if (sizeof(u_int64_t) > sizeof(bus_addr_t)) {
206 if (basep != 0)
207 *basep = PCI_MAPREG_MEM_ADDR(address);
208 if (sizep != 0)
209 *sizep = PCI_MAPREG_MEM_SIZE(mask);
210 } else {
211 if (basep != 0)
212 *basep = PCI_MAPREG_MEM64_ADDR(waddress);
213 if (sizep != 0)
214 *sizep = PCI_MAPREG_MEM64_SIZE(wmask);
216 if (flagsp != 0)
217 *flagsp = (isrom || PCI_MAPREG_MEM_PREFETCHABLE(address)) ?
218 BUS_SPACE_MAP_PREFETCHABLE : 0;
220 return (0);
223 #define _PCI_MAPREG_TYPEBITS(reg) \
224 (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO ? \
225 reg & PCI_MAPREG_TYPE_MASK : \
226 reg & (PCI_MAPREG_TYPE_MASK|PCI_MAPREG_MEM_TYPE_MASK))
228 pcireg_t
229 pci_mapreg_type(pci_chipset_tag_t pc, pcitag_t tag, int reg)
232 return (_PCI_MAPREG_TYPEBITS(pci_conf_read(pc, tag, reg)));
236 pci_mapreg_probe(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *typep)
238 pcireg_t address, mask;
239 int s;
241 s = splhigh();
242 address = pci_conf_read(pc, tag, reg);
243 pci_conf_write(pc, tag, reg, 0xffffffff);
244 mask = pci_conf_read(pc, tag, reg);
245 pci_conf_write(pc, tag, reg, address);
246 splx(s);
248 if (mask == 0) /* unimplemented mapping register */
249 return (0);
251 if (typep)
252 *typep = _PCI_MAPREG_TYPEBITS(address);
253 return (1);
257 pci_mapreg_info(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
258 bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
261 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
262 return (pci_io_find(pc, tag, reg, type, basep, sizep,
263 flagsp));
264 else
265 return (pci_mem_find(pc, tag, reg, type, basep, sizep,
266 flagsp));
270 pci_mapreg_map(struct pci_attach_args *pa, int reg, pcireg_t type,
271 int busflags, bus_space_tag_t *tagp, bus_space_handle_t *handlep,
272 bus_addr_t *basep, bus_size_t *sizep)
274 return pci_mapreg_submap(pa, reg, type, busflags, 0, 0, tagp,
275 handlep, basep, sizep);
279 pci_mapreg_submap(struct pci_attach_args *pa, int reg, pcireg_t type,
280 int busflags, bus_size_t maxsize, bus_size_t offset, bus_space_tag_t *tagp,
281 bus_space_handle_t *handlep, bus_addr_t *basep, bus_size_t *sizep)
283 bus_space_tag_t tag;
284 bus_space_handle_t handle;
285 bus_addr_t base;
286 bus_size_t size;
287 int flags;
289 if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
290 if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0)
291 return (1);
292 if (pci_io_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
293 &size, &flags))
294 return (1);
295 tag = pa->pa_iot;
296 } else {
297 if ((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) == 0)
298 return (1);
299 if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
300 &size, &flags))
301 return (1);
302 tag = pa->pa_memt;
305 if (reg == PCI_MAPREG_ROM) {
306 pcireg_t mask;
307 int s;
308 /* we have to enable the ROM address decoder... */
309 s = splhigh();
310 mask = pci_conf_read(pa->pa_pc, pa->pa_tag, reg);
311 mask |= PCI_MAPREG_ROM_ENABLE;
312 pci_conf_write(pa->pa_pc, pa->pa_tag, reg, mask);
313 splx(s);
316 /* If we're called with maxsize/offset of 0, behave like
317 * pci_mapreg_map.
320 maxsize = (maxsize && offset) ? maxsize : size;
321 base += offset;
323 if ((maxsize < size && offset + maxsize <= size) || offset != 0)
324 return (1);
326 if (bus_space_map(tag, base, maxsize, busflags | flags, &handle))
327 return (1);
329 if (tagp != 0)
330 *tagp = tag;
331 if (handlep != 0)
332 *handlep = handle;
333 if (basep != 0)
334 *basep = base;
335 if (sizep != 0)
336 *sizep = maxsize;
338 return (0);
342 pci_find_rom(struct pci_attach_args *pa, bus_space_tag_t bst,
343 bus_space_handle_t bsh, int type, bus_space_handle_t *romh, bus_size_t *sz)
345 bus_size_t romsz, offset = 0, imagesz;
346 uint16_t ptr;
347 int done = 0;
349 if (pci_mem_find(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
350 PCI_MAPREG_TYPE_ROM, NULL, &romsz, NULL))
351 return 1;
354 * no upper bound check; i cannot imagine a 4GB ROM, but
355 * it appears the spec would allow it!
357 if (romsz < 1024)
358 return 1;
360 while (offset < romsz && !done){
361 struct pci_rom_header hdr;
362 struct pci_rom rom;
364 hdr.romh_magic = bus_space_read_2(bst, bsh,
365 offset + offsetof (struct pci_rom_header, romh_magic));
366 hdr.romh_data_ptr = bus_space_read_2(bst, bsh,
367 offset + offsetof (struct pci_rom_header, romh_data_ptr));
369 /* no warning: quite possibly ROM is simply not populated */
370 if (hdr.romh_magic != PCI_ROM_HEADER_MAGIC)
371 return 1;
373 ptr = offset + hdr.romh_data_ptr;
375 if (ptr > romsz) {
376 printf("pci_find_rom: rom data ptr out of range\n");
377 return 1;
380 rom.rom_signature = bus_space_read_4(bst, bsh, ptr);
381 rom.rom_vendor = bus_space_read_2(bst, bsh, ptr +
382 offsetof(struct pci_rom, rom_vendor));
383 rom.rom_product = bus_space_read_2(bst, bsh, ptr +
384 offsetof(struct pci_rom, rom_product));
385 rom.rom_class = bus_space_read_1(bst, bsh,
386 ptr + offsetof (struct pci_rom, rom_class));
387 rom.rom_subclass = bus_space_read_1(bst, bsh,
388 ptr + offsetof (struct pci_rom, rom_subclass));
389 rom.rom_interface = bus_space_read_1(bst, bsh,
390 ptr + offsetof (struct pci_rom, rom_interface));
391 rom.rom_len = bus_space_read_2(bst, bsh,
392 ptr + offsetof (struct pci_rom, rom_len));
393 rom.rom_code_type = bus_space_read_1(bst, bsh,
394 ptr + offsetof (struct pci_rom, rom_code_type));
395 rom.rom_indicator = bus_space_read_1(bst, bsh,
396 ptr + offsetof (struct pci_rom, rom_indicator));
398 if (rom.rom_signature != PCI_ROM_SIGNATURE) {
399 printf("pci_find_rom: bad rom data signature\n");
400 return 1;
403 imagesz = rom.rom_len * 512;
405 if ((rom.rom_vendor == PCI_VENDOR(pa->pa_id)) &&
406 (rom.rom_product == PCI_PRODUCT(pa->pa_id)) &&
407 (rom.rom_class == PCI_CLASS(pa->pa_class)) &&
408 (rom.rom_subclass == PCI_SUBCLASS(pa->pa_class)) &&
409 (rom.rom_interface == PCI_INTERFACE(pa->pa_class)) &&
410 (rom.rom_code_type == type)) {
411 *sz = imagesz;
412 bus_space_subregion(bst, bsh, offset, imagesz, romh);
413 return 0;
416 /* last image check */
417 if (rom.rom_indicator & PCI_ROM_INDICATOR_LAST)
418 return 1;
420 /* offset by size */
421 offset += imagesz;
423 return 1;