]>
Commit | Line | Data |
---|---|---|
7d2be074 HS |
1 | /* |
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> | |
deec9ae3 | 12 | #include <linux/debugfs.h> |
7d2be074 | 13 | #include <linux/device.h> |
65e8b083 HS |
14 | #include <linux/dmaengine.h> |
15 | #include <linux/dma-mapping.h> | |
fbfca4b8 | 16 | #include <linux/err.h> |
3c26e170 | 17 | #include <linux/gpio.h> |
7d2be074 HS |
18 | #include <linux/init.h> |
19 | #include <linux/interrupt.h> | |
20 | #include <linux/ioport.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/platform_device.h> | |
23 | #include <linux/scatterlist.h> | |
deec9ae3 | 24 | #include <linux/seq_file.h> |
5a0e3ad6 | 25 | #include <linux/slab.h> |
deec9ae3 | 26 | #include <linux/stat.h> |
e2b35f3d | 27 | #include <linux/types.h> |
7d2be074 HS |
28 | |
29 | #include <linux/mmc/host.h> | |
2f1d7918 | 30 | #include <linux/mmc/sdio.h> |
2635d1ba NF |
31 | |
32 | #include <mach/atmel-mci.h> | |
c42aa775 | 33 | #include <linux/atmel-mci.h> |
796211b7 | 34 | #include <linux/atmel_pdc.h> |
7d2be074 | 35 | |
7d2be074 HS |
36 | #include <asm/io.h> |
37 | #include <asm/unaligned.h> | |
38 | ||
04d699c3 | 39 | #include <mach/cpu.h> |
3663b736 | 40 | #include <mach/board.h> |
7d2be074 HS |
41 | |
42 | #include "atmel-mci-regs.h" | |
43 | ||
2c96a293 | 44 | #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE) |
65e8b083 | 45 | #define ATMCI_DMA_THRESHOLD 16 |
7d2be074 HS |
46 | |
47 | enum { | |
f5177547 | 48 | EVENT_CMD_RDY = 0, |
7d2be074 | 49 | EVENT_XFER_COMPLETE, |
f5177547 | 50 | EVENT_NOTBUSY, |
c06ad258 HS |
51 | EVENT_DATA_ERROR, |
52 | }; | |
53 | ||
54 | enum atmel_mci_state { | |
965ebf33 HS |
55 | STATE_IDLE = 0, |
56 | STATE_SENDING_CMD, | |
f5177547 LD |
57 | STATE_DATA_XFER, |
58 | STATE_WAITING_NOTBUSY, | |
c06ad258 | 59 | STATE_SENDING_STOP, |
f5177547 | 60 | STATE_END_REQUEST, |
7d2be074 HS |
61 | }; |
62 | ||
796211b7 LD |
63 | enum atmci_xfer_dir { |
64 | XFER_RECEIVE = 0, | |
65 | XFER_TRANSMIT, | |
66 | }; | |
67 | ||
68 | enum atmci_pdc_buf { | |
69 | PDC_FIRST_BUF = 0, | |
70 | PDC_SECOND_BUF, | |
71 | }; | |
72 | ||
73 | struct atmel_mci_caps { | |
74 | bool has_dma; | |
75 | bool has_pdc; | |
76 | bool has_cfg_reg; | |
77 | bool has_cstor_reg; | |
78 | bool has_highspeed; | |
79 | bool has_rwproof; | |
faf8180b | 80 | bool has_odd_clk_div; |
24011f34 LD |
81 | bool has_bad_data_ordering; |
82 | bool need_reset_after_xfer; | |
83 | bool need_blksz_mul_4; | |
796211b7 LD |
84 | }; |
85 | ||
65e8b083 | 86 | struct atmel_mci_dma { |
65e8b083 HS |
87 | struct dma_chan *chan; |
88 | struct dma_async_tx_descriptor *data_desc; | |
65e8b083 HS |
89 | }; |
90 | ||
965ebf33 HS |
91 | /** |
92 | * struct atmel_mci - MMC controller state shared between all slots | |
93 | * @lock: Spinlock protecting the queue and associated data. | |
94 | * @regs: Pointer to MMIO registers. | |
796211b7 | 95 | * @sg: Scatterlist entry currently being processed by PIO or PDC code. |
965ebf33 | 96 | * @pio_offset: Offset into the current scatterlist entry. |
7a90dcc2 LD |
97 | * @buffer: Buffer used if we don't have the r/w proof capability. We |
98 | * don't have the time to switch pdc buffers so we have to use only | |
99 | * one buffer for the full transaction. | |
100 | * @buf_size: size of the buffer. | |
101 | * @phys_buf_addr: buffer address needed for pdc. | |
965ebf33 HS |
102 | * @cur_slot: The slot which is currently using the controller. |
103 | * @mrq: The request currently being processed on @cur_slot, | |
104 | * or NULL if the controller is idle. | |
105 | * @cmd: The command currently being sent to the card, or NULL. | |
106 | * @data: The data currently being transferred, or NULL if no data | |
107 | * transfer is in progress. | |
796211b7 | 108 | * @data_size: just data->blocks * data->blksz. |
65e8b083 HS |
109 | * @dma: DMA client state. |
110 | * @data_chan: DMA channel being used for the current data transfer. | |
965ebf33 HS |
111 | * @cmd_status: Snapshot of SR taken upon completion of the current |
112 | * command. Only valid when EVENT_CMD_COMPLETE is pending. | |
113 | * @data_status: Snapshot of SR taken upon completion of the current | |
114 | * data transfer. Only valid when EVENT_DATA_COMPLETE or | |
115 | * EVENT_DATA_ERROR is pending. | |
116 | * @stop_cmdr: Value to be loaded into CMDR when the stop command is | |
117 | * to be sent. | |
118 | * @tasklet: Tasklet running the request state machine. | |
119 | * @pending_events: Bitmask of events flagged by the interrupt handler | |
120 | * to be processed by the tasklet. | |
121 | * @completed_events: Bitmask of events which the state machine has | |
122 | * processed. | |
123 | * @state: Tasklet state. | |
124 | * @queue: List of slots waiting for access to the controller. | |
125 | * @need_clock_update: Update the clock rate before the next request. | |
126 | * @need_reset: Reset controller before next request. | |
24011f34 | 127 | * @timer: Timer to balance the data timeout error flag which cannot rise. |
965ebf33 | 128 | * @mode_reg: Value of the MR register. |
74791a2d | 129 | * @cfg_reg: Value of the CFG register. |
965ebf33 HS |
130 | * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus |
131 | * rate and timeout calculations. | |
132 | * @mapbase: Physical address of the MMIO registers. | |
133 | * @mck: The peripheral bus clock hooked up to the MMC controller. | |
134 | * @pdev: Platform device associated with the MMC controller. | |
135 | * @slot: Slots sharing this MMC controller. | |
796211b7 LD |
136 | * @caps: MCI capabilities depending on MCI version. |
137 | * @prepare_data: function to setup MCI before data transfer which | |
138 | * depends on MCI capabilities. | |
139 | * @submit_data: function to start data transfer which depends on MCI | |
140 | * capabilities. | |
141 | * @stop_transfer: function to stop data transfer which depends on MCI | |
142 | * capabilities. | |
965ebf33 HS |
143 | * |
144 | * Locking | |
145 | * ======= | |
146 | * | |
147 | * @lock is a softirq-safe spinlock protecting @queue as well as | |
148 | * @cur_slot, @mrq and @state. These must always be updated | |
149 | * at the same time while holding @lock. | |
150 | * | |
151 | * @lock also protects mode_reg and need_clock_update since these are | |
152 | * used to synchronize mode register updates with the queue | |
153 | * processing. | |
154 | * | |
155 | * The @mrq field of struct atmel_mci_slot is also protected by @lock, | |
156 | * and must always be written at the same time as the slot is added to | |
157 | * @queue. | |
158 | * | |
159 | * @pending_events and @completed_events are accessed using atomic bit | |
160 | * operations, so they don't need any locking. | |
161 | * | |
162 | * None of the fields touched by the interrupt handler need any | |
163 | * locking. However, ordering is important: Before EVENT_DATA_ERROR or | |
164 | * EVENT_DATA_COMPLETE is set in @pending_events, all data-related | |
165 | * interrupts must be disabled and @data_status updated with a | |
166 | * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the | |
25985edc | 167 | * CMDRDY interrupt must be disabled and @cmd_status updated with a |
965ebf33 HS |
168 | * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the |
169 | * bytes_xfered field of @data must be written. This is ensured by | |
170 | * using barriers. | |
171 | */ | |
7d2be074 | 172 | struct atmel_mci { |
965ebf33 | 173 | spinlock_t lock; |
7d2be074 HS |
174 | void __iomem *regs; |
175 | ||
176 | struct scatterlist *sg; | |
177 | unsigned int pio_offset; | |
7a90dcc2 LD |
178 | unsigned int *buffer; |
179 | unsigned int buf_size; | |
180 | dma_addr_t buf_phys_addr; | |
7d2be074 | 181 | |
965ebf33 | 182 | struct atmel_mci_slot *cur_slot; |
7d2be074 HS |
183 | struct mmc_request *mrq; |
184 | struct mmc_command *cmd; | |
185 | struct mmc_data *data; | |
796211b7 | 186 | unsigned int data_size; |
7d2be074 | 187 | |
65e8b083 HS |
188 | struct atmel_mci_dma dma; |
189 | struct dma_chan *data_chan; | |
e2b35f3d | 190 | struct dma_slave_config dma_conf; |
65e8b083 | 191 | |
7d2be074 HS |
192 | u32 cmd_status; |
193 | u32 data_status; | |
7d2be074 HS |
194 | u32 stop_cmdr; |
195 | ||
7d2be074 HS |
196 | struct tasklet_struct tasklet; |
197 | unsigned long pending_events; | |
198 | unsigned long completed_events; | |
c06ad258 | 199 | enum atmel_mci_state state; |
965ebf33 | 200 | struct list_head queue; |
7d2be074 | 201 | |
965ebf33 HS |
202 | bool need_clock_update; |
203 | bool need_reset; | |
24011f34 | 204 | struct timer_list timer; |
965ebf33 | 205 | u32 mode_reg; |
74791a2d | 206 | u32 cfg_reg; |
7d2be074 HS |
207 | unsigned long bus_hz; |
208 | unsigned long mapbase; | |
209 | struct clk *mck; | |
210 | struct platform_device *pdev; | |
965ebf33 | 211 | |
2c96a293 | 212 | struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS]; |
796211b7 LD |
213 | |
214 | struct atmel_mci_caps caps; | |
215 | ||
216 | u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data); | |
217 | void (*submit_data)(struct atmel_mci *host, struct mmc_data *data); | |
218 | void (*stop_transfer)(struct atmel_mci *host); | |
965ebf33 HS |
219 | }; |
220 | ||
221 | /** | |
222 | * struct atmel_mci_slot - MMC slot state | |
223 | * @mmc: The mmc_host representing this slot. | |
224 | * @host: The MMC controller this slot is using. | |
225 | * @sdc_reg: Value of SDCR to be written before using this slot. | |
88ff82ed | 226 | * @sdio_irq: SDIO irq mask for this slot. |
965ebf33 HS |
227 | * @mrq: mmc_request currently being processed or waiting to be |
228 | * processed, or NULL when the slot is idle. | |
229 | * @queue_node: List node for placing this node in the @queue list of | |
230 | * &struct atmel_mci. | |
231 | * @clock: Clock rate configured by set_ios(). Protected by host->lock. | |
232 | * @flags: Random state bits associated with the slot. | |
233 | * @detect_pin: GPIO pin used for card detection, or negative if not | |
234 | * available. | |
235 | * @wp_pin: GPIO pin used for card write protect sending, or negative | |
236 | * if not available. | |
1c1452be | 237 | * @detect_is_active_high: The state of the detect pin when it is active. |
965ebf33 HS |
238 | * @detect_timer: Timer used for debouncing @detect_pin interrupts. |
239 | */ | |
240 | struct atmel_mci_slot { | |
241 | struct mmc_host *mmc; | |
242 | struct atmel_mci *host; | |
243 | ||
244 | u32 sdc_reg; | |
88ff82ed | 245 | u32 sdio_irq; |
965ebf33 HS |
246 | |
247 | struct mmc_request *mrq; | |
248 | struct list_head queue_node; | |
249 | ||
250 | unsigned int clock; | |
251 | unsigned long flags; | |
252 | #define ATMCI_CARD_PRESENT 0 | |
253 | #define ATMCI_CARD_NEED_INIT 1 | |
254 | #define ATMCI_SHUTDOWN 2 | |
5c2f2b9b | 255 | #define ATMCI_SUSPENDED 3 |
965ebf33 HS |
256 | |
257 | int detect_pin; | |
258 | int wp_pin; | |
1c1452be | 259 | bool detect_is_active_high; |
965ebf33 HS |
260 | |
261 | struct timer_list detect_timer; | |
7d2be074 HS |
262 | }; |
263 | ||
7d2be074 HS |
264 | #define atmci_test_and_clear_pending(host, event) \ |
265 | test_and_clear_bit(event, &host->pending_events) | |
7d2be074 HS |
266 | #define atmci_set_completed(host, event) \ |
267 | set_bit(event, &host->completed_events) | |
268 | #define atmci_set_pending(host, event) \ | |
269 | set_bit(event, &host->pending_events) | |
7d2be074 | 270 | |
deec9ae3 HS |
271 | /* |
272 | * The debugfs stuff below is mostly optimized away when | |
273 | * CONFIG_DEBUG_FS is not set. | |
274 | */ | |
275 | static int atmci_req_show(struct seq_file *s, void *v) | |
276 | { | |
965ebf33 HS |
277 | struct atmel_mci_slot *slot = s->private; |
278 | struct mmc_request *mrq; | |
deec9ae3 HS |
279 | struct mmc_command *cmd; |
280 | struct mmc_command *stop; | |
281 | struct mmc_data *data; | |
282 | ||
283 | /* Make sure we get a consistent snapshot */ | |
965ebf33 HS |
284 | spin_lock_bh(&slot->host->lock); |
285 | mrq = slot->mrq; | |
deec9ae3 HS |
286 | |
287 | if (mrq) { | |
288 | cmd = mrq->cmd; | |
289 | data = mrq->data; | |
290 | stop = mrq->stop; | |
291 | ||
292 | if (cmd) | |
293 | seq_printf(s, | |
294 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", | |
295 | cmd->opcode, cmd->arg, cmd->flags, | |
296 | cmd->resp[0], cmd->resp[1], cmd->resp[2], | |
d586ebbb | 297 | cmd->resp[3], cmd->error); |
deec9ae3 HS |
298 | if (data) |
299 | seq_printf(s, "DATA %u / %u * %u flg %x err %d\n", | |
300 | data->bytes_xfered, data->blocks, | |
301 | data->blksz, data->flags, data->error); | |
302 | if (stop) | |
303 | seq_printf(s, | |
304 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", | |
305 | stop->opcode, stop->arg, stop->flags, | |
306 | stop->resp[0], stop->resp[1], stop->resp[2], | |
d586ebbb | 307 | stop->resp[3], stop->error); |
deec9ae3 HS |
308 | } |
309 | ||
965ebf33 | 310 | spin_unlock_bh(&slot->host->lock); |
deec9ae3 HS |
311 | |
312 | return 0; | |
313 | } | |
314 | ||
315 | static int atmci_req_open(struct inode *inode, struct file *file) | |
316 | { | |
317 | return single_open(file, atmci_req_show, inode->i_private); | |
318 | } | |
319 | ||
320 | static const struct file_operations atmci_req_fops = { | |
321 | .owner = THIS_MODULE, | |
322 | .open = atmci_req_open, | |
323 | .read = seq_read, | |
324 | .llseek = seq_lseek, | |
325 | .release = single_release, | |
326 | }; | |
327 | ||
328 | static void atmci_show_status_reg(struct seq_file *s, | |
329 | const char *regname, u32 value) | |
330 | { | |
331 | static const char *sr_bit[] = { | |
332 | [0] = "CMDRDY", | |
333 | [1] = "RXRDY", | |
334 | [2] = "TXRDY", | |
335 | [3] = "BLKE", | |
336 | [4] = "DTIP", | |
337 | [5] = "NOTBUSY", | |
04d699c3 RE |
338 | [6] = "ENDRX", |
339 | [7] = "ENDTX", | |
deec9ae3 HS |
340 | [8] = "SDIOIRQA", |
341 | [9] = "SDIOIRQB", | |
04d699c3 RE |
342 | [12] = "SDIOWAIT", |
343 | [14] = "RXBUFF", | |
344 | [15] = "TXBUFE", | |
deec9ae3 HS |
345 | [16] = "RINDE", |
346 | [17] = "RDIRE", | |
347 | [18] = "RCRCE", | |
348 | [19] = "RENDE", | |
349 | [20] = "RTOE", | |
350 | [21] = "DCRCE", | |
351 | [22] = "DTOE", | |
04d699c3 RE |
352 | [23] = "CSTOE", |
353 | [24] = "BLKOVRE", | |
354 | [25] = "DMADONE", | |
355 | [26] = "FIFOEMPTY", | |
356 | [27] = "XFRDONE", | |
deec9ae3 HS |
357 | [30] = "OVRE", |
358 | [31] = "UNRE", | |
359 | }; | |
360 | unsigned int i; | |
361 | ||
362 | seq_printf(s, "%s:\t0x%08x", regname, value); | |
363 | for (i = 0; i < ARRAY_SIZE(sr_bit); i++) { | |
364 | if (value & (1 << i)) { | |
365 | if (sr_bit[i]) | |
366 | seq_printf(s, " %s", sr_bit[i]); | |
367 | else | |
368 | seq_puts(s, " UNKNOWN"); | |
369 | } | |
370 | } | |
371 | seq_putc(s, '\n'); | |
372 | } | |
373 | ||
374 | static int atmci_regs_show(struct seq_file *s, void *v) | |
375 | { | |
376 | struct atmel_mci *host = s->private; | |
377 | u32 *buf; | |
378 | ||
2c96a293 | 379 | buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL); |
deec9ae3 HS |
380 | if (!buf) |
381 | return -ENOMEM; | |
382 | ||
965ebf33 HS |
383 | /* |
384 | * Grab a more or less consistent snapshot. Note that we're | |
385 | * not disabling interrupts, so IMR and SR may not be | |
386 | * consistent. | |
387 | */ | |
388 | spin_lock_bh(&host->lock); | |
87e60f2b | 389 | clk_enable(host->mck); |
2c96a293 | 390 | memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE); |
87e60f2b | 391 | clk_disable(host->mck); |
965ebf33 | 392 | spin_unlock_bh(&host->lock); |
deec9ae3 HS |
393 | |
394 | seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n", | |
2c96a293 LD |
395 | buf[ATMCI_MR / 4], |
396 | buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "", | |
397 | buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : "", | |
398 | buf[ATMCI_MR / 4] & 0xff); | |
399 | seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]); | |
400 | seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]); | |
401 | seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]); | |
deec9ae3 | 402 | seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n", |
2c96a293 LD |
403 | buf[ATMCI_BLKR / 4], |
404 | buf[ATMCI_BLKR / 4] & 0xffff, | |
405 | (buf[ATMCI_BLKR / 4] >> 16) & 0xffff); | |
796211b7 | 406 | if (host->caps.has_cstor_reg) |
2c96a293 | 407 | seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]); |
deec9ae3 HS |
408 | |
409 | /* Don't read RSPR and RDR; it will consume the data there */ | |
410 | ||
2c96a293 LD |
411 | atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]); |
412 | atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]); | |
deec9ae3 | 413 | |
796211b7 | 414 | if (host->caps.has_dma) { |
74791a2d NF |
415 | u32 val; |
416 | ||
2c96a293 | 417 | val = buf[ATMCI_DMA / 4]; |
74791a2d NF |
418 | seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n", |
419 | val, val & 3, | |
420 | ((val >> 4) & 3) ? | |
421 | 1 << (((val >> 4) & 3) + 1) : 1, | |
2c96a293 | 422 | val & ATMCI_DMAEN ? " DMAEN" : ""); |
796211b7 LD |
423 | } |
424 | if (host->caps.has_cfg_reg) { | |
425 | u32 val; | |
74791a2d | 426 | |
2c96a293 | 427 | val = buf[ATMCI_CFG / 4]; |
74791a2d NF |
428 | seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n", |
429 | val, | |
2c96a293 LD |
430 | val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "", |
431 | val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "", | |
432 | val & ATMCI_CFG_HSMODE ? " HSMODE" : "", | |
433 | val & ATMCI_CFG_LSYNC ? " LSYNC" : ""); | |
74791a2d NF |
434 | } |
435 | ||
b17339a1 HS |
436 | kfree(buf); |
437 | ||
deec9ae3 HS |
438 | return 0; |
439 | } | |
440 | ||
441 | static int atmci_regs_open(struct inode *inode, struct file *file) | |
442 | { | |
443 | return single_open(file, atmci_regs_show, inode->i_private); | |
444 | } | |
445 | ||
446 | static const struct file_operations atmci_regs_fops = { | |
447 | .owner = THIS_MODULE, | |
448 | .open = atmci_regs_open, | |
449 | .read = seq_read, | |
450 | .llseek = seq_lseek, | |
451 | .release = single_release, | |
452 | }; | |
453 | ||
965ebf33 | 454 | static void atmci_init_debugfs(struct atmel_mci_slot *slot) |
deec9ae3 | 455 | { |
965ebf33 HS |
456 | struct mmc_host *mmc = slot->mmc; |
457 | struct atmel_mci *host = slot->host; | |
458 | struct dentry *root; | |
459 | struct dentry *node; | |
deec9ae3 | 460 | |
deec9ae3 HS |
461 | root = mmc->debugfs_root; |
462 | if (!root) | |
463 | return; | |
464 | ||
465 | node = debugfs_create_file("regs", S_IRUSR, root, host, | |
466 | &atmci_regs_fops); | |
467 | if (IS_ERR(node)) | |
468 | return; | |
469 | if (!node) | |
470 | goto err; | |
471 | ||
965ebf33 | 472 | node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops); |
deec9ae3 HS |
473 | if (!node) |
474 | goto err; | |
475 | ||
c06ad258 HS |
476 | node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state); |
477 | if (!node) | |
478 | goto err; | |
479 | ||
deec9ae3 HS |
480 | node = debugfs_create_x32("pending_events", S_IRUSR, root, |
481 | (u32 *)&host->pending_events); | |
482 | if (!node) | |
483 | goto err; | |
484 | ||
485 | node = debugfs_create_x32("completed_events", S_IRUSR, root, | |
486 | (u32 *)&host->completed_events); | |
487 | if (!node) | |
488 | goto err; | |
489 | ||
490 | return; | |
491 | ||
492 | err: | |
965ebf33 | 493 | dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n"); |
deec9ae3 | 494 | } |
7d2be074 | 495 | |
7a90dcc2 LD |
496 | static inline unsigned int atmci_get_version(struct atmel_mci *host) |
497 | { | |
498 | return atmci_readl(host, ATMCI_VERSION) & 0x00000fff; | |
499 | } | |
500 | ||
24011f34 LD |
501 | static void atmci_timeout_timer(unsigned long data) |
502 | { | |
503 | struct atmel_mci *host; | |
504 | ||
505 | host = (struct atmel_mci *)data; | |
506 | ||
507 | dev_dbg(&host->pdev->dev, "software timeout\n"); | |
508 | ||
509 | if (host->mrq->cmd->data) { | |
510 | host->mrq->cmd->data->error = -ETIMEDOUT; | |
511 | host->data = NULL; | |
512 | } else { | |
513 | host->mrq->cmd->error = -ETIMEDOUT; | |
514 | host->cmd = NULL; | |
515 | } | |
516 | host->need_reset = 1; | |
517 | host->state = STATE_END_REQUEST; | |
518 | smp_wmb(); | |
519 | tasklet_schedule(&host->tasklet); | |
520 | } | |
521 | ||
2c96a293 | 522 | static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host, |
7d2be074 HS |
523 | unsigned int ns) |
524 | { | |
66292ad9 LD |
525 | /* |
526 | * It is easier here to use us instead of ns for the timeout, | |
527 | * it prevents from overflows during calculation. | |
528 | */ | |
529 | unsigned int us = DIV_ROUND_UP(ns, 1000); | |
530 | ||
531 | /* Maximum clock frequency is host->bus_hz/2 */ | |
532 | return us * (DIV_ROUND_UP(host->bus_hz, 2000000)); | |
7d2be074 HS |
533 | } |
534 | ||
535 | static void atmci_set_timeout(struct atmel_mci *host, | |
965ebf33 | 536 | struct atmel_mci_slot *slot, struct mmc_data *data) |
7d2be074 HS |
537 | { |
538 | static unsigned dtomul_to_shift[] = { | |
539 | 0, 4, 7, 8, 10, 12, 16, 20 | |
540 | }; | |
541 | unsigned timeout; | |
542 | unsigned dtocyc; | |
543 | unsigned dtomul; | |
544 | ||
2c96a293 LD |
545 | timeout = atmci_ns_to_clocks(host, data->timeout_ns) |
546 | + data->timeout_clks; | |
7d2be074 HS |
547 | |
548 | for (dtomul = 0; dtomul < 8; dtomul++) { | |
549 | unsigned shift = dtomul_to_shift[dtomul]; | |
550 | dtocyc = (timeout + (1 << shift) - 1) >> shift; | |
551 | if (dtocyc < 15) | |
552 | break; | |
553 | } | |
554 | ||
555 | if (dtomul >= 8) { | |
556 | dtomul = 7; | |
557 | dtocyc = 15; | |
558 | } | |
559 | ||
965ebf33 | 560 | dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n", |
7d2be074 | 561 | dtocyc << dtomul_to_shift[dtomul]); |
03fc9a7f | 562 | atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc))); |
7d2be074 HS |
563 | } |
564 | ||
565 | /* | |
566 | * Return mask with command flags to be enabled for this command. | |
567 | */ | |
568 | static u32 atmci_prepare_command(struct mmc_host *mmc, | |
569 | struct mmc_command *cmd) | |
570 | { | |
571 | struct mmc_data *data; | |
572 | u32 cmdr; | |
573 | ||
574 | cmd->error = -EINPROGRESS; | |
575 | ||
2c96a293 | 576 | cmdr = ATMCI_CMDR_CMDNB(cmd->opcode); |
7d2be074 HS |
577 | |
578 | if (cmd->flags & MMC_RSP_PRESENT) { | |
579 | if (cmd->flags & MMC_RSP_136) | |
2c96a293 | 580 | cmdr |= ATMCI_CMDR_RSPTYP_136BIT; |
7d2be074 | 581 | else |
2c96a293 | 582 | cmdr |= ATMCI_CMDR_RSPTYP_48BIT; |
7d2be074 HS |
583 | } |
584 | ||
585 | /* | |
586 | * This should really be MAXLAT_5 for CMD2 and ACMD41, but | |
587 | * it's too difficult to determine whether this is an ACMD or | |
588 | * not. Better make it 64. | |
589 | */ | |
2c96a293 | 590 | cmdr |= ATMCI_CMDR_MAXLAT_64CYC; |
7d2be074 HS |
591 | |
592 | if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN) | |
2c96a293 | 593 | cmdr |= ATMCI_CMDR_OPDCMD; |
7d2be074 HS |
594 | |
595 | data = cmd->data; | |
596 | if (data) { | |
2c96a293 | 597 | cmdr |= ATMCI_CMDR_START_XFER; |
2f1d7918 NF |
598 | |
599 | if (cmd->opcode == SD_IO_RW_EXTENDED) { | |
2c96a293 | 600 | cmdr |= ATMCI_CMDR_SDIO_BLOCK; |
2f1d7918 NF |
601 | } else { |
602 | if (data->flags & MMC_DATA_STREAM) | |
2c96a293 | 603 | cmdr |= ATMCI_CMDR_STREAM; |
2f1d7918 | 604 | else if (data->blocks > 1) |
2c96a293 | 605 | cmdr |= ATMCI_CMDR_MULTI_BLOCK; |
2f1d7918 | 606 | else |
2c96a293 | 607 | cmdr |= ATMCI_CMDR_BLOCK; |
2f1d7918 | 608 | } |
7d2be074 HS |
609 | |
610 | if (data->flags & MMC_DATA_READ) | |
2c96a293 | 611 | cmdr |= ATMCI_CMDR_TRDIR_READ; |
7d2be074 HS |
612 | } |
613 | ||
614 | return cmdr; | |
615 | } | |
616 | ||
11d1488b | 617 | static void atmci_send_command(struct atmel_mci *host, |
965ebf33 | 618 | struct mmc_command *cmd, u32 cmd_flags) |
7d2be074 | 619 | { |
7d2be074 HS |
620 | WARN_ON(host->cmd); |
621 | host->cmd = cmd; | |
622 | ||
965ebf33 | 623 | dev_vdbg(&host->pdev->dev, |
7d2be074 HS |
624 | "start command: ARGR=0x%08x CMDR=0x%08x\n", |
625 | cmd->arg, cmd_flags); | |
626 | ||
03fc9a7f LD |
627 | atmci_writel(host, ATMCI_ARGR, cmd->arg); |
628 | atmci_writel(host, ATMCI_CMDR, cmd_flags); | |
7d2be074 HS |
629 | } |
630 | ||
2c96a293 | 631 | static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data) |
7d2be074 | 632 | { |
6801c41a | 633 | dev_dbg(&host->pdev->dev, "send stop command\n"); |
11d1488b | 634 | atmci_send_command(host, data->stop, host->stop_cmdr); |
03fc9a7f | 635 | atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY); |
7d2be074 HS |
636 | } |
637 | ||
796211b7 LD |
638 | /* |
639 | * Configure given PDC buffer taking care of alignement issues. | |
640 | * Update host->data_size and host->sg. | |
641 | */ | |
642 | static void atmci_pdc_set_single_buf(struct atmel_mci *host, | |
643 | enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb) | |
644 | { | |
645 | u32 pointer_reg, counter_reg; | |
7a90dcc2 | 646 | unsigned int buf_size; |
796211b7 LD |
647 | |
648 | if (dir == XFER_RECEIVE) { | |
649 | pointer_reg = ATMEL_PDC_RPR; | |
650 | counter_reg = ATMEL_PDC_RCR; | |
651 | } else { | |
652 | pointer_reg = ATMEL_PDC_TPR; | |
653 | counter_reg = ATMEL_PDC_TCR; | |
654 | } | |
655 | ||
656 | if (buf_nb == PDC_SECOND_BUF) { | |
1ebbe3d3 LD |
657 | pointer_reg += ATMEL_PDC_SCND_BUF_OFF; |
658 | counter_reg += ATMEL_PDC_SCND_BUF_OFF; | |
796211b7 LD |
659 | } |
660 | ||
7a90dcc2 LD |
661 | if (!host->caps.has_rwproof) { |
662 | buf_size = host->buf_size; | |
663 | atmci_writel(host, pointer_reg, host->buf_phys_addr); | |
664 | } else { | |
665 | buf_size = sg_dma_len(host->sg); | |
666 | atmci_writel(host, pointer_reg, sg_dma_address(host->sg)); | |
667 | } | |
668 | ||
669 | if (host->data_size <= buf_size) { | |
796211b7 LD |
670 | if (host->data_size & 0x3) { |
671 | /* If size is different from modulo 4, transfer bytes */ | |
672 | atmci_writel(host, counter_reg, host->data_size); | |
673 | atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE); | |
674 | } else { | |
675 | /* Else transfer 32-bits words */ | |
676 | atmci_writel(host, counter_reg, host->data_size / 4); | |
677 | } | |
678 | host->data_size = 0; | |
679 | } else { | |
680 | /* We assume the size of a page is 32-bits aligned */ | |
341fa4c3 LD |
681 | atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4); |
682 | host->data_size -= sg_dma_len(host->sg); | |
796211b7 LD |
683 | if (host->data_size) |
684 | host->sg = sg_next(host->sg); | |
685 | } | |
686 | } | |
687 | ||
688 | /* | |
689 | * Configure PDC buffer according to the data size ie configuring one or two | |
690 | * buffers. Don't use this function if you want to configure only the second | |
691 | * buffer. In this case, use atmci_pdc_set_single_buf. | |
692 | */ | |
693 | static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir) | |
65e8b083 | 694 | { |
796211b7 LD |
695 | atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF); |
696 | if (host->data_size) | |
697 | atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF); | |
698 | } | |
699 | ||
700 | /* | |
701 | * Unmap sg lists, called when transfer is finished. | |
702 | */ | |
703 | static void atmci_pdc_cleanup(struct atmel_mci *host) | |
704 | { | |
705 | struct mmc_data *data = host->data; | |
65e8b083 | 706 | |
009a891b | 707 | if (data) |
796211b7 LD |
708 | dma_unmap_sg(&host->pdev->dev, |
709 | data->sg, data->sg_len, | |
710 | ((data->flags & MMC_DATA_WRITE) | |
711 | ? DMA_TO_DEVICE : DMA_FROM_DEVICE)); | |
65e8b083 HS |
712 | } |
713 | ||
796211b7 LD |
714 | /* |
715 | * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after | |
716 | * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY | |
717 | * interrupt needed for both transfer directions. | |
718 | */ | |
719 | static void atmci_pdc_complete(struct atmel_mci *host) | |
65e8b083 | 720 | { |
7a90dcc2 | 721 | int transfer_size = host->data->blocks * host->data->blksz; |
24011f34 | 722 | int i; |
7a90dcc2 | 723 | |
796211b7 | 724 | atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS); |
7a90dcc2 LD |
725 | |
726 | if ((!host->caps.has_rwproof) | |
24011f34 LD |
727 | && (host->data->flags & MMC_DATA_READ)) { |
728 | if (host->caps.has_bad_data_ordering) | |
729 | for (i = 0; i < transfer_size; i++) | |
730 | host->buffer[i] = swab32(host->buffer[i]); | |
7a90dcc2 LD |
731 | sg_copy_from_buffer(host->data->sg, host->data->sg_len, |
732 | host->buffer, transfer_size); | |
24011f34 | 733 | } |
7a90dcc2 | 734 | |
796211b7 | 735 | atmci_pdc_cleanup(host); |
65e8b083 | 736 | |
796211b7 LD |
737 | /* |
738 | * If the card was removed, data will be NULL. No point trying | |
739 | * to send the stop command or waiting for NBUSY in this case. | |
740 | */ | |
741 | if (host->data) { | |
6801c41a LD |
742 | dev_dbg(&host->pdev->dev, |
743 | "(%s) set pending xfer complete\n", __func__); | |
65e8b083 | 744 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
796211b7 | 745 | tasklet_schedule(&host->tasklet); |
65e8b083 HS |
746 | } |
747 | } | |
748 | ||
796211b7 LD |
749 | static void atmci_dma_cleanup(struct atmel_mci *host) |
750 | { | |
751 | struct mmc_data *data = host->data; | |
752 | ||
753 | if (data) | |
754 | dma_unmap_sg(host->dma.chan->device->dev, | |
755 | data->sg, data->sg_len, | |
756 | ((data->flags & MMC_DATA_WRITE) | |
757 | ? DMA_TO_DEVICE : DMA_FROM_DEVICE)); | |
758 | } | |
759 | ||
760 | /* | |
761 | * This function is called by the DMA driver from tasklet context. | |
762 | */ | |
65e8b083 HS |
763 | static void atmci_dma_complete(void *arg) |
764 | { | |
765 | struct atmel_mci *host = arg; | |
766 | struct mmc_data *data = host->data; | |
767 | ||
768 | dev_vdbg(&host->pdev->dev, "DMA complete\n"); | |
769 | ||
796211b7 | 770 | if (host->caps.has_dma) |
74791a2d | 771 | /* Disable DMA hardware handshaking on MCI */ |
03fc9a7f | 772 | atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN); |
74791a2d | 773 | |
65e8b083 HS |
774 | atmci_dma_cleanup(host); |
775 | ||
776 | /* | |
777 | * If the card was removed, data will be NULL. No point trying | |
778 | * to send the stop command or waiting for NBUSY in this case. | |
779 | */ | |
780 | if (data) { | |
6801c41a LD |
781 | dev_dbg(&host->pdev->dev, |
782 | "(%s) set pending xfer complete\n", __func__); | |
65e8b083 HS |
783 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
784 | tasklet_schedule(&host->tasklet); | |
785 | ||
786 | /* | |
787 | * Regardless of what the documentation says, we have | |
788 | * to wait for NOTBUSY even after block read | |
789 | * operations. | |
790 | * | |
791 | * When the DMA transfer is complete, the controller | |
792 | * may still be reading the CRC from the card, i.e. | |
793 | * the data transfer is still in progress and we | |
794 | * haven't seen all the potential error bits yet. | |
795 | * | |
796 | * The interrupt handler will schedule a different | |
797 | * tasklet to finish things up when the data transfer | |
798 | * is completely done. | |
799 | * | |
800 | * We may not complete the mmc request here anyway | |
801 | * because the mmc layer may call back and cause us to | |
802 | * violate the "don't submit new operations from the | |
803 | * completion callback" rule of the dma engine | |
804 | * framework. | |
805 | */ | |
03fc9a7f | 806 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
65e8b083 HS |
807 | } |
808 | } | |
809 | ||
796211b7 LD |
810 | /* |
811 | * Returns a mask of interrupt flags to be enabled after the whole | |
812 | * request has been prepared. | |
813 | */ | |
814 | static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data) | |
815 | { | |
816 | u32 iflags; | |
817 | ||
818 | data->error = -EINPROGRESS; | |
819 | ||
820 | host->sg = data->sg; | |
821 | host->data = data; | |
822 | host->data_chan = NULL; | |
823 | ||
824 | iflags = ATMCI_DATA_ERROR_FLAGS; | |
825 | ||
826 | /* | |
827 | * Errata: MMC data write operation with less than 12 | |
828 | * bytes is impossible. | |
829 | * | |
830 | * Errata: MCI Transmit Data Register (TDR) FIFO | |
831 | * corruption when length is not multiple of 4. | |
832 | */ | |
833 | if (data->blocks * data->blksz < 12 | |
834 | || (data->blocks * data->blksz) & 3) | |
835 | host->need_reset = true; | |
836 | ||
837 | host->pio_offset = 0; | |
838 | if (data->flags & MMC_DATA_READ) | |
839 | iflags |= ATMCI_RXRDY; | |
840 | else | |
841 | iflags |= ATMCI_TXRDY; | |
842 | ||
843 | return iflags; | |
844 | } | |
845 | ||
846 | /* | |
847 | * Set interrupt flags and set block length into the MCI mode register even | |
848 | * if this value is also accessible in the MCI block register. It seems to be | |
849 | * necessary before the High Speed MCI version. It also map sg and configure | |
850 | * PDC registers. | |
851 | */ | |
852 | static u32 | |
853 | atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data) | |
854 | { | |
855 | u32 iflags, tmp; | |
856 | unsigned int sg_len; | |
857 | enum dma_data_direction dir; | |
24011f34 | 858 | int i; |
796211b7 LD |
859 | |
860 | data->error = -EINPROGRESS; | |
861 | ||
862 | host->data = data; | |
863 | host->sg = data->sg; | |
864 | iflags = ATMCI_DATA_ERROR_FLAGS; | |
865 | ||
866 | /* Enable pdc mode */ | |
867 | atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE); | |
868 | ||
869 | if (data->flags & MMC_DATA_READ) { | |
870 | dir = DMA_FROM_DEVICE; | |
871 | iflags |= ATMCI_ENDRX | ATMCI_RXBUFF; | |
872 | } else { | |
873 | dir = DMA_TO_DEVICE; | |
f5177547 | 874 | iflags |= ATMCI_ENDTX | ATMCI_TXBUFE | ATMCI_BLKE; |
796211b7 LD |
875 | } |
876 | ||
877 | /* Set BLKLEN */ | |
878 | tmp = atmci_readl(host, ATMCI_MR); | |
879 | tmp &= 0x0000ffff; | |
880 | tmp |= ATMCI_BLKLEN(data->blksz); | |
881 | atmci_writel(host, ATMCI_MR, tmp); | |
882 | ||
883 | /* Configure PDC */ | |
884 | host->data_size = data->blocks * data->blksz; | |
885 | sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir); | |
7a90dcc2 LD |
886 | |
887 | if ((!host->caps.has_rwproof) | |
24011f34 | 888 | && (host->data->flags & MMC_DATA_WRITE)) { |
7a90dcc2 LD |
889 | sg_copy_to_buffer(host->data->sg, host->data->sg_len, |
890 | host->buffer, host->data_size); | |
24011f34 LD |
891 | if (host->caps.has_bad_data_ordering) |
892 | for (i = 0; i < host->data_size; i++) | |
893 | host->buffer[i] = swab32(host->buffer[i]); | |
894 | } | |
7a90dcc2 | 895 | |
796211b7 LD |
896 | if (host->data_size) |
897 | atmci_pdc_set_both_buf(host, | |
898 | ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT)); | |
899 | ||
900 | return iflags; | |
901 | } | |
902 | ||
903 | static u32 | |
74791a2d | 904 | atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data) |
65e8b083 HS |
905 | { |
906 | struct dma_chan *chan; | |
907 | struct dma_async_tx_descriptor *desc; | |
908 | struct scatterlist *sg; | |
909 | unsigned int i; | |
910 | enum dma_data_direction direction; | |
05f5799c | 911 | enum dma_transfer_direction slave_dirn; |
657a77fa | 912 | unsigned int sglen; |
693e5e20 | 913 | u32 maxburst; |
796211b7 LD |
914 | u32 iflags; |
915 | ||
916 | data->error = -EINPROGRESS; | |
917 | ||
918 | WARN_ON(host->data); | |
919 | host->sg = NULL; | |
920 | host->data = data; | |
921 | ||
922 | iflags = ATMCI_DATA_ERROR_FLAGS; | |
65e8b083 HS |
923 | |
924 | /* | |
925 | * We don't do DMA on "complex" transfers, i.e. with | |
926 | * non-word-aligned buffers or lengths. Also, we don't bother | |
927 | * with all the DMA setup overhead for short transfers. | |
928 | */ | |
796211b7 LD |
929 | if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD) |
930 | return atmci_prepare_data(host, data); | |
65e8b083 | 931 | if (data->blksz & 3) |
796211b7 | 932 | return atmci_prepare_data(host, data); |
65e8b083 HS |
933 | |
934 | for_each_sg(data->sg, sg, data->sg_len, i) { | |
935 | if (sg->offset & 3 || sg->length & 3) | |
796211b7 | 936 | return atmci_prepare_data(host, data); |
65e8b083 HS |
937 | } |
938 | ||
939 | /* If we don't have a channel, we can't do DMA */ | |
940 | chan = host->dma.chan; | |
6f49a57a | 941 | if (chan) |
65e8b083 | 942 | host->data_chan = chan; |
65e8b083 HS |
943 | |
944 | if (!chan) | |
945 | return -ENODEV; | |
946 | ||
05f5799c | 947 | if (data->flags & MMC_DATA_READ) { |
65e8b083 | 948 | direction = DMA_FROM_DEVICE; |
e2b35f3d | 949 | host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM; |
693e5e20 | 950 | maxburst = atmci_convert_chksize(host->dma_conf.src_maxburst); |
05f5799c | 951 | } else { |
65e8b083 | 952 | direction = DMA_TO_DEVICE; |
e2b35f3d | 953 | host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV; |
693e5e20 | 954 | maxburst = atmci_convert_chksize(host->dma_conf.dst_maxburst); |
05f5799c | 955 | } |
65e8b083 | 956 | |
693e5e20 NF |
957 | atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(maxburst) | ATMCI_DMAEN); |
958 | ||
266ac3f2 | 959 | sglen = dma_map_sg(chan->device->dev, data->sg, |
796211b7 | 960 | data->sg_len, direction); |
88ce4db3 | 961 | |
e2b35f3d | 962 | dmaengine_slave_config(chan, &host->dma_conf); |
16052827 | 963 | desc = dmaengine_prep_slave_sg(chan, |
05f5799c | 964 | data->sg, sglen, slave_dirn, |
65e8b083 HS |
965 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
966 | if (!desc) | |
657a77fa | 967 | goto unmap_exit; |
65e8b083 HS |
968 | |
969 | host->dma.data_desc = desc; | |
970 | desc->callback = atmci_dma_complete; | |
971 | desc->callback_param = host; | |
65e8b083 | 972 | |
796211b7 | 973 | return iflags; |
657a77fa | 974 | unmap_exit: |
88ce4db3 | 975 | dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction); |
657a77fa | 976 | return -ENOMEM; |
65e8b083 HS |
977 | } |
978 | ||
796211b7 LD |
979 | static void |
980 | atmci_submit_data(struct atmel_mci *host, struct mmc_data *data) | |
981 | { | |
982 | return; | |
983 | } | |
984 | ||
985 | /* | |
986 | * Start PDC according to transfer direction. | |
987 | */ | |
988 | static void | |
989 | atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data) | |
990 | { | |
991 | if (data->flags & MMC_DATA_READ) | |
992 | atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); | |
993 | else | |
994 | atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); | |
995 | } | |
996 | ||
997 | static void | |
998 | atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data) | |
74791a2d NF |
999 | { |
1000 | struct dma_chan *chan = host->data_chan; | |
1001 | struct dma_async_tx_descriptor *desc = host->dma.data_desc; | |
1002 | ||
1003 | if (chan) { | |
5328906a LW |
1004 | dmaengine_submit(desc); |
1005 | dma_async_issue_pending(chan); | |
74791a2d NF |
1006 | } |
1007 | } | |
1008 | ||
796211b7 | 1009 | static void atmci_stop_transfer(struct atmel_mci *host) |
65e8b083 | 1010 | { |
6801c41a LD |
1011 | dev_dbg(&host->pdev->dev, |
1012 | "(%s) set pending xfer complete\n", __func__); | |
65e8b083 | 1013 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
03fc9a7f | 1014 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
65e8b083 HS |
1015 | } |
1016 | ||
7d2be074 | 1017 | /* |
796211b7 | 1018 | * Stop data transfer because error(s) occured. |
7d2be074 | 1019 | */ |
796211b7 | 1020 | static void atmci_stop_transfer_pdc(struct atmel_mci *host) |
7d2be074 | 1021 | { |
f5177547 | 1022 | atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS); |
796211b7 | 1023 | } |
965ebf33 | 1024 | |
796211b7 LD |
1025 | static void atmci_stop_transfer_dma(struct atmel_mci *host) |
1026 | { | |
1027 | struct dma_chan *chan = host->data_chan; | |
965ebf33 | 1028 | |
796211b7 LD |
1029 | if (chan) { |
1030 | dmaengine_terminate_all(chan); | |
1031 | atmci_dma_cleanup(host); | |
1032 | } else { | |
1033 | /* Data transfer was stopped by the interrupt handler */ | |
6801c41a LD |
1034 | dev_dbg(&host->pdev->dev, |
1035 | "(%s) set pending xfer complete\n", __func__); | |
796211b7 LD |
1036 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
1037 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); | |
65e8b083 | 1038 | } |
7d2be074 HS |
1039 | } |
1040 | ||
796211b7 LD |
1041 | /* |
1042 | * Start a request: prepare data if needed, prepare the command and activate | |
1043 | * interrupts. | |
1044 | */ | |
965ebf33 HS |
1045 | static void atmci_start_request(struct atmel_mci *host, |
1046 | struct atmel_mci_slot *slot) | |
7d2be074 | 1047 | { |
965ebf33 | 1048 | struct mmc_request *mrq; |
7d2be074 | 1049 | struct mmc_command *cmd; |
965ebf33 | 1050 | struct mmc_data *data; |
7d2be074 | 1051 | u32 iflags; |
965ebf33 | 1052 | u32 cmdflags; |
7d2be074 | 1053 | |
965ebf33 HS |
1054 | mrq = slot->mrq; |
1055 | host->cur_slot = slot; | |
7d2be074 | 1056 | host->mrq = mrq; |
965ebf33 | 1057 | |
7d2be074 HS |
1058 | host->pending_events = 0; |
1059 | host->completed_events = 0; | |
f5177547 | 1060 | host->cmd_status = 0; |
ca55f46e | 1061 | host->data_status = 0; |
7d2be074 | 1062 | |
6801c41a LD |
1063 | dev_dbg(&host->pdev->dev, "start request: cmd %u\n", mrq->cmd->opcode); |
1064 | ||
24011f34 | 1065 | if (host->need_reset || host->caps.need_reset_after_xfer) { |
18ee684b LD |
1066 | iflags = atmci_readl(host, ATMCI_IMR); |
1067 | iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB); | |
03fc9a7f LD |
1068 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
1069 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN); | |
1070 | atmci_writel(host, ATMCI_MR, host->mode_reg); | |
796211b7 | 1071 | if (host->caps.has_cfg_reg) |
03fc9a7f | 1072 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
18ee684b | 1073 | atmci_writel(host, ATMCI_IER, iflags); |
965ebf33 HS |
1074 | host->need_reset = false; |
1075 | } | |
03fc9a7f | 1076 | atmci_writel(host, ATMCI_SDCR, slot->sdc_reg); |
965ebf33 | 1077 | |
03fc9a7f | 1078 | iflags = atmci_readl(host, ATMCI_IMR); |
2c96a293 | 1079 | if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB)) |
f5177547 | 1080 | dev_dbg(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n", |
965ebf33 HS |
1081 | iflags); |
1082 | ||
1083 | if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) { | |
1084 | /* Send init sequence (74 clock cycles) */ | |
03fc9a7f LD |
1085 | atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT); |
1086 | while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY)) | |
965ebf33 HS |
1087 | cpu_relax(); |
1088 | } | |
74791a2d | 1089 | iflags = 0; |
7d2be074 HS |
1090 | data = mrq->data; |
1091 | if (data) { | |
965ebf33 | 1092 | atmci_set_timeout(host, slot, data); |
a252e3e3 HS |
1093 | |
1094 | /* Must set block count/size before sending command */ | |
03fc9a7f | 1095 | atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks) |
2c96a293 | 1096 | | ATMCI_BLKLEN(data->blksz)); |
965ebf33 | 1097 | dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n", |
2c96a293 | 1098 | ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz)); |
74791a2d | 1099 | |
796211b7 | 1100 | iflags |= host->prepare_data(host, data); |
7d2be074 HS |
1101 | } |
1102 | ||
2c96a293 | 1103 | iflags |= ATMCI_CMDRDY; |
7d2be074 | 1104 | cmd = mrq->cmd; |
965ebf33 | 1105 | cmdflags = atmci_prepare_command(slot->mmc, cmd); |
11d1488b | 1106 | atmci_send_command(host, cmd, cmdflags); |
7d2be074 HS |
1107 | |
1108 | if (data) | |
796211b7 | 1109 | host->submit_data(host, data); |
7d2be074 HS |
1110 | |
1111 | if (mrq->stop) { | |
965ebf33 | 1112 | host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop); |
2c96a293 | 1113 | host->stop_cmdr |= ATMCI_CMDR_STOP_XFER; |
7d2be074 | 1114 | if (!(data->flags & MMC_DATA_WRITE)) |
2c96a293 | 1115 | host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ; |
7d2be074 | 1116 | if (data->flags & MMC_DATA_STREAM) |
2c96a293 | 1117 | host->stop_cmdr |= ATMCI_CMDR_STREAM; |
7d2be074 | 1118 | else |
2c96a293 | 1119 | host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK; |
7d2be074 HS |
1120 | } |
1121 | ||
1122 | /* | |
1123 | * We could have enabled interrupts earlier, but I suspect | |
1124 | * that would open up a nice can of interesting race | |
1125 | * conditions (e.g. command and data complete, but stop not | |
1126 | * prepared yet.) | |
1127 | */ | |
03fc9a7f | 1128 | atmci_writel(host, ATMCI_IER, iflags); |
24011f34 LD |
1129 | |
1130 | mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000)); | |
965ebf33 | 1131 | } |
7d2be074 | 1132 | |
965ebf33 HS |
1133 | static void atmci_queue_request(struct atmel_mci *host, |
1134 | struct atmel_mci_slot *slot, struct mmc_request *mrq) | |
1135 | { | |
1136 | dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n", | |
1137 | host->state); | |
1138 | ||
1139 | spin_lock_bh(&host->lock); | |
1140 | slot->mrq = mrq; | |
1141 | if (host->state == STATE_IDLE) { | |
1142 | host->state = STATE_SENDING_CMD; | |
1143 | atmci_start_request(host, slot); | |
1144 | } else { | |
6801c41a | 1145 | dev_dbg(&host->pdev->dev, "queue request\n"); |
965ebf33 HS |
1146 | list_add_tail(&slot->queue_node, &host->queue); |
1147 | } | |
1148 | spin_unlock_bh(&host->lock); | |
1149 | } | |
7d2be074 | 1150 | |
965ebf33 HS |
1151 | static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
1152 | { | |
1153 | struct atmel_mci_slot *slot = mmc_priv(mmc); | |
1154 | struct atmel_mci *host = slot->host; | |
1155 | struct mmc_data *data; | |
1156 | ||
1157 | WARN_ON(slot->mrq); | |
6801c41a | 1158 | dev_dbg(&host->pdev->dev, "MRQ: cmd %u\n", mrq->cmd->opcode); |
965ebf33 HS |
1159 | |
1160 | /* | |
1161 | * We may "know" the card is gone even though there's still an | |
1162 | * electrical connection. If so, we really need to communicate | |
1163 | * this to the MMC core since there won't be any more | |
1164 | * interrupts as the card is completely removed. Otherwise, | |
1165 | * the MMC core might believe the card is still there even | |
1166 | * though the card was just removed very slowly. | |
1167 | */ | |
1168 | if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) { | |
1169 | mrq->cmd->error = -ENOMEDIUM; | |
1170 | mmc_request_done(mmc, mrq); | |
1171 | return; | |
1172 | } | |
1173 | ||
1174 | /* We don't support multiple blocks of weird lengths. */ | |
1175 | data = mrq->data; | |
1176 | if (data && data->blocks > 1 && data->blksz & 3) { | |
1177 | mrq->cmd->error = -EINVAL; | |
1178 | mmc_request_done(mmc, mrq); | |
1179 | } | |
1180 | ||
1181 | atmci_queue_request(host, slot, mrq); | |
7d2be074 HS |
1182 | } |
1183 | ||
1184 | static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) | |
1185 | { | |
965ebf33 HS |
1186 | struct atmel_mci_slot *slot = mmc_priv(mmc); |
1187 | struct atmel_mci *host = slot->host; | |
1188 | unsigned int i; | |
7d2be074 | 1189 | |
2c96a293 | 1190 | slot->sdc_reg &= ~ATMCI_SDCBUS_MASK; |
945533b5 HS |
1191 | switch (ios->bus_width) { |
1192 | case MMC_BUS_WIDTH_1: | |
2c96a293 | 1193 | slot->sdc_reg |= ATMCI_SDCBUS_1BIT; |
945533b5 HS |
1194 | break; |
1195 | case MMC_BUS_WIDTH_4: | |
2c96a293 | 1196 | slot->sdc_reg |= ATMCI_SDCBUS_4BIT; |
945533b5 HS |
1197 | break; |
1198 | } | |
1199 | ||
7d2be074 | 1200 | if (ios->clock) { |
965ebf33 | 1201 | unsigned int clock_min = ~0U; |
7d2be074 HS |
1202 | u32 clkdiv; |
1203 | ||
965ebf33 HS |
1204 | spin_lock_bh(&host->lock); |
1205 | if (!host->mode_reg) { | |
945533b5 | 1206 | clk_enable(host->mck); |
03fc9a7f LD |
1207 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
1208 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN); | |
796211b7 | 1209 | if (host->caps.has_cfg_reg) |
03fc9a7f | 1210 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
965ebf33 | 1211 | } |
945533b5 | 1212 | |
965ebf33 HS |
1213 | /* |
1214 | * Use mirror of ios->clock to prevent race with mmc | |
1215 | * core ios update when finding the minimum. | |
1216 | */ | |
1217 | slot->clock = ios->clock; | |
2c96a293 | 1218 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
965ebf33 HS |
1219 | if (host->slot[i] && host->slot[i]->clock |
1220 | && host->slot[i]->clock < clock_min) | |
1221 | clock_min = host->slot[i]->clock; | |
1222 | } | |
1223 | ||
1224 | /* Calculate clock divider */ | |
faf8180b LD |
1225 | if (host->caps.has_odd_clk_div) { |
1226 | clkdiv = DIV_ROUND_UP(host->bus_hz, clock_min) - 2; | |
1227 | if (clkdiv > 511) { | |
1228 | dev_warn(&mmc->class_dev, | |
1229 | "clock %u too slow; using %lu\n", | |
1230 | clock_min, host->bus_hz / (511 + 2)); | |
1231 | clkdiv = 511; | |
1232 | } | |
1233 | host->mode_reg = ATMCI_MR_CLKDIV(clkdiv >> 1) | |
1234 | | ATMCI_MR_CLKODD(clkdiv & 1); | |
1235 | } else { | |
1236 | clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1; | |
1237 | if (clkdiv > 255) { | |
1238 | dev_warn(&mmc->class_dev, | |
1239 | "clock %u too slow; using %lu\n", | |
1240 | clock_min, host->bus_hz / (2 * 256)); | |
1241 | clkdiv = 255; | |
1242 | } | |
1243 | host->mode_reg = ATMCI_MR_CLKDIV(clkdiv); | |
7d2be074 HS |
1244 | } |
1245 | ||
965ebf33 HS |
1246 | /* |
1247 | * WRPROOF and RDPROOF prevent overruns/underruns by | |
1248 | * stopping the clock when the FIFO is full/empty. | |
1249 | * This state is not expected to last for long. | |
1250 | */ | |
796211b7 | 1251 | if (host->caps.has_rwproof) |
2c96a293 | 1252 | host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF); |
7d2be074 | 1253 | |
796211b7 | 1254 | if (host->caps.has_cfg_reg) { |
99ddffd8 NF |
1255 | /* setup High Speed mode in relation with card capacity */ |
1256 | if (ios->timing == MMC_TIMING_SD_HS) | |
2c96a293 | 1257 | host->cfg_reg |= ATMCI_CFG_HSMODE; |
99ddffd8 | 1258 | else |
2c96a293 | 1259 | host->cfg_reg &= ~ATMCI_CFG_HSMODE; |
99ddffd8 NF |
1260 | } |
1261 | ||
1262 | if (list_empty(&host->queue)) { | |
03fc9a7f | 1263 | atmci_writel(host, ATMCI_MR, host->mode_reg); |
796211b7 | 1264 | if (host->caps.has_cfg_reg) |
03fc9a7f | 1265 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
99ddffd8 | 1266 | } else { |
965ebf33 | 1267 | host->need_clock_update = true; |
99ddffd8 | 1268 | } |
965ebf33 HS |
1269 | |
1270 | spin_unlock_bh(&host->lock); | |
945533b5 | 1271 | } else { |
965ebf33 HS |
1272 | bool any_slot_active = false; |
1273 | ||
1274 | spin_lock_bh(&host->lock); | |
1275 | slot->clock = 0; | |
2c96a293 | 1276 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
965ebf33 HS |
1277 | if (host->slot[i] && host->slot[i]->clock) { |
1278 | any_slot_active = true; | |
1279 | break; | |
1280 | } | |
945533b5 | 1281 | } |
965ebf33 | 1282 | if (!any_slot_active) { |
03fc9a7f | 1283 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS); |
965ebf33 | 1284 | if (host->mode_reg) { |
03fc9a7f | 1285 | atmci_readl(host, ATMCI_MR); |
965ebf33 HS |
1286 | clk_disable(host->mck); |
1287 | } | |
1288 | host->mode_reg = 0; | |
1289 | } | |
1290 | spin_unlock_bh(&host->lock); | |
7d2be074 HS |
1291 | } |
1292 | ||
1293 | switch (ios->power_mode) { | |
965ebf33 HS |
1294 | case MMC_POWER_UP: |
1295 | set_bit(ATMCI_CARD_NEED_INIT, &slot->flags); | |
1296 | break; | |
7d2be074 HS |
1297 | default: |
1298 | /* | |
1299 | * TODO: None of the currently available AVR32-based | |
1300 | * boards allow MMC power to be turned off. Implement | |
1301 | * power control when this can be tested properly. | |
965ebf33 HS |
1302 | * |
1303 | * We also need to hook this into the clock management | |
1304 | * somehow so that newly inserted cards aren't | |
1305 | * subjected to a fast clock before we have a chance | |
1306 | * to figure out what the maximum rate is. Currently, | |
1307 | * there's no way to avoid this, and there never will | |
1308 | * be for boards that don't support power control. | |
7d2be074 HS |
1309 | */ |
1310 | break; | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | static int atmci_get_ro(struct mmc_host *mmc) | |
1315 | { | |
965ebf33 HS |
1316 | int read_only = -ENOSYS; |
1317 | struct atmel_mci_slot *slot = mmc_priv(mmc); | |
7d2be074 | 1318 | |
965ebf33 HS |
1319 | if (gpio_is_valid(slot->wp_pin)) { |
1320 | read_only = gpio_get_value(slot->wp_pin); | |
7d2be074 HS |
1321 | dev_dbg(&mmc->class_dev, "card is %s\n", |
1322 | read_only ? "read-only" : "read-write"); | |
7d2be074 HS |
1323 | } |
1324 | ||
1325 | return read_only; | |
1326 | } | |
1327 | ||
965ebf33 HS |
1328 | static int atmci_get_cd(struct mmc_host *mmc) |
1329 | { | |
1330 | int present = -ENOSYS; | |
1331 | struct atmel_mci_slot *slot = mmc_priv(mmc); | |
1332 | ||
1333 | if (gpio_is_valid(slot->detect_pin)) { | |
1c1452be JL |
1334 | present = !(gpio_get_value(slot->detect_pin) ^ |
1335 | slot->detect_is_active_high); | |
965ebf33 HS |
1336 | dev_dbg(&mmc->class_dev, "card is %spresent\n", |
1337 | present ? "" : "not "); | |
1338 | } | |
1339 | ||
1340 | return present; | |
1341 | } | |
1342 | ||
88ff82ed AG |
1343 | static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable) |
1344 | { | |
1345 | struct atmel_mci_slot *slot = mmc_priv(mmc); | |
1346 | struct atmel_mci *host = slot->host; | |
1347 | ||
1348 | if (enable) | |
03fc9a7f | 1349 | atmci_writel(host, ATMCI_IER, slot->sdio_irq); |
88ff82ed | 1350 | else |
03fc9a7f | 1351 | atmci_writel(host, ATMCI_IDR, slot->sdio_irq); |
88ff82ed AG |
1352 | } |
1353 | ||
965ebf33 | 1354 | static const struct mmc_host_ops atmci_ops = { |
7d2be074 HS |
1355 | .request = atmci_request, |
1356 | .set_ios = atmci_set_ios, | |
1357 | .get_ro = atmci_get_ro, | |
965ebf33 | 1358 | .get_cd = atmci_get_cd, |
88ff82ed | 1359 | .enable_sdio_irq = atmci_enable_sdio_irq, |
7d2be074 HS |
1360 | }; |
1361 | ||
965ebf33 HS |
1362 | /* Called with host->lock held */ |
1363 | static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq) | |
1364 | __releases(&host->lock) | |
1365 | __acquires(&host->lock) | |
1366 | { | |
1367 | struct atmel_mci_slot *slot = NULL; | |
1368 | struct mmc_host *prev_mmc = host->cur_slot->mmc; | |
1369 | ||
1370 | WARN_ON(host->cmd || host->data); | |
1371 | ||
1372 | /* | |
1373 | * Update the MMC clock rate if necessary. This may be | |
1374 | * necessary if set_ios() is called when a different slot is | |
25985edc | 1375 | * busy transferring data. |
965ebf33 | 1376 | */ |
99ddffd8 | 1377 | if (host->need_clock_update) { |
03fc9a7f | 1378 | atmci_writel(host, ATMCI_MR, host->mode_reg); |
796211b7 | 1379 | if (host->caps.has_cfg_reg) |
03fc9a7f | 1380 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
99ddffd8 | 1381 | } |
965ebf33 HS |
1382 | |
1383 | host->cur_slot->mrq = NULL; | |
1384 | host->mrq = NULL; | |
1385 | if (!list_empty(&host->queue)) { | |
1386 | slot = list_entry(host->queue.next, | |
1387 | struct atmel_mci_slot, queue_node); | |
1388 | list_del(&slot->queue_node); | |
1389 | dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n", | |
1390 | mmc_hostname(slot->mmc)); | |
1391 | host->state = STATE_SENDING_CMD; | |
1392 | atmci_start_request(host, slot); | |
1393 | } else { | |
1394 | dev_vdbg(&host->pdev->dev, "list empty\n"); | |
1395 | host->state = STATE_IDLE; | |
1396 | } | |
1397 | ||
24011f34 LD |
1398 | del_timer(&host->timer); |
1399 | ||
965ebf33 HS |
1400 | spin_unlock(&host->lock); |
1401 | mmc_request_done(prev_mmc, mrq); | |
1402 | spin_lock(&host->lock); | |
1403 | } | |
1404 | ||
7d2be074 | 1405 | static void atmci_command_complete(struct atmel_mci *host, |
c06ad258 | 1406 | struct mmc_command *cmd) |
7d2be074 | 1407 | { |
c06ad258 HS |
1408 | u32 status = host->cmd_status; |
1409 | ||
7d2be074 | 1410 | /* Read the response from the card (up to 16 bytes) */ |
03fc9a7f LD |
1411 | cmd->resp[0] = atmci_readl(host, ATMCI_RSPR); |
1412 | cmd->resp[1] = atmci_readl(host, ATMCI_RSPR); | |
1413 | cmd->resp[2] = atmci_readl(host, ATMCI_RSPR); | |
1414 | cmd->resp[3] = atmci_readl(host, ATMCI_RSPR); | |
7d2be074 | 1415 | |
2c96a293 | 1416 | if (status & ATMCI_RTOE) |
7d2be074 | 1417 | cmd->error = -ETIMEDOUT; |
2c96a293 | 1418 | else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE)) |
7d2be074 | 1419 | cmd->error = -EILSEQ; |
2c96a293 | 1420 | else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE)) |
7d2be074 | 1421 | cmd->error = -EIO; |
24011f34 LD |
1422 | else if (host->mrq->data && (host->mrq->data->blksz & 3)) { |
1423 | if (host->caps.need_blksz_mul_4) { | |
1424 | cmd->error = -EINVAL; | |
1425 | host->need_reset = 1; | |
1426 | } | |
1427 | } else | |
7d2be074 | 1428 | cmd->error = 0; |
7d2be074 HS |
1429 | } |
1430 | ||
1431 | static void atmci_detect_change(unsigned long data) | |
1432 | { | |
965ebf33 HS |
1433 | struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data; |
1434 | bool present; | |
1435 | bool present_old; | |
7d2be074 HS |
1436 | |
1437 | /* | |
965ebf33 HS |
1438 | * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before |
1439 | * freeing the interrupt. We must not re-enable the interrupt | |
1440 | * if it has been freed, and if we're shutting down, it | |
1441 | * doesn't really matter whether the card is present or not. | |
7d2be074 HS |
1442 | */ |
1443 | smp_rmb(); | |
965ebf33 | 1444 | if (test_bit(ATMCI_SHUTDOWN, &slot->flags)) |
7d2be074 HS |
1445 | return; |
1446 | ||
965ebf33 | 1447 | enable_irq(gpio_to_irq(slot->detect_pin)); |
1c1452be JL |
1448 | present = !(gpio_get_value(slot->detect_pin) ^ |
1449 | slot->detect_is_active_high); | |
965ebf33 | 1450 | present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags); |
7d2be074 | 1451 | |
965ebf33 HS |
1452 | dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n", |
1453 | present, present_old); | |
7d2be074 | 1454 | |
965ebf33 HS |
1455 | if (present != present_old) { |
1456 | struct atmel_mci *host = slot->host; | |
1457 | struct mmc_request *mrq; | |
1458 | ||
1459 | dev_dbg(&slot->mmc->class_dev, "card %s\n", | |
7d2be074 | 1460 | present ? "inserted" : "removed"); |
7d2be074 | 1461 | |
965ebf33 HS |
1462 | spin_lock(&host->lock); |
1463 | ||
1464 | if (!present) | |
1465 | clear_bit(ATMCI_CARD_PRESENT, &slot->flags); | |
1466 | else | |
1467 | set_bit(ATMCI_CARD_PRESENT, &slot->flags); | |
7d2be074 HS |
1468 | |
1469 | /* Clean up queue if present */ | |
965ebf33 | 1470 | mrq = slot->mrq; |
7d2be074 | 1471 | if (mrq) { |
965ebf33 HS |
1472 | if (mrq == host->mrq) { |
1473 | /* | |
1474 | * Reset controller to terminate any ongoing | |
1475 | * commands or data transfers. | |
1476 | */ | |
03fc9a7f LD |
1477 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
1478 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN); | |
1479 | atmci_writel(host, ATMCI_MR, host->mode_reg); | |
796211b7 | 1480 | if (host->caps.has_cfg_reg) |
03fc9a7f | 1481 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
965ebf33 HS |
1482 | |
1483 | host->data = NULL; | |
1484 | host->cmd = NULL; | |
1485 | ||
1486 | switch (host->state) { | |
1487 | case STATE_IDLE: | |
c06ad258 | 1488 | break; |
965ebf33 HS |
1489 | case STATE_SENDING_CMD: |
1490 | mrq->cmd->error = -ENOMEDIUM; | |
f5177547 LD |
1491 | if (mrq->data) |
1492 | host->stop_transfer(host); | |
1493 | break; | |
1494 | case STATE_DATA_XFER: | |
c06ad258 | 1495 | mrq->data->error = -ENOMEDIUM; |
796211b7 | 1496 | host->stop_transfer(host); |
c06ad258 | 1497 | break; |
f5177547 LD |
1498 | case STATE_WAITING_NOTBUSY: |
1499 | mrq->data->error = -ENOMEDIUM; | |
1500 | break; | |
965ebf33 HS |
1501 | case STATE_SENDING_STOP: |
1502 | mrq->stop->error = -ENOMEDIUM; | |
1503 | break; | |
f5177547 LD |
1504 | case STATE_END_REQUEST: |
1505 | break; | |
965ebf33 | 1506 | } |
7d2be074 | 1507 | |
965ebf33 HS |
1508 | atmci_request_end(host, mrq); |
1509 | } else { | |
1510 | list_del(&slot->queue_node); | |
1511 | mrq->cmd->error = -ENOMEDIUM; | |
1512 | if (mrq->data) | |
1513 | mrq->data->error = -ENOMEDIUM; | |
1514 | if (mrq->stop) | |
1515 | mrq->stop->error = -ENOMEDIUM; | |
1516 | ||
1517 | spin_unlock(&host->lock); | |
1518 | mmc_request_done(slot->mmc, mrq); | |
1519 | spin_lock(&host->lock); | |
1520 | } | |
7d2be074 | 1521 | } |
965ebf33 | 1522 | spin_unlock(&host->lock); |
7d2be074 | 1523 | |
965ebf33 | 1524 | mmc_detect_change(slot->mmc, 0); |
7d2be074 HS |
1525 | } |
1526 | } | |
1527 | ||
1528 | static void atmci_tasklet_func(unsigned long priv) | |
1529 | { | |
965ebf33 | 1530 | struct atmel_mci *host = (struct atmel_mci *)priv; |
7d2be074 HS |
1531 | struct mmc_request *mrq = host->mrq; |
1532 | struct mmc_data *data = host->data; | |
c06ad258 HS |
1533 | enum atmel_mci_state state = host->state; |
1534 | enum atmel_mci_state prev_state; | |
1535 | u32 status; | |
1536 | ||
965ebf33 HS |
1537 | spin_lock(&host->lock); |
1538 | ||
c06ad258 | 1539 | state = host->state; |
7d2be074 | 1540 | |
965ebf33 | 1541 | dev_vdbg(&host->pdev->dev, |
c06ad258 HS |
1542 | "tasklet: state %u pending/completed/mask %lx/%lx/%x\n", |
1543 | state, host->pending_events, host->completed_events, | |
03fc9a7f | 1544 | atmci_readl(host, ATMCI_IMR)); |
7d2be074 | 1545 | |
c06ad258 HS |
1546 | do { |
1547 | prev_state = state; | |
6801c41a | 1548 | dev_dbg(&host->pdev->dev, "FSM: state=%d\n", state); |
7d2be074 | 1549 | |
c06ad258 | 1550 | switch (state) { |
965ebf33 HS |
1551 | case STATE_IDLE: |
1552 | break; | |
1553 | ||
c06ad258 | 1554 | case STATE_SENDING_CMD: |
f5177547 LD |
1555 | /* |
1556 | * Command has been sent, we are waiting for command | |
1557 | * ready. Then we have three next states possible: | |
1558 | * END_REQUEST by default, WAITING_NOTBUSY if it's a | |
1559 | * command needing it or DATA_XFER if there is data. | |
1560 | */ | |
6801c41a | 1561 | dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n"); |
c06ad258 | 1562 | if (!atmci_test_and_clear_pending(host, |
f5177547 | 1563 | EVENT_CMD_RDY)) |
c06ad258 | 1564 | break; |
7d2be074 | 1565 | |
6801c41a | 1566 | dev_dbg(&host->pdev->dev, "set completed cmd ready\n"); |
c06ad258 | 1567 | host->cmd = NULL; |
f5177547 | 1568 | atmci_set_completed(host, EVENT_CMD_RDY); |
c06ad258 | 1569 | atmci_command_complete(host, mrq->cmd); |
f5177547 | 1570 | if (mrq->data) { |
6801c41a LD |
1571 | dev_dbg(&host->pdev->dev, |
1572 | "command with data transfer"); | |
f5177547 LD |
1573 | /* |
1574 | * If there is a command error don't start | |
1575 | * data transfer. | |
1576 | */ | |
1577 | if (mrq->cmd->error) { | |
1578 | host->stop_transfer(host); | |
1579 | host->data = NULL; | |
1580 | atmci_writel(host, ATMCI_IDR, | |
1581 | ATMCI_TXRDY | ATMCI_RXRDY | |
1582 | | ATMCI_DATA_ERROR_FLAGS); | |
1583 | state = STATE_END_REQUEST; | |
1584 | } else | |
1585 | state = STATE_DATA_XFER; | |
1586 | } else if ((!mrq->data) && (mrq->cmd->flags & MMC_RSP_BUSY)) { | |
6801c41a LD |
1587 | dev_dbg(&host->pdev->dev, |
1588 | "command response need waiting notbusy"); | |
f5177547 LD |
1589 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
1590 | state = STATE_WAITING_NOTBUSY; | |
1591 | } else | |
1592 | state = STATE_END_REQUEST; | |
7d2be074 | 1593 | |
f5177547 | 1594 | break; |
7d2be074 | 1595 | |
f5177547 | 1596 | case STATE_DATA_XFER: |
c06ad258 HS |
1597 | if (atmci_test_and_clear_pending(host, |
1598 | EVENT_DATA_ERROR)) { | |
6801c41a | 1599 | dev_dbg(&host->pdev->dev, "set completed data error\n"); |
f5177547 LD |
1600 | atmci_set_completed(host, EVENT_DATA_ERROR); |
1601 | state = STATE_END_REQUEST; | |
c06ad258 HS |
1602 | break; |
1603 | } | |
7d2be074 | 1604 | |
f5177547 LD |
1605 | /* |
1606 | * A data transfer is in progress. The event expected | |
1607 | * to move to the next state depends of data transfer | |
1608 | * type (PDC or DMA). Once transfer done we can move | |
1609 | * to the next step which is WAITING_NOTBUSY in write | |
1610 | * case and directly SENDING_STOP in read case. | |
1611 | */ | |
6801c41a | 1612 | dev_dbg(&host->pdev->dev, "FSM: xfer complete?\n"); |
c06ad258 HS |
1613 | if (!atmci_test_and_clear_pending(host, |
1614 | EVENT_XFER_COMPLETE)) | |
1615 | break; | |
7d2be074 | 1616 | |
6801c41a LD |
1617 | dev_dbg(&host->pdev->dev, |
1618 | "(%s) set completed xfer complete\n", | |
1619 | __func__); | |
c06ad258 | 1620 | atmci_set_completed(host, EVENT_XFER_COMPLETE); |
7d2be074 | 1621 | |
f5177547 LD |
1622 | if (host->data->flags & MMC_DATA_WRITE) { |
1623 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); | |
1624 | state = STATE_WAITING_NOTBUSY; | |
1625 | } else if (host->mrq->stop) { | |
1626 | atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY); | |
1627 | atmci_send_stop_cmd(host, data); | |
1628 | state = STATE_SENDING_STOP; | |
c06ad258 | 1629 | } else { |
f5177547 | 1630 | host->data = NULL; |
c06ad258 HS |
1631 | data->bytes_xfered = data->blocks * data->blksz; |
1632 | data->error = 0; | |
f5177547 | 1633 | state = STATE_END_REQUEST; |
c06ad258 | 1634 | } |
f5177547 | 1635 | break; |
c06ad258 | 1636 | |
f5177547 LD |
1637 | case STATE_WAITING_NOTBUSY: |
1638 | /* | |
1639 | * We can be in the state for two reasons: a command | |
1640 | * requiring waiting not busy signal (stop command | |
1641 | * included) or a write operation. In the latest case, | |
1642 | * we need to send a stop command. | |
1643 | */ | |
6801c41a | 1644 | dev_dbg(&host->pdev->dev, "FSM: not busy?\n"); |
f5177547 LD |
1645 | if (!atmci_test_and_clear_pending(host, |
1646 | EVENT_NOTBUSY)) | |
1647 | break; | |
7d2be074 | 1648 | |
6801c41a | 1649 | dev_dbg(&host->pdev->dev, "set completed not busy\n"); |
f5177547 LD |
1650 | atmci_set_completed(host, EVENT_NOTBUSY); |
1651 | ||
1652 | if (host->data) { | |
1653 | /* | |
1654 | * For some commands such as CMD53, even if | |
1655 | * there is data transfer, there is no stop | |
1656 | * command to send. | |
1657 | */ | |
1658 | if (host->mrq->stop) { | |
1659 | atmci_writel(host, ATMCI_IER, | |
1660 | ATMCI_CMDRDY); | |
1661 | atmci_send_stop_cmd(host, data); | |
1662 | state = STATE_SENDING_STOP; | |
1663 | } else { | |
1664 | host->data = NULL; | |
1665 | data->bytes_xfered = data->blocks | |
1666 | * data->blksz; | |
1667 | data->error = 0; | |
1668 | state = STATE_END_REQUEST; | |
1669 | } | |
1670 | } else | |
1671 | state = STATE_END_REQUEST; | |
1672 | break; | |
c06ad258 HS |
1673 | |
1674 | case STATE_SENDING_STOP: | |
f5177547 LD |
1675 | /* |
1676 | * In this state, it is important to set host->data to | |
1677 | * NULL (which is tested in the waiting notbusy state) | |
1678 | * in order to go to the end request state instead of | |
1679 | * sending stop again. | |
1680 | */ | |
6801c41a | 1681 | dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n"); |
c06ad258 | 1682 | if (!atmci_test_and_clear_pending(host, |
f5177547 | 1683 | EVENT_CMD_RDY)) |
c06ad258 HS |
1684 | break; |
1685 | ||
6801c41a | 1686 | dev_dbg(&host->pdev->dev, "FSM: cmd ready\n"); |
c06ad258 | 1687 | host->cmd = NULL; |
f5177547 LD |
1688 | host->data = NULL; |
1689 | data->bytes_xfered = data->blocks * data->blksz; | |
1690 | data->error = 0; | |
c06ad258 | 1691 | atmci_command_complete(host, mrq->stop); |
f5177547 LD |
1692 | if (mrq->stop->error) { |
1693 | host->stop_transfer(host); | |
1694 | atmci_writel(host, ATMCI_IDR, | |
1695 | ATMCI_TXRDY | ATMCI_RXRDY | |
1696 | | ATMCI_DATA_ERROR_FLAGS); | |
1697 | state = STATE_END_REQUEST; | |
1698 | } else { | |
1699 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); | |
1700 | state = STATE_WAITING_NOTBUSY; | |
1701 | } | |
1702 | break; | |
c06ad258 | 1703 | |
f5177547 LD |
1704 | case STATE_END_REQUEST: |
1705 | atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY | ATMCI_RXRDY | |
1706 | | ATMCI_DATA_ERROR_FLAGS); | |
1707 | status = host->data_status; | |
1708 | if (unlikely(status)) { | |
1709 | host->stop_transfer(host); | |
1710 | host->data = NULL; | |
1711 | if (status & ATMCI_DTOE) { | |
1712 | data->error = -ETIMEDOUT; | |
1713 | } else if (status & ATMCI_DCRCE) { | |
1714 | data->error = -EILSEQ; | |
1715 | } else { | |
1716 | data->error = -EIO; | |
1717 | } | |
1718 | } | |
c06ad258 | 1719 | |
f5177547 LD |
1720 | atmci_request_end(host, host->mrq); |
1721 | state = STATE_IDLE; | |
c06ad258 HS |
1722 | break; |
1723 | } | |
1724 | } while (state != prev_state); | |
1725 | ||
1726 | host->state = state; | |
965ebf33 | 1727 | |
965ebf33 | 1728 | spin_unlock(&host->lock); |
7d2be074 HS |
1729 | } |
1730 | ||
1731 | static void atmci_read_data_pio(struct atmel_mci *host) | |
1732 | { | |
1733 | struct scatterlist *sg = host->sg; | |
1734 | void *buf = sg_virt(sg); | |
1735 | unsigned int offset = host->pio_offset; | |
1736 | struct mmc_data *data = host->data; | |
1737 | u32 value; | |
1738 | u32 status; | |
1739 | unsigned int nbytes = 0; | |
1740 | ||
1741 | do { | |
03fc9a7f | 1742 | value = atmci_readl(host, ATMCI_RDR); |
7d2be074 HS |
1743 | if (likely(offset + 4 <= sg->length)) { |
1744 | put_unaligned(value, (u32 *)(buf + offset)); | |
1745 | ||
1746 | offset += 4; | |
1747 | nbytes += 4; | |
1748 | ||
1749 | if (offset == sg->length) { | |
5e7184ae | 1750 | flush_dcache_page(sg_page(sg)); |
7d2be074 HS |
1751 | host->sg = sg = sg_next(sg); |
1752 | if (!sg) | |
1753 | goto done; | |
1754 | ||
1755 | offset = 0; | |
1756 | buf = sg_virt(sg); | |
1757 | } | |
1758 | } else { | |
1759 | unsigned int remaining = sg->length - offset; | |
1760 | memcpy(buf + offset, &value, remaining); | |
1761 | nbytes += remaining; | |
1762 | ||
1763 | flush_dcache_page(sg_page(sg)); | |
1764 | host->sg = sg = sg_next(sg); | |
1765 | if (!sg) | |
1766 | goto done; | |
1767 | ||
1768 | offset = 4 - remaining; | |
1769 | buf = sg_virt(sg); | |
1770 | memcpy(buf, (u8 *)&value + remaining, offset); | |
1771 | nbytes += offset; | |
1772 | } | |
1773 | ||
03fc9a7f | 1774 | status = atmci_readl(host, ATMCI_SR); |
7d2be074 | 1775 | if (status & ATMCI_DATA_ERROR_FLAGS) { |
03fc9a7f | 1776 | atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY |
7d2be074 HS |
1777 | | ATMCI_DATA_ERROR_FLAGS)); |
1778 | host->data_status = status; | |
965ebf33 | 1779 | data->bytes_xfered += nbytes; |
965ebf33 | 1780 | return; |
7d2be074 | 1781 | } |
2c96a293 | 1782 | } while (status & ATMCI_RXRDY); |
7d2be074 HS |
1783 | |
1784 | host->pio_offset = offset; | |
1785 | data->bytes_xfered += nbytes; | |
1786 | ||
1787 | return; | |
1788 | ||
1789 | done: | |
03fc9a7f LD |
1790 | atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY); |
1791 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); | |
7d2be074 | 1792 | data->bytes_xfered += nbytes; |
965ebf33 | 1793 | smp_wmb(); |
c06ad258 | 1794 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
7d2be074 HS |
1795 | } |
1796 | ||
1797 | static void atmci_write_data_pio(struct atmel_mci *host) | |
1798 | { | |
1799 | struct scatterlist *sg = host->sg; | |
1800 | void *buf = sg_virt(sg); | |
1801 | unsigned int offset = host->pio_offset; | |
1802 | struct mmc_data *data = host->data; | |
1803 | u32 value; | |
1804 | u32 status; | |
1805 | unsigned int nbytes = 0; | |
1806 | ||
1807 | do { | |
1808 | if (likely(offset + 4 <= sg->length)) { | |
1809 | value = get_unaligned((u32 *)(buf + offset)); | |
03fc9a7f | 1810 | atmci_writel(host, ATMCI_TDR, value); |
7d2be074 HS |
1811 | |
1812 | offset += 4; | |
1813 | nbytes += 4; | |
1814 | if (offset == sg->length) { | |
1815 | host->sg = sg = sg_next(sg); | |
1816 | if (!sg) | |
1817 | goto done; | |
1818 | ||
1819 | offset = 0; | |
1820 | buf = sg_virt(sg); | |
1821 | } | |
1822 | } else { | |
1823 | unsigned int remaining = sg->length - offset; | |
1824 | ||
1825 | value = 0; | |
1826 | memcpy(&value, buf + offset, remaining); | |
1827 | nbytes += remaining; | |
1828 | ||
1829 | host->sg = sg = sg_next(sg); | |
1830 | if (!sg) { | |
03fc9a7f | 1831 | atmci_writel(host, ATMCI_TDR, value); |
7d2be074 HS |
1832 | goto done; |
1833 | } | |
1834 | ||
1835 | offset = 4 - remaining; | |
1836 | buf = sg_virt(sg); | |
1837 | memcpy((u8 *)&value + remaining, buf, offset); | |
03fc9a7f | 1838 | atmci_writel(host, ATMCI_TDR, value); |
7d2be074 HS |
1839 | nbytes += offset; |
1840 | } | |
1841 | ||
03fc9a7f | 1842 | status = atmci_readl(host, ATMCI_SR); |
7d2be074 | 1843 | if (status & ATMCI_DATA_ERROR_FLAGS) { |
03fc9a7f | 1844 | atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY |
7d2be074 HS |
1845 | | ATMCI_DATA_ERROR_FLAGS)); |
1846 | host->data_status = status; | |
965ebf33 | 1847 | data->bytes_xfered += nbytes; |
965ebf33 | 1848 | return; |
7d2be074 | 1849 | } |
2c96a293 | 1850 | } while (status & ATMCI_TXRDY); |
7d2be074 HS |
1851 | |
1852 | host->pio_offset = offset; | |
1853 | data->bytes_xfered += nbytes; | |
1854 | ||
1855 | return; | |
1856 | ||
1857 | done: | |
03fc9a7f LD |
1858 | atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY); |
1859 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); | |
7d2be074 | 1860 | data->bytes_xfered += nbytes; |
965ebf33 | 1861 | smp_wmb(); |
c06ad258 | 1862 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
7d2be074 HS |
1863 | } |
1864 | ||
88ff82ed AG |
1865 | static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status) |
1866 | { | |
1867 | int i; | |
1868 | ||
2c96a293 | 1869 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
88ff82ed AG |
1870 | struct atmel_mci_slot *slot = host->slot[i]; |
1871 | if (slot && (status & slot->sdio_irq)) { | |
1872 | mmc_signal_sdio_irq(slot->mmc); | |
1873 | } | |
1874 | } | |
1875 | } | |
1876 | ||
1877 | ||
7d2be074 HS |
1878 | static irqreturn_t atmci_interrupt(int irq, void *dev_id) |
1879 | { | |
965ebf33 | 1880 | struct atmel_mci *host = dev_id; |
7d2be074 HS |
1881 | u32 status, mask, pending; |
1882 | unsigned int pass_count = 0; | |
1883 | ||
7d2be074 | 1884 | do { |
03fc9a7f LD |
1885 | status = atmci_readl(host, ATMCI_SR); |
1886 | mask = atmci_readl(host, ATMCI_IMR); | |
7d2be074 HS |
1887 | pending = status & mask; |
1888 | if (!pending) | |
1889 | break; | |
1890 | ||
1891 | if (pending & ATMCI_DATA_ERROR_FLAGS) { | |
6801c41a | 1892 | dev_dbg(&host->pdev->dev, "IRQ: data error\n"); |
03fc9a7f | 1893 | atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS |
f5177547 LD |
1894 | | ATMCI_RXRDY | ATMCI_TXRDY |
1895 | | ATMCI_ENDRX | ATMCI_ENDTX | |
1896 | | ATMCI_RXBUFF | ATMCI_TXBUFE); | |
965ebf33 | 1897 | |
7d2be074 | 1898 | host->data_status = status; |
6801c41a | 1899 | dev_dbg(&host->pdev->dev, "set pending data error\n"); |
965ebf33 | 1900 | smp_wmb(); |
7d2be074 HS |
1901 | atmci_set_pending(host, EVENT_DATA_ERROR); |
1902 | tasklet_schedule(&host->tasklet); | |
1903 | } | |
796211b7 | 1904 | |
796211b7 | 1905 | if (pending & ATMCI_TXBUFE) { |
6801c41a | 1906 | dev_dbg(&host->pdev->dev, "IRQ: tx buffer empty\n"); |
796211b7 | 1907 | atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE); |
7e8ba228 | 1908 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX); |
796211b7 LD |
1909 | /* |
1910 | * We can receive this interruption before having configured | |
1911 | * the second pdc buffer, so we need to reconfigure first and | |
1912 | * second buffers again | |
1913 | */ | |
1914 | if (host->data_size) { | |
1915 | atmci_pdc_set_both_buf(host, XFER_TRANSMIT); | |
7e8ba228 | 1916 | atmci_writel(host, ATMCI_IER, ATMCI_ENDTX); |
796211b7 LD |
1917 | atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE); |
1918 | } else { | |
1919 | atmci_pdc_complete(host); | |
1920 | } | |
7e8ba228 | 1921 | } else if (pending & ATMCI_ENDTX) { |
6801c41a | 1922 | dev_dbg(&host->pdev->dev, "IRQ: end of tx buffer\n"); |
7e8ba228 | 1923 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX); |
796211b7 LD |
1924 | |
1925 | if (host->data_size) { | |
1926 | atmci_pdc_set_single_buf(host, | |
7e8ba228 LD |
1927 | XFER_TRANSMIT, PDC_SECOND_BUF); |
1928 | atmci_writel(host, ATMCI_IER, ATMCI_ENDTX); | |
796211b7 LD |
1929 | } |
1930 | } | |
1931 | ||
1932 | if (pending & ATMCI_RXBUFF) { | |
6801c41a | 1933 | dev_dbg(&host->pdev->dev, "IRQ: rx buffer full\n"); |
796211b7 | 1934 | atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF); |
7e8ba228 | 1935 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX); |
796211b7 LD |
1936 | /* |
1937 | * We can receive this interruption before having configured | |
1938 | * the second pdc buffer, so we need to reconfigure first and | |
1939 | * second buffers again | |
1940 | */ | |
1941 | if (host->data_size) { | |
1942 | atmci_pdc_set_both_buf(host, XFER_RECEIVE); | |
7e8ba228 | 1943 | atmci_writel(host, ATMCI_IER, ATMCI_ENDRX); |
796211b7 LD |
1944 | atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF); |
1945 | } else { | |
1946 | atmci_pdc_complete(host); | |
1947 | } | |
7e8ba228 | 1948 | } else if (pending & ATMCI_ENDRX) { |
6801c41a | 1949 | dev_dbg(&host->pdev->dev, "IRQ: end of rx buffer\n"); |
7e8ba228 LD |
1950 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX); |
1951 | ||
1952 | if (host->data_size) { | |
1953 | atmci_pdc_set_single_buf(host, | |
1954 | XFER_RECEIVE, PDC_SECOND_BUF); | |
1955 | atmci_writel(host, ATMCI_IER, ATMCI_ENDRX); | |
1956 | } | |
796211b7 LD |
1957 | } |
1958 | ||
f5177547 LD |
1959 | /* |
1960 | * First mci IPs, so mainly the ones having pdc, have some | |
1961 | * issues with the notbusy signal. You can't get it after | |
1962 | * data transmission if you have not sent a stop command. | |
1963 | * The appropriate workaround is to use the BLKE signal. | |
1964 | */ | |
1965 | if (pending & ATMCI_BLKE) { | |
6801c41a | 1966 | dev_dbg(&host->pdev->dev, "IRQ: blke\n"); |
f5177547 LD |
1967 | atmci_writel(host, ATMCI_IDR, ATMCI_BLKE); |
1968 | smp_wmb(); | |
6801c41a | 1969 | dev_dbg(&host->pdev->dev, "set pending notbusy\n"); |
f5177547 LD |
1970 | atmci_set_pending(host, EVENT_NOTBUSY); |
1971 | tasklet_schedule(&host->tasklet); | |
1972 | } | |
7e8ba228 | 1973 | |
2c96a293 | 1974 | if (pending & ATMCI_NOTBUSY) { |
6801c41a | 1975 | dev_dbg(&host->pdev->dev, "IRQ: not_busy\n"); |
f5177547 | 1976 | atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY); |
965ebf33 | 1977 | smp_wmb(); |
6801c41a | 1978 | dev_dbg(&host->pdev->dev, "set pending notbusy\n"); |
f5177547 | 1979 | atmci_set_pending(host, EVENT_NOTBUSY); |
7d2be074 HS |
1980 | tasklet_schedule(&host->tasklet); |
1981 | } | |
f5177547 | 1982 | |
2c96a293 | 1983 | if (pending & ATMCI_RXRDY) |
7d2be074 | 1984 | atmci_read_data_pio(host); |
2c96a293 | 1985 | if (pending & ATMCI_TXRDY) |
7d2be074 HS |
1986 | atmci_write_data_pio(host); |
1987 | ||
f5177547 | 1988 | if (pending & ATMCI_CMDRDY) { |
6801c41a | 1989 | dev_dbg(&host->pdev->dev, "IRQ: cmd ready\n"); |
f5177547 LD |
1990 | atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY); |
1991 | host->cmd_status = status; | |
1992 | smp_wmb(); | |
6801c41a | 1993 | dev_dbg(&host->pdev->dev, "set pending cmd rdy\n"); |
f5177547 LD |
1994 | atmci_set_pending(host, EVENT_CMD_RDY); |
1995 | tasklet_schedule(&host->tasklet); | |
1996 | } | |
88ff82ed | 1997 | |
2c96a293 | 1998 | if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB)) |
88ff82ed AG |
1999 | atmci_sdio_interrupt(host, status); |
2000 | ||
7d2be074 HS |
2001 | } while (pass_count++ < 5); |
2002 | ||
7d2be074 HS |
2003 | return pass_count ? IRQ_HANDLED : IRQ_NONE; |
2004 | } | |
2005 | ||
2006 | static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id) | |
2007 | { | |
965ebf33 | 2008 | struct atmel_mci_slot *slot = dev_id; |
7d2be074 HS |
2009 | |
2010 | /* | |
2011 | * Disable interrupts until the pin has stabilized and check | |
2012 | * the state then. Use mod_timer() since we may be in the | |
2013 | * middle of the timer routine when this interrupt triggers. | |
2014 | */ | |
2015 | disable_irq_nosync(irq); | |
965ebf33 | 2016 | mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20)); |
7d2be074 HS |
2017 | |
2018 | return IRQ_HANDLED; | |
2019 | } | |
2020 | ||
965ebf33 HS |
2021 | static int __init atmci_init_slot(struct atmel_mci *host, |
2022 | struct mci_slot_pdata *slot_data, unsigned int id, | |
88ff82ed | 2023 | u32 sdc_reg, u32 sdio_irq) |
965ebf33 HS |
2024 | { |
2025 | struct mmc_host *mmc; | |
2026 | struct atmel_mci_slot *slot; | |
2027 | ||
2028 | mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev); | |
2029 | if (!mmc) | |
2030 | return -ENOMEM; | |
2031 | ||
2032 | slot = mmc_priv(mmc); | |
2033 | slot->mmc = mmc; | |
2034 | slot->host = host; | |
2035 | slot->detect_pin = slot_data->detect_pin; | |
2036 | slot->wp_pin = slot_data->wp_pin; | |
1c1452be | 2037 | slot->detect_is_active_high = slot_data->detect_is_active_high; |
965ebf33 | 2038 | slot->sdc_reg = sdc_reg; |
88ff82ed | 2039 | slot->sdio_irq = sdio_irq; |
965ebf33 HS |
2040 | |
2041 | mmc->ops = &atmci_ops; | |
2042 | mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512); | |
2043 | mmc->f_max = host->bus_hz / 2; | |
2044 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; | |
88ff82ed AG |
2045 | if (sdio_irq) |
2046 | mmc->caps |= MMC_CAP_SDIO_IRQ; | |
796211b7 | 2047 | if (host->caps.has_highspeed) |
99ddffd8 | 2048 | mmc->caps |= MMC_CAP_SD_HIGHSPEED; |
7a90dcc2 LD |
2049 | /* |
2050 | * Without the read/write proof capability, it is strongly suggested to | |
2051 | * use only one bit for data to prevent fifo underruns and overruns | |
2052 | * which will corrupt data. | |
2053 | */ | |
2054 | if ((slot_data->bus_width >= 4) && host->caps.has_rwproof) | |
965ebf33 HS |
2055 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
2056 | ||
7a90dcc2 LD |
2057 | if (atmci_get_version(host) < 0x200) { |
2058 | mmc->max_segs = 256; | |
2059 | mmc->max_blk_size = 4095; | |
2060 | mmc->max_blk_count = 256; | |
2061 | mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; | |
2062 | mmc->max_seg_size = mmc->max_blk_size * mmc->max_segs; | |
2063 | } else { | |
2064 | mmc->max_segs = 64; | |
2065 | mmc->max_req_size = 32768 * 512; | |
2066 | mmc->max_blk_size = 32768; | |
2067 | mmc->max_blk_count = 512; | |
2068 | } | |
965ebf33 HS |
2069 | |
2070 | /* Assume card is present initially */ | |
2071 | set_bit(ATMCI_CARD_PRESENT, &slot->flags); | |
2072 | if (gpio_is_valid(slot->detect_pin)) { | |
2073 | if (gpio_request(slot->detect_pin, "mmc_detect")) { | |
2074 | dev_dbg(&mmc->class_dev, "no detect pin available\n"); | |
2075 | slot->detect_pin = -EBUSY; | |
1c1452be JL |
2076 | } else if (gpio_get_value(slot->detect_pin) ^ |
2077 | slot->detect_is_active_high) { | |
965ebf33 HS |
2078 | clear_bit(ATMCI_CARD_PRESENT, &slot->flags); |
2079 | } | |
2080 | } | |
2081 | ||
2082 | if (!gpio_is_valid(slot->detect_pin)) | |
2083 | mmc->caps |= MMC_CAP_NEEDS_POLL; | |
2084 | ||
2085 | if (gpio_is_valid(slot->wp_pin)) { | |
2086 | if (gpio_request(slot->wp_pin, "mmc_wp")) { | |
2087 | dev_dbg(&mmc->class_dev, "no WP pin available\n"); | |
2088 | slot->wp_pin = -EBUSY; | |
2089 | } | |
2090 | } | |
2091 | ||
2092 | host->slot[id] = slot; | |
2093 | mmc_add_host(mmc); | |
2094 | ||
2095 | if (gpio_is_valid(slot->detect_pin)) { | |
2096 | int ret; | |
2097 | ||
2098 | setup_timer(&slot->detect_timer, atmci_detect_change, | |
2099 | (unsigned long)slot); | |
2100 | ||
2101 | ret = request_irq(gpio_to_irq(slot->detect_pin), | |
2102 | atmci_detect_interrupt, | |
2103 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, | |
2104 | "mmc-detect", slot); | |
2105 | if (ret) { | |
2106 | dev_dbg(&mmc->class_dev, | |
2107 | "could not request IRQ %d for detect pin\n", | |
2108 | gpio_to_irq(slot->detect_pin)); | |
2109 | gpio_free(slot->detect_pin); | |
2110 | slot->detect_pin = -EBUSY; | |
2111 | } | |
2112 | } | |
2113 | ||
2114 | atmci_init_debugfs(slot); | |
2115 | ||
2116 | return 0; | |
2117 | } | |
2118 | ||
2119 | static void __exit atmci_cleanup_slot(struct atmel_mci_slot *slot, | |
2120 | unsigned int id) | |
2121 | { | |
2122 | /* Debugfs stuff is cleaned up by mmc core */ | |
2123 | ||
2124 | set_bit(ATMCI_SHUTDOWN, &slot->flags); | |
2125 | smp_wmb(); | |
2126 | ||
2127 | mmc_remove_host(slot->mmc); | |
2128 | ||
2129 | if (gpio_is_valid(slot->detect_pin)) { | |
2130 | int pin = slot->detect_pin; | |
2131 | ||
2132 | free_irq(gpio_to_irq(pin), slot); | |
2133 | del_timer_sync(&slot->detect_timer); | |
2134 | gpio_free(pin); | |
2135 | } | |
2136 | if (gpio_is_valid(slot->wp_pin)) | |
2137 | gpio_free(slot->wp_pin); | |
2138 | ||
2139 | slot->host->slot[id] = NULL; | |
2140 | mmc_free_host(slot->mmc); | |
2141 | } | |
2142 | ||
2c96a293 | 2143 | static bool atmci_filter(struct dma_chan *chan, void *slave) |
74465b4f | 2144 | { |
2635d1ba | 2145 | struct mci_dma_data *sl = slave; |
74465b4f | 2146 | |
2635d1ba NF |
2147 | if (sl && find_slave_dev(sl) == chan->device->dev) { |
2148 | chan->private = slave_data_ptr(sl); | |
7dd60251 | 2149 | return true; |
2635d1ba | 2150 | } else { |
7dd60251 | 2151 | return false; |
2635d1ba | 2152 | } |
74465b4f | 2153 | } |
2635d1ba | 2154 | |
ef878198 | 2155 | static bool atmci_configure_dma(struct atmel_mci *host) |
2635d1ba NF |
2156 | { |
2157 | struct mci_platform_data *pdata; | |
2158 | ||
2159 | if (host == NULL) | |
ef878198 | 2160 | return false; |
2635d1ba NF |
2161 | |
2162 | pdata = host->pdev->dev.platform_data; | |
2163 | ||
2164 | if (pdata && find_slave_dev(pdata->dma_slave)) { | |
2165 | dma_cap_mask_t mask; | |
2166 | ||
2635d1ba NF |
2167 | /* Try to grab a DMA channel */ |
2168 | dma_cap_zero(mask); | |
2169 | dma_cap_set(DMA_SLAVE, mask); | |
2170 | host->dma.chan = | |
2c96a293 | 2171 | dma_request_channel(mask, atmci_filter, pdata->dma_slave); |
2635d1ba | 2172 | } |
ef878198 LD |
2173 | if (!host->dma.chan) { |
2174 | dev_warn(&host->pdev->dev, "no DMA channel available\n"); | |
2175 | return false; | |
2176 | } else { | |
74791a2d | 2177 | dev_info(&host->pdev->dev, |
b81cfc41 | 2178 | "using %s for DMA transfers\n", |
74791a2d | 2179 | dma_chan_name(host->dma.chan)); |
e2b35f3d VK |
2180 | |
2181 | host->dma_conf.src_addr = host->mapbase + ATMCI_RDR; | |
2182 | host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; | |
2183 | host->dma_conf.src_maxburst = 1; | |
2184 | host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR; | |
2185 | host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; | |
2186 | host->dma_conf.dst_maxburst = 1; | |
2187 | host->dma_conf.device_fc = false; | |
ef878198 LD |
2188 | return true; |
2189 | } | |
2635d1ba | 2190 | } |
796211b7 | 2191 | |
796211b7 LD |
2192 | /* |
2193 | * HSMCI (High Speed MCI) module is not fully compatible with MCI module. | |
2194 | * HSMCI provides DMA support and a new config register but no more supports | |
2195 | * PDC. | |
2196 | */ | |
2197 | static void __init atmci_get_cap(struct atmel_mci *host) | |
2198 | { | |
2199 | unsigned int version; | |
2200 | ||
2201 | version = atmci_get_version(host); | |
2202 | dev_info(&host->pdev->dev, | |
2203 | "version: 0x%x\n", version); | |
2204 | ||
2205 | host->caps.has_dma = 0; | |
faf8180b | 2206 | host->caps.has_pdc = 1; |
796211b7 LD |
2207 | host->caps.has_cfg_reg = 0; |
2208 | host->caps.has_cstor_reg = 0; | |
2209 | host->caps.has_highspeed = 0; | |
2210 | host->caps.has_rwproof = 0; | |
faf8180b | 2211 | host->caps.has_odd_clk_div = 0; |
24011f34 LD |
2212 | host->caps.has_bad_data_ordering = 1; |
2213 | host->caps.need_reset_after_xfer = 1; | |
2214 | host->caps.need_blksz_mul_4 = 1; | |
796211b7 LD |
2215 | |
2216 | /* keep only major version number */ | |
2217 | switch (version & 0xf00) { | |
796211b7 | 2218 | case 0x500: |
faf8180b LD |
2219 | host->caps.has_odd_clk_div = 1; |
2220 | case 0x400: | |
2221 | case 0x300: | |
796211b7 LD |
2222 | #ifdef CONFIG_AT_HDMAC |
2223 | host->caps.has_dma = 1; | |
2635d1ba | 2224 | #else |
796211b7 LD |
2225 | dev_info(&host->pdev->dev, |
2226 | "has dma capability but dma engine is not selected, then use pio\n"); | |
74465b4f | 2227 | #endif |
faf8180b | 2228 | host->caps.has_pdc = 0; |
796211b7 LD |
2229 | host->caps.has_cfg_reg = 1; |
2230 | host->caps.has_cstor_reg = 1; | |
2231 | host->caps.has_highspeed = 1; | |
faf8180b | 2232 | case 0x200: |
796211b7 | 2233 | host->caps.has_rwproof = 1; |
24011f34 | 2234 | host->caps.need_blksz_mul_4 = 0; |
faf8180b | 2235 | case 0x100: |
24011f34 LD |
2236 | host->caps.has_bad_data_ordering = 0; |
2237 | host->caps.need_reset_after_xfer = 0; | |
2238 | case 0x0: | |
796211b7 LD |
2239 | break; |
2240 | default: | |
faf8180b | 2241 | host->caps.has_pdc = 0; |
796211b7 LD |
2242 | dev_warn(&host->pdev->dev, |
2243 | "Unmanaged mci version, set minimum capabilities\n"); | |
2244 | break; | |
2245 | } | |
2246 | } | |
74465b4f | 2247 | |
7d2be074 HS |
2248 | static int __init atmci_probe(struct platform_device *pdev) |
2249 | { | |
2250 | struct mci_platform_data *pdata; | |
965ebf33 HS |
2251 | struct atmel_mci *host; |
2252 | struct resource *regs; | |
2253 | unsigned int nr_slots; | |
2254 | int irq; | |
2255 | int ret; | |
7d2be074 HS |
2256 | |
2257 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
2258 | if (!regs) | |
2259 | return -ENXIO; | |
2260 | pdata = pdev->dev.platform_data; | |
2261 | if (!pdata) | |
2262 | return -ENXIO; | |
2263 | irq = platform_get_irq(pdev, 0); | |
2264 | if (irq < 0) | |
2265 | return irq; | |
2266 | ||
965ebf33 HS |
2267 | host = kzalloc(sizeof(struct atmel_mci), GFP_KERNEL); |
2268 | if (!host) | |
7d2be074 HS |
2269 | return -ENOMEM; |
2270 | ||
7d2be074 | 2271 | host->pdev = pdev; |
965ebf33 HS |
2272 | spin_lock_init(&host->lock); |
2273 | INIT_LIST_HEAD(&host->queue); | |
7d2be074 HS |
2274 | |
2275 | host->mck = clk_get(&pdev->dev, "mci_clk"); | |
2276 | if (IS_ERR(host->mck)) { | |
2277 | ret = PTR_ERR(host->mck); | |
2278 | goto err_clk_get; | |
2279 | } | |
2280 | ||
2281 | ret = -ENOMEM; | |
e8e3f6ca | 2282 | host->regs = ioremap(regs->start, resource_size(regs)); |
7d2be074 HS |
2283 | if (!host->regs) |
2284 | goto err_ioremap; | |
2285 | ||
2286 | clk_enable(host->mck); | |
03fc9a7f | 2287 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
7d2be074 HS |
2288 | host->bus_hz = clk_get_rate(host->mck); |
2289 | clk_disable(host->mck); | |
2290 | ||
2291 | host->mapbase = regs->start; | |
2292 | ||
965ebf33 | 2293 | tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host); |
7d2be074 | 2294 | |
89c8aa20 | 2295 | ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host); |
7d2be074 HS |
2296 | if (ret) |
2297 | goto err_request_irq; | |
2298 | ||
796211b7 LD |
2299 | /* Get MCI capabilities and set operations according to it */ |
2300 | atmci_get_cap(host); | |
ef878198 | 2301 | if (host->caps.has_dma && atmci_configure_dma(host)) { |
796211b7 LD |
2302 | host->prepare_data = &atmci_prepare_data_dma; |
2303 | host->submit_data = &atmci_submit_data_dma; | |
2304 | host->stop_transfer = &atmci_stop_transfer_dma; | |
2305 | } else if (host->caps.has_pdc) { | |
2306 | dev_info(&pdev->dev, "using PDC\n"); | |
2307 | host->prepare_data = &atmci_prepare_data_pdc; | |
2308 | host->submit_data = &atmci_submit_data_pdc; | |
2309 | host->stop_transfer = &atmci_stop_transfer_pdc; | |
2310 | } else { | |
ef878198 | 2311 | dev_info(&pdev->dev, "using PIO\n"); |
796211b7 LD |
2312 | host->prepare_data = &atmci_prepare_data; |
2313 | host->submit_data = &atmci_submit_data; | |
2314 | host->stop_transfer = &atmci_stop_transfer; | |
2315 | } | |
2316 | ||
7d2be074 HS |
2317 | platform_set_drvdata(pdev, host); |
2318 | ||
b87cc1b5 LD |
2319 | setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host); |
2320 | ||
965ebf33 HS |
2321 | /* We need at least one slot to succeed */ |
2322 | nr_slots = 0; | |
2323 | ret = -ENODEV; | |
2324 | if (pdata->slot[0].bus_width) { | |
2325 | ret = atmci_init_slot(host, &pdata->slot[0], | |
2c96a293 | 2326 | 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA); |
7a90dcc2 | 2327 | if (!ret) { |
965ebf33 | 2328 | nr_slots++; |
7a90dcc2 LD |
2329 | host->buf_size = host->slot[0]->mmc->max_req_size; |
2330 | } | |
965ebf33 HS |
2331 | } |
2332 | if (pdata->slot[1].bus_width) { | |
2333 | ret = atmci_init_slot(host, &pdata->slot[1], | |
2c96a293 | 2334 | 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB); |
7a90dcc2 | 2335 | if (!ret) { |
965ebf33 | 2336 | nr_slots++; |
7a90dcc2 LD |
2337 | if (host->slot[1]->mmc->max_req_size > host->buf_size) |
2338 | host->buf_size = | |
2339 | host->slot[1]->mmc->max_req_size; | |
2340 | } | |
7d2be074 HS |
2341 | } |
2342 | ||
04d699c3 RE |
2343 | if (!nr_slots) { |
2344 | dev_err(&pdev->dev, "init failed: no slot defined\n"); | |
965ebf33 | 2345 | goto err_init_slot; |
04d699c3 | 2346 | } |
7d2be074 | 2347 | |
7a90dcc2 LD |
2348 | if (!host->caps.has_rwproof) { |
2349 | host->buffer = dma_alloc_coherent(&pdev->dev, host->buf_size, | |
2350 | &host->buf_phys_addr, | |
2351 | GFP_KERNEL); | |
2352 | if (!host->buffer) { | |
2353 | ret = -ENOMEM; | |
2354 | dev_err(&pdev->dev, "buffer allocation failed\n"); | |
2355 | goto err_init_slot; | |
2356 | } | |
2357 | } | |
2358 | ||
965ebf33 HS |
2359 | dev_info(&pdev->dev, |
2360 | "Atmel MCI controller at 0x%08lx irq %d, %u slots\n", | |
2361 | host->mapbase, irq, nr_slots); | |
deec9ae3 | 2362 | |
7d2be074 HS |
2363 | return 0; |
2364 | ||
965ebf33 | 2365 | err_init_slot: |
74465b4f DW |
2366 | if (host->dma.chan) |
2367 | dma_release_channel(host->dma.chan); | |
965ebf33 | 2368 | free_irq(irq, host); |
7d2be074 HS |
2369 | err_request_irq: |
2370 | iounmap(host->regs); | |
2371 | err_ioremap: | |
2372 | clk_put(host->mck); | |
2373 | err_clk_get: | |
965ebf33 | 2374 | kfree(host); |
7d2be074 HS |
2375 | return ret; |
2376 | } | |
2377 | ||
2378 | static int __exit atmci_remove(struct platform_device *pdev) | |
2379 | { | |
965ebf33 HS |
2380 | struct atmel_mci *host = platform_get_drvdata(pdev); |
2381 | unsigned int i; | |
7d2be074 HS |
2382 | |
2383 | platform_set_drvdata(pdev, NULL); | |
2384 | ||
7a90dcc2 LD |
2385 | if (host->buffer) |
2386 | dma_free_coherent(&pdev->dev, host->buf_size, | |
2387 | host->buffer, host->buf_phys_addr); | |
2388 | ||
2c96a293 | 2389 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
965ebf33 HS |
2390 | if (host->slot[i]) |
2391 | atmci_cleanup_slot(host->slot[i], i); | |
2392 | } | |
7d2be074 | 2393 | |
965ebf33 | 2394 | clk_enable(host->mck); |
03fc9a7f LD |
2395 | atmci_writel(host, ATMCI_IDR, ~0UL); |
2396 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS); | |
2397 | atmci_readl(host, ATMCI_SR); | |
965ebf33 | 2398 | clk_disable(host->mck); |
7d2be074 | 2399 | |
65e8b083 | 2400 | #ifdef CONFIG_MMC_ATMELMCI_DMA |
74465b4f DW |
2401 | if (host->dma.chan) |
2402 | dma_release_channel(host->dma.chan); | |
65e8b083 HS |
2403 | #endif |
2404 | ||
965ebf33 HS |
2405 | free_irq(platform_get_irq(pdev, 0), host); |
2406 | iounmap(host->regs); | |
7d2be074 | 2407 | |
965ebf33 HS |
2408 | clk_put(host->mck); |
2409 | kfree(host); | |
7d2be074 | 2410 | |
7d2be074 HS |
2411 | return 0; |
2412 | } | |
2413 | ||
5c2f2b9b NF |
2414 | #ifdef CONFIG_PM |
2415 | static int atmci_suspend(struct device *dev) | |
2416 | { | |
2417 | struct atmel_mci *host = dev_get_drvdata(dev); | |
2418 | int i; | |
2419 | ||
2c96a293 | 2420 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
5c2f2b9b NF |
2421 | struct atmel_mci_slot *slot = host->slot[i]; |
2422 | int ret; | |
2423 | ||
2424 | if (!slot) | |
2425 | continue; | |
2426 | ret = mmc_suspend_host(slot->mmc); | |
2427 | if (ret < 0) { | |
2428 | while (--i >= 0) { | |
2429 | slot = host->slot[i]; | |
2430 | if (slot | |
2431 | && test_bit(ATMCI_SUSPENDED, &slot->flags)) { | |
2432 | mmc_resume_host(host->slot[i]->mmc); | |
2433 | clear_bit(ATMCI_SUSPENDED, &slot->flags); | |
2434 | } | |
2435 | } | |
2436 | return ret; | |
2437 | } else { | |
2438 | set_bit(ATMCI_SUSPENDED, &slot->flags); | |
2439 | } | |
2440 | } | |
2441 | ||
2442 | return 0; | |
2443 | } | |
2444 | ||
2445 | static int atmci_resume(struct device *dev) | |
2446 | { | |
2447 | struct atmel_mci *host = dev_get_drvdata(dev); | |
2448 | int i; | |
2449 | int ret = 0; | |
2450 | ||
2c96a293 | 2451 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
5c2f2b9b NF |
2452 | struct atmel_mci_slot *slot = host->slot[i]; |
2453 | int err; | |
2454 | ||
2455 | slot = host->slot[i]; | |
2456 | if (!slot) | |
2457 | continue; | |
2458 | if (!test_bit(ATMCI_SUSPENDED, &slot->flags)) | |
2459 | continue; | |
2460 | err = mmc_resume_host(slot->mmc); | |
2461 | if (err < 0) | |
2462 | ret = err; | |
2463 | else | |
2464 | clear_bit(ATMCI_SUSPENDED, &slot->flags); | |
2465 | } | |
2466 | ||
2467 | return ret; | |
2468 | } | |
2469 | static SIMPLE_DEV_PM_OPS(atmci_pm, atmci_suspend, atmci_resume); | |
2470 | #define ATMCI_PM_OPS (&atmci_pm) | |
2471 | #else | |
2472 | #define ATMCI_PM_OPS NULL | |
2473 | #endif | |
2474 | ||
7d2be074 HS |
2475 | static struct platform_driver atmci_driver = { |
2476 | .remove = __exit_p(atmci_remove), | |
2477 | .driver = { | |
2478 | .name = "atmel_mci", | |
5c2f2b9b | 2479 | .pm = ATMCI_PM_OPS, |
7d2be074 HS |
2480 | }, |
2481 | }; | |
2482 | ||
2483 | static int __init atmci_init(void) | |
2484 | { | |
2485 | return platform_driver_probe(&atmci_driver, atmci_probe); | |
2486 | } | |
2487 | ||
2488 | static void __exit atmci_exit(void) | |
2489 | { | |
2490 | platform_driver_unregister(&atmci_driver); | |
2491 | } | |
2492 | ||
74465b4f | 2493 | late_initcall(atmci_init); /* try to load after dma driver when built-in */ |
7d2be074 HS |
2494 | module_exit(atmci_exit); |
2495 | ||
2496 | MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver"); | |
e05503ef | 2497 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
7d2be074 | 2498 | MODULE_LICENSE("GPL v2"); |