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