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