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