1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 3 #include <commonlib/sdhci.h> 4 #include <device/pci.h> 5 #include <device/pci_ops.h> 6 #include <stdint.h> 7 8 #include "sd_mmc.h" 9 #include "storage.h" 10 11 /* Initialize an SDHCI port */ sdhci_controller_init(struct sdhci_ctrlr * sdhci_ctrlr,void * ioaddr)12int sdhci_controller_init(struct sdhci_ctrlr *sdhci_ctrlr, void *ioaddr) 13 { 14 sdhci_ctrlr->ioaddr = ioaddr; 15 return add_sdhci(sdhci_ctrlr); 16 } 17 new_mem_sdhci_controller(void * ioaddr,int (* pre_init_func)(struct sdhci_ctrlr * host))18struct sd_mmc_ctrlr *new_mem_sdhci_controller(void *ioaddr, 19 int (*pre_init_func)(struct sdhci_ctrlr *host)) 20 { 21 static bool sdhci_init_done; 22 static struct sdhci_ctrlr sdhci_ctrlr = {0}; 23 24 if (sdhci_init_done == true) { 25 sdhc_error("Error: SDHCI is already initialized.\n"); 26 return NULL; 27 } 28 29 sdhci_ctrlr.attach = pre_init_func; 30 31 if (sdhci_controller_init(&sdhci_ctrlr, ioaddr)) { 32 sdhc_error("Error: SDHCI initialization failed.\n"); 33 return NULL; 34 } 35 36 sdhci_init_done = true; 37 38 return &sdhci_ctrlr.sd_mmc_ctrlr; 39 } 40 new_pci_sdhci_controller(pci_devfn_t dev)41struct sd_mmc_ctrlr *new_pci_sdhci_controller(pci_devfn_t dev) 42 { 43 uintptr_t addr; 44 45 addr = pci_s_read_config32(dev, PCI_BASE_ADDRESS_0); 46 if (addr == ((uint32_t)~0)) { 47 sdhc_error("Error: PCI SDHCI not found\n"); 48 return NULL; 49 } 50 51 addr &= ~0xf; 52 return new_mem_sdhci_controller((void *)addr, NULL); 53 } 54