source: rtems-libbsd/freebsd/sys/dev/mmc/mmc.c @ 0237319

55-freebsd-126-freebsd-12
Last change on this file since 0237319 was 0237319, checked in by Sebastian Huber <sebastian.huber@…>, on 05/23/17 at 11:18:31

Update due to Newlib 2017-06-07 changes

The following files are now provided by Newlib:

  • arpa/inet.h
  • net/if.h
  • netinet/in.h
  • netinet/tcp.h
  • sys/socket.h
  • sys/uio.h
  • sys/un.h

The <sys/param.h> and <sys/cpuset.h> are now compatible enough to be
used directly.

Update #2833.

  • Property mode set to 100644
File size: 57.0 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2
3/*-
4 * Copyright (c) 2006 Bernd Walter.  All rights reserved.
5 * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
6 * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Portions of this software may have been developed with reference to
29 * the SD Simplified Specification.  The following disclaimer may apply:
30 *
31 * The following conditions apply to the release of the simplified
32 * specification ("Simplified Specification") by the SD Card Association and
33 * the SD Group. The Simplified Specification is a subset of the complete SD
34 * Specification which is owned by the SD Card Association and the SD
35 * Group. This Simplified Specification is provided on a non-confidential
36 * basis subject to the disclaimers below. Any implementation of the
37 * Simplified Specification may require a license from the SD Card
38 * Association, SD Group, SD-3C LLC or other third parties.
39 *
40 * Disclaimers:
41 *
42 * The information contained in the Simplified Specification is presented only
43 * as a standard specification for SD Cards and SD Host/Ancillary products and
44 * is provided "AS-IS" without any representations or warranties of any
45 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
46 * Card Association for any damages, any infringements of patents or other
47 * right of the SD Group, SD-3C LLC, the SD Card Association or any third
48 * parties, which may result from its use. No license is granted by
49 * implication, estoppel or otherwise under any patent or other rights of the
50 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
51 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
52 * or the SD Card Association to disclose or distribute any technical
53 * information, know-how or other confidential information to any third party.
54 */
55
56#include <sys/cdefs.h>
57__FBSDID("$FreeBSD$");
58
59#include <sys/param.h>
60#include <sys/systm.h>
61#include <sys/kernel.h>
62#include <sys/malloc.h>
63#include <rtems/bsd/sys/lock.h>
64#include <sys/module.h>
65#include <sys/mutex.h>
66#include <sys/bus.h>
67#include <sys/endian.h>
68#include <sys/sysctl.h>
69#include <sys/time.h>
70
71#include <dev/mmc/bridge.h>
72#include <dev/mmc/mmc_private.h>
73#include <dev/mmc/mmc_subr.h>
74#include <dev/mmc/mmcreg.h>
75#include <dev/mmc/mmcbrvar.h>
76#include <dev/mmc/mmcvar.h>
77
78#include <rtems/bsd/local/mmcbr_if.h>
79#include <rtems/bsd/local/mmcbus_if.h>
80
81CTASSERT(bus_timing_max <= sizeof(uint32_t) * NBBY);
82
83/*
84 * Per-card data
85 */
86struct mmc_ivars {
87        uint32_t raw_cid[4];    /* Raw bits of the CID */
88        uint32_t raw_csd[4];    /* Raw bits of the CSD */
89        uint32_t raw_scr[2];    /* Raw bits of the SCR */
90        uint8_t raw_ext_csd[MMC_EXTCSD_SIZE]; /* Raw bits of the EXT_CSD */
91        uint32_t raw_sd_status[16];     /* Raw bits of the SD_STATUS */
92        uint16_t rca;
93        enum mmc_card_mode mode;
94        struct mmc_cid cid;     /* cid decoded */
95        struct mmc_csd csd;     /* csd decoded */
96        struct mmc_scr scr;     /* scr decoded */
97        struct mmc_sd_status sd_status; /* SD_STATUS decoded */
98        u_char read_only;       /* True when the device is read-only */
99        u_char bus_width;       /* Bus width to use */
100        u_char high_cap;        /* High Capacity card (block addressed) */
101        uint32_t sec_count;     /* Card capacity in 512byte blocks */
102        uint32_t timings;       /* Mask of bus timings supported */
103        uint32_t vccq_120;      /* Mask of bus timings at VCCQ of 1.2 V */
104        uint32_t vccq_180;      /* Mask of bus timings at VCCQ of 1.8 V */
105        uint32_t tran_speed;    /* Max speed in normal mode */
106        uint32_t hs_tran_speed; /* Max speed in high speed mode */
107        uint32_t erase_sector;  /* Card native erase sector size */
108        uint32_t cmd6_time;     /* Generic switch timeout [us] */
109        char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
110        char card_sn_string[16];/* Formatted serial # for disk->d_ident */
111};
112
113#define CMD_RETRIES     3
114
115static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
116
117static int mmc_debug;
118SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &mmc_debug, 0,
119    "Debug level");
120
121/* bus entry points */
122static int mmc_acquire_bus(device_t busdev, device_t dev);
123static int mmc_attach(device_t dev);
124static int mmc_child_location_str(device_t dev, device_t child, char *buf,
125    size_t buflen);
126static int mmc_detach(device_t dev);
127static int mmc_probe(device_t dev);
128static int mmc_read_ivar(device_t bus, device_t child, int which,
129    uintptr_t *result);
130static int mmc_release_bus(device_t busdev, device_t dev);
131static int mmc_resume(device_t dev);
132static int mmc_suspend(device_t dev);
133static int mmc_wait_for_request(device_t brdev, device_t reqdev,
134    struct mmc_request *req);
135static int mmc_write_ivar(device_t bus, device_t child, int which,
136    uintptr_t value);
137
138#define MMC_LOCK(_sc)           mtx_lock(&(_sc)->sc_mtx)
139#define MMC_UNLOCK(_sc)         mtx_unlock(&(_sc)->sc_mtx)
140#define MMC_LOCK_INIT(_sc)                                              \
141        mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->dev),       \
142            "mmc", MTX_DEF)
143#define MMC_LOCK_DESTROY(_sc)   mtx_destroy(&(_sc)->sc_mtx);
144#define MMC_ASSERT_LOCKED(_sc)  mtx_assert(&(_sc)->sc_mtx, MA_OWNED);
145#define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED);
146
147static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
148static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
149static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
150    struct mmc_sd_status *sd_status);
151static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
152    uint32_t *rawsdstatus);
153static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
154    uint32_t *rawscr);
155static int mmc_calculate_clock(struct mmc_softc *sc);
156static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid,
157    bool is_4_41p);
158static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
159static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
160static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
161static void mmc_delayed_attach(void *xsc);
162static int mmc_delete_cards(struct mmc_softc *sc);
163static void mmc_discover_cards(struct mmc_softc *sc);
164static void mmc_format_card_id_string(struct mmc_ivars *ivar);
165static void mmc_go_discovery(struct mmc_softc *sc);
166static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
167    int size);
168static int mmc_highest_voltage(uint32_t ocr);
169static void mmc_idle_cards(struct mmc_softc *sc);
170static void mmc_ms_delay(int ms);
171static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
172static void mmc_power_down(struct mmc_softc *sc);
173static void mmc_power_up(struct mmc_softc *sc);
174static void mmc_rescan_cards(struct mmc_softc *sc);
175static void mmc_scan(struct mmc_softc *sc);
176static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
177    uint8_t value, uint8_t *res);
178static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
179static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
180static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
181    uint32_t *rocr);
182static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
183static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
184static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
185    uint32_t *rocr);
186static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
187static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
188static int mmc_set_card_bus_width(struct mmc_softc *sc, struct mmc_ivars *ivar);
189static int mmc_set_power_class(struct mmc_softc *sc, struct mmc_ivars *ivar);
190static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
191static int mmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar,
192    enum mmc_bus_timing timing);
193static int mmc_test_bus_width(struct mmc_softc *sc);
194static uint32_t mmc_timing_to_dtr(struct mmc_ivars *ivar,
195    enum mmc_bus_timing timing);
196static const char *mmc_timing_to_string(enum mmc_bus_timing timing);
197static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
198    uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
199static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
200static void mmc_wakeup(struct mmc_request *req);
201
202static void
203mmc_ms_delay(int ms)
204{
205
206        DELAY(1000 * ms);       /* XXX BAD */
207}
208
209static int
210mmc_probe(device_t dev)
211{
212
213        device_set_desc(dev, "MMC/SD bus");
214        return (0);
215}
216
217static int
218mmc_attach(device_t dev)
219{
220        struct mmc_softc *sc;
221
222        sc = device_get_softc(dev);
223        sc->dev = dev;
224        MMC_LOCK_INIT(sc);
225
226        /* We'll probe and attach our children later, but before / mount */
227        sc->config_intrhook.ich_func = mmc_delayed_attach;
228        sc->config_intrhook.ich_arg = sc;
229        if (config_intrhook_establish(&sc->config_intrhook) != 0)
230                device_printf(dev, "config_intrhook_establish failed\n");
231        return (0);
232}
233
234static int
235mmc_detach(device_t dev)
236{
237        struct mmc_softc *sc = device_get_softc(dev);
238        int err;
239
240        if ((err = mmc_delete_cards(sc)) != 0)
241                return (err);
242        mmc_power_down(sc);
243        MMC_LOCK_DESTROY(sc);
244
245        return (0);
246}
247
248static int
249mmc_suspend(device_t dev)
250{
251        struct mmc_softc *sc = device_get_softc(dev);
252        int err;
253
254        err = bus_generic_suspend(dev);
255        if (err)
256                return (err);
257        mmc_power_down(sc);
258        return (0);
259}
260
261static int
262mmc_resume(device_t dev)
263{
264        struct mmc_softc *sc = device_get_softc(dev);
265
266        mmc_scan(sc);
267        return (bus_generic_resume(dev));
268}
269
270static int
271mmc_acquire_bus(device_t busdev, device_t dev)
272{
273        struct mmc_softc *sc;
274        struct mmc_ivars *ivar;
275        int err, rca;
276        enum mmc_bus_timing timing;
277
278        err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
279        if (err)
280                return (err);
281        sc = device_get_softc(busdev);
282        MMC_LOCK(sc);
283        if (sc->owner)
284                panic("mmc: host bridge didn't serialize us.");
285        sc->owner = dev;
286        MMC_UNLOCK(sc);
287
288        if (busdev != dev) {
289                /*
290                 * Keep track of the last rca that we've selected.  If
291                 * we're asked to do it again, don't.  We never
292                 * unselect unless the bus code itself wants the mmc
293                 * bus, and constantly reselecting causes problems.
294                 */
295                ivar = device_get_ivars(dev);
296                rca = ivar->rca;
297                if (sc->last_rca != rca) {
298                        if (mmc_select_card(sc, rca) != MMC_ERR_NONE) {
299                                device_printf(sc->dev, "Card at relative "
300                                    "address %d failed to select.\n", rca);
301                                return (ENXIO);
302                        }
303                        sc->last_rca = rca;
304                        timing = mmcbr_get_timing(busdev);
305                        /* Prepare bus width for the new card. */
306                        if (bootverbose || mmc_debug) {
307                                device_printf(busdev,
308                                    "setting bus width to %d bits %s timing\n",
309                                    (ivar->bus_width == bus_width_4) ? 4 :
310                                    (ivar->bus_width == bus_width_8) ? 8 : 1,
311                                    mmc_timing_to_string(timing));
312                        }
313                        if (mmc_set_card_bus_width(sc, ivar) != MMC_ERR_NONE) {
314                                device_printf(sc->dev, "Card at relative "
315                                    "address %d failed to set bus width.\n",
316                                    rca);
317                                return (ENXIO);
318                        }
319                        if (isset(&ivar->vccq_120, timing))
320                                mmcbr_set_vccq(busdev, vccq_120);
321                        else if (isset(&ivar->vccq_180, timing))
322                                mmcbr_set_vccq(busdev, vccq_180);
323                        else
324                                mmcbr_set_vccq(busdev, vccq_330);
325                        if (mmcbr_switch_vccq(busdev) != 0) {
326                                device_printf(sc->dev, "Failed to set VCCQ "
327                                    "for card at relative address %d.\n", rca);
328                                return (ENXIO);
329                        }
330                        if (mmc_set_power_class(sc, ivar) != MMC_ERR_NONE) {
331                                device_printf(sc->dev, "Card at relative "
332                                    "address %d failed to set power class.\n",
333                                    rca);
334                                return (ENXIO);
335                        }
336                        mmcbr_set_bus_width(busdev, ivar->bus_width);
337                        mmcbr_update_ios(busdev);
338                }
339        } else {
340                /*
341                 * If there's a card selected, stand down.
342                 */
343                if (sc->last_rca != 0) {
344                        mmc_select_card(sc, 0);
345                        sc->last_rca = 0;
346                }
347        }
348
349        return (0);
350}
351
352static int
353mmc_release_bus(device_t busdev, device_t dev)
354{
355        struct mmc_softc *sc;
356        int err;
357
358        sc = device_get_softc(busdev);
359
360        MMC_LOCK(sc);
361        if (!sc->owner)
362                panic("mmc: releasing unowned bus.");
363        if (sc->owner != dev)
364                panic("mmc: you don't own the bus.  game over.");
365        MMC_UNLOCK(sc);
366        err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
367        if (err)
368                return (err);
369        MMC_LOCK(sc);
370        sc->owner = NULL;
371        MMC_UNLOCK(sc);
372        return (0);
373}
374
375static uint32_t
376mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
377{
378
379        return (ocr & MMC_OCR_VOLTAGE);
380}
381
382static int
383mmc_highest_voltage(uint32_t ocr)
384{
385        int i;
386
387        for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
388            i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
389                if (ocr & (1 << i))
390                        return (i);
391        return (-1);
392}
393
394static void
395mmc_wakeup(struct mmc_request *req)
396{
397        struct mmc_softc *sc;
398
399        sc = (struct mmc_softc *)req->done_data;
400        MMC_LOCK(sc);
401        req->flags |= MMC_REQ_DONE;
402        MMC_UNLOCK(sc);
403        wakeup(req);
404}
405
406static int
407mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
408{
409
410        req->done = mmc_wakeup;
411        req->done_data = sc;
412        if (mmc_debug > 1) {
413                device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
414                    req->cmd->opcode, req->cmd->arg, req->cmd->flags);
415                if (req->cmd->data) {
416                        printf(" data %d\n", (int)req->cmd->data->len);
417                } else
418                        printf("\n");
419        }
420        MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
421        MMC_LOCK(sc);
422        while ((req->flags & MMC_REQ_DONE) == 0)
423                msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
424        MMC_UNLOCK(sc);
425        if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
426                device_printf(sc->dev, "CMD%d RESULT: %d\n",
427                    req->cmd->opcode, req->cmd->error);
428        return (0);
429}
430
431static int
432mmc_wait_for_request(device_t brdev, device_t reqdev __unused,
433    struct mmc_request *req)
434{
435        struct mmc_softc *sc = device_get_softc(brdev);
436
437        return (mmc_wait_for_req(sc, req));
438}
439
440static int
441mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
442    uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
443{
444        struct mmc_command cmd;
445        int err;
446
447        memset(&cmd, 0, sizeof(cmd));
448        cmd.opcode = opcode;
449        cmd.arg = arg;
450        cmd.flags = flags;
451        cmd.data = NULL;
452        err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, retries);
453        if (err)
454                return (err);
455        if (resp) {
456                if (flags & MMC_RSP_136)
457                        memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
458                else
459                        *resp = cmd.resp[0];
460        }
461        return (0);
462}
463
464static void
465mmc_idle_cards(struct mmc_softc *sc)
466{
467        device_t dev;
468        struct mmc_command cmd;
469
470        dev = sc->dev;
471        mmcbr_set_chip_select(dev, cs_high);
472        mmcbr_update_ios(dev);
473        mmc_ms_delay(1);
474
475        memset(&cmd, 0, sizeof(cmd));
476        cmd.opcode = MMC_GO_IDLE_STATE;
477        cmd.arg = 0;
478        cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
479        cmd.data = NULL;
480        mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
481        mmc_ms_delay(1);
482
483        mmcbr_set_chip_select(dev, cs_dontcare);
484        mmcbr_update_ios(dev);
485        mmc_ms_delay(1);
486}
487
488static int
489mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
490{
491        struct mmc_command cmd;
492        int err = MMC_ERR_NONE, i;
493
494        memset(&cmd, 0, sizeof(cmd));
495        cmd.opcode = ACMD_SD_SEND_OP_COND;
496        cmd.arg = ocr;
497        cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
498        cmd.data = NULL;
499
500        for (i = 0; i < 1000; i++) {
501                err = mmc_wait_for_app_cmd(sc->dev, sc->dev, 0, &cmd,
502                    CMD_RETRIES);
503                if (err != MMC_ERR_NONE)
504                        break;
505                if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
506                    (ocr & MMC_OCR_VOLTAGE) == 0)
507                        break;
508                err = MMC_ERR_TIMEOUT;
509                mmc_ms_delay(10);
510        }
511        if (rocr && err == MMC_ERR_NONE)
512                *rocr = cmd.resp[0];
513        return (err);
514}
515
516static int
517mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
518{
519        struct mmc_command cmd;
520        int err = MMC_ERR_NONE, i;
521
522        memset(&cmd, 0, sizeof(cmd));
523        cmd.opcode = MMC_SEND_OP_COND;
524        cmd.arg = ocr;
525        cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
526        cmd.data = NULL;
527
528        for (i = 0; i < 1000; i++) {
529                err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
530                if (err != MMC_ERR_NONE)
531                        break;
532                if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
533                    (ocr & MMC_OCR_VOLTAGE) == 0)
534                        break;
535                err = MMC_ERR_TIMEOUT;
536                mmc_ms_delay(10);
537        }
538        if (rocr && err == MMC_ERR_NONE)
539                *rocr = cmd.resp[0];
540        return (err);
541}
542
543static int
544mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
545{
546        struct mmc_command cmd;
547        int err;
548
549        memset(&cmd, 0, sizeof(cmd));
550        cmd.opcode = SD_SEND_IF_COND;
551        cmd.arg = (vhs << 8) + 0xAA;
552        cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
553        cmd.data = NULL;
554
555        err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
556        return (err);
557}
558
559static void
560mmc_power_up(struct mmc_softc *sc)
561{
562        device_t dev;
563        enum mmc_vccq vccq;
564
565        dev = sc->dev;
566        mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
567        mmcbr_set_bus_mode(dev, opendrain);
568        mmcbr_set_chip_select(dev, cs_dontcare);
569        mmcbr_set_bus_width(dev, bus_width_1);
570        mmcbr_set_power_mode(dev, power_up);
571        mmcbr_set_clock(dev, 0);
572        mmcbr_update_ios(dev);
573        for (vccq = vccq_330; ; vccq--) {
574                mmcbr_set_vccq(dev, vccq);
575                if (mmcbr_switch_vccq(dev) == 0 || vccq == vccq_120)
576                        break;
577        }
578        mmc_ms_delay(1);
579
580        mmcbr_set_clock(dev, SD_MMC_CARD_ID_FREQUENCY);
581        mmcbr_set_timing(dev, bus_timing_normal);
582        mmcbr_set_power_mode(dev, power_on);
583        mmcbr_update_ios(dev);
584        mmc_ms_delay(2);
585}
586
587static void
588mmc_power_down(struct mmc_softc *sc)
589{
590        device_t dev = sc->dev;
591
592        mmcbr_set_bus_mode(dev, opendrain);
593        mmcbr_set_chip_select(dev, cs_dontcare);
594        mmcbr_set_bus_width(dev, bus_width_1);
595        mmcbr_set_power_mode(dev, power_off);
596        mmcbr_set_clock(dev, 0);
597        mmcbr_set_timing(dev, bus_timing_normal);
598        mmcbr_update_ios(dev);
599}
600
601static int
602mmc_select_card(struct mmc_softc *sc, uint16_t rca)
603{
604        int flags;
605
606        flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
607        return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
608            flags, NULL, CMD_RETRIES));
609}
610
611static int
612mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
613    uint8_t *res)
614{
615        int err;
616        struct mmc_command cmd;
617        struct mmc_data data;
618
619        memset(&cmd, 0, sizeof(cmd));
620        memset(&data, 0, sizeof(data));
621        memset(res, 0, 64);
622
623        cmd.opcode = SD_SWITCH_FUNC;
624        cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
625        cmd.arg = mode << 31;                   /* 0 - check, 1 - set */
626        cmd.arg |= 0x00FFFFFF;
627        cmd.arg &= ~(0xF << (grp * 4));
628        cmd.arg |= value << (grp * 4);
629        cmd.data = &data;
630
631        data.data = res;
632        data.len = 64;
633        data.flags = MMC_DATA_READ;
634
635        err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
636        return (err);
637}
638
639static int
640mmc_set_card_bus_width(struct mmc_softc *sc, struct mmc_ivars *ivar)
641{
642        struct mmc_command cmd;
643        int err;
644        uint8_t value;
645
646        if (mmcbr_get_mode(sc->dev) == mode_sd) {
647                memset(&cmd, 0, sizeof(cmd));
648                cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
649                cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
650                cmd.arg = SD_CLR_CARD_DETECT;
651                err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd,
652                    CMD_RETRIES);
653                if (err != 0)
654                        return (err);
655                memset(&cmd, 0, sizeof(cmd));
656                cmd.opcode = ACMD_SET_BUS_WIDTH;
657                cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
658                switch (ivar->bus_width) {
659                case bus_width_1:
660                        cmd.arg = SD_BUS_WIDTH_1;
661                        break;
662                case bus_width_4:
663                        cmd.arg = SD_BUS_WIDTH_4;
664                        break;
665                default:
666                        return (MMC_ERR_INVALID);
667                }
668                err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd,
669                    CMD_RETRIES);
670        } else {
671                switch (ivar->bus_width) {
672                case bus_width_1:
673                        value = EXT_CSD_BUS_WIDTH_1;
674                        break;
675                case bus_width_4:
676                        switch (mmcbr_get_timing(sc->dev)) {
677                        case bus_timing_mmc_ddr52:
678                        case bus_timing_mmc_hs200:
679                        case bus_timing_mmc_hs400:
680                        case bus_timing_mmc_hs400es:
681                                value = EXT_CSD_BUS_WIDTH_4_DDR;
682                                break;
683                        default:
684                                value = EXT_CSD_BUS_WIDTH_4;
685                                break;
686                        }
687                        break;
688                case bus_width_8:
689                        switch (mmcbr_get_timing(sc->dev)) {
690                        case bus_timing_mmc_ddr52:
691                        case bus_timing_mmc_hs200:
692                        case bus_timing_mmc_hs400:
693                        case bus_timing_mmc_hs400es:
694                                value = EXT_CSD_BUS_WIDTH_8_DDR;
695                                break;
696                        default:
697                                value = EXT_CSD_BUS_WIDTH_8;
698                                break;
699                        }
700                        break;
701                default:
702                        return (MMC_ERR_INVALID);
703                }
704                err = mmc_switch(sc->dev, sc->dev, ivar->rca,
705                    EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, value,
706                    ivar->cmd6_time, true);
707        }
708        return (err);
709}
710
711static int
712mmc_set_power_class(struct mmc_softc *sc, struct mmc_ivars *ivar)
713{
714        device_t dev;
715        const uint8_t *ext_csd;
716        uint32_t clock;
717        uint8_t value;
718
719        dev = sc->dev;
720        if (mmcbr_get_mode(dev) != mode_mmc || ivar->csd.spec_vers < 4)
721                return (MMC_ERR_NONE);
722
723        value = 0;
724        ext_csd = ivar->raw_ext_csd;
725        clock = mmcbr_get_clock(dev);
726        switch (1 << mmcbr_get_vdd(dev)) {
727        case MMC_OCR_LOW_VOLTAGE:
728                if (clock <= MMC_TYPE_HS_26_MAX)
729                        value = ext_csd[EXT_CSD_PWR_CL_26_195];
730                else if (clock <= MMC_TYPE_HS_52_MAX) {
731                        if (mmcbr_get_timing(dev) >= bus_timing_mmc_ddr52 &&
732                            ivar->bus_width >= bus_width_4)
733                                value = ext_csd[EXT_CSD_PWR_CL_52_195_DDR];
734                        else
735                                value = ext_csd[EXT_CSD_PWR_CL_52_195];
736                } else if (clock <= MMC_TYPE_HS200_HS400ES_MAX)
737                        value = ext_csd[EXT_CSD_PWR_CL_200_195];
738                break;
739        case MMC_OCR_270_280:
740        case MMC_OCR_280_290:
741        case MMC_OCR_290_300:
742        case MMC_OCR_300_310:
743        case MMC_OCR_310_320:
744        case MMC_OCR_320_330:
745        case MMC_OCR_330_340:
746        case MMC_OCR_340_350:
747        case MMC_OCR_350_360:
748                if (clock <= MMC_TYPE_HS_26_MAX)
749                        value = ext_csd[EXT_CSD_PWR_CL_26_360];
750                else if (clock <= MMC_TYPE_HS_52_MAX) {
751                        if (mmcbr_get_timing(dev) == bus_timing_mmc_ddr52 &&
752                            ivar->bus_width >= bus_width_4)
753                                value = ext_csd[EXT_CSD_PWR_CL_52_360_DDR];
754                        else
755                                value = ext_csd[EXT_CSD_PWR_CL_52_360];
756                } else if (clock <= MMC_TYPE_HS200_HS400ES_MAX) {
757                        if (ivar->bus_width == bus_width_8)
758                                value = ext_csd[EXT_CSD_PWR_CL_200_360_DDR];
759                        else
760                                value = ext_csd[EXT_CSD_PWR_CL_200_360];
761                }
762                break;
763        default:
764                device_printf(dev, "No power class support for VDD 0x%x\n",
765                        1 << mmcbr_get_vdd(dev));
766                return (MMC_ERR_INVALID);
767        }
768
769        if (ivar->bus_width == bus_width_8)
770                value = (value & EXT_CSD_POWER_CLASS_8BIT_MASK) >>
771                    EXT_CSD_POWER_CLASS_8BIT_SHIFT;
772        else
773                value = (value & EXT_CSD_POWER_CLASS_4BIT_MASK) >>
774                    EXT_CSD_POWER_CLASS_4BIT_SHIFT;
775
776        if (value == 0)
777                return (MMC_ERR_NONE);
778
779        return (mmc_switch(dev, dev, ivar->rca, EXT_CSD_CMD_SET_NORMAL,
780            EXT_CSD_POWER_CLASS, value, ivar->cmd6_time, true));
781}
782
783static int
784mmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar,
785    enum mmc_bus_timing timing)
786{
787        u_char switch_res[64];
788        uint8_t value;
789        int err;
790
791        if (mmcbr_get_mode(sc->dev) == mode_sd) {
792                switch (timing) {
793                case bus_timing_normal:
794                        value = SD_SWITCH_NORMAL_MODE;
795                        break;
796                case bus_timing_hs:
797                        value = SD_SWITCH_HS_MODE;
798                        break;
799                default:
800                        return (MMC_ERR_INVALID);
801                }
802                err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
803                    value, switch_res);
804                if (err != MMC_ERR_NONE)
805                        return (err);
806                if ((switch_res[16] & 0xf) != value)
807                        return (MMC_ERR_FAILED);
808                mmcbr_set_timing(sc->dev, timing);
809                mmcbr_update_ios(sc->dev);
810        } else {
811                switch (timing) {
812                case bus_timing_normal:
813                        value = EXT_CSD_HS_TIMING_BC;
814                        break;
815                case bus_timing_hs:
816                case bus_timing_mmc_ddr52:
817                        value = EXT_CSD_HS_TIMING_HS;
818                        break;
819                default:
820                        return (MMC_ERR_INVALID);
821                }
822                err = mmc_switch(sc->dev, sc->dev, ivar->rca,
823                    EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, value,
824                    ivar->cmd6_time, false);
825                if (err != MMC_ERR_NONE)
826                        return (err);
827                mmcbr_set_timing(sc->dev, timing);
828                mmcbr_update_ios(sc->dev);
829                err = mmc_switch_status(sc->dev, sc->dev, ivar->rca,
830                    ivar->cmd6_time);
831        }
832        return (err);
833}
834
835static const uint8_t p8[8] = {
836        0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
837};
838
839static const uint8_t p8ok[8] = {
840        0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
841};
842
843static const uint8_t p4[4] = {
844        0x5A, 0x00, 0x00, 0x00
845};
846
847static const uint8_t p4ok[4] = {
848        0xA5, 0x00, 0x00, 0x00
849};
850
851static int
852mmc_test_bus_width(struct mmc_softc *sc)
853{
854        struct mmc_command cmd;
855        struct mmc_data data;
856        uint8_t buf[8];
857        int err;
858
859        if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
860                mmcbr_set_bus_width(sc->dev, bus_width_8);
861                mmcbr_update_ios(sc->dev);
862
863                sc->squelched++; /* Errors are expected, squelch reporting. */
864                memset(&cmd, 0, sizeof(cmd));
865                memset(&data, 0, sizeof(data));
866                cmd.opcode = MMC_BUSTEST_W;
867                cmd.arg = 0;
868                cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
869                cmd.data = &data;
870
871                data.data = __DECONST(void *, p8);
872                data.len = 8;
873                data.flags = MMC_DATA_WRITE;
874                mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
875
876                memset(&cmd, 0, sizeof(cmd));
877                memset(&data, 0, sizeof(data));
878                cmd.opcode = MMC_BUSTEST_R;
879                cmd.arg = 0;
880                cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
881                cmd.data = &data;
882
883                data.data = buf;
884                data.len = 8;
885                data.flags = MMC_DATA_READ;
886                err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
887                sc->squelched--;
888
889                mmcbr_set_bus_width(sc->dev, bus_width_1);
890                mmcbr_update_ios(sc->dev);
891
892                if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
893                        return (bus_width_8);
894        }
895
896        if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
897                mmcbr_set_bus_width(sc->dev, bus_width_4);
898                mmcbr_update_ios(sc->dev);
899
900                sc->squelched++; /* Errors are expected, squelch reporting. */
901                memset(&cmd, 0, sizeof(cmd));
902                memset(&data, 0, sizeof(data));
903                cmd.opcode = MMC_BUSTEST_W;
904                cmd.arg = 0;
905                cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
906                cmd.data = &data;
907
908                data.data = __DECONST(void *, p4);
909                data.len = 4;
910                data.flags = MMC_DATA_WRITE;
911                mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
912
913                memset(&cmd, 0, sizeof(cmd));
914                memset(&data, 0, sizeof(data));
915                cmd.opcode = MMC_BUSTEST_R;
916                cmd.arg = 0;
917                cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
918                cmd.data = &data;
919
920                data.data = buf;
921                data.len = 4;
922                data.flags = MMC_DATA_READ;
923                err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
924                sc->squelched--;
925
926                mmcbr_set_bus_width(sc->dev, bus_width_1);
927                mmcbr_update_ios(sc->dev);
928
929                if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
930                        return (bus_width_4);
931        }
932        return (bus_width_1);
933}
934
935static uint32_t
936mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
937{
938        const int i = (bit_len / 32) - (start / 32) - 1;
939        const int shift = start & 31;
940        uint32_t retval = bits[i] >> shift;
941
942        if (size + shift > 32)
943                retval |= bits[i - 1] << (32 - shift);
944        return (retval & ((1llu << size) - 1));
945}
946
947static void
948mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
949{
950        int i;
951
952        /* There's no version info, so we take it on faith */
953        memset(cid, 0, sizeof(*cid));
954        cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
955        cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
956        for (i = 0; i < 5; i++)
957                cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
958        cid->pnm[5] = 0;
959        cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
960        cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
961        cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
962        cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
963}
964
965static void
966mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid, bool is_4_41p)
967{
968        int i;
969
970        /* There's no version info, so we take it on faith */
971        memset(cid, 0, sizeof(*cid));
972        cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
973        cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
974        for (i = 0; i < 6; i++)
975                cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
976        cid->pnm[6] = 0;
977        cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
978        cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
979        cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
980        cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4);
981        if (is_4_41p)
982                cid->mdt_year += 2013;
983        else
984                cid->mdt_year += 1997;
985}
986
987static void
988mmc_format_card_id_string(struct mmc_ivars *ivar)
989{
990        char oidstr[8];
991        uint8_t c1;
992        uint8_t c2;
993
994        /*
995         * Format a card ID string for use by the mmcsd driver, it's what
996         * appears between the <> in the following:
997         * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
998         * 22.5MHz/4bit/128-block
999         *
1000         * Also format just the card serial number, which the mmcsd driver will
1001         * use as the disk->d_ident string.
1002         *
1003         * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
1004         * and our max formatted length is currently 55 bytes if every field
1005         * contains the largest value.
1006         *
1007         * Sometimes the oid is two printable ascii chars; when it's not,
1008         * format it as 0xnnnn instead.
1009         */
1010        c1 = (ivar->cid.oid >> 8) & 0x0ff;
1011        c2 = ivar->cid.oid & 0x0ff;
1012        if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
1013                snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
1014        else
1015                snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
1016        snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string),
1017            "%08X", ivar->cid.psn);
1018        snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
1019            "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
1020            ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
1021            ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
1022            ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
1023            ivar->cid.mid, oidstr);
1024}
1025
1026static const int exp[8] = {
1027        1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
1028};
1029
1030static const int mant[16] = {
1031        0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
1032};
1033
1034static const int cur_min[8] = {
1035        500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
1036};
1037
1038static const int cur_max[8] = {
1039        1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
1040};
1041
1042static void
1043mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
1044{
1045        int v;
1046        int m;
1047        int e;
1048
1049        memset(csd, 0, sizeof(*csd));
1050        csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
1051        if (v == 0) {
1052                m = mmc_get_bits(raw_csd, 128, 115, 4);
1053                e = mmc_get_bits(raw_csd, 128, 112, 3);
1054                csd->tacc = (exp[e] * mant[m] + 9) / 10;
1055                csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1056                m = mmc_get_bits(raw_csd, 128, 99, 4);
1057                e = mmc_get_bits(raw_csd, 128, 96, 3);
1058                csd->tran_speed = exp[e] * 10000 * mant[m];
1059                csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1060                csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1061                csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1062                csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1063                csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1064                csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1065                csd->vdd_r_curr_min =
1066                    cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1067                csd->vdd_r_curr_max =
1068                    cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1069                csd->vdd_w_curr_min =
1070                    cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1071                csd->vdd_w_curr_max =
1072                    cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1073                m = mmc_get_bits(raw_csd, 128, 62, 12);
1074                e = mmc_get_bits(raw_csd, 128, 47, 3);
1075                csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1076                csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1077                csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1078                csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1079                csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1080                csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1081                csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1082                csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1083        } else if (v == 1) {
1084                m = mmc_get_bits(raw_csd, 128, 115, 4);
1085                e = mmc_get_bits(raw_csd, 128, 112, 3);
1086                csd->tacc = (exp[e] * mant[m] + 9) / 10;
1087                csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1088                m = mmc_get_bits(raw_csd, 128, 99, 4);
1089                e = mmc_get_bits(raw_csd, 128, 96, 3);
1090                csd->tran_speed = exp[e] * 10000 * mant[m];
1091                csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1092                csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1093                csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1094                csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1095                csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1096                csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1097                csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) +
1098                    1) * 512 * 1024;
1099                csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1100                csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1101                csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1102                csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1103                csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1104                csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1105                csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1106        } else
1107                panic("unknown SD CSD version");
1108}
1109
1110static void
1111mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
1112{
1113        int m;
1114        int e;
1115
1116        memset(csd, 0, sizeof(*csd));
1117        csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
1118        csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
1119        m = mmc_get_bits(raw_csd, 128, 115, 4);
1120        e = mmc_get_bits(raw_csd, 128, 112, 3);
1121        csd->tacc = exp[e] * mant[m] + 9 / 10;
1122        csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1123        m = mmc_get_bits(raw_csd, 128, 99, 4);
1124        e = mmc_get_bits(raw_csd, 128, 96, 3);
1125        csd->tran_speed = exp[e] * 10000 * mant[m];
1126        csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1127        csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1128        csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1129        csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1130        csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1131        csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1132        csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1133        csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1134        csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1135        csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1136        m = mmc_get_bits(raw_csd, 128, 62, 12);
1137        e = mmc_get_bits(raw_csd, 128, 47, 3);
1138        csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1139        csd->erase_blk_en = 0;
1140        csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
1141            (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1142        csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1143        csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1144        csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1145        csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1146        csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1147}
1148
1149static void
1150mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1151{
1152        unsigned int scr_struct;
1153
1154        memset(scr, 0, sizeof(*scr));
1155
1156        scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1157        if (scr_struct != 0) {
1158                printf("Unrecognised SCR structure version %d\n",
1159                    scr_struct);
1160                return;
1161        }
1162        scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1163        scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1164}
1165
1166static void
1167mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1168    struct mmc_sd_status *sd_status)
1169{
1170
1171        memset(sd_status, 0, sizeof(*sd_status));
1172
1173        sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1174        sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1175        sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1176        sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1177        sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1178        sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1179        sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1180        sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1181        sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1182        sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1183}
1184
1185static int
1186mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1187{
1188        struct mmc_command cmd;
1189        int err;
1190
1191        memset(&cmd, 0, sizeof(cmd));
1192        cmd.opcode = MMC_ALL_SEND_CID;
1193        cmd.arg = 0;
1194        cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1195        cmd.data = NULL;
1196        err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1197        memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1198        return (err);
1199}
1200
1201static int
1202mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1203{
1204        struct mmc_command cmd;
1205        int err;
1206
1207        memset(&cmd, 0, sizeof(cmd));
1208        cmd.opcode = MMC_SEND_CSD;
1209        cmd.arg = rca << 16;
1210        cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1211        cmd.data = NULL;
1212        err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1213        memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1214        return (err);
1215}
1216
1217static int
1218mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1219{
1220        int err;
1221        struct mmc_command cmd;
1222        struct mmc_data data;
1223
1224        memset(&cmd, 0, sizeof(cmd));
1225        memset(&data, 0, sizeof(data));
1226
1227        memset(rawscr, 0, 8);
1228        cmd.opcode = ACMD_SEND_SCR;
1229        cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1230        cmd.arg = 0;
1231        cmd.data = &data;
1232
1233        data.data = rawscr;
1234        data.len = 8;
1235        data.flags = MMC_DATA_READ;
1236
1237        err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES);
1238        rawscr[0] = be32toh(rawscr[0]);
1239        rawscr[1] = be32toh(rawscr[1]);
1240        return (err);
1241}
1242
1243static int
1244mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1245{
1246        struct mmc_command cmd;
1247        struct mmc_data data;
1248        int err, i;
1249
1250        memset(&cmd, 0, sizeof(cmd));
1251        memset(&data, 0, sizeof(data));
1252
1253        memset(rawsdstatus, 0, 64);
1254        cmd.opcode = ACMD_SD_STATUS;
1255        cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1256        cmd.arg = 0;
1257        cmd.data = &data;
1258
1259        data.data = rawsdstatus;
1260        data.len = 64;
1261        data.flags = MMC_DATA_READ;
1262
1263        err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES);
1264        for (i = 0; i < 16; i++)
1265            rawsdstatus[i] = be32toh(rawsdstatus[i]);
1266        return (err);
1267}
1268
1269static int
1270mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1271{
1272        struct mmc_command cmd;
1273        int err;
1274
1275        memset(&cmd, 0, sizeof(cmd));
1276        cmd.opcode = MMC_SET_RELATIVE_ADDR;
1277        cmd.arg = resp << 16;
1278        cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1279        cmd.data = NULL;
1280        err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1281        return (err);
1282}
1283
1284static int
1285mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1286{
1287        struct mmc_command cmd;
1288        int err;
1289
1290        memset(&cmd, 0, sizeof(cmd));
1291        cmd.opcode = SD_SEND_RELATIVE_ADDR;
1292        cmd.arg = 0;
1293        cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1294        cmd.data = NULL;
1295        err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1296        *resp = cmd.resp[0];
1297        return (err);
1298}
1299
1300static int
1301mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1302{
1303        struct mmc_command cmd;
1304        int err;
1305
1306        memset(&cmd, 0, sizeof(cmd));
1307        cmd.opcode = MMC_SET_BLOCKLEN;
1308        cmd.arg = len;
1309        cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1310        cmd.data = NULL;
1311        err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1312        return (err);
1313}
1314
1315static uint32_t
1316mmc_timing_to_dtr(struct mmc_ivars *ivar, enum mmc_bus_timing timing)
1317{
1318
1319        switch (timing) {
1320        case bus_timing_normal:
1321                return (ivar->tran_speed);
1322        case bus_timing_hs:
1323                return (ivar->hs_tran_speed);
1324        case bus_timing_uhs_sdr12:
1325                return (SD_SDR12_MAX);
1326        case bus_timing_uhs_sdr25:
1327                return (SD_SDR25_MAX);
1328        case bus_timing_uhs_ddr50:
1329                return (SD_DDR50_MAX);
1330        case bus_timing_uhs_sdr50:
1331                return (SD_SDR50_MAX);
1332        case bus_timing_uhs_sdr104:
1333                return (SD_SDR104_MAX);
1334        case bus_timing_mmc_ddr52:
1335                return (MMC_TYPE_DDR52_MAX);
1336        case bus_timing_mmc_hs200:
1337        case bus_timing_mmc_hs400:
1338        case bus_timing_mmc_hs400es:
1339                return (MMC_TYPE_HS200_HS400ES_MAX);
1340        }
1341        return (0);
1342}
1343
1344static const char *
1345mmc_timing_to_string(enum mmc_bus_timing timing)
1346{
1347
1348        switch (timing) {
1349        case bus_timing_normal:
1350                return ("normal speed");
1351        case bus_timing_hs:
1352                return ("high speed");
1353        case bus_timing_uhs_sdr12:
1354        case bus_timing_uhs_sdr25:
1355        case bus_timing_uhs_sdr50:
1356        case bus_timing_uhs_sdr104:
1357                return ("single data rate");
1358        case bus_timing_uhs_ddr50:
1359        case bus_timing_mmc_ddr52:
1360                return ("dual data rate");
1361        case bus_timing_mmc_hs200:
1362                return ("HS200");
1363        case bus_timing_mmc_hs400:
1364                return ("HS400");
1365        case bus_timing_mmc_hs400es:
1366                return ("HS400 with enhanced strobe");
1367        }
1368        return ("");
1369}
1370
1371static void
1372mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1373{
1374        enum mmc_bus_timing max_timing, timing;
1375
1376        device_printf(dev, "Card at relative address 0x%04x%s:\n",
1377            ivar->rca, newcard ? " added" : "");
1378        device_printf(dev, " card: %s\n", ivar->card_id_string);
1379        max_timing = bus_timing_normal;
1380        for (timing = bus_timing_max; timing > bus_timing_normal; timing--) {
1381                if (isset(&ivar->timings, timing)) {
1382                        max_timing = timing;
1383                        break;
1384                }
1385        }
1386        device_printf(dev, " bus: %ubit, %uMHz (%s timing)\n",
1387            (ivar->bus_width == bus_width_1 ? 1 :
1388            (ivar->bus_width == bus_width_4 ? 4 : 8)),
1389            mmc_timing_to_dtr(ivar, timing) / 1000000,
1390            mmc_timing_to_string(timing));
1391        device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1392            ivar->sec_count, ivar->erase_sector,
1393            ivar->read_only ? ", read-only" : "");
1394}
1395
1396static void
1397mmc_discover_cards(struct mmc_softc *sc)
1398{
1399        u_char switch_res[64];
1400        uint32_t raw_cid[4];
1401        struct mmc_ivars *ivar = NULL;
1402        device_t *devlist;
1403        device_t child;
1404        int devcount, err, host_caps, i, newcard;
1405        uint32_t resp, sec_count, status;
1406        uint16_t rca = 2;
1407
1408        host_caps = mmcbr_get_caps(sc->dev);
1409        if (bootverbose || mmc_debug)
1410                device_printf(sc->dev, "Probing cards\n");
1411        while (1) {
1412                sc->squelched++; /* Errors are expected, squelch reporting. */
1413                err = mmc_all_send_cid(sc, raw_cid);
1414                sc->squelched--;
1415                if (err == MMC_ERR_TIMEOUT)
1416                        break;
1417                if (err != MMC_ERR_NONE) {
1418                        device_printf(sc->dev, "Error reading CID %d\n", err);
1419                        break;
1420                }
1421                newcard = 1;
1422                if ((err = device_get_children(sc->dev, &devlist,
1423                    &devcount)) != 0)
1424                        return;
1425                for (i = 0; i < devcount; i++) {
1426                        ivar = device_get_ivars(devlist[i]);
1427                        if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) ==
1428                            0) {
1429                                newcard = 0;
1430                                break;
1431                        }
1432                }
1433                free(devlist, M_TEMP);
1434                if (bootverbose || mmc_debug) {
1435                        device_printf(sc->dev,
1436                            "%sard detected (CID %08x%08x%08x%08x)\n",
1437                            newcard ? "New c" : "C",
1438                            raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1439                }
1440                if (newcard) {
1441                        ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1442                            M_WAITOK | M_ZERO);
1443                        memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1444                }
1445                if (mmcbr_get_ro(sc->dev))
1446                        ivar->read_only = 1;
1447                ivar->bus_width = bus_width_1;
1448                setbit(&ivar->timings, bus_timing_normal);
1449                ivar->mode = mmcbr_get_mode(sc->dev);
1450                if (ivar->mode == mode_sd) {
1451                        mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1452                        err = mmc_send_relative_addr(sc, &resp);
1453                        if (err != MMC_ERR_NONE) {
1454                                device_printf(sc->dev,
1455                                    "Error getting RCA %d\n", err);
1456                                break;
1457                        }
1458                        ivar->rca = resp >> 16;
1459                        /* Get card CSD. */
1460                        err = mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1461                        if (err != MMC_ERR_NONE) {
1462                                device_printf(sc->dev,
1463                                    "Error getting CSD %d\n", err);
1464                                break;
1465                        }
1466                        if (bootverbose || mmc_debug)
1467                                device_printf(sc->dev,
1468                                    "%sard detected (CSD %08x%08x%08x%08x)\n",
1469                                    newcard ? "New c" : "C", ivar->raw_csd[0],
1470                                    ivar->raw_csd[1], ivar->raw_csd[2],
1471                                    ivar->raw_csd[3]);
1472                        mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1473                        ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1474                        if (ivar->csd.csd_structure > 0)
1475                                ivar->high_cap = 1;
1476                        ivar->tran_speed = ivar->csd.tran_speed;
1477                        ivar->erase_sector = ivar->csd.erase_sector *
1478                            ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1479
1480                        err = mmc_send_status(sc->dev, sc->dev, ivar->rca,
1481                            &status);
1482                        if (err != MMC_ERR_NONE) {
1483                                device_printf(sc->dev,
1484                                    "Error reading card status %d\n", err);
1485                                break;
1486                        }
1487                        if ((status & R1_CARD_IS_LOCKED) != 0) {
1488                                device_printf(sc->dev,
1489                                    "Card is password protected, skipping.\n");
1490                                break;
1491                        }
1492
1493                        /* Get card SCR.  Card must be selected to fetch it. */
1494                        err = mmc_select_card(sc, ivar->rca);
1495                        if (err != MMC_ERR_NONE) {
1496                                device_printf(sc->dev,
1497                                    "Error selecting card %d\n", err);
1498                                break;
1499                        }
1500                        err = mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1501                        if (err != MMC_ERR_NONE) {
1502                                device_printf(sc->dev,
1503                                    "Error reading SCR %d\n", err);
1504                                break;
1505                        }
1506                        mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1507                        /* Get card switch capabilities (command class 10). */
1508                        if ((ivar->scr.sda_vsn >= 1) &&
1509                            (ivar->csd.ccc & (1 << 10))) {
1510                                err = mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1511                                    SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1512                                    switch_res);
1513                                if (err == MMC_ERR_NONE &&
1514                                    switch_res[13] & (1 << SD_SWITCH_HS_MODE)) {
1515                                        setbit(&ivar->timings, bus_timing_hs);
1516                                        ivar->hs_tran_speed = SD_HS_MAX;
1517                                }
1518                        }
1519
1520                        /*
1521                         * We deselect then reselect the card here.  Some cards
1522                         * become unselected and timeout with the above two
1523                         * commands, although the state tables / diagrams in the
1524                         * standard suggest they go back to the transfer state.
1525                         * Other cards don't become deselected, and if we
1526                         * attempt to blindly re-select them, we get timeout
1527                         * errors from some controllers.  So we deselect then
1528                         * reselect to handle all situations.  The only thing we
1529                         * use from the sd_status is the erase sector size, but
1530                         * it is still nice to get that right.
1531                         */
1532                        mmc_select_card(sc, 0);
1533                        (void)mmc_select_card(sc, ivar->rca);
1534                        (void)mmc_app_sd_status(sc, ivar->rca,
1535                            ivar->raw_sd_status);
1536                        mmc_app_decode_sd_status(ivar->raw_sd_status,
1537                            &ivar->sd_status);
1538                        if (ivar->sd_status.au_size != 0) {
1539                                ivar->erase_sector =
1540                                    16 << ivar->sd_status.au_size;
1541                        }
1542                        /* Find max supported bus width. */
1543                        if ((host_caps & MMC_CAP_4_BIT_DATA) &&
1544                            (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1545                                ivar->bus_width = bus_width_4;
1546
1547                        /*
1548                         * Some cards that report maximum I/O block sizes
1549                         * greater than 512 require the block length to be
1550                         * set to 512, even though that is supposed to be
1551                         * the default.  Example:
1552                         *
1553                         * Transcend 2GB SDSC card, CID:
1554                         * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1555                         */
1556                        if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1557                            ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1558                                mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1559
1560                        mmc_format_card_id_string(ivar);
1561
1562                        if (bootverbose || mmc_debug)
1563                                mmc_log_card(sc->dev, ivar, newcard);
1564                        if (newcard) {
1565                                /* Add device. */
1566                                child = device_add_child(sc->dev, NULL, -1);
1567                                device_set_ivars(child, ivar);
1568                        }
1569                        mmc_select_card(sc, 0);
1570                        return;
1571                }
1572                ivar->rca = rca++;
1573                err = mmc_set_relative_addr(sc, ivar->rca);
1574                if (err != MMC_ERR_NONE) {
1575                        device_printf(sc->dev, "Error setting RCA %d\n", err);
1576                        break;
1577                }
1578                /* Get card CSD. */
1579                err = mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1580                if (err != MMC_ERR_NONE) {
1581                        device_printf(sc->dev, "Error getting CSD %d\n", err);
1582                        break;
1583                }
1584                if (bootverbose || mmc_debug)
1585                        device_printf(sc->dev,
1586                            "%sard detected (CSD %08x%08x%08x%08x)\n",
1587                            newcard ? "New c" : "C", ivar->raw_csd[0],
1588                            ivar->raw_csd[1], ivar->raw_csd[2],
1589                            ivar->raw_csd[3]);
1590
1591                mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1592                ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1593                ivar->tran_speed = ivar->csd.tran_speed;
1594                ivar->erase_sector = ivar->csd.erase_sector *
1595                    ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1596
1597                err = mmc_send_status(sc->dev, sc->dev, ivar->rca, &status);
1598                if (err != MMC_ERR_NONE) {
1599                        device_printf(sc->dev,
1600                            "Error reading card status %d\n", err);
1601                        break;
1602                }
1603                if ((status & R1_CARD_IS_LOCKED) != 0) {
1604                        device_printf(sc->dev,
1605                            "Card is password protected, skipping.\n");
1606                        break;
1607                }
1608
1609                err = mmc_select_card(sc, ivar->rca);
1610                if (err != MMC_ERR_NONE) {
1611                        device_printf(sc->dev, "Error selecting card %d\n",
1612                            err);
1613                        break;
1614                }
1615
1616                /* Only MMC >= 4.x devices support EXT_CSD. */
1617                if (ivar->csd.spec_vers >= 4) {
1618                        err = mmc_send_ext_csd(sc->dev, sc->dev,
1619                            ivar->raw_ext_csd);
1620                        if (err != MMC_ERR_NONE) {
1621                                device_printf(sc->dev,
1622                                    "Error reading EXT_CSD %d\n", err);
1623                                break;
1624                        }
1625                        /* Handle extended capacity from EXT_CSD */
1626                        sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1627                            (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1628                            (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1629                            (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1630                        if (sec_count != 0) {
1631                                ivar->sec_count = sec_count;
1632                                ivar->high_cap = 1;
1633                        }
1634                        /* Get device speeds beyond normal mode. */
1635                        if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] &
1636                            EXT_CSD_CARD_TYPE_HS_52) != 0) {
1637                                setbit(&ivar->timings, bus_timing_hs);
1638                                ivar->hs_tran_speed = MMC_TYPE_HS_52_MAX;
1639                        } else if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] &
1640                            EXT_CSD_CARD_TYPE_HS_26) != 0) {
1641                                setbit(&ivar->timings, bus_timing_hs);
1642                                ivar->hs_tran_speed = MMC_TYPE_HS_26_MAX;
1643                        }
1644                        if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] &
1645                            EXT_CSD_CARD_TYPE_DDR_52_1_2V) != 0 &&
1646                            (host_caps & MMC_CAP_SIGNALING_120) != 0) {
1647                                setbit(&ivar->timings, bus_timing_mmc_ddr52);
1648                                setbit(&ivar->vccq_120, bus_timing_mmc_ddr52);
1649                        }
1650                        if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] &
1651                            EXT_CSD_CARD_TYPE_DDR_52_1_8V) != 0 &&
1652                            (host_caps & MMC_CAP_SIGNALING_180) != 0) {
1653                                setbit(&ivar->timings, bus_timing_mmc_ddr52);
1654                                setbit(&ivar->vccq_180, bus_timing_mmc_ddr52);
1655                        }
1656                        /*
1657                         * Determine generic switch timeout (provided in
1658                         * units of 10 ms), defaulting to 500 ms.
1659                         */
1660                        ivar->cmd6_time = 500 * 1000;
1661                        if (ivar->csd.spec_vers >= 6)
1662                                ivar->cmd6_time = 10 *
1663                                    ivar->raw_ext_csd[EXT_CSD_GEN_CMD6_TIME];
1664                        /* Find max supported bus width. */
1665                        ivar->bus_width = mmc_test_bus_width(sc);
1666                        /* Handle HC erase sector size. */
1667                        if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1668                                ivar->erase_sector = 1024 *
1669                                    ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1670                                err = mmc_switch(sc->dev, sc->dev, ivar->rca,
1671                                    EXT_CSD_CMD_SET_NORMAL,
1672                                    EXT_CSD_ERASE_GRP_DEF,
1673                                    EXT_CSD_ERASE_GRP_DEF_EN,
1674                                    ivar->cmd6_time, true);
1675                                if (err != MMC_ERR_NONE) {
1676                                        device_printf(sc->dev,
1677                                            "Error setting erase group %d\n",
1678                                            err);
1679                                        break;
1680                                }
1681                        }
1682                }
1683
1684                /*
1685                 * Some cards that report maximum I/O block sizes greater
1686                 * than 512 require the block length to be set to 512, even
1687                 * though that is supposed to be the default.  Example:
1688                 *
1689                 * Transcend 2GB SDSC card, CID:
1690                 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1691                 */
1692                if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1693                    ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1694                        mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1695
1696                mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid,
1697                    ivar->raw_ext_csd[EXT_CSD_REV] >= 5);
1698                mmc_format_card_id_string(ivar);
1699
1700                if (bootverbose || mmc_debug)
1701                        mmc_log_card(sc->dev, ivar, newcard);
1702                if (newcard) {
1703                        /* Add device. */
1704                        child = device_add_child(sc->dev, NULL, -1);
1705                        device_set_ivars(child, ivar);
1706                }
1707                mmc_select_card(sc, 0);
1708        }
1709}
1710
1711static void
1712mmc_rescan_cards(struct mmc_softc *sc)
1713{
1714        struct mmc_ivars *ivar;
1715        device_t *devlist;
1716        int err, i, devcount;
1717
1718        if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1719                return;
1720        for (i = 0; i < devcount; i++) {
1721                ivar = device_get_ivars(devlist[i]);
1722                if (mmc_select_card(sc, ivar->rca) != MMC_ERR_NONE) {
1723                        if (bootverbose || mmc_debug)
1724                                device_printf(sc->dev,
1725                                    "Card at relative address %d lost.\n",
1726                                    ivar->rca);
1727                        device_delete_child(sc->dev, devlist[i]);
1728                        free(ivar, M_DEVBUF);
1729                }
1730        }
1731        free(devlist, M_TEMP);
1732        mmc_select_card(sc, 0);
1733}
1734
1735static int
1736mmc_delete_cards(struct mmc_softc *sc)
1737{
1738        struct mmc_ivars *ivar;
1739        device_t *devlist;
1740        int err, i, devcount;
1741
1742        if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1743                return (err);
1744        for (i = 0; i < devcount; i++) {
1745                ivar = device_get_ivars(devlist[i]);
1746                if (bootverbose || mmc_debug)
1747                        device_printf(sc->dev,
1748                            "Card at relative address %d deleted.\n",
1749                            ivar->rca);
1750                device_delete_child(sc->dev, devlist[i]);
1751                free(ivar, M_DEVBUF);
1752        }
1753        free(devlist, M_TEMP);
1754        return (0);
1755}
1756
1757static void
1758mmc_go_discovery(struct mmc_softc *sc)
1759{
1760        uint32_t ocr;
1761        device_t dev;
1762        int err;
1763
1764        dev = sc->dev;
1765        if (mmcbr_get_power_mode(dev) != power_on) {
1766                /*
1767                 * First, try SD modes
1768                 */
1769                sc->squelched++; /* Errors are expected, squelch reporting. */
1770                mmcbr_set_mode(dev, mode_sd);
1771                mmc_power_up(sc);
1772                mmcbr_set_bus_mode(dev, pushpull);
1773                if (bootverbose || mmc_debug)
1774                        device_printf(sc->dev, "Probing bus\n");
1775                mmc_idle_cards(sc);
1776                err = mmc_send_if_cond(sc, 1);
1777                if ((bootverbose || mmc_debug) && err == 0)
1778                        device_printf(sc->dev,
1779                            "SD 2.0 interface conditions: OK\n");
1780                if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1781                        if (bootverbose || mmc_debug)
1782                                device_printf(sc->dev, "SD probe: failed\n");
1783                        /*
1784                         * Failed, try MMC
1785                         */
1786                        mmcbr_set_mode(dev, mode_mmc);
1787                        if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1788                                if (bootverbose || mmc_debug)
1789                                        device_printf(sc->dev,
1790                                            "MMC probe: failed\n");
1791                                ocr = 0; /* Failed both, powerdown. */
1792                        } else if (bootverbose || mmc_debug)
1793                                device_printf(sc->dev,
1794                                    "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1795                } else if (bootverbose || mmc_debug)
1796                        device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n",
1797                            ocr);
1798                sc->squelched--;
1799
1800                mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1801                if (mmcbr_get_ocr(dev) != 0)
1802                        mmc_idle_cards(sc);
1803        } else {
1804                mmcbr_set_bus_mode(dev, opendrain);
1805                mmcbr_set_clock(dev, SD_MMC_CARD_ID_FREQUENCY);
1806                mmcbr_update_ios(dev);
1807                /* XXX recompute vdd based on new cards? */
1808        }
1809        /*
1810         * Make sure that we have a mutually agreeable voltage to at least
1811         * one card on the bus.
1812         */
1813        if (bootverbose || mmc_debug)
1814                device_printf(sc->dev, "Current OCR: 0x%08x\n",
1815                    mmcbr_get_ocr(dev));
1816        if (mmcbr_get_ocr(dev) == 0) {
1817                device_printf(sc->dev, "No compatible cards found on bus\n");
1818                mmc_delete_cards(sc);
1819                mmc_power_down(sc);
1820                return;
1821        }
1822        /*
1823         * Reselect the cards after we've idled them above.
1824         */
1825        if (mmcbr_get_mode(dev) == mode_sd) {
1826                err = mmc_send_if_cond(sc, 1);
1827                mmc_send_app_op_cond(sc,
1828                    (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1829        } else
1830                mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL);
1831        mmc_discover_cards(sc);
1832        mmc_rescan_cards(sc);
1833
1834        mmcbr_set_bus_mode(dev, pushpull);
1835        mmcbr_update_ios(dev);
1836        mmc_calculate_clock(sc);
1837}
1838
1839static int
1840mmc_calculate_clock(struct mmc_softc *sc)
1841{
1842        device_t *kids;
1843        struct mmc_ivars *ivar;
1844        int host_caps, i, nkid;
1845        uint32_t dtr, max_dtr;
1846        enum mmc_bus_timing max_timing, timing;
1847        bool changed;
1848
1849        max_dtr = mmcbr_get_f_max(sc->dev);
1850        host_caps = mmcbr_get_caps(sc->dev);
1851        if ((host_caps & MMC_CAP_MMC_DDR52) != 0)
1852                max_timing = bus_timing_mmc_ddr52;
1853        else if ((host_caps & MMC_CAP_HSPEED) != 0)
1854                max_timing = bus_timing_hs;
1855        else
1856                max_timing = bus_timing_normal;
1857        if (device_get_children(sc->dev, &kids, &nkid) != 0)
1858                panic("can't get children");
1859        do {
1860                changed = false;
1861                for (i = 0; i < nkid; i++) {
1862                        ivar = device_get_ivars(kids[i]);
1863                        if (isclr(&ivar->timings, max_timing)) {
1864                                for (timing = max_timing; timing >=
1865                                    bus_timing_normal; timing--) {
1866                                        if (isset(&ivar->timings, timing)) {
1867                                                max_timing = timing;
1868                                                break;
1869                                        }
1870                                }
1871                                changed = true;
1872                        }
1873                        dtr = mmc_timing_to_dtr(ivar, max_timing);
1874                        if (dtr < max_dtr) {
1875                                max_dtr = dtr;
1876                                changed = true;
1877                        }
1878                }
1879        } while (changed == true);
1880        if (bootverbose || mmc_debug) {
1881                device_printf(sc->dev,
1882                    "setting transfer rate to %d.%03dMHz (%s timing)\n",
1883                    max_dtr / 1000000, (max_dtr / 1000) % 1000,
1884                    mmc_timing_to_string(max_timing));
1885        }
1886        for (i = 0; i < nkid; i++) {
1887                ivar = device_get_ivars(kids[i]);
1888                if ((ivar->timings & ~(1 << bus_timing_normal)) == 0)
1889                        continue;
1890                if (mmc_select_card(sc, ivar->rca) != MMC_ERR_NONE ||
1891                    mmc_set_timing(sc, ivar, max_timing) != MMC_ERR_NONE)
1892                        device_printf(sc->dev, "Card at relative address %d "
1893                            "failed to set timing.\n", ivar->rca);
1894        }
1895        mmc_select_card(sc, 0);
1896        free(kids, M_TEMP);
1897        mmcbr_set_clock(sc->dev, max_dtr);
1898        mmcbr_update_ios(sc->dev);
1899        return (max_dtr);
1900}
1901
1902static void
1903mmc_scan(struct mmc_softc *sc)
1904{
1905        device_t dev = sc->dev;
1906
1907        mmc_acquire_bus(dev, dev);
1908        mmc_go_discovery(sc);
1909        mmc_release_bus(dev, dev);
1910
1911        bus_generic_attach(dev);
1912}
1913
1914static int
1915mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1916{
1917        struct mmc_ivars *ivar = device_get_ivars(child);
1918
1919        switch (which) {
1920        default:
1921                return (EINVAL);
1922        case MMC_IVAR_SPEC_VERS:
1923                *result = ivar->csd.spec_vers;
1924                break;
1925        case MMC_IVAR_DSR_IMP:
1926                *result = ivar->csd.dsr_imp;
1927                break;
1928        case MMC_IVAR_MEDIA_SIZE:
1929                *result = ivar->sec_count;
1930                break;
1931        case MMC_IVAR_RCA:
1932                *result = ivar->rca;
1933                break;
1934        case MMC_IVAR_SECTOR_SIZE:
1935                *result = MMC_SECTOR_SIZE;
1936                break;
1937        case MMC_IVAR_TRAN_SPEED:
1938                *result = mmcbr_get_clock(bus);
1939                break;
1940        case MMC_IVAR_READ_ONLY:
1941                *result = ivar->read_only;
1942                break;
1943        case MMC_IVAR_HIGH_CAP:
1944                *result = ivar->high_cap;
1945                break;
1946        case MMC_IVAR_CARD_TYPE:
1947                *result = ivar->mode;
1948                break;
1949        case MMC_IVAR_BUS_WIDTH:
1950                *result = ivar->bus_width;
1951                break;
1952        case MMC_IVAR_ERASE_SECTOR:
1953                *result = ivar->erase_sector;
1954                break;
1955        case MMC_IVAR_MAX_DATA:
1956                *result = mmcbr_get_max_data(bus);
1957                break;
1958        case MMC_IVAR_CARD_ID_STRING:
1959                *(char **)result = ivar->card_id_string;
1960                break;
1961        case MMC_IVAR_CARD_SN_STRING:
1962                *(char **)result = ivar->card_sn_string;
1963                break;
1964        }
1965        return (0);
1966}
1967
1968static int
1969mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1970{
1971
1972        /*
1973         * None are writable ATM
1974         */
1975        return (EINVAL);
1976}
1977
1978static void
1979mmc_delayed_attach(void *xsc)
1980{
1981        struct mmc_softc *sc = xsc;
1982
1983        mmc_scan(sc);
1984        config_intrhook_disestablish(&sc->config_intrhook);
1985}
1986
1987static int
1988mmc_child_location_str(device_t dev, device_t child, char *buf,
1989    size_t buflen)
1990{
1991
1992        snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1993        return (0);
1994}
1995
1996static device_method_t mmc_methods[] = {
1997        /* device_if */
1998        DEVMETHOD(device_probe, mmc_probe),
1999        DEVMETHOD(device_attach, mmc_attach),
2000        DEVMETHOD(device_detach, mmc_detach),
2001        DEVMETHOD(device_suspend, mmc_suspend),
2002        DEVMETHOD(device_resume, mmc_resume),
2003
2004        /* Bus interface */
2005        DEVMETHOD(bus_read_ivar, mmc_read_ivar),
2006        DEVMETHOD(bus_write_ivar, mmc_write_ivar),
2007        DEVMETHOD(bus_child_location_str, mmc_child_location_str),
2008
2009        /* MMC Bus interface */
2010        DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
2011        DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
2012        DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
2013
2014        DEVMETHOD_END
2015};
2016
2017driver_t mmc_driver = {
2018        "mmc",
2019        mmc_methods,
2020        sizeof(struct mmc_softc),
2021};
2022devclass_t mmc_devclass;
2023
2024MODULE_VERSION(mmc, MMC_VERSION);
Note: See TracBrowser for help on using the repository browser.