Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 |
| 3 | * Rob Emanuele <rob@emanuele.us> |
| 4 | * Reinhard Meyer, EMK Elektronik <reinhard.meyer@emk-elektronik.de> |
| 5 | * |
| 6 | * Original Driver: |
| 7 | * Copyright (C) 2004-2006 Atmel Corporation |
| 8 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 13 | #include <clk.h> |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 14 | #include <mmc.h> |
| 15 | #include <part.h> |
| 16 | #include <malloc.h> |
| 17 | #include <asm/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 18 | #include <linux/errno.h> |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 19 | #include <asm/byteorder.h> |
| 20 | #include <asm/arch/clk.h> |
Reinhard Meyer | 7f619cb | 2010-11-03 16:32:56 +0100 | [diff] [blame] | 21 | #include <asm/arch/hardware.h> |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 22 | #include <dm/device.h> |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 23 | #include "atmel_mci.h" |
| 24 | |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 27 | #ifndef CONFIG_SYS_MMC_CLK_OD |
| 28 | # define CONFIG_SYS_MMC_CLK_OD 150000 |
| 29 | #endif |
| 30 | |
| 31 | #define MMC_DEFAULT_BLKLEN 512 |
| 32 | |
| 33 | #if defined(CONFIG_ATMEL_MCI_PORTB) |
| 34 | # define MCI_BUS 1 |
| 35 | #else |
| 36 | # define MCI_BUS 0 |
| 37 | #endif |
| 38 | |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 39 | struct atmel_mci_priv { |
| 40 | struct mmc_config cfg; |
| 41 | struct atmel_mci *mci; |
Marek Vasut | 0b7d54e | 2015-10-23 20:46:31 +0200 | [diff] [blame] | 42 | unsigned int initialized:1; |
Gregory CLEMENT | 31f8a00 | 2015-11-05 20:58:30 +0100 | [diff] [blame] | 43 | unsigned int curr_clk; |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 44 | #ifdef CONFIG_DM_MMC |
| 45 | struct mmc mmc; |
| 46 | ulong bus_clk_rate; |
| 47 | #endif |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
Bo Shen | 644b476 | 2013-04-26 00:27:06 +0000 | [diff] [blame] | 50 | /* Read Atmel MCI IP version */ |
| 51 | static unsigned int atmel_mci_get_version(struct atmel_mci *mci) |
| 52 | { |
| 53 | return readl(&mci->version) & 0x00000fff; |
| 54 | } |
| 55 | |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 56 | /* |
| 57 | * Print command and status: |
| 58 | * |
| 59 | * - always when DEBUG is defined |
| 60 | * - on command errors |
| 61 | */ |
| 62 | static void dump_cmd(u32 cmdr, u32 arg, u32 status, const char* msg) |
| 63 | { |
Marek Vasut | 34524b1 | 2015-10-23 20:46:28 +0200 | [diff] [blame] | 64 | debug("gen_atmel_mci: CMDR %08x (%2u) ARGR %08x (SR: %08x) %s\n", |
| 65 | cmdr, cmdr & 0x3F, arg, status, msg); |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* Setup for MCI Clock and Block Size */ |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 69 | #ifdef CONFIG_DM_MMC |
| 70 | static void mci_set_mode(struct atmel_mci_priv *priv, u32 hz, u32 blklen) |
| 71 | { |
| 72 | struct mmc *mmc = &priv->mmc; |
| 73 | u32 bus_hz = priv->bus_clk_rate; |
| 74 | #else |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 75 | static void mci_set_mode(struct mmc *mmc, u32 hz, u32 blklen) |
| 76 | { |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 77 | struct atmel_mci_priv *priv = mmc->priv; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 78 | u32 bus_hz = get_mci_clk_rate(); |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 79 | #endif |
| 80 | |
| 81 | atmel_mci_t *mci = priv->mci; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 82 | u32 clkdiv = 255; |
Bo Shen | 02d7728 | 2014-07-31 14:39:30 +0800 | [diff] [blame] | 83 | unsigned int version = atmel_mci_get_version(mci); |
| 84 | u32 clkodd = 0; |
| 85 | u32 mr; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 86 | |
| 87 | debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n", |
| 88 | bus_hz, hz, blklen); |
| 89 | if (hz > 0) { |
Bo Shen | 02d7728 | 2014-07-31 14:39:30 +0800 | [diff] [blame] | 90 | if (version >= 0x500) { |
| 91 | clkdiv = DIV_ROUND_UP(bus_hz, hz) - 2; |
| 92 | if (clkdiv > 511) |
| 93 | clkdiv = 511; |
| 94 | |
| 95 | clkodd = clkdiv & 1; |
| 96 | clkdiv >>= 1; |
| 97 | |
Marek Vasut | 34524b1 | 2015-10-23 20:46:28 +0200 | [diff] [blame] | 98 | debug("mci: setting clock %u Hz, block size %u\n", |
| 99 | bus_hz / (clkdiv * 2 + clkodd + 2), blklen); |
Bo Shen | 02d7728 | 2014-07-31 14:39:30 +0800 | [diff] [blame] | 100 | } else { |
| 101 | /* find clkdiv yielding a rate <= than requested */ |
| 102 | for (clkdiv = 0; clkdiv < 255; clkdiv++) { |
| 103 | if ((bus_hz / (clkdiv + 1) / 2) <= hz) |
| 104 | break; |
| 105 | } |
Marek Vasut | 34524b1 | 2015-10-23 20:46:28 +0200 | [diff] [blame] | 106 | debug("mci: setting clock %u Hz, block size %u\n", |
| 107 | (bus_hz / (clkdiv + 1)) / 2, blklen); |
Bo Shen | 02d7728 | 2014-07-31 14:39:30 +0800 | [diff] [blame] | 108 | |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 109 | } |
| 110 | } |
Gregory CLEMENT | 31f8a00 | 2015-11-05 20:58:30 +0100 | [diff] [blame] | 111 | if (version >= 0x500) |
| 112 | priv->curr_clk = bus_hz / (clkdiv * 2 + clkodd + 2); |
| 113 | else |
| 114 | priv->curr_clk = (bus_hz / (clkdiv + 1)) / 2; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 115 | blklen &= 0xfffc; |
Bo Shen | 02d7728 | 2014-07-31 14:39:30 +0800 | [diff] [blame] | 116 | |
| 117 | mr = MMCI_BF(CLKDIV, clkdiv); |
| 118 | |
| 119 | /* MCI IP version >= 0x200 has R/WPROOF */ |
| 120 | if (version >= 0x200) |
| 121 | mr |= MMCI_BIT(RDPROOF) | MMCI_BIT(WRPROOF); |
| 122 | |
Wu, Josh | d1e486b | 2012-09-13 22:22:04 +0000 | [diff] [blame] | 123 | /* |
Bo Shen | 02d7728 | 2014-07-31 14:39:30 +0800 | [diff] [blame] | 124 | * MCI IP version >= 0x500 use bit 16 as clkodd. |
| 125 | * MCI IP version < 0x500 use upper 16 bits for blklen. |
Wu, Josh | d1e486b | 2012-09-13 22:22:04 +0000 | [diff] [blame] | 126 | */ |
Bo Shen | 02d7728 | 2014-07-31 14:39:30 +0800 | [diff] [blame] | 127 | if (version >= 0x500) |
| 128 | mr |= MMCI_BF(CLKODD, clkodd); |
| 129 | else |
| 130 | mr |= MMCI_BF(BLKLEN, blklen); |
| 131 | |
| 132 | writel(mr, &mci->mr); |
| 133 | |
| 134 | /* MCI IP version >= 0x200 has blkr */ |
| 135 | if (version >= 0x200) |
| 136 | writel(MMCI_BF(BLKLEN, blklen), &mci->blkr); |
| 137 | |
Bo Shen | 1dc856d | 2014-07-31 14:39:32 +0800 | [diff] [blame] | 138 | if (mmc->card_caps & mmc->cfg->host_caps & MMC_MODE_HS) |
| 139 | writel(MMCI_BIT(HSMODE), &mci->cfg); |
| 140 | |
Marek Vasut | 0b7d54e | 2015-10-23 20:46:31 +0200 | [diff] [blame] | 141 | priv->initialized = 1; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* Return the CMDR with flags for a given command and data packet */ |
| 145 | static u32 mci_encode_cmd( |
| 146 | struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags) |
| 147 | { |
| 148 | u32 cmdr = 0; |
| 149 | |
| 150 | /* Default Flags for Errors */ |
| 151 | *error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) | |
| 152 | MMCI_BIT(RINDE) | MMCI_BIT(RTOE)); |
| 153 | |
| 154 | /* Default Flags for the Command */ |
| 155 | cmdr |= MMCI_BIT(MAXLAT); |
| 156 | |
| 157 | if (data) { |
| 158 | cmdr |= MMCI_BF(TRCMD, 1); |
| 159 | if (data->blocks > 1) |
| 160 | cmdr |= MMCI_BF(TRTYP, 1); |
| 161 | if (data->flags & MMC_DATA_READ) |
| 162 | cmdr |= MMCI_BIT(TRDIR); |
| 163 | } |
| 164 | |
| 165 | if (cmd->resp_type & MMC_RSP_CRC) |
| 166 | *error_flags |= MMCI_BIT(RCRCE); |
| 167 | if (cmd->resp_type & MMC_RSP_136) |
| 168 | cmdr |= MMCI_BF(RSPTYP, 2); |
| 169 | else if (cmd->resp_type & MMC_RSP_BUSY) |
| 170 | cmdr |= MMCI_BF(RSPTYP, 3); |
| 171 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 172 | cmdr |= MMCI_BF(RSPTYP, 1); |
| 173 | |
| 174 | return cmdr | MMCI_BF(CMDNB, cmd->cmdidx); |
| 175 | } |
| 176 | |
| 177 | /* Entered into function pointer in mci_send_cmd */ |
| 178 | static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags) |
| 179 | { |
| 180 | u32 status; |
| 181 | |
| 182 | do { |
| 183 | status = readl(&mci->sr); |
| 184 | if (status & (error_flags | MMCI_BIT(OVRE))) |
| 185 | goto io_fail; |
| 186 | } while (!(status & MMCI_BIT(RXRDY))); |
| 187 | |
| 188 | if (status & MMCI_BIT(RXRDY)) { |
| 189 | *data = readl(&mci->rdr); |
| 190 | status = 0; |
| 191 | } |
| 192 | io_fail: |
| 193 | return status; |
| 194 | } |
| 195 | |
| 196 | /* Entered into function pointer in mci_send_cmd */ |
| 197 | static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags) |
| 198 | { |
| 199 | u32 status; |
| 200 | |
| 201 | do { |
| 202 | status = readl(&mci->sr); |
| 203 | if (status & (error_flags | MMCI_BIT(UNRE))) |
| 204 | goto io_fail; |
| 205 | } while (!(status & MMCI_BIT(TXRDY))); |
| 206 | |
| 207 | if (status & MMCI_BIT(TXRDY)) { |
| 208 | writel(*data, &mci->tdr); |
| 209 | status = 0; |
| 210 | } |
| 211 | io_fail: |
| 212 | return status; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Entered into mmc structure during driver init |
| 217 | * |
| 218 | * Sends a command out on the bus and deals with the block data. |
| 219 | * Takes the mmc pointer, a command pointer, and an optional data pointer. |
| 220 | */ |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 221 | #ifdef CONFIG_DM_MMC |
| 222 | static int atmel_mci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 223 | struct mmc_data *data) |
| 224 | { |
| 225 | struct atmel_mci_priv *priv = dev_get_priv(dev); |
| 226 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 227 | #else |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 228 | static int |
| 229 | mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 230 | { |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 231 | struct atmel_mci_priv *priv = mmc->priv; |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 232 | #endif |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 233 | atmel_mci_t *mci = priv->mci; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 234 | u32 cmdr; |
| 235 | u32 error_flags = 0; |
| 236 | u32 status; |
| 237 | |
Marek Vasut | 0b7d54e | 2015-10-23 20:46:31 +0200 | [diff] [blame] | 238 | if (!priv->initialized) { |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 239 | puts ("MCI not initialized!\n"); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 240 | return -ECOMM; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* Figure out the transfer arguments */ |
| 244 | cmdr = mci_encode_cmd(cmd, data, &error_flags); |
| 245 | |
Wu, Josh | d1e486b | 2012-09-13 22:22:04 +0000 | [diff] [blame] | 246 | /* For multi blocks read/write, set the block register */ |
| 247 | if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) |
| 248 | || (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) |
| 249 | writel(data->blocks | MMCI_BF(BLKLEN, mmc->read_bl_len), |
| 250 | &mci->blkr); |
| 251 | |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 252 | /* Send the command */ |
| 253 | writel(cmd->cmdarg, &mci->argr); |
| 254 | writel(cmdr, &mci->cmdr); |
| 255 | |
| 256 | #ifdef DEBUG |
| 257 | dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG"); |
| 258 | #endif |
| 259 | |
| 260 | /* Wait for the command to complete */ |
| 261 | while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY))); |
| 262 | |
Bo Shen | 03a743f | 2013-04-26 00:27:07 +0000 | [diff] [blame] | 263 | if ((status & error_flags) & MMCI_BIT(RTOE)) { |
| 264 | dump_cmd(cmdr, cmd->cmdarg, status, "Command Time Out"); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 265 | return -ETIMEDOUT; |
Bo Shen | 03a743f | 2013-04-26 00:27:07 +0000 | [diff] [blame] | 266 | } else if (status & error_flags) { |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 267 | dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed"); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 268 | return -ECOMM; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* Copy the response to the response buffer */ |
| 272 | if (cmd->resp_type & MMC_RSP_136) { |
| 273 | cmd->response[0] = readl(&mci->rspr); |
| 274 | cmd->response[1] = readl(&mci->rspr1); |
| 275 | cmd->response[2] = readl(&mci->rspr2); |
| 276 | cmd->response[3] = readl(&mci->rspr3); |
| 277 | } else |
| 278 | cmd->response[0] = readl(&mci->rspr); |
| 279 | |
| 280 | /* transfer all of the blocks */ |
| 281 | if (data) { |
| 282 | u32 word_count, block_count; |
| 283 | u32* ioptr; |
| 284 | u32 sys_blocksize, dummy, i; |
| 285 | u32 (*mci_data_op) |
| 286 | (atmel_mci_t *mci, u32* data, u32 error_flags); |
| 287 | |
| 288 | if (data->flags & MMC_DATA_READ) { |
| 289 | mci_data_op = mci_data_read; |
| 290 | sys_blocksize = mmc->read_bl_len; |
| 291 | ioptr = (u32*)data->dest; |
| 292 | } else { |
| 293 | mci_data_op = mci_data_write; |
| 294 | sys_blocksize = mmc->write_bl_len; |
| 295 | ioptr = (u32*)data->src; |
| 296 | } |
| 297 | |
| 298 | status = 0; |
| 299 | for (block_count = 0; |
| 300 | block_count < data->blocks && !status; |
| 301 | block_count++) { |
| 302 | word_count = 0; |
| 303 | do { |
| 304 | status = mci_data_op(mci, ioptr, error_flags); |
| 305 | word_count++; |
| 306 | ioptr++; |
| 307 | } while (!status && word_count < (data->blocksize/4)); |
| 308 | #ifdef DEBUG |
| 309 | if (data->flags & MMC_DATA_READ) |
| 310 | { |
Wu, Josh | 6bcb562 | 2014-05-07 17:06:08 +0800 | [diff] [blame] | 311 | u32 cnt = word_count * 4; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 312 | printf("Read Data:\n"); |
Wu, Josh | 6bcb562 | 2014-05-07 17:06:08 +0800 | [diff] [blame] | 313 | print_buffer(0, data->dest + cnt * block_count, |
| 314 | 1, cnt, 0); |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 315 | } |
| 316 | #endif |
| 317 | #ifdef DEBUG |
| 318 | if (!status && word_count < (sys_blocksize / 4)) |
| 319 | printf("filling rest of block...\n"); |
| 320 | #endif |
| 321 | /* fill the rest of a full block */ |
| 322 | while (!status && word_count < (sys_blocksize / 4)) { |
| 323 | status = mci_data_op(mci, &dummy, |
| 324 | error_flags); |
| 325 | word_count++; |
| 326 | } |
| 327 | if (status) { |
| 328 | dump_cmd(cmdr, cmd->cmdarg, status, |
| 329 | "Data Transfer Failed"); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 330 | return -ECOMM; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | /* Wait for Transfer End */ |
| 335 | i = 0; |
| 336 | do { |
| 337 | status = readl(&mci->sr); |
| 338 | |
| 339 | if (status & error_flags) { |
| 340 | dump_cmd(cmdr, cmd->cmdarg, status, |
| 341 | "DTIP Wait Failed"); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 342 | return -ECOMM; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 343 | } |
| 344 | i++; |
| 345 | } while ((status & MMCI_BIT(DTIP)) && i < 10000); |
| 346 | if (status & MMCI_BIT(DTIP)) { |
| 347 | dump_cmd(cmdr, cmd->cmdarg, status, |
| 348 | "XFER DTIP never unset, ignoring"); |
| 349 | } |
| 350 | } |
| 351 | |
Gregory CLEMENT | 31f8a00 | 2015-11-05 20:58:30 +0100 | [diff] [blame] | 352 | /* |
| 353 | * After the switch command, wait for 8 clocks before the next |
| 354 | * command |
| 355 | */ |
| 356 | if (cmd->cmdidx == MMC_CMD_SWITCH) |
| 357 | udelay(8*1000000 / priv->curr_clk); /* 8 clk in us */ |
| 358 | |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 362 | #ifdef CONFIG_DM_MMC |
| 363 | static int atmel_mci_set_ios(struct udevice *dev) |
| 364 | { |
| 365 | struct atmel_mci_priv *priv = dev_get_priv(dev); |
| 366 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 367 | #else |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 368 | /* Entered into mmc structure during driver init */ |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 369 | static int mci_set_ios(struct mmc *mmc) |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 370 | { |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 371 | struct atmel_mci_priv *priv = mmc->priv; |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 372 | #endif |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 373 | atmel_mci_t *mci = priv->mci; |
Bo Shen | 644b476 | 2013-04-26 00:27:06 +0000 | [diff] [blame] | 374 | int bus_width = mmc->bus_width; |
| 375 | unsigned int version = atmel_mci_get_version(mci); |
| 376 | int busw; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 377 | |
| 378 | /* Set the clock speed */ |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 379 | #ifdef CONFIG_DM_MMC |
| 380 | mci_set_mode(priv, mmc->clock, MMC_DEFAULT_BLKLEN); |
| 381 | #else |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 382 | mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN); |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 383 | #endif |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * set the bus width and select slot for this interface |
| 387 | * there is no capability for multiple slots on the same interface yet |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 388 | */ |
Bo Shen | 644b476 | 2013-04-26 00:27:06 +0000 | [diff] [blame] | 389 | if ((version & 0xf00) >= 0x300) { |
| 390 | switch (bus_width) { |
| 391 | case 8: |
| 392 | busw = 3; |
| 393 | break; |
| 394 | case 4: |
| 395 | busw = 2; |
| 396 | break; |
| 397 | default: |
| 398 | busw = 0; |
| 399 | break; |
| 400 | } |
| 401 | |
| 402 | writel(busw << 6 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); |
| 403 | } else { |
| 404 | busw = (bus_width == 4) ? 1 : 0; |
| 405 | |
| 406 | writel(busw << 7 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); |
| 407 | } |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 408 | |
| 409 | return 0; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 412 | #ifdef CONFIG_DM_MMC |
| 413 | static int atmel_mci_hw_init(struct atmel_mci_priv *priv) |
| 414 | { |
| 415 | #else |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 416 | /* Entered into mmc structure during driver init */ |
| 417 | static int mci_init(struct mmc *mmc) |
| 418 | { |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 419 | struct atmel_mci_priv *priv = mmc->priv; |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 420 | #endif |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 421 | atmel_mci_t *mci = priv->mci; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 422 | |
| 423 | /* Initialize controller */ |
| 424 | writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */ |
| 425 | writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */ |
| 426 | writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */ |
Reinhard Meyer | f267557 | 2010-11-16 09:24:41 +0100 | [diff] [blame] | 427 | writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */ |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 428 | |
Wu, Josh | ec35c91 | 2012-09-13 22:22:06 +0000 | [diff] [blame] | 429 | /* This delay can be optimized, but stick with max value */ |
| 430 | writel(0x7f, &mci->dtor); |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 431 | /* Disable Interrupts */ |
| 432 | writel(~0UL, &mci->idr); |
| 433 | |
| 434 | /* Set default clocks and blocklen */ |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 435 | #ifdef CONFIG_DM_MMC |
| 436 | mci_set_mode(priv, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN); |
| 437 | #else |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 438 | mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN); |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 439 | #endif |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 444 | #ifndef CONFIG_DM_MMC |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 445 | static const struct mmc_ops atmel_mci_ops = { |
| 446 | .send_cmd = mci_send_cmd, |
| 447 | .set_ios = mci_set_ios, |
| 448 | .init = mci_init, |
| 449 | }; |
| 450 | |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 451 | /* |
| 452 | * This is the only exported function |
| 453 | * |
| 454 | * Call it with the MCI register base address |
| 455 | */ |
| 456 | int atmel_mci_init(void *regs) |
| 457 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 458 | struct mmc *mmc; |
| 459 | struct mmc_config *cfg; |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 460 | struct atmel_mci_priv *priv; |
Bo Shen | 644b476 | 2013-04-26 00:27:06 +0000 | [diff] [blame] | 461 | unsigned int version; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 462 | |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 463 | priv = calloc(1, sizeof(*priv)); |
| 464 | if (!priv) |
| 465 | return -ENOMEM; |
Bo Shen | 644b476 | 2013-04-26 00:27:06 +0000 | [diff] [blame] | 466 | |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 467 | cfg = &priv->cfg; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 468 | |
| 469 | cfg->name = "mci"; |
| 470 | cfg->ops = &atmel_mci_ops; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 471 | |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 472 | priv->mci = (struct atmel_mci *)regs; |
Marek Vasut | 0b7d54e | 2015-10-23 20:46:31 +0200 | [diff] [blame] | 473 | priv->initialized = 0; |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 474 | |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 475 | /* need to be able to pass these in on a board by board basis */ |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 476 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 477 | version = atmel_mci_get_version(priv->mci); |
Bo Shen | 1dc856d | 2014-07-31 14:39:32 +0800 | [diff] [blame] | 478 | if ((version & 0xf00) >= 0x300) { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 479 | cfg->host_caps = MMC_MODE_8BIT; |
Bo Shen | 1dc856d | 2014-07-31 14:39:32 +0800 | [diff] [blame] | 480 | cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz; |
| 481 | } |
Bo Shen | 644b476 | 2013-04-26 00:27:06 +0000 | [diff] [blame] | 482 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 483 | cfg->host_caps |= MMC_MODE_4BIT; |
Bo Shen | 644b476 | 2013-04-26 00:27:06 +0000 | [diff] [blame] | 484 | |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 485 | /* |
| 486 | * min and max frequencies determined by |
| 487 | * max and min of clock divider |
| 488 | */ |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 489 | cfg->f_min = get_mci_clk_rate() / (2*256); |
| 490 | cfg->f_max = get_mci_clk_rate() / (2*1); |
| 491 | |
| 492 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 493 | |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 494 | mmc = mmc_create(cfg, priv); |
John Rigby | f2f4366 | 2011-04-18 05:50:08 +0000 | [diff] [blame] | 495 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 496 | if (mmc == NULL) { |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 497 | free(priv); |
| 498 | return -ENODEV; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 499 | } |
Marek Vasut | 903fc1d | 2015-10-23 20:46:30 +0200 | [diff] [blame] | 500 | /* NOTE: possibly leaking the priv structure */ |
Reinhard Meyer | c718a56 | 2010-08-13 10:31:06 +0200 | [diff] [blame] | 501 | |
| 502 | return 0; |
| 503 | } |
Wenyou Yang | 7a7a64c | 2017-04-13 10:29:22 +0800 | [diff] [blame^] | 504 | #endif |
| 505 | |
| 506 | #ifdef CONFIG_DM_MMC |
| 507 | static const struct dm_mmc_ops atmel_mci_mmc_ops = { |
| 508 | .send_cmd = atmel_mci_send_cmd, |
| 509 | .set_ios = atmel_mci_set_ios, |
| 510 | }; |
| 511 | |
| 512 | static void atmel_mci_setup_cfg(struct atmel_mci_priv *priv) |
| 513 | { |
| 514 | struct mmc_config *cfg; |
| 515 | u32 version; |
| 516 | |
| 517 | cfg = &priv->cfg; |
| 518 | cfg->name = "Atmel mci"; |
| 519 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 520 | |
| 521 | /* |
| 522 | * If the version is above 3.0, the capabilities of the 8-bit |
| 523 | * bus width and high speed are supported. |
| 524 | */ |
| 525 | version = atmel_mci_get_version(priv->mci); |
| 526 | if ((version & 0xf00) >= 0x300) { |
| 527 | cfg->host_caps = MMC_MODE_8BIT | |
| 528 | MMC_MODE_HS | MMC_MODE_HS_52MHz; |
| 529 | } |
| 530 | |
| 531 | cfg->host_caps |= MMC_MODE_4BIT; |
| 532 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 533 | cfg->f_min = priv->bus_clk_rate / (2 * 256); |
| 534 | cfg->f_max = priv->bus_clk_rate / 2; |
| 535 | } |
| 536 | |
| 537 | static int atmel_mci_enable_clk(struct udevice *dev) |
| 538 | { |
| 539 | struct atmel_mci_priv *priv = dev_get_priv(dev); |
| 540 | struct clk clk; |
| 541 | ulong clk_rate; |
| 542 | int ret = 0; |
| 543 | |
| 544 | ret = clk_get_by_index(dev, 0, &clk); |
| 545 | if (ret) { |
| 546 | ret = -EINVAL; |
| 547 | goto failed; |
| 548 | } |
| 549 | |
| 550 | ret = clk_enable(&clk); |
| 551 | if (ret) |
| 552 | goto failed; |
| 553 | |
| 554 | clk_rate = clk_get_rate(&clk); |
| 555 | if (!clk_rate) { |
| 556 | ret = -EINVAL; |
| 557 | goto failed; |
| 558 | } |
| 559 | |
| 560 | priv->bus_clk_rate = clk_rate; |
| 561 | |
| 562 | failed: |
| 563 | clk_free(&clk); |
| 564 | |
| 565 | return ret; |
| 566 | } |
| 567 | |
| 568 | static int atmel_mci_probe(struct udevice *dev) |
| 569 | { |
| 570 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 571 | struct atmel_mci_priv *priv = dev_get_priv(dev); |
| 572 | struct mmc *mmc; |
| 573 | int ret; |
| 574 | |
| 575 | ret = atmel_mci_enable_clk(dev); |
| 576 | if (ret) |
| 577 | return ret; |
| 578 | |
| 579 | priv->mci = (struct atmel_mci *)dev_get_addr_ptr(dev); |
| 580 | |
| 581 | atmel_mci_setup_cfg(priv); |
| 582 | |
| 583 | mmc = &priv->mmc; |
| 584 | mmc->cfg = &priv->cfg; |
| 585 | mmc->dev = dev; |
| 586 | upriv->mmc = mmc; |
| 587 | |
| 588 | atmel_mci_hw_init(priv); |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int atmel_mci_bind(struct udevice *dev) |
| 594 | { |
| 595 | struct atmel_mci_priv *priv = dev_get_priv(dev); |
| 596 | |
| 597 | return mmc_bind(dev, &priv->mmc, &priv->cfg); |
| 598 | } |
| 599 | |
| 600 | static const struct udevice_id atmel_mci_ids[] = { |
| 601 | { .compatible = "atmel,hsmci" }, |
| 602 | { } |
| 603 | }; |
| 604 | |
| 605 | U_BOOT_DRIVER(atmel_mci) = { |
| 606 | .name = "atmel-mci", |
| 607 | .id = UCLASS_MMC, |
| 608 | .of_match = atmel_mci_ids, |
| 609 | .bind = atmel_mci_bind, |
| 610 | .probe = atmel_mci_probe, |
| 611 | .priv_auto_alloc_size = sizeof(struct atmel_mci_priv), |
| 612 | .ops = &atmel_mci_mmc_ops, |
| 613 | }; |
| 614 | #endif |