Clock Control
Chips stop clocks to save power, switch sources at power-up, and keep one domain always running. Test each control. Gates open at reset; a stopped block answers and keeps its state. The core's clock stops and starts again, and a hang becomes a failure.
You Will Be Able To
- Say what each clock control of ap_soc does and who drives it.
- Check what a block answers while its clock is stopped, and what it keeps.
- Test a sleep that stops the core's clock, with a backstop for a wake that never comes.
- Explain why a glitch on a gated clock is checked by structure, and how simulation can show one.
- Lesson
- V9.4
- Time
- 26 min
- Runs on
- Your browser
- Check
- 5 questions
A core whose clock stops and never starts again cannot report anything. This lesson tests every clock control, and gives the sleep test a backstop that does not need the core.
- Cycle 15
The clock rises at 15 ns. Both gated clocks follow it.
- Cycle 17
The enable falls at 17 ns. The AND gate cuts its pulse to 2 ns; ap_clk_gate keeps it whole until 20 ns.
- Cycle 37
The enable comes back at 37 ns, inside a high phase. The AND gate passes a runt of 3 ns; ap_clk_gate waits for the next whole pulse, at 45 ns.
The example below, run on Icarus Verilog version 14.0; one column is 1 ns. ap_clk_gate.sv is the gate cell of ap_soc v1.5 (tools/soc/ap_soc/rtl).
Before You Start
Read A Test for Every Reset Source first. Gates and the clock mux are built in Dividers and Clock Gates and The Glitch-Free Clock Mux. Sleep is in Sleep and Wake.
| Question | |
|---|---|
| 1 | Which clock controls does ap_soc have, and which can software reach? |
| 2 | What does a block answer while its clock is stopped? |
| 3 | How does a test show that the core's clock stopped in wfi and started again? |
| 4 | What turns a sleep that never ends into a failure a test can report? |
| 5 | How is a glitch on a gated clock found, if the bench cannot see it? |
The Idea: Every Control, Both Ways
- Processor
- Clock / reset
- Peripheral
- Control
- Clock
- Software gates
CLK_GATE stops and starts five peripheral clocks.
- The core's gate
The instruction wfi stops the core's own clock until an enabled interrupt.
- Always on
clk_aon is never gated; the watchdog runs on it.
Read the diagram as text
- Firmware: CLK_GATE, wfi
- ap_crg: gates and the mux
- Five peripherals: UART0, GPIO, timer, SPI, DMA
- The core's clock: stops in wfi
- clk_aon: never gated: watchdog
- Firmware → ap_crg: CLK_GATE, wfi
- ap_crg → Five peripherals: gated clocks
- ap_crg → The core's clock: clk_core
ap_crg.sv and ap_clk_gate.sv (facts 27.11).
ap_soc has four kinds of clock control. Software closes and opens five peripheral gates with CLK_GATE. The core's own gate closes in wfi and opens on an enabled interrupt. ap_pwrctl stops the clocks of the switched power domain on the way down. And at power-up the chip moves clk_sys from its RC oscillator to the PLL.
Each control must work both ways: a clock that stops must start again. And while a clock is stopped, the rest of the chip must not hang on it. A stopped block answers at once with ERROR and changes nothing; its registers keep their values for when it runs again.
The hardest case is the core itself. A core that sleeps and never wakes cannot report anything. Only a part of the chip with its own clock can turn that into a failure. That is the watchdog, on the always-on clock, which nothing gates.
New Words
| Word | Meaning |
|---|---|
| Clock gate | A cell that lets a clock through or stops it, from an enable. |
| Glitch | A pulse shorter than the clock's own, made when a gate or a mux changes in the middle of a high phase. |
| Clock switch | Moving a domain from one clock source to another. ap_soc moves clk_sys from the RC oscillator to the PLL once, at power-up. |
| Always-on clock | clk_aon: its own oscillator, never gated; the watchdog, ap_pwrctl and the reset controller run on it. |
| Backstop | Something outside the core that ends a hang: here the watchdog's reset. |
| Wake source | An enabled interrupt that opens the core's gate after wfi. |
The Smallest Example: Where a Glitch Comes From
A clock rises at 5, 15, 25 ns and is high for 5 ns. An enable falls at 17 ns. With an AND gate alone, when does the gated pulse that started at 15 ns end? With a gate that takes the enable only while the clock is low?
| Gate | Pulse from 15 ns | Next pulse after the enable returns at 37 ns |
|---|---|---|
| AND alone | ends at 17 ns: 2 ns wide | a runt from 37 to 40 ns: 3 ns |
| ap_clk_gate | ends at 20 ns: whole | none until 45 ns: whole |
From the example below; the picture at the top draws it.
A flop clocked by a runt may or may not see an edge: nobody can say which. So a gate must change its mind only while the clock is low. ap_clk_gate takes its enable on the falling edge and holds it through the high phase.
Why is this example a small slice on Icarus, and not the chip? The chip bench's checkers sample their signals once at each edge of clk_sys. A runt that starts and ends between two samples never reaches them. Icarus is an event-driven simulator. It can put the enable at 17 ns and print every edge of the gated clock, however short. The slice has no delays, so it shows where a glitch can come from, not how long a real one lasts.
The Rules
Rule 1: Every gate is open after every reset.
Firmware that runs after a reset did not close any gate. CLK_GATE reads 0x1F, and checking it once per reset finds a gate that stays closed.
Rule 2: A stopped block answers at once and changes nothing.
An access to it answers ERROR without waiting; a write does not land. A hang or a silent loss would be a chip bug.
Rule 3: A stopped block keeps its state.
Its flops see no clock, so they hold. Write a register, stop the clock, start it, read the same value.
Rule 4: Some clocks run against their gate, on purpose.
The DMA keeps its clock until its job ends; the timer and SPI lose theirs while their power domain sleeps. A checker must allow both.
Rule 5: A sleep needs a wake and a backstop.
Test that the core's clock stops in wfi and that the right interrupt starts it. Arm the watchdog before, so a wake that never comes ends in a reset the next boot can report.
Rule 6: Check glitch-free switching by structure.
The bench samples on clk_sys and cannot see a glitch between its edges. A gate or a mux is glitch-free by construction. Simulating the cell shows it; a structural check confirms the cell was used.
Sources for These Rules
The gates, clk_on and the DMA exception: tools/soc/ap_soc/rtl/ap_crg.sv and ap_clk_gate.sv; what a stopped block answers: ap_apb_guard.sv; facts 27.11.
The core's gate is open unless the core sleeps in wfi, both ports idle, no enabled interrupt pending. See ap_crg.sv (core_gate), facts 27.11 and S5.8.
The clock switch to the PLL: ap_clk_mux.sv. Each source's grant passes two flops on its falling edge (facts 10, 27.11).
The instruction wfi resumes when a locally enabled interrupt is pending, whatever the global enable: RV-PRIV 3.3.3.
The Lab: Gates, a Sleep and a Backstop
First, the gates from the bus-manager seat. The table reads CLK_GATE, closes UART0's gate and reads it, opens it again, then closes the DMA's gate while a copy runs.
tb wait 20 cycles
core r 0x10004014 expect 0x1f
core w 0x10000010 = 0x19
core w 0x10004014 = 0x1e
core re 0x10000010
core w 0x10004014 = 0x1f
core r 0x10000010 expect 0x19
core w 0x10006000 = 0x80001000
core w 0x10006004 = 0x80002000
core w 0x10006008 = 0x20
core w 0x1000600c = 0x1
core w 0x10004014 = 0xf
tb wait 400 cycles
core re 0x10006014
core w 0x10004014 = 0x1f
core r 0x10006014 expect 0x2Every row held: the run passed with 80 transfers and 32 DMA bytes. Rules 2 and 3 at work: with its gate closed, UART0 answered ERROR at once, and DIV still read 25 afterwards. Rule 4: the DMA finished its copy with its gate closed, then answered ERROR until the gate opened.
Second, the core's own clock. A program sets the CLINT timer 200 cycles ahead, enables its line in mie and executes wfi. The bench's sample window shows clk_core stopped for 161 cycles of clk_sys. Then the timer fired, and the program printed its line.
EVENT 231: 00000094
CONSOLE 478: awakeThird, a sleep that nothing wakes: no interrupt is enabled. The program arms the watchdog, notes "asleep" in TB_ARG, and executes wfi.
EVENT 244: 00000095
CONSOLE 1339: the watchdog ended the sleep, cause 00000005Rule 5 at work: the core never woke, and could never have reported it. The watchdog, on clk_aon, reset the chip 338 cycles after arming. The next boot found TB_ARG and RESET_CAUSE 0x05, and said what happened.
Why It Is Built This Way
Why does a stopped block answer ERROR instead of waiting? A wait would hold the bus until someone opened the gate, which might be never. A write that is lost with an OKAY answer would be a silent failure. ERROR at once is loud and safe.
Why does the switch to the PLL happen only once, at power-up? The bus leaves reset only when clk_sys runs on the PLL, so no program ever runs on the RC clock. The switch is tested by every power-up: the release order of V3.2 depends on it.
Traps
The first trap is a sleep test with no backstop. On a chip whose core never wakes, the run ends at the cycle limit. That reads as "the test is too slow", not "the chip hangs". It is not counted as a failure that names anything.
The second is a gate test that only closes gates. Closing a gate is easy to see. A gate that never opens again after reset is seen only by a check at the reset.
The third is a test that reads a stopped block. In firmware an ERROR answer becomes a trap, and the kit ends the test with code 0xFFFF. Read a block only with its gate open. Or test the ERROR from the seat, where an ERROR row is an ordinary check.
At Scale
This section looks ahead. ap_soc has five software gates, one core gate and one switch.
Hundreds of gates
Large chips gate thousands of registers with automatic gating inserted by synthesis. Equivalence checking proves the gating changes no behaviour; simulation checks only the controls software owns.
Dynamic frequency
Chips change clock frequency at run time. Each switch is a test: no glitch, no lost transfer, and software waits for the new clock.
Clock monitors
Some chips watch their own clocks and raise a reset when one stops. That monitor is one more reset source of V9.1.
Try It
Run it: every high pulse of both gated clocks, with its width.
Find the two pulses of the AND gate that are not 5 ns wide.
Move the enable to 19 ns. Which gate changes its output, and which does not?
AND gate: high from 5 to 10 ns, 5 ns wide ap_clk_gate: high from 5 to 10 ns, 5 ns wide AND gate: high from 15 to 17 ns, 2 ns wide ap_clk_gate: high from 15 to 20 ns, 5 ns wide AND gate: high from 37 to 40 ns, 3 ns wide AND gate: high from 45 to 50 ns, 5 ns wide ap_clk_gate: high from 45 to 50 ns, 5 ns wide AND gate: high from 55 to 60 ns, 5 ns wide ap_clk_gate: high from 55 to 60 ns, 5 ns wideCheck Yourself
Common Mistakes
Wrong Test a sleep with no backstop.
A core that never wakes cannot report; the run ends at the cycle limit and names nothing.
Fix Arm the watchdog before the sleep and leave a note in a scratch register.
Wrong Check only that a gate closes.
A gate that stays closed after reset is seen only at the reset.
Fix Check CLK_GATE right after every reset.
Wrong Expect every enable to equal its control bit.
The DMA keeps its clock until its job ends; PD_PER loses its clocks while it sleeps.
Fix Write the exceptions into the checker as one-way rules.
Remember
- Every gate is open after reset; a stopped block answers ERROR at once and keeps its state.
- The DMA and the switched power domain run against their gates on purpose; checkers must allow it.
- A sleep test checks that the core's clock stops and that the right interrupt starts it, with the watchdog as backstop.
- Glitch-free gating is checked by structure; the chip bench cannot see between its clock edges.