blob: e13c5f41fac48f82e4c97f7732ae641201e436ea [file] [log] [blame]
Yusuke Godafdc50a92010-05-26 14:41:59 -07001/*
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 Liakhovetskif985da12011-12-25 21:07:52 +010019/*
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 Liakhovetski86df1742011-11-23 15:52:30 +010045#include <linux/bitops.h>
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +000046#include <linux/clk.h>
47#include <linux/completion.h>
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +000048#include <linux/delay.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070049#include <linux/dma-mapping.h>
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +000050#include <linux/dmaengine.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070051#include <linux/mmc/card.h>
52#include <linux/mmc/core.h>
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +000053#include <linux/mmc/host.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070054#include <linux/mmc/mmc.h>
55#include <linux/mmc/sdio.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070056#include <linux/mmc/sh_mmcif.h>
Guennadi Liakhovetskie4806062012-06-14 14:24:35 +020057#include <linux/mmc/slot-gpio.h>
Guennadi Liakhovetskibf68a812012-05-01 18:18:16 +020058#include <linux/mod_devicetable.h>
Guennadi Liakhovetski80473102012-12-12 15:38:14 +010059#include <linux/mutex.h>
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +000060#include <linux/pagemap.h>
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +000061#include <linux/platform_device.h>
Rafael J. Wysockiefe6a8a2012-03-13 01:02:15 +010062#include <linux/pm_qos.h>
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +000063#include <linux/pm_runtime.h>
Guennadi Liakhovetskid00cada2013-08-02 14:48:02 +020064#include <linux/sh_dma.h>
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +000065#include <linux/spinlock.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040066#include <linux/module.h>
Yusuke Godafdc50a92010-05-26 14:41:59 -070067
68#define DRIVER_NAME "sh_mmcif"
69#define DRIVER_VERSION "2010-04-28"
70
Yusuke Godafdc50a92010-05-26 14:41:59 -070071/* CE_CMD_SET */
72#define CMD_MASK 0x3f000000
73#define CMD_SET_RTYP_NO ((0 << 23) | (0 << 22))
74#define CMD_SET_RTYP_6B ((0 << 23) | (1 << 22)) /* R1/R1b/R3/R4/R5 */
75#define CMD_SET_RTYP_17B ((1 << 23) | (0 << 22)) /* R2 */
76#define CMD_SET_RBSY (1 << 21) /* R1b */
77#define CMD_SET_CCSEN (1 << 20)
78#define CMD_SET_WDAT (1 << 19) /* 1: on data, 0: no data */
79#define CMD_SET_DWEN (1 << 18) /* 1: write, 0: read */
80#define CMD_SET_CMLTE (1 << 17) /* 1: multi block trans, 0: single */
81#define CMD_SET_CMD12EN (1 << 16) /* 1: CMD12 auto issue */
82#define CMD_SET_RIDXC_INDEX ((0 << 15) | (0 << 14)) /* index check */
83#define CMD_SET_RIDXC_BITS ((0 << 15) | (1 << 14)) /* check bits check */
84#define CMD_SET_RIDXC_NO ((1 << 15) | (0 << 14)) /* no check */
85#define CMD_SET_CRC7C ((0 << 13) | (0 << 12)) /* CRC7 check*/
86#define CMD_SET_CRC7C_BITS ((0 << 13) | (1 << 12)) /* check bits check*/
87#define CMD_SET_CRC7C_INTERNAL ((1 << 13) | (0 << 12)) /* internal CRC7 check*/
88#define CMD_SET_CRC16C (1 << 10) /* 0: CRC16 check*/
89#define CMD_SET_CRCSTE (1 << 8) /* 1: not receive CRC status */
90#define CMD_SET_TBIT (1 << 7) /* 1: tran mission bit "Low" */
91#define CMD_SET_OPDM (1 << 6) /* 1: open/drain */
92#define CMD_SET_CCSH (1 << 5)
Teppei Kamijou555061f2012-12-12 15:38:08 +010093#define CMD_SET_DARS (1 << 2) /* Dual Data Rate */
Yusuke Godafdc50a92010-05-26 14:41:59 -070094#define CMD_SET_DATW_1 ((0 << 1) | (0 << 0)) /* 1bit */
95#define CMD_SET_DATW_4 ((0 << 1) | (1 << 0)) /* 4bit */
96#define CMD_SET_DATW_8 ((1 << 1) | (0 << 0)) /* 8bit */
97
98/* CE_CMD_CTRL */
99#define CMD_CTRL_BREAK (1 << 0)
100
101/* CE_BLOCK_SET */
102#define BLOCK_SIZE_MASK 0x0000ffff
103
Yusuke Godafdc50a92010-05-26 14:41:59 -0700104/* CE_INT */
105#define INT_CCSDE (1 << 29)
106#define INT_CMD12DRE (1 << 26)
107#define INT_CMD12RBE (1 << 25)
108#define INT_CMD12CRE (1 << 24)
109#define INT_DTRANE (1 << 23)
110#define INT_BUFRE (1 << 22)
111#define INT_BUFWEN (1 << 21)
112#define INT_BUFREN (1 << 20)
113#define INT_CCSRCV (1 << 19)
114#define INT_RBSYE (1 << 17)
115#define INT_CRSPE (1 << 16)
116#define INT_CMDVIO (1 << 15)
117#define INT_BUFVIO (1 << 14)
118#define INT_WDATERR (1 << 11)
119#define INT_RDATERR (1 << 10)
120#define INT_RIDXERR (1 << 9)
121#define INT_RSPERR (1 << 8)
122#define INT_CCSTO (1 << 5)
123#define INT_CRCSTO (1 << 4)
124#define INT_WDATTO (1 << 3)
125#define INT_RDATTO (1 << 2)
126#define INT_RBSYTO (1 << 1)
127#define INT_RSPTO (1 << 0)
128#define INT_ERR_STS (INT_CMDVIO | INT_BUFVIO | INT_WDATERR | \
129 INT_RDATERR | INT_RIDXERR | INT_RSPERR | \
130 INT_CCSTO | INT_CRCSTO | INT_WDATTO | \
131 INT_RDATTO | INT_RBSYTO | INT_RSPTO)
132
Guennadi Liakhovetski8af50752012-12-12 15:45:14 +0100133#define INT_ALL (INT_RBSYE | INT_CRSPE | INT_BUFREN | \
134 INT_BUFWEN | INT_CMD12DRE | INT_BUFRE | \
135 INT_DTRANE | INT_CMD12RBE | INT_CMD12CRE)
136
Guennadi Liakhovetski967bcb72013-07-10 21:21:12 +0200137#define INT_CCS (INT_CCSTO | INT_CCSRCV | INT_CCSDE)
138
Yusuke Godafdc50a92010-05-26 14:41:59 -0700139/* CE_INT_MASK */
140#define MASK_ALL 0x00000000
141#define MASK_MCCSDE (1 << 29)
142#define MASK_MCMD12DRE (1 << 26)
143#define MASK_MCMD12RBE (1 << 25)
144#define MASK_MCMD12CRE (1 << 24)
145#define MASK_MDTRANE (1 << 23)
146#define MASK_MBUFRE (1 << 22)
147#define MASK_MBUFWEN (1 << 21)
148#define MASK_MBUFREN (1 << 20)
149#define MASK_MCCSRCV (1 << 19)
150#define MASK_MRBSYE (1 << 17)
151#define MASK_MCRSPE (1 << 16)
152#define MASK_MCMDVIO (1 << 15)
153#define MASK_MBUFVIO (1 << 14)
154#define MASK_MWDATERR (1 << 11)
155#define MASK_MRDATERR (1 << 10)
156#define MASK_MRIDXERR (1 << 9)
157#define MASK_MRSPERR (1 << 8)
158#define MASK_MCCSTO (1 << 5)
159#define MASK_MCRCSTO (1 << 4)
160#define MASK_MWDATTO (1 << 3)
161#define MASK_MRDATTO (1 << 2)
162#define MASK_MRBSYTO (1 << 1)
163#define MASK_MRSPTO (1 << 0)
164
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100165#define MASK_START_CMD (MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR | \
166 MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR | \
Guennadi Liakhovetski967bcb72013-07-10 21:21:12 +0200167 MASK_MCRCSTO | MASK_MWDATTO | \
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100168 MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO)
169
Guennadi Liakhovetski8af50752012-12-12 15:45:14 +0100170#define MASK_CLEAN (INT_ERR_STS | MASK_MRBSYE | MASK_MCRSPE | \
171 MASK_MBUFREN | MASK_MBUFWEN | \
172 MASK_MCMD12DRE | MASK_MBUFRE | MASK_MDTRANE | \
173 MASK_MCMD12RBE | MASK_MCMD12CRE)
174
Yusuke Godafdc50a92010-05-26 14:41:59 -0700175/* CE_HOST_STS1 */
176#define STS1_CMDSEQ (1 << 31)
177
178/* CE_HOST_STS2 */
179#define STS2_CRCSTE (1 << 31)
180#define STS2_CRC16E (1 << 30)
181#define STS2_AC12CRCE (1 << 29)
182#define STS2_RSPCRC7E (1 << 28)
183#define STS2_CRCSTEBE (1 << 27)
184#define STS2_RDATEBE (1 << 26)
185#define STS2_AC12REBE (1 << 25)
186#define STS2_RSPEBE (1 << 24)
187#define STS2_AC12IDXE (1 << 23)
188#define STS2_RSPIDXE (1 << 22)
189#define STS2_CCSTO (1 << 15)
190#define STS2_RDATTO (1 << 14)
191#define STS2_DATBSYTO (1 << 13)
192#define STS2_CRCSTTO (1 << 12)
193#define STS2_AC12BSYTO (1 << 11)
194#define STS2_RSPBSYTO (1 << 10)
195#define STS2_AC12RSPTO (1 << 9)
196#define STS2_RSPTO (1 << 8)
197#define STS2_CRC_ERR (STS2_CRCSTE | STS2_CRC16E | \
198 STS2_AC12CRCE | STS2_RSPCRC7E | STS2_CRCSTEBE)
199#define STS2_TIMEOUT_ERR (STS2_CCSTO | STS2_RDATTO | \
200 STS2_DATBSYTO | STS2_CRCSTTO | \
201 STS2_AC12BSYTO | STS2_RSPBSYTO | \
202 STS2_AC12RSPTO | STS2_RSPTO)
203
Yusuke Godafdc50a92010-05-26 14:41:59 -0700204#define CLKDEV_EMMC_DATA 52000000 /* 52MHz */
205#define CLKDEV_MMC_DATA 20000000 /* 20MHz */
206#define CLKDEV_INIT 400000 /* 400 KHz */
207
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +0000208enum sh_mmcif_state {
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000209 STATE_IDLE,
210 STATE_REQUEST,
211 STATE_IOS,
Guennadi Liakhovetski80473102012-12-12 15:38:14 +0100212 STATE_TIMEOUT,
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000213};
214
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +0000215enum sh_mmcif_wait_for {
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100216 MMCIF_WAIT_FOR_REQUEST,
217 MMCIF_WAIT_FOR_CMD,
218 MMCIF_WAIT_FOR_MREAD,
219 MMCIF_WAIT_FOR_MWRITE,
220 MMCIF_WAIT_FOR_READ,
221 MMCIF_WAIT_FOR_WRITE,
222 MMCIF_WAIT_FOR_READ_END,
223 MMCIF_WAIT_FOR_WRITE_END,
224 MMCIF_WAIT_FOR_STOP,
225};
226
Yusuke Godafdc50a92010-05-26 14:41:59 -0700227struct sh_mmcif_host {
228 struct mmc_host *mmc;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100229 struct mmc_request *mrq;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700230 struct platform_device *pd;
Kuninori Morimoto6aed6782015-04-23 08:15:08 +0000231 struct clk *clk;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700232 int bus_width;
Teppei Kamijou555061f2012-12-12 15:38:08 +0100233 unsigned char timing;
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000234 bool sd_error;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100235 bool dying;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700236 long timeout;
237 void __iomem *addr;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100238 u32 *pio_ptr;
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100239 spinlock_t lock; /* protect sh_mmcif_host::state */
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +0000240 enum sh_mmcif_state state;
241 enum sh_mmcif_wait_for wait_for;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100242 struct delayed_work timeout_work;
243 size_t blocksize;
244 int sg_idx;
245 int sg_blkidx;
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +0000246 bool power;
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +0200247 bool card_present;
Guennadi Liakhovetski967bcb72013-07-10 21:21:12 +0200248 bool ccs_enable; /* Command Completion Signal support */
Guennadi Liakhovetski6d6fd362013-07-10 21:21:13 +0200249 bool clk_ctrl2_enable;
Guennadi Liakhovetski80473102012-12-12 15:38:14 +0100250 struct mutex thread_lock;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700251
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000252 /* DMA support */
253 struct dma_chan *chan_rx;
254 struct dma_chan *chan_tx;
255 struct completion dma_complete;
Linus Walleijf38f94c2011-02-10 16:09:50 +0100256 bool dma_active;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000257};
Yusuke Godafdc50a92010-05-26 14:41:59 -0700258
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +0000259static const struct of_device_id sh_mmcif_of_match[] = {
Kuninori Morimoto70830b42015-04-23 08:13:25 +0000260 { .compatible = "renesas,sh-mmcif" },
261 { }
262};
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +0000263MODULE_DEVICE_TABLE(of, sh_mmcif_of_match);
Kuninori Morimoto70830b42015-04-23 08:13:25 +0000264
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000265#define sh_mmcif_host_to_dev(host) (&host->pd->dev)
266
Yusuke Godafdc50a92010-05-26 14:41:59 -0700267static inline void sh_mmcif_bitset(struct sh_mmcif_host *host,
268 unsigned int reg, u32 val)
269{
Magnus Damm487d9fc2010-05-18 14:42:51 +0000270 writel(val | readl(host->addr + reg), host->addr + reg);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700271}
272
273static inline void sh_mmcif_bitclr(struct sh_mmcif_host *host,
274 unsigned int reg, u32 val)
275{
Magnus Damm487d9fc2010-05-18 14:42:51 +0000276 writel(~val & readl(host->addr + reg), host->addr + reg);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700277}
278
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +0000279static void sh_mmcif_dma_complete(void *arg)
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000280{
281 struct sh_mmcif_host *host = arg;
Guennadi Liakhovetski80473102012-12-12 15:38:14 +0100282 struct mmc_request *mrq = host->mrq;
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000283 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500284
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000285 dev_dbg(dev, "Command completed\n");
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000286
Guennadi Liakhovetski80473102012-12-12 15:38:14 +0100287 if (WARN(!mrq || !mrq->data, "%s: NULL data in DMA completion!\n",
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000288 dev_name(dev)))
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000289 return;
290
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000291 complete(&host->dma_complete);
292}
293
294static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host)
295{
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500296 struct mmc_data *data = host->mrq->data;
297 struct scatterlist *sg = data->sg;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000298 struct dma_async_tx_descriptor *desc = NULL;
299 struct dma_chan *chan = host->chan_rx;
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000300 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000301 dma_cookie_t cookie = -EINVAL;
302 int ret;
303
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500304 ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
Linus Walleij1ed828d2011-02-10 16:09:29 +0100305 DMA_FROM_DEVICE);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000306 if (ret > 0) {
Linus Walleijf38f94c2011-02-10 16:09:50 +0100307 host->dma_active = true;
Alexandre Bounine16052822012-03-08 16:11:18 -0500308 desc = dmaengine_prep_slave_sg(chan, sg, ret,
Vinod Koul05f57992011-10-14 10:45:11 +0530309 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000310 }
311
312 if (desc) {
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +0000313 desc->callback = sh_mmcif_dma_complete;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000314 desc->callback_param = host;
Linus Walleija5ece7d2011-02-10 16:10:00 +0100315 cookie = dmaengine_submit(desc);
316 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN);
317 dma_async_issue_pending(chan);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000318 }
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000319 dev_dbg(dev, "%s(): mapped %d -> %d, cookie %d\n",
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500320 __func__, data->sg_len, ret, cookie);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000321
322 if (!desc) {
323 /* DMA failed, fall back to PIO */
324 if (ret >= 0)
325 ret = -EIO;
326 host->chan_rx = NULL;
Linus Walleijf38f94c2011-02-10 16:09:50 +0100327 host->dma_active = false;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000328 dma_release_channel(chan);
329 /* Free the Tx channel too */
330 chan = host->chan_tx;
331 if (chan) {
332 host->chan_tx = NULL;
333 dma_release_channel(chan);
334 }
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000335 dev_warn(dev,
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000336 "DMA failed: %d, falling back to PIO\n", ret);
337 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
338 }
339
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000340 dev_dbg(dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500341 desc, cookie, data->sg_len);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000342}
343
344static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
345{
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500346 struct mmc_data *data = host->mrq->data;
347 struct scatterlist *sg = data->sg;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000348 struct dma_async_tx_descriptor *desc = NULL;
349 struct dma_chan *chan = host->chan_tx;
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000350 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000351 dma_cookie_t cookie = -EINVAL;
352 int ret;
353
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500354 ret = dma_map_sg(chan->device->dev, sg, data->sg_len,
Linus Walleij1ed828d2011-02-10 16:09:29 +0100355 DMA_TO_DEVICE);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000356 if (ret > 0) {
Linus Walleijf38f94c2011-02-10 16:09:50 +0100357 host->dma_active = true;
Alexandre Bounine16052822012-03-08 16:11:18 -0500358 desc = dmaengine_prep_slave_sg(chan, sg, ret,
Vinod Koul05f57992011-10-14 10:45:11 +0530359 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000360 }
361
362 if (desc) {
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +0000363 desc->callback = sh_mmcif_dma_complete;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000364 desc->callback_param = host;
Linus Walleija5ece7d2011-02-10 16:10:00 +0100365 cookie = dmaengine_submit(desc);
366 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAWEN);
367 dma_async_issue_pending(chan);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000368 }
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000369 dev_dbg(dev, "%s(): mapped %d -> %d, cookie %d\n",
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500370 __func__, data->sg_len, ret, cookie);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000371
372 if (!desc) {
373 /* DMA failed, fall back to PIO */
374 if (ret >= 0)
375 ret = -EIO;
376 host->chan_tx = NULL;
Linus Walleijf38f94c2011-02-10 16:09:50 +0100377 host->dma_active = false;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000378 dma_release_channel(chan);
379 /* Free the Rx channel too */
380 chan = host->chan_rx;
381 if (chan) {
382 host->chan_rx = NULL;
383 dma_release_channel(chan);
384 }
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000385 dev_warn(dev,
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000386 "DMA failed: %d, falling back to PIO\n", ret);
387 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
388 }
389
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000390 dev_dbg(dev, "%s(): desc %p, cookie %d\n", __func__,
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000391 desc, cookie);
392}
393
Laurent Pincharte5a233c2013-10-30 12:34:51 +0100394static struct dma_chan *
395sh_mmcif_request_dma_one(struct sh_mmcif_host *host,
396 struct sh_mmcif_plat_data *pdata,
397 enum dma_transfer_direction direction)
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000398{
Laurent Pinchartd25006e2014-07-16 00:45:12 +0200399 struct dma_slave_config cfg = { 0, };
Laurent Pincharte5a233c2013-10-30 12:34:51 +0100400 struct dma_chan *chan;
Kuninori Morimoto5f48dd02015-02-17 01:47:01 +0000401 void *slave_data = NULL;
Laurent Pincharte5a233c2013-10-30 12:34:51 +0100402 struct resource *res;
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000403 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200404 dma_cap_mask_t mask;
405 int ret;
406
Laurent Pincharte5a233c2013-10-30 12:34:51 +0100407 dma_cap_zero(mask);
408 dma_cap_set(DMA_SLAVE, mask);
409
410 if (pdata)
Kuninori Morimoto5f48dd02015-02-17 01:47:01 +0000411 slave_data = direction == DMA_MEM_TO_DEV ?
412 (void *)pdata->slave_id_tx :
413 (void *)pdata->slave_id_rx;
Laurent Pincharte5a233c2013-10-30 12:34:51 +0100414
415 chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000416 slave_data, dev,
Laurent Pincharte5a233c2013-10-30 12:34:51 +0100417 direction == DMA_MEM_TO_DEV ? "tx" : "rx");
418
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000419 dev_dbg(dev, "%s: %s: got channel %p\n", __func__,
Laurent Pincharte5a233c2013-10-30 12:34:51 +0100420 direction == DMA_MEM_TO_DEV ? "TX" : "RX", chan);
421
422 if (!chan)
423 return NULL;
424
425 res = platform_get_resource(host->pd, IORESOURCE_MEM, 0);
426
Laurent Pincharte5a233c2013-10-30 12:34:51 +0100427 cfg.direction = direction;
Laurent Pinchartd25006e2014-07-16 00:45:12 +0200428
Laurent Pincharte36152a2014-07-16 00:45:13 +0200429 if (direction == DMA_DEV_TO_MEM) {
Laurent Pinchartd25006e2014-07-16 00:45:12 +0200430 cfg.src_addr = res->start + MMCIF_CE_DATA;
Laurent Pincharte36152a2014-07-16 00:45:13 +0200431 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
432 } else {
Laurent Pinchartd25006e2014-07-16 00:45:12 +0200433 cfg.dst_addr = res->start + MMCIF_CE_DATA;
Laurent Pincharte36152a2014-07-16 00:45:13 +0200434 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
435 }
Laurent Pinchartd25006e2014-07-16 00:45:12 +0200436
Laurent Pincharte5a233c2013-10-30 12:34:51 +0100437 ret = dmaengine_slave_config(chan, &cfg);
438 if (ret < 0) {
439 dma_release_channel(chan);
440 return NULL;
441 }
442
443 return chan;
444}
445
446static void sh_mmcif_request_dma(struct sh_mmcif_host *host,
447 struct sh_mmcif_plat_data *pdata)
448{
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000449 struct device *dev = sh_mmcif_host_to_dev(host);
Linus Walleijf38f94c2011-02-10 16:09:50 +0100450 host->dma_active = false;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000451
Guennadi Liakhovetskiacd6d772013-06-24 14:36:34 +0200452 if (pdata) {
453 if (pdata->slave_id_tx <= 0 || pdata->slave_id_rx <= 0)
454 return;
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000455 } else if (!dev->of_node) {
Guennadi Liakhovetskibf68a812012-05-01 18:18:16 +0200456 return;
Guennadi Liakhovetskiacd6d772013-06-24 14:36:34 +0200457 }
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200458
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000459 /* We can only either use DMA for both Tx and Rx or not use it at all */
Laurent Pincharte5a233c2013-10-30 12:34:51 +0100460 host->chan_tx = sh_mmcif_request_dma_one(host, pdata, DMA_MEM_TO_DEV);
Guennadi Liakhovetski0e79f9a2012-07-05 12:29:43 +0200461 if (!host->chan_tx)
462 return;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000463
Laurent Pincharte5a233c2013-10-30 12:34:51 +0100464 host->chan_rx = sh_mmcif_request_dma_one(host, pdata, DMA_DEV_TO_MEM);
465 if (!host->chan_rx) {
466 dma_release_channel(host->chan_tx);
467 host->chan_tx = NULL;
468 }
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000469}
470
471static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
472{
473 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
474 /* Descriptors are freed automatically */
475 if (host->chan_tx) {
476 struct dma_chan *chan = host->chan_tx;
477 host->chan_tx = NULL;
478 dma_release_channel(chan);
479 }
480 if (host->chan_rx) {
481 struct dma_chan *chan = host->chan_rx;
482 host->chan_rx = NULL;
483 dma_release_channel(chan);
484 }
485
Linus Walleijf38f94c2011-02-10 16:09:50 +0100486 host->dma_active = false;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +0000487}
Yusuke Godafdc50a92010-05-26 14:41:59 -0700488
489static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
490{
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000491 struct device *dev = sh_mmcif_host_to_dev(host);
492 struct sh_mmcif_plat_data *p = dev->platform_data;
Guennadi Liakhovetskibf68a812012-05-01 18:18:16 +0200493 bool sup_pclk = p ? p->sup_pclk : false;
Kuninori Morimoto6aed6782015-04-23 08:15:08 +0000494 unsigned int current_clk = clk_get_rate(host->clk);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700495
496 sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
497 sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
498
499 if (!clk)
500 return;
Kuninori Morimoto6aed6782015-04-23 08:15:08 +0000501 if (sup_pclk && clk == current_clk)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700502 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
503 else
504 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
Kuninori Morimoto6aed6782015-04-23 08:15:08 +0000505 ((fls(DIV_ROUND_UP(current_clk,
Simon Hormanf9388252012-03-28 18:01:09 +0900506 clk) - 1) - 1) << 16));
Yusuke Godafdc50a92010-05-26 14:41:59 -0700507
508 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
509}
510
511static void sh_mmcif_sync_reset(struct sh_mmcif_host *host)
512{
513 u32 tmp;
514
Magnus Damm487d9fc2010-05-18 14:42:51 +0000515 tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700516
Magnus Damm487d9fc2010-05-18 14:42:51 +0000517 sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON);
518 sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF);
Guennadi Liakhovetski967bcb72013-07-10 21:21:12 +0200519 if (host->ccs_enable)
520 tmp |= SCCSTO_29;
Guennadi Liakhovetski6d6fd362013-07-10 21:21:13 +0200521 if (host->clk_ctrl2_enable)
522 sh_mmcif_writel(host->addr, MMCIF_CE_CLK_CTRL2, 0x0F0F0000);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700523 sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp |
Guennadi Liakhovetski967bcb72013-07-10 21:21:12 +0200524 SRSPTO_256 | SRBSYTO_29 | SRWDTO_29);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700525 /* byte swap on */
526 sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
527}
528
529static int sh_mmcif_error_manage(struct sh_mmcif_host *host)
530{
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000531 struct device *dev = sh_mmcif_host_to_dev(host);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700532 u32 state1, state2;
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100533 int ret, timeout;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700534
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +0000535 host->sd_error = false;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700536
Magnus Damm487d9fc2010-05-18 14:42:51 +0000537 state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1);
538 state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2);
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000539 dev_dbg(dev, "ERR HOST_STS1 = %08x\n", state1);
540 dev_dbg(dev, "ERR HOST_STS2 = %08x\n", state2);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700541
542 if (state1 & STS1_CMDSEQ) {
543 sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK);
544 sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK);
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100545 for (timeout = 10000000; timeout; timeout--) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000546 if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1)
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100547 & STS1_CMDSEQ))
Yusuke Godafdc50a92010-05-26 14:41:59 -0700548 break;
549 mdelay(1);
550 }
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100551 if (!timeout) {
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000552 dev_err(dev,
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100553 "Forced end of command sequence timeout err\n");
554 return -EIO;
555 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700556 sh_mmcif_sync_reset(host);
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000557 dev_dbg(dev, "Forced end of command sequence\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700558 return -EIO;
559 }
560
561 if (state2 & STS2_CRC_ERR) {
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000562 dev_err(dev, " CRC error: state %u, wait %u\n",
Teppei Kamijoue475b272012-12-12 15:38:18 +0100563 host->state, host->wait_for);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700564 ret = -EIO;
565 } else if (state2 & STS2_TIMEOUT_ERR) {
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000566 dev_err(dev, " Timeout: state %u, wait %u\n",
Teppei Kamijoue475b272012-12-12 15:38:18 +0100567 host->state, host->wait_for);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700568 ret = -ETIMEDOUT;
569 } else {
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000570 dev_dbg(dev, " End/Index error: state %u, wait %u\n",
Teppei Kamijoue475b272012-12-12 15:38:18 +0100571 host->state, host->wait_for);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700572 ret = -EIO;
573 }
574 return ret;
575}
576
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100577static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700578{
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100579 struct mmc_data *data = host->mrq->data;
580
581 host->sg_blkidx += host->blocksize;
582
583 /* data->sg->length must be a multiple of host->blocksize? */
584 BUG_ON(host->sg_blkidx > data->sg->length);
585
586 if (host->sg_blkidx == data->sg->length) {
587 host->sg_blkidx = 0;
588 if (++host->sg_idx < data->sg_len)
589 host->pio_ptr = sg_virt(++data->sg);
590 } else {
591 host->pio_ptr = p;
592 }
593
Guennadi Liakhovetski99eb9d82012-12-12 15:38:13 +0100594 return host->sg_idx != data->sg_len;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100595}
596
597static void sh_mmcif_single_read(struct sh_mmcif_host *host,
598 struct mmc_request *mrq)
599{
600 host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
601 BLOCK_SIZE_MASK) + 3;
602
603 host->wait_for = MMCIF_WAIT_FOR_READ;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700604
Yusuke Godafdc50a92010-05-26 14:41:59 -0700605 /* buf read enable */
606 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100607}
Yusuke Godafdc50a92010-05-26 14:41:59 -0700608
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100609static bool sh_mmcif_read_block(struct sh_mmcif_host *host)
610{
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000611 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100612 struct mmc_data *data = host->mrq->data;
613 u32 *p = sg_virt(data->sg);
614 int i;
615
616 if (host->sd_error) {
617 data->error = sh_mmcif_error_manage(host);
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000618 dev_dbg(dev, "%s(): %d\n", __func__, data->error);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100619 return false;
620 }
621
622 for (i = 0; i < host->blocksize / 4; i++)
Magnus Damm487d9fc2010-05-18 14:42:51 +0000623 *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700624
625 /* buffer read end */
626 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100627 host->wait_for = MMCIF_WAIT_FOR_READ_END;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700628
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100629 return true;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700630}
631
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100632static void sh_mmcif_multi_read(struct sh_mmcif_host *host,
633 struct mmc_request *mrq)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700634{
635 struct mmc_data *data = mrq->data;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700636
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100637 if (!data->sg_len || !data->sg->length)
638 return;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700639
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100640 host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
641 BLOCK_SIZE_MASK;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700642
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100643 host->wait_for = MMCIF_WAIT_FOR_MREAD;
644 host->sg_idx = 0;
645 host->sg_blkidx = 0;
646 host->pio_ptr = sg_virt(data->sg);
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +0100647
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100648 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
649}
650
651static bool sh_mmcif_mread_block(struct sh_mmcif_host *host)
652{
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000653 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100654 struct mmc_data *data = host->mrq->data;
655 u32 *p = host->pio_ptr;
656 int i;
657
658 if (host->sd_error) {
659 data->error = sh_mmcif_error_manage(host);
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000660 dev_dbg(dev, "%s(): %d\n", __func__, data->error);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100661 return false;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700662 }
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100663
664 BUG_ON(!data->sg->length);
665
666 for (i = 0; i < host->blocksize / 4; i++)
667 *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA);
668
669 if (!sh_mmcif_next_block(host, p))
670 return false;
671
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100672 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN);
673
674 return true;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700675}
676
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100677static void sh_mmcif_single_write(struct sh_mmcif_host *host,
Yusuke Godafdc50a92010-05-26 14:41:59 -0700678 struct mmc_request *mrq)
679{
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100680 host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
681 BLOCK_SIZE_MASK) + 3;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700682
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100683 host->wait_for = MMCIF_WAIT_FOR_WRITE;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700684
685 /* buf write enable */
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100686 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
687}
Yusuke Godafdc50a92010-05-26 14:41:59 -0700688
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100689static bool sh_mmcif_write_block(struct sh_mmcif_host *host)
690{
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000691 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100692 struct mmc_data *data = host->mrq->data;
693 u32 *p = sg_virt(data->sg);
694 int i;
695
696 if (host->sd_error) {
697 data->error = sh_mmcif_error_manage(host);
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000698 dev_dbg(dev, "%s(): %d\n", __func__, data->error);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100699 return false;
700 }
701
702 for (i = 0; i < host->blocksize / 4; i++)
Magnus Damm487d9fc2010-05-18 14:42:51 +0000703 sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700704
705 /* buffer write end */
706 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100707 host->wait_for = MMCIF_WAIT_FOR_WRITE_END;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700708
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100709 return true;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700710}
711
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100712static void sh_mmcif_multi_write(struct sh_mmcif_host *host,
713 struct mmc_request *mrq)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700714{
715 struct mmc_data *data = mrq->data;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700716
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100717 if (!data->sg_len || !data->sg->length)
718 return;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700719
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100720 host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) &
721 BLOCK_SIZE_MASK;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700722
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100723 host->wait_for = MMCIF_WAIT_FOR_MWRITE;
724 host->sg_idx = 0;
725 host->sg_blkidx = 0;
726 host->pio_ptr = sg_virt(data->sg);
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +0100727
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100728 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
729}
Yusuke Godafdc50a92010-05-26 14:41:59 -0700730
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100731static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host)
732{
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000733 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100734 struct mmc_data *data = host->mrq->data;
735 u32 *p = host->pio_ptr;
736 int i;
737
738 if (host->sd_error) {
739 data->error = sh_mmcif_error_manage(host);
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000740 dev_dbg(dev, "%s(): %d\n", __func__, data->error);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100741 return false;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700742 }
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100743
744 BUG_ON(!data->sg->length);
745
746 for (i = 0; i < host->blocksize / 4; i++)
747 sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++);
748
749 if (!sh_mmcif_next_block(host, p))
750 return false;
751
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100752 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN);
753
754 return true;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700755}
756
757static void sh_mmcif_get_response(struct sh_mmcif_host *host,
758 struct mmc_command *cmd)
759{
760 if (cmd->flags & MMC_RSP_136) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000761 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3);
762 cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2);
763 cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1);
764 cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700765 } else
Magnus Damm487d9fc2010-05-18 14:42:51 +0000766 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700767}
768
769static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host,
770 struct mmc_command *cmd)
771{
Magnus Damm487d9fc2010-05-18 14:42:51 +0000772 cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700773}
774
775static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host,
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500776 struct mmc_request *mrq)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700777{
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000778 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500779 struct mmc_data *data = mrq->data;
780 struct mmc_command *cmd = mrq->cmd;
781 u32 opc = cmd->opcode;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700782 u32 tmp = 0;
783
784 /* Response Type check */
785 switch (mmc_resp_type(cmd)) {
786 case MMC_RSP_NONE:
787 tmp |= CMD_SET_RTYP_NO;
788 break;
789 case MMC_RSP_R1:
790 case MMC_RSP_R1B:
791 case MMC_RSP_R3:
792 tmp |= CMD_SET_RTYP_6B;
793 break;
794 case MMC_RSP_R2:
795 tmp |= CMD_SET_RTYP_17B;
796 break;
797 default:
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000798 dev_err(dev, "Unsupported response type.\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700799 break;
800 }
801 switch (opc) {
802 /* RBSY */
Teppei Kamijoua812ba02012-12-12 15:38:10 +0100803 case MMC_SLEEP_AWAKE:
Yusuke Godafdc50a92010-05-26 14:41:59 -0700804 case MMC_SWITCH:
805 case MMC_STOP_TRANSMISSION:
806 case MMC_SET_WRITE_PROT:
807 case MMC_CLR_WRITE_PROT:
808 case MMC_ERASE:
Yusuke Godafdc50a92010-05-26 14:41:59 -0700809 tmp |= CMD_SET_RBSY;
810 break;
811 }
812 /* WDAT / DATW */
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500813 if (data) {
Yusuke Godafdc50a92010-05-26 14:41:59 -0700814 tmp |= CMD_SET_WDAT;
815 switch (host->bus_width) {
816 case MMC_BUS_WIDTH_1:
817 tmp |= CMD_SET_DATW_1;
818 break;
819 case MMC_BUS_WIDTH_4:
820 tmp |= CMD_SET_DATW_4;
821 break;
822 case MMC_BUS_WIDTH_8:
823 tmp |= CMD_SET_DATW_8;
824 break;
825 default:
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000826 dev_err(dev, "Unsupported bus width.\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -0700827 break;
828 }
Teppei Kamijou555061f2012-12-12 15:38:08 +0100829 switch (host->timing) {
Seungwon Jeon4039ff472014-03-14 21:12:33 +0900830 case MMC_TIMING_MMC_DDR52:
Teppei Kamijou555061f2012-12-12 15:38:08 +0100831 /*
832 * MMC core will only set this timing, if the host
Seungwon Jeon4039ff472014-03-14 21:12:33 +0900833 * advertises the MMC_CAP_1_8V_DDR/MMC_CAP_1_2V_DDR
834 * capability. MMCIF implementations with this
835 * capability, e.g. sh73a0, will have to set it
836 * in their platform data.
Teppei Kamijou555061f2012-12-12 15:38:08 +0100837 */
838 tmp |= CMD_SET_DARS;
839 break;
840 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700841 }
842 /* DWEN */
843 if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK)
844 tmp |= CMD_SET_DWEN;
845 /* CMLTE/CMD12EN */
846 if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) {
847 tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN;
848 sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET,
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500849 data->blocks << 16);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700850 }
851 /* RIDXC[1:0] check bits */
852 if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID ||
853 opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
854 tmp |= CMD_SET_RIDXC_BITS;
855 /* RCRC7C[1:0] check bits */
856 if (opc == MMC_SEND_OP_COND)
857 tmp |= CMD_SET_CRC7C_BITS;
858 /* RCRC7C[1:0] internal CRC7 */
859 if (opc == MMC_ALL_SEND_CID ||
860 opc == MMC_SEND_CSD || opc == MMC_SEND_CID)
861 tmp |= CMD_SET_CRC7C_INTERNAL;
862
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500863 return (opc << 24) | tmp;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700864}
865
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +0000866static int sh_mmcif_data_trans(struct sh_mmcif_host *host,
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100867 struct mmc_request *mrq, u32 opc)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700868{
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000869 struct device *dev = sh_mmcif_host_to_dev(host);
870
Yusuke Godafdc50a92010-05-26 14:41:59 -0700871 switch (opc) {
872 case MMC_READ_MULTIPLE_BLOCK:
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100873 sh_mmcif_multi_read(host, mrq);
874 return 0;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700875 case MMC_WRITE_MULTIPLE_BLOCK:
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100876 sh_mmcif_multi_write(host, mrq);
877 return 0;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700878 case MMC_WRITE_BLOCK:
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100879 sh_mmcif_single_write(host, mrq);
880 return 0;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700881 case MMC_READ_SINGLE_BLOCK:
882 case MMC_SEND_EXT_CSD:
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100883 sh_mmcif_single_read(host, mrq);
884 return 0;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700885 default:
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000886 dev_err(dev, "Unsupported CMD%d\n", opc);
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100887 return -EINVAL;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700888 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700889}
890
891static void sh_mmcif_start_cmd(struct sh_mmcif_host *host,
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100892 struct mmc_request *mrq)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700893{
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100894 struct mmc_command *cmd = mrq->cmd;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100895 u32 opc = cmd->opcode;
896 u32 mask;
Kouichi Tomitadbb42d92015-02-15 23:46:46 +0900897 unsigned long flags;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700898
Yusuke Godafdc50a92010-05-26 14:41:59 -0700899 switch (opc) {
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100900 /* response busy check */
Teppei Kamijoua812ba02012-12-12 15:38:10 +0100901 case MMC_SLEEP_AWAKE:
Yusuke Godafdc50a92010-05-26 14:41:59 -0700902 case MMC_SWITCH:
903 case MMC_STOP_TRANSMISSION:
904 case MMC_SET_WRITE_PROT:
905 case MMC_CLR_WRITE_PROT:
906 case MMC_ERASE:
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100907 mask = MASK_START_CMD | MASK_MRBSYE;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700908 break;
909 default:
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100910 mask = MASK_START_CMD | MASK_MCRSPE;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700911 break;
912 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700913
Guennadi Liakhovetski967bcb72013-07-10 21:21:12 +0200914 if (host->ccs_enable)
915 mask |= MASK_MCCSTO;
916
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500917 if (mrq->data) {
Magnus Damm487d9fc2010-05-18 14:42:51 +0000918 sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0);
919 sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET,
920 mrq->data->blksz);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700921 }
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500922 opc = sh_mmcif_set_cmd(host, mrq);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700923
Guennadi Liakhovetski967bcb72013-07-10 21:21:12 +0200924 if (host->ccs_enable)
925 sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0);
926 else
927 sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0 | INT_CCS);
Magnus Damm487d9fc2010-05-18 14:42:51 +0000928 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700929 /* set arg */
Magnus Damm487d9fc2010-05-18 14:42:51 +0000930 sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700931 /* set cmd */
Kouichi Tomitadbb42d92015-02-15 23:46:46 +0900932 spin_lock_irqsave(&host->lock, flags);
Magnus Damm487d9fc2010-05-18 14:42:51 +0000933 sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700934
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100935 host->wait_for = MMCIF_WAIT_FOR_CMD;
936 schedule_delayed_work(&host->timeout_work, host->timeout);
Kouichi Tomitadbb42d92015-02-15 23:46:46 +0900937 spin_unlock_irqrestore(&host->lock, flags);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700938}
939
940static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host,
Guennadi Liakhovetskiee4b8882011-12-14 19:31:52 +0100941 struct mmc_request *mrq)
Yusuke Godafdc50a92010-05-26 14:41:59 -0700942{
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000943 struct device *dev = sh_mmcif_host_to_dev(host);
944
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500945 switch (mrq->cmd->opcode) {
946 case MMC_READ_MULTIPLE_BLOCK:
Yusuke Godafdc50a92010-05-26 14:41:59 -0700947 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE);
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500948 break;
949 case MMC_WRITE_MULTIPLE_BLOCK:
Yusuke Godafdc50a92010-05-26 14:41:59 -0700950 sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE);
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500951 break;
952 default:
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000953 dev_err(dev, "unsupported stop cmd\n");
Guennadi Liakhovetski69983402011-12-26 12:52:13 -0500954 mrq->stop->error = sh_mmcif_error_manage(host);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700955 return;
956 }
957
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100958 host->wait_for = MMCIF_WAIT_FOR_STOP;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700959}
960
961static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq)
962{
963 struct sh_mmcif_host *host = mmc_priv(mmc);
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000964 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000965 unsigned long flags;
966
967 spin_lock_irqsave(&host->lock, flags);
968 if (host->state != STATE_IDLE) {
Kuninori Morimoto585c3a52015-05-14 07:21:18 +0000969 dev_dbg(dev, "%s() rejected, state %u\n",
970 __func__, host->state);
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000971 spin_unlock_irqrestore(&host->lock, flags);
972 mrq->cmd->error = -EAGAIN;
973 mmc_request_done(mmc, mrq);
974 return;
975 }
976
977 host->state = STATE_REQUEST;
978 spin_unlock_irqrestore(&host->lock, flags);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700979
980 switch (mrq->cmd->opcode) {
981 /* MMCIF does not support SD/SDIO command */
Laurent Pinchart7541ca92012-06-12 22:56:09 +0200982 case MMC_SLEEP_AWAKE: /* = SD_IO_SEND_OP_COND (5) */
983 case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */
984 if ((mrq->cmd->flags & MMC_CMD_MASK) != MMC_CMD_BCR)
985 break;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700986 case MMC_APP_CMD:
Teppei Kamijou92ff0c52012-12-12 15:38:05 +0100987 case SD_IO_RW_DIRECT:
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +0000988 host->state = STATE_IDLE;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700989 mrq->cmd->error = -ETIMEDOUT;
990 mmc_request_done(mmc, mrq);
991 return;
Yusuke Godafdc50a92010-05-26 14:41:59 -0700992 default:
993 break;
994 }
Yusuke Godafdc50a92010-05-26 14:41:59 -0700995
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100996 host->mrq = mrq;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +0100997
998 sh_mmcif_start_cmd(host, mrq);
Yusuke Godafdc50a92010-05-26 14:41:59 -0700999}
1000
Kuninori Morimoto9bb09a32015-04-23 08:16:04 +00001001static void sh_mmcif_clk_setup(struct sh_mmcif_host *host)
Guennadi Liakhovetskia6609262012-04-19 18:02:50 +02001002{
Kuninori Morimoto9bb09a32015-04-23 08:16:04 +00001003 unsigned int clk = clk_get_rate(host->clk);
Guennadi Liakhovetskia6609262012-04-19 18:02:50 +02001004
Kuninori Morimoto9bb09a32015-04-23 08:16:04 +00001005 host->mmc->f_max = clk / 2;
1006 host->mmc->f_min = clk / 512;
Guennadi Liakhovetskia6609262012-04-19 18:02:50 +02001007}
1008
Guennadi Liakhovetski7d17baa2012-04-20 18:27:13 +02001009static void sh_mmcif_set_power(struct sh_mmcif_host *host, struct mmc_ios *ios)
1010{
Guennadi Liakhovetski7d17baa2012-04-20 18:27:13 +02001011 struct mmc_host *mmc = host->mmc;
1012
Guennadi Liakhovetski7d17baa2012-04-20 18:27:13 +02001013 if (!IS_ERR(mmc->supply.vmmc))
1014 /* Errors ignored... */
1015 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1016 ios->power_mode ? ios->vdd : 0);
1017}
1018
Yusuke Godafdc50a92010-05-26 14:41:59 -07001019static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1020{
1021 struct sh_mmcif_host *host = mmc_priv(mmc);
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001022 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +00001023 unsigned long flags;
1024
1025 spin_lock_irqsave(&host->lock, flags);
1026 if (host->state != STATE_IDLE) {
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001027 dev_dbg(dev, "%s() rejected, state %u\n",
1028 __func__, host->state);
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +00001029 spin_unlock_irqrestore(&host->lock, flags);
1030 return;
1031 }
1032
1033 host->state = STATE_IOS;
1034 spin_unlock_irqrestore(&host->lock, flags);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001035
Guennadi Liakhovetskif5e0cec2011-02-25 16:58:38 +01001036 if (ios->power_mode == MMC_POWER_UP) {
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +02001037 if (!host->card_present) {
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001038 /* See if we also get DMA */
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001039 sh_mmcif_request_dma(host, dev->platform_data);
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +02001040 host->card_present = true;
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001041 }
Guennadi Liakhovetski7d17baa2012-04-20 18:27:13 +02001042 sh_mmcif_set_power(host, ios);
Guennadi Liakhovetskif5e0cec2011-02-25 16:58:38 +01001043 } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
1044 /* clock stop */
1045 sh_mmcif_clock_control(host, 0);
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001046 if (ios->power_mode == MMC_POWER_OFF) {
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +02001047 if (host->card_present) {
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001048 sh_mmcif_release_dma(host);
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +02001049 host->card_present = false;
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001050 }
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +02001051 }
1052 if (host->power) {
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001053 pm_runtime_put_sync(dev);
Kuninori Morimoto6aed6782015-04-23 08:15:08 +00001054 clk_disable_unprepare(host->clk);
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +02001055 host->power = false;
Guennadi Liakhovetski7d17baa2012-04-20 18:27:13 +02001056 if (ios->power_mode == MMC_POWER_OFF)
1057 sh_mmcif_set_power(host, ios);
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001058 }
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +00001059 host->state = STATE_IDLE;
Guennadi Liakhovetskif5e0cec2011-02-25 16:58:38 +01001060 return;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001061 }
1062
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +02001063 if (ios->clock) {
1064 if (!host->power) {
Kuninori Morimoto9bb09a32015-04-23 08:16:04 +00001065 clk_prepare_enable(host->clk);
1066
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001067 pm_runtime_get_sync(dev);
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +02001068 host->power = true;
1069 sh_mmcif_sync_reset(host);
1070 }
Yusuke Godafdc50a92010-05-26 14:41:59 -07001071 sh_mmcif_clock_control(host, ios->clock);
Guennadi Liakhovetskic9b0cef2011-05-26 15:33:30 +02001072 }
Yusuke Godafdc50a92010-05-26 14:41:59 -07001073
Teppei Kamijou555061f2012-12-12 15:38:08 +01001074 host->timing = ios->timing;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001075 host->bus_width = ios->bus_width;
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +00001076 host->state = STATE_IDLE;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001077}
1078
Arnd Hannemann777271d2010-08-24 17:27:01 +02001079static int sh_mmcif_get_cd(struct mmc_host *mmc)
1080{
1081 struct sh_mmcif_host *host = mmc_priv(mmc);
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001082 struct device *dev = sh_mmcif_host_to_dev(host);
1083 struct sh_mmcif_plat_data *p = dev->platform_data;
Guennadi Liakhovetskie4806062012-06-14 14:24:35 +02001084 int ret = mmc_gpio_get_cd(mmc);
1085
1086 if (ret >= 0)
1087 return ret;
Arnd Hannemann777271d2010-08-24 17:27:01 +02001088
Guennadi Liakhovetskibf68a812012-05-01 18:18:16 +02001089 if (!p || !p->get_cd)
Arnd Hannemann777271d2010-08-24 17:27:01 +02001090 return -ENOSYS;
1091 else
1092 return p->get_cd(host->pd);
1093}
1094
Yusuke Godafdc50a92010-05-26 14:41:59 -07001095static struct mmc_host_ops sh_mmcif_ops = {
1096 .request = sh_mmcif_request,
1097 .set_ios = sh_mmcif_set_ios,
Arnd Hannemann777271d2010-08-24 17:27:01 +02001098 .get_cd = sh_mmcif_get_cd,
Yusuke Godafdc50a92010-05-26 14:41:59 -07001099};
1100
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001101static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host)
1102{
1103 struct mmc_command *cmd = host->mrq->cmd;
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001104 struct mmc_data *data = host->mrq->data;
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001105 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001106 long time;
1107
1108 if (host->sd_error) {
1109 switch (cmd->opcode) {
1110 case MMC_ALL_SEND_CID:
1111 case MMC_SELECT_CARD:
1112 case MMC_APP_CMD:
1113 cmd->error = -ETIMEDOUT;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001114 break;
1115 default:
1116 cmd->error = sh_mmcif_error_manage(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001117 break;
1118 }
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001119 dev_dbg(dev, "CMD%d error %d\n",
Teppei Kamijoue475b272012-12-12 15:38:18 +01001120 cmd->opcode, cmd->error);
Guennadi Liakhovetskiaba9d642012-12-12 15:38:15 +01001121 host->sd_error = false;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001122 return false;
1123 }
1124 if (!(cmd->flags & MMC_RSP_PRESENT)) {
1125 cmd->error = 0;
1126 return false;
1127 }
1128
1129 sh_mmcif_get_response(host, cmd);
1130
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001131 if (!data)
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001132 return false;
1133
Guennadi Liakhovetski90f1cb42012-12-12 15:38:16 +01001134 /*
1135 * Completion can be signalled from DMA callback and error, so, have to
1136 * reset here, before setting .dma_active
1137 */
1138 init_completion(&host->dma_complete);
1139
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001140 if (data->flags & MMC_DATA_READ) {
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001141 if (host->chan_rx)
1142 sh_mmcif_start_dma_rx(host);
1143 } else {
1144 if (host->chan_tx)
1145 sh_mmcif_start_dma_tx(host);
1146 }
1147
1148 if (!host->dma_active) {
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001149 data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode);
Guennadi Liakhovetski99eb9d82012-12-12 15:38:13 +01001150 return !data->error;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001151 }
1152
1153 /* Running in the IRQ thread, can sleep */
1154 time = wait_for_completion_interruptible_timeout(&host->dma_complete,
1155 host->timeout);
Teppei Kamijoueae30982012-12-12 15:38:12 +01001156
1157 if (data->flags & MMC_DATA_READ)
1158 dma_unmap_sg(host->chan_rx->device->dev,
1159 data->sg, data->sg_len,
1160 DMA_FROM_DEVICE);
1161 else
1162 dma_unmap_sg(host->chan_tx->device->dev,
1163 data->sg, data->sg_len,
1164 DMA_TO_DEVICE);
1165
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001166 if (host->sd_error) {
1167 dev_err(host->mmc->parent,
1168 "Error IRQ while waiting for DMA completion!\n");
1169 /* Woken up by an error IRQ: abort DMA */
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001170 data->error = sh_mmcif_error_manage(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001171 } else if (!time) {
Teppei Kamijoue475b272012-12-12 15:38:18 +01001172 dev_err(host->mmc->parent, "DMA timeout!\n");
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001173 data->error = -ETIMEDOUT;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001174 } else if (time < 0) {
Teppei Kamijoue475b272012-12-12 15:38:18 +01001175 dev_err(host->mmc->parent,
1176 "wait_for_completion_...() error %ld!\n", time);
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001177 data->error = time;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001178 }
1179 sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC,
1180 BUF_ACC_DMAREN | BUF_ACC_DMAWEN);
1181 host->dma_active = false;
1182
Teppei Kamijoueae30982012-12-12 15:38:12 +01001183 if (data->error) {
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001184 data->bytes_xfered = 0;
Teppei Kamijoueae30982012-12-12 15:38:12 +01001185 /* Abort DMA */
1186 if (data->flags & MMC_DATA_READ)
1187 dmaengine_terminate_all(host->chan_rx);
1188 else
1189 dmaengine_terminate_all(host->chan_tx);
1190 }
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001191
1192 return false;
1193}
1194
1195static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id)
1196{
1197 struct sh_mmcif_host *host = dev_id;
Guennadi Liakhovetski80473102012-12-12 15:38:14 +01001198 struct mmc_request *mrq;
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001199 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +01001200 bool wait = false;
Kouichi Tomitadbb42d92015-02-15 23:46:46 +09001201 unsigned long flags;
1202 int wait_work;
1203
1204 spin_lock_irqsave(&host->lock, flags);
1205 wait_work = host->wait_for;
1206 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001207
1208 cancel_delayed_work_sync(&host->timeout_work);
1209
Guennadi Liakhovetski80473102012-12-12 15:38:14 +01001210 mutex_lock(&host->thread_lock);
1211
1212 mrq = host->mrq;
1213 if (!mrq) {
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001214 dev_dbg(dev, "IRQ thread state %u, wait %u: NULL mrq!\n",
Guennadi Liakhovetski80473102012-12-12 15:38:14 +01001215 host->state, host->wait_for);
1216 mutex_unlock(&host->thread_lock);
1217 return IRQ_HANDLED;
1218 }
1219
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001220 /*
1221 * All handlers return true, if processing continues, and false, if the
1222 * request has to be completed - successfully or not
1223 */
Kouichi Tomitadbb42d92015-02-15 23:46:46 +09001224 switch (wait_work) {
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001225 case MMCIF_WAIT_FOR_REQUEST:
1226 /* We're too late, the timeout has already kicked in */
Guennadi Liakhovetski80473102012-12-12 15:38:14 +01001227 mutex_unlock(&host->thread_lock);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001228 return IRQ_HANDLED;
1229 case MMCIF_WAIT_FOR_CMD:
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +01001230 /* Wait for data? */
1231 wait = sh_mmcif_end_cmd(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001232 break;
1233 case MMCIF_WAIT_FOR_MREAD:
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +01001234 /* Wait for more data? */
1235 wait = sh_mmcif_mread_block(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001236 break;
1237 case MMCIF_WAIT_FOR_READ:
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +01001238 /* Wait for data end? */
1239 wait = sh_mmcif_read_block(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001240 break;
1241 case MMCIF_WAIT_FOR_MWRITE:
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +01001242 /* Wait data to write? */
1243 wait = sh_mmcif_mwrite_block(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001244 break;
1245 case MMCIF_WAIT_FOR_WRITE:
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +01001246 /* Wait for data end? */
1247 wait = sh_mmcif_write_block(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001248 break;
1249 case MMCIF_WAIT_FOR_STOP:
1250 if (host->sd_error) {
1251 mrq->stop->error = sh_mmcif_error_manage(host);
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001252 dev_dbg(dev, "%s(): %d\n", __func__, mrq->stop->error);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001253 break;
1254 }
1255 sh_mmcif_get_cmd12response(host, mrq->stop);
1256 mrq->stop->error = 0;
1257 break;
1258 case MMCIF_WAIT_FOR_READ_END:
1259 case MMCIF_WAIT_FOR_WRITE_END:
Teppei Kamijoue475b272012-12-12 15:38:18 +01001260 if (host->sd_error) {
Guennadi Liakhovetski91ab2522012-08-22 06:49:47 +00001261 mrq->data->error = sh_mmcif_error_manage(host);
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001262 dev_dbg(dev, "%s(): %d\n", __func__, mrq->data->error);
Teppei Kamijoue475b272012-12-12 15:38:18 +01001263 }
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001264 break;
1265 default:
1266 BUG();
1267 }
1268
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +01001269 if (wait) {
1270 schedule_delayed_work(&host->timeout_work, host->timeout);
1271 /* Wait for more data */
Guennadi Liakhovetski80473102012-12-12 15:38:14 +01001272 mutex_unlock(&host->thread_lock);
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +01001273 return IRQ_HANDLED;
1274 }
1275
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001276 if (host->wait_for != MMCIF_WAIT_FOR_STOP) {
Guennadi Liakhovetski91ab2522012-08-22 06:49:47 +00001277 struct mmc_data *data = mrq->data;
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001278 if (!mrq->cmd->error && data && !data->error)
1279 data->bytes_xfered =
1280 data->blocks * data->blksz;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001281
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001282 if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) {
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001283 sh_mmcif_stop_cmd(host, mrq);
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +01001284 if (!mrq->stop->error) {
1285 schedule_delayed_work(&host->timeout_work, host->timeout);
Guennadi Liakhovetski80473102012-12-12 15:38:14 +01001286 mutex_unlock(&host->thread_lock);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001287 return IRQ_HANDLED;
Guennadi Liakhovetski5df460b2012-12-12 15:38:11 +01001288 }
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001289 }
1290 }
1291
1292 host->wait_for = MMCIF_WAIT_FOR_REQUEST;
1293 host->state = STATE_IDLE;
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001294 host->mrq = NULL;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001295 mmc_request_done(host->mmc, mrq);
1296
Guennadi Liakhovetski80473102012-12-12 15:38:14 +01001297 mutex_unlock(&host->thread_lock);
1298
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001299 return IRQ_HANDLED;
1300}
1301
Yusuke Godafdc50a92010-05-26 14:41:59 -07001302static irqreturn_t sh_mmcif_intr(int irq, void *dev_id)
1303{
1304 struct sh_mmcif_host *host = dev_id;
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001305 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetski967bcb72013-07-10 21:21:12 +02001306 u32 state, mask;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001307
Magnus Damm487d9fc2010-05-18 14:42:51 +00001308 state = sh_mmcif_readl(host->addr, MMCIF_CE_INT);
Guennadi Liakhovetski967bcb72013-07-10 21:21:12 +02001309 mask = sh_mmcif_readl(host->addr, MMCIF_CE_INT_MASK);
1310 if (host->ccs_enable)
1311 sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~(state & mask));
1312 else
1313 sh_mmcif_writel(host->addr, MMCIF_CE_INT, INT_CCS | ~(state & mask));
Guennadi Liakhovetski8af50752012-12-12 15:45:14 +01001314 sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state & MASK_CLEAN);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001315
Guennadi Liakhovetski8af50752012-12-12 15:45:14 +01001316 if (state & ~MASK_CLEAN)
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001317 dev_dbg(dev, "IRQ state = 0x%08x incompletely cleared\n",
Guennadi Liakhovetski8af50752012-12-12 15:45:14 +01001318 state);
1319
1320 if (state & INT_ERR_STS || state & ~INT_ALL) {
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001321 host->sd_error = true;
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001322 dev_dbg(dev, "int err state = 0x%08x\n", state);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001323 }
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001324 if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) {
Guennadi Liakhovetski8af50752012-12-12 15:45:14 +01001325 if (!host->mrq)
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001326 dev_dbg(dev, "NULL IRQ state = 0x%08x\n", state);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001327 if (!host->dma_active)
1328 return IRQ_WAKE_THREAD;
1329 else if (host->sd_error)
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +00001330 sh_mmcif_dma_complete(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001331 } else {
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001332 dev_dbg(dev, "Unexpected IRQ 0x%x\n", state);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001333 }
Yusuke Godafdc50a92010-05-26 14:41:59 -07001334
1335 return IRQ_HANDLED;
1336}
1337
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +00001338static void sh_mmcif_timeout_work(struct work_struct *work)
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001339{
1340 struct delayed_work *d = container_of(work, struct delayed_work, work);
1341 struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work);
1342 struct mmc_request *mrq = host->mrq;
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001343 struct device *dev = sh_mmcif_host_to_dev(host);
Guennadi Liakhovetski80473102012-12-12 15:38:14 +01001344 unsigned long flags;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001345
1346 if (host->dying)
1347 /* Don't run after mmc_remove_host() */
1348 return;
1349
Guennadi Liakhovetski80473102012-12-12 15:38:14 +01001350 spin_lock_irqsave(&host->lock, flags);
1351 if (host->state == STATE_IDLE) {
1352 spin_unlock_irqrestore(&host->lock, flags);
1353 return;
1354 }
1355
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001356 dev_err(dev, "Timeout waiting for %u on CMD%u\n",
Kouichi Tomita4cbd5222015-02-15 23:46:47 +09001357 host->wait_for, mrq->cmd->opcode);
1358
Guennadi Liakhovetski80473102012-12-12 15:38:14 +01001359 host->state = STATE_TIMEOUT;
1360 spin_unlock_irqrestore(&host->lock, flags);
1361
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001362 /*
1363 * Handle races with cancel_delayed_work(), unless
1364 * cancel_delayed_work_sync() is used
1365 */
1366 switch (host->wait_for) {
1367 case MMCIF_WAIT_FOR_CMD:
1368 mrq->cmd->error = sh_mmcif_error_manage(host);
1369 break;
1370 case MMCIF_WAIT_FOR_STOP:
1371 mrq->stop->error = sh_mmcif_error_manage(host);
1372 break;
1373 case MMCIF_WAIT_FOR_MREAD:
1374 case MMCIF_WAIT_FOR_MWRITE:
1375 case MMCIF_WAIT_FOR_READ:
1376 case MMCIF_WAIT_FOR_WRITE:
1377 case MMCIF_WAIT_FOR_READ_END:
1378 case MMCIF_WAIT_FOR_WRITE_END:
Guennadi Liakhovetski69983402011-12-26 12:52:13 -05001379 mrq->data->error = sh_mmcif_error_manage(host);
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001380 break;
1381 default:
1382 BUG();
1383 }
1384
1385 host->state = STATE_IDLE;
1386 host->wait_for = MMCIF_WAIT_FOR_REQUEST;
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001387 host->mrq = NULL;
1388 mmc_request_done(host->mmc, mrq);
1389}
1390
Guennadi Liakhovetski7d17baa2012-04-20 18:27:13 +02001391static void sh_mmcif_init_ocr(struct sh_mmcif_host *host)
1392{
Kuninori Morimoto585c3a52015-05-14 07:21:18 +00001393 struct device *dev = sh_mmcif_host_to_dev(host);
1394 struct sh_mmcif_plat_data *pd = dev->platform_data;
Guennadi Liakhovetski7d17baa2012-04-20 18:27:13 +02001395 struct mmc_host *mmc = host->mmc;
1396
1397 mmc_regulator_get_supply(mmc);
1398
Guennadi Liakhovetskibf68a812012-05-01 18:18:16 +02001399 if (!pd)
1400 return;
1401
Guennadi Liakhovetski7d17baa2012-04-20 18:27:13 +02001402 if (!mmc->ocr_avail)
1403 mmc->ocr_avail = pd->ocr;
1404 else if (pd->ocr)
1405 dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
1406}
1407
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001408static int sh_mmcif_probe(struct platform_device *pdev)
Yusuke Godafdc50a92010-05-26 14:41:59 -07001409{
1410 int ret = 0, irq[2];
1411 struct mmc_host *mmc;
Guennadi Liakhovetskie47bf322010-11-24 10:05:18 +00001412 struct sh_mmcif_host *host;
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001413 struct device *dev = &pdev->dev;
1414 struct sh_mmcif_plat_data *pd = dev->platform_data;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001415 struct resource *res;
1416 void __iomem *reg;
Shinya Kuribayashi2cd5b3e2013-01-14 14:12:36 -05001417 const char *name;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001418
1419 irq[0] = platform_get_irq(pdev, 0);
1420 irq[1] = platform_get_irq(pdev, 1);
Shinya Kuribayashi2cd5b3e2013-01-14 14:12:36 -05001421 if (irq[0] < 0) {
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001422 dev_err(dev, "Get irq error\n");
Yusuke Godafdc50a92010-05-26 14:41:59 -07001423 return -ENXIO;
1424 }
Ben Dooks18f55fc2014-06-04 12:42:09 +01001425
Yusuke Godafdc50a92010-05-26 14:41:59 -07001426 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001427 reg = devm_ioremap_resource(dev, res);
Ben Dooks18f55fc2014-06-04 12:42:09 +01001428 if (IS_ERR(reg))
1429 return PTR_ERR(reg);
Guennadi Liakhovetskie1aae2e2012-04-19 16:15:52 +02001430
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001431 mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), dev);
Ben Dooks18f55fc2014-06-04 12:42:09 +01001432 if (!mmc)
1433 return -ENOMEM;
Simon Baatz2c9054d2013-06-09 22:14:12 +02001434
1435 ret = mmc_of_parse(mmc);
1436 if (ret < 0)
Ben Dooks46991002014-06-04 12:42:10 +01001437 goto err_host;
Simon Baatz2c9054d2013-06-09 22:14:12 +02001438
Yusuke Godafdc50a92010-05-26 14:41:59 -07001439 host = mmc_priv(mmc);
1440 host->mmc = mmc;
1441 host->addr = reg;
Takeshi Kiharabad43712015-04-30 02:03:51 +09001442 host->timeout = msecs_to_jiffies(10000);
Guennadi Liakhovetski967bcb72013-07-10 21:21:12 +02001443 host->ccs_enable = !pd || !pd->ccs_unsupported;
Guennadi Liakhovetski6d6fd362013-07-10 21:21:13 +02001444 host->clk_ctrl2_enable = pd && pd->clk_ctrl2_present;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001445
Yusuke Godafdc50a92010-05-26 14:41:59 -07001446 host->pd = pdev;
1447
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +00001448 spin_lock_init(&host->lock);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001449
1450 mmc->ops = &sh_mmcif_ops;
Guennadi Liakhovetski7d17baa2012-04-20 18:27:13 +02001451 sh_mmcif_init_ocr(host);
1452
Guennadi Liakhovetskieca889f2013-02-15 16:13:54 +01001453 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY;
Guennadi Liakhovetskibf68a812012-05-01 18:18:16 +02001454 if (pd && pd->caps)
Yusuke Godafdc50a92010-05-26 14:41:59 -07001455 mmc->caps |= pd->caps;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001456 mmc->max_segs = 32;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001457 mmc->max_blk_size = 512;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001458 mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs;
1459 mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001460 mmc->max_seg_size = mmc->max_req_size;
1461
Yusuke Godafdc50a92010-05-26 14:41:59 -07001462 platform_set_drvdata(pdev, host);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001463
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001464 pm_runtime_enable(dev);
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001465 host->power = false;
1466
Kuninori Morimoto6aed6782015-04-23 08:15:08 +00001467 host->clk = devm_clk_get(dev, NULL);
1468 if (IS_ERR(host->clk)) {
1469 ret = PTR_ERR(host->clk);
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001470 dev_err(dev, "cannot get clock: %d\n", ret);
Ben Dooks46991002014-06-04 12:42:10 +01001471 goto err_pm;
Guennadi Liakhovetskib2891742012-04-19 18:02:05 +02001472 }
Kuninori Morimoto9bb09a32015-04-23 08:16:04 +00001473
1474 ret = clk_prepare_enable(host->clk);
Guennadi Liakhovetskia6609262012-04-19 18:02:50 +02001475 if (ret < 0)
Ben Dooks46991002014-06-04 12:42:10 +01001476 goto err_pm;
Guennadi Liakhovetskib2891742012-04-19 18:02:05 +02001477
Kuninori Morimoto9bb09a32015-04-23 08:16:04 +00001478 sh_mmcif_clk_setup(host);
1479
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001480 ret = pm_runtime_resume(dev);
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001481 if (ret < 0)
Ben Dooks46991002014-06-04 12:42:10 +01001482 goto err_clk;
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +00001483
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +00001484 INIT_DELAYED_WORK(&host->timeout_work, sh_mmcif_timeout_work);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001485
Guennadi Liakhovetskib2891742012-04-19 18:02:05 +02001486 sh_mmcif_sync_reset(host);
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +00001487 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1488
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001489 name = irq[1] < 0 ? dev_name(dev) : "sh_mmc:error";
1490 ret = devm_request_threaded_irq(dev, irq[0], sh_mmcif_intr,
Ben Dooks6f4789e2014-06-04 12:42:11 +01001491 sh_mmcif_irqt, 0, name, host);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001492 if (ret) {
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001493 dev_err(dev, "request_irq error (%s)\n", name);
Ben Dooks11a80852014-06-04 12:42:12 +01001494 goto err_clk;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001495 }
Shinya Kuribayashi2cd5b3e2013-01-14 14:12:36 -05001496 if (irq[1] >= 0) {
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001497 ret = devm_request_threaded_irq(dev, irq[1],
Ben Dooks6f4789e2014-06-04 12:42:11 +01001498 sh_mmcif_intr, sh_mmcif_irqt,
1499 0, "sh_mmc:int", host);
Shinya Kuribayashi2cd5b3e2013-01-14 14:12:36 -05001500 if (ret) {
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001501 dev_err(dev, "request_irq error (sh_mmc:int)\n");
Ben Dooks11a80852014-06-04 12:42:12 +01001502 goto err_clk;
Shinya Kuribayashi2cd5b3e2013-01-14 14:12:36 -05001503 }
Yusuke Godafdc50a92010-05-26 14:41:59 -07001504 }
1505
Guennadi Liakhovetskie4806062012-06-14 14:24:35 +02001506 if (pd && pd->use_cd_gpio) {
Laurent Pinchart214fc302013-08-08 12:38:31 +02001507 ret = mmc_gpio_request_cd(mmc, pd->cd_gpio, 0);
Guennadi Liakhovetskie4806062012-06-14 14:24:35 +02001508 if (ret < 0)
Ben Dooks7f67f3a2014-06-04 12:42:13 +01001509 goto err_clk;
Guennadi Liakhovetskie4806062012-06-14 14:24:35 +02001510 }
1511
Guennadi Liakhovetski80473102012-12-12 15:38:14 +01001512 mutex_init(&host->thread_lock);
1513
Guennadi Liakhovetski5ba85d92012-01-21 00:41:28 +01001514 ret = mmc_add_host(mmc);
1515 if (ret < 0)
Ben Dooks7f67f3a2014-06-04 12:42:13 +01001516 goto err_clk;
Yusuke Godafdc50a92010-05-26 14:41:59 -07001517
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001518 dev_pm_qos_expose_latency_limit(dev, 100);
Rafael J. Wysockiefe6a8a2012-03-13 01:02:15 +01001519
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001520 dev_info(dev, "Chip version 0x%04x, clock rate %luMHz\n",
Ben Dooksce7eb682014-06-04 12:42:08 +01001521 sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0xffff,
Kuninori Morimoto6aed6782015-04-23 08:15:08 +00001522 clk_get_rate(host->clk) / 1000000UL);
Ben Dooksce7eb682014-06-04 12:42:08 +01001523
Kuninori Morimoto6aed6782015-04-23 08:15:08 +00001524 clk_disable_unprepare(host->clk);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001525 return ret;
1526
Ben Dooks46991002014-06-04 12:42:10 +01001527err_clk:
Kuninori Morimoto6aed6782015-04-23 08:15:08 +00001528 clk_disable_unprepare(host->clk);
Ben Dooks46991002014-06-04 12:42:10 +01001529err_pm:
Kuninori Morimoto60985c32015-04-23 08:14:12 +00001530 pm_runtime_disable(dev);
Ben Dooks46991002014-06-04 12:42:10 +01001531err_host:
Yusuke Godafdc50a92010-05-26 14:41:59 -07001532 mmc_free_host(mmc);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001533 return ret;
1534}
1535
Bill Pemberton6e0ee712012-11-19 13:26:03 -05001536static int sh_mmcif_remove(struct platform_device *pdev)
Yusuke Godafdc50a92010-05-26 14:41:59 -07001537{
1538 struct sh_mmcif_host *host = platform_get_drvdata(pdev);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001539
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001540 host->dying = true;
Kuninori Morimoto6aed6782015-04-23 08:15:08 +00001541 clk_prepare_enable(host->clk);
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001542 pm_runtime_get_sync(&pdev->dev);
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001543
Rafael J. Wysockiefe6a8a2012-03-13 01:02:15 +01001544 dev_pm_qos_hide_latency_limit(&pdev->dev);
1545
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001546 mmc_remove_host(host->mmc);
Guennadi Liakhovetski3b0beaf2011-04-15 18:30:47 +00001547 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
1548
Guennadi Liakhovetskif985da12011-12-25 21:07:52 +01001549 /*
1550 * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the
1551 * mmc_remove_host() call above. But swapping order doesn't help either
1552 * (a query on the linux-mmc mailing list didn't bring any replies).
1553 */
1554 cancel_delayed_work_sync(&host->timeout_work);
1555
Kuninori Morimoto6aed6782015-04-23 08:15:08 +00001556 clk_disable_unprepare(host->clk);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001557 mmc_free_host(host->mmc);
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001558 pm_runtime_put_sync(&pdev->dev);
1559 pm_runtime_disable(&pdev->dev);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001560
1561 return 0;
1562}
1563
Ulf Hansson51129f32013-10-01 14:01:46 +02001564#ifdef CONFIG_PM_SLEEP
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001565static int sh_mmcif_suspend(struct device *dev)
1566{
Guennadi Liakhovetskib2891742012-04-19 18:02:05 +02001567 struct sh_mmcif_host *host = dev_get_drvdata(dev);
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001568
Ulf Hanssoncb3ca1a2013-09-26 10:19:09 +02001569 sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001570
Ulf Hanssoncb3ca1a2013-09-26 10:19:09 +02001571 return 0;
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001572}
1573
1574static int sh_mmcif_resume(struct device *dev)
1575{
Ulf Hanssoncb3ca1a2013-09-26 10:19:09 +02001576 return 0;
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001577}
Ulf Hansson51129f32013-10-01 14:01:46 +02001578#endif
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001579
1580static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
Ulf Hansson51129f32013-10-01 14:01:46 +02001581 SET_SYSTEM_SLEEP_PM_OPS(sh_mmcif_suspend, sh_mmcif_resume)
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001582};
1583
Yusuke Godafdc50a92010-05-26 14:41:59 -07001584static struct platform_driver sh_mmcif_driver = {
1585 .probe = sh_mmcif_probe,
1586 .remove = sh_mmcif_remove,
1587 .driver = {
1588 .name = DRIVER_NAME,
Guennadi Liakhovetskifaca6642011-05-05 16:20:48 +00001589 .pm = &sh_mmcif_dev_pm_ops,
Kuninori Morimoto1b1a6942015-05-14 07:21:36 +00001590 .of_match_table = sh_mmcif_of_match,
Yusuke Godafdc50a92010-05-26 14:41:59 -07001591 },
1592};
1593
Axel Lind1f81a62011-11-26 12:55:43 +08001594module_platform_driver(sh_mmcif_driver);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001595
1596MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver");
1597MODULE_LICENSE("GPL");
Guennadi Liakhovetskiaa0787a2010-11-24 10:05:12 +00001598MODULE_ALIAS("platform:" DRIVER_NAME);
Yusuke Godafdc50a92010-05-26 14:41:59 -07001599MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>");