Blame view
drivers/mmc/host/sh_mmcif.c
38 KB
fdc50a944
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* * MMCIF eMMC driver. * * Copyright (C) 2010 Renesas Solutions Corp. * Yusuke Goda <yusuke.goda.sx@renesas.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License. * * * TODO * 1. DMA * 2. Power management * 3. Handle MMC errors better * */ |
f985da17f
|
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* * The MMCIF driver is now processing MMC requests asynchronously, according * to the Linux MMC API requirement. * * The MMCIF driver processes MMC requests in up to 3 stages: command, optional * data, and optional stop. To achieve asynchronous processing each of these * stages is split into two halves: a top and a bottom half. The top half * initialises the hardware, installs a timeout handler to handle completion * timeouts, and returns. In case of the command stage this immediately returns * control to the caller, leaving all further processing to run asynchronously. * All further request processing is performed by the bottom halves. * * The bottom half further consists of a "hard" IRQ handler, an IRQ handler * thread, a DMA completion callback, if DMA is used, a timeout work, and * request- and stage-specific handler methods. * * Each bottom half run begins with either a hardware interrupt, a DMA callback * invocation, or a timeout work run. In case of an error or a successful * processing completion, the MMC core is informed and the request processing is * finished. In case processing has to continue, i.e., if data has to be read * from or written to the card, or if a stop command has to be sent, the next * top half is called, which performs the necessary hardware handling and * reschedules the timeout work. This returns the driver state machine into the * bottom half waiting state. */ |
86df17458
|
43 |
#include <linux/bitops.h> |
aa0787a90
|
44 45 |
#include <linux/clk.h> #include <linux/completion.h> |
e47bf32aa
|
46 |
#include <linux/delay.h> |
fdc50a944
|
47 |
#include <linux/dma-mapping.h> |
a782d688e
|
48 |
#include <linux/dmaengine.h> |
fdc50a944
|
49 50 |
#include <linux/mmc/card.h> #include <linux/mmc/core.h> |
e47bf32aa
|
51 |
#include <linux/mmc/host.h> |
fdc50a944
|
52 53 |
#include <linux/mmc/mmc.h> #include <linux/mmc/sdio.h> |
fdc50a944
|
54 |
#include <linux/mmc/sh_mmcif.h> |
a782d688e
|
55 |
#include <linux/pagemap.h> |
e47bf32aa
|
56 |
#include <linux/platform_device.h> |
faca6648e
|
57 |
#include <linux/pm_runtime.h> |
3b0beafc9
|
58 |
#include <linux/spinlock.h> |
88b476797
|
59 |
#include <linux/module.h> |
fdc50a944
|
60 61 62 |
#define DRIVER_NAME "sh_mmcif" #define DRIVER_VERSION "2010-04-28" |
fdc50a944
|
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
/* CE_CMD_SET */ #define CMD_MASK 0x3f000000 #define CMD_SET_RTYP_NO ((0 << 23) | (0 << 22)) #define CMD_SET_RTYP_6B ((0 << 23) | (1 << 22)) /* R1/R1b/R3/R4/R5 */ #define CMD_SET_RTYP_17B ((1 << 23) | (0 << 22)) /* R2 */ #define CMD_SET_RBSY (1 << 21) /* R1b */ #define CMD_SET_CCSEN (1 << 20) #define CMD_SET_WDAT (1 << 19) /* 1: on data, 0: no data */ #define CMD_SET_DWEN (1 << 18) /* 1: write, 0: read */ #define CMD_SET_CMLTE (1 << 17) /* 1: multi block trans, 0: single */ #define CMD_SET_CMD12EN (1 << 16) /* 1: CMD12 auto issue */ #define CMD_SET_RIDXC_INDEX ((0 << 15) | (0 << 14)) /* index check */ #define CMD_SET_RIDXC_BITS ((0 << 15) | (1 << 14)) /* check bits check */ #define CMD_SET_RIDXC_NO ((1 << 15) | (0 << 14)) /* no check */ #define CMD_SET_CRC7C ((0 << 13) | (0 << 12)) /* CRC7 check*/ #define CMD_SET_CRC7C_BITS ((0 << 13) | (1 << 12)) /* check bits check*/ #define CMD_SET_CRC7C_INTERNAL ((1 << 13) | (0 << 12)) /* internal CRC7 check*/ #define CMD_SET_CRC16C (1 << 10) /* 0: CRC16 check*/ #define CMD_SET_CRCSTE (1 << 8) /* 1: not receive CRC status */ #define CMD_SET_TBIT (1 << 7) /* 1: tran mission bit "Low" */ #define CMD_SET_OPDM (1 << 6) /* 1: open/drain */ #define CMD_SET_CCSH (1 << 5) #define CMD_SET_DATW_1 ((0 << 1) | (0 << 0)) /* 1bit */ #define CMD_SET_DATW_4 ((0 << 1) | (1 << 0)) /* 4bit */ #define CMD_SET_DATW_8 ((1 << 1) | (0 << 0)) /* 8bit */ /* CE_CMD_CTRL */ #define CMD_CTRL_BREAK (1 << 0) /* CE_BLOCK_SET */ #define BLOCK_SIZE_MASK 0x0000ffff |
fdc50a944
|
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
/* CE_INT */ #define INT_CCSDE (1 << 29) #define INT_CMD12DRE (1 << 26) #define INT_CMD12RBE (1 << 25) #define INT_CMD12CRE (1 << 24) #define INT_DTRANE (1 << 23) #define INT_BUFRE (1 << 22) #define INT_BUFWEN (1 << 21) #define INT_BUFREN (1 << 20) #define INT_CCSRCV (1 << 19) #define INT_RBSYE (1 << 17) #define INT_CRSPE (1 << 16) #define INT_CMDVIO (1 << 15) #define INT_BUFVIO (1 << 14) #define INT_WDATERR (1 << 11) #define INT_RDATERR (1 << 10) #define INT_RIDXERR (1 << 9) #define INT_RSPERR (1 << 8) #define INT_CCSTO (1 << 5) #define INT_CRCSTO (1 << 4) #define INT_WDATTO (1 << 3) #define INT_RDATTO (1 << 2) #define INT_RBSYTO (1 << 1) #define INT_RSPTO (1 << 0) #define INT_ERR_STS (INT_CMDVIO | INT_BUFVIO | INT_WDATERR | \ INT_RDATERR | INT_RIDXERR | INT_RSPERR | \ INT_CCSTO | INT_CRCSTO | INT_WDATTO | \ INT_RDATTO | INT_RBSYTO | INT_RSPTO) /* CE_INT_MASK */ #define MASK_ALL 0x00000000 #define MASK_MCCSDE (1 << 29) #define MASK_MCMD12DRE (1 << 26) #define MASK_MCMD12RBE (1 << 25) #define MASK_MCMD12CRE (1 << 24) #define MASK_MDTRANE (1 << 23) #define MASK_MBUFRE (1 << 22) #define MASK_MBUFWEN (1 << 21) #define MASK_MBUFREN (1 << 20) #define MASK_MCCSRCV (1 << 19) #define MASK_MRBSYE (1 << 17) #define MASK_MCRSPE (1 << 16) #define MASK_MCMDVIO (1 << 15) #define MASK_MBUFVIO (1 << 14) #define MASK_MWDATERR (1 << 11) #define MASK_MRDATERR (1 << 10) #define MASK_MRIDXERR (1 << 9) #define MASK_MRSPERR (1 << 8) #define MASK_MCCSTO (1 << 5) #define MASK_MCRCSTO (1 << 4) #define MASK_MWDATTO (1 << 3) #define MASK_MRDATTO (1 << 2) #define MASK_MRBSYTO (1 << 1) #define MASK_MRSPTO (1 << 0) |
ee4b88879
|
148 149 150 151 |
#define MASK_START_CMD (MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR | \ MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR | \ MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO | \ MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO) |
fdc50a944
|
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
/* CE_HOST_STS1 */ #define STS1_CMDSEQ (1 << 31) /* CE_HOST_STS2 */ #define STS2_CRCSTE (1 << 31) #define STS2_CRC16E (1 << 30) #define STS2_AC12CRCE (1 << 29) #define STS2_RSPCRC7E (1 << 28) #define STS2_CRCSTEBE (1 << 27) #define STS2_RDATEBE (1 << 26) #define STS2_AC12REBE (1 << 25) #define STS2_RSPEBE (1 << 24) #define STS2_AC12IDXE (1 << 23) #define STS2_RSPIDXE (1 << 22) #define STS2_CCSTO (1 << 15) #define STS2_RDATTO (1 << 14) #define STS2_DATBSYTO (1 << 13) #define STS2_CRCSTTO (1 << 12) #define STS2_AC12BSYTO (1 << 11) #define STS2_RSPBSYTO (1 << 10) #define STS2_AC12RSPTO (1 << 9) #define STS2_RSPTO (1 << 8) #define STS2_CRC_ERR (STS2_CRCSTE | STS2_CRC16E | \ STS2_AC12CRCE | STS2_RSPCRC7E | STS2_CRCSTEBE) #define STS2_TIMEOUT_ERR (STS2_CCSTO | STS2_RDATTO | \ STS2_DATBSYTO | STS2_CRCSTTO | \ STS2_AC12BSYTO | STS2_RSPBSYTO | \ STS2_AC12RSPTO | STS2_RSPTO) |
fdc50a944
|
180 181 182 |
#define CLKDEV_EMMC_DATA 52000000 /* 52MHz */ #define CLKDEV_MMC_DATA 20000000 /* 20MHz */ #define CLKDEV_INIT 400000 /* 400 KHz */ |
3b0beafc9
|
183 184 185 186 187 |
enum mmcif_state { STATE_IDLE, STATE_REQUEST, STATE_IOS, }; |
f985da17f
|
188 189 190 191 192 193 194 195 196 197 198 |
enum mmcif_wait_for { MMCIF_WAIT_FOR_REQUEST, MMCIF_WAIT_FOR_CMD, MMCIF_WAIT_FOR_MREAD, MMCIF_WAIT_FOR_MWRITE, MMCIF_WAIT_FOR_READ, MMCIF_WAIT_FOR_WRITE, MMCIF_WAIT_FOR_READ_END, MMCIF_WAIT_FOR_WRITE_END, MMCIF_WAIT_FOR_STOP, }; |
fdc50a944
|
199 200 |
struct sh_mmcif_host { struct mmc_host *mmc; |
f985da17f
|
201 |
struct mmc_request *mrq; |
fdc50a944
|
202 |
struct platform_device *pd; |
714c4a6e3
|
203 204 |
struct sh_dmae_slave dma_slave_tx; struct sh_dmae_slave dma_slave_rx; |
fdc50a944
|
205 206 207 |
struct clk *hclk; unsigned int clk; int bus_width; |
aa0787a90
|
208 |
bool sd_error; |
f985da17f
|
209 |
bool dying; |
fdc50a944
|
210 211 |
long timeout; void __iomem *addr; |
f985da17f
|
212 |
u32 *pio_ptr; |
ee4b88879
|
213 |
spinlock_t lock; /* protect sh_mmcif_host::state */ |
3b0beafc9
|
214 |
enum mmcif_state state; |
f985da17f
|
215 216 217 218 219 |
enum mmcif_wait_for wait_for; struct delayed_work timeout_work; size_t blocksize; int sg_idx; int sg_blkidx; |
faca6648e
|
220 |
bool power; |
c9b0cef23
|
221 |
bool card_present; |
fdc50a944
|
222 |
|
a782d688e
|
223 224 225 226 |
/* DMA support */ struct dma_chan *chan_rx; struct dma_chan *chan_tx; struct completion dma_complete; |
f38f94c6b
|
227 |
bool dma_active; |
a782d688e
|
228 |
}; |
fdc50a944
|
229 230 231 232 |
static inline void sh_mmcif_bitset(struct sh_mmcif_host *host, unsigned int reg, u32 val) { |
487d9fc50
|
233 |
writel(val | readl(host->addr + reg), host->addr + reg); |
fdc50a944
|
234 235 236 237 238 |
} static inline void sh_mmcif_bitclr(struct sh_mmcif_host *host, unsigned int reg, u32 val) { |
487d9fc50
|
239 |
writel(~val & readl(host->addr + reg), host->addr + reg); |
fdc50a944
|
240 |
} |
a782d688e
|
241 242 243 |
static void mmcif_dma_complete(void *arg) { struct sh_mmcif_host *host = arg; |
699834045
|
244 |
struct mmc_data *data = host->mrq->data; |
a782d688e
|
245 246 |
dev_dbg(&host->pd->dev, "Command completed "); |
699834045
|
247 248 |
if (WARN(!data, "%s: NULL data in DMA completion! ", |
a782d688e
|
249 250 |
dev_name(&host->pd->dev))) return; |
699834045
|
251 |
if (data->flags & MMC_DATA_READ) |
1ed828dbb
|
252 |
dma_unmap_sg(host->chan_rx->device->dev, |
699834045
|
253 |
data->sg, data->sg_len, |
a782d688e
|
254 255 |
DMA_FROM_DEVICE); else |
1ed828dbb
|
256 |
dma_unmap_sg(host->chan_tx->device->dev, |
699834045
|
257 |
data->sg, data->sg_len, |
a782d688e
|
258 259 260 261 262 263 264 |
DMA_TO_DEVICE); complete(&host->dma_complete); } static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host) { |
699834045
|
265 266 |
struct mmc_data *data = host->mrq->data; struct scatterlist *sg = data->sg; |
a782d688e
|
267 268 269 270 |
struct dma_async_tx_descriptor *desc = NULL; struct dma_chan *chan = host->chan_rx; dma_cookie_t cookie = -EINVAL; int ret; |
699834045
|
271 |
ret = dma_map_sg(chan->device->dev, sg, data->sg_len, |
1ed828dbb
|
272 |
DMA_FROM_DEVICE); |
a782d688e
|
273 |
if (ret > 0) { |
f38f94c6b
|
274 |
host->dma_active = true; |
a782d688e
|
275 276 277 278 279 280 281 |
desc = chan->device->device_prep_slave_sg(chan, sg, ret, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); } if (desc) { desc->callback = mmcif_dma_complete; desc->callback_param = host; |
a5ece7d29
|
282 283 284 |
cookie = dmaengine_submit(desc); sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN); dma_async_issue_pending(chan); |
a782d688e
|
285 286 287 |
} dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d ", |
699834045
|
288 |
__func__, data->sg_len, ret, cookie); |
a782d688e
|
289 290 291 292 293 294 |
if (!desc) { /* DMA failed, fall back to PIO */ if (ret >= 0) ret = -EIO; host->chan_rx = NULL; |
f38f94c6b
|
295 |
host->dma_active = false; |
a782d688e
|
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
dma_release_channel(chan); /* Free the Tx channel too */ chan = host->chan_tx; if (chan) { host->chan_tx = NULL; dma_release_channel(chan); } dev_warn(&host->pd->dev, "DMA failed: %d, falling back to PIO ", ret); sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN); } dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d, sg[%d] ", __func__, |
699834045
|
311 |
desc, cookie, data->sg_len); |
a782d688e
|
312 313 314 315 |
} static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host) { |
699834045
|
316 317 |
struct mmc_data *data = host->mrq->data; struct scatterlist *sg = data->sg; |
a782d688e
|
318 319 320 321 |
struct dma_async_tx_descriptor *desc = NULL; struct dma_chan *chan = host->chan_tx; dma_cookie_t cookie = -EINVAL; int ret; |
699834045
|
322 |
ret = dma_map_sg(chan->device->dev, sg, data->sg_len, |
1ed828dbb
|
323 |
DMA_TO_DEVICE); |
a782d688e
|
324 |
if (ret > 0) { |
f38f94c6b
|
325 |
host->dma_active = true; |
a782d688e
|
326 327 328 329 330 331 332 |
desc = chan->device->device_prep_slave_sg(chan, sg, ret, DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); } if (desc) { desc->callback = mmcif_dma_complete; desc->callback_param = host; |
a5ece7d29
|
333 334 335 |
cookie = dmaengine_submit(desc); sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAWEN); dma_async_issue_pending(chan); |
a782d688e
|
336 337 338 |
} dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d ", |
699834045
|
339 |
__func__, data->sg_len, ret, cookie); |
a782d688e
|
340 341 342 343 344 345 |
if (!desc) { /* DMA failed, fall back to PIO */ if (ret >= 0) ret = -EIO; host->chan_tx = NULL; |
f38f94c6b
|
346 |
host->dma_active = false; |
a782d688e
|
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
dma_release_channel(chan); /* Free the Rx channel too */ chan = host->chan_rx; if (chan) { host->chan_rx = NULL; dma_release_channel(chan); } dev_warn(&host->pd->dev, "DMA failed: %d, falling back to PIO ", ret); sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN); } dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d ", __func__, desc, cookie); } static bool sh_mmcif_filter(struct dma_chan *chan, void *arg) { dev_dbg(chan->device->dev, "%s: slave data %p ", __func__, arg); chan->private = arg; return true; } static void sh_mmcif_request_dma(struct sh_mmcif_host *host, struct sh_mmcif_plat_data *pdata) { |
714c4a6e3
|
376 |
struct sh_dmae_slave *tx, *rx; |
f38f94c6b
|
377 |
host->dma_active = false; |
a782d688e
|
378 379 380 |
/* We can only either use DMA for both Tx and Rx or not use it at all */ if (pdata->dma) { |
714c4a6e3
|
381 382 383 384 385 386 387 388 389 390 391 392 |
dev_warn(&host->pd->dev, "Update your platform to use embedded DMA slave IDs "); tx = &pdata->dma->chan_priv_tx; rx = &pdata->dma->chan_priv_rx; } else { tx = &host->dma_slave_tx; tx->slave_id = pdata->slave_id_tx; rx = &host->dma_slave_rx; rx->slave_id = pdata->slave_id_rx; } if (tx->slave_id > 0 && rx->slave_id > 0) { |
a782d688e
|
393 394 395 396 |
dma_cap_mask_t mask; dma_cap_zero(mask); dma_cap_set(DMA_SLAVE, mask); |
714c4a6e3
|
397 |
host->chan_tx = dma_request_channel(mask, sh_mmcif_filter, tx); |
a782d688e
|
398 399 400 401 402 403 |
dev_dbg(&host->pd->dev, "%s: TX: got channel %p ", __func__, host->chan_tx); if (!host->chan_tx) return; |
714c4a6e3
|
404 |
host->chan_rx = dma_request_channel(mask, sh_mmcif_filter, rx); |
a782d688e
|
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
dev_dbg(&host->pd->dev, "%s: RX: got channel %p ", __func__, host->chan_rx); if (!host->chan_rx) { dma_release_channel(host->chan_tx); host->chan_tx = NULL; return; } init_completion(&host->dma_complete); } } static void sh_mmcif_release_dma(struct sh_mmcif_host *host) { sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN); /* Descriptors are freed automatically */ if (host->chan_tx) { struct dma_chan *chan = host->chan_tx; host->chan_tx = NULL; dma_release_channel(chan); } if (host->chan_rx) { struct dma_chan *chan = host->chan_rx; host->chan_rx = NULL; dma_release_channel(chan); } |
f38f94c6b
|
433 |
host->dma_active = false; |
a782d688e
|
434 |
} |
fdc50a944
|
435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk) { struct sh_mmcif_plat_data *p = host->pd->dev.platform_data; sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE); sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR); if (!clk) return; if (p->sup_pclk && clk == host->clk) sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK); else sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR & |
86df17458
|
449 |
((fls(host->clk / clk) - 1) << 16)); |
fdc50a944
|
450 451 452 453 454 455 456 |
sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE); } static void sh_mmcif_sync_reset(struct sh_mmcif_host *host) { u32 tmp; |
487d9fc50
|
457 |
tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL); |
fdc50a944
|
458 |
|
487d9fc50
|
459 460 |
sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON); sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF); |
fdc50a944
|
461 462 463 464 465 466 467 468 469 |
sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp | SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29); /* byte swap on */ sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP); } static int sh_mmcif_error_manage(struct sh_mmcif_host *host) { u32 state1, state2; |
ee4b88879
|
470 |
int ret, timeout; |
fdc50a944
|
471 |
|
aa0787a90
|
472 |
host->sd_error = false; |
fdc50a944
|
473 |
|
487d9fc50
|
474 475 |
state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1); state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2); |
e47bf32aa
|
476 477 478 479 |
dev_dbg(&host->pd->dev, "ERR HOST_STS1 = %08x ", state1); dev_dbg(&host->pd->dev, "ERR HOST_STS2 = %08x ", state2); |
fdc50a944
|
480 481 482 483 |
if (state1 & STS1_CMDSEQ) { sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK); sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK); |
ee4b88879
|
484 |
for (timeout = 10000000; timeout; timeout--) { |
487d9fc50
|
485 |
if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1) |
ee4b88879
|
486 |
& STS1_CMDSEQ)) |
fdc50a944
|
487 488 489 |
break; mdelay(1); } |
ee4b88879
|
490 491 492 493 494 495 |
if (!timeout) { dev_err(&host->pd->dev, "Forced end of command sequence timeout err "); return -EIO; } |
fdc50a944
|
496 |
sh_mmcif_sync_reset(host); |
e47bf32aa
|
497 498 |
dev_dbg(&host->pd->dev, "Forced end of command sequence "); |
fdc50a944
|
499 500 501 502 |
return -EIO; } if (state2 & STS2_CRC_ERR) { |
ee4b88879
|
503 504 |
dev_dbg(&host->pd->dev, ": CRC error "); |
fdc50a944
|
505 506 |
ret = -EIO; } else if (state2 & STS2_TIMEOUT_ERR) { |
ee4b88879
|
507 508 |
dev_dbg(&host->pd->dev, ": Timeout "); |
fdc50a944
|
509 510 |
ret = -ETIMEDOUT; } else { |
ee4b88879
|
511 512 |
dev_dbg(&host->pd->dev, ": End/Index error "); |
fdc50a944
|
513 514 515 516 |
ret = -EIO; } return ret; } |
f985da17f
|
517 |
static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p) |
fdc50a944
|
518 |
{ |
f985da17f
|
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
struct mmc_data *data = host->mrq->data; host->sg_blkidx += host->blocksize; /* data->sg->length must be a multiple of host->blocksize? */ BUG_ON(host->sg_blkidx > data->sg->length); if (host->sg_blkidx == data->sg->length) { host->sg_blkidx = 0; if (++host->sg_idx < data->sg_len) host->pio_ptr = sg_virt(++data->sg); } else { host->pio_ptr = p; } if (host->sg_idx == data->sg_len) return false; return true; } static void sh_mmcif_single_read(struct sh_mmcif_host *host, struct mmc_request *mrq) { host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & BLOCK_SIZE_MASK) + 3; host->wait_for = MMCIF_WAIT_FOR_READ; schedule_delayed_work(&host->timeout_work, host->timeout); |
fdc50a944
|
548 |
|
fdc50a944
|
549 550 |
/* buf read enable */ sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN); |
f985da17f
|
551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
} static bool sh_mmcif_read_block(struct sh_mmcif_host *host) { struct mmc_data *data = host->mrq->data; u32 *p = sg_virt(data->sg); int i; if (host->sd_error) { data->error = sh_mmcif_error_manage(host); return false; } for (i = 0; i < host->blocksize / 4; i++) |
487d9fc50
|
565 |
*p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA); |
fdc50a944
|
566 567 568 |
/* buffer read end */ sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE); |
f985da17f
|
569 |
host->wait_for = MMCIF_WAIT_FOR_READ_END; |
fdc50a944
|
570 |
|
f985da17f
|
571 |
return true; |
fdc50a944
|
572 |
} |
f985da17f
|
573 574 |
static void sh_mmcif_multi_read(struct sh_mmcif_host *host, struct mmc_request *mrq) |
fdc50a944
|
575 576 |
{ struct mmc_data *data = mrq->data; |
f985da17f
|
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
if (!data->sg_len || !data->sg->length) return; host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & BLOCK_SIZE_MASK; host->wait_for = MMCIF_WAIT_FOR_MREAD; host->sg_idx = 0; host->sg_blkidx = 0; host->pio_ptr = sg_virt(data->sg); schedule_delayed_work(&host->timeout_work, host->timeout); sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN); } static bool sh_mmcif_mread_block(struct sh_mmcif_host *host) { struct mmc_data *data = host->mrq->data; u32 *p = host->pio_ptr; int i; if (host->sd_error) { data->error = sh_mmcif_error_manage(host); return false; |
fdc50a944
|
601 |
} |
f985da17f
|
602 603 604 605 606 607 608 609 610 611 612 613 614 |
BUG_ON(!data->sg->length); for (i = 0; i < host->blocksize / 4; i++) *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA); if (!sh_mmcif_next_block(host, p)) return false; schedule_delayed_work(&host->timeout_work, host->timeout); sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN); return true; |
fdc50a944
|
615 |
} |
f985da17f
|
616 |
static void sh_mmcif_single_write(struct sh_mmcif_host *host, |
fdc50a944
|
617 618 |
struct mmc_request *mrq) { |
f985da17f
|
619 620 |
host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & BLOCK_SIZE_MASK) + 3; |
fdc50a944
|
621 |
|
f985da17f
|
622 623 |
host->wait_for = MMCIF_WAIT_FOR_WRITE; schedule_delayed_work(&host->timeout_work, host->timeout); |
fdc50a944
|
624 625 |
/* buf write enable */ |
f985da17f
|
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 |
sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN); } static bool sh_mmcif_write_block(struct sh_mmcif_host *host) { struct mmc_data *data = host->mrq->data; u32 *p = sg_virt(data->sg); int i; if (host->sd_error) { data->error = sh_mmcif_error_manage(host); return false; } for (i = 0; i < host->blocksize / 4; i++) |
487d9fc50
|
641 |
sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++); |
fdc50a944
|
642 643 644 |
/* buffer write end */ sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE); |
f985da17f
|
645 |
host->wait_for = MMCIF_WAIT_FOR_WRITE_END; |
fdc50a944
|
646 |
|
f985da17f
|
647 |
return true; |
fdc50a944
|
648 |
} |
f985da17f
|
649 650 |
static void sh_mmcif_multi_write(struct sh_mmcif_host *host, struct mmc_request *mrq) |
fdc50a944
|
651 652 |
{ struct mmc_data *data = mrq->data; |
fdc50a944
|
653 |
|
f985da17f
|
654 655 |
if (!data->sg_len || !data->sg->length) return; |
fdc50a944
|
656 |
|
f985da17f
|
657 658 |
host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & BLOCK_SIZE_MASK; |
fdc50a944
|
659 |
|
f985da17f
|
660 661 662 663 664 665 666 |
host->wait_for = MMCIF_WAIT_FOR_MWRITE; host->sg_idx = 0; host->sg_blkidx = 0; host->pio_ptr = sg_virt(data->sg); schedule_delayed_work(&host->timeout_work, host->timeout); sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN); } |
fdc50a944
|
667 |
|
f985da17f
|
668 669 670 671 672 673 674 675 676 |
static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host) { struct mmc_data *data = host->mrq->data; u32 *p = host->pio_ptr; int i; if (host->sd_error) { data->error = sh_mmcif_error_manage(host); return false; |
fdc50a944
|
677 |
} |
f985da17f
|
678 679 680 681 682 683 684 685 686 687 688 689 690 |
BUG_ON(!data->sg->length); for (i = 0; i < host->blocksize / 4; i++) sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++); if (!sh_mmcif_next_block(host, p)) return false; schedule_delayed_work(&host->timeout_work, host->timeout); sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN); return true; |
fdc50a944
|
691 692 693 694 695 696 |
} static void sh_mmcif_get_response(struct sh_mmcif_host *host, struct mmc_command *cmd) { if (cmd->flags & MMC_RSP_136) { |
487d9fc50
|
697 698 699 700 |
cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3); cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2); cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1); cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0); |
fdc50a944
|
701 |
} else |
487d9fc50
|
702 |
cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0); |
fdc50a944
|
703 704 705 706 707 |
} static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host, struct mmc_command *cmd) { |
487d9fc50
|
708 |
cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12); |
fdc50a944
|
709 710 711 |
} static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host, |
699834045
|
712 |
struct mmc_request *mrq) |
fdc50a944
|
713 |
{ |
699834045
|
714 715 716 |
struct mmc_data *data = mrq->data; struct mmc_command *cmd = mrq->cmd; u32 opc = cmd->opcode; |
fdc50a944
|
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 |
u32 tmp = 0; /* Response Type check */ switch (mmc_resp_type(cmd)) { case MMC_RSP_NONE: tmp |= CMD_SET_RTYP_NO; break; case MMC_RSP_R1: case MMC_RSP_R1B: case MMC_RSP_R3: tmp |= CMD_SET_RTYP_6B; break; case MMC_RSP_R2: tmp |= CMD_SET_RTYP_17B; break; default: |
e47bf32aa
|
733 734 |
dev_err(&host->pd->dev, "Unsupported response type. "); |
fdc50a944
|
735 736 737 738 739 740 741 742 743 744 745 746 747 748 |
break; } switch (opc) { /* RBSY */ case MMC_SWITCH: case MMC_STOP_TRANSMISSION: case MMC_SET_WRITE_PROT: case MMC_CLR_WRITE_PROT: case MMC_ERASE: case MMC_GEN_CMD: tmp |= CMD_SET_RBSY; break; } /* WDAT / DATW */ |
699834045
|
749 |
if (data) { |
fdc50a944
|
750 751 752 753 754 755 756 757 758 759 760 761 |
tmp |= CMD_SET_WDAT; switch (host->bus_width) { case MMC_BUS_WIDTH_1: tmp |= CMD_SET_DATW_1; break; case MMC_BUS_WIDTH_4: tmp |= CMD_SET_DATW_4; break; case MMC_BUS_WIDTH_8: tmp |= CMD_SET_DATW_8; break; default: |
e47bf32aa
|
762 763 |
dev_err(&host->pd->dev, "Unsupported bus width. "); |
fdc50a944
|
764 765 766 767 768 769 770 771 772 773 |
break; } } /* DWEN */ if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) tmp |= CMD_SET_DWEN; /* CMLTE/CMD12EN */ if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) { tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN; sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET, |
699834045
|
774 |
data->blocks << 16); |
fdc50a944
|
775 776 777 778 779 780 781 782 783 784 785 786 |
} /* RIDXC[1:0] check bits */ if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID || opc == MMC_SEND_CSD || opc == MMC_SEND_CID) tmp |= CMD_SET_RIDXC_BITS; /* RCRC7C[1:0] check bits */ if (opc == MMC_SEND_OP_COND) tmp |= CMD_SET_CRC7C_BITS; /* RCRC7C[1:0] internal CRC7 */ if (opc == MMC_ALL_SEND_CID || opc == MMC_SEND_CSD || opc == MMC_SEND_CID) tmp |= CMD_SET_CRC7C_INTERNAL; |
699834045
|
787 |
return (opc << 24) | tmp; |
fdc50a944
|
788 |
} |
e47bf32aa
|
789 |
static int sh_mmcif_data_trans(struct sh_mmcif_host *host, |
f985da17f
|
790 |
struct mmc_request *mrq, u32 opc) |
fdc50a944
|
791 |
{ |
fdc50a944
|
792 793 |
switch (opc) { case MMC_READ_MULTIPLE_BLOCK: |
f985da17f
|
794 795 |
sh_mmcif_multi_read(host, mrq); return 0; |
fdc50a944
|
796 |
case MMC_WRITE_MULTIPLE_BLOCK: |
f985da17f
|
797 798 |
sh_mmcif_multi_write(host, mrq); return 0; |
fdc50a944
|
799 |
case MMC_WRITE_BLOCK: |
f985da17f
|
800 801 |
sh_mmcif_single_write(host, mrq); return 0; |
fdc50a944
|
802 803 |
case MMC_READ_SINGLE_BLOCK: case MMC_SEND_EXT_CSD: |
f985da17f
|
804 805 |
sh_mmcif_single_read(host, mrq); return 0; |
fdc50a944
|
806 |
default: |
e47bf32aa
|
807 808 |
dev_err(&host->pd->dev, "UNSUPPORTED CMD = d'%08d ", opc); |
ee4b88879
|
809 |
return -EINVAL; |
fdc50a944
|
810 |
} |
fdc50a944
|
811 812 813 |
} static void sh_mmcif_start_cmd(struct sh_mmcif_host *host, |
ee4b88879
|
814 |
struct mmc_request *mrq) |
fdc50a944
|
815 |
{ |
ee4b88879
|
816 |
struct mmc_command *cmd = mrq->cmd; |
f985da17f
|
817 818 |
u32 opc = cmd->opcode; u32 mask; |
fdc50a944
|
819 |
|
fdc50a944
|
820 |
switch (opc) { |
ee4b88879
|
821 |
/* response busy check */ |
fdc50a944
|
822 823 824 825 826 827 |
case MMC_SWITCH: case MMC_STOP_TRANSMISSION: case MMC_SET_WRITE_PROT: case MMC_CLR_WRITE_PROT: case MMC_ERASE: case MMC_GEN_CMD: |
ee4b88879
|
828 |
mask = MASK_START_CMD | MASK_MRBSYE; |
fdc50a944
|
829 830 |
break; default: |
ee4b88879
|
831 |
mask = MASK_START_CMD | MASK_MCRSPE; |
fdc50a944
|
832 833 |
break; } |
fdc50a944
|
834 |
|
699834045
|
835 |
if (mrq->data) { |
487d9fc50
|
836 837 838 |
sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0); sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, mrq->data->blksz); |
fdc50a944
|
839 |
} |
699834045
|
840 |
opc = sh_mmcif_set_cmd(host, mrq); |
fdc50a944
|
841 |
|
487d9fc50
|
842 843 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0); sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask); |
fdc50a944
|
844 |
/* set arg */ |
487d9fc50
|
845 |
sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg); |
fdc50a944
|
846 |
/* set cmd */ |
487d9fc50
|
847 |
sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc); |
fdc50a944
|
848 |
|
f985da17f
|
849 850 |
host->wait_for = MMCIF_WAIT_FOR_CMD; schedule_delayed_work(&host->timeout_work, host->timeout); |
fdc50a944
|
851 852 853 |
} static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host, |
ee4b88879
|
854 |
struct mmc_request *mrq) |
fdc50a944
|
855 |
{ |
699834045
|
856 857 |
switch (mrq->cmd->opcode) { case MMC_READ_MULTIPLE_BLOCK: |
fdc50a944
|
858 |
sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE); |
699834045
|
859 860 |
break; case MMC_WRITE_MULTIPLE_BLOCK: |
fdc50a944
|
861 |
sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE); |
699834045
|
862 863 |
break; default: |
e47bf32aa
|
864 865 |
dev_err(&host->pd->dev, "unsupported stop cmd "); |
699834045
|
866 |
mrq->stop->error = sh_mmcif_error_manage(host); |
fdc50a944
|
867 868 |
return; } |
f985da17f
|
869 870 |
host->wait_for = MMCIF_WAIT_FOR_STOP; schedule_delayed_work(&host->timeout_work, host->timeout); |
fdc50a944
|
871 872 873 874 875 |
} static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq) { struct sh_mmcif_host *host = mmc_priv(mmc); |
3b0beafc9
|
876 877 878 879 880 881 882 883 884 885 886 887 |
unsigned long flags; spin_lock_irqsave(&host->lock, flags); if (host->state != STATE_IDLE) { spin_unlock_irqrestore(&host->lock, flags); mrq->cmd->error = -EAGAIN; mmc_request_done(mmc, mrq); return; } host->state = STATE_REQUEST; spin_unlock_irqrestore(&host->lock, flags); |
fdc50a944
|
888 889 890 891 892 |
switch (mrq->cmd->opcode) { /* MMCIF does not support SD/SDIO command */ case SD_IO_SEND_OP_COND: case MMC_APP_CMD: |
3b0beafc9
|
893 |
host->state = STATE_IDLE; |
fdc50a944
|
894 895 896 897 898 899 |
mrq->cmd->error = -ETIMEDOUT; mmc_request_done(mmc, mrq); return; case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */ if (!mrq->data) { /* send_if_cond cmd (not support) */ |
3b0beafc9
|
900 |
host->state = STATE_IDLE; |
fdc50a944
|
901 902 903 904 905 906 907 908 |
mrq->cmd->error = -ETIMEDOUT; mmc_request_done(mmc, mrq); return; } break; default: break; } |
f985da17f
|
909 910 |
host->mrq = mrq; |
fdc50a944
|
911 |
|
f985da17f
|
912 |
sh_mmcif_start_cmd(host, mrq); |
fdc50a944
|
913 914 915 916 917 918 |
} static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) { struct sh_mmcif_host *host = mmc_priv(mmc); struct sh_mmcif_plat_data *p = host->pd->dev.platform_data; |
3b0beafc9
|
919 920 921 922 923 924 925 926 927 928 |
unsigned long flags; spin_lock_irqsave(&host->lock, flags); if (host->state != STATE_IDLE) { spin_unlock_irqrestore(&host->lock, flags); return; } host->state = STATE_IOS; spin_unlock_irqrestore(&host->lock, flags); |
fdc50a944
|
929 |
|
f5e0cec44
|
930 |
if (ios->power_mode == MMC_POWER_UP) { |
c9b0cef23
|
931 |
if (!host->card_present) { |
faca6648e
|
932 933 |
/* See if we also get DMA */ sh_mmcif_request_dma(host, host->pd->dev.platform_data); |
c9b0cef23
|
934 |
host->card_present = true; |
faca6648e
|
935 |
} |
f5e0cec44
|
936 |
} else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) { |
fdc50a944
|
937 938 |
/* clock stop */ sh_mmcif_clock_control(host, 0); |
faca6648e
|
939 |
if (ios->power_mode == MMC_POWER_OFF) { |
c9b0cef23
|
940 |
if (host->card_present) { |
faca6648e
|
941 |
sh_mmcif_release_dma(host); |
c9b0cef23
|
942 |
host->card_present = false; |
faca6648e
|
943 |
} |
c9b0cef23
|
944 945 946 947 |
} if (host->power) { pm_runtime_put(&host->pd->dev); host->power = false; |
f6bc41fb0
|
948 |
if (p->down_pwr && ios->power_mode == MMC_POWER_OFF) |
faca6648e
|
949 950 |
p->down_pwr(host->pd); } |
3b0beafc9
|
951 |
host->state = STATE_IDLE; |
fdc50a944
|
952 |
return; |
fdc50a944
|
953 |
} |
c9b0cef23
|
954 955 956 957 958 959 960 961 |
if (ios->clock) { if (!host->power) { if (p->set_pwr) p->set_pwr(host->pd, ios->power_mode); pm_runtime_get_sync(&host->pd->dev); host->power = true; sh_mmcif_sync_reset(host); } |
fdc50a944
|
962 |
sh_mmcif_clock_control(host, ios->clock); |
c9b0cef23
|
963 |
} |
fdc50a944
|
964 965 |
host->bus_width = ios->bus_width; |
3b0beafc9
|
966 |
host->state = STATE_IDLE; |
fdc50a944
|
967 |
} |
777271d0f
|
968 969 970 971 972 973 974 975 976 977 |
static int sh_mmcif_get_cd(struct mmc_host *mmc) { struct sh_mmcif_host *host = mmc_priv(mmc); struct sh_mmcif_plat_data *p = host->pd->dev.platform_data; if (!p->get_cd) return -ENOSYS; else return p->get_cd(host->pd); } |
fdc50a944
|
978 979 980 |
static struct mmc_host_ops sh_mmcif_ops = { .request = sh_mmcif_request, .set_ios = sh_mmcif_set_ios, |
777271d0f
|
981 |
.get_cd = sh_mmcif_get_cd, |
fdc50a944
|
982 |
}; |
f985da17f
|
983 984 985 |
static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host) { struct mmc_command *cmd = host->mrq->cmd; |
699834045
|
986 |
struct mmc_data *data = host->mrq->data; |
f985da17f
|
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 |
long time; if (host->sd_error) { switch (cmd->opcode) { case MMC_ALL_SEND_CID: case MMC_SELECT_CARD: case MMC_APP_CMD: cmd->error = -ETIMEDOUT; host->sd_error = false; break; default: cmd->error = sh_mmcif_error_manage(host); dev_dbg(&host->pd->dev, "Cmd(d'%d) error %d ", cmd->opcode, cmd->error); break; } return false; } if (!(cmd->flags & MMC_RSP_PRESENT)) { cmd->error = 0; return false; } sh_mmcif_get_response(host, cmd); |
699834045
|
1012 |
if (!data) |
f985da17f
|
1013 |
return false; |
699834045
|
1014 |
if (data->flags & MMC_DATA_READ) { |
f985da17f
|
1015 1016 1017 1018 1019 1020 1021 1022 |
if (host->chan_rx) sh_mmcif_start_dma_rx(host); } else { if (host->chan_tx) sh_mmcif_start_dma_tx(host); } if (!host->dma_active) { |
699834045
|
1023 1024 |
data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode); if (!data->error) |
f985da17f
|
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 |
return true; return false; } /* Running in the IRQ thread, can sleep */ time = wait_for_completion_interruptible_timeout(&host->dma_complete, host->timeout); if (host->sd_error) { dev_err(host->mmc->parent, "Error IRQ while waiting for DMA completion! "); /* Woken up by an error IRQ: abort DMA */ |
699834045
|
1037 |
if (data->flags & MMC_DATA_READ) |
f985da17f
|
1038 1039 1040 |
dmaengine_terminate_all(host->chan_rx); else dmaengine_terminate_all(host->chan_tx); |
699834045
|
1041 |
data->error = sh_mmcif_error_manage(host); |
f985da17f
|
1042 |
} else if (!time) { |
699834045
|
1043 |
data->error = -ETIMEDOUT; |
f985da17f
|
1044 |
} else if (time < 0) { |
699834045
|
1045 |
data->error = time; |
f985da17f
|
1046 1047 1048 1049 |
} sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN); host->dma_active = false; |
699834045
|
1050 1051 |
if (data->error) data->bytes_xfered = 0; |
f985da17f
|
1052 1053 1054 1055 1056 1057 1058 1059 |
return false; } static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id) { struct sh_mmcif_host *host = dev_id; struct mmc_request *mrq = host->mrq; |
699834045
|
1060 |
struct mmc_data *data = mrq->data; |
f985da17f
|
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 |
cancel_delayed_work_sync(&host->timeout_work); /* * All handlers return true, if processing continues, and false, if the * request has to be completed - successfully or not */ switch (host->wait_for) { case MMCIF_WAIT_FOR_REQUEST: /* We're too late, the timeout has already kicked in */ return IRQ_HANDLED; case MMCIF_WAIT_FOR_CMD: if (sh_mmcif_end_cmd(host)) /* Wait for data */ return IRQ_HANDLED; break; case MMCIF_WAIT_FOR_MREAD: if (sh_mmcif_mread_block(host)) /* Wait for more data */ return IRQ_HANDLED; break; case MMCIF_WAIT_FOR_READ: if (sh_mmcif_read_block(host)) /* Wait for data end */ return IRQ_HANDLED; break; case MMCIF_WAIT_FOR_MWRITE: if (sh_mmcif_mwrite_block(host)) /* Wait data to write */ return IRQ_HANDLED; break; case MMCIF_WAIT_FOR_WRITE: if (sh_mmcif_write_block(host)) /* Wait for data end */ return IRQ_HANDLED; break; case MMCIF_WAIT_FOR_STOP: if (host->sd_error) { mrq->stop->error = sh_mmcif_error_manage(host); break; } sh_mmcif_get_cmd12response(host, mrq->stop); mrq->stop->error = 0; break; case MMCIF_WAIT_FOR_READ_END: case MMCIF_WAIT_FOR_WRITE_END: if (host->sd_error) |
699834045
|
1108 |
data->error = sh_mmcif_error_manage(host); |
f985da17f
|
1109 1110 1111 1112 1113 1114 |
break; default: BUG(); } if (host->wait_for != MMCIF_WAIT_FOR_STOP) { |
699834045
|
1115 1116 1117 |
if (!mrq->cmd->error && data && !data->error) data->bytes_xfered = data->blocks * data->blksz; |
f985da17f
|
1118 |
|
699834045
|
1119 |
if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) { |
f985da17f
|
1120 1121 1122 1123 1124 1125 1126 1127 |
sh_mmcif_stop_cmd(host, mrq); if (!mrq->stop->error) return IRQ_HANDLED; } } host->wait_for = MMCIF_WAIT_FOR_REQUEST; host->state = STATE_IDLE; |
699834045
|
1128 |
host->mrq = NULL; |
f985da17f
|
1129 1130 1131 1132 |
mmc_request_done(host->mmc, mrq); return IRQ_HANDLED; } |
fdc50a944
|
1133 1134 1135 |
static irqreturn_t sh_mmcif_intr(int irq, void *dev_id) { struct sh_mmcif_host *host = dev_id; |
aa0787a90
|
1136 |
u32 state; |
fdc50a944
|
1137 |
int err = 0; |
487d9fc50
|
1138 |
state = sh_mmcif_readl(host->addr, MMCIF_CE_INT); |
fdc50a944
|
1139 |
|
8a8284a98
|
1140 1141 1142 1143 1144 1145 |
if (state & INT_ERR_STS) { /* error interrupts - process first */ sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state); sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state); err = 1; } else if (state & INT_RBSYE) { |
487d9fc50
|
1146 1147 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~(INT_RBSYE | INT_CRSPE)); |
fdc50a944
|
1148 1149 |
sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MRBSYE); } else if (state & INT_CRSPE) { |
487d9fc50
|
1150 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_CRSPE); |
fdc50a944
|
1151 1152 |
sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCRSPE); } else if (state & INT_BUFREN) { |
487d9fc50
|
1153 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFREN); |
fdc50a944
|
1154 1155 |
sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFREN); } else if (state & INT_BUFWEN) { |
487d9fc50
|
1156 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFWEN); |
fdc50a944
|
1157 1158 |
sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN); } else if (state & INT_CMD12DRE) { |
487d9fc50
|
1159 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT, |
fdc50a944
|
1160 1161 1162 1163 |
~(INT_CMD12DRE | INT_CMD12RBE | INT_CMD12CRE | INT_BUFRE)); sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE); } else if (state & INT_BUFRE) { |
487d9fc50
|
1164 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFRE); |
fdc50a944
|
1165 1166 |
sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFRE); } else if (state & INT_DTRANE) { |
487d9fc50
|
1167 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_DTRANE); |
fdc50a944
|
1168 1169 |
sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MDTRANE); } else if (state & INT_CMD12RBE) { |
487d9fc50
|
1170 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT, |
fdc50a944
|
1171 1172 |
~(INT_CMD12RBE | INT_CMD12CRE)); sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE); |
fdc50a944
|
1173 |
} else { |
faca6648e
|
1174 1175 |
dev_dbg(&host->pd->dev, "Unsupported interrupt: 0x%x ", state); |
487d9fc50
|
1176 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state); |
fdc50a944
|
1177 1178 1179 1180 |
sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state); err = 1; } if (err) { |
aa0787a90
|
1181 |
host->sd_error = true; |
e47bf32aa
|
1182 1183 |
dev_dbg(&host->pd->dev, "int err state = %08x ", state); |
fdc50a944
|
1184 |
} |
f985da17f
|
1185 1186 1187 1188 1189 1190 |
if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) { if (!host->dma_active) return IRQ_WAKE_THREAD; else if (host->sd_error) mmcif_dma_complete(host); } else { |
aa0787a90
|
1191 1192 |
dev_dbg(&host->pd->dev, "Unexpected IRQ 0x%x ", state); |
f985da17f
|
1193 |
} |
fdc50a944
|
1194 1195 1196 |
return IRQ_HANDLED; } |
f985da17f
|
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 |
static void mmcif_timeout_work(struct work_struct *work) { struct delayed_work *d = container_of(work, struct delayed_work, work); struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work); struct mmc_request *mrq = host->mrq; if (host->dying) /* Don't run after mmc_remove_host() */ return; /* * Handle races with cancel_delayed_work(), unless * cancel_delayed_work_sync() is used */ switch (host->wait_for) { case MMCIF_WAIT_FOR_CMD: mrq->cmd->error = sh_mmcif_error_manage(host); break; case MMCIF_WAIT_FOR_STOP: mrq->stop->error = sh_mmcif_error_manage(host); break; case MMCIF_WAIT_FOR_MREAD: case MMCIF_WAIT_FOR_MWRITE: case MMCIF_WAIT_FOR_READ: case MMCIF_WAIT_FOR_WRITE: case MMCIF_WAIT_FOR_READ_END: case MMCIF_WAIT_FOR_WRITE_END: |
699834045
|
1224 |
mrq->data->error = sh_mmcif_error_manage(host); |
f985da17f
|
1225 1226 1227 1228 1229 1230 1231 |
break; default: BUG(); } host->state = STATE_IDLE; host->wait_for = MMCIF_WAIT_FOR_REQUEST; |
f985da17f
|
1232 1233 1234 |
host->mrq = NULL; mmc_request_done(host->mmc, mrq); } |
fdc50a944
|
1235 1236 1237 1238 |
static int __devinit sh_mmcif_probe(struct platform_device *pdev) { int ret = 0, irq[2]; struct mmc_host *mmc; |
e47bf32aa
|
1239 1240 |
struct sh_mmcif_host *host; struct sh_mmcif_plat_data *pd; |
fdc50a944
|
1241 1242 1243 1244 1245 1246 1247 |
struct resource *res; void __iomem *reg; char clk_name[8]; irq[0] = platform_get_irq(pdev, 0); irq[1] = platform_get_irq(pdev, 1); if (irq[0] < 0 || irq[1] < 0) { |
e47bf32aa
|
1248 1249 |
dev_err(&pdev->dev, "Get irq error "); |
fdc50a944
|
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 |
return -ENXIO; } res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!res) { dev_err(&pdev->dev, "platform_get_resource error. "); return -ENXIO; } reg = ioremap(res->start, resource_size(res)); if (!reg) { dev_err(&pdev->dev, "ioremap error. "); return -ENOMEM; } |
e47bf32aa
|
1264 |
pd = pdev->dev.platform_data; |
fdc50a944
|
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 |
if (!pd) { dev_err(&pdev->dev, "sh_mmcif plat data error. "); ret = -ENXIO; goto clean_up; } mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev); if (!mmc) { ret = -ENOMEM; goto clean_up; } host = mmc_priv(mmc); host->mmc = mmc; host->addr = reg; host->timeout = 1000; snprintf(clk_name, sizeof(clk_name), "mmc%d", pdev->id); host->hclk = clk_get(&pdev->dev, clk_name); if (IS_ERR(host->hclk)) { dev_err(&pdev->dev, "cannot get clock \"%s\" ", clk_name); ret = PTR_ERR(host->hclk); goto clean_up1; } clk_enable(host->hclk); host->clk = clk_get_rate(host->hclk); host->pd = pdev; |
3b0beafc9
|
1292 |
spin_lock_init(&host->lock); |
fdc50a944
|
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 |
mmc->ops = &sh_mmcif_ops; mmc->f_max = host->clk; /* close to 400KHz */ if (mmc->f_max < 51200000) mmc->f_min = mmc->f_max / 128; else if (mmc->f_max < 102400000) mmc->f_min = mmc->f_max / 256; else mmc->f_min = mmc->f_max / 512; if (pd->ocr) mmc->ocr_avail = pd->ocr; mmc->caps = MMC_CAP_MMC_HIGHSPEED; if (pd->caps) mmc->caps |= pd->caps; |
a782d688e
|
1308 |
mmc->max_segs = 32; |
fdc50a944
|
1309 |
mmc->max_blk_size = 512; |
a782d688e
|
1310 1311 |
mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs; mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size; |
fdc50a944
|
1312 1313 1314 1315 |
mmc->max_seg_size = mmc->max_req_size; sh_mmcif_sync_reset(host); platform_set_drvdata(pdev, host); |
a782d688e
|
1316 |
|
faca6648e
|
1317 1318 1319 1320 1321 1322 |
pm_runtime_enable(&pdev->dev); host->power = false; ret = pm_runtime_resume(&pdev->dev); if (ret < 0) goto clean_up2; |
a782d688e
|
1323 |
|
fdc50a944
|
1324 |
mmc_add_host(mmc); |
3b0beafc9
|
1325 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL); |
f985da17f
|
1326 |
ret = request_threaded_irq(irq[0], sh_mmcif_intr, sh_mmcif_irqt, 0, "sh_mmc:error", host); |
fdc50a944
|
1327 |
if (ret) { |
e47bf32aa
|
1328 1329 |
dev_err(&pdev->dev, "request_irq error (sh_mmc:error) "); |
faca6648e
|
1330 |
goto clean_up3; |
fdc50a944
|
1331 |
} |
f985da17f
|
1332 |
ret = request_threaded_irq(irq[1], sh_mmcif_intr, sh_mmcif_irqt, 0, "sh_mmc:int", host); |
fdc50a944
|
1333 1334 |
if (ret) { free_irq(irq[0], host); |
e47bf32aa
|
1335 1336 |
dev_err(&pdev->dev, "request_irq error (sh_mmc:int) "); |
faca6648e
|
1337 |
goto clean_up3; |
fdc50a944
|
1338 |
} |
f985da17f
|
1339 |
INIT_DELAYED_WORK(&host->timeout_work, mmcif_timeout_work); |
ee4b88879
|
1340 |
mmc_detect_change(host->mmc, 0); |
fdc50a944
|
1341 |
|
e47bf32aa
|
1342 1343 1344 1345 |
dev_info(&pdev->dev, "driver version %s ", DRIVER_VERSION); dev_dbg(&pdev->dev, "chip ver H'%04x ", |
487d9fc50
|
1346 |
sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff); |
fdc50a944
|
1347 |
return ret; |
faca6648e
|
1348 1349 1350 |
clean_up3: mmc_remove_host(mmc); pm_runtime_suspend(&pdev->dev); |
fdc50a944
|
1351 |
clean_up2: |
faca6648e
|
1352 |
pm_runtime_disable(&pdev->dev); |
fdc50a944
|
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 |
clk_disable(host->hclk); clean_up1: mmc_free_host(mmc); clean_up: if (reg) iounmap(reg); return ret; } static int __devexit sh_mmcif_remove(struct platform_device *pdev) { struct sh_mmcif_host *host = platform_get_drvdata(pdev); int irq[2]; |
f985da17f
|
1366 |
host->dying = true; |
faca6648e
|
1367 |
pm_runtime_get_sync(&pdev->dev); |
fdc50a944
|
1368 |
|
faca6648e
|
1369 |
mmc_remove_host(host->mmc); |
3b0beafc9
|
1370 |
sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL); |
f985da17f
|
1371 1372 1373 1374 1375 1376 |
/* * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the * mmc_remove_host() call above. But swapping order doesn't help either * (a query on the linux-mmc mailing list didn't bring any replies). */ cancel_delayed_work_sync(&host->timeout_work); |
fdc50a944
|
1377 1378 |
if (host->addr) iounmap(host->addr); |
aa0787a90
|
1379 1380 |
irq[0] = platform_get_irq(pdev, 0); irq[1] = platform_get_irq(pdev, 1); |
fdc50a944
|
1381 1382 1383 |
free_irq(irq[0], host); free_irq(irq[1], host); |
aa0787a90
|
1384 |
platform_set_drvdata(pdev, NULL); |
fdc50a944
|
1385 1386 |
clk_disable(host->hclk); mmc_free_host(host->mmc); |
faca6648e
|
1387 1388 |
pm_runtime_put_sync(&pdev->dev); pm_runtime_disable(&pdev->dev); |
fdc50a944
|
1389 1390 1391 |
return 0; } |
faca6648e
|
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 |
#ifdef CONFIG_PM static int sh_mmcif_suspend(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); struct sh_mmcif_host *host = platform_get_drvdata(pdev); int ret = mmc_suspend_host(host->mmc); if (!ret) { sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL); clk_disable(host->hclk); } return ret; } static int sh_mmcif_resume(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); struct sh_mmcif_host *host = platform_get_drvdata(pdev); clk_enable(host->hclk); return mmc_resume_host(host->mmc); } #else #define sh_mmcif_suspend NULL #define sh_mmcif_resume NULL #endif /* CONFIG_PM */ static const struct dev_pm_ops sh_mmcif_dev_pm_ops = { .suspend = sh_mmcif_suspend, .resume = sh_mmcif_resume, }; |
fdc50a944
|
1425 1426 1427 1428 1429 |
static struct platform_driver sh_mmcif_driver = { .probe = sh_mmcif_probe, .remove = sh_mmcif_remove, .driver = { .name = DRIVER_NAME, |
faca6648e
|
1430 |
.pm = &sh_mmcif_dev_pm_ops, |
fdc50a944
|
1431 1432 |
}, }; |
d1f81a64a
|
1433 |
module_platform_driver(sh_mmcif_driver); |
fdc50a944
|
1434 1435 1436 |
MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver"); MODULE_LICENSE("GPL"); |
aa0787a90
|
1437 |
MODULE_ALIAS("platform:" DRIVER_NAME); |
fdc50a944
|
1438 |
MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>"); |