1/*	$NetBSD: plumiobus.c,v 1.12 2005/12/11 12:17:33 christos Exp $ */
2
3/*-
4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
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#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: plumiobus.c,v 1.12 2005/12/11 12:17:33 christos Exp $");
34
35#define PLUMIOBUSDEBUG
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/device.h>
40#include <sys/malloc.h>
41
42#include <machine/bus.h>
43#include <machine/intr.h>
44
45#include <hpcmips/tx/tx39var.h>
46#include <hpcmips/dev/plumvar.h>
47#include <hpcmips/dev/plumicuvar.h>
48#include <hpcmips/dev/plumpowervar.h>
49#include <hpcmips/dev/plumiobusreg.h>
50#include <hpcmips/dev/plumiobusvar.h>
51
52#include "locators.h"
53
54#ifdef PLUMIOBUSDEBUG
55int	plumiobus_debug = 0;
56#define	DPRINTF(arg) if (plumiobus_debug) printf arg;
57#define	DPRINTFN(n, arg) if (plumiobus_debug > (n)) printf arg;
58#else
59#define	DPRINTF(arg)
60#define DPRINTFN(n, arg)
61#endif
62
63int plumiobus_match(struct device *, struct cfdata *, void *);
64void plumiobus_attach(struct device *, struct device *, void *);
65int plumiobus_print(void *, const char *);
66int plumiobus_search(struct device *, struct cfdata *,
67		     const int *, void *);
68
69struct plumisa_resource {
70	int		pr_irq;
71	bus_space_tag_t	pr_iot;
72	int		pr_enabled;
73};
74
75struct plumiobus_softc {
76	struct	device		sc_dev;
77	plum_chipset_tag_t	sc_pc;
78	bus_space_tag_t		sc_regt;
79	bus_space_handle_t	sc_regh;
80	bus_space_tag_t		sc_iot;
81	bus_space_handle_t	sc_ioh;
82	struct plumisa_resource	sc_isa[PLUM_IOBUS_IO5CSMAX];
83};
84
85CFATTACH_DECL(plumiobus, sizeof(struct plumiobus_softc),
86    plumiobus_match, plumiobus_attach, NULL, NULL);
87
88bus_space_tag_t __plumiobus_subregion(bus_space_tag_t, bus_addr_t,
89    bus_size_t);
90#ifdef PLUMIOBUSDEBUG
91void plumiobus_dump(struct plumiobus_softc *);
92#endif
93
94int
95plumiobus_match(struct device *parent, struct cfdata *cf, void *aux)
96{
97
98	return (1);
99}
100
101void
102plumiobus_attach(struct device *parent, struct device *self, void *aux)
103{
104	struct plum_attach_args *pa = aux;
105	struct plumiobus_softc *sc = (void*)self;
106	struct plumisa_resource *pr;
107
108	sc->sc_pc	= pa->pa_pc;
109	sc->sc_regt	= pa->pa_regt;
110	sc->sc_iot	= pa->pa_iot;
111
112	if (bus_space_map(sc->sc_regt, PLUM_IOBUS_REGBASE,
113	    PLUM_IOBUS_REGSIZE, 0, &sc->sc_regh)) {
114		printf(": register map failed.\n");
115		return;
116	}
117	printf("\n");
118	plum_power_establish(sc->sc_pc, PLUM_PWR_IO5);
119
120	/* Address space <-> IRQ mapping */
121	pr = &sc->sc_isa[IO5CS0];
122	pr->pr_irq = PLUM_INT_EXT5IO0;
123	pr->pr_iot = __plumiobus_subregion(
124		sc->sc_iot,
125		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS0BASE,
126		PLUM_IOBUS_IO5SIZE);
127
128	pr = &sc->sc_isa[IO5CS1];
129	pr->pr_irq = PLUM_INT_EXT5IO1;
130	pr->pr_iot = __plumiobus_subregion(
131		sc->sc_iot,
132		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS1BASE,
133		PLUM_IOBUS_IO5SIZE);
134
135	pr = &sc->sc_isa[IO5CS2];
136	pr->pr_irq = PLUM_INT_EXT5IO2;
137	pr->pr_iot = __plumiobus_subregion(
138		sc->sc_iot,
139		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS2BASE,
140		PLUM_IOBUS_IO5SIZE);
141
142	pr = &sc->sc_isa[IO5CS3];
143	pr->pr_irq = PLUM_INT_EXT5IO3;
144	pr->pr_iot = __plumiobus_subregion(
145		sc->sc_iot,
146		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS3BASE,
147		PLUM_IOBUS_IO5SIZE);
148
149	pr = &sc->sc_isa[IO5CS4];
150	pr->pr_irq = PLUM_INT_EXT3IO0; /* XXX */
151	pr->pr_iot = __plumiobus_subregion(
152		sc->sc_iot,
153		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS4BASE,
154		PLUM_IOBUS_IO5SIZE);
155
156
157	pr = &sc->sc_isa[IO5NCS];
158	pr->pr_irq = PLUM_INT_EXT3IO1;
159	pr->pr_iot = __plumiobus_subregion(
160		sc->sc_iot,
161		PLUM_IOBUS_IOBASE + PLUM_IOBUS_IO5CS5BASE,
162		PLUM_IOBUS_IO5SIZE);
163
164#ifdef PLUMIOBUSDEBUG
165	plumiobus_dump(sc);
166#endif
167
168	config_search_ia(plumiobus_search, self, "plumiobusif", plumiobus_print);
169}
170
171/* XXX something kludge */
172bus_space_tag_t
173__plumiobus_subregion(bus_space_tag_t t, bus_addr_t ofs, bus_size_t size)
174{
175	struct hpcmips_bus_space *hbs;
176
177	if (!(hbs = malloc(sizeof(struct hpcmips_bus_space),
178	    M_DEVBUF, M_NOWAIT))) {
179		panic ("__plumiobus_subregion: no memory.");
180	}
181	*hbs = *t;
182	hbs->t_base += ofs;
183	hbs->t_size = size;
184
185	return (hbs);
186}
187
188int
189plumiobus_search(struct device *parent, struct cfdata *cf,
190		 const int *ldesc, void *aux)
191{
192	struct plumiobus_softc *sc = (void*)parent;
193	struct plumiobus_attach_args pba;
194	int slot;
195
196	/* Disallow wildcarded IO5CS slot */
197	if (cf->cf_loc[PLUMIOBUSIFCF_SLOT] == PLUMIOBUSIFCF_SLOT_DEFAULT) {
198		printf("plumiobus_search: wildcarded slot, skipping\n");
199		return (0);
200	}
201	slot = pba.pba_slot = cf->cf_loc[PLUMIOBUSIFCF_SLOT];
202
203	pba.pba_pc	= sc->sc_pc;
204	pba.pba_iot	= sc->sc_isa[slot].pr_iot;
205	pba.pba_irq	= sc->sc_isa[slot].pr_irq;
206	pba.pba_busname	= "plumisab";
207
208	if (!(sc->sc_isa[slot].pr_enabled) && /* not attached slot */
209	    config_match(parent, cf, &pba)) {
210		config_attach(parent, cf, &pba, plumiobus_print);
211		sc->sc_isa[slot].pr_enabled = 1;
212	}
213
214	return (0);
215}
216
217int
218plumiobus_print(void *aux, const char *pnp)
219{
220
221	return (pnp ? QUIET : UNCONF);
222}
223
224#ifdef PLUMIOBUSDEBUG
225void
226plumiobus_dump(struct plumiobus_softc *sc)
227{
228	bus_space_tag_t regt = sc->sc_regt;
229	bus_space_handle_t regh = sc->sc_regh;
230	plumreg_t reg;
231	int i, wait;
232
233	reg = plum_conf_read(regt, regh, PLUM_IOBUS_IOXBSZ_REG);
234	printf("8bit port:");
235	for (i = 0; i < 6; i++) {
236		if (reg & (1 << i)) {
237			printf(" IO5CS%d", i);
238		}
239	}
240	printf("\n");
241
242	reg = PLUM_IOBUS_IOXCCNT_MASK &
243	    plum_conf_read(regt, regh, PLUM_IOBUS_IOXCCNT_REG);
244	printf(" # of wait to become from the access begining: %d clock\n",
245	    reg + 1);
246	reg = plum_conf_read(regt, regh, PLUM_IOBUS_IOXACNT_REG);
247	printf(" # of wait in access clock: ");
248	for (i = 0; i < 5; i++) {
249		wait = (reg >> (i * PLUM_IOBUS_IOXACNT_SHIFT))
250		    & PLUM_IOBUS_IOXACNT_MASK;
251		printf("[CS%d:%d] ", i, wait + 1);
252	}
253	printf("\n");
254
255	reg = PLUM_IOBUS_IOXSCNT_MASK &
256	    plum_conf_read(regt, regh, PLUM_IOBUS_IOXSCNT_REG);
257	printf(" # of wait during access by I/O bus : %d clock\n", reg + 1);
258
259	reg = plum_conf_read(regt, regh, PLUM_IOBUS_IDEMODE_REG);
260	if (reg & PLUM_IOBUS_IDEMODE) {
261		printf("IO5CS3,4 IDE mode\n");
262	}
263}
264#endif /* PLUMIOBUSDEBUG */
265