Home | History | Annotate | Line # | Download | only in cardbus
cardbus_exrom.c revision 1.7
      1 /* $NetBSD: cardbus_exrom.c,v 1.7 2004/10/10 21:58:46 enami Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to
      8  * The NetBSD Foundation by Johan Danielsson.
      9  *
     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  *
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  *
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * 3. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: cardbus_exrom.c,v 1.7 2004/10/10 21:58:46 enami Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/queue.h>
     44 #include <sys/malloc.h>
     45 
     46 #include <machine/bus.h>
     47 
     48 #include <dev/cardbus/cardbus_exrom.h>
     49 
     50 #define READ_INT16(T, H, O)			\
     51     (bus_space_read_1((T), (H), (O)) |		\
     52     (bus_space_read_1((T), (H), (O) + 1) << 8))
     53 
     54 /*  A PCI ROM is divided into a number of images. Each image has two
     55  *  data structures, a header located at the start of the image, and a
     56  *  `data structure' at some offset into it.
     57  *
     58  *  The header is a 26 byte structure:
     59  *
     60  *  Offset	Length	Description
     61  *  0x00	   1	signature byte 1 (0x55)
     62  *  0x01	   1	signature byte 2 (0xAA)
     63  *  0x02	  22	processor architecture data
     64  *  0x18	   2	pointer to the data structure
     65  *
     66  *  The data structure is a 24 byte structure:
     67  *
     68  *  Offset	Length	Description
     69  *  0x00	   4	signature (PCIR)
     70  *  0x04	   2	vendor id
     71  *  0x06	   2	device id
     72  *  0x08	   2	reserved
     73  *  0x0A	   2	data structure length
     74  *  0x0C	   1	data structure revision (0)
     75  *  0x0D	   3	class code
     76  *  0x10	   2	image length (in 512 byte blocks)
     77  *  0x12	   2	code revision level
     78  *  0x14	   1	code type
     79  *  0x15	   1	indicator (bit 7 indicates final image)
     80  *  0x16	   2	reserved
     81  *
     82  */
     83 
     84 /*
     85  *  Scan through a PCI expansion ROM, and create subregions for each
     86  *  ROM image. This function assumes that the ROM is mapped at
     87  *  (tag,handle), and that the expansion ROM address decoder is
     88  *  enabled. The PCI specification requires that no other BAR should
     89  *  be accessed while the ROM is enabled, so interrupts should be
     90  *  disabled.
     91  */
     92 
     93 int
     94 cardbus_read_exrom(bus_space_tag_t romt, bus_space_handle_t romh,
     95      struct cardbus_rom_image_head *head)
     96 {
     97 	static const char thisfunc[] = "cardbus_read_exrom";
     98 	size_t addr = 0; /* offset of current rom image */
     99 	size_t dataptr;
    100 	unsigned int rom_image = 0;
    101 
    102 	SIMPLEQ_INIT(head);
    103 	do {
    104 		size_t image_size;
    105 		struct cardbus_rom_image *image;
    106 		u_int16_t val;
    107 
    108 		val = READ_INT16(romt, romh, addr + CARDBUS_EXROM_SIGNATURE);
    109 		if (val != 0xaa55) {
    110 			printf("%s: "
    111 			    "bad header signature in ROM image %u: 0x%04x\n",
    112 			    thisfunc, rom_image, val);
    113 			return 1;
    114 		}
    115 		dataptr = addr +
    116 		    READ_INT16(romt, romh, addr + CARDBUS_EXROM_DATA_PTR);
    117 		/* get the ROM image size, in blocks */
    118 		image_size = READ_INT16(romt, romh,
    119 		    dataptr + CARDBUS_EXROM_DATA_IMAGE_LENGTH);
    120 		if (image_size == 0)
    121 			/*
    122 			 * XXX some ROMs seem to have this as zero,
    123 			 * can we assume this means 1 block?
    124 			 */
    125 			image_size = 1;
    126 		image_size <<= 9;
    127 		image = malloc(sizeof(*image), M_DEVBUF, M_NOWAIT);
    128 		if (image == NULL) {
    129 			printf("%s: out of memory\n", thisfunc);
    130 			return 1;
    131 		}
    132 		image->rom_image = rom_image;
    133 		image->image_size = image_size;
    134 		image->romt = romt;
    135 		if (bus_space_subregion(romt, romh, addr,
    136 		    image_size, &image->romh)) {
    137 			printf("%s: bus_space_subregion failed", thisfunc);
    138 			free(image, M_DEVBUF);
    139 			return 1;
    140 		}
    141 		SIMPLEQ_INSERT_TAIL(head, image, next);
    142 		addr += image_size;
    143 		rom_image++;
    144 	} while ((bus_space_read_1(romt, romh,
    145 	    dataptr + CARDBUS_EXROM_DATA_INDICATOR) & 0x80) == 0);
    146 	return 0;
    147 }
    148 
    149 
    150 #if 0
    151 struct cardbus_exrom_data_structure {
    152 	char		signature[4];
    153 	cardbusreg_t	id; /* vendor & device id */
    154 	u_int16_t	structure_length;
    155 	u_int8_t	structure_revision;
    156 	cardbusreg_t	class; /* class code in upper 24 bits */
    157 	u_int16_t	image_length;
    158 	u_int16_t	data_revision;
    159 	u_int8_t	code_type;
    160 	u_int8_t	indicator;
    161 };
    162 
    163 void
    164 pci_exrom_parse_data_structure(bus_space_tag_t tag,
    165     bus_space_handle_t handle,
    166     struct pci_exrom_data_structure *ds)
    167 {
    168 	unsigned char hdr[16];
    169 
    170 	bus_space_read_region_1(tag, handle, dataptr, hdr, sizeof(hdr));
    171 	memcpy(header->signature, hdr + PCI_EXROM_DATA_SIGNATURE, 4);
    172 #define LEINT16(B, O) ((B)[(O)] | ((B)[(O) + 1] << 8))
    173 	header->id = LEINT16(hdr, PCI_EXROM_DATA_VENDOR_ID) |
    174 	    (LEINT16(hdr, PCI_EXROM_DATA_DEVICE_ID) << 16);
    175 	header->structure_length = LEINT16(hdr, PCI_EXROM_DATA_LENGTH);
    176 	header->structure_rev = hdr[PCI_EXROM_DATA_REV];
    177 	header->class = (hdr[PCI_EXROM_DATA_CLASS_CODE] << 8) |
    178 	    (hdr[PCI_EXROM_DATA_CLASS_CODE + 1] << 16) |
    179 	    (hdr[PCI_EXROM_DATA_CLASS_CODE + 2] << 24);
    180 	header->image_length = LEINT16(hdr, PCI_EXROM_DATA_IMAGE_LENGTH) << 16;
    181 	header->data_revision = LEINT16(hdr, PCI_EXROM_DATA_DATA_REV);
    182 	header->code_type = hdr[PCI_EXROM_DATA_CODE_TYPE];
    183 	header->indicator = hdr[PCI_EXROM_DATA_INDICATOR];
    184 	length = min(length, header->image_length - 0x18 - offset);
    185 	bus_space_read_region_1(tag, handle, dataptr + 0x18 + offset,
    186 	    buf, length);
    187 	ret = length;
    188 }
    189 #endif
    190