1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2018 MediaTek Inc.
4  *
5  */
6 
7 #ifndef __MTK_CMDQ_H__
8 #define __MTK_CMDQ_H__
9 
10 #include <linux/mailbox_client.h>
11 #include <linux/mailbox/mtk-cmdq-mailbox.h>
12 #include <linux/timer.h>
13 
14 #define CMDQ_NO_TIMEOUT		0xffffffffu
15 
16 struct cmdq_pkt;
17 
18 struct cmdq_client_reg {
19 	u8 subsys;
20 	u16 offset;
21 	u16 size;
22 };
23 
24 struct cmdq_client {
25 	spinlock_t lock;
26 	u32 pkt_cnt;
27 	struct mbox_client client;
28 	struct mbox_chan *chan;
29 	struct timer_list timer;
30 	u32 timeout_ms; /* in unit of microsecond */
31 };
32 
33 /**
34  * cmdq_dev_get_client_reg() - parse cmdq client reg from the device
35  *			       node of CMDQ client
36  * @dev:	device of CMDQ mailbox client
37  * @client_reg: CMDQ client reg pointer
38  * @idx:	the index of desired reg
39  *
40  * Return: 0 for success; else the error code is returned
41  *
42  * Help CMDQ client parsing the cmdq client reg
43  * from the device node of CMDQ client.
44  */
45 int cmdq_dev_get_client_reg(struct device *dev,
46 			    struct cmdq_client_reg *client_reg, int idx);
47 
48 /**
49  * cmdq_mbox_create() - create CMDQ mailbox client and channel
50  * @dev:	device of CMDQ mailbox client
51  * @index:	index of CMDQ mailbox channel
52  * @timeout:	timeout of a pkt execution by GCE, in unit of microsecond, set
53  *		CMDQ_NO_TIMEOUT if a timer is not used.
54  *
55  * Return: CMDQ mailbox client pointer
56  */
57 struct cmdq_client *cmdq_mbox_create(struct device *dev, int index,
58 				     u32 timeout);
59 
60 /**
61  * cmdq_mbox_destroy() - destroy CMDQ mailbox client and channel
62  * @client:	the CMDQ mailbox client
63  */
64 void cmdq_mbox_destroy(struct cmdq_client *client);
65 
66 /**
67  * cmdq_pkt_create() - create a CMDQ packet
68  * @client:	the CMDQ mailbox client
69  * @size:	required CMDQ buffer size
70  *
71  * Return: CMDQ packet pointer
72  */
73 struct cmdq_pkt *cmdq_pkt_create(struct cmdq_client *client, size_t size);
74 
75 /**
76  * cmdq_pkt_destroy() - destroy the CMDQ packet
77  * @pkt:	the CMDQ packet
78  */
79 void cmdq_pkt_destroy(struct cmdq_pkt *pkt);
80 
81 /**
82  * cmdq_pkt_write() - append write command to the CMDQ packet
83  * @pkt:	the CMDQ packet
84  * @subsys:	the CMDQ sub system code
85  * @offset:	register offset from CMDQ sub system
86  * @value:	the specified target register value
87  *
88  * Return: 0 for success; else the error code is returned
89  */
90 int cmdq_pkt_write(struct cmdq_pkt *pkt, u8 subsys, u16 offset, u32 value);
91 
92 /**
93  * cmdq_pkt_write_mask() - append write command with mask to the CMDQ packet
94  * @pkt:	the CMDQ packet
95  * @subsys:	the CMDQ sub system code
96  * @offset:	register offset from CMDQ sub system
97  * @value:	the specified target register value
98  * @mask:	the specified target register mask
99  *
100  * Return: 0 for success; else the error code is returned
101  */
102 int cmdq_pkt_write_mask(struct cmdq_pkt *pkt, u8 subsys,
103 			u16 offset, u32 value, u32 mask);
104 
105 /**
106  * cmdq_pkt_wfe() - append wait for event command to the CMDQ packet
107  * @pkt:	the CMDQ packet
108  * @event:	the desired event type to "wait and CLEAR"
109  *
110  * Return: 0 for success; else the error code is returned
111  */
112 int cmdq_pkt_wfe(struct cmdq_pkt *pkt, u16 event);
113 
114 /**
115  * cmdq_pkt_clear_event() - append clear event command to the CMDQ packet
116  * @pkt:	the CMDQ packet
117  * @event:	the desired event to be cleared
118  *
119  * Return: 0 for success; else the error code is returned
120  */
121 int cmdq_pkt_clear_event(struct cmdq_pkt *pkt, u16 event);
122 
123 /**
124  * cmdq_pkt_poll() - Append polling command to the CMDQ packet, ask GCE to
125  *		     execute an instruction that wait for a specified
126  *		     hardware register to check for the value w/o mask.
127  *		     All GCE hardware threads will be blocked by this
128  *		     instruction.
129  * @pkt:	the CMDQ packet
130  * @subsys:	the CMDQ sub system code
131  * @offset:	register offset from CMDQ sub system
132  * @value:	the specified target register value
133  *
134  * Return: 0 for success; else the error code is returned
135  */
136 int cmdq_pkt_poll(struct cmdq_pkt *pkt, u8 subsys,
137 		  u16 offset, u32 value);
138 
139 /**
140  * cmdq_pkt_poll_mask() - Append polling command to the CMDQ packet, ask GCE to
141  *		          execute an instruction that wait for a specified
142  *		          hardware register to check for the value w/ mask.
143  *		          All GCE hardware threads will be blocked by this
144  *		          instruction.
145  * @pkt:	the CMDQ packet
146  * @subsys:	the CMDQ sub system code
147  * @offset:	register offset from CMDQ sub system
148  * @value:	the specified target register value
149  * @mask:	the specified target register mask
150  *
151  * Return: 0 for success; else the error code is returned
152  */
153 int cmdq_pkt_poll_mask(struct cmdq_pkt *pkt, u8 subsys,
154 		       u16 offset, u32 value, u32 mask);
155 /**
156  * cmdq_pkt_flush_async() - trigger CMDQ to asynchronously execute the CMDQ
157  *                          packet and call back at the end of done packet
158  * @pkt:	the CMDQ packet
159  * @cb:		called at the end of done packet
160  * @data:	this data will pass back to cb
161  *
162  * Return: 0 for success; else the error code is returned
163  *
164  * Trigger CMDQ to asynchronously execute the CMDQ packet and call back
165  * at the end of done packet. Note that this is an ASYNC function. When the
166  * function returned, it may or may not be finished.
167  */
168 int cmdq_pkt_flush_async(struct cmdq_pkt *pkt, cmdq_async_flush_cb cb,
169 			 void *data);
170 
171 /**
172  * cmdq_pkt_flush() - trigger CMDQ to execute the CMDQ packet
173  * @pkt:	the CMDQ packet
174  *
175  * Return: 0 for success; else the error code is returned
176  *
177  * Trigger CMDQ to execute the CMDQ packet. Note that this is a
178  * synchronous flush function. When the function returned, the recorded
179  * commands have been done.
180  */
181 int cmdq_pkt_flush(struct cmdq_pkt *pkt);
182 
183 #endif	/* __MTK_CMDQ_H__ */
184