1 /*	$NetBSD: i82365_pci.c,v 1.33 2012/10/27 17:18:32 chs Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Marc Horowitz.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * XXX this driver frontend is *very* i386 dependent and should be relocated
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: i82365_pci.c,v 1.33 2012/10/27 17:18:32 chs Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 
43 #include <dev/ic/i82365reg.h>
44 #include <dev/ic/i82365var.h>
45 
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcidevs.h>
49 #include <dev/pci/i82365_pcivar.h>
50 
51 #include <dev/isa/isavar.h>
52 #include <dev/isa/i82365_isavar.h>
53 
54 /*
55  * PCI constants.
56  * XXX These should be in a common file!
57  */
58 #define	PCI_CBIO		0x10	/* Configuration Base IO Address */
59 
60 int	pcic_pci_match(device_t, cfdata_t, void *);
61 void	pcic_pci_attach(device_t, device_t, void *);
62 
63 CFATTACH_DECL_NEW(pcic_pci, sizeof(struct pcic_pci_softc),
64     pcic_pci_match, pcic_pci_attach, NULL, NULL);
65 
66 static const struct pcmcia_chip_functions pcic_pci_functions = {
67 	pcic_chip_mem_alloc,
68 	pcic_chip_mem_free,
69 	pcic_chip_mem_map,
70 	pcic_chip_mem_unmap,
71 
72 	pcic_chip_io_alloc,
73 	pcic_chip_io_free,
74 	pcic_chip_io_map,
75 	pcic_chip_io_unmap,
76 
77 	/* XXX */
78 	pcic_isa_chip_intr_establish,
79 	pcic_isa_chip_intr_disestablish,
80 
81 	pcic_chip_socket_enable,
82 	pcic_chip_socket_disable,
83 	pcic_chip_socket_settype,
84 
85 	NULL,				/* card_detect */
86 };
87 
88 static void pcic_pci_callback(device_t);
89 
90 int
pcic_pci_match(device_t parent,cfdata_t match,void * aux)91 pcic_pci_match(device_t parent, cfdata_t match, void *aux)
92 {
93 	struct pci_attach_args *pa = aux;
94 
95 	switch (PCI_VENDOR(pa->pa_id)) {
96 	case PCI_VENDOR_CIRRUS:
97 		switch (PCI_PRODUCT(pa->pa_id)) {
98 		case PCI_PRODUCT_CIRRUS_CL_PD6729:
99 			break;
100 		default:
101 			return 0;
102 		}
103 		break;
104 	default:
105 		return 0;
106 	}
107 	return 1;
108 }
109 
110 void
pcic_pci_attach(device_t parent,device_t self,void * aux)111 pcic_pci_attach(device_t parent, device_t self, void *aux)
112 {
113 	struct pcic_pci_softc *psc = device_private(self);
114 	struct pcic_softc *sc = &psc->sc_pcic;
115 	struct pci_attach_args *pa = aux;
116 	pci_chipset_tag_t pc = pa->pa_pc;
117 	bus_space_tag_t memt = pa->pa_memt;
118 	bus_space_handle_t memh;
119 	const char *model;
120 
121 	sc->dev = self;
122 
123 	aprint_naive(": PCMCIA controller\n");
124 
125 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
126 	    &sc->iot, &sc->ioh, NULL, NULL)) {
127 		aprint_error(": can't map i/o space\n");
128 		return;
129 	}
130 
131 	/*
132 	 * XXX need some memory for mapping pcmcia cards into. Ideally, this
133 	 * would be completely dynamic.  Practically this doesn't work,
134 	 * because the extent mapper doesn't know about all the devices all
135 	 * the time.  With ISA we could finesse the issue by specifying the
136 	 * memory region in the config line.  We can't do that here, so we
137 	 * cheat for now. Jason Thorpe, you are my Savior, come up with a fix
138 	 * :-)
139 	 */
140 
141 	/* Map mem space. */
142 	if (bus_space_map(memt, 0xd0000, 0x4000, 0, &memh))
143 		panic("pcic_pci_attach: can't map mem space");
144 
145 	sc->membase = 0xd0000;
146 	sc->subregionmask = (1 << (0x4000 / PCIC_MEM_PAGESIZE)) - 1;
147 
148 	/* same deal for io allocation */
149 
150 	sc->iobase = 0x400;
151 	sc->iosize = 0xbff;
152 
153 	/* end XXX */
154 
155 	sc->pct = &pcic_pci_functions;
156 
157 	sc->memt = memt;
158 	sc->memh = memh;
159 
160 	switch (PCI_PRODUCT(pa->pa_id)) {
161 	case PCI_PRODUCT_CIRRUS_CL_PD6729:
162 		model = "Cirrus Logic PD6729 PCMCIA controller";
163 		break;
164 	default:
165 		model = "Model unknown";
166 		break;
167 	}
168 
169 	aprint_normal(": %s\n", model);
170 
171 	/* Enable the card. */
172 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
173 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
174 	    PCI_COMMAND_MASTER_ENABLE);
175 
176 	pcic_attach(sc);
177 
178 	/*
179 	 * Check to see if we're using PCI or ISA interrupts. I don't
180 	 * know of any i386 systems that use the 6729 in PCI interrupt
181 	 * mode, but maybe when the PCMCIA code runs on other platforms
182 	 * we'll need to fix this.
183 	 */
184 	pcic_write(&sc->handle[0], PCIC_CIRRUS_EXTENDED_INDEX,
185 	    PCIC_CIRRUS_EXT_CONTROL_1);
186 	if ((pcic_read(&sc->handle[0], PCIC_CIRRUS_EXTENDED_DATA) &
187 	    PCIC_CIRRUS_EXT_CONTROL_1_PCI_INTR_MASK)) {
188 		aprint_error_dev(self, "PCI interrupts not supported\n");
189 		return;
190 	}
191 
192 	psc->intr_est = pcic_pci_machdep_intr_est(pc);
193 	sc->irq = -1;
194 
195 #if 0
196 	/* Map and establish the interrupt. */
197 	sc->ih = pcic_pci_machdep_pcic_intr_establish(sc, pcic_intr);
198 	if (sc->ih == NULL) {
199 		aprint_error_dev(self, "couldn't map interrupt\n");
200 		return;
201 	}
202 #endif
203 
204 	/*
205 	 * Defer configuration of children until ISA has had its chance
206 	 * to use up whatever IO space and IRQs it wants. XXX This will
207 	 * only work if ISA is attached to a pcib, AND the PCI probe finds
208 	 * and defers the ISA attachment before this one.
209 	 */
210 	config_defer(self, pcic_pci_callback);
211 	config_interrupts(self, pcic_isa_config_interrupts);
212 }
213 
214 static void
pcic_pci_callback(device_t self)215 pcic_pci_callback(device_t self)
216 {
217 	struct pcic_softc *sc = device_private(self);
218 
219 	pcic_attach_sockets(sc);
220 }
221