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