blob: 34080f590f69441be48dc419748596215aa82918 [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 {
48 EVENT_CMD_COMPLETE = 0,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020049 EVENT_XFER_COMPLETE,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +020050 EVENT_DATA_COMPLETE,
51 EVENT_DATA_ERROR,
52};
53
54enum atmel_mci_state {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +020055 STATE_IDLE = 0,
56 STATE_SENDING_CMD,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +020057 STATE_SENDING_DATA,
58 STATE_DATA_BUSY,
59 STATE_SENDING_STOP,
60 STATE_DATA_ERROR,
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;
80};
81
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020082struct atmel_mci_dma {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020083 struct dma_chan *chan;
84 struct dma_async_tx_descriptor *data_desc;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020085};
86
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +020087/**
88 * struct atmel_mci - MMC controller state shared between all slots
89 * @lock: Spinlock protecting the queue and associated data.
90 * @regs: Pointer to MMIO registers.
Ludovic Desroches796211b2011-08-11 15:25:44 +000091 * @sg: Scatterlist entry currently being processed by PIO or PDC code.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +020092 * @pio_offset: Offset into the current scatterlist entry.
93 * @cur_slot: The slot which is currently using the controller.
94 * @mrq: The request currently being processed on @cur_slot,
95 * or NULL if the controller is idle.
96 * @cmd: The command currently being sent to the card, or NULL.
97 * @data: The data currently being transferred, or NULL if no data
98 * transfer is in progress.
Ludovic Desroches796211b2011-08-11 15:25:44 +000099 * @data_size: just data->blocks * data->blksz.
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200100 * @dma: DMA client state.
101 * @data_chan: DMA channel being used for the current data transfer.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200102 * @cmd_status: Snapshot of SR taken upon completion of the current
103 * command. Only valid when EVENT_CMD_COMPLETE is pending.
104 * @data_status: Snapshot of SR taken upon completion of the current
105 * data transfer. Only valid when EVENT_DATA_COMPLETE or
106 * EVENT_DATA_ERROR is pending.
107 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
108 * to be sent.
109 * @tasklet: Tasklet running the request state machine.
110 * @pending_events: Bitmask of events flagged by the interrupt handler
111 * to be processed by the tasklet.
112 * @completed_events: Bitmask of events which the state machine has
113 * processed.
114 * @state: Tasklet state.
115 * @queue: List of slots waiting for access to the controller.
116 * @need_clock_update: Update the clock rate before the next request.
117 * @need_reset: Reset controller before next request.
118 * @mode_reg: Value of the MR register.
Nicolas Ferre74791a22009-12-14 18:01:31 -0800119 * @cfg_reg: Value of the CFG register.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200120 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
121 * rate and timeout calculations.
122 * @mapbase: Physical address of the MMIO registers.
123 * @mck: The peripheral bus clock hooked up to the MMC controller.
124 * @pdev: Platform device associated with the MMC controller.
125 * @slot: Slots sharing this MMC controller.
Ludovic Desroches796211b2011-08-11 15:25:44 +0000126 * @caps: MCI capabilities depending on MCI version.
127 * @prepare_data: function to setup MCI before data transfer which
128 * depends on MCI capabilities.
129 * @submit_data: function to start data transfer which depends on MCI
130 * capabilities.
131 * @stop_transfer: function to stop data transfer which depends on MCI
132 * capabilities.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200133 *
134 * Locking
135 * =======
136 *
137 * @lock is a softirq-safe spinlock protecting @queue as well as
138 * @cur_slot, @mrq and @state. These must always be updated
139 * at the same time while holding @lock.
140 *
141 * @lock also protects mode_reg and need_clock_update since these are
142 * used to synchronize mode register updates with the queue
143 * processing.
144 *
145 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
146 * and must always be written at the same time as the slot is added to
147 * @queue.
148 *
149 * @pending_events and @completed_events are accessed using atomic bit
150 * operations, so they don't need any locking.
151 *
152 * None of the fields touched by the interrupt handler need any
153 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
154 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
155 * interrupts must be disabled and @data_status updated with a
156 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300157 * CMDRDY interrupt must be disabled and @cmd_status updated with a
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200158 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
159 * bytes_xfered field of @data must be written. This is ensured by
160 * using barriers.
161 */
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200162struct atmel_mci {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200163 spinlock_t lock;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200164 void __iomem *regs;
165
166 struct scatterlist *sg;
167 unsigned int pio_offset;
168
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200169 struct atmel_mci_slot *cur_slot;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200170 struct mmc_request *mrq;
171 struct mmc_command *cmd;
172 struct mmc_data *data;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000173 unsigned int data_size;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200174
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200175 struct atmel_mci_dma dma;
176 struct dma_chan *data_chan;
Viresh Kumare2b35f32012-02-01 16:12:27 +0530177 struct dma_slave_config dma_conf;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200178
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200179 u32 cmd_status;
180 u32 data_status;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200181 u32 stop_cmdr;
182
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200183 struct tasklet_struct tasklet;
184 unsigned long pending_events;
185 unsigned long completed_events;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200186 enum atmel_mci_state state;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200187 struct list_head queue;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200188
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200189 bool need_clock_update;
190 bool need_reset;
191 u32 mode_reg;
Nicolas Ferre74791a22009-12-14 18:01:31 -0800192 u32 cfg_reg;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200193 unsigned long bus_hz;
194 unsigned long mapbase;
195 struct clk *mck;
196 struct platform_device *pdev;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200197
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000198 struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
Ludovic Desroches796211b2011-08-11 15:25:44 +0000199
200 struct atmel_mci_caps caps;
201
202 u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
203 void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
204 void (*stop_transfer)(struct atmel_mci *host);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200205};
206
207/**
208 * struct atmel_mci_slot - MMC slot state
209 * @mmc: The mmc_host representing this slot.
210 * @host: The MMC controller this slot is using.
211 * @sdc_reg: Value of SDCR to be written before using this slot.
Anders Grahn88ff82e2010-05-26 14:42:01 -0700212 * @sdio_irq: SDIO irq mask for this slot.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200213 * @mrq: mmc_request currently being processed or waiting to be
214 * processed, or NULL when the slot is idle.
215 * @queue_node: List node for placing this node in the @queue list of
216 * &struct atmel_mci.
217 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
218 * @flags: Random state bits associated with the slot.
219 * @detect_pin: GPIO pin used for card detection, or negative if not
220 * available.
221 * @wp_pin: GPIO pin used for card write protect sending, or negative
222 * if not available.
Jonas Larsson1c1452b2009-03-31 11:16:48 +0200223 * @detect_is_active_high: The state of the detect pin when it is active.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200224 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
225 */
226struct atmel_mci_slot {
227 struct mmc_host *mmc;
228 struct atmel_mci *host;
229
230 u32 sdc_reg;
Anders Grahn88ff82e2010-05-26 14:42:01 -0700231 u32 sdio_irq;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200232
233 struct mmc_request *mrq;
234 struct list_head queue_node;
235
236 unsigned int clock;
237 unsigned long flags;
238#define ATMCI_CARD_PRESENT 0
239#define ATMCI_CARD_NEED_INIT 1
240#define ATMCI_SHUTDOWN 2
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +0200241#define ATMCI_SUSPENDED 3
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200242
243 int detect_pin;
244 int wp_pin;
Jonas Larsson1c1452b2009-03-31 11:16:48 +0200245 bool detect_is_active_high;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200246
247 struct timer_list detect_timer;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200248};
249
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200250#define atmci_test_and_clear_pending(host, event) \
251 test_and_clear_bit(event, &host->pending_events)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200252#define atmci_set_completed(host, event) \
253 set_bit(event, &host->completed_events)
254#define atmci_set_pending(host, event) \
255 set_bit(event, &host->pending_events)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200256
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200257/*
258 * The debugfs stuff below is mostly optimized away when
259 * CONFIG_DEBUG_FS is not set.
260 */
261static int atmci_req_show(struct seq_file *s, void *v)
262{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200263 struct atmel_mci_slot *slot = s->private;
264 struct mmc_request *mrq;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200265 struct mmc_command *cmd;
266 struct mmc_command *stop;
267 struct mmc_data *data;
268
269 /* Make sure we get a consistent snapshot */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200270 spin_lock_bh(&slot->host->lock);
271 mrq = slot->mrq;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200272
273 if (mrq) {
274 cmd = mrq->cmd;
275 data = mrq->data;
276 stop = mrq->stop;
277
278 if (cmd)
279 seq_printf(s,
280 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
281 cmd->opcode, cmd->arg, cmd->flags,
282 cmd->resp[0], cmd->resp[1], cmd->resp[2],
Nicolas Ferred586ebb2010-05-11 14:06:50 -0700283 cmd->resp[3], cmd->error);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200284 if (data)
285 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
286 data->bytes_xfered, data->blocks,
287 data->blksz, data->flags, data->error);
288 if (stop)
289 seq_printf(s,
290 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
291 stop->opcode, stop->arg, stop->flags,
292 stop->resp[0], stop->resp[1], stop->resp[2],
Nicolas Ferred586ebb2010-05-11 14:06:50 -0700293 stop->resp[3], stop->error);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200294 }
295
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200296 spin_unlock_bh(&slot->host->lock);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200297
298 return 0;
299}
300
301static int atmci_req_open(struct inode *inode, struct file *file)
302{
303 return single_open(file, atmci_req_show, inode->i_private);
304}
305
306static const struct file_operations atmci_req_fops = {
307 .owner = THIS_MODULE,
308 .open = atmci_req_open,
309 .read = seq_read,
310 .llseek = seq_lseek,
311 .release = single_release,
312};
313
314static void atmci_show_status_reg(struct seq_file *s,
315 const char *regname, u32 value)
316{
317 static const char *sr_bit[] = {
318 [0] = "CMDRDY",
319 [1] = "RXRDY",
320 [2] = "TXRDY",
321 [3] = "BLKE",
322 [4] = "DTIP",
323 [5] = "NOTBUSY",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700324 [6] = "ENDRX",
325 [7] = "ENDTX",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200326 [8] = "SDIOIRQA",
327 [9] = "SDIOIRQB",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700328 [12] = "SDIOWAIT",
329 [14] = "RXBUFF",
330 [15] = "TXBUFE",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200331 [16] = "RINDE",
332 [17] = "RDIRE",
333 [18] = "RCRCE",
334 [19] = "RENDE",
335 [20] = "RTOE",
336 [21] = "DCRCE",
337 [22] = "DTOE",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700338 [23] = "CSTOE",
339 [24] = "BLKOVRE",
340 [25] = "DMADONE",
341 [26] = "FIFOEMPTY",
342 [27] = "XFRDONE",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200343 [30] = "OVRE",
344 [31] = "UNRE",
345 };
346 unsigned int i;
347
348 seq_printf(s, "%s:\t0x%08x", regname, value);
349 for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
350 if (value & (1 << i)) {
351 if (sr_bit[i])
352 seq_printf(s, " %s", sr_bit[i]);
353 else
354 seq_puts(s, " UNKNOWN");
355 }
356 }
357 seq_putc(s, '\n');
358}
359
360static int atmci_regs_show(struct seq_file *s, void *v)
361{
362 struct atmel_mci *host = s->private;
363 u32 *buf;
364
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000365 buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200366 if (!buf)
367 return -ENOMEM;
368
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200369 /*
370 * Grab a more or less consistent snapshot. Note that we're
371 * not disabling interrupts, so IMR and SR may not be
372 * consistent.
373 */
374 spin_lock_bh(&host->lock);
Haavard Skinnemoen87e60f22008-09-19 21:09:27 +0200375 clk_enable(host->mck);
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000376 memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
Haavard Skinnemoen87e60f22008-09-19 21:09:27 +0200377 clk_disable(host->mck);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200378 spin_unlock_bh(&host->lock);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200379
380 seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000381 buf[ATMCI_MR / 4],
382 buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
383 buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "",
384 buf[ATMCI_MR / 4] & 0xff);
385 seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
386 seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
387 seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200388 seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000389 buf[ATMCI_BLKR / 4],
390 buf[ATMCI_BLKR / 4] & 0xffff,
391 (buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000392 if (host->caps.has_cstor_reg)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000393 seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200394
395 /* Don't read RSPR and RDR; it will consume the data there */
396
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000397 atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
398 atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200399
Ludovic Desroches796211b2011-08-11 15:25:44 +0000400 if (host->caps.has_dma) {
Nicolas Ferre74791a22009-12-14 18:01:31 -0800401 u32 val;
402
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000403 val = buf[ATMCI_DMA / 4];
Nicolas Ferre74791a22009-12-14 18:01:31 -0800404 seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
405 val, val & 3,
406 ((val >> 4) & 3) ?
407 1 << (((val >> 4) & 3) + 1) : 1,
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000408 val & ATMCI_DMAEN ? " DMAEN" : "");
Ludovic Desroches796211b2011-08-11 15:25:44 +0000409 }
410 if (host->caps.has_cfg_reg) {
411 u32 val;
Nicolas Ferre74791a22009-12-14 18:01:31 -0800412
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000413 val = buf[ATMCI_CFG / 4];
Nicolas Ferre74791a22009-12-14 18:01:31 -0800414 seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
415 val,
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000416 val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
417 val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
418 val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
419 val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
Nicolas Ferre74791a22009-12-14 18:01:31 -0800420 }
421
Haavard Skinnemoenb17339a2008-09-19 21:09:28 +0200422 kfree(buf);
423
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200424 return 0;
425}
426
427static int atmci_regs_open(struct inode *inode, struct file *file)
428{
429 return single_open(file, atmci_regs_show, inode->i_private);
430}
431
432static const struct file_operations atmci_regs_fops = {
433 .owner = THIS_MODULE,
434 .open = atmci_regs_open,
435 .read = seq_read,
436 .llseek = seq_lseek,
437 .release = single_release,
438};
439
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200440static void atmci_init_debugfs(struct atmel_mci_slot *slot)
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200441{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200442 struct mmc_host *mmc = slot->mmc;
443 struct atmel_mci *host = slot->host;
444 struct dentry *root;
445 struct dentry *node;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200446
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200447 root = mmc->debugfs_root;
448 if (!root)
449 return;
450
451 node = debugfs_create_file("regs", S_IRUSR, root, host,
452 &atmci_regs_fops);
453 if (IS_ERR(node))
454 return;
455 if (!node)
456 goto err;
457
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200458 node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200459 if (!node)
460 goto err;
461
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200462 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
463 if (!node)
464 goto err;
465
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200466 node = debugfs_create_x32("pending_events", S_IRUSR, root,
467 (u32 *)&host->pending_events);
468 if (!node)
469 goto err;
470
471 node = debugfs_create_x32("completed_events", S_IRUSR, root,
472 (u32 *)&host->completed_events);
473 if (!node)
474 goto err;
475
476 return;
477
478err:
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200479 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200480}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200481
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000482static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200483 unsigned int ns)
484{
Ludovic Desroches66292ad2012-03-28 12:28:33 +0200485 /*
486 * It is easier here to use us instead of ns for the timeout,
487 * it prevents from overflows during calculation.
488 */
489 unsigned int us = DIV_ROUND_UP(ns, 1000);
490
491 /* Maximum clock frequency is host->bus_hz/2 */
492 return us * (DIV_ROUND_UP(host->bus_hz, 2000000));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200493}
494
495static void atmci_set_timeout(struct atmel_mci *host,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200496 struct atmel_mci_slot *slot, struct mmc_data *data)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200497{
498 static unsigned dtomul_to_shift[] = {
499 0, 4, 7, 8, 10, 12, 16, 20
500 };
501 unsigned timeout;
502 unsigned dtocyc;
503 unsigned dtomul;
504
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000505 timeout = atmci_ns_to_clocks(host, data->timeout_ns)
506 + data->timeout_clks;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200507
508 for (dtomul = 0; dtomul < 8; dtomul++) {
509 unsigned shift = dtomul_to_shift[dtomul];
510 dtocyc = (timeout + (1 << shift) - 1) >> shift;
511 if (dtocyc < 15)
512 break;
513 }
514
515 if (dtomul >= 8) {
516 dtomul = 7;
517 dtocyc = 15;
518 }
519
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200520 dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200521 dtocyc << dtomul_to_shift[dtomul]);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000522 atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200523}
524
525/*
526 * Return mask with command flags to be enabled for this command.
527 */
528static u32 atmci_prepare_command(struct mmc_host *mmc,
529 struct mmc_command *cmd)
530{
531 struct mmc_data *data;
532 u32 cmdr;
533
534 cmd->error = -EINPROGRESS;
535
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000536 cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200537
538 if (cmd->flags & MMC_RSP_PRESENT) {
539 if (cmd->flags & MMC_RSP_136)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000540 cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200541 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000542 cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200543 }
544
545 /*
546 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
547 * it's too difficult to determine whether this is an ACMD or
548 * not. Better make it 64.
549 */
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000550 cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200551
552 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000553 cmdr |= ATMCI_CMDR_OPDCMD;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200554
555 data = cmd->data;
556 if (data) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000557 cmdr |= ATMCI_CMDR_START_XFER;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100558
559 if (cmd->opcode == SD_IO_RW_EXTENDED) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000560 cmdr |= ATMCI_CMDR_SDIO_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100561 } else {
562 if (data->flags & MMC_DATA_STREAM)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000563 cmdr |= ATMCI_CMDR_STREAM;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100564 else if (data->blocks > 1)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000565 cmdr |= ATMCI_CMDR_MULTI_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100566 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000567 cmdr |= ATMCI_CMDR_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100568 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200569
570 if (data->flags & MMC_DATA_READ)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000571 cmdr |= ATMCI_CMDR_TRDIR_READ;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200572 }
573
574 return cmdr;
575}
576
Ludovic Desroches11d14882011-08-11 15:25:45 +0000577static void atmci_send_command(struct atmel_mci *host,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200578 struct mmc_command *cmd, u32 cmd_flags)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200579{
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200580 WARN_ON(host->cmd);
581 host->cmd = cmd;
582
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200583 dev_vdbg(&host->pdev->dev,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200584 "start command: ARGR=0x%08x CMDR=0x%08x\n",
585 cmd->arg, cmd_flags);
586
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000587 atmci_writel(host, ATMCI_ARGR, cmd->arg);
588 atmci_writel(host, ATMCI_CMDR, cmd_flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200589}
590
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000591static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200592{
Ludovic Desroches11d14882011-08-11 15:25:45 +0000593 atmci_send_command(host, data->stop, host->stop_cmdr);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000594 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200595}
596
Ludovic Desroches796211b2011-08-11 15:25:44 +0000597/*
598 * Configure given PDC buffer taking care of alignement issues.
599 * Update host->data_size and host->sg.
600 */
601static void atmci_pdc_set_single_buf(struct atmel_mci *host,
602 enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200603{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000604 u32 pointer_reg, counter_reg;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200605
Ludovic Desroches796211b2011-08-11 15:25:44 +0000606 if (dir == XFER_RECEIVE) {
607 pointer_reg = ATMEL_PDC_RPR;
608 counter_reg = ATMEL_PDC_RCR;
609 } else {
610 pointer_reg = ATMEL_PDC_TPR;
611 counter_reg = ATMEL_PDC_TCR;
612 }
613
614 if (buf_nb == PDC_SECOND_BUF) {
Ludovic Desroches1ebbe3d2011-08-11 15:25:46 +0000615 pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
616 counter_reg += ATMEL_PDC_SCND_BUF_OFF;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000617 }
618
619 atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
Ludovic Desroches341fa4c2011-08-11 15:25:47 +0000620 if (host->data_size <= sg_dma_len(host->sg)) {
Ludovic Desroches796211b2011-08-11 15:25:44 +0000621 if (host->data_size & 0x3) {
622 /* If size is different from modulo 4, transfer bytes */
623 atmci_writel(host, counter_reg, host->data_size);
624 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE);
625 } else {
626 /* Else transfer 32-bits words */
627 atmci_writel(host, counter_reg, host->data_size / 4);
628 }
629 host->data_size = 0;
630 } else {
631 /* We assume the size of a page is 32-bits aligned */
Ludovic Desroches341fa4c2011-08-11 15:25:47 +0000632 atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
633 host->data_size -= sg_dma_len(host->sg);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000634 if (host->data_size)
635 host->sg = sg_next(host->sg);
636 }
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200637}
638
Ludovic Desroches796211b2011-08-11 15:25:44 +0000639/*
640 * Configure PDC buffer according to the data size ie configuring one or two
641 * buffers. Don't use this function if you want to configure only the second
642 * buffer. In this case, use atmci_pdc_set_single_buf.
643 */
644static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200645{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000646 atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF);
647 if (host->data_size)
648 atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF);
649}
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200650
Ludovic Desroches796211b2011-08-11 15:25:44 +0000651/*
652 * Unmap sg lists, called when transfer is finished.
653 */
654static void atmci_pdc_cleanup(struct atmel_mci *host)
655{
656 struct mmc_data *data = host->data;
657
658 if (data)
659 dma_unmap_sg(&host->pdev->dev,
660 data->sg, data->sg_len,
661 ((data->flags & MMC_DATA_WRITE)
662 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
663}
664
665/*
666 * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
667 * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
668 * interrupt needed for both transfer directions.
669 */
670static void atmci_pdc_complete(struct atmel_mci *host)
671{
672 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
673 atmci_pdc_cleanup(host);
674
675 /*
676 * If the card was removed, data will be NULL. No point trying
677 * to send the stop command or waiting for NBUSY in this case.
678 */
679 if (host->data) {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200680 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000681 tasklet_schedule(&host->tasklet);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000682 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200683 }
684}
685
Ludovic Desroches796211b2011-08-11 15:25:44 +0000686static void atmci_dma_cleanup(struct atmel_mci *host)
687{
688 struct mmc_data *data = host->data;
689
690 if (data)
691 dma_unmap_sg(host->dma.chan->device->dev,
692 data->sg, data->sg_len,
693 ((data->flags & MMC_DATA_WRITE)
694 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
695}
696
697/*
698 * This function is called by the DMA driver from tasklet context.
699 */
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200700static void atmci_dma_complete(void *arg)
701{
702 struct atmel_mci *host = arg;
703 struct mmc_data *data = host->data;
704
705 dev_vdbg(&host->pdev->dev, "DMA complete\n");
706
Ludovic Desroches796211b2011-08-11 15:25:44 +0000707 if (host->caps.has_dma)
Nicolas Ferre74791a22009-12-14 18:01:31 -0800708 /* Disable DMA hardware handshaking on MCI */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000709 atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
Nicolas Ferre74791a22009-12-14 18:01:31 -0800710
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200711 atmci_dma_cleanup(host);
712
713 /*
714 * If the card was removed, data will be NULL. No point trying
715 * to send the stop command or waiting for NBUSY in this case.
716 */
717 if (data) {
718 atmci_set_pending(host, EVENT_XFER_COMPLETE);
719 tasklet_schedule(&host->tasklet);
720
721 /*
722 * Regardless of what the documentation says, we have
723 * to wait for NOTBUSY even after block read
724 * operations.
725 *
726 * When the DMA transfer is complete, the controller
727 * may still be reading the CRC from the card, i.e.
728 * the data transfer is still in progress and we
729 * haven't seen all the potential error bits yet.
730 *
731 * The interrupt handler will schedule a different
732 * tasklet to finish things up when the data transfer
733 * is completely done.
734 *
735 * We may not complete the mmc request here anyway
736 * because the mmc layer may call back and cause us to
737 * violate the "don't submit new operations from the
738 * completion callback" rule of the dma engine
739 * framework.
740 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000741 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200742 }
743}
744
Ludovic Desroches796211b2011-08-11 15:25:44 +0000745/*
746 * Returns a mask of interrupt flags to be enabled after the whole
747 * request has been prepared.
748 */
749static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
750{
751 u32 iflags;
752
753 data->error = -EINPROGRESS;
754
755 host->sg = data->sg;
756 host->data = data;
757 host->data_chan = NULL;
758
759 iflags = ATMCI_DATA_ERROR_FLAGS;
760
761 /*
762 * Errata: MMC data write operation with less than 12
763 * bytes is impossible.
764 *
765 * Errata: MCI Transmit Data Register (TDR) FIFO
766 * corruption when length is not multiple of 4.
767 */
768 if (data->blocks * data->blksz < 12
769 || (data->blocks * data->blksz) & 3)
770 host->need_reset = true;
771
772 host->pio_offset = 0;
773 if (data->flags & MMC_DATA_READ)
774 iflags |= ATMCI_RXRDY;
775 else
776 iflags |= ATMCI_TXRDY;
777
778 return iflags;
779}
780
781/*
782 * Set interrupt flags and set block length into the MCI mode register even
783 * if this value is also accessible in the MCI block register. It seems to be
784 * necessary before the High Speed MCI version. It also map sg and configure
785 * PDC registers.
786 */
787static u32
788atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
789{
790 u32 iflags, tmp;
791 unsigned int sg_len;
792 enum dma_data_direction dir;
793
794 data->error = -EINPROGRESS;
795
796 host->data = data;
797 host->sg = data->sg;
798 iflags = ATMCI_DATA_ERROR_FLAGS;
799
800 /* Enable pdc mode */
801 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE);
802
803 if (data->flags & MMC_DATA_READ) {
804 dir = DMA_FROM_DEVICE;
805 iflags |= ATMCI_ENDRX | ATMCI_RXBUFF;
806 } else {
807 dir = DMA_TO_DEVICE;
808 iflags |= ATMCI_ENDTX | ATMCI_TXBUFE;
809 }
810
811 /* Set BLKLEN */
812 tmp = atmci_readl(host, ATMCI_MR);
813 tmp &= 0x0000ffff;
814 tmp |= ATMCI_BLKLEN(data->blksz);
815 atmci_writel(host, ATMCI_MR, tmp);
816
817 /* Configure PDC */
818 host->data_size = data->blocks * data->blksz;
819 sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000820 if (host->data_size)
821 atmci_pdc_set_both_buf(host,
822 ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));
823
824 return iflags;
825}
826
827static u32
Nicolas Ferre74791a22009-12-14 18:01:31 -0800828atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200829{
830 struct dma_chan *chan;
831 struct dma_async_tx_descriptor *desc;
832 struct scatterlist *sg;
833 unsigned int i;
834 enum dma_data_direction direction;
Vinod Koule0d23ef2011-11-17 14:54:38 +0530835 enum dma_transfer_direction slave_dirn;
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700836 unsigned int sglen;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000837 u32 iflags;
838
839 data->error = -EINPROGRESS;
840
841 WARN_ON(host->data);
842 host->sg = NULL;
843 host->data = data;
844
845 iflags = ATMCI_DATA_ERROR_FLAGS;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200846
847 /*
848 * We don't do DMA on "complex" transfers, i.e. with
849 * non-word-aligned buffers or lengths. Also, we don't bother
850 * with all the DMA setup overhead for short transfers.
851 */
Ludovic Desroches796211b2011-08-11 15:25:44 +0000852 if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
853 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200854 if (data->blksz & 3)
Ludovic Desroches796211b2011-08-11 15:25:44 +0000855 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200856
857 for_each_sg(data->sg, sg, data->sg_len, i) {
858 if (sg->offset & 3 || sg->length & 3)
Ludovic Desroches796211b2011-08-11 15:25:44 +0000859 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200860 }
861
862 /* If we don't have a channel, we can't do DMA */
863 chan = host->dma.chan;
Dan Williams6f49a572009-01-06 11:38:14 -0700864 if (chan)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200865 host->data_chan = chan;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200866
867 if (!chan)
868 return -ENODEV;
869
Ludovic Desroches796211b2011-08-11 15:25:44 +0000870 if (host->caps.has_dma)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000871 atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(3) | ATMCI_DMAEN);
Nicolas Ferre74791a22009-12-14 18:01:31 -0800872
Vinod Koule0d23ef2011-11-17 14:54:38 +0530873 if (data->flags & MMC_DATA_READ) {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200874 direction = DMA_FROM_DEVICE;
Viresh Kumare2b35f32012-02-01 16:12:27 +0530875 host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM;
Vinod Koule0d23ef2011-11-17 14:54:38 +0530876 } else {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200877 direction = DMA_TO_DEVICE;
Viresh Kumare2b35f32012-02-01 16:12:27 +0530878 host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV;
Vinod Koule0d23ef2011-11-17 14:54:38 +0530879 }
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200880
Linus Walleij266ac3f2011-02-10 16:08:06 +0100881 sglen = dma_map_sg(chan->device->dev, data->sg,
Ludovic Desroches796211b2011-08-11 15:25:44 +0000882 data->sg_len, direction);
Linus Walleij88ce4db32011-02-10 16:08:16 +0100883
Viresh Kumare2b35f32012-02-01 16:12:27 +0530884 dmaengine_slave_config(chan, &host->dma_conf);
Alexandre Bounine16052822012-03-08 16:11:18 -0500885 desc = dmaengine_prep_slave_sg(chan,
Vinod Koule0d23ef2011-11-17 14:54:38 +0530886 data->sg, sglen, slave_dirn,
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200887 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
888 if (!desc)
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700889 goto unmap_exit;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200890
891 host->dma.data_desc = desc;
892 desc->callback = atmci_dma_complete;
893 desc->callback_param = host;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200894
Ludovic Desroches796211b2011-08-11 15:25:44 +0000895 return iflags;
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700896unmap_exit:
Linus Walleij88ce4db32011-02-10 16:08:16 +0100897 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700898 return -ENOMEM;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200899}
900
Ludovic Desroches796211b2011-08-11 15:25:44 +0000901static void
902atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
903{
904 return;
905}
906
907/*
908 * Start PDC according to transfer direction.
909 */
910static void
911atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data)
912{
913 if (data->flags & MMC_DATA_READ)
914 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
915 else
916 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
917}
918
919static void
920atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
Nicolas Ferre74791a22009-12-14 18:01:31 -0800921{
922 struct dma_chan *chan = host->data_chan;
923 struct dma_async_tx_descriptor *desc = host->dma.data_desc;
924
925 if (chan) {
Linus Walleij53289062011-02-10 16:08:26 +0100926 dmaengine_submit(desc);
927 dma_async_issue_pending(chan);
Nicolas Ferre74791a22009-12-14 18:01:31 -0800928 }
929}
930
Ludovic Desroches796211b2011-08-11 15:25:44 +0000931static void atmci_stop_transfer(struct atmel_mci *host)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200932{
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200933 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000934 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200935}
936
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200937/*
Ludovic Desroches796211b2011-08-11 15:25:44 +0000938 * Stop data transfer because error(s) occured.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200939 */
Ludovic Desroches796211b2011-08-11 15:25:44 +0000940static void atmci_stop_transfer_pdc(struct atmel_mci *host)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200941{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000942 atmci_set_pending(host, EVENT_XFER_COMPLETE);
943 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200944}
945
Ludovic Desroches796211b2011-08-11 15:25:44 +0000946static void atmci_stop_transfer_dma(struct atmel_mci *host)
947{
948 struct dma_chan *chan = host->data_chan;
949
950 if (chan) {
951 dmaengine_terminate_all(chan);
952 atmci_dma_cleanup(host);
953 } else {
954 /* Data transfer was stopped by the interrupt handler */
955 atmci_set_pending(host, EVENT_XFER_COMPLETE);
956 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
957 }
958}
959
960/*
961 * Start a request: prepare data if needed, prepare the command and activate
962 * interrupts.
963 */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200964static void atmci_start_request(struct atmel_mci *host,
965 struct atmel_mci_slot *slot)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200966{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200967 struct mmc_request *mrq;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200968 struct mmc_command *cmd;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200969 struct mmc_data *data;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200970 u32 iflags;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200971 u32 cmdflags;
972
973 mrq = slot->mrq;
974 host->cur_slot = slot;
975 host->mrq = mrq;
976
977 host->pending_events = 0;
978 host->completed_events = 0;
Haavard Skinnemoenca55f462008-10-05 15:16:59 +0200979 host->data_status = 0;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200980
981 if (host->need_reset) {
Ludovic Desroches18ee6842012-02-09 11:55:29 +0100982 iflags = atmci_readl(host, ATMCI_IMR);
983 iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000984 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
985 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
986 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000987 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000988 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Ludovic Desroches18ee6842012-02-09 11:55:29 +0100989 atmci_writel(host, ATMCI_IER, iflags);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200990 host->need_reset = false;
991 }
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000992 atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200993
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000994 iflags = atmci_readl(host, ATMCI_IMR);
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000995 if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200996 dev_warn(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
997 iflags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200998
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200999 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
1000 /* Send init sequence (74 clock cycles) */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001001 atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
1002 while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001003 cpu_relax();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001004 }
Nicolas Ferre74791a22009-12-14 18:01:31 -08001005 iflags = 0;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001006 data = mrq->data;
1007 if (data) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001008 atmci_set_timeout(host, slot, data);
Haavard Skinnemoena252e3e2008-10-03 14:46:17 +02001009
1010 /* Must set block count/size before sending command */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001011 atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001012 | ATMCI_BLKLEN(data->blksz));
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001013 dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001014 ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
Nicolas Ferre74791a22009-12-14 18:01:31 -08001015
Ludovic Desroches796211b2011-08-11 15:25:44 +00001016 iflags |= host->prepare_data(host, data);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001017 }
1018
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001019 iflags |= ATMCI_CMDRDY;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001020 cmd = mrq->cmd;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001021 cmdflags = atmci_prepare_command(slot->mmc, cmd);
Ludovic Desroches11d14882011-08-11 15:25:45 +00001022 atmci_send_command(host, cmd, cmdflags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001023
1024 if (data)
Ludovic Desroches796211b2011-08-11 15:25:44 +00001025 host->submit_data(host, data);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001026
1027 if (mrq->stop) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001028 host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001029 host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001030 if (!(data->flags & MMC_DATA_WRITE))
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001031 host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001032 if (data->flags & MMC_DATA_STREAM)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001033 host->stop_cmdr |= ATMCI_CMDR_STREAM;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001034 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001035 host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001036 }
1037
1038 /*
1039 * We could have enabled interrupts earlier, but I suspect
1040 * that would open up a nice can of interesting race
1041 * conditions (e.g. command and data complete, but stop not
1042 * prepared yet.)
1043 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001044 atmci_writel(host, ATMCI_IER, iflags);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001045}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001046
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001047static void atmci_queue_request(struct atmel_mci *host,
1048 struct atmel_mci_slot *slot, struct mmc_request *mrq)
1049{
1050 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1051 host->state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001052
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001053 spin_lock_bh(&host->lock);
1054 slot->mrq = mrq;
1055 if (host->state == STATE_IDLE) {
1056 host->state = STATE_SENDING_CMD;
1057 atmci_start_request(host, slot);
1058 } else {
1059 list_add_tail(&slot->queue_node, &host->queue);
1060 }
1061 spin_unlock_bh(&host->lock);
1062}
1063
1064static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1065{
1066 struct atmel_mci_slot *slot = mmc_priv(mmc);
1067 struct atmel_mci *host = slot->host;
1068 struct mmc_data *data;
1069
1070 WARN_ON(slot->mrq);
1071
1072 /*
1073 * We may "know" the card is gone even though there's still an
1074 * electrical connection. If so, we really need to communicate
1075 * this to the MMC core since there won't be any more
1076 * interrupts as the card is completely removed. Otherwise,
1077 * the MMC core might believe the card is still there even
1078 * though the card was just removed very slowly.
1079 */
1080 if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
1081 mrq->cmd->error = -ENOMEDIUM;
1082 mmc_request_done(mmc, mrq);
1083 return;
1084 }
1085
1086 /* We don't support multiple blocks of weird lengths. */
1087 data = mrq->data;
1088 if (data && data->blocks > 1 && data->blksz & 3) {
1089 mrq->cmd->error = -EINVAL;
1090 mmc_request_done(mmc, mrq);
1091 }
1092
1093 atmci_queue_request(host, slot, mrq);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001094}
1095
1096static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1097{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001098 struct atmel_mci_slot *slot = mmc_priv(mmc);
1099 struct atmel_mci *host = slot->host;
1100 unsigned int i;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001101
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001102 slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001103 switch (ios->bus_width) {
1104 case MMC_BUS_WIDTH_1:
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001105 slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001106 break;
1107 case MMC_BUS_WIDTH_4:
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001108 slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001109 break;
1110 }
1111
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001112 if (ios->clock) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001113 unsigned int clock_min = ~0U;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001114 u32 clkdiv;
1115
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001116 spin_lock_bh(&host->lock);
1117 if (!host->mode_reg) {
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001118 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001119 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1120 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001121 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001122 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001123 }
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001124
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001125 /*
1126 * Use mirror of ios->clock to prevent race with mmc
1127 * core ios update when finding the minimum.
1128 */
1129 slot->clock = ios->clock;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001130 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001131 if (host->slot[i] && host->slot[i]->clock
1132 && host->slot[i]->clock < clock_min)
1133 clock_min = host->slot[i]->clock;
1134 }
1135
1136 /* Calculate clock divider */
1137 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001138 if (clkdiv > 255) {
1139 dev_warn(&mmc->class_dev,
1140 "clock %u too slow; using %lu\n",
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001141 clock_min, host->bus_hz / (2 * 256));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001142 clkdiv = 255;
1143 }
1144
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001145 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
Rob Emanuele04d699c2009-09-22 16:45:19 -07001146
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001147 /*
1148 * WRPROOF and RDPROOF prevent overruns/underruns by
1149 * stopping the clock when the FIFO is full/empty.
1150 * This state is not expected to last for long.
1151 */
Ludovic Desroches796211b2011-08-11 15:25:44 +00001152 if (host->caps.has_rwproof)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001153 host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001154
Ludovic Desroches796211b2011-08-11 15:25:44 +00001155 if (host->caps.has_cfg_reg) {
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001156 /* setup High Speed mode in relation with card capacity */
1157 if (ios->timing == MMC_TIMING_SD_HS)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001158 host->cfg_reg |= ATMCI_CFG_HSMODE;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001159 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001160 host->cfg_reg &= ~ATMCI_CFG_HSMODE;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001161 }
1162
1163 if (list_empty(&host->queue)) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001164 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001165 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001166 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001167 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001168 host->need_clock_update = true;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001169 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001170
1171 spin_unlock_bh(&host->lock);
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001172 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001173 bool any_slot_active = false;
1174
1175 spin_lock_bh(&host->lock);
1176 slot->clock = 0;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001177 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001178 if (host->slot[i] && host->slot[i]->clock) {
1179 any_slot_active = true;
1180 break;
1181 }
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001182 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001183 if (!any_slot_active) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001184 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001185 if (host->mode_reg) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001186 atmci_readl(host, ATMCI_MR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001187 clk_disable(host->mck);
1188 }
1189 host->mode_reg = 0;
1190 }
1191 spin_unlock_bh(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001192 }
1193
1194 switch (ios->power_mode) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001195 case MMC_POWER_UP:
1196 set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
1197 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001198 default:
1199 /*
1200 * TODO: None of the currently available AVR32-based
1201 * boards allow MMC power to be turned off. Implement
1202 * power control when this can be tested properly.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001203 *
1204 * We also need to hook this into the clock management
1205 * somehow so that newly inserted cards aren't
1206 * subjected to a fast clock before we have a chance
1207 * to figure out what the maximum rate is. Currently,
1208 * there's no way to avoid this, and there never will
1209 * be for boards that don't support power control.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001210 */
1211 break;
1212 }
1213}
1214
1215static int atmci_get_ro(struct mmc_host *mmc)
1216{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001217 int read_only = -ENOSYS;
1218 struct atmel_mci_slot *slot = mmc_priv(mmc);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001219
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001220 if (gpio_is_valid(slot->wp_pin)) {
1221 read_only = gpio_get_value(slot->wp_pin);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001222 dev_dbg(&mmc->class_dev, "card is %s\n",
1223 read_only ? "read-only" : "read-write");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001224 }
1225
1226 return read_only;
1227}
1228
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001229static int atmci_get_cd(struct mmc_host *mmc)
1230{
1231 int present = -ENOSYS;
1232 struct atmel_mci_slot *slot = mmc_priv(mmc);
1233
1234 if (gpio_is_valid(slot->detect_pin)) {
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001235 present = !(gpio_get_value(slot->detect_pin) ^
1236 slot->detect_is_active_high);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001237 dev_dbg(&mmc->class_dev, "card is %spresent\n",
1238 present ? "" : "not ");
1239 }
1240
1241 return present;
1242}
1243
Anders Grahn88ff82e2010-05-26 14:42:01 -07001244static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1245{
1246 struct atmel_mci_slot *slot = mmc_priv(mmc);
1247 struct atmel_mci *host = slot->host;
1248
1249 if (enable)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001250 atmci_writel(host, ATMCI_IER, slot->sdio_irq);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001251 else
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001252 atmci_writel(host, ATMCI_IDR, slot->sdio_irq);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001253}
1254
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001255static const struct mmc_host_ops atmci_ops = {
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001256 .request = atmci_request,
1257 .set_ios = atmci_set_ios,
1258 .get_ro = atmci_get_ro,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001259 .get_cd = atmci_get_cd,
Anders Grahn88ff82e2010-05-26 14:42:01 -07001260 .enable_sdio_irq = atmci_enable_sdio_irq,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001261};
1262
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001263/* Called with host->lock held */
1264static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
1265 __releases(&host->lock)
1266 __acquires(&host->lock)
1267{
1268 struct atmel_mci_slot *slot = NULL;
1269 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1270
1271 WARN_ON(host->cmd || host->data);
1272
1273 /*
1274 * Update the MMC clock rate if necessary. This may be
1275 * necessary if set_ios() is called when a different slot is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001276 * busy transferring data.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001277 */
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001278 if (host->need_clock_update) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001279 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001280 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001281 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001282 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001283
1284 host->cur_slot->mrq = NULL;
1285 host->mrq = NULL;
1286 if (!list_empty(&host->queue)) {
1287 slot = list_entry(host->queue.next,
1288 struct atmel_mci_slot, queue_node);
1289 list_del(&slot->queue_node);
1290 dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
1291 mmc_hostname(slot->mmc));
1292 host->state = STATE_SENDING_CMD;
1293 atmci_start_request(host, slot);
1294 } else {
1295 dev_vdbg(&host->pdev->dev, "list empty\n");
1296 host->state = STATE_IDLE;
1297 }
1298
1299 spin_unlock(&host->lock);
1300 mmc_request_done(prev_mmc, mrq);
1301 spin_lock(&host->lock);
1302}
1303
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001304static void atmci_command_complete(struct atmel_mci *host,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001305 struct mmc_command *cmd)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001306{
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001307 u32 status = host->cmd_status;
1308
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001309 /* Read the response from the card (up to 16 bytes) */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001310 cmd->resp[0] = atmci_readl(host, ATMCI_RSPR);
1311 cmd->resp[1] = atmci_readl(host, ATMCI_RSPR);
1312 cmd->resp[2] = atmci_readl(host, ATMCI_RSPR);
1313 cmd->resp[3] = atmci_readl(host, ATMCI_RSPR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001314
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001315 if (status & ATMCI_RTOE)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001316 cmd->error = -ETIMEDOUT;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001317 else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001318 cmd->error = -EILSEQ;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001319 else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001320 cmd->error = -EIO;
1321 else
1322 cmd->error = 0;
1323
1324 if (cmd->error) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001325 dev_dbg(&host->pdev->dev,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001326 "command error: status=0x%08x\n", status);
1327
1328 if (cmd->data) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00001329 host->stop_transfer(host);
Nicolas Ferre009a8912010-05-11 14:06:49 -07001330 host->data = NULL;
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001331 atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001332 | ATMCI_TXRDY | ATMCI_RXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001333 | ATMCI_DATA_ERROR_FLAGS);
1334 }
1335 }
1336}
1337
1338static void atmci_detect_change(unsigned long data)
1339{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001340 struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
1341 bool present;
1342 bool present_old;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001343
1344 /*
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001345 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
1346 * freeing the interrupt. We must not re-enable the interrupt
1347 * if it has been freed, and if we're shutting down, it
1348 * doesn't really matter whether the card is present or not.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001349 */
1350 smp_rmb();
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001351 if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001352 return;
1353
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001354 enable_irq(gpio_to_irq(slot->detect_pin));
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001355 present = !(gpio_get_value(slot->detect_pin) ^
1356 slot->detect_is_active_high);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001357 present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001358
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001359 dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
1360 present, present_old);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001361
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001362 if (present != present_old) {
1363 struct atmel_mci *host = slot->host;
1364 struct mmc_request *mrq;
1365
1366 dev_dbg(&slot->mmc->class_dev, "card %s\n",
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001367 present ? "inserted" : "removed");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001368
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001369 spin_lock(&host->lock);
1370
1371 if (!present)
1372 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1373 else
1374 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001375
1376 /* Clean up queue if present */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001377 mrq = slot->mrq;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001378 if (mrq) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001379 if (mrq == host->mrq) {
1380 /*
1381 * Reset controller to terminate any ongoing
1382 * commands or data transfers.
1383 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001384 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1385 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1386 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001387 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001388 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001389
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001390 host->data = NULL;
1391 host->cmd = NULL;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001392
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001393 switch (host->state) {
1394 case STATE_IDLE:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001395 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001396 case STATE_SENDING_CMD:
1397 mrq->cmd->error = -ENOMEDIUM;
1398 if (!mrq->data)
1399 break;
1400 /* fall through */
1401 case STATE_SENDING_DATA:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001402 mrq->data->error = -ENOMEDIUM;
Ludovic Desroches796211b2011-08-11 15:25:44 +00001403 host->stop_transfer(host);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001404 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001405 case STATE_DATA_BUSY:
1406 case STATE_DATA_ERROR:
1407 if (mrq->data->error == -EINPROGRESS)
1408 mrq->data->error = -ENOMEDIUM;
1409 if (!mrq->stop)
1410 break;
1411 /* fall through */
1412 case STATE_SENDING_STOP:
1413 mrq->stop->error = -ENOMEDIUM;
1414 break;
1415 }
1416
1417 atmci_request_end(host, mrq);
1418 } else {
1419 list_del(&slot->queue_node);
1420 mrq->cmd->error = -ENOMEDIUM;
1421 if (mrq->data)
1422 mrq->data->error = -ENOMEDIUM;
1423 if (mrq->stop)
1424 mrq->stop->error = -ENOMEDIUM;
1425
1426 spin_unlock(&host->lock);
1427 mmc_request_done(slot->mmc, mrq);
1428 spin_lock(&host->lock);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001429 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001430 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001431 spin_unlock(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001432
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001433 mmc_detect_change(slot->mmc, 0);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001434 }
1435}
1436
1437static void atmci_tasklet_func(unsigned long priv)
1438{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001439 struct atmel_mci *host = (struct atmel_mci *)priv;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001440 struct mmc_request *mrq = host->mrq;
1441 struct mmc_data *data = host->data;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001442 struct mmc_command *cmd = host->cmd;
1443 enum atmel_mci_state state = host->state;
1444 enum atmel_mci_state prev_state;
1445 u32 status;
1446
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001447 spin_lock(&host->lock);
1448
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001449 state = host->state;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001450
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001451 dev_vdbg(&host->pdev->dev,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001452 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1453 state, host->pending_events, host->completed_events,
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001454 atmci_readl(host, ATMCI_IMR));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001455
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001456 do {
1457 prev_state = state;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001458
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001459 switch (state) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001460 case STATE_IDLE:
1461 break;
1462
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001463 case STATE_SENDING_CMD:
1464 if (!atmci_test_and_clear_pending(host,
1465 EVENT_CMD_COMPLETE))
1466 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001467
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001468 host->cmd = NULL;
1469 atmci_set_completed(host, EVENT_CMD_COMPLETE);
1470 atmci_command_complete(host, mrq->cmd);
1471 if (!mrq->data || cmd->error) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001472 atmci_request_end(host, host->mrq);
1473 goto unlock;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001474 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001475
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001476 prev_state = state = STATE_SENDING_DATA;
1477 /* fall through */
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001478
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001479 case STATE_SENDING_DATA:
1480 if (atmci_test_and_clear_pending(host,
1481 EVENT_DATA_ERROR)) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00001482 host->stop_transfer(host);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001483 if (data->stop)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001484 atmci_send_stop_cmd(host, data);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001485 state = STATE_DATA_ERROR;
1486 break;
1487 }
1488
1489 if (!atmci_test_and_clear_pending(host,
1490 EVENT_XFER_COMPLETE))
1491 break;
1492
1493 atmci_set_completed(host, EVENT_XFER_COMPLETE);
1494 prev_state = state = STATE_DATA_BUSY;
1495 /* fall through */
1496
1497 case STATE_DATA_BUSY:
1498 if (!atmci_test_and_clear_pending(host,
1499 EVENT_DATA_COMPLETE))
1500 break;
1501
1502 host->data = NULL;
1503 atmci_set_completed(host, EVENT_DATA_COMPLETE);
1504 status = host->data_status;
1505 if (unlikely(status & ATMCI_DATA_ERROR_FLAGS)) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001506 if (status & ATMCI_DTOE) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001507 dev_dbg(&host->pdev->dev,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001508 "data timeout error\n");
1509 data->error = -ETIMEDOUT;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001510 } else if (status & ATMCI_DCRCE) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001511 dev_dbg(&host->pdev->dev,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001512 "data CRC error\n");
1513 data->error = -EILSEQ;
1514 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001515 dev_dbg(&host->pdev->dev,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001516 "data FIFO error (status=%08x)\n",
1517 status);
1518 data->error = -EIO;
1519 }
1520 } else {
1521 data->bytes_xfered = data->blocks * data->blksz;
1522 data->error = 0;
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001523 atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001524 }
1525
1526 if (!data->stop) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001527 atmci_request_end(host, host->mrq);
1528 goto unlock;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001529 }
1530
1531 prev_state = state = STATE_SENDING_STOP;
1532 if (!data->error)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001533 atmci_send_stop_cmd(host, data);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001534 /* fall through */
1535
1536 case STATE_SENDING_STOP:
1537 if (!atmci_test_and_clear_pending(host,
1538 EVENT_CMD_COMPLETE))
1539 break;
1540
1541 host->cmd = NULL;
1542 atmci_command_complete(host, mrq->stop);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001543 atmci_request_end(host, host->mrq);
1544 goto unlock;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001545
1546 case STATE_DATA_ERROR:
1547 if (!atmci_test_and_clear_pending(host,
1548 EVENT_XFER_COMPLETE))
1549 break;
1550
1551 state = STATE_DATA_BUSY;
1552 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001553 }
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001554 } while (state != prev_state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001555
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001556 host->state = state;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001557
1558unlock:
1559 spin_unlock(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001560}
1561
1562static void atmci_read_data_pio(struct atmel_mci *host)
1563{
1564 struct scatterlist *sg = host->sg;
1565 void *buf = sg_virt(sg);
1566 unsigned int offset = host->pio_offset;
1567 struct mmc_data *data = host->data;
1568 u32 value;
1569 u32 status;
1570 unsigned int nbytes = 0;
1571
1572 do {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001573 value = atmci_readl(host, ATMCI_RDR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001574 if (likely(offset + 4 <= sg->length)) {
1575 put_unaligned(value, (u32 *)(buf + offset));
1576
1577 offset += 4;
1578 nbytes += 4;
1579
1580 if (offset == sg->length) {
Haavard Skinnemoen5e7184a2008-10-05 15:27:50 +02001581 flush_dcache_page(sg_page(sg));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001582 host->sg = sg = sg_next(sg);
1583 if (!sg)
1584 goto done;
1585
1586 offset = 0;
1587 buf = sg_virt(sg);
1588 }
1589 } else {
1590 unsigned int remaining = sg->length - offset;
1591 memcpy(buf + offset, &value, remaining);
1592 nbytes += remaining;
1593
1594 flush_dcache_page(sg_page(sg));
1595 host->sg = sg = sg_next(sg);
1596 if (!sg)
1597 goto done;
1598
1599 offset = 4 - remaining;
1600 buf = sg_virt(sg);
1601 memcpy(buf, (u8 *)&value + remaining, offset);
1602 nbytes += offset;
1603 }
1604
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001605 status = atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001606 if (status & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001607 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001608 | ATMCI_DATA_ERROR_FLAGS));
1609 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001610 data->bytes_xfered += nbytes;
1611 smp_wmb();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001612 atmci_set_pending(host, EVENT_DATA_ERROR);
1613 tasklet_schedule(&host->tasklet);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001614 return;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001615 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001616 } while (status & ATMCI_RXRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001617
1618 host->pio_offset = offset;
1619 data->bytes_xfered += nbytes;
1620
1621 return;
1622
1623done:
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001624 atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
1625 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001626 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001627 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001628 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001629}
1630
1631static void atmci_write_data_pio(struct atmel_mci *host)
1632{
1633 struct scatterlist *sg = host->sg;
1634 void *buf = sg_virt(sg);
1635 unsigned int offset = host->pio_offset;
1636 struct mmc_data *data = host->data;
1637 u32 value;
1638 u32 status;
1639 unsigned int nbytes = 0;
1640
1641 do {
1642 if (likely(offset + 4 <= sg->length)) {
1643 value = get_unaligned((u32 *)(buf + offset));
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001644 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001645
1646 offset += 4;
1647 nbytes += 4;
1648 if (offset == sg->length) {
1649 host->sg = sg = sg_next(sg);
1650 if (!sg)
1651 goto done;
1652
1653 offset = 0;
1654 buf = sg_virt(sg);
1655 }
1656 } else {
1657 unsigned int remaining = sg->length - offset;
1658
1659 value = 0;
1660 memcpy(&value, buf + offset, remaining);
1661 nbytes += remaining;
1662
1663 host->sg = sg = sg_next(sg);
1664 if (!sg) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001665 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001666 goto done;
1667 }
1668
1669 offset = 4 - remaining;
1670 buf = sg_virt(sg);
1671 memcpy((u8 *)&value + remaining, buf, offset);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001672 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001673 nbytes += offset;
1674 }
1675
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001676 status = atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001677 if (status & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001678 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001679 | ATMCI_DATA_ERROR_FLAGS));
1680 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001681 data->bytes_xfered += nbytes;
1682 smp_wmb();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001683 atmci_set_pending(host, EVENT_DATA_ERROR);
1684 tasklet_schedule(&host->tasklet);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001685 return;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001686 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001687 } while (status & ATMCI_TXRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001688
1689 host->pio_offset = offset;
1690 data->bytes_xfered += nbytes;
1691
1692 return;
1693
1694done:
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001695 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
1696 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001697 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001698 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001699 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001700}
1701
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001702static void atmci_cmd_interrupt(struct atmel_mci *host, u32 status)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001703{
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001704 atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001705
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001706 host->cmd_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001707 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001708 atmci_set_pending(host, EVENT_CMD_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001709 tasklet_schedule(&host->tasklet);
1710}
1711
Anders Grahn88ff82e2010-05-26 14:42:01 -07001712static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
1713{
1714 int i;
1715
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001716 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Anders Grahn88ff82e2010-05-26 14:42:01 -07001717 struct atmel_mci_slot *slot = host->slot[i];
1718 if (slot && (status & slot->sdio_irq)) {
1719 mmc_signal_sdio_irq(slot->mmc);
1720 }
1721 }
1722}
1723
1724
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001725static irqreturn_t atmci_interrupt(int irq, void *dev_id)
1726{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001727 struct atmel_mci *host = dev_id;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001728 u32 status, mask, pending;
1729 unsigned int pass_count = 0;
1730
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001731 do {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001732 status = atmci_readl(host, ATMCI_SR);
1733 mask = atmci_readl(host, ATMCI_IMR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001734 pending = status & mask;
1735 if (!pending)
1736 break;
1737
1738 if (pending & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001739 atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001740 | ATMCI_RXRDY | ATMCI_TXRDY);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001741 pending &= atmci_readl(host, ATMCI_IMR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001742
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001743 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001744 smp_wmb();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001745 atmci_set_pending(host, EVENT_DATA_ERROR);
1746 tasklet_schedule(&host->tasklet);
1747 }
Ludovic Desroches796211b2011-08-11 15:25:44 +00001748
Ludovic Desroches796211b2011-08-11 15:25:44 +00001749 if (pending & ATMCI_TXBUFE) {
1750 atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001751 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001752 /*
1753 * We can receive this interruption before having configured
1754 * the second pdc buffer, so we need to reconfigure first and
1755 * second buffers again
1756 */
1757 if (host->data_size) {
1758 atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001759 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001760 atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
1761 } else {
1762 atmci_pdc_complete(host);
1763 }
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001764 } else if (pending & ATMCI_ENDTX) {
1765 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
1766
1767 if (host->data_size) {
1768 atmci_pdc_set_single_buf(host,
1769 XFER_TRANSMIT, PDC_SECOND_BUF);
1770 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
1771 }
Ludovic Desroches796211b2011-08-11 15:25:44 +00001772 }
1773
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001774 if (pending & ATMCI_RXBUFF) {
1775 atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
1776 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
1777 /*
1778 * We can receive this interruption before having configured
1779 * the second pdc buffer, so we need to reconfigure first and
1780 * second buffers again
1781 */
1782 if (host->data_size) {
1783 atmci_pdc_set_both_buf(host, XFER_RECEIVE);
1784 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
1785 atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
1786 } else {
1787 atmci_pdc_complete(host);
1788 }
1789 } else if (pending & ATMCI_ENDRX) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00001790 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
1791
1792 if (host->data_size) {
1793 atmci_pdc_set_single_buf(host,
1794 XFER_RECEIVE, PDC_SECOND_BUF);
1795 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
1796 }
1797 }
1798
Ludovic Desroches796211b2011-08-11 15:25:44 +00001799
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001800 if (pending & ATMCI_NOTBUSY) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001801 atmci_writel(host, ATMCI_IDR,
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001802 ATMCI_DATA_ERROR_FLAGS | ATMCI_NOTBUSY);
Haavard Skinnemoenca55f462008-10-05 15:16:59 +02001803 if (!host->data_status)
1804 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001805 smp_wmb();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001806 atmci_set_pending(host, EVENT_DATA_COMPLETE);
1807 tasklet_schedule(&host->tasklet);
1808 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001809 if (pending & ATMCI_RXRDY)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001810 atmci_read_data_pio(host);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001811 if (pending & ATMCI_TXRDY)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001812 atmci_write_data_pio(host);
1813
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001814 if (pending & ATMCI_CMDRDY)
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001815 atmci_cmd_interrupt(host, status);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001816
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001817 if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
Anders Grahn88ff82e2010-05-26 14:42:01 -07001818 atmci_sdio_interrupt(host, status);
1819
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001820 } while (pass_count++ < 5);
1821
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001822 return pass_count ? IRQ_HANDLED : IRQ_NONE;
1823}
1824
1825static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
1826{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001827 struct atmel_mci_slot *slot = dev_id;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001828
1829 /*
1830 * Disable interrupts until the pin has stabilized and check
1831 * the state then. Use mod_timer() since we may be in the
1832 * middle of the timer routine when this interrupt triggers.
1833 */
1834 disable_irq_nosync(irq);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001835 mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001836
1837 return IRQ_HANDLED;
1838}
1839
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001840static int __init atmci_init_slot(struct atmel_mci *host,
1841 struct mci_slot_pdata *slot_data, unsigned int id,
Anders Grahn88ff82e2010-05-26 14:42:01 -07001842 u32 sdc_reg, u32 sdio_irq)
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001843{
1844 struct mmc_host *mmc;
1845 struct atmel_mci_slot *slot;
1846
1847 mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
1848 if (!mmc)
1849 return -ENOMEM;
1850
1851 slot = mmc_priv(mmc);
1852 slot->mmc = mmc;
1853 slot->host = host;
1854 slot->detect_pin = slot_data->detect_pin;
1855 slot->wp_pin = slot_data->wp_pin;
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001856 slot->detect_is_active_high = slot_data->detect_is_active_high;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001857 slot->sdc_reg = sdc_reg;
Anders Grahn88ff82e2010-05-26 14:42:01 -07001858 slot->sdio_irq = sdio_irq;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001859
1860 mmc->ops = &atmci_ops;
1861 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
1862 mmc->f_max = host->bus_hz / 2;
1863 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Anders Grahn88ff82e2010-05-26 14:42:01 -07001864 if (sdio_irq)
1865 mmc->caps |= MMC_CAP_SDIO_IRQ;
Ludovic Desroches796211b2011-08-11 15:25:44 +00001866 if (host->caps.has_highspeed)
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001867 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001868 if (slot_data->bus_width >= 4)
1869 mmc->caps |= MMC_CAP_4_BIT_DATA;
1870
Martin K. Petersena36274e2010-09-10 01:33:59 -04001871 mmc->max_segs = 64;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001872 mmc->max_req_size = 32768 * 512;
1873 mmc->max_blk_size = 32768;
1874 mmc->max_blk_count = 512;
1875
1876 /* Assume card is present initially */
1877 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
1878 if (gpio_is_valid(slot->detect_pin)) {
1879 if (gpio_request(slot->detect_pin, "mmc_detect")) {
1880 dev_dbg(&mmc->class_dev, "no detect pin available\n");
1881 slot->detect_pin = -EBUSY;
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001882 } else if (gpio_get_value(slot->detect_pin) ^
1883 slot->detect_is_active_high) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001884 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1885 }
1886 }
1887
1888 if (!gpio_is_valid(slot->detect_pin))
1889 mmc->caps |= MMC_CAP_NEEDS_POLL;
1890
1891 if (gpio_is_valid(slot->wp_pin)) {
1892 if (gpio_request(slot->wp_pin, "mmc_wp")) {
1893 dev_dbg(&mmc->class_dev, "no WP pin available\n");
1894 slot->wp_pin = -EBUSY;
1895 }
1896 }
1897
1898 host->slot[id] = slot;
1899 mmc_add_host(mmc);
1900
1901 if (gpio_is_valid(slot->detect_pin)) {
1902 int ret;
1903
1904 setup_timer(&slot->detect_timer, atmci_detect_change,
1905 (unsigned long)slot);
1906
1907 ret = request_irq(gpio_to_irq(slot->detect_pin),
1908 atmci_detect_interrupt,
1909 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1910 "mmc-detect", slot);
1911 if (ret) {
1912 dev_dbg(&mmc->class_dev,
1913 "could not request IRQ %d for detect pin\n",
1914 gpio_to_irq(slot->detect_pin));
1915 gpio_free(slot->detect_pin);
1916 slot->detect_pin = -EBUSY;
1917 }
1918 }
1919
1920 atmci_init_debugfs(slot);
1921
1922 return 0;
1923}
1924
1925static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
1926 unsigned int id)
1927{
1928 /* Debugfs stuff is cleaned up by mmc core */
1929
1930 set_bit(ATMCI_SHUTDOWN, &slot->flags);
1931 smp_wmb();
1932
1933 mmc_remove_host(slot->mmc);
1934
1935 if (gpio_is_valid(slot->detect_pin)) {
1936 int pin = slot->detect_pin;
1937
1938 free_irq(gpio_to_irq(pin), slot);
1939 del_timer_sync(&slot->detect_timer);
1940 gpio_free(pin);
1941 }
1942 if (gpio_is_valid(slot->wp_pin))
1943 gpio_free(slot->wp_pin);
1944
1945 slot->host->slot[id] = NULL;
1946 mmc_free_host(slot->mmc);
1947}
1948
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001949static bool atmci_filter(struct dma_chan *chan, void *slave)
Dan Williams74465b42009-01-06 11:38:16 -07001950{
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001951 struct mci_dma_data *sl = slave;
Dan Williams74465b42009-01-06 11:38:16 -07001952
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001953 if (sl && find_slave_dev(sl) == chan->device->dev) {
1954 chan->private = slave_data_ptr(sl);
Dan Williams7dd60252009-01-06 11:38:19 -07001955 return true;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001956 } else {
Dan Williams7dd60252009-01-06 11:38:19 -07001957 return false;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001958 }
Dan Williams74465b42009-01-06 11:38:16 -07001959}
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001960
Ludovic Desrochesef878192012-02-09 16:33:53 +01001961static bool atmci_configure_dma(struct atmel_mci *host)
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001962{
1963 struct mci_platform_data *pdata;
1964
1965 if (host == NULL)
Ludovic Desrochesef878192012-02-09 16:33:53 +01001966 return false;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001967
1968 pdata = host->pdev->dev.platform_data;
1969
1970 if (pdata && find_slave_dev(pdata->dma_slave)) {
1971 dma_cap_mask_t mask;
1972
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001973 /* Try to grab a DMA channel */
1974 dma_cap_zero(mask);
1975 dma_cap_set(DMA_SLAVE, mask);
1976 host->dma.chan =
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001977 dma_request_channel(mask, atmci_filter, pdata->dma_slave);
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001978 }
Ludovic Desrochesef878192012-02-09 16:33:53 +01001979 if (!host->dma.chan) {
1980 dev_warn(&host->pdev->dev, "no DMA channel available\n");
1981 return false;
1982 } else {
Nicolas Ferre74791a22009-12-14 18:01:31 -08001983 dev_info(&host->pdev->dev,
Ludovic Desrochesb81cfc42012-02-09 16:33:54 +01001984 "using %s for DMA transfers\n",
Nicolas Ferre74791a22009-12-14 18:01:31 -08001985 dma_chan_name(host->dma.chan));
Viresh Kumare2b35f32012-02-01 16:12:27 +05301986
1987 host->dma_conf.src_addr = host->mapbase + ATMCI_RDR;
1988 host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1989 host->dma_conf.src_maxburst = 1;
1990 host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR;
1991 host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1992 host->dma_conf.dst_maxburst = 1;
1993 host->dma_conf.device_fc = false;
Ludovic Desrochesef878192012-02-09 16:33:53 +01001994 return true;
1995 }
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08001996}
Ludovic Desroches796211b2011-08-11 15:25:44 +00001997
1998static inline unsigned int atmci_get_version(struct atmel_mci *host)
1999{
2000 return atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
2001}
2002
2003/*
2004 * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
2005 * HSMCI provides DMA support and a new config register but no more supports
2006 * PDC.
2007 */
2008static void __init atmci_get_cap(struct atmel_mci *host)
2009{
2010 unsigned int version;
2011
2012 version = atmci_get_version(host);
2013 dev_info(&host->pdev->dev,
2014 "version: 0x%x\n", version);
2015
2016 host->caps.has_dma = 0;
2017 host->caps.has_pdc = 0;
2018 host->caps.has_cfg_reg = 0;
2019 host->caps.has_cstor_reg = 0;
2020 host->caps.has_highspeed = 0;
2021 host->caps.has_rwproof = 0;
2022
2023 /* keep only major version number */
2024 switch (version & 0xf00) {
2025 case 0x100:
2026 case 0x200:
2027 host->caps.has_pdc = 1;
2028 host->caps.has_rwproof = 1;
2029 break;
2030 case 0x300:
2031 case 0x400:
2032 case 0x500:
2033#ifdef CONFIG_AT_HDMAC
2034 host->caps.has_dma = 1;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002035#else
Ludovic Desroches796211b2011-08-11 15:25:44 +00002036 host->caps.has_dma = 0;
2037 dev_info(&host->pdev->dev,
2038 "has dma capability but dma engine is not selected, then use pio\n");
Dan Williams74465b42009-01-06 11:38:16 -07002039#endif
Ludovic Desroches796211b2011-08-11 15:25:44 +00002040 host->caps.has_cfg_reg = 1;
2041 host->caps.has_cstor_reg = 1;
2042 host->caps.has_highspeed = 1;
2043 host->caps.has_rwproof = 1;
2044 break;
2045 default:
2046 dev_warn(&host->pdev->dev,
2047 "Unmanaged mci version, set minimum capabilities\n");
2048 break;
2049 }
2050}
Dan Williams74465b42009-01-06 11:38:16 -07002051
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002052static int __init atmci_probe(struct platform_device *pdev)
2053{
2054 struct mci_platform_data *pdata;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002055 struct atmel_mci *host;
2056 struct resource *regs;
2057 unsigned int nr_slots;
2058 int irq;
2059 int ret;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002060
2061 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2062 if (!regs)
2063 return -ENXIO;
2064 pdata = pdev->dev.platform_data;
2065 if (!pdata)
2066 return -ENXIO;
2067 irq = platform_get_irq(pdev, 0);
2068 if (irq < 0)
2069 return irq;
2070
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002071 host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL);
2072 if (!host)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002073 return -ENOMEM;
2074
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002075 host->pdev = pdev;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002076 spin_lock_init(&host->lock);
2077 INIT_LIST_HEAD(&host->queue);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002078
2079 host->mck = clk_get(&pdev->dev, "mci_clk");
2080 if (IS_ERR(host->mck)) {
2081 ret = PTR_ERR(host->mck);
2082 goto err_clk_get;
2083 }
2084
2085 ret = -ENOMEM;
H Hartley Sweetene8e3f6c2009-12-14 14:11:56 -05002086 host->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002087 if (!host->regs)
2088 goto err_ioremap;
2089
2090 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002091 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002092 host->bus_hz = clk_get_rate(host->mck);
2093 clk_disable(host->mck);
2094
2095 host->mapbase = regs->start;
2096
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002097 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002098
Kay Sievers89c8aa22009-02-02 21:08:30 +01002099 ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002100 if (ret)
2101 goto err_request_irq;
2102
Ludovic Desroches796211b2011-08-11 15:25:44 +00002103 /* Get MCI capabilities and set operations according to it */
2104 atmci_get_cap(host);
Ludovic Desrochesef878192012-02-09 16:33:53 +01002105 if (host->caps.has_dma && atmci_configure_dma(host)) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00002106 host->prepare_data = &atmci_prepare_data_dma;
2107 host->submit_data = &atmci_submit_data_dma;
2108 host->stop_transfer = &atmci_stop_transfer_dma;
2109 } else if (host->caps.has_pdc) {
2110 dev_info(&pdev->dev, "using PDC\n");
2111 host->prepare_data = &atmci_prepare_data_pdc;
2112 host->submit_data = &atmci_submit_data_pdc;
2113 host->stop_transfer = &atmci_stop_transfer_pdc;
2114 } else {
Ludovic Desrochesef878192012-02-09 16:33:53 +01002115 dev_info(&pdev->dev, "using PIO\n");
Ludovic Desroches796211b2011-08-11 15:25:44 +00002116 host->prepare_data = &atmci_prepare_data;
2117 host->submit_data = &atmci_submit_data;
2118 host->stop_transfer = &atmci_stop_transfer;
2119 }
2120
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002121 platform_set_drvdata(pdev, host);
2122
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002123 /* We need at least one slot to succeed */
2124 nr_slots = 0;
2125 ret = -ENODEV;
2126 if (pdata->slot[0].bus_width) {
2127 ret = atmci_init_slot(host, &pdata->slot[0],
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002128 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002129 if (!ret)
2130 nr_slots++;
2131 }
2132 if (pdata->slot[1].bus_width) {
2133 ret = atmci_init_slot(host, &pdata->slot[1],
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002134 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002135 if (!ret)
2136 nr_slots++;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002137 }
2138
Rob Emanuele04d699c2009-09-22 16:45:19 -07002139 if (!nr_slots) {
2140 dev_err(&pdev->dev, "init failed: no slot defined\n");
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002141 goto err_init_slot;
Rob Emanuele04d699c2009-09-22 16:45:19 -07002142 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002143
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002144 dev_info(&pdev->dev,
2145 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
2146 host->mapbase, irq, nr_slots);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +02002147
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002148 return 0;
2149
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002150err_init_slot:
Dan Williams74465b42009-01-06 11:38:16 -07002151 if (host->dma.chan)
2152 dma_release_channel(host->dma.chan);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002153 free_irq(irq, host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002154err_request_irq:
2155 iounmap(host->regs);
2156err_ioremap:
2157 clk_put(host->mck);
2158err_clk_get:
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002159 kfree(host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002160 return ret;
2161}
2162
2163static int __exit atmci_remove(struct platform_device *pdev)
2164{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002165 struct atmel_mci *host = platform_get_drvdata(pdev);
2166 unsigned int i;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002167
2168 platform_set_drvdata(pdev, NULL);
2169
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002170 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002171 if (host->slot[i])
2172 atmci_cleanup_slot(host->slot[i], i);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002173 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002174
2175 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002176 atmci_writel(host, ATMCI_IDR, ~0UL);
2177 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
2178 atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002179 clk_disable(host->mck);
2180
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02002181#ifdef CONFIG_MMC_ATMELMCI_DMA
Dan Williams74465b42009-01-06 11:38:16 -07002182 if (host->dma.chan)
2183 dma_release_channel(host->dma.chan);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02002184#endif
2185
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002186 free_irq(platform_get_irq(pdev, 0), host);
2187 iounmap(host->regs);
2188
2189 clk_put(host->mck);
2190 kfree(host);
2191
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002192 return 0;
2193}
2194
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002195#ifdef CONFIG_PM
2196static int atmci_suspend(struct device *dev)
2197{
2198 struct atmel_mci *host = dev_get_drvdata(dev);
2199 int i;
2200
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002201 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002202 struct atmel_mci_slot *slot = host->slot[i];
2203 int ret;
2204
2205 if (!slot)
2206 continue;
2207 ret = mmc_suspend_host(slot->mmc);
2208 if (ret < 0) {
2209 while (--i >= 0) {
2210 slot = host->slot[i];
2211 if (slot
2212 && test_bit(ATMCI_SUSPENDED, &slot->flags)) {
2213 mmc_resume_host(host->slot[i]->mmc);
2214 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2215 }
2216 }
2217 return ret;
2218 } else {
2219 set_bit(ATMCI_SUSPENDED, &slot->flags);
2220 }
2221 }
2222
2223 return 0;
2224}
2225
2226static int atmci_resume(struct device *dev)
2227{
2228 struct atmel_mci *host = dev_get_drvdata(dev);
2229 int i;
2230 int ret = 0;
2231
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002232 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002233 struct atmel_mci_slot *slot = host->slot[i];
2234 int err;
2235
2236 slot = host->slot[i];
2237 if (!slot)
2238 continue;
2239 if (!test_bit(ATMCI_SUSPENDED, &slot->flags))
2240 continue;
2241 err = mmc_resume_host(slot->mmc);
2242 if (err < 0)
2243 ret = err;
2244 else
2245 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2246 }
2247
2248 return ret;
2249}
2250static SIMPLE_DEV_PM_OPS(atmci_pm, atmci_suspend, atmci_resume);
2251#define ATMCI_PM_OPS (&atmci_pm)
2252#else
2253#define ATMCI_PM_OPS NULL
2254#endif
2255
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002256static struct platform_driver atmci_driver = {
2257 .remove = __exit_p(atmci_remove),
2258 .driver = {
2259 .name = "atmel_mci",
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002260 .pm = ATMCI_PM_OPS,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002261 },
2262};
2263
2264static int __init atmci_init(void)
2265{
2266 return platform_driver_probe(&atmci_driver, atmci_probe);
2267}
2268
2269static void __exit atmci_exit(void)
2270{
2271 platform_driver_unregister(&atmci_driver);
2272}
2273
Dan Williams74465b42009-01-06 11:38:16 -07002274late_initcall(atmci_init); /* try to load after dma driver when built-in */
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002275module_exit(atmci_exit);
2276
2277MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002278MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002279MODULE_LICENSE("GPL v2");