summaryrefslogtreecommitdiff
path: root/utils/hwstub/tools/lua/stmp/clkctrl.lua
diff options
context:
space:
mode:
Diffstat (limited to 'utils/hwstub/tools/lua/stmp/clkctrl.lua')
-rw-r--r--utils/hwstub/tools/lua/stmp/clkctrl.lua174
1 files changed, 174 insertions, 0 deletions
diff --git a/utils/hwstub/tools/lua/stmp/clkctrl.lua b/utils/hwstub/tools/lua/stmp/clkctrl.lua
new file mode 100644
index 0000000000..a6b84f9f64
--- /dev/null
+++ b/utils/hwstub/tools/lua/stmp/clkctrl.lua
@@ -0,0 +1,174 @@
1---
2--- DIGCTL
3---
4STMP.clkctrl = {}
5
6local h = HELP:get_topic("STMP"):create_topic("clkctrl")
7h:add("The STMP.clkctrl table handles the clkctrl device for all STMPs.")
8
9local hh = h:create_topic("list_all")
10hh:add("The STMP.clkctrl.list_all() function returns the list of all clocks names.")
11
12local hh = h:create_topic("is_enabled")
13hh:add("The STMP.clkctrl.is_enabled(clk) function returns the state of the clock gate or true is there is none.")
14hh:add("Note that some clock is disabled though other means than a clock gate (divider gate).")
15
16local hh = h:create_topic("is_enabled")
17hh:add("The STMP.clkctrl.is_enabled(clk) function returns the state of the clock gate or true is there is none.")
18hh:add("Note that some clock is disabled though other means than a clock gate (divider gate).")
19
20local hh = h:create_topic("get_div")
21hh:add("The STMP.clkctrl.get_div(clk) function returns the integer divider of the clock or 1 if there is none.")
22
23local hh = h:create_topic("get_frac_div")
24hh:add("The STMP.clkctrl.get_frac_div(clk) function returns the fractional divider of the clock gate or 1 is there is none.")
25hh:add("Note that the effect of a fractional divider might depend on other fields or on the clock itself.")
26
27local hh = h:create_topic("get_bypass")
28hh:add("The STMP.clkctrl.get_bypass(clk) function returns the state of the PLL bypass of the clock or false if there is none.")
29
30local hh = h:create_topic("get_freq")
31hh:add("The STMP.clkctrl.get_frac(clk) function returns the frequency of the clock in HZ, or 0 if it is disabled.")
32
33function STMP.clkctrl.list_all()
34 local list = {"clk_pll", "clk_xtal", "clk_io", "clk_cpu", "clk_hbus", "clk_ssp",
35 "clk_emi", "clk_xbus", "clk_filt", "clk_dri", "clk_pwm", "clk_timrot",
36 "clk_uart"}
37 if hwstub.dev.stmp.chipid >= 0x3700 then
38 table.insert(list, "clk_pix")
39 end
40 return list
41end
42
43function STMP.clkctrl.is_enabled(clk)
44 if clk == "clk_pll" then return HW.CLKCTRL.PLLCTRL0.POWER.read() == 1
45 elseif clk == "clk_pix" then return HW.CLKCTRL.PIX.CLKGATE.read() == 0
46 elseif clk == "clk_ssp" then return HW.CLKCTRL.SSP.CLKGATE.read() == 0
47 elseif clk == "clk_dri" then return HW.CLKCTRL.XTAL.DRI_CLK24M_GATE.read() == 0
48 elseif clk == "clk_pwm" then return HW.CLKCTRL.XTAL.PWM_CLK24M_GATE.read() == 0
49 elseif clk == "clk_uart" then return HW.CLKCTRL.XTAL.UART_CLK24M_GATE.read() == 0
50 elseif clk == "clk_filt" then return HW.CLKCTRL.XTAL.FILT_CLK24M_GATE.read() == 0
51 elseif clk == "clk_timrot" then return HW.CLKCTRL.XTAL.TIMROT_CLK24M_GATE.read() == 0
52 else return true end
53end
54
55function STMP.clkctrl.get_div(clk)
56 if hwstub.dev.stmp.chipid >= 0x3700 then
57 if clk == "clk_pix" then return HW.CLKCTRL.PIX.DIV.read()
58 elseif clk == "clk_cpu" then return HW.CLKCTRL.CPU.DIV_CPU.read()
59 elseif clk == "clk_emi" then return HW.CLKCTRL.EMI.DIV_EMI.read() end
60 else
61 if clk == "clk_cpu" then return HW.CLKCTRL.CPU.DIV.read()
62 elseif clk == "clk_emi" then return HW.CLKCTRL.EMI.DIV.read() end
63 end
64 if clk == "clk_ssp" then return HW.CLKCTRL.SSP.DIV.read()
65 elseif clk == "clk_hbus" then return HW.CLKCTRL.HBUS.DIV.read()
66 elseif clk == "clk_xbus" then return HW.CLKCTRL.XBUS.read()
67 else return 0 end
68end
69
70function STMP.clkctrl.get_frac_div(clk)
71 local name = nil
72 if hwstub.dev.stmp.chipid >= 0x3700 and clk == "clk_pix" then name = "PIX"
73 elseif clk == "clk_io" then name = "IO"
74 elseif clk == "clk_cpu" then name = "CPU"
75 elseif clk == "clk_emi" then name = "EMI"
76 else return 0 end
77
78 if name == nil then return 0 end
79 if HW.CLKCTRL.FRAC["CLKGATE" .. name].read() == 1 then return 0
80 else return HW.CLKCTRL.FRAC[name .. "FRAC"].read() end
81end
82
83function STMP.clkctrl.get_bypass(clk)
84 if hwstub.dev.stmp.chipid >= 0x3700 and clk == "clk_pix" then return HW.CLKCTRL.CLKSEQ.BYPASS_PIX.read() == 1
85 elseif clk == "clk_ssp" then return HW.CLKCTRL.CLKSEQ.BYPASS_SSP.read() == 1
86 elseif clk == "clk_cpu" then return HW.CLKCTRL.CLKSEQ.BYPASS_CPU.read() == 1
87 elseif clk == "clk_emi" then return HW.CLKCTRL.CLKSEQ.BYPASS_EMI.read() == 1
88 else return false end
89end
90
91function STMP.clkctrl.get_freq(clk)
92 if clk == "clk_pll" then
93 return STMP.clkctrl.is_enabled(clk) and 480000000 or 0
94 elseif clk == "clk_xtal" then return 24000000
95 elseif clk == "clk_cpu" then
96 if hwstub.dev.stmp.chipid >= 0x3700 then
97 if STMP.clkctrl.get_bypass(clk) then
98 local ref = STMP.clkctrl.get_freq("clk_xtal")
99 if HW.CLKCTRL.CPU.DIV_XTAL_FRAC_EN.read() == 1 then
100 return ref * HW.CLKCTRL.CPU.DIV_XTAL.read() / 32
101 else
102 return ref / STMP.clkctrl.get_div(clk)
103 end
104 else
105 local ref = STMP.clkctrl.get_freq("clk_pll")
106 if STMP.clkctrl.get_frac_div(clk) ~= 0 then
107 ref = ref * 18 / STMP.clkctrl.get_frac_div(clk)
108 end
109 return ref / STMP.clkctrl.get_div(clk)
110 end
111 else
112 return STMP.CLKCTRL.get_freq("clk_pll") / STMP.clkctrl.get_div(clk)
113 end
114 elseif clk == "clk_hbus" then
115 local ref = STMP.clkctrl.get_freq("clk_cpu")
116 if hwstub.dev.stmp.chipid >= 0x3700 and STMP.clkctrl.get_frac_div(clk) ~= 0 then
117 ref = ref * STMP.clkctrl.get_frac_div(clk) / 32
118 end
119 if STMP.clkctrl.get_div(clk) ~= 0 then
120 ref = ref / STMP.clkctrl.get_div(clk)
121 end
122 return ref
123 elseif clk == "clk_io" then
124 local ref = STMP.clkctrl.get_freq("clk_pll")
125 if hwstub.dev.stmp.chipid >= 0x3700 and STMP.clkctrl.get_frac_div(clk) ~= 0 then
126 ref = ref * 18 / STMP.clkctrl.get_frac_div(clk)
127 end
128 return ref
129 elseif hwstub.dev.stmp.chipid >= 0x3700 and clk == "clk_pix" then
130 local ref = nil
131 if not STMP.clkctrl.is_enabled(clk) then
132 ref = 0
133 elseif STMP.clkctrl.get_bypass(clk) then
134 ref = STMP.clkctrl.get_freq("clk_xtal")
135 else
136 ref = STMP.clkctrl.get_freq("clk_pll")
137 if STMP.clkctrl.get_frac_div(clk) ~= 0 then
138 ref = ref * 18 / STMP.clkctrl.get_frac_div(clk)
139 else
140 ref = 0
141 end
142 end
143 return ref / STMP.clkctrl.get_div(clk)
144 elseif clk == "clk_ssp" then
145 local ref = nil
146 if not STMP.clkctrl.is_enabled(clk) then
147 ref = 0
148 elseif hwstub.dev.stmp.chipid >= 0x3700 and STMP.clkctrl.get_bypass(clk) then
149 ref = STMP.clkctrl.get_freq("clk_xtal")
150 else
151 ref = STMP.clkctrl.get_freq("clk_io")
152 end
153 return ref / STMP.clkctrl.get_div(clk)
154 elseif clk == "clk_emi" then
155 if hwstub.dev.stmp.chipid >= 0x3700 then
156 if STMP.clkctrl.get_bypass("clk_emi") then
157 if HW.CLKCTRL.EMI.CLKGATE.read() then return 0
158 else return STMP.clkctrl.get_freq("clk_xtal") / HW.CLKCTRL.EMI.DIV_XTAL.read() end
159 else
160 local ref = STMP.clkctrl.get_freq("clk_pll")
161 if STMP.clkctrl.get_frac_div(clk) ~= 0 then
162 ref = ref * 18 / STMP.clkctrl.get_frac_div(clk)
163 end
164 return ref / STMP.clkctrl.get_div(clk)
165 end
166 else
167 return STMP.clkctrl.get_freq("clk_pll") / STMP.clkctrl.get_div(clk);
168 end
169 elseif clk == "clk_xbus" then
170 return STMP.clkctrl.get_freq("clk_xtal") / STMP.clkctrl.get_div(clk)
171 else
172 return 0
173 end
174end