sdhci.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. }
  214. wr32(SDHCI_INT_STATUS, SDHCI_INT_CMD_COMPLETE);
  215. if (flags & SDHCI_CMD_DATA) {
  216. if (sdhci_pio(buf, blocks ? blocks : 1,
  217. (mode & SDHCI_TRNS_READ) ? 0 : 1) != 0) {
  218. log_fail("pio", cmd, rd32(SDHCI_INT_STATUS));
  219. return -1;
  220. }
  221. t0 = get_ticks_ms();
  222. for (;;) {
  223. sts = rd32(SDHCI_INT_STATUS);
  224. if (sts & 0xFFFF0000u) {
  225. log_fail("transfer", cmd, sts);
  226. return -1;
  227. }
  228. if (sts & SDHCI_INT_TRANS_COMPLETE)
  229. break;
  230. if (get_ticks_ms() - t0 > SDHCI_DATA_TIMEOUT_MS) {
  231. log_fail("transfer timeout", cmd, sts);
  232. return -1;
  233. }
  234. }
  235. wr32(SDHCI_INT_STATUS, 0xFFFFFFFFu);
  236. }
  237. return 0;
  238. }
  239. static int sdhci_clock_div(unsigned int div)
  240. {
  241. unsigned int t0;
  242. if (div < 1)
  243. div = 1;
  244. if (div > 0xFF)
  245. div = 0xFF;
  246. wr16(SDHCI_CLOCK_CONTROL, 0);
  247. wr16(SDHCI_CLOCK_CONTROL,
  248. (unsigned short)((div << 8) | SDHCI_CLOCK_INT_EN));
  249. t0 = get_ticks_ms();
  250. while (!(rd16(SDHCI_CLOCK_CONTROL) & SDHCI_CLOCK_INT_STABLE)) {
  251. if (get_ticks_ms() - t0 > 100)
  252. return -1;
  253. }
  254. wr16(SDHCI_CLOCK_CONTROL,
  255. (unsigned short)((div << 8) | SDHCI_CLOCK_INT_EN |
  256. SDHCI_CLOCK_CARD_EN));
  257. tiny_delay(2);
  258. return 0;
  259. }
  260. static int sdhci_reset_host(void)
  261. {
  262. unsigned int t0;
  263. wr8(SDHCI_SOFTWARE_RESET, SDHCI_RESET_ALL);
  264. t0 = get_ticks_ms();
  265. while (rd8(SDHCI_SOFTWARE_RESET) & SDHCI_RESET_ALL) {
  266. if (get_ticks_ms() - t0 > 100)
  267. return -1;
  268. }
  269. return 0;
  270. }
  271. static int pci_find_mmc(unsigned int *bus, unsigned int *dev,
  272. unsigned int *func)
  273. {
  274. unsigned int b, d, f;
  275. for (b = 0; b < 256; b++) {
  276. for (d = 0; d < 32; d++) {
  277. unsigned int id = pci_cfg_read32(b, d, 0, 0x00);
  278. unsigned int htype, nfunc;
  279. if (id == 0xFFFFFFFFu || id == 0)
  280. continue;
  281. htype = (pci_cfg_read32(b, d, 0, 0x0C) >> 16) & 0xFFu;
  282. nfunc = (htype & 0x80u) ? 8u : 1u;
  283. for (f = 0; f < nfunc; f++) {
  284. unsigned int idf = pci_cfg_read32(b, d, f, 0x00);
  285. unsigned int vendor = idf & 0xFFFFu;
  286. unsigned int device = idf >> 16;
  287. unsigned int crev, cls, sub;
  288. if (idf == 0xFFFFFFFFu || idf == 0)
  289. continue;
  290. crev = pci_cfg_read32(b, d, f, 0x08);
  291. cls = (crev >> 24) & 0xFFu;
  292. sub = (crev >> 16) & 0xFFu;
  293. if (vendor != MMC_PCI_VENDOR)
  294. continue;
  295. if (device == MMC_PCI_DEVICE ||
  296. (device != MMC_PCI_DEVICE_SDIO &&
  297. device != MMC_PCI_DEVICE_SD &&
  298. cls == 0x08 && sub == 0x05)) {
  299. *bus = b;
  300. *dev = d;
  301. *func = f;
  302. return 0;
  303. }
  304. }
  305. }
  306. }
  307. return -1;
  308. }
  309. static int sdhci_init(void)
  310. {
  311. unsigned int bus, dev, func, idf, cls, sub, bar, cmd, caps;
  312. unsigned int base_mhz, div, ocr, rca, sec;
  313. unsigned char ext[512];
  314. unsigned char power;
  315. unsigned int i;
  316. unsigned int t0;
  317. unsigned short version, hc2;
  318. mmc.state = 0;
  319. if (pci_find_mmc(&bus, &dev, &func) != 0) {
  320. serial_puts(SERIAL_COM1, "sdhci: no Intel MMC controller\n");
  321. return -1;
  322. }
  323. idf = pci_cfg_read32(bus, dev, func, 0x00);
  324. cls = (pci_cfg_read32(bus, dev, func, 0x08) >> 24) & 0xFFu;
  325. sub = (pci_cfg_read32(bus, dev, func, 0x08) >> 16) & 0xFFu;
  326. serial_puts(SERIAL_COM1, "sdhci: PCI ");
  327. serial_putdec(SERIAL_COM1, bus);
  328. serial_putc(SERIAL_COM1, ':');
  329. serial_putdec(SERIAL_COM1, dev);
  330. serial_putc(SERIAL_COM1, '.');
  331. serial_putdec(SERIAL_COM1, func);
  332. serial_puts(SERIAL_COM1, " id ");
  333. serial_puthex(SERIAL_COM1, idf);
  334. serial_puts(SERIAL_COM1, " class ");
  335. serial_puthex(SERIAL_COM1, (cls << 8) | sub);
  336. serial_puts(SERIAL_COM1, "\n");
  337. bar = pci_cfg_read32(bus, dev, func, 0x10);
  338. if ((bar & 1u) || bar == 0 || bar == 0xFFFFFFFFu) {
  339. serial_puts(SERIAL_COM1, "sdhci: BAR0 is not a usable MMIO address\n");
  340. return -1;
  341. }
  342. if (((bar >> 1) & 3u) == 2u) {
  343. if (pci_cfg_read32(bus, dev, func, 0x14) != 0) {
  344. serial_puts(SERIAL_COM1, "sdhci: 64-bit BAR above 4 GB\n");
  345. return -1;
  346. }
  347. }
  348. bar &= ~0xFu;
  349. serial_puts(SERIAL_COM1, "sdhci: BAR0 ");
  350. serial_puthex(SERIAL_COM1, bar);
  351. serial_puts(SERIAL_COM1, "\n");
  352. process_map_mmio_all(bar, 0x1000);
  353. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  354. pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0006u);
  355. mmc.bar = bar;
  356. mmc.state = 0;
  357. caps = rd32(SDHCI_CAPABILITIES);
  358. version = rd16(SDHCI_HOST_VERSION);
  359. base_mhz = (caps >> 8) & 0xFFu;
  360. if (!base_mhz)
  361. base_mhz = 50;
  362. mmc.base_khz = base_mhz * 1000u;
  363. serial_puts(SERIAL_COM1, "sdhci: caps 0x");
  364. serial_puthex(SERIAL_COM1, caps);
  365. serial_puts(SERIAL_COM1, ", version 0x");
  366. serial_puthex(SERIAL_COM1, version);
  367. serial_puts(SERIAL_COM1, ", base ");
  368. serial_putdec(SERIAL_COM1, base_mhz);
  369. serial_puts(SERIAL_COM1, " MHz");
  370. if (!((caps >> 8) & 0xFFu))
  371. serial_puts(SERIAL_COM1, " (default)");
  372. serial_puts(SERIAL_COM1, "\n");
  373. if (sdhci_reset_host() != 0) {
  374. serial_puts(SERIAL_COM1, "sdhci: host reset timeout\n");
  375. return -1;
  376. }
  377. power = 0x0F; /* bus power + 3.3 V */
  378. if (!(caps & (1u << 24))) {
  379. if (caps & (1u << 26))
  380. power = 0x0B; /* 1.8 V */
  381. else if (caps & (1u << 25))
  382. power = 0x0D; /* 3.0 V */
  383. }
  384. /* The Intel eMMC is often 1.8 V-only (caps bit 26): the power
  385. * control selects the rail, and Host Control 2 bit 3 enables 1.8 V
  386. * signalling. Clear the power register first and let the rail
  387. * settle before applying the clock. */
  388. wr8(SDHCI_POWER_CONTROL, 0);
  389. tiny_delay(10);
  390. wr8(SDHCI_POWER_CONTROL, power);
  391. hc2 = rd16(SDHCI_HOST_CONTROL2);
  392. if (power == 0x0B)
  393. hc2 |= SDHCI_CTRL_VDD_180;
  394. else
  395. hc2 &= (unsigned short)~SDHCI_CTRL_VDD_180;
  396. wr16(SDHCI_HOST_CONTROL2, hc2);
  397. tiny_delay(10);
  398. wr8(SDHCI_TIMEOUT_CONTROL, 0x0E);
  399. wr8(SDHCI_HOST_CONTROL, 0);
  400. /* Enable every status bit (we poll the status register) but leave the
  401. * interrupt signal disabled. With the status-enable bits clear the
  402. * controller keeps INT_STATUS at zero, which looked like a command
  403. * timeout on the first target probe. */
  404. wr16(SDHCI_INT_STATUS_EN, 0xFFFF);
  405. wr16(SDHCI_ERR_STATUS_EN, 0xFFFF);
  406. wr16(SDHCI_INT_SIGNAL_EN, 0);
  407. wr16(SDHCI_ERR_SIGNAL_EN, 0);
  408. wr32(SDHCI_INT_STATUS, 0xFFFFFFFFu);
  409. if (sdhci_clock_div(0xFF) != 0) {
  410. serial_puts(SERIAL_COM1, "sdhci: clock did not stabilise\n");
  411. return -1;
  412. }
  413. serial_puts(SERIAL_COM1, "sdhci: power 0x");
  414. serial_puthex(SERIAL_COM1, power);
  415. serial_puts(SERIAL_COM1, ", hc2 0x");
  416. serial_puthex(SERIAL_COM1, hc2);
  417. serial_puts(SERIAL_COM1, ", clock 0x");
  418. serial_puthex(SERIAL_COM1, rd16(SDHCI_CLOCK_CONTROL));
  419. serial_puts(SERIAL_COM1, ", state 0x");
  420. serial_puthex(SERIAL_COM1, rd32(SDHCI_PRESENT_STATE));
  421. serial_puts(SERIAL_COM1, "\n");
  422. if (sdhci_cmd(MMC_GO_IDLE_STATE, 0, SDHCI_CMD_RESP_NONE, 0, 0, 0) != 0)
  423. return -1;
  424. t0 = get_ticks_ms();
  425. ocr = 0;
  426. for (;;) {
  427. if (sdhci_cmd(MMC_SEND_OP_COND, MMC_OCR_ARG,
  428. SDHCI_CMD_RESP_SHORT, 0, 0, 0) == 0) {
  429. ocr = rd32(SDHCI_RESPONSE);
  430. if (ocr & 0x80000000u)
  431. break;
  432. }
  433. if (get_ticks_ms() - t0 > 2000) {
  434. serial_puts(SERIAL_COM1, "sdhci: CMD1 timeout, ocr 0x");
  435. serial_puthex(SERIAL_COM1, ocr);
  436. serial_puts(SERIAL_COM1, "\n");
  437. return -1;
  438. }
  439. tiny_delay(1);
  440. }
  441. serial_puts(SERIAL_COM1, "sdhci: ocr 0x");
  442. serial_puthex(SERIAL_COM1, ocr);
  443. serial_puts(SERIAL_COM1, "\n");
  444. mmc.high_capacity = (ocr & 0x40000000u) ? 1 : 0;
  445. if (sdhci_cmd(MMC_ALL_SEND_CID, 0, SDHCI_CMD_RESP_LONG | SDHCI_CMD_CRC,
  446. 0, 0, 0) != 0)
  447. return -1;
  448. if (sdhci_cmd(MMC_SET_RELATIVE_ADDR, MMC_RCA << 16,
  449. SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
  450. 0, 0, 0) != 0)
  451. return -1;
  452. rca = (rd32(SDHCI_RESPONSE) >> 16) & 0xFFFFu;
  453. if (!rca)
  454. rca = MMC_RCA;
  455. if (sdhci_cmd(MMC_SELECT_CARD, rca << 16,
  456. SDHCI_CMD_RESP_BUSY | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
  457. 0, 0, 0) != 0)
  458. return -1;
  459. if (sdhci_cmd(MMC_SET_BLOCKLEN, 512,
  460. SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
  461. 0, 0, 0) != 0)
  462. return -1;
  463. for (i = 0; i < sizeof(ext); i++)
  464. ext[i] = 0;
  465. if (sdhci_cmd(MMC_SEND_EXT_CSD, 0,
  466. SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC | SDHCI_CMD_INDEX |
  467. SDHCI_CMD_DATA,
  468. 1, SDHCI_TRNS_READ, ext) != 0)
  469. return -1;
  470. sec = (unsigned int)ext[212] | ((unsigned int)ext[213] << 8) |
  471. ((unsigned int)ext[214] << 16) | ((unsigned int)ext[215] << 24);
  472. if (sec == 0 || sec == 0xFFFFFFFFu) {
  473. serial_puts(SERIAL_COM1, "sdhci: EXT_CSD has no sector count\n");
  474. return -1;
  475. }
  476. mmc.cache_size = (unsigned int)ext[249] |
  477. ((unsigned int)ext[250] << 8) |
  478. ((unsigned int)ext[251] << 16) |
  479. ((unsigned int)ext[252] << 24);
  480. div = mmc.base_khz / 50000u; /* 25 MHz: base / (2 * 25000) */
  481. if (!((caps >> 8) & 0xFFu))
  482. div = 4; /* conservative if the base is unknown */
  483. if (sdhci_clock_div(div) != 0) {
  484. serial_puts(SERIAL_COM1, "sdhci: 25 MHz clock did not stabilise\n");
  485. return -1;
  486. }
  487. mmc.sectors = sec;
  488. mmc.high_capacity = mmc.high_capacity ? 1 : 0;
  489. for (i = 0; i < sizeof(mmc.model) - 1; i++)
  490. mmc.model[i] = 0;
  491. {
  492. static const char name[] = "Intel eMMC";
  493. for (i = 0; name[i] && i < sizeof(mmc.model) - 1; i++)
  494. mmc.model[i] = name[i];
  495. }
  496. mmc.state = 1;
  497. serial_puts(SERIAL_COM1, "sdhci: eMMC ");
  498. serial_putdec(SERIAL_COM1, (sec / 2048u) / 1024u);
  499. serial_puts(SERIAL_COM1, " GiB, ");
  500. serial_putdec(SERIAL_COM1, sec);
  501. serial_puts(SERIAL_COM1, " sectors, ");
  502. serial_puts(SERIAL_COM1, mmc.high_capacity ? "block" : "byte");
  503. serial_puts(SERIAL_COM1, " addressing\n");
  504. return 0;
  505. }
  506. unsigned int sdhci_scan(struct sdhci_disk *out, unsigned int max)
  507. {
  508. unsigned int i;
  509. if (mmc.state < 0)
  510. (void)sdhci_init();
  511. if (mmc.state <= 0 || max == 0)
  512. return 0;
  513. out[0].sectors = mmc.sectors;
  514. for (i = 0; i < sizeof(out[0].model) - 1 && mmc.model[i]; i++)
  515. out[0].model[i] = mmc.model[i];
  516. out[0].model[i] = '\0';
  517. return 1;
  518. }
  519. int sdhci_read_sectors(unsigned int lba, void *buf, unsigned int count)
  520. {
  521. unsigned int i;
  522. if (mmc.state <= 0)
  523. return -1;
  524. for (i = 0; i < count; i++) {
  525. unsigned int arg = mmc.high_capacity ? (lba + i) :
  526. (lba + i) * 512u;
  527. if (sdhci_cmd(MMC_READ_SINGLE_BLOCK, arg,
  528. SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC |
  529. SDHCI_CMD_INDEX | SDHCI_CMD_DATA,
  530. 1, SDHCI_TRNS_READ,
  531. (unsigned char *)buf + i * 512u) != 0)
  532. return -1;
  533. /* The polled PIO path is slow; give the WM/terminal a slice
  534. * during a long format, which runs outside the filesystem's
  535. * non-preemptible section. A normal file read disables
  536. * preemption, so the yield is a no-op there. */
  537. if ((i & 31u) == 31u)
  538. sched_yield();
  539. }
  540. return 0;
  541. }
  542. int sdhci_write_sectors(unsigned int lba, const void *buf, unsigned int count)
  543. {
  544. unsigned int i;
  545. if (mmc.state <= 0)
  546. return -1;
  547. for (i = 0; i < count; i++) {
  548. unsigned int arg = mmc.high_capacity ? (lba + i) :
  549. (lba + i) * 512u;
  550. if (sdhci_cmd(MMC_WRITE_SINGLE_BLOCK, arg,
  551. SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC |
  552. SDHCI_CMD_INDEX | SDHCI_CMD_DATA,
  553. 1, 0,
  554. (void *)((const unsigned char *)buf + i * 512u)) != 0)
  555. return -1;
  556. if ((i & 31u) == 31u)
  557. sched_yield();
  558. }
  559. return 0;
  560. }
  561. int sdhci_flush(void)
  562. {
  563. /* eMMC CACHE_SIZE (EXT_CSD[249..252]) is non-zero when the card has
  564. * a write-back cache. EXT_CSD_FLUSH_CACHE (index 32) with value 1
  565. * commits it; failures are harmless (the card then has no cache). */
  566. if (mmc.state <= 0 || !mmc.cache_size)
  567. return 0;
  568. (void)sdhci_cmd(MMC_SWITCH, 0x0C200100u,
  569. SDHCI_CMD_RESP_BUSY | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
  570. 0, 0, 0);
  571. return 0;
  572. }