mmc: alcor: don't write data before command has completed
[sfrench/cifs-2.6.git] / drivers / mmc / host / alcor.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018 Oleksij Rempel <linux@rempel-privat.de>
4  *
5  * Driver for Alcor Micro AU6601 and AU6621 controllers
6  */
7
8 /* Note: this driver was created without any documentation. Based
9  * on sniffing, testing and in some cases mimic of original driver.
10  * As soon as some one with documentation or more experience in SD/MMC, or
11  * reverse engineering then me, please review this driver and question every
12  * thing what I did. 2018 Oleksij Rempel <linux@rempel-privat.de>
13  */
14
15 #include <linux/delay.h>
16 #include <linux/pci.h>
17 #include <linux/module.h>
18 #include <linux/io.h>
19 #include <linux/pm.h>
20 #include <linux/irq.h>
21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h>
23
24 #include <linux/mmc/host.h>
25 #include <linux/mmc/mmc.h>
26
27 #include <linux/alcor_pci.h>
28
29 enum alcor_cookie {
30         COOKIE_UNMAPPED,
31         COOKIE_PRE_MAPPED,
32         COOKIE_MAPPED,
33 };
34
35 struct alcor_pll_conf {
36         unsigned int clk_src_freq;
37         unsigned int clk_src_reg;
38         unsigned int min_div;
39         unsigned int max_div;
40 };
41
42 struct alcor_sdmmc_host {
43         struct  device *dev;
44         struct alcor_pci_priv *alcor_pci;
45
46         struct mmc_host *mmc;
47         struct mmc_request *mrq;
48         struct mmc_command *cmd;
49         struct mmc_data *data;
50         unsigned int dma_on:1;
51
52         struct mutex cmd_mutex;
53
54         struct delayed_work timeout_work;
55
56         struct sg_mapping_iter sg_miter;        /* SG state for PIO */
57         struct scatterlist *sg;
58         unsigned int blocks;            /* remaining PIO blocks */
59         int sg_count;
60
61         u32                     irq_status_sd;
62         unsigned char           cur_power_mode;
63 };
64
65 static const struct alcor_pll_conf alcor_pll_cfg[] = {
66         /* MHZ,         CLK src,                max div, min div */
67         { 31250000,     AU6601_CLK_31_25_MHZ,   1,      511},
68         { 48000000,     AU6601_CLK_48_MHZ,      1,      511},
69         {125000000,     AU6601_CLK_125_MHZ,     1,      511},
70         {384000000,     AU6601_CLK_384_MHZ,     1,      511},
71 };
72
73 static inline void alcor_rmw8(struct alcor_sdmmc_host *host, unsigned int addr,
74                                u8 clear, u8 set)
75 {
76         struct alcor_pci_priv *priv = host->alcor_pci;
77         u32 var;
78
79         var = alcor_read8(priv, addr);
80         var &= ~clear;
81         var |= set;
82         alcor_write8(priv, var, addr);
83 }
84
85 /* As soon as irqs are masked, some status updates may be missed.
86  * Use this with care.
87  */
88 static inline void alcor_mask_sd_irqs(struct alcor_sdmmc_host *host)
89 {
90         struct alcor_pci_priv *priv = host->alcor_pci;
91
92         alcor_write32(priv, 0, AU6601_REG_INT_ENABLE);
93 }
94
95 static inline void alcor_unmask_sd_irqs(struct alcor_sdmmc_host *host)
96 {
97         struct alcor_pci_priv *priv = host->alcor_pci;
98
99         alcor_write32(priv, AU6601_INT_CMD_MASK | AU6601_INT_DATA_MASK |
100                   AU6601_INT_CARD_INSERT | AU6601_INT_CARD_REMOVE |
101                   AU6601_INT_OVER_CURRENT_ERR,
102                   AU6601_REG_INT_ENABLE);
103 }
104
105 static void alcor_reset(struct alcor_sdmmc_host *host, u8 val)
106 {
107         struct alcor_pci_priv *priv = host->alcor_pci;
108         int i;
109
110         alcor_write8(priv, val | AU6601_BUF_CTRL_RESET,
111                       AU6601_REG_SW_RESET);
112         for (i = 0; i < 100; i++) {
113                 if (!(alcor_read8(priv, AU6601_REG_SW_RESET) & val))
114                         return;
115                 udelay(50);
116         }
117         dev_err(host->dev, "%s: timeout\n", __func__);
118 }
119
120 static void alcor_data_set_dma(struct alcor_sdmmc_host *host)
121 {
122         struct alcor_pci_priv *priv = host->alcor_pci;
123         u32 addr;
124
125         if (!host->sg_count)
126                 return;
127
128         if (!host->sg) {
129                 dev_err(host->dev, "have blocks, but no SG\n");
130                 return;
131         }
132
133         if (!sg_dma_len(host->sg)) {
134                 dev_err(host->dev, "DMA SG len == 0\n");
135                 return;
136         }
137
138
139         addr = (u32)sg_dma_address(host->sg);
140
141         alcor_write32(priv, addr, AU6601_REG_SDMA_ADDR);
142         host->sg = sg_next(host->sg);
143         host->sg_count--;
144 }
145
146 static void alcor_trigger_data_transfer(struct alcor_sdmmc_host *host)
147 {
148         struct alcor_pci_priv *priv = host->alcor_pci;
149         struct mmc_data *data = host->data;
150         u8 ctrl = 0;
151
152         if (data->flags & MMC_DATA_WRITE)
153                 ctrl |= AU6601_DATA_WRITE;
154
155         if (data->host_cookie == COOKIE_MAPPED) {
156                 alcor_data_set_dma(host);
157                 ctrl |= AU6601_DATA_DMA_MODE;
158                 host->dma_on = 1;
159                 alcor_write32(priv, data->sg_count * 0x1000,
160                                AU6601_REG_BLOCK_SIZE);
161         } else {
162                 alcor_write32(priv, data->blksz, AU6601_REG_BLOCK_SIZE);
163         }
164
165         alcor_write8(priv, ctrl | AU6601_DATA_START_XFER,
166                       AU6601_DATA_XFER_CTRL);
167 }
168
169 static void alcor_trf_block_pio(struct alcor_sdmmc_host *host, bool read)
170 {
171         struct alcor_pci_priv *priv = host->alcor_pci;
172         size_t blksize, len;
173         u8 *buf;
174
175         if (!host->blocks)
176                 return;
177
178         if (host->dma_on) {
179                 dev_err(host->dev, "configured DMA but got PIO request.\n");
180                 return;
181         }
182
183         if (!!(host->data->flags & MMC_DATA_READ) != read) {
184                 dev_err(host->dev, "got unexpected direction %i != %i\n",
185                         !!(host->data->flags & MMC_DATA_READ), read);
186         }
187
188         if (!sg_miter_next(&host->sg_miter))
189                 return;
190
191         blksize = host->data->blksz;
192         len = min(host->sg_miter.length, blksize);
193
194         dev_dbg(host->dev, "PIO, %s block size: 0x%zx\n",
195                 read ? "read" : "write", blksize);
196
197         host->sg_miter.consumed = len;
198         host->blocks--;
199
200         buf = host->sg_miter.addr;
201
202         if (read)
203                 ioread32_rep(priv->iobase + AU6601_REG_BUFFER, buf, len >> 2);
204         else
205                 iowrite32_rep(priv->iobase + AU6601_REG_BUFFER, buf, len >> 2);
206
207         sg_miter_stop(&host->sg_miter);
208 }
209
210 static void alcor_prepare_sg_miter(struct alcor_sdmmc_host *host)
211 {
212         unsigned int flags = SG_MITER_ATOMIC;
213         struct mmc_data *data = host->data;
214
215         if (data->flags & MMC_DATA_READ)
216                 flags |= SG_MITER_TO_SG;
217         else
218                 flags |= SG_MITER_FROM_SG;
219         sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
220 }
221
222 static void alcor_prepare_data(struct alcor_sdmmc_host *host,
223                                struct mmc_command *cmd)
224 {
225         struct alcor_pci_priv *priv = host->alcor_pci;
226         struct mmc_data *data = cmd->data;
227
228         if (!data)
229                 return;
230
231
232         host->data = data;
233         host->data->bytes_xfered = 0;
234         host->blocks = data->blocks;
235         host->sg = data->sg;
236         host->sg_count = data->sg_count;
237         dev_dbg(host->dev, "prepare DATA: sg %i, blocks: %i\n",
238                         host->sg_count, host->blocks);
239
240         if (data->host_cookie != COOKIE_MAPPED)
241                 alcor_prepare_sg_miter(host);
242
243         alcor_write8(priv, 0, AU6601_DATA_XFER_CTRL);
244 }
245
246 static void alcor_send_cmd(struct alcor_sdmmc_host *host,
247                            struct mmc_command *cmd, bool set_timeout)
248 {
249         struct alcor_pci_priv *priv = host->alcor_pci;
250         unsigned long timeout = 0;
251         u8 ctrl = 0;
252
253         host->cmd = cmd;
254         alcor_prepare_data(host, cmd);
255
256         dev_dbg(host->dev, "send CMD. opcode: 0x%02x, arg; 0x%08x\n",
257                 cmd->opcode, cmd->arg);
258         alcor_write8(priv, cmd->opcode | 0x40, AU6601_REG_CMD_OPCODE);
259         alcor_write32be(priv, cmd->arg, AU6601_REG_CMD_ARG);
260
261         switch (mmc_resp_type(cmd)) {
262         case MMC_RSP_NONE:
263                 ctrl = AU6601_CMD_NO_RESP;
264                 break;
265         case MMC_RSP_R1:
266                 ctrl = AU6601_CMD_6_BYTE_CRC;
267                 break;
268         case MMC_RSP_R1B:
269                 ctrl = AU6601_CMD_6_BYTE_CRC | AU6601_CMD_STOP_WAIT_RDY;
270                 break;
271         case MMC_RSP_R2:
272                 ctrl = AU6601_CMD_17_BYTE_CRC;
273                 break;
274         case MMC_RSP_R3:
275                 ctrl = AU6601_CMD_6_BYTE_WO_CRC;
276                 break;
277         default:
278                 dev_err(host->dev, "%s: cmd->flag (0x%02x) is not valid\n",
279                         mmc_hostname(host->mmc), mmc_resp_type(cmd));
280                 break;
281         }
282
283         if (set_timeout) {
284                 if (!cmd->data && cmd->busy_timeout)
285                         timeout = cmd->busy_timeout;
286                 else
287                         timeout = 10000;
288
289                 schedule_delayed_work(&host->timeout_work,
290                                       msecs_to_jiffies(timeout));
291         }
292
293         dev_dbg(host->dev, "xfer ctrl: 0x%02x; timeout: %lu\n", ctrl, timeout);
294         alcor_write8(priv, ctrl | AU6601_CMD_START_XFER,
295                                  AU6601_CMD_XFER_CTRL);
296 }
297
298 static void alcor_request_complete(struct alcor_sdmmc_host *host,
299                                    bool cancel_timeout)
300 {
301         struct mmc_request *mrq;
302
303         /*
304          * If this work gets rescheduled while running, it will
305          * be run again afterwards but without any active request.
306          */
307         if (!host->mrq)
308                 return;
309
310         if (cancel_timeout)
311                 cancel_delayed_work(&host->timeout_work);
312
313         mrq = host->mrq;
314
315         host->mrq = NULL;
316         host->cmd = NULL;
317         host->data = NULL;
318         host->dma_on = 0;
319
320         mmc_request_done(host->mmc, mrq);
321 }
322
323 static void alcor_finish_data(struct alcor_sdmmc_host *host)
324 {
325         struct mmc_data *data;
326
327         data = host->data;
328         host->data = NULL;
329         host->dma_on = 0;
330
331         /*
332          * The specification states that the block count register must
333          * be updated, but it does not specify at what point in the
334          * data flow. That makes the register entirely useless to read
335          * back so we have to assume that nothing made it to the card
336          * in the event of an error.
337          */
338         if (data->error)
339                 data->bytes_xfered = 0;
340         else
341                 data->bytes_xfered = data->blksz * data->blocks;
342
343         /*
344          * Need to send CMD12 if -
345          * a) open-ended multiblock transfer (no CMD23)
346          * b) error in multiblock transfer
347          */
348         if (data->stop &&
349             (data->error ||
350              !host->mrq->sbc)) {
351
352                 /*
353                  * The controller needs a reset of internal state machines
354                  * upon error conditions.
355                  */
356                 if (data->error)
357                         alcor_reset(host, AU6601_RESET_CMD | AU6601_RESET_DATA);
358
359                 alcor_unmask_sd_irqs(host);
360                 alcor_send_cmd(host, data->stop, false);
361                 return;
362         }
363
364         alcor_request_complete(host, 1);
365 }
366
367 static void alcor_err_irq(struct alcor_sdmmc_host *host, u32 intmask)
368 {
369         dev_dbg(host->dev, "ERR IRQ %x\n", intmask);
370
371         if (host->cmd) {
372                 if (intmask & AU6601_INT_CMD_TIMEOUT_ERR)
373                         host->cmd->error = -ETIMEDOUT;
374                 else
375                         host->cmd->error = -EILSEQ;
376         }
377
378         if (host->data) {
379                 if (intmask & AU6601_INT_DATA_TIMEOUT_ERR)
380                         host->data->error = -ETIMEDOUT;
381                 else
382                         host->data->error = -EILSEQ;
383
384                 host->data->bytes_xfered = 0;
385         }
386
387         alcor_reset(host, AU6601_RESET_CMD | AU6601_RESET_DATA);
388         alcor_request_complete(host, 1);
389 }
390
391 static int alcor_cmd_irq_done(struct alcor_sdmmc_host *host, u32 intmask)
392 {
393         struct alcor_pci_priv *priv = host->alcor_pci;
394
395         intmask &= AU6601_INT_CMD_END;
396
397         if (!intmask)
398                 return true;
399
400         /* got CMD_END but no CMD is in progress, wake thread an process the
401          * error
402          */
403         if (!host->cmd)
404                 return false;
405
406         if (host->cmd->flags & MMC_RSP_PRESENT) {
407                 struct mmc_command *cmd = host->cmd;
408
409                 cmd->resp[0] = alcor_read32be(priv, AU6601_REG_CMD_RSP0);
410                 dev_dbg(host->dev, "RSP0: 0x%04x\n", cmd->resp[0]);
411                 if (host->cmd->flags & MMC_RSP_136) {
412                         cmd->resp[1] =
413                                 alcor_read32be(priv, AU6601_REG_CMD_RSP1);
414                         cmd->resp[2] =
415                                 alcor_read32be(priv, AU6601_REG_CMD_RSP2);
416                         cmd->resp[3] =
417                                 alcor_read32be(priv, AU6601_REG_CMD_RSP3);
418                         dev_dbg(host->dev, "RSP1,2,3: 0x%04x 0x%04x 0x%04x\n",
419                                 cmd->resp[1], cmd->resp[2], cmd->resp[3]);
420                 }
421
422         }
423
424         host->cmd->error = 0;
425
426         /* Processed actual command. */
427         if (!host->data)
428                 return false;
429
430         alcor_trigger_data_transfer(host);
431         host->cmd = NULL;
432         return true;
433 }
434
435 static void alcor_cmd_irq_thread(struct alcor_sdmmc_host *host, u32 intmask)
436 {
437         intmask &= AU6601_INT_CMD_END;
438
439         if (!intmask)
440                 return;
441
442         if (!host->cmd && intmask & AU6601_INT_CMD_END) {
443                 dev_dbg(host->dev, "Got command interrupt 0x%08x even though no command operation was in progress.\n",
444                         intmask);
445         }
446
447         /* Processed actual command. */
448         if (!host->data)
449                 alcor_request_complete(host, 1);
450         else
451                 alcor_trigger_data_transfer(host);
452         host->cmd = NULL;
453 }
454
455 static int alcor_data_irq_done(struct alcor_sdmmc_host *host, u32 intmask)
456 {
457         u32 tmp;
458
459         intmask &= AU6601_INT_DATA_MASK;
460
461         /* nothing here to do */
462         if (!intmask)
463                 return 1;
464
465         /* we was too fast and got DATA_END after it was processed?
466          * lets ignore it for now.
467          */
468         if (!host->data && intmask == AU6601_INT_DATA_END)
469                 return 1;
470
471         /* looks like an error, so lets handle it. */
472         if (!host->data)
473                 return 0;
474
475         tmp = intmask & (AU6601_INT_READ_BUF_RDY | AU6601_INT_WRITE_BUF_RDY
476                          | AU6601_INT_DMA_END);
477         switch (tmp) {
478         case 0:
479                 break;
480         case AU6601_INT_READ_BUF_RDY:
481                 alcor_trf_block_pio(host, true);
482                 return 1;
483         case AU6601_INT_WRITE_BUF_RDY:
484                 alcor_trf_block_pio(host, false);
485                 return 1;
486         case AU6601_INT_DMA_END:
487                 if (!host->sg_count)
488                         break;
489
490                 alcor_data_set_dma(host);
491                 break;
492         default:
493                 dev_err(host->dev, "Got READ_BUF_RDY and WRITE_BUF_RDY at same time\n");
494                 break;
495         }
496
497         if (intmask & AU6601_INT_DATA_END) {
498                 if (!host->dma_on && host->blocks) {
499                         alcor_trigger_data_transfer(host);
500                         return 1;
501                 } else {
502                         return 0;
503                 }
504         }
505
506         return 1;
507 }
508
509 static void alcor_data_irq_thread(struct alcor_sdmmc_host *host, u32 intmask)
510 {
511         intmask &= AU6601_INT_DATA_MASK;
512
513         if (!intmask)
514                 return;
515
516         if (!host->data) {
517                 dev_dbg(host->dev, "Got data interrupt 0x%08x even though no data operation was in progress.\n",
518                         intmask);
519                 alcor_reset(host, AU6601_RESET_DATA);
520                 return;
521         }
522
523         if (alcor_data_irq_done(host, intmask))
524                 return;
525
526         if ((intmask & AU6601_INT_DATA_END) || !host->blocks ||
527             (host->dma_on && !host->sg_count))
528                 alcor_finish_data(host);
529 }
530
531 static void alcor_cd_irq(struct alcor_sdmmc_host *host, u32 intmask)
532 {
533         dev_dbg(host->dev, "card %s\n",
534                 intmask & AU6601_INT_CARD_REMOVE ? "removed" : "inserted");
535
536         if (host->mrq) {
537                 dev_dbg(host->dev, "cancel all pending tasks.\n");
538
539                 if (host->data)
540                         host->data->error = -ENOMEDIUM;
541
542                 if (host->cmd)
543                         host->cmd->error = -ENOMEDIUM;
544                 else
545                         host->mrq->cmd->error = -ENOMEDIUM;
546
547                 alcor_request_complete(host, 1);
548         }
549
550         mmc_detect_change(host->mmc, msecs_to_jiffies(1));
551 }
552
553 static irqreturn_t alcor_irq_thread(int irq, void *d)
554 {
555         struct alcor_sdmmc_host *host = d;
556         irqreturn_t ret = IRQ_HANDLED;
557         u32 intmask, tmp;
558
559         mutex_lock(&host->cmd_mutex);
560
561         intmask = host->irq_status_sd;
562
563         /* some thing bad */
564         if (unlikely(!intmask || AU6601_INT_ALL_MASK == intmask)) {
565                 dev_dbg(host->dev, "unexpected IRQ: 0x%04x\n", intmask);
566                 ret = IRQ_NONE;
567                 goto exit;
568         }
569
570         tmp = intmask & (AU6601_INT_CMD_MASK | AU6601_INT_DATA_MASK);
571         if (tmp) {
572                 if (tmp & AU6601_INT_ERROR_MASK)
573                         alcor_err_irq(host, tmp);
574                 else {
575                         alcor_cmd_irq_thread(host, tmp);
576                         alcor_data_irq_thread(host, tmp);
577                 }
578                 intmask &= ~(AU6601_INT_CMD_MASK | AU6601_INT_DATA_MASK);
579         }
580
581         if (intmask & (AU6601_INT_CARD_INSERT | AU6601_INT_CARD_REMOVE)) {
582                 alcor_cd_irq(host, intmask);
583                 intmask &= ~(AU6601_INT_CARD_INSERT | AU6601_INT_CARD_REMOVE);
584         }
585
586         if (intmask & AU6601_INT_OVER_CURRENT_ERR) {
587                 dev_warn(host->dev,
588                          "warning: over current detected!\n");
589                 intmask &= ~AU6601_INT_OVER_CURRENT_ERR;
590         }
591
592         if (intmask)
593                 dev_dbg(host->dev, "got not handled IRQ: 0x%04x\n", intmask);
594
595 exit:
596         mutex_unlock(&host->cmd_mutex);
597         alcor_unmask_sd_irqs(host);
598         return ret;
599 }
600
601
602 static irqreturn_t alcor_irq(int irq, void *d)
603 {
604         struct alcor_sdmmc_host *host = d;
605         struct alcor_pci_priv *priv = host->alcor_pci;
606         u32 status, tmp;
607         irqreturn_t ret;
608         int cmd_done, data_done;
609
610         status = alcor_read32(priv, AU6601_REG_INT_STATUS);
611         if (!status)
612                 return IRQ_NONE;
613
614         alcor_write32(priv, status, AU6601_REG_INT_STATUS);
615
616         tmp = status & (AU6601_INT_READ_BUF_RDY | AU6601_INT_WRITE_BUF_RDY
617                         | AU6601_INT_DATA_END | AU6601_INT_DMA_END
618                         | AU6601_INT_CMD_END);
619         if (tmp == status) {
620                 cmd_done = alcor_cmd_irq_done(host, tmp);
621                 data_done = alcor_data_irq_done(host, tmp);
622                 /* use fast path for simple tasks */
623                 if (cmd_done && data_done) {
624                         ret = IRQ_HANDLED;
625                         goto alcor_irq_done;
626                 }
627         }
628
629         host->irq_status_sd = status;
630         ret = IRQ_WAKE_THREAD;
631         alcor_mask_sd_irqs(host);
632 alcor_irq_done:
633         return ret;
634 }
635
636 static void alcor_set_clock(struct alcor_sdmmc_host *host, unsigned int clock)
637 {
638         struct alcor_pci_priv *priv = host->alcor_pci;
639         int i, diff = 0x7fffffff, tmp_clock = 0;
640         u16 clk_src = 0;
641         u8 clk_div = 0;
642
643         if (clock == 0) {
644                 alcor_write16(priv, 0, AU6601_CLK_SELECT);
645                 return;
646         }
647
648         for (i = 0; i < ARRAY_SIZE(alcor_pll_cfg); i++) {
649                 unsigned int tmp_div, tmp_diff;
650                 const struct alcor_pll_conf *cfg = &alcor_pll_cfg[i];
651
652                 tmp_div = DIV_ROUND_UP(cfg->clk_src_freq, clock);
653                 if (cfg->min_div > tmp_div || tmp_div > cfg->max_div)
654                         continue;
655
656                 tmp_clock = DIV_ROUND_UP(cfg->clk_src_freq, tmp_div);
657                 tmp_diff = abs(clock - tmp_clock);
658
659                 if (tmp_diff >= 0 && tmp_diff < diff) {
660                         diff = tmp_diff;
661                         clk_src = cfg->clk_src_reg;
662                         clk_div = tmp_div;
663                 }
664         }
665
666         clk_src |= ((clk_div - 1) << 8);
667         clk_src |= AU6601_CLK_ENABLE;
668
669         dev_dbg(host->dev, "set freq %d cal freq %d, use div %d, mod %x\n",
670                         clock, tmp_clock, clk_div, clk_src);
671
672         alcor_write16(priv, clk_src, AU6601_CLK_SELECT);
673
674 }
675
676 static void alcor_set_timing(struct mmc_host *mmc, struct mmc_ios *ios)
677 {
678         struct alcor_sdmmc_host *host = mmc_priv(mmc);
679
680         if (ios->timing == MMC_TIMING_LEGACY) {
681                 alcor_rmw8(host, AU6601_CLK_DELAY,
682                             AU6601_CLK_POSITIVE_EDGE_ALL, 0);
683         } else {
684                 alcor_rmw8(host, AU6601_CLK_DELAY,
685                             0, AU6601_CLK_POSITIVE_EDGE_ALL);
686         }
687 }
688
689 static void alcor_set_bus_width(struct mmc_host *mmc, struct mmc_ios *ios)
690 {
691         struct alcor_sdmmc_host *host = mmc_priv(mmc);
692         struct alcor_pci_priv *priv = host->alcor_pci;
693
694         if (ios->bus_width == MMC_BUS_WIDTH_1) {
695                 alcor_write8(priv, 0, AU6601_REG_BUS_CTRL);
696         } else if (ios->bus_width == MMC_BUS_WIDTH_4) {
697                 alcor_write8(priv, AU6601_BUS_WIDTH_4BIT,
698                               AU6601_REG_BUS_CTRL);
699         } else
700                 dev_err(host->dev, "Unknown BUS mode\n");
701
702 }
703
704 static int alcor_card_busy(struct mmc_host *mmc)
705 {
706         struct alcor_sdmmc_host *host = mmc_priv(mmc);
707         struct alcor_pci_priv *priv = host->alcor_pci;
708         u8 status;
709
710         /* Check whether dat[0:3] low */
711         status = alcor_read8(priv, AU6601_DATA_PIN_STATE);
712
713         return !(status & AU6601_BUS_STAT_DAT_MASK);
714 }
715
716 static int alcor_get_cd(struct mmc_host *mmc)
717 {
718         struct alcor_sdmmc_host *host = mmc_priv(mmc);
719         struct alcor_pci_priv *priv = host->alcor_pci;
720         u8 detect;
721
722         detect = alcor_read8(priv, AU6601_DETECT_STATUS)
723                 & AU6601_DETECT_STATUS_M;
724         /* check if card is present then send command and data */
725         return (detect == AU6601_SD_DETECTED);
726 }
727
728 static int alcor_get_ro(struct mmc_host *mmc)
729 {
730         struct alcor_sdmmc_host *host = mmc_priv(mmc);
731         struct alcor_pci_priv *priv = host->alcor_pci;
732         u8 status;
733
734         /* get write protect pin status */
735         status = alcor_read8(priv, AU6601_INTERFACE_MODE_CTRL);
736
737         return !!(status & AU6601_SD_CARD_WP);
738 }
739
740 static void alcor_request(struct mmc_host *mmc, struct mmc_request *mrq)
741 {
742         struct alcor_sdmmc_host *host = mmc_priv(mmc);
743
744         mutex_lock(&host->cmd_mutex);
745
746         host->mrq = mrq;
747
748         /* check if card is present then send command and data */
749         if (alcor_get_cd(mmc))
750                 alcor_send_cmd(host, mrq->cmd, true);
751         else {
752                 mrq->cmd->error = -ENOMEDIUM;
753                 alcor_request_complete(host, 1);
754         }
755
756         mutex_unlock(&host->cmd_mutex);
757 }
758
759 static void alcor_pre_req(struct mmc_host *mmc,
760                            struct mmc_request *mrq)
761 {
762         struct alcor_sdmmc_host *host = mmc_priv(mmc);
763         struct mmc_data *data = mrq->data;
764         struct mmc_command *cmd = mrq->cmd;
765         struct scatterlist *sg;
766         unsigned int i, sg_len;
767
768         if (!data || !cmd)
769                 return;
770
771         data->host_cookie = COOKIE_UNMAPPED;
772
773         /* FIXME: looks like the DMA engine works only with CMD18 */
774         if (cmd->opcode != 18)
775                 return;
776         /*
777          * We don't do DMA on "complex" transfers, i.e. with
778          * non-word-aligned buffers or lengths. Also, we don't bother
779          * with all the DMA setup overhead for short transfers.
780          */
781         if (data->blocks * data->blksz < AU6601_MAX_DMA_BLOCK_SIZE)
782                 return;
783
784         if (data->blksz & 3)
785                 return;
786
787         for_each_sg(data->sg, sg, data->sg_len, i) {
788                 if (sg->length != AU6601_MAX_DMA_BLOCK_SIZE)
789                         return;
790         }
791
792         /* This data might be unmapped at this time */
793
794         sg_len = dma_map_sg(host->dev, data->sg, data->sg_len,
795                             mmc_get_dma_dir(data));
796         if (sg_len)
797                 data->host_cookie = COOKIE_MAPPED;
798
799         data->sg_count = sg_len;
800 }
801
802 static void alcor_post_req(struct mmc_host *mmc,
803                             struct mmc_request *mrq,
804                             int err)
805 {
806         struct alcor_sdmmc_host *host = mmc_priv(mmc);
807         struct mmc_data *data = mrq->data;
808
809         if (!data)
810                 return;
811
812         if (data->host_cookie == COOKIE_MAPPED) {
813                 dma_unmap_sg(host->dev,
814                              data->sg,
815                              data->sg_len,
816                              mmc_get_dma_dir(data));
817         }
818
819         data->host_cookie = COOKIE_UNMAPPED;
820 }
821
822 static void alcor_set_power_mode(struct mmc_host *mmc, struct mmc_ios *ios)
823 {
824         struct alcor_sdmmc_host *host = mmc_priv(mmc);
825         struct alcor_pci_priv *priv = host->alcor_pci;
826
827         switch (ios->power_mode) {
828         case MMC_POWER_OFF:
829                 alcor_set_clock(host, ios->clock);
830                 /* set all pins to input */
831                 alcor_write8(priv, 0, AU6601_OUTPUT_ENABLE);
832                 /* turn of VDD */
833                 alcor_write8(priv, 0, AU6601_POWER_CONTROL);
834                 break;
835         case MMC_POWER_UP:
836                 break;
837         case MMC_POWER_ON:
838                 /* This is most trickiest part. The order and timings of
839                  * instructions seems to play important role. Any changes may
840                  * confuse internal state engine if this HW.
841                  * FIXME: If we will ever get access to documentation, then this
842                  * part should be reviewed again.
843                  */
844
845                 /* enable SD card mode */
846                 alcor_write8(priv, AU6601_SD_CARD,
847                               AU6601_ACTIVE_CTRL);
848                 /* set signal voltage to 3.3V */
849                 alcor_write8(priv, 0, AU6601_OPT);
850                 /* no documentation about clk delay, for now just try to mimic
851                  * original driver.
852                  */
853                 alcor_write8(priv, 0x20, AU6601_CLK_DELAY);
854                 /* set BUS width to 1 bit */
855                 alcor_write8(priv, 0, AU6601_REG_BUS_CTRL);
856                 /* set CLK first time */
857                 alcor_set_clock(host, ios->clock);
858                 /* power on VDD */
859                 alcor_write8(priv, AU6601_SD_CARD,
860                               AU6601_POWER_CONTROL);
861                 /* wait until the CLK will get stable */
862                 mdelay(20);
863                 /* set CLK again, mimic original driver. */
864                 alcor_set_clock(host, ios->clock);
865
866                 /* enable output */
867                 alcor_write8(priv, AU6601_SD_CARD,
868                               AU6601_OUTPUT_ENABLE);
869                 /* The clk will not work on au6621. We need to trigger data
870                  * transfer.
871                  */
872                 alcor_write8(priv, AU6601_DATA_WRITE,
873                               AU6601_DATA_XFER_CTRL);
874                 /* configure timeout. Not clear what exactly it means. */
875                 alcor_write8(priv, 0x7d, AU6601_TIME_OUT_CTRL);
876                 mdelay(100);
877                 break;
878         default:
879                 dev_err(host->dev, "Unknown power parameter\n");
880         }
881 }
882
883 static void alcor_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
884 {
885         struct alcor_sdmmc_host *host = mmc_priv(mmc);
886
887         mutex_lock(&host->cmd_mutex);
888
889         dev_dbg(host->dev, "set ios. bus width: %x, power mode: %x\n",
890                 ios->bus_width, ios->power_mode);
891
892         if (ios->power_mode != host->cur_power_mode) {
893                 alcor_set_power_mode(mmc, ios);
894                 host->cur_power_mode = ios->power_mode;
895         } else {
896                 alcor_set_timing(mmc, ios);
897                 alcor_set_bus_width(mmc, ios);
898                 alcor_set_clock(host, ios->clock);
899         }
900
901         mutex_unlock(&host->cmd_mutex);
902 }
903
904 static int alcor_signal_voltage_switch(struct mmc_host *mmc,
905                                        struct mmc_ios *ios)
906 {
907         struct alcor_sdmmc_host *host = mmc_priv(mmc);
908
909         mutex_lock(&host->cmd_mutex);
910
911         switch (ios->signal_voltage) {
912         case MMC_SIGNAL_VOLTAGE_330:
913                 alcor_rmw8(host, AU6601_OPT, AU6601_OPT_SD_18V, 0);
914                 break;
915         case MMC_SIGNAL_VOLTAGE_180:
916                 alcor_rmw8(host, AU6601_OPT, 0, AU6601_OPT_SD_18V);
917                 break;
918         default:
919                 /* No signal voltage switch required */
920                 break;
921         }
922
923         mutex_unlock(&host->cmd_mutex);
924         return 0;
925 }
926
927 static const struct mmc_host_ops alcor_sdc_ops = {
928         .card_busy      = alcor_card_busy,
929         .get_cd         = alcor_get_cd,
930         .get_ro         = alcor_get_ro,
931         .post_req       = alcor_post_req,
932         .pre_req        = alcor_pre_req,
933         .request        = alcor_request,
934         .set_ios        = alcor_set_ios,
935         .start_signal_voltage_switch = alcor_signal_voltage_switch,
936 };
937
938 static void alcor_timeout_timer(struct work_struct *work)
939 {
940         struct delayed_work *d = to_delayed_work(work);
941         struct alcor_sdmmc_host *host = container_of(d, struct alcor_sdmmc_host,
942                                                 timeout_work);
943         mutex_lock(&host->cmd_mutex);
944
945         dev_dbg(host->dev, "triggered timeout\n");
946         if (host->mrq) {
947                 dev_err(host->dev, "Timeout waiting for hardware interrupt.\n");
948
949                 if (host->data) {
950                         host->data->error = -ETIMEDOUT;
951                 } else {
952                         if (host->cmd)
953                                 host->cmd->error = -ETIMEDOUT;
954                         else
955                                 host->mrq->cmd->error = -ETIMEDOUT;
956                 }
957
958                 alcor_reset(host, AU6601_RESET_CMD | AU6601_RESET_DATA);
959                 alcor_request_complete(host, 0);
960         }
961
962         mmiowb();
963         mutex_unlock(&host->cmd_mutex);
964 }
965
966 static void alcor_hw_init(struct alcor_sdmmc_host *host)
967 {
968         struct alcor_pci_priv *priv = host->alcor_pci;
969         struct alcor_dev_cfg *cfg = priv->cfg;
970
971         /* FIXME: This part is a mimics HW init of original driver.
972          * If we will ever get access to documentation, then this part
973          * should be reviewed again.
974          */
975
976         /* reset command state engine */
977         alcor_reset(host, AU6601_RESET_CMD);
978
979         alcor_write8(priv, 0, AU6601_DMA_BOUNDARY);
980         /* enable sd card mode */
981         alcor_write8(priv, AU6601_SD_CARD, AU6601_ACTIVE_CTRL);
982
983         /* set BUS width to 1 bit */
984         alcor_write8(priv, 0, AU6601_REG_BUS_CTRL);
985
986         /* reset data state engine */
987         alcor_reset(host, AU6601_RESET_DATA);
988         /* Not sure if a voodoo with AU6601_DMA_BOUNDARY is really needed */
989         alcor_write8(priv, 0, AU6601_DMA_BOUNDARY);
990
991         alcor_write8(priv, 0, AU6601_INTERFACE_MODE_CTRL);
992         /* not clear what we are doing here. */
993         alcor_write8(priv, 0x44, AU6601_PAD_DRIVE0);
994         alcor_write8(priv, 0x44, AU6601_PAD_DRIVE1);
995         alcor_write8(priv, 0x00, AU6601_PAD_DRIVE2);
996
997         /* for 6601 - dma_boundary; for 6621 - dma_page_cnt
998          * exact meaning of this register is not clear.
999          */
1000         alcor_write8(priv, cfg->dma, AU6601_DMA_BOUNDARY);
1001
1002         /* make sure all pins are set to input and VDD is off */
1003         alcor_write8(priv, 0, AU6601_OUTPUT_ENABLE);
1004         alcor_write8(priv, 0, AU6601_POWER_CONTROL);
1005
1006         alcor_write8(priv, AU6601_DETECT_EN, AU6601_DETECT_STATUS);
1007         /* now we should be safe to enable IRQs */
1008         alcor_unmask_sd_irqs(host);
1009 }
1010
1011 static void alcor_hw_uninit(struct alcor_sdmmc_host *host)
1012 {
1013         struct alcor_pci_priv *priv = host->alcor_pci;
1014
1015         alcor_mask_sd_irqs(host);
1016         alcor_reset(host, AU6601_RESET_CMD | AU6601_RESET_DATA);
1017
1018         alcor_write8(priv, 0, AU6601_DETECT_STATUS);
1019
1020         alcor_write8(priv, 0, AU6601_OUTPUT_ENABLE);
1021         alcor_write8(priv, 0, AU6601_POWER_CONTROL);
1022
1023         alcor_write8(priv, 0, AU6601_OPT);
1024 }
1025
1026 static void alcor_init_mmc(struct alcor_sdmmc_host *host)
1027 {
1028         struct mmc_host *mmc = host->mmc;
1029
1030         mmc->f_min = AU6601_MIN_CLOCK;
1031         mmc->f_max = AU6601_MAX_CLOCK;
1032         mmc->ocr_avail = MMC_VDD_33_34;
1033         mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SD_HIGHSPEED
1034                 | MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_SDR50
1035                 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50;
1036         mmc->caps2 = MMC_CAP2_NO_SDIO;
1037         mmc->ops = &alcor_sdc_ops;
1038
1039         /* The hardware does DMA data transfer of 4096 bytes to/from a single
1040          * buffer address. Scatterlists are not supported, but upon DMA
1041          * completion (signalled via IRQ), the original vendor driver does
1042          * then immediately set up another DMA transfer of the next 4096
1043          * bytes.
1044          *
1045          * This means that we need to handle the I/O in 4096 byte chunks.
1046          * Lacking a way to limit the sglist entries to 4096 bytes, we instead
1047          * impose that only one segment is provided, with maximum size 4096,
1048          * which also happens to be the minimum size. This means that the
1049          * single-entry sglist handled by this driver can be handed directly
1050          * to the hardware, nice and simple.
1051          *
1052          * Unfortunately though, that means we only do 4096 bytes I/O per
1053          * MMC command. A future improvement would be to make the driver
1054          * accept sg lists and entries of any size, and simply iterate
1055          * through them 4096 bytes at a time.
1056          */
1057         mmc->max_segs = AU6601_MAX_DMA_SEGMENTS;
1058         mmc->max_seg_size = AU6601_MAX_DMA_BLOCK_SIZE;
1059         mmc->max_req_size = mmc->max_seg_size;
1060 }
1061
1062 static int alcor_pci_sdmmc_drv_probe(struct platform_device *pdev)
1063 {
1064         struct alcor_pci_priv *priv = pdev->dev.platform_data;
1065         struct mmc_host *mmc;
1066         struct alcor_sdmmc_host *host;
1067         int ret;
1068
1069         mmc = mmc_alloc_host(sizeof(*host), &pdev->dev);
1070         if (!mmc) {
1071                 dev_err(&pdev->dev, "Can't allocate MMC\n");
1072                 return -ENOMEM;
1073         }
1074
1075         host = mmc_priv(mmc);
1076         host->mmc = mmc;
1077         host->dev = &pdev->dev;
1078         host->cur_power_mode = MMC_POWER_UNDEFINED;
1079         host->alcor_pci = priv;
1080
1081         /* make sure irqs are disabled */
1082         alcor_write32(priv, 0, AU6601_REG_INT_ENABLE);
1083         alcor_write32(priv, 0, AU6601_MS_INT_ENABLE);
1084
1085         ret = devm_request_threaded_irq(&pdev->dev, priv->irq,
1086                         alcor_irq, alcor_irq_thread, IRQF_SHARED,
1087                         DRV_NAME_ALCOR_PCI_SDMMC, host);
1088
1089         if (ret) {
1090                 dev_err(&pdev->dev, "Failed to get irq for data line\n");
1091                 return ret;
1092         }
1093
1094         mutex_init(&host->cmd_mutex);
1095         INIT_DELAYED_WORK(&host->timeout_work, alcor_timeout_timer);
1096
1097         alcor_init_mmc(host);
1098         alcor_hw_init(host);
1099
1100         dev_set_drvdata(&pdev->dev, host);
1101         mmc_add_host(mmc);
1102         return 0;
1103 }
1104
1105 static int alcor_pci_sdmmc_drv_remove(struct platform_device *pdev)
1106 {
1107         struct alcor_sdmmc_host *host = dev_get_drvdata(&pdev->dev);
1108
1109         if (cancel_delayed_work_sync(&host->timeout_work))
1110                 alcor_request_complete(host, 0);
1111
1112         alcor_hw_uninit(host);
1113         mmc_remove_host(host->mmc);
1114         mmc_free_host(host->mmc);
1115
1116         return 0;
1117 }
1118
1119 #ifdef CONFIG_PM_SLEEP
1120 static int alcor_pci_sdmmc_suspend(struct device *dev)
1121 {
1122         struct alcor_sdmmc_host *host = dev_get_drvdata(dev);
1123
1124         if (cancel_delayed_work_sync(&host->timeout_work))
1125                 alcor_request_complete(host, 0);
1126
1127         alcor_hw_uninit(host);
1128
1129         return 0;
1130 }
1131
1132 static int alcor_pci_sdmmc_resume(struct device *dev)
1133 {
1134         struct alcor_sdmmc_host *host = dev_get_drvdata(dev);
1135
1136         alcor_hw_init(host);
1137
1138         return 0;
1139 }
1140 #endif /* CONFIG_PM_SLEEP */
1141
1142 static SIMPLE_DEV_PM_OPS(alcor_mmc_pm_ops, alcor_pci_sdmmc_suspend,
1143                          alcor_pci_sdmmc_resume);
1144
1145 static const struct platform_device_id alcor_pci_sdmmc_ids[] = {
1146         {
1147                 .name = DRV_NAME_ALCOR_PCI_SDMMC,
1148         }, {
1149                 /* sentinel */
1150         }
1151 };
1152 MODULE_DEVICE_TABLE(platform, alcor_pci_sdmmc_ids);
1153
1154 static struct platform_driver alcor_pci_sdmmc_driver = {
1155         .probe          = alcor_pci_sdmmc_drv_probe,
1156         .remove         = alcor_pci_sdmmc_drv_remove,
1157         .id_table       = alcor_pci_sdmmc_ids,
1158         .driver         = {
1159                 .name   = DRV_NAME_ALCOR_PCI_SDMMC,
1160                 .pm     = &alcor_mmc_pm_ops
1161         },
1162 };
1163 module_platform_driver(alcor_pci_sdmmc_driver);
1164
1165 MODULE_AUTHOR("Oleksij Rempel <linux@rempel-privat.de>");
1166 MODULE_DESCRIPTION("PCI driver for Alcor Micro AU6601 Secure Digital Host Controller Interface");
1167 MODULE_LICENSE("GPL");