Home | History | Annotate | Line # | Download | only in cardbus
      1  1.14       chs /* $NetBSD: cardbus_exrom.c,v 1.14 2019/11/10 21:16:34 chs 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.14       chs __KERNEL_RCSID(0, "$NetBSD: cardbus_exrom.c,v 1.14 2019/11/10 21:16:34 chs 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.14       chs 		image = malloc(sizeof(*image), M_DEVBUF, M_WAITOK);
    125   1.7     enami 		image->rom_image = rom_image;
    126   1.7     enami 		image->image_size = image_size;
    127   1.7     enami 		image->romt = romt;
    128   1.7     enami 		if (bus_space_subregion(romt, romh, addr,
    129   1.7     enami 		    image_size, &image->romh)) {
    130   1.7     enami 			printf("%s: bus_space_subregion failed", thisfunc);
    131   1.7     enami 			free(image, M_DEVBUF);
    132   1.7     enami 			return 1;
    133   1.7     enami 		}
    134   1.7     enami 		SIMPLEQ_INSERT_TAIL(head, image, next);
    135   1.7     enami 		addr += image_size;
    136   1.7     enami 		rom_image++;
    137   1.7     enami 	} while ((bus_space_read_1(romt, romh,
    138   1.7     enami 	    dataptr + CARDBUS_EXROM_DATA_INDICATOR) & 0x80) == 0);
    139   1.7     enami 	return 0;
    140   1.1      joda }
    141   1.1      joda 
    142   1.1      joda 
    143   1.1      joda #if 0
    144   1.1      joda struct cardbus_exrom_data_structure {
    145   1.7     enami 	char		signature[4];
    146  1.12    dyoung 	pcireg_t	id; /* vendor & device id */
    147   1.7     enami 	u_int16_t	structure_length;
    148   1.7     enami 	u_int8_t	structure_revision;
    149  1.12    dyoung 	pcireg_t	class; /* class code in upper 24 bits */
    150   1.7     enami 	u_int16_t	image_length;
    151   1.7     enami 	u_int16_t	data_revision;
    152   1.7     enami 	u_int8_t	code_type;
    153   1.7     enami 	u_int8_t	indicator;
    154   1.1      joda };
    155   1.1      joda 
    156   1.7     enami void
    157   1.1      joda pci_exrom_parse_data_structure(bus_space_tag_t tag,
    158   1.7     enami     bus_space_handle_t handle,
    159   1.7     enami     struct pci_exrom_data_structure *ds)
    160   1.1      joda {
    161   1.7     enami 	unsigned char hdr[16];
    162   1.7     enami 
    163   1.7     enami 	bus_space_read_region_1(tag, handle, dataptr, hdr, sizeof(hdr));
    164   1.7     enami 	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.7     enami 	header->id = LEINT16(hdr, PCI_EXROM_DATA_VENDOR_ID) |
    167   1.7     enami 	    (LEINT16(hdr, PCI_EXROM_DATA_DEVICE_ID) << 16);
    168   1.7     enami 	header->structure_length = LEINT16(hdr, PCI_EXROM_DATA_LENGTH);
    169   1.7     enami 	header->structure_rev = hdr[PCI_EXROM_DATA_REV];
    170   1.7     enami 	header->class = (hdr[PCI_EXROM_DATA_CLASS_CODE] << 8) |
    171   1.7     enami 	    (hdr[PCI_EXROM_DATA_CLASS_CODE + 1] << 16) |
    172   1.7     enami 	    (hdr[PCI_EXROM_DATA_CLASS_CODE + 2] << 24);
    173   1.7     enami 	header->image_length = LEINT16(hdr, PCI_EXROM_DATA_IMAGE_LENGTH) << 16;
    174   1.7     enami 	header->data_revision = LEINT16(hdr, PCI_EXROM_DATA_DATA_REV);
    175   1.7     enami 	header->code_type = hdr[PCI_EXROM_DATA_CODE_TYPE];
    176   1.7     enami 	header->indicator = hdr[PCI_EXROM_DATA_INDICATOR];
    177  1.13  riastrad 	length = uimin(length, header->image_length - 0x18 - offset);
    178   1.7     enami 	bus_space_read_region_1(tag, handle, dataptr + 0x18 + offset,
    179   1.7     enami 	    buf, length);
    180   1.7     enami 	ret = length;
    181   1.1      joda }
    182   1.1      joda #endif
    183