Home | History | Annotate | Line # | Download | only in pci
      1  1.58  thorpej /* $NetBSD: lca.c,v 1.58 2023/12/04 00:32:10 thorpej Exp $ */
      2  1.35  thorpej 
      3  1.35  thorpej /*-
      4  1.35  thorpej  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.35  thorpej  * All rights reserved.
      6  1.35  thorpej  *
      7  1.35  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.35  thorpej  * by Jason R. Thorpe.
      9  1.35  thorpej  *
     10  1.35  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.35  thorpej  * modification, are permitted provided that the following conditions
     12  1.35  thorpej  * are met:
     13  1.35  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.35  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.35  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.35  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.35  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.35  thorpej  *
     19  1.35  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.35  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.35  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.35  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.35  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.35  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.35  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.35  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.35  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.35  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.35  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30  1.35  thorpej  */
     31   1.1      cgd 
     32   1.1      cgd /*
     33   1.4      cgd  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
     34   1.1      cgd  * All rights reserved.
     35   1.1      cgd  *
     36   1.3      cgd  * Authors: Jeffrey Hsu and Chris G. Demetriou
     37  1.51     matt  *
     38   1.1      cgd  * Permission to use, copy, modify and distribute this software and
     39   1.1      cgd  * its documentation is hereby granted, provided that both the copyright
     40   1.1      cgd  * notice and this permission notice appear in all copies of the
     41   1.1      cgd  * software, derivative works or modified versions, and any portions
     42   1.1      cgd  * thereof, and that both notices appear in supporting documentation.
     43  1.51     matt  *
     44  1.51     matt  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     45  1.51     matt  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     46   1.1      cgd  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     47  1.51     matt  *
     48   1.1      cgd  * Carnegie Mellon requests users of this software to return to
     49   1.1      cgd  *
     50   1.1      cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     51   1.1      cgd  *  School of Computer Science
     52   1.1      cgd  *  Carnegie Mellon University
     53   1.1      cgd  *  Pittsburgh PA 15213-3890
     54   1.1      cgd  *
     55   1.1      cgd  * any improvements or extensions that they make and grant Carnegie the
     56   1.1      cgd  * rights to redistribute these changes.
     57   1.1      cgd  */
     58  1.17      cgd 
     59  1.18      cgd #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     60  1.18      cgd 
     61  1.58  thorpej __KERNEL_RCSID(0, "$NetBSD: lca.c,v 1.58 2023/12/04 00:32:10 thorpej Exp $");
     62   1.1      cgd 
     63   1.1      cgd #include <sys/param.h>
     64   1.1      cgd #include <sys/systm.h>
     65   1.1      cgd #include <sys/kernel.h>
     66   1.1      cgd #include <sys/device.h>
     67  1.36      mrg 
     68  1.56  thorpej #include <uvm/uvm_extern.h>
     69  1.56  thorpej 
     70   1.1      cgd #include <machine/autoconf.h>
     71   1.1      cgd #include <machine/rpb.h>
     72  1.35  thorpej #include <machine/sysarch.h>
     73   1.1      cgd 
     74   1.1      cgd #include <dev/isa/isareg.h>
     75   1.1      cgd #include <dev/isa/isavar.h>
     76   1.1      cgd 
     77   1.1      cgd #include <dev/pci/pcireg.h>
     78   1.1      cgd #include <dev/pci/pcivar.h>
     79   1.1      cgd #include <alpha/pci/lcareg.h>
     80   1.1      cgd #include <alpha/pci/lcavar.h>
     81   1.1      cgd 
     82  1.53  thorpej static int	lcamatch(device_t, cfdata_t, void *);
     83  1.53  thorpej static void	lcaattach(device_t, device_t, void *);
     84   1.1      cgd 
     85  1.50     matt CFATTACH_DECL_NEW(lca, sizeof(struct lca_softc),
     86  1.39  thorpej     lcamatch, lcaattach, NULL, NULL);
     87   1.2  thorpej 
     88  1.26  thorpej extern struct cfdriver lca_cd;
     89   1.1      cgd 
     90  1.53  thorpej static int	lca_bus_get_window(int, int,
     91  1.53  thorpej 		    struct alpha_bus_space_translation *);
     92  1.35  thorpej 
     93   1.1      cgd /* There can be only one. */
     94   1.1      cgd struct lca_config lca_configuration;
     95  1.53  thorpej static int lcafound;
     96   1.1      cgd 
     97  1.56  thorpej static const u_int lca_bcache_sizes[__SHIFTOUT(MEMC_CAR_BCS, MEMC_CAR_BCS)] = {
     98  1.56  thorpej [BCS_64K]	=	64 * 1024,
     99  1.56  thorpej [BCS_128K]	=	128 * 1024,
    100  1.56  thorpej [BCS_256K]	=	256 * 1024,
    101  1.56  thorpej [BCS_512K]	=	512 * 1024,
    102  1.56  thorpej [BCS_1M]	=	1024 * 1024,
    103  1.56  thorpej [BCS_2M]	=	2048 * 1024,
    104  1.56  thorpej /* Other values are reserved and we treat them as 0. */
    105  1.56  thorpej };
    106  1.56  thorpej 
    107  1.53  thorpej static int
    108  1.50     matt lcamatch(device_t parent, cfdata_t match, void *aux)
    109   1.1      cgd {
    110  1.28  thorpej 	struct mainbus_attach_args *ma = aux;
    111   1.1      cgd 
    112   1.1      cgd 	/* Make sure that we're looking for a LCA. */
    113  1.28  thorpej 	if (strcmp(ma->ma_name, lca_cd.cd_name) != 0)
    114   1.1      cgd 		return (0);
    115   1.1      cgd 
    116   1.1      cgd 	if (lcafound)
    117   1.1      cgd 		return (0);
    118   1.1      cgd 
    119   1.1      cgd 	return (1);
    120   1.1      cgd }
    121   1.1      cgd 
    122   1.1      cgd /*
    123  1.56  thorpej  * Probe the memory controller's Bcache configuration.
    124  1.56  thorpej  */
    125  1.56  thorpej void
    126  1.56  thorpej lca_probe_bcache(void)
    127  1.56  thorpej {
    128  1.56  thorpej 	const uint64_t car = REGVAL64(LCA_MEMC_CAR);
    129  1.56  thorpej 
    130  1.56  thorpej 	if (lca_configuration.lc_bcache_size != 0) {
    131  1.56  thorpej 		/* Already done. */
    132  1.56  thorpej 		return;
    133  1.56  thorpej 	}
    134  1.56  thorpej 
    135  1.56  thorpej 	if (car & MEMC_CAR_BCE) {
    136  1.56  thorpej 		lca_configuration.lc_bcache_size =
    137  1.56  thorpej 		    lca_bcache_sizes[__SHIFTOUT(car, MEMC_CAR_BCS)];
    138  1.56  thorpej 	} else {
    139  1.56  thorpej 		lca_configuration.lc_bcache_size = 0;
    140  1.56  thorpej 	}
    141  1.56  thorpej 
    142  1.56  thorpej 	if (lca_configuration.lc_bcache_size) {
    143  1.56  thorpej 		uvmexp.ncolors = atop(lca_configuration.lc_bcache_size);
    144  1.56  thorpej 	}
    145  1.56  thorpej }
    146  1.56  thorpej 
    147  1.56  thorpej /*
    148   1.1      cgd  * Set up the chipset's function pointers.
    149   1.1      cgd  */
    150   1.1      cgd void
    151  1.58  thorpej lca_init(struct lca_config *lcp)
    152   1.1      cgd {
    153   1.1      cgd 
    154   1.1      cgd 	/*
    155  1.13      cgd 	 * The LCA HAE register is WRITE-ONLY, so we can't tell where
    156  1.13      cgd 	 * the second sparse window is actually mapped.  Therefore,
    157  1.13      cgd 	 * we have to guess where it is.  This seems to be the normal
    158  1.13      cgd 	 * address.
    159  1.13      cgd 	 */
    160  1.13      cgd 	lcp->lc_s_mem_w2_masked_base = 0x80000000;
    161  1.13      cgd 
    162  1.13      cgd 	if (!lcp->lc_initted) {
    163  1.13      cgd 		/* don't do these twice since they set up extents */
    164  1.21  thorpej 		lca_bus_io_init(&lcp->lc_iot, lcp);
    165  1.21  thorpej 		lca_bus_mem_init(&lcp->lc_memt, lcp);
    166  1.35  thorpej 
    167  1.35  thorpej 		/*
    168  1.35  thorpej 		 * We have 1 I/O window and 3 MEM windows.
    169  1.35  thorpej 		 */
    170  1.35  thorpej 		alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_IO] = 1;
    171  1.35  thorpej 		alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_MEM] = 3;
    172  1.35  thorpej 		alpha_bus_get_window = lca_bus_get_window;
    173  1.13      cgd 	}
    174  1.13      cgd 
    175   1.3      cgd 	lca_pci_init(&lcp->lc_pc, lcp);
    176  1.35  thorpej 	alpha_pci_chipset = &lcp->lc_pc;
    177   1.1      cgd 
    178   1.5      cgd 	/*
    179   1.5      cgd 	 * Refer to ``DECchip 21066 and DECchip 21068 Alpha AXP Microprocessors
    180   1.5      cgd 	 * Hardware Reference Manual''.
    181   1.5      cgd 	 * ...
    182   1.5      cgd 	 */
    183   1.5      cgd 
    184   1.5      cgd 	/*
    185   1.5      cgd 	 * According to section 6.4.1, all bits of the IOC_HAE register are
    186   1.5      cgd 	 * undefined after reset.  Bits <31:27> are write-only.  However, we
    187   1.5      cgd 	 * cannot blindly set it to zero.  The serial ROM code that initializes
    188   1.5      cgd 	 * the PCI devices' address spaces, allocates sparse memory blocks in
    189   1.5      cgd 	 * the range that must use the IOC_HAE register for address translation,
    190   1.5      cgd 	 * and sets this register accordingly (see section 6.4.14).
    191   1.5      cgd 	 *
    192   1.5      cgd 	 *	IOC_HAE left AS IS.
    193   1.5      cgd 	 */
    194   1.1      cgd 
    195   1.5      cgd 	/* According to section 6.4.2, all bits of the IOC_CONF register are
    196   1.5      cgd 	 * undefined after reset.  Bits <1:0> are write-only.  Set them to
    197   1.5      cgd 	 * 0x00 for PCI Type 0 configuration access.
    198   1.5      cgd 	 *
    199   1.5      cgd 	 *	IOC_CONF set to ZERO.
    200   1.5      cgd 	 */
    201  1.20  thorpej 	REGVAL64(LCA_IOC_CONF) = 0;
    202   1.1      cgd 
    203  1.13      cgd 	lcp->lc_initted = 1;
    204   1.1      cgd }
    205   1.1      cgd 
    206  1.53  thorpej static void
    207  1.50     matt lcaattach(device_t parent, device_t self, void *aux)
    208   1.1      cgd {
    209  1.50     matt 	struct lca_softc *sc = device_private(self);
    210   1.1      cgd 	struct lca_config *lcp;
    211   1.3      cgd 	struct pcibus_attach_args pba;
    212   1.1      cgd 
    213   1.1      cgd 	/* note that we've attached the chipset; can't have 2 LCAs. */
    214   1.1      cgd 	lcafound = 1;
    215  1.50     matt 	sc->sc_dev = self;
    216   1.1      cgd 
    217   1.1      cgd 	/*
    218   1.1      cgd 	 * set up the chipset's info; done once at console init time
    219  1.20  thorpej 	 * (maybe), but we must do it twice to take care of things
    220  1.20  thorpej 	 * that need to use memory allocation.
    221   1.1      cgd 	 */
    222   1.1      cgd 	lcp = sc->sc_lcp = &lca_configuration;
    223  1.58  thorpej 	lca_init(lcp);
    224   1.1      cgd 
    225   1.1      cgd 	/* XXX print chipset information */
    226  1.50     matt 	aprint_normal("\n");
    227  1.56  thorpej 	if (lcp->lc_bcache_size != 0) {
    228  1.56  thorpej 		char buf[sizeof("256 KB")];
    229  1.56  thorpej 		if (format_bytes(buf, sizeof(buf), lcp->lc_bcache_size) > 0) {
    230  1.56  thorpej 			aprint_normal_dev(self, "%s Bcache detected\n", buf);
    231  1.56  thorpej 		}
    232  1.56  thorpej 	}
    233  1.29  thorpej 
    234  1.29  thorpej 	lca_dma_init(lcp);
    235   1.1      cgd 
    236  1.54  thorpej 	alpha_pci_intr_init(lcp, &lcp->lc_iot, &lcp->lc_memt, &lcp->lc_pc);
    237   1.1      cgd 
    238  1.22  thorpej 	pba.pba_iot = &lcp->lc_iot;
    239  1.22  thorpej 	pba.pba_memt = &lcp->lc_memt;
    240  1.27  thorpej 	pba.pba_dmat =
    241  1.27  thorpej 	    alphabus_dma_get_tag(&lcp->lc_dmat_direct, ALPHA_BUS_PCI);
    242  1.41     fvdl 	pba.pba_dmat64 = NULL;
    243   1.3      cgd 	pba.pba_pc = &lcp->lc_pc;
    244   1.3      cgd 	pba.pba_bus = 0;
    245  1.37  thorpej 	pba.pba_bridgetag = NULL;
    246  1.49   dyoung 	pba.pba_flags = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY |
    247  1.34  thorpej 	    PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
    248  1.57  thorpej 	config_found(self, &pba, pcibusprint, CFARGS_NONE);
    249  1.35  thorpej }
    250  1.35  thorpej 
    251  1.53  thorpej static int
    252  1.53  thorpej lca_bus_get_window(int type, int window,
    253  1.53  thorpej     struct alpha_bus_space_translation *abst)
    254  1.35  thorpej {
    255  1.35  thorpej 	struct lca_config *lcp = &lca_configuration;
    256  1.35  thorpej 	bus_space_tag_t st;
    257  1.35  thorpej 
    258  1.35  thorpej 	switch (type) {
    259  1.35  thorpej 	case ALPHA_BUS_TYPE_PCI_IO:
    260  1.35  thorpej 		st = &lcp->lc_iot;
    261  1.35  thorpej 		break;
    262  1.35  thorpej 
    263  1.35  thorpej 	case ALPHA_BUS_TYPE_PCI_MEM:
    264  1.35  thorpej 		st = &lcp->lc_memt;
    265  1.35  thorpej 		break;
    266  1.35  thorpej 
    267  1.35  thorpej 	default:
    268  1.35  thorpej 		panic("lca_bus_get_window");
    269  1.35  thorpej 	}
    270  1.35  thorpej 
    271  1.35  thorpej 	return (alpha_bus_space_get_window(st, window, abst));
    272   1.1      cgd }
    273