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