1a111bfbfSMasahiro Yamada /* 24e3d8406SMasahiro Yamada * Copyright (C) 2016 Socionext Inc. 34e3d8406SMasahiro Yamada * Author: Masahiro Yamada <yamada.masahiro@socionext.com> 4a111bfbfSMasahiro Yamada * 5a111bfbfSMasahiro Yamada * SPDX-License-Identifier: GPL-2.0+ 6a111bfbfSMasahiro Yamada */ 7a111bfbfSMasahiro Yamada 8a111bfbfSMasahiro Yamada #include <common.h> 9a111bfbfSMasahiro Yamada #include <clk.h> 10a111bfbfSMasahiro Yamada #include <fdtdec.h> 11a111bfbfSMasahiro Yamada #include <mmc.h> 12a111bfbfSMasahiro Yamada #include <dm/device.h> 13a111bfbfSMasahiro Yamada #include <linux/compat.h> 14a111bfbfSMasahiro Yamada #include <linux/io.h> 154f80501bSMasahiro 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 void __iomem *regbase; 126a111bfbfSMasahiro Yamada unsigned long mclk; 127a111bfbfSMasahiro Yamada unsigned int version; 128a111bfbfSMasahiro Yamada u32 caps; 129a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CAP_NONREMOVABLE BIT(0) /* Nonremovable e.g. eMMC */ 130a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CAP_DMA_INTERNAL BIT(1) /* have internal DMA engine */ 131a111bfbfSMasahiro Yamada #define UNIPHIER_SD_CAP_DIV1024 BIT(2) /* divisor 1024 is available */ 132a111bfbfSMasahiro Yamada }; 133a111bfbfSMasahiro Yamada 134a111bfbfSMasahiro Yamada static dma_addr_t __dma_map_single(void *ptr, size_t size, 135a111bfbfSMasahiro Yamada enum dma_data_direction dir) 136a111bfbfSMasahiro Yamada { 137a111bfbfSMasahiro Yamada unsigned long addr = (unsigned long)ptr; 138a111bfbfSMasahiro Yamada 139a111bfbfSMasahiro Yamada if (dir == DMA_FROM_DEVICE) 140a111bfbfSMasahiro Yamada invalidate_dcache_range(addr, addr + size); 141a111bfbfSMasahiro Yamada else 142a111bfbfSMasahiro Yamada flush_dcache_range(addr, addr + size); 143a111bfbfSMasahiro Yamada 144a111bfbfSMasahiro Yamada return addr; 145a111bfbfSMasahiro Yamada } 146a111bfbfSMasahiro Yamada 147a111bfbfSMasahiro Yamada static void __dma_unmap_single(dma_addr_t addr, size_t size, 148a111bfbfSMasahiro Yamada enum dma_data_direction dir) 149a111bfbfSMasahiro Yamada { 150a111bfbfSMasahiro Yamada if (dir != DMA_TO_DEVICE) 151a111bfbfSMasahiro Yamada invalidate_dcache_range(addr, addr + size); 152a111bfbfSMasahiro Yamada } 153a111bfbfSMasahiro Yamada 1543937404fSMasahiro Yamada static int uniphier_sd_check_error(struct udevice *dev) 155a111bfbfSMasahiro Yamada { 1563937404fSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 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 */ 165915ffa52SJaehoon Chung return -ETIMEDOUT; 166a111bfbfSMasahiro Yamada } 167a111bfbfSMasahiro Yamada 168a111bfbfSMasahiro Yamada if (info2 & UNIPHIER_SD_INFO2_ERR_TO) { 1693937404fSMasahiro Yamada dev_err(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)) { 1753937404fSMasahiro Yamada dev_err(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)) { 1813937404fSMasahiro Yamada dev_err(dev, "illegal access\n"); 182a111bfbfSMasahiro Yamada return -EIO; 183a111bfbfSMasahiro Yamada } 184a111bfbfSMasahiro Yamada 185a111bfbfSMasahiro Yamada return 0; 186a111bfbfSMasahiro Yamada } 187a111bfbfSMasahiro Yamada 1883937404fSMasahiro Yamada static int uniphier_sd_wait_for_irq(struct udevice *dev, unsigned int reg, 1893937404fSMasahiro Yamada u32 flag) 190a111bfbfSMasahiro Yamada { 1913937404fSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 192a111bfbfSMasahiro Yamada long wait = 1000000; 193a111bfbfSMasahiro Yamada int ret; 194a111bfbfSMasahiro Yamada 195a111bfbfSMasahiro Yamada while (!(readl(priv->regbase + reg) & flag)) { 196a111bfbfSMasahiro Yamada if (wait-- < 0) { 1973937404fSMasahiro Yamada dev_err(dev, "timeout\n"); 198a111bfbfSMasahiro Yamada return -ETIMEDOUT; 199a111bfbfSMasahiro Yamada } 200a111bfbfSMasahiro Yamada 2013937404fSMasahiro Yamada ret = uniphier_sd_check_error(dev); 202a111bfbfSMasahiro Yamada if (ret) 203a111bfbfSMasahiro Yamada return ret; 204a111bfbfSMasahiro Yamada 205a111bfbfSMasahiro Yamada udelay(1); 206a111bfbfSMasahiro Yamada } 207a111bfbfSMasahiro Yamada 208a111bfbfSMasahiro Yamada return 0; 209a111bfbfSMasahiro Yamada } 210a111bfbfSMasahiro Yamada 2113937404fSMasahiro Yamada static int uniphier_sd_pio_read_one_block(struct udevice *dev, u32 **pbuf, 212a111bfbfSMasahiro Yamada uint blocksize) 213a111bfbfSMasahiro Yamada { 2143937404fSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 215a111bfbfSMasahiro Yamada int i, ret; 216a111bfbfSMasahiro Yamada 217a111bfbfSMasahiro Yamada /* wait until the buffer is filled with data */ 2183937404fSMasahiro Yamada ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO2, 219a111bfbfSMasahiro Yamada UNIPHIER_SD_INFO2_BRE); 220a111bfbfSMasahiro Yamada if (ret) 221a111bfbfSMasahiro Yamada return ret; 222a111bfbfSMasahiro Yamada 223a111bfbfSMasahiro Yamada /* 224a111bfbfSMasahiro Yamada * Clear the status flag _before_ read the buffer out because 225a111bfbfSMasahiro Yamada * UNIPHIER_SD_INFO2_BRE is edge-triggered, not level-triggered. 226a111bfbfSMasahiro Yamada */ 227a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_INFO2); 228a111bfbfSMasahiro Yamada 229a111bfbfSMasahiro Yamada if (likely(IS_ALIGNED((unsigned long)*pbuf, 4))) { 230a111bfbfSMasahiro Yamada for (i = 0; i < blocksize / 4; i++) 231a111bfbfSMasahiro Yamada *(*pbuf)++ = readl(priv->regbase + UNIPHIER_SD_BUF); 232a111bfbfSMasahiro Yamada } else { 233a111bfbfSMasahiro Yamada for (i = 0; i < blocksize / 4; i++) 234a111bfbfSMasahiro Yamada put_unaligned(readl(priv->regbase + UNIPHIER_SD_BUF), 235a111bfbfSMasahiro Yamada (*pbuf)++); 236a111bfbfSMasahiro Yamada } 237a111bfbfSMasahiro Yamada 238a111bfbfSMasahiro Yamada return 0; 239a111bfbfSMasahiro Yamada } 240a111bfbfSMasahiro Yamada 2413937404fSMasahiro Yamada static int uniphier_sd_pio_write_one_block(struct udevice *dev, 2423937404fSMasahiro Yamada const u32 **pbuf, uint blocksize) 243a111bfbfSMasahiro Yamada { 2443937404fSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 245a111bfbfSMasahiro Yamada int i, ret; 246a111bfbfSMasahiro Yamada 247a111bfbfSMasahiro Yamada /* wait until the buffer becomes empty */ 2483937404fSMasahiro Yamada ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO2, 249a111bfbfSMasahiro Yamada UNIPHIER_SD_INFO2_BWE); 250a111bfbfSMasahiro Yamada if (ret) 251a111bfbfSMasahiro Yamada return ret; 252a111bfbfSMasahiro Yamada 253a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_INFO2); 254a111bfbfSMasahiro Yamada 255a111bfbfSMasahiro Yamada if (likely(IS_ALIGNED((unsigned long)*pbuf, 4))) { 256a111bfbfSMasahiro Yamada for (i = 0; i < blocksize / 4; i++) 257a111bfbfSMasahiro Yamada writel(*(*pbuf)++, priv->regbase + UNIPHIER_SD_BUF); 258a111bfbfSMasahiro Yamada } else { 259a111bfbfSMasahiro Yamada for (i = 0; i < blocksize / 4; i++) 260a111bfbfSMasahiro Yamada writel(get_unaligned((*pbuf)++), 261a111bfbfSMasahiro Yamada priv->regbase + UNIPHIER_SD_BUF); 262a111bfbfSMasahiro Yamada } 263a111bfbfSMasahiro Yamada 264a111bfbfSMasahiro Yamada return 0; 265a111bfbfSMasahiro Yamada } 266a111bfbfSMasahiro Yamada 2673937404fSMasahiro Yamada static int uniphier_sd_pio_xfer(struct udevice *dev, struct mmc_data *data) 268a111bfbfSMasahiro Yamada { 269a111bfbfSMasahiro Yamada u32 *dest = (u32 *)data->dest; 270a111bfbfSMasahiro Yamada const u32 *src = (const u32 *)data->src; 271a111bfbfSMasahiro Yamada int i, ret; 272a111bfbfSMasahiro Yamada 273a111bfbfSMasahiro Yamada for (i = 0; i < data->blocks; i++) { 274a111bfbfSMasahiro Yamada if (data->flags & MMC_DATA_READ) 2753937404fSMasahiro Yamada ret = uniphier_sd_pio_read_one_block(dev, &dest, 276a111bfbfSMasahiro Yamada data->blocksize); 277a111bfbfSMasahiro Yamada else 2783937404fSMasahiro Yamada ret = uniphier_sd_pio_write_one_block(dev, &src, 279a111bfbfSMasahiro Yamada data->blocksize); 280a111bfbfSMasahiro Yamada if (ret) 281a111bfbfSMasahiro Yamada return ret; 282a111bfbfSMasahiro Yamada } 283a111bfbfSMasahiro Yamada 284a111bfbfSMasahiro Yamada return 0; 285a111bfbfSMasahiro Yamada } 286a111bfbfSMasahiro Yamada 287a111bfbfSMasahiro Yamada static void uniphier_sd_dma_start(struct uniphier_sd_priv *priv, 288a111bfbfSMasahiro Yamada dma_addr_t dma_addr) 289a111bfbfSMasahiro Yamada { 290a111bfbfSMasahiro Yamada u32 tmp; 291a111bfbfSMasahiro Yamada 292a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_DMA_INFO1); 293a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_DMA_INFO2); 294a111bfbfSMasahiro Yamada 295a111bfbfSMasahiro Yamada /* enable DMA */ 296a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_EXTMODE); 297a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_EXTMODE_DMA_EN; 298a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_EXTMODE); 299a111bfbfSMasahiro Yamada 300a111bfbfSMasahiro Yamada writel(dma_addr & U32_MAX, priv->regbase + UNIPHIER_SD_DMA_ADDR_L); 301a111bfbfSMasahiro Yamada 302a111bfbfSMasahiro Yamada /* suppress the warning "right shift count >= width of type" */ 303a111bfbfSMasahiro Yamada dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr)); 304a111bfbfSMasahiro Yamada 305a111bfbfSMasahiro Yamada writel(dma_addr & U32_MAX, priv->regbase + UNIPHIER_SD_DMA_ADDR_H); 306a111bfbfSMasahiro Yamada 307a111bfbfSMasahiro Yamada writel(UNIPHIER_SD_DMA_CTL_START, priv->regbase + UNIPHIER_SD_DMA_CTL); 308a111bfbfSMasahiro Yamada } 309a111bfbfSMasahiro Yamada 3103937404fSMasahiro Yamada static int uniphier_sd_dma_wait_for_irq(struct udevice *dev, u32 flag, 311a111bfbfSMasahiro Yamada unsigned int blocks) 312a111bfbfSMasahiro Yamada { 3133937404fSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 314a111bfbfSMasahiro Yamada long wait = 1000000 + 10 * blocks; 315a111bfbfSMasahiro Yamada 316a111bfbfSMasahiro Yamada while (!(readl(priv->regbase + UNIPHIER_SD_DMA_INFO1) & flag)) { 317a111bfbfSMasahiro Yamada if (wait-- < 0) { 3183937404fSMasahiro Yamada dev_err(dev, "timeout during DMA\n"); 319a111bfbfSMasahiro Yamada return -ETIMEDOUT; 320a111bfbfSMasahiro Yamada } 321a111bfbfSMasahiro Yamada 322a111bfbfSMasahiro Yamada udelay(10); 323a111bfbfSMasahiro Yamada } 324a111bfbfSMasahiro Yamada 325a111bfbfSMasahiro Yamada if (readl(priv->regbase + UNIPHIER_SD_DMA_INFO2)) { 3263937404fSMasahiro Yamada dev_err(dev, "error during DMA\n"); 327a111bfbfSMasahiro Yamada return -EIO; 328a111bfbfSMasahiro Yamada } 329a111bfbfSMasahiro Yamada 330a111bfbfSMasahiro Yamada return 0; 331a111bfbfSMasahiro Yamada } 332a111bfbfSMasahiro Yamada 3333937404fSMasahiro Yamada static int uniphier_sd_dma_xfer(struct udevice *dev, struct mmc_data *data) 334a111bfbfSMasahiro Yamada { 3353937404fSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 336a111bfbfSMasahiro Yamada size_t len = data->blocks * data->blocksize; 337a111bfbfSMasahiro Yamada void *buf; 338a111bfbfSMasahiro Yamada enum dma_data_direction dir; 339a111bfbfSMasahiro Yamada dma_addr_t dma_addr; 340a111bfbfSMasahiro Yamada u32 poll_flag, tmp; 341a111bfbfSMasahiro Yamada int ret; 342a111bfbfSMasahiro Yamada 343a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_DMA_MODE); 344a111bfbfSMasahiro Yamada 345a111bfbfSMasahiro Yamada if (data->flags & MMC_DATA_READ) { 346a111bfbfSMasahiro Yamada buf = data->dest; 347a111bfbfSMasahiro Yamada dir = DMA_FROM_DEVICE; 348a111bfbfSMasahiro Yamada poll_flag = UNIPHIER_SD_DMA_INFO1_END_RD2; 349a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_DMA_MODE_DIR_RD; 350a111bfbfSMasahiro Yamada } else { 351a111bfbfSMasahiro Yamada buf = (void *)data->src; 352a111bfbfSMasahiro Yamada dir = DMA_TO_DEVICE; 353a111bfbfSMasahiro Yamada poll_flag = UNIPHIER_SD_DMA_INFO1_END_WR; 354a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_DMA_MODE_DIR_RD; 355a111bfbfSMasahiro Yamada } 356a111bfbfSMasahiro Yamada 357a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_DMA_MODE); 358a111bfbfSMasahiro Yamada 359a111bfbfSMasahiro Yamada dma_addr = __dma_map_single(buf, len, dir); 360a111bfbfSMasahiro Yamada 361a111bfbfSMasahiro Yamada uniphier_sd_dma_start(priv, dma_addr); 362a111bfbfSMasahiro Yamada 3633937404fSMasahiro Yamada ret = uniphier_sd_dma_wait_for_irq(dev, poll_flag, data->blocks); 364a111bfbfSMasahiro Yamada 365a111bfbfSMasahiro Yamada __dma_unmap_single(dma_addr, len, dir); 366a111bfbfSMasahiro Yamada 367a111bfbfSMasahiro Yamada return ret; 368a111bfbfSMasahiro Yamada } 369a111bfbfSMasahiro Yamada 370a111bfbfSMasahiro Yamada /* check if the address is DMA'able */ 371a111bfbfSMasahiro Yamada static bool uniphier_sd_addr_is_dmaable(unsigned long addr) 372a111bfbfSMasahiro Yamada { 373a111bfbfSMasahiro Yamada if (!IS_ALIGNED(addr, UNIPHIER_SD_DMA_MINALIGN)) 374a111bfbfSMasahiro Yamada return false; 375a111bfbfSMasahiro Yamada 376a111bfbfSMasahiro Yamada #if defined(CONFIG_ARCH_UNIPHIER) && !defined(CONFIG_ARM64) && \ 377a111bfbfSMasahiro Yamada defined(CONFIG_SPL_BUILD) 378a111bfbfSMasahiro Yamada /* 379a111bfbfSMasahiro Yamada * For UniPhier ARMv7 SoCs, the stack is allocated in the locked ways 380a111bfbfSMasahiro Yamada * of L2, which is unreachable from the DMA engine. 381a111bfbfSMasahiro Yamada */ 382a111bfbfSMasahiro Yamada if (addr < CONFIG_SPL_STACK) 383a111bfbfSMasahiro Yamada return false; 384a111bfbfSMasahiro Yamada #endif 385a111bfbfSMasahiro Yamada 386a111bfbfSMasahiro Yamada return true; 387a111bfbfSMasahiro Yamada } 388a111bfbfSMasahiro Yamada 3893937404fSMasahiro Yamada static int uniphier_sd_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, 390a111bfbfSMasahiro Yamada struct mmc_data *data) 391a111bfbfSMasahiro Yamada { 3923937404fSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 393a111bfbfSMasahiro Yamada int ret; 394a111bfbfSMasahiro Yamada u32 tmp; 395a111bfbfSMasahiro Yamada 396a111bfbfSMasahiro Yamada if (readl(priv->regbase + UNIPHIER_SD_INFO2) & UNIPHIER_SD_INFO2_CBSY) { 3973937404fSMasahiro Yamada dev_err(dev, "command busy\n"); 398a111bfbfSMasahiro Yamada return -EBUSY; 399a111bfbfSMasahiro Yamada } 400a111bfbfSMasahiro Yamada 401a111bfbfSMasahiro Yamada /* clear all status flags */ 402a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_INFO1); 403a111bfbfSMasahiro Yamada writel(0, priv->regbase + UNIPHIER_SD_INFO2); 404a111bfbfSMasahiro Yamada 405a111bfbfSMasahiro Yamada /* disable DMA once */ 406a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_EXTMODE); 407a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_EXTMODE_DMA_EN; 408a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_EXTMODE); 409a111bfbfSMasahiro Yamada 410a111bfbfSMasahiro Yamada writel(cmd->cmdarg, priv->regbase + UNIPHIER_SD_ARG); 411a111bfbfSMasahiro Yamada 412a111bfbfSMasahiro Yamada tmp = cmd->cmdidx; 413a111bfbfSMasahiro Yamada 414a111bfbfSMasahiro Yamada if (data) { 415a111bfbfSMasahiro Yamada writel(data->blocksize, priv->regbase + UNIPHIER_SD_SIZE); 416a111bfbfSMasahiro Yamada writel(data->blocks, priv->regbase + UNIPHIER_SD_SECCNT); 417a111bfbfSMasahiro Yamada 418a111bfbfSMasahiro Yamada /* Do not send CMD12 automatically */ 419a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_NOSTOP | UNIPHIER_SD_CMD_DATA; 420a111bfbfSMasahiro Yamada 421a111bfbfSMasahiro Yamada if (data->blocks > 1) 422a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_MULTI; 423a111bfbfSMasahiro Yamada 424a111bfbfSMasahiro Yamada if (data->flags & MMC_DATA_READ) 425a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RD; 426a111bfbfSMasahiro Yamada } 427a111bfbfSMasahiro Yamada 428a111bfbfSMasahiro Yamada /* 429a111bfbfSMasahiro Yamada * Do not use the response type auto-detection on this hardware. 430a111bfbfSMasahiro Yamada * CMD8, for example, has different response types on SD and eMMC, 431a111bfbfSMasahiro Yamada * while this controller always assumes the response type for SD. 432a111bfbfSMasahiro Yamada * Set the response type manually. 433a111bfbfSMasahiro Yamada */ 434a111bfbfSMasahiro Yamada switch (cmd->resp_type) { 435a111bfbfSMasahiro Yamada case MMC_RSP_NONE: 436a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RSP_NONE; 437a111bfbfSMasahiro Yamada break; 438a111bfbfSMasahiro Yamada case MMC_RSP_R1: 439a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RSP_R1; 440a111bfbfSMasahiro Yamada break; 441a111bfbfSMasahiro Yamada case MMC_RSP_R1b: 442a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RSP_R1B; 443a111bfbfSMasahiro Yamada break; 444a111bfbfSMasahiro Yamada case MMC_RSP_R2: 445a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RSP_R2; 446a111bfbfSMasahiro Yamada break; 447a111bfbfSMasahiro Yamada case MMC_RSP_R3: 448a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CMD_RSP_R3; 449a111bfbfSMasahiro Yamada break; 450a111bfbfSMasahiro Yamada default: 4513937404fSMasahiro Yamada dev_err(dev, "unknown response type\n"); 452a111bfbfSMasahiro Yamada return -EINVAL; 453a111bfbfSMasahiro Yamada } 454a111bfbfSMasahiro Yamada 4553937404fSMasahiro Yamada dev_dbg(dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n", 456a111bfbfSMasahiro Yamada cmd->cmdidx, tmp, cmd->cmdarg); 457a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_CMD); 458a111bfbfSMasahiro Yamada 4593937404fSMasahiro Yamada ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO1, 460a111bfbfSMasahiro Yamada UNIPHIER_SD_INFO1_RSP); 461a111bfbfSMasahiro Yamada if (ret) 462a111bfbfSMasahiro Yamada return ret; 463a111bfbfSMasahiro Yamada 464a111bfbfSMasahiro Yamada if (cmd->resp_type & MMC_RSP_136) { 465a111bfbfSMasahiro Yamada u32 rsp_127_104 = readl(priv->regbase + UNIPHIER_SD_RSP76); 466a111bfbfSMasahiro Yamada u32 rsp_103_72 = readl(priv->regbase + UNIPHIER_SD_RSP54); 467a111bfbfSMasahiro Yamada u32 rsp_71_40 = readl(priv->regbase + UNIPHIER_SD_RSP32); 468a111bfbfSMasahiro Yamada u32 rsp_39_8 = readl(priv->regbase + UNIPHIER_SD_RSP10); 469a111bfbfSMasahiro Yamada 470a111bfbfSMasahiro Yamada cmd->response[0] = (rsp_127_104 & 0xffffff) << 8 | 471a111bfbfSMasahiro Yamada (rsp_103_72 & 0xff); 472a111bfbfSMasahiro Yamada cmd->response[1] = (rsp_103_72 & 0xffffff) << 8 | 473a111bfbfSMasahiro Yamada (rsp_71_40 & 0xff); 474a111bfbfSMasahiro Yamada cmd->response[2] = (rsp_71_40 & 0xffffff) << 8 | 475a111bfbfSMasahiro Yamada (rsp_39_8 & 0xff); 476a111bfbfSMasahiro Yamada cmd->response[3] = (rsp_39_8 & 0xffffff) << 8; 477a111bfbfSMasahiro Yamada } else { 478a111bfbfSMasahiro Yamada /* bit 39-8 */ 479a111bfbfSMasahiro Yamada cmd->response[0] = readl(priv->regbase + UNIPHIER_SD_RSP10); 480a111bfbfSMasahiro Yamada } 481a111bfbfSMasahiro Yamada 482a111bfbfSMasahiro Yamada if (data) { 483a111bfbfSMasahiro Yamada /* use DMA if the HW supports it and the buffer is aligned */ 484a111bfbfSMasahiro Yamada if (priv->caps & UNIPHIER_SD_CAP_DMA_INTERNAL && 485a111bfbfSMasahiro Yamada uniphier_sd_addr_is_dmaable((long)data->src)) 4863937404fSMasahiro Yamada ret = uniphier_sd_dma_xfer(dev, data); 487a111bfbfSMasahiro Yamada else 4883937404fSMasahiro Yamada ret = uniphier_sd_pio_xfer(dev, data); 489a111bfbfSMasahiro Yamada 4903937404fSMasahiro Yamada ret = uniphier_sd_wait_for_irq(dev, UNIPHIER_SD_INFO1, 491a111bfbfSMasahiro Yamada UNIPHIER_SD_INFO1_CMP); 492a111bfbfSMasahiro Yamada if (ret) 493a111bfbfSMasahiro Yamada return ret; 494a111bfbfSMasahiro Yamada } 495a111bfbfSMasahiro Yamada 496a111bfbfSMasahiro Yamada return ret; 497a111bfbfSMasahiro Yamada } 498a111bfbfSMasahiro Yamada 499a111bfbfSMasahiro Yamada static void uniphier_sd_set_bus_width(struct uniphier_sd_priv *priv, 500a111bfbfSMasahiro Yamada struct mmc *mmc) 501a111bfbfSMasahiro Yamada { 502a111bfbfSMasahiro Yamada u32 val, tmp; 503a111bfbfSMasahiro Yamada 504a111bfbfSMasahiro Yamada switch (mmc->bus_width) { 505a111bfbfSMasahiro Yamada case 1: 506a111bfbfSMasahiro Yamada val = UNIPHIER_SD_OPTION_WIDTH_1; 507a111bfbfSMasahiro Yamada break; 508a111bfbfSMasahiro Yamada case 4: 509a111bfbfSMasahiro Yamada val = UNIPHIER_SD_OPTION_WIDTH_4; 510a111bfbfSMasahiro Yamada break; 511a111bfbfSMasahiro Yamada case 8: 512a111bfbfSMasahiro Yamada val = UNIPHIER_SD_OPTION_WIDTH_8; 513a111bfbfSMasahiro Yamada break; 514a111bfbfSMasahiro Yamada default: 515a111bfbfSMasahiro Yamada BUG(); 516a111bfbfSMasahiro Yamada break; 517a111bfbfSMasahiro Yamada } 518a111bfbfSMasahiro Yamada 519a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_OPTION); 520a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_OPTION_WIDTH_MASK; 521a111bfbfSMasahiro Yamada tmp |= val; 522a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_OPTION); 523a111bfbfSMasahiro Yamada } 524a111bfbfSMasahiro Yamada 525a111bfbfSMasahiro Yamada static void uniphier_sd_set_ddr_mode(struct uniphier_sd_priv *priv, 526a111bfbfSMasahiro Yamada struct mmc *mmc) 527a111bfbfSMasahiro Yamada { 528a111bfbfSMasahiro Yamada u32 tmp; 529a111bfbfSMasahiro Yamada 530a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_IF_MODE); 531a111bfbfSMasahiro Yamada if (mmc->ddr_mode) 532a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_IF_MODE_DDR; 533a111bfbfSMasahiro Yamada else 534a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_IF_MODE_DDR; 535a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_IF_MODE); 536a111bfbfSMasahiro Yamada } 537a111bfbfSMasahiro Yamada 538a111bfbfSMasahiro Yamada static void uniphier_sd_set_clk_rate(struct uniphier_sd_priv *priv, 539a111bfbfSMasahiro Yamada struct mmc *mmc) 540a111bfbfSMasahiro Yamada { 541a111bfbfSMasahiro Yamada unsigned int divisor; 542a111bfbfSMasahiro Yamada u32 val, tmp; 543a111bfbfSMasahiro Yamada 544a111bfbfSMasahiro Yamada if (!mmc->clock) 545a111bfbfSMasahiro Yamada return; 546a111bfbfSMasahiro Yamada 547a111bfbfSMasahiro Yamada divisor = DIV_ROUND_UP(priv->mclk, mmc->clock); 548a111bfbfSMasahiro Yamada 549a111bfbfSMasahiro Yamada if (divisor <= 1) 550a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV1; 551a111bfbfSMasahiro Yamada else if (divisor <= 2) 552a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV2; 553a111bfbfSMasahiro Yamada else if (divisor <= 4) 554a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV4; 555a111bfbfSMasahiro Yamada else if (divisor <= 8) 556a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV8; 557a111bfbfSMasahiro Yamada else if (divisor <= 16) 558a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV16; 559a111bfbfSMasahiro Yamada else if (divisor <= 32) 560a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV32; 561a111bfbfSMasahiro Yamada else if (divisor <= 64) 562a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV64; 563a111bfbfSMasahiro Yamada else if (divisor <= 128) 564a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV128; 565a111bfbfSMasahiro Yamada else if (divisor <= 256) 566a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV256; 567a111bfbfSMasahiro Yamada else if (divisor <= 512 || !(priv->caps & UNIPHIER_SD_CAP_DIV1024)) 568a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV512; 569a111bfbfSMasahiro Yamada else 570a111bfbfSMasahiro Yamada val = UNIPHIER_SD_CLKCTL_DIV1024; 571a111bfbfSMasahiro Yamada 572a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_CLKCTL); 573a111bfbfSMasahiro Yamada 574a111bfbfSMasahiro Yamada /* stop the clock before changing its rate to avoid a glitch signal */ 575a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_CLKCTL_SCLKEN; 576a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_CLKCTL); 577a111bfbfSMasahiro Yamada 578a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_CLKCTL_DIV_MASK; 579a111bfbfSMasahiro Yamada tmp |= val | UNIPHIER_SD_CLKCTL_OFFEN; 580a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_CLKCTL); 581a111bfbfSMasahiro Yamada 582a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_CLKCTL_SCLKEN; 583a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_CLKCTL); 584a111bfbfSMasahiro Yamada } 585a111bfbfSMasahiro Yamada 5863937404fSMasahiro Yamada static int uniphier_sd_set_ios(struct udevice *dev) 587a111bfbfSMasahiro Yamada { 5883937404fSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 5893937404fSMasahiro Yamada struct mmc *mmc = mmc_get_mmc_dev(dev); 590a111bfbfSMasahiro Yamada 5913937404fSMasahiro Yamada dev_dbg(dev, "clock %uHz, DDRmode %d, width %u\n", 592a111bfbfSMasahiro Yamada mmc->clock, mmc->ddr_mode, mmc->bus_width); 593a111bfbfSMasahiro Yamada 594a111bfbfSMasahiro Yamada uniphier_sd_set_bus_width(priv, mmc); 595a111bfbfSMasahiro Yamada uniphier_sd_set_ddr_mode(priv, mmc); 596a111bfbfSMasahiro Yamada uniphier_sd_set_clk_rate(priv, mmc); 597a111bfbfSMasahiro Yamada 598a111bfbfSMasahiro Yamada udelay(1000); 5993937404fSMasahiro Yamada 6003937404fSMasahiro Yamada return 0; 601a111bfbfSMasahiro Yamada } 602a111bfbfSMasahiro Yamada 603*4eb00846SMasahiro Yamada static int uniphier_sd_get_cd(struct udevice *dev) 604*4eb00846SMasahiro Yamada { 605*4eb00846SMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 606*4eb00846SMasahiro Yamada 607*4eb00846SMasahiro Yamada if (priv->caps & UNIPHIER_SD_CAP_NONREMOVABLE) 608*4eb00846SMasahiro Yamada return 1; 609*4eb00846SMasahiro Yamada 610*4eb00846SMasahiro Yamada return !!(readl(priv->regbase + UNIPHIER_SD_INFO1) & 611*4eb00846SMasahiro Yamada UNIPHIER_SD_INFO1_CD); 612*4eb00846SMasahiro Yamada } 613*4eb00846SMasahiro Yamada 614*4eb00846SMasahiro Yamada static const struct dm_mmc_ops uniphier_sd_ops = { 615*4eb00846SMasahiro Yamada .send_cmd = uniphier_sd_send_cmd, 616*4eb00846SMasahiro Yamada .set_ios = uniphier_sd_set_ios, 617*4eb00846SMasahiro Yamada .get_cd = uniphier_sd_get_cd, 618*4eb00846SMasahiro Yamada }; 619*4eb00846SMasahiro Yamada 620*4eb00846SMasahiro Yamada static void uniphier_sd_host_init(struct uniphier_sd_priv *priv) 621a111bfbfSMasahiro Yamada { 622a111bfbfSMasahiro Yamada u32 tmp; 623a111bfbfSMasahiro Yamada 624a111bfbfSMasahiro Yamada /* soft reset of the host */ 625a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_SOFT_RST); 626a111bfbfSMasahiro Yamada tmp &= ~UNIPHIER_SD_SOFT_RST_RSTX; 627a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_SOFT_RST); 628a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_SOFT_RST_RSTX; 629a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_SOFT_RST); 630a111bfbfSMasahiro Yamada 631a111bfbfSMasahiro Yamada /* FIXME: implement eMMC hw_reset */ 632a111bfbfSMasahiro Yamada 633a111bfbfSMasahiro Yamada writel(UNIPHIER_SD_STOP_SEC, priv->regbase + UNIPHIER_SD_STOP); 634a111bfbfSMasahiro Yamada 635a111bfbfSMasahiro Yamada /* 636a111bfbfSMasahiro Yamada * Connected to 32bit AXI. 637a111bfbfSMasahiro Yamada * This register dropped backward compatibility at version 0x10. 638a111bfbfSMasahiro Yamada * Write an appropriate value depending on the IP version. 639a111bfbfSMasahiro Yamada */ 640a111bfbfSMasahiro Yamada writel(priv->version >= 0x10 ? 0x00000101 : 0x00000000, 641a111bfbfSMasahiro Yamada priv->regbase + UNIPHIER_SD_HOST_MODE); 642a111bfbfSMasahiro Yamada 643a111bfbfSMasahiro Yamada if (priv->caps & UNIPHIER_SD_CAP_DMA_INTERNAL) { 644a111bfbfSMasahiro Yamada tmp = readl(priv->regbase + UNIPHIER_SD_DMA_MODE); 645a111bfbfSMasahiro Yamada tmp |= UNIPHIER_SD_DMA_MODE_ADDR_INC; 646a111bfbfSMasahiro Yamada writel(tmp, priv->regbase + UNIPHIER_SD_DMA_MODE); 647a111bfbfSMasahiro Yamada } 648a111bfbfSMasahiro Yamada } 649a111bfbfSMasahiro Yamada 6504a70d262SMasahiro Yamada static int uniphier_sd_probe(struct udevice *dev) 651a111bfbfSMasahiro Yamada { 652a111bfbfSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 653a111bfbfSMasahiro Yamada struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); 654a111bfbfSMasahiro Yamada fdt_addr_t base; 655135aa950SStephen Warren struct clk clk; 656a111bfbfSMasahiro Yamada int ret; 657a111bfbfSMasahiro Yamada 6584f80501bSMasahiro Yamada base = dev_get_addr(dev); 6594f80501bSMasahiro Yamada if (base == FDT_ADDR_T_NONE) 6604f80501bSMasahiro Yamada return -EINVAL; 6614f80501bSMasahiro Yamada 6624e3d8406SMasahiro Yamada priv->regbase = devm_ioremap(dev, base, SZ_2K); 663a111bfbfSMasahiro Yamada if (!priv->regbase) 664a111bfbfSMasahiro Yamada return -ENOMEM; 665a111bfbfSMasahiro Yamada 666135aa950SStephen Warren ret = clk_get_by_index(dev, 0, &clk); 667135aa950SStephen Warren if (ret < 0) { 668a111bfbfSMasahiro Yamada dev_err(dev, "failed to get host clock\n"); 669135aa950SStephen Warren return ret; 670a111bfbfSMasahiro Yamada } 671a111bfbfSMasahiro Yamada 672a111bfbfSMasahiro Yamada /* set to max rate */ 673135aa950SStephen Warren priv->mclk = clk_set_rate(&clk, ULONG_MAX); 674a111bfbfSMasahiro Yamada if (IS_ERR_VALUE(priv->mclk)) { 675a111bfbfSMasahiro Yamada dev_err(dev, "failed to set rate for host clock\n"); 676135aa950SStephen Warren clk_free(&clk); 677a111bfbfSMasahiro Yamada return priv->mclk; 678a111bfbfSMasahiro Yamada } 679a111bfbfSMasahiro Yamada 680135aa950SStephen Warren ret = clk_enable(&clk); 681135aa950SStephen Warren clk_free(&clk); 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.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS; 689a111bfbfSMasahiro Yamada 690a111bfbfSMasahiro Yamada switch (fdtdec_get_int(gd->fdt_blob, dev->of_offset, "bus-width", 1)) { 691a111bfbfSMasahiro Yamada case 8: 692a111bfbfSMasahiro Yamada priv->cfg.host_caps |= MMC_MODE_8BIT; 693a111bfbfSMasahiro Yamada break; 694a111bfbfSMasahiro Yamada case 4: 695a111bfbfSMasahiro Yamada priv->cfg.host_caps |= MMC_MODE_4BIT; 696a111bfbfSMasahiro Yamada break; 697a111bfbfSMasahiro Yamada case 1: 698a111bfbfSMasahiro Yamada break; 699a111bfbfSMasahiro Yamada default: 700a111bfbfSMasahiro Yamada dev_err(dev, "Invalid \"bus-width\" value\n"); 701a111bfbfSMasahiro Yamada return -EINVAL; 702a111bfbfSMasahiro Yamada } 703a111bfbfSMasahiro Yamada 704a111bfbfSMasahiro Yamada if (fdt_get_property(gd->fdt_blob, dev->of_offset, "non-removable", 705a111bfbfSMasahiro Yamada NULL)) 706a111bfbfSMasahiro Yamada priv->caps |= UNIPHIER_SD_CAP_NONREMOVABLE; 707a111bfbfSMasahiro Yamada 708a111bfbfSMasahiro Yamada priv->version = readl(priv->regbase + UNIPHIER_SD_VERSION) & 709a111bfbfSMasahiro Yamada UNIPHIER_SD_VERSION_IP; 710a111bfbfSMasahiro Yamada dev_dbg(dev, "version %x\n", priv->version); 711a111bfbfSMasahiro Yamada if (priv->version >= 0x10) { 712a111bfbfSMasahiro Yamada priv->caps |= UNIPHIER_SD_CAP_DMA_INTERNAL; 713a111bfbfSMasahiro Yamada priv->caps |= UNIPHIER_SD_CAP_DIV1024; 714a111bfbfSMasahiro Yamada } 715a111bfbfSMasahiro Yamada 716*4eb00846SMasahiro Yamada uniphier_sd_host_init(priv); 7173937404fSMasahiro Yamada 718a111bfbfSMasahiro Yamada priv->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34; 719a111bfbfSMasahiro Yamada priv->cfg.f_min = priv->mclk / 720a111bfbfSMasahiro Yamada (priv->caps & UNIPHIER_SD_CAP_DIV1024 ? 1024 : 512); 721a111bfbfSMasahiro Yamada priv->cfg.f_max = priv->mclk; 722a111bfbfSMasahiro Yamada priv->cfg.b_max = U32_MAX; /* max value of UNIPHIER_SD_SECCNT */ 723a111bfbfSMasahiro Yamada 724a111bfbfSMasahiro Yamada priv->mmc = mmc_create(&priv->cfg, priv); 725a111bfbfSMasahiro Yamada if (!priv->mmc) 726a111bfbfSMasahiro Yamada return -EIO; 727a111bfbfSMasahiro Yamada 728a111bfbfSMasahiro Yamada upriv->mmc = priv->mmc; 729cffe5d86SSimon Glass priv->mmc->dev = dev; 730a111bfbfSMasahiro Yamada 731a111bfbfSMasahiro Yamada return 0; 732a111bfbfSMasahiro Yamada } 733a111bfbfSMasahiro Yamada 7344a70d262SMasahiro Yamada static int uniphier_sd_remove(struct udevice *dev) 735a111bfbfSMasahiro Yamada { 736a111bfbfSMasahiro Yamada struct uniphier_sd_priv *priv = dev_get_priv(dev); 737a111bfbfSMasahiro Yamada 738a111bfbfSMasahiro Yamada mmc_destroy(priv->mmc); 739a111bfbfSMasahiro Yamada 740a111bfbfSMasahiro Yamada return 0; 741a111bfbfSMasahiro Yamada } 742a111bfbfSMasahiro Yamada 743a111bfbfSMasahiro Yamada static const struct udevice_id uniphier_sd_match[] = { 744a111bfbfSMasahiro Yamada { .compatible = "socionext,uniphier-sdhc" }, 745a111bfbfSMasahiro Yamada { /* sentinel */ } 746a111bfbfSMasahiro Yamada }; 747a111bfbfSMasahiro Yamada 748a111bfbfSMasahiro Yamada U_BOOT_DRIVER(uniphier_mmc) = { 749a111bfbfSMasahiro Yamada .name = "uniphier-mmc", 750a111bfbfSMasahiro Yamada .id = UCLASS_MMC, 751a111bfbfSMasahiro Yamada .of_match = uniphier_sd_match, 752a111bfbfSMasahiro Yamada .probe = uniphier_sd_probe, 753a111bfbfSMasahiro Yamada .remove = uniphier_sd_remove, 754a111bfbfSMasahiro Yamada .priv_auto_alloc_size = sizeof(struct uniphier_sd_priv), 7553937404fSMasahiro Yamada .ops = &uniphier_sd_ops, 756a111bfbfSMasahiro Yamada }; 757