|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2022, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2023-02-25 GuEe-GUI the first version |
| 9 | + */ |
| 10 | + |
| 11 | +#define SDHCI_REG_BAR 0 |
| 12 | + |
| 13 | +#include "../dev_sdio_dm.h" |
| 14 | + |
| 15 | +struct pci_sdhci_host |
| 16 | +{ |
| 17 | + struct rt_sdhci_host parent; |
| 18 | +}; |
| 19 | + |
| 20 | +static const struct rt_sdhci_ops pci_sdhci_ops = |
| 21 | +{ |
| 22 | + .set_clock = rt_sdhci_set_clock, |
| 23 | + .set_bus_width = rt_sdhci_set_bus_width, |
| 24 | + .reset = rt_sdhci_reset, |
| 25 | +}; |
| 26 | + |
| 27 | +static rt_err_t pci_sdhci_probe(struct rt_pci_device *pdev) |
| 28 | +{ |
| 29 | + rt_err_t err; |
| 30 | + struct rt_sdhci_host *host; |
| 31 | + struct pci_sdhci_host *pci_host; |
| 32 | + |
| 33 | + host = rt_sdhci_alloc_host(&pdev->parent, sizeof(struct pci_sdhci_host)); |
| 34 | + |
| 35 | + if (!host) |
| 36 | + { |
| 37 | + return -RT_ENOMEM; |
| 38 | + } |
| 39 | + pci_host = rt_container_of(host, struct pci_sdhci_host, parent); |
| 40 | + |
| 41 | + host->ioaddr = rt_pci_iomap(pdev, SDHCI_REG_BAR); |
| 42 | + |
| 43 | + if (!host->ioaddr) |
| 44 | + { |
| 45 | + err = -RT_EIO; |
| 46 | + goto _fail; |
| 47 | + } |
| 48 | + |
| 49 | + host->irq = pdev->irq; |
| 50 | + host->ops = &pci_sdhci_ops; |
| 51 | + |
| 52 | + rt_pci_irq_unmask(pdev); |
| 53 | + rt_pci_set_master(pdev); |
| 54 | + |
| 55 | + if ((err = rt_sdhci_set_and_add_host(host))) |
| 56 | + { |
| 57 | + goto _fail; |
| 58 | + } |
| 59 | + |
| 60 | + pdev->parent.user_data = pci_host; |
| 61 | + |
| 62 | + return RT_EOK; |
| 63 | + |
| 64 | +_fail: |
| 65 | + if (host->ioaddr) |
| 66 | + { |
| 67 | + rt_iounmap(host->ioaddr); |
| 68 | + } |
| 69 | + |
| 70 | + rt_sdhci_free_host(host); |
| 71 | + |
| 72 | + return err; |
| 73 | +} |
| 74 | + |
| 75 | +static rt_err_t pci_sdhci_remove(struct rt_pci_device *pdev) |
| 76 | +{ |
| 77 | + rt_bool_t dead; |
| 78 | + struct rt_sdhci_host *host; |
| 79 | + struct pci_sdhci_host *pci_host = pdev->parent.user_data; |
| 80 | + |
| 81 | + host = &pci_host->parent; |
| 82 | + |
| 83 | + /* INTx is shared, don't mask all */ |
| 84 | + rt_hw_interrupt_umask(pdev->irq); |
| 85 | + rt_pci_irq_mask(pdev); |
| 86 | + rt_pci_clear_master(pdev); |
| 87 | + |
| 88 | + dead = (HWREG32(host->ioaddr + RT_SDHCI_INT_STATUS) == 0xffffffff); |
| 89 | + |
| 90 | + rt_sdhci_uninit_host(host, dead); |
| 91 | + |
| 92 | + rt_iounmap(host->ioaddr); |
| 93 | + rt_sdhci_free_host(host); |
| 94 | + |
| 95 | + return RT_EOK; |
| 96 | +} |
| 97 | + |
| 98 | +static const struct rt_pci_device_id pci_sdhci_pci_ids[] = |
| 99 | +{ |
| 100 | + { RT_PCI_DEVICE_ID(PCI_VENDOR_ID_REDHAT, 0x0007), }, |
| 101 | + { RT_PCI_DEVICE_CLASS(PCIS_SYSTEM_SDHCI, ~0) }, |
| 102 | + { /* sentinel */ } |
| 103 | +}; |
| 104 | + |
| 105 | +static struct rt_pci_driver pci_sdhci_driver = |
| 106 | +{ |
| 107 | + .name = "sdhci-pci", |
| 108 | + |
| 109 | + .ids = pci_sdhci_pci_ids, |
| 110 | + .probe = pci_sdhci_probe, |
| 111 | + .remove = pci_sdhci_remove, |
| 112 | +}; |
| 113 | +RT_PCI_DRIVER_EXPORT(pci_sdhci_driver); |
0 commit comments