cpc700.c revision 1.1 1 /* $NetBSD: cpc700.c,v 1.1 2002/05/21 02:58:25 augustss 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 (at) 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * The IBM CPC700 is a bridge chip for the PowerPC. It contains
41 * - CPU interface
42 * - DRAM controller
43 * - PCI bus master & slave controller
44 * - interrupt controller
45 * - timer
46 * - two UARTs
47 * - two IIC ports
48 *
49 * This driver handles the overall device and enumeration of the
50 * supported subdevices. NetBSD knows how to handle:
51 * - PCI master
52 * - interrupt controller
53 * - UARTs
54 * Skeleton drivers are provided for the timer and IIC.
55 *
56 * XXX This driver assumes that there is only one instance of it.
57 */
58
59 #include "pci.h"
60 #include "opt_pci.h"
61
62 #include <sys/param.h>
63 #include <sys/extent.h>
64 #include <sys/device.h>
65 #include <sys/malloc.h>
66 #include <sys/systm.h>
67
68 #include <machine/bus.h>
69 #include "locators.h"
70
71 #include <dev/pci/pcivar.h>
72 #include <dev/pci/pcireg.h>
73 #include <dev/pci/pciconf.h>
74
75 #include <dev/ic/cpc700reg.h>
76 #include <dev/ic/cpc700var.h>
77 #include <dev/ic/cpc700uic.h>
78
79 union attach_args {
80 const char *busname; /* first elem of all */
81 struct pcibus_attach_args pba;
82 struct cpcbus_attach_args cba;
83 };
84
85
86 void
87 cpc_attach(struct device *self, pci_chipset_tag_t pc, bus_space_tag_t mem,
88 bus_space_tag_t pciio, bus_dma_tag_t tag, int attachpci,
89 uint freq);
90
91 static bus_space_tag_t the_cpc_tag;
92 static bus_space_handle_t the_cpc_handle;
93 #define INL(a) bus_space_read_stream_4(the_cpc_tag, the_cpc_handle, (a))
94 #define OUTL(a, d) bus_space_write_stream_4(the_cpc_tag, the_cpc_handle, (a), d)
95
96 static int
97 cpc_print(void *aux, const char *pnp)
98 {
99 struct cpcbus_attach_args *caa = aux;
100
101 if (pnp)
102 printf("%s at %s", caa->cpca_name, pnp);
103
104 printf(" addr 0x%08x", caa->cpca_addr);
105 if (caa->cpca_irq != CPCBUSCF_IRQ_DEFAULT)
106 printf(" irq %d", caa->cpca_irq);
107
108 return (UNCONF);
109 }
110
111 static int
112 cpcpci_print(void *aux, const char *pnp)
113 {
114 union attach_args *aa = aux;
115
116 if (pnp)
117 printf("%s at %s", aa->busname, pnp);
118
119 return (UNCONF);
120 }
121
122 static int
123 cpc_submatch(struct device *parent, struct cfdata *cf, void *aux)
124 {
125 struct cpcbus_attach_args *caa = aux;
126
127 if (cf->cf_loc[CPCBUSCF_ADDR] != caa->cpca_addr)
128 return (0);
129
130 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
131 }
132
133 /*
134 * Attach the cpc.
135 */
136 void
137 cpc_attach(struct device *self, pci_chipset_tag_t pc, bus_space_tag_t mem,
138 bus_space_tag_t pciio, bus_dma_tag_t dma, int attachpci,
139 uint freq)
140 {
141 union attach_args aa;
142 int i;
143 pcitag_t tag;
144 pcireg_t erren;
145 static struct {
146 const char *name;
147 bus_addr_t addr;
148 int irq;
149 } devs[] = {
150 { "com", CPC_COM0, CPC_IB_UART_0 },
151 { "com", CPC_COM1, CPC_IB_UART_1 },
152 { "timer", CPC_TIMER, CPCBUSCF_IRQ_DEFAULT },
153 { "iic", CPC_IIC0, CPC_IB_IIC_0 },
154 { "iic", CPC_IIC1, CPC_IB_IIC_1 },
155 { NULL, 0 }
156 };
157 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
158 struct extent *ioext, *memext;
159 #ifdef PCI_CONFIGURE_VERBOSE
160 extern int pci_conf_debug;
161
162 pci_conf_debug = 1;
163 #endif
164 #endif
165
166 printf(": IBM CPC700\n");
167
168 the_cpc_tag = mem;
169 if (bus_space_map(mem, CPC_UIC_BASE, CPC_UIC_SIZE, 0,
170 &the_cpc_handle)) {
171 printf("%s: can't map i/o space\n", self->dv_xname);
172 return;
173 }
174
175 aa.cba.cpca_tag = mem;
176 aa.cba.cpca_freq = freq;
177 for (i = 0; devs[i].name; i++) {
178 aa.cba.cpca_name = devs[i].name;
179 aa.cba.cpca_addr = devs[i].addr;
180 aa.cba.cpca_irq = devs[i].irq;
181 config_found_sm(self, &aa.cba, cpc_print, cpc_submatch);
182 }
183
184 tag = pci_make_tag(pc, 0, 0, 0);
185
186 aa.pba.pba_busname = "pci";
187 aa.pba.pba_iot = pciio;
188 aa.pba.pba_memt = mem;
189 aa.pba.pba_dmat = dma;
190 aa.pba.pba_pc = 0;
191 aa.pba.pba_flags = PCI_FLAGS_MEM_ENABLED | PCI_FLAGS_IO_ENABLED;
192 aa.pba.pba_bus = 0;
193
194 /* Save PCI error condition reg. */
195 erren = pci_conf_read(pc, tag, CPC_PCI_BRDGERR);
196 pci_conf_write(pc, tag, CPC_PCI_BRDGERR, 0);
197
198 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
199 ioext = extent_create("pciio", CPC_PCI_IO_START, CPC_PCI_IO_END,
200 M_DEVBUF, NULL, 0, EX_NOWAIT);
201 memext = extent_create("pcimem", CPC_PCI_MEM_BASE, CPC_PCI_MEM_END,
202 M_DEVBUF, NULL, 0, EX_NOWAIT);
203
204 printf("WARNING: PCI_NETBSD_CONFIGURE is broken\n");
205 pci_configure_bus(0, ioext, memext, NULL, 0, 32);
206
207 extent_destroy(ioext);
208 extent_destroy(memext);
209 #endif
210
211 config_found(self, &aa.pba, cpcpci_print);
212
213 /* Restore error triggers, and clear errors */
214 pci_conf_write(pc, tag, CPC_PCI_BRDGERR, erren | CPC_PCI_CLEARERR);
215 }
216
217 /***************************************************************************/
218
219 /*
220 * Interrupt controller.
221 */
222
223 void
224 cpc700_init_intr(bus_space_tag_t bt, bus_space_handle_t bh,
225 u_int32_t active, u_int32_t level)
226 {
227 /* XXX */
228 the_cpc_tag = bt;
229 the_cpc_handle = bh;
230 /*
231 * See CPC700 manual for information about what
232 * interrupts have which properties.
233 */
234 OUTL(CPC_UIC_SR, 0xffffffff); /* clear all intrs */
235 OUTL(CPC_UIC_ER, 0x00000000); /* disable all intrs */
236 OUTL(CPC_UIC_CR, 0xffffffff); /* gen INT not MCP */
237 OUTL(CPC_UIC_PR, 0xffff8000 | active); /* 0 = active low */
238 OUTL(CPC_UIC_TR, 0xc0000000 | level); /* 0 = level intr */
239 OUTL(CPC_UIC_VR, CPC_UIC_CVR_PRI); /* intr 0 is highest */
240 }
241
242 int
243 cpc700_read_irq(void)
244 {
245 int irq;
246 u_int32_t irqs;
247
248 irqs = INL(CPC_UIC_MSR);
249 for (irq = 0; irq < ICU_LEN; irq++) {
250 if (irqs & CPC_INTR_MASK(irq))
251 return (irq);
252 }
253 return (-1);
254 }
255
256 void
257 cpc700_eoi(int irq)
258 {
259 OUTL(CPC_UIC_SR, CPC_INTR_MASK(irq));
260 }
261
262 void
263 cpc700_disable_irq(int irq)
264 {
265 u_int32_t reg;
266
267 reg = INL(CPC_UIC_ER);
268 reg &= ~CPC_INTR_MASK(irq);
269 OUTL(CPC_UIC_ER, reg);
270 }
271
272 void
273 cpc700_enable_irq(int irq)
274 {
275 u_int32_t reg;
276
277 reg = INL(CPC_UIC_ER);
278 reg |= CPC_INTR_MASK(irq);
279 OUTL(CPC_UIC_ER, reg);
280 }
281