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