slhci_pcmcia.c revision 1.6.4.1 1 1.6.4.1 rmind /* $NetBSD: slhci_pcmcia.c,v 1.6.4.1 2011/03/05 20:54:01 rmind Exp $ */
2 1.2 kiyohara /*
3 1.2 kiyohara * Not (c) 2007 Matthew Orgass
4 1.2 kiyohara * This file is public domain, meaning anyone can make any use of part or all
5 1.2 kiyohara * of this file including copying into other works without credit. Any use,
6 1.2 kiyohara * modified or not, is solely the responsibility of the user. If this file is
7 1.2 kiyohara * part of a collection then use in the collection is governed by the terms of
8 1.2 kiyohara * the collection.
9 1.2 kiyohara */
10 1.2 kiyohara
11 1.2 kiyohara /* Glue for RATOC USB HOST CF+ Card (SL811HS chip) */
12 1.2 kiyohara
13 1.2 kiyohara #include <sys/cdefs.h>
14 1.6.4.1 rmind __KERNEL_RCSID(0, "$NetBSD: slhci_pcmcia.c,v 1.6.4.1 2011/03/05 20:54:01 rmind Exp $");
15 1.2 kiyohara
16 1.2 kiyohara #include <sys/param.h>
17 1.2 kiyohara #include <sys/device.h>
18 1.2 kiyohara #include <sys/queue.h>
19 1.2 kiyohara #include <sys/gcq.h>
20 1.2 kiyohara #include <sys/systm.h>
21 1.2 kiyohara #include <sys/errno.h>
22 1.2 kiyohara
23 1.3 ad #include <sys/bus.h>
24 1.2 kiyohara #include <dev/pcmcia/pcmciareg.h>
25 1.2 kiyohara #include <dev/pcmcia/pcmciavar.h>
26 1.2 kiyohara #include <dev/pcmcia/pcmciadevs.h>
27 1.2 kiyohara
28 1.2 kiyohara #include <dev/usb/usb.h>
29 1.2 kiyohara #include <dev/usb/usbdi.h>
30 1.2 kiyohara #include <dev/usb/usbdivar.h>
31 1.2 kiyohara
32 1.2 kiyohara #include <dev/ic/sl811hsvar.h>
33 1.2 kiyohara
34 1.2 kiyohara struct slhci_pcmcia_softc {
35 1.2 kiyohara struct slhci_softc sc_slhci;
36 1.2 kiyohara
37 1.2 kiyohara struct pcmcia_function *sc_pf;
38 1.2 kiyohara void * sc_ih;
39 1.2 kiyohara int sc_flags;
40 1.2 kiyohara #define PFL_ENABLED (0x1)
41 1.2 kiyohara };
42 1.2 kiyohara
43 1.2 kiyohara
44 1.6 cegger int slhci_pcmcia_probe(device_t, cfdata_t, void *);
45 1.6 cegger void slhci_pcmcia_attach(device_t, device_t, void *);
46 1.6 cegger int slhci_pcmcia_detach(device_t, int);
47 1.2 kiyohara int slhci_pcmcia_validate_config(struct pcmcia_config_entry *);
48 1.2 kiyohara int slhci_pcmcia_enable(struct slhci_pcmcia_softc *, int);
49 1.2 kiyohara
50 1.4 drochner CFATTACH_DECL_NEW(slhci_pcmcia, sizeof(struct slhci_pcmcia_softc),
51 1.2 kiyohara slhci_pcmcia_probe, slhci_pcmcia_attach, slhci_pcmcia_detach,
52 1.2 kiyohara slhci_activate);
53 1.2 kiyohara
54 1.2 kiyohara /* Ratoc has two PCMCIA products with id 1.
55 1.2 kiyohara * Ratoc has a firmware update that modifies the CIS. The new CIS may
56 1.2 kiyohara * need a new entry. */
57 1.2 kiyohara static const struct pcmcia_product slhci_pcmcia_products[] = {
58 1.2 kiyohara { PCMCIA_VENDOR_RATOC, PCMCIA_PRODUCT_RATOC_REX_CFU1,
59 1.2 kiyohara PCMCIA_CIS_RATOC_REX_CFU1 },
60 1.2 kiyohara };
61 1.2 kiyohara static const size_t slhci_pcmcia_nproducts =
62 1.2 kiyohara sizeof(slhci_pcmcia_products) / sizeof(slhci_pcmcia_products[0]);
63 1.2 kiyohara
64 1.2 kiyohara int
65 1.6 cegger slhci_pcmcia_probe(device_t parent, cfdata_t match, void *aux)
66 1.2 kiyohara {
67 1.2 kiyohara struct pcmcia_attach_args *pa = aux;
68 1.2 kiyohara
69 1.2 kiyohara if (pcmcia_product_lookup(pa, slhci_pcmcia_products,
70 1.2 kiyohara slhci_pcmcia_nproducts, sizeof(slhci_pcmcia_products[0]), NULL))
71 1.2 kiyohara return 1;
72 1.2 kiyohara
73 1.2 kiyohara return 0;
74 1.2 kiyohara }
75 1.2 kiyohara
76 1.2 kiyohara int
77 1.2 kiyohara slhci_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
78 1.2 kiyohara {
79 1.2 kiyohara if (cfe->num_iospace != 1 || cfe->num_memspace != 0)
80 1.2 kiyohara return EINVAL;
81 1.2 kiyohara return 0;
82 1.2 kiyohara }
83 1.2 kiyohara
84 1.2 kiyohara void
85 1.6 cegger slhci_pcmcia_attach(device_t parent, device_t self, void *aux)
86 1.2 kiyohara {
87 1.4 drochner struct slhci_pcmcia_softc *psc = device_private(self);
88 1.2 kiyohara struct pcmcia_attach_args *pa = aux;
89 1.2 kiyohara struct pcmcia_function *pf = pa->pf;
90 1.2 kiyohara
91 1.4 drochner psc->sc_slhci.sc_dev = self;
92 1.4 drochner
93 1.2 kiyohara psc->sc_pf = pf;
94 1.2 kiyohara psc->sc_flags = 0;
95 1.2 kiyohara
96 1.2 kiyohara slhci_pcmcia_enable(psc, 1);
97 1.2 kiyohara
98 1.2 kiyohara return;
99 1.2 kiyohara }
100 1.2 kiyohara
101 1.2 kiyohara int
102 1.6 cegger slhci_pcmcia_detach(device_t self, int flags)
103 1.2 kiyohara {
104 1.4 drochner struct slhci_pcmcia_softc *psc = device_private(self);
105 1.2 kiyohara
106 1.2 kiyohara slhci_pcmcia_enable(psc, 0);
107 1.2 kiyohara
108 1.2 kiyohara return slhci_detach(&psc->sc_slhci, flags);
109 1.2 kiyohara }
110 1.2 kiyohara
111 1.2 kiyohara int
112 1.2 kiyohara slhci_pcmcia_enable(struct slhci_pcmcia_softc *psc, int enable)
113 1.2 kiyohara {
114 1.2 kiyohara struct pcmcia_function *pf;
115 1.2 kiyohara struct pcmcia_io_handle *pioh;
116 1.2 kiyohara struct slhci_softc *sc;
117 1.2 kiyohara int error;
118 1.2 kiyohara
119 1.2 kiyohara pf = psc->sc_pf;
120 1.2 kiyohara sc = &psc->sc_slhci;
121 1.2 kiyohara
122 1.2 kiyohara if (enable) {
123 1.2 kiyohara if (psc->sc_flags & PFL_ENABLED)
124 1.2 kiyohara return 0;
125 1.2 kiyohara
126 1.2 kiyohara error = pcmcia_function_configure(pf,
127 1.2 kiyohara slhci_pcmcia_validate_config);
128 1.2 kiyohara if (error) {
129 1.2 kiyohara printf("%s: configure failed, error=%d\n",
130 1.2 kiyohara SC_NAME(sc), error);
131 1.2 kiyohara return 1;
132 1.2 kiyohara }
133 1.2 kiyohara
134 1.2 kiyohara pioh = &pf->cfe->iospace[0].handle;
135 1.2 kiyohara
136 1.2 kiyohara /* The data port is repeated three times; using a stride of
137 1.2 kiyohara * 2 prevents read/write errors on a Clio C-1000 hpcmips
138 1.2 kiyohara * system.
139 1.2 kiyohara */
140 1.2 kiyohara slhci_preinit(sc, NULL, pioh->iot, pioh->ioh, 100, 2);
141 1.2 kiyohara
142 1.2 kiyohara psc->sc_ih = pcmcia_intr_establish(pf, IPL_HARDUSB,
143 1.2 kiyohara slhci_intr, sc);
144 1.2 kiyohara
145 1.2 kiyohara if (psc->sc_ih == NULL) {
146 1.2 kiyohara printf("%s: unable to establish interrupt\n",
147 1.2 kiyohara SC_NAME(sc));
148 1.2 kiyohara goto fail1;
149 1.2 kiyohara }
150 1.2 kiyohara
151 1.2 kiyohara if (pcmcia_function_enable(pf)) {
152 1.2 kiyohara printf("%s: function enable failed\n", SC_NAME(sc));
153 1.2 kiyohara goto fail2;
154 1.2 kiyohara }
155 1.2 kiyohara
156 1.2 kiyohara if (slhci_attach(sc)) {
157 1.2 kiyohara printf("%s: slhci_attach failed\n", SC_NAME(sc));
158 1.2 kiyohara goto fail3;
159 1.2 kiyohara }
160 1.2 kiyohara
161 1.2 kiyohara psc->sc_flags |= PFL_ENABLED;
162 1.2 kiyohara return 0;
163 1.2 kiyohara } else {
164 1.2 kiyohara if (!(psc->sc_flags & PFL_ENABLED))
165 1.2 kiyohara return 1;
166 1.2 kiyohara psc->sc_flags &= ~PFL_ENABLED;
167 1.2 kiyohara fail3:
168 1.2 kiyohara pcmcia_function_disable(pf);
169 1.2 kiyohara fail2:
170 1.2 kiyohara pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
171 1.2 kiyohara fail1:
172 1.2 kiyohara pcmcia_function_unconfigure(pf);
173 1.2 kiyohara
174 1.2 kiyohara return 1;
175 1.2 kiyohara }
176 1.2 kiyohara }
177 1.2 kiyohara
178 1.2 kiyohara
179