aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mmc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mmc')
-rw-r--r--drivers/mmc/card/block.c87
-rw-r--r--drivers/mmc/card/mmc_test.c811
-rw-r--r--drivers/mmc/card/queue.c21
-rw-r--r--drivers/mmc/core/bus.c9
-rw-r--r--drivers/mmc/core/core.c441
-rw-r--r--drivers/mmc/core/core.h2
-rw-r--r--drivers/mmc/core/host.c4
-rw-r--r--drivers/mmc/core/mmc.c75
-rw-r--r--drivers/mmc/core/sd.c331
-rw-r--r--drivers/mmc/core/sd.h17
-rw-r--r--drivers/mmc/core/sd_ops.c48
-rw-r--r--drivers/mmc/core/sd_ops.h1
-rw-r--r--drivers/mmc/core/sdio.c210
-rw-r--r--drivers/mmc/host/Kconfig18
-rw-r--r--drivers/mmc/host/Makefile6
-rw-r--r--drivers/mmc/host/jz4740_mmc.c1029
-rw-r--r--drivers/mmc/host/mmc_spi.c9
-rw-r--r--drivers/mmc/host/mmci.c148
-rw-r--r--drivers/mmc/host/mmci.h39
-rw-r--r--drivers/mmc/host/msm_sdcc.c25
-rw-r--r--drivers/mmc/host/msm_sdcc.h4
-rw-r--r--drivers/mmc/host/mxcmmc.c48
-rw-r--r--drivers/mmc/host/omap_hsmmc.c47
-rw-r--r--drivers/mmc/host/sdhci-cns3xxx.c97
-rw-r--r--drivers/mmc/host/sdhci-of-core.c12
-rw-r--r--drivers/mmc/host/sdhci-pci.c49
-rw-r--r--drivers/mmc/host/sdhci-pltfm.c17
-rw-r--r--drivers/mmc/host/sdhci-pltfm.h18
-rw-r--r--drivers/mmc/host/sdhci-s3c.c123
-rw-r--r--drivers/mmc/host/sdhci.c53
-rw-r--r--drivers/mmc/host/sdhci.h10
-rw-r--r--drivers/mmc/host/sdricoh_cs.c1
32 files changed, 3452 insertions, 358 deletions
diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index cb9fbc83b090..d545f79f6000 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -29,6 +29,7 @@
29#include <linux/kdev_t.h> 29#include <linux/kdev_t.h>
30#include <linux/blkdev.h> 30#include <linux/blkdev.h>
31#include <linux/mutex.h> 31#include <linux/mutex.h>
32#include <linux/smp_lock.h>
32#include <linux/scatterlist.h> 33#include <linux/scatterlist.h>
33#include <linux/string_helpers.h> 34#include <linux/string_helpers.h>
34 35
@@ -107,6 +108,7 @@ static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
107 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); 108 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
108 int ret = -ENXIO; 109 int ret = -ENXIO;
109 110
111 lock_kernel();
110 if (md) { 112 if (md) {
111 if (md->usage == 2) 113 if (md->usage == 2)
112 check_disk_change(bdev); 114 check_disk_change(bdev);
@@ -117,6 +119,7 @@ static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
117 ret = -EROFS; 119 ret = -EROFS;
118 } 120 }
119 } 121 }
122 unlock_kernel();
120 123
121 return ret; 124 return ret;
122} 125}
@@ -125,7 +128,9 @@ static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
125{ 128{
126 struct mmc_blk_data *md = disk->private_data; 129 struct mmc_blk_data *md = disk->private_data;
127 130
131 lock_kernel();
128 mmc_blk_put(md); 132 mmc_blk_put(md);
133 unlock_kernel();
129 return 0; 134 return 0;
130} 135}
131 136
@@ -242,7 +247,76 @@ static u32 get_card_status(struct mmc_card *card, struct request *req)
242 return cmd.resp[0]; 247 return cmd.resp[0];
243} 248}
244 249
245static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) 250static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
251{
252 struct mmc_blk_data *md = mq->data;
253 struct mmc_card *card = md->queue.card;
254 unsigned int from, nr, arg;
255 int err = 0;
256
257 mmc_claim_host(card->host);
258
259 if (!mmc_can_erase(card)) {
260 err = -EOPNOTSUPP;
261 goto out;
262 }
263
264 from = blk_rq_pos(req);
265 nr = blk_rq_sectors(req);
266
267 if (mmc_can_trim(card))
268 arg = MMC_TRIM_ARG;
269 else
270 arg = MMC_ERASE_ARG;
271
272 err = mmc_erase(card, from, nr, arg);
273out:
274 spin_lock_irq(&md->lock);
275 __blk_end_request(req, err, blk_rq_bytes(req));
276 spin_unlock_irq(&md->lock);
277
278 mmc_release_host(card->host);
279
280 return err ? 0 : 1;
281}
282
283static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
284 struct request *req)
285{
286 struct mmc_blk_data *md = mq->data;
287 struct mmc_card *card = md->queue.card;
288 unsigned int from, nr, arg;
289 int err = 0;
290
291 mmc_claim_host(card->host);
292
293 if (!mmc_can_secure_erase_trim(card)) {
294 err = -EOPNOTSUPP;
295 goto out;
296 }
297
298 from = blk_rq_pos(req);
299 nr = blk_rq_sectors(req);
300
301 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
302 arg = MMC_SECURE_TRIM1_ARG;
303 else
304 arg = MMC_SECURE_ERASE_ARG;
305
306 err = mmc_erase(card, from, nr, arg);
307 if (!err && arg == MMC_SECURE_TRIM1_ARG)
308 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
309out:
310 spin_lock_irq(&md->lock);
311 __blk_end_request(req, err, blk_rq_bytes(req));
312 spin_unlock_irq(&md->lock);
313
314 mmc_release_host(card->host);
315
316 return err ? 0 : 1;
317}
318
319static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
246{ 320{
247 struct mmc_blk_data *md = mq->data; 321 struct mmc_blk_data *md = mq->data;
248 struct mmc_card *card = md->queue.card; 322 struct mmc_card *card = md->queue.card;
@@ -470,6 +544,17 @@ static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
470 return 0; 544 return 0;
471} 545}
472 546
547static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
548{
549 if (req->cmd_flags & REQ_DISCARD) {
550 if (req->cmd_flags & REQ_SECURE)
551 return mmc_blk_issue_secdiscard_rq(mq, req);
552 else
553 return mmc_blk_issue_discard_rq(mq, req);
554 } else {
555 return mmc_blk_issue_rw_rq(mq, req);
556 }
557}
473 558
474static inline int mmc_blk_readonly(struct mmc_card *card) 559static inline int mmc_blk_readonly(struct mmc_card *card)
475{ 560{
diff --git a/drivers/mmc/card/mmc_test.c b/drivers/mmc/card/mmc_test.c
index 445d7db2277e..5dd8576b5c18 100644
--- a/drivers/mmc/card/mmc_test.c
+++ b/drivers/mmc/card/mmc_test.c
@@ -16,6 +16,7 @@
16#include <linux/slab.h> 16#include <linux/slab.h>
17 17
18#include <linux/scatterlist.h> 18#include <linux/scatterlist.h>
19#include <linux/swap.h> /* For nr_free_buffer_pages() */
19 20
20#define RESULT_OK 0 21#define RESULT_OK 0
21#define RESULT_FAIL 1 22#define RESULT_FAIL 1
@@ -25,6 +26,60 @@
25#define BUFFER_ORDER 2 26#define BUFFER_ORDER 2
26#define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER) 27#define BUFFER_SIZE (PAGE_SIZE << BUFFER_ORDER)
27 28
29/*
30 * Limit the test area size to the maximum MMC HC erase group size. Note that
31 * the maximum SD allocation unit size is just 4MiB.
32 */
33#define TEST_AREA_MAX_SIZE (128 * 1024 * 1024)
34
35/**
36 * struct mmc_test_pages - pages allocated by 'alloc_pages()'.
37 * @page: first page in the allocation
38 * @order: order of the number of pages allocated
39 */
40struct mmc_test_pages {
41 struct page *page;
42 unsigned int order;
43};
44
45/**
46 * struct mmc_test_mem - allocated memory.
47 * @arr: array of allocations
48 * @cnt: number of allocations
49 */
50struct mmc_test_mem {
51 struct mmc_test_pages *arr;
52 unsigned int cnt;
53};
54
55/**
56 * struct mmc_test_area - information for performance tests.
57 * @max_sz: test area size (in bytes)
58 * @dev_addr: address on card at which to do performance tests
59 * @max_segs: maximum segments in scatterlist @sg
60 * @blocks: number of (512 byte) blocks currently mapped by @sg
61 * @sg_len: length of currently mapped scatterlist @sg
62 * @mem: allocated memory
63 * @sg: scatterlist
64 */
65struct mmc_test_area {
66 unsigned long max_sz;
67 unsigned int dev_addr;
68 unsigned int max_segs;
69 unsigned int blocks;
70 unsigned int sg_len;
71 struct mmc_test_mem *mem;
72 struct scatterlist *sg;
73};
74
75/**
76 * struct mmc_test_card - test information.
77 * @card: card under test
78 * @scratch: transfer buffer
79 * @buffer: transfer buffer
80 * @highmem: buffer for highmem tests
81 * @area: information for performance tests
82 */
28struct mmc_test_card { 83struct mmc_test_card {
29 struct mmc_card *card; 84 struct mmc_card *card;
30 85
@@ -33,6 +88,7 @@ struct mmc_test_card {
33#ifdef CONFIG_HIGHMEM 88#ifdef CONFIG_HIGHMEM
34 struct page *highmem; 89 struct page *highmem;
35#endif 90#endif
91 struct mmc_test_area area;
36}; 92};
37 93
38/*******************************************************************/ 94/*******************************************************************/
@@ -97,6 +153,12 @@ static void mmc_test_prepare_mrq(struct mmc_test_card *test,
97 mmc_set_data_timeout(mrq->data, test->card); 153 mmc_set_data_timeout(mrq->data, test->card);
98} 154}
99 155
156static int mmc_test_busy(struct mmc_command *cmd)
157{
158 return !(cmd->resp[0] & R1_READY_FOR_DATA) ||
159 (R1_CURRENT_STATE(cmd->resp[0]) == 7);
160}
161
100/* 162/*
101 * Wait for the card to finish the busy state 163 * Wait for the card to finish the busy state
102 */ 164 */
@@ -117,13 +179,13 @@ static int mmc_test_wait_busy(struct mmc_test_card *test)
117 if (ret) 179 if (ret)
118 break; 180 break;
119 181
120 if (!busy && !(cmd.resp[0] & R1_READY_FOR_DATA)) { 182 if (!busy && mmc_test_busy(&cmd)) {
121 busy = 1; 183 busy = 1;
122 printk(KERN_INFO "%s: Warning: Host did not " 184 printk(KERN_INFO "%s: Warning: Host did not "
123 "wait for busy state to end.\n", 185 "wait for busy state to end.\n",
124 mmc_hostname(test->card->host)); 186 mmc_hostname(test->card->host));
125 } 187 }
126 } while (!(cmd.resp[0] & R1_READY_FOR_DATA)); 188 } while (mmc_test_busy(&cmd));
127 189
128 return ret; 190 return ret;
129} 191}
@@ -170,6 +232,248 @@ static int mmc_test_buffer_transfer(struct mmc_test_card *test,
170 return 0; 232 return 0;
171} 233}
172 234
235static void mmc_test_free_mem(struct mmc_test_mem *mem)
236{
237 if (!mem)
238 return;
239 while (mem->cnt--)
240 __free_pages(mem->arr[mem->cnt].page,
241 mem->arr[mem->cnt].order);
242 kfree(mem->arr);
243 kfree(mem);
244}
245
246/*
247 * Allocate a lot of memory, preferrably max_sz but at least min_sz. In case
248 * there isn't much memory do not exceed 1/16th total lowmem pages.
249 */
250static struct mmc_test_mem *mmc_test_alloc_mem(unsigned long min_sz,
251 unsigned long max_sz)
252{
253 unsigned long max_page_cnt = DIV_ROUND_UP(max_sz, PAGE_SIZE);
254 unsigned long min_page_cnt = DIV_ROUND_UP(min_sz, PAGE_SIZE);
255 unsigned long page_cnt = 0;
256 unsigned long limit = nr_free_buffer_pages() >> 4;
257 struct mmc_test_mem *mem;
258
259 if (max_page_cnt > limit)
260 max_page_cnt = limit;
261 if (max_page_cnt < min_page_cnt)
262 max_page_cnt = min_page_cnt;
263
264 mem = kzalloc(sizeof(struct mmc_test_mem), GFP_KERNEL);
265 if (!mem)
266 return NULL;
267
268 mem->arr = kzalloc(sizeof(struct mmc_test_pages) * max_page_cnt,
269 GFP_KERNEL);
270 if (!mem->arr)
271 goto out_free;
272
273 while (max_page_cnt) {
274 struct page *page;
275 unsigned int order;
276 gfp_t flags = GFP_KERNEL | GFP_DMA | __GFP_NOWARN |
277 __GFP_NORETRY;
278
279 order = get_order(max_page_cnt << PAGE_SHIFT);
280 while (1) {
281 page = alloc_pages(flags, order);
282 if (page || !order)
283 break;
284 order -= 1;
285 }
286 if (!page) {
287 if (page_cnt < min_page_cnt)
288 goto out_free;
289 break;
290 }
291 mem->arr[mem->cnt].page = page;
292 mem->arr[mem->cnt].order = order;
293 mem->cnt += 1;
294 if (max_page_cnt <= (1UL << order))
295 break;
296 max_page_cnt -= 1UL << order;
297 page_cnt += 1UL << order;
298 }
299
300 return mem;
301
302out_free:
303 mmc_test_free_mem(mem);
304 return NULL;
305}
306
307/*
308 * Map memory into a scatterlist. Optionally allow the same memory to be
309 * mapped more than once.
310 */
311static int mmc_test_map_sg(struct mmc_test_mem *mem, unsigned long sz,
312 struct scatterlist *sglist, int repeat,
313 unsigned int max_segs, unsigned int *sg_len)
314{
315 struct scatterlist *sg = NULL;
316 unsigned int i;
317
318 sg_init_table(sglist, max_segs);
319
320 *sg_len = 0;
321 do {
322 for (i = 0; i < mem->cnt; i++) {
323 unsigned long len = PAGE_SIZE << mem->arr[i].order;
324
325 if (sz < len)
326 len = sz;
327 if (sg)
328 sg = sg_next(sg);
329 else
330 sg = sglist;
331 if (!sg)
332 return -EINVAL;
333 sg_set_page(sg, mem->arr[i].page, len, 0);
334 sz -= len;
335 *sg_len += 1;
336 if (!sz)
337 break;
338 }
339 } while (sz && repeat);
340
341 if (sz)
342 return -EINVAL;
343
344 if (sg)
345 sg_mark_end(sg);
346
347 return 0;
348}
349
350/*
351 * Map memory into a scatterlist so that no pages are contiguous. Allow the
352 * same memory to be mapped more than once.
353 */
354static int mmc_test_map_sg_max_scatter(struct mmc_test_mem *mem,
355 unsigned long sz,
356 struct scatterlist *sglist,
357 unsigned int max_segs,
358 unsigned int *sg_len)
359{
360 struct scatterlist *sg = NULL;
361 unsigned int i = mem->cnt, cnt;
362 unsigned long len;
363 void *base, *addr, *last_addr = NULL;
364
365 sg_init_table(sglist, max_segs);
366
367 *sg_len = 0;
368 while (sz && i) {
369 base = page_address(mem->arr[--i].page);
370 cnt = 1 << mem->arr[i].order;
371 while (sz && cnt) {
372 addr = base + PAGE_SIZE * --cnt;
373 if (last_addr && last_addr + PAGE_SIZE == addr)
374 continue;
375 last_addr = addr;
376 len = PAGE_SIZE;
377 if (sz < len)
378 len = sz;
379 if (sg)
380 sg = sg_next(sg);
381 else
382 sg = sglist;
383 if (!sg)
384 return -EINVAL;
385 sg_set_page(sg, virt_to_page(addr), len, 0);
386 sz -= len;
387 *sg_len += 1;
388 }
389 }
390
391 if (sg)
392 sg_mark_end(sg);
393
394 return 0;
395}
396
397/*
398 * Calculate transfer rate in bytes per second.
399 */
400static unsigned int mmc_test_rate(uint64_t bytes, struct timespec *ts)
401{
402 uint64_t ns;
403
404 ns = ts->tv_sec;
405 ns *= 1000000000;
406 ns += ts->tv_nsec;
407
408 bytes *= 1000000000;
409
410 while (ns > UINT_MAX) {
411 bytes >>= 1;
412 ns >>= 1;
413 }
414
415 if (!ns)
416 return 0;
417
418 do_div(bytes, (uint32_t)ns);
419
420 return bytes;
421}
422
423/*
424 * Print the transfer rate.
425 */
426static void mmc_test_print_rate(struct mmc_test_card *test, uint64_t bytes,
427 struct timespec *ts1, struct timespec *ts2)
428{
429 unsigned int rate, sectors = bytes >> 9;
430 struct timespec ts;
431
432 ts = timespec_sub(*ts2, *ts1);
433
434 rate = mmc_test_rate(bytes, &ts);
435
436 printk(KERN_INFO "%s: Transfer of %u sectors (%u%s KiB) took %lu.%09lu "
437 "seconds (%u kB/s, %u KiB/s)\n",
438 mmc_hostname(test->card->host), sectors, sectors >> 1,
439 (sectors == 1 ? ".5" : ""), (unsigned long)ts.tv_sec,
440 (unsigned long)ts.tv_nsec, rate / 1000, rate / 1024);
441}
442
443/*
444 * Print the average transfer rate.
445 */
446static void mmc_test_print_avg_rate(struct mmc_test_card *test, uint64_t bytes,
447 unsigned int count, struct timespec *ts1,
448 struct timespec *ts2)
449{
450 unsigned int rate, sectors = bytes >> 9;
451 uint64_t tot = bytes * count;
452 struct timespec ts;
453
454 ts = timespec_sub(*ts2, *ts1);
455
456 rate = mmc_test_rate(tot, &ts);
457
458 printk(KERN_INFO "%s: Transfer of %u x %u sectors (%u x %u%s KiB) took "
459 "%lu.%09lu seconds (%u kB/s, %u KiB/s)\n",
460 mmc_hostname(test->card->host), count, sectors, count,
461 sectors >> 1, (sectors == 1 ? ".5" : ""),
462 (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
463 rate / 1000, rate / 1024);
464}
465
466/*
467 * Return the card size in sectors.
468 */
469static unsigned int mmc_test_capacity(struct mmc_card *card)
470{
471 if (!mmc_card_sd(card) && mmc_card_blockaddr(card))
472 return card->ext_csd.sectors;
473 else
474 return card->csd.capacity << (card->csd.read_blkbits - 9);
475}
476
173/*******************************************************************/ 477/*******************************************************************/
174/* Test preparation and cleanup */ 478/* Test preparation and cleanup */
175/*******************************************************************/ 479/*******************************************************************/
@@ -893,8 +1197,419 @@ static int mmc_test_multi_read_high(struct mmc_test_card *test)
893 return 0; 1197 return 0;
894} 1198}
895 1199
1200#else
1201
1202static int mmc_test_no_highmem(struct mmc_test_card *test)
1203{
1204 printk(KERN_INFO "%s: Highmem not configured - test skipped\n",
1205 mmc_hostname(test->card->host));
1206 return 0;
1207}
1208
896#endif /* CONFIG_HIGHMEM */ 1209#endif /* CONFIG_HIGHMEM */
897 1210
1211/*
1212 * Map sz bytes so that it can be transferred.
1213 */
1214static int mmc_test_area_map(struct mmc_test_card *test, unsigned long sz,
1215 int max_scatter)
1216{
1217 struct mmc_test_area *t = &test->area;
1218
1219 t->blocks = sz >> 9;
1220
1221 if (max_scatter) {
1222 return mmc_test_map_sg_max_scatter(t->mem, sz, t->sg,
1223 t->max_segs, &t->sg_len);
1224 } else {
1225 return mmc_test_map_sg(t->mem, sz, t->sg, 1, t->max_segs,
1226 &t->sg_len);
1227 }
1228}
1229
1230/*
1231 * Transfer bytes mapped by mmc_test_area_map().
1232 */
1233static int mmc_test_area_transfer(struct mmc_test_card *test,
1234 unsigned int dev_addr, int write)
1235{
1236 struct mmc_test_area *t = &test->area;
1237
1238 return mmc_test_simple_transfer(test, t->sg, t->sg_len, dev_addr,
1239 t->blocks, 512, write);
1240}
1241
1242/*
1243 * Map and transfer bytes.
1244 */
1245static int mmc_test_area_io(struct mmc_test_card *test, unsigned long sz,
1246 unsigned int dev_addr, int write, int max_scatter,
1247 int timed)
1248{
1249 struct timespec ts1, ts2;
1250 int ret;
1251
1252 ret = mmc_test_area_map(test, sz, max_scatter);
1253 if (ret)
1254 return ret;
1255
1256 if (timed)
1257 getnstimeofday(&ts1);
1258
1259 ret = mmc_test_area_transfer(test, dev_addr, write);
1260 if (ret)
1261 return ret;
1262
1263 if (timed)
1264 getnstimeofday(&ts2);
1265
1266 if (timed)
1267 mmc_test_print_rate(test, sz, &ts1, &ts2);
1268
1269 return 0;
1270}
1271
1272/*
1273 * Write the test area entirely.
1274 */
1275static int mmc_test_area_fill(struct mmc_test_card *test)
1276{
1277 return mmc_test_area_io(test, test->area.max_sz, test->area.dev_addr,
1278 1, 0, 0);
1279}
1280
1281/*
1282 * Erase the test area entirely.
1283 */
1284static int mmc_test_area_erase(struct mmc_test_card *test)
1285{
1286 struct mmc_test_area *t = &test->area;
1287
1288 if (!mmc_can_erase(test->card))
1289 return 0;
1290
1291 return mmc_erase(test->card, t->dev_addr, test->area.max_sz >> 9,
1292 MMC_ERASE_ARG);
1293}
1294
1295/*
1296 * Cleanup struct mmc_test_area.
1297 */
1298static int mmc_test_area_cleanup(struct mmc_test_card *test)
1299{
1300 struct mmc_test_area *t = &test->area;
1301
1302 kfree(t->sg);
1303 mmc_test_free_mem(t->mem);
1304
1305 return 0;
1306}
1307
1308/*
1309 * Initialize an area for testing large transfers. The size of the area is the
1310 * preferred erase size which is a good size for optimal transfer speed. Note
1311 * that is typically 4MiB for modern cards. The test area is set to the middle
1312 * of the card because cards may have different charateristics at the front
1313 * (for FAT file system optimization). Optionally, the area is erased (if the
1314 * card supports it) which may improve write performance. Optionally, the area
1315 * is filled with data for subsequent read tests.
1316 */
1317static int mmc_test_area_init(struct mmc_test_card *test, int erase, int fill)
1318{
1319 struct mmc_test_area *t = &test->area;
1320 unsigned long min_sz = 64 * 1024;
1321 int ret;
1322
1323 ret = mmc_test_set_blksize(test, 512);
1324 if (ret)
1325 return ret;
1326
1327 if (test->card->pref_erase > TEST_AREA_MAX_SIZE >> 9)
1328 t->max_sz = TEST_AREA_MAX_SIZE;
1329 else
1330 t->max_sz = (unsigned long)test->card->pref_erase << 9;
1331 /*
1332 * Try to allocate enough memory for the whole area. Less is OK
1333 * because the same memory can be mapped into the scatterlist more than
1334 * once.
1335 */
1336 t->mem = mmc_test_alloc_mem(min_sz, t->max_sz);
1337 if (!t->mem)
1338 return -ENOMEM;
1339
1340 t->max_segs = DIV_ROUND_UP(t->max_sz, PAGE_SIZE);
1341 t->sg = kmalloc(sizeof(struct scatterlist) * t->max_segs, GFP_KERNEL);
1342 if (!t->sg) {
1343 ret = -ENOMEM;
1344 goto out_free;
1345 }
1346
1347 t->dev_addr = mmc_test_capacity(test->card) / 2;
1348 t->dev_addr -= t->dev_addr % (t->max_sz >> 9);
1349
1350 if (erase) {
1351 ret = mmc_test_area_erase(test);
1352 if (ret)
1353 goto out_free;
1354 }
1355
1356 if (fill) {
1357 ret = mmc_test_area_fill(test);
1358 if (ret)
1359 goto out_free;
1360 }
1361
1362 return 0;
1363
1364out_free:
1365 mmc_test_area_cleanup(test);
1366 return ret;
1367}
1368
1369/*
1370 * Prepare for large transfers. Do not erase the test area.
1371 */
1372static int mmc_test_area_prepare(struct mmc_test_card *test)
1373{
1374 return mmc_test_area_init(test, 0, 0);
1375}
1376
1377/*
1378 * Prepare for large transfers. Do erase the test area.
1379 */
1380static int mmc_test_area_prepare_erase(struct mmc_test_card *test)
1381{
1382 return mmc_test_area_init(test, 1, 0);
1383}
1384
1385/*
1386 * Prepare for large transfers. Erase and fill the test area.
1387 */
1388static int mmc_test_area_prepare_fill(struct mmc_test_card *test)
1389{
1390 return mmc_test_area_init(test, 1, 1);
1391}
1392
1393/*
1394 * Test best-case performance. Best-case performance is expected from
1395 * a single large transfer.
1396 *
1397 * An additional option (max_scatter) allows the measurement of the same
1398 * transfer but with no contiguous pages in the scatter list. This tests
1399 * the efficiency of DMA to handle scattered pages.
1400 */
1401static int mmc_test_best_performance(struct mmc_test_card *test, int write,
1402 int max_scatter)
1403{
1404 return mmc_test_area_io(test, test->area.max_sz, test->area.dev_addr,
1405 write, max_scatter, 1);
1406}
1407
1408/*
1409 * Best-case read performance.
1410 */
1411static int mmc_test_best_read_performance(struct mmc_test_card *test)
1412{
1413 return mmc_test_best_performance(test, 0, 0);
1414}
1415
1416/*
1417 * Best-case write performance.
1418 */
1419static int mmc_test_best_write_performance(struct mmc_test_card *test)
1420{
1421 return mmc_test_best_performance(test, 1, 0);
1422}
1423
1424/*
1425 * Best-case read performance into scattered pages.
1426 */
1427static int mmc_test_best_read_perf_max_scatter(struct mmc_test_card *test)
1428{
1429 return mmc_test_best_performance(test, 0, 1);
1430}
1431
1432/*
1433 * Best-case write performance from scattered pages.
1434 */
1435static int mmc_test_best_write_perf_max_scatter(struct mmc_test_card *test)
1436{
1437 return mmc_test_best_performance(test, 1, 1);
1438}
1439
1440/*
1441 * Single read performance by transfer size.
1442 */
1443static int mmc_test_profile_read_perf(struct mmc_test_card *test)
1444{
1445 unsigned long sz;
1446 unsigned int dev_addr;
1447 int ret;
1448
1449 for (sz = 512; sz < test->area.max_sz; sz <<= 1) {
1450 dev_addr = test->area.dev_addr + (sz >> 9);
1451 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1452 if (ret)
1453 return ret;
1454 }
1455 dev_addr = test->area.dev_addr;
1456 return mmc_test_area_io(test, sz, dev_addr, 0, 0, 1);
1457}
1458
1459/*
1460 * Single write performance by transfer size.
1461 */
1462static int mmc_test_profile_write_perf(struct mmc_test_card *test)
1463{
1464 unsigned long sz;
1465 unsigned int dev_addr;
1466 int ret;
1467
1468 ret = mmc_test_area_erase(test);
1469 if (ret)
1470 return ret;
1471 for (sz = 512; sz < test->area.max_sz; sz <<= 1) {
1472 dev_addr = test->area.dev_addr + (sz >> 9);
1473 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1474 if (ret)
1475 return ret;
1476 }
1477 ret = mmc_test_area_erase(test);
1478 if (ret)
1479 return ret;
1480 dev_addr = test->area.dev_addr;
1481 return mmc_test_area_io(test, sz, dev_addr, 1, 0, 1);
1482}
1483
1484/*
1485 * Single trim performance by transfer size.
1486 */
1487static int mmc_test_profile_trim_perf(struct mmc_test_card *test)
1488{
1489 unsigned long sz;
1490 unsigned int dev_addr;
1491 struct timespec ts1, ts2;
1492 int ret;
1493
1494 if (!mmc_can_trim(test->card))
1495 return RESULT_UNSUP_CARD;
1496
1497 if (!mmc_can_erase(test->card))
1498 return RESULT_UNSUP_HOST;
1499
1500 for (sz = 512; sz < test->area.max_sz; sz <<= 1) {
1501 dev_addr = test->area.dev_addr + (sz >> 9);
1502 getnstimeofday(&ts1);
1503 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1504 if (ret)
1505 return ret;
1506 getnstimeofday(&ts2);
1507 mmc_test_print_rate(test, sz, &ts1, &ts2);
1508 }
1509 dev_addr = test->area.dev_addr;
1510 getnstimeofday(&ts1);
1511 ret = mmc_erase(test->card, dev_addr, sz >> 9, MMC_TRIM_ARG);
1512 if (ret)
1513 return ret;
1514 getnstimeofday(&ts2);
1515 mmc_test_print_rate(test, sz, &ts1, &ts2);
1516 return 0;
1517}
1518
1519/*
1520 * Consecutive read performance by transfer size.
1521 */
1522static int mmc_test_profile_seq_read_perf(struct mmc_test_card *test)
1523{
1524 unsigned long sz;
1525 unsigned int dev_addr, i, cnt;
1526 struct timespec ts1, ts2;
1527 int ret;
1528
1529 for (sz = 512; sz <= test->area.max_sz; sz <<= 1) {
1530 cnt = test->area.max_sz / sz;
1531 dev_addr = test->area.dev_addr;
1532 getnstimeofday(&ts1);
1533 for (i = 0; i < cnt; i++) {
1534 ret = mmc_test_area_io(test, sz, dev_addr, 0, 0, 0);
1535 if (ret)
1536 return ret;
1537 dev_addr += (sz >> 9);
1538 }
1539 getnstimeofday(&ts2);
1540 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1541 }
1542 return 0;
1543}
1544
1545/*
1546 * Consecutive write performance by transfer size.
1547 */
1548static int mmc_test_profile_seq_write_perf(struct mmc_test_card *test)
1549{
1550 unsigned long sz;
1551 unsigned int dev_addr, i, cnt;
1552 struct timespec ts1, ts2;
1553 int ret;
1554
1555 for (sz = 512; sz <= test->area.max_sz; sz <<= 1) {
1556 ret = mmc_test_area_erase(test);
1557 if (ret)
1558 return ret;
1559 cnt = test->area.max_sz / sz;
1560 dev_addr = test->area.dev_addr;
1561 getnstimeofday(&ts1);
1562 for (i = 0; i < cnt; i++) {
1563 ret = mmc_test_area_io(test, sz, dev_addr, 1, 0, 0);
1564 if (ret)
1565 return ret;
1566 dev_addr += (sz >> 9);
1567 }
1568 getnstimeofday(&ts2);
1569 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1570 }
1571 return 0;
1572}
1573
1574/*
1575 * Consecutive trim performance by transfer size.
1576 */
1577static int mmc_test_profile_seq_trim_perf(struct mmc_test_card *test)
1578{
1579 unsigned long sz;
1580 unsigned int dev_addr, i, cnt;
1581 struct timespec ts1, ts2;
1582 int ret;
1583
1584 if (!mmc_can_trim(test->card))
1585 return RESULT_UNSUP_CARD;
1586
1587 if (!mmc_can_erase(test->card))
1588 return RESULT_UNSUP_HOST;
1589
1590 for (sz = 512; sz <= test->area.max_sz; sz <<= 1) {
1591 ret = mmc_test_area_erase(test);
1592 if (ret)
1593 return ret;
1594 ret = mmc_test_area_fill(test);
1595 if (ret)
1596 return ret;
1597 cnt = test->area.max_sz / sz;
1598 dev_addr = test->area.dev_addr;
1599 getnstimeofday(&ts1);
1600 for (i = 0; i < cnt; i++) {
1601 ret = mmc_erase(test->card, dev_addr, sz >> 9,
1602 MMC_TRIM_ARG);
1603 if (ret)
1604 return ret;
1605 dev_addr += (sz >> 9);
1606 }
1607 getnstimeofday(&ts2);
1608 mmc_test_print_avg_rate(test, sz, cnt, &ts1, &ts2);
1609 }
1610 return 0;
1611}
1612
898static const struct mmc_test_case mmc_test_cases[] = { 1613static const struct mmc_test_case mmc_test_cases[] = {
899 { 1614 {
900 .name = "Basic write (no data verification)", 1615 .name = "Basic write (no data verification)",
@@ -1040,8 +1755,100 @@ static const struct mmc_test_case mmc_test_cases[] = {
1040 .cleanup = mmc_test_cleanup, 1755 .cleanup = mmc_test_cleanup,
1041 }, 1756 },
1042 1757
1758#else
1759
1760 {
1761 .name = "Highmem write",
1762 .run = mmc_test_no_highmem,
1763 },
1764
1765 {
1766 .name = "Highmem read",
1767 .run = mmc_test_no_highmem,
1768 },
1769
1770 {
1771 .name = "Multi-block highmem write",
1772 .run = mmc_test_no_highmem,
1773 },
1774
1775 {
1776 .name = "Multi-block highmem read",
1777 .run = mmc_test_no_highmem,
1778 },
1779
1043#endif /* CONFIG_HIGHMEM */ 1780#endif /* CONFIG_HIGHMEM */
1044 1781
1782 {
1783 .name = "Best-case read performance",
1784 .prepare = mmc_test_area_prepare_fill,
1785 .run = mmc_test_best_read_performance,
1786 .cleanup = mmc_test_area_cleanup,
1787 },
1788
1789 {
1790 .name = "Best-case write performance",
1791 .prepare = mmc_test_area_prepare_erase,
1792 .run = mmc_test_best_write_performance,
1793 .cleanup = mmc_test_area_cleanup,
1794 },
1795
1796 {
1797 .name = "Best-case read performance into scattered pages",
1798 .prepare = mmc_test_area_prepare_fill,
1799 .run = mmc_test_best_read_perf_max_scatter,
1800 .cleanup = mmc_test_area_cleanup,
1801 },
1802
1803 {
1804 .name = "Best-case write performance from scattered pages",
1805 .prepare = mmc_test_area_prepare_erase,
1806 .run = mmc_test_best_write_perf_max_scatter,
1807 .cleanup = mmc_test_area_cleanup,
1808 },
1809
1810 {
1811 .name = "Single read performance by transfer size",
1812 .prepare = mmc_test_area_prepare_fill,
1813 .run = mmc_test_profile_read_perf,
1814 .cleanup = mmc_test_area_cleanup,
1815 },
1816
1817 {
1818 .name = "Single write performance by transfer size",
1819 .prepare = mmc_test_area_prepare,
1820 .run = mmc_test_profile_write_perf,
1821 .cleanup = mmc_test_area_cleanup,
1822 },
1823
1824 {
1825 .name = "Single trim performance by transfer size",
1826 .prepare = mmc_test_area_prepare_fill,
1827 .run = mmc_test_profile_trim_perf,
1828 .cleanup = mmc_test_area_cleanup,
1829 },
1830
1831 {
1832 .name = "Consecutive read performance by transfer size",
1833 .prepare = mmc_test_area_prepare_fill,
1834 .run = mmc_test_profile_seq_read_perf,
1835 .cleanup = mmc_test_area_cleanup,
1836 },
1837
1838 {
1839 .name = "Consecutive write performance by transfer size",
1840 .prepare = mmc_test_area_prepare,
1841 .run = mmc_test_profile_seq_write_perf,
1842 .cleanup = mmc_test_area_cleanup,
1843 },
1844
1845 {
1846 .name = "Consecutive trim performance by transfer size",
1847 .prepare = mmc_test_area_prepare,
1848 .run = mmc_test_profile_seq_trim_perf,
1849 .cleanup = mmc_test_area_cleanup,
1850 },
1851
1045}; 1852};
1046 1853
1047static DEFINE_MUTEX(mmc_test_lock); 1854static DEFINE_MUTEX(mmc_test_lock);
diff --git a/drivers/mmc/card/queue.c b/drivers/mmc/card/queue.c
index d6ded247d941..e876678176be 100644
--- a/drivers/mmc/card/queue.c
+++ b/drivers/mmc/card/queue.c
@@ -30,9 +30,9 @@
30static int mmc_prep_request(struct request_queue *q, struct request *req) 30static int mmc_prep_request(struct request_queue *q, struct request *req)
31{ 31{
32 /* 32 /*
33 * We only like normal block requests. 33 * We only like normal block requests and discards.
34 */ 34 */
35 if (!blk_fs_request(req)) { 35 if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
36 blk_dump_rq_flags(req, "MMC bad request"); 36 blk_dump_rq_flags(req, "MMC bad request");
37 return BLKPREP_KILL; 37 return BLKPREP_KILL;
38 } 38 }
@@ -128,8 +128,23 @@ int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock
128 mq->req = NULL; 128 mq->req = NULL;
129 129
130 blk_queue_prep_rq(mq->queue, mmc_prep_request); 130 blk_queue_prep_rq(mq->queue, mmc_prep_request);
131 blk_queue_ordered(mq->queue, QUEUE_ORDERED_DRAIN, NULL); 131 blk_queue_ordered(mq->queue, QUEUE_ORDERED_DRAIN);
132 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue); 132 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
133 if (mmc_can_erase(card)) {
134 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mq->queue);
135 mq->queue->limits.max_discard_sectors = UINT_MAX;
136 if (card->erased_byte == 0)
137 mq->queue->limits.discard_zeroes_data = 1;
138 if (!mmc_can_trim(card) && is_power_of_2(card->erase_size)) {
139 mq->queue->limits.discard_granularity =
140 card->erase_size << 9;
141 mq->queue->limits.discard_alignment =
142 card->erase_size << 9;
143 }
144 if (mmc_can_secure_erase_trim(card))
145 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD,
146 mq->queue);
147 }
133 148
134#ifdef CONFIG_MMC_BLOCK_BOUNCE 149#ifdef CONFIG_MMC_BLOCK_BOUNCE
135 if (host->max_hw_segs == 1) { 150 if (host->max_hw_segs == 1) {
diff --git a/drivers/mmc/core/bus.c b/drivers/mmc/core/bus.c
index 49d9dcaeca49..7cd9749dc21d 100644
--- a/drivers/mmc/core/bus.c
+++ b/drivers/mmc/core/bus.c
@@ -37,6 +37,8 @@ static ssize_t mmc_type_show(struct device *dev,
37 return sprintf(buf, "SD\n"); 37 return sprintf(buf, "SD\n");
38 case MMC_TYPE_SDIO: 38 case MMC_TYPE_SDIO:
39 return sprintf(buf, "SDIO\n"); 39 return sprintf(buf, "SDIO\n");
40 case MMC_TYPE_SD_COMBO:
41 return sprintf(buf, "SDcombo\n");
40 default: 42 default:
41 return -EFAULT; 43 return -EFAULT;
42 } 44 }
@@ -74,6 +76,9 @@ mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
74 case MMC_TYPE_SDIO: 76 case MMC_TYPE_SDIO:
75 type = "SDIO"; 77 type = "SDIO";
76 break; 78 break;
79 case MMC_TYPE_SD_COMBO:
80 type = "SDcombo";
81 break;
77 default: 82 default:
78 type = NULL; 83 type = NULL;
79 } 84 }
@@ -239,6 +244,10 @@ int mmc_add_card(struct mmc_card *card)
239 case MMC_TYPE_SDIO: 244 case MMC_TYPE_SDIO:
240 type = "SDIO"; 245 type = "SDIO";
241 break; 246 break;
247 case MMC_TYPE_SD_COMBO:
248 type = "SD-combo";
249 if (mmc_card_blockaddr(card))
250 type = "SDHC-combo";
242 default: 251 default:
243 type = "?"; 252 type = "?";
244 break; 253 break;
diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index 569e94da844c..5db49b124ffa 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -1050,6 +1050,352 @@ void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1050 1050
1051EXPORT_SYMBOL(mmc_detect_change); 1051EXPORT_SYMBOL(mmc_detect_change);
1052 1052
1053void mmc_init_erase(struct mmc_card *card)
1054{
1055 unsigned int sz;
1056
1057 if (is_power_of_2(card->erase_size))
1058 card->erase_shift = ffs(card->erase_size) - 1;
1059 else
1060 card->erase_shift = 0;
1061
1062 /*
1063 * It is possible to erase an arbitrarily large area of an SD or MMC
1064 * card. That is not desirable because it can take a long time
1065 * (minutes) potentially delaying more important I/O, and also the
1066 * timeout calculations become increasingly hugely over-estimated.
1067 * Consequently, 'pref_erase' is defined as a guide to limit erases
1068 * to that size and alignment.
1069 *
1070 * For SD cards that define Allocation Unit size, limit erases to one
1071 * Allocation Unit at a time. For MMC cards that define High Capacity
1072 * Erase Size, whether it is switched on or not, limit to that size.
1073 * Otherwise just have a stab at a good value. For modern cards it
1074 * will end up being 4MiB. Note that if the value is too small, it
1075 * can end up taking longer to erase.
1076 */
1077 if (mmc_card_sd(card) && card->ssr.au) {
1078 card->pref_erase = card->ssr.au;
1079 card->erase_shift = ffs(card->ssr.au) - 1;
1080 } else if (card->ext_csd.hc_erase_size) {
1081 card->pref_erase = card->ext_csd.hc_erase_size;
1082 } else {
1083 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1084 if (sz < 128)
1085 card->pref_erase = 512 * 1024 / 512;
1086 else if (sz < 512)
1087 card->pref_erase = 1024 * 1024 / 512;
1088 else if (sz < 1024)
1089 card->pref_erase = 2 * 1024 * 1024 / 512;
1090 else
1091 card->pref_erase = 4 * 1024 * 1024 / 512;
1092 if (card->pref_erase < card->erase_size)
1093 card->pref_erase = card->erase_size;
1094 else {
1095 sz = card->pref_erase % card->erase_size;
1096 if (sz)
1097 card->pref_erase += card->erase_size - sz;
1098 }
1099 }
1100}
1101
1102static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
1103 struct mmc_command *cmd,
1104 unsigned int arg, unsigned int qty)
1105{
1106 unsigned int erase_timeout;
1107
1108 if (card->ext_csd.erase_group_def & 1) {
1109 /* High Capacity Erase Group Size uses HC timeouts */
1110 if (arg == MMC_TRIM_ARG)
1111 erase_timeout = card->ext_csd.trim_timeout;
1112 else
1113 erase_timeout = card->ext_csd.hc_erase_timeout;
1114 } else {
1115 /* CSD Erase Group Size uses write timeout */
1116 unsigned int mult = (10 << card->csd.r2w_factor);
1117 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1118 unsigned int timeout_us;
1119
1120 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1121 if (card->csd.tacc_ns < 1000000)
1122 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1123 else
1124 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1125
1126 /*
1127 * ios.clock is only a target. The real clock rate might be
1128 * less but not that much less, so fudge it by multiplying by 2.
1129 */
1130 timeout_clks <<= 1;
1131 timeout_us += (timeout_clks * 1000) /
1132 (card->host->ios.clock / 1000);
1133
1134 erase_timeout = timeout_us / 1000;
1135
1136 /*
1137 * Theoretically, the calculation could underflow so round up
1138 * to 1ms in that case.
1139 */
1140 if (!erase_timeout)
1141 erase_timeout = 1;
1142 }
1143
1144 /* Multiplier for secure operations */
1145 if (arg & MMC_SECURE_ARGS) {
1146 if (arg == MMC_SECURE_ERASE_ARG)
1147 erase_timeout *= card->ext_csd.sec_erase_mult;
1148 else
1149 erase_timeout *= card->ext_csd.sec_trim_mult;
1150 }
1151
1152 erase_timeout *= qty;
1153
1154 /*
1155 * Ensure at least a 1 second timeout for SPI as per
1156 * 'mmc_set_data_timeout()'
1157 */
1158 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1159 erase_timeout = 1000;
1160
1161 cmd->erase_timeout = erase_timeout;
1162}
1163
1164static void mmc_set_sd_erase_timeout(struct mmc_card *card,
1165 struct mmc_command *cmd, unsigned int arg,
1166 unsigned int qty)
1167{
1168 if (card->ssr.erase_timeout) {
1169 /* Erase timeout specified in SD Status Register (SSR) */
1170 cmd->erase_timeout = card->ssr.erase_timeout * qty +
1171 card->ssr.erase_offset;
1172 } else {
1173 /*
1174 * Erase timeout not specified in SD Status Register (SSR) so
1175 * use 250ms per write block.
1176 */
1177 cmd->erase_timeout = 250 * qty;
1178 }
1179
1180 /* Must not be less than 1 second */
1181 if (cmd->erase_timeout < 1000)
1182 cmd->erase_timeout = 1000;
1183}
1184
1185static void mmc_set_erase_timeout(struct mmc_card *card,
1186 struct mmc_command *cmd, unsigned int arg,
1187 unsigned int qty)
1188{
1189 if (mmc_card_sd(card))
1190 mmc_set_sd_erase_timeout(card, cmd, arg, qty);
1191 else
1192 mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
1193}
1194
1195static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1196 unsigned int to, unsigned int arg)
1197{
1198 struct mmc_command cmd;
1199 unsigned int qty = 0;
1200 int err;
1201
1202 /*
1203 * qty is used to calculate the erase timeout which depends on how many
1204 * erase groups (or allocation units in SD terminology) are affected.
1205 * We count erasing part of an erase group as one erase group.
1206 * For SD, the allocation units are always a power of 2. For MMC, the
1207 * erase group size is almost certainly also power of 2, but it does not
1208 * seem to insist on that in the JEDEC standard, so we fall back to
1209 * division in that case. SD may not specify an allocation unit size,
1210 * in which case the timeout is based on the number of write blocks.
1211 *
1212 * Note that the timeout for secure trim 2 will only be correct if the
1213 * number of erase groups specified is the same as the total of all
1214 * preceding secure trim 1 commands. Since the power may have been
1215 * lost since the secure trim 1 commands occurred, it is generally
1216 * impossible to calculate the secure trim 2 timeout correctly.
1217 */
1218 if (card->erase_shift)
1219 qty += ((to >> card->erase_shift) -
1220 (from >> card->erase_shift)) + 1;
1221 else if (mmc_card_sd(card))
1222 qty += to - from + 1;
1223 else
1224 qty += ((to / card->erase_size) -
1225 (from / card->erase_size)) + 1;
1226
1227 if (!mmc_card_blockaddr(card)) {
1228 from <<= 9;
1229 to <<= 9;
1230 }
1231
1232 memset(&cmd, 0, sizeof(struct mmc_command));
1233 if (mmc_card_sd(card))
1234 cmd.opcode = SD_ERASE_WR_BLK_START;
1235 else
1236 cmd.opcode = MMC_ERASE_GROUP_START;
1237 cmd.arg = from;
1238 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1239 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1240 if (err) {
1241 printk(KERN_ERR "mmc_erase: group start error %d, "
1242 "status %#x\n", err, cmd.resp[0]);
1243 err = -EINVAL;
1244 goto out;
1245 }
1246
1247 memset(&cmd, 0, sizeof(struct mmc_command));
1248 if (mmc_card_sd(card))
1249 cmd.opcode = SD_ERASE_WR_BLK_END;
1250 else
1251 cmd.opcode = MMC_ERASE_GROUP_END;
1252 cmd.arg = to;
1253 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1254 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1255 if (err) {
1256 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1257 err, cmd.resp[0]);
1258 err = -EINVAL;
1259 goto out;
1260 }
1261
1262 memset(&cmd, 0, sizeof(struct mmc_command));
1263 cmd.opcode = MMC_ERASE;
1264 cmd.arg = arg;
1265 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1266 mmc_set_erase_timeout(card, &cmd, arg, qty);
1267 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1268 if (err) {
1269 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1270 err, cmd.resp[0]);
1271 err = -EIO;
1272 goto out;
1273 }
1274
1275 if (mmc_host_is_spi(card->host))
1276 goto out;
1277
1278 do {
1279 memset(&cmd, 0, sizeof(struct mmc_command));
1280 cmd.opcode = MMC_SEND_STATUS;
1281 cmd.arg = card->rca << 16;
1282 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1283 /* Do not retry else we can't see errors */
1284 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1285 if (err || (cmd.resp[0] & 0xFDF92000)) {
1286 printk(KERN_ERR "error %d requesting status %#x\n",
1287 err, cmd.resp[0]);
1288 err = -EIO;
1289 goto out;
1290 }
1291 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1292 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1293out:
1294 return err;
1295}
1296
1297/**
1298 * mmc_erase - erase sectors.
1299 * @card: card to erase
1300 * @from: first sector to erase
1301 * @nr: number of sectors to erase
1302 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1303 *
1304 * Caller must claim host before calling this function.
1305 */
1306int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1307 unsigned int arg)
1308{
1309 unsigned int rem, to = from + nr;
1310
1311 if (!(card->host->caps & MMC_CAP_ERASE) ||
1312 !(card->csd.cmdclass & CCC_ERASE))
1313 return -EOPNOTSUPP;
1314
1315 if (!card->erase_size)
1316 return -EOPNOTSUPP;
1317
1318 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1319 return -EOPNOTSUPP;
1320
1321 if ((arg & MMC_SECURE_ARGS) &&
1322 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1323 return -EOPNOTSUPP;
1324
1325 if ((arg & MMC_TRIM_ARGS) &&
1326 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1327 return -EOPNOTSUPP;
1328
1329 if (arg == MMC_SECURE_ERASE_ARG) {
1330 if (from % card->erase_size || nr % card->erase_size)
1331 return -EINVAL;
1332 }
1333
1334 if (arg == MMC_ERASE_ARG) {
1335 rem = from % card->erase_size;
1336 if (rem) {
1337 rem = card->erase_size - rem;
1338 from += rem;
1339 if (nr > rem)
1340 nr -= rem;
1341 else
1342 return 0;
1343 }
1344 rem = nr % card->erase_size;
1345 if (rem)
1346 nr -= rem;
1347 }
1348
1349 if (nr == 0)
1350 return 0;
1351
1352 to = from + nr;
1353
1354 if (to <= from)
1355 return -EINVAL;
1356
1357 /* 'from' and 'to' are inclusive */
1358 to -= 1;
1359
1360 return mmc_do_erase(card, from, to, arg);
1361}
1362EXPORT_SYMBOL(mmc_erase);
1363
1364int mmc_can_erase(struct mmc_card *card)
1365{
1366 if ((card->host->caps & MMC_CAP_ERASE) &&
1367 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1368 return 1;
1369 return 0;
1370}
1371EXPORT_SYMBOL(mmc_can_erase);
1372
1373int mmc_can_trim(struct mmc_card *card)
1374{
1375 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1376 return 1;
1377 return 0;
1378}
1379EXPORT_SYMBOL(mmc_can_trim);
1380
1381int mmc_can_secure_erase_trim(struct mmc_card *card)
1382{
1383 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1384 return 1;
1385 return 0;
1386}
1387EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1388
1389int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1390 unsigned int nr)
1391{
1392 if (!card->erase_size)
1393 return 0;
1394 if (from % card->erase_size || nr % card->erase_size)
1395 return 0;
1396 return 1;
1397}
1398EXPORT_SYMBOL(mmc_erase_group_aligned);
1053 1399
1054void mmc_rescan(struct work_struct *work) 1400void mmc_rescan(struct work_struct *work)
1055{ 1401{
@@ -1057,6 +1403,17 @@ void mmc_rescan(struct work_struct *work)
1057 container_of(work, struct mmc_host, detect.work); 1403 container_of(work, struct mmc_host, detect.work);
1058 u32 ocr; 1404 u32 ocr;
1059 int err; 1405 int err;
1406 unsigned long flags;
1407
1408 spin_lock_irqsave(&host->lock, flags);
1409
1410 if (host->rescan_disable) {
1411 spin_unlock_irqrestore(&host->lock, flags);
1412 return;
1413 }
1414
1415 spin_unlock_irqrestore(&host->lock, flags);
1416
1060 1417
1061 mmc_bus_get(host); 1418 mmc_bus_get(host);
1062 1419
@@ -1099,8 +1456,15 @@ void mmc_rescan(struct work_struct *work)
1099 */ 1456 */
1100 err = mmc_send_io_op_cond(host, 0, &ocr); 1457 err = mmc_send_io_op_cond(host, 0, &ocr);
1101 if (!err) { 1458 if (!err) {
1102 if (mmc_attach_sdio(host, ocr)) 1459 if (mmc_attach_sdio(host, ocr)) {
1103 mmc_power_off(host); 1460 mmc_claim_host(host);
1461 /* try SDMEM (but not MMC) even if SDIO is broken */
1462 if (mmc_send_app_op_cond(host, 0, &ocr))
1463 goto out_fail;
1464
1465 if (mmc_attach_sd(host, ocr))
1466 mmc_power_off(host);
1467 }
1104 goto out; 1468 goto out;
1105 } 1469 }
1106 1470
@@ -1124,6 +1488,7 @@ void mmc_rescan(struct work_struct *work)
1124 goto out; 1488 goto out;
1125 } 1489 }
1126 1490
1491out_fail:
1127 mmc_release_host(host); 1492 mmc_release_host(host);
1128 mmc_power_off(host); 1493 mmc_power_off(host);
1129 1494
@@ -1266,19 +1631,6 @@ int mmc_suspend_host(struct mmc_host *host)
1266 if (host->bus_ops && !host->bus_dead) { 1631 if (host->bus_ops && !host->bus_dead) {
1267 if (host->bus_ops->suspend) 1632 if (host->bus_ops->suspend)
1268 err = host->bus_ops->suspend(host); 1633 err = host->bus_ops->suspend(host);
1269 if (err == -ENOSYS || !host->bus_ops->resume) {
1270 /*
1271 * We simply "remove" the card in this case.
1272 * It will be redetected on resume.
1273 */
1274 if (host->bus_ops->remove)
1275 host->bus_ops->remove(host);
1276 mmc_claim_host(host);
1277 mmc_detach_bus(host);
1278 mmc_release_host(host);
1279 host->pm_flags = 0;
1280 err = 0;
1281 }
1282 } 1634 }
1283 mmc_bus_put(host); 1635 mmc_bus_put(host);
1284 1636
@@ -1310,28 +1662,61 @@ int mmc_resume_host(struct mmc_host *host)
1310 printk(KERN_WARNING "%s: error %d during resume " 1662 printk(KERN_WARNING "%s: error %d during resume "
1311 "(card was removed?)\n", 1663 "(card was removed?)\n",
1312 mmc_hostname(host), err); 1664 mmc_hostname(host), err);
1313 if (host->bus_ops->remove)
1314 host->bus_ops->remove(host);
1315 mmc_claim_host(host);
1316 mmc_detach_bus(host);
1317 mmc_release_host(host);
1318 /* no need to bother upper layers */
1319 err = 0; 1665 err = 0;
1320 } 1666 }
1321 } 1667 }
1322 mmc_bus_put(host); 1668 mmc_bus_put(host);
1323 1669
1324 /*
1325 * We add a slight delay here so that resume can progress
1326 * in parallel.
1327 */
1328 mmc_detect_change(host, 1);
1329
1330 return err; 1670 return err;
1331} 1671}
1332
1333EXPORT_SYMBOL(mmc_resume_host); 1672EXPORT_SYMBOL(mmc_resume_host);
1334 1673
1674/* Do the card removal on suspend if card is assumed removeable
1675 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1676 to sync the card.
1677*/
1678int mmc_pm_notify(struct notifier_block *notify_block,
1679 unsigned long mode, void *unused)
1680{
1681 struct mmc_host *host = container_of(
1682 notify_block, struct mmc_host, pm_notify);
1683 unsigned long flags;
1684
1685
1686 switch (mode) {
1687 case PM_HIBERNATION_PREPARE:
1688 case PM_SUSPEND_PREPARE:
1689
1690 spin_lock_irqsave(&host->lock, flags);
1691 host->rescan_disable = 1;
1692 spin_unlock_irqrestore(&host->lock, flags);
1693 cancel_delayed_work_sync(&host->detect);
1694
1695 if (!host->bus_ops || host->bus_ops->suspend)
1696 break;
1697
1698 mmc_claim_host(host);
1699
1700 if (host->bus_ops->remove)
1701 host->bus_ops->remove(host);
1702
1703 mmc_detach_bus(host);
1704 mmc_release_host(host);
1705 host->pm_flags = 0;
1706 break;
1707
1708 case PM_POST_SUSPEND:
1709 case PM_POST_HIBERNATION:
1710
1711 spin_lock_irqsave(&host->lock, flags);
1712 host->rescan_disable = 0;
1713 spin_unlock_irqrestore(&host->lock, flags);
1714 mmc_detect_change(host, 0);
1715
1716 }
1717
1718 return 0;
1719}
1335#endif 1720#endif
1336 1721
1337static int __init mmc_init(void) 1722static int __init mmc_init(void)
diff --git a/drivers/mmc/core/core.h b/drivers/mmc/core/core.h
index a811c52a1659..9d9eef50e5d1 100644
--- a/drivers/mmc/core/core.h
+++ b/drivers/mmc/core/core.h
@@ -29,6 +29,8 @@ struct mmc_bus_ops {
29void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops); 29void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops);
30void mmc_detach_bus(struct mmc_host *host); 30void mmc_detach_bus(struct mmc_host *host);
31 31
32void mmc_init_erase(struct mmc_card *card);
33
32void mmc_set_chip_select(struct mmc_host *host, int mode); 34void mmc_set_chip_select(struct mmc_host *host, int mode);
33void mmc_set_clock(struct mmc_host *host, unsigned int hz); 35void mmc_set_clock(struct mmc_host *host, unsigned int hz);
34void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode); 36void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode);
diff --git a/drivers/mmc/core/host.c b/drivers/mmc/core/host.c
index 47353909e345..0efe631e50ca 100644
--- a/drivers/mmc/core/host.c
+++ b/drivers/mmc/core/host.c
@@ -17,6 +17,7 @@
17#include <linux/pagemap.h> 17#include <linux/pagemap.h>
18#include <linux/leds.h> 18#include <linux/leds.h>
19#include <linux/slab.h> 19#include <linux/slab.h>
20#include <linux/suspend.h>
20 21
21#include <linux/mmc/host.h> 22#include <linux/mmc/host.h>
22 23
@@ -85,6 +86,7 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
85 init_waitqueue_head(&host->wq); 86 init_waitqueue_head(&host->wq);
86 INIT_DELAYED_WORK(&host->detect, mmc_rescan); 87 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
87 INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable); 88 INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);
89 host->pm_notify.notifier_call = mmc_pm_notify;
88 90
89 /* 91 /*
90 * By default, hosts do not support SGIO or large requests. 92 * By default, hosts do not support SGIO or large requests.
@@ -133,6 +135,7 @@ int mmc_add_host(struct mmc_host *host)
133#endif 135#endif
134 136
135 mmc_start_host(host); 137 mmc_start_host(host);
138 register_pm_notifier(&host->pm_notify);
136 139
137 return 0; 140 return 0;
138} 141}
@@ -149,6 +152,7 @@ EXPORT_SYMBOL(mmc_add_host);
149 */ 152 */
150void mmc_remove_host(struct mmc_host *host) 153void mmc_remove_host(struct mmc_host *host)
151{ 154{
155 unregister_pm_notifier(&host->pm_notify);
152 mmc_stop_host(host); 156 mmc_stop_host(host);
153 157
154#ifdef CONFIG_DEBUG_FS 158#ifdef CONFIG_DEBUG_FS
diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
index 89f7a25b7ac1..6909a54c39be 100644
--- a/drivers/mmc/core/mmc.c
+++ b/drivers/mmc/core/mmc.c
@@ -108,23 +108,34 @@ static int mmc_decode_cid(struct mmc_card *card)
108 return 0; 108 return 0;
109} 109}
110 110
111static void mmc_set_erase_size(struct mmc_card *card)
112{
113 if (card->ext_csd.erase_group_def & 1)
114 card->erase_size = card->ext_csd.hc_erase_size;
115 else
116 card->erase_size = card->csd.erase_size;
117
118 mmc_init_erase(card);
119}
120
111/* 121/*
112 * Given a 128-bit response, decode to our card CSD structure. 122 * Given a 128-bit response, decode to our card CSD structure.
113 */ 123 */
114static int mmc_decode_csd(struct mmc_card *card) 124static int mmc_decode_csd(struct mmc_card *card)
115{ 125{
116 struct mmc_csd *csd = &card->csd; 126 struct mmc_csd *csd = &card->csd;
117 unsigned int e, m, csd_struct; 127 unsigned int e, m, a, b;
118 u32 *resp = card->raw_csd; 128 u32 *resp = card->raw_csd;
119 129
120 /* 130 /*
121 * We only understand CSD structure v1.1 and v1.2. 131 * We only understand CSD structure v1.1 and v1.2.
122 * v1.2 has extra information in bits 15, 11 and 10. 132 * v1.2 has extra information in bits 15, 11 and 10.
133 * We also support eMMC v4.4 & v4.41.
123 */ 134 */
124 csd_struct = UNSTUFF_BITS(resp, 126, 2); 135 csd->structure = UNSTUFF_BITS(resp, 126, 2);
125 if (csd_struct != 1 && csd_struct != 2) { 136 if (csd->structure == 0) {
126 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n", 137 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
127 mmc_hostname(card->host), csd_struct); 138 mmc_hostname(card->host), csd->structure);
128 return -EINVAL; 139 return -EINVAL;
129 } 140 }
130 141
@@ -151,6 +162,13 @@ static int mmc_decode_csd(struct mmc_card *card)
151 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 162 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
152 csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 163 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
153 164
165 if (csd->write_blkbits >= 9) {
166 a = UNSTUFF_BITS(resp, 42, 5);
167 b = UNSTUFF_BITS(resp, 37, 5);
168 csd->erase_size = (a + 1) * (b + 1);
169 csd->erase_size <<= csd->write_blkbits - 9;
170 }
171
154 return 0; 172 return 0;
155} 173}
156 174
@@ -207,11 +225,22 @@ static int mmc_read_ext_csd(struct mmc_card *card)
207 goto out; 225 goto out;
208 } 226 }
209 227
228 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
229 if (card->csd.structure == 3) {
230 int ext_csd_struct = ext_csd[EXT_CSD_STRUCTURE];
231 if (ext_csd_struct > 2) {
232 printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
233 "version %d\n", mmc_hostname(card->host),
234 ext_csd_struct);
235 err = -EINVAL;
236 goto out;
237 }
238 }
239
210 card->ext_csd.rev = ext_csd[EXT_CSD_REV]; 240 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
211 if (card->ext_csd.rev > 5) { 241 if (card->ext_csd.rev > 5) {
212 printk(KERN_ERR "%s: unrecognised EXT_CSD structure " 242 printk(KERN_ERR "%s: unrecognised EXT_CSD revision %d\n",
213 "version %d\n", mmc_hostname(card->host), 243 mmc_hostname(card->host), card->ext_csd.rev);
214 card->ext_csd.rev);
215 err = -EINVAL; 244 err = -EINVAL;
216 goto out; 245 goto out;
217 } 246 }
@@ -222,7 +251,9 @@ static int mmc_read_ext_csd(struct mmc_card *card)
222 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | 251 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
223 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | 252 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
224 ext_csd[EXT_CSD_SEC_CNT + 3] << 24; 253 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
225 if (card->ext_csd.sectors) 254
255 /* Cards with density > 2GiB are sector addressed */
256 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
226 mmc_card_set_blockaddr(card); 257 mmc_card_set_blockaddr(card);
227 } 258 }
228 259
@@ -247,8 +278,30 @@ static int mmc_read_ext_csd(struct mmc_card *card)
247 if (sa_shift > 0 && sa_shift <= 0x17) 278 if (sa_shift > 0 && sa_shift <= 0x17)
248 card->ext_csd.sa_timeout = 279 card->ext_csd.sa_timeout =
249 1 << ext_csd[EXT_CSD_S_A_TIMEOUT]; 280 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
281 card->ext_csd.erase_group_def =
282 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
283 card->ext_csd.hc_erase_timeout = 300 *
284 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
285 card->ext_csd.hc_erase_size =
286 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
250 } 287 }
251 288
289 if (card->ext_csd.rev >= 4) {
290 card->ext_csd.sec_trim_mult =
291 ext_csd[EXT_CSD_SEC_TRIM_MULT];
292 card->ext_csd.sec_erase_mult =
293 ext_csd[EXT_CSD_SEC_ERASE_MULT];
294 card->ext_csd.sec_feature_support =
295 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
296 card->ext_csd.trim_timeout = 300 *
297 ext_csd[EXT_CSD_TRIM_MULT];
298 }
299
300 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
301 card->erased_byte = 0xFF;
302 else
303 card->erased_byte = 0x0;
304
252out: 305out:
253 kfree(ext_csd); 306 kfree(ext_csd);
254 307
@@ -260,6 +313,8 @@ MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
260MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], 313MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
261 card->raw_csd[2], card->raw_csd[3]); 314 card->raw_csd[2], card->raw_csd[3]);
262MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); 315MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
316MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
317MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
263MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); 318MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
264MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); 319MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
265MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); 320MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
@@ -271,6 +326,8 @@ static struct attribute *mmc_std_attrs[] = {
271 &dev_attr_cid.attr, 326 &dev_attr_cid.attr,
272 &dev_attr_csd.attr, 327 &dev_attr_csd.attr,
273 &dev_attr_date.attr, 328 &dev_attr_date.attr,
329 &dev_attr_erase_size.attr,
330 &dev_attr_preferred_erase_size.attr,
274 &dev_attr_fwrev.attr, 331 &dev_attr_fwrev.attr,
275 &dev_attr_hwrev.attr, 332 &dev_attr_hwrev.attr,
276 &dev_attr_manfid.attr, 333 &dev_attr_manfid.attr,
@@ -407,6 +464,8 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
407 err = mmc_read_ext_csd(card); 464 err = mmc_read_ext_csd(card);
408 if (err) 465 if (err)
409 goto free_card; 466 goto free_card;
467 /* Erase size depends on CSD and Extended CSD */
468 mmc_set_erase_size(card);
410 } 469 }
411 470
412 /* 471 /*
diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c
index 5eac21df4809..0f5241085557 100644
--- a/drivers/mmc/core/sd.c
+++ b/drivers/mmc/core/sd.c
@@ -59,7 +59,7 @@ static const unsigned int tacc_mant[] = {
59/* 59/*
60 * Given the decoded CSD structure, decode the raw CID to our CID structure. 60 * Given the decoded CSD structure, decode the raw CID to our CID structure.
61 */ 61 */
62static void mmc_decode_cid(struct mmc_card *card) 62void mmc_decode_cid(struct mmc_card *card)
63{ 63{
64 u32 *resp = card->raw_cid; 64 u32 *resp = card->raw_cid;
65 65
@@ -119,6 +119,13 @@ static int mmc_decode_csd(struct mmc_card *card)
119 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 119 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
120 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 120 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
121 csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 121 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
122
123 if (UNSTUFF_BITS(resp, 46, 1)) {
124 csd->erase_size = 1;
125 } else if (csd->write_blkbits >= 9) {
126 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
127 csd->erase_size <<= csd->write_blkbits - 9;
128 }
122 break; 129 break;
123 case 1: 130 case 1:
124 /* 131 /*
@@ -147,6 +154,7 @@ static int mmc_decode_csd(struct mmc_card *card)
147 csd->r2w_factor = 4; /* Unused */ 154 csd->r2w_factor = 4; /* Unused */
148 csd->write_blkbits = 9; 155 csd->write_blkbits = 9;
149 csd->write_partial = 0; 156 csd->write_partial = 0;
157 csd->erase_size = 1;
150 break; 158 break;
151 default: 159 default:
152 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n", 160 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
@@ -154,6 +162,8 @@ static int mmc_decode_csd(struct mmc_card *card)
154 return -EINVAL; 162 return -EINVAL;
155 } 163 }
156 164
165 card->erase_size = csd->erase_size;
166
157 return 0; 167 return 0;
158} 168}
159 169
@@ -179,10 +189,68 @@ static int mmc_decode_scr(struct mmc_card *card)
179 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 189 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
180 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 190 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
181 191
192 if (UNSTUFF_BITS(resp, 55, 1))
193 card->erased_byte = 0xFF;
194 else
195 card->erased_byte = 0x0;
196
182 return 0; 197 return 0;
183} 198}
184 199
185/* 200/*
201 * Fetch and process SD Status register.
202 */
203static int mmc_read_ssr(struct mmc_card *card)
204{
205 unsigned int au, es, et, eo;
206 int err, i;
207 u32 *ssr;
208
209 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
210 printk(KERN_WARNING "%s: card lacks mandatory SD Status "
211 "function.\n", mmc_hostname(card->host));
212 return 0;
213 }
214
215 ssr = kmalloc(64, GFP_KERNEL);
216 if (!ssr)
217 return -ENOMEM;
218
219 err = mmc_app_sd_status(card, ssr);
220 if (err) {
221 printk(KERN_WARNING "%s: problem reading SD Status "
222 "register.\n", mmc_hostname(card->host));
223 err = 0;
224 goto out;
225 }
226
227 for (i = 0; i < 16; i++)
228 ssr[i] = be32_to_cpu(ssr[i]);
229
230 /*
231 * UNSTUFF_BITS only works with four u32s so we have to offset the
232 * bitfield positions accordingly.
233 */
234 au = UNSTUFF_BITS(ssr, 428 - 384, 4);
235 if (au > 0 || au <= 9) {
236 card->ssr.au = 1 << (au + 4);
237 es = UNSTUFF_BITS(ssr, 408 - 384, 16);
238 et = UNSTUFF_BITS(ssr, 402 - 384, 6);
239 eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
240 if (es && et) {
241 card->ssr.erase_timeout = (et * 1000) / es;
242 card->ssr.erase_offset = eo * 1000;
243 }
244 } else {
245 printk(KERN_WARNING "%s: SD Status: Invalid Allocation Unit "
246 "size.\n", mmc_hostname(card->host));
247 }
248out:
249 kfree(ssr);
250 return err;
251}
252
253/*
186 * Fetches and decodes switch information 254 * Fetches and decodes switch information
187 */ 255 */
188static int mmc_read_switch(struct mmc_card *card) 256static int mmc_read_switch(struct mmc_card *card)
@@ -238,7 +306,7 @@ out:
238/* 306/*
239 * Test if the card supports high-speed mode and, if so, switch to it. 307 * Test if the card supports high-speed mode and, if so, switch to it.
240 */ 308 */
241static int mmc_switch_hs(struct mmc_card *card) 309int mmc_sd_switch_hs(struct mmc_card *card)
242{ 310{
243 int err; 311 int err;
244 u8 *status; 312 u8 *status;
@@ -272,9 +340,9 @@ static int mmc_switch_hs(struct mmc_card *card)
272 printk(KERN_WARNING "%s: Problem switching card " 340 printk(KERN_WARNING "%s: Problem switching card "
273 "into high-speed mode!\n", 341 "into high-speed mode!\n",
274 mmc_hostname(card->host)); 342 mmc_hostname(card->host));
343 err = 0;
275 } else { 344 } else {
276 mmc_card_set_highspeed(card); 345 err = 1;
277 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
278 } 346 }
279 347
280out: 348out:
@@ -289,6 +357,8 @@ MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
289 card->raw_csd[2], card->raw_csd[3]); 357 card->raw_csd[2], card->raw_csd[3]);
290MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]); 358MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
291MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); 359MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
360MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
361MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
292MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); 362MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
293MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); 363MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
294MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); 364MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
@@ -302,6 +372,8 @@ static struct attribute *sd_std_attrs[] = {
302 &dev_attr_csd.attr, 372 &dev_attr_csd.attr,
303 &dev_attr_scr.attr, 373 &dev_attr_scr.attr,
304 &dev_attr_date.attr, 374 &dev_attr_date.attr,
375 &dev_attr_erase_size.attr,
376 &dev_attr_preferred_erase_size.attr,
305 &dev_attr_fwrev.attr, 377 &dev_attr_fwrev.attr,
306 &dev_attr_hwrev.attr, 378 &dev_attr_hwrev.attr,
307 &dev_attr_manfid.attr, 379 &dev_attr_manfid.attr,
@@ -320,26 +392,16 @@ static const struct attribute_group *sd_attr_groups[] = {
320 NULL, 392 NULL,
321}; 393};
322 394
323static struct device_type sd_type = { 395struct device_type sd_type = {
324 .groups = sd_attr_groups, 396 .groups = sd_attr_groups,
325}; 397};
326 398
327/* 399/*
328 * Handle the detection and initialisation of a card. 400 * Fetch CID from card.
329 *
330 * In the case of a resume, "oldcard" will contain the card
331 * we're trying to reinitialise.
332 */ 401 */
333static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, 402int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid)
334 struct mmc_card *oldcard)
335{ 403{
336 struct mmc_card *card;
337 int err; 404 int err;
338 u32 cid[4];
339 unsigned int max_dtr;
340
341 BUG_ON(!host);
342 WARN_ON(!host->claimed);
343 405
344 /* 406 /*
345 * Since we're changing the OCR value, we seem to 407 * Since we're changing the OCR value, we seem to
@@ -361,92 +423,67 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
361 423
362 err = mmc_send_app_op_cond(host, ocr, NULL); 424 err = mmc_send_app_op_cond(host, ocr, NULL);
363 if (err) 425 if (err)
364 goto err; 426 return err;
365 427
366 /*
367 * Fetch CID from card.
368 */
369 if (mmc_host_is_spi(host)) 428 if (mmc_host_is_spi(host))
370 err = mmc_send_cid(host, cid); 429 err = mmc_send_cid(host, cid);
371 else 430 else
372 err = mmc_all_send_cid(host, cid); 431 err = mmc_all_send_cid(host, cid);
373 if (err)
374 goto err;
375 432
376 if (oldcard) { 433 return err;
377 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) { 434}
378 err = -ENOENT;
379 goto err;
380 }
381
382 card = oldcard;
383 } else {
384 /*
385 * Allocate card structure.
386 */
387 card = mmc_alloc_card(host, &sd_type);
388 if (IS_ERR(card)) {
389 err = PTR_ERR(card);
390 goto err;
391 }
392 435
393 card->type = MMC_TYPE_SD; 436int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
394 memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); 437{
395 } 438 int err;
396 439
397 /* 440 /*
398 * For native busses: get card RCA and quit open drain mode. 441 * Fetch CSD from card.
399 */ 442 */
400 if (!mmc_host_is_spi(host)) { 443 err = mmc_send_csd(card, card->raw_csd);
401 err = mmc_send_relative_addr(host, &card->rca); 444 if (err)
402 if (err) 445 return err;
403 goto free_card;
404 446
405 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); 447 err = mmc_decode_csd(card);
406 } 448 if (err)
449 return err;
407 450
408 if (!oldcard) { 451 return 0;
452}
453
454int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
455 bool reinit)
456{
457 int err;
458
459 if (!reinit) {
409 /* 460 /*
410 * Fetch CSD from card. 461 * Fetch SCR from card.
411 */ 462 */
412 err = mmc_send_csd(card, card->raw_csd); 463 err = mmc_app_send_scr(card, card->raw_scr);
413 if (err)
414 goto free_card;
415
416 err = mmc_decode_csd(card);
417 if (err) 464 if (err)
418 goto free_card; 465 return err;
419
420 mmc_decode_cid(card);
421 }
422 466
423 /* 467 err = mmc_decode_scr(card);
424 * Select card, as all following commands rely on that.
425 */
426 if (!mmc_host_is_spi(host)) {
427 err = mmc_select_card(card);
428 if (err) 468 if (err)
429 goto free_card; 469 return err;
430 }
431 470
432 if (!oldcard) {
433 /* 471 /*
434 * Fetch SCR from card. 472 * Fetch and process SD Status register.
435 */ 473 */
436 err = mmc_app_send_scr(card, card->raw_scr); 474 err = mmc_read_ssr(card);
437 if (err) 475 if (err)
438 goto free_card; 476 return err;
439 477
440 err = mmc_decode_scr(card); 478 /* Erase init depends on CSD and SSR */
441 if (err < 0) 479 mmc_init_erase(card);
442 goto free_card;
443 480
444 /* 481 /*
445 * Fetch switch information from card. 482 * Fetch switch information from card.
446 */ 483 */
447 err = mmc_read_switch(card); 484 err = mmc_read_switch(card);
448 if (err) 485 if (err)
449 goto free_card; 486 return err;
450 } 487 }
451 488
452 /* 489 /*
@@ -458,20 +495,34 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
458 if (mmc_host_is_spi(host)) { 495 if (mmc_host_is_spi(host)) {
459 err = mmc_spi_set_crc(host, use_spi_crc); 496 err = mmc_spi_set_crc(host, use_spi_crc);
460 if (err) 497 if (err)
461 goto free_card; 498 return err;
462 } 499 }
463 500
464 /* 501 /*
465 * Attempt to change to high-speed (if supported) 502 * Check if read-only switch is active.
466 */ 503 */
467 err = mmc_switch_hs(card); 504 if (!reinit) {
468 if (err) 505 int ro = -1;
469 goto free_card;
470 506
471 /* 507 if (host->ops->get_ro)
472 * Compute bus speed. 508 ro = host->ops->get_ro(host);
473 */ 509
474 max_dtr = (unsigned int)-1; 510 if (ro < 0) {
511 printk(KERN_WARNING "%s: host does not "
512 "support reading read-only "
513 "switch. assuming write-enable.\n",
514 mmc_hostname(host));
515 } else if (ro > 0) {
516 mmc_card_set_readonly(card);
517 }
518 }
519
520 return 0;
521}
522
523unsigned mmc_sd_get_max_clock(struct mmc_card *card)
524{
525 unsigned max_dtr = (unsigned int)-1;
475 526
476 if (mmc_card_highspeed(card)) { 527 if (mmc_card_highspeed(card)) {
477 if (max_dtr > card->sw_caps.hs_max_dtr) 528 if (max_dtr > card->sw_caps.hs_max_dtr)
@@ -480,7 +531,97 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
480 max_dtr = card->csd.max_dtr; 531 max_dtr = card->csd.max_dtr;
481 } 532 }
482 533
483 mmc_set_clock(host, max_dtr); 534 return max_dtr;
535}
536
537void mmc_sd_go_highspeed(struct mmc_card *card)
538{
539 mmc_card_set_highspeed(card);
540 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
541}
542
543/*
544 * Handle the detection and initialisation of a card.
545 *
546 * In the case of a resume, "oldcard" will contain the card
547 * we're trying to reinitialise.
548 */
549static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
550 struct mmc_card *oldcard)
551{
552 struct mmc_card *card;
553 int err;
554 u32 cid[4];
555
556 BUG_ON(!host);
557 WARN_ON(!host->claimed);
558
559 err = mmc_sd_get_cid(host, ocr, cid);
560 if (err)
561 return err;
562
563 if (oldcard) {
564 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
565 return -ENOENT;
566
567 card = oldcard;
568 } else {
569 /*
570 * Allocate card structure.
571 */
572 card = mmc_alloc_card(host, &sd_type);
573 if (IS_ERR(card))
574 return PTR_ERR(card);
575
576 card->type = MMC_TYPE_SD;
577 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
578 }
579
580 /*
581 * For native busses: get card RCA and quit open drain mode.
582 */
583 if (!mmc_host_is_spi(host)) {
584 err = mmc_send_relative_addr(host, &card->rca);
585 if (err)
586 return err;
587
588 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
589 }
590
591 if (!oldcard) {
592 err = mmc_sd_get_csd(host, card);
593 if (err)
594 return err;
595
596 mmc_decode_cid(card);
597 }
598
599 /*
600 * Select card, as all following commands rely on that.
601 */
602 if (!mmc_host_is_spi(host)) {
603 err = mmc_select_card(card);
604 if (err)
605 return err;
606 }
607
608 err = mmc_sd_setup_card(host, card, oldcard != NULL);
609 if (err)
610 goto free_card;
611
612 /*
613 * Attempt to change to high-speed (if supported)
614 */
615 err = mmc_sd_switch_hs(card);
616 if (err > 0)
617 mmc_sd_go_highspeed(card);
618 else if (err)
619 goto free_card;
620
621 /*
622 * Set bus speed.
623 */
624 mmc_set_clock(host, mmc_sd_get_max_clock(card));
484 625
485 /* 626 /*
486 * Switch to wider bus (if supported). 627 * Switch to wider bus (if supported).
@@ -494,30 +635,12 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
494 mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 635 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
495 } 636 }
496 637
497 /* 638 host->card = card;
498 * Check if read-only switch is active.
499 */
500 if (!oldcard) {
501 if (!host->ops->get_ro || host->ops->get_ro(host) < 0) {
502 printk(KERN_WARNING "%s: host does not "
503 "support reading read-only "
504 "switch. assuming write-enable.\n",
505 mmc_hostname(host));
506 } else {
507 if (host->ops->get_ro(host) > 0)
508 mmc_card_set_readonly(card);
509 }
510 }
511
512 if (!oldcard)
513 host->card = card;
514
515 return 0; 639 return 0;
516 640
517free_card: 641free_card:
518 if (!oldcard) 642 if (!oldcard)
519 mmc_remove_card(card); 643 mmc_remove_card(card);
520err:
521 644
522 return err; 645 return err;
523} 646}
diff --git a/drivers/mmc/core/sd.h b/drivers/mmc/core/sd.h
new file mode 100644
index 000000000000..3d8800fa7600
--- /dev/null
+++ b/drivers/mmc/core/sd.h
@@ -0,0 +1,17 @@
1#ifndef _MMC_CORE_SD_H
2#define _MMC_CORE_SD_H
3
4#include <linux/mmc/card.h>
5
6extern struct device_type sd_type;
7
8int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid);
9int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card);
10void mmc_decode_cid(struct mmc_card *card);
11int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
12 bool reinit);
13unsigned mmc_sd_get_max_clock(struct mmc_card *card);
14int mmc_sd_switch_hs(struct mmc_card *card);
15void mmc_sd_go_highspeed(struct mmc_card *card);
16
17#endif
diff --git a/drivers/mmc/core/sd_ops.c b/drivers/mmc/core/sd_ops.c
index 63772e7e7608..797cdb5887fd 100644
--- a/drivers/mmc/core/sd_ops.c
+++ b/drivers/mmc/core/sd_ops.c
@@ -346,3 +346,51 @@ int mmc_sd_switch(struct mmc_card *card, int mode, int group,
346 return 0; 346 return 0;
347} 347}
348 348
349int mmc_app_sd_status(struct mmc_card *card, void *ssr)
350{
351 int err;
352 struct mmc_request mrq;
353 struct mmc_command cmd;
354 struct mmc_data data;
355 struct scatterlist sg;
356
357 BUG_ON(!card);
358 BUG_ON(!card->host);
359 BUG_ON(!ssr);
360
361 /* NOTE: caller guarantees ssr is heap-allocated */
362
363 err = mmc_app_cmd(card->host, card);
364 if (err)
365 return err;
366
367 memset(&mrq, 0, sizeof(struct mmc_request));
368 memset(&cmd, 0, sizeof(struct mmc_command));
369 memset(&data, 0, sizeof(struct mmc_data));
370
371 mrq.cmd = &cmd;
372 mrq.data = &data;
373
374 cmd.opcode = SD_APP_SD_STATUS;
375 cmd.arg = 0;
376 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
377
378 data.blksz = 64;
379 data.blocks = 1;
380 data.flags = MMC_DATA_READ;
381 data.sg = &sg;
382 data.sg_len = 1;
383
384 sg_init_one(&sg, ssr, 64);
385
386 mmc_set_data_timeout(&data, card);
387
388 mmc_wait_for_req(card->host, &mrq);
389
390 if (cmd.error)
391 return cmd.error;
392 if (data.error)
393 return data.error;
394
395 return 0;
396}
diff --git a/drivers/mmc/core/sd_ops.h b/drivers/mmc/core/sd_ops.h
index 9742d8a30664..ffc2305d905f 100644
--- a/drivers/mmc/core/sd_ops.h
+++ b/drivers/mmc/core/sd_ops.h
@@ -19,6 +19,7 @@ int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca);
19int mmc_app_send_scr(struct mmc_card *card, u32 *scr); 19int mmc_app_send_scr(struct mmc_card *card, u32 *scr);
20int mmc_sd_switch(struct mmc_card *card, int mode, int group, 20int mmc_sd_switch(struct mmc_card *card, int mode, int group,
21 u8 value, u8 *resp); 21 u8 value, u8 *resp);
22int mmc_app_sd_status(struct mmc_card *card, void *ssr);
22 23
23#endif 24#endif
24 25
diff --git a/drivers/mmc/core/sdio.c b/drivers/mmc/core/sdio.c
index b9dee28ee7d0..bd2755e8d9a3 100644
--- a/drivers/mmc/core/sdio.c
+++ b/drivers/mmc/core/sdio.c
@@ -18,6 +18,7 @@
18 18
19#include "core.h" 19#include "core.h"
20#include "bus.h" 20#include "bus.h"
21#include "sd.h"
21#include "sdio_bus.h" 22#include "sdio_bus.h"
22#include "mmc_ops.h" 23#include "mmc_ops.h"
23#include "sd_ops.h" 24#include "sd_ops.h"
@@ -62,13 +63,19 @@ static int sdio_init_func(struct mmc_card *card, unsigned int fn)
62 63
63 func->num = fn; 64 func->num = fn;
64 65
65 ret = sdio_read_fbr(func); 66 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
66 if (ret) 67 ret = sdio_read_fbr(func);
67 goto fail; 68 if (ret)
69 goto fail;
68 70
69 ret = sdio_read_func_cis(func); 71 ret = sdio_read_func_cis(func);
70 if (ret) 72 if (ret)
71 goto fail; 73 goto fail;
74 } else {
75 func->vendor = func->card->cis.vendor;
76 func->device = func->card->cis.device;
77 func->max_blksize = func->card->cis.blksize;
78 }
72 79
73 card->sdio_func[fn - 1] = func; 80 card->sdio_func[fn - 1] = func;
74 81
@@ -159,9 +166,7 @@ static int sdio_enable_wide(struct mmc_card *card)
159 if (ret) 166 if (ret)
160 return ret; 167 return ret;
161 168
162 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 169 return 1;
163
164 return 0;
165} 170}
166 171
167/* 172/*
@@ -221,10 +226,34 @@ static int sdio_disable_wide(struct mmc_card *card)
221 return 0; 226 return 0;
222} 227}
223 228
229
230static int sdio_enable_4bit_bus(struct mmc_card *card)
231{
232 int err;
233
234 if (card->type == MMC_TYPE_SDIO)
235 return sdio_enable_wide(card);
236
237 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
238 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
239 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
240 if (err)
241 return err;
242 } else
243 return 0;
244
245 err = sdio_enable_wide(card);
246 if (err <= 0)
247 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
248
249 return err;
250}
251
252
224/* 253/*
225 * Test if the card supports high-speed mode and, if so, switch to it. 254 * Test if the card supports high-speed mode and, if so, switch to it.
226 */ 255 */
227static int sdio_enable_hs(struct mmc_card *card) 256static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
228{ 257{
229 int ret; 258 int ret;
230 u8 speed; 259 u8 speed;
@@ -239,16 +268,56 @@ static int sdio_enable_hs(struct mmc_card *card)
239 if (ret) 268 if (ret)
240 return ret; 269 return ret;
241 270
242 speed |= SDIO_SPEED_EHS; 271 if (enable)
272 speed |= SDIO_SPEED_EHS;
273 else
274 speed &= ~SDIO_SPEED_EHS;
243 275
244 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL); 276 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
245 if (ret) 277 if (ret)
246 return ret; 278 return ret;
247 279
248 mmc_card_set_highspeed(card); 280 return 1;
249 mmc_set_timing(card->host, MMC_TIMING_SD_HS); 281}
250 282
251 return 0; 283/*
284 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
285 */
286static int sdio_enable_hs(struct mmc_card *card)
287{
288 int ret;
289
290 ret = mmc_sdio_switch_hs(card, true);
291 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
292 return ret;
293
294 ret = mmc_sd_switch_hs(card);
295 if (ret <= 0)
296 mmc_sdio_switch_hs(card, false);
297
298 return ret;
299}
300
301static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
302{
303 unsigned max_dtr;
304
305 if (mmc_card_highspeed(card)) {
306 /*
307 * The SDIO specification doesn't mention how
308 * the CIS transfer speed register relates to
309 * high-speed, but it seems that 50 MHz is
310 * mandatory.
311 */
312 max_dtr = 50000000;
313 } else {
314 max_dtr = card->cis.max_dtr;
315 }
316
317 if (card->type == MMC_TYPE_SD_COMBO)
318 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
319
320 return max_dtr;
252} 321}
253 322
254/* 323/*
@@ -293,7 +362,24 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
293 goto err; 362 goto err;
294 } 363 }
295 364
296 card->type = MMC_TYPE_SDIO; 365 err = mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid);
366
367 if (!err) {
368 card->type = MMC_TYPE_SD_COMBO;
369
370 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
371 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
372 mmc_remove_card(card);
373 return -ENOENT;
374 }
375 } else {
376 card->type = MMC_TYPE_SDIO;
377
378 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
379 mmc_remove_card(card);
380 return -ENOENT;
381 }
382 }
297 383
298 /* 384 /*
299 * Call the optional HC's init_card function to handle quirks. 385 * Call the optional HC's init_card function to handle quirks.
@@ -313,6 +399,17 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
313 } 399 }
314 400
315 /* 401 /*
402 * Read CSD, before selecting the card
403 */
404 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
405 err = mmc_sd_get_csd(host, card);
406 if (err)
407 return err;
408
409 mmc_decode_cid(card);
410 }
411
412 /*
316 * Select card, as all following commands rely on that. 413 * Select card, as all following commands rely on that.
317 */ 414 */
318 if (!powered_resume && !mmc_host_is_spi(host)) { 415 if (!powered_resume && !mmc_host_is_spi(host)) {
@@ -321,6 +418,23 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
321 goto remove; 418 goto remove;
322 } 419 }
323 420
421 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
422 /*
423 * This is non-standard SDIO device, meaning it doesn't
424 * have any CIA (Common I/O area) registers present.
425 * It's host's responsibility to fill cccr and cis
426 * structures in init_card().
427 */
428 mmc_set_clock(host, card->cis.max_dtr);
429
430 if (card->cccr.high_speed) {
431 mmc_card_set_highspeed(card);
432 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
433 }
434
435 goto finish;
436 }
437
324 /* 438 /*
325 * Read the common registers. 439 * Read the common registers.
326 */ 440 */
@@ -339,43 +453,57 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
339 int same = (card->cis.vendor == oldcard->cis.vendor && 453 int same = (card->cis.vendor == oldcard->cis.vendor &&
340 card->cis.device == oldcard->cis.device); 454 card->cis.device == oldcard->cis.device);
341 mmc_remove_card(card); 455 mmc_remove_card(card);
342 if (!same) { 456 if (!same)
343 err = -ENOENT; 457 return -ENOENT;
344 goto err; 458
345 }
346 card = oldcard; 459 card = oldcard;
347 return 0; 460 return 0;
348 } 461 }
349 462
463 if (card->type == MMC_TYPE_SD_COMBO) {
464 err = mmc_sd_setup_card(host, card, oldcard != NULL);
465 /* handle as SDIO-only card if memory init failed */
466 if (err) {
467 mmc_go_idle(host);
468 if (mmc_host_is_spi(host))
469 /* should not fail, as it worked previously */
470 mmc_spi_set_crc(host, use_spi_crc);
471 card->type = MMC_TYPE_SDIO;
472 } else
473 card->dev.type = &sd_type;
474 }
475
476 /*
477 * If needed, disconnect card detection pull-up resistor.
478 */
479 err = sdio_disable_cd(card);
480 if (err)
481 goto remove;
482
350 /* 483 /*
351 * Switch to high-speed (if supported). 484 * Switch to high-speed (if supported).
352 */ 485 */
353 err = sdio_enable_hs(card); 486 err = sdio_enable_hs(card);
354 if (err) 487 if (err > 0)
488 mmc_sd_go_highspeed(card);
489 else if (err)
355 goto remove; 490 goto remove;
356 491
357 /* 492 /*
358 * Change to the card's maximum speed. 493 * Change to the card's maximum speed.
359 */ 494 */
360 if (mmc_card_highspeed(card)) { 495 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
361 /*
362 * The SDIO specification doesn't mention how
363 * the CIS transfer speed register relates to
364 * high-speed, but it seems that 50 MHz is
365 * mandatory.
366 */
367 mmc_set_clock(host, 50000000);
368 } else {
369 mmc_set_clock(host, card->cis.max_dtr);
370 }
371 496
372 /* 497 /*
373 * Switch to wider bus (if supported). 498 * Switch to wider bus (if supported).
374 */ 499 */
375 err = sdio_enable_wide(card); 500 err = sdio_enable_4bit_bus(card);
376 if (err) 501 if (err > 0)
502 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
503 else if (err)
377 goto remove; 504 goto remove;
378 505
506finish:
379 if (!oldcard) 507 if (!oldcard)
380 host->card = card; 508 host->card = card;
381 return 0; 509 return 0;
@@ -487,9 +615,14 @@ static int mmc_sdio_resume(struct mmc_host *host)
487 mmc_claim_host(host); 615 mmc_claim_host(host);
488 err = mmc_sdio_init_card(host, host->ocr, host->card, 616 err = mmc_sdio_init_card(host, host->ocr, host->card,
489 (host->pm_flags & MMC_PM_KEEP_POWER)); 617 (host->pm_flags & MMC_PM_KEEP_POWER));
490 if (!err) 618 if (!err) {
491 /* We may have switched to 1-bit mode during suspend. */ 619 /* We may have switched to 1-bit mode during suspend. */
492 err = sdio_enable_wide(host->card); 620 err = sdio_enable_4bit_bus(host->card);
621 if (err > 0) {
622 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
623 err = 0;
624 }
625 }
493 if (!err && host->sdio_irqs) 626 if (!err && host->sdio_irqs)
494 mmc_signal_sdio_irq(host); 627 mmc_signal_sdio_irq(host);
495 mmc_release_host(host); 628 mmc_release_host(host);
@@ -574,13 +707,6 @@ int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
574 card->sdio_funcs = 0; 707 card->sdio_funcs = 0;
575 708
576 /* 709 /*
577 * If needed, disconnect card detection pull-up resistor.
578 */
579 err = sdio_disable_cd(card);
580 if (err)
581 goto remove;
582
583 /*
584 * Initialize (but don't add) all present functions. 710 * Initialize (but don't add) all present functions.
585 */ 711 */
586 for (i = 0; i < funcs; i++, card->sdio_funcs++) { 712 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
diff --git a/drivers/mmc/host/Kconfig b/drivers/mmc/host/Kconfig
index f06d06e7fdfa..c997474c649f 100644
--- a/drivers/mmc/host/Kconfig
+++ b/drivers/mmc/host/Kconfig
@@ -121,6 +121,15 @@ config MMC_SDHCI_PLTFM
121 121
122 If unsure, say N. 122 If unsure, say N.
123 123
124config MMC_SDHCI_CNS3XXX
125 bool "SDHCI support on the Cavium Networks CNS3xxx SoC"
126 depends on ARCH_CNS3XXX
127 depends on MMC_SDHCI_PLTFM
128 help
129 This selects the SDHCI support for CNS3xxx System-on-Chip devices.
130
131 If unsure, say N.
132
124config MMC_SDHCI_S3C 133config MMC_SDHCI_S3C
125 tristate "SDHCI support on Samsung S3C SoC" 134 tristate "SDHCI support on Samsung S3C SoC"
126 depends on MMC_SDHCI && (PLAT_S3C24XX || PLAT_S3C64XX) 135 depends on MMC_SDHCI && (PLAT_S3C24XX || PLAT_S3C64XX)
@@ -432,3 +441,12 @@ config MMC_SH_MMCIF
432 This selects the MMC Host Interface controler (MMCIF). 441 This selects the MMC Host Interface controler (MMCIF).
433 442
434 This driver supports MMCIF in sh7724/sh7757/sh7372. 443 This driver supports MMCIF in sh7724/sh7757/sh7372.
444
445config MMC_JZ4740
446 tristate "JZ4740 SD/Multimedia Card Interface support"
447 depends on MACH_JZ4740
448 help
449 This selects support for the SD/MMC controller on Ingenic JZ4740
450 SoCs.
451 If you have a board based on such a SoC and with a SD/MMC slot,
452 say Y or M here.
diff --git a/drivers/mmc/host/Makefile b/drivers/mmc/host/Makefile
index e30c2ee48894..fe0ba4e2b8b0 100644
--- a/drivers/mmc/host/Makefile
+++ b/drivers/mmc/host/Makefile
@@ -12,7 +12,6 @@ obj-$(CONFIG_MMC_IMX) += imxmmc.o
12obj-$(CONFIG_MMC_MXC) += mxcmmc.o 12obj-$(CONFIG_MMC_MXC) += mxcmmc.o
13obj-$(CONFIG_MMC_SDHCI) += sdhci.o 13obj-$(CONFIG_MMC_SDHCI) += sdhci.o
14obj-$(CONFIG_MMC_SDHCI_PCI) += sdhci-pci.o 14obj-$(CONFIG_MMC_SDHCI_PCI) += sdhci-pci.o
15obj-$(CONFIG_MMC_SDHCI_PLTFM) += sdhci-pltfm.o
16obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c.o 15obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c.o
17obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o 16obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
18obj-$(CONFIG_MMC_WBSD) += wbsd.o 17obj-$(CONFIG_MMC_WBSD) += wbsd.o
@@ -36,6 +35,11 @@ obj-$(CONFIG_MMC_CB710) += cb710-mmc.o
36obj-$(CONFIG_MMC_VIA_SDMMC) += via-sdmmc.o 35obj-$(CONFIG_MMC_VIA_SDMMC) += via-sdmmc.o
37obj-$(CONFIG_SDH_BFIN) += bfin_sdh.o 36obj-$(CONFIG_SDH_BFIN) += bfin_sdh.o
38obj-$(CONFIG_MMC_SH_MMCIF) += sh_mmcif.o 37obj-$(CONFIG_MMC_SH_MMCIF) += sh_mmcif.o
38obj-$(CONFIG_MMC_JZ4740) += jz4740_mmc.o
39
40obj-$(CONFIG_MMC_SDHCI_PLTFM) += sdhci-platform.o
41sdhci-platform-y := sdhci-pltfm.o
42sdhci-platform-$(CONFIG_MMC_SDHCI_CNS3XXX) += sdhci-cns3xxx.o
39 43
40obj-$(CONFIG_MMC_SDHCI_OF) += sdhci-of.o 44obj-$(CONFIG_MMC_SDHCI_OF) += sdhci-of.o
41sdhci-of-y := sdhci-of-core.o 45sdhci-of-y := sdhci-of-core.o
diff --git a/drivers/mmc/host/jz4740_mmc.c b/drivers/mmc/host/jz4740_mmc.c
new file mode 100644
index 000000000000..ad4f9870e3ca
--- /dev/null
+++ b/drivers/mmc/host/jz4740_mmc.c
@@ -0,0 +1,1029 @@
1/*
2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4740 SD/MMC controller driver
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
13 *
14 */
15
16#include <linux/mmc/host.h>
17#include <linux/io.h>
18#include <linux/irq.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/delay.h>
23#include <linux/scatterlist.h>
24#include <linux/clk.h>
25
26#include <linux/bitops.h>
27#include <linux/gpio.h>
28#include <asm/mach-jz4740/gpio.h>
29#include <asm/cacheflush.h>
30#include <linux/dma-mapping.h>
31
32#include <asm/mach-jz4740/jz4740_mmc.h>
33
34#define JZ_REG_MMC_STRPCL 0x00
35#define JZ_REG_MMC_STATUS 0x04
36#define JZ_REG_MMC_CLKRT 0x08
37#define JZ_REG_MMC_CMDAT 0x0C
38#define JZ_REG_MMC_RESTO 0x10
39#define JZ_REG_MMC_RDTO 0x14
40#define JZ_REG_MMC_BLKLEN 0x18
41#define JZ_REG_MMC_NOB 0x1C
42#define JZ_REG_MMC_SNOB 0x20
43#define JZ_REG_MMC_IMASK 0x24
44#define JZ_REG_MMC_IREG 0x28
45#define JZ_REG_MMC_CMD 0x2C
46#define JZ_REG_MMC_ARG 0x30
47#define JZ_REG_MMC_RESP_FIFO 0x34
48#define JZ_REG_MMC_RXFIFO 0x38
49#define JZ_REG_MMC_TXFIFO 0x3C
50
51#define JZ_MMC_STRPCL_EXIT_MULTIPLE BIT(7)
52#define JZ_MMC_STRPCL_EXIT_TRANSFER BIT(6)
53#define JZ_MMC_STRPCL_START_READWAIT BIT(5)
54#define JZ_MMC_STRPCL_STOP_READWAIT BIT(4)
55#define JZ_MMC_STRPCL_RESET BIT(3)
56#define JZ_MMC_STRPCL_START_OP BIT(2)
57#define JZ_MMC_STRPCL_CLOCK_CONTROL (BIT(1) | BIT(0))
58#define JZ_MMC_STRPCL_CLOCK_STOP BIT(0)
59#define JZ_MMC_STRPCL_CLOCK_START BIT(1)
60
61
62#define JZ_MMC_STATUS_IS_RESETTING BIT(15)
63#define JZ_MMC_STATUS_SDIO_INT_ACTIVE BIT(14)
64#define JZ_MMC_STATUS_PRG_DONE BIT(13)
65#define JZ_MMC_STATUS_DATA_TRAN_DONE BIT(12)
66#define JZ_MMC_STATUS_END_CMD_RES BIT(11)
67#define JZ_MMC_STATUS_DATA_FIFO_AFULL BIT(10)
68#define JZ_MMC_STATUS_IS_READWAIT BIT(9)
69#define JZ_MMC_STATUS_CLK_EN BIT(8)
70#define JZ_MMC_STATUS_DATA_FIFO_FULL BIT(7)
71#define JZ_MMC_STATUS_DATA_FIFO_EMPTY BIT(6)
72#define JZ_MMC_STATUS_CRC_RES_ERR BIT(5)
73#define JZ_MMC_STATUS_CRC_READ_ERROR BIT(4)
74#define JZ_MMC_STATUS_TIMEOUT_WRITE BIT(3)
75#define JZ_MMC_STATUS_CRC_WRITE_ERROR BIT(2)
76#define JZ_MMC_STATUS_TIMEOUT_RES BIT(1)
77#define JZ_MMC_STATUS_TIMEOUT_READ BIT(0)
78
79#define JZ_MMC_STATUS_READ_ERROR_MASK (BIT(4) | BIT(0))
80#define JZ_MMC_STATUS_WRITE_ERROR_MASK (BIT(3) | BIT(2))
81
82
83#define JZ_MMC_CMDAT_IO_ABORT BIT(11)
84#define JZ_MMC_CMDAT_BUS_WIDTH_4BIT BIT(10)
85#define JZ_MMC_CMDAT_DMA_EN BIT(8)
86#define JZ_MMC_CMDAT_INIT BIT(7)
87#define JZ_MMC_CMDAT_BUSY BIT(6)
88#define JZ_MMC_CMDAT_STREAM BIT(5)
89#define JZ_MMC_CMDAT_WRITE BIT(4)
90#define JZ_MMC_CMDAT_DATA_EN BIT(3)
91#define JZ_MMC_CMDAT_RESPONSE_FORMAT (BIT(2) | BIT(1) | BIT(0))
92#define JZ_MMC_CMDAT_RSP_R1 1
93#define JZ_MMC_CMDAT_RSP_R2 2
94#define JZ_MMC_CMDAT_RSP_R3 3
95
96#define JZ_MMC_IRQ_SDIO BIT(7)
97#define JZ_MMC_IRQ_TXFIFO_WR_REQ BIT(6)
98#define JZ_MMC_IRQ_RXFIFO_RD_REQ BIT(5)
99#define JZ_MMC_IRQ_END_CMD_RES BIT(2)
100#define JZ_MMC_IRQ_PRG_DONE BIT(1)
101#define JZ_MMC_IRQ_DATA_TRAN_DONE BIT(0)
102
103
104#define JZ_MMC_CLK_RATE 24000000
105
106enum jz4740_mmc_state {
107 JZ4740_MMC_STATE_READ_RESPONSE,
108 JZ4740_MMC_STATE_TRANSFER_DATA,
109 JZ4740_MMC_STATE_SEND_STOP,
110 JZ4740_MMC_STATE_DONE,
111};
112
113struct jz4740_mmc_host {
114 struct mmc_host *mmc;
115 struct platform_device *pdev;
116 struct jz4740_mmc_platform_data *pdata;
117 struct clk *clk;
118
119 int irq;
120 int card_detect_irq;
121
122 struct resource *mem;
123 void __iomem *base;
124 struct mmc_request *req;
125 struct mmc_command *cmd;
126
127 unsigned long waiting;
128
129 uint32_t cmdat;
130
131 uint16_t irq_mask;
132
133 spinlock_t lock;
134
135 struct timer_list timeout_timer;
136 struct sg_mapping_iter miter;
137 enum jz4740_mmc_state state;
138};
139
140static void jz4740_mmc_set_irq_enabled(struct jz4740_mmc_host *host,
141 unsigned int irq, bool enabled)
142{
143 unsigned long flags;
144
145 spin_lock_irqsave(&host->lock, flags);
146 if (enabled)
147 host->irq_mask &= ~irq;
148 else
149 host->irq_mask |= irq;
150 spin_unlock_irqrestore(&host->lock, flags);
151
152 writew(host->irq_mask, host->base + JZ_REG_MMC_IMASK);
153}
154
155static void jz4740_mmc_clock_enable(struct jz4740_mmc_host *host,
156 bool start_transfer)
157{
158 uint16_t val = JZ_MMC_STRPCL_CLOCK_START;
159
160 if (start_transfer)
161 val |= JZ_MMC_STRPCL_START_OP;
162
163 writew(val, host->base + JZ_REG_MMC_STRPCL);
164}
165
166static void jz4740_mmc_clock_disable(struct jz4740_mmc_host *host)
167{
168 uint32_t status;
169 unsigned int timeout = 1000;
170
171 writew(JZ_MMC_STRPCL_CLOCK_STOP, host->base + JZ_REG_MMC_STRPCL);
172 do {
173 status = readl(host->base + JZ_REG_MMC_STATUS);
174 } while (status & JZ_MMC_STATUS_CLK_EN && --timeout);
175}
176
177static void jz4740_mmc_reset(struct jz4740_mmc_host *host)
178{
179 uint32_t status;
180 unsigned int timeout = 1000;
181
182 writew(JZ_MMC_STRPCL_RESET, host->base + JZ_REG_MMC_STRPCL);
183 udelay(10);
184 do {
185 status = readl(host->base + JZ_REG_MMC_STATUS);
186 } while (status & JZ_MMC_STATUS_IS_RESETTING && --timeout);
187}
188
189static void jz4740_mmc_request_done(struct jz4740_mmc_host *host)
190{
191 struct mmc_request *req;
192
193 req = host->req;
194 host->req = NULL;
195
196 mmc_request_done(host->mmc, req);
197}
198
199static unsigned int jz4740_mmc_poll_irq(struct jz4740_mmc_host *host,
200 unsigned int irq)
201{
202 unsigned int timeout = 0x800;
203 uint16_t status;
204
205 do {
206 status = readw(host->base + JZ_REG_MMC_IREG);
207 } while (!(status & irq) && --timeout);
208
209 if (timeout == 0) {
210 set_bit(0, &host->waiting);
211 mod_timer(&host->timeout_timer, jiffies + 5*HZ);
212 jz4740_mmc_set_irq_enabled(host, irq, true);
213 return true;
214 }
215
216 return false;
217}
218
219static void jz4740_mmc_transfer_check_state(struct jz4740_mmc_host *host,
220 struct mmc_data *data)
221{
222 int status;
223
224 status = readl(host->base + JZ_REG_MMC_STATUS);
225 if (status & JZ_MMC_STATUS_WRITE_ERROR_MASK) {
226 if (status & (JZ_MMC_STATUS_TIMEOUT_WRITE)) {
227 host->req->cmd->error = -ETIMEDOUT;
228 data->error = -ETIMEDOUT;
229 } else {
230 host->req->cmd->error = -EIO;
231 data->error = -EIO;
232 }
233 }
234}
235
236static bool jz4740_mmc_write_data(struct jz4740_mmc_host *host,
237 struct mmc_data *data)
238{
239 struct sg_mapping_iter *miter = &host->miter;
240 void __iomem *fifo_addr = host->base + JZ_REG_MMC_TXFIFO;
241 uint32_t *buf;
242 bool timeout;
243 size_t i, j;
244
245 while (sg_miter_next(miter)) {
246 buf = miter->addr;
247 i = miter->length / 4;
248 j = i / 8;
249 i = i & 0x7;
250 while (j) {
251 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
252 if (unlikely(timeout))
253 goto poll_timeout;
254
255 writel(buf[0], fifo_addr);
256 writel(buf[1], fifo_addr);
257 writel(buf[2], fifo_addr);
258 writel(buf[3], fifo_addr);
259 writel(buf[4], fifo_addr);
260 writel(buf[5], fifo_addr);
261 writel(buf[6], fifo_addr);
262 writel(buf[7], fifo_addr);
263 buf += 8;
264 --j;
265 }
266 if (unlikely(i)) {
267 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
268 if (unlikely(timeout))
269 goto poll_timeout;
270
271 while (i) {
272 writel(*buf, fifo_addr);
273 ++buf;
274 --i;
275 }
276 }
277 data->bytes_xfered += miter->length;
278 }
279 sg_miter_stop(miter);
280
281 return false;
282
283poll_timeout:
284 miter->consumed = (void *)buf - miter->addr;
285 data->bytes_xfered += miter->consumed;
286 sg_miter_stop(miter);
287
288 return true;
289}
290
291static bool jz4740_mmc_read_data(struct jz4740_mmc_host *host,
292 struct mmc_data *data)
293{
294 struct sg_mapping_iter *miter = &host->miter;
295 void __iomem *fifo_addr = host->base + JZ_REG_MMC_RXFIFO;
296 uint32_t *buf;
297 uint32_t d;
298 uint16_t status;
299 size_t i, j;
300 unsigned int timeout;
301
302 while (sg_miter_next(miter)) {
303 buf = miter->addr;
304 i = miter->length;
305 j = i / 32;
306 i = i & 0x1f;
307 while (j) {
308 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
309 if (unlikely(timeout))
310 goto poll_timeout;
311
312 buf[0] = readl(fifo_addr);
313 buf[1] = readl(fifo_addr);
314 buf[2] = readl(fifo_addr);
315 buf[3] = readl(fifo_addr);
316 buf[4] = readl(fifo_addr);
317 buf[5] = readl(fifo_addr);
318 buf[6] = readl(fifo_addr);
319 buf[7] = readl(fifo_addr);
320
321 buf += 8;
322 --j;
323 }
324
325 if (unlikely(i)) {
326 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
327 if (unlikely(timeout))
328 goto poll_timeout;
329
330 while (i >= 4) {
331 *buf++ = readl(fifo_addr);
332 i -= 4;
333 }
334 if (unlikely(i > 0)) {
335 d = readl(fifo_addr);
336 memcpy(buf, &d, i);
337 }
338 }
339 data->bytes_xfered += miter->length;
340
341 /* This can go away once MIPS implements
342 * flush_kernel_dcache_page */
343 flush_dcache_page(miter->page);
344 }
345 sg_miter_stop(miter);
346
347 /* For whatever reason there is sometime one word more in the fifo then
348 * requested */
349 timeout = 1000;
350 status = readl(host->base + JZ_REG_MMC_STATUS);
351 while (!(status & JZ_MMC_STATUS_DATA_FIFO_EMPTY) && --timeout) {
352 d = readl(fifo_addr);
353 status = readl(host->base + JZ_REG_MMC_STATUS);
354 }
355
356 return false;
357
358poll_timeout:
359 miter->consumed = (void *)buf - miter->addr;
360 data->bytes_xfered += miter->consumed;
361 sg_miter_stop(miter);
362
363 return true;
364}
365
366static void jz4740_mmc_timeout(unsigned long data)
367{
368 struct jz4740_mmc_host *host = (struct jz4740_mmc_host *)data;
369
370 if (!test_and_clear_bit(0, &host->waiting))
371 return;
372
373 jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, false);
374
375 host->req->cmd->error = -ETIMEDOUT;
376 jz4740_mmc_request_done(host);
377}
378
379static void jz4740_mmc_read_response(struct jz4740_mmc_host *host,
380 struct mmc_command *cmd)
381{
382 int i;
383 uint16_t tmp;
384 void __iomem *fifo_addr = host->base + JZ_REG_MMC_RESP_FIFO;
385
386 if (cmd->flags & MMC_RSP_136) {
387 tmp = readw(fifo_addr);
388 for (i = 0; i < 4; ++i) {
389 cmd->resp[i] = tmp << 24;
390 tmp = readw(fifo_addr);
391 cmd->resp[i] |= tmp << 8;
392 tmp = readw(fifo_addr);
393 cmd->resp[i] |= tmp >> 8;
394 }
395 } else {
396 cmd->resp[0] = readw(fifo_addr) << 24;
397 cmd->resp[0] |= readw(fifo_addr) << 8;
398 cmd->resp[0] |= readw(fifo_addr) & 0xff;
399 }
400}
401
402static void jz4740_mmc_send_command(struct jz4740_mmc_host *host,
403 struct mmc_command *cmd)
404{
405 uint32_t cmdat = host->cmdat;
406
407 host->cmdat &= ~JZ_MMC_CMDAT_INIT;
408 jz4740_mmc_clock_disable(host);
409
410 host->cmd = cmd;
411
412 if (cmd->flags & MMC_RSP_BUSY)
413 cmdat |= JZ_MMC_CMDAT_BUSY;
414
415 switch (mmc_resp_type(cmd)) {
416 case MMC_RSP_R1B:
417 case MMC_RSP_R1:
418 cmdat |= JZ_MMC_CMDAT_RSP_R1;
419 break;
420 case MMC_RSP_R2:
421 cmdat |= JZ_MMC_CMDAT_RSP_R2;
422 break;
423 case MMC_RSP_R3:
424 cmdat |= JZ_MMC_CMDAT_RSP_R3;
425 break;
426 default:
427 break;
428 }
429
430 if (cmd->data) {
431 cmdat |= JZ_MMC_CMDAT_DATA_EN;
432 if (cmd->data->flags & MMC_DATA_WRITE)
433 cmdat |= JZ_MMC_CMDAT_WRITE;
434 if (cmd->data->flags & MMC_DATA_STREAM)
435 cmdat |= JZ_MMC_CMDAT_STREAM;
436
437 writew(cmd->data->blksz, host->base + JZ_REG_MMC_BLKLEN);
438 writew(cmd->data->blocks, host->base + JZ_REG_MMC_NOB);
439 }
440
441 writeb(cmd->opcode, host->base + JZ_REG_MMC_CMD);
442 writel(cmd->arg, host->base + JZ_REG_MMC_ARG);
443 writel(cmdat, host->base + JZ_REG_MMC_CMDAT);
444
445 jz4740_mmc_clock_enable(host, 1);
446}
447
448static void jz_mmc_prepare_data_transfer(struct jz4740_mmc_host *host)
449{
450 struct mmc_command *cmd = host->req->cmd;
451 struct mmc_data *data = cmd->data;
452 int direction;
453
454 if (data->flags & MMC_DATA_READ)
455 direction = SG_MITER_TO_SG;
456 else
457 direction = SG_MITER_FROM_SG;
458
459 sg_miter_start(&host->miter, data->sg, data->sg_len, direction);
460}
461
462
463static irqreturn_t jz_mmc_irq_worker(int irq, void *devid)
464{
465 struct jz4740_mmc_host *host = (struct jz4740_mmc_host *)devid;
466 struct mmc_command *cmd = host->req->cmd;
467 struct mmc_request *req = host->req;
468 bool timeout = false;
469
470 if (cmd->error)
471 host->state = JZ4740_MMC_STATE_DONE;
472
473 switch (host->state) {
474 case JZ4740_MMC_STATE_READ_RESPONSE:
475 if (cmd->flags & MMC_RSP_PRESENT)
476 jz4740_mmc_read_response(host, cmd);
477
478 if (!cmd->data)
479 break;
480
481 jz_mmc_prepare_data_transfer(host);
482
483 case JZ4740_MMC_STATE_TRANSFER_DATA:
484 if (cmd->data->flags & MMC_DATA_READ)
485 timeout = jz4740_mmc_read_data(host, cmd->data);
486 else
487 timeout = jz4740_mmc_write_data(host, cmd->data);
488
489 if (unlikely(timeout)) {
490 host->state = JZ4740_MMC_STATE_TRANSFER_DATA;
491 break;
492 }
493
494 jz4740_mmc_transfer_check_state(host, cmd->data);
495
496 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_DATA_TRAN_DONE);
497 if (unlikely(timeout)) {
498 host->state = JZ4740_MMC_STATE_SEND_STOP;
499 break;
500 }
501 writew(JZ_MMC_IRQ_DATA_TRAN_DONE, host->base + JZ_REG_MMC_IREG);
502
503 case JZ4740_MMC_STATE_SEND_STOP:
504 if (!req->stop)
505 break;
506
507 jz4740_mmc_send_command(host, req->stop);
508
509 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_PRG_DONE);
510 if (timeout) {
511 host->state = JZ4740_MMC_STATE_DONE;
512 break;
513 }
514 case JZ4740_MMC_STATE_DONE:
515 break;
516 }
517
518 if (!timeout)
519 jz4740_mmc_request_done(host);
520
521 return IRQ_HANDLED;
522}
523
524static irqreturn_t jz_mmc_irq(int irq, void *devid)
525{
526 struct jz4740_mmc_host *host = devid;
527 struct mmc_command *cmd = host->cmd;
528 uint16_t irq_reg, status, tmp;
529
530 irq_reg = readw(host->base + JZ_REG_MMC_IREG);
531
532 tmp = irq_reg;
533 irq_reg &= ~host->irq_mask;
534
535 tmp &= ~(JZ_MMC_IRQ_TXFIFO_WR_REQ | JZ_MMC_IRQ_RXFIFO_RD_REQ |
536 JZ_MMC_IRQ_PRG_DONE | JZ_MMC_IRQ_DATA_TRAN_DONE);
537
538 if (tmp != irq_reg)
539 writew(tmp & ~irq_reg, host->base + JZ_REG_MMC_IREG);
540
541 if (irq_reg & JZ_MMC_IRQ_SDIO) {
542 writew(JZ_MMC_IRQ_SDIO, host->base + JZ_REG_MMC_IREG);
543 mmc_signal_sdio_irq(host->mmc);
544 irq_reg &= ~JZ_MMC_IRQ_SDIO;
545 }
546
547 if (host->req && cmd && irq_reg) {
548 if (test_and_clear_bit(0, &host->waiting)) {
549 del_timer(&host->timeout_timer);
550
551 status = readl(host->base + JZ_REG_MMC_STATUS);
552
553 if (status & JZ_MMC_STATUS_TIMEOUT_RES) {
554 cmd->error = -ETIMEDOUT;
555 } else if (status & JZ_MMC_STATUS_CRC_RES_ERR) {
556 cmd->error = -EIO;
557 } else if (status & (JZ_MMC_STATUS_CRC_READ_ERROR |
558 JZ_MMC_STATUS_CRC_WRITE_ERROR)) {
559 if (cmd->data)
560 cmd->data->error = -EIO;
561 cmd->error = -EIO;
562 } else if (status & (JZ_MMC_STATUS_CRC_READ_ERROR |
563 JZ_MMC_STATUS_CRC_WRITE_ERROR)) {
564 if (cmd->data)
565 cmd->data->error = -EIO;
566 cmd->error = -EIO;
567 }
568
569 jz4740_mmc_set_irq_enabled(host, irq_reg, false);
570 writew(irq_reg, host->base + JZ_REG_MMC_IREG);
571
572 return IRQ_WAKE_THREAD;
573 }
574 }
575
576 return IRQ_HANDLED;
577}
578
579static int jz4740_mmc_set_clock_rate(struct jz4740_mmc_host *host, int rate)
580{
581 int div = 0;
582 int real_rate;
583
584 jz4740_mmc_clock_disable(host);
585 clk_set_rate(host->clk, JZ_MMC_CLK_RATE);
586
587 real_rate = clk_get_rate(host->clk);
588
589 while (real_rate > rate && div < 7) {
590 ++div;
591 real_rate >>= 1;
592 }
593
594 writew(div, host->base + JZ_REG_MMC_CLKRT);
595 return real_rate;
596}
597
598static void jz4740_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
599{
600 struct jz4740_mmc_host *host = mmc_priv(mmc);
601
602 host->req = req;
603
604 writew(0xffff, host->base + JZ_REG_MMC_IREG);
605
606 writew(JZ_MMC_IRQ_END_CMD_RES, host->base + JZ_REG_MMC_IREG);
607 jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, true);
608
609 host->state = JZ4740_MMC_STATE_READ_RESPONSE;
610 set_bit(0, &host->waiting);
611 mod_timer(&host->timeout_timer, jiffies + 5*HZ);
612 jz4740_mmc_send_command(host, req->cmd);
613}
614
615static void jz4740_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
616{
617 struct jz4740_mmc_host *host = mmc_priv(mmc);
618 if (ios->clock)
619 jz4740_mmc_set_clock_rate(host, ios->clock);
620
621 switch (ios->power_mode) {
622 case MMC_POWER_UP:
623 jz4740_mmc_reset(host);
624 if (gpio_is_valid(host->pdata->gpio_power))
625 gpio_set_value(host->pdata->gpio_power,
626 !host->pdata->power_active_low);
627 host->cmdat |= JZ_MMC_CMDAT_INIT;
628 clk_enable(host->clk);
629 break;
630 case MMC_POWER_ON:
631 break;
632 default:
633 if (gpio_is_valid(host->pdata->gpio_power))
634 gpio_set_value(host->pdata->gpio_power,
635 host->pdata->power_active_low);
636 clk_disable(host->clk);
637 break;
638 }
639
640 switch (ios->bus_width) {
641 case MMC_BUS_WIDTH_1:
642 host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
643 break;
644 case MMC_BUS_WIDTH_4:
645 host->cmdat |= JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
646 break;
647 default:
648 break;
649 }
650}
651
652static int jz4740_mmc_get_ro(struct mmc_host *mmc)
653{
654 struct jz4740_mmc_host *host = mmc_priv(mmc);
655 if (!gpio_is_valid(host->pdata->gpio_read_only))
656 return -ENOSYS;
657
658 return gpio_get_value(host->pdata->gpio_read_only) ^
659 host->pdata->read_only_active_low;
660}
661
662static int jz4740_mmc_get_cd(struct mmc_host *mmc)
663{
664 struct jz4740_mmc_host *host = mmc_priv(mmc);
665 if (!gpio_is_valid(host->pdata->gpio_card_detect))
666 return -ENOSYS;
667
668 return gpio_get_value(host->pdata->gpio_card_detect) ^
669 host->pdata->card_detect_active_low;
670}
671
672static irqreturn_t jz4740_mmc_card_detect_irq(int irq, void *devid)
673{
674 struct jz4740_mmc_host *host = devid;
675
676 mmc_detect_change(host->mmc, HZ / 2);
677
678 return IRQ_HANDLED;
679}
680
681static void jz4740_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
682{
683 struct jz4740_mmc_host *host = mmc_priv(mmc);
684 jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_SDIO, enable);
685}
686
687static const struct mmc_host_ops jz4740_mmc_ops = {
688 .request = jz4740_mmc_request,
689 .set_ios = jz4740_mmc_set_ios,
690 .get_ro = jz4740_mmc_get_ro,
691 .get_cd = jz4740_mmc_get_cd,
692 .enable_sdio_irq = jz4740_mmc_enable_sdio_irq,
693};
694
695static const struct jz_gpio_bulk_request jz4740_mmc_pins[] = {
696 JZ_GPIO_BULK_PIN(MSC_CMD),
697 JZ_GPIO_BULK_PIN(MSC_CLK),
698 JZ_GPIO_BULK_PIN(MSC_DATA0),
699 JZ_GPIO_BULK_PIN(MSC_DATA1),
700 JZ_GPIO_BULK_PIN(MSC_DATA2),
701 JZ_GPIO_BULK_PIN(MSC_DATA3),
702};
703
704static int __devinit jz4740_mmc_request_gpio(struct device *dev, int gpio,
705 const char *name, bool output, int value)
706{
707 int ret;
708
709 if (!gpio_is_valid(gpio))
710 return 0;
711
712 ret = gpio_request(gpio, name);
713 if (ret) {
714 dev_err(dev, "Failed to request %s gpio: %d\n", name, ret);
715 return ret;
716 }
717
718 if (output)
719 gpio_direction_output(gpio, value);
720 else
721 gpio_direction_input(gpio);
722
723 return 0;
724}
725
726static int __devinit jz4740_mmc_request_gpios(struct platform_device *pdev)
727{
728 int ret;
729 struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
730
731 if (!pdata)
732 return 0;
733
734 ret = jz4740_mmc_request_gpio(&pdev->dev, pdata->gpio_card_detect,
735 "MMC detect change", false, 0);
736 if (ret)
737 goto err;
738
739 ret = jz4740_mmc_request_gpio(&pdev->dev, pdata->gpio_read_only,
740 "MMC read only", false, 0);
741 if (ret)
742 goto err_free_gpio_card_detect;
743
744 ret = jz4740_mmc_request_gpio(&pdev->dev, pdata->gpio_power,
745 "MMC read only", true, pdata->power_active_low);
746 if (ret)
747 goto err_free_gpio_read_only;
748
749 return 0;
750
751err_free_gpio_read_only:
752 if (gpio_is_valid(pdata->gpio_read_only))
753 gpio_free(pdata->gpio_read_only);
754err_free_gpio_card_detect:
755 if (gpio_is_valid(pdata->gpio_card_detect))
756 gpio_free(pdata->gpio_card_detect);
757err:
758 return ret;
759}
760
761static int __devinit jz4740_mmc_request_cd_irq(struct platform_device *pdev,
762 struct jz4740_mmc_host *host)
763{
764 struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
765
766 if (!gpio_is_valid(pdata->gpio_card_detect))
767 return 0;
768
769 host->card_detect_irq = gpio_to_irq(pdata->gpio_card_detect);
770 if (host->card_detect_irq < 0) {
771 dev_warn(&pdev->dev, "Failed to get card detect irq\n");
772 return 0;
773 }
774
775 return request_irq(host->card_detect_irq, jz4740_mmc_card_detect_irq,
776 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
777 "MMC card detect", host);
778}
779
780static void jz4740_mmc_free_gpios(struct platform_device *pdev)
781{
782 struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
783
784 if (!pdata)
785 return;
786
787 if (gpio_is_valid(pdata->gpio_power))
788 gpio_free(pdata->gpio_power);
789 if (gpio_is_valid(pdata->gpio_read_only))
790 gpio_free(pdata->gpio_read_only);
791 if (gpio_is_valid(pdata->gpio_card_detect))
792 gpio_free(pdata->gpio_card_detect);
793}
794
795static inline size_t jz4740_mmc_num_pins(struct jz4740_mmc_host *host)
796{
797 size_t num_pins = ARRAY_SIZE(jz4740_mmc_pins);
798 if (host->pdata && host->pdata->data_1bit)
799 num_pins -= 3;
800
801 return num_pins;
802}
803
804static int __devinit jz4740_mmc_probe(struct platform_device* pdev)
805{
806 int ret;
807 struct mmc_host *mmc;
808 struct jz4740_mmc_host *host;
809 struct jz4740_mmc_platform_data *pdata;
810
811 pdata = pdev->dev.platform_data;
812
813 mmc = mmc_alloc_host(sizeof(struct jz4740_mmc_host), &pdev->dev);
814 if (!mmc) {
815 dev_err(&pdev->dev, "Failed to alloc mmc host structure\n");
816 return -ENOMEM;
817 }
818
819 host = mmc_priv(mmc);
820 host->pdata = pdata;
821
822 host->irq = platform_get_irq(pdev, 0);
823 if (host->irq < 0) {
824 ret = host->irq;
825 dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
826 goto err_free_host;
827 }
828
829 host->clk = clk_get(&pdev->dev, "mmc");
830 if (!host->clk) {
831 ret = -ENOENT;
832 dev_err(&pdev->dev, "Failed to get mmc clock\n");
833 goto err_free_host;
834 }
835
836 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
837 if (!host->mem) {
838 ret = -ENOENT;
839 dev_err(&pdev->dev, "Failed to get base platform memory\n");
840 goto err_clk_put;
841 }
842
843 host->mem = request_mem_region(host->mem->start,
844 resource_size(host->mem), pdev->name);
845 if (!host->mem) {
846 ret = -EBUSY;
847 dev_err(&pdev->dev, "Failed to request base memory region\n");
848 goto err_clk_put;
849 }
850
851 host->base = ioremap_nocache(host->mem->start, resource_size(host->mem));
852 if (!host->base) {
853 ret = -EBUSY;
854 dev_err(&pdev->dev, "Failed to ioremap base memory\n");
855 goto err_release_mem_region;
856 }
857
858 ret = jz_gpio_bulk_request(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
859 if (ret) {
860 dev_err(&pdev->dev, "Failed to request mmc pins: %d\n", ret);
861 goto err_iounmap;
862 }
863
864 ret = jz4740_mmc_request_gpios(pdev);
865 if (ret)
866 goto err_gpio_bulk_free;
867
868 mmc->ops = &jz4740_mmc_ops;
869 mmc->f_min = JZ_MMC_CLK_RATE / 128;
870 mmc->f_max = JZ_MMC_CLK_RATE;
871 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
872 mmc->caps = (pdata && pdata->data_1bit) ? 0 : MMC_CAP_4_BIT_DATA;
873 mmc->caps |= MMC_CAP_SDIO_IRQ;
874
875 mmc->max_blk_size = (1 << 10) - 1;
876 mmc->max_blk_count = (1 << 15) - 1;
877 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
878
879 mmc->max_phys_segs = 128;
880 mmc->max_hw_segs = 128;
881 mmc->max_seg_size = mmc->max_req_size;
882
883 host->mmc = mmc;
884 host->pdev = pdev;
885 spin_lock_init(&host->lock);
886 host->irq_mask = 0xffff;
887
888 ret = jz4740_mmc_request_cd_irq(pdev, host);
889 if (ret) {
890 dev_err(&pdev->dev, "Failed to request card detect irq\n");
891 goto err_free_gpios;
892 }
893
894 ret = request_threaded_irq(host->irq, jz_mmc_irq, jz_mmc_irq_worker, 0,
895 dev_name(&pdev->dev), host);
896 if (ret) {
897 dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
898 goto err_free_card_detect_irq;
899 }
900
901 jz4740_mmc_reset(host);
902 jz4740_mmc_clock_disable(host);
903 setup_timer(&host->timeout_timer, jz4740_mmc_timeout,
904 (unsigned long)host);
905 /* It is not important when it times out, it just needs to timeout. */
906 set_timer_slack(&host->timeout_timer, HZ);
907
908 platform_set_drvdata(pdev, host);
909 ret = mmc_add_host(mmc);
910
911 if (ret) {
912 dev_err(&pdev->dev, "Failed to add mmc host: %d\n", ret);
913 goto err_free_irq;
914 }
915 dev_info(&pdev->dev, "JZ SD/MMC card driver registered\n");
916
917 return 0;
918
919err_free_irq:
920 free_irq(host->irq, host);
921err_free_card_detect_irq:
922 if (host->card_detect_irq >= 0)
923 free_irq(host->card_detect_irq, host);
924err_free_gpios:
925 jz4740_mmc_free_gpios(pdev);
926err_gpio_bulk_free:
927 jz_gpio_bulk_free(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
928err_iounmap:
929 iounmap(host->base);
930err_release_mem_region:
931 release_mem_region(host->mem->start, resource_size(host->mem));
932err_clk_put:
933 clk_put(host->clk);
934err_free_host:
935 platform_set_drvdata(pdev, NULL);
936 mmc_free_host(mmc);
937
938 return ret;
939}
940
941static int __devexit jz4740_mmc_remove(struct platform_device *pdev)
942{
943 struct jz4740_mmc_host *host = platform_get_drvdata(pdev);
944
945 del_timer_sync(&host->timeout_timer);
946 jz4740_mmc_set_irq_enabled(host, 0xff, false);
947 jz4740_mmc_reset(host);
948
949 mmc_remove_host(host->mmc);
950
951 free_irq(host->irq, host);
952 if (host->card_detect_irq >= 0)
953 free_irq(host->card_detect_irq, host);
954
955 jz4740_mmc_free_gpios(pdev);
956 jz_gpio_bulk_free(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
957
958 iounmap(host->base);
959 release_mem_region(host->mem->start, resource_size(host->mem));
960
961 clk_put(host->clk);
962
963 platform_set_drvdata(pdev, NULL);
964 mmc_free_host(host->mmc);
965
966 return 0;
967}
968
969#ifdef CONFIG_PM
970
971static int jz4740_mmc_suspend(struct device *dev)
972{
973 struct jz4740_mmc_host *host = dev_get_drvdata(dev);
974
975 mmc_suspend_host(host->mmc);
976
977 jz_gpio_bulk_suspend(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
978
979 return 0;
980}
981
982static int jz4740_mmc_resume(struct device *dev)
983{
984 struct jz4740_mmc_host *host = dev_get_drvdata(dev);
985
986 jz_gpio_bulk_resume(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
987
988 mmc_resume_host(host->mmc);
989
990 return 0;
991}
992
993const struct dev_pm_ops jz4740_mmc_pm_ops = {
994 .suspend = jz4740_mmc_suspend,
995 .resume = jz4740_mmc_resume,
996 .poweroff = jz4740_mmc_suspend,
997 .restore = jz4740_mmc_resume,
998};
999
1000#define JZ4740_MMC_PM_OPS (&jz4740_mmc_pm_ops)
1001#else
1002#define JZ4740_MMC_PM_OPS NULL
1003#endif
1004
1005static struct platform_driver jz4740_mmc_driver = {
1006 .probe = jz4740_mmc_probe,
1007 .remove = __devexit_p(jz4740_mmc_remove),
1008 .driver = {
1009 .name = "jz4740-mmc",
1010 .owner = THIS_MODULE,
1011 .pm = JZ4740_MMC_PM_OPS,
1012 },
1013};
1014
1015static int __init jz4740_mmc_init(void)
1016{
1017 return platform_driver_register(&jz4740_mmc_driver);
1018}
1019module_init(jz4740_mmc_init);
1020
1021static void __exit jz4740_mmc_exit(void)
1022{
1023 platform_driver_unregister(&jz4740_mmc_driver);
1024}
1025module_exit(jz4740_mmc_exit);
1026
1027MODULE_DESCRIPTION("JZ4740 SD/MMC controller driver");
1028MODULE_LICENSE("GPL");
1029MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
index ad847a24a675..1145ea0792e6 100644
--- a/drivers/mmc/host/mmc_spi.c
+++ b/drivers/mmc/host/mmc_spi.c
@@ -1533,12 +1533,21 @@ static int __devexit mmc_spi_remove(struct spi_device *spi)
1533 return 0; 1533 return 0;
1534} 1534}
1535 1535
1536#if defined(CONFIG_OF)
1537static struct of_device_id mmc_spi_of_match_table[] __devinitdata = {
1538 { .compatible = "mmc-spi-slot", },
1539 {},
1540};
1541#endif
1536 1542
1537static struct spi_driver mmc_spi_driver = { 1543static struct spi_driver mmc_spi_driver = {
1538 .driver = { 1544 .driver = {
1539 .name = "mmc_spi", 1545 .name = "mmc_spi",
1540 .bus = &spi_bus_type, 1546 .bus = &spi_bus_type,
1541 .owner = THIS_MODULE, 1547 .owner = THIS_MODULE,
1548#if defined(CONFIG_OF)
1549 .of_match_table = mmc_spi_of_match_table,
1550#endif
1542 }, 1551 },
1543 .probe = mmc_spi_probe, 1552 .probe = mmc_spi_probe,
1544 .remove = __devexit_p(mmc_spi_remove), 1553 .remove = __devexit_p(mmc_spi_remove),
diff --git a/drivers/mmc/host/mmci.c b/drivers/mmc/host/mmci.c
index 2ed435bd4b6c..840b301b5671 100644
--- a/drivers/mmc/host/mmci.c
+++ b/drivers/mmc/host/mmci.c
@@ -26,7 +26,6 @@
26#include <linux/amba/mmci.h> 26#include <linux/amba/mmci.h>
27#include <linux/regulator/consumer.h> 27#include <linux/regulator/consumer.h>
28 28
29#include <asm/cacheflush.h>
30#include <asm/div64.h> 29#include <asm/div64.h>
31#include <asm/io.h> 30#include <asm/io.h>
32#include <asm/sizes.h> 31#include <asm/sizes.h>
@@ -37,12 +36,39 @@
37 36
38static unsigned int fmax = 515633; 37static unsigned int fmax = 515633;
39 38
39/**
40 * struct variant_data - MMCI variant-specific quirks
41 * @clkreg: default value for MCICLOCK register
42 * @clkreg_enable: enable value for MMCICLOCK register
43 * @datalength_bits: number of bits in the MMCIDATALENGTH register
44 */
45struct variant_data {
46 unsigned int clkreg;
47 unsigned int clkreg_enable;
48 unsigned int datalength_bits;
49};
50
51static struct variant_data variant_arm = {
52 .datalength_bits = 16,
53};
54
55static struct variant_data variant_u300 = {
56 .clkreg_enable = 1 << 13, /* HWFCEN */
57 .datalength_bits = 16,
58};
59
60static struct variant_data variant_ux500 = {
61 .clkreg = MCI_CLK_ENABLE,
62 .clkreg_enable = 1 << 14, /* HWFCEN */
63 .datalength_bits = 24,
64};
40/* 65/*
41 * This must be called with host->lock held 66 * This must be called with host->lock held
42 */ 67 */
43static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired) 68static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
44{ 69{
45 u32 clk = 0; 70 struct variant_data *variant = host->variant;
71 u32 clk = variant->clkreg;
46 72
47 if (desired) { 73 if (desired) {
48 if (desired >= host->mclk) { 74 if (desired >= host->mclk) {
@@ -54,8 +80,8 @@ static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
54 clk = 255; 80 clk = 255;
55 host->cclk = host->mclk / (2 * (clk + 1)); 81 host->cclk = host->mclk / (2 * (clk + 1));
56 } 82 }
57 if (host->hw_designer == AMBA_VENDOR_ST) 83
58 clk |= MCI_ST_FCEN; /* Bug fix in ST IP block */ 84 clk |= variant->clkreg_enable;
59 clk |= MCI_CLK_ENABLE; 85 clk |= MCI_CLK_ENABLE;
60 /* This hasn't proven to be worthwhile */ 86 /* This hasn't proven to be worthwhile */
61 /* clk |= MCI_CLK_PWRSAVE; */ 87 /* clk |= MCI_CLK_PWRSAVE; */
@@ -98,6 +124,18 @@ static void mmci_stop_data(struct mmci_host *host)
98 host->data = NULL; 124 host->data = NULL;
99} 125}
100 126
127static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
128{
129 unsigned int flags = SG_MITER_ATOMIC;
130
131 if (data->flags & MMC_DATA_READ)
132 flags |= SG_MITER_TO_SG;
133 else
134 flags |= SG_MITER_FROM_SG;
135
136 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
137}
138
101static void mmci_start_data(struct mmci_host *host, struct mmc_data *data) 139static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
102{ 140{
103 unsigned int datactrl, timeout, irqmask; 141 unsigned int datactrl, timeout, irqmask;
@@ -109,7 +147,7 @@ static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
109 data->blksz, data->blocks, data->flags); 147 data->blksz, data->blocks, data->flags);
110 148
111 host->data = data; 149 host->data = data;
112 host->size = data->blksz; 150 host->size = data->blksz * data->blocks;
113 host->data_xfered = 0; 151 host->data_xfered = 0;
114 152
115 mmci_init_sg(host, data); 153 mmci_init_sg(host, data);
@@ -210,8 +248,17 @@ mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
210 * We hit an error condition. Ensure that any data 248 * We hit an error condition. Ensure that any data
211 * partially written to a page is properly coherent. 249 * partially written to a page is properly coherent.
212 */ 250 */
213 if (host->sg_len && data->flags & MMC_DATA_READ) 251 if (data->flags & MMC_DATA_READ) {
214 flush_dcache_page(sg_page(host->sg_ptr)); 252 struct sg_mapping_iter *sg_miter = &host->sg_miter;
253 unsigned long flags;
254
255 local_irq_save(flags);
256 if (sg_miter_next(sg_miter)) {
257 flush_dcache_page(sg_miter->page);
258 sg_miter_stop(sg_miter);
259 }
260 local_irq_restore(flags);
261 }
215 } 262 }
216 if (status & MCI_DATAEND) { 263 if (status & MCI_DATAEND) {
217 mmci_stop_data(host); 264 mmci_stop_data(host);
@@ -314,15 +361,18 @@ static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int rem
314static irqreturn_t mmci_pio_irq(int irq, void *dev_id) 361static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
315{ 362{
316 struct mmci_host *host = dev_id; 363 struct mmci_host *host = dev_id;
364 struct sg_mapping_iter *sg_miter = &host->sg_miter;
317 void __iomem *base = host->base; 365 void __iomem *base = host->base;
366 unsigned long flags;
318 u32 status; 367 u32 status;
319 368
320 status = readl(base + MMCISTATUS); 369 status = readl(base + MMCISTATUS);
321 370
322 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status); 371 dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
323 372
373 local_irq_save(flags);
374
324 do { 375 do {
325 unsigned long flags;
326 unsigned int remain, len; 376 unsigned int remain, len;
327 char *buffer; 377 char *buffer;
328 378
@@ -336,11 +386,11 @@ static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
336 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL))) 386 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
337 break; 387 break;
338 388
339 /* 389 if (!sg_miter_next(sg_miter))
340 * Map the current scatter buffer. 390 break;
341 */ 391
342 buffer = mmci_kmap_atomic(host, &flags) + host->sg_off; 392 buffer = sg_miter->addr;
343 remain = host->sg_ptr->length - host->sg_off; 393 remain = sg_miter->length;
344 394
345 len = 0; 395 len = 0;
346 if (status & MCI_RXACTIVE) 396 if (status & MCI_RXACTIVE)
@@ -348,31 +398,24 @@ static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
348 if (status & MCI_TXACTIVE) 398 if (status & MCI_TXACTIVE)
349 len = mmci_pio_write(host, buffer, remain, status); 399 len = mmci_pio_write(host, buffer, remain, status);
350 400
351 /* 401 sg_miter->consumed = len;
352 * Unmap the buffer.
353 */
354 mmci_kunmap_atomic(host, buffer, &flags);
355 402
356 host->sg_off += len;
357 host->size -= len; 403 host->size -= len;
358 remain -= len; 404 remain -= len;
359 405
360 if (remain) 406 if (remain)
361 break; 407 break;
362 408
363 /*
364 * If we were reading, and we have completed this
365 * page, ensure that the data cache is coherent.
366 */
367 if (status & MCI_RXACTIVE) 409 if (status & MCI_RXACTIVE)
368 flush_dcache_page(sg_page(host->sg_ptr)); 410 flush_dcache_page(sg_miter->page);
369
370 if (!mmci_next_sg(host))
371 break;
372 411
373 status = readl(base + MMCISTATUS); 412 status = readl(base + MMCISTATUS);
374 } while (1); 413 } while (1);
375 414
415 sg_miter_stop(sg_miter);
416
417 local_irq_restore(flags);
418
376 /* 419 /*
377 * If we're nearing the end of the read, switch to 420 * If we're nearing the end of the read, switch to
378 * "any data available" mode. 421 * "any data available" mode.
@@ -477,16 +520,9 @@ static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
477 /* This implicitly enables the regulator */ 520 /* This implicitly enables the regulator */
478 mmc_regulator_set_ocr(host->vcc, ios->vdd); 521 mmc_regulator_set_ocr(host->vcc, ios->vdd);
479#endif 522#endif
480 /* 523 if (host->plat->vdd_handler)
481 * The translate_vdd function is not used if you have 524 pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
482 * an external regulator, or your design is really weird. 525 ios->power_mode);
483 * Using it would mean sending in power control BOTH using
484 * a regulator AND the 4 MMCIPWR bits. If we don't have
485 * a regulator, we might have some other platform specific
486 * power control behind this translate function.
487 */
488 if (!host->vcc && host->plat->translate_vdd)
489 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
490 /* The ST version does not have this, fall through to POWER_ON */ 526 /* The ST version does not have this, fall through to POWER_ON */
491 if (host->hw_designer != AMBA_VENDOR_ST) { 527 if (host->hw_designer != AMBA_VENDOR_ST) {
492 pwr |= MCI_PWR_UP; 528 pwr |= MCI_PWR_UP;
@@ -555,21 +591,10 @@ static const struct mmc_host_ops mmci_ops = {
555 .get_cd = mmci_get_cd, 591 .get_cd = mmci_get_cd,
556}; 592};
557 593
558static void mmci_check_status(unsigned long data)
559{
560 struct mmci_host *host = (struct mmci_host *)data;
561 unsigned int status = mmci_get_cd(host->mmc);
562
563 if (status ^ host->oldstat)
564 mmc_detect_change(host->mmc, 0);
565
566 host->oldstat = status;
567 mod_timer(&host->timer, jiffies + HZ);
568}
569
570static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id) 594static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
571{ 595{
572 struct mmci_platform_data *plat = dev->dev.platform_data; 596 struct mmci_platform_data *plat = dev->dev.platform_data;
597 struct variant_data *variant = id->data;
573 struct mmci_host *host; 598 struct mmci_host *host;
574 struct mmc_host *mmc; 599 struct mmc_host *mmc;
575 int ret; 600 int ret;
@@ -613,6 +638,7 @@ static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
613 goto clk_free; 638 goto clk_free;
614 639
615 host->plat = plat; 640 host->plat = plat;
641 host->variant = variant;
616 host->mclk = clk_get_rate(host->clk); 642 host->mclk = clk_get_rate(host->clk);
617 /* 643 /*
618 * According to the spec, mclk is max 100 MHz, 644 * According to the spec, mclk is max 100 MHz,
@@ -673,6 +699,7 @@ static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
673 if (host->vcc == NULL) 699 if (host->vcc == NULL)
674 mmc->ocr_avail = plat->ocr_mask; 700 mmc->ocr_avail = plat->ocr_mask;
675 mmc->caps = plat->capabilities; 701 mmc->caps = plat->capabilities;
702 mmc->caps |= MMC_CAP_NEEDS_POLL;
676 703
677 /* 704 /*
678 * We can do SGIO 705 * We can do SGIO
@@ -681,10 +708,11 @@ static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
681 mmc->max_phys_segs = NR_SG; 708 mmc->max_phys_segs = NR_SG;
682 709
683 /* 710 /*
684 * Since we only have a 16-bit data length register, we must 711 * Since only a certain number of bits are valid in the data length
685 * ensure that we don't exceed 2^16-1 bytes in a single request. 712 * register, we must ensure that we don't exceed 2^num-1 bytes in a
713 * single request.
686 */ 714 */
687 mmc->max_req_size = 65535; 715 mmc->max_req_size = (1 << variant->datalength_bits) - 1;
688 716
689 /* 717 /*
690 * Set the maximum segment size. Since we aren't doing DMA 718 * Set the maximum segment size. Since we aren't doing DMA
@@ -738,7 +766,6 @@ static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
738 writel(MCI_IRQENABLE, host->base + MMCIMASK0); 766 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
739 767
740 amba_set_drvdata(dev, mmc); 768 amba_set_drvdata(dev, mmc);
741 host->oldstat = mmci_get_cd(host->mmc);
742 769
743 mmc_add_host(mmc); 770 mmc_add_host(mmc);
744 771
@@ -746,12 +773,6 @@ static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
746 mmc_hostname(mmc), amba_rev(dev), amba_config(dev), 773 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
747 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]); 774 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
748 775
749 init_timer(&host->timer);
750 host->timer.data = (unsigned long)host;
751 host->timer.function = mmci_check_status;
752 host->timer.expires = jiffies + HZ;
753 add_timer(&host->timer);
754
755 return 0; 776 return 0;
756 777
757 irq0_free: 778 irq0_free:
@@ -785,8 +806,6 @@ static int __devexit mmci_remove(struct amba_device *dev)
785 if (mmc) { 806 if (mmc) {
786 struct mmci_host *host = mmc_priv(mmc); 807 struct mmci_host *host = mmc_priv(mmc);
787 808
788 del_timer_sync(&host->timer);
789
790 mmc_remove_host(mmc); 809 mmc_remove_host(mmc);
791 810
792 writel(0, host->base + MMCIMASK0); 811 writel(0, host->base + MMCIMASK0);
@@ -860,19 +879,28 @@ static struct amba_id mmci_ids[] = {
860 { 879 {
861 .id = 0x00041180, 880 .id = 0x00041180,
862 .mask = 0x000fffff, 881 .mask = 0x000fffff,
882 .data = &variant_arm,
863 }, 883 },
864 { 884 {
865 .id = 0x00041181, 885 .id = 0x00041181,
866 .mask = 0x000fffff, 886 .mask = 0x000fffff,
887 .data = &variant_arm,
867 }, 888 },
868 /* ST Micro variants */ 889 /* ST Micro variants */
869 { 890 {
870 .id = 0x00180180, 891 .id = 0x00180180,
871 .mask = 0x00ffffff, 892 .mask = 0x00ffffff,
893 .data = &variant_u300,
872 }, 894 },
873 { 895 {
874 .id = 0x00280180, 896 .id = 0x00280180,
875 .mask = 0x00ffffff, 897 .mask = 0x00ffffff,
898 .data = &variant_u300,
899 },
900 {
901 .id = 0x00480180,
902 .mask = 0x00ffffff,
903 .data = &variant_ux500,
876 }, 904 },
877 { 0, 0 }, 905 { 0, 0 },
878}; 906};
diff --git a/drivers/mmc/host/mmci.h b/drivers/mmc/host/mmci.h
index d77062e5e3af..68970cfb81e1 100644
--- a/drivers/mmc/host/mmci.h
+++ b/drivers/mmc/host/mmci.h
@@ -28,8 +28,6 @@
28#define MCI_4BIT_BUS (1 << 11) 28#define MCI_4BIT_BUS (1 << 11)
29/* 8bit wide buses supported in ST Micro versions */ 29/* 8bit wide buses supported in ST Micro versions */
30#define MCI_ST_8BIT_BUS (1 << 12) 30#define MCI_ST_8BIT_BUS (1 << 12)
31/* HW flow control on the ST Micro version */
32#define MCI_ST_FCEN (1 << 13)
33 31
34#define MMCIARGUMENT 0x008 32#define MMCIARGUMENT 0x008
35#define MMCICOMMAND 0x00c 33#define MMCICOMMAND 0x00c
@@ -145,6 +143,7 @@
145#define NR_SG 16 143#define NR_SG 16
146 144
147struct clk; 145struct clk;
146struct variant_data;
148 147
149struct mmci_host { 148struct mmci_host {
150 void __iomem *base; 149 void __iomem *base;
@@ -164,6 +163,7 @@ struct mmci_host {
164 unsigned int cclk; 163 unsigned int cclk;
165 u32 pwr; 164 u32 pwr;
166 struct mmci_platform_data *plat; 165 struct mmci_platform_data *plat;
166 struct variant_data *variant;
167 167
168 u8 hw_designer; 168 u8 hw_designer;
169 u8 hw_revision:4; 169 u8 hw_revision:4;
@@ -171,42 +171,9 @@ struct mmci_host {
171 struct timer_list timer; 171 struct timer_list timer;
172 unsigned int oldstat; 172 unsigned int oldstat;
173 173
174 unsigned int sg_len;
175
176 /* pio stuff */ 174 /* pio stuff */
177 struct scatterlist *sg_ptr; 175 struct sg_mapping_iter sg_miter;
178 unsigned int sg_off;
179 unsigned int size; 176 unsigned int size;
180 struct regulator *vcc; 177 struct regulator *vcc;
181}; 178};
182 179
183static inline void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
184{
185 /*
186 * Ideally, we want the higher levels to pass us a scatter list.
187 */
188 host->sg_len = data->sg_len;
189 host->sg_ptr = data->sg;
190 host->sg_off = 0;
191}
192
193static inline int mmci_next_sg(struct mmci_host *host)
194{
195 host->sg_ptr++;
196 host->sg_off = 0;
197 return --host->sg_len;
198}
199
200static inline char *mmci_kmap_atomic(struct mmci_host *host, unsigned long *flags)
201{
202 struct scatterlist *sg = host->sg_ptr;
203
204 local_irq_save(*flags);
205 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
206}
207
208static inline void mmci_kunmap_atomic(struct mmci_host *host, void *buffer, unsigned long *flags)
209{
210 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
211 local_irq_restore(*flags);
212}
diff --git a/drivers/mmc/host/msm_sdcc.c b/drivers/mmc/host/msm_sdcc.c
index 24e09454e522..3bd8ff71ec22 100644
--- a/drivers/mmc/host/msm_sdcc.c
+++ b/drivers/mmc/host/msm_sdcc.c
@@ -1057,22 +1057,6 @@ msmsdcc_init_dma(struct msmsdcc_host *host)
1057 return 0; 1057 return 0;
1058} 1058}
1059 1059
1060#ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
1061static void
1062do_resume_work(struct work_struct *work)
1063{
1064 struct msmsdcc_host *host =
1065 container_of(work, struct msmsdcc_host, resume_task);
1066 struct mmc_host *mmc = host->mmc;
1067
1068 if (mmc) {
1069 mmc_resume_host(mmc);
1070 if (host->stat_irq)
1071 enable_irq(host->stat_irq);
1072 }
1073}
1074#endif
1075
1076static int 1060static int
1077msmsdcc_probe(struct platform_device *pdev) 1061msmsdcc_probe(struct platform_device *pdev)
1078{ 1062{
@@ -1145,15 +1129,6 @@ msmsdcc_probe(struct platform_device *pdev)
1145 host->dmares = dmares; 1129 host->dmares = dmares;
1146 spin_lock_init(&host->lock); 1130 spin_lock_init(&host->lock);
1147 1131
1148#ifdef CONFIG_MMC_EMBEDDED_SDIO
1149 if (plat->embedded_sdio)
1150 mmc_set_embedded_sdio_data(mmc,
1151 &plat->embedded_sdio->cis,
1152 &plat->embedded_sdio->cccr,
1153 plat->embedded_sdio->funcs,
1154 plat->embedded_sdio->num_funcs);
1155#endif
1156
1157 /* 1132 /*
1158 * Setup DMA 1133 * Setup DMA
1159 */ 1134 */
diff --git a/drivers/mmc/host/msm_sdcc.h b/drivers/mmc/host/msm_sdcc.h
index da0039c9285e..d7dedcf33b5b 100644
--- a/drivers/mmc/host/msm_sdcc.h
+++ b/drivers/mmc/host/msm_sdcc.h
@@ -235,10 +235,6 @@ struct msmsdcc_host {
235 int cmdpoll; 235 int cmdpoll;
236 struct msmsdcc_stats stats; 236 struct msmsdcc_stats stats;
237 237
238#ifdef CONFIG_MMC_MSM7X00A_RESUME_IN_WQ
239 struct work_struct resume_task;
240#endif
241
242 /* Command parameters */ 238 /* Command parameters */
243 unsigned int cmd_timeout; 239 unsigned int cmd_timeout;
244 unsigned int cmd_pio_irqmask; 240 unsigned int cmd_pio_irqmask;
diff --git a/drivers/mmc/host/mxcmmc.c b/drivers/mmc/host/mxcmmc.c
index d9d4a72e0ec7..350f78e86245 100644
--- a/drivers/mmc/host/mxcmmc.c
+++ b/drivers/mmc/host/mxcmmc.c
@@ -119,6 +119,7 @@ struct mxcmci_host {
119 int detect_irq; 119 int detect_irq;
120 int dma; 120 int dma;
121 int do_dma; 121 int do_dma;
122 int default_irq_mask;
122 int use_sdio; 123 int use_sdio;
123 unsigned int power_mode; 124 unsigned int power_mode;
124 struct imxmmc_platform_data *pdata; 125 struct imxmmc_platform_data *pdata;
@@ -228,7 +229,7 @@ static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
228static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd, 229static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd,
229 unsigned int cmdat) 230 unsigned int cmdat)
230{ 231{
231 u32 int_cntr; 232 u32 int_cntr = host->default_irq_mask;
232 unsigned long flags; 233 unsigned long flags;
233 234
234 WARN_ON(host->cmd != NULL); 235 WARN_ON(host->cmd != NULL);
@@ -275,7 +276,7 @@ static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd,
275static void mxcmci_finish_request(struct mxcmci_host *host, 276static void mxcmci_finish_request(struct mxcmci_host *host,
276 struct mmc_request *req) 277 struct mmc_request *req)
277{ 278{
278 u32 int_cntr = 0; 279 u32 int_cntr = host->default_irq_mask;
279 unsigned long flags; 280 unsigned long flags;
280 281
281 spin_lock_irqsave(&host->lock, flags); 282 spin_lock_irqsave(&host->lock, flags);
@@ -585,6 +586,9 @@ static irqreturn_t mxcmci_irq(int irq, void *devid)
585 (stat & (STATUS_DATA_TRANS_DONE | STATUS_WRITE_OP_DONE))) 586 (stat & (STATUS_DATA_TRANS_DONE | STATUS_WRITE_OP_DONE)))
586 mxcmci_data_done(host, stat); 587 mxcmci_data_done(host, stat);
587#endif 588#endif
589 if (host->default_irq_mask &&
590 (stat & (STATUS_CARD_INSERTION | STATUS_CARD_REMOVAL)))
591 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
588 return IRQ_HANDLED; 592 return IRQ_HANDLED;
589} 593}
590 594
@@ -809,6 +813,12 @@ static int mxcmci_probe(struct platform_device *pdev)
809 else 813 else
810 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; 814 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
811 815
816 if (host->pdata && host->pdata->dat3_card_detect)
817 host->default_irq_mask =
818 INT_CARD_INSERTION_EN | INT_CARD_REMOVAL_EN;
819 else
820 host->default_irq_mask = 0;
821
812 host->res = r; 822 host->res = r;
813 host->irq = irq; 823 host->irq = irq;
814 824
@@ -835,7 +845,7 @@ static int mxcmci_probe(struct platform_device *pdev)
835 /* recommended in data sheet */ 845 /* recommended in data sheet */
836 writew(0x2db4, host->base + MMC_REG_READ_TO); 846 writew(0x2db4, host->base + MMC_REG_READ_TO);
837 847
838 writel(0, host->base + MMC_REG_INT_CNTR); 848 writel(host->default_irq_mask, host->base + MMC_REG_INT_CNTR);
839 849
840#ifdef HAS_DMA 850#ifdef HAS_DMA
841 host->dma = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_LOW); 851 host->dma = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_LOW);
@@ -926,43 +936,47 @@ static int mxcmci_remove(struct platform_device *pdev)
926} 936}
927 937
928#ifdef CONFIG_PM 938#ifdef CONFIG_PM
929static int mxcmci_suspend(struct platform_device *dev, pm_message_t state) 939static int mxcmci_suspend(struct device *dev)
930{ 940{
931 struct mmc_host *mmc = platform_get_drvdata(dev); 941 struct mmc_host *mmc = dev_get_drvdata(dev);
942 struct mxcmci_host *host = mmc_priv(mmc);
932 int ret = 0; 943 int ret = 0;
933 944
934 if (mmc) 945 if (mmc)
935 ret = mmc_suspend_host(mmc); 946 ret = mmc_suspend_host(mmc);
947 clk_disable(host->clk);
936 948
937 return ret; 949 return ret;
938} 950}
939 951
940static int mxcmci_resume(struct platform_device *dev) 952static int mxcmci_resume(struct device *dev)
941{ 953{
942 struct mmc_host *mmc = platform_get_drvdata(dev); 954 struct mmc_host *mmc = dev_get_drvdata(dev);
943 struct mxcmci_host *host; 955 struct mxcmci_host *host = mmc_priv(mmc);
944 int ret = 0; 956 int ret = 0;
945 957
946 if (mmc) { 958 clk_enable(host->clk);
947 host = mmc_priv(mmc); 959 if (mmc)
948 ret = mmc_resume_host(mmc); 960 ret = mmc_resume_host(mmc);
949 }
950 961
951 return ret; 962 return ret;
952} 963}
953#else 964
954#define mxcmci_suspend NULL 965static const struct dev_pm_ops mxcmci_pm_ops = {
955#define mxcmci_resume NULL 966 .suspend = mxcmci_suspend,
956#endif /* CONFIG_PM */ 967 .resume = mxcmci_resume,
968};
969#endif
957 970
958static struct platform_driver mxcmci_driver = { 971static struct platform_driver mxcmci_driver = {
959 .probe = mxcmci_probe, 972 .probe = mxcmci_probe,
960 .remove = mxcmci_remove, 973 .remove = mxcmci_remove,
961 .suspend = mxcmci_suspend,
962 .resume = mxcmci_resume,
963 .driver = { 974 .driver = {
964 .name = DRIVER_NAME, 975 .name = DRIVER_NAME,
965 .owner = THIS_MODULE, 976 .owner = THIS_MODULE,
977#ifdef CONFIG_PM
978 .pm = &mxcmci_pm_ops,
979#endif
966 } 980 }
967}; 981};
968 982
diff --git a/drivers/mmc/host/omap_hsmmc.c b/drivers/mmc/host/omap_hsmmc.c
index b032828c6126..4a8776f8afdd 100644
--- a/drivers/mmc/host/omap_hsmmc.c
+++ b/drivers/mmc/host/omap_hsmmc.c
@@ -28,6 +28,7 @@
28#include <linux/clk.h> 28#include <linux/clk.h>
29#include <linux/mmc/host.h> 29#include <linux/mmc/host.h>
30#include <linux/mmc/core.h> 30#include <linux/mmc/core.h>
31#include <linux/mmc/mmc.h>
31#include <linux/io.h> 32#include <linux/io.h>
32#include <linux/semaphore.h> 33#include <linux/semaphore.h>
33#include <linux/gpio.h> 34#include <linux/gpio.h>
@@ -78,6 +79,7 @@
78#define INT_EN_MASK 0x307F0033 79#define INT_EN_MASK 0x307F0033
79#define BWR_ENABLE (1 << 4) 80#define BWR_ENABLE (1 << 4)
80#define BRR_ENABLE (1 << 5) 81#define BRR_ENABLE (1 << 5)
82#define DTO_ENABLE (1 << 20)
81#define INIT_STREAM (1 << 1) 83#define INIT_STREAM (1 << 1)
82#define DP_SELECT (1 << 21) 84#define DP_SELECT (1 << 21)
83#define DDIR (1 << 4) 85#define DDIR (1 << 4)
@@ -523,7 +525,8 @@ static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
523 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n"); 525 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
524} 526}
525 527
526static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host) 528static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
529 struct mmc_command *cmd)
527{ 530{
528 unsigned int irq_mask; 531 unsigned int irq_mask;
529 532
@@ -532,6 +535,10 @@ static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host)
532 else 535 else
533 irq_mask = INT_EN_MASK; 536 irq_mask = INT_EN_MASK;
534 537
538 /* Disable timeout for erases */
539 if (cmd->opcode == MMC_ERASE)
540 irq_mask &= ~DTO_ENABLE;
541
535 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR); 542 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
536 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask); 543 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
537 OMAP_HSMMC_WRITE(host->base, IE, irq_mask); 544 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
@@ -782,7 +789,7 @@ omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
782 mmc_hostname(host->mmc), cmd->opcode, cmd->arg); 789 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
783 host->cmd = cmd; 790 host->cmd = cmd;
784 791
785 omap_hsmmc_enable_irq(host); 792 omap_hsmmc_enable_irq(host, cmd);
786 793
787 host->response_busy = 0; 794 host->response_busy = 0;
788 if (cmd->flags & MMC_RSP_PRESENT) { 795 if (cmd->flags & MMC_RSP_PRESENT) {
@@ -1273,8 +1280,11 @@ static void omap_hsmmc_dma_cb(int lch, u16 ch_status, void *cb_data)
1273 struct mmc_data *data = host->mrq->data; 1280 struct mmc_data *data = host->mrq->data;
1274 int dma_ch, req_in_progress; 1281 int dma_ch, req_in_progress;
1275 1282
1276 if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ) 1283 if (!(ch_status & OMAP_DMA_BLOCK_IRQ)) {
1277 dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n"); 1284 dev_warn(mmc_dev(host->mmc), "unexpected dma status %x\n",
1285 ch_status);
1286 return;
1287 }
1278 1288
1279 spin_lock(&host->irq_lock); 1289 spin_lock(&host->irq_lock);
1280 if (host->dma_ch < 0) { 1290 if (host->dma_ch < 0) {
@@ -1598,6 +1608,14 @@ static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1598 return mmc_slot(host).get_ro(host->dev, 0); 1608 return mmc_slot(host).get_ro(host->dev, 0);
1599} 1609}
1600 1610
1611static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1612{
1613 struct omap_hsmmc_host *host = mmc_priv(mmc);
1614
1615 if (mmc_slot(host).init_card)
1616 mmc_slot(host).init_card(card);
1617}
1618
1601static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host) 1619static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
1602{ 1620{
1603 u32 hctl, capa, value; 1621 u32 hctl, capa, value;
@@ -1869,6 +1887,7 @@ static const struct mmc_host_ops omap_hsmmc_ops = {
1869 .set_ios = omap_hsmmc_set_ios, 1887 .set_ios = omap_hsmmc_set_ios,
1870 .get_cd = omap_hsmmc_get_cd, 1888 .get_cd = omap_hsmmc_get_cd,
1871 .get_ro = omap_hsmmc_get_ro, 1889 .get_ro = omap_hsmmc_get_ro,
1890 .init_card = omap_hsmmc_init_card,
1872 /* NYET -- enable_sdio_irq */ 1891 /* NYET -- enable_sdio_irq */
1873}; 1892};
1874 1893
@@ -1879,6 +1898,7 @@ static const struct mmc_host_ops omap_hsmmc_ps_ops = {
1879 .set_ios = omap_hsmmc_set_ios, 1898 .set_ios = omap_hsmmc_set_ios,
1880 .get_cd = omap_hsmmc_get_cd, 1899 .get_cd = omap_hsmmc_get_cd,
1881 .get_ro = omap_hsmmc_get_ro, 1900 .get_ro = omap_hsmmc_get_ro,
1901 .init_card = omap_hsmmc_init_card,
1882 /* NYET -- enable_sdio_irq */ 1902 /* NYET -- enable_sdio_irq */
1883}; 1903};
1884 1904
@@ -2094,12 +2114,25 @@ static int __init omap_hsmmc_probe(struct platform_device *pdev)
2094 mmc->max_seg_size = mmc->max_req_size; 2114 mmc->max_seg_size = mmc->max_req_size;
2095 2115
2096 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED | 2116 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
2097 MMC_CAP_WAIT_WHILE_BUSY; 2117 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
2098 2118
2099 if (mmc_slot(host).wires >= 8) 2119 switch (mmc_slot(host).wires) {
2120 case 8:
2100 mmc->caps |= MMC_CAP_8_BIT_DATA; 2121 mmc->caps |= MMC_CAP_8_BIT_DATA;
2101 else if (mmc_slot(host).wires >= 4) 2122 /* Fall through */
2123 case 4:
2102 mmc->caps |= MMC_CAP_4_BIT_DATA; 2124 mmc->caps |= MMC_CAP_4_BIT_DATA;
2125 break;
2126 case 1:
2127 /* Nothing to crib here */
2128 case 0:
2129 /* Assuming nothing was given by board, Core use's 1-Bit */
2130 break;
2131 default:
2132 /* Completely unexpected.. Core goes with 1-Bit Width */
2133 dev_crit(mmc_dev(host->mmc), "Invalid width %d\n used!"
2134 "using 1 instead\n", mmc_slot(host).wires);
2135 }
2103 2136
2104 if (mmc_slot(host).nonremovable) 2137 if (mmc_slot(host).nonremovable)
2105 mmc->caps |= MMC_CAP_NONREMOVABLE; 2138 mmc->caps |= MMC_CAP_NONREMOVABLE;
diff --git a/drivers/mmc/host/sdhci-cns3xxx.c b/drivers/mmc/host/sdhci-cns3xxx.c
new file mode 100644
index 000000000000..b7050b380d5f
--- /dev/null
+++ b/drivers/mmc/host/sdhci-cns3xxx.c
@@ -0,0 +1,97 @@
1/*
2 * SDHCI support for CNS3xxx SoC
3 *
4 * Copyright 2008 Cavium Networks
5 * Copyright 2010 MontaVista Software, LLC.
6 *
7 * Authors: Scott Shu
8 * Anton Vorontsov <avorontsov@mvista.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/delay.h>
16#include <linux/device.h>
17#include <linux/mmc/host.h>
18#include <linux/sdhci-pltfm.h>
19#include <mach/cns3xxx.h>
20#include "sdhci.h"
21#include "sdhci-pltfm.h"
22
23static unsigned int sdhci_cns3xxx_get_max_clk(struct sdhci_host *host)
24{
25 return 150000000;
26}
27
28static void sdhci_cns3xxx_set_clock(struct sdhci_host *host, unsigned int clock)
29{
30 struct device *dev = mmc_dev(host->mmc);
31 int div = 1;
32 u16 clk;
33 unsigned long timeout;
34
35 if (clock == host->clock)
36 return;
37
38 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
39
40 if (clock == 0)
41 goto out;
42
43 while (host->max_clk / div > clock) {
44 /*
45 * On CNS3xxx divider grows linearly up to 4, and then
46 * exponentially up to 256.
47 */
48 if (div < 4)
49 div += 1;
50 else if (div < 256)
51 div *= 2;
52 else
53 break;
54 }
55
56 dev_dbg(dev, "desired SD clock: %d, actual: %d\n",
57 clock, host->max_clk / div);
58
59 /* Divide by 3 is special. */
60 if (div != 3)
61 div >>= 1;
62
63 clk = div << SDHCI_DIVIDER_SHIFT;
64 clk |= SDHCI_CLOCK_INT_EN;
65 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
66
67 timeout = 20;
68 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
69 & SDHCI_CLOCK_INT_STABLE)) {
70 if (timeout == 0) {
71 dev_warn(dev, "clock is unstable");
72 break;
73 }
74 timeout--;
75 mdelay(1);
76 }
77
78 clk |= SDHCI_CLOCK_CARD_EN;
79 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
80out:
81 host->clock = clock;
82}
83
84static struct sdhci_ops sdhci_cns3xxx_ops = {
85 .get_max_clock = sdhci_cns3xxx_get_max_clk,
86 .set_clock = sdhci_cns3xxx_set_clock,
87};
88
89struct sdhci_pltfm_data sdhci_cns3xxx_pdata = {
90 .ops = &sdhci_cns3xxx_ops,
91 .quirks = SDHCI_QUIRK_BROKEN_DMA |
92 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
93 SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
94 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
95 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
96 SDHCI_QUIRK_NONSTANDARD_CLOCK,
97};
diff --git a/drivers/mmc/host/sdhci-of-core.c b/drivers/mmc/host/sdhci-of-core.c
index a2e9820cd42f..c51b71174c1d 100644
--- a/drivers/mmc/host/sdhci-of-core.c
+++ b/drivers/mmc/host/sdhci-of-core.c
@@ -85,14 +85,14 @@ void sdhci_be32bs_writeb(struct sdhci_host *host, u8 val, int reg)
85 85
86#ifdef CONFIG_PM 86#ifdef CONFIG_PM
87 87
88static int sdhci_of_suspend(struct of_device *ofdev, pm_message_t state) 88static int sdhci_of_suspend(struct platform_device *ofdev, pm_message_t state)
89{ 89{
90 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev); 90 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
91 91
92 return mmc_suspend_host(host->mmc); 92 return mmc_suspend_host(host->mmc);
93} 93}
94 94
95static int sdhci_of_resume(struct of_device *ofdev) 95static int sdhci_of_resume(struct platform_device *ofdev)
96{ 96{
97 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev); 97 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
98 98
@@ -115,7 +115,7 @@ static bool __devinit sdhci_of_wp_inverted(struct device_node *np)
115 return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds); 115 return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
116} 116}
117 117
118static int __devinit sdhci_of_probe(struct of_device *ofdev, 118static int __devinit sdhci_of_probe(struct platform_device *ofdev,
119 const struct of_device_id *match) 119 const struct of_device_id *match)
120{ 120{
121 struct device_node *np = ofdev->dev.of_node; 121 struct device_node *np = ofdev->dev.of_node;
@@ -154,6 +154,10 @@ static int __devinit sdhci_of_probe(struct of_device *ofdev,
154 host->ops = &sdhci_of_data->ops; 154 host->ops = &sdhci_of_data->ops;
155 } 155 }
156 156
157 if (of_get_property(np, "sdhci,auto-cmd12", NULL))
158 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
159
160
157 if (of_get_property(np, "sdhci,1-bit-only", NULL)) 161 if (of_get_property(np, "sdhci,1-bit-only", NULL))
158 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA; 162 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
159 163
@@ -179,7 +183,7 @@ err_addr_map:
179 return ret; 183 return ret;
180} 184}
181 185
182static int __devexit sdhci_of_remove(struct of_device *ofdev) 186static int __devexit sdhci_of_remove(struct platform_device *ofdev)
183{ 187{
184 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev); 188 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
185 189
diff --git a/drivers/mmc/host/sdhci-pci.c b/drivers/mmc/host/sdhci-pci.c
index 65483fdea45b..e8aa99deae9a 100644
--- a/drivers/mmc/host/sdhci-pci.c
+++ b/drivers/mmc/host/sdhci-pci.c
@@ -17,6 +17,7 @@
17#include <linux/pci.h> 17#include <linux/pci.h>
18#include <linux/dma-mapping.h> 18#include <linux/dma-mapping.h>
19#include <linux/slab.h> 19#include <linux/slab.h>
20#include <linux/device.h>
20 21
21#include <linux/mmc/host.h> 22#include <linux/mmc/host.h>
22 23
@@ -84,7 +85,30 @@ static int ricoh_probe(struct sdhci_pci_chip *chip)
84 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG || 85 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG ||
85 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY) 86 chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SONY)
86 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET; 87 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
88 return 0;
89}
90
91static int ricoh_mmc_probe_slot(struct sdhci_pci_slot *slot)
92{
93 slot->host->caps =
94 ((0x21 << SDHCI_TIMEOUT_CLK_SHIFT)
95 & SDHCI_TIMEOUT_CLK_MASK) |
96
97 ((0x21 << SDHCI_CLOCK_BASE_SHIFT)
98 & SDHCI_CLOCK_BASE_MASK) |
87 99
100 SDHCI_TIMEOUT_CLK_UNIT |
101 SDHCI_CAN_VDD_330 |
102 SDHCI_CAN_DO_SDMA;
103 return 0;
104}
105
106static int ricoh_mmc_resume(struct sdhci_pci_chip *chip)
107{
108 /* Apply a delay to allow controller to settle */
109 /* Otherwise it becomes confused if card state changed
110 during suspend */
111 msleep(500);
88 return 0; 112 return 0;
89} 113}
90 114
@@ -95,6 +119,15 @@ static const struct sdhci_pci_fixes sdhci_ricoh = {
95 SDHCI_QUIRK_CLOCK_BEFORE_RESET, 119 SDHCI_QUIRK_CLOCK_BEFORE_RESET,
96}; 120};
97 121
122static const struct sdhci_pci_fixes sdhci_ricoh_mmc = {
123 .probe_slot = ricoh_mmc_probe_slot,
124 .resume = ricoh_mmc_resume,
125 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR |
126 SDHCI_QUIRK_CLOCK_BEFORE_RESET |
127 SDHCI_QUIRK_NO_CARD_NO_RESET |
128 SDHCI_QUIRK_MISSING_CAPS
129};
130
98static const struct sdhci_pci_fixes sdhci_ene_712 = { 131static const struct sdhci_pci_fixes sdhci_ene_712 = {
99 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE | 132 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
100 SDHCI_QUIRK_BROKEN_DMA, 133 SDHCI_QUIRK_BROKEN_DMA,
@@ -374,6 +407,22 @@ static const struct pci_device_id pci_ids[] __devinitdata = {
374 }, 407 },
375 408
376 { 409 {
410 .vendor = PCI_VENDOR_ID_RICOH,
411 .device = 0x843,
412 .subvendor = PCI_ANY_ID,
413 .subdevice = PCI_ANY_ID,
414 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
415 },
416
417 {
418 .vendor = PCI_VENDOR_ID_RICOH,
419 .device = 0xe822,
420 .subvendor = PCI_ANY_ID,
421 .subdevice = PCI_ANY_ID,
422 .driver_data = (kernel_ulong_t)&sdhci_ricoh_mmc,
423 },
424
425 {
377 .vendor = PCI_VENDOR_ID_ENE, 426 .vendor = PCI_VENDOR_ID_ENE,
378 .device = PCI_DEVICE_ID_ENE_CB712_SD, 427 .device = PCI_DEVICE_ID_ENE_CB712_SD,
379 .subvendor = PCI_ANY_ID, 428 .subvendor = PCI_ANY_ID,
diff --git a/drivers/mmc/host/sdhci-pltfm.c b/drivers/mmc/host/sdhci-pltfm.c
index b6ee0d719698..e045e3c61dde 100644
--- a/drivers/mmc/host/sdhci-pltfm.c
+++ b/drivers/mmc/host/sdhci-pltfm.c
@@ -24,6 +24,7 @@
24 24
25#include <linux/delay.h> 25#include <linux/delay.h>
26#include <linux/highmem.h> 26#include <linux/highmem.h>
27#include <linux/mod_devicetable.h>
27#include <linux/platform_device.h> 28#include <linux/platform_device.h>
28 29
29#include <linux/mmc/host.h> 30#include <linux/mmc/host.h>
@@ -32,6 +33,7 @@
32#include <linux/sdhci-pltfm.h> 33#include <linux/sdhci-pltfm.h>
33 34
34#include "sdhci.h" 35#include "sdhci.h"
36#include "sdhci-pltfm.h"
35 37
36/*****************************************************************************\ 38/*****************************************************************************\
37 * * 39 * *
@@ -51,10 +53,14 @@ static struct sdhci_ops sdhci_pltfm_ops = {
51static int __devinit sdhci_pltfm_probe(struct platform_device *pdev) 53static int __devinit sdhci_pltfm_probe(struct platform_device *pdev)
52{ 54{
53 struct sdhci_pltfm_data *pdata = pdev->dev.platform_data; 55 struct sdhci_pltfm_data *pdata = pdev->dev.platform_data;
56 const struct platform_device_id *platid = platform_get_device_id(pdev);
54 struct sdhci_host *host; 57 struct sdhci_host *host;
55 struct resource *iomem; 58 struct resource *iomem;
56 int ret; 59 int ret;
57 60
61 if (!pdata && platid && platid->driver_data)
62 pdata = (void *)platid->driver_data;
63
58 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 64 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
59 if (!iomem) { 65 if (!iomem) {
60 ret = -ENOMEM; 66 ret = -ENOMEM;
@@ -150,6 +156,15 @@ static int __devexit sdhci_pltfm_remove(struct platform_device *pdev)
150 return 0; 156 return 0;
151} 157}
152 158
159static const struct platform_device_id sdhci_pltfm_ids[] = {
160 { "sdhci", },
161#ifdef CONFIG_MMC_SDHCI_CNS3XXX
162 { "sdhci-cns3xxx", (kernel_ulong_t)&sdhci_cns3xxx_pdata },
163#endif
164 { },
165};
166MODULE_DEVICE_TABLE(platform, sdhci_pltfm_ids);
167
153static struct platform_driver sdhci_pltfm_driver = { 168static struct platform_driver sdhci_pltfm_driver = {
154 .driver = { 169 .driver = {
155 .name = "sdhci", 170 .name = "sdhci",
@@ -157,6 +172,7 @@ static struct platform_driver sdhci_pltfm_driver = {
157 }, 172 },
158 .probe = sdhci_pltfm_probe, 173 .probe = sdhci_pltfm_probe,
159 .remove = __devexit_p(sdhci_pltfm_remove), 174 .remove = __devexit_p(sdhci_pltfm_remove),
175 .id_table = sdhci_pltfm_ids,
160}; 176};
161 177
162/*****************************************************************************\ 178/*****************************************************************************\
@@ -181,4 +197,3 @@ module_exit(sdhci_drv_exit);
181MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver"); 197MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver");
182MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>"); 198MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
183MODULE_LICENSE("GPL v2"); 199MODULE_LICENSE("GPL v2");
184MODULE_ALIAS("platform:sdhci");
diff --git a/drivers/mmc/host/sdhci-pltfm.h b/drivers/mmc/host/sdhci-pltfm.h
new file mode 100644
index 000000000000..900f32902f73
--- /dev/null
+++ b/drivers/mmc/host/sdhci-pltfm.h
@@ -0,0 +1,18 @@
1/*
2 * Copyright 2010 MontaVista Software, LLC.
3 *
4 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
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 version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#ifndef _DRIVERS_MMC_SDHCI_PLTFM_H
12#define _DRIVERS_MMC_SDHCI_PLTFM_H
13
14#include <linux/sdhci-pltfm.h>
15
16extern struct sdhci_pltfm_data sdhci_cns3xxx_pdata;
17
18#endif /* _DRIVERS_MMC_SDHCI_PLTFM_H */
diff --git a/drivers/mmc/host/sdhci-s3c.c b/drivers/mmc/host/sdhci-s3c.c
index ad30f074ee15..0a7f2614c6f0 100644
--- a/drivers/mmc/host/sdhci-s3c.c
+++ b/drivers/mmc/host/sdhci-s3c.c
@@ -18,6 +18,7 @@
18#include <linux/slab.h> 18#include <linux/slab.h>
19#include <linux/clk.h> 19#include <linux/clk.h>
20#include <linux/io.h> 20#include <linux/io.h>
21#include <linux/gpio.h>
21 22
22#include <linux/mmc/host.h> 23#include <linux/mmc/host.h>
23 24
@@ -44,6 +45,8 @@ struct sdhci_s3c {
44 struct resource *ioarea; 45 struct resource *ioarea;
45 struct s3c_sdhci_platdata *pdata; 46 struct s3c_sdhci_platdata *pdata;
46 unsigned int cur_clk; 47 unsigned int cur_clk;
48 int ext_cd_irq;
49 int ext_cd_gpio;
47 50
48 struct clk *clk_io; 51 struct clk *clk_io;
49 struct clk *clk_bus[MAX_BUS_CLK]; 52 struct clk *clk_bus[MAX_BUS_CLK];
@@ -110,11 +113,6 @@ static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
110 return max; 113 return max;
111} 114}
112 115
113static unsigned int sdhci_s3c_get_timeout_clk(struct sdhci_host *host)
114{
115 return sdhci_s3c_get_max_clk(host) / 1000000;
116}
117
118/** 116/**
119 * sdhci_s3c_consider_clock - consider one the bus clocks for current setting 117 * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
120 * @ourhost: Our SDHCI instance. 118 * @ourhost: Our SDHCI instance.
@@ -188,7 +186,6 @@ static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
188 186
189 ourhost->cur_clk = best_src; 187 ourhost->cur_clk = best_src;
190 host->max_clk = clk_get_rate(clk); 188 host->max_clk = clk_get_rate(clk);
191 host->timeout_clk = sdhci_s3c_get_timeout_clk(host);
192 189
193 ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2); 190 ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
194 ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK; 191 ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
@@ -209,12 +206,93 @@ static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
209 } 206 }
210} 207}
211 208
209/**
210 * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
211 * @host: The SDHCI host being queried
212 *
213 * To init mmc host properly a minimal clock value is needed. For high system
214 * bus clock's values the standard formula gives values out of allowed range.
215 * The clock still can be set to lower values, if clock source other then
216 * system bus is selected.
217*/
218static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
219{
220 struct sdhci_s3c *ourhost = to_s3c(host);
221 unsigned int delta, min = UINT_MAX;
222 int src;
223
224 for (src = 0; src < MAX_BUS_CLK; src++) {
225 delta = sdhci_s3c_consider_clock(ourhost, src, 0);
226 if (delta == UINT_MAX)
227 continue;
228 /* delta is a negative value in this case */
229 if (-delta < min)
230 min = -delta;
231 }
232 return min;
233}
234
212static struct sdhci_ops sdhci_s3c_ops = { 235static struct sdhci_ops sdhci_s3c_ops = {
213 .get_max_clock = sdhci_s3c_get_max_clk, 236 .get_max_clock = sdhci_s3c_get_max_clk,
214 .get_timeout_clock = sdhci_s3c_get_timeout_clk,
215 .set_clock = sdhci_s3c_set_clock, 237 .set_clock = sdhci_s3c_set_clock,
238 .get_min_clock = sdhci_s3c_get_min_clock,
216}; 239};
217 240
241static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
242{
243 struct sdhci_host *host = platform_get_drvdata(dev);
244 if (host) {
245 mutex_lock(&host->lock);
246 if (state) {
247 dev_dbg(&dev->dev, "card inserted.\n");
248 host->flags &= ~SDHCI_DEVICE_DEAD;
249 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
250 } else {
251 dev_dbg(&dev->dev, "card removed.\n");
252 host->flags |= SDHCI_DEVICE_DEAD;
253 host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
254 }
255 sdhci_card_detect(host);
256 mutex_unlock(&host->lock);
257 }
258}
259
260static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
261{
262 struct sdhci_s3c *sc = dev_id;
263 int status = gpio_get_value(sc->ext_cd_gpio);
264 if (sc->pdata->ext_cd_gpio_invert)
265 status = !status;
266 sdhci_s3c_notify_change(sc->pdev, status);
267 return IRQ_HANDLED;
268}
269
270static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
271{
272 struct s3c_sdhci_platdata *pdata = sc->pdata;
273 struct device *dev = &sc->pdev->dev;
274
275 if (gpio_request(pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
276 sc->ext_cd_gpio = pdata->ext_cd_gpio;
277 sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
278 if (sc->ext_cd_irq &&
279 request_threaded_irq(sc->ext_cd_irq, NULL,
280 sdhci_s3c_gpio_card_detect_thread,
281 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
282 dev_name(dev), sc) == 0) {
283 int status = gpio_get_value(sc->ext_cd_gpio);
284 if (pdata->ext_cd_gpio_invert)
285 status = !status;
286 sdhci_s3c_notify_change(sc->pdev, status);
287 } else {
288 dev_warn(dev, "cannot request irq for card detect\n");
289 sc->ext_cd_irq = 0;
290 }
291 } else {
292 dev_err(dev, "cannot request gpio for card detect\n");
293 }
294}
295
218static int __devinit sdhci_s3c_probe(struct platform_device *pdev) 296static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
219{ 297{
220 struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data; 298 struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data;
@@ -252,6 +330,7 @@ static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
252 sc->host = host; 330 sc->host = host;
253 sc->pdev = pdev; 331 sc->pdev = pdev;
254 sc->pdata = pdata; 332 sc->pdata = pdata;
333 sc->ext_cd_gpio = -1; /* invalid gpio number */
255 334
256 platform_set_drvdata(pdev, host); 335 platform_set_drvdata(pdev, host);
257 336
@@ -318,6 +397,7 @@ static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
318 397
319 /* Setup quirks for the controller */ 398 /* Setup quirks for the controller */
320 host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC; 399 host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
400 host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
321 401
322#ifndef CONFIG_MMC_SDHCI_S3C_DMA 402#ifndef CONFIG_MMC_SDHCI_S3C_DMA
323 403
@@ -332,15 +412,34 @@ static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
332 * SDHCI block, or a missing configuration that needs to be set. */ 412 * SDHCI block, or a missing configuration that needs to be set. */
333 host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ; 413 host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
334 414
415 if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
416 pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
417 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
418
419 if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
420 host->mmc->caps = MMC_CAP_NONREMOVABLE;
421
335 host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR | 422 host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
336 SDHCI_QUIRK_32BIT_DMA_SIZE); 423 SDHCI_QUIRK_32BIT_DMA_SIZE);
337 424
425 /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
426 host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
427
338 ret = sdhci_add_host(host); 428 ret = sdhci_add_host(host);
339 if (ret) { 429 if (ret) {
340 dev_err(dev, "sdhci_add_host() failed\n"); 430 dev_err(dev, "sdhci_add_host() failed\n");
341 goto err_add_host; 431 goto err_add_host;
342 } 432 }
343 433
434 /* The following two methods of card detection might call
435 sdhci_s3c_notify_change() immediately, so they can be called
436 only after sdhci_add_host(). Setup errors are ignored. */
437 if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
438 pdata->ext_cd_init(&sdhci_s3c_notify_change);
439 if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
440 gpio_is_valid(pdata->ext_cd_gpio))
441 sdhci_s3c_setup_card_detect_gpio(sc);
442
344 return 0; 443 return 0;
345 444
346 err_add_host: 445 err_add_host:
@@ -365,10 +464,20 @@ static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
365 464
366static int __devexit sdhci_s3c_remove(struct platform_device *pdev) 465static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
367{ 466{
467 struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data;
368 struct sdhci_host *host = platform_get_drvdata(pdev); 468 struct sdhci_host *host = platform_get_drvdata(pdev);
369 struct sdhci_s3c *sc = sdhci_priv(host); 469 struct sdhci_s3c *sc = sdhci_priv(host);
370 int ptr; 470 int ptr;
371 471
472 if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
473 pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
474
475 if (sc->ext_cd_irq)
476 free_irq(sc->ext_cd_irq, sc);
477
478 if (gpio_is_valid(sc->ext_cd_gpio))
479 gpio_free(sc->ext_cd_gpio);
480
372 sdhci_remove_host(host, 1); 481 sdhci_remove_host(host, 1);
373 482
374 for (ptr = 0; ptr < 3; ptr++) { 483 for (ptr = 0; ptr < 3; ptr++) {
diff --git a/drivers/mmc/host/sdhci.c b/drivers/mmc/host/sdhci.c
index c6d1bd8d4ac4..785512133b50 100644
--- a/drivers/mmc/host/sdhci.c
+++ b/drivers/mmc/host/sdhci.c
@@ -19,6 +19,7 @@
19#include <linux/dma-mapping.h> 19#include <linux/dma-mapping.h>
20#include <linux/slab.h> 20#include <linux/slab.h>
21#include <linux/scatterlist.h> 21#include <linux/scatterlist.h>
22#include <linux/regulator/consumer.h>
22 23
23#include <linux/leds.h> 24#include <linux/leds.h>
24 25
@@ -817,8 +818,12 @@ static void sdhci_set_transfer_mode(struct sdhci_host *host,
817 WARN_ON(!host->data); 818 WARN_ON(!host->data);
818 819
819 mode = SDHCI_TRNS_BLK_CNT_EN; 820 mode = SDHCI_TRNS_BLK_CNT_EN;
820 if (data->blocks > 1) 821 if (data->blocks > 1) {
821 mode |= SDHCI_TRNS_MULTI; 822 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
823 mode |= SDHCI_TRNS_MULTI | SDHCI_TRNS_ACMD12;
824 else
825 mode |= SDHCI_TRNS_MULTI;
826 }
822 if (data->flags & MMC_DATA_READ) 827 if (data->flags & MMC_DATA_READ)
823 mode |= SDHCI_TRNS_READ; 828 mode |= SDHCI_TRNS_READ;
824 if (host->flags & SDHCI_REQ_USE_DMA) 829 if (host->flags & SDHCI_REQ_USE_DMA)
@@ -1108,6 +1113,12 @@ static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1108#ifndef SDHCI_USE_LEDS_CLASS 1113#ifndef SDHCI_USE_LEDS_CLASS
1109 sdhci_activate_led(host); 1114 sdhci_activate_led(host);
1110#endif 1115#endif
1116 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
1117 if (mrq->stop) {
1118 mrq->data->stop = NULL;
1119 mrq->stop = NULL;
1120 }
1121 }
1111 1122
1112 host->mrq = mrq; 1123 host->mrq = mrq;
1113 1124
@@ -1159,6 +1170,11 @@ static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1159 1170
1160 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); 1171 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1161 1172
1173 if (ios->bus_width == MMC_BUS_WIDTH_8)
1174 ctrl |= SDHCI_CTRL_8BITBUS;
1175 else
1176 ctrl &= ~SDHCI_CTRL_8BITBUS;
1177
1162 if (ios->bus_width == MMC_BUS_WIDTH_4) 1178 if (ios->bus_width == MMC_BUS_WIDTH_4)
1163 ctrl |= SDHCI_CTRL_4BITBUS; 1179 ctrl |= SDHCI_CTRL_4BITBUS;
1164 else 1180 else
@@ -1603,7 +1619,10 @@ int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
1603 1619
1604 free_irq(host->irq, host); 1620 free_irq(host->irq, host);
1605 1621
1606 return 0; 1622 if (host->vmmc)
1623 ret = regulator_disable(host->vmmc);
1624
1625 return ret;
1607} 1626}
1608 1627
1609EXPORT_SYMBOL_GPL(sdhci_suspend_host); 1628EXPORT_SYMBOL_GPL(sdhci_suspend_host);
@@ -1612,6 +1631,13 @@ int sdhci_resume_host(struct sdhci_host *host)
1612{ 1631{
1613 int ret; 1632 int ret;
1614 1633
1634 if (host->vmmc) {
1635 int ret = regulator_enable(host->vmmc);
1636 if (ret)
1637 return ret;
1638 }
1639
1640
1615 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { 1641 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
1616 if (host->ops->enable_dma) 1642 if (host->ops->enable_dma)
1617 host->ops->enable_dma(host); 1643 host->ops->enable_dma(host);
@@ -1687,7 +1713,8 @@ int sdhci_add_host(struct sdhci_host *host)
1687 host->version); 1713 host->version);
1688 } 1714 }
1689 1715
1690 caps = sdhci_readl(host, SDHCI_CAPABILITIES); 1716 caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
1717 sdhci_readl(host, SDHCI_CAPABILITIES);
1691 1718
1692 if (host->quirks & SDHCI_QUIRK_FORCE_DMA) 1719 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
1693 host->flags |= SDHCI_USE_SDMA; 1720 host->flags |= SDHCI_USE_SDMA;
@@ -1785,13 +1812,12 @@ int sdhci_add_host(struct sdhci_host *host)
1785 * Set host parameters. 1812 * Set host parameters.
1786 */ 1813 */
1787 mmc->ops = &sdhci_ops; 1814 mmc->ops = &sdhci_ops;
1788 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK && 1815 if (host->ops->get_min_clock)
1789 host->ops->set_clock && host->ops->get_min_clock)
1790 mmc->f_min = host->ops->get_min_clock(host); 1816 mmc->f_min = host->ops->get_min_clock(host);
1791 else 1817 else
1792 mmc->f_min = host->max_clk / 256; 1818 mmc->f_min = host->max_clk / 256;
1793 mmc->f_max = host->max_clk; 1819 mmc->f_max = host->max_clk;
1794 mmc->caps = MMC_CAP_SDIO_IRQ; 1820 mmc->caps |= MMC_CAP_SDIO_IRQ;
1795 1821
1796 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA)) 1822 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
1797 mmc->caps |= MMC_CAP_4_BIT_DATA; 1823 mmc->caps |= MMC_CAP_4_BIT_DATA;
@@ -1884,6 +1910,14 @@ int sdhci_add_host(struct sdhci_host *host)
1884 if (ret) 1910 if (ret)
1885 goto untasklet; 1911 goto untasklet;
1886 1912
1913 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
1914 if (IS_ERR(host->vmmc)) {
1915 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
1916 host->vmmc = NULL;
1917 } else {
1918 regulator_enable(host->vmmc);
1919 }
1920
1887 sdhci_init(host, 0); 1921 sdhci_init(host, 0);
1888 1922
1889#ifdef CONFIG_MMC_DEBUG 1923#ifdef CONFIG_MMC_DEBUG
@@ -1968,6 +2002,11 @@ void sdhci_remove_host(struct sdhci_host *host, int dead)
1968 tasklet_kill(&host->card_tasklet); 2002 tasklet_kill(&host->card_tasklet);
1969 tasklet_kill(&host->finish_tasklet); 2003 tasklet_kill(&host->finish_tasklet);
1970 2004
2005 if (host->vmmc) {
2006 regulator_disable(host->vmmc);
2007 regulator_put(host->vmmc);
2008 }
2009
1971 kfree(host->adma_desc); 2010 kfree(host->adma_desc);
1972 kfree(host->align_buffer); 2011 kfree(host->align_buffer);
1973 2012
diff --git a/drivers/mmc/host/sdhci.h b/drivers/mmc/host/sdhci.h
index c8468134adc9..036cfae76368 100644
--- a/drivers/mmc/host/sdhci.h
+++ b/drivers/mmc/host/sdhci.h
@@ -72,6 +72,7 @@
72#define SDHCI_CTRL_ADMA1 0x08 72#define SDHCI_CTRL_ADMA1 0x08
73#define SDHCI_CTRL_ADMA32 0x10 73#define SDHCI_CTRL_ADMA32 0x10
74#define SDHCI_CTRL_ADMA64 0x18 74#define SDHCI_CTRL_ADMA64 0x18
75#define SDHCI_CTRL_8BITBUS 0x20
75 76
76#define SDHCI_POWER_CONTROL 0x29 77#define SDHCI_POWER_CONTROL 0x29
77#define SDHCI_POWER_ON 0x01 78#define SDHCI_POWER_ON 0x01
@@ -240,12 +241,18 @@ struct sdhci_host {
240#define SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN (1<<25) 241#define SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN (1<<25)
241/* Controller cannot support End Attribute in NOP ADMA descriptor */ 242/* Controller cannot support End Attribute in NOP ADMA descriptor */
242#define SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC (1<<26) 243#define SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC (1<<26)
244/* Controller is missing device caps. Use caps provided by host */
245#define SDHCI_QUIRK_MISSING_CAPS (1<<27)
246/* Controller uses Auto CMD12 command to stop the transfer */
247#define SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12 (1<<28)
243 248
244 int irq; /* Device IRQ */ 249 int irq; /* Device IRQ */
245 void __iomem * ioaddr; /* Mapped address */ 250 void __iomem * ioaddr; /* Mapped address */
246 251
247 const struct sdhci_ops *ops; /* Low level hw interface */ 252 const struct sdhci_ops *ops; /* Low level hw interface */
248 253
254 struct regulator *vmmc; /* Power regulator */
255
249 /* Internal data */ 256 /* Internal data */
250 struct mmc_host *mmc; /* MMC structure */ 257 struct mmc_host *mmc; /* MMC structure */
251 u64 dma_mask; /* custom DMA mask */ 258 u64 dma_mask; /* custom DMA mask */
@@ -292,6 +299,8 @@ struct sdhci_host {
292 299
293 struct timer_list timer; /* Timer for timeouts */ 300 struct timer_list timer; /* Timer for timeouts */
294 301
302 unsigned int caps; /* Alternative capabilities */
303
295 unsigned long private[0] ____cacheline_aligned; 304 unsigned long private[0] ____cacheline_aligned;
296}; 305};
297 306
@@ -407,6 +416,7 @@ static inline void *sdhci_priv(struct sdhci_host *host)
407 return (void *)host->private; 416 return (void *)host->private;
408} 417}
409 418
419extern void sdhci_card_detect(struct sdhci_host *host);
410extern int sdhci_add_host(struct sdhci_host *host); 420extern int sdhci_add_host(struct sdhci_host *host);
411extern void sdhci_remove_host(struct sdhci_host *host, int dead); 421extern void sdhci_remove_host(struct sdhci_host *host, int dead);
412 422
diff --git a/drivers/mmc/host/sdricoh_cs.c b/drivers/mmc/host/sdricoh_cs.c
index e7507af3856e..7aa65bb2af4a 100644
--- a/drivers/mmc/host/sdricoh_cs.c
+++ b/drivers/mmc/host/sdricoh_cs.c
@@ -30,7 +30,6 @@
30#include <linux/ioport.h> 30#include <linux/ioport.h>
31#include <linux/scatterlist.h> 31#include <linux/scatterlist.h>
32 32
33#include <pcmcia/cs_types.h>
34#include <pcmcia/cs.h> 33#include <pcmcia/cs.h>
35#include <pcmcia/cistpl.h> 34#include <pcmcia/cistpl.h>
36#include <pcmcia/ds.h> 35#include <pcmcia/ds.h>