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.

A Gated Clock, Clean and GlitchedAn enable that changes in a high phase makes an AND gate cut pulses; ap_clk_gate passes only whole ones.
0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960clkenableAND gateap_clk_gateenable fallsenable rises
Scroll horizontally ↔
  1. Cycle 15

    The clock rises at 15 ns. Both gated clocks follow it.

  2. 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.

  3. 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.

Five questions this page answers
Question
1Which clock controls does ap_soc have, and which can software reach?
2What does a block answer while its clock is stopped?
3How does a test show that the core's clock stopped in wfi and started again?
4What turns a sleep that never ends into a failure a test can report?
5How is a glitch on a gated clock found, if the bench cannot see it?

The Idea: Every Control, Both Ways

The Clock Controls of ap_socWho stops which clock, and what keeps running.
  • Processor
  • Clock / reset
  • Peripheral
  • Control
  • Clock
The Clock Controls of ap_socWho stops which clock, and what keeps running. processor Firmware CLK_GATE, wfi clock reset ap_crg gates and the mux peripheral Five peripherals UART0, GPIO, timer,SPI, DMA processor The core's clock stops in wfi clock reset clk_aon never gated:watchdog CLK_GATE, wfigated clocksclk_core
  1. processor FirmwareCLK_GATE, wfi
    • CLK_GATE, wfi → ap_crg
  2. clock reset ap_crggates and the mux
    • gated clocks → Five peripherals
    • clk_core → The core's clock
  3. peripheral Five peripheralsUART0, GPIO, timer, SPI, DMA
  4. processor The core's clockstops in wfi
  5. clock reset clk_aonnever gated: watchdog
  1. Software gates

    CLK_GATE stops and starts five peripheral clocks.

  2. The core's gate

    The instruction wfi stops the core's own clock until an enabled interrupt.

  3. 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

New words on this page
WordMeaning
Clock gateA cell that lets a clock through or stops it, from an enable.
GlitchA pulse shorter than the clock's own, made when a gate or a mux changes in the middle of a high phase.
Clock switchMoving 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 clockclk_aon: its own oscillator, never gated; the watchdog, ap_pwrctl and the reset controller run on it.
BackstopSomething outside the core that ends a hang: here the watchdog's reset.
Wake sourceAn 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?

Two gates, one enable
GatePulse from 15 nsNext pulse after the enable returns at 37 ns
AND aloneends at 17 ns: 2 ns widea runt from 37 to 40 ns: 3 ns
ap_clk_gateends at 20 ns: wholenone 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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

The table (re: the access must answer ERROR).
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 0x2

Every 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.

The marker event before wfi, and the line after the wake.
EVENT 231: 00000094
CONSOLE 478: awake

Third, a sleep that nothing wakes: no interrupt is enabled. The program arms the watchdog, notes "asleep" in TB_ARG, and executes wfi.

The marker after arming, and the line of the next boot.
EVENT 244: 00000095
CONSOLE 1339: the watchdog ended the sleep, cause 00000005

Rule 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

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_soc has five software gates, one core gate and one switch.

  1. 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.

  2. 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.

  3. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 wide

Check Yourself

Predict the outputThe enable now falls at 19 ns and comes back at 39 ns. What is the last line printed?
// Two clock gates and one enable that drops in the middle of a high phase, at 19 ns. The clock rises at
// 5, 15, 25 ns. Each gated clock's high pulses are printed with their widths.
// ap_clk_gate: a clock gate. The enable is taken on the falling edge, so the gated clock never
// shows a short pulse: it changes its mind only while the clock is low (facts 11, S5.8).
// A real chip uses the gate cell of its library here.
// Plan: docs/soc-verification-plan.md section 3.8, stage E4.
module ap_clk_gate (
  input  logic clk,      // the free clock
  input  logic resetn,   // reset, active low: the gate is open after reset
  input  logic enable,   // 1: let the clock through (a level of the clk domain)
  output logic gclk      // the gated clock
);
  logic open_q;  // the enable as taken at the last falling edge

  always_ff @(negedge clk or negedge resetn) begin
    if (!resetn) begin
      open_q <= 1'b1;
    end else begin
      open_q <= enable;
    end
  end

  assign gclk = clk && open_q;
endmodule

module tb;
  logic clk = 0, resetn = 0, enable = 1;
  wire bare = clk && enable;                       // an AND gate alone
  wire safe;
  ap_clk_gate gate (.clk(clk), .resetn(resetn), .enable(enable), .gclk(safe));
  initial begin #5; forever begin clk = 1; #5; clk = 0; #5; end end
  initial begin #2 resetn = 1; #17 enable = 0; #20 enable = 1; #30 $finish; end
  realtime b_up, s_up;
  always @(posedge bare) b_up = $realtime;
  always @(negedge bare) $display("AND gate:     high from %0.0f to %0.0f ns, %0.0f ns wide", b_up, $realtime, $realtime - b_up);
  always @(posedge safe) s_up = $realtime;
  always @(negedge safe) $display("ap_clk_gate:  high from %0.0f to %0.0f ns, %0.0f ns wide", s_up, $realtime, $realtime - s_up);
endmodule
ConceptIn the lab, what did UART0 answer while its gate was closed?
ConceptWhat ended the sleep that nothing could wake?
ConceptWhy can the chip bench not see a glitch on a gated clock?
ConceptThe DMA's gate is closed while a copy runs. What happens?

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.

Practice

Report a problem with this page