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