Home | History | Annotate | Line # | Download | only in ofw
igsfb_ofbus.c revision 1.13.12.3
      1  1.13.12.2       tls /*	$NetBSD: igsfb_ofbus.c,v 1.13.12.3 2017/12/03 11:36:42 jdolecek Exp $ */
      2        1.1  macallan 
      3        1.1  macallan /*
      4        1.1  macallan  * Copyright (c) 2006 Michael Lorenz
      5        1.1  macallan  * All rights reserved.
      6        1.1  macallan  *
      7        1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8        1.1  macallan  * modification, are permitted provided that the following conditions
      9        1.1  macallan  * are met:
     10        1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11        1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12        1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14        1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15        1.1  macallan  *
     16        1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17        1.1  macallan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18        1.1  macallan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19        1.1  macallan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20        1.1  macallan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21        1.1  macallan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22        1.1  macallan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23        1.1  macallan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24        1.1  macallan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25        1.1  macallan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26        1.1  macallan  */
     27        1.1  macallan 
     28        1.1  macallan /*
     29        1.1  macallan  * Integraphics Systems IGA 168x and CyberPro series.
     30        1.1  macallan  * ofbus attachment for Valeriy E. Ushakov's igsfb driver
     31        1.1  macallan  */
     32        1.1  macallan 
     33        1.1  macallan #include <sys/cdefs.h>
     34  1.13.12.2       tls __KERNEL_RCSID(0, "$NetBSD: igsfb_ofbus.c,v 1.13.12.3 2017/12/03 11:36:42 jdolecek Exp $");
     35        1.1  macallan 
     36        1.1  macallan #include <sys/param.h>
     37        1.1  macallan #include <sys/systm.h>
     38        1.1  macallan #include <sys/kernel.h>
     39        1.1  macallan #include <sys/device.h>
     40        1.1  macallan #include <sys/malloc.h>
     41        1.1  macallan #include <sys/buf.h>
     42       1.12    dyoung #include <sys/bus.h>
     43       1.11  macallan #include <uvm/uvm.h>
     44        1.1  macallan 
     45        1.1  macallan #include <machine/intr.h>
     46        1.1  macallan #include <machine/ofw.h>
     47       1.11  macallan #include <machine/pmap.h>
     48        1.1  macallan 
     49        1.1  macallan #include <dev/isa/isavar.h>
     50        1.1  macallan 
     51        1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     52        1.1  macallan #include <dev/wscons/wsconsio.h>
     53        1.1  macallan #include <dev/rasops/rasops.h>
     54        1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     55        1.1  macallan #include <dev/ofw/openfirm.h>
     56        1.1  macallan 
     57        1.1  macallan #include <dev/ic/igsfbreg.h>
     58        1.1  macallan #include <dev/ic/igsfbvar.h>
     59        1.3       uwe #include <shark/ofw/igsfb_ofbusvar.h>
     60        1.1  macallan 
     61        1.1  macallan static int	igsfb_ofbus_is_console(int);
     62        1.1  macallan 
     63        1.1  macallan static int igsfb_ofbus_console = 0;
     64        1.1  macallan static int igsfb_ofbus_phandle = 0;
     65        1.1  macallan 
     66        1.1  macallan 
     67        1.1  macallan 
     68       1.13       mrg static int	igsfb_ofbus_match(device_t, cfdata_t, void *);
     69       1.13       mrg static void	igsfb_ofbus_attach(device_t, device_t, void *);
     70        1.1  macallan static int	igsfb_setup_dc(struct igsfb_devconfig *);
     71        1.9  macallan static paddr_t	igsfb_ofbus_mmap(void *, void *, off_t, int);
     72        1.1  macallan 
     73       1.13       mrg CFATTACH_DECL_NEW(igsfb_ofbus, sizeof(struct igsfb_softc),
     74        1.1  macallan     igsfb_ofbus_match, igsfb_ofbus_attach, NULL, NULL);
     75        1.1  macallan 
     76  1.13.12.2       tls static const char * const compat_strings[] = { "igs,cyperpro2010", NULL };
     77        1.1  macallan 
     78        1.1  macallan vaddr_t igsfb_mem_vaddr = 0, igsfb_mmio_vaddr = 0;
     79        1.1  macallan paddr_t igsfb_mem_paddr;
     80        1.9  macallan extern paddr_t isa_io_physaddr;
     81        1.1  macallan struct bus_space igsfb_memt, igsfb_iot;
     82        1.1  macallan 
     83        1.7      jmmv #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
     84        1.2  macallan extern int console_ihandle;
     85        1.5      jmmv #endif
     86        1.2  macallan 
     87        1.1  macallan int
     88        1.1  macallan igsfb_ofbus_cnattach(bus_space_tag_t iot, bus_space_tag_t memt)
     89        1.1  macallan {
     90        1.1  macallan 	struct igsfb_devconfig *dc;
     91        1.1  macallan 	int ret;
     92        1.1  macallan 	int chosen_phandle, igs_node;
     93        1.1  macallan 	int stdout_ihandle, stdout_phandle;
     94        1.1  macallan 	uint32_t regs[16];
     95       1.10  macallan 	char mode_buffer[64];
     96        1.1  macallan 
     97        1.1  macallan 	stdout_phandle = 0;
     98        1.1  macallan 
     99        1.1  macallan 	/* first find out if there's a CyberPro at all in this machine */
    100        1.4       uwe 	igs_node = OF_finddevice("/vlbus/display");
    101        1.4       uwe 	if (igs_node == -1)
    102        1.1  macallan 		return ENXIO;
    103        1.1  macallan 	if (of_compatible(igs_node, compat_strings) < 0)
    104        1.1  macallan 		return ENXIO;
    105        1.4       uwe 
    106        1.1  macallan 	/*
    107        1.1  macallan 	 * now we know there's a CyberPro in this machine so map it into
    108        1.1  macallan 	 * kernel space, even if it's not the console
    109        1.1  macallan 	 */
    110        1.1  macallan 	if (OF_getprop(igs_node, "reg", regs, sizeof(regs)) <= 0)
    111        1.1  macallan 		return ENXIO;
    112        1.1  macallan 
    113        1.1  macallan 	igsfb_mem_paddr = be32toh(regs[13]);
    114        1.9  macallan 	/* 4MB VRAM aperture, bufferable and cacheable */
    115  1.13.12.3  jdolecek 	igsfb_mem_vaddr = ofw_map(igsfb_mem_paddr, 0x00400000, L2_B);
    116        1.1  macallan 	/* MMIO registers */
    117        1.1  macallan 	igsfb_mmio_vaddr = ofw_map(igsfb_mem_paddr + IGS_MEM_MMIO_SELECT,
    118        1.1  macallan 	    0x00100000, 0);
    119        1.1  macallan 
    120        1.1  macallan 	memcpy(&igsfb_memt, memt, sizeof(struct bus_space));
    121        1.1  macallan 	igsfb_memt.bs_cookie = (void *)igsfb_mem_vaddr;
    122        1.1  macallan 	memcpy(&igsfb_iot, memt, sizeof(struct bus_space));
    123        1.1  macallan 	igsfb_iot.bs_cookie = (void *)igsfb_mmio_vaddr;
    124        1.1  macallan 
    125        1.1  macallan 	/*
    126        1.1  macallan 	 * check if the firmware output device is indeed the CyberPro
    127        1.1  macallan 	 */
    128        1.1  macallan 	if ((chosen_phandle = OF_finddevice("/chosen")) == -1 ||
    129        1.1  macallan 	    OF_getprop(chosen_phandle, "stdout", &stdout_ihandle,
    130        1.1  macallan 	    sizeof(stdout_ihandle)) != sizeof(stdout_ihandle)) {
    131        1.1  macallan 		return ENXIO;
    132        1.1  macallan 	}
    133        1.4       uwe 	stdout_ihandle = of_decode_int((void *)&stdout_ihandle);
    134        1.1  macallan 	stdout_phandle = OF_instance_to_package(stdout_ihandle);
    135        1.1  macallan 
    136  1.13.12.3  jdolecek 	if (stdout_phandle != igs_node) {
    137  1.13.12.3  jdolecek 		/*
    138  1.13.12.3  jdolecek 		 * If we aren't the boot console, the CyberPro probably
    139  1.13.12.3  jdolecek 		 * hasn't been brought up yet.  Bring it up now, it's
    140  1.13.12.3  jdolecek 		 * still early enough to do so.
    141  1.13.12.3  jdolecek 		 */
    142  1.13.12.3  jdolecek 		const int handle = OF_open("/vlbus/display");
    143  1.13.12.3  jdolecek 		if (handle != -1)
    144  1.13.12.3  jdolecek 			OF_close(handle);
    145        1.1  macallan 		return ENXIO;
    146  1.13.12.3  jdolecek 	}
    147        1.1  macallan 
    148        1.1  macallan 	/* ok, now setup and attach the console */
    149        1.1  macallan 	dc = &igsfb_console_dc;
    150        1.1  macallan 	ret = igsfb_setup_dc(dc);
    151        1.1  macallan 	if (ret)
    152        1.1  macallan 		return ret;
    153        1.1  macallan 
    154       1.10  macallan 	if (of_get_mode_string(mode_buffer, sizeof(mode_buffer))) {
    155       1.10  macallan 		strcpy(dc->dc_modestring, mode_buffer);
    156       1.10  macallan 	}
    157       1.10  macallan 
    158        1.1  macallan 	ret = igsfb_cnattach_subr(dc);
    159        1.1  macallan 	if (ret)
    160        1.1  macallan 		return ret;
    161        1.1  macallan 
    162        1.1  macallan 	igsfb_ofbus_console = 1;
    163        1.1  macallan 	igsfb_ofbus_phandle = stdout_phandle;
    164        1.7      jmmv #if (NIGSFB_OFBUS > 0) || (NVGA_OFBUS > 0)
    165        1.2  macallan 	console_ihandle = stdout_ihandle;
    166        1.5      jmmv #endif
    167        1.1  macallan 	return 0;
    168        1.1  macallan }
    169        1.1  macallan 
    170        1.1  macallan static int
    171        1.1  macallan igsfb_setup_dc(struct igsfb_devconfig *dc)
    172        1.1  macallan {
    173        1.1  macallan 	int ret;
    174        1.1  macallan 
    175        1.4       uwe 	dc->dc_id = 0x2010;
    176        1.1  macallan 	dc->dc_memt = &igsfb_memt;
    177        1.1  macallan 	dc->dc_memaddr = 0;
    178        1.1  macallan 	dc->dc_memsz= 0x00400000;
    179        1.4       uwe 	dc->dc_memflags = 0;
    180        1.1  macallan 
    181        1.1  macallan 	dc->dc_iot = &igsfb_iot;
    182        1.1  macallan 	dc->dc_iobase = 0;
    183        1.1  macallan 	dc->dc_ioflags = 0;
    184        1.9  macallan 	dc->dc_mmap = igsfb_ofbus_mmap;
    185        1.1  macallan 	if (bus_space_map(dc->dc_iot,
    186        1.1  macallan 			  dc->dc_iobase + IGS_REG_BASE, IGS_REG_SIZE,
    187        1.1  macallan 			  dc->dc_ioflags,
    188        1.1  macallan 			  &dc->dc_ioh) != 0)
    189        1.1  macallan 	{
    190        1.1  macallan 		printf("unable to map I/O registers\n");
    191        1.1  macallan 		return 1;
    192        1.1  macallan 	}
    193        1.1  macallan 	ret = igsfb_enable(dc->dc_iot, dc->dc_iobase, dc->dc_ioflags);
    194        1.1  macallan 	if (ret)
    195        1.1  macallan 		return ret;
    196        1.1  macallan 	return 0;
    197        1.1  macallan }
    198        1.1  macallan 
    199        1.1  macallan static int
    200        1.1  macallan igsfb_ofbus_is_console(int phandle)
    201        1.1  macallan {
    202        1.1  macallan 
    203        1.1  macallan 	return igsfb_ofbus_console && (phandle == igsfb_ofbus_phandle);
    204        1.1  macallan }
    205        1.1  macallan 
    206        1.1  macallan 
    207        1.1  macallan static int
    208       1.13       mrg igsfb_ofbus_match(device_t parent, cfdata_t match, void *aux)
    209        1.1  macallan {
    210        1.1  macallan 	struct ofbus_attach_args *oba = aux;
    211        1.1  macallan 
    212        1.4       uwe 	if (of_compatible(oba->oba_phandle, compat_strings) < 0)
    213        1.4       uwe 		return 0;
    214        1.4       uwe 
    215        1.1  macallan 	return 10;	/* beat vga etc. */
    216        1.1  macallan }
    217        1.1  macallan 
    218        1.1  macallan static void
    219       1.13       mrg igsfb_ofbus_attach(device_t parent, device_t self, void *aux)
    220        1.1  macallan {
    221  1.13.12.1       tls 	struct igsfb_softc *sc = device_private(self);
    222        1.1  macallan 	struct ofbus_attach_args *oba = aux;
    223        1.1  macallan 	uint32_t regs[16];
    224        1.1  macallan 	int isconsole, ret;
    225       1.10  macallan 
    226       1.13       mrg 	sc->sc_dev = self;
    227        1.1  macallan 	if (igsfb_ofbus_is_console(oba->oba_phandle)) {
    228        1.4       uwe 		isconsole = 1;
    229        1.1  macallan 		sc->sc_dc = &igsfb_console_dc;
    230        1.1  macallan 	} else {
    231        1.4       uwe 		isconsole = 0;
    232        1.1  macallan 		sc->sc_dc = malloc(sizeof(struct igsfb_devconfig),
    233        1.1  macallan 				   M_DEVBUF, M_NOWAIT | M_ZERO);
    234        1.1  macallan 		if (sc->sc_dc == NULL)
    235        1.1  macallan 			panic("unable to allocate igsfb_devconfig");
    236        1.4       uwe 		if (OF_getprop(oba->oba_phandle, "reg",
    237        1.4       uwe 			       regs, sizeof(regs)) <= 0)
    238        1.4       uwe 		{
    239        1.4       uwe 			printf(": unable to read 'reg' property\n");
    240        1.1  macallan 			return;
    241        1.1  macallan 		}
    242        1.1  macallan 		ret = igsfb_setup_dc(sc->sc_dc);
    243        1.1  macallan 		if (ret)
    244        1.1  macallan 			return;
    245        1.1  macallan 	}
    246        1.1  macallan 	printf(": IGS CyberPro 2010 at 0x%08x\n", (uint32_t)igsfb_mem_paddr);
    247        1.1  macallan 
    248        1.1  macallan 	igsfb_attach_subr(sc, isconsole);
    249        1.1  macallan }
    250        1.9  macallan 
    251        1.9  macallan static paddr_t
    252        1.9  macallan igsfb_ofbus_mmap(void *v, void *vs, off_t offset, int prot)
    253        1.9  macallan {
    254        1.9  macallan 
    255        1.9  macallan #ifdef PCI_MAGIC_IO_RANGE
    256        1.9  macallan 	/* access to IO ports */
    257        1.9  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    258        1.9  macallan 	    (offset < (PCI_MAGIC_IO_RANGE + 0x10000))) {
    259        1.9  macallan 		paddr_t pa;
    260        1.9  macallan 
    261        1.9  macallan 		pa = isa_io_physaddr + offset - PCI_MAGIC_IO_RANGE;
    262        1.9  macallan 		return arm_btop(pa);
    263        1.9  macallan 	}
    264        1.9  macallan #endif
    265        1.9  macallan 	/*
    266        1.9  macallan 	 * we also need to allow mapping of the whole aperture, including MMIO
    267        1.9  macallan 	 * registers on CyberPro at its physical address
    268        1.9  macallan 	 */
    269        1.9  macallan 	if ((offset >= igsfb_mem_paddr) &&
    270       1.11  macallan 	    (offset < (igsfb_mem_paddr + 0x00800000))) {
    271       1.11  macallan 		return (arm_btop(offset) | ARM32_MMAP_WRITECOMBINE);
    272       1.11  macallan 	}
    273       1.11  macallan 	if ((offset >= (igsfb_mem_paddr + 0x00800000)) &&
    274        1.9  macallan 	    (offset < (igsfb_mem_paddr + 0x01000000)))
    275        1.9  macallan 		return arm_btop(offset);
    276        1.9  macallan 
    277        1.9  macallan 	return -1;
    278        1.9  macallan }
    279