blob: d02f5cf76b3d171d75760ab8debe9bb91edab56b [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/**
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#include <linux/pinctrl/consumer.h>
29#include <linux/sys_soc.h>
30
31#include "sdhci-pltfm.h"
32
33#define SDHCI_OMAP_CON 0x12c
34#define CON_DW8 BIT(5)
35#define CON_DMA_MASTER BIT(20)
36#define CON_DDR BIT(19)
37#define CON_CLKEXTFREE BIT(16)
38#define CON_PADEN BIT(15)
39#define CON_CTPL BIT(11)
40#define CON_INIT BIT(1)
41#define CON_OD BIT(0)
42
43#define SDHCI_OMAP_DLL 0x0134
44#define DLL_SWT BIT(20)
45#define DLL_FORCE_SR_C_SHIFT 13
46#define DLL_FORCE_SR_C_MASK (0x7f << DLL_FORCE_SR_C_SHIFT)
47#define DLL_FORCE_VALUE BIT(12)
48#define DLL_CALIB BIT(1)
49
50#define SDHCI_OMAP_CMD 0x20c
51
52#define SDHCI_OMAP_PSTATE 0x0224
53#define PSTATE_DLEV_DAT0 BIT(20)
54#define PSTATE_DATI BIT(1)
55
56#define SDHCI_OMAP_HCTL 0x228
57#define HCTL_SDBP BIT(8)
58#define HCTL_SDVS_SHIFT 9
59#define HCTL_SDVS_MASK (0x7 << HCTL_SDVS_SHIFT)
60#define HCTL_SDVS_33 (0x7 << HCTL_SDVS_SHIFT)
61#define HCTL_SDVS_30 (0x6 << HCTL_SDVS_SHIFT)
62#define HCTL_SDVS_18 (0x5 << HCTL_SDVS_SHIFT)
63
64#define SDHCI_OMAP_SYSCTL 0x22c
65#define SYSCTL_CEN BIT(2)
66#define SYSCTL_CLKD_SHIFT 6
67#define SYSCTL_CLKD_MASK 0x3ff
68
69#define SDHCI_OMAP_STAT 0x230
70
71#define SDHCI_OMAP_IE 0x234
72#define INT_CC_EN BIT(0)
73
74#define SDHCI_OMAP_AC12 0x23c
75#define AC12_V1V8_SIGEN BIT(19)
76#define AC12_SCLK_SEL BIT(23)
77
78#define SDHCI_OMAP_CAPA 0x240
79#define CAPA_VS33 BIT(24)
80#define CAPA_VS30 BIT(25)
81#define CAPA_VS18 BIT(26)
82
83#define SDHCI_OMAP_CAPA2 0x0244
84#define CAPA2_TSDR50 BIT(13)
85
86#define SDHCI_OMAP_TIMEOUT 1 /* 1 msec */
87
88#define SYSCTL_CLKD_MAX 0x3FF
89
90#define IOV_1V8 1800000 /* 180000 uV */
91#define IOV_3V0 3000000 /* 300000 uV */
92#define IOV_3V3 3300000 /* 330000 uV */
93
94#define MAX_PHASE_DELAY 0x7C
95
96/* sdhci-omap controller flags */
97#define SDHCI_OMAP_REQUIRE_IODELAY BIT(0)
98
99struct sdhci_omap_data {
100 u32 offset;
101 u8 flags;
102};
103
104struct sdhci_omap_host {
105 char *version;
106 void __iomem *base;
107 struct device *dev;
108 struct regulator *pbias;
109 bool pbias_enabled;
110 struct sdhci_host *host;
111 u8 bus_mode;
112 u8 power_mode;
113 u8 timing;
114 u8 flags;
115
116 struct pinctrl *pinctrl;
117 struct pinctrl_state **pinctrl_state;
118};
119
120static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host);
121static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host);
122
123static inline u32 sdhci_omap_readl(struct sdhci_omap_host *host,
124 unsigned int offset)
125{
126 return readl(host->base + offset);
127}
128
129static inline void sdhci_omap_writel(struct sdhci_omap_host *host,
130 unsigned int offset, u32 data)
131{
132 writel(data, host->base + offset);
133}
134
135static int sdhci_omap_set_pbias(struct sdhci_omap_host *omap_host,
136 bool power_on, unsigned int iov)
137{
138 int ret;
139 struct device *dev = omap_host->dev;
140
141 if (IS_ERR(omap_host->pbias))
142 return 0;
143
144 if (power_on) {
145 ret = regulator_set_voltage(omap_host->pbias, iov, iov);
146 if (ret) {
147 dev_err(dev, "pbias set voltage failed\n");
148 return ret;
149 }
150
151 if (omap_host->pbias_enabled)
152 return 0;
153
154 ret = regulator_enable(omap_host->pbias);
155 if (ret) {
156 dev_err(dev, "pbias reg enable fail\n");
157 return ret;
158 }
159
160 omap_host->pbias_enabled = true;
161 } else {
162 if (!omap_host->pbias_enabled)
163 return 0;
164
165 ret = regulator_disable(omap_host->pbias);
166 if (ret) {
167 dev_err(dev, "pbias reg disable fail\n");
168 return ret;
169 }
170 omap_host->pbias_enabled = false;
171 }
172
173 return 0;
174}
175
176static int sdhci_omap_enable_iov(struct sdhci_omap_host *omap_host,
177 unsigned int iov)
178{
179 int ret;
180 struct sdhci_host *host = omap_host->host;
181 struct mmc_host *mmc = host->mmc;
182
183 ret = sdhci_omap_set_pbias(omap_host, false, 0);
184 if (ret)
185 return ret;
186
187 if (!IS_ERR(mmc->supply.vqmmc)) {
188 ret = regulator_set_voltage(mmc->supply.vqmmc, iov, iov);
189 if (ret) {
190 dev_err(mmc_dev(mmc), "vqmmc set voltage failed\n");
191 return ret;
192 }
193 }
194
195 ret = sdhci_omap_set_pbias(omap_host, true, iov);
196 if (ret)
197 return ret;
198
199 return 0;
200}
201
202static void sdhci_omap_conf_bus_power(struct sdhci_omap_host *omap_host,
203 unsigned char signal_voltage)
204{
205 u32 reg;
206 ktime_t timeout;
207
208 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL);
209 reg &= ~HCTL_SDVS_MASK;
210
211 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
212 reg |= HCTL_SDVS_33;
213 else
214 reg |= HCTL_SDVS_18;
215
216 sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
217
218 reg |= HCTL_SDBP;
219 sdhci_omap_writel(omap_host, SDHCI_OMAP_HCTL, reg);
220
221 /* wait 1ms */
222 timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
223 while (1) {
224 bool timedout = ktime_after(ktime_get(), timeout);
225
226 if (sdhci_omap_readl(omap_host, SDHCI_OMAP_HCTL) & HCTL_SDBP)
227 break;
228 if (WARN_ON(timedout))
229 return;
230 usleep_range(5, 10);
231 }
232}
233
234static void sdhci_omap_enable_sdio_irq(struct mmc_host *mmc, int enable)
235{
236 struct sdhci_host *host = mmc_priv(mmc);
237 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
238 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
239 u32 reg;
240
241 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
242 if (enable)
243 reg |= (CON_CTPL | CON_CLKEXTFREE);
244 else
245 reg &= ~(CON_CTPL | CON_CLKEXTFREE);
246 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
247
248 sdhci_enable_sdio_irq(mmc, enable);
249}
250
251static inline void sdhci_omap_set_dll(struct sdhci_omap_host *omap_host,
252 int count)
253{
254 int i;
255 u32 reg;
256
257 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
258 reg |= DLL_FORCE_VALUE;
259 reg &= ~DLL_FORCE_SR_C_MASK;
260 reg |= (count << DLL_FORCE_SR_C_SHIFT);
261 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
262
263 reg |= DLL_CALIB;
264 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
265 for (i = 0; i < 1000; i++) {
266 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
267 if (reg & DLL_CALIB)
268 break;
269 }
270 reg &= ~DLL_CALIB;
271 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
272}
273
274static void sdhci_omap_disable_tuning(struct sdhci_omap_host *omap_host)
275{
276 u32 reg;
277
278 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
279 reg &= ~AC12_SCLK_SEL;
280 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
281
282 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
283 reg &= ~(DLL_FORCE_VALUE | DLL_SWT);
284 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
285}
286
287static int sdhci_omap_execute_tuning(struct mmc_host *mmc, u32 opcode)
288{
289 struct sdhci_host *host = mmc_priv(mmc);
290 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
291 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
292 struct device *dev = omap_host->dev;
293 struct mmc_ios *ios = &mmc->ios;
294 u32 start_window = 0, max_window = 0;
295 bool dcrc_was_enabled = false;
296 u8 cur_match, prev_match = 0;
297 u32 length = 0, max_len = 0;
298 u32 phase_delay = 0;
299 int ret = 0;
300 u32 reg;
301
302 pltfm_host = sdhci_priv(host);
303 omap_host = sdhci_pltfm_priv(pltfm_host);
304 dev = omap_host->dev;
305
306 /* clock tuning is not needed for upto 52MHz */
307 if (ios->clock <= 52000000)
308 return 0;
309
310 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA2);
311 if (ios->timing == MMC_TIMING_UHS_SDR50 && !(reg & CAPA2_TSDR50))
312 return 0;
313
314 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_DLL);
315 reg |= DLL_SWT;
316 sdhci_omap_writel(omap_host, SDHCI_OMAP_DLL, reg);
317
318 /*
319 * OMAP5/DRA74X/DRA72x Errata i802:
320 * DCRC error interrupts (MMCHS_STAT[21] DCRC=0x1) can occur
321 * during the tuning procedure. So disable it during the
322 * tuning procedure.
323 */
324 if (host->ier & SDHCI_INT_DATA_CRC) {
325 host->ier &= ~SDHCI_INT_DATA_CRC;
326 dcrc_was_enabled = true;
327 }
328
329 while (phase_delay <= MAX_PHASE_DELAY) {
330 sdhci_omap_set_dll(omap_host, phase_delay);
331
332 cur_match = !mmc_send_tuning(mmc, opcode, NULL);
333 if (cur_match) {
334 if (prev_match) {
335 length++;
336 } else {
337 start_window = phase_delay;
338 length = 1;
339 }
340 }
341
342 if (length > max_len) {
343 max_window = start_window;
344 max_len = length;
345 }
346
347 prev_match = cur_match;
348 phase_delay += 4;
349 }
350
351 if (!max_len) {
352 dev_err(dev, "Unable to find match\n");
353 ret = -EIO;
354 goto tuning_error;
355 }
356
357 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
358 if (!(reg & AC12_SCLK_SEL)) {
359 ret = -EIO;
360 goto tuning_error;
361 }
362
363 phase_delay = max_window + 4 * (max_len >> 1);
364 sdhci_omap_set_dll(omap_host, phase_delay);
365
366 goto ret;
367
368tuning_error:
369 dev_err(dev, "Tuning failed\n");
370 sdhci_omap_disable_tuning(omap_host);
371
372ret:
373 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
374 /* Reenable forbidden interrupt */
375 if (dcrc_was_enabled)
376 host->ier |= SDHCI_INT_DATA_CRC;
377 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
378 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
379 return ret;
380}
381
382static int sdhci_omap_card_busy(struct mmc_host *mmc)
383{
384 u32 reg, ac12;
385 int ret = false;
386 struct sdhci_host *host = mmc_priv(mmc);
387 struct sdhci_pltfm_host *pltfm_host;
388 struct sdhci_omap_host *omap_host;
389 u32 ier = host->ier;
390
391 pltfm_host = sdhci_priv(host);
392 omap_host = sdhci_pltfm_priv(pltfm_host);
393
394 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
395 ac12 = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
396 reg &= ~CON_CLKEXTFREE;
397 if (ac12 & AC12_V1V8_SIGEN)
398 reg |= CON_CLKEXTFREE;
399 reg |= CON_PADEN;
400 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
401
402 disable_irq(host->irq);
403 ier |= SDHCI_INT_CARD_INT;
404 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
405 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
406
407 /*
408 * Delay is required for PSTATE to correctly reflect
409 * DLEV/CLEV values after PADEN is set.
410 */
411 usleep_range(50, 100);
412 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_PSTATE);
413 if ((reg & PSTATE_DATI) || !(reg & PSTATE_DLEV_DAT0))
414 ret = true;
415
416 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
417 reg &= ~(CON_CLKEXTFREE | CON_PADEN);
418 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
419
420 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
421 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
422 enable_irq(host->irq);
423
424 return ret;
425}
426
427static int sdhci_omap_start_signal_voltage_switch(struct mmc_host *mmc,
428 struct mmc_ios *ios)
429{
430 u32 reg;
431 int ret;
432 unsigned int iov;
433 struct sdhci_host *host = mmc_priv(mmc);
434 struct sdhci_pltfm_host *pltfm_host;
435 struct sdhci_omap_host *omap_host;
436 struct device *dev;
437
438 pltfm_host = sdhci_priv(host);
439 omap_host = sdhci_pltfm_priv(pltfm_host);
440 dev = omap_host->dev;
441
442 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
443 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
444 if (!(reg & CAPA_VS33))
445 return -EOPNOTSUPP;
446
447 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
448
449 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
450 reg &= ~AC12_V1V8_SIGEN;
451 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
452
453 iov = IOV_3V3;
454 } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
455 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
456 if (!(reg & CAPA_VS18))
457 return -EOPNOTSUPP;
458
459 sdhci_omap_conf_bus_power(omap_host, ios->signal_voltage);
460
461 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_AC12);
462 reg |= AC12_V1V8_SIGEN;
463 sdhci_omap_writel(omap_host, SDHCI_OMAP_AC12, reg);
464
465 iov = IOV_1V8;
466 } else {
467 return -EOPNOTSUPP;
468 }
469
470 ret = sdhci_omap_enable_iov(omap_host, iov);
471 if (ret) {
472 dev_err(dev, "failed to switch IO voltage to %dmV\n", iov);
473 return ret;
474 }
475
476 dev_dbg(dev, "IO voltage switched to %dmV\n", iov);
477 return 0;
478}
479
480static void sdhci_omap_set_timing(struct sdhci_omap_host *omap_host, u8 timing)
481{
482 int ret;
483 struct pinctrl_state *pinctrl_state;
484 struct device *dev = omap_host->dev;
485
486 if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
487 return;
488
489 if (omap_host->timing == timing)
490 return;
491
492 sdhci_omap_stop_clock(omap_host);
493
494 pinctrl_state = omap_host->pinctrl_state[timing];
495 ret = pinctrl_select_state(omap_host->pinctrl, pinctrl_state);
496 if (ret) {
497 dev_err(dev, "failed to select pinctrl state\n");
498 return;
499 }
500
501 sdhci_omap_start_clock(omap_host);
502 omap_host->timing = timing;
503}
504
505static void sdhci_omap_set_power_mode(struct sdhci_omap_host *omap_host,
506 u8 power_mode)
507{
508 if (omap_host->bus_mode == MMC_POWER_OFF)
509 sdhci_omap_disable_tuning(omap_host);
510 omap_host->power_mode = power_mode;
511}
512
513static void sdhci_omap_set_bus_mode(struct sdhci_omap_host *omap_host,
514 unsigned int mode)
515{
516 u32 reg;
517
518 if (omap_host->bus_mode == mode)
519 return;
520
521 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
522 if (mode == MMC_BUSMODE_OPENDRAIN)
523 reg |= CON_OD;
524 else
525 reg &= ~CON_OD;
526 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
527
528 omap_host->bus_mode = mode;
529}
530
531static void sdhci_omap_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
532{
533 struct sdhci_host *host = mmc_priv(mmc);
534 struct sdhci_pltfm_host *pltfm_host;
535 struct sdhci_omap_host *omap_host;
536
537 pltfm_host = sdhci_priv(host);
538 omap_host = sdhci_pltfm_priv(pltfm_host);
539
540 sdhci_omap_set_bus_mode(omap_host, ios->bus_mode);
541 sdhci_omap_set_timing(omap_host, ios->timing);
542 sdhci_set_ios(mmc, ios);
543 sdhci_omap_set_power_mode(omap_host, ios->power_mode);
544}
545
546static u16 sdhci_omap_calc_divisor(struct sdhci_pltfm_host *host,
547 unsigned int clock)
548{
549 u16 dsor;
550
551 dsor = DIV_ROUND_UP(clk_get_rate(host->clk), clock);
552 if (dsor > SYSCTL_CLKD_MAX)
553 dsor = SYSCTL_CLKD_MAX;
554
555 return dsor;
556}
557
558static void sdhci_omap_start_clock(struct sdhci_omap_host *omap_host)
559{
560 u32 reg;
561
562 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
563 reg |= SYSCTL_CEN;
564 sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
565}
566
567static void sdhci_omap_stop_clock(struct sdhci_omap_host *omap_host)
568{
569 u32 reg;
570
571 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_SYSCTL);
572 reg &= ~SYSCTL_CEN;
573 sdhci_omap_writel(omap_host, SDHCI_OMAP_SYSCTL, reg);
574}
575
576static void sdhci_omap_set_clock(struct sdhci_host *host, unsigned int clock)
577{
578 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
579 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
580 unsigned long clkdiv;
581
582 sdhci_omap_stop_clock(omap_host);
583
584 if (!clock)
585 return;
586
587 clkdiv = sdhci_omap_calc_divisor(pltfm_host, clock);
588 clkdiv = (clkdiv & SYSCTL_CLKD_MASK) << SYSCTL_CLKD_SHIFT;
589 sdhci_enable_clk(host, clkdiv);
590
591 sdhci_omap_start_clock(omap_host);
592}
593
594static void sdhci_omap_set_power(struct sdhci_host *host, unsigned char mode,
595 unsigned short vdd)
596{
597 struct mmc_host *mmc = host->mmc;
598
599 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
600}
601
602static int sdhci_omap_enable_dma(struct sdhci_host *host)
603{
604 u32 reg;
605 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
606 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
607
608 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
609 reg |= CON_DMA_MASTER;
610 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
611
612 return 0;
613}
614
615static unsigned int sdhci_omap_get_min_clock(struct sdhci_host *host)
616{
617 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
618
619 return clk_get_rate(pltfm_host->clk) / SYSCTL_CLKD_MAX;
620}
621
622static void sdhci_omap_set_bus_width(struct sdhci_host *host, int width)
623{
624 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
625 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
626 u32 reg;
627
628 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
629 if (width == MMC_BUS_WIDTH_8)
630 reg |= CON_DW8;
631 else
632 reg &= ~CON_DW8;
633 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
634
635 sdhci_set_bus_width(host, width);
636}
637
638static void sdhci_omap_init_74_clocks(struct sdhci_host *host, u8 power_mode)
639{
640 u32 reg;
641 ktime_t timeout;
642 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
643 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
644
645 if (omap_host->power_mode == power_mode)
646 return;
647
648 if (power_mode != MMC_POWER_ON)
649 return;
650
651 disable_irq(host->irq);
652
653 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
654 reg |= CON_INIT;
655 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
656 sdhci_omap_writel(omap_host, SDHCI_OMAP_CMD, 0x0);
657
658 /* wait 1ms */
659 timeout = ktime_add_ms(ktime_get(), SDHCI_OMAP_TIMEOUT);
660 while (1) {
661 bool timedout = ktime_after(ktime_get(), timeout);
662
663 if (sdhci_omap_readl(omap_host, SDHCI_OMAP_STAT) & INT_CC_EN)
664 break;
665 if (WARN_ON(timedout))
666 return;
667 usleep_range(5, 10);
668 }
669
670 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
671 reg &= ~CON_INIT;
672 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
673 sdhci_omap_writel(omap_host, SDHCI_OMAP_STAT, INT_CC_EN);
674
675 enable_irq(host->irq);
676}
677
678static void sdhci_omap_set_uhs_signaling(struct sdhci_host *host,
679 unsigned int timing)
680{
681 u32 reg;
682 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
683 struct sdhci_omap_host *omap_host = sdhci_pltfm_priv(pltfm_host);
684
685 sdhci_omap_stop_clock(omap_host);
686
687 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CON);
688 if (timing == MMC_TIMING_UHS_DDR50 || timing == MMC_TIMING_MMC_DDR52)
689 reg |= CON_DDR;
690 else
691 reg &= ~CON_DDR;
692 sdhci_omap_writel(omap_host, SDHCI_OMAP_CON, reg);
693
694 sdhci_set_uhs_signaling(host, timing);
695 sdhci_omap_start_clock(omap_host);
696}
697
698static struct sdhci_ops sdhci_omap_ops = {
699 .set_clock = sdhci_omap_set_clock,
700 .set_power = sdhci_omap_set_power,
701 .enable_dma = sdhci_omap_enable_dma,
702 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
703 .get_min_clock = sdhci_omap_get_min_clock,
704 .set_bus_width = sdhci_omap_set_bus_width,
705 .platform_send_init_74_clocks = sdhci_omap_init_74_clocks,
706 .reset = sdhci_reset,
707 .set_uhs_signaling = sdhci_omap_set_uhs_signaling,
708};
709
710static int sdhci_omap_set_capabilities(struct sdhci_omap_host *omap_host)
711{
712 u32 reg;
713 int ret = 0;
714 struct device *dev = omap_host->dev;
715 struct regulator *vqmmc;
716
717 vqmmc = regulator_get(dev, "vqmmc");
718 if (IS_ERR(vqmmc)) {
719 ret = PTR_ERR(vqmmc);
720 goto reg_put;
721 }
722
723 /* voltage capabilities might be set by boot loader, clear it */
724 reg = sdhci_omap_readl(omap_host, SDHCI_OMAP_CAPA);
725 reg &= ~(CAPA_VS18 | CAPA_VS30 | CAPA_VS33);
726
727 if (regulator_is_supported_voltage(vqmmc, IOV_3V3, IOV_3V3))
728 reg |= CAPA_VS33;
729 if (regulator_is_supported_voltage(vqmmc, IOV_1V8, IOV_1V8))
730 reg |= CAPA_VS18;
731
732 sdhci_omap_writel(omap_host, SDHCI_OMAP_CAPA, reg);
733
734reg_put:
735 regulator_put(vqmmc);
736
737 return ret;
738}
739
740static const struct sdhci_pltfm_data sdhci_omap_pdata = {
741 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
742 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
743 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
744 SDHCI_QUIRK_NO_HISPD_BIT |
745 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC,
746 .quirks2 = SDHCI_QUIRK2_ACMD23_BROKEN |
747 SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
748 SDHCI_QUIRK2_RSP_136_HAS_CRC |
749 SDHCI_QUIRK2_DISABLE_HW_TIMEOUT,
750 .ops = &sdhci_omap_ops,
751};
752
753static const struct sdhci_omap_data k2g_data = {
754 .offset = 0x200,
755};
756
757static const struct sdhci_omap_data dra7_data = {
758 .offset = 0x200,
759 .flags = SDHCI_OMAP_REQUIRE_IODELAY,
760};
761
762static const struct of_device_id omap_sdhci_match[] = {
763 { .compatible = "ti,dra7-sdhci", .data = &dra7_data },
764 { .compatible = "ti,k2g-sdhci", .data = &k2g_data },
765 {},
766};
767MODULE_DEVICE_TABLE(of, omap_sdhci_match);
768
769static struct pinctrl_state
770*sdhci_omap_iodelay_pinctrl_state(struct sdhci_omap_host *omap_host, char *mode,
771 u32 *caps, u32 capmask)
772{
773 struct device *dev = omap_host->dev;
774 char *version = omap_host->version;
775 struct pinctrl_state *pinctrl_state = ERR_PTR(-ENODEV);
776 char str[20];
777
778 if (!(*caps & capmask))
779 goto ret;
780
781 if (version) {
782 snprintf(str, 20, "%s-%s", mode, version);
783 pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, str);
784 }
785
786 if (IS_ERR(pinctrl_state))
787 pinctrl_state = pinctrl_lookup_state(omap_host->pinctrl, mode);
788
789 if (IS_ERR(pinctrl_state)) {
790 dev_err(dev, "no pinctrl state for %s mode", mode);
791 *caps &= ~capmask;
792 }
793
794ret:
795 return pinctrl_state;
796}
797
798static int sdhci_omap_config_iodelay_pinctrl_state(struct sdhci_omap_host
799 *omap_host)
800{
801 struct device *dev = omap_host->dev;
802 struct sdhci_host *host = omap_host->host;
803 struct mmc_host *mmc = host->mmc;
804 u32 *caps = &mmc->caps;
805 u32 *caps2 = &mmc->caps2;
806 struct pinctrl_state *state;
807 struct pinctrl_state **pinctrl_state;
808
809 if (!(omap_host->flags & SDHCI_OMAP_REQUIRE_IODELAY))
810 return 0;
811
812 pinctrl_state = devm_kcalloc(dev,
813 MMC_TIMING_MMC_HS200 + 1,
814 sizeof(*pinctrl_state),
815 GFP_KERNEL);
816 if (!pinctrl_state)
817 return -ENOMEM;
818
819 omap_host->pinctrl = devm_pinctrl_get(omap_host->dev);
820 if (IS_ERR(omap_host->pinctrl)) {
821 dev_err(dev, "Cannot get pinctrl\n");
822 return PTR_ERR(omap_host->pinctrl);
823 }
824
825 state = pinctrl_lookup_state(omap_host->pinctrl, "default");
826 if (IS_ERR(state)) {
827 dev_err(dev, "no pinctrl state for default mode\n");
828 return PTR_ERR(state);
829 }
830 pinctrl_state[MMC_TIMING_LEGACY] = state;
831
832 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr104", caps,
833 MMC_CAP_UHS_SDR104);
834 if (!IS_ERR(state))
835 pinctrl_state[MMC_TIMING_UHS_SDR104] = state;
836
837 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr50", caps,
838 MMC_CAP_UHS_DDR50);
839 if (!IS_ERR(state))
840 pinctrl_state[MMC_TIMING_UHS_DDR50] = state;
841
842 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr50", caps,
843 MMC_CAP_UHS_SDR50);
844 if (!IS_ERR(state))
845 pinctrl_state[MMC_TIMING_UHS_SDR50] = state;
846
847 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr25", caps,
848 MMC_CAP_UHS_SDR25);
849 if (!IS_ERR(state))
850 pinctrl_state[MMC_TIMING_UHS_SDR25] = state;
851
852 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "sdr12", caps,
853 MMC_CAP_UHS_SDR12);
854 if (!IS_ERR(state))
855 pinctrl_state[MMC_TIMING_UHS_SDR12] = state;
856
857 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_1_8v", caps,
858 MMC_CAP_1_8V_DDR);
859 if (!IS_ERR(state)) {
860 pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
861 } else {
862 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "ddr_3_3v",
863 caps,
864 MMC_CAP_3_3V_DDR);
865 if (!IS_ERR(state))
866 pinctrl_state[MMC_TIMING_MMC_DDR52] = state;
867 }
868
869 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
870 MMC_CAP_SD_HIGHSPEED);
871 if (!IS_ERR(state))
872 pinctrl_state[MMC_TIMING_SD_HS] = state;
873
874 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs", caps,
875 MMC_CAP_MMC_HIGHSPEED);
876 if (!IS_ERR(state))
877 pinctrl_state[MMC_TIMING_MMC_HS] = state;
878
879 state = sdhci_omap_iodelay_pinctrl_state(omap_host, "hs200_1_8v", caps2,
880 MMC_CAP2_HS200_1_8V_SDR);
881 if (!IS_ERR(state))
882 pinctrl_state[MMC_TIMING_MMC_HS200] = state;
883
884 omap_host->pinctrl_state = pinctrl_state;
885
886 return 0;
887}
888
889static const struct soc_device_attribute sdhci_omap_soc_devices[] = {
890 {
891 .machine = "DRA7[45]*",
892 .revision = "ES1.[01]",
893 },
894 {
895 /* sentinel */
896 }
897};
898
899static int sdhci_omap_probe(struct platform_device *pdev)
900{
901 int ret;
902 u32 offset;
903 struct device *dev = &pdev->dev;
904 struct sdhci_host *host;
905 struct sdhci_pltfm_host *pltfm_host;
906 struct sdhci_omap_host *omap_host;
907 struct mmc_host *mmc;
908 const struct of_device_id *match;
909 struct sdhci_omap_data *data;
910 const struct soc_device_attribute *soc;
911
912 match = of_match_device(omap_sdhci_match, dev);
913 if (!match)
914 return -EINVAL;
915
916 data = (struct sdhci_omap_data *)match->data;
917 if (!data) {
918 dev_err(dev, "no sdhci omap data\n");
919 return -EINVAL;
920 }
921 offset = data->offset;
922
923 host = sdhci_pltfm_init(pdev, &sdhci_omap_pdata,
924 sizeof(*omap_host));
925 if (IS_ERR(host)) {
926 dev_err(dev, "Failed sdhci_pltfm_init\n");
927 return PTR_ERR(host);
928 }
929
930 pltfm_host = sdhci_priv(host);
931 omap_host = sdhci_pltfm_priv(pltfm_host);
932 omap_host->host = host;
933 omap_host->base = host->ioaddr;
934 omap_host->dev = dev;
935 omap_host->power_mode = MMC_POWER_UNDEFINED;
936 omap_host->timing = MMC_TIMING_LEGACY;
937 omap_host->flags = data->flags;
938 host->ioaddr += offset;
939
940 mmc = host->mmc;
941 sdhci_get_of_property(pdev);
942 ret = mmc_of_parse(mmc);
943 if (ret)
944 goto err_pltfm_free;
945
946 soc = soc_device_match(sdhci_omap_soc_devices);
947 if (soc) {
948 omap_host->version = "rev11";
949 if (!strcmp(dev_name(dev), "4809c000.mmc"))
950 mmc->f_max = 96000000;
951 if (!strcmp(dev_name(dev), "480b4000.mmc"))
952 mmc->f_max = 48000000;
953 if (!strcmp(dev_name(dev), "480ad000.mmc"))
954 mmc->f_max = 48000000;
955 }
956
957 pltfm_host->clk = devm_clk_get(dev, "fck");
958 if (IS_ERR(pltfm_host->clk)) {
959 ret = PTR_ERR(pltfm_host->clk);
960 goto err_pltfm_free;
961 }
962
963 ret = clk_set_rate(pltfm_host->clk, mmc->f_max);
964 if (ret) {
965 dev_err(dev, "failed to set clock to %d\n", mmc->f_max);
966 goto err_pltfm_free;
967 }
968
969 omap_host->pbias = devm_regulator_get_optional(dev, "pbias");
970 if (IS_ERR(omap_host->pbias)) {
971 ret = PTR_ERR(omap_host->pbias);
972 if (ret != -ENODEV)
973 goto err_pltfm_free;
974 dev_dbg(dev, "unable to get pbias regulator %d\n", ret);
975 }
976 omap_host->pbias_enabled = false;
977
978 /*
979 * omap_device_pm_domain has callbacks to enable the main
980 * functional clock, interface clock and also configure the
981 * SYSCONFIG register of omap devices. The callback will be invoked
982 * as part of pm_runtime_get_sync.
983 */
984 pm_runtime_enable(dev);
985 ret = pm_runtime_get_sync(dev);
986 if (ret < 0) {
987 dev_err(dev, "pm_runtime_get_sync failed\n");
988 pm_runtime_put_noidle(dev);
989 goto err_rpm_disable;
990 }
991
992 ret = sdhci_omap_set_capabilities(omap_host);
993 if (ret) {
994 dev_err(dev, "failed to set system capabilities\n");
995 goto err_put_sync;
996 }
997
998 host->mmc_host_ops.get_ro = mmc_gpio_get_ro;
999 host->mmc_host_ops.start_signal_voltage_switch =
1000 sdhci_omap_start_signal_voltage_switch;
1001 host->mmc_host_ops.set_ios = sdhci_omap_set_ios;
1002 host->mmc_host_ops.card_busy = sdhci_omap_card_busy;
1003 host->mmc_host_ops.execute_tuning = sdhci_omap_execute_tuning;
1004 host->mmc_host_ops.enable_sdio_irq = sdhci_omap_enable_sdio_irq;
1005
1006 ret = sdhci_setup_host(host);
1007 if (ret)
1008 goto err_put_sync;
1009
1010 ret = sdhci_omap_config_iodelay_pinctrl_state(omap_host);
1011 if (ret)
1012 goto err_cleanup_host;
1013
1014 ret = __sdhci_add_host(host);
1015 if (ret)
1016 goto err_cleanup_host;
1017
1018 return 0;
1019
1020err_cleanup_host:
1021 sdhci_cleanup_host(host);
1022
1023err_put_sync:
1024 pm_runtime_put_sync(dev);
1025
1026err_rpm_disable:
1027 pm_runtime_disable(dev);
1028
1029err_pltfm_free:
1030 sdhci_pltfm_free(pdev);
1031 return ret;
1032}
1033
1034static int sdhci_omap_remove(struct platform_device *pdev)
1035{
1036 struct device *dev = &pdev->dev;
1037 struct sdhci_host *host = platform_get_drvdata(pdev);
1038
1039 sdhci_remove_host(host, true);
1040 pm_runtime_put_sync(dev);
1041 pm_runtime_disable(dev);
1042 sdhci_pltfm_free(pdev);
1043
1044 return 0;
1045}
1046
1047static struct platform_driver sdhci_omap_driver = {
1048 .probe = sdhci_omap_probe,
1049 .remove = sdhci_omap_remove,
1050 .driver = {
1051 .name = "sdhci-omap",
1052 .of_match_table = omap_sdhci_match,
1053 },
1054};
1055
1056module_platform_driver(sdhci_omap_driver);
1057
1058MODULE_DESCRIPTION("SDHCI driver for OMAP SoCs");
1059MODULE_AUTHOR("Texas Instruments Inc.");
1060MODULE_LICENSE("GPL v2");
1061MODULE_ALIAS("platform:sdhci_omap");