Home | History | Annotate | Line # | Download | only in pci
igsfb_pci.c revision 1.7.2.1
      1  1.7.2.1    skrll /*	$NetBSD: igsfb_pci.c,v 1.7.2.1 2004/08/03 10:49:09 skrll Exp $ */
      2      1.1      uwe 
      3      1.1      uwe /*
      4      1.7      uwe  * Copyright (c) 2002, 2003 Valeriy E. Ushakov
      5      1.1      uwe  * All rights reserved.
      6      1.1      uwe  *
      7      1.1      uwe  * Redistribution and use in source and binary forms, with or without
      8      1.1      uwe  * modification, are permitted provided that the following conditions
      9      1.1      uwe  * are met:
     10      1.1      uwe  * 1. Redistributions of source code must retain the above copyright
     11      1.1      uwe  *    notice, this list of conditions and the following disclaimer.
     12      1.1      uwe  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1      uwe  *    notice, this list of conditions and the following disclaimer in the
     14      1.1      uwe  *    documentation and/or other materials provided with the distribution.
     15      1.1      uwe  * 3. The name of the author may not be used to endorse or promote products
     16      1.1      uwe  *    derived from this software without specific prior written permission
     17      1.1      uwe  *
     18      1.1      uwe  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19      1.1      uwe  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20      1.1      uwe  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21      1.1      uwe  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22      1.1      uwe  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23      1.1      uwe  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24      1.1      uwe  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25      1.1      uwe  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26      1.1      uwe  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27      1.1      uwe  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28      1.1      uwe  */
     29      1.1      uwe 
     30      1.1      uwe /*
     31      1.3      uwe  * Integraphics Systems IGA 168x and CyberPro series.
     32      1.1      uwe  */
     33      1.1      uwe #include <sys/cdefs.h>
     34  1.7.2.1    skrll __KERNEL_RCSID(0, "$NetBSD: igsfb_pci.c,v 1.7.2.1 2004/08/03 10:49:09 skrll Exp $");
     35      1.1      uwe 
     36      1.1      uwe #include <sys/param.h>
     37      1.1      uwe #include <sys/systm.h>
     38      1.1      uwe #include <sys/kernel.h>
     39      1.1      uwe #include <sys/device.h>
     40      1.1      uwe #include <sys/malloc.h>
     41      1.1      uwe #include <sys/buf.h>
     42      1.1      uwe 
     43      1.4      uwe #ifdef __sparc__  /* XXX: this doesn't belong here */
     44      1.1      uwe #include <machine/autoconf.h>
     45      1.4      uwe #endif
     46      1.1      uwe #include <machine/bus.h>
     47      1.1      uwe #include <machine/intr.h>
     48      1.1      uwe 
     49      1.1      uwe #include <dev/pci/pcivar.h>
     50      1.1      uwe #include <dev/pci/pcireg.h>
     51      1.1      uwe #include <dev/pci/pcidevs.h>
     52      1.1      uwe 
     53      1.7      uwe #include <dev/wscons/wsdisplayvar.h>
     54      1.1      uwe #include <dev/wscons/wsconsio.h>
     55      1.7      uwe #include <dev/rasops/rasops.h>
     56      1.7      uwe 
     57      1.1      uwe #include <dev/ic/igsfbreg.h>
     58      1.1      uwe #include <dev/ic/igsfbvar.h>
     59      1.7      uwe #include <dev/pci/igsfb_pcivar.h>
     60      1.7      uwe 
     61      1.7      uwe 
     62      1.7      uwe static int	igsfb_pci_match_by_id(pcireg_t);
     63      1.7      uwe static int	igsfb_pci_map_regs(struct igsfb_devconfig *,
     64      1.7      uwe 				   bus_space_tag_t, bus_space_tag_t,
     65      1.7      uwe 				   pci_chipset_tag_t,
     66      1.7      uwe 				   pcitag_t, pci_product_id_t);
     67      1.7      uwe static int	igsfb_pci_is_console(pci_chipset_tag_t, pcitag_t);
     68      1.7      uwe 
     69      1.7      uwe static int igsfb_pci_console = 0;
     70      1.7      uwe static pcitag_t igsfb_pci_constag;
     71      1.7      uwe 
     72      1.1      uwe 
     73      1.1      uwe 
     74      1.1      uwe static int	igsfb_pci_match(struct device *, struct cfdata *, void *);
     75      1.1      uwe static void	igsfb_pci_attach(struct device *, struct device *, void *);
     76      1.1      uwe 
     77      1.5  thorpej CFATTACH_DECL(igsfb_pci, sizeof(struct igsfb_softc),
     78      1.6  thorpej     igsfb_pci_match, igsfb_pci_attach, NULL, NULL);
     79      1.1      uwe 
     80      1.7      uwe 
     81      1.1      uwe static int
     82      1.7      uwe igsfb_pci_match_by_id(id)
     83      1.7      uwe 	pcireg_t id;
     84      1.1      uwe {
     85      1.1      uwe 
     86      1.7      uwe 	if (PCI_VENDOR(id) != PCI_VENDOR_INTEGRAPHICS)
     87      1.7      uwe 		return (0);
     88      1.7      uwe 
     89      1.7      uwe 	switch (PCI_PRODUCT(id)) {
     90      1.7      uwe 	case PCI_PRODUCT_INTEGRAPHICS_IGA1682:		/* FALLTHROUGH */
     91      1.7      uwe 	case PCI_PRODUCT_INTEGRAPHICS_CYBERPRO2000:	/* FALLTHROUGH */
     92      1.7      uwe 	case PCI_PRODUCT_INTEGRAPHICS_CYBERPRO2010:
     93      1.7      uwe 		return (1);
     94      1.7      uwe 	default:
     95      1.1      uwe 		return (0);
     96      1.7      uwe 	}
     97      1.7      uwe }
     98      1.1      uwe 
     99      1.7      uwe 
    100      1.7      uwe int
    101      1.7      uwe igsfb_pci_cnattach(iot, memt, pc, bus, device, function)
    102      1.7      uwe 	bus_space_tag_t iot, memt;
    103      1.7      uwe 	pci_chipset_tag_t pc;
    104      1.7      uwe 	int bus, device, function;
    105      1.7      uwe {
    106      1.7      uwe 	struct igsfb_devconfig *dc;
    107      1.7      uwe 	pcitag_t tag;
    108      1.7      uwe 	pcireg_t id;
    109      1.7      uwe 	int ret;
    110      1.7      uwe 
    111      1.7      uwe 	tag = pci_make_tag(pc, bus, device, function);
    112      1.7      uwe 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    113      1.7      uwe 
    114      1.7      uwe 	if (igsfb_pci_match_by_id(id) == 0)
    115      1.1      uwe 		return (1);
    116      1.1      uwe 
    117      1.7      uwe 	dc = &igsfb_console_dc;
    118      1.7      uwe 	if (igsfb_pci_map_regs(dc, iot, memt, pc, tag, PCI_PRODUCT(id)) != 0)
    119      1.4      uwe 		return (1);
    120      1.4      uwe 
    121      1.7      uwe 	ret = igsfb_enable(dc->dc_iot, dc->dc_iobase, dc->dc_ioflags);
    122      1.7      uwe 	if (ret)
    123      1.7      uwe 		return (ret);
    124      1.7      uwe 
    125      1.7      uwe 	ret = igsfb_cnattach_subr(dc);
    126      1.7      uwe 	if (ret)
    127      1.7      uwe 		return (ret);
    128      1.7      uwe 
    129      1.7      uwe 	igsfb_pci_console = 1;
    130      1.7      uwe 	igsfb_pci_constag = tag;
    131      1.7      uwe 
    132      1.1      uwe 	return (0);
    133      1.1      uwe }
    134      1.1      uwe 
    135      1.1      uwe 
    136      1.7      uwe static int
    137      1.7      uwe igsfb_pci_is_console(pc, tag)
    138      1.7      uwe 	pci_chipset_tag_t pc;
    139      1.7      uwe 	pcitag_t tag;
    140      1.7      uwe {
    141      1.7      uwe 
    142      1.7      uwe 	return (igsfb_pci_is_console && (tag == igsfb_pci_constag));
    143      1.7      uwe }
    144      1.7      uwe 
    145      1.7      uwe 
    146      1.7      uwe static int
    147      1.7      uwe igsfb_pci_match(parent, match, aux)
    148      1.7      uwe 	struct device *parent;
    149      1.7      uwe 	struct cfdata *match;
    150      1.7      uwe 	void *aux;
    151      1.7      uwe {
    152      1.7      uwe 	struct pci_attach_args *pa = aux;
    153      1.7      uwe 
    154      1.7      uwe 	return (igsfb_pci_match_by_id(pa->pa_id));
    155      1.7      uwe }
    156      1.7      uwe 
    157      1.7      uwe 
    158      1.1      uwe static void
    159      1.1      uwe igsfb_pci_attach(parent, self, aux)
    160      1.1      uwe 	struct device *parent, *self;
    161      1.1      uwe 	void *aux;
    162      1.1      uwe {
    163      1.1      uwe 	struct igsfb_softc *sc = (struct igsfb_softc *)self;
    164      1.1      uwe 	struct pci_attach_args *pa = aux;
    165      1.1      uwe 	int isconsole;
    166      1.1      uwe 	char devinfo[256];
    167      1.1      uwe 
    168  1.7.2.1    skrll 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    169      1.4      uwe 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    170      1.1      uwe 
    171      1.7      uwe 
    172      1.7      uwe #if defined(__sparc__) && !defined(KRUPS_FORCE_SERIAL_CONSOLE)
    173      1.7      uwe 	/* XXX: this doesn't belong here */
    174      1.7      uwe 	if (PCITAG_NODE(pa->pa_tag) == prom_instance_to_package(prom_stdout()))
    175      1.7      uwe 	{
    176      1.7      uwe 		int b, d, f;
    177      1.7      uwe 
    178      1.7      uwe 		pci_decompose_tag(pa->pa_pc, pa->pa_tag, &b, &d, &f);
    179      1.7      uwe 		igsfb_pci_cnattach(pa->pa_iot, pa->pa_memt, pa->pa_pc, b,d,f);
    180      1.7      uwe 	}
    181      1.7      uwe #endif
    182      1.7      uwe 
    183      1.7      uwe 	isconsole = 0;
    184      1.7      uwe 	if (igsfb_pci_is_console(pa->pa_pc, pa->pa_tag)) {
    185      1.7      uwe 		sc->sc_dc = &igsfb_console_dc;
    186      1.7      uwe 		isconsole = 1;
    187      1.7      uwe 	} else {
    188      1.7      uwe 		sc->sc_dc = malloc(sizeof(struct igsfb_devconfig),
    189      1.7      uwe 				   M_DEVBUF, M_NOWAIT | M_ZERO);
    190      1.7      uwe 		if (sc->sc_dc == NULL)
    191      1.7      uwe 			panic("unable to allocate igsfb_devconfig");
    192      1.7      uwe 		if (igsfb_pci_map_regs(sc->sc_dc,
    193      1.7      uwe 			    pa->pa_iot, pa->pa_memt, pa->pa_pc,
    194      1.7      uwe 			    pa->pa_tag, PCI_PRODUCT(pa->pa_id)) != 0)
    195      1.7      uwe 		{
    196      1.7      uwe 			printf("unable to map device registers\n");
    197      1.7      uwe 			free(sc->sc_dc, M_DEVBUF);
    198      1.7      uwe 			sc->sc_dc = NULL;
    199      1.7      uwe 			return;
    200      1.7      uwe 		}
    201      1.7      uwe 
    202      1.7      uwe 		igsfb_enable(sc->sc_dc->dc_iot, sc->sc_dc->dc_iobase,
    203      1.7      uwe 			     sc->sc_dc->dc_ioflags);
    204      1.7      uwe 	}
    205      1.7      uwe 
    206      1.7      uwe 	igsfb_attach_subr(sc, isconsole);
    207      1.7      uwe }
    208      1.7      uwe 
    209      1.7      uwe 
    210      1.7      uwe /*
    211      1.7      uwe  * Init memory and i/o bus space tags.  Map device registers.
    212      1.7      uwe  * Use memory space mapped i/o space access for i/o registers
    213      1.7      uwe  * for CyberPro cards.
    214      1.7      uwe  */
    215      1.7      uwe static int
    216      1.7      uwe igsfb_pci_map_regs(dc, iot, memt, pc, tag, id)
    217      1.7      uwe 	struct igsfb_devconfig *dc;
    218      1.7      uwe 	bus_space_tag_t iot, memt;
    219      1.7      uwe 	pci_chipset_tag_t pc;
    220      1.7      uwe 	pcitag_t tag;
    221      1.7      uwe 	pci_product_id_t id;
    222      1.7      uwe {
    223      1.7      uwe 
    224      1.7      uwe 	dc->dc_id = id;
    225      1.1      uwe 
    226      1.1      uwe 	/*
    227      1.3      uwe 	 * Configure memory space first since for CyberPro we use
    228      1.3      uwe 	 * memory-mapped i/o access.  Note that we are NOT mapping any
    229      1.4      uwe 	 * of it yet.  (XXX: search for memory BAR?)
    230      1.1      uwe 	 */
    231      1.1      uwe #define IGS_MEM_MAPREG (PCI_MAPREG_START + 0)
    232      1.1      uwe 
    233      1.7      uwe 	dc->dc_memt = memt;
    234      1.7      uwe 	if (pci_mapreg_info(pc, tag,
    235      1.1      uwe 		IGS_MEM_MAPREG, PCI_MAPREG_TYPE_MEM,
    236      1.7      uwe 		&dc->dc_memaddr, &dc->dc_memsz, &dc->dc_memflags) != 0)
    237      1.1      uwe 	{
    238      1.1      uwe 		printf("unable to configure memory space\n");
    239      1.7      uwe 		return (1);
    240      1.1      uwe 	}
    241      1.1      uwe 
    242      1.1      uwe 	/*
    243      1.7      uwe 	 * Configure I/O space.  On CyberPro use MMIO.  IGS 168x doesn't
    244      1.7      uwe 	 * have a BAR for its i/o space, so we have to hardcode it.
    245      1.1      uwe 	 */
    246      1.7      uwe 	if (id >= PCI_PRODUCT_INTEGRAPHICS_CYBERPRO2000) {
    247      1.7      uwe 		dc->dc_iot = dc->dc_memt;
    248      1.7      uwe 		dc->dc_iobase = dc->dc_memaddr | IGS_MEM_MMIO_SELECT;
    249      1.7      uwe 		dc->dc_ioflags = dc->dc_memflags;
    250      1.1      uwe 	} else {
    251      1.7      uwe 		dc->dc_iot = iot;
    252      1.7      uwe 		dc->dc_iobase = 0;
    253      1.7      uwe 		dc->dc_ioflags = 0;
    254      1.1      uwe 	}
    255      1.1      uwe 
    256      1.3      uwe 	/*
    257      1.3      uwe 	 * Map I/O registers.  This is done in bus glue, not in common
    258      1.3      uwe 	 * code because on e.g. ISA bus we'd need to access registers
    259      1.3      uwe 	 * to obtain/program linear memory location.
    260      1.3      uwe 	 */
    261      1.7      uwe 	if (bus_space_map(dc->dc_iot,
    262      1.7      uwe 			  dc->dc_iobase + IGS_REG_BASE, IGS_REG_SIZE,
    263      1.7      uwe 			  dc->dc_ioflags,
    264      1.7      uwe 			  &dc->dc_ioh) != 0)
    265      1.1      uwe 	{
    266      1.3      uwe 		printf("unable to map I/O registers\n");
    267      1.7      uwe 		return (1);
    268      1.1      uwe 	}
    269      1.1      uwe 
    270      1.7      uwe 	return (0);
    271      1.1      uwe }
    272