xref: /haiku/src/add-ons/kernel/bus_managers/mmc/mmc_bus.cpp (revision 5ec64c5cdd43187f30726a3a5dece4c0db863689)
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);
4324136793SAdrien Destugues 
4424136793SAdrien Destugues 	fController->set_scan_semaphore(fCookie, fScanSemaphore);
4525b6a6f1Skrish_iyer }
4625b6a6f1Skrish_iyer 
4725b6a6f1Skrish_iyer 
4825b6a6f1Skrish_iyer MMCBus::~MMCBus()
4925b6a6f1Skrish_iyer {
5025b6a6f1Skrish_iyer 	CALLED();
51ff76d2dfSAdrien Destugues 
52ff76d2dfSAdrien Destugues 	// stop worker thread
53ff76d2dfSAdrien Destugues 	fStatus = B_SHUTTING_DOWN;
54ff76d2dfSAdrien Destugues 
55ff76d2dfSAdrien Destugues 	status_t result;
56ff76d2dfSAdrien Destugues 	if (fWorkerThread != 0)
57ff76d2dfSAdrien Destugues 		wait_for_thread(fWorkerThread, &result);
58ff76d2dfSAdrien Destugues 	// TODO power off cards, stop clock, etc if needed.
5974b60970SAnarchos 
6074b60970SAnarchos 	delete_sem(fLockSemaphore);
6174b60970SAnarchos 	delete_sem(fScanSemaphore);
6225b6a6f1Skrish_iyer }
6325b6a6f1Skrish_iyer 
6425b6a6f1Skrish_iyer 
6525b6a6f1Skrish_iyer status_t
6625b6a6f1Skrish_iyer MMCBus::InitCheck()
6725b6a6f1Skrish_iyer {
6825b6a6f1Skrish_iyer 	return fStatus;
6925b6a6f1Skrish_iyer }
70ff76d2dfSAdrien Destugues 
71ff76d2dfSAdrien Destugues 
72dedbe94eSAdrien Destugues void
73dedbe94eSAdrien Destugues MMCBus::Rescan()
74dedbe94eSAdrien Destugues {
75dedbe94eSAdrien Destugues 	// Just wake up the thread for a scan
7674b60970SAnarchos 	release_sem(fScanSemaphore);
77dedbe94eSAdrien Destugues }
78dedbe94eSAdrien Destugues 
79dedbe94eSAdrien Destugues 
80ff76d2dfSAdrien Destugues status_t
81522c141dSAdrien Destugues MMCBus::ExecuteCommand(uint16_t rca, uint8_t command, uint32_t argument,
82522c141dSAdrien Destugues 	uint32_t* response)
83ff76d2dfSAdrien Destugues {
84522c141dSAdrien Destugues 	status_t status = _ActivateDevice(rca);
8574b60970SAnarchos 	if (status != B_OK)
8674b60970SAnarchos 		return status;
87ff76d2dfSAdrien Destugues 	return fController->execute_command(fCookie, command, argument, response);
88ff76d2dfSAdrien Destugues }
89ff76d2dfSAdrien Destugues 
90ff76d2dfSAdrien Destugues 
91ff76d2dfSAdrien Destugues status_t
92d1fee57dSAdrien Destugues MMCBus::DoIO(uint16_t rca, uint8_t command, IOOperation* operation,
93d1fee57dSAdrien Destugues 	bool offsetAsSectors)
9474b60970SAnarchos {
9574b60970SAnarchos 	status_t status = _ActivateDevice(rca);
9674b60970SAnarchos 	if (status != B_OK)
9774b60970SAnarchos 		return status;
98d1fee57dSAdrien Destugues 	return fController->do_io(fCookie, command, operation, offsetAsSectors);
9974b60970SAnarchos }
10074b60970SAnarchos 
10174b60970SAnarchos 
102522c141dSAdrien Destugues void
103522c141dSAdrien Destugues MMCBus::SetClock(int frequency)
104522c141dSAdrien Destugues {
105522c141dSAdrien Destugues 	fController->set_clock(fCookie, frequency);
106522c141dSAdrien Destugues }
107522c141dSAdrien Destugues 
108522c141dSAdrien Destugues 
10934552f8eSAdrien Destugues void
11034552f8eSAdrien Destugues MMCBus::SetBusWidth(int width)
11134552f8eSAdrien Destugues {
11234552f8eSAdrien Destugues 	fController->set_bus_width(fCookie, width);
11334552f8eSAdrien Destugues }
11434552f8eSAdrien Destugues 
11534552f8eSAdrien Destugues 
11674b60970SAnarchos status_t
11774b60970SAnarchos MMCBus::_ActivateDevice(uint16_t rca)
11874b60970SAnarchos {
11974b60970SAnarchos 	// Do nothing if the device is already activated
12074b60970SAnarchos 	if (fActiveDevice == rca)
12174b60970SAnarchos 		return B_OK;
12274b60970SAnarchos 
12374b60970SAnarchos 	uint32_t response;
12474b60970SAnarchos 	status_t result;
12574b60970SAnarchos 	result = fController->execute_command(fCookie, SD_SELECT_DESELECT_CARD,
12674b60970SAnarchos 		((uint32)rca) << 16, &response);
12774b60970SAnarchos 
12874b60970SAnarchos 	if (result == B_OK)
12974b60970SAnarchos 		fActiveDevice = rca;
13074b60970SAnarchos 
13174b60970SAnarchos 	return result;
13274b60970SAnarchos }
13374b60970SAnarchos 
13474b60970SAnarchos 
13524136793SAdrien Destugues void MMCBus::_AcquireScanSemaphore()
13624136793SAdrien Destugues {
13724136793SAdrien Destugues 	release_sem(fLockSemaphore);
13824136793SAdrien Destugues 	acquire_sem(fScanSemaphore);
13924136793SAdrien Destugues 	acquire_sem(fLockSemaphore);
14024136793SAdrien Destugues }
14124136793SAdrien Destugues 
14224136793SAdrien Destugues 
14374b60970SAnarchos status_t
14474b60970SAnarchos MMCBus::_WorkerThread(void* cookie)
145ff76d2dfSAdrien Destugues {
146ff76d2dfSAdrien Destugues 	MMCBus* bus = (MMCBus*)cookie;
147ff76d2dfSAdrien Destugues 	uint32_t response;
148ff76d2dfSAdrien Destugues 
14974b60970SAnarchos 	acquire_sem(bus->fLockSemaphore);
15074b60970SAnarchos 
151ff76d2dfSAdrien Destugues 	// We assume the bus defaults to 400kHz clock and has already powered on
152ff76d2dfSAdrien Destugues 	// cards.
153ff76d2dfSAdrien Destugues 
154ff76d2dfSAdrien Destugues 	// Reset all cards on the bus
15524136793SAdrien Destugues 	// This does not work if the bus has not been powered on yet (the command
15624136793SAdrien Destugues 	// will timeout), in that case we wait until asked to scan again when a
15724136793SAdrien Destugues 	// card has been inserted and powered on.
15824136793SAdrien Destugues 	status_t result;
15924136793SAdrien Destugues 	do {
16024136793SAdrien Destugues 		bus->_AcquireScanSemaphore();
16134552f8eSAdrien Destugues 
16224136793SAdrien Destugues 		TRACE("Reset the bus...\n");
163522c141dSAdrien Destugues 		result = bus->ExecuteCommand(0, SD_GO_IDLE_STATE, 0, NULL);
16424136793SAdrien Destugues 		TRACE("CMD0 result: %s\n", strerror(result));
16524136793SAdrien Destugues 	} while (result != B_OK);
16624136793SAdrien Destugues 
16724136793SAdrien Destugues 	// Need to wait at least 8 clock cycles after CMD0 before sending the next
168522c141dSAdrien Destugues 	// command. With the default 400kHz clock that would be 20 microseconds,
16934552f8eSAdrien Destugues 	// but we need to wait at least 20ms here, otherwise the next command times
17034552f8eSAdrien Destugues 	// out
171*5ec64c5cSAdrien Destugues 	snooze(30000);
172ff76d2dfSAdrien Destugues 
173dedbe94eSAdrien Destugues 	while (bus->fStatus != B_SHUTTING_DOWN) {
174dedbe94eSAdrien Destugues 		TRACE("Scanning the bus\n");
175dedbe94eSAdrien Destugues 
17634552f8eSAdrien Destugues 		// Use the low speed clock and 1bit bus width for scanning
177522c141dSAdrien Destugues 		bus->SetClock(400);
17834552f8eSAdrien Destugues 		bus->SetBusWidth(1);
179522c141dSAdrien Destugues 
180ff76d2dfSAdrien Destugues 		// Probe the voltage range
18174b60970SAnarchos 		enum {
18274b60970SAnarchos 			// Table 4-40 in physical layer specification v8.00
18374b60970SAnarchos 			// All other values are currently reserved
18474b60970SAnarchos 			HOST_27_36V = 1, //Host supplied voltage 2.7-3.6V
18574b60970SAnarchos 		};
18674b60970SAnarchos 
18774b60970SAnarchos 		// An arbitrary value, we just need to check that the response
18874b60970SAnarchos 		// containts the same.
18974b60970SAnarchos 		static const uint8 kVoltageCheckPattern = 0xAA;
19074b60970SAnarchos 
191ff76d2dfSAdrien Destugues 		// FIXME MMC cards will not reply to this! They expect CMD1 instead
192ff76d2dfSAdrien Destugues 		// SD v1 cards will also not reply, but we can proceed to ACMD41
193ff76d2dfSAdrien Destugues 		// If ACMD41 also does not work, it may be an SDIO card, too
19474b60970SAnarchos 		uint32_t probe = (HOST_27_36V << 8) | kVoltageCheckPattern;
195518af33fSAdrien Destugues 		uint32_t hcs = 1 << 30;
196522c141dSAdrien Destugues 		if (bus->ExecuteCommand(0, SD_SEND_IF_COND, probe, &response) != B_OK) {
197518af33fSAdrien Destugues 			TRACE("Card does not implement CMD8, may be a V1 SD card\n");
198518af33fSAdrien Destugues 			// Do not check for SDHC support in this case
199518af33fSAdrien Destugues 			hcs = 0;
200518af33fSAdrien Destugues 		} else if (response != probe) {
201dedbe94eSAdrien Destugues 			ERROR("Card does not support voltage range (expected %x, "
202dedbe94eSAdrien Destugues 				"reply %x)\n", probe, response);
20324136793SAdrien Destugues 			// TODO we should power off the bus in this case.
204ff76d2dfSAdrien Destugues 		}
205ff76d2dfSAdrien Destugues 
206ff76d2dfSAdrien Destugues 		// Probe OCR, waiting for card to become ready
20724136793SAdrien Destugues 		// We keep repeating ACMD41 until the card replies that it is
20824136793SAdrien Destugues 		// initialized.
209ff76d2dfSAdrien Destugues 		uint32_t ocr;
210ff76d2dfSAdrien Destugues 		do {
211ff76d2dfSAdrien Destugues 			uint32_t cardStatus;
212522c141dSAdrien Destugues 			while (bus->ExecuteCommand(0, SD_APP_CMD, 0, &cardStatus)
213ff76d2dfSAdrien Destugues 					== B_BUSY) {
214ff76d2dfSAdrien Destugues 				ERROR("Card locked after CMD8...\n");
215ff76d2dfSAdrien Destugues 				snooze(1000000);
216ff76d2dfSAdrien Destugues 			}
217ff76d2dfSAdrien Destugues 			if ((cardStatus & 0xFFFF8000) != 0)
218dedbe94eSAdrien Destugues 				ERROR("SD card reports error %x\n", cardStatus);
219ff76d2dfSAdrien Destugues 			if ((cardStatus & (1 << 5)) == 0)
220dedbe94eSAdrien Destugues 				ERROR("Card did not enter ACMD mode\n");
221ff76d2dfSAdrien Destugues 
222522c141dSAdrien Destugues 			bus->ExecuteCommand(0, SD_SEND_OP_COND, hcs | 0xFF8000, &ocr);
223ff76d2dfSAdrien Destugues 
224ff76d2dfSAdrien Destugues 			if ((ocr & (1 << 31)) == 0) {
225ff76d2dfSAdrien Destugues 				TRACE("Card is busy\n");
226ff76d2dfSAdrien Destugues 				snooze(100000);
227ff76d2dfSAdrien Destugues 			}
228ff76d2dfSAdrien Destugues 		} while (((ocr & (1 << 31)) == 0));
229ff76d2dfSAdrien Destugues 
230dedbe94eSAdrien Destugues 		// FIXME this should be asked to each card, when there are multiple
231dedbe94eSAdrien Destugues 		// ones. So ACMD41 should be moved inside the probing loop below?
232dedbe94eSAdrien Destugues 		uint8_t cardType = CARD_TYPE_SD;
233dedbe94eSAdrien Destugues 
234eb92a834SAdrien Destugues 		if ((ocr & hcs) != 0)
235dedbe94eSAdrien Destugues 			cardType = CARD_TYPE_SDHC;
236eb92a834SAdrien Destugues 		if ((ocr & (1 << 29)) != 0)
237dedbe94eSAdrien Destugues 			cardType = CARD_TYPE_UHS2;
238eb92a834SAdrien Destugues 		if ((ocr & (1 << 24)) != 0)
239ff76d2dfSAdrien Destugues 			TRACE("Card supports 1.8v");
240ff76d2dfSAdrien Destugues 		TRACE("Voltage range: %x\n", ocr & 0xFFFFFF);
241ff76d2dfSAdrien Destugues 
242ff76d2dfSAdrien Destugues 		// TODO send CMD11 to switch to low voltage mode if card supports it?
243ff76d2dfSAdrien Destugues 
24474b60970SAnarchos 		// We use CMD2 (ALL_SEND_CID) and CMD3 (SEND_RELATIVE_ADDR) to assign
24574b60970SAnarchos 		// an RCA to all cards. Initially all cards have an RCA of 0 and will
24674b60970SAnarchos 		// all receive CMD2. But only ne of them will reply (they do collision
24774b60970SAnarchos 		// detection while sending the CID in reply). We assign a new RCA to
24874b60970SAnarchos 		// that first card, and repeat the process with the remaining ones
24974b60970SAnarchos 		// until no one answers to CMD2. Then we know all cards have an RCA
25074b60970SAnarchos 		// (and a matching published device on our side).
251ff76d2dfSAdrien Destugues 		uint32_t cid[4];
25274b60970SAnarchos 
253522c141dSAdrien Destugues 		while (bus->ExecuteCommand(0, SD_ALL_SEND_CID, 0, cid) == B_OK) {
254522c141dSAdrien Destugues 			bus->ExecuteCommand(0, SD_SEND_RELATIVE_ADDR, 0, &response);
255ff76d2dfSAdrien Destugues 
256ff76d2dfSAdrien Destugues 			TRACE("RCA: %x Status: %x\n", response >> 16, response & 0xFFFF);
257ff76d2dfSAdrien Destugues 
258dedbe94eSAdrien Destugues 			if ((response & 0xFF00) != 0x500) {
259dedbe94eSAdrien Destugues 				TRACE("Card did not enter data state\n");
260dedbe94eSAdrien Destugues 				// This probably means there are no more cards to scan on the
261dedbe94eSAdrien Destugues 				// bus, so exit the loop.
262dedbe94eSAdrien Destugues 				break;
263dedbe94eSAdrien Destugues 			}
264ff76d2dfSAdrien Destugues 
265dedbe94eSAdrien Destugues 			// The card now has an RCA and it entered the data phase, which
266dedbe94eSAdrien Destugues 			// means our initializing job is over, we can pass it on to the
267dedbe94eSAdrien Destugues 			// mmc_disk driver.
268ff76d2dfSAdrien Destugues 
269dedbe94eSAdrien Destugues 			uint32_t vendor = cid[3] & 0xFFFFFF;
270eb92a834SAdrien Destugues 			char name[6] = {(char)(cid[2] >> 24), (char)(cid[2] >> 16),
271eb92a834SAdrien Destugues 				(char)(cid[2] >> 8), (char)cid[2], (char)(cid[1] >> 24), 0};
272dedbe94eSAdrien Destugues 			uint32_t serial = (cid[1] << 16) | (cid[0] >> 16);
273dedbe94eSAdrien Destugues 			uint16_t revision = (cid[1] >> 20) & 0xF;
274dedbe94eSAdrien Destugues 			revision *= 100;
275dedbe94eSAdrien Destugues 			revision += (cid[1] >> 16) & 0xF;
276dedbe94eSAdrien Destugues 			uint8_t month = cid[0] & 0xF;
277dedbe94eSAdrien Destugues 			uint16_t year = 2000 + ((cid[0] >> 4) & 0xFF);
278dedbe94eSAdrien Destugues 			uint16_t rca = response >> 16;
279ff76d2dfSAdrien Destugues 
280dedbe94eSAdrien Destugues 			device_attr attrs[] = {
281dedbe94eSAdrien Destugues 				{ B_DEVICE_BUS, B_STRING_TYPE, {string: "mmc" }},
28262eaf4c0SAdrien Destugues 				{ B_DEVICE_PRETTY_NAME, B_STRING_TYPE, {string: "mmc device" }},
283dedbe94eSAdrien Destugues 				{ B_DEVICE_VENDOR_ID, B_UINT32_TYPE, {ui32: vendor}},
284dedbe94eSAdrien Destugues 				{ B_DEVICE_ID, B_STRING_TYPE, {string: name}},
285dedbe94eSAdrien Destugues 				{ B_DEVICE_UNIQUE_ID, B_UINT32_TYPE, {ui32: serial}},
286dedbe94eSAdrien Destugues 				{ "mmc/revision", B_UINT16_TYPE, {ui16: revision}},
287dedbe94eSAdrien Destugues 				{ "mmc/month", B_UINT8_TYPE, {ui8: month}},
288dedbe94eSAdrien Destugues 				{ "mmc/year", B_UINT16_TYPE, {ui16: year}},
289e4689893SAdrien Destugues 				{ kMmcRcaAttribute, B_UINT16_TYPE, {ui16: rca}},
290e4689893SAdrien Destugues 				{ kMmcTypeAttribute, B_UINT8_TYPE, {ui8: cardType}},
291dedbe94eSAdrien Destugues 				{}
292dedbe94eSAdrien Destugues 			};
293dedbe94eSAdrien Destugues 
294dedbe94eSAdrien Destugues 			// publish child device for the card
295dedbe94eSAdrien Destugues 			gDeviceManager->register_node(bus->fNode, MMC_BUS_MODULE_NAME,
296dedbe94eSAdrien Destugues 				attrs, NULL, NULL);
297dedbe94eSAdrien Destugues 		}
298dedbe94eSAdrien Destugues 
299522c141dSAdrien Destugues 		// TODO if there is a single card active, check if it supports CMD6
300522c141dSAdrien Destugues 		// (spec version 1.10 or later in SCR). If it does, check if CMD6 can
301522c141dSAdrien Destugues 		// enable high speed mode, use that to go to 50MHz instead of 25.
302522c141dSAdrien Destugues 		bus->SetClock(25000);
303522c141dSAdrien Destugues 
304dedbe94eSAdrien Destugues 		// FIXME we also need to unpublish devices that are gone. Probably need
305dedbe94eSAdrien Destugues 		// to "ping" all RCAs somehow? Or is there an interrupt we can look for
306dedbe94eSAdrien Destugues 		// to detect added/removed cards?
30724136793SAdrien Destugues 
30824136793SAdrien Destugues 		// Wait for the next scan request
30924136793SAdrien Destugues 		// The thread will spend most of its time waiting here
31024136793SAdrien Destugues 		bus->_AcquireScanSemaphore();
311dedbe94eSAdrien Destugues 	}
312dedbe94eSAdrien Destugues 
31374b60970SAnarchos 	release_sem(bus->fLockSemaphore);
31474b60970SAnarchos 
315dedbe94eSAdrien Destugues 	TRACE("poller thread terminating");
316dedbe94eSAdrien Destugues 	return B_OK;
317ff76d2dfSAdrien Destugues }
318