Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Marvell MMC/SD/SDIO driver |
| 4 | * |
Gerald Kerma | 2591fbd | 2014-12-13 21:35:31 +0100 | [diff] [blame] | 5 | * (C) Copyright 2012-2014 |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 6 | * Marvell Semiconductor <www.marvell.com> |
| 7 | * Written-by: Maen Suleiman, Gerald Kerma |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 11 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 13 | #include <malloc.h> |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 14 | #include <dm.h> |
| 15 | #include <fdtdec.h> |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 16 | #include <part.h> |
| 17 | #include <mmc.h> |
| 18 | #include <asm/io.h> |
| 19 | #include <asm/arch/cpu.h> |
Stefan Roese | 3dc23f7 | 2014-10-22 12:13:06 +0200 | [diff] [blame] | 20 | #include <asm/arch/soc.h> |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 21 | #include <mvebu_mmc.h> |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 22 | #include <dm/device_compat.h> |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 23 | |
Mario Schuknecht | bcd0698 | 2014-08-25 14:12:26 +0200 | [diff] [blame] | 24 | #define MVEBU_TARGET_DRAM 0 |
| 25 | |
Gerald Kerma | 28d27b7 | 2014-12-13 21:35:32 +0100 | [diff] [blame] | 26 | #define TIMEOUT_DELAY 5*CONFIG_SYS_HZ /* wait 5 seconds */ |
| 27 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 28 | static inline void *get_regbase(const struct mmc *mmc) |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 29 | { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 30 | struct mvebu_mmc_plat *pdata = mmc->priv; |
| 31 | |
| 32 | return pdata->iobase; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 35 | static void mvebu_mmc_write(const struct mmc *mmc, u32 offs, u32 val) |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 36 | { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 37 | writel(val, get_regbase(mmc) + (offs)); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 40 | static u32 mvebu_mmc_read(const struct mmc *mmc, u32 offs) |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 41 | { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 42 | return readl(get_regbase(mmc) + (offs)); |
| 43 | } |
| 44 | |
| 45 | static int mvebu_mmc_setup_data(struct udevice *dev, struct mmc_data *data) |
| 46 | { |
| 47 | struct mvebu_mmc_plat *pdata = dev_get_plat(dev); |
| 48 | struct mmc *mmc = &pdata->mmc; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 49 | u32 ctrl_reg; |
| 50 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 51 | dev_dbg(dev, "data %s : blocks=%d blksz=%d\n", |
| 52 | (data->flags & MMC_DATA_READ) ? "read" : "write", |
| 53 | data->blocks, data->blocksize); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 54 | |
| 55 | /* default to maximum timeout */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 56 | ctrl_reg = mvebu_mmc_read(mmc, SDIO_HOST_CTRL); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 57 | ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX); |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 58 | mvebu_mmc_write(mmc, SDIO_HOST_CTRL, ctrl_reg); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 59 | |
| 60 | if (data->flags & MMC_DATA_READ) { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 61 | mvebu_mmc_write(mmc, SDIO_SYS_ADDR_LOW, (u32)data->dest & 0xffff); |
| 62 | mvebu_mmc_write(mmc, SDIO_SYS_ADDR_HI, (u32)data->dest >> 16); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 63 | } else { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 64 | mvebu_mmc_write(mmc, SDIO_SYS_ADDR_LOW, (u32)data->src & 0xffff); |
| 65 | mvebu_mmc_write(mmc, SDIO_SYS_ADDR_HI, (u32)data->src >> 16); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 68 | mvebu_mmc_write(mmc, SDIO_BLK_COUNT, data->blocks); |
| 69 | mvebu_mmc_write(mmc, SDIO_BLK_SIZE, data->blocksize); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 74 | static int mvebu_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 75 | struct mmc_data *data) |
| 76 | { |
Gerald Kerma | 28d27b7 | 2014-12-13 21:35:32 +0100 | [diff] [blame] | 77 | ulong start; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 78 | ushort waittype = 0; |
| 79 | ushort resptype = 0; |
| 80 | ushort xfertype = 0; |
| 81 | ushort resp_indx = 0; |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 82 | struct mvebu_mmc_plat *pdata = dev_get_plat(dev); |
| 83 | struct mmc *mmc = &pdata->mmc; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 84 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 85 | dev_dbg(dev, "cmdidx [0x%x] resp_type[0x%x] cmdarg[0x%x]\n", |
| 86 | cmd->cmdidx, cmd->resp_type, cmd->cmdarg); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 87 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 88 | dev_dbg(dev, "cmd %d (hw state 0x%04x)\n", |
| 89 | cmd->cmdidx, mvebu_mmc_read(mmc, SDIO_HW_STATE)); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 90 | |
Gerald Kerma | 28d27b7 | 2014-12-13 21:35:32 +0100 | [diff] [blame] | 91 | /* |
| 92 | * Hardware weirdness. The FIFO_EMPTY bit of the HW_STATE |
| 93 | * register is sometimes not set before a while when some |
| 94 | * "unusual" data block sizes are used (such as with the SWITCH |
| 95 | * command), even despite the fact that the XFER_DONE interrupt |
| 96 | * was raised. And if another data transfer starts before |
| 97 | * this bit comes to good sense (which eventually happens by |
| 98 | * itself) then the new transfer simply fails with a timeout. |
| 99 | */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 100 | if (!(mvebu_mmc_read(mmc, SDIO_HW_STATE) & CMD_FIFO_EMPTY)) { |
Gerald Kerma | 28d27b7 | 2014-12-13 21:35:32 +0100 | [diff] [blame] | 101 | ushort hw_state, count = 0; |
| 102 | |
| 103 | start = get_timer(0); |
| 104 | do { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 105 | hw_state = mvebu_mmc_read(mmc, SDIO_HW_STATE); |
Gerald Kerma | 28d27b7 | 2014-12-13 21:35:32 +0100 | [diff] [blame] | 106 | if ((get_timer(0) - start) > TIMEOUT_DELAY) { |
| 107 | printf("%s : FIFO_EMPTY bit missing\n", |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 108 | dev->name); |
Gerald Kerma | 28d27b7 | 2014-12-13 21:35:32 +0100 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | count++; |
| 112 | } while (!(hw_state & CMD_FIFO_EMPTY)); |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 113 | dev_dbg(dev, "*** wait for FIFO_EMPTY bit (hw=0x%04x, count=%d, jiffies=%ld)\n", |
| 114 | hw_state, count, (get_timer(0) - (start))); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Gerald Kerma | 02b2739 | 2014-12-13 21:35:35 +0100 | [diff] [blame] | 117 | /* Clear status */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 118 | mvebu_mmc_write(mmc, SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK); |
| 119 | mvebu_mmc_write(mmc, SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 120 | |
| 121 | resptype = SDIO_CMD_INDEX(cmd->cmdidx); |
| 122 | |
| 123 | /* Analyzing resptype/xfertype/waittype for the command */ |
| 124 | if (cmd->resp_type & MMC_RSP_BUSY) |
| 125 | resptype |= SDIO_CMD_RSP_48BUSY; |
| 126 | else if (cmd->resp_type & MMC_RSP_136) |
| 127 | resptype |= SDIO_CMD_RSP_136; |
| 128 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 129 | resptype |= SDIO_CMD_RSP_48; |
| 130 | else |
| 131 | resptype |= SDIO_CMD_RSP_NONE; |
| 132 | |
| 133 | if (cmd->resp_type & MMC_RSP_CRC) |
| 134 | resptype |= SDIO_CMD_CHECK_CMDCRC; |
| 135 | |
| 136 | if (cmd->resp_type & MMC_RSP_OPCODE) |
| 137 | resptype |= SDIO_CMD_INDX_CHECK; |
| 138 | |
| 139 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 140 | resptype |= SDIO_UNEXPECTED_RESP; |
| 141 | waittype |= SDIO_NOR_UNEXP_RSP; |
| 142 | } |
| 143 | |
| 144 | if (data) { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 145 | int err = mvebu_mmc_setup_data(dev, data); |
Gerald Kerma | 02b2739 | 2014-12-13 21:35:35 +0100 | [diff] [blame] | 146 | |
| 147 | if (err) { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 148 | dev_dbg(dev, "command DATA error :%x\n", err); |
Gerald Kerma | 02b2739 | 2014-12-13 21:35:35 +0100 | [diff] [blame] | 149 | return err; |
| 150 | } |
| 151 | |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 152 | resptype |= SDIO_CMD_DATA_PRESENT | SDIO_CMD_CHECK_DATACRC16; |
| 153 | xfertype |= SDIO_XFER_MODE_HW_WR_DATA_EN; |
| 154 | if (data->flags & MMC_DATA_READ) { |
| 155 | xfertype |= SDIO_XFER_MODE_TO_HOST; |
| 156 | waittype = SDIO_NOR_DMA_INI; |
| 157 | } else { |
| 158 | waittype |= SDIO_NOR_XFER_DONE; |
| 159 | } |
| 160 | } else { |
| 161 | waittype |= SDIO_NOR_CMD_DONE; |
| 162 | } |
| 163 | |
| 164 | /* Setting cmd arguments */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 165 | mvebu_mmc_write(mmc, SDIO_ARG_LOW, cmd->cmdarg & 0xffff); |
| 166 | mvebu_mmc_write(mmc, SDIO_ARG_HI, cmd->cmdarg >> 16); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 167 | |
| 168 | /* Setting Xfer mode */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 169 | mvebu_mmc_write(mmc, SDIO_XFER_MODE, xfertype); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 170 | |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 171 | /* Sending command */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 172 | mvebu_mmc_write(mmc, SDIO_CMD, resptype); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 173 | |
Gerald Kerma | 28d27b7 | 2014-12-13 21:35:32 +0100 | [diff] [blame] | 174 | start = get_timer(0); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 175 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 176 | while (!((mvebu_mmc_read(mmc, SDIO_NOR_INTR_STATUS)) & waittype)) { |
| 177 | if (mvebu_mmc_read(mmc, SDIO_NOR_INTR_STATUS) & SDIO_NOR_ERROR) { |
| 178 | dev_dbg(dev, "error! cmdidx : %d, err reg: %04x\n", |
| 179 | cmd->cmdidx, |
| 180 | mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS)); |
| 181 | if (mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS) & |
Gerald Kerma | fc0f25f | 2014-12-13 21:35:33 +0100 | [diff] [blame] | 182 | (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT)) { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 183 | dev_dbg(dev, "command READ timed out\n"); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 184 | return -ETIMEDOUT; |
Gerald Kerma | fc0f25f | 2014-12-13 21:35:33 +0100 | [diff] [blame] | 185 | } |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 186 | dev_dbg(dev, "command READ error\n"); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 187 | return -ECOMM; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Gerald Kerma | 28d27b7 | 2014-12-13 21:35:32 +0100 | [diff] [blame] | 190 | if ((get_timer(0) - start) > TIMEOUT_DELAY) { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 191 | dev_dbg(dev, "command timed out\n"); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 192 | return -ETIMEDOUT; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 193 | } |
| 194 | } |
Gerald Kerma | 28d27b7 | 2014-12-13 21:35:32 +0100 | [diff] [blame] | 195 | |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 196 | /* Handling response */ |
| 197 | if (cmd->resp_type & MMC_RSP_136) { |
| 198 | uint response[8]; |
| 199 | |
| 200 | for (resp_indx = 0; resp_indx < 8; resp_indx++) |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 201 | response[resp_indx] = mvebu_mmc_read(mmc, SDIO_RSP(resp_indx)); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 202 | |
| 203 | cmd->response[0] = ((response[0] & 0x03ff) << 22) | |
| 204 | ((response[1] & 0xffff) << 6) | |
| 205 | ((response[2] & 0xfc00) >> 10); |
| 206 | cmd->response[1] = ((response[2] & 0x03ff) << 22) | |
| 207 | ((response[3] & 0xffff) << 6) | |
| 208 | ((response[4] & 0xfc00) >> 10); |
| 209 | cmd->response[2] = ((response[4] & 0x03ff) << 22) | |
| 210 | ((response[5] & 0xffff) << 6) | |
| 211 | ((response[6] & 0xfc00) >> 10); |
| 212 | cmd->response[3] = ((response[6] & 0x03ff) << 22) | |
| 213 | ((response[7] & 0x3fff) << 8); |
| 214 | } else if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 215 | uint response[3]; |
| 216 | |
| 217 | for (resp_indx = 0; resp_indx < 3; resp_indx++) |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 218 | response[resp_indx] = mvebu_mmc_read(mmc, SDIO_RSP(resp_indx)); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 219 | |
| 220 | cmd->response[0] = ((response[2] & 0x003f) << (8 - 8)) | |
| 221 | ((response[1] & 0xffff) << (14 - 8)) | |
| 222 | ((response[0] & 0x03ff) << (30 - 8)); |
| 223 | cmd->response[1] = ((response[0] & 0xfc00) >> 10); |
| 224 | cmd->response[2] = 0; |
| 225 | cmd->response[3] = 0; |
Gerald Kerma | 02b2739 | 2014-12-13 21:35:35 +0100 | [diff] [blame] | 226 | } else { |
| 227 | cmd->response[0] = 0; |
| 228 | cmd->response[1] = 0; |
| 229 | cmd->response[2] = 0; |
| 230 | cmd->response[3] = 0; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 233 | dev_dbg(dev, "resp[0x%x] ", cmd->resp_type); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 234 | debug("[0x%x] ", cmd->response[0]); |
| 235 | debug("[0x%x] ", cmd->response[1]); |
| 236 | debug("[0x%x] ", cmd->response[2]); |
| 237 | debug("[0x%x] ", cmd->response[3]); |
| 238 | debug("\n"); |
| 239 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 240 | if (mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS) & |
Gerald Kerma | 02b2739 | 2014-12-13 21:35:35 +0100 | [diff] [blame] | 241 | (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT)) |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 242 | return -ETIMEDOUT; |
Gerald Kerma | 02b2739 | 2014-12-13 21:35:35 +0100 | [diff] [blame] | 243 | |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 247 | static void mvebu_mmc_power_up(struct udevice *dev) |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 248 | { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 249 | struct mvebu_mmc_plat *pdata = dev_get_plat(dev); |
| 250 | struct mmc *mmc = &pdata->mmc; |
| 251 | |
| 252 | dev_dbg(dev, "power up\n"); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 253 | |
| 254 | /* disable interrupts */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 255 | mvebu_mmc_write(mmc, SDIO_NOR_INTR_EN, 0); |
| 256 | mvebu_mmc_write(mmc, SDIO_ERR_INTR_EN, 0); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 257 | |
| 258 | /* SW reset */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 259 | mvebu_mmc_write(mmc, SDIO_SW_RESET, SDIO_SW_RESET_NOW); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 260 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 261 | mvebu_mmc_write(mmc, SDIO_XFER_MODE, 0); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 262 | |
| 263 | /* enable status */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 264 | mvebu_mmc_write(mmc, SDIO_NOR_STATUS_EN, SDIO_POLL_MASK); |
| 265 | mvebu_mmc_write(mmc, SDIO_ERR_STATUS_EN, SDIO_POLL_MASK); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 266 | |
| 267 | /* enable interrupts status */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 268 | mvebu_mmc_write(mmc, SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK); |
| 269 | mvebu_mmc_write(mmc, SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 272 | static void mvebu_mmc_set_clk(struct udevice *dev, unsigned int clock) |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 273 | { |
| 274 | unsigned int m; |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 275 | struct mvebu_mmc_plat *pdata = dev_get_plat(dev); |
| 276 | struct mmc *mmc = &pdata->mmc; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 277 | |
| 278 | if (clock == 0) { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 279 | dev_dbg(dev, "clock off\n"); |
| 280 | mvebu_mmc_write(mmc, SDIO_XFER_MODE, SDIO_XFER_MODE_STOP_CLK); |
| 281 | mvebu_mmc_write(mmc, SDIO_CLK_DIV, MVEBU_MMC_BASE_DIV_MAX); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 282 | } else { |
| 283 | m = MVEBU_MMC_BASE_FAST_CLOCK/(2*clock) - 1; |
| 284 | if (m > MVEBU_MMC_BASE_DIV_MAX) |
| 285 | m = MVEBU_MMC_BASE_DIV_MAX; |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 286 | mvebu_mmc_write(mmc, SDIO_CLK_DIV, m & MVEBU_MMC_BASE_DIV_MAX); |
| 287 | dev_dbg(dev, "clock (%d) div : %d\n", clock, m); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 288 | } |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 291 | static void mvebu_mmc_set_bus(struct udevice *dev, unsigned int bus) |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 292 | { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 293 | struct mvebu_mmc_plat *pdata = dev_get_plat(dev); |
| 294 | struct mmc *mmc = &pdata->mmc; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 295 | u32 ctrl_reg = 0; |
| 296 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 297 | ctrl_reg = mvebu_mmc_read(mmc, SDIO_HOST_CTRL); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 298 | ctrl_reg &= ~SDIO_HOST_CTRL_DATA_WIDTH_4_BITS; |
| 299 | |
| 300 | switch (bus) { |
| 301 | case 4: |
| 302 | ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_4_BITS; |
| 303 | break; |
| 304 | case 1: |
| 305 | default: |
| 306 | ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_1_BIT; |
| 307 | } |
| 308 | |
| 309 | /* default transfer mode */ |
| 310 | ctrl_reg |= SDIO_HOST_CTRL_BIG_ENDIAN; |
| 311 | ctrl_reg &= ~SDIO_HOST_CTRL_LSB_FIRST; |
| 312 | |
| 313 | /* default to maximum timeout */ |
| 314 | ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX); |
Mario Schuknecht | bcd0698 | 2014-08-25 14:12:26 +0200 | [diff] [blame] | 315 | ctrl_reg |= SDIO_HOST_CTRL_TMOUT_EN; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 316 | |
| 317 | ctrl_reg |= SDIO_HOST_CTRL_PUSH_PULL_EN; |
| 318 | |
| 319 | ctrl_reg |= SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY; |
| 320 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 321 | dev_dbg(dev, "ctrl 0x%04x: %s %s %s\n", ctrl_reg, |
| 322 | (ctrl_reg & SDIO_HOST_CTRL_PUSH_PULL_EN) ? |
| 323 | "push-pull" : "open-drain", |
| 324 | (ctrl_reg & SDIO_HOST_CTRL_DATA_WIDTH_4_BITS) ? |
| 325 | "4bit-width" : "1bit-width", |
| 326 | (ctrl_reg & SDIO_HOST_CTRL_HI_SPEED_EN) ? |
| 327 | "high-speed" : ""); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 328 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 329 | mvebu_mmc_write(mmc, SDIO_HOST_CTRL, ctrl_reg); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 332 | static int mvebu_mmc_set_ios(struct udevice *dev) |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 333 | { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 334 | struct mvebu_mmc_plat *pdata = dev_get_plat(dev); |
| 335 | struct mmc *mmc = &pdata->mmc; |
| 336 | |
| 337 | dev_dbg(dev, "bus[%d] clock[%d]\n", |
| 338 | mmc->bus_width, mmc->clock); |
| 339 | mvebu_mmc_set_bus(dev, mmc->bus_width); |
| 340 | mvebu_mmc_set_clk(dev, mmc->clock); |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 341 | |
| 342 | return 0; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Mario Schuknecht | bcd0698 | 2014-08-25 14:12:26 +0200 | [diff] [blame] | 345 | /* |
| 346 | * Set window register. |
| 347 | */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 348 | static void mvebu_window_setup(const struct mmc *mmc) |
Mario Schuknecht | bcd0698 | 2014-08-25 14:12:26 +0200 | [diff] [blame] | 349 | { |
| 350 | int i; |
| 351 | |
| 352 | for (i = 0; i < 4; i++) { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 353 | mvebu_mmc_write(mmc, WINDOW_CTRL(i), 0); |
| 354 | mvebu_mmc_write(mmc, WINDOW_BASE(i), 0); |
Mario Schuknecht | bcd0698 | 2014-08-25 14:12:26 +0200 | [diff] [blame] | 355 | } |
| 356 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 357 | u32 size, base, attrib; |
| 358 | |
| 359 | /* Enable DRAM bank */ |
| 360 | switch (i) { |
| 361 | case 0: |
| 362 | attrib = KWCPU_ATTR_DRAM_CS0; |
| 363 | break; |
| 364 | case 1: |
| 365 | attrib = KWCPU_ATTR_DRAM_CS1; |
| 366 | break; |
| 367 | case 2: |
| 368 | attrib = KWCPU_ATTR_DRAM_CS2; |
| 369 | break; |
| 370 | case 3: |
| 371 | attrib = KWCPU_ATTR_DRAM_CS3; |
| 372 | break; |
| 373 | default: |
| 374 | /* invalide bank, disable access */ |
| 375 | attrib = 0; |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | size = gd->bd->bi_dram[i].size; |
| 380 | base = gd->bd->bi_dram[i].start; |
| 381 | if (size && attrib) { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 382 | mvebu_mmc_write(mmc, WINDOW_CTRL(i), |
Mario Schuknecht | bcd0698 | 2014-08-25 14:12:26 +0200 | [diff] [blame] | 383 | MVCPU_WIN_CTRL_DATA(size, |
| 384 | MVEBU_TARGET_DRAM, |
| 385 | attrib, |
| 386 | MVCPU_WIN_ENABLE)); |
| 387 | } else { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 388 | mvebu_mmc_write(mmc, WINDOW_CTRL(i), MVCPU_WIN_DISABLE); |
Mario Schuknecht | bcd0698 | 2014-08-25 14:12:26 +0200 | [diff] [blame] | 389 | } |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 390 | mvebu_mmc_write(mmc, WINDOW_BASE(i), base); |
Mario Schuknecht | bcd0698 | 2014-08-25 14:12:26 +0200 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 394 | static int mvebu_mmc_initialize(struct udevice *dev) |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 395 | { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 396 | struct mvebu_mmc_plat *pdata = dev_get_plat(dev); |
| 397 | struct mmc *mmc = &pdata->mmc; |
| 398 | |
| 399 | dev_dbg(dev, "%s\n", __func__); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 400 | |
| 401 | /* |
| 402 | * Setting host parameters |
| 403 | * Initial Host Ctrl : Timeout : max , Normal Speed mode, |
| 404 | * 4-bit data mode, Big Endian, SD memory Card, Push_pull CMD Line |
| 405 | */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 406 | mvebu_mmc_write(mmc, SDIO_HOST_CTRL, |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 407 | SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX) | |
| 408 | SDIO_HOST_CTRL_DATA_WIDTH_4_BITS | |
| 409 | SDIO_HOST_CTRL_BIG_ENDIAN | |
| 410 | SDIO_HOST_CTRL_PUSH_PULL_EN | |
| 411 | SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY); |
| 412 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 413 | mvebu_mmc_write(mmc, SDIO_CLK_CTRL, 0); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 414 | |
| 415 | /* enable status */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 416 | mvebu_mmc_write(mmc, SDIO_NOR_STATUS_EN, SDIO_POLL_MASK); |
| 417 | mvebu_mmc_write(mmc, SDIO_ERR_STATUS_EN, SDIO_POLL_MASK); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 418 | |
| 419 | /* disable interrupts */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 420 | mvebu_mmc_write(mmc, SDIO_NOR_INTR_EN, 0); |
| 421 | mvebu_mmc_write(mmc, SDIO_ERR_INTR_EN, 0); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 422 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 423 | mvebu_window_setup(mmc); |
Mario Schuknecht | bcd0698 | 2014-08-25 14:12:26 +0200 | [diff] [blame] | 424 | |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 425 | /* SW reset */ |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 426 | mvebu_mmc_write(mmc, SDIO_SW_RESET, SDIO_SW_RESET_NOW); |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 427 | |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 431 | static int mvebu_mmc_of_to_plat(struct udevice *dev) |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 432 | { |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 433 | struct mvebu_mmc_plat *pdata = dev_get_plat(dev); |
| 434 | fdt_addr_t addr; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 435 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 436 | addr = dev_read_addr(dev); |
| 437 | if (addr == FDT_ADDR_T_NONE) |
| 438 | return -EINVAL; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 439 | |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 440 | pdata->iobase = (void *)addr; |
DrEagle | 3fe3b4f | 2014-07-25 21:07:30 +0200 | [diff] [blame] | 441 | |
| 442 | return 0; |
| 443 | } |
Harm Berntsen | c689ae0 | 2021-03-30 10:19:41 +0200 | [diff] [blame] | 444 | |
| 445 | static int mvebu_mmc_probe(struct udevice *dev) |
| 446 | { |
| 447 | struct mvebu_mmc_plat *pdata = dev_get_plat(dev); |
| 448 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 449 | struct mmc *mmc = &pdata->mmc; |
| 450 | struct mmc_config *cfg = &pdata->cfg; |
| 451 | |
| 452 | cfg->name = dev->name; |
| 453 | cfg->f_min = MVEBU_MMC_BASE_FAST_CLOCK / MVEBU_MMC_BASE_DIV_MAX; |
| 454 | cfg->f_max = MVEBU_MMC_CLOCKRATE_MAX; |
| 455 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 456 | cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_HS | MMC_MODE_HS_52MHz; |
| 457 | cfg->part_type = PART_TYPE_DOS; |
| 458 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 459 | |
| 460 | mmc->cfg = cfg; |
| 461 | mmc->priv = pdata; |
| 462 | mmc->dev = dev; |
| 463 | upriv->mmc = mmc; |
| 464 | |
| 465 | mvebu_mmc_power_up(dev); |
| 466 | mvebu_mmc_initialize(dev); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static const struct dm_mmc_ops mvebu_dm_mmc_ops = { |
| 472 | .send_cmd = mvebu_mmc_send_cmd, |
| 473 | .set_ios = mvebu_mmc_set_ios, |
| 474 | }; |
| 475 | |
| 476 | static int mvebu_mmc_bind(struct udevice *dev) |
| 477 | { |
| 478 | struct mvebu_mmc_plat *pdata = dev_get_plat(dev); |
| 479 | |
| 480 | return mmc_bind(dev, &pdata->mmc, &pdata->cfg); |
| 481 | } |
| 482 | |
| 483 | static const struct udevice_id mvebu_mmc_match[] = { |
| 484 | { .compatible = "marvell,orion-sdio" }, |
| 485 | { /* sentinel */ } |
| 486 | }; |
| 487 | |
| 488 | U_BOOT_DRIVER(mvebu_mmc) = { |
| 489 | .name = "mvebu_mmc", |
| 490 | .id = UCLASS_MMC, |
| 491 | .of_match = mvebu_mmc_match, |
| 492 | .ops = &mvebu_dm_mmc_ops, |
| 493 | .probe = mvebu_mmc_probe, |
| 494 | .bind = mvebu_mmc_bind, |
| 495 | .of_to_plat = mvebu_mmc_of_to_plat, |
| 496 | .plat_auto = sizeof(struct mvebu_mmc_plat), |
| 497 | }; |