blob: 5fe83008b74653e5229702aaa2b4701fb191447d [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>
Viresh Kumare2b35f32012-02-01 16:12:27 +053027#include <linux/types.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020028
29#include <linux/mmc/host.h>
Nicolas Ferre2f1d7912010-12-10 19:14:32 +010030#include <linux/mmc/sdio.h>
Nicolas Ferre2635d1b2009-12-14 18:01:30 -080031
32#include <mach/atmel-mci.h>
Nicolas Ferrec42aa772008-11-20 15:59:12 +010033#include <linux/atmel-mci.h>
Ludovic Desroches796211b2011-08-11 15:25:44 +000034#include <linux/atmel_pdc.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020035
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020036#include <asm/io.h>
37#include <asm/unaligned.h>
38
Rob Emanuele04d699c2009-09-22 16:45:19 -070039#include <mach/cpu.h>
Haavard Skinnemoen3663b732008-08-05 13:57:38 +020040#include <mach/board.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020041
42#include "atmel-mci-regs.h"
43
Ludovic Desroches2c96a292011-08-11 15:25:41 +000044#define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020045#define ATMCI_DMA_THRESHOLD 16
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020046
47enum {
Ludovic Desrochesf5177542012-05-16 15:25:59 +020048 EVENT_CMD_RDY = 0,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020049 EVENT_XFER_COMPLETE,
Ludovic Desrochesf5177542012-05-16 15:25:59 +020050 EVENT_NOTBUSY,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +020051 EVENT_DATA_ERROR,
52};
53
54enum atmel_mci_state {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +020055 STATE_IDLE = 0,
56 STATE_SENDING_CMD,
Ludovic Desrochesf5177542012-05-16 15:25:59 +020057 STATE_DATA_XFER,
58 STATE_WAITING_NOTBUSY,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +020059 STATE_SENDING_STOP,
Ludovic Desrochesf5177542012-05-16 15:25:59 +020060 STATE_END_REQUEST,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020061};
62
Ludovic Desroches796211b2011-08-11 15:25:44 +000063enum atmci_xfer_dir {
64 XFER_RECEIVE = 0,
65 XFER_TRANSMIT,
66};
67
68enum atmci_pdc_buf {
69 PDC_FIRST_BUF = 0,
70 PDC_SECOND_BUF,
71};
72
73struct atmel_mci_caps {
74 bool has_dma;
75 bool has_pdc;
76 bool has_cfg_reg;
77 bool has_cstor_reg;
78 bool has_highspeed;
79 bool has_rwproof;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +010080 bool has_odd_clk_div;
Ludovic Desroches24011f32012-05-16 15:26:00 +020081 bool has_bad_data_ordering;
82 bool need_reset_after_xfer;
83 bool need_blksz_mul_4;
Ludovic Desroches796211b2011-08-11 15:25:44 +000084};
85
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020086struct atmel_mci_dma {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020087 struct dma_chan *chan;
88 struct dma_async_tx_descriptor *data_desc;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020089};
90
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +020091/**
92 * struct atmel_mci - MMC controller state shared between all slots
93 * @lock: Spinlock protecting the queue and associated data.
94 * @regs: Pointer to MMIO registers.
Ludovic Desroches796211b2011-08-11 15:25:44 +000095 * @sg: Scatterlist entry currently being processed by PIO or PDC code.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +020096 * @pio_offset: Offset into the current scatterlist entry.
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +020097 * @buffer: Buffer used if we don't have the r/w proof capability. We
98 * don't have the time to switch pdc buffers so we have to use only
99 * one buffer for the full transaction.
100 * @buf_size: size of the buffer.
101 * @phys_buf_addr: buffer address needed for pdc.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200102 * @cur_slot: The slot which is currently using the controller.
103 * @mrq: The request currently being processed on @cur_slot,
104 * or NULL if the controller is idle.
105 * @cmd: The command currently being sent to the card, or NULL.
106 * @data: The data currently being transferred, or NULL if no data
107 * transfer is in progress.
Ludovic Desroches796211b2011-08-11 15:25:44 +0000108 * @data_size: just data->blocks * data->blksz.
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200109 * @dma: DMA client state.
110 * @data_chan: DMA channel being used for the current data transfer.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200111 * @cmd_status: Snapshot of SR taken upon completion of the current
112 * command. Only valid when EVENT_CMD_COMPLETE is pending.
113 * @data_status: Snapshot of SR taken upon completion of the current
114 * data transfer. Only valid when EVENT_DATA_COMPLETE or
115 * EVENT_DATA_ERROR is pending.
116 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
117 * to be sent.
118 * @tasklet: Tasklet running the request state machine.
119 * @pending_events: Bitmask of events flagged by the interrupt handler
120 * to be processed by the tasklet.
121 * @completed_events: Bitmask of events which the state machine has
122 * processed.
123 * @state: Tasklet state.
124 * @queue: List of slots waiting for access to the controller.
125 * @need_clock_update: Update the clock rate before the next request.
126 * @need_reset: Reset controller before next request.
Ludovic Desroches24011f32012-05-16 15:26:00 +0200127 * @timer: Timer to balance the data timeout error flag which cannot rise.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200128 * @mode_reg: Value of the MR register.
Nicolas Ferre74791a22009-12-14 18:01:31 -0800129 * @cfg_reg: Value of the CFG register.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200130 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
131 * rate and timeout calculations.
132 * @mapbase: Physical address of the MMIO registers.
133 * @mck: The peripheral bus clock hooked up to the MMC controller.
134 * @pdev: Platform device associated with the MMC controller.
135 * @slot: Slots sharing this MMC controller.
Ludovic Desroches796211b2011-08-11 15:25:44 +0000136 * @caps: MCI capabilities depending on MCI version.
137 * @prepare_data: function to setup MCI before data transfer which
138 * depends on MCI capabilities.
139 * @submit_data: function to start data transfer which depends on MCI
140 * capabilities.
141 * @stop_transfer: function to stop data transfer which depends on MCI
142 * capabilities.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200143 *
144 * Locking
145 * =======
146 *
147 * @lock is a softirq-safe spinlock protecting @queue as well as
148 * @cur_slot, @mrq and @state. These must always be updated
149 * at the same time while holding @lock.
150 *
151 * @lock also protects mode_reg and need_clock_update since these are
152 * used to synchronize mode register updates with the queue
153 * processing.
154 *
155 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
156 * and must always be written at the same time as the slot is added to
157 * @queue.
158 *
159 * @pending_events and @completed_events are accessed using atomic bit
160 * operations, so they don't need any locking.
161 *
162 * None of the fields touched by the interrupt handler need any
163 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
164 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
165 * interrupts must be disabled and @data_status updated with a
166 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300167 * CMDRDY interrupt must be disabled and @cmd_status updated with a
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200168 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
169 * bytes_xfered field of @data must be written. This is ensured by
170 * using barriers.
171 */
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200172struct atmel_mci {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200173 spinlock_t lock;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200174 void __iomem *regs;
175
176 struct scatterlist *sg;
177 unsigned int pio_offset;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200178 unsigned int *buffer;
179 unsigned int buf_size;
180 dma_addr_t buf_phys_addr;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200181
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200182 struct atmel_mci_slot *cur_slot;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200183 struct mmc_request *mrq;
184 struct mmc_command *cmd;
185 struct mmc_data *data;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000186 unsigned int data_size;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200187
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200188 struct atmel_mci_dma dma;
189 struct dma_chan *data_chan;
Viresh Kumare2b35f32012-02-01 16:12:27 +0530190 struct dma_slave_config dma_conf;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200191
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200192 u32 cmd_status;
193 u32 data_status;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200194 u32 stop_cmdr;
195
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200196 struct tasklet_struct tasklet;
197 unsigned long pending_events;
198 unsigned long completed_events;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200199 enum atmel_mci_state state;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200200 struct list_head queue;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200201
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200202 bool need_clock_update;
203 bool need_reset;
Ludovic Desroches24011f32012-05-16 15:26:00 +0200204 struct timer_list timer;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200205 u32 mode_reg;
Nicolas Ferre74791a22009-12-14 18:01:31 -0800206 u32 cfg_reg;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200207 unsigned long bus_hz;
208 unsigned long mapbase;
209 struct clk *mck;
210 struct platform_device *pdev;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200211
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000212 struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
Ludovic Desroches796211b2011-08-11 15:25:44 +0000213
214 struct atmel_mci_caps caps;
215
216 u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
217 void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
218 void (*stop_transfer)(struct atmel_mci *host);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200219};
220
221/**
222 * struct atmel_mci_slot - MMC slot state
223 * @mmc: The mmc_host representing this slot.
224 * @host: The MMC controller this slot is using.
225 * @sdc_reg: Value of SDCR to be written before using this slot.
Anders Grahn88ff82e2010-05-26 14:42:01 -0700226 * @sdio_irq: SDIO irq mask for this slot.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200227 * @mrq: mmc_request currently being processed or waiting to be
228 * processed, or NULL when the slot is idle.
229 * @queue_node: List node for placing this node in the @queue list of
230 * &struct atmel_mci.
231 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
232 * @flags: Random state bits associated with the slot.
233 * @detect_pin: GPIO pin used for card detection, or negative if not
234 * available.
235 * @wp_pin: GPIO pin used for card write protect sending, or negative
236 * if not available.
Jonas Larsson1c1452b2009-03-31 11:16:48 +0200237 * @detect_is_active_high: The state of the detect pin when it is active.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200238 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
239 */
240struct atmel_mci_slot {
241 struct mmc_host *mmc;
242 struct atmel_mci *host;
243
244 u32 sdc_reg;
Anders Grahn88ff82e2010-05-26 14:42:01 -0700245 u32 sdio_irq;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200246
247 struct mmc_request *mrq;
248 struct list_head queue_node;
249
250 unsigned int clock;
251 unsigned long flags;
252#define ATMCI_CARD_PRESENT 0
253#define ATMCI_CARD_NEED_INIT 1
254#define ATMCI_SHUTDOWN 2
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +0200255#define ATMCI_SUSPENDED 3
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200256
257 int detect_pin;
258 int wp_pin;
Jonas Larsson1c1452b2009-03-31 11:16:48 +0200259 bool detect_is_active_high;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200260
261 struct timer_list detect_timer;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200262};
263
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200264#define atmci_test_and_clear_pending(host, event) \
265 test_and_clear_bit(event, &host->pending_events)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200266#define atmci_set_completed(host, event) \
267 set_bit(event, &host->completed_events)
268#define atmci_set_pending(host, event) \
269 set_bit(event, &host->pending_events)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200270
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200271/*
272 * The debugfs stuff below is mostly optimized away when
273 * CONFIG_DEBUG_FS is not set.
274 */
275static int atmci_req_show(struct seq_file *s, void *v)
276{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200277 struct atmel_mci_slot *slot = s->private;
278 struct mmc_request *mrq;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200279 struct mmc_command *cmd;
280 struct mmc_command *stop;
281 struct mmc_data *data;
282
283 /* Make sure we get a consistent snapshot */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200284 spin_lock_bh(&slot->host->lock);
285 mrq = slot->mrq;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200286
287 if (mrq) {
288 cmd = mrq->cmd;
289 data = mrq->data;
290 stop = mrq->stop;
291
292 if (cmd)
293 seq_printf(s,
294 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
295 cmd->opcode, cmd->arg, cmd->flags,
296 cmd->resp[0], cmd->resp[1], cmd->resp[2],
Nicolas Ferred586ebb2010-05-11 14:06:50 -0700297 cmd->resp[3], cmd->error);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200298 if (data)
299 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
300 data->bytes_xfered, data->blocks,
301 data->blksz, data->flags, data->error);
302 if (stop)
303 seq_printf(s,
304 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
305 stop->opcode, stop->arg, stop->flags,
306 stop->resp[0], stop->resp[1], stop->resp[2],
Nicolas Ferred586ebb2010-05-11 14:06:50 -0700307 stop->resp[3], stop->error);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200308 }
309
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200310 spin_unlock_bh(&slot->host->lock);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200311
312 return 0;
313}
314
315static int atmci_req_open(struct inode *inode, struct file *file)
316{
317 return single_open(file, atmci_req_show, inode->i_private);
318}
319
320static const struct file_operations atmci_req_fops = {
321 .owner = THIS_MODULE,
322 .open = atmci_req_open,
323 .read = seq_read,
324 .llseek = seq_lseek,
325 .release = single_release,
326};
327
328static void atmci_show_status_reg(struct seq_file *s,
329 const char *regname, u32 value)
330{
331 static const char *sr_bit[] = {
332 [0] = "CMDRDY",
333 [1] = "RXRDY",
334 [2] = "TXRDY",
335 [3] = "BLKE",
336 [4] = "DTIP",
337 [5] = "NOTBUSY",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700338 [6] = "ENDRX",
339 [7] = "ENDTX",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200340 [8] = "SDIOIRQA",
341 [9] = "SDIOIRQB",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700342 [12] = "SDIOWAIT",
343 [14] = "RXBUFF",
344 [15] = "TXBUFE",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200345 [16] = "RINDE",
346 [17] = "RDIRE",
347 [18] = "RCRCE",
348 [19] = "RENDE",
349 [20] = "RTOE",
350 [21] = "DCRCE",
351 [22] = "DTOE",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700352 [23] = "CSTOE",
353 [24] = "BLKOVRE",
354 [25] = "DMADONE",
355 [26] = "FIFOEMPTY",
356 [27] = "XFRDONE",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200357 [30] = "OVRE",
358 [31] = "UNRE",
359 };
360 unsigned int i;
361
362 seq_printf(s, "%s:\t0x%08x", regname, value);
363 for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
364 if (value & (1 << i)) {
365 if (sr_bit[i])
366 seq_printf(s, " %s", sr_bit[i]);
367 else
368 seq_puts(s, " UNKNOWN");
369 }
370 }
371 seq_putc(s, '\n');
372}
373
374static int atmci_regs_show(struct seq_file *s, void *v)
375{
376 struct atmel_mci *host = s->private;
377 u32 *buf;
378
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000379 buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200380 if (!buf)
381 return -ENOMEM;
382
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200383 /*
384 * Grab a more or less consistent snapshot. Note that we're
385 * not disabling interrupts, so IMR and SR may not be
386 * consistent.
387 */
388 spin_lock_bh(&host->lock);
Haavard Skinnemoen87e60f22008-09-19 21:09:27 +0200389 clk_enable(host->mck);
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000390 memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
Haavard Skinnemoen87e60f22008-09-19 21:09:27 +0200391 clk_disable(host->mck);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200392 spin_unlock_bh(&host->lock);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200393
394 seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000395 buf[ATMCI_MR / 4],
396 buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
397 buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "",
398 buf[ATMCI_MR / 4] & 0xff);
399 seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
400 seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
401 seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200402 seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000403 buf[ATMCI_BLKR / 4],
404 buf[ATMCI_BLKR / 4] & 0xffff,
405 (buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000406 if (host->caps.has_cstor_reg)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000407 seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200408
409 /* Don't read RSPR and RDR; it will consume the data there */
410
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000411 atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
412 atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200413
Ludovic Desroches796211b2011-08-11 15:25:44 +0000414 if (host->caps.has_dma) {
Nicolas Ferre74791a22009-12-14 18:01:31 -0800415 u32 val;
416
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000417 val = buf[ATMCI_DMA / 4];
Nicolas Ferre74791a22009-12-14 18:01:31 -0800418 seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
419 val, val & 3,
420 ((val >> 4) & 3) ?
421 1 << (((val >> 4) & 3) + 1) : 1,
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000422 val & ATMCI_DMAEN ? " DMAEN" : "");
Ludovic Desroches796211b2011-08-11 15:25:44 +0000423 }
424 if (host->caps.has_cfg_reg) {
425 u32 val;
Nicolas Ferre74791a22009-12-14 18:01:31 -0800426
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000427 val = buf[ATMCI_CFG / 4];
Nicolas Ferre74791a22009-12-14 18:01:31 -0800428 seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
429 val,
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000430 val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
431 val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
432 val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
433 val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
Nicolas Ferre74791a22009-12-14 18:01:31 -0800434 }
435
Haavard Skinnemoenb17339a2008-09-19 21:09:28 +0200436 kfree(buf);
437
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200438 return 0;
439}
440
441static int atmci_regs_open(struct inode *inode, struct file *file)
442{
443 return single_open(file, atmci_regs_show, inode->i_private);
444}
445
446static const struct file_operations atmci_regs_fops = {
447 .owner = THIS_MODULE,
448 .open = atmci_regs_open,
449 .read = seq_read,
450 .llseek = seq_lseek,
451 .release = single_release,
452};
453
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200454static void atmci_init_debugfs(struct atmel_mci_slot *slot)
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200455{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200456 struct mmc_host *mmc = slot->mmc;
457 struct atmel_mci *host = slot->host;
458 struct dentry *root;
459 struct dentry *node;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200460
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200461 root = mmc->debugfs_root;
462 if (!root)
463 return;
464
465 node = debugfs_create_file("regs", S_IRUSR, root, host,
466 &atmci_regs_fops);
467 if (IS_ERR(node))
468 return;
469 if (!node)
470 goto err;
471
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200472 node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200473 if (!node)
474 goto err;
475
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200476 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
477 if (!node)
478 goto err;
479
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200480 node = debugfs_create_x32("pending_events", S_IRUSR, root,
481 (u32 *)&host->pending_events);
482 if (!node)
483 goto err;
484
485 node = debugfs_create_x32("completed_events", S_IRUSR, root,
486 (u32 *)&host->completed_events);
487 if (!node)
488 goto err;
489
490 return;
491
492err:
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200493 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200494}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200495
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200496static inline unsigned int atmci_get_version(struct atmel_mci *host)
497{
498 return atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
499}
500
Ludovic Desroches24011f32012-05-16 15:26:00 +0200501static void atmci_timeout_timer(unsigned long data)
502{
503 struct atmel_mci *host;
504
505 host = (struct atmel_mci *)data;
506
507 dev_dbg(&host->pdev->dev, "software timeout\n");
508
509 if (host->mrq->cmd->data) {
510 host->mrq->cmd->data->error = -ETIMEDOUT;
511 host->data = NULL;
512 } else {
513 host->mrq->cmd->error = -ETIMEDOUT;
514 host->cmd = NULL;
515 }
516 host->need_reset = 1;
517 host->state = STATE_END_REQUEST;
518 smp_wmb();
519 tasklet_schedule(&host->tasklet);
520}
521
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000522static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200523 unsigned int ns)
524{
Ludovic Desroches66292ad2012-03-28 12:28:33 +0200525 /*
526 * It is easier here to use us instead of ns for the timeout,
527 * it prevents from overflows during calculation.
528 */
529 unsigned int us = DIV_ROUND_UP(ns, 1000);
530
531 /* Maximum clock frequency is host->bus_hz/2 */
532 return us * (DIV_ROUND_UP(host->bus_hz, 2000000));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200533}
534
535static void atmci_set_timeout(struct atmel_mci *host,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200536 struct atmel_mci_slot *slot, struct mmc_data *data)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200537{
538 static unsigned dtomul_to_shift[] = {
539 0, 4, 7, 8, 10, 12, 16, 20
540 };
541 unsigned timeout;
542 unsigned dtocyc;
543 unsigned dtomul;
544
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000545 timeout = atmci_ns_to_clocks(host, data->timeout_ns)
546 + data->timeout_clks;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200547
548 for (dtomul = 0; dtomul < 8; dtomul++) {
549 unsigned shift = dtomul_to_shift[dtomul];
550 dtocyc = (timeout + (1 << shift) - 1) >> shift;
551 if (dtocyc < 15)
552 break;
553 }
554
555 if (dtomul >= 8) {
556 dtomul = 7;
557 dtocyc = 15;
558 }
559
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200560 dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200561 dtocyc << dtomul_to_shift[dtomul]);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000562 atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200563}
564
565/*
566 * Return mask with command flags to be enabled for this command.
567 */
568static u32 atmci_prepare_command(struct mmc_host *mmc,
569 struct mmc_command *cmd)
570{
571 struct mmc_data *data;
572 u32 cmdr;
573
574 cmd->error = -EINPROGRESS;
575
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000576 cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200577
578 if (cmd->flags & MMC_RSP_PRESENT) {
579 if (cmd->flags & MMC_RSP_136)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000580 cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200581 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000582 cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200583 }
584
585 /*
586 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
587 * it's too difficult to determine whether this is an ACMD or
588 * not. Better make it 64.
589 */
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000590 cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200591
592 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000593 cmdr |= ATMCI_CMDR_OPDCMD;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200594
595 data = cmd->data;
596 if (data) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000597 cmdr |= ATMCI_CMDR_START_XFER;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100598
599 if (cmd->opcode == SD_IO_RW_EXTENDED) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000600 cmdr |= ATMCI_CMDR_SDIO_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100601 } else {
602 if (data->flags & MMC_DATA_STREAM)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000603 cmdr |= ATMCI_CMDR_STREAM;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100604 else if (data->blocks > 1)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000605 cmdr |= ATMCI_CMDR_MULTI_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100606 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000607 cmdr |= ATMCI_CMDR_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100608 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200609
610 if (data->flags & MMC_DATA_READ)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000611 cmdr |= ATMCI_CMDR_TRDIR_READ;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200612 }
613
614 return cmdr;
615}
616
Ludovic Desroches11d14882011-08-11 15:25:45 +0000617static void atmci_send_command(struct atmel_mci *host,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200618 struct mmc_command *cmd, u32 cmd_flags)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200619{
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200620 WARN_ON(host->cmd);
621 host->cmd = cmd;
622
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200623 dev_vdbg(&host->pdev->dev,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200624 "start command: ARGR=0x%08x CMDR=0x%08x\n",
625 cmd->arg, cmd_flags);
626
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000627 atmci_writel(host, ATMCI_ARGR, cmd->arg);
628 atmci_writel(host, ATMCI_CMDR, cmd_flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200629}
630
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000631static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200632{
Ludovic Desroches11d14882011-08-11 15:25:45 +0000633 atmci_send_command(host, data->stop, host->stop_cmdr);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000634 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200635}
636
Ludovic Desroches796211b2011-08-11 15:25:44 +0000637/*
638 * Configure given PDC buffer taking care of alignement issues.
639 * Update host->data_size and host->sg.
640 */
641static void atmci_pdc_set_single_buf(struct atmel_mci *host,
642 enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200643{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000644 u32 pointer_reg, counter_reg;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200645 unsigned int buf_size;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200646
Ludovic Desroches796211b2011-08-11 15:25:44 +0000647 if (dir == XFER_RECEIVE) {
648 pointer_reg = ATMEL_PDC_RPR;
649 counter_reg = ATMEL_PDC_RCR;
650 } else {
651 pointer_reg = ATMEL_PDC_TPR;
652 counter_reg = ATMEL_PDC_TCR;
653 }
654
655 if (buf_nb == PDC_SECOND_BUF) {
Ludovic Desroches1ebbe3d2011-08-11 15:25:46 +0000656 pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
657 counter_reg += ATMEL_PDC_SCND_BUF_OFF;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000658 }
659
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200660 if (!host->caps.has_rwproof) {
661 buf_size = host->buf_size;
662 atmci_writel(host, pointer_reg, host->buf_phys_addr);
663 } else {
664 buf_size = sg_dma_len(host->sg);
665 atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
666 }
667
668 if (host->data_size <= buf_size) {
Ludovic Desroches796211b2011-08-11 15:25:44 +0000669 if (host->data_size & 0x3) {
670 /* If size is different from modulo 4, transfer bytes */
671 atmci_writel(host, counter_reg, host->data_size);
672 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE);
673 } else {
674 /* Else transfer 32-bits words */
675 atmci_writel(host, counter_reg, host->data_size / 4);
676 }
677 host->data_size = 0;
678 } else {
679 /* We assume the size of a page is 32-bits aligned */
Ludovic Desroches341fa4c2011-08-11 15:25:47 +0000680 atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
681 host->data_size -= sg_dma_len(host->sg);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000682 if (host->data_size)
683 host->sg = sg_next(host->sg);
684 }
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200685}
686
Ludovic Desroches796211b2011-08-11 15:25:44 +0000687/*
688 * Configure PDC buffer according to the data size ie configuring one or two
689 * buffers. Don't use this function if you want to configure only the second
690 * buffer. In this case, use atmci_pdc_set_single_buf.
691 */
692static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200693{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000694 atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF);
695 if (host->data_size)
696 atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF);
697}
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200698
Ludovic Desroches796211b2011-08-11 15:25:44 +0000699/*
700 * Unmap sg lists, called when transfer is finished.
701 */
702static void atmci_pdc_cleanup(struct atmel_mci *host)
703{
704 struct mmc_data *data = host->data;
705
706 if (data)
707 dma_unmap_sg(&host->pdev->dev,
708 data->sg, data->sg_len,
709 ((data->flags & MMC_DATA_WRITE)
710 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
711}
712
713/*
714 * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
715 * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
716 * interrupt needed for both transfer directions.
717 */
718static void atmci_pdc_complete(struct atmel_mci *host)
719{
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200720 int transfer_size = host->data->blocks * host->data->blksz;
Ludovic Desroches24011f32012-05-16 15:26:00 +0200721 int i;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200722
Ludovic Desroches796211b2011-08-11 15:25:44 +0000723 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200724
725 if ((!host->caps.has_rwproof)
Ludovic Desroches24011f32012-05-16 15:26:00 +0200726 && (host->data->flags & MMC_DATA_READ)) {
727 if (host->caps.has_bad_data_ordering)
728 for (i = 0; i < transfer_size; i++)
729 host->buffer[i] = swab32(host->buffer[i]);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200730 sg_copy_from_buffer(host->data->sg, host->data->sg_len,
731 host->buffer, transfer_size);
Ludovic Desroches24011f32012-05-16 15:26:00 +0200732 }
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200733
Ludovic Desroches796211b2011-08-11 15:25:44 +0000734 atmci_pdc_cleanup(host);
735
736 /*
737 * If the card was removed, data will be NULL. No point trying
738 * to send the stop command or waiting for NBUSY in this case.
739 */
740 if (host->data) {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200741 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000742 tasklet_schedule(&host->tasklet);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200743 }
744}
745
Ludovic Desroches796211b2011-08-11 15:25:44 +0000746static void atmci_dma_cleanup(struct atmel_mci *host)
747{
748 struct mmc_data *data = host->data;
749
750 if (data)
751 dma_unmap_sg(host->dma.chan->device->dev,
752 data->sg, data->sg_len,
753 ((data->flags & MMC_DATA_WRITE)
754 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
755}
756
757/*
758 * This function is called by the DMA driver from tasklet context.
759 */
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200760static void atmci_dma_complete(void *arg)
761{
762 struct atmel_mci *host = arg;
763 struct mmc_data *data = host->data;
764
765 dev_vdbg(&host->pdev->dev, "DMA complete\n");
766
Ludovic Desroches796211b2011-08-11 15:25:44 +0000767 if (host->caps.has_dma)
Nicolas Ferre74791a22009-12-14 18:01:31 -0800768 /* Disable DMA hardware handshaking on MCI */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000769 atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
Nicolas Ferre74791a22009-12-14 18:01:31 -0800770
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200771 atmci_dma_cleanup(host);
772
773 /*
774 * If the card was removed, data will be NULL. No point trying
775 * to send the stop command or waiting for NBUSY in this case.
776 */
777 if (data) {
778 atmci_set_pending(host, EVENT_XFER_COMPLETE);
779 tasklet_schedule(&host->tasklet);
780
781 /*
782 * Regardless of what the documentation says, we have
783 * to wait for NOTBUSY even after block read
784 * operations.
785 *
786 * When the DMA transfer is complete, the controller
787 * may still be reading the CRC from the card, i.e.
788 * the data transfer is still in progress and we
789 * haven't seen all the potential error bits yet.
790 *
791 * The interrupt handler will schedule a different
792 * tasklet to finish things up when the data transfer
793 * is completely done.
794 *
795 * We may not complete the mmc request here anyway
796 * because the mmc layer may call back and cause us to
797 * violate the "don't submit new operations from the
798 * completion callback" rule of the dma engine
799 * framework.
800 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000801 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200802 }
803}
804
Ludovic Desroches796211b2011-08-11 15:25:44 +0000805/*
806 * Returns a mask of interrupt flags to be enabled after the whole
807 * request has been prepared.
808 */
809static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
810{
811 u32 iflags;
812
813 data->error = -EINPROGRESS;
814
815 host->sg = data->sg;
816 host->data = data;
817 host->data_chan = NULL;
818
819 iflags = ATMCI_DATA_ERROR_FLAGS;
820
821 /*
822 * Errata: MMC data write operation with less than 12
823 * bytes is impossible.
824 *
825 * Errata: MCI Transmit Data Register (TDR) FIFO
826 * corruption when length is not multiple of 4.
827 */
828 if (data->blocks * data->blksz < 12
829 || (data->blocks * data->blksz) & 3)
830 host->need_reset = true;
831
832 host->pio_offset = 0;
833 if (data->flags & MMC_DATA_READ)
834 iflags |= ATMCI_RXRDY;
835 else
836 iflags |= ATMCI_TXRDY;
837
838 return iflags;
839}
840
841/*
842 * Set interrupt flags and set block length into the MCI mode register even
843 * if this value is also accessible in the MCI block register. It seems to be
844 * necessary before the High Speed MCI version. It also map sg and configure
845 * PDC registers.
846 */
847static u32
848atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
849{
850 u32 iflags, tmp;
851 unsigned int sg_len;
852 enum dma_data_direction dir;
Ludovic Desroches24011f32012-05-16 15:26:00 +0200853 int i;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000854
855 data->error = -EINPROGRESS;
856
857 host->data = data;
858 host->sg = data->sg;
859 iflags = ATMCI_DATA_ERROR_FLAGS;
860
861 /* Enable pdc mode */
862 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE);
863
864 if (data->flags & MMC_DATA_READ) {
865 dir = DMA_FROM_DEVICE;
866 iflags |= ATMCI_ENDRX | ATMCI_RXBUFF;
867 } else {
868 dir = DMA_TO_DEVICE;
Ludovic Desrochesf5177542012-05-16 15:25:59 +0200869 iflags |= ATMCI_ENDTX | ATMCI_TXBUFE | ATMCI_BLKE;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000870 }
871
872 /* Set BLKLEN */
873 tmp = atmci_readl(host, ATMCI_MR);
874 tmp &= 0x0000ffff;
875 tmp |= ATMCI_BLKLEN(data->blksz);
876 atmci_writel(host, ATMCI_MR, tmp);
877
878 /* Configure PDC */
879 host->data_size = data->blocks * data->blksz;
880 sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200881
882 if ((!host->caps.has_rwproof)
Ludovic Desroches24011f32012-05-16 15:26:00 +0200883 && (host->data->flags & MMC_DATA_WRITE)) {
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200884 sg_copy_to_buffer(host->data->sg, host->data->sg_len,
885 host->buffer, host->data_size);
Ludovic Desroches24011f32012-05-16 15:26:00 +0200886 if (host->caps.has_bad_data_ordering)
887 for (i = 0; i < host->data_size; i++)
888 host->buffer[i] = swab32(host->buffer[i]);
889 }
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200890
Ludovic Desroches796211b2011-08-11 15:25:44 +0000891 if (host->data_size)
892 atmci_pdc_set_both_buf(host,
893 ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));
894
895 return iflags;
896}
897
898static u32
Nicolas Ferre74791a22009-12-14 18:01:31 -0800899atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200900{
901 struct dma_chan *chan;
902 struct dma_async_tx_descriptor *desc;
903 struct scatterlist *sg;
904 unsigned int i;
905 enum dma_data_direction direction;
Vinod Koule0d23ef2011-11-17 14:54:38 +0530906 enum dma_transfer_direction slave_dirn;
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700907 unsigned int sglen;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000908 u32 iflags;
909
910 data->error = -EINPROGRESS;
911
912 WARN_ON(host->data);
913 host->sg = NULL;
914 host->data = data;
915
916 iflags = ATMCI_DATA_ERROR_FLAGS;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200917
918 /*
919 * We don't do DMA on "complex" transfers, i.e. with
920 * non-word-aligned buffers or lengths. Also, we don't bother
921 * with all the DMA setup overhead for short transfers.
922 */
Ludovic Desroches796211b2011-08-11 15:25:44 +0000923 if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
924 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200925 if (data->blksz & 3)
Ludovic Desroches796211b2011-08-11 15:25:44 +0000926 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200927
928 for_each_sg(data->sg, sg, data->sg_len, i) {
929 if (sg->offset & 3 || sg->length & 3)
Ludovic Desroches796211b2011-08-11 15:25:44 +0000930 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200931 }
932
933 /* If we don't have a channel, we can't do DMA */
934 chan = host->dma.chan;
Dan Williams6f49a572009-01-06 11:38:14 -0700935 if (chan)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200936 host->data_chan = chan;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200937
938 if (!chan)
939 return -ENODEV;
940
Ludovic Desroches796211b2011-08-11 15:25:44 +0000941 if (host->caps.has_dma)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000942 atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(3) | ATMCI_DMAEN);
Nicolas Ferre74791a22009-12-14 18:01:31 -0800943
Vinod Koule0d23ef2011-11-17 14:54:38 +0530944 if (data->flags & MMC_DATA_READ) {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200945 direction = DMA_FROM_DEVICE;
Viresh Kumare2b35f32012-02-01 16:12:27 +0530946 host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM;
Vinod Koule0d23ef2011-11-17 14:54:38 +0530947 } else {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200948 direction = DMA_TO_DEVICE;
Viresh Kumare2b35f32012-02-01 16:12:27 +0530949 host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV;
Vinod Koule0d23ef2011-11-17 14:54:38 +0530950 }
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200951
Linus Walleij266ac3f2011-02-10 16:08:06 +0100952 sglen = dma_map_sg(chan->device->dev, data->sg,
Ludovic Desroches796211b2011-08-11 15:25:44 +0000953 data->sg_len, direction);
Linus Walleij88ce4db2011-02-10 16:08:16 +0100954
Viresh Kumare2b35f32012-02-01 16:12:27 +0530955 dmaengine_slave_config(chan, &host->dma_conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500956 desc = dmaengine_prep_slave_sg(chan,
Vinod Koule0d23ef2011-11-17 14:54:38 +0530957 data->sg, sglen, slave_dirn,
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200958 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
959 if (!desc)
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700960 goto unmap_exit;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200961
962 host->dma.data_desc = desc;
963 desc->callback = atmci_dma_complete;
964 desc->callback_param = host;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200965
Ludovic Desroches796211b2011-08-11 15:25:44 +0000966 return iflags;
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700967unmap_exit:
Linus Walleij88ce4db2011-02-10 16:08:16 +0100968 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700969 return -ENOMEM;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200970}
971
Ludovic Desroches796211b2011-08-11 15:25:44 +0000972static void
973atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
974{
975 return;
976}
977
978/*
979 * Start PDC according to transfer direction.
980 */
981static void
982atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data)
983{
984 if (data->flags & MMC_DATA_READ)
985 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
986 else
987 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
988}
989
990static void
991atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
Nicolas Ferre74791a22009-12-14 18:01:31 -0800992{
993 struct dma_chan *chan = host->data_chan;
994 struct dma_async_tx_descriptor *desc = host->dma.data_desc;
995
996 if (chan) {
Linus Walleij53289062011-02-10 16:08:26 +0100997 dmaengine_submit(desc);
998 dma_async_issue_pending(chan);
Nicolas Ferre74791a22009-12-14 18:01:31 -0800999 }
1000}
1001
Ludovic Desroches796211b2011-08-11 15:25:44 +00001002static void atmci_stop_transfer(struct atmel_mci *host)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001003{
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001004 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001005 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001006}
1007
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001008/*
Ludovic Desroches796211b2011-08-11 15:25:44 +00001009 * Stop data transfer because error(s) occured.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001010 */
Ludovic Desroches796211b2011-08-11 15:25:44 +00001011static void atmci_stop_transfer_pdc(struct atmel_mci *host)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001012{
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001013 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001014}
1015
Ludovic Desroches796211b2011-08-11 15:25:44 +00001016static void atmci_stop_transfer_dma(struct atmel_mci *host)
1017{
1018 struct dma_chan *chan = host->data_chan;
1019
1020 if (chan) {
1021 dmaengine_terminate_all(chan);
1022 atmci_dma_cleanup(host);
1023 } else {
1024 /* Data transfer was stopped by the interrupt handler */
1025 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1026 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1027 }
1028}
1029
1030/*
1031 * Start a request: prepare data if needed, prepare the command and activate
1032 * interrupts.
1033 */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001034static void atmci_start_request(struct atmel_mci *host,
1035 struct atmel_mci_slot *slot)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001036{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001037 struct mmc_request *mrq;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001038 struct mmc_command *cmd;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001039 struct mmc_data *data;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001040 u32 iflags;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001041 u32 cmdflags;
1042
1043 mrq = slot->mrq;
1044 host->cur_slot = slot;
1045 host->mrq = mrq;
1046
1047 host->pending_events = 0;
1048 host->completed_events = 0;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001049 host->cmd_status = 0;
Haavard Skinnemoenca55f462008-10-05 15:16:59 +02001050 host->data_status = 0;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001051
Ludovic Desroches24011f32012-05-16 15:26:00 +02001052 if (host->need_reset || host->caps.need_reset_after_xfer) {
Ludovic Desroches18ee6842012-02-09 11:55:29 +01001053 iflags = atmci_readl(host, ATMCI_IMR);
1054 iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001055 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1056 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1057 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001058 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001059 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Ludovic Desroches18ee6842012-02-09 11:55:29 +01001060 atmci_writel(host, ATMCI_IER, iflags);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001061 host->need_reset = false;
1062 }
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001063 atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001064
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001065 iflags = atmci_readl(host, ATMCI_IMR);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001066 if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001067 dev_dbg(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001068 iflags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001069
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001070 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
1071 /* Send init sequence (74 clock cycles) */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001072 atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
1073 while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001074 cpu_relax();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001075 }
Nicolas Ferre74791a22009-12-14 18:01:31 -08001076 iflags = 0;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001077 data = mrq->data;
1078 if (data) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001079 atmci_set_timeout(host, slot, data);
Haavard Skinnemoena252e3e2008-10-03 14:46:17 +02001080
1081 /* Must set block count/size before sending command */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001082 atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001083 | ATMCI_BLKLEN(data->blksz));
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001084 dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001085 ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
Nicolas Ferre74791a22009-12-14 18:01:31 -08001086
Ludovic Desroches796211b2011-08-11 15:25:44 +00001087 iflags |= host->prepare_data(host, data);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001088 }
1089
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001090 iflags |= ATMCI_CMDRDY;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001091 cmd = mrq->cmd;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001092 cmdflags = atmci_prepare_command(slot->mmc, cmd);
Ludovic Desroches11d14882011-08-11 15:25:45 +00001093 atmci_send_command(host, cmd, cmdflags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001094
1095 if (data)
Ludovic Desroches796211b2011-08-11 15:25:44 +00001096 host->submit_data(host, data);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001097
1098 if (mrq->stop) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001099 host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001100 host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001101 if (!(data->flags & MMC_DATA_WRITE))
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001102 host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001103 if (data->flags & MMC_DATA_STREAM)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001104 host->stop_cmdr |= ATMCI_CMDR_STREAM;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001105 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001106 host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001107 }
1108
1109 /*
1110 * We could have enabled interrupts earlier, but I suspect
1111 * that would open up a nice can of interesting race
1112 * conditions (e.g. command and data complete, but stop not
1113 * prepared yet.)
1114 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001115 atmci_writel(host, ATMCI_IER, iflags);
Ludovic Desroches24011f32012-05-16 15:26:00 +02001116
1117 mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000));
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001118}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001119
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001120static void atmci_queue_request(struct atmel_mci *host,
1121 struct atmel_mci_slot *slot, struct mmc_request *mrq)
1122{
1123 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1124 host->state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001125
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001126 spin_lock_bh(&host->lock);
1127 slot->mrq = mrq;
1128 if (host->state == STATE_IDLE) {
1129 host->state = STATE_SENDING_CMD;
1130 atmci_start_request(host, slot);
1131 } else {
1132 list_add_tail(&slot->queue_node, &host->queue);
1133 }
1134 spin_unlock_bh(&host->lock);
1135}
1136
1137static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1138{
1139 struct atmel_mci_slot *slot = mmc_priv(mmc);
1140 struct atmel_mci *host = slot->host;
1141 struct mmc_data *data;
1142
1143 WARN_ON(slot->mrq);
1144
1145 /*
1146 * We may "know" the card is gone even though there's still an
1147 * electrical connection. If so, we really need to communicate
1148 * this to the MMC core since there won't be any more
1149 * interrupts as the card is completely removed. Otherwise,
1150 * the MMC core might believe the card is still there even
1151 * though the card was just removed very slowly.
1152 */
1153 if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
1154 mrq->cmd->error = -ENOMEDIUM;
1155 mmc_request_done(mmc, mrq);
1156 return;
1157 }
1158
1159 /* We don't support multiple blocks of weird lengths. */
1160 data = mrq->data;
1161 if (data && data->blocks > 1 && data->blksz & 3) {
1162 mrq->cmd->error = -EINVAL;
1163 mmc_request_done(mmc, mrq);
1164 }
1165
1166 atmci_queue_request(host, slot, mrq);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001167}
1168
1169static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1170{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001171 struct atmel_mci_slot *slot = mmc_priv(mmc);
1172 struct atmel_mci *host = slot->host;
1173 unsigned int i;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001174
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001175 slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001176 switch (ios->bus_width) {
1177 case MMC_BUS_WIDTH_1:
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001178 slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001179 break;
1180 case MMC_BUS_WIDTH_4:
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001181 slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001182 break;
1183 }
1184
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001185 if (ios->clock) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001186 unsigned int clock_min = ~0U;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001187 u32 clkdiv;
1188
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001189 spin_lock_bh(&host->lock);
1190 if (!host->mode_reg) {
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001191 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001192 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1193 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001194 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001195 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001196 }
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001197
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001198 /*
1199 * Use mirror of ios->clock to prevent race with mmc
1200 * core ios update when finding the minimum.
1201 */
1202 slot->clock = ios->clock;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001203 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001204 if (host->slot[i] && host->slot[i]->clock
1205 && host->slot[i]->clock < clock_min)
1206 clock_min = host->slot[i]->clock;
1207 }
1208
1209 /* Calculate clock divider */
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01001210 if (host->caps.has_odd_clk_div) {
1211 clkdiv = DIV_ROUND_UP(host->bus_hz, clock_min) - 2;
1212 if (clkdiv > 511) {
1213 dev_warn(&mmc->class_dev,
1214 "clock %u too slow; using %lu\n",
1215 clock_min, host->bus_hz / (511 + 2));
1216 clkdiv = 511;
1217 }
1218 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv >> 1)
1219 | ATMCI_MR_CLKODD(clkdiv & 1);
1220 } else {
1221 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
1222 if (clkdiv > 255) {
1223 dev_warn(&mmc->class_dev,
1224 "clock %u too slow; using %lu\n",
1225 clock_min, host->bus_hz / (2 * 256));
1226 clkdiv = 255;
1227 }
1228 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001229 }
1230
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001231 /*
1232 * WRPROOF and RDPROOF prevent overruns/underruns by
1233 * stopping the clock when the FIFO is full/empty.
1234 * This state is not expected to last for long.
1235 */
Ludovic Desroches796211b2011-08-11 15:25:44 +00001236 if (host->caps.has_rwproof)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001237 host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001238
Ludovic Desroches796211b2011-08-11 15:25:44 +00001239 if (host->caps.has_cfg_reg) {
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001240 /* setup High Speed mode in relation with card capacity */
1241 if (ios->timing == MMC_TIMING_SD_HS)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001242 host->cfg_reg |= ATMCI_CFG_HSMODE;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001243 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001244 host->cfg_reg &= ~ATMCI_CFG_HSMODE;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001245 }
1246
1247 if (list_empty(&host->queue)) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001248 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001249 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001250 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001251 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001252 host->need_clock_update = true;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001253 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001254
1255 spin_unlock_bh(&host->lock);
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001256 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001257 bool any_slot_active = false;
1258
1259 spin_lock_bh(&host->lock);
1260 slot->clock = 0;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001261 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001262 if (host->slot[i] && host->slot[i]->clock) {
1263 any_slot_active = true;
1264 break;
1265 }
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001266 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001267 if (!any_slot_active) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001268 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001269 if (host->mode_reg) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001270 atmci_readl(host, ATMCI_MR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001271 clk_disable(host->mck);
1272 }
1273 host->mode_reg = 0;
1274 }
1275 spin_unlock_bh(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001276 }
1277
1278 switch (ios->power_mode) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001279 case MMC_POWER_UP:
1280 set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
1281 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001282 default:
1283 /*
1284 * TODO: None of the currently available AVR32-based
1285 * boards allow MMC power to be turned off. Implement
1286 * power control when this can be tested properly.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001287 *
1288 * We also need to hook this into the clock management
1289 * somehow so that newly inserted cards aren't
1290 * subjected to a fast clock before we have a chance
1291 * to figure out what the maximum rate is. Currently,
1292 * there's no way to avoid this, and there never will
1293 * be for boards that don't support power control.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001294 */
1295 break;
1296 }
1297}
1298
1299static int atmci_get_ro(struct mmc_host *mmc)
1300{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001301 int read_only = -ENOSYS;
1302 struct atmel_mci_slot *slot = mmc_priv(mmc);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001303
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001304 if (gpio_is_valid(slot->wp_pin)) {
1305 read_only = gpio_get_value(slot->wp_pin);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001306 dev_dbg(&mmc->class_dev, "card is %s\n",
1307 read_only ? "read-only" : "read-write");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001308 }
1309
1310 return read_only;
1311}
1312
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001313static int atmci_get_cd(struct mmc_host *mmc)
1314{
1315 int present = -ENOSYS;
1316 struct atmel_mci_slot *slot = mmc_priv(mmc);
1317
1318 if (gpio_is_valid(slot->detect_pin)) {
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001319 present = !(gpio_get_value(slot->detect_pin) ^
1320 slot->detect_is_active_high);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001321 dev_dbg(&mmc->class_dev, "card is %spresent\n",
1322 present ? "" : "not ");
1323 }
1324
1325 return present;
1326}
1327
Anders Grahn88ff82e2010-05-26 14:42:01 -07001328static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1329{
1330 struct atmel_mci_slot *slot = mmc_priv(mmc);
1331 struct atmel_mci *host = slot->host;
1332
1333 if (enable)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001334 atmci_writel(host, ATMCI_IER, slot->sdio_irq);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001335 else
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001336 atmci_writel(host, ATMCI_IDR, slot->sdio_irq);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001337}
1338
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001339static const struct mmc_host_ops atmci_ops = {
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001340 .request = atmci_request,
1341 .set_ios = atmci_set_ios,
1342 .get_ro = atmci_get_ro,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001343 .get_cd = atmci_get_cd,
Anders Grahn88ff82e2010-05-26 14:42:01 -07001344 .enable_sdio_irq = atmci_enable_sdio_irq,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001345};
1346
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001347/* Called with host->lock held */
1348static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
1349 __releases(&host->lock)
1350 __acquires(&host->lock)
1351{
1352 struct atmel_mci_slot *slot = NULL;
1353 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1354
1355 WARN_ON(host->cmd || host->data);
1356
1357 /*
1358 * Update the MMC clock rate if necessary. This may be
1359 * necessary if set_ios() is called when a different slot is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001360 * busy transferring data.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001361 */
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001362 if (host->need_clock_update) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001363 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001364 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001365 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001366 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001367
1368 host->cur_slot->mrq = NULL;
1369 host->mrq = NULL;
1370 if (!list_empty(&host->queue)) {
1371 slot = list_entry(host->queue.next,
1372 struct atmel_mci_slot, queue_node);
1373 list_del(&slot->queue_node);
1374 dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
1375 mmc_hostname(slot->mmc));
1376 host->state = STATE_SENDING_CMD;
1377 atmci_start_request(host, slot);
1378 } else {
1379 dev_vdbg(&host->pdev->dev, "list empty\n");
1380 host->state = STATE_IDLE;
1381 }
1382
Ludovic Desroches24011f32012-05-16 15:26:00 +02001383 del_timer(&host->timer);
1384
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001385 spin_unlock(&host->lock);
1386 mmc_request_done(prev_mmc, mrq);
1387 spin_lock(&host->lock);
1388}
1389
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001390static void atmci_command_complete(struct atmel_mci *host,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001391 struct mmc_command *cmd)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001392{
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001393 u32 status = host->cmd_status;
1394
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001395 /* Read the response from the card (up to 16 bytes) */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001396 cmd->resp[0] = atmci_readl(host, ATMCI_RSPR);
1397 cmd->resp[1] = atmci_readl(host, ATMCI_RSPR);
1398 cmd->resp[2] = atmci_readl(host, ATMCI_RSPR);
1399 cmd->resp[3] = atmci_readl(host, ATMCI_RSPR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001400
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001401 if (status & ATMCI_RTOE)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001402 cmd->error = -ETIMEDOUT;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001403 else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001404 cmd->error = -EILSEQ;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001405 else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001406 cmd->error = -EIO;
Ludovic Desroches24011f32012-05-16 15:26:00 +02001407 else if (host->mrq->data && (host->mrq->data->blksz & 3)) {
1408 if (host->caps.need_blksz_mul_4) {
1409 cmd->error = -EINVAL;
1410 host->need_reset = 1;
1411 }
1412 } else
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001413 cmd->error = 0;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001414}
1415
1416static void atmci_detect_change(unsigned long data)
1417{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001418 struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
1419 bool present;
1420 bool present_old;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001421
1422 /*
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001423 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
1424 * freeing the interrupt. We must not re-enable the interrupt
1425 * if it has been freed, and if we're shutting down, it
1426 * doesn't really matter whether the card is present or not.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001427 */
1428 smp_rmb();
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001429 if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001430 return;
1431
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001432 enable_irq(gpio_to_irq(slot->detect_pin));
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001433 present = !(gpio_get_value(slot->detect_pin) ^
1434 slot->detect_is_active_high);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001435 present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001436
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001437 dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
1438 present, present_old);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001439
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001440 if (present != present_old) {
1441 struct atmel_mci *host = slot->host;
1442 struct mmc_request *mrq;
1443
1444 dev_dbg(&slot->mmc->class_dev, "card %s\n",
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001445 present ? "inserted" : "removed");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001446
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001447 spin_lock(&host->lock);
1448
1449 if (!present)
1450 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1451 else
1452 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001453
1454 /* Clean up queue if present */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001455 mrq = slot->mrq;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001456 if (mrq) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001457 if (mrq == host->mrq) {
1458 /*
1459 * Reset controller to terminate any ongoing
1460 * commands or data transfers.
1461 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001462 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1463 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1464 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001465 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001466 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001467
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001468 host->data = NULL;
1469 host->cmd = NULL;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001470
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001471 switch (host->state) {
1472 case STATE_IDLE:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001473 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001474 case STATE_SENDING_CMD:
1475 mrq->cmd->error = -ENOMEDIUM;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001476 if (mrq->data)
1477 host->stop_transfer(host);
1478 break;
1479 case STATE_DATA_XFER:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001480 mrq->data->error = -ENOMEDIUM;
Ludovic Desroches796211b2011-08-11 15:25:44 +00001481 host->stop_transfer(host);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001482 break;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001483 case STATE_WAITING_NOTBUSY:
1484 mrq->data->error = -ENOMEDIUM;
1485 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001486 case STATE_SENDING_STOP:
1487 mrq->stop->error = -ENOMEDIUM;
1488 break;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001489 case STATE_END_REQUEST:
1490 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001491 }
1492
1493 atmci_request_end(host, mrq);
1494 } else {
1495 list_del(&slot->queue_node);
1496 mrq->cmd->error = -ENOMEDIUM;
1497 if (mrq->data)
1498 mrq->data->error = -ENOMEDIUM;
1499 if (mrq->stop)
1500 mrq->stop->error = -ENOMEDIUM;
1501
1502 spin_unlock(&host->lock);
1503 mmc_request_done(slot->mmc, mrq);
1504 spin_lock(&host->lock);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001505 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001506 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001507 spin_unlock(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001508
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001509 mmc_detect_change(slot->mmc, 0);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001510 }
1511}
1512
1513static void atmci_tasklet_func(unsigned long priv)
1514{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001515 struct atmel_mci *host = (struct atmel_mci *)priv;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001516 struct mmc_request *mrq = host->mrq;
1517 struct mmc_data *data = host->data;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001518 enum atmel_mci_state state = host->state;
1519 enum atmel_mci_state prev_state;
1520 u32 status;
1521
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001522 spin_lock(&host->lock);
1523
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001524 state = host->state;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001525
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001526 dev_vdbg(&host->pdev->dev,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001527 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1528 state, host->pending_events, host->completed_events,
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001529 atmci_readl(host, ATMCI_IMR));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001530
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001531 do {
1532 prev_state = state;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001533
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001534 switch (state) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001535 case STATE_IDLE:
1536 break;
1537
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001538 case STATE_SENDING_CMD:
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001539 /*
1540 * Command has been sent, we are waiting for command
1541 * ready. Then we have three next states possible:
1542 * END_REQUEST by default, WAITING_NOTBUSY if it's a
1543 * command needing it or DATA_XFER if there is data.
1544 */
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001545 if (!atmci_test_and_clear_pending(host,
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001546 EVENT_CMD_RDY))
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001547 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001548
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001549 host->cmd = NULL;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001550 atmci_set_completed(host, EVENT_CMD_RDY);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001551 atmci_command_complete(host, mrq->cmd);
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001552 if (mrq->data) {
1553 /*
1554 * If there is a command error don't start
1555 * data transfer.
1556 */
1557 if (mrq->cmd->error) {
1558 host->stop_transfer(host);
1559 host->data = NULL;
1560 atmci_writel(host, ATMCI_IDR,
1561 ATMCI_TXRDY | ATMCI_RXRDY
1562 | ATMCI_DATA_ERROR_FLAGS);
1563 state = STATE_END_REQUEST;
1564 } else
1565 state = STATE_DATA_XFER;
1566 } else if ((!mrq->data) && (mrq->cmd->flags & MMC_RSP_BUSY)) {
1567 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1568 state = STATE_WAITING_NOTBUSY;
1569 } else
1570 state = STATE_END_REQUEST;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001571
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001572 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001573
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001574 case STATE_DATA_XFER:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001575 if (atmci_test_and_clear_pending(host,
1576 EVENT_DATA_ERROR)) {
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001577 atmci_set_completed(host, EVENT_DATA_ERROR);
1578 state = STATE_END_REQUEST;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001579 break;
1580 }
1581
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001582 /*
1583 * A data transfer is in progress. The event expected
1584 * to move to the next state depends of data transfer
1585 * type (PDC or DMA). Once transfer done we can move
1586 * to the next step which is WAITING_NOTBUSY in write
1587 * case and directly SENDING_STOP in read case.
1588 */
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001589 if (!atmci_test_and_clear_pending(host,
1590 EVENT_XFER_COMPLETE))
1591 break;
1592
1593 atmci_set_completed(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001594
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001595 if (host->data->flags & MMC_DATA_WRITE) {
1596 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1597 state = STATE_WAITING_NOTBUSY;
1598 } else if (host->mrq->stop) {
1599 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
1600 atmci_send_stop_cmd(host, data);
1601 state = STATE_SENDING_STOP;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001602 } else {
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001603 host->data = NULL;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001604 data->bytes_xfered = data->blocks * data->blksz;
1605 data->error = 0;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001606 state = STATE_END_REQUEST;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001607 }
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001608 break;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001609
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001610 case STATE_WAITING_NOTBUSY:
1611 /*
1612 * We can be in the state for two reasons: a command
1613 * requiring waiting not busy signal (stop command
1614 * included) or a write operation. In the latest case,
1615 * we need to send a stop command.
1616 */
1617 if (!atmci_test_and_clear_pending(host,
1618 EVENT_NOTBUSY))
1619 break;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001620
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001621 atmci_set_completed(host, EVENT_NOTBUSY);
1622
1623 if (host->data) {
1624 /*
1625 * For some commands such as CMD53, even if
1626 * there is data transfer, there is no stop
1627 * command to send.
1628 */
1629 if (host->mrq->stop) {
1630 atmci_writel(host, ATMCI_IER,
1631 ATMCI_CMDRDY);
1632 atmci_send_stop_cmd(host, data);
1633 state = STATE_SENDING_STOP;
1634 } else {
1635 host->data = NULL;
1636 data->bytes_xfered = data->blocks
1637 * data->blksz;
1638 data->error = 0;
1639 state = STATE_END_REQUEST;
1640 }
1641 } else
1642 state = STATE_END_REQUEST;
1643 break;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001644
1645 case STATE_SENDING_STOP:
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001646 /*
1647 * In this state, it is important to set host->data to
1648 * NULL (which is tested in the waiting notbusy state)
1649 * in order to go to the end request state instead of
1650 * sending stop again.
1651 */
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001652 if (!atmci_test_and_clear_pending(host,
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001653 EVENT_CMD_RDY))
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001654 break;
1655
1656 host->cmd = NULL;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001657 host->data = NULL;
1658 data->bytes_xfered = data->blocks * data->blksz;
1659 data->error = 0;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001660 atmci_command_complete(host, mrq->stop);
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001661 if (mrq->stop->error) {
1662 host->stop_transfer(host);
1663 atmci_writel(host, ATMCI_IDR,
1664 ATMCI_TXRDY | ATMCI_RXRDY
1665 | ATMCI_DATA_ERROR_FLAGS);
1666 state = STATE_END_REQUEST;
1667 } else {
1668 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1669 state = STATE_WAITING_NOTBUSY;
1670 }
1671 break;
1672
1673 case STATE_END_REQUEST:
1674 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY | ATMCI_RXRDY
1675 | ATMCI_DATA_ERROR_FLAGS);
1676 status = host->data_status;
1677 if (unlikely(status)) {
1678 host->stop_transfer(host);
1679 host->data = NULL;
1680 if (status & ATMCI_DTOE) {
1681 data->error = -ETIMEDOUT;
1682 } else if (status & ATMCI_DCRCE) {
1683 data->error = -EILSEQ;
1684 } else {
1685 data->error = -EIO;
1686 }
1687 }
1688
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001689 atmci_request_end(host, host->mrq);
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001690 state = STATE_IDLE;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001691 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001692 }
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001693 } while (state != prev_state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001694
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001695 host->state = state;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001696
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001697 spin_unlock(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001698}
1699
1700static void atmci_read_data_pio(struct atmel_mci *host)
1701{
1702 struct scatterlist *sg = host->sg;
1703 void *buf = sg_virt(sg);
1704 unsigned int offset = host->pio_offset;
1705 struct mmc_data *data = host->data;
1706 u32 value;
1707 u32 status;
1708 unsigned int nbytes = 0;
1709
1710 do {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001711 value = atmci_readl(host, ATMCI_RDR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001712 if (likely(offset + 4 <= sg->length)) {
1713 put_unaligned(value, (u32 *)(buf + offset));
1714
1715 offset += 4;
1716 nbytes += 4;
1717
1718 if (offset == sg->length) {
Haavard Skinnemoen5e7184a2008-10-05 15:27:50 +02001719 flush_dcache_page(sg_page(sg));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001720 host->sg = sg = sg_next(sg);
1721 if (!sg)
1722 goto done;
1723
1724 offset = 0;
1725 buf = sg_virt(sg);
1726 }
1727 } else {
1728 unsigned int remaining = sg->length - offset;
1729 memcpy(buf + offset, &value, remaining);
1730 nbytes += remaining;
1731
1732 flush_dcache_page(sg_page(sg));
1733 host->sg = sg = sg_next(sg);
1734 if (!sg)
1735 goto done;
1736
1737 offset = 4 - remaining;
1738 buf = sg_virt(sg);
1739 memcpy(buf, (u8 *)&value + remaining, offset);
1740 nbytes += offset;
1741 }
1742
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001743 status = atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001744 if (status & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001745 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001746 | ATMCI_DATA_ERROR_FLAGS));
1747 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001748 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001749 return;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001750 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001751 } while (status & ATMCI_RXRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001752
1753 host->pio_offset = offset;
1754 data->bytes_xfered += nbytes;
1755
1756 return;
1757
1758done:
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001759 atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
1760 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001761 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001762 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001763 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001764}
1765
1766static void atmci_write_data_pio(struct atmel_mci *host)
1767{
1768 struct scatterlist *sg = host->sg;
1769 void *buf = sg_virt(sg);
1770 unsigned int offset = host->pio_offset;
1771 struct mmc_data *data = host->data;
1772 u32 value;
1773 u32 status;
1774 unsigned int nbytes = 0;
1775
1776 do {
1777 if (likely(offset + 4 <= sg->length)) {
1778 value = get_unaligned((u32 *)(buf + offset));
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001779 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001780
1781 offset += 4;
1782 nbytes += 4;
1783 if (offset == sg->length) {
1784 host->sg = sg = sg_next(sg);
1785 if (!sg)
1786 goto done;
1787
1788 offset = 0;
1789 buf = sg_virt(sg);
1790 }
1791 } else {
1792 unsigned int remaining = sg->length - offset;
1793
1794 value = 0;
1795 memcpy(&value, buf + offset, remaining);
1796 nbytes += remaining;
1797
1798 host->sg = sg = sg_next(sg);
1799 if (!sg) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001800 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001801 goto done;
1802 }
1803
1804 offset = 4 - remaining;
1805 buf = sg_virt(sg);
1806 memcpy((u8 *)&value + remaining, buf, offset);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001807 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001808 nbytes += offset;
1809 }
1810
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001811 status = atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001812 if (status & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001813 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001814 | ATMCI_DATA_ERROR_FLAGS));
1815 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001816 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001817 return;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001818 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001819 } while (status & ATMCI_TXRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001820
1821 host->pio_offset = offset;
1822 data->bytes_xfered += nbytes;
1823
1824 return;
1825
1826done:
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001827 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
1828 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001829 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001830 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001831 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001832}
1833
Anders Grahn88ff82e2010-05-26 14:42:01 -07001834static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
1835{
1836 int i;
1837
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001838 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Anders Grahn88ff82e2010-05-26 14:42:01 -07001839 struct atmel_mci_slot *slot = host->slot[i];
1840 if (slot && (status & slot->sdio_irq)) {
1841 mmc_signal_sdio_irq(slot->mmc);
1842 }
1843 }
1844}
1845
1846
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001847static irqreturn_t atmci_interrupt(int irq, void *dev_id)
1848{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001849 struct atmel_mci *host = dev_id;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001850 u32 status, mask, pending;
1851 unsigned int pass_count = 0;
1852
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001853 do {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001854 status = atmci_readl(host, ATMCI_SR);
1855 mask = atmci_readl(host, ATMCI_IMR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001856 pending = status & mask;
1857 if (!pending)
1858 break;
1859
1860 if (pending & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001861 atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001862 | ATMCI_RXRDY | ATMCI_TXRDY
1863 | ATMCI_ENDRX | ATMCI_ENDTX
1864 | ATMCI_RXBUFF | ATMCI_TXBUFE);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001865
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001866 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001867 smp_wmb();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001868 atmci_set_pending(host, EVENT_DATA_ERROR);
1869 tasklet_schedule(&host->tasklet);
1870 }
Ludovic Desroches796211b2011-08-11 15:25:44 +00001871
Ludovic Desroches796211b2011-08-11 15:25:44 +00001872 if (pending & ATMCI_TXBUFE) {
1873 atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001874 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001875 /*
1876 * We can receive this interruption before having configured
1877 * the second pdc buffer, so we need to reconfigure first and
1878 * second buffers again
1879 */
1880 if (host->data_size) {
1881 atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001882 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001883 atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
1884 } else {
1885 atmci_pdc_complete(host);
1886 }
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001887 } else if (pending & ATMCI_ENDTX) {
1888 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
1889
1890 if (host->data_size) {
1891 atmci_pdc_set_single_buf(host,
1892 XFER_TRANSMIT, PDC_SECOND_BUF);
1893 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
1894 }
Ludovic Desroches796211b2011-08-11 15:25:44 +00001895 }
1896
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001897 if (pending & ATMCI_RXBUFF) {
1898 atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
1899 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
1900 /*
1901 * We can receive this interruption before having configured
1902 * the second pdc buffer, so we need to reconfigure first and
1903 * second buffers again
1904 */
1905 if (host->data_size) {
1906 atmci_pdc_set_both_buf(host, XFER_RECEIVE);
1907 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
1908 atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
1909 } else {
1910 atmci_pdc_complete(host);
1911 }
1912 } else if (pending & ATMCI_ENDRX) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00001913 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
1914
1915 if (host->data_size) {
1916 atmci_pdc_set_single_buf(host,
1917 XFER_RECEIVE, PDC_SECOND_BUF);
1918 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
1919 }
1920 }
1921
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001922 /*
1923 * First mci IPs, so mainly the ones having pdc, have some
1924 * issues with the notbusy signal. You can't get it after
1925 * data transmission if you have not sent a stop command.
1926 * The appropriate workaround is to use the BLKE signal.
1927 */
1928 if (pending & ATMCI_BLKE) {
1929 atmci_writel(host, ATMCI_IDR, ATMCI_BLKE);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001930 smp_wmb();
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001931 atmci_set_pending(host, EVENT_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001932 tasklet_schedule(&host->tasklet);
1933 }
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001934
1935 if (pending & ATMCI_NOTBUSY) {
1936 atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY);
1937 smp_wmb();
1938 atmci_set_pending(host, EVENT_NOTBUSY);
1939 tasklet_schedule(&host->tasklet);
1940 }
1941
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001942 if (pending & ATMCI_RXRDY)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001943 atmci_read_data_pio(host);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001944 if (pending & ATMCI_TXRDY)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001945 atmci_write_data_pio(host);
1946
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001947 if (pending & ATMCI_CMDRDY) {
1948 atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
1949 host->cmd_status = status;
1950 smp_wmb();
1951 atmci_set_pending(host, EVENT_CMD_RDY);
1952 tasklet_schedule(&host->tasklet);
1953 }
Anders Grahn88ff82e2010-05-26 14:42:01 -07001954
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001955 if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
Anders Grahn88ff82e2010-05-26 14:42:01 -07001956 atmci_sdio_interrupt(host, status);
1957
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001958 } while (pass_count++ < 5);
1959
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001960 return pass_count ? IRQ_HANDLED : IRQ_NONE;
1961}
1962
1963static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
1964{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001965 struct atmel_mci_slot *slot = dev_id;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001966
1967 /*
1968 * Disable interrupts until the pin has stabilized and check
1969 * the state then. Use mod_timer() since we may be in the
1970 * middle of the timer routine when this interrupt triggers.
1971 */
1972 disable_irq_nosync(irq);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001973 mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001974
1975 return IRQ_HANDLED;
1976}
1977
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001978static int __init atmci_init_slot(struct atmel_mci *host,
1979 struct mci_slot_pdata *slot_data, unsigned int id,
Anders Grahn88ff82e2010-05-26 14:42:01 -07001980 u32 sdc_reg, u32 sdio_irq)
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001981{
1982 struct mmc_host *mmc;
1983 struct atmel_mci_slot *slot;
1984
1985 mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
1986 if (!mmc)
1987 return -ENOMEM;
1988
1989 slot = mmc_priv(mmc);
1990 slot->mmc = mmc;
1991 slot->host = host;
1992 slot->detect_pin = slot_data->detect_pin;
1993 slot->wp_pin = slot_data->wp_pin;
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001994 slot->detect_is_active_high = slot_data->detect_is_active_high;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001995 slot->sdc_reg = sdc_reg;
Anders Grahn88ff82e2010-05-26 14:42:01 -07001996 slot->sdio_irq = sdio_irq;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001997
1998 mmc->ops = &atmci_ops;
1999 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
2000 mmc->f_max = host->bus_hz / 2;
2001 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Anders Grahn88ff82e2010-05-26 14:42:01 -07002002 if (sdio_irq)
2003 mmc->caps |= MMC_CAP_SDIO_IRQ;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002004 if (host->caps.has_highspeed)
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07002005 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002006 /*
2007 * Without the read/write proof capability, it is strongly suggested to
2008 * use only one bit for data to prevent fifo underruns and overruns
2009 * which will corrupt data.
2010 */
2011 if ((slot_data->bus_width >= 4) && host->caps.has_rwproof)
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002012 mmc->caps |= MMC_CAP_4_BIT_DATA;
2013
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002014 if (atmci_get_version(host) < 0x200) {
2015 mmc->max_segs = 256;
2016 mmc->max_blk_size = 4095;
2017 mmc->max_blk_count = 256;
2018 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2019 mmc->max_seg_size = mmc->max_blk_size * mmc->max_segs;
2020 } else {
2021 mmc->max_segs = 64;
2022 mmc->max_req_size = 32768 * 512;
2023 mmc->max_blk_size = 32768;
2024 mmc->max_blk_count = 512;
2025 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002026
2027 /* Assume card is present initially */
2028 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
2029 if (gpio_is_valid(slot->detect_pin)) {
2030 if (gpio_request(slot->detect_pin, "mmc_detect")) {
2031 dev_dbg(&mmc->class_dev, "no detect pin available\n");
2032 slot->detect_pin = -EBUSY;
Jonas Larsson1c1452b2009-03-31 11:16:48 +02002033 } else if (gpio_get_value(slot->detect_pin) ^
2034 slot->detect_is_active_high) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002035 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
2036 }
2037 }
2038
2039 if (!gpio_is_valid(slot->detect_pin))
2040 mmc->caps |= MMC_CAP_NEEDS_POLL;
2041
2042 if (gpio_is_valid(slot->wp_pin)) {
2043 if (gpio_request(slot->wp_pin, "mmc_wp")) {
2044 dev_dbg(&mmc->class_dev, "no WP pin available\n");
2045 slot->wp_pin = -EBUSY;
2046 }
2047 }
2048
2049 host->slot[id] = slot;
2050 mmc_add_host(mmc);
2051
2052 if (gpio_is_valid(slot->detect_pin)) {
2053 int ret;
2054
2055 setup_timer(&slot->detect_timer, atmci_detect_change,
2056 (unsigned long)slot);
2057
2058 ret = request_irq(gpio_to_irq(slot->detect_pin),
2059 atmci_detect_interrupt,
2060 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
2061 "mmc-detect", slot);
2062 if (ret) {
2063 dev_dbg(&mmc->class_dev,
2064 "could not request IRQ %d for detect pin\n",
2065 gpio_to_irq(slot->detect_pin));
2066 gpio_free(slot->detect_pin);
2067 slot->detect_pin = -EBUSY;
2068 }
2069 }
2070
2071 atmci_init_debugfs(slot);
2072
2073 return 0;
2074}
2075
2076static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
2077 unsigned int id)
2078{
2079 /* Debugfs stuff is cleaned up by mmc core */
2080
2081 set_bit(ATMCI_SHUTDOWN, &slot->flags);
2082 smp_wmb();
2083
2084 mmc_remove_host(slot->mmc);
2085
2086 if (gpio_is_valid(slot->detect_pin)) {
2087 int pin = slot->detect_pin;
2088
2089 free_irq(gpio_to_irq(pin), slot);
2090 del_timer_sync(&slot->detect_timer);
2091 gpio_free(pin);
2092 }
2093 if (gpio_is_valid(slot->wp_pin))
2094 gpio_free(slot->wp_pin);
2095
2096 slot->host->slot[id] = NULL;
2097 mmc_free_host(slot->mmc);
2098}
2099
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002100static bool atmci_filter(struct dma_chan *chan, void *slave)
Dan Williams74465b42009-01-06 11:38:16 -07002101{
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002102 struct mci_dma_data *sl = slave;
Dan Williams74465b42009-01-06 11:38:16 -07002103
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002104 if (sl && find_slave_dev(sl) == chan->device->dev) {
2105 chan->private = slave_data_ptr(sl);
Dan Williams7dd60252009-01-06 11:38:19 -07002106 return true;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002107 } else {
Dan Williams7dd60252009-01-06 11:38:19 -07002108 return false;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002109 }
Dan Williams74465b42009-01-06 11:38:16 -07002110}
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002111
Ludovic Desrochesef878192012-02-09 16:33:53 +01002112static bool atmci_configure_dma(struct atmel_mci *host)
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002113{
2114 struct mci_platform_data *pdata;
2115
2116 if (host == NULL)
Ludovic Desrochesef878192012-02-09 16:33:53 +01002117 return false;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002118
2119 pdata = host->pdev->dev.platform_data;
2120
2121 if (pdata && find_slave_dev(pdata->dma_slave)) {
2122 dma_cap_mask_t mask;
2123
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002124 /* Try to grab a DMA channel */
2125 dma_cap_zero(mask);
2126 dma_cap_set(DMA_SLAVE, mask);
2127 host->dma.chan =
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002128 dma_request_channel(mask, atmci_filter, pdata->dma_slave);
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002129 }
Ludovic Desrochesef878192012-02-09 16:33:53 +01002130 if (!host->dma.chan) {
2131 dev_warn(&host->pdev->dev, "no DMA channel available\n");
2132 return false;
2133 } else {
Nicolas Ferre74791a22009-12-14 18:01:31 -08002134 dev_info(&host->pdev->dev,
Ludovic Desrochesb81cfc42012-02-09 16:33:54 +01002135 "using %s for DMA transfers\n",
Nicolas Ferre74791a22009-12-14 18:01:31 -08002136 dma_chan_name(host->dma.chan));
Viresh Kumare2b35f32012-02-01 16:12:27 +05302137
2138 host->dma_conf.src_addr = host->mapbase + ATMCI_RDR;
2139 host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2140 host->dma_conf.src_maxburst = 1;
2141 host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR;
2142 host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2143 host->dma_conf.dst_maxburst = 1;
2144 host->dma_conf.device_fc = false;
Ludovic Desrochesef878192012-02-09 16:33:53 +01002145 return true;
2146 }
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002147}
Ludovic Desroches796211b2011-08-11 15:25:44 +00002148
Ludovic Desroches796211b2011-08-11 15:25:44 +00002149/*
2150 * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
2151 * HSMCI provides DMA support and a new config register but no more supports
2152 * PDC.
2153 */
2154static void __init atmci_get_cap(struct atmel_mci *host)
2155{
2156 unsigned int version;
2157
2158 version = atmci_get_version(host);
2159 dev_info(&host->pdev->dev,
2160 "version: 0x%x\n", version);
2161
2162 host->caps.has_dma = 0;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002163 host->caps.has_pdc = 1;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002164 host->caps.has_cfg_reg = 0;
2165 host->caps.has_cstor_reg = 0;
2166 host->caps.has_highspeed = 0;
2167 host->caps.has_rwproof = 0;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002168 host->caps.has_odd_clk_div = 0;
Ludovic Desroches24011f32012-05-16 15:26:00 +02002169 host->caps.has_bad_data_ordering = 1;
2170 host->caps.need_reset_after_xfer = 1;
2171 host->caps.need_blksz_mul_4 = 1;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002172
2173 /* keep only major version number */
2174 switch (version & 0xf00) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00002175 case 0x500:
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002176 host->caps.has_odd_clk_div = 1;
2177 case 0x400:
2178 case 0x300:
Ludovic Desroches796211b2011-08-11 15:25:44 +00002179#ifdef CONFIG_AT_HDMAC
2180 host->caps.has_dma = 1;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002181#else
Ludovic Desroches796211b2011-08-11 15:25:44 +00002182 dev_info(&host->pdev->dev,
2183 "has dma capability but dma engine is not selected, then use pio\n");
Dan Williams74465b42009-01-06 11:38:16 -07002184#endif
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002185 host->caps.has_pdc = 0;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002186 host->caps.has_cfg_reg = 1;
2187 host->caps.has_cstor_reg = 1;
2188 host->caps.has_highspeed = 1;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002189 case 0x200:
Ludovic Desroches796211b2011-08-11 15:25:44 +00002190 host->caps.has_rwproof = 1;
Ludovic Desroches24011f32012-05-16 15:26:00 +02002191 host->caps.need_blksz_mul_4 = 0;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002192 case 0x100:
Ludovic Desroches24011f32012-05-16 15:26:00 +02002193 host->caps.has_bad_data_ordering = 0;
2194 host->caps.need_reset_after_xfer = 0;
2195 case 0x0:
Ludovic Desroches796211b2011-08-11 15:25:44 +00002196 break;
2197 default:
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002198 host->caps.has_pdc = 0;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002199 dev_warn(&host->pdev->dev,
2200 "Unmanaged mci version, set minimum capabilities\n");
2201 break;
2202 }
2203}
Dan Williams74465b42009-01-06 11:38:16 -07002204
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002205static int __init atmci_probe(struct platform_device *pdev)
2206{
2207 struct mci_platform_data *pdata;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002208 struct atmel_mci *host;
2209 struct resource *regs;
2210 unsigned int nr_slots;
2211 int irq;
2212 int ret;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002213
2214 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2215 if (!regs)
2216 return -ENXIO;
2217 pdata = pdev->dev.platform_data;
2218 if (!pdata)
2219 return -ENXIO;
2220 irq = platform_get_irq(pdev, 0);
2221 if (irq < 0)
2222 return irq;
2223
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002224 host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL);
2225 if (!host)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002226 return -ENOMEM;
2227
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002228 host->pdev = pdev;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002229 spin_lock_init(&host->lock);
2230 INIT_LIST_HEAD(&host->queue);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002231
2232 host->mck = clk_get(&pdev->dev, "mci_clk");
2233 if (IS_ERR(host->mck)) {
2234 ret = PTR_ERR(host->mck);
2235 goto err_clk_get;
2236 }
2237
2238 ret = -ENOMEM;
H Hartley Sweetene8e3f6c2009-12-14 14:11:56 -05002239 host->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002240 if (!host->regs)
2241 goto err_ioremap;
2242
2243 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002244 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002245 host->bus_hz = clk_get_rate(host->mck);
2246 clk_disable(host->mck);
2247
2248 host->mapbase = regs->start;
2249
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002250 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002251
Kay Sievers89c8aa22009-02-02 21:08:30 +01002252 ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002253 if (ret)
2254 goto err_request_irq;
2255
Ludovic Desroches796211b2011-08-11 15:25:44 +00002256 /* Get MCI capabilities and set operations according to it */
2257 atmci_get_cap(host);
Ludovic Desrochesef878192012-02-09 16:33:53 +01002258 if (host->caps.has_dma && atmci_configure_dma(host)) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00002259 host->prepare_data = &atmci_prepare_data_dma;
2260 host->submit_data = &atmci_submit_data_dma;
2261 host->stop_transfer = &atmci_stop_transfer_dma;
2262 } else if (host->caps.has_pdc) {
2263 dev_info(&pdev->dev, "using PDC\n");
2264 host->prepare_data = &atmci_prepare_data_pdc;
2265 host->submit_data = &atmci_submit_data_pdc;
2266 host->stop_transfer = &atmci_stop_transfer_pdc;
2267 } else {
Ludovic Desrochesef878192012-02-09 16:33:53 +01002268 dev_info(&pdev->dev, "using PIO\n");
Ludovic Desroches796211b2011-08-11 15:25:44 +00002269 host->prepare_data = &atmci_prepare_data;
2270 host->submit_data = &atmci_submit_data;
2271 host->stop_transfer = &atmci_stop_transfer;
2272 }
2273
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002274 platform_set_drvdata(pdev, host);
2275
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002276 /* We need at least one slot to succeed */
2277 nr_slots = 0;
2278 ret = -ENODEV;
2279 if (pdata->slot[0].bus_width) {
2280 ret = atmci_init_slot(host, &pdata->slot[0],
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002281 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002282 if (!ret) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002283 nr_slots++;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002284 host->buf_size = host->slot[0]->mmc->max_req_size;
2285 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002286 }
2287 if (pdata->slot[1].bus_width) {
2288 ret = atmci_init_slot(host, &pdata->slot[1],
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002289 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002290 if (!ret) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002291 nr_slots++;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002292 if (host->slot[1]->mmc->max_req_size > host->buf_size)
2293 host->buf_size =
2294 host->slot[1]->mmc->max_req_size;
2295 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002296 }
2297
Rob Emanuele04d699c2009-09-22 16:45:19 -07002298 if (!nr_slots) {
2299 dev_err(&pdev->dev, "init failed: no slot defined\n");
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002300 goto err_init_slot;
Rob Emanuele04d699c2009-09-22 16:45:19 -07002301 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002302
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002303 if (!host->caps.has_rwproof) {
2304 host->buffer = dma_alloc_coherent(&pdev->dev, host->buf_size,
2305 &host->buf_phys_addr,
2306 GFP_KERNEL);
2307 if (!host->buffer) {
2308 ret = -ENOMEM;
2309 dev_err(&pdev->dev, "buffer allocation failed\n");
2310 goto err_init_slot;
2311 }
2312 }
2313
Ludovic Desroches24011f32012-05-16 15:26:00 +02002314 setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host);
2315
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002316 dev_info(&pdev->dev,
2317 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
2318 host->mapbase, irq, nr_slots);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +02002319
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002320 return 0;
2321
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002322err_init_slot:
Dan Williams74465b42009-01-06 11:38:16 -07002323 if (host->dma.chan)
2324 dma_release_channel(host->dma.chan);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002325 free_irq(irq, host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002326err_request_irq:
2327 iounmap(host->regs);
2328err_ioremap:
2329 clk_put(host->mck);
2330err_clk_get:
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002331 kfree(host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002332 return ret;
2333}
2334
2335static int __exit atmci_remove(struct platform_device *pdev)
2336{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002337 struct atmel_mci *host = platform_get_drvdata(pdev);
2338 unsigned int i;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002339
2340 platform_set_drvdata(pdev, NULL);
2341
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002342 if (host->buffer)
2343 dma_free_coherent(&pdev->dev, host->buf_size,
2344 host->buffer, host->buf_phys_addr);
2345
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002346 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002347 if (host->slot[i])
2348 atmci_cleanup_slot(host->slot[i], i);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002349 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002350
2351 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002352 atmci_writel(host, ATMCI_IDR, ~0UL);
2353 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
2354 atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002355 clk_disable(host->mck);
2356
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02002357#ifdef CONFIG_MMC_ATMELMCI_DMA
Dan Williams74465b42009-01-06 11:38:16 -07002358 if (host->dma.chan)
2359 dma_release_channel(host->dma.chan);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02002360#endif
2361
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002362 free_irq(platform_get_irq(pdev, 0), host);
2363 iounmap(host->regs);
2364
2365 clk_put(host->mck);
2366 kfree(host);
2367
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002368 return 0;
2369}
2370
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002371#ifdef CONFIG_PM
2372static int atmci_suspend(struct device *dev)
2373{
2374 struct atmel_mci *host = dev_get_drvdata(dev);
2375 int i;
2376
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002377 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002378 struct atmel_mci_slot *slot = host->slot[i];
2379 int ret;
2380
2381 if (!slot)
2382 continue;
2383 ret = mmc_suspend_host(slot->mmc);
2384 if (ret < 0) {
2385 while (--i >= 0) {
2386 slot = host->slot[i];
2387 if (slot
2388 && test_bit(ATMCI_SUSPENDED, &slot->flags)) {
2389 mmc_resume_host(host->slot[i]->mmc);
2390 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2391 }
2392 }
2393 return ret;
2394 } else {
2395 set_bit(ATMCI_SUSPENDED, &slot->flags);
2396 }
2397 }
2398
2399 return 0;
2400}
2401
2402static int atmci_resume(struct device *dev)
2403{
2404 struct atmel_mci *host = dev_get_drvdata(dev);
2405 int i;
2406 int ret = 0;
2407
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002408 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002409 struct atmel_mci_slot *slot = host->slot[i];
2410 int err;
2411
2412 slot = host->slot[i];
2413 if (!slot)
2414 continue;
2415 if (!test_bit(ATMCI_SUSPENDED, &slot->flags))
2416 continue;
2417 err = mmc_resume_host(slot->mmc);
2418 if (err < 0)
2419 ret = err;
2420 else
2421 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2422 }
2423
2424 return ret;
2425}
2426static SIMPLE_DEV_PM_OPS(atmci_pm, atmci_suspend, atmci_resume);
2427#define ATMCI_PM_OPS (&atmci_pm)
2428#else
2429#define ATMCI_PM_OPS NULL
2430#endif
2431
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002432static struct platform_driver atmci_driver = {
2433 .remove = __exit_p(atmci_remove),
2434 .driver = {
2435 .name = "atmel_mci",
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002436 .pm = ATMCI_PM_OPS,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002437 },
2438};
2439
2440static int __init atmci_init(void)
2441{
2442 return platform_driver_probe(&atmci_driver, atmci_probe);
2443}
2444
2445static void __exit atmci_exit(void)
2446{
2447 platform_driver_unregister(&atmci_driver);
2448}
2449
Dan Williams74465b42009-01-06 11:38:16 -07002450late_initcall(atmci_init); /* try to load after dma driver when built-in */
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002451module_exit(atmci_exit);
2452
2453MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002454MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002455MODULE_LICENSE("GPL v2");