Deleted Added
sdiff udiff text old ( 183542 ) new ( 183704 )
full compact
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

--- 37 unchanged lines hidden (view full) ---

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
53#include <sys/cdefs.h>
54__FBSDID("$FreeBSD: head/sys/dev/mmc/mmc.c 183542 2008-10-02 07:06:59Z imp $");
55
56#include <sys/param.h>
57#include <sys/systm.h>
58#include <sys/kernel.h>
59#include <sys/malloc.h>
60#include <sys/lock.h>
61#include <sys/module.h>
62#include <sys/mutex.h>
63#include <sys/bus.h>
64
65#include <dev/mmc/mmcreg.h>
66#include <dev/mmc/mmcbrvar.h>
67#include <dev/mmc/mmcvar.h>
68#include "mmcbr_if.h"
69#include "mmcbus_if.h"
70
71struct mmc_softc {

--- 5 unchanged lines hidden (view full) ---

77};
78
79/*
80 * Per-card data
81 */
82struct mmc_ivars {
83 uint32_t raw_cid[4]; /* Raw bits of the CID */
84 uint32_t raw_csd[4]; /* Raw bits of the CSD */
85 uint16_t rca;
86 enum mmc_card_mode mode;
87 struct mmc_cid cid; /* cid decoded */
88 struct mmc_csd csd; /* csd decoded */
89 u_char read_only; /* True when the device is read-only */
90};
91
92#define CMD_RETRIES 3
93
94/* bus entry points */
95static int mmc_probe(device_t dev);
96static int mmc_attach(device_t dev);
97static int mmc_detach(device_t dev);

--- 8 unchanged lines hidden (view full) ---

106#define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
107
108static void mmc_delayed_attach(void *);
109static void mmc_power_down(struct mmc_softc *sc);
110static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
111 int retries);
112static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
113 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
114
115static void
116mmc_ms_delay(int ms)
117{
118 DELAY(1000 * ms); /* XXX BAD */
119}
120
121static int

--- 46 unchanged lines hidden (view full) ---

168
169 return (0);
170}
171
172static int
173mmc_acquire_bus(device_t busdev, device_t dev)
174{
175 struct mmc_softc *sc;
176 int err;
177 int rca;
178
179 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
180 if (err)
181 return (err);
182 sc = device_get_softc(busdev);
183 MMC_LOCK(sc);

--- 6 unchanged lines hidden (view full) ---

190 /*
191 * Keep track of the last rca that we've selected. If
192 * we're asked to do it again, don't. We never
193 * unselect unless the bus code itself wants the mmc
194 * bus, and constantly reselecting causes problems.
195 */
196 rca = mmc_get_rca(dev);
197 if (sc->last_rca != rca) {
198 mmc_wait_for_command(sc, MMC_SELECT_CARD, rca << 16,
199 MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES);
200 sc->last_rca = rca;
201 }
202 /* XXX should set bus width here? */
203 } else {
204 /*
205 * If there's a card selected, stand down.
206 */
207 if (sc->last_rca != 0) {
208 mmc_wait_for_command(sc, MMC_SELECT_CARD, 0,
209 MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES);
210 sc->last_rca = 0;
211 }
212 /* XXX should set bus width here? */
213 }
214
215 return (0);
216}
217
218static int
219mmc_release_bus(device_t busdev, device_t dev)
220{

--- 218 unchanged lines hidden (view full) ---

439 err = MMC_ERR_TIMEOUT;
440 mmc_ms_delay(10);
441 }
442 if (rocr && err == MMC_ERR_NONE)
443 *rocr = cmd.resp[0];
444 return (err);
445}
446
447static void
448mmc_power_up(struct mmc_softc *sc)
449{
450 device_t dev;
451
452 dev = sc->dev;
453 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
454 mmcbr_set_bus_mode(dev, opendrain);
455 mmcbr_set_chip_select(dev, cs_dontcare);
456 mmcbr_set_bus_width(dev, bus_width_1);
457 mmcbr_set_power_mode(dev, power_up);
458 mmcbr_set_clock(dev, 0);
459 mmcbr_update_ios(dev);
460 mmc_ms_delay(1);
461
462 mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev));
463 mmcbr_set_power_mode(dev, power_on);
464 mmcbr_update_ios(dev);
465 mmc_ms_delay(2);
466}
467
468static void
469mmc_power_down(struct mmc_softc *sc)
470{
471 device_t dev = sc->dev;
472
473 mmcbr_set_bus_mode(dev, opendrain);
474 mmcbr_set_chip_select(dev, cs_dontcare);
475 mmcbr_set_bus_width(dev, bus_width_1);
476 mmcbr_set_power_mode(dev, power_off);
477 mmcbr_set_clock(dev, 0);
478 mmcbr_update_ios(dev);
479}
480
481static uint32_t
482mmc_get_bits(uint32_t *bits, int start, int size)
483{
484 const int i = 3 - (start / 32);
485 const int shift = start & 31;
486 uint32_t retval = bits[i] >> shift;
487 if (size + shift > 32)
488 retval |= bits[i - 1] << (32 - shift);

--- 12 unchanged lines hidden (view full) ---

501 cid->oid = mmc_get_bits(raw_cid, 104, 16);
502 for (i = 0; i < 5; i++)
503 cid->pnm[i] = mmc_get_bits(raw_cid, 96 - i * 8, 8);
504 cid->prv = mmc_get_bits(raw_cid, 56, 8);
505 cid->psn = mmc_get_bits(raw_cid, 24, 32);
506 cid->mdt_year = mmc_get_bits(raw_cid, 12, 8) + 2001;
507 cid->mdt_month = mmc_get_bits(raw_cid, 8, 4);
508 } else {
509 /* XXX write me */
510 panic("write mmc cid decoder");
511 }
512}
513
514static const int exp[8] = {
515 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
516};
517static const int mant[16] = {
518 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80

--- 39 unchanged lines hidden (view full) ---

558 csd->erase_blk_en = mmc_get_bits(raw_csd, 46, 1);
559 csd->sector_size = mmc_get_bits(raw_csd, 39, 7);
560 csd->wp_grp_size = mmc_get_bits(raw_csd, 32, 7);
561 csd->wp_grp_enable = mmc_get_bits(raw_csd, 31, 1);
562 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 26, 3);
563 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 22, 4);
564 csd->write_bl_partial = mmc_get_bits(raw_csd, 21, 1);
565 } else if (v == 1) {
566 panic("Write SDHC CSD parser");
567 } else
568 panic("unknown SD CSD version");
569 } else {
570 panic("Write a MMC CSD parser");
571 }
572}
573
574static int
575mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
576{
577 struct mmc_command cmd;
578 int err;
579
580 cmd.opcode = MMC_ALL_SEND_CID;
581 cmd.arg = 0;

--- 15 unchanged lines hidden (view full) ---

597 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
598 cmd.data = NULL;
599 err = mmc_wait_for_cmd(sc, &cmd, 0);
600 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
601 return (err);
602}
603
604static int
605mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
606{
607 struct mmc_command cmd;
608 int err;
609
610 cmd.opcode = SD_SEND_RELATIVE_ADDR;
611 cmd.arg = 0;
612 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;

--- 5 unchanged lines hidden (view full) ---

618
619static void
620mmc_discover_cards(struct mmc_softc *sc)
621{
622 struct mmc_ivars *ivar;
623 int err;
624 uint32_t resp;
625 device_t child;
626
627 while (1) {
628 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
629 M_WAITOK | M_ZERO);
630 if (!ivar)
631 return;
632 err = mmc_all_send_cid(sc, ivar->raw_cid);
633 if (err == MMC_ERR_TIMEOUT)
634 break;
635 if (err != MMC_ERR_NONE) {
636 device_printf(sc->dev, "Error reading CID %d\n", err);
637 break;
638 }
639 if (mmcbr_get_mode(sc->dev) == mode_sd) {
640 ivar->mode = mode_sd;
641 mmc_decode_cid(1, ivar->raw_cid, &ivar->cid);
642 mmc_send_relative_addr(sc, &resp);
643 ivar->rca = resp >> 16;
644 if (mmcbr_get_ro(sc->dev))
645 ivar->read_only = 1;
646 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
647 mmc_decode_csd(1, ivar->raw_csd, &ivar->csd);
648 printf("SD CARD: %lld bytes\n", (long long)
649 ivar->csd.capacity);
650 child = device_add_child(sc->dev, NULL, -1);
651 device_set_ivars(child, ivar);
652 return;
653 }
654 panic("Write MMC card code here");
655 }
656 free(ivar, M_DEVBUF);
657}
658
659static void
660mmc_go_discovery(struct mmc_softc *sc)
661{
662 uint32_t ocr;
663 device_t dev;
664
665 dev = sc->dev;
666 if (mmcbr_get_power_mode(dev) != power_on) {
667 /*
668 * First, try SD modes
669 */
670 mmcbr_set_mode(dev, mode_sd);
671 mmc_power_up(sc);
672 mmcbr_set_bus_mode(dev, pushpull);
673 mmc_idle_cards(sc);
674 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
675 /*
676 * Failed, try MMC
677 */
678 mmcbr_set_mode(dev, mode_mmc);
679 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE)
680 return; /* Failed both, punt! XXX powerdown? */
681 }
682 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));

--- 9 unchanged lines hidden (view full) ---

692 * Make sure that we have a mutually agreeable voltage to at least
693 * one card on the bus.
694 */
695 if (mmcbr_get_ocr(dev) == 0)
696 return;
697 /*
698 * Reselect the cards after we've idled them above.
699 */
700 if (mmcbr_get_mode(dev) == mode_sd)
701 mmc_send_app_op_cond(sc, mmcbr_get_ocr(dev), NULL);
702 else
703 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
704 mmc_discover_cards(sc);
705
706 mmcbr_set_bus_mode(dev, pushpull);
707 mmcbr_update_ios(dev);
708 bus_generic_attach(dev);
709/* mmc_update_children_sysctl(dev);*/
710}
711
712static int
713mmc_calculate_clock(struct mmc_softc *sc)
714{
715 int max_dtr = 0;
716 int nkid, i, f_min, f_max;
717 device_t *kids;
718
719 f_min = mmcbr_get_f_min(sc->dev);
720 f_max = mmcbr_get_f_max(sc->dev);
721 max_dtr = f_max;
722 if (device_get_children(sc->dev, &kids, &nkid) != 0)
723 panic("can't get children");
724 for (i = 0; i < nkid; i++)
725 if (mmc_get_tran_speed(kids[i]) < max_dtr)
726 max_dtr = mmc_get_tran_speed(kids[i]);
727 free(kids, M_TEMP);
728 device_printf(sc->dev, "setting transfer rate to %d.%03dMHz\n",
729 max_dtr / 1000000, (max_dtr / 1000) % 1000);
730 return (max_dtr);
731}
732
733static void
734mmc_scan(struct mmc_softc *sc)
735{
736 device_t dev;
737
738 dev = sc->dev;
739 mmc_acquire_bus(dev, dev);
740
741 if (mmcbr_get_power_mode(dev) == power_on)
742 mmc_rescan_cards(sc);
743 mmc_go_discovery(sc);
744 mmcbr_set_clock(dev, mmc_calculate_clock(sc));
745 mmcbr_update_ios(dev);
746
747 mmc_release_bus(dev, dev);
748 /* XXX probe/attach/detach children? */
749}
750
751static int
752mmc_read_ivar(device_t bus, device_t child, int which, u_char *result)
753{

--- 15 unchanged lines hidden (view full) ---

769 *(int *)result = MMC_SECTOR_SIZE;
770 break;
771 case MMC_IVAR_TRAN_SPEED:
772 *(int *)result = ivar->csd.tran_speed;
773 break;
774 case MMC_IVAR_READ_ONLY:
775 *(int *)result = ivar->read_only;
776 break;
777 }
778 return (0);
779}
780
781static int
782mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
783{
784 /*

--- 43 unchanged lines hidden ---