]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/sdhci/sdhci_pci.c
MFC: r273180, r283754, r297329, r299414, r300707, r310309, r310340 (partial),
[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 R5CE822 SD",
87             SDHCI_QUIRK_FORCE_DMA |
88             SDHCI_QUIRK_LOWER_FREQUENCY },
89         { 0xe8231180,   0xffff, "RICOH R5CE823 SD",
90             SDHCI_QUIRK_LOWER_FREQUENCY },
91         { 0x8034104c,   0xffff, "TI XX21/XX11 SD",
92             SDHCI_QUIRK_FORCE_DMA },
93         { 0x05501524,   0xffff, "ENE CB712 SD",
94             SDHCI_QUIRK_BROKEN_TIMINGS },
95         { 0x05511524,   0xffff, "ENE CB712 SD 2",
96             SDHCI_QUIRK_BROKEN_TIMINGS },
97         { 0x07501524,   0xffff, "ENE CB714 SD",
98             SDHCI_QUIRK_RESET_ON_IOS |
99             SDHCI_QUIRK_BROKEN_TIMINGS },
100         { 0x07511524,   0xffff, "ENE CB714 SD 2",
101             SDHCI_QUIRK_RESET_ON_IOS |
102             SDHCI_QUIRK_BROKEN_TIMINGS },
103         { 0x410111ab,   0xffff, "Marvell CaFe SD",
104             SDHCI_QUIRK_INCR_TIMEOUT_CONTROL },
105         { 0x2381197B,   0xffff, "JMicron JMB38X SD",
106             SDHCI_QUIRK_32BIT_DMA_SIZE |
107             SDHCI_QUIRK_RESET_AFTER_REQUEST },
108         { 0x16bc14e4,   0xffff, "Broadcom BCM577xx SDXC/MMC Card Reader",
109             SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC },
110         { 0x0f148086,   0xffff, "Intel Bay Trail eMMC 4.5 Controller",
111             SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE |
112             SDHCI_QUIRK_INTEL_POWER_UP_RESET },
113         { 0x0f508086,   0xffff, "Intel Bay Trail eMMC 4.5 Controller",
114             SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE |
115             SDHCI_QUIRK_INTEL_POWER_UP_RESET },
116         { 0x22948086,   0xffff, "Intel Braswell eMMC 4.5.1 Controller",
117             SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE |
118             SDHCI_QUIRK_DATA_TIMEOUT_1MHZ |
119             SDHCI_QUIRK_INTEL_POWER_UP_RESET },
120         { 0x5acc8086,   0xffff, "Intel Apollo Lake eMMC 5.0 Controller",
121             SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE |
122             SDHCI_QUIRK_INTEL_POWER_UP_RESET },
123         { 0,            0xffff, NULL,
124             0 }
125 };
126
127 struct sdhci_pci_softc {
128         u_int           quirks;         /* Chip specific quirks */
129         struct resource *irq_res;       /* IRQ resource */
130         void            *intrhand;      /* Interrupt handle */
131
132         int             num_slots;      /* Number of slots on this controller */
133         struct sdhci_slot slots[6];
134         struct resource *mem_res[6];    /* Memory resource */
135         uint8_t         cfg_freq;       /* Saved frequency */
136         uint8_t         cfg_mode;       /* Saved mode */
137 };
138
139 static int sdhci_enable_msi = 1;
140 TUNABLE_INT("hw.sdhci.enable_msi", &sdhci_enable_msi);
141 SYSCTL_INT(_hw_sdhci, OID_AUTO, enable_msi, CTLFLAG_RDTUN, &sdhci_enable_msi,
142     0, "Enable MSI interrupts");
143
144 static uint8_t
145 sdhci_pci_read_1(device_t dev, struct sdhci_slot *slot, bus_size_t off)
146 {
147         struct sdhci_pci_softc *sc = device_get_softc(dev);
148
149         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
150             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
151         return bus_read_1(sc->mem_res[slot->num], off);
152 }
153
154 static void
155 sdhci_pci_write_1(device_t dev, struct sdhci_slot *slot, bus_size_t off, uint8_t val)
156 {
157         struct sdhci_pci_softc *sc = device_get_softc(dev);
158
159         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
160             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
161         bus_write_1(sc->mem_res[slot->num], off, val);
162 }
163
164 static uint16_t
165 sdhci_pci_read_2(device_t dev, struct sdhci_slot *slot, bus_size_t off)
166 {
167         struct sdhci_pci_softc *sc = device_get_softc(dev);
168
169         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
170             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
171         return bus_read_2(sc->mem_res[slot->num], off);
172 }
173
174 static void
175 sdhci_pci_write_2(device_t dev, struct sdhci_slot *slot, bus_size_t off, uint16_t val)
176 {
177         struct sdhci_pci_softc *sc = device_get_softc(dev);
178
179         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
180             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
181         bus_write_2(sc->mem_res[slot->num], off, val);
182 }
183
184 static uint32_t
185 sdhci_pci_read_4(device_t dev, struct sdhci_slot *slot, bus_size_t off)
186 {
187         struct sdhci_pci_softc *sc = device_get_softc(dev);
188
189         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
190             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
191         return bus_read_4(sc->mem_res[slot->num], off);
192 }
193
194 static void
195 sdhci_pci_write_4(device_t dev, struct sdhci_slot *slot, bus_size_t off, uint32_t val)
196 {
197         struct sdhci_pci_softc *sc = device_get_softc(dev);
198
199         bus_barrier(sc->mem_res[slot->num], 0, 0xFF,
200             BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
201         bus_write_4(sc->mem_res[slot->num], off, val);
202 }
203
204 static void
205 sdhci_pci_read_multi_4(device_t dev, struct sdhci_slot *slot,
206     bus_size_t off, uint32_t *data, bus_size_t count)
207 {
208         struct sdhci_pci_softc *sc = device_get_softc(dev);
209
210         bus_read_multi_stream_4(sc->mem_res[slot->num], off, data, count);
211 }
212
213 static void
214 sdhci_pci_write_multi_4(device_t dev, struct sdhci_slot *slot,
215     bus_size_t off, uint32_t *data, bus_size_t count)
216 {
217         struct sdhci_pci_softc *sc = device_get_softc(dev);
218
219         bus_write_multi_stream_4(sc->mem_res[slot->num], off, data, count);
220 }
221
222 static void sdhci_pci_intr(void *arg);
223
224 static void
225 sdhci_lower_frequency(device_t dev)
226 {
227         struct sdhci_pci_softc *sc = device_get_softc(dev);
228
229         /*
230          * Enable SD2.0 mode.
231          * NB: for RICOH R5CE823, this changes the PCI device ID to 0xe822.
232          */
233         pci_write_config(dev, SDHC_PCI_MODE_KEY, 0xfc, 1);
234         sc->cfg_mode = pci_read_config(dev, SDHC_PCI_MODE, 1);
235         pci_write_config(dev, SDHC_PCI_MODE, SDHC_PCI_MODE_SD20, 1);
236         pci_write_config(dev, SDHC_PCI_MODE_KEY, 0x00, 1);
237
238         /*
239          * Some SD/MMC cards don't work with the default base
240          * clock frequency of 200 MHz.  Lower it to 50 MHz.
241          */
242         pci_write_config(dev, SDHC_PCI_BASE_FREQ_KEY, 0x01, 1);
243         sc->cfg_freq = pci_read_config(dev, SDHC_PCI_BASE_FREQ, 1);
244         pci_write_config(dev, SDHC_PCI_BASE_FREQ, 50, 1);
245         pci_write_config(dev, SDHC_PCI_BASE_FREQ_KEY, 0x00, 1);
246 }
247
248 static void
249 sdhci_restore_frequency(device_t dev)
250 {
251         struct sdhci_pci_softc *sc = device_get_softc(dev);
252
253         /* Restore mode. */
254         pci_write_config(dev, SDHC_PCI_MODE_KEY, 0xfc, 1);
255         pci_write_config(dev, SDHC_PCI_MODE, sc->cfg_mode, 1);
256         pci_write_config(dev, SDHC_PCI_MODE_KEY, 0x00, 1);
257
258         /* Restore frequency. */
259         pci_write_config(dev, SDHC_PCI_BASE_FREQ_KEY, 0x01, 1);
260         pci_write_config(dev, SDHC_PCI_BASE_FREQ, sc->cfg_freq, 1);
261         pci_write_config(dev, SDHC_PCI_BASE_FREQ_KEY, 0x00, 1);
262 }
263
264 static int
265 sdhci_pci_probe(device_t dev)
266 {
267         uint32_t model;
268         uint16_t subvendor;
269         uint8_t class, subclass;
270         int i, result;
271
272         model = (uint32_t)pci_get_device(dev) << 16;
273         model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
274         subvendor = pci_get_subvendor(dev);
275         class = pci_get_class(dev);
276         subclass = pci_get_subclass(dev);
277
278         result = ENXIO;
279         for (i = 0; sdhci_devices[i].model != 0; i++) {
280                 if (sdhci_devices[i].model == model &&
281                     (sdhci_devices[i].subvendor == 0xffff ||
282                     sdhci_devices[i].subvendor == subvendor)) {
283                         device_set_desc(dev, sdhci_devices[i].desc);
284                         result = BUS_PROBE_DEFAULT;
285                         break;
286                 }
287         }
288         if (result == ENXIO && class == PCIC_BASEPERIPH &&
289             subclass == PCIS_BASEPERIPH_SDHC) {
290                 device_set_desc(dev, "Generic SD HCI");
291                 result = BUS_PROBE_GENERIC;
292         }
293
294         return (result);
295 }
296
297 static int
298 sdhci_pci_attach(device_t dev)
299 {
300         struct sdhci_pci_softc *sc = device_get_softc(dev);
301         uint32_t model;
302         uint16_t subvendor;
303         int bar, err, rid, slots, i;
304
305         model = (uint32_t)pci_get_device(dev) << 16;
306         model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
307         subvendor = pci_get_subvendor(dev);
308         /* Apply chip specific quirks. */
309         for (i = 0; sdhci_devices[i].model != 0; i++) {
310                 if (sdhci_devices[i].model == model &&
311                     (sdhci_devices[i].subvendor == 0xffff ||
312                     sdhci_devices[i].subvendor == subvendor)) {
313                         sc->quirks = sdhci_devices[i].quirks;
314                         break;
315                 }
316         }
317         /* Some controllers need to be bumped into the right mode. */
318         if (sc->quirks & SDHCI_QUIRK_LOWER_FREQUENCY)
319                 sdhci_lower_frequency(dev);
320         /* Read slots info from PCI registers. */
321         slots = pci_read_config(dev, PCI_SLOT_INFO, 1);
322         bar = PCI_SLOT_INFO_FIRST_BAR(slots);
323         slots = PCI_SLOT_INFO_SLOTS(slots);
324         if (slots > 6 || bar > 5) {
325                 device_printf(dev, "Incorrect slots information (%d, %d).\n",
326                     slots, bar);
327                 return (EINVAL);
328         }
329         /* Allocate IRQ. */
330         i = 1;
331         rid = 0;
332         if (sdhci_enable_msi != 0 && pci_alloc_msi(dev, &i) == 0)
333                 rid = 1;
334         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
335                 RF_ACTIVE | (rid != 0 ? 0 : RF_SHAREABLE));
336         if (sc->irq_res == NULL) {
337                 device_printf(dev, "Can't allocate IRQ\n");
338                 pci_release_msi(dev);
339                 return (ENOMEM);
340         }
341         /* Scan all slots. */
342         for (i = 0; i < slots; i++) {
343                 struct sdhci_slot *slot = &sc->slots[sc->num_slots];
344
345                 /* Allocate memory. */
346                 rid = PCIR_BAR(bar + i);
347                 sc->mem_res[i] = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
348                     &rid, RF_ACTIVE);
349                 if (sc->mem_res[i] == NULL) {
350                         device_printf(dev, "Can't allocate memory for slot %d\n", i);
351                         continue;
352                 }
353                 
354                 slot->quirks = sc->quirks;
355
356                 if (sdhci_init_slot(dev, slot, i) != 0)
357                         continue;
358
359                 sc->num_slots++;
360         }
361         device_printf(dev, "%d slot(s) allocated\n", sc->num_slots);
362         /* Activate the interrupt */
363         err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
364             NULL, sdhci_pci_intr, sc, &sc->intrhand);
365         if (err)
366                 device_printf(dev, "Can't setup IRQ\n");
367         pci_enable_busmaster(dev);
368         /* Process cards detection. */
369         for (i = 0; i < sc->num_slots; i++) {
370                 struct sdhci_slot *slot = &sc->slots[i];
371
372                 sdhci_start_slot(slot);
373         }
374
375         return (0);
376 }
377
378 static int
379 sdhci_pci_detach(device_t dev)
380 {
381         struct sdhci_pci_softc *sc = device_get_softc(dev);
382         int i;
383
384         bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
385         bus_release_resource(dev, SYS_RES_IRQ,
386             rman_get_rid(sc->irq_res), sc->irq_res);
387         pci_release_msi(dev);
388
389         for (i = 0; i < sc->num_slots; i++) {
390                 struct sdhci_slot *slot = &sc->slots[i];
391
392                 sdhci_cleanup_slot(slot);
393                 bus_release_resource(dev, SYS_RES_MEMORY,
394                     rman_get_rid(sc->mem_res[i]), sc->mem_res[i]);
395         }
396         if (sc->quirks & SDHCI_QUIRK_LOWER_FREQUENCY)
397                 sdhci_restore_frequency(dev);
398         return (0);
399 }
400
401 static int
402 sdhci_pci_shutdown(device_t dev)
403 {
404         struct sdhci_pci_softc *sc = device_get_softc(dev);
405
406         if (sc->quirks & SDHCI_QUIRK_LOWER_FREQUENCY)
407                 sdhci_restore_frequency(dev);
408         return (0);
409 }
410
411 static int
412 sdhci_pci_suspend(device_t dev)
413 {
414         struct sdhci_pci_softc *sc = device_get_softc(dev);
415         int i, err;
416
417         err = bus_generic_suspend(dev);
418         if (err)
419                 return (err);
420         for (i = 0; i < sc->num_slots; i++)
421                 sdhci_generic_suspend(&sc->slots[i]);
422         return (0);
423 }
424
425 static int
426 sdhci_pci_resume(device_t dev)
427 {
428         struct sdhci_pci_softc *sc = device_get_softc(dev);
429         int i, err;
430
431         for (i = 0; i < sc->num_slots; i++)
432                 sdhci_generic_resume(&sc->slots[i]);
433         err = bus_generic_resume(dev);
434         if (err)
435                 return (err);
436         if (sc->quirks & SDHCI_QUIRK_LOWER_FREQUENCY)
437                 sdhci_lower_frequency(dev);
438         return (0);
439 }
440
441 static void
442 sdhci_pci_intr(void *arg)
443 {
444         struct sdhci_pci_softc *sc = (struct sdhci_pci_softc *)arg;
445         int i;
446
447         for (i = 0; i < sc->num_slots; i++) {
448                 struct sdhci_slot *slot = &sc->slots[i];
449                 sdhci_generic_intr(slot);
450         }
451 }
452
453 static device_method_t sdhci_methods[] = {
454         /* device_if */
455         DEVMETHOD(device_probe, sdhci_pci_probe),
456         DEVMETHOD(device_attach, sdhci_pci_attach),
457         DEVMETHOD(device_detach, sdhci_pci_detach),
458         DEVMETHOD(device_shutdown, sdhci_pci_shutdown),
459         DEVMETHOD(device_suspend, sdhci_pci_suspend),
460         DEVMETHOD(device_resume, sdhci_pci_resume),
461
462         /* Bus interface */
463         DEVMETHOD(bus_read_ivar,        sdhci_generic_read_ivar),
464         DEVMETHOD(bus_write_ivar,       sdhci_generic_write_ivar),
465
466         /* mmcbr_if */
467         DEVMETHOD(mmcbr_update_ios,     sdhci_generic_update_ios),
468         DEVMETHOD(mmcbr_request,        sdhci_generic_request),
469         DEVMETHOD(mmcbr_get_ro,         sdhci_generic_get_ro),
470         DEVMETHOD(mmcbr_acquire_host,   sdhci_generic_acquire_host),
471         DEVMETHOD(mmcbr_release_host,   sdhci_generic_release_host),
472
473         /* SDHCI registers accessors */
474         DEVMETHOD(sdhci_read_1,         sdhci_pci_read_1),
475         DEVMETHOD(sdhci_read_2,         sdhci_pci_read_2),
476         DEVMETHOD(sdhci_read_4,         sdhci_pci_read_4),
477         DEVMETHOD(sdhci_read_multi_4,   sdhci_pci_read_multi_4),
478         DEVMETHOD(sdhci_write_1,        sdhci_pci_write_1),
479         DEVMETHOD(sdhci_write_2,        sdhci_pci_write_2),
480         DEVMETHOD(sdhci_write_4,        sdhci_pci_write_4),
481         DEVMETHOD(sdhci_write_multi_4,  sdhci_pci_write_multi_4),
482
483         DEVMETHOD_END
484 };
485
486 static driver_t sdhci_pci_driver = {
487         "sdhci_pci",
488         sdhci_methods,
489         sizeof(struct sdhci_pci_softc),
490 };
491 static devclass_t sdhci_pci_devclass;
492
493 DRIVER_MODULE(sdhci_pci, pci, sdhci_pci_driver, sdhci_pci_devclass, NULL,
494     NULL);
495 MODULE_DEPEND(sdhci_pci, sdhci, 1, 1, 1);