Deleted Added
full compact
mmc.c (227309) mmc.c (234524)
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>
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 227309 2011-11-07 15:43:11Z ed $");
54__FBSDID("$FreeBSD: head/sys/dev/mmc/mmc.c 234524 2012-04-21 01:51:16Z marius $");
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>

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

96 u_char read_only; /* True when the device is read-only */
97 u_char bus_width; /* Bus width to use */
98 u_char timing; /* Bus timing support */
99 u_char high_cap; /* High Capacity card (block addressed) */
100 uint32_t sec_count; /* Card capacity in 512byte blocks */
101 uint32_t tran_speed; /* Max speed in normal mode */
102 uint32_t hs_tran_speed; /* Max speed in high speed mode */
103 uint32_t erase_sector; /* Card native erase sector size */
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>

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

96 u_char read_only; /* True when the device is read-only */
97 u_char bus_width; /* Bus width to use */
98 u_char timing; /* Bus timing support */
99 u_char high_cap; /* High Capacity card (block addressed) */
100 uint32_t sec_count; /* Card capacity in 512byte blocks */
101 uint32_t tran_speed; /* Max speed in normal mode */
102 uint32_t hs_tran_speed; /* Max speed in high speed mode */
103 uint32_t erase_sector; /* Card native erase sector size */
104 char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
104};
105
106#define CMD_RETRIES 3
107
108static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
109
110static int mmc_debug;
111SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level");

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

135 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
136static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
137static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width);
138static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr);
139static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
140static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
141static void mmc_scan(struct mmc_softc *sc);
142static int mmc_delete_cards(struct mmc_softc *sc);
105};
106
107#define CMD_RETRIES 3
108
109static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
110
111static int mmc_debug;
112SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level");

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

136 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
137static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
138static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width);
139static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr);
140static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
141static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
142static void mmc_scan(struct mmc_softc *sc);
143static int mmc_delete_cards(struct mmc_softc *sc);
144static void mmc_format_card_id_string(struct mmc_ivars *ivar);
143
144static void
145mmc_ms_delay(int ms)
146{
147 DELAY(1000 * ms); /* XXX BAD */
148}
149
150static int

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

601mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
602{
603 struct mmc_command cmd;
604 int err;
605 uint8_t value;
606
607 if (mmcbr_get_mode(sc->dev) == mode_sd) {
608 memset(&cmd, 0, sizeof(struct mmc_command));
145
146static void
147mmc_ms_delay(int ms)
148{
149 DELAY(1000 * ms); /* XXX BAD */
150}
151
152static int

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

603mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
604{
605 struct mmc_command cmd;
606 int err;
607 uint8_t value;
608
609 if (mmcbr_get_mode(sc->dev) == mode_sd) {
610 memset(&cmd, 0, sizeof(struct mmc_command));
611 cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
612 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
613 cmd.arg = SD_CLR_CARD_DETECT;
614 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
615 if (err != 0)
616 return (err);
617 memset(&cmd, 0, sizeof(struct mmc_command));
609 cmd.opcode = ACMD_SET_BUS_WIDTH;
610 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
611 switch (width) {
612 case bus_width_1:
613 cmd.arg = SD_BUS_WIDTH_1;
614 break;
615 case bus_width_4:
616 cmd.arg = SD_BUS_WIDTH_4;

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

783 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
784 cid->pnm[6] = 0;
785 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
786 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
787 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
788 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
789}
790
618 cmd.opcode = ACMD_SET_BUS_WIDTH;
619 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
620 switch (width) {
621 case bus_width_1:
622 cmd.arg = SD_BUS_WIDTH_1;
623 break;
624 case bus_width_4:
625 cmd.arg = SD_BUS_WIDTH_4;

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

792 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
793 cid->pnm[6] = 0;
794 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
795 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
796 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
797 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
798}
799
800static void
801mmc_format_card_id_string(struct mmc_ivars *ivar)
802{
803 char oidstr[8];
804 uint8_t c1;
805 uint8_t c2;
806
807 /*
808 * Format a card ID string for use by the mmcsd driver, it's what
809 * appears between the <> in the following:
810 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
811 * 22.5MHz/4bit/128-block
812 *
813 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
814 * and our max formatted length is currently 55 bytes if every field
815 * contains the largest value.
816 *
817 * Sometimes the oid is two printable ascii chars; when it's not,
818 * format it as 0xnnnn instead.
819 */
820 c1 = (ivar->cid.oid >> 8) & 0x0ff;
821 c2 = ivar->cid.oid & 0x0ff;
822 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
823 snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
824 else
825 snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
826 snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
827 "%s%s %s %d.%d SN %d MFG %02d/%04d by %d %s",
828 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
829 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
830 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
831 ivar->cid.mid, oidstr);
832}
833
791static const int exp[8] = {
792 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
793};
834static const int exp[8] = {
835 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
836};
837
794static const int mant[16] = {
838static const int mant[16] = {
795 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
839 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
796};
840};
841
797static const int cur_min[8] = {
798 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
799};
842static const int cur_min[8] = {
843 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
844};
845
800static const int cur_max[8] = {
801 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
802};
803
804static void
805mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
806{
807 int v;

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

1075 return (err);
1076}
1077
1078static void
1079mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1080{
1081 device_printf(dev, "Card at relative address %d%s:\n",
1082 ivar->rca, newcard ? " added" : "");
846static const int cur_max[8] = {
847 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
848};
849
850static void
851mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
852{
853 int v;

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

1121 return (err);
1122}
1123
1124static void
1125mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1126{
1127 device_printf(dev, "Card at relative address %d%s:\n",
1128 ivar->rca, newcard ? " added" : "");
1083 device_printf(dev, " card: %s%s (0x%x/0x%x/\"%s\" rev %d.%d "
1084 "m/d %02d.%04d s/n %08x)\n",
1085 ivar->mode == mode_sd ? "SD" : "MMC",
1086 ivar->high_cap ? " High Capacity" : "",
1087 ivar->cid.mid, ivar->cid.oid,
1088 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
1089 ivar->cid.mdt_month, ivar->cid.mdt_year, ivar->cid.psn);
1129 device_printf(dev, " card: %s\n", ivar->card_id_string);
1090 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1091 (ivar->bus_width == bus_width_1 ? 1 :
1092 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1093 (ivar->timing == bus_timing_hs ?
1094 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1095 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1096 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1097 ivar->sec_count, ivar->erase_sector,

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

1183 ivar->erase_sector =
1184 16 << ivar->sd_status.au_size;
1185 }
1186 mmc_select_card(sc, 0);
1187 /* Find max supported bus width. */
1188 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1189 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1190 ivar->bus_width = bus_width_4;
1130 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1131 (ivar->bus_width == bus_width_1 ? 1 :
1132 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1133 (ivar->timing == bus_timing_hs ?
1134 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1135 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1136 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1137 ivar->sec_count, ivar->erase_sector,

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

1223 ivar->erase_sector =
1224 16 << ivar->sd_status.au_size;
1225 }
1226 mmc_select_card(sc, 0);
1227 /* Find max supported bus width. */
1228 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1229 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1230 ivar->bus_width = bus_width_4;
1231 mmc_format_card_id_string(ivar);
1191 if (bootverbose || mmc_debug)
1192 mmc_log_card(sc->dev, ivar, newcard);
1193 if (newcard) {
1194 /* Add device. */
1195 child = device_add_child(sc->dev, NULL, -1);
1196 device_set_ivars(child, ivar);
1197 }
1198 return;

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

1240 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1241 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1242 EXT_CSD_ERASE_GRP_DEF, 1);
1243 }
1244 } else {
1245 ivar->bus_width = bus_width_1;
1246 ivar->timing = bus_timing_normal;
1247 }
1232 if (bootverbose || mmc_debug)
1233 mmc_log_card(sc->dev, ivar, newcard);
1234 if (newcard) {
1235 /* Add device. */
1236 child = device_add_child(sc->dev, NULL, -1);
1237 device_set_ivars(child, ivar);
1238 }
1239 return;

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

1281 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1282 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1283 EXT_CSD_ERASE_GRP_DEF, 1);
1284 }
1285 } else {
1286 ivar->bus_width = bus_width_1;
1287 ivar->timing = bus_timing_normal;
1288 }
1289 mmc_format_card_id_string(ivar);
1248 if (bootverbose || mmc_debug)
1249 mmc_log_card(sc->dev, ivar, newcard);
1250 if (newcard) {
1251 /* Add device. */
1252 child = device_add_child(sc->dev, NULL, -1);
1253 device_set_ivars(child, ivar);
1254 }
1255 }

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

1472 *result = ivar->bus_width;
1473 break;
1474 case MMC_IVAR_ERASE_SECTOR:
1475 *result = ivar->erase_sector;
1476 break;
1477 case MMC_IVAR_MAX_DATA:
1478 *result = mmcbr_get_max_data(bus);
1479 break;
1290 if (bootverbose || mmc_debug)
1291 mmc_log_card(sc->dev, ivar, newcard);
1292 if (newcard) {
1293 /* Add device. */
1294 child = device_add_child(sc->dev, NULL, -1);
1295 device_set_ivars(child, ivar);
1296 }
1297 }

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

1514 *result = ivar->bus_width;
1515 break;
1516 case MMC_IVAR_ERASE_SECTOR:
1517 *result = ivar->erase_sector;
1518 break;
1519 case MMC_IVAR_MAX_DATA:
1520 *result = mmcbr_get_max_data(bus);
1521 break;
1522 case MMC_IVAR_CARD_ID_STRING:
1523 *(char **)result = ivar->card_id_string;
1524 break;
1480 }
1481 return (0);
1482}
1483
1484static int
1485mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1486{
1487 /*

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

1522 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1523 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1524
1525 /* MMC Bus interface */
1526 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1527 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1528 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1529
1525 }
1526 return (0);
1527}
1528
1529static int
1530mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1531{
1532 /*

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

1567 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1568 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1569
1570 /* MMC Bus interface */
1571 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1572 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1573 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1574
1530 {0, 0},
1575 DEVMETHOD_END
1531};
1532
1533static driver_t mmc_driver = {
1534 "mmc",
1535 mmc_methods,
1536 sizeof(struct mmc_softc),
1537};
1538static devclass_t mmc_devclass;
1539
1576};
1577
1578static driver_t mmc_driver = {
1579 "mmc",
1580 mmc_methods,
1581 sizeof(struct mmc_softc),
1582};
1583static devclass_t mmc_devclass;
1584
1540
1541DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL);
1542DRIVER_MODULE(mmc, sdhci, mmc_driver, mmc_devclass, NULL, NULL);
1585DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL);
1586DRIVER_MODULE(mmc, sdhci, mmc_driver, mmc_devclass, NULL, NULL);