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