]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/sdhci/sdhci_pci.c
MFC: r270885, r270948
[FreeBSD/stable/10.git] / sys / dev / sdhci / sdhci_pci.c
1 /*-
2  * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/conf.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/resource.h>
38 #include <sys/rman.h>
39 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
41
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
44
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 #include <machine/stdarg.h>
48
49 #include <dev/mmc/bridge.h>
50 #include <dev/mmc/mmcreg.h>
51 #include <dev/mmc/mmcbrvar.h>
52
53 #include "sdhci.h"
54 #include "mmcbr_if.h"
55 #include "sdhci_if.h"
56
57 /*
58  * PCI registers
59  */
60
61 #define PCI_SDHCI_IFPIO                 0x00
62 #define PCI_SDHCI_IFDMA                 0x01
63 #define PCI_SDHCI_IFVENDOR              0x02
64
65 #define PCI_SLOT_INFO                   0x40    /* 8 bits */
66 #define  PCI_SLOT_INFO_SLOTS(x)         (((x >> 4) & 7) + 1)
67 #define  PCI_SLOT_INFO_FIRST_BAR(x)     ((x) & 7)
68
69 /*
70  * RICOH specific PCI registers
71  */
72 #define SDHC_PCI_MODE_KEY               0xf9
73 #define SDHC_PCI_MODE                   0x150
74 #define SDHC_PCI_MODE_SD20              0x10
75 #define SDHC_PCI_BASE_FREQ_KEY          0xfc
76 #define SDHC_PCI_BASE_FREQ              0xe1
77
78 static const struct sdhci_device {
79         uint32_t        model;
80         uint16_t        subvendor;
81         const char      *desc;
82         u_int           quirks;
83 } sdhci_devices[] = {
84         { 0x08221180,   0xffff, "RICOH R5C822 SD",
85             SDHCI_QUIRK_FORCE_DMA },
86         { 0xe8221180,   0xffff, "RICOH SD",
87             SDHCI_QUIRK_FORCE_DMA },
88         { 0xe8231180,   0xffff, "RICOH R5CE823 SD",
89             SDHCI_QUIRK_LOWER_FREQUENCY },
90         { 0x8034104c,   0xffff, "TI XX21/XX11 SD",
91             SDHCI_QUIRK_FORCE_DMA },
92         { 0x05501524,   0xffff, "ENE CB712 SD",
93             SDHCI_QUIRK_BROKEN_TIMINGS },
94         { 0x05511524,   0xffff, "ENE CB712 SD 2",
95             SDHCI_QUIRK_BROKEN_TIMINGS },
96         { 0x07501524,   0xffff, "ENE CB714 SD",
97             SDHCI_QUIRK_RESET_ON_IOS |
98             SDHCI_QUIRK_BROKEN_TIMINGS },
99         { 0x07511524,   0xffff, "ENE CB714 SD 2",
100             SDHCI_QUIRK_RESET_ON_IOS |
101             SDHCI_QUIRK_BROKEN_TIMINGS },
102         { 0x410111ab,   0xffff, "Marvell CaFe SD",
103             SDHCI_QUIRK_INCR_TIMEOUT_CONTROL },
104         { 0x2381197B,   0xffff, "JMicron JMB38X SD",
105             SDHCI_QUIRK_32BIT_DMA_SIZE |
106             SDHCI_QUIRK_RESET_AFTER_REQUEST },
107         { 0,            0xffff, NULL,
108             0 }
109 };
110
111 struct sdhci_pci_softc {
112         device_t        dev;            /* Controller device */
113         u_int           quirks;         /* Chip specific quirks */
114         struct resource *irq_res;       /* IRQ resource */
115         void            *intrhand;      /* Interrupt handle */
116
117         int             num_slots;      /* Number of slots on this controller */
118         struct sdhci_slot slots[6];
119         struct resource *mem_res[6];    /* Memory resource */
120 };
121
122 static int sdhci_enable_msi = 1;
123 TUNABLE_INT("hw.sdhci.enable_msi", &sdhci_enable_msi);
124 SYSCTL_INT(_hw_sdhci, OID_AUTO, enable_msi, CTLFLAG_RDTUN, &sdhci_enable_msi,
125     0, "Enable MSI interrupts");
126
127 static uint8_t
128 sdhci_pci_read_1(device_t dev, struct sdhci_slot *slot, bus_size_t off)
129 {
130         struct sdhci_pci_softc *sc = device_get_softc(dev);
131
132         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
133             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
134         return bus_read_1(sc->mem_res[slot->num], off);
135 }
136
137 static void
138 sdhci_pci_write_1(device_t dev, struct sdhci_slot *slot, bus_size_t off, uint8_t val)
139 {
140         struct sdhci_pci_softc *sc = device_get_softc(dev);
141
142         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
143             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
144         bus_write_1(sc->mem_res[slot->num], off, val);
145 }
146
147 static uint16_t
148 sdhci_pci_read_2(device_t dev, struct sdhci_slot *slot, bus_size_t off)
149 {
150         struct sdhci_pci_softc *sc = device_get_softc(dev);
151
152         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
153             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
154         return bus_read_2(sc->mem_res[slot->num], off);
155 }
156
157 static void
158 sdhci_pci_write_2(device_t dev, struct sdhci_slot *slot, bus_size_t off, uint16_t val)
159 {
160         struct sdhci_pci_softc *sc = device_get_softc(dev);
161
162         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
163             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
164         bus_write_2(sc->mem_res[slot->num], off, val);
165 }
166
167 static uint32_t
168 sdhci_pci_read_4(device_t dev, struct sdhci_slot *slot, bus_size_t off)
169 {
170         struct sdhci_pci_softc *sc = device_get_softc(dev);
171
172         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
173             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
174         return bus_read_4(sc->mem_res[slot->num], off);
175 }
176
177 static void
178 sdhci_pci_write_4(device_t dev, struct sdhci_slot *slot, bus_size_t off, uint32_t val)
179 {
180         struct sdhci_pci_softc *sc = device_get_softc(dev);
181
182         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
183             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
184         bus_write_4(sc->mem_res[slot->num], off, val);
185 }
186
187 static void
188 sdhci_pci_read_multi_4(device_t dev, struct sdhci_slot *slot,
189     bus_size_t off, uint32_t *data, bus_size_t count)
190 {
191         struct sdhci_pci_softc *sc = device_get_softc(dev);
192
193         bus_read_multi_stream_4(sc->mem_res[slot->num], off, data, count);
194 }
195
196 static void
197 sdhci_pci_write_multi_4(device_t dev, struct sdhci_slot *slot,
198     bus_size_t off, uint32_t *data, bus_size_t count)
199 {
200         struct sdhci_pci_softc *sc = device_get_softc(dev);
201
202         bus_write_multi_stream_4(sc->mem_res[slot->num], off, data, count);
203 }
204
205 static void sdhci_pci_intr(void *arg);
206
207 static void
208 sdhci_lower_frequency(device_t dev)
209 {
210
211         /* Enable SD2.0 mode. */
212         pci_write_config(dev, SDHC_PCI_MODE_KEY, 0xfc, 1);
213         pci_write_config(dev, SDHC_PCI_MODE, SDHC_PCI_MODE_SD20, 1);
214         pci_write_config(dev, SDHC_PCI_MODE_KEY, 0x00, 1);
215
216         /*
217          * Some SD/MMC cards don't work with the default base
218          * clock frequency of 200MHz.  Lower it to 50Hz.
219          */
220         pci_write_config(dev, SDHC_PCI_BASE_FREQ_KEY, 0x01, 1);
221         pci_write_config(dev, SDHC_PCI_BASE_FREQ, 50, 1);
222         pci_write_config(dev, SDHC_PCI_BASE_FREQ_KEY, 0x00, 1);
223 }
224
225 static int
226 sdhci_pci_probe(device_t dev)
227 {
228         uint32_t model;
229         uint16_t subvendor;
230         uint8_t class, subclass;
231         int i, result;
232
233         model = (uint32_t)pci_get_device(dev) << 16;
234         model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
235         subvendor = pci_get_subvendor(dev);
236         class = pci_get_class(dev);
237         subclass = pci_get_subclass(dev);
238
239         result = ENXIO;
240         for (i = 0; sdhci_devices[i].model != 0; i++) {
241                 if (sdhci_devices[i].model == model &&
242                     (sdhci_devices[i].subvendor == 0xffff ||
243                     sdhci_devices[i].subvendor == subvendor)) {
244                         device_set_desc(dev, sdhci_devices[i].desc);
245                         result = BUS_PROBE_DEFAULT;
246                         break;
247                 }
248         }
249         if (result == ENXIO && class == PCIC_BASEPERIPH &&
250             subclass == PCIS_BASEPERIPH_SDHC) {
251                 device_set_desc(dev, "Generic SD HCI");
252                 result = BUS_PROBE_GENERIC;
253         }
254
255         return (result);
256 }
257
258 static int
259 sdhci_pci_attach(device_t dev)
260 {
261         struct sdhci_pci_softc *sc = device_get_softc(dev);
262         uint32_t model;
263         uint16_t subvendor;
264         uint8_t class, subclass, progif;
265         int bar, err, rid, slots, i;
266
267         sc->dev = dev;
268         model = (uint32_t)pci_get_device(dev) << 16;
269         model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
270         subvendor = pci_get_subvendor(dev);
271         class = pci_get_class(dev);
272         subclass = pci_get_subclass(dev);
273         progif = pci_get_progif(dev);
274         /* Apply chip specific quirks. */
275         for (i = 0; sdhci_devices[i].model != 0; i++) {
276                 if (sdhci_devices[i].model == model &&
277                     (sdhci_devices[i].subvendor == 0xffff ||
278                     sdhci_devices[i].subvendor == subvendor)) {
279                         sc->quirks = sdhci_devices[i].quirks;
280                         break;
281                 }
282         }
283         /* Some controllers need to be bumped into the right mode. */
284         if (sc->quirks & SDHCI_QUIRK_LOWER_FREQUENCY)
285                 sdhci_lower_frequency(dev);
286         /* Read slots info from PCI registers. */
287         slots = pci_read_config(dev, PCI_SLOT_INFO, 1);
288         bar = PCI_SLOT_INFO_FIRST_BAR(slots);
289         slots = PCI_SLOT_INFO_SLOTS(slots);
290         if (slots > 6 || bar > 5) {
291                 device_printf(dev, "Incorrect slots information (%d, %d).\n",
292                     slots, bar);
293                 return (EINVAL);
294         }
295         /* Allocate IRQ. */
296         i = 1;
297         rid = 0;
298         if (sdhci_enable_msi != 0 && pci_alloc_msi(dev, &i) == 0)
299                 rid = 1;
300         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
301                 RF_ACTIVE | (rid != 0 ? 0 : RF_SHAREABLE));
302         if (sc->irq_res == NULL) {
303                 device_printf(dev, "Can't allocate IRQ\n");
304                 pci_release_msi(dev);
305                 return (ENOMEM);
306         }
307         /* Scan all slots. */
308         for (i = 0; i < slots; i++) {
309                 struct sdhci_slot *slot = &sc->slots[sc->num_slots];
310
311                 /* Allocate memory. */
312                 rid = PCIR_BAR(bar + i);
313                 sc->mem_res[i] = bus_alloc_resource(dev, SYS_RES_MEMORY,
314                     &rid, 0ul, ~0ul, 0x100, RF_ACTIVE);
315                 if (sc->mem_res[i] == NULL) {
316                         device_printf(dev, "Can't allocate memory for slot %d\n", i);
317                         continue;
318                 }
319
320                 if (sdhci_init_slot(dev, slot, i) != 0)
321                         continue;
322
323                 sc->num_slots++;
324         }
325         device_printf(dev, "%d slot(s) allocated\n", sc->num_slots);
326         /* Activate the interrupt */
327         err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
328             NULL, sdhci_pci_intr, sc, &sc->intrhand);
329         if (err)
330                 device_printf(dev, "Can't setup IRQ\n");
331         pci_enable_busmaster(dev);
332         /* Process cards detection. */
333         for (i = 0; i < sc->num_slots; i++) {
334                 struct sdhci_slot *slot = &sc->slots[i];
335
336                 sdhci_start_slot(slot);
337         }
338
339         return (0);
340 }
341
342 static int
343 sdhci_pci_detach(device_t dev)
344 {
345         struct sdhci_pci_softc *sc = device_get_softc(dev);
346         int i;
347
348         bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
349         bus_release_resource(dev, SYS_RES_IRQ,
350             rman_get_rid(sc->irq_res), sc->irq_res);
351         pci_release_msi(dev);
352
353         for (i = 0; i < sc->num_slots; i++) {
354                 struct sdhci_slot *slot = &sc->slots[i];
355
356                 sdhci_cleanup_slot(slot);
357                 bus_release_resource(dev, SYS_RES_MEMORY,
358                     rman_get_rid(sc->mem_res[i]), sc->mem_res[i]);
359         }
360         return (0);
361 }
362
363 static int
364 sdhci_pci_suspend(device_t dev)
365 {
366         struct sdhci_pci_softc *sc = device_get_softc(dev);
367         int i, err;
368
369         err = bus_generic_suspend(dev);
370         if (err)
371                 return (err);
372         for (i = 0; i < sc->num_slots; i++)
373                 sdhci_generic_suspend(&sc->slots[i]);
374         return (0);
375 }
376
377 static int
378 sdhci_pci_resume(device_t dev)
379 {
380         struct sdhci_pci_softc *sc = device_get_softc(dev);
381         int i;
382
383         for (i = 0; i < sc->num_slots; i++)
384                 sdhci_generic_resume(&sc->slots[i]);
385         return (bus_generic_resume(dev));
386 }
387
388 static void
389 sdhci_pci_intr(void *arg)
390 {
391         struct sdhci_pci_softc *sc = (struct sdhci_pci_softc *)arg;
392         int i;
393
394         for (i = 0; i < sc->num_slots; i++) {
395                 struct sdhci_slot *slot = &sc->slots[i];
396                 sdhci_generic_intr(slot);
397         }
398 }
399
400 static device_method_t sdhci_methods[] = {
401         /* device_if */
402         DEVMETHOD(device_probe, sdhci_pci_probe),
403         DEVMETHOD(device_attach, sdhci_pci_attach),
404         DEVMETHOD(device_detach, sdhci_pci_detach),
405         DEVMETHOD(device_suspend, sdhci_pci_suspend),
406         DEVMETHOD(device_resume, sdhci_pci_resume),
407
408         /* Bus interface */
409         DEVMETHOD(bus_read_ivar,        sdhci_generic_read_ivar),
410         DEVMETHOD(bus_write_ivar,       sdhci_generic_write_ivar),
411
412         /* mmcbr_if */
413         DEVMETHOD(mmcbr_update_ios, sdhci_generic_update_ios),
414         DEVMETHOD(mmcbr_request, sdhci_generic_request),
415         DEVMETHOD(mmcbr_get_ro, sdhci_generic_get_ro),
416         DEVMETHOD(mmcbr_acquire_host, sdhci_generic_acquire_host),
417         DEVMETHOD(mmcbr_release_host, sdhci_generic_release_host),
418
419         /* SDHCI registers accessors */
420         DEVMETHOD(sdhci_read_1,         sdhci_pci_read_1),
421         DEVMETHOD(sdhci_read_2,         sdhci_pci_read_2),
422         DEVMETHOD(sdhci_read_4,         sdhci_pci_read_4),
423         DEVMETHOD(sdhci_read_multi_4,   sdhci_pci_read_multi_4),
424         DEVMETHOD(sdhci_write_1,        sdhci_pci_write_1),
425         DEVMETHOD(sdhci_write_2,        sdhci_pci_write_2),
426         DEVMETHOD(sdhci_write_4,        sdhci_pci_write_4),
427         DEVMETHOD(sdhci_write_multi_4,  sdhci_pci_write_multi_4),
428
429         DEVMETHOD_END
430 };
431
432 static driver_t sdhci_pci_driver = {
433         "sdhci_pci",
434         sdhci_methods,
435         sizeof(struct sdhci_pci_softc),
436 };
437 static devclass_t sdhci_pci_devclass;
438
439 DRIVER_MODULE(sdhci_pci, pci, sdhci_pci_driver, sdhci_pci_devclass, NULL,
440     NULL);
441 MODULE_DEPEND(sdhci_pci, sdhci, 1, 1, 1);