Home | History | Annotate | Line # | Download | only in cardbus
cardbus_exrom.c revision 1.5
      1 /* $NetBSD: cardbus_exrom.c,v 1.5 2000/05/08 18:23:36 thorpej 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/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/queue.h>
     41 #include <sys/malloc.h>
     42 
     43 #include <machine/bus.h>
     44 
     45 #include <dev/cardbus/cardbus_exrom.h>
     46 
     47 #define READ_INT16(T, H, O)  \
     48 (bus_space_read_1((T), (H), (O)) | (bus_space_read_1((T), (H), (O) + 1) << 8))
     49 
     50 /*  A PCI ROM is divided into a number of images. Each image has two
     51  *  data structures, a header located at the start of the image, and a
     52  *  `data structure' at some offset into it.
     53  *
     54  *  The header is a 26 byte structure:
     55  *
     56  *  Offset	Length	Description
     57  *  0x00	   1	signature byte 1 (0x55)
     58  *  0x01	   1	signature byte 2 (0xAA)
     59  *  0x02	  22	processor architecture data
     60  *  0x18	   2	pointer to the data structure
     61  *
     62  *  The data structure is a 24 byte structure:
     63  *
     64  *  Offset	Length	Description
     65  *  0x00	   4	signature (PCIR)
     66  *  0x04	   2	vendor id
     67  *  0x06	   2	device id
     68  *  0x08	   2	reserved
     69  *  0x0A	   2	data structure length
     70  *  0x0C	   1	data structure revision (0)
     71  *  0x0D	   3	class code
     72  *  0x10	   2	image length (in 512 byte blocks)
     73  *  0x12	   2	code revision level
     74  *  0x14	   1	code type
     75  *  0x15	   1	indicator (bit 7 indicates final image)
     76  *  0x16	   2	reserved
     77  *
     78  */
     79 
     80 /*
     81  *  Scan through a PCI expansion ROM, and create subregions for each
     82  *  ROM image. This function assumes that the ROM is mapped at
     83  *  (tag,handle), and that the expansion ROM address decoder is
     84  *  enabled. The PCI specification requires that no other BAR should
     85  *  be accessed while the ROM is enabled, so interrupts should be
     86  *  disabled.
     87  */
     88 
     89 int
     90 cardbus_read_exrom(romt, romh, head)
     91      bus_space_tag_t romt;
     92      bus_space_handle_t romh;
     93      struct cardbus_rom_image_head *head;
     94 {
     95     static const char thisfunc[] = "cardbus_read_exrom";
     96 
     97     size_t addr = 0; /* offset of current rom image */
     98     size_t dataptr;
     99     unsigned int rom_image = 0;
    100 
    101     SIMPLEQ_INIT(head);
    102     do {
    103 	size_t image_size;
    104 	struct cardbus_rom_image *image;
    105 	u_int16_t val;
    106 
    107 	val = READ_INT16(romt, romh, addr + CARDBUS_EXROM_SIGNATURE);
    108 	if(val != 0xaa55) {
    109 	    printf("%s: bad header signature in ROM image %u: 0x%04x\n",
    110 		   thisfunc, rom_image, val);
    111 	    return 1;
    112 	}
    113 	dataptr = addr + READ_INT16(romt, romh, addr + CARDBUS_EXROM_DATA_PTR);
    114 	/* get the ROM image size, in blocks */
    115 	image_size = READ_INT16(romt, romh,
    116 		         dataptr + CARDBUS_EXROM_DATA_IMAGE_LENGTH);
    117 	if(image_size == 0)
    118 	    /* XXX some ROMs seem to have this as zero, can we assume
    119                this means 1 block? */
    120 	    image_size = 1;
    121 	image_size <<= 9;
    122 	image = malloc(sizeof(*image), M_DEVBUF, M_NOWAIT);
    123 	if(image == NULL) {
    124 	    printf("%s: out of memory\n", thisfunc);
    125 	    return 1;
    126 	}
    127 	image->rom_image = rom_image;
    128 	image->image_size = image_size;
    129 	image->romt = romt;
    130 	if(bus_space_subregion(romt, romh, addr,
    131 			       image_size, &image->romh)) {
    132 	    printf("%s: bus_space_subregion failed", thisfunc);
    133 	    free(image, M_DEVBUF);
    134 	    return 1;
    135 	}
    136 	SIMPLEQ_INSERT_TAIL(head, image, next);
    137 	addr += image_size;
    138 	rom_image++;
    139     } while ((bus_space_read_1(romt, romh,
    140                   dataptr + CARDBUS_EXROM_DATA_INDICATOR) & 0x80) == 0);
    141     return 0;
    142 }
    143 
    144 
    145 #if 0
    146 struct cardbus_exrom_data_structure {
    147     char	signature[4];
    148     cardbusreg_t	id; /* vendor & device id */
    149     u_int16_t	structure_length;
    150     u_int8_t	structure_revision;
    151     cardbusreg_t	class; /* class code in upper 24 bits */
    152     u_int16_t	image_length;
    153     u_int16_t	data_revision;
    154     u_int8_t	code_type;
    155     u_int8_t	indicator;
    156 };
    157 
    158 pci_exrom_parse_data_structure(bus_space_tag_t tag,
    159 			       bus_space_handle_t handle,
    160 			       struct pci_exrom_data_structure *ds)
    161 {
    162     unsigned char hdr[16];
    163     bus_space_read_region_1(tag, handle, dataptr, hdr, sizeof(hdr));
    164     memcpy(header->signature, hdr + PCI_EXROM_DATA_SIGNATURE, 4);
    165 #define LEINT16(B, O) ((B)[(O)] | ((B)[(O) + 1] << 8))
    166     header->id = LEINT16(hdr, PCI_EXROM_DATA_VENDOR_ID) |
    167 	(LEINT16(hdr, PCI_EXROM_DATA_DEVICE_ID) << 16);
    168     header->structure_length = LEINT16(hdr, PCI_EXROM_DATA_LENGTH);
    169     header->structure_rev = hdr[PCI_EXROM_DATA_REV];
    170     header->class = (hdr[PCI_EXROM_DATA_CLASS_CODE] << 8) |
    171 	(hdr[PCI_EXROM_DATA_CLASS_CODE + 1] << 16) |
    172 	(hdr[PCI_EXROM_DATA_CLASS_CODE + 2] << 24);
    173     header->image_length = LEINT16(hdr, PCI_EXROM_DATA_IMAGE_LENGTH) << 16;
    174     header->data_revision = LEINT16(hdr, PCI_EXROM_DATA_DATA_REV);
    175     header->code_type = hdr[PCI_EXROM_DATA_CODE_TYPE];
    176     header->indicator = hdr[PCI_EXROM_DATA_INDICATOR];
    177     length = min(length, header->image_length - 0x18 - offset);
    178     bus_space_read_region_1(tag, handle, dataptr + 0x18 + offset,
    179 			    buf, length);
    180     ret = length;
    181 }
    182 #endif
    183