]>
Commit | Line | Data |
---|---|---|
8cb7cf56 SH |
1 | /* |
2 | * System Control and Power Interface (SCPI) Message Protocol driver | |
3 | * | |
4 | * SCPI Message Protocol is used between the System Control Processor(SCP) | |
5 | * and the Application Processors(AP). The Message Handling Unit(MHU) | |
6 | * provides a mechanism for inter-processor communication between SCP's | |
7 | * Cortex M3 and AP. | |
8 | * | |
9 | * SCP offers control and management of the core/cluster power states, | |
10 | * various power domain DVFS including the core/cluster, certain system | |
11 | * clocks configuration, thermal sensors and many others. | |
12 | * | |
13 | * Copyright (C) 2015 ARM Ltd. | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or modify it | |
16 | * under the terms and conditions of the GNU General Public License, | |
17 | * version 2, as published by the Free Software Foundation. | |
18 | * | |
19 | * This program is distributed in the hope it will be useful, but WITHOUT | |
20 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
21 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
22 | * more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License along | |
25 | * with this program. If not, see <http://www.gnu.org/licenses/>. | |
26 | */ | |
27 | ||
28 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
29 | ||
30 | #include <linux/bitmap.h> | |
31 | #include <linux/device.h> | |
32 | #include <linux/err.h> | |
33 | #include <linux/export.h> | |
34 | #include <linux/io.h> | |
35 | #include <linux/kernel.h> | |
36 | #include <linux/list.h> | |
37 | #include <linux/mailbox_client.h> | |
38 | #include <linux/module.h> | |
39 | #include <linux/of_address.h> | |
40 | #include <linux/of_platform.h> | |
41 | #include <linux/printk.h> | |
42 | #include <linux/scpi_protocol.h> | |
43 | #include <linux/slab.h> | |
44 | #include <linux/sort.h> | |
45 | #include <linux/spinlock.h> | |
46 | ||
47 | #define CMD_ID_SHIFT 0 | |
48 | #define CMD_ID_MASK 0x7f | |
49 | #define CMD_TOKEN_ID_SHIFT 8 | |
50 | #define CMD_TOKEN_ID_MASK 0xff | |
51 | #define CMD_DATA_SIZE_SHIFT 16 | |
52 | #define CMD_DATA_SIZE_MASK 0x1ff | |
53 | #define PACK_SCPI_CMD(cmd_id, tx_sz) \ | |
54 | ((((cmd_id) & CMD_ID_MASK) << CMD_ID_SHIFT) | \ | |
55 | (((tx_sz) & CMD_DATA_SIZE_MASK) << CMD_DATA_SIZE_SHIFT)) | |
56 | #define ADD_SCPI_TOKEN(cmd, token) \ | |
57 | ((cmd) |= (((token) & CMD_TOKEN_ID_MASK) << CMD_TOKEN_ID_SHIFT)) | |
58 | ||
59 | #define CMD_SIZE(cmd) (((cmd) >> CMD_DATA_SIZE_SHIFT) & CMD_DATA_SIZE_MASK) | |
60 | #define CMD_UNIQ_MASK (CMD_TOKEN_ID_MASK << CMD_TOKEN_ID_SHIFT | CMD_ID_MASK) | |
61 | #define CMD_XTRACT_UNIQ(cmd) ((cmd) & CMD_UNIQ_MASK) | |
62 | ||
63 | #define SCPI_SLOT 0 | |
64 | ||
65 | #define MAX_DVFS_DOMAINS 8 | |
66 | #define MAX_DVFS_OPPS 8 | |
67 | #define DVFS_LATENCY(hdr) (le32_to_cpu(hdr) >> 16) | |
68 | #define DVFS_OPP_COUNT(hdr) ((le32_to_cpu(hdr) >> 8) & 0xff) | |
69 | ||
70 | #define PROTOCOL_REV_MINOR_BITS 16 | |
71 | #define PROTOCOL_REV_MINOR_MASK ((1U << PROTOCOL_REV_MINOR_BITS) - 1) | |
72 | #define PROTOCOL_REV_MAJOR(x) ((x) >> PROTOCOL_REV_MINOR_BITS) | |
73 | #define PROTOCOL_REV_MINOR(x) ((x) & PROTOCOL_REV_MINOR_MASK) | |
74 | ||
75 | #define FW_REV_MAJOR_BITS 24 | |
76 | #define FW_REV_MINOR_BITS 16 | |
77 | #define FW_REV_PATCH_MASK ((1U << FW_REV_MINOR_BITS) - 1) | |
78 | #define FW_REV_MINOR_MASK ((1U << FW_REV_MAJOR_BITS) - 1) | |
79 | #define FW_REV_MAJOR(x) ((x) >> FW_REV_MAJOR_BITS) | |
80 | #define FW_REV_MINOR(x) (((x) & FW_REV_MINOR_MASK) >> FW_REV_MINOR_BITS) | |
81 | #define FW_REV_PATCH(x) ((x) & FW_REV_PATCH_MASK) | |
82 | ||
3bdd8843 | 83 | #define MAX_RX_TIMEOUT (msecs_to_jiffies(30)) |
8cb7cf56 SH |
84 | |
85 | enum scpi_error_codes { | |
86 | SCPI_SUCCESS = 0, /* Success */ | |
87 | SCPI_ERR_PARAM = 1, /* Invalid parameter(s) */ | |
88 | SCPI_ERR_ALIGN = 2, /* Invalid alignment */ | |
89 | SCPI_ERR_SIZE = 3, /* Invalid size */ | |
90 | SCPI_ERR_HANDLER = 4, /* Invalid handler/callback */ | |
91 | SCPI_ERR_ACCESS = 5, /* Invalid access/permission denied */ | |
92 | SCPI_ERR_RANGE = 6, /* Value out of range */ | |
93 | SCPI_ERR_TIMEOUT = 7, /* Timeout has occurred */ | |
94 | SCPI_ERR_NOMEM = 8, /* Invalid memory area or pointer */ | |
95 | SCPI_ERR_PWRSTATE = 9, /* Invalid power state */ | |
96 | SCPI_ERR_SUPPORT = 10, /* Not supported or disabled */ | |
97 | SCPI_ERR_DEVICE = 11, /* Device error */ | |
98 | SCPI_ERR_BUSY = 12, /* Device busy */ | |
99 | SCPI_ERR_MAX | |
100 | }; | |
101 | ||
102 | enum scpi_std_cmd { | |
103 | SCPI_CMD_INVALID = 0x00, | |
104 | SCPI_CMD_SCPI_READY = 0x01, | |
105 | SCPI_CMD_SCPI_CAPABILITIES = 0x02, | |
106 | SCPI_CMD_SET_CSS_PWR_STATE = 0x03, | |
107 | SCPI_CMD_GET_CSS_PWR_STATE = 0x04, | |
108 | SCPI_CMD_SET_SYS_PWR_STATE = 0x05, | |
109 | SCPI_CMD_SET_CPU_TIMER = 0x06, | |
110 | SCPI_CMD_CANCEL_CPU_TIMER = 0x07, | |
111 | SCPI_CMD_DVFS_CAPABILITIES = 0x08, | |
112 | SCPI_CMD_GET_DVFS_INFO = 0x09, | |
113 | SCPI_CMD_SET_DVFS = 0x0a, | |
114 | SCPI_CMD_GET_DVFS = 0x0b, | |
115 | SCPI_CMD_GET_DVFS_STAT = 0x0c, | |
116 | SCPI_CMD_CLOCK_CAPABILITIES = 0x0d, | |
117 | SCPI_CMD_GET_CLOCK_INFO = 0x0e, | |
118 | SCPI_CMD_SET_CLOCK_VALUE = 0x0f, | |
119 | SCPI_CMD_GET_CLOCK_VALUE = 0x10, | |
120 | SCPI_CMD_PSU_CAPABILITIES = 0x11, | |
121 | SCPI_CMD_GET_PSU_INFO = 0x12, | |
122 | SCPI_CMD_SET_PSU = 0x13, | |
123 | SCPI_CMD_GET_PSU = 0x14, | |
124 | SCPI_CMD_SENSOR_CAPABILITIES = 0x15, | |
125 | SCPI_CMD_SENSOR_INFO = 0x16, | |
126 | SCPI_CMD_SENSOR_VALUE = 0x17, | |
127 | SCPI_CMD_SENSOR_CFG_PERIODIC = 0x18, | |
128 | SCPI_CMD_SENSOR_CFG_BOUNDS = 0x19, | |
129 | SCPI_CMD_SENSOR_ASYNC_VALUE = 0x1a, | |
130 | SCPI_CMD_SET_DEVICE_PWR_STATE = 0x1b, | |
131 | SCPI_CMD_GET_DEVICE_PWR_STATE = 0x1c, | |
132 | SCPI_CMD_COUNT | |
133 | }; | |
134 | ||
135 | struct scpi_xfer { | |
136 | u32 slot; /* has to be first element */ | |
137 | u32 cmd; | |
138 | u32 status; | |
139 | const void *tx_buf; | |
140 | void *rx_buf; | |
141 | unsigned int tx_len; | |
142 | unsigned int rx_len; | |
143 | struct list_head node; | |
144 | struct completion done; | |
145 | }; | |
146 | ||
147 | struct scpi_chan { | |
148 | struct mbox_client cl; | |
149 | struct mbox_chan *chan; | |
150 | void __iomem *tx_payload; | |
151 | void __iomem *rx_payload; | |
152 | struct list_head rx_pending; | |
153 | struct list_head xfers_list; | |
154 | struct scpi_xfer *xfers; | |
155 | spinlock_t rx_lock; /* locking for the rx pending list */ | |
156 | struct mutex xfers_lock; | |
157 | u8 token; | |
158 | }; | |
159 | ||
160 | struct scpi_drvinfo { | |
161 | u32 protocol_version; | |
162 | u32 firmware_version; | |
163 | int num_chans; | |
164 | atomic_t next_chan; | |
165 | struct scpi_ops *scpi_ops; | |
166 | struct scpi_chan *channels; | |
167 | struct scpi_dvfs_info *dvfs[MAX_DVFS_DOMAINS]; | |
168 | }; | |
169 | ||
170 | /* | |
171 | * The SCP firmware only executes in little-endian mode, so any buffers | |
172 | * shared through SCPI should have their contents converted to little-endian | |
173 | */ | |
174 | struct scpi_shared_mem { | |
175 | __le32 command; | |
176 | __le32 status; | |
177 | u8 payload[0]; | |
178 | } __packed; | |
179 | ||
180 | struct scp_capabilities { | |
181 | __le32 protocol_version; | |
182 | __le32 event_version; | |
183 | __le32 platform_version; | |
184 | __le32 commands[4]; | |
185 | } __packed; | |
186 | ||
187 | struct clk_get_info { | |
188 | __le16 id; | |
189 | __le16 flags; | |
190 | __le32 min_rate; | |
191 | __le32 max_rate; | |
192 | u8 name[20]; | |
193 | } __packed; | |
194 | ||
195 | struct clk_get_value { | |
196 | __le32 rate; | |
197 | } __packed; | |
198 | ||
199 | struct clk_set_value { | |
200 | __le16 id; | |
201 | __le16 reserved; | |
202 | __le32 rate; | |
203 | } __packed; | |
204 | ||
205 | struct dvfs_info { | |
206 | __le32 header; | |
207 | struct { | |
208 | __le32 freq; | |
209 | __le32 m_volt; | |
210 | } opps[MAX_DVFS_OPPS]; | |
211 | } __packed; | |
212 | ||
8cb7cf56 SH |
213 | struct dvfs_set { |
214 | u8 domain; | |
215 | u8 index; | |
216 | } __packed; | |
217 | ||
38a1bdc9 PA |
218 | struct sensor_capabilities { |
219 | __le16 sensors; | |
220 | } __packed; | |
221 | ||
222 | struct _scpi_sensor_info { | |
223 | __le16 sensor_id; | |
224 | u8 class; | |
225 | u8 trigger_type; | |
226 | char name[20]; | |
227 | }; | |
228 | ||
229 | struct sensor_value { | |
2e874159 SH |
230 | __le32 lo_val; |
231 | __le32 hi_val; | |
38a1bdc9 PA |
232 | } __packed; |
233 | ||
8cb7cf56 SH |
234 | static struct scpi_drvinfo *scpi_info; |
235 | ||
236 | static int scpi_linux_errmap[SCPI_ERR_MAX] = { | |
237 | /* better than switch case as long as return value is continuous */ | |
238 | 0, /* SCPI_SUCCESS */ | |
239 | -EINVAL, /* SCPI_ERR_PARAM */ | |
240 | -ENOEXEC, /* SCPI_ERR_ALIGN */ | |
241 | -EMSGSIZE, /* SCPI_ERR_SIZE */ | |
242 | -EINVAL, /* SCPI_ERR_HANDLER */ | |
243 | -EACCES, /* SCPI_ERR_ACCESS */ | |
244 | -ERANGE, /* SCPI_ERR_RANGE */ | |
245 | -ETIMEDOUT, /* SCPI_ERR_TIMEOUT */ | |
246 | -ENOMEM, /* SCPI_ERR_NOMEM */ | |
247 | -EINVAL, /* SCPI_ERR_PWRSTATE */ | |
248 | -EOPNOTSUPP, /* SCPI_ERR_SUPPORT */ | |
249 | -EIO, /* SCPI_ERR_DEVICE */ | |
250 | -EBUSY, /* SCPI_ERR_BUSY */ | |
251 | }; | |
252 | ||
253 | static inline int scpi_to_linux_errno(int errno) | |
254 | { | |
255 | if (errno >= SCPI_SUCCESS && errno < SCPI_ERR_MAX) | |
256 | return scpi_linux_errmap[errno]; | |
257 | return -EIO; | |
258 | } | |
259 | ||
260 | static void scpi_process_cmd(struct scpi_chan *ch, u32 cmd) | |
261 | { | |
262 | unsigned long flags; | |
263 | struct scpi_xfer *t, *match = NULL; | |
264 | ||
265 | spin_lock_irqsave(&ch->rx_lock, flags); | |
266 | if (list_empty(&ch->rx_pending)) { | |
267 | spin_unlock_irqrestore(&ch->rx_lock, flags); | |
268 | return; | |
269 | } | |
270 | ||
271 | list_for_each_entry(t, &ch->rx_pending, node) | |
272 | if (CMD_XTRACT_UNIQ(t->cmd) == CMD_XTRACT_UNIQ(cmd)) { | |
273 | list_del(&t->node); | |
274 | match = t; | |
275 | break; | |
276 | } | |
277 | /* check if wait_for_completion is in progress or timed-out */ | |
278 | if (match && !completion_done(&match->done)) { | |
279 | struct scpi_shared_mem *mem = ch->rx_payload; | |
280 | unsigned int len = min(match->rx_len, CMD_SIZE(cmd)); | |
281 | ||
282 | match->status = le32_to_cpu(mem->status); | |
283 | memcpy_fromio(match->rx_buf, mem->payload, len); | |
284 | if (match->rx_len > len) | |
285 | memset(match->rx_buf + len, 0, match->rx_len - len); | |
286 | complete(&match->done); | |
287 | } | |
288 | spin_unlock_irqrestore(&ch->rx_lock, flags); | |
289 | } | |
290 | ||
291 | static void scpi_handle_remote_msg(struct mbox_client *c, void *msg) | |
292 | { | |
293 | struct scpi_chan *ch = container_of(c, struct scpi_chan, cl); | |
294 | struct scpi_shared_mem *mem = ch->rx_payload; | |
295 | u32 cmd = le32_to_cpu(mem->command); | |
296 | ||
297 | scpi_process_cmd(ch, cmd); | |
298 | } | |
299 | ||
300 | static void scpi_tx_prepare(struct mbox_client *c, void *msg) | |
301 | { | |
302 | unsigned long flags; | |
303 | struct scpi_xfer *t = msg; | |
304 | struct scpi_chan *ch = container_of(c, struct scpi_chan, cl); | |
305 | struct scpi_shared_mem *mem = (struct scpi_shared_mem *)ch->tx_payload; | |
306 | ||
307 | if (t->tx_buf) | |
308 | memcpy_toio(mem->payload, t->tx_buf, t->tx_len); | |
309 | if (t->rx_buf) { | |
310 | if (!(++ch->token)) | |
311 | ++ch->token; | |
312 | ADD_SCPI_TOKEN(t->cmd, ch->token); | |
313 | spin_lock_irqsave(&ch->rx_lock, flags); | |
314 | list_add_tail(&t->node, &ch->rx_pending); | |
315 | spin_unlock_irqrestore(&ch->rx_lock, flags); | |
316 | } | |
317 | mem->command = cpu_to_le32(t->cmd); | |
318 | } | |
319 | ||
320 | static struct scpi_xfer *get_scpi_xfer(struct scpi_chan *ch) | |
321 | { | |
322 | struct scpi_xfer *t; | |
323 | ||
324 | mutex_lock(&ch->xfers_lock); | |
325 | if (list_empty(&ch->xfers_list)) { | |
326 | mutex_unlock(&ch->xfers_lock); | |
327 | return NULL; | |
328 | } | |
329 | t = list_first_entry(&ch->xfers_list, struct scpi_xfer, node); | |
330 | list_del(&t->node); | |
331 | mutex_unlock(&ch->xfers_lock); | |
332 | return t; | |
333 | } | |
334 | ||
335 | static void put_scpi_xfer(struct scpi_xfer *t, struct scpi_chan *ch) | |
336 | { | |
337 | mutex_lock(&ch->xfers_lock); | |
338 | list_add_tail(&t->node, &ch->xfers_list); | |
339 | mutex_unlock(&ch->xfers_lock); | |
340 | } | |
341 | ||
342 | static int scpi_send_message(u8 cmd, void *tx_buf, unsigned int tx_len, | |
343 | void *rx_buf, unsigned int rx_len) | |
344 | { | |
345 | int ret; | |
346 | u8 chan; | |
347 | struct scpi_xfer *msg; | |
348 | struct scpi_chan *scpi_chan; | |
349 | ||
350 | chan = atomic_inc_return(&scpi_info->next_chan) % scpi_info->num_chans; | |
351 | scpi_chan = scpi_info->channels + chan; | |
352 | ||
353 | msg = get_scpi_xfer(scpi_chan); | |
354 | if (!msg) | |
355 | return -ENOMEM; | |
356 | ||
357 | msg->slot = BIT(SCPI_SLOT); | |
358 | msg->cmd = PACK_SCPI_CMD(cmd, tx_len); | |
359 | msg->tx_buf = tx_buf; | |
360 | msg->tx_len = tx_len; | |
361 | msg->rx_buf = rx_buf; | |
362 | msg->rx_len = rx_len; | |
363 | init_completion(&msg->done); | |
364 | ||
365 | ret = mbox_send_message(scpi_chan->chan, msg); | |
366 | if (ret < 0 || !rx_buf) | |
367 | goto out; | |
368 | ||
369 | if (!wait_for_completion_timeout(&msg->done, MAX_RX_TIMEOUT)) | |
370 | ret = -ETIMEDOUT; | |
371 | else | |
372 | /* first status word */ | |
dd9a1d69 | 373 | ret = msg->status; |
8cb7cf56 SH |
374 | out: |
375 | if (ret < 0 && rx_buf) /* remove entry from the list if timed-out */ | |
376 | scpi_process_cmd(scpi_chan, msg->cmd); | |
377 | ||
378 | put_scpi_xfer(msg, scpi_chan); | |
379 | /* SCPI error codes > 0, translate them to Linux scale*/ | |
380 | return ret > 0 ? scpi_to_linux_errno(ret) : ret; | |
381 | } | |
382 | ||
383 | static u32 scpi_get_version(void) | |
384 | { | |
385 | return scpi_info->protocol_version; | |
386 | } | |
387 | ||
388 | static int | |
389 | scpi_clk_get_range(u16 clk_id, unsigned long *min, unsigned long *max) | |
390 | { | |
391 | int ret; | |
392 | struct clk_get_info clk; | |
393 | __le16 le_clk_id = cpu_to_le16(clk_id); | |
394 | ||
395 | ret = scpi_send_message(SCPI_CMD_GET_CLOCK_INFO, &le_clk_id, | |
396 | sizeof(le_clk_id), &clk, sizeof(clk)); | |
397 | if (!ret) { | |
398 | *min = le32_to_cpu(clk.min_rate); | |
399 | *max = le32_to_cpu(clk.max_rate); | |
400 | } | |
401 | return ret; | |
402 | } | |
403 | ||
404 | static unsigned long scpi_clk_get_val(u16 clk_id) | |
405 | { | |
406 | int ret; | |
407 | struct clk_get_value clk; | |
408 | __le16 le_clk_id = cpu_to_le16(clk_id); | |
409 | ||
410 | ret = scpi_send_message(SCPI_CMD_GET_CLOCK_VALUE, &le_clk_id, | |
411 | sizeof(le_clk_id), &clk, sizeof(clk)); | |
412 | return ret ? ret : le32_to_cpu(clk.rate); | |
413 | } | |
414 | ||
415 | static int scpi_clk_set_val(u16 clk_id, unsigned long rate) | |
416 | { | |
417 | int stat; | |
418 | struct clk_set_value clk = { | |
419 | .id = cpu_to_le16(clk_id), | |
420 | .rate = cpu_to_le32(rate) | |
421 | }; | |
422 | ||
423 | return scpi_send_message(SCPI_CMD_SET_CLOCK_VALUE, &clk, sizeof(clk), | |
424 | &stat, sizeof(stat)); | |
425 | } | |
426 | ||
427 | static int scpi_dvfs_get_idx(u8 domain) | |
428 | { | |
429 | int ret; | |
f9d91de0 | 430 | u8 dvfs_idx; |
8cb7cf56 SH |
431 | |
432 | ret = scpi_send_message(SCPI_CMD_GET_DVFS, &domain, sizeof(domain), | |
f9d91de0 SH |
433 | &dvfs_idx, sizeof(dvfs_idx)); |
434 | return ret ? ret : dvfs_idx; | |
8cb7cf56 SH |
435 | } |
436 | ||
437 | static int scpi_dvfs_set_idx(u8 domain, u8 index) | |
438 | { | |
439 | int stat; | |
440 | struct dvfs_set dvfs = {domain, index}; | |
441 | ||
442 | return scpi_send_message(SCPI_CMD_SET_DVFS, &dvfs, sizeof(dvfs), | |
443 | &stat, sizeof(stat)); | |
444 | } | |
445 | ||
446 | static int opp_cmp_func(const void *opp1, const void *opp2) | |
447 | { | |
448 | const struct scpi_opp *t1 = opp1, *t2 = opp2; | |
449 | ||
450 | return t1->freq - t2->freq; | |
451 | } | |
452 | ||
453 | static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain) | |
454 | { | |
455 | struct scpi_dvfs_info *info; | |
456 | struct scpi_opp *opp; | |
457 | struct dvfs_info buf; | |
458 | int ret, i; | |
459 | ||
460 | if (domain >= MAX_DVFS_DOMAINS) | |
461 | return ERR_PTR(-EINVAL); | |
462 | ||
463 | if (scpi_info->dvfs[domain]) /* data already populated */ | |
464 | return scpi_info->dvfs[domain]; | |
465 | ||
466 | ret = scpi_send_message(SCPI_CMD_GET_DVFS_INFO, &domain, sizeof(domain), | |
467 | &buf, sizeof(buf)); | |
468 | ||
469 | if (ret) | |
470 | return ERR_PTR(ret); | |
471 | ||
472 | info = kmalloc(sizeof(*info), GFP_KERNEL); | |
473 | if (!info) | |
474 | return ERR_PTR(-ENOMEM); | |
475 | ||
476 | info->count = DVFS_OPP_COUNT(buf.header); | |
477 | info->latency = DVFS_LATENCY(buf.header) * 1000; /* uS to nS */ | |
478 | ||
479 | info->opps = kcalloc(info->count, sizeof(*opp), GFP_KERNEL); | |
480 | if (!info->opps) { | |
481 | kfree(info); | |
482 | return ERR_PTR(-ENOMEM); | |
483 | } | |
484 | ||
485 | for (i = 0, opp = info->opps; i < info->count; i++, opp++) { | |
486 | opp->freq = le32_to_cpu(buf.opps[i].freq); | |
487 | opp->m_volt = le32_to_cpu(buf.opps[i].m_volt); | |
488 | } | |
489 | ||
490 | sort(info->opps, info->count, sizeof(*opp), opp_cmp_func, NULL); | |
491 | ||
492 | scpi_info->dvfs[domain] = info; | |
493 | return info; | |
494 | } | |
495 | ||
38a1bdc9 PA |
496 | static int scpi_sensor_get_capability(u16 *sensors) |
497 | { | |
498 | struct sensor_capabilities cap_buf; | |
499 | int ret; | |
500 | ||
501 | ret = scpi_send_message(SCPI_CMD_SENSOR_CAPABILITIES, NULL, 0, &cap_buf, | |
502 | sizeof(cap_buf)); | |
503 | if (!ret) | |
504 | *sensors = le16_to_cpu(cap_buf.sensors); | |
505 | ||
506 | return ret; | |
507 | } | |
508 | ||
509 | static int scpi_sensor_get_info(u16 sensor_id, struct scpi_sensor_info *info) | |
510 | { | |
511 | __le16 id = cpu_to_le16(sensor_id); | |
512 | struct _scpi_sensor_info _info; | |
513 | int ret; | |
514 | ||
515 | ret = scpi_send_message(SCPI_CMD_SENSOR_INFO, &id, sizeof(id), | |
516 | &_info, sizeof(_info)); | |
517 | if (!ret) { | |
518 | memcpy(info, &_info, sizeof(*info)); | |
519 | info->sensor_id = le16_to_cpu(_info.sensor_id); | |
520 | } | |
521 | ||
522 | return ret; | |
523 | } | |
524 | ||
3678b98f | 525 | static int scpi_sensor_get_value(u16 sensor, u64 *val) |
38a1bdc9 | 526 | { |
dd9a1d69 | 527 | __le16 id = cpu_to_le16(sensor); |
38a1bdc9 PA |
528 | struct sensor_value buf; |
529 | int ret; | |
530 | ||
dd9a1d69 | 531 | ret = scpi_send_message(SCPI_CMD_SENSOR_VALUE, &id, sizeof(id), |
38a1bdc9 PA |
532 | &buf, sizeof(buf)); |
533 | if (!ret) | |
2e874159 SH |
534 | *val = (u64)le32_to_cpu(buf.hi_val) << 32 | |
535 | le32_to_cpu(buf.lo_val); | |
38a1bdc9 PA |
536 | |
537 | return ret; | |
538 | } | |
539 | ||
8cb7cf56 SH |
540 | static struct scpi_ops scpi_ops = { |
541 | .get_version = scpi_get_version, | |
542 | .clk_get_range = scpi_clk_get_range, | |
543 | .clk_get_val = scpi_clk_get_val, | |
544 | .clk_set_val = scpi_clk_set_val, | |
545 | .dvfs_get_idx = scpi_dvfs_get_idx, | |
546 | .dvfs_set_idx = scpi_dvfs_set_idx, | |
547 | .dvfs_get_info = scpi_dvfs_get_info, | |
38a1bdc9 PA |
548 | .sensor_get_capability = scpi_sensor_get_capability, |
549 | .sensor_get_info = scpi_sensor_get_info, | |
550 | .sensor_get_value = scpi_sensor_get_value, | |
8cb7cf56 SH |
551 | }; |
552 | ||
553 | struct scpi_ops *get_scpi_ops(void) | |
554 | { | |
555 | return scpi_info ? scpi_info->scpi_ops : NULL; | |
556 | } | |
557 | EXPORT_SYMBOL_GPL(get_scpi_ops); | |
558 | ||
559 | static int scpi_init_versions(struct scpi_drvinfo *info) | |
560 | { | |
561 | int ret; | |
562 | struct scp_capabilities caps; | |
563 | ||
564 | ret = scpi_send_message(SCPI_CMD_SCPI_CAPABILITIES, NULL, 0, | |
565 | &caps, sizeof(caps)); | |
566 | if (!ret) { | |
567 | info->protocol_version = le32_to_cpu(caps.protocol_version); | |
568 | info->firmware_version = le32_to_cpu(caps.platform_version); | |
569 | } | |
570 | return ret; | |
571 | } | |
572 | ||
573 | static ssize_t protocol_version_show(struct device *dev, | |
574 | struct device_attribute *attr, char *buf) | |
575 | { | |
576 | struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev); | |
577 | ||
578 | return sprintf(buf, "%d.%d\n", | |
579 | PROTOCOL_REV_MAJOR(scpi_info->protocol_version), | |
580 | PROTOCOL_REV_MINOR(scpi_info->protocol_version)); | |
581 | } | |
582 | static DEVICE_ATTR_RO(protocol_version); | |
583 | ||
584 | static ssize_t firmware_version_show(struct device *dev, | |
585 | struct device_attribute *attr, char *buf) | |
586 | { | |
587 | struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev); | |
588 | ||
589 | return sprintf(buf, "%d.%d.%d\n", | |
590 | FW_REV_MAJOR(scpi_info->firmware_version), | |
591 | FW_REV_MINOR(scpi_info->firmware_version), | |
592 | FW_REV_PATCH(scpi_info->firmware_version)); | |
593 | } | |
594 | static DEVICE_ATTR_RO(firmware_version); | |
595 | ||
596 | static struct attribute *versions_attrs[] = { | |
597 | &dev_attr_firmware_version.attr, | |
598 | &dev_attr_protocol_version.attr, | |
599 | NULL, | |
600 | }; | |
601 | ATTRIBUTE_GROUPS(versions); | |
602 | ||
603 | static void | |
604 | scpi_free_channels(struct device *dev, struct scpi_chan *pchan, int count) | |
605 | { | |
606 | int i; | |
607 | ||
608 | for (i = 0; i < count && pchan->chan; i++, pchan++) { | |
609 | mbox_free_channel(pchan->chan); | |
610 | devm_kfree(dev, pchan->xfers); | |
611 | devm_iounmap(dev, pchan->rx_payload); | |
612 | } | |
613 | } | |
614 | ||
615 | static int scpi_remove(struct platform_device *pdev) | |
616 | { | |
617 | int i; | |
618 | struct device *dev = &pdev->dev; | |
619 | struct scpi_drvinfo *info = platform_get_drvdata(pdev); | |
620 | ||
621 | scpi_info = NULL; /* stop exporting SCPI ops through get_scpi_ops */ | |
622 | ||
623 | of_platform_depopulate(dev); | |
624 | sysfs_remove_groups(&dev->kobj, versions_groups); | |
625 | scpi_free_channels(dev, info->channels, info->num_chans); | |
626 | platform_set_drvdata(pdev, NULL); | |
627 | ||
628 | for (i = 0; i < MAX_DVFS_DOMAINS && info->dvfs[i]; i++) { | |
629 | kfree(info->dvfs[i]->opps); | |
630 | kfree(info->dvfs[i]); | |
631 | } | |
632 | devm_kfree(dev, info->channels); | |
633 | devm_kfree(dev, info); | |
634 | ||
635 | return 0; | |
636 | } | |
637 | ||
638 | #define MAX_SCPI_XFERS 10 | |
639 | static int scpi_alloc_xfer_list(struct device *dev, struct scpi_chan *ch) | |
640 | { | |
641 | int i; | |
642 | struct scpi_xfer *xfers; | |
643 | ||
644 | xfers = devm_kzalloc(dev, MAX_SCPI_XFERS * sizeof(*xfers), GFP_KERNEL); | |
645 | if (!xfers) | |
646 | return -ENOMEM; | |
647 | ||
648 | ch->xfers = xfers; | |
649 | for (i = 0; i < MAX_SCPI_XFERS; i++, xfers++) | |
650 | list_add_tail(&xfers->node, &ch->xfers_list); | |
651 | return 0; | |
652 | } | |
653 | ||
654 | static int scpi_probe(struct platform_device *pdev) | |
655 | { | |
656 | int count, idx, ret; | |
657 | struct resource res; | |
658 | struct scpi_chan *scpi_chan; | |
659 | struct device *dev = &pdev->dev; | |
660 | struct device_node *np = dev->of_node; | |
661 | ||
662 | scpi_info = devm_kzalloc(dev, sizeof(*scpi_info), GFP_KERNEL); | |
663 | if (!scpi_info) | |
664 | return -ENOMEM; | |
665 | ||
666 | count = of_count_phandle_with_args(np, "mboxes", "#mbox-cells"); | |
667 | if (count < 0) { | |
668 | dev_err(dev, "no mboxes property in '%s'\n", np->full_name); | |
669 | return -ENODEV; | |
670 | } | |
671 | ||
672 | scpi_chan = devm_kcalloc(dev, count, sizeof(*scpi_chan), GFP_KERNEL); | |
673 | if (!scpi_chan) | |
674 | return -ENOMEM; | |
675 | ||
676 | for (idx = 0; idx < count; idx++) { | |
677 | resource_size_t size; | |
678 | struct scpi_chan *pchan = scpi_chan + idx; | |
679 | struct mbox_client *cl = &pchan->cl; | |
680 | struct device_node *shmem = of_parse_phandle(np, "shmem", idx); | |
681 | ||
682 | if (of_address_to_resource(shmem, 0, &res)) { | |
683 | dev_err(dev, "failed to get SCPI payload mem resource\n"); | |
684 | ret = -EINVAL; | |
685 | goto err; | |
686 | } | |
687 | ||
688 | size = resource_size(&res); | |
689 | pchan->rx_payload = devm_ioremap(dev, res.start, size); | |
690 | if (!pchan->rx_payload) { | |
691 | dev_err(dev, "failed to ioremap SCPI payload\n"); | |
692 | ret = -EADDRNOTAVAIL; | |
693 | goto err; | |
694 | } | |
695 | pchan->tx_payload = pchan->rx_payload + (size >> 1); | |
696 | ||
697 | cl->dev = dev; | |
698 | cl->rx_callback = scpi_handle_remote_msg; | |
699 | cl->tx_prepare = scpi_tx_prepare; | |
700 | cl->tx_block = true; | |
3bdd8843 | 701 | cl->tx_tout = 20; |
8cb7cf56 SH |
702 | cl->knows_txdone = false; /* controller can't ack */ |
703 | ||
704 | INIT_LIST_HEAD(&pchan->rx_pending); | |
705 | INIT_LIST_HEAD(&pchan->xfers_list); | |
706 | spin_lock_init(&pchan->rx_lock); | |
707 | mutex_init(&pchan->xfers_lock); | |
708 | ||
709 | ret = scpi_alloc_xfer_list(dev, pchan); | |
710 | if (!ret) { | |
711 | pchan->chan = mbox_request_channel(cl, idx); | |
712 | if (!IS_ERR(pchan->chan)) | |
713 | continue; | |
714 | ret = PTR_ERR(pchan->chan); | |
715 | if (ret != -EPROBE_DEFER) | |
716 | dev_err(dev, "failed to get channel%d err %d\n", | |
717 | idx, ret); | |
718 | } | |
719 | err: | |
720 | scpi_free_channels(dev, scpi_chan, idx); | |
721 | scpi_info = NULL; | |
722 | return ret; | |
723 | } | |
724 | ||
725 | scpi_info->channels = scpi_chan; | |
726 | scpi_info->num_chans = count; | |
727 | platform_set_drvdata(pdev, scpi_info); | |
728 | ||
729 | ret = scpi_init_versions(scpi_info); | |
730 | if (ret) { | |
731 | dev_err(dev, "incorrect or no SCP firmware found\n"); | |
732 | scpi_remove(pdev); | |
733 | return ret; | |
734 | } | |
735 | ||
736 | _dev_info(dev, "SCP Protocol %d.%d Firmware %d.%d.%d version\n", | |
737 | PROTOCOL_REV_MAJOR(scpi_info->protocol_version), | |
738 | PROTOCOL_REV_MINOR(scpi_info->protocol_version), | |
739 | FW_REV_MAJOR(scpi_info->firmware_version), | |
740 | FW_REV_MINOR(scpi_info->firmware_version), | |
741 | FW_REV_PATCH(scpi_info->firmware_version)); | |
742 | scpi_info->scpi_ops = &scpi_ops; | |
743 | ||
744 | ret = sysfs_create_groups(&dev->kobj, versions_groups); | |
745 | if (ret) | |
746 | dev_err(dev, "unable to create sysfs version group\n"); | |
747 | ||
748 | return of_platform_populate(dev->of_node, NULL, NULL, dev); | |
749 | } | |
750 | ||
751 | static const struct of_device_id scpi_of_match[] = { | |
752 | {.compatible = "arm,scpi"}, | |
753 | {}, | |
754 | }; | |
755 | ||
756 | MODULE_DEVICE_TABLE(of, scpi_of_match); | |
757 | ||
758 | static struct platform_driver scpi_driver = { | |
759 | .driver = { | |
760 | .name = "scpi_protocol", | |
761 | .of_match_table = scpi_of_match, | |
762 | }, | |
763 | .probe = scpi_probe, | |
764 | .remove = scpi_remove, | |
765 | }; | |
766 | module_platform_driver(scpi_driver); | |
767 | ||
768 | MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>"); | |
769 | MODULE_DESCRIPTION("ARM SCPI mailbox protocol driver"); | |
770 | MODULE_LICENSE("GPL v2"); |