Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Atmel MultiMedia Card Interface driver |
| 3 | * |
| 4 | * Copyright (C) 2004-2008 Atmel Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/clk.h> |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 12 | #include <linux/debugfs.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 13 | #include <linux/device.h> |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 14 | #include <linux/dmaengine.h> |
| 15 | #include <linux/dma-mapping.h> |
Ben Nizette | fbfca4b | 2008-07-18 16:48:09 +1000 | [diff] [blame] | 16 | #include <linux/err.h> |
David Brownell | 3c26e17 | 2008-07-27 02:34:45 -0700 | [diff] [blame] | 17 | #include <linux/gpio.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/interrupt.h> |
Pramod Gurav | 7bca646 | 2014-09-23 18:21:48 +0530 | [diff] [blame] | 20 | #include <linux/io.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 21 | #include <linux/ioport.h> |
| 22 | #include <linux/module.h> |
Ludovic Desroches | e919fd2 | 2012-07-24 15:30:03 +0200 | [diff] [blame] | 23 | #include <linux/of.h> |
| 24 | #include <linux/of_device.h> |
| 25 | #include <linux/of_gpio.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/scatterlist.h> |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 28 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 30 | #include <linux/stat.h> |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 31 | #include <linux/types.h> |
Alexandre Belloni | 9cbef73 | 2014-10-17 10:26:36 +0200 | [diff] [blame] | 32 | #include <linux/platform_data/mmc-atmel-mci.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 33 | |
| 34 | #include <linux/mmc/host.h> |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 35 | #include <linux/mmc/sdio.h> |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 36 | |
Nicolas Ferre | c42aa77 | 2008-11-20 15:59:12 +0100 | [diff] [blame] | 37 | #include <linux/atmel-mci.h> |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 38 | #include <linux/atmel_pdc.h> |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 39 | #include <linux/pm.h> |
| 40 | #include <linux/pm_runtime.h> |
Wenyou Yang | b5b64fa | 2014-11-07 08:48:13 +0800 | [diff] [blame] | 41 | #include <linux/pinctrl/consumer.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 42 | |
Arnd Bergmann | bf614c7 | 2014-06-05 23:14:38 +0200 | [diff] [blame] | 43 | #include <asm/cacheflush.h> |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 44 | #include <asm/io.h> |
| 45 | #include <asm/unaligned.h> |
| 46 | |
ludovic.desroches@atmel.com | ec8fc9c | 2015-11-23 16:27:30 +0100 | [diff] [blame^] | 47 | /* |
| 48 | * Superset of MCI IP registers integrated in Atmel AVR32 and AT91 Processors |
| 49 | * Registers and bitfields marked with [2] are only available in MCI2 |
| 50 | */ |
| 51 | |
| 52 | /* MCI Register Definitions */ |
| 53 | #define ATMCI_CR 0x0000 /* Control */ |
| 54 | #define ATMCI_CR_MCIEN BIT(0) /* MCI Enable */ |
| 55 | #define ATMCI_CR_MCIDIS BIT(1) /* MCI Disable */ |
| 56 | #define ATMCI_CR_PWSEN BIT(2) /* Power Save Enable */ |
| 57 | #define ATMCI_CR_PWSDIS BIT(3) /* Power Save Disable */ |
| 58 | #define ATMCI_CR_SWRST BIT(7) /* Software Reset */ |
| 59 | #define ATMCI_MR 0x0004 /* Mode */ |
| 60 | #define ATMCI_MR_CLKDIV(x) ((x) << 0) /* Clock Divider */ |
| 61 | #define ATMCI_MR_PWSDIV(x) ((x) << 8) /* Power Saving Divider */ |
| 62 | #define ATMCI_MR_RDPROOF BIT(11) /* Read Proof */ |
| 63 | #define ATMCI_MR_WRPROOF BIT(12) /* Write Proof */ |
| 64 | #define ATMCI_MR_PDCFBYTE BIT(13) /* Force Byte Transfer */ |
| 65 | #define ATMCI_MR_PDCPADV BIT(14) /* Padding Value */ |
| 66 | #define ATMCI_MR_PDCMODE BIT(15) /* PDC-oriented Mode */ |
| 67 | #define ATMCI_MR_CLKODD(x) ((x) << 16) /* LSB of Clock Divider */ |
| 68 | #define ATMCI_DTOR 0x0008 /* Data Timeout */ |
| 69 | #define ATMCI_DTOCYC(x) ((x) << 0) /* Data Timeout Cycles */ |
| 70 | #define ATMCI_DTOMUL(x) ((x) << 4) /* Data Timeout Multiplier */ |
| 71 | #define ATMCI_SDCR 0x000c /* SD Card / SDIO */ |
| 72 | #define ATMCI_SDCSEL_SLOT_A (0 << 0) /* Select SD slot A */ |
| 73 | #define ATMCI_SDCSEL_SLOT_B (1 << 0) /* Select SD slot A */ |
| 74 | #define ATMCI_SDCSEL_MASK (3 << 0) |
| 75 | #define ATMCI_SDCBUS_1BIT (0 << 6) /* 1-bit data bus */ |
| 76 | #define ATMCI_SDCBUS_4BIT (2 << 6) /* 4-bit data bus */ |
| 77 | #define ATMCI_SDCBUS_8BIT (3 << 6) /* 8-bit data bus[2] */ |
| 78 | #define ATMCI_SDCBUS_MASK (3 << 6) |
| 79 | #define ATMCI_ARGR 0x0010 /* Command Argument */ |
| 80 | #define ATMCI_CMDR 0x0014 /* Command */ |
| 81 | #define ATMCI_CMDR_CMDNB(x) ((x) << 0) /* Command Opcode */ |
| 82 | #define ATMCI_CMDR_RSPTYP_NONE (0 << 6) /* No response */ |
| 83 | #define ATMCI_CMDR_RSPTYP_48BIT (1 << 6) /* 48-bit response */ |
| 84 | #define ATMCI_CMDR_RSPTYP_136BIT (2 << 6) /* 136-bit response */ |
| 85 | #define ATMCI_CMDR_SPCMD_INIT (1 << 8) /* Initialization command */ |
| 86 | #define ATMCI_CMDR_SPCMD_SYNC (2 << 8) /* Synchronized command */ |
| 87 | #define ATMCI_CMDR_SPCMD_INT (4 << 8) /* Interrupt command */ |
| 88 | #define ATMCI_CMDR_SPCMD_INTRESP (5 << 8) /* Interrupt response */ |
| 89 | #define ATMCI_CMDR_OPDCMD (1 << 11) /* Open Drain */ |
| 90 | #define ATMCI_CMDR_MAXLAT_5CYC (0 << 12) /* Max latency 5 cycles */ |
| 91 | #define ATMCI_CMDR_MAXLAT_64CYC (1 << 12) /* Max latency 64 cycles */ |
| 92 | #define ATMCI_CMDR_START_XFER (1 << 16) /* Start data transfer */ |
| 93 | #define ATMCI_CMDR_STOP_XFER (2 << 16) /* Stop data transfer */ |
| 94 | #define ATMCI_CMDR_TRDIR_WRITE (0 << 18) /* Write data */ |
| 95 | #define ATMCI_CMDR_TRDIR_READ (1 << 18) /* Read data */ |
| 96 | #define ATMCI_CMDR_BLOCK (0 << 19) /* Single-block transfer */ |
| 97 | #define ATMCI_CMDR_MULTI_BLOCK (1 << 19) /* Multi-block transfer */ |
| 98 | #define ATMCI_CMDR_STREAM (2 << 19) /* MMC Stream transfer */ |
| 99 | #define ATMCI_CMDR_SDIO_BYTE (4 << 19) /* SDIO Byte transfer */ |
| 100 | #define ATMCI_CMDR_SDIO_BLOCK (5 << 19) /* SDIO Block transfer */ |
| 101 | #define ATMCI_CMDR_SDIO_SUSPEND (1 << 24) /* SDIO Suspend Command */ |
| 102 | #define ATMCI_CMDR_SDIO_RESUME (2 << 24) /* SDIO Resume Command */ |
| 103 | #define ATMCI_BLKR 0x0018 /* Block */ |
| 104 | #define ATMCI_BCNT(x) ((x) << 0) /* Data Block Count */ |
| 105 | #define ATMCI_BLKLEN(x) ((x) << 16) /* Data Block Length */ |
| 106 | #define ATMCI_CSTOR 0x001c /* Completion Signal Timeout[2] */ |
| 107 | #define ATMCI_CSTOCYC(x) ((x) << 0) /* CST cycles */ |
| 108 | #define ATMCI_CSTOMUL(x) ((x) << 4) /* CST multiplier */ |
| 109 | #define ATMCI_RSPR 0x0020 /* Response 0 */ |
| 110 | #define ATMCI_RSPR1 0x0024 /* Response 1 */ |
| 111 | #define ATMCI_RSPR2 0x0028 /* Response 2 */ |
| 112 | #define ATMCI_RSPR3 0x002c /* Response 3 */ |
| 113 | #define ATMCI_RDR 0x0030 /* Receive Data */ |
| 114 | #define ATMCI_TDR 0x0034 /* Transmit Data */ |
| 115 | #define ATMCI_SR 0x0040 /* Status */ |
| 116 | #define ATMCI_IER 0x0044 /* Interrupt Enable */ |
| 117 | #define ATMCI_IDR 0x0048 /* Interrupt Disable */ |
| 118 | #define ATMCI_IMR 0x004c /* Interrupt Mask */ |
| 119 | #define ATMCI_CMDRDY BIT(0) /* Command Ready */ |
| 120 | #define ATMCI_RXRDY BIT(1) /* Receiver Ready */ |
| 121 | #define ATMCI_TXRDY BIT(2) /* Transmitter Ready */ |
| 122 | #define ATMCI_BLKE BIT(3) /* Data Block Ended */ |
| 123 | #define ATMCI_DTIP BIT(4) /* Data Transfer In Progress */ |
| 124 | #define ATMCI_NOTBUSY BIT(5) /* Data Not Busy */ |
| 125 | #define ATMCI_ENDRX BIT(6) /* End of RX Buffer */ |
| 126 | #define ATMCI_ENDTX BIT(7) /* End of TX Buffer */ |
| 127 | #define ATMCI_SDIOIRQA BIT(8) /* SDIO IRQ in slot A */ |
| 128 | #define ATMCI_SDIOIRQB BIT(9) /* SDIO IRQ in slot B */ |
| 129 | #define ATMCI_SDIOWAIT BIT(12) /* SDIO Read Wait Operation Status */ |
| 130 | #define ATMCI_CSRCV BIT(13) /* CE-ATA Completion Signal Received */ |
| 131 | #define ATMCI_RXBUFF BIT(14) /* RX Buffer Full */ |
| 132 | #define ATMCI_TXBUFE BIT(15) /* TX Buffer Empty */ |
| 133 | #define ATMCI_RINDE BIT(16) /* Response Index Error */ |
| 134 | #define ATMCI_RDIRE BIT(17) /* Response Direction Error */ |
| 135 | #define ATMCI_RCRCE BIT(18) /* Response CRC Error */ |
| 136 | #define ATMCI_RENDE BIT(19) /* Response End Bit Error */ |
| 137 | #define ATMCI_RTOE BIT(20) /* Response Time-Out Error */ |
| 138 | #define ATMCI_DCRCE BIT(21) /* Data CRC Error */ |
| 139 | #define ATMCI_DTOE BIT(22) /* Data Time-Out Error */ |
| 140 | #define ATMCI_CSTOE BIT(23) /* Completion Signal Time-out Error */ |
| 141 | #define ATMCI_BLKOVRE BIT(24) /* DMA Block Overrun Error */ |
| 142 | #define ATMCI_DMADONE BIT(25) /* DMA Transfer Done */ |
| 143 | #define ATMCI_FIFOEMPTY BIT(26) /* FIFO Empty Flag */ |
| 144 | #define ATMCI_XFRDONE BIT(27) /* Transfer Done Flag */ |
| 145 | #define ATMCI_ACKRCV BIT(28) /* Boot Operation Acknowledge Received */ |
| 146 | #define ATMCI_ACKRCVE BIT(29) /* Boot Operation Acknowledge Error */ |
| 147 | #define ATMCI_OVRE BIT(30) /* RX Overrun Error */ |
| 148 | #define ATMCI_UNRE BIT(31) /* TX Underrun Error */ |
| 149 | #define ATMCI_DMA 0x0050 /* DMA Configuration[2] */ |
| 150 | #define ATMCI_DMA_OFFSET(x) ((x) << 0) /* DMA Write Buffer Offset */ |
| 151 | #define ATMCI_DMA_CHKSIZE(x) ((x) << 4) /* DMA Channel Read and Write Chunk Size */ |
| 152 | #define ATMCI_DMAEN BIT(8) /* DMA Hardware Handshaking Enable */ |
| 153 | #define ATMCI_CFG 0x0054 /* Configuration[2] */ |
| 154 | #define ATMCI_CFG_FIFOMODE_1DATA BIT(0) /* MCI Internal FIFO control mode */ |
| 155 | #define ATMCI_CFG_FERRCTRL_COR BIT(4) /* Flow Error flag reset control mode */ |
| 156 | #define ATMCI_CFG_HSMODE BIT(8) /* High Speed Mode */ |
| 157 | #define ATMCI_CFG_LSYNC BIT(12) /* Synchronize on the last block */ |
| 158 | #define ATMCI_WPMR 0x00e4 /* Write Protection Mode[2] */ |
| 159 | #define ATMCI_WP_EN BIT(0) /* WP Enable */ |
| 160 | #define ATMCI_WP_KEY (0x4d4349 << 8) /* WP Key */ |
| 161 | #define ATMCI_WPSR 0x00e8 /* Write Protection Status[2] */ |
| 162 | #define ATMCI_GET_WP_VS(x) ((x) & 0x0f) |
| 163 | #define ATMCI_GET_WP_VSRC(x) (((x) >> 8) & 0xffff) |
| 164 | #define ATMCI_VERSION 0x00FC /* Version */ |
| 165 | #define ATMCI_FIFO_APERTURE 0x0200 /* FIFO Aperture[2] */ |
| 166 | |
| 167 | /* This is not including the FIFO Aperture on MCI2 */ |
| 168 | #define ATMCI_REGS_SIZE 0x100 |
| 169 | |
| 170 | /* Register access macros */ |
| 171 | #define atmci_readl(port, reg) \ |
| 172 | __raw_readl((port)->regs + reg) |
| 173 | #define atmci_writel(port, reg, value) \ |
| 174 | __raw_writel((value), (port)->regs + reg) |
| 175 | |
| 176 | /* On AVR chips the Peripheral DMA Controller is not connected to MCI. */ |
| 177 | #ifdef CONFIG_AVR32 |
| 178 | # define ATMCI_PDC_CONNECTED 0 |
| 179 | #else |
| 180 | # define ATMCI_PDC_CONNECTED 1 |
| 181 | #endif |
| 182 | |
| 183 | /* |
| 184 | * Fix sconfig's burst size according to atmel MCI. We need to convert them as: |
| 185 | * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3. |
| 186 | * |
| 187 | * This can be done by finding most significant bit set. |
| 188 | */ |
| 189 | static inline unsigned int atmci_convert_chksize(unsigned int maxburst) |
| 190 | { |
| 191 | if (maxburst > 1) |
| 192 | return fls(maxburst) - 2; |
| 193 | else |
| 194 | return 0; |
| 195 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 196 | |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 197 | #define AUTOSUSPEND_DELAY 50 |
| 198 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 199 | #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 200 | #define ATMCI_DMA_THRESHOLD 16 |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 201 | |
| 202 | enum { |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 203 | EVENT_CMD_RDY = 0, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 204 | EVENT_XFER_COMPLETE, |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 205 | EVENT_NOTBUSY, |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 206 | EVENT_DATA_ERROR, |
| 207 | }; |
| 208 | |
| 209 | enum atmel_mci_state { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 210 | STATE_IDLE = 0, |
| 211 | STATE_SENDING_CMD, |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 212 | STATE_DATA_XFER, |
| 213 | STATE_WAITING_NOTBUSY, |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 214 | STATE_SENDING_STOP, |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 215 | STATE_END_REQUEST, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 216 | }; |
| 217 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 218 | enum atmci_xfer_dir { |
| 219 | XFER_RECEIVE = 0, |
| 220 | XFER_TRANSMIT, |
| 221 | }; |
| 222 | |
| 223 | enum atmci_pdc_buf { |
| 224 | PDC_FIRST_BUF = 0, |
| 225 | PDC_SECOND_BUF, |
| 226 | }; |
| 227 | |
| 228 | struct atmel_mci_caps { |
Hein_Tibosch | ccdfe61 | 2012-08-30 16:34:38 +0000 | [diff] [blame] | 229 | bool has_dma_conf_reg; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 230 | bool has_pdc; |
| 231 | bool has_cfg_reg; |
| 232 | bool has_cstor_reg; |
| 233 | bool has_highspeed; |
| 234 | bool has_rwproof; |
Ludovic Desroches | faf8180 | 2012-03-21 16:41:23 +0100 | [diff] [blame] | 235 | bool has_odd_clk_div; |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 236 | bool has_bad_data_ordering; |
| 237 | bool need_reset_after_xfer; |
| 238 | bool need_blksz_mul_4; |
Ludovic Desroches | 077d407 | 2012-07-24 11:42:04 +0200 | [diff] [blame] | 239 | bool need_notbusy_for_read_ops; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 242 | struct atmel_mci_dma { |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 243 | struct dma_chan *chan; |
| 244 | struct dma_async_tx_descriptor *data_desc; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 245 | }; |
| 246 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 247 | /** |
| 248 | * struct atmel_mci - MMC controller state shared between all slots |
| 249 | * @lock: Spinlock protecting the queue and associated data. |
| 250 | * @regs: Pointer to MMIO registers. |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 251 | * @sg: Scatterlist entry currently being processed by PIO or PDC code. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 252 | * @pio_offset: Offset into the current scatterlist entry. |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 253 | * @buffer: Buffer used if we don't have the r/w proof capability. We |
| 254 | * don't have the time to switch pdc buffers so we have to use only |
| 255 | * one buffer for the full transaction. |
| 256 | * @buf_size: size of the buffer. |
| 257 | * @phys_buf_addr: buffer address needed for pdc. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 258 | * @cur_slot: The slot which is currently using the controller. |
| 259 | * @mrq: The request currently being processed on @cur_slot, |
| 260 | * or NULL if the controller is idle. |
| 261 | * @cmd: The command currently being sent to the card, or NULL. |
| 262 | * @data: The data currently being transferred, or NULL if no data |
| 263 | * transfer is in progress. |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 264 | * @data_size: just data->blocks * data->blksz. |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 265 | * @dma: DMA client state. |
| 266 | * @data_chan: DMA channel being used for the current data transfer. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 267 | * @cmd_status: Snapshot of SR taken upon completion of the current |
| 268 | * command. Only valid when EVENT_CMD_COMPLETE is pending. |
| 269 | * @data_status: Snapshot of SR taken upon completion of the current |
| 270 | * data transfer. Only valid when EVENT_DATA_COMPLETE or |
| 271 | * EVENT_DATA_ERROR is pending. |
| 272 | * @stop_cmdr: Value to be loaded into CMDR when the stop command is |
| 273 | * to be sent. |
| 274 | * @tasklet: Tasklet running the request state machine. |
| 275 | * @pending_events: Bitmask of events flagged by the interrupt handler |
| 276 | * to be processed by the tasklet. |
| 277 | * @completed_events: Bitmask of events which the state machine has |
| 278 | * processed. |
| 279 | * @state: Tasklet state. |
| 280 | * @queue: List of slots waiting for access to the controller. |
| 281 | * @need_clock_update: Update the clock rate before the next request. |
| 282 | * @need_reset: Reset controller before next request. |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 283 | * @timer: Timer to balance the data timeout error flag which cannot rise. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 284 | * @mode_reg: Value of the MR register. |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 285 | * @cfg_reg: Value of the CFG register. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 286 | * @bus_hz: The rate of @mck in Hz. This forms the basis for MMC bus |
| 287 | * rate and timeout calculations. |
| 288 | * @mapbase: Physical address of the MMIO registers. |
| 289 | * @mck: The peripheral bus clock hooked up to the MMC controller. |
| 290 | * @pdev: Platform device associated with the MMC controller. |
| 291 | * @slot: Slots sharing this MMC controller. |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 292 | * @caps: MCI capabilities depending on MCI version. |
| 293 | * @prepare_data: function to setup MCI before data transfer which |
| 294 | * depends on MCI capabilities. |
| 295 | * @submit_data: function to start data transfer which depends on MCI |
| 296 | * capabilities. |
| 297 | * @stop_transfer: function to stop data transfer which depends on MCI |
| 298 | * capabilities. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 299 | * |
| 300 | * Locking |
| 301 | * ======= |
| 302 | * |
| 303 | * @lock is a softirq-safe spinlock protecting @queue as well as |
| 304 | * @cur_slot, @mrq and @state. These must always be updated |
| 305 | * at the same time while holding @lock. |
| 306 | * |
| 307 | * @lock also protects mode_reg and need_clock_update since these are |
| 308 | * used to synchronize mode register updates with the queue |
| 309 | * processing. |
| 310 | * |
| 311 | * The @mrq field of struct atmel_mci_slot is also protected by @lock, |
| 312 | * and must always be written at the same time as the slot is added to |
| 313 | * @queue. |
| 314 | * |
| 315 | * @pending_events and @completed_events are accessed using atomic bit |
| 316 | * operations, so they don't need any locking. |
| 317 | * |
| 318 | * None of the fields touched by the interrupt handler need any |
| 319 | * locking. However, ordering is important: Before EVENT_DATA_ERROR or |
| 320 | * EVENT_DATA_COMPLETE is set in @pending_events, all data-related |
| 321 | * interrupts must be disabled and @data_status updated with a |
| 322 | * snapshot of SR. Similarly, before EVENT_CMD_COMPLETE is set, the |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 323 | * CMDRDY interrupt must be disabled and @cmd_status updated with a |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 324 | * snapshot of SR, and before EVENT_XFER_COMPLETE can be set, the |
| 325 | * bytes_xfered field of @data must be written. This is ensured by |
| 326 | * using barriers. |
| 327 | */ |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 328 | struct atmel_mci { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 329 | spinlock_t lock; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 330 | void __iomem *regs; |
| 331 | |
| 332 | struct scatterlist *sg; |
Terry Barnaby | bdbc5d0 | 2013-04-08 12:05:47 -0400 | [diff] [blame] | 333 | unsigned int sg_len; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 334 | unsigned int pio_offset; |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 335 | unsigned int *buffer; |
| 336 | unsigned int buf_size; |
| 337 | dma_addr_t buf_phys_addr; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 338 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 339 | struct atmel_mci_slot *cur_slot; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 340 | struct mmc_request *mrq; |
| 341 | struct mmc_command *cmd; |
| 342 | struct mmc_data *data; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 343 | unsigned int data_size; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 344 | |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 345 | struct atmel_mci_dma dma; |
| 346 | struct dma_chan *data_chan; |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 347 | struct dma_slave_config dma_conf; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 348 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 349 | u32 cmd_status; |
| 350 | u32 data_status; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 351 | u32 stop_cmdr; |
| 352 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 353 | struct tasklet_struct tasklet; |
| 354 | unsigned long pending_events; |
| 355 | unsigned long completed_events; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 356 | enum atmel_mci_state state; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 357 | struct list_head queue; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 358 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 359 | bool need_clock_update; |
| 360 | bool need_reset; |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 361 | struct timer_list timer; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 362 | u32 mode_reg; |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 363 | u32 cfg_reg; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 364 | unsigned long bus_hz; |
| 365 | unsigned long mapbase; |
| 366 | struct clk *mck; |
| 367 | struct platform_device *pdev; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 368 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 369 | struct atmel_mci_slot *slot[ATMCI_MAX_NR_SLOTS]; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 370 | |
| 371 | struct atmel_mci_caps caps; |
| 372 | |
| 373 | u32 (*prepare_data)(struct atmel_mci *host, struct mmc_data *data); |
| 374 | void (*submit_data)(struct atmel_mci *host, struct mmc_data *data); |
| 375 | void (*stop_transfer)(struct atmel_mci *host); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | /** |
| 379 | * struct atmel_mci_slot - MMC slot state |
| 380 | * @mmc: The mmc_host representing this slot. |
| 381 | * @host: The MMC controller this slot is using. |
| 382 | * @sdc_reg: Value of SDCR to be written before using this slot. |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 383 | * @sdio_irq: SDIO irq mask for this slot. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 384 | * @mrq: mmc_request currently being processed or waiting to be |
| 385 | * processed, or NULL when the slot is idle. |
| 386 | * @queue_node: List node for placing this node in the @queue list of |
| 387 | * &struct atmel_mci. |
| 388 | * @clock: Clock rate configured by set_ios(). Protected by host->lock. |
| 389 | * @flags: Random state bits associated with the slot. |
| 390 | * @detect_pin: GPIO pin used for card detection, or negative if not |
| 391 | * available. |
| 392 | * @wp_pin: GPIO pin used for card write protect sending, or negative |
| 393 | * if not available. |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 394 | * @detect_is_active_high: The state of the detect pin when it is active. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 395 | * @detect_timer: Timer used for debouncing @detect_pin interrupts. |
| 396 | */ |
| 397 | struct atmel_mci_slot { |
| 398 | struct mmc_host *mmc; |
| 399 | struct atmel_mci *host; |
| 400 | |
| 401 | u32 sdc_reg; |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 402 | u32 sdio_irq; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 403 | |
| 404 | struct mmc_request *mrq; |
| 405 | struct list_head queue_node; |
| 406 | |
| 407 | unsigned int clock; |
| 408 | unsigned long flags; |
| 409 | #define ATMCI_CARD_PRESENT 0 |
| 410 | #define ATMCI_CARD_NEED_INIT 1 |
| 411 | #define ATMCI_SHUTDOWN 2 |
| 412 | |
| 413 | int detect_pin; |
| 414 | int wp_pin; |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 415 | bool detect_is_active_high; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 416 | |
| 417 | struct timer_list detect_timer; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 418 | }; |
| 419 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 420 | #define atmci_test_and_clear_pending(host, event) \ |
| 421 | test_and_clear_bit(event, &host->pending_events) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 422 | #define atmci_set_completed(host, event) \ |
| 423 | set_bit(event, &host->completed_events) |
| 424 | #define atmci_set_pending(host, event) \ |
| 425 | set_bit(event, &host->pending_events) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 426 | |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 427 | /* |
| 428 | * The debugfs stuff below is mostly optimized away when |
| 429 | * CONFIG_DEBUG_FS is not set. |
| 430 | */ |
| 431 | static int atmci_req_show(struct seq_file *s, void *v) |
| 432 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 433 | struct atmel_mci_slot *slot = s->private; |
| 434 | struct mmc_request *mrq; |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 435 | struct mmc_command *cmd; |
| 436 | struct mmc_command *stop; |
| 437 | struct mmc_data *data; |
| 438 | |
| 439 | /* Make sure we get a consistent snapshot */ |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 440 | spin_lock_bh(&slot->host->lock); |
| 441 | mrq = slot->mrq; |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 442 | |
| 443 | if (mrq) { |
| 444 | cmd = mrq->cmd; |
| 445 | data = mrq->data; |
| 446 | stop = mrq->stop; |
| 447 | |
| 448 | if (cmd) |
| 449 | seq_printf(s, |
| 450 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", |
| 451 | cmd->opcode, cmd->arg, cmd->flags, |
| 452 | cmd->resp[0], cmd->resp[1], cmd->resp[2], |
Nicolas Ferre | d586ebb | 2010-05-11 14:06:50 -0700 | [diff] [blame] | 453 | cmd->resp[3], cmd->error); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 454 | if (data) |
| 455 | seq_printf(s, "DATA %u / %u * %u flg %x err %d\n", |
| 456 | data->bytes_xfered, data->blocks, |
| 457 | data->blksz, data->flags, data->error); |
| 458 | if (stop) |
| 459 | seq_printf(s, |
| 460 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", |
| 461 | stop->opcode, stop->arg, stop->flags, |
| 462 | stop->resp[0], stop->resp[1], stop->resp[2], |
Nicolas Ferre | d586ebb | 2010-05-11 14:06:50 -0700 | [diff] [blame] | 463 | stop->resp[3], stop->error); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 466 | spin_unlock_bh(&slot->host->lock); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int atmci_req_open(struct inode *inode, struct file *file) |
| 472 | { |
| 473 | return single_open(file, atmci_req_show, inode->i_private); |
| 474 | } |
| 475 | |
| 476 | static const struct file_operations atmci_req_fops = { |
| 477 | .owner = THIS_MODULE, |
| 478 | .open = atmci_req_open, |
| 479 | .read = seq_read, |
| 480 | .llseek = seq_lseek, |
| 481 | .release = single_release, |
| 482 | }; |
| 483 | |
| 484 | static void atmci_show_status_reg(struct seq_file *s, |
| 485 | const char *regname, u32 value) |
| 486 | { |
| 487 | static const char *sr_bit[] = { |
| 488 | [0] = "CMDRDY", |
| 489 | [1] = "RXRDY", |
| 490 | [2] = "TXRDY", |
| 491 | [3] = "BLKE", |
| 492 | [4] = "DTIP", |
| 493 | [5] = "NOTBUSY", |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 494 | [6] = "ENDRX", |
| 495 | [7] = "ENDTX", |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 496 | [8] = "SDIOIRQA", |
| 497 | [9] = "SDIOIRQB", |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 498 | [12] = "SDIOWAIT", |
| 499 | [14] = "RXBUFF", |
| 500 | [15] = "TXBUFE", |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 501 | [16] = "RINDE", |
| 502 | [17] = "RDIRE", |
| 503 | [18] = "RCRCE", |
| 504 | [19] = "RENDE", |
| 505 | [20] = "RTOE", |
| 506 | [21] = "DCRCE", |
| 507 | [22] = "DTOE", |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 508 | [23] = "CSTOE", |
| 509 | [24] = "BLKOVRE", |
| 510 | [25] = "DMADONE", |
| 511 | [26] = "FIFOEMPTY", |
| 512 | [27] = "XFRDONE", |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 513 | [30] = "OVRE", |
| 514 | [31] = "UNRE", |
| 515 | }; |
| 516 | unsigned int i; |
| 517 | |
| 518 | seq_printf(s, "%s:\t0x%08x", regname, value); |
| 519 | for (i = 0; i < ARRAY_SIZE(sr_bit); i++) { |
| 520 | if (value & (1 << i)) { |
| 521 | if (sr_bit[i]) |
| 522 | seq_printf(s, " %s", sr_bit[i]); |
| 523 | else |
| 524 | seq_puts(s, " UNKNOWN"); |
| 525 | } |
| 526 | } |
| 527 | seq_putc(s, '\n'); |
| 528 | } |
| 529 | |
| 530 | static int atmci_regs_show(struct seq_file *s, void *v) |
| 531 | { |
| 532 | struct atmel_mci *host = s->private; |
| 533 | u32 *buf; |
Boris BREZILLON | b3894f2 | 2013-07-18 09:38:52 +0200 | [diff] [blame] | 534 | int ret = 0; |
| 535 | |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 536 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 537 | buf = kmalloc(ATMCI_REGS_SIZE, GFP_KERNEL); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 538 | if (!buf) |
| 539 | return -ENOMEM; |
| 540 | |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 541 | pm_runtime_get_sync(&host->pdev->dev); |
| 542 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 543 | /* |
| 544 | * Grab a more or less consistent snapshot. Note that we're |
| 545 | * not disabling interrupts, so IMR and SR may not be |
| 546 | * consistent. |
| 547 | */ |
| 548 | spin_lock_bh(&host->lock); |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 549 | memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 550 | spin_unlock_bh(&host->lock); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 551 | |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 552 | pm_runtime_mark_last_busy(&host->pdev->dev); |
| 553 | pm_runtime_put_autosuspend(&host->pdev->dev); |
Boris BREZILLON | b3894f2 | 2013-07-18 09:38:52 +0200 | [diff] [blame] | 554 | |
Nicolas Ferre | 8a4de07 | 2012-07-06 12:11:51 +0200 | [diff] [blame] | 555 | seq_printf(s, "MR:\t0x%08x%s%s ", |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 556 | buf[ATMCI_MR / 4], |
| 557 | buf[ATMCI_MR / 4] & ATMCI_MR_RDPROOF ? " RDPROOF" : "", |
Nicolas Ferre | 8a4de07 | 2012-07-06 12:11:51 +0200 | [diff] [blame] | 558 | buf[ATMCI_MR / 4] & ATMCI_MR_WRPROOF ? " WRPROOF" : ""); |
| 559 | if (host->caps.has_odd_clk_div) |
| 560 | seq_printf(s, "{CLKDIV,CLKODD}=%u\n", |
| 561 | ((buf[ATMCI_MR / 4] & 0xff) << 1) |
| 562 | | ((buf[ATMCI_MR / 4] >> 16) & 1)); |
| 563 | else |
| 564 | seq_printf(s, "CLKDIV=%u\n", |
| 565 | (buf[ATMCI_MR / 4] & 0xff)); |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 566 | seq_printf(s, "DTOR:\t0x%08x\n", buf[ATMCI_DTOR / 4]); |
| 567 | seq_printf(s, "SDCR:\t0x%08x\n", buf[ATMCI_SDCR / 4]); |
| 568 | seq_printf(s, "ARGR:\t0x%08x\n", buf[ATMCI_ARGR / 4]); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 569 | seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n", |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 570 | buf[ATMCI_BLKR / 4], |
| 571 | buf[ATMCI_BLKR / 4] & 0xffff, |
| 572 | (buf[ATMCI_BLKR / 4] >> 16) & 0xffff); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 573 | if (host->caps.has_cstor_reg) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 574 | seq_printf(s, "CSTOR:\t0x%08x\n", buf[ATMCI_CSTOR / 4]); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 575 | |
| 576 | /* Don't read RSPR and RDR; it will consume the data there */ |
| 577 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 578 | atmci_show_status_reg(s, "SR", buf[ATMCI_SR / 4]); |
| 579 | atmci_show_status_reg(s, "IMR", buf[ATMCI_IMR / 4]); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 580 | |
Hein_Tibosch | ccdfe61 | 2012-08-30 16:34:38 +0000 | [diff] [blame] | 581 | if (host->caps.has_dma_conf_reg) { |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 582 | u32 val; |
| 583 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 584 | val = buf[ATMCI_DMA / 4]; |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 585 | seq_printf(s, "DMA:\t0x%08x OFFSET=%u CHKSIZE=%u%s\n", |
| 586 | val, val & 3, |
| 587 | ((val >> 4) & 3) ? |
| 588 | 1 << (((val >> 4) & 3) + 1) : 1, |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 589 | val & ATMCI_DMAEN ? " DMAEN" : ""); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 590 | } |
| 591 | if (host->caps.has_cfg_reg) { |
| 592 | u32 val; |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 593 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 594 | val = buf[ATMCI_CFG / 4]; |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 595 | seq_printf(s, "CFG:\t0x%08x%s%s%s%s\n", |
| 596 | val, |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 597 | val & ATMCI_CFG_FIFOMODE_1DATA ? " FIFOMODE_ONE_DATA" : "", |
| 598 | val & ATMCI_CFG_FERRCTRL_COR ? " FERRCTRL_CLEAR_ON_READ" : "", |
| 599 | val & ATMCI_CFG_HSMODE ? " HSMODE" : "", |
| 600 | val & ATMCI_CFG_LSYNC ? " LSYNC" : ""); |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Haavard Skinnemoen | b17339a | 2008-09-19 21:09:28 +0200 | [diff] [blame] | 603 | kfree(buf); |
| 604 | |
Boris BREZILLON | b3894f2 | 2013-07-18 09:38:52 +0200 | [diff] [blame] | 605 | return ret; |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | static int atmci_regs_open(struct inode *inode, struct file *file) |
| 609 | { |
| 610 | return single_open(file, atmci_regs_show, inode->i_private); |
| 611 | } |
| 612 | |
| 613 | static const struct file_operations atmci_regs_fops = { |
| 614 | .owner = THIS_MODULE, |
| 615 | .open = atmci_regs_open, |
| 616 | .read = seq_read, |
| 617 | .llseek = seq_lseek, |
| 618 | .release = single_release, |
| 619 | }; |
| 620 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 621 | static void atmci_init_debugfs(struct atmel_mci_slot *slot) |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 622 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 623 | struct mmc_host *mmc = slot->mmc; |
| 624 | struct atmel_mci *host = slot->host; |
| 625 | struct dentry *root; |
| 626 | struct dentry *node; |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 627 | |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 628 | root = mmc->debugfs_root; |
| 629 | if (!root) |
| 630 | return; |
| 631 | |
| 632 | node = debugfs_create_file("regs", S_IRUSR, root, host, |
| 633 | &atmci_regs_fops); |
| 634 | if (IS_ERR(node)) |
| 635 | return; |
| 636 | if (!node) |
| 637 | goto err; |
| 638 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 639 | node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 640 | if (!node) |
| 641 | goto err; |
| 642 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 643 | node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state); |
| 644 | if (!node) |
| 645 | goto err; |
| 646 | |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 647 | node = debugfs_create_x32("pending_events", S_IRUSR, root, |
| 648 | (u32 *)&host->pending_events); |
| 649 | if (!node) |
| 650 | goto err; |
| 651 | |
| 652 | node = debugfs_create_x32("completed_events", S_IRUSR, root, |
| 653 | (u32 *)&host->completed_events); |
| 654 | if (!node) |
| 655 | goto err; |
| 656 | |
| 657 | return; |
| 658 | |
| 659 | err: |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 660 | dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n"); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 661 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 662 | |
Ludovic Desroches | e919fd2 | 2012-07-24 15:30:03 +0200 | [diff] [blame] | 663 | #if defined(CONFIG_OF) |
| 664 | static const struct of_device_id atmci_dt_ids[] = { |
| 665 | { .compatible = "atmel,hsmci" }, |
| 666 | { /* sentinel */ } |
| 667 | }; |
| 668 | |
| 669 | MODULE_DEVICE_TABLE(of, atmci_dt_ids); |
| 670 | |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 671 | static struct mci_platform_data* |
Ludovic Desroches | e919fd2 | 2012-07-24 15:30:03 +0200 | [diff] [blame] | 672 | atmci_of_init(struct platform_device *pdev) |
| 673 | { |
| 674 | struct device_node *np = pdev->dev.of_node; |
| 675 | struct device_node *cnp; |
| 676 | struct mci_platform_data *pdata; |
| 677 | u32 slot_id; |
| 678 | |
| 679 | if (!np) { |
| 680 | dev_err(&pdev->dev, "device node not found\n"); |
| 681 | return ERR_PTR(-EINVAL); |
| 682 | } |
| 683 | |
| 684 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 685 | if (!pdata) { |
| 686 | dev_err(&pdev->dev, "could not allocate memory for pdata\n"); |
| 687 | return ERR_PTR(-ENOMEM); |
| 688 | } |
| 689 | |
| 690 | for_each_child_of_node(np, cnp) { |
| 691 | if (of_property_read_u32(cnp, "reg", &slot_id)) { |
| 692 | dev_warn(&pdev->dev, "reg property is missing for %s\n", |
| 693 | cnp->full_name); |
| 694 | continue; |
| 695 | } |
| 696 | |
| 697 | if (slot_id >= ATMCI_MAX_NR_SLOTS) { |
| 698 | dev_warn(&pdev->dev, "can't have more than %d slots\n", |
| 699 | ATMCI_MAX_NR_SLOTS); |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | if (of_property_read_u32(cnp, "bus-width", |
| 704 | &pdata->slot[slot_id].bus_width)) |
| 705 | pdata->slot[slot_id].bus_width = 1; |
| 706 | |
| 707 | pdata->slot[slot_id].detect_pin = |
| 708 | of_get_named_gpio(cnp, "cd-gpios", 0); |
| 709 | |
| 710 | pdata->slot[slot_id].detect_is_active_high = |
| 711 | of_property_read_bool(cnp, "cd-inverted"); |
| 712 | |
Timo Kokkonen | 76d5556 | 2014-11-03 13:12:59 +0200 | [diff] [blame] | 713 | pdata->slot[slot_id].non_removable = |
| 714 | of_property_read_bool(cnp, "non-removable"); |
| 715 | |
Ludovic Desroches | e919fd2 | 2012-07-24 15:30:03 +0200 | [diff] [blame] | 716 | pdata->slot[slot_id].wp_pin = |
| 717 | of_get_named_gpio(cnp, "wp-gpios", 0); |
| 718 | } |
| 719 | |
| 720 | return pdata; |
| 721 | } |
| 722 | #else /* CONFIG_OF */ |
| 723 | static inline struct mci_platform_data* |
| 724 | atmci_of_init(struct platform_device *dev) |
| 725 | { |
| 726 | return ERR_PTR(-EINVAL); |
| 727 | } |
| 728 | #endif |
| 729 | |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 730 | static inline unsigned int atmci_get_version(struct atmel_mci *host) |
| 731 | { |
| 732 | return atmci_readl(host, ATMCI_VERSION) & 0x00000fff; |
| 733 | } |
| 734 | |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 735 | static void atmci_timeout_timer(unsigned long data) |
| 736 | { |
| 737 | struct atmel_mci *host; |
| 738 | |
| 739 | host = (struct atmel_mci *)data; |
| 740 | |
| 741 | dev_dbg(&host->pdev->dev, "software timeout\n"); |
| 742 | |
| 743 | if (host->mrq->cmd->data) { |
| 744 | host->mrq->cmd->data->error = -ETIMEDOUT; |
| 745 | host->data = NULL; |
Ludovic Desroches | c1fa342 | 2013-09-09 17:29:56 +0200 | [diff] [blame] | 746 | /* |
| 747 | * With some SDIO modules, sometimes DMA transfer hangs. If |
| 748 | * stop_transfer() is not called then the DMA request is not |
| 749 | * removed, following ones are queued and never computed. |
| 750 | */ |
| 751 | if (host->state == STATE_DATA_XFER) |
| 752 | host->stop_transfer(host); |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 753 | } else { |
| 754 | host->mrq->cmd->error = -ETIMEDOUT; |
| 755 | host->cmd = NULL; |
| 756 | } |
| 757 | host->need_reset = 1; |
| 758 | host->state = STATE_END_REQUEST; |
| 759 | smp_wmb(); |
| 760 | tasklet_schedule(&host->tasklet); |
| 761 | } |
| 762 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 763 | static inline unsigned int atmci_ns_to_clocks(struct atmel_mci *host, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 764 | unsigned int ns) |
| 765 | { |
Ludovic Desroches | 66292ad | 2012-03-28 12:28:33 +0200 | [diff] [blame] | 766 | /* |
| 767 | * It is easier here to use us instead of ns for the timeout, |
| 768 | * it prevents from overflows during calculation. |
| 769 | */ |
| 770 | unsigned int us = DIV_ROUND_UP(ns, 1000); |
| 771 | |
| 772 | /* Maximum clock frequency is host->bus_hz/2 */ |
| 773 | return us * (DIV_ROUND_UP(host->bus_hz, 2000000)); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | static void atmci_set_timeout(struct atmel_mci *host, |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 777 | struct atmel_mci_slot *slot, struct mmc_data *data) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 778 | { |
| 779 | static unsigned dtomul_to_shift[] = { |
| 780 | 0, 4, 7, 8, 10, 12, 16, 20 |
| 781 | }; |
| 782 | unsigned timeout; |
| 783 | unsigned dtocyc; |
| 784 | unsigned dtomul; |
| 785 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 786 | timeout = atmci_ns_to_clocks(host, data->timeout_ns) |
| 787 | + data->timeout_clks; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 788 | |
| 789 | for (dtomul = 0; dtomul < 8; dtomul++) { |
| 790 | unsigned shift = dtomul_to_shift[dtomul]; |
| 791 | dtocyc = (timeout + (1 << shift) - 1) >> shift; |
| 792 | if (dtocyc < 15) |
| 793 | break; |
| 794 | } |
| 795 | |
| 796 | if (dtomul >= 8) { |
| 797 | dtomul = 7; |
| 798 | dtocyc = 15; |
| 799 | } |
| 800 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 801 | dev_vdbg(&slot->mmc->class_dev, "setting timeout to %u cycles\n", |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 802 | dtocyc << dtomul_to_shift[dtomul]); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 803 | atmci_writel(host, ATMCI_DTOR, (ATMCI_DTOMUL(dtomul) | ATMCI_DTOCYC(dtocyc))); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Return mask with command flags to be enabled for this command. |
| 808 | */ |
| 809 | static u32 atmci_prepare_command(struct mmc_host *mmc, |
| 810 | struct mmc_command *cmd) |
| 811 | { |
| 812 | struct mmc_data *data; |
| 813 | u32 cmdr; |
| 814 | |
| 815 | cmd->error = -EINPROGRESS; |
| 816 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 817 | cmdr = ATMCI_CMDR_CMDNB(cmd->opcode); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 818 | |
| 819 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 820 | if (cmd->flags & MMC_RSP_136) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 821 | cmdr |= ATMCI_CMDR_RSPTYP_136BIT; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 822 | else |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 823 | cmdr |= ATMCI_CMDR_RSPTYP_48BIT; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /* |
| 827 | * This should really be MAXLAT_5 for CMD2 and ACMD41, but |
| 828 | * it's too difficult to determine whether this is an ACMD or |
| 829 | * not. Better make it 64. |
| 830 | */ |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 831 | cmdr |= ATMCI_CMDR_MAXLAT_64CYC; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 832 | |
| 833 | if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 834 | cmdr |= ATMCI_CMDR_OPDCMD; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 835 | |
| 836 | data = cmd->data; |
| 837 | if (data) { |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 838 | cmdr |= ATMCI_CMDR_START_XFER; |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 839 | |
| 840 | if (cmd->opcode == SD_IO_RW_EXTENDED) { |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 841 | cmdr |= ATMCI_CMDR_SDIO_BLOCK; |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 842 | } else { |
| 843 | if (data->flags & MMC_DATA_STREAM) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 844 | cmdr |= ATMCI_CMDR_STREAM; |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 845 | else if (data->blocks > 1) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 846 | cmdr |= ATMCI_CMDR_MULTI_BLOCK; |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 847 | else |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 848 | cmdr |= ATMCI_CMDR_BLOCK; |
Nicolas Ferre | 2f1d791 | 2010-12-10 19:14:32 +0100 | [diff] [blame] | 849 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 850 | |
| 851 | if (data->flags & MMC_DATA_READ) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 852 | cmdr |= ATMCI_CMDR_TRDIR_READ; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | return cmdr; |
| 856 | } |
| 857 | |
Ludovic Desroches | 11d1488 | 2011-08-11 15:25:45 +0000 | [diff] [blame] | 858 | static void atmci_send_command(struct atmel_mci *host, |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 859 | struct mmc_command *cmd, u32 cmd_flags) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 860 | { |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 861 | WARN_ON(host->cmd); |
| 862 | host->cmd = cmd; |
| 863 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 864 | dev_vdbg(&host->pdev->dev, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 865 | "start command: ARGR=0x%08x CMDR=0x%08x\n", |
| 866 | cmd->arg, cmd_flags); |
| 867 | |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 868 | atmci_writel(host, ATMCI_ARGR, cmd->arg); |
| 869 | atmci_writel(host, ATMCI_CMDR, cmd_flags); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 870 | } |
| 871 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 872 | static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 873 | { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 874 | dev_dbg(&host->pdev->dev, "send stop command\n"); |
Ludovic Desroches | 11d1488 | 2011-08-11 15:25:45 +0000 | [diff] [blame] | 875 | atmci_send_command(host, data->stop, host->stop_cmdr); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 876 | atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 877 | } |
| 878 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 879 | /* |
| 880 | * Configure given PDC buffer taking care of alignement issues. |
| 881 | * Update host->data_size and host->sg. |
| 882 | */ |
| 883 | static void atmci_pdc_set_single_buf(struct atmel_mci *host, |
| 884 | enum atmci_xfer_dir dir, enum atmci_pdc_buf buf_nb) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 885 | { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 886 | u32 pointer_reg, counter_reg; |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 887 | unsigned int buf_size; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 888 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 889 | if (dir == XFER_RECEIVE) { |
| 890 | pointer_reg = ATMEL_PDC_RPR; |
| 891 | counter_reg = ATMEL_PDC_RCR; |
| 892 | } else { |
| 893 | pointer_reg = ATMEL_PDC_TPR; |
| 894 | counter_reg = ATMEL_PDC_TCR; |
| 895 | } |
| 896 | |
| 897 | if (buf_nb == PDC_SECOND_BUF) { |
Ludovic Desroches | 1ebbe3d | 2011-08-11 15:25:46 +0000 | [diff] [blame] | 898 | pointer_reg += ATMEL_PDC_SCND_BUF_OFF; |
| 899 | counter_reg += ATMEL_PDC_SCND_BUF_OFF; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 902 | if (!host->caps.has_rwproof) { |
| 903 | buf_size = host->buf_size; |
| 904 | atmci_writel(host, pointer_reg, host->buf_phys_addr); |
| 905 | } else { |
| 906 | buf_size = sg_dma_len(host->sg); |
| 907 | atmci_writel(host, pointer_reg, sg_dma_address(host->sg)); |
| 908 | } |
| 909 | |
| 910 | if (host->data_size <= buf_size) { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 911 | if (host->data_size & 0x3) { |
| 912 | /* If size is different from modulo 4, transfer bytes */ |
| 913 | atmci_writel(host, counter_reg, host->data_size); |
| 914 | atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCFBYTE); |
| 915 | } else { |
| 916 | /* Else transfer 32-bits words */ |
| 917 | atmci_writel(host, counter_reg, host->data_size / 4); |
| 918 | } |
| 919 | host->data_size = 0; |
| 920 | } else { |
| 921 | /* We assume the size of a page is 32-bits aligned */ |
Ludovic Desroches | 341fa4c | 2011-08-11 15:25:47 +0000 | [diff] [blame] | 922 | atmci_writel(host, counter_reg, sg_dma_len(host->sg) / 4); |
| 923 | host->data_size -= sg_dma_len(host->sg); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 924 | if (host->data_size) |
| 925 | host->sg = sg_next(host->sg); |
| 926 | } |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 927 | } |
| 928 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 929 | /* |
| 930 | * Configure PDC buffer according to the data size ie configuring one or two |
| 931 | * buffers. Don't use this function if you want to configure only the second |
| 932 | * buffer. In this case, use atmci_pdc_set_single_buf. |
| 933 | */ |
| 934 | static void atmci_pdc_set_both_buf(struct atmel_mci *host, int dir) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 935 | { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 936 | atmci_pdc_set_single_buf(host, dir, PDC_FIRST_BUF); |
| 937 | if (host->data_size) |
| 938 | atmci_pdc_set_single_buf(host, dir, PDC_SECOND_BUF); |
| 939 | } |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 940 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 941 | /* |
| 942 | * Unmap sg lists, called when transfer is finished. |
| 943 | */ |
| 944 | static void atmci_pdc_cleanup(struct atmel_mci *host) |
| 945 | { |
| 946 | struct mmc_data *data = host->data; |
| 947 | |
| 948 | if (data) |
| 949 | dma_unmap_sg(&host->pdev->dev, |
| 950 | data->sg, data->sg_len, |
| 951 | ((data->flags & MMC_DATA_WRITE) |
| 952 | ? DMA_TO_DEVICE : DMA_FROM_DEVICE)); |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Disable PDC transfers. Update pending flags to EVENT_XFER_COMPLETE after |
| 957 | * having received ATMCI_TXBUFE or ATMCI_RXBUFF interrupt. Enable ATMCI_NOTBUSY |
| 958 | * interrupt needed for both transfer directions. |
| 959 | */ |
| 960 | static void atmci_pdc_complete(struct atmel_mci *host) |
| 961 | { |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 962 | int transfer_size = host->data->blocks * host->data->blksz; |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 963 | int i; |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 964 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 965 | atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS); |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 966 | |
| 967 | if ((!host->caps.has_rwproof) |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 968 | && (host->data->flags & MMC_DATA_READ)) { |
| 969 | if (host->caps.has_bad_data_ordering) |
| 970 | for (i = 0; i < transfer_size; i++) |
| 971 | host->buffer[i] = swab32(host->buffer[i]); |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 972 | sg_copy_from_buffer(host->data->sg, host->data->sg_len, |
| 973 | host->buffer, transfer_size); |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 974 | } |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 975 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 976 | atmci_pdc_cleanup(host); |
| 977 | |
Alexandre Belloni | 6e9e406 | 2014-05-06 17:43:26 +0200 | [diff] [blame] | 978 | dev_dbg(&host->pdev->dev, "(%s) set pending xfer complete\n", __func__); |
| 979 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
| 980 | tasklet_schedule(&host->tasklet); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 981 | } |
| 982 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 983 | static void atmci_dma_cleanup(struct atmel_mci *host) |
| 984 | { |
| 985 | struct mmc_data *data = host->data; |
| 986 | |
| 987 | if (data) |
| 988 | dma_unmap_sg(host->dma.chan->device->dev, |
| 989 | data->sg, data->sg_len, |
| 990 | ((data->flags & MMC_DATA_WRITE) |
| 991 | ? DMA_TO_DEVICE : DMA_FROM_DEVICE)); |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * This function is called by the DMA driver from tasklet context. |
| 996 | */ |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 997 | static void atmci_dma_complete(void *arg) |
| 998 | { |
| 999 | struct atmel_mci *host = arg; |
| 1000 | struct mmc_data *data = host->data; |
| 1001 | |
| 1002 | dev_vdbg(&host->pdev->dev, "DMA complete\n"); |
| 1003 | |
Hein_Tibosch | ccdfe61 | 2012-08-30 16:34:38 +0000 | [diff] [blame] | 1004 | if (host->caps.has_dma_conf_reg) |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 1005 | /* Disable DMA hardware handshaking on MCI */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1006 | atmci_writel(host, ATMCI_DMA, atmci_readl(host, ATMCI_DMA) & ~ATMCI_DMAEN); |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 1007 | |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1008 | atmci_dma_cleanup(host); |
| 1009 | |
| 1010 | /* |
| 1011 | * If the card was removed, data will be NULL. No point trying |
| 1012 | * to send the stop command or waiting for NBUSY in this case. |
| 1013 | */ |
| 1014 | if (data) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1015 | dev_dbg(&host->pdev->dev, |
| 1016 | "(%s) set pending xfer complete\n", __func__); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1017 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
| 1018 | tasklet_schedule(&host->tasklet); |
| 1019 | |
| 1020 | /* |
| 1021 | * Regardless of what the documentation says, we have |
| 1022 | * to wait for NOTBUSY even after block read |
| 1023 | * operations. |
| 1024 | * |
| 1025 | * When the DMA transfer is complete, the controller |
| 1026 | * may still be reading the CRC from the card, i.e. |
| 1027 | * the data transfer is still in progress and we |
| 1028 | * haven't seen all the potential error bits yet. |
| 1029 | * |
| 1030 | * The interrupt handler will schedule a different |
| 1031 | * tasklet to finish things up when the data transfer |
| 1032 | * is completely done. |
| 1033 | * |
| 1034 | * We may not complete the mmc request here anyway |
| 1035 | * because the mmc layer may call back and cause us to |
| 1036 | * violate the "don't submit new operations from the |
| 1037 | * completion callback" rule of the dma engine |
| 1038 | * framework. |
| 1039 | */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1040 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1044 | /* |
| 1045 | * Returns a mask of interrupt flags to be enabled after the whole |
| 1046 | * request has been prepared. |
| 1047 | */ |
| 1048 | static u32 atmci_prepare_data(struct atmel_mci *host, struct mmc_data *data) |
| 1049 | { |
| 1050 | u32 iflags; |
| 1051 | |
| 1052 | data->error = -EINPROGRESS; |
| 1053 | |
| 1054 | host->sg = data->sg; |
Terry Barnaby | bdbc5d0 | 2013-04-08 12:05:47 -0400 | [diff] [blame] | 1055 | host->sg_len = data->sg_len; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1056 | host->data = data; |
| 1057 | host->data_chan = NULL; |
| 1058 | |
| 1059 | iflags = ATMCI_DATA_ERROR_FLAGS; |
| 1060 | |
| 1061 | /* |
| 1062 | * Errata: MMC data write operation with less than 12 |
| 1063 | * bytes is impossible. |
| 1064 | * |
| 1065 | * Errata: MCI Transmit Data Register (TDR) FIFO |
| 1066 | * corruption when length is not multiple of 4. |
| 1067 | */ |
| 1068 | if (data->blocks * data->blksz < 12 |
| 1069 | || (data->blocks * data->blksz) & 3) |
| 1070 | host->need_reset = true; |
| 1071 | |
| 1072 | host->pio_offset = 0; |
| 1073 | if (data->flags & MMC_DATA_READ) |
| 1074 | iflags |= ATMCI_RXRDY; |
| 1075 | else |
| 1076 | iflags |= ATMCI_TXRDY; |
| 1077 | |
| 1078 | return iflags; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * Set interrupt flags and set block length into the MCI mode register even |
| 1083 | * if this value is also accessible in the MCI block register. It seems to be |
| 1084 | * necessary before the High Speed MCI version. It also map sg and configure |
| 1085 | * PDC registers. |
| 1086 | */ |
| 1087 | static u32 |
| 1088 | atmci_prepare_data_pdc(struct atmel_mci *host, struct mmc_data *data) |
| 1089 | { |
| 1090 | u32 iflags, tmp; |
| 1091 | unsigned int sg_len; |
| 1092 | enum dma_data_direction dir; |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 1093 | int i; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1094 | |
| 1095 | data->error = -EINPROGRESS; |
| 1096 | |
| 1097 | host->data = data; |
| 1098 | host->sg = data->sg; |
| 1099 | iflags = ATMCI_DATA_ERROR_FLAGS; |
| 1100 | |
| 1101 | /* Enable pdc mode */ |
| 1102 | atmci_writel(host, ATMCI_MR, host->mode_reg | ATMCI_MR_PDCMODE); |
| 1103 | |
| 1104 | if (data->flags & MMC_DATA_READ) { |
| 1105 | dir = DMA_FROM_DEVICE; |
| 1106 | iflags |= ATMCI_ENDRX | ATMCI_RXBUFF; |
| 1107 | } else { |
| 1108 | dir = DMA_TO_DEVICE; |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1109 | iflags |= ATMCI_ENDTX | ATMCI_TXBUFE | ATMCI_BLKE; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /* Set BLKLEN */ |
| 1113 | tmp = atmci_readl(host, ATMCI_MR); |
| 1114 | tmp &= 0x0000ffff; |
| 1115 | tmp |= ATMCI_BLKLEN(data->blksz); |
| 1116 | atmci_writel(host, ATMCI_MR, tmp); |
| 1117 | |
| 1118 | /* Configure PDC */ |
| 1119 | host->data_size = data->blocks * data->blksz; |
| 1120 | sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len, dir); |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 1121 | |
| 1122 | if ((!host->caps.has_rwproof) |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 1123 | && (host->data->flags & MMC_DATA_WRITE)) { |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 1124 | sg_copy_to_buffer(host->data->sg, host->data->sg_len, |
| 1125 | host->buffer, host->data_size); |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 1126 | if (host->caps.has_bad_data_ordering) |
| 1127 | for (i = 0; i < host->data_size; i++) |
| 1128 | host->buffer[i] = swab32(host->buffer[i]); |
| 1129 | } |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 1130 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1131 | if (host->data_size) |
| 1132 | atmci_pdc_set_both_buf(host, |
| 1133 | ((dir == DMA_FROM_DEVICE) ? XFER_RECEIVE : XFER_TRANSMIT)); |
| 1134 | |
| 1135 | return iflags; |
| 1136 | } |
| 1137 | |
| 1138 | static u32 |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 1139 | atmci_prepare_data_dma(struct atmel_mci *host, struct mmc_data *data) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1140 | { |
| 1141 | struct dma_chan *chan; |
| 1142 | struct dma_async_tx_descriptor *desc; |
| 1143 | struct scatterlist *sg; |
| 1144 | unsigned int i; |
| 1145 | enum dma_data_direction direction; |
Vinod Koul | e0d23ef | 2011-11-17 14:54:38 +0530 | [diff] [blame] | 1146 | enum dma_transfer_direction slave_dirn; |
Atsushi Nemoto | 657a77f | 2009-09-08 17:53:05 -0700 | [diff] [blame] | 1147 | unsigned int sglen; |
Nicolas Ferre | 693e5e2 | 2012-06-06 12:19:44 +0200 | [diff] [blame] | 1148 | u32 maxburst; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1149 | u32 iflags; |
| 1150 | |
| 1151 | data->error = -EINPROGRESS; |
| 1152 | |
| 1153 | WARN_ON(host->data); |
| 1154 | host->sg = NULL; |
| 1155 | host->data = data; |
| 1156 | |
| 1157 | iflags = ATMCI_DATA_ERROR_FLAGS; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1158 | |
| 1159 | /* |
| 1160 | * We don't do DMA on "complex" transfers, i.e. with |
| 1161 | * non-word-aligned buffers or lengths. Also, we don't bother |
| 1162 | * with all the DMA setup overhead for short transfers. |
| 1163 | */ |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1164 | if (data->blocks * data->blksz < ATMCI_DMA_THRESHOLD) |
| 1165 | return atmci_prepare_data(host, data); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1166 | if (data->blksz & 3) |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1167 | return atmci_prepare_data(host, data); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1168 | |
| 1169 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 1170 | if (sg->offset & 3 || sg->length & 3) |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1171 | return atmci_prepare_data(host, data); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | /* If we don't have a channel, we can't do DMA */ |
| 1175 | chan = host->dma.chan; |
Dan Williams | 6f49a57 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 1176 | if (chan) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1177 | host->data_chan = chan; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1178 | |
| 1179 | if (!chan) |
| 1180 | return -ENODEV; |
| 1181 | |
Vinod Koul | e0d23ef | 2011-11-17 14:54:38 +0530 | [diff] [blame] | 1182 | if (data->flags & MMC_DATA_READ) { |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1183 | direction = DMA_FROM_DEVICE; |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 1184 | host->dma_conf.direction = slave_dirn = DMA_DEV_TO_MEM; |
Nicolas Ferre | 693e5e2 | 2012-06-06 12:19:44 +0200 | [diff] [blame] | 1185 | maxburst = atmci_convert_chksize(host->dma_conf.src_maxburst); |
Vinod Koul | e0d23ef | 2011-11-17 14:54:38 +0530 | [diff] [blame] | 1186 | } else { |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1187 | direction = DMA_TO_DEVICE; |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 1188 | host->dma_conf.direction = slave_dirn = DMA_MEM_TO_DEV; |
Nicolas Ferre | 693e5e2 | 2012-06-06 12:19:44 +0200 | [diff] [blame] | 1189 | maxburst = atmci_convert_chksize(host->dma_conf.dst_maxburst); |
Vinod Koul | e0d23ef | 2011-11-17 14:54:38 +0530 | [diff] [blame] | 1190 | } |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1191 | |
Hein_Tibosch | ccdfe61 | 2012-08-30 16:34:38 +0000 | [diff] [blame] | 1192 | if (host->caps.has_dma_conf_reg) |
| 1193 | atmci_writel(host, ATMCI_DMA, ATMCI_DMA_CHKSIZE(maxburst) | |
| 1194 | ATMCI_DMAEN); |
Nicolas Ferre | 693e5e2 | 2012-06-06 12:19:44 +0200 | [diff] [blame] | 1195 | |
Linus Walleij | 266ac3f | 2011-02-10 16:08:06 +0100 | [diff] [blame] | 1196 | sglen = dma_map_sg(chan->device->dev, data->sg, |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1197 | data->sg_len, direction); |
Linus Walleij | 88ce4db3 | 2011-02-10 16:08:16 +0100 | [diff] [blame] | 1198 | |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 1199 | dmaengine_slave_config(chan, &host->dma_conf); |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 1200 | desc = dmaengine_prep_slave_sg(chan, |
Vinod Koul | e0d23ef | 2011-11-17 14:54:38 +0530 | [diff] [blame] | 1201 | data->sg, sglen, slave_dirn, |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1202 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 1203 | if (!desc) |
Atsushi Nemoto | 657a77f | 2009-09-08 17:53:05 -0700 | [diff] [blame] | 1204 | goto unmap_exit; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1205 | |
| 1206 | host->dma.data_desc = desc; |
| 1207 | desc->callback = atmci_dma_complete; |
| 1208 | desc->callback_param = host; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1209 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1210 | return iflags; |
Atsushi Nemoto | 657a77f | 2009-09-08 17:53:05 -0700 | [diff] [blame] | 1211 | unmap_exit: |
Linus Walleij | 88ce4db3 | 2011-02-10 16:08:16 +0100 | [diff] [blame] | 1212 | dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, direction); |
Atsushi Nemoto | 657a77f | 2009-09-08 17:53:05 -0700 | [diff] [blame] | 1213 | return -ENOMEM; |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1216 | static void |
| 1217 | atmci_submit_data(struct atmel_mci *host, struct mmc_data *data) |
| 1218 | { |
| 1219 | return; |
| 1220 | } |
| 1221 | |
| 1222 | /* |
| 1223 | * Start PDC according to transfer direction. |
| 1224 | */ |
| 1225 | static void |
| 1226 | atmci_submit_data_pdc(struct atmel_mci *host, struct mmc_data *data) |
| 1227 | { |
| 1228 | if (data->flags & MMC_DATA_READ) |
| 1229 | atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); |
| 1230 | else |
| 1231 | atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); |
| 1232 | } |
| 1233 | |
| 1234 | static void |
| 1235 | atmci_submit_data_dma(struct atmel_mci *host, struct mmc_data *data) |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 1236 | { |
| 1237 | struct dma_chan *chan = host->data_chan; |
| 1238 | struct dma_async_tx_descriptor *desc = host->dma.data_desc; |
| 1239 | |
| 1240 | if (chan) { |
Linus Walleij | 5328906 | 2011-02-10 16:08:26 +0100 | [diff] [blame] | 1241 | dmaengine_submit(desc); |
| 1242 | dma_async_issue_pending(chan); |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1246 | static void atmci_stop_transfer(struct atmel_mci *host) |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1247 | { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1248 | dev_dbg(&host->pdev->dev, |
| 1249 | "(%s) set pending xfer complete\n", __func__); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1250 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1251 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 1252 | } |
| 1253 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1254 | /* |
Masanari Iida | 7122bbb | 2012-08-05 23:25:40 +0900 | [diff] [blame] | 1255 | * Stop data transfer because error(s) occurred. |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1256 | */ |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1257 | static void atmci_stop_transfer_pdc(struct atmel_mci *host) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1258 | { |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1259 | atmci_writel(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1260 | } |
| 1261 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1262 | static void atmci_stop_transfer_dma(struct atmel_mci *host) |
| 1263 | { |
| 1264 | struct dma_chan *chan = host->data_chan; |
| 1265 | |
| 1266 | if (chan) { |
| 1267 | dmaengine_terminate_all(chan); |
| 1268 | atmci_dma_cleanup(host); |
| 1269 | } else { |
| 1270 | /* Data transfer was stopped by the interrupt handler */ |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1271 | dev_dbg(&host->pdev->dev, |
| 1272 | "(%s) set pending xfer complete\n", __func__); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1273 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
| 1274 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * Start a request: prepare data if needed, prepare the command and activate |
| 1280 | * interrupts. |
| 1281 | */ |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1282 | static void atmci_start_request(struct atmel_mci *host, |
| 1283 | struct atmel_mci_slot *slot) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1284 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1285 | struct mmc_request *mrq; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1286 | struct mmc_command *cmd; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1287 | struct mmc_data *data; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1288 | u32 iflags; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1289 | u32 cmdflags; |
| 1290 | |
| 1291 | mrq = slot->mrq; |
| 1292 | host->cur_slot = slot; |
| 1293 | host->mrq = mrq; |
| 1294 | |
| 1295 | host->pending_events = 0; |
| 1296 | host->completed_events = 0; |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1297 | host->cmd_status = 0; |
Haavard Skinnemoen | ca55f46 | 2008-10-05 15:16:59 +0200 | [diff] [blame] | 1298 | host->data_status = 0; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1299 | |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1300 | dev_dbg(&host->pdev->dev, "start request: cmd %u\n", mrq->cmd->opcode); |
| 1301 | |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 1302 | if (host->need_reset || host->caps.need_reset_after_xfer) { |
Ludovic Desroches | 18ee684 | 2012-02-09 11:55:29 +0100 | [diff] [blame] | 1303 | iflags = atmci_readl(host, ATMCI_IMR); |
| 1304 | iflags &= (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1305 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
| 1306 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN); |
| 1307 | atmci_writel(host, ATMCI_MR, host->mode_reg); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1308 | if (host->caps.has_cfg_reg) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1309 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
Ludovic Desroches | 18ee684 | 2012-02-09 11:55:29 +0100 | [diff] [blame] | 1310 | atmci_writel(host, ATMCI_IER, iflags); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1311 | host->need_reset = false; |
| 1312 | } |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1313 | atmci_writel(host, ATMCI_SDCR, slot->sdc_reg); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1314 | |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1315 | iflags = atmci_readl(host, ATMCI_IMR); |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1316 | if (iflags & ~(ATMCI_SDIOIRQA | ATMCI_SDIOIRQB)) |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1317 | dev_dbg(&slot->mmc->class_dev, "WARNING: IMR=0x%08x\n", |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1318 | iflags); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1319 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1320 | if (unlikely(test_and_clear_bit(ATMCI_CARD_NEED_INIT, &slot->flags))) { |
| 1321 | /* Send init sequence (74 clock cycles) */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1322 | atmci_writel(host, ATMCI_CMDR, ATMCI_CMDR_SPCMD_INIT); |
| 1323 | while (!(atmci_readl(host, ATMCI_SR) & ATMCI_CMDRDY)) |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1324 | cpu_relax(); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1325 | } |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 1326 | iflags = 0; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1327 | data = mrq->data; |
| 1328 | if (data) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1329 | atmci_set_timeout(host, slot, data); |
Haavard Skinnemoen | a252e3e | 2008-10-03 14:46:17 +0200 | [diff] [blame] | 1330 | |
| 1331 | /* Must set block count/size before sending command */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1332 | atmci_writel(host, ATMCI_BLKR, ATMCI_BCNT(data->blocks) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1333 | | ATMCI_BLKLEN(data->blksz)); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1334 | dev_vdbg(&slot->mmc->class_dev, "BLKR=0x%08x\n", |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1335 | ATMCI_BCNT(data->blocks) | ATMCI_BLKLEN(data->blksz)); |
Nicolas Ferre | 74791a2 | 2009-12-14 18:01:31 -0800 | [diff] [blame] | 1336 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1337 | iflags |= host->prepare_data(host, data); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1338 | } |
| 1339 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1340 | iflags |= ATMCI_CMDRDY; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1341 | cmd = mrq->cmd; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1342 | cmdflags = atmci_prepare_command(slot->mmc, cmd); |
Ludovic Desroches | 66b512e | 2013-11-20 16:01:11 +0100 | [diff] [blame] | 1343 | |
| 1344 | /* |
| 1345 | * DMA transfer should be started before sending the command to avoid |
| 1346 | * unexpected errors especially for read operations in SDIO mode. |
| 1347 | * Unfortunately, in PDC mode, command has to be sent before starting |
| 1348 | * the transfer. |
| 1349 | */ |
| 1350 | if (host->submit_data != &atmci_submit_data_dma) |
| 1351 | atmci_send_command(host, cmd, cmdflags); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1352 | |
| 1353 | if (data) |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1354 | host->submit_data(host, data); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1355 | |
Ludovic Desroches | 66b512e | 2013-11-20 16:01:11 +0100 | [diff] [blame] | 1356 | if (host->submit_data == &atmci_submit_data_dma) |
| 1357 | atmci_send_command(host, cmd, cmdflags); |
| 1358 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1359 | if (mrq->stop) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1360 | host->stop_cmdr = atmci_prepare_command(slot->mmc, mrq->stop); |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1361 | host->stop_cmdr |= ATMCI_CMDR_STOP_XFER; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1362 | if (!(data->flags & MMC_DATA_WRITE)) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1363 | host->stop_cmdr |= ATMCI_CMDR_TRDIR_READ; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1364 | if (data->flags & MMC_DATA_STREAM) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1365 | host->stop_cmdr |= ATMCI_CMDR_STREAM; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1366 | else |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1367 | host->stop_cmdr |= ATMCI_CMDR_MULTI_BLOCK; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | * We could have enabled interrupts earlier, but I suspect |
| 1372 | * that would open up a nice can of interesting race |
| 1373 | * conditions (e.g. command and data complete, but stop not |
| 1374 | * prepared yet.) |
| 1375 | */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1376 | atmci_writel(host, ATMCI_IER, iflags); |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 1377 | |
| 1378 | mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000)); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1379 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1380 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1381 | static void atmci_queue_request(struct atmel_mci *host, |
| 1382 | struct atmel_mci_slot *slot, struct mmc_request *mrq) |
| 1383 | { |
| 1384 | dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n", |
| 1385 | host->state); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1386 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1387 | spin_lock_bh(&host->lock); |
| 1388 | slot->mrq = mrq; |
| 1389 | if (host->state == STATE_IDLE) { |
| 1390 | host->state = STATE_SENDING_CMD; |
| 1391 | atmci_start_request(host, slot); |
| 1392 | } else { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1393 | dev_dbg(&host->pdev->dev, "queue request\n"); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1394 | list_add_tail(&slot->queue_node, &host->queue); |
| 1395 | } |
| 1396 | spin_unlock_bh(&host->lock); |
| 1397 | } |
| 1398 | |
| 1399 | static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 1400 | { |
| 1401 | struct atmel_mci_slot *slot = mmc_priv(mmc); |
| 1402 | struct atmel_mci *host = slot->host; |
| 1403 | struct mmc_data *data; |
| 1404 | |
| 1405 | WARN_ON(slot->mrq); |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1406 | dev_dbg(&host->pdev->dev, "MRQ: cmd %u\n", mrq->cmd->opcode); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1407 | |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 1408 | pm_runtime_get_sync(&host->pdev->dev); |
| 1409 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1410 | /* |
| 1411 | * We may "know" the card is gone even though there's still an |
| 1412 | * electrical connection. If so, we really need to communicate |
| 1413 | * this to the MMC core since there won't be any more |
| 1414 | * interrupts as the card is completely removed. Otherwise, |
| 1415 | * the MMC core might believe the card is still there even |
| 1416 | * though the card was just removed very slowly. |
| 1417 | */ |
| 1418 | if (!test_bit(ATMCI_CARD_PRESENT, &slot->flags)) { |
| 1419 | mrq->cmd->error = -ENOMEDIUM; |
| 1420 | mmc_request_done(mmc, mrq); |
| 1421 | return; |
| 1422 | } |
| 1423 | |
| 1424 | /* We don't support multiple blocks of weird lengths. */ |
| 1425 | data = mrq->data; |
| 1426 | if (data && data->blocks > 1 && data->blksz & 3) { |
| 1427 | mrq->cmd->error = -EINVAL; |
| 1428 | mmc_request_done(mmc, mrq); |
| 1429 | } |
| 1430 | |
| 1431 | atmci_queue_request(host, slot, mrq); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1435 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1436 | struct atmel_mci_slot *slot = mmc_priv(mmc); |
| 1437 | struct atmel_mci *host = slot->host; |
| 1438 | unsigned int i; |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 1439 | |
| 1440 | pm_runtime_get_sync(&host->pdev->dev); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1441 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1442 | slot->sdc_reg &= ~ATMCI_SDCBUS_MASK; |
Haavard Skinnemoen | 945533b | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1443 | switch (ios->bus_width) { |
| 1444 | case MMC_BUS_WIDTH_1: |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1445 | slot->sdc_reg |= ATMCI_SDCBUS_1BIT; |
Haavard Skinnemoen | 945533b | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1446 | break; |
| 1447 | case MMC_BUS_WIDTH_4: |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1448 | slot->sdc_reg |= ATMCI_SDCBUS_4BIT; |
Haavard Skinnemoen | 945533b | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1449 | break; |
| 1450 | } |
| 1451 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1452 | if (ios->clock) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1453 | unsigned int clock_min = ~0U; |
Ludovic Desroches | 60c8f78 | 2015-05-06 15:16:46 +0200 | [diff] [blame] | 1454 | int clkdiv; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1455 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1456 | spin_lock_bh(&host->lock); |
| 1457 | if (!host->mode_reg) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1458 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
| 1459 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1460 | if (host->caps.has_cfg_reg) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1461 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1462 | } |
Haavard Skinnemoen | 945533b | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1463 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1464 | /* |
| 1465 | * Use mirror of ios->clock to prevent race with mmc |
| 1466 | * core ios update when finding the minimum. |
| 1467 | */ |
| 1468 | slot->clock = ios->clock; |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1469 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1470 | if (host->slot[i] && host->slot[i]->clock |
| 1471 | && host->slot[i]->clock < clock_min) |
| 1472 | clock_min = host->slot[i]->clock; |
| 1473 | } |
| 1474 | |
| 1475 | /* Calculate clock divider */ |
Ludovic Desroches | faf8180 | 2012-03-21 16:41:23 +0100 | [diff] [blame] | 1476 | if (host->caps.has_odd_clk_div) { |
| 1477 | clkdiv = DIV_ROUND_UP(host->bus_hz, clock_min) - 2; |
Ludovic Desroches | 60c8f78 | 2015-05-06 15:16:46 +0200 | [diff] [blame] | 1478 | if (clkdiv < 0) { |
| 1479 | dev_warn(&mmc->class_dev, |
| 1480 | "clock %u too fast; using %lu\n", |
| 1481 | clock_min, host->bus_hz / 2); |
| 1482 | clkdiv = 0; |
| 1483 | } else if (clkdiv > 511) { |
Ludovic Desroches | faf8180 | 2012-03-21 16:41:23 +0100 | [diff] [blame] | 1484 | dev_warn(&mmc->class_dev, |
| 1485 | "clock %u too slow; using %lu\n", |
| 1486 | clock_min, host->bus_hz / (511 + 2)); |
| 1487 | clkdiv = 511; |
| 1488 | } |
| 1489 | host->mode_reg = ATMCI_MR_CLKDIV(clkdiv >> 1) |
| 1490 | | ATMCI_MR_CLKODD(clkdiv & 1); |
| 1491 | } else { |
| 1492 | clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * clock_min) - 1; |
| 1493 | if (clkdiv > 255) { |
| 1494 | dev_warn(&mmc->class_dev, |
| 1495 | "clock %u too slow; using %lu\n", |
| 1496 | clock_min, host->bus_hz / (2 * 256)); |
| 1497 | clkdiv = 255; |
| 1498 | } |
| 1499 | host->mode_reg = ATMCI_MR_CLKDIV(clkdiv); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1500 | } |
| 1501 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1502 | /* |
| 1503 | * WRPROOF and RDPROOF prevent overruns/underruns by |
| 1504 | * stopping the clock when the FIFO is full/empty. |
| 1505 | * This state is not expected to last for long. |
| 1506 | */ |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1507 | if (host->caps.has_rwproof) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1508 | host->mode_reg |= (ATMCI_MR_WRPROOF | ATMCI_MR_RDPROOF); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1509 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1510 | if (host->caps.has_cfg_reg) { |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1511 | /* setup High Speed mode in relation with card capacity */ |
| 1512 | if (ios->timing == MMC_TIMING_SD_HS) |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1513 | host->cfg_reg |= ATMCI_CFG_HSMODE; |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1514 | else |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1515 | host->cfg_reg &= ~ATMCI_CFG_HSMODE; |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | if (list_empty(&host->queue)) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1519 | atmci_writel(host, ATMCI_MR, host->mode_reg); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1520 | if (host->caps.has_cfg_reg) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1521 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1522 | } else { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1523 | host->need_clock_update = true; |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1524 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1525 | |
| 1526 | spin_unlock_bh(&host->lock); |
Haavard Skinnemoen | 945533b | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1527 | } else { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1528 | bool any_slot_active = false; |
| 1529 | |
| 1530 | spin_lock_bh(&host->lock); |
| 1531 | slot->clock = 0; |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1532 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1533 | if (host->slot[i] && host->slot[i]->clock) { |
| 1534 | any_slot_active = true; |
| 1535 | break; |
| 1536 | } |
Haavard Skinnemoen | 945533b | 2008-10-03 17:48:16 +0200 | [diff] [blame] | 1537 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1538 | if (!any_slot_active) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1539 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1540 | if (host->mode_reg) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1541 | atmci_readl(host, ATMCI_MR); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1542 | } |
| 1543 | host->mode_reg = 0; |
| 1544 | } |
| 1545 | spin_unlock_bh(&host->lock); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | switch (ios->power_mode) { |
Alexandre Belloni | 9e7861f | 2013-10-17 12:46:48 +0200 | [diff] [blame] | 1549 | case MMC_POWER_OFF: |
| 1550 | if (!IS_ERR(mmc->supply.vmmc)) |
| 1551 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); |
| 1552 | break; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1553 | case MMC_POWER_UP: |
| 1554 | set_bit(ATMCI_CARD_NEED_INIT, &slot->flags); |
Alexandre Belloni | 9e7861f | 2013-10-17 12:46:48 +0200 | [diff] [blame] | 1555 | if (!IS_ERR(mmc->supply.vmmc)) |
| 1556 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1557 | break; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1558 | default: |
| 1559 | /* |
| 1560 | * TODO: None of the currently available AVR32-based |
| 1561 | * boards allow MMC power to be turned off. Implement |
| 1562 | * power control when this can be tested properly. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1563 | * |
| 1564 | * We also need to hook this into the clock management |
| 1565 | * somehow so that newly inserted cards aren't |
| 1566 | * subjected to a fast clock before we have a chance |
| 1567 | * to figure out what the maximum rate is. Currently, |
| 1568 | * there's no way to avoid this, and there never will |
| 1569 | * be for boards that don't support power control. |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1570 | */ |
| 1571 | break; |
| 1572 | } |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 1573 | |
| 1574 | pm_runtime_mark_last_busy(&host->pdev->dev); |
| 1575 | pm_runtime_put_autosuspend(&host->pdev->dev); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | static int atmci_get_ro(struct mmc_host *mmc) |
| 1579 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1580 | int read_only = -ENOSYS; |
| 1581 | struct atmel_mci_slot *slot = mmc_priv(mmc); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1582 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1583 | if (gpio_is_valid(slot->wp_pin)) { |
| 1584 | read_only = gpio_get_value(slot->wp_pin); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1585 | dev_dbg(&mmc->class_dev, "card is %s\n", |
| 1586 | read_only ? "read-only" : "read-write"); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | return read_only; |
| 1590 | } |
| 1591 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1592 | static int atmci_get_cd(struct mmc_host *mmc) |
| 1593 | { |
| 1594 | int present = -ENOSYS; |
| 1595 | struct atmel_mci_slot *slot = mmc_priv(mmc); |
| 1596 | |
| 1597 | if (gpio_is_valid(slot->detect_pin)) { |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 1598 | present = !(gpio_get_value(slot->detect_pin) ^ |
| 1599 | slot->detect_is_active_high); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1600 | dev_dbg(&mmc->class_dev, "card is %spresent\n", |
| 1601 | present ? "" : "not "); |
| 1602 | } |
| 1603 | |
| 1604 | return present; |
| 1605 | } |
| 1606 | |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1607 | static void atmci_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 1608 | { |
| 1609 | struct atmel_mci_slot *slot = mmc_priv(mmc); |
| 1610 | struct atmel_mci *host = slot->host; |
| 1611 | |
| 1612 | if (enable) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1613 | atmci_writel(host, ATMCI_IER, slot->sdio_irq); |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1614 | else |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1615 | atmci_writel(host, ATMCI_IDR, slot->sdio_irq); |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1618 | static const struct mmc_host_ops atmci_ops = { |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1619 | .request = atmci_request, |
| 1620 | .set_ios = atmci_set_ios, |
| 1621 | .get_ro = atmci_get_ro, |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1622 | .get_cd = atmci_get_cd, |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 1623 | .enable_sdio_irq = atmci_enable_sdio_irq, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1624 | }; |
| 1625 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1626 | /* Called with host->lock held */ |
| 1627 | static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq) |
| 1628 | __releases(&host->lock) |
| 1629 | __acquires(&host->lock) |
| 1630 | { |
| 1631 | struct atmel_mci_slot *slot = NULL; |
| 1632 | struct mmc_host *prev_mmc = host->cur_slot->mmc; |
| 1633 | |
| 1634 | WARN_ON(host->cmd || host->data); |
| 1635 | |
| 1636 | /* |
| 1637 | * Update the MMC clock rate if necessary. This may be |
| 1638 | * necessary if set_ios() is called when a different slot is |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1639 | * busy transferring data. |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1640 | */ |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1641 | if (host->need_clock_update) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1642 | atmci_writel(host, ATMCI_MR, host->mode_reg); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1643 | if (host->caps.has_cfg_reg) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1644 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1645 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1646 | |
| 1647 | host->cur_slot->mrq = NULL; |
| 1648 | host->mrq = NULL; |
| 1649 | if (!list_empty(&host->queue)) { |
| 1650 | slot = list_entry(host->queue.next, |
| 1651 | struct atmel_mci_slot, queue_node); |
| 1652 | list_del(&slot->queue_node); |
| 1653 | dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n", |
| 1654 | mmc_hostname(slot->mmc)); |
| 1655 | host->state = STATE_SENDING_CMD; |
| 1656 | atmci_start_request(host, slot); |
| 1657 | } else { |
| 1658 | dev_vdbg(&host->pdev->dev, "list empty\n"); |
| 1659 | host->state = STATE_IDLE; |
| 1660 | } |
| 1661 | |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 1662 | del_timer(&host->timer); |
| 1663 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1664 | spin_unlock(&host->lock); |
| 1665 | mmc_request_done(prev_mmc, mrq); |
| 1666 | spin_lock(&host->lock); |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 1667 | |
| 1668 | pm_runtime_mark_last_busy(&host->pdev->dev); |
| 1669 | pm_runtime_put_autosuspend(&host->pdev->dev); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1670 | } |
| 1671 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1672 | static void atmci_command_complete(struct atmel_mci *host, |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1673 | struct mmc_command *cmd) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1674 | { |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1675 | u32 status = host->cmd_status; |
| 1676 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1677 | /* Read the response from the card (up to 16 bytes) */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1678 | cmd->resp[0] = atmci_readl(host, ATMCI_RSPR); |
| 1679 | cmd->resp[1] = atmci_readl(host, ATMCI_RSPR); |
| 1680 | cmd->resp[2] = atmci_readl(host, ATMCI_RSPR); |
| 1681 | cmd->resp[3] = atmci_readl(host, ATMCI_RSPR); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1682 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1683 | if (status & ATMCI_RTOE) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1684 | cmd->error = -ETIMEDOUT; |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1685 | else if ((cmd->flags & MMC_RSP_CRC) && (status & ATMCI_RCRCE)) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1686 | cmd->error = -EILSEQ; |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 1687 | else if (status & (ATMCI_RINDE | ATMCI_RDIRE | ATMCI_RENDE)) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1688 | cmd->error = -EIO; |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 1689 | else if (host->mrq->data && (host->mrq->data->blksz & 3)) { |
| 1690 | if (host->caps.need_blksz_mul_4) { |
| 1691 | cmd->error = -EINVAL; |
| 1692 | host->need_reset = 1; |
| 1693 | } |
| 1694 | } else |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1695 | cmd->error = 0; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | static void atmci_detect_change(unsigned long data) |
| 1699 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1700 | struct atmel_mci_slot *slot = (struct atmel_mci_slot *)data; |
| 1701 | bool present; |
| 1702 | bool present_old; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1703 | |
| 1704 | /* |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1705 | * atmci_cleanup_slot() sets the ATMCI_SHUTDOWN flag before |
| 1706 | * freeing the interrupt. We must not re-enable the interrupt |
| 1707 | * if it has been freed, and if we're shutting down, it |
| 1708 | * doesn't really matter whether the card is present or not. |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1709 | */ |
| 1710 | smp_rmb(); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1711 | if (test_bit(ATMCI_SHUTDOWN, &slot->flags)) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1712 | return; |
| 1713 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1714 | enable_irq(gpio_to_irq(slot->detect_pin)); |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 1715 | present = !(gpio_get_value(slot->detect_pin) ^ |
| 1716 | slot->detect_is_active_high); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1717 | present_old = test_bit(ATMCI_CARD_PRESENT, &slot->flags); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1718 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1719 | dev_vdbg(&slot->mmc->class_dev, "detect change: %d (was %d)\n", |
| 1720 | present, present_old); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1721 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1722 | if (present != present_old) { |
| 1723 | struct atmel_mci *host = slot->host; |
| 1724 | struct mmc_request *mrq; |
| 1725 | |
| 1726 | dev_dbg(&slot->mmc->class_dev, "card %s\n", |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1727 | present ? "inserted" : "removed"); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1728 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1729 | spin_lock(&host->lock); |
| 1730 | |
| 1731 | if (!present) |
| 1732 | clear_bit(ATMCI_CARD_PRESENT, &slot->flags); |
| 1733 | else |
| 1734 | set_bit(ATMCI_CARD_PRESENT, &slot->flags); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1735 | |
| 1736 | /* Clean up queue if present */ |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1737 | mrq = slot->mrq; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1738 | if (mrq) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1739 | if (mrq == host->mrq) { |
| 1740 | /* |
| 1741 | * Reset controller to terminate any ongoing |
| 1742 | * commands or data transfers. |
| 1743 | */ |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1744 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
| 1745 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN); |
| 1746 | atmci_writel(host, ATMCI_MR, host->mode_reg); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1747 | if (host->caps.has_cfg_reg) |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1748 | atmci_writel(host, ATMCI_CFG, host->cfg_reg); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1749 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1750 | host->data = NULL; |
| 1751 | host->cmd = NULL; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1752 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1753 | switch (host->state) { |
| 1754 | case STATE_IDLE: |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1755 | break; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1756 | case STATE_SENDING_CMD: |
| 1757 | mrq->cmd->error = -ENOMEDIUM; |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1758 | if (mrq->data) |
| 1759 | host->stop_transfer(host); |
| 1760 | break; |
| 1761 | case STATE_DATA_XFER: |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1762 | mrq->data->error = -ENOMEDIUM; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 1763 | host->stop_transfer(host); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1764 | break; |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1765 | case STATE_WAITING_NOTBUSY: |
| 1766 | mrq->data->error = -ENOMEDIUM; |
| 1767 | break; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1768 | case STATE_SENDING_STOP: |
| 1769 | mrq->stop->error = -ENOMEDIUM; |
| 1770 | break; |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1771 | case STATE_END_REQUEST: |
| 1772 | break; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | atmci_request_end(host, mrq); |
| 1776 | } else { |
| 1777 | list_del(&slot->queue_node); |
| 1778 | mrq->cmd->error = -ENOMEDIUM; |
| 1779 | if (mrq->data) |
| 1780 | mrq->data->error = -ENOMEDIUM; |
| 1781 | if (mrq->stop) |
| 1782 | mrq->stop->error = -ENOMEDIUM; |
| 1783 | |
| 1784 | spin_unlock(&host->lock); |
| 1785 | mmc_request_done(slot->mmc, mrq); |
| 1786 | spin_lock(&host->lock); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1787 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1788 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1789 | spin_unlock(&host->lock); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1790 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1791 | mmc_detect_change(slot->mmc, 0); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | static void atmci_tasklet_func(unsigned long priv) |
| 1796 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1797 | struct atmel_mci *host = (struct atmel_mci *)priv; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1798 | struct mmc_request *mrq = host->mrq; |
| 1799 | struct mmc_data *data = host->data; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1800 | enum atmel_mci_state state = host->state; |
| 1801 | enum atmel_mci_state prev_state; |
| 1802 | u32 status; |
| 1803 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1804 | spin_lock(&host->lock); |
| 1805 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1806 | state = host->state; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1807 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1808 | dev_vdbg(&host->pdev->dev, |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1809 | "tasklet: state %u pending/completed/mask %lx/%lx/%x\n", |
| 1810 | state, host->pending_events, host->completed_events, |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 1811 | atmci_readl(host, ATMCI_IMR)); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1812 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1813 | do { |
| 1814 | prev_state = state; |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1815 | dev_dbg(&host->pdev->dev, "FSM: state=%d\n", state); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1816 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1817 | switch (state) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1818 | case STATE_IDLE: |
| 1819 | break; |
| 1820 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1821 | case STATE_SENDING_CMD: |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1822 | /* |
| 1823 | * Command has been sent, we are waiting for command |
| 1824 | * ready. Then we have three next states possible: |
| 1825 | * END_REQUEST by default, WAITING_NOTBUSY if it's a |
| 1826 | * command needing it or DATA_XFER if there is data. |
| 1827 | */ |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1828 | dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n"); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1829 | if (!atmci_test_and_clear_pending(host, |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1830 | EVENT_CMD_RDY)) |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1831 | break; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1832 | |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1833 | dev_dbg(&host->pdev->dev, "set completed cmd ready\n"); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1834 | host->cmd = NULL; |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1835 | atmci_set_completed(host, EVENT_CMD_RDY); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1836 | atmci_command_complete(host, mrq->cmd); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1837 | if (mrq->data) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1838 | dev_dbg(&host->pdev->dev, |
| 1839 | "command with data transfer"); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1840 | /* |
| 1841 | * If there is a command error don't start |
| 1842 | * data transfer. |
| 1843 | */ |
| 1844 | if (mrq->cmd->error) { |
| 1845 | host->stop_transfer(host); |
| 1846 | host->data = NULL; |
| 1847 | atmci_writel(host, ATMCI_IDR, |
| 1848 | ATMCI_TXRDY | ATMCI_RXRDY |
| 1849 | | ATMCI_DATA_ERROR_FLAGS); |
| 1850 | state = STATE_END_REQUEST; |
| 1851 | } else |
| 1852 | state = STATE_DATA_XFER; |
| 1853 | } else if ((!mrq->data) && (mrq->cmd->flags & MMC_RSP_BUSY)) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1854 | dev_dbg(&host->pdev->dev, |
| 1855 | "command response need waiting notbusy"); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1856 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
| 1857 | state = STATE_WAITING_NOTBUSY; |
| 1858 | } else |
| 1859 | state = STATE_END_REQUEST; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1860 | |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1861 | break; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1862 | |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1863 | case STATE_DATA_XFER: |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1864 | if (atmci_test_and_clear_pending(host, |
| 1865 | EVENT_DATA_ERROR)) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1866 | dev_dbg(&host->pdev->dev, "set completed data error\n"); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1867 | atmci_set_completed(host, EVENT_DATA_ERROR); |
| 1868 | state = STATE_END_REQUEST; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1869 | break; |
| 1870 | } |
| 1871 | |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1872 | /* |
| 1873 | * A data transfer is in progress. The event expected |
| 1874 | * to move to the next state depends of data transfer |
| 1875 | * type (PDC or DMA). Once transfer done we can move |
| 1876 | * to the next step which is WAITING_NOTBUSY in write |
| 1877 | * case and directly SENDING_STOP in read case. |
| 1878 | */ |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1879 | dev_dbg(&host->pdev->dev, "FSM: xfer complete?\n"); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1880 | if (!atmci_test_and_clear_pending(host, |
| 1881 | EVENT_XFER_COMPLETE)) |
| 1882 | break; |
| 1883 | |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1884 | dev_dbg(&host->pdev->dev, |
| 1885 | "(%s) set completed xfer complete\n", |
| 1886 | __func__); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1887 | atmci_set_completed(host, EVENT_XFER_COMPLETE); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1888 | |
Ludovic Desroches | 077d407 | 2012-07-24 11:42:04 +0200 | [diff] [blame] | 1889 | if (host->caps.need_notbusy_for_read_ops || |
| 1890 | (host->data->flags & MMC_DATA_WRITE)) { |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1891 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
| 1892 | state = STATE_WAITING_NOTBUSY; |
| 1893 | } else if (host->mrq->stop) { |
| 1894 | atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY); |
| 1895 | atmci_send_stop_cmd(host, data); |
| 1896 | state = STATE_SENDING_STOP; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1897 | } else { |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1898 | host->data = NULL; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1899 | data->bytes_xfered = data->blocks * data->blksz; |
| 1900 | data->error = 0; |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1901 | state = STATE_END_REQUEST; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1902 | } |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1903 | break; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1904 | |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1905 | case STATE_WAITING_NOTBUSY: |
| 1906 | /* |
| 1907 | * We can be in the state for two reasons: a command |
| 1908 | * requiring waiting not busy signal (stop command |
| 1909 | * included) or a write operation. In the latest case, |
| 1910 | * we need to send a stop command. |
| 1911 | */ |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1912 | dev_dbg(&host->pdev->dev, "FSM: not busy?\n"); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1913 | if (!atmci_test_and_clear_pending(host, |
| 1914 | EVENT_NOTBUSY)) |
| 1915 | break; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1916 | |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1917 | dev_dbg(&host->pdev->dev, "set completed not busy\n"); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1918 | atmci_set_completed(host, EVENT_NOTBUSY); |
| 1919 | |
| 1920 | if (host->data) { |
| 1921 | /* |
| 1922 | * For some commands such as CMD53, even if |
| 1923 | * there is data transfer, there is no stop |
| 1924 | * command to send. |
| 1925 | */ |
| 1926 | if (host->mrq->stop) { |
| 1927 | atmci_writel(host, ATMCI_IER, |
| 1928 | ATMCI_CMDRDY); |
| 1929 | atmci_send_stop_cmd(host, data); |
| 1930 | state = STATE_SENDING_STOP; |
| 1931 | } else { |
| 1932 | host->data = NULL; |
| 1933 | data->bytes_xfered = data->blocks |
| 1934 | * data->blksz; |
| 1935 | data->error = 0; |
| 1936 | state = STATE_END_REQUEST; |
| 1937 | } |
| 1938 | } else |
| 1939 | state = STATE_END_REQUEST; |
| 1940 | break; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1941 | |
| 1942 | case STATE_SENDING_STOP: |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1943 | /* |
| 1944 | * In this state, it is important to set host->data to |
| 1945 | * NULL (which is tested in the waiting notbusy state) |
| 1946 | * in order to go to the end request state instead of |
| 1947 | * sending stop again. |
| 1948 | */ |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1949 | dev_dbg(&host->pdev->dev, "FSM: cmd ready?\n"); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1950 | if (!atmci_test_and_clear_pending(host, |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1951 | EVENT_CMD_RDY)) |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1952 | break; |
| 1953 | |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 1954 | dev_dbg(&host->pdev->dev, "FSM: cmd ready\n"); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1955 | host->cmd = NULL; |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1956 | data->bytes_xfered = data->blocks * data->blksz; |
| 1957 | data->error = 0; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1958 | atmci_command_complete(host, mrq->stop); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1959 | if (mrq->stop->error) { |
| 1960 | host->stop_transfer(host); |
| 1961 | atmci_writel(host, ATMCI_IDR, |
| 1962 | ATMCI_TXRDY | ATMCI_RXRDY |
| 1963 | | ATMCI_DATA_ERROR_FLAGS); |
| 1964 | state = STATE_END_REQUEST; |
| 1965 | } else { |
| 1966 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
| 1967 | state = STATE_WAITING_NOTBUSY; |
| 1968 | } |
Nicolas Ferre | 41b4e9a | 2012-07-06 11:58:33 +0200 | [diff] [blame] | 1969 | host->data = NULL; |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1970 | break; |
| 1971 | |
| 1972 | case STATE_END_REQUEST: |
| 1973 | atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY | ATMCI_RXRDY |
| 1974 | | ATMCI_DATA_ERROR_FLAGS); |
| 1975 | status = host->data_status; |
| 1976 | if (unlikely(status)) { |
| 1977 | host->stop_transfer(host); |
| 1978 | host->data = NULL; |
Rodolfo Giometti | fbd986c | 2013-09-09 17:31:59 +0200 | [diff] [blame] | 1979 | if (data) { |
| 1980 | if (status & ATMCI_DTOE) { |
| 1981 | data->error = -ETIMEDOUT; |
| 1982 | } else if (status & ATMCI_DCRCE) { |
| 1983 | data->error = -EILSEQ; |
| 1984 | } else { |
| 1985 | data->error = -EIO; |
| 1986 | } |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1987 | } |
| 1988 | } |
| 1989 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1990 | atmci_request_end(host, host->mrq); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 1991 | state = STATE_IDLE; |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1992 | break; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1993 | } |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1994 | } while (state != prev_state); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1995 | |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 1996 | host->state = state; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1997 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 1998 | spin_unlock(&host->lock); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | static void atmci_read_data_pio(struct atmel_mci *host) |
| 2002 | { |
| 2003 | struct scatterlist *sg = host->sg; |
| 2004 | void *buf = sg_virt(sg); |
| 2005 | unsigned int offset = host->pio_offset; |
| 2006 | struct mmc_data *data = host->data; |
| 2007 | u32 value; |
| 2008 | u32 status; |
| 2009 | unsigned int nbytes = 0; |
| 2010 | |
| 2011 | do { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2012 | value = atmci_readl(host, ATMCI_RDR); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2013 | if (likely(offset + 4 <= sg->length)) { |
| 2014 | put_unaligned(value, (u32 *)(buf + offset)); |
| 2015 | |
| 2016 | offset += 4; |
| 2017 | nbytes += 4; |
| 2018 | |
| 2019 | if (offset == sg->length) { |
Haavard Skinnemoen | 5e7184a | 2008-10-05 15:27:50 +0200 | [diff] [blame] | 2020 | flush_dcache_page(sg_page(sg)); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2021 | host->sg = sg = sg_next(sg); |
Terry Barnaby | bdbc5d0 | 2013-04-08 12:05:47 -0400 | [diff] [blame] | 2022 | host->sg_len--; |
| 2023 | if (!sg || !host->sg_len) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2024 | goto done; |
| 2025 | |
| 2026 | offset = 0; |
| 2027 | buf = sg_virt(sg); |
| 2028 | } |
| 2029 | } else { |
| 2030 | unsigned int remaining = sg->length - offset; |
| 2031 | memcpy(buf + offset, &value, remaining); |
| 2032 | nbytes += remaining; |
| 2033 | |
| 2034 | flush_dcache_page(sg_page(sg)); |
| 2035 | host->sg = sg = sg_next(sg); |
Terry Barnaby | bdbc5d0 | 2013-04-08 12:05:47 -0400 | [diff] [blame] | 2036 | host->sg_len--; |
| 2037 | if (!sg || !host->sg_len) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2038 | goto done; |
| 2039 | |
| 2040 | offset = 4 - remaining; |
| 2041 | buf = sg_virt(sg); |
| 2042 | memcpy(buf, (u8 *)&value + remaining, offset); |
| 2043 | nbytes += offset; |
| 2044 | } |
| 2045 | |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2046 | status = atmci_readl(host, ATMCI_SR); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2047 | if (status & ATMCI_DATA_ERROR_FLAGS) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2048 | atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_RXRDY |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2049 | | ATMCI_DATA_ERROR_FLAGS)); |
| 2050 | host->data_status = status; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2051 | data->bytes_xfered += nbytes; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2052 | return; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2053 | } |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2054 | } while (status & ATMCI_RXRDY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2055 | |
| 2056 | host->pio_offset = offset; |
| 2057 | data->bytes_xfered += nbytes; |
| 2058 | |
| 2059 | return; |
| 2060 | |
| 2061 | done: |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2062 | atmci_writel(host, ATMCI_IDR, ATMCI_RXRDY); |
| 2063 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2064 | data->bytes_xfered += nbytes; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2065 | smp_wmb(); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 2066 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | static void atmci_write_data_pio(struct atmel_mci *host) |
| 2070 | { |
| 2071 | struct scatterlist *sg = host->sg; |
| 2072 | void *buf = sg_virt(sg); |
| 2073 | unsigned int offset = host->pio_offset; |
| 2074 | struct mmc_data *data = host->data; |
| 2075 | u32 value; |
| 2076 | u32 status; |
| 2077 | unsigned int nbytes = 0; |
| 2078 | |
| 2079 | do { |
| 2080 | if (likely(offset + 4 <= sg->length)) { |
| 2081 | value = get_unaligned((u32 *)(buf + offset)); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2082 | atmci_writel(host, ATMCI_TDR, value); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2083 | |
| 2084 | offset += 4; |
| 2085 | nbytes += 4; |
| 2086 | if (offset == sg->length) { |
| 2087 | host->sg = sg = sg_next(sg); |
Terry Barnaby | bdbc5d0 | 2013-04-08 12:05:47 -0400 | [diff] [blame] | 2088 | host->sg_len--; |
| 2089 | if (!sg || !host->sg_len) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2090 | goto done; |
| 2091 | |
| 2092 | offset = 0; |
| 2093 | buf = sg_virt(sg); |
| 2094 | } |
| 2095 | } else { |
| 2096 | unsigned int remaining = sg->length - offset; |
| 2097 | |
| 2098 | value = 0; |
| 2099 | memcpy(&value, buf + offset, remaining); |
| 2100 | nbytes += remaining; |
| 2101 | |
| 2102 | host->sg = sg = sg_next(sg); |
Terry Barnaby | bdbc5d0 | 2013-04-08 12:05:47 -0400 | [diff] [blame] | 2103 | host->sg_len--; |
| 2104 | if (!sg || !host->sg_len) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2105 | atmci_writel(host, ATMCI_TDR, value); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2106 | goto done; |
| 2107 | } |
| 2108 | |
| 2109 | offset = 4 - remaining; |
| 2110 | buf = sg_virt(sg); |
| 2111 | memcpy((u8 *)&value + remaining, buf, offset); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2112 | atmci_writel(host, ATMCI_TDR, value); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2113 | nbytes += offset; |
| 2114 | } |
| 2115 | |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2116 | status = atmci_readl(host, ATMCI_SR); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2117 | if (status & ATMCI_DATA_ERROR_FLAGS) { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2118 | atmci_writel(host, ATMCI_IDR, (ATMCI_NOTBUSY | ATMCI_TXRDY |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2119 | | ATMCI_DATA_ERROR_FLAGS)); |
| 2120 | host->data_status = status; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2121 | data->bytes_xfered += nbytes; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2122 | return; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2123 | } |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2124 | } while (status & ATMCI_TXRDY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2125 | |
| 2126 | host->pio_offset = offset; |
| 2127 | data->bytes_xfered += nbytes; |
| 2128 | |
| 2129 | return; |
| 2130 | |
| 2131 | done: |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2132 | atmci_writel(host, ATMCI_IDR, ATMCI_TXRDY); |
| 2133 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2134 | data->bytes_xfered += nbytes; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2135 | smp_wmb(); |
Haavard Skinnemoen | c06ad25 | 2008-07-31 14:49:16 +0200 | [diff] [blame] | 2136 | atmci_set_pending(host, EVENT_XFER_COMPLETE); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2137 | } |
| 2138 | |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 2139 | static void atmci_sdio_interrupt(struct atmel_mci *host, u32 status) |
| 2140 | { |
| 2141 | int i; |
| 2142 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2143 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 2144 | struct atmel_mci_slot *slot = host->slot[i]; |
| 2145 | if (slot && (status & slot->sdio_irq)) { |
| 2146 | mmc_signal_sdio_irq(slot->mmc); |
| 2147 | } |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2152 | static irqreturn_t atmci_interrupt(int irq, void *dev_id) |
| 2153 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2154 | struct atmel_mci *host = dev_id; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2155 | u32 status, mask, pending; |
| 2156 | unsigned int pass_count = 0; |
| 2157 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2158 | do { |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2159 | status = atmci_readl(host, ATMCI_SR); |
| 2160 | mask = atmci_readl(host, ATMCI_IMR); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2161 | pending = status & mask; |
| 2162 | if (!pending) |
| 2163 | break; |
| 2164 | |
| 2165 | if (pending & ATMCI_DATA_ERROR_FLAGS) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2166 | dev_dbg(&host->pdev->dev, "IRQ: data error\n"); |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2167 | atmci_writel(host, ATMCI_IDR, ATMCI_DATA_ERROR_FLAGS |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 2168 | | ATMCI_RXRDY | ATMCI_TXRDY |
| 2169 | | ATMCI_ENDRX | ATMCI_ENDTX |
| 2170 | | ATMCI_RXBUFF | ATMCI_TXBUFE); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2171 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2172 | host->data_status = status; |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2173 | dev_dbg(&host->pdev->dev, "set pending data error\n"); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2174 | smp_wmb(); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2175 | atmci_set_pending(host, EVENT_DATA_ERROR); |
| 2176 | tasklet_schedule(&host->tasklet); |
| 2177 | } |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2178 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2179 | if (pending & ATMCI_TXBUFE) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2180 | dev_dbg(&host->pdev->dev, "IRQ: tx buffer empty\n"); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2181 | atmci_writel(host, ATMCI_IDR, ATMCI_TXBUFE); |
Ludovic Desroches | 7e8ba22 | 2011-08-11 15:25:48 +0000 | [diff] [blame] | 2182 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2183 | /* |
| 2184 | * We can receive this interruption before having configured |
| 2185 | * the second pdc buffer, so we need to reconfigure first and |
| 2186 | * second buffers again |
| 2187 | */ |
| 2188 | if (host->data_size) { |
| 2189 | atmci_pdc_set_both_buf(host, XFER_TRANSMIT); |
Ludovic Desroches | 7e8ba22 | 2011-08-11 15:25:48 +0000 | [diff] [blame] | 2190 | atmci_writel(host, ATMCI_IER, ATMCI_ENDTX); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2191 | atmci_writel(host, ATMCI_IER, ATMCI_TXBUFE); |
| 2192 | } else { |
| 2193 | atmci_pdc_complete(host); |
| 2194 | } |
Ludovic Desroches | 7e8ba22 | 2011-08-11 15:25:48 +0000 | [diff] [blame] | 2195 | } else if (pending & ATMCI_ENDTX) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2196 | dev_dbg(&host->pdev->dev, "IRQ: end of tx buffer\n"); |
Ludovic Desroches | 7e8ba22 | 2011-08-11 15:25:48 +0000 | [diff] [blame] | 2197 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDTX); |
| 2198 | |
| 2199 | if (host->data_size) { |
| 2200 | atmci_pdc_set_single_buf(host, |
| 2201 | XFER_TRANSMIT, PDC_SECOND_BUF); |
| 2202 | atmci_writel(host, ATMCI_IER, ATMCI_ENDTX); |
| 2203 | } |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Ludovic Desroches | 7e8ba22 | 2011-08-11 15:25:48 +0000 | [diff] [blame] | 2206 | if (pending & ATMCI_RXBUFF) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2207 | dev_dbg(&host->pdev->dev, "IRQ: rx buffer full\n"); |
Ludovic Desroches | 7e8ba22 | 2011-08-11 15:25:48 +0000 | [diff] [blame] | 2208 | atmci_writel(host, ATMCI_IDR, ATMCI_RXBUFF); |
| 2209 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX); |
| 2210 | /* |
| 2211 | * We can receive this interruption before having configured |
| 2212 | * the second pdc buffer, so we need to reconfigure first and |
| 2213 | * second buffers again |
| 2214 | */ |
| 2215 | if (host->data_size) { |
| 2216 | atmci_pdc_set_both_buf(host, XFER_RECEIVE); |
| 2217 | atmci_writel(host, ATMCI_IER, ATMCI_ENDRX); |
| 2218 | atmci_writel(host, ATMCI_IER, ATMCI_RXBUFF); |
| 2219 | } else { |
| 2220 | atmci_pdc_complete(host); |
| 2221 | } |
| 2222 | } else if (pending & ATMCI_ENDRX) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2223 | dev_dbg(&host->pdev->dev, "IRQ: end of rx buffer\n"); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2224 | atmci_writel(host, ATMCI_IDR, ATMCI_ENDRX); |
| 2225 | |
| 2226 | if (host->data_size) { |
| 2227 | atmci_pdc_set_single_buf(host, |
| 2228 | XFER_RECEIVE, PDC_SECOND_BUF); |
| 2229 | atmci_writel(host, ATMCI_IER, ATMCI_ENDRX); |
| 2230 | } |
| 2231 | } |
| 2232 | |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 2233 | /* |
| 2234 | * First mci IPs, so mainly the ones having pdc, have some |
| 2235 | * issues with the notbusy signal. You can't get it after |
| 2236 | * data transmission if you have not sent a stop command. |
| 2237 | * The appropriate workaround is to use the BLKE signal. |
| 2238 | */ |
| 2239 | if (pending & ATMCI_BLKE) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2240 | dev_dbg(&host->pdev->dev, "IRQ: blke\n"); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 2241 | atmci_writel(host, ATMCI_IDR, ATMCI_BLKE); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2242 | smp_wmb(); |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2243 | dev_dbg(&host->pdev->dev, "set pending notbusy\n"); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 2244 | atmci_set_pending(host, EVENT_NOTBUSY); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2245 | tasklet_schedule(&host->tasklet); |
| 2246 | } |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 2247 | |
| 2248 | if (pending & ATMCI_NOTBUSY) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2249 | dev_dbg(&host->pdev->dev, "IRQ: not_busy\n"); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 2250 | atmci_writel(host, ATMCI_IDR, ATMCI_NOTBUSY); |
| 2251 | smp_wmb(); |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2252 | dev_dbg(&host->pdev->dev, "set pending notbusy\n"); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 2253 | atmci_set_pending(host, EVENT_NOTBUSY); |
| 2254 | tasklet_schedule(&host->tasklet); |
| 2255 | } |
| 2256 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2257 | if (pending & ATMCI_RXRDY) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2258 | atmci_read_data_pio(host); |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2259 | if (pending & ATMCI_TXRDY) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2260 | atmci_write_data_pio(host); |
| 2261 | |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 2262 | if (pending & ATMCI_CMDRDY) { |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2263 | dev_dbg(&host->pdev->dev, "IRQ: cmd ready\n"); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 2264 | atmci_writel(host, ATMCI_IDR, ATMCI_CMDRDY); |
| 2265 | host->cmd_status = status; |
| 2266 | smp_wmb(); |
Ludovic Desroches | 6801c41 | 2012-05-16 15:26:01 +0200 | [diff] [blame] | 2267 | dev_dbg(&host->pdev->dev, "set pending cmd rdy\n"); |
Ludovic Desroches | f517754 | 2012-05-16 15:25:59 +0200 | [diff] [blame] | 2268 | atmci_set_pending(host, EVENT_CMD_RDY); |
| 2269 | tasklet_schedule(&host->tasklet); |
| 2270 | } |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 2271 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2272 | if (pending & (ATMCI_SDIOIRQA | ATMCI_SDIOIRQB)) |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 2273 | atmci_sdio_interrupt(host, status); |
| 2274 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2275 | } while (pass_count++ < 5); |
| 2276 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2277 | return pass_count ? IRQ_HANDLED : IRQ_NONE; |
| 2278 | } |
| 2279 | |
| 2280 | static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id) |
| 2281 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2282 | struct atmel_mci_slot *slot = dev_id; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2283 | |
| 2284 | /* |
| 2285 | * Disable interrupts until the pin has stabilized and check |
| 2286 | * the state then. Use mod_timer() since we may be in the |
| 2287 | * middle of the timer routine when this interrupt triggers. |
| 2288 | */ |
| 2289 | disable_irq_nosync(irq); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2290 | mod_timer(&slot->detect_timer, jiffies + msecs_to_jiffies(20)); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2291 | |
| 2292 | return IRQ_HANDLED; |
| 2293 | } |
| 2294 | |
ludovic.desroches@atmel.com | ab050b9 | 2014-12-01 15:35:07 +0100 | [diff] [blame] | 2295 | static int atmci_init_slot(struct atmel_mci *host, |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2296 | struct mci_slot_pdata *slot_data, unsigned int id, |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 2297 | u32 sdc_reg, u32 sdio_irq) |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2298 | { |
| 2299 | struct mmc_host *mmc; |
| 2300 | struct atmel_mci_slot *slot; |
| 2301 | |
| 2302 | mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev); |
| 2303 | if (!mmc) |
| 2304 | return -ENOMEM; |
| 2305 | |
| 2306 | slot = mmc_priv(mmc); |
| 2307 | slot->mmc = mmc; |
| 2308 | slot->host = host; |
| 2309 | slot->detect_pin = slot_data->detect_pin; |
| 2310 | slot->wp_pin = slot_data->wp_pin; |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 2311 | slot->detect_is_active_high = slot_data->detect_is_active_high; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2312 | slot->sdc_reg = sdc_reg; |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 2313 | slot->sdio_irq = sdio_irq; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2314 | |
Ludovic Desroches | e919fd2 | 2012-07-24 15:30:03 +0200 | [diff] [blame] | 2315 | dev_dbg(&mmc->class_dev, |
| 2316 | "slot[%u]: bus_width=%u, detect_pin=%d, " |
| 2317 | "detect_is_active_high=%s, wp_pin=%d\n", |
| 2318 | id, slot_data->bus_width, slot_data->detect_pin, |
| 2319 | slot_data->detect_is_active_high ? "true" : "false", |
| 2320 | slot_data->wp_pin); |
| 2321 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2322 | mmc->ops = &atmci_ops; |
| 2323 | mmc->f_min = DIV_ROUND_UP(host->bus_hz, 512); |
| 2324 | mmc->f_max = host->bus_hz / 2; |
| 2325 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
Anders Grahn | 88ff82e | 2010-05-26 14:42:01 -0700 | [diff] [blame] | 2326 | if (sdio_irq) |
| 2327 | mmc->caps |= MMC_CAP_SDIO_IRQ; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2328 | if (host->caps.has_highspeed) |
Nicolas Ferre | 99ddffd | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 2329 | mmc->caps |= MMC_CAP_SD_HIGHSPEED; |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 2330 | /* |
| 2331 | * Without the read/write proof capability, it is strongly suggested to |
| 2332 | * use only one bit for data to prevent fifo underruns and overruns |
| 2333 | * which will corrupt data. |
| 2334 | */ |
| 2335 | if ((slot_data->bus_width >= 4) && host->caps.has_rwproof) |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2336 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
| 2337 | |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 2338 | if (atmci_get_version(host) < 0x200) { |
| 2339 | mmc->max_segs = 256; |
| 2340 | mmc->max_blk_size = 4095; |
| 2341 | mmc->max_blk_count = 256; |
| 2342 | mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; |
| 2343 | mmc->max_seg_size = mmc->max_blk_size * mmc->max_segs; |
| 2344 | } else { |
| 2345 | mmc->max_segs = 64; |
| 2346 | mmc->max_req_size = 32768 * 512; |
| 2347 | mmc->max_blk_size = 32768; |
| 2348 | mmc->max_blk_count = 512; |
| 2349 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2350 | |
| 2351 | /* Assume card is present initially */ |
| 2352 | set_bit(ATMCI_CARD_PRESENT, &slot->flags); |
| 2353 | if (gpio_is_valid(slot->detect_pin)) { |
Pramod Gurav | 7bca646 | 2014-09-23 18:21:48 +0530 | [diff] [blame] | 2354 | if (devm_gpio_request(&host->pdev->dev, slot->detect_pin, |
| 2355 | "mmc_detect")) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2356 | dev_dbg(&mmc->class_dev, "no detect pin available\n"); |
| 2357 | slot->detect_pin = -EBUSY; |
Jonas Larsson | 1c1452b | 2009-03-31 11:16:48 +0200 | [diff] [blame] | 2358 | } else if (gpio_get_value(slot->detect_pin) ^ |
| 2359 | slot->detect_is_active_high) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2360 | clear_bit(ATMCI_CARD_PRESENT, &slot->flags); |
| 2361 | } |
| 2362 | } |
| 2363 | |
Timo Kokkonen | 76d5556 | 2014-11-03 13:12:59 +0200 | [diff] [blame] | 2364 | if (!gpio_is_valid(slot->detect_pin)) { |
| 2365 | if (slot_data->non_removable) |
| 2366 | mmc->caps |= MMC_CAP_NONREMOVABLE; |
| 2367 | else |
| 2368 | mmc->caps |= MMC_CAP_NEEDS_POLL; |
| 2369 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2370 | |
| 2371 | if (gpio_is_valid(slot->wp_pin)) { |
Pramod Gurav | 7bca646 | 2014-09-23 18:21:48 +0530 | [diff] [blame] | 2372 | if (devm_gpio_request(&host->pdev->dev, slot->wp_pin, |
| 2373 | "mmc_wp")) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2374 | dev_dbg(&mmc->class_dev, "no WP pin available\n"); |
| 2375 | slot->wp_pin = -EBUSY; |
| 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | host->slot[id] = slot; |
Alexandre Belloni | 9e7861f | 2013-10-17 12:46:48 +0200 | [diff] [blame] | 2380 | mmc_regulator_get_supply(mmc); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2381 | mmc_add_host(mmc); |
| 2382 | |
| 2383 | if (gpio_is_valid(slot->detect_pin)) { |
| 2384 | int ret; |
| 2385 | |
| 2386 | setup_timer(&slot->detect_timer, atmci_detect_change, |
| 2387 | (unsigned long)slot); |
| 2388 | |
| 2389 | ret = request_irq(gpio_to_irq(slot->detect_pin), |
| 2390 | atmci_detect_interrupt, |
| 2391 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
| 2392 | "mmc-detect", slot); |
| 2393 | if (ret) { |
| 2394 | dev_dbg(&mmc->class_dev, |
| 2395 | "could not request IRQ %d for detect pin\n", |
| 2396 | gpio_to_irq(slot->detect_pin)); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2397 | slot->detect_pin = -EBUSY; |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | atmci_init_debugfs(slot); |
| 2402 | |
| 2403 | return 0; |
| 2404 | } |
| 2405 | |
Arnd Bergmann | 5fef365 | 2014-09-26 21:34:58 +0200 | [diff] [blame] | 2406 | static void atmci_cleanup_slot(struct atmel_mci_slot *slot, |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2407 | unsigned int id) |
| 2408 | { |
| 2409 | /* Debugfs stuff is cleaned up by mmc core */ |
| 2410 | |
| 2411 | set_bit(ATMCI_SHUTDOWN, &slot->flags); |
| 2412 | smp_wmb(); |
| 2413 | |
| 2414 | mmc_remove_host(slot->mmc); |
| 2415 | |
| 2416 | if (gpio_is_valid(slot->detect_pin)) { |
| 2417 | int pin = slot->detect_pin; |
| 2418 | |
| 2419 | free_irq(gpio_to_irq(pin), slot); |
| 2420 | del_timer_sync(&slot->detect_timer); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2421 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2422 | |
| 2423 | slot->host->slot[id] = NULL; |
| 2424 | mmc_free_host(slot->mmc); |
| 2425 | } |
| 2426 | |
ludovic.desroches@atmel.com | 467e081 | 2014-12-01 15:35:09 +0100 | [diff] [blame] | 2427 | static int atmci_configure_dma(struct atmel_mci *host) |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 2428 | { |
ludovic.desroches@atmel.com | 467e081 | 2014-12-01 15:35:09 +0100 | [diff] [blame] | 2429 | host->dma.chan = dma_request_slave_channel_reason(&host->pdev->dev, |
| 2430 | "rxtx"); |
| 2431 | if (IS_ERR(host->dma.chan)) |
| 2432 | return PTR_ERR(host->dma.chan); |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 2433 | |
ludovic.desroches@atmel.com | 467e081 | 2014-12-01 15:35:09 +0100 | [diff] [blame] | 2434 | dev_info(&host->pdev->dev, "using %s for DMA transfers\n", |
| 2435 | dma_chan_name(host->dma.chan)); |
Viresh Kumar | e2b35f3 | 2012-02-01 16:12:27 +0530 | [diff] [blame] | 2436 | |
ludovic.desroches@atmel.com | 467e081 | 2014-12-01 15:35:09 +0100 | [diff] [blame] | 2437 | host->dma_conf.src_addr = host->mapbase + ATMCI_RDR; |
| 2438 | host->dma_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 2439 | host->dma_conf.src_maxburst = 1; |
| 2440 | host->dma_conf.dst_addr = host->mapbase + ATMCI_TDR; |
| 2441 | host->dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 2442 | host->dma_conf.dst_maxburst = 1; |
| 2443 | host->dma_conf.device_fc = false; |
| 2444 | |
| 2445 | return 0; |
Nicolas Ferre | 2635d1b | 2009-12-14 18:01:30 -0800 | [diff] [blame] | 2446 | } |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2447 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2448 | /* |
| 2449 | * HSMCI (High Speed MCI) module is not fully compatible with MCI module. |
| 2450 | * HSMCI provides DMA support and a new config register but no more supports |
| 2451 | * PDC. |
| 2452 | */ |
ludovic.desroches@atmel.com | ab050b9 | 2014-12-01 15:35:07 +0100 | [diff] [blame] | 2453 | static void atmci_get_cap(struct atmel_mci *host) |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2454 | { |
| 2455 | unsigned int version; |
| 2456 | |
| 2457 | version = atmci_get_version(host); |
| 2458 | dev_info(&host->pdev->dev, |
| 2459 | "version: 0x%x\n", version); |
| 2460 | |
Hein_Tibosch | ccdfe61 | 2012-08-30 16:34:38 +0000 | [diff] [blame] | 2461 | host->caps.has_dma_conf_reg = 0; |
Hein_Tibosch | 6bf2af8 | 2012-08-30 16:34:27 +0000 | [diff] [blame] | 2462 | host->caps.has_pdc = ATMCI_PDC_CONNECTED; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2463 | host->caps.has_cfg_reg = 0; |
| 2464 | host->caps.has_cstor_reg = 0; |
| 2465 | host->caps.has_highspeed = 0; |
| 2466 | host->caps.has_rwproof = 0; |
Ludovic Desroches | faf8180 | 2012-03-21 16:41:23 +0100 | [diff] [blame] | 2467 | host->caps.has_odd_clk_div = 0; |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 2468 | host->caps.has_bad_data_ordering = 1; |
| 2469 | host->caps.need_reset_after_xfer = 1; |
| 2470 | host->caps.need_blksz_mul_4 = 1; |
Ludovic Desroches | 077d407 | 2012-07-24 11:42:04 +0200 | [diff] [blame] | 2471 | host->caps.need_notbusy_for_read_ops = 0; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2472 | |
| 2473 | /* keep only major version number */ |
| 2474 | switch (version & 0xf00) { |
Nicolas Ferre | 215ba39 | 2014-06-12 09:47:45 +0200 | [diff] [blame] | 2475 | case 0x600: |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2476 | case 0x500: |
Ludovic Desroches | faf8180 | 2012-03-21 16:41:23 +0100 | [diff] [blame] | 2477 | host->caps.has_odd_clk_div = 1; |
| 2478 | case 0x400: |
| 2479 | case 0x300: |
Hein_Tibosch | ccdfe61 | 2012-08-30 16:34:38 +0000 | [diff] [blame] | 2480 | host->caps.has_dma_conf_reg = 1; |
Ludovic Desroches | faf8180 | 2012-03-21 16:41:23 +0100 | [diff] [blame] | 2481 | host->caps.has_pdc = 0; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2482 | host->caps.has_cfg_reg = 1; |
| 2483 | host->caps.has_cstor_reg = 1; |
| 2484 | host->caps.has_highspeed = 1; |
Ludovic Desroches | faf8180 | 2012-03-21 16:41:23 +0100 | [diff] [blame] | 2485 | case 0x200: |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2486 | host->caps.has_rwproof = 1; |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 2487 | host->caps.need_blksz_mul_4 = 0; |
Ludovic Desroches | 077d407 | 2012-07-24 11:42:04 +0200 | [diff] [blame] | 2488 | host->caps.need_notbusy_for_read_ops = 1; |
Ludovic Desroches | faf8180 | 2012-03-21 16:41:23 +0100 | [diff] [blame] | 2489 | case 0x100: |
Ludovic Desroches | 24011f3 | 2012-05-16 15:26:00 +0200 | [diff] [blame] | 2490 | host->caps.has_bad_data_ordering = 0; |
| 2491 | host->caps.need_reset_after_xfer = 0; |
| 2492 | case 0x0: |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2493 | break; |
| 2494 | default: |
Ludovic Desroches | faf8180 | 2012-03-21 16:41:23 +0100 | [diff] [blame] | 2495 | host->caps.has_pdc = 0; |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2496 | dev_warn(&host->pdev->dev, |
| 2497 | "Unmanaged mci version, set minimum capabilities\n"); |
| 2498 | break; |
| 2499 | } |
| 2500 | } |
Dan Williams | 74465b4 | 2009-01-06 11:38:16 -0700 | [diff] [blame] | 2501 | |
ludovic.desroches@atmel.com | ab050b9 | 2014-12-01 15:35:07 +0100 | [diff] [blame] | 2502 | static int atmci_probe(struct platform_device *pdev) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2503 | { |
| 2504 | struct mci_platform_data *pdata; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2505 | struct atmel_mci *host; |
| 2506 | struct resource *regs; |
| 2507 | unsigned int nr_slots; |
| 2508 | int irq; |
Pramod Gurav | 528bc78 | 2014-09-23 15:50:06 +0530 | [diff] [blame] | 2509 | int ret, i; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2510 | |
| 2511 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2512 | if (!regs) |
| 2513 | return -ENXIO; |
| 2514 | pdata = pdev->dev.platform_data; |
Ludovic Desroches | e919fd2 | 2012-07-24 15:30:03 +0200 | [diff] [blame] | 2515 | if (!pdata) { |
| 2516 | pdata = atmci_of_init(pdev); |
| 2517 | if (IS_ERR(pdata)) { |
| 2518 | dev_err(&pdev->dev, "platform data not available\n"); |
| 2519 | return PTR_ERR(pdata); |
| 2520 | } |
| 2521 | } |
| 2522 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2523 | irq = platform_get_irq(pdev, 0); |
| 2524 | if (irq < 0) |
| 2525 | return irq; |
| 2526 | |
Pramod Gurav | 7bca646 | 2014-09-23 18:21:48 +0530 | [diff] [blame] | 2527 | host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2528 | if (!host) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2529 | return -ENOMEM; |
| 2530 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2531 | host->pdev = pdev; |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2532 | spin_lock_init(&host->lock); |
| 2533 | INIT_LIST_HEAD(&host->queue); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2534 | |
Pramod Gurav | 7bca646 | 2014-09-23 18:21:48 +0530 | [diff] [blame] | 2535 | host->mck = devm_clk_get(&pdev->dev, "mci_clk"); |
| 2536 | if (IS_ERR(host->mck)) |
| 2537 | return PTR_ERR(host->mck); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2538 | |
Pramod Gurav | 7bca646 | 2014-09-23 18:21:48 +0530 | [diff] [blame] | 2539 | host->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs)); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2540 | if (!host->regs) |
Pramod Gurav | 7bca646 | 2014-09-23 18:21:48 +0530 | [diff] [blame] | 2541 | return -ENOMEM; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2542 | |
Boris BREZILLON | b3894f2 | 2013-07-18 09:38:52 +0200 | [diff] [blame] | 2543 | ret = clk_prepare_enable(host->mck); |
| 2544 | if (ret) |
Pramod Gurav | 7bca646 | 2014-09-23 18:21:48 +0530 | [diff] [blame] | 2545 | return ret; |
| 2546 | |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2547 | atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2548 | host->bus_hz = clk_get_rate(host->mck); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2549 | |
| 2550 | host->mapbase = regs->start; |
| 2551 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2552 | tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2553 | |
Kay Sievers | 89c8aa2 | 2009-02-02 21:08:30 +0100 | [diff] [blame] | 2554 | ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host); |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2555 | if (ret) { |
| 2556 | clk_disable_unprepare(host->mck); |
Pramod Gurav | 7bca646 | 2014-09-23 18:21:48 +0530 | [diff] [blame] | 2557 | return ret; |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2558 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2559 | |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2560 | /* Get MCI capabilities and set operations according to it */ |
| 2561 | atmci_get_cap(host); |
ludovic.desroches@atmel.com | 467e081 | 2014-12-01 15:35:09 +0100 | [diff] [blame] | 2562 | ret = atmci_configure_dma(host); |
| 2563 | if (ret == -EPROBE_DEFER) |
| 2564 | goto err_dma_probe_defer; |
| 2565 | if (ret == 0) { |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2566 | host->prepare_data = &atmci_prepare_data_dma; |
| 2567 | host->submit_data = &atmci_submit_data_dma; |
| 2568 | host->stop_transfer = &atmci_stop_transfer_dma; |
| 2569 | } else if (host->caps.has_pdc) { |
| 2570 | dev_info(&pdev->dev, "using PDC\n"); |
| 2571 | host->prepare_data = &atmci_prepare_data_pdc; |
| 2572 | host->submit_data = &atmci_submit_data_pdc; |
| 2573 | host->stop_transfer = &atmci_stop_transfer_pdc; |
| 2574 | } else { |
Ludovic Desroches | ef87819 | 2012-02-09 16:33:53 +0100 | [diff] [blame] | 2575 | dev_info(&pdev->dev, "using PIO\n"); |
Ludovic Desroches | 796211b | 2011-08-11 15:25:44 +0000 | [diff] [blame] | 2576 | host->prepare_data = &atmci_prepare_data; |
| 2577 | host->submit_data = &atmci_submit_data; |
| 2578 | host->stop_transfer = &atmci_stop_transfer; |
| 2579 | } |
| 2580 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2581 | platform_set_drvdata(pdev, host); |
| 2582 | |
Ludovic Desroches | b87cc1b | 2012-05-23 15:52:15 +0200 | [diff] [blame] | 2583 | setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host); |
| 2584 | |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2585 | pm_runtime_get_noresume(&pdev->dev); |
| 2586 | pm_runtime_set_active(&pdev->dev); |
| 2587 | pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_DELAY); |
| 2588 | pm_runtime_use_autosuspend(&pdev->dev); |
| 2589 | pm_runtime_enable(&pdev->dev); |
| 2590 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2591 | /* We need at least one slot to succeed */ |
| 2592 | nr_slots = 0; |
| 2593 | ret = -ENODEV; |
| 2594 | if (pdata->slot[0].bus_width) { |
| 2595 | ret = atmci_init_slot(host, &pdata->slot[0], |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2596 | 0, ATMCI_SDCSEL_SLOT_A, ATMCI_SDIOIRQA); |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 2597 | if (!ret) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2598 | nr_slots++; |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 2599 | host->buf_size = host->slot[0]->mmc->max_req_size; |
| 2600 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2601 | } |
| 2602 | if (pdata->slot[1].bus_width) { |
| 2603 | ret = atmci_init_slot(host, &pdata->slot[1], |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2604 | 1, ATMCI_SDCSEL_SLOT_B, ATMCI_SDIOIRQB); |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 2605 | if (!ret) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2606 | nr_slots++; |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 2607 | if (host->slot[1]->mmc->max_req_size > host->buf_size) |
| 2608 | host->buf_size = |
| 2609 | host->slot[1]->mmc->max_req_size; |
| 2610 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2611 | } |
| 2612 | |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 2613 | if (!nr_slots) { |
| 2614 | dev_err(&pdev->dev, "init failed: no slot defined\n"); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2615 | goto err_init_slot; |
Rob Emanuele | 04d699c | 2009-09-22 16:45:19 -0700 | [diff] [blame] | 2616 | } |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2617 | |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 2618 | if (!host->caps.has_rwproof) { |
| 2619 | host->buffer = dma_alloc_coherent(&pdev->dev, host->buf_size, |
| 2620 | &host->buf_phys_addr, |
| 2621 | GFP_KERNEL); |
| 2622 | if (!host->buffer) { |
| 2623 | ret = -ENOMEM; |
| 2624 | dev_err(&pdev->dev, "buffer allocation failed\n"); |
Pramod Gurav | 528bc78 | 2014-09-23 15:50:06 +0530 | [diff] [blame] | 2625 | goto err_dma_alloc; |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 2626 | } |
| 2627 | } |
| 2628 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2629 | dev_info(&pdev->dev, |
| 2630 | "Atmel MCI controller at 0x%08lx irq %d, %u slots\n", |
| 2631 | host->mapbase, irq, nr_slots); |
Haavard Skinnemoen | deec9ae | 2008-07-24 14:18:59 +0200 | [diff] [blame] | 2632 | |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2633 | pm_runtime_mark_last_busy(&host->pdev->dev); |
| 2634 | pm_runtime_put_autosuspend(&pdev->dev); |
| 2635 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2636 | return 0; |
| 2637 | |
Pramod Gurav | 528bc78 | 2014-09-23 15:50:06 +0530 | [diff] [blame] | 2638 | err_dma_alloc: |
| 2639 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
| 2640 | if (host->slot[i]) |
| 2641 | atmci_cleanup_slot(host->slot[i], i); |
| 2642 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2643 | err_init_slot: |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2644 | clk_disable_unprepare(host->mck); |
| 2645 | |
| 2646 | pm_runtime_disable(&pdev->dev); |
| 2647 | pm_runtime_put_noidle(&pdev->dev); |
| 2648 | |
Pramod Gurav | 528bc78 | 2014-09-23 15:50:06 +0530 | [diff] [blame] | 2649 | del_timer_sync(&host->timer); |
ludovic.desroches@atmel.com | 467e081 | 2014-12-01 15:35:09 +0100 | [diff] [blame] | 2650 | if (!IS_ERR(host->dma.chan)) |
Dan Williams | 74465b4 | 2009-01-06 11:38:16 -0700 | [diff] [blame] | 2651 | dma_release_channel(host->dma.chan); |
ludovic.desroches@atmel.com | 467e081 | 2014-12-01 15:35:09 +0100 | [diff] [blame] | 2652 | err_dma_probe_defer: |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2653 | free_irq(irq, host); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2654 | return ret; |
| 2655 | } |
| 2656 | |
ludovic.desroches@atmel.com | ab050b9 | 2014-12-01 15:35:07 +0100 | [diff] [blame] | 2657 | static int atmci_remove(struct platform_device *pdev) |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2658 | { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2659 | struct atmel_mci *host = platform_get_drvdata(pdev); |
| 2660 | unsigned int i; |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2661 | |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2662 | pm_runtime_get_sync(&pdev->dev); |
| 2663 | |
Ludovic Desroches | 7a90dcc | 2012-05-16 15:25:58 +0200 | [diff] [blame] | 2664 | if (host->buffer) |
| 2665 | dma_free_coherent(&pdev->dev, host->buf_size, |
| 2666 | host->buffer, host->buf_phys_addr); |
| 2667 | |
Ludovic Desroches | 2c96a29 | 2011-08-11 15:25:41 +0000 | [diff] [blame] | 2668 | for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2669 | if (host->slot[i]) |
| 2670 | atmci_cleanup_slot(host->slot[i], i); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2671 | } |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2672 | |
Ludovic Desroches | 03fc9a7 | 2011-08-11 15:25:42 +0000 | [diff] [blame] | 2673 | atmci_writel(host, ATMCI_IDR, ~0UL); |
| 2674 | atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS); |
| 2675 | atmci_readl(host, ATMCI_SR); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2676 | |
Pramod Gurav | 528bc78 | 2014-09-23 15:50:06 +0530 | [diff] [blame] | 2677 | del_timer_sync(&host->timer); |
ludovic.desroches@atmel.com | 467e081 | 2014-12-01 15:35:09 +0100 | [diff] [blame] | 2678 | if (!IS_ERR(host->dma.chan)) |
Dan Williams | 74465b4 | 2009-01-06 11:38:16 -0700 | [diff] [blame] | 2679 | dma_release_channel(host->dma.chan); |
Haavard Skinnemoen | 65e8b08 | 2008-07-30 20:29:03 +0200 | [diff] [blame] | 2680 | |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2681 | free_irq(platform_get_irq(pdev, 0), host); |
Haavard Skinnemoen | 965ebf3 | 2008-09-17 20:53:55 +0200 | [diff] [blame] | 2682 | |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2683 | clk_disable_unprepare(host->mck); |
| 2684 | |
| 2685 | pm_runtime_disable(&pdev->dev); |
| 2686 | pm_runtime_put_noidle(&pdev->dev); |
| 2687 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2688 | return 0; |
| 2689 | } |
| 2690 | |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2691 | #ifdef CONFIG_PM |
| 2692 | static int atmci_runtime_suspend(struct device *dev) |
| 2693 | { |
| 2694 | struct atmel_mci *host = dev_get_drvdata(dev); |
| 2695 | |
| 2696 | clk_disable_unprepare(host->mck); |
| 2697 | |
Wenyou Yang | b5b64fa | 2014-11-07 08:48:13 +0800 | [diff] [blame] | 2698 | pinctrl_pm_select_sleep_state(dev); |
| 2699 | |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2700 | return 0; |
| 2701 | } |
| 2702 | |
| 2703 | static int atmci_runtime_resume(struct device *dev) |
| 2704 | { |
| 2705 | struct atmel_mci *host = dev_get_drvdata(dev); |
| 2706 | |
Wenyou Yang | b5b64fa | 2014-11-07 08:48:13 +0800 | [diff] [blame] | 2707 | pinctrl_pm_select_default_state(dev); |
| 2708 | |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2709 | return clk_prepare_enable(host->mck); |
| 2710 | } |
| 2711 | #endif |
| 2712 | |
| 2713 | static const struct dev_pm_ops atmci_dev_pm_ops = { |
| 2714 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 2715 | pm_runtime_force_resume) |
Ludovic Desroches | c3cb6ba | 2014-12-13 00:44:11 +0100 | [diff] [blame] | 2716 | SET_RUNTIME_PM_OPS(atmci_runtime_suspend, atmci_runtime_resume, NULL) |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2717 | }; |
| 2718 | |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2719 | static struct platform_driver atmci_driver = { |
ludovic.desroches@atmel.com | 5e0fe89 | 2014-12-01 15:35:08 +0100 | [diff] [blame] | 2720 | .probe = atmci_probe, |
ludovic.desroches@atmel.com | ab050b9 | 2014-12-01 15:35:07 +0100 | [diff] [blame] | 2721 | .remove = atmci_remove, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2722 | .driver = { |
| 2723 | .name = "atmel_mci", |
Ludovic Desroches | e919fd2 | 2012-07-24 15:30:03 +0200 | [diff] [blame] | 2724 | .of_match_table = of_match_ptr(atmci_dt_ids), |
Wenyou Yang | ae552ab | 2014-10-30 12:00:41 +0800 | [diff] [blame] | 2725 | .pm = &atmci_dev_pm_ops, |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2726 | }, |
| 2727 | }; |
ludovic.desroches@atmel.com | 5e0fe89 | 2014-12-01 15:35:08 +0100 | [diff] [blame] | 2728 | module_platform_driver(atmci_driver); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2729 | |
| 2730 | MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver"); |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 2731 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Haavard Skinnemoen | 7d2be07 | 2008-06-30 18:35:03 +0200 | [diff] [blame] | 2732 | MODULE_LICENSE("GPL v2"); |