phytium-mci
A no_std Rust driver for SD/MMC cards on Phytium E2000 series SoCs.
Overview
phytium-mci is a comprehensive SD/MMC host controller driver designed for Phytium SoC platforms (specifically the E2000 series used in Phytium Pi development boards). It implements the full SD/MMC protocol stack from hardware register manipulation to high-level card operations, supporting both SD and eMMC cards with DMA and PIO transfer modes.
Key Features
- Full SD Specification Support: SDSC, SDHC, SDXC (Specification versions 1.0-3.0)
- eMMC Support: MMC protocol implementation
- Flexible Transfer Modes: DMA (high-performance) and PIO (simple) transfers
- Voltage Support: 3.3V (default) and 1.8V (UHS-I modes)
- Bus Widths: 1-bit, 4-bit, and 8-bit (eMMC) data bus
- High-Speed Modes: SDR12, SDR25, SDR50, SDR104, DDR50
- Clock Speed Support: From 400 KHz (initialization) up to 208 MHz (SDR104)
- Card Detection: GPIO-based and host-based card detection
- Interrupt Support: Command completion, data transfer, and card detection interrupts
- Platform Abstraction: Clean separation through the
Kerneltrait
Architecture
The driver is organized into distinct layers:
Application Layer (SdCard, MCIHost - High-level API)
↓
Protocol Layer (Command/Data transfer, Card initialization)
↓
Hardware Abstraction (Register access, DMA/PIO control)
↓
Hardware Support (IoPad pin configuration, OSA memory/timing)
Module Structure
- mci/ - Hardware controller driver (register access, DMA/PIO, interrupts)
- mci_host/ - Host controller protocol layer (SD/MMC protocol implementation)
- iopad/ - I/O pad configuration for pin multiplexing
- osa/ - OS abstraction layer (memory management, event flags)
Requirements
- Rust 2021 edition
- Phytium E2000 series SoC or compatible platform
no_stdenvironment (bare-metal or custom OS)
Dependencies
= "0.9.0" # Type-safe register access
= "0.4" # Logging facade
= "1.1" # Non-blocking I/O
= "2.8" # Bit flags
= "1.22.0" # Safe byte casting
= "1.5.0" # Global state
= "0.10.0" # Spin locks
= "0.2.1" # Memory allocator
Features
| Feature | Description | Default |
|---|---|---|
dma |
Enable DMA transfers | No |
pio |
Enable PIO transfers | Yes |
poll |
Enable polling mode | Yes |
irq |
Enable interrupt mode | No |
# Default: PIO + Poll mode (simpler, good for debugging)
[]
= { = "0.1.0" }
# Recommended: DMA + IRQ (high performance)
[]
= { = "0.1.0", = ["dma", "irq"] }
Usage
1. Platform Integration
Implement the Kernel trait to provide platform-specific functionality:
use ;
use ;
;
// Register your implementation
set_impl!;
2. Basic SD Card Initialization
use ;
use NonNull;
3. Reading Blocks
use SdCard;
use Vec;
4. Writing Blocks
use SdCard;
use Vec;
5. Configuration for Different Modes
use MCIConfig;
use MCIHostConfig;
use MCIHostType;
use MCIHostCardType;
use MCIHostEndianMode;
// For DMA mode (high performance)
let host_config = MCIHostConfig ;
Hardware Details
Target Hardware
| Component | Description |
|---|---|
| SoC | Phytium E2000 series (ARMv8-A architecture) |
| Board | Phytium Pi development board |
| Controller | Phytium SDIF (Synopsys DesignWare-based) |
| MCI0 Base | 0x2800_1000 |
| MCI1 Base | 0x2800_2000 |
| IOPAD Base | 0x2800_0000 |
Clock Configuration
- Source Clock: 1.2 GHz
- Initialization: 400 KHz (for card detection and initialization)
- Default Speed: 25 MHz
- High Speed: 50 MHz
- UHS-I SDR104: Up to 208 MHz
Voltage Modes
| Mode | Voltage | Bus Width | Max Clock |
|---|---|---|---|
| Default | 3.3V | 1-bit/4-bit | 25 MHz |
| High Speed | 3.3V | 4-bit | 50 MHz |
| SDR12 | 1.8V | 4-bit | 25 MHz |
| SDR25 | 1.8V | 4-bit | 50 MHz |
| SDR50 | 1.8V | 4-bit | 100 MHz |
| SDR104 | 1.8V | 4-bit | 208 MHz |
| DDR50 | 1.8V | 4-bit | 50 MHz |
API Reference
Main Types
SdCard
High-level SD card interface.
MCIHost
Host controller abstraction.
IoPad
I/O pad configuration for pin multiplexing.
Card Information Structures
Error Handling
The crate provides comprehensive error types:
// MCI (Hardware) errors
// Host (Protocol) errors
Memory Management
The crate includes a custom TLSF-based memory pool allocator for DMA operations:
use ;
// Initialize the global memory pool
unsafe
// Allocate aligned buffer for DMA
let buffer = alloc.expect;
// Use buffer...
// Buffer is automatically freed when dropped
Testing
⚠️ Hardware integration tests require physical Phytium Pi hardware.
This project provides bare-metal integration tests that run on actual Phytium Pi hardware to verify SD/MMC card functionality.
Prerequisites
-
Phytium Pi Hardware
- Phytium Pi development board
- SD card inserted
- Serial port connected
-
Install ostool:
-
Configure device tree (use
firmware/phytium.dtb)
Running Hardware Tests
# Build and run on Phytium Pi
# PIO mode only
IMPORTANT: Hardware integration tests CANNOT run on:
- ❌ Virtual machines or emulators
- ❌ x86_64 or other non-ARM platforms
- ❌ Systems without SD/MMC hardware
The tests communicate via serial port and require:
- U-Boot with TFTP support
- Physical Phytium Pi hardware
- Working SD card interface
Platform Abstraction
The Kernel trait provides a clean abstraction for platform-specific operations:
This allows the driver to work with:
- Bare-metal applications
- Custom operating systems
- Embedded frameworks
Card Information
The driver provides detailed card information:
let sdcard: &SdCard = /* ... */;
// Basic information
println!;
println!;
println!;
// Card identification
println!;
println!;
println!;
println!;
// Card specific data
println!;
println!;
println!;
// SD configuration
println!;
println!;
License
This project is licensed under MIT OR Apache-2.0.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For issues, questions, or contributions related to Phytium hardware, please visit: