aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHaavard Skinnemoen <haavard.skinnemoen@atmel.com>2008-09-17 14:53:55 -0400
committerHaavard Skinnemoen <haavard.skinnemoen@atmel.com>2008-10-05 14:39:21 -0400
commit965ebf33ea5afb6386f5b57cc71e6572253746b3 (patch)
treea4ee6bdce1a3a9c133c36a5463d12baa089591d9
parent6b918657b7431e4c5c953b8222ae2f4fc1b2576a (diff)
atmel-mci: support multiple mmc slots
The Atmel MCI controller can drive multiple cards through separate sets of pins, but only one at a time. This patch adds support for multiplexing access to the controller so that multiple card slots can be used as if they were hooked up to separate mmc controllers. The atmel-mci driver registers each slot as a separate mmc_host. Both access the same common controller state, but they also have some state on their own for card detection/write protect handling, and separate shadows of the MR and SDCR registers. When one of the slots receives a request from the mmc core, the common controller state is checked. If it's idle, the request is submitted immediately. If not, the request is added to a queue. When a request is done, the queue is checked and if there is a queued request, it is submitted before the completion callback is called. This patch also includes a few cleanups and fixes, including a locking overhaul. I had to change the locking extensively in any case, so I might as well try to get it right. The driver no longer takes any irq-safe locks, which may or may not improve the overall system performance. This patch also adds a bit of documentation of the internal data structures. Signed-off-by: Haavard Skinnemoen <haavard.skinnemoen@atmel.com>
-rw-r--r--arch/avr32/include/asm/atmel-mci.h8
-rw-r--r--drivers/mmc/host/atmel-mci.c922
2 files changed, 621 insertions, 309 deletions
diff --git a/arch/avr32/include/asm/atmel-mci.h b/arch/avr32/include/asm/atmel-mci.h
index d38c64ca41e8..5d5ae1295cfd 100644
--- a/arch/avr32/include/asm/atmel-mci.h
+++ b/arch/avr32/include/asm/atmel-mci.h
@@ -1,6 +1,8 @@
1#ifndef __ASM_AVR32_ATMEL_MCI_H 1#ifndef __ASM_AVR32_ATMEL_MCI_H
2#define __ASM_AVR32_ATMEL_MCI_H 2#define __ASM_AVR32_ATMEL_MCI_H
3 3
4#define ATMEL_MCI_MAX_NR_SLOTS 2
5
4/** 6/**
5 * struct mci_slot_pdata - board-specific per-slot configuration 7 * struct mci_slot_pdata - board-specific per-slot configuration
6 * @bus_width: Number of data lines wired up the slot 8 * @bus_width: Number of data lines wired up the slot
@@ -11,6 +13,10 @@
11 * set to 0. The other fields are ignored in this case. 13 * set to 0. The other fields are ignored in this case.
12 * 14 *
13 * Any pins that aren't available should be set to a negative value. 15 * Any pins that aren't available should be set to a negative value.
16 *
17 * Note that support for multiple slots is experimental -- some cards
18 * might get upset if we don't get the clock management exactly right.
19 * But in most cases, it should work just fine.
14 */ 20 */
15struct mci_slot_pdata { 21struct mci_slot_pdata {
16 unsigned int bus_width; 22 unsigned int bus_width;
@@ -23,7 +29,7 @@ struct mci_slot_pdata {
23 * @slot: Per-slot configuration data. 29 * @slot: Per-slot configuration data.
24 */ 30 */
25struct mci_platform_data { 31struct mci_platform_data {
26 struct mci_slot_pdata slot[2]; 32 struct mci_slot_pdata slot[ATMEL_MCI_MAX_NR_SLOTS];
27}; 33};
28 34
29#endif /* __ASM_AVR32_ATMEL_MCI_H */ 35#endif /* __ASM_AVR32_ATMEL_MCI_H */
diff --git a/drivers/mmc/host/atmel-mci.c b/drivers/mmc/host/atmel-mci.c
index 8170905a0401..d8ab35175a53 100644
--- a/drivers/mmc/host/atmel-mci.c
+++ b/drivers/mmc/host/atmel-mci.c
@@ -42,20 +42,86 @@ enum {
42}; 42};
43 43
44enum atmel_mci_state { 44enum atmel_mci_state {
45 STATE_SENDING_CMD = 0, 45 STATE_IDLE = 0,
46 STATE_SENDING_CMD,
46 STATE_SENDING_DATA, 47 STATE_SENDING_DATA,
47 STATE_DATA_BUSY, 48 STATE_DATA_BUSY,
48 STATE_SENDING_STOP, 49 STATE_SENDING_STOP,
49 STATE_DATA_ERROR, 50 STATE_DATA_ERROR,
50}; 51};
51 52
53/**
54 * struct atmel_mci - MMC controller state shared between all slots
55 * @lock: Spinlock protecting the queue and associated data.
56 * @regs: Pointer to MMIO registers.
57 * @sg: Scatterlist entry currently being processed by PIO code, if any.
58 * @pio_offset: Offset into the current scatterlist entry.
59 * @cur_slot: The slot which is currently using the controller.
60 * @mrq: The request currently being processed on @cur_slot,
61 * or NULL if the controller is idle.
62 * @cmd: The command currently being sent to the card, or NULL.
63 * @data: The data currently being transferred, or NULL if no data
64 * transfer is in progress.
65 * @cmd_status: Snapshot of SR taken upon completion of the current
66 * command. Only valid when EVENT_CMD_COMPLETE is pending.
67 * @data_status: Snapshot of SR taken upon completion of the current
68 * data transfer. Only valid when EVENT_DATA_COMPLETE or
69 * EVENT_DATA_ERROR is pending.
70 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
71 * to be sent.
72 * @tasklet: Tasklet running the request state machine.
73 * @pending_events: Bitmask of events flagged by the interrupt handler
74 * to be processed by the tasklet.
75 * @completed_events: Bitmask of events which the state machine has
76 * processed.
77 * @state: Tasklet state.
78 * @queue: List of slots waiting for access to the controller.
79 * @need_clock_update: Update the clock rate before the next request.
80 * @need_reset: Reset controller before next request.
81 * @mode_reg: Value of the MR register.
82 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
83 * rate and timeout calculations.
84 * @mapbase: Physical address of the MMIO registers.
85 * @mck: The peripheral bus clock hooked up to the MMC controller.
86 * @pdev: Platform device associated with the MMC controller.
87 * @slot: Slots sharing this MMC controller.
88 *
89 * Locking
90 * =======
91 *
92 * @lock is a softirq-safe spinlock protecting @queue as well as
93 * @cur_slot, @mrq and @state. These must always be updated
94 * at the same time while holding @lock.
95 *
96 * @lock also protects mode_reg and need_clock_update since these are
97 * used to synchronize mode register updates with the queue
98 * processing.
99 *
100 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
101 * and must always be written at the same time as the slot is added to
102 * @queue.
103 *
104 * @pending_events and @completed_events are accessed using atomic bit
105 * operations, so they don't need any locking.
106 *
107 * None of the fields touched by the interrupt handler need any
108 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
109 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
110 * interrupts must be disabled and @data_status updated with a
111 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
112 * CMDRDY interupt must be disabled and @cmd_status updated with a
113 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
114 * bytes_xfered field of @data must be written. This is ensured by
115 * using barriers.
116 */
52struct atmel_mci { 117struct atmel_mci {
53 struct mmc_host *mmc; 118 spinlock_t lock;
54 void __iomem *regs; 119 void __iomem *regs;
55 120
56 struct scatterlist *sg; 121 struct scatterlist *sg;
57 unsigned int pio_offset; 122 unsigned int pio_offset;
58 123
124 struct atmel_mci_slot *cur_slot;
59 struct mmc_request *mrq; 125 struct mmc_request *mrq;
60 struct mmc_command *cmd; 126 struct mmc_command *cmd;
61 struct mmc_data *data; 127 struct mmc_data *data;
@@ -64,25 +130,59 @@ struct atmel_mci {
64 u32 data_status; 130 u32 data_status;
65 u32 stop_cmdr; 131 u32 stop_cmdr;
66 132
67 u32 mode_reg;
68 u32 sdc_reg;
69
70 struct tasklet_struct tasklet; 133 struct tasklet_struct tasklet;
71 unsigned long pending_events; 134 unsigned long pending_events;
72 unsigned long completed_events; 135 unsigned long completed_events;
73 enum atmel_mci_state state; 136 enum atmel_mci_state state;
137 struct list_head queue;
74 138
75 int present; 139 bool need_clock_update;
76 int detect_pin; 140 bool need_reset;
77 int wp_pin; 141 u32 mode_reg;
78
79 /* For detect pin debouncing */
80 struct timer_list detect_timer;
81
82 unsigned long bus_hz; 142 unsigned long bus_hz;
83 unsigned long mapbase; 143 unsigned long mapbase;
84 struct clk *mck; 144 struct clk *mck;
85 struct platform_device *pdev; 145 struct platform_device *pdev;
146
147 struct atmel_mci_slot *slot[ATMEL_MCI_MAX_NR_SLOTS];
148};
149
150/**
151 * struct atmel_mci_slot - MMC slot state
152 * @mmc: The mmc_host representing this slot.
153 * @host: The MMC controller this slot is using.
154 * @sdc_reg: Value of SDCR to be written before using this slot.
155 * @mrq: mmc_request currently being processed or waiting to be
156 * processed, or NULL when the slot is idle.
157 * @queue_node: List node for placing this node in the @queue list of
158 * &struct atmel_mci.
159 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
160 * @flags: Random state bits associated with the slot.
161 * @detect_pin: GPIO pin used for card detection, or negative if not
162 * available.
163 * @wp_pin: GPIO pin used for card write protect sending, or negative
164 * if not available.
165 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
166 */
167struct atmel_mci_slot {
168 struct mmc_host *mmc;
169 struct atmel_mci *host;
170
171 u32 sdc_reg;
172
173 struct mmc_request *mrq;
174 struct list_head queue_node;
175
176 unsigned int clock;
177 unsigned long flags;
178#define ATMCI_CARD_PRESENT 0
179#define ATMCI_CARD_NEED_INIT 1
180#define ATMCI_SHUTDOWN 2
181
182 int detect_pin;
183 int wp_pin;
184
185 struct timer_list detect_timer;
86}; 186};
87 187
88#define atmci_test_and_clear_pending(host, event) \ 188#define atmci_test_and_clear_pending(host, event) \
@@ -98,14 +198,15 @@ struct atmel_mci {
98 */ 198 */
99static int atmci_req_show(struct seq_file *s, void *v) 199static int atmci_req_show(struct seq_file *s, void *v)
100{ 200{
101 struct atmel_mci *host = s->private; 201 struct atmel_mci_slot *slot = s->private;
102 struct mmc_request *mrq = host->mrq; 202 struct mmc_request *mrq;
103 struct mmc_command *cmd; 203 struct mmc_command *cmd;
104 struct mmc_command *stop; 204 struct mmc_command *stop;
105 struct mmc_data *data; 205 struct mmc_data *data;
106 206
107 /* Make sure we get a consistent snapshot */ 207 /* Make sure we get a consistent snapshot */
108 spin_lock_irq(&host->mmc->lock); 208 spin_lock_bh(&slot->host->lock);
209 mrq = slot->mrq;
109 210
110 if (mrq) { 211 if (mrq) {
111 cmd = mrq->cmd; 212 cmd = mrq->cmd;
@@ -130,7 +231,7 @@ static int atmci_req_show(struct seq_file *s, void *v)
130 stop->resp[2], stop->error); 231 stop->resp[2], stop->error);
131 } 232 }
132 233
133 spin_unlock_irq(&host->mmc->lock); 234 spin_unlock_bh(&slot->host->lock);
134 235
135 return 0; 236 return 0;
136} 237}
@@ -193,12 +294,16 @@ static int atmci_regs_show(struct seq_file *s, void *v)
193 if (!buf) 294 if (!buf)
194 return -ENOMEM; 295 return -ENOMEM;
195 296
196 /* Grab a more or less consistent snapshot */ 297 /*
197 spin_lock_irq(&host->mmc->lock); 298 * Grab a more or less consistent snapshot. Note that we're
299 * not disabling interrupts, so IMR and SR may not be
300 * consistent.
301 */
302 spin_lock_bh(&host->lock);
198 clk_enable(host->mck); 303 clk_enable(host->mck);
199 memcpy_fromio(buf, host->regs, MCI_REGS_SIZE); 304 memcpy_fromio(buf, host->regs, MCI_REGS_SIZE);
200 clk_disable(host->mck); 305 clk_disable(host->mck);
201 spin_unlock_irq(&host->mmc->lock); 306 spin_unlock_bh(&host->lock);
202 307
203 seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n", 308 seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
204 buf[MCI_MR / 4], 309 buf[MCI_MR / 4],
@@ -236,13 +341,13 @@ static const struct file_operations atmci_regs_fops = {
236 .release = single_release, 341 .release = single_release,
237}; 342};
238 343
239static void atmci_init_debugfs(struct atmel_mci *host) 344static void atmci_init_debugfs(struct atmel_mci_slot *slot)
240{ 345{
241 struct mmc_host *mmc; 346 struct mmc_host *mmc = slot->mmc;
242 struct dentry *root; 347 struct atmel_mci *host = slot->host;
243 struct dentry *node; 348 struct dentry *root;
349 struct dentry *node;
244 350
245 mmc = host->mmc;
246 root = mmc->debugfs_root; 351 root = mmc->debugfs_root;
247 if (!root) 352 if (!root)
248 return; 353 return;
@@ -254,7 +359,7 @@ static void atmci_init_debugfs(struct atmel_mci *host)
254 if (!node) 359 if (!node)
255 goto err; 360 goto err;
256 361
257 node = debugfs_create_file("req", S_IRUSR, root, host, &atmci_req_fops); 362 node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
258 if (!node) 363 if (!node)
259 goto err; 364 goto err;
260 365
@@ -275,8 +380,7 @@ static void atmci_init_debugfs(struct atmel_mci *host)
275 return; 380 return;
276 381
277err: 382err:
278 dev_err(&host->pdev->dev, 383 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
279 "failed to initialize debugfs for controller\n");
280} 384}
281 385
282static inline unsigned int ns_to_clocks(struct atmel_mci *host, 386static inline unsigned int ns_to_clocks(struct atmel_mci *host,
@@ -286,7 +390,7 @@ static inline unsigned int ns_to_clocks(struct atmel_mci *host,
286} 390}
287 391
288static void atmci_set_timeout(struct atmel_mci *host, 392static void atmci_set_timeout(struct atmel_mci *host,
289 struct mmc_data *data) 393 struct atmel_mci_slot *slot, struct mmc_data *data)
290{ 394{
291 static unsigned dtomul_to_shift[] = { 395 static unsigned dtomul_to_shift[] = {
292 0, 4, 7, 8, 10, 12, 16, 20 396 0, 4, 7, 8, 10, 12, 16, 20
@@ -309,7 +413,7 @@ static void atmci_set_timeout(struct atmel_mci *host,
309 dtocyc = 15; 413 dtocyc = 15;
310 } 414 }
311 415
312 dev_vdbg(&host->mmc->class_dev, "setting timeout to %u cycles\n", 416 dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
313 dtocyc << dtomul_to_shift[dtomul]); 417 dtocyc << dtomul_to_shift[dtomul]);
314 mci_writel(host, DTOR, (MCI_DTOMUL(dtomul) | MCI_DTOCYC(dtocyc))); 418 mci_writel(host, DTOR, (MCI_DTOMUL(dtomul) | MCI_DTOCYC(dtocyc)));
315} 419}
@@ -362,13 +466,12 @@ static u32 atmci_prepare_command(struct mmc_host *mmc,
362} 466}
363 467
364static void atmci_start_command(struct atmel_mci *host, 468static void atmci_start_command(struct atmel_mci *host,
365 struct mmc_command *cmd, 469 struct mmc_command *cmd, u32 cmd_flags)
366 u32 cmd_flags)
367{ 470{
368 WARN_ON(host->cmd); 471 WARN_ON(host->cmd);
369 host->cmd = cmd; 472 host->cmd = cmd;
370 473
371 dev_vdbg(&host->mmc->class_dev, 474 dev_vdbg(&host->pdev->dev,
372 "start command: ARGR=0x%08x CMDR=0x%08x\n", 475 "start command: ARGR=0x%08x CMDR=0x%08x\n",
373 cmd->arg, cmd_flags); 476 cmd->arg, cmd_flags);
374 477
@@ -376,32 +479,19 @@ static void atmci_start_command(struct atmel_mci *host,
376 mci_writel(host, CMDR, cmd_flags); 479 mci_writel(host, CMDR, cmd_flags);
377} 480}
378 481
379static void send_stop_cmd(struct mmc_host *mmc, struct mmc_data *data) 482static void send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
380{ 483{
381 struct atmel_mci *host = mmc_priv(mmc);
382
383 atmci_start_command(host, data->stop, host->stop_cmdr); 484 atmci_start_command(host, data->stop, host->stop_cmdr);
384 mci_writel(host, IER, MCI_CMDRDY); 485 mci_writel(host, IER, MCI_CMDRDY);
385} 486}
386 487
387static void atmci_request_end(struct mmc_host *mmc, struct mmc_request *mrq)
388{
389 struct atmel_mci *host = mmc_priv(mmc);
390
391 WARN_ON(host->cmd || host->data);
392 host->mrq = NULL;
393
394 mmc_request_done(mmc, mrq);
395}
396
397/* 488/*
398 * Returns a mask of interrupt flags to be enabled after the whole 489 * Returns a mask of interrupt flags to be enabled after the whole
399 * request has been prepared. 490 * request has been prepared.
400 */ 491 */
401static u32 atmci_submit_data(struct mmc_host *mmc, struct mmc_data *data) 492static u32 atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
402{ 493{
403 struct atmel_mci *host = mmc_priv(mmc); 494 u32 iflags;
404 u32 iflags;
405 495
406 data->error = -EINPROGRESS; 496 data->error = -EINPROGRESS;
407 497
@@ -409,10 +499,19 @@ static u32 atmci_submit_data(struct mmc_host *mmc, struct mmc_data *data)
409 host->sg = NULL; 499 host->sg = NULL;
410 host->data = data; 500 host->data = data;
411 501
412 dev_vdbg(&mmc->class_dev, "BLKR=0x%08x\n",
413 MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
414
415 iflags = ATMCI_DATA_ERROR_FLAGS; 502 iflags = ATMCI_DATA_ERROR_FLAGS;
503
504 /*
505 * Errata: MMC data write operation with less than 12
506 * bytes is impossible.
507 *
508 * Errata: MCI Transmit Data Register (TDR) FIFO
509 * corruption when length is not multiple of 4.
510 */
511 if (data->blocks * data->blksz < 12
512 || (data->blocks * data->blksz) & 3)
513 host->need_reset = true;
514
416 host->sg = data->sg; 515 host->sg = data->sg;
417 host->pio_offset = 0; 516 host->pio_offset = 0;
418 if (data->flags & MMC_DATA_READ) 517 if (data->flags & MMC_DATA_READ)
@@ -423,62 +522,62 @@ static u32 atmci_submit_data(struct mmc_host *mmc, struct mmc_data *data)
423 return iflags; 522 return iflags;
424} 523}
425 524
426static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq) 525static void atmci_start_request(struct atmel_mci *host,
526 struct atmel_mci_slot *slot)
427{ 527{
428 struct atmel_mci *host = mmc_priv(mmc); 528 struct mmc_request *mrq;
429 struct mmc_data *data;
430 struct mmc_command *cmd; 529 struct mmc_command *cmd;
530 struct mmc_data *data;
431 u32 iflags; 531 u32 iflags;
432 u32 cmdflags = 0; 532 u32 cmdflags;
433
434 iflags = mci_readl(host, IMR);
435 if (iflags)
436 dev_warn(&mmc->class_dev, "WARNING: IMR=0x%08x\n",
437 mci_readl(host, IMR));
438
439 WARN_ON(host->mrq != NULL);
440
441 /*
442 * We may "know" the card is gone even though there's still an
443 * electrical connection. If so, we really need to communicate
444 * this to the MMC core since there won't be any more
445 * interrupts as the card is completely removed. Otherwise,
446 * the MMC core might believe the card is still there even
447 * though the card was just removed very slowly.
448 */
449 if (!host->present) {
450 mrq->cmd->error = -ENOMEDIUM;
451 mmc_request_done(mmc, mrq);
452 return;
453 }
454 533
534 mrq = slot->mrq;
535 host->cur_slot = slot;
455 host->mrq = mrq; 536 host->mrq = mrq;
537
456 host->pending_events = 0; 538 host->pending_events = 0;
457 host->completed_events = 0; 539 host->completed_events = 0;
458 host->state = STATE_SENDING_CMD;
459 540
460 /* We don't support multiple blocks of weird lengths. */ 541 if (host->need_reset) {
542 mci_writel(host, CR, MCI_CR_SWRST);
543 mci_writel(host, CR, MCI_CR_MCIEN);
544 mci_writel(host, MR, host->mode_reg);
545 host->need_reset = false;
546 }
547 mci_writel(host, SDCR, slot->sdc_reg);
548
549 iflags = mci_readl(host, IMR);
550 if (iflags)
551 dev_warn(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
552 iflags);
553
554 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
555 /* Send init sequence (74 clock cycles) */
556 mci_writel(host, CMDR, MCI_CMDR_SPCMD_INIT);
557 while (!(mci_readl(host, SR) & MCI_CMDRDY))
558 cpu_relax();
559 }
461 data = mrq->data; 560 data = mrq->data;
462 if (data) { 561 if (data) {
463 if (data->blocks > 1 && data->blksz & 3) 562 atmci_set_timeout(host, slot, data);
464 goto fail;
465 atmci_set_timeout(host, data);
466 563
467 /* Must set block count/size before sending command */ 564 /* Must set block count/size before sending command */
468 mci_writel(host, BLKR, MCI_BCNT(data->blocks) 565 mci_writel(host, BLKR, MCI_BCNT(data->blocks)
469 | MCI_BLKLEN(data->blksz)); 566 | MCI_BLKLEN(data->blksz));
567 dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
568 MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
470 } 569 }
471 570
472 iflags = MCI_CMDRDY; 571 iflags = MCI_CMDRDY;
473 cmd = mrq->cmd; 572 cmd = mrq->cmd;
474 cmdflags = atmci_prepare_command(mmc, cmd); 573 cmdflags = atmci_prepare_command(slot->mmc, cmd);
475 atmci_start_command(host, cmd, cmdflags); 574 atmci_start_command(host, cmd, cmdflags);
476 575
477 if (data) 576 if (data)
478 iflags |= atmci_submit_data(mmc, data); 577 iflags |= atmci_submit_data(host, data);
479 578
480 if (mrq->stop) { 579 if (mrq->stop) {
481 host->stop_cmdr = atmci_prepare_command(mmc, mrq->stop); 580 host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
482 host->stop_cmdr |= MCI_CMDR_STOP_XFER; 581 host->stop_cmdr |= MCI_CMDR_STOP_XFER;
483 if (!(data->flags & MMC_DATA_WRITE)) 582 if (!(data->flags & MMC_DATA_WRITE))
484 host->stop_cmdr |= MCI_CMDR_TRDIR_READ; 583 host->stop_cmdr |= MCI_CMDR_TRDIR_READ;
@@ -495,65 +594,156 @@ static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
495 * prepared yet.) 594 * prepared yet.)
496 */ 595 */
497 mci_writel(host, IER, iflags); 596 mci_writel(host, IER, iflags);
597}
498 598
499 return; 599static void atmci_queue_request(struct atmel_mci *host,
600 struct atmel_mci_slot *slot, struct mmc_request *mrq)
601{
602 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
603 host->state);
604
605 spin_lock_bh(&host->lock);
606 slot->mrq = mrq;
607 if (host->state == STATE_IDLE) {
608 host->state = STATE_SENDING_CMD;
609 atmci_start_request(host, slot);
610 } else {
611 list_add_tail(&slot->queue_node, &host->queue);
612 }
613 spin_unlock_bh(&host->lock);
614}
500 615
501fail: 616static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
502 host->mrq = NULL; 617{
503 mrq->cmd->error = -EINVAL; 618 struct atmel_mci_slot *slot = mmc_priv(mmc);
504 mmc_request_done(mmc, mrq); 619 struct atmel_mci *host = slot->host;
620 struct mmc_data *data;
621
622 WARN_ON(slot->mrq);
623
624 /*
625 * We may "know" the card is gone even though there's still an
626 * electrical connection. If so, we really need to communicate
627 * this to the MMC core since there won't be any more
628 * interrupts as the card is completely removed. Otherwise,
629 * the MMC core might believe the card is still there even
630 * though the card was just removed very slowly.
631 */
632 if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
633 mrq->cmd->error = -ENOMEDIUM;
634 mmc_request_done(mmc, mrq);
635 return;
636 }
637
638 /* We don't support multiple blocks of weird lengths. */
639 data = mrq->data;
640 if (data && data->blocks > 1 && data->blksz & 3) {
641 mrq->cmd->error = -EINVAL;
642 mmc_request_done(mmc, mrq);
643 }
644
645 atmci_queue_request(host, slot, mrq);
505} 646}
506 647
507static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 648static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
508{ 649{
509 struct atmel_mci *host = mmc_priv(mmc); 650 struct atmel_mci_slot *slot = mmc_priv(mmc);
651 struct atmel_mci *host = slot->host;
652 unsigned int i;
510 653
511 host->sdc_reg &= ~MCI_SDCBUS_MASK; 654 slot->sdc_reg &= ~MCI_SDCBUS_MASK;
512 switch (ios->bus_width) { 655 switch (ios->bus_width) {
513 case MMC_BUS_WIDTH_1: 656 case MMC_BUS_WIDTH_1:
514 host->sdc_reg |= MCI_SDCBUS_1BIT; 657 slot->sdc_reg |= MCI_SDCBUS_1BIT;
515 break; 658 break;
516 case MMC_BUS_WIDTH_4: 659 case MMC_BUS_WIDTH_4:
517 host->sdc_reg = MCI_SDCBUS_4BIT; 660 slot->sdc_reg = MCI_SDCBUS_4BIT;
518 break; 661 break;
519 } 662 }
520 663
521 if (ios->clock) { 664 if (ios->clock) {
665 unsigned int clock_min = ~0U;
522 u32 clkdiv; 666 u32 clkdiv;
523 667
524 if (!host->mode_reg) 668 spin_lock_bh(&host->lock);
669 if (!host->mode_reg) {
525 clk_enable(host->mck); 670 clk_enable(host->mck);
671 mci_writel(host, CR, MCI_CR_SWRST);
672 mci_writel(host, CR, MCI_CR_MCIEN);
673 }
526 674
527 /* Set clock rate */ 675 /*
528 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * ios->clock) - 1; 676 * Use mirror of ios->clock to prevent race with mmc
677 * core ios update when finding the minimum.
678 */
679 slot->clock = ios->clock;
680 for (i = 0; i < ATMEL_MCI_MAX_NR_SLOTS; i++) {
681 if (host->slot[i] && host->slot[i]->clock
682 && host->slot[i]->clock < clock_min)
683 clock_min = host->slot[i]->clock;
684 }
685
686 /* Calculate clock divider */
687 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
529 if (clkdiv > 255) { 688 if (clkdiv > 255) {
530 dev_warn(&mmc->class_dev, 689 dev_warn(&mmc->class_dev,
531 "clock %u too slow; using %lu\n", 690 "clock %u too slow; using %lu\n",
532 ios->clock, host->bus_hz / (2 * 256)); 691 clock_min, host->bus_hz / (2 * 256));
533 clkdiv = 255; 692 clkdiv = 255;
534 } 693 }
535 694
695 /*
696 * WRPROOF and RDPROOF prevent overruns/underruns by
697 * stopping the clock when the FIFO is full/empty.
698 * This state is not expected to last for long.
699 */
536 host->mode_reg = MCI_MR_CLKDIV(clkdiv) | MCI_MR_WRPROOF 700 host->mode_reg = MCI_MR_CLKDIV(clkdiv) | MCI_MR_WRPROOF
537 | MCI_MR_RDPROOF; 701 | MCI_MR_RDPROOF;
538 702
539 mci_writel(host, CR, MCI_CR_MCIEN); 703 if (list_empty(&host->queue))
540 mci_writel(host, MR, host->mode_reg); 704 mci_writel(host, MR, host->mode_reg);
541 mci_writel(host, SDCR, host->sdc_reg); 705 else
706 host->need_clock_update = true;
707
708 spin_unlock_bh(&host->lock);
542 } else { 709 } else {
543 mci_writel(host, CR, MCI_CR_MCIDIS); 710 bool any_slot_active = false;
544 if (host->mode_reg) { 711
545 mci_readl(host, MR); 712 spin_lock_bh(&host->lock);
546 clk_disable(host->mck); 713 slot->clock = 0;
714 for (i = 0; i < ATMEL_MCI_MAX_NR_SLOTS; i++) {
715 if (host->slot[i] && host->slot[i]->clock) {
716 any_slot_active = true;
717 break;
718 }
547 } 719 }
548 host->mode_reg = 0; 720 if (!any_slot_active) {
721 mci_writel(host, CR, MCI_CR_MCIDIS);
722 if (host->mode_reg) {
723 mci_readl(host, MR);
724 clk_disable(host->mck);
725 }
726 host->mode_reg = 0;
727 }
728 spin_unlock_bh(&host->lock);
549 } 729 }
550 730
551 switch (ios->power_mode) { 731 switch (ios->power_mode) {
732 case MMC_POWER_UP:
733 set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
734 break;
552 default: 735 default:
553 /* 736 /*
554 * TODO: None of the currently available AVR32-based 737 * TODO: None of the currently available AVR32-based
555 * boards allow MMC power to be turned off. Implement 738 * boards allow MMC power to be turned off. Implement
556 * power control when this can be tested properly. 739 * power control when this can be tested properly.
740 *
741 * We also need to hook this into the clock management
742 * somehow so that newly inserted cards aren't
743 * subjected to a fast clock before we have a chance
744 * to figure out what the maximum rate is. Currently,
745 * there's no way to avoid this, and there never will
746 * be for boards that don't support power control.
557 */ 747 */
558 break; 748 break;
559 } 749 }
@@ -561,28 +751,77 @@ static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
561 751
562static int atmci_get_ro(struct mmc_host *mmc) 752static int atmci_get_ro(struct mmc_host *mmc)
563{ 753{
564 int read_only = 0; 754 int read_only = -ENOSYS;
565 struct atmel_mci *host = mmc_priv(mmc); 755 struct atmel_mci_slot *slot = mmc_priv(mmc);
566 756
567 if (gpio_is_valid(host->wp_pin)) { 757 if (gpio_is_valid(slot->wp_pin)) {
568 read_only = gpio_get_value(host->wp_pin); 758 read_only = gpio_get_value(slot->wp_pin);
569 dev_dbg(&mmc->class_dev, "card is %s\n", 759 dev_dbg(&mmc->class_dev, "card is %s\n",
570 read_only ? "read-only" : "read-write"); 760 read_only ? "read-only" : "read-write");
571 } else {
572 dev_dbg(&mmc->class_dev,
573 "no pin for checking read-only switch."
574 " Assuming write-enable.\n");
575 } 761 }
576 762
577 return read_only; 763 return read_only;
578} 764}
579 765
580static struct mmc_host_ops atmci_ops = { 766static int atmci_get_cd(struct mmc_host *mmc)
767{
768 int present = -ENOSYS;
769 struct atmel_mci_slot *slot = mmc_priv(mmc);
770
771 if (gpio_is_valid(slot->detect_pin)) {
772 present = !gpio_get_value(slot->detect_pin);
773 dev_dbg(&mmc->class_dev, "card is %spresent\n",
774 present ? "" : "not ");
775 }
776
777 return present;
778}
779
780static const struct mmc_host_ops atmci_ops = {
581 .request = atmci_request, 781 .request = atmci_request,
582 .set_ios = atmci_set_ios, 782 .set_ios = atmci_set_ios,
583 .get_ro = atmci_get_ro, 783 .get_ro = atmci_get_ro,
784 .get_cd = atmci_get_cd,
584}; 785};
585 786
787/* Called with host->lock held */
788static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
789 __releases(&host->lock)
790 __acquires(&host->lock)
791{
792 struct atmel_mci_slot *slot = NULL;
793 struct mmc_host *prev_mmc = host->cur_slot->mmc;
794
795 WARN_ON(host->cmd || host->data);
796
797 /*
798 * Update the MMC clock rate if necessary. This may be
799 * necessary if set_ios() is called when a different slot is
800 * busy transfering data.
801 */
802 if (host->need_clock_update)
803 mci_writel(host, MR, host->mode_reg);
804
805 host->cur_slot->mrq = NULL;
806 host->mrq = NULL;
807 if (!list_empty(&host->queue)) {
808 slot = list_entry(host->queue.next,
809 struct atmel_mci_slot, queue_node);
810 list_del(&slot->queue_node);
811 dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
812 mmc_hostname(slot->mmc));
813 host->state = STATE_SENDING_CMD;
814 atmci_start_request(host, slot);
815 } else {
816 dev_vdbg(&host->pdev->dev, "list empty\n");
817 host->state = STATE_IDLE;
818 }
819
820 spin_unlock(&host->lock);
821 mmc_request_done(prev_mmc, mrq);
822 spin_lock(&host->lock);
823}
824
586static void atmci_command_complete(struct atmel_mci *host, 825static void atmci_command_complete(struct atmel_mci *host,
587 struct mmc_command *cmd) 826 struct mmc_command *cmd)
588{ 827{
@@ -604,7 +843,7 @@ static void atmci_command_complete(struct atmel_mci *host,
604 cmd->error = 0; 843 cmd->error = 0;
605 844
606 if (cmd->error) { 845 if (cmd->error) {
607 dev_dbg(&host->mmc->class_dev, 846 dev_dbg(&host->pdev->dev,
608 "command error: status=0x%08x\n", status); 847 "command error: status=0x%08x\n", status);
609 848
610 if (cmd->data) { 849 if (cmd->data) {
@@ -618,81 +857,102 @@ static void atmci_command_complete(struct atmel_mci *host,
618 857
619static void atmci_detect_change(unsigned long data) 858static void atmci_detect_change(unsigned long data)
620{ 859{
621 struct atmel_mci *host = (struct atmel_mci *)data; 860 struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
622 struct mmc_request *mrq = host->mrq; 861 bool present;
623 int present; 862 bool present_old;
624 863
625 /* 864 /*
626 * atmci_remove() sets detect_pin to -1 before freeing the 865 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
627 * interrupt. We must not re-enable the interrupt if it has 866 * freeing the interrupt. We must not re-enable the interrupt
628 * been freed. 867 * if it has been freed, and if we're shutting down, it
868 * doesn't really matter whether the card is present or not.
629 */ 869 */
630 smp_rmb(); 870 smp_rmb();
631 if (!gpio_is_valid(host->detect_pin)) 871 if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
632 return; 872 return;
633 873
634 enable_irq(gpio_to_irq(host->detect_pin)); 874 enable_irq(gpio_to_irq(slot->detect_pin));
635 present = !gpio_get_value(host->detect_pin); 875 present = !gpio_get_value(slot->detect_pin);
876 present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
636 877
637 dev_vdbg(&host->pdev->dev, "detect change: %d (was %d)\n", 878 dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
638 present, host->present); 879 present, present_old);
639 880
640 if (present != host->present) { 881 if (present != present_old) {
641 dev_dbg(&host->mmc->class_dev, "card %s\n", 882 struct atmel_mci *host = slot->host;
883 struct mmc_request *mrq;
884
885 dev_dbg(&slot->mmc->class_dev, "card %s\n",
642 present ? "inserted" : "removed"); 886 present ? "inserted" : "removed");
643 host->present = present;
644 887
645 /* Reset controller if card is gone */ 888 spin_lock(&host->lock);
646 if (!present) { 889
647 mci_writel(host, CR, MCI_CR_SWRST); 890 if (!present)
648 mci_writel(host, IDR, ~0UL); 891 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
649 mci_writel(host, CR, MCI_CR_MCIEN); 892 else
650 } 893 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
651 894
652 /* Clean up queue if present */ 895 /* Clean up queue if present */
896 mrq = slot->mrq;
653 if (mrq) { 897 if (mrq) {
654 /* 898 if (mrq == host->mrq) {
655 * Reset controller to terminate any ongoing 899 /*
656 * commands or data transfers. 900 * Reset controller to terminate any ongoing
657 */ 901 * commands or data transfers.
658 mci_writel(host, CR, MCI_CR_SWRST); 902 */
659 mci_readl(host, SR); 903 mci_writel(host, CR, MCI_CR_SWRST);
660 904 mci_writel(host, CR, MCI_CR_MCIEN);
661 host->data = NULL; 905 mci_writel(host, MR, host->mode_reg);
662 host->cmd = NULL; 906
663 907 host->data = NULL;
664 switch (host->state) { 908 host->cmd = NULL;
665 case STATE_SENDING_CMD: 909
666 mrq->cmd->error = -ENOMEDIUM; 910 switch (host->state) {
667 if (!mrq->data) 911 case STATE_IDLE:
668 break; 912 break;
669 /* fall through */ 913 case STATE_SENDING_CMD:
670 case STATE_SENDING_DATA: 914 mrq->cmd->error = -ENOMEDIUM;
671 mrq->data->error = -ENOMEDIUM; 915 if (!mrq->data)
672 break; 916 break;
673 case STATE_DATA_BUSY: 917 /* fall through */
674 case STATE_DATA_ERROR: 918 case STATE_SENDING_DATA:
675 if (mrq->data->error == -EINPROGRESS)
676 mrq->data->error = -ENOMEDIUM; 919 mrq->data->error = -ENOMEDIUM;
677 if (!mrq->stop)
678 break; 920 break;
679 /* fall through */ 921 case STATE_DATA_BUSY:
680 case STATE_SENDING_STOP: 922 case STATE_DATA_ERROR:
681 mrq->stop->error = -ENOMEDIUM; 923 if (mrq->data->error == -EINPROGRESS)
682 break; 924 mrq->data->error = -ENOMEDIUM;
683 } 925 if (!mrq->stop)
926 break;
927 /* fall through */
928 case STATE_SENDING_STOP:
929 mrq->stop->error = -ENOMEDIUM;
930 break;
931 }
684 932
685 atmci_request_end(host->mmc, mrq); 933 atmci_request_end(host, mrq);
934 } else {
935 list_del(&slot->queue_node);
936 mrq->cmd->error = -ENOMEDIUM;
937 if (mrq->data)
938 mrq->data->error = -ENOMEDIUM;
939 if (mrq->stop)
940 mrq->stop->error = -ENOMEDIUM;
941
942 spin_unlock(&host->lock);
943 mmc_request_done(slot->mmc, mrq);
944 spin_lock(&host->lock);
945 }
686 } 946 }
947 spin_unlock(&host->lock);
687 948
688 mmc_detect_change(host->mmc, 0); 949 mmc_detect_change(slot->mmc, 0);
689 } 950 }
690} 951}
691 952
692static void atmci_tasklet_func(unsigned long priv) 953static void atmci_tasklet_func(unsigned long priv)
693{ 954{
694 struct mmc_host *mmc = (struct mmc_host *)priv; 955 struct atmel_mci *host = (struct atmel_mci *)priv;
695 struct atmel_mci *host = mmc_priv(mmc);
696 struct mmc_request *mrq = host->mrq; 956 struct mmc_request *mrq = host->mrq;
697 struct mmc_data *data = host->data; 957 struct mmc_data *data = host->data;
698 struct mmc_command *cmd = host->cmd; 958 struct mmc_command *cmd = host->cmd;
@@ -700,9 +960,11 @@ static void atmci_tasklet_func(unsigned long priv)
700 enum atmel_mci_state prev_state; 960 enum atmel_mci_state prev_state;
701 u32 status; 961 u32 status;
702 962
963 spin_lock(&host->lock);
964
703 state = host->state; 965 state = host->state;
704 966
705 dev_vdbg(&mmc->class_dev, 967 dev_vdbg(&host->pdev->dev,
706 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n", 968 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
707 state, host->pending_events, host->completed_events, 969 state, host->pending_events, host->completed_events,
708 mci_readl(host, IMR)); 970 mci_readl(host, IMR));
@@ -711,6 +973,9 @@ static void atmci_tasklet_func(unsigned long priv)
711 prev_state = state; 973 prev_state = state;
712 974
713 switch (state) { 975 switch (state) {
976 case STATE_IDLE:
977 break;
978
714 case STATE_SENDING_CMD: 979 case STATE_SENDING_CMD:
715 if (!atmci_test_and_clear_pending(host, 980 if (!atmci_test_and_clear_pending(host,
716 EVENT_CMD_COMPLETE)) 981 EVENT_CMD_COMPLETE))
@@ -720,8 +985,8 @@ static void atmci_tasklet_func(unsigned long priv)
720 atmci_set_completed(host, EVENT_CMD_COMPLETE); 985 atmci_set_completed(host, EVENT_CMD_COMPLETE);
721 atmci_command_complete(host, mrq->cmd); 986 atmci_command_complete(host, mrq->cmd);
722 if (!mrq->data || cmd->error) { 987 if (!mrq->data || cmd->error) {
723 atmci_request_end(mmc, host->mrq); 988 atmci_request_end(host, host->mrq);
724 break; 989 goto unlock;
725 } 990 }
726 991
727 prev_state = state = STATE_SENDING_DATA; 992 prev_state = state = STATE_SENDING_DATA;
@@ -731,7 +996,7 @@ static void atmci_tasklet_func(unsigned long priv)
731 if (atmci_test_and_clear_pending(host, 996 if (atmci_test_and_clear_pending(host,
732 EVENT_DATA_ERROR)) { 997 EVENT_DATA_ERROR)) {
733 if (data->stop) 998 if (data->stop)
734 send_stop_cmd(host->mmc, data); 999 send_stop_cmd(host, data);
735 state = STATE_DATA_ERROR; 1000 state = STATE_DATA_ERROR;
736 break; 1001 break;
737 } 1002 }
@@ -754,15 +1019,15 @@ static void atmci_tasklet_func(unsigned long priv)
754 status = host->data_status; 1019 status = host->data_status;
755 if (unlikely(status & ATMCI_DATA_ERROR_FLAGS)) { 1020 if (unlikely(status & ATMCI_DATA_ERROR_FLAGS)) {
756 if (status & MCI_DTOE) { 1021 if (status & MCI_DTOE) {
757 dev_dbg(&mmc->class_dev, 1022 dev_dbg(&host->pdev->dev,
758 "data timeout error\n"); 1023 "data timeout error\n");
759 data->error = -ETIMEDOUT; 1024 data->error = -ETIMEDOUT;
760 } else if (status & MCI_DCRCE) { 1025 } else if (status & MCI_DCRCE) {
761 dev_dbg(&mmc->class_dev, 1026 dev_dbg(&host->pdev->dev,
762 "data CRC error\n"); 1027 "data CRC error\n");
763 data->error = -EILSEQ; 1028 data->error = -EILSEQ;
764 } else { 1029 } else {
765 dev_dbg(&mmc->class_dev, 1030 dev_dbg(&host->pdev->dev,
766 "data FIFO error (status=%08x)\n", 1031 "data FIFO error (status=%08x)\n",
767 status); 1032 status);
768 data->error = -EIO; 1033 data->error = -EIO;
@@ -773,14 +1038,13 @@ static void atmci_tasklet_func(unsigned long priv)
773 } 1038 }
774 1039
775 if (!data->stop) { 1040 if (!data->stop) {
776 atmci_request_end(mmc, host->mrq); 1041 atmci_request_end(host, host->mrq);
777 prev_state = state; 1042 goto unlock;
778 break;
779 } 1043 }
780 1044
781 prev_state = state = STATE_SENDING_STOP; 1045 prev_state = state = STATE_SENDING_STOP;
782 if (!data->error) 1046 if (!data->error)
783 send_stop_cmd(host->mmc, data); 1047 send_stop_cmd(host, data);
784 /* fall through */ 1048 /* fall through */
785 1049
786 case STATE_SENDING_STOP: 1050 case STATE_SENDING_STOP:
@@ -790,9 +1054,8 @@ static void atmci_tasklet_func(unsigned long priv)
790 1054
791 host->cmd = NULL; 1055 host->cmd = NULL;
792 atmci_command_complete(host, mrq->stop); 1056 atmci_command_complete(host, mrq->stop);
793 atmci_request_end(mmc, host->mrq); 1057 atmci_request_end(host, host->mrq);
794 prev_state = state; 1058 goto unlock;
795 break;
796 1059
797 case STATE_DATA_ERROR: 1060 case STATE_DATA_ERROR:
798 if (!atmci_test_and_clear_pending(host, 1061 if (!atmci_test_and_clear_pending(host,
@@ -805,6 +1068,9 @@ static void atmci_tasklet_func(unsigned long priv)
805 } while (state != prev_state); 1068 } while (state != prev_state);
806 1069
807 host->state = state; 1070 host->state = state;
1071
1072unlock:
1073 spin_unlock(&host->lock);
808} 1074}
809 1075
810static void atmci_read_data_pio(struct atmel_mci *host) 1076static void atmci_read_data_pio(struct atmel_mci *host)
@@ -854,9 +1120,11 @@ static void atmci_read_data_pio(struct atmel_mci *host)
854 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY 1120 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY
855 | ATMCI_DATA_ERROR_FLAGS)); 1121 | ATMCI_DATA_ERROR_FLAGS));
856 host->data_status = status; 1122 host->data_status = status;
1123 data->bytes_xfered += nbytes;
1124 smp_wmb();
857 atmci_set_pending(host, EVENT_DATA_ERROR); 1125 atmci_set_pending(host, EVENT_DATA_ERROR);
858 tasklet_schedule(&host->tasklet); 1126 tasklet_schedule(&host->tasklet);
859 break; 1127 return;
860 } 1128 }
861 } while (status & MCI_RXRDY); 1129 } while (status & MCI_RXRDY);
862 1130
@@ -869,6 +1137,7 @@ done:
869 mci_writel(host, IDR, MCI_RXRDY); 1137 mci_writel(host, IDR, MCI_RXRDY);
870 mci_writel(host, IER, MCI_NOTBUSY); 1138 mci_writel(host, IER, MCI_NOTBUSY);
871 data->bytes_xfered += nbytes; 1139 data->bytes_xfered += nbytes;
1140 smp_wmb();
872 atmci_set_pending(host, EVENT_XFER_COMPLETE); 1141 atmci_set_pending(host, EVENT_XFER_COMPLETE);
873} 1142}
874 1143
@@ -922,9 +1191,11 @@ static void atmci_write_data_pio(struct atmel_mci *host)
922 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY 1191 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY
923 | ATMCI_DATA_ERROR_FLAGS)); 1192 | ATMCI_DATA_ERROR_FLAGS));
924 host->data_status = status; 1193 host->data_status = status;
1194 data->bytes_xfered += nbytes;
1195 smp_wmb();
925 atmci_set_pending(host, EVENT_DATA_ERROR); 1196 atmci_set_pending(host, EVENT_DATA_ERROR);
926 tasklet_schedule(&host->tasklet); 1197 tasklet_schedule(&host->tasklet);
927 break; 1198 return;
928 } 1199 }
929 } while (status & MCI_TXRDY); 1200 } while (status & MCI_TXRDY);
930 1201
@@ -937,29 +1208,26 @@ done:
937 mci_writel(host, IDR, MCI_TXRDY); 1208 mci_writel(host, IDR, MCI_TXRDY);
938 mci_writel(host, IER, MCI_NOTBUSY); 1209 mci_writel(host, IER, MCI_NOTBUSY);
939 data->bytes_xfered += nbytes; 1210 data->bytes_xfered += nbytes;
1211 smp_wmb();
940 atmci_set_pending(host, EVENT_XFER_COMPLETE); 1212 atmci_set_pending(host, EVENT_XFER_COMPLETE);
941} 1213}
942 1214
943static void atmci_cmd_interrupt(struct mmc_host *mmc, u32 status) 1215static void atmci_cmd_interrupt(struct atmel_mci *host, u32 status)
944{ 1216{
945 struct atmel_mci *host = mmc_priv(mmc);
946
947 mci_writel(host, IDR, MCI_CMDRDY); 1217 mci_writel(host, IDR, MCI_CMDRDY);
948 1218
949 host->cmd_status = status; 1219 host->cmd_status = status;
1220 smp_wmb();
950 atmci_set_pending(host, EVENT_CMD_COMPLETE); 1221 atmci_set_pending(host, EVENT_CMD_COMPLETE);
951 tasklet_schedule(&host->tasklet); 1222 tasklet_schedule(&host->tasklet);
952} 1223}
953 1224
954static irqreturn_t atmci_interrupt(int irq, void *dev_id) 1225static irqreturn_t atmci_interrupt(int irq, void *dev_id)
955{ 1226{
956 struct mmc_host *mmc = dev_id; 1227 struct atmel_mci *host = dev_id;
957 struct atmel_mci *host = mmc_priv(mmc);
958 u32 status, mask, pending; 1228 u32 status, mask, pending;
959 unsigned int pass_count = 0; 1229 unsigned int pass_count = 0;
960 1230
961 spin_lock(&mmc->lock);
962
963 do { 1231 do {
964 status = mci_readl(host, SR); 1232 status = mci_readl(host, SR);
965 mask = mci_readl(host, IMR); 1233 mask = mci_readl(host, IMR);
@@ -971,7 +1239,9 @@ static irqreturn_t atmci_interrupt(int irq, void *dev_id)
971 mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS 1239 mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS
972 | MCI_RXRDY | MCI_TXRDY); 1240 | MCI_RXRDY | MCI_TXRDY);
973 pending &= mci_readl(host, IMR); 1241 pending &= mci_readl(host, IMR);
1242
974 host->data_status = status; 1243 host->data_status = status;
1244 smp_wmb();
975 atmci_set_pending(host, EVENT_DATA_ERROR); 1245 atmci_set_pending(host, EVENT_DATA_ERROR);
976 tasklet_schedule(&host->tasklet); 1246 tasklet_schedule(&host->tasklet);
977 } 1247 }
@@ -979,6 +1249,7 @@ static irqreturn_t atmci_interrupt(int irq, void *dev_id)
979 mci_writel(host, IDR, 1249 mci_writel(host, IDR,
980 ATMCI_DATA_ERROR_FLAGS | MCI_NOTBUSY); 1250 ATMCI_DATA_ERROR_FLAGS | MCI_NOTBUSY);
981 host->data_status = status; 1251 host->data_status = status;
1252 smp_wmb();
982 atmci_set_pending(host, EVENT_DATA_COMPLETE); 1253 atmci_set_pending(host, EVENT_DATA_COMPLETE);
983 tasklet_schedule(&host->tasklet); 1254 tasklet_schedule(&host->tasklet);
984 } 1255 }
@@ -988,18 +1259,15 @@ static irqreturn_t atmci_interrupt(int irq, void *dev_id)
988 atmci_write_data_pio(host); 1259 atmci_write_data_pio(host);
989 1260
990 if (pending & MCI_CMDRDY) 1261 if (pending & MCI_CMDRDY)
991 atmci_cmd_interrupt(mmc, status); 1262 atmci_cmd_interrupt(host, status);
992 } while (pass_count++ < 5); 1263 } while (pass_count++ < 5);
993 1264
994 spin_unlock(&mmc->lock);
995
996 return pass_count ? IRQ_HANDLED : IRQ_NONE; 1265 return pass_count ? IRQ_HANDLED : IRQ_NONE;
997} 1266}
998 1267
999static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id) 1268static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
1000{ 1269{
1001 struct mmc_host *mmc = dev_id; 1270 struct atmel_mci_slot *slot = dev_id;
1002 struct atmel_mci *host = mmc_priv(mmc);
1003 1271
1004 /* 1272 /*
1005 * Disable interrupts until the pin has stabilized and check 1273 * Disable interrupts until the pin has stabilized and check
@@ -1007,21 +1275,122 @@ static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
1007 * middle of the timer routine when this interrupt triggers. 1275 * middle of the timer routine when this interrupt triggers.
1008 */ 1276 */
1009 disable_irq_nosync(irq); 1277 disable_irq_nosync(irq);
1010 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(20)); 1278 mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
1011 1279
1012 return IRQ_HANDLED; 1280 return IRQ_HANDLED;
1013} 1281}
1014 1282
1283static int __init atmci_init_slot(struct atmel_mci *host,
1284 struct mci_slot_pdata *slot_data, unsigned int id,
1285 u32 sdc_reg)
1286{
1287 struct mmc_host *mmc;
1288 struct atmel_mci_slot *slot;
1289
1290 mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
1291 if (!mmc)
1292 return -ENOMEM;
1293
1294 slot = mmc_priv(mmc);
1295 slot->mmc = mmc;
1296 slot->host = host;
1297 slot->detect_pin = slot_data->detect_pin;
1298 slot->wp_pin = slot_data->wp_pin;
1299 slot->sdc_reg = sdc_reg;
1300
1301 mmc->ops = &atmci_ops;
1302 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
1303 mmc->f_max = host->bus_hz / 2;
1304 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1305 if (slot_data->bus_width >= 4)
1306 mmc->caps |= MMC_CAP_4_BIT_DATA;
1307
1308 mmc->max_hw_segs = 64;
1309 mmc->max_phys_segs = 64;
1310 mmc->max_req_size = 32768 * 512;
1311 mmc->max_blk_size = 32768;
1312 mmc->max_blk_count = 512;
1313
1314 /* Assume card is present initially */
1315 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
1316 if (gpio_is_valid(slot->detect_pin)) {
1317 if (gpio_request(slot->detect_pin, "mmc_detect")) {
1318 dev_dbg(&mmc->class_dev, "no detect pin available\n");
1319 slot->detect_pin = -EBUSY;
1320 } else if (gpio_get_value(slot->detect_pin)) {
1321 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1322 }
1323 }
1324
1325 if (!gpio_is_valid(slot->detect_pin))
1326 mmc->caps |= MMC_CAP_NEEDS_POLL;
1327
1328 if (gpio_is_valid(slot->wp_pin)) {
1329 if (gpio_request(slot->wp_pin, "mmc_wp")) {
1330 dev_dbg(&mmc->class_dev, "no WP pin available\n");
1331 slot->wp_pin = -EBUSY;
1332 }
1333 }
1334
1335 host->slot[id] = slot;
1336 mmc_add_host(mmc);
1337
1338 if (gpio_is_valid(slot->detect_pin)) {
1339 int ret;
1340
1341 setup_timer(&slot->detect_timer, atmci_detect_change,
1342 (unsigned long)slot);
1343
1344 ret = request_irq(gpio_to_irq(slot->detect_pin),
1345 atmci_detect_interrupt,
1346 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1347 "mmc-detect", slot);
1348 if (ret) {
1349 dev_dbg(&mmc->class_dev,
1350 "could not request IRQ %d for detect pin\n",
1351 gpio_to_irq(slot->detect_pin));
1352 gpio_free(slot->detect_pin);
1353 slot->detect_pin = -EBUSY;
1354 }
1355 }
1356
1357 atmci_init_debugfs(slot);
1358
1359 return 0;
1360}
1361
1362static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
1363 unsigned int id)
1364{
1365 /* Debugfs stuff is cleaned up by mmc core */
1366
1367 set_bit(ATMCI_SHUTDOWN, &slot->flags);
1368 smp_wmb();
1369
1370 mmc_remove_host(slot->mmc);
1371
1372 if (gpio_is_valid(slot->detect_pin)) {
1373 int pin = slot->detect_pin;
1374
1375 free_irq(gpio_to_irq(pin), slot);
1376 del_timer_sync(&slot->detect_timer);
1377 gpio_free(pin);
1378 }
1379 if (gpio_is_valid(slot->wp_pin))
1380 gpio_free(slot->wp_pin);
1381
1382 slot->host->slot[id] = NULL;
1383 mmc_free_host(slot->mmc);
1384}
1385
1015static int __init atmci_probe(struct platform_device *pdev) 1386static int __init atmci_probe(struct platform_device *pdev)
1016{ 1387{
1017 struct mci_platform_data *pdata; 1388 struct mci_platform_data *pdata;
1018 struct mci_slot_pdata *slot; 1389 struct atmel_mci *host;
1019 struct atmel_mci *host; 1390 struct resource *regs;
1020 struct mmc_host *mmc; 1391 unsigned int nr_slots;
1021 struct resource *regs; 1392 int irq;
1022 u32 sdc_reg; 1393 int ret;
1023 int irq;
1024 int ret;
1025 1394
1026 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1395 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1027 if (!regs) 1396 if (!regs)
@@ -1033,27 +1402,13 @@ static int __init atmci_probe(struct platform_device *pdev)
1033 if (irq < 0) 1402 if (irq < 0)
1034 return irq; 1403 return irq;
1035 1404
1036 /* TODO: Allow using several slots at once */ 1405 host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL);
1037 if (pdata->slot[0].bus_width) { 1406 if (!host)
1038 sdc_reg = MCI_SDCSEL_SLOT_A;
1039 slot = &pdata->slot[0];
1040 } else if (pdata->slot[1].bus_width) {
1041 sdc_reg = MCI_SDCSEL_SLOT_B;
1042 slot = &pdata->slot[1];
1043 } else {
1044 return -EINVAL;
1045 }
1046
1047 mmc = mmc_alloc_host(sizeof(struct atmel_mci), &pdev->dev);
1048 if (!mmc)
1049 return -ENOMEM; 1407 return -ENOMEM;
1050 1408
1051 host = mmc_priv(mmc);
1052 host->pdev = pdev; 1409 host->pdev = pdev;
1053 host->mmc = mmc; 1410 spin_lock_init(&host->lock);
1054 host->detect_pin = slot->detect_pin; 1411 INIT_LIST_HEAD(&host->queue);
1055 host->wp_pin = slot->wp_pin;
1056 host->sdc_reg = sdc_reg;
1057 1412
1058 host->mck = clk_get(&pdev->dev, "mci_clk"); 1413 host->mck = clk_get(&pdev->dev, "mci_clk");
1059 if (IS_ERR(host->mck)) { 1414 if (IS_ERR(host->mck)) {
@@ -1073,123 +1428,74 @@ static int __init atmci_probe(struct platform_device *pdev)
1073 1428
1074 host->mapbase = regs->start; 1429 host->mapbase = regs->start;
1075 1430
1076 mmc->ops = &atmci_ops; 1431 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
1077 mmc->f_min = (host->bus_hz + 511) / 512;
1078 mmc->f_max = host->bus_hz / 2;
1079 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1080 if (slot->bus_width >= 4)
1081 mmc->caps |= MMC_CAP_4_BIT_DATA;
1082
1083 mmc->max_hw_segs = 64;
1084 mmc->max_phys_segs = 64;
1085 mmc->max_req_size = 32768 * 512;
1086 mmc->max_blk_size = 32768;
1087 mmc->max_blk_count = 512;
1088
1089 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)mmc);
1090 1432
1091 ret = request_irq(irq, atmci_interrupt, 0, pdev->dev.bus_id, mmc); 1433 ret = request_irq(irq, atmci_interrupt, 0, pdev->dev.bus_id, host);
1092 if (ret) 1434 if (ret)
1093 goto err_request_irq; 1435 goto err_request_irq;
1094 1436
1095 /* Assume card is present if we don't have a detect pin */
1096 host->present = 1;
1097 if (gpio_is_valid(host->detect_pin)) {
1098 if (gpio_request(host->detect_pin, "mmc_detect")) {
1099 dev_dbg(&mmc->class_dev, "no detect pin available\n");
1100 host->detect_pin = -1;
1101 } else {
1102 host->present = !gpio_get_value(host->detect_pin);
1103 }
1104 }
1105
1106 if (!gpio_is_valid(host->detect_pin))
1107 mmc->caps |= MMC_CAP_NEEDS_POLL;
1108
1109 if (gpio_is_valid(host->wp_pin)) {
1110 if (gpio_request(host->wp_pin, "mmc_wp")) {
1111 dev_dbg(&mmc->class_dev, "no WP pin available\n");
1112 host->wp_pin = -1;
1113 }
1114 }
1115
1116 platform_set_drvdata(pdev, host); 1437 platform_set_drvdata(pdev, host);
1117 1438
1118 mmc_add_host(mmc); 1439 /* We need at least one slot to succeed */
1119 1440 nr_slots = 0;
1120 if (gpio_is_valid(host->detect_pin)) { 1441 ret = -ENODEV;
1121 setup_timer(&host->detect_timer, atmci_detect_change, 1442 if (pdata->slot[0].bus_width) {
1122 (unsigned long)host); 1443 ret = atmci_init_slot(host, &pdata->slot[0],
1123 1444 MCI_SDCSEL_SLOT_A, 0);
1124 ret = request_irq(gpio_to_irq(host->detect_pin), 1445 if (!ret)
1125 atmci_detect_interrupt, 1446 nr_slots++;
1126 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 1447 }
1127 "mmc-detect", mmc); 1448 if (pdata->slot[1].bus_width) {
1128 if (ret) { 1449 ret = atmci_init_slot(host, &pdata->slot[1],
1129 dev_dbg(&mmc->class_dev, 1450 MCI_SDCSEL_SLOT_B, 1);
1130 "could not request IRQ %d for detect pin\n", 1451 if (!ret)
1131 gpio_to_irq(host->detect_pin)); 1452 nr_slots++;
1132 gpio_free(host->detect_pin);
1133 host->detect_pin = -1;
1134 }
1135 } 1453 }
1136 1454
1137 dev_info(&mmc->class_dev, 1455 if (!nr_slots)
1138 "Atmel MCI controller at 0x%08lx irq %d\n", 1456 goto err_init_slot;
1139 host->mapbase, irq);
1140 1457
1141 atmci_init_debugfs(host); 1458 dev_info(&pdev->dev,
1459 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
1460 host->mapbase, irq, nr_slots);
1142 1461
1143 return 0; 1462 return 0;
1144 1463
1464err_init_slot:
1465 free_irq(irq, host);
1145err_request_irq: 1466err_request_irq:
1146 iounmap(host->regs); 1467 iounmap(host->regs);
1147err_ioremap: 1468err_ioremap:
1148 clk_put(host->mck); 1469 clk_put(host->mck);
1149err_clk_get: 1470err_clk_get:
1150 mmc_free_host(mmc); 1471 kfree(host);
1151 return ret; 1472 return ret;
1152} 1473}
1153 1474
1154static int __exit atmci_remove(struct platform_device *pdev) 1475static int __exit atmci_remove(struct platform_device *pdev)
1155{ 1476{
1156 struct atmel_mci *host = platform_get_drvdata(pdev); 1477 struct atmel_mci *host = platform_get_drvdata(pdev);
1478 unsigned int i;
1157 1479
1158 platform_set_drvdata(pdev, NULL); 1480 platform_set_drvdata(pdev, NULL);
1159 1481
1160 if (host) { 1482 for (i = 0; i < ATMEL_MCI_MAX_NR_SLOTS; i++) {
1161 /* Debugfs stuff is cleaned up by mmc core */ 1483 if (host->slot[i])
1162 1484 atmci_cleanup_slot(host->slot[i], i);
1163 if (gpio_is_valid(host->detect_pin)) { 1485 }
1164 int pin = host->detect_pin;
1165
1166 /* Make sure the timer doesn't enable the interrupt */
1167 host->detect_pin = -1;
1168 smp_wmb();
1169
1170 free_irq(gpio_to_irq(pin), host->mmc);
1171 del_timer_sync(&host->detect_timer);
1172 gpio_free(pin);
1173 }
1174
1175 mmc_remove_host(host->mmc);
1176
1177 clk_enable(host->mck);
1178 mci_writel(host, IDR, ~0UL);
1179 mci_writel(host, CR, MCI_CR_MCIDIS);
1180 mci_readl(host, SR);
1181 clk_disable(host->mck);
1182 1486
1183 if (gpio_is_valid(host->wp_pin)) 1487 clk_enable(host->mck);
1184 gpio_free(host->wp_pin); 1488 mci_writel(host, IDR, ~0UL);
1489 mci_writel(host, CR, MCI_CR_MCIDIS);
1490 mci_readl(host, SR);
1491 clk_disable(host->mck);
1185 1492
1186 free_irq(platform_get_irq(pdev, 0), host->mmc); 1493 free_irq(platform_get_irq(pdev, 0), host);
1187 iounmap(host->regs); 1494 iounmap(host->regs);
1188 1495
1189 clk_put(host->mck); 1496 clk_put(host->mck);
1497 kfree(host);
1190 1498
1191 mmc_free_host(host->mmc);
1192 }
1193 return 0; 1499 return 0;
1194} 1500}
1195 1501