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