Home | History | Annotate | Line # | Download | only in dev
gt.c revision 1.34
      1  1.34  thorpej /*	$NetBSD: gt.c,v 1.34 2021/08/07 16:18:47 thorpej Exp $	*/
      2   1.1    soren 
      3   1.1    soren /*
      4   1.1    soren  * Copyright (c) 2000 Soren S. Jorvang.  All rights reserved.
      5   1.1    soren  *
      6   1.1    soren  * Redistribution and use in source and binary forms, with or without
      7   1.1    soren  * modification, are permitted provided that the following conditions
      8   1.1    soren  * are met:
      9   1.1    soren  * 1. Redistributions of source code must retain the above copyright
     10   1.1    soren  *    notice, this list of conditions, and the following disclaimer.
     11   1.1    soren  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1    soren  *    notice, this list of conditions and the following disclaimer in the
     13   1.1    soren  *    documentation and/or other materials provided with the distribution.
     14   1.1    soren  *
     15   1.1    soren  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16   1.1    soren  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17   1.1    soren  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18   1.1    soren  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19   1.1    soren  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20   1.1    soren  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21   1.1    soren  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22   1.1    soren  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23   1.1    soren  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24   1.1    soren  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25   1.1    soren  * SUCH DAMAGE.
     26   1.1    soren  */
     27   1.9    lukem 
     28   1.9    lukem #include <sys/cdefs.h>
     29  1.34  thorpej __KERNEL_RCSID(0, "$NetBSD: gt.c,v 1.34 2021/08/07 16:18:47 thorpej Exp $");
     30  1.13  tsutsui 
     31  1.13  tsutsui #include "opt_pci.h"
     32  1.13  tsutsui #include "pci.h"
     33   1.1    soren 
     34   1.1    soren #include <sys/param.h>
     35  1.27     matt #include <sys/bus.h>
     36  1.27     matt #include <sys/conf.h>
     37  1.27     matt #include <sys/device.h>
     38  1.27     matt #include <sys/file.h>
     39  1.27     matt #include <sys/intr.h>
     40   1.1    soren #include <sys/ioctl.h>
     41  1.27     matt #include <sys/kernel.h>
     42  1.27     matt #include <sys/malloc.h>
     43  1.27     matt #include <sys/proc.h>
     44   1.1    soren #include <sys/select.h>
     45  1.27     matt #include <sys/syslog.h>
     46  1.27     matt #include <sys/systm.h>
     47   1.1    soren #include <sys/tty.h>
     48   1.1    soren #include <sys/uio.h>
     49   1.1    soren 
     50  1.10  tsutsui #include <machine/autoconf.h>
     51   1.1    soren 
     52  1.16  tsutsui #include <mips/cache.h>
     53  1.16  tsutsui 
     54   1.1    soren #include <dev/pci/pcivar.h>
     55  1.13  tsutsui #ifdef PCI_NETBSD_CONFIGURE
     56  1.13  tsutsui #include <dev/pci/pciconf.h>
     57  1.13  tsutsui #endif
     58  1.10  tsutsui 
     59  1.29    skrll #include <cobalt/dev/gtvar.h>
     60  1.10  tsutsui #include <cobalt/dev/gtreg.h>
     61  1.10  tsutsui 
     62   1.1    soren struct gt_softc {
     63  1.21  tsutsui 	device_t	sc_dev;
     64  1.10  tsutsui 
     65  1.10  tsutsui 	bus_space_tag_t sc_bst;
     66  1.10  tsutsui 	bus_space_handle_t sc_bsh;
     67  1.11  tsutsui 	struct cobalt_pci_chipset sc_pc;
     68   1.1    soren };
     69   1.1    soren 
     70  1.21  tsutsui static int	gt_match(device_t, cfdata_t, void *);
     71  1.21  tsutsui static void	gt_attach(device_t, device_t, void *);
     72   1.1    soren static int	gt_print(void *aux, const char *pnp);
     73   1.1    soren 
     74  1.10  tsutsui static void	gt_timer_init(struct gt_softc *sc);
     75  1.20  tsutsui #if 0 /* unused */
     76  1.10  tsutsui static void	gt_timer0_init(void *);
     77  1.10  tsutsui static long	gt_timer0_read(void *);
     78  1.20  tsutsui #endif
     79  1.10  tsutsui 
     80  1.29    skrll struct mips_bus_space gt_iot;
     81  1.29    skrll struct mips_bus_space gt_memt;
     82  1.29    skrll 
     83  1.21  tsutsui CFATTACH_DECL_NEW(gt, sizeof(struct gt_softc),
     84   1.7  thorpej     gt_match, gt_attach, NULL, NULL);
     85   1.1    soren 
     86  1.32  thorpej #define	PCI_IO_START	0x00001000
     87  1.32  thorpej #define	PCI_IO_END	0x01ffffff
     88  1.32  thorpej #define	PCI_IO_SIZE	((PCI_IO_END - PCI_IO_START) + 1)
     89  1.32  thorpej 
     90  1.32  thorpej #define	PCI_MEM_START	0x12000000
     91  1.32  thorpej #define	PCI_MEM_END	0x13ffffff
     92  1.32  thorpej #define	PCI_MEM_SIZE	((PCI_MEM_END - PCI_MEM_START) + 1)
     93  1.32  thorpej 
     94   1.4    soren static int
     95  1.21  tsutsui gt_match(device_t parent, cfdata_t cf, void *aux)
     96   1.1    soren {
     97  1.15  tsutsui 
     98   1.1    soren 	return 1;
     99   1.1    soren }
    100   1.1    soren 
    101  1.10  tsutsui #define GT_REG_REGION	0x1000
    102  1.10  tsutsui 
    103   1.4    soren static void
    104  1.21  tsutsui gt_attach(device_t parent, device_t self, void *aux)
    105   1.1    soren {
    106  1.21  tsutsui 	struct gt_softc *sc = device_private(self);
    107  1.10  tsutsui 	struct mainbus_attach_args *ma = aux;
    108  1.10  tsutsui #if NPCI > 0
    109  1.11  tsutsui 	pci_chipset_tag_t pc;
    110   1.1    soren 	struct pcibus_attach_args pba;
    111  1.10  tsutsui #endif
    112  1.10  tsutsui 
    113  1.21  tsutsui 	sc->sc_dev = self;
    114  1.10  tsutsui 	sc->sc_bst = ma->ma_iot;
    115  1.10  tsutsui 	if (bus_space_map(sc->sc_bst, ma->ma_addr, GT_REG_REGION,
    116  1.10  tsutsui 	    0, &sc->sc_bsh)) {
    117  1.21  tsutsui 		aprint_error(": unable to map GT64111 registers\n");
    118  1.10  tsutsui 		return;
    119  1.10  tsutsui 	}
    120   1.1    soren 
    121  1.21  tsutsui 	aprint_normal("\n");
    122   1.1    soren 
    123  1.10  tsutsui 	gt_timer_init(sc);
    124  1.10  tsutsui 
    125  1.10  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_PCI_COMMAND,
    126  1.10  tsutsui 	    (bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_PCI_COMMAND) &
    127  1.10  tsutsui 	    ~PCI_SYNCMODE) | PCI_PCLK_HIGH);
    128   1.2    soren 
    129  1.19  tsutsui 	(void)bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_PCI_TIMEOUT_RETRY);
    130  1.19  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_PCI_TIMEOUT_RETRY,
    131  1.19  tsutsui 	    0x00 << PCI_RETRYCTR_SHIFT | 0xff << PCI_TIMEOUT1_SHIFT | 0xff);
    132  1.19  tsutsui 
    133  1.29    skrll 	gt_bus_mem_init(&gt_memt, NULL);
    134  1.29    skrll 	gt_bus_io_init(&gt_iot, NULL);
    135  1.29    skrll 
    136   1.1    soren #if NPCI > 0
    137  1.11  tsutsui 	pc = &sc->sc_pc;
    138  1.11  tsutsui 	pc->pc_bst = sc->sc_bst;
    139  1.11  tsutsui 	pc->pc_bsh = sc->sc_bsh;
    140  1.11  tsutsui 
    141  1.13  tsutsui #ifdef PCI_NETBSD_CONFIGURE
    142  1.32  thorpej 	struct pciconf_resources *pcires = pciconf_resource_init();
    143  1.32  thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
    144  1.32  thorpej 	    PCI_IO_START, PCI_IO_SIZE);
    145  1.32  thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
    146  1.32  thorpej 	    PCI_MEM_START, PCI_MEM_SIZE);
    147  1.32  thorpej 	pci_configure_bus(pc, pcires, 0, mips_cache_info.mci_dcache_align);
    148  1.32  thorpej 	pciconf_resource_fini(pcires);
    149  1.13  tsutsui #endif
    150  1.29    skrll 	memset(&pba, 0, sizeof(pba));
    151  1.29    skrll 	pba.pba_memt = &gt_memt;
    152  1.29    skrll 	pba.pba_iot = &gt_iot;
    153   1.1    soren 	pba.pba_dmat = &pci_bus_dma_tag;
    154   1.8     fvdl 	pba.pba_dmat64 = NULL;
    155   1.1    soren 	pba.pba_bus = 0;
    156   1.5  thorpej 	pba.pba_bridgetag = NULL;
    157  1.25   dyoung 	pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY |
    158   1.3    soren 		PCI_FLAGS_MRL_OKAY | /*PCI_FLAGS_MRM_OKAY|*/ PCI_FLAGS_MWI_OKAY;
    159  1.11  tsutsui 	pba.pba_pc = pc;
    160  1.34  thorpej 	config_found(self, &pba, gt_print, CFARGS_NONE);
    161   1.1    soren #endif
    162   1.1    soren }
    163   1.1    soren 
    164   1.4    soren static int
    165  1.15  tsutsui gt_print(void *aux, const char *pnp)
    166   1.1    soren {
    167  1.15  tsutsui 
    168   1.1    soren 	/* XXX */
    169   1.1    soren 	return 0;
    170   1.1    soren }
    171  1.10  tsutsui 
    172  1.10  tsutsui static void
    173  1.10  tsutsui gt_timer_init(struct gt_softc *sc)
    174  1.10  tsutsui {
    175  1.10  tsutsui 
    176  1.10  tsutsui 	/* stop timer0 */
    177  1.10  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL,
    178  1.10  tsutsui 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL) & ~ENTC0);
    179  1.17  tsutsui 	/* mask timer0 interrupt */
    180  1.17  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_MASTER_MASK,
    181  1.17  tsutsui 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_MASTER_MASK) & ~T0EXP);
    182  1.10  tsutsui }
    183  1.10  tsutsui 
    184  1.20  tsutsui #if 0	/* unused; now NetBSD/cobalt uses CPU INT5 for hardclock(9) */
    185  1.10  tsutsui #define TIMER0_INIT_VALUE 500000
    186  1.10  tsutsui 
    187  1.10  tsutsui static void
    188  1.10  tsutsui gt_timer0_init(void *cookie)
    189  1.10  tsutsui {
    190  1.10  tsutsui 	struct gt_softc *sc = cookie;
    191  1.10  tsutsui 
    192  1.10  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    193  1.10  tsutsui 	    GT_TIMER_COUNTER0, TIMER0_INIT_VALUE);
    194  1.10  tsutsui 	/* start timer0 */
    195  1.10  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL,
    196  1.10  tsutsui 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL) | ENTC0);
    197  1.17  tsutsui 	/* unmask timer0 interrupt */
    198  1.17  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_MASTER_MASK,
    199  1.17  tsutsui 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_MASTER_MASK) | T0EXP);
    200  1.10  tsutsui }
    201  1.10  tsutsui 
    202  1.10  tsutsui static long
    203  1.10  tsutsui gt_timer0_read(void *cookie)
    204  1.10  tsutsui {
    205  1.10  tsutsui 	struct gt_softc *sc = cookie;
    206  1.10  tsutsui 	uint32_t counter0;
    207  1.10  tsutsui 
    208  1.10  tsutsui 	counter0 = bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_COUNTER0);
    209  1.10  tsutsui 	counter0 = TIMER0_INIT_VALUE - counter0;
    210  1.10  tsutsui #if 0
    211  1.10  tsutsui 	counter /= 50;
    212  1.10  tsutsui #else
    213  1.10  tsutsui 	/*
    214  1.10  tsutsui 	 * From pmax/pmax/dec_3min.c:
    215  1.10  tsutsui 	 * 1/64 + 1/256 + 1/2048 = 41/2048 = 1/49.9512...
    216  1.10  tsutsui 	 */
    217  1.10  tsutsui 	counter0 = (counter0 >> 6) + (counter0 >> 8) + (counter0 >> 11);
    218  1.10  tsutsui #endif
    219  1.10  tsutsui 	return counter0;
    220  1.10  tsutsui }
    221  1.20  tsutsui #endif
    222