sdhci.c 18 KB

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