blob: 6985cdb0bb26e641fb340c0218d331e48844614a [file] [log] [blame]
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001/*
2 * Atmel MultiMedia Card Interface driver
3 *
4 * Copyright (C) 2004-2008 Atmel Corporation
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#include <linux/blkdev.h>
11#include <linux/clk.h>
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +020012#include <linux/debugfs.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020013#include <linux/device.h>
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020014#include <linux/dmaengine.h>
15#include <linux/dma-mapping.h>
Ben Nizettefbfca4b2008-07-18 16:48:09 +100016#include <linux/err.h>
David Brownell3c26e172008-07-27 02:34:45 -070017#include <linux/gpio.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020018#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/ioport.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/scatterlist.h>
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +020024#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +020026#include <linux/stat.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020027
28#include <linux/mmc/host.h>
Nicolas Ferre2f1d7912010-12-10 19:14:32 +010029#include <linux/mmc/sdio.h>
Nicolas Ferre2635d1b2009-12-14 18:01:30 -080030
31#include <mach/atmel-mci.h>
Nicolas Ferrec42aa772008-11-20 15:59:12 +010032#include <linux/atmel-mci.h>
Ludovic Desroches796211b2011-08-11 15:25:44 +000033#include <linux/atmel_pdc.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020034
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020035#include <asm/io.h>
36#include <asm/unaligned.h>
37
Rob Emanuele04d699c2009-09-22 16:45:19 -070038#include <mach/cpu.h>
Haavard Skinnemoen3663b732008-08-05 13:57:38 +020039#include <mach/board.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020040
41#include "atmel-mci-regs.h"
42
Ludovic Desroches2c96a292011-08-11 15:25:41 +000043#define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020044#define ATMCI_DMA_THRESHOLD 16
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020045
46enum {
47 EVENT_CMD_COMPLETE = 0,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020048 EVENT_XFER_COMPLETE,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +020049 EVENT_DATA_COMPLETE,
50 EVENT_DATA_ERROR,
51};
52
53enum atmel_mci_state {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +020054 STATE_IDLE = 0,
55 STATE_SENDING_CMD,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +020056 STATE_SENDING_DATA,
57 STATE_DATA_BUSY,
58 STATE_SENDING_STOP,
59 STATE_DATA_ERROR,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020060};
61
Ludovic Desroches796211b2011-08-11 15:25:44 +000062enum atmci_xfer_dir {
63 XFER_RECEIVE = 0,
64 XFER_TRANSMIT,
65};
66
67enum atmci_pdc_buf {
68 PDC_FIRST_BUF = 0,
69 PDC_SECOND_BUF,
70};
71
72struct atmel_mci_caps {
73 bool has_dma;
74 bool has_pdc;
75 bool has_cfg_reg;
76 bool has_cstor_reg;
77 bool has_highspeed;
78 bool has_rwproof;
79};
80
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020081struct atmel_mci_dma {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020082 struct dma_chan *chan;
83 struct dma_async_tx_descriptor *data_desc;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020084};
85
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +020086/**
87 * struct atmel_mci - MMC controller state shared between all slots
88 * @lock: Spinlock protecting the queue and associated data.
89 * @regs: Pointer to MMIO registers.
Ludovic Desroches796211b2011-08-11 15:25:44 +000090 * @sg: Scatterlist entry currently being processed by PIO or PDC code.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +020091 * @pio_offset: Offset into the current scatterlist entry.
92 * @cur_slot: The slot which is currently using the controller.
93 * @mrq: The request currently being processed on @cur_slot,
94 * or NULL if the controller is idle.
95 * @cmd: The command currently being sent to the card, or NULL.
96 * @data: The data currently being transferred, or NULL if no data
97 * transfer is in progress.
Ludovic Desroches796211b2011-08-11 15:25:44 +000098 * @data_size: just data->blocks * data->blksz.
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020099 * @dma: DMA client state.
100 * @data_chan: DMA channel being used for the current data transfer.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200101 * @cmd_status: Snapshot of SR taken upon completion of the current
102 * command. Only valid when EVENT_CMD_COMPLETE is pending.
103 * @data_status: Snapshot of SR taken upon completion of the current
104 * data transfer. Only valid when EVENT_DATA_COMPLETE or
105 * EVENT_DATA_ERROR is pending.
106 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
107 * to be sent.
108 * @tasklet: Tasklet running the request state machine.
109 * @pending_events: Bitmask of events flagged by the interrupt handler
110 * to be processed by the tasklet.
111 * @completed_events: Bitmask of events which the state machine has
112 * processed.
113 * @state: Tasklet state.
114 * @queue: List of slots waiting for access to the controller.
115 * @need_clock_update: Update the clock rate before the next request.
116 * @need_reset: Reset controller before next request.
117 * @mode_reg: Value of the MR register.
Nicolas Ferre74791a22009-12-14 18:01:31 -0800118 * @cfg_reg: Value of the CFG register.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200119 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
120 * rate and timeout calculations.
121 * @mapbase: Physical address of the MMIO registers.
122 * @mck: The peripheral bus clock hooked up to the MMC controller.
123 * @pdev: Platform device associated with the MMC controller.
124 * @slot: Slots sharing this MMC controller.
Ludovic Desroches796211b2011-08-11 15:25:44 +0000125 * @caps: MCI capabilities depending on MCI version.
126 * @prepare_data: function to setup MCI before data transfer which
127 * depends on MCI capabilities.
128 * @submit_data: function to start data transfer which depends on MCI
129 * capabilities.
130 * @stop_transfer: function to stop data transfer which depends on MCI
131 * capabilities.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200132 *
133 * Locking
134 * =======
135 *
136 * @lock is a softirq-safe spinlock protecting @queue as well as
137 * @cur_slot, @mrq and @state. These must always be updated
138 * at the same time while holding @lock.
139 *
140 * @lock also protects mode_reg and need_clock_update since these are
141 * used to synchronize mode register updates with the queue
142 * processing.
143 *
144 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
145 * and must always be written at the same time as the slot is added to
146 * @queue.
147 *
148 * @pending_events and @completed_events are accessed using atomic bit
149 * operations, so they don't need any locking.
150 *
151 * None of the fields touched by the interrupt handler need any
152 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
153 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
154 * interrupts must be disabled and @data_status updated with a
155 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300156 * CMDRDY interrupt must be disabled and @cmd_status updated with a
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200157 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
158 * bytes_xfered field of @data must be written. This is ensured by
159 * using barriers.
160 */
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200161struct atmel_mci {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200162 spinlock_t lock;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200163 void __iomem *regs;
164
165 struct scatterlist *sg;
166 unsigned int pio_offset;
167
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200168 struct atmel_mci_slot *cur_slot;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200169 struct mmc_request *mrq;
170 struct mmc_command *cmd;
171 struct mmc_data *data;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000172 unsigned int data_size;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200173
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200174 struct atmel_mci_dma dma;
175 struct dma_chan *data_chan;
176
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200177 u32 cmd_status;
178 u32 data_status;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200179 u32 stop_cmdr;
180
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200181 struct tasklet_struct tasklet;
182 unsigned long pending_events;
183 unsigned long completed_events;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200184 enum atmel_mci_state state;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200185 struct list_head queue;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200186
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200187 bool need_clock_update;
188 bool need_reset;
189 u32 mode_reg;
Nicolas Ferre74791a22009-12-14 18:01:31 -0800190 u32 cfg_reg;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200191 unsigned long bus_hz;
192 unsigned long mapbase;
193 struct clk *mck;
194 struct platform_device *pdev;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200195
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000196 struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
Ludovic Desroches796211b2011-08-11 15:25:44 +0000197
198 struct atmel_mci_caps caps;
199
200 u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
201 void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
202 void (*stop_transfer)(struct atmel_mci *host);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200203};
204
205/**
206 * struct atmel_mci_slot - MMC slot state
207 * @mmc: The mmc_host representing this slot.
208 * @host: The MMC controller this slot is using.
209 * @sdc_reg: Value of SDCR to be written before using this slot.
Anders Grahn88ff82e2010-05-26 14:42:01 -0700210 * @sdio_irq: SDIO irq mask for this slot.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200211 * @mrq: mmc_request currently being processed or waiting to be
212 * processed, or NULL when the slot is idle.
213 * @queue_node: List node for placing this node in the @queue list of
214 * &struct atmel_mci.
215 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
216 * @flags: Random state bits associated with the slot.
217 * @detect_pin: GPIO pin used for card detection, or negative if not
218 * available.
219 * @wp_pin: GPIO pin used for card write protect sending, or negative
220 * if not available.
Jonas Larsson1c1452b2009-03-31 11:16:48 +0200221 * @detect_is_active_high: The state of the detect pin when it is active.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200222 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
223 */
224struct atmel_mci_slot {
225 struct mmc_host *mmc;
226 struct atmel_mci *host;
227
228 u32 sdc_reg;
Anders Grahn88ff82e2010-05-26 14:42:01 -0700229 u32 sdio_irq;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200230
231 struct mmc_request *mrq;
232 struct list_head queue_node;
233
234 unsigned int clock;
235 unsigned long flags;
236#define ATMCI_CARD_PRESENT 0
237#define ATMCI_CARD_NEED_INIT 1
238#define ATMCI_SHUTDOWN 2
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +0200239#define ATMCI_SUSPENDED 3
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200240
241 int detect_pin;
242 int wp_pin;
Jonas Larsson1c1452b2009-03-31 11:16:48 +0200243 bool detect_is_active_high;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200244
245 struct timer_list detect_timer;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200246};
247
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200248#define atmci_test_and_clear_pending(host, event) \
249 test_and_clear_bit(event, &host->pending_events)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200250#define atmci_set_completed(host, event) \
251 set_bit(event, &host->completed_events)
252#define atmci_set_pending(host, event) \
253 set_bit(event, &host->pending_events)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200254
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200255/*
256 * The debugfs stuff below is mostly optimized away when
257 * CONFIG_DEBUG_FS is not set.
258 */
259static int atmci_req_show(struct seq_file *s, void *v)
260{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200261 struct atmel_mci_slot *slot = s->private;
262 struct mmc_request *mrq;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200263 struct mmc_command *cmd;
264 struct mmc_command *stop;
265 struct mmc_data *data;
266
267 /* Make sure we get a consistent snapshot */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200268 spin_lock_bh(&slot->host->lock);
269 mrq = slot->mrq;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200270
271 if (mrq) {
272 cmd = mrq->cmd;
273 data = mrq->data;
274 stop = mrq->stop;
275
276 if (cmd)
277 seq_printf(s,
278 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
279 cmd->opcode, cmd->arg, cmd->flags,
280 cmd->resp[0], cmd->resp[1], cmd->resp[2],
Nicolas Ferred586ebb2010-05-11 14:06:50 -0700281 cmd->resp[3], cmd->error);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200282 if (data)
283 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
284 data->bytes_xfered, data->blocks,
285 data->blksz, data->flags, data->error);
286 if (stop)
287 seq_printf(s,
288 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
289 stop->opcode, stop->arg, stop->flags,
290 stop->resp[0], stop->resp[1], stop->resp[2],
Nicolas Ferred586ebb2010-05-11 14:06:50 -0700291 stop->resp[3], stop->error);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200292 }
293
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200294 spin_unlock_bh(&slot->host->lock);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200295
296 return 0;
297}
298
299static int atmci_req_open(struct inode *inode, struct file *file)
300{
301 return single_open(file, atmci_req_show, inode->i_private);
302}
303
304static const struct file_operations atmci_req_fops = {
305 .owner = THIS_MODULE,
306 .open = atmci_req_open,
307 .read = seq_read,
308 .llseek = seq_lseek,
309 .release = single_release,
310};
311
312static void atmci_show_status_reg(struct seq_file *s,
313 const char *regname, u32 value)
314{
315 static const char *sr_bit[] = {
316 [0] = "CMDRDY",
317 [1] = "RXRDY",
318 [2] = "TXRDY",
319 [3] = "BLKE",
320 [4] = "DTIP",
321 [5] = "NOTBUSY",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700322 [6] = "ENDRX",
323 [7] = "ENDTX",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200324 [8] = "SDIOIRQA",
325 [9] = "SDIOIRQB",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700326 [12] = "SDIOWAIT",
327 [14] = "RXBUFF",
328 [15] = "TXBUFE",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200329 [16] = "RINDE",
330 [17] = "RDIRE",
331 [18] = "RCRCE",
332 [19] = "RENDE",
333 [20] = "RTOE",
334 [21] = "DCRCE",
335 [22] = "DTOE",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700336 [23] = "CSTOE",
337 [24] = "BLKOVRE",
338 [25] = "DMADONE",
339 [26] = "FIFOEMPTY",
340 [27] = "XFRDONE",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200341 [30] = "OVRE",
342 [31] = "UNRE",
343 };
344 unsigned int i;
345
346 seq_printf(s, "%s:\t0x%08x", regname, value);
347 for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
348 if (value & (1 << i)) {
349 if (sr_bit[i])
350 seq_printf(s, " %s", sr_bit[i]);
351 else
352 seq_puts(s, " UNKNOWN");
353 }
354 }
355 seq_putc(s, '\n');
356}
357
358static int atmci_regs_show(struct seq_file *s, void *v)
359{
360 struct atmel_mci *host = s->private;
361 u32 *buf;
362
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000363 buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200364 if (!buf)
365 return -ENOMEM;
366
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200367 /*
368 * Grab a more or less consistent snapshot. Note that we're
369 * not disabling interrupts, so IMR and SR may not be
370 * consistent.
371 */
372 spin_lock_bh(&host->lock);
Haavard Skinnemoen87e60f22008-09-19 21:09:27 +0200373 clk_enable(host->mck);
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000374 memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
Haavard Skinnemoen87e60f22008-09-19 21:09:27 +0200375 clk_disable(host->mck);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200376 spin_unlock_bh(&host->lock);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200377
378 seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000379 buf[ATMCI_MR / 4],
380 buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
381 buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "",
382 buf[ATMCI_MR / 4] & 0xff);
383 seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
384 seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
385 seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200386 seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000387 buf[ATMCI_BLKR / 4],
388 buf[ATMCI_BLKR / 4] & 0xffff,
389 (buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000390 if (host->caps.has_cstor_reg)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000391 seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200392
393 /* Don't read RSPR and RDR; it will consume the data there */
394
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000395 atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
396 atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200397
Ludovic Desroches796211b2011-08-11 15:25:44 +0000398 if (host->caps.has_dma) {
Nicolas Ferre74791a22009-12-14 18:01:31 -0800399 u32 val;
400
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000401 val = buf[ATMCI_DMA / 4];
Nicolas Ferre74791a22009-12-14 18:01:31 -0800402 seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
403 val, val & 3,
404 ((val >> 4) & 3) ?
405 1 << (((val >> 4) & 3) + 1) : 1,
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000406 val & ATMCI_DMAEN ? " DMAEN" : "");
Ludovic Desroches796211b2011-08-11 15:25:44 +0000407 }
408 if (host->caps.has_cfg_reg) {
409 u32 val;
Nicolas Ferre74791a22009-12-14 18:01:31 -0800410
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000411 val = buf[ATMCI_CFG / 4];
Nicolas Ferre74791a22009-12-14 18:01:31 -0800412 seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
413 val,
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000414 val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
415 val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
416 val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
417 val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
Nicolas Ferre74791a22009-12-14 18:01:31 -0800418 }
419
Haavard Skinnemoenb17339a2008-09-19 21:09:28 +0200420 kfree(buf);
421
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200422 return 0;
423}
424
425static int atmci_regs_open(struct inode *inode, struct file *file)
426{
427 return single_open(file, atmci_regs_show, inode->i_private);
428}
429
430static const struct file_operations atmci_regs_fops = {
431 .owner = THIS_MODULE,
432 .open = atmci_regs_open,
433 .read = seq_read,
434 .llseek = seq_lseek,
435 .release = single_release,
436};
437
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200438static void atmci_init_debugfs(struct atmel_mci_slot *slot)
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200439{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200440 struct mmc_host *mmc = slot->mmc;
441 struct atmel_mci *host = slot->host;
442 struct dentry *root;
443 struct dentry *node;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200444
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200445 root = mmc->debugfs_root;
446 if (!root)
447 return;
448
449 node = debugfs_create_file("regs", S_IRUSR, root, host,
450 &atmci_regs_fops);
451 if (IS_ERR(node))
452 return;
453 if (!node)
454 goto err;
455
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200456 node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200457 if (!node)
458 goto err;
459
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200460 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
461 if (!node)
462 goto err;
463
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200464 node = debugfs_create_x32("pending_events", S_IRUSR, root,
465 (u32 *)&host->pending_events);
466 if (!node)
467 goto err;
468
469 node = debugfs_create_x32("completed_events", S_IRUSR, root,
470 (u32 *)&host->completed_events);
471 if (!node)
472 goto err;
473
474 return;
475
476err:
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200477 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200478}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200479
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000480static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200481 unsigned int ns)
482{
483 return (ns * (host->bus_hz / 1000000) + 999) / 1000;
484}
485
486static void atmci_set_timeout(struct atmel_mci *host,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200487 struct atmel_mci_slot *slot, struct mmc_data *data)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200488{
489 static unsigned dtomul_to_shift[] = {
490 0, 4, 7, 8, 10, 12, 16, 20
491 };
492 unsigned timeout;
493 unsigned dtocyc;
494 unsigned dtomul;
495
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000496 timeout = atmci_ns_to_clocks(host, data->timeout_ns)
497 + data->timeout_clks;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200498
499 for (dtomul = 0; dtomul < 8; dtomul++) {
500 unsigned shift = dtomul_to_shift[dtomul];
501 dtocyc = (timeout + (1 << shift) - 1) >> shift;
502 if (dtocyc < 15)
503 break;
504 }
505
506 if (dtomul >= 8) {
507 dtomul = 7;
508 dtocyc = 15;
509 }
510
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200511 dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200512 dtocyc << dtomul_to_shift[dtomul]);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000513 atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200514}
515
516/*
517 * Return mask with command flags to be enabled for this command.
518 */
519static u32 atmci_prepare_command(struct mmc_host *mmc,
520 struct mmc_command *cmd)
521{
522 struct mmc_data *data;
523 u32 cmdr;
524
525 cmd->error = -EINPROGRESS;
526
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000527 cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200528
529 if (cmd->flags & MMC_RSP_PRESENT) {
530 if (cmd->flags & MMC_RSP_136)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000531 cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200532 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000533 cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200534 }
535
536 /*
537 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
538 * it's too difficult to determine whether this is an ACMD or
539 * not. Better make it 64.
540 */
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000541 cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200542
543 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000544 cmdr |= ATMCI_CMDR_OPDCMD;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200545
546 data = cmd->data;
547 if (data) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000548 cmdr |= ATMCI_CMDR_START_XFER;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100549
550 if (cmd->opcode == SD_IO_RW_EXTENDED) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000551 cmdr |= ATMCI_CMDR_SDIO_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100552 } else {
553 if (data->flags & MMC_DATA_STREAM)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000554 cmdr |= ATMCI_CMDR_STREAM;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100555 else if (data->blocks > 1)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000556 cmdr |= ATMCI_CMDR_MULTI_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100557 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000558 cmdr |= ATMCI_CMDR_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100559 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200560
561 if (data->flags & MMC_DATA_READ)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000562 cmdr |= ATMCI_CMDR_TRDIR_READ;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200563 }
564
565 return cmdr;
566}
567
Ludovic Desroches11d14882011-08-11 15:25:45 +0000568static void atmci_send_command(struct atmel_mci *host,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200569 struct mmc_command *cmd, u32 cmd_flags)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200570{
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200571 WARN_ON(host->cmd);
572 host->cmd = cmd;
573
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200574 dev_vdbg(&host->pdev->dev,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200575 "start command: ARGR=0x%08x CMDR=0x%08x\n",
576 cmd->arg, cmd_flags);
577
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000578 atmci_writel(host, ATMCI_ARGR, cmd->arg);
579 atmci_writel(host, ATMCI_CMDR, cmd_flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200580}
581
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000582static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200583{
Ludovic Desroches11d14882011-08-11 15:25:45 +0000584 atmci_send_command(host, data->stop, host->stop_cmdr);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000585 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200586}
587
Ludovic Desroches796211b2011-08-11 15:25:44 +0000588/*
589 * Configure given PDC buffer taking care of alignement issues.
590 * Update host->data_size and host->sg.
591 */
592static void atmci_pdc_set_single_buf(struct atmel_mci *host,
593 enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200594{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000595 u32 pointer_reg, counter_reg;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200596
Ludovic Desroches796211b2011-08-11 15:25:44 +0000597 if (dir == XFER_RECEIVE) {
598 pointer_reg = ATMEL_PDC_RPR;
599 counter_reg = ATMEL_PDC_RCR;
600 } else {
601 pointer_reg = ATMEL_PDC_TPR;
602 counter_reg = ATMEL_PDC_TCR;
603 }
604
605 if (buf_nb == PDC_SECOND_BUF) {
Ludovic Desroches1ebbe3d2011-08-11 15:25:46 +0000606 pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
607 counter_reg += ATMEL_PDC_SCND_BUF_OFF;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000608 }
609
610 atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
Ludovic Desroches341fa4c2011-08-11 15:25:47 +0000611 if (host->data_size <= sg_dma_len(host->sg)) {
Ludovic Desroches796211b2011-08-11 15:25:44 +0000612 if (host->data_size & 0x3) {
613 /* If size is different from modulo 4, transfer bytes */
614 atmci_writel(host, counter_reg, host->data_size);
615 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE);
616 } else {
617 /* Else transfer 32-bits words */
618 atmci_writel(host, counter_reg, host->data_size / 4);
619 }
620 host->data_size = 0;
621 } else {
622 /* We assume the size of a page is 32-bits aligned */
Ludovic Desroches341fa4c2011-08-11 15:25:47 +0000623 atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
624 host->data_size -= sg_dma_len(host->sg);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000625 if (host->data_size)
626 host->sg = sg_next(host->sg);
627 }
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200628}
629
Ludovic Desroches796211b2011-08-11 15:25:44 +0000630/*
631 * Configure PDC buffer according to the data size ie configuring one or two
632 * buffers. Don't use this function if you want to configure only the second
633 * buffer. In this case, use atmci_pdc_set_single_buf.
634 */
635static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200636{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000637 atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF);
638 if (host->data_size)
639 atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF);
640}
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200641
Ludovic Desroches796211b2011-08-11 15:25:44 +0000642/*
643 * Unmap sg lists, called when transfer is finished.
644 */
645static void atmci_pdc_cleanup(struct atmel_mci *host)
646{
647 struct mmc_data *data = host->data;
648
649 if (data)
650 dma_unmap_sg(&host->pdev->dev,
651 data->sg, data->sg_len,
652 ((data->flags & MMC_DATA_WRITE)
653 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
654}
655
656/*
657 * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
658 * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
659 * interrupt needed for both transfer directions.
660 */
661static void atmci_pdc_complete(struct atmel_mci *host)
662{
663 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
664 atmci_pdc_cleanup(host);
665
666 /*
667 * If the card was removed, data will be NULL. No point trying
668 * to send the stop command or waiting for NBUSY in this case.
669 */
670 if (host->data) {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200671 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000672 tasklet_schedule(&host->tasklet);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000673 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200674 }
675}
676
Ludovic Desroches796211b2011-08-11 15:25:44 +0000677static void atmci_dma_cleanup(struct atmel_mci *host)
678{
679 struct mmc_data *data = host->data;
680
681 if (data)
682 dma_unmap_sg(host->dma.chan->device->dev,
683 data->sg, data->sg_len,
684 ((data->flags & MMC_DATA_WRITE)
685 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
686}
687
688/*
689 * This function is called by the DMA driver from tasklet context.
690 */
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200691static void atmci_dma_complete(void *arg)
692{
693 struct atmel_mci *host = arg;
694 struct mmc_data *data = host->data;
695
696 dev_vdbg(&host->pdev->dev, "DMA complete\n");
697
Ludovic Desroches796211b2011-08-11 15:25:44 +0000698 if (host->caps.has_dma)
Nicolas Ferre74791a22009-12-14 18:01:31 -0800699 /* Disable DMA hardware handshaking on MCI */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000700 atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
Nicolas Ferre74791a22009-12-14 18:01:31 -0800701
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200702 atmci_dma_cleanup(host);
703
704 /*
705 * If the card was removed, data will be NULL. No point trying
706 * to send the stop command or waiting for NBUSY in this case.
707 */
708 if (data) {
709 atmci_set_pending(host, EVENT_XFER_COMPLETE);
710 tasklet_schedule(&host->tasklet);
711
712 /*
713 * Regardless of what the documentation says, we have
714 * to wait for NOTBUSY even after block read
715 * operations.
716 *
717 * When the DMA transfer is complete, the controller
718 * may still be reading the CRC from the card, i.e.
719 * the data transfer is still in progress and we
720 * haven't seen all the potential error bits yet.
721 *
722 * The interrupt handler will schedule a different
723 * tasklet to finish things up when the data transfer
724 * is completely done.
725 *
726 * We may not complete the mmc request here anyway
727 * because the mmc layer may call back and cause us to
728 * violate the "don't submit new operations from the
729 * completion callback" rule of the dma engine
730 * framework.
731 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000732 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200733 }
734}
735
Ludovic Desroches796211b2011-08-11 15:25:44 +0000736/*
737 * Returns a mask of interrupt flags to be enabled after the whole
738 * request has been prepared.
739 */
740static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
741{
742 u32 iflags;
743
744 data->error = -EINPROGRESS;
745
746 host->sg = data->sg;
747 host->data = data;
748 host->data_chan = NULL;
749
750 iflags = ATMCI_DATA_ERROR_FLAGS;
751
752 /*
753 * Errata: MMC data write operation with less than 12
754 * bytes is impossible.
755 *
756 * Errata: MCI Transmit Data Register (TDR) FIFO
757 * corruption when length is not multiple of 4.
758 */
759 if (data->blocks * data->blksz < 12
760 || (data->blocks * data->blksz) & 3)
761 host->need_reset = true;
762
763 host->pio_offset = 0;
764 if (data->flags & MMC_DATA_READ)
765 iflags |= ATMCI_RXRDY;
766 else
767 iflags |= ATMCI_TXRDY;
768
769 return iflags;
770}
771
772/*
773 * Set interrupt flags and set block length into the MCI mode register even
774 * if this value is also accessible in the MCI block register. It seems to be
775 * necessary before the High Speed MCI version. It also map sg and configure
776 * PDC registers.
777 */
778static u32
779atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
780{
781 u32 iflags, tmp;
782 unsigned int sg_len;
783 enum dma_data_direction dir;
784
785 data->error = -EINPROGRESS;
786
787 host->data = data;
788 host->sg = data->sg;
789 iflags = ATMCI_DATA_ERROR_FLAGS;
790
791 /* Enable pdc mode */
792 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE);
793
794 if (data->flags & MMC_DATA_READ) {
795 dir = DMA_FROM_DEVICE;
796 iflags |= ATMCI_ENDRX | ATMCI_RXBUFF;
797 } else {
798 dir = DMA_TO_DEVICE;
799 iflags |= ATMCI_ENDTX | ATMCI_TXBUFE;
800 }
801
802 /* Set BLKLEN */
803 tmp = atmci_readl(host, ATMCI_MR);
804 tmp &= 0x0000ffff;
805 tmp |= ATMCI_BLKLEN(data->blksz);
806 atmci_writel(host, ATMCI_MR, tmp);
807
808 /* Configure PDC */
809 host->data_size = data->blocks * data->blksz;
810 sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000811 if (host->data_size)
812 atmci_pdc_set_both_buf(host,
813 ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));
814
815 return iflags;
816}
817
818static u32
Nicolas Ferre74791a22009-12-14 18:01:31 -0800819atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200820{
821 struct dma_chan *chan;
822 struct dma_async_tx_descriptor *desc;
823 struct scatterlist *sg;
824 unsigned int i;
825 enum dma_data_direction direction;
Vinod Koule0d23ef2011-11-17 14:54:38 +0530826 enum dma_transfer_direction slave_dirn;
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700827 unsigned int sglen;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000828 u32 iflags;
829
830 data->error = -EINPROGRESS;
831
832 WARN_ON(host->data);
833 host->sg = NULL;
834 host->data = data;
835
836 iflags = ATMCI_DATA_ERROR_FLAGS;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200837
838 /*
839 * We don't do DMA on "complex" transfers, i.e. with
840 * non-word-aligned buffers or lengths. Also, we don't bother
841 * with all the DMA setup overhead for short transfers.
842 */
Ludovic Desroches796211b2011-08-11 15:25:44 +0000843 if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
844 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200845 if (data->blksz & 3)
Ludovic Desroches796211b2011-08-11 15:25:44 +0000846 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200847
848 for_each_sg(data->sg, sg, data->sg_len, i) {
849 if (sg->offset & 3 || sg->length & 3)
Ludovic Desroches796211b2011-08-11 15:25:44 +0000850 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200851 }
852
853 /* If we don't have a channel, we can't do DMA */
854 chan = host->dma.chan;
Dan Williams6f49a572009-01-06 11:38:14 -0700855 if (chan)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200856 host->data_chan = chan;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200857
858 if (!chan)
859 return -ENODEV;
860
Ludovic Desroches796211b2011-08-11 15:25:44 +0000861 if (host->caps.has_dma)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000862 atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(3) | ATMCI_DMAEN);
Nicolas Ferre74791a22009-12-14 18:01:31 -0800863
Vinod Koule0d23ef2011-11-17 14:54:38 +0530864 if (data->flags & MMC_DATA_READ) {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200865 direction = DMA_FROM_DEVICE;
Vinod Koule0d23ef2011-11-17 14:54:38 +0530866 slave_dirn = DMA_DEV_TO_MEM;
867 } else {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200868 direction = DMA_TO_DEVICE;
Vinod Koule0d23ef2011-11-17 14:54:38 +0530869 slave_dirn = DMA_MEM_TO_DEV;
870 }
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200871
Linus Walleij266ac3f2011-02-10 16:08:06 +0100872 sglen = dma_map_sg(chan->device->dev, data->sg,
Ludovic Desroches796211b2011-08-11 15:25:44 +0000873 data->sg_len, direction);
Linus Walleij88ce4db2011-02-10 16:08:16 +0100874
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200875 desc = chan->device->device_prep_slave_sg(chan,
Vinod Koule0d23ef2011-11-17 14:54:38 +0530876 data->sg, sglen, slave_dirn,
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200877 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
878 if (!desc)
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700879 goto unmap_exit;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200880
881 host->dma.data_desc = desc;
882 desc->callback = atmci_dma_complete;
883 desc->callback_param = host;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200884
Ludovic Desroches796211b2011-08-11 15:25:44 +0000885 return iflags;
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700886unmap_exit:
Linus Walleij88ce4db2011-02-10 16:08:16 +0100887 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700888 return -ENOMEM;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200889}
890
Ludovic Desroches796211b2011-08-11 15:25:44 +0000891static void
892atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
893{
894 return;
895}
896
897/*
898 * Start PDC according to transfer direction.
899 */
900static void
901atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data)
902{
903 if (data->flags & MMC_DATA_READ)
904 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
905 else
906 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
907}
908
909static void
910atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
Nicolas Ferre74791a22009-12-14 18:01:31 -0800911{
912 struct dma_chan *chan = host->data_chan;
913 struct dma_async_tx_descriptor *desc = host->dma.data_desc;
914
915 if (chan) {
Linus Walleij53289062011-02-10 16:08:26 +0100916 dmaengine_submit(desc);
917 dma_async_issue_pending(chan);
Nicolas Ferre74791a22009-12-14 18:01:31 -0800918 }
919}
920
Ludovic Desroches796211b2011-08-11 15:25:44 +0000921static void atmci_stop_transfer(struct atmel_mci *host)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200922{
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200923 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000924 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200925}
926
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200927/*
Ludovic Desroches796211b2011-08-11 15:25:44 +0000928 * Stop data transfer because error(s) occured.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200929 */
Ludovic Desroches796211b2011-08-11 15:25:44 +0000930static void atmci_stop_transfer_pdc(struct atmel_mci *host)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200931{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000932 atmci_set_pending(host, EVENT_XFER_COMPLETE);
933 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200934}
935
Ludovic Desroches796211b2011-08-11 15:25:44 +0000936static void atmci_stop_transfer_dma(struct atmel_mci *host)
937{
938 struct dma_chan *chan = host->data_chan;
939
940 if (chan) {
941 dmaengine_terminate_all(chan);
942 atmci_dma_cleanup(host);
943 } else {
944 /* Data transfer was stopped by the interrupt handler */
945 atmci_set_pending(host, EVENT_XFER_COMPLETE);
946 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
947 }
948}
949
950/*
951 * Start a request: prepare data if needed, prepare the command and activate
952 * interrupts.
953 */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200954static void atmci_start_request(struct atmel_mci *host,
955 struct atmel_mci_slot *slot)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200956{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200957 struct mmc_request *mrq;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200958 struct mmc_command *cmd;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200959 struct mmc_data *data;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200960 u32 iflags;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200961 u32 cmdflags;
962
963 mrq = slot->mrq;
964 host->cur_slot = slot;
965 host->mrq = mrq;
966
967 host->pending_events = 0;
968 host->completed_events = 0;
Haavard Skinnemoenca55f462008-10-05 15:16:59 +0200969 host->data_status = 0;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200970
971 if (host->need_reset) {
Ludovic Desroches18ee6842012-02-09 11:55:29 +0100972 iflags = atmci_readl(host, ATMCI_IMR);
973 iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000974 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
975 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
976 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000977 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000978 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Ludovic Desroches18ee6842012-02-09 11:55:29 +0100979 atmci_writel(host, ATMCI_IER, iflags);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200980 host->need_reset = false;
981 }
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000982 atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200983
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000984 iflags = atmci_readl(host, ATMCI_IMR);
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000985 if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200986 dev_warn(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
987 iflags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200988
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200989 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
990 /* Send init sequence (74 clock cycles) */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000991 atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
992 while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200993 cpu_relax();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200994 }
Nicolas Ferre74791a22009-12-14 18:01:31 -0800995 iflags = 0;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200996 data = mrq->data;
997 if (data) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200998 atmci_set_timeout(host, slot, data);
Haavard Skinnemoena252e3e2008-10-03 14:46:17 +0200999
1000 /* Must set block count/size before sending command */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001001 atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001002 | ATMCI_BLKLEN(data->blksz));
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001003 dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001004 ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
Nicolas Ferre74791a22009-12-14 18:01:31 -08001005
Ludovic Desroches796211b2011-08-11 15:25:44 +00001006 iflags |= host->prepare_data(host, data);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001007 }
1008
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001009 iflags |= ATMCI_CMDRDY;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001010 cmd = mrq->cmd;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001011 cmdflags = atmci_prepare_command(slot->mmc, cmd);
Ludovic Desroches11d14882011-08-11 15:25:45 +00001012 atmci_send_command(host, cmd, cmdflags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001013
1014 if (data)
Ludovic Desroches796211b2011-08-11 15:25:44 +00001015 host->submit_data(host, data);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001016
1017 if (mrq->stop) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001018 host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001019 host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001020 if (!(data->flags & MMC_DATA_WRITE))
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001021 host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001022 if (data->flags & MMC_DATA_STREAM)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001023 host->stop_cmdr |= ATMCI_CMDR_STREAM;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001024 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001025 host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001026 }
1027
1028 /*
1029 * We could have enabled interrupts earlier, but I suspect
1030 * that would open up a nice can of interesting race
1031 * conditions (e.g. command and data complete, but stop not
1032 * prepared yet.)
1033 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001034 atmci_writel(host, ATMCI_IER, iflags);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001035}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001036
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001037static void atmci_queue_request(struct atmel_mci *host,
1038 struct atmel_mci_slot *slot, struct mmc_request *mrq)
1039{
1040 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1041 host->state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001042
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001043 spin_lock_bh(&host->lock);
1044 slot->mrq = mrq;
1045 if (host->state == STATE_IDLE) {
1046 host->state = STATE_SENDING_CMD;
1047 atmci_start_request(host, slot);
1048 } else {
1049 list_add_tail(&slot->queue_node, &host->queue);
1050 }
1051 spin_unlock_bh(&host->lock);
1052}
1053
1054static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1055{
1056 struct atmel_mci_slot *slot = mmc_priv(mmc);
1057 struct atmel_mci *host = slot->host;
1058 struct mmc_data *data;
1059
1060 WARN_ON(slot->mrq);
1061
1062 /*
1063 * We may "know" the card is gone even though there's still an
1064 * electrical connection. If so, we really need to communicate
1065 * this to the MMC core since there won't be any more
1066 * interrupts as the card is completely removed. Otherwise,
1067 * the MMC core might believe the card is still there even
1068 * though the card was just removed very slowly.
1069 */
1070 if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
1071 mrq->cmd->error = -ENOMEDIUM;
1072 mmc_request_done(mmc, mrq);
1073 return;
1074 }
1075
1076 /* We don't support multiple blocks of weird lengths. */
1077 data = mrq->data;
1078 if (data && data->blocks > 1 && data->blksz & 3) {
1079 mrq->cmd->error = -EINVAL;
1080 mmc_request_done(mmc, mrq);
1081 }
1082
1083 atmci_queue_request(host, slot, mrq);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001084}
1085
1086static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1087{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001088 struct atmel_mci_slot *slot = mmc_priv(mmc);
1089 struct atmel_mci *host = slot->host;
1090 unsigned int i;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001091
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001092 slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001093 switch (ios->bus_width) {
1094 case MMC_BUS_WIDTH_1:
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001095 slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001096 break;
1097 case MMC_BUS_WIDTH_4:
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001098 slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001099 break;
1100 }
1101
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001102 if (ios->clock) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001103 unsigned int clock_min = ~0U;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001104 u32 clkdiv;
1105
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001106 spin_lock_bh(&host->lock);
1107 if (!host->mode_reg) {
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001108 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001109 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1110 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001111 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001112 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001113 }
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001114
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001115 /*
1116 * Use mirror of ios->clock to prevent race with mmc
1117 * core ios update when finding the minimum.
1118 */
1119 slot->clock = ios->clock;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001120 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001121 if (host->slot[i] && host->slot[i]->clock
1122 && host->slot[i]->clock < clock_min)
1123 clock_min = host->slot[i]->clock;
1124 }
1125
1126 /* Calculate clock divider */
1127 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001128 if (clkdiv > 255) {
1129 dev_warn(&mmc->class_dev,
1130 "clock %u too slow; using %lu\n",
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001131 clock_min, host->bus_hz / (2 * 256));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001132 clkdiv = 255;
1133 }
1134
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001135 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
Rob Emanuele04d699c2009-09-22 16:45:19 -07001136
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001137 /*
1138 * WRPROOF and RDPROOF prevent overruns/underruns by
1139 * stopping the clock when the FIFO is full/empty.
1140 * This state is not expected to last for long.
1141 */
Ludovic Desroches796211b2011-08-11 15:25:44 +00001142 if (host->caps.has_rwproof)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001143 host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001144
Ludovic Desroches796211b2011-08-11 15:25:44 +00001145 if (host->caps.has_cfg_reg) {
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001146 /* setup High Speed mode in relation with card capacity */
1147 if (ios->timing == MMC_TIMING_SD_HS)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001148 host->cfg_reg |= ATMCI_CFG_HSMODE;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001149 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001150 host->cfg_reg &= ~ATMCI_CFG_HSMODE;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001151 }
1152
1153 if (list_empty(&host->queue)) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001154 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001155 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001156 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001157 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001158 host->need_clock_update = true;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001159 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001160
1161 spin_unlock_bh(&host->lock);
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001162 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001163 bool any_slot_active = false;
1164
1165 spin_lock_bh(&host->lock);
1166 slot->clock = 0;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001167 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001168 if (host->slot[i] && host->slot[i]->clock) {
1169 any_slot_active = true;
1170 break;
1171 }
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001172 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001173 if (!any_slot_active) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001174 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001175 if (host->mode_reg) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001176 atmci_readl(host, ATMCI_MR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001177 clk_disable(host->mck);
1178 }
1179 host->mode_reg = 0;
1180 }
1181 spin_unlock_bh(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001182 }
1183
1184 switch (ios->power_mode) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001185 case MMC_POWER_UP:
1186 set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
1187 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001188 default:
1189 /*
1190 * TODO: None of the currently available AVR32-based
1191 * boards allow MMC power to be turned off. Implement
1192 * power control when this can be tested properly.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001193 *
1194 * We also need to hook this into the clock management
1195 * somehow so that newly inserted cards aren't
1196 * subjected to a fast clock before we have a chance
1197 * to figure out what the maximum rate is. Currently,
1198 * there's no way to avoid this, and there never will
1199 * be for boards that don't support power control.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001200 */
1201 break;
1202 }
1203}
1204
1205static int atmci_get_ro(struct mmc_host *mmc)
1206{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001207 int read_only = -ENOSYS;
1208 struct atmel_mci_slot *slot = mmc_priv(mmc);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001209
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001210 if (gpio_is_valid(slot->wp_pin)) {
1211 read_only = gpio_get_value(slot->wp_pin);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001212 dev_dbg(&mmc->class_dev, "card is %s\n",
1213 read_only ? "read-only" : "read-write");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001214 }
1215
1216 return read_only;
1217}
1218
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001219static int atmci_get_cd(struct mmc_host *mmc)
1220{
1221 int present = -ENOSYS;
1222 struct atmel_mci_slot *slot = mmc_priv(mmc);
1223
1224 if (gpio_is_valid(slot->detect_pin)) {
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001225 present = !(gpio_get_value(slot->detect_pin) ^
1226 slot->detect_is_active_high);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001227 dev_dbg(&mmc->class_dev, "card is %spresent\n",
1228 present ? "" : "not ");
1229 }
1230
1231 return present;
1232}
1233
Anders Grahn88ff82e2010-05-26 14:42:01 -07001234static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1235{
1236 struct atmel_mci_slot *slot = mmc_priv(mmc);
1237 struct atmel_mci *host = slot->host;
1238
1239 if (enable)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001240 atmci_writel(host, ATMCI_IER, slot->sdio_irq);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001241 else
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001242 atmci_writel(host, ATMCI_IDR, slot->sdio_irq);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001243}
1244
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001245static const struct mmc_host_ops atmci_ops = {
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001246 .request = atmci_request,
1247 .set_ios = atmci_set_ios,
1248 .get_ro = atmci_get_ro,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001249 .get_cd = atmci_get_cd,
Anders Grahn88ff82e2010-05-26 14:42:01 -07001250 .enable_sdio_irq = atmci_enable_sdio_irq,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001251};
1252
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001253/* Called with host->lock held */
1254static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
1255 __releases(&host->lock)
1256 __acquires(&host->lock)
1257{
1258 struct atmel_mci_slot *slot = NULL;
1259 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1260
1261 WARN_ON(host->cmd || host->data);
1262
1263 /*
1264 * Update the MMC clock rate if necessary. This may be
1265 * necessary if set_ios() is called when a different slot is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001266 * busy transferring data.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001267 */
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001268 if (host->need_clock_update) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001269 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001270 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001271 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001272 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001273
1274 host->cur_slot->mrq = NULL;
1275 host->mrq = NULL;
1276 if (!list_empty(&host->queue)) {
1277 slot = list_entry(host->queue.next,
1278 struct atmel_mci_slot, queue_node);
1279 list_del(&slot->queue_node);
1280 dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
1281 mmc_hostname(slot->mmc));
1282 host->state = STATE_SENDING_CMD;
1283 atmci_start_request(host, slot);
1284 } else {
1285 dev_vdbg(&host->pdev->dev, "list empty\n");
1286 host->state = STATE_IDLE;
1287 }
1288
1289 spin_unlock(&host->lock);
1290 mmc_request_done(prev_mmc, mrq);
1291 spin_lock(&host->lock);
1292}
1293
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001294static void atmci_command_complete(struct atmel_mci *host,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001295 struct mmc_command *cmd)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001296{
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001297 u32 status = host->cmd_status;
1298
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001299 /* Read the response from the card (up to 16 bytes) */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001300 cmd->resp[0] = atmci_readl(host, ATMCI_RSPR);
1301 cmd->resp[1] = atmci_readl(host, ATMCI_RSPR);
1302 cmd->resp[2] = atmci_readl(host, ATMCI_RSPR);
1303 cmd->resp[3] = atmci_readl(host, ATMCI_RSPR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001304
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001305 if (status & ATMCI_RTOE)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001306 cmd->error = -ETIMEDOUT;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001307 else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001308 cmd->error = -EILSEQ;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001309 else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001310 cmd->error = -EIO;
1311 else
1312 cmd->error = 0;
1313
1314 if (cmd->error) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001315 dev_dbg(&host->pdev->dev,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001316 "command error: status=0x%08x\n", status);
1317
1318 if (cmd->data) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00001319 host->stop_transfer(host);
Nicolas Ferre009a8912010-05-11 14:06:49 -07001320 host->data = NULL;
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001321 atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001322 | ATMCI_TXRDY | ATMCI_RXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001323 | ATMCI_DATA_ERROR_FLAGS);
1324 }
1325 }
1326}
1327
1328static void atmci_detect_change(unsigned long data)
1329{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001330 struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
1331 bool present;
1332 bool present_old;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001333
1334 /*
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001335 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
1336 * freeing the interrupt. We must not re-enable the interrupt
1337 * if it has been freed, and if we're shutting down, it
1338 * doesn't really matter whether the card is present or not.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001339 */
1340 smp_rmb();
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001341 if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001342 return;
1343
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001344 enable_irq(gpio_to_irq(slot->detect_pin));
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001345 present = !(gpio_get_value(slot->detect_pin) ^
1346 slot->detect_is_active_high);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001347 present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001348
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001349 dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
1350 present, present_old);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001351
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001352 if (present != present_old) {
1353 struct atmel_mci *host = slot->host;
1354 struct mmc_request *mrq;
1355
1356 dev_dbg(&slot->mmc->class_dev, "card %s\n",
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001357 present ? "inserted" : "removed");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001358
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001359 spin_lock(&host->lock);
1360
1361 if (!present)
1362 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1363 else
1364 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001365
1366 /* Clean up queue if present */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001367 mrq = slot->mrq;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001368 if (mrq) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001369 if (mrq == host->mrq) {
1370 /*
1371 * Reset controller to terminate any ongoing
1372 * commands or data transfers.
1373 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001374 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1375 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1376 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001377 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001378 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001379
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001380 host->data = NULL;
1381 host->cmd = NULL;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001382
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001383 switch (host->state) {
1384 case STATE_IDLE:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001385 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001386 case STATE_SENDING_CMD:
1387 mrq->cmd->error = -ENOMEDIUM;
1388 if (!mrq->data)
1389 break;
1390 /* fall through */
1391 case STATE_SENDING_DATA:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001392 mrq->data->error = -ENOMEDIUM;
Ludovic Desroches796211b2011-08-11 15:25:44 +00001393 host->stop_transfer(host);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001394 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001395 case STATE_DATA_BUSY:
1396 case STATE_DATA_ERROR:
1397 if (mrq->data->error == -EINPROGRESS)
1398 mrq->data->error = -ENOMEDIUM;
1399 if (!mrq->stop)
1400 break;
1401 /* fall through */
1402 case STATE_SENDING_STOP:
1403 mrq->stop->error = -ENOMEDIUM;
1404 break;
1405 }
1406
1407 atmci_request_end(host, mrq);
1408 } else {
1409 list_del(&slot->queue_node);
1410 mrq->cmd->error = -ENOMEDIUM;
1411 if (mrq->data)
1412 mrq->data->error = -ENOMEDIUM;
1413 if (mrq->stop)
1414 mrq->stop->error = -ENOMEDIUM;
1415
1416 spin_unlock(&host->lock);
1417 mmc_request_done(slot->mmc, mrq);
1418 spin_lock(&host->lock);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001419 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001420 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001421 spin_unlock(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001422
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001423 mmc_detect_change(slot->mmc, 0);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001424 }
1425}
1426
1427static void atmci_tasklet_func(unsigned long priv)
1428{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001429 struct atmel_mci *host = (struct atmel_mci *)priv;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001430 struct mmc_request *mrq = host->mrq;
1431 struct mmc_data *data = host->data;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001432 struct mmc_command *cmd = host->cmd;
1433 enum atmel_mci_state state = host->state;
1434 enum atmel_mci_state prev_state;
1435 u32 status;
1436
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001437 spin_lock(&host->lock);
1438
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001439 state = host->state;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001440
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001441 dev_vdbg(&host->pdev->dev,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001442 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1443 state, host->pending_events, host->completed_events,
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001444 atmci_readl(host, ATMCI_IMR));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001445
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001446 do {
1447 prev_state = state;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001448
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001449 switch (state) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001450 case STATE_IDLE:
1451 break;
1452
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001453 case STATE_SENDING_CMD:
1454 if (!atmci_test_and_clear_pending(host,
1455 EVENT_CMD_COMPLETE))
1456 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001457
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001458 host->cmd = NULL;
1459 atmci_set_completed(host, EVENT_CMD_COMPLETE);
1460 atmci_command_complete(host, mrq->cmd);
1461 if (!mrq->data || cmd->error) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001462 atmci_request_end(host, host->mrq);
1463 goto unlock;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001464 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001465
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001466 prev_state = state = STATE_SENDING_DATA;
1467 /* fall through */
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001468
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001469 case STATE_SENDING_DATA:
1470 if (atmci_test_and_clear_pending(host,
1471 EVENT_DATA_ERROR)) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00001472 host->stop_transfer(host);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001473 if (data->stop)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001474 atmci_send_stop_cmd(host, data);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001475 state = STATE_DATA_ERROR;
1476 break;
1477 }
1478
1479 if (!atmci_test_and_clear_pending(host,
1480 EVENT_XFER_COMPLETE))
1481 break;
1482
1483 atmci_set_completed(host, EVENT_XFER_COMPLETE);
1484 prev_state = state = STATE_DATA_BUSY;
1485 /* fall through */
1486
1487 case STATE_DATA_BUSY:
1488 if (!atmci_test_and_clear_pending(host,
1489 EVENT_DATA_COMPLETE))
1490 break;
1491
1492 host->data = NULL;
1493 atmci_set_completed(host, EVENT_DATA_COMPLETE);
1494 status = host->data_status;
1495 if (unlikely(status & ATMCI_DATA_ERROR_FLAGS)) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001496 if (status & ATMCI_DTOE) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001497 dev_dbg(&host->pdev->dev,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001498 "data timeout error\n");
1499 data->error = -ETIMEDOUT;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001500 } else if (status & ATMCI_DCRCE) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001501 dev_dbg(&host->pdev->dev,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001502 "data CRC error\n");
1503 data->error = -EILSEQ;
1504 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001505 dev_dbg(&host->pdev->dev,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001506 "data FIFO error (status=%08x)\n",
1507 status);
1508 data->error = -EIO;
1509 }
1510 } else {
1511 data->bytes_xfered = data->blocks * data->blksz;
1512 data->error = 0;
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001513 atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001514 }
1515
1516 if (!data->stop) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001517 atmci_request_end(host, host->mrq);
1518 goto unlock;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001519 }
1520
1521 prev_state = state = STATE_SENDING_STOP;
1522 if (!data->error)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001523 atmci_send_stop_cmd(host, data);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001524 /* fall through */
1525
1526 case STATE_SENDING_STOP:
1527 if (!atmci_test_and_clear_pending(host,
1528 EVENT_CMD_COMPLETE))
1529 break;
1530
1531 host->cmd = NULL;
1532 atmci_command_complete(host, mrq->stop);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001533 atmci_request_end(host, host->mrq);
1534 goto unlock;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001535
1536 case STATE_DATA_ERROR:
1537 if (!atmci_test_and_clear_pending(host,
1538 EVENT_XFER_COMPLETE))
1539 break;
1540
1541 state = STATE_DATA_BUSY;
1542 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001543 }
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001544 } while (state != prev_state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001545
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001546 host->state = state;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001547
1548unlock:
1549 spin_unlock(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001550}
1551
1552static void atmci_read_data_pio(struct atmel_mci *host)
1553{
1554 struct scatterlist *sg = host->sg;
1555 void *buf = sg_virt(sg);
1556 unsigned int offset = host->pio_offset;
1557 struct mmc_data *data = host->data;
1558 u32 value;
1559 u32 status;
1560 unsigned int nbytes = 0;
1561
1562 do {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001563 value = atmci_readl(host, ATMCI_RDR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001564 if (likely(offset + 4 <= sg->length)) {
1565 put_unaligned(value, (u32 *)(buf + offset));
1566
1567 offset += 4;
1568 nbytes += 4;
1569
1570 if (offset == sg->length) {
Haavard Skinnemoen5e7184a2008-10-05 15:27:50 +02001571 flush_dcache_page(sg_page(sg));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001572 host->sg = sg = sg_next(sg);
1573 if (!sg)
1574 goto done;
1575
1576 offset = 0;
1577 buf = sg_virt(sg);
1578 }
1579 } else {
1580 unsigned int remaining = sg->length - offset;
1581 memcpy(buf + offset, &value, remaining);
1582 nbytes += remaining;
1583
1584 flush_dcache_page(sg_page(sg));
1585 host->sg = sg = sg_next(sg);
1586 if (!sg)
1587 goto done;
1588
1589 offset = 4 - remaining;
1590 buf = sg_virt(sg);
1591 memcpy(buf, (u8 *)&value + remaining, offset);
1592 nbytes += offset;
1593 }
1594
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001595 status = atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001596 if (status & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001597 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001598 | ATMCI_DATA_ERROR_FLAGS));
1599 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001600 data->bytes_xfered += nbytes;
1601 smp_wmb();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001602 atmci_set_pending(host, EVENT_DATA_ERROR);
1603 tasklet_schedule(&host->tasklet);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001604 return;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001605 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001606 } while (status & ATMCI_RXRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001607
1608 host->pio_offset = offset;
1609 data->bytes_xfered += nbytes;
1610
1611 return;
1612
1613done:
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001614 atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
1615 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001616 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001617 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001618 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001619}
1620
1621static void atmci_write_data_pio(struct atmel_mci *host)
1622{
1623 struct scatterlist *sg = host->sg;
1624 void *buf = sg_virt(sg);
1625 unsigned int offset = host->pio_offset;
1626 struct mmc_data *data = host->data;
1627 u32 value;
1628 u32 status;
1629 unsigned int nbytes = 0;
1630
1631 do {
1632 if (likely(offset + 4 <= sg->length)) {
1633 value = get_unaligned((u32 *)(buf + offset));
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001634 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001635
1636 offset += 4;
1637 nbytes += 4;
1638 if (offset == sg->length) {
1639 host->sg = sg = sg_next(sg);
1640 if (!sg)
1641 goto done;
1642
1643 offset = 0;
1644 buf = sg_virt(sg);
1645 }
1646 } else {
1647 unsigned int remaining = sg->length - offset;
1648
1649 value = 0;
1650 memcpy(&value, buf + offset, remaining);
1651 nbytes += remaining;
1652
1653 host->sg = sg = sg_next(sg);
1654 if (!sg) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001655 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001656 goto done;
1657 }
1658
1659 offset = 4 - remaining;
1660 buf = sg_virt(sg);
1661 memcpy((u8 *)&value + remaining, buf, offset);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001662 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001663 nbytes += offset;
1664 }
1665
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001666 status = atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001667 if (status & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001668 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001669 | ATMCI_DATA_ERROR_FLAGS));
1670 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001671 data->bytes_xfered += nbytes;
1672 smp_wmb();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001673 atmci_set_pending(host, EVENT_DATA_ERROR);
1674 tasklet_schedule(&host->tasklet);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001675 return;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001676 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001677 } while (status & ATMCI_TXRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001678
1679 host->pio_offset = offset;
1680 data->bytes_xfered += nbytes;
1681
1682 return;
1683
1684done:
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001685 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
1686 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001687 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001688 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001689 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001690}
1691
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001692static void atmci_cmd_interrupt(struct atmel_mci *host, u32 status)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001693{
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001694 atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001695
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001696 host->cmd_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001697 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001698 atmci_set_pending(host, EVENT_CMD_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001699 tasklet_schedule(&host->tasklet);
1700}
1701
Anders Grahn88ff82e2010-05-26 14:42:01 -07001702static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
1703{
1704 int i;
1705
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001706 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Anders Grahn88ff82e2010-05-26 14:42:01 -07001707 struct atmel_mci_slot *slot = host->slot[i];
1708 if (slot && (status & slot->sdio_irq)) {
1709 mmc_signal_sdio_irq(slot->mmc);
1710 }
1711 }
1712}
1713
1714
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001715static irqreturn_t atmci_interrupt(int irq, void *dev_id)
1716{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001717 struct atmel_mci *host = dev_id;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001718 u32 status, mask, pending;
1719 unsigned int pass_count = 0;
1720
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001721 do {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001722 status = atmci_readl(host, ATMCI_SR);
1723 mask = atmci_readl(host, ATMCI_IMR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001724 pending = status & mask;
1725 if (!pending)
1726 break;
1727
1728 if (pending & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001729 atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001730 | ATMCI_RXRDY | ATMCI_TXRDY);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001731 pending &= atmci_readl(host, ATMCI_IMR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001732
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001733 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001734 smp_wmb();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001735 atmci_set_pending(host, EVENT_DATA_ERROR);
1736 tasklet_schedule(&host->tasklet);
1737 }
Ludovic Desroches796211b2011-08-11 15:25:44 +00001738
Ludovic Desroches796211b2011-08-11 15:25:44 +00001739 if (pending & ATMCI_TXBUFE) {
1740 atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001741 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001742 /*
1743 * We can receive this interruption before having configured
1744 * the second pdc buffer, so we need to reconfigure first and
1745 * second buffers again
1746 */
1747 if (host->data_size) {
1748 atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001749 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001750 atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
1751 } else {
1752 atmci_pdc_complete(host);
1753 }
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001754 } else if (pending & ATMCI_ENDTX) {
1755 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
1756
1757 if (host->data_size) {
1758 atmci_pdc_set_single_buf(host,
1759 XFER_TRANSMIT, PDC_SECOND_BUF);
1760 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
1761 }
Ludovic Desroches796211b2011-08-11 15:25:44 +00001762 }
1763
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001764 if (pending & ATMCI_RXBUFF) {
1765 atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
1766 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
1767 /*
1768 * We can receive this interruption before having configured
1769 * the second pdc buffer, so we need to reconfigure first and
1770 * second buffers again
1771 */
1772 if (host->data_size) {
1773 atmci_pdc_set_both_buf(host, XFER_RECEIVE);
1774 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
1775 atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
1776 } else {
1777 atmci_pdc_complete(host);
1778 }
1779 } else if (pending & ATMCI_ENDRX) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00001780 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
1781
1782 if (host->data_size) {
1783 atmci_pdc_set_single_buf(host,
1784 XFER_RECEIVE, PDC_SECOND_BUF);
1785 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
1786 }
1787 }
1788
Ludovic Desroches796211b2011-08-11 15:25:44 +00001789
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001790 if (pending & ATMCI_NOTBUSY) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001791 atmci_writel(host, ATMCI_IDR,
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001792 ATMCI_DATA_ERROR_FLAGS | ATMCI_NOTBUSY);
Haavard Skinnemoenca55f462008-10-05 15:16:59 +02001793 if (!host->data_status)
1794 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001795 smp_wmb();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001796 atmci_set_pending(host, EVENT_DATA_COMPLETE);
1797 tasklet_schedule(&host->tasklet);
1798 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001799 if (pending & ATMCI_RXRDY)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001800 atmci_read_data_pio(host);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001801 if (pending & ATMCI_TXRDY)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001802 atmci_write_data_pio(host);
1803
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001804 if (pending & ATMCI_CMDRDY)
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001805 atmci_cmd_interrupt(host, status);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001806
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001807 if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
Anders Grahn88ff82e2010-05-26 14:42:01 -07001808 atmci_sdio_interrupt(host, status);
1809
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001810 } while (pass_count++ < 5);
1811
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001812 return pass_count ? IRQ_HANDLED : IRQ_NONE;
1813}
1814
1815static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
1816{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001817 struct atmel_mci_slot *slot = dev_id;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001818
1819 /*
1820 * Disable interrupts until the pin has stabilized and check
1821 * the state then. Use mod_timer() since we may be in the
1822 * middle of the timer routine when this interrupt triggers.
1823 */
1824 disable_irq_nosync(irq);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001825 mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001826
1827 return IRQ_HANDLED;
1828}
1829
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001830static int __init atmci_init_slot(struct atmel_mci *host,
1831 struct mci_slot_pdata *slot_data, unsigned int id,
Anders Grahn88ff82e2010-05-26 14:42:01 -07001832 u32 sdc_reg, u32 sdio_irq)
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001833{
1834 struct mmc_host *mmc;
1835 struct atmel_mci_slot *slot;
1836
1837 mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
1838 if (!mmc)
1839 return -ENOMEM;
1840
1841 slot = mmc_priv(mmc);
1842 slot->mmc = mmc;
1843 slot->host = host;
1844 slot->detect_pin = slot_data->detect_pin;
1845 slot->wp_pin = slot_data->wp_pin;
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001846 slot->detect_is_active_high = slot_data->detect_is_active_high;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001847 slot->sdc_reg = sdc_reg;
Anders Grahn88ff82e2010-05-26 14:42:01 -07001848 slot->sdio_irq = sdio_irq;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001849
1850 mmc->ops = &atmci_ops;
1851 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
1852 mmc->f_max = host->bus_hz / 2;
1853 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Anders Grahn88ff82e2010-05-26 14:42:01 -07001854 if (sdio_irq)
1855 mmc->caps |= MMC_CAP_SDIO_IRQ;
Ludovic Desroches796211b2011-08-11 15:25:44 +00001856 if (host->caps.has_highspeed)
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001857 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001858 if (slot_data->bus_width >= 4)
1859 mmc->caps |= MMC_CAP_4_BIT_DATA;
1860
Martin K. Petersena36274e2010-09-10 01:33:59 -04001861 mmc->max_segs = 64;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001862 mmc->max_req_size = 32768 * 512;
1863 mmc->max_blk_size = 32768;
1864 mmc->max_blk_count = 512;
1865
1866 /* Assume card is present initially */
1867 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
1868 if (gpio_is_valid(slot->detect_pin)) {
1869 if (gpio_request(slot->detect_pin, "mmc_detect")) {
1870 dev_dbg(&mmc->class_dev, "no detect pin available\n");
1871 slot->detect_pin = -EBUSY;
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001872 } else if (gpio_get_value(slot->detect_pin) ^
1873 slot->detect_is_active_high) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001874 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1875 }
1876 }
1877
1878 if (!gpio_is_valid(slot->detect_pin))
1879 mmc->caps |= MMC_CAP_NEEDS_POLL;
1880
1881 if (gpio_is_valid(slot->wp_pin)) {
1882 if (gpio_request(slot->wp_pin, "mmc_wp")) {
1883 dev_dbg(&mmc->class_dev, "no WP pin available\n");
1884 slot->wp_pin = -EBUSY;
1885 }
1886 }
1887
1888 host->slot[id] = slot;
1889 mmc_add_host(mmc);
1890
1891 if (gpio_is_valid(slot->detect_pin)) {
1892 int ret;
1893
1894 setup_timer(&slot->detect_timer, atmci_detect_change,
1895 (unsigned long)slot);
1896
1897 ret = request_irq(gpio_to_irq(slot->detect_pin),
1898 atmci_detect_interrupt,
1899 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1900 "mmc-detect", slot);
1901 if (ret) {
1902 dev_dbg(&mmc->class_dev,
1903 "could not request IRQ %d for detect pin\n",
1904 gpio_to_irq(slot->detect_pin));
1905 gpio_free(slot->detect_pin);
1906 slot->detect_pin = -EBUSY;
1907 }
1908 }
1909
1910 atmci_init_debugfs(slot);
1911
1912 return 0;
1913}
1914
1915static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
1916 unsigned int id)
1917{
1918 /* Debugfs stuff is cleaned up by mmc core */
1919
1920 set_bit(ATMCI_SHUTDOWN, &slot->flags);
1921 smp_wmb();
1922
1923 mmc_remove_host(slot->mmc);
1924
1925 if (gpio_is_valid(slot->detect_pin)) {
1926 int pin = slot->detect_pin;
1927
1928 free_irq(gpio_to_irq(pin), slot);
1929 del_timer_sync(&slot->detect_timer);
1930 gpio_free(pin);
1931 }
1932 if (gpio_is_valid(slot->wp_pin))
1933 gpio_free(slot->wp_pin);
1934
1935 slot->host->slot[id] = NULL;
1936 mmc_free_host(slot->mmc);
1937}
1938
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001939static bool atmci_filter(struct dma_chan *chan, void *slave)
Dan Williams74465b42009-01-06 11:38:16 -07001940{
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001941 struct mci_dma_data *sl = slave;
Dan Williams74465b42009-01-06 11:38:16 -07001942
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001943 if (sl && find_slave_dev(sl) == chan->device->dev) {
1944 chan->private = slave_data_ptr(sl);
Dan Williams7dd60252009-01-06 11:38:19 -07001945 return true;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001946 } else {
Dan Williams7dd60252009-01-06 11:38:19 -07001947 return false;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001948 }
Dan Williams74465b42009-01-06 11:38:16 -07001949}
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001950
1951static void atmci_configure_dma(struct atmel_mci *host)
1952{
1953 struct mci_platform_data *pdata;
1954
1955 if (host == NULL)
1956 return;
1957
1958 pdata = host->pdev->dev.platform_data;
1959
1960 if (pdata && find_slave_dev(pdata->dma_slave)) {
1961 dma_cap_mask_t mask;
1962
1963 setup_dma_addr(pdata->dma_slave,
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001964 host->mapbase + ATMCI_TDR,
1965 host->mapbase + ATMCI_RDR);
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001966
1967 /* Try to grab a DMA channel */
1968 dma_cap_zero(mask);
1969 dma_cap_set(DMA_SLAVE, mask);
1970 host->dma.chan =
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001971 dma_request_channel(mask, atmci_filter, pdata->dma_slave);
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001972 }
1973 if (!host->dma.chan)
1974 dev_notice(&host->pdev->dev, "DMA not available, using PIO\n");
Nicolas Ferre74791a22009-12-14 18:01:31 -08001975 else
1976 dev_info(&host->pdev->dev,
1977 "Using %s for DMA transfers\n",
1978 dma_chan_name(host->dma.chan));
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001979}
Ludovic Desroches796211b2011-08-11 15:25:44 +00001980
1981static inline unsigned int atmci_get_version(struct atmel_mci *host)
1982{
1983 return atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
1984}
1985
1986/*
1987 * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
1988 * HSMCI provides DMA support and a new config register but no more supports
1989 * PDC.
1990 */
1991static void __init atmci_get_cap(struct atmel_mci *host)
1992{
1993 unsigned int version;
1994
1995 version = atmci_get_version(host);
1996 dev_info(&host->pdev->dev,
1997 "version: 0x%x\n", version);
1998
1999 host->caps.has_dma = 0;
2000 host->caps.has_pdc = 0;
2001 host->caps.has_cfg_reg = 0;
2002 host->caps.has_cstor_reg = 0;
2003 host->caps.has_highspeed = 0;
2004 host->caps.has_rwproof = 0;
2005
2006 /* keep only major version number */
2007 switch (version & 0xf00) {
2008 case 0x100:
2009 case 0x200:
2010 host->caps.has_pdc = 1;
2011 host->caps.has_rwproof = 1;
2012 break;
2013 case 0x300:
2014 case 0x400:
2015 case 0x500:
2016#ifdef CONFIG_AT_HDMAC
2017 host->caps.has_dma = 1;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002018#else
Ludovic Desroches796211b2011-08-11 15:25:44 +00002019 host->caps.has_dma = 0;
2020 dev_info(&host->pdev->dev,
2021 "has dma capability but dma engine is not selected, then use pio\n");
Dan Williams74465b42009-01-06 11:38:16 -07002022#endif
Ludovic Desroches796211b2011-08-11 15:25:44 +00002023 host->caps.has_cfg_reg = 1;
2024 host->caps.has_cstor_reg = 1;
2025 host->caps.has_highspeed = 1;
2026 host->caps.has_rwproof = 1;
2027 break;
2028 default:
2029 dev_warn(&host->pdev->dev,
2030 "Unmanaged mci version, set minimum capabilities\n");
2031 break;
2032 }
2033}
Dan Williams74465b42009-01-06 11:38:16 -07002034
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002035static int __init atmci_probe(struct platform_device *pdev)
2036{
2037 struct mci_platform_data *pdata;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002038 struct atmel_mci *host;
2039 struct resource *regs;
2040 unsigned int nr_slots;
2041 int irq;
2042 int ret;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002043
2044 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2045 if (!regs)
2046 return -ENXIO;
2047 pdata = pdev->dev.platform_data;
2048 if (!pdata)
2049 return -ENXIO;
2050 irq = platform_get_irq(pdev, 0);
2051 if (irq < 0)
2052 return irq;
2053
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002054 host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL);
2055 if (!host)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002056 return -ENOMEM;
2057
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002058 host->pdev = pdev;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002059 spin_lock_init(&host->lock);
2060 INIT_LIST_HEAD(&host->queue);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002061
2062 host->mck = clk_get(&pdev->dev, "mci_clk");
2063 if (IS_ERR(host->mck)) {
2064 ret = PTR_ERR(host->mck);
2065 goto err_clk_get;
2066 }
2067
2068 ret = -ENOMEM;
H Hartley Sweetene8e3f6c2009-12-14 14:11:56 -05002069 host->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002070 if (!host->regs)
2071 goto err_ioremap;
2072
2073 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002074 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002075 host->bus_hz = clk_get_rate(host->mck);
2076 clk_disable(host->mck);
2077
2078 host->mapbase = regs->start;
2079
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002080 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002081
Kay Sievers89c8aa22009-02-02 21:08:30 +01002082 ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002083 if (ret)
2084 goto err_request_irq;
2085
Ludovic Desroches796211b2011-08-11 15:25:44 +00002086 /* Get MCI capabilities and set operations according to it */
2087 atmci_get_cap(host);
2088 if (host->caps.has_dma) {
2089 dev_info(&pdev->dev, "using DMA\n");
2090 host->prepare_data = &atmci_prepare_data_dma;
2091 host->submit_data = &atmci_submit_data_dma;
2092 host->stop_transfer = &atmci_stop_transfer_dma;
2093 } else if (host->caps.has_pdc) {
2094 dev_info(&pdev->dev, "using PDC\n");
2095 host->prepare_data = &atmci_prepare_data_pdc;
2096 host->submit_data = &atmci_submit_data_pdc;
2097 host->stop_transfer = &atmci_stop_transfer_pdc;
2098 } else {
2099 dev_info(&pdev->dev, "no DMA, no PDC\n");
2100 host->prepare_data = &atmci_prepare_data;
2101 host->submit_data = &atmci_submit_data;
2102 host->stop_transfer = &atmci_stop_transfer;
2103 }
2104
2105 if (host->caps.has_dma)
2106 atmci_configure_dma(host);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02002107
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002108 platform_set_drvdata(pdev, host);
2109
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002110 /* We need at least one slot to succeed */
2111 nr_slots = 0;
2112 ret = -ENODEV;
2113 if (pdata->slot[0].bus_width) {
2114 ret = atmci_init_slot(host, &pdata->slot[0],
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002115 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002116 if (!ret)
2117 nr_slots++;
2118 }
2119 if (pdata->slot[1].bus_width) {
2120 ret = atmci_init_slot(host, &pdata->slot[1],
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002121 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002122 if (!ret)
2123 nr_slots++;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002124 }
2125
Rob Emanuele04d699c2009-09-22 16:45:19 -07002126 if (!nr_slots) {
2127 dev_err(&pdev->dev, "init failed: no slot defined\n");
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002128 goto err_init_slot;
Rob Emanuele04d699c2009-09-22 16:45:19 -07002129 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002130
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002131 dev_info(&pdev->dev,
2132 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
2133 host->mapbase, irq, nr_slots);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +02002134
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002135 return 0;
2136
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002137err_init_slot:
Dan Williams74465b42009-01-06 11:38:16 -07002138 if (host->dma.chan)
2139 dma_release_channel(host->dma.chan);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002140 free_irq(irq, host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002141err_request_irq:
2142 iounmap(host->regs);
2143err_ioremap:
2144 clk_put(host->mck);
2145err_clk_get:
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002146 kfree(host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002147 return ret;
2148}
2149
2150static int __exit atmci_remove(struct platform_device *pdev)
2151{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002152 struct atmel_mci *host = platform_get_drvdata(pdev);
2153 unsigned int i;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002154
2155 platform_set_drvdata(pdev, NULL);
2156
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002157 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002158 if (host->slot[i])
2159 atmci_cleanup_slot(host->slot[i], i);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002160 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002161
2162 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002163 atmci_writel(host, ATMCI_IDR, ~0UL);
2164 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
2165 atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002166 clk_disable(host->mck);
2167
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02002168#ifdef CONFIG_MMC_ATMELMCI_DMA
Dan Williams74465b42009-01-06 11:38:16 -07002169 if (host->dma.chan)
2170 dma_release_channel(host->dma.chan);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02002171#endif
2172
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002173 free_irq(platform_get_irq(pdev, 0), host);
2174 iounmap(host->regs);
2175
2176 clk_put(host->mck);
2177 kfree(host);
2178
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002179 return 0;
2180}
2181
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002182#ifdef CONFIG_PM
2183static int atmci_suspend(struct device *dev)
2184{
2185 struct atmel_mci *host = dev_get_drvdata(dev);
2186 int i;
2187
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002188 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002189 struct atmel_mci_slot *slot = host->slot[i];
2190 int ret;
2191
2192 if (!slot)
2193 continue;
2194 ret = mmc_suspend_host(slot->mmc);
2195 if (ret < 0) {
2196 while (--i >= 0) {
2197 slot = host->slot[i];
2198 if (slot
2199 && test_bit(ATMCI_SUSPENDED, &slot->flags)) {
2200 mmc_resume_host(host->slot[i]->mmc);
2201 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2202 }
2203 }
2204 return ret;
2205 } else {
2206 set_bit(ATMCI_SUSPENDED, &slot->flags);
2207 }
2208 }
2209
2210 return 0;
2211}
2212
2213static int atmci_resume(struct device *dev)
2214{
2215 struct atmel_mci *host = dev_get_drvdata(dev);
2216 int i;
2217 int ret = 0;
2218
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002219 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002220 struct atmel_mci_slot *slot = host->slot[i];
2221 int err;
2222
2223 slot = host->slot[i];
2224 if (!slot)
2225 continue;
2226 if (!test_bit(ATMCI_SUSPENDED, &slot->flags))
2227 continue;
2228 err = mmc_resume_host(slot->mmc);
2229 if (err < 0)
2230 ret = err;
2231 else
2232 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2233 }
2234
2235 return ret;
2236}
2237static SIMPLE_DEV_PM_OPS(atmci_pm, atmci_suspend, atmci_resume);
2238#define ATMCI_PM_OPS (&atmci_pm)
2239#else
2240#define ATMCI_PM_OPS NULL
2241#endif
2242
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002243static struct platform_driver atmci_driver = {
2244 .remove = __exit_p(atmci_remove),
2245 .driver = {
2246 .name = "atmel_mci",
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002247 .pm = ATMCI_PM_OPS,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002248 },
2249};
2250
2251static int __init atmci_init(void)
2252{
2253 return platform_driver_probe(&atmci_driver, atmci_probe);
2254}
2255
2256static void __exit atmci_exit(void)
2257{
2258 platform_driver_unregister(&atmci_driver);
2259}
2260
Dan Williams74465b42009-01-06 11:38:16 -07002261late_initcall(atmci_init); /* try to load after dma driver when built-in */
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002262module_exit(atmci_exit);
2263
2264MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002265MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002266MODULE_LICENSE("GPL v2");