xref: /dragonfly/sys/dev/disk/sdhci/sdhci_pci.c (revision dbeb2594)
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/param.h>
27 #include <sys/systm.h>
28 #include <sys/bus.h>
29 #include <sys/conf.h>
30 #include <sys/kernel.h>
31 #include <sys/lock.h>
32 #include <sys/module.h>
33 #include <sys/mutex.h>
34 #include <sys/resource.h>
35 #include <sys/rman.h>
36 #include <sys/sysctl.h>
37 #include <sys/taskqueue.h>
38 #include <sys/lock.h>
39 
40 #include <bus/pci/pcireg.h>
41 #include <bus/pci/pcivar.h>
42 
43 #include <sys/bus.h>
44 #include <sys/resource.h>
45 #include <machine/stdarg.h>
46 
47 #include <bus/mmc/bridge.h>
48 #include <bus/mmc/mmcreg.h>
49 #include <bus/mmc/mmcbrvar.h>
50 
51 #include "sdhci.h"
52 #include "mmcbr_if.h"
53 #include "sdhci_if.h"
54 
55 /*
56  * PCI registers
57  */
58 
59 #define PCI_SDHCI_IFPIO			0x00
60 #define PCI_SDHCI_IFDMA			0x01
61 #define PCI_SDHCI_IFVENDOR		0x02
62 
63 #define PCI_SLOT_INFO			0x40	/* 8 bits */
64 #define  PCI_SLOT_INFO_SLOTS(x)		(((x >> 4) & 7) + 1)
65 #define  PCI_SLOT_INFO_FIRST_BAR(x)	((x) & 7)
66 
67 /*
68  * RICOH specific PCI registers
69  */
70 #define	SDHC_PCI_MODE_KEY		0xf9
71 #define	SDHC_PCI_MODE			0x150
72 #define	SDHC_PCI_MODE_SD20		0x10
73 #define	SDHC_PCI_BASE_FREQ_KEY		0xfc
74 #define	SDHC_PCI_BASE_FREQ		0xe1
75 
76 static const struct sdhci_device {
77 	uint32_t	model;
78 	uint16_t	subvendor;
79 	const char	*desc;
80 	u_int		quirks;
81 } sdhci_devices[] = {
82 	{ 0x08221180, 	0xffff,	"RICOH R5C822 SD",
83 	    SDHCI_QUIRK_FORCE_SDMA },
84 	{ 0xe8221180, 	0xffff,	"RICOH R5CE822 SD",
85 	    SDHCI_QUIRK_FORCE_SDMA |
86 	    SDHCI_QUIRK_LOWER_FREQUENCY },
87 	{ 0xe8231180, 	0xffff,	"RICOH R5CE823 SD",
88 	    SDHCI_QUIRK_LOWER_FREQUENCY },
89 	{ 0x8034104c, 	0xffff, "TI XX21/XX11 SD",
90 	    SDHCI_QUIRK_FORCE_SDMA },
91 	{ 0x05501524, 	0xffff, "ENE CB712 SD",
92 	    SDHCI_QUIRK_BROKEN_TIMINGS },
93 	{ 0x05511524, 	0xffff, "ENE CB712 SD 2",
94 	    SDHCI_QUIRK_BROKEN_TIMINGS },
95 	{ 0x07501524, 	0xffff, "ENE CB714 SD",
96 	    SDHCI_QUIRK_RESET_ON_IOS |
97 	    SDHCI_QUIRK_BROKEN_TIMINGS },
98 	{ 0x07511524, 	0xffff, "ENE CB714 SD 2",
99 	    SDHCI_QUIRK_RESET_ON_IOS |
100 	    SDHCI_QUIRK_BROKEN_TIMINGS },
101 	{ 0x410111ab, 	0xffff, "Marvell CaFe SD",
102 	    SDHCI_QUIRK_INCR_TIMEOUT_CONTROL },
103 	{ 0x2381197B, 	0xffff,	"JMicron JMB38X SD",
104 	    SDHCI_QUIRK_32BIT_DMA_SIZE |
105 	    SDHCI_QUIRK_RESET_AFTER_REQUEST },
106 	{ 0x16bc14e4,	0xffff,	"Broadcom BCM577xx SDXC/MMC Card Reader",
107 	    SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC },
108 	{ 0,		0xffff,	NULL,
109 	    0 }
110 };
111 
112 struct sdhci_pci_softc {
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 	uint8_t		cfg_freq;	/* Saved mode */
121 	uint8_t		cfg_mode;	/* Saved frequency */
122 };
123 
124 static int sdhci_enable_msi = 1;
125 TUNABLE_INT("hw.sdhci_enable_msi", &sdhci_enable_msi);
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 	struct sdhci_pci_softc *sc = device_get_softc(dev);
211 
212 	/*
213 	 * Enable SD2.0 mode.
214 	 * NB: for RICOH R5CE823, this changes the PCI device ID to 0xe822.
215 	 */
216 	pci_write_config(dev, SDHC_PCI_MODE_KEY, 0xfc, 1);
217 	sc->cfg_mode = pci_read_config(dev, SDHC_PCI_MODE, 1);
218 	pci_write_config(dev, SDHC_PCI_MODE, SDHC_PCI_MODE_SD20, 1);
219 	pci_write_config(dev, SDHC_PCI_MODE_KEY, 0x00, 1);
220 
221 	/*
222 	 * Some SD/MMC cards don't work with the default base
223 	 * clock frequency of 200 MHz.  Lower it to 50 MHz.
224 	 */
225 	pci_write_config(dev, SDHC_PCI_BASE_FREQ_KEY, 0x01, 1);
226 	sc->cfg_freq = pci_read_config(dev, SDHC_PCI_BASE_FREQ, 1);
227 	pci_write_config(dev, SDHC_PCI_BASE_FREQ, 50, 1);
228 	pci_write_config(dev, SDHC_PCI_BASE_FREQ_KEY, 0x00, 1);
229 }
230 
231 static void
232 sdhci_restore_frequency(device_t dev)
233 {
234 	struct sdhci_pci_softc *sc = device_get_softc(dev);
235 
236 	/* Restore mode. */
237 	pci_write_config(dev, SDHC_PCI_MODE_KEY, 0xfc, 1);
238 	pci_write_config(dev, SDHC_PCI_MODE, sc->cfg_mode, 1);
239 	pci_write_config(dev, SDHC_PCI_MODE_KEY, 0x00, 1);
240 
241 	/* Restore frequency. */
242 	pci_write_config(dev, SDHC_PCI_BASE_FREQ_KEY, 0x01, 1);
243 	pci_write_config(dev, SDHC_PCI_BASE_FREQ, sc->cfg_freq, 1);
244 	pci_write_config(dev, SDHC_PCI_BASE_FREQ_KEY, 0x00, 1);
245 }
246 
247 static int
248 sdhci_pci_probe(device_t dev)
249 {
250 	uint32_t model;
251 	uint16_t subvendor;
252 	uint8_t class, subclass;
253 	int i, result;
254 
255 	model = (uint32_t)pci_get_device(dev) << 16;
256 	model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
257 	subvendor = pci_get_subvendor(dev);
258 	class = pci_get_class(dev);
259 	subclass = pci_get_subclass(dev);
260 
261 	result = ENXIO;
262 	for (i = 0; sdhci_devices[i].model != 0; i++) {
263 		if (sdhci_devices[i].model == model &&
264 		    (sdhci_devices[i].subvendor == 0xffff ||
265 		    sdhci_devices[i].subvendor == subvendor)) {
266 			device_set_desc(dev, sdhci_devices[i].desc);
267 			result = BUS_PROBE_DEFAULT;
268 			break;
269 		}
270 	}
271 	if (result == ENXIO && class == PCIC_BASEPERIPH &&
272 	    subclass == PCIS_BASEPERIPH_SDHC) {
273 		device_set_desc(dev, "Generic SD HCI");
274 		result = BUS_PROBE_GENERIC;
275 	}
276 
277 	return (result);
278 }
279 
280 static int
281 sdhci_pci_attach(device_t dev)
282 {
283 	struct sdhci_pci_softc *sc = device_get_softc(dev);
284 	uint32_t model;
285 	uint16_t subvendor;
286 	int bar, count, err, rid, slots, i;
287 
288 	model = (uint32_t)pci_get_device(dev) << 16;
289 	model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
290 	subvendor = pci_get_subvendor(dev);
291 	/* Apply chip specific quirks. */
292 	for (i = 0; sdhci_devices[i].model != 0; i++) {
293 		if (sdhci_devices[i].model == model &&
294 		    (sdhci_devices[i].subvendor == 0xffff ||
295 		    sdhci_devices[i].subvendor == subvendor)) {
296 			sc->quirks = sdhci_devices[i].quirks;
297 			break;
298 		}
299 	}
300 	/* Some controllers need to be bumped into the right mode. */
301 	if (sc->quirks & SDHCI_QUIRK_LOWER_FREQUENCY)
302 		sdhci_lower_frequency(dev);
303 	/* Read slots info from PCI registers. */
304 	slots = pci_read_config(dev, PCI_SLOT_INFO, 1);
305 	bar = PCI_SLOT_INFO_FIRST_BAR(slots);
306 	slots = PCI_SLOT_INFO_SLOTS(slots);
307 	if (slots > 6 || bar > 5) {
308 		device_printf(dev, "Incorrect slots information (%d, %d).\n",
309 		    slots, bar);
310 		return (EINVAL);
311 	}
312 	/* Allocate IRQ. */
313 	i = 1;
314 	rid = 0;
315 	if (sdhci_enable_msi != 0) {
316 		count = pci_msi_count(dev);
317 		if (count >= 1) {
318 			count = 1;
319 			if (pci_alloc_msi(dev, &i, 1, count) == 0) {
320 				if (bootverbose)
321 					device_printf(dev, "MSI enabled\n");
322 				rid = 1;
323 			}
324 		}
325 	}
326 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
327 		RF_ACTIVE | (rid != 0 ? 0 : RF_SHAREABLE));
328 	if (sc->irq_res == NULL) {
329 		device_printf(dev, "Can't allocate IRQ\n");
330 		pci_release_msi(dev);
331 		return (ENOMEM);
332 	}
333 	/* Scan all slots. */
334 	for (i = 0; i < slots; i++) {
335 		struct sdhci_slot *slot = &sc->slots[sc->num_slots];
336 
337 		/* Allocate memory. */
338 		rid = PCIR_BAR(bar + i);
339 		sc->mem_res[i] = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
340 		    &rid, RF_ACTIVE);
341 		if (sc->mem_res[i] == NULL) {
342 			device_printf(dev, "Can't allocate memory for slot %d\n", i);
343 			continue;
344 		}
345 
346 		slot->quirks = sc->quirks;
347 
348 		if (sdhci_init_slot(dev, slot, i) != 0)
349 			continue;
350 
351 		sc->num_slots++;
352 	}
353 	device_printf(dev, "%d slot(s) allocated\n", sc->num_slots);
354 	/* Activate the interrupt */
355 	err = bus_setup_intr(dev, sc->irq_res, INTR_MPSAFE,
356 	    sdhci_pci_intr, sc, &sc->intrhand, NULL);
357 	if (err)
358 		device_printf(dev, "Can't setup IRQ\n");
359 	pci_enable_busmaster(dev);
360 	/* Process cards detection. */
361 	for (i = 0; i < sc->num_slots; i++) {
362 		struct sdhci_slot *slot = &sc->slots[i];
363 
364 		sdhci_start_slot(slot);
365 	}
366 
367 	return (0);
368 }
369 
370 static int
371 sdhci_pci_detach(device_t dev)
372 {
373 	struct sdhci_pci_softc *sc = device_get_softc(dev);
374 	int i;
375 
376 	bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
377 	bus_release_resource(dev, SYS_RES_IRQ,
378 	    rman_get_rid(sc->irq_res), sc->irq_res);
379 	pci_release_msi(dev);
380 
381 	for (i = 0; i < sc->num_slots; i++) {
382 		struct sdhci_slot *slot = &sc->slots[i];
383 
384 		sdhci_cleanup_slot(slot);
385 		bus_release_resource(dev, SYS_RES_MEMORY,
386 		    rman_get_rid(sc->mem_res[i]), sc->mem_res[i]);
387 	}
388 	if (sc->quirks & SDHCI_QUIRK_LOWER_FREQUENCY)
389 		sdhci_restore_frequency(dev);
390 	return (0);
391 }
392 
393 static int
394 sdhci_pci_shutdown(device_t dev)
395 {
396 	struct sdhci_pci_softc *sc = device_get_softc(dev);
397 
398 	if (sc->quirks & SDHCI_QUIRK_LOWER_FREQUENCY)
399 		sdhci_restore_frequency(dev);
400 	return (0);
401 }
402 
403 static int
404 sdhci_pci_suspend(device_t dev)
405 {
406 	struct sdhci_pci_softc *sc = device_get_softc(dev);
407 	int i, err;
408 
409 	err = bus_generic_suspend(dev);
410 	if (err)
411 		return (err);
412 	for (i = 0; i < sc->num_slots; i++)
413 		sdhci_generic_suspend(&sc->slots[i]);
414 	return (0);
415 }
416 
417 static int
418 sdhci_pci_resume(device_t dev)
419 {
420 	struct sdhci_pci_softc *sc = device_get_softc(dev);
421 	int i;
422 
423 	for (i = 0; i < sc->num_slots; i++)
424 		sdhci_generic_resume(&sc->slots[i]);
425 	return (bus_generic_resume(dev));
426 }
427 
428 static void
429 sdhci_pci_intr(void *arg)
430 {
431 	struct sdhci_pci_softc *sc = (struct sdhci_pci_softc *)arg;
432 	int i;
433 
434 	for (i = 0; i < sc->num_slots; i++) {
435 		struct sdhci_slot *slot = &sc->slots[i];
436 		sdhci_generic_intr(slot);
437 	}
438 }
439 
440 static device_method_t sdhci_methods[] = {
441 	/* device_if */
442 	DEVMETHOD(device_probe, sdhci_pci_probe),
443 	DEVMETHOD(device_attach, sdhci_pci_attach),
444 	DEVMETHOD(device_detach, sdhci_pci_detach),
445 	DEVMETHOD(device_shutdown, sdhci_pci_shutdown),
446 	DEVMETHOD(device_suspend, sdhci_pci_suspend),
447 	DEVMETHOD(device_resume, sdhci_pci_resume),
448 
449 	/* Bus interface */
450 	DEVMETHOD(bus_read_ivar,	sdhci_generic_read_ivar),
451 	DEVMETHOD(bus_write_ivar,	sdhci_generic_write_ivar),
452 
453 	/* mmcbr_if */
454 	DEVMETHOD(mmcbr_update_ios, sdhci_generic_update_ios),
455 	DEVMETHOD(mmcbr_request, sdhci_generic_request),
456 	DEVMETHOD(mmcbr_get_ro, sdhci_generic_get_ro),
457 	DEVMETHOD(mmcbr_acquire_host, sdhci_generic_acquire_host),
458 	DEVMETHOD(mmcbr_release_host, sdhci_generic_release_host),
459 
460 	/* SDHCI registers accessors */
461 	DEVMETHOD(sdhci_read_1,		sdhci_pci_read_1),
462 	DEVMETHOD(sdhci_read_2,		sdhci_pci_read_2),
463 	DEVMETHOD(sdhci_read_4,		sdhci_pci_read_4),
464 	DEVMETHOD(sdhci_read_multi_4,	sdhci_pci_read_multi_4),
465 	DEVMETHOD(sdhci_write_1,	sdhci_pci_write_1),
466 	DEVMETHOD(sdhci_write_2,	sdhci_pci_write_2),
467 	DEVMETHOD(sdhci_write_4,	sdhci_pci_write_4),
468 	DEVMETHOD(sdhci_write_multi_4,	sdhci_pci_write_multi_4),
469 
470 	DEVMETHOD_END
471 };
472 
473 static driver_t sdhci_pci_driver = {
474 	"sdhci_pci",
475 	sdhci_methods,
476 	sizeof(struct sdhci_pci_softc),
477 };
478 static devclass_t sdhci_pci_devclass;
479 
480 DRIVER_MODULE(sdhci_pci, pci, sdhci_pci_driver, sdhci_pci_devclass, NULL,
481     NULL);
482 MODULE_DEPEND(sdhci_pci, sdhci, 1, 1, 1);
483