xref: /freebsd-12.1/sys/dev/pccbb/pccbb_isa.c (revision 5fe4723c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002-2004 M. Warner Losh.
5  * 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Driver for ISA to PCMCIA bridges compliant with the Intel ExCA
31  * specification.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/condvar.h>
41 #include <sys/errno.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
48 #include <sys/kthread.h>
49 #include <sys/bus.h>
50 #include <machine/bus.h>
51 #include <sys/rman.h>
52 #include <machine/resource.h>
53 
54 #include <isa/isavar.h>
55 
56 #include <dev/pci/pcivar.h>
57 #include <dev/pci/pcib_private.h>
58 
59 #include <dev/pccard/pccardreg.h>
60 #include <dev/pccard/pccardvar.h>
61 
62 #include <dev/exca/excareg.h>
63 #include <dev/exca/excavar.h>
64 
65 #include <dev/pccbb/pccbbreg.h>
66 #include <dev/pccbb/pccbbvar.h>
67 
68 #include "power_if.h"
69 #include "card_if.h"
70 
71 /*****************************************************************************
72  * Configurable parameters.
73  *****************************************************************************/
74 
75 /* sysctl vars */
76 static SYSCTL_NODE(_hw, OID_AUTO, pcic, CTLFLAG_RD, 0, "PCIC parameters");
77 
78 static int isa_intr_mask = EXCA_INT_MASK_ALLOWED;
79 SYSCTL_INT(_hw_pcic, OID_AUTO, intr_mask, CTLFLAG_RDTUN, &isa_intr_mask, 0,
80     "Mask of allowable interrupts for this laptop.  The default is generally"
81     " correct, but some laptops do not route all the IRQ pins to the bridge to"
82     " save wires.  Sometimes you need a more restrictive mask because some of"
83     " the hardware in your laptop may not have a driver so its IRQ might not be"
84     " allocated.");
85 
86 /*
87  * CL-PD6722's VSENSE method
88  *     0: NO VSENSE (assume a 5.0V card always)
89  *     1: 6710's method (default)
90  *     2: 6729's method
91  */
92 int pcic_pd6722_vsense = 1;
93 SYSCTL_INT(_hw_pcic, OID_AUTO, pd6722_vsense, CTLFLAG_RDTUN,
94     &pcic_pd6722_vsense, 1,
95     "Select CL-PD6722's VSENSE method.  VSENSE is used to determine the"
96     " voltage of inserted cards.  The CL-PD6722 has two methods to determine"
97     " the voltage of the card.  0 means assume a 5.0V card and do not check.  1"
98     " means use the same method that the CL-PD6710 uses (default).  2 means use"
99     " the same method as the CL-PD6729.  2 is documented in the datasheet as"
100     " being the correct way, but 1 seems to give better results on more"
101     " laptops.");
102 /*****************************************************************************
103  * End of configurable parameters.
104  *****************************************************************************/
105 
106 #define	DPRINTF(x) do { if (cbb_debug) printf x; } while (0)
107 #define	DEVPRINTF(x) do { if (cbb_debug) device_printf x; } while (0)
108 
109 static struct isa_pnp_id pcic_ids[] = {
110 	{EXCA_PNP_ACTIONTEC,		NULL},		/* AEI0218 */
111 	{EXCA_PNP_IBM3765,		NULL},		/* IBM3765 */
112 	{EXCA_PNP_82365,		NULL},		/* PNP0E00 */
113 	{EXCA_PNP_CL_PD6720,		NULL},		/* PNP0E01 */
114 	{EXCA_PNP_VLSI_82C146,		NULL},		/* PNP0E02 */
115 	{EXCA_PNP_82365_CARDBUS,	NULL},		/* PNP0E03 */
116 	{EXCA_PNP_SCM_SWAPBOX,		NULL},		/* SCM0469 */
117 	{0}
118 };
119 
120 /************************************************************************/
121 /* Probe/Attach								*/
122 /************************************************************************/
123 
124 static int
cbb_isa_activate(device_t dev)125 cbb_isa_activate(device_t dev)
126 {
127 	struct cbb_softc *sc = device_get_softc(dev);
128 	struct resource *res;
129 	int rid;
130 	int i;
131 
132 	/* A little bogus, but go ahead and get the irq for CSC events */
133 	rid = 0;
134 	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
135 	if (res == NULL) {
136 		/*
137 		 * No IRQ specified, find one.  This can be due to the PnP
138 		 * data not specifying any IRQ, or the default kernel not
139 		 * assinging an IRQ.
140 		 */
141 		for (i = 0; i < 16 && res == NULL; i++) {
142 			if (((1 << i) & isa_intr_mask) == 0)
143 				continue;
144 			res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, i, i,
145 			    1, RF_ACTIVE);
146 		}
147 	}
148 	if (res == NULL)
149 		return (ENXIO);
150 	sc->irq_res = res;
151 	rid = 0;
152 	res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
153 	if (res == NULL) {
154 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
155 		sc->irq_res = NULL;
156 		device_printf(dev, "Cannot allocate I/O\n");
157 		return (ENOMEM);
158 	}
159 	sc->bst = rman_get_bustag(res);
160 	sc->bsh = rman_get_bushandle(res);
161 	sc->base_res = res;
162 	return (0);
163 }
164 
165 static void
cbb_isa_deactivate(device_t dev)166 cbb_isa_deactivate(device_t dev)
167 {
168 	struct cbb_softc *sc = device_get_softc(dev);
169 
170 	if (sc->irq_res)
171 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
172 	sc->irq_res = NULL;
173 	if (sc->base_res)
174 		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->base_res);
175 	sc->base_res = NULL;
176 }
177 
178 static int
cbb_isa_probe(device_t dev)179 cbb_isa_probe(device_t dev)
180 {
181 	int error;
182 	struct cbb_softc *sc = device_get_softc(dev);
183 
184 	/* Check isapnp ids */
185 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, pcic_ids);
186 	if (error != 0 && error != ENOENT)
187 		return (error);
188 
189 	error = cbb_isa_activate(dev);
190 	if (error != 0)
191 		return (error);
192 
193 	/* Check to make sure that we have actual hardware */
194 	error = exca_probe_slots(dev, &sc->exca[0], sc->bst, sc->bsh);
195 	cbb_isa_deactivate(dev);
196 	return (error);
197 }
198 
199 static int
cbb_isa_attach(device_t dev)200 cbb_isa_attach(device_t dev)
201 {
202 	return (ENOMEM);
203 }
204 
205 static int
cbb_isa_suspend(device_t dev)206 cbb_isa_suspend(device_t dev)
207 {
208 	return (0);
209 }
210 
211 static int
cbb_isa_resume(device_t dev)212 cbb_isa_resume(device_t dev)
213 {
214 	return (0);
215 }
216 
217 static device_method_t cbb_methods[] = {
218 	/* Device interface */
219 	DEVMETHOD(device_probe,			cbb_isa_probe),
220 	DEVMETHOD(device_attach,		cbb_isa_attach),
221 	DEVMETHOD(device_detach,		cbb_detach),
222 	DEVMETHOD(device_suspend,		cbb_isa_suspend),
223 	DEVMETHOD(device_resume,		cbb_isa_resume),
224 
225 	/* bus methods */
226 	DEVMETHOD(bus_read_ivar,		cbb_read_ivar),
227 	DEVMETHOD(bus_write_ivar,		cbb_write_ivar),
228 	DEVMETHOD(bus_alloc_resource,		cbb_alloc_resource),
229 	DEVMETHOD(bus_release_resource,		cbb_release_resource),
230 	DEVMETHOD(bus_activate_resource,	cbb_activate_resource),
231 	DEVMETHOD(bus_deactivate_resource,	cbb_deactivate_resource),
232 	DEVMETHOD(bus_driver_added,		cbb_driver_added),
233 	DEVMETHOD(bus_child_detached,		cbb_child_detached),
234 	DEVMETHOD(bus_setup_intr,		cbb_setup_intr),
235 	DEVMETHOD(bus_teardown_intr,		cbb_teardown_intr),
236 	DEVMETHOD(bus_child_present,		cbb_child_present),
237 
238 	/* 16-bit card interface */
239 	DEVMETHOD(card_set_res_flags,		cbb_pcic_set_res_flags),
240 	DEVMETHOD(card_set_memory_offset,	cbb_pcic_set_memory_offset),
241 
242 	/* power interface */
243 	DEVMETHOD(power_enable_socket,		cbb_power_enable_socket),
244 	DEVMETHOD(power_disable_socket,		cbb_power_disable_socket),
245 
246 	DEVMETHOD_END
247 };
248 
249 static driver_t cbb_isa_driver = {
250 	"cbb",
251 	cbb_methods,
252 	sizeof(struct cbb_softc)
253 };
254 
255 DRIVER_MODULE(cbb, isa, cbb_isa_driver, cbb_devclass, 0, 0);
256 MODULE_DEPEND(cbb, exca, 1, 1, 1);
257 ISA_PNP_INFO(pcic_ids);
258