The Glitch-Free Clock Mux

The chip starts on one clock and moves to another while it runs. The switch between them is a mux, and an ordinary mux is the wrong part for the job. See why, and how the right part takes turns.

You Will Be Able To

  • Explain why an ordinary mux makes a short pulse when it switches between two running clocks.
  • Give the order of the steps in a glitch-free clock mux, and say on whose edge each one happens.
  • Explain why the output pauses during a switch, and why a stopped source stops the switch.
Lesson
S5.10
Time
26 min
Runs on
Your browser
Check
6 questions

The chip moves from its slow clock to its fast one a thousand times without trouble. Then one counter skips a step. The rate is right before the switch and right after it. So what went past in between?

Before You Start

You need Clock Sources and the Chip-Level Clock Tree: the RC oscillator, the PLL, lock and the source mux. You need Dividers and Clock Gates: a clock gate changes only while its clock is low.

One step of this page uses two flip-flops in a row to take in a signal from another clock. A page of the design track explains why. You can follow this page without it.

Five questions this page answers
Question
1Why does an ordinary mux make a short pulse when it switches between two clocks?
2In a glitch-free clock mux, which happens first: the old source goes off, or the new one comes on?
3On which edge of which clock does a grant change?
4Why does the clock root pause during a switch?
5What happens when the new source has stopped?

The Idea: Switch off, Then Switch On

A mux for data is simple: the select changes and the output follows the other input at once. That is fine for data, which is looked at only on a clock edge. A clock is looked at all the time.

Put two running clocks on an ordinary mux. The request to switch comes when software sends it, at any point of either clock. The output jumps from one clock to the other in the middle of a pulse. What comes out can be a pulse far shorter than any real one. It is a runt pulse, with the same harm as on the last page.

The cure reuses the clock gate. Give each source a gate of its own, and let the gates take turns. First the gate of the old source closes, on a falling edge of the old clock. Only after that the gate of the new source opens, on a falling edge of the new clock.

Each step waits for a moment when its own clock is low, so no pulse is ever cut. Between the two steps both gates are closed and the output rests at 0. A switch therefore costs a short pause. It also needs edges from both sources, and that is a trap of its own.

New Words

New words on this page
WordMeaning
Clock muxA circuit that passes one of two or more clocks to its output.
Glitch-free clock muxA clock mux whose output carries whole pulses only, also while it switches.
Switch requestThe bit from software that names the wanted source. This page says request for short.
Grant of a sourceA stored bit, one for each source. While it is 1, the gate of that source is open.
HandoverThe time from the request until the new source reaches the output.
Stopped sourceA clock source that no longer toggles: an oscillator that is off, or a PLL without its reference.

One Picture: Two Gates That Take Turns

A Clock Mux Is Two Gates That Take TurnsStep through the picture. It builds the mux from the clock gate of the last page.
  • Clock / reset
  • Control / FSM
  • Memory
  • Bus
A Clock Mux Is Two Gates That Take TurnsStep through the picture. It builds the mux from the clock gate of the last page. component RC clock the source in use component PLL clock the source thatsoftware wants component Request from software: 0 forRC, 1 for PLL component RC grant andgate changes on fallingedges of the RC clock component PLL grant andgate changes on fallingedges of the PLLclock component OR gate joins the two gatedclocks component Clock root whole pulses only clockclockwants RCwants PLLRC grant is offPLL grant is offgated RCgated PLLone clock
  1. component RC clockthe source in use
    • clock → RC grant and gate
  2. component PLL clockthe source that software wants
    • clock → PLL grant and gate
  3. component Requestfrom software: 0 for RC, 1 for PLL
    • wants RC → RC grant and gate
    • wants PLL → PLL grant and gate
  4. component RC grant and gatechanges on falling edges of the RC clock
    • RC grant is off → PLL grant and gate
    • gated RC → OR gate
  5. component PLL grant and gatechanges on falling edges of the PLL clock
    • PLL grant is off → RC grant and gate
    • gated PLL → OR gate
  6. component OR gatejoins the two gated clocks
    • one clock → Clock root
  7. component Clock rootwhole pulses only
  1. Two sources, one request

    Two clocks run side by side. They share no edges. Software asks for one of them with a single bit, at a moment of its own choosing.

  2. A gate for each source

    Each source gets a clock gate of its own. A stored bit opens it: call that bit the grant of the source. A grant changes only on a falling edge of its own clock.

  3. The outputs are joined

    An OR gate joins the two gated clocks. While one grant is 1 and the other is 0, the root carries exactly one source.

  4. The old source goes off first

    The request changes. The RC gate sees that it is no longer wanted. On a falling edge of the RC clock, its grant goes off. The last RC pulse was a whole one.

  5. Only then the new source comes on

    The PLL gate is wanted, but it must also see that the RC grant is off. Then, on a falling edge of the PLL clock, its grant comes on. The first PLL pulse is a whole one.

  6. The pause in between

    From the moment the old grant goes off until the new grant comes on, both gates are closed. The root is low. In the lab run it stays low for 25 ns, from 200 to 225 ns.

Read the diagram as text
  • RC clock: the source in use
  • PLL clock: the source that software wants
  • Request: from software: 0 for RC, 1 for PLL
  • RC grant and gate: changes on falling edges of the RC clock
  • PLL grant and gate: changes on falling edges of the PLL clock
  • OR gate: joins the two gated clocks
  • Clock root: whole pulses only
  • RC clock → RC grant and gate: clock
  • PLL clock → PLL grant and gate: clock
  • Request → RC grant and gate: wants RC
  • Request → PLL grant and gate: wants PLL
  • RC grant and gate → PLL grant and gate: RC grant is off
  • PLL grant and gate → RC grant and gate: PLL grant is off
  • RC grant and gate → OR gate: gated RC
  • PLL grant and gate → OR gate: gated PLL
  • OR gate → Clock root: one clock

The glitch-free clock mux as two clock gates. The lab models its behaviour; the first practice problem builds its gates.

The Smallest Example, Worked by Hand

Use the clocks of the lab chip. The RC clock has a period of 40 ns. It rises at 140 and 180 ns and falls at 160 and 200 ns. The PLL clock has a period of 10 ns. It rises at 145, 155 and so on, and falls at 150, 160 and so on.

Software sends the request at 141 ns. The RC clock rose 1 ns ago and is high. The PLL clock is low.

The same request at two muxes, by hand
TimeOrdinary muxGlitch-free mux
140 nsPasses RC: the output rises.Passes RC: the output rises.
141 nsPasses the PLL clock at once. It is low, so the output falls. A pulse of 1 ns.Nothing yet. The RC grant waits for falling edges of RC.
160 nsFollows the PLL clock.RC falls. The pulse that began at 140 was a whole 20 ns.
200 nsFollows the PLL clock.RC falls again: the RC grant goes off. Both grants are 0.
210, 220 nsFollows the PLL clock.The PLL clock falls twice. At 220 the PLL grant comes on.
225 nsFollows the PLL clock.First PLL rise at the output. The output was low from 200 to 225: 25 ns.

Why two falling edges at each step, and not one? Each gate takes the request in through two flip-flops in a row. The request comes from another clock, and two flip-flops give it time to settle. Our lab models that as a wait of two edges.

An Ordinary Mux: A Pulse of One NanosecondStep through the samples. Each column is one nanosecond. The first column is 139 ns.
Runt 01234567891011RC clockPLL clockrequestordinary muxglitch-free mux
Scroll horizontally ↔
  1. Cycle 1

    At 140 ns the RC clock rises. Both muxes pass it: both outputs go high.

  2. Cycle 2

    At 141 ns the request changes. The ordinary mux now passes the PLL clock, which is low. Its pulse ends after 1 ns.

  3. Cycle 6

    At 145 ns the PLL clock rises, and the ordinary mux follows it. The glitch-free mux is still passing the RC pulse.

  4. Cycle 11

    The glitch-free mux has not changed its source yet. It lets the RC pulse run its full 20 ns.

Read phases and sample points (1 ns)
  • Runt: columns 1 through 1.

Recorded samples from the lab on Icarus, one for each nanosecond from 139 ns to 150 ns.

The Glitch-Free Mux: Old off, a Pause, New OnStep through the samples. Each column is 5 ns. The first column is at 143 ns.
012345678910111213141516171819RC clockRC grantPLL clockPLL grantclock root
Scroll horizontally ↔
  1. Cycle 0

    The request has just changed. The RC grant is still 1, so the root still carries the RC clock.

  2. Cycle 8

    The RC clock has fallen once since the request, at 160 ns. The mux waits for a second falling edge. One more whole RC pulse passes.

  3. Cycle 12

    At 200 ns the RC clock falls again and the RC grant goes off. Both grants are 0 now. The root is low.

  4. Cycle 16

    At 220 ns, on the second falling edge of the PLL clock, the PLL grant comes on. The PLL clock is low at that moment.

  5. Cycle 17

    The first rising edge of the PLL clock reaches the root at 225 ns. Every pulse from here on is a whole PLL pulse.

Recorded samples from the lab on Icarus, one for each 5 ns from 143 ns to 238 ns.

The Rules

  1. Rule 1: An ordinary mux must not switch between running clocks.

    Its output changes source in the middle of a pulse.

  2. Rule 2: Each source has a grant, and the output is the OR of the granted clocks.

    A source with a grant of 0 gives a steady 0.

  3. Rule 3: A grant changes only on a falling edge of its own clock.

    So the pulse before the change, and the pulse after it, are whole.

  4. Rule 4: The new grant comes on only after the old grant is off.

    The two gates are never open together.

  5. Rule 5: The output is low between the two grants.

    The pause depends on both clocks. In the lab it is 25 ns: over a PLL period, under an RC period. Every block on that clock must bear it.

  6. Rule 6: Each step needs an edge of its own source.

    A stopped source can give no edge. The switch then never finishes.

Sources for These Rules

The glitch-free clock mux of this page is common design practice. No book or standard in our reference library describes it, so no rule here is quoted from one.

Rule 3 rests on the rule of the last page, that an enable must be stable while its clock is high: Weste and Harris, CMOS VLSI Design, fourth edition, section 10.3.5.

Two flip-flops in a row for a signal from another clock: Weste and Harris, CMOS VLSI Design, fourth edition, section 10.6.2.

The count of two falling edges at each step, and the times, belong to our lab model.

Now Run It

The lab at the end of this page gives one request to two muxes. The first is an ordinary mux. The second is a model of the glitch-free mux: it models the waiting, not the gates. A third mux gets a PLL input that never toggles.

Printed by the lab on Icarus. All times are in nanoseconds.
request t=141 rc=1 pll=0
plain high=1 ns
root  high=20 ns
rc grant off t=200
pll grant on t=220
root low for 25 ns
root  high=5 ns source=PLL
dead source: rc_grant=0 pll_grant=0 root=0

Rule 1 at work: the ordinary mux lets out a pulse of 1 ns. Rule 3 at work: the same pulse at the glitch-free mux is 20 ns, a whole RC pulse.

Rule 4 at work: the RC grant goes off at 200. The PLL grant comes on at 220, never the other way round. Rule 5 at work: the root is low for 25 ns. The first PLL pulse is high for 5 ns, half of its period.

Rule 6 at work, in the last row. The RC grant went off as it should. The PLL input gave no falling edge, so the PLL grant never came on. Both grants are 0 and the root is 0, for good.

This lab leaves things out. The model waits for edges with a few lines of test code. It has no flip-flops, so it cannot show the settling that the two flip-flops are for. Both sources are ideal.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Two gates that take turnsAn ordinary mux, and a reset of the chip after each switchThe chip keeps running through a switch. The mux needs edges from both sources.
Off before onOpen the new gate first, for a seamless switchNo overlap, so no pulse made of two clocks. A pause on the output.
Grants change on falling edgesGrants change on rising edgesThe gate closes while its clock is low, so nothing is cut. The same rule as the clock gate.
Two flip-flops at each gateOne flip-flopTime for a request from another clock to settle. One more period of waiting at each step.

The Trap: A Switch to a Stopped Clock

Back to the skipped count. That chip had an ordinary mux, and the request was not tied to either clock. Most requests fell where the two clocks agreed. One fell 1 ns after a rising edge, and a pulse of 1 ns went down the tree.

The glitch-free mux removes that fault and brings a new one. Suppose boot code asks for the PLL, and the crystal on this board is broken. The PLL has no reference clock, and its output never moves.

The mux does its duty. It closes the RC gate and waits for a falling edge of the PLL clock. None comes. Now the chip has no clock at all. The code that could ask for RC again needs a clock to run.

There are two cures, and a good chip has both. Software asks for the PLL only after lock. And a small supervisor on a clock of its own watches the switch. It forces a known state if the switch does not finish. The debug problem on this page is about that supervisor.

At Scale

This page2 sources, 1 mux
A phone chipdozens of muxes, several PLLs

This section looks ahead. One mux between two sources becomes a clock controller that software drives through registers.

  1. A status bit for the switch

    A clock controller reports which source is in use now. Software reads it after a request, and does not assume.

  2. A clock monitor

    A counter on an independent clock checks that each source still toggles. It can refuse a request for a stopped source.

  3. The timing check must know

    Two clocks reach every flip-flop below the mux, and never at once. The program that checks timing must be told so. The page on declaring clocks shows how.

  4. The pause matters to devices

    A UART in the middle of a byte sees its clock stop and change rate. Software switches clocks when its devices are idle.

Try It

Run the lab. An ordinary mux and the glitch-free mux get the same request at 141 ns. The last row comes from a second glitch-free mux whose PLL input is held at 0.

Move the request from 141 to 148. The PLL clock is high then. Predict the width of the cut pulse.

Move the request to 163, when both clocks are low. Does the ordinary mux still make a short pulse?

Names Printed by the Lab
NameMeaning
requestThe time at which software asks for the PLL, and the level of each clock at that moment.
plain highThe width of the pulse that the ordinary mux was passing when the request came.
root highThe width of the same pulse at the output of the glitch-free mux.
rc grant offThe time at which the gate of the RC clock closes.
pll grant onThe time at which the gate of the PLL clock opens.
root low forHow long the root stayed low between its last RC pulse and its first PLL pulse.
dead sourceThe grants and the output of the second mux, whose PLL input never toggles.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: request t=141 rc=1 pll=0 plain high=1 ns root high=20 ns rc grant off t=200 pll grant on t=220 root low for 25 ns root high=5 ns source=PLL dead source: rc_grant=0 pll_grant=0 root=0
Read included file: clock_source_model.sv
// Simulation-only nominal sources. REF_PERIOD is in ns; no jitter or analog loop.
// REF_START: how long the crystal oscillator takes to start, in ns (0: it runs from the first moment).
`timescale 1ns/1ns
module clock_source_model #(parameter REF_PERIOD = 20, LOCK_EDGES = 5, REF_START = 0)
  (output bit clk_ref, clk_rc, clk_pll, locked);
  bit ref_running;
  initial #(REF_START) ref_running <= 1;
  always #(REF_PERIOD/2) if (ref_running) clk_ref = ~clk_ref;
  always #20 clk_rc = ~clk_rc;
  initial begin
    @(posedge clk_ref);
    forever #(REF_PERIOD/4) clk_pll = ~clk_pll;
  end
  initial begin
    repeat (LOCK_EDGES) @(posedge clk_ref);
    locked <= 1;
  end
endmodule
Read included file: clock_path_model.sv
// Transaction-level handoff for the lab. Both sources must keep running.
// Start on RC. The requester holds its choice until the switch finishes.
module clock_path_model(input wire clk_rc, clk_pll, request_pll, gate_open,
                        output wire clk_root, clk_leaf, output bit clk_div);
  bit pass_rc = 1, pass_pll = 0;
  always begin
    wait (request_pll);
    repeat (2) @(negedge clk_rc);
    pass_rc <= 0;
    @(negedge pass_rc);              // the new source counts only edges after the old grant is off
    repeat (2) @(negedge clk_pll);
    pass_pll <= 1;
    wait (!request_pll);
    repeat (2) @(negedge clk_pll);
    pass_pll <= 0;
    @(negedge pass_pll);
    repeat (2) @(negedge clk_rc);
    pass_rc <= 1;
  end
  assign clk_root = (pass_rc & clk_rc) | (pass_pll & clk_pll);
  always @(posedge clk_root) clk_div <= ~clk_div;
  bit held_open;
  always_latch if (!clk_div) held_open <= gate_open;
  assign clk_leaf = clk_div & held_open;
endmodule

Check Yourself

Predict the outputIn this run the request comes at 163 ns. The RC clock falls at 160, 200 and 240 ns. The run ends when the RC grant goes off. What is the last row the lab prints?
`timescale 1ns/1ns
module tb;
  wire clk_ref, clk_rc, clk_pll, locked;
  clock_source_model #(.REF_PERIOD(20)) sources(.*);   // RC: period 40 ns. PLL: period 10 ns.
  bit request_pll;                             // the request of software: 0 = RC, 1 = PLL

  wire plain = request_pll ? clk_pll : clk_rc; // an ordinary mux, used on two clocks

  wire clk_root;                               // the output of the glitch-free mux
  clock_path_model safe(.clk_rc, .clk_pll, .request_pll, .gate_open(1'b1),
                        .clk_root, .clk_div(), .clk_leaf());
  wire rc_grant = safe.pass_rc, pll_grant = safe.pass_pll;

  time plain_rose, root_rose, root_fell;
  bit watch_plain, watch_root, first_new;
  always @(posedge plain) plain_rose = $time;
  always @(negedge plain) if (watch_plain) begin
    $display("plain high=%0d ns", $time - plain_rose);
    watch_plain = 0;
  end
  always @(posedge clk_root) begin
    root_rose = $time;
    if (first_new) $display("root low for %0d ns", $time - root_fell);
  end
  always @(negedge clk_root) begin
    root_fell = $time;
    if (watch_root) begin $display("root  high=%0d ns", $time - root_rose); watch_root = 0; end
    if (first_new) begin $display("root  high=%0d ns source=PLL", $time - root_rose); first_new = 0; end
  end
  always @(negedge rc_grant) begin $display("rc grant off t=%0d", $time); $finish; end
  always @(posedge pll_grant) begin $display("pll grant on t=%0d", $time); first_new = 1; end

  initial begin
    #163;
    request_pll = 1;                           // software asks for the PLL
    watch_plain = 1;
    watch_root = 1;
    $display("request t=%0d rc=%b pll=%b", $time, clk_rc, clk_pll);
    @(posedge pll_grant);
    repeat (2) @(negedge clk_root);

    #1 $finish;
  end
endmodule
Read included file: clock_source_model.sv
// Simulation-only nominal sources. REF_PERIOD is in ns; no jitter or analog loop.
// REF_START: how long the crystal oscillator takes to start, in ns (0: it runs from the first moment).
`timescale 1ns/1ns
module clock_source_model #(parameter REF_PERIOD = 20, LOCK_EDGES = 5, REF_START = 0)
  (output bit clk_ref, clk_rc, clk_pll, locked);
  bit ref_running;
  initial #(REF_START) ref_running <= 1;
  always #(REF_PERIOD/2) if (ref_running) clk_ref = ~clk_ref;
  always #20 clk_rc = ~clk_rc;
  initial begin
    @(posedge clk_ref);
    forever #(REF_PERIOD/4) clk_pll = ~clk_pll;
  end
  initial begin
    repeat (LOCK_EDGES) @(posedge clk_ref);
    locked <= 1;
  end
endmodule
Read included file: clock_path_model.sv
// Transaction-level handoff for the lab. Both sources must keep running.
// Start on RC. The requester holds its choice until the switch finishes.
module clock_path_model(input wire clk_rc, clk_pll, request_pll, gate_open,
                        output wire clk_root, clk_leaf, output bit clk_div);
  bit pass_rc = 1, pass_pll = 0;
  always begin
    wait (request_pll);
    repeat (2) @(negedge clk_rc);
    pass_rc <= 0;
    @(negedge pass_rc);              // the new source counts only edges after the old grant is off
    repeat (2) @(negedge clk_pll);
    pass_pll <= 1;
    wait (!request_pll);
    repeat (2) @(negedge clk_pll);
    pass_pll <= 0;
    @(negedge pass_pll);
    repeat (2) @(negedge clk_rc);
    pass_rc <= 1;
  end
  assign clk_root = (pass_rc & clk_rc) | (pass_pll & clk_pll);
  always @(posedge clk_root) clk_div <= ~clk_div;
  bit held_open;
  always_latch if (!clk_div) held_open <= gate_open;
  assign clk_leaf = clk_div & held_open;
endmodule
ConceptWhy does an ordinary mux make a short pulse when it switches between two clocks?
ConceptIn a glitch-free clock mux, which step comes first?
ConceptOn which edge does the grant of a source change?
ConceptWhy does the clock root pause during a switch?
ConceptSoftware asks for a source that has stopped. What does the glitch-free mux do?

Common Mistakes

Wrong Switch between two running clocks with an ordinary mux.

The output changes source in the middle of a pulse.

Fix Close the old gate first, then open the new one, each on its own falling edge.

Wrong Expect the new clock on the very next edge.

A handover takes several edges of both clocks, with a pause between.

Fix Read the status of the switch, or wait long enough.

Wrong Ask for a source without checking that it runs.

The mux drops the old source and waits for an edge that never comes.

Fix Check lock first, and keep a supervisor on an independent clock.

Remember

  • An ordinary mux cuts a pulse when it switches between two running clocks.
  • A glitch-free clock mux is two clock gates: the old one closes first, then the new one opens.
  • Each grant changes on a falling edge of its own clock. The root is low in between.
  • A switch needs edges from both sources. A stopped source leaves the chip with no clock.

Practice

Report a problem with this page