1a111bfbfSMasahiro Yamada /* 2a111bfbfSMasahiro Yamada * Copyright (C) 2016 Masahiro Yamada <yamada.masahiro@socionext.com> 3a111bfbfSMasahiro Yamada * 4a111bfbfSMasahiro Yamada * SPDX-License-Identifier: GPL-2.0+ 5a111bfbfSMasahiro Yamada */ 6a111bfbfSMasahiro Yamada 7a111bfbfSMasahiro Yamada #include <common.h> 8a111bfbfSMasahiro Yamada #include <clk.h> 9a111bfbfSMasahiro Yamada #include <fdtdec.h> 10a111bfbfSMasahiro Yamada #include <mapmem.h> 11a111bfbfSMasahiro Yamada #include <mmc.h> 12a111bfbfSMasahiro Yamada #include <dm/device.h> 13a111bfbfSMasahiro Yamada #include <linux/compat.h> 14a111bfbfSMasahiro Yamada #include <linux/io.h> 15*4f80501bSMasahiro Yamada #include <linux/sizes.h> 16a111bfbfSMasahiro Yamada #include <asm/unaligned.h> 17a111bfbfSMasahiro Yamada #include <asm/dma-mapping.h> 18a111bfbfSMasahiro Yamada 19a111bfbfSMasahiro Yamada DECLARE_GLOBAL_DATA_PTR; 20a111bfbfSMasahiro Yamada 21a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD 0x000 /* command */ 22a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD_NOSTOP BIT(14) /* No automatic CMD12 issue */ 23a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD_MULTI BIT(13) /* multiple block transfer */ 24a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD_RD BIT(12) /* 1: read, 0: write */ 25a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD_DATA BIT(11) /* data transfer */ 26a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD_APP BIT(6) /* ACMD preceded by CMD55 */ 27a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD_NORMAL (0 << 8)/* auto-detect of resp-type */ 28a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD_RSP_NONE (3 << 8)/* response: none */ 29a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD_RSP_R1 (4 << 8)/* response: R1, R5, R6, R7 */ 30a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD_RSP_R1B (5 << 8)/* response: R1b, R5b */ 31a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD_RSP_R2 (6 << 8)/* response: R2 */ 32a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CMD_RSP_R3 (7 << 8)/* response: R3, R4 */ 33a111bfbfSMasahiro Yamada #define UNIPHIER_SD_ARG 0x008 /* command argument */ 34a111bfbfSMasahiro Yamada #define UNIPHIER_SD_STOP 0x010 /* stop action control */ 35a111bfbfSMasahiro Yamada #define UNIPHIER_SD_STOP_SEC BIT(8) /* use sector count */ 36a111bfbfSMasahiro Yamada #define UNIPHIER_SD_STOP_STP BIT(0) /* issue CMD12 */ 37a111bfbfSMasahiro Yamada #define UNIPHIER_SD_SECCNT 0x014 /* sector counter */ 38a111bfbfSMasahiro Yamada #define UNIPHIER_SD_RSP10 0x018 /* response[39:8] */ 39a111bfbfSMasahiro Yamada #define UNIPHIER_SD_RSP32 0x020 /* response[71:40] */ 40a111bfbfSMasahiro Yamada #define UNIPHIER_SD_RSP54 0x028 /* response[103:72] */ 41a111bfbfSMasahiro Yamada #define UNIPHIER_SD_RSP76 0x030 /* response[127:104] */ 42a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO1 0x038 /* IRQ status 1 */ 43a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO1_CD BIT(5) /* state of card detect */ 44a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO1_INSERT BIT(4) /* card inserted */ 45a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO1_REMOVE BIT(3) /* card removed */ 46a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO1_CMP BIT(2) /* data complete */ 47a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO1_RSP BIT(0) /* response complete */ 48a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2 0x03c /* IRQ status 2 */ 49a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_ERR_ILA BIT(15) /* illegal access err */ 50a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_CBSY BIT(14) /* command busy */ 51a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_BWE BIT(9) /* write buffer ready */ 52a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_BRE BIT(8) /* read buffer ready */ 53a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_DAT0 BIT(7) /* SDDAT0 */ 54a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_ERR_RTO BIT(6) /* response time out */ 55a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_ERR_ILR BIT(5) /* illegal read err */ 56a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_ERR_ILW BIT(4) /* illegal write err */ 57a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_ERR_TO BIT(3) /* time out error */ 58a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_ERR_END BIT(2) /* END bit error */ 59a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_ERR_CRC BIT(1) /* CRC error */ 60a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_ERR_IDX BIT(0) /* cmd index error */ 61a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO1_MASK 0x040 62a111bfbfSMasahiro Yamada #define UNIPHIER_SD_INFO2_MASK 0x044 63a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL 0x048 /* clock divisor */ 64a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV_MASK 0x104ff 65a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV1024 BIT(16) /* SDCLK = CLK / 1024 */ 66a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV512 BIT(7) /* SDCLK = CLK / 512 */ 67a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV256 BIT(6) /* SDCLK = CLK / 256 */ 68a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV128 BIT(5) /* SDCLK = CLK / 128 */ 69a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV64 BIT(4) /* SDCLK = CLK / 64 */ 70a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV32 BIT(3) /* SDCLK = CLK / 32 */ 71a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV16 BIT(2) /* SDCLK = CLK / 16 */ 72a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV8 BIT(1) /* SDCLK = CLK / 8 */ 73a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV4 BIT(0) /* SDCLK = CLK / 4 */ 74a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV2 0 /* SDCLK = CLK / 2 */ 75a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_DIV1 BIT(10) /* SDCLK = CLK */ 76a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_OFFEN BIT(9) /* stop SDCLK when unused */ 77a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CLKCTL_SCLKEN BIT(8) /* SDCLK output enable */ 78a111bfbfSMasahiro Yamada #define UNIPHIER_SD_SIZE 0x04c /* block size */ 79a111bfbfSMasahiro Yamada #define UNIPHIER_SD_OPTION 0x050 80a111bfbfSMasahiro Yamada #define UNIPHIER_SD_OPTION_WIDTH_MASK (5 << 13) 81a111bfbfSMasahiro Yamada #define UNIPHIER_SD_OPTION_WIDTH_1 (4 << 13) 82a111bfbfSMasahiro Yamada #define UNIPHIER_SD_OPTION_WIDTH_4 (0 << 13) 83a111bfbfSMasahiro Yamada #define UNIPHIER_SD_OPTION_WIDTH_8 (1 << 13) 84a111bfbfSMasahiro Yamada #define UNIPHIER_SD_BUF 0x060 /* read/write buffer */ 85a111bfbfSMasahiro Yamada #define UNIPHIER_SD_EXTMODE 0x1b0 86a111bfbfSMasahiro Yamada #define UNIPHIER_SD_EXTMODE_DMA_EN BIT(1) /* transfer 1: DMA, 0: pio */ 87a111bfbfSMasahiro Yamada #define UNIPHIER_SD_SOFT_RST 0x1c0 88a111bfbfSMasahiro Yamada #define UNIPHIER_SD_SOFT_RST_RSTX BIT(0) /* reset deassert */ 89a111bfbfSMasahiro Yamada #define UNIPHIER_SD_VERSION 0x1c4 /* version register */ 90a111bfbfSMasahiro Yamada #define UNIPHIER_SD_VERSION_IP 0xff /* IP version */ 91a111bfbfSMasahiro Yamada #define UNIPHIER_SD_HOST_MODE 0x1c8 92a111bfbfSMasahiro Yamada #define UNIPHIER_SD_IF_MODE 0x1cc 93a111bfbfSMasahiro Yamada #define UNIPHIER_SD_IF_MODE_DDR BIT(0) /* DDR mode */ 94a111bfbfSMasahiro Yamada #define UNIPHIER_SD_VOLT 0x1e4 /* voltage switch */ 95a111bfbfSMasahiro Yamada #define UNIPHIER_SD_VOLT_MASK (3 << 0) 96a111bfbfSMasahiro Yamada #define UNIPHIER_SD_VOLT_OFF (0 << 0) 97a111bfbfSMasahiro Yamada #define UNIPHIER_SD_VOLT_330 (1 << 0)/* 3.3V signal */ 98a111bfbfSMasahiro Yamada #define UNIPHIER_SD_VOLT_180 (2 << 0)/* 1.8V signal */ 99a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_MODE 0x410 100a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_MODE_DIR_RD BIT(16) /* 1: from device, 0: to dev */ 101a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_MODE_ADDR_INC BIT(0) /* 1: address inc, 0: fixed */ 102a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_CTL 0x414 103a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_CTL_START BIT(0) /* start DMA (auto cleared) */ 104a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_RST 0x418 105a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_RST_RD BIT(9) 106a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_RST_WR BIT(8) 107a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_INFO1 0x420 108a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_INFO1_END_RD2 BIT(20) /* DMA from device is complete*/ 109a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_INFO1_END_RD BIT(17) /* Don't use! Hardware bug */ 110a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_INFO1_END_WR BIT(16) /* DMA to device is complete */ 111a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_INFO1_MASK 0x424 112a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_INFO2 0x428 113a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_INFO2_ERR_RD BIT(17) 114a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_INFO2_ERR_WR BIT(16) 115a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_INFO2_MASK 0x42c 116a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_ADDR_L 0x440 117a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_ADDR_H 0x444 118a111bfbfSMasahiro Yamada 119a111bfbfSMasahiro Yamada /* alignment required by the DMA engine of this controller */ 120a111bfbfSMasahiro Yamada #define UNIPHIER_SD_DMA_MINALIGN 0x10 121a111bfbfSMasahiro Yamada 122a111bfbfSMasahiro Yamada struct uniphier_sd_priv { 123a111bfbfSMasahiro Yamada struct mmc_config cfg; 124a111bfbfSMasahiro Yamada struct mmc *mmc; 125a111bfbfSMasahiro Yamada struct udevice *dev; 126a111bfbfSMasahiro Yamada void __iomem *regbase; 127a111bfbfSMasahiro Yamada unsigned long mclk; 128a111bfbfSMasahiro Yamada unsigned int version; 129a111bfbfSMasahiro Yamada u32 caps; 130a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CAP_NONREMOVABLE BIT(0) /* Nonremovable e.g. eMMC */ 131a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CAP_DMA_INTERNAL BIT(1) /* have internal DMA engine */ 132a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CAP_DIV1024 BIT(2) /* divisor 1024 is available */ 133a111bfbfSMasahiro Yamada }; 134a111bfbfSMasahiro Yamada 135a111bfbfSMasahiro Yamada static dma_addr_t __dma_map_single(void *ptr, size_t size, 136a111bfbfSMasahiro Yamada enum dma_data_direction dir) 137a111bfbfSMasahiro Yamada { 138a111bfbfSMasahiro Yamada unsigned long addr = (unsigned long)ptr; 139a111bfbfSMasahiro Yamada 140a111bfbfSMasahiro Yamada if (dir == DMA_FROM_DEVICE) 141a111bfbfSMasahiro Yamada invalidate_dcache_range(addr, addr + size); 142a111bfbfSMasahiro Yamada else 143a111bfbfSMasahiro Yamada flush_dcache_range(addr, addr + size); 144a111bfbfSMasahiro Yamada 145a111bfbfSMasahiro Yamada return addr; 146a111bfbfSMasahiro Yamada } 147a111bfbfSMasahiro Yamada 148a111bfbfSMasahiro Yamada static void __dma_unmap_single(dma_addr_t addr, size_t size, 149a111bfbfSMasahiro Yamada enum dma_data_direction dir) 150a111bfbfSMasahiro Yamada { 151a111bfbfSMasahiro Yamada if (dir != DMA_TO_DEVICE) 152a111bfbfSMasahiro Yamada invalidate_dcache_range(addr, addr + size); 153a111bfbfSMasahiro Yamada } 154a111bfbfSMasahiro Yamada 155a111bfbfSMasahiro Yamada static int uniphier_sd_check_error(struct uniphier_sd_priv *priv) 156a111bfbfSMasahiro Yamada { 157a111bfbfSMasahiro Yamada u32 info2 = readl(priv->regbase + UNIPHIER_SD_INFO2); 158a111bfbfSMasahiro Yamada 159a111bfbfSMasahiro Yamada if (info2 & UNIPHIER_SD_INFO2_ERR_RTO) { 160a111bfbfSMasahiro Yamada /* 161a111bfbfSMasahiro Yamada * TIMEOUT must be returned for unsupported command. Do not 162a111bfbfSMasahiro Yamada * display error log since this might be a part of sequence to 163a111bfbfSMasahiro Yamada * distinguish between SD and MMC. 164a111bfbfSMasahiro Yamada */ 165a111bfbfSMasahiro Yamada return TIMEOUT; 166a111bfbfSMasahiro Yamada } 167a111bfbfSMasahiro Yamada 168a111bfbfSMasahiro Yamada if (info2 & UNIPHIER_SD_INFO2_ERR_TO) { 169a111bfbfSMasahiro Yamada dev_err(priv->dev, "timeout error\n"); 170a111bfbfSMasahiro Yamada return -ETIMEDOUT; 171a111bfbfSMasahiro Yamada } 172a111bfbfSMasahiro Yamada 173a111bfbfSMasahiro Yamada if (info2 & (UNIPHIER_SD_INFO2_ERR_END | UNIPHIER_SD_INFO2_ERR_CRC | 174a111bfbfSMasahiro Yamada UNIPHIER_SD_INFO2_ERR_IDX)) { 175a111bfbfSMasahiro Yamada dev_err(priv->dev, "communication out of sync\n"); 176a111bfbfSMasahiro Yamada return -EILSEQ; 177a111bfbfSMasahiro Yamada } 178a111bfbfSMasahiro Yamada 179a111bfbfSMasahiro Yamada if (info2 & (UNIPHIER_SD_INFO2_ERR_ILA | UNIPHIER_SD_INFO2_ERR_ILR | 180a111bfbfSMasahiro Yamada UNIPHIER_SD_INFO2_ERR_ILW)) { 181a111bfbfSMasahiro Yamada dev_err(priv->dev, "illegal access\n"); 182a111bfbfSMasahiro Yamada return -EIO; 183a111bfbfSMasahiro Yamada } 184a111bfbfSMasahiro Yamada 185a111bfbfSMasahiro Yamada return 0; 186a111bfbfSMasahiro Yamada } 187a111bfbfSMasahiro Yamada 188a111bfbfSMasahiro Yamada static int uniphier_sd_wait_for_irq(struct uniphier_sd_priv *priv, 189a111bfbfSMasahiro Yamada unsigned int reg, u32 flag) 190a111bfbfSMasahiro Yamada { 191a111bfbfSMasahiro Yamada long wait = 1000000; 192a111bfbfSMasahiro Yamada int ret; 193a111bfbfSMasahiro Yamada 194a111bfbfSMasahiro Yamada while (!(readl(priv->regbase + reg) & flag)) { 195a111bfbfSMasahiro Yamada if (wait-- < 0) { 196a111bfbfSMasahiro Yamada dev_err(priv->dev, "timeout\n"); 197a111bfbfSMasahiro Yamada return -ETIMEDOUT; 198a111bfbfSMasahiro Yamada } 199a111bfbfSMasahiro Yamada 200a111bfbfSMasahiro Yamada ret = uniphier_sd_check_error(priv); 201a111bfbfSMasahiro Yamada if (ret) 202a111bfbfSMasahiro Yamada return ret; 203a111bfbfSMasahiro Yamada 204a111bfbfSMasahiro Yamada udelay(1); 205a111bfbfSMasahiro Yamada } 206a111bfbfSMasahiro Yamada 207a111bfbfSMasahiro Yamada return 0; 208a111bfbfSMasahiro Yamada } 209a111bfbfSMasahiro Yamada 210a111bfbfSMasahiro Yamada static int uniphier_sd_pio_read_one_block(struct mmc *mmc, u32 **pbuf, 211a111bfbfSMasahiro Yamada uint blocksize) 212a111bfbfSMasahiro Yamada { 213a111bfbfSMasahiro Yamada struct uniphier_sd_priv *priv = mmc->priv; 214a111bfbfSMasahiro Yamada int i, ret; 215a111bfbfSMasahiro Yamada 216a111bfbfSMasahiro Yamada /* wait until the buffer is filled with data */ 217a111bfbfSMasahiro Yamada ret = uniphier_sd_wait_for_irq(priv, UNIPHIER_SD_INFO2, 218a111bfbfSMasahiro Yamada UNIPHIER_SD_INFO2_BRE); 219a111bfbfSMasahiro Yamada if (ret) 220a111bfbfSMasahiro Yamada return ret; 221a111bfbfSMasahiro Yamada 222a111bfbfSMasahiro Yamada /* 223a111bfbfSMasahiro Yamada * Clear the status flag _before_ read the buffer out because 224a111bfbfSMasahiro Yamada * UNIPHIER_SD_INFO2_BRE is edge-triggered, not level-triggered. 225a111bfbfSMasahiro Yamada */ 226a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_INFO2); 227a111bfbfSMasahiro Yamada 228a111bfbfSMasahiro Yamada if (likely(IS_ALIGNED((unsigned long)*pbuf, 4))) { 229a111bfbfSMasahiro Yamada for (i = 0; i < blocksize / 4; i++) 230a111bfbfSMasahiro Yamada *(*pbuf)++ = readl(priv->regbase + UNIPHIER_SD_BUF); 231a111bfbfSMasahiro Yamada } else { 232a111bfbfSMasahiro Yamada for (i = 0; i < blocksize / 4; i++) 233a111bfbfSMasahiro Yamada put_unaligned(readl(priv->regbase + UNIPHIER_SD_BUF), 234a111bfbfSMasahiro Yamada (*pbuf)++); 235a111bfbfSMasahiro Yamada } 236a111bfbfSMasahiro Yamada 237a111bfbfSMasahiro Yamada return 0; 238a111bfbfSMasahiro Yamada } 239a111bfbfSMasahiro Yamada 240a111bfbfSMasahiro Yamada static int uniphier_sd_pio_write_one_block(struct mmc *mmc, const u32 **pbuf, 241a111bfbfSMasahiro Yamada uint blocksize) 242a111bfbfSMasahiro Yamada { 243a111bfbfSMasahiro Yamada struct uniphier_sd_priv *priv = mmc->priv; 244a111bfbfSMasahiro Yamada int i, ret; 245a111bfbfSMasahiro Yamada 246a111bfbfSMasahiro Yamada /* wait until the buffer becomes empty */ 247a111bfbfSMasahiro Yamada ret = uniphier_sd_wait_for_irq(priv, UNIPHIER_SD_INFO2, 248a111bfbfSMasahiro Yamada UNIPHIER_SD_INFO2_BWE); 249a111bfbfSMasahiro Yamada if (ret) 250a111bfbfSMasahiro Yamada return ret; 251a111bfbfSMasahiro Yamada 252a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_INFO2); 253a111bfbfSMasahiro Yamada 254a111bfbfSMasahiro Yamada if (likely(IS_ALIGNED((unsigned long)*pbuf, 4))) { 255a111bfbfSMasahiro Yamada for (i = 0; i < blocksize / 4; i++) 256a111bfbfSMasahiro Yamada writel(*(*pbuf)++, priv->regbase + UNIPHIER_SD_BUF); 257a111bfbfSMasahiro Yamada } else { 258a111bfbfSMasahiro Yamada for (i = 0; i < blocksize / 4; i++) 259a111bfbfSMasahiro Yamada writel(get_unaligned((*pbuf)++), 260a111bfbfSMasahiro Yamada priv->regbase + UNIPHIER_SD_BUF); 261a111bfbfSMasahiro Yamada } 262a111bfbfSMasahiro Yamada 263a111bfbfSMasahiro Yamada return 0; 264a111bfbfSMasahiro Yamada } 265a111bfbfSMasahiro Yamada 266a111bfbfSMasahiro Yamada static int uniphier_sd_pio_xfer(struct mmc *mmc, struct mmc_data *data) 267a111bfbfSMasahiro Yamada { 268a111bfbfSMasahiro Yamada u32 *dest = (u32 *)data->dest; 269a111bfbfSMasahiro Yamada const u32 *src = (const u32 *)data->src; 270a111bfbfSMasahiro Yamada int i, ret; 271a111bfbfSMasahiro Yamada 272a111bfbfSMasahiro Yamada for (i = 0; i < data->blocks; i++) { 273a111bfbfSMasahiro Yamada if (data->flags & MMC_DATA_READ) 274a111bfbfSMasahiro Yamada ret = uniphier_sd_pio_read_one_block(mmc, &dest, 275a111bfbfSMasahiro Yamada data->blocksize); 276a111bfbfSMasahiro Yamada else 277a111bfbfSMasahiro Yamada ret = uniphier_sd_pio_write_one_block(mmc, &src, 278a111bfbfSMasahiro Yamada data->blocksize); 279a111bfbfSMasahiro Yamada if (ret) 280a111bfbfSMasahiro Yamada return ret; 281a111bfbfSMasahiro Yamada } 282a111bfbfSMasahiro Yamada 283a111bfbfSMasahiro Yamada return 0; 284a111bfbfSMasahiro Yamada } 285a111bfbfSMasahiro Yamada 286a111bfbfSMasahiro Yamada static void uniphier_sd_dma_start(struct uniphier_sd_priv *priv, 287a111bfbfSMasahiro Yamada dma_addr_t dma_addr) 288a111bfbfSMasahiro Yamada { 289a111bfbfSMasahiro Yamada u32 tmp; 290a111bfbfSMasahiro Yamada 291a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_DMA_INFO1); 292a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_DMA_INFO2); 293a111bfbfSMasahiro Yamada 294a111bfbfSMasahiro Yamada /* enable DMA */ 295a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_EXTMODE); 296a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_EXTMODE_DMA_EN; 297a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_EXTMODE); 298a111bfbfSMasahiro Yamada 299a111bfbfSMasahiro Yamada writel(dma_addr & U32_MAX, priv->regbase + UNIPHIER_SD_DMA_ADDR_L); 300a111bfbfSMasahiro Yamada 301a111bfbfSMasahiro Yamada /* suppress the warning "right shift count >= width of type" */ 302a111bfbfSMasahiro Yamada dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr)); 303a111bfbfSMasahiro Yamada 304a111bfbfSMasahiro Yamada writel(dma_addr & U32_MAX, priv->regbase + UNIPHIER_SD_DMA_ADDR_H); 305a111bfbfSMasahiro Yamada 306a111bfbfSMasahiro Yamada writel(UNIPHIER_SD_DMA_CTL_START, priv->regbase + UNIPHIER_SD_DMA_CTL); 307a111bfbfSMasahiro Yamada } 308a111bfbfSMasahiro Yamada 309a111bfbfSMasahiro Yamada static int uniphier_sd_dma_wait_for_irq(struct uniphier_sd_priv *priv, u32 flag, 310a111bfbfSMasahiro Yamada unsigned int blocks) 311a111bfbfSMasahiro Yamada { 312a111bfbfSMasahiro Yamada long wait = 1000000 + 10 * blocks; 313a111bfbfSMasahiro Yamada 314a111bfbfSMasahiro Yamada while (!(readl(priv->regbase + UNIPHIER_SD_DMA_INFO1) & flag)) { 315a111bfbfSMasahiro Yamada if (wait-- < 0) { 316a111bfbfSMasahiro Yamada dev_err(priv->dev, "timeout during DMA\n"); 317a111bfbfSMasahiro Yamada return -ETIMEDOUT; 318a111bfbfSMasahiro Yamada } 319a111bfbfSMasahiro Yamada 320a111bfbfSMasahiro Yamada udelay(10); 321a111bfbfSMasahiro Yamada } 322a111bfbfSMasahiro Yamada 323a111bfbfSMasahiro Yamada if (readl(priv->regbase + UNIPHIER_SD_DMA_INFO2)) { 324a111bfbfSMasahiro Yamada dev_err(priv->dev, "error during DMA\n"); 325a111bfbfSMasahiro Yamada return -EIO; 326a111bfbfSMasahiro Yamada } 327a111bfbfSMasahiro Yamada 328a111bfbfSMasahiro Yamada return 0; 329a111bfbfSMasahiro Yamada } 330a111bfbfSMasahiro Yamada 331a111bfbfSMasahiro Yamada static int uniphier_sd_dma_xfer(struct mmc *mmc, struct mmc_data *data) 332a111bfbfSMasahiro Yamada { 333a111bfbfSMasahiro Yamada struct uniphier_sd_priv *priv = mmc->priv; 334a111bfbfSMasahiro Yamada size_t len = data->blocks * data->blocksize; 335a111bfbfSMasahiro Yamada void *buf; 336a111bfbfSMasahiro Yamada enum dma_data_direction dir; 337a111bfbfSMasahiro Yamada dma_addr_t dma_addr; 338a111bfbfSMasahiro Yamada u32 poll_flag, tmp; 339a111bfbfSMasahiro Yamada int ret; 340a111bfbfSMasahiro Yamada 341a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_DMA_MODE); 342a111bfbfSMasahiro Yamada 343a111bfbfSMasahiro Yamada if (data->flags & MMC_DATA_READ) { 344a111bfbfSMasahiro Yamada buf = data->dest; 345a111bfbfSMasahiro Yamada dir = DMA_FROM_DEVICE; 346a111bfbfSMasahiro Yamada poll_flag = UNIPHIER_SD_DMA_INFO1_END_RD2; 347a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_DMA_MODE_DIR_RD; 348a111bfbfSMasahiro Yamada } else { 349a111bfbfSMasahiro Yamada buf = (void *)data->src; 350a111bfbfSMasahiro Yamada dir = DMA_TO_DEVICE; 351a111bfbfSMasahiro Yamada poll_flag = UNIPHIER_SD_DMA_INFO1_END_WR; 352a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_DMA_MODE_DIR_RD; 353a111bfbfSMasahiro Yamada } 354a111bfbfSMasahiro Yamada 355a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_DMA_MODE); 356a111bfbfSMasahiro Yamada 357a111bfbfSMasahiro Yamada dma_addr = __dma_map_single(buf, len, dir); 358a111bfbfSMasahiro Yamada 359a111bfbfSMasahiro Yamada uniphier_sd_dma_start(priv, dma_addr); 360a111bfbfSMasahiro Yamada 361a111bfbfSMasahiro Yamada ret = uniphier_sd_dma_wait_for_irq(priv, poll_flag, data->blocks); 362a111bfbfSMasahiro Yamada 363a111bfbfSMasahiro Yamada __dma_unmap_single(dma_addr, len, dir); 364a111bfbfSMasahiro Yamada 365a111bfbfSMasahiro Yamada return ret; 366a111bfbfSMasahiro Yamada } 367a111bfbfSMasahiro Yamada 368a111bfbfSMasahiro Yamada /* check if the address is DMA'able */ 369a111bfbfSMasahiro Yamada static bool uniphier_sd_addr_is_dmaable(unsigned long addr) 370a111bfbfSMasahiro Yamada { 371a111bfbfSMasahiro Yamada if (!IS_ALIGNED(addr, UNIPHIER_SD_DMA_MINALIGN)) 372a111bfbfSMasahiro Yamada return false; 373a111bfbfSMasahiro Yamada 374a111bfbfSMasahiro Yamada #if defined(CONFIG_ARCH_UNIPHIER) && !defined(CONFIG_ARM64) && \ 375a111bfbfSMasahiro Yamada defined(CONFIG_SPL_BUILD) 376a111bfbfSMasahiro Yamada /* 377a111bfbfSMasahiro Yamada * For UniPhier ARMv7 SoCs, the stack is allocated in the locked ways 378a111bfbfSMasahiro Yamada * of L2, which is unreachable from the DMA engine. 379a111bfbfSMasahiro Yamada */ 380a111bfbfSMasahiro Yamada if (addr < CONFIG_SPL_STACK) 381a111bfbfSMasahiro Yamada return false; 382a111bfbfSMasahiro Yamada #endif 383a111bfbfSMasahiro Yamada 384a111bfbfSMasahiro Yamada return true; 385a111bfbfSMasahiro Yamada } 386a111bfbfSMasahiro Yamada 387a111bfbfSMasahiro Yamada static int uniphier_sd_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, 388a111bfbfSMasahiro Yamada struct mmc_data *data) 389a111bfbfSMasahiro Yamada { 390a111bfbfSMasahiro Yamada struct uniphier_sd_priv *priv = mmc->priv; 391a111bfbfSMasahiro Yamada int ret; 392a111bfbfSMasahiro Yamada u32 tmp; 393a111bfbfSMasahiro Yamada 394a111bfbfSMasahiro Yamada if (readl(priv->regbase + UNIPHIER_SD_INFO2) & UNIPHIER_SD_INFO2_CBSY) { 395a111bfbfSMasahiro Yamada dev_err(priv->dev, "command busy\n"); 396a111bfbfSMasahiro Yamada return -EBUSY; 397a111bfbfSMasahiro Yamada } 398a111bfbfSMasahiro Yamada 399a111bfbfSMasahiro Yamada /* clear all status flags */ 400a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_INFO1); 401a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_INFO2); 402a111bfbfSMasahiro Yamada 403a111bfbfSMasahiro Yamada /* disable DMA once */ 404a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_EXTMODE); 405a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_EXTMODE_DMA_EN; 406a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_EXTMODE); 407a111bfbfSMasahiro Yamada 408a111bfbfSMasahiro Yamada writel(cmd->cmdarg, priv->regbase + UNIPHIER_SD_ARG); 409a111bfbfSMasahiro Yamada 410a111bfbfSMasahiro Yamada tmp = cmd->cmdidx; 411a111bfbfSMasahiro Yamada 412a111bfbfSMasahiro Yamada if (data) { 413a111bfbfSMasahiro Yamada writel(data->blocksize, priv->regbase + UNIPHIER_SD_SIZE); 414a111bfbfSMasahiro Yamada writel(data->blocks, priv->regbase + UNIPHIER_SD_SECCNT); 415a111bfbfSMasahiro Yamada 416a111bfbfSMasahiro Yamada /* Do not send CMD12 automatically */ 417a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_NOSTOP | UNIPHIER_SD_CMD_DATA; 418a111bfbfSMasahiro Yamada 419a111bfbfSMasahiro Yamada if (data->blocks > 1) 420a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_MULTI; 421a111bfbfSMasahiro Yamada 422a111bfbfSMasahiro Yamada if (data->flags & MMC_DATA_READ) 423a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RD; 424a111bfbfSMasahiro Yamada } 425a111bfbfSMasahiro Yamada 426a111bfbfSMasahiro Yamada /* 427a111bfbfSMasahiro Yamada * Do not use the response type auto-detection on this hardware. 428a111bfbfSMasahiro Yamada * CMD8, for example, has different response types on SD and eMMC, 429a111bfbfSMasahiro Yamada * while this controller always assumes the response type for SD. 430a111bfbfSMasahiro Yamada * Set the response type manually. 431a111bfbfSMasahiro Yamada */ 432a111bfbfSMasahiro Yamada switch (cmd->resp_type) { 433a111bfbfSMasahiro Yamada case MMC_RSP_NONE: 434a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RSP_NONE; 435a111bfbfSMasahiro Yamada break; 436a111bfbfSMasahiro Yamada case MMC_RSP_R1: 437a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RSP_R1; 438a111bfbfSMasahiro Yamada break; 439a111bfbfSMasahiro Yamada case MMC_RSP_R1b: 440a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RSP_R1B; 441a111bfbfSMasahiro Yamada break; 442a111bfbfSMasahiro Yamada case MMC_RSP_R2: 443a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RSP_R2; 444a111bfbfSMasahiro Yamada break; 445a111bfbfSMasahiro Yamada case MMC_RSP_R3: 446a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RSP_R3; 447a111bfbfSMasahiro Yamada break; 448a111bfbfSMasahiro Yamada default: 449a111bfbfSMasahiro Yamada dev_err(priv->dev, "unknown response type\n"); 450a111bfbfSMasahiro Yamada return -EINVAL; 451a111bfbfSMasahiro Yamada } 452a111bfbfSMasahiro Yamada 453a111bfbfSMasahiro Yamada dev_dbg(priv->dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n", 454a111bfbfSMasahiro Yamada cmd->cmdidx, tmp, cmd->cmdarg); 455a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_CMD); 456a111bfbfSMasahiro Yamada 457a111bfbfSMasahiro Yamada ret = uniphier_sd_wait_for_irq(priv, UNIPHIER_SD_INFO1, 458a111bfbfSMasahiro Yamada UNIPHIER_SD_INFO1_RSP); 459a111bfbfSMasahiro Yamada if (ret) 460a111bfbfSMasahiro Yamada return ret; 461a111bfbfSMasahiro Yamada 462a111bfbfSMasahiro Yamada if (cmd->resp_type & MMC_RSP_136) { 463a111bfbfSMasahiro Yamada u32 rsp_127_104 = readl(priv->regbase + UNIPHIER_SD_RSP76); 464a111bfbfSMasahiro Yamada u32 rsp_103_72 = readl(priv->regbase + UNIPHIER_SD_RSP54); 465a111bfbfSMasahiro Yamada u32 rsp_71_40 = readl(priv->regbase + UNIPHIER_SD_RSP32); 466a111bfbfSMasahiro Yamada u32 rsp_39_8 = readl(priv->regbase + UNIPHIER_SD_RSP10); 467a111bfbfSMasahiro Yamada 468a111bfbfSMasahiro Yamada cmd->response[0] = (rsp_127_104 & 0xffffff) << 8 | 469a111bfbfSMasahiro Yamada (rsp_103_72 & 0xff); 470a111bfbfSMasahiro Yamada cmd->response[1] = (rsp_103_72 & 0xffffff) << 8 | 471a111bfbfSMasahiro Yamada (rsp_71_40 & 0xff); 472a111bfbfSMasahiro Yamada cmd->response[2] = (rsp_71_40 & 0xffffff) << 8 | 473a111bfbfSMasahiro Yamada (rsp_39_8 & 0xff); 474a111bfbfSMasahiro Yamada cmd->response[3] = (rsp_39_8 & 0xffffff) << 8; 475a111bfbfSMasahiro Yamada } else { 476a111bfbfSMasahiro Yamada /* bit 39-8 */ 477a111bfbfSMasahiro Yamada cmd->response[0] = readl(priv->regbase + UNIPHIER_SD_RSP10); 478a111bfbfSMasahiro Yamada } 479a111bfbfSMasahiro Yamada 480a111bfbfSMasahiro Yamada if (data) { 481a111bfbfSMasahiro Yamada /* use DMA if the HW supports it and the buffer is aligned */ 482a111bfbfSMasahiro Yamada if (priv->caps & UNIPHIER_SD_CAP_DMA_INTERNAL && 483a111bfbfSMasahiro Yamada uniphier_sd_addr_is_dmaable((long)data->src)) 484a111bfbfSMasahiro Yamada ret = uniphier_sd_dma_xfer(mmc, data); 485a111bfbfSMasahiro Yamada else 486a111bfbfSMasahiro Yamada ret = uniphier_sd_pio_xfer(mmc, data); 487a111bfbfSMasahiro Yamada 488a111bfbfSMasahiro Yamada ret = uniphier_sd_wait_for_irq(priv, UNIPHIER_SD_INFO1, 489a111bfbfSMasahiro Yamada UNIPHIER_SD_INFO1_CMP); 490a111bfbfSMasahiro Yamada if (ret) 491a111bfbfSMasahiro Yamada return ret; 492a111bfbfSMasahiro Yamada } 493a111bfbfSMasahiro Yamada 494a111bfbfSMasahiro Yamada return ret; 495a111bfbfSMasahiro Yamada } 496a111bfbfSMasahiro Yamada 497a111bfbfSMasahiro Yamada static void uniphier_sd_set_bus_width(struct uniphier_sd_priv *priv, 498a111bfbfSMasahiro Yamada struct mmc *mmc) 499a111bfbfSMasahiro Yamada { 500a111bfbfSMasahiro Yamada u32 val, tmp; 501a111bfbfSMasahiro Yamada 502a111bfbfSMasahiro Yamada switch (mmc->bus_width) { 503a111bfbfSMasahiro Yamada case 1: 504a111bfbfSMasahiro Yamada val = UNIPHIER_SD_OPTION_WIDTH_1; 505a111bfbfSMasahiro Yamada break; 506a111bfbfSMasahiro Yamada case 4: 507a111bfbfSMasahiro Yamada val = UNIPHIER_SD_OPTION_WIDTH_4; 508a111bfbfSMasahiro Yamada break; 509a111bfbfSMasahiro Yamada case 8: 510a111bfbfSMasahiro Yamada val = UNIPHIER_SD_OPTION_WIDTH_8; 511a111bfbfSMasahiro Yamada break; 512a111bfbfSMasahiro Yamada default: 513a111bfbfSMasahiro Yamada BUG(); 514a111bfbfSMasahiro Yamada break; 515a111bfbfSMasahiro Yamada } 516a111bfbfSMasahiro Yamada 517a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_OPTION); 518a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_OPTION_WIDTH_MASK; 519a111bfbfSMasahiro Yamada tmp |= val; 520a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_OPTION); 521a111bfbfSMasahiro Yamada } 522a111bfbfSMasahiro Yamada 523a111bfbfSMasahiro Yamada static void uniphier_sd_set_ddr_mode(struct uniphier_sd_priv *priv, 524a111bfbfSMasahiro Yamada struct mmc *mmc) 525a111bfbfSMasahiro Yamada { 526a111bfbfSMasahiro Yamada u32 tmp; 527a111bfbfSMasahiro Yamada 528a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_IF_MODE); 529a111bfbfSMasahiro Yamada if (mmc->ddr_mode) 530a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_IF_MODE_DDR; 531a111bfbfSMasahiro Yamada else 532a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_IF_MODE_DDR; 533a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_IF_MODE); 534a111bfbfSMasahiro Yamada } 535a111bfbfSMasahiro Yamada 536a111bfbfSMasahiro Yamada static void uniphier_sd_set_clk_rate(struct uniphier_sd_priv *priv, 537a111bfbfSMasahiro Yamada struct mmc *mmc) 538a111bfbfSMasahiro Yamada { 539a111bfbfSMasahiro Yamada unsigned int divisor; 540a111bfbfSMasahiro Yamada u32 val, tmp; 541a111bfbfSMasahiro Yamada 542a111bfbfSMasahiro Yamada if (!mmc->clock) 543a111bfbfSMasahiro Yamada return; 544a111bfbfSMasahiro Yamada 545a111bfbfSMasahiro Yamada divisor = DIV_ROUND_UP(priv->mclk, mmc->clock); 546a111bfbfSMasahiro Yamada 547a111bfbfSMasahiro Yamada if (divisor <= 1) 548a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV1; 549a111bfbfSMasahiro Yamada else if (divisor <= 2) 550a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV2; 551a111bfbfSMasahiro Yamada else if (divisor <= 4) 552a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV4; 553a111bfbfSMasahiro Yamada else if (divisor <= 8) 554a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV8; 555a111bfbfSMasahiro Yamada else if (divisor <= 16) 556a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV16; 557a111bfbfSMasahiro Yamada else if (divisor <= 32) 558a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV32; 559a111bfbfSMasahiro Yamada else if (divisor <= 64) 560a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV64; 561a111bfbfSMasahiro Yamada else if (divisor <= 128) 562a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV128; 563a111bfbfSMasahiro Yamada else if (divisor <= 256) 564a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV256; 565a111bfbfSMasahiro Yamada else if (divisor <= 512 || !(priv->caps & UNIPHIER_SD_CAP_DIV1024)) 566a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV512; 567a111bfbfSMasahiro Yamada else 568a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV1024; 569a111bfbfSMasahiro Yamada 570a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_CLKCTL); 571a111bfbfSMasahiro Yamada 572a111bfbfSMasahiro Yamada /* stop the clock before changing its rate to avoid a glitch signal */ 573a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_CLKCTL_SCLKEN; 574a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_CLKCTL); 575a111bfbfSMasahiro Yamada 576a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_CLKCTL_DIV_MASK; 577a111bfbfSMasahiro Yamada tmp |= val | UNIPHIER_SD_CLKCTL_OFFEN; 578a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_CLKCTL); 579a111bfbfSMasahiro Yamada 580a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CLKCTL_SCLKEN; 581a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_CLKCTL); 582a111bfbfSMasahiro Yamada } 583a111bfbfSMasahiro Yamada 584a111bfbfSMasahiro Yamada static void uniphier_sd_set_ios(struct mmc *mmc) 585a111bfbfSMasahiro Yamada { 586a111bfbfSMasahiro Yamada struct uniphier_sd_priv *priv = mmc->priv; 587a111bfbfSMasahiro Yamada 588a111bfbfSMasahiro Yamada dev_dbg(priv->dev, "clock %uHz, DDRmode %d, width %u\n", 589a111bfbfSMasahiro Yamada mmc->clock, mmc->ddr_mode, mmc->bus_width); 590a111bfbfSMasahiro Yamada 591a111bfbfSMasahiro Yamada uniphier_sd_set_bus_width(priv, mmc); 592a111bfbfSMasahiro Yamada uniphier_sd_set_ddr_mode(priv, mmc); 593a111bfbfSMasahiro Yamada uniphier_sd_set_clk_rate(priv, mmc); 594a111bfbfSMasahiro Yamada 595a111bfbfSMasahiro Yamada udelay(1000); 596a111bfbfSMasahiro Yamada } 597a111bfbfSMasahiro Yamada 598a111bfbfSMasahiro Yamada static int uniphier_sd_init(struct mmc *mmc) 599a111bfbfSMasahiro Yamada { 600a111bfbfSMasahiro Yamada struct uniphier_sd_priv *priv = mmc->priv; 601a111bfbfSMasahiro Yamada u32 tmp; 602a111bfbfSMasahiro Yamada 603a111bfbfSMasahiro Yamada /* soft reset of the host */ 604a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_SOFT_RST); 605a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_SOFT_RST_RSTX; 606a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_SOFT_RST); 607a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_SOFT_RST_RSTX; 608a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_SOFT_RST); 609a111bfbfSMasahiro Yamada 610a111bfbfSMasahiro Yamada /* FIXME: implement eMMC hw_reset */ 611a111bfbfSMasahiro Yamada 612a111bfbfSMasahiro Yamada writel(UNIPHIER_SD_STOP_SEC, priv->regbase + UNIPHIER_SD_STOP); 613a111bfbfSMasahiro Yamada 614a111bfbfSMasahiro Yamada /* 615a111bfbfSMasahiro Yamada * Connected to 32bit AXI. 616a111bfbfSMasahiro Yamada * This register dropped backward compatibility at version 0x10. 617a111bfbfSMasahiro Yamada * Write an appropriate value depending on the IP version. 618a111bfbfSMasahiro Yamada */ 619a111bfbfSMasahiro Yamada writel(priv->version >= 0x10 ? 0x00000101 : 0x00000000, 620a111bfbfSMasahiro Yamada priv->regbase + UNIPHIER_SD_HOST_MODE); 621a111bfbfSMasahiro Yamada 622a111bfbfSMasahiro Yamada if (priv->caps & UNIPHIER_SD_CAP_DMA_INTERNAL) { 623a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_DMA_MODE); 624a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_DMA_MODE_ADDR_INC; 625a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_DMA_MODE); 626a111bfbfSMasahiro Yamada } 627a111bfbfSMasahiro Yamada 628a111bfbfSMasahiro Yamada return 0; 629a111bfbfSMasahiro Yamada } 630a111bfbfSMasahiro Yamada 631a111bfbfSMasahiro Yamada static int uniphier_sd_getcd(struct mmc *mmc) 632a111bfbfSMasahiro Yamada { 633a111bfbfSMasahiro Yamada struct uniphier_sd_priv *priv = mmc->priv; 634a111bfbfSMasahiro Yamada 635a111bfbfSMasahiro Yamada if (priv->caps & UNIPHIER_SD_CAP_NONREMOVABLE) 636a111bfbfSMasahiro Yamada return 1; 637a111bfbfSMasahiro Yamada 638a111bfbfSMasahiro Yamada return !!(readl(priv->regbase + UNIPHIER_SD_INFO1) & 639a111bfbfSMasahiro Yamada UNIPHIER_SD_INFO1_CD); 640a111bfbfSMasahiro Yamada } 641a111bfbfSMasahiro Yamada 642a111bfbfSMasahiro Yamada static const struct mmc_ops uniphier_sd_ops = { 643a111bfbfSMasahiro Yamada .send_cmd = uniphier_sd_send_cmd, 644a111bfbfSMasahiro Yamada .set_ios = uniphier_sd_set_ios, 645a111bfbfSMasahiro Yamada .init = uniphier_sd_init, 646a111bfbfSMasahiro Yamada .getcd = uniphier_sd_getcd, 647a111bfbfSMasahiro Yamada }; 648a111bfbfSMasahiro Yamada 649a111bfbfSMasahiro Yamada int uniphier_sd_probe(struct udevice *dev) 650a111bfbfSMasahiro Yamada { 651a111bfbfSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 652a111bfbfSMasahiro Yamada struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); 653a111bfbfSMasahiro Yamada fdt_addr_t base; 654a111bfbfSMasahiro Yamada struct udevice *clk_dev; 655a111bfbfSMasahiro Yamada int clk_id; 656a111bfbfSMasahiro Yamada int ret; 657a111bfbfSMasahiro Yamada 658a111bfbfSMasahiro Yamada priv->dev = dev; 659a111bfbfSMasahiro Yamada 660*4f80501bSMasahiro Yamada base = dev_get_addr(dev); 661*4f80501bSMasahiro Yamada if (base == FDT_ADDR_T_NONE) 662*4f80501bSMasahiro Yamada return -EINVAL; 663*4f80501bSMasahiro Yamada 664*4f80501bSMasahiro Yamada priv->regbase = map_sysmem(base, SZ_2K); 665a111bfbfSMasahiro Yamada if (!priv->regbase) 666a111bfbfSMasahiro Yamada return -ENOMEM; 667a111bfbfSMasahiro Yamada 668a111bfbfSMasahiro Yamada clk_id = clk_get_by_index(dev, 0, &clk_dev); 669a111bfbfSMasahiro Yamada if (clk_id < 0) { 670a111bfbfSMasahiro Yamada dev_err(dev, "failed to get host clock\n"); 671a111bfbfSMasahiro Yamada return clk_id; 672a111bfbfSMasahiro Yamada } 673a111bfbfSMasahiro Yamada 674a111bfbfSMasahiro Yamada /* set to max rate */ 675a111bfbfSMasahiro Yamada priv->mclk = clk_set_periph_rate(clk_dev, clk_id, ULONG_MAX); 676a111bfbfSMasahiro Yamada if (IS_ERR_VALUE(priv->mclk)) { 677a111bfbfSMasahiro Yamada dev_err(dev, "failed to set rate for host clock\n"); 678a111bfbfSMasahiro Yamada return priv->mclk; 679a111bfbfSMasahiro Yamada } 680a111bfbfSMasahiro Yamada 681a111bfbfSMasahiro Yamada ret = clk_enable(clk_dev, clk_id); 682a111bfbfSMasahiro Yamada if (ret) { 683a111bfbfSMasahiro Yamada dev_err(dev, "failed to enable host clock\n"); 684a111bfbfSMasahiro Yamada return ret; 685a111bfbfSMasahiro Yamada } 686a111bfbfSMasahiro Yamada 687a111bfbfSMasahiro Yamada priv->cfg.name = dev->name; 688a111bfbfSMasahiro Yamada priv->cfg.ops = &uniphier_sd_ops; 689a111bfbfSMasahiro Yamada priv->cfg.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS; 690a111bfbfSMasahiro Yamada 691a111bfbfSMasahiro Yamada switch (fdtdec_get_int(gd->fdt_blob, dev->of_offset, "bus-width", 1)) { 692a111bfbfSMasahiro Yamada case 8: 693a111bfbfSMasahiro Yamada priv->cfg.host_caps |= MMC_MODE_8BIT; 694a111bfbfSMasahiro Yamada break; 695a111bfbfSMasahiro Yamada case 4: 696a111bfbfSMasahiro Yamada priv->cfg.host_caps |= MMC_MODE_4BIT; 697a111bfbfSMasahiro Yamada break; 698a111bfbfSMasahiro Yamada case 1: 699a111bfbfSMasahiro Yamada break; 700a111bfbfSMasahiro Yamada default: 701a111bfbfSMasahiro Yamada dev_err(dev, "Invalid \"bus-width\" value\n"); 702a111bfbfSMasahiro Yamada return -EINVAL; 703a111bfbfSMasahiro Yamada } 704a111bfbfSMasahiro Yamada 705a111bfbfSMasahiro Yamada if (fdt_get_property(gd->fdt_blob, dev->of_offset, "non-removable", 706a111bfbfSMasahiro Yamada NULL)) 707a111bfbfSMasahiro Yamada priv->caps |= UNIPHIER_SD_CAP_NONREMOVABLE; 708a111bfbfSMasahiro Yamada 709a111bfbfSMasahiro Yamada priv->version = readl(priv->regbase + UNIPHIER_SD_VERSION) & 710a111bfbfSMasahiro Yamada UNIPHIER_SD_VERSION_IP; 711a111bfbfSMasahiro Yamada dev_dbg(dev, "version %x\n", priv->version); 712a111bfbfSMasahiro Yamada if (priv->version >= 0x10) { 713a111bfbfSMasahiro Yamada priv->caps |= UNIPHIER_SD_CAP_DMA_INTERNAL; 714a111bfbfSMasahiro Yamada priv->caps |= UNIPHIER_SD_CAP_DIV1024; 715a111bfbfSMasahiro Yamada } 716a111bfbfSMasahiro Yamada 717a111bfbfSMasahiro Yamada priv->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34; 718a111bfbfSMasahiro Yamada priv->cfg.f_min = priv->mclk / 719a111bfbfSMasahiro Yamada (priv->caps & UNIPHIER_SD_CAP_DIV1024 ? 1024 : 512); 720a111bfbfSMasahiro Yamada priv->cfg.f_max = priv->mclk; 721a111bfbfSMasahiro Yamada priv->cfg.b_max = U32_MAX; /* max value of UNIPHIER_SD_SECCNT */ 722a111bfbfSMasahiro Yamada 723a111bfbfSMasahiro Yamada priv->mmc = mmc_create(&priv->cfg, priv); 724a111bfbfSMasahiro Yamada if (!priv->mmc) 725a111bfbfSMasahiro Yamada return -EIO; 726a111bfbfSMasahiro Yamada 727a111bfbfSMasahiro Yamada upriv->mmc = priv->mmc; 728a111bfbfSMasahiro Yamada 729a111bfbfSMasahiro Yamada return 0; 730a111bfbfSMasahiro Yamada } 731a111bfbfSMasahiro Yamada 732a111bfbfSMasahiro Yamada int uniphier_sd_remove(struct udevice *dev) 733a111bfbfSMasahiro Yamada { 734a111bfbfSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 735a111bfbfSMasahiro Yamada 736a111bfbfSMasahiro Yamada unmap_sysmem(priv->regbase); 737a111bfbfSMasahiro Yamada mmc_destroy(priv->mmc); 738a111bfbfSMasahiro Yamada 739a111bfbfSMasahiro Yamada return 0; 740a111bfbfSMasahiro Yamada } 741a111bfbfSMasahiro Yamada 742a111bfbfSMasahiro Yamada static const struct udevice_id uniphier_sd_match[] = { 743a111bfbfSMasahiro Yamada { .compatible = "socionext,uniphier-sdhc" }, 744a111bfbfSMasahiro Yamada { /* sentinel */ } 745a111bfbfSMasahiro Yamada }; 746a111bfbfSMasahiro Yamada 747a111bfbfSMasahiro Yamada U_BOOT_DRIVER(uniphier_mmc) = { 748a111bfbfSMasahiro Yamada .name = "uniphier-mmc", 749a111bfbfSMasahiro Yamada .id = UCLASS_MMC, 750a111bfbfSMasahiro Yamada .of_match = uniphier_sd_match, 751a111bfbfSMasahiro Yamada .probe = uniphier_sd_probe, 752a111bfbfSMasahiro Yamada .remove = uniphier_sd_remove, 753a111bfbfSMasahiro Yamada .priv_auto_alloc_size = sizeof(struct uniphier_sd_priv), 754a111bfbfSMasahiro Yamada }; 755