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_ADDR_HIGH(addr)	((u32)(((addr) >> 16) & GENMASK(31, 0)))
15 #define CMDQ_ADDR_LOW(addr)	((u16)(addr) | BIT(1))
16 
17 struct cmdq_pkt;
18 
19 struct cmdq_client_reg {
20 	u8 subsys;
21 	u16 offset;
22 	u16 size;
23 };
24 
25 struct cmdq_client {
26 	struct mbox_client client;
27 	struct mbox_chan *chan;
28 };
29 
30 /**
31  * cmdq_dev_get_client_reg() - parse cmdq client reg from the device
32  *			       node of CMDQ client
33  * @dev:	device of CMDQ mailbox client
34  * @client_reg: CMDQ client reg pointer
35  * @idx:	the index of desired reg
36  *
37  * Return: 0 for success; else the error code is returned
38  *
39  * Help CMDQ client parsing the cmdq client reg
40  * from the device node of CMDQ client.
41  */
42 int cmdq_dev_get_client_reg(struct device *dev,
43 			    struct cmdq_client_reg *client_reg, int idx);
44 
45 /**
46  * cmdq_mbox_create() - create CMDQ mailbox client and channel
47  * @dev:	device of CMDQ mailbox client
48  * @index:	index of CMDQ mailbox channel
49  *
50  * Return: CMDQ mailbox client pointer
51  */
52 struct cmdq_client *cmdq_mbox_create(struct device *dev, int index);
53 
54 /**
55  * cmdq_mbox_destroy() - destroy CMDQ mailbox client and channel
56  * @client:	the CMDQ mailbox client
57  */
58 void cmdq_mbox_destroy(struct cmdq_client *client);
59 
60 /**
61  * cmdq_pkt_create() - create a CMDQ packet
62  * @client:	the CMDQ mailbox client
63  * @size:	required CMDQ buffer size
64  *
65  * Return: CMDQ packet pointer
66  */
67 struct cmdq_pkt *cmdq_pkt_create(struct cmdq_client *client, size_t size);
68 
69 /**
70  * cmdq_pkt_destroy() - destroy the CMDQ packet
71  * @pkt:	the CMDQ packet
72  */
73 void cmdq_pkt_destroy(struct cmdq_pkt *pkt);
74 
75 /**
76  * cmdq_pkt_write() - append write command to the CMDQ packet
77  * @pkt:	the CMDQ packet
78  * @subsys:	the CMDQ sub system code
79  * @offset:	register offset from CMDQ sub system
80  * @value:	the specified target register value
81  *
82  * Return: 0 for success; else the error code is returned
83  */
84 int cmdq_pkt_write(struct cmdq_pkt *pkt, u8 subsys, u16 offset, u32 value);
85 
86 /**
87  * cmdq_pkt_write_mask() - append write command with mask to the CMDQ packet
88  * @pkt:	the CMDQ packet
89  * @subsys:	the CMDQ sub system code
90  * @offset:	register offset from CMDQ sub system
91  * @value:	the specified target register value
92  * @mask:	the specified target register mask
93  *
94  * Return: 0 for success; else the error code is returned
95  */
96 int cmdq_pkt_write_mask(struct cmdq_pkt *pkt, u8 subsys,
97 			u16 offset, u32 value, u32 mask);
98 
99 /*
100  * cmdq_pkt_read_s() - append read_s command to the CMDQ packet
101  * @pkt:	the CMDQ packet
102  * @high_addr_reg_idx:	internal register ID which contains high address of pa
103  * @addr_low:	low address of pa
104  * @reg_idx:	the CMDQ internal register ID to cache read data
105  *
106  * Return: 0 for success; else the error code is returned
107  */
108 int cmdq_pkt_read_s(struct cmdq_pkt *pkt, u16 high_addr_reg_idx, u16 addr_low,
109 		    u16 reg_idx);
110 
111 /**
112  * cmdq_pkt_write_s() - append write_s command to the CMDQ packet
113  * @pkt:	the CMDQ packet
114  * @high_addr_reg_idx:	internal register ID which contains high address of pa
115  * @addr_low:	low address of pa
116  * @src_reg_idx:	the CMDQ internal register ID which cache source value
117  *
118  * Return: 0 for success; else the error code is returned
119  *
120  * Support write value to physical address without subsys. Use CMDQ_ADDR_HIGH()
121  * to get high address and call cmdq_pkt_assign() to assign value into internal
122  * reg. Also use CMDQ_ADDR_LOW() to get low address for addr_low parameter when
123  * call to this function.
124  */
125 int cmdq_pkt_write_s(struct cmdq_pkt *pkt, u16 high_addr_reg_idx,
126 		     u16 addr_low, u16 src_reg_idx);
127 
128 /**
129  * cmdq_pkt_write_s_mask() - append write_s with mask command to the CMDQ packet
130  * @pkt:	the CMDQ packet
131  * @high_addr_reg_idx:	internal register ID which contains high address of pa
132  * @addr_low:	low address of pa
133  * @src_reg_idx:	the CMDQ internal register ID which cache source value
134  * @mask:	the specified target address mask, use U32_MAX if no need
135  *
136  * Return: 0 for success; else the error code is returned
137  *
138  * Support write value to physical address without subsys. Use CMDQ_ADDR_HIGH()
139  * to get high address and call cmdq_pkt_assign() to assign value into internal
140  * reg. Also use CMDQ_ADDR_LOW() to get low address for addr_low parameter when
141  * call to this function.
142  */
143 int cmdq_pkt_write_s_mask(struct cmdq_pkt *pkt, u16 high_addr_reg_idx,
144 			  u16 addr_low, u16 src_reg_idx, u32 mask);
145 
146 /**
147  * cmdq_pkt_write_s_value() - append write_s command to the CMDQ packet which
148  *			      write value to a physical address
149  * @pkt:	the CMDQ packet
150  * @high_addr_reg_idx:	internal register ID which contains high address of pa
151  * @addr_low:	low address of pa
152  * @value:	the specified target value
153  *
154  * Return: 0 for success; else the error code is returned
155  */
156 int cmdq_pkt_write_s_value(struct cmdq_pkt *pkt, u8 high_addr_reg_idx,
157 			   u16 addr_low, u32 value);
158 
159 /**
160  * cmdq_pkt_write_s_mask_value() - append write_s command with mask to the CMDQ
161  *				   packet which write value to a physical
162  *				   address
163  * @pkt:	the CMDQ packet
164  * @high_addr_reg_idx:	internal register ID which contains high address of pa
165  * @addr_low:	low address of pa
166  * @value:	the specified target value
167  * @mask:	the specified target mask
168  *
169  * Return: 0 for success; else the error code is returned
170  */
171 int cmdq_pkt_write_s_mask_value(struct cmdq_pkt *pkt, u8 high_addr_reg_idx,
172 				u16 addr_low, u32 value, u32 mask);
173 
174 /**
175  * cmdq_pkt_wfe() - append wait for event command to the CMDQ packet
176  * @pkt:	the CMDQ packet
177  * @event:	the desired event type to wait
178  * @clear:	clear event or not after event arrive
179  *
180  * Return: 0 for success; else the error code is returned
181  */
182 int cmdq_pkt_wfe(struct cmdq_pkt *pkt, u16 event, bool clear);
183 
184 /**
185  * cmdq_pkt_clear_event() - append clear event command to the CMDQ packet
186  * @pkt:	the CMDQ packet
187  * @event:	the desired event to be cleared
188  *
189  * Return: 0 for success; else the error code is returned
190  */
191 int cmdq_pkt_clear_event(struct cmdq_pkt *pkt, u16 event);
192 
193 /**
194  * cmdq_pkt_set_event() - append set event command to the CMDQ packet
195  * @pkt:	the CMDQ packet
196  * @event:	the desired event to be set
197  *
198  * Return: 0 for success; else the error code is returned
199  */
200 int cmdq_pkt_set_event(struct cmdq_pkt *pkt, u16 event);
201 
202 /**
203  * cmdq_pkt_poll() - Append polling command to the CMDQ packet, ask GCE to
204  *		     execute an instruction that wait for a specified
205  *		     hardware register to check for the value w/o mask.
206  *		     All GCE hardware threads will be blocked by this
207  *		     instruction.
208  * @pkt:	the CMDQ packet
209  * @subsys:	the CMDQ sub system code
210  * @offset:	register offset from CMDQ sub system
211  * @value:	the specified target register value
212  *
213  * Return: 0 for success; else the error code is returned
214  */
215 int cmdq_pkt_poll(struct cmdq_pkt *pkt, u8 subsys,
216 		  u16 offset, u32 value);
217 
218 /**
219  * cmdq_pkt_poll_mask() - Append polling command to the CMDQ packet, ask GCE to
220  *		          execute an instruction that wait for a specified
221  *		          hardware register to check for the value w/ mask.
222  *		          All GCE hardware threads will be blocked by this
223  *		          instruction.
224  * @pkt:	the CMDQ packet
225  * @subsys:	the CMDQ sub system code
226  * @offset:	register offset from CMDQ sub system
227  * @value:	the specified target register value
228  * @mask:	the specified target register mask
229  *
230  * Return: 0 for success; else the error code is returned
231  */
232 int cmdq_pkt_poll_mask(struct cmdq_pkt *pkt, u8 subsys,
233 		       u16 offset, u32 value, u32 mask);
234 
235 /**
236  * cmdq_pkt_assign() - Append logic assign command to the CMDQ packet, ask GCE
237  *		       to execute an instruction that set a constant value into
238  *		       internal register and use as value, mask or address in
239  *		       read/write instruction.
240  * @pkt:	the CMDQ packet
241  * @reg_idx:	the CMDQ internal register ID
242  * @value:	the specified value
243  *
244  * Return: 0 for success; else the error code is returned
245  */
246 int cmdq_pkt_assign(struct cmdq_pkt *pkt, u16 reg_idx, u32 value);
247 
248 /**
249  * cmdq_pkt_jump() - Append jump command to the CMDQ packet, ask GCE
250  *		     to execute an instruction that change current thread PC to
251  *		     a physical address which should contains more instruction.
252  * @pkt:        the CMDQ packet
253  * @addr:       physical address of target instruction buffer
254  *
255  * Return: 0 for success; else the error code is returned
256  */
257 int cmdq_pkt_jump(struct cmdq_pkt *pkt, dma_addr_t addr);
258 
259 /**
260  * cmdq_pkt_finalize() - Append EOC and jump command to pkt.
261  * @pkt:	the CMDQ packet
262  *
263  * Return: 0 for success; else the error code is returned
264  */
265 int cmdq_pkt_finalize(struct cmdq_pkt *pkt);
266 
267 /**
268  * cmdq_pkt_flush_async() - trigger CMDQ to asynchronously execute the CMDQ
269  *                          packet and call back at the end of done packet
270  * @pkt:	the CMDQ packet
271  *
272  * Return: 0 for success; else the error code is returned
273  *
274  * Trigger CMDQ to asynchronously execute the CMDQ packet and call back
275  * at the end of done packet. Note that this is an ASYNC function. When the
276  * function returned, it may or may not be finished.
277  */
278 int cmdq_pkt_flush_async(struct cmdq_pkt *pkt);
279 
280 #endif	/* __MTK_CMDQ_H__ */
281