Home | History | Annotate | Line # | Download | only in pci
pci_map.c revision 1.1
      1 /*	$NetBSD: pci_map.c,v 1.1 1997/08/30 06:46:58 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1997 Charles M. Hannum.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Charles M. Hannum.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * PCI device mapping.
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 
     40 #include <dev/pci/pcireg.h>
     41 #include <dev/pci/pcivar.h>
     42 
     43 static int pci_io_find __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t,
     44     bus_addr_t *, bus_size_t *, int *));
     45 static int pci_mem_find __P((pci_chipset_tag_t, pcitag_t, int, pcireg_t,
     46     bus_addr_t *, bus_size_t *, int *));
     47 
     48 static int
     49 pci_io_find(pc, tag, reg, type, basep, sizep, flagsp)
     50 	pci_chipset_tag_t pc;
     51 	pcitag_t tag;
     52 	int reg;
     53 	pcireg_t type;
     54 	bus_addr_t *basep;
     55 	bus_size_t *sizep;
     56 	int *flagsp;
     57 {
     58 	pcireg_t address, mask;
     59 	int s;
     60 
     61 	if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
     62 		panic("pci_io_find: bad request");
     63 
     64 	/*
     65 	 * Section 6.2.5.1, `Address Maps', tells us that:
     66 	 *
     67 	 * 1) The builtin software should have already mapped the device in a
     68 	 * reasonable way.
     69 	 *
     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.
     73 	 */
     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);
     80 
     81 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
     82 		printf("pci_io_find: expected type i/o, found mem\n");
     83 		return (1);
     84 	}
     85 
     86 	if (basep != 0)
     87 		*basep = PCI_MAPREG_IO_ADDR(address);
     88 	if (sizep != 0)
     89 		*sizep = PCI_MAPREG_IO_SIZE(mask);
     90 	if (flagsp != 0)
     91 		*flagsp = 0;
     92 
     93 	return (0);
     94 }
     95 
     96 static int
     97 pci_mem_find(pc, tag, reg, type, basep, sizep, flagsp)
     98 	pci_chipset_tag_t pc;
     99 	pcitag_t tag;
    100 	int reg;
    101 	pcireg_t type;
    102 	bus_addr_t *basep;
    103 	bus_size_t *sizep;
    104 	int *flagsp;
    105 {
    106 	pcireg_t address, mask;
    107 	int s;
    108 
    109 	if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3))
    110 		panic("pci_find_mem: bad request");
    111 
    112 	/*
    113 	 * Section 6.2.5.1, `Address Maps', tells us that:
    114 	 *
    115 	 * 1) The builtin software should have already mapped the device in a
    116 	 * reasonable way.
    117 	 *
    118 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
    119 	 * n bits of the address to 0.  As recommended, we write all 1s and see
    120 	 * what we get back.
    121 	 */
    122 	s = splhigh();
    123 	address = pci_conf_read(pc, tag, reg);
    124 	pci_conf_write(pc, tag, reg, 0xffffffff);
    125 	mask = pci_conf_read(pc, tag, reg);
    126 	pci_conf_write(pc, tag, reg, address);
    127 	splx(s);
    128 
    129 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
    130 		printf("pci_mem_find: expected type mem, found i/o\n");
    131 		return (1);
    132 	}
    133 	if (PCI_MAPREG_MEM_TYPE(address) != PCI_MAPREG_MEM_TYPE(type)) {
    134 		printf("pci_mem_find: expected mem type %08x, found %08x\n",
    135 		    PCI_MAPREG_MEM_TYPE(type),
    136 		    PCI_MAPREG_MEM_TYPE(address));
    137 		return (1);
    138 	}
    139 
    140 	switch (PCI_MAPREG_MEM_TYPE(address)) {
    141 	case PCI_MAPREG_MEM_TYPE_32BIT:
    142 	case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    143 		break;
    144 	case PCI_MAPREG_MEM_TYPE_64BIT:
    145 		printf("pci_mem_find: 64-bit memory mapping register\n");
    146 		return (1);
    147 	default:
    148 		printf("pci_mem_find: reserved mapping register type\n");
    149 		return (1);
    150 	}
    151 
    152 	if (basep != 0)
    153 		*basep = PCI_MAPREG_MEM_ADDR(address);
    154 	if (sizep != 0)
    155 		*sizep = PCI_MAPREG_MEM_SIZE(mask);
    156 	if (flagsp != 0)
    157 		*flagsp = PCI_MAPREG_MEM_CACHEABLE(address) ?
    158 		    BUS_SPACE_MAP_CACHEABLE : 0;
    159 
    160 	return (0);
    161 }
    162 
    163 int
    164 pci_mapreg_info(pc, tag, reg, type, basep, sizep, flagsp)
    165 	pci_chipset_tag_t pc;
    166 	pcitag_t tag;
    167 	int reg;
    168 	pcireg_t type;
    169 	bus_addr_t *basep;
    170 	bus_size_t *sizep;
    171 	int *flagsp;
    172 {
    173 
    174 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
    175 		return (pci_io_find(pc, tag, reg, type, basep, sizep,
    176 		    flagsp));
    177 	else
    178 		return (pci_mem_find(pc, tag, reg, type, basep, sizep,
    179 		    flagsp));
    180 }
    181 
    182 int
    183 pci_mapreg_map(pa, reg, type, busflags, tagp, handlep, basep, sizep)
    184 	struct pci_attach_args *pa;
    185 	int reg, busflags;
    186 	pcireg_t type;
    187 	bus_space_tag_t *tagp;
    188 	bus_space_handle_t *handlep;
    189 	bus_addr_t *basep;
    190 	bus_size_t *sizep;
    191 {
    192 	bus_space_tag_t tag;
    193 	bus_space_handle_t handle;
    194 	bus_addr_t base;
    195 	bus_size_t size;
    196 	int flags;
    197 
    198 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
    199 		if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0)
    200 			return (1);
    201 		if (pci_io_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    202 		    &size, &flags))
    203 			return (1);
    204 		tag = pa->pa_iot;
    205 	} else {
    206 		if ((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) == 0)
    207 			return (1);
    208 		if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    209 		    &size, &flags))
    210 			return (1);
    211 		tag = pa->pa_memt;
    212 	}
    213 
    214 	if (bus_space_map(tag, base, size, busflags | flags, &handle))
    215 		return (1);
    216 
    217 	if (tagp != 0)
    218 		*tagp = tag;
    219 	if (handlep != 0)
    220 		*handlep = handle;
    221 	if (basep != 0)
    222 		*basep = base;
    223 	if (sizep != 0)
    224 		*sizep = size;
    225 
    226 	return (0);
    227 }
    228