sdhci.c 17 KB

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