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