blob: 32321bd596d880027358db10e9eb5f5b45957c1d [file] [log] [blame]
Adrian Hunterc4e05032012-11-23 21:17:34 +01001/*
2 * Secure Digital Host Controller Interface ACPI driver.
3 *
4 * Copyright (c) 2012, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <linux/init.h>
22#include <linux/export.h>
23#include <linux/module.h>
24#include <linux/device.h>
25#include <linux/platform_device.h>
26#include <linux/ioport.h>
27#include <linux/io.h>
28#include <linux/dma-mapping.h>
29#include <linux/compiler.h>
30#include <linux/stddef.h>
31#include <linux/bitops.h>
32#include <linux/types.h>
33#include <linux/err.h>
34#include <linux/interrupt.h>
35#include <linux/acpi.h>
36#include <linux/pm.h>
37#include <linux/pm_runtime.h>
Adrian Hunterb04fa062013-06-13 11:50:27 +030038#include <linux/delay.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010039
40#include <linux/mmc/host.h>
41#include <linux/mmc/pm.h>
Adrian Hunter4fd44092014-03-10 15:02:42 +020042#include <linux/mmc/slot-gpio.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010043
Adrian Hunter6e1c7d62016-04-15 14:06:57 +030044#ifdef CONFIG_X86
45#include <asm/cpu_device_id.h>
Dave Hansen8ba4cb52016-06-02 17:19:51 -070046#include <asm/intel-family.h>
Adrian Hunter6e1c7d62016-04-15 14:06:57 +030047#include <asm/iosf_mbi.h>
Adrian Hunter17753d12017-06-21 15:08:39 +030048#include <linux/pci.h>
Adrian Hunter6e1c7d62016-04-15 14:06:57 +030049#endif
50
Adrian Hunterc4e05032012-11-23 21:17:34 +010051#include "sdhci.h"
52
53enum {
Adrian Hunter4fd44092014-03-10 15:02:42 +020054 SDHCI_ACPI_SD_CD = BIT(0),
55 SDHCI_ACPI_RUNTIME_PM = BIT(1),
56 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
Adrian Hunterc4e05032012-11-23 21:17:34 +010057};
58
59struct sdhci_acpi_chip {
60 const struct sdhci_ops *ops;
61 unsigned int quirks;
62 unsigned int quirks2;
63 unsigned long caps;
64 unsigned int caps2;
65 mmc_pm_flag_t pm_caps;
66};
67
68struct sdhci_acpi_slot {
69 const struct sdhci_acpi_chip *chip;
70 unsigned int quirks;
71 unsigned int quirks2;
72 unsigned long caps;
73 unsigned int caps2;
74 mmc_pm_flag_t pm_caps;
75 unsigned int flags;
Adrian Hunterf07b7952017-10-19 13:41:45 +030076 size_t priv_size;
Adrian Hunter7dafca82014-10-06 15:23:06 +030077 int (*probe_slot)(struct platform_device *, const char *, const char *);
Gao, Yunpeng578b36b2014-09-01 11:35:40 +080078 int (*remove_slot)(struct platform_device *);
Adrian Hunter0cc1a0f2017-12-08 15:04:58 +020079 int (*setup_host)(struct platform_device *pdev);
Adrian Hunterc4e05032012-11-23 21:17:34 +010080};
81
82struct sdhci_acpi_host {
83 struct sdhci_host *host;
84 const struct sdhci_acpi_slot *slot;
85 struct platform_device *pdev;
86 bool use_runtime_pm;
Adrian Hunterf07b7952017-10-19 13:41:45 +030087 unsigned long private[0] ____cacheline_aligned;
Adrian Hunterc4e05032012-11-23 21:17:34 +010088};
89
Adrian Hunterf07b7952017-10-19 13:41:45 +030090static inline void *sdhci_acpi_priv(struct sdhci_acpi_host *c)
91{
92 return (void *)c->private;
93}
94
Adrian Hunterc4e05032012-11-23 21:17:34 +010095static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
96{
97 return c->slot && (c->slot->flags & flag);
98}
99
Adrian Hunter0acccf42017-12-08 15:08:18 +0200100#define INTEL_DSM_HS_CAPS_SDR25 BIT(0)
101#define INTEL_DSM_HS_CAPS_DDR50 BIT(1)
102#define INTEL_DSM_HS_CAPS_SDR50 BIT(2)
103#define INTEL_DSM_HS_CAPS_SDR104 BIT(3)
104
Adrian Hunter1c451c12017-10-19 13:41:46 +0300105enum {
106 INTEL_DSM_FNS = 0,
107 INTEL_DSM_V18_SWITCH = 3,
108 INTEL_DSM_V33_SWITCH = 4,
Adrian Hunter0acccf42017-12-08 15:08:18 +0200109 INTEL_DSM_HS_CAPS = 8,
Adrian Hunter1c451c12017-10-19 13:41:46 +0300110};
111
112struct intel_host {
113 u32 dsm_fns;
Adrian Hunter0acccf42017-12-08 15:08:18 +0200114 u32 hs_caps;
Adrian Hunter1c451c12017-10-19 13:41:46 +0300115};
116
117static const guid_t intel_dsm_guid =
118 GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
119 0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
120
121static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
122 unsigned int fn, u32 *result)
123{
124 union acpi_object *obj;
125 int err = 0;
126
127 obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
128 if (!obj)
129 return -EOPNOTSUPP;
130
131 if (obj->type == ACPI_TYPE_INTEGER) {
132 *result = obj->integer.value;
133 } else if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length > 0) {
134 size_t len = min_t(size_t, obj->buffer.length, 4);
135
136 *result = 0;
137 memcpy(result, obj->buffer.pointer, len);
138 } else {
139 dev_err(dev, "%s DSM fn %u obj->type %d obj->buffer.length %d\n",
140 __func__, fn, obj->type, obj->buffer.length);
141 err = -EINVAL;
142 }
143
144 ACPI_FREE(obj);
145
146 return err;
147}
148
149static int intel_dsm(struct intel_host *intel_host, struct device *dev,
150 unsigned int fn, u32 *result)
151{
152 if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
153 return -EOPNOTSUPP;
154
155 return __intel_dsm(intel_host, dev, fn, result);
156}
157
158static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
159 struct mmc_host *mmc)
160{
161 int err;
162
Adrian Hunter0acccf42017-12-08 15:08:18 +0200163 intel_host->hs_caps = ~0;
164
Adrian Hunter1c451c12017-10-19 13:41:46 +0300165 err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
166 if (err) {
167 pr_debug("%s: DSM not supported, error %d\n",
168 mmc_hostname(mmc), err);
169 return;
170 }
171
172 pr_debug("%s: DSM function mask %#x\n",
173 mmc_hostname(mmc), intel_host->dsm_fns);
Adrian Hunter0acccf42017-12-08 15:08:18 +0200174
175 intel_dsm(intel_host, dev, INTEL_DSM_HS_CAPS, &intel_host->hs_caps);
Adrian Hunter1c451c12017-10-19 13:41:46 +0300176}
177
178static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
179 struct mmc_ios *ios)
180{
181 struct device *dev = mmc_dev(mmc);
182 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
183 struct intel_host *intel_host = sdhci_acpi_priv(c);
184 unsigned int fn;
185 u32 result = 0;
186 int err;
187
188 err = sdhci_start_signal_voltage_switch(mmc, ios);
189 if (err)
190 return err;
191
192 switch (ios->signal_voltage) {
193 case MMC_SIGNAL_VOLTAGE_330:
194 fn = INTEL_DSM_V33_SWITCH;
195 break;
196 case MMC_SIGNAL_VOLTAGE_180:
197 fn = INTEL_DSM_V18_SWITCH;
198 break;
199 default:
200 return 0;
201 }
202
203 err = intel_dsm(intel_host, dev, fn, &result);
204 pr_debug("%s: %s DSM fn %u error %d result %u\n",
205 mmc_hostname(mmc), __func__, fn, err, result);
206
207 return 0;
208}
209
Adrian Hunterb04fa062013-06-13 11:50:27 +0300210static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
211{
212 u8 reg;
213
214 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
215 reg |= 0x10;
216 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
217 /* For eMMC, minimum is 1us but give it 9us for good measure */
218 udelay(9);
219 reg &= ~0x10;
220 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
221 /* For eMMC, minimum is 200us but give it 300us for good measure */
222 usleep_range(300, 1000);
223}
224
Adrian Hunterc4e05032012-11-23 21:17:34 +0100225static const struct sdhci_ops sdhci_acpi_ops_dflt = {
Russell King17710592014-04-25 12:58:55 +0100226 .set_clock = sdhci_set_clock,
Russell King2317f562014-04-25 12:57:07 +0100227 .set_bus_width = sdhci_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +0100228 .reset = sdhci_reset,
Russell King96d7b782014-04-25 12:59:26 +0100229 .set_uhs_signaling = sdhci_set_uhs_signaling,
Adrian Hunterc4e05032012-11-23 21:17:34 +0100230};
231
Adrian Hunterb04fa062013-06-13 11:50:27 +0300232static const struct sdhci_ops sdhci_acpi_ops_int = {
Russell King17710592014-04-25 12:58:55 +0100233 .set_clock = sdhci_set_clock,
Russell King2317f562014-04-25 12:57:07 +0100234 .set_bus_width = sdhci_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +0100235 .reset = sdhci_reset,
Russell King96d7b782014-04-25 12:59:26 +0100236 .set_uhs_signaling = sdhci_set_uhs_signaling,
Adrian Hunterb04fa062013-06-13 11:50:27 +0300237 .hw_reset = sdhci_acpi_int_hw_reset,
238};
239
240static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
241 .ops = &sdhci_acpi_ops_int,
242};
243
Adrian Hunter6e1c7d62016-04-15 14:06:57 +0300244#ifdef CONFIG_X86
245
246static bool sdhci_acpi_byt(void)
247{
248 static const struct x86_cpu_id byt[] = {
Dave Hansen8ba4cb52016-06-02 17:19:51 -0700249 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 },
Adrian Hunter6e1c7d62016-04-15 14:06:57 +0300250 {}
251 };
252
253 return x86_match_cpu(byt);
254}
255
Adrian Hunter17753d12017-06-21 15:08:39 +0300256static bool sdhci_acpi_cht(void)
257{
258 static const struct x86_cpu_id cht[] = {
259 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT },
260 {}
261 };
262
263 return x86_match_cpu(cht);
264}
265
Adrian Hunter6e1c7d62016-04-15 14:06:57 +0300266#define BYT_IOSF_SCCEP 0x63
267#define BYT_IOSF_OCP_NETCTRL0 0x1078
268#define BYT_IOSF_OCP_TIMEOUT_BASE GENMASK(10, 8)
269
270static void sdhci_acpi_byt_setting(struct device *dev)
271{
272 u32 val = 0;
273
274 if (!sdhci_acpi_byt())
275 return;
276
277 if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0,
278 &val)) {
279 dev_err(dev, "%s read error\n", __func__);
280 return;
281 }
282
283 if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE))
284 return;
285
286 val &= ~BYT_IOSF_OCP_TIMEOUT_BASE;
287
288 if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0,
289 val)) {
290 dev_err(dev, "%s write error\n", __func__);
291 return;
292 }
293
294 dev_dbg(dev, "%s completed\n", __func__);
295}
296
297static bool sdhci_acpi_byt_defer(struct device *dev)
298{
299 if (!sdhci_acpi_byt())
300 return false;
301
302 if (!iosf_mbi_available())
303 return true;
304
305 sdhci_acpi_byt_setting(dev);
306
307 return false;
308}
309
Adrian Hunter17753d12017-06-21 15:08:39 +0300310static bool sdhci_acpi_cht_pci_wifi(unsigned int vendor, unsigned int device,
311 unsigned int slot, unsigned int parent_slot)
312{
313 struct pci_dev *dev, *parent, *from = NULL;
314
315 while (1) {
316 dev = pci_get_device(vendor, device, from);
317 pci_dev_put(from);
318 if (!dev)
319 break;
320 parent = pci_upstream_bridge(dev);
321 if (ACPI_COMPANION(&dev->dev) && PCI_SLOT(dev->devfn) == slot &&
322 parent && PCI_SLOT(parent->devfn) == parent_slot &&
323 !pci_upstream_bridge(parent)) {
324 pci_dev_put(dev);
325 return true;
326 }
327 from = dev;
328 }
329
330 return false;
331}
332
333/*
334 * GPDwin uses PCI wifi which conflicts with SDIO's use of
335 * acpi_device_fix_up_power() on child device nodes. Identifying GPDwin is
336 * problematic, but since SDIO is only used for wifi, the presence of the PCI
337 * wifi card in the expected slot with an ACPI companion node, is used to
338 * indicate that acpi_device_fix_up_power() should be avoided.
339 */
340static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
341 const char *uid)
342{
343 return sdhci_acpi_cht() &&
344 !strcmp(hid, "80860F14") &&
345 !strcmp(uid, "2") &&
346 sdhci_acpi_cht_pci_wifi(0x14e4, 0x43ec, 0, 28);
347}
348
Adrian Hunter6e1c7d62016-04-15 14:06:57 +0300349#else
350
351static inline void sdhci_acpi_byt_setting(struct device *dev)
352{
353}
354
355static inline bool sdhci_acpi_byt_defer(struct device *dev)
356{
357 return false;
358}
359
Adrian Hunter17753d12017-06-21 15:08:39 +0300360static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
361 const char *uid)
362{
363 return false;
364}
365
Adrian Hunter6e1c7d62016-04-15 14:06:57 +0300366#endif
367
Adrian Hunter6a645dd2016-02-09 16:12:38 +0200368static int bxt_get_cd(struct mmc_host *mmc)
369{
370 int gpio_cd = mmc_gpio_get_cd(mmc);
371 struct sdhci_host *host = mmc_priv(mmc);
372 unsigned long flags;
373 int ret = 0;
374
375 if (!gpio_cd)
376 return 0;
377
Adrian Hunter6a645dd2016-02-09 16:12:38 +0200378 spin_lock_irqsave(&host->lock, flags);
379
380 if (host->flags & SDHCI_DEVICE_DEAD)
381 goto out;
382
383 ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
384out:
385 spin_unlock_irqrestore(&host->lock, flags);
386
Adrian Hunter6a645dd2016-02-09 16:12:38 +0200387 return ret;
388}
389
Adrian Hunter159cd322017-10-19 13:41:44 +0300390static int intel_probe_slot(struct platform_device *pdev, const char *hid,
391 const char *uid)
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800392{
393 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
Adrian Hunter1c451c12017-10-19 13:41:46 +0300394 struct intel_host *intel_host = sdhci_acpi_priv(c);
Adrian Hunter159cd322017-10-19 13:41:44 +0300395 struct sdhci_host *host = c->host;
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800396
Adrian Hunter80243792014-10-06 15:23:07 +0300397 if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
398 sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
399 sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
400 host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
401
Azhar Shaikhd3e97402017-03-29 11:16:32 -0700402 if (hid && !strcmp(hid, "80865ACA"))
Adrian Hunter6a645dd2016-02-09 16:12:38 +0200403 host->mmc_host_ops.get_cd = bxt_get_cd;
404
Adrian Hunter1c451c12017-10-19 13:41:46 +0300405 intel_dsm_init(intel_host, &pdev->dev, host->mmc);
406
407 host->mmc_host_ops.start_signal_voltage_switch =
408 intel_start_signal_voltage_switch;
409
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800410 return 0;
411}
412
Adrian Hunter0acccf42017-12-08 15:08:18 +0200413static int intel_setup_host(struct platform_device *pdev)
414{
415 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
416 struct intel_host *intel_host = sdhci_acpi_priv(c);
417
418 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR25))
419 c->host->mmc->caps &= ~MMC_CAP_UHS_SDR25;
420
421 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR50))
422 c->host->mmc->caps &= ~MMC_CAP_UHS_SDR50;
423
424 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_DDR50))
425 c->host->mmc->caps &= ~MMC_CAP_UHS_DDR50;
426
427 if (!(intel_host->hs_caps & INTEL_DSM_HS_CAPS_SDR104))
428 c->host->mmc->caps &= ~MMC_CAP_UHS_SDR104;
429
430 return 0;
431}
432
Adrian Hunter07a58882013-04-26 11:27:22 +0300433static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
Adrian Hunterb04fa062013-06-13 11:50:27 +0300434 .chip = &sdhci_acpi_chip_int,
Maurice Petallof25c3372014-07-08 19:11:01 +0800435 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
Adrian Hunter9d65cb82014-12-01 15:51:19 +0200436 MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
Adrian Hunterc80f2752016-08-16 13:44:15 +0300437 MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY,
Adrian Hunter07a58882013-04-26 11:27:22 +0300438 .flags = SDHCI_ACPI_RUNTIME_PM,
Adrian Huntere1f56332014-12-01 15:51:17 +0200439 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Huntere839b132015-10-21 11:15:46 +0300440 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
441 SDHCI_QUIRK2_STOP_WITH_TC |
442 SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
Adrian Hunter159cd322017-10-19 13:41:44 +0300443 .probe_slot = intel_probe_slot,
Adrian Hunter0acccf42017-12-08 15:08:18 +0200444 .setup_host = intel_setup_host,
Adrian Hunter1c451c12017-10-19 13:41:46 +0300445 .priv_size = sizeof(struct intel_host),
Adrian Hunter07a58882013-04-26 11:27:22 +0300446};
447
Adrian Huntere5571392012-12-10 21:18:48 +0100448static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
Adrian Huntere1f56332014-12-01 15:51:17 +0200449 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
450 SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Huntere5571392012-12-10 21:18:48 +0100451 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
Adrian Hunter9d65cb82014-12-01 15:51:19 +0200452 .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
Adrian Hunter265984b2016-05-20 10:33:48 +0300453 MMC_CAP_WAIT_WHILE_BUSY,
Adrian Huntere5571392012-12-10 21:18:48 +0100454 .flags = SDHCI_ACPI_RUNTIME_PM,
455 .pm_caps = MMC_PM_KEEP_POWER,
Adrian Hunter159cd322017-10-19 13:41:44 +0300456 .probe_slot = intel_probe_slot,
Adrian Hunter0acccf42017-12-08 15:08:18 +0200457 .setup_host = intel_setup_host,
Adrian Hunter1c451c12017-10-19 13:41:46 +0300458 .priv_size = sizeof(struct intel_host),
Adrian Huntere5571392012-12-10 21:18:48 +0100459};
460
Adrian Hunter07a58882013-04-26 11:27:22 +0300461static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
Adrian Hunter4fd44092014-03-10 15:02:42 +0200462 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
463 SDHCI_ACPI_RUNTIME_PM,
Adrian Huntere1f56332014-12-01 15:51:17 +0200464 .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
Adrian Hunter934e31b2014-09-24 10:27:28 +0300465 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
466 SDHCI_QUIRK2_STOP_WITH_TC,
Azhar Shaikhd3e97402017-03-29 11:16:32 -0700467 .caps = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM,
Adrian Hunter159cd322017-10-19 13:41:44 +0300468 .probe_slot = intel_probe_slot,
Adrian Hunter0acccf42017-12-08 15:08:18 +0200469 .setup_host = intel_setup_host,
Adrian Hunter1c451c12017-10-19 13:41:46 +0300470 .priv_size = sizeof(struct intel_host),
Adrian Hunter07a58882013-04-26 11:27:22 +0300471};
472
Philip Elcan70cce2a2016-03-03 11:38:46 -0500473static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = {
474 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
475 .quirks2 = SDHCI_QUIRK2_NO_1_8_V,
476 .caps = MMC_CAP_NONREMOVABLE,
477};
478
479static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = {
480 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
481 .caps = MMC_CAP_NONREMOVABLE,
482};
483
Shah Nehal-Bakulchandra34597a32017-12-01 15:38:52 +0530484/* AMD sdhci reset dll register. */
485#define SDHCI_AMD_RESET_DLL_REGISTER 0x908
486
487static int amd_select_drive_strength(struct mmc_card *card,
488 unsigned int max_dtr, int host_drv,
489 int card_drv, int *drv_type)
490{
491 return MMC_SET_DRIVER_TYPE_A;
492}
493
494static void sdhci_acpi_amd_hs400_dll(struct sdhci_host *host)
495{
496 /* AMD Platform requires dll setting */
497 sdhci_writel(host, 0x40003210, SDHCI_AMD_RESET_DLL_REGISTER);
498 usleep_range(10, 20);
499 sdhci_writel(host, 0x40033210, SDHCI_AMD_RESET_DLL_REGISTER);
500}
501
502/*
503 * For AMD Platform it is required to disable the tuning
504 * bit first controller to bring to HS Mode from HS200
505 * mode, later enable to tune to HS400 mode.
506 */
507static void amd_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
508{
509 struct sdhci_host *host = mmc_priv(mmc);
510 unsigned int old_timing = host->timing;
511
512 sdhci_set_ios(mmc, ios);
513 if (old_timing == MMC_TIMING_MMC_HS200 &&
514 ios->timing == MMC_TIMING_MMC_HS)
515 sdhci_writew(host, 0x9, SDHCI_HOST_CONTROL2);
516 if (old_timing != MMC_TIMING_MMC_HS400 &&
517 ios->timing == MMC_TIMING_MMC_HS400) {
518 sdhci_writew(host, 0x80, SDHCI_HOST_CONTROL2);
519 sdhci_acpi_amd_hs400_dll(host);
520 }
521}
522
523static const struct sdhci_ops sdhci_acpi_ops_amd = {
524 .set_clock = sdhci_set_clock,
525 .set_bus_width = sdhci_set_bus_width,
526 .reset = sdhci_reset,
527 .set_uhs_signaling = sdhci_set_uhs_signaling,
528};
529
530static const struct sdhci_acpi_chip sdhci_acpi_chip_amd = {
531 .ops = &sdhci_acpi_ops_amd,
532};
533
534static int sdhci_acpi_emmc_amd_probe_slot(struct platform_device *pdev,
535 const char *hid, const char *uid)
536{
537 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
538 struct sdhci_host *host = c->host;
539
540 sdhci_read_caps(host);
541 if (host->caps1 & SDHCI_SUPPORT_DDR50)
542 host->mmc->caps = MMC_CAP_1_8V_DDR;
543
544 if ((host->caps1 & SDHCI_SUPPORT_SDR104) &&
545 (host->mmc->caps & MMC_CAP_1_8V_DDR))
546 host->mmc->caps2 = MMC_CAP2_HS400_1_8V;
547
548 host->mmc_host_ops.select_drive_strength = amd_select_drive_strength;
549 host->mmc_host_ops.set_ios = amd_set_ios;
550 return 0;
551}
552
553static const struct sdhci_acpi_slot sdhci_acpi_slot_amd_emmc = {
554 .chip = &sdhci_acpi_chip_amd,
555 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE,
556 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR | SDHCI_QUIRK_32BIT_DMA_SIZE |
557 SDHCI_QUIRK_32BIT_ADMA_SIZE,
558 .probe_slot = sdhci_acpi_emmc_amd_probe_slot,
559};
560
Adrian Hunter07a58882013-04-26 11:27:22 +0300561struct sdhci_acpi_uid_slot {
562 const char *hid;
563 const char *uid;
564 const struct sdhci_acpi_slot *slot;
565};
566
567static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
Adrian Huntere839b132015-10-21 11:15:46 +0300568 { "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
569 { "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
570 { "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
Adrian Hunter07a58882013-04-26 11:27:22 +0300571 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
Daniel Drakedb52d4f2016-12-01 14:33:03 -0600572 { "80860F14" , "2" , &sdhci_acpi_slot_int_sdio },
Adrian Hunter07a58882013-04-26 11:27:22 +0300573 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
Adrian Hunteraad95dc2014-03-10 15:02:43 +0200574 { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
Adrian Hunter07a58882013-04-26 11:27:22 +0300575 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
Adrian Hunter7147eaf2014-09-24 10:27:29 +0300576 { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd },
Adrian Hunter07a58882013-04-26 11:27:22 +0300577 { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
Mika Westerberg07c001c2013-11-12 12:01:33 +0200578 { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
Adrian Hunterd0ed8e62015-01-05 14:47:57 +0200579 { "INT344D" , NULL, &sdhci_acpi_slot_int_sdio },
Michele Curti0cd2f042015-09-05 08:49:52 +0200580 { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd },
Adrian Hunter07a58882013-04-26 11:27:22 +0300581 { "PNP0D40" },
Philip Elcan70cce2a2016-03-03 11:38:46 -0500582 { "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v },
583 { "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd },
Shah Nehal-Bakulchandra34597a32017-12-01 15:38:52 +0530584 { "AMDI0040", NULL, &sdhci_acpi_slot_amd_emmc },
Adrian Hunter07a58882013-04-26 11:27:22 +0300585 { },
586};
587
Adrian Hunterc4e05032012-11-23 21:17:34 +0100588static const struct acpi_device_id sdhci_acpi_ids[] = {
Adrian Huntere839b132015-10-21 11:15:46 +0300589 { "80865ACA" },
590 { "80865ACC" },
591 { "80865AD0" },
Adrian Hunter07a58882013-04-26 11:27:22 +0300592 { "80860F14" },
Adrian Hunteraad95dc2014-03-10 15:02:43 +0200593 { "80860F16" },
Adrian Hunter07a58882013-04-26 11:27:22 +0300594 { "INT33BB" },
595 { "INT33C6" },
Mika Westerberg07c001c2013-11-12 12:01:33 +0200596 { "INT3436" },
Adrian Hunterd0ed8e62015-01-05 14:47:57 +0200597 { "INT344D" },
Adrian Hunter07a58882013-04-26 11:27:22 +0300598 { "PNP0D40" },
Philip Elcan70cce2a2016-03-03 11:38:46 -0500599 { "QCOM8051" },
600 { "QCOM8052" },
Shah Nehal-Bakulchandra34597a32017-12-01 15:38:52 +0530601 { "AMDI0040" },
Adrian Hunterc4e05032012-11-23 21:17:34 +0100602 { },
603};
604MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
605
Adrian Hunter3db35252014-10-06 15:23:05 +0300606static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
607 const char *uid)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100608{
Adrian Hunter07a58882013-04-26 11:27:22 +0300609 const struct sdhci_acpi_uid_slot *u;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100610
Adrian Hunter07a58882013-04-26 11:27:22 +0300611 for (u = sdhci_acpi_uids; u->hid; u++) {
612 if (strcmp(u->hid, hid))
613 continue;
614 if (!u->uid)
615 return u->slot;
616 if (uid && !strcmp(u->uid, uid))
617 return u->slot;
618 }
Adrian Hunterc4e05032012-11-23 21:17:34 +0100619 return NULL;
620}
621
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800622static int sdhci_acpi_probe(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100623{
624 struct device *dev = &pdev->dev;
Adrian Hunterf07b7952017-10-19 13:41:45 +0300625 const struct sdhci_acpi_slot *slot;
Adrian Huntere5bbf302016-05-19 15:25:42 +0200626 struct acpi_device *device, *child;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100627 struct sdhci_acpi_host *c;
628 struct sdhci_host *host;
629 struct resource *iomem;
630 resource_size_t len;
Adrian Hunterf07b7952017-10-19 13:41:45 +0300631 size_t priv_size;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100632 const char *hid;
Adrian Hunter3db35252014-10-06 15:23:05 +0300633 const char *uid;
Mika Westerberg87875652014-01-08 12:40:55 +0200634 int err;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100635
Andy Shevchenkocd25c7b2017-07-24 17:59:58 +0300636 device = ACPI_COMPANION(dev);
637 if (!device)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100638 return -ENODEV;
639
Adrian Hunter17753d12017-06-21 15:08:39 +0300640 hid = acpi_device_hid(device);
Adrian Huntera2038492017-10-19 13:41:43 +0300641 uid = acpi_device_uid(device);
Adrian Hunter17753d12017-06-21 15:08:39 +0300642
Adrian Hunterf07b7952017-10-19 13:41:45 +0300643 slot = sdhci_acpi_get_slot(hid, uid);
644
Adrian Huntere5bbf302016-05-19 15:25:42 +0200645 /* Power on the SDHCI controller and its children */
646 acpi_device_fix_up_power(device);
Adrian Hunter17753d12017-06-21 15:08:39 +0300647 if (!sdhci_acpi_no_fixup_child_power(hid, uid)) {
648 list_for_each_entry(child, &device->children, node)
649 if (child->status.present && child->status.enabled)
650 acpi_device_fix_up_power(child);
651 }
Adrian Huntere5bbf302016-05-19 15:25:42 +0200652
Adrian Hunter6e1c7d62016-04-15 14:06:57 +0300653 if (sdhci_acpi_byt_defer(dev))
654 return -EPROBE_DEFER;
655
Adrian Hunterc4e05032012-11-23 21:17:34 +0100656 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
657 if (!iomem)
658 return -ENOMEM;
659
660 len = resource_size(iomem);
661 if (len < 0x100)
662 dev_err(dev, "Invalid iomem size!\n");
663
664 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
665 return -ENOMEM;
666
Adrian Hunterf07b7952017-10-19 13:41:45 +0300667 priv_size = slot ? slot->priv_size : 0;
668 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100669 if (IS_ERR(host))
670 return PTR_ERR(host);
671
672 c = sdhci_priv(host);
673 c->host = host;
Adrian Hunterf07b7952017-10-19 13:41:45 +0300674 c->slot = slot;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100675 c->pdev = pdev;
676 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
677
678 platform_set_drvdata(pdev, c);
679
680 host->hw_name = "ACPI";
681 host->ops = &sdhci_acpi_ops_dflt;
682 host->irq = platform_get_irq(pdev, 0);
Adrian Hunterd58ac802018-03-21 11:49:40 +0200683 if (host->irq < 0) {
Arvind Yadav1b7ba572017-11-19 10:22:45 +0530684 err = -EINVAL;
685 goto err_free;
686 }
Adrian Hunterc4e05032012-11-23 21:17:34 +0100687
688 host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
689 resource_size(iomem));
690 if (host->ioaddr == NULL) {
691 err = -ENOMEM;
692 goto err_free;
693 }
694
Adrian Hunterc4e05032012-11-23 21:17:34 +0100695 if (c->slot) {
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800696 if (c->slot->probe_slot) {
Adrian Hunter7dafca82014-10-06 15:23:06 +0300697 err = c->slot->probe_slot(pdev, hid, uid);
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800698 if (err)
699 goto err_free;
700 }
Adrian Hunterc4e05032012-11-23 21:17:34 +0100701 if (c->slot->chip) {
702 host->ops = c->slot->chip->ops;
703 host->quirks |= c->slot->chip->quirks;
704 host->quirks2 |= c->slot->chip->quirks2;
705 host->mmc->caps |= c->slot->chip->caps;
706 host->mmc->caps2 |= c->slot->chip->caps2;
707 host->mmc->pm_caps |= c->slot->chip->pm_caps;
708 }
709 host->quirks |= c->slot->quirks;
710 host->quirks2 |= c->slot->quirks2;
711 host->mmc->caps |= c->slot->caps;
712 host->mmc->caps2 |= c->slot->caps2;
713 host->mmc->pm_caps |= c->slot->pm_caps;
714 }
715
Adrian Hunter0d3e3352013-04-04 16:41:06 +0300716 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
717
Adrian Hunter4fd44092014-03-10 15:02:42 +0200718 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
719 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
720
Zhang Ruie28d6f02017-01-18 17:46:18 +0800721 err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL);
722 if (err) {
723 if (err == -EPROBE_DEFER)
724 goto err_free;
Adrian Hunter4fd44092014-03-10 15:02:42 +0200725 dev_warn(dev, "failed to setup card detect gpio\n");
726 c->use_runtime_pm = false;
727 }
728 }
729
Adrian Hunter0cc1a0f2017-12-08 15:04:58 +0200730 err = sdhci_setup_host(host);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100731 if (err)
732 goto err_free;
733
Adrian Hunter0cc1a0f2017-12-08 15:04:58 +0200734 if (c->slot && c->slot->setup_host) {
735 err = c->slot->setup_host(pdev);
736 if (err)
737 goto err_cleanup;
738 }
739
740 err = __sdhci_add_host(host);
741 if (err)
742 goto err_cleanup;
743
Adrian Hunterc4e05032012-11-23 21:17:34 +0100744 if (c->use_runtime_pm) {
Adrian Hunter1d1ff452013-04-26 11:27:21 +0300745 pm_runtime_set_active(dev);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100746 pm_suspend_ignore_children(dev, 1);
747 pm_runtime_set_autosuspend_delay(dev, 50);
748 pm_runtime_use_autosuspend(dev);
749 pm_runtime_enable(dev);
750 }
751
Fu, Zhonghui4e6a2ef2016-01-22 12:35:26 +0800752 device_enable_async_suspend(dev);
753
Adrian Hunterc4e05032012-11-23 21:17:34 +0100754 return 0;
755
Adrian Hunter0cc1a0f2017-12-08 15:04:58 +0200756err_cleanup:
757 sdhci_cleanup_host(c->host);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100758err_free:
Adrian Hunterc4e05032012-11-23 21:17:34 +0100759 sdhci_free_host(c->host);
760 return err;
761}
762
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800763static int sdhci_acpi_remove(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100764{
765 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
766 struct device *dev = &pdev->dev;
767 int dead;
768
769 if (c->use_runtime_pm) {
770 pm_runtime_get_sync(dev);
771 pm_runtime_disable(dev);
772 pm_runtime_put_noidle(dev);
773 }
774
Gao, Yunpeng578b36b2014-09-01 11:35:40 +0800775 if (c->slot && c->slot->remove_slot)
776 c->slot->remove_slot(pdev);
777
Adrian Hunterc4e05032012-11-23 21:17:34 +0100778 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
779 sdhci_remove_host(c->host, dead);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100780 sdhci_free_host(c->host);
781
782 return 0;
783}
784
785#ifdef CONFIG_PM_SLEEP
786
787static int sdhci_acpi_suspend(struct device *dev)
788{
789 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
Adrian Hunterd38dcad2017-03-20 19:50:32 +0200790 struct sdhci_host *host = c->host;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100791
Adrian Hunterd38dcad2017-03-20 19:50:32 +0200792 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
793 mmc_retune_needed(host->mmc);
794
795 return sdhci_suspend_host(host);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100796}
797
798static int sdhci_acpi_resume(struct device *dev)
799{
800 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
801
Adrian Hunter6e1c7d62016-04-15 14:06:57 +0300802 sdhci_acpi_byt_setting(&c->pdev->dev);
803
Adrian Hunterc4e05032012-11-23 21:17:34 +0100804 return sdhci_resume_host(c->host);
805}
806
Adrian Hunterc4e05032012-11-23 21:17:34 +0100807#endif
808
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +0100809#ifdef CONFIG_PM
Adrian Hunterc4e05032012-11-23 21:17:34 +0100810
811static int sdhci_acpi_runtime_suspend(struct device *dev)
812{
813 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
Adrian Hunterd38dcad2017-03-20 19:50:32 +0200814 struct sdhci_host *host = c->host;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100815
Adrian Hunterd38dcad2017-03-20 19:50:32 +0200816 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
817 mmc_retune_needed(host->mmc);
818
819 return sdhci_runtime_suspend_host(host);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100820}
821
822static int sdhci_acpi_runtime_resume(struct device *dev)
823{
824 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
825
Adrian Hunter6e1c7d62016-04-15 14:06:57 +0300826 sdhci_acpi_byt_setting(&c->pdev->dev);
827
Adrian Hunterc4e05032012-11-23 21:17:34 +0100828 return sdhci_runtime_resume_host(c->host);
829}
830
Adrian Hunterc4e05032012-11-23 21:17:34 +0100831#endif
832
833static const struct dev_pm_ops sdhci_acpi_pm_ops = {
Ulf Hanssondafed442016-07-27 10:39:52 +0200834 SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume)
Peter Griffin1d75f742014-08-12 17:14:29 +0100835 SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
Ulf Hansson9b449e92015-01-02 14:47:00 +0100836 sdhci_acpi_runtime_resume, NULL)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100837};
838
839static struct platform_driver sdhci_acpi_driver = {
840 .driver = {
841 .name = "sdhci-acpi",
Adrian Hunterc4e05032012-11-23 21:17:34 +0100842 .acpi_match_table = sdhci_acpi_ids,
843 .pm = &sdhci_acpi_pm_ops,
844 },
845 .probe = sdhci_acpi_probe,
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800846 .remove = sdhci_acpi_remove,
Adrian Hunterc4e05032012-11-23 21:17:34 +0100847};
848
849module_platform_driver(sdhci_acpi_driver);
850
851MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
852MODULE_AUTHOR("Adrian Hunter");
853MODULE_LICENSE("GPL v2");