Home | History | Annotate | Line # | Download | only in ic
cpc700.c revision 1.22
      1  1.22   thorpej /*	$NetBSD: cpc700.c,v 1.22 2021/04/24 23:36:55 thorpej Exp $	*/
      2   1.1  augustss 
      3   1.1  augustss /*
      4   1.1  augustss  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5   1.1  augustss  * All rights reserved.
      6   1.1  augustss  *
      7   1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at Sandburst Corp.
      9   1.1  augustss  *
     10   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11   1.1  augustss  * modification, are permitted provided that the following conditions
     12   1.1  augustss  * are met:
     13   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18   1.1  augustss  *
     19   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  augustss  */
     31   1.1  augustss 
     32   1.1  augustss /*
     33   1.1  augustss  * The IBM CPC700 is a bridge chip for the PowerPC.  It contains
     34   1.1  augustss  *  - CPU interface
     35   1.1  augustss  *  - DRAM controller
     36   1.1  augustss  *  - PCI bus master & slave controller
     37   1.1  augustss  *  - interrupt controller
     38   1.1  augustss  *  - timer
     39   1.1  augustss  *  - two UARTs
     40   1.1  augustss  *  - two IIC ports
     41   1.1  augustss  *
     42   1.1  augustss  *  This driver handles the overall device and enumeration of the
     43   1.1  augustss  *  supported subdevices.  NetBSD knows how to handle:
     44   1.1  augustss  *  - PCI master
     45   1.1  augustss  *  - interrupt controller
     46   1.1  augustss  *  - UARTs
     47   1.1  augustss  *  Skeleton drivers are provided for the timer and IIC.
     48   1.1  augustss  *
     49   1.1  augustss  * XXX This driver assumes that there is only one instance of it.
     50   1.1  augustss  */
     51   1.6     lukem 
     52   1.6     lukem #include <sys/cdefs.h>
     53  1.22   thorpej __KERNEL_RCSID(0, "$NetBSD: cpc700.c,v 1.22 2021/04/24 23:36:55 thorpej Exp $");
     54   1.1  augustss 
     55   1.1  augustss #include "pci.h"
     56   1.1  augustss #include "opt_pci.h"
     57   1.1  augustss 
     58   1.1  augustss #include <sys/param.h>
     59   1.1  augustss #include <sys/device.h>
     60   1.1  augustss #include <sys/malloc.h>
     61   1.1  augustss #include <sys/systm.h>
     62   1.1  augustss 
     63  1.12        ad #include <sys/bus.h>
     64   1.1  augustss #include "locators.h"
     65   1.1  augustss 
     66   1.1  augustss #include <dev/pci/pcivar.h>
     67   1.1  augustss #include <dev/pci/pcireg.h>
     68   1.1  augustss #include <dev/pci/pciconf.h>
     69   1.1  augustss 
     70   1.1  augustss #include <dev/ic/cpc700reg.h>
     71   1.1  augustss #include <dev/ic/cpc700var.h>
     72   1.1  augustss #include <dev/ic/cpc700uic.h>
     73   1.1  augustss 
     74   1.1  augustss union attach_args {
     75   1.1  augustss 	struct pcibus_attach_args pba;
     76   1.1  augustss 	struct cpcbus_attach_args cba;
     77   1.1  augustss };
     78   1.1  augustss 
     79   1.1  augustss 
     80   1.1  augustss void
     81  1.16    cegger cpc_attach(device_t self, pci_chipset_tag_t pc, bus_space_tag_t mem,
     82   1.1  augustss 	   bus_space_tag_t pciio, bus_dma_tag_t tag, int attachpci,
     83   1.1  augustss 	   uint freq);
     84   1.1  augustss 
     85   1.1  augustss static bus_space_tag_t the_cpc_tag;
     86   1.1  augustss static bus_space_handle_t the_cpc_handle;
     87   1.1  augustss #define INL(a) bus_space_read_stream_4(the_cpc_tag, the_cpc_handle, (a))
     88   1.1  augustss #define OUTL(a, d) bus_space_write_stream_4(the_cpc_tag, the_cpc_handle, (a), d)
     89   1.1  augustss 
     90  1.21   thorpej #define	PCI_IO_START	CPC_PCI_IO_START
     91  1.21   thorpej #define	PCI_IO_END	CPC_PCI_IO_END
     92  1.21   thorpej #define	PCI_IO_SIZE	((PCI_IO_END - PCI_IO_START) + 1)
     93  1.21   thorpej 
     94  1.21   thorpej #define	PCI_MEM_START	CPC_PCI_MEM_BASE
     95  1.21   thorpej #define	PCI_MEM_END	CPC_PCI_MEM_END
     96  1.21   thorpej #define	PCI_MEM_SIZE	((PCI_MEM_END - PCI_MEM_START) + 1)
     97  1.21   thorpej 
     98   1.1  augustss static int
     99   1.1  augustss cpc_print(void *aux, const char *pnp)
    100   1.1  augustss {
    101   1.1  augustss 	struct cpcbus_attach_args *caa = aux;
    102   1.1  augustss 
    103   1.1  augustss 	if (pnp)
    104   1.5   thorpej 		aprint_normal("%s at %s", caa->cpca_name, pnp);
    105   1.1  augustss 
    106   1.5   thorpej 	aprint_normal(" addr 0x%08x", caa->cpca_addr);
    107   1.1  augustss 	if (caa->cpca_irq != CPCBUSCF_IRQ_DEFAULT)
    108   1.5   thorpej 		aprint_normal(" irq %d", caa->cpca_irq);
    109   1.1  augustss 
    110   1.1  augustss 	return (UNCONF);
    111   1.1  augustss }
    112   1.1  augustss 
    113   1.1  augustss static int
    114  1.16    cegger cpc_submatch(device_t parent, cfdata_t cf,
    115  1.10  drochner 	     const int *ldesc, void *aux)
    116   1.1  augustss {
    117   1.1  augustss 	struct cpcbus_attach_args *caa = aux;
    118   1.1  augustss 
    119   1.1  augustss 	if (cf->cf_loc[CPCBUSCF_ADDR] != caa->cpca_addr)
    120   1.1  augustss 		return (0);
    121   1.1  augustss 
    122   1.4   thorpej 	return (config_match(parent, cf, aux));
    123   1.1  augustss }
    124   1.1  augustss 
    125   1.1  augustss /*
    126   1.1  augustss  * Attach the cpc.
    127   1.1  augustss  */
    128   1.1  augustss void
    129  1.16    cegger cpc_attach(device_t self, pci_chipset_tag_t pc, bus_space_tag_t mem,
    130   1.1  augustss 	   bus_space_tag_t pciio, bus_dma_tag_t dma, int attachpci,
    131   1.1  augustss 	   uint freq)
    132   1.1  augustss {
    133   1.1  augustss 	union attach_args aa;
    134   1.1  augustss 	int i;
    135   1.9     perry 	pcitag_t tag;
    136   1.1  augustss 	pcireg_t erren;
    137   1.7  augustss 	pcireg_t v;
    138   1.1  augustss 	static struct {
    139   1.1  augustss 		const char *name;
    140   1.1  augustss 		bus_addr_t addr;
    141   1.1  augustss 		int irq;
    142   1.1  augustss 	} devs[] = {
    143   1.2  augustss 		{ "com",    CPC_COM0, CPC_IB_UART_0 },
    144   1.2  augustss 		{ "com",    CPC_COM1, CPC_IB_UART_1 },
    145   1.2  augustss 		{ "cpctim", CPC_TIMER, CPCBUSCF_IRQ_DEFAULT },
    146   1.2  augustss 		{ "cpciic", CPC_IIC0, CPC_IB_IIC_0 },
    147   1.2  augustss 		{ "cpciic", CPC_IIC1, CPC_IB_IIC_1 },
    148   1.1  augustss 		{ NULL, 0 }
    149   1.1  augustss 	};
    150   1.1  augustss #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
    151   1.1  augustss #ifdef PCI_CONFIGURE_VERBOSE
    152   1.1  augustss 	extern int pci_conf_debug;
    153   1.1  augustss 
    154   1.1  augustss 	pci_conf_debug = 1;
    155   1.1  augustss #endif
    156   1.1  augustss #endif
    157   1.1  augustss 
    158   1.1  augustss 	printf(": IBM CPC700\n");
    159   1.1  augustss 
    160   1.1  augustss 	the_cpc_tag = mem;
    161   1.1  augustss 	if (bus_space_map(mem, CPC_UIC_BASE, CPC_UIC_SIZE, 0,
    162   1.1  augustss 			  &the_cpc_handle)) {
    163  1.13    cegger 		aprint_error_dev(self, "can't map i/o space\n");
    164   1.1  augustss 		return;
    165   1.1  augustss 	}
    166   1.1  augustss 
    167   1.1  augustss 	aa.cba.cpca_tag = mem;
    168   1.1  augustss 	aa.cba.cpca_freq = freq;
    169   1.1  augustss 	for (i = 0; devs[i].name; i++) {
    170   1.1  augustss 		aa.cba.cpca_name = devs[i].name;
    171   1.1  augustss 		aa.cba.cpca_addr = devs[i].addr;
    172   1.1  augustss 		aa.cba.cpca_irq = devs[i].irq;
    173  1.22   thorpej 		config_found(self, &aa.cba, cpc_print,
    174  1.22   thorpej 		    CFARG_SUBMATCH, cpc_submatch,
    175  1.22   thorpej 		    CFARG_IATTR, "cpcbus",
    176  1.22   thorpej 		    CFARG_EOL);
    177   1.1  augustss 	}
    178   1.1  augustss 
    179   1.1  augustss 	tag = pci_make_tag(pc, 0, 0, 0);
    180   1.1  augustss 
    181   1.1  augustss 	aa.pba.pba_iot = pciio;
    182   1.1  augustss 	aa.pba.pba_memt = mem;
    183   1.1  augustss 	aa.pba.pba_dmat = dma;
    184  1.17      matt 	aa.pba.pba_pc = pc;
    185  1.18    dyoung 	aa.pba.pba_flags = PCI_FLAGS_MEM_OKAY | PCI_FLAGS_IO_OKAY;
    186   1.1  augustss 	aa.pba.pba_bus = 0;
    187   1.1  augustss 
    188   1.1  augustss 	/* Save PCI error condition reg. */
    189   1.1  augustss 	erren = pci_conf_read(pc, tag, CPC_PCI_BRDGERR);
    190   1.7  augustss 	/* Don't generate errors during probe. */
    191   1.1  augustss 	pci_conf_write(pc, tag, CPC_PCI_BRDGERR, 0);
    192   1.7  augustss 
    193   1.7  augustss 	/* Program MITL */
    194   1.7  augustss 	v = pci_conf_read(pc, tag, CPC_BRIDGE_OPTIONS2);
    195   1.7  augustss 	v &= ~(CPC_BRIDGE_O2_ILAT_MASK | CPC_BRIDGE_O2_SLAT_MASK);
    196   1.7  augustss 	v |= (CPC_BRIDGE_O2_ILAT_PRIM_ASYNC << CPC_BRIDGE_O2_ILAT_SHIFT) |
    197   1.7  augustss 	  (CPC_BRIDGE_O2_2LAT_PRIM_ASYNC << CPC_BRIDGE_O2_SLAT_SHIFT);
    198   1.7  augustss 	pci_conf_write(pc, tag, CPC_BRIDGE_OPTIONS2, v);
    199   1.1  augustss 
    200   1.1  augustss #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
    201  1.21   thorpej 	struct pciconf_resources *pcires = pciconf_resource_init();
    202   1.1  augustss 
    203  1.21   thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
    204  1.21   thorpej 	    PCI_IO_START, PCI_IO_SIZE);
    205  1.21   thorpej 	pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
    206  1.21   thorpej 	    PCI_MEM_START, PCI_MEM_SIZE);
    207   1.1  augustss 
    208  1.21   thorpej 	pci_configure_bus(0, pcires, 0, 32);
    209   1.1  augustss #endif
    210   1.1  augustss 
    211  1.22   thorpej 	config_found(self, &aa.pba, pcibusprint,
    212  1.22   thorpej 	    CFARG_IATTR, "pcibus",
    213  1.22   thorpej 	    CFARG_EOL);
    214   1.1  augustss 
    215   1.1  augustss 	/* Restore error triggers, and clear errors */
    216   1.1  augustss 	pci_conf_write(pc, tag, CPC_PCI_BRDGERR, erren | CPC_PCI_CLEARERR);
    217   1.1  augustss }
    218   1.1  augustss 
    219   1.1  augustss /***************************************************************************/
    220   1.1  augustss 
    221   1.1  augustss /*
    222   1.1  augustss  * Interrupt controller.
    223   1.1  augustss  */
    224   1.1  augustss 
    225   1.1  augustss void
    226   1.1  augustss cpc700_init_intr(bus_space_tag_t bt, bus_space_handle_t bh,
    227   1.1  augustss 		 u_int32_t active, u_int32_t level)
    228   1.1  augustss {
    229   1.1  augustss 	/* XXX */
    230   1.1  augustss 	the_cpc_tag = bt;
    231   1.1  augustss 	the_cpc_handle = bh;
    232   1.9     perry 	/*
    233   1.1  augustss 	 * See CPC700 manual for information about what
    234   1.1  augustss 	 * interrupts have which properties.
    235   1.1  augustss 	 */
    236   1.1  augustss 	OUTL(CPC_UIC_SR, 0xffffffff);    /* clear all intrs */
    237   1.1  augustss 	OUTL(CPC_UIC_ER, 0x00000000);    /* disable all intrs */
    238   1.1  augustss 	OUTL(CPC_UIC_CR, 0xffffffff);    /* gen INT not MCP */
    239   1.1  augustss 	OUTL(CPC_UIC_PR, 0xffff8000 | active);    /* 0 = active low */
    240   1.1  augustss 	OUTL(CPC_UIC_TR, 0xc0000000 | level);    /* 0 = level intr */
    241   1.1  augustss 	OUTL(CPC_UIC_VR, CPC_UIC_CVR_PRI); /* intr 0 is highest */
    242   1.1  augustss }
    243   1.1  augustss 
    244   1.1  augustss int
    245   1.1  augustss cpc700_read_irq(void)
    246   1.1  augustss {
    247   1.1  augustss 	int irq;
    248   1.1  augustss 	u_int32_t irqs;
    249   1.1  augustss 
    250   1.1  augustss 	irqs = INL(CPC_UIC_MSR);
    251   1.1  augustss 	for (irq = 0; irq < ICU_LEN; irq++) {
    252   1.1  augustss 		if (irqs & CPC_INTR_MASK(irq))
    253   1.1  augustss 			return (irq);
    254   1.1  augustss 	}
    255   1.1  augustss 	return (-1);
    256   1.1  augustss }
    257   1.1  augustss 
    258   1.1  augustss void
    259   1.1  augustss cpc700_eoi(int irq)
    260   1.1  augustss {
    261   1.1  augustss 	OUTL(CPC_UIC_SR, CPC_INTR_MASK(irq));
    262   1.1  augustss }
    263   1.1  augustss 
    264   1.1  augustss void
    265   1.1  augustss cpc700_disable_irq(int irq)
    266   1.1  augustss {
    267   1.1  augustss 	u_int32_t reg;
    268   1.1  augustss 
    269   1.1  augustss 	reg = INL(CPC_UIC_ER);
    270   1.1  augustss 	reg &= ~CPC_INTR_MASK(irq);
    271   1.1  augustss 	OUTL(CPC_UIC_ER, reg);
    272   1.1  augustss }
    273   1.1  augustss 
    274   1.1  augustss void
    275   1.1  augustss cpc700_enable_irq(int irq)
    276   1.1  augustss {
    277   1.1  augustss 	u_int32_t reg;
    278   1.1  augustss 
    279   1.1  augustss 	reg = INL(CPC_UIC_ER);
    280   1.1  augustss 	reg |= CPC_INTR_MASK(irq);
    281   1.1  augustss 	OUTL(CPC_UIC_ER, reg);
    282   1.1  augustss }
    283