xref: /illumos-gate/usr/src/uts/common/io/sdcard/adapters/sdhost/sdhost.c (revision 47842382d52f28aa3173aa6b511781c322ccb6a2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include "sdhost.h"
27 
28 typedef	struct sdstats	sdstats_t;
29 typedef	struct sdslot	sdslot_t;
30 typedef	struct sdhost	sdhost_t;
31 
32 struct sdstats {
33 	kstat_named_t	ks_ncmd;
34 	kstat_named_t	ks_ixfr;
35 	kstat_named_t	ks_oxfr;
36 	kstat_named_t	ks_ibytes;
37 	kstat_named_t	ks_obytes;
38 	kstat_named_t	ks_npio;
39 	kstat_named_t	ks_ndma;
40 	kstat_named_t	ks_nmulti;
41 	kstat_named_t	ks_baseclk;
42 	kstat_named_t	ks_cardclk;
43 	kstat_named_t	ks_tmusecs;
44 	kstat_named_t	ks_width;
45 	kstat_named_t	ks_flags;
46 	kstat_named_t	ks_capab;
47 };
48 
49 #define	SDFLAG_FORCE_PIO		(1U << 0)
50 #define	SDFLAG_FORCE_DMA		(1U << 1)
51 
52 /*
53  * Per slot state.
54  */
55 struct sdslot {
56 	sda_host_t		*ss_host;
57 	int			ss_num;
58 	ddi_acc_handle_t	ss_acch;
59 	caddr_t 		ss_regva;
60 	kmutex_t		ss_lock;
61 	uint8_t			ss_tmoutclk;
62 	uint32_t		ss_ocr;		/* OCR formatted voltages */
63 	uint16_t		ss_mode;
64 	boolean_t		ss_suspended;
65 	sdstats_t		ss_stats;
66 #define	ss_ncmd			ss_stats.ks_ncmd.value.ui64
67 #define	ss_ixfr			ss_stats.ks_ixfr.value.ui64
68 #define	ss_oxfr			ss_stats.ks_oxfr.value.ui64
69 #define	ss_ibytes		ss_stats.ks_ibytes.value.ui64
70 #define	ss_obytes		ss_stats.ks_obytes.value.ui64
71 #define	ss_ndma			ss_stats.ks_ndma.value.ui64
72 #define	ss_npio			ss_stats.ks_npio.value.ui64
73 #define	ss_nmulti		ss_stats.ks_nmulti.value.ui64
74 
75 #define	ss_baseclk		ss_stats.ks_baseclk.value.ui32
76 #define	ss_cardclk		ss_stats.ks_cardclk.value.ui32
77 #define	ss_tmusecs		ss_stats.ks_tmusecs.value.ui32
78 #define	ss_width		ss_stats.ks_width.value.ui32
79 #define	ss_flags		ss_stats.ks_flags.value.ui32
80 #define	ss_capab		ss_stats.ks_capab.value.ui32
81 	kstat_t			*ss_ksp;
82 
83 	/*
84 	 * Command in progress
85 	 */
86 	uint8_t			*ss_kvaddr;
87 	int			ss_blksz;
88 	uint16_t		ss_resid;	/* in blocks */
89 	int			ss_rcnt;
90 
91 	/* scratch buffer, to receive extra PIO data */
92 	caddr_t			ss_bounce;
93 	ddi_dma_handle_t	ss_bufdmah;
94 	ddi_acc_handle_t	ss_bufacch;
95 	ddi_dma_cookie_t	ss_bufdmac;
96 };
97 
98 /*
99  * This allocates a rather large chunk of contiguous memory for DMA.
100  * But doing so means that we'll almost never have to resort to PIO.
101  */
102 #define	SDHOST_BOUNCESZ		65536
103 
104 /*
105  * Per controller state.
106  */
107 struct sdhost {
108 	int			sh_numslots;
109 	ddi_dma_attr_t		sh_dmaattr;
110 	sdslot_t		sh_slots[SDHOST_MAXSLOTS];
111 	sda_host_t		*sh_host;
112 
113 	/*
114 	 * Interrupt related information.
115 	 */
116 	ddi_intr_handle_t	sh_ihandle;
117 	int			sh_icap;
118 	uint_t			sh_ipri;
119 };
120 
121 #define	PROPSET(x)							\
122 	(ddi_prop_get_int(DDI_DEV_T_ANY, dip,				\
123 	DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, x, 0) != 0)
124 
125 
126 static int sdhost_attach(dev_info_t *, ddi_attach_cmd_t);
127 static int sdhost_detach(dev_info_t *, ddi_detach_cmd_t);
128 static int sdhost_quiesce(dev_info_t *);
129 static int sdhost_suspend(dev_info_t *);
130 static int sdhost_resume(dev_info_t *);
131 
132 static void sdhost_enable_interrupts(sdslot_t *);
133 static void sdhost_disable_interrupts(sdslot_t *);
134 static int sdhost_setup_intr(dev_info_t *, sdhost_t *);
135 static uint_t sdhost_intr(caddr_t, caddr_t);
136 static int sdhost_init_slot(dev_info_t *, sdhost_t *, int, int);
137 static void sdhost_uninit_slot(sdhost_t *, int);
138 static sda_err_t sdhost_soft_reset(sdslot_t *, uint8_t);
139 static sda_err_t sdhost_set_clock(sdslot_t *, uint32_t);
140 static void sdhost_xfer_done(sdslot_t *, sda_err_t);
141 static sda_err_t sdhost_wait_cmd(sdslot_t *, sda_cmd_t *);
142 static uint_t sdhost_slot_intr(sdslot_t *);
143 
144 static sda_err_t sdhost_cmd(void *, sda_cmd_t *);
145 static sda_err_t sdhost_getprop(void *, sda_prop_t, uint32_t *);
146 static sda_err_t sdhost_setprop(void *, sda_prop_t, uint32_t);
147 static sda_err_t sdhost_poll(void *);
148 static sda_err_t sdhost_reset(void *);
149 static sda_err_t sdhost_halt(void *);
150 
151 static struct dev_ops sdhost_dev_ops = {
152 	DEVO_REV,			/* devo_rev */
153 	0,				/* devo_refcnt */
154 	ddi_no_info,			/* devo_getinfo */
155 	nulldev,			/* devo_identify */
156 	nulldev,			/* devo_probe */
157 	sdhost_attach,			/* devo_attach */
158 	sdhost_detach,			/* devo_detach */
159 	nodev,				/* devo_reset */
160 	NULL,				/* devo_cb_ops */
161 	NULL,				/* devo_bus_ops */
162 	NULL,				/* devo_power */
163 	sdhost_quiesce,			/* devo_quiesce */
164 };
165 
166 static struct modldrv sdhost_modldrv = {
167 	&mod_driverops,			/* drv_modops */
168 	"Standard SD Host Controller",	/* drv_linkinfo */
169 	&sdhost_dev_ops			/* drv_dev_ops */
170 };
171 
172 static struct modlinkage modlinkage = {
173 	MODREV_1,			/* ml_rev */
174 	{ &sdhost_modldrv, NULL }	/* ml_linkage */
175 };
176 
177 static struct sda_ops sdhost_ops = {
178 	SDA_OPS_VERSION,
179 	sdhost_cmd,			/* so_cmd */
180 	sdhost_getprop,			/* so_getprop */
181 	sdhost_setprop,			/* so_setprop */
182 	sdhost_poll,			/* so_poll */
183 	sdhost_reset,			/* so_reset */
184 	sdhost_halt,			/* so_halt */
185 };
186 
187 static ddi_device_acc_attr_t sdhost_regattr = {
188 	DDI_DEVICE_ATTR_V0,	/* devacc_attr_version */
189 	DDI_STRUCTURE_LE_ACC,	/* devacc_attr_endian_flags */
190 	DDI_STRICTORDER_ACC,	/* devacc_attr_dataorder */
191 	DDI_DEFAULT_ACC,	/* devacc_attr_access */
192 };
193 static ddi_device_acc_attr_t sdhost_bufattr = {
194 	DDI_DEVICE_ATTR_V0,	/* devacc_attr_version */
195 	DDI_NEVERSWAP_ACC,	/* devacc_attr_endian_flags */
196 	DDI_STRICTORDER_ACC,	/* devacc_attr_dataorder */
197 	DDI_DEFAULT_ACC,	/* devacc_attr_access */
198 };
199 
200 #define	GET16(ss, reg)	\
201 	ddi_get16(ss->ss_acch, (void *)(ss->ss_regva + reg))
202 #define	PUT16(ss, reg, val)	\
203 	ddi_put16(ss->ss_acch, (void *)(ss->ss_regva + reg), val)
204 #define	GET32(ss, reg)	\
205 	ddi_get32(ss->ss_acch, (void *)(ss->ss_regva + reg))
206 #define	PUT32(ss, reg, val)	\
207 	ddi_put32(ss->ss_acch, (void *)(ss->ss_regva + reg), val)
208 #define	GET64(ss, reg)	\
209 	ddi_get64(ss->ss_acch, (void *)(ss->ss_regva + reg))
210 
211 #define	GET8(ss, reg)	\
212 	ddi_get8(ss->ss_acch, (void *)(ss->ss_regva + reg))
213 #define	PUT8(ss, reg, val)	\
214 	ddi_put8(ss->ss_acch, (void *)(ss->ss_regva + reg), val)
215 
216 #define	CLR8(ss, reg, mask)	PUT8(ss, reg, GET8(ss, reg) & ~(mask))
217 #define	SET8(ss, reg, mask)	PUT8(ss, reg, GET8(ss, reg) | (mask))
218 
219 /*
220  * If ever anyone uses PIO on SPARC, we have to endian-swap.  But we
221  * think that SD Host Controllers are likely to be uncommon on SPARC,
222  * and hopefully when they exist at all they will be able to use DMA.
223  */
224 #ifdef	_BIG_ENDIAN
225 #define	sw32(x)		ddi_swap32(x)
226 #define	sw16(x)		ddi_swap16(x)
227 #else
228 #define	sw32(x)		(x)
229 #define	sw16(x)		(x)
230 #endif
231 
232 #define	GETDATA32(ss)		sw32(GET32(ss, REG_DATA))
233 #define	GETDATA16(ss)		sw16(GET16(ss, REG_DATA))
234 #define	GETDATA8(ss)		GET8(ss, REG_DATA)
235 
236 #define	PUTDATA32(ss, val)	PUT32(ss, REG_DATA, sw32(val))
237 #define	PUTDATA16(ss, val)	PUT16(ss, REG_DATA, sw16(val))
238 #define	PUTDATA8(ss, val)	PUT8(ss, REG_DATA, val)
239 
240 #define	CHECK_STATE(ss, nm)	\
241 	((GET32(ss, REG_PRS) & PRS_ ## nm) != 0)
242 
243 int
244 _init(void)
245 {
246 	int	rv;
247 
248 	sda_host_init_ops(&sdhost_dev_ops);
249 
250 	if ((rv = mod_install(&modlinkage)) != 0) {
251 		sda_host_fini_ops(&sdhost_dev_ops);
252 	}
253 
254 	return (rv);
255 }
256 
257 int
258 _fini(void)
259 {
260 	int	rv;
261 
262 	if ((rv = mod_remove(&modlinkage)) == 0) {
263 		sda_host_fini_ops(&sdhost_dev_ops);
264 	}
265 	return (rv);
266 }
267 
268 int
269 _info(struct modinfo *modinfop)
270 {
271 	return (mod_info(&modlinkage, modinfop));
272 }
273 
274 int
275 sdhost_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
276 {
277 	sdhost_t		*shp;
278 	ddi_acc_handle_t	pcih;
279 	uint8_t			slotinfo;
280 	uint8_t			bar;
281 	int			i;
282 	int			rv;
283 
284 	switch (cmd) {
285 	case DDI_ATTACH:
286 		break;
287 
288 	case DDI_RESUME:
289 		return (sdhost_resume(dip));
290 
291 	default:
292 		return (DDI_FAILURE);
293 	}
294 
295 	/*
296 	 * Soft state allocation.
297 	 */
298 	shp = kmem_zalloc(sizeof (*shp), KM_SLEEP);
299 	ddi_set_driver_private(dip, shp);
300 
301 	/*
302 	 * Initialize DMA attributes.  For now we initialize as for
303 	 * SDMA.  If we add ADMA support we can improve this.
304 	 */
305 	shp->sh_dmaattr.dma_attr_version = DMA_ATTR_V0;
306 	shp->sh_dmaattr.dma_attr_addr_lo = 0;
307 	shp->sh_dmaattr.dma_attr_addr_hi = 0xffffffffU;
308 	shp->sh_dmaattr.dma_attr_count_max = 0xffffffffU;
309 	shp->sh_dmaattr.dma_attr_align = 4096;		/* Ricoh needs it */
310 	shp->sh_dmaattr.dma_attr_burstsizes = 0;	/* for now! */
311 	shp->sh_dmaattr.dma_attr_minxfer = 1;
312 	shp->sh_dmaattr.dma_attr_maxxfer = 0x7ffffU;
313 	shp->sh_dmaattr.dma_attr_sgllen = 1;		/* no scatter/gather */
314 	shp->sh_dmaattr.dma_attr_seg = 0x7ffffU;	/* not to cross 512K */
315 	shp->sh_dmaattr.dma_attr_granular = 1;
316 	shp->sh_dmaattr.dma_attr_flags = 0;
317 
318 	/*
319 	 * PCI configuration access to figure out number of slots present.
320 	 */
321 	if (pci_config_setup(dip, &pcih) != DDI_SUCCESS) {
322 		cmn_err(CE_WARN, "pci_config_setup failed");
323 		goto failed;
324 	}
325 
326 	slotinfo = pci_config_get8(pcih, SLOTINFO);
327 	shp->sh_numslots = SLOTINFO_NSLOT(slotinfo);
328 
329 	if (shp->sh_numslots > SDHOST_MAXSLOTS) {
330 		cmn_err(CE_WARN, "Host reports to have too many slots: %d",
331 		    shp->sh_numslots);
332 		goto failed;
333 	}
334 
335 	/*
336 	 * Enable master accesses and DMA.
337 	 */
338 	pci_config_put16(pcih, PCI_CONF_COMM,
339 	    pci_config_get16(pcih, PCI_CONF_COMM) |
340 	    PCI_COMM_MAE | PCI_COMM_ME);
341 
342 	/*
343 	 * Figure out which BAR to use.  Note that we number BARs from
344 	 * 1, although PCI and SD Host numbers from 0.  (We number
345 	 * from 1, because register number 0 means PCI configuration
346 	 * space in Solaris.)
347 	 */
348 	bar = SLOTINFO_BAR(slotinfo) + 1;
349 
350 	pci_config_teardown(&pcih);
351 
352 	/*
353 	 * Setup interrupts ... supports the new DDI interrupt API.  This
354 	 * will support MSI or MSI-X interrupts if a device is found to
355 	 * support it.
356 	 */
357 	if (sdhost_setup_intr(dip, shp) != DDI_SUCCESS) {
358 		cmn_err(CE_WARN, "Failed to setup interrupts");
359 		goto failed;
360 	}
361 
362 	shp->sh_host = sda_host_alloc(dip, shp->sh_numslots, &sdhost_ops,
363 	    &shp->sh_dmaattr);
364 	if (shp->sh_host == NULL) {
365 		cmn_err(CE_WARN, "Failed allocating SD host structure");
366 		goto failed;
367 	}
368 
369 	/*
370 	 * Configure slots, this also maps registers, enables
371 	 * interrupts, etc.  Most of the hardware setup is done here.
372 	 */
373 	for (i = 0; i < shp->sh_numslots; i++) {
374 		if (sdhost_init_slot(dip, shp, i, bar + i) != DDI_SUCCESS) {
375 			cmn_err(CE_WARN, "Failed initializing slot %d", i);
376 			goto failed;
377 		}
378 	}
379 
380 	ddi_report_dev(dip);
381 
382 	/*
383 	 * Enable device interrupts at the DDI layer.
384 	 */
385 	if (shp->sh_icap & DDI_INTR_FLAG_BLOCK) {
386 		rv = ddi_intr_block_enable(&shp->sh_ihandle, 1);
387 	} else {
388 		rv = ddi_intr_enable(shp->sh_ihandle);
389 	}
390 	if (rv != DDI_SUCCESS) {
391 		cmn_err(CE_WARN, "Failed enabling interrupts");
392 		goto failed;
393 	}
394 
395 	/*
396 	 * Mark the slots online with the framework.  This will cause
397 	 * the framework to probe them for the presence of cards.
398 	 */
399 	if (sda_host_attach(shp->sh_host) != DDI_SUCCESS) {
400 		cmn_err(CE_WARN, "Failed attaching to SDA framework");
401 		if (shp->sh_icap & DDI_INTR_FLAG_BLOCK) {
402 			(void) ddi_intr_block_disable(&shp->sh_ihandle, 1);
403 		} else {
404 			(void) ddi_intr_disable(shp->sh_ihandle);
405 		}
406 		goto failed;
407 	}
408 
409 	return (DDI_SUCCESS);
410 
411 failed:
412 	if (shp->sh_ihandle != NULL) {
413 		(void) ddi_intr_remove_handler(shp->sh_ihandle);
414 		(void) ddi_intr_free(shp->sh_ihandle);
415 	}
416 	for (i = 0; i < shp->sh_numslots; i++)
417 		sdhost_uninit_slot(shp, i);
418 	kmem_free(shp, sizeof (*shp));
419 
420 	return (DDI_FAILURE);
421 }
422 
423 int
424 sdhost_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
425 {
426 	sdhost_t	*shp;
427 	int		i;
428 
429 	switch (cmd) {
430 	case DDI_DETACH:
431 		break;
432 
433 	case DDI_SUSPEND:
434 		return (sdhost_suspend(dip));
435 
436 	default:
437 		return (DDI_FAILURE);
438 	}
439 
440 	shp = ddi_get_driver_private(dip);
441 
442 	/*
443 	 * Take host offline with the framework.
444 	 */
445 	sda_host_detach(shp->sh_host);
446 
447 	/*
448 	 * Tear down interrupts.
449 	 */
450 	if (shp->sh_ihandle != NULL) {
451 		if (shp->sh_icap & DDI_INTR_FLAG_BLOCK) {
452 			(void) ddi_intr_block_disable(&shp->sh_ihandle, 1);
453 		} else {
454 			(void) ddi_intr_disable(shp->sh_ihandle);
455 		}
456 		(void) ddi_intr_remove_handler(shp->sh_ihandle);
457 		(void) ddi_intr_free(shp->sh_ihandle);
458 	}
459 
460 	/*
461 	 * Tear down register mappings, etc.
462 	 */
463 	for (i = 0; i < shp->sh_numslots; i++)
464 		sdhost_uninit_slot(shp, i);
465 	kmem_free(shp, sizeof (*shp));
466 
467 	return (DDI_SUCCESS);
468 }
469 
470 int
471 sdhost_quiesce(dev_info_t *dip)
472 {
473 	sdhost_t	*shp;
474 	sdslot_t	*ss;
475 
476 	shp = ddi_get_driver_private(dip);
477 
478 	/* reset each slot separately */
479 	for (int i = 0; i < shp->sh_numslots; i++) {
480 		ss = &shp->sh_slots[i];
481 		if (ss->ss_acch == NULL)
482 			continue;
483 
484 		(void) sdhost_soft_reset(ss, SOFT_RESET_ALL);
485 	}
486 	return (DDI_SUCCESS);
487 }
488 
489 int
490 sdhost_suspend(dev_info_t *dip)
491 {
492 	sdhost_t	*shp;
493 	sdslot_t	*ss;
494 	int		i;
495 
496 	shp = ddi_get_driver_private(dip);
497 
498 	sda_host_suspend(shp->sh_host);
499 
500 	for (i = 0; i < shp->sh_numslots; i++) {
501 		ss = &shp->sh_slots[i];
502 		mutex_enter(&ss->ss_lock);
503 		ss->ss_suspended = B_TRUE;
504 		sdhost_disable_interrupts(ss);
505 		(void) sdhost_soft_reset(ss, SOFT_RESET_ALL);
506 		mutex_exit(&ss->ss_lock);
507 	}
508 	return (DDI_SUCCESS);
509 }
510 
511 int
512 sdhost_resume(dev_info_t *dip)
513 {
514 	sdhost_t	*shp;
515 	sdslot_t	*ss;
516 	int		i;
517 
518 	shp = ddi_get_driver_private(dip);
519 
520 	for (i = 0; i < shp->sh_numslots; i++) {
521 		ss = &shp->sh_slots[i];
522 		mutex_enter(&ss->ss_lock);
523 		ss->ss_suspended = B_FALSE;
524 		(void) sdhost_soft_reset(ss, SOFT_RESET_ALL);
525 		sdhost_enable_interrupts(ss);
526 		mutex_exit(&ss->ss_lock);
527 	}
528 
529 	sda_host_resume(shp->sh_host);
530 
531 	return (DDI_SUCCESS);
532 }
533 
534 sda_err_t
535 sdhost_set_clock(sdslot_t *ss, uint32_t hz)
536 {
537 	uint16_t	div;
538 	uint32_t	val;
539 	uint32_t	clk;
540 	int		count;
541 
542 	/*
543 	 * Shut off the clock to begin.
544 	 */
545 	ss->ss_cardclk = 0;
546 	PUT16(ss, REG_CLOCK_CONTROL, 0);
547 	if (hz == 0) {
548 		return (SDA_EOK);
549 	}
550 
551 	if (ss->ss_baseclk == 0) {
552 		sda_host_log(ss->ss_host, ss->ss_num,
553 		    "Base clock frequency not established.");
554 		return (SDA_EINVAL);
555 	}
556 
557 	if ((hz > 25000000) && ((ss->ss_capab & CAPAB_HIGH_SPEED) != 0)) {
558 		/* this clock requires high speed timings! */
559 		SET8(ss, REG_HOST_CONTROL, HOST_CONTROL_HIGH_SPEED_EN);
560 	} else {
561 		/* don't allow clock to run faster than 25MHz */
562 		hz = min(hz, 25000000);
563 		CLR8(ss, REG_HOST_CONTROL, HOST_CONTROL_HIGH_SPEED_EN);
564 	}
565 
566 	/* figure out the divider */
567 	clk = ss->ss_baseclk;
568 	div  = 1;
569 	while (clk > hz) {
570 		if (div > 0x80)
571 			break;
572 		clk >>= 1;	/* divide clock by two */
573 		div <<= 1;	/* divider goes up by one */
574 	}
575 	div >>= 1;	/* 0 == divide by 1, 1 = divide by 2 */
576 
577 	/*
578 	 * Set the internal clock divider first, without enabling the
579 	 * card clock yet.
580 	 */
581 	PUT16(ss, REG_CLOCK_CONTROL,
582 	    (div << CLOCK_CONTROL_FREQ_SHIFT) | CLOCK_CONTROL_INT_CLOCK_EN);
583 
584 	/*
585 	 * Wait up to 100 msec for the internal clock to stabilize.
586 	 * (The spec does not seem to indicate a maximum timeout, but
587 	 * it also suggests that an infinite loop be used, which is
588 	 * not appropriate for hardened Solaris drivers.)
589 	 */
590 	for (count = 100000; count; count -= 10) {
591 
592 		val = GET16(ss, REG_CLOCK_CONTROL);
593 
594 		if (val & CLOCK_CONTROL_INT_CLOCK_STABLE) {
595 			/* if clock is stable, enable the SD clock pin */
596 			PUT16(ss, REG_CLOCK_CONTROL, val |
597 			    CLOCK_CONTROL_SD_CLOCK_EN);
598 
599 			ss->ss_cardclk = clk;
600 			return (SDA_EOK);
601 		}
602 
603 		drv_usecwait(10);
604 	}
605 
606 	return (SDA_ETIME);
607 }
608 
609 sda_err_t
610 sdhost_soft_reset(sdslot_t *ss, uint8_t bits)
611 {
612 	int	count;
613 
614 	/*
615 	 * There appears to be a bug where Ricoh hosts might have a
616 	 * problem if the host frequency is not set.  If the card
617 	 * isn't present, or we are doing a master reset, just enable
618 	 * the internal clock at its native speed.  (No dividers, and
619 	 * not exposed to card.).
620 	 */
621 	if ((bits == SOFT_RESET_ALL) || !(CHECK_STATE(ss, CARD_INSERTED))) {
622 		PUT16(ss, REG_CLOCK_CONTROL, CLOCK_CONTROL_INT_CLOCK_EN);
623 		/* simple 1msec wait, don't wait for clock to stabilize */
624 		drv_usecwait(1000);
625 		/*
626 		 * reset the card clock & width -- master reset also
627 		 * resets these
628 		 */
629 		ss->ss_cardclk = 0;
630 		ss->ss_width = 1;
631 	}
632 
633 
634 	PUT8(ss, REG_SOFT_RESET, bits);
635 	for (count = 100000; count != 0; count -= 10) {
636 		if ((GET8(ss, REG_SOFT_RESET) & bits) == 0) {
637 			return (SDA_EOK);
638 		}
639 		drv_usecwait(10);
640 	}
641 
642 	return (SDA_ETIME);
643 }
644 
645 void
646 sdhost_disable_interrupts(sdslot_t *ss)
647 {
648 	/* disable slot interrupts for card insert and remove */
649 	PUT16(ss, REG_INT_MASK, 0);
650 	PUT16(ss, REG_INT_EN, 0);
651 
652 	/* disable error interrupts */
653 	PUT16(ss, REG_ERR_MASK, 0);
654 	PUT16(ss, REG_ERR_EN, 0);
655 }
656 
657 void
658 sdhost_enable_interrupts(sdslot_t *ss)
659 {
660 	/*
661 	 * Note that we want to enable reading of the CMD related
662 	 * bits, but we do not want them to generate an interrupt.
663 	 * (The busy wait for typical CMD stuff will normally be less
664 	 * than 10usec, so its simpler/easier to just poll.  Even in
665 	 * the worst case of 100 kHz, the poll is at worst 2 msec.)
666 	 */
667 
668 	/* enable slot interrupts for card insert and remove */
669 	PUT16(ss, REG_INT_MASK, INT_MASK);
670 	PUT16(ss, REG_INT_EN, INT_ENAB);
671 
672 	/* enable error interrupts */
673 	PUT16(ss, REG_ERR_MASK, ERR_MASK);
674 	PUT16(ss, REG_ERR_EN, ERR_ENAB);
675 }
676 
677 int
678 sdhost_setup_intr(dev_info_t *dip, sdhost_t *shp)
679 {
680 	int		itypes;
681 	int		itype;
682 
683 	/*
684 	 * Set up interrupt handler.
685 	 */
686 	if (ddi_intr_get_supported_types(dip, &itypes) != DDI_SUCCESS) {
687 		cmn_err(CE_WARN, "ddi_intr_get_supported_types failed");
688 		return (DDI_FAILURE);
689 	}
690 
691 	/*
692 	 * It turns out that some controllers don't properly implement MSI,
693 	 * but advertise MSI capability in their  PCI config space.
694 	 *
695 	 * While this is really a chip-specific bug, the simplest solution
696 	 * is to just suppress MSI for now by default -- every device seen
697 	 * so far can use FIXED interrupts.
698 	 *
699 	 * We offer an override property, though, just in case someone really
700 	 * wants to force it.
701 	 *
702 	 * We don't do this if the FIXED type isn't supported though!
703 	 */
704 	if (itypes & DDI_INTR_TYPE_FIXED) {
705 		if (!PROPSET(SDHOST_PROP_ENABLE_MSI)) {
706 			itypes &= ~DDI_INTR_TYPE_MSI;
707 		}
708 		if (!PROPSET(SDHOST_PROP_ENABLE_MSIX)) {
709 			itypes &= ~DDI_INTR_TYPE_MSIX;
710 		}
711 	}
712 
713 	/*
714 	 * Interrupt types are bits in a mask.  We know about these ones:
715 	 * FIXED = 1
716 	 * MSI = 2
717 	 * MSIX = 4
718 	 */
719 	for (itype = DDI_INTR_TYPE_MSIX; itype != 0; itype >>= 1) {
720 
721 		int			count;
722 
723 		if ((itypes & itype) == 0) {
724 			/* this type is not supported on this device! */
725 			continue;
726 		}
727 
728 		if ((ddi_intr_get_nintrs(dip, itype, &count) != DDI_SUCCESS) ||
729 		    (count == 0)) {
730 			cmn_err(CE_WARN, "ddi_intr_get_nintrs failed");
731 			continue;
732 		}
733 
734 		/*
735 		 * We have not seen a host device with multiple
736 		 * interrupts (one per slot?), and the spec does not
737 		 * indicate that they exist.  But if one ever occurs,
738 		 * we spew a warning to help future debugging/support
739 		 * efforts.
740 		 */
741 		if (count > 1) {
742 			cmn_err(CE_WARN, "Controller offers %d interrupts, "
743 			    "but driver only supports one", count);
744 			continue;
745 		}
746 
747 		if ((ddi_intr_alloc(dip, &shp->sh_ihandle, itype, 0, 1,
748 		    &count, DDI_INTR_ALLOC_NORMAL) != DDI_SUCCESS) ||
749 		    (count != 1)) {
750 			cmn_err(CE_WARN, "ddi_intr_alloc failed");
751 			continue;
752 		}
753 
754 		if (ddi_intr_get_pri(shp->sh_ihandle, &shp->sh_ipri) !=
755 		    DDI_SUCCESS) {
756 			cmn_err(CE_WARN, "ddi_intr_get_pri failed");
757 			(void) ddi_intr_free(shp->sh_ihandle);
758 			shp->sh_ihandle = NULL;
759 			continue;
760 		}
761 
762 		if (shp->sh_ipri >= ddi_intr_get_hilevel_pri()) {
763 			cmn_err(CE_WARN, "Hi level interrupt not supported");
764 			(void) ddi_intr_free(shp->sh_ihandle);
765 			shp->sh_ihandle = NULL;
766 			continue;
767 		}
768 
769 		if (ddi_intr_get_cap(shp->sh_ihandle, &shp->sh_icap) !=
770 		    DDI_SUCCESS) {
771 			cmn_err(CE_WARN, "ddi_intr_get_cap failed");
772 			(void) ddi_intr_free(shp->sh_ihandle);
773 			shp->sh_ihandle = NULL;
774 			continue;
775 		}
776 
777 		if (ddi_intr_add_handler(shp->sh_ihandle, sdhost_intr,
778 		    shp, NULL) != DDI_SUCCESS) {
779 			cmn_err(CE_WARN, "ddi_intr_add_handler failed");
780 			(void) ddi_intr_free(shp->sh_ihandle);
781 			shp->sh_ihandle = NULL;
782 			continue;
783 		}
784 
785 		return (DDI_SUCCESS);
786 	}
787 
788 	return (DDI_FAILURE);
789 }
790 
791 void
792 sdhost_xfer_done(sdslot_t *ss, sda_err_t errno)
793 {
794 	if ((errno == SDA_EOK) && (ss->ss_resid != 0)) {
795 		/* an unexpected partial transfer was found */
796 		errno = SDA_ERESID;
797 	}
798 	ss->ss_blksz = 0;
799 	ss->ss_resid = 0;
800 
801 	if (errno != SDA_EOK) {
802 		(void) sdhost_soft_reset(ss, SOFT_RESET_CMD);
803 		(void) sdhost_soft_reset(ss, SOFT_RESET_DAT);
804 
805 		/* send a STOP command if necessary */
806 		if (ss->ss_mode & XFR_MODE_AUTO_CMD12) {
807 			PUT32(ss, REG_ARGUMENT, 0);
808 			PUT16(ss, REG_COMMAND,
809 			    (CMD_STOP_TRANSMIT << 8) |
810 			    COMMAND_TYPE_NORM | COMMAND_INDEX_CHECK_EN |
811 			    COMMAND_CRC_CHECK_EN | COMMAND_RESP_48_BUSY);
812 		}
813 	}
814 
815 	sda_host_transfer(ss->ss_host, ss->ss_num, errno);
816 }
817 
818 uint_t
819 sdhost_slot_intr(sdslot_t *ss)
820 {
821 	uint16_t	intr;
822 	uint16_t	errs;
823 	caddr_t		data;
824 	int		count;
825 
826 	mutex_enter(&ss->ss_lock);
827 
828 	if (ss->ss_suspended) {
829 		mutex_exit(&ss->ss_lock);
830 		return (DDI_INTR_UNCLAIMED);
831 	}
832 
833 	intr = GET16(ss, REG_INT_STAT);
834 	if (intr == 0) {
835 		mutex_exit(&ss->ss_lock);
836 		return (DDI_INTR_UNCLAIMED);
837 	}
838 	errs = GET16(ss, REG_ERR_STAT);
839 
840 	if (intr & (INT_REM | INT_INS)) {
841 
842 		PUT16(ss, REG_INT_STAT, intr);
843 		mutex_exit(&ss->ss_lock);
844 
845 		sda_host_detect(ss->ss_host, ss->ss_num);
846 		/* no further interrupt processing this cycle */
847 		return (DDI_INTR_CLAIMED);
848 	}
849 
850 	if (intr & INT_DMA) {
851 		/*
852 		 * We have crossed a DMA/page boundary.  Cope with it.
853 		 */
854 		/*
855 		 * Apparently some sdhost controllers issue a final
856 		 * DMA interrupt if the DMA completes on a boundary,
857 		 * even though there is no further data to transfer.
858 		 *
859 		 * There might be a risk here of the controller
860 		 * continuing to access the same data over and over
861 		 * again, but we accept the risk.
862 		 */
863 		PUT16(ss, REG_INT_STAT, INT_DMA);
864 	}
865 
866 	if (intr & INT_RD) {
867 		/*
868 		 * PIO read!  PIO is quite suboptimal, but we expect
869 		 * performance critical applications to use DMA
870 		 * whenever possible.  We have to stage this through
871 		 * the bounce buffer to meet alignment considerations.
872 		 */
873 
874 		PUT16(ss, REG_INT_STAT, INT_RD);
875 
876 		while ((ss->ss_resid > 0) && CHECK_STATE(ss, BUF_RD_EN)) {
877 
878 			data = ss->ss_bounce;
879 			count = ss->ss_blksz;
880 
881 			ASSERT(count > 0);
882 			ASSERT(ss->ss_kvaddr != NULL);
883 
884 			while (count >= sizeof (uint32_t)) {
885 				*(uint32_t *)(void *)data = GETDATA32(ss);
886 				data += sizeof (uint32_t);
887 				count -= sizeof (uint32_t);
888 			}
889 			while (count >= sizeof (uint16_t)) {
890 				*(uint16_t *)(void *)data = GETDATA16(ss);
891 				data += sizeof (uint16_t);
892 				count -= sizeof (uint16_t);
893 			}
894 			while (count >= sizeof (uint8_t)) {
895 				*(uint8_t *)data = GETDATA8(ss);
896 				data += sizeof (uint8_t);
897 				count -= sizeof (uint8_t);
898 			}
899 
900 			bcopy(ss->ss_bounce, ss->ss_kvaddr, ss->ss_blksz);
901 			ss->ss_kvaddr += ss->ss_blksz;
902 			ss->ss_resid--;
903 		}
904 	}
905 
906 	if (intr & INT_WR) {
907 		/*
908 		 * PIO write!  PIO is quite suboptimal, but we expect
909 		 * performance critical applications to use DMA
910 		 * whenever possible.  We have to stage this through
911 		 * the bounce buffer to meet alignment considerations.
912 		 */
913 
914 		PUT16(ss, REG_INT_STAT, INT_WR);
915 
916 		while ((ss->ss_resid > 0) && CHECK_STATE(ss, BUF_WR_EN)) {
917 
918 			data = ss->ss_bounce;
919 			count = ss->ss_blksz;
920 
921 			ASSERT(count > 0);
922 			ASSERT(ss->ss_kvaddr != NULL);
923 
924 			bcopy(ss->ss_kvaddr, data, count);
925 			while (count >= sizeof (uint32_t)) {
926 				PUTDATA32(ss, *(uint32_t *)(void *)data);
927 				data += sizeof (uint32_t);
928 				count -= sizeof (uint32_t);
929 			}
930 			while (count >= sizeof (uint16_t)) {
931 				PUTDATA16(ss, *(uint16_t *)(void *)data);
932 				data += sizeof (uint16_t);
933 				count -= sizeof (uint16_t);
934 			}
935 			while (count >= sizeof (uint8_t)) {
936 				PUTDATA8(ss, *(uint8_t *)data);
937 				data += sizeof (uint8_t);
938 				count -= sizeof (uint8_t);
939 			}
940 
941 			ss->ss_kvaddr += ss->ss_blksz;
942 			ss->ss_resid--;
943 		}
944 	}
945 
946 	if (intr & INT_XFR) {
947 		if ((ss->ss_mode & (XFR_MODE_READ | XFR_MODE_DMA_EN)) ==
948 		    (XFR_MODE_READ | XFR_MODE_DMA_EN)) {
949 			(void) ddi_dma_sync(ss->ss_bufdmah, 0, 0,
950 			    DDI_DMA_SYNC_FORKERNEL);
951 			bcopy(ss->ss_bounce, ss->ss_kvaddr, ss->ss_rcnt);
952 			ss->ss_rcnt = 0;
953 		}
954 		PUT16(ss, REG_INT_STAT, INT_XFR);
955 
956 		sdhost_xfer_done(ss, SDA_EOK);
957 	}
958 
959 	if (intr & INT_ERR) {
960 		PUT16(ss, REG_ERR_STAT, errs);
961 		PUT16(ss, REG_INT_STAT, INT_ERR);
962 
963 		if (errs & ERR_DAT) {
964 			if ((errs & ERR_DAT_END) == ERR_DAT_END) {
965 				sdhost_xfer_done(ss, SDA_EPROTO);
966 			} else if ((errs & ERR_DAT_CRC) == ERR_DAT_CRC) {
967 				sdhost_xfer_done(ss, SDA_ECRC7);
968 			} else {
969 				sdhost_xfer_done(ss, SDA_ETIME);
970 			}
971 
972 		} else if (errs & ERR_ACMD12) {
973 			/*
974 			 * Generally, this is bad news.  we need a full
975 			 * reset to recover properly.
976 			 */
977 			sdhost_xfer_done(ss, SDA_ECMD12);
978 		}
979 
980 		/*
981 		 * This asynchronous error leaves the slot more or less
982 		 * useless.  Report it to the framework.
983 		 */
984 		if (errs & ERR_CURRENT) {
985 			sda_host_fault(ss->ss_host, ss->ss_num,
986 			    SDA_FAULT_CURRENT);
987 		}
988 	}
989 
990 	mutex_exit(&ss->ss_lock);
991 
992 	return (DDI_INTR_CLAIMED);
993 }
994 
995 /*ARGSUSED1*/
996 uint_t
997 sdhost_intr(caddr_t arg1, caddr_t arg2)
998 {
999 	sdhost_t	*shp = (void *)arg1;
1000 	int		rv = DDI_INTR_UNCLAIMED;
1001 	int		num;
1002 
1003 	/* interrupt for each of the slots present in the system */
1004 	for (num = 0; num < shp->sh_numslots; num++) {
1005 		if (sdhost_slot_intr(&shp->sh_slots[num]) ==
1006 		    DDI_INTR_CLAIMED) {
1007 			rv = DDI_INTR_CLAIMED;
1008 		}
1009 	}
1010 	return (rv);
1011 }
1012 
1013 int
1014 sdhost_init_slot(dev_info_t *dip, sdhost_t *shp, int num, int bar)
1015 {
1016 	sdslot_t	*ss;
1017 	uint32_t	capab;
1018 	uint32_t	clk;
1019 	char		ksname[16];
1020 	size_t		blen;
1021 	unsigned	ndmac;
1022 	int		rv;
1023 
1024 	/*
1025 	 * Register the private state.
1026 	 */
1027 	ss = &shp->sh_slots[num];
1028 	ss->ss_host = shp->sh_host;
1029 	ss->ss_num = num;
1030 	sda_host_set_private(shp->sh_host, num, ss);
1031 	/*
1032 	 * Initialize core data structure, locks, etc.
1033 	 */
1034 	mutex_init(&ss->ss_lock, NULL, MUTEX_DRIVER,
1035 	    DDI_INTR_PRI(shp->sh_ipri));
1036 
1037 	/*
1038 	 * Set up DMA.
1039 	 */
1040 	rv = ddi_dma_alloc_handle(dip, &shp->sh_dmaattr,
1041 	    DDI_DMA_SLEEP, NULL, &ss->ss_bufdmah);
1042 	if (rv != DDI_SUCCESS) {
1043 		cmn_err(CE_WARN, "Failed to alloc dma handle (%d)!", rv);
1044 		return (DDI_FAILURE);
1045 	}
1046 
1047 	rv = ddi_dma_mem_alloc(ss->ss_bufdmah, SDHOST_BOUNCESZ,
1048 	    &sdhost_bufattr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
1049 	    &ss->ss_bounce, &blen, &ss->ss_bufacch);
1050 	if (rv != DDI_SUCCESS) {
1051 		cmn_err(CE_WARN, "Failed to alloc bounce buffer (%d)!", rv);
1052 		return (DDI_FAILURE);
1053 	}
1054 
1055 	rv = ddi_dma_addr_bind_handle(ss->ss_bufdmah, NULL, ss->ss_bounce,
1056 	    blen, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
1057 	    &ss->ss_bufdmac, &ndmac);
1058 	if ((rv != DDI_DMA_MAPPED) || (ndmac != 1)) {
1059 		cmn_err(CE_WARN, "Failed to bind DMA bounce buffer (%d, %u)!",
1060 		    rv, ndmac);
1061 		return (DDI_FAILURE);
1062 	}
1063 
1064 	/*
1065 	 * Set up virtual kstats.
1066 	 */
1067 	(void) snprintf(ksname, sizeof (ksname), "slot%d", num);
1068 	ss->ss_ksp = kstat_create(ddi_driver_name(dip), ddi_get_instance(dip),
1069 	    ksname, "misc", KSTAT_TYPE_NAMED,
1070 	    sizeof (sdstats_t) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
1071 	if (ss->ss_ksp != NULL) {
1072 		sdstats_t	*sp = &ss->ss_stats;
1073 		ss->ss_ksp->ks_data = sp;
1074 		ss->ss_ksp->ks_private = ss;
1075 		ss->ss_ksp->ks_lock = &ss->ss_lock;
1076 		/* counters are 64 bits wide */
1077 		kstat_named_init(&sp->ks_ncmd, "ncmd", KSTAT_DATA_UINT64);
1078 		kstat_named_init(&sp->ks_ixfr, "ixfr", KSTAT_DATA_UINT64);
1079 		kstat_named_init(&sp->ks_oxfr, "oxfr", KSTAT_DATA_UINT64);
1080 		kstat_named_init(&sp->ks_ibytes, "ibytes", KSTAT_DATA_UINT64);
1081 		kstat_named_init(&sp->ks_obytes, "obytes", KSTAT_DATA_UINT64);
1082 		kstat_named_init(&sp->ks_npio, "npio", KSTAT_DATA_UINT64);
1083 		kstat_named_init(&sp->ks_ndma, "ndma", KSTAT_DATA_UINT64);
1084 		kstat_named_init(&sp->ks_nmulti, "nmulti", KSTAT_DATA_UINT64);
1085 		/* these aren't counters -- leave them at 32 bits */
1086 		kstat_named_init(&sp->ks_baseclk, "baseclk", KSTAT_DATA_UINT32);
1087 		kstat_named_init(&sp->ks_cardclk, "cardclk", KSTAT_DATA_UINT32);
1088 		kstat_named_init(&sp->ks_tmusecs, "tmusecs", KSTAT_DATA_UINT32);
1089 		kstat_named_init(&sp->ks_width, "width", KSTAT_DATA_UINT32);
1090 		kstat_named_init(&sp->ks_flags, "flags", KSTAT_DATA_UINT32);
1091 		kstat_named_init(&sp->ks_capab, "capab", KSTAT_DATA_UINT32);
1092 		kstat_install(ss->ss_ksp);
1093 	}
1094 
1095 	if (PROPSET(SDHOST_PROP_FORCE_PIO)) {
1096 		ss->ss_flags |= SDFLAG_FORCE_PIO;
1097 	}
1098 	if (PROPSET(SDHOST_PROP_FORCE_DMA)) {
1099 		ss->ss_flags |= SDFLAG_FORCE_DMA;
1100 	}
1101 
1102 	if (ddi_regs_map_setup(dip, bar, &ss->ss_regva, 0, 0, &sdhost_regattr,
1103 	    &ss->ss_acch) != DDI_SUCCESS) {
1104 		cmn_err(CE_WARN, "Failed to map registers!");
1105 		return (DDI_FAILURE);
1106 	}
1107 
1108 	/* reset before reading capabilities */
1109 	if (sdhost_soft_reset(ss, SOFT_RESET_ALL) != SDA_EOK)
1110 		return (DDI_FAILURE);
1111 
1112 	capab = GET64(ss, REG_CAPAB) & 0xffffffffU; /* upper bits reserved */
1113 	ss->ss_capab = capab;
1114 
1115 	/* host voltages in OCR format */
1116 	ss->ss_ocr = 0;
1117 	if (capab & CAPAB_18V)
1118 		ss->ss_ocr |= OCR_18_19V;	/* 1.8V */
1119 	if (capab & CAPAB_30V)
1120 		ss->ss_ocr |= OCR_30_31V;
1121 	if (capab & CAPAB_33V)
1122 		ss->ss_ocr |= OCR_32_33V;
1123 
1124 	/* base clock */
1125 	ss->ss_baseclk =
1126 	    ((capab & CAPAB_BASE_FREQ_MASK) >> CAPAB_BASE_FREQ_SHIFT);
1127 	ss->ss_baseclk *= 1000000;
1128 
1129 	/*
1130 	 * Timeout clock.  We can calculate this using the following
1131 	 * formula:
1132 	 *
1133 	 * (1000000 usec/1sec) * (1sec/tmoutclk) * base factor = clock time
1134 	 *
1135 	 * Clock time is the length of the base clock in usecs.
1136 	 *
1137 	 * Our base factor is 2^13, which is the shortest clock we
1138 	 * can count.
1139 	 *
1140 	 * To simplify the math and avoid overflow, we cancel out the
1141 	 * zeros for kHz or MHz.  Since we want to wait more clocks, not
1142 	 * less, on error, we truncate the result rather than rounding
1143 	 * up.
1144 	 */
1145 	clk = ((capab & CAPAB_TIMEOUT_FREQ_MASK) >> CAPAB_TIMEOUT_FREQ_SHIFT);
1146 	if ((ss->ss_baseclk == 0) || (clk == 0)) {
1147 		cmn_err(CE_WARN, "Unable to determine clock frequencies");
1148 		return (DDI_FAILURE);
1149 	}
1150 
1151 	if (capab & CAPAB_TIMEOUT_UNITS) {
1152 		/* MHz */
1153 		ss->ss_tmusecs = (1 << 13) / clk;
1154 		clk *= 1000000;
1155 	} else {
1156 		/* kHz */
1157 		ss->ss_tmusecs = (1000 * (1 << 13)) / clk;
1158 		clk *= 1000;
1159 	}
1160 
1161 	/*
1162 	 * Calculation of the timeout.
1163 	 *
1164 	 * SDIO cards use a 1sec timeout, and SDHC cards use fixed
1165 	 * 100msec for read and 250 msec for write.
1166 	 *
1167 	 * Legacy cards running at 375kHz have a worst case of about
1168 	 * 15 seconds.  Running at 25MHz (the standard speed) it is
1169 	 * about 100msec for read, and about 3.2 sec for write.
1170 	 * Typical values are 1/100th that, or about 1msec for read,
1171 	 * and 32 msec for write.
1172 	 *
1173 	 * No transaction at full speed should ever take more than 4
1174 	 * seconds.  (Some slow legacy cards might have trouble, but
1175 	 * we'll worry about them if they ever are seen.  Nobody wants
1176 	 * to wait 4 seconds to access a single block anyway!)
1177 	 *
1178 	 * To get to 4 seconds, we continuously double usec until we
1179 	 * get to the maximum value, or a timeout greater than 4
1180 	 * seconds.
1181 	 *
1182 	 * Note that for high-speed timeout clocks, we might not be
1183 	 * able to get to the full 4 seconds.  E.g. with a 48MHz
1184 	 * timeout clock, we can only get to about 2.8 seconds.  Its
1185 	 * possible that there could be some slow MMC cards that will
1186 	 * timeout at this clock rate, but it seems unlikely.  (The
1187 	 * device would have to be pressing the very worst times,
1188 	 * against the 100-fold "permissive" window allowed, and
1189 	 * running at only 12.5MHz.)
1190 	 *
1191 	 * XXX: this could easily be a tunable.  Someone dealing with only
1192 	 * reasonable cards could set this to just 1 second.
1193 	 */
1194 	for (ss->ss_tmoutclk = 0; ss->ss_tmoutclk < 14; ss->ss_tmoutclk++) {
1195 		if ((ss->ss_tmusecs * (1 << ss->ss_tmoutclk)) >= 4000000) {
1196 			break;
1197 		}
1198 	}
1199 
1200 	/*
1201 	 * Enable slot interrupts.
1202 	 */
1203 	sdhost_enable_interrupts(ss);
1204 
1205 	return (DDI_SUCCESS);
1206 }
1207 
1208 void
1209 sdhost_uninit_slot(sdhost_t *shp, int num)
1210 {
1211 	sdslot_t	*ss;
1212 
1213 	ss = &shp->sh_slots[num];
1214 	if (ss->ss_acch == NULL)
1215 		return;
1216 
1217 	(void) sdhost_soft_reset(ss, SOFT_RESET_ALL);
1218 
1219 	if (ss->ss_bufdmac.dmac_address) {
1220 		(void) ddi_dma_unbind_handle(ss->ss_bufdmah);
1221 	}
1222 	if (ss->ss_bufacch != NULL) {
1223 		ddi_dma_mem_free(&ss->ss_bufacch);
1224 	}
1225 	if (ss->ss_bufdmah != NULL) {
1226 		ddi_dma_free_handle(&ss->ss_bufdmah);
1227 	}
1228 	if (ss->ss_ksp != NULL) {
1229 		kstat_delete(ss->ss_ksp);
1230 		ss->ss_ksp = NULL;
1231 	}
1232 
1233 	ddi_regs_map_free(&ss->ss_acch);
1234 	mutex_destroy(&ss->ss_lock);
1235 }
1236 
1237 void
1238 sdhost_get_response(sdslot_t *ss, sda_cmd_t *cmdp)
1239 {
1240 	uint32_t	*resp = cmdp->sc_response;
1241 	int		i;
1242 
1243 	resp[0] = GET32(ss, REG_RESP1);
1244 	resp[1] = GET32(ss, REG_RESP2);
1245 	resp[2] = GET32(ss, REG_RESP3);
1246 	resp[3] = GET32(ss, REG_RESP4);
1247 
1248 	/*
1249 	 * Response 2 is goofy because the host drops the low
1250 	 * order CRC bits.  This makes it a bit awkward, so we
1251 	 * have to shift the bits to make it work out right.
1252 	 *
1253 	 * Note that the framework expects the 32 bit
1254 	 * words to be ordered in LE fashion.  (The
1255 	 * bits within the words are in native order).
1256 	 */
1257 	if (cmdp->sc_rtype == R2) {
1258 		for (i = 3; i > 0; i--) {
1259 			resp[i] <<= 8;
1260 			resp[i] |= (resp[i - 1] >> 24);
1261 		}
1262 		resp[0] <<= 8;
1263 	}
1264 }
1265 
1266 sda_err_t
1267 sdhost_wait_cmd(sdslot_t *ss, sda_cmd_t *cmdp)
1268 {
1269 	int		i;
1270 	uint16_t	errs;
1271 	sda_err_t	rv;
1272 
1273 	/*
1274 	 * Worst case for 100kHz timeout is 2msec (200 clocks), we add
1275 	 * a tiny bit for safety.  (Generally timeout will be far, far
1276 	 * less than that.)
1277 	 *
1278 	 * Note that at more typical 12MHz (and normally it will be
1279 	 * even faster than that!) that the device timeout is only
1280 	 * 16.67 usec.  We could be smarter and reduce the delay time,
1281 	 * but that would require putting more intelligence into the
1282 	 * code, and we don't expect CMD timeout to normally occur
1283 	 * except during initialization.  (At which time we need the
1284 	 * full timeout anyway.)
1285 	 *
1286 	 * Checking the ERR_STAT will normally cause the timeout to
1287 	 * terminate to finish early if the device is healthy, anyway.
1288 	 */
1289 
1290 	for (i = 3000; i > 0; i -= 5) {
1291 		if (GET16(ss, REG_INT_STAT) & INT_CMD) {
1292 
1293 			PUT16(ss, REG_INT_STAT, INT_CMD);
1294 
1295 			/* command completed */
1296 			sdhost_get_response(ss, cmdp);
1297 			return (SDA_EOK);
1298 		}
1299 
1300 		if ((errs = (GET16(ss, REG_ERR_STAT) & ERR_CMD)) != 0) {
1301 			PUT16(ss, REG_ERR_STAT, errs);
1302 
1303 			/* command timeout isn't a host failure */
1304 			if ((errs & ERR_CMD_TMO) == ERR_CMD_TMO) {
1305 				rv = SDA_ETIME;
1306 			} else if ((errs & ERR_CMD_CRC) == ERR_CMD_CRC) {
1307 				rv = SDA_ECRC7;
1308 			} else {
1309 				rv = SDA_EPROTO;
1310 			}
1311 			goto error;
1312 		}
1313 
1314 		drv_usecwait(5);
1315 	}
1316 
1317 	rv = SDA_ETIME;
1318 
1319 error:
1320 	/*
1321 	 * NB: We need to soft reset the CMD and DAT
1322 	 * lines after a failure of this sort.
1323 	 */
1324 	(void) sdhost_soft_reset(ss, SOFT_RESET_CMD);
1325 	(void) sdhost_soft_reset(ss, SOFT_RESET_DAT);
1326 
1327 	return (rv);
1328 }
1329 
1330 sda_err_t
1331 sdhost_poll(void *arg)
1332 {
1333 	sdslot_t	*ss = arg;
1334 
1335 	(void) sdhost_slot_intr(ss);
1336 	return (SDA_EOK);
1337 }
1338 
1339 sda_err_t
1340 sdhost_cmd(void *arg, sda_cmd_t *cmdp)
1341 {
1342 	sdslot_t	*ss = arg;
1343 	uint16_t	command;
1344 	uint16_t	mode;
1345 	sda_err_t	rv;
1346 
1347 	/*
1348 	 * Command register:
1349 	 * bit 13-8	= command index
1350 	 * bit 7-6	= command type (always zero for us!)
1351 	 * bit 5	= data present select
1352 	 * bit 4	= command index check (always on!)
1353 	 * bit 3	= command CRC check enable
1354 	 * bit 2	= reserved
1355 	 * bit 1-0	= response type
1356 	 */
1357 
1358 	command = ((uint16_t)cmdp->sc_index << 8);
1359 	command |= COMMAND_TYPE_NORM |
1360 	    COMMAND_INDEX_CHECK_EN | COMMAND_CRC_CHECK_EN;
1361 
1362 	switch (cmdp->sc_rtype) {
1363 	case R0:
1364 		command |= COMMAND_RESP_NONE;
1365 		break;
1366 	case R1:
1367 	case R5:
1368 	case R6:
1369 	case R7:
1370 		command |= COMMAND_RESP_48;
1371 		break;
1372 	case R1b:
1373 	case R5b:
1374 		command |= COMMAND_RESP_48_BUSY;
1375 		break;
1376 	case R2:
1377 		command |= COMMAND_RESP_136;
1378 		command &= ~(COMMAND_INDEX_CHECK_EN | COMMAND_CRC_CHECK_EN);
1379 		break;
1380 	case R3:
1381 	case R4:
1382 		command |= COMMAND_RESP_48;
1383 		command &= ~COMMAND_CRC_CHECK_EN;
1384 		command &= ~COMMAND_INDEX_CHECK_EN;
1385 		break;
1386 	default:
1387 		return (SDA_EINVAL);
1388 	}
1389 
1390 	mutex_enter(&ss->ss_lock);
1391 	if (ss->ss_suspended) {
1392 		mutex_exit(&ss->ss_lock);
1393 		return (SDA_ESUSPENDED);
1394 	}
1395 
1396 	if (cmdp->sc_nblks != 0) {
1397 		uint16_t	blksz;
1398 		uint16_t	nblks;
1399 
1400 		blksz = cmdp->sc_blksz;
1401 		nblks = cmdp->sc_nblks;
1402 
1403 		/*
1404 		 * Ensure that we have good data.
1405 		 */
1406 		if ((blksz < 1) || (blksz > 2048)) {
1407 			mutex_exit(&ss->ss_lock);
1408 			return (SDA_EINVAL);
1409 		}
1410 		command |= COMMAND_DATA_PRESENT;
1411 
1412 		ss->ss_blksz = blksz;
1413 
1414 		ss->ss_kvaddr = (void *)cmdp->sc_kvaddr;
1415 		ss->ss_rcnt = 0;
1416 		ss->ss_resid = 0;
1417 
1418 		/*
1419 		 * Only SDMA for now.  We can investigate ADMA2 later.
1420 		 * (Right now we don't have ADMA2 capable hardware.)
1421 		 * We always use a bounce buffer, which solves weird
1422 		 * problems with certain controllers.  Doing this with
1423 		 * a large contiguous buffer may be faster than
1424 		 * servicing all the little per-page interrupts
1425 		 * anyway. (Bcopy of 64 K vs. 16 interrupts.)
1426 		 */
1427 		if (((ss->ss_capab & CAPAB_SDMA) != 0) &&
1428 		    ((ss->ss_flags & SDFLAG_FORCE_PIO) == 0) &&
1429 		    ((blksz * nblks) <= SDHOST_BOUNCESZ)) {
1430 
1431 			if (cmdp->sc_flags & SDA_CMDF_WRITE) {
1432 				/*
1433 				 * if we're writing, prepare initial round
1434 				 * of data
1435 				 */
1436 				bcopy(cmdp->sc_kvaddr, ss->ss_bounce,
1437 				    nblks * blksz);
1438 				(void) ddi_dma_sync(ss->ss_bufdmah, 0, 0,
1439 				    DDI_DMA_SYNC_FORDEV);
1440 			} else {
1441 				ss->ss_rcnt = nblks * blksz;
1442 			}
1443 			PUT32(ss, REG_SDMA_ADDR, ss->ss_bufdmac.dmac_address);
1444 			mode = XFR_MODE_DMA_EN;
1445 			PUT16(ss, REG_BLKSZ, BLKSZ_BOUNDARY_512K | blksz);
1446 			ss->ss_ndma++;
1447 
1448 		} else {
1449 			mode = 0;
1450 			ss->ss_npio++;
1451 			ss->ss_resid = nblks;
1452 			PUT16(ss, REG_BLKSZ, blksz);
1453 		}
1454 
1455 		if (nblks > 1) {
1456 			mode |= XFR_MODE_MULTI | XFR_MODE_COUNT;
1457 			if (cmdp->sc_flags & SDA_CMDF_AUTO_CMD12)
1458 				mode |= XFR_MODE_AUTO_CMD12;
1459 			ss->ss_nmulti++;
1460 		}
1461 		if ((cmdp->sc_flags & SDA_CMDF_READ) != 0) {
1462 			mode |= XFR_MODE_READ;
1463 			ss->ss_ixfr++;
1464 			ss->ss_ibytes += nblks * blksz;
1465 		} else {
1466 			ss->ss_oxfr++;
1467 			ss->ss_obytes += nblks * blksz;
1468 		}
1469 
1470 		ss->ss_mode = mode;
1471 
1472 		PUT8(ss, REG_TIMEOUT_CONTROL, ss->ss_tmoutclk);
1473 		PUT16(ss, REG_BLOCK_COUNT, nblks);
1474 		PUT16(ss, REG_XFR_MODE, mode);
1475 	}
1476 
1477 	PUT32(ss, REG_ARGUMENT, cmdp->sc_argument);
1478 	PUT16(ss, REG_COMMAND, command);
1479 
1480 	ss->ss_ncmd++;
1481 	rv = sdhost_wait_cmd(ss, cmdp);
1482 
1483 	mutex_exit(&ss->ss_lock);
1484 
1485 	return (rv);
1486 }
1487 
1488 sda_err_t
1489 sdhost_getprop(void *arg, sda_prop_t prop, uint32_t *val)
1490 {
1491 	sdslot_t	*ss = arg;
1492 	sda_err_t	rv = 0;
1493 
1494 	mutex_enter(&ss->ss_lock);
1495 
1496 	if (ss->ss_suspended) {
1497 		mutex_exit(&ss->ss_lock);
1498 		return (SDA_ESUSPENDED);
1499 	}
1500 	switch (prop) {
1501 	case SDA_PROP_INSERTED:
1502 		if (CHECK_STATE(ss, CARD_INSERTED)) {
1503 			*val = B_TRUE;
1504 		} else {
1505 			*val = B_FALSE;
1506 		}
1507 		break;
1508 
1509 	case SDA_PROP_WPROTECT:
1510 		if (CHECK_STATE(ss, WRITE_ENABLE)) {
1511 			*val = B_FALSE;
1512 		} else {
1513 			*val = B_TRUE;
1514 		}
1515 		break;
1516 
1517 	case SDA_PROP_OCR:
1518 		*val = ss->ss_ocr;
1519 		break;
1520 
1521 	case SDA_PROP_CLOCK:
1522 		*val = ss->ss_cardclk;
1523 		break;
1524 
1525 	case SDA_PROP_CAP_HISPEED:
1526 		if ((ss->ss_capab & CAPAB_HIGH_SPEED) != 0) {
1527 			*val = B_TRUE;
1528 		} else {
1529 			*val = B_FALSE;
1530 		}
1531 		break;
1532 
1533 	case SDA_PROP_CAP_4BITS:
1534 		*val = B_TRUE;
1535 		break;
1536 
1537 	case SDA_PROP_CAP_NOPIO:
1538 		/*
1539 		 * We might have to use PIO for buffers that don't
1540 		 * have reasonable alignments.  A few controllers seem
1541 		 * not to deal with granularity or alignments of
1542 		 * something other 32-bits.
1543 		 */
1544 		*val = B_FALSE;
1545 		break;
1546 
1547 	case SDA_PROP_CAP_INTR:
1548 	case SDA_PROP_CAP_8BITS:
1549 		*val = B_FALSE;
1550 		break;
1551 
1552 	default:
1553 		rv = SDA_ENOTSUP;
1554 		break;
1555 	}
1556 	mutex_exit(&ss->ss_lock);
1557 
1558 	return (rv);
1559 }
1560 
1561 sda_err_t
1562 sdhost_setprop(void *arg, sda_prop_t prop, uint32_t val)
1563 {
1564 	sdslot_t	*ss = arg;
1565 	sda_err_t	rv = SDA_EOK;
1566 
1567 	mutex_enter(&ss->ss_lock);
1568 
1569 	if (ss->ss_suspended) {
1570 		mutex_exit(&ss->ss_lock);
1571 		return (SDA_ESUSPENDED);
1572 	}
1573 
1574 	switch (prop) {
1575 	case SDA_PROP_LED:
1576 		if (val) {
1577 			SET8(ss, REG_HOST_CONTROL, HOST_CONTROL_LED_ON);
1578 		} else {
1579 			CLR8(ss, REG_HOST_CONTROL, HOST_CONTROL_LED_ON);
1580 		}
1581 		break;
1582 
1583 	case SDA_PROP_CLOCK:
1584 		rv = sdhost_set_clock(arg, val);
1585 		break;
1586 
1587 	case SDA_PROP_BUSWIDTH:
1588 		switch (val) {
1589 		case 1:
1590 			ss->ss_width = val;
1591 			CLR8(ss, REG_HOST_CONTROL, HOST_CONTROL_DATA_WIDTH);
1592 			break;
1593 		case 4:
1594 			ss->ss_width = val;
1595 			SET8(ss, REG_HOST_CONTROL, HOST_CONTROL_DATA_WIDTH);
1596 			break;
1597 		default:
1598 			rv = SDA_EINVAL;
1599 		}
1600 		break;
1601 
1602 	case SDA_PROP_OCR:
1603 		val &= ss->ss_ocr;
1604 
1605 		if (val & OCR_17_18V) {
1606 			PUT8(ss, REG_POWER_CONTROL, POWER_CONTROL_18V);
1607 			PUT8(ss, REG_POWER_CONTROL, POWER_CONTROL_18V |
1608 			    POWER_CONTROL_BUS_POWER);
1609 		} else if (val & OCR_29_30V) {
1610 			PUT8(ss, REG_POWER_CONTROL, POWER_CONTROL_30V);
1611 			PUT8(ss, REG_POWER_CONTROL, POWER_CONTROL_30V |
1612 			    POWER_CONTROL_BUS_POWER);
1613 		} else if (val & OCR_32_33V) {
1614 			PUT8(ss, REG_POWER_CONTROL, POWER_CONTROL_33V);
1615 			PUT8(ss, REG_POWER_CONTROL, POWER_CONTROL_33V |
1616 			    POWER_CONTROL_BUS_POWER);
1617 		} else if (val == 0) {
1618 			/* turn off power */
1619 			PUT8(ss, REG_POWER_CONTROL, 0);
1620 		} else {
1621 			rv = SDA_EINVAL;
1622 		}
1623 		break;
1624 
1625 	case SDA_PROP_HISPEED:
1626 		if (val) {
1627 			SET8(ss, REG_HOST_CONTROL, HOST_CONTROL_HIGH_SPEED_EN);
1628 		} else {
1629 			CLR8(ss, REG_HOST_CONTROL, HOST_CONTROL_HIGH_SPEED_EN);
1630 		}
1631 		/* give clocks time to settle */
1632 		drv_usecwait(10);
1633 		break;
1634 
1635 	default:
1636 		rv = SDA_ENOTSUP;
1637 		break;
1638 	}
1639 
1640 	/*
1641 	 * Apparently some controllers (ENE) have issues with changing
1642 	 * certain parameters (bus width seems to be one), requiring
1643 	 * a reset of the DAT and CMD lines.
1644 	 */
1645 	if (rv == SDA_EOK) {
1646 		(void) sdhost_soft_reset(ss, SOFT_RESET_CMD);
1647 		(void) sdhost_soft_reset(ss, SOFT_RESET_DAT);
1648 	}
1649 	mutex_exit(&ss->ss_lock);
1650 	return (rv);
1651 }
1652 
1653 sda_err_t
1654 sdhost_reset(void *arg)
1655 {
1656 	sdslot_t	*ss = arg;
1657 
1658 	mutex_enter(&ss->ss_lock);
1659 	if (!ss->ss_suspended) {
1660 		if (sdhost_soft_reset(ss, SOFT_RESET_ALL) != SDA_EOK) {
1661 			mutex_exit(&ss->ss_lock);
1662 			return (SDA_ETIME);
1663 		}
1664 		sdhost_enable_interrupts(ss);
1665 	}
1666 	mutex_exit(&ss->ss_lock);
1667 	return (SDA_EOK);
1668 }
1669 
1670 sda_err_t
1671 sdhost_halt(void *arg)
1672 {
1673 	sdslot_t	*ss = arg;
1674 
1675 	mutex_enter(&ss->ss_lock);
1676 	if (!ss->ss_suspended) {
1677 		sdhost_disable_interrupts(ss);
1678 		/* this has the side effect of removing power from the card */
1679 		if (sdhost_soft_reset(ss, SOFT_RESET_ALL) != SDA_EOK) {
1680 			mutex_exit(&ss->ss_lock);
1681 			return (SDA_ETIME);
1682 		}
1683 	}
1684 	mutex_exit(&ss->ss_lock);
1685 	return (SDA_EOK);
1686 }
1687