Commit | Line | Data |
---|---|---|
56ca9040 | 1 | /* |
70f10482 | 2 | * linux/drivers/mmc/host/imxmmc.c - Motorola i.MX MMCI driver |
56ca9040 PP |
3 | * |
4 | * Copyright (C) 2004 Sascha Hauer, Pengutronix <sascha@saschahauer.de> | |
5 | * Copyright (C) 2006 Pavel Pisa, PiKRON <ppisa@pikron.com> | |
6 | * | |
7 | * derived from pxamci.c by Russell King | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | * | |
56ca9040 | 13 | */ |
56ca9040 | 14 | |
56ca9040 PP |
15 | #include <linux/module.h> |
16 | #include <linux/init.h> | |
17 | #include <linux/ioport.h> | |
18 | #include <linux/platform_device.h> | |
19 | #include <linux/interrupt.h> | |
20 | #include <linux/blkdev.h> | |
21 | #include <linux/dma-mapping.h> | |
22 | #include <linux/mmc/host.h> | |
23 | #include <linux/mmc/card.h> | |
56ca9040 | 24 | #include <linux/delay.h> |
38a41fdf | 25 | #include <linux/clk.h> |
4b7c0e4c | 26 | #include <linux/io.h> |
56ca9040 PP |
27 | |
28 | #include <asm/dma.h> | |
56ca9040 PP |
29 | #include <asm/irq.h> |
30 | #include <asm/sizes.h> | |
a09e64fb RK |
31 | #include <mach/mmc.h> |
32 | #include <mach/imx-dma.h> | |
56ca9040 PP |
33 | |
34 | #include "imxmmc.h" | |
35 | ||
36 | #define DRIVER_NAME "imx-mmc" | |
37 | ||
38 | #define IMXMCI_INT_MASK_DEFAULT (INT_MASK_BUF_READY | INT_MASK_DATA_TRAN | \ | |
4b7c0e4c MKB |
39 | INT_MASK_WRITE_OP_DONE | INT_MASK_END_CMD_RES | \ |
40 | INT_MASK_AUTO_CARD_DETECT | INT_MASK_DAT0_EN | INT_MASK_SDIO) | |
56ca9040 PP |
41 | |
42 | struct imxmci_host { | |
43 | struct mmc_host *mmc; | |
44 | spinlock_t lock; | |
45 | struct resource *res; | |
df25f9da | 46 | void __iomem *base; |
56ca9040 PP |
47 | int irq; |
48 | imx_dmach_t dma; | |
56ca9040 PP |
49 | volatile unsigned int imask; |
50 | unsigned int power_mode; | |
51 | unsigned int present; | |
52 | struct imxmmc_platform_data *pdata; | |
53 | ||
54 | struct mmc_request *req; | |
55 | struct mmc_command *cmd; | |
56 | struct mmc_data *data; | |
57 | ||
58 | struct timer_list timer; | |
59 | struct tasklet_struct tasklet; | |
60 | unsigned int status_reg; | |
61 | unsigned long pending_events; | |
4b7c0e4c | 62 | /* Next two fields are there for CPU driven transfers to overcome SDHC deficiencies */ |
56ca9040 PP |
63 | u16 *data_ptr; |
64 | unsigned int data_cnt; | |
65 | atomic_t stuck_timeout; | |
66 | ||
67 | unsigned int dma_nents; | |
68 | unsigned int dma_size; | |
69 | unsigned int dma_dir; | |
70 | int dma_allocated; | |
71 | ||
72 | unsigned char actual_bus_width; | |
148f93d5 PP |
73 | |
74 | int prev_cmd_code; | |
38a41fdf SH |
75 | |
76 | struct clk *clk; | |
56ca9040 PP |
77 | }; |
78 | ||
79 | #define IMXMCI_PEND_IRQ_b 0 | |
80 | #define IMXMCI_PEND_DMA_END_b 1 | |
81 | #define IMXMCI_PEND_DMA_ERR_b 2 | |
82 | #define IMXMCI_PEND_WAIT_RESP_b 3 | |
83 | #define IMXMCI_PEND_DMA_DATA_b 4 | |
84 | #define IMXMCI_PEND_CPU_DATA_b 5 | |
85 | #define IMXMCI_PEND_CARD_XCHG_b 6 | |
86 | #define IMXMCI_PEND_SET_INIT_b 7 | |
81d38428 | 87 | #define IMXMCI_PEND_STARTED_b 8 |
56ca9040 PP |
88 | |
89 | #define IMXMCI_PEND_IRQ_m (1 << IMXMCI_PEND_IRQ_b) | |
90 | #define IMXMCI_PEND_DMA_END_m (1 << IMXMCI_PEND_DMA_END_b) | |
91 | #define IMXMCI_PEND_DMA_ERR_m (1 << IMXMCI_PEND_DMA_ERR_b) | |
92 | #define IMXMCI_PEND_WAIT_RESP_m (1 << IMXMCI_PEND_WAIT_RESP_b) | |
93 | #define IMXMCI_PEND_DMA_DATA_m (1 << IMXMCI_PEND_DMA_DATA_b) | |
94 | #define IMXMCI_PEND_CPU_DATA_m (1 << IMXMCI_PEND_CPU_DATA_b) | |
95 | #define IMXMCI_PEND_CARD_XCHG_m (1 << IMXMCI_PEND_CARD_XCHG_b) | |
96 | #define IMXMCI_PEND_SET_INIT_m (1 << IMXMCI_PEND_SET_INIT_b) | |
81d38428 | 97 | #define IMXMCI_PEND_STARTED_m (1 << IMXMCI_PEND_STARTED_b) |
56ca9040 PP |
98 | |
99 | static void imxmci_stop_clock(struct imxmci_host *host) | |
100 | { | |
101 | int i = 0; | |
df25f9da MKB |
102 | u16 reg; |
103 | ||
104 | reg = readw(host->base + MMC_REG_STR_STP_CLK); | |
105 | writew(reg & ~STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK); | |
4b7c0e4c | 106 | while (i < 0x1000) { |
df25f9da MKB |
107 | if (!(i & 0x7f)) { |
108 | reg = readw(host->base + MMC_REG_STR_STP_CLK); | |
109 | writew(reg | STR_STP_CLK_STOP_CLK, | |
110 | host->base + MMC_REG_STR_STP_CLK); | |
111 | } | |
56ca9040 | 112 | |
df25f9da MKB |
113 | reg = readw(host->base + MMC_REG_STATUS); |
114 | if (!(reg & STATUS_CARD_BUS_CLK_RUN)) { | |
56ca9040 | 115 | /* Check twice before cut */ |
df25f9da MKB |
116 | reg = readw(host->base + MMC_REG_STATUS); |
117 | if (!(reg & STATUS_CARD_BUS_CLK_RUN)) | |
56ca9040 PP |
118 | return; |
119 | } | |
120 | ||
121 | i++; | |
122 | } | |
123 | dev_dbg(mmc_dev(host->mmc), "imxmci_stop_clock blocked, no luck\n"); | |
124 | } | |
125 | ||
81d38428 | 126 | static int imxmci_start_clock(struct imxmci_host *host) |
56ca9040 | 127 | { |
81d38428 PP |
128 | unsigned int trials = 0; |
129 | unsigned int delay_limit = 128; | |
130 | unsigned long flags; | |
df25f9da | 131 | u16 reg; |
81d38428 | 132 | |
df25f9da MKB |
133 | reg = readw(host->base + MMC_REG_STR_STP_CLK); |
134 | writew(reg & ~STR_STP_CLK_STOP_CLK, host->base + MMC_REG_STR_STP_CLK); | |
56ca9040 | 135 | |
81d38428 PP |
136 | clear_bit(IMXMCI_PEND_STARTED_b, &host->pending_events); |
137 | ||
138 | /* | |
139 | * Command start of the clock, this usually succeeds in less | |
140 | * then 6 delay loops, but during card detection (low clockrate) | |
141 | * it takes up to 5000 delay loops and sometimes fails for the first time | |
142 | */ | |
df25f9da MKB |
143 | reg = readw(host->base + MMC_REG_STR_STP_CLK); |
144 | writew(reg | STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK); | |
81d38428 PP |
145 | |
146 | do { | |
147 | unsigned int delay = delay_limit; | |
148 | ||
4b7c0e4c | 149 | while (delay--) { |
df25f9da MKB |
150 | reg = readw(host->base + MMC_REG_STATUS); |
151 | if (reg & STATUS_CARD_BUS_CLK_RUN) | |
81d38428 | 152 | /* Check twice before cut */ |
df25f9da MKB |
153 | reg = readw(host->base + MMC_REG_STATUS); |
154 | if (reg & STATUS_CARD_BUS_CLK_RUN) | |
81d38428 PP |
155 | return 0; |
156 | ||
4b7c0e4c | 157 | if (test_bit(IMXMCI_PEND_STARTED_b, &host->pending_events)) |
81d38428 | 158 | return 0; |
56ca9040 PP |
159 | } |
160 | ||
81d38428 PP |
161 | local_irq_save(flags); |
162 | /* | |
163 | * Ensure, that request is not doubled under all possible circumstances. | |
164 | * It is possible, that cock running state is missed, because some other | |
165 | * IRQ or schedule delays this function execution and the clocks has | |
166 | * been already stopped by other means (response processing, SDHC HW) | |
167 | */ | |
df25f9da MKB |
168 | if (!test_bit(IMXMCI_PEND_STARTED_b, &host->pending_events)) { |
169 | reg = readw(host->base + MMC_REG_STR_STP_CLK); | |
170 | writew(reg | STR_STP_CLK_START_CLK, | |
171 | host->base + MMC_REG_STR_STP_CLK); | |
172 | } | |
81d38428 PP |
173 | local_irq_restore(flags); |
174 | ||
4b7c0e4c | 175 | } while (++trials < 256); |
81d38428 PP |
176 | |
177 | dev_err(mmc_dev(host->mmc), "imxmci_start_clock blocked, no luck\n"); | |
178 | ||
179 | return -1; | |
56ca9040 PP |
180 | } |
181 | ||
df25f9da | 182 | static void imxmci_softreset(struct imxmci_host *host) |
56ca9040 | 183 | { |
df25f9da MKB |
184 | int i; |
185 | ||
56ca9040 | 186 | /* reset sequence */ |
df25f9da MKB |
187 | writew(0x08, host->base + MMC_REG_STR_STP_CLK); |
188 | writew(0x0D, host->base + MMC_REG_STR_STP_CLK); | |
189 | ||
190 | for (i = 0; i < 8; i++) | |
191 | writew(0x05, host->base + MMC_REG_STR_STP_CLK); | |
192 | ||
193 | writew(0xff, host->base + MMC_REG_RES_TO); | |
194 | writew(512, host->base + MMC_REG_BLK_LEN); | |
195 | writew(1, host->base + MMC_REG_NOB); | |
56ca9040 PP |
196 | } |
197 | ||
198 | static int imxmci_busy_wait_for_status(struct imxmci_host *host, | |
4b7c0e4c MKB |
199 | unsigned int *pstat, unsigned int stat_mask, |
200 | int timeout, const char *where) | |
56ca9040 | 201 | { |
4b7c0e4c MKB |
202 | int loops = 0; |
203 | ||
204 | while (!(*pstat & stat_mask)) { | |
205 | loops += 2; | |
206 | if (loops >= timeout) { | |
56ca9040 PP |
207 | dev_dbg(mmc_dev(host->mmc), "busy wait timeout in %s, STATUS = 0x%x (0x%x)\n", |
208 | where, *pstat, stat_mask); | |
209 | return -1; | |
210 | } | |
211 | udelay(2); | |
df25f9da | 212 | *pstat |= readw(host->base + MMC_REG_STATUS); |
56ca9040 | 213 | } |
4b7c0e4c | 214 | if (!loops) |
56ca9040 PP |
215 | return 0; |
216 | ||
2c171bf1 | 217 | /* The busy-wait is expected there for clock <8MHz due to SDHC hardware flaws */ |
4b7c0e4c | 218 | if (!(stat_mask & STATUS_END_CMD_RESP) || (host->mmc->ios.clock >= 8000000)) |
2c171bf1 | 219 | dev_info(mmc_dev(host->mmc), "busy wait for %d usec in %s, STATUS = 0x%x (0x%x)\n", |
4b7c0e4c | 220 | loops, where, *pstat, stat_mask); |
56ca9040 PP |
221 | return loops; |
222 | } | |
223 | ||
224 | static void imxmci_setup_data(struct imxmci_host *host, struct mmc_data *data) | |
225 | { | |
226 | unsigned int nob = data->blocks; | |
a3fd4a1b | 227 | unsigned int blksz = data->blksz; |
56ca9040 PP |
228 | unsigned int datasz = nob * blksz; |
229 | int i; | |
230 | ||
231 | if (data->flags & MMC_DATA_STREAM) | |
232 | nob = 0xffff; | |
233 | ||
234 | host->data = data; | |
235 | data->bytes_xfered = 0; | |
236 | ||
df25f9da MKB |
237 | writew(nob, host->base + MMC_REG_NOB); |
238 | writew(blksz, host->base + MMC_REG_BLK_LEN); | |
56ca9040 PP |
239 | |
240 | /* | |
241 | * DMA cannot be used for small block sizes, we have to use CPU driven transfers otherwise. | |
242 | * We are in big troubles for non-512 byte transfers according to note in the paragraph | |
243 | * 20.6.7 of User Manual anyway, but we need to be able to transfer SCR at least. | |
244 | * The situation is even more complex in reality. The SDHC in not able to handle wll | |
245 | * partial FIFO fills and reads. The length has to be rounded up to burst size multiple. | |
246 | * This is required for SCR read at least. | |
247 | */ | |
148f93d5 | 248 | if (datasz < 512) { |
56ca9040 PP |
249 | host->dma_size = datasz; |
250 | if (data->flags & MMC_DATA_READ) { | |
251 | host->dma_dir = DMA_FROM_DEVICE; | |
252 | ||
253 | /* Hack to enable read SCR */ | |
df25f9da MKB |
254 | writew(1, host->base + MMC_REG_NOB); |
255 | writew(512, host->base + MMC_REG_BLK_LEN); | |
56ca9040 PP |
256 | } else { |
257 | host->dma_dir = DMA_TO_DEVICE; | |
258 | } | |
259 | ||
260 | /* Convert back to virtual address */ | |
4b7c0e4c | 261 | host->data_ptr = (u16 *)sg_virt(data->sg); |
56ca9040 PP |
262 | host->data_cnt = 0; |
263 | ||
264 | clear_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events); | |
265 | set_bit(IMXMCI_PEND_CPU_DATA_b, &host->pending_events); | |
266 | ||
267 | return; | |
268 | } | |
269 | ||
270 | if (data->flags & MMC_DATA_READ) { | |
271 | host->dma_dir = DMA_FROM_DEVICE; | |
272 | host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg, | |
4b7c0e4c | 273 | data->sg_len, host->dma_dir); |
56ca9040 PP |
274 | |
275 | imx_dma_setup_sg(host->dma, data->sg, data->sg_len, datasz, | |
df25f9da MKB |
276 | host->res->start + MMC_REG_BUFFER_ACCESS, |
277 | DMA_MODE_READ); | |
56ca9040 PP |
278 | |
279 | /*imx_dma_setup_mem2dev_ccr(host->dma, DMA_MODE_READ, IMX_DMA_WIDTH_16, CCR_REN);*/ | |
280 | CCR(host->dma) = CCR_DMOD_LINEAR | CCR_DSIZ_32 | CCR_SMOD_FIFO | CCR_SSIZ_16 | CCR_REN; | |
281 | } else { | |
282 | host->dma_dir = DMA_TO_DEVICE; | |
283 | ||
284 | host->dma_nents = dma_map_sg(mmc_dev(host->mmc), data->sg, | |
4b7c0e4c | 285 | data->sg_len, host->dma_dir); |
56ca9040 PP |
286 | |
287 | imx_dma_setup_sg(host->dma, data->sg, data->sg_len, datasz, | |
df25f9da MKB |
288 | host->res->start + MMC_REG_BUFFER_ACCESS, |
289 | DMA_MODE_WRITE); | |
56ca9040 PP |
290 | |
291 | /*imx_dma_setup_mem2dev_ccr(host->dma, DMA_MODE_WRITE, IMX_DMA_WIDTH_16, CCR_REN);*/ | |
292 | CCR(host->dma) = CCR_SMOD_LINEAR | CCR_SSIZ_32 | CCR_DMOD_FIFO | CCR_DSIZ_16 | CCR_REN; | |
293 | } | |
294 | ||
295 | #if 1 /* This code is there only for consistency checking and can be disabled in future */ | |
296 | host->dma_size = 0; | |
4b7c0e4c MKB |
297 | for (i = 0; i < host->dma_nents; i++) |
298 | host->dma_size += data->sg[i].length; | |
56ca9040 PP |
299 | |
300 | if (datasz > host->dma_size) { | |
301 | dev_err(mmc_dev(host->mmc), "imxmci_setup_data datasz 0x%x > 0x%x dm_size\n", | |
4b7c0e4c | 302 | datasz, host->dma_size); |
56ca9040 PP |
303 | } |
304 | #endif | |
305 | ||
306 | host->dma_size = datasz; | |
307 | ||
308 | wmb(); | |
309 | ||
4b7c0e4c | 310 | if (host->actual_bus_width == MMC_BUS_WIDTH_4) |
56ca9040 PP |
311 | BLR(host->dma) = 0; /* burst 64 byte read / 64 bytes write */ |
312 | else | |
313 | BLR(host->dma) = 16; /* burst 16 byte read / 16 bytes write */ | |
314 | ||
315 | RSSR(host->dma) = DMA_REQ_SDHC; | |
316 | ||
317 | set_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events); | |
318 | clear_bit(IMXMCI_PEND_CPU_DATA_b, &host->pending_events); | |
319 | ||
320 | /* start DMA engine for read, write is delayed after initial response */ | |
4b7c0e4c | 321 | if (host->dma_dir == DMA_FROM_DEVICE) |
56ca9040 | 322 | imx_dma_enable(host->dma); |
56ca9040 PP |
323 | } |
324 | ||
325 | static void imxmci_start_cmd(struct imxmci_host *host, struct mmc_command *cmd, unsigned int cmdat) | |
326 | { | |
327 | unsigned long flags; | |
328 | u32 imask; | |
329 | ||
330 | WARN_ON(host->cmd != NULL); | |
331 | host->cmd = cmd; | |
332 | ||
2c171bf1 PP |
333 | /* Ensure, that clock are stopped else command programming and start fails */ |
334 | imxmci_stop_clock(host); | |
335 | ||
56ca9040 PP |
336 | if (cmd->flags & MMC_RSP_BUSY) |
337 | cmdat |= CMD_DAT_CONT_BUSY; | |
338 | ||
339 | switch (mmc_resp_type(cmd)) { | |
340 | case MMC_RSP_R1: /* short CRC, OPCODE */ | |
341 | case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */ | |
342 | cmdat |= CMD_DAT_CONT_RESPONSE_FORMAT_R1; | |
343 | break; | |
344 | case MMC_RSP_R2: /* long 136 bit + CRC */ | |
345 | cmdat |= CMD_DAT_CONT_RESPONSE_FORMAT_R2; | |
346 | break; | |
347 | case MMC_RSP_R3: /* short */ | |
348 | cmdat |= CMD_DAT_CONT_RESPONSE_FORMAT_R3; | |
349 | break; | |
56ca9040 PP |
350 | default: |
351 | break; | |
352 | } | |
353 | ||
4b7c0e4c | 354 | if (test_and_clear_bit(IMXMCI_PEND_SET_INIT_b, &host->pending_events)) |
56ca9040 PP |
355 | cmdat |= CMD_DAT_CONT_INIT; /* This command needs init */ |
356 | ||
4b7c0e4c | 357 | if (host->actual_bus_width == MMC_BUS_WIDTH_4) |
56ca9040 PP |
358 | cmdat |= CMD_DAT_CONT_BUS_WIDTH_4; |
359 | ||
df25f9da MKB |
360 | writew(cmd->opcode, host->base + MMC_REG_CMD); |
361 | writew(cmd->arg >> 16, host->base + MMC_REG_ARGH); | |
362 | writew(cmd->arg & 0xffff, host->base + MMC_REG_ARGL); | |
363 | writew(cmdat, host->base + MMC_REG_CMD_DAT_CONT); | |
56ca9040 PP |
364 | |
365 | atomic_set(&host->stuck_timeout, 0); | |
366 | set_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events); | |
367 | ||
368 | ||
369 | imask = IMXMCI_INT_MASK_DEFAULT; | |
370 | imask &= ~INT_MASK_END_CMD_RES; | |
4b7c0e4c MKB |
371 | if (cmdat & CMD_DAT_CONT_DATA_ENABLE) { |
372 | /* imask &= ~INT_MASK_BUF_READY; */ | |
56ca9040 | 373 | imask &= ~INT_MASK_DATA_TRAN; |
4b7c0e4c | 374 | if (cmdat & CMD_DAT_CONT_WRITE) |
56ca9040 | 375 | imask &= ~INT_MASK_WRITE_OP_DONE; |
4b7c0e4c | 376 | if (test_bit(IMXMCI_PEND_CPU_DATA_b, &host->pending_events)) |
56ca9040 PP |
377 | imask &= ~INT_MASK_BUF_READY; |
378 | } | |
379 | ||
380 | spin_lock_irqsave(&host->lock, flags); | |
381 | host->imask = imask; | |
df25f9da | 382 | writew(host->imask, host->base + MMC_REG_INT_MASK); |
56ca9040 PP |
383 | spin_unlock_irqrestore(&host->lock, flags); |
384 | ||
385 | dev_dbg(mmc_dev(host->mmc), "CMD%02d (0x%02x) mask set to 0x%04x\n", | |
386 | cmd->opcode, cmd->opcode, imask); | |
387 | ||
388 | imxmci_start_clock(host); | |
389 | } | |
390 | ||
391 | static void imxmci_finish_request(struct imxmci_host *host, struct mmc_request *req) | |
392 | { | |
393 | unsigned long flags; | |
394 | ||
395 | spin_lock_irqsave(&host->lock, flags); | |
396 | ||
397 | host->pending_events &= ~(IMXMCI_PEND_WAIT_RESP_m | IMXMCI_PEND_DMA_END_m | | |
4b7c0e4c | 398 | IMXMCI_PEND_DMA_DATA_m | IMXMCI_PEND_CPU_DATA_m); |
56ca9040 PP |
399 | |
400 | host->imask = IMXMCI_INT_MASK_DEFAULT; | |
df25f9da | 401 | writew(host->imask, host->base + MMC_REG_INT_MASK); |
56ca9040 PP |
402 | |
403 | spin_unlock_irqrestore(&host->lock, flags); | |
404 | ||
4b7c0e4c | 405 | if (req && req->cmd) |
148f93d5 PP |
406 | host->prev_cmd_code = req->cmd->opcode; |
407 | ||
56ca9040 PP |
408 | host->req = NULL; |
409 | host->cmd = NULL; | |
410 | host->data = NULL; | |
411 | mmc_request_done(host->mmc, req); | |
412 | } | |
413 | ||
414 | static int imxmci_finish_data(struct imxmci_host *host, unsigned int stat) | |
415 | { | |
416 | struct mmc_data *data = host->data; | |
417 | int data_error; | |
418 | ||
4b7c0e4c | 419 | if (test_and_clear_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events)) { |
56ca9040 PP |
420 | imx_dma_disable(host->dma); |
421 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_nents, | |
422 | host->dma_dir); | |
423 | } | |
424 | ||
4b7c0e4c MKB |
425 | if (stat & STATUS_ERR_MASK) { |
426 | dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n", stat); | |
427 | if (stat & (STATUS_CRC_READ_ERR | STATUS_CRC_WRITE_ERR)) | |
17b0429d | 428 | data->error = -EILSEQ; |
4b7c0e4c | 429 | else if (stat & STATUS_TIME_OUT_READ) |
17b0429d | 430 | data->error = -ETIMEDOUT; |
56ca9040 | 431 | else |
17b0429d | 432 | data->error = -EIO; |
56ca9040 PP |
433 | } else { |
434 | data->bytes_xfered = host->dma_size; | |
435 | } | |
436 | ||
437 | data_error = data->error; | |
438 | ||
439 | host->data = NULL; | |
440 | ||
441 | return data_error; | |
442 | } | |
443 | ||
444 | static int imxmci_cmd_done(struct imxmci_host *host, unsigned int stat) | |
445 | { | |
446 | struct mmc_command *cmd = host->cmd; | |
447 | int i; | |
4b7c0e4c | 448 | u32 a, b, c; |
56ca9040 PP |
449 | struct mmc_data *data = host->data; |
450 | ||
451 | if (!cmd) | |
452 | return 0; | |
453 | ||
454 | host->cmd = NULL; | |
455 | ||
456 | if (stat & STATUS_TIME_OUT_RESP) { | |
457 | dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n"); | |
17b0429d | 458 | cmd->error = -ETIMEDOUT; |
56ca9040 PP |
459 | } else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) { |
460 | dev_dbg(mmc_dev(host->mmc), "cmd crc error\n"); | |
17b0429d | 461 | cmd->error = -EILSEQ; |
56ca9040 PP |
462 | } |
463 | ||
4b7c0e4c MKB |
464 | if (cmd->flags & MMC_RSP_PRESENT) { |
465 | if (cmd->flags & MMC_RSP_136) { | |
56ca9040 | 466 | for (i = 0; i < 4; i++) { |
df25f9da MKB |
467 | a = readw(host->base + MMC_REG_RES_FIFO); |
468 | b = readw(host->base + MMC_REG_RES_FIFO); | |
469 | cmd->resp[i] = a << 16 | b; | |
56ca9040 PP |
470 | } |
471 | } else { | |
df25f9da MKB |
472 | a = readw(host->base + MMC_REG_RES_FIFO); |
473 | b = readw(host->base + MMC_REG_RES_FIFO); | |
474 | c = readw(host->base + MMC_REG_RES_FIFO); | |
4b7c0e4c | 475 | cmd->resp[0] = a << 24 | b << 8 | c >> 8; |
56ca9040 PP |
476 | } |
477 | } | |
478 | ||
479 | dev_dbg(mmc_dev(host->mmc), "RESP 0x%08x, 0x%08x, 0x%08x, 0x%08x, error %d\n", | |
480 | cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3], cmd->error); | |
481 | ||
17b0429d | 482 | if (data && !cmd->error && !(stat & STATUS_ERR_MASK)) { |
56ca9040 PP |
483 | if (host->req->data->flags & MMC_DATA_WRITE) { |
484 | ||
485 | /* Wait for FIFO to be empty before starting DMA write */ | |
486 | ||
df25f9da | 487 | stat = readw(host->base + MMC_REG_STATUS); |
4b7c0e4c MKB |
488 | if (imxmci_busy_wait_for_status(host, &stat, |
489 | STATUS_APPL_BUFF_FE, | |
490 | 40, "imxmci_cmd_done DMA WR") < 0) { | |
17b0429d | 491 | cmd->error = -EIO; |
56ca9040 | 492 | imxmci_finish_data(host, stat); |
4b7c0e4c | 493 | if (host->req) |
56ca9040 PP |
494 | imxmci_finish_request(host, host->req); |
495 | dev_warn(mmc_dev(host->mmc), "STATUS = 0x%04x\n", | |
4b7c0e4c | 496 | stat); |
56ca9040 PP |
497 | return 0; |
498 | } | |
499 | ||
4b7c0e4c | 500 | if (test_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events)) |
56ca9040 | 501 | imx_dma_enable(host->dma); |
56ca9040 PP |
502 | } |
503 | } else { | |
504 | struct mmc_request *req; | |
505 | imxmci_stop_clock(host); | |
506 | req = host->req; | |
507 | ||
4b7c0e4c | 508 | if (data) |
56ca9040 PP |
509 | imxmci_finish_data(host, stat); |
510 | ||
4b7c0e4c | 511 | if (req) |
56ca9040 | 512 | imxmci_finish_request(host, req); |
4b7c0e4c | 513 | else |
56ca9040 | 514 | dev_warn(mmc_dev(host->mmc), "imxmci_cmd_done: no request to finish\n"); |
56ca9040 PP |
515 | } |
516 | ||
517 | return 1; | |
518 | } | |
519 | ||
520 | static int imxmci_data_done(struct imxmci_host *host, unsigned int stat) | |
521 | { | |
522 | struct mmc_data *data = host->data; | |
523 | int data_error; | |
524 | ||
525 | if (!data) | |
526 | return 0; | |
527 | ||
528 | data_error = imxmci_finish_data(host, stat); | |
529 | ||
58741e8b | 530 | if (host->req->stop) { |
56ca9040 PP |
531 | imxmci_stop_clock(host); |
532 | imxmci_start_cmd(host, host->req->stop, 0); | |
533 | } else { | |
534 | struct mmc_request *req; | |
535 | req = host->req; | |
4b7c0e4c | 536 | if (req) |
56ca9040 | 537 | imxmci_finish_request(host, req); |
4b7c0e4c | 538 | else |
56ca9040 | 539 | dev_warn(mmc_dev(host->mmc), "imxmci_data_done: no request to finish\n"); |
56ca9040 PP |
540 | } |
541 | ||
542 | return 1; | |
543 | } | |
544 | ||
545 | static int imxmci_cpu_driven_data(struct imxmci_host *host, unsigned int *pstat) | |
546 | { | |
547 | int i; | |
548 | int burst_len; | |
56ca9040 PP |
549 | int trans_done = 0; |
550 | unsigned int stat = *pstat; | |
551 | ||
4b7c0e4c | 552 | if (host->actual_bus_width != MMC_BUS_WIDTH_4) |
56ca9040 PP |
553 | burst_len = 16; |
554 | else | |
555 | burst_len = 64; | |
556 | ||
557 | /* This is unfortunately required */ | |
558 | dev_dbg(mmc_dev(host->mmc), "imxmci_cpu_driven_data running STATUS = 0x%x\n", | |
559 | stat); | |
560 | ||
148f93d5 PP |
561 | udelay(20); /* required for clocks < 8MHz*/ |
562 | ||
4b7c0e4c | 563 | if (host->dma_dir == DMA_FROM_DEVICE) { |
56ca9040 | 564 | imxmci_busy_wait_for_status(host, &stat, |
4b7c0e4c MKB |
565 | STATUS_APPL_BUFF_FF | STATUS_DATA_TRANS_DONE | |
566 | STATUS_TIME_OUT_READ, | |
567 | 50, "imxmci_cpu_driven_data read"); | |
56ca9040 | 568 | |
4b7c0e4c MKB |
569 | while ((stat & (STATUS_APPL_BUFF_FF | STATUS_DATA_TRANS_DONE)) && |
570 | !(stat & STATUS_TIME_OUT_READ) && | |
571 | (host->data_cnt < 512)) { | |
148f93d5 PP |
572 | |
573 | udelay(20); /* required for clocks < 8MHz*/ | |
56ca9040 | 574 | |
4b7c0e4c | 575 | for (i = burst_len; i >= 2 ; i -= 2) { |
148f93d5 | 576 | u16 data; |
df25f9da | 577 | data = readw(host->base + MMC_REG_BUFFER_ACCESS); |
148f93d5 | 578 | udelay(10); /* required for clocks < 8MHz*/ |
4b7c0e4c | 579 | if (host->data_cnt+2 <= host->dma_size) { |
148f93d5 PP |
580 | *(host->data_ptr++) = data; |
581 | } else { | |
4b7c0e4c MKB |
582 | if (host->data_cnt < host->dma_size) |
583 | *(u8 *)(host->data_ptr) = data; | |
148f93d5 PP |
584 | } |
585 | host->data_cnt += 2; | |
56ca9040 PP |
586 | } |
587 | ||
df25f9da | 588 | stat = readw(host->base + MMC_REG_STATUS); |
56ca9040 | 589 | |
148f93d5 PP |
590 | dev_dbg(mmc_dev(host->mmc), "imxmci_cpu_driven_data read %d burst %d STATUS = 0x%x\n", |
591 | host->data_cnt, burst_len, stat); | |
56ca9040 | 592 | } |
148f93d5 | 593 | |
4b7c0e4c | 594 | if ((stat & STATUS_DATA_TRANS_DONE) && (host->data_cnt >= 512)) |
148f93d5 PP |
595 | trans_done = 1; |
596 | ||
4b7c0e4c | 597 | if (host->dma_size & 0x1ff) |
148f93d5 PP |
598 | stat &= ~STATUS_CRC_READ_ERR; |
599 | ||
4b7c0e4c | 600 | if (stat & STATUS_TIME_OUT_READ) { |
2cb3320b PP |
601 | dev_dbg(mmc_dev(host->mmc), "imxmci_cpu_driven_data read timeout STATUS = 0x%x\n", |
602 | stat); | |
603 | trans_done = -1; | |
604 | } | |
605 | ||
56ca9040 PP |
606 | } else { |
607 | imxmci_busy_wait_for_status(host, &stat, | |
4b7c0e4c MKB |
608 | STATUS_APPL_BUFF_FE, |
609 | 20, "imxmci_cpu_driven_data write"); | |
56ca9040 | 610 | |
4b7c0e4c MKB |
611 | while ((stat & STATUS_APPL_BUFF_FE) && |
612 | (host->data_cnt < host->dma_size)) { | |
613 | if (burst_len >= host->dma_size - host->data_cnt) { | |
56ca9040 PP |
614 | burst_len = host->dma_size - host->data_cnt; |
615 | host->data_cnt = host->dma_size; | |
616 | trans_done = 1; | |
617 | } else { | |
618 | host->data_cnt += burst_len; | |
619 | } | |
620 | ||
4b7c0e4c | 621 | for (i = burst_len; i > 0 ; i -= 2) |
df25f9da | 622 | writew(*(host->data_ptr++), host->base + MMC_REG_BUFFER_ACCESS); |
56ca9040 | 623 | |
df25f9da | 624 | stat = readw(host->base + MMC_REG_STATUS); |
56ca9040 PP |
625 | |
626 | dev_dbg(mmc_dev(host->mmc), "imxmci_cpu_driven_data write burst %d STATUS = 0x%x\n", | |
627 | burst_len, stat); | |
628 | } | |
629 | } | |
630 | ||
631 | *pstat = stat; | |
632 | ||
633 | return trans_done; | |
634 | } | |
635 | ||
7d12e780 | 636 | static void imxmci_dma_irq(int dma, void *devid) |
56ca9040 PP |
637 | { |
638 | struct imxmci_host *host = devid; | |
df25f9da | 639 | u32 stat = readw(host->base + MMC_REG_STATUS); |
56ca9040 PP |
640 | |
641 | atomic_set(&host->stuck_timeout, 0); | |
642 | host->status_reg = stat; | |
643 | set_bit(IMXMCI_PEND_DMA_END_b, &host->pending_events); | |
644 | tasklet_schedule(&host->tasklet); | |
645 | } | |
646 | ||
7d12e780 | 647 | static irqreturn_t imxmci_irq(int irq, void *devid) |
56ca9040 PP |
648 | { |
649 | struct imxmci_host *host = devid; | |
df25f9da | 650 | u32 stat = readw(host->base + MMC_REG_STATUS); |
56ca9040 PP |
651 | int handled = 1; |
652 | ||
df25f9da MKB |
653 | writew(host->imask | INT_MASK_SDIO | INT_MASK_AUTO_CARD_DETECT, |
654 | host->base + MMC_REG_INT_MASK); | |
56ca9040 PP |
655 | |
656 | atomic_set(&host->stuck_timeout, 0); | |
657 | host->status_reg = stat; | |
658 | set_bit(IMXMCI_PEND_IRQ_b, &host->pending_events); | |
81d38428 | 659 | set_bit(IMXMCI_PEND_STARTED_b, &host->pending_events); |
56ca9040 PP |
660 | tasklet_schedule(&host->tasklet); |
661 | ||
662 | return IRQ_RETVAL(handled);; | |
663 | } | |
664 | ||
665 | static void imxmci_tasklet_fnc(unsigned long data) | |
666 | { | |
667 | struct imxmci_host *host = (struct imxmci_host *)data; | |
668 | u32 stat; | |
669 | unsigned int data_dir_mask = 0; /* STATUS_WR_CRC_ERROR_CODE_MASK */ | |
670 | int timeout = 0; | |
671 | ||
4b7c0e4c | 672 | if (atomic_read(&host->stuck_timeout) > 4) { |
56ca9040 PP |
673 | char *what; |
674 | timeout = 1; | |
df25f9da | 675 | stat = readw(host->base + MMC_REG_STATUS); |
56ca9040 PP |
676 | host->status_reg = stat; |
677 | if (test_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events)) | |
678 | if (test_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events)) | |
679 | what = "RESP+DMA"; | |
680 | else | |
681 | what = "RESP"; | |
682 | else | |
683 | if (test_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events)) | |
4b7c0e4c | 684 | if (test_bit(IMXMCI_PEND_DMA_END_b, &host->pending_events)) |
56ca9040 PP |
685 | what = "DATA"; |
686 | else | |
687 | what = "DMA"; | |
688 | else | |
689 | what = "???"; | |
690 | ||
df25f9da MKB |
691 | dev_err(mmc_dev(host->mmc), |
692 | "%s TIMEOUT, hardware stucked STATUS = 0x%04x IMASK = 0x%04x\n", | |
693 | what, stat, | |
694 | readw(host->base + MMC_REG_INT_MASK)); | |
695 | dev_err(mmc_dev(host->mmc), | |
696 | "CMD_DAT_CONT = 0x%04x, MMC_BLK_LEN = 0x%04x, MMC_NOB = 0x%04x, DMA_CCR = 0x%08x\n", | |
697 | readw(host->base + MMC_REG_CMD_DAT_CONT), | |
698 | readw(host->base + MMC_REG_BLK_LEN), | |
699 | readw(host->base + MMC_REG_NOB), | |
700 | CCR(host->dma)); | |
148f93d5 | 701 | dev_err(mmc_dev(host->mmc), "CMD%d, prevCMD%d, bus %d-bit, dma_size = 0x%x\n", |
df25f9da MKB |
702 | host->cmd ? host->cmd->opcode : 0, |
703 | host->prev_cmd_code, | |
704 | 1 << host->actual_bus_width, host->dma_size); | |
56ca9040 PP |
705 | } |
706 | ||
4b7c0e4c | 707 | if (!host->present || timeout) |
56ca9040 | 708 | host->status_reg = STATUS_TIME_OUT_RESP | STATUS_TIME_OUT_READ | |
4b7c0e4c | 709 | STATUS_CRC_READ_ERR | STATUS_CRC_WRITE_ERR; |
56ca9040 | 710 | |
4b7c0e4c | 711 | if (test_bit(IMXMCI_PEND_IRQ_b, &host->pending_events) || timeout) { |
56ca9040 PP |
712 | clear_bit(IMXMCI_PEND_IRQ_b, &host->pending_events); |
713 | ||
df25f9da | 714 | stat = readw(host->base + MMC_REG_STATUS); |
56ca9040 PP |
715 | /* |
716 | * This is not required in theory, but there is chance to miss some flag | |
717 | * which clears automatically by mask write, FreeScale original code keeps | |
718 | * stat from IRQ time so do I | |
719 | */ | |
720 | stat |= host->status_reg; | |
721 | ||
4b7c0e4c | 722 | if (test_bit(IMXMCI_PEND_CPU_DATA_b, &host->pending_events)) |
2cb3320b PP |
723 | stat &= ~STATUS_CRC_READ_ERR; |
724 | ||
4b7c0e4c | 725 | if (test_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events)) { |
56ca9040 | 726 | imxmci_busy_wait_for_status(host, &stat, |
4b7c0e4c MKB |
727 | STATUS_END_CMD_RESP | STATUS_ERR_MASK, |
728 | 20, "imxmci_tasklet_fnc resp (ERRATUM #4)"); | |
56ca9040 PP |
729 | } |
730 | ||
4b7c0e4c MKB |
731 | if (stat & (STATUS_END_CMD_RESP | STATUS_ERR_MASK)) { |
732 | if (test_and_clear_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events)) | |
56ca9040 | 733 | imxmci_cmd_done(host, stat); |
4b7c0e4c | 734 | if (host->data && (stat & STATUS_ERR_MASK)) |
56ca9040 PP |
735 | imxmci_data_done(host, stat); |
736 | } | |
737 | ||
4b7c0e4c | 738 | if (test_bit(IMXMCI_PEND_CPU_DATA_b, &host->pending_events)) { |
df25f9da | 739 | stat |= readw(host->base + MMC_REG_STATUS); |
4b7c0e4c MKB |
740 | if (imxmci_cpu_driven_data(host, &stat)) { |
741 | if (test_and_clear_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events)) | |
56ca9040 PP |
742 | imxmci_cmd_done(host, stat); |
743 | atomic_clear_mask(IMXMCI_PEND_IRQ_m|IMXMCI_PEND_CPU_DATA_m, | |
4b7c0e4c | 744 | &host->pending_events); |
56ca9040 PP |
745 | imxmci_data_done(host, stat); |
746 | } | |
747 | } | |
748 | } | |
749 | ||
4b7c0e4c MKB |
750 | if (test_bit(IMXMCI_PEND_DMA_END_b, &host->pending_events) && |
751 | !test_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events)) { | |
56ca9040 | 752 | |
df25f9da | 753 | stat = readw(host->base + MMC_REG_STATUS); |
56ca9040 PP |
754 | /* Same as above */ |
755 | stat |= host->status_reg; | |
756 | ||
4b7c0e4c | 757 | if (host->dma_dir == DMA_TO_DEVICE) |
56ca9040 | 758 | data_dir_mask = STATUS_WRITE_OP_DONE; |
4b7c0e4c | 759 | else |
56ca9040 | 760 | data_dir_mask = STATUS_DATA_TRANS_DONE; |
56ca9040 | 761 | |
4b7c0e4c | 762 | if (stat & data_dir_mask) { |
56ca9040 PP |
763 | clear_bit(IMXMCI_PEND_DMA_END_b, &host->pending_events); |
764 | imxmci_data_done(host, stat); | |
765 | } | |
766 | } | |
767 | ||
4b7c0e4c | 768 | if (test_and_clear_bit(IMXMCI_PEND_CARD_XCHG_b, &host->pending_events)) { |
56ca9040 | 769 | |
4b7c0e4c | 770 | if (host->cmd) |
56ca9040 PP |
771 | imxmci_cmd_done(host, STATUS_TIME_OUT_RESP); |
772 | ||
4b7c0e4c | 773 | if (host->data) |
56ca9040 PP |
774 | imxmci_data_done(host, STATUS_TIME_OUT_READ | |
775 | STATUS_CRC_READ_ERR | STATUS_CRC_WRITE_ERR); | |
776 | ||
4b7c0e4c | 777 | if (host->req) |
56ca9040 PP |
778 | imxmci_finish_request(host, host->req); |
779 | ||
780 | mmc_detect_change(host->mmc, msecs_to_jiffies(100)); | |
781 | ||
782 | } | |
783 | } | |
784 | ||
785 | static void imxmci_request(struct mmc_host *mmc, struct mmc_request *req) | |
786 | { | |
787 | struct imxmci_host *host = mmc_priv(mmc); | |
788 | unsigned int cmdat; | |
789 | ||
790 | WARN_ON(host->req != NULL); | |
791 | ||
792 | host->req = req; | |
793 | ||
794 | cmdat = 0; | |
795 | ||
796 | if (req->data) { | |
797 | imxmci_setup_data(host, req->data); | |
798 | ||
799 | cmdat |= CMD_DAT_CONT_DATA_ENABLE; | |
800 | ||
801 | if (req->data->flags & MMC_DATA_WRITE) | |
802 | cmdat |= CMD_DAT_CONT_WRITE; | |
803 | ||
4b7c0e4c | 804 | if (req->data->flags & MMC_DATA_STREAM) |
56ca9040 | 805 | cmdat |= CMD_DAT_CONT_STREAM_BLOCK; |
56ca9040 PP |
806 | } |
807 | ||
808 | imxmci_start_cmd(host, req->cmd, cmdat); | |
809 | } | |
810 | ||
811 | #define CLK_RATE 19200000 | |
812 | ||
813 | static void imxmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) | |
814 | { | |
815 | struct imxmci_host *host = mmc_priv(mmc); | |
816 | int prescaler; | |
817 | ||
4b7c0e4c | 818 | if (ios->bus_width == MMC_BUS_WIDTH_4) { |
56ca9040 PP |
819 | host->actual_bus_width = MMC_BUS_WIDTH_4; |
820 | imx_gpio_mode(PB11_PF_SD_DAT3); | |
4b7c0e4c | 821 | } else { |
56ca9040 PP |
822 | host->actual_bus_width = MMC_BUS_WIDTH_1; |
823 | imx_gpio_mode(GPIO_PORTB | GPIO_IN | GPIO_PUEN | 11); | |
824 | } | |
825 | ||
4b7c0e4c | 826 | if (host->power_mode != ios->power_mode) { |
56ca9040 PP |
827 | switch (ios->power_mode) { |
828 | case MMC_POWER_OFF: | |
4b7c0e4c | 829 | break; |
56ca9040 PP |
830 | case MMC_POWER_UP: |
831 | set_bit(IMXMCI_PEND_SET_INIT_b, &host->pending_events); | |
4b7c0e4c | 832 | break; |
56ca9040 | 833 | case MMC_POWER_ON: |
4b7c0e4c | 834 | break; |
56ca9040 PP |
835 | } |
836 | host->power_mode = ios->power_mode; | |
837 | } | |
838 | ||
4b7c0e4c | 839 | if (ios->clock) { |
56ca9040 | 840 | unsigned int clk; |
df25f9da | 841 | u16 reg; |
56ca9040 PP |
842 | |
843 | /* The prescaler is 5 for PERCLK2 equal to 96MHz | |
844 | * then 96MHz / 5 = 19.2 MHz | |
845 | */ | |
38a41fdf | 846 | clk = clk_get_rate(host->clk); |
4b7c0e4c MKB |
847 | prescaler = (clk + (CLK_RATE * 7) / 8) / CLK_RATE; |
848 | switch (prescaler) { | |
56ca9040 PP |
849 | case 0: |
850 | case 1: prescaler = 0; | |
851 | break; | |
852 | case 2: prescaler = 1; | |
853 | break; | |
854 | case 3: prescaler = 2; | |
855 | break; | |
856 | case 4: prescaler = 4; | |
857 | break; | |
858 | default: | |
859 | case 5: prescaler = 5; | |
860 | break; | |
861 | } | |
862 | ||
863 | dev_dbg(mmc_dev(host->mmc), "PERCLK2 %d MHz -> prescaler %d\n", | |
864 | clk, prescaler); | |
865 | ||
4b7c0e4c | 866 | for (clk = 0; clk < 8; clk++) { |
56ca9040 | 867 | int x; |
4b7c0e4c MKB |
868 | x = CLK_RATE / (1 << clk); |
869 | if (x <= ios->clock) | |
56ca9040 PP |
870 | break; |
871 | } | |
872 | ||
df25f9da MKB |
873 | /* enable controller */ |
874 | reg = readw(host->base + MMC_REG_STR_STP_CLK); | |
875 | writew(reg | STR_STP_CLK_ENABLE, | |
876 | host->base + MMC_REG_STR_STP_CLK); | |
56ca9040 PP |
877 | |
878 | imxmci_stop_clock(host); | |
df25f9da | 879 | writew((prescaler << 3) | clk, host->base + MMC_REG_CLK_RATE); |
2c171bf1 PP |
880 | /* |
881 | * Under my understanding, clock should not be started there, because it would | |
882 | * initiate SDHC sequencer and send last or random command into card | |
883 | */ | |
4b7c0e4c | 884 | /* imxmci_start_clock(host); */ |
56ca9040 | 885 | |
df25f9da MKB |
886 | dev_dbg(mmc_dev(host->mmc), |
887 | "MMC_CLK_RATE: 0x%08x\n", | |
888 | readw(host->base + MMC_REG_CLK_RATE)); | |
56ca9040 PP |
889 | } else { |
890 | imxmci_stop_clock(host); | |
891 | } | |
892 | } | |
893 | ||
faf39ede PP |
894 | static int imxmci_get_ro(struct mmc_host *mmc) |
895 | { | |
896 | struct imxmci_host *host = mmc_priv(mmc); | |
897 | ||
898 | if (host->pdata && host->pdata->get_ro) | |
08f80bb5 AV |
899 | return !!host->pdata->get_ro(mmc_dev(mmc)); |
900 | /* | |
901 | * Board doesn't support read only detection; let the mmc core | |
902 | * decide what to do. | |
903 | */ | |
904 | return -ENOSYS; | |
faf39ede PP |
905 | } |
906 | ||
907 | ||
ab7aefd0 | 908 | static const struct mmc_host_ops imxmci_ops = { |
56ca9040 PP |
909 | .request = imxmci_request, |
910 | .set_ios = imxmci_set_ios, | |
faf39ede | 911 | .get_ro = imxmci_get_ro, |
56ca9040 PP |
912 | }; |
913 | ||
56ca9040 PP |
914 | static void imxmci_check_status(unsigned long data) |
915 | { | |
916 | struct imxmci_host *host = (struct imxmci_host *)data; | |
917 | ||
c5d5e9c4 PZ |
918 | if (host->pdata && host->pdata->card_present && |
919 | host->pdata->card_present(mmc_dev(host->mmc)) != host->present) { | |
56ca9040 PP |
920 | host->present ^= 1; |
921 | dev_info(mmc_dev(host->mmc), "card %s\n", | |
922 | host->present ? "inserted" : "removed"); | |
923 | ||
924 | set_bit(IMXMCI_PEND_CARD_XCHG_b, &host->pending_events); | |
925 | tasklet_schedule(&host->tasklet); | |
926 | } | |
927 | ||
4b7c0e4c MKB |
928 | if (test_bit(IMXMCI_PEND_WAIT_RESP_b, &host->pending_events) || |
929 | test_bit(IMXMCI_PEND_DMA_DATA_b, &host->pending_events)) { | |
56ca9040 | 930 | atomic_inc(&host->stuck_timeout); |
4b7c0e4c | 931 | if (atomic_read(&host->stuck_timeout) > 4) |
56ca9040 PP |
932 | tasklet_schedule(&host->tasklet); |
933 | } else { | |
934 | atomic_set(&host->stuck_timeout, 0); | |
935 | ||
936 | } | |
937 | ||
938 | mod_timer(&host->timer, jiffies + (HZ>>1)); | |
939 | } | |
940 | ||
941 | static int imxmci_probe(struct platform_device *pdev) | |
942 | { | |
943 | struct mmc_host *mmc; | |
944 | struct imxmci_host *host = NULL; | |
945 | struct resource *r; | |
946 | int ret = 0, irq; | |
df25f9da | 947 | u16 rev_no; |
56ca9040 PP |
948 | |
949 | printk(KERN_INFO "i.MX mmc driver\n"); | |
950 | ||
5fc63dfb PZ |
951 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
952 | irq = platform_get_irq(pdev, 0); | |
953 | if (!r || irq < 0) | |
56ca9040 PP |
954 | return -ENXIO; |
955 | ||
df25f9da MKB |
956 | r = request_mem_region(r->start, resource_size(r), pdev->name); |
957 | if (!r) | |
56ca9040 PP |
958 | return -EBUSY; |
959 | ||
960 | mmc = mmc_alloc_host(sizeof(struct imxmci_host), &pdev->dev); | |
961 | if (!mmc) { | |
962 | ret = -ENOMEM; | |
963 | goto out; | |
964 | } | |
965 | ||
966 | mmc->ops = &imxmci_ops; | |
967 | mmc->f_min = 150000; | |
968 | mmc->f_max = CLK_RATE/2; | |
969 | mmc->ocr_avail = MMC_VDD_32_33; | |
255d01af | 970 | mmc->caps = MMC_CAP_4_BIT_DATA; |
56ca9040 PP |
971 | |
972 | /* MMC core transfer sizes tunable parameters */ | |
973 | mmc->max_hw_segs = 64; | |
974 | mmc->max_phys_segs = 64; | |
56ca9040 | 975 | mmc->max_seg_size = 64*512; /* default PAGE_CACHE_SIZE */ |
55db890a | 976 | mmc->max_req_size = 64*512; /* default PAGE_CACHE_SIZE */ |
fe4a3c7a | 977 | mmc->max_blk_size = 2048; |
55db890a | 978 | mmc->max_blk_count = 65535; |
56ca9040 PP |
979 | |
980 | host = mmc_priv(mmc); | |
df25f9da MKB |
981 | host->base = ioremap(r->start, resource_size(r)); |
982 | if (!host->base) { | |
983 | ret = -ENOMEM; | |
984 | goto out; | |
985 | } | |
986 | ||
56ca9040 PP |
987 | host->mmc = mmc; |
988 | host->dma_allocated = 0; | |
989 | host->pdata = pdev->dev.platform_data; | |
c5d5e9c4 PZ |
990 | if (!host->pdata) |
991 | dev_warn(&pdev->dev, "No platform data provided!\n"); | |
56ca9040 PP |
992 | |
993 | spin_lock_init(&host->lock); | |
994 | host->res = r; | |
995 | host->irq = irq; | |
996 | ||
38a41fdf SH |
997 | host->clk = clk_get(&pdev->dev, "perclk2"); |
998 | if (IS_ERR(host->clk)) { | |
999 | ret = PTR_ERR(host->clk); | |
1000 | goto out; | |
1001 | } | |
1002 | clk_enable(host->clk); | |
1003 | ||
56ca9040 PP |
1004 | imx_gpio_mode(PB8_PF_SD_DAT0); |
1005 | imx_gpio_mode(PB9_PF_SD_DAT1); | |
1006 | imx_gpio_mode(PB10_PF_SD_DAT2); | |
1007 | /* Configured as GPIO with pull-up to ensure right MCC card mode */ | |
1008 | /* Switched to PB11_PF_SD_DAT3 if 4 bit bus is configured */ | |
1009 | imx_gpio_mode(GPIO_PORTB | GPIO_IN | GPIO_PUEN | 11); | |
1010 | /* imx_gpio_mode(PB11_PF_SD_DAT3); */ | |
1011 | imx_gpio_mode(PB12_PF_SD_CLK); | |
1012 | imx_gpio_mode(PB13_PF_SD_CMD); | |
1013 | ||
df25f9da | 1014 | imxmci_softreset(host); |
56ca9040 | 1015 | |
df25f9da MKB |
1016 | rev_no = readw(host->base + MMC_REG_REV_NO); |
1017 | if (rev_no != 0x390) { | |
56ca9040 | 1018 | dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n", |
df25f9da | 1019 | readw(host->base + MMC_REG_REV_NO)); |
56ca9040 PP |
1020 | goto out; |
1021 | } | |
1022 | ||
df25f9da MKB |
1023 | /* recommended in data sheet */ |
1024 | writew(0x2db4, host->base + MMC_REG_READ_TO); | |
56ca9040 PP |
1025 | |
1026 | host->imask = IMXMCI_INT_MASK_DEFAULT; | |
df25f9da | 1027 | writew(host->imask, host->base + MMC_REG_INT_MASK); |
56ca9040 | 1028 | |
f7def13e PZ |
1029 | host->dma = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_LOW); |
1030 | if(host->dma < 0) { | |
56ca9040 PP |
1031 | dev_err(mmc_dev(host->mmc), "imx_dma_request_by_prio failed\n"); |
1032 | ret = -EBUSY; | |
1033 | goto out; | |
1034 | } | |
4b7c0e4c | 1035 | host->dma_allocated = 1; |
56ca9040 PP |
1036 | imx_dma_setup_handlers(host->dma, imxmci_dma_irq, NULL, host); |
1037 | ||
1038 | tasklet_init(&host->tasklet, imxmci_tasklet_fnc, (unsigned long)host); | |
1039 | host->status_reg=0; | |
1040 | host->pending_events=0; | |
1041 | ||
1042 | ret = request_irq(host->irq, imxmci_irq, 0, DRIVER_NAME, host); | |
1043 | if (ret) | |
1044 | goto out; | |
1045 | ||
c5d5e9c4 PZ |
1046 | if (host->pdata && host->pdata->card_present) |
1047 | host->present = host->pdata->card_present(mmc_dev(mmc)); | |
1048 | else /* if there is no way to detect assume that card is present */ | |
1049 | host->present = 1; | |
1050 | ||
56ca9040 PP |
1051 | init_timer(&host->timer); |
1052 | host->timer.data = (unsigned long)host; | |
1053 | host->timer.function = imxmci_check_status; | |
1054 | add_timer(&host->timer); | |
4b7c0e4c | 1055 | mod_timer(&host->timer, jiffies + (HZ >> 1)); |
56ca9040 PP |
1056 | |
1057 | platform_set_drvdata(pdev, mmc); | |
1058 | ||
1059 | mmc_add_host(mmc); | |
1060 | ||
1061 | return 0; | |
1062 | ||
1063 | out: | |
1064 | if (host) { | |
4b7c0e4c | 1065 | if (host->dma_allocated) { |
56ca9040 | 1066 | imx_dma_free(host->dma); |
4b7c0e4c | 1067 | host->dma_allocated = 0; |
56ca9040 | 1068 | } |
38a41fdf SH |
1069 | if (host->clk) { |
1070 | clk_disable(host->clk); | |
1071 | clk_put(host->clk); | |
1072 | } | |
df25f9da MKB |
1073 | if (host->base) |
1074 | iounmap(host->base); | |
56ca9040 PP |
1075 | } |
1076 | if (mmc) | |
1077 | mmc_free_host(mmc); | |
df25f9da | 1078 | release_mem_region(r->start, resource_size(r)); |
56ca9040 PP |
1079 | return ret; |
1080 | } | |
1081 | ||
1082 | static int imxmci_remove(struct platform_device *pdev) | |
1083 | { | |
1084 | struct mmc_host *mmc = platform_get_drvdata(pdev); | |
1085 | ||
1086 | platform_set_drvdata(pdev, NULL); | |
1087 | ||
1088 | if (mmc) { | |
1089 | struct imxmci_host *host = mmc_priv(mmc); | |
1090 | ||
1091 | tasklet_disable(&host->tasklet); | |
1092 | ||
1093 | del_timer_sync(&host->timer); | |
1094 | mmc_remove_host(mmc); | |
1095 | ||
1096 | free_irq(host->irq, host); | |
df25f9da | 1097 | iounmap(host->base); |
4b7c0e4c | 1098 | if (host->dma_allocated) { |
56ca9040 | 1099 | imx_dma_free(host->dma); |
4b7c0e4c | 1100 | host->dma_allocated = 0; |
56ca9040 PP |
1101 | } |
1102 | ||
1103 | tasklet_kill(&host->tasklet); | |
1104 | ||
38a41fdf SH |
1105 | clk_disable(host->clk); |
1106 | clk_put(host->clk); | |
1107 | ||
df25f9da | 1108 | release_mem_region(host->res->start, resource_size(host->res)); |
56ca9040 PP |
1109 | |
1110 | mmc_free_host(mmc); | |
1111 | } | |
1112 | return 0; | |
1113 | } | |
1114 | ||
1115 | #ifdef CONFIG_PM | |
1116 | static int imxmci_suspend(struct platform_device *dev, pm_message_t state) | |
1117 | { | |
1118 | struct mmc_host *mmc = platform_get_drvdata(dev); | |
1119 | int ret = 0; | |
1120 | ||
1121 | if (mmc) | |
1122 | ret = mmc_suspend_host(mmc, state); | |
1123 | ||
1124 | return ret; | |
1125 | } | |
1126 | ||
1127 | static int imxmci_resume(struct platform_device *dev) | |
1128 | { | |
1129 | struct mmc_host *mmc = platform_get_drvdata(dev); | |
1130 | struct imxmci_host *host; | |
1131 | int ret = 0; | |
1132 | ||
1133 | if (mmc) { | |
1134 | host = mmc_priv(mmc); | |
4b7c0e4c | 1135 | if (host) |
56ca9040 PP |
1136 | set_bit(IMXMCI_PEND_SET_INIT_b, &host->pending_events); |
1137 | ret = mmc_resume_host(mmc); | |
1138 | } | |
1139 | ||
1140 | return ret; | |
1141 | } | |
1142 | #else | |
1143 | #define imxmci_suspend NULL | |
1144 | #define imxmci_resume NULL | |
1145 | #endif /* CONFIG_PM */ | |
1146 | ||
1147 | static struct platform_driver imxmci_driver = { | |
1148 | .probe = imxmci_probe, | |
1149 | .remove = imxmci_remove, | |
1150 | .suspend = imxmci_suspend, | |
1151 | .resume = imxmci_resume, | |
1152 | .driver = { | |
1153 | .name = DRIVER_NAME, | |
bc65c724 | 1154 | .owner = THIS_MODULE, |
56ca9040 PP |
1155 | } |
1156 | }; | |
1157 | ||
1158 | static int __init imxmci_init(void) | |
1159 | { | |
1160 | return platform_driver_register(&imxmci_driver); | |
1161 | } | |
1162 | ||
1163 | static void __exit imxmci_exit(void) | |
1164 | { | |
1165 | platform_driver_unregister(&imxmci_driver); | |
1166 | } | |
1167 | ||
1168 | module_init(imxmci_init); | |
1169 | module_exit(imxmci_exit); | |
1170 | ||
1171 | MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver"); | |
1172 | MODULE_AUTHOR("Sascha Hauer, Pengutronix"); | |
1173 | MODULE_LICENSE("GPL"); | |
bc65c724 | 1174 | MODULE_ALIAS("platform:imx-mmc"); |