blob: 9f3bb611000fef17fa983f7409e30561fcd79700 [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>
Pramod Gurav7bca6462014-09-23 18:21:48 +053020#include <linux/io.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020021#include <linux/ioport.h>
22#include <linux/module.h>
Ludovic Desrochese919fd22012-07-24 15:30:03 +020023#include <linux/of.h>
24#include <linux/of_device.h>
25#include <linux/of_gpio.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020026#include <linux/platform_device.h>
27#include <linux/scatterlist.h>
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +020028#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +020030#include <linux/stat.h>
Viresh Kumare2b35f32012-02-01 16:12:27 +053031#include <linux/types.h>
Alexandre Belloni9cbef732014-10-17 10:26:36 +020032#include <linux/platform_data/mmc-atmel-mci.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020033
34#include <linux/mmc/host.h>
Nicolas Ferre2f1d7912010-12-10 19:14:32 +010035#include <linux/mmc/sdio.h>
Nicolas Ferre2635d1b2009-12-14 18:01:30 -080036
Nicolas Ferrec42aa772008-11-20 15:59:12 +010037#include <linux/atmel-mci.h>
Ludovic Desroches796211b2011-08-11 15:25:44 +000038#include <linux/atmel_pdc.h>
Wenyou Yangae552ab2014-10-30 12:00:41 +080039#include <linux/pm.h>
40#include <linux/pm_runtime.h>
Wenyou Yangb5b64fa2014-11-07 08:48:13 +080041#include <linux/pinctrl/consumer.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020042
Arnd Bergmannbf614c72014-06-05 23:14:38 +020043#include <asm/cacheflush.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020044#include <asm/io.h>
45#include <asm/unaligned.h>
46
ludovic.desroches@atmel.comec8fc9c2015-11-23 16:27:30 +010047/*
48 * Superset of MCI IP registers integrated in Atmel AVR32 and AT91 Processors
49 * Registers and bitfields marked with [2] are only available in MCI2
50 */
51
52/* MCI Register Definitions */
53#define ATMCI_CR 0x0000 /* Control */
54#define ATMCI_CR_MCIEN BIT(0) /* MCI Enable */
55#define ATMCI_CR_MCIDIS BIT(1) /* MCI Disable */
56#define ATMCI_CR_PWSEN BIT(2) /* Power Save Enable */
57#define ATMCI_CR_PWSDIS BIT(3) /* Power Save Disable */
58#define ATMCI_CR_SWRST BIT(7) /* Software Reset */
59#define ATMCI_MR 0x0004 /* Mode */
60#define ATMCI_MR_CLKDIV(x) ((x) << 0) /* Clock Divider */
61#define ATMCI_MR_PWSDIV(x) ((x) << 8) /* Power Saving Divider */
62#define ATMCI_MR_RDPROOF BIT(11) /* Read Proof */
63#define ATMCI_MR_WRPROOF BIT(12) /* Write Proof */
64#define ATMCI_MR_PDCFBYTE BIT(13) /* Force Byte Transfer */
65#define ATMCI_MR_PDCPADV BIT(14) /* Padding Value */
66#define ATMCI_MR_PDCMODE BIT(15) /* PDC-oriented Mode */
67#define ATMCI_MR_CLKODD(x) ((x) << 16) /* LSB of Clock Divider */
68#define ATMCI_DTOR 0x0008 /* Data Timeout */
69#define ATMCI_DTOCYC(x) ((x) << 0) /* Data Timeout Cycles */
70#define ATMCI_DTOMUL(x) ((x) << 4) /* Data Timeout Multiplier */
71#define ATMCI_SDCR 0x000c /* SD Card / SDIO */
72#define ATMCI_SDCSEL_SLOT_A (0 << 0) /* Select SD slot A */
73#define ATMCI_SDCSEL_SLOT_B (1 << 0) /* Select SD slot A */
74#define ATMCI_SDCSEL_MASK (3 << 0)
75#define ATMCI_SDCBUS_1BIT (0 << 6) /* 1-bit data bus */
76#define ATMCI_SDCBUS_4BIT (2 << 6) /* 4-bit data bus */
77#define ATMCI_SDCBUS_8BIT (3 << 6) /* 8-bit data bus[2] */
78#define ATMCI_SDCBUS_MASK (3 << 6)
79#define ATMCI_ARGR 0x0010 /* Command Argument */
80#define ATMCI_CMDR 0x0014 /* Command */
81#define ATMCI_CMDR_CMDNB(x) ((x) << 0) /* Command Opcode */
82#define ATMCI_CMDR_RSPTYP_NONE (0 << 6) /* No response */
83#define ATMCI_CMDR_RSPTYP_48BIT (1 << 6) /* 48-bit response */
84#define ATMCI_CMDR_RSPTYP_136BIT (2 << 6) /* 136-bit response */
85#define ATMCI_CMDR_SPCMD_INIT (1 << 8) /* Initialization command */
86#define ATMCI_CMDR_SPCMD_SYNC (2 << 8) /* Synchronized command */
87#define ATMCI_CMDR_SPCMD_INT (4 << 8) /* Interrupt command */
88#define ATMCI_CMDR_SPCMD_INTRESP (5 << 8) /* Interrupt response */
89#define ATMCI_CMDR_OPDCMD (1 << 11) /* Open Drain */
90#define ATMCI_CMDR_MAXLAT_5CYC (0 << 12) /* Max latency 5 cycles */
91#define ATMCI_CMDR_MAXLAT_64CYC (1 << 12) /* Max latency 64 cycles */
92#define ATMCI_CMDR_START_XFER (1 << 16) /* Start data transfer */
93#define ATMCI_CMDR_STOP_XFER (2 << 16) /* Stop data transfer */
94#define ATMCI_CMDR_TRDIR_WRITE (0 << 18) /* Write data */
95#define ATMCI_CMDR_TRDIR_READ (1 << 18) /* Read data */
96#define ATMCI_CMDR_BLOCK (0 << 19) /* Single-block transfer */
97#define ATMCI_CMDR_MULTI_BLOCK (1 << 19) /* Multi-block transfer */
98#define ATMCI_CMDR_STREAM (2 << 19) /* MMC Stream transfer */
99#define ATMCI_CMDR_SDIO_BYTE (4 << 19) /* SDIO Byte transfer */
100#define ATMCI_CMDR_SDIO_BLOCK (5 << 19) /* SDIO Block transfer */
101#define ATMCI_CMDR_SDIO_SUSPEND (1 << 24) /* SDIO Suspend Command */
102#define ATMCI_CMDR_SDIO_RESUME (2 << 24) /* SDIO Resume Command */
103#define ATMCI_BLKR 0x0018 /* Block */
104#define ATMCI_BCNT(x) ((x) << 0) /* Data Block Count */
105#define ATMCI_BLKLEN(x) ((x) << 16) /* Data Block Length */
106#define ATMCI_CSTOR 0x001c /* Completion Signal Timeout[2] */
107#define ATMCI_CSTOCYC(x) ((x) << 0) /* CST cycles */
108#define ATMCI_CSTOMUL(x) ((x) << 4) /* CST multiplier */
109#define ATMCI_RSPR 0x0020 /* Response 0 */
110#define ATMCI_RSPR1 0x0024 /* Response 1 */
111#define ATMCI_RSPR2 0x0028 /* Response 2 */
112#define ATMCI_RSPR3 0x002c /* Response 3 */
113#define ATMCI_RDR 0x0030 /* Receive Data */
114#define ATMCI_TDR 0x0034 /* Transmit Data */
115#define ATMCI_SR 0x0040 /* Status */
116#define ATMCI_IER 0x0044 /* Interrupt Enable */
117#define ATMCI_IDR 0x0048 /* Interrupt Disable */
118#define ATMCI_IMR 0x004c /* Interrupt Mask */
119#define ATMCI_CMDRDY BIT(0) /* Command Ready */
120#define ATMCI_RXRDY BIT(1) /* Receiver Ready */
121#define ATMCI_TXRDY BIT(2) /* Transmitter Ready */
122#define ATMCI_BLKE BIT(3) /* Data Block Ended */
123#define ATMCI_DTIP BIT(4) /* Data Transfer In Progress */
124#define ATMCI_NOTBUSY BIT(5) /* Data Not Busy */
125#define ATMCI_ENDRX BIT(6) /* End of RX Buffer */
126#define ATMCI_ENDTX BIT(7) /* End of TX Buffer */
127#define ATMCI_SDIOIRQA BIT(8) /* SDIO IRQ in slot A */
128#define ATMCI_SDIOIRQB BIT(9) /* SDIO IRQ in slot B */
129#define ATMCI_SDIOWAIT BIT(12) /* SDIO Read Wait Operation Status */
130#define ATMCI_CSRCV BIT(13) /* CE-ATA Completion Signal Received */
131#define ATMCI_RXBUFF BIT(14) /* RX Buffer Full */
132#define ATMCI_TXBUFE BIT(15) /* TX Buffer Empty */
133#define ATMCI_RINDE BIT(16) /* Response Index Error */
134#define ATMCI_RDIRE BIT(17) /* Response Direction Error */
135#define ATMCI_RCRCE BIT(18) /* Response CRC Error */
136#define ATMCI_RENDE BIT(19) /* Response End Bit Error */
137#define ATMCI_RTOE BIT(20) /* Response Time-Out Error */
138#define ATMCI_DCRCE BIT(21) /* Data CRC Error */
139#define ATMCI_DTOE BIT(22) /* Data Time-Out Error */
140#define ATMCI_CSTOE BIT(23) /* Completion Signal Time-out Error */
141#define ATMCI_BLKOVRE BIT(24) /* DMA Block Overrun Error */
142#define ATMCI_DMADONE BIT(25) /* DMA Transfer Done */
143#define ATMCI_FIFOEMPTY BIT(26) /* FIFO Empty Flag */
144#define ATMCI_XFRDONE BIT(27) /* Transfer Done Flag */
145#define ATMCI_ACKRCV BIT(28) /* Boot Operation Acknowledge Received */
146#define ATMCI_ACKRCVE BIT(29) /* Boot Operation Acknowledge Error */
147#define ATMCI_OVRE BIT(30) /* RX Overrun Error */
148#define ATMCI_UNRE BIT(31) /* TX Underrun Error */
149#define ATMCI_DMA 0x0050 /* DMA Configuration[2] */
150#define ATMCI_DMA_OFFSET(x) ((x) << 0) /* DMA Write Buffer Offset */
151#define ATMCI_DMA_CHKSIZE(x) ((x) << 4) /* DMA Channel Read and Write Chunk Size */
152#define ATMCI_DMAEN BIT(8) /* DMA Hardware Handshaking Enable */
153#define ATMCI_CFG 0x0054 /* Configuration[2] */
154#define ATMCI_CFG_FIFOMODE_1DATA BIT(0) /* MCI Internal FIFO control mode */
155#define ATMCI_CFG_FERRCTRL_COR BIT(4) /* Flow Error flag reset control mode */
156#define ATMCI_CFG_HSMODE BIT(8) /* High Speed Mode */
157#define ATMCI_CFG_LSYNC BIT(12) /* Synchronize on the last block */
158#define ATMCI_WPMR 0x00e4 /* Write Protection Mode[2] */
159#define ATMCI_WP_EN BIT(0) /* WP Enable */
160#define ATMCI_WP_KEY (0x4d4349 << 8) /* WP Key */
161#define ATMCI_WPSR 0x00e8 /* Write Protection Status[2] */
162#define ATMCI_GET_WP_VS(x) ((x) & 0x0f)
163#define ATMCI_GET_WP_VSRC(x) (((x) >> 8) & 0xffff)
164#define ATMCI_VERSION 0x00FC /* Version */
165#define ATMCI_FIFO_APERTURE 0x0200 /* FIFO Aperture[2] */
166
167/* This is not including the FIFO Aperture on MCI2 */
168#define ATMCI_REGS_SIZE 0x100
169
170/* Register access macros */
171#define atmci_readl(port, reg) \
172 __raw_readl((port)->regs + reg)
173#define atmci_writel(port, reg, value) \
174 __raw_writel((value), (port)->regs + reg)
175
176/* On AVR chips the Peripheral DMA Controller is not connected to MCI. */
177#ifdef CONFIG_AVR32
178# define ATMCI_PDC_CONNECTED 0
179#else
180# define ATMCI_PDC_CONNECTED 1
181#endif
182
183/*
184 * Fix sconfig's burst size according to atmel MCI. We need to convert them as:
185 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
186 *
187 * This can be done by finding most significant bit set.
188 */
189static inline unsigned int atmci_convert_chksize(unsigned int maxburst)
190{
191 if (maxburst > 1)
192 return fls(maxburst) - 2;
193 else
194 return 0;
195}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200196
Wenyou Yangae552ab2014-10-30 12:00:41 +0800197#define AUTOSUSPEND_DELAY 50
198
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000199#define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200200#define ATMCI_DMA_THRESHOLD 16
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200201
202enum {
Ludovic Desrochesf5177542012-05-16 15:25:59 +0200203 EVENT_CMD_RDY = 0,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200204 EVENT_XFER_COMPLETE,
Ludovic Desrochesf5177542012-05-16 15:25:59 +0200205 EVENT_NOTBUSY,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200206 EVENT_DATA_ERROR,
207};
208
209enum atmel_mci_state {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200210 STATE_IDLE = 0,
211 STATE_SENDING_CMD,
Ludovic Desrochesf5177542012-05-16 15:25:59 +0200212 STATE_DATA_XFER,
213 STATE_WAITING_NOTBUSY,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200214 STATE_SENDING_STOP,
Ludovic Desrochesf5177542012-05-16 15:25:59 +0200215 STATE_END_REQUEST,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200216};
217
Ludovic Desroches796211b2011-08-11 15:25:44 +0000218enum atmci_xfer_dir {
219 XFER_RECEIVE = 0,
220 XFER_TRANSMIT,
221};
222
223enum atmci_pdc_buf {
224 PDC_FIRST_BUF = 0,
225 PDC_SECOND_BUF,
226};
227
228struct atmel_mci_caps {
Hein_Tiboschccdfe612012-08-30 16:34:38 +0000229 bool has_dma_conf_reg;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000230 bool has_pdc;
231 bool has_cfg_reg;
232 bool has_cstor_reg;
233 bool has_highspeed;
234 bool has_rwproof;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +0100235 bool has_odd_clk_div;
Ludovic Desroches24011f32012-05-16 15:26:00 +0200236 bool has_bad_data_ordering;
237 bool need_reset_after_xfer;
238 bool need_blksz_mul_4;
Ludovic Desroches077d4072012-07-24 11:42:04 +0200239 bool need_notbusy_for_read_ops;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000240};
241
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200242struct atmel_mci_dma {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200243 struct dma_chan *chan;
244 struct dma_async_tx_descriptor *data_desc;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200245};
246
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200247/**
248 * struct atmel_mci - MMC controller state shared between all slots
249 * @lock: Spinlock protecting the queue and associated data.
250 * @regs: Pointer to MMIO registers.
Ludovic Desroches796211b2011-08-11 15:25:44 +0000251 * @sg: Scatterlist entry currently being processed by PIO or PDC code.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200252 * @pio_offset: Offset into the current scatterlist entry.
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200253 * @buffer: Buffer used if we don't have the r/w proof capability. We
254 * don't have the time to switch pdc buffers so we have to use only
255 * one buffer for the full transaction.
256 * @buf_size: size of the buffer.
257 * @phys_buf_addr: buffer address needed for pdc.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200258 * @cur_slot: The slot which is currently using the controller.
259 * @mrq: The request currently being processed on @cur_slot,
260 * or NULL if the controller is idle.
261 * @cmd: The command currently being sent to the card, or NULL.
262 * @data: The data currently being transferred, or NULL if no data
263 * transfer is in progress.
Ludovic Desroches796211b2011-08-11 15:25:44 +0000264 * @data_size: just data->blocks * data->blksz.
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200265 * @dma: DMA client state.
266 * @data_chan: DMA channel being used for the current data transfer.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200267 * @cmd_status: Snapshot of SR taken upon completion of the current
268 * command. Only valid when EVENT_CMD_COMPLETE is pending.
269 * @data_status: Snapshot of SR taken upon completion of the current
270 * data transfer. Only valid when EVENT_DATA_COMPLETE or
271 * EVENT_DATA_ERROR is pending.
272 * @stop_cmdr: Value to be loaded into CMDR when the stop command is
273 * to be sent.
274 * @tasklet: Tasklet running the request state machine.
275 * @pending_events: Bitmask of events flagged by the interrupt handler
276 * to be processed by the tasklet.
277 * @completed_events: Bitmask of events which the state machine has
278 * processed.
279 * @state: Tasklet state.
280 * @queue: List of slots waiting for access to the controller.
281 * @need_clock_update: Update the clock rate before the next request.
282 * @need_reset: Reset controller before next request.
Ludovic Desroches24011f32012-05-16 15:26:00 +0200283 * @timer: Timer to balance the data timeout error flag which cannot rise.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200284 * @mode_reg: Value of the MR register.
Nicolas Ferre74791a22009-12-14 18:01:31 -0800285 * @cfg_reg: Value of the CFG register.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200286 * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus
287 * rate and timeout calculations.
288 * @mapbase: Physical address of the MMIO registers.
289 * @mck: The peripheral bus clock hooked up to the MMC controller.
290 * @pdev: Platform device associated with the MMC controller.
291 * @slot: Slots sharing this MMC controller.
Ludovic Desroches796211b2011-08-11 15:25:44 +0000292 * @caps: MCI capabilities depending on MCI version.
293 * @prepare_data: function to setup MCI before data transfer which
294 * depends on MCI capabilities.
295 * @submit_data: function to start data transfer which depends on MCI
296 * capabilities.
297 * @stop_transfer: function to stop data transfer which depends on MCI
298 * capabilities.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200299 *
300 * Locking
301 * =======
302 *
303 * @lock is a softirq-safe spinlock protecting @queue as well as
304 * @cur_slot, @mrq and @state. These must always be updated
305 * at the same time while holding @lock.
306 *
307 * @lock also protects mode_reg and need_clock_update since these are
308 * used to synchronize mode register updates with the queue
309 * processing.
310 *
311 * The @mrq field of struct atmel_mci_slot is also protected by @lock,
312 * and must always be written at the same time as the slot is added to
313 * @queue.
314 *
315 * @pending_events and @completed_events are accessed using atomic bit
316 * operations, so they don't need any locking.
317 *
318 * None of the fields touched by the interrupt handler need any
319 * locking. However, ordering is important: Before EVENT_DATA_ERROR or
320 * EVENT_DATA_COMPLETE is set in @pending_events, all data-related
321 * interrupts must be disabled and @data_status updated with a
322 * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300323 * CMDRDY interrupt must be disabled and @cmd_status updated with a
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200324 * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the
325 * bytes_xfered field of @data must be written. This is ensured by
326 * using barriers.
327 */
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200328struct atmel_mci {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200329 spinlock_t lock;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200330 void __iomem *regs;
331
332 struct scatterlist *sg;
Terry Barnabybdbc5d02013-04-08 12:05:47 -0400333 unsigned int sg_len;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200334 unsigned int pio_offset;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200335 unsigned int *buffer;
336 unsigned int buf_size;
337 dma_addr_t buf_phys_addr;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200338
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200339 struct atmel_mci_slot *cur_slot;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200340 struct mmc_request *mrq;
341 struct mmc_command *cmd;
342 struct mmc_data *data;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000343 unsigned int data_size;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200344
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200345 struct atmel_mci_dma dma;
346 struct dma_chan *data_chan;
Viresh Kumare2b35f32012-02-01 16:12:27 +0530347 struct dma_slave_config dma_conf;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200348
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200349 u32 cmd_status;
350 u32 data_status;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200351 u32 stop_cmdr;
352
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200353 struct tasklet_struct tasklet;
354 unsigned long pending_events;
355 unsigned long completed_events;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200356 enum atmel_mci_state state;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200357 struct list_head queue;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200358
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200359 bool need_clock_update;
360 bool need_reset;
Ludovic Desroches24011f32012-05-16 15:26:00 +0200361 struct timer_list timer;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200362 u32 mode_reg;
Nicolas Ferre74791a22009-12-14 18:01:31 -0800363 u32 cfg_reg;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200364 unsigned long bus_hz;
365 unsigned long mapbase;
366 struct clk *mck;
367 struct platform_device *pdev;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200368
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000369 struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS];
Ludovic Desroches796211b2011-08-11 15:25:44 +0000370
371 struct atmel_mci_caps caps;
372
373 u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data);
374 void (*submit_data)(struct atmel_mci *host, struct mmc_data *data);
375 void (*stop_transfer)(struct atmel_mci *host);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200376};
377
378/**
379 * struct atmel_mci_slot - MMC slot state
380 * @mmc: The mmc_host representing this slot.
381 * @host: The MMC controller this slot is using.
382 * @sdc_reg: Value of SDCR to be written before using this slot.
Anders Grahn88ff82e2010-05-26 14:42:01 -0700383 * @sdio_irq: SDIO irq mask for this slot.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200384 * @mrq: mmc_request currently being processed or waiting to be
385 * processed, or NULL when the slot is idle.
386 * @queue_node: List node for placing this node in the @queue list of
387 * &struct atmel_mci.
388 * @clock: Clock rate configured by set_ios(). Protected by host->lock.
389 * @flags: Random state bits associated with the slot.
390 * @detect_pin: GPIO pin used for card detection, or negative if not
391 * available.
392 * @wp_pin: GPIO pin used for card write protect sending, or negative
393 * if not available.
Jonas Larsson1c1452b2009-03-31 11:16:48 +0200394 * @detect_is_active_high: The state of the detect pin when it is active.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200395 * @detect_timer: Timer used for debouncing @detect_pin interrupts.
396 */
397struct atmel_mci_slot {
398 struct mmc_host *mmc;
399 struct atmel_mci *host;
400
401 u32 sdc_reg;
Anders Grahn88ff82e2010-05-26 14:42:01 -0700402 u32 sdio_irq;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200403
404 struct mmc_request *mrq;
405 struct list_head queue_node;
406
407 unsigned int clock;
408 unsigned long flags;
409#define ATMCI_CARD_PRESENT 0
410#define ATMCI_CARD_NEED_INIT 1
411#define ATMCI_SHUTDOWN 2
412
413 int detect_pin;
414 int wp_pin;
Jonas Larsson1c1452b2009-03-31 11:16:48 +0200415 bool detect_is_active_high;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200416
417 struct timer_list detect_timer;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200418};
419
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200420#define atmci_test_and_clear_pending(host, event) \
421 test_and_clear_bit(event, &host->pending_events)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200422#define atmci_set_completed(host, event) \
423 set_bit(event, &host->completed_events)
424#define atmci_set_pending(host, event) \
425 set_bit(event, &host->pending_events)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200426
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200427/*
428 * The debugfs stuff below is mostly optimized away when
429 * CONFIG_DEBUG_FS is not set.
430 */
431static int atmci_req_show(struct seq_file *s, void *v)
432{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200433 struct atmel_mci_slot *slot = s->private;
434 struct mmc_request *mrq;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200435 struct mmc_command *cmd;
436 struct mmc_command *stop;
437 struct mmc_data *data;
438
439 /* Make sure we get a consistent snapshot */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200440 spin_lock_bh(&slot->host->lock);
441 mrq = slot->mrq;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200442
443 if (mrq) {
444 cmd = mrq->cmd;
445 data = mrq->data;
446 stop = mrq->stop;
447
448 if (cmd)
449 seq_printf(s,
450 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
451 cmd->opcode, cmd->arg, cmd->flags,
452 cmd->resp[0], cmd->resp[1], cmd->resp[2],
Nicolas Ferred586ebb2010-05-11 14:06:50 -0700453 cmd->resp[3], cmd->error);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200454 if (data)
455 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
456 data->bytes_xfered, data->blocks,
457 data->blksz, data->flags, data->error);
458 if (stop)
459 seq_printf(s,
460 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
461 stop->opcode, stop->arg, stop->flags,
462 stop->resp[0], stop->resp[1], stop->resp[2],
Nicolas Ferred586ebb2010-05-11 14:06:50 -0700463 stop->resp[3], stop->error);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200464 }
465
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200466 spin_unlock_bh(&slot->host->lock);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200467
468 return 0;
469}
470
471static int atmci_req_open(struct inode *inode, struct file *file)
472{
473 return single_open(file, atmci_req_show, inode->i_private);
474}
475
476static const struct file_operations atmci_req_fops = {
477 .owner = THIS_MODULE,
478 .open = atmci_req_open,
479 .read = seq_read,
480 .llseek = seq_lseek,
481 .release = single_release,
482};
483
484static void atmci_show_status_reg(struct seq_file *s,
485 const char *regname, u32 value)
486{
487 static const char *sr_bit[] = {
488 [0] = "CMDRDY",
489 [1] = "RXRDY",
490 [2] = "TXRDY",
491 [3] = "BLKE",
492 [4] = "DTIP",
493 [5] = "NOTBUSY",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700494 [6] = "ENDRX",
495 [7] = "ENDTX",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200496 [8] = "SDIOIRQA",
497 [9] = "SDIOIRQB",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700498 [12] = "SDIOWAIT",
499 [14] = "RXBUFF",
500 [15] = "TXBUFE",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200501 [16] = "RINDE",
502 [17] = "RDIRE",
503 [18] = "RCRCE",
504 [19] = "RENDE",
505 [20] = "RTOE",
506 [21] = "DCRCE",
507 [22] = "DTOE",
Rob Emanuele04d699c2009-09-22 16:45:19 -0700508 [23] = "CSTOE",
509 [24] = "BLKOVRE",
510 [25] = "DMADONE",
511 [26] = "FIFOEMPTY",
512 [27] = "XFRDONE",
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200513 [30] = "OVRE",
514 [31] = "UNRE",
515 };
516 unsigned int i;
517
518 seq_printf(s, "%s:\t0x%08x", regname, value);
519 for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
520 if (value & (1 << i)) {
521 if (sr_bit[i])
522 seq_printf(s, " %s", sr_bit[i]);
523 else
524 seq_puts(s, " UNKNOWN");
525 }
526 }
527 seq_putc(s, '\n');
528}
529
530static int atmci_regs_show(struct seq_file *s, void *v)
531{
532 struct atmel_mci *host = s->private;
533 u32 *buf;
Boris BREZILLONb3894f22013-07-18 09:38:52 +0200534 int ret = 0;
535
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200536
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000537 buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200538 if (!buf)
539 return -ENOMEM;
540
Wenyou Yangae552ab2014-10-30 12:00:41 +0800541 pm_runtime_get_sync(&host->pdev->dev);
542
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200543 /*
544 * Grab a more or less consistent snapshot. Note that we're
545 * not disabling interrupts, so IMR and SR may not be
546 * consistent.
547 */
548 spin_lock_bh(&host->lock);
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000549 memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200550 spin_unlock_bh(&host->lock);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200551
Wenyou Yangae552ab2014-10-30 12:00:41 +0800552 pm_runtime_mark_last_busy(&host->pdev->dev);
553 pm_runtime_put_autosuspend(&host->pdev->dev);
Boris BREZILLONb3894f22013-07-18 09:38:52 +0200554
Nicolas Ferre8a4de072012-07-06 12:11:51 +0200555 seq_printf(s, "MR:\t0x%08x%s%s ",
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000556 buf[ATMCI_MR / 4],
557 buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "",
Nicolas Ferre8a4de072012-07-06 12:11:51 +0200558 buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "");
559 if (host->caps.has_odd_clk_div)
560 seq_printf(s, "{CLKDIV,CLKODD}=%u\n",
561 ((buf[ATMCI_MR / 4] & 0xff) << 1)
562 | ((buf[ATMCI_MR / 4] >> 16) & 1));
563 else
564 seq_printf(s, "CLKDIV=%u\n",
565 (buf[ATMCI_MR / 4] & 0xff));
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000566 seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]);
567 seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]);
568 seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200569 seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000570 buf[ATMCI_BLKR / 4],
571 buf[ATMCI_BLKR / 4] & 0xffff,
572 (buf[ATMCI_BLKR / 4] >> 16) & 0xffff);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000573 if (host->caps.has_cstor_reg)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000574 seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200575
576 /* Don't read RSPR and RDR; it will consume the data there */
577
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000578 atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]);
579 atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200580
Hein_Tiboschccdfe612012-08-30 16:34:38 +0000581 if (host->caps.has_dma_conf_reg) {
Nicolas Ferre74791a22009-12-14 18:01:31 -0800582 u32 val;
583
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000584 val = buf[ATMCI_DMA / 4];
Nicolas Ferre74791a22009-12-14 18:01:31 -0800585 seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n",
586 val, val & 3,
587 ((val >> 4) & 3) ?
588 1 << (((val >> 4) & 3) + 1) : 1,
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000589 val & ATMCI_DMAEN ? " DMAEN" : "");
Ludovic Desroches796211b2011-08-11 15:25:44 +0000590 }
591 if (host->caps.has_cfg_reg) {
592 u32 val;
Nicolas Ferre74791a22009-12-14 18:01:31 -0800593
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000594 val = buf[ATMCI_CFG / 4];
Nicolas Ferre74791a22009-12-14 18:01:31 -0800595 seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n",
596 val,
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000597 val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "",
598 val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "",
599 val & ATMCI_CFG_HSMODE ? " HSMODE" : "",
600 val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
Nicolas Ferre74791a22009-12-14 18:01:31 -0800601 }
602
Haavard Skinnemoenb17339a2008-09-19 21:09:28 +0200603 kfree(buf);
604
Boris BREZILLONb3894f22013-07-18 09:38:52 +0200605 return ret;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200606}
607
608static int atmci_regs_open(struct inode *inode, struct file *file)
609{
610 return single_open(file, atmci_regs_show, inode->i_private);
611}
612
613static const struct file_operations atmci_regs_fops = {
614 .owner = THIS_MODULE,
615 .open = atmci_regs_open,
616 .read = seq_read,
617 .llseek = seq_lseek,
618 .release = single_release,
619};
620
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200621static void atmci_init_debugfs(struct atmel_mci_slot *slot)
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200622{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200623 struct mmc_host *mmc = slot->mmc;
624 struct atmel_mci *host = slot->host;
625 struct dentry *root;
626 struct dentry *node;
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200627
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200628 root = mmc->debugfs_root;
629 if (!root)
630 return;
631
632 node = debugfs_create_file("regs", S_IRUSR, root, host,
633 &atmci_regs_fops);
634 if (IS_ERR(node))
635 return;
636 if (!node)
637 goto err;
638
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200639 node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200640 if (!node)
641 goto err;
642
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +0200643 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
644 if (!node)
645 goto err;
646
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200647 node = debugfs_create_x32("pending_events", S_IRUSR, root,
648 (u32 *)&host->pending_events);
649 if (!node)
650 goto err;
651
652 node = debugfs_create_x32("completed_events", S_IRUSR, root,
653 (u32 *)&host->completed_events);
654 if (!node)
655 goto err;
656
657 return;
658
659err:
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200660 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200661}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200662
Ludovic Desrochese919fd22012-07-24 15:30:03 +0200663#if defined(CONFIG_OF)
664static const struct of_device_id atmci_dt_ids[] = {
665 { .compatible = "atmel,hsmci" },
666 { /* sentinel */ }
667};
668
669MODULE_DEVICE_TABLE(of, atmci_dt_ids);
670
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500671static struct mci_platform_data*
Ludovic Desrochese919fd22012-07-24 15:30:03 +0200672atmci_of_init(struct platform_device *pdev)
673{
674 struct device_node *np = pdev->dev.of_node;
675 struct device_node *cnp;
676 struct mci_platform_data *pdata;
677 u32 slot_id;
678
679 if (!np) {
680 dev_err(&pdev->dev, "device node not found\n");
681 return ERR_PTR(-EINVAL);
682 }
683
684 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
685 if (!pdata) {
686 dev_err(&pdev->dev, "could not allocate memory for pdata\n");
687 return ERR_PTR(-ENOMEM);
688 }
689
690 for_each_child_of_node(np, cnp) {
691 if (of_property_read_u32(cnp, "reg", &slot_id)) {
692 dev_warn(&pdev->dev, "reg property is missing for %s\n",
693 cnp->full_name);
694 continue;
695 }
696
697 if (slot_id >= ATMCI_MAX_NR_SLOTS) {
698 dev_warn(&pdev->dev, "can't have more than %d slots\n",
699 ATMCI_MAX_NR_SLOTS);
700 break;
701 }
702
703 if (of_property_read_u32(cnp, "bus-width",
704 &pdata->slot[slot_id].bus_width))
705 pdata->slot[slot_id].bus_width = 1;
706
707 pdata->slot[slot_id].detect_pin =
708 of_get_named_gpio(cnp, "cd-gpios", 0);
709
710 pdata->slot[slot_id].detect_is_active_high =
711 of_property_read_bool(cnp, "cd-inverted");
712
Timo Kokkonen76d55562014-11-03 13:12:59 +0200713 pdata->slot[slot_id].non_removable =
714 of_property_read_bool(cnp, "non-removable");
715
Ludovic Desrochese919fd22012-07-24 15:30:03 +0200716 pdata->slot[slot_id].wp_pin =
717 of_get_named_gpio(cnp, "wp-gpios", 0);
718 }
719
720 return pdata;
721}
722#else /* CONFIG_OF */
723static inline struct mci_platform_data*
724atmci_of_init(struct platform_device *dev)
725{
726 return ERR_PTR(-EINVAL);
727}
728#endif
729
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200730static inline unsigned int atmci_get_version(struct atmel_mci *host)
731{
732 return atmci_readl(host, ATMCI_VERSION) & 0x00000fff;
733}
734
Ludovic Desroches24011f32012-05-16 15:26:00 +0200735static void atmci_timeout_timer(unsigned long data)
736{
737 struct atmel_mci *host;
738
739 host = (struct atmel_mci *)data;
740
741 dev_dbg(&host->pdev->dev, "software timeout\n");
742
743 if (host->mrq->cmd->data) {
744 host->mrq->cmd->data->error = -ETIMEDOUT;
745 host->data = NULL;
Ludovic Desrochesc1fa3422013-09-09 17:29:56 +0200746 /*
747 * With some SDIO modules, sometimes DMA transfer hangs. If
748 * stop_transfer() is not called then the DMA request is not
749 * removed, following ones are queued and never computed.
750 */
751 if (host->state == STATE_DATA_XFER)
752 host->stop_transfer(host);
Ludovic Desroches24011f32012-05-16 15:26:00 +0200753 } else {
754 host->mrq->cmd->error = -ETIMEDOUT;
755 host->cmd = NULL;
756 }
757 host->need_reset = 1;
758 host->state = STATE_END_REQUEST;
759 smp_wmb();
760 tasklet_schedule(&host->tasklet);
761}
762
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000763static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200764 unsigned int ns)
765{
Ludovic Desroches66292ad2012-03-28 12:28:33 +0200766 /*
767 * It is easier here to use us instead of ns for the timeout,
768 * it prevents from overflows during calculation.
769 */
770 unsigned int us = DIV_ROUND_UP(ns, 1000);
771
772 /* Maximum clock frequency is host->bus_hz/2 */
773 return us * (DIV_ROUND_UP(host->bus_hz, 2000000));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200774}
775
776static void atmci_set_timeout(struct atmel_mci *host,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200777 struct atmel_mci_slot *slot, struct mmc_data *data)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200778{
779 static unsigned dtomul_to_shift[] = {
780 0, 4, 7, 8, 10, 12, 16, 20
781 };
782 unsigned timeout;
783 unsigned dtocyc;
784 unsigned dtomul;
785
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000786 timeout = atmci_ns_to_clocks(host, data->timeout_ns)
787 + data->timeout_clks;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200788
789 for (dtomul = 0; dtomul < 8; dtomul++) {
790 unsigned shift = dtomul_to_shift[dtomul];
791 dtocyc = (timeout + (1 << shift) - 1) >> shift;
792 if (dtocyc < 15)
793 break;
794 }
795
796 if (dtomul >= 8) {
797 dtomul = 7;
798 dtocyc = 15;
799 }
800
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200801 dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n",
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200802 dtocyc << dtomul_to_shift[dtomul]);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000803 atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc)));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200804}
805
806/*
807 * Return mask with command flags to be enabled for this command.
808 */
809static u32 atmci_prepare_command(struct mmc_host *mmc,
810 struct mmc_command *cmd)
811{
812 struct mmc_data *data;
813 u32 cmdr;
814
815 cmd->error = -EINPROGRESS;
816
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000817 cmdr = ATMCI_CMDR_CMDNB(cmd->opcode);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200818
819 if (cmd->flags & MMC_RSP_PRESENT) {
820 if (cmd->flags & MMC_RSP_136)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000821 cmdr |= ATMCI_CMDR_RSPTYP_136BIT;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200822 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000823 cmdr |= ATMCI_CMDR_RSPTYP_48BIT;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200824 }
825
826 /*
827 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
828 * it's too difficult to determine whether this is an ACMD or
829 * not. Better make it 64.
830 */
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000831 cmdr |= ATMCI_CMDR_MAXLAT_64CYC;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200832
833 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000834 cmdr |= ATMCI_CMDR_OPDCMD;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200835
836 data = cmd->data;
837 if (data) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000838 cmdr |= ATMCI_CMDR_START_XFER;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100839
840 if (cmd->opcode == SD_IO_RW_EXTENDED) {
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000841 cmdr |= ATMCI_CMDR_SDIO_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100842 } else {
843 if (data->flags & MMC_DATA_STREAM)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000844 cmdr |= ATMCI_CMDR_STREAM;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100845 else if (data->blocks > 1)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000846 cmdr |= ATMCI_CMDR_MULTI_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100847 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000848 cmdr |= ATMCI_CMDR_BLOCK;
Nicolas Ferre2f1d7912010-12-10 19:14:32 +0100849 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200850
851 if (data->flags & MMC_DATA_READ)
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000852 cmdr |= ATMCI_CMDR_TRDIR_READ;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200853 }
854
855 return cmdr;
856}
857
Ludovic Desroches11d14882011-08-11 15:25:45 +0000858static void atmci_send_command(struct atmel_mci *host,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200859 struct mmc_command *cmd, u32 cmd_flags)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200860{
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200861 WARN_ON(host->cmd);
862 host->cmd = cmd;
863
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +0200864 dev_vdbg(&host->pdev->dev,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200865 "start command: ARGR=0x%08x CMDR=0x%08x\n",
866 cmd->arg, cmd_flags);
867
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000868 atmci_writel(host, ATMCI_ARGR, cmd->arg);
869 atmci_writel(host, ATMCI_CMDR, cmd_flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200870}
871
Ludovic Desroches2c96a292011-08-11 15:25:41 +0000872static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200873{
Ludovic Desroches6801c412012-05-16 15:26:01 +0200874 dev_dbg(&host->pdev->dev, "send stop command\n");
Ludovic Desroches11d14882011-08-11 15:25:45 +0000875 atmci_send_command(host, data->stop, host->stop_cmdr);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +0000876 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200877}
878
Ludovic Desroches796211b2011-08-11 15:25:44 +0000879/*
880 * Configure given PDC buffer taking care of alignement issues.
881 * Update host->data_size and host->sg.
882 */
883static void atmci_pdc_set_single_buf(struct atmel_mci *host,
884 enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200885{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000886 u32 pointer_reg, counter_reg;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200887 unsigned int buf_size;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200888
Ludovic Desroches796211b2011-08-11 15:25:44 +0000889 if (dir == XFER_RECEIVE) {
890 pointer_reg = ATMEL_PDC_RPR;
891 counter_reg = ATMEL_PDC_RCR;
892 } else {
893 pointer_reg = ATMEL_PDC_TPR;
894 counter_reg = ATMEL_PDC_TCR;
895 }
896
897 if (buf_nb == PDC_SECOND_BUF) {
Ludovic Desroches1ebbe3d2011-08-11 15:25:46 +0000898 pointer_reg += ATMEL_PDC_SCND_BUF_OFF;
899 counter_reg += ATMEL_PDC_SCND_BUF_OFF;
Ludovic Desroches796211b2011-08-11 15:25:44 +0000900 }
901
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200902 if (!host->caps.has_rwproof) {
903 buf_size = host->buf_size;
904 atmci_writel(host, pointer_reg, host->buf_phys_addr);
905 } else {
906 buf_size = sg_dma_len(host->sg);
907 atmci_writel(host, pointer_reg, sg_dma_address(host->sg));
908 }
909
910 if (host->data_size <= buf_size) {
Ludovic Desroches796211b2011-08-11 15:25:44 +0000911 if (host->data_size & 0x3) {
912 /* If size is different from modulo 4, transfer bytes */
913 atmci_writel(host, counter_reg, host->data_size);
914 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE);
915 } else {
916 /* Else transfer 32-bits words */
917 atmci_writel(host, counter_reg, host->data_size / 4);
918 }
919 host->data_size = 0;
920 } else {
921 /* We assume the size of a page is 32-bits aligned */
Ludovic Desroches341fa4c2011-08-11 15:25:47 +0000922 atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4);
923 host->data_size -= sg_dma_len(host->sg);
Ludovic Desroches796211b2011-08-11 15:25:44 +0000924 if (host->data_size)
925 host->sg = sg_next(host->sg);
926 }
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200927}
928
Ludovic Desroches796211b2011-08-11 15:25:44 +0000929/*
930 * Configure PDC buffer according to the data size ie configuring one or two
931 * buffers. Don't use this function if you want to configure only the second
932 * buffer. In this case, use atmci_pdc_set_single_buf.
933 */
934static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200935{
Ludovic Desroches796211b2011-08-11 15:25:44 +0000936 atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF);
937 if (host->data_size)
938 atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF);
939}
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200940
Ludovic Desroches796211b2011-08-11 15:25:44 +0000941/*
942 * Unmap sg lists, called when transfer is finished.
943 */
944static void atmci_pdc_cleanup(struct atmel_mci *host)
945{
946 struct mmc_data *data = host->data;
947
948 if (data)
949 dma_unmap_sg(&host->pdev->dev,
950 data->sg, data->sg_len,
951 ((data->flags & MMC_DATA_WRITE)
952 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
953}
954
955/*
956 * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after
957 * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY
958 * interrupt needed for both transfer directions.
959 */
960static void atmci_pdc_complete(struct atmel_mci *host)
961{
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200962 int transfer_size = host->data->blocks * host->data->blksz;
Ludovic Desroches24011f32012-05-16 15:26:00 +0200963 int i;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200964
Ludovic Desroches796211b2011-08-11 15:25:44 +0000965 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200966
967 if ((!host->caps.has_rwproof)
Ludovic Desroches24011f32012-05-16 15:26:00 +0200968 && (host->data->flags & MMC_DATA_READ)) {
969 if (host->caps.has_bad_data_ordering)
970 for (i = 0; i < transfer_size; i++)
971 host->buffer[i] = swab32(host->buffer[i]);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200972 sg_copy_from_buffer(host->data->sg, host->data->sg_len,
973 host->buffer, transfer_size);
Ludovic Desroches24011f32012-05-16 15:26:00 +0200974 }
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +0200975
Ludovic Desroches796211b2011-08-11 15:25:44 +0000976 atmci_pdc_cleanup(host);
977
Alexandre Belloni6e9e4062014-05-06 17:43:26 +0200978 dev_dbg(&host->pdev->dev, "(%s) set pending xfer complete\n", __func__);
979 atmci_set_pending(host, EVENT_XFER_COMPLETE);
980 tasklet_schedule(&host->tasklet);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200981}
982
Ludovic Desroches796211b2011-08-11 15:25:44 +0000983static void atmci_dma_cleanup(struct atmel_mci *host)
984{
985 struct mmc_data *data = host->data;
986
987 if (data)
988 dma_unmap_sg(host->dma.chan->device->dev,
989 data->sg, data->sg_len,
990 ((data->flags & MMC_DATA_WRITE)
991 ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
992}
993
994/*
995 * This function is called by the DMA driver from tasklet context.
996 */
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +0200997static void atmci_dma_complete(void *arg)
998{
999 struct atmel_mci *host = arg;
1000 struct mmc_data *data = host->data;
1001
1002 dev_vdbg(&host->pdev->dev, "DMA complete\n");
1003
Hein_Tiboschccdfe612012-08-30 16:34:38 +00001004 if (host->caps.has_dma_conf_reg)
Nicolas Ferre74791a22009-12-14 18:01:31 -08001005 /* Disable DMA hardware handshaking on MCI */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001006 atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN);
Nicolas Ferre74791a22009-12-14 18:01:31 -08001007
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001008 atmci_dma_cleanup(host);
1009
1010 /*
1011 * If the card was removed, data will be NULL. No point trying
1012 * to send the stop command or waiting for NBUSY in this case.
1013 */
1014 if (data) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001015 dev_dbg(&host->pdev->dev,
1016 "(%s) set pending xfer complete\n", __func__);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001017 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1018 tasklet_schedule(&host->tasklet);
1019
1020 /*
1021 * Regardless of what the documentation says, we have
1022 * to wait for NOTBUSY even after block read
1023 * operations.
1024 *
1025 * When the DMA transfer is complete, the controller
1026 * may still be reading the CRC from the card, i.e.
1027 * the data transfer is still in progress and we
1028 * haven't seen all the potential error bits yet.
1029 *
1030 * The interrupt handler will schedule a different
1031 * tasklet to finish things up when the data transfer
1032 * is completely done.
1033 *
1034 * We may not complete the mmc request here anyway
1035 * because the mmc layer may call back and cause us to
1036 * violate the "don't submit new operations from the
1037 * completion callback" rule of the dma engine
1038 * framework.
1039 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001040 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001041 }
1042}
1043
Ludovic Desroches796211b2011-08-11 15:25:44 +00001044/*
1045 * Returns a mask of interrupt flags to be enabled after the whole
1046 * request has been prepared.
1047 */
1048static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data)
1049{
1050 u32 iflags;
1051
1052 data->error = -EINPROGRESS;
1053
1054 host->sg = data->sg;
Terry Barnabybdbc5d02013-04-08 12:05:47 -04001055 host->sg_len = data->sg_len;
Ludovic Desroches796211b2011-08-11 15:25:44 +00001056 host->data = data;
1057 host->data_chan = NULL;
1058
1059 iflags = ATMCI_DATA_ERROR_FLAGS;
1060
1061 /*
1062 * Errata: MMC data write operation with less than 12
1063 * bytes is impossible.
1064 *
1065 * Errata: MCI Transmit Data Register (TDR) FIFO
1066 * corruption when length is not multiple of 4.
1067 */
1068 if (data->blocks * data->blksz < 12
1069 || (data->blocks * data->blksz) & 3)
1070 host->need_reset = true;
1071
1072 host->pio_offset = 0;
1073 if (data->flags & MMC_DATA_READ)
1074 iflags |= ATMCI_RXRDY;
1075 else
1076 iflags |= ATMCI_TXRDY;
1077
1078 return iflags;
1079}
1080
1081/*
1082 * Set interrupt flags and set block length into the MCI mode register even
1083 * if this value is also accessible in the MCI block register. It seems to be
1084 * necessary before the High Speed MCI version. It also map sg and configure
1085 * PDC registers.
1086 */
1087static u32
1088atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data)
1089{
1090 u32 iflags, tmp;
1091 unsigned int sg_len;
1092 enum dma_data_direction dir;
Ludovic Desroches24011f32012-05-16 15:26:00 +02001093 int i;
Ludovic Desroches796211b2011-08-11 15:25:44 +00001094
1095 data->error = -EINPROGRESS;
1096
1097 host->data = data;
1098 host->sg = data->sg;
1099 iflags = ATMCI_DATA_ERROR_FLAGS;
1100
1101 /* Enable pdc mode */
1102 atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE);
1103
1104 if (data->flags & MMC_DATA_READ) {
1105 dir = DMA_FROM_DEVICE;
1106 iflags |= ATMCI_ENDRX | ATMCI_RXBUFF;
1107 } else {
1108 dir = DMA_TO_DEVICE;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001109 iflags |= ATMCI_ENDTX | ATMCI_TXBUFE | ATMCI_BLKE;
Ludovic Desroches796211b2011-08-11 15:25:44 +00001110 }
1111
1112 /* Set BLKLEN */
1113 tmp = atmci_readl(host, ATMCI_MR);
1114 tmp &= 0x0000ffff;
1115 tmp |= ATMCI_BLKLEN(data->blksz);
1116 atmci_writel(host, ATMCI_MR, tmp);
1117
1118 /* Configure PDC */
1119 host->data_size = data->blocks * data->blksz;
1120 sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02001121
1122 if ((!host->caps.has_rwproof)
Ludovic Desroches24011f32012-05-16 15:26:00 +02001123 && (host->data->flags & MMC_DATA_WRITE)) {
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02001124 sg_copy_to_buffer(host->data->sg, host->data->sg_len,
1125 host->buffer, host->data_size);
Ludovic Desroches24011f32012-05-16 15:26:00 +02001126 if (host->caps.has_bad_data_ordering)
1127 for (i = 0; i < host->data_size; i++)
1128 host->buffer[i] = swab32(host->buffer[i]);
1129 }
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02001130
Ludovic Desroches796211b2011-08-11 15:25:44 +00001131 if (host->data_size)
1132 atmci_pdc_set_both_buf(host,
1133 ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT));
1134
1135 return iflags;
1136}
1137
1138static u32
Nicolas Ferre74791a22009-12-14 18:01:31 -08001139atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001140{
1141 struct dma_chan *chan;
1142 struct dma_async_tx_descriptor *desc;
1143 struct scatterlist *sg;
1144 unsigned int i;
1145 enum dma_data_direction direction;
Vinod Koule0d23ef2011-11-17 14:54:38 +05301146 enum dma_transfer_direction slave_dirn;
Atsushi Nemoto657a77f2009-09-08 17:53:05 -07001147 unsigned int sglen;
Nicolas Ferre693e5e22012-06-06 12:19:44 +02001148 u32 maxburst;
Ludovic Desroches796211b2011-08-11 15:25:44 +00001149 u32 iflags;
1150
1151 data->error = -EINPROGRESS;
1152
1153 WARN_ON(host->data);
1154 host->sg = NULL;
1155 host->data = data;
1156
1157 iflags = ATMCI_DATA_ERROR_FLAGS;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001158
1159 /*
1160 * We don't do DMA on "complex" transfers, i.e. with
1161 * non-word-aligned buffers or lengths. Also, we don't bother
1162 * with all the DMA setup overhead for short transfers.
1163 */
Ludovic Desroches796211b2011-08-11 15:25:44 +00001164 if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD)
1165 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001166 if (data->blksz & 3)
Ludovic Desroches796211b2011-08-11 15:25:44 +00001167 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001168
1169 for_each_sg(data->sg, sg, data->sg_len, i) {
1170 if (sg->offset & 3 || sg->length & 3)
Ludovic Desroches796211b2011-08-11 15:25:44 +00001171 return atmci_prepare_data(host, data);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001172 }
1173
1174 /* If we don't have a channel, we can't do DMA */
1175 chan = host->dma.chan;
Dan Williams6f49a572009-01-06 11:38:14 -07001176 if (chan)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001177 host->data_chan = chan;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001178
1179 if (!chan)
1180 return -ENODEV;
1181
Vinod Koule0d23ef2011-11-17 14:54:38 +05301182 if (data->flags & MMC_DATA_READ) {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001183 direction = DMA_FROM_DEVICE;
Viresh Kumare2b35f32012-02-01 16:12:27 +05301184 host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM;
Nicolas Ferre693e5e22012-06-06 12:19:44 +02001185 maxburst = atmci_convert_chksize(host->dma_conf.src_maxburst);
Vinod Koule0d23ef2011-11-17 14:54:38 +05301186 } else {
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001187 direction = DMA_TO_DEVICE;
Viresh Kumare2b35f32012-02-01 16:12:27 +05301188 host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV;
Nicolas Ferre693e5e22012-06-06 12:19:44 +02001189 maxburst = atmci_convert_chksize(host->dma_conf.dst_maxburst);
Vinod Koule0d23ef2011-11-17 14:54:38 +05301190 }
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001191
Hein_Tiboschccdfe612012-08-30 16:34:38 +00001192 if (host->caps.has_dma_conf_reg)
1193 atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(maxburst) |
1194 ATMCI_DMAEN);
Nicolas Ferre693e5e22012-06-06 12:19:44 +02001195
Linus Walleij266ac3f2011-02-10 16:08:06 +01001196 sglen = dma_map_sg(chan->device->dev, data->sg,
Ludovic Desroches796211b2011-08-11 15:25:44 +00001197 data->sg_len, direction);
Linus Walleij88ce4db32011-02-10 16:08:16 +01001198
Viresh Kumare2b35f32012-02-01 16:12:27 +05301199 dmaengine_slave_config(chan, &host->dma_conf);
Alexandre Bounine16052822012-03-08 16:11:18 -05001200 desc = dmaengine_prep_slave_sg(chan,
Vinod Koule0d23ef2011-11-17 14:54:38 +05301201 data->sg, sglen, slave_dirn,
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001202 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1203 if (!desc)
Atsushi Nemoto657a77f2009-09-08 17:53:05 -07001204 goto unmap_exit;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001205
1206 host->dma.data_desc = desc;
1207 desc->callback = atmci_dma_complete;
1208 desc->callback_param = host;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001209
Ludovic Desroches796211b2011-08-11 15:25:44 +00001210 return iflags;
Atsushi Nemoto657a77f2009-09-08 17:53:05 -07001211unmap_exit:
Linus Walleij88ce4db32011-02-10 16:08:16 +01001212 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction);
Atsushi Nemoto657a77f2009-09-08 17:53:05 -07001213 return -ENOMEM;
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001214}
1215
Ludovic Desroches796211b2011-08-11 15:25:44 +00001216static void
1217atmci_submit_data(struct atmel_mci *host, struct mmc_data *data)
1218{
1219 return;
1220}
1221
1222/*
1223 * Start PDC according to transfer direction.
1224 */
1225static void
1226atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data)
1227{
1228 if (data->flags & MMC_DATA_READ)
1229 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
1230 else
1231 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1232}
1233
1234static void
1235atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data)
Nicolas Ferre74791a22009-12-14 18:01:31 -08001236{
1237 struct dma_chan *chan = host->data_chan;
1238 struct dma_async_tx_descriptor *desc = host->dma.data_desc;
1239
1240 if (chan) {
Linus Walleij53289062011-02-10 16:08:26 +01001241 dmaengine_submit(desc);
1242 dma_async_issue_pending(chan);
Nicolas Ferre74791a22009-12-14 18:01:31 -08001243 }
1244}
1245
Ludovic Desroches796211b2011-08-11 15:25:44 +00001246static void atmci_stop_transfer(struct atmel_mci *host)
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001247{
Ludovic Desroches6801c412012-05-16 15:26:01 +02001248 dev_dbg(&host->pdev->dev,
1249 "(%s) set pending xfer complete\n", __func__);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001250 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001251 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02001252}
1253
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001254/*
Masanari Iida7122bbb2012-08-05 23:25:40 +09001255 * Stop data transfer because error(s) occurred.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001256 */
Ludovic Desroches796211b2011-08-11 15:25:44 +00001257static void atmci_stop_transfer_pdc(struct atmel_mci *host)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001258{
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001259 atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001260}
1261
Ludovic Desroches796211b2011-08-11 15:25:44 +00001262static void atmci_stop_transfer_dma(struct atmel_mci *host)
1263{
1264 struct dma_chan *chan = host->data_chan;
1265
1266 if (chan) {
1267 dmaengine_terminate_all(chan);
1268 atmci_dma_cleanup(host);
1269 } else {
1270 /* Data transfer was stopped by the interrupt handler */
Ludovic Desroches6801c412012-05-16 15:26:01 +02001271 dev_dbg(&host->pdev->dev,
1272 "(%s) set pending xfer complete\n", __func__);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001273 atmci_set_pending(host, EVENT_XFER_COMPLETE);
1274 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1275 }
1276}
1277
1278/*
1279 * Start a request: prepare data if needed, prepare the command and activate
1280 * interrupts.
1281 */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001282static void atmci_start_request(struct atmel_mci *host,
1283 struct atmel_mci_slot *slot)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001284{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001285 struct mmc_request *mrq;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001286 struct mmc_command *cmd;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001287 struct mmc_data *data;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001288 u32 iflags;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001289 u32 cmdflags;
1290
1291 mrq = slot->mrq;
1292 host->cur_slot = slot;
1293 host->mrq = mrq;
1294
1295 host->pending_events = 0;
1296 host->completed_events = 0;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001297 host->cmd_status = 0;
Haavard Skinnemoenca55f462008-10-05 15:16:59 +02001298 host->data_status = 0;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001299
Ludovic Desroches6801c412012-05-16 15:26:01 +02001300 dev_dbg(&host->pdev->dev, "start request: cmd %u\n", mrq->cmd->opcode);
1301
Ludovic Desroches24011f32012-05-16 15:26:00 +02001302 if (host->need_reset || host->caps.need_reset_after_xfer) {
Ludovic Desroches18ee6842012-02-09 11:55:29 +01001303 iflags = atmci_readl(host, ATMCI_IMR);
1304 iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001305 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1306 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1307 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001308 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001309 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Ludovic Desroches18ee6842012-02-09 11:55:29 +01001310 atmci_writel(host, ATMCI_IER, iflags);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001311 host->need_reset = false;
1312 }
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001313 atmci_writel(host, ATMCI_SDCR, slot->sdc_reg);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001314
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001315 iflags = atmci_readl(host, ATMCI_IMR);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001316 if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001317 dev_dbg(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n",
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001318 iflags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001319
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001320 if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) {
1321 /* Send init sequence (74 clock cycles) */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001322 atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT);
1323 while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY))
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001324 cpu_relax();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001325 }
Nicolas Ferre74791a22009-12-14 18:01:31 -08001326 iflags = 0;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001327 data = mrq->data;
1328 if (data) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001329 atmci_set_timeout(host, slot, data);
Haavard Skinnemoena252e3e2008-10-03 14:46:17 +02001330
1331 /* Must set block count/size before sending command */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001332 atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001333 | ATMCI_BLKLEN(data->blksz));
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001334 dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n",
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001335 ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz));
Nicolas Ferre74791a22009-12-14 18:01:31 -08001336
Ludovic Desroches796211b2011-08-11 15:25:44 +00001337 iflags |= host->prepare_data(host, data);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001338 }
1339
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001340 iflags |= ATMCI_CMDRDY;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001341 cmd = mrq->cmd;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001342 cmdflags = atmci_prepare_command(slot->mmc, cmd);
Ludovic Desroches66b512e2013-11-20 16:01:11 +01001343
1344 /*
1345 * DMA transfer should be started before sending the command to avoid
1346 * unexpected errors especially for read operations in SDIO mode.
1347 * Unfortunately, in PDC mode, command has to be sent before starting
1348 * the transfer.
1349 */
1350 if (host->submit_data != &atmci_submit_data_dma)
1351 atmci_send_command(host, cmd, cmdflags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001352
1353 if (data)
Ludovic Desroches796211b2011-08-11 15:25:44 +00001354 host->submit_data(host, data);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001355
Ludovic Desroches66b512e2013-11-20 16:01:11 +01001356 if (host->submit_data == &atmci_submit_data_dma)
1357 atmci_send_command(host, cmd, cmdflags);
1358
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001359 if (mrq->stop) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001360 host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001361 host->stop_cmdr |= ATMCI_CMDR_STOP_XFER;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001362 if (!(data->flags & MMC_DATA_WRITE))
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001363 host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001364 if (data->flags & MMC_DATA_STREAM)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001365 host->stop_cmdr |= ATMCI_CMDR_STREAM;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001366 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001367 host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001368 }
1369
1370 /*
1371 * We could have enabled interrupts earlier, but I suspect
1372 * that would open up a nice can of interesting race
1373 * conditions (e.g. command and data complete, but stop not
1374 * prepared yet.)
1375 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001376 atmci_writel(host, ATMCI_IER, iflags);
Ludovic Desroches24011f32012-05-16 15:26:00 +02001377
1378 mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000));
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001379}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001380
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001381static void atmci_queue_request(struct atmel_mci *host,
1382 struct atmel_mci_slot *slot, struct mmc_request *mrq)
1383{
1384 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1385 host->state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001386
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001387 spin_lock_bh(&host->lock);
1388 slot->mrq = mrq;
1389 if (host->state == STATE_IDLE) {
1390 host->state = STATE_SENDING_CMD;
1391 atmci_start_request(host, slot);
1392 } else {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001393 dev_dbg(&host->pdev->dev, "queue request\n");
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001394 list_add_tail(&slot->queue_node, &host->queue);
1395 }
1396 spin_unlock_bh(&host->lock);
1397}
1398
1399static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1400{
1401 struct atmel_mci_slot *slot = mmc_priv(mmc);
1402 struct atmel_mci *host = slot->host;
1403 struct mmc_data *data;
1404
1405 WARN_ON(slot->mrq);
Ludovic Desroches6801c412012-05-16 15:26:01 +02001406 dev_dbg(&host->pdev->dev, "MRQ: cmd %u\n", mrq->cmd->opcode);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001407
Wenyou Yangae552ab2014-10-30 12:00:41 +08001408 pm_runtime_get_sync(&host->pdev->dev);
1409
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001410 /*
1411 * We may "know" the card is gone even though there's still an
1412 * electrical connection. If so, we really need to communicate
1413 * this to the MMC core since there won't be any more
1414 * interrupts as the card is completely removed. Otherwise,
1415 * the MMC core might believe the card is still there even
1416 * though the card was just removed very slowly.
1417 */
1418 if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) {
1419 mrq->cmd->error = -ENOMEDIUM;
1420 mmc_request_done(mmc, mrq);
1421 return;
1422 }
1423
1424 /* We don't support multiple blocks of weird lengths. */
1425 data = mrq->data;
1426 if (data && data->blocks > 1 && data->blksz & 3) {
1427 mrq->cmd->error = -EINVAL;
1428 mmc_request_done(mmc, mrq);
1429 }
1430
1431 atmci_queue_request(host, slot, mrq);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001432}
1433
1434static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1435{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001436 struct atmel_mci_slot *slot = mmc_priv(mmc);
1437 struct atmel_mci *host = slot->host;
1438 unsigned int i;
Wenyou Yangae552ab2014-10-30 12:00:41 +08001439
1440 pm_runtime_get_sync(&host->pdev->dev);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001441
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001442 slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001443 switch (ios->bus_width) {
1444 case MMC_BUS_WIDTH_1:
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001445 slot->sdc_reg |= ATMCI_SDCBUS_1BIT;
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001446 break;
1447 case MMC_BUS_WIDTH_4:
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001448 slot->sdc_reg |= ATMCI_SDCBUS_4BIT;
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001449 break;
1450 }
1451
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001452 if (ios->clock) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001453 unsigned int clock_min = ~0U;
Ludovic Desroches60c8f782015-05-06 15:16:46 +02001454 int clkdiv;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001455
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001456 spin_lock_bh(&host->lock);
1457 if (!host->mode_reg) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001458 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1459 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001460 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001461 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001462 }
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001463
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001464 /*
1465 * Use mirror of ios->clock to prevent race with mmc
1466 * core ios update when finding the minimum.
1467 */
1468 slot->clock = ios->clock;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001469 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001470 if (host->slot[i] && host->slot[i]->clock
1471 && host->slot[i]->clock < clock_min)
1472 clock_min = host->slot[i]->clock;
1473 }
1474
1475 /* Calculate clock divider */
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01001476 if (host->caps.has_odd_clk_div) {
1477 clkdiv = DIV_ROUND_UP(host->bus_hz, clock_min) - 2;
Ludovic Desroches60c8f782015-05-06 15:16:46 +02001478 if (clkdiv < 0) {
1479 dev_warn(&mmc->class_dev,
1480 "clock %u too fast; using %lu\n",
1481 clock_min, host->bus_hz / 2);
1482 clkdiv = 0;
1483 } else if (clkdiv > 511) {
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01001484 dev_warn(&mmc->class_dev,
1485 "clock %u too slow; using %lu\n",
1486 clock_min, host->bus_hz / (511 + 2));
1487 clkdiv = 511;
1488 }
1489 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv >> 1)
1490 | ATMCI_MR_CLKODD(clkdiv & 1);
1491 } else {
1492 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1;
1493 if (clkdiv > 255) {
1494 dev_warn(&mmc->class_dev,
1495 "clock %u too slow; using %lu\n",
1496 clock_min, host->bus_hz / (2 * 256));
1497 clkdiv = 255;
1498 }
1499 host->mode_reg = ATMCI_MR_CLKDIV(clkdiv);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001500 }
1501
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001502 /*
1503 * WRPROOF and RDPROOF prevent overruns/underruns by
1504 * stopping the clock when the FIFO is full/empty.
1505 * This state is not expected to last for long.
1506 */
Ludovic Desroches796211b2011-08-11 15:25:44 +00001507 if (host->caps.has_rwproof)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001508 host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001509
Ludovic Desroches796211b2011-08-11 15:25:44 +00001510 if (host->caps.has_cfg_reg) {
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001511 /* setup High Speed mode in relation with card capacity */
1512 if (ios->timing == MMC_TIMING_SD_HS)
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001513 host->cfg_reg |= ATMCI_CFG_HSMODE;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001514 else
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001515 host->cfg_reg &= ~ATMCI_CFG_HSMODE;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001516 }
1517
1518 if (list_empty(&host->queue)) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001519 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001520 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001521 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001522 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001523 host->need_clock_update = true;
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001524 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001525
1526 spin_unlock_bh(&host->lock);
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001527 } else {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001528 bool any_slot_active = false;
1529
1530 spin_lock_bh(&host->lock);
1531 slot->clock = 0;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001532 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001533 if (host->slot[i] && host->slot[i]->clock) {
1534 any_slot_active = true;
1535 break;
1536 }
Haavard Skinnemoen945533b2008-10-03 17:48:16 +02001537 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001538 if (!any_slot_active) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001539 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001540 if (host->mode_reg) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001541 atmci_readl(host, ATMCI_MR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001542 }
1543 host->mode_reg = 0;
1544 }
1545 spin_unlock_bh(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001546 }
1547
1548 switch (ios->power_mode) {
Alexandre Belloni9e7861f2013-10-17 12:46:48 +02001549 case MMC_POWER_OFF:
1550 if (!IS_ERR(mmc->supply.vmmc))
1551 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1552 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001553 case MMC_POWER_UP:
1554 set_bit(ATMCI_CARD_NEED_INIT, &slot->flags);
Alexandre Belloni9e7861f2013-10-17 12:46:48 +02001555 if (!IS_ERR(mmc->supply.vmmc))
1556 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001557 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001558 default:
1559 /*
1560 * TODO: None of the currently available AVR32-based
1561 * boards allow MMC power to be turned off. Implement
1562 * power control when this can be tested properly.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001563 *
1564 * We also need to hook this into the clock management
1565 * somehow so that newly inserted cards aren't
1566 * subjected to a fast clock before we have a chance
1567 * to figure out what the maximum rate is. Currently,
1568 * there's no way to avoid this, and there never will
1569 * be for boards that don't support power control.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001570 */
1571 break;
1572 }
Wenyou Yangae552ab2014-10-30 12:00:41 +08001573
1574 pm_runtime_mark_last_busy(&host->pdev->dev);
1575 pm_runtime_put_autosuspend(&host->pdev->dev);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001576}
1577
1578static int atmci_get_ro(struct mmc_host *mmc)
1579{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001580 int read_only = -ENOSYS;
1581 struct atmel_mci_slot *slot = mmc_priv(mmc);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001582
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001583 if (gpio_is_valid(slot->wp_pin)) {
1584 read_only = gpio_get_value(slot->wp_pin);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001585 dev_dbg(&mmc->class_dev, "card is %s\n",
1586 read_only ? "read-only" : "read-write");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001587 }
1588
1589 return read_only;
1590}
1591
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001592static int atmci_get_cd(struct mmc_host *mmc)
1593{
1594 int present = -ENOSYS;
1595 struct atmel_mci_slot *slot = mmc_priv(mmc);
1596
1597 if (gpio_is_valid(slot->detect_pin)) {
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001598 present = !(gpio_get_value(slot->detect_pin) ^
1599 slot->detect_is_active_high);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001600 dev_dbg(&mmc->class_dev, "card is %spresent\n",
1601 present ? "" : "not ");
1602 }
1603
1604 return present;
1605}
1606
Anders Grahn88ff82e2010-05-26 14:42:01 -07001607static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1608{
1609 struct atmel_mci_slot *slot = mmc_priv(mmc);
1610 struct atmel_mci *host = slot->host;
1611
1612 if (enable)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001613 atmci_writel(host, ATMCI_IER, slot->sdio_irq);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001614 else
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001615 atmci_writel(host, ATMCI_IDR, slot->sdio_irq);
Anders Grahn88ff82e2010-05-26 14:42:01 -07001616}
1617
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001618static const struct mmc_host_ops atmci_ops = {
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001619 .request = atmci_request,
1620 .set_ios = atmci_set_ios,
1621 .get_ro = atmci_get_ro,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001622 .get_cd = atmci_get_cd,
Anders Grahn88ff82e2010-05-26 14:42:01 -07001623 .enable_sdio_irq = atmci_enable_sdio_irq,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001624};
1625
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001626/* Called with host->lock held */
1627static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
1628 __releases(&host->lock)
1629 __acquires(&host->lock)
1630{
1631 struct atmel_mci_slot *slot = NULL;
1632 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1633
1634 WARN_ON(host->cmd || host->data);
1635
1636 /*
1637 * Update the MMC clock rate if necessary. This may be
1638 * necessary if set_ios() is called when a different slot is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001639 * busy transferring data.
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001640 */
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001641 if (host->need_clock_update) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001642 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001643 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001644 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07001645 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001646
1647 host->cur_slot->mrq = NULL;
1648 host->mrq = NULL;
1649 if (!list_empty(&host->queue)) {
1650 slot = list_entry(host->queue.next,
1651 struct atmel_mci_slot, queue_node);
1652 list_del(&slot->queue_node);
1653 dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
1654 mmc_hostname(slot->mmc));
1655 host->state = STATE_SENDING_CMD;
1656 atmci_start_request(host, slot);
1657 } else {
1658 dev_vdbg(&host->pdev->dev, "list empty\n");
1659 host->state = STATE_IDLE;
1660 }
1661
Ludovic Desroches24011f32012-05-16 15:26:00 +02001662 del_timer(&host->timer);
1663
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001664 spin_unlock(&host->lock);
1665 mmc_request_done(prev_mmc, mrq);
1666 spin_lock(&host->lock);
Wenyou Yangae552ab2014-10-30 12:00:41 +08001667
1668 pm_runtime_mark_last_busy(&host->pdev->dev);
1669 pm_runtime_put_autosuspend(&host->pdev->dev);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001670}
1671
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001672static void atmci_command_complete(struct atmel_mci *host,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001673 struct mmc_command *cmd)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001674{
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001675 u32 status = host->cmd_status;
1676
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001677 /* Read the response from the card (up to 16 bytes) */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001678 cmd->resp[0] = atmci_readl(host, ATMCI_RSPR);
1679 cmd->resp[1] = atmci_readl(host, ATMCI_RSPR);
1680 cmd->resp[2] = atmci_readl(host, ATMCI_RSPR);
1681 cmd->resp[3] = atmci_readl(host, ATMCI_RSPR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001682
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001683 if (status & ATMCI_RTOE)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001684 cmd->error = -ETIMEDOUT;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001685 else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001686 cmd->error = -EILSEQ;
Ludovic Desroches2c96a292011-08-11 15:25:41 +00001687 else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001688 cmd->error = -EIO;
Ludovic Desroches24011f32012-05-16 15:26:00 +02001689 else if (host->mrq->data && (host->mrq->data->blksz & 3)) {
1690 if (host->caps.need_blksz_mul_4) {
1691 cmd->error = -EINVAL;
1692 host->need_reset = 1;
1693 }
1694 } else
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001695 cmd->error = 0;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001696}
1697
1698static void atmci_detect_change(unsigned long data)
1699{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001700 struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data;
1701 bool present;
1702 bool present_old;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001703
1704 /*
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001705 * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before
1706 * freeing the interrupt. We must not re-enable the interrupt
1707 * if it has been freed, and if we're shutting down, it
1708 * doesn't really matter whether the card is present or not.
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001709 */
1710 smp_rmb();
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001711 if (test_bit(ATMCI_SHUTDOWN, &slot->flags))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001712 return;
1713
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001714 enable_irq(gpio_to_irq(slot->detect_pin));
Jonas Larsson1c1452b2009-03-31 11:16:48 +02001715 present = !(gpio_get_value(slot->detect_pin) ^
1716 slot->detect_is_active_high);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001717 present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001718
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001719 dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n",
1720 present, present_old);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001721
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001722 if (present != present_old) {
1723 struct atmel_mci *host = slot->host;
1724 struct mmc_request *mrq;
1725
1726 dev_dbg(&slot->mmc->class_dev, "card %s\n",
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001727 present ? "inserted" : "removed");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001728
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001729 spin_lock(&host->lock);
1730
1731 if (!present)
1732 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
1733 else
1734 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001735
1736 /* Clean up queue if present */
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001737 mrq = slot->mrq;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001738 if (mrq) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001739 if (mrq == host->mrq) {
1740 /*
1741 * Reset controller to terminate any ongoing
1742 * commands or data transfers.
1743 */
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001744 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
1745 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
1746 atmci_writel(host, ATMCI_MR, host->mode_reg);
Ludovic Desroches796211b2011-08-11 15:25:44 +00001747 if (host->caps.has_cfg_reg)
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001748 atmci_writel(host, ATMCI_CFG, host->cfg_reg);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001749
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001750 host->data = NULL;
1751 host->cmd = NULL;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001752
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001753 switch (host->state) {
1754 case STATE_IDLE:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001755 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001756 case STATE_SENDING_CMD:
1757 mrq->cmd->error = -ENOMEDIUM;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001758 if (mrq->data)
1759 host->stop_transfer(host);
1760 break;
1761 case STATE_DATA_XFER:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001762 mrq->data->error = -ENOMEDIUM;
Ludovic Desroches796211b2011-08-11 15:25:44 +00001763 host->stop_transfer(host);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001764 break;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001765 case STATE_WAITING_NOTBUSY:
1766 mrq->data->error = -ENOMEDIUM;
1767 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001768 case STATE_SENDING_STOP:
1769 mrq->stop->error = -ENOMEDIUM;
1770 break;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001771 case STATE_END_REQUEST:
1772 break;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001773 }
1774
1775 atmci_request_end(host, mrq);
1776 } else {
1777 list_del(&slot->queue_node);
1778 mrq->cmd->error = -ENOMEDIUM;
1779 if (mrq->data)
1780 mrq->data->error = -ENOMEDIUM;
1781 if (mrq->stop)
1782 mrq->stop->error = -ENOMEDIUM;
1783
1784 spin_unlock(&host->lock);
1785 mmc_request_done(slot->mmc, mrq);
1786 spin_lock(&host->lock);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001787 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001788 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001789 spin_unlock(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001790
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001791 mmc_detect_change(slot->mmc, 0);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001792 }
1793}
1794
1795static void atmci_tasklet_func(unsigned long priv)
1796{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001797 struct atmel_mci *host = (struct atmel_mci *)priv;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001798 struct mmc_request *mrq = host->mrq;
1799 struct mmc_data *data = host->data;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001800 enum atmel_mci_state state = host->state;
1801 enum atmel_mci_state prev_state;
1802 u32 status;
1803
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001804 spin_lock(&host->lock);
1805
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001806 state = host->state;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001807
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001808 dev_vdbg(&host->pdev->dev,
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001809 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
1810 state, host->pending_events, host->completed_events,
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00001811 atmci_readl(host, ATMCI_IMR));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001812
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001813 do {
1814 prev_state = state;
Ludovic Desroches6801c412012-05-16 15:26:01 +02001815 dev_dbg(&host->pdev->dev, "FSM: state=%d\n", state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001816
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001817 switch (state) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001818 case STATE_IDLE:
1819 break;
1820
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001821 case STATE_SENDING_CMD:
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001822 /*
1823 * Command has been sent, we are waiting for command
1824 * ready. Then we have three next states possible:
1825 * END_REQUEST by default, WAITING_NOTBUSY if it's a
1826 * command needing it or DATA_XFER if there is data.
1827 */
Ludovic Desroches6801c412012-05-16 15:26:01 +02001828 dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001829 if (!atmci_test_and_clear_pending(host,
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001830 EVENT_CMD_RDY))
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001831 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001832
Ludovic Desroches6801c412012-05-16 15:26:01 +02001833 dev_dbg(&host->pdev->dev, "set completed cmd ready\n");
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001834 host->cmd = NULL;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001835 atmci_set_completed(host, EVENT_CMD_RDY);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001836 atmci_command_complete(host, mrq->cmd);
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001837 if (mrq->data) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001838 dev_dbg(&host->pdev->dev,
1839 "command with data transfer");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001840 /*
1841 * If there is a command error don't start
1842 * data transfer.
1843 */
1844 if (mrq->cmd->error) {
1845 host->stop_transfer(host);
1846 host->data = NULL;
1847 atmci_writel(host, ATMCI_IDR,
1848 ATMCI_TXRDY | ATMCI_RXRDY
1849 | ATMCI_DATA_ERROR_FLAGS);
1850 state = STATE_END_REQUEST;
1851 } else
1852 state = STATE_DATA_XFER;
1853 } else if ((!mrq->data) && (mrq->cmd->flags & MMC_RSP_BUSY)) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001854 dev_dbg(&host->pdev->dev,
1855 "command response need waiting notbusy");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001856 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1857 state = STATE_WAITING_NOTBUSY;
1858 } else
1859 state = STATE_END_REQUEST;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001860
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001861 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001862
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001863 case STATE_DATA_XFER:
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001864 if (atmci_test_and_clear_pending(host,
1865 EVENT_DATA_ERROR)) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02001866 dev_dbg(&host->pdev->dev, "set completed data error\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001867 atmci_set_completed(host, EVENT_DATA_ERROR);
1868 state = STATE_END_REQUEST;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001869 break;
1870 }
1871
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001872 /*
1873 * A data transfer is in progress. The event expected
1874 * to move to the next state depends of data transfer
1875 * type (PDC or DMA). Once transfer done we can move
1876 * to the next step which is WAITING_NOTBUSY in write
1877 * case and directly SENDING_STOP in read case.
1878 */
Ludovic Desroches6801c412012-05-16 15:26:01 +02001879 dev_dbg(&host->pdev->dev, "FSM: xfer complete?\n");
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001880 if (!atmci_test_and_clear_pending(host,
1881 EVENT_XFER_COMPLETE))
1882 break;
1883
Ludovic Desroches6801c412012-05-16 15:26:01 +02001884 dev_dbg(&host->pdev->dev,
1885 "(%s) set completed xfer complete\n",
1886 __func__);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001887 atmci_set_completed(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001888
Ludovic Desroches077d4072012-07-24 11:42:04 +02001889 if (host->caps.need_notbusy_for_read_ops ||
1890 (host->data->flags & MMC_DATA_WRITE)) {
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001891 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1892 state = STATE_WAITING_NOTBUSY;
1893 } else if (host->mrq->stop) {
1894 atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY);
1895 atmci_send_stop_cmd(host, data);
1896 state = STATE_SENDING_STOP;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001897 } else {
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001898 host->data = NULL;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001899 data->bytes_xfered = data->blocks * data->blksz;
1900 data->error = 0;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001901 state = STATE_END_REQUEST;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001902 }
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001903 break;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001904
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001905 case STATE_WAITING_NOTBUSY:
1906 /*
1907 * We can be in the state for two reasons: a command
1908 * requiring waiting not busy signal (stop command
1909 * included) or a write operation. In the latest case,
1910 * we need to send a stop command.
1911 */
Ludovic Desroches6801c412012-05-16 15:26:01 +02001912 dev_dbg(&host->pdev->dev, "FSM: not busy?\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001913 if (!atmci_test_and_clear_pending(host,
1914 EVENT_NOTBUSY))
1915 break;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001916
Ludovic Desroches6801c412012-05-16 15:26:01 +02001917 dev_dbg(&host->pdev->dev, "set completed not busy\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001918 atmci_set_completed(host, EVENT_NOTBUSY);
1919
1920 if (host->data) {
1921 /*
1922 * For some commands such as CMD53, even if
1923 * there is data transfer, there is no stop
1924 * command to send.
1925 */
1926 if (host->mrq->stop) {
1927 atmci_writel(host, ATMCI_IER,
1928 ATMCI_CMDRDY);
1929 atmci_send_stop_cmd(host, data);
1930 state = STATE_SENDING_STOP;
1931 } else {
1932 host->data = NULL;
1933 data->bytes_xfered = data->blocks
1934 * data->blksz;
1935 data->error = 0;
1936 state = STATE_END_REQUEST;
1937 }
1938 } else
1939 state = STATE_END_REQUEST;
1940 break;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001941
1942 case STATE_SENDING_STOP:
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001943 /*
1944 * In this state, it is important to set host->data to
1945 * NULL (which is tested in the waiting notbusy state)
1946 * in order to go to the end request state instead of
1947 * sending stop again.
1948 */
Ludovic Desroches6801c412012-05-16 15:26:01 +02001949 dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n");
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001950 if (!atmci_test_and_clear_pending(host,
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001951 EVENT_CMD_RDY))
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001952 break;
1953
Ludovic Desroches6801c412012-05-16 15:26:01 +02001954 dev_dbg(&host->pdev->dev, "FSM: cmd ready\n");
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001955 host->cmd = NULL;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001956 data->bytes_xfered = data->blocks * data->blksz;
1957 data->error = 0;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001958 atmci_command_complete(host, mrq->stop);
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001959 if (mrq->stop->error) {
1960 host->stop_transfer(host);
1961 atmci_writel(host, ATMCI_IDR,
1962 ATMCI_TXRDY | ATMCI_RXRDY
1963 | ATMCI_DATA_ERROR_FLAGS);
1964 state = STATE_END_REQUEST;
1965 } else {
1966 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
1967 state = STATE_WAITING_NOTBUSY;
1968 }
Nicolas Ferre41b4e9a2012-07-06 11:58:33 +02001969 host->data = NULL;
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001970 break;
1971
1972 case STATE_END_REQUEST:
1973 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY | ATMCI_RXRDY
1974 | ATMCI_DATA_ERROR_FLAGS);
1975 status = host->data_status;
1976 if (unlikely(status)) {
1977 host->stop_transfer(host);
1978 host->data = NULL;
Rodolfo Giomettifbd986c2013-09-09 17:31:59 +02001979 if (data) {
1980 if (status & ATMCI_DTOE) {
1981 data->error = -ETIMEDOUT;
1982 } else if (status & ATMCI_DCRCE) {
1983 data->error = -EILSEQ;
1984 } else {
1985 data->error = -EIO;
1986 }
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001987 }
1988 }
1989
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001990 atmci_request_end(host, host->mrq);
Ludovic Desrochesf5177542012-05-16 15:25:59 +02001991 state = STATE_IDLE;
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001992 break;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001993 }
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001994 } while (state != prev_state);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001995
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02001996 host->state = state;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001997
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02001998 spin_unlock(&host->lock);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001999}
2000
2001static void atmci_read_data_pio(struct atmel_mci *host)
2002{
2003 struct scatterlist *sg = host->sg;
2004 void *buf = sg_virt(sg);
2005 unsigned int offset = host->pio_offset;
2006 struct mmc_data *data = host->data;
2007 u32 value;
2008 u32 status;
2009 unsigned int nbytes = 0;
2010
2011 do {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002012 value = atmci_readl(host, ATMCI_RDR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002013 if (likely(offset + 4 <= sg->length)) {
2014 put_unaligned(value, (u32 *)(buf + offset));
2015
2016 offset += 4;
2017 nbytes += 4;
2018
2019 if (offset == sg->length) {
Haavard Skinnemoen5e7184a2008-10-05 15:27:50 +02002020 flush_dcache_page(sg_page(sg));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002021 host->sg = sg = sg_next(sg);
Terry Barnabybdbc5d02013-04-08 12:05:47 -04002022 host->sg_len--;
2023 if (!sg || !host->sg_len)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002024 goto done;
2025
2026 offset = 0;
2027 buf = sg_virt(sg);
2028 }
2029 } else {
2030 unsigned int remaining = sg->length - offset;
2031 memcpy(buf + offset, &value, remaining);
2032 nbytes += remaining;
2033
2034 flush_dcache_page(sg_page(sg));
2035 host->sg = sg = sg_next(sg);
Terry Barnabybdbc5d02013-04-08 12:05:47 -04002036 host->sg_len--;
2037 if (!sg || !host->sg_len)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002038 goto done;
2039
2040 offset = 4 - remaining;
2041 buf = sg_virt(sg);
2042 memcpy(buf, (u8 *)&value + remaining, offset);
2043 nbytes += offset;
2044 }
2045
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002046 status = atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002047 if (status & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002048 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002049 | ATMCI_DATA_ERROR_FLAGS));
2050 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002051 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002052 return;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002053 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002054 } while (status & ATMCI_RXRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002055
2056 host->pio_offset = offset;
2057 data->bytes_xfered += nbytes;
2058
2059 return;
2060
2061done:
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002062 atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY);
2063 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002064 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002065 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02002066 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002067}
2068
2069static void atmci_write_data_pio(struct atmel_mci *host)
2070{
2071 struct scatterlist *sg = host->sg;
2072 void *buf = sg_virt(sg);
2073 unsigned int offset = host->pio_offset;
2074 struct mmc_data *data = host->data;
2075 u32 value;
2076 u32 status;
2077 unsigned int nbytes = 0;
2078
2079 do {
2080 if (likely(offset + 4 <= sg->length)) {
2081 value = get_unaligned((u32 *)(buf + offset));
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002082 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002083
2084 offset += 4;
2085 nbytes += 4;
2086 if (offset == sg->length) {
2087 host->sg = sg = sg_next(sg);
Terry Barnabybdbc5d02013-04-08 12:05:47 -04002088 host->sg_len--;
2089 if (!sg || !host->sg_len)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002090 goto done;
2091
2092 offset = 0;
2093 buf = sg_virt(sg);
2094 }
2095 } else {
2096 unsigned int remaining = sg->length - offset;
2097
2098 value = 0;
2099 memcpy(&value, buf + offset, remaining);
2100 nbytes += remaining;
2101
2102 host->sg = sg = sg_next(sg);
Terry Barnabybdbc5d02013-04-08 12:05:47 -04002103 host->sg_len--;
2104 if (!sg || !host->sg_len) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002105 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002106 goto done;
2107 }
2108
2109 offset = 4 - remaining;
2110 buf = sg_virt(sg);
2111 memcpy((u8 *)&value + remaining, buf, offset);
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002112 atmci_writel(host, ATMCI_TDR, value);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002113 nbytes += offset;
2114 }
2115
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002116 status = atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002117 if (status & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002118 atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002119 | ATMCI_DATA_ERROR_FLAGS));
2120 host->data_status = status;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002121 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002122 return;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002123 }
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002124 } while (status & ATMCI_TXRDY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002125
2126 host->pio_offset = offset;
2127 data->bytes_xfered += nbytes;
2128
2129 return;
2130
2131done:
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002132 atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY);
2133 atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002134 data->bytes_xfered += nbytes;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002135 smp_wmb();
Haavard Skinnemoenc06ad252008-07-31 14:49:16 +02002136 atmci_set_pending(host, EVENT_XFER_COMPLETE);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002137}
2138
Anders Grahn88ff82e2010-05-26 14:42:01 -07002139static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status)
2140{
2141 int i;
2142
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002143 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Anders Grahn88ff82e2010-05-26 14:42:01 -07002144 struct atmel_mci_slot *slot = host->slot[i];
2145 if (slot && (status & slot->sdio_irq)) {
2146 mmc_signal_sdio_irq(slot->mmc);
2147 }
2148 }
2149}
2150
2151
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002152static irqreturn_t atmci_interrupt(int irq, void *dev_id)
2153{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002154 struct atmel_mci *host = dev_id;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002155 u32 status, mask, pending;
2156 unsigned int pass_count = 0;
2157
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002158 do {
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002159 status = atmci_readl(host, ATMCI_SR);
2160 mask = atmci_readl(host, ATMCI_IMR);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002161 pending = status & mask;
2162 if (!pending)
2163 break;
2164
2165 if (pending & ATMCI_DATA_ERROR_FLAGS) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002166 dev_dbg(&host->pdev->dev, "IRQ: data error\n");
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002167 atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002168 | ATMCI_RXRDY | ATMCI_TXRDY
2169 | ATMCI_ENDRX | ATMCI_ENDTX
2170 | ATMCI_RXBUFF | ATMCI_TXBUFE);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002171
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002172 host->data_status = status;
Ludovic Desroches6801c412012-05-16 15:26:01 +02002173 dev_dbg(&host->pdev->dev, "set pending data error\n");
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002174 smp_wmb();
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002175 atmci_set_pending(host, EVENT_DATA_ERROR);
2176 tasklet_schedule(&host->tasklet);
2177 }
Ludovic Desroches796211b2011-08-11 15:25:44 +00002178
Ludovic Desroches796211b2011-08-11 15:25:44 +00002179 if (pending & ATMCI_TXBUFE) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002180 dev_dbg(&host->pdev->dev, "IRQ: tx buffer empty\n");
Ludovic Desroches796211b2011-08-11 15:25:44 +00002181 atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE);
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00002182 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
Ludovic Desroches796211b2011-08-11 15:25:44 +00002183 /*
2184 * We can receive this interruption before having configured
2185 * the second pdc buffer, so we need to reconfigure first and
2186 * second buffers again
2187 */
2188 if (host->data_size) {
2189 atmci_pdc_set_both_buf(host, XFER_TRANSMIT);
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00002190 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
Ludovic Desroches796211b2011-08-11 15:25:44 +00002191 atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE);
2192 } else {
2193 atmci_pdc_complete(host);
2194 }
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00002195 } else if (pending & ATMCI_ENDTX) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002196 dev_dbg(&host->pdev->dev, "IRQ: end of tx buffer\n");
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00002197 atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX);
2198
2199 if (host->data_size) {
2200 atmci_pdc_set_single_buf(host,
2201 XFER_TRANSMIT, PDC_SECOND_BUF);
2202 atmci_writel(host, ATMCI_IER, ATMCI_ENDTX);
2203 }
Ludovic Desroches796211b2011-08-11 15:25:44 +00002204 }
2205
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00002206 if (pending & ATMCI_RXBUFF) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002207 dev_dbg(&host->pdev->dev, "IRQ: rx buffer full\n");
Ludovic Desroches7e8ba222011-08-11 15:25:48 +00002208 atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF);
2209 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
2210 /*
2211 * We can receive this interruption before having configured
2212 * the second pdc buffer, so we need to reconfigure first and
2213 * second buffers again
2214 */
2215 if (host->data_size) {
2216 atmci_pdc_set_both_buf(host, XFER_RECEIVE);
2217 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
2218 atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF);
2219 } else {
2220 atmci_pdc_complete(host);
2221 }
2222 } else if (pending & ATMCI_ENDRX) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002223 dev_dbg(&host->pdev->dev, "IRQ: end of rx buffer\n");
Ludovic Desroches796211b2011-08-11 15:25:44 +00002224 atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX);
2225
2226 if (host->data_size) {
2227 atmci_pdc_set_single_buf(host,
2228 XFER_RECEIVE, PDC_SECOND_BUF);
2229 atmci_writel(host, ATMCI_IER, ATMCI_ENDRX);
2230 }
2231 }
2232
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002233 /*
2234 * First mci IPs, so mainly the ones having pdc, have some
2235 * issues with the notbusy signal. You can't get it after
2236 * data transmission if you have not sent a stop command.
2237 * The appropriate workaround is to use the BLKE signal.
2238 */
2239 if (pending & ATMCI_BLKE) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002240 dev_dbg(&host->pdev->dev, "IRQ: blke\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002241 atmci_writel(host, ATMCI_IDR, ATMCI_BLKE);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002242 smp_wmb();
Ludovic Desroches6801c412012-05-16 15:26:01 +02002243 dev_dbg(&host->pdev->dev, "set pending notbusy\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002244 atmci_set_pending(host, EVENT_NOTBUSY);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002245 tasklet_schedule(&host->tasklet);
2246 }
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002247
2248 if (pending & ATMCI_NOTBUSY) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002249 dev_dbg(&host->pdev->dev, "IRQ: not_busy\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002250 atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY);
2251 smp_wmb();
Ludovic Desroches6801c412012-05-16 15:26:01 +02002252 dev_dbg(&host->pdev->dev, "set pending notbusy\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002253 atmci_set_pending(host, EVENT_NOTBUSY);
2254 tasklet_schedule(&host->tasklet);
2255 }
2256
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002257 if (pending & ATMCI_RXRDY)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002258 atmci_read_data_pio(host);
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002259 if (pending & ATMCI_TXRDY)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002260 atmci_write_data_pio(host);
2261
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002262 if (pending & ATMCI_CMDRDY) {
Ludovic Desroches6801c412012-05-16 15:26:01 +02002263 dev_dbg(&host->pdev->dev, "IRQ: cmd ready\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002264 atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY);
2265 host->cmd_status = status;
2266 smp_wmb();
Ludovic Desroches6801c412012-05-16 15:26:01 +02002267 dev_dbg(&host->pdev->dev, "set pending cmd rdy\n");
Ludovic Desrochesf5177542012-05-16 15:25:59 +02002268 atmci_set_pending(host, EVENT_CMD_RDY);
2269 tasklet_schedule(&host->tasklet);
2270 }
Anders Grahn88ff82e2010-05-26 14:42:01 -07002271
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002272 if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB))
Anders Grahn88ff82e2010-05-26 14:42:01 -07002273 atmci_sdio_interrupt(host, status);
2274
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002275 } while (pass_count++ < 5);
2276
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002277 return pass_count ? IRQ_HANDLED : IRQ_NONE;
2278}
2279
2280static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
2281{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002282 struct atmel_mci_slot *slot = dev_id;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002283
2284 /*
2285 * Disable interrupts until the pin has stabilized and check
2286 * the state then. Use mod_timer() since we may be in the
2287 * middle of the timer routine when this interrupt triggers.
2288 */
2289 disable_irq_nosync(irq);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002290 mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002291
2292 return IRQ_HANDLED;
2293}
2294
ludovic.desroches@atmel.comab050b92014-12-01 15:35:07 +01002295static int atmci_init_slot(struct atmel_mci *host,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002296 struct mci_slot_pdata *slot_data, unsigned int id,
Anders Grahn88ff82e2010-05-26 14:42:01 -07002297 u32 sdc_reg, u32 sdio_irq)
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002298{
2299 struct mmc_host *mmc;
2300 struct atmel_mci_slot *slot;
2301
2302 mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev);
2303 if (!mmc)
2304 return -ENOMEM;
2305
2306 slot = mmc_priv(mmc);
2307 slot->mmc = mmc;
2308 slot->host = host;
2309 slot->detect_pin = slot_data->detect_pin;
2310 slot->wp_pin = slot_data->wp_pin;
Jonas Larsson1c1452b2009-03-31 11:16:48 +02002311 slot->detect_is_active_high = slot_data->detect_is_active_high;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002312 slot->sdc_reg = sdc_reg;
Anders Grahn88ff82e2010-05-26 14:42:01 -07002313 slot->sdio_irq = sdio_irq;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002314
Ludovic Desrochese919fd22012-07-24 15:30:03 +02002315 dev_dbg(&mmc->class_dev,
2316 "slot[%u]: bus_width=%u, detect_pin=%d, "
2317 "detect_is_active_high=%s, wp_pin=%d\n",
2318 id, slot_data->bus_width, slot_data->detect_pin,
2319 slot_data->detect_is_active_high ? "true" : "false",
2320 slot_data->wp_pin);
2321
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002322 mmc->ops = &atmci_ops;
2323 mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512);
2324 mmc->f_max = host->bus_hz / 2;
2325 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Anders Grahn88ff82e2010-05-26 14:42:01 -07002326 if (sdio_irq)
2327 mmc->caps |= MMC_CAP_SDIO_IRQ;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002328 if (host->caps.has_highspeed)
Nicolas Ferre99ddffd2010-05-26 14:41:59 -07002329 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002330 /*
2331 * Without the read/write proof capability, it is strongly suggested to
2332 * use only one bit for data to prevent fifo underruns and overruns
2333 * which will corrupt data.
2334 */
2335 if ((slot_data->bus_width >= 4) && host->caps.has_rwproof)
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002336 mmc->caps |= MMC_CAP_4_BIT_DATA;
2337
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002338 if (atmci_get_version(host) < 0x200) {
2339 mmc->max_segs = 256;
2340 mmc->max_blk_size = 4095;
2341 mmc->max_blk_count = 256;
2342 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2343 mmc->max_seg_size = mmc->max_blk_size * mmc->max_segs;
2344 } else {
2345 mmc->max_segs = 64;
2346 mmc->max_req_size = 32768 * 512;
2347 mmc->max_blk_size = 32768;
2348 mmc->max_blk_count = 512;
2349 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002350
2351 /* Assume card is present initially */
2352 set_bit(ATMCI_CARD_PRESENT, &slot->flags);
2353 if (gpio_is_valid(slot->detect_pin)) {
Pramod Gurav7bca6462014-09-23 18:21:48 +05302354 if (devm_gpio_request(&host->pdev->dev, slot->detect_pin,
2355 "mmc_detect")) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002356 dev_dbg(&mmc->class_dev, "no detect pin available\n");
2357 slot->detect_pin = -EBUSY;
Jonas Larsson1c1452b2009-03-31 11:16:48 +02002358 } else if (gpio_get_value(slot->detect_pin) ^
2359 slot->detect_is_active_high) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002360 clear_bit(ATMCI_CARD_PRESENT, &slot->flags);
2361 }
2362 }
2363
Timo Kokkonen76d55562014-11-03 13:12:59 +02002364 if (!gpio_is_valid(slot->detect_pin)) {
2365 if (slot_data->non_removable)
2366 mmc->caps |= MMC_CAP_NONREMOVABLE;
2367 else
2368 mmc->caps |= MMC_CAP_NEEDS_POLL;
2369 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002370
2371 if (gpio_is_valid(slot->wp_pin)) {
Pramod Gurav7bca6462014-09-23 18:21:48 +05302372 if (devm_gpio_request(&host->pdev->dev, slot->wp_pin,
2373 "mmc_wp")) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002374 dev_dbg(&mmc->class_dev, "no WP pin available\n");
2375 slot->wp_pin = -EBUSY;
2376 }
2377 }
2378
2379 host->slot[id] = slot;
Alexandre Belloni9e7861f2013-10-17 12:46:48 +02002380 mmc_regulator_get_supply(mmc);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002381 mmc_add_host(mmc);
2382
2383 if (gpio_is_valid(slot->detect_pin)) {
2384 int ret;
2385
2386 setup_timer(&slot->detect_timer, atmci_detect_change,
2387 (unsigned long)slot);
2388
2389 ret = request_irq(gpio_to_irq(slot->detect_pin),
2390 atmci_detect_interrupt,
2391 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
2392 "mmc-detect", slot);
2393 if (ret) {
2394 dev_dbg(&mmc->class_dev,
2395 "could not request IRQ %d for detect pin\n",
2396 gpio_to_irq(slot->detect_pin));
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002397 slot->detect_pin = -EBUSY;
2398 }
2399 }
2400
2401 atmci_init_debugfs(slot);
2402
2403 return 0;
2404}
2405
Arnd Bergmann5fef3652014-09-26 21:34:58 +02002406static void atmci_cleanup_slot(struct atmel_mci_slot *slot,
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002407 unsigned int id)
2408{
2409 /* Debugfs stuff is cleaned up by mmc core */
2410
2411 set_bit(ATMCI_SHUTDOWN, &slot->flags);
2412 smp_wmb();
2413
2414 mmc_remove_host(slot->mmc);
2415
2416 if (gpio_is_valid(slot->detect_pin)) {
2417 int pin = slot->detect_pin;
2418
2419 free_irq(gpio_to_irq(pin), slot);
2420 del_timer_sync(&slot->detect_timer);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002421 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002422
2423 slot->host->slot[id] = NULL;
2424 mmc_free_host(slot->mmc);
2425}
2426
ludovic.desroches@atmel.com467e0812014-12-01 15:35:09 +01002427static int atmci_configure_dma(struct atmel_mci *host)
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002428{
ludovic.desroches@atmel.com467e0812014-12-01 15:35:09 +01002429 host->dma.chan = dma_request_slave_channel_reason(&host->pdev->dev,
2430 "rxtx");
2431 if (IS_ERR(host->dma.chan))
2432 return PTR_ERR(host->dma.chan);
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002433
ludovic.desroches@atmel.com467e0812014-12-01 15:35:09 +01002434 dev_info(&host->pdev->dev, "using %s for DMA transfers\n",
2435 dma_chan_name(host->dma.chan));
Viresh Kumare2b35f32012-02-01 16:12:27 +05302436
ludovic.desroches@atmel.com467e0812014-12-01 15:35:09 +01002437 host->dma_conf.src_addr = host->mapbase + ATMCI_RDR;
2438 host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2439 host->dma_conf.src_maxburst = 1;
2440 host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR;
2441 host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2442 host->dma_conf.dst_maxburst = 1;
2443 host->dma_conf.device_fc = false;
2444
2445 return 0;
Nicolas Ferre2635d1b2009-12-14 18:01:30 -08002446}
Ludovic Desroches796211b2011-08-11 15:25:44 +00002447
Ludovic Desroches796211b2011-08-11 15:25:44 +00002448/*
2449 * HSMCI (High Speed MCI) module is not fully compatible with MCI module.
2450 * HSMCI provides DMA support and a new config register but no more supports
2451 * PDC.
2452 */
ludovic.desroches@atmel.comab050b92014-12-01 15:35:07 +01002453static void atmci_get_cap(struct atmel_mci *host)
Ludovic Desroches796211b2011-08-11 15:25:44 +00002454{
2455 unsigned int version;
2456
2457 version = atmci_get_version(host);
2458 dev_info(&host->pdev->dev,
2459 "version: 0x%x\n", version);
2460
Hein_Tiboschccdfe612012-08-30 16:34:38 +00002461 host->caps.has_dma_conf_reg = 0;
Hein_Tibosch6bf2af82012-08-30 16:34:27 +00002462 host->caps.has_pdc = ATMCI_PDC_CONNECTED;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002463 host->caps.has_cfg_reg = 0;
2464 host->caps.has_cstor_reg = 0;
2465 host->caps.has_highspeed = 0;
2466 host->caps.has_rwproof = 0;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002467 host->caps.has_odd_clk_div = 0;
Ludovic Desroches24011f32012-05-16 15:26:00 +02002468 host->caps.has_bad_data_ordering = 1;
2469 host->caps.need_reset_after_xfer = 1;
2470 host->caps.need_blksz_mul_4 = 1;
Ludovic Desroches077d4072012-07-24 11:42:04 +02002471 host->caps.need_notbusy_for_read_ops = 0;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002472
2473 /* keep only major version number */
2474 switch (version & 0xf00) {
Nicolas Ferre215ba392014-06-12 09:47:45 +02002475 case 0x600:
Ludovic Desroches796211b2011-08-11 15:25:44 +00002476 case 0x500:
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002477 host->caps.has_odd_clk_div = 1;
2478 case 0x400:
2479 case 0x300:
Hein_Tiboschccdfe612012-08-30 16:34:38 +00002480 host->caps.has_dma_conf_reg = 1;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002481 host->caps.has_pdc = 0;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002482 host->caps.has_cfg_reg = 1;
2483 host->caps.has_cstor_reg = 1;
2484 host->caps.has_highspeed = 1;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002485 case 0x200:
Ludovic Desroches796211b2011-08-11 15:25:44 +00002486 host->caps.has_rwproof = 1;
Ludovic Desroches24011f32012-05-16 15:26:00 +02002487 host->caps.need_blksz_mul_4 = 0;
Ludovic Desroches077d4072012-07-24 11:42:04 +02002488 host->caps.need_notbusy_for_read_ops = 1;
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002489 case 0x100:
Ludovic Desroches24011f32012-05-16 15:26:00 +02002490 host->caps.has_bad_data_ordering = 0;
2491 host->caps.need_reset_after_xfer = 0;
2492 case 0x0:
Ludovic Desroches796211b2011-08-11 15:25:44 +00002493 break;
2494 default:
Ludovic Desrochesfaf81802012-03-21 16:41:23 +01002495 host->caps.has_pdc = 0;
Ludovic Desroches796211b2011-08-11 15:25:44 +00002496 dev_warn(&host->pdev->dev,
2497 "Unmanaged mci version, set minimum capabilities\n");
2498 break;
2499 }
2500}
Dan Williams74465b42009-01-06 11:38:16 -07002501
ludovic.desroches@atmel.comab050b92014-12-01 15:35:07 +01002502static int atmci_probe(struct platform_device *pdev)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002503{
2504 struct mci_platform_data *pdata;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002505 struct atmel_mci *host;
2506 struct resource *regs;
2507 unsigned int nr_slots;
2508 int irq;
Pramod Gurav528bc782014-09-23 15:50:06 +05302509 int ret, i;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002510
2511 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2512 if (!regs)
2513 return -ENXIO;
2514 pdata = pdev->dev.platform_data;
Ludovic Desrochese919fd22012-07-24 15:30:03 +02002515 if (!pdata) {
2516 pdata = atmci_of_init(pdev);
2517 if (IS_ERR(pdata)) {
2518 dev_err(&pdev->dev, "platform data not available\n");
2519 return PTR_ERR(pdata);
2520 }
2521 }
2522
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002523 irq = platform_get_irq(pdev, 0);
2524 if (irq < 0)
2525 return irq;
2526
Pramod Gurav7bca6462014-09-23 18:21:48 +05302527 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002528 if (!host)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002529 return -ENOMEM;
2530
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002531 host->pdev = pdev;
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002532 spin_lock_init(&host->lock);
2533 INIT_LIST_HEAD(&host->queue);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002534
Pramod Gurav7bca6462014-09-23 18:21:48 +05302535 host->mck = devm_clk_get(&pdev->dev, "mci_clk");
2536 if (IS_ERR(host->mck))
2537 return PTR_ERR(host->mck);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002538
Pramod Gurav7bca6462014-09-23 18:21:48 +05302539 host->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002540 if (!host->regs)
Pramod Gurav7bca6462014-09-23 18:21:48 +05302541 return -ENOMEM;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002542
Boris BREZILLONb3894f22013-07-18 09:38:52 +02002543 ret = clk_prepare_enable(host->mck);
2544 if (ret)
Pramod Gurav7bca6462014-09-23 18:21:48 +05302545 return ret;
2546
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002547 atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002548 host->bus_hz = clk_get_rate(host->mck);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002549
2550 host->mapbase = regs->start;
2551
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002552 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002553
Kay Sievers89c8aa22009-02-02 21:08:30 +01002554 ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
Wenyou Yangae552ab2014-10-30 12:00:41 +08002555 if (ret) {
2556 clk_disable_unprepare(host->mck);
Pramod Gurav7bca6462014-09-23 18:21:48 +05302557 return ret;
Wenyou Yangae552ab2014-10-30 12:00:41 +08002558 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002559
Ludovic Desroches796211b2011-08-11 15:25:44 +00002560 /* Get MCI capabilities and set operations according to it */
2561 atmci_get_cap(host);
ludovic.desroches@atmel.com467e0812014-12-01 15:35:09 +01002562 ret = atmci_configure_dma(host);
2563 if (ret == -EPROBE_DEFER)
2564 goto err_dma_probe_defer;
2565 if (ret == 0) {
Ludovic Desroches796211b2011-08-11 15:25:44 +00002566 host->prepare_data = &atmci_prepare_data_dma;
2567 host->submit_data = &atmci_submit_data_dma;
2568 host->stop_transfer = &atmci_stop_transfer_dma;
2569 } else if (host->caps.has_pdc) {
2570 dev_info(&pdev->dev, "using PDC\n");
2571 host->prepare_data = &atmci_prepare_data_pdc;
2572 host->submit_data = &atmci_submit_data_pdc;
2573 host->stop_transfer = &atmci_stop_transfer_pdc;
2574 } else {
Ludovic Desrochesef878192012-02-09 16:33:53 +01002575 dev_info(&pdev->dev, "using PIO\n");
Ludovic Desroches796211b2011-08-11 15:25:44 +00002576 host->prepare_data = &atmci_prepare_data;
2577 host->submit_data = &atmci_submit_data;
2578 host->stop_transfer = &atmci_stop_transfer;
2579 }
2580
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002581 platform_set_drvdata(pdev, host);
2582
Ludovic Desrochesb87cc1b2012-05-23 15:52:15 +02002583 setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host);
2584
Wenyou Yangae552ab2014-10-30 12:00:41 +08002585 pm_runtime_get_noresume(&pdev->dev);
2586 pm_runtime_set_active(&pdev->dev);
2587 pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_DELAY);
2588 pm_runtime_use_autosuspend(&pdev->dev);
2589 pm_runtime_enable(&pdev->dev);
2590
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002591 /* We need at least one slot to succeed */
2592 nr_slots = 0;
2593 ret = -ENODEV;
2594 if (pdata->slot[0].bus_width) {
2595 ret = atmci_init_slot(host, &pdata->slot[0],
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002596 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002597 if (!ret) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002598 nr_slots++;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002599 host->buf_size = host->slot[0]->mmc->max_req_size;
2600 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002601 }
2602 if (pdata->slot[1].bus_width) {
2603 ret = atmci_init_slot(host, &pdata->slot[1],
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002604 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB);
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002605 if (!ret) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002606 nr_slots++;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002607 if (host->slot[1]->mmc->max_req_size > host->buf_size)
2608 host->buf_size =
2609 host->slot[1]->mmc->max_req_size;
2610 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002611 }
2612
Rob Emanuele04d699c2009-09-22 16:45:19 -07002613 if (!nr_slots) {
2614 dev_err(&pdev->dev, "init failed: no slot defined\n");
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002615 goto err_init_slot;
Rob Emanuele04d699c2009-09-22 16:45:19 -07002616 }
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002617
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002618 if (!host->caps.has_rwproof) {
2619 host->buffer = dma_alloc_coherent(&pdev->dev, host->buf_size,
2620 &host->buf_phys_addr,
2621 GFP_KERNEL);
2622 if (!host->buffer) {
2623 ret = -ENOMEM;
2624 dev_err(&pdev->dev, "buffer allocation failed\n");
Pramod Gurav528bc782014-09-23 15:50:06 +05302625 goto err_dma_alloc;
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002626 }
2627 }
2628
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002629 dev_info(&pdev->dev,
2630 "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
2631 host->mapbase, irq, nr_slots);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +02002632
Wenyou Yangae552ab2014-10-30 12:00:41 +08002633 pm_runtime_mark_last_busy(&host->pdev->dev);
2634 pm_runtime_put_autosuspend(&pdev->dev);
2635
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002636 return 0;
2637
Pramod Gurav528bc782014-09-23 15:50:06 +05302638err_dma_alloc:
2639 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
2640 if (host->slot[i])
2641 atmci_cleanup_slot(host->slot[i], i);
2642 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002643err_init_slot:
Wenyou Yangae552ab2014-10-30 12:00:41 +08002644 clk_disable_unprepare(host->mck);
2645
2646 pm_runtime_disable(&pdev->dev);
2647 pm_runtime_put_noidle(&pdev->dev);
2648
Pramod Gurav528bc782014-09-23 15:50:06 +05302649 del_timer_sync(&host->timer);
ludovic.desroches@atmel.com467e0812014-12-01 15:35:09 +01002650 if (!IS_ERR(host->dma.chan))
Dan Williams74465b42009-01-06 11:38:16 -07002651 dma_release_channel(host->dma.chan);
ludovic.desroches@atmel.com467e0812014-12-01 15:35:09 +01002652err_dma_probe_defer:
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002653 free_irq(irq, host);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002654 return ret;
2655}
2656
ludovic.desroches@atmel.comab050b92014-12-01 15:35:07 +01002657static int atmci_remove(struct platform_device *pdev)
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002658{
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002659 struct atmel_mci *host = platform_get_drvdata(pdev);
2660 unsigned int i;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002661
Wenyou Yangae552ab2014-10-30 12:00:41 +08002662 pm_runtime_get_sync(&pdev->dev);
2663
Ludovic Desroches7a90dcc2012-05-16 15:25:58 +02002664 if (host->buffer)
2665 dma_free_coherent(&pdev->dev, host->buf_size,
2666 host->buffer, host->buf_phys_addr);
2667
Ludovic Desroches2c96a292011-08-11 15:25:41 +00002668 for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002669 if (host->slot[i])
2670 atmci_cleanup_slot(host->slot[i], i);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002671 }
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002672
Ludovic Desroches03fc9a72011-08-11 15:25:42 +00002673 atmci_writel(host, ATMCI_IDR, ~0UL);
2674 atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
2675 atmci_readl(host, ATMCI_SR);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002676
Pramod Gurav528bc782014-09-23 15:50:06 +05302677 del_timer_sync(&host->timer);
ludovic.desroches@atmel.com467e0812014-12-01 15:35:09 +01002678 if (!IS_ERR(host->dma.chan))
Dan Williams74465b42009-01-06 11:38:16 -07002679 dma_release_channel(host->dma.chan);
Haavard Skinnemoen65e8b082008-07-30 20:29:03 +02002680
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002681 free_irq(platform_get_irq(pdev, 0), host);
Haavard Skinnemoen965ebf32008-09-17 20:53:55 +02002682
Wenyou Yangae552ab2014-10-30 12:00:41 +08002683 clk_disable_unprepare(host->mck);
2684
2685 pm_runtime_disable(&pdev->dev);
2686 pm_runtime_put_noidle(&pdev->dev);
2687
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002688 return 0;
2689}
2690
Wenyou Yangae552ab2014-10-30 12:00:41 +08002691#ifdef CONFIG_PM
2692static int atmci_runtime_suspend(struct device *dev)
2693{
2694 struct atmel_mci *host = dev_get_drvdata(dev);
2695
2696 clk_disable_unprepare(host->mck);
2697
Wenyou Yangb5b64fa2014-11-07 08:48:13 +08002698 pinctrl_pm_select_sleep_state(dev);
2699
Wenyou Yangae552ab2014-10-30 12:00:41 +08002700 return 0;
2701}
2702
2703static int atmci_runtime_resume(struct device *dev)
2704{
2705 struct atmel_mci *host = dev_get_drvdata(dev);
2706
Wenyou Yangb5b64fa2014-11-07 08:48:13 +08002707 pinctrl_pm_select_default_state(dev);
2708
Wenyou Yangae552ab2014-10-30 12:00:41 +08002709 return clk_prepare_enable(host->mck);
2710}
2711#endif
2712
2713static const struct dev_pm_ops atmci_dev_pm_ops = {
2714 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
2715 pm_runtime_force_resume)
Ludovic Desrochesc3cb6ba2014-12-13 00:44:11 +01002716 SET_RUNTIME_PM_OPS(atmci_runtime_suspend, atmci_runtime_resume, NULL)
Wenyou Yangae552ab2014-10-30 12:00:41 +08002717};
2718
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002719static struct platform_driver atmci_driver = {
ludovic.desroches@atmel.com5e0fe892014-12-01 15:35:08 +01002720 .probe = atmci_probe,
ludovic.desroches@atmel.comab050b92014-12-01 15:35:07 +01002721 .remove = atmci_remove,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002722 .driver = {
2723 .name = "atmel_mci",
Ludovic Desrochese919fd22012-07-24 15:30:03 +02002724 .of_match_table = of_match_ptr(atmci_dt_ids),
Wenyou Yangae552ab2014-10-30 12:00:41 +08002725 .pm = &atmci_dev_pm_ops,
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002726 },
2727};
ludovic.desroches@atmel.com5e0fe892014-12-01 15:35:08 +01002728module_platform_driver(atmci_driver);
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002729
2730MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002731MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02002732MODULE_LICENSE("GPL v2");