Deleted Added
full compact
mmc.c (183452) mmc.c (183453)
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 183452 2008-09-29 01:28:30Z imp $");
54__FBSDID("$FreeBSD: head/sys/dev/mmc/mmc.c 183453 2008-09-29 01:32:21Z 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>

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

182 sc = device_get_softc(busdev);
183 MMC_LOCK(sc);
184 if (sc->owner)
185 panic("mmc: host bridge didn't seralize us.");
186 sc->owner = dev;
187 MMC_UNLOCK(sc);
188
189 if (busdev != dev) {
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>

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

182 sc = device_get_softc(busdev);
183 MMC_LOCK(sc);
184 if (sc->owner)
185 panic("mmc: host bridge didn't seralize us.");
186 sc->owner = dev;
187 MMC_UNLOCK(sc);
188
189 if (busdev != dev) {
190 // Keep track of the last rca that we've selected. If
191 // we're asked to do it again, don't. We never unselect
192 // unless the bus code itself wants the mmc bus.
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 */
193 rca = mmc_get_rca(dev);
194 if (sc->last_rca != rca) {
195 mmc_wait_for_command(sc, MMC_SELECT_CARD, rca << 16,
196 MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES);
197 sc->last_rca = rca;
198 }
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 }
199 // XXX should set bus width here?
202 /* XXX should set bus width here? */
200 } else {
203 } else {
201 // If there's a card selected, stand down.
204 /*
205 * If there's a card selected, stand down.
206 */
202 if (sc->last_rca != 0) {
203 mmc_wait_for_command(sc, MMC_SELECT_CARD, 0,
204 MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES);
205 sc->last_rca = 0;
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 }
207 // XXX should set bus width here?
212 /* XXX should set bus width here? */
208 }
209
210 return (0);
211}
212
213static int
214mmc_release_bus(device_t busdev, device_t dev)
215{

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

257 return (-1);
258}
259
260static void
261mmc_wakeup(struct mmc_request *req)
262{
263 struct mmc_softc *sc;
264
213 }
214
215 return (0);
216}
217
218static int
219mmc_release_bus(device_t busdev, device_t dev)
220{

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

262 return (-1);
263}
264
265static void
266mmc_wakeup(struct mmc_request *req)
267{
268 struct mmc_softc *sc;
269
265// printf("Wakeup for req %p done_data %p\n", req, req->done_data);
270/* printf("Wakeup for req %p done_data %p\n", req, req->done_data); */
266 sc = (struct mmc_softc *)req->done_data;
267 MMC_LOCK(sc);
268 req->flags |= MMC_REQ_DONE;
269 wakeup(req);
270 MMC_UNLOCK(sc);
271}
272
273static int
274mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
275{
276 int err;
277
278 req->done = mmc_wakeup;
279 req->done_data = sc;
271 sc = (struct mmc_softc *)req->done_data;
272 MMC_LOCK(sc);
273 req->flags |= MMC_REQ_DONE;
274 wakeup(req);
275 MMC_UNLOCK(sc);
276}
277
278static int
279mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
280{
281 int err;
282
283 req->done = mmc_wakeup;
284 req->done_data = sc;
280// printf("Submitting request %p sc %p\n", req, sc);
285/* printf("Submitting request %p sc %p\n", req, sc); */
281 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
282 MMC_LOCK(sc);
283 do {
284 err = msleep(req, &sc->sc_mtx, PZERO | PCATCH, "mmcreq",
285 hz / 10);
286 } while (!(req->flags & MMC_REQ_DONE) && err == EAGAIN);
286 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
287 MMC_LOCK(sc);
288 do {
289 err = msleep(req, &sc->sc_mtx, PZERO | PCATCH, "mmcreq",
290 hz / 10);
291 } while (!(req->flags & MMC_REQ_DONE) && err == EAGAIN);
287// printf("Request %p done with error %d\n", req, err);
292/* printf("Request %p done with error %d\n", req, err); */
288 MMC_UNLOCK(sc);
289 return (err);
290}
291
292static int
293mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
294{
295 struct mmc_softc *sc = device_get_softc(brdev);

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

302{
303 struct mmc_request mreq;
304
305 memset(&mreq, 0, sizeof(mreq));
306 memset(cmd->resp, 0, sizeof(cmd->resp));
307 cmd->retries = retries;
308 cmd->data = NULL;
309 mreq.cmd = cmd;
293 MMC_UNLOCK(sc);
294 return (err);
295}
296
297static int
298mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
299{
300 struct mmc_softc *sc = device_get_softc(brdev);

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

307{
308 struct mmc_request mreq;
309
310 memset(&mreq, 0, sizeof(mreq));
311 memset(cmd->resp, 0, sizeof(cmd->resp));
312 cmd->retries = retries;
313 cmd->data = NULL;
314 mreq.cmd = cmd;
310// printf("CMD: %x ARG %x\n", cmd->opcode, cmd->arg);
315/* printf("CMD: %x ARG %x\n", cmd->opcode, cmd->arg); */
311 mmc_wait_for_req(sc, &mreq);
312 return (cmd->error);
313}
314
315static int
316mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
317 struct mmc_command *cmd, int retries)
318{

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

492 cid->oid = mmc_get_bits(raw_cid, 104, 16);
493 for (i = 0; i < 5; i++)
494 cid->pnm[i] = mmc_get_bits(raw_cid, 96 - i * 8, 8);
495 cid->prv = mmc_get_bits(raw_cid, 56, 8);
496 cid->psn = mmc_get_bits(raw_cid, 24, 32);
497 cid->mdt_year = mmc_get_bits(raw_cid, 12, 8) + 2001;
498 cid->mdt_month = mmc_get_bits(raw_cid, 8, 4);
499 } else {
316 mmc_wait_for_req(sc, &mreq);
317 return (cmd->error);
318}
319
320static int
321mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
322 struct mmc_command *cmd, int retries)
323{

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

497 cid->oid = mmc_get_bits(raw_cid, 104, 16);
498 for (i = 0; i < 5; i++)
499 cid->pnm[i] = mmc_get_bits(raw_cid, 96 - i * 8, 8);
500 cid->prv = mmc_get_bits(raw_cid, 56, 8);
501 cid->psn = mmc_get_bits(raw_cid, 24, 32);
502 cid->mdt_year = mmc_get_bits(raw_cid, 12, 8) + 2001;
503 cid->mdt_month = mmc_get_bits(raw_cid, 8, 4);
504 } else {
500 // XXX write me
505 /* XXX write me */
501 panic("write mmc cid decoder");
502 }
503}
504
505static const int exp[8] = {
506 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
507};
508static const int mant[16] = {

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

646static void
647mmc_go_discovery(struct mmc_softc *sc)
648{
649 uint32_t ocr;
650 device_t dev;
651
652 dev = sc->dev;
653 if (mmcbr_get_power_mode(dev) != power_on) {
506 panic("write mmc cid decoder");
507 }
508}
509
510static const int exp[8] = {
511 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
512};
513static const int mant[16] = {

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

651static void
652mmc_go_discovery(struct mmc_softc *sc)
653{
654 uint32_t ocr;
655 device_t dev;
656
657 dev = sc->dev;
658 if (mmcbr_get_power_mode(dev) != power_on) {
654 // First, try SD modes
659 /*
660 * First, try SD modes
661 */
655 mmcbr_set_mode(dev, mode_sd);
656 mmc_power_up(sc);
657 mmcbr_set_bus_mode(dev, pushpull);
658 mmc_idle_cards(sc);
659 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
662 mmcbr_set_mode(dev, mode_sd);
663 mmc_power_up(sc);
664 mmcbr_set_bus_mode(dev, pushpull);
665 mmc_idle_cards(sc);
666 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
660 // Failed, try MMC
667 /*
668 * Failed, try MMC
669 */
661 mmcbr_set_mode(dev, mode_mmc);
662 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE)
670 mmcbr_set_mode(dev, mode_mmc);
671 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE)
663 return; // Failed both, punt! XXX power down?
672 return; /* Failed both, punt! XXX powerdown? */
664 }
665 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
666 if (mmcbr_get_ocr(dev) != 0)
667 mmc_idle_cards(sc);
668 } else {
669 mmcbr_set_bus_mode(dev, opendrain);
670 mmcbr_set_clock(dev, mmcbr_get_f_min(dev));
671 mmcbr_update_ios(dev);
673 }
674 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
675 if (mmcbr_get_ocr(dev) != 0)
676 mmc_idle_cards(sc);
677 } else {
678 mmcbr_set_bus_mode(dev, opendrain);
679 mmcbr_set_clock(dev, mmcbr_get_f_min(dev));
680 mmcbr_update_ios(dev);
672 // XXX recompute vdd based on new cards?
681 /* XXX recompute vdd based on new cards? */
673 }
674 /*
675 * Make sure that we have a mutually agreeable voltage to at least
676 * one card on the bus.
677 */
678 if (mmcbr_get_ocr(dev) == 0)
679 return;
680 /*
681 * Reselect the cards after we've idled them above.
682 */
683 if (mmcbr_get_mode(dev) == mode_sd)
684 mmc_send_app_op_cond(sc, mmcbr_get_ocr(dev), NULL);
685 else
686 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
687 mmc_discover_cards(sc);
688
689 mmcbr_set_bus_mode(dev, pushpull);
690 mmcbr_update_ios(dev);
691 bus_generic_attach(dev);
682 }
683 /*
684 * Make sure that we have a mutually agreeable voltage to at least
685 * one card on the bus.
686 */
687 if (mmcbr_get_ocr(dev) == 0)
688 return;
689 /*
690 * Reselect the cards after we've idled them above.
691 */
692 if (mmcbr_get_mode(dev) == mode_sd)
693 mmc_send_app_op_cond(sc, mmcbr_get_ocr(dev), NULL);
694 else
695 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
696 mmc_discover_cards(sc);
697
698 mmcbr_set_bus_mode(dev, pushpull);
699 mmcbr_update_ios(dev);
700 bus_generic_attach(dev);
692// mmc_update_children_sysctl(dev);
701/* mmc_update_children_sysctl(dev);*/
693}
694
695static int
696mmc_calculate_clock(struct mmc_softc *sc)
697{
698 int max_dtr = 0;
699 int nkid, i, f_min, f_max;
700 device_t *kids;

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

723
724 if (mmcbr_get_power_mode(dev) == power_on)
725 mmc_rescan_cards(sc);
726 mmc_go_discovery(sc);
727 mmcbr_set_clock(dev, mmc_calculate_clock(sc));
728 mmcbr_update_ios(dev);
729
730 mmc_release_bus(dev, dev);
702}
703
704static int
705mmc_calculate_clock(struct mmc_softc *sc)
706{
707 int max_dtr = 0;
708 int nkid, i, f_min, f_max;
709 device_t *kids;

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

732
733 if (mmcbr_get_power_mode(dev) == power_on)
734 mmc_rescan_cards(sc);
735 mmc_go_discovery(sc);
736 mmcbr_set_clock(dev, mmc_calculate_clock(sc));
737 mmcbr_update_ios(dev);
738
739 mmc_release_bus(dev, dev);
731 // XXX probe/attach/detach children?
740 /* XXX probe/attach/detach children? */
732}
733
734static int
735mmc_read_ivar(device_t bus, device_t child, int which, u_char *result)
736{
737 struct mmc_ivars *ivar = device_get_ivars(child);
738
739 switch (which) {

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

759 break;
760 }
761 return (0);
762}
763
764static int
765mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
766{
741}
742
743static int
744mmc_read_ivar(device_t bus, device_t child, int which, u_char *result)
745{
746 struct mmc_ivars *ivar = device_get_ivars(child);
747
748 switch (which) {

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

768 break;
769 }
770 return (0);
771}
772
773static int
774mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
775{
767 // None are writable ATM
768 switch (which) {
769 default:
770 return (EINVAL);
771 }
772 return (0);
776 /*
777 * None are writable ATM
778 */
779 return (EINVAL);
773}
774
775
776static void
777mmc_delayed_attach(void *xsc)
778{
779 struct mmc_softc *sc = xsc;
780

--- 32 unchanged lines hidden ---
780}
781
782
783static void
784mmc_delayed_attach(void *xsc)
785{
786 struct mmc_softc *sc = xsc;
787

--- 32 unchanged lines hidden ---