1 /* $OpenBSD: sdhc_pci.c,v 1.12 2011/12/23 21:58:47 kettenis Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/device.h> 21 #include <sys/systm.h> 22 #include <sys/malloc.h> 23 24 #include <dev/pci/pcivar.h> 25 #include <dev/pci/pcidevs.h> 26 #include <dev/sdmmc/sdhcreg.h> 27 #include <dev/sdmmc/sdhcvar.h> 28 #include <dev/sdmmc/sdmmcvar.h> 29 30 /* 31 * 8-bit PCI configuration register that tells us how many slots there 32 * are and which BAR entry corresponds to the first slot. 33 */ 34 #define SDHC_PCI_CONF_SLOT_INFO 0x40 35 #define SDHC_PCI_NUM_SLOTS(info) ((((info) >> 4) & 0x7) + 1) 36 #define SDHC_PCI_FIRST_BAR(info) ((info) & 0x7) 37 38 /* TI specific register */ 39 #define SDHC_PCI_GENERAL_CTL 0x4c 40 #define MMC_SD_DIS 0x02 41 42 /* RICOH specific registers */ 43 #define SDHC_PCI_MODE_KEY 0xf9 44 #define SDHC_PCI_MODE 0x150 45 #define SDHC_PCI_MODE_SD20 0x10 46 #define SDHC_PCI_BASE_FREQ_KEY 0xfc 47 #define SDHC_PCI_BASE_FREQ 0xe1 48 49 struct sdhc_pci_softc { 50 struct sdhc_softc sc; 51 void *sc_ih; 52 }; 53 54 int sdhc_pci_match(struct device *, void *, void *); 55 void sdhc_pci_attach(struct device *, struct device *, void *); 56 void sdhc_takecontroller(struct pci_attach_args *); 57 void sdhc_pci_conf_write(struct pci_attach_args *, int, uint8_t); 58 59 struct cfattach sdhc_pci_ca = { 60 sizeof(struct sdhc_pci_softc), sdhc_pci_match, sdhc_pci_attach, 61 NULL, sdhc_activate 62 }; 63 64 int 65 sdhc_pci_match(struct device *parent, void *match, void *aux) 66 { 67 struct pci_attach_args *pa = aux; 68 69 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SYSTEM && 70 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SYSTEM_SDHC) 71 return 1; 72 73 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RICOH && 74 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_RICOH_R5U822 || 75 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_RICOH_R5U823)) 76 return 1; 77 78 return 0; 79 } 80 81 void 82 sdhc_pci_attach(struct device *parent, struct device *self, void *aux) 83 { 84 struct sdhc_pci_softc *sc = (struct sdhc_pci_softc *)self; 85 struct pci_attach_args *pa = aux; 86 pci_intr_handle_t ih; 87 char const *intrstr; 88 int slotinfo; 89 int nslots; 90 int usedma; 91 int reg; 92 bus_space_tag_t iot; 93 bus_space_handle_t ioh; 94 bus_size_t size; 95 u_int32_t caps = 0; 96 97 /* Some TI controllers needs special treatment. */ 98 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TI && 99 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TI_PCI7XX1_SD && 100 pa->pa_function == 4) 101 sdhc_takecontroller(pa); 102 103 /* ENE controllers break if set to 0V bus power. */ 104 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ENE && 105 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ENE_SDCARD) 106 sc->sc.sc_flags |= SDHC_F_NOPWR0; 107 108 /* Some RICOH controllers need to be bumped into the right mode. */ 109 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RICOH && 110 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_RICOH_R5U823) { 111 /* Enable SD2.0 mode. */ 112 sdhc_pci_conf_write(pa, SDHC_PCI_MODE_KEY, 0xfc); 113 sdhc_pci_conf_write(pa, SDHC_PCI_MODE, SDHC_PCI_MODE_SD20); 114 sdhc_pci_conf_write(pa, SDHC_PCI_MODE_KEY, 0x00); 115 116 /* 117 * Some SD/MMC cards don't work with the default base 118 * clock frequency of 200MHz. Lower it to 50Hz. 119 */ 120 sdhc_pci_conf_write(pa, SDHC_PCI_BASE_FREQ_KEY, 0x01); 121 sdhc_pci_conf_write(pa, SDHC_PCI_BASE_FREQ, 50); 122 sdhc_pci_conf_write(pa, SDHC_PCI_BASE_FREQ_KEY, 0x00); 123 } 124 125 if (pci_intr_map(pa, &ih)) { 126 printf(": can't map interrupt\n"); 127 return; 128 } 129 130 intrstr = pci_intr_string(pa->pa_pc, ih); 131 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_SDMMC, 132 sdhc_intr, sc, sc->sc.sc_dev.dv_xname); 133 if (sc->sc_ih == NULL) { 134 printf(": can't establish interrupt\n"); 135 return; 136 } 137 printf(": %s\n", intrstr); 138 139 /* Enable use of DMA if supported by the interface. */ 140 usedma = PCI_INTERFACE(pa->pa_class) == SDHC_PCI_INTERFACE_DMA; 141 142 /* 143 * Map and attach all hosts supported by the host controller. 144 */ 145 slotinfo = pci_conf_read(pa->pa_pc, pa->pa_tag, 146 SDHC_PCI_CONF_SLOT_INFO); 147 nslots = SDHC_PCI_NUM_SLOTS(slotinfo); 148 149 /* Allocate an array big enough to hold all the possible hosts */ 150 sc->sc.sc_host = malloc(sizeof(struct sdhc_host *) * nslots, M_DEVBUF, 151 M_WAITOK); 152 153 /* XXX: handle 64-bit BARs */ 154 for (reg = SDHC_PCI_BAR_START + SDHC_PCI_FIRST_BAR(slotinfo) * 155 sizeof(u_int32_t); 156 reg < SDHC_PCI_BAR_END && nslots > 0; 157 reg += sizeof(u_int32_t), nslots--) { 158 159 if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, 160 NULL, NULL, NULL) != 0) 161 continue; 162 163 if (pci_mapreg_map(pa, reg, PCI_MAPREG_TYPE_MEM, 0, 164 &iot, &ioh, NULL, &size, 0)) { 165 printf("%s at 0x%x: can't map registers\n", 166 sc->sc.sc_dev.dv_xname, reg); 167 continue; 168 } 169 170 if (sdhc_host_found(&sc->sc, iot, ioh, size, usedma, caps) != 0) 171 /* XXX: sc->sc_host leak */ 172 printf("%s at 0x%x: can't initialize host\n", 173 sc->sc.sc_dev.dv_xname, reg); 174 } 175 176 /* 177 * Establish shutdown hooks. 178 */ 179 (void)shutdownhook_establish(sdhc_shutdown, &sc->sc); 180 } 181 182 void 183 sdhc_takecontroller(struct pci_attach_args *pa) 184 { 185 pcitag_t tag; 186 pcireg_t id, reg; 187 188 /* Look at func 3 for the flash device */ 189 tag = pci_make_tag(pa->pa_pc, pa->pa_bus, pa->pa_device, 3); 190 id = pci_conf_read(pa->pa_pc, tag, PCI_ID_REG); 191 if (PCI_PRODUCT(id) != PCI_PRODUCT_TI_PCI7XX1_FLASH) 192 return; 193 194 /* 195 * Disable MMC/SD on the flash media controller so the 196 * SD host takes over. 197 */ 198 reg = pci_conf_read(pa->pa_pc, tag, SDHC_PCI_GENERAL_CTL); 199 reg |= MMC_SD_DIS; 200 pci_conf_write(pa->pa_pc, tag, SDHC_PCI_GENERAL_CTL, reg); 201 } 202 203 void 204 sdhc_pci_conf_write(struct pci_attach_args *pa, int reg, uint8_t val) 205 { 206 pcireg_t tmp; 207 208 tmp = pci_conf_read(pa->pa_pc, pa->pa_tag, reg & ~0x3); 209 tmp &= ~(0xff << ((reg & 0x3) * 8)); 210 tmp |= (val << ((reg & 0x3) * 8)); 211 pci_conf_write(pa->pa_pc, pa->pa_tag, reg & ~0x3, tmp); 212 } 213