pci_mmc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015, Google, Inc
  4. * Copyright (C) 2014, Bin Meng <bmeng.cn@gmail.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <log.h>
  10. #include <malloc.h>
  11. #include <mapmem.h>
  12. #include <sdhci.h>
  13. #include <acpi/acpigen.h>
  14. #include <acpi/acpi_device.h>
  15. #include <acpi/acpi_dp.h>
  16. #include <asm-generic/gpio.h>
  17. #include <dm/acpi.h>
  18. struct pci_mmc_plat {
  19. struct mmc_config cfg;
  20. struct mmc mmc;
  21. };
  22. struct pci_mmc_priv {
  23. struct sdhci_host host;
  24. void *base;
  25. struct gpio_desc cd_gpio;
  26. };
  27. static int pci_mmc_probe(struct udevice *dev)
  28. {
  29. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  30. struct pci_mmc_plat *plat = dev_get_platdata(dev);
  31. struct pci_mmc_priv *priv = dev_get_priv(dev);
  32. struct sdhci_host *host = &priv->host;
  33. int ret;
  34. host->ioaddr = (void *)dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0,
  35. PCI_REGION_MEM);
  36. host->name = dev->name;
  37. host->mmc = &plat->mmc;
  38. host->mmc->dev = dev;
  39. ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
  40. if (ret)
  41. return ret;
  42. host->mmc->priv = &priv->host;
  43. upriv->mmc = host->mmc;
  44. return sdhci_probe(dev);
  45. }
  46. static int pci_mmc_ofdata_to_platdata(struct udevice *dev)
  47. {
  48. struct pci_mmc_priv *priv = dev_get_priv(dev);
  49. gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
  50. return 0;
  51. }
  52. static int pci_mmc_bind(struct udevice *dev)
  53. {
  54. struct pci_mmc_plat *plat = dev_get_platdata(dev);
  55. return sdhci_bind(dev, &plat->mmc, &plat->cfg);
  56. }
  57. static int pci_mmc_acpi_fill_ssdt(const struct udevice *dev,
  58. struct acpi_ctx *ctx)
  59. {
  60. struct pci_mmc_priv *priv = dev_get_priv(dev);
  61. char path[ACPI_PATH_MAX];
  62. struct acpi_gpio gpio;
  63. struct acpi_dp *dp;
  64. int ret;
  65. if (!dev_of_valid(dev))
  66. return 0;
  67. ret = gpio_get_acpi(&priv->cd_gpio, &gpio);
  68. if (ret)
  69. return log_msg_ret("gpio", ret);
  70. gpio.type = ACPI_GPIO_TYPE_INTERRUPT;
  71. gpio.pull = ACPI_GPIO_PULL_NONE;
  72. gpio.irq.mode = ACPI_IRQ_EDGE_TRIGGERED;
  73. gpio.irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
  74. gpio.irq.shared = ACPI_IRQ_SHARED;
  75. gpio.irq.wake = ACPI_IRQ_WAKE;
  76. gpio.interrupt_debounce_timeout = 10000; /* 100ms */
  77. /* Use device path as the Scope for the SSDT */
  78. ret = acpi_device_path(dev, path, sizeof(path));
  79. if (ret)
  80. return log_msg_ret("path", ret);
  81. acpigen_write_scope(ctx, path);
  82. acpigen_write_name(ctx, "_CRS");
  83. /* Write GpioInt() as default (if set) or custom from devicetree */
  84. acpigen_write_resourcetemplate_header(ctx);
  85. acpi_device_write_gpio(ctx, &gpio);
  86. acpigen_write_resourcetemplate_footer(ctx);
  87. /* Bind the cd-gpio name to the GpioInt() resource */
  88. dp = acpi_dp_new_table("_DSD");
  89. if (!dp)
  90. return -ENOMEM;
  91. acpi_dp_add_gpio(dp, "cd-gpio", path, 0, 0, 1);
  92. ret = acpi_dp_write(ctx, dp);
  93. if (ret)
  94. return log_msg_ret("cd", ret);
  95. acpigen_pop_len(ctx);
  96. return 0;
  97. }
  98. struct acpi_ops pci_mmc_acpi_ops = {
  99. .fill_ssdt = pci_mmc_acpi_fill_ssdt,
  100. };
  101. static const struct udevice_id pci_mmc_match[] = {
  102. { .compatible = "intel,apl-sd" },
  103. { }
  104. };
  105. U_BOOT_DRIVER(pci_mmc) = {
  106. .name = "pci_mmc",
  107. .id = UCLASS_MMC,
  108. .of_match = pci_mmc_match,
  109. .bind = pci_mmc_bind,
  110. .ofdata_to_platdata = pci_mmc_ofdata_to_platdata,
  111. .probe = pci_mmc_probe,
  112. .ops = &sdhci_ops,
  113. .priv_auto_alloc_size = sizeof(struct pci_mmc_priv),
  114. .platdata_auto_alloc_size = sizeof(struct pci_mmc_plat),
  115. ACPI_OPS_PTR(&pci_mmc_acpi_ops)
  116. };
  117. static struct pci_device_id mmc_supported[] = {
  118. { PCI_DEVICE_CLASS(PCI_CLASS_SYSTEM_SDHCI << 8, 0xffff00) },
  119. {},
  120. };
  121. U_BOOT_PCI_DEVICE(pci_mmc, mmc_supported);