Commit | Line | Data |
---|---|---|
aa276781 NM |
1 | /* |
2 | * Texas Instruments System Control Interface Protocol Driver | |
3 | * | |
4 | * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/ | |
5 | * Nishanth Menon | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any | |
12 | * kind, whether express or implied; without even the implied warranty | |
13 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | */ | |
16 | ||
17 | #define pr_fmt(fmt) "%s: " fmt, __func__ | |
18 | ||
19 | #include <linux/bitmap.h> | |
20 | #include <linux/debugfs.h> | |
21 | #include <linux/export.h> | |
22 | #include <linux/io.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/mailbox_client.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/of_device.h> | |
27 | #include <linux/semaphore.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/soc/ti/ti-msgmgr.h> | |
30 | #include <linux/soc/ti/ti_sci_protocol.h> | |
912cffb4 | 31 | #include <linux/reboot.h> |
aa276781 NM |
32 | |
33 | #include "ti_sci.h" | |
34 | ||
35 | /* List of all TI SCI devices active in system */ | |
36 | static LIST_HEAD(ti_sci_list); | |
37 | /* Protection for the entire list */ | |
38 | static DEFINE_MUTEX(ti_sci_list_mutex); | |
39 | ||
40 | /** | |
41 | * struct ti_sci_xfer - Structure representing a message flow | |
42 | * @tx_message: Transmit message | |
43 | * @rx_len: Receive message length | |
44 | * @xfer_buf: Preallocated buffer to store receive message | |
45 | * Since we work with request-ACK protocol, we can | |
46 | * reuse the same buffer for the rx path as we | |
47 | * use for the tx path. | |
48 | * @done: completion event | |
49 | */ | |
50 | struct ti_sci_xfer { | |
51 | struct ti_msgmgr_message tx_message; | |
52 | u8 rx_len; | |
53 | u8 *xfer_buf; | |
54 | struct completion done; | |
55 | }; | |
56 | ||
57 | /** | |
58 | * struct ti_sci_xfers_info - Structure to manage transfer information | |
59 | * @sem_xfer_count: Counting Semaphore for managing max simultaneous | |
60 | * Messages. | |
61 | * @xfer_block: Preallocated Message array | |
62 | * @xfer_alloc_table: Bitmap table for allocated messages. | |
63 | * Index of this bitmap table is also used for message | |
64 | * sequence identifier. | |
65 | * @xfer_lock: Protection for message allocation | |
66 | */ | |
67 | struct ti_sci_xfers_info { | |
68 | struct semaphore sem_xfer_count; | |
69 | struct ti_sci_xfer *xfer_block; | |
70 | unsigned long *xfer_alloc_table; | |
71 | /* protect transfer allocation */ | |
72 | spinlock_t xfer_lock; | |
73 | }; | |
74 | ||
75 | /** | |
76 | * struct ti_sci_desc - Description of SoC integration | |
77 | * @host_id: Host identifier representing the compute entity | |
78 | * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds) | |
79 | * @max_msgs: Maximum number of messages that can be pending | |
80 | * simultaneously in the system | |
81 | * @max_msg_size: Maximum size of data per message that can be handled. | |
82 | */ | |
83 | struct ti_sci_desc { | |
84 | u8 host_id; | |
85 | int max_rx_timeout_ms; | |
86 | int max_msgs; | |
87 | int max_msg_size; | |
88 | }; | |
89 | ||
90 | /** | |
91 | * struct ti_sci_info - Structure representing a TI SCI instance | |
92 | * @dev: Device pointer | |
93 | * @desc: SoC description for this instance | |
912cffb4 | 94 | * @nb: Reboot Notifier block |
aa276781 NM |
95 | * @d: Debugfs file entry |
96 | * @debug_region: Memory region where the debug message are available | |
97 | * @debug_region_size: Debug region size | |
98 | * @debug_buffer: Buffer allocated to copy debug messages. | |
99 | * @handle: Instance of TI SCI handle to send to clients. | |
100 | * @cl: Mailbox Client | |
101 | * @chan_tx: Transmit mailbox channel | |
102 | * @chan_rx: Receive mailbox channel | |
103 | * @minfo: Message info | |
104 | * @node: list head | |
105 | * @users: Number of users of this instance | |
106 | */ | |
107 | struct ti_sci_info { | |
108 | struct device *dev; | |
912cffb4 | 109 | struct notifier_block nb; |
aa276781 NM |
110 | const struct ti_sci_desc *desc; |
111 | struct dentry *d; | |
112 | void __iomem *debug_region; | |
113 | char *debug_buffer; | |
114 | size_t debug_region_size; | |
115 | struct ti_sci_handle handle; | |
116 | struct mbox_client cl; | |
117 | struct mbox_chan *chan_tx; | |
118 | struct mbox_chan *chan_rx; | |
119 | struct ti_sci_xfers_info minfo; | |
120 | struct list_head node; | |
121 | /* protected by ti_sci_list_mutex */ | |
122 | int users; | |
912cffb4 | 123 | |
aa276781 NM |
124 | }; |
125 | ||
126 | #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl) | |
127 | #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle) | |
912cffb4 | 128 | #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb) |
aa276781 NM |
129 | |
130 | #ifdef CONFIG_DEBUG_FS | |
131 | ||
132 | /** | |
133 | * ti_sci_debug_show() - Helper to dump the debug log | |
134 | * @s: sequence file pointer | |
135 | * @unused: unused. | |
136 | * | |
137 | * Return: 0 | |
138 | */ | |
139 | static int ti_sci_debug_show(struct seq_file *s, void *unused) | |
140 | { | |
141 | struct ti_sci_info *info = s->private; | |
142 | ||
143 | memcpy_fromio(info->debug_buffer, info->debug_region, | |
144 | info->debug_region_size); | |
145 | /* | |
146 | * We don't trust firmware to leave NULL terminated last byte (hence | |
147 | * we have allocated 1 extra 0 byte). Since we cannot guarantee any | |
148 | * specific data format for debug messages, We just present the data | |
149 | * in the buffer as is - we expect the messages to be self explanatory. | |
150 | */ | |
151 | seq_puts(s, info->debug_buffer); | |
152 | return 0; | |
153 | } | |
154 | ||
155 | /** | |
156 | * ti_sci_debug_open() - debug file open | |
157 | * @inode: inode pointer | |
158 | * @file: file pointer | |
159 | * | |
160 | * Return: result of single_open | |
161 | */ | |
162 | static int ti_sci_debug_open(struct inode *inode, struct file *file) | |
163 | { | |
164 | return single_open(file, ti_sci_debug_show, inode->i_private); | |
165 | } | |
166 | ||
167 | /* log file operations */ | |
168 | static const struct file_operations ti_sci_debug_fops = { | |
169 | .open = ti_sci_debug_open, | |
170 | .read = seq_read, | |
171 | .llseek = seq_lseek, | |
172 | .release = single_release, | |
173 | }; | |
174 | ||
175 | /** | |
176 | * ti_sci_debugfs_create() - Create log debug file | |
177 | * @pdev: platform device pointer | |
178 | * @info: Pointer to SCI entity information | |
179 | * | |
180 | * Return: 0 if all went fine, else corresponding error. | |
181 | */ | |
182 | static int ti_sci_debugfs_create(struct platform_device *pdev, | |
183 | struct ti_sci_info *info) | |
184 | { | |
185 | struct device *dev = &pdev->dev; | |
186 | struct resource *res; | |
187 | char debug_name[50] = "ti_sci_debug@"; | |
188 | ||
189 | /* Debug region is optional */ | |
190 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, | |
191 | "debug_messages"); | |
192 | info->debug_region = devm_ioremap_resource(dev, res); | |
193 | if (IS_ERR(info->debug_region)) | |
194 | return 0; | |
195 | info->debug_region_size = resource_size(res); | |
196 | ||
197 | info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1, | |
198 | sizeof(char), GFP_KERNEL); | |
199 | if (!info->debug_buffer) | |
200 | return -ENOMEM; | |
201 | /* Setup NULL termination */ | |
202 | info->debug_buffer[info->debug_region_size] = 0; | |
203 | ||
204 | info->d = debugfs_create_file(strncat(debug_name, dev_name(dev), | |
76cefef8 AB |
205 | sizeof(debug_name) - |
206 | sizeof("ti_sci_debug@")), | |
aa276781 NM |
207 | 0444, NULL, info, &ti_sci_debug_fops); |
208 | if (IS_ERR(info->d)) | |
209 | return PTR_ERR(info->d); | |
210 | ||
211 | dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n", | |
212 | info->debug_region, info->debug_region_size, res); | |
213 | return 0; | |
214 | } | |
215 | ||
216 | /** | |
217 | * ti_sci_debugfs_destroy() - clean up log debug file | |
218 | * @pdev: platform device pointer | |
219 | * @info: Pointer to SCI entity information | |
220 | */ | |
221 | static void ti_sci_debugfs_destroy(struct platform_device *pdev, | |
222 | struct ti_sci_info *info) | |
223 | { | |
224 | if (IS_ERR(info->debug_region)) | |
225 | return; | |
226 | ||
227 | debugfs_remove(info->d); | |
228 | } | |
229 | #else /* CONFIG_DEBUG_FS */ | |
230 | static inline int ti_sci_debugfs_create(struct platform_device *dev, | |
231 | struct ti_sci_info *info) | |
232 | { | |
233 | return 0; | |
234 | } | |
235 | ||
236 | static inline void ti_sci_debugfs_destroy(struct platform_device *dev, | |
237 | struct ti_sci_info *info) | |
238 | { | |
239 | } | |
240 | #endif /* CONFIG_DEBUG_FS */ | |
241 | ||
242 | /** | |
243 | * ti_sci_dump_header_dbg() - Helper to dump a message header. | |
244 | * @dev: Device pointer corresponding to the SCI entity | |
245 | * @hdr: pointer to header. | |
246 | */ | |
247 | static inline void ti_sci_dump_header_dbg(struct device *dev, | |
248 | struct ti_sci_msg_hdr *hdr) | |
249 | { | |
250 | dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n", | |
251 | hdr->type, hdr->host, hdr->seq, hdr->flags); | |
252 | } | |
253 | ||
254 | /** | |
255 | * ti_sci_rx_callback() - mailbox client callback for receive messages | |
256 | * @cl: client pointer | |
257 | * @m: mailbox message | |
258 | * | |
259 | * Processes one received message to appropriate transfer information and | |
260 | * signals completion of the transfer. | |
261 | * | |
262 | * NOTE: This function will be invoked in IRQ context, hence should be | |
263 | * as optimal as possible. | |
264 | */ | |
265 | static void ti_sci_rx_callback(struct mbox_client *cl, void *m) | |
266 | { | |
267 | struct ti_sci_info *info = cl_to_ti_sci_info(cl); | |
268 | struct device *dev = info->dev; | |
269 | struct ti_sci_xfers_info *minfo = &info->minfo; | |
270 | struct ti_msgmgr_message *mbox_msg = m; | |
271 | struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf; | |
272 | struct ti_sci_xfer *xfer; | |
273 | u8 xfer_id; | |
274 | ||
275 | xfer_id = hdr->seq; | |
276 | ||
277 | /* | |
278 | * Are we even expecting this? | |
279 | * NOTE: barriers were implicit in locks used for modifying the bitmap | |
280 | */ | |
281 | if (!test_bit(xfer_id, minfo->xfer_alloc_table)) { | |
282 | dev_err(dev, "Message for %d is not expected!\n", xfer_id); | |
283 | return; | |
284 | } | |
285 | ||
286 | xfer = &minfo->xfer_block[xfer_id]; | |
287 | ||
288 | /* Is the message of valid length? */ | |
289 | if (mbox_msg->len > info->desc->max_msg_size) { | |
bd0fa74e | 290 | dev_err(dev, "Unable to handle %zu xfer(max %d)\n", |
aa276781 NM |
291 | mbox_msg->len, info->desc->max_msg_size); |
292 | ti_sci_dump_header_dbg(dev, hdr); | |
293 | return; | |
294 | } | |
295 | if (mbox_msg->len < xfer->rx_len) { | |
bd0fa74e | 296 | dev_err(dev, "Recv xfer %zu < expected %d length\n", |
aa276781 NM |
297 | mbox_msg->len, xfer->rx_len); |
298 | ti_sci_dump_header_dbg(dev, hdr); | |
299 | return; | |
300 | } | |
301 | ||
302 | ti_sci_dump_header_dbg(dev, hdr); | |
303 | /* Take a copy to the rx buffer.. */ | |
304 | memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len); | |
305 | complete(&xfer->done); | |
306 | } | |
307 | ||
308 | /** | |
309 | * ti_sci_get_one_xfer() - Allocate one message | |
310 | * @info: Pointer to SCI entity information | |
311 | * @msg_type: Message type | |
312 | * @msg_flags: Flag to set for the message | |
313 | * @tx_message_size: transmit message size | |
314 | * @rx_message_size: receive message size | |
315 | * | |
316 | * Helper function which is used by various command functions that are | |
317 | * exposed to clients of this driver for allocating a message traffic event. | |
318 | * | |
319 | * This function can sleep depending on pending requests already in the system | |
320 | * for the SCI entity. Further, this also holds a spinlock to maintain integrity | |
321 | * of internal data structures. | |
322 | * | |
323 | * Return: 0 if all went fine, else corresponding error. | |
324 | */ | |
325 | static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info, | |
326 | u16 msg_type, u32 msg_flags, | |
327 | size_t tx_message_size, | |
328 | size_t rx_message_size) | |
329 | { | |
330 | struct ti_sci_xfers_info *minfo = &info->minfo; | |
331 | struct ti_sci_xfer *xfer; | |
332 | struct ti_sci_msg_hdr *hdr; | |
333 | unsigned long flags; | |
334 | unsigned long bit_pos; | |
335 | u8 xfer_id; | |
336 | int ret; | |
337 | int timeout; | |
338 | ||
339 | /* Ensure we have sane transfer sizes */ | |
340 | if (rx_message_size > info->desc->max_msg_size || | |
341 | tx_message_size > info->desc->max_msg_size || | |
342 | rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr)) | |
343 | return ERR_PTR(-ERANGE); | |
344 | ||
345 | /* | |
346 | * Ensure we have only controlled number of pending messages. | |
347 | * Ideally, we might just have to wait a single message, be | |
348 | * conservative and wait 5 times that.. | |
349 | */ | |
350 | timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5; | |
351 | ret = down_timeout(&minfo->sem_xfer_count, timeout); | |
352 | if (ret < 0) | |
353 | return ERR_PTR(ret); | |
354 | ||
355 | /* Keep the locked section as small as possible */ | |
356 | spin_lock_irqsave(&minfo->xfer_lock, flags); | |
357 | bit_pos = find_first_zero_bit(minfo->xfer_alloc_table, | |
358 | info->desc->max_msgs); | |
359 | set_bit(bit_pos, minfo->xfer_alloc_table); | |
360 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); | |
361 | ||
362 | /* | |
363 | * We already ensured in probe that we can have max messages that can | |
364 | * fit in hdr.seq - NOTE: this improves access latencies | |
365 | * to predictable O(1) access, BUT, it opens us to risk if | |
366 | * remote misbehaves with corrupted message sequence responses. | |
367 | * If that happens, we are going to be messed up anyways.. | |
368 | */ | |
369 | xfer_id = (u8)bit_pos; | |
370 | ||
371 | xfer = &minfo->xfer_block[xfer_id]; | |
372 | ||
373 | hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf; | |
374 | xfer->tx_message.len = tx_message_size; | |
375 | xfer->rx_len = (u8)rx_message_size; | |
376 | ||
377 | reinit_completion(&xfer->done); | |
378 | ||
379 | hdr->seq = xfer_id; | |
380 | hdr->type = msg_type; | |
381 | hdr->host = info->desc->host_id; | |
382 | hdr->flags = msg_flags; | |
383 | ||
384 | return xfer; | |
385 | } | |
386 | ||
387 | /** | |
388 | * ti_sci_put_one_xfer() - Release a message | |
389 | * @minfo: transfer info pointer | |
390 | * @xfer: message that was reserved by ti_sci_get_one_xfer | |
391 | * | |
392 | * This holds a spinlock to maintain integrity of internal data structures. | |
393 | */ | |
394 | static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo, | |
395 | struct ti_sci_xfer *xfer) | |
396 | { | |
397 | unsigned long flags; | |
398 | struct ti_sci_msg_hdr *hdr; | |
399 | u8 xfer_id; | |
400 | ||
401 | hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf; | |
402 | xfer_id = hdr->seq; | |
403 | ||
404 | /* | |
405 | * Keep the locked section as small as possible | |
406 | * NOTE: we might escape with smp_mb and no lock here.. | |
407 | * but just be conservative and symmetric. | |
408 | */ | |
409 | spin_lock_irqsave(&minfo->xfer_lock, flags); | |
410 | clear_bit(xfer_id, minfo->xfer_alloc_table); | |
411 | spin_unlock_irqrestore(&minfo->xfer_lock, flags); | |
412 | ||
413 | /* Increment the count for the next user to get through */ | |
414 | up(&minfo->sem_xfer_count); | |
415 | } | |
416 | ||
417 | /** | |
418 | * ti_sci_do_xfer() - Do one transfer | |
419 | * @info: Pointer to SCI entity information | |
420 | * @xfer: Transfer to initiate and wait for response | |
421 | * | |
422 | * Return: -ETIMEDOUT in case of no response, if transmit error, | |
423 | * return corresponding error, else if all goes well, | |
424 | * return 0. | |
425 | */ | |
426 | static inline int ti_sci_do_xfer(struct ti_sci_info *info, | |
427 | struct ti_sci_xfer *xfer) | |
428 | { | |
429 | int ret; | |
430 | int timeout; | |
431 | struct device *dev = info->dev; | |
432 | ||
433 | ret = mbox_send_message(info->chan_tx, &xfer->tx_message); | |
434 | if (ret < 0) | |
435 | return ret; | |
436 | ||
437 | ret = 0; | |
438 | ||
439 | /* And we wait for the response. */ | |
440 | timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms); | |
441 | if (!wait_for_completion_timeout(&xfer->done, timeout)) { | |
595f3a9d | 442 | dev_err(dev, "Mbox timedout in resp(caller: %pS)\n", |
aa276781 NM |
443 | (void *)_RET_IP_); |
444 | ret = -ETIMEDOUT; | |
445 | } | |
446 | /* | |
447 | * NOTE: we might prefer not to need the mailbox ticker to manage the | |
448 | * transfer queueing since the protocol layer queues things by itself. | |
449 | * Unfortunately, we have to kick the mailbox framework after we have | |
450 | * received our message. | |
451 | */ | |
452 | mbox_client_txdone(info->chan_tx, ret); | |
453 | ||
454 | return ret; | |
455 | } | |
456 | ||
457 | /** | |
458 | * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity | |
459 | * @info: Pointer to SCI entity information | |
460 | * | |
461 | * Updates the SCI information in the internal data structure. | |
462 | * | |
463 | * Return: 0 if all went fine, else return appropriate error. | |
464 | */ | |
465 | static int ti_sci_cmd_get_revision(struct ti_sci_info *info) | |
466 | { | |
467 | struct device *dev = info->dev; | |
468 | struct ti_sci_handle *handle = &info->handle; | |
469 | struct ti_sci_version_info *ver = &handle->version; | |
470 | struct ti_sci_msg_resp_version *rev_info; | |
471 | struct ti_sci_xfer *xfer; | |
472 | int ret; | |
473 | ||
474 | /* No need to setup flags since it is expected to respond */ | |
475 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION, | |
476 | 0x0, sizeof(struct ti_sci_msg_hdr), | |
477 | sizeof(*rev_info)); | |
478 | if (IS_ERR(xfer)) { | |
479 | ret = PTR_ERR(xfer); | |
480 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
481 | return ret; | |
482 | } | |
483 | ||
484 | rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf; | |
485 | ||
486 | ret = ti_sci_do_xfer(info, xfer); | |
487 | if (ret) { | |
488 | dev_err(dev, "Mbox send fail %d\n", ret); | |
489 | goto fail; | |
490 | } | |
491 | ||
492 | ver->abi_major = rev_info->abi_major; | |
493 | ver->abi_minor = rev_info->abi_minor; | |
494 | ver->firmware_revision = rev_info->firmware_revision; | |
495 | strncpy(ver->firmware_description, rev_info->firmware_description, | |
496 | sizeof(ver->firmware_description)); | |
497 | ||
498 | fail: | |
499 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
500 | return ret; | |
501 | } | |
502 | ||
9e7d756d NM |
503 | /** |
504 | * ti_sci_is_response_ack() - Generic ACK/NACK message checkup | |
505 | * @r: pointer to response buffer | |
506 | * | |
507 | * Return: true if the response was an ACK, else returns false. | |
508 | */ | |
509 | static inline bool ti_sci_is_response_ack(void *r) | |
510 | { | |
511 | struct ti_sci_msg_hdr *hdr = r; | |
512 | ||
513 | return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false; | |
514 | } | |
515 | ||
516 | /** | |
517 | * ti_sci_set_device_state() - Set device state helper | |
518 | * @handle: pointer to TI SCI handle | |
519 | * @id: Device identifier | |
520 | * @flags: flags to setup for the device | |
521 | * @state: State to move the device to | |
522 | * | |
523 | * Return: 0 if all went well, else returns appropriate error value. | |
524 | */ | |
525 | static int ti_sci_set_device_state(const struct ti_sci_handle *handle, | |
526 | u32 id, u32 flags, u8 state) | |
527 | { | |
528 | struct ti_sci_info *info; | |
529 | struct ti_sci_msg_req_set_device_state *req; | |
530 | struct ti_sci_msg_hdr *resp; | |
531 | struct ti_sci_xfer *xfer; | |
532 | struct device *dev; | |
533 | int ret = 0; | |
534 | ||
535 | if (IS_ERR(handle)) | |
536 | return PTR_ERR(handle); | |
537 | if (!handle) | |
538 | return -EINVAL; | |
539 | ||
540 | info = handle_to_ti_sci_info(handle); | |
541 | dev = info->dev; | |
542 | ||
543 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE, | |
544 | flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, | |
545 | sizeof(*req), sizeof(*resp)); | |
546 | if (IS_ERR(xfer)) { | |
547 | ret = PTR_ERR(xfer); | |
548 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
549 | return ret; | |
550 | } | |
551 | req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf; | |
552 | req->id = id; | |
553 | req->state = state; | |
554 | ||
555 | ret = ti_sci_do_xfer(info, xfer); | |
556 | if (ret) { | |
557 | dev_err(dev, "Mbox send fail %d\n", ret); | |
558 | goto fail; | |
559 | } | |
560 | ||
561 | resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf; | |
562 | ||
563 | ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV; | |
564 | ||
565 | fail: | |
566 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
567 | ||
568 | return ret; | |
569 | } | |
570 | ||
571 | /** | |
572 | * ti_sci_get_device_state() - Get device state helper | |
573 | * @handle: Handle to the device | |
574 | * @id: Device Identifier | |
575 | * @clcnt: Pointer to Context Loss Count | |
576 | * @resets: pointer to resets | |
577 | * @p_state: pointer to p_state | |
578 | * @c_state: pointer to c_state | |
579 | * | |
580 | * Return: 0 if all went fine, else return appropriate error. | |
581 | */ | |
582 | static int ti_sci_get_device_state(const struct ti_sci_handle *handle, | |
583 | u32 id, u32 *clcnt, u32 *resets, | |
584 | u8 *p_state, u8 *c_state) | |
585 | { | |
586 | struct ti_sci_info *info; | |
587 | struct ti_sci_msg_req_get_device_state *req; | |
588 | struct ti_sci_msg_resp_get_device_state *resp; | |
589 | struct ti_sci_xfer *xfer; | |
590 | struct device *dev; | |
591 | int ret = 0; | |
592 | ||
593 | if (IS_ERR(handle)) | |
594 | return PTR_ERR(handle); | |
595 | if (!handle) | |
596 | return -EINVAL; | |
597 | ||
598 | if (!clcnt && !resets && !p_state && !c_state) | |
599 | return -EINVAL; | |
600 | ||
601 | info = handle_to_ti_sci_info(handle); | |
602 | dev = info->dev; | |
603 | ||
604 | /* Response is expected, so need of any flags */ | |
605 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE, | |
606 | 0, sizeof(*req), sizeof(*resp)); | |
607 | if (IS_ERR(xfer)) { | |
608 | ret = PTR_ERR(xfer); | |
609 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
610 | return ret; | |
611 | } | |
612 | req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf; | |
613 | req->id = id; | |
614 | ||
615 | ret = ti_sci_do_xfer(info, xfer); | |
616 | if (ret) { | |
617 | dev_err(dev, "Mbox send fail %d\n", ret); | |
618 | goto fail; | |
619 | } | |
620 | ||
621 | resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf; | |
622 | if (!ti_sci_is_response_ack(resp)) { | |
623 | ret = -ENODEV; | |
624 | goto fail; | |
625 | } | |
626 | ||
627 | if (clcnt) | |
628 | *clcnt = resp->context_loss_count; | |
629 | if (resets) | |
630 | *resets = resp->resets; | |
631 | if (p_state) | |
632 | *p_state = resp->programmed_state; | |
633 | if (c_state) | |
634 | *c_state = resp->current_state; | |
635 | fail: | |
636 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
637 | ||
638 | return ret; | |
639 | } | |
640 | ||
641 | /** | |
642 | * ti_sci_cmd_get_device() - command to request for device managed by TISCI | |
643 | * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle | |
644 | * @id: Device Identifier | |
645 | * | |
646 | * Request for the device - NOTE: the client MUST maintain integrity of | |
647 | * usage count by balancing get_device with put_device. No refcounting is | |
648 | * managed by driver for that purpose. | |
649 | * | |
650 | * NOTE: The request is for exclusive access for the processor. | |
651 | * | |
652 | * Return: 0 if all went fine, else return appropriate error. | |
653 | */ | |
654 | static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id) | |
655 | { | |
656 | return ti_sci_set_device_state(handle, id, | |
657 | MSG_FLAG_DEVICE_EXCLUSIVE, | |
658 | MSG_DEVICE_SW_STATE_ON); | |
659 | } | |
660 | ||
661 | /** | |
662 | * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI | |
663 | * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle | |
664 | * @id: Device Identifier | |
665 | * | |
666 | * Request for the device - NOTE: the client MUST maintain integrity of | |
667 | * usage count by balancing get_device with put_device. No refcounting is | |
668 | * managed by driver for that purpose. | |
669 | * | |
670 | * Return: 0 if all went fine, else return appropriate error. | |
671 | */ | |
672 | static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id) | |
673 | { | |
674 | return ti_sci_set_device_state(handle, id, | |
675 | MSG_FLAG_DEVICE_EXCLUSIVE, | |
676 | MSG_DEVICE_SW_STATE_RETENTION); | |
677 | } | |
678 | ||
679 | /** | |
680 | * ti_sci_cmd_put_device() - command to release a device managed by TISCI | |
681 | * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle | |
682 | * @id: Device Identifier | |
683 | * | |
684 | * Request for the device - NOTE: the client MUST maintain integrity of | |
685 | * usage count by balancing get_device with put_device. No refcounting is | |
686 | * managed by driver for that purpose. | |
687 | * | |
688 | * Return: 0 if all went fine, else return appropriate error. | |
689 | */ | |
690 | static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id) | |
691 | { | |
692 | return ti_sci_set_device_state(handle, id, | |
693 | 0, MSG_DEVICE_SW_STATE_AUTO_OFF); | |
694 | } | |
695 | ||
696 | /** | |
697 | * ti_sci_cmd_dev_is_valid() - Is the device valid | |
698 | * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle | |
699 | * @id: Device Identifier | |
700 | * | |
701 | * Return: 0 if all went fine and the device ID is valid, else return | |
702 | * appropriate error. | |
703 | */ | |
704 | static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id) | |
705 | { | |
706 | u8 unused; | |
707 | ||
708 | /* check the device state which will also tell us if the ID is valid */ | |
709 | return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused); | |
710 | } | |
711 | ||
712 | /** | |
713 | * ti_sci_cmd_dev_get_clcnt() - Get context loss counter | |
714 | * @handle: Pointer to TISCI handle | |
715 | * @id: Device Identifier | |
716 | * @count: Pointer to Context Loss counter to populate | |
717 | * | |
718 | * Return: 0 if all went fine, else return appropriate error. | |
719 | */ | |
720 | static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id, | |
721 | u32 *count) | |
722 | { | |
723 | return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL); | |
724 | } | |
725 | ||
726 | /** | |
727 | * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle | |
728 | * @handle: Pointer to TISCI handle | |
729 | * @id: Device Identifier | |
730 | * @r_state: true if requested to be idle | |
731 | * | |
732 | * Return: 0 if all went fine, else return appropriate error. | |
733 | */ | |
734 | static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id, | |
735 | bool *r_state) | |
736 | { | |
737 | int ret; | |
738 | u8 state; | |
739 | ||
740 | if (!r_state) | |
741 | return -EINVAL; | |
742 | ||
743 | ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL); | |
744 | if (ret) | |
745 | return ret; | |
746 | ||
747 | *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION); | |
748 | ||
749 | return 0; | |
750 | } | |
751 | ||
752 | /** | |
753 | * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped | |
754 | * @handle: Pointer to TISCI handle | |
755 | * @id: Device Identifier | |
756 | * @r_state: true if requested to be stopped | |
757 | * @curr_state: true if currently stopped. | |
758 | * | |
759 | * Return: 0 if all went fine, else return appropriate error. | |
760 | */ | |
761 | static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id, | |
762 | bool *r_state, bool *curr_state) | |
763 | { | |
764 | int ret; | |
765 | u8 p_state, c_state; | |
766 | ||
767 | if (!r_state && !curr_state) | |
768 | return -EINVAL; | |
769 | ||
770 | ret = | |
771 | ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state); | |
772 | if (ret) | |
773 | return ret; | |
774 | ||
775 | if (r_state) | |
776 | *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF); | |
777 | if (curr_state) | |
778 | *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF); | |
779 | ||
780 | return 0; | |
781 | } | |
782 | ||
783 | /** | |
784 | * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON | |
785 | * @handle: Pointer to TISCI handle | |
786 | * @id: Device Identifier | |
787 | * @r_state: true if requested to be ON | |
788 | * @curr_state: true if currently ON and active | |
789 | * | |
790 | * Return: 0 if all went fine, else return appropriate error. | |
791 | */ | |
792 | static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id, | |
793 | bool *r_state, bool *curr_state) | |
794 | { | |
795 | int ret; | |
796 | u8 p_state, c_state; | |
797 | ||
798 | if (!r_state && !curr_state) | |
799 | return -EINVAL; | |
800 | ||
801 | ret = | |
802 | ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state); | |
803 | if (ret) | |
804 | return ret; | |
805 | ||
806 | if (r_state) | |
807 | *r_state = (p_state == MSG_DEVICE_SW_STATE_ON); | |
808 | if (curr_state) | |
809 | *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON); | |
810 | ||
811 | return 0; | |
812 | } | |
813 | ||
814 | /** | |
815 | * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning | |
816 | * @handle: Pointer to TISCI handle | |
817 | * @id: Device Identifier | |
818 | * @curr_state: true if currently transitioning. | |
819 | * | |
820 | * Return: 0 if all went fine, else return appropriate error. | |
821 | */ | |
822 | static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id, | |
823 | bool *curr_state) | |
824 | { | |
825 | int ret; | |
826 | u8 state; | |
827 | ||
828 | if (!curr_state) | |
829 | return -EINVAL; | |
830 | ||
831 | ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state); | |
832 | if (ret) | |
833 | return ret; | |
834 | ||
835 | *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS); | |
836 | ||
837 | return 0; | |
838 | } | |
839 | ||
840 | /** | |
841 | * ti_sci_cmd_set_device_resets() - command to set resets for device managed | |
842 | * by TISCI | |
843 | * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle | |
844 | * @id: Device Identifier | |
845 | * @reset_state: Device specific reset bit field | |
846 | * | |
847 | * Return: 0 if all went fine, else return appropriate error. | |
848 | */ | |
849 | static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle, | |
850 | u32 id, u32 reset_state) | |
851 | { | |
852 | struct ti_sci_info *info; | |
853 | struct ti_sci_msg_req_set_device_resets *req; | |
854 | struct ti_sci_msg_hdr *resp; | |
855 | struct ti_sci_xfer *xfer; | |
856 | struct device *dev; | |
857 | int ret = 0; | |
858 | ||
859 | if (IS_ERR(handle)) | |
860 | return PTR_ERR(handle); | |
861 | if (!handle) | |
862 | return -EINVAL; | |
863 | ||
864 | info = handle_to_ti_sci_info(handle); | |
865 | dev = info->dev; | |
866 | ||
867 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS, | |
868 | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, | |
869 | sizeof(*req), sizeof(*resp)); | |
870 | if (IS_ERR(xfer)) { | |
871 | ret = PTR_ERR(xfer); | |
872 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
873 | return ret; | |
874 | } | |
875 | req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf; | |
876 | req->id = id; | |
877 | req->resets = reset_state; | |
878 | ||
879 | ret = ti_sci_do_xfer(info, xfer); | |
880 | if (ret) { | |
881 | dev_err(dev, "Mbox send fail %d\n", ret); | |
882 | goto fail; | |
883 | } | |
884 | ||
885 | resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf; | |
886 | ||
887 | ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV; | |
888 | ||
889 | fail: | |
890 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
891 | ||
892 | return ret; | |
893 | } | |
894 | ||
895 | /** | |
896 | * ti_sci_cmd_get_device_resets() - Get reset state for device managed | |
897 | * by TISCI | |
898 | * @handle: Pointer to TISCI handle | |
899 | * @id: Device Identifier | |
900 | * @reset_state: Pointer to reset state to populate | |
901 | * | |
902 | * Return: 0 if all went fine, else return appropriate error. | |
903 | */ | |
904 | static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle, | |
905 | u32 id, u32 *reset_state) | |
906 | { | |
907 | return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL, | |
908 | NULL); | |
909 | } | |
910 | ||
9f723220 NM |
911 | /** |
912 | * ti_sci_set_clock_state() - Set clock state helper | |
913 | * @handle: pointer to TI SCI handle | |
914 | * @dev_id: Device identifier this request is for | |
915 | * @clk_id: Clock identifier for the device for this request. | |
916 | * Each device has it's own set of clock inputs. This indexes | |
917 | * which clock input to modify. | |
918 | * @flags: Header flags as needed | |
919 | * @state: State to request for the clock. | |
920 | * | |
921 | * Return: 0 if all went well, else returns appropriate error value. | |
922 | */ | |
923 | static int ti_sci_set_clock_state(const struct ti_sci_handle *handle, | |
924 | u32 dev_id, u8 clk_id, | |
925 | u32 flags, u8 state) | |
926 | { | |
927 | struct ti_sci_info *info; | |
928 | struct ti_sci_msg_req_set_clock_state *req; | |
929 | struct ti_sci_msg_hdr *resp; | |
930 | struct ti_sci_xfer *xfer; | |
931 | struct device *dev; | |
932 | int ret = 0; | |
933 | ||
934 | if (IS_ERR(handle)) | |
935 | return PTR_ERR(handle); | |
936 | if (!handle) | |
937 | return -EINVAL; | |
938 | ||
939 | info = handle_to_ti_sci_info(handle); | |
940 | dev = info->dev; | |
941 | ||
942 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE, | |
943 | flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, | |
944 | sizeof(*req), sizeof(*resp)); | |
945 | if (IS_ERR(xfer)) { | |
946 | ret = PTR_ERR(xfer); | |
947 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
948 | return ret; | |
949 | } | |
950 | req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf; | |
951 | req->dev_id = dev_id; | |
952 | req->clk_id = clk_id; | |
953 | req->request_state = state; | |
954 | ||
955 | ret = ti_sci_do_xfer(info, xfer); | |
956 | if (ret) { | |
957 | dev_err(dev, "Mbox send fail %d\n", ret); | |
958 | goto fail; | |
959 | } | |
960 | ||
961 | resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf; | |
962 | ||
963 | ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV; | |
964 | ||
965 | fail: | |
966 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
967 | ||
968 | return ret; | |
969 | } | |
970 | ||
971 | /** | |
972 | * ti_sci_cmd_get_clock_state() - Get clock state helper | |
973 | * @handle: pointer to TI SCI handle | |
974 | * @dev_id: Device identifier this request is for | |
975 | * @clk_id: Clock identifier for the device for this request. | |
976 | * Each device has it's own set of clock inputs. This indexes | |
977 | * which clock input to modify. | |
978 | * @programmed_state: State requested for clock to move to | |
979 | * @current_state: State that the clock is currently in | |
980 | * | |
981 | * Return: 0 if all went well, else returns appropriate error value. | |
982 | */ | |
983 | static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle, | |
984 | u32 dev_id, u8 clk_id, | |
985 | u8 *programmed_state, u8 *current_state) | |
986 | { | |
987 | struct ti_sci_info *info; | |
988 | struct ti_sci_msg_req_get_clock_state *req; | |
989 | struct ti_sci_msg_resp_get_clock_state *resp; | |
990 | struct ti_sci_xfer *xfer; | |
991 | struct device *dev; | |
992 | int ret = 0; | |
993 | ||
994 | if (IS_ERR(handle)) | |
995 | return PTR_ERR(handle); | |
996 | if (!handle) | |
997 | return -EINVAL; | |
998 | ||
999 | if (!programmed_state && !current_state) | |
1000 | return -EINVAL; | |
1001 | ||
1002 | info = handle_to_ti_sci_info(handle); | |
1003 | dev = info->dev; | |
1004 | ||
1005 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE, | |
1006 | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, | |
1007 | sizeof(*req), sizeof(*resp)); | |
1008 | if (IS_ERR(xfer)) { | |
1009 | ret = PTR_ERR(xfer); | |
1010 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
1011 | return ret; | |
1012 | } | |
1013 | req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf; | |
1014 | req->dev_id = dev_id; | |
1015 | req->clk_id = clk_id; | |
1016 | ||
1017 | ret = ti_sci_do_xfer(info, xfer); | |
1018 | if (ret) { | |
1019 | dev_err(dev, "Mbox send fail %d\n", ret); | |
1020 | goto fail; | |
1021 | } | |
1022 | ||
1023 | resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf; | |
1024 | ||
1025 | if (!ti_sci_is_response_ack(resp)) { | |
1026 | ret = -ENODEV; | |
1027 | goto fail; | |
1028 | } | |
1029 | ||
1030 | if (programmed_state) | |
1031 | *programmed_state = resp->programmed_state; | |
1032 | if (current_state) | |
1033 | *current_state = resp->current_state; | |
1034 | ||
1035 | fail: | |
1036 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
1037 | ||
1038 | return ret; | |
1039 | } | |
1040 | ||
1041 | /** | |
1042 | * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI | |
1043 | * @handle: pointer to TI SCI handle | |
1044 | * @dev_id: Device identifier this request is for | |
1045 | * @clk_id: Clock identifier for the device for this request. | |
1046 | * Each device has it's own set of clock inputs. This indexes | |
1047 | * which clock input to modify. | |
1048 | * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false' | |
1049 | * @can_change_freq: 'true' if frequency change is desired, else 'false' | |
1050 | * @enable_input_term: 'true' if input termination is desired, else 'false' | |
1051 | * | |
1052 | * Return: 0 if all went well, else returns appropriate error value. | |
1053 | */ | |
1054 | static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id, | |
1055 | u8 clk_id, bool needs_ssc, bool can_change_freq, | |
1056 | bool enable_input_term) | |
1057 | { | |
1058 | u32 flags = 0; | |
1059 | ||
1060 | flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0; | |
1061 | flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0; | |
1062 | flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0; | |
1063 | ||
1064 | return ti_sci_set_clock_state(handle, dev_id, clk_id, flags, | |
1065 | MSG_CLOCK_SW_STATE_REQ); | |
1066 | } | |
1067 | ||
1068 | /** | |
1069 | * ti_sci_cmd_idle_clock() - Idle a clock which is in our control | |
1070 | * @handle: pointer to TI SCI handle | |
1071 | * @dev_id: Device identifier this request is for | |
1072 | * @clk_id: Clock identifier for the device for this request. | |
1073 | * Each device has it's own set of clock inputs. This indexes | |
1074 | * which clock input to modify. | |
1075 | * | |
1076 | * NOTE: This clock must have been requested by get_clock previously. | |
1077 | * | |
1078 | * Return: 0 if all went well, else returns appropriate error value. | |
1079 | */ | |
1080 | static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle, | |
1081 | u32 dev_id, u8 clk_id) | |
1082 | { | |
1083 | return ti_sci_set_clock_state(handle, dev_id, clk_id, 0, | |
1084 | MSG_CLOCK_SW_STATE_UNREQ); | |
1085 | } | |
1086 | ||
1087 | /** | |
1088 | * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI | |
1089 | * @handle: pointer to TI SCI handle | |
1090 | * @dev_id: Device identifier this request is for | |
1091 | * @clk_id: Clock identifier for the device for this request. | |
1092 | * Each device has it's own set of clock inputs. This indexes | |
1093 | * which clock input to modify. | |
1094 | * | |
1095 | * NOTE: This clock must have been requested by get_clock previously. | |
1096 | * | |
1097 | * Return: 0 if all went well, else returns appropriate error value. | |
1098 | */ | |
1099 | static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle, | |
1100 | u32 dev_id, u8 clk_id) | |
1101 | { | |
1102 | return ti_sci_set_clock_state(handle, dev_id, clk_id, 0, | |
1103 | MSG_CLOCK_SW_STATE_AUTO); | |
1104 | } | |
1105 | ||
1106 | /** | |
1107 | * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed | |
1108 | * @handle: pointer to TI SCI handle | |
1109 | * @dev_id: Device identifier this request is for | |
1110 | * @clk_id: Clock identifier for the device for this request. | |
1111 | * Each device has it's own set of clock inputs. This indexes | |
1112 | * which clock input to modify. | |
1113 | * @req_state: state indicating if the clock is auto managed | |
1114 | * | |
1115 | * Return: 0 if all went well, else returns appropriate error value. | |
1116 | */ | |
1117 | static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle, | |
1118 | u32 dev_id, u8 clk_id, bool *req_state) | |
1119 | { | |
1120 | u8 state = 0; | |
1121 | int ret; | |
1122 | ||
1123 | if (!req_state) | |
1124 | return -EINVAL; | |
1125 | ||
1126 | ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL); | |
1127 | if (ret) | |
1128 | return ret; | |
1129 | ||
1130 | *req_state = (state == MSG_CLOCK_SW_STATE_AUTO); | |
1131 | return 0; | |
1132 | } | |
1133 | ||
1134 | /** | |
1135 | * ti_sci_cmd_clk_is_on() - Is the clock ON | |
1136 | * @handle: pointer to TI SCI handle | |
1137 | * @dev_id: Device identifier this request is for | |
1138 | * @clk_id: Clock identifier for the device for this request. | |
1139 | * Each device has it's own set of clock inputs. This indexes | |
1140 | * which clock input to modify. | |
1141 | * @req_state: state indicating if the clock is managed by us and enabled | |
1142 | * @curr_state: state indicating if the clock is ready for operation | |
1143 | * | |
1144 | * Return: 0 if all went well, else returns appropriate error value. | |
1145 | */ | |
1146 | static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id, | |
1147 | u8 clk_id, bool *req_state, bool *curr_state) | |
1148 | { | |
1149 | u8 c_state = 0, r_state = 0; | |
1150 | int ret; | |
1151 | ||
1152 | if (!req_state && !curr_state) | |
1153 | return -EINVAL; | |
1154 | ||
1155 | ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, | |
1156 | &r_state, &c_state); | |
1157 | if (ret) | |
1158 | return ret; | |
1159 | ||
1160 | if (req_state) | |
1161 | *req_state = (r_state == MSG_CLOCK_SW_STATE_REQ); | |
1162 | if (curr_state) | |
1163 | *curr_state = (c_state == MSG_CLOCK_HW_STATE_READY); | |
1164 | return 0; | |
1165 | } | |
1166 | ||
1167 | /** | |
1168 | * ti_sci_cmd_clk_is_off() - Is the clock OFF | |
1169 | * @handle: pointer to TI SCI handle | |
1170 | * @dev_id: Device identifier this request is for | |
1171 | * @clk_id: Clock identifier for the device for this request. | |
1172 | * Each device has it's own set of clock inputs. This indexes | |
1173 | * which clock input to modify. | |
1174 | * @req_state: state indicating if the clock is managed by us and disabled | |
1175 | * @curr_state: state indicating if the clock is NOT ready for operation | |
1176 | * | |
1177 | * Return: 0 if all went well, else returns appropriate error value. | |
1178 | */ | |
1179 | static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id, | |
1180 | u8 clk_id, bool *req_state, bool *curr_state) | |
1181 | { | |
1182 | u8 c_state = 0, r_state = 0; | |
1183 | int ret; | |
1184 | ||
1185 | if (!req_state && !curr_state) | |
1186 | return -EINVAL; | |
1187 | ||
1188 | ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, | |
1189 | &r_state, &c_state); | |
1190 | if (ret) | |
1191 | return ret; | |
1192 | ||
1193 | if (req_state) | |
1194 | *req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ); | |
1195 | if (curr_state) | |
1196 | *curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY); | |
1197 | return 0; | |
1198 | } | |
1199 | ||
1200 | /** | |
1201 | * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock | |
1202 | * @handle: pointer to TI SCI handle | |
1203 | * @dev_id: Device identifier this request is for | |
1204 | * @clk_id: Clock identifier for the device for this request. | |
1205 | * Each device has it's own set of clock inputs. This indexes | |
1206 | * which clock input to modify. | |
1207 | * @parent_id: Parent clock identifier to set | |
1208 | * | |
1209 | * Return: 0 if all went well, else returns appropriate error value. | |
1210 | */ | |
1211 | static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle, | |
1212 | u32 dev_id, u8 clk_id, u8 parent_id) | |
1213 | { | |
1214 | struct ti_sci_info *info; | |
1215 | struct ti_sci_msg_req_set_clock_parent *req; | |
1216 | struct ti_sci_msg_hdr *resp; | |
1217 | struct ti_sci_xfer *xfer; | |
1218 | struct device *dev; | |
1219 | int ret = 0; | |
1220 | ||
1221 | if (IS_ERR(handle)) | |
1222 | return PTR_ERR(handle); | |
1223 | if (!handle) | |
1224 | return -EINVAL; | |
1225 | ||
1226 | info = handle_to_ti_sci_info(handle); | |
1227 | dev = info->dev; | |
1228 | ||
1229 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT, | |
1230 | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, | |
1231 | sizeof(*req), sizeof(*resp)); | |
1232 | if (IS_ERR(xfer)) { | |
1233 | ret = PTR_ERR(xfer); | |
1234 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
1235 | return ret; | |
1236 | } | |
1237 | req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf; | |
1238 | req->dev_id = dev_id; | |
1239 | req->clk_id = clk_id; | |
1240 | req->parent_id = parent_id; | |
1241 | ||
1242 | ret = ti_sci_do_xfer(info, xfer); | |
1243 | if (ret) { | |
1244 | dev_err(dev, "Mbox send fail %d\n", ret); | |
1245 | goto fail; | |
1246 | } | |
1247 | ||
1248 | resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf; | |
1249 | ||
1250 | ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV; | |
1251 | ||
1252 | fail: | |
1253 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
1254 | ||
1255 | return ret; | |
1256 | } | |
1257 | ||
1258 | /** | |
1259 | * ti_sci_cmd_clk_get_parent() - Get current parent clock source | |
1260 | * @handle: pointer to TI SCI handle | |
1261 | * @dev_id: Device identifier this request is for | |
1262 | * @clk_id: Clock identifier for the device for this request. | |
1263 | * Each device has it's own set of clock inputs. This indexes | |
1264 | * which clock input to modify. | |
1265 | * @parent_id: Current clock parent | |
1266 | * | |
1267 | * Return: 0 if all went well, else returns appropriate error value. | |
1268 | */ | |
1269 | static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle, | |
1270 | u32 dev_id, u8 clk_id, u8 *parent_id) | |
1271 | { | |
1272 | struct ti_sci_info *info; | |
1273 | struct ti_sci_msg_req_get_clock_parent *req; | |
1274 | struct ti_sci_msg_resp_get_clock_parent *resp; | |
1275 | struct ti_sci_xfer *xfer; | |
1276 | struct device *dev; | |
1277 | int ret = 0; | |
1278 | ||
1279 | if (IS_ERR(handle)) | |
1280 | return PTR_ERR(handle); | |
1281 | if (!handle || !parent_id) | |
1282 | return -EINVAL; | |
1283 | ||
1284 | info = handle_to_ti_sci_info(handle); | |
1285 | dev = info->dev; | |
1286 | ||
1287 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT, | |
1288 | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, | |
1289 | sizeof(*req), sizeof(*resp)); | |
1290 | if (IS_ERR(xfer)) { | |
1291 | ret = PTR_ERR(xfer); | |
1292 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
1293 | return ret; | |
1294 | } | |
1295 | req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf; | |
1296 | req->dev_id = dev_id; | |
1297 | req->clk_id = clk_id; | |
1298 | ||
1299 | ret = ti_sci_do_xfer(info, xfer); | |
1300 | if (ret) { | |
1301 | dev_err(dev, "Mbox send fail %d\n", ret); | |
1302 | goto fail; | |
1303 | } | |
1304 | ||
1305 | resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf; | |
1306 | ||
1307 | if (!ti_sci_is_response_ack(resp)) | |
1308 | ret = -ENODEV; | |
1309 | else | |
1310 | *parent_id = resp->parent_id; | |
1311 | ||
1312 | fail: | |
1313 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
1314 | ||
1315 | return ret; | |
1316 | } | |
1317 | ||
1318 | /** | |
1319 | * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source | |
1320 | * @handle: pointer to TI SCI handle | |
1321 | * @dev_id: Device identifier this request is for | |
1322 | * @clk_id: Clock identifier for the device for this request. | |
1323 | * Each device has it's own set of clock inputs. This indexes | |
1324 | * which clock input to modify. | |
1325 | * @num_parents: Returns he number of parents to the current clock. | |
1326 | * | |
1327 | * Return: 0 if all went well, else returns appropriate error value. | |
1328 | */ | |
1329 | static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle, | |
1330 | u32 dev_id, u8 clk_id, | |
1331 | u8 *num_parents) | |
1332 | { | |
1333 | struct ti_sci_info *info; | |
1334 | struct ti_sci_msg_req_get_clock_num_parents *req; | |
1335 | struct ti_sci_msg_resp_get_clock_num_parents *resp; | |
1336 | struct ti_sci_xfer *xfer; | |
1337 | struct device *dev; | |
1338 | int ret = 0; | |
1339 | ||
1340 | if (IS_ERR(handle)) | |
1341 | return PTR_ERR(handle); | |
1342 | if (!handle || !num_parents) | |
1343 | return -EINVAL; | |
1344 | ||
1345 | info = handle_to_ti_sci_info(handle); | |
1346 | dev = info->dev; | |
1347 | ||
1348 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS, | |
1349 | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, | |
1350 | sizeof(*req), sizeof(*resp)); | |
1351 | if (IS_ERR(xfer)) { | |
1352 | ret = PTR_ERR(xfer); | |
1353 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
1354 | return ret; | |
1355 | } | |
1356 | req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf; | |
1357 | req->dev_id = dev_id; | |
1358 | req->clk_id = clk_id; | |
1359 | ||
1360 | ret = ti_sci_do_xfer(info, xfer); | |
1361 | if (ret) { | |
1362 | dev_err(dev, "Mbox send fail %d\n", ret); | |
1363 | goto fail; | |
1364 | } | |
1365 | ||
1366 | resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf; | |
1367 | ||
1368 | if (!ti_sci_is_response_ack(resp)) | |
1369 | ret = -ENODEV; | |
1370 | else | |
1371 | *num_parents = resp->num_parents; | |
1372 | ||
1373 | fail: | |
1374 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
1375 | ||
1376 | return ret; | |
1377 | } | |
1378 | ||
1379 | /** | |
1380 | * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency | |
1381 | * @handle: pointer to TI SCI handle | |
1382 | * @dev_id: Device identifier this request is for | |
1383 | * @clk_id: Clock identifier for the device for this request. | |
1384 | * Each device has it's own set of clock inputs. This indexes | |
1385 | * which clock input to modify. | |
1386 | * @min_freq: The minimum allowable frequency in Hz. This is the minimum | |
1387 | * allowable programmed frequency and does not account for clock | |
1388 | * tolerances and jitter. | |
1389 | * @target_freq: The target clock frequency in Hz. A frequency will be | |
1390 | * processed as close to this target frequency as possible. | |
1391 | * @max_freq: The maximum allowable frequency in Hz. This is the maximum | |
1392 | * allowable programmed frequency and does not account for clock | |
1393 | * tolerances and jitter. | |
1394 | * @match_freq: Frequency match in Hz response. | |
1395 | * | |
1396 | * Return: 0 if all went well, else returns appropriate error value. | |
1397 | */ | |
1398 | static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle, | |
1399 | u32 dev_id, u8 clk_id, u64 min_freq, | |
1400 | u64 target_freq, u64 max_freq, | |
1401 | u64 *match_freq) | |
1402 | { | |
1403 | struct ti_sci_info *info; | |
1404 | struct ti_sci_msg_req_query_clock_freq *req; | |
1405 | struct ti_sci_msg_resp_query_clock_freq *resp; | |
1406 | struct ti_sci_xfer *xfer; | |
1407 | struct device *dev; | |
1408 | int ret = 0; | |
1409 | ||
1410 | if (IS_ERR(handle)) | |
1411 | return PTR_ERR(handle); | |
1412 | if (!handle || !match_freq) | |
1413 | return -EINVAL; | |
1414 | ||
1415 | info = handle_to_ti_sci_info(handle); | |
1416 | dev = info->dev; | |
1417 | ||
1418 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ, | |
1419 | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, | |
1420 | sizeof(*req), sizeof(*resp)); | |
1421 | if (IS_ERR(xfer)) { | |
1422 | ret = PTR_ERR(xfer); | |
1423 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
1424 | return ret; | |
1425 | } | |
1426 | req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf; | |
1427 | req->dev_id = dev_id; | |
1428 | req->clk_id = clk_id; | |
1429 | req->min_freq_hz = min_freq; | |
1430 | req->target_freq_hz = target_freq; | |
1431 | req->max_freq_hz = max_freq; | |
1432 | ||
1433 | ret = ti_sci_do_xfer(info, xfer); | |
1434 | if (ret) { | |
1435 | dev_err(dev, "Mbox send fail %d\n", ret); | |
1436 | goto fail; | |
1437 | } | |
1438 | ||
1439 | resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf; | |
1440 | ||
1441 | if (!ti_sci_is_response_ack(resp)) | |
1442 | ret = -ENODEV; | |
1443 | else | |
1444 | *match_freq = resp->freq_hz; | |
1445 | ||
1446 | fail: | |
1447 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
1448 | ||
1449 | return ret; | |
1450 | } | |
1451 | ||
1452 | /** | |
1453 | * ti_sci_cmd_clk_set_freq() - Set a frequency for clock | |
1454 | * @handle: pointer to TI SCI handle | |
1455 | * @dev_id: Device identifier this request is for | |
1456 | * @clk_id: Clock identifier for the device for this request. | |
1457 | * Each device has it's own set of clock inputs. This indexes | |
1458 | * which clock input to modify. | |
1459 | * @min_freq: The minimum allowable frequency in Hz. This is the minimum | |
1460 | * allowable programmed frequency and does not account for clock | |
1461 | * tolerances and jitter. | |
1462 | * @target_freq: The target clock frequency in Hz. A frequency will be | |
1463 | * processed as close to this target frequency as possible. | |
1464 | * @max_freq: The maximum allowable frequency in Hz. This is the maximum | |
1465 | * allowable programmed frequency and does not account for clock | |
1466 | * tolerances and jitter. | |
1467 | * | |
1468 | * Return: 0 if all went well, else returns appropriate error value. | |
1469 | */ | |
1470 | static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle, | |
1471 | u32 dev_id, u8 clk_id, u64 min_freq, | |
1472 | u64 target_freq, u64 max_freq) | |
1473 | { | |
1474 | struct ti_sci_info *info; | |
1475 | struct ti_sci_msg_req_set_clock_freq *req; | |
1476 | struct ti_sci_msg_hdr *resp; | |
1477 | struct ti_sci_xfer *xfer; | |
1478 | struct device *dev; | |
1479 | int ret = 0; | |
1480 | ||
1481 | if (IS_ERR(handle)) | |
1482 | return PTR_ERR(handle); | |
1483 | if (!handle) | |
1484 | return -EINVAL; | |
1485 | ||
1486 | info = handle_to_ti_sci_info(handle); | |
1487 | dev = info->dev; | |
1488 | ||
1489 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ, | |
1490 | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, | |
1491 | sizeof(*req), sizeof(*resp)); | |
1492 | if (IS_ERR(xfer)) { | |
1493 | ret = PTR_ERR(xfer); | |
1494 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
1495 | return ret; | |
1496 | } | |
1497 | req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf; | |
1498 | req->dev_id = dev_id; | |
1499 | req->clk_id = clk_id; | |
1500 | req->min_freq_hz = min_freq; | |
1501 | req->target_freq_hz = target_freq; | |
1502 | req->max_freq_hz = max_freq; | |
1503 | ||
1504 | ret = ti_sci_do_xfer(info, xfer); | |
1505 | if (ret) { | |
1506 | dev_err(dev, "Mbox send fail %d\n", ret); | |
1507 | goto fail; | |
1508 | } | |
1509 | ||
1510 | resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf; | |
1511 | ||
1512 | ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV; | |
1513 | ||
1514 | fail: | |
1515 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
1516 | ||
1517 | return ret; | |
1518 | } | |
1519 | ||
1520 | /** | |
1521 | * ti_sci_cmd_clk_get_freq() - Get current frequency | |
1522 | * @handle: pointer to TI SCI handle | |
1523 | * @dev_id: Device identifier this request is for | |
1524 | * @clk_id: Clock identifier for the device for this request. | |
1525 | * Each device has it's own set of clock inputs. This indexes | |
1526 | * which clock input to modify. | |
1527 | * @freq: Currently frequency in Hz | |
1528 | * | |
1529 | * Return: 0 if all went well, else returns appropriate error value. | |
1530 | */ | |
1531 | static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle, | |
1532 | u32 dev_id, u8 clk_id, u64 *freq) | |
1533 | { | |
1534 | struct ti_sci_info *info; | |
1535 | struct ti_sci_msg_req_get_clock_freq *req; | |
1536 | struct ti_sci_msg_resp_get_clock_freq *resp; | |
1537 | struct ti_sci_xfer *xfer; | |
1538 | struct device *dev; | |
1539 | int ret = 0; | |
1540 | ||
1541 | if (IS_ERR(handle)) | |
1542 | return PTR_ERR(handle); | |
1543 | if (!handle || !freq) | |
1544 | return -EINVAL; | |
1545 | ||
1546 | info = handle_to_ti_sci_info(handle); | |
1547 | dev = info->dev; | |
1548 | ||
1549 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ, | |
1550 | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, | |
1551 | sizeof(*req), sizeof(*resp)); | |
1552 | if (IS_ERR(xfer)) { | |
1553 | ret = PTR_ERR(xfer); | |
1554 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
1555 | return ret; | |
1556 | } | |
1557 | req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf; | |
1558 | req->dev_id = dev_id; | |
1559 | req->clk_id = clk_id; | |
1560 | ||
1561 | ret = ti_sci_do_xfer(info, xfer); | |
1562 | if (ret) { | |
1563 | dev_err(dev, "Mbox send fail %d\n", ret); | |
1564 | goto fail; | |
1565 | } | |
1566 | ||
1567 | resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf; | |
1568 | ||
1569 | if (!ti_sci_is_response_ack(resp)) | |
1570 | ret = -ENODEV; | |
1571 | else | |
1572 | *freq = resp->freq_hz; | |
1573 | ||
1574 | fail: | |
1575 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
1576 | ||
1577 | return ret; | |
1578 | } | |
1579 | ||
912cffb4 NM |
1580 | static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle) |
1581 | { | |
1582 | struct ti_sci_info *info; | |
1583 | struct ti_sci_msg_req_reboot *req; | |
1584 | struct ti_sci_msg_hdr *resp; | |
1585 | struct ti_sci_xfer *xfer; | |
1586 | struct device *dev; | |
1587 | int ret = 0; | |
1588 | ||
1589 | if (IS_ERR(handle)) | |
1590 | return PTR_ERR(handle); | |
1591 | if (!handle) | |
1592 | return -EINVAL; | |
1593 | ||
1594 | info = handle_to_ti_sci_info(handle); | |
1595 | dev = info->dev; | |
1596 | ||
1597 | xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET, | |
1598 | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, | |
1599 | sizeof(*req), sizeof(*resp)); | |
1600 | if (IS_ERR(xfer)) { | |
1601 | ret = PTR_ERR(xfer); | |
1602 | dev_err(dev, "Message alloc failed(%d)\n", ret); | |
1603 | return ret; | |
1604 | } | |
1605 | req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf; | |
1606 | ||
1607 | ret = ti_sci_do_xfer(info, xfer); | |
1608 | if (ret) { | |
1609 | dev_err(dev, "Mbox send fail %d\n", ret); | |
1610 | goto fail; | |
1611 | } | |
1612 | ||
1613 | resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf; | |
1614 | ||
1615 | if (!ti_sci_is_response_ack(resp)) | |
1616 | ret = -ENODEV; | |
1617 | else | |
1618 | ret = 0; | |
1619 | ||
1620 | fail: | |
1621 | ti_sci_put_one_xfer(&info->minfo, xfer); | |
1622 | ||
1623 | return ret; | |
1624 | } | |
1625 | ||
9e7d756d NM |
1626 | /* |
1627 | * ti_sci_setup_ops() - Setup the operations structures | |
1628 | * @info: pointer to TISCI pointer | |
1629 | */ | |
1630 | static void ti_sci_setup_ops(struct ti_sci_info *info) | |
1631 | { | |
1632 | struct ti_sci_ops *ops = &info->handle.ops; | |
912cffb4 | 1633 | struct ti_sci_core_ops *core_ops = &ops->core_ops; |
9e7d756d | 1634 | struct ti_sci_dev_ops *dops = &ops->dev_ops; |
9f723220 | 1635 | struct ti_sci_clk_ops *cops = &ops->clk_ops; |
9e7d756d | 1636 | |
912cffb4 NM |
1637 | core_ops->reboot_device = ti_sci_cmd_core_reboot; |
1638 | ||
9e7d756d NM |
1639 | dops->get_device = ti_sci_cmd_get_device; |
1640 | dops->idle_device = ti_sci_cmd_idle_device; | |
1641 | dops->put_device = ti_sci_cmd_put_device; | |
1642 | ||
1643 | dops->is_valid = ti_sci_cmd_dev_is_valid; | |
1644 | dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt; | |
1645 | dops->is_idle = ti_sci_cmd_dev_is_idle; | |
1646 | dops->is_stop = ti_sci_cmd_dev_is_stop; | |
1647 | dops->is_on = ti_sci_cmd_dev_is_on; | |
1648 | dops->is_transitioning = ti_sci_cmd_dev_is_trans; | |
1649 | dops->set_device_resets = ti_sci_cmd_set_device_resets; | |
1650 | dops->get_device_resets = ti_sci_cmd_get_device_resets; | |
9f723220 NM |
1651 | |
1652 | cops->get_clock = ti_sci_cmd_get_clock; | |
1653 | cops->idle_clock = ti_sci_cmd_idle_clock; | |
1654 | cops->put_clock = ti_sci_cmd_put_clock; | |
1655 | cops->is_auto = ti_sci_cmd_clk_is_auto; | |
1656 | cops->is_on = ti_sci_cmd_clk_is_on; | |
1657 | cops->is_off = ti_sci_cmd_clk_is_off; | |
1658 | ||
1659 | cops->set_parent = ti_sci_cmd_clk_set_parent; | |
1660 | cops->get_parent = ti_sci_cmd_clk_get_parent; | |
1661 | cops->get_num_parents = ti_sci_cmd_clk_get_num_parents; | |
1662 | ||
1663 | cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq; | |
1664 | cops->set_freq = ti_sci_cmd_clk_set_freq; | |
1665 | cops->get_freq = ti_sci_cmd_clk_get_freq; | |
9e7d756d NM |
1666 | } |
1667 | ||
aa276781 NM |
1668 | /** |
1669 | * ti_sci_get_handle() - Get the TI SCI handle for a device | |
1670 | * @dev: Pointer to device for which we want SCI handle | |
1671 | * | |
1672 | * NOTE: The function does not track individual clients of the framework | |
1673 | * and is expected to be maintained by caller of TI SCI protocol library. | |
1674 | * ti_sci_put_handle must be balanced with successful ti_sci_get_handle | |
1675 | * Return: pointer to handle if successful, else: | |
1676 | * -EPROBE_DEFER if the instance is not ready | |
1677 | * -ENODEV if the required node handler is missing | |
1678 | * -EINVAL if invalid conditions are encountered. | |
1679 | */ | |
1680 | const struct ti_sci_handle *ti_sci_get_handle(struct device *dev) | |
1681 | { | |
1682 | struct device_node *ti_sci_np; | |
1683 | struct list_head *p; | |
1684 | struct ti_sci_handle *handle = NULL; | |
1685 | struct ti_sci_info *info; | |
1686 | ||
1687 | if (!dev) { | |
1688 | pr_err("I need a device pointer\n"); | |
1689 | return ERR_PTR(-EINVAL); | |
1690 | } | |
1691 | ti_sci_np = of_get_parent(dev->of_node); | |
1692 | if (!ti_sci_np) { | |
1693 | dev_err(dev, "No OF information\n"); | |
1694 | return ERR_PTR(-EINVAL); | |
1695 | } | |
1696 | ||
1697 | mutex_lock(&ti_sci_list_mutex); | |
1698 | list_for_each(p, &ti_sci_list) { | |
1699 | info = list_entry(p, struct ti_sci_info, node); | |
1700 | if (ti_sci_np == info->dev->of_node) { | |
1701 | handle = &info->handle; | |
1702 | info->users++; | |
1703 | break; | |
1704 | } | |
1705 | } | |
1706 | mutex_unlock(&ti_sci_list_mutex); | |
1707 | of_node_put(ti_sci_np); | |
1708 | ||
1709 | if (!handle) | |
1710 | return ERR_PTR(-EPROBE_DEFER); | |
1711 | ||
1712 | return handle; | |
1713 | } | |
1714 | EXPORT_SYMBOL_GPL(ti_sci_get_handle); | |
1715 | ||
1716 | /** | |
1717 | * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle | |
1718 | * @handle: Handle acquired by ti_sci_get_handle | |
1719 | * | |
1720 | * NOTE: The function does not track individual clients of the framework | |
1721 | * and is expected to be maintained by caller of TI SCI protocol library. | |
1722 | * ti_sci_put_handle must be balanced with successful ti_sci_get_handle | |
1723 | * | |
1724 | * Return: 0 is successfully released | |
1725 | * if an error pointer was passed, it returns the error value back, | |
1726 | * if null was passed, it returns -EINVAL; | |
1727 | */ | |
1728 | int ti_sci_put_handle(const struct ti_sci_handle *handle) | |
1729 | { | |
1730 | struct ti_sci_info *info; | |
1731 | ||
1732 | if (IS_ERR(handle)) | |
1733 | return PTR_ERR(handle); | |
1734 | if (!handle) | |
1735 | return -EINVAL; | |
1736 | ||
1737 | info = handle_to_ti_sci_info(handle); | |
1738 | mutex_lock(&ti_sci_list_mutex); | |
1739 | if (!WARN_ON(!info->users)) | |
1740 | info->users--; | |
1741 | mutex_unlock(&ti_sci_list_mutex); | |
1742 | ||
1743 | return 0; | |
1744 | } | |
1745 | EXPORT_SYMBOL_GPL(ti_sci_put_handle); | |
1746 | ||
1747 | static void devm_ti_sci_release(struct device *dev, void *res) | |
1748 | { | |
1749 | const struct ti_sci_handle **ptr = res; | |
1750 | const struct ti_sci_handle *handle = *ptr; | |
1751 | int ret; | |
1752 | ||
1753 | ret = ti_sci_put_handle(handle); | |
1754 | if (ret) | |
1755 | dev_err(dev, "failed to put handle %d\n", ret); | |
1756 | } | |
1757 | ||
1758 | /** | |
1759 | * devm_ti_sci_get_handle() - Managed get handle | |
1760 | * @dev: device for which we want SCI handle for. | |
1761 | * | |
1762 | * NOTE: This releases the handle once the device resources are | |
1763 | * no longer needed. MUST NOT BE released with ti_sci_put_handle. | |
1764 | * The function does not track individual clients of the framework | |
1765 | * and is expected to be maintained by caller of TI SCI protocol library. | |
1766 | * | |
1767 | * Return: 0 if all went fine, else corresponding error. | |
1768 | */ | |
1769 | const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev) | |
1770 | { | |
1771 | const struct ti_sci_handle **ptr; | |
1772 | const struct ti_sci_handle *handle; | |
1773 | ||
1774 | ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL); | |
1775 | if (!ptr) | |
1776 | return ERR_PTR(-ENOMEM); | |
1777 | handle = ti_sci_get_handle(dev); | |
1778 | ||
1779 | if (!IS_ERR(handle)) { | |
1780 | *ptr = handle; | |
1781 | devres_add(dev, ptr); | |
1782 | } else { | |
1783 | devres_free(ptr); | |
1784 | } | |
1785 | ||
1786 | return handle; | |
1787 | } | |
1788 | EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle); | |
1789 | ||
912cffb4 NM |
1790 | static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode, |
1791 | void *cmd) | |
1792 | { | |
1793 | struct ti_sci_info *info = reboot_to_ti_sci_info(nb); | |
1794 | const struct ti_sci_handle *handle = &info->handle; | |
1795 | ||
1796 | ti_sci_cmd_core_reboot(handle); | |
1797 | ||
1798 | /* call fail OR pass, we should not be here in the first place */ | |
1799 | return NOTIFY_BAD; | |
1800 | } | |
1801 | ||
aa276781 NM |
1802 | /* Description for K2G */ |
1803 | static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = { | |
1804 | .host_id = 2, | |
1805 | /* Conservative duration */ | |
1806 | .max_rx_timeout_ms = 1000, | |
1807 | /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */ | |
1808 | .max_msgs = 20, | |
1809 | .max_msg_size = 64, | |
1810 | }; | |
1811 | ||
1812 | static const struct of_device_id ti_sci_of_match[] = { | |
1813 | {.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc}, | |
1814 | { /* Sentinel */ }, | |
1815 | }; | |
1816 | MODULE_DEVICE_TABLE(of, ti_sci_of_match); | |
1817 | ||
1818 | static int ti_sci_probe(struct platform_device *pdev) | |
1819 | { | |
1820 | struct device *dev = &pdev->dev; | |
1821 | const struct of_device_id *of_id; | |
1822 | const struct ti_sci_desc *desc; | |
1823 | struct ti_sci_xfer *xfer; | |
1824 | struct ti_sci_info *info = NULL; | |
1825 | struct ti_sci_xfers_info *minfo; | |
1826 | struct mbox_client *cl; | |
1827 | int ret = -EINVAL; | |
1828 | int i; | |
912cffb4 | 1829 | int reboot = 0; |
aa276781 NM |
1830 | |
1831 | of_id = of_match_device(ti_sci_of_match, dev); | |
1832 | if (!of_id) { | |
1833 | dev_err(dev, "OF data missing\n"); | |
1834 | return -EINVAL; | |
1835 | } | |
1836 | desc = of_id->data; | |
1837 | ||
1838 | info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); | |
1839 | if (!info) | |
1840 | return -ENOMEM; | |
1841 | ||
1842 | info->dev = dev; | |
1843 | info->desc = desc; | |
912cffb4 NM |
1844 | reboot = of_property_read_bool(dev->of_node, |
1845 | "ti,system-reboot-controller"); | |
aa276781 NM |
1846 | INIT_LIST_HEAD(&info->node); |
1847 | minfo = &info->minfo; | |
1848 | ||
1849 | /* | |
1850 | * Pre-allocate messages | |
1851 | * NEVER allocate more than what we can indicate in hdr.seq | |
1852 | * if we have data description bug, force a fix.. | |
1853 | */ | |
1854 | if (WARN_ON(desc->max_msgs >= | |
1855 | 1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq))) | |
1856 | return -EINVAL; | |
1857 | ||
1858 | minfo->xfer_block = devm_kcalloc(dev, | |
1859 | desc->max_msgs, | |
1860 | sizeof(*minfo->xfer_block), | |
1861 | GFP_KERNEL); | |
1862 | if (!minfo->xfer_block) | |
1863 | return -ENOMEM; | |
1864 | ||
1865 | minfo->xfer_alloc_table = devm_kzalloc(dev, | |
1866 | BITS_TO_LONGS(desc->max_msgs) | |
1867 | * sizeof(unsigned long), | |
1868 | GFP_KERNEL); | |
1869 | if (!minfo->xfer_alloc_table) | |
1870 | return -ENOMEM; | |
1871 | bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs); | |
1872 | ||
1873 | /* Pre-initialize the buffer pointer to pre-allocated buffers */ | |
1874 | for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) { | |
1875 | xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size, | |
1876 | GFP_KERNEL); | |
1877 | if (!xfer->xfer_buf) | |
1878 | return -ENOMEM; | |
1879 | ||
1880 | xfer->tx_message.buf = xfer->xfer_buf; | |
1881 | init_completion(&xfer->done); | |
1882 | } | |
1883 | ||
1884 | ret = ti_sci_debugfs_create(pdev, info); | |
1885 | if (ret) | |
1886 | dev_warn(dev, "Failed to create debug file\n"); | |
1887 | ||
1888 | platform_set_drvdata(pdev, info); | |
1889 | ||
1890 | cl = &info->cl; | |
1891 | cl->dev = dev; | |
1892 | cl->tx_block = false; | |
1893 | cl->rx_callback = ti_sci_rx_callback; | |
1894 | cl->knows_txdone = true; | |
1895 | ||
1896 | spin_lock_init(&minfo->xfer_lock); | |
1897 | sema_init(&minfo->sem_xfer_count, desc->max_msgs); | |
1898 | ||
1899 | info->chan_rx = mbox_request_channel_byname(cl, "rx"); | |
1900 | if (IS_ERR(info->chan_rx)) { | |
1901 | ret = PTR_ERR(info->chan_rx); | |
1902 | goto out; | |
1903 | } | |
1904 | ||
1905 | info->chan_tx = mbox_request_channel_byname(cl, "tx"); | |
1906 | if (IS_ERR(info->chan_tx)) { | |
1907 | ret = PTR_ERR(info->chan_tx); | |
1908 | goto out; | |
1909 | } | |
1910 | ret = ti_sci_cmd_get_revision(info); | |
1911 | if (ret) { | |
1912 | dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret); | |
1913 | goto out; | |
1914 | } | |
1915 | ||
9e7d756d NM |
1916 | ti_sci_setup_ops(info); |
1917 | ||
912cffb4 NM |
1918 | if (reboot) { |
1919 | info->nb.notifier_call = tisci_reboot_handler; | |
1920 | info->nb.priority = 128; | |
1921 | ||
1922 | ret = register_restart_handler(&info->nb); | |
1923 | if (ret) { | |
1924 | dev_err(dev, "reboot registration fail(%d)\n", ret); | |
1925 | return ret; | |
1926 | } | |
1927 | } | |
1928 | ||
aa276781 NM |
1929 | dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n", |
1930 | info->handle.version.abi_major, info->handle.version.abi_minor, | |
1931 | info->handle.version.firmware_revision, | |
1932 | info->handle.version.firmware_description); | |
1933 | ||
1934 | mutex_lock(&ti_sci_list_mutex); | |
1935 | list_add_tail(&info->node, &ti_sci_list); | |
1936 | mutex_unlock(&ti_sci_list_mutex); | |
1937 | ||
1938 | return of_platform_populate(dev->of_node, NULL, NULL, dev); | |
1939 | out: | |
1940 | if (!IS_ERR(info->chan_tx)) | |
1941 | mbox_free_channel(info->chan_tx); | |
1942 | if (!IS_ERR(info->chan_rx)) | |
1943 | mbox_free_channel(info->chan_rx); | |
1944 | debugfs_remove(info->d); | |
1945 | return ret; | |
1946 | } | |
1947 | ||
1948 | static int ti_sci_remove(struct platform_device *pdev) | |
1949 | { | |
1950 | struct ti_sci_info *info; | |
1951 | struct device *dev = &pdev->dev; | |
1952 | int ret = 0; | |
1953 | ||
1954 | of_platform_depopulate(dev); | |
1955 | ||
1956 | info = platform_get_drvdata(pdev); | |
1957 | ||
912cffb4 NM |
1958 | if (info->nb.notifier_call) |
1959 | unregister_restart_handler(&info->nb); | |
1960 | ||
aa276781 NM |
1961 | mutex_lock(&ti_sci_list_mutex); |
1962 | if (info->users) | |
1963 | ret = -EBUSY; | |
1964 | else | |
1965 | list_del(&info->node); | |
1966 | mutex_unlock(&ti_sci_list_mutex); | |
1967 | ||
1968 | if (!ret) { | |
1969 | ti_sci_debugfs_destroy(pdev, info); | |
1970 | ||
1971 | /* Safe to free channels since no more users */ | |
1972 | mbox_free_channel(info->chan_tx); | |
1973 | mbox_free_channel(info->chan_rx); | |
1974 | } | |
1975 | ||
1976 | return ret; | |
1977 | } | |
1978 | ||
1979 | static struct platform_driver ti_sci_driver = { | |
1980 | .probe = ti_sci_probe, | |
1981 | .remove = ti_sci_remove, | |
1982 | .driver = { | |
1983 | .name = "ti-sci", | |
1984 | .of_match_table = of_match_ptr(ti_sci_of_match), | |
1985 | }, | |
1986 | }; | |
1987 | module_platform_driver(ti_sci_driver); | |
1988 | ||
1989 | MODULE_LICENSE("GPL v2"); | |
1990 | MODULE_DESCRIPTION("TI System Control Interface(SCI) driver"); | |
1991 | MODULE_AUTHOR("Nishanth Menon"); | |
1992 | MODULE_ALIAS("platform:ti-sci"); |