blob: 6823d963376eecbfc38e81976d9220d7db880558 [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>
Ludovic Desrochese919fd22012-07-24 15:30:03 +020022#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/of_gpio.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020025#include <linux/platform_device.h>
26#include <linux/scatterlist.h>
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +020027#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +020029#include <linux/stat.h>
Viresh Kumare2b35f32012-02-01 16:12:27 +053030#include <linux/types.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020031
32#include <linux/mmc/host.h>
Nicolas Ferre2f1d7912010-12-10 19:14:32 +010033#include <linux/mmc/sdio.h>
Nicolas Ferre2635d1b2009-12-14 18:01:30 -080034
35#include <mach/atmel-mci.h>
Nicolas Ferrec42aa772008-11-20 15:59:12 +010036#include <linux/atmel-mci.h>
Ludovic Desroches796211b2011-08-11 15:25:44 +000037#include <linux/atmel_pdc.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020038
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020039#include <asm/io.h>
40#include <asm/unaligned.h>
41
Rob Emanuele04d699c2009-09-22 16:45:19 -070042#include <mach/cpu.h>
Haavard Skinnemoen3663b732008-08-05 13:57:38 +020043#include <mach/board.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020044
45#include "atmel-mci-regs.h"
46
Ludovic Desroches2c96a292011-08-11 15:25:41 +000047#define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020048#define ATMCI_DMA_THRESHOLD 16
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020049
50enum {
Ludovic Desrochesf5177542012-05-16 15:25:59 +020051 EVENT_CMD_RDY = 0,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020052 EVENT_XFER_COMPLETE,
Ludovic Desrochesf5177542012-05-16 15:25:59 +020053 EVENT_NOTBUSY,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +020054 EVENT_DATA_ERROR,
55};
56
57enum atmel_mci_state {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +020058 STATE_IDLE = 0,
59 STATE_SENDING_CMD,
Ludovic Desrochesf5177542012-05-16 15:25:59 +020060 STATE_DATA_XFER,
61 STATE_WAITING_NOTBUSY,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +020062 STATE_SENDING_STOP,
Ludovic Desrochesf5177542012-05-16 15:25:59 +020063 STATE_END_REQUEST,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020064};
65
Ludovic Desroches796211b2011-08-11 15:25:44 +000066enum atmci_xfer_dir {
67 XFER_RECEIVE = 0,
68 XFER_TRANSMIT,
69};
70
71enum atmci_pdc_buf {
72 PDC_FIRST_BUF = 0,
73 PDC_SECOND_BUF,
74};
75
76struct atmel_mci_caps {
Hein_Tiboschccdfe612012-08-30 16:34:38 +000077 bool has_dma_conf_reg;
Ludovic Desroches796211b2011-08-11 15:25:44 +000078 bool has_pdc;
79 bool has_cfg_reg;
80 bool has_cstor_reg;
81 bool has_highspeed;
82 bool has_rwproof;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +010083 bool has_odd_clk_div;
Ludovic Desroches24011f32012-05-16 15:26:00 +020084 bool has_bad_data_ordering;
85 bool need_reset_after_xfer;
86 bool need_blksz_mul_4;
Ludovic Desroches077d4072012-07-24 11:42:04 +020087 bool need_notbusy_for_read_ops;
Ludovic Desroches796211b2011-08-11 15:25:44 +000088};
89
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020090struct atmel_mci_dma {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020091 struct dma_chan *chan;
92 struct dma_async_tx_descriptor *data_desc;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +020093};
94
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +020095/**
96 * struct atmel_mci - MMC controller state shared between all slots
97 * @lock: Spinlock protecting the queue and associated data.
98 * @regs: Pointer to MMIO registers.
Ludovic Desroches796211b2011-08-11 15:25:44 +000099 * @sg: Scatterlist entry currently being processed by PIO or PDC code.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200100 * @pio_offset: Offset into the current scatterlist entry.
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200101 * @buffer: Buffer used if we don't have the r/w proof capability. We
102 * don't have the time to switch pdc buffers so we have to use only
103 * one buffer for the full transaction.
104 * @buf_size: size of the buffer.
105 * @phys_buf_addr: buffer address needed for pdc.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200106 * @cur_slot: The slot which is currently using the controller.
107 * @mrq: The request currently being processed on @cur_slot,
108 * or NULL if the controller is idle.
109 * @cmd: The command currently being sent to the card, or NULL.
110 * @data: The data currently being transferred, or NULL if no data
111 * transfer is in progress.
Ludovic Desroches796211b2011-08-11 15:25:44 +0000112 * @data_size: just data->blocks * data->blksz.
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200113 * @dma: DMA client state.
114 * @data_chan: DMA channel being used for the current data transfer.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200115 * @cmd_status: Snapshot of SR taken upon completion of the current
116 * command. Only valid when EVENT_CMD_COMPLETE is pending.
117 * @data_status: Snapshot of SR taken upon completion of the current
118 * data transfer. Only valid when EVENT_DATA_COMPLETE or
119 * EVENT_DATA_ERROR is pending.
120 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
121 * to be sent.
122 * @tasklet: Tasklet running the request state machine.
123 * @pending_events: Bitmask of events flagged by the interrupt handler
124 * to be processed by the tasklet.
125 * @completed_events: Bitmask of events which the state machine has
126 * processed.
127 * @state: Tasklet state.
128 * @queue: List of slots waiting for access to the controller.
129 * @need_clock_update: Update the clock rate before the next request.
130 * @need_reset: Reset controller before next request.
Ludovic Desroches24011f32012-05-16 15:26:00 +0200131 * @timer: Timer to balance the data timeout error flag which cannot rise.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200132 * @mode_reg: Value of the MR register.
Nicolas Ferre74791a22009-12-14 18:01:31 -0800133 * @cfg_reg: Value of the CFG register.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200134 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
135 * rate and timeout calculations.
136 * @mapbase: Physical address of the MMIO registers.
137 * @mck: The peripheral bus clock hooked up to the MMC controller.
138 * @pdev: Platform device associated with the MMC controller.
139 * @slot: Slots sharing this MMC controller.
Ludovic Desroches796211b2011-08-11 15:25:44 +0000140 * @caps: MCI capabilities depending on MCI version.
141 * @prepare_data: function to setup MCI before data transfer which
142 * depends on MCI capabilities.
143 * @submit_data: function to start data transfer which depends on MCI
144 * capabilities.
145 * @stop_transfer: function to stop data transfer which depends on MCI
146 * capabilities.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200147 *
148 * Locking
149 * =======
150 *
151 * @lock is a softirq-safe spinlock protecting @queue as well as
152 * @cur_slot, @mrq and @state. These must always be updated
153 * at the same time while holding @lock.
154 *
155 * @lock also protects mode_reg and need_clock_update since these are
156 * used to synchronize mode register updates with the queue
157 * processing.
158 *
159 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
160 * and must always be written at the same time as the slot is added to
161 * @queue.
162 *
163 * @pending_events and @completed_events are accessed using atomic bit
164 * operations, so they don't need any locking.
165 *
166 * None of the fields touched by the interrupt handler need any
167 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
168 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
169 * interrupts must be disabled and @data_status updated with a
170 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300171 * CMDRDY interrupt must be disabled and @cmd_status updated with a
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200172 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
173 * bytes_xfered field of @data must be written. This is ensured by
174 * using barriers.
175 */
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200176struct atmel_mci {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200177 spinlock_t lock;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200178 void __iomem *regs;
179
180 struct scatterlist *sg;
181 unsigned int pio_offset;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200182 unsigned int *buffer;
183 unsigned int buf_size;
184 dma_addr_t buf_phys_addr;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200185
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200186 struct atmel_mci_slot *cur_slot;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200187 struct mmc_request *mrq;
188 struct mmc_command *cmd;
189 struct mmc_data *data;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000190 unsigned int data_size;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200191
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200192 struct atmel_mci_dma dma;
193 struct dma_chan *data_chan;
Viresh Kumare2b35f32012-02-01 16:12:27 +0530194 struct dma_slave_config dma_conf;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200195
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200196 u32 cmd_status;
197 u32 data_status;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200198 u32 stop_cmdr;
199
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200200 struct tasklet_struct tasklet;
201 unsigned long pending_events;
202 unsigned long completed_events;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200203 enum atmel_mci_state state;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200204 struct list_head queue;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200205
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200206 bool need_clock_update;
207 bool need_reset;
Ludovic Desroches24011f32012-05-16 15:26:00 +0200208 struct timer_list timer;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200209 u32 mode_reg;
Nicolas Ferre74791a22009-12-14 18:01:31 -0800210 u32 cfg_reg;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200211 unsigned long bus_hz;
212 unsigned long mapbase;
213 struct clk *mck;
214 struct platform_device *pdev;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200215
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000216 struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
Ludovic Desroches796211b2011-08-11 15:25:44 +0000217
218 struct atmel_mci_caps caps;
219
220 u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
221 void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
222 void (*stop_transfer)(struct atmel_mci *host);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200223};
224
225/**
226 * struct atmel_mci_slot - MMC slot state
227 * @mmc: The mmc_host representing this slot.
228 * @host: The MMC controller this slot is using.
229 * @sdc_reg: Value of SDCR to be written before using this slot.
Anders Grahn88ff82e2010-05-26 14:42:01 -0700230 * @sdio_irq: SDIO irq mask for this slot.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200231 * @mrq: mmc_request currently being processed or waiting to be
232 * processed, or NULL when the slot is idle.
233 * @queue_node: List node for placing this node in the @queue list of
234 * &struct atmel_mci.
235 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
236 * @flags: Random state bits associated with the slot.
237 * @detect_pin: GPIO pin used for card detection, or negative if not
238 * available.
239 * @wp_pin: GPIO pin used for card write protect sending, or negative
240 * if not available.
Jonas Larsson1c1452b2009-03-31 11:16:48 +0200241 * @detect_is_active_high: The state of the detect pin when it is active.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200242 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
243 */
244struct atmel_mci_slot {
245 struct mmc_host *mmc;
246 struct atmel_mci *host;
247
248 u32 sdc_reg;
Anders Grahn88ff82e2010-05-26 14:42:01 -0700249 u32 sdio_irq;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200250
251 struct mmc_request *mrq;
252 struct list_head queue_node;
253
254 unsigned int clock;
255 unsigned long flags;
256#define ATMCI_CARD_PRESENT 0
257#define ATMCI_CARD_NEED_INIT 1
258#define ATMCI_SHUTDOWN 2
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +0200259#define ATMCI_SUSPENDED 3
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200260
261 int detect_pin;
262 int wp_pin;
Jonas Larsson1c1452b2009-03-31 11:16:48 +0200263 bool detect_is_active_high;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200264
265 struct timer_list detect_timer;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200266};
267
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200268#define atmci_test_and_clear_pending(host, event) \
269 test_and_clear_bit(event, &host->pending_events)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200270#define atmci_set_completed(host, event) \
271 set_bit(event, &host->completed_events)
272#define atmci_set_pending(host, event) \
273 set_bit(event, &host->pending_events)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200274
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200275/*
276 * The debugfs stuff below is mostly optimized away when
277 * CONFIG_DEBUG_FS is not set.
278 */
279static int atmci_req_show(struct seq_file *s, void *v)
280{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200281 struct atmel_mci_slot *slot = s->private;
282 struct mmc_request *mrq;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200283 struct mmc_command *cmd;
284 struct mmc_command *stop;
285 struct mmc_data *data;
286
287 /* Make sure we get a consistent snapshot */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200288 spin_lock_bh(&slot->host->lock);
289 mrq = slot->mrq;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200290
291 if (mrq) {
292 cmd = mrq->cmd;
293 data = mrq->data;
294 stop = mrq->stop;
295
296 if (cmd)
297 seq_printf(s,
298 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
299 cmd->opcode, cmd->arg, cmd->flags,
300 cmd->resp[0], cmd->resp[1], cmd->resp[2],
Nicolas Ferred586ebb2010-05-11 14:06:50 -0700301 cmd->resp[3], cmd->error);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200302 if (data)
303 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
304 data->bytes_xfered, data->blocks,
305 data->blksz, data->flags, data->error);
306 if (stop)
307 seq_printf(s,
308 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
309 stop->opcode, stop->arg, stop->flags,
310 stop->resp[0], stop->resp[1], stop->resp[2],
Nicolas Ferred586ebb2010-05-11 14:06:50 -0700311 stop->resp[3], stop->error);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200312 }
313
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200314 spin_unlock_bh(&slot->host->lock);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200315
316 return 0;
317}
318
319static int atmci_req_open(struct inode *inode, struct file *file)
320{
321 return single_open(file, atmci_req_show, inode->i_private);
322}
323
324static const struct file_operations atmci_req_fops = {
325 .owner = THIS_MODULE,
326 .open = atmci_req_open,
327 .read = seq_read,
328 .llseek = seq_lseek,
329 .release = single_release,
330};
331
332static void atmci_show_status_reg(struct seq_file *s,
333 const char *regname, u32 value)
334{
335 static const char *sr_bit[] = {
336 [0] = "CMDRDY",
337 [1] = "RXRDY",
338 [2] = "TXRDY",
339 [3] = "BLKE",
340 [4] = "DTIP",
341 [5] = "NOTBUSY",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700342 [6] = "ENDRX",
343 [7] = "ENDTX",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200344 [8] = "SDIOIRQA",
345 [9] = "SDIOIRQB",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700346 [12] = "SDIOWAIT",
347 [14] = "RXBUFF",
348 [15] = "TXBUFE",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200349 [16] = "RINDE",
350 [17] = "RDIRE",
351 [18] = "RCRCE",
352 [19] = "RENDE",
353 [20] = "RTOE",
354 [21] = "DCRCE",
355 [22] = "DTOE",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700356 [23] = "CSTOE",
357 [24] = "BLKOVRE",
358 [25] = "DMADONE",
359 [26] = "FIFOEMPTY",
360 [27] = "XFRDONE",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200361 [30] = "OVRE",
362 [31] = "UNRE",
363 };
364 unsigned int i;
365
366 seq_printf(s, "%s:\t0x%08x", regname, value);
367 for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
368 if (value & (1 << i)) {
369 if (sr_bit[i])
370 seq_printf(s, " %s", sr_bit[i]);
371 else
372 seq_puts(s, " UNKNOWN");
373 }
374 }
375 seq_putc(s, '\n');
376}
377
378static int atmci_regs_show(struct seq_file *s, void *v)
379{
380 struct atmel_mci *host = s->private;
381 u32 *buf;
382
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000383 buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200384 if (!buf)
385 return -ENOMEM;
386
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200387 /*
388 * Grab a more or less consistent snapshot. Note that we're
389 * not disabling interrupts, so IMR and SR may not be
390 * consistent.
391 */
392 spin_lock_bh(&host->lock);
Haavard Skinnemoen87e60f22008-09-19 21:09:27 +0200393 clk_enable(host->mck);
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000394 memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
Haavard Skinnemoen87e60f22008-09-19 21:09:27 +0200395 clk_disable(host->mck);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200396 spin_unlock_bh(&host->lock);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200397
Nicolas Ferre8a4de072012-07-06 12:11:51 +0200398 seq_printf(s, "MR:\t0x%08x%s%s ",
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000399 buf[ATMCI_MR / 4],
400 buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
Nicolas Ferre8a4de072012-07-06 12:11:51 +0200401 buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "");
402 if (host->caps.has_odd_clk_div)
403 seq_printf(s, "{CLKDIV,CLKODD}=%u\n",
404 ((buf[ATMCI_MR / 4] & 0xff) << 1)
405 | ((buf[ATMCI_MR / 4] >> 16) & 1));
406 else
407 seq_printf(s, "CLKDIV=%u\n",
408 (buf[ATMCI_MR / 4] & 0xff));
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000409 seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
410 seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
411 seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200412 seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000413 buf[ATMCI_BLKR / 4],
414 buf[ATMCI_BLKR / 4] & 0xffff,
415 (buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000416 if (host->caps.has_cstor_reg)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000417 seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200418
419 /* Don't read RSPR and RDR; it will consume the data there */
420
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000421 atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
422 atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200423
Hein_Tiboschccdfe612012-08-30 16:34:38 +0000424 if (host->caps.has_dma_conf_reg) {
Nicolas Ferre74791a22009-12-14 18:01:31 -0800425 u32 val;
426
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000427 val = buf[ATMCI_DMA / 4];
Nicolas Ferre74791a22009-12-14 18:01:31 -0800428 seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
429 val, val & 3,
430 ((val >> 4) & 3) ?
431 1 << (((val >> 4) & 3) + 1) : 1,
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000432 val & ATMCI_DMAEN ? " DMAEN" : "");
Ludovic Desroches796211b2011-08-11 15:25:44 +0000433 }
434 if (host->caps.has_cfg_reg) {
435 u32 val;
Nicolas Ferre74791a22009-12-14 18:01:31 -0800436
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000437 val = buf[ATMCI_CFG / 4];
Nicolas Ferre74791a22009-12-14 18:01:31 -0800438 seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
439 val,
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000440 val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
441 val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
442 val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
443 val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
Nicolas Ferre74791a22009-12-14 18:01:31 -0800444 }
445
Haavard Skinnemoenb17339a2008-09-19 21:09:28 +0200446 kfree(buf);
447
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200448 return 0;
449}
450
451static int atmci_regs_open(struct inode *inode, struct file *file)
452{
453 return single_open(file, atmci_regs_show, inode->i_private);
454}
455
456static const struct file_operations atmci_regs_fops = {
457 .owner = THIS_MODULE,
458 .open = atmci_regs_open,
459 .read = seq_read,
460 .llseek = seq_lseek,
461 .release = single_release,
462};
463
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200464static void atmci_init_debugfs(struct atmel_mci_slot *slot)
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200465{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200466 struct mmc_host *mmc = slot->mmc;
467 struct atmel_mci *host = slot->host;
468 struct dentry *root;
469 struct dentry *node;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200470
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200471 root = mmc->debugfs_root;
472 if (!root)
473 return;
474
475 node = debugfs_create_file("regs", S_IRUSR, root, host,
476 &atmci_regs_fops);
477 if (IS_ERR(node))
478 return;
479 if (!node)
480 goto err;
481
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200482 node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200483 if (!node)
484 goto err;
485
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200486 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
487 if (!node)
488 goto err;
489
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200490 node = debugfs_create_x32("pending_events", S_IRUSR, root,
491 (u32 *)&host->pending_events);
492 if (!node)
493 goto err;
494
495 node = debugfs_create_x32("completed_events", S_IRUSR, root,
496 (u32 *)&host->completed_events);
497 if (!node)
498 goto err;
499
500 return;
501
502err:
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200503 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200504}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200505
Ludovic Desrochese919fd22012-07-24 15:30:03 +0200506#if defined(CONFIG_OF)
507static const struct of_device_id atmci_dt_ids[] = {
508 { .compatible = "atmel,hsmci" },
509 { /* sentinel */ }
510};
511
512MODULE_DEVICE_TABLE(of, atmci_dt_ids);
513
514static struct mci_platform_data __devinit*
515atmci_of_init(struct platform_device *pdev)
516{
517 struct device_node *np = pdev->dev.of_node;
518 struct device_node *cnp;
519 struct mci_platform_data *pdata;
520 u32 slot_id;
521
522 if (!np) {
523 dev_err(&pdev->dev, "device node not found\n");
524 return ERR_PTR(-EINVAL);
525 }
526
527 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
528 if (!pdata) {
529 dev_err(&pdev->dev, "could not allocate memory for pdata\n");
530 return ERR_PTR(-ENOMEM);
531 }
532
533 for_each_child_of_node(np, cnp) {
534 if (of_property_read_u32(cnp, "reg", &slot_id)) {
535 dev_warn(&pdev->dev, "reg property is missing for %s\n",
536 cnp->full_name);
537 continue;
538 }
539
540 if (slot_id >= ATMCI_MAX_NR_SLOTS) {
541 dev_warn(&pdev->dev, "can't have more than %d slots\n",
542 ATMCI_MAX_NR_SLOTS);
543 break;
544 }
545
546 if (of_property_read_u32(cnp, "bus-width",
547 &pdata->slot[slot_id].bus_width))
548 pdata->slot[slot_id].bus_width = 1;
549
550 pdata->slot[slot_id].detect_pin =
551 of_get_named_gpio(cnp, "cd-gpios", 0);
552
553 pdata->slot[slot_id].detect_is_active_high =
554 of_property_read_bool(cnp, "cd-inverted");
555
556 pdata->slot[slot_id].wp_pin =
557 of_get_named_gpio(cnp, "wp-gpios", 0);
558 }
559
560 return pdata;
561}
562#else /* CONFIG_OF */
563static inline struct mci_platform_data*
564atmci_of_init(struct platform_device *dev)
565{
566 return ERR_PTR(-EINVAL);
567}
568#endif
569
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200570static inline unsigned int atmci_get_version(struct atmel_mci *host)
571{
572 return atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
573}
574
Ludovic Desroches24011f32012-05-16 15:26:00 +0200575static void atmci_timeout_timer(unsigned long data)
576{
577 struct atmel_mci *host;
578
579 host = (struct atmel_mci *)data;
580
581 dev_dbg(&host->pdev->dev, "software timeout\n");
582
583 if (host->mrq->cmd->data) {
584 host->mrq->cmd->data->error = -ETIMEDOUT;
585 host->data = NULL;
586 } else {
587 host->mrq->cmd->error = -ETIMEDOUT;
588 host->cmd = NULL;
589 }
590 host->need_reset = 1;
591 host->state = STATE_END_REQUEST;
592 smp_wmb();
593 tasklet_schedule(&host->tasklet);
594}
595
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000596static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200597 unsigned int ns)
598{
Ludovic Desroches66292ad2012-03-28 12:28:33 +0200599 /*
600 * It is easier here to use us instead of ns for the timeout,
601 * it prevents from overflows during calculation.
602 */
603 unsigned int us = DIV_ROUND_UP(ns, 1000);
604
605 /* Maximum clock frequency is host->bus_hz/2 */
606 return us * (DIV_ROUND_UP(host->bus_hz, 2000000));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200607}
608
609static void atmci_set_timeout(struct atmel_mci *host,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200610 struct atmel_mci_slot *slot, struct mmc_data *data)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200611{
612 static unsigned dtomul_to_shift[] = {
613 0, 4, 7, 8, 10, 12, 16, 20
614 };
615 unsigned timeout;
616 unsigned dtocyc;
617 unsigned dtomul;
618
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000619 timeout = atmci_ns_to_clocks(host, data->timeout_ns)
620 + data->timeout_clks;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200621
622 for (dtomul = 0; dtomul < 8; dtomul++) {
623 unsigned shift = dtomul_to_shift[dtomul];
624 dtocyc = (timeout + (1 << shift) - 1) >> shift;
625 if (dtocyc < 15)
626 break;
627 }
628
629 if (dtomul >= 8) {
630 dtomul = 7;
631 dtocyc = 15;
632 }
633
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200634 dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200635 dtocyc << dtomul_to_shift[dtomul]);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000636 atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200637}
638
639/*
640 * Return mask with command flags to be enabled for this command.
641 */
642static u32 atmci_prepare_command(struct mmc_host *mmc,
643 struct mmc_command *cmd)
644{
645 struct mmc_data *data;
646 u32 cmdr;
647
648 cmd->error = -EINPROGRESS;
649
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000650 cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200651
652 if (cmd->flags & MMC_RSP_PRESENT) {
653 if (cmd->flags & MMC_RSP_136)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000654 cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200655 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000656 cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200657 }
658
659 /*
660 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
661 * it's too difficult to determine whether this is an ACMD or
662 * not. Better make it 64.
663 */
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000664 cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200665
666 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000667 cmdr |= ATMCI_CMDR_OPDCMD;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200668
669 data = cmd->data;
670 if (data) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000671 cmdr |= ATMCI_CMDR_START_XFER;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100672
673 if (cmd->opcode == SD_IO_RW_EXTENDED) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000674 cmdr |= ATMCI_CMDR_SDIO_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100675 } else {
676 if (data->flags & MMC_DATA_STREAM)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000677 cmdr |= ATMCI_CMDR_STREAM;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100678 else if (data->blocks > 1)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000679 cmdr |= ATMCI_CMDR_MULTI_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100680 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000681 cmdr |= ATMCI_CMDR_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100682 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200683
684 if (data->flags & MMC_DATA_READ)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000685 cmdr |= ATMCI_CMDR_TRDIR_READ;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200686 }
687
688 return cmdr;
689}
690
Ludovic Desroches11d14882011-08-11 15:25:45 +0000691static void atmci_send_command(struct atmel_mci *host,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200692 struct mmc_command *cmd, u32 cmd_flags)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200693{
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200694 WARN_ON(host->cmd);
695 host->cmd = cmd;
696
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200697 dev_vdbg(&host->pdev->dev,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200698 "start command: ARGR=0x%08x CMDR=0x%08x\n",
699 cmd->arg, cmd_flags);
700
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000701 atmci_writel(host, ATMCI_ARGR, cmd->arg);
702 atmci_writel(host, ATMCI_CMDR, cmd_flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200703}
704
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000705static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200706{
Ludovic Desroches6801c412012-05-16 15:26:01 +0200707 dev_dbg(&host->pdev->dev, "send stop command\n");
Ludovic Desroches11d14882011-08-11 15:25:45 +0000708 atmci_send_command(host, data->stop, host->stop_cmdr);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000709 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200710}
711
Ludovic Desroches796211b2011-08-11 15:25:44 +0000712/*
713 * Configure given PDC buffer taking care of alignement issues.
714 * Update host->data_size and host->sg.
715 */
716static void atmci_pdc_set_single_buf(struct atmel_mci *host,
717 enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200718{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000719 u32 pointer_reg, counter_reg;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200720 unsigned int buf_size;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200721
Ludovic Desroches796211b2011-08-11 15:25:44 +0000722 if (dir == XFER_RECEIVE) {
723 pointer_reg = ATMEL_PDC_RPR;
724 counter_reg = ATMEL_PDC_RCR;
725 } else {
726 pointer_reg = ATMEL_PDC_TPR;
727 counter_reg = ATMEL_PDC_TCR;
728 }
729
730 if (buf_nb == PDC_SECOND_BUF) {
Ludovic Desroches1ebbe3d2011-08-11 15:25:46 +0000731 pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
732 counter_reg += ATMEL_PDC_SCND_BUF_OFF;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000733 }
734
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200735 if (!host->caps.has_rwproof) {
736 buf_size = host->buf_size;
737 atmci_writel(host, pointer_reg, host->buf_phys_addr);
738 } else {
739 buf_size = sg_dma_len(host->sg);
740 atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
741 }
742
743 if (host->data_size <= buf_size) {
Ludovic Desroches796211b2011-08-11 15:25:44 +0000744 if (host->data_size & 0x3) {
745 /* If size is different from modulo 4, transfer bytes */
746 atmci_writel(host, counter_reg, host->data_size);
747 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE);
748 } else {
749 /* Else transfer 32-bits words */
750 atmci_writel(host, counter_reg, host->data_size / 4);
751 }
752 host->data_size = 0;
753 } else {
754 /* We assume the size of a page is 32-bits aligned */
Ludovic Desroches341fa4c2011-08-11 15:25:47 +0000755 atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
756 host->data_size -= sg_dma_len(host->sg);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000757 if (host->data_size)
758 host->sg = sg_next(host->sg);
759 }
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200760}
761
Ludovic Desroches796211b2011-08-11 15:25:44 +0000762/*
763 * Configure PDC buffer according to the data size ie configuring one or two
764 * buffers. Don't use this function if you want to configure only the second
765 * buffer. In this case, use atmci_pdc_set_single_buf.
766 */
767static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200768{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000769 atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF);
770 if (host->data_size)
771 atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF);
772}
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200773
Ludovic Desroches796211b2011-08-11 15:25:44 +0000774/*
775 * Unmap sg lists, called when transfer is finished.
776 */
777static void atmci_pdc_cleanup(struct atmel_mci *host)
778{
779 struct mmc_data *data = host->data;
780
781 if (data)
782 dma_unmap_sg(&host->pdev->dev,
783 data->sg, data->sg_len,
784 ((data->flags & MMC_DATA_WRITE)
785 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
786}
787
788/*
789 * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
790 * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
791 * interrupt needed for both transfer directions.
792 */
793static void atmci_pdc_complete(struct atmel_mci *host)
794{
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200795 int transfer_size = host->data->blocks * host->data->blksz;
Ludovic Desroches24011f32012-05-16 15:26:00 +0200796 int i;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200797
Ludovic Desroches796211b2011-08-11 15:25:44 +0000798 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200799
800 if ((!host->caps.has_rwproof)
Ludovic Desroches24011f32012-05-16 15:26:00 +0200801 && (host->data->flags & MMC_DATA_READ)) {
802 if (host->caps.has_bad_data_ordering)
803 for (i = 0; i < transfer_size; i++)
804 host->buffer[i] = swab32(host->buffer[i]);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200805 sg_copy_from_buffer(host->data->sg, host->data->sg_len,
806 host->buffer, transfer_size);
Ludovic Desroches24011f32012-05-16 15:26:00 +0200807 }
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200808
Ludovic Desroches796211b2011-08-11 15:25:44 +0000809 atmci_pdc_cleanup(host);
810
811 /*
812 * If the card was removed, data will be NULL. No point trying
813 * to send the stop command or waiting for NBUSY in this case.
814 */
815 if (host->data) {
Ludovic Desroches6801c412012-05-16 15:26:01 +0200816 dev_dbg(&host->pdev->dev,
817 "(%s) set pending xfer complete\n", __func__);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200818 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000819 tasklet_schedule(&host->tasklet);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200820 }
821}
822
Ludovic Desroches796211b2011-08-11 15:25:44 +0000823static void atmci_dma_cleanup(struct atmel_mci *host)
824{
825 struct mmc_data *data = host->data;
826
827 if (data)
828 dma_unmap_sg(host->dma.chan->device->dev,
829 data->sg, data->sg_len,
830 ((data->flags & MMC_DATA_WRITE)
831 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
832}
833
834/*
835 * This function is called by the DMA driver from tasklet context.
836 */
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200837static void atmci_dma_complete(void *arg)
838{
839 struct atmel_mci *host = arg;
840 struct mmc_data *data = host->data;
841
842 dev_vdbg(&host->pdev->dev, "DMA complete\n");
843
Hein_Tiboschccdfe612012-08-30 16:34:38 +0000844 if (host->caps.has_dma_conf_reg)
Nicolas Ferre74791a22009-12-14 18:01:31 -0800845 /* Disable DMA hardware handshaking on MCI */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000846 atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
Nicolas Ferre74791a22009-12-14 18:01:31 -0800847
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200848 atmci_dma_cleanup(host);
849
850 /*
851 * If the card was removed, data will be NULL. No point trying
852 * to send the stop command or waiting for NBUSY in this case.
853 */
854 if (data) {
Ludovic Desroches6801c412012-05-16 15:26:01 +0200855 dev_dbg(&host->pdev->dev,
856 "(%s) set pending xfer complete\n", __func__);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200857 atmci_set_pending(host, EVENT_XFER_COMPLETE);
858 tasklet_schedule(&host->tasklet);
859
860 /*
861 * Regardless of what the documentation says, we have
862 * to wait for NOTBUSY even after block read
863 * operations.
864 *
865 * When the DMA transfer is complete, the controller
866 * may still be reading the CRC from the card, i.e.
867 * the data transfer is still in progress and we
868 * haven't seen all the potential error bits yet.
869 *
870 * The interrupt handler will schedule a different
871 * tasklet to finish things up when the data transfer
872 * is completely done.
873 *
874 * We may not complete the mmc request here anyway
875 * because the mmc layer may call back and cause us to
876 * violate the "don't submit new operations from the
877 * completion callback" rule of the dma engine
878 * framework.
879 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000880 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200881 }
882}
883
Ludovic Desroches796211b2011-08-11 15:25:44 +0000884/*
885 * Returns a mask of interrupt flags to be enabled after the whole
886 * request has been prepared.
887 */
888static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
889{
890 u32 iflags;
891
892 data->error = -EINPROGRESS;
893
894 host->sg = data->sg;
895 host->data = data;
896 host->data_chan = NULL;
897
898 iflags = ATMCI_DATA_ERROR_FLAGS;
899
900 /*
901 * Errata: MMC data write operation with less than 12
902 * bytes is impossible.
903 *
904 * Errata: MCI Transmit Data Register (TDR) FIFO
905 * corruption when length is not multiple of 4.
906 */
907 if (data->blocks * data->blksz < 12
908 || (data->blocks * data->blksz) & 3)
909 host->need_reset = true;
910
911 host->pio_offset = 0;
912 if (data->flags & MMC_DATA_READ)
913 iflags |= ATMCI_RXRDY;
914 else
915 iflags |= ATMCI_TXRDY;
916
917 return iflags;
918}
919
920/*
921 * Set interrupt flags and set block length into the MCI mode register even
922 * if this value is also accessible in the MCI block register. It seems to be
923 * necessary before the High Speed MCI version. It also map sg and configure
924 * PDC registers.
925 */
926static u32
927atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
928{
929 u32 iflags, tmp;
930 unsigned int sg_len;
931 enum dma_data_direction dir;
Ludovic Desroches24011f32012-05-16 15:26:00 +0200932 int i;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000933
934 data->error = -EINPROGRESS;
935
936 host->data = data;
937 host->sg = data->sg;
938 iflags = ATMCI_DATA_ERROR_FLAGS;
939
940 /* Enable pdc mode */
941 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE);
942
943 if (data->flags & MMC_DATA_READ) {
944 dir = DMA_FROM_DEVICE;
945 iflags |= ATMCI_ENDRX | ATMCI_RXBUFF;
946 } else {
947 dir = DMA_TO_DEVICE;
Ludovic Desrochesf5177542012-05-16 15:25:59 +0200948 iflags |= ATMCI_ENDTX | ATMCI_TXBUFE | ATMCI_BLKE;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000949 }
950
951 /* Set BLKLEN */
952 tmp = atmci_readl(host, ATMCI_MR);
953 tmp &= 0x0000ffff;
954 tmp |= ATMCI_BLKLEN(data->blksz);
955 atmci_writel(host, ATMCI_MR, tmp);
956
957 /* Configure PDC */
958 host->data_size = data->blocks * data->blksz;
959 sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200960
961 if ((!host->caps.has_rwproof)
Ludovic Desroches24011f32012-05-16 15:26:00 +0200962 && (host->data->flags & MMC_DATA_WRITE)) {
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200963 sg_copy_to_buffer(host->data->sg, host->data->sg_len,
964 host->buffer, host->data_size);
Ludovic Desroches24011f32012-05-16 15:26:00 +0200965 if (host->caps.has_bad_data_ordering)
966 for (i = 0; i < host->data_size; i++)
967 host->buffer[i] = swab32(host->buffer[i]);
968 }
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200969
Ludovic Desroches796211b2011-08-11 15:25:44 +0000970 if (host->data_size)
971 atmci_pdc_set_both_buf(host,
972 ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));
973
974 return iflags;
975}
976
977static u32
Nicolas Ferre74791a22009-12-14 18:01:31 -0800978atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200979{
980 struct dma_chan *chan;
981 struct dma_async_tx_descriptor *desc;
982 struct scatterlist *sg;
983 unsigned int i;
984 enum dma_data_direction direction;
Vinod Koule0d23ef2011-11-17 14:54:38 +0530985 enum dma_transfer_direction slave_dirn;
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700986 unsigned int sglen;
Nicolas Ferre693e5e22012-06-06 12:19:44 +0200987 u32 maxburst;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000988 u32 iflags;
989
990 data->error = -EINPROGRESS;
991
992 WARN_ON(host->data);
993 host->sg = NULL;
994 host->data = data;
995
996 iflags = ATMCI_DATA_ERROR_FLAGS;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200997
998 /*
999 * We don't do DMA on "complex" transfers, i.e. with
1000 * non-word-aligned buffers or lengths. Also, we don't bother
1001 * with all the DMA setup overhead for short transfers.
1002 */
Ludovic Desroches796211b2011-08-11 15:25:44 +00001003 if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
1004 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001005 if (data->blksz & 3)
Ludovic Desroches796211b2011-08-11 15:25:44 +00001006 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001007
1008 for_each_sg(data->sg, sg, data->sg_len, i) {
1009 if (sg->offset & 3 || sg->length & 3)
Ludovic Desroches796211b2011-08-11 15:25:44 +00001010 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001011 }
1012
1013 /* If we don't have a channel, we can't do DMA */
1014 chan = host->dma.chan;
Dan Williams6f49a572009-01-06 11:38:14 -07001015 if (chan)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001016 host->data_chan = chan;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001017
1018 if (!chan)
1019 return -ENODEV;
1020
Vinod Koule0d23ef2011-11-17 14:54:38 +05301021 if (data->flags & MMC_DATA_READ) {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001022 direction = DMA_FROM_DEVICE;
Viresh Kumare2b35f32012-02-01 16:12:27 +05301023 host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM;
Nicolas Ferre693e5e22012-06-06 12:19:44 +02001024 maxburst = atmci_convert_chksize(host->dma_conf.src_maxburst);
Vinod Koule0d23ef2011-11-17 14:54:38 +05301025 } else {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001026 direction = DMA_TO_DEVICE;
Viresh Kumare2b35f32012-02-01 16:12:27 +05301027 host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV;
Nicolas Ferre693e5e22012-06-06 12:19:44 +02001028 maxburst = atmci_convert_chksize(host->dma_conf.dst_maxburst);
Vinod Koule0d23ef2011-11-17 14:54:38 +05301029 }
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001030
Hein_Tiboschccdfe612012-08-30 16:34:38 +00001031 if (host->caps.has_dma_conf_reg)
1032 atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(maxburst) |
1033 ATMCI_DMAEN);
Nicolas Ferre693e5e22012-06-06 12:19:44 +02001034
Linus Walleij266ac3f2011-02-10 16:08:06 +01001035 sglen = dma_map_sg(chan->device->dev, data->sg,
Ludovic Desroches796211b2011-08-11 15:25:44 +00001036 data->sg_len, direction);
Linus Walleij88ce4db32011-02-10 16:08:16 +01001037
Viresh Kumare2b35f32012-02-01 16:12:27 +05301038 dmaengine_slave_config(chan, &host->dma_conf);
Alexandre Bounine16052822012-03-08 16:11:18 -05001039 desc = dmaengine_prep_slave_sg(chan,
Vinod Koule0d23ef2011-11-17 14:54:38 +05301040 data->sg, sglen, slave_dirn,
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001041 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1042 if (!desc)
Atsushi Nemoto657a77f2009-09-08 17:53:05 -07001043 goto unmap_exit;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001044
1045 host->dma.data_desc = desc;
1046 desc->callback = atmci_dma_complete;
1047 desc->callback_param = host;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001048
Ludovic Desroches796211b2011-08-11 15:25:44 +00001049 return iflags;
Atsushi Nemoto657a77f2009-09-08 17:53:05 -07001050unmap_exit:
Linus Walleij88ce4db32011-02-10 16:08:16 +01001051 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
Atsushi Nemoto657a77f2009-09-08 17:53:05 -07001052 return -ENOMEM;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001053}
1054
Ludovic Desroches796211b2011-08-11 15:25:44 +00001055static void
1056atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
1057{
1058 return;
1059}
1060
1061/*
1062 * Start PDC according to transfer direction.
1063 */
1064static void
1065atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data)
1066{
1067 if (data->flags & MMC_DATA_READ)
1068 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
1069 else
1070 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1071}
1072
1073static void
1074atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
Nicolas Ferre74791a22009-12-14 18:01:31 -08001075{
1076 struct dma_chan *chan = host->data_chan;
1077 struct dma_async_tx_descriptor *desc = host->dma.data_desc;
1078
1079 if (chan) {
Linus Walleij53289062011-02-10 16:08:26 +01001080 dmaengine_submit(desc);
1081 dma_async_issue_pending(chan);
Nicolas Ferre74791a22009-12-14 18:01:31 -08001082 }
1083}
1084
Ludovic Desroches796211b2011-08-11 15:25:44 +00001085static void atmci_stop_transfer(struct atmel_mci *host)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001086{
Ludovic Desroches6801c412012-05-16 15:26:01 +02001087 dev_dbg(&host->pdev->dev,
1088 "(%s) set pending xfer complete\n", __func__);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001089 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001090 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001091}
1092
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001093/*
Ludovic Desroches796211b2011-08-11 15:25:44 +00001094 * Stop data transfer because error(s) occured.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001095 */
Ludovic Desroches796211b2011-08-11 15:25:44 +00001096static void atmci_stop_transfer_pdc(struct atmel_mci *host)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001097{
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001098 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001099}
1100
Ludovic Desroches796211b2011-08-11 15:25:44 +00001101static void atmci_stop_transfer_dma(struct atmel_mci *host)
1102{
1103 struct dma_chan *chan = host->data_chan;
1104
1105 if (chan) {
1106 dmaengine_terminate_all(chan);
1107 atmci_dma_cleanup(host);
1108 } else {
1109 /* Data transfer was stopped by the interrupt handler */
Ludovic Desroches6801c412012-05-16 15:26:01 +02001110 dev_dbg(&host->pdev->dev,
1111 "(%s) set pending xfer complete\n", __func__);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001112 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1113 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1114 }
1115}
1116
1117/*
1118 * Start a request: prepare data if needed, prepare the command and activate
1119 * interrupts.
1120 */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001121static void atmci_start_request(struct atmel_mci *host,
1122 struct atmel_mci_slot *slot)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001123{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001124 struct mmc_request *mrq;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001125 struct mmc_command *cmd;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001126 struct mmc_data *data;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001127 u32 iflags;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001128 u32 cmdflags;
1129
1130 mrq = slot->mrq;
1131 host->cur_slot = slot;
1132 host->mrq = mrq;
1133
1134 host->pending_events = 0;
1135 host->completed_events = 0;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001136 host->cmd_status = 0;
Haavard Skinnemoenca55f462008-10-05 15:16:59 +02001137 host->data_status = 0;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001138
Ludovic Desroches6801c412012-05-16 15:26:01 +02001139 dev_dbg(&host->pdev->dev, "start request: cmd %u\n", mrq->cmd->opcode);
1140
Ludovic Desroches24011f32012-05-16 15:26:00 +02001141 if (host->need_reset || host->caps.need_reset_after_xfer) {
Ludovic Desroches18ee6842012-02-09 11:55:29 +01001142 iflags = atmci_readl(host, ATMCI_IMR);
1143 iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001144 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1145 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1146 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001147 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001148 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Ludovic Desroches18ee6842012-02-09 11:55:29 +01001149 atmci_writel(host, ATMCI_IER, iflags);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001150 host->need_reset = false;
1151 }
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001152 atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001153
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001154 iflags = atmci_readl(host, ATMCI_IMR);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001155 if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001156 dev_dbg(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001157 iflags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001158
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001159 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
1160 /* Send init sequence (74 clock cycles) */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001161 atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
1162 while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001163 cpu_relax();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001164 }
Nicolas Ferre74791a22009-12-14 18:01:31 -08001165 iflags = 0;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001166 data = mrq->data;
1167 if (data) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001168 atmci_set_timeout(host, slot, data);
Haavard Skinnemoena252e3e2008-10-03 14:46:17 +02001169
1170 /* Must set block count/size before sending command */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001171 atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001172 | ATMCI_BLKLEN(data->blksz));
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001173 dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001174 ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
Nicolas Ferre74791a22009-12-14 18:01:31 -08001175
Ludovic Desroches796211b2011-08-11 15:25:44 +00001176 iflags |= host->prepare_data(host, data);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001177 }
1178
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001179 iflags |= ATMCI_CMDRDY;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001180 cmd = mrq->cmd;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001181 cmdflags = atmci_prepare_command(slot->mmc, cmd);
Ludovic Desroches11d14882011-08-11 15:25:45 +00001182 atmci_send_command(host, cmd, cmdflags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001183
1184 if (data)
Ludovic Desroches796211b2011-08-11 15:25:44 +00001185 host->submit_data(host, data);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001186
1187 if (mrq->stop) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001188 host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001189 host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001190 if (!(data->flags & MMC_DATA_WRITE))
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001191 host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001192 if (data->flags & MMC_DATA_STREAM)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001193 host->stop_cmdr |= ATMCI_CMDR_STREAM;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001194 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001195 host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001196 }
1197
1198 /*
1199 * We could have enabled interrupts earlier, but I suspect
1200 * that would open up a nice can of interesting race
1201 * conditions (e.g. command and data complete, but stop not
1202 * prepared yet.)
1203 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001204 atmci_writel(host, ATMCI_IER, iflags);
Ludovic Desroches24011f32012-05-16 15:26:00 +02001205
1206 mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000));
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001207}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001208
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001209static void atmci_queue_request(struct atmel_mci *host,
1210 struct atmel_mci_slot *slot, struct mmc_request *mrq)
1211{
1212 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1213 host->state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001214
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001215 spin_lock_bh(&host->lock);
1216 slot->mrq = mrq;
1217 if (host->state == STATE_IDLE) {
1218 host->state = STATE_SENDING_CMD;
1219 atmci_start_request(host, slot);
1220 } else {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001221 dev_dbg(&host->pdev->dev, "queue request\n");
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001222 list_add_tail(&slot->queue_node, &host->queue);
1223 }
1224 spin_unlock_bh(&host->lock);
1225}
1226
1227static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1228{
1229 struct atmel_mci_slot *slot = mmc_priv(mmc);
1230 struct atmel_mci *host = slot->host;
1231 struct mmc_data *data;
1232
1233 WARN_ON(slot->mrq);
Ludovic Desroches6801c412012-05-16 15:26:01 +02001234 dev_dbg(&host->pdev->dev, "MRQ: cmd %u\n", mrq->cmd->opcode);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001235
1236 /*
1237 * We may "know" the card is gone even though there's still an
1238 * electrical connection. If so, we really need to communicate
1239 * this to the MMC core since there won't be any more
1240 * interrupts as the card is completely removed. Otherwise,
1241 * the MMC core might believe the card is still there even
1242 * though the card was just removed very slowly.
1243 */
1244 if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
1245 mrq->cmd->error = -ENOMEDIUM;
1246 mmc_request_done(mmc, mrq);
1247 return;
1248 }
1249
1250 /* We don't support multiple blocks of weird lengths. */
1251 data = mrq->data;
1252 if (data && data->blocks > 1 && data->blksz & 3) {
1253 mrq->cmd->error = -EINVAL;
1254 mmc_request_done(mmc, mrq);
1255 }
1256
1257 atmci_queue_request(host, slot, mrq);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001258}
1259
1260static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1261{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001262 struct atmel_mci_slot *slot = mmc_priv(mmc);
1263 struct atmel_mci *host = slot->host;
1264 unsigned int i;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001265
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001266 slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001267 switch (ios->bus_width) {
1268 case MMC_BUS_WIDTH_1:
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001269 slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001270 break;
1271 case MMC_BUS_WIDTH_4:
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001272 slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001273 break;
1274 }
1275
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001276 if (ios->clock) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001277 unsigned int clock_min = ~0U;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001278 u32 clkdiv;
1279
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001280 spin_lock_bh(&host->lock);
1281 if (!host->mode_reg) {
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001282 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001283 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1284 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001285 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001286 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001287 }
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001288
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001289 /*
1290 * Use mirror of ios->clock to prevent race with mmc
1291 * core ios update when finding the minimum.
1292 */
1293 slot->clock = ios->clock;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001294 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001295 if (host->slot[i] && host->slot[i]->clock
1296 && host->slot[i]->clock < clock_min)
1297 clock_min = host->slot[i]->clock;
1298 }
1299
1300 /* Calculate clock divider */
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01001301 if (host->caps.has_odd_clk_div) {
1302 clkdiv = DIV_ROUND_UP(host->bus_hz, clock_min) - 2;
1303 if (clkdiv > 511) {
1304 dev_warn(&mmc->class_dev,
1305 "clock %u too slow; using %lu\n",
1306 clock_min, host->bus_hz / (511 + 2));
1307 clkdiv = 511;
1308 }
1309 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv >> 1)
1310 | ATMCI_MR_CLKODD(clkdiv & 1);
1311 } else {
1312 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
1313 if (clkdiv > 255) {
1314 dev_warn(&mmc->class_dev,
1315 "clock %u too slow; using %lu\n",
1316 clock_min, host->bus_hz / (2 * 256));
1317 clkdiv = 255;
1318 }
1319 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001320 }
1321
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001322 /*
1323 * WRPROOF and RDPROOF prevent overruns/underruns by
1324 * stopping the clock when the FIFO is full/empty.
1325 * This state is not expected to last for long.
1326 */
Ludovic Desroches796211b2011-08-11 15:25:44 +00001327 if (host->caps.has_rwproof)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001328 host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001329
Ludovic Desroches796211b2011-08-11 15:25:44 +00001330 if (host->caps.has_cfg_reg) {
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001331 /* setup High Speed mode in relation with card capacity */
1332 if (ios->timing == MMC_TIMING_SD_HS)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001333 host->cfg_reg |= ATMCI_CFG_HSMODE;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001334 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001335 host->cfg_reg &= ~ATMCI_CFG_HSMODE;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001336 }
1337
1338 if (list_empty(&host->queue)) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001339 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001340 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001341 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001342 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001343 host->need_clock_update = true;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001344 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001345
1346 spin_unlock_bh(&host->lock);
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001347 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001348 bool any_slot_active = false;
1349
1350 spin_lock_bh(&host->lock);
1351 slot->clock = 0;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001352 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001353 if (host->slot[i] && host->slot[i]->clock) {
1354 any_slot_active = true;
1355 break;
1356 }
Haavard Skinnemoen945533b52008-10-03 17:48:16 +02001357 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001358 if (!any_slot_active) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001359 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001360 if (host->mode_reg) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001361 atmci_readl(host, ATMCI_MR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001362 clk_disable(host->mck);
1363 }
1364 host->mode_reg = 0;
1365 }
1366 spin_unlock_bh(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001367 }
1368
1369 switch (ios->power_mode) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001370 case MMC_POWER_UP:
1371 set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
1372 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001373 default:
1374 /*
1375 * TODO: None of the currently available AVR32-based
1376 * boards allow MMC power to be turned off. Implement
1377 * power control when this can be tested properly.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001378 *
1379 * We also need to hook this into the clock management
1380 * somehow so that newly inserted cards aren't
1381 * subjected to a fast clock before we have a chance
1382 * to figure out what the maximum rate is. Currently,
1383 * there's no way to avoid this, and there never will
1384 * be for boards that don't support power control.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001385 */
1386 break;
1387 }
1388}
1389
1390static int atmci_get_ro(struct mmc_host *mmc)
1391{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001392 int read_only = -ENOSYS;
1393 struct atmel_mci_slot *slot = mmc_priv(mmc);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001394
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001395 if (gpio_is_valid(slot->wp_pin)) {
1396 read_only = gpio_get_value(slot->wp_pin);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001397 dev_dbg(&mmc->class_dev, "card is %s\n",
1398 read_only ? "read-only" : "read-write");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001399 }
1400
1401 return read_only;
1402}
1403
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001404static int atmci_get_cd(struct mmc_host *mmc)
1405{
1406 int present = -ENOSYS;
1407 struct atmel_mci_slot *slot = mmc_priv(mmc);
1408
1409 if (gpio_is_valid(slot->detect_pin)) {
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001410 present = !(gpio_get_value(slot->detect_pin) ^
1411 slot->detect_is_active_high);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001412 dev_dbg(&mmc->class_dev, "card is %spresent\n",
1413 present ? "" : "not ");
1414 }
1415
1416 return present;
1417}
1418
Anders Grahn88ff82e2010-05-26 14:42:01 -07001419static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1420{
1421 struct atmel_mci_slot *slot = mmc_priv(mmc);
1422 struct atmel_mci *host = slot->host;
1423
1424 if (enable)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001425 atmci_writel(host, ATMCI_IER, slot->sdio_irq);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001426 else
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001427 atmci_writel(host, ATMCI_IDR, slot->sdio_irq);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001428}
1429
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001430static const struct mmc_host_ops atmci_ops = {
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001431 .request = atmci_request,
1432 .set_ios = atmci_set_ios,
1433 .get_ro = atmci_get_ro,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001434 .get_cd = atmci_get_cd,
Anders Grahn88ff82e2010-05-26 14:42:01 -07001435 .enable_sdio_irq = atmci_enable_sdio_irq,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001436};
1437
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001438/* Called with host->lock held */
1439static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
1440 __releases(&host->lock)
1441 __acquires(&host->lock)
1442{
1443 struct atmel_mci_slot *slot = NULL;
1444 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1445
1446 WARN_ON(host->cmd || host->data);
1447
1448 /*
1449 * Update the MMC clock rate if necessary. This may be
1450 * necessary if set_ios() is called when a different slot is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001451 * busy transferring data.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001452 */
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001453 if (host->need_clock_update) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001454 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001455 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001456 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001457 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001458
1459 host->cur_slot->mrq = NULL;
1460 host->mrq = NULL;
1461 if (!list_empty(&host->queue)) {
1462 slot = list_entry(host->queue.next,
1463 struct atmel_mci_slot, queue_node);
1464 list_del(&slot->queue_node);
1465 dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
1466 mmc_hostname(slot->mmc));
1467 host->state = STATE_SENDING_CMD;
1468 atmci_start_request(host, slot);
1469 } else {
1470 dev_vdbg(&host->pdev->dev, "list empty\n");
1471 host->state = STATE_IDLE;
1472 }
1473
Ludovic Desroches24011f32012-05-16 15:26:00 +02001474 del_timer(&host->timer);
1475
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001476 spin_unlock(&host->lock);
1477 mmc_request_done(prev_mmc, mrq);
1478 spin_lock(&host->lock);
1479}
1480
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001481static void atmci_command_complete(struct atmel_mci *host,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001482 struct mmc_command *cmd)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001483{
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001484 u32 status = host->cmd_status;
1485
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001486 /* Read the response from the card (up to 16 bytes) */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001487 cmd->resp[0] = atmci_readl(host, ATMCI_RSPR);
1488 cmd->resp[1] = atmci_readl(host, ATMCI_RSPR);
1489 cmd->resp[2] = atmci_readl(host, ATMCI_RSPR);
1490 cmd->resp[3] = atmci_readl(host, ATMCI_RSPR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001491
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001492 if (status & ATMCI_RTOE)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001493 cmd->error = -ETIMEDOUT;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001494 else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001495 cmd->error = -EILSEQ;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001496 else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001497 cmd->error = -EIO;
Ludovic Desroches24011f32012-05-16 15:26:00 +02001498 else if (host->mrq->data && (host->mrq->data->blksz & 3)) {
1499 if (host->caps.need_blksz_mul_4) {
1500 cmd->error = -EINVAL;
1501 host->need_reset = 1;
1502 }
1503 } else
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001504 cmd->error = 0;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001505}
1506
1507static void atmci_detect_change(unsigned long data)
1508{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001509 struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
1510 bool present;
1511 bool present_old;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001512
1513 /*
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001514 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
1515 * freeing the interrupt. We must not re-enable the interrupt
1516 * if it has been freed, and if we're shutting down, it
1517 * doesn't really matter whether the card is present or not.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001518 */
1519 smp_rmb();
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001520 if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001521 return;
1522
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001523 enable_irq(gpio_to_irq(slot->detect_pin));
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001524 present = !(gpio_get_value(slot->detect_pin) ^
1525 slot->detect_is_active_high);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001526 present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001527
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001528 dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
1529 present, present_old);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001530
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001531 if (present != present_old) {
1532 struct atmel_mci *host = slot->host;
1533 struct mmc_request *mrq;
1534
1535 dev_dbg(&slot->mmc->class_dev, "card %s\n",
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001536 present ? "inserted" : "removed");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001537
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001538 spin_lock(&host->lock);
1539
1540 if (!present)
1541 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1542 else
1543 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001544
1545 /* Clean up queue if present */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001546 mrq = slot->mrq;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001547 if (mrq) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001548 if (mrq == host->mrq) {
1549 /*
1550 * Reset controller to terminate any ongoing
1551 * commands or data transfers.
1552 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001553 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1554 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1555 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001556 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001557 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001558
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001559 host->data = NULL;
1560 host->cmd = NULL;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001561
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001562 switch (host->state) {
1563 case STATE_IDLE:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001564 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001565 case STATE_SENDING_CMD:
1566 mrq->cmd->error = -ENOMEDIUM;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001567 if (mrq->data)
1568 host->stop_transfer(host);
1569 break;
1570 case STATE_DATA_XFER:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001571 mrq->data->error = -ENOMEDIUM;
Ludovic Desroches796211b2011-08-11 15:25:44 +00001572 host->stop_transfer(host);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001573 break;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001574 case STATE_WAITING_NOTBUSY:
1575 mrq->data->error = -ENOMEDIUM;
1576 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001577 case STATE_SENDING_STOP:
1578 mrq->stop->error = -ENOMEDIUM;
1579 break;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001580 case STATE_END_REQUEST:
1581 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001582 }
1583
1584 atmci_request_end(host, mrq);
1585 } else {
1586 list_del(&slot->queue_node);
1587 mrq->cmd->error = -ENOMEDIUM;
1588 if (mrq->data)
1589 mrq->data->error = -ENOMEDIUM;
1590 if (mrq->stop)
1591 mrq->stop->error = -ENOMEDIUM;
1592
1593 spin_unlock(&host->lock);
1594 mmc_request_done(slot->mmc, mrq);
1595 spin_lock(&host->lock);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001596 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001597 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001598 spin_unlock(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001599
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001600 mmc_detect_change(slot->mmc, 0);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001601 }
1602}
1603
1604static void atmci_tasklet_func(unsigned long priv)
1605{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001606 struct atmel_mci *host = (struct atmel_mci *)priv;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001607 struct mmc_request *mrq = host->mrq;
1608 struct mmc_data *data = host->data;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001609 enum atmel_mci_state state = host->state;
1610 enum atmel_mci_state prev_state;
1611 u32 status;
1612
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001613 spin_lock(&host->lock);
1614
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001615 state = host->state;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001616
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001617 dev_vdbg(&host->pdev->dev,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001618 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1619 state, host->pending_events, host->completed_events,
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001620 atmci_readl(host, ATMCI_IMR));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001621
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001622 do {
1623 prev_state = state;
Ludovic Desroches6801c412012-05-16 15:26:01 +02001624 dev_dbg(&host->pdev->dev, "FSM: state=%d\n", state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001625
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001626 switch (state) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001627 case STATE_IDLE:
1628 break;
1629
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001630 case STATE_SENDING_CMD:
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001631 /*
1632 * Command has been sent, we are waiting for command
1633 * ready. Then we have three next states possible:
1634 * END_REQUEST by default, WAITING_NOTBUSY if it's a
1635 * command needing it or DATA_XFER if there is data.
1636 */
Ludovic Desroches6801c412012-05-16 15:26:01 +02001637 dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001638 if (!atmci_test_and_clear_pending(host,
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001639 EVENT_CMD_RDY))
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001640 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001641
Ludovic Desroches6801c412012-05-16 15:26:01 +02001642 dev_dbg(&host->pdev->dev, "set completed cmd ready\n");
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001643 host->cmd = NULL;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001644 atmci_set_completed(host, EVENT_CMD_RDY);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001645 atmci_command_complete(host, mrq->cmd);
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001646 if (mrq->data) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001647 dev_dbg(&host->pdev->dev,
1648 "command with data transfer");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001649 /*
1650 * If there is a command error don't start
1651 * data transfer.
1652 */
1653 if (mrq->cmd->error) {
1654 host->stop_transfer(host);
1655 host->data = NULL;
1656 atmci_writel(host, ATMCI_IDR,
1657 ATMCI_TXRDY | ATMCI_RXRDY
1658 | ATMCI_DATA_ERROR_FLAGS);
1659 state = STATE_END_REQUEST;
1660 } else
1661 state = STATE_DATA_XFER;
1662 } else if ((!mrq->data) && (mrq->cmd->flags & MMC_RSP_BUSY)) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001663 dev_dbg(&host->pdev->dev,
1664 "command response need waiting notbusy");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001665 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1666 state = STATE_WAITING_NOTBUSY;
1667 } else
1668 state = STATE_END_REQUEST;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001669
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001670 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001671
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001672 case STATE_DATA_XFER:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001673 if (atmci_test_and_clear_pending(host,
1674 EVENT_DATA_ERROR)) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001675 dev_dbg(&host->pdev->dev, "set completed data error\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001676 atmci_set_completed(host, EVENT_DATA_ERROR);
1677 state = STATE_END_REQUEST;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001678 break;
1679 }
1680
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001681 /*
1682 * A data transfer is in progress. The event expected
1683 * to move to the next state depends of data transfer
1684 * type (PDC or DMA). Once transfer done we can move
1685 * to the next step which is WAITING_NOTBUSY in write
1686 * case and directly SENDING_STOP in read case.
1687 */
Ludovic Desroches6801c412012-05-16 15:26:01 +02001688 dev_dbg(&host->pdev->dev, "FSM: xfer complete?\n");
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001689 if (!atmci_test_and_clear_pending(host,
1690 EVENT_XFER_COMPLETE))
1691 break;
1692
Ludovic Desroches6801c412012-05-16 15:26:01 +02001693 dev_dbg(&host->pdev->dev,
1694 "(%s) set completed xfer complete\n",
1695 __func__);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001696 atmci_set_completed(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001697
Ludovic Desroches077d4072012-07-24 11:42:04 +02001698 if (host->caps.need_notbusy_for_read_ops ||
1699 (host->data->flags & MMC_DATA_WRITE)) {
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001700 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1701 state = STATE_WAITING_NOTBUSY;
1702 } else if (host->mrq->stop) {
1703 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
1704 atmci_send_stop_cmd(host, data);
1705 state = STATE_SENDING_STOP;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001706 } else {
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001707 host->data = NULL;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001708 data->bytes_xfered = data->blocks * data->blksz;
1709 data->error = 0;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001710 state = STATE_END_REQUEST;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001711 }
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001712 break;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001713
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001714 case STATE_WAITING_NOTBUSY:
1715 /*
1716 * We can be in the state for two reasons: a command
1717 * requiring waiting not busy signal (stop command
1718 * included) or a write operation. In the latest case,
1719 * we need to send a stop command.
1720 */
Ludovic Desroches6801c412012-05-16 15:26:01 +02001721 dev_dbg(&host->pdev->dev, "FSM: not busy?\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001722 if (!atmci_test_and_clear_pending(host,
1723 EVENT_NOTBUSY))
1724 break;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001725
Ludovic Desroches6801c412012-05-16 15:26:01 +02001726 dev_dbg(&host->pdev->dev, "set completed not busy\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001727 atmci_set_completed(host, EVENT_NOTBUSY);
1728
1729 if (host->data) {
1730 /*
1731 * For some commands such as CMD53, even if
1732 * there is data transfer, there is no stop
1733 * command to send.
1734 */
1735 if (host->mrq->stop) {
1736 atmci_writel(host, ATMCI_IER,
1737 ATMCI_CMDRDY);
1738 atmci_send_stop_cmd(host, data);
1739 state = STATE_SENDING_STOP;
1740 } else {
1741 host->data = NULL;
1742 data->bytes_xfered = data->blocks
1743 * data->blksz;
1744 data->error = 0;
1745 state = STATE_END_REQUEST;
1746 }
1747 } else
1748 state = STATE_END_REQUEST;
1749 break;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001750
1751 case STATE_SENDING_STOP:
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001752 /*
1753 * In this state, it is important to set host->data to
1754 * NULL (which is tested in the waiting notbusy state)
1755 * in order to go to the end request state instead of
1756 * sending stop again.
1757 */
Ludovic Desroches6801c412012-05-16 15:26:01 +02001758 dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001759 if (!atmci_test_and_clear_pending(host,
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001760 EVENT_CMD_RDY))
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001761 break;
1762
Ludovic Desroches6801c412012-05-16 15:26:01 +02001763 dev_dbg(&host->pdev->dev, "FSM: cmd ready\n");
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001764 host->cmd = NULL;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001765 data->bytes_xfered = data->blocks * data->blksz;
1766 data->error = 0;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001767 atmci_command_complete(host, mrq->stop);
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001768 if (mrq->stop->error) {
1769 host->stop_transfer(host);
1770 atmci_writel(host, ATMCI_IDR,
1771 ATMCI_TXRDY | ATMCI_RXRDY
1772 | ATMCI_DATA_ERROR_FLAGS);
1773 state = STATE_END_REQUEST;
1774 } else {
1775 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1776 state = STATE_WAITING_NOTBUSY;
1777 }
Nicolas Ferre41b4e9a2012-07-06 11:58:33 +02001778 host->data = NULL;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001779 break;
1780
1781 case STATE_END_REQUEST:
1782 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY | ATMCI_RXRDY
1783 | ATMCI_DATA_ERROR_FLAGS);
1784 status = host->data_status;
1785 if (unlikely(status)) {
1786 host->stop_transfer(host);
1787 host->data = NULL;
1788 if (status & ATMCI_DTOE) {
1789 data->error = -ETIMEDOUT;
1790 } else if (status & ATMCI_DCRCE) {
1791 data->error = -EILSEQ;
1792 } else {
1793 data->error = -EIO;
1794 }
1795 }
1796
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001797 atmci_request_end(host, host->mrq);
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001798 state = STATE_IDLE;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001799 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001800 }
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001801 } while (state != prev_state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001802
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001803 host->state = state;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001804
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001805 spin_unlock(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001806}
1807
1808static void atmci_read_data_pio(struct atmel_mci *host)
1809{
1810 struct scatterlist *sg = host->sg;
1811 void *buf = sg_virt(sg);
1812 unsigned int offset = host->pio_offset;
1813 struct mmc_data *data = host->data;
1814 u32 value;
1815 u32 status;
1816 unsigned int nbytes = 0;
1817
1818 do {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001819 value = atmci_readl(host, ATMCI_RDR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001820 if (likely(offset + 4 <= sg->length)) {
1821 put_unaligned(value, (u32 *)(buf + offset));
1822
1823 offset += 4;
1824 nbytes += 4;
1825
1826 if (offset == sg->length) {
Haavard Skinnemoen5e7184a2008-10-05 15:27:50 +02001827 flush_dcache_page(sg_page(sg));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001828 host->sg = sg = sg_next(sg);
1829 if (!sg)
1830 goto done;
1831
1832 offset = 0;
1833 buf = sg_virt(sg);
1834 }
1835 } else {
1836 unsigned int remaining = sg->length - offset;
1837 memcpy(buf + offset, &value, remaining);
1838 nbytes += remaining;
1839
1840 flush_dcache_page(sg_page(sg));
1841 host->sg = sg = sg_next(sg);
1842 if (!sg)
1843 goto done;
1844
1845 offset = 4 - remaining;
1846 buf = sg_virt(sg);
1847 memcpy(buf, (u8 *)&value + remaining, offset);
1848 nbytes += offset;
1849 }
1850
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001851 status = atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001852 if (status & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001853 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001854 | ATMCI_DATA_ERROR_FLAGS));
1855 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001856 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001857 return;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001858 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001859 } while (status & ATMCI_RXRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001860
1861 host->pio_offset = offset;
1862 data->bytes_xfered += nbytes;
1863
1864 return;
1865
1866done:
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001867 atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
1868 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001869 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001870 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001871 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001872}
1873
1874static void atmci_write_data_pio(struct atmel_mci *host)
1875{
1876 struct scatterlist *sg = host->sg;
1877 void *buf = sg_virt(sg);
1878 unsigned int offset = host->pio_offset;
1879 struct mmc_data *data = host->data;
1880 u32 value;
1881 u32 status;
1882 unsigned int nbytes = 0;
1883
1884 do {
1885 if (likely(offset + 4 <= sg->length)) {
1886 value = get_unaligned((u32 *)(buf + offset));
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001887 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001888
1889 offset += 4;
1890 nbytes += 4;
1891 if (offset == sg->length) {
1892 host->sg = sg = sg_next(sg);
1893 if (!sg)
1894 goto done;
1895
1896 offset = 0;
1897 buf = sg_virt(sg);
1898 }
1899 } else {
1900 unsigned int remaining = sg->length - offset;
1901
1902 value = 0;
1903 memcpy(&value, buf + offset, remaining);
1904 nbytes += remaining;
1905
1906 host->sg = sg = sg_next(sg);
1907 if (!sg) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001908 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001909 goto done;
1910 }
1911
1912 offset = 4 - remaining;
1913 buf = sg_virt(sg);
1914 memcpy((u8 *)&value + remaining, buf, offset);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001915 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001916 nbytes += offset;
1917 }
1918
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001919 status = atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001920 if (status & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001921 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001922 | ATMCI_DATA_ERROR_FLAGS));
1923 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001924 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001925 return;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001926 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001927 } while (status & ATMCI_TXRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001928
1929 host->pio_offset = offset;
1930 data->bytes_xfered += nbytes;
1931
1932 return;
1933
1934done:
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001935 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
1936 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001937 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001938 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001939 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001940}
1941
Anders Grahn88ff82e2010-05-26 14:42:01 -07001942static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
1943{
1944 int i;
1945
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001946 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Anders Grahn88ff82e2010-05-26 14:42:01 -07001947 struct atmel_mci_slot *slot = host->slot[i];
1948 if (slot && (status & slot->sdio_irq)) {
1949 mmc_signal_sdio_irq(slot->mmc);
1950 }
1951 }
1952}
1953
1954
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001955static irqreturn_t atmci_interrupt(int irq, void *dev_id)
1956{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001957 struct atmel_mci *host = dev_id;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001958 u32 status, mask, pending;
1959 unsigned int pass_count = 0;
1960
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001961 do {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001962 status = atmci_readl(host, ATMCI_SR);
1963 mask = atmci_readl(host, ATMCI_IMR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001964 pending = status & mask;
1965 if (!pending)
1966 break;
1967
1968 if (pending & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001969 dev_dbg(&host->pdev->dev, "IRQ: data error\n");
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001970 atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001971 | ATMCI_RXRDY | ATMCI_TXRDY
1972 | ATMCI_ENDRX | ATMCI_ENDTX
1973 | ATMCI_RXBUFF | ATMCI_TXBUFE);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001974
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001975 host->data_status = status;
Ludovic Desroches6801c412012-05-16 15:26:01 +02001976 dev_dbg(&host->pdev->dev, "set pending data error\n");
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001977 smp_wmb();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001978 atmci_set_pending(host, EVENT_DATA_ERROR);
1979 tasklet_schedule(&host->tasklet);
1980 }
Ludovic Desroches796211b2011-08-11 15:25:44 +00001981
Ludovic Desroches796211b2011-08-11 15:25:44 +00001982 if (pending & ATMCI_TXBUFE) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001983 dev_dbg(&host->pdev->dev, "IRQ: tx buffer empty\n");
Ludovic Desroches796211b2011-08-11 15:25:44 +00001984 atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001985 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001986 /*
1987 * We can receive this interruption before having configured
1988 * the second pdc buffer, so we need to reconfigure first and
1989 * second buffers again
1990 */
1991 if (host->data_size) {
1992 atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001993 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001994 atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
1995 } else {
1996 atmci_pdc_complete(host);
1997 }
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00001998 } else if (pending & ATMCI_ENDTX) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001999 dev_dbg(&host->pdev->dev, "IRQ: end of tx buffer\n");
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00002000 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
2001
2002 if (host->data_size) {
2003 atmci_pdc_set_single_buf(host,
2004 XFER_TRANSMIT, PDC_SECOND_BUF);
2005 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
2006 }
Ludovic Desroches796211b2011-08-11 15:25:44 +00002007 }
2008
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00002009 if (pending & ATMCI_RXBUFF) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002010 dev_dbg(&host->pdev->dev, "IRQ: rx buffer full\n");
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00002011 atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
2012 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
2013 /*
2014 * We can receive this interruption before having configured
2015 * the second pdc buffer, so we need to reconfigure first and
2016 * second buffers again
2017 */
2018 if (host->data_size) {
2019 atmci_pdc_set_both_buf(host, XFER_RECEIVE);
2020 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
2021 atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
2022 } else {
2023 atmci_pdc_complete(host);
2024 }
2025 } else if (pending & ATMCI_ENDRX) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002026 dev_dbg(&host->pdev->dev, "IRQ: end of rx buffer\n");
Ludovic Desroches796211b2011-08-11 15:25:44 +00002027 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
2028
2029 if (host->data_size) {
2030 atmci_pdc_set_single_buf(host,
2031 XFER_RECEIVE, PDC_SECOND_BUF);
2032 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
2033 }
2034 }
2035
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002036 /*
2037 * First mci IPs, so mainly the ones having pdc, have some
2038 * issues with the notbusy signal. You can't get it after
2039 * data transmission if you have not sent a stop command.
2040 * The appropriate workaround is to use the BLKE signal.
2041 */
2042 if (pending & ATMCI_BLKE) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002043 dev_dbg(&host->pdev->dev, "IRQ: blke\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002044 atmci_writel(host, ATMCI_IDR, ATMCI_BLKE);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002045 smp_wmb();
Ludovic Desroches6801c412012-05-16 15:26:01 +02002046 dev_dbg(&host->pdev->dev, "set pending notbusy\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002047 atmci_set_pending(host, EVENT_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002048 tasklet_schedule(&host->tasklet);
2049 }
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002050
2051 if (pending & ATMCI_NOTBUSY) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002052 dev_dbg(&host->pdev->dev, "IRQ: not_busy\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002053 atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY);
2054 smp_wmb();
Ludovic Desroches6801c412012-05-16 15:26:01 +02002055 dev_dbg(&host->pdev->dev, "set pending notbusy\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002056 atmci_set_pending(host, EVENT_NOTBUSY);
2057 tasklet_schedule(&host->tasklet);
2058 }
2059
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002060 if (pending & ATMCI_RXRDY)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002061 atmci_read_data_pio(host);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002062 if (pending & ATMCI_TXRDY)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002063 atmci_write_data_pio(host);
2064
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002065 if (pending & ATMCI_CMDRDY) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002066 dev_dbg(&host->pdev->dev, "IRQ: cmd ready\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002067 atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
2068 host->cmd_status = status;
2069 smp_wmb();
Ludovic Desroches6801c412012-05-16 15:26:01 +02002070 dev_dbg(&host->pdev->dev, "set pending cmd rdy\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002071 atmci_set_pending(host, EVENT_CMD_RDY);
2072 tasklet_schedule(&host->tasklet);
2073 }
Anders Grahn88ff82e2010-05-26 14:42:01 -07002074
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002075 if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
Anders Grahn88ff82e2010-05-26 14:42:01 -07002076 atmci_sdio_interrupt(host, status);
2077
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002078 } while (pass_count++ < 5);
2079
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002080 return pass_count ? IRQ_HANDLED : IRQ_NONE;
2081}
2082
2083static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
2084{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002085 struct atmel_mci_slot *slot = dev_id;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002086
2087 /*
2088 * Disable interrupts until the pin has stabilized and check
2089 * the state then. Use mod_timer() since we may be in the
2090 * middle of the timer routine when this interrupt triggers.
2091 */
2092 disable_irq_nosync(irq);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002093 mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002094
2095 return IRQ_HANDLED;
2096}
2097
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002098static int __init atmci_init_slot(struct atmel_mci *host,
2099 struct mci_slot_pdata *slot_data, unsigned int id,
Anders Grahn88ff82e2010-05-26 14:42:01 -07002100 u32 sdc_reg, u32 sdio_irq)
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002101{
2102 struct mmc_host *mmc;
2103 struct atmel_mci_slot *slot;
2104
2105 mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
2106 if (!mmc)
2107 return -ENOMEM;
2108
2109 slot = mmc_priv(mmc);
2110 slot->mmc = mmc;
2111 slot->host = host;
2112 slot->detect_pin = slot_data->detect_pin;
2113 slot->wp_pin = slot_data->wp_pin;
Jonas Larsson1c1452b2009-03-31 11:16:48 +02002114 slot->detect_is_active_high = slot_data->detect_is_active_high;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002115 slot->sdc_reg = sdc_reg;
Anders Grahn88ff82e2010-05-26 14:42:01 -07002116 slot->sdio_irq = sdio_irq;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002117
Ludovic Desrochese919fd22012-07-24 15:30:03 +02002118 dev_dbg(&mmc->class_dev,
2119 "slot[%u]: bus_width=%u, detect_pin=%d, "
2120 "detect_is_active_high=%s, wp_pin=%d\n",
2121 id, slot_data->bus_width, slot_data->detect_pin,
2122 slot_data->detect_is_active_high ? "true" : "false",
2123 slot_data->wp_pin);
2124
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002125 mmc->ops = &atmci_ops;
2126 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
2127 mmc->f_max = host->bus_hz / 2;
2128 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Anders Grahn88ff82e2010-05-26 14:42:01 -07002129 if (sdio_irq)
2130 mmc->caps |= MMC_CAP_SDIO_IRQ;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002131 if (host->caps.has_highspeed)
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07002132 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002133 /*
2134 * Without the read/write proof capability, it is strongly suggested to
2135 * use only one bit for data to prevent fifo underruns and overruns
2136 * which will corrupt data.
2137 */
2138 if ((slot_data->bus_width >= 4) && host->caps.has_rwproof)
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002139 mmc->caps |= MMC_CAP_4_BIT_DATA;
2140
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002141 if (atmci_get_version(host) < 0x200) {
2142 mmc->max_segs = 256;
2143 mmc->max_blk_size = 4095;
2144 mmc->max_blk_count = 256;
2145 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2146 mmc->max_seg_size = mmc->max_blk_size * mmc->max_segs;
2147 } else {
2148 mmc->max_segs = 64;
2149 mmc->max_req_size = 32768 * 512;
2150 mmc->max_blk_size = 32768;
2151 mmc->max_blk_count = 512;
2152 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002153
2154 /* Assume card is present initially */
2155 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
2156 if (gpio_is_valid(slot->detect_pin)) {
2157 if (gpio_request(slot->detect_pin, "mmc_detect")) {
2158 dev_dbg(&mmc->class_dev, "no detect pin available\n");
2159 slot->detect_pin = -EBUSY;
Jonas Larsson1c1452b2009-03-31 11:16:48 +02002160 } else if (gpio_get_value(slot->detect_pin) ^
2161 slot->detect_is_active_high) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002162 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
2163 }
2164 }
2165
2166 if (!gpio_is_valid(slot->detect_pin))
2167 mmc->caps |= MMC_CAP_NEEDS_POLL;
2168
2169 if (gpio_is_valid(slot->wp_pin)) {
2170 if (gpio_request(slot->wp_pin, "mmc_wp")) {
2171 dev_dbg(&mmc->class_dev, "no WP pin available\n");
2172 slot->wp_pin = -EBUSY;
2173 }
2174 }
2175
2176 host->slot[id] = slot;
2177 mmc_add_host(mmc);
2178
2179 if (gpio_is_valid(slot->detect_pin)) {
2180 int ret;
2181
2182 setup_timer(&slot->detect_timer, atmci_detect_change,
2183 (unsigned long)slot);
2184
2185 ret = request_irq(gpio_to_irq(slot->detect_pin),
2186 atmci_detect_interrupt,
2187 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
2188 "mmc-detect", slot);
2189 if (ret) {
2190 dev_dbg(&mmc->class_dev,
2191 "could not request IRQ %d for detect pin\n",
2192 gpio_to_irq(slot->detect_pin));
2193 gpio_free(slot->detect_pin);
2194 slot->detect_pin = -EBUSY;
2195 }
2196 }
2197
2198 atmci_init_debugfs(slot);
2199
2200 return 0;
2201}
2202
2203static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot,
2204 unsigned int id)
2205{
2206 /* Debugfs stuff is cleaned up by mmc core */
2207
2208 set_bit(ATMCI_SHUTDOWN, &slot->flags);
2209 smp_wmb();
2210
2211 mmc_remove_host(slot->mmc);
2212
2213 if (gpio_is_valid(slot->detect_pin)) {
2214 int pin = slot->detect_pin;
2215
2216 free_irq(gpio_to_irq(pin), slot);
2217 del_timer_sync(&slot->detect_timer);
2218 gpio_free(pin);
2219 }
2220 if (gpio_is_valid(slot->wp_pin))
2221 gpio_free(slot->wp_pin);
2222
2223 slot->host->slot[id] = NULL;
2224 mmc_free_host(slot->mmc);
2225}
2226
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002227static bool atmci_filter(struct dma_chan *chan, void *slave)
Dan Williams74465b42009-01-06 11:38:16 -07002228{
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002229 struct mci_dma_data *sl = slave;
Dan Williams74465b42009-01-06 11:38:16 -07002230
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002231 if (sl && find_slave_dev(sl) == chan->device->dev) {
2232 chan->private = slave_data_ptr(sl);
Dan Williams7dd60252009-01-06 11:38:19 -07002233 return true;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002234 } else {
Dan Williams7dd60252009-01-06 11:38:19 -07002235 return false;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002236 }
Dan Williams74465b42009-01-06 11:38:16 -07002237}
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002238
Ludovic Desrochesef878192012-02-09 16:33:53 +01002239static bool atmci_configure_dma(struct atmel_mci *host)
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002240{
2241 struct mci_platform_data *pdata;
2242
2243 if (host == NULL)
Ludovic Desrochesef878192012-02-09 16:33:53 +01002244 return false;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002245
2246 pdata = host->pdev->dev.platform_data;
2247
Hein_Tiboschccdfe612012-08-30 16:34:38 +00002248 if (!pdata)
2249 return false;
2250
2251 if (pdata->dma_slave && find_slave_dev(pdata->dma_slave)) {
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002252 dma_cap_mask_t mask;
2253
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002254 /* Try to grab a DMA channel */
2255 dma_cap_zero(mask);
2256 dma_cap_set(DMA_SLAVE, mask);
2257 host->dma.chan =
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002258 dma_request_channel(mask, atmci_filter, pdata->dma_slave);
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002259 }
Ludovic Desrochesef878192012-02-09 16:33:53 +01002260 if (!host->dma.chan) {
2261 dev_warn(&host->pdev->dev, "no DMA channel available\n");
2262 return false;
2263 } else {
Nicolas Ferre74791a22009-12-14 18:01:31 -08002264 dev_info(&host->pdev->dev,
Ludovic Desrochesb81cfc42012-02-09 16:33:54 +01002265 "using %s for DMA transfers\n",
Nicolas Ferre74791a22009-12-14 18:01:31 -08002266 dma_chan_name(host->dma.chan));
Viresh Kumare2b35f32012-02-01 16:12:27 +05302267
2268 host->dma_conf.src_addr = host->mapbase + ATMCI_RDR;
2269 host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2270 host->dma_conf.src_maxburst = 1;
2271 host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR;
2272 host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2273 host->dma_conf.dst_maxburst = 1;
2274 host->dma_conf.device_fc = false;
Ludovic Desrochesef878192012-02-09 16:33:53 +01002275 return true;
2276 }
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002277}
Ludovic Desroches796211b2011-08-11 15:25:44 +00002278
Ludovic Desroches796211b2011-08-11 15:25:44 +00002279/*
2280 * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
2281 * HSMCI provides DMA support and a new config register but no more supports
2282 * PDC.
2283 */
2284static void __init atmci_get_cap(struct atmel_mci *host)
2285{
2286 unsigned int version;
2287
2288 version = atmci_get_version(host);
2289 dev_info(&host->pdev->dev,
2290 "version: 0x%x\n", version);
2291
Hein_Tiboschccdfe612012-08-30 16:34:38 +00002292 host->caps.has_dma_conf_reg = 0;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002293 host->caps.has_pdc = 1;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002294 host->caps.has_cfg_reg = 0;
2295 host->caps.has_cstor_reg = 0;
2296 host->caps.has_highspeed = 0;
2297 host->caps.has_rwproof = 0;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002298 host->caps.has_odd_clk_div = 0;
Ludovic Desroches24011f32012-05-16 15:26:00 +02002299 host->caps.has_bad_data_ordering = 1;
2300 host->caps.need_reset_after_xfer = 1;
2301 host->caps.need_blksz_mul_4 = 1;
Ludovic Desroches077d4072012-07-24 11:42:04 +02002302 host->caps.need_notbusy_for_read_ops = 0;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002303
2304 /* keep only major version number */
2305 switch (version & 0xf00) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00002306 case 0x500:
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002307 host->caps.has_odd_clk_div = 1;
2308 case 0x400:
2309 case 0x300:
Hein_Tiboschccdfe612012-08-30 16:34:38 +00002310 host->caps.has_dma_conf_reg = 1;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002311 host->caps.has_pdc = 0;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002312 host->caps.has_cfg_reg = 1;
2313 host->caps.has_cstor_reg = 1;
2314 host->caps.has_highspeed = 1;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002315 case 0x200:
Ludovic Desroches796211b2011-08-11 15:25:44 +00002316 host->caps.has_rwproof = 1;
Ludovic Desroches24011f32012-05-16 15:26:00 +02002317 host->caps.need_blksz_mul_4 = 0;
Ludovic Desroches077d4072012-07-24 11:42:04 +02002318 host->caps.need_notbusy_for_read_ops = 1;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002319 case 0x100:
Ludovic Desroches24011f32012-05-16 15:26:00 +02002320 host->caps.has_bad_data_ordering = 0;
2321 host->caps.need_reset_after_xfer = 0;
2322 case 0x0:
Ludovic Desroches796211b2011-08-11 15:25:44 +00002323 break;
2324 default:
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002325 host->caps.has_pdc = 0;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002326 dev_warn(&host->pdev->dev,
2327 "Unmanaged mci version, set minimum capabilities\n");
2328 break;
2329 }
2330}
Dan Williams74465b42009-01-06 11:38:16 -07002331
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002332static int __init atmci_probe(struct platform_device *pdev)
2333{
2334 struct mci_platform_data *pdata;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002335 struct atmel_mci *host;
2336 struct resource *regs;
2337 unsigned int nr_slots;
2338 int irq;
2339 int ret;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002340
2341 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2342 if (!regs)
2343 return -ENXIO;
2344 pdata = pdev->dev.platform_data;
Ludovic Desrochese919fd22012-07-24 15:30:03 +02002345 if (!pdata) {
2346 pdata = atmci_of_init(pdev);
2347 if (IS_ERR(pdata)) {
2348 dev_err(&pdev->dev, "platform data not available\n");
2349 return PTR_ERR(pdata);
2350 }
2351 }
2352
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002353 irq = platform_get_irq(pdev, 0);
2354 if (irq < 0)
2355 return irq;
2356
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002357 host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL);
2358 if (!host)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002359 return -ENOMEM;
2360
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002361 host->pdev = pdev;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002362 spin_lock_init(&host->lock);
2363 INIT_LIST_HEAD(&host->queue);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002364
2365 host->mck = clk_get(&pdev->dev, "mci_clk");
2366 if (IS_ERR(host->mck)) {
2367 ret = PTR_ERR(host->mck);
2368 goto err_clk_get;
2369 }
2370
2371 ret = -ENOMEM;
H Hartley Sweetene8e3f6c2009-12-14 14:11:56 -05002372 host->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002373 if (!host->regs)
2374 goto err_ioremap;
2375
2376 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002377 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002378 host->bus_hz = clk_get_rate(host->mck);
2379 clk_disable(host->mck);
2380
2381 host->mapbase = regs->start;
2382
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002383 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002384
Kay Sievers89c8aa22009-02-02 21:08:30 +01002385 ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002386 if (ret)
2387 goto err_request_irq;
2388
Ludovic Desroches796211b2011-08-11 15:25:44 +00002389 /* Get MCI capabilities and set operations according to it */
2390 atmci_get_cap(host);
Hein_Tiboschccdfe612012-08-30 16:34:38 +00002391 if (atmci_configure_dma(host)) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00002392 host->prepare_data = &atmci_prepare_data_dma;
2393 host->submit_data = &atmci_submit_data_dma;
2394 host->stop_transfer = &atmci_stop_transfer_dma;
2395 } else if (host->caps.has_pdc) {
2396 dev_info(&pdev->dev, "using PDC\n");
2397 host->prepare_data = &atmci_prepare_data_pdc;
2398 host->submit_data = &atmci_submit_data_pdc;
2399 host->stop_transfer = &atmci_stop_transfer_pdc;
2400 } else {
Ludovic Desrochesef878192012-02-09 16:33:53 +01002401 dev_info(&pdev->dev, "using PIO\n");
Ludovic Desroches796211b2011-08-11 15:25:44 +00002402 host->prepare_data = &atmci_prepare_data;
2403 host->submit_data = &atmci_submit_data;
2404 host->stop_transfer = &atmci_stop_transfer;
2405 }
2406
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002407 platform_set_drvdata(pdev, host);
2408
Ludovic Desrochesb87cc1b2012-05-23 15:52:15 +02002409 setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host);
2410
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002411 /* We need at least one slot to succeed */
2412 nr_slots = 0;
2413 ret = -ENODEV;
2414 if (pdata->slot[0].bus_width) {
2415 ret = atmci_init_slot(host, &pdata->slot[0],
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002416 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002417 if (!ret) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002418 nr_slots++;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002419 host->buf_size = host->slot[0]->mmc->max_req_size;
2420 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002421 }
2422 if (pdata->slot[1].bus_width) {
2423 ret = atmci_init_slot(host, &pdata->slot[1],
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002424 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002425 if (!ret) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002426 nr_slots++;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002427 if (host->slot[1]->mmc->max_req_size > host->buf_size)
2428 host->buf_size =
2429 host->slot[1]->mmc->max_req_size;
2430 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002431 }
2432
Rob Emanuele04d699c2009-09-22 16:45:19 -07002433 if (!nr_slots) {
2434 dev_err(&pdev->dev, "init failed: no slot defined\n");
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002435 goto err_init_slot;
Rob Emanuele04d699c2009-09-22 16:45:19 -07002436 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002437
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002438 if (!host->caps.has_rwproof) {
2439 host->buffer = dma_alloc_coherent(&pdev->dev, host->buf_size,
2440 &host->buf_phys_addr,
2441 GFP_KERNEL);
2442 if (!host->buffer) {
2443 ret = -ENOMEM;
2444 dev_err(&pdev->dev, "buffer allocation failed\n");
2445 goto err_init_slot;
2446 }
2447 }
2448
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002449 dev_info(&pdev->dev,
2450 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
2451 host->mapbase, irq, nr_slots);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +02002452
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002453 return 0;
2454
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002455err_init_slot:
Dan Williams74465b42009-01-06 11:38:16 -07002456 if (host->dma.chan)
2457 dma_release_channel(host->dma.chan);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002458 free_irq(irq, host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002459err_request_irq:
2460 iounmap(host->regs);
2461err_ioremap:
2462 clk_put(host->mck);
2463err_clk_get:
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002464 kfree(host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002465 return ret;
2466}
2467
2468static int __exit atmci_remove(struct platform_device *pdev)
2469{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002470 struct atmel_mci *host = platform_get_drvdata(pdev);
2471 unsigned int i;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002472
2473 platform_set_drvdata(pdev, NULL);
2474
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002475 if (host->buffer)
2476 dma_free_coherent(&pdev->dev, host->buf_size,
2477 host->buffer, host->buf_phys_addr);
2478
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002479 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002480 if (host->slot[i])
2481 atmci_cleanup_slot(host->slot[i], i);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002482 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002483
2484 clk_enable(host->mck);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002485 atmci_writel(host, ATMCI_IDR, ~0UL);
2486 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
2487 atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002488 clk_disable(host->mck);
2489
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02002490#ifdef CONFIG_MMC_ATMELMCI_DMA
Dan Williams74465b42009-01-06 11:38:16 -07002491 if (host->dma.chan)
2492 dma_release_channel(host->dma.chan);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02002493#endif
2494
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002495 free_irq(platform_get_irq(pdev, 0), host);
2496 iounmap(host->regs);
2497
2498 clk_put(host->mck);
2499 kfree(host);
2500
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002501 return 0;
2502}
2503
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002504#ifdef CONFIG_PM
2505static int atmci_suspend(struct device *dev)
2506{
2507 struct atmel_mci *host = dev_get_drvdata(dev);
2508 int i;
2509
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002510 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002511 struct atmel_mci_slot *slot = host->slot[i];
2512 int ret;
2513
2514 if (!slot)
2515 continue;
2516 ret = mmc_suspend_host(slot->mmc);
2517 if (ret < 0) {
2518 while (--i >= 0) {
2519 slot = host->slot[i];
2520 if (slot
2521 && test_bit(ATMCI_SUSPENDED, &slot->flags)) {
2522 mmc_resume_host(host->slot[i]->mmc);
2523 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2524 }
2525 }
2526 return ret;
2527 } else {
2528 set_bit(ATMCI_SUSPENDED, &slot->flags);
2529 }
2530 }
2531
2532 return 0;
2533}
2534
2535static int atmci_resume(struct device *dev)
2536{
2537 struct atmel_mci *host = dev_get_drvdata(dev);
2538 int i;
2539 int ret = 0;
2540
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002541 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002542 struct atmel_mci_slot *slot = host->slot[i];
2543 int err;
2544
2545 slot = host->slot[i];
2546 if (!slot)
2547 continue;
2548 if (!test_bit(ATMCI_SUSPENDED, &slot->flags))
2549 continue;
2550 err = mmc_resume_host(slot->mmc);
2551 if (err < 0)
2552 ret = err;
2553 else
2554 clear_bit(ATMCI_SUSPENDED, &slot->flags);
2555 }
2556
2557 return ret;
2558}
2559static SIMPLE_DEV_PM_OPS(atmci_pm, atmci_suspend, atmci_resume);
2560#define ATMCI_PM_OPS (&atmci_pm)
2561#else
2562#define ATMCI_PM_OPS NULL
2563#endif
2564
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002565static struct platform_driver atmci_driver = {
2566 .remove = __exit_p(atmci_remove),
2567 .driver = {
2568 .name = "atmel_mci",
Nicolas Ferre5c2f2b92011-07-06 11:31:36 +02002569 .pm = ATMCI_PM_OPS,
Ludovic Desrochese919fd22012-07-24 15:30:03 +02002570 .of_match_table = of_match_ptr(atmci_dt_ids),
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002571 },
2572};
2573
2574static int __init atmci_init(void)
2575{
2576 return platform_driver_probe(&atmci_driver, atmci_probe);
2577}
2578
2579static void __exit atmci_exit(void)
2580{
2581 platform_driver_unregister(&atmci_driver);
2582}
2583
Dan Williams74465b42009-01-06 11:38:16 -07002584late_initcall(atmci_init); /* try to load after dma driver when built-in */
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002585module_exit(atmci_exit);
2586
2587MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002588MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002589MODULE_LICENSE("GPL v2");