Skip to content

Power

An iPod classic lives on a small battery and a spinning hard drive, and the original firmware spends a good deal of code keeping the two apart. Chikuma idles the CPU, dims the backlight, gates the clocks of most blocks it does not use, and selects the charge current the way the original does. It also powers the drive down a second after its last read, steps the CPU down when nothing is busy, and puts the iPod into light sleep on battery, the way the original does. Hibernate and power off are still missing.

What the original does

Everything below was read in the firmware and measured in the emulator, with the original running on battery and no charger attached.

The speed governor

A power-management task samples the load of every task and moves between four performance levels. It changes the CPU and bus clock dividers, and steps the core voltage between 1.2 V and 1.1 V. The voltage goes up before the clocks speed up, and down after they slow.

Full speed is forced whenever one of these holds:

  • the backlight is lit
  • the drive is powered
  • the read-ahead is filling
  • the video scaler runs
  • a boost is open
  • USB is in use

A key press puts the governor at full speed at once. About ten seconds after the backlight goes dark, it steps down one level at a time to the slowest, and playback carries on there.

Idle and clock gates

When nothing is runnable, the CPU waits for an interrupt, with no clock change. Clock gates sit around each block's user: the AES and SHA-1 engines, the video decoder, the audio interface, USB and the drive interface.

The drive between bursts

Each user of the disk holds it, and releases it a second after its last access. When nothing holds the disk, the firmware sends the drive STANDBY IMMEDIATE, then cuts its power rail at the PMU.

Tracks are read in 512 KiB chunks to the end of the file and then into the next track, so the drive stays off in between. Shuffling a small library, the original read all of it (28 MiB) in about a second. Half a second later the drive was in standby with its rail cut, and the next 276 seconds of playback issued no disk command at all.

Backlight, sleep and hibernate

The backlight times out from the setting.

Light sleep comes after two minutes dark and not playing, on battery only. On external power the Charging screen shows instead. The firmware arms an RTC alarm and takes down the drive rail, the panel and the click wheel. Main memory goes into self-refresh, almost every clock gets gated, and the CPU waits. In the emulator, the original wrote exactly the register values read in its code.

Hibernate follows thirty minutes into light sleep. The state is saved, a descriptor is left for the bootloader to resume from, and the PMU is told to shut down. Power off is the same request with a different value.

Smaller steps

The audio codec has a power-down path, and only sleep and hibernate take it. Pause and stop leave the codec powered. USB steps a regulator down instead of switching it off.

What Chikuma does of it

Mechanism Chikuma
Idle wait for interrupt Done, in the kernel's idle task
Backlight timeout Done
Clock gates Done: AES, SHA-1, video decoder, audio interface and USB gate as the original's do, and the drive interface's clock closes with the drive's rail
Drive power-down Done: STANDBY IMMEDIATE goes out 1000 ms after the last disk command, then the rail is cut in the original's order. The next command powers the drive up and configures it again. On the real iPod it cut and restored the rail repeatedly with no refused command. The current saved is not measured yet.
Read-ahead Partial: the same 512 KiB chunks, as a sliding window with a smaller pool. While the drive is up, the next track is read to its end into the slots the playing track leaves, so an album plays with the drive asleep between bursts.
Speed governor Partial: the original's level change, in its order and with its tables, but only two targets. Any busy flag holds full speed, and with none the CPU steps down to the slowest level. There is no load sampling yet. While a track plays, Chikuma also holds full speed, since its codec programs have not been measured at the slow clock.
USB regulator step Partial: the USB clock is gated, the regulator is not stepped
Light sleep Done, on battery after two minutes dark and not playing, with the original's whole sequence: drive, panel, codec, GPIOs, wheel, PMU rails, caches, memory self-refresh, clocks and the wait. MEASURED on the real iPod: MENU woke it twice in a row. The alarm that leads to hibernate and the charger's handling across sleep are not ported.
Hibernate, power off Missing
Codec power-down Done, on sleep only. The original never powers the codec down on pause or stop, and neither does Chikuma.

Light sleep

On battery, after two minutes with the backlight dark and nothing playing, Chikuma puts the iPod to sleep. On external power the Charging screen comes up instead, as in the original, and the iPod stays awake. The drive still spins down a second after its last read there.

The way down follows the original's order:

  1. The drive gets STANDBY IMMEDIATE and its rail is cut.
  2. Interrupts are masked, the system tick stops and the CPU goes back to full speed.
  3. The PMU's pending interrupt status is read, which clears it.
  4. The panel runs its power-off script. The audio codec mutes and powers down.
  5. The GPIOs are saved, then frozen to the original's sleep table, which holds the drive's bus low.
  6. The click wheel moves to the mode where a press can still wake the machine.
  7. The PMU switches off the drive's rail and one more output, bit 0 of its register 0x43.
  8. The wake lines are armed. The caches are cleaned and switched off, and SDRAM goes into self-refresh.
  9. The clocks are gated down, and the CPU waits for an interrupt.

The last two steps run from SRAM, on a stack in SRAM, since nothing can be read from main memory while it refreshes itself.

The way back runs the same list in reverse. Clocks, memory and caches come back first, then the wheel's normal mode, the saved GPIOs, the codec and the panel. The tick starts again, interrupts are unmasked, and the speed governor settles back to its level.

Three things wake the iPod:

  • MENU, whose press reaches the CPU through the PMU's interrupt line
  • the hold switch
  • USB

The PMU keeps its interrupt line asserted until its status bytes are read. So the first thing Chikuma does after the wait is that read, on the I2C bus it has held since before the sleep. Without it, the wake line stays up and the next wait ends at once. For the same reason the status is also read just before sleeping, and the PMU's interrupt masks are set as the original sets them, so a finished ADC reading never raises the line.

The PMU's line can also blip during the clock-down, with nothing in its status. A wake like that has no cause, so the iPod goes straight back to sleep, up to eight times in a row. Elapsed time cannot tell such a wake from a real one, because the sleep stops the timer's clock along with the rest. After an hour asleep the timer has barely moved. So a wake counts as spurious only when the PMU's line alone fired and every status byte is empty.

Hibernate is not built yet, nor the RTC alarm that leads to it thirty minutes into a sleep. The charger's handling across sleep is missing too, where the original changes it on the way down and puts it back on the way up. A long press on Play asks for sleep on battery, and for now Chikuma only pauses and turns the backlight off.

Build order

The order is ranked by battery saved per unit of effort, for a device that mostly plays with its screen dark:

  1. the drive power-down, now done
  2. reading the whole next track ahead, now done, since with only its head read ahead the rest of each track still needed the drive once it played
  3. a two-level governor, in place, with playback still held at full speed
  4. light sleep, with the codec power-down that belongs to it, now done
  5. hibernate last, since on external power the original does not sleep at all

Two risks

Cutting the drive's rail with a write in flight loses data. Chikuma writes the disk through a journal and whole-sector read-modify-write cycles. So the drive's cache has to be flushed and every write held open until it completes before the rail goes down. A drive powered back up needs configuring again.

The governor's slow levels also slow the audio DSP's domain and the DMA that feeds it, though the sample clock itself keeps its rate. The voltage order has to match the original's exactly. Underruns must be measured on the device before it ships, because an underrun is audible.

Where the power comes from

The PMU reports three sources, and the firmware asks about them in order. Only one of them means a computer, and only that one wakes the USB stack. The other two are power only, and move the battery display without starting USB.

In the emulator, an iPod with nothing plugged in reports no source at all.

The charger

The charger is a separate chip, driven by GPIO pins instead of PMU registers. Its modes are 100 mA, 500 mA, 1 A, suspended and off. The bootloader leaves it at 100 mA.

Situation Original Chikuma
USB bus reset 100 mA same
Host configures the device 500 mA same: on the development unit, configured by a Mac, the pins read 500 mA, charging
Apple vendor request the host grants more on top same
Session ends charging off, from the PMU's detach interrupt charging off at the end of the USB session, OURS, since there is no PMU interrupt task yet
Wall charger classified by measuring two of its pins through the PMU's ADC, which picks 500 mA or 1 A stays at 100 mA, since there is no PMU ADC driver yet

What the battery tells

The device can know two things about its battery: its voltage, and whether the charger says it is charging or done. There is no fuel gauge, no temperature reading and no current measurement. The charger's own thermal pause stays invisible to the CPU.

The pin and register names were checked against the public datasheets of the charger and of the PMU's documented sibling.