Home | History | Annotate | Line # | Download | only in dev
gt.c revision 1.35
      1  1.35  thorpej /*	$NetBSD: gt.c,v 1.35 2023/12/20 06:36:03 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.35  thorpej __KERNEL_RCSID(0, "$NetBSD: gt.c,v 1.35 2023/12/20 06:36:03 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/proc.h>
     43   1.1    soren #include <sys/select.h>
     44  1.27     matt #include <sys/syslog.h>
     45  1.27     matt #include <sys/systm.h>
     46   1.1    soren #include <sys/tty.h>
     47   1.1    soren #include <sys/uio.h>
     48   1.1    soren 
     49  1.10  tsutsui #include <machine/autoconf.h>
     50   1.1    soren 
     51  1.16  tsutsui #include <mips/cache.h>
     52  1.16  tsutsui 
     53   1.1    soren #include <dev/pci/pcivar.h>
     54  1.13  tsutsui #ifdef PCI_NETBSD_CONFIGURE
     55  1.13  tsutsui #include <dev/pci/pciconf.h>
     56  1.13  tsutsui #endif
     57  1.10  tsutsui 
     58  1.29    skrll #include <cobalt/dev/gtvar.h>
     59  1.10  tsutsui #include <cobalt/dev/gtreg.h>
     60  1.10  tsutsui 
     61   1.1    soren struct gt_softc {
     62  1.21  tsutsui 	device_t	sc_dev;
     63  1.10  tsutsui 
     64  1.10  tsutsui 	bus_space_tag_t sc_bst;
     65  1.10  tsutsui 	bus_space_handle_t sc_bsh;
     66  1.11  tsutsui 	struct cobalt_pci_chipset sc_pc;
     67   1.1    soren };
     68   1.1    soren 
     69  1.21  tsutsui static int	gt_match(device_t, cfdata_t, void *);
     70  1.21  tsutsui static void	gt_attach(device_t, device_t, void *);
     71   1.1    soren static int	gt_print(void *aux, const char *pnp);
     72   1.1    soren 
     73  1.10  tsutsui static void	gt_timer_init(struct gt_softc *sc);
     74  1.20  tsutsui #if 0 /* unused */
     75  1.10  tsutsui static void	gt_timer0_init(void *);
     76  1.10  tsutsui static long	gt_timer0_read(void *);
     77  1.20  tsutsui #endif
     78  1.10  tsutsui 
     79  1.29    skrll struct mips_bus_space gt_iot;
     80  1.29    skrll struct mips_bus_space gt_memt;
     81  1.29    skrll 
     82  1.21  tsutsui CFATTACH_DECL_NEW(gt, sizeof(struct gt_softc),
     83   1.7  thorpej     gt_match, gt_attach, NULL, NULL);
     84   1.1    soren 
     85  1.32  thorpej #define	PCI_IO_START	0x00001000
     86  1.32  thorpej #define	PCI_IO_END	0x01ffffff
     87  1.32  thorpej #define	PCI_IO_SIZE	((PCI_IO_END - PCI_IO_START) + 1)
     88  1.32  thorpej 
     89  1.32  thorpej #define	PCI_MEM_START	0x12000000
     90  1.32  thorpej #define	PCI_MEM_END	0x13ffffff
     91  1.32  thorpej #define	PCI_MEM_SIZE	((PCI_MEM_END - PCI_MEM_START) + 1)
     92  1.32  thorpej 
     93   1.4    soren static int
     94  1.21  tsutsui gt_match(device_t parent, cfdata_t cf, void *aux)
     95   1.1    soren {
     96  1.15  tsutsui 
     97   1.1    soren 	return 1;
     98   1.1    soren }
     99   1.1    soren 
    100  1.10  tsutsui #define GT_REG_REGION	0x1000
    101  1.10  tsutsui 
    102   1.4    soren static void
    103  1.21  tsutsui gt_attach(device_t parent, device_t self, void *aux)
    104   1.1    soren {
    105  1.21  tsutsui 	struct gt_softc *sc = device_private(self);
    106  1.10  tsutsui 	struct mainbus_attach_args *ma = aux;
    107  1.10  tsutsui #if NPCI > 0
    108  1.11  tsutsui 	pci_chipset_tag_t pc;
    109   1.1    soren 	struct pcibus_attach_args pba;
    110  1.10  tsutsui #endif
    111  1.10  tsutsui 
    112  1.21  tsutsui 	sc->sc_dev = self;
    113  1.10  tsutsui 	sc->sc_bst = ma->ma_iot;
    114  1.10  tsutsui 	if (bus_space_map(sc->sc_bst, ma->ma_addr, GT_REG_REGION,
    115  1.10  tsutsui 	    0, &sc->sc_bsh)) {
    116  1.21  tsutsui 		aprint_error(": unable to map GT64111 registers\n");
    117  1.10  tsutsui 		return;
    118  1.10  tsutsui 	}
    119   1.1    soren 
    120  1.21  tsutsui 	aprint_normal("\n");
    121   1.1    soren 
    122  1.10  tsutsui 	gt_timer_init(sc);
    123  1.10  tsutsui 
    124  1.10  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_PCI_COMMAND,
    125  1.10  tsutsui 	    (bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_PCI_COMMAND) &
    126  1.10  tsutsui 	    ~PCI_SYNCMODE) | PCI_PCLK_HIGH);
    127   1.2    soren 
    128  1.19  tsutsui 	(void)bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_PCI_TIMEOUT_RETRY);
    129  1.19  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_PCI_TIMEOUT_RETRY,
    130  1.19  tsutsui 	    0x00 << PCI_RETRYCTR_SHIFT | 0xff << PCI_TIMEOUT1_SHIFT | 0xff);
    131  1.19  tsutsui 
    132  1.29    skrll 	gt_bus_mem_init(&gt_memt, NULL);
    133  1.29    skrll 	gt_bus_io_init(&gt_iot, NULL);
    134  1.29    skrll 
    135   1.1    soren #if NPCI > 0
    136  1.11  tsutsui 	pc = &sc->sc_pc;
    137  1.11  tsutsui 	pc->pc_bst = sc->sc_bst;
    138  1.11  tsutsui 	pc->pc_bsh = sc->sc_bsh;
    139  1.11  tsutsui 
    140  1.13  tsutsui #ifdef PCI_NETBSD_CONFIGURE
    141  1.32  thorpej 	struct pciconf_resources *pcires = pciconf_resource_init();
    142  1.32  thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
    143  1.32  thorpej 	    PCI_IO_START, PCI_IO_SIZE);
    144  1.32  thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
    145  1.32  thorpej 	    PCI_MEM_START, PCI_MEM_SIZE);
    146  1.32  thorpej 	pci_configure_bus(pc, pcires, 0, mips_cache_info.mci_dcache_align);
    147  1.32  thorpej 	pciconf_resource_fini(pcires);
    148  1.13  tsutsui #endif
    149  1.29    skrll 	memset(&pba, 0, sizeof(pba));
    150  1.29    skrll 	pba.pba_memt = &gt_memt;
    151  1.29    skrll 	pba.pba_iot = &gt_iot;
    152   1.1    soren 	pba.pba_dmat = &pci_bus_dma_tag;
    153   1.8     fvdl 	pba.pba_dmat64 = NULL;
    154   1.1    soren 	pba.pba_bus = 0;
    155   1.5  thorpej 	pba.pba_bridgetag = NULL;
    156  1.25   dyoung 	pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY |
    157   1.3    soren 		PCI_FLAGS_MRL_OKAY | /*PCI_FLAGS_MRM_OKAY|*/ PCI_FLAGS_MWI_OKAY;
    158  1.11  tsutsui 	pba.pba_pc = pc;
    159  1.34  thorpej 	config_found(self, &pba, gt_print, CFARGS_NONE);
    160   1.1    soren #endif
    161   1.1    soren }
    162   1.1    soren 
    163   1.4    soren static int
    164  1.15  tsutsui gt_print(void *aux, const char *pnp)
    165   1.1    soren {
    166  1.15  tsutsui 
    167   1.1    soren 	/* XXX */
    168   1.1    soren 	return 0;
    169   1.1    soren }
    170  1.10  tsutsui 
    171  1.10  tsutsui static void
    172  1.10  tsutsui gt_timer_init(struct gt_softc *sc)
    173  1.10  tsutsui {
    174  1.10  tsutsui 
    175  1.10  tsutsui 	/* stop timer0 */
    176  1.10  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL,
    177  1.10  tsutsui 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL) & ~ENTC0);
    178  1.17  tsutsui 	/* mask timer0 interrupt */
    179  1.17  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_MASTER_MASK,
    180  1.17  tsutsui 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_MASTER_MASK) & ~T0EXP);
    181  1.10  tsutsui }
    182  1.10  tsutsui 
    183  1.20  tsutsui #if 0	/* unused; now NetBSD/cobalt uses CPU INT5 for hardclock(9) */
    184  1.10  tsutsui #define TIMER0_INIT_VALUE 500000
    185  1.10  tsutsui 
    186  1.10  tsutsui static void
    187  1.10  tsutsui gt_timer0_init(void *cookie)
    188  1.10  tsutsui {
    189  1.10  tsutsui 	struct gt_softc *sc = cookie;
    190  1.10  tsutsui 
    191  1.10  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    192  1.10  tsutsui 	    GT_TIMER_COUNTER0, TIMER0_INIT_VALUE);
    193  1.10  tsutsui 	/* start timer0 */
    194  1.10  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL,
    195  1.10  tsutsui 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL) | ENTC0);
    196  1.17  tsutsui 	/* unmask timer0 interrupt */
    197  1.17  tsutsui 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_MASTER_MASK,
    198  1.17  tsutsui 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_MASTER_MASK) | T0EXP);
    199  1.10  tsutsui }
    200  1.10  tsutsui 
    201  1.10  tsutsui static long
    202  1.10  tsutsui gt_timer0_read(void *cookie)
    203  1.10  tsutsui {
    204  1.10  tsutsui 	struct gt_softc *sc = cookie;
    205  1.10  tsutsui 	uint32_t counter0;
    206  1.10  tsutsui 
    207  1.10  tsutsui 	counter0 = bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_COUNTER0);
    208  1.10  tsutsui 	counter0 = TIMER0_INIT_VALUE - counter0;
    209  1.10  tsutsui #if 0
    210  1.10  tsutsui 	counter /= 50;
    211  1.10  tsutsui #else
    212  1.10  tsutsui 	/*
    213  1.10  tsutsui 	 * From pmax/pmax/dec_3min.c:
    214  1.10  tsutsui 	 * 1/64 + 1/256 + 1/2048 = 41/2048 = 1/49.9512...
    215  1.10  tsutsui 	 */
    216  1.10  tsutsui 	counter0 = (counter0 >> 6) + (counter0 >> 8) + (counter0 >> 11);
    217  1.10  tsutsui #endif
    218  1.10  tsutsui 	return counter0;
    219  1.10  tsutsui }
    220  1.20  tsutsui #endif
    221