Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * MMCIF eMMC driver. |
| 3 | * |
| 4 | * Copyright (C) 2010 Renesas Solutions Corp. |
| 5 | * Yusuke Goda <yusuke.goda.sx@renesas.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License. |
| 10 | * |
| 11 | * |
| 12 | * TODO |
| 13 | * 1. DMA |
| 14 | * 2. Power management |
| 15 | * 3. Handle MMC errors better |
| 16 | * |
| 17 | */ |
| 18 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 19 | /* |
| 20 | * The MMCIF driver is now processing MMC requests asynchronously, according |
| 21 | * to the Linux MMC API requirement. |
| 22 | * |
| 23 | * The MMCIF driver processes MMC requests in up to 3 stages: command, optional |
| 24 | * data, and optional stop. To achieve asynchronous processing each of these |
| 25 | * stages is split into two halves: a top and a bottom half. The top half |
| 26 | * initialises the hardware, installs a timeout handler to handle completion |
| 27 | * timeouts, and returns. In case of the command stage this immediately returns |
| 28 | * control to the caller, leaving all further processing to run asynchronously. |
| 29 | * All further request processing is performed by the bottom halves. |
| 30 | * |
| 31 | * The bottom half further consists of a "hard" IRQ handler, an IRQ handler |
| 32 | * thread, a DMA completion callback, if DMA is used, a timeout work, and |
| 33 | * request- and stage-specific handler methods. |
| 34 | * |
| 35 | * Each bottom half run begins with either a hardware interrupt, a DMA callback |
| 36 | * invocation, or a timeout work run. In case of an error or a successful |
| 37 | * processing completion, the MMC core is informed and the request processing is |
| 38 | * finished. In case processing has to continue, i.e., if data has to be read |
| 39 | * from or written to the card, or if a stop command has to be sent, the next |
| 40 | * top half is called, which performs the necessary hardware handling and |
| 41 | * reschedules the timeout work. This returns the driver state machine into the |
| 42 | * bottom half waiting state. |
| 43 | */ |
| 44 | |
Guennadi Liakhovetski | 86df174 | 2011-11-23 15:52:30 +0100 | [diff] [blame] | 45 | #include <linux/bitops.h> |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 46 | #include <linux/clk.h> |
| 47 | #include <linux/completion.h> |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 48 | #include <linux/delay.h> |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 49 | #include <linux/dma-mapping.h> |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 50 | #include <linux/dmaengine.h> |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 51 | #include <linux/mmc/card.h> |
| 52 | #include <linux/mmc/core.h> |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 53 | #include <linux/mmc/host.h> |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 54 | #include <linux/mmc/mmc.h> |
| 55 | #include <linux/mmc/sdio.h> |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 56 | #include <linux/mmc/sh_mmcif.h> |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 57 | #include <linux/pagemap.h> |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 58 | #include <linux/platform_device.h> |
Rafael J. Wysocki | efe6a8a | 2012-03-13 01:02:15 +0100 | [diff] [blame] | 59 | #include <linux/pm_qos.h> |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 60 | #include <linux/pm_runtime.h> |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 61 | #include <linux/spinlock.h> |
Paul Gortmaker | 88b4767 | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 62 | #include <linux/module.h> |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 63 | |
| 64 | #define DRIVER_NAME "sh_mmcif" |
| 65 | #define DRIVER_VERSION "2010-04-28" |
| 66 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 67 | /* CE_CMD_SET */ |
| 68 | #define CMD_MASK 0x3f000000 |
| 69 | #define CMD_SET_RTYP_NO ((0 << 23) | (0 << 22)) |
| 70 | #define CMD_SET_RTYP_6B ((0 << 23) | (1 << 22)) /* R1/R1b/R3/R4/R5 */ |
| 71 | #define CMD_SET_RTYP_17B ((1 << 23) | (0 << 22)) /* R2 */ |
| 72 | #define CMD_SET_RBSY (1 << 21) /* R1b */ |
| 73 | #define CMD_SET_CCSEN (1 << 20) |
| 74 | #define CMD_SET_WDAT (1 << 19) /* 1: on data, 0: no data */ |
| 75 | #define CMD_SET_DWEN (1 << 18) /* 1: write, 0: read */ |
| 76 | #define CMD_SET_CMLTE (1 << 17) /* 1: multi block trans, 0: single */ |
| 77 | #define CMD_SET_CMD12EN (1 << 16) /* 1: CMD12 auto issue */ |
| 78 | #define CMD_SET_RIDXC_INDEX ((0 << 15) | (0 << 14)) /* index check */ |
| 79 | #define CMD_SET_RIDXC_BITS ((0 << 15) | (1 << 14)) /* check bits check */ |
| 80 | #define CMD_SET_RIDXC_NO ((1 << 15) | (0 << 14)) /* no check */ |
| 81 | #define CMD_SET_CRC7C ((0 << 13) | (0 << 12)) /* CRC7 check*/ |
| 82 | #define CMD_SET_CRC7C_BITS ((0 << 13) | (1 << 12)) /* check bits check*/ |
| 83 | #define CMD_SET_CRC7C_INTERNAL ((1 << 13) | (0 << 12)) /* internal CRC7 check*/ |
| 84 | #define CMD_SET_CRC16C (1 << 10) /* 0: CRC16 check*/ |
| 85 | #define CMD_SET_CRCSTE (1 << 8) /* 1: not receive CRC status */ |
| 86 | #define CMD_SET_TBIT (1 << 7) /* 1: tran mission bit "Low" */ |
| 87 | #define CMD_SET_OPDM (1 << 6) /* 1: open/drain */ |
| 88 | #define CMD_SET_CCSH (1 << 5) |
| 89 | #define CMD_SET_DATW_1 ((0 << 1) | (0 << 0)) /* 1bit */ |
| 90 | #define CMD_SET_DATW_4 ((0 << 1) | (1 << 0)) /* 4bit */ |
| 91 | #define CMD_SET_DATW_8 ((1 << 1) | (0 << 0)) /* 8bit */ |
| 92 | |
| 93 | /* CE_CMD_CTRL */ |
| 94 | #define CMD_CTRL_BREAK (1 << 0) |
| 95 | |
| 96 | /* CE_BLOCK_SET */ |
| 97 | #define BLOCK_SIZE_MASK 0x0000ffff |
| 98 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 99 | /* CE_INT */ |
| 100 | #define INT_CCSDE (1 << 29) |
| 101 | #define INT_CMD12DRE (1 << 26) |
| 102 | #define INT_CMD12RBE (1 << 25) |
| 103 | #define INT_CMD12CRE (1 << 24) |
| 104 | #define INT_DTRANE (1 << 23) |
| 105 | #define INT_BUFRE (1 << 22) |
| 106 | #define INT_BUFWEN (1 << 21) |
| 107 | #define INT_BUFREN (1 << 20) |
| 108 | #define INT_CCSRCV (1 << 19) |
| 109 | #define INT_RBSYE (1 << 17) |
| 110 | #define INT_CRSPE (1 << 16) |
| 111 | #define INT_CMDVIO (1 << 15) |
| 112 | #define INT_BUFVIO (1 << 14) |
| 113 | #define INT_WDATERR (1 << 11) |
| 114 | #define INT_RDATERR (1 << 10) |
| 115 | #define INT_RIDXERR (1 << 9) |
| 116 | #define INT_RSPERR (1 << 8) |
| 117 | #define INT_CCSTO (1 << 5) |
| 118 | #define INT_CRCSTO (1 << 4) |
| 119 | #define INT_WDATTO (1 << 3) |
| 120 | #define INT_RDATTO (1 << 2) |
| 121 | #define INT_RBSYTO (1 << 1) |
| 122 | #define INT_RSPTO (1 << 0) |
| 123 | #define INT_ERR_STS (INT_CMDVIO | INT_BUFVIO | INT_WDATERR | \ |
| 124 | INT_RDATERR | INT_RIDXERR | INT_RSPERR | \ |
| 125 | INT_CCSTO | INT_CRCSTO | INT_WDATTO | \ |
| 126 | INT_RDATTO | INT_RBSYTO | INT_RSPTO) |
| 127 | |
| 128 | /* CE_INT_MASK */ |
| 129 | #define MASK_ALL 0x00000000 |
| 130 | #define MASK_MCCSDE (1 << 29) |
| 131 | #define MASK_MCMD12DRE (1 << 26) |
| 132 | #define MASK_MCMD12RBE (1 << 25) |
| 133 | #define MASK_MCMD12CRE (1 << 24) |
| 134 | #define MASK_MDTRANE (1 << 23) |
| 135 | #define MASK_MBUFRE (1 << 22) |
| 136 | #define MASK_MBUFWEN (1 << 21) |
| 137 | #define MASK_MBUFREN (1 << 20) |
| 138 | #define MASK_MCCSRCV (1 << 19) |
| 139 | #define MASK_MRBSYE (1 << 17) |
| 140 | #define MASK_MCRSPE (1 << 16) |
| 141 | #define MASK_MCMDVIO (1 << 15) |
| 142 | #define MASK_MBUFVIO (1 << 14) |
| 143 | #define MASK_MWDATERR (1 << 11) |
| 144 | #define MASK_MRDATERR (1 << 10) |
| 145 | #define MASK_MRIDXERR (1 << 9) |
| 146 | #define MASK_MRSPERR (1 << 8) |
| 147 | #define MASK_MCCSTO (1 << 5) |
| 148 | #define MASK_MCRCSTO (1 << 4) |
| 149 | #define MASK_MWDATTO (1 << 3) |
| 150 | #define MASK_MRDATTO (1 << 2) |
| 151 | #define MASK_MRBSYTO (1 << 1) |
| 152 | #define MASK_MRSPTO (1 << 0) |
| 153 | |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 154 | #define MASK_START_CMD (MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR | \ |
| 155 | MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR | \ |
| 156 | MASK_MCCSTO | MASK_MCRCSTO | MASK_MWDATTO | \ |
| 157 | MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO) |
| 158 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 159 | /* CE_HOST_STS1 */ |
| 160 | #define STS1_CMDSEQ (1 << 31) |
| 161 | |
| 162 | /* CE_HOST_STS2 */ |
| 163 | #define STS2_CRCSTE (1 << 31) |
| 164 | #define STS2_CRC16E (1 << 30) |
| 165 | #define STS2_AC12CRCE (1 << 29) |
| 166 | #define STS2_RSPCRC7E (1 << 28) |
| 167 | #define STS2_CRCSTEBE (1 << 27) |
| 168 | #define STS2_RDATEBE (1 << 26) |
| 169 | #define STS2_AC12REBE (1 << 25) |
| 170 | #define STS2_RSPEBE (1 << 24) |
| 171 | #define STS2_AC12IDXE (1 << 23) |
| 172 | #define STS2_RSPIDXE (1 << 22) |
| 173 | #define STS2_CCSTO (1 << 15) |
| 174 | #define STS2_RDATTO (1 << 14) |
| 175 | #define STS2_DATBSYTO (1 << 13) |
| 176 | #define STS2_CRCSTTO (1 << 12) |
| 177 | #define STS2_AC12BSYTO (1 << 11) |
| 178 | #define STS2_RSPBSYTO (1 << 10) |
| 179 | #define STS2_AC12RSPTO (1 << 9) |
| 180 | #define STS2_RSPTO (1 << 8) |
| 181 | #define STS2_CRC_ERR (STS2_CRCSTE | STS2_CRC16E | \ |
| 182 | STS2_AC12CRCE | STS2_RSPCRC7E | STS2_CRCSTEBE) |
| 183 | #define STS2_TIMEOUT_ERR (STS2_CCSTO | STS2_RDATTO | \ |
| 184 | STS2_DATBSYTO | STS2_CRCSTTO | \ |
| 185 | STS2_AC12BSYTO | STS2_RSPBSYTO | \ |
| 186 | STS2_AC12RSPTO | STS2_RSPTO) |
| 187 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 188 | #define CLKDEV_EMMC_DATA 52000000 /* 52MHz */ |
| 189 | #define CLKDEV_MMC_DATA 20000000 /* 20MHz */ |
| 190 | #define CLKDEV_INIT 400000 /* 400 KHz */ |
| 191 | |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 192 | enum mmcif_state { |
| 193 | STATE_IDLE, |
| 194 | STATE_REQUEST, |
| 195 | STATE_IOS, |
| 196 | }; |
| 197 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 198 | enum mmcif_wait_for { |
| 199 | MMCIF_WAIT_FOR_REQUEST, |
| 200 | MMCIF_WAIT_FOR_CMD, |
| 201 | MMCIF_WAIT_FOR_MREAD, |
| 202 | MMCIF_WAIT_FOR_MWRITE, |
| 203 | MMCIF_WAIT_FOR_READ, |
| 204 | MMCIF_WAIT_FOR_WRITE, |
| 205 | MMCIF_WAIT_FOR_READ_END, |
| 206 | MMCIF_WAIT_FOR_WRITE_END, |
| 207 | MMCIF_WAIT_FOR_STOP, |
| 208 | }; |
| 209 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 210 | struct sh_mmcif_host { |
| 211 | struct mmc_host *mmc; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 212 | struct mmc_request *mrq; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 213 | struct platform_device *pd; |
| 214 | struct clk *hclk; |
| 215 | unsigned int clk; |
| 216 | int bus_width; |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 217 | bool sd_error; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 218 | bool dying; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 219 | long timeout; |
| 220 | void __iomem *addr; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 221 | u32 *pio_ptr; |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 222 | spinlock_t lock; /* protect sh_mmcif_host::state */ |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 223 | enum mmcif_state state; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 224 | enum mmcif_wait_for wait_for; |
| 225 | struct delayed_work timeout_work; |
| 226 | size_t blocksize; |
| 227 | int sg_idx; |
| 228 | int sg_blkidx; |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 229 | bool power; |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 230 | bool card_present; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 231 | |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 232 | /* DMA support */ |
| 233 | struct dma_chan *chan_rx; |
| 234 | struct dma_chan *chan_tx; |
| 235 | struct completion dma_complete; |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 236 | bool dma_active; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 237 | }; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 238 | |
| 239 | static inline void sh_mmcif_bitset(struct sh_mmcif_host *host, |
| 240 | unsigned int reg, u32 val) |
| 241 | { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 242 | writel(val | readl(host->addr + reg), host->addr + reg); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static inline void sh_mmcif_bitclr(struct sh_mmcif_host *host, |
| 246 | unsigned int reg, u32 val) |
| 247 | { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 248 | writel(~val & readl(host->addr + reg), host->addr + reg); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 251 | static void mmcif_dma_complete(void *arg) |
| 252 | { |
| 253 | struct sh_mmcif_host *host = arg; |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 254 | struct mmc_data *data = host->mrq->data; |
| 255 | |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 256 | dev_dbg(&host->pd->dev, "Command completed\n"); |
| 257 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 258 | if (WARN(!data, "%s: NULL data in DMA completion!\n", |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 259 | dev_name(&host->pd->dev))) |
| 260 | return; |
| 261 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 262 | if (data->flags & MMC_DATA_READ) |
Linus Walleij | 1ed828d | 2011-02-10 16:09:29 +0100 | [diff] [blame] | 263 | dma_unmap_sg(host->chan_rx->device->dev, |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 264 | data->sg, data->sg_len, |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 265 | DMA_FROM_DEVICE); |
| 266 | else |
Linus Walleij | 1ed828d | 2011-02-10 16:09:29 +0100 | [diff] [blame] | 267 | dma_unmap_sg(host->chan_tx->device->dev, |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 268 | data->sg, data->sg_len, |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 269 | DMA_TO_DEVICE); |
| 270 | |
| 271 | complete(&host->dma_complete); |
| 272 | } |
| 273 | |
| 274 | static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host) |
| 275 | { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 276 | struct mmc_data *data = host->mrq->data; |
| 277 | struct scatterlist *sg = data->sg; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 278 | struct dma_async_tx_descriptor *desc = NULL; |
| 279 | struct dma_chan *chan = host->chan_rx; |
| 280 | dma_cookie_t cookie = -EINVAL; |
| 281 | int ret; |
| 282 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 283 | ret = dma_map_sg(chan->device->dev, sg, data->sg_len, |
Linus Walleij | 1ed828d | 2011-02-10 16:09:29 +0100 | [diff] [blame] | 284 | DMA_FROM_DEVICE); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 285 | if (ret > 0) { |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 286 | host->dma_active = true; |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 287 | desc = dmaengine_prep_slave_sg(chan, sg, ret, |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 288 | DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if (desc) { |
| 292 | desc->callback = mmcif_dma_complete; |
| 293 | desc->callback_param = host; |
Linus Walleij | a5ece7d | 2011-02-10 16:10:00 +0100 | [diff] [blame] | 294 | cookie = dmaengine_submit(desc); |
| 295 | sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN); |
| 296 | dma_async_issue_pending(chan); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 297 | } |
| 298 | dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n", |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 299 | __func__, data->sg_len, ret, cookie); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 300 | |
| 301 | if (!desc) { |
| 302 | /* DMA failed, fall back to PIO */ |
| 303 | if (ret >= 0) |
| 304 | ret = -EIO; |
| 305 | host->chan_rx = NULL; |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 306 | host->dma_active = false; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 307 | dma_release_channel(chan); |
| 308 | /* Free the Tx channel too */ |
| 309 | chan = host->chan_tx; |
| 310 | if (chan) { |
| 311 | host->chan_tx = NULL; |
| 312 | dma_release_channel(chan); |
| 313 | } |
| 314 | dev_warn(&host->pd->dev, |
| 315 | "DMA failed: %d, falling back to PIO\n", ret); |
| 316 | sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN); |
| 317 | } |
| 318 | |
| 319 | dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__, |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 320 | desc, cookie, data->sg_len); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host) |
| 324 | { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 325 | struct mmc_data *data = host->mrq->data; |
| 326 | struct scatterlist *sg = data->sg; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 327 | struct dma_async_tx_descriptor *desc = NULL; |
| 328 | struct dma_chan *chan = host->chan_tx; |
| 329 | dma_cookie_t cookie = -EINVAL; |
| 330 | int ret; |
| 331 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 332 | ret = dma_map_sg(chan->device->dev, sg, data->sg_len, |
Linus Walleij | 1ed828d | 2011-02-10 16:09:29 +0100 | [diff] [blame] | 333 | DMA_TO_DEVICE); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 334 | if (ret > 0) { |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 335 | host->dma_active = true; |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 336 | desc = dmaengine_prep_slave_sg(chan, sg, ret, |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 337 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | if (desc) { |
| 341 | desc->callback = mmcif_dma_complete; |
| 342 | desc->callback_param = host; |
Linus Walleij | a5ece7d | 2011-02-10 16:10:00 +0100 | [diff] [blame] | 343 | cookie = dmaengine_submit(desc); |
| 344 | sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAWEN); |
| 345 | dma_async_issue_pending(chan); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 346 | } |
| 347 | dev_dbg(&host->pd->dev, "%s(): mapped %d -> %d, cookie %d\n", |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 348 | __func__, data->sg_len, ret, cookie); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 349 | |
| 350 | if (!desc) { |
| 351 | /* DMA failed, fall back to PIO */ |
| 352 | if (ret >= 0) |
| 353 | ret = -EIO; |
| 354 | host->chan_tx = NULL; |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 355 | host->dma_active = false; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 356 | dma_release_channel(chan); |
| 357 | /* Free the Rx channel too */ |
| 358 | chan = host->chan_rx; |
| 359 | if (chan) { |
| 360 | host->chan_rx = NULL; |
| 361 | dma_release_channel(chan); |
| 362 | } |
| 363 | dev_warn(&host->pd->dev, |
| 364 | "DMA failed: %d, falling back to PIO\n", ret); |
| 365 | sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN); |
| 366 | } |
| 367 | |
| 368 | dev_dbg(&host->pd->dev, "%s(): desc %p, cookie %d\n", __func__, |
| 369 | desc, cookie); |
| 370 | } |
| 371 | |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 372 | static void sh_mmcif_request_dma(struct sh_mmcif_host *host, |
| 373 | struct sh_mmcif_plat_data *pdata) |
| 374 | { |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame^] | 375 | struct resource *res = platform_get_resource(host->pd, IORESOURCE_MEM, 0); |
| 376 | struct dma_slave_config cfg; |
| 377 | dma_cap_mask_t mask; |
| 378 | int ret; |
| 379 | |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 380 | host->dma_active = false; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 381 | |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame^] | 382 | if (pdata->slave_id_tx <= 0 || pdata->slave_id_rx <= 0) |
| 383 | return; |
| 384 | |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 385 | /* We can only either use DMA for both Tx and Rx or not use it at all */ |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame^] | 386 | dma_cap_zero(mask); |
| 387 | dma_cap_set(DMA_SLAVE, mask); |
Guennadi Liakhovetski | 916001f | 2012-05-09 17:09:15 +0200 | [diff] [blame] | 388 | |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame^] | 389 | host->chan_tx = dma_request_channel(mask, shdma_chan_filter, |
| 390 | (void *)pdata->slave_id_tx); |
| 391 | dev_dbg(&host->pd->dev, "%s: TX: got channel %p\n", __func__, |
| 392 | host->chan_tx); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 393 | |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame^] | 394 | if (!host->chan_tx) |
| 395 | return; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 396 | |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame^] | 397 | cfg.slave_id = pdata->slave_id_tx; |
| 398 | cfg.direction = DMA_MEM_TO_DEV; |
| 399 | cfg.dst_addr = res->start + MMCIF_CE_DATA; |
| 400 | cfg.src_addr = 0; |
| 401 | ret = dmaengine_slave_config(host->chan_tx, &cfg); |
| 402 | if (ret < 0) |
| 403 | goto ecfgtx; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 404 | |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame^] | 405 | host->chan_rx = dma_request_channel(mask, shdma_chan_filter, |
| 406 | (void *)pdata->slave_id_rx); |
| 407 | dev_dbg(&host->pd->dev, "%s: RX: got channel %p\n", __func__, |
| 408 | host->chan_rx); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 409 | |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame^] | 410 | if (!host->chan_rx) |
| 411 | goto erqrx; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 412 | |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame^] | 413 | cfg.slave_id = pdata->slave_id_rx; |
| 414 | cfg.direction = DMA_DEV_TO_MEM; |
| 415 | cfg.dst_addr = 0; |
| 416 | cfg.src_addr = res->start + MMCIF_CE_DATA; |
| 417 | ret = dmaengine_slave_config(host->chan_rx, &cfg); |
| 418 | if (ret < 0) |
| 419 | goto ecfgrx; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 420 | |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame^] | 421 | init_completion(&host->dma_complete); |
| 422 | |
| 423 | return; |
| 424 | |
| 425 | ecfgrx: |
| 426 | dma_release_channel(host->chan_rx); |
| 427 | host->chan_rx = NULL; |
| 428 | erqrx: |
| 429 | ecfgtx: |
| 430 | dma_release_channel(host->chan_tx); |
| 431 | host->chan_tx = NULL; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static void sh_mmcif_release_dma(struct sh_mmcif_host *host) |
| 435 | { |
| 436 | sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN); |
| 437 | /* Descriptors are freed automatically */ |
| 438 | if (host->chan_tx) { |
| 439 | struct dma_chan *chan = host->chan_tx; |
| 440 | host->chan_tx = NULL; |
| 441 | dma_release_channel(chan); |
| 442 | } |
| 443 | if (host->chan_rx) { |
| 444 | struct dma_chan *chan = host->chan_rx; |
| 445 | host->chan_rx = NULL; |
| 446 | dma_release_channel(chan); |
| 447 | } |
| 448 | |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 449 | host->dma_active = false; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 450 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 451 | |
| 452 | static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk) |
| 453 | { |
| 454 | struct sh_mmcif_plat_data *p = host->pd->dev.platform_data; |
| 455 | |
| 456 | sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE); |
| 457 | sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR); |
| 458 | |
| 459 | if (!clk) |
| 460 | return; |
| 461 | if (p->sup_pclk && clk == host->clk) |
| 462 | sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK); |
| 463 | else |
| 464 | sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR & |
Simon Horman | f938825 | 2012-03-28 18:01:09 +0900 | [diff] [blame] | 465 | ((fls(DIV_ROUND_UP(host->clk, |
| 466 | clk) - 1) - 1) << 16)); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 467 | |
| 468 | sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE); |
| 469 | } |
| 470 | |
| 471 | static void sh_mmcif_sync_reset(struct sh_mmcif_host *host) |
| 472 | { |
| 473 | u32 tmp; |
| 474 | |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 475 | tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 476 | |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 477 | sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON); |
| 478 | sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 479 | sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp | |
| 480 | SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 | SCCSTO_29); |
| 481 | /* byte swap on */ |
| 482 | sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP); |
| 483 | } |
| 484 | |
| 485 | static int sh_mmcif_error_manage(struct sh_mmcif_host *host) |
| 486 | { |
| 487 | u32 state1, state2; |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 488 | int ret, timeout; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 489 | |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 490 | host->sd_error = false; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 491 | |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 492 | state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1); |
| 493 | state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2); |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 494 | dev_dbg(&host->pd->dev, "ERR HOST_STS1 = %08x\n", state1); |
| 495 | dev_dbg(&host->pd->dev, "ERR HOST_STS2 = %08x\n", state2); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 496 | |
| 497 | if (state1 & STS1_CMDSEQ) { |
| 498 | sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK); |
| 499 | sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK); |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 500 | for (timeout = 10000000; timeout; timeout--) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 501 | if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1) |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 502 | & STS1_CMDSEQ)) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 503 | break; |
| 504 | mdelay(1); |
| 505 | } |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 506 | if (!timeout) { |
| 507 | dev_err(&host->pd->dev, |
| 508 | "Forced end of command sequence timeout err\n"); |
| 509 | return -EIO; |
| 510 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 511 | sh_mmcif_sync_reset(host); |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 512 | dev_dbg(&host->pd->dev, "Forced end of command sequence\n"); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 513 | return -EIO; |
| 514 | } |
| 515 | |
| 516 | if (state2 & STS2_CRC_ERR) { |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 517 | dev_dbg(&host->pd->dev, ": CRC error\n"); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 518 | ret = -EIO; |
| 519 | } else if (state2 & STS2_TIMEOUT_ERR) { |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 520 | dev_dbg(&host->pd->dev, ": Timeout\n"); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 521 | ret = -ETIMEDOUT; |
| 522 | } else { |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 523 | dev_dbg(&host->pd->dev, ": End/Index error\n"); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 524 | ret = -EIO; |
| 525 | } |
| 526 | return ret; |
| 527 | } |
| 528 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 529 | static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 530 | { |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 531 | struct mmc_data *data = host->mrq->data; |
| 532 | |
| 533 | host->sg_blkidx += host->blocksize; |
| 534 | |
| 535 | /* data->sg->length must be a multiple of host->blocksize? */ |
| 536 | BUG_ON(host->sg_blkidx > data->sg->length); |
| 537 | |
| 538 | if (host->sg_blkidx == data->sg->length) { |
| 539 | host->sg_blkidx = 0; |
| 540 | if (++host->sg_idx < data->sg_len) |
| 541 | host->pio_ptr = sg_virt(++data->sg); |
| 542 | } else { |
| 543 | host->pio_ptr = p; |
| 544 | } |
| 545 | |
| 546 | if (host->sg_idx == data->sg_len) |
| 547 | return false; |
| 548 | |
| 549 | return true; |
| 550 | } |
| 551 | |
| 552 | static void sh_mmcif_single_read(struct sh_mmcif_host *host, |
| 553 | struct mmc_request *mrq) |
| 554 | { |
| 555 | host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & |
| 556 | BLOCK_SIZE_MASK) + 3; |
| 557 | |
| 558 | host->wait_for = MMCIF_WAIT_FOR_READ; |
| 559 | schedule_delayed_work(&host->timeout_work, host->timeout); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 560 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 561 | /* buf read enable */ |
| 562 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 563 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 564 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 565 | static bool sh_mmcif_read_block(struct sh_mmcif_host *host) |
| 566 | { |
| 567 | struct mmc_data *data = host->mrq->data; |
| 568 | u32 *p = sg_virt(data->sg); |
| 569 | int i; |
| 570 | |
| 571 | if (host->sd_error) { |
| 572 | data->error = sh_mmcif_error_manage(host); |
| 573 | return false; |
| 574 | } |
| 575 | |
| 576 | for (i = 0; i < host->blocksize / 4; i++) |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 577 | *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 578 | |
| 579 | /* buffer read end */ |
| 580 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 581 | host->wait_for = MMCIF_WAIT_FOR_READ_END; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 582 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 583 | return true; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 586 | static void sh_mmcif_multi_read(struct sh_mmcif_host *host, |
| 587 | struct mmc_request *mrq) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 588 | { |
| 589 | struct mmc_data *data = mrq->data; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 590 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 591 | if (!data->sg_len || !data->sg->length) |
| 592 | return; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 593 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 594 | host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & |
| 595 | BLOCK_SIZE_MASK; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 596 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 597 | host->wait_for = MMCIF_WAIT_FOR_MREAD; |
| 598 | host->sg_idx = 0; |
| 599 | host->sg_blkidx = 0; |
| 600 | host->pio_ptr = sg_virt(data->sg); |
| 601 | schedule_delayed_work(&host->timeout_work, host->timeout); |
| 602 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN); |
| 603 | } |
| 604 | |
| 605 | static bool sh_mmcif_mread_block(struct sh_mmcif_host *host) |
| 606 | { |
| 607 | struct mmc_data *data = host->mrq->data; |
| 608 | u32 *p = host->pio_ptr; |
| 609 | int i; |
| 610 | |
| 611 | if (host->sd_error) { |
| 612 | data->error = sh_mmcif_error_manage(host); |
| 613 | return false; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 614 | } |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 615 | |
| 616 | BUG_ON(!data->sg->length); |
| 617 | |
| 618 | for (i = 0; i < host->blocksize / 4; i++) |
| 619 | *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA); |
| 620 | |
| 621 | if (!sh_mmcif_next_block(host, p)) |
| 622 | return false; |
| 623 | |
| 624 | schedule_delayed_work(&host->timeout_work, host->timeout); |
| 625 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN); |
| 626 | |
| 627 | return true; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 630 | static void sh_mmcif_single_write(struct sh_mmcif_host *host, |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 631 | struct mmc_request *mrq) |
| 632 | { |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 633 | host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & |
| 634 | BLOCK_SIZE_MASK) + 3; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 635 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 636 | host->wait_for = MMCIF_WAIT_FOR_WRITE; |
| 637 | schedule_delayed_work(&host->timeout_work, host->timeout); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 638 | |
| 639 | /* buf write enable */ |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 640 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN); |
| 641 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 642 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 643 | static bool sh_mmcif_write_block(struct sh_mmcif_host *host) |
| 644 | { |
| 645 | struct mmc_data *data = host->mrq->data; |
| 646 | u32 *p = sg_virt(data->sg); |
| 647 | int i; |
| 648 | |
| 649 | if (host->sd_error) { |
| 650 | data->error = sh_mmcif_error_manage(host); |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | for (i = 0; i < host->blocksize / 4; i++) |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 655 | sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 656 | |
| 657 | /* buffer write end */ |
| 658 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 659 | host->wait_for = MMCIF_WAIT_FOR_WRITE_END; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 660 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 661 | return true; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 664 | static void sh_mmcif_multi_write(struct sh_mmcif_host *host, |
| 665 | struct mmc_request *mrq) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 666 | { |
| 667 | struct mmc_data *data = mrq->data; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 668 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 669 | if (!data->sg_len || !data->sg->length) |
| 670 | return; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 671 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 672 | host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & |
| 673 | BLOCK_SIZE_MASK; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 674 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 675 | host->wait_for = MMCIF_WAIT_FOR_MWRITE; |
| 676 | host->sg_idx = 0; |
| 677 | host->sg_blkidx = 0; |
| 678 | host->pio_ptr = sg_virt(data->sg); |
| 679 | schedule_delayed_work(&host->timeout_work, host->timeout); |
| 680 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN); |
| 681 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 682 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 683 | static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host) |
| 684 | { |
| 685 | struct mmc_data *data = host->mrq->data; |
| 686 | u32 *p = host->pio_ptr; |
| 687 | int i; |
| 688 | |
| 689 | if (host->sd_error) { |
| 690 | data->error = sh_mmcif_error_manage(host); |
| 691 | return false; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 692 | } |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 693 | |
| 694 | BUG_ON(!data->sg->length); |
| 695 | |
| 696 | for (i = 0; i < host->blocksize / 4; i++) |
| 697 | sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++); |
| 698 | |
| 699 | if (!sh_mmcif_next_block(host, p)) |
| 700 | return false; |
| 701 | |
| 702 | schedule_delayed_work(&host->timeout_work, host->timeout); |
| 703 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN); |
| 704 | |
| 705 | return true; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static void sh_mmcif_get_response(struct sh_mmcif_host *host, |
| 709 | struct mmc_command *cmd) |
| 710 | { |
| 711 | if (cmd->flags & MMC_RSP_136) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 712 | cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3); |
| 713 | cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2); |
| 714 | cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1); |
| 715 | cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 716 | } else |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 717 | cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host, |
| 721 | struct mmc_command *cmd) |
| 722 | { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 723 | cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host, |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 727 | struct mmc_request *mrq) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 728 | { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 729 | struct mmc_data *data = mrq->data; |
| 730 | struct mmc_command *cmd = mrq->cmd; |
| 731 | u32 opc = cmd->opcode; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 732 | u32 tmp = 0; |
| 733 | |
| 734 | /* Response Type check */ |
| 735 | switch (mmc_resp_type(cmd)) { |
| 736 | case MMC_RSP_NONE: |
| 737 | tmp |= CMD_SET_RTYP_NO; |
| 738 | break; |
| 739 | case MMC_RSP_R1: |
| 740 | case MMC_RSP_R1B: |
| 741 | case MMC_RSP_R3: |
| 742 | tmp |= CMD_SET_RTYP_6B; |
| 743 | break; |
| 744 | case MMC_RSP_R2: |
| 745 | tmp |= CMD_SET_RTYP_17B; |
| 746 | break; |
| 747 | default: |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 748 | dev_err(&host->pd->dev, "Unsupported response type.\n"); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 749 | break; |
| 750 | } |
| 751 | switch (opc) { |
| 752 | /* RBSY */ |
| 753 | case MMC_SWITCH: |
| 754 | case MMC_STOP_TRANSMISSION: |
| 755 | case MMC_SET_WRITE_PROT: |
| 756 | case MMC_CLR_WRITE_PROT: |
| 757 | case MMC_ERASE: |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 758 | tmp |= CMD_SET_RBSY; |
| 759 | break; |
| 760 | } |
| 761 | /* WDAT / DATW */ |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 762 | if (data) { |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 763 | tmp |= CMD_SET_WDAT; |
| 764 | switch (host->bus_width) { |
| 765 | case MMC_BUS_WIDTH_1: |
| 766 | tmp |= CMD_SET_DATW_1; |
| 767 | break; |
| 768 | case MMC_BUS_WIDTH_4: |
| 769 | tmp |= CMD_SET_DATW_4; |
| 770 | break; |
| 771 | case MMC_BUS_WIDTH_8: |
| 772 | tmp |= CMD_SET_DATW_8; |
| 773 | break; |
| 774 | default: |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 775 | dev_err(&host->pd->dev, "Unsupported bus width.\n"); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 776 | break; |
| 777 | } |
| 778 | } |
| 779 | /* DWEN */ |
| 780 | if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) |
| 781 | tmp |= CMD_SET_DWEN; |
| 782 | /* CMLTE/CMD12EN */ |
| 783 | if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) { |
| 784 | tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN; |
| 785 | sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET, |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 786 | data->blocks << 16); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 787 | } |
| 788 | /* RIDXC[1:0] check bits */ |
| 789 | if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID || |
| 790 | opc == MMC_SEND_CSD || opc == MMC_SEND_CID) |
| 791 | tmp |= CMD_SET_RIDXC_BITS; |
| 792 | /* RCRC7C[1:0] check bits */ |
| 793 | if (opc == MMC_SEND_OP_COND) |
| 794 | tmp |= CMD_SET_CRC7C_BITS; |
| 795 | /* RCRC7C[1:0] internal CRC7 */ |
| 796 | if (opc == MMC_ALL_SEND_CID || |
| 797 | opc == MMC_SEND_CSD || opc == MMC_SEND_CID) |
| 798 | tmp |= CMD_SET_CRC7C_INTERNAL; |
| 799 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 800 | return (opc << 24) | tmp; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 803 | static int sh_mmcif_data_trans(struct sh_mmcif_host *host, |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 804 | struct mmc_request *mrq, u32 opc) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 805 | { |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 806 | switch (opc) { |
| 807 | case MMC_READ_MULTIPLE_BLOCK: |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 808 | sh_mmcif_multi_read(host, mrq); |
| 809 | return 0; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 810 | case MMC_WRITE_MULTIPLE_BLOCK: |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 811 | sh_mmcif_multi_write(host, mrq); |
| 812 | return 0; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 813 | case MMC_WRITE_BLOCK: |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 814 | sh_mmcif_single_write(host, mrq); |
| 815 | return 0; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 816 | case MMC_READ_SINGLE_BLOCK: |
| 817 | case MMC_SEND_EXT_CSD: |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 818 | sh_mmcif_single_read(host, mrq); |
| 819 | return 0; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 820 | default: |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 821 | dev_err(&host->pd->dev, "UNSUPPORTED CMD = d'%08d\n", opc); |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 822 | return -EINVAL; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 823 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | static void sh_mmcif_start_cmd(struct sh_mmcif_host *host, |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 827 | struct mmc_request *mrq) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 828 | { |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 829 | struct mmc_command *cmd = mrq->cmd; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 830 | u32 opc = cmd->opcode; |
| 831 | u32 mask; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 832 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 833 | switch (opc) { |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 834 | /* response busy check */ |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 835 | case MMC_SWITCH: |
| 836 | case MMC_STOP_TRANSMISSION: |
| 837 | case MMC_SET_WRITE_PROT: |
| 838 | case MMC_CLR_WRITE_PROT: |
| 839 | case MMC_ERASE: |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 840 | mask = MASK_START_CMD | MASK_MRBSYE; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 841 | break; |
| 842 | default: |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 843 | mask = MASK_START_CMD | MASK_MCRSPE; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 844 | break; |
| 845 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 846 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 847 | if (mrq->data) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 848 | sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0); |
| 849 | sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, |
| 850 | mrq->data->blksz); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 851 | } |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 852 | opc = sh_mmcif_set_cmd(host, mrq); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 853 | |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 854 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0); |
| 855 | sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 856 | /* set arg */ |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 857 | sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 858 | /* set cmd */ |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 859 | sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 860 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 861 | host->wait_for = MMCIF_WAIT_FOR_CMD; |
| 862 | schedule_delayed_work(&host->timeout_work, host->timeout); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host, |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 866 | struct mmc_request *mrq) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 867 | { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 868 | switch (mrq->cmd->opcode) { |
| 869 | case MMC_READ_MULTIPLE_BLOCK: |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 870 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE); |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 871 | break; |
| 872 | case MMC_WRITE_MULTIPLE_BLOCK: |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 873 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE); |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 874 | break; |
| 875 | default: |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 876 | dev_err(&host->pd->dev, "unsupported stop cmd\n"); |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 877 | mrq->stop->error = sh_mmcif_error_manage(host); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 878 | return; |
| 879 | } |
| 880 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 881 | host->wait_for = MMCIF_WAIT_FOR_STOP; |
| 882 | schedule_delayed_work(&host->timeout_work, host->timeout); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 886 | { |
| 887 | struct sh_mmcif_host *host = mmc_priv(mmc); |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 888 | unsigned long flags; |
| 889 | |
| 890 | spin_lock_irqsave(&host->lock, flags); |
| 891 | if (host->state != STATE_IDLE) { |
| 892 | spin_unlock_irqrestore(&host->lock, flags); |
| 893 | mrq->cmd->error = -EAGAIN; |
| 894 | mmc_request_done(mmc, mrq); |
| 895 | return; |
| 896 | } |
| 897 | |
| 898 | host->state = STATE_REQUEST; |
| 899 | spin_unlock_irqrestore(&host->lock, flags); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 900 | |
| 901 | switch (mrq->cmd->opcode) { |
| 902 | /* MMCIF does not support SD/SDIO command */ |
| 903 | case SD_IO_SEND_OP_COND: |
| 904 | case MMC_APP_CMD: |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 905 | host->state = STATE_IDLE; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 906 | mrq->cmd->error = -ETIMEDOUT; |
| 907 | mmc_request_done(mmc, mrq); |
| 908 | return; |
| 909 | case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */ |
| 910 | if (!mrq->data) { |
| 911 | /* send_if_cond cmd (not support) */ |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 912 | host->state = STATE_IDLE; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 913 | mrq->cmd->error = -ETIMEDOUT; |
| 914 | mmc_request_done(mmc, mrq); |
| 915 | return; |
| 916 | } |
| 917 | break; |
| 918 | default: |
| 919 | break; |
| 920 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 921 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 922 | host->mrq = mrq; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 923 | |
| 924 | sh_mmcif_start_cmd(host, mrq); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 928 | { |
| 929 | struct sh_mmcif_host *host = mmc_priv(mmc); |
| 930 | struct sh_mmcif_plat_data *p = host->pd->dev.platform_data; |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 931 | unsigned long flags; |
| 932 | |
| 933 | spin_lock_irqsave(&host->lock, flags); |
| 934 | if (host->state != STATE_IDLE) { |
| 935 | spin_unlock_irqrestore(&host->lock, flags); |
| 936 | return; |
| 937 | } |
| 938 | |
| 939 | host->state = STATE_IOS; |
| 940 | spin_unlock_irqrestore(&host->lock, flags); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 941 | |
Guennadi Liakhovetski | f5e0cec | 2011-02-25 16:58:38 +0100 | [diff] [blame] | 942 | if (ios->power_mode == MMC_POWER_UP) { |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 943 | if (!host->card_present) { |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 944 | /* See if we also get DMA */ |
| 945 | sh_mmcif_request_dma(host, host->pd->dev.platform_data); |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 946 | host->card_present = true; |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 947 | } |
Guennadi Liakhovetski | f5e0cec | 2011-02-25 16:58:38 +0100 | [diff] [blame] | 948 | } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) { |
| 949 | /* clock stop */ |
| 950 | sh_mmcif_clock_control(host, 0); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 951 | if (ios->power_mode == MMC_POWER_OFF) { |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 952 | if (host->card_present) { |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 953 | sh_mmcif_release_dma(host); |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 954 | host->card_present = false; |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 955 | } |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 956 | } |
| 957 | if (host->power) { |
| 958 | pm_runtime_put(&host->pd->dev); |
| 959 | host->power = false; |
Guennadi Liakhovetski | f6bc41f | 2011-11-16 10:10:41 +0100 | [diff] [blame] | 960 | if (p->down_pwr && ios->power_mode == MMC_POWER_OFF) |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 961 | p->down_pwr(host->pd); |
| 962 | } |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 963 | host->state = STATE_IDLE; |
Guennadi Liakhovetski | f5e0cec | 2011-02-25 16:58:38 +0100 | [diff] [blame] | 964 | return; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 967 | if (ios->clock) { |
| 968 | if (!host->power) { |
| 969 | if (p->set_pwr) |
| 970 | p->set_pwr(host->pd, ios->power_mode); |
| 971 | pm_runtime_get_sync(&host->pd->dev); |
| 972 | host->power = true; |
| 973 | sh_mmcif_sync_reset(host); |
| 974 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 975 | sh_mmcif_clock_control(host, ios->clock); |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 976 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 977 | |
| 978 | host->bus_width = ios->bus_width; |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 979 | host->state = STATE_IDLE; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Arnd Hannemann | 777271d | 2010-08-24 17:27:01 +0200 | [diff] [blame] | 982 | static int sh_mmcif_get_cd(struct mmc_host *mmc) |
| 983 | { |
| 984 | struct sh_mmcif_host *host = mmc_priv(mmc); |
| 985 | struct sh_mmcif_plat_data *p = host->pd->dev.platform_data; |
| 986 | |
| 987 | if (!p->get_cd) |
| 988 | return -ENOSYS; |
| 989 | else |
| 990 | return p->get_cd(host->pd); |
| 991 | } |
| 992 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 993 | static struct mmc_host_ops sh_mmcif_ops = { |
| 994 | .request = sh_mmcif_request, |
| 995 | .set_ios = sh_mmcif_set_ios, |
Arnd Hannemann | 777271d | 2010-08-24 17:27:01 +0200 | [diff] [blame] | 996 | .get_cd = sh_mmcif_get_cd, |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 997 | }; |
| 998 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 999 | static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host) |
| 1000 | { |
| 1001 | struct mmc_command *cmd = host->mrq->cmd; |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1002 | struct mmc_data *data = host->mrq->data; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1003 | long time; |
| 1004 | |
| 1005 | if (host->sd_error) { |
| 1006 | switch (cmd->opcode) { |
| 1007 | case MMC_ALL_SEND_CID: |
| 1008 | case MMC_SELECT_CARD: |
| 1009 | case MMC_APP_CMD: |
| 1010 | cmd->error = -ETIMEDOUT; |
| 1011 | host->sd_error = false; |
| 1012 | break; |
| 1013 | default: |
| 1014 | cmd->error = sh_mmcif_error_manage(host); |
| 1015 | dev_dbg(&host->pd->dev, "Cmd(d'%d) error %d\n", |
| 1016 | cmd->opcode, cmd->error); |
| 1017 | break; |
| 1018 | } |
| 1019 | return false; |
| 1020 | } |
| 1021 | if (!(cmd->flags & MMC_RSP_PRESENT)) { |
| 1022 | cmd->error = 0; |
| 1023 | return false; |
| 1024 | } |
| 1025 | |
| 1026 | sh_mmcif_get_response(host, cmd); |
| 1027 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1028 | if (!data) |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1029 | return false; |
| 1030 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1031 | if (data->flags & MMC_DATA_READ) { |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1032 | if (host->chan_rx) |
| 1033 | sh_mmcif_start_dma_rx(host); |
| 1034 | } else { |
| 1035 | if (host->chan_tx) |
| 1036 | sh_mmcif_start_dma_tx(host); |
| 1037 | } |
| 1038 | |
| 1039 | if (!host->dma_active) { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1040 | data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode); |
| 1041 | if (!data->error) |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1042 | return true; |
| 1043 | return false; |
| 1044 | } |
| 1045 | |
| 1046 | /* Running in the IRQ thread, can sleep */ |
| 1047 | time = wait_for_completion_interruptible_timeout(&host->dma_complete, |
| 1048 | host->timeout); |
| 1049 | if (host->sd_error) { |
| 1050 | dev_err(host->mmc->parent, |
| 1051 | "Error IRQ while waiting for DMA completion!\n"); |
| 1052 | /* Woken up by an error IRQ: abort DMA */ |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1053 | if (data->flags & MMC_DATA_READ) |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1054 | dmaengine_terminate_all(host->chan_rx); |
| 1055 | else |
| 1056 | dmaengine_terminate_all(host->chan_tx); |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1057 | data->error = sh_mmcif_error_manage(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1058 | } else if (!time) { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1059 | data->error = -ETIMEDOUT; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1060 | } else if (time < 0) { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1061 | data->error = time; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1062 | } |
| 1063 | sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, |
| 1064 | BUF_ACC_DMAREN | BUF_ACC_DMAWEN); |
| 1065 | host->dma_active = false; |
| 1066 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1067 | if (data->error) |
| 1068 | data->bytes_xfered = 0; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1069 | |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
| 1073 | static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id) |
| 1074 | { |
| 1075 | struct sh_mmcif_host *host = dev_id; |
| 1076 | struct mmc_request *mrq = host->mrq; |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1077 | struct mmc_data *data = mrq->data; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1078 | |
| 1079 | cancel_delayed_work_sync(&host->timeout_work); |
| 1080 | |
| 1081 | /* |
| 1082 | * All handlers return true, if processing continues, and false, if the |
| 1083 | * request has to be completed - successfully or not |
| 1084 | */ |
| 1085 | switch (host->wait_for) { |
| 1086 | case MMCIF_WAIT_FOR_REQUEST: |
| 1087 | /* We're too late, the timeout has already kicked in */ |
| 1088 | return IRQ_HANDLED; |
| 1089 | case MMCIF_WAIT_FOR_CMD: |
| 1090 | if (sh_mmcif_end_cmd(host)) |
| 1091 | /* Wait for data */ |
| 1092 | return IRQ_HANDLED; |
| 1093 | break; |
| 1094 | case MMCIF_WAIT_FOR_MREAD: |
| 1095 | if (sh_mmcif_mread_block(host)) |
| 1096 | /* Wait for more data */ |
| 1097 | return IRQ_HANDLED; |
| 1098 | break; |
| 1099 | case MMCIF_WAIT_FOR_READ: |
| 1100 | if (sh_mmcif_read_block(host)) |
| 1101 | /* Wait for data end */ |
| 1102 | return IRQ_HANDLED; |
| 1103 | break; |
| 1104 | case MMCIF_WAIT_FOR_MWRITE: |
| 1105 | if (sh_mmcif_mwrite_block(host)) |
| 1106 | /* Wait data to write */ |
| 1107 | return IRQ_HANDLED; |
| 1108 | break; |
| 1109 | case MMCIF_WAIT_FOR_WRITE: |
| 1110 | if (sh_mmcif_write_block(host)) |
| 1111 | /* Wait for data end */ |
| 1112 | return IRQ_HANDLED; |
| 1113 | break; |
| 1114 | case MMCIF_WAIT_FOR_STOP: |
| 1115 | if (host->sd_error) { |
| 1116 | mrq->stop->error = sh_mmcif_error_manage(host); |
| 1117 | break; |
| 1118 | } |
| 1119 | sh_mmcif_get_cmd12response(host, mrq->stop); |
| 1120 | mrq->stop->error = 0; |
| 1121 | break; |
| 1122 | case MMCIF_WAIT_FOR_READ_END: |
| 1123 | case MMCIF_WAIT_FOR_WRITE_END: |
| 1124 | if (host->sd_error) |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1125 | data->error = sh_mmcif_error_manage(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1126 | break; |
| 1127 | default: |
| 1128 | BUG(); |
| 1129 | } |
| 1130 | |
| 1131 | if (host->wait_for != MMCIF_WAIT_FOR_STOP) { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1132 | if (!mrq->cmd->error && data && !data->error) |
| 1133 | data->bytes_xfered = |
| 1134 | data->blocks * data->blksz; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1135 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1136 | if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) { |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1137 | sh_mmcif_stop_cmd(host, mrq); |
| 1138 | if (!mrq->stop->error) |
| 1139 | return IRQ_HANDLED; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | host->wait_for = MMCIF_WAIT_FOR_REQUEST; |
| 1144 | host->state = STATE_IDLE; |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1145 | host->mrq = NULL; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1146 | mmc_request_done(host->mmc, mrq); |
| 1147 | |
| 1148 | return IRQ_HANDLED; |
| 1149 | } |
| 1150 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1151 | static irqreturn_t sh_mmcif_intr(int irq, void *dev_id) |
| 1152 | { |
| 1153 | struct sh_mmcif_host *host = dev_id; |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 1154 | u32 state; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1155 | int err = 0; |
| 1156 | |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1157 | state = sh_mmcif_readl(host->addr, MMCIF_CE_INT); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1158 | |
Guennadi Liakhovetski | 8a8284a | 2011-12-14 19:31:51 +0100 | [diff] [blame] | 1159 | if (state & INT_ERR_STS) { |
| 1160 | /* error interrupts - process first */ |
| 1161 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state); |
| 1162 | sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state); |
| 1163 | err = 1; |
| 1164 | } else if (state & INT_RBSYE) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1165 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, |
| 1166 | ~(INT_RBSYE | INT_CRSPE)); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1167 | sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MRBSYE); |
| 1168 | } else if (state & INT_CRSPE) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1169 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_CRSPE); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1170 | sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCRSPE); |
| 1171 | } else if (state & INT_BUFREN) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1172 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFREN); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1173 | sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFREN); |
| 1174 | } else if (state & INT_BUFWEN) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1175 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFWEN); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1176 | sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN); |
| 1177 | } else if (state & INT_CMD12DRE) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1178 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1179 | ~(INT_CMD12DRE | INT_CMD12RBE | |
| 1180 | INT_CMD12CRE | INT_BUFRE)); |
| 1181 | sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE); |
| 1182 | } else if (state & INT_BUFRE) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1183 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_BUFRE); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1184 | sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MBUFRE); |
| 1185 | } else if (state & INT_DTRANE) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1186 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~INT_DTRANE); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1187 | sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MDTRANE); |
| 1188 | } else if (state & INT_CMD12RBE) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1189 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1190 | ~(INT_CMD12RBE | INT_CMD12CRE)); |
| 1191 | sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1192 | } else { |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1193 | dev_dbg(&host->pd->dev, "Unsupported interrupt: 0x%x\n", state); |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1194 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~state); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1195 | sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state); |
| 1196 | err = 1; |
| 1197 | } |
| 1198 | if (err) { |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 1199 | host->sd_error = true; |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 1200 | dev_dbg(&host->pd->dev, "int err state = %08x\n", state); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1201 | } |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1202 | if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) { |
| 1203 | if (!host->dma_active) |
| 1204 | return IRQ_WAKE_THREAD; |
| 1205 | else if (host->sd_error) |
| 1206 | mmcif_dma_complete(host); |
| 1207 | } else { |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 1208 | dev_dbg(&host->pd->dev, "Unexpected IRQ 0x%x\n", state); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1209 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1210 | |
| 1211 | return IRQ_HANDLED; |
| 1212 | } |
| 1213 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1214 | static void mmcif_timeout_work(struct work_struct *work) |
| 1215 | { |
| 1216 | struct delayed_work *d = container_of(work, struct delayed_work, work); |
| 1217 | struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work); |
| 1218 | struct mmc_request *mrq = host->mrq; |
| 1219 | |
| 1220 | if (host->dying) |
| 1221 | /* Don't run after mmc_remove_host() */ |
| 1222 | return; |
| 1223 | |
| 1224 | /* |
| 1225 | * Handle races with cancel_delayed_work(), unless |
| 1226 | * cancel_delayed_work_sync() is used |
| 1227 | */ |
| 1228 | switch (host->wait_for) { |
| 1229 | case MMCIF_WAIT_FOR_CMD: |
| 1230 | mrq->cmd->error = sh_mmcif_error_manage(host); |
| 1231 | break; |
| 1232 | case MMCIF_WAIT_FOR_STOP: |
| 1233 | mrq->stop->error = sh_mmcif_error_manage(host); |
| 1234 | break; |
| 1235 | case MMCIF_WAIT_FOR_MREAD: |
| 1236 | case MMCIF_WAIT_FOR_MWRITE: |
| 1237 | case MMCIF_WAIT_FOR_READ: |
| 1238 | case MMCIF_WAIT_FOR_WRITE: |
| 1239 | case MMCIF_WAIT_FOR_READ_END: |
| 1240 | case MMCIF_WAIT_FOR_WRITE_END: |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1241 | mrq->data->error = sh_mmcif_error_manage(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1242 | break; |
| 1243 | default: |
| 1244 | BUG(); |
| 1245 | } |
| 1246 | |
| 1247 | host->state = STATE_IDLE; |
| 1248 | host->wait_for = MMCIF_WAIT_FOR_REQUEST; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1249 | host->mrq = NULL; |
| 1250 | mmc_request_done(host->mmc, mrq); |
| 1251 | } |
| 1252 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1253 | static int __devinit sh_mmcif_probe(struct platform_device *pdev) |
| 1254 | { |
| 1255 | int ret = 0, irq[2]; |
| 1256 | struct mmc_host *mmc; |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 1257 | struct sh_mmcif_host *host; |
| 1258 | struct sh_mmcif_plat_data *pd; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1259 | struct resource *res; |
| 1260 | void __iomem *reg; |
| 1261 | char clk_name[8]; |
| 1262 | |
| 1263 | irq[0] = platform_get_irq(pdev, 0); |
| 1264 | irq[1] = platform_get_irq(pdev, 1); |
| 1265 | if (irq[0] < 0 || irq[1] < 0) { |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 1266 | dev_err(&pdev->dev, "Get irq error\n"); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1267 | return -ENXIO; |
| 1268 | } |
| 1269 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1270 | if (!res) { |
| 1271 | dev_err(&pdev->dev, "platform_get_resource error.\n"); |
| 1272 | return -ENXIO; |
| 1273 | } |
| 1274 | reg = ioremap(res->start, resource_size(res)); |
| 1275 | if (!reg) { |
| 1276 | dev_err(&pdev->dev, "ioremap error.\n"); |
| 1277 | return -ENOMEM; |
| 1278 | } |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 1279 | pd = pdev->dev.platform_data; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1280 | if (!pd) { |
| 1281 | dev_err(&pdev->dev, "sh_mmcif plat data error.\n"); |
| 1282 | ret = -ENXIO; |
| 1283 | goto clean_up; |
| 1284 | } |
| 1285 | mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), &pdev->dev); |
| 1286 | if (!mmc) { |
| 1287 | ret = -ENOMEM; |
| 1288 | goto clean_up; |
| 1289 | } |
| 1290 | host = mmc_priv(mmc); |
| 1291 | host->mmc = mmc; |
| 1292 | host->addr = reg; |
| 1293 | host->timeout = 1000; |
| 1294 | |
| 1295 | snprintf(clk_name, sizeof(clk_name), "mmc%d", pdev->id); |
| 1296 | host->hclk = clk_get(&pdev->dev, clk_name); |
| 1297 | if (IS_ERR(host->hclk)) { |
| 1298 | dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name); |
| 1299 | ret = PTR_ERR(host->hclk); |
| 1300 | goto clean_up1; |
| 1301 | } |
| 1302 | clk_enable(host->hclk); |
| 1303 | host->clk = clk_get_rate(host->hclk); |
| 1304 | host->pd = pdev; |
| 1305 | |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 1306 | spin_lock_init(&host->lock); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1307 | |
| 1308 | mmc->ops = &sh_mmcif_ops; |
Simon Horman | 930f152 | 2012-03-28 18:01:10 +0900 | [diff] [blame] | 1309 | mmc->f_max = host->clk / 2; |
Simon Horman | eb91b91 | 2012-03-28 18:01:11 +0900 | [diff] [blame] | 1310 | mmc->f_min = host->clk / 512; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1311 | if (pd->ocr) |
| 1312 | mmc->ocr_avail = pd->ocr; |
| 1313 | mmc->caps = MMC_CAP_MMC_HIGHSPEED; |
| 1314 | if (pd->caps) |
| 1315 | mmc->caps |= pd->caps; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 1316 | mmc->max_segs = 32; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1317 | mmc->max_blk_size = 512; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 1318 | mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs; |
| 1319 | mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1320 | mmc->max_seg_size = mmc->max_req_size; |
| 1321 | |
| 1322 | sh_mmcif_sync_reset(host); |
| 1323 | platform_set_drvdata(pdev, host); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 1324 | |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1325 | pm_runtime_enable(&pdev->dev); |
| 1326 | host->power = false; |
| 1327 | |
| 1328 | ret = pm_runtime_resume(&pdev->dev); |
| 1329 | if (ret < 0) |
| 1330 | goto clean_up2; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 1331 | |
Guennadi Liakhovetski | 5ba85d9 | 2012-01-21 00:41:28 +0100 | [diff] [blame] | 1332 | INIT_DELAYED_WORK(&host->timeout_work, mmcif_timeout_work); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1333 | |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 1334 | sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL); |
| 1335 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1336 | ret = request_threaded_irq(irq[0], sh_mmcif_intr, sh_mmcif_irqt, 0, "sh_mmc:error", host); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1337 | if (ret) { |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 1338 | dev_err(&pdev->dev, "request_irq error (sh_mmc:error)\n"); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1339 | goto clean_up3; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1340 | } |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1341 | ret = request_threaded_irq(irq[1], sh_mmcif_intr, sh_mmcif_irqt, 0, "sh_mmc:int", host); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1342 | if (ret) { |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 1343 | dev_err(&pdev->dev, "request_irq error (sh_mmc:int)\n"); |
Guennadi Liakhovetski | 5ba85d9 | 2012-01-21 00:41:28 +0100 | [diff] [blame] | 1344 | goto clean_up4; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Guennadi Liakhovetski | 5ba85d9 | 2012-01-21 00:41:28 +0100 | [diff] [blame] | 1347 | ret = mmc_add_host(mmc); |
| 1348 | if (ret < 0) |
| 1349 | goto clean_up5; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1350 | |
Rafael J. Wysocki | efe6a8a | 2012-03-13 01:02:15 +0100 | [diff] [blame] | 1351 | dev_pm_qos_expose_latency_limit(&pdev->dev, 100); |
| 1352 | |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 1353 | dev_info(&pdev->dev, "driver version %s\n", DRIVER_VERSION); |
| 1354 | dev_dbg(&pdev->dev, "chip ver H'%04x\n", |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1355 | sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0x0000ffff); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1356 | return ret; |
| 1357 | |
Guennadi Liakhovetski | 5ba85d9 | 2012-01-21 00:41:28 +0100 | [diff] [blame] | 1358 | clean_up5: |
| 1359 | free_irq(irq[1], host); |
| 1360 | clean_up4: |
| 1361 | free_irq(irq[0], host); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1362 | clean_up3: |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1363 | pm_runtime_suspend(&pdev->dev); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1364 | clean_up2: |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1365 | pm_runtime_disable(&pdev->dev); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1366 | clk_disable(host->hclk); |
| 1367 | clean_up1: |
| 1368 | mmc_free_host(mmc); |
| 1369 | clean_up: |
| 1370 | if (reg) |
| 1371 | iounmap(reg); |
| 1372 | return ret; |
| 1373 | } |
| 1374 | |
| 1375 | static int __devexit sh_mmcif_remove(struct platform_device *pdev) |
| 1376 | { |
| 1377 | struct sh_mmcif_host *host = platform_get_drvdata(pdev); |
| 1378 | int irq[2]; |
| 1379 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1380 | host->dying = true; |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1381 | pm_runtime_get_sync(&pdev->dev); |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 1382 | |
Rafael J. Wysocki | efe6a8a | 2012-03-13 01:02:15 +0100 | [diff] [blame] | 1383 | dev_pm_qos_hide_latency_limit(&pdev->dev); |
| 1384 | |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1385 | mmc_remove_host(host->mmc); |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 1386 | sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL); |
| 1387 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1388 | /* |
| 1389 | * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the |
| 1390 | * mmc_remove_host() call above. But swapping order doesn't help either |
| 1391 | * (a query on the linux-mmc mailing list didn't bring any replies). |
| 1392 | */ |
| 1393 | cancel_delayed_work_sync(&host->timeout_work); |
| 1394 | |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 1395 | if (host->addr) |
| 1396 | iounmap(host->addr); |
| 1397 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1398 | irq[0] = platform_get_irq(pdev, 0); |
| 1399 | irq[1] = platform_get_irq(pdev, 1); |
| 1400 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1401 | free_irq(irq[0], host); |
| 1402 | free_irq(irq[1], host); |
| 1403 | |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 1404 | platform_set_drvdata(pdev, NULL); |
| 1405 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1406 | clk_disable(host->hclk); |
| 1407 | mmc_free_host(host->mmc); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1408 | pm_runtime_put_sync(&pdev->dev); |
| 1409 | pm_runtime_disable(&pdev->dev); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1410 | |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1414 | #ifdef CONFIG_PM |
| 1415 | static int sh_mmcif_suspend(struct device *dev) |
| 1416 | { |
| 1417 | struct platform_device *pdev = to_platform_device(dev); |
| 1418 | struct sh_mmcif_host *host = platform_get_drvdata(pdev); |
| 1419 | int ret = mmc_suspend_host(host->mmc); |
| 1420 | |
| 1421 | if (!ret) { |
| 1422 | sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL); |
| 1423 | clk_disable(host->hclk); |
| 1424 | } |
| 1425 | |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | static int sh_mmcif_resume(struct device *dev) |
| 1430 | { |
| 1431 | struct platform_device *pdev = to_platform_device(dev); |
| 1432 | struct sh_mmcif_host *host = platform_get_drvdata(pdev); |
| 1433 | |
| 1434 | clk_enable(host->hclk); |
| 1435 | |
| 1436 | return mmc_resume_host(host->mmc); |
| 1437 | } |
| 1438 | #else |
| 1439 | #define sh_mmcif_suspend NULL |
| 1440 | #define sh_mmcif_resume NULL |
| 1441 | #endif /* CONFIG_PM */ |
| 1442 | |
| 1443 | static const struct dev_pm_ops sh_mmcif_dev_pm_ops = { |
| 1444 | .suspend = sh_mmcif_suspend, |
| 1445 | .resume = sh_mmcif_resume, |
| 1446 | }; |
| 1447 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1448 | static struct platform_driver sh_mmcif_driver = { |
| 1449 | .probe = sh_mmcif_probe, |
| 1450 | .remove = sh_mmcif_remove, |
| 1451 | .driver = { |
| 1452 | .name = DRIVER_NAME, |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1453 | .pm = &sh_mmcif_dev_pm_ops, |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1454 | }, |
| 1455 | }; |
| 1456 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 1457 | module_platform_driver(sh_mmcif_driver); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1458 | |
| 1459 | MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver"); |
| 1460 | MODULE_LICENSE("GPL"); |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 1461 | MODULE_ALIAS("platform:" DRIVER_NAME); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1462 | MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>"); |