sdhci.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /* Polled SDHCI/eMMC driver for the Intel Braswell MMC controller.
  2. *
  3. * The HP Chromebook's eMMC is the SoC MMC function at PCI 00:10.0
  4. * (8086:2294), a standard SDHCI host controller. The firmware has already
  5. * assigned its BAR and powered the card, so the kernel only has to reset the
  6. * host, run the eMMC identification sequence (CMD0/1/2/3/7/16), read the
  7. * 512-byte EXT_CSD with CMD8 (which carries SEC_COUNT) and then use CMD17/
  8. * CMD24 with polled PIO for sector I/O.
  9. *
  10. * Everything is polled: there is no IRQ handler and no DMA descriptor ring.
  11. * The card is kept in its default 1-bit/25 MHz mode; that is slow (a few
  12. * MB/s) but enough for the storage/format and FAT paths, and avoids the
  13. * CMD6 bus-width/timing sequencing. The result is cached after the first
  14. * probe so the per-command vfs_mount_all() refreshes do not re-initialise
  15. * the card.
  16. */
  17. #include "sdhci.h"
  18. #include "vfs.h"
  19. #include "paging.h"
  20. #include "pci.h"
  21. #include "process.h"
  22. #include "sched.h"
  23. #include "serial.h"
  24. #include "timer.h"
  25. #define MMC_PCI_VENDOR 0x8086
  26. #define MMC_PCI_DEVICE 0x2294 /* Braswell eMMC */
  27. #define MMC_PCI_DEVICE_SDIO 0x2295
  28. #define MMC_PCI_DEVICE_SD 0x2296
  29. /* SDHCI register offsets. */
  30. #define SDHCI_BLOCK_SIZE 0x04
  31. #define SDHCI_BLOCK_COUNT 0x06
  32. #define SDHCI_ARGUMENT 0x08
  33. #define SDHCI_TRANSFER_MODE 0x0C
  34. #define SDHCI_COMMAND 0x0E
  35. #define SDHCI_RESPONSE 0x10
  36. #define SDHCI_BUFFER 0x20
  37. #define SDHCI_PRESENT_STATE 0x24
  38. #define SDHCI_HOST_CONTROL 0x28
  39. #define SDHCI_POWER_CONTROL 0x29
  40. #define SDHCI_CLOCK_CONTROL 0x2C
  41. #define SDHCI_TIMEOUT_CONTROL 0x2E
  42. #define SDHCI_SOFTWARE_RESET 0x2F
  43. #define SDHCI_INT_STATUS 0x30 /* 32-bit read covers normal + error */
  44. #define SDHCI_INT_STATUS_EN 0x34
  45. #define SDHCI_ERR_STATUS_EN 0x36
  46. #define SDHCI_INT_SIGNAL_EN 0x38
  47. #define SDHCI_ERR_SIGNAL_EN 0x3A
  48. #define SDHCI_HOST_CONTROL2 0x3E
  49. #define SDHCI_CAPABILITIES 0x40
  50. #define SDHCI_HOST_VERSION 0xFE
  51. #define SDHCI_CTRL_VDD_180 0x0008u
  52. /* Present State bits. */
  53. #define SDHCI_CMD_INHIBIT 0x00000001u
  54. #define SDHCI_DAT_INHIBIT 0x00000002u
  55. #define SDHCI_BUF_WRITE 0x00000400u
  56. #define SDHCI_BUF_READ 0x00000800u
  57. /* Normal interrupt bits. */
  58. #define SDHCI_INT_CMD_COMPLETE 0x0001u
  59. #define SDHCI_INT_TRANS_COMPLETE 0x0002u
  60. /* Clock control bits. */
  61. #define SDHCI_CLOCK_INT_EN 0x0001u
  62. #define SDHCI_CLOCK_INT_STABLE 0x0002u
  63. #define SDHCI_CLOCK_CARD_EN 0x0004u
  64. /* Software reset. */
  65. #define SDHCI_RESET_ALL 0x01u
  66. /* Command register flags. */
  67. #define SDHCI_CMD_RESP_NONE 0x00u
  68. #define SDHCI_CMD_RESP_LONG 0x01u
  69. #define SDHCI_CMD_RESP_SHORT 0x02u
  70. #define SDHCI_CMD_RESP_BUSY 0x03u
  71. #define SDHCI_CMD_CRC 0x08u
  72. #define SDHCI_CMD_INDEX 0x10u
  73. #define SDHCI_CMD_DATA 0x20u
  74. /* Transfer mode. */
  75. #define SDHCI_TRNS_READ 0x10u
  76. /* MMC commands used here. */
  77. #define MMC_GO_IDLE_STATE 0
  78. #define MMC_SEND_OP_COND 1
  79. #define MMC_ALL_SEND_CID 2
  80. #define MMC_SET_RELATIVE_ADDR 3
  81. #define MMC_SELECT_CARD 7
  82. #define MMC_SEND_EXT_CSD 8
  83. #define MMC_SWITCH 6
  84. #define MMC_SET_BLOCKLEN 16
  85. #define MMC_READ_SINGLE_BLOCK 17
  86. #define MMC_WRITE_SINGLE_BLOCK 24
  87. #define MMC_OCR_ARG 0x40FF8080u /* sector mode + full voltage */
  88. #define MMC_RCA 1
  89. #define SDHCI_TIMEOUT_MS 1000
  90. #define SDHCI_DATA_TIMEOUT_MS 5000
  91. static struct {
  92. int state; /* -1 unprobed, 0 absent, 1 ready */
  93. unsigned int bar;
  94. unsigned int base_khz;
  95. unsigned int sectors;
  96. unsigned int cache_size;
  97. int high_capacity;
  98. char model[41];
  99. } mmc = { .state = -1, .model = "" };
  100. static unsigned char rd8(unsigned int off)
  101. {
  102. return *(volatile unsigned char *)(mmc.bar + off);
  103. }
  104. static unsigned short rd16(unsigned int off)
  105. {
  106. return *(volatile unsigned short *)(mmc.bar + off);
  107. }
  108. static unsigned int rd32(unsigned int off)
  109. {
  110. return *(volatile unsigned int *)(mmc.bar + off);
  111. }
  112. static void wr8(unsigned int off, unsigned char v)
  113. {
  114. *(volatile unsigned char *)(mmc.bar + off) = v;
  115. }
  116. static void wr16(unsigned int off, unsigned short v)
  117. {
  118. *(volatile unsigned short *)(mmc.bar + off) = v;
  119. }
  120. static void wr32(unsigned int off, unsigned int v)
  121. {
  122. *(volatile unsigned int *)(mmc.bar + off) = v;
  123. }
  124. static void tiny_delay(unsigned int ms)
  125. {
  126. unsigned int t0 = get_ticks_ms();
  127. while (get_ticks_ms() - t0 < ms)
  128. vfs_scan_poll(t0);
  129. }
  130. static int wait_state(unsigned int mask, int set, unsigned int ms)
  131. {
  132. unsigned int t0 = get_ticks_ms();
  133. for (;;) {
  134. unsigned int v = rd32(SDHCI_PRESENT_STATE);
  135. if (set ? ((v & mask) == mask) : ((v & mask) == 0))
  136. return 0;
  137. if (get_ticks_ms() - t0 > ms)
  138. return -1;
  139. vfs_scan_poll(t0);
  140. }
  141. }
  142. static void log_fail(const char *what, unsigned int cmd, unsigned int sts)
  143. {
  144. serial_puts(SERIAL_COM1, "sdhci: ");
  145. serial_puts(SERIAL_COM1, what);
  146. if (cmd != 0xFFFFFFFFu) {
  147. serial_puts(SERIAL_COM1, " cmd ");
  148. serial_putdec(SERIAL_COM1, cmd);
  149. }
  150. serial_puts(SERIAL_COM1, " failed, int 0x");
  151. serial_puthex(SERIAL_COM1, sts);
  152. serial_puts(SERIAL_COM1, "\n");
  153. }
  154. static int sdhci_pio(void *buf, unsigned int blocks, int write)
  155. {
  156. unsigned int words = blocks * 128; /* 512 bytes per block */
  157. unsigned int i;
  158. const unsigned int *src = (const unsigned int *)buf;
  159. unsigned int *dst = (unsigned int *)buf;
  160. for (i = 0; i < words; i++) {
  161. unsigned int t0 = get_ticks_ms();
  162. if (write) {
  163. while (!(rd32(SDHCI_PRESENT_STATE) & SDHCI_BUF_WRITE)) {
  164. if (rd32(SDHCI_INT_STATUS) & 0xFFFF0000u)
  165. return -1;
  166. if (get_ticks_ms() - t0 > SDHCI_TIMEOUT_MS)
  167. return -1;
  168. }
  169. wr32(SDHCI_BUFFER, *src++);
  170. } else {
  171. while (!(rd32(SDHCI_PRESENT_STATE) & SDHCI_BUF_READ)) {
  172. if (rd32(SDHCI_INT_STATUS) & 0xFFFF0000u)
  173. return -1;
  174. if (get_ticks_ms() - t0 > SDHCI_TIMEOUT_MS)
  175. return -1;
  176. }
  177. *dst++ = rd32(SDHCI_BUFFER);
  178. }
  179. }
  180. return 0;
  181. }
  182. static int sdhci_cmd(unsigned int cmd, unsigned int arg, unsigned int flags,
  183. unsigned int blocks, unsigned int mode, void *buf)
  184. {
  185. unsigned int sts = 0;
  186. unsigned int t0;
  187. if (wait_state(SDHCI_CMD_INHIBIT | SDHCI_DAT_INHIBIT, 0,
  188. SDHCI_TIMEOUT_MS) != 0) {
  189. log_fail("inhibit", cmd, rd32(SDHCI_PRESENT_STATE));
  190. return -1;
  191. }
  192. wr32(SDHCI_INT_STATUS, 0xFFFFFFFFu);
  193. wr32(SDHCI_ARGUMENT, arg);
  194. if (flags & SDHCI_CMD_DATA) {
  195. wr16(SDHCI_BLOCK_SIZE, 512);
  196. wr16(SDHCI_BLOCK_COUNT, (unsigned short)(blocks ? blocks : 1));
  197. wr8(SDHCI_TRANSFER_MODE, (unsigned char)mode);
  198. }
  199. wr16(SDHCI_COMMAND, (unsigned short)((cmd << 8) | flags));
  200. t0 = get_ticks_ms();
  201. for (;;) {
  202. sts = rd32(SDHCI_INT_STATUS);
  203. if (sts & 0xFFFF0000u) {
  204. log_fail("command", cmd, sts);
  205. return -1;
  206. }
  207. if (sts & SDHCI_INT_CMD_COMPLETE)
  208. break;
  209. if (get_ticks_ms() - t0 > SDHCI_TIMEOUT_MS) {
  210. log_fail("command timeout", cmd, sts);
  211. return -1;
  212. }
  213. vfs_scan_poll(t0);
  214. }
  215. wr32(SDHCI_INT_STATUS, SDHCI_INT_CMD_COMPLETE);
  216. if (flags & SDHCI_CMD_DATA) {
  217. if (sdhci_pio(buf, blocks ? blocks : 1,
  218. (mode & SDHCI_TRNS_READ) ? 0 : 1) != 0) {
  219. log_fail("pio", cmd, rd32(SDHCI_INT_STATUS));
  220. return -1;
  221. }
  222. t0 = get_ticks_ms();
  223. for (;;) {
  224. sts = rd32(SDHCI_INT_STATUS);
  225. if (sts & 0xFFFF0000u) {
  226. log_fail("transfer", cmd, sts);
  227. return -1;
  228. }
  229. if (sts & SDHCI_INT_TRANS_COMPLETE)
  230. break;
  231. if (get_ticks_ms() - t0 > SDHCI_DATA_TIMEOUT_MS) {
  232. log_fail("transfer timeout", cmd, sts);
  233. return -1;
  234. }
  235. vfs_scan_poll(t0);
  236. }
  237. wr32(SDHCI_INT_STATUS, 0xFFFFFFFFu);
  238. }
  239. return 0;
  240. }
  241. static int sdhci_clock_div(unsigned int div)
  242. {
  243. unsigned int t0;
  244. if (div < 1)
  245. div = 1;
  246. if (div > 0xFF)
  247. div = 0xFF;
  248. wr16(SDHCI_CLOCK_CONTROL, 0);
  249. wr16(SDHCI_CLOCK_CONTROL,
  250. (unsigned short)((div << 8) | SDHCI_CLOCK_INT_EN));
  251. t0 = get_ticks_ms();
  252. while (!(rd16(SDHCI_CLOCK_CONTROL) & SDHCI_CLOCK_INT_STABLE)) {
  253. if (get_ticks_ms() - t0 > 100)
  254. return -1;
  255. vfs_scan_poll(t0);
  256. }
  257. wr16(SDHCI_CLOCK_CONTROL,
  258. (unsigned short)((div << 8) | SDHCI_CLOCK_INT_EN |
  259. SDHCI_CLOCK_CARD_EN));
  260. tiny_delay(2);
  261. return 0;
  262. }
  263. static int sdhci_reset_host(void)
  264. {
  265. unsigned int t0;
  266. wr8(SDHCI_SOFTWARE_RESET, SDHCI_RESET_ALL);
  267. t0 = get_ticks_ms();
  268. while (rd8(SDHCI_SOFTWARE_RESET) & SDHCI_RESET_ALL) {
  269. if (get_ticks_ms() - t0 > 100)
  270. return -1;
  271. vfs_scan_poll(t0);
  272. }
  273. return 0;
  274. }
  275. static int pci_find_mmc(unsigned int *bus, unsigned int *dev,
  276. unsigned int *func)
  277. {
  278. unsigned int b, d, f;
  279. for (b = 0; b < 256; b++) {
  280. for (d = 0; d < 32; d++) {
  281. unsigned int id = pci_cfg_read32(b, d, 0, 0x00);
  282. unsigned int htype, nfunc;
  283. if (id == 0xFFFFFFFFu || id == 0)
  284. continue;
  285. htype = (pci_cfg_read32(b, d, 0, 0x0C) >> 16) & 0xFFu;
  286. nfunc = (htype & 0x80u) ? 8u : 1u;
  287. for (f = 0; f < nfunc; f++) {
  288. unsigned int idf = pci_cfg_read32(b, d, f, 0x00);
  289. unsigned int vendor = idf & 0xFFFFu;
  290. unsigned int device = idf >> 16;
  291. unsigned int crev, cls, sub;
  292. if (idf == 0xFFFFFFFFu || idf == 0)
  293. continue;
  294. crev = pci_cfg_read32(b, d, f, 0x08);
  295. cls = (crev >> 24) & 0xFFu;
  296. sub = (crev >> 16) & 0xFFu;
  297. if (vendor != MMC_PCI_VENDOR)
  298. continue;
  299. if (device == MMC_PCI_DEVICE ||
  300. (device != MMC_PCI_DEVICE_SDIO &&
  301. device != MMC_PCI_DEVICE_SD &&
  302. cls == 0x08 && sub == 0x05)) {
  303. *bus = b;
  304. *dev = d;
  305. *func = f;
  306. return 0;
  307. }
  308. }
  309. }
  310. }
  311. return -1;
  312. }
  313. static int sdhci_init(void)
  314. {
  315. unsigned int bus, dev, func, idf, cls, sub, bar, cmd, caps;
  316. unsigned int base_mhz, div, ocr, rca, sec;
  317. unsigned char ext[512];
  318. unsigned char power;
  319. unsigned int i;
  320. unsigned int t0;
  321. unsigned short version, hc2;
  322. mmc.state = 0;
  323. if (pci_find_mmc(&bus, &dev, &func) != 0) {
  324. serial_puts(SERIAL_COM1, "sdhci: no Intel MMC controller\n");
  325. return -1;
  326. }
  327. idf = pci_cfg_read32(bus, dev, func, 0x00);
  328. cls = (pci_cfg_read32(bus, dev, func, 0x08) >> 24) & 0xFFu;
  329. sub = (pci_cfg_read32(bus, dev, func, 0x08) >> 16) & 0xFFu;
  330. serial_puts(SERIAL_COM1, "sdhci: PCI ");
  331. serial_putdec(SERIAL_COM1, bus);
  332. serial_putc(SERIAL_COM1, ':');
  333. serial_putdec(SERIAL_COM1, dev);
  334. serial_putc(SERIAL_COM1, '.');
  335. serial_putdec(SERIAL_COM1, func);
  336. serial_puts(SERIAL_COM1, " id ");
  337. serial_puthex(SERIAL_COM1, idf);
  338. serial_puts(SERIAL_COM1, " class ");
  339. serial_puthex(SERIAL_COM1, (cls << 8) | sub);
  340. serial_puts(SERIAL_COM1, "\n");
  341. bar = pci_cfg_read32(bus, dev, func, 0x10);
  342. if ((bar & 1u) || bar == 0 || bar == 0xFFFFFFFFu) {
  343. serial_puts(SERIAL_COM1, "sdhci: BAR0 is not a usable MMIO address\n");
  344. return -1;
  345. }
  346. if (((bar >> 1) & 3u) == 2u) {
  347. if (pci_cfg_read32(bus, dev, func, 0x14) != 0) {
  348. serial_puts(SERIAL_COM1, "sdhci: 64-bit BAR above 4 GB\n");
  349. return -1;
  350. }
  351. }
  352. bar &= ~0xFu;
  353. serial_puts(SERIAL_COM1, "sdhci: BAR0 ");
  354. serial_puthex(SERIAL_COM1, bar);
  355. serial_puts(SERIAL_COM1, "\n");
  356. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  357. pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0006u);
  358. mmc.bar = (unsigned int)ioremap(bar, 0x1000, IOREMAP_MMIO);
  359. if (!mmc.bar)
  360. return -1;
  361. mmc.state = 0;
  362. caps = rd32(SDHCI_CAPABILITIES);
  363. version = rd16(SDHCI_HOST_VERSION);
  364. base_mhz = (caps >> 8) & 0xFFu;
  365. if (!base_mhz)
  366. base_mhz = 50;
  367. mmc.base_khz = base_mhz * 1000u;
  368. serial_puts(SERIAL_COM1, "sdhci: caps 0x");
  369. serial_puthex(SERIAL_COM1, caps);
  370. serial_puts(SERIAL_COM1, ", version 0x");
  371. serial_puthex(SERIAL_COM1, version);
  372. serial_puts(SERIAL_COM1, ", base ");
  373. serial_putdec(SERIAL_COM1, base_mhz);
  374. serial_puts(SERIAL_COM1, " MHz");
  375. if (!((caps >> 8) & 0xFFu))
  376. serial_puts(SERIAL_COM1, " (default)");
  377. serial_puts(SERIAL_COM1, "\n");
  378. if (sdhci_reset_host() != 0) {
  379. serial_puts(SERIAL_COM1, "sdhci: host reset timeout\n");
  380. return -1;
  381. }
  382. power = 0x0F; /* bus power + 3.3 V */
  383. if (!(caps & (1u << 24))) {
  384. if (caps & (1u << 26))
  385. power = 0x0B; /* 1.8 V */
  386. else if (caps & (1u << 25))
  387. power = 0x0D; /* 3.0 V */
  388. }
  389. /* The Intel eMMC is often 1.8 V-only (caps bit 26): the power
  390. * control selects the rail, and Host Control 2 bit 3 enables 1.8 V
  391. * signalling. Clear the power register first and let the rail
  392. * settle before applying the clock. */
  393. wr8(SDHCI_POWER_CONTROL, 0);
  394. tiny_delay(10);
  395. wr8(SDHCI_POWER_CONTROL, power);
  396. hc2 = rd16(SDHCI_HOST_CONTROL2);
  397. if (power == 0x0B)
  398. hc2 |= SDHCI_CTRL_VDD_180;
  399. else
  400. hc2 &= (unsigned short)~SDHCI_CTRL_VDD_180;
  401. wr16(SDHCI_HOST_CONTROL2, hc2);
  402. tiny_delay(10);
  403. wr8(SDHCI_TIMEOUT_CONTROL, 0x0E);
  404. wr8(SDHCI_HOST_CONTROL, 0);
  405. /* Enable every status bit (we poll the status register) but leave the
  406. * interrupt signal disabled. With the status-enable bits clear the
  407. * controller keeps INT_STATUS at zero, which looked like a command
  408. * timeout on the first target probe. */
  409. wr16(SDHCI_INT_STATUS_EN, 0xFFFF);
  410. wr16(SDHCI_ERR_STATUS_EN, 0xFFFF);
  411. wr16(SDHCI_INT_SIGNAL_EN, 0);
  412. wr16(SDHCI_ERR_SIGNAL_EN, 0);
  413. wr32(SDHCI_INT_STATUS, 0xFFFFFFFFu);
  414. if (sdhci_clock_div(0xFF) != 0) {
  415. serial_puts(SERIAL_COM1, "sdhci: clock did not stabilise\n");
  416. return -1;
  417. }
  418. serial_puts(SERIAL_COM1, "sdhci: power 0x");
  419. serial_puthex(SERIAL_COM1, power);
  420. serial_puts(SERIAL_COM1, ", hc2 0x");
  421. serial_puthex(SERIAL_COM1, hc2);
  422. serial_puts(SERIAL_COM1, ", clock 0x");
  423. serial_puthex(SERIAL_COM1, rd16(SDHCI_CLOCK_CONTROL));
  424. serial_puts(SERIAL_COM1, ", state 0x");
  425. serial_puthex(SERIAL_COM1, rd32(SDHCI_PRESENT_STATE));
  426. serial_puts(SERIAL_COM1, "\n");
  427. if (sdhci_cmd(MMC_GO_IDLE_STATE, 0, SDHCI_CMD_RESP_NONE, 0, 0, 0) != 0)
  428. return -1;
  429. t0 = get_ticks_ms();
  430. ocr = 0;
  431. for (;;) {
  432. if (sdhci_cmd(MMC_SEND_OP_COND, MMC_OCR_ARG,
  433. SDHCI_CMD_RESP_SHORT, 0, 0, 0) == 0) {
  434. ocr = rd32(SDHCI_RESPONSE);
  435. if (ocr & 0x80000000u)
  436. break;
  437. }
  438. if (get_ticks_ms() - t0 > 2000) {
  439. serial_puts(SERIAL_COM1, "sdhci: CMD1 timeout, ocr 0x");
  440. serial_puthex(SERIAL_COM1, ocr);
  441. serial_puts(SERIAL_COM1, "\n");
  442. return -1;
  443. }
  444. /* The card can take up to ~1 s to leave its power-up busy
  445. * state: sleep between polls during the storage rescan
  446. * (tiny_delay(1) alone never reaches vfs_scan_poll's 2 ms). */
  447. if (!vfs_scan_relax(1))
  448. tiny_delay(1);
  449. }
  450. serial_puts(SERIAL_COM1, "sdhci: ocr 0x");
  451. serial_puthex(SERIAL_COM1, ocr);
  452. serial_puts(SERIAL_COM1, "\n");
  453. mmc.high_capacity = (ocr & 0x40000000u) ? 1 : 0;
  454. if (sdhci_cmd(MMC_ALL_SEND_CID, 0, SDHCI_CMD_RESP_LONG | SDHCI_CMD_CRC,
  455. 0, 0, 0) != 0)
  456. return -1;
  457. if (sdhci_cmd(MMC_SET_RELATIVE_ADDR, MMC_RCA << 16,
  458. SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
  459. 0, 0, 0) != 0)
  460. return -1;
  461. rca = (rd32(SDHCI_RESPONSE) >> 16) & 0xFFFFu;
  462. if (!rca)
  463. rca = MMC_RCA;
  464. if (sdhci_cmd(MMC_SELECT_CARD, rca << 16,
  465. SDHCI_CMD_RESP_BUSY | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
  466. 0, 0, 0) != 0)
  467. return -1;
  468. if (sdhci_cmd(MMC_SET_BLOCKLEN, 512,
  469. SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
  470. 0, 0, 0) != 0)
  471. return -1;
  472. for (i = 0; i < sizeof(ext); i++)
  473. ext[i] = 0;
  474. if (sdhci_cmd(MMC_SEND_EXT_CSD, 0,
  475. SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC | SDHCI_CMD_INDEX |
  476. SDHCI_CMD_DATA,
  477. 1, SDHCI_TRNS_READ, ext) != 0)
  478. return -1;
  479. sec = (unsigned int)ext[212] | ((unsigned int)ext[213] << 8) |
  480. ((unsigned int)ext[214] << 16) | ((unsigned int)ext[215] << 24);
  481. if (sec == 0 || sec == 0xFFFFFFFFu) {
  482. serial_puts(SERIAL_COM1, "sdhci: EXT_CSD has no sector count\n");
  483. return -1;
  484. }
  485. mmc.cache_size = (unsigned int)ext[249] |
  486. ((unsigned int)ext[250] << 8) |
  487. ((unsigned int)ext[251] << 16) |
  488. ((unsigned int)ext[252] << 24);
  489. div = mmc.base_khz / 50000u; /* 25 MHz: base / (2 * 25000) */
  490. if (!((caps >> 8) & 0xFFu))
  491. div = 4; /* conservative if the base is unknown */
  492. if (sdhci_clock_div(div) != 0) {
  493. serial_puts(SERIAL_COM1, "sdhci: 25 MHz clock did not stabilise\n");
  494. return -1;
  495. }
  496. mmc.sectors = sec;
  497. mmc.high_capacity = mmc.high_capacity ? 1 : 0;
  498. for (i = 0; i < sizeof(mmc.model) - 1; i++)
  499. mmc.model[i] = 0;
  500. {
  501. static const char name[] = "Intel eMMC";
  502. for (i = 0; name[i] && i < sizeof(mmc.model) - 1; i++)
  503. mmc.model[i] = name[i];
  504. }
  505. mmc.state = 1;
  506. serial_puts(SERIAL_COM1, "sdhci: eMMC ");
  507. serial_putdec(SERIAL_COM1, (sec / 2048u) / 1024u);
  508. serial_puts(SERIAL_COM1, " GiB, ");
  509. serial_putdec(SERIAL_COM1, sec);
  510. serial_puts(SERIAL_COM1, " sectors, ");
  511. serial_puts(SERIAL_COM1, mmc.high_capacity ? "block" : "byte");
  512. serial_puts(SERIAL_COM1, " addressing\n");
  513. return 0;
  514. }
  515. unsigned int sdhci_scan(struct sdhci_disk *out, unsigned int max)
  516. {
  517. unsigned int i;
  518. if (mmc.state < 0)
  519. (void)sdhci_init();
  520. if (mmc.state <= 0 || max == 0)
  521. return 0;
  522. out[0].sectors = mmc.sectors;
  523. for (i = 0; i < sizeof(out[0].model) - 1 && mmc.model[i]; i++)
  524. out[0].model[i] = mmc.model[i];
  525. out[0].model[i] = '\0';
  526. return 1;
  527. }
  528. int sdhci_read_sectors(unsigned int lba, void *buf, unsigned int count)
  529. {
  530. unsigned int i;
  531. if (mmc.state <= 0)
  532. return -1;
  533. for (i = 0; i < count; i++) {
  534. unsigned int arg = mmc.high_capacity ? (lba + i) :
  535. (lba + i) * 512u;
  536. if (sdhci_cmd(MMC_READ_SINGLE_BLOCK, arg,
  537. SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC |
  538. SDHCI_CMD_INDEX | SDHCI_CMD_DATA,
  539. 1, SDHCI_TRNS_READ,
  540. (unsigned char *)buf + i * 512u) != 0)
  541. return -1;
  542. /* The polled PIO path is slow; give the WM/terminal a slice
  543. * during a long format, which runs outside the filesystem's
  544. * non-preemptible section. A normal file read disables
  545. * preemption, so the yield is a no-op there. */
  546. if ((i & 31u) == 31u)
  547. sched_yield();
  548. }
  549. return 0;
  550. }
  551. int sdhci_write_sectors(unsigned int lba, const void *buf, unsigned int count)
  552. {
  553. unsigned int i;
  554. if (mmc.state <= 0)
  555. return -1;
  556. for (i = 0; i < count; i++) {
  557. unsigned int arg = mmc.high_capacity ? (lba + i) :
  558. (lba + i) * 512u;
  559. if (sdhci_cmd(MMC_WRITE_SINGLE_BLOCK, arg,
  560. SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC |
  561. SDHCI_CMD_INDEX | SDHCI_CMD_DATA,
  562. 1, 0,
  563. (void *)((const unsigned char *)buf + i * 512u)) != 0)
  564. return -1;
  565. if ((i & 31u) == 31u)
  566. sched_yield();
  567. }
  568. return 0;
  569. }
  570. int sdhci_flush(void)
  571. {
  572. /* eMMC CACHE_SIZE (EXT_CSD[249..252]) is non-zero when the card has
  573. * a write-back cache. EXT_CSD_FLUSH_CACHE (index 32) with value 1
  574. * commits it; failures are harmless (the card then has no cache). */
  575. if (mmc.state <= 0 || !mmc.cache_size)
  576. return 0;
  577. (void)sdhci_cmd(MMC_SWITCH, 0x0C200100u,
  578. SDHCI_CMD_RESP_BUSY | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
  579. 0, 0, 0);
  580. return 0;
  581. }