mmc: sdhci-omap: Update 'power_mode' outside sdhci_omap_init_74_clocks
[linux-2.6-microblaze.git] / drivers / mmc / host / sdhci-omap.c
1 /**
2  * SDHCI Controller driver for TI's OMAP SoCs
3  *
4  * Copyright (C) 2017 Texas Instruments
5  * Author: Kishon Vijay Abraham I <kishon@ti.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 of
9  * the License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/delay.h>
21 #include <linux/mmc/slot-gpio.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/regulator/consumer.h>
28
29 #include "sdhci-pltfm.h"
30
31 #define SDHCI_OMAP_CON          0x12c
32 #define CON_DW8                 BIT(5)
33 #define CON_DMA_MASTER          BIT(20)
34 #define CON_INIT                BIT(1)
35 #define CON_OD                  BIT(0)
36
37 #define SDHCI_OMAP_CMD          0x20c
38
39 #define SDHCI_OMAP_HCTL         0x228
40 #define HCTL_SDBP               BIT(8)
41 #define HCTL_SDVS_SHIFT         9
42 #define HCTL_SDVS_MASK          (0x7 << HCTL_SDVS_SHIFT)
43 #define HCTL_SDVS_33            (0x7 << HCTL_SDVS_SHIFT)
44 #define HCTL_SDVS_30            (0x6 << HCTL_SDVS_SHIFT)
45 #define HCTL_SDVS_18            (0x5 << HCTL_SDVS_SHIFT)
46
47 #define SDHCI_OMAP_SYSCTL       0x22c
48 #define SYSCTL_CEN              BIT(2)
49 #define SYSCTL_CLKD_SHIFT       6
50 #define SYSCTL_CLKD_MASK        0x3ff
51
52 #define SDHCI_OMAP_STAT         0x230
53
54 #define SDHCI_OMAP_IE           0x234
55 #define INT_CC_EN               BIT(0)
56
57 #define SDHCI_OMAP_AC12         0x23c
58 #define AC12_V1V8_SIGEN         BIT(19)
59
60 #define SDHCI_OMAP_CAPA         0x240
61 #define CAPA_VS33               BIT(24)
62 #define CAPA_VS30               BIT(25)
63 #define CAPA_VS18               BIT(26)
64
65 #define SDHCI_OMAP_TIMEOUT      1               /* 1 msec */
66
67 #define SYSCTL_CLKD_MAX         0x3FF
68
69 #define IOV_1V8                 1800000         /* 180000 uV */
70 #define IOV_3V0                 3000000         /* 300000 uV */
71 #define IOV_3V3                 3300000         /* 330000 uV */
72
73 struct sdhci_omap_data {
74         u32 offset;
75 };
76
77 struct sdhci_omap_host {
78         void __iomem            *base;
79         struct device           *dev;
80         struct  regulator       *pbias;
81         bool                    pbias_enabled;
82         struct sdhci_host       *host;
83         u8                      bus_mode;
84         u8                      power_mode;
85 };
86
87 static inline u32 sdhci_omap_readl(struct sdhci_omap_host *host,
88                                    unsigned int offset)
89 {
90         return readl(host->base + offset);
91 }
92
93 static inline void sdhci_omap_writel(struct sdhci_omap_host *host,
94                                      unsigned int offset, u32 data)
95 {
96         writel(data, host->base + offset);
97 }
98
99 static int sdhci_omap_set_pbias(struct sdhci_omap_host *omap_host,
100                                 bool power_on, unsigned int iov)
101 {
102         int ret;
103         struct device *dev = omap_host->dev;
104
105         if (IS_ERR(omap_host->pbias))
106                 return 0;
107
108         if (power_on) {
109                 ret = regulator_set_voltage(omap_host->pbias, iov, iov);
110                 if (ret) {
111                         dev_err(dev, "pbias set voltage failed\n");
112                         return ret;
113                 }
114
115                 if (omap_host->pbias_enabled)
116                         return 0;
117
118                 ret = regulator_enable(omap_host->pbias);
119                 if (ret) {
120                         dev_err(dev, "pbias reg enable fail\n");
121                         return ret;
122                 }
123
124                 omap_host->pbias_enabled = true;
125         } else {
126                 if (!omap_host->pbias_enabled)
127                         return 0;
128
129                 ret = regulator_disable(omap_host->pbias);
130                 if (ret) {
131                         dev_err(dev, "pbias reg disable fail\n");
132                         return ret;
133                 }
134                 omap_host->pbias_enabled = false;
135         }
136
137         return 0;
138 }
139
140 static int sdhci_omap_enable_iov(struct sdhci_omap_host *omap_host,
141                                  unsigned int iov)
142 {
143         int ret;
144         struct sdhci_host *host = omap_host->host;
145         struct mmc_host *mmc = host->mmc;
146
147         ret = sdhci_omap_set_pbias(omap_host, false, 0);
148         if (ret)
149                 return ret;
150
151         if (!IS_ERR(mmc->supply.vqmmc)) {
152                 ret = regulator_set_voltage(mmc->supply.vqmmc, iov, iov);
153                 if (ret) {
154                         dev_err(mmc_dev(mmc), "vqmmc set voltage failed\n");
155                         return ret;
156                 }
157         }
158
159         ret = sdhci_omap_set_pbias(omap_host, true, iov);
160         if (ret)
161                 return ret;
162
163         return 0;
164 }
165
166 static void sdhci_omap_conf_bus_power(struct sdhci_omap_host *omap_host,
167                                       unsigned char signal_voltage)
168 {
169         u32 reg;
170         ktime_t timeout;
171
172         reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL);
173         reg &= ~HCTL_SDVS_MASK;
174
175         if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
176                 reg |= HCTL_SDVS_33;
177         else
178                 reg |= HCTL_SDVS_18;
179
180         sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
181
182         reg |= HCTL_SDBP;
183         sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
184
185         /* wait 1ms */
186         timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
187         while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL) & HCTL_SDBP)) {
188                 if (WARN_ON(ktime_after(ktime_get(), timeout)))
189                         return;
190                 usleep_range(5, 10);
191         }
192 }
193
194 static int sdhci_omap_start_signal_voltage_switch(struct mmc_host *mmc,
195                                                   struct mmc_ios *ios)
196 {
197         u32 reg;
198         int ret;
199         unsigned int iov;
200         struct sdhci_host *host = mmc_priv(mmc);
201         struct sdhci_pltfm_host *pltfm_host;
202         struct sdhci_omap_host *omap_host;
203         struct device *dev;
204
205         pltfm_host = sdhci_priv(host);
206         omap_host = sdhci_pltfm_priv(pltfm_host);
207         dev = omap_host->dev;
208
209         if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
210                 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
211                 if (!(reg & CAPA_VS33))
212                         return -EOPNOTSUPP;
213
214                 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
215
216                 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
217                 reg &= ~AC12_V1V8_SIGEN;
218                 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
219
220                 iov = IOV_3V3;
221         } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
222                 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
223                 if (!(reg & CAPA_VS18))
224                         return -EOPNOTSUPP;
225
226                 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
227
228                 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
229                 reg |= AC12_V1V8_SIGEN;
230                 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
231
232                 iov = IOV_1V8;
233         } else {
234                 return -EOPNOTSUPP;
235         }
236
237         ret = sdhci_omap_enable_iov(omap_host, iov);
238         if (ret) {
239                 dev_err(dev, "failed to switch IO voltage to %dmV\n", iov);
240                 return ret;
241         }
242
243         dev_dbg(dev, "IO voltage switched to %dmV\n", iov);
244         return 0;
245 }
246
247 static void sdhci_omap_set_power_mode(struct sdhci_omap_host *omap_host,
248                                       u8 power_mode)
249 {
250         omap_host->power_mode = power_mode;
251 }
252
253 static void sdhci_omap_set_bus_mode(struct sdhci_omap_host *omap_host,
254                                     unsigned int mode)
255 {
256         u32 reg;
257
258         if (omap_host->bus_mode == mode)
259                 return;
260
261         reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
262         if (mode == MMC_BUSMODE_OPENDRAIN)
263                 reg |= CON_OD;
264         else
265                 reg &= ~CON_OD;
266         sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
267
268         omap_host->bus_mode = mode;
269 }
270
271 static void sdhci_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
272 {
273         struct sdhci_host *host = mmc_priv(mmc);
274         struct sdhci_pltfm_host *pltfm_host;
275         struct sdhci_omap_host *omap_host;
276
277         pltfm_host = sdhci_priv(host);
278         omap_host = sdhci_pltfm_priv(pltfm_host);
279
280         sdhci_omap_set_bus_mode(omap_host, ios->bus_mode);
281         sdhci_set_ios(mmc, ios);
282         sdhci_omap_set_power_mode(omap_host, ios->power_mode);
283 }
284
285 static u16 sdhci_omap_calc_divisor(struct sdhci_pltfm_host *host,
286                                    unsigned int clock)
287 {
288         u16 dsor;
289
290         dsor = DIV_ROUND_UP(clk_get_rate(host->clk), clock);
291         if (dsor > SYSCTL_CLKD_MAX)
292                 dsor = SYSCTL_CLKD_MAX;
293
294         return dsor;
295 }
296
297 static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host)
298 {
299         u32 reg;
300
301         reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
302         reg |= SYSCTL_CEN;
303         sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
304 }
305
306 static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host)
307 {
308         u32 reg;
309
310         reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
311         reg &= ~SYSCTL_CEN;
312         sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
313 }
314
315 static void sdhci_omap_set_clock(struct sdhci_host *host, unsigned int clock)
316 {
317         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
318         struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
319         unsigned long clkdiv;
320
321         sdhci_omap_stop_clock(omap_host);
322
323         if (!clock)
324                 return;
325
326         clkdiv = sdhci_omap_calc_divisor(pltfm_host, clock);
327         clkdiv = (clkdiv & SYSCTL_CLKD_MASK) << SYSCTL_CLKD_SHIFT;
328         sdhci_enable_clk(host, clkdiv);
329
330         sdhci_omap_start_clock(omap_host);
331 }
332
333 static void sdhci_omap_set_power(struct sdhci_host *host, unsigned char mode,
334                           unsigned short vdd)
335 {
336         struct mmc_host *mmc = host->mmc;
337
338         mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
339 }
340
341 static int sdhci_omap_enable_dma(struct sdhci_host *host)
342 {
343         u32 reg;
344         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
345         struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
346
347         reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
348         reg |= CON_DMA_MASTER;
349         sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
350
351         return 0;
352 }
353
354 static unsigned int sdhci_omap_get_min_clock(struct sdhci_host *host)
355 {
356         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
357
358         return clk_get_rate(pltfm_host->clk) / SYSCTL_CLKD_MAX;
359 }
360
361 static void sdhci_omap_set_bus_width(struct sdhci_host *host, int width)
362 {
363         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
364         struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
365         u32 reg;
366
367         reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
368         if (width == MMC_BUS_WIDTH_8)
369                 reg |= CON_DW8;
370         else
371                 reg &= ~CON_DW8;
372         sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
373
374         sdhci_set_bus_width(host, width);
375 }
376
377 static void sdhci_omap_init_74_clocks(struct sdhci_host *host, u8 power_mode)
378 {
379         u32 reg;
380         ktime_t timeout;
381         struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
382         struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
383
384         if (omap_host->power_mode == power_mode)
385                 return;
386
387         if (power_mode != MMC_POWER_ON)
388                 return;
389
390         disable_irq(host->irq);
391
392         reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
393         reg |= CON_INIT;
394         sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
395         sdhci_omap_writel(omap_host, SDHCI_OMAP_CMD, 0x0);
396
397         /* wait 1ms */
398         timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
399         while (!(sdhci_omap_readl(omap_host, SDHCI_OMAP_STAT) & INT_CC_EN)) {
400                 if (WARN_ON(ktime_after(ktime_get(), timeout)))
401                         return;
402                 usleep_range(5, 10);
403         }
404
405         reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
406         reg &= ~CON_INIT;
407         sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
408         sdhci_omap_writel(omap_host, SDHCI_OMAP_STAT, INT_CC_EN);
409
410         enable_irq(host->irq);
411 }
412
413 static struct sdhci_ops sdhci_omap_ops = {
414         .set_clock = sdhci_omap_set_clock,
415         .set_power = sdhci_omap_set_power,
416         .enable_dma = sdhci_omap_enable_dma,
417         .get_max_clock = sdhci_pltfm_clk_get_max_clock,
418         .get_min_clock = sdhci_omap_get_min_clock,
419         .set_bus_width = sdhci_omap_set_bus_width,
420         .platform_send_init_74_clocks = sdhci_omap_init_74_clocks,
421         .reset = sdhci_reset,
422         .set_uhs_signaling = sdhci_set_uhs_signaling,
423 };
424
425 static int sdhci_omap_set_capabilities(struct sdhci_omap_host *omap_host)
426 {
427         u32 reg;
428         int ret = 0;
429         struct device *dev = omap_host->dev;
430         struct regulator *vqmmc;
431
432         vqmmc = regulator_get(dev, "vqmmc");
433         if (IS_ERR(vqmmc)) {
434                 ret = PTR_ERR(vqmmc);
435                 goto reg_put;
436         }
437
438         /* voltage capabilities might be set by boot loader, clear it */
439         reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
440         reg &= ~(CAPA_VS18 | CAPA_VS30 | CAPA_VS33);
441
442         if (regulator_is_supported_voltage(vqmmc, IOV_3V3, IOV_3V3))
443                 reg |= CAPA_VS33;
444         if (regulator_is_supported_voltage(vqmmc, IOV_1V8, IOV_1V8))
445                 reg |= CAPA_VS18;
446
447         sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, reg);
448
449 reg_put:
450         regulator_put(vqmmc);
451
452         return ret;
453 }
454
455 static const struct sdhci_pltfm_data sdhci_omap_pdata = {
456         .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
457                   SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
458                   SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
459                   SDHCI_QUIRK_NO_HISPD_BIT |
460                   SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
461         .quirks2 = SDHCI_QUIRK2_NO_1_8_V |
462                    SDHCI_QUIRK2_ACMD23_BROKEN |
463                    SDHCI_QUIRK2_RSP_136_HAS_CRC,
464         .ops = &sdhci_omap_ops,
465 };
466
467 static const struct sdhci_omap_data dra7_data = {
468         .offset = 0x200,
469 };
470
471 static const struct of_device_id omap_sdhci_match[] = {
472         { .compatible = "ti,dra7-sdhci", .data = &dra7_data },
473         {},
474 };
475 MODULE_DEVICE_TABLE(of, omap_sdhci_match);
476
477 static int sdhci_omap_probe(struct platform_device *pdev)
478 {
479         int ret;
480         u32 offset;
481         struct device *dev = &pdev->dev;
482         struct sdhci_host *host;
483         struct sdhci_pltfm_host *pltfm_host;
484         struct sdhci_omap_host *omap_host;
485         struct mmc_host *mmc;
486         const struct of_device_id *match;
487         struct sdhci_omap_data *data;
488
489         match = of_match_device(omap_sdhci_match, dev);
490         if (!match)
491                 return -EINVAL;
492
493         data = (struct sdhci_omap_data *)match->data;
494         if (!data) {
495                 dev_err(dev, "no sdhci omap data\n");
496                 return -EINVAL;
497         }
498         offset = data->offset;
499
500         host = sdhci_pltfm_init(pdev, &sdhci_omap_pdata,
501                                 sizeof(*omap_host));
502         if (IS_ERR(host)) {
503                 dev_err(dev, "Failed sdhci_pltfm_init\n");
504                 return PTR_ERR(host);
505         }
506
507         pltfm_host = sdhci_priv(host);
508         omap_host = sdhci_pltfm_priv(pltfm_host);
509         omap_host->host = host;
510         omap_host->base = host->ioaddr;
511         omap_host->dev = dev;
512         omap_host->power_mode = MMC_POWER_UNDEFINED;
513         host->ioaddr += offset;
514
515         mmc = host->mmc;
516         ret = mmc_of_parse(mmc);
517         if (ret)
518                 goto err_pltfm_free;
519
520         pltfm_host->clk = devm_clk_get(dev, "fck");
521         if (IS_ERR(pltfm_host->clk)) {
522                 ret = PTR_ERR(pltfm_host->clk);
523                 goto err_pltfm_free;
524         }
525
526         ret = clk_set_rate(pltfm_host->clk, mmc->f_max);
527         if (ret) {
528                 dev_err(dev, "failed to set clock to %d\n", mmc->f_max);
529                 goto err_pltfm_free;
530         }
531
532         omap_host->pbias = devm_regulator_get_optional(dev, "pbias");
533         if (IS_ERR(omap_host->pbias)) {
534                 ret = PTR_ERR(omap_host->pbias);
535                 if (ret != -ENODEV)
536                         goto err_pltfm_free;
537                 dev_dbg(dev, "unable to get pbias regulator %d\n", ret);
538         }
539         omap_host->pbias_enabled = false;
540
541         /*
542          * omap_device_pm_domain has callbacks to enable the main
543          * functional clock, interface clock and also configure the
544          * SYSCONFIG register of omap devices. The callback will be invoked
545          * as part of pm_runtime_get_sync.
546          */
547         pm_runtime_enable(dev);
548         ret = pm_runtime_get_sync(dev);
549         if (ret < 0) {
550                 dev_err(dev, "pm_runtime_get_sync failed\n");
551                 pm_runtime_put_noidle(dev);
552                 goto err_rpm_disable;
553         }
554
555         ret = sdhci_omap_set_capabilities(omap_host);
556         if (ret) {
557                 dev_err(dev, "failed to set system capabilities\n");
558                 goto err_put_sync;
559         }
560
561         host->mmc_host_ops.get_ro = mmc_gpio_get_ro;
562         host->mmc_host_ops.start_signal_voltage_switch =
563                                         sdhci_omap_start_signal_voltage_switch;
564         host->mmc_host_ops.set_ios = sdhci_omap_set_ios;
565
566         sdhci_read_caps(host);
567         host->caps |= SDHCI_CAN_DO_ADMA2;
568
569         ret = sdhci_add_host(host);
570         if (ret)
571                 goto err_put_sync;
572
573         return 0;
574
575 err_put_sync:
576         pm_runtime_put_sync(dev);
577
578 err_rpm_disable:
579         pm_runtime_disable(dev);
580
581 err_pltfm_free:
582         sdhci_pltfm_free(pdev);
583         return ret;
584 }
585
586 static int sdhci_omap_remove(struct platform_device *pdev)
587 {
588         struct device *dev = &pdev->dev;
589         struct sdhci_host *host = platform_get_drvdata(pdev);
590
591         sdhci_remove_host(host, true);
592         pm_runtime_put_sync(dev);
593         pm_runtime_disable(dev);
594         sdhci_pltfm_free(pdev);
595
596         return 0;
597 }
598
599 static struct platform_driver sdhci_omap_driver = {
600         .probe = sdhci_omap_probe,
601         .remove = sdhci_omap_remove,
602         .driver = {
603                    .name = "sdhci-omap",
604                    .of_match_table = omap_sdhci_match,
605                   },
606 };
607
608 module_platform_driver(sdhci_omap_driver);
609
610 MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs");
611 MODULE_AUTHOR("Texas Instruments Inc.");
612 MODULE_LICENSE("GPL v2");
613 MODULE_ALIAS("platform:sdhci_omap");