4 * Copyright (C) 2011 Renesas Solutions Corp.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License.
17 #include <asm/errno.h>
21 #define DRIVER_NAME "sh_mmcif"
23 static void *mmc_priv(struct mmc *mmc)
25 return (void *)mmc->priv;
28 static int sh_mmcif_intr(void *dev_id)
30 struct sh_mmcif_host *host = dev_id;
33 state = sh_mmcif_read(&host->regs->ce_int);
34 state &= sh_mmcif_read(&host->regs->ce_int_mask);
36 if (state & INT_RBSYE) {
37 sh_mmcif_write(~(INT_RBSYE | INT_CRSPE), &host->regs->ce_int);
38 sh_mmcif_bitclr(MASK_MRBSYE, &host->regs->ce_int_mask);
40 } else if (state & INT_CRSPE) {
41 sh_mmcif_write(~INT_CRSPE, &host->regs->ce_int);
42 sh_mmcif_bitclr(MASK_MCRSPE, &host->regs->ce_int_mask);
43 /* one more interrupt (INT_RBSYE) */
44 if (sh_mmcif_read(&host->regs->ce_cmd_set) & CMD_SET_RBSY)
47 } else if (state & INT_BUFREN) {
48 sh_mmcif_write(~INT_BUFREN, &host->regs->ce_int);
49 sh_mmcif_bitclr(MASK_MBUFREN, &host->regs->ce_int_mask);
51 } else if (state & INT_BUFWEN) {
52 sh_mmcif_write(~INT_BUFWEN, &host->regs->ce_int);
53 sh_mmcif_bitclr(MASK_MBUFWEN, &host->regs->ce_int_mask);
55 } else if (state & INT_CMD12DRE) {
56 sh_mmcif_write(~(INT_CMD12DRE | INT_CMD12RBE | INT_CMD12CRE |
57 INT_BUFRE), &host->regs->ce_int);
58 sh_mmcif_bitclr(MASK_MCMD12DRE, &host->regs->ce_int_mask);
60 } else if (state & INT_BUFRE) {
61 sh_mmcif_write(~INT_BUFRE, &host->regs->ce_int);
62 sh_mmcif_bitclr(MASK_MBUFRE, &host->regs->ce_int_mask);
64 } else if (state & INT_DTRANE) {
65 sh_mmcif_write(~INT_DTRANE, &host->regs->ce_int);
66 sh_mmcif_bitclr(MASK_MDTRANE, &host->regs->ce_int_mask);
68 } else if (state & INT_CMD12RBE) {
69 sh_mmcif_write(~(INT_CMD12RBE | INT_CMD12CRE),
71 sh_mmcif_bitclr(MASK_MCMD12RBE, &host->regs->ce_int_mask);
73 } else if (state & INT_ERR_STS) {
75 sh_mmcif_write(~state, &host->regs->ce_int);
76 sh_mmcif_bitclr(state, &host->regs->ce_int_mask);
83 debug("%s: int err state = %08x\n", DRIVER_NAME, state);
89 static int mmcif_wait_interrupt_flag(struct sh_mmcif_host *host)
91 int timeout = 10000000;
100 if (!sh_mmcif_intr(host))
103 udelay(1); /* 1 usec */
106 return 1; /* Return value: NOT 0 = complete waiting */
109 static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
113 sh_mmcif_bitclr(CLK_ENABLE, &host->regs->ce_clk_ctrl);
114 sh_mmcif_bitclr(CLK_CLEAR, &host->regs->ce_clk_ctrl);
118 if (clk == CLKDEV_EMMC_DATA) {
119 sh_mmcif_bitset(CLK_PCLK, &host->regs->ce_clk_ctrl);
121 for (i = 1; (unsigned int)host->clk / (1 << i) >= clk; i++)
123 sh_mmcif_bitset((i - 1) << 16, &host->regs->ce_clk_ctrl);
125 sh_mmcif_bitset(CLK_ENABLE, &host->regs->ce_clk_ctrl);
128 static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
132 tmp = sh_mmcif_read(&host->regs->ce_clk_ctrl) & (CLK_ENABLE |
135 sh_mmcif_write(SOFT_RST_ON, &host->regs->ce_version);
136 sh_mmcif_write(SOFT_RST_OFF, &host->regs->ce_version);
137 sh_mmcif_bitset(tmp | SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29,
138 &host->regs->ce_clk_ctrl);
140 sh_mmcif_bitset(BUF_ACC_ATYP, &host->regs->ce_buf_acc);
143 static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
146 int ret, timeout = 10000000;
151 state1 = sh_mmcif_read(&host->regs->ce_host_sts1);
152 state2 = sh_mmcif_read(&host->regs->ce_host_sts2);
153 debug("%s: ERR HOST_STS1 = %08x\n", \
154 DRIVER_NAME, sh_mmcif_read(&host->regs->ce_host_sts1));
155 debug("%s: ERR HOST_STS2 = %08x\n", \
156 DRIVER_NAME, sh_mmcif_read(&host->regs->ce_host_sts2));
158 if (state1 & STS1_CMDSEQ) {
159 debug("%s: Forced end of command sequence\n", DRIVER_NAME);
160 sh_mmcif_bitset(CMD_CTRL_BREAK, &host->regs->ce_cmd_ctrl);
161 sh_mmcif_bitset(~CMD_CTRL_BREAK, &host->regs->ce_cmd_ctrl);
165 printf(DRIVER_NAME": Forceed end of " \
166 "command sequence timeout err\n");
169 if (!(sh_mmcif_read(&host->regs->ce_host_sts1)
173 sh_mmcif_sync_reset(host);
177 if (state2 & STS2_CRC_ERR)
179 else if (state2 & STS2_TIMEOUT_ERR)
186 static int sh_mmcif_single_read(struct sh_mmcif_host *host,
187 struct mmc_data *data)
191 unsigned long *p = (unsigned long *)data->dest;
193 if ((unsigned long)p & 0x00000001) {
194 printf("%s: The data pointer is unaligned.", __func__);
200 /* buf read enable */
201 sh_mmcif_bitset(MASK_MBUFREN, &host->regs->ce_int_mask);
202 time = mmcif_wait_interrupt_flag(host);
203 if (time == 0 || host->sd_error != 0)
204 return sh_mmcif_error_manage(host);
207 blocksize = (BLOCK_SIZE_MASK &
208 sh_mmcif_read(&host->regs->ce_block_set)) + 3;
209 for (i = 0; i < blocksize / 4; i++)
210 *p++ = sh_mmcif_read(&host->regs->ce_data);
212 /* buffer read end */
213 sh_mmcif_bitset(MASK_MBUFRE, &host->regs->ce_int_mask);
214 time = mmcif_wait_interrupt_flag(host);
215 if (time == 0 || host->sd_error != 0)
216 return sh_mmcif_error_manage(host);
222 static int sh_mmcif_multi_read(struct sh_mmcif_host *host,
223 struct mmc_data *data)
227 unsigned long *p = (unsigned long *)data->dest;
229 if ((unsigned long)p & 0x00000001) {
230 printf("%s: The data pointer is unaligned.", __func__);
235 blocksize = BLOCK_SIZE_MASK & sh_mmcif_read(&host->regs->ce_block_set);
236 for (j = 0; j < data->blocks; j++) {
237 sh_mmcif_bitset(MASK_MBUFREN, &host->regs->ce_int_mask);
238 time = mmcif_wait_interrupt_flag(host);
239 if (time == 0 || host->sd_error != 0)
240 return sh_mmcif_error_manage(host);
243 for (i = 0; i < blocksize / 4; i++)
244 *p++ = sh_mmcif_read(&host->regs->ce_data);
251 static int sh_mmcif_single_write(struct sh_mmcif_host *host,
252 struct mmc_data *data)
256 const unsigned long *p = (unsigned long *)data->dest;
258 if ((unsigned long)p & 0x00000001) {
259 printf("%s: The data pointer is unaligned.", __func__);
264 sh_mmcif_bitset(MASK_MBUFWEN, &host->regs->ce_int_mask);
266 time = mmcif_wait_interrupt_flag(host);
267 if (time == 0 || host->sd_error != 0)
268 return sh_mmcif_error_manage(host);
271 blocksize = (BLOCK_SIZE_MASK &
272 sh_mmcif_read(&host->regs->ce_block_set)) + 3;
273 for (i = 0; i < blocksize / 4; i++)
274 sh_mmcif_write(*p++, &host->regs->ce_data);
276 /* buffer write end */
277 sh_mmcif_bitset(MASK_MDTRANE, &host->regs->ce_int_mask);
279 time = mmcif_wait_interrupt_flag(host);
280 if (time == 0 || host->sd_error != 0)
281 return sh_mmcif_error_manage(host);
287 static int sh_mmcif_multi_write(struct sh_mmcif_host *host,
288 struct mmc_data *data)
292 const unsigned long *p = (unsigned long *)data->dest;
294 if ((unsigned long)p & 0x00000001) {
295 printf("%s: The data pointer is unaligned.", __func__);
300 blocksize = BLOCK_SIZE_MASK & sh_mmcif_read(&host->regs->ce_block_set);
301 for (j = 0; j < data->blocks; j++) {
302 sh_mmcif_bitset(MASK_MBUFWEN, &host->regs->ce_int_mask);
304 time = mmcif_wait_interrupt_flag(host);
306 if (time == 0 || host->sd_error != 0)
307 return sh_mmcif_error_manage(host);
310 for (i = 0; i < blocksize / 4; i++)
311 sh_mmcif_write(*p++, &host->regs->ce_data);
318 static void sh_mmcif_get_response(struct sh_mmcif_host *host,
321 if (cmd->resp_type & MMC_RSP_136) {
322 cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp3);
323 cmd->response[1] = sh_mmcif_read(&host->regs->ce_resp2);
324 cmd->response[2] = sh_mmcif_read(&host->regs->ce_resp1);
325 cmd->response[3] = sh_mmcif_read(&host->regs->ce_resp0);
326 debug(" RESP %08x, %08x, %08x, %08x\n", cmd->response[0],
327 cmd->response[1], cmd->response[2], cmd->response[3]);
329 cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp0);
333 static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
336 cmd->response[0] = sh_mmcif_read(&host->regs->ce_resp_cmd12);
339 static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
340 struct mmc_data *data, struct mmc_cmd *cmd)
343 u32 opc = cmd->cmdidx;
345 /* Response Type check */
346 switch (cmd->resp_type) {
348 tmp |= CMD_SET_RTYP_NO;
353 tmp |= CMD_SET_RTYP_6B;
356 tmp |= CMD_SET_RTYP_17B;
359 printf(DRIVER_NAME": Not support type response.\n");
364 if (opc == MMC_CMD_SWITCH)
370 switch (host->bus_width) {
371 case MMC_BUS_WIDTH_1:
372 tmp |= CMD_SET_DATW_1;
374 case MMC_BUS_WIDTH_4:
375 tmp |= CMD_SET_DATW_4;
377 case MMC_BUS_WIDTH_8:
378 tmp |= CMD_SET_DATW_8;
381 printf(DRIVER_NAME": Not support bus width.\n");
386 if (opc == MMC_CMD_WRITE_SINGLE_BLOCK ||
387 opc == MMC_CMD_WRITE_MULTIPLE_BLOCK)
390 if (opc == MMC_CMD_READ_MULTIPLE_BLOCK ||
391 opc == MMC_CMD_WRITE_MULTIPLE_BLOCK) {
392 tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
393 sh_mmcif_bitset(data->blocks << 16, &host->regs->ce_block_set);
395 /* RIDXC[1:0] check bits */
396 if (opc == MMC_CMD_SEND_OP_COND || opc == MMC_CMD_ALL_SEND_CID ||
397 opc == MMC_CMD_SEND_CSD || opc == MMC_CMD_SEND_CID)
398 tmp |= CMD_SET_RIDXC_BITS;
399 /* RCRC7C[1:0] check bits */
400 if (opc == MMC_CMD_SEND_OP_COND)
401 tmp |= CMD_SET_CRC7C_BITS;
402 /* RCRC7C[1:0] internal CRC7 */
403 if (opc == MMC_CMD_ALL_SEND_CID ||
404 opc == MMC_CMD_SEND_CSD || opc == MMC_CMD_SEND_CID)
405 tmp |= CMD_SET_CRC7C_INTERNAL;
407 return opc = ((opc << 24) | tmp);
410 static u32 sh_mmcif_data_trans(struct sh_mmcif_host *host,
411 struct mmc_data *data, u16 opc)
416 case MMC_CMD_READ_MULTIPLE_BLOCK:
417 ret = sh_mmcif_multi_read(host, data);
419 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
420 ret = sh_mmcif_multi_write(host, data);
422 case MMC_CMD_WRITE_SINGLE_BLOCK:
423 ret = sh_mmcif_single_write(host, data);
425 case MMC_CMD_READ_SINGLE_BLOCK:
426 case MMC_CMD_SEND_EXT_CSD:
427 ret = sh_mmcif_single_read(host, data);
430 printf(DRIVER_NAME": NOT SUPPORT CMD = d'%08d\n", opc);
437 static int sh_mmcif_start_cmd(struct sh_mmcif_host *host,
438 struct mmc_data *data, struct mmc_cmd *cmd)
441 int ret = 0, mask = 0;
442 u32 opc = cmd->cmdidx;
444 if (opc == MMC_CMD_STOP_TRANSMISSION) {
445 /* MMCIF sends the STOP command automatically */
446 if (host->last_cmd == MMC_CMD_READ_MULTIPLE_BLOCK)
447 sh_mmcif_bitset(MASK_MCMD12DRE,
448 &host->regs->ce_int_mask);
450 sh_mmcif_bitset(MASK_MCMD12RBE,
451 &host->regs->ce_int_mask);
453 time = mmcif_wait_interrupt_flag(host);
454 if (time == 0 || host->sd_error != 0)
455 return sh_mmcif_error_manage(host);
457 sh_mmcif_get_cmd12response(host, cmd);
460 if (opc == MMC_CMD_SWITCH)
465 mask |= MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR |
466 MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR |
467 MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO |
468 MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO;
471 sh_mmcif_write(0, &host->regs->ce_block_set);
472 sh_mmcif_write(data->blocksize, &host->regs->ce_block_set);
474 opc = sh_mmcif_set_cmd(host, data, cmd);
476 sh_mmcif_write(INT_START_MAGIC, &host->regs->ce_int);
477 sh_mmcif_write(mask, &host->regs->ce_int_mask);
479 debug("CMD%d ARG:%08x\n", cmd->cmdidx, cmd->cmdarg);
481 sh_mmcif_write(cmd->cmdarg, &host->regs->ce_arg);
484 sh_mmcif_write(opc, &host->regs->ce_cmd_set);
486 time = mmcif_wait_interrupt_flag(host);
488 return sh_mmcif_error_manage(host);
490 if (host->sd_error) {
491 switch (cmd->cmdidx) {
492 case MMC_CMD_ALL_SEND_CID:
493 case MMC_CMD_SELECT_CARD:
494 case MMC_CMD_APP_CMD:
498 printf(DRIVER_NAME": Cmd(d'%d) err\n", cmd->cmdidx);
499 ret = sh_mmcif_error_manage(host);
508 if (!(opc & 0x00C00000))
511 if (host->wait_int == 1) {
512 sh_mmcif_get_response(host, cmd);
516 ret = sh_mmcif_data_trans(host, data, cmd->cmdidx);
517 host->last_cmd = cmd->cmdidx;
522 static int sh_mmcif_request(struct mmc *mmc, struct mmc_cmd *cmd,
523 struct mmc_data *data)
525 struct sh_mmcif_host *host = mmc_priv(mmc);
530 switch (cmd->cmdidx) {
531 case MMC_CMD_APP_CMD:
533 case MMC_CMD_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
538 /* send_if_cond cmd (not support) */
545 ret = sh_mmcif_start_cmd(host, data, cmd);
551 static void sh_mmcif_set_ios(struct mmc *mmc)
553 struct sh_mmcif_host *host = mmc_priv(mmc);
556 sh_mmcif_clock_control(host, mmc->clock);
558 if (mmc->bus_width == 8)
559 host->bus_width = MMC_BUS_WIDTH_8;
560 else if (mmc->bus_width == 4)
561 host->bus_width = MMC_BUS_WIDTH_4;
563 host->bus_width = MMC_BUS_WIDTH_1;
565 debug("clock = %d, buswidth = %d\n", mmc->clock, mmc->bus_width);
568 static int sh_mmcif_init(struct mmc *mmc)
570 struct sh_mmcif_host *host = mmc_priv(mmc);
572 sh_mmcif_sync_reset(host);
573 sh_mmcif_write(MASK_ALL, &host->regs->ce_int_mask);
577 int mmcif_mmc_init(void)
581 struct sh_mmcif_host *host = NULL;
583 mmc = malloc(sizeof(struct mmc));
586 memset(mmc, 0, sizeof(*mmc));
587 host = malloc(sizeof(struct sh_mmcif_host));
590 memset(host, 0, sizeof(*host));
592 mmc->f_min = CLKDEV_MMC_INIT;
593 mmc->f_max = CLKDEV_EMMC_DATA;
594 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
595 mmc->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT |
597 memcpy(mmc->name, DRIVER_NAME, sizeof(DRIVER_NAME));
598 mmc->send_cmd = sh_mmcif_request;
599 mmc->set_ios = sh_mmcif_set_ios;
600 mmc->init = sh_mmcif_init;
602 host->regs = (struct sh_mmcif_regs *)CONFIG_SH_MMCIF_ADDR;
603 host->clk = CONFIG_SH_MMCIF_CLK;