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