GNU Linux-libre 4.14.265-gnu1
[releases.git] / drivers / mmc / host / tmio_mmc_core.c
1 /*
2  * Driver for the MMC / SD / SDIO IP found in:
3  *
4  * TC6393XB, TC6391XB, TC6387XB, T7L66XB, ASIC3, SH-Mobile SoCs
5  *
6  * Copyright (C) 2015-17 Renesas Electronics Corporation
7  * Copyright (C) 2016-17 Sang Engineering, Wolfram Sang
8  * Copyright (C) 2017 Horms Solutions, Simon Horman
9  * Copyright (C) 2011 Guennadi Liakhovetski
10  * Copyright (C) 2007 Ian Molton
11  * Copyright (C) 2004 Ian Molton
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  *
17  * This driver draws mainly on scattered spec sheets, Reverse engineering
18  * of the toshiba e800  SD driver and some parts of the 2.4 ASIC3 driver (4 bit
19  * support). (Further 4 bit support from a later datasheet).
20  *
21  * TODO:
22  *   Investigate using a workqueue for PIO transfers
23  *   Eliminate FIXMEs
24  *   Better Power management
25  *   Handle MMC errors better
26  *   double buffer support
27  *
28  */
29
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/highmem.h>
33 #include <linux/interrupt.h>
34 #include <linux/io.h>
35 #include <linux/irq.h>
36 #include <linux/mfd/tmio.h>
37 #include <linux/mmc/card.h>
38 #include <linux/mmc/host.h>
39 #include <linux/mmc/mmc.h>
40 #include <linux/mmc/slot-gpio.h>
41 #include <linux/module.h>
42 #include <linux/pagemap.h>
43 #include <linux/platform_device.h>
44 #include <linux/pm_qos.h>
45 #include <linux/pm_runtime.h>
46 #include <linux/regulator/consumer.h>
47 #include <linux/mmc/sdio.h>
48 #include <linux/scatterlist.h>
49 #include <linux/sizes.h>
50 #include <linux/spinlock.h>
51 #include <linux/swiotlb.h>
52 #include <linux/workqueue.h>
53
54 #include "tmio_mmc.h"
55
56 static inline void tmio_mmc_start_dma(struct tmio_mmc_host *host,
57                                       struct mmc_data *data)
58 {
59         if (host->dma_ops)
60                 host->dma_ops->start(host, data);
61 }
62
63 static inline void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
64 {
65         if (host->dma_ops)
66                 host->dma_ops->enable(host, enable);
67 }
68
69 static inline void tmio_mmc_request_dma(struct tmio_mmc_host *host,
70                                         struct tmio_mmc_data *pdata)
71 {
72         if (host->dma_ops) {
73                 host->dma_ops->request(host, pdata);
74         } else {
75                 host->chan_tx = NULL;
76                 host->chan_rx = NULL;
77         }
78 }
79
80 static inline void tmio_mmc_release_dma(struct tmio_mmc_host *host)
81 {
82         if (host->dma_ops)
83                 host->dma_ops->release(host);
84 }
85
86 static inline void tmio_mmc_abort_dma(struct tmio_mmc_host *host)
87 {
88         if (host->dma_ops)
89                 host->dma_ops->abort(host);
90 }
91
92 static inline void tmio_mmc_dataend_dma(struct tmio_mmc_host *host)
93 {
94         if (host->dma_ops)
95                 host->dma_ops->dataend(host);
96 }
97
98 void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
99 {
100         host->sdcard_irq_mask &= ~(i & TMIO_MASK_IRQ);
101         sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
102 }
103 EXPORT_SYMBOL_GPL(tmio_mmc_enable_mmc_irqs);
104
105 void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
106 {
107         host->sdcard_irq_mask |= (i & TMIO_MASK_IRQ);
108         sd_ctrl_write32_as_16_and_16(host, CTL_IRQ_MASK, host->sdcard_irq_mask);
109 }
110 EXPORT_SYMBOL_GPL(tmio_mmc_disable_mmc_irqs);
111
112 static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i)
113 {
114         sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, ~i);
115 }
116
117 static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
118 {
119         host->sg_len = data->sg_len;
120         host->sg_ptr = data->sg;
121         host->sg_orig = data->sg;
122         host->sg_off = 0;
123 }
124
125 static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
126 {
127         host->sg_ptr = sg_next(host->sg_ptr);
128         host->sg_off = 0;
129         return --host->sg_len;
130 }
131
132 #define CMDREQ_TIMEOUT  5000
133
134 static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
135 {
136         struct tmio_mmc_host *host = mmc_priv(mmc);
137
138         if (enable && !host->sdio_irq_enabled) {
139                 u16 sdio_status;
140
141                 /* Keep device active while SDIO irq is enabled */
142                 pm_runtime_get_sync(mmc_dev(mmc));
143
144                 host->sdio_irq_enabled = true;
145                 host->sdio_irq_mask = TMIO_SDIO_MASK_ALL & ~TMIO_SDIO_STAT_IOIRQ;
146
147                 /* Clear obsolete interrupts before enabling */
148                 sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS) & ~TMIO_SDIO_MASK_ALL;
149                 if (host->pdata->flags & TMIO_MMC_SDIO_STATUS_SETBITS)
150                         sdio_status |= TMIO_SDIO_SETBITS_MASK;
151                 sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status);
152
153                 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
154         } else if (!enable && host->sdio_irq_enabled) {
155                 host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
156                 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
157
158                 host->sdio_irq_enabled = false;
159                 pm_runtime_mark_last_busy(mmc_dev(mmc));
160                 pm_runtime_put_autosuspend(mmc_dev(mmc));
161         }
162 }
163
164 static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
165 {
166         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, CLK_CTL_SCLKEN |
167                 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
168
169         /* HW engineers overrode docs: no sleep needed on R-Car2+ */
170         if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
171                 msleep(10);
172
173         if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG) {
174                 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
175                 msleep(10);
176         }
177 }
178
179 static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
180 {
181         if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG) {
182                 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
183                 msleep(10);
184         }
185
186         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
187                 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
188
189         /* HW engineers overrode docs: no sleep needed on R-Car2+ */
190         if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
191                 msleep(10);
192 }
193
194 static void tmio_mmc_set_clock(struct tmio_mmc_host *host,
195                                unsigned int new_clock)
196 {
197         u32 clk = 0, clock;
198
199         if (new_clock == 0) {
200                 tmio_mmc_clk_stop(host);
201                 return;
202         }
203
204         if (host->clk_update)
205                 clock = host->clk_update(host, new_clock) / 512;
206         else
207                 clock = host->mmc->f_min;
208
209         for (clk = 0x80000080; new_clock >= (clock << 1); clk >>= 1)
210                 clock <<= 1;
211
212         /* 1/1 clock is option */
213         if ((host->pdata->flags & TMIO_MMC_CLK_ACTUAL) && ((clk >> 22) & 0x1))
214                 clk |= 0xff;
215
216         if (host->set_clk_div)
217                 host->set_clk_div(host->pdev, (clk >> 22) & 1);
218
219         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN &
220                         sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
221         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & CLK_CTL_DIV_MASK);
222         if (!(host->pdata->flags & TMIO_MMC_MIN_RCAR2))
223                 msleep(10);
224
225         tmio_mmc_clk_start(host);
226 }
227
228 static void tmio_mmc_reset(struct tmio_mmc_host *host)
229 {
230         /* FIXME - should we set stop clock reg here */
231         sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
232         if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG)
233                 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
234         msleep(10);
235         sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
236         if (host->pdata->flags & TMIO_MMC_HAVE_HIGH_REG)
237                 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
238         msleep(10);
239
240         if (host->pdata->flags & TMIO_MMC_SDIO_IRQ) {
241                 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, host->sdio_irq_mask);
242                 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
243         }
244
245 }
246
247 static void tmio_mmc_reset_work(struct work_struct *work)
248 {
249         struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
250                                                   delayed_reset_work.work);
251         struct mmc_request *mrq;
252         unsigned long flags;
253
254         spin_lock_irqsave(&host->lock, flags);
255         mrq = host->mrq;
256
257         /*
258          * is request already finished? Since we use a non-blocking
259          * cancel_delayed_work(), it can happen, that a .set_ios() call preempts
260          * us, so, have to check for IS_ERR(host->mrq)
261          */
262         if (IS_ERR_OR_NULL(mrq) ||
263             time_is_after_jiffies(host->last_req_ts +
264                                   msecs_to_jiffies(CMDREQ_TIMEOUT))) {
265                 spin_unlock_irqrestore(&host->lock, flags);
266                 return;
267         }
268
269         dev_warn(&host->pdev->dev,
270                  "timeout waiting for hardware interrupt (CMD%u)\n",
271                  mrq->cmd->opcode);
272
273         if (host->data)
274                 host->data->error = -ETIMEDOUT;
275         else if (host->cmd)
276                 host->cmd->error = -ETIMEDOUT;
277         else
278                 mrq->cmd->error = -ETIMEDOUT;
279
280         host->cmd = NULL;
281         host->data = NULL;
282         host->force_pio = false;
283
284         spin_unlock_irqrestore(&host->lock, flags);
285
286         tmio_mmc_reset(host);
287
288         /* Ready for new calls */
289         host->mrq = NULL;
290
291         tmio_mmc_abort_dma(host);
292         mmc_request_done(host->mmc, mrq);
293 }
294
295 /* These are the bitmasks the tmio chip requires to implement the MMC response
296  * types. Note that R1 and R6 are the same in this scheme. */
297 #define APP_CMD        0x0040
298 #define RESP_NONE      0x0300
299 #define RESP_R1        0x0400
300 #define RESP_R1B       0x0500
301 #define RESP_R2        0x0600
302 #define RESP_R3        0x0700
303 #define DATA_PRESENT   0x0800
304 #define TRANSFER_READ  0x1000
305 #define TRANSFER_MULTI 0x2000
306 #define SECURITY_CMD   0x4000
307 #define NO_CMD12_ISSUE 0x4000 /* TMIO_MMC_HAVE_CMD12_CTRL */
308
309 static int tmio_mmc_start_command(struct tmio_mmc_host *host,
310                                   struct mmc_command *cmd)
311 {
312         struct mmc_data *data = host->data;
313         int c = cmd->opcode;
314         u32 irq_mask = TMIO_MASK_CMD;
315
316         switch (mmc_resp_type(cmd)) {
317         case MMC_RSP_NONE: c |= RESP_NONE; break;
318         case MMC_RSP_R1:
319         case MMC_RSP_R1_NO_CRC:
320                            c |= RESP_R1;   break;
321         case MMC_RSP_R1B:  c |= RESP_R1B;  break;
322         case MMC_RSP_R2:   c |= RESP_R2;   break;
323         case MMC_RSP_R3:   c |= RESP_R3;   break;
324         default:
325                 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
326                 return -EINVAL;
327         }
328
329         host->cmd = cmd;
330
331 /* FIXME - this seems to be ok commented out but the spec suggest this bit
332  *         should be set when issuing app commands.
333  *      if(cmd->flags & MMC_FLAG_ACMD)
334  *              c |= APP_CMD;
335  */
336         if (data) {
337                 c |= DATA_PRESENT;
338                 if (data->blocks > 1) {
339                         sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, TMIO_STOP_SEC);
340                         c |= TRANSFER_MULTI;
341
342                         /*
343                          * Disable auto CMD12 at IO_RW_EXTENDED and
344                          * SET_BLOCK_COUNT when doing multiple block transfer
345                          */
346                         if ((host->pdata->flags & TMIO_MMC_HAVE_CMD12_CTRL) &&
347                             (cmd->opcode == SD_IO_RW_EXTENDED || host->mrq->sbc))
348                                 c |= NO_CMD12_ISSUE;
349                 }
350                 if (data->flags & MMC_DATA_READ)
351                         c |= TRANSFER_READ;
352         }
353
354         if (!host->native_hotplug)
355                 irq_mask &= ~(TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
356         tmio_mmc_enable_mmc_irqs(host, irq_mask);
357
358         /* Fire off the command */
359         sd_ctrl_write32_as_16_and_16(host, CTL_ARG_REG, cmd->arg);
360         sd_ctrl_write16(host, CTL_SD_CMD, c);
361
362         return 0;
363 }
364
365 static void tmio_mmc_transfer_data(struct tmio_mmc_host *host,
366                                    unsigned short *buf,
367                                    unsigned int count)
368 {
369         int is_read = host->data->flags & MMC_DATA_READ;
370         u8  *buf8;
371
372         /*
373          * Transfer the data
374          */
375         if (host->pdata->flags & TMIO_MMC_32BIT_DATA_PORT) {
376                 u32 data = 0;
377                 u32 *buf32 = (u32 *)buf;
378
379                 if (is_read)
380                         sd_ctrl_read32_rep(host, CTL_SD_DATA_PORT, buf32,
381                                            count >> 2);
382                 else
383                         sd_ctrl_write32_rep(host, CTL_SD_DATA_PORT, buf32,
384                                             count >> 2);
385
386                 /* if count was multiple of 4 */
387                 if (!(count & 0x3))
388                         return;
389
390                 buf32 += count >> 2;
391                 count %= 4;
392
393                 if (is_read) {
394                         sd_ctrl_read32_rep(host, CTL_SD_DATA_PORT, &data, 1);
395                         memcpy(buf32, &data, count);
396                 } else {
397                         memcpy(&data, buf32, count);
398                         sd_ctrl_write32_rep(host, CTL_SD_DATA_PORT, &data, 1);
399                 }
400
401                 return;
402         }
403
404         if (is_read)
405                 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
406         else
407                 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
408
409         /* if count was even number */
410         if (!(count & 0x1))
411                 return;
412
413         /* if count was odd number */
414         buf8 = (u8 *)(buf + (count >> 1));
415
416         /*
417          * FIXME
418          *
419          * driver and this function are assuming that
420          * it is used as little endian
421          */
422         if (is_read)
423                 *buf8 = sd_ctrl_read16(host, CTL_SD_DATA_PORT) & 0xff;
424         else
425                 sd_ctrl_write16(host, CTL_SD_DATA_PORT, *buf8);
426 }
427
428 /*
429  * This chip always returns (at least?) as much data as you ask for.
430  * I'm unsure what happens if you ask for less than a block. This should be
431  * looked into to ensure that a funny length read doesn't hose the controller.
432  */
433 static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
434 {
435         struct mmc_data *data = host->data;
436         void *sg_virt;
437         unsigned short *buf;
438         unsigned int count;
439         unsigned long flags;
440
441         if ((host->chan_tx || host->chan_rx) && !host->force_pio) {
442                 pr_err("PIO IRQ in DMA mode!\n");
443                 return;
444         } else if (!data) {
445                 pr_debug("Spurious PIO IRQ\n");
446                 return;
447         }
448
449         sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
450         buf = (unsigned short *)(sg_virt + host->sg_off);
451
452         count = host->sg_ptr->length - host->sg_off;
453         if (count > data->blksz)
454                 count = data->blksz;
455
456         pr_debug("count: %08x offset: %08x flags %08x\n",
457                  count, host->sg_off, data->flags);
458
459         /* Transfer the data */
460         tmio_mmc_transfer_data(host, buf, count);
461
462         host->sg_off += count;
463
464         tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
465
466         if (host->sg_off == host->sg_ptr->length)
467                 tmio_mmc_next_sg(host);
468 }
469
470 static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
471 {
472         if (host->sg_ptr == &host->bounce_sg) {
473                 unsigned long flags;
474                 void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
475
476                 memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
477                 tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
478         }
479 }
480
481 /* needs to be called with host->lock held */
482 void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
483 {
484         struct mmc_data *data = host->data;
485         struct mmc_command *stop;
486
487         host->data = NULL;
488
489         if (!data) {
490                 dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
491                 return;
492         }
493         stop = data->stop;
494
495         /* FIXME - return correct transfer count on errors */
496         if (!data->error)
497                 data->bytes_xfered = data->blocks * data->blksz;
498         else
499                 data->bytes_xfered = 0;
500
501         pr_debug("Completed data request\n");
502
503         /*
504          * FIXME: other drivers allow an optional stop command of any given type
505          *        which we dont do, as the chip can auto generate them.
506          *        Perhaps we can be smarter about when to use auto CMD12 and
507          *        only issue the auto request when we know this is the desired
508          *        stop command, allowing fallback to the stop command the
509          *        upper layers expect. For now, we do what works.
510          */
511
512         if (data->flags & MMC_DATA_READ) {
513                 if (host->chan_rx && !host->force_pio)
514                         tmio_mmc_check_bounce_buffer(host);
515                 dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
516                         host->mrq);
517         } else {
518                 dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
519                         host->mrq);
520         }
521
522         if (stop && !host->mrq->sbc) {
523                 if (stop->opcode != MMC_STOP_TRANSMISSION || stop->arg)
524                         dev_err(&host->pdev->dev, "unsupported stop: CMD%u,0x%x. We did CMD12,0\n",
525                                 stop->opcode, stop->arg);
526
527                 /* fill in response from auto CMD12 */
528                 stop->resp[0] = sd_ctrl_read16_and_16_as_32(host, CTL_RESPONSE);
529
530                 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0);
531         }
532
533         schedule_work(&host->done);
534 }
535 EXPORT_SYMBOL_GPL(tmio_mmc_do_data_irq);
536
537 static void tmio_mmc_data_irq(struct tmio_mmc_host *host, unsigned int stat)
538 {
539         struct mmc_data *data;
540
541         spin_lock(&host->lock);
542         data = host->data;
543
544         if (!data)
545                 goto out;
546
547         if (stat & TMIO_STAT_CRCFAIL || stat & TMIO_STAT_STOPBIT_ERR ||
548             stat & TMIO_STAT_TXUNDERRUN)
549                 data->error = -EILSEQ;
550         if (host->chan_tx && (data->flags & MMC_DATA_WRITE) && !host->force_pio) {
551                 u32 status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
552                 bool done = false;
553
554                 /*
555                  * Has all data been written out yet? Testing on SuperH showed,
556                  * that in most cases the first interrupt comes already with the
557                  * BUSY status bit clear, but on some operations, like mount or
558                  * in the beginning of a write / sync / umount, there is one
559                  * DATAEND interrupt with the BUSY bit set, in this cases
560                  * waiting for one more interrupt fixes the problem.
561                  */
562                 if (host->pdata->flags & TMIO_MMC_HAS_IDLE_WAIT) {
563                         if (status & TMIO_STAT_SCLKDIVEN)
564                                 done = true;
565                 } else {
566                         if (!(status & TMIO_STAT_CMD_BUSY))
567                                 done = true;
568                 }
569
570                 if (done) {
571                         tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
572                         tmio_mmc_dataend_dma(host);
573                 }
574         } else if (host->chan_rx && (data->flags & MMC_DATA_READ) && !host->force_pio) {
575                 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
576                 tmio_mmc_dataend_dma(host);
577         } else {
578                 tmio_mmc_do_data_irq(host);
579                 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
580         }
581 out:
582         spin_unlock(&host->lock);
583 }
584
585 static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host, unsigned int stat)
586 {
587         struct mmc_command *cmd = host->cmd;
588         int i, addr;
589
590         spin_lock(&host->lock);
591
592         if (!host->cmd) {
593                 pr_debug("Spurious CMD irq\n");
594                 goto out;
595         }
596
597         /* This controller is sicker than the PXA one. Not only do we need to
598          * drop the top 8 bits of the first response word, we also need to
599          * modify the order of the response for short response command types.
600          */
601
602         for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
603                 cmd->resp[i] = sd_ctrl_read16_and_16_as_32(host, addr);
604
605         if (cmd->flags &  MMC_RSP_136) {
606                 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
607                 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
608                 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
609                 cmd->resp[3] <<= 8;
610         } else if (cmd->flags & MMC_RSP_R3) {
611                 cmd->resp[0] = cmd->resp[3];
612         }
613
614         if (stat & TMIO_STAT_CMDTIMEOUT)
615                 cmd->error = -ETIMEDOUT;
616         else if ((stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC) ||
617                  stat & TMIO_STAT_STOPBIT_ERR ||
618                  stat & TMIO_STAT_CMD_IDX_ERR)
619                 cmd->error = -EILSEQ;
620
621         /* If there is data to handle we enable data IRQs here, and
622          * we will ultimatley finish the request in the data_end handler.
623          * If theres no data or we encountered an error, finish now.
624          */
625         if (host->data && (!cmd->error || cmd->error == -EILSEQ)) {
626                 if (host->data->flags & MMC_DATA_READ) {
627                         if (host->force_pio || !host->chan_rx)
628                                 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
629                         else
630                                 tasklet_schedule(&host->dma_issue);
631                 } else {
632                         if (host->force_pio || !host->chan_tx)
633                                 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
634                         else
635                                 tasklet_schedule(&host->dma_issue);
636                 }
637         } else {
638                 schedule_work(&host->done);
639         }
640
641 out:
642         spin_unlock(&host->lock);
643 }
644
645 static bool __tmio_mmc_card_detect_irq(struct tmio_mmc_host *host,
646                                        int ireg, int status)
647 {
648         struct mmc_host *mmc = host->mmc;
649
650         /* Card insert / remove attempts */
651         if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
652                 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
653                         TMIO_STAT_CARD_REMOVE);
654                 if ((((ireg & TMIO_STAT_CARD_REMOVE) && mmc->card) ||
655                      ((ireg & TMIO_STAT_CARD_INSERT) && !mmc->card)) &&
656                     !work_pending(&mmc->detect.work))
657                         mmc_detect_change(host->mmc, msecs_to_jiffies(100));
658                 return true;
659         }
660
661         return false;
662 }
663
664 static bool __tmio_mmc_sdcard_irq(struct tmio_mmc_host *host, int ireg,
665                                   int status)
666 {
667         /* Command completion */
668         if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
669                 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CMDRESPEND |
670                                       TMIO_STAT_CMDTIMEOUT);
671                 tmio_mmc_cmd_irq(host, status);
672                 return true;
673         }
674
675         /* Data transfer */
676         if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
677                 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
678                 tmio_mmc_pio_irq(host);
679                 return true;
680         }
681
682         /* Data transfer completion */
683         if (ireg & TMIO_STAT_DATAEND) {
684                 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
685                 tmio_mmc_data_irq(host, status);
686                 return true;
687         }
688
689         return false;
690 }
691
692 static bool __tmio_mmc_sdio_irq(struct tmio_mmc_host *host)
693 {
694         struct mmc_host *mmc = host->mmc;
695         struct tmio_mmc_data *pdata = host->pdata;
696         unsigned int ireg, status;
697         unsigned int sdio_status;
698
699         if (!(pdata->flags & TMIO_MMC_SDIO_IRQ))
700                 return false;
701
702         status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
703         ireg = status & TMIO_SDIO_MASK_ALL & ~host->sdio_irq_mask;
704
705         sdio_status = status & ~TMIO_SDIO_MASK_ALL;
706         if (pdata->flags & TMIO_MMC_SDIO_STATUS_SETBITS)
707                 sdio_status |= TMIO_SDIO_SETBITS_MASK;
708
709         sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status);
710
711         if (mmc->caps & MMC_CAP_SDIO_IRQ && ireg & TMIO_SDIO_STAT_IOIRQ)
712                 mmc_signal_sdio_irq(mmc);
713
714         return ireg;
715 }
716
717 irqreturn_t tmio_mmc_irq(int irq, void *devid)
718 {
719         struct tmio_mmc_host *host = devid;
720         unsigned int ireg, status;
721
722         status = sd_ctrl_read16_and_16_as_32(host, CTL_STATUS);
723         ireg = status & TMIO_MASK_IRQ & ~host->sdcard_irq_mask;
724
725         /* Clear the status except the interrupt status */
726         sd_ctrl_write32_as_16_and_16(host, CTL_STATUS, TMIO_MASK_IRQ);
727
728         if (__tmio_mmc_card_detect_irq(host, ireg, status))
729                 return IRQ_HANDLED;
730         if (__tmio_mmc_sdcard_irq(host, ireg, status))
731                 return IRQ_HANDLED;
732
733         if (__tmio_mmc_sdio_irq(host))
734                 return IRQ_HANDLED;
735
736         return IRQ_NONE;
737 }
738 EXPORT_SYMBOL_GPL(tmio_mmc_irq);
739
740 static int tmio_mmc_start_data(struct tmio_mmc_host *host,
741                                struct mmc_data *data)
742 {
743         struct tmio_mmc_data *pdata = host->pdata;
744
745         pr_debug("setup data transfer: blocksize %08x  nr_blocks %d\n",
746                  data->blksz, data->blocks);
747
748         /* Some hardware cannot perform 2 byte requests in 4/8 bit mode */
749         if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4 ||
750             host->mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
751                 int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
752
753                 if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
754                         pr_err("%s: %d byte block unsupported in 4/8 bit mode\n",
755                                mmc_hostname(host->mmc), data->blksz);
756                         return -EINVAL;
757                 }
758         }
759
760         tmio_mmc_init_sg(host, data);
761         host->data = data;
762
763         /* Set transfer length / blocksize */
764         sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
765         if (host->mmc->max_blk_count >= SZ_64K)
766                 sd_ctrl_write32(host, CTL_XFER_BLK_COUNT, data->blocks);
767         else
768                 sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
769
770         tmio_mmc_start_dma(host, data);
771
772         return 0;
773 }
774
775 static void tmio_mmc_hw_reset(struct mmc_host *mmc)
776 {
777         struct tmio_mmc_host *host = mmc_priv(mmc);
778
779         if (host->hw_reset)
780                 host->hw_reset(host);
781 }
782
783 static int tmio_mmc_execute_tuning(struct mmc_host *mmc, u32 opcode)
784 {
785         struct tmio_mmc_host *host = mmc_priv(mmc);
786         int i, ret = 0;
787
788         if (!host->init_tuning || !host->select_tuning)
789                 /* Tuning is not supported */
790                 goto out;
791
792         host->tap_num = host->init_tuning(host);
793         if (!host->tap_num)
794                 /* Tuning is not supported */
795                 goto out;
796
797         if (host->tap_num * 2 >= sizeof(host->taps) * BITS_PER_BYTE) {
798                 dev_warn_once(&host->pdev->dev,
799                         "Too many taps, skipping tuning. Please consider updating size of taps field of tmio_mmc_host\n");
800                 goto out;
801         }
802
803         bitmap_zero(host->taps, host->tap_num * 2);
804
805         /* Issue CMD19 twice for each tap */
806         for (i = 0; i < 2 * host->tap_num; i++) {
807                 if (host->prepare_tuning)
808                         host->prepare_tuning(host, i % host->tap_num);
809
810                 ret = mmc_send_tuning(mmc, opcode, NULL);
811                 if (ret && ret != -EILSEQ)
812                         goto out;
813                 if (ret == 0)
814                         set_bit(i, host->taps);
815
816                 mdelay(1);
817         }
818
819         ret = host->select_tuning(host);
820
821 out:
822         if (ret < 0) {
823                 dev_warn(&host->pdev->dev, "Tuning procedure failed\n");
824                 tmio_mmc_hw_reset(mmc);
825         }
826
827         return ret;
828 }
829
830 static void tmio_process_mrq(struct tmio_mmc_host *host,
831                              struct mmc_request *mrq)
832 {
833         struct mmc_command *cmd;
834         int ret;
835
836         if (mrq->sbc && host->cmd != mrq->sbc) {
837                 cmd = mrq->sbc;
838         } else {
839                 cmd = mrq->cmd;
840                 if (mrq->data) {
841                         ret = tmio_mmc_start_data(host, mrq->data);
842                         if (ret)
843                                 goto fail;
844                 }
845         }
846
847         ret = tmio_mmc_start_command(host, cmd);
848         if (ret)
849                 goto fail;
850
851         schedule_delayed_work(&host->delayed_reset_work,
852                               msecs_to_jiffies(CMDREQ_TIMEOUT));
853         return;
854
855 fail:
856         host->force_pio = false;
857         host->mrq = NULL;
858         mrq->cmd->error = ret;
859         mmc_request_done(host->mmc, mrq);
860 }
861
862 /* Process requests from the MMC layer */
863 static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
864 {
865         struct tmio_mmc_host *host = mmc_priv(mmc);
866         unsigned long flags;
867
868         spin_lock_irqsave(&host->lock, flags);
869
870         if (host->mrq) {
871                 pr_debug("request not null\n");
872                 if (IS_ERR(host->mrq)) {
873                         spin_unlock_irqrestore(&host->lock, flags);
874                         mrq->cmd->error = -EAGAIN;
875                         mmc_request_done(mmc, mrq);
876                         return;
877                 }
878         }
879
880         host->last_req_ts = jiffies;
881         wmb();
882         host->mrq = mrq;
883
884         spin_unlock_irqrestore(&host->lock, flags);
885
886         tmio_process_mrq(host, mrq);
887 }
888
889 static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
890 {
891         struct mmc_request *mrq;
892         unsigned long flags;
893
894         spin_lock_irqsave(&host->lock, flags);
895
896         mrq = host->mrq;
897         if (IS_ERR_OR_NULL(mrq)) {
898                 spin_unlock_irqrestore(&host->lock, flags);
899                 return;
900         }
901
902         /* If not SET_BLOCK_COUNT, clear old data */
903         if (host->cmd != mrq->sbc) {
904                 host->cmd = NULL;
905                 host->data = NULL;
906                 host->force_pio = false;
907                 host->mrq = NULL;
908         }
909
910         cancel_delayed_work(&host->delayed_reset_work);
911
912         spin_unlock_irqrestore(&host->lock, flags);
913
914         if (mrq->cmd->error || (mrq->data && mrq->data->error))
915                 tmio_mmc_abort_dma(host);
916
917         /* SCC error means retune, but executed command was still successful */
918         if (host->check_scc_error && host->check_scc_error(host))
919                 mmc_retune_needed(host->mmc);
920
921         /* If SET_BLOCK_COUNT, continue with main command */
922         if (host->mrq && !mrq->cmd->error) {
923                 tmio_process_mrq(host, mrq);
924                 return;
925         }
926
927         mmc_request_done(host->mmc, mrq);
928 }
929
930 static void tmio_mmc_done_work(struct work_struct *work)
931 {
932         struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
933                                                   done);
934         tmio_mmc_finish_request(host);
935 }
936
937 static int tmio_mmc_clk_enable(struct tmio_mmc_host *host)
938 {
939         if (!host->clk_enable)
940                 return -ENOTSUPP;
941
942         return host->clk_enable(host);
943 }
944
945 static void tmio_mmc_clk_disable(struct tmio_mmc_host *host)
946 {
947         if (host->clk_disable)
948                 host->clk_disable(host);
949 }
950
951 static void tmio_mmc_power_on(struct tmio_mmc_host *host, unsigned short vdd)
952 {
953         struct mmc_host *mmc = host->mmc;
954         int ret = 0;
955
956         /* .set_ios() is returning void, so, no chance to report an error */
957
958         if (host->set_pwr)
959                 host->set_pwr(host->pdev, 1);
960
961         if (!IS_ERR(mmc->supply.vmmc)) {
962                 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
963                 /*
964                  * Attention: empiric value. With a b43 WiFi SDIO card this
965                  * delay proved necessary for reliable card-insertion probing.
966                  * 100us were not enough. Is this the same 140us delay, as in
967                  * tmio_mmc_set_ios()?
968                  */
969                 udelay(200);
970         }
971         /*
972          * It seems, VccQ should be switched on after Vcc, this is also what the
973          * omap_hsmmc.c driver does.
974          */
975         if (!IS_ERR(mmc->supply.vqmmc) && !ret) {
976                 ret = regulator_enable(mmc->supply.vqmmc);
977                 udelay(200);
978         }
979
980         if (ret < 0)
981                 dev_dbg(&host->pdev->dev, "Regulators failed to power up: %d\n",
982                         ret);
983 }
984
985 static void tmio_mmc_power_off(struct tmio_mmc_host *host)
986 {
987         struct mmc_host *mmc = host->mmc;
988
989         if (!IS_ERR(mmc->supply.vqmmc))
990                 regulator_disable(mmc->supply.vqmmc);
991
992         if (!IS_ERR(mmc->supply.vmmc))
993                 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
994
995         if (host->set_pwr)
996                 host->set_pwr(host->pdev, 0);
997 }
998
999 static void tmio_mmc_set_bus_width(struct tmio_mmc_host *host,
1000                                    unsigned char bus_width)
1001 {
1002         u16 reg = sd_ctrl_read16(host, CTL_SD_MEM_CARD_OPT)
1003                                 & ~(CARD_OPT_WIDTH | CARD_OPT_WIDTH8);
1004
1005         /* reg now applies to MMC_BUS_WIDTH_4 */
1006         if (bus_width == MMC_BUS_WIDTH_1)
1007                 reg |= CARD_OPT_WIDTH;
1008         else if (bus_width == MMC_BUS_WIDTH_8)
1009                 reg |= CARD_OPT_WIDTH8;
1010
1011         sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, reg);
1012 }
1013
1014 /* Set MMC clock / power.
1015  * Note: This controller uses a simple divider scheme therefore it cannot
1016  * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
1017  * MMC wont run that fast, it has to be clocked at 12MHz which is the next
1018  * slowest setting.
1019  */
1020 static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1021 {
1022         struct tmio_mmc_host *host = mmc_priv(mmc);
1023         struct device *dev = &host->pdev->dev;
1024         unsigned long flags;
1025
1026         mutex_lock(&host->ios_lock);
1027
1028         spin_lock_irqsave(&host->lock, flags);
1029         if (host->mrq) {
1030                 if (IS_ERR(host->mrq)) {
1031                         dev_dbg(dev,
1032                                 "%s.%d: concurrent .set_ios(), clk %u, mode %u\n",
1033                                 current->comm, task_pid_nr(current),
1034                                 ios->clock, ios->power_mode);
1035                         host->mrq = ERR_PTR(-EINTR);
1036                 } else {
1037                         dev_dbg(dev,
1038                                 "%s.%d: CMD%u active since %lu, now %lu!\n",
1039                                 current->comm, task_pid_nr(current),
1040                                 host->mrq->cmd->opcode, host->last_req_ts,
1041                                 jiffies);
1042                 }
1043                 spin_unlock_irqrestore(&host->lock, flags);
1044
1045                 mutex_unlock(&host->ios_lock);
1046                 return;
1047         }
1048
1049         host->mrq = ERR_PTR(-EBUSY);
1050
1051         spin_unlock_irqrestore(&host->lock, flags);
1052
1053         switch (ios->power_mode) {
1054         case MMC_POWER_OFF:
1055                 tmio_mmc_power_off(host);
1056                 tmio_mmc_clk_stop(host);
1057                 break;
1058         case MMC_POWER_UP:
1059                 tmio_mmc_power_on(host, ios->vdd);
1060                 tmio_mmc_set_clock(host, ios->clock);
1061                 tmio_mmc_set_bus_width(host, ios->bus_width);
1062                 break;
1063         case MMC_POWER_ON:
1064                 tmio_mmc_set_clock(host, ios->clock);
1065                 tmio_mmc_set_bus_width(host, ios->bus_width);
1066                 break;
1067         }
1068
1069         /* Let things settle. delay taken from winCE driver */
1070         udelay(140);
1071         if (PTR_ERR(host->mrq) == -EINTR)
1072                 dev_dbg(&host->pdev->dev,
1073                         "%s.%d: IOS interrupted: clk %u, mode %u",
1074                         current->comm, task_pid_nr(current),
1075                         ios->clock, ios->power_mode);
1076         host->mrq = NULL;
1077
1078         host->clk_cache = ios->clock;
1079
1080         mutex_unlock(&host->ios_lock);
1081 }
1082
1083 static int tmio_mmc_get_ro(struct mmc_host *mmc)
1084 {
1085         struct tmio_mmc_host *host = mmc_priv(mmc);
1086         struct tmio_mmc_data *pdata = host->pdata;
1087         int ret = mmc_gpio_get_ro(mmc);
1088
1089         if (ret >= 0)
1090                 return ret;
1091
1092         ret = !((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
1093                 (sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT));
1094
1095         return ret;
1096 }
1097
1098 static int tmio_multi_io_quirk(struct mmc_card *card,
1099                                unsigned int direction, int blk_size)
1100 {
1101         struct tmio_mmc_host *host = mmc_priv(card->host);
1102
1103         if (host->multi_io_quirk)
1104                 return host->multi_io_quirk(card, direction, blk_size);
1105
1106         return blk_size;
1107 }
1108
1109 static struct mmc_host_ops tmio_mmc_ops = {
1110         .request        = tmio_mmc_request,
1111         .set_ios        = tmio_mmc_set_ios,
1112         .get_ro         = tmio_mmc_get_ro,
1113         .get_cd         = mmc_gpio_get_cd,
1114         .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
1115         .multi_io_quirk = tmio_multi_io_quirk,
1116         .hw_reset       = tmio_mmc_hw_reset,
1117         .execute_tuning = tmio_mmc_execute_tuning,
1118 };
1119
1120 static int tmio_mmc_init_ocr(struct tmio_mmc_host *host)
1121 {
1122         struct tmio_mmc_data *pdata = host->pdata;
1123         struct mmc_host *mmc = host->mmc;
1124         int err;
1125
1126         err = mmc_regulator_get_supply(mmc);
1127         if (err)
1128                 return err;
1129
1130         /* use ocr_mask if no regulator */
1131         if (!mmc->ocr_avail)
1132                 mmc->ocr_avail =  pdata->ocr_mask;
1133
1134         /*
1135          * try again.
1136          * There is possibility that regulator has not been probed
1137          */
1138         if (!mmc->ocr_avail)
1139                 return -EPROBE_DEFER;
1140
1141         return 0;
1142 }
1143
1144 static void tmio_mmc_of_parse(struct platform_device *pdev,
1145                               struct tmio_mmc_data *pdata)
1146 {
1147         const struct device_node *np = pdev->dev.of_node;
1148
1149         if (!np)
1150                 return;
1151
1152         if (of_get_property(np, "toshiba,mmc-wrprotect-disable", NULL))
1153                 pdata->flags |= TMIO_MMC_WRPROTECT_DISABLE;
1154 }
1155
1156 struct tmio_mmc_host*
1157 tmio_mmc_host_alloc(struct platform_device *pdev)
1158 {
1159         struct tmio_mmc_host *host;
1160         struct mmc_host *mmc;
1161
1162         mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
1163         if (!mmc)
1164                 return NULL;
1165
1166         host = mmc_priv(mmc);
1167         host->mmc = mmc;
1168         host->pdev = pdev;
1169
1170         return host;
1171 }
1172 EXPORT_SYMBOL_GPL(tmio_mmc_host_alloc);
1173
1174 void tmio_mmc_host_free(struct tmio_mmc_host *host)
1175 {
1176         mmc_free_host(host->mmc);
1177 }
1178 EXPORT_SYMBOL_GPL(tmio_mmc_host_free);
1179
1180 int tmio_mmc_host_probe(struct tmio_mmc_host *_host,
1181                         struct tmio_mmc_data *pdata,
1182                         const struct tmio_mmc_dma_ops *dma_ops)
1183 {
1184         struct platform_device *pdev = _host->pdev;
1185         struct mmc_host *mmc = _host->mmc;
1186         struct resource *res_ctl;
1187         int ret;
1188         u32 irq_mask = TMIO_MASK_CMD;
1189
1190         tmio_mmc_of_parse(pdev, pdata);
1191
1192         if (!(pdata->flags & TMIO_MMC_HAS_IDLE_WAIT))
1193                 _host->write16_hook = NULL;
1194
1195         res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1196         if (!res_ctl)
1197                 return -EINVAL;
1198
1199         ret = mmc_of_parse(mmc);
1200         if (ret < 0)
1201                 return ret;
1202
1203         _host->pdata = pdata;
1204         platform_set_drvdata(pdev, mmc);
1205
1206         _host->set_pwr = pdata->set_pwr;
1207         _host->set_clk_div = pdata->set_clk_div;
1208
1209         ret = tmio_mmc_init_ocr(_host);
1210         if (ret < 0)
1211                 return ret;
1212
1213         _host->ctl = devm_ioremap(&pdev->dev,
1214                                   res_ctl->start, resource_size(res_ctl));
1215         if (!_host->ctl)
1216                 return -ENOMEM;
1217
1218         tmio_mmc_ops.card_busy = _host->card_busy;
1219         tmio_mmc_ops.start_signal_voltage_switch =
1220                 _host->start_signal_voltage_switch;
1221         mmc->ops = &tmio_mmc_ops;
1222
1223         mmc->caps |= MMC_CAP_ERASE | MMC_CAP_4_BIT_DATA | pdata->capabilities;
1224         mmc->caps2 |= pdata->capabilities2;
1225         mmc->max_segs = pdata->max_segs ? : 32;
1226         mmc->max_blk_size = 512;
1227         mmc->max_blk_count = pdata->max_blk_count ? :
1228                 (PAGE_SIZE / mmc->max_blk_size) * mmc->max_segs;
1229         mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1230         /*
1231          * Since swiotlb has memory size limitation, this will calculate
1232          * the maximum size locally (because we don't have any APIs for it now)
1233          * and check the current max_req_size. And then, this will update
1234          * the max_req_size if needed as a workaround.
1235          */
1236         if (swiotlb_max_segment()) {
1237                 unsigned int max_size = (1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE;
1238
1239                 if (mmc->max_req_size > max_size)
1240                         mmc->max_req_size = max_size;
1241         }
1242         mmc->max_seg_size = mmc->max_req_size;
1243
1244         _host->native_hotplug = !(pdata->flags & TMIO_MMC_USE_GPIO_CD ||
1245                                   mmc->caps & MMC_CAP_NEEDS_POLL ||
1246                                   !mmc_card_is_removable(mmc));
1247
1248         /*
1249          * On Gen2+, eMMC with NONREMOVABLE currently fails because native
1250          * hotplug gets disabled. It seems RuntimePM related yet we need further
1251          * research. Since we are planning a PM overhaul anyway, let's enforce
1252          * for now the device being active by enabling native hotplug always.
1253          */
1254         if (pdata->flags & TMIO_MMC_MIN_RCAR2)
1255                 _host->native_hotplug = true;
1256
1257         if (tmio_mmc_clk_enable(_host) < 0) {
1258                 mmc->f_max = pdata->hclk;
1259                 mmc->f_min = mmc->f_max / 512;
1260         }
1261
1262         /*
1263          * Check the sanity of mmc->f_min to prevent tmio_mmc_set_clock() from
1264          * looping forever...
1265          */
1266         if (mmc->f_min == 0)
1267                 return -EINVAL;
1268
1269         /*
1270          * While using internal tmio hardware logic for card detection, we need
1271          * to ensure it stays powered for it to work.
1272          */
1273         if (_host->native_hotplug)
1274                 pm_runtime_get_noresume(&pdev->dev);
1275
1276         _host->sdio_irq_enabled = false;
1277         if (pdata->flags & TMIO_MMC_SDIO_IRQ)
1278                 _host->sdio_irq_mask = TMIO_SDIO_MASK_ALL;
1279
1280         tmio_mmc_clk_stop(_host);
1281         tmio_mmc_reset(_host);
1282
1283         _host->sdcard_irq_mask = sd_ctrl_read16_and_16_as_32(_host, CTL_IRQ_MASK);
1284         tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
1285
1286         /* Unmask the IRQs we want to know about */
1287         if (!_host->chan_rx)
1288                 irq_mask |= TMIO_MASK_READOP;
1289         if (!_host->chan_tx)
1290                 irq_mask |= TMIO_MASK_WRITEOP;
1291         if (!_host->native_hotplug)
1292                 irq_mask &= ~(TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT);
1293
1294         _host->sdcard_irq_mask &= ~irq_mask;
1295
1296         spin_lock_init(&_host->lock);
1297         mutex_init(&_host->ios_lock);
1298
1299         /* Init delayed work for request timeouts */
1300         INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
1301         INIT_WORK(&_host->done, tmio_mmc_done_work);
1302
1303         /* See if we also get DMA */
1304         _host->dma_ops = dma_ops;
1305         tmio_mmc_request_dma(_host, pdata);
1306
1307         pm_runtime_set_active(&pdev->dev);
1308         pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1309         pm_runtime_use_autosuspend(&pdev->dev);
1310         pm_runtime_enable(&pdev->dev);
1311
1312         ret = mmc_add_host(mmc);
1313         if (ret < 0) {
1314                 tmio_mmc_host_remove(_host);
1315                 return ret;
1316         }
1317
1318         dev_pm_qos_expose_latency_limit(&pdev->dev, 100);
1319
1320         if (pdata->flags & TMIO_MMC_USE_GPIO_CD) {
1321                 ret = mmc_gpio_request_cd(mmc, pdata->cd_gpio, 0);
1322                 if (ret < 0) {
1323                         tmio_mmc_host_remove(_host);
1324                         return ret;
1325                 }
1326                 mmc_gpiod_request_cd_irq(mmc);
1327         }
1328
1329         return 0;
1330 }
1331 EXPORT_SYMBOL_GPL(tmio_mmc_host_probe);
1332
1333 void tmio_mmc_host_remove(struct tmio_mmc_host *host)
1334 {
1335         struct platform_device *pdev = host->pdev;
1336         struct mmc_host *mmc = host->mmc;
1337
1338         if (host->pdata->flags & TMIO_MMC_SDIO_IRQ)
1339                 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
1340
1341         if (!host->native_hotplug)
1342                 pm_runtime_get_sync(&pdev->dev);
1343
1344         dev_pm_qos_hide_latency_limit(&pdev->dev);
1345
1346         mmc_remove_host(mmc);
1347         cancel_work_sync(&host->done);
1348         cancel_delayed_work_sync(&host->delayed_reset_work);
1349         tmio_mmc_release_dma(host);
1350
1351         pm_runtime_put_sync(&pdev->dev);
1352         pm_runtime_disable(&pdev->dev);
1353
1354         tmio_mmc_clk_disable(host);
1355 }
1356 EXPORT_SYMBOL_GPL(tmio_mmc_host_remove);
1357
1358 #ifdef CONFIG_PM
1359 int tmio_mmc_host_runtime_suspend(struct device *dev)
1360 {
1361         struct mmc_host *mmc = dev_get_drvdata(dev);
1362         struct tmio_mmc_host *host = mmc_priv(mmc);
1363
1364         tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
1365
1366         if (host->clk_cache)
1367                 tmio_mmc_clk_stop(host);
1368
1369         tmio_mmc_clk_disable(host);
1370
1371         return 0;
1372 }
1373 EXPORT_SYMBOL_GPL(tmio_mmc_host_runtime_suspend);
1374
1375 static bool tmio_mmc_can_retune(struct tmio_mmc_host *host)
1376 {
1377         return host->tap_num && mmc_can_retune(host->mmc);
1378 }
1379
1380 int tmio_mmc_host_runtime_resume(struct device *dev)
1381 {
1382         struct mmc_host *mmc = dev_get_drvdata(dev);
1383         struct tmio_mmc_host *host = mmc_priv(mmc);
1384
1385         tmio_mmc_reset(host);
1386         tmio_mmc_clk_enable(host);
1387
1388         if (host->clk_cache)
1389                 tmio_mmc_set_clock(host, host->clk_cache);
1390
1391         tmio_mmc_enable_dma(host, true);
1392
1393         if (tmio_mmc_can_retune(host) && host->select_tuning(host))
1394                 dev_warn(&host->pdev->dev, "Tuning selection failed\n");
1395
1396         return 0;
1397 }
1398 EXPORT_SYMBOL_GPL(tmio_mmc_host_runtime_resume);
1399 #endif
1400
1401 MODULE_LICENSE("GPL v2");