xref: /linux/drivers/mailbox/ti-msgmgr.c (revision a2b79838b891718dd4f0caf86dfa193af789245d)
14f0ceb87SNishanth Menon // SPDX-License-Identifier: GPL-2.0
2aace66b1SNishanth Menon /*
3aace66b1SNishanth Menon  * Texas Instruments' Message Manager Driver
4aace66b1SNishanth Menon  *
54f0ceb87SNishanth Menon  * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
6aace66b1SNishanth Menon  *	Nishanth Menon
7aace66b1SNishanth Menon  */
8aace66b1SNishanth Menon 
9aace66b1SNishanth Menon #define pr_fmt(fmt) "%s: " fmt, __func__
10aace66b1SNishanth Menon 
11aace66b1SNishanth Menon #include <linux/device.h>
12aace66b1SNishanth Menon #include <linux/interrupt.h>
13aace66b1SNishanth Menon #include <linux/io.h>
14aace66b1SNishanth Menon #include <linux/kernel.h>
15aace66b1SNishanth Menon #include <linux/mailbox_controller.h>
16aace66b1SNishanth Menon #include <linux/module.h>
17aace66b1SNishanth Menon #include <linux/of_device.h>
18aace66b1SNishanth Menon #include <linux/of.h>
19aace66b1SNishanth Menon #include <linux/of_irq.h>
20aace66b1SNishanth Menon #include <linux/platform_device.h>
21aace66b1SNishanth Menon #include <linux/soc/ti/ti-msgmgr.h>
22aace66b1SNishanth Menon 
23aace66b1SNishanth Menon #define Q_DATA_OFFSET(proxy, queue, reg)	\
24aace66b1SNishanth Menon 		     ((0x10000 * (proxy)) + (0x80 * (queue)) + ((reg) * 4))
25aace66b1SNishanth Menon #define Q_STATE_OFFSET(queue)			((queue) * 0x4)
26aace66b1SNishanth Menon #define Q_STATE_ENTRY_COUNT_MASK		(0xFFF000)
27aace66b1SNishanth Menon 
28*a2b79838SNishanth Menon #define SPROXY_THREAD_OFFSET(tid) (0x1000 * (tid))
29*a2b79838SNishanth Menon #define SPROXY_THREAD_DATA_OFFSET(tid, reg) \
30*a2b79838SNishanth Menon 	(SPROXY_THREAD_OFFSET(tid) + ((reg) * 0x4) + 0x4)
31*a2b79838SNishanth Menon 
32*a2b79838SNishanth Menon #define SPROXY_THREAD_STATUS_OFFSET(tid) (SPROXY_THREAD_OFFSET(tid))
33*a2b79838SNishanth Menon 
34*a2b79838SNishanth Menon #define SPROXY_THREAD_STATUS_COUNT_MASK (0xFF)
35*a2b79838SNishanth Menon 
36*a2b79838SNishanth Menon #define SPROXY_THREAD_CTRL_OFFSET(tid) (0x1000 + SPROXY_THREAD_OFFSET(tid))
37*a2b79838SNishanth Menon #define SPROXY_THREAD_CTRL_DIR_MASK (0x1 << 31)
38*a2b79838SNishanth Menon 
39aace66b1SNishanth Menon /**
40aace66b1SNishanth Menon  * struct ti_msgmgr_valid_queue_desc - SoC valid queues meant for this processor
41aace66b1SNishanth Menon  * @queue_id:	Queue Number for this path
42aace66b1SNishanth Menon  * @proxy_id:	Proxy ID representing the processor in SoC
43aace66b1SNishanth Menon  * @is_tx:	Is this a receive path?
44aace66b1SNishanth Menon  */
45aace66b1SNishanth Menon struct ti_msgmgr_valid_queue_desc {
46aace66b1SNishanth Menon 	u8 queue_id;
47aace66b1SNishanth Menon 	u8 proxy_id;
48aace66b1SNishanth Menon 	bool is_tx;
49aace66b1SNishanth Menon };
50aace66b1SNishanth Menon 
51aace66b1SNishanth Menon /**
52aace66b1SNishanth Menon  * struct ti_msgmgr_desc - Description of message manager integration
53aace66b1SNishanth Menon  * @queue_count:	Number of Queues
54aace66b1SNishanth Menon  * @max_message_size:	Message size in bytes
55aace66b1SNishanth Menon  * @max_messages:	Number of messages
56aace66b1SNishanth Menon  * @data_first_reg:	First data register for proxy data region
57aace66b1SNishanth Menon  * @data_last_reg:	Last data register for proxy data region
588e560862SNishanth Menon  * @status_cnt_mask:	Mask for getting the status value
59*a2b79838SNishanth Menon  * @status_err_mask:	Mask for getting the error value, if applicable
60aace66b1SNishanth Menon  * @tx_polled:		Do I need to use polled mechanism for tx
61aace66b1SNishanth Menon  * @tx_poll_timeout_ms: Timeout in ms if polled
62aace66b1SNishanth Menon  * @valid_queues:	List of Valid queues that the processor can access
6389c976c2SNishanth Menon  * @data_region_name:	Name of the proxy data region
6489c976c2SNishanth Menon  * @status_region_name:	Name of the proxy status region
65*a2b79838SNishanth Menon  * @ctrl_region_name:	Name of the proxy control region
66aace66b1SNishanth Menon  * @num_valid_queues:	Number of valid queues
67*a2b79838SNishanth Menon  * @is_sproxy:		Is this an Secure Proxy instance?
68aace66b1SNishanth Menon  *
69aace66b1SNishanth Menon  * This structure is used in of match data to describe how integration
70aace66b1SNishanth Menon  * for a specific compatible SoC is done.
71aace66b1SNishanth Menon  */
72aace66b1SNishanth Menon struct ti_msgmgr_desc {
73aace66b1SNishanth Menon 	u8 queue_count;
74aace66b1SNishanth Menon 	u8 max_message_size;
75aace66b1SNishanth Menon 	u8 max_messages;
76aace66b1SNishanth Menon 	u8 data_first_reg;
77aace66b1SNishanth Menon 	u8 data_last_reg;
788e560862SNishanth Menon 	u32 status_cnt_mask;
79*a2b79838SNishanth Menon 	u32 status_err_mask;
80aace66b1SNishanth Menon 	bool tx_polled;
81aace66b1SNishanth Menon 	int tx_poll_timeout_ms;
82aace66b1SNishanth Menon 	const struct ti_msgmgr_valid_queue_desc *valid_queues;
8389c976c2SNishanth Menon 	const char *data_region_name;
8489c976c2SNishanth Menon 	const char *status_region_name;
85*a2b79838SNishanth Menon 	const char *ctrl_region_name;
86aace66b1SNishanth Menon 	int num_valid_queues;
87*a2b79838SNishanth Menon 	bool is_sproxy;
88aace66b1SNishanth Menon };
89aace66b1SNishanth Menon 
90aace66b1SNishanth Menon /**
91aace66b1SNishanth Menon  * struct ti_queue_inst - Description of a queue instance
92aace66b1SNishanth Menon  * @name:	Queue Name
93aace66b1SNishanth Menon  * @queue_id:	Queue Identifier as mapped on SoC
94aace66b1SNishanth Menon  * @proxy_id:	Proxy Identifier as mapped on SoC
95aace66b1SNishanth Menon  * @irq:	IRQ for Rx Queue
96aace66b1SNishanth Menon  * @is_tx:	'true' if transmit queue, else, 'false'
97aace66b1SNishanth Menon  * @queue_buff_start: First register of Data Buffer
98aace66b1SNishanth Menon  * @queue_buff_end: Last (or confirmation) register of Data buffer
99aace66b1SNishanth Menon  * @queue_state: Queue status register
100*a2b79838SNishanth Menon  * @queue_ctrl: Queue Control register
101aace66b1SNishanth Menon  * @chan:	Mailbox channel
102aace66b1SNishanth Menon  * @rx_buff:	Receive buffer pointer allocated at probe, max_message_size
103aace66b1SNishanth Menon  */
104aace66b1SNishanth Menon struct ti_queue_inst {
105aace66b1SNishanth Menon 	char name[30];
106aace66b1SNishanth Menon 	u8 queue_id;
107aace66b1SNishanth Menon 	u8 proxy_id;
108aace66b1SNishanth Menon 	int irq;
109aace66b1SNishanth Menon 	bool is_tx;
110aace66b1SNishanth Menon 	void __iomem *queue_buff_start;
111aace66b1SNishanth Menon 	void __iomem *queue_buff_end;
112aace66b1SNishanth Menon 	void __iomem *queue_state;
113*a2b79838SNishanth Menon 	void __iomem *queue_ctrl;
114aace66b1SNishanth Menon 	struct mbox_chan *chan;
115aace66b1SNishanth Menon 	u32 *rx_buff;
116aace66b1SNishanth Menon };
117aace66b1SNishanth Menon 
118aace66b1SNishanth Menon /**
119aace66b1SNishanth Menon  * struct ti_msgmgr_inst - Description of a Message Manager Instance
120aace66b1SNishanth Menon  * @dev:	device pointer corresponding to the Message Manager instance
121aace66b1SNishanth Menon  * @desc:	Description of the SoC integration
122aace66b1SNishanth Menon  * @queue_proxy_region:	Queue proxy region where queue buffers are located
123aace66b1SNishanth Menon  * @queue_state_debug_region:	Queue status register regions
124*a2b79838SNishanth Menon  * @queue_ctrl_region:	Queue Control register regions
125aace66b1SNishanth Menon  * @num_valid_queues:	Number of valid queues defined for the processor
126aace66b1SNishanth Menon  *		Note: other queues are probably reserved for other processors
127aace66b1SNishanth Menon  *		in the SoC.
128aace66b1SNishanth Menon  * @qinsts:	Array of valid Queue Instances for the Processor
129aace66b1SNishanth Menon  * @mbox:	Mailbox Controller
130aace66b1SNishanth Menon  * @chans:	Array for channels corresponding to the Queue Instances.
131aace66b1SNishanth Menon  */
132aace66b1SNishanth Menon struct ti_msgmgr_inst {
133aace66b1SNishanth Menon 	struct device *dev;
134aace66b1SNishanth Menon 	const struct ti_msgmgr_desc *desc;
135aace66b1SNishanth Menon 	void __iomem *queue_proxy_region;
136aace66b1SNishanth Menon 	void __iomem *queue_state_debug_region;
137*a2b79838SNishanth Menon 	void __iomem *queue_ctrl_region;
138aace66b1SNishanth Menon 	u8 num_valid_queues;
139aace66b1SNishanth Menon 	struct ti_queue_inst *qinsts;
140aace66b1SNishanth Menon 	struct mbox_controller mbox;
141aace66b1SNishanth Menon 	struct mbox_chan *chans;
142aace66b1SNishanth Menon };
143aace66b1SNishanth Menon 
144aace66b1SNishanth Menon /**
145aace66b1SNishanth Menon  * ti_msgmgr_queue_get_num_messages() - Get the number of pending messages
1468e560862SNishanth Menon  * @d:		Description of message manager
147aace66b1SNishanth Menon  * @qinst:	Queue instance for which we check the number of pending messages
148aace66b1SNishanth Menon  *
149aace66b1SNishanth Menon  * Return: number of messages pending in the queue (0 == no pending messages)
150aace66b1SNishanth Menon  */
1518e560862SNishanth Menon static inline int
1528e560862SNishanth Menon ti_msgmgr_queue_get_num_messages(const struct ti_msgmgr_desc *d,
1538e560862SNishanth Menon 				 struct ti_queue_inst *qinst)
154aace66b1SNishanth Menon {
155aace66b1SNishanth Menon 	u32 val;
1568e560862SNishanth Menon 	u32 status_cnt_mask = d->status_cnt_mask;
157aace66b1SNishanth Menon 
158aace66b1SNishanth Menon 	/*
159aace66b1SNishanth Menon 	 * We cannot use relaxed operation here - update may happen
160aace66b1SNishanth Menon 	 * real-time.
161aace66b1SNishanth Menon 	 */
1628e560862SNishanth Menon 	val = readl(qinst->queue_state) & status_cnt_mask;
1638e560862SNishanth Menon 	val >>= __ffs(status_cnt_mask);
164aace66b1SNishanth Menon 
165aace66b1SNishanth Menon 	return val;
166aace66b1SNishanth Menon }
167aace66b1SNishanth Menon 
168aace66b1SNishanth Menon /**
169*a2b79838SNishanth Menon  * ti_msgmgr_queue_is_error() - Check to see if there is queue error
170*a2b79838SNishanth Menon  * @d:		Description of message manager
171*a2b79838SNishanth Menon  * @qinst:	Queue instance for which we check the number of pending messages
172*a2b79838SNishanth Menon  *
173*a2b79838SNishanth Menon  * Return: true if error, else false
174*a2b79838SNishanth Menon  */
175*a2b79838SNishanth Menon static inline bool ti_msgmgr_queue_is_error(const struct ti_msgmgr_desc *d,
176*a2b79838SNishanth Menon 					    struct ti_queue_inst *qinst)
177*a2b79838SNishanth Menon {
178*a2b79838SNishanth Menon 	u32 val;
179*a2b79838SNishanth Menon 
180*a2b79838SNishanth Menon 	/* Msgmgr has no error detection */
181*a2b79838SNishanth Menon 	if (!d->is_sproxy)
182*a2b79838SNishanth Menon 		return false;
183*a2b79838SNishanth Menon 
184*a2b79838SNishanth Menon 	/*
185*a2b79838SNishanth Menon 	 * We cannot use relaxed operation here - update may happen
186*a2b79838SNishanth Menon 	 * real-time.
187*a2b79838SNishanth Menon 	 */
188*a2b79838SNishanth Menon 	val = readl(qinst->queue_state) & d->status_err_mask;
189*a2b79838SNishanth Menon 
190*a2b79838SNishanth Menon 	return val ? true : false;
191*a2b79838SNishanth Menon }
192*a2b79838SNishanth Menon 
193*a2b79838SNishanth Menon /**
194aace66b1SNishanth Menon  * ti_msgmgr_queue_rx_interrupt() - Interrupt handler for receive Queue
195aace66b1SNishanth Menon  * @irq:	Interrupt number
196aace66b1SNishanth Menon  * @p:		Channel Pointer
197aace66b1SNishanth Menon  *
198aace66b1SNishanth Menon  * Return: -EINVAL if there is no instance
199aace66b1SNishanth Menon  * IRQ_NONE if the interrupt is not ours.
200aace66b1SNishanth Menon  * IRQ_HANDLED if the rx interrupt was successfully handled.
201aace66b1SNishanth Menon  */
202aace66b1SNishanth Menon static irqreturn_t ti_msgmgr_queue_rx_interrupt(int irq, void *p)
203aace66b1SNishanth Menon {
204aace66b1SNishanth Menon 	struct mbox_chan *chan = p;
205aace66b1SNishanth Menon 	struct device *dev = chan->mbox->dev;
206aace66b1SNishanth Menon 	struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
207aace66b1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
208aace66b1SNishanth Menon 	const struct ti_msgmgr_desc *desc;
209aace66b1SNishanth Menon 	int msg_count, num_words;
210aace66b1SNishanth Menon 	struct ti_msgmgr_message message;
211aace66b1SNishanth Menon 	void __iomem *data_reg;
212aace66b1SNishanth Menon 	u32 *word_data;
213aace66b1SNishanth Menon 
214aace66b1SNishanth Menon 	if (WARN_ON(!inst)) {
215aace66b1SNishanth Menon 		dev_err(dev, "no platform drv data??\n");
216aace66b1SNishanth Menon 		return -EINVAL;
217aace66b1SNishanth Menon 	}
218aace66b1SNishanth Menon 
219aace66b1SNishanth Menon 	/* Do I have an invalid interrupt source? */
220aace66b1SNishanth Menon 	if (qinst->is_tx) {
221aace66b1SNishanth Menon 		dev_err(dev, "Cannot handle rx interrupt on tx channel %s\n",
222aace66b1SNishanth Menon 			qinst->name);
223aace66b1SNishanth Menon 		return IRQ_NONE;
224aace66b1SNishanth Menon 	}
225aace66b1SNishanth Menon 
2268e560862SNishanth Menon 	desc = inst->desc;
227*a2b79838SNishanth Menon 	if (ti_msgmgr_queue_is_error(desc, qinst)) {
228*a2b79838SNishanth Menon 		dev_err(dev, "Error on Rx channel %s\n", qinst->name);
229*a2b79838SNishanth Menon 		return IRQ_NONE;
230*a2b79838SNishanth Menon 	}
231*a2b79838SNishanth Menon 
232aace66b1SNishanth Menon 	/* Do I actually have messages to read? */
2338e560862SNishanth Menon 	msg_count = ti_msgmgr_queue_get_num_messages(desc, qinst);
234aace66b1SNishanth Menon 	if (!msg_count) {
235aace66b1SNishanth Menon 		/* Shared IRQ? */
236aace66b1SNishanth Menon 		dev_dbg(dev, "Spurious event - 0 pending data!\n");
237aace66b1SNishanth Menon 		return IRQ_NONE;
238aace66b1SNishanth Menon 	}
239aace66b1SNishanth Menon 
240aace66b1SNishanth Menon 	/*
241aace66b1SNishanth Menon 	 * I have no idea about the protocol being used to communicate with the
242aace66b1SNishanth Menon 	 * remote producer - 0 could be valid data, so I wont make a judgement
243aace66b1SNishanth Menon 	 * of how many bytes I should be reading. Let the client figure this
244aace66b1SNishanth Menon 	 * out.. I just read the full message and pass it on..
245aace66b1SNishanth Menon 	 */
246aace66b1SNishanth Menon 	message.len = desc->max_message_size;
247aace66b1SNishanth Menon 	message.buf = (u8 *)qinst->rx_buff;
248aace66b1SNishanth Menon 
249aace66b1SNishanth Menon 	/*
250aace66b1SNishanth Menon 	 * NOTE about register access involved here:
251aace66b1SNishanth Menon 	 * the hardware block is implemented with 32bit access operations and no
252aace66b1SNishanth Menon 	 * support for data splitting.  We don't want the hardware to misbehave
253aace66b1SNishanth Menon 	 * with sub 32bit access - For example: if the last register read is
254aace66b1SNishanth Menon 	 * split into byte wise access, it can result in the queue getting
255aace66b1SNishanth Menon 	 * stuck or indeterminate behavior. An out of order read operation may
256aace66b1SNishanth Menon 	 * result in weird data results as well.
257aace66b1SNishanth Menon 	 * Hence, we do not use memcpy_fromio or __ioread32_copy here, instead
258aace66b1SNishanth Menon 	 * we depend on readl for the purpose.
259aace66b1SNishanth Menon 	 *
260aace66b1SNishanth Menon 	 * Also note that the final register read automatically marks the
261aace66b1SNishanth Menon 	 * queue message as read.
262aace66b1SNishanth Menon 	 */
263aace66b1SNishanth Menon 	for (data_reg = qinst->queue_buff_start, word_data = qinst->rx_buff,
264aace66b1SNishanth Menon 	     num_words = (desc->max_message_size / sizeof(u32));
265aace66b1SNishanth Menon 	     num_words; num_words--, data_reg += sizeof(u32), word_data++)
266aace66b1SNishanth Menon 		*word_data = readl(data_reg);
267aace66b1SNishanth Menon 
268aace66b1SNishanth Menon 	/*
269aace66b1SNishanth Menon 	 * Last register read automatically clears the IRQ if only 1 message
270aace66b1SNishanth Menon 	 * is pending - so send the data up the stack..
271aace66b1SNishanth Menon 	 * NOTE: Client is expected to be as optimal as possible, since
272aace66b1SNishanth Menon 	 * we invoke the handler in IRQ context.
273aace66b1SNishanth Menon 	 */
274aace66b1SNishanth Menon 	mbox_chan_received_data(chan, (void *)&message);
275aace66b1SNishanth Menon 
276aace66b1SNishanth Menon 	return IRQ_HANDLED;
277aace66b1SNishanth Menon }
278aace66b1SNishanth Menon 
279aace66b1SNishanth Menon /**
280aace66b1SNishanth Menon  * ti_msgmgr_queue_peek_data() - Peek to see if there are any rx messages.
281aace66b1SNishanth Menon  * @chan:	Channel Pointer
282aace66b1SNishanth Menon  *
283aace66b1SNishanth Menon  * Return: 'true' if there is pending rx data, 'false' if there is none.
284aace66b1SNishanth Menon  */
285aace66b1SNishanth Menon static bool ti_msgmgr_queue_peek_data(struct mbox_chan *chan)
286aace66b1SNishanth Menon {
287aace66b1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
2888e560862SNishanth Menon 	struct device *dev = chan->mbox->dev;
2898e560862SNishanth Menon 	struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
290*a2b79838SNishanth Menon 	const struct ti_msgmgr_desc *desc = inst->desc;
291aace66b1SNishanth Menon 	int msg_count;
292aace66b1SNishanth Menon 
293aace66b1SNishanth Menon 	if (qinst->is_tx)
294aace66b1SNishanth Menon 		return false;
295aace66b1SNishanth Menon 
296*a2b79838SNishanth Menon 	if (ti_msgmgr_queue_is_error(desc, qinst)) {
297*a2b79838SNishanth Menon 		dev_err(dev, "Error on channel %s\n", qinst->name);
298*a2b79838SNishanth Menon 		return false;
299*a2b79838SNishanth Menon 	}
300*a2b79838SNishanth Menon 
301*a2b79838SNishanth Menon 	msg_count = ti_msgmgr_queue_get_num_messages(desc, qinst);
302aace66b1SNishanth Menon 
303aace66b1SNishanth Menon 	return msg_count ? true : false;
304aace66b1SNishanth Menon }
305aace66b1SNishanth Menon 
306aace66b1SNishanth Menon /**
307aace66b1SNishanth Menon  * ti_msgmgr_last_tx_done() - See if all the tx messages are sent
308aace66b1SNishanth Menon  * @chan:	Channel pointer
309aace66b1SNishanth Menon  *
310aace66b1SNishanth Menon  * Return: 'true' is no pending tx data, 'false' if there are any.
311aace66b1SNishanth Menon  */
312aace66b1SNishanth Menon static bool ti_msgmgr_last_tx_done(struct mbox_chan *chan)
313aace66b1SNishanth Menon {
314aace66b1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
3158e560862SNishanth Menon 	struct device *dev = chan->mbox->dev;
3168e560862SNishanth Menon 	struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
317*a2b79838SNishanth Menon 	const struct ti_msgmgr_desc *desc = inst->desc;
318aace66b1SNishanth Menon 	int msg_count;
319aace66b1SNishanth Menon 
320aace66b1SNishanth Menon 	if (!qinst->is_tx)
321aace66b1SNishanth Menon 		return false;
322aace66b1SNishanth Menon 
323*a2b79838SNishanth Menon 	if (ti_msgmgr_queue_is_error(desc, qinst)) {
324*a2b79838SNishanth Menon 		dev_err(dev, "Error on channel %s\n", qinst->name);
325*a2b79838SNishanth Menon 		return false;
326*a2b79838SNishanth Menon 	}
327*a2b79838SNishanth Menon 
328*a2b79838SNishanth Menon 	msg_count = ti_msgmgr_queue_get_num_messages(desc, qinst);
329*a2b79838SNishanth Menon 
330*a2b79838SNishanth Menon 	if (desc->is_sproxy) {
331*a2b79838SNishanth Menon 		/* In secure proxy, msg_count indicates how many we can send */
332*a2b79838SNishanth Menon 		return msg_count ? true : false;
333*a2b79838SNishanth Menon 	}
334aace66b1SNishanth Menon 
335aace66b1SNishanth Menon 	/* if we have any messages pending.. */
336aace66b1SNishanth Menon 	return msg_count ? false : true;
337aace66b1SNishanth Menon }
338aace66b1SNishanth Menon 
339aace66b1SNishanth Menon /**
340aace66b1SNishanth Menon  * ti_msgmgr_send_data() - Send data
341aace66b1SNishanth Menon  * @chan:	Channel Pointer
342aace66b1SNishanth Menon  * @data:	ti_msgmgr_message * Message Pointer
343aace66b1SNishanth Menon  *
344aace66b1SNishanth Menon  * Return: 0 if all goes good, else appropriate error messages.
345aace66b1SNishanth Menon  */
346aace66b1SNishanth Menon static int ti_msgmgr_send_data(struct mbox_chan *chan, void *data)
347aace66b1SNishanth Menon {
348aace66b1SNishanth Menon 	struct device *dev = chan->mbox->dev;
349aace66b1SNishanth Menon 	struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
350aace66b1SNishanth Menon 	const struct ti_msgmgr_desc *desc;
351aace66b1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
352aace66b1SNishanth Menon 	int num_words, trail_bytes;
353aace66b1SNishanth Menon 	struct ti_msgmgr_message *message = data;
354aace66b1SNishanth Menon 	void __iomem *data_reg;
355aace66b1SNishanth Menon 	u32 *word_data;
356aace66b1SNishanth Menon 
357aace66b1SNishanth Menon 	if (WARN_ON(!inst)) {
358aace66b1SNishanth Menon 		dev_err(dev, "no platform drv data??\n");
359aace66b1SNishanth Menon 		return -EINVAL;
360aace66b1SNishanth Menon 	}
361aace66b1SNishanth Menon 	desc = inst->desc;
362aace66b1SNishanth Menon 
363*a2b79838SNishanth Menon 	if (ti_msgmgr_queue_is_error(desc, qinst)) {
364*a2b79838SNishanth Menon 		dev_err(dev, "Error on channel %s\n", qinst->name);
365*a2b79838SNishanth Menon 		return false;
366*a2b79838SNishanth Menon 	}
367*a2b79838SNishanth Menon 
368aace66b1SNishanth Menon 	if (desc->max_message_size < message->len) {
369ca64af43SNishanth Menon 		dev_err(dev, "Queue %s message length %zu > max %d\n",
370aace66b1SNishanth Menon 			qinst->name, message->len, desc->max_message_size);
371aace66b1SNishanth Menon 		return -EINVAL;
372aace66b1SNishanth Menon 	}
373aace66b1SNishanth Menon 
374aace66b1SNishanth Menon 	/* NOTE: Constraints similar to rx path exists here as well */
375aace66b1SNishanth Menon 	for (data_reg = qinst->queue_buff_start,
376aace66b1SNishanth Menon 	     num_words = message->len / sizeof(u32),
377aace66b1SNishanth Menon 	     word_data = (u32 *)message->buf;
378aace66b1SNishanth Menon 	     num_words; num_words--, data_reg += sizeof(u32), word_data++)
379aace66b1SNishanth Menon 		writel(*word_data, data_reg);
380aace66b1SNishanth Menon 
381aace66b1SNishanth Menon 	trail_bytes = message->len % sizeof(u32);
382aace66b1SNishanth Menon 	if (trail_bytes) {
383aace66b1SNishanth Menon 		u32 data_trail = *word_data;
384aace66b1SNishanth Menon 
385aace66b1SNishanth Menon 		/* Ensure all unused data is 0 */
386aace66b1SNishanth Menon 		data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
387aace66b1SNishanth Menon 		writel(data_trail, data_reg);
388aace66b1SNishanth Menon 		data_reg++;
389aace66b1SNishanth Menon 	}
390aace66b1SNishanth Menon 	/*
391aace66b1SNishanth Menon 	 * 'data_reg' indicates next register to write. If we did not already
392aace66b1SNishanth Menon 	 * write on tx complete reg(last reg), we must do so for transmit
393aace66b1SNishanth Menon 	 */
394aace66b1SNishanth Menon 	if (data_reg <= qinst->queue_buff_end)
395aace66b1SNishanth Menon 		writel(0, qinst->queue_buff_end);
396aace66b1SNishanth Menon 
397aace66b1SNishanth Menon 	return 0;
398aace66b1SNishanth Menon }
399aace66b1SNishanth Menon 
400aace66b1SNishanth Menon /**
4015ab935e1SNishanth Menon  *  ti_msgmgr_queue_rx_irq_req() - RX IRQ request
4025ab935e1SNishanth Menon  *  @dev:	device pointer
403*a2b79838SNishanth Menon  *  @d:		descriptor for ti_msgmgr
4045ab935e1SNishanth Menon  *  @qinst:	Queue instance
4055ab935e1SNishanth Menon  *  @chan:	Channel pointer
4065ab935e1SNishanth Menon  */
4075ab935e1SNishanth Menon static int ti_msgmgr_queue_rx_irq_req(struct device *dev,
408*a2b79838SNishanth Menon 				      const struct ti_msgmgr_desc *d,
4095ab935e1SNishanth Menon 				      struct ti_queue_inst *qinst,
4105ab935e1SNishanth Menon 				      struct mbox_chan *chan)
4115ab935e1SNishanth Menon {
4125ab935e1SNishanth Menon 	int ret = 0;
4135ab935e1SNishanth Menon 	char of_rx_irq_name[7];
4145ab935e1SNishanth Menon 	struct device_node *np;
4155ab935e1SNishanth Menon 
4165ab935e1SNishanth Menon 	snprintf(of_rx_irq_name, sizeof(of_rx_irq_name),
417*a2b79838SNishanth Menon 		 "rx_%03d", d->is_sproxy ? qinst->proxy_id : qinst->queue_id);
4185ab935e1SNishanth Menon 
4195ab935e1SNishanth Menon 	/* Get the IRQ if not found */
4205ab935e1SNishanth Menon 	if (qinst->irq < 0) {
4215ab935e1SNishanth Menon 		np = of_node_get(dev->of_node);
4225ab935e1SNishanth Menon 		if (!np)
4235ab935e1SNishanth Menon 			return -ENODATA;
4245ab935e1SNishanth Menon 		qinst->irq = of_irq_get_byname(np, of_rx_irq_name);
4255ab935e1SNishanth Menon 		of_node_put(np);
4265ab935e1SNishanth Menon 
4275ab935e1SNishanth Menon 		if (qinst->irq < 0) {
4285ab935e1SNishanth Menon 			dev_err(dev,
4295ab935e1SNishanth Menon 				"QID %d PID %d:No IRQ[%s]: %d\n",
4305ab935e1SNishanth Menon 				qinst->queue_id, qinst->proxy_id,
4315ab935e1SNishanth Menon 				of_rx_irq_name, qinst->irq);
4325ab935e1SNishanth Menon 			return qinst->irq;
4335ab935e1SNishanth Menon 		}
4345ab935e1SNishanth Menon 	}
4355ab935e1SNishanth Menon 
4365ab935e1SNishanth Menon 	/* With the expectation that the IRQ might be shared in SoC */
4375ab935e1SNishanth Menon 	ret = request_irq(qinst->irq, ti_msgmgr_queue_rx_interrupt,
4385ab935e1SNishanth Menon 			  IRQF_SHARED, qinst->name, chan);
4395ab935e1SNishanth Menon 	if (ret) {
4405ab935e1SNishanth Menon 		dev_err(dev, "Unable to get IRQ %d on %s(res=%d)\n",
4415ab935e1SNishanth Menon 			qinst->irq, qinst->name, ret);
4425ab935e1SNishanth Menon 	}
4435ab935e1SNishanth Menon 
4445ab935e1SNishanth Menon 	return ret;
4455ab935e1SNishanth Menon }
4465ab935e1SNishanth Menon 
4475ab935e1SNishanth Menon /**
448aace66b1SNishanth Menon  * ti_msgmgr_queue_startup() - Startup queue
449aace66b1SNishanth Menon  * @chan:	Channel pointer
450aace66b1SNishanth Menon  *
451aace66b1SNishanth Menon  * Return: 0 if all goes good, else return corresponding error message
452aace66b1SNishanth Menon  */
453aace66b1SNishanth Menon static int ti_msgmgr_queue_startup(struct mbox_chan *chan)
454aace66b1SNishanth Menon {
455aace66b1SNishanth Menon 	struct device *dev = chan->mbox->dev;
4565ab935e1SNishanth Menon 	struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
4575ab935e1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
4585ab935e1SNishanth Menon 	const struct ti_msgmgr_desc *d = inst->desc;
459aace66b1SNishanth Menon 	int ret;
460*a2b79838SNishanth Menon 	int msg_count;
461*a2b79838SNishanth Menon 
462*a2b79838SNishanth Menon 	/*
463*a2b79838SNishanth Menon 	 * If sproxy is starting and can send messages, we are a Tx thread,
464*a2b79838SNishanth Menon 	 * else Rx
465*a2b79838SNishanth Menon 	 */
466*a2b79838SNishanth Menon 	if (d->is_sproxy) {
467*a2b79838SNishanth Menon 		qinst->is_tx = (readl(qinst->queue_ctrl) &
468*a2b79838SNishanth Menon 				SPROXY_THREAD_CTRL_DIR_MASK) ? false : true;
469*a2b79838SNishanth Menon 
470*a2b79838SNishanth Menon 		msg_count = ti_msgmgr_queue_get_num_messages(d, qinst);
471*a2b79838SNishanth Menon 
472*a2b79838SNishanth Menon 		if (!msg_count && qinst->is_tx) {
473*a2b79838SNishanth Menon 			dev_err(dev, "%s: Cannot transmit with 0 credits!\n",
474*a2b79838SNishanth Menon 				qinst->name);
475*a2b79838SNishanth Menon 			return -EINVAL;
476*a2b79838SNishanth Menon 		}
477*a2b79838SNishanth Menon 	}
478aace66b1SNishanth Menon 
479aace66b1SNishanth Menon 	if (!qinst->is_tx) {
4805ab935e1SNishanth Menon 		/* Allocate usage buffer for rx */
4815ab935e1SNishanth Menon 		qinst->rx_buff = kzalloc(d->max_message_size, GFP_KERNEL);
4825ab935e1SNishanth Menon 		if (!qinst->rx_buff)
4835ab935e1SNishanth Menon 			return -ENOMEM;
4845ab935e1SNishanth Menon 		/* Request IRQ */
485*a2b79838SNishanth Menon 		ret = ti_msgmgr_queue_rx_irq_req(dev, d, qinst, chan);
486aace66b1SNishanth Menon 		if (ret) {
4875ab935e1SNishanth Menon 			kfree(qinst->rx_buff);
488aace66b1SNishanth Menon 			return ret;
489aace66b1SNishanth Menon 		}
490aace66b1SNishanth Menon 	}
491aace66b1SNishanth Menon 
492aace66b1SNishanth Menon 	return 0;
493aace66b1SNishanth Menon }
494aace66b1SNishanth Menon 
495aace66b1SNishanth Menon /**
496aace66b1SNishanth Menon  * ti_msgmgr_queue_shutdown() - Shutdown the queue
497aace66b1SNishanth Menon  * @chan:	Channel pointer
498aace66b1SNishanth Menon  */
499aace66b1SNishanth Menon static void ti_msgmgr_queue_shutdown(struct mbox_chan *chan)
500aace66b1SNishanth Menon {
501aace66b1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
502aace66b1SNishanth Menon 
5035ab935e1SNishanth Menon 	if (!qinst->is_tx) {
504aace66b1SNishanth Menon 		free_irq(qinst->irq, chan);
5055ab935e1SNishanth Menon 		kfree(qinst->rx_buff);
5065ab935e1SNishanth Menon 	}
507aace66b1SNishanth Menon }
508aace66b1SNishanth Menon 
509aace66b1SNishanth Menon /**
510aace66b1SNishanth Menon  * ti_msgmgr_of_xlate() - Translation of phandle to queue
511aace66b1SNishanth Menon  * @mbox:	Mailbox controller
512aace66b1SNishanth Menon  * @p:		phandle pointer
513aace66b1SNishanth Menon  *
514aace66b1SNishanth Menon  * Return: Mailbox channel corresponding to the queue, else return error
515aace66b1SNishanth Menon  * pointer.
516aace66b1SNishanth Menon  */
517aace66b1SNishanth Menon static struct mbox_chan *ti_msgmgr_of_xlate(struct mbox_controller *mbox,
518aace66b1SNishanth Menon 					    const struct of_phandle_args *p)
519aace66b1SNishanth Menon {
520aace66b1SNishanth Menon 	struct ti_msgmgr_inst *inst;
521aace66b1SNishanth Menon 	int req_qid, req_pid;
522aace66b1SNishanth Menon 	struct ti_queue_inst *qinst;
523*a2b79838SNishanth Menon 	const struct ti_msgmgr_desc *d;
524*a2b79838SNishanth Menon 	int i, ncells;
525aace66b1SNishanth Menon 
526aace66b1SNishanth Menon 	inst = container_of(mbox, struct ti_msgmgr_inst, mbox);
527aace66b1SNishanth Menon 	if (WARN_ON(!inst))
528aace66b1SNishanth Menon 		return ERR_PTR(-EINVAL);
529aace66b1SNishanth Menon 
530*a2b79838SNishanth Menon 	d = inst->desc;
531*a2b79838SNishanth Menon 
532*a2b79838SNishanth Menon 	if (d->is_sproxy)
533*a2b79838SNishanth Menon 		ncells = 1;
534*a2b79838SNishanth Menon 	else
535*a2b79838SNishanth Menon 		ncells = 2;
536*a2b79838SNishanth Menon 	if (p->args_count != ncells) {
537*a2b79838SNishanth Menon 		dev_err(inst->dev, "Invalid arguments in dt[%d]. Must be %d\n",
538*a2b79838SNishanth Menon 			p->args_count, ncells);
539aace66b1SNishanth Menon 		return ERR_PTR(-EINVAL);
540aace66b1SNishanth Menon 	}
541*a2b79838SNishanth Menon 	if (ncells == 1) {
542*a2b79838SNishanth Menon 		req_qid = 0;
543*a2b79838SNishanth Menon 		req_pid = p->args[0];
544*a2b79838SNishanth Menon 	} else {
545aace66b1SNishanth Menon 		req_qid = p->args[0];
546aace66b1SNishanth Menon 		req_pid = p->args[1];
547*a2b79838SNishanth Menon 	}
548*a2b79838SNishanth Menon 
549*a2b79838SNishanth Menon 	if (d->is_sproxy) {
550*a2b79838SNishanth Menon 		if (req_pid > d->num_valid_queues)
551*a2b79838SNishanth Menon 			goto err;
552*a2b79838SNishanth Menon 		qinst = &inst->qinsts[req_pid];
553*a2b79838SNishanth Menon 		return qinst->chan;
554*a2b79838SNishanth Menon 	}
555aace66b1SNishanth Menon 
556aace66b1SNishanth Menon 	for (qinst = inst->qinsts, i = 0; i < inst->num_valid_queues;
557aace66b1SNishanth Menon 	     i++, qinst++) {
558aace66b1SNishanth Menon 		if (req_qid == qinst->queue_id && req_pid == qinst->proxy_id)
559aace66b1SNishanth Menon 			return qinst->chan;
560aace66b1SNishanth Menon 	}
561aace66b1SNishanth Menon 
562*a2b79838SNishanth Menon err:
563aace66b1SNishanth Menon 	dev_err(inst->dev, "Queue ID %d, Proxy ID %d is wrong on %s\n",
564aace66b1SNishanth Menon 		req_qid, req_pid, p->np->name);
565aace66b1SNishanth Menon 	return ERR_PTR(-ENOENT);
566aace66b1SNishanth Menon }
567aace66b1SNishanth Menon 
568aace66b1SNishanth Menon /**
569aace66b1SNishanth Menon  * ti_msgmgr_queue_setup() - Setup data structures for each queue instance
570aace66b1SNishanth Menon  * @idx:	index of the queue
571aace66b1SNishanth Menon  * @dev:	pointer to the message manager device
572aace66b1SNishanth Menon  * @np:		pointer to the of node
573aace66b1SNishanth Menon  * @inst:	Queue instance pointer
574aace66b1SNishanth Menon  * @d:		Message Manager instance description data
575aace66b1SNishanth Menon  * @qd:		Queue description data
576aace66b1SNishanth Menon  * @qinst:	Queue instance pointer
577aace66b1SNishanth Menon  * @chan:	pointer to mailbox channel
578aace66b1SNishanth Menon  *
579aace66b1SNishanth Menon  * Return: 0 if all went well, else return corresponding error
580aace66b1SNishanth Menon  */
581aace66b1SNishanth Menon static int ti_msgmgr_queue_setup(int idx, struct device *dev,
582aace66b1SNishanth Menon 				 struct device_node *np,
583aace66b1SNishanth Menon 				 struct ti_msgmgr_inst *inst,
584aace66b1SNishanth Menon 				 const struct ti_msgmgr_desc *d,
585aace66b1SNishanth Menon 				 const struct ti_msgmgr_valid_queue_desc *qd,
586aace66b1SNishanth Menon 				 struct ti_queue_inst *qinst,
587aace66b1SNishanth Menon 				 struct mbox_chan *chan)
588aace66b1SNishanth Menon {
589*a2b79838SNishanth Menon 	char *dir;
590*a2b79838SNishanth Menon 
591aace66b1SNishanth Menon 	qinst->proxy_id = qd->proxy_id;
592aace66b1SNishanth Menon 	qinst->queue_id = qd->queue_id;
593aace66b1SNishanth Menon 
594aace66b1SNishanth Menon 	if (qinst->queue_id > d->queue_count) {
595aace66b1SNishanth Menon 		dev_err(dev, "Queue Data [idx=%d] queuid %d > %d\n",
596aace66b1SNishanth Menon 			idx, qinst->queue_id, d->queue_count);
597aace66b1SNishanth Menon 		return -ERANGE;
598aace66b1SNishanth Menon 	}
599aace66b1SNishanth Menon 
600*a2b79838SNishanth Menon 	if (d->is_sproxy) {
601aace66b1SNishanth Menon 		qinst->queue_buff_start = inst->queue_proxy_region +
602*a2b79838SNishanth Menon 		    SPROXY_THREAD_DATA_OFFSET(qinst->proxy_id,
603*a2b79838SNishanth Menon 					      d->data_first_reg);
604aace66b1SNishanth Menon 		qinst->queue_buff_end = inst->queue_proxy_region +
605*a2b79838SNishanth Menon 		    SPROXY_THREAD_DATA_OFFSET(qinst->proxy_id,
606*a2b79838SNishanth Menon 					      d->data_last_reg);
607aace66b1SNishanth Menon 		qinst->queue_state = inst->queue_state_debug_region +
608*a2b79838SNishanth Menon 		    SPROXY_THREAD_STATUS_OFFSET(qinst->proxy_id);
609*a2b79838SNishanth Menon 		qinst->queue_ctrl = inst->queue_ctrl_region +
610*a2b79838SNishanth Menon 		    SPROXY_THREAD_CTRL_OFFSET(qinst->proxy_id);
611*a2b79838SNishanth Menon 
612*a2b79838SNishanth Menon 		/* XXX: DONOT read registers here!.. Some may be unusable */
613*a2b79838SNishanth Menon 		dir = "thr";
614*a2b79838SNishanth Menon 		snprintf(qinst->name, sizeof(qinst->name), "%s %s_%03d",
615*a2b79838SNishanth Menon 			 dev_name(dev), dir, qinst->proxy_id);
616*a2b79838SNishanth Menon 	} else {
617*a2b79838SNishanth Menon 		qinst->queue_buff_start = inst->queue_proxy_region +
618*a2b79838SNishanth Menon 		    Q_DATA_OFFSET(qinst->proxy_id, qinst->queue_id,
619*a2b79838SNishanth Menon 				  d->data_first_reg);
620*a2b79838SNishanth Menon 		qinst->queue_buff_end = inst->queue_proxy_region +
621*a2b79838SNishanth Menon 		    Q_DATA_OFFSET(qinst->proxy_id, qinst->queue_id,
622*a2b79838SNishanth Menon 				  d->data_last_reg);
623*a2b79838SNishanth Menon 		qinst->queue_state =
624*a2b79838SNishanth Menon 		    inst->queue_state_debug_region +
625aace66b1SNishanth Menon 		    Q_STATE_OFFSET(qinst->queue_id);
626*a2b79838SNishanth Menon 		qinst->is_tx = qd->is_tx;
627*a2b79838SNishanth Menon 		dir = qinst->is_tx ? "tx" : "rx";
628*a2b79838SNishanth Menon 		snprintf(qinst->name, sizeof(qinst->name), "%s %s_%03d_%03d",
629*a2b79838SNishanth Menon 			 dev_name(dev), dir, qinst->queue_id, qinst->proxy_id);
630*a2b79838SNishanth Menon 	}
631*a2b79838SNishanth Menon 
632aace66b1SNishanth Menon 	qinst->chan = chan;
633aace66b1SNishanth Menon 
6345ab935e1SNishanth Menon 	/* Setup an error value for IRQ - Lazy allocation */
6355ab935e1SNishanth Menon 	qinst->irq = -EINVAL;
6365ab935e1SNishanth Menon 
637aace66b1SNishanth Menon 	chan->con_priv = qinst;
638aace66b1SNishanth Menon 
639aace66b1SNishanth Menon 	dev_dbg(dev, "[%d] qidx=%d pidx=%d irq=%d q_s=%p q_e = %p\n",
640aace66b1SNishanth Menon 		idx, qinst->queue_id, qinst->proxy_id, qinst->irq,
641aace66b1SNishanth Menon 		qinst->queue_buff_start, qinst->queue_buff_end);
642aace66b1SNishanth Menon 	return 0;
643aace66b1SNishanth Menon }
644aace66b1SNishanth Menon 
645aace66b1SNishanth Menon /* Queue operations */
646aace66b1SNishanth Menon static const struct mbox_chan_ops ti_msgmgr_chan_ops = {
647aace66b1SNishanth Menon 	.startup = ti_msgmgr_queue_startup,
648aace66b1SNishanth Menon 	.shutdown = ti_msgmgr_queue_shutdown,
649aace66b1SNishanth Menon 	.peek_data = ti_msgmgr_queue_peek_data,
650aace66b1SNishanth Menon 	.last_tx_done = ti_msgmgr_last_tx_done,
651aace66b1SNishanth Menon 	.send_data = ti_msgmgr_send_data,
652aace66b1SNishanth Menon };
653aace66b1SNishanth Menon 
654aace66b1SNishanth Menon /* Keystone K2G SoC integration details */
655aace66b1SNishanth Menon static const struct ti_msgmgr_valid_queue_desc k2g_valid_queues[] = {
656aace66b1SNishanth Menon 	{.queue_id = 0, .proxy_id = 0, .is_tx = true,},
657aace66b1SNishanth Menon 	{.queue_id = 1, .proxy_id = 0, .is_tx = true,},
658aace66b1SNishanth Menon 	{.queue_id = 2, .proxy_id = 0, .is_tx = true,},
659aace66b1SNishanth Menon 	{.queue_id = 3, .proxy_id = 0, .is_tx = true,},
660aace66b1SNishanth Menon 	{.queue_id = 5, .proxy_id = 2, .is_tx = false,},
661aace66b1SNishanth Menon 	{.queue_id = 56, .proxy_id = 1, .is_tx = true,},
662aace66b1SNishanth Menon 	{.queue_id = 57, .proxy_id = 2, .is_tx = false,},
663aace66b1SNishanth Menon 	{.queue_id = 58, .proxy_id = 3, .is_tx = true,},
664aace66b1SNishanth Menon 	{.queue_id = 59, .proxy_id = 4, .is_tx = true,},
665aace66b1SNishanth Menon 	{.queue_id = 60, .proxy_id = 5, .is_tx = true,},
666aace66b1SNishanth Menon 	{.queue_id = 61, .proxy_id = 6, .is_tx = true,},
667aace66b1SNishanth Menon };
668aace66b1SNishanth Menon 
669aace66b1SNishanth Menon static const struct ti_msgmgr_desc k2g_desc = {
670aace66b1SNishanth Menon 	.queue_count = 64,
671aace66b1SNishanth Menon 	.max_message_size = 64,
672aace66b1SNishanth Menon 	.max_messages = 128,
67389c976c2SNishanth Menon 	.data_region_name = "queue_proxy_region",
67489c976c2SNishanth Menon 	.status_region_name = "queue_state_debug_region",
675aace66b1SNishanth Menon 	.data_first_reg = 16,
676aace66b1SNishanth Menon 	.data_last_reg = 31,
6778e560862SNishanth Menon 	.status_cnt_mask = Q_STATE_ENTRY_COUNT_MASK,
678aace66b1SNishanth Menon 	.tx_polled = false,
679aace66b1SNishanth Menon 	.valid_queues = k2g_valid_queues,
680aace66b1SNishanth Menon 	.num_valid_queues = ARRAY_SIZE(k2g_valid_queues),
681*a2b79838SNishanth Menon 	.is_sproxy = false,
682*a2b79838SNishanth Menon };
683*a2b79838SNishanth Menon 
684*a2b79838SNishanth Menon static const struct ti_msgmgr_desc am654_desc = {
685*a2b79838SNishanth Menon 	.queue_count = 190,
686*a2b79838SNishanth Menon 	.num_valid_queues = 190,
687*a2b79838SNishanth Menon 	.max_message_size = 60,
688*a2b79838SNishanth Menon 	.data_region_name = "target_data",
689*a2b79838SNishanth Menon 	.status_region_name = "rt",
690*a2b79838SNishanth Menon 	.ctrl_region_name = "scfg",
691*a2b79838SNishanth Menon 	.data_first_reg = 0,
692*a2b79838SNishanth Menon 	.data_last_reg = 14,
693*a2b79838SNishanth Menon 	.status_cnt_mask = SPROXY_THREAD_STATUS_COUNT_MASK,
694*a2b79838SNishanth Menon 	.tx_polled = false,
695*a2b79838SNishanth Menon 	.is_sproxy = true,
696aace66b1SNishanth Menon };
697aace66b1SNishanth Menon 
698aace66b1SNishanth Menon static const struct of_device_id ti_msgmgr_of_match[] = {
699aace66b1SNishanth Menon 	{.compatible = "ti,k2g-message-manager", .data = &k2g_desc},
700*a2b79838SNishanth Menon 	{.compatible = "ti,am654-secure-proxy", .data = &am654_desc},
701aace66b1SNishanth Menon 	{ /* Sentinel */ }
702aace66b1SNishanth Menon };
703*a2b79838SNishanth Menon 
704aace66b1SNishanth Menon MODULE_DEVICE_TABLE(of, ti_msgmgr_of_match);
705aace66b1SNishanth Menon 
706aace66b1SNishanth Menon static int ti_msgmgr_probe(struct platform_device *pdev)
707aace66b1SNishanth Menon {
708aace66b1SNishanth Menon 	struct device *dev = &pdev->dev;
709aace66b1SNishanth Menon 	const struct of_device_id *of_id;
710aace66b1SNishanth Menon 	struct device_node *np;
711aace66b1SNishanth Menon 	struct resource *res;
712aace66b1SNishanth Menon 	const struct ti_msgmgr_desc *desc;
713aace66b1SNishanth Menon 	struct ti_msgmgr_inst *inst;
714aace66b1SNishanth Menon 	struct ti_queue_inst *qinst;
715aace66b1SNishanth Menon 	struct mbox_controller *mbox;
716aace66b1SNishanth Menon 	struct mbox_chan *chans;
717aace66b1SNishanth Menon 	int queue_count;
718aace66b1SNishanth Menon 	int i;
719aace66b1SNishanth Menon 	int ret = -EINVAL;
720aace66b1SNishanth Menon 	const struct ti_msgmgr_valid_queue_desc *queue_desc;
721aace66b1SNishanth Menon 
722aace66b1SNishanth Menon 	if (!dev->of_node) {
723aace66b1SNishanth Menon 		dev_err(dev, "no OF information\n");
724aace66b1SNishanth Menon 		return -EINVAL;
725aace66b1SNishanth Menon 	}
726aace66b1SNishanth Menon 	np = dev->of_node;
727aace66b1SNishanth Menon 
728aace66b1SNishanth Menon 	of_id = of_match_device(ti_msgmgr_of_match, dev);
729aace66b1SNishanth Menon 	if (!of_id) {
730aace66b1SNishanth Menon 		dev_err(dev, "OF data missing\n");
731aace66b1SNishanth Menon 		return -EINVAL;
732aace66b1SNishanth Menon 	}
733aace66b1SNishanth Menon 	desc = of_id->data;
734aace66b1SNishanth Menon 
735aace66b1SNishanth Menon 	inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
736aace66b1SNishanth Menon 	if (!inst)
737aace66b1SNishanth Menon 		return -ENOMEM;
738aace66b1SNishanth Menon 
739aace66b1SNishanth Menon 	inst->dev = dev;
740aace66b1SNishanth Menon 	inst->desc = desc;
741aace66b1SNishanth Menon 
742aace66b1SNishanth Menon 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
74389c976c2SNishanth Menon 					   desc->data_region_name);
744aace66b1SNishanth Menon 	inst->queue_proxy_region = devm_ioremap_resource(dev, res);
745aace66b1SNishanth Menon 	if (IS_ERR(inst->queue_proxy_region))
746aace66b1SNishanth Menon 		return PTR_ERR(inst->queue_proxy_region);
747aace66b1SNishanth Menon 
748aace66b1SNishanth Menon 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
74989c976c2SNishanth Menon 					   desc->status_region_name);
750aace66b1SNishanth Menon 	inst->queue_state_debug_region = devm_ioremap_resource(dev, res);
751aace66b1SNishanth Menon 	if (IS_ERR(inst->queue_state_debug_region))
752aace66b1SNishanth Menon 		return PTR_ERR(inst->queue_state_debug_region);
753aace66b1SNishanth Menon 
754*a2b79838SNishanth Menon 	if (desc->is_sproxy) {
755*a2b79838SNishanth Menon 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
756*a2b79838SNishanth Menon 						   desc->ctrl_region_name);
757*a2b79838SNishanth Menon 		inst->queue_ctrl_region = devm_ioremap_resource(dev, res);
758*a2b79838SNishanth Menon 		if (IS_ERR(inst->queue_ctrl_region))
759*a2b79838SNishanth Menon 			return PTR_ERR(inst->queue_ctrl_region);
760*a2b79838SNishanth Menon 	}
761*a2b79838SNishanth Menon 
762aace66b1SNishanth Menon 	dev_dbg(dev, "proxy region=%p, queue_state=%p\n",
763aace66b1SNishanth Menon 		inst->queue_proxy_region, inst->queue_state_debug_region);
764aace66b1SNishanth Menon 
765aace66b1SNishanth Menon 	queue_count = desc->num_valid_queues;
766aace66b1SNishanth Menon 	if (!queue_count || queue_count > desc->queue_count) {
767aace66b1SNishanth Menon 		dev_crit(dev, "Invalid Number of queues %d. Max %d\n",
768aace66b1SNishanth Menon 			 queue_count, desc->queue_count);
769aace66b1SNishanth Menon 		return -ERANGE;
770aace66b1SNishanth Menon 	}
771aace66b1SNishanth Menon 	inst->num_valid_queues = queue_count;
772aace66b1SNishanth Menon 
773a86854d0SKees Cook 	qinst = devm_kcalloc(dev, queue_count, sizeof(*qinst), GFP_KERNEL);
774aace66b1SNishanth Menon 	if (!qinst)
775aace66b1SNishanth Menon 		return -ENOMEM;
776aace66b1SNishanth Menon 	inst->qinsts = qinst;
777aace66b1SNishanth Menon 
778a86854d0SKees Cook 	chans = devm_kcalloc(dev, queue_count, sizeof(*chans), GFP_KERNEL);
779aace66b1SNishanth Menon 	if (!chans)
780aace66b1SNishanth Menon 		return -ENOMEM;
781aace66b1SNishanth Menon 	inst->chans = chans;
782aace66b1SNishanth Menon 
783*a2b79838SNishanth Menon 	if (desc->is_sproxy) {
784*a2b79838SNishanth Menon 		struct ti_msgmgr_valid_queue_desc sproxy_desc;
785*a2b79838SNishanth Menon 
786*a2b79838SNishanth Menon 		/* All proxies may be valid in Secure Proxy instance */
787*a2b79838SNishanth Menon 		for (i = 0; i < queue_count; i++, qinst++, chans++) {
788*a2b79838SNishanth Menon 			sproxy_desc.queue_id = 0;
789*a2b79838SNishanth Menon 			sproxy_desc.proxy_id = i;
790*a2b79838SNishanth Menon 			ret = ti_msgmgr_queue_setup(i, dev, np, inst,
791*a2b79838SNishanth Menon 						    desc, &sproxy_desc, qinst,
792*a2b79838SNishanth Menon 						    chans);
793*a2b79838SNishanth Menon 			if (ret)
794*a2b79838SNishanth Menon 				return ret;
795*a2b79838SNishanth Menon 		}
796*a2b79838SNishanth Menon 	} else {
797*a2b79838SNishanth Menon 		/* Only Some proxies are valid in Message Manager */
798aace66b1SNishanth Menon 		for (i = 0, queue_desc = desc->valid_queues;
799aace66b1SNishanth Menon 		     i < queue_count; i++, qinst++, chans++, queue_desc++) {
800aace66b1SNishanth Menon 			ret = ti_msgmgr_queue_setup(i, dev, np, inst,
801*a2b79838SNishanth Menon 						    desc, queue_desc, qinst,
802*a2b79838SNishanth Menon 						    chans);
803aace66b1SNishanth Menon 			if (ret)
804aace66b1SNishanth Menon 				return ret;
805aace66b1SNishanth Menon 		}
806*a2b79838SNishanth Menon 	}
807aace66b1SNishanth Menon 
808aace66b1SNishanth Menon 	mbox = &inst->mbox;
809aace66b1SNishanth Menon 	mbox->dev = dev;
810aace66b1SNishanth Menon 	mbox->ops = &ti_msgmgr_chan_ops;
811aace66b1SNishanth Menon 	mbox->chans = inst->chans;
812aace66b1SNishanth Menon 	mbox->num_chans = inst->num_valid_queues;
813aace66b1SNishanth Menon 	mbox->txdone_irq = false;
814aace66b1SNishanth Menon 	mbox->txdone_poll = desc->tx_polled;
815aace66b1SNishanth Menon 	if (desc->tx_polled)
816aace66b1SNishanth Menon 		mbox->txpoll_period = desc->tx_poll_timeout_ms;
817aace66b1SNishanth Menon 	mbox->of_xlate = ti_msgmgr_of_xlate;
818aace66b1SNishanth Menon 
819aace66b1SNishanth Menon 	platform_set_drvdata(pdev, inst);
820aace66b1SNishanth Menon 	ret = mbox_controller_register(mbox);
821aace66b1SNishanth Menon 	if (ret)
822aace66b1SNishanth Menon 		dev_err(dev, "Failed to register mbox_controller(%d)\n", ret);
823aace66b1SNishanth Menon 
824aace66b1SNishanth Menon 	return ret;
825aace66b1SNishanth Menon }
826aace66b1SNishanth Menon 
827aace66b1SNishanth Menon static int ti_msgmgr_remove(struct platform_device *pdev)
828aace66b1SNishanth Menon {
829aace66b1SNishanth Menon 	struct ti_msgmgr_inst *inst;
830aace66b1SNishanth Menon 
831aace66b1SNishanth Menon 	inst = platform_get_drvdata(pdev);
832aace66b1SNishanth Menon 	mbox_controller_unregister(&inst->mbox);
833aace66b1SNishanth Menon 
834aace66b1SNishanth Menon 	return 0;
835aace66b1SNishanth Menon }
836aace66b1SNishanth Menon 
837aace66b1SNishanth Menon static struct platform_driver ti_msgmgr_driver = {
838aace66b1SNishanth Menon 	.probe = ti_msgmgr_probe,
839aace66b1SNishanth Menon 	.remove = ti_msgmgr_remove,
840aace66b1SNishanth Menon 	.driver = {
841aace66b1SNishanth Menon 		   .name = "ti-msgmgr",
842aace66b1SNishanth Menon 		   .of_match_table = of_match_ptr(ti_msgmgr_of_match),
843aace66b1SNishanth Menon 	},
844aace66b1SNishanth Menon };
845aace66b1SNishanth Menon module_platform_driver(ti_msgmgr_driver);
846aace66b1SNishanth Menon 
847aace66b1SNishanth Menon MODULE_LICENSE("GPL v2");
848aace66b1SNishanth Menon MODULE_DESCRIPTION("TI message manager driver");
849aace66b1SNishanth Menon MODULE_AUTHOR("Nishanth Menon");
850aace66b1SNishanth Menon MODULE_ALIAS("platform:ti-msgmgr");
851