mmc.c (fa5f41d31c4658ad48a5abf30d251f13de87e51b) mmc.c (67d752c3d4390824bf963b63494a835f977f7722)
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

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

110#define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
111#define MMC_LOCK_INIT(_sc) \
112 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
113 "mmc", MTX_DEF)
114#define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
115#define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
116#define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
117
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

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

110#define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
111#define MMC_LOCK_INIT(_sc) \
112 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
113 "mmc", MTX_DEF)
114#define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
115#define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
116#define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
117
118static int mmc_calculate_clock(struct mmc_softc *sc);
118static void mmc_delayed_attach(void *);
119static void mmc_power_down(struct mmc_softc *sc);
120static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
121 int retries);
122static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
123 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
124static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
119static void mmc_delayed_attach(void *);
120static void mmc_power_down(struct mmc_softc *sc);
121static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
122 int retries);
123static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
124 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
125static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
125static int mmc_set_bus_width(struct mmc_softc *sc, uint16_t rca, int width);
126static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width);
126static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr);
127static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
128static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
129
130static void
131mmc_ms_delay(int ms)
132{
133 DELAY(1000 * ms); /* XXX BAD */

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

210 * bus, and constantly reselecting causes problems.
211 */
212 rca = mmc_get_rca(dev);
213 if (sc->last_rca != rca) {
214 mmc_select_card(sc, rca);
215 sc->last_rca = rca;
216 /* Prepare bus width for the new card. */
217 ivar = device_get_ivars(dev);
127static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr);
128static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
129static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
130
131static void
132mmc_ms_delay(int ms)
133{
134 DELAY(1000 * ms); /* XXX BAD */

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

211 * bus, and constantly reselecting causes problems.
212 */
213 rca = mmc_get_rca(dev);
214 if (sc->last_rca != rca) {
215 mmc_select_card(sc, rca);
216 sc->last_rca = rca;
217 /* Prepare bus width for the new card. */
218 ivar = device_get_ivars(dev);
218 device_printf(busdev,
219 "setting bus width to %d bits\n",
220 (ivar->bus_width == bus_width_4)?4:
221 (ivar->bus_width == bus_width_8)?8:1);
222 mmc_set_bus_width(sc, rca, ivar->bus_width);
219 if (bootverbose) {
220 device_printf(busdev,
221 "setting bus width to %d bits\n",
222 (ivar->bus_width == bus_width_4)?4:
223 (ivar->bus_width == bus_width_8)?8:1);
224 }
225 mmc_set_card_bus_width(sc, rca, ivar->bus_width);
223 mmcbr_set_bus_width(busdev, ivar->bus_width);
224 mmcbr_update_ios(busdev);
225 }
226 } else {
227 /*
228 * If there's a card selected, stand down.
229 */
230 if (sc->last_rca != 0) {

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

566 data.len = 64;
567 data.flags = MMC_DATA_READ;
568
569 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
570 return (err);
571}
572
573static int
226 mmcbr_set_bus_width(busdev, ivar->bus_width);
227 mmcbr_update_ios(busdev);
228 }
229 } else {
230 /*
231 * If there's a card selected, stand down.
232 */
233 if (sc->last_rca != 0) {

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

569 data.len = 64;
570 data.flags = MMC_DATA_READ;
571
572 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
573 return (err);
574}
575
576static int
574mmc_set_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
577mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
575{
576 int err;
577
578 if (mmcbr_get_mode(sc->dev) == mode_sd) {
579 struct mmc_command cmd;
580
581 memset(&cmd, 0, sizeof(struct mmc_command));
582 cmd.opcode = ACMD_SET_BUS_WIDTH;

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

1162 mmc_send_app_op_cond(sc,
1163 (err?0:MMC_OCR_CCS)|mmcbr_get_ocr(dev), NULL);
1164 } else
1165 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
1166 mmc_discover_cards(sc);
1167
1168 mmcbr_set_bus_mode(dev, pushpull);
1169 mmcbr_update_ios(dev);
578{
579 int err;
580
581 if (mmcbr_get_mode(sc->dev) == mode_sd) {
582 struct mmc_command cmd;
583
584 memset(&cmd, 0, sizeof(struct mmc_command));
585 cmd.opcode = ACMD_SET_BUS_WIDTH;

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

1165 mmc_send_app_op_cond(sc,
1166 (err?0:MMC_OCR_CCS)|mmcbr_get_ocr(dev), NULL);
1167 } else
1168 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
1169 mmc_discover_cards(sc);
1170
1171 mmcbr_set_bus_mode(dev, pushpull);
1172 mmcbr_update_ios(dev);
1173 mmc_calculate_clock(sc);
1170 bus_generic_attach(dev);
1171/* mmc_update_children_sysctl(dev);*/
1172}
1173
1174static int
1175mmc_calculate_clock(struct mmc_softc *sc)
1176{
1177 int max_dtr, max_hs_dtr, max_timing;

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

1203 continue;
1204 mmc_select_card(sc, ivar->rca);
1205 mmc_set_timing(sc, max_timing);
1206 }
1207 mmc_select_card(sc, 0);
1208 free(kids, M_TEMP);
1209 if (max_timing == bus_timing_hs)
1210 max_dtr = max_hs_dtr;
1174 bus_generic_attach(dev);
1175/* mmc_update_children_sysctl(dev);*/
1176}
1177
1178static int
1179mmc_calculate_clock(struct mmc_softc *sc)
1180{
1181 int max_dtr, max_hs_dtr, max_timing;

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

1207 continue;
1208 mmc_select_card(sc, ivar->rca);
1209 mmc_set_timing(sc, max_timing);
1210 }
1211 mmc_select_card(sc, 0);
1212 free(kids, M_TEMP);
1213 if (max_timing == bus_timing_hs)
1214 max_dtr = max_hs_dtr;
1211 device_printf(sc->dev, "setting transfer rate to %d.%03dMHz%s\n",
1212 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1213 (max_timing == bus_timing_hs)?" with high speed timing":"");
1215 if (bootverbose) {
1216 device_printf(sc->dev, "setting transfer rate to %d.%03dMHz%s\n",
1217 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1218 (max_timing == bus_timing_hs)?" with high speed timing":"");
1219 }
1214 mmcbr_set_timing(sc->dev, max_timing);
1215 mmcbr_set_clock(sc->dev, max_dtr);
1216 mmcbr_update_ios(sc->dev);
1217 return max_dtr;
1218}
1219
1220static void
1221mmc_scan(struct mmc_softc *sc)
1222{
1223 device_t dev;
1224
1225 dev = sc->dev;
1226 mmc_acquire_bus(dev, dev);
1227
1228 if (mmcbr_get_power_mode(dev) == power_on)
1229 mmc_rescan_cards(sc);
1230 mmc_go_discovery(sc);
1220 mmcbr_set_timing(sc->dev, max_timing);
1221 mmcbr_set_clock(sc->dev, max_dtr);
1222 mmcbr_update_ios(sc->dev);
1223 return max_dtr;
1224}
1225
1226static void
1227mmc_scan(struct mmc_softc *sc)
1228{
1229 device_t dev;
1230
1231 dev = sc->dev;
1232 mmc_acquire_bus(dev, dev);
1233
1234 if (mmcbr_get_power_mode(dev) == power_on)
1235 mmc_rescan_cards(sc);
1236 mmc_go_discovery(sc);
1231 mmc_calculate_clock(sc);
1232
1233 mmc_release_bus(dev, dev);
1234 /* XXX probe/attach/detach children? */
1235}
1236
1237static int
1238mmc_read_ivar(device_t bus, device_t child, int which, u_char *result)
1239{

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

1250 break;
1251 case MMC_IVAR_RCA:
1252 *(int *)result = ivar->rca;
1253 break;
1254 case MMC_IVAR_SECTOR_SIZE:
1255 *(int *)result = MMC_SECTOR_SIZE;
1256 break;
1257 case MMC_IVAR_TRAN_SPEED:
1237
1238 mmc_release_bus(dev, dev);
1239 /* XXX probe/attach/detach children? */
1240}
1241
1242static int
1243mmc_read_ivar(device_t bus, device_t child, int which, u_char *result)
1244{

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

1255 break;
1256 case MMC_IVAR_RCA:
1257 *(int *)result = ivar->rca;
1258 break;
1259 case MMC_IVAR_SECTOR_SIZE:
1260 *(int *)result = MMC_SECTOR_SIZE;
1261 break;
1262 case MMC_IVAR_TRAN_SPEED:
1258 *(int *)result = ivar->csd.tran_speed;
1263 *(int *)result = mmcbr_get_clock(bus);
1259 break;
1260 case MMC_IVAR_READ_ONLY:
1261 *(int *)result = ivar->read_only;
1262 break;
1263 case MMC_IVAR_HIGH_CAP:
1264 *(int *)result = ivar->high_cap;
1265 break;
1264 break;
1265 case MMC_IVAR_READ_ONLY:
1266 *(int *)result = ivar->read_only;
1267 break;
1268 case MMC_IVAR_HIGH_CAP:
1269 *(int *)result = ivar->high_cap;
1270 break;
1271 case MMC_IVAR_CARD_TYPE:
1272 *(int *)result = ivar->mode;
1273 break;
1274 case MMC_IVAR_BUS_WIDTH:
1275 *(int *)result = ivar->bus_width;
1276 break;
1266 }
1267 return (0);
1268}
1269
1270static int
1271mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1272{
1273 /*

--- 43 unchanged lines hidden ---
1277 }
1278 return (0);
1279}
1280
1281static int
1282mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1283{
1284 /*

--- 43 unchanged lines hidden ---