David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for sunxi SD/MMC host controllers |
| 3 | * (C) Copyright 2007-2011 Reuuimlla Technology Co., Ltd. |
| 4 | * (C) Copyright 2007-2011 Aaron Maoye <leafy.myeh@reuuimllatech.com> |
| 5 | * (C) Copyright 2013-2014 O2S GmbH <www.o2s.ch> |
| 6 | * (C) Copyright 2013-2014 David Lanzend�rfer <david.lanzendoerfer@o2s.ch> |
| 7 | * (C) Copyright 2013-2014 Hans de Goede <hdegoede@redhat.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/err.h> |
| 22 | |
| 23 | #include <linux/clk.h> |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 24 | #include <linux/gpio.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/scatterlist.h> |
| 28 | #include <linux/dma-mapping.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/reset.h> |
| 31 | |
| 32 | #include <linux/of_address.h> |
| 33 | #include <linux/of_gpio.h> |
| 34 | #include <linux/of_platform.h> |
| 35 | |
| 36 | #include <linux/mmc/host.h> |
| 37 | #include <linux/mmc/sd.h> |
| 38 | #include <linux/mmc/sdio.h> |
| 39 | #include <linux/mmc/mmc.h> |
| 40 | #include <linux/mmc/core.h> |
| 41 | #include <linux/mmc/card.h> |
| 42 | #include <linux/mmc/slot-gpio.h> |
| 43 | |
| 44 | /* register offset definitions */ |
| 45 | #define SDXC_REG_GCTRL (0x00) /* SMC Global Control Register */ |
| 46 | #define SDXC_REG_CLKCR (0x04) /* SMC Clock Control Register */ |
| 47 | #define SDXC_REG_TMOUT (0x08) /* SMC Time Out Register */ |
| 48 | #define SDXC_REG_WIDTH (0x0C) /* SMC Bus Width Register */ |
| 49 | #define SDXC_REG_BLKSZ (0x10) /* SMC Block Size Register */ |
| 50 | #define SDXC_REG_BCNTR (0x14) /* SMC Byte Count Register */ |
| 51 | #define SDXC_REG_CMDR (0x18) /* SMC Command Register */ |
| 52 | #define SDXC_REG_CARG (0x1C) /* SMC Argument Register */ |
| 53 | #define SDXC_REG_RESP0 (0x20) /* SMC Response Register 0 */ |
| 54 | #define SDXC_REG_RESP1 (0x24) /* SMC Response Register 1 */ |
| 55 | #define SDXC_REG_RESP2 (0x28) /* SMC Response Register 2 */ |
| 56 | #define SDXC_REG_RESP3 (0x2C) /* SMC Response Register 3 */ |
| 57 | #define SDXC_REG_IMASK (0x30) /* SMC Interrupt Mask Register */ |
| 58 | #define SDXC_REG_MISTA (0x34) /* SMC Masked Interrupt Status Register */ |
| 59 | #define SDXC_REG_RINTR (0x38) /* SMC Raw Interrupt Status Register */ |
| 60 | #define SDXC_REG_STAS (0x3C) /* SMC Status Register */ |
| 61 | #define SDXC_REG_FTRGL (0x40) /* SMC FIFO Threshold Watermark Registe */ |
| 62 | #define SDXC_REG_FUNS (0x44) /* SMC Function Select Register */ |
| 63 | #define SDXC_REG_CBCR (0x48) /* SMC CIU Byte Count Register */ |
| 64 | #define SDXC_REG_BBCR (0x4C) /* SMC BIU Byte Count Register */ |
| 65 | #define SDXC_REG_DBGC (0x50) /* SMC Debug Enable Register */ |
| 66 | #define SDXC_REG_HWRST (0x78) /* SMC Card Hardware Reset for Register */ |
| 67 | #define SDXC_REG_DMAC (0x80) /* SMC IDMAC Control Register */ |
| 68 | #define SDXC_REG_DLBA (0x84) /* SMC IDMAC Descriptor List Base Addre */ |
| 69 | #define SDXC_REG_IDST (0x88) /* SMC IDMAC Status Register */ |
| 70 | #define SDXC_REG_IDIE (0x8C) /* SMC IDMAC Interrupt Enable Register */ |
| 71 | #define SDXC_REG_CHDA (0x90) |
| 72 | #define SDXC_REG_CBDA (0x94) |
| 73 | |
| 74 | #define mmc_readl(host, reg) \ |
| 75 | readl((host)->reg_base + SDXC_##reg) |
| 76 | #define mmc_writel(host, reg, value) \ |
| 77 | writel((value), (host)->reg_base + SDXC_##reg) |
| 78 | |
| 79 | /* global control register bits */ |
| 80 | #define SDXC_SOFT_RESET BIT(0) |
| 81 | #define SDXC_FIFO_RESET BIT(1) |
| 82 | #define SDXC_DMA_RESET BIT(2) |
| 83 | #define SDXC_INTERRUPT_ENABLE_BIT BIT(4) |
| 84 | #define SDXC_DMA_ENABLE_BIT BIT(5) |
| 85 | #define SDXC_DEBOUNCE_ENABLE_BIT BIT(8) |
| 86 | #define SDXC_POSEDGE_LATCH_DATA BIT(9) |
| 87 | #define SDXC_DDR_MODE BIT(10) |
| 88 | #define SDXC_MEMORY_ACCESS_DONE BIT(29) |
| 89 | #define SDXC_ACCESS_DONE_DIRECT BIT(30) |
| 90 | #define SDXC_ACCESS_BY_AHB BIT(31) |
| 91 | #define SDXC_ACCESS_BY_DMA (0 << 31) |
| 92 | #define SDXC_HARDWARE_RESET \ |
| 93 | (SDXC_SOFT_RESET | SDXC_FIFO_RESET | SDXC_DMA_RESET) |
| 94 | |
| 95 | /* clock control bits */ |
| 96 | #define SDXC_CARD_CLOCK_ON BIT(16) |
| 97 | #define SDXC_LOW_POWER_ON BIT(17) |
| 98 | |
| 99 | /* bus width */ |
| 100 | #define SDXC_WIDTH1 0 |
| 101 | #define SDXC_WIDTH4 1 |
| 102 | #define SDXC_WIDTH8 2 |
| 103 | |
| 104 | /* smc command bits */ |
| 105 | #define SDXC_RESP_EXPIRE BIT(6) |
| 106 | #define SDXC_LONG_RESPONSE BIT(7) |
| 107 | #define SDXC_CHECK_RESPONSE_CRC BIT(8) |
| 108 | #define SDXC_DATA_EXPIRE BIT(9) |
| 109 | #define SDXC_WRITE BIT(10) |
| 110 | #define SDXC_SEQUENCE_MODE BIT(11) |
| 111 | #define SDXC_SEND_AUTO_STOP BIT(12) |
| 112 | #define SDXC_WAIT_PRE_OVER BIT(13) |
| 113 | #define SDXC_STOP_ABORT_CMD BIT(14) |
| 114 | #define SDXC_SEND_INIT_SEQUENCE BIT(15) |
| 115 | #define SDXC_UPCLK_ONLY BIT(21) |
| 116 | #define SDXC_READ_CEATA_DEV BIT(22) |
| 117 | #define SDXC_CCS_EXPIRE BIT(23) |
| 118 | #define SDXC_ENABLE_BIT_BOOT BIT(24) |
| 119 | #define SDXC_ALT_BOOT_OPTIONS BIT(25) |
| 120 | #define SDXC_BOOT_ACK_EXPIRE BIT(26) |
| 121 | #define SDXC_BOOT_ABORT BIT(27) |
| 122 | #define SDXC_VOLTAGE_SWITCH BIT(28) |
| 123 | #define SDXC_USE_HOLD_REGISTER BIT(29) |
| 124 | #define SDXC_START BIT(31) |
| 125 | |
| 126 | /* interrupt bits */ |
| 127 | #define SDXC_RESP_ERROR BIT(1) |
| 128 | #define SDXC_COMMAND_DONE BIT(2) |
| 129 | #define SDXC_DATA_OVER BIT(3) |
| 130 | #define SDXC_TX_DATA_REQUEST BIT(4) |
| 131 | #define SDXC_RX_DATA_REQUEST BIT(5) |
| 132 | #define SDXC_RESP_CRC_ERROR BIT(6) |
| 133 | #define SDXC_DATA_CRC_ERROR BIT(7) |
| 134 | #define SDXC_RESP_TIMEOUT BIT(8) |
| 135 | #define SDXC_DATA_TIMEOUT BIT(9) |
| 136 | #define SDXC_VOLTAGE_CHANGE_DONE BIT(10) |
| 137 | #define SDXC_FIFO_RUN_ERROR BIT(11) |
| 138 | #define SDXC_HARD_WARE_LOCKED BIT(12) |
| 139 | #define SDXC_START_BIT_ERROR BIT(13) |
| 140 | #define SDXC_AUTO_COMMAND_DONE BIT(14) |
| 141 | #define SDXC_END_BIT_ERROR BIT(15) |
| 142 | #define SDXC_SDIO_INTERRUPT BIT(16) |
| 143 | #define SDXC_CARD_INSERT BIT(30) |
| 144 | #define SDXC_CARD_REMOVE BIT(31) |
| 145 | #define SDXC_INTERRUPT_ERROR_BIT \ |
| 146 | (SDXC_RESP_ERROR | SDXC_RESP_CRC_ERROR | SDXC_DATA_CRC_ERROR | \ |
| 147 | SDXC_RESP_TIMEOUT | SDXC_DATA_TIMEOUT | SDXC_FIFO_RUN_ERROR | \ |
| 148 | SDXC_HARD_WARE_LOCKED | SDXC_START_BIT_ERROR | SDXC_END_BIT_ERROR) |
| 149 | #define SDXC_INTERRUPT_DONE_BIT \ |
| 150 | (SDXC_AUTO_COMMAND_DONE | SDXC_DATA_OVER | \ |
| 151 | SDXC_COMMAND_DONE | SDXC_VOLTAGE_CHANGE_DONE) |
| 152 | |
| 153 | /* status */ |
| 154 | #define SDXC_RXWL_FLAG BIT(0) |
| 155 | #define SDXC_TXWL_FLAG BIT(1) |
| 156 | #define SDXC_FIFO_EMPTY BIT(2) |
| 157 | #define SDXC_FIFO_FULL BIT(3) |
| 158 | #define SDXC_CARD_PRESENT BIT(8) |
| 159 | #define SDXC_CARD_DATA_BUSY BIT(9) |
| 160 | #define SDXC_DATA_FSM_BUSY BIT(10) |
| 161 | #define SDXC_DMA_REQUEST BIT(31) |
| 162 | #define SDXC_FIFO_SIZE 16 |
| 163 | |
| 164 | /* Function select */ |
| 165 | #define SDXC_CEATA_ON (0xceaa << 16) |
| 166 | #define SDXC_SEND_IRQ_RESPONSE BIT(0) |
| 167 | #define SDXC_SDIO_READ_WAIT BIT(1) |
| 168 | #define SDXC_ABORT_READ_DATA BIT(2) |
| 169 | #define SDXC_SEND_CCSD BIT(8) |
| 170 | #define SDXC_SEND_AUTO_STOPCCSD BIT(9) |
| 171 | #define SDXC_CEATA_DEV_IRQ_ENABLE BIT(10) |
| 172 | |
| 173 | /* IDMA controller bus mod bit field */ |
| 174 | #define SDXC_IDMAC_SOFT_RESET BIT(0) |
| 175 | #define SDXC_IDMAC_FIX_BURST BIT(1) |
| 176 | #define SDXC_IDMAC_IDMA_ON BIT(7) |
| 177 | #define SDXC_IDMAC_REFETCH_DES BIT(31) |
| 178 | |
| 179 | /* IDMA status bit field */ |
| 180 | #define SDXC_IDMAC_TRANSMIT_INTERRUPT BIT(0) |
| 181 | #define SDXC_IDMAC_RECEIVE_INTERRUPT BIT(1) |
| 182 | #define SDXC_IDMAC_FATAL_BUS_ERROR BIT(2) |
| 183 | #define SDXC_IDMAC_DESTINATION_INVALID BIT(4) |
| 184 | #define SDXC_IDMAC_CARD_ERROR_SUM BIT(5) |
| 185 | #define SDXC_IDMAC_NORMAL_INTERRUPT_SUM BIT(8) |
| 186 | #define SDXC_IDMAC_ABNORMAL_INTERRUPT_SUM BIT(9) |
| 187 | #define SDXC_IDMAC_HOST_ABORT_INTERRUPT BIT(10) |
| 188 | #define SDXC_IDMAC_IDLE (0 << 13) |
| 189 | #define SDXC_IDMAC_SUSPEND (1 << 13) |
| 190 | #define SDXC_IDMAC_DESC_READ (2 << 13) |
| 191 | #define SDXC_IDMAC_DESC_CHECK (3 << 13) |
| 192 | #define SDXC_IDMAC_READ_REQUEST_WAIT (4 << 13) |
| 193 | #define SDXC_IDMAC_WRITE_REQUEST_WAIT (5 << 13) |
| 194 | #define SDXC_IDMAC_READ (6 << 13) |
| 195 | #define SDXC_IDMAC_WRITE (7 << 13) |
| 196 | #define SDXC_IDMAC_DESC_CLOSE (8 << 13) |
| 197 | |
| 198 | /* |
| 199 | * If the idma-des-size-bits of property is ie 13, bufsize bits are: |
| 200 | * Bits 0-12: buf1 size |
| 201 | * Bits 13-25: buf2 size |
| 202 | * Bits 26-31: not used |
| 203 | * Since we only ever set buf1 size, we can simply store it directly. |
| 204 | */ |
| 205 | #define SDXC_IDMAC_DES0_DIC BIT(1) /* disable interrupt on completion */ |
| 206 | #define SDXC_IDMAC_DES0_LD BIT(2) /* last descriptor */ |
| 207 | #define SDXC_IDMAC_DES0_FD BIT(3) /* first descriptor */ |
| 208 | #define SDXC_IDMAC_DES0_CH BIT(4) /* chain mode */ |
| 209 | #define SDXC_IDMAC_DES0_ER BIT(5) /* end of ring */ |
| 210 | #define SDXC_IDMAC_DES0_CES BIT(30) /* card error summary */ |
| 211 | #define SDXC_IDMAC_DES0_OWN BIT(31) /* 1-idma owns it, 0-host owns it */ |
| 212 | |
| 213 | struct sunxi_idma_des { |
| 214 | u32 config; |
| 215 | u32 buf_size; |
| 216 | u32 buf_addr_ptr1; |
| 217 | u32 buf_addr_ptr2; |
| 218 | }; |
| 219 | |
| 220 | struct sunxi_mmc_host { |
| 221 | struct mmc_host *mmc; |
| 222 | struct reset_control *reset; |
| 223 | |
| 224 | /* IO mapping base */ |
| 225 | void __iomem *reg_base; |
| 226 | |
| 227 | /* clock management */ |
| 228 | struct clk *clk_ahb; |
| 229 | struct clk *clk_mmc; |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 230 | struct clk *clk_sample; |
| 231 | struct clk *clk_output; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 232 | |
| 233 | /* irq */ |
| 234 | spinlock_t lock; |
| 235 | int irq; |
| 236 | u32 int_sum; |
| 237 | u32 sdio_imask; |
| 238 | |
| 239 | /* dma */ |
| 240 | u32 idma_des_size_bits; |
| 241 | dma_addr_t sg_dma; |
| 242 | void *sg_cpu; |
| 243 | bool wait_dma; |
| 244 | |
| 245 | struct mmc_request *mrq; |
| 246 | struct mmc_request *manual_stop_mrq; |
| 247 | int ferror; |
| 248 | }; |
| 249 | |
| 250 | static int sunxi_mmc_reset_host(struct sunxi_mmc_host *host) |
| 251 | { |
| 252 | unsigned long expire = jiffies + msecs_to_jiffies(250); |
| 253 | u32 rval; |
| 254 | |
David Lanzendörfer | 0f0fcd3 | 2014-12-16 15:11:10 +0100 | [diff] [blame] | 255 | mmc_writel(host, REG_GCTRL, SDXC_HARDWARE_RESET); |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 256 | do { |
| 257 | rval = mmc_readl(host, REG_GCTRL); |
| 258 | } while (time_before(jiffies, expire) && (rval & SDXC_HARDWARE_RESET)); |
| 259 | |
| 260 | if (rval & SDXC_HARDWARE_RESET) { |
| 261 | dev_err(mmc_dev(host->mmc), "fatal err reset timeout\n"); |
| 262 | return -EIO; |
| 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int sunxi_mmc_init_host(struct mmc_host *mmc) |
| 269 | { |
| 270 | u32 rval; |
| 271 | struct sunxi_mmc_host *host = mmc_priv(mmc); |
| 272 | |
| 273 | if (sunxi_mmc_reset_host(host)) |
| 274 | return -EIO; |
| 275 | |
| 276 | mmc_writel(host, REG_FTRGL, 0x20070008); |
| 277 | mmc_writel(host, REG_TMOUT, 0xffffffff); |
| 278 | mmc_writel(host, REG_IMASK, host->sdio_imask); |
| 279 | mmc_writel(host, REG_RINTR, 0xffffffff); |
| 280 | mmc_writel(host, REG_DBGC, 0xdeb); |
| 281 | mmc_writel(host, REG_FUNS, SDXC_CEATA_ON); |
| 282 | mmc_writel(host, REG_DLBA, host->sg_dma); |
| 283 | |
| 284 | rval = mmc_readl(host, REG_GCTRL); |
| 285 | rval |= SDXC_INTERRUPT_ENABLE_BIT; |
| 286 | rval &= ~SDXC_ACCESS_DONE_DIRECT; |
| 287 | mmc_writel(host, REG_GCTRL, rval); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static void sunxi_mmc_init_idma_des(struct sunxi_mmc_host *host, |
| 293 | struct mmc_data *data) |
| 294 | { |
| 295 | struct sunxi_idma_des *pdes = (struct sunxi_idma_des *)host->sg_cpu; |
| 296 | struct sunxi_idma_des *pdes_pa = (struct sunxi_idma_des *)host->sg_dma; |
| 297 | int i, max_len = (1 << host->idma_des_size_bits); |
| 298 | |
| 299 | for (i = 0; i < data->sg_len; i++) { |
| 300 | pdes[i].config = SDXC_IDMAC_DES0_CH | SDXC_IDMAC_DES0_OWN | |
| 301 | SDXC_IDMAC_DES0_DIC; |
| 302 | |
| 303 | if (data->sg[i].length == max_len) |
| 304 | pdes[i].buf_size = 0; /* 0 == max_len */ |
| 305 | else |
| 306 | pdes[i].buf_size = data->sg[i].length; |
| 307 | |
| 308 | pdes[i].buf_addr_ptr1 = sg_dma_address(&data->sg[i]); |
| 309 | pdes[i].buf_addr_ptr2 = (u32)&pdes_pa[i + 1]; |
| 310 | } |
| 311 | |
| 312 | pdes[0].config |= SDXC_IDMAC_DES0_FD; |
Hans de Goede | e8a5904 | 2014-12-16 15:10:59 +0100 | [diff] [blame] | 313 | pdes[i - 1].config |= SDXC_IDMAC_DES0_LD | SDXC_IDMAC_DES0_ER; |
| 314 | pdes[i - 1].config &= ~SDXC_IDMAC_DES0_DIC; |
| 315 | pdes[i - 1].buf_addr_ptr2 = 0; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 316 | |
| 317 | /* |
| 318 | * Avoid the io-store starting the idmac hitting io-mem before the |
| 319 | * descriptors hit the main-mem. |
| 320 | */ |
| 321 | wmb(); |
| 322 | } |
| 323 | |
| 324 | static enum dma_data_direction sunxi_mmc_get_dma_dir(struct mmc_data *data) |
| 325 | { |
| 326 | if (data->flags & MMC_DATA_WRITE) |
| 327 | return DMA_TO_DEVICE; |
| 328 | else |
| 329 | return DMA_FROM_DEVICE; |
| 330 | } |
| 331 | |
| 332 | static int sunxi_mmc_map_dma(struct sunxi_mmc_host *host, |
| 333 | struct mmc_data *data) |
| 334 | { |
| 335 | u32 i, dma_len; |
| 336 | struct scatterlist *sg; |
| 337 | |
| 338 | dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len, |
| 339 | sunxi_mmc_get_dma_dir(data)); |
| 340 | if (dma_len == 0) { |
| 341 | dev_err(mmc_dev(host->mmc), "dma_map_sg failed\n"); |
| 342 | return -ENOMEM; |
| 343 | } |
| 344 | |
| 345 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 346 | if (sg->offset & 3 || sg->length & 3) { |
| 347 | dev_err(mmc_dev(host->mmc), |
| 348 | "unaligned scatterlist: os %x length %d\n", |
| 349 | sg->offset, sg->length); |
| 350 | return -EINVAL; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static void sunxi_mmc_start_dma(struct sunxi_mmc_host *host, |
| 358 | struct mmc_data *data) |
| 359 | { |
| 360 | u32 rval; |
| 361 | |
| 362 | sunxi_mmc_init_idma_des(host, data); |
| 363 | |
| 364 | rval = mmc_readl(host, REG_GCTRL); |
| 365 | rval |= SDXC_DMA_ENABLE_BIT; |
| 366 | mmc_writel(host, REG_GCTRL, rval); |
| 367 | rval |= SDXC_DMA_RESET; |
| 368 | mmc_writel(host, REG_GCTRL, rval); |
| 369 | |
| 370 | mmc_writel(host, REG_DMAC, SDXC_IDMAC_SOFT_RESET); |
| 371 | |
| 372 | if (!(data->flags & MMC_DATA_WRITE)) |
| 373 | mmc_writel(host, REG_IDIE, SDXC_IDMAC_RECEIVE_INTERRUPT); |
| 374 | |
| 375 | mmc_writel(host, REG_DMAC, |
| 376 | SDXC_IDMAC_FIX_BURST | SDXC_IDMAC_IDMA_ON); |
| 377 | } |
| 378 | |
| 379 | static void sunxi_mmc_send_manual_stop(struct sunxi_mmc_host *host, |
| 380 | struct mmc_request *req) |
| 381 | { |
| 382 | u32 arg, cmd_val, ri; |
| 383 | unsigned long expire = jiffies + msecs_to_jiffies(1000); |
| 384 | |
| 385 | cmd_val = SDXC_START | SDXC_RESP_EXPIRE | |
| 386 | SDXC_STOP_ABORT_CMD | SDXC_CHECK_RESPONSE_CRC; |
| 387 | |
| 388 | if (req->cmd->opcode == SD_IO_RW_EXTENDED) { |
| 389 | cmd_val |= SD_IO_RW_DIRECT; |
| 390 | arg = (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) | |
| 391 | ((req->cmd->arg >> 28) & 0x7); |
| 392 | } else { |
| 393 | cmd_val |= MMC_STOP_TRANSMISSION; |
| 394 | arg = 0; |
| 395 | } |
| 396 | |
| 397 | mmc_writel(host, REG_CARG, arg); |
| 398 | mmc_writel(host, REG_CMDR, cmd_val); |
| 399 | |
| 400 | do { |
| 401 | ri = mmc_readl(host, REG_RINTR); |
| 402 | } while (!(ri & (SDXC_COMMAND_DONE | SDXC_INTERRUPT_ERROR_BIT)) && |
| 403 | time_before(jiffies, expire)); |
| 404 | |
| 405 | if (!(ri & SDXC_COMMAND_DONE) || (ri & SDXC_INTERRUPT_ERROR_BIT)) { |
| 406 | dev_err(mmc_dev(host->mmc), "send stop command failed\n"); |
| 407 | if (req->stop) |
| 408 | req->stop->resp[0] = -ETIMEDOUT; |
| 409 | } else { |
| 410 | if (req->stop) |
| 411 | req->stop->resp[0] = mmc_readl(host, REG_RESP0); |
| 412 | } |
| 413 | |
| 414 | mmc_writel(host, REG_RINTR, 0xffff); |
| 415 | } |
| 416 | |
| 417 | static void sunxi_mmc_dump_errinfo(struct sunxi_mmc_host *host) |
| 418 | { |
| 419 | struct mmc_command *cmd = host->mrq->cmd; |
| 420 | struct mmc_data *data = host->mrq->data; |
| 421 | |
| 422 | /* For some cmds timeout is normal with sd/mmc cards */ |
| 423 | if ((host->int_sum & SDXC_INTERRUPT_ERROR_BIT) == |
| 424 | SDXC_RESP_TIMEOUT && (cmd->opcode == SD_IO_SEND_OP_COND || |
| 425 | cmd->opcode == SD_IO_RW_DIRECT)) |
| 426 | return; |
| 427 | |
| 428 | dev_err(mmc_dev(host->mmc), |
| 429 | "smc %d err, cmd %d,%s%s%s%s%s%s%s%s%s%s !!\n", |
| 430 | host->mmc->index, cmd->opcode, |
| 431 | data ? (data->flags & MMC_DATA_WRITE ? " WR" : " RD") : "", |
| 432 | host->int_sum & SDXC_RESP_ERROR ? " RE" : "", |
| 433 | host->int_sum & SDXC_RESP_CRC_ERROR ? " RCE" : "", |
| 434 | host->int_sum & SDXC_DATA_CRC_ERROR ? " DCE" : "", |
| 435 | host->int_sum & SDXC_RESP_TIMEOUT ? " RTO" : "", |
| 436 | host->int_sum & SDXC_DATA_TIMEOUT ? " DTO" : "", |
| 437 | host->int_sum & SDXC_FIFO_RUN_ERROR ? " FE" : "", |
| 438 | host->int_sum & SDXC_HARD_WARE_LOCKED ? " HL" : "", |
| 439 | host->int_sum & SDXC_START_BIT_ERROR ? " SBE" : "", |
| 440 | host->int_sum & SDXC_END_BIT_ERROR ? " EBE" : "" |
| 441 | ); |
| 442 | } |
| 443 | |
| 444 | /* Called in interrupt context! */ |
| 445 | static irqreturn_t sunxi_mmc_finalize_request(struct sunxi_mmc_host *host) |
| 446 | { |
| 447 | struct mmc_request *mrq = host->mrq; |
| 448 | struct mmc_data *data = mrq->data; |
| 449 | u32 rval; |
| 450 | |
| 451 | mmc_writel(host, REG_IMASK, host->sdio_imask); |
| 452 | mmc_writel(host, REG_IDIE, 0); |
| 453 | |
| 454 | if (host->int_sum & SDXC_INTERRUPT_ERROR_BIT) { |
| 455 | sunxi_mmc_dump_errinfo(host); |
| 456 | mrq->cmd->error = -ETIMEDOUT; |
| 457 | |
| 458 | if (data) { |
| 459 | data->error = -ETIMEDOUT; |
| 460 | host->manual_stop_mrq = mrq; |
| 461 | } |
| 462 | |
| 463 | if (mrq->stop) |
| 464 | mrq->stop->error = -ETIMEDOUT; |
| 465 | } else { |
| 466 | if (mrq->cmd->flags & MMC_RSP_136) { |
| 467 | mrq->cmd->resp[0] = mmc_readl(host, REG_RESP3); |
| 468 | mrq->cmd->resp[1] = mmc_readl(host, REG_RESP2); |
| 469 | mrq->cmd->resp[2] = mmc_readl(host, REG_RESP1); |
| 470 | mrq->cmd->resp[3] = mmc_readl(host, REG_RESP0); |
| 471 | } else { |
| 472 | mrq->cmd->resp[0] = mmc_readl(host, REG_RESP0); |
| 473 | } |
| 474 | |
| 475 | if (data) |
| 476 | data->bytes_xfered = data->blocks * data->blksz; |
| 477 | } |
| 478 | |
| 479 | if (data) { |
| 480 | mmc_writel(host, REG_IDST, 0x337); |
| 481 | mmc_writel(host, REG_DMAC, 0); |
| 482 | rval = mmc_readl(host, REG_GCTRL); |
| 483 | rval |= SDXC_DMA_RESET; |
| 484 | mmc_writel(host, REG_GCTRL, rval); |
| 485 | rval &= ~SDXC_DMA_ENABLE_BIT; |
| 486 | mmc_writel(host, REG_GCTRL, rval); |
| 487 | rval |= SDXC_FIFO_RESET; |
| 488 | mmc_writel(host, REG_GCTRL, rval); |
| 489 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, |
| 490 | sunxi_mmc_get_dma_dir(data)); |
| 491 | } |
| 492 | |
| 493 | mmc_writel(host, REG_RINTR, 0xffff); |
| 494 | |
| 495 | host->mrq = NULL; |
| 496 | host->int_sum = 0; |
| 497 | host->wait_dma = false; |
| 498 | |
| 499 | return host->manual_stop_mrq ? IRQ_WAKE_THREAD : IRQ_HANDLED; |
| 500 | } |
| 501 | |
| 502 | static irqreturn_t sunxi_mmc_irq(int irq, void *dev_id) |
| 503 | { |
| 504 | struct sunxi_mmc_host *host = dev_id; |
| 505 | struct mmc_request *mrq; |
| 506 | u32 msk_int, idma_int; |
| 507 | bool finalize = false; |
| 508 | bool sdio_int = false; |
| 509 | irqreturn_t ret = IRQ_HANDLED; |
| 510 | |
| 511 | spin_lock(&host->lock); |
| 512 | |
| 513 | idma_int = mmc_readl(host, REG_IDST); |
| 514 | msk_int = mmc_readl(host, REG_MISTA); |
| 515 | |
| 516 | dev_dbg(mmc_dev(host->mmc), "irq: rq %p mi %08x idi %08x\n", |
| 517 | host->mrq, msk_int, idma_int); |
| 518 | |
| 519 | mrq = host->mrq; |
| 520 | if (mrq) { |
| 521 | if (idma_int & SDXC_IDMAC_RECEIVE_INTERRUPT) |
| 522 | host->wait_dma = false; |
| 523 | |
| 524 | host->int_sum |= msk_int; |
| 525 | |
| 526 | /* Wait for COMMAND_DONE on RESPONSE_TIMEOUT before finalize */ |
| 527 | if ((host->int_sum & SDXC_RESP_TIMEOUT) && |
| 528 | !(host->int_sum & SDXC_COMMAND_DONE)) |
| 529 | mmc_writel(host, REG_IMASK, |
| 530 | host->sdio_imask | SDXC_COMMAND_DONE); |
| 531 | /* Don't wait for dma on error */ |
| 532 | else if (host->int_sum & SDXC_INTERRUPT_ERROR_BIT) |
| 533 | finalize = true; |
| 534 | else if ((host->int_sum & SDXC_INTERRUPT_DONE_BIT) && |
| 535 | !host->wait_dma) |
| 536 | finalize = true; |
| 537 | } |
| 538 | |
| 539 | if (msk_int & SDXC_SDIO_INTERRUPT) |
| 540 | sdio_int = true; |
| 541 | |
| 542 | mmc_writel(host, REG_RINTR, msk_int); |
| 543 | mmc_writel(host, REG_IDST, idma_int); |
| 544 | |
| 545 | if (finalize) |
| 546 | ret = sunxi_mmc_finalize_request(host); |
| 547 | |
| 548 | spin_unlock(&host->lock); |
| 549 | |
| 550 | if (finalize && ret == IRQ_HANDLED) |
| 551 | mmc_request_done(host->mmc, mrq); |
| 552 | |
| 553 | if (sdio_int) |
| 554 | mmc_signal_sdio_irq(host->mmc); |
| 555 | |
| 556 | return ret; |
| 557 | } |
| 558 | |
| 559 | static irqreturn_t sunxi_mmc_handle_manual_stop(int irq, void *dev_id) |
| 560 | { |
| 561 | struct sunxi_mmc_host *host = dev_id; |
| 562 | struct mmc_request *mrq; |
| 563 | unsigned long iflags; |
| 564 | |
| 565 | spin_lock_irqsave(&host->lock, iflags); |
| 566 | mrq = host->manual_stop_mrq; |
| 567 | spin_unlock_irqrestore(&host->lock, iflags); |
| 568 | |
| 569 | if (!mrq) { |
| 570 | dev_err(mmc_dev(host->mmc), "no request for manual stop\n"); |
| 571 | return IRQ_HANDLED; |
| 572 | } |
| 573 | |
| 574 | dev_err(mmc_dev(host->mmc), "data error, sending stop command\n"); |
David Lanzendörfer | dd9b380 | 2014-12-16 15:11:04 +0100 | [diff] [blame] | 575 | |
| 576 | /* |
| 577 | * We will never have more than one outstanding request, |
| 578 | * and we do not complete the request until after |
| 579 | * we've cleared host->manual_stop_mrq so we do not need to |
| 580 | * spin lock this function. |
| 581 | * Additionally we have wait states within this function |
| 582 | * so having it in a lock is a very bad idea. |
| 583 | */ |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 584 | sunxi_mmc_send_manual_stop(host, mrq); |
| 585 | |
| 586 | spin_lock_irqsave(&host->lock, iflags); |
| 587 | host->manual_stop_mrq = NULL; |
| 588 | spin_unlock_irqrestore(&host->lock, iflags); |
| 589 | |
| 590 | mmc_request_done(host->mmc, mrq); |
| 591 | |
| 592 | return IRQ_HANDLED; |
| 593 | } |
| 594 | |
| 595 | static int sunxi_mmc_oclk_onoff(struct sunxi_mmc_host *host, u32 oclk_en) |
| 596 | { |
| 597 | unsigned long expire = jiffies + msecs_to_jiffies(250); |
| 598 | u32 rval; |
| 599 | |
| 600 | rval = mmc_readl(host, REG_CLKCR); |
| 601 | rval &= ~(SDXC_CARD_CLOCK_ON | SDXC_LOW_POWER_ON); |
| 602 | |
| 603 | if (oclk_en) |
| 604 | rval |= SDXC_CARD_CLOCK_ON; |
| 605 | |
| 606 | mmc_writel(host, REG_CLKCR, rval); |
| 607 | |
| 608 | rval = SDXC_START | SDXC_UPCLK_ONLY | SDXC_WAIT_PRE_OVER; |
| 609 | mmc_writel(host, REG_CMDR, rval); |
| 610 | |
| 611 | do { |
| 612 | rval = mmc_readl(host, REG_CMDR); |
| 613 | } while (time_before(jiffies, expire) && (rval & SDXC_START)); |
| 614 | |
| 615 | /* clear irq status bits set by the command */ |
| 616 | mmc_writel(host, REG_RINTR, |
| 617 | mmc_readl(host, REG_RINTR) & ~SDXC_SDIO_INTERRUPT); |
| 618 | |
| 619 | if (rval & SDXC_START) { |
| 620 | dev_err(mmc_dev(host->mmc), "fatal err update clk timeout\n"); |
| 621 | return -EIO; |
| 622 | } |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static int sunxi_mmc_clk_set_rate(struct sunxi_mmc_host *host, |
| 628 | struct mmc_ios *ios) |
| 629 | { |
David Lanzendörfer | de0673c | 2014-12-16 15:11:15 +0100 | [diff] [blame] | 630 | u32 rate, oclk_dly, rval, sclk_dly; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 631 | int ret; |
| 632 | |
| 633 | rate = clk_round_rate(host->clk_mmc, ios->clock); |
| 634 | dev_dbg(mmc_dev(host->mmc), "setting clk to %d, rounded %d\n", |
| 635 | ios->clock, rate); |
| 636 | |
| 637 | /* setting clock rate */ |
| 638 | ret = clk_set_rate(host->clk_mmc, rate); |
| 639 | if (ret) { |
| 640 | dev_err(mmc_dev(host->mmc), "error setting clk to %d: %d\n", |
| 641 | rate, ret); |
| 642 | return ret; |
| 643 | } |
| 644 | |
| 645 | ret = sunxi_mmc_oclk_onoff(host, 0); |
| 646 | if (ret) |
| 647 | return ret; |
| 648 | |
| 649 | /* clear internal divider */ |
| 650 | rval = mmc_readl(host, REG_CLKCR); |
| 651 | rval &= ~0xff; |
| 652 | mmc_writel(host, REG_CLKCR, rval); |
| 653 | |
| 654 | /* determine delays */ |
| 655 | if (rate <= 400000) { |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 656 | oclk_dly = 180; |
| 657 | sclk_dly = 42; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 658 | } else if (rate <= 25000000) { |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 659 | oclk_dly = 180; |
| 660 | sclk_dly = 75; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 661 | } else if (rate <= 50000000) { |
| 662 | if (ios->timing == MMC_TIMING_UHS_DDR50) { |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 663 | oclk_dly = 60; |
| 664 | sclk_dly = 120; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 665 | } else { |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 666 | oclk_dly = 90; |
| 667 | sclk_dly = 150; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 668 | } |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 669 | } else if (rate <= 100000000) { |
| 670 | oclk_dly = 6; |
| 671 | sclk_dly = 24; |
| 672 | } else if (rate <= 200000000) { |
| 673 | oclk_dly = 3; |
| 674 | sclk_dly = 12; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 675 | } else { |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 676 | return -EINVAL; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 677 | } |
| 678 | |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 679 | clk_set_phase(host->clk_sample, sclk_dly); |
| 680 | clk_set_phase(host->clk_output, oclk_dly); |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 681 | |
| 682 | return sunxi_mmc_oclk_onoff(host, 1); |
| 683 | } |
| 684 | |
| 685 | static void sunxi_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 686 | { |
| 687 | struct sunxi_mmc_host *host = mmc_priv(mmc); |
| 688 | u32 rval; |
| 689 | |
| 690 | /* Set the power state */ |
| 691 | switch (ios->power_mode) { |
| 692 | case MMC_POWER_ON: |
| 693 | break; |
| 694 | |
| 695 | case MMC_POWER_UP: |
| 696 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd); |
| 697 | |
| 698 | host->ferror = sunxi_mmc_init_host(mmc); |
| 699 | if (host->ferror) |
| 700 | return; |
| 701 | |
| 702 | dev_dbg(mmc_dev(mmc), "power on!\n"); |
| 703 | break; |
| 704 | |
| 705 | case MMC_POWER_OFF: |
| 706 | dev_dbg(mmc_dev(mmc), "power off!\n"); |
| 707 | sunxi_mmc_reset_host(host); |
| 708 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); |
| 709 | break; |
| 710 | } |
| 711 | |
| 712 | /* set bus width */ |
| 713 | switch (ios->bus_width) { |
| 714 | case MMC_BUS_WIDTH_1: |
| 715 | mmc_writel(host, REG_WIDTH, SDXC_WIDTH1); |
| 716 | break; |
| 717 | case MMC_BUS_WIDTH_4: |
| 718 | mmc_writel(host, REG_WIDTH, SDXC_WIDTH4); |
| 719 | break; |
| 720 | case MMC_BUS_WIDTH_8: |
| 721 | mmc_writel(host, REG_WIDTH, SDXC_WIDTH8); |
| 722 | break; |
| 723 | } |
| 724 | |
| 725 | /* set ddr mode */ |
| 726 | rval = mmc_readl(host, REG_GCTRL); |
| 727 | if (ios->timing == MMC_TIMING_UHS_DDR50) |
| 728 | rval |= SDXC_DDR_MODE; |
| 729 | else |
| 730 | rval &= ~SDXC_DDR_MODE; |
| 731 | mmc_writel(host, REG_GCTRL, rval); |
| 732 | |
| 733 | /* set up clock */ |
| 734 | if (ios->clock && ios->power_mode) { |
| 735 | host->ferror = sunxi_mmc_clk_set_rate(host, ios); |
| 736 | /* Android code had a usleep_range(50000, 55000); here */ |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | static void sunxi_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 741 | { |
| 742 | struct sunxi_mmc_host *host = mmc_priv(mmc); |
| 743 | unsigned long flags; |
| 744 | u32 imask; |
| 745 | |
| 746 | spin_lock_irqsave(&host->lock, flags); |
| 747 | |
| 748 | imask = mmc_readl(host, REG_IMASK); |
| 749 | if (enable) { |
| 750 | host->sdio_imask = SDXC_SDIO_INTERRUPT; |
| 751 | imask |= SDXC_SDIO_INTERRUPT; |
| 752 | } else { |
| 753 | host->sdio_imask = 0; |
| 754 | imask &= ~SDXC_SDIO_INTERRUPT; |
| 755 | } |
| 756 | mmc_writel(host, REG_IMASK, imask); |
| 757 | spin_unlock_irqrestore(&host->lock, flags); |
| 758 | } |
| 759 | |
| 760 | static void sunxi_mmc_hw_reset(struct mmc_host *mmc) |
| 761 | { |
| 762 | struct sunxi_mmc_host *host = mmc_priv(mmc); |
| 763 | mmc_writel(host, REG_HWRST, 0); |
| 764 | udelay(10); |
| 765 | mmc_writel(host, REG_HWRST, 1); |
| 766 | udelay(300); |
| 767 | } |
| 768 | |
| 769 | static void sunxi_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 770 | { |
| 771 | struct sunxi_mmc_host *host = mmc_priv(mmc); |
| 772 | struct mmc_command *cmd = mrq->cmd; |
| 773 | struct mmc_data *data = mrq->data; |
| 774 | unsigned long iflags; |
| 775 | u32 imask = SDXC_INTERRUPT_ERROR_BIT; |
| 776 | u32 cmd_val = SDXC_START | (cmd->opcode & 0x3f); |
David Lanzendörfer | dd9b380 | 2014-12-16 15:11:04 +0100 | [diff] [blame] | 777 | bool wait_dma = host->wait_dma; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 778 | int ret; |
| 779 | |
| 780 | /* Check for set_ios errors (should never happen) */ |
| 781 | if (host->ferror) { |
| 782 | mrq->cmd->error = host->ferror; |
| 783 | mmc_request_done(mmc, mrq); |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | if (data) { |
| 788 | ret = sunxi_mmc_map_dma(host, data); |
| 789 | if (ret < 0) { |
| 790 | dev_err(mmc_dev(mmc), "map DMA failed\n"); |
| 791 | cmd->error = ret; |
| 792 | data->error = ret; |
| 793 | mmc_request_done(mmc, mrq); |
| 794 | return; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | if (cmd->opcode == MMC_GO_IDLE_STATE) { |
| 799 | cmd_val |= SDXC_SEND_INIT_SEQUENCE; |
| 800 | imask |= SDXC_COMMAND_DONE; |
| 801 | } |
| 802 | |
| 803 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 804 | cmd_val |= SDXC_RESP_EXPIRE; |
| 805 | if (cmd->flags & MMC_RSP_136) |
| 806 | cmd_val |= SDXC_LONG_RESPONSE; |
| 807 | if (cmd->flags & MMC_RSP_CRC) |
| 808 | cmd_val |= SDXC_CHECK_RESPONSE_CRC; |
| 809 | |
| 810 | if ((cmd->flags & MMC_CMD_MASK) == MMC_CMD_ADTC) { |
| 811 | cmd_val |= SDXC_DATA_EXPIRE | SDXC_WAIT_PRE_OVER; |
| 812 | if (cmd->data->flags & MMC_DATA_STREAM) { |
| 813 | imask |= SDXC_AUTO_COMMAND_DONE; |
| 814 | cmd_val |= SDXC_SEQUENCE_MODE | |
| 815 | SDXC_SEND_AUTO_STOP; |
| 816 | } |
| 817 | |
| 818 | if (cmd->data->stop) { |
| 819 | imask |= SDXC_AUTO_COMMAND_DONE; |
| 820 | cmd_val |= SDXC_SEND_AUTO_STOP; |
| 821 | } else { |
| 822 | imask |= SDXC_DATA_OVER; |
| 823 | } |
| 824 | |
| 825 | if (cmd->data->flags & MMC_DATA_WRITE) |
| 826 | cmd_val |= SDXC_WRITE; |
| 827 | else |
David Lanzendörfer | dd9b380 | 2014-12-16 15:11:04 +0100 | [diff] [blame] | 828 | wait_dma = true; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 829 | } else { |
| 830 | imask |= SDXC_COMMAND_DONE; |
| 831 | } |
| 832 | } else { |
| 833 | imask |= SDXC_COMMAND_DONE; |
| 834 | } |
| 835 | |
| 836 | dev_dbg(mmc_dev(mmc), "cmd %d(%08x) arg %x ie 0x%08x len %d\n", |
| 837 | cmd_val & 0x3f, cmd_val, cmd->arg, imask, |
| 838 | mrq->data ? mrq->data->blksz * mrq->data->blocks : 0); |
| 839 | |
| 840 | spin_lock_irqsave(&host->lock, iflags); |
| 841 | |
| 842 | if (host->mrq || host->manual_stop_mrq) { |
| 843 | spin_unlock_irqrestore(&host->lock, iflags); |
| 844 | |
| 845 | if (data) |
| 846 | dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len, |
| 847 | sunxi_mmc_get_dma_dir(data)); |
| 848 | |
| 849 | dev_err(mmc_dev(mmc), "request already pending\n"); |
| 850 | mrq->cmd->error = -EBUSY; |
| 851 | mmc_request_done(mmc, mrq); |
| 852 | return; |
| 853 | } |
| 854 | |
| 855 | if (data) { |
| 856 | mmc_writel(host, REG_BLKSZ, data->blksz); |
| 857 | mmc_writel(host, REG_BCNTR, data->blksz * data->blocks); |
| 858 | sunxi_mmc_start_dma(host, data); |
| 859 | } |
| 860 | |
| 861 | host->mrq = mrq; |
David Lanzendörfer | dd9b380 | 2014-12-16 15:11:04 +0100 | [diff] [blame] | 862 | host->wait_dma = wait_dma; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 863 | mmc_writel(host, REG_IMASK, host->sdio_imask | imask); |
| 864 | mmc_writel(host, REG_CARG, cmd->arg); |
| 865 | mmc_writel(host, REG_CMDR, cmd_val); |
| 866 | |
| 867 | spin_unlock_irqrestore(&host->lock, iflags); |
| 868 | } |
| 869 | |
| 870 | static const struct of_device_id sunxi_mmc_of_match[] = { |
| 871 | { .compatible = "allwinner,sun4i-a10-mmc", }, |
| 872 | { .compatible = "allwinner,sun5i-a13-mmc", }, |
| 873 | { /* sentinel */ } |
| 874 | }; |
| 875 | MODULE_DEVICE_TABLE(of, sunxi_mmc_of_match); |
| 876 | |
| 877 | static struct mmc_host_ops sunxi_mmc_ops = { |
| 878 | .request = sunxi_mmc_request, |
| 879 | .set_ios = sunxi_mmc_set_ios, |
| 880 | .get_ro = mmc_gpio_get_ro, |
| 881 | .get_cd = mmc_gpio_get_cd, |
| 882 | .enable_sdio_irq = sunxi_mmc_enable_sdio_irq, |
| 883 | .hw_reset = sunxi_mmc_hw_reset, |
| 884 | }; |
| 885 | |
| 886 | static int sunxi_mmc_resource_request(struct sunxi_mmc_host *host, |
| 887 | struct platform_device *pdev) |
| 888 | { |
| 889 | struct device_node *np = pdev->dev.of_node; |
| 890 | int ret; |
| 891 | |
| 892 | if (of_device_is_compatible(np, "allwinner,sun4i-a10-mmc")) |
| 893 | host->idma_des_size_bits = 13; |
| 894 | else |
| 895 | host->idma_des_size_bits = 16; |
| 896 | |
| 897 | ret = mmc_regulator_get_supply(host->mmc); |
| 898 | if (ret) { |
| 899 | if (ret != -EPROBE_DEFER) |
| 900 | dev_err(&pdev->dev, "Could not get vmmc supply\n"); |
| 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | host->reg_base = devm_ioremap_resource(&pdev->dev, |
| 905 | platform_get_resource(pdev, IORESOURCE_MEM, 0)); |
| 906 | if (IS_ERR(host->reg_base)) |
| 907 | return PTR_ERR(host->reg_base); |
| 908 | |
| 909 | host->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); |
| 910 | if (IS_ERR(host->clk_ahb)) { |
| 911 | dev_err(&pdev->dev, "Could not get ahb clock\n"); |
| 912 | return PTR_ERR(host->clk_ahb); |
| 913 | } |
| 914 | |
| 915 | host->clk_mmc = devm_clk_get(&pdev->dev, "mmc"); |
| 916 | if (IS_ERR(host->clk_mmc)) { |
| 917 | dev_err(&pdev->dev, "Could not get mmc clock\n"); |
| 918 | return PTR_ERR(host->clk_mmc); |
| 919 | } |
| 920 | |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 921 | host->clk_output = devm_clk_get(&pdev->dev, "output"); |
| 922 | if (IS_ERR(host->clk_output)) { |
| 923 | dev_err(&pdev->dev, "Could not get output clock\n"); |
| 924 | return PTR_ERR(host->clk_output); |
| 925 | } |
| 926 | |
| 927 | host->clk_sample = devm_clk_get(&pdev->dev, "sample"); |
| 928 | if (IS_ERR(host->clk_sample)) { |
| 929 | dev_err(&pdev->dev, "Could not get sample clock\n"); |
| 930 | return PTR_ERR(host->clk_sample); |
| 931 | } |
| 932 | |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 933 | host->reset = devm_reset_control_get(&pdev->dev, "ahb"); |
| 934 | |
| 935 | ret = clk_prepare_enable(host->clk_ahb); |
| 936 | if (ret) { |
| 937 | dev_err(&pdev->dev, "Enable ahb clk err %d\n", ret); |
| 938 | return ret; |
| 939 | } |
| 940 | |
| 941 | ret = clk_prepare_enable(host->clk_mmc); |
| 942 | if (ret) { |
| 943 | dev_err(&pdev->dev, "Enable mmc clk err %d\n", ret); |
| 944 | goto error_disable_clk_ahb; |
| 945 | } |
| 946 | |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 947 | ret = clk_prepare_enable(host->clk_output); |
| 948 | if (ret) { |
| 949 | dev_err(&pdev->dev, "Enable output clk err %d\n", ret); |
| 950 | goto error_disable_clk_mmc; |
| 951 | } |
| 952 | |
| 953 | ret = clk_prepare_enable(host->clk_sample); |
| 954 | if (ret) { |
| 955 | dev_err(&pdev->dev, "Enable sample clk err %d\n", ret); |
| 956 | goto error_disable_clk_output; |
| 957 | } |
| 958 | |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 959 | if (!IS_ERR(host->reset)) { |
| 960 | ret = reset_control_deassert(host->reset); |
| 961 | if (ret) { |
| 962 | dev_err(&pdev->dev, "reset err %d\n", ret); |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 963 | goto error_disable_clk_sample; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Sometimes the controller asserts the irq on boot for some reason, |
| 969 | * make sure the controller is in a sane state before enabling irqs. |
| 970 | */ |
| 971 | ret = sunxi_mmc_reset_host(host); |
| 972 | if (ret) |
| 973 | goto error_assert_reset; |
| 974 | |
| 975 | host->irq = platform_get_irq(pdev, 0); |
| 976 | return devm_request_threaded_irq(&pdev->dev, host->irq, sunxi_mmc_irq, |
| 977 | sunxi_mmc_handle_manual_stop, 0, "sunxi-mmc", host); |
| 978 | |
| 979 | error_assert_reset: |
| 980 | if (!IS_ERR(host->reset)) |
| 981 | reset_control_assert(host->reset); |
Maxime Ripard | 6c09bb8 | 2014-07-12 12:01:33 +0200 | [diff] [blame] | 982 | error_disable_clk_sample: |
| 983 | clk_disable_unprepare(host->clk_sample); |
| 984 | error_disable_clk_output: |
| 985 | clk_disable_unprepare(host->clk_output); |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 986 | error_disable_clk_mmc: |
| 987 | clk_disable_unprepare(host->clk_mmc); |
| 988 | error_disable_clk_ahb: |
| 989 | clk_disable_unprepare(host->clk_ahb); |
| 990 | return ret; |
| 991 | } |
| 992 | |
| 993 | static int sunxi_mmc_probe(struct platform_device *pdev) |
| 994 | { |
| 995 | struct sunxi_mmc_host *host; |
| 996 | struct mmc_host *mmc; |
| 997 | int ret; |
| 998 | |
| 999 | mmc = mmc_alloc_host(sizeof(struct sunxi_mmc_host), &pdev->dev); |
| 1000 | if (!mmc) { |
| 1001 | dev_err(&pdev->dev, "mmc alloc host failed\n"); |
| 1002 | return -ENOMEM; |
| 1003 | } |
| 1004 | |
| 1005 | host = mmc_priv(mmc); |
| 1006 | host->mmc = mmc; |
| 1007 | spin_lock_init(&host->lock); |
| 1008 | |
| 1009 | ret = sunxi_mmc_resource_request(host, pdev); |
| 1010 | if (ret) |
| 1011 | goto error_free_host; |
| 1012 | |
| 1013 | host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, |
| 1014 | &host->sg_dma, GFP_KERNEL); |
| 1015 | if (!host->sg_cpu) { |
| 1016 | dev_err(&pdev->dev, "Failed to allocate DMA descriptor mem\n"); |
| 1017 | ret = -ENOMEM; |
| 1018 | goto error_free_host; |
| 1019 | } |
| 1020 | |
| 1021 | mmc->ops = &sunxi_mmc_ops; |
| 1022 | mmc->max_blk_count = 8192; |
| 1023 | mmc->max_blk_size = 4096; |
| 1024 | mmc->max_segs = PAGE_SIZE / sizeof(struct sunxi_idma_des); |
| 1025 | mmc->max_seg_size = (1 << host->idma_des_size_bits); |
| 1026 | mmc->max_req_size = mmc->max_seg_size * mmc->max_segs; |
| 1027 | /* 400kHz ~ 50MHz */ |
| 1028 | mmc->f_min = 400000; |
| 1029 | mmc->f_max = 50000000; |
Chen-Yu Tsai | 3df01a9 | 2014-08-20 21:39:20 +0800 | [diff] [blame] | 1030 | mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED | |
| 1031 | MMC_CAP_ERASE; |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 1032 | |
| 1033 | ret = mmc_of_parse(mmc); |
| 1034 | if (ret) |
| 1035 | goto error_free_dma; |
| 1036 | |
| 1037 | ret = mmc_add_host(mmc); |
| 1038 | if (ret) |
| 1039 | goto error_free_dma; |
| 1040 | |
| 1041 | dev_info(&pdev->dev, "base:0x%p irq:%u\n", host->reg_base, host->irq); |
| 1042 | platform_set_drvdata(pdev, mmc); |
| 1043 | return 0; |
| 1044 | |
| 1045 | error_free_dma: |
| 1046 | dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma); |
| 1047 | error_free_host: |
| 1048 | mmc_free_host(mmc); |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | static int sunxi_mmc_remove(struct platform_device *pdev) |
| 1053 | { |
| 1054 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
| 1055 | struct sunxi_mmc_host *host = mmc_priv(mmc); |
| 1056 | |
| 1057 | mmc_remove_host(mmc); |
| 1058 | disable_irq(host->irq); |
| 1059 | sunxi_mmc_reset_host(host); |
| 1060 | |
| 1061 | if (!IS_ERR(host->reset)) |
| 1062 | reset_control_assert(host->reset); |
| 1063 | |
| 1064 | clk_disable_unprepare(host->clk_mmc); |
| 1065 | clk_disable_unprepare(host->clk_ahb); |
| 1066 | |
| 1067 | dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma); |
| 1068 | mmc_free_host(mmc); |
| 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | static struct platform_driver sunxi_mmc_driver = { |
| 1074 | .driver = { |
| 1075 | .name = "sunxi-mmc", |
David Lanzendörfer | 3cbcb160 | 2014-05-12 14:04:48 +0200 | [diff] [blame] | 1076 | .of_match_table = of_match_ptr(sunxi_mmc_of_match), |
| 1077 | }, |
| 1078 | .probe = sunxi_mmc_probe, |
| 1079 | .remove = sunxi_mmc_remove, |
| 1080 | }; |
| 1081 | module_platform_driver(sunxi_mmc_driver); |
| 1082 | |
| 1083 | MODULE_DESCRIPTION("Allwinner's SD/MMC Card Controller Driver"); |
| 1084 | MODULE_LICENSE("GPL v2"); |
| 1085 | MODULE_AUTHOR("David Lanzend�rfer <david.lanzendoerfer@o2s.ch>"); |
| 1086 | MODULE_ALIAS("platform:sunxi-mmc"); |