blob: ac3b5c714876f3012622503f45f9d8bcfcff3a78 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2019 MediaTek Inc.
4 * Author: Pierre Lee <pierre.lee@mediatek.com>
5 */
6
7#include "clk-fhctl.h"
8#include "sspm_ipi.h"
9
10#define FHCTL_D_LEN 9
11#define MAX_SSC_RATE 8
12
13
14/* SSPM IPI CMD. Should sync with mt_freqhopping.h in tinysys driver. */
15enum FH_DEVCTL_CMD_ID {
16 FH_DCTL_CMD_SSC_ENABLE = 0x1004,
17 FH_DCTL_CMD_SSC_DISABLE = 0x1005,
18 FH_DCTL_CMD_GENERAL_DFS = 0x1006,
19 FH_DCTL_CMD_ARM_DFS = 0x1007,
20 FH_DCTL_CMD_SSC_TBL_CONFIG = 0x100A,
21 FH_DCTL_CMD_PLL_PAUSE = 0x100E,
22 FH_DCTL_CMD_MAX
23};
24
25struct freqhopping_ioctl {
26 unsigned int pll_id;
27 struct freqhopping_ssc {
28 unsigned int idx_pattern; /* idx_pattern: Deprecated Field */
29 unsigned int dt;
30 unsigned int df;
31 unsigned int upbnd;
32 unsigned int lowbnd;
33 unsigned int dds; /* dds: Deprecated Field */
34 } ssc_setting; /* used only when user-define */
35 int result;
36};
37
38struct fhctl_ipi_data {
39 unsigned int cmd;
40 union {
41 struct freqhopping_ioctl fh_ctl;
42 unsigned int args[8];
43 } u;
44};
45
46
47static int fhctl_to_sspm_command(unsigned int cmd,
48 struct fhctl_ipi_data *ipi_data)
49{
50 int ret = 0;
51 unsigned int ack_data = 0;
52
53 pr_debug("send ipi command %x", cmd);
54
55 switch (cmd) {
56 case FH_DCTL_CMD_SSC_ENABLE:
57 case FH_DCTL_CMD_SSC_DISABLE:
58 case FH_DCTL_CMD_GENERAL_DFS:
59 case FH_DCTL_CMD_ARM_DFS:
60 case FH_DCTL_CMD_SSC_TBL_CONFIG:
61 case FH_DCTL_CMD_PLL_PAUSE:
62 ipi_data->cmd = cmd;
63 ret = sspm_ipi_send_sync(IPI_ID_FHCTL, IPI_OPT_POLLING,
64 ipi_data, FHCTL_D_LEN, &ack_data, 1);
65 if (ret != 0)
66 pr_info("sspm_ipi_send_sync error(%d) ret:%d - %d",
67 cmd, ret, ack_data);
68 else if (ack_data < 0)
69 pr_info("cmd(%d) return error(%d)", cmd, ack_data);
70 break;
71 default:
72 pr_info("[Error]Undefined IPI command");
73 break;
74 } /* switch */
75
76 pr_debug("send ipi command %x, response: ack_data: %d",
77 cmd, ack_data);
78
79 return ack_data;
80}
81
82static int clk_mt_fh_sspm_pll_init(struct clk_mt_fhctl *fh)
83{
84 struct fhctl_ipi_data ipi_data;
85 int pll_id;
86
87 pll_id = fh->pll_data->pll_id;
88
89 /* Check default enable SSC */
90 if (fh->pll_data->pll_default_ssc_rate > 0) {
91 /* Init SSPM g_pll_ssc_setting_tbl table */
92 ipi_data.u.fh_ctl.pll_id = pll_id;
93 ipi_data.u.fh_ctl.ssc_setting.dt = 0;
94 ipi_data.u.fh_ctl.ssc_setting.df = 0;
95 ipi_data.u.fh_ctl.ssc_setting.upbnd = 0;
96 ipi_data.u.fh_ctl.ssc_setting.lowbnd =
97 fh->pll_data->pll_default_ssc_rate;
98 fhctl_to_sspm_command(FH_DCTL_CMD_SSC_TBL_CONFIG, &ipi_data);
99
100 pr_debug("Default Enable SSC PLL_ID:%d SSC_RATE:0~-%d",
101 pll_id, fh->pll_data->pll_default_ssc_rate);
102
103 /* Default Enable SSC to 0~-N%; */
104 fh->hal_ops->pll_ssc_enable(fh,
105 fh->pll_data->pll_default_ssc_rate);
106 }
107
108 return 0;
109}
110
111static int __clk_mt_fh_sspm_pll_pause(struct clk_mt_fhctl *fh, bool pause)
112{
113 struct fhctl_ipi_data ipi_data;
114 int pll_id;
115
116 pll_id = fh->pll_data->pll_id;
117
118 /* Only for support pause in CPU PLL. */
119 if (fh->pll_data->pll_type != FH_PLL_TYPE_CPU)
120 return -EPERM;
121
122 ipi_data.u.args[0] = pll_id;
123 ipi_data.u.args[1] = (pause) ? 1 : 0;
124 fhctl_to_sspm_command(FH_DCTL_CMD_PLL_PAUSE, &ipi_data);
125
126 return 0;
127}
128
129static int clk_mt_fh_sspm_pll_unpause(struct clk_mt_fhctl *fh)
130{
131 return __clk_mt_fh_sspm_pll_pause(fh, false);
132}
133
134static int clk_mt_fh_sspm_pll_pause(struct clk_mt_fhctl *fh)
135{
136
137 return __clk_mt_fh_sspm_pll_pause(fh, true);
138}
139
140static int clk_mt_fh_sspm_pll_ssc_disable(struct clk_mt_fhctl *fh)
141{
142 struct freqhopping_ioctl fh_ctl;
143 struct fhctl_ipi_data ipi_data;
144 int pll_id;
145
146 pll_id = fh->pll_data->pll_id;
147
148 fh_ctl.pll_id = pll_id;
149
150 memset(&ipi_data, 0, sizeof(struct fhctl_ipi_data));
151 memcpy(&ipi_data.u.fh_ctl, &fh_ctl,
152 sizeof(struct freqhopping_ioctl));
153
154 fhctl_to_sspm_command(FH_DCTL_CMD_SSC_DISABLE, &ipi_data);
155
156 return 0;
157}
158
159
160static int clk_mt_fh_sspm_pll_ssc_enable(struct clk_mt_fhctl *fh, int ssc_rate)
161{
162 struct freqhopping_ioctl fh_ctl;
163 struct fhctl_ipi_data ipi_data;
164 int pll_id;
165
166 pll_id = fh->pll_data->pll_id;
167 fh_ctl.pll_id = pll_id;
168
169 if (fh->pll_data->pll_type == FH_PLL_TYPE_NOT_SUPPORT) {
170 pr_info("%s not support SSC.", fh->pll_data->pll_name);
171 return -EPERM;
172 }
173
174 if (ssc_rate > MAX_SSC_RATE) {
175 pr_info("[Error] ssc_rate:%d over spec!!!", ssc_rate);
176 return -EINVAL;
177 }
178
179 fh_ctl.ssc_setting.dt = 0; /* default setting */
180 fh_ctl.ssc_setting.df = 9; /* default setting */
181 fh_ctl.ssc_setting.upbnd = 0; /* default setting */
182 fh_ctl.ssc_setting.lowbnd = ssc_rate;
183
184 memset(&ipi_data, 0, sizeof(struct fhctl_ipi_data));
185 memcpy(&ipi_data.u.fh_ctl, &fh_ctl,
186 sizeof(struct freqhopping_ioctl));
187
188 fhctl_to_sspm_command(FH_DCTL_CMD_SSC_ENABLE, &ipi_data);
189
190 pr_info("PLL:%d ssc rate change [O]:%d => [N]:%d ",
191 pll_id, fh->pll_data->pll_default_ssc_rate, ssc_rate);
192
193 /* Update clock ssc rate variable. */
194
195 fh->pll_data->pll_default_ssc_rate = ssc_rate;
196
197 return 0;
198}
199
200static int clk_mt_fh_sspm_pll_hopping(struct clk_mt_fhctl *fh,
201 unsigned int new_dds,
202 int postdiv)
203{
204 struct fhctl_ipi_data ipi_data;
205 int pll_id, cmd_id;
206
207
208 pll_id = fh->pll_data->pll_id;
209
210 /* CPU is forbidden hopping in AP side. (clk driver owner reqest) */
211 if ((fh->pll_data->pll_type == FH_PLL_TYPE_NOT_SUPPORT) ||
212 (fh->pll_data->pll_type == FH_PLL_TYPE_CPU)) {
213 pr_info("%s not support hopping in AP side.",
214 fh->pll_data->pll_name);
215 return 0;
216 }
217
218 cmd_id = FH_DCTL_CMD_GENERAL_DFS;
219
220 memset(&ipi_data, 0, sizeof(struct fhctl_ipi_data));
221 ipi_data.u.args[0] = pll_id;
222 ipi_data.u.args[1] = new_dds;
223 ipi_data.u.args[2] = postdiv;
224
225 fhctl_to_sspm_command(cmd_id, &ipi_data);
226
227 return 0;
228}
229
230const struct clk_mt_fhctl_hal_ops mt_fhctl_hal_ops = {
231 .pll_init = clk_mt_fh_sspm_pll_init,
232 .pll_unpause = clk_mt_fh_sspm_pll_unpause,
233 .pll_pause = clk_mt_fh_sspm_pll_pause,
234 .pll_ssc_disable = clk_mt_fh_sspm_pll_ssc_disable,
235 .pll_ssc_enable = clk_mt_fh_sspm_pll_ssc_enable,
236 .pll_hopping = clk_mt_fh_sspm_pll_hopping,
237};
238
239