1/*	$NetBSD: mainbus.c,v 1.25 2011/07/09 16:03:01 matt Exp $	*/
2
3/*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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: mainbus.c,v 1.25 2011/07/09 16:03:01 matt Exp $");
34
35#include "opt_algor_p4032.h"
36#include "opt_algor_p5064.h"
37#include "opt_algor_p6032.h"
38
39#include "opt_pci.h"
40
41#include <sys/param.h>
42#include <sys/bus.h>
43#include <sys/conf.h>
44#include <sys/device.h>
45#include <sys/extent.h>
46#include <sys/malloc.h>
47#include <sys/reboot.h>
48#include <sys/systm.h>
49
50#include <algor/autoconf.h>
51
52#include <mips/cache.h>
53
54#include <dev/pci/pcivar.h>
55#include <dev/pci/pciconf.h>
56
57#if defined(PCI_NETBSD_CONFIGURE) && defined(PCI_NETBSD_ENABLE_IDE)
58#if defined(ALGOR_P5064) || defined(ALGOR_P6032)
59#include <dev/pci/pciide_piix_reg.h>
60#endif /* ALGOR_P5064 || ALGOR_P6032 */
61#endif /* PCI_NETBSD_CONFIGURE && PCI_NETBSD_ENABLE_IDE */
62
63#include "locators.h"
64#include "pci.h"
65
66int	mainbus_match(device_t, cfdata_t, void *);
67void	mainbus_attach(device_t, device_t, void *);
68
69CFATTACH_DECL_NEW(mainbus, 0,
70    mainbus_match, mainbus_attach, NULL, NULL);
71
72int	mainbus_print(void *, const char *);
73int	mainbus_submatch(device_t, cfdata_t,
74			 const int *, void *);
75
76/* There can be only one. */
77int	mainbus_found;
78
79struct mainbusdev {
80	const char *md_name;
81	bus_addr_t md_addr;
82	int md_irq;
83};
84
85#if defined(ALGOR_P4032)
86#include <algor/algor/algor_p4032reg.h>
87#include <algor/algor/algor_p4032var.h>
88
89struct mainbusdev mainbusdevs[] = {
90	{ "cpu",		-1,			-1 },
91	{ "mcclock",		P4032_RTC,		P4032_IRQ_RTC },
92	{ "com",		P4032_COM1,		P4032_IRQ_COM1 },
93	{ "com",		P4032_COM2,		P4032_IRQ_COM2 },
94	{ "lpt",		P4032_LPT,		P4032_IRQ_LPT },
95	{ "pckbc",		P4032_PCKBC,		P4032_IRQ_PCKBC },
96	{ "fdc",		P4032_FDC,		P4032_IRQ_FLOPPY },
97	{ "vtpbc",		P4032_V962PBC,		-1 },
98
99	{ NULL,			0,			0 },
100};
101#endif /* ALGOR_P4032 */
102
103#if defined(ALGOR_P5064)
104#include <algor/algor/algor_p5064reg.h>
105#include <algor/algor/algor_p5064var.h>
106
107struct mainbusdev mainbusdevs[] = {
108	{ "cpu",		-1,			-1 },
109	{ "vtpbc",		P5064_V360EPC,		-1 },
110
111	{ NULL,			0,			0 },
112};
113#endif /* ALGOR_P5064 */
114
115#if defined(ALGOR_P6032)
116#include <algor/algor/algor_p6032reg.h>
117#include <algor/algor/algor_p6032var.h>
118
119struct mainbusdev mainbusdevs[] = {
120	{ "cpu",		-1,			-1 },
121	{ "bonito",		BONITO_REG_BASE,	-1 },
122
123	{ NULL,			0,			0 },
124};
125#endif /* ALGOR_P6032 */
126
127int
128mainbus_match(device_t parent, cfdata_t cf, void *aux)
129{
130
131	if (mainbus_found)
132		return (0);
133
134	return (1);
135}
136
137void
138mainbus_attach(device_t parent, device_t self, void *aux)
139{
140	struct mainbus_attach_args ma;
141	struct mainbusdev *md;
142	bus_space_tag_t st;
143#if defined(PCI_NETBSD_CONFIGURE)
144	struct extent *ioext, *memext;
145	pci_chipset_tag_t pc;
146#if defined(PCI_NETBSD_ENABLE_IDE)
147	pcitag_t idetag;
148	pcireg_t idetim;
149#endif
150#endif
151
152	mainbus_found = 1;
153
154	printf("\n");
155
156#if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
157#if defined(ALGOR_P4032)
158	/*
159	 * Reserve the bottom 64K of the I/O space for ISA devices.
160	 */
161	ioext  = extent_create("pciio",  0x00010000, 0x000effff,
162	    NULL, 0, EX_NOWAIT);
163	memext = extent_create("pcimem", 0x01000000, 0x07ffffff,
164	    NULL, 0, EX_NOWAIT);
165
166	pc = &p4032_configuration.ac_pc;
167#elif defined(ALGOR_P5064)
168	/*
169	 * Reserve the bottom 512K of the I/O space for ISA devices.
170	 * According to the PMON sources, this is a work-around for
171	 * a bug in the ISA bridge.
172	 */
173	ioext  = extent_create("pciio",  0x00080000, 0x00ffffff,
174	    NULL, 0, EX_NOWAIT);
175	memext = extent_create("pcimem", 0x01000000, 0x07ffffff,
176	    NULL, 0, EX_NOWAIT);
177
178	pc = &p5064_configuration.ac_pc;
179#if defined(PCI_NETBSD_ENABLE_IDE)
180	idetag = pci_make_tag(pc, 0, 2, 1);
181#endif
182#elif defined(ALGOR_P6032)
183	/*
184	 * Reserve the bottom 64K of the I/O space for ISA devices.
185	 */
186	ioext  = extent_create("pciio",  0x00010000, 0x000effff,
187	    NULL, 0, EX_NOWAIT);
188	memext = extent_create("pcimem", 0x01000000, 0x0affffff,
189	    NULL, 0, EX_NOWAIT);
190
191	pc = &p6032_configuration.ac_pc;
192#if defined(PCI_NETBSD_ENABLE_IDE)
193	idetag = pci_make_tag(pc, 0, 17, 1);
194#endif
195#endif /* ALGOR_P4032 || ALGOR_P5064 || ALGOR_P6032 */
196
197	pci_configure_bus(pc, ioext, memext, NULL, 0, mips_cache_info.mci_dcache_align);
198	extent_destroy(ioext);
199	extent_destroy(memext);
200
201#if defined(PCI_NETBSD_ENABLE_IDE)
202	/*
203	 * Perhaps PMON has not enabled the IDE controller.  Easy to
204	 * fix -- just set the ENABLE bits for each channel in the
205	 * IDETIM register.  Just clear all the bits for the channel
206	 * except for the ENABLE bits -- the `pciide' driver will
207	 * properly configure it later.
208	 */
209	idetim = 0;
210	if (PCI_NETBSD_ENABLE_IDE & 0x01)
211		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE, 0);
212	if (PCI_NETBSD_ENABLE_IDE & 0x02)
213		idetim = PIIX_IDETIM_SET(idetim, PIIX_IDETIM_IDE, 1);
214	pci_conf_write(pc, idetag, PIIX_IDETIM, idetim);
215#endif
216#endif /* NPCI > 0 && defined(PCI_NETBSD_CONFIGURE) */
217
218#if defined(ALGOR_P4032)
219	st = &p4032_configuration.ac_lociot;
220#elif defined(ALGOR_P5064)
221	st = NULL;
222#elif defined(ALGOR_P6032)
223	st = NULL;
224#endif
225
226	for (md = mainbusdevs; md->md_name != NULL; md++) {
227		ma.ma_name = md->md_name;
228		ma.ma_st = st;
229		ma.ma_addr = md->md_addr;
230		ma.ma_irq = md->md_irq;
231		(void) config_found_sm_loc(self, "mainbus", NULL, &ma,
232		    mainbus_print, mainbus_submatch);
233	}
234}
235
236int
237mainbus_print(void *aux, const char *pnp)
238{
239	struct mainbus_attach_args *ma = aux;
240
241	if (pnp)
242		aprint_normal("%s at %s", ma->ma_name, pnp);
243	if (ma->ma_addr != (bus_addr_t) -1)
244		aprint_normal(" addr %#" PRIxBUSADDR, ma->ma_addr);
245
246	return (UNCONF);
247}
248
249int
250mainbus_submatch(device_t parent, cfdata_t cf,
251		 const int *ldesc, void *aux)
252{
253	struct mainbus_attach_args *ma = aux;
254
255	if (cf->cf_loc[MAINBUSCF_ADDR] != MAINBUSCF_ADDR_DEFAULT &&
256	    cf->cf_loc[MAINBUSCF_ADDR] != ma->ma_addr)
257		return (0);
258
259	return (config_match(parent, cf, aux));
260}
261