cpc700.c revision 1.24 1 1.24 thorpej /* $NetBSD: cpc700.c,v 1.24 2022/09/25 18:43:32 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.24 thorpej __KERNEL_RCSID(0, "$NetBSD: cpc700.c,v 1.24 2022/09/25 18:43:32 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/systm.h>
61 1.1 augustss
62 1.12 ad #include <sys/bus.h>
63 1.1 augustss #include "locators.h"
64 1.1 augustss
65 1.1 augustss #include <dev/pci/pcivar.h>
66 1.1 augustss #include <dev/pci/pcireg.h>
67 1.1 augustss #include <dev/pci/pciconf.h>
68 1.1 augustss
69 1.1 augustss #include <dev/ic/cpc700reg.h>
70 1.1 augustss #include <dev/ic/cpc700var.h>
71 1.1 augustss #include <dev/ic/cpc700uic.h>
72 1.1 augustss
73 1.1 augustss union attach_args {
74 1.1 augustss struct pcibus_attach_args pba;
75 1.1 augustss struct cpcbus_attach_args cba;
76 1.1 augustss };
77 1.1 augustss
78 1.1 augustss
79 1.1 augustss void
80 1.16 cegger cpc_attach(device_t self, pci_chipset_tag_t pc, bus_space_tag_t mem,
81 1.1 augustss bus_space_tag_t pciio, bus_dma_tag_t tag, int attachpci,
82 1.1 augustss uint freq);
83 1.1 augustss
84 1.1 augustss static bus_space_tag_t the_cpc_tag;
85 1.1 augustss static bus_space_handle_t the_cpc_handle;
86 1.1 augustss #define INL(a) bus_space_read_stream_4(the_cpc_tag, the_cpc_handle, (a))
87 1.1 augustss #define OUTL(a, d) bus_space_write_stream_4(the_cpc_tag, the_cpc_handle, (a), d)
88 1.1 augustss
89 1.21 thorpej #define PCI_IO_START CPC_PCI_IO_START
90 1.21 thorpej #define PCI_IO_END CPC_PCI_IO_END
91 1.21 thorpej #define PCI_IO_SIZE ((PCI_IO_END - PCI_IO_START) + 1)
92 1.21 thorpej
93 1.21 thorpej #define PCI_MEM_START CPC_PCI_MEM_BASE
94 1.21 thorpej #define PCI_MEM_END CPC_PCI_MEM_END
95 1.21 thorpej #define PCI_MEM_SIZE ((PCI_MEM_END - PCI_MEM_START) + 1)
96 1.21 thorpej
97 1.1 augustss static int
98 1.1 augustss cpc_print(void *aux, const char *pnp)
99 1.1 augustss {
100 1.1 augustss struct cpcbus_attach_args *caa = aux;
101 1.1 augustss
102 1.1 augustss if (pnp)
103 1.5 thorpej aprint_normal("%s at %s", caa->cpca_name, pnp);
104 1.1 augustss
105 1.5 thorpej aprint_normal(" addr 0x%08x", caa->cpca_addr);
106 1.1 augustss if (caa->cpca_irq != CPCBUSCF_IRQ_DEFAULT)
107 1.5 thorpej aprint_normal(" irq %d", caa->cpca_irq);
108 1.1 augustss
109 1.1 augustss return (UNCONF);
110 1.1 augustss }
111 1.1 augustss
112 1.1 augustss static int
113 1.16 cegger cpc_submatch(device_t parent, cfdata_t cf,
114 1.10 drochner const int *ldesc, void *aux)
115 1.1 augustss {
116 1.1 augustss struct cpcbus_attach_args *caa = aux;
117 1.1 augustss
118 1.1 augustss if (cf->cf_loc[CPCBUSCF_ADDR] != caa->cpca_addr)
119 1.1 augustss return (0);
120 1.1 augustss
121 1.4 thorpej return (config_match(parent, cf, aux));
122 1.1 augustss }
123 1.1 augustss
124 1.1 augustss /*
125 1.1 augustss * Attach the cpc.
126 1.1 augustss */
127 1.1 augustss void
128 1.16 cegger cpc_attach(device_t self, pci_chipset_tag_t pc, bus_space_tag_t mem,
129 1.1 augustss bus_space_tag_t pciio, bus_dma_tag_t dma, int attachpci,
130 1.1 augustss uint freq)
131 1.1 augustss {
132 1.1 augustss union attach_args aa;
133 1.1 augustss int i;
134 1.9 perry pcitag_t tag;
135 1.1 augustss pcireg_t erren;
136 1.7 augustss pcireg_t v;
137 1.1 augustss static struct {
138 1.1 augustss const char *name;
139 1.1 augustss bus_addr_t addr;
140 1.1 augustss int irq;
141 1.1 augustss } devs[] = {
142 1.2 augustss { "com", CPC_COM0, CPC_IB_UART_0 },
143 1.2 augustss { "com", CPC_COM1, CPC_IB_UART_1 },
144 1.2 augustss { "cpctim", CPC_TIMER, CPCBUSCF_IRQ_DEFAULT },
145 1.2 augustss { "cpciic", CPC_IIC0, CPC_IB_IIC_0 },
146 1.2 augustss { "cpciic", CPC_IIC1, CPC_IB_IIC_1 },
147 1.1 augustss { NULL, 0 }
148 1.1 augustss };
149 1.1 augustss #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
150 1.1 augustss #ifdef PCI_CONFIGURE_VERBOSE
151 1.1 augustss extern int pci_conf_debug;
152 1.1 augustss
153 1.1 augustss pci_conf_debug = 1;
154 1.1 augustss #endif
155 1.1 augustss #endif
156 1.1 augustss
157 1.1 augustss printf(": IBM CPC700\n");
158 1.1 augustss
159 1.1 augustss the_cpc_tag = mem;
160 1.1 augustss if (bus_space_map(mem, CPC_UIC_BASE, CPC_UIC_SIZE, 0,
161 1.1 augustss &the_cpc_handle)) {
162 1.13 cegger aprint_error_dev(self, "can't map i/o space\n");
163 1.1 augustss return;
164 1.1 augustss }
165 1.1 augustss
166 1.1 augustss aa.cba.cpca_tag = mem;
167 1.1 augustss aa.cba.cpca_freq = freq;
168 1.1 augustss for (i = 0; devs[i].name; i++) {
169 1.1 augustss aa.cba.cpca_name = devs[i].name;
170 1.1 augustss aa.cba.cpca_addr = devs[i].addr;
171 1.1 augustss aa.cba.cpca_irq = devs[i].irq;
172 1.22 thorpej config_found(self, &aa.cba, cpc_print,
173 1.23 thorpej CFARGS(.submatch = cpc_submatch,
174 1.23 thorpej .iattr = "cpcbus"));
175 1.1 augustss }
176 1.1 augustss
177 1.1 augustss tag = pci_make_tag(pc, 0, 0, 0);
178 1.1 augustss
179 1.1 augustss aa.pba.pba_iot = pciio;
180 1.1 augustss aa.pba.pba_memt = mem;
181 1.1 augustss aa.pba.pba_dmat = dma;
182 1.17 matt aa.pba.pba_pc = pc;
183 1.18 dyoung aa.pba.pba_flags = PCI_FLAGS_MEM_OKAY | PCI_FLAGS_IO_OKAY;
184 1.1 augustss aa.pba.pba_bus = 0;
185 1.1 augustss
186 1.1 augustss /* Save PCI error condition reg. */
187 1.1 augustss erren = pci_conf_read(pc, tag, CPC_PCI_BRDGERR);
188 1.7 augustss /* Don't generate errors during probe. */
189 1.1 augustss pci_conf_write(pc, tag, CPC_PCI_BRDGERR, 0);
190 1.7 augustss
191 1.7 augustss /* Program MITL */
192 1.7 augustss v = pci_conf_read(pc, tag, CPC_BRIDGE_OPTIONS2);
193 1.7 augustss v &= ~(CPC_BRIDGE_O2_ILAT_MASK | CPC_BRIDGE_O2_SLAT_MASK);
194 1.7 augustss v |= (CPC_BRIDGE_O2_ILAT_PRIM_ASYNC << CPC_BRIDGE_O2_ILAT_SHIFT) |
195 1.7 augustss (CPC_BRIDGE_O2_2LAT_PRIM_ASYNC << CPC_BRIDGE_O2_SLAT_SHIFT);
196 1.7 augustss pci_conf_write(pc, tag, CPC_BRIDGE_OPTIONS2, v);
197 1.1 augustss
198 1.1 augustss #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
199 1.21 thorpej struct pciconf_resources *pcires = pciconf_resource_init();
200 1.1 augustss
201 1.21 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_IO,
202 1.21 thorpej PCI_IO_START, PCI_IO_SIZE);
203 1.21 thorpej pciconf_resource_add(pcires, PCICONF_RESOURCE_MEM,
204 1.21 thorpej PCI_MEM_START, PCI_MEM_SIZE);
205 1.1 augustss
206 1.21 thorpej pci_configure_bus(0, pcires, 0, 32);
207 1.1 augustss #endif
208 1.1 augustss
209 1.22 thorpej config_found(self, &aa.pba, pcibusprint,
210 1.23 thorpej CFARGS(.iattr = "pcibus"));
211 1.1 augustss
212 1.1 augustss /* Restore error triggers, and clear errors */
213 1.1 augustss pci_conf_write(pc, tag, CPC_PCI_BRDGERR, erren | CPC_PCI_CLEARERR);
214 1.1 augustss }
215 1.1 augustss
216 1.1 augustss /***************************************************************************/
217 1.1 augustss
218 1.1 augustss /*
219 1.1 augustss * Interrupt controller.
220 1.1 augustss */
221 1.1 augustss
222 1.1 augustss void
223 1.1 augustss cpc700_init_intr(bus_space_tag_t bt, bus_space_handle_t bh,
224 1.1 augustss u_int32_t active, u_int32_t level)
225 1.1 augustss {
226 1.1 augustss /* XXX */
227 1.1 augustss the_cpc_tag = bt;
228 1.1 augustss the_cpc_handle = bh;
229 1.9 perry /*
230 1.1 augustss * See CPC700 manual for information about what
231 1.1 augustss * interrupts have which properties.
232 1.1 augustss */
233 1.1 augustss OUTL(CPC_UIC_SR, 0xffffffff); /* clear all intrs */
234 1.1 augustss OUTL(CPC_UIC_ER, 0x00000000); /* disable all intrs */
235 1.1 augustss OUTL(CPC_UIC_CR, 0xffffffff); /* gen INT not MCP */
236 1.1 augustss OUTL(CPC_UIC_PR, 0xffff8000 | active); /* 0 = active low */
237 1.1 augustss OUTL(CPC_UIC_TR, 0xc0000000 | level); /* 0 = level intr */
238 1.1 augustss OUTL(CPC_UIC_VR, CPC_UIC_CVR_PRI); /* intr 0 is highest */
239 1.1 augustss }
240 1.1 augustss
241 1.1 augustss int
242 1.1 augustss cpc700_read_irq(void)
243 1.1 augustss {
244 1.1 augustss int irq;
245 1.1 augustss u_int32_t irqs;
246 1.1 augustss
247 1.1 augustss irqs = INL(CPC_UIC_MSR);
248 1.1 augustss for (irq = 0; irq < ICU_LEN; irq++) {
249 1.1 augustss if (irqs & CPC_INTR_MASK(irq))
250 1.1 augustss return (irq);
251 1.1 augustss }
252 1.1 augustss return (-1);
253 1.1 augustss }
254 1.1 augustss
255 1.1 augustss void
256 1.1 augustss cpc700_eoi(int irq)
257 1.1 augustss {
258 1.1 augustss OUTL(CPC_UIC_SR, CPC_INTR_MASK(irq));
259 1.1 augustss }
260 1.1 augustss
261 1.1 augustss void
262 1.1 augustss cpc700_disable_irq(int irq)
263 1.1 augustss {
264 1.1 augustss u_int32_t reg;
265 1.1 augustss
266 1.1 augustss reg = INL(CPC_UIC_ER);
267 1.1 augustss reg &= ~CPC_INTR_MASK(irq);
268 1.1 augustss OUTL(CPC_UIC_ER, reg);
269 1.1 augustss }
270 1.1 augustss
271 1.1 augustss void
272 1.1 augustss cpc700_enable_irq(int irq)
273 1.1 augustss {
274 1.1 augustss u_int32_t reg;
275 1.1 augustss
276 1.1 augustss reg = INL(CPC_UIC_ER);
277 1.1 augustss reg |= CPC_INTR_MASK(irq);
278 1.1 augustss OUTL(CPC_UIC_ER, reg);
279 1.1 augustss }
280