Skip to content

Commit 5c4e6f1

Browse files
author
Pierre Ossman
committed
mmc: detect SDIO cards
Really basic init sequence for SDIO cards. Signed-off-by: Pierre Ossman <drzeus@drzeus.cx>
1 parent 1d4de9e commit 5c4e6f1

10 files changed

Lines changed: 304 additions & 14 deletions

File tree

‎MAINTAINERS‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2561,7 +2561,7 @@ L: linux-kernel@vger.kernel.org
25612561
W: http://www.atnf.csiro.au/~rgooch/linux/kernel-patches.html
25622562
S: Maintained
25632563

2564-
MULTIMEDIA CARD (MMC) AND SECURE DIGITAL (SD) SUBSYSTEM
2564+
MULTIMEDIA CARD (MMC), SECURE DIGITAL (SD) AND SDIO SUBSYSTEM
25652565
P: Pierre Ossman
25662566
M: drzeus-mmc@drzeus.cx
25672567
L: linux-kernel@vger.kernel.org

‎drivers/mmc/core/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ endif
88

99
obj-$(CONFIG_MMC) += mmc_core.o
1010
mmc_core-y := core.o sysfs.o bus.o host.o \
11-
mmc.o mmc_ops.o sd.o sd_ops.o
11+
mmc.o mmc_ops.o sd.o sd_ops.o \
12+
sdio.o sdio_ops.o
1213

‎drivers/mmc/core/bus.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ static ssize_t mmc_type_show(struct device *dev,
3434
return sprintf(buf, "MMC\n");
3535
case MMC_TYPE_SD:
3636
return sprintf(buf, "SD\n");
37+
case MMC_TYPE_SDIO:
38+
return sprintf(buf, "SDIO\n");
3739
default:
3840
return -EFAULT;
3941
}
@@ -76,6 +78,9 @@ mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
7678
case MMC_TYPE_SD:
7779
add_env("MMC_TYPE=%s", "SD");
7880
break;
81+
case MMC_TYPE_SDIO:
82+
add_env("MMC_TYPE=%s", "SDIO");
83+
break;
7984
}
8085

8186
add_env("MMC_NAME=%s", mmc_card_name(card));
@@ -221,6 +226,9 @@ int mmc_add_card(struct mmc_card *card)
221226
if (mmc_card_blockaddr(card))
222227
type = "SDHC";
223228
break;
229+
case MMC_TYPE_SDIO:
230+
type = "SDIO";
231+
break;
224232
default:
225233
type = "?";
226234
break;

‎drivers/mmc/core/core.c‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232

3333
#include "mmc_ops.h"
3434
#include "sd_ops.h"
35+
#include "sdio_ops.h"
3536

3637
extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
3738
extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
39+
extern int mmc_attach_sdio(struct mmc_host *host, u32 ocr);
3840

3941
static struct workqueue_struct *workqueue;
4042

@@ -595,24 +597,38 @@ void mmc_rescan(struct work_struct *work)
595597

596598
mmc_send_if_cond(host, host->ocr_avail);
597599

600+
/*
601+
* First we search for SDIO...
602+
*/
603+
err = mmc_send_io_op_cond(host, 0, &ocr);
604+
if (!err) {
605+
if (mmc_attach_sdio(host, ocr))
606+
mmc_power_off(host);
607+
return;
608+
}
609+
610+
/*
611+
* ...then normal SD...
612+
*/
598613
err = mmc_send_app_op_cond(host, 0, &ocr);
599614
if (!err) {
600615
if (mmc_attach_sd(host, ocr))
601616
mmc_power_off(host);
602-
} else {
603-
/*
604-
* If we fail to detect any SD cards then try
605-
* searching for MMC cards.
606-
*/
607-
err = mmc_send_op_cond(host, 0, &ocr);
608-
if (!err) {
609-
if (mmc_attach_mmc(host, ocr))
610-
mmc_power_off(host);
611-
} else {
617+
return;
618+
}
619+
620+
/*
621+
* ...and finally MMC.
622+
*/
623+
err = mmc_send_op_cond(host, 0, &ocr);
624+
if (!err) {
625+
if (mmc_attach_mmc(host, ocr))
612626
mmc_power_off(host);
613-
mmc_release_host(host);
614-
}
627+
return;
615628
}
629+
630+
mmc_release_host(host);
631+
mmc_power_off(host);
616632
} else {
617633
if (host->bus_ops->detect && !host->bus_dead)
618634
host->bus_ops->detect(host);

‎drivers/mmc/core/sdio.c‎

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* linux/drivers/mmc/sdio.c
3+
*
4+
* Copyright 2006-2007 Pierre Ossman
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or (at
9+
* your option) any later version.
10+
*/
11+
12+
#include <linux/err.h>
13+
14+
#include <linux/mmc/host.h>
15+
#include <linux/mmc/card.h>
16+
17+
#include "core.h"
18+
#include "bus.h"
19+
#include "mmc_ops.h"
20+
#include "sd_ops.h"
21+
#include "sdio_ops.h"
22+
23+
/*
24+
* Host is being removed. Free up the current card.
25+
*/
26+
static void mmc_sdio_remove(struct mmc_host *host)
27+
{
28+
BUG_ON(!host);
29+
BUG_ON(!host->card);
30+
31+
mmc_remove_card(host->card);
32+
host->card = NULL;
33+
}
34+
35+
/*
36+
* Card detection callback from host.
37+
*/
38+
static void mmc_sdio_detect(struct mmc_host *host)
39+
{
40+
int err;
41+
42+
BUG_ON(!host);
43+
BUG_ON(!host->card);
44+
45+
mmc_claim_host(host);
46+
47+
/*
48+
* Just check if our card has been removed.
49+
*/
50+
err = mmc_select_card(host->card);
51+
52+
mmc_release_host(host);
53+
54+
if (err) {
55+
mmc_sdio_remove(host);
56+
57+
mmc_claim_host(host);
58+
mmc_detach_bus(host);
59+
mmc_release_host(host);
60+
}
61+
}
62+
63+
64+
static const struct mmc_bus_ops mmc_sdio_ops = {
65+
.remove = mmc_sdio_remove,
66+
.detect = mmc_sdio_detect,
67+
};
68+
69+
70+
/*
71+
* Starting point for SDIO card init.
72+
*/
73+
int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
74+
{
75+
int err;
76+
int funcs;
77+
struct mmc_card *card;
78+
79+
BUG_ON(!host);
80+
BUG_ON(!host->claimed);
81+
82+
mmc_attach_bus(host, &mmc_sdio_ops);
83+
84+
/*
85+
* Sanity check the voltages that the card claims to
86+
* support.
87+
*/
88+
if (ocr & 0x7F) {
89+
printk(KERN_WARNING "%s: card claims to support voltages "
90+
"below the defined range. These will be ignored.\n",
91+
mmc_hostname(host));
92+
ocr &= ~0x7F;
93+
}
94+
95+
if (ocr & MMC_VDD_165_195) {
96+
printk(KERN_WARNING "%s: SDIO card claims to support the "
97+
"incompletely defined 'low voltage range'. This "
98+
"will be ignored.\n", mmc_hostname(host));
99+
ocr &= ~MMC_VDD_165_195;
100+
}
101+
102+
host->ocr = mmc_select_voltage(host, ocr);
103+
104+
/*
105+
* Can we support the voltage(s) of the card(s)?
106+
*/
107+
if (!host->ocr) {
108+
err = -EINVAL;
109+
goto err;
110+
}
111+
112+
/*
113+
* Inform the card of the voltage
114+
*/
115+
err = mmc_send_io_op_cond(host, host->ocr, &ocr);
116+
if (err)
117+
goto err;
118+
119+
/*
120+
* The number of functions on the card is encoded inside
121+
* the ocr.
122+
*/
123+
funcs = (ocr & 0x70000000) >> 28;
124+
125+
/*
126+
* Allocate card structure.
127+
*/
128+
card = mmc_alloc_card(host);
129+
if (IS_ERR(card)) {
130+
err = PTR_ERR(card);
131+
goto err;
132+
}
133+
134+
card->type = MMC_TYPE_SDIO;
135+
136+
/*
137+
* Set card RCA.
138+
*/
139+
err = mmc_send_relative_addr(host, &card->rca);
140+
if (err)
141+
goto free_card;
142+
143+
mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
144+
145+
/*
146+
* Select card, as all following commands rely on that.
147+
*/
148+
err = mmc_select_card(card);
149+
if (err)
150+
goto free_card;
151+
152+
host->card = card;
153+
154+
mmc_release_host(host);
155+
156+
err = mmc_add_card(host->card);
157+
if (err)
158+
goto reclaim_host;
159+
160+
return 0;
161+
162+
reclaim_host:
163+
mmc_claim_host(host);
164+
free_card:
165+
mmc_remove_card(card);
166+
host->card = NULL;
167+
err:
168+
mmc_detach_bus(host);
169+
mmc_release_host(host);
170+
171+
printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
172+
mmc_hostname(host), err);
173+
174+
return err;
175+
}
176+

‎drivers/mmc/core/sdio_ops.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* linux/drivers/mmc/sdio_ops.c
3+
*
4+
* Copyright 2006-2007 Pierre Ossman
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or (at
9+
* your option) any later version.
10+
*/
11+
12+
#include <linux/mmc/host.h>
13+
#include <linux/mmc/mmc.h>
14+
#include <linux/mmc/sdio.h>
15+
16+
#include "core.h"
17+
18+
int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
19+
{
20+
struct mmc_command cmd;
21+
int i, err = 0;
22+
23+
BUG_ON(!host);
24+
25+
memset(&cmd, 0, sizeof(struct mmc_command));
26+
27+
cmd.opcode = SD_IO_SEND_OP_COND;
28+
cmd.arg = ocr;
29+
cmd.flags = MMC_RSP_R4 | MMC_CMD_BCR;
30+
31+
for (i = 100; i; i--) {
32+
err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
33+
if (err)
34+
break;
35+
36+
if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
37+
break;
38+
39+
err = -ETIMEDOUT;
40+
41+
mmc_delay(10);
42+
}
43+
44+
if (rocr)
45+
*rocr = cmd.resp[0];
46+
47+
return err;
48+
}
49+

‎drivers/mmc/core/sdio_ops.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* linux/drivers/mmc/sdio_ops.c
3+
*
4+
* Copyright 2006-2007 Pierre Ossman
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or (at
9+
* your option) any later version.
10+
*/
11+
12+
#ifndef _MMC_SDIO_OPS_H
13+
#define _MMC_SDIO_OPS_H
14+
15+
int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);
16+
17+
#endif
18+

‎include/linux/mmc/card.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct mmc_card {
6767
unsigned int type; /* card type */
6868
#define MMC_TYPE_MMC 0 /* MMC card */
6969
#define MMC_TYPE_SD 1 /* SD card */
70+
#define MMC_TYPE_SDIO 2 /* SDIO card */
7071
unsigned int state; /* (our) card state */
7172
#define MMC_STATE_PRESENT (1<<0) /* present in sysfs */
7273
#define MMC_STATE_READONLY (1<<1) /* card is read-only */
@@ -84,6 +85,7 @@ struct mmc_card {
8485

8586
#define mmc_card_mmc(c) ((c)->type == MMC_TYPE_MMC)
8687
#define mmc_card_sd(c) ((c)->type == MMC_TYPE_SD)
88+
#define mmc_card_sdio(c) ((c)->type == MMC_TYPE_SDIO)
8789

8890
#define mmc_card_present(c) ((c)->state & MMC_STATE_PRESENT)
8991
#define mmc_card_readonly(c) ((c)->state & MMC_STATE_READONLY)

‎include/linux/mmc/core.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct mmc_command {
4141
#define MMC_RSP_R1B (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY)
4242
#define MMC_RSP_R2 (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
4343
#define MMC_RSP_R3 (MMC_RSP_PRESENT)
44+
#define MMC_RSP_R4 (MMC_RSP_PRESENT)
4445
#define MMC_RSP_R6 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
4546
#define MMC_RSP_R7 (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
4647

‎include/linux/mmc/sdio.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* include/linux/mmc/sdio.h
3+
*
4+
* Copyright 2006-2007 Pierre Ossman
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or (at
9+
* your option) any later version.
10+
*/
11+
12+
#ifndef MMC_SDIO_H
13+
#define MMC_SDIO_H
14+
15+
/* SDIO commands type argument response */
16+
#define SD_IO_SEND_OP_COND 5 /* bcr [23:0] OCR R4 */
17+
18+
#endif
19+

0 commit comments

Comments
 (0)