kernel: Add descriptions to the intrhooks that miss them.
[dragonfly.git] / sys / bus / mmc / mmc.c
CommitLineData
d6644679
SW
1/*-
2 * Copyright (c) 2006 Bernd Walter. All rights reserved.
3 * Copyright (c) 2006 M. Warner Losh. 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 * Portions of this software may have been developed with reference to
26 * the SD Simplified Specification. The following disclaimer may apply:
27 *
28 * The following conditions apply to the release of the simplified
29 * specification ("Simplified Specification") by the SD Card Association and
30 * the SD Group. The Simplified Specification is a subset of the complete SD
31 * Specification which is owned by the SD Card Association and the SD
32 * Group. This Simplified Specification is provided on a non-confidential
33 * basis subject to the disclaimers below. Any implementation of the
34 * Simplified Specification may require a license from the SD Card
35 * Association, SD Group, SD-3C LLC or other third parties.
36 *
37 * Disclaimers:
38 *
39 * The information contained in the Simplified Specification is presented only
40 * as a standard specification for SD Cards and SD Host/Ancillary products and
41 * is provided "AS-IS" without any representations or warranties of any
42 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
43 * Card Association for any damages, any infringements of patents or other
44 * right of the SD Group, SD-3C LLC, the SD Card Association or any third
45 * parties, which may result from its use. No license is granted by
46 * implication, estoppel or otherwise under any patent or other rights of the
47 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49 * or the SD Card Association to disclose or distribute any technical
50 * information, know-how or other confidential information to any third party.
51 *
52 * $FreeBSD: src/sys/dev/mmc/mmc.c,v 1.38 2009/08/20 19:17:53 jhb Exp $
53 */
54
55#include <sys/param.h>
56#include <sys/systm.h>
57#include <sys/kernel.h>
58#include <sys/malloc.h>
59#include <sys/lock.h>
60#include <sys/module.h>
61#include <sys/spinlock.h>
62#include <sys/bus.h>
63#include <sys/endian.h>
64#include <sys/sysctl.h>
65
66#include <bus/mmc/mmcreg.h>
67#include <bus/mmc/mmcbrvar.h>
68#include <bus/mmc/mmcvar.h>
69#include "mmcbr_if.h"
70#include "mmcbus_if.h"
71
72struct mmc_softc {
73 device_t dev;
74 struct lock sc_lock;
75 struct intr_config_hook config_intrhook;
76 device_t owner;
77 uint32_t last_rca;
78};
79
80/*
81 * Per-card data
82 */
83struct mmc_ivars {
84 uint32_t raw_cid[4]; /* Raw bits of the CID */
85 uint32_t raw_csd[4]; /* Raw bits of the CSD */
86 uint32_t raw_scr[2]; /* Raw bits of the SCR */
87 uint8_t raw_ext_csd[512]; /* Raw bits of the EXT_CSD */
88 uint32_t raw_sd_status[16]; /* Raw bits of the SD_STATUS */
89 uint16_t rca;
90 enum mmc_card_mode mode;
91 struct mmc_cid cid; /* cid decoded */
92 struct mmc_csd csd; /* csd decoded */
93 struct mmc_scr scr; /* scr decoded */
94 struct mmc_sd_status sd_status; /* SD_STATUS decoded */
95 u_char read_only; /* True when the device is read-only */
96 u_char bus_width; /* Bus width to use */
97 u_char timing; /* Bus timing support */
98 u_char high_cap; /* High Capacity card (block addressed) */
99 uint32_t sec_count; /* Card capacity in 512byte blocks */
100 uint32_t tran_speed; /* Max speed in normal mode */
101 uint32_t hs_tran_speed; /* Max speed in high speed mode */
102 uint32_t erase_sector; /* Card native erase sector size */
103};
104
105#define CMD_RETRIES 3
106
107SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
108
109static int mmc_debug;
110SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level");
111
112/* bus entry points */
113static int mmc_probe(device_t dev);
114static int mmc_attach(device_t dev);
115static int mmc_detach(device_t dev);
116static int mmc_suspend(device_t dev);
117static int mmc_resume(device_t dev);
118
119#define MMC_LOCK(_sc) lockmgr(&(_sc)->sc_lock, LK_EXCLUSIVE)
120#define MMC_UNLOCK(_sc) lockmgr(&(_sc)->sc_lock, LK_RELEASE)
121#define MMC_LOCK_INIT(_sc) lockinit(&(_sc)->sc_lock, "mmc", 0, LK_CANRECURSE)
122#define MMC_LOCK_DESTROY(_sc) lockuninit(&(_sc)->sc_lock);
123#define MMC_ASSERT_LOCKED(_sc) KKASSERT(lockstatus(&(_sc)->sc_lock, curthread) != 0);
124#define MMC_ASSERT_UNLOCKED(_sc) KKASSERT(lockstatus(&(_sc)->sc_lock, curthread) == 0);
125
126static int mmc_calculate_clock(struct mmc_softc *sc);
127static void mmc_delayed_attach(void *);
128static void mmc_power_down(struct mmc_softc *sc);
129static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
130 int retries);
131static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
132 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
133static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
134static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width);
135static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr);
136static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
137static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
138static void mmc_scan(struct mmc_softc *sc);
139static int mmc_delete_cards(struct mmc_softc *sc);
140
141static void
142mmc_ms_delay(int ms)
143{
144 DELAY(1000 * ms); /* XXX BAD */
145}
146
147static int
148mmc_probe(device_t dev)
149{
150
151 device_set_desc(dev, "MMC/SD bus");
152 return (0);
153}
154
155static int
156mmc_attach(device_t dev)
157{
158 struct mmc_softc *sc;
159
160 sc = device_get_softc(dev);
161 sc->dev = dev;
162 MMC_LOCK_INIT(sc);
163
164 /* We'll probe and attach our children later, but before / mount */
165 sc->config_intrhook.ich_func = mmc_delayed_attach;
166 sc->config_intrhook.ich_arg = sc;
52800c9d 167 sc->config_intrhook.ich_desc = "mmc";
d6644679
SW
168 if (config_intrhook_establish(&sc->config_intrhook) != 0)
169 device_printf(dev, "config_intrhook_establish failed\n");
170 return (0);
171}
172
173static int
174mmc_detach(device_t dev)
175{
176 struct mmc_softc *sc = device_get_softc(dev);
177 int err;
178
179 if ((err = mmc_delete_cards(sc)) != 0)
180 return (err);
181 mmc_power_down(sc);
182 MMC_LOCK_DESTROY(sc);
183
184 return (0);
185}
186
187static int
188mmc_suspend(device_t dev)
189{
190 struct mmc_softc *sc = device_get_softc(dev);
191 int err;
192
193 err = bus_generic_suspend(dev);
194 if (err)
195 return (err);
196 mmc_power_down(sc);
197 return (0);
198}
199
200static int
201mmc_resume(device_t dev)
202{
203 struct mmc_softc *sc = device_get_softc(dev);
204
205 mmc_scan(sc);
206 return (bus_generic_resume(dev));
207}
208
209static int
210mmc_acquire_bus(device_t busdev, device_t dev)
211{
212 struct mmc_softc *sc;
213 struct mmc_ivars *ivar;
214 int err;
215 int rca;
216
217 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
218 if (err)
219 return (err);
220 sc = device_get_softc(busdev);
221 MMC_LOCK(sc);
222 if (sc->owner)
223 panic("mmc: host bridge didn't seralize us.");
224 sc->owner = dev;
225 MMC_UNLOCK(sc);
226
227 if (busdev != dev) {
228 /*
229 * Keep track of the last rca that we've selected. If
230 * we're asked to do it again, don't. We never
231 * unselect unless the bus code itself wants the mmc
232 * bus, and constantly reselecting causes problems.
233 */
234 rca = mmc_get_rca(dev);
235 if (sc->last_rca != rca) {
236 mmc_select_card(sc, rca);
237 sc->last_rca = rca;
238 /* Prepare bus width for the new card. */
239 ivar = device_get_ivars(dev);
240 if (bootverbose || mmc_debug) {
241 device_printf(busdev,
242 "setting bus width to %d bits\n",
243 (ivar->bus_width == bus_width_4) ? 4 :
244 (ivar->bus_width == bus_width_8) ? 8 : 1);
245 }
246 mmc_set_card_bus_width(sc, rca, ivar->bus_width);
247 mmcbr_set_bus_width(busdev, ivar->bus_width);
248 mmcbr_update_ios(busdev);
249 }
250 } else {
251 /*
252 * If there's a card selected, stand down.
253 */
254 if (sc->last_rca != 0) {
255 mmc_select_card(sc, 0);
256 sc->last_rca = 0;
257 }
258 }
259
260 return (0);
261}
262
263static int
264mmc_release_bus(device_t busdev, device_t dev)
265{
266 struct mmc_softc *sc;
267 int err;
268
269 sc = device_get_softc(busdev);
270
271 MMC_LOCK(sc);
272 if (!sc->owner)
273 panic("mmc: releasing unowned bus.");
274 if (sc->owner != dev)
275 panic("mmc: you don't own the bus. game over.");
276 MMC_UNLOCK(sc);
277 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
278 if (err)
279 return (err);
280 MMC_LOCK(sc);
281 sc->owner = NULL;
282 MMC_UNLOCK(sc);
283 return (0);
284}
285
286static uint32_t
287mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
288{
289
290 return (ocr & MMC_OCR_VOLTAGE);
291}
292
293static int
294mmc_highest_voltage(uint32_t ocr)
295{
296 int i;
297
298 for (i = 30; i >= 0; i--)
299 if (ocr & (1 << i))
300 return (i);
301 return (-1);
302}
303
304static void
305mmc_wakeup(struct mmc_request *req)
306{
307 struct mmc_softc *sc;
308
309 sc = (struct mmc_softc *)req->done_data;
310 MMC_LOCK(sc);
311 req->flags |= MMC_REQ_DONE;
312 MMC_UNLOCK(sc);
313 wakeup(req);
314}
315
316static int
317mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
318{
319
320 req->done = mmc_wakeup;
321 req->done_data = sc;
322 if (mmc_debug > 1) {
323 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
324 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
325 if (req->cmd->data) {
326 kprintf(" data %d\n", (int)req->cmd->data->len);
327 } else
328 kprintf("\n");
329 }
330 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
331 MMC_LOCK(sc);
332 while ((req->flags & MMC_REQ_DONE) == 0)
333 lksleep(req, &sc->sc_lock, 0, "mmcreq", 0);
334 MMC_UNLOCK(sc);
335 if (mmc_debug > 2 || (mmc_debug > 1 && req->cmd->error))
336 device_printf(sc->dev, "RESULT: %d\n", req->cmd->error);
337 return (0);
338}
339
340static int
341mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
342{
343 struct mmc_softc *sc = device_get_softc(brdev);
344
345 return (mmc_wait_for_req(sc, req));
346}
347
348static int
349mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
350{
351 struct mmc_request mreq;
352
353 memset(&mreq, 0, sizeof(mreq));
354 memset(cmd->resp, 0, sizeof(cmd->resp));
355 cmd->retries = retries;
356 mreq.cmd = cmd;
357 mmc_wait_for_req(sc, &mreq);
358 return (cmd->error);
359}
360
361static int
362mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
363 struct mmc_command *cmd, int retries)
364{
365 struct mmc_command appcmd;
366 int err = MMC_ERR_NONE, i;
367
368 for (i = 0; i <= retries; i++) {
369 appcmd.opcode = MMC_APP_CMD;
370 appcmd.arg = rca << 16;
371 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
372 appcmd.data = NULL;
373 mmc_wait_for_cmd(sc, &appcmd, 0);
374 err = appcmd.error;
375 if (err != MMC_ERR_NONE)
376 continue;
377 if (!(appcmd.resp[0] & R1_APP_CMD))
378 return MMC_ERR_FAILED;
379 mmc_wait_for_cmd(sc, cmd, 0);
380 err = cmd->error;
381 if (err == MMC_ERR_NONE)
382 break;
383 }
384 return (err);
385}
386
387static int
388mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
389 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
390{
391 struct mmc_command cmd;
392 int err;
393
394 memset(&cmd, 0, sizeof(cmd));
395 cmd.opcode = opcode;
396 cmd.arg = arg;
397 cmd.flags = flags;
398 cmd.data = NULL;
399 err = mmc_wait_for_cmd(sc, &cmd, retries);
400 if (err)
401 return (err);
402 if (cmd.error)
403 return (cmd.error);
404 if (resp) {
405 if (flags & MMC_RSP_136)
406 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
407 else
408 *resp = cmd.resp[0];
409 }
410 return (0);
411}
412
413static void
414mmc_idle_cards(struct mmc_softc *sc)
415{
416 device_t dev;
417 struct mmc_command cmd;
418
419 dev = sc->dev;
420 mmcbr_set_chip_select(dev, cs_high);
421 mmcbr_update_ios(dev);
422 mmc_ms_delay(1);
423
424 memset(&cmd, 0, sizeof(cmd));
425 cmd.opcode = MMC_GO_IDLE_STATE;
426 cmd.arg = 0;
427 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
428 cmd.data = NULL;
429 mmc_wait_for_cmd(sc, &cmd, 0);
430 mmc_ms_delay(1);
431
432 mmcbr_set_chip_select(dev, cs_dontcare);
433 mmcbr_update_ios(dev);
434 mmc_ms_delay(1);
435}
436
437static int
438mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
439{
440 struct mmc_command cmd;
441 int err = MMC_ERR_NONE, i;
442
443 memset(&cmd, 0, sizeof(cmd));
444 cmd.opcode = ACMD_SD_SEND_OP_COND;
445 cmd.arg = ocr;
446 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
447 cmd.data = NULL;
448
449 for (i = 0; i < 100; i++) {
450 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
451 if (err != MMC_ERR_NONE)
452 break;
453 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
454 (ocr & MMC_OCR_VOLTAGE) == 0)
455 break;
456 err = MMC_ERR_TIMEOUT;
457 mmc_ms_delay(10);
458 }
459 if (rocr && err == MMC_ERR_NONE)
460 *rocr = cmd.resp[0];
461 return (err);
462}
463
464static int
465mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
466{
467 struct mmc_command cmd;
468 int err = MMC_ERR_NONE, i;
469
470 memset(&cmd, 0, sizeof(cmd));
471 cmd.opcode = MMC_SEND_OP_COND;
472 cmd.arg = ocr;
473 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
474 cmd.data = NULL;
475
476 for (i = 0; i < 100; i++) {
477 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
478 if (err != MMC_ERR_NONE)
479 break;
480 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
481 (ocr & MMC_OCR_VOLTAGE) == 0)
482 break;
483 err = MMC_ERR_TIMEOUT;
484 mmc_ms_delay(10);
485 }
486 if (rocr && err == MMC_ERR_NONE)
487 *rocr = cmd.resp[0];
488 return (err);
489}
490
491static int
492mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
493{
494 struct mmc_command cmd;
495 int err;
496
497 memset(&cmd, 0, sizeof(cmd));
498 cmd.opcode = SD_SEND_IF_COND;
499 cmd.arg = (vhs << 8) + 0xAA;
500 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
501 cmd.data = NULL;
502
503 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
504 return (err);
505}
506
507static void
508mmc_power_up(struct mmc_softc *sc)
509{
510 device_t dev;
511
512 dev = sc->dev;
513 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
514 mmcbr_set_bus_mode(dev, opendrain);
515 mmcbr_set_chip_select(dev, cs_dontcare);
516 mmcbr_set_bus_width(dev, bus_width_1);
517 mmcbr_set_power_mode(dev, power_up);
518 mmcbr_set_clock(dev, 0);
519 mmcbr_update_ios(dev);
520 mmc_ms_delay(1);
521
522 mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev));
523 mmcbr_set_timing(dev, bus_timing_normal);
524 mmcbr_set_power_mode(dev, power_on);
525 mmcbr_update_ios(dev);
526 mmc_ms_delay(2);
527}
528
529static void
530mmc_power_down(struct mmc_softc *sc)
531{
532 device_t dev = sc->dev;
533
534 mmcbr_set_bus_mode(dev, opendrain);
535 mmcbr_set_chip_select(dev, cs_dontcare);
536 mmcbr_set_bus_width(dev, bus_width_1);
537 mmcbr_set_power_mode(dev, power_off);
538 mmcbr_set_clock(dev, 0);
539 mmcbr_set_timing(dev, bus_timing_normal);
540 mmcbr_update_ios(dev);
541}
542
543static int
544mmc_select_card(struct mmc_softc *sc, uint16_t rca)
545{
546 int flags;
547
548 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
549 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
550 flags, NULL, CMD_RETRIES));
551}
552
553static int
554mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
555{
556 struct mmc_command cmd;
557 int err;
558
559 cmd.opcode = MMC_SWITCH_FUNC;
560 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
561 (index << 16) |
562 (value << 8) |
563 set;
564 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
565 cmd.data = NULL;
566 err = mmc_wait_for_cmd(sc, &cmd, 0);
567 return (err);
568}
569
570static int
571mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
572 uint8_t *res)
573{
574 int err;
575 struct mmc_command cmd;
576 struct mmc_data data;
577
578 memset(&cmd, 0, sizeof(struct mmc_command));
579 memset(&data, 0, sizeof(struct mmc_data));
580 memset(res, 0, 64);
581
582 cmd.opcode = SD_SWITCH_FUNC;
583 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
584 cmd.arg = mode << 31; /* 0 - check, 1 - set */
585 cmd.arg |= 0x00FFFFFF;
586 cmd.arg &= ~(0xF << (grp * 4));
587 cmd.arg |= value << (grp * 4);
588 cmd.data = &data;
589
590 data.data = res;
591 data.len = 64;
592 data.flags = MMC_DATA_READ;
593
594 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
595 return (err);
596}
597
598static int
599mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
600{
601 struct mmc_command cmd;
602 int err;
603 uint8_t value;
604
605 if (mmcbr_get_mode(sc->dev) == mode_sd) {
606 memset(&cmd, 0, sizeof(struct mmc_command));
607 cmd.opcode = ACMD_SET_BUS_WIDTH;
608 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
609 switch (width) {
610 case bus_width_1:
611 cmd.arg = SD_BUS_WIDTH_1;
612 break;
613 case bus_width_4:
614 cmd.arg = SD_BUS_WIDTH_4;
615 break;
616 default:
617 return (MMC_ERR_INVALID);
618 }
619 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
620 } else {
621 switch (width) {
622 case bus_width_1:
623 value = EXT_CSD_BUS_WIDTH_1;
624 break;
625 case bus_width_4:
626 value = EXT_CSD_BUS_WIDTH_4;
627 break;
628 case bus_width_8:
629 value = EXT_CSD_BUS_WIDTH_8;
630 break;
631 default:
632 return (MMC_ERR_INVALID);
633 }
634 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
635 value);
636 }
637 return (err);
638}
639
640static int
641mmc_set_timing(struct mmc_softc *sc, int timing)
642{
643 int err;
644 uint8_t value;
645 u_char switch_res[64];
646
647 switch (timing) {
648 case bus_timing_normal:
649 value = 0;
650 break;
651 case bus_timing_hs:
652 value = 1;
653 break;
654 default:
655 return (MMC_ERR_INVALID);
656 }
657 if (mmcbr_get_mode(sc->dev) == mode_sd)
658 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
659 value, switch_res);
660 else
661 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
662 EXT_CSD_HS_TIMING, value);
663 return (err);
664}
665
666static int
667mmc_test_bus_width(struct mmc_softc *sc)
668{
669 struct mmc_command cmd;
670 struct mmc_data data;
671 int err;
672 uint8_t buf[8];
673 uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
674 uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
675 uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, };
676 uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
677
678 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
679 mmcbr_set_bus_width(sc->dev, bus_width_8);
680 mmcbr_update_ios(sc->dev);
681
682 cmd.opcode = MMC_BUSTEST_W;
683 cmd.arg = 0;
684 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
685 cmd.data = &data;
686
687 data.data = p8;
688 data.len = 8;
689 data.flags = MMC_DATA_WRITE;
690 mmc_wait_for_cmd(sc, &cmd, 0);
691
692 cmd.opcode = MMC_BUSTEST_R;
693 cmd.arg = 0;
694 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
695 cmd.data = &data;
696
697 data.data = buf;
698 data.len = 8;
699 data.flags = MMC_DATA_READ;
700 err = mmc_wait_for_cmd(sc, &cmd, 0);
701
702 mmcbr_set_bus_width(sc->dev, bus_width_1);
703 mmcbr_update_ios(sc->dev);
704
705 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
706 return (bus_width_8);
707 }
708
709 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
710 mmcbr_set_bus_width(sc->dev, bus_width_4);
711 mmcbr_update_ios(sc->dev);
712
713 cmd.opcode = MMC_BUSTEST_W;
714 cmd.arg = 0;
715 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
716 cmd.data = &data;
717
718 data.data = p4;
719 data.len = 4;
720 data.flags = MMC_DATA_WRITE;
721 mmc_wait_for_cmd(sc, &cmd, 0);
722
723 cmd.opcode = MMC_BUSTEST_R;
724 cmd.arg = 0;
725 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
726 cmd.data = &data;
727
728 data.data = buf;
729 data.len = 4;
730 data.flags = MMC_DATA_READ;
731 err = mmc_wait_for_cmd(sc, &cmd, 0);
732
733 mmcbr_set_bus_width(sc->dev, bus_width_1);
734 mmcbr_update_ios(sc->dev);
735
736 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
737 return (bus_width_4);
738 }
739 return (bus_width_1);
740}
741
742static uint32_t
743mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
744{
745 const int i = (bit_len / 32) - (start / 32) - 1;
746 const int shift = start & 31;
747 uint32_t retval = bits[i] >> shift;
748 if (size + shift > 32)
749 retval |= bits[i - 1] << (32 - shift);
750 return (retval & ((1 << size) - 1));
751}
752
753static void
754mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
755{
756 int i;
757
758 /* There's no version info, so we take it on faith */
759 memset(cid, 0, sizeof(*cid));
760 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
761 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
762 for (i = 0; i < 5; i++)
763 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
764 cid->pnm[5] = 0;
765 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
766 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
767 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
768 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
769}
770
771static void
772mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
773{
774 int i;
775
776 /* There's no version info, so we take it on faith */
777 memset(cid, 0, sizeof(*cid));
778 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
779 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
780 for (i = 0; i < 6; i++)
781 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
782 cid->pnm[6] = 0;
783 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
784 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
785 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
786 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
787}
788
789static const int exp[8] = {
790 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
791};
792static const int mant[16] = {
793 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
794};
795static const int cur_min[8] = {
796 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
797};
798static const int cur_max[8] = {
799 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
800};
801
802static void
803mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
804{
805 int v;
806 int m;
807 int e;
808
809 memset(csd, 0, sizeof(*csd));
810 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
811 if (v == 0) {
812 m = mmc_get_bits(raw_csd, 128, 115, 4);
813 e = mmc_get_bits(raw_csd, 128, 112, 3);
814 csd->tacc = exp[e] * mant[m] + 9 / 10;
815 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
816 m = mmc_get_bits(raw_csd, 128, 99, 4);
817 e = mmc_get_bits(raw_csd, 128, 96, 3);
818 csd->tran_speed = exp[e] * 10000 * mant[m];
819 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
820 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
821 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
822 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
823 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
824 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
825 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
826 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
827 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
828 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
829 m = mmc_get_bits(raw_csd, 128, 62, 12);
830 e = mmc_get_bits(raw_csd, 128, 47, 3);
831 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
832 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
833 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
834 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
835 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
836 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
837 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
838 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
839 } else if (v == 1) {
840 m = mmc_get_bits(raw_csd, 128, 115, 4);
841 e = mmc_get_bits(raw_csd, 128, 112, 3);
842 csd->tacc = exp[e] * mant[m] + 9 / 10;
843 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
844 m = mmc_get_bits(raw_csd, 128, 99, 4);
845 e = mmc_get_bits(raw_csd, 128, 96, 3);
846 csd->tran_speed = exp[e] * 10000 * mant[m];
847 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
848 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
849 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
850 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
851 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
852 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
853 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
854 512 * 1024;
855 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
856 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
857 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
858 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
859 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
860 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
861 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
862 } else
863 panic("unknown SD CSD version");
864}
865
866static void
867mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
868{
869 int m;
870 int e;
871
872 memset(csd, 0, sizeof(*csd));
873 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
874 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
875 m = mmc_get_bits(raw_csd, 128, 115, 4);
876 e = mmc_get_bits(raw_csd, 128, 112, 3);
877 csd->tacc = exp[e] * mant[m] + 9 / 10;
878 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
879 m = mmc_get_bits(raw_csd, 128, 99, 4);
880 e = mmc_get_bits(raw_csd, 128, 96, 3);
881 csd->tran_speed = exp[e] * 10000 * mant[m];
882 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
883 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
884 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
885 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
886 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
887 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
888 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
889 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
890 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
891 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
892 m = mmc_get_bits(raw_csd, 128, 62, 12);
893 e = mmc_get_bits(raw_csd, 128, 47, 3);
894 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
895 csd->erase_blk_en = 0;
896 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
897 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
898 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
899 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
900 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
901 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
902 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
903}
904
905static void
906mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
907{
908 unsigned int scr_struct;
909
910 memset(scr, 0, sizeof(*scr));
911
912 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
913 if (scr_struct != 0) {
914 kprintf("Unrecognised SCR structure version %d\n",
915 scr_struct);
916 return;
917 }
918 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
919 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
920}
921
922static void
923mmc_app_decode_sd_status(uint32_t *raw_sd_status,
924 struct mmc_sd_status *sd_status)
925{
926
927 memset(sd_status, 0, sizeof(*sd_status));
928
929 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
930 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
931 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
932 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
933 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
934 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
935 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
936 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
937 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
938 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
939}
940
941static int
942mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
943{
944 struct mmc_command cmd;
945 int err;
946
947 cmd.opcode = MMC_ALL_SEND_CID;
948 cmd.arg = 0;
949 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
950 cmd.data = NULL;
951 err = mmc_wait_for_cmd(sc, &cmd, 0);
952 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
953 return (err);
954}
955
956static int
957mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcid)
958{
959 struct mmc_command cmd;
960 int err;
961
962 cmd.opcode = MMC_SEND_CSD;
963 cmd.arg = rca << 16;
964 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
965 cmd.data = NULL;
966 err = mmc_wait_for_cmd(sc, &cmd, 0);
967 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
968 return (err);
969}
970
971static int
972mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
973{
974 int err;
975 struct mmc_command cmd;
976 struct mmc_data data;
977
978 memset(&cmd, 0, sizeof(struct mmc_command));
979 memset(&data, 0, sizeof(struct mmc_data));
980
981 memset(rawscr, 0, 8);
982 cmd.opcode = ACMD_SEND_SCR;
983 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
984 cmd.arg = 0;
985 cmd.data = &data;
986
987 data.data = rawscr;
988 data.len = 8;
989 data.flags = MMC_DATA_READ;
990
991 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
992 rawscr[0] = be32toh(rawscr[0]);
993 rawscr[1] = be32toh(rawscr[1]);
994 return (err);
995}
996
997static int
998mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
999{
1000 int err;
1001 struct mmc_command cmd;
1002 struct mmc_data data;
1003
1004 memset(&cmd, 0, sizeof(struct mmc_command));
1005 memset(&data, 0, sizeof(struct mmc_data));
1006
1007 memset(rawextcsd, 0, 512);
1008 cmd.opcode = MMC_SEND_EXT_CSD;
1009 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1010 cmd.arg = 0;
1011 cmd.data = &data;
1012
1013 data.data = rawextcsd;
1014 data.len = 512;
1015 data.flags = MMC_DATA_READ;
1016
1017 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1018 return (err);
1019}
1020
1021static int
1022mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1023{
1024 int err, i;
1025 struct mmc_command cmd;
1026 struct mmc_data data;
1027
1028 memset(&cmd, 0, sizeof(struct mmc_command));
1029 memset(&data, 0, sizeof(struct mmc_data));
1030
1031 memset(rawsdstatus, 0, 64);
1032 cmd.opcode = ACMD_SD_STATUS;
1033 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1034 cmd.arg = 0;
1035 cmd.data = &data;
1036
1037 data.data = rawsdstatus;
1038 data.len = 64;
1039 data.flags = MMC_DATA_READ;
1040
1041 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1042 for (i = 0; i < 16; i++)
1043 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1044 return (err);
1045}
1046
1047static int
1048mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1049{
1050 struct mmc_command cmd;
1051 int err;
1052
1053 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1054 cmd.arg = resp << 16;
1055 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1056 cmd.data = NULL;
1057 err = mmc_wait_for_cmd(sc, &cmd, 0);
1058 return (err);
1059}
1060
1061static int
1062mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1063{
1064 struct mmc_command cmd;
1065 int err;
1066
1067 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1068 cmd.arg = 0;
1069 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1070 cmd.data = NULL;
1071 err = mmc_wait_for_cmd(sc, &cmd, 0);
1072 *resp = cmd.resp[0];
1073 return (err);
1074}
1075
1076static void
1077mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1078{
1079 device_printf(dev, "Card at relative address %d%s:\n",
1080 ivar->rca, newcard ? " added" : "");
1081 device_printf(dev, " card: %s%s (0x%x/0x%x/\"%s\" rev %d.%d "
1082 "m/d %02d.%04d s/n %08x)\n",
1083 ivar->mode == mode_sd ? "SD" : "MMC",
1084 ivar->high_cap ? " High Capacity" : "",
1085 ivar->cid.mid, ivar->cid.oid,
1086 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
1087 ivar->cid.mdt_month, ivar->cid.mdt_year, ivar->cid.psn);
1088 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1089 (ivar->bus_width == bus_width_1 ? 1 :
1090 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1091 (ivar->timing == bus_timing_hs ?
1092 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1093 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1094 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1095 ivar->sec_count, ivar->erase_sector,
1096 ivar->read_only ? ", read-only" : "");
1097}
1098
1099static void
1100mmc_discover_cards(struct mmc_softc *sc)
1101{
1102 struct mmc_ivars *ivar = NULL;
1103 device_t *devlist;
1104 int err, i, devcount, newcard;
1105 uint32_t raw_cid[4];
1106 uint32_t resp, sec_count;
1107 device_t child;
1108 uint16_t rca = 2;
1109 u_char switch_res[64];
1110
1111 if (bootverbose || mmc_debug)
1112 device_printf(sc->dev, "Probing cards\n");
1113 while (1) {
1114 err = mmc_all_send_cid(sc, raw_cid);
1115 if (err == MMC_ERR_TIMEOUT)
1116 break;
1117 if (err != MMC_ERR_NONE) {
1118 device_printf(sc->dev, "Error reading CID %d\n", err);
1119 break;
1120 }
1121 newcard = 1;
1122 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1123 return;
1124 for (i = 0; i < devcount; i++) {
1125 ivar = device_get_ivars(devlist[i]);
1126 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) {
1127 newcard = 0;
1128 break;
1129 }
1130 }
1131 kfree(devlist, M_TEMP);
1132 if (bootverbose || mmc_debug) {
1133 device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n",
1134 newcard ? "New c" : "C",
1135 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1136 }
1137 if (newcard) {
1138 ivar = kmalloc(sizeof(struct mmc_ivars), M_DEVBUF,
1139 M_WAITOK | M_ZERO);
d6644679
SW
1140 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1141 }
1142 if (mmcbr_get_ro(sc->dev))
1143 ivar->read_only = 1;
1144 ivar->bus_width = bus_width_1;
1145 ivar->timing = bus_timing_normal;
1146 ivar->mode = mmcbr_get_mode(sc->dev);
1147 if (ivar->mode == mode_sd) {
1148 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1149 mmc_send_relative_addr(sc, &resp);
1150 ivar->rca = resp >> 16;
1151 /* Get card CSD. */
1152 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1153 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1154 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1155 if (ivar->csd.csd_structure > 0)
1156 ivar->high_cap = 1;
1157 ivar->tran_speed = ivar->csd.tran_speed;
1158 ivar->erase_sector = ivar->csd.erase_sector *
1159 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1160 /* Get card SCR. Card must be selected to fetch it. */
1161 mmc_select_card(sc, ivar->rca);
1162 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1163 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1164 /* Get card switch capabilities (command class 10). */
1165 if ((ivar->scr.sda_vsn >= 1) &&
1166 (ivar->csd.ccc & (1<<10))) {
1167 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1168 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1169 switch_res);
1170 if (switch_res[13] & 2) {
1171 ivar->timing = bus_timing_hs;
1172 ivar->hs_tran_speed = SD_MAX_HS;
1173 }
1174 }
1175 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1176 mmc_app_decode_sd_status(ivar->raw_sd_status,
1177 &ivar->sd_status);
1178 if (ivar->sd_status.au_size != 0) {
1179 ivar->erase_sector =
1180 16 << ivar->sd_status.au_size;
1181 }
1182 mmc_select_card(sc, 0);
1183 /* Find max supported bus width. */
1184 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1185 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1186 ivar->bus_width = bus_width_4;
1187 if (bootverbose || mmc_debug)
1188 mmc_log_card(sc->dev, ivar, newcard);
1189 if (newcard) {
1190 /* Add device. */
1191 child = device_add_child(sc->dev, NULL, -1);
1192 device_set_ivars(child, ivar);
1193 }
1194 return;
1195 }
1196 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1197 ivar->rca = rca++;
1198 mmc_set_relative_addr(sc, ivar->rca);
1199 /* Get card CSD. */
1200 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1201 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1202 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1203 ivar->tran_speed = ivar->csd.tran_speed;
1204 ivar->erase_sector = ivar->csd.erase_sector *
1205 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1206 /* Only MMC >= 4.x cards support EXT_CSD. */
1207 if (ivar->csd.spec_vers >= 4) {
1208 /* Card must be selected to fetch EXT_CSD. */
1209 mmc_select_card(sc, ivar->rca);
1210 mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1211 /* Handle extended capacity from EXT_CSD */
1212 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1213 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1214 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1215 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1216 if (sec_count != 0) {
1217 ivar->sec_count = sec_count;
1218 ivar->high_cap = 1;
1219 }
1220 /* Get card speed in high speed mode. */
1221 ivar->timing = bus_timing_hs;
1222 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1223 & EXT_CSD_CARD_TYPE_52)
1224 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1225 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1226 & EXT_CSD_CARD_TYPE_26)
1227 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1228 else
1229 ivar->hs_tran_speed = ivar->tran_speed;
1230 /* Find max supported bus width. */
1231 ivar->bus_width = mmc_test_bus_width(sc);
1232 mmc_select_card(sc, 0);
1233 /* Handle HC erase sector size. */
1234 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1235 ivar->erase_sector = 1024 *
1236 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1237 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1238 EXT_CSD_ERASE_GRP_DEF, 1);
1239 }
1240 } else {
1241 ivar->bus_width = bus_width_1;
1242 ivar->timing = bus_timing_normal;
1243 }
1244 if (bootverbose || mmc_debug)
1245 mmc_log_card(sc->dev, ivar, newcard);
1246 if (newcard) {
1247 /* Add device. */
1248 child = device_add_child(sc->dev, NULL, -1);
1249 device_set_ivars(child, ivar);
1250 }
1251 }
1252}
1253
1254static void
1255mmc_rescan_cards(struct mmc_softc *sc)
1256{
1257 struct mmc_ivars *ivar = NULL;
1258 device_t *devlist;
1259 int err, i, devcount;
1260
1261 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1262 return;
1263 for (i = 0; i < devcount; i++) {
1264 ivar = device_get_ivars(devlist[i]);
1265 if (mmc_select_card(sc, ivar->rca)) {
1266 if (bootverbose || mmc_debug)
1267 device_printf(sc->dev, "Card at relative address %d lost.\n",
1268 ivar->rca);
1269 device_delete_child(sc->dev, devlist[i]);
1270 kfree(ivar, M_DEVBUF);
1271 }
1272 }
1273 kfree(devlist, M_TEMP);
1274 mmc_select_card(sc, 0);
1275}
1276
1277static int
1278mmc_delete_cards(struct mmc_softc *sc)
1279{
1280 struct mmc_ivars *ivar;
1281 device_t *devlist;
1282 int err, i, devcount;
1283
1284 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1285 return (err);
1286 for (i = 0; i < devcount; i++) {
1287 ivar = device_get_ivars(devlist[i]);
1288 if (bootverbose || mmc_debug)
1289 device_printf(sc->dev, "Card at relative address %d deleted.\n",
1290 ivar->rca);
1291 device_delete_child(sc->dev, devlist[i]);
1292 kfree(ivar, M_DEVBUF);
1293 }
1294 kfree(devlist, M_TEMP);
1295 return (0);
1296}
1297
1298static void
1299mmc_go_discovery(struct mmc_softc *sc)
1300{
1301 uint32_t ocr;
1302 device_t dev;
1303 int err;
1304
1305 dev = sc->dev;
1306 if (mmcbr_get_power_mode(dev) != power_on) {
1307 /*
1308 * First, try SD modes
1309 */
1310 mmcbr_set_mode(dev, mode_sd);
1311 mmc_power_up(sc);
1312 mmcbr_set_bus_mode(dev, pushpull);
1313 if (bootverbose || mmc_debug)
1314 device_printf(sc->dev, "Probing bus\n");
1315 mmc_idle_cards(sc);
1316 err = mmc_send_if_cond(sc, 1);
1317 if ((bootverbose || mmc_debug) && err == 0)
1318 device_printf(sc->dev, "SD 2.0 interface conditions: OK\n");
1319 if (mmc_send_app_op_cond(sc, err ? 0 : MMC_OCR_CCS, &ocr) !=
1320 MMC_ERR_NONE) {
1321 if (bootverbose || mmc_debug)
1322 device_printf(sc->dev, "SD probe: failed\n");
1323 /*
1324 * Failed, try MMC
1325 */
1326 mmcbr_set_mode(dev, mode_mmc);
1327 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1328 if (bootverbose || mmc_debug)
1329 device_printf(sc->dev, "MMC probe: failed\n");
1330 ocr = 0; /* Failed both, powerdown. */
1331 } else if (bootverbose || mmc_debug)
1332 device_printf(sc->dev,
1333 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1334 } else if (bootverbose || mmc_debug)
1335 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr);
1336
1337 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1338 if (mmcbr_get_ocr(dev) != 0)
1339 mmc_idle_cards(sc);
1340 } else {
1341 mmcbr_set_bus_mode(dev, opendrain);
1342 mmcbr_set_clock(dev, mmcbr_get_f_min(dev));
1343 mmcbr_update_ios(dev);
1344 /* XXX recompute vdd based on new cards? */
1345 }
1346 /*
1347 * Make sure that we have a mutually agreeable voltage to at least
1348 * one card on the bus.
1349 */
1350 if (bootverbose || mmc_debug)
1351 device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev));
1352 if (mmcbr_get_ocr(dev) == 0) {
1353 mmc_delete_cards(sc);
1354 mmc_power_down(sc);
1355 return;
1356 }
1357 /*
1358 * Reselect the cards after we've idled them above.
1359 */
1360 if (mmcbr_get_mode(dev) == mode_sd) {
1361 err = mmc_send_if_cond(sc, 1);
1362 mmc_send_app_op_cond(sc,
1363 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1364 } else
1365 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
1366 mmc_discover_cards(sc);
1367 mmc_rescan_cards(sc);
1368
1369 mmcbr_set_bus_mode(dev, pushpull);
1370 mmcbr_update_ios(dev);
1371 mmc_calculate_clock(sc);
1372 bus_generic_attach(dev);
1373/* mmc_update_children_sysctl(dev);*/
1374}
1375
1376static int
1377mmc_calculate_clock(struct mmc_softc *sc)
1378{
1379 int max_dtr, max_hs_dtr, max_timing;
1380 int nkid, i, f_min, f_max;
1381 device_t *kids;
1382 struct mmc_ivars *ivar;
1383
1384 f_min = mmcbr_get_f_min(sc->dev);
1385 f_max = mmcbr_get_f_max(sc->dev);
1386 max_dtr = max_hs_dtr = f_max;
1387 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1388 max_timing = bus_timing_hs;
1389 else
1390 max_timing = bus_timing_normal;
1391 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1392 panic("can't get children");
1393 for (i = 0; i < nkid; i++) {
1394 ivar = device_get_ivars(kids[i]);
1395 if (ivar->timing < max_timing)
1396 max_timing = ivar->timing;
1397 if (ivar->tran_speed < max_dtr)
1398 max_dtr = ivar->tran_speed;
1399 if (ivar->hs_tran_speed < max_hs_dtr)
1400 max_hs_dtr = ivar->hs_tran_speed;
1401 }
1402 for (i = 0; i < nkid; i++) {
1403 ivar = device_get_ivars(kids[i]);
1404 if (ivar->timing == bus_timing_normal)
1405 continue;
1406 mmc_select_card(sc, ivar->rca);
1407 mmc_set_timing(sc, max_timing);
1408 }
1409 mmc_select_card(sc, 0);
1410 kfree(kids, M_TEMP);
1411 if (max_timing == bus_timing_hs)
1412 max_dtr = max_hs_dtr;
1413 if (bootverbose || mmc_debug) {
1414 device_printf(sc->dev,
1415 "setting transfer rate to %d.%03dMHz%s\n",
1416 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1417 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1418 }
1419 mmcbr_set_timing(sc->dev, max_timing);
1420 mmcbr_set_clock(sc->dev, max_dtr);
1421 mmcbr_update_ios(sc->dev);
1422 return max_dtr;
1423}
1424
1425static void
1426mmc_scan(struct mmc_softc *sc)
1427{
1428 device_t dev = sc->dev;
1429
1430 mmc_acquire_bus(dev, dev);
1431 mmc_go_discovery(sc);
1432 mmc_release_bus(dev, dev);
1433}
1434
1435static int
1436mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1437{
1438 struct mmc_ivars *ivar = device_get_ivars(child);
1439
1440 switch (which) {
1441 default:
1442 return (EINVAL);
1443 case MMC_IVAR_DSR_IMP:
1444 *(int *)result = ivar->csd.dsr_imp;
1445 break;
1446 case MMC_IVAR_MEDIA_SIZE:
1447 *(off_t *)result = ivar->sec_count;
1448 break;
1449 case MMC_IVAR_RCA:
1450 *(int *)result = ivar->rca;
1451 break;
1452 case MMC_IVAR_SECTOR_SIZE:
1453 *(int *)result = MMC_SECTOR_SIZE;
1454 break;
1455 case MMC_IVAR_TRAN_SPEED:
1456 *(int *)result = mmcbr_get_clock(bus);
1457 break;
1458 case MMC_IVAR_READ_ONLY:
1459 *(int *)result = ivar->read_only;
1460 break;
1461 case MMC_IVAR_HIGH_CAP:
1462 *(int *)result = ivar->high_cap;
1463 break;
1464 case MMC_IVAR_CARD_TYPE:
1465 *(int *)result = ivar->mode;
1466 break;
1467 case MMC_IVAR_BUS_WIDTH:
1468 *(int *)result = ivar->bus_width;
1469 break;
1470 case MMC_IVAR_ERASE_SECTOR:
1471 *(int *)result = ivar->erase_sector;
1472 break;
1473 case MMC_IVAR_MAX_DATA:
1474 *(int *)result = mmcbr_get_max_data(bus);
1475 break;
1476 }
1477 return (0);
1478}
1479
1480static int
1481mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1482{
1483 /*
1484 * None are writable ATM
1485 */
1486 return (EINVAL);
1487}
1488
1489
1490static void
1491mmc_delayed_attach(void *xsc)
1492{
1493 struct mmc_softc *sc = xsc;
1494
1495 mmc_scan(sc);
1496 config_intrhook_disestablish(&sc->config_intrhook);
1497}
1498
1499static device_method_t mmc_methods[] = {
1500 /* device_if */
1501 DEVMETHOD(device_probe, mmc_probe),
1502 DEVMETHOD(device_attach, mmc_attach),
1503 DEVMETHOD(device_detach, mmc_detach),
1504 DEVMETHOD(device_suspend, mmc_suspend),
1505 DEVMETHOD(device_resume, mmc_resume),
1506
1507 /* Bus interface */
1508 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1509 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1510
1511 /* MMC Bus interface */
1512 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1513 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1514 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1515
1516 {0, 0},
1517};
1518
1519static driver_t mmc_driver = {
1520 "mmc",
1521 mmc_methods,
1522 sizeof(struct mmc_softc),
1523};
1524static devclass_t mmc_devclass;
1525
1526
d6644679 1527DRIVER_MODULE(mmc, sdhci, mmc_driver, mmc_devclass, NULL, NULL);