Procházet zdrojové kódy

Add eMMC/SDHCI storage support

Drive the Intel Braswell MMC function at PCI 00:10.0 (8086:2294) with a
polled SDHCI implementation.  It resets the host, selects the 1.8 V rail
and Host Control 2 signalling, runs the 400 kHz CMD0/1/2/3/7/16 and CMD8
EXT_CSD identification, and uses CMD17/CMD24 PIO at 1-bit/25 MHz for
sector I/O.  EXT_CSD SEC_COUNT provides the geometry and CACHE_SIZE
drives the CMD6 FLUSH_CACHE on blk_flush().

Register the card as mmc0 so storage, the VFS MBR+FAT mount path and
format mmc0 work like IDE/SATA.  Validated on the HP Chromebook 11 G5:
storage reports the 14.6 GB eMMC and format mmc0 creates the FAT32
partition.

Update AGENTS.md and the architecture pages; the eMMC driver is polled
PIO only, with no DMA/IRQ, bus-width switch or hotplug.
Johannes Zellner před 1 týdnem
rodič
revize
e7e6e1d048
10 změnil soubory, kde provedl 739 přidání a 16 odebrání
  1. 22 1
      AGENTS.md
  2. 6 3
      Makefile
  3. 31 1
      blkdev.c
  4. 3 2
      blkdev.h
  5. 25 6
      docs/drivers.html
  6. 1 1
      docs/filesystems.html
  7. 1 1
      docs/index.html
  8. 626 0
      sdhci.c
  9. 23 0
      sdhci.h
  10. 1 1
      syscall.h

+ 22 - 1
AGENTS.md

@@ -439,7 +439,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   see the panic section below.
 - **Filesystem**: `fat.c` (FAT12/16/32 read/write, device-parameterized),
   `vfs.c` (mounts every FAT device under `/`), `fatdetect.c`/`fatformat.c`,
-  `blkdev.c` (floppy + IDE + SATA registry), `ata.c` (ATA PIO, either the
+  `blkdev.c` (floppy + IDE + SATA + eMMC registry), `ata.c` (ATA PIO, either the
   legacy ports or the PCI IDE interface's native BARs — `ata_init_ports()`;
   multi-sector PIO waits for DRQ before every sector, since drives drop it
   between sectors, polls the command-block status rather than the alt-status
@@ -477,6 +477,19 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   as `sata0`..`sataN` (named by AHCI port index) and mount like IDE disks
   (`sata0p1` for an MBR partition). >2 TB disks are clamped to 32-bit LBA;
   a disk added after boot needs a rescan (no COMRESET hotplug).
+- **eMMC/SDHCI**: `sdhci.c/h` — polled SDHCI driver for the Intel Braswell
+  MMC function at PCI `00:10.0` (8086:2294, class 0805, the HP Chromebook
+  eMMC). `sdhci_init()` resets the host, selects the 1.8 V rail from the
+  capabilities and enables Host Control 2's 1.8 V signalling, runs a 400 kHz
+  init clock, then CMD0/1/2/3/7/16 and CMD8 to read the 512-byte EXT_CSD
+  (`SEC_COUNT` at 212, `CACHE_SIZE` at 249), and switches to a 25 MHz data
+  clock. Sector I/O is CMD17/CMD24 over the polled PIO buffer port (one
+  512-byte block per command; no DMA, IRQ, CMD6 bus-width/high-speed switch
+  or hotplug). `blkdev.c` registers it as `mmc0`, so `storage`, the VFS MBR+
+  FAT mount path and `format mmc0` work exactly like an IDE/SATA disk;
+  `sdhci_flush()` issues the CMD6 `FLUSH_CACHE` when the card reports a
+  write-back cache. A stock eMMC holds the ChromeOS image, so formatting it
+  is destructive.
 - **Serial**: `serial.c`, `serial_ring_buf.c/h`, `serial_loop.c` — 16550 UART
   with an IRQ-driven ring buffer and the `serial test` loopback check.
 - **Network**: `netdev.c/h` — NIC driver registry (`struct netdev`, first probe
@@ -1617,3 +1630,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     of 0xffff.  The lid/power-button switches are read by `sensors` but do
     not trigger a power-off or display blank; suspend/S0ix is not
     implemented.
+
+31. eMMC/SDHCI: the Chromebook eMMC is driven polled at 1-bit/25 MHz, with
+    no DMA, IRQ, CMD6 bus-width/high-speed/DDR switch or hotplug; only the
+    eMMC function (8086:2294) is handled, not the SDIO/SD functions or the
+    SD card reader.  The write path uses CMD24 PIO and the format command's
+    cache flush uses CMD6 FLUSH_CACHE when EXT_CSD CACHE_SIZE is non-zero.
+    The eMMC contains the stock ChromeOS image, so `format mmc0` is
+    destructive; the board boots from USB.

+ 6 - 3
Makefile

@@ -265,9 +265,12 @@ netdev.o: netdev.c netdev.h ne2000.h r8168.h e1000e.h
 net.o: net.c net.h io.h netdev.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-blkdev.o: blkdev.c blkdev.h ata.h ahci.h disk.h
+blkdev.o: blkdev.c blkdev.h ata.h ahci.h disk.h sdhci.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
+sdhci.o: sdhci.c sdhci.h paging.h pci.h process.h serial.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 ata.o: ata.c ata.h pci.h serial.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -385,7 +388,7 @@ sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h nt
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o inflate.o ramfs.o share.o payload.o
+kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o sdhci.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o inflate.o ramfs.o share.o payload.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $(filter-out kernel.ld,$^)
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -1171,4 +1174,4 @@ kernel-net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o disk.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img hello.elf cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h libc.a libc/*.o malloc_test format_test math_test div64_test setjmp_test mujs_test js_browser.o $(MUJS_DIR)/mujs.o ufont.o ufont_ttf.o ufont_draw.o ufont_file.o ufont_test ufont_test_asan
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o disk.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o sdhci.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img hello.elf cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h libc.a libc/*.o malloc_test format_test math_test div64_test setjmp_test mujs_test js_browser.o $(MUJS_DIR)/mujs.o ufont.o ufont_ttf.o ufont_draw.o ufont_file.o ufont_test ufont_test_asan

+ 31 - 1
blkdev.c

@@ -1,9 +1,11 @@
-/* Block device registry: floppy (via FDC) + IDE (via ATA PIO) + SATA (AHCI). */
+/* Block device registry: floppy (via FDC) + IDE (via ATA PIO) + SATA (AHCI)
+ * + eMMC (SDHCI). */
 
 #include "blkdev.h"
 #include "ahci.h"
 #include "ata.h"
 #include "disk.h"
+#include "sdhci.h"
 #include "serial.h"
 #include "timer.h"
 
@@ -130,6 +132,28 @@ void blk_scan(void)
 			d->model[40] = '\0';
 		}
 	}
+
+	/* eMMC: the Intel Braswell SDHCI controller, one soldered card. */
+	{
+		struct sdhci_disk disk;
+		unsigned int n;
+
+		t0 = get_ticks_ms();
+		n = sdhci_scan(&disk, 1);
+		blk_trace("mmc0", n ? 1 : 0, get_ticks_ms() - t0);
+		if (n) {
+			unsigned int j;
+
+			d = add("mmc0", BLK_MMC, 0, 0);
+			if (d) {
+				d->present = 1;
+				d->total_sectors = disk.sectors;
+				for (j = 0; j < 40; j++)
+					d->model[j] = disk.model[j];
+				d->model[40] = '\0';
+			}
+		}
+	}
 }
 
 static int name_eq(const char *a, const char *b)
@@ -162,6 +186,8 @@ int blk_read(blk_dev_t *d, unsigned int lba, void *buf, unsigned int count)
 	}
 	if (d->kind == BLK_SATA)
 		return ahci_read_sectors(d->unit, lba, buf, count);
+	if (d->kind == BLK_MMC)
+		return sdhci_read_sectors(lba, buf, count);
 	return ata_read_sectors(d->bus, d->unit, lba, buf, count);
 }
 
@@ -175,6 +201,8 @@ int blk_write(blk_dev_t *d, unsigned int lba, const void *buf, unsigned int coun
 	}
 	if (d->kind == BLK_SATA)
 		return ahci_write_sectors(d->unit, lba, buf, count);
+	if (d->kind == BLK_MMC)
+		return sdhci_write_sectors(lba, buf, count);
 	return ata_write_sectors(d->bus, d->unit, lba, buf, count);
 }
 
@@ -186,5 +214,7 @@ int blk_flush(blk_dev_t *d)
 		return 0;
 	if (d->kind == BLK_SATA)
 		return ahci_flush(d->unit);
+	if (d->kind == BLK_MMC)
+		return sdhci_flush();
 	return ata_flush(d->bus, d->unit);
 }

+ 3 - 2
blkdev.h

@@ -3,10 +3,11 @@
 #ifndef BLKDEV_H
 #define BLKDEV_H
 
-typedef enum { BLK_FLOPPY, BLK_IDE, BLK_SATA } blk_kind_t;
+typedef enum { BLK_FLOPPY, BLK_IDE, BLK_SATA, BLK_MMC } blk_kind_t;
 
 typedef struct {
-	const char *name;          /* "floppy0","floppy1","ide0".."ide3","sata0".. */
+	const char *name;          /* "floppy0","floppy1","ide0".."ide3",
+				    * "sata0".., "mmc0" */
 	blk_kind_t kind;
 	unsigned int bus;          /* floppy: drive #; ide: 0=primary,1=secondary;
 				    * sata: controller index (0) */

+ 25 - 6
docs/drivers.html

@@ -81,6 +81,7 @@
 					<tr><td>Floppy</td><td><span class="file">disk.c</span></td><td>ISA FDC + DMA channel 2</td><td>IRQ 6; FAT operations set <code>sys_fs_busy</code></td></tr>
 					<tr><td>PATA</td><td><span class="file">ata.c</span></td><td>Legacy ports or PCI native BARs</td><td>Polled PIO, DRQ wait per sector</td></tr>
 					<tr><td>SATA</td><td><span class="file">ahci.c</span></td><td>PCI AHCI MMIO</td><td>Polled, READ/WRITE DMA EXT</td></tr>
+					<tr><td>eMMC</td><td><span class="file">sdhci.c</span></td><td>Intel Braswell SDHCI, PCI <code>00:10.0</code> (<code>8086:2294</code>)</td><td>Polled PIO, 1-bit/25 MHz; no DMA/IRQ</td></tr>
 					<tr><td>USB</td><td><span class="file">usb.c</span> + <span class="file">uhci.c</span> <span class="file">ohci.c</span> <span class="file">ehci.c</span> <span class="file">xhci.c</span></td><td>PCI I/O ports or MMIO</td><td>Polled; hotplug watcher in the WM loop</td></tr>
 					<tr><td>Network</td><td><span class="file">e1000e.c</span> <span class="file">r8168.c</span> <span class="file">ne2000.c</span> <span class="file">asix.c</span></td><td>PCI or USB</td><td>Polled, one active receiver</td></tr>
 					<tr><td>PS/2</td><td><span class="file">mouse.c</span>, <span class="file">io.c</span></td><td>i8042</td><td>IRQ 1 keyboard, IRQ 12 mouse; polling fallback</td></tr>
@@ -118,7 +119,7 @@
 			devices. Each device exposes sector read/write plus geometry; FAT and
 			the format command sit above that interface and never talk to a
 			controller directly. Devices are named <code>floppy0</code>,
-			<code>ide0</code>, <code>sata0</code> and so on; partitions append
+			<code>ide0</code>, <code>sata0</code>, <code>mmc0</code> and so on; partitions append
 			<code>p1</code>, <code>p2</code>.
 		</p>
 
@@ -137,7 +138,7 @@
 
 				<rect class="svg-box" x="650" y="30" width="230" height="62" rx="9"/>
 				<text class="svg-text" x="670" y="57">driver</text>
-				<text class="svg-sub" x="670" y="77">FDC · ATA · AHCI</text>
+				<text class="svg-sub" x="670" y="77">FDC · ATA · AHCI · SDHCI</text>
 
 				<path class="svg-line blue" d="M270 61h67"/>
 				<path class="svg-line blue" d="M330 55l7 6-7 6"/>
@@ -153,8 +154,8 @@
 				<text class="svg-sub" x="365" y="189">PIO, legacy or native BARs</text>
 
 				<rect class="svg-box green" x="650" y="142" width="230" height="62" rx="9"/>
-				<text class="svg-text" x="670" y="169">sata0 / sata0pN</text>
-				<text class="svg-sub" x="670" y="189">AHCI port index</text>
+				<text class="svg-text" x="670" y="169">sata0 · mmc0</text>
+				<text class="svg-sub" x="670" y="189">AHCI port · SDHCI PIO</text>
 
 				<path class="svg-line" d="M175 92v42"/>
 				<path class="svg-line" d="M169 126l6 7 6-7"/>
@@ -165,8 +166,8 @@
 			</svg>
 			<figcaption>
 				<strong>One narrow waist.</strong> Filesystems and tools only see
-				<code>struct blk_dev</code>. The floppy still needs IRQ 6, but ATA
-				and AHCI are polled; a FAT operation takes <code>sys_fs_busy</code>
+				<code>struct blk_dev</code>. The floppy still needs IRQ 6, but ATA,
+				AHCI and SDHCI are polled; a FAT operation takes <code>sys_fs_busy</code>
 				so the scheduler does not switch away mid-transfer.
 			</figcaption>
 		</figure>
@@ -194,6 +195,22 @@
 			through a page-allocated bounce buffer. A disk added after boot needs
 			a rescan; there is no COMRESET hotplug.
 		</p>
+		<h3>eMMC/SDHCI</h3>
+		<p>
+			<span class="file">sdhci.c</span> drives the Intel Braswell MMC
+			function at PCI <code>00:10.0</code> (<code>8086:2294</code>, class
+			<code>0805</code>), the HP Chromebook's eMMC. It resets the SDHCI
+			host, selects the 1.8 V rail and Host Control 2's 1.8 V signalling,
+			runs a 400 kHz initialization clock, then CMD0/1/2/3/7/16 and CMD8
+			to read EXT_CSD (<code>SEC_COUNT</code> at 212,
+			<code>CACHE_SIZE</code> at 249), and switches to a 25 MHz data clock.
+			Sector I/O is CMD17/CMD24 through the polled PIO buffer port at
+			1-bit/25 MHz; there is no DMA, IRQ, bus-width/high-speed switch or
+			hotplug. The eMMC appears as <code>mmc0</code>, and
+			<code>format mmc0</code> writes an MBR + FAT partition exactly like
+			an IDE/SATA disk. The write path flushes the card cache with CMD6
+			<code>FLUSH_CACHE</code> when EXT_CSD reports one.
+		</p>
 	</section>
 
 	<section id="usb">
@@ -421,6 +438,7 @@
 			<li><strong>xHCI.</strong> USB2 root ports only; USB3 SuperSpeed devices are not handled.</li>
 			<li><strong>HID.</strong> Report IDs are not handled, absolute pointers/tablets are ignored, and there is no wheel support.</li>
 			<li><strong>AHCI.</strong> First controller only, no hotplug after boot, disks above 2 TB are clamped to 32-bit LBA.</li>
+			<li><strong>eMMC.</strong> Polled 1-bit/25 MHz PIO; no DMA, IRQ, CMD6 bus-width/high-speed/DDR switch or hotplug. Only the eMMC function is driven; the SDIO/SD functions and SD card reader are not.</li>
 			<li><strong>Storage serialization.</strong> A FAT/floppy operation sets <code>sys_fs_busy</code> and stalls process switching for its duration; the PIT keeps ticking.</li>
 			<li><strong>EC.</strong> The Chrome EC path uses the ACPI v2 memmap only: no MEC host-command interface, EC SCI events, charger current/voltage, charging limit or EC firmware version. The ThinkPad H8 path is limited to its page-selected battery window and has no SCI handler. Battery/AC is polled by the WM once a second.</li>
 			<li><strong>Suspend.</strong> There is no S0ix/suspend path; package C-states and the EC sleep handshake remain firmware-managed.</li>
@@ -432,6 +450,7 @@
 		<ul class="filelist">
 			<li><code>pci.c</code>, <code>pci.h</code> — enumeration</li>
 			<li><code>blkdev.c</code> — block registry</li>
+			<li><code>sdhci.c</code>, <code>sdhci.h</code> — Intel Braswell eMMC</li>
 			<li><code>disk.c</code> — floppy / DMA</li>
 			<li><code>ata.c</code>, <code>ahci.c</code> — PATA/SATA</li>
 			<li><code>usb.c</code>, <code>usb_hcd.h</code> — USB core</li>

+ 1 - 1
docs/filesystems.html

@@ -163,7 +163,7 @@
 			<span class="file">fatdetect.c</span> and <span class="file">fatformat.c</span>
 			back the <code>format</code> command. A disk can be formatted in
 			binary units (<code>format ide0 100mb</code>, <code>1gb</code>) or
-			whole-disk.
+			whole-disk; the Chromebook eMMC is <code>format mmc0</code>.
 		</p>
 		<div class="warn">
 			<p><strong>FAT32 limits.</strong> The FSInfo free-count is not updated,

+ 1 - 1
docs/index.html

@@ -202,7 +202,7 @@
 			</a>
 			<a class="card" href="drivers.html">
 				<h3>Drivers &amp; buses</h3>
-				<p>PCI enumeration, FDC/PATA/SATA, the USB core and four host controllers, HID, the I2C touchpad, serial, board sensors and the ThinkPad/Chrome EC power sources.</p>
+				<p>PCI enumeration, FDC/PATA/SATA/eMMC, the USB core and four host controllers, HID, the I2C touchpad, serial, board sensors and the ThinkPad/Chrome EC power sources.</p>
 				<span class="card-tag">drivers.html</span>
 			</a>
 			<a class="card" href="filesystems.html">

+ 626 - 0
sdhci.c

@@ -0,0 +1,626 @@
+/* Polled SDHCI/eMMC driver for the Intel Braswell MMC controller.
+ *
+ * The HP Chromebook's eMMC is the SoC MMC function at PCI 00:10.0
+ * (8086:2294), a standard SDHCI host controller.  The firmware has already
+ * assigned its BAR and powered the card, so the kernel only has to reset the
+ * host, run the eMMC identification sequence (CMD0/1/2/3/7/16), read the
+ * 512-byte EXT_CSD with CMD8 (which carries SEC_COUNT) and then use CMD17/
+ * CMD24 with polled PIO for sector I/O.
+ *
+ * Everything is polled: there is no IRQ handler and no DMA descriptor ring.
+ * The card is kept in its default 1-bit/25 MHz mode; that is slow (a few
+ * MB/s) but enough for the storage/format and FAT paths, and avoids the
+ * CMD6 bus-width/timing sequencing.  The result is cached after the first
+ * probe so the per-command vfs_mount_all() refreshes do not re-initialise
+ * the card.
+ */
+#include "sdhci.h"
+#include "paging.h"
+#include "pci.h"
+#include "process.h"
+#include "serial.h"
+#include "timer.h"
+
+#define MMC_PCI_VENDOR       0x8086
+#define MMC_PCI_DEVICE       0x2294   /* Braswell eMMC */
+#define MMC_PCI_DEVICE_SDIO  0x2295
+#define MMC_PCI_DEVICE_SD    0x2296
+
+/* SDHCI register offsets. */
+#define SDHCI_BLOCK_SIZE      0x04
+#define SDHCI_BLOCK_COUNT     0x06
+#define SDHCI_ARGUMENT        0x08
+#define SDHCI_TRANSFER_MODE   0x0C
+#define SDHCI_COMMAND         0x0E
+#define SDHCI_RESPONSE        0x10
+#define SDHCI_BUFFER          0x20
+#define SDHCI_PRESENT_STATE   0x24
+#define SDHCI_HOST_CONTROL    0x28
+#define SDHCI_POWER_CONTROL   0x29
+#define SDHCI_CLOCK_CONTROL   0x2C
+#define SDHCI_TIMEOUT_CONTROL 0x2E
+#define SDHCI_SOFTWARE_RESET  0x2F
+#define SDHCI_INT_STATUS      0x30   /* 32-bit read covers normal + error */
+#define SDHCI_INT_STATUS_EN   0x34
+#define SDHCI_ERR_STATUS_EN   0x36
+#define SDHCI_INT_SIGNAL_EN   0x38
+#define SDHCI_ERR_SIGNAL_EN   0x3A
+#define SDHCI_HOST_CONTROL2   0x3E
+#define SDHCI_CAPABILITIES    0x40
+#define SDHCI_HOST_VERSION    0xFE
+
+#define SDHCI_CTRL_VDD_180    0x0008u
+
+/* Present State bits. */
+#define SDHCI_CMD_INHIBIT     0x00000001u
+#define SDHCI_DAT_INHIBIT     0x00000002u
+#define SDHCI_BUF_WRITE       0x00000400u
+#define SDHCI_BUF_READ        0x00000800u
+
+/* Normal interrupt bits. */
+#define SDHCI_INT_CMD_COMPLETE  0x0001u
+#define SDHCI_INT_TRANS_COMPLETE 0x0002u
+
+/* Clock control bits. */
+#define SDHCI_CLOCK_INT_EN    0x0001u
+#define SDHCI_CLOCK_INT_STABLE 0x0002u
+#define SDHCI_CLOCK_CARD_EN   0x0004u
+
+/* Software reset. */
+#define SDHCI_RESET_ALL       0x01u
+
+/* Command register flags. */
+#define SDHCI_CMD_RESP_NONE   0x00u
+#define SDHCI_CMD_RESP_LONG   0x01u
+#define SDHCI_CMD_RESP_SHORT  0x02u
+#define SDHCI_CMD_RESP_BUSY   0x03u
+#define SDHCI_CMD_CRC         0x08u
+#define SDHCI_CMD_INDEX       0x10u
+#define SDHCI_CMD_DATA        0x20u
+
+/* Transfer mode. */
+#define SDHCI_TRNS_READ       0x10u
+
+/* MMC commands used here. */
+#define MMC_GO_IDLE_STATE     0
+#define MMC_SEND_OP_COND      1
+#define MMC_ALL_SEND_CID      2
+#define MMC_SET_RELATIVE_ADDR 3
+#define MMC_SELECT_CARD       7
+#define MMC_SEND_EXT_CSD      8
+#define MMC_SWITCH            6
+#define MMC_SET_BLOCKLEN      16
+#define MMC_READ_SINGLE_BLOCK 17
+#define MMC_WRITE_SINGLE_BLOCK 24
+
+#define MMC_OCR_ARG           0x40FF8080u  /* sector mode + full voltage */
+#define MMC_RCA               1
+
+#define SDHCI_TIMEOUT_MS      1000
+#define SDHCI_DATA_TIMEOUT_MS 5000
+
+static struct {
+	int state;                 /* -1 unprobed, 0 absent, 1 ready */
+	unsigned int bar;
+	unsigned int base_khz;
+	unsigned int sectors;
+	unsigned int cache_size;
+	int high_capacity;
+	char model[41];
+} mmc = { .state = -1, .model = "" };
+
+static unsigned char rd8(unsigned int off)
+{
+	return *(volatile unsigned char *)(mmc.bar + off);
+}
+
+static unsigned short rd16(unsigned int off)
+{
+	return *(volatile unsigned short *)(mmc.bar + off);
+}
+
+static unsigned int rd32(unsigned int off)
+{
+	return *(volatile unsigned int *)(mmc.bar + off);
+}
+
+static void wr8(unsigned int off, unsigned char v)
+{
+	*(volatile unsigned char *)(mmc.bar + off) = v;
+}
+
+static void wr16(unsigned int off, unsigned short v)
+{
+	*(volatile unsigned short *)(mmc.bar + off) = v;
+}
+
+static void wr32(unsigned int off, unsigned int v)
+{
+	*(volatile unsigned int *)(mmc.bar + off) = v;
+}
+
+static void tiny_delay(unsigned int ms)
+{
+	unsigned int t0 = get_ticks_ms();
+
+	while (get_ticks_ms() - t0 < ms)
+		;
+}
+
+static int wait_state(unsigned int mask, int set, unsigned int ms)
+{
+	unsigned int t0 = get_ticks_ms();
+
+	for (;;) {
+		unsigned int v = rd32(SDHCI_PRESENT_STATE);
+
+		if (set ? ((v & mask) == mask) : ((v & mask) == 0))
+			return 0;
+		if (get_ticks_ms() - t0 > ms)
+			return -1;
+	}
+}
+
+static void log_fail(const char *what, unsigned int cmd, unsigned int sts)
+{
+	serial_puts(SERIAL_COM1, "sdhci: ");
+	serial_puts(SERIAL_COM1, what);
+	if (cmd != 0xFFFFFFFFu) {
+		serial_puts(SERIAL_COM1, " cmd ");
+		serial_putdec(SERIAL_COM1, cmd);
+	}
+	serial_puts(SERIAL_COM1, " failed, int 0x");
+	serial_puthex(SERIAL_COM1, sts);
+	serial_puts(SERIAL_COM1, "\n");
+}
+
+static int sdhci_pio(void *buf, unsigned int blocks, int write)
+{
+	unsigned int words = blocks * 128;   /* 512 bytes per block */
+	unsigned int i;
+	const unsigned int *src = (const unsigned int *)buf;
+	unsigned int *dst = (unsigned int *)buf;
+
+	for (i = 0; i < words; i++) {
+		unsigned int t0 = get_ticks_ms();
+
+		if (write) {
+			while (!(rd32(SDHCI_PRESENT_STATE) & SDHCI_BUF_WRITE)) {
+				if (rd32(SDHCI_INT_STATUS) & 0xFFFF0000u)
+					return -1;
+				if (get_ticks_ms() - t0 > SDHCI_TIMEOUT_MS)
+					return -1;
+			}
+			wr32(SDHCI_BUFFER, *src++);
+		} else {
+			while (!(rd32(SDHCI_PRESENT_STATE) & SDHCI_BUF_READ)) {
+				if (rd32(SDHCI_INT_STATUS) & 0xFFFF0000u)
+					return -1;
+				if (get_ticks_ms() - t0 > SDHCI_TIMEOUT_MS)
+					return -1;
+			}
+			*dst++ = rd32(SDHCI_BUFFER);
+		}
+	}
+	return 0;
+}
+
+static int sdhci_cmd(unsigned int cmd, unsigned int arg, unsigned int flags,
+		     unsigned int blocks, unsigned int mode, void *buf)
+{
+	unsigned int sts = 0;
+	unsigned int t0;
+
+	if (wait_state(SDHCI_CMD_INHIBIT | SDHCI_DAT_INHIBIT, 0,
+		       SDHCI_TIMEOUT_MS) != 0) {
+		log_fail("inhibit", cmd, rd32(SDHCI_PRESENT_STATE));
+		return -1;
+	}
+
+	wr32(SDHCI_INT_STATUS, 0xFFFFFFFFu);
+	wr32(SDHCI_ARGUMENT, arg);
+	if (flags & SDHCI_CMD_DATA) {
+		wr16(SDHCI_BLOCK_SIZE, 512);
+		wr16(SDHCI_BLOCK_COUNT, (unsigned short)(blocks ? blocks : 1));
+		wr8(SDHCI_TRANSFER_MODE, (unsigned char)mode);
+	}
+	wr16(SDHCI_COMMAND, (unsigned short)((cmd << 8) | flags));
+
+	t0 = get_ticks_ms();
+	for (;;) {
+		sts = rd32(SDHCI_INT_STATUS);
+		if (sts & 0xFFFF0000u) {
+			log_fail("command", cmd, sts);
+			return -1;
+		}
+		if (sts & SDHCI_INT_CMD_COMPLETE)
+			break;
+		if (get_ticks_ms() - t0 > SDHCI_TIMEOUT_MS) {
+			log_fail("command timeout", cmd, sts);
+			return -1;
+		}
+	}
+	wr32(SDHCI_INT_STATUS, SDHCI_INT_CMD_COMPLETE);
+
+	if (flags & SDHCI_CMD_DATA) {
+		if (sdhci_pio(buf, blocks ? blocks : 1,
+			      (mode & SDHCI_TRNS_READ) ? 0 : 1) != 0) {
+			log_fail("pio", cmd, rd32(SDHCI_INT_STATUS));
+			return -1;
+		}
+		t0 = get_ticks_ms();
+		for (;;) {
+			sts = rd32(SDHCI_INT_STATUS);
+			if (sts & 0xFFFF0000u) {
+				log_fail("transfer", cmd, sts);
+				return -1;
+			}
+			if (sts & SDHCI_INT_TRANS_COMPLETE)
+				break;
+			if (get_ticks_ms() - t0 > SDHCI_DATA_TIMEOUT_MS) {
+				log_fail("transfer timeout", cmd, sts);
+				return -1;
+			}
+		}
+		wr32(SDHCI_INT_STATUS, 0xFFFFFFFFu);
+	}
+	return 0;
+}
+
+static int sdhci_clock_div(unsigned int div)
+{
+	unsigned int t0;
+
+	if (div < 1)
+		div = 1;
+	if (div > 0xFF)
+		div = 0xFF;
+	wr16(SDHCI_CLOCK_CONTROL, 0);
+	wr16(SDHCI_CLOCK_CONTROL,
+	     (unsigned short)((div << 8) | SDHCI_CLOCK_INT_EN));
+	t0 = get_ticks_ms();
+	while (!(rd16(SDHCI_CLOCK_CONTROL) & SDHCI_CLOCK_INT_STABLE)) {
+		if (get_ticks_ms() - t0 > 100)
+			return -1;
+	}
+	wr16(SDHCI_CLOCK_CONTROL,
+	     (unsigned short)((div << 8) | SDHCI_CLOCK_INT_EN |
+			      SDHCI_CLOCK_CARD_EN));
+	tiny_delay(2);
+	return 0;
+}
+
+static int sdhci_reset_host(void)
+{
+	unsigned int t0;
+
+	wr8(SDHCI_SOFTWARE_RESET, SDHCI_RESET_ALL);
+	t0 = get_ticks_ms();
+	while (rd8(SDHCI_SOFTWARE_RESET) & SDHCI_RESET_ALL) {
+		if (get_ticks_ms() - t0 > 100)
+			return -1;
+	}
+	return 0;
+}
+
+static int pci_find_mmc(unsigned int *bus, unsigned int *dev,
+			unsigned int *func)
+{
+	unsigned int b, d, f;
+
+	for (b = 0; b < 256; b++) {
+		for (d = 0; d < 32; d++) {
+			unsigned int id = pci_cfg_read32(b, d, 0, 0x00);
+			unsigned int htype, nfunc;
+
+			if (id == 0xFFFFFFFFu || id == 0)
+				continue;
+			htype = (pci_cfg_read32(b, d, 0, 0x0C) >> 16) & 0xFFu;
+			nfunc = (htype & 0x80u) ? 8u : 1u;
+			for (f = 0; f < nfunc; f++) {
+				unsigned int idf = pci_cfg_read32(b, d, f, 0x00);
+				unsigned int vendor = idf & 0xFFFFu;
+				unsigned int device = idf >> 16;
+				unsigned int crev, cls, sub;
+
+				if (idf == 0xFFFFFFFFu || idf == 0)
+					continue;
+				crev = pci_cfg_read32(b, d, f, 0x08);
+				cls = (crev >> 24) & 0xFFu;
+				sub = (crev >> 16) & 0xFFu;
+				if (vendor != MMC_PCI_VENDOR)
+					continue;
+				if (device == MMC_PCI_DEVICE ||
+				    (device != MMC_PCI_DEVICE_SDIO &&
+				     device != MMC_PCI_DEVICE_SD &&
+				     cls == 0x08 && sub == 0x05)) {
+					*bus = b;
+					*dev = d;
+					*func = f;
+					return 0;
+				}
+			}
+		}
+	}
+	return -1;
+}
+
+static int sdhci_init(void)
+{
+	unsigned int bus, dev, func, idf, cls, sub, bar, cmd, caps;
+	unsigned int base_mhz, div, ocr, rca, sec;
+	unsigned char ext[512];
+	unsigned char power;
+	unsigned int i;
+	unsigned int t0;
+	unsigned short version, hc2;
+
+	mmc.state = 0;
+	if (pci_find_mmc(&bus, &dev, &func) != 0) {
+		serial_puts(SERIAL_COM1, "sdhci: no Intel MMC controller\n");
+		return -1;
+	}
+	idf = pci_cfg_read32(bus, dev, func, 0x00);
+	cls = (pci_cfg_read32(bus, dev, func, 0x08) >> 24) & 0xFFu;
+	sub = (pci_cfg_read32(bus, dev, func, 0x08) >> 16) & 0xFFu;
+	serial_puts(SERIAL_COM1, "sdhci: PCI ");
+	serial_putdec(SERIAL_COM1, bus);
+	serial_putc(SERIAL_COM1, ':');
+	serial_putdec(SERIAL_COM1, dev);
+	serial_putc(SERIAL_COM1, '.');
+	serial_putdec(SERIAL_COM1, func);
+	serial_puts(SERIAL_COM1, " id ");
+	serial_puthex(SERIAL_COM1, idf);
+	serial_puts(SERIAL_COM1, " class ");
+	serial_puthex(SERIAL_COM1, (cls << 8) | sub);
+	serial_puts(SERIAL_COM1, "\n");
+
+	bar = pci_cfg_read32(bus, dev, func, 0x10);
+	if ((bar & 1u) || bar == 0 || bar == 0xFFFFFFFFu) {
+		serial_puts(SERIAL_COM1, "sdhci: BAR0 is not a usable MMIO address\n");
+		return -1;
+	}
+	if (((bar >> 1) & 3u) == 2u) {
+		if (pci_cfg_read32(bus, dev, func, 0x14) != 0) {
+			serial_puts(SERIAL_COM1, "sdhci: 64-bit BAR above 4 GB\n");
+			return -1;
+		}
+	}
+	bar &= ~0xFu;
+	serial_puts(SERIAL_COM1, "sdhci: BAR0 ");
+	serial_puthex(SERIAL_COM1, bar);
+	serial_puts(SERIAL_COM1, "\n");
+
+	process_map_mmio_all(bar, 0x1000);
+	cmd = pci_cfg_read32(bus, dev, func, 0x04);
+	pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0006u);
+	mmc.bar = bar;
+	mmc.state = 0;
+
+	caps = rd32(SDHCI_CAPABILITIES);
+	version = rd16(SDHCI_HOST_VERSION);
+	base_mhz = (caps >> 8) & 0xFFu;
+	if (!base_mhz)
+		base_mhz = 50;
+	mmc.base_khz = base_mhz * 1000u;
+	serial_puts(SERIAL_COM1, "sdhci: caps 0x");
+	serial_puthex(SERIAL_COM1, caps);
+	serial_puts(SERIAL_COM1, ", version 0x");
+	serial_puthex(SERIAL_COM1, version);
+	serial_puts(SERIAL_COM1, ", base ");
+	serial_putdec(SERIAL_COM1, base_mhz);
+	serial_puts(SERIAL_COM1, " MHz");
+	if (!((caps >> 8) & 0xFFu))
+		serial_puts(SERIAL_COM1, " (default)");
+	serial_puts(SERIAL_COM1, "\n");
+
+	if (sdhci_reset_host() != 0) {
+		serial_puts(SERIAL_COM1, "sdhci: host reset timeout\n");
+		return -1;
+	}
+
+	power = 0x0F;   /* bus power + 3.3 V */
+	if (!(caps & (1u << 24))) {
+		if (caps & (1u << 26))
+			power = 0x0B;   /* 1.8 V */
+		else if (caps & (1u << 25))
+			power = 0x0D;   /* 3.0 V */
+	}
+	/* The Intel eMMC is often 1.8 V-only (caps bit 26): the power
+	 * control selects the rail, and Host Control 2 bit 3 enables 1.8 V
+	 * signalling.  Clear the power register first and let the rail
+	 * settle before applying the clock. */
+	wr8(SDHCI_POWER_CONTROL, 0);
+	tiny_delay(10);
+	wr8(SDHCI_POWER_CONTROL, power);
+	hc2 = rd16(SDHCI_HOST_CONTROL2);
+	if (power == 0x0B)
+		hc2 |= SDHCI_CTRL_VDD_180;
+	else
+		hc2 &= (unsigned short)~SDHCI_CTRL_VDD_180;
+	wr16(SDHCI_HOST_CONTROL2, hc2);
+	tiny_delay(10);
+	wr8(SDHCI_TIMEOUT_CONTROL, 0x0E);
+	wr8(SDHCI_HOST_CONTROL, 0);
+	/* Enable every status bit (we poll the status register) but leave the
+	 * interrupt signal disabled.  With the status-enable bits clear the
+	 * controller keeps INT_STATUS at zero, which looked like a command
+	 * timeout on the first target probe. */
+	wr16(SDHCI_INT_STATUS_EN, 0xFFFF);
+	wr16(SDHCI_ERR_STATUS_EN, 0xFFFF);
+	wr16(SDHCI_INT_SIGNAL_EN, 0);
+	wr16(SDHCI_ERR_SIGNAL_EN, 0);
+	wr32(SDHCI_INT_STATUS, 0xFFFFFFFFu);
+	if (sdhci_clock_div(0xFF) != 0) {
+		serial_puts(SERIAL_COM1, "sdhci: clock did not stabilise\n");
+		return -1;
+	}
+	serial_puts(SERIAL_COM1, "sdhci: power 0x");
+	serial_puthex(SERIAL_COM1, power);
+	serial_puts(SERIAL_COM1, ", hc2 0x");
+	serial_puthex(SERIAL_COM1, hc2);
+	serial_puts(SERIAL_COM1, ", clock 0x");
+	serial_puthex(SERIAL_COM1, rd16(SDHCI_CLOCK_CONTROL));
+	serial_puts(SERIAL_COM1, ", state 0x");
+	serial_puthex(SERIAL_COM1, rd32(SDHCI_PRESENT_STATE));
+	serial_puts(SERIAL_COM1, "\n");
+
+	if (sdhci_cmd(MMC_GO_IDLE_STATE, 0, SDHCI_CMD_RESP_NONE, 0, 0, 0) != 0)
+		return -1;
+
+	t0 = get_ticks_ms();
+	ocr = 0;
+	for (;;) {
+		if (sdhci_cmd(MMC_SEND_OP_COND, MMC_OCR_ARG,
+			      SDHCI_CMD_RESP_SHORT, 0, 0, 0) == 0) {
+			ocr = rd32(SDHCI_RESPONSE);
+			if (ocr & 0x80000000u)
+				break;
+		}
+		if (get_ticks_ms() - t0 > 2000) {
+			serial_puts(SERIAL_COM1, "sdhci: CMD1 timeout, ocr 0x");
+			serial_puthex(SERIAL_COM1, ocr);
+			serial_puts(SERIAL_COM1, "\n");
+			return -1;
+		}
+		tiny_delay(1);
+	}
+	serial_puts(SERIAL_COM1, "sdhci: ocr 0x");
+	serial_puthex(SERIAL_COM1, ocr);
+	serial_puts(SERIAL_COM1, "\n");
+	mmc.high_capacity = (ocr & 0x40000000u) ? 1 : 0;
+
+	if (sdhci_cmd(MMC_ALL_SEND_CID, 0, SDHCI_CMD_RESP_LONG | SDHCI_CMD_CRC,
+		      0, 0, 0) != 0)
+		return -1;
+	if (sdhci_cmd(MMC_SET_RELATIVE_ADDR, MMC_RCA << 16,
+		      SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
+		      0, 0, 0) != 0)
+		return -1;
+	rca = (rd32(SDHCI_RESPONSE) >> 16) & 0xFFFFu;
+	if (!rca)
+		rca = MMC_RCA;
+	if (sdhci_cmd(MMC_SELECT_CARD, rca << 16,
+		      SDHCI_CMD_RESP_BUSY | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
+		      0, 0, 0) != 0)
+		return -1;
+	if (sdhci_cmd(MMC_SET_BLOCKLEN, 512,
+		      SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
+		      0, 0, 0) != 0)
+		return -1;
+
+	for (i = 0; i < sizeof(ext); i++)
+		ext[i] = 0;
+	if (sdhci_cmd(MMC_SEND_EXT_CSD, 0,
+		      SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC | SDHCI_CMD_INDEX |
+			      SDHCI_CMD_DATA,
+		      1, SDHCI_TRNS_READ, ext) != 0)
+		return -1;
+
+	sec = (unsigned int)ext[212] | ((unsigned int)ext[213] << 8) |
+	      ((unsigned int)ext[214] << 16) | ((unsigned int)ext[215] << 24);
+	if (sec == 0 || sec == 0xFFFFFFFFu) {
+		serial_puts(SERIAL_COM1, "sdhci: EXT_CSD has no sector count\n");
+		return -1;
+	}
+	mmc.cache_size = (unsigned int)ext[249] |
+			 ((unsigned int)ext[250] << 8) |
+			 ((unsigned int)ext[251] << 16) |
+			 ((unsigned int)ext[252] << 24);
+
+	div = mmc.base_khz / 50000u;   /* 25 MHz: base / (2 * 25000) */
+	if (!((caps >> 8) & 0xFFu))
+		div = 4;               /* conservative if the base is unknown */
+	if (sdhci_clock_div(div) != 0) {
+		serial_puts(SERIAL_COM1, "sdhci: 25 MHz clock did not stabilise\n");
+		return -1;
+	}
+
+	mmc.sectors = sec;
+	mmc.high_capacity = mmc.high_capacity ? 1 : 0;
+	for (i = 0; i < sizeof(mmc.model) - 1; i++)
+		mmc.model[i] = 0;
+	{
+		static const char name[] = "Intel eMMC";
+		for (i = 0; name[i] && i < sizeof(mmc.model) - 1; i++)
+			mmc.model[i] = name[i];
+	}
+	mmc.state = 1;
+
+	serial_puts(SERIAL_COM1, "sdhci: eMMC ");
+	serial_putdec(SERIAL_COM1, (sec / 2048u) / 1024u);
+	serial_puts(SERIAL_COM1, " GiB, ");
+	serial_putdec(SERIAL_COM1, sec);
+	serial_puts(SERIAL_COM1, " sectors, ");
+	serial_puts(SERIAL_COM1, mmc.high_capacity ? "block" : "byte");
+	serial_puts(SERIAL_COM1, " addressing\n");
+	return 0;
+}
+
+unsigned int sdhci_scan(struct sdhci_disk *out, unsigned int max)
+{
+	unsigned int i;
+
+	if (mmc.state < 0)
+		(void)sdhci_init();
+	if (mmc.state <= 0 || max == 0)
+		return 0;
+	out[0].sectors = mmc.sectors;
+	for (i = 0; i < sizeof(out[0].model) - 1 && mmc.model[i]; i++)
+		out[0].model[i] = mmc.model[i];
+	out[0].model[i] = '\0';
+	return 1;
+}
+
+int sdhci_read_sectors(unsigned int lba, void *buf, unsigned int count)
+{
+	unsigned int i;
+
+	if (mmc.state <= 0)
+		return -1;
+	for (i = 0; i < count; i++) {
+		unsigned int arg = mmc.high_capacity ? (lba + i) :
+				   (lba + i) * 512u;
+
+		if (sdhci_cmd(MMC_READ_SINGLE_BLOCK, arg,
+			      SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC |
+				      SDHCI_CMD_INDEX | SDHCI_CMD_DATA,
+			      1, SDHCI_TRNS_READ,
+			      (unsigned char *)buf + i * 512u) != 0)
+			return -1;
+	}
+	return 0;
+}
+
+int sdhci_write_sectors(unsigned int lba, const void *buf, unsigned int count)
+{
+	unsigned int i;
+
+	if (mmc.state <= 0)
+		return -1;
+	for (i = 0; i < count; i++) {
+		unsigned int arg = mmc.high_capacity ? (lba + i) :
+				   (lba + i) * 512u;
+
+		if (sdhci_cmd(MMC_WRITE_SINGLE_BLOCK, arg,
+			      SDHCI_CMD_RESP_SHORT | SDHCI_CMD_CRC |
+				      SDHCI_CMD_INDEX | SDHCI_CMD_DATA,
+			      1, 0,
+			      (void *)((const unsigned char *)buf + i * 512u)) != 0)
+			return -1;
+	}
+	return 0;
+}
+
+int sdhci_flush(void)
+{
+	/* eMMC CACHE_SIZE (EXT_CSD[249..252]) is non-zero when the card has
+	 * a write-back cache.  EXT_CSD_FLUSH_CACHE (index 32) with value 1
+	 * commits it; failures are harmless (the card then has no cache). */
+	if (mmc.state <= 0 || !mmc.cache_size)
+		return 0;
+	(void)sdhci_cmd(MMC_SWITCH, 0x0C200100u,
+			SDHCI_CMD_RESP_BUSY | SDHCI_CMD_CRC | SDHCI_CMD_INDEX,
+			0, 0, 0);
+	return 0;
+}

+ 23 - 0
sdhci.h

@@ -0,0 +1,23 @@
+/* Polled SDHCI/eMMC driver for the Intel Braswell MMC controller. */
+#ifndef SDHCI_H
+#define SDHCI_H
+
+struct sdhci_disk {
+	unsigned int sectors;      /* 512-byte sectors */
+	char model[41];
+};
+
+/* Probe the Intel MMC controller and initialise the eMMC.  Returns the
+ * number of disks found (0 or 1); the result is cached after the first
+ * call.  Logs the PCI id, BAR, capabilities and eMMC capacity to COM1. */
+unsigned int sdhci_scan(struct sdhci_disk *out, unsigned int max);
+
+/* 512-byte-sector I/O on the selected eMMC.  Returns 0 on success. */
+int sdhci_read_sectors(unsigned int lba, void *buf, unsigned int count);
+int sdhci_write_sectors(unsigned int lba, const void *buf, unsigned int count);
+
+/* The eMMC write path completes on the write command; there is no host
+ * cache to flush. */
+int sdhci_flush(void);
+
+#endif

+ 1 - 1
syscall.h

@@ -412,7 +412,7 @@ struct sys_storage_info {
 struct sys_blkdev {
 	char name[8];
 	char model[41];
-	unsigned int kind;       /* 0 = floppy, 1 = IDE, 2 = SATA */
+	unsigned int kind;       /* 0 = floppy, 1 = IDE, 2 = SATA, 3 = MMC */
 	unsigned int present;
 	unsigned int total_sectors;
 };