125b6a6f1Skrish_iyer /* 2dedbe94eSAdrien Destugues * Copyright 2018-2020 Haiku, Inc. All rights reserved. 325b6a6f1Skrish_iyer * Distributed under the terms of the MIT License. 425b6a6f1Skrish_iyer * 525b6a6f1Skrish_iyer * Authors: 625b6a6f1Skrish_iyer * B Krishnan Iyer, krishnaniyer97@gmail.com 725b6a6f1Skrish_iyer */ 825b6a6f1Skrish_iyer #include "mmc_bus.h" 925b6a6f1Skrish_iyer 10ff76d2dfSAdrien Destugues #include <Errors.h> 11ff76d2dfSAdrien Destugues 12ff76d2dfSAdrien Destugues #include <stdint.h> 13ff76d2dfSAdrien Destugues 1425b6a6f1Skrish_iyer 1525b6a6f1Skrish_iyer MMCBus::MMCBus(device_node* node) 1625b6a6f1Skrish_iyer : 1725b6a6f1Skrish_iyer fNode(node), 1825b6a6f1Skrish_iyer fController(NULL), 1925b6a6f1Skrish_iyer fCookie(NULL), 2025b6a6f1Skrish_iyer fStatus(B_OK), 2174b60970SAnarchos fWorkerThread(-1), 2274b60970SAnarchos fActiveDevice(0) 2325b6a6f1Skrish_iyer { 2425b6a6f1Skrish_iyer CALLED(); 25ff76d2dfSAdrien Destugues 26ff76d2dfSAdrien Destugues // Get the parent info, it includes the API to send commands to the hardware 2725b6a6f1Skrish_iyer device_node* parent = gDeviceManager->get_parent_node(node); 2825b6a6f1Skrish_iyer fStatus = gDeviceManager->get_driver(parent, 2925b6a6f1Skrish_iyer (driver_module_info**)&fController, &fCookie); 3025b6a6f1Skrish_iyer gDeviceManager->put_node(parent); 3125b6a6f1Skrish_iyer 3225b6a6f1Skrish_iyer if (fStatus != B_OK) { 3325b6a6f1Skrish_iyer ERROR("Not able to establish the bus %s\n", 3425b6a6f1Skrish_iyer strerror(fStatus)); 3525b6a6f1Skrish_iyer return; 3625b6a6f1Skrish_iyer } 37ff76d2dfSAdrien Destugues 3874b60970SAnarchos fScanSemaphore = create_sem(0, "MMC bus scan"); 3974b60970SAnarchos fLockSemaphore = create_sem(1, "MMC bus lock"); 4074b60970SAnarchos fWorkerThread = spawn_kernel_thread(_WorkerThread, "SD bus controller", 41ff76d2dfSAdrien Destugues B_NORMAL_PRIORITY, this); 42ff76d2dfSAdrien Destugues resume_thread(fWorkerThread); 4325b6a6f1Skrish_iyer } 4425b6a6f1Skrish_iyer 4525b6a6f1Skrish_iyer 4625b6a6f1Skrish_iyer MMCBus::~MMCBus() 4725b6a6f1Skrish_iyer { 4825b6a6f1Skrish_iyer CALLED(); 49ff76d2dfSAdrien Destugues 50ff76d2dfSAdrien Destugues // stop worker thread 51ff76d2dfSAdrien Destugues fStatus = B_SHUTTING_DOWN; 52ff76d2dfSAdrien Destugues 53ff76d2dfSAdrien Destugues status_t result; 54ff76d2dfSAdrien Destugues if (fWorkerThread != 0) 55ff76d2dfSAdrien Destugues wait_for_thread(fWorkerThread, &result); 56ff76d2dfSAdrien Destugues // TODO power off cards, stop clock, etc if needed. 5774b60970SAnarchos 5874b60970SAnarchos delete_sem(fLockSemaphore); 5974b60970SAnarchos delete_sem(fScanSemaphore); 6025b6a6f1Skrish_iyer } 6125b6a6f1Skrish_iyer 6225b6a6f1Skrish_iyer 6325b6a6f1Skrish_iyer status_t 6425b6a6f1Skrish_iyer MMCBus::InitCheck() 6525b6a6f1Skrish_iyer { 6625b6a6f1Skrish_iyer return fStatus; 6725b6a6f1Skrish_iyer } 68ff76d2dfSAdrien Destugues 69ff76d2dfSAdrien Destugues 70dedbe94eSAdrien Destugues void 71dedbe94eSAdrien Destugues MMCBus::Rescan() 72dedbe94eSAdrien Destugues { 73dedbe94eSAdrien Destugues // Just wake up the thread for a scan 7474b60970SAnarchos release_sem(fScanSemaphore); 75dedbe94eSAdrien Destugues } 76dedbe94eSAdrien Destugues 77dedbe94eSAdrien Destugues 78ff76d2dfSAdrien Destugues status_t 79ff76d2dfSAdrien Destugues MMCBus::ExecuteCommand(uint8_t command, uint32_t argument, uint32_t* response) 80ff76d2dfSAdrien Destugues { 8174b60970SAnarchos status_t status = _ActivateDevice(0); 8274b60970SAnarchos if (status != B_OK) 8374b60970SAnarchos return status; 84ff76d2dfSAdrien Destugues return fController->execute_command(fCookie, command, argument, response); 85ff76d2dfSAdrien Destugues } 86ff76d2dfSAdrien Destugues 87ff76d2dfSAdrien Destugues 88ff76d2dfSAdrien Destugues status_t 89*d1fee57dSAdrien Destugues MMCBus::DoIO(uint16_t rca, uint8_t command, IOOperation* operation, 90*d1fee57dSAdrien Destugues bool offsetAsSectors) 9174b60970SAnarchos { 9274b60970SAnarchos status_t status = _ActivateDevice(rca); 9374b60970SAnarchos if (status != B_OK) 9474b60970SAnarchos return status; 95*d1fee57dSAdrien Destugues return fController->do_io(fCookie, command, operation, offsetAsSectors); 9674b60970SAnarchos } 9774b60970SAnarchos 9874b60970SAnarchos 9974b60970SAnarchos status_t 10074b60970SAnarchos MMCBus::_ActivateDevice(uint16_t rca) 10174b60970SAnarchos { 10274b60970SAnarchos // Do nothing if the device is already activated 10374b60970SAnarchos if (fActiveDevice == rca) 10474b60970SAnarchos return B_OK; 10574b60970SAnarchos 10674b60970SAnarchos uint32_t response; 10774b60970SAnarchos status_t result; 10874b60970SAnarchos result = fController->execute_command(fCookie, SD_SELECT_DESELECT_CARD, 10974b60970SAnarchos ((uint32)rca) << 16, &response); 11074b60970SAnarchos 11174b60970SAnarchos if (result == B_OK) 11274b60970SAnarchos fActiveDevice = rca; 11374b60970SAnarchos 11474b60970SAnarchos return result; 11574b60970SAnarchos } 11674b60970SAnarchos 11774b60970SAnarchos 11874b60970SAnarchos status_t 11974b60970SAnarchos MMCBus::_WorkerThread(void* cookie) 120ff76d2dfSAdrien Destugues { 121ff76d2dfSAdrien Destugues MMCBus* bus = (MMCBus*)cookie; 122ff76d2dfSAdrien Destugues uint32_t response; 123ff76d2dfSAdrien Destugues 12474b60970SAnarchos acquire_sem(bus->fLockSemaphore); 12574b60970SAnarchos 126ff76d2dfSAdrien Destugues // We assume the bus defaults to 400kHz clock and has already powered on 127ff76d2dfSAdrien Destugues // cards. 128ff76d2dfSAdrien Destugues 129ff76d2dfSAdrien Destugues // Reset all cards on the bus 13074b60970SAnarchos bus->ExecuteCommand(SD_GO_IDLE_STATE, 0, NULL); 131ff76d2dfSAdrien Destugues 132dedbe94eSAdrien Destugues while (bus->fStatus != B_SHUTTING_DOWN) { 13374b60970SAnarchos release_sem(bus->fLockSemaphore); 134dedbe94eSAdrien Destugues // wait for bus to signal a card is inserted 13574b60970SAnarchos // Most of the time the thread will be waiting here, with 13674b60970SAnarchos // fLockSemaphore released 13774b60970SAnarchos acquire_sem(bus->fScanSemaphore); 13874b60970SAnarchos acquire_sem(bus->fLockSemaphore); 13974b60970SAnarchos 140dedbe94eSAdrien Destugues TRACE("Scanning the bus\n"); 141dedbe94eSAdrien Destugues 142ff76d2dfSAdrien Destugues // Probe the voltage range 14374b60970SAnarchos enum { 14474b60970SAnarchos // Table 4-40 in physical layer specification v8.00 14574b60970SAnarchos // All other values are currently reserved 14674b60970SAnarchos HOST_27_36V = 1, //Host supplied voltage 2.7-3.6V 14774b60970SAnarchos }; 14874b60970SAnarchos 14974b60970SAnarchos // An arbitrary value, we just need to check that the response 15074b60970SAnarchos // containts the same. 15174b60970SAnarchos static const uint8 kVoltageCheckPattern = 0xAA; 15274b60970SAnarchos 153ff76d2dfSAdrien Destugues // FIXME MMC cards will not reply to this! They expect CMD1 instead 154ff76d2dfSAdrien Destugues // SD v1 cards will also not reply, but we can proceed to ACMD41 155ff76d2dfSAdrien Destugues // If ACMD41 also does not work, it may be an SDIO card, too 15674b60970SAnarchos uint32_t probe = (HOST_27_36V << 8) | kVoltageCheckPattern; 157518af33fSAdrien Destugues uint32_t hcs = 1 << 30; 15874b60970SAnarchos if (bus->ExecuteCommand(SD_SEND_IF_COND, probe, &response) != B_OK) { 159518af33fSAdrien Destugues TRACE("Card does not implement CMD8, may be a V1 SD card\n"); 160518af33fSAdrien Destugues // Do not check for SDHC support in this case 161518af33fSAdrien Destugues hcs = 0; 162518af33fSAdrien Destugues } else if (response != probe) { 163dedbe94eSAdrien Destugues ERROR("Card does not support voltage range (expected %x, " 164dedbe94eSAdrien Destugues "reply %x)\n", probe, response); 165ff76d2dfSAdrien Destugues // TODO what now? 166ff76d2dfSAdrien Destugues } 167ff76d2dfSAdrien Destugues 168ff76d2dfSAdrien Destugues // Probe OCR, waiting for card to become ready 169ff76d2dfSAdrien Destugues uint32_t ocr; 170ff76d2dfSAdrien Destugues do { 171ff76d2dfSAdrien Destugues uint32_t cardStatus; 17274b60970SAnarchos while (bus->ExecuteCommand(SD_APP_CMD, 0, &cardStatus) 173ff76d2dfSAdrien Destugues == B_BUSY) { 174ff76d2dfSAdrien Destugues ERROR("Card locked after CMD8...\n"); 175ff76d2dfSAdrien Destugues snooze(1000000); 176ff76d2dfSAdrien Destugues } 177ff76d2dfSAdrien Destugues if ((cardStatus & 0xFFFF8000) != 0) 178dedbe94eSAdrien Destugues ERROR("SD card reports error %x\n", cardStatus); 179ff76d2dfSAdrien Destugues if ((cardStatus & (1 << 5)) == 0) 180dedbe94eSAdrien Destugues ERROR("Card did not enter ACMD mode\n"); 181ff76d2dfSAdrien Destugues 18274b60970SAnarchos bus->ExecuteCommand(SD_SEND_OP_COND, hcs | 0xFF8000, &ocr); 183ff76d2dfSAdrien Destugues 184ff76d2dfSAdrien Destugues if ((ocr & (1 << 31)) == 0) { 185ff76d2dfSAdrien Destugues TRACE("Card is busy\n"); 186ff76d2dfSAdrien Destugues snooze(100000); 187ff76d2dfSAdrien Destugues } 188ff76d2dfSAdrien Destugues } while (((ocr & (1 << 31)) == 0)); 189ff76d2dfSAdrien Destugues 190dedbe94eSAdrien Destugues // FIXME this should be asked to each card, when there are multiple 191dedbe94eSAdrien Destugues // ones. So ACMD41 should be moved inside the probing loop below? 192dedbe94eSAdrien Destugues uint8_t cardType = CARD_TYPE_SD; 193dedbe94eSAdrien Destugues 194eb92a834SAdrien Destugues if ((ocr & hcs) != 0) 195dedbe94eSAdrien Destugues cardType = CARD_TYPE_SDHC; 196eb92a834SAdrien Destugues if ((ocr & (1 << 29)) != 0) 197dedbe94eSAdrien Destugues cardType = CARD_TYPE_UHS2; 198eb92a834SAdrien Destugues if ((ocr & (1 << 24)) != 0) 199ff76d2dfSAdrien Destugues TRACE("Card supports 1.8v"); 200ff76d2dfSAdrien Destugues TRACE("Voltage range: %x\n", ocr & 0xFFFFFF); 201ff76d2dfSAdrien Destugues 202ff76d2dfSAdrien Destugues // TODO send CMD11 to switch to low voltage mode if card supports it? 203ff76d2dfSAdrien Destugues 20474b60970SAnarchos // We use CMD2 (ALL_SEND_CID) and CMD3 (SEND_RELATIVE_ADDR) to assign 20574b60970SAnarchos // an RCA to all cards. Initially all cards have an RCA of 0 and will 20674b60970SAnarchos // all receive CMD2. But only ne of them will reply (they do collision 20774b60970SAnarchos // detection while sending the CID in reply). We assign a new RCA to 20874b60970SAnarchos // that first card, and repeat the process with the remaining ones 20974b60970SAnarchos // until no one answers to CMD2. Then we know all cards have an RCA 21074b60970SAnarchos // (and a matching published device on our side). 211ff76d2dfSAdrien Destugues uint32_t cid[4]; 21274b60970SAnarchos 21374b60970SAnarchos while (bus->ExecuteCommand(SD_ALL_SEND_CID, 0, cid) == B_OK) { 21474b60970SAnarchos bus->ExecuteCommand(SD_SEND_RELATIVE_ADDR, 0, &response); 215ff76d2dfSAdrien Destugues 216ff76d2dfSAdrien Destugues TRACE("RCA: %x Status: %x\n", response >> 16, response & 0xFFFF); 217ff76d2dfSAdrien Destugues 218dedbe94eSAdrien Destugues if ((response & 0xFF00) != 0x500) { 219dedbe94eSAdrien Destugues TRACE("Card did not enter data state\n"); 220dedbe94eSAdrien Destugues // This probably means there are no more cards to scan on the 221dedbe94eSAdrien Destugues // bus, so exit the loop. 222dedbe94eSAdrien Destugues break; 223dedbe94eSAdrien Destugues } 224ff76d2dfSAdrien Destugues 225dedbe94eSAdrien Destugues // The card now has an RCA and it entered the data phase, which 226dedbe94eSAdrien Destugues // means our initializing job is over, we can pass it on to the 227dedbe94eSAdrien Destugues // mmc_disk driver. 228ff76d2dfSAdrien Destugues 229dedbe94eSAdrien Destugues uint32_t vendor = cid[3] & 0xFFFFFF; 230eb92a834SAdrien Destugues char name[6] = {(char)(cid[2] >> 24), (char)(cid[2] >> 16), 231eb92a834SAdrien Destugues (char)(cid[2] >> 8), (char)cid[2], (char)(cid[1] >> 24), 0}; 232dedbe94eSAdrien Destugues uint32_t serial = (cid[1] << 16) | (cid[0] >> 16); 233dedbe94eSAdrien Destugues uint16_t revision = (cid[1] >> 20) & 0xF; 234dedbe94eSAdrien Destugues revision *= 100; 235dedbe94eSAdrien Destugues revision += (cid[1] >> 16) & 0xF; 236dedbe94eSAdrien Destugues uint8_t month = cid[0] & 0xF; 237dedbe94eSAdrien Destugues uint16_t year = 2000 + ((cid[0] >> 4) & 0xFF); 238dedbe94eSAdrien Destugues uint16_t rca = response >> 16; 239ff76d2dfSAdrien Destugues 240dedbe94eSAdrien Destugues device_attr attrs[] = { 241dedbe94eSAdrien Destugues { B_DEVICE_BUS, B_STRING_TYPE, {string: "mmc" }}, 24262eaf4c0SAdrien Destugues { B_DEVICE_PRETTY_NAME, B_STRING_TYPE, {string: "mmc device" }}, 243dedbe94eSAdrien Destugues { B_DEVICE_VENDOR_ID, B_UINT32_TYPE, {ui32: vendor}}, 244dedbe94eSAdrien Destugues { B_DEVICE_ID, B_STRING_TYPE, {string: name}}, 245dedbe94eSAdrien Destugues { B_DEVICE_UNIQUE_ID, B_UINT32_TYPE, {ui32: serial}}, 246dedbe94eSAdrien Destugues { "mmc/revision", B_UINT16_TYPE, {ui16: revision}}, 247dedbe94eSAdrien Destugues { "mmc/month", B_UINT8_TYPE, {ui8: month}}, 248dedbe94eSAdrien Destugues { "mmc/year", B_UINT16_TYPE, {ui16: year}}, 249e4689893SAdrien Destugues { kMmcRcaAttribute, B_UINT16_TYPE, {ui16: rca}}, 250e4689893SAdrien Destugues { kMmcTypeAttribute, B_UINT8_TYPE, {ui8: cardType}}, 251dedbe94eSAdrien Destugues {} 252dedbe94eSAdrien Destugues }; 253dedbe94eSAdrien Destugues 254dedbe94eSAdrien Destugues // publish child device for the card 255dedbe94eSAdrien Destugues gDeviceManager->register_node(bus->fNode, MMC_BUS_MODULE_NAME, 256dedbe94eSAdrien Destugues attrs, NULL, NULL); 257dedbe94eSAdrien Destugues } 258dedbe94eSAdrien Destugues 259dedbe94eSAdrien Destugues // FIXME we also need to unpublish devices that are gone. Probably need 260dedbe94eSAdrien Destugues // to "ping" all RCAs somehow? Or is there an interrupt we can look for 261dedbe94eSAdrien Destugues // to detect added/removed cards? 262dedbe94eSAdrien Destugues } 263dedbe94eSAdrien Destugues 26474b60970SAnarchos release_sem(bus->fLockSemaphore); 26574b60970SAnarchos 266dedbe94eSAdrien Destugues TRACE("poller thread terminating"); 267dedbe94eSAdrien Destugues return B_OK; 268ff76d2dfSAdrien Destugues } 269