xref: /linux/drivers/mailbox/ti-msgmgr.c (revision ca64af43924f85d5de1b384709af3a3ea8c7ebbb)
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 
28aace66b1SNishanth Menon /**
29aace66b1SNishanth Menon  * struct ti_msgmgr_valid_queue_desc - SoC valid queues meant for this processor
30aace66b1SNishanth Menon  * @queue_id:	Queue Number for this path
31aace66b1SNishanth Menon  * @proxy_id:	Proxy ID representing the processor in SoC
32aace66b1SNishanth Menon  * @is_tx:	Is this a receive path?
33aace66b1SNishanth Menon  */
34aace66b1SNishanth Menon struct ti_msgmgr_valid_queue_desc {
35aace66b1SNishanth Menon 	u8 queue_id;
36aace66b1SNishanth Menon 	u8 proxy_id;
37aace66b1SNishanth Menon 	bool is_tx;
38aace66b1SNishanth Menon };
39aace66b1SNishanth Menon 
40aace66b1SNishanth Menon /**
41aace66b1SNishanth Menon  * struct ti_msgmgr_desc - Description of message manager integration
42aace66b1SNishanth Menon  * @queue_count:	Number of Queues
43aace66b1SNishanth Menon  * @max_message_size:	Message size in bytes
44aace66b1SNishanth Menon  * @max_messages:	Number of messages
45aace66b1SNishanth Menon  * @q_slices:		Number of queue engines
46aace66b1SNishanth Menon  * @q_proxies:		Number of queue proxies per page
47aace66b1SNishanth Menon  * @data_first_reg:	First data register for proxy data region
48aace66b1SNishanth Menon  * @data_last_reg:	Last data register for proxy data region
49aace66b1SNishanth Menon  * @tx_polled:		Do I need to use polled mechanism for tx
50aace66b1SNishanth Menon  * @tx_poll_timeout_ms: Timeout in ms if polled
51aace66b1SNishanth Menon  * @valid_queues:	List of Valid queues that the processor can access
52aace66b1SNishanth Menon  * @num_valid_queues:	Number of valid queues
53aace66b1SNishanth Menon  *
54aace66b1SNishanth Menon  * This structure is used in of match data to describe how integration
55aace66b1SNishanth Menon  * for a specific compatible SoC is done.
56aace66b1SNishanth Menon  */
57aace66b1SNishanth Menon struct ti_msgmgr_desc {
58aace66b1SNishanth Menon 	u8 queue_count;
59aace66b1SNishanth Menon 	u8 max_message_size;
60aace66b1SNishanth Menon 	u8 max_messages;
61aace66b1SNishanth Menon 	u8 q_slices;
62aace66b1SNishanth Menon 	u8 q_proxies;
63aace66b1SNishanth Menon 	u8 data_first_reg;
64aace66b1SNishanth Menon 	u8 data_last_reg;
65aace66b1SNishanth Menon 	bool tx_polled;
66aace66b1SNishanth Menon 	int tx_poll_timeout_ms;
67aace66b1SNishanth Menon 	const struct ti_msgmgr_valid_queue_desc *valid_queues;
68aace66b1SNishanth Menon 	int num_valid_queues;
69aace66b1SNishanth Menon };
70aace66b1SNishanth Menon 
71aace66b1SNishanth Menon /**
72aace66b1SNishanth Menon  * struct ti_queue_inst - Description of a queue instance
73aace66b1SNishanth Menon  * @name:	Queue Name
74aace66b1SNishanth Menon  * @queue_id:	Queue Identifier as mapped on SoC
75aace66b1SNishanth Menon  * @proxy_id:	Proxy Identifier as mapped on SoC
76aace66b1SNishanth Menon  * @irq:	IRQ for Rx Queue
77aace66b1SNishanth Menon  * @is_tx:	'true' if transmit queue, else, 'false'
78aace66b1SNishanth Menon  * @queue_buff_start: First register of Data Buffer
79aace66b1SNishanth Menon  * @queue_buff_end: Last (or confirmation) register of Data buffer
80aace66b1SNishanth Menon  * @queue_state: Queue status register
81aace66b1SNishanth Menon  * @chan:	Mailbox channel
82aace66b1SNishanth Menon  * @rx_buff:	Receive buffer pointer allocated at probe, max_message_size
83aace66b1SNishanth Menon  */
84aace66b1SNishanth Menon struct ti_queue_inst {
85aace66b1SNishanth Menon 	char name[30];
86aace66b1SNishanth Menon 	u8 queue_id;
87aace66b1SNishanth Menon 	u8 proxy_id;
88aace66b1SNishanth Menon 	int irq;
89aace66b1SNishanth Menon 	bool is_tx;
90aace66b1SNishanth Menon 	void __iomem *queue_buff_start;
91aace66b1SNishanth Menon 	void __iomem *queue_buff_end;
92aace66b1SNishanth Menon 	void __iomem *queue_state;
93aace66b1SNishanth Menon 	struct mbox_chan *chan;
94aace66b1SNishanth Menon 	u32 *rx_buff;
95aace66b1SNishanth Menon };
96aace66b1SNishanth Menon 
97aace66b1SNishanth Menon /**
98aace66b1SNishanth Menon  * struct ti_msgmgr_inst - Description of a Message Manager Instance
99aace66b1SNishanth Menon  * @dev:	device pointer corresponding to the Message Manager instance
100aace66b1SNishanth Menon  * @desc:	Description of the SoC integration
101aace66b1SNishanth Menon  * @queue_proxy_region:	Queue proxy region where queue buffers are located
102aace66b1SNishanth Menon  * @queue_state_debug_region:	Queue status register regions
103aace66b1SNishanth Menon  * @num_valid_queues:	Number of valid queues defined for the processor
104aace66b1SNishanth Menon  *		Note: other queues are probably reserved for other processors
105aace66b1SNishanth Menon  *		in the SoC.
106aace66b1SNishanth Menon  * @qinsts:	Array of valid Queue Instances for the Processor
107aace66b1SNishanth Menon  * @mbox:	Mailbox Controller
108aace66b1SNishanth Menon  * @chans:	Array for channels corresponding to the Queue Instances.
109aace66b1SNishanth Menon  */
110aace66b1SNishanth Menon struct ti_msgmgr_inst {
111aace66b1SNishanth Menon 	struct device *dev;
112aace66b1SNishanth Menon 	const struct ti_msgmgr_desc *desc;
113aace66b1SNishanth Menon 	void __iomem *queue_proxy_region;
114aace66b1SNishanth Menon 	void __iomem *queue_state_debug_region;
115aace66b1SNishanth Menon 	u8 num_valid_queues;
116aace66b1SNishanth Menon 	struct ti_queue_inst *qinsts;
117aace66b1SNishanth Menon 	struct mbox_controller mbox;
118aace66b1SNishanth Menon 	struct mbox_chan *chans;
119aace66b1SNishanth Menon };
120aace66b1SNishanth Menon 
121aace66b1SNishanth Menon /**
122aace66b1SNishanth Menon  * ti_msgmgr_queue_get_num_messages() - Get the number of pending messages
123aace66b1SNishanth Menon  * @qinst:	Queue instance for which we check the number of pending messages
124aace66b1SNishanth Menon  *
125aace66b1SNishanth Menon  * Return: number of messages pending in the queue (0 == no pending messages)
126aace66b1SNishanth Menon  */
127aace66b1SNishanth Menon static inline int ti_msgmgr_queue_get_num_messages(struct ti_queue_inst *qinst)
128aace66b1SNishanth Menon {
129aace66b1SNishanth Menon 	u32 val;
130aace66b1SNishanth Menon 
131aace66b1SNishanth Menon 	/*
132aace66b1SNishanth Menon 	 * We cannot use relaxed operation here - update may happen
133aace66b1SNishanth Menon 	 * real-time.
134aace66b1SNishanth Menon 	 */
135aace66b1SNishanth Menon 	val = readl(qinst->queue_state) & Q_STATE_ENTRY_COUNT_MASK;
136aace66b1SNishanth Menon 	val >>= __ffs(Q_STATE_ENTRY_COUNT_MASK);
137aace66b1SNishanth Menon 
138aace66b1SNishanth Menon 	return val;
139aace66b1SNishanth Menon }
140aace66b1SNishanth Menon 
141aace66b1SNishanth Menon /**
142aace66b1SNishanth Menon  * ti_msgmgr_queue_rx_interrupt() - Interrupt handler for receive Queue
143aace66b1SNishanth Menon  * @irq:	Interrupt number
144aace66b1SNishanth Menon  * @p:		Channel Pointer
145aace66b1SNishanth Menon  *
146aace66b1SNishanth Menon  * Return: -EINVAL if there is no instance
147aace66b1SNishanth Menon  * IRQ_NONE if the interrupt is not ours.
148aace66b1SNishanth Menon  * IRQ_HANDLED if the rx interrupt was successfully handled.
149aace66b1SNishanth Menon  */
150aace66b1SNishanth Menon static irqreturn_t ti_msgmgr_queue_rx_interrupt(int irq, void *p)
151aace66b1SNishanth Menon {
152aace66b1SNishanth Menon 	struct mbox_chan *chan = p;
153aace66b1SNishanth Menon 	struct device *dev = chan->mbox->dev;
154aace66b1SNishanth Menon 	struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
155aace66b1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
156aace66b1SNishanth Menon 	const struct ti_msgmgr_desc *desc;
157aace66b1SNishanth Menon 	int msg_count, num_words;
158aace66b1SNishanth Menon 	struct ti_msgmgr_message message;
159aace66b1SNishanth Menon 	void __iomem *data_reg;
160aace66b1SNishanth Menon 	u32 *word_data;
161aace66b1SNishanth Menon 
162aace66b1SNishanth Menon 	if (WARN_ON(!inst)) {
163aace66b1SNishanth Menon 		dev_err(dev, "no platform drv data??\n");
164aace66b1SNishanth Menon 		return -EINVAL;
165aace66b1SNishanth Menon 	}
166aace66b1SNishanth Menon 
167aace66b1SNishanth Menon 	/* Do I have an invalid interrupt source? */
168aace66b1SNishanth Menon 	if (qinst->is_tx) {
169aace66b1SNishanth Menon 		dev_err(dev, "Cannot handle rx interrupt on tx channel %s\n",
170aace66b1SNishanth Menon 			qinst->name);
171aace66b1SNishanth Menon 		return IRQ_NONE;
172aace66b1SNishanth Menon 	}
173aace66b1SNishanth Menon 
174aace66b1SNishanth Menon 	/* Do I actually have messages to read? */
175aace66b1SNishanth Menon 	msg_count = ti_msgmgr_queue_get_num_messages(qinst);
176aace66b1SNishanth Menon 	if (!msg_count) {
177aace66b1SNishanth Menon 		/* Shared IRQ? */
178aace66b1SNishanth Menon 		dev_dbg(dev, "Spurious event - 0 pending data!\n");
179aace66b1SNishanth Menon 		return IRQ_NONE;
180aace66b1SNishanth Menon 	}
181aace66b1SNishanth Menon 
182aace66b1SNishanth Menon 	/*
183aace66b1SNishanth Menon 	 * I have no idea about the protocol being used to communicate with the
184aace66b1SNishanth Menon 	 * remote producer - 0 could be valid data, so I wont make a judgement
185aace66b1SNishanth Menon 	 * of how many bytes I should be reading. Let the client figure this
186aace66b1SNishanth Menon 	 * out.. I just read the full message and pass it on..
187aace66b1SNishanth Menon 	 */
188aace66b1SNishanth Menon 	desc = inst->desc;
189aace66b1SNishanth Menon 	message.len = desc->max_message_size;
190aace66b1SNishanth Menon 	message.buf = (u8 *)qinst->rx_buff;
191aace66b1SNishanth Menon 
192aace66b1SNishanth Menon 	/*
193aace66b1SNishanth Menon 	 * NOTE about register access involved here:
194aace66b1SNishanth Menon 	 * the hardware block is implemented with 32bit access operations and no
195aace66b1SNishanth Menon 	 * support for data splitting.  We don't want the hardware to misbehave
196aace66b1SNishanth Menon 	 * with sub 32bit access - For example: if the last register read is
197aace66b1SNishanth Menon 	 * split into byte wise access, it can result in the queue getting
198aace66b1SNishanth Menon 	 * stuck or indeterminate behavior. An out of order read operation may
199aace66b1SNishanth Menon 	 * result in weird data results as well.
200aace66b1SNishanth Menon 	 * Hence, we do not use memcpy_fromio or __ioread32_copy here, instead
201aace66b1SNishanth Menon 	 * we depend on readl for the purpose.
202aace66b1SNishanth Menon 	 *
203aace66b1SNishanth Menon 	 * Also note that the final register read automatically marks the
204aace66b1SNishanth Menon 	 * queue message as read.
205aace66b1SNishanth Menon 	 */
206aace66b1SNishanth Menon 	for (data_reg = qinst->queue_buff_start, word_data = qinst->rx_buff,
207aace66b1SNishanth Menon 	     num_words = (desc->max_message_size / sizeof(u32));
208aace66b1SNishanth Menon 	     num_words; num_words--, data_reg += sizeof(u32), word_data++)
209aace66b1SNishanth Menon 		*word_data = readl(data_reg);
210aace66b1SNishanth Menon 
211aace66b1SNishanth Menon 	/*
212aace66b1SNishanth Menon 	 * Last register read automatically clears the IRQ if only 1 message
213aace66b1SNishanth Menon 	 * is pending - so send the data up the stack..
214aace66b1SNishanth Menon 	 * NOTE: Client is expected to be as optimal as possible, since
215aace66b1SNishanth Menon 	 * we invoke the handler in IRQ context.
216aace66b1SNishanth Menon 	 */
217aace66b1SNishanth Menon 	mbox_chan_received_data(chan, (void *)&message);
218aace66b1SNishanth Menon 
219aace66b1SNishanth Menon 	return IRQ_HANDLED;
220aace66b1SNishanth Menon }
221aace66b1SNishanth Menon 
222aace66b1SNishanth Menon /**
223aace66b1SNishanth Menon  * ti_msgmgr_queue_peek_data() - Peek to see if there are any rx messages.
224aace66b1SNishanth Menon  * @chan:	Channel Pointer
225aace66b1SNishanth Menon  *
226aace66b1SNishanth Menon  * Return: 'true' if there is pending rx data, 'false' if there is none.
227aace66b1SNishanth Menon  */
228aace66b1SNishanth Menon static bool ti_msgmgr_queue_peek_data(struct mbox_chan *chan)
229aace66b1SNishanth Menon {
230aace66b1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
231aace66b1SNishanth Menon 	int msg_count;
232aace66b1SNishanth Menon 
233aace66b1SNishanth Menon 	if (qinst->is_tx)
234aace66b1SNishanth Menon 		return false;
235aace66b1SNishanth Menon 
236aace66b1SNishanth Menon 	msg_count = ti_msgmgr_queue_get_num_messages(qinst);
237aace66b1SNishanth Menon 
238aace66b1SNishanth Menon 	return msg_count ? true : false;
239aace66b1SNishanth Menon }
240aace66b1SNishanth Menon 
241aace66b1SNishanth Menon /**
242aace66b1SNishanth Menon  * ti_msgmgr_last_tx_done() - See if all the tx messages are sent
243aace66b1SNishanth Menon  * @chan:	Channel pointer
244aace66b1SNishanth Menon  *
245aace66b1SNishanth Menon  * Return: 'true' is no pending tx data, 'false' if there are any.
246aace66b1SNishanth Menon  */
247aace66b1SNishanth Menon static bool ti_msgmgr_last_tx_done(struct mbox_chan *chan)
248aace66b1SNishanth Menon {
249aace66b1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
250aace66b1SNishanth Menon 	int msg_count;
251aace66b1SNishanth Menon 
252aace66b1SNishanth Menon 	if (!qinst->is_tx)
253aace66b1SNishanth Menon 		return false;
254aace66b1SNishanth Menon 
255aace66b1SNishanth Menon 	msg_count = ti_msgmgr_queue_get_num_messages(qinst);
256aace66b1SNishanth Menon 
257aace66b1SNishanth Menon 	/* if we have any messages pending.. */
258aace66b1SNishanth Menon 	return msg_count ? false : true;
259aace66b1SNishanth Menon }
260aace66b1SNishanth Menon 
261aace66b1SNishanth Menon /**
262aace66b1SNishanth Menon  * ti_msgmgr_send_data() - Send data
263aace66b1SNishanth Menon  * @chan:	Channel Pointer
264aace66b1SNishanth Menon  * @data:	ti_msgmgr_message * Message Pointer
265aace66b1SNishanth Menon  *
266aace66b1SNishanth Menon  * Return: 0 if all goes good, else appropriate error messages.
267aace66b1SNishanth Menon  */
268aace66b1SNishanth Menon static int ti_msgmgr_send_data(struct mbox_chan *chan, void *data)
269aace66b1SNishanth Menon {
270aace66b1SNishanth Menon 	struct device *dev = chan->mbox->dev;
271aace66b1SNishanth Menon 	struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
272aace66b1SNishanth Menon 	const struct ti_msgmgr_desc *desc;
273aace66b1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
274aace66b1SNishanth Menon 	int num_words, trail_bytes;
275aace66b1SNishanth Menon 	struct ti_msgmgr_message *message = data;
276aace66b1SNishanth Menon 	void __iomem *data_reg;
277aace66b1SNishanth Menon 	u32 *word_data;
278aace66b1SNishanth Menon 
279aace66b1SNishanth Menon 	if (WARN_ON(!inst)) {
280aace66b1SNishanth Menon 		dev_err(dev, "no platform drv data??\n");
281aace66b1SNishanth Menon 		return -EINVAL;
282aace66b1SNishanth Menon 	}
283aace66b1SNishanth Menon 	desc = inst->desc;
284aace66b1SNishanth Menon 
285aace66b1SNishanth Menon 	if (desc->max_message_size < message->len) {
286*ca64af43SNishanth Menon 		dev_err(dev, "Queue %s message length %zu > max %d\n",
287aace66b1SNishanth Menon 			qinst->name, message->len, desc->max_message_size);
288aace66b1SNishanth Menon 		return -EINVAL;
289aace66b1SNishanth Menon 	}
290aace66b1SNishanth Menon 
291aace66b1SNishanth Menon 	/* NOTE: Constraints similar to rx path exists here as well */
292aace66b1SNishanth Menon 	for (data_reg = qinst->queue_buff_start,
293aace66b1SNishanth Menon 	     num_words = message->len / sizeof(u32),
294aace66b1SNishanth Menon 	     word_data = (u32 *)message->buf;
295aace66b1SNishanth Menon 	     num_words; num_words--, data_reg += sizeof(u32), word_data++)
296aace66b1SNishanth Menon 		writel(*word_data, data_reg);
297aace66b1SNishanth Menon 
298aace66b1SNishanth Menon 	trail_bytes = message->len % sizeof(u32);
299aace66b1SNishanth Menon 	if (trail_bytes) {
300aace66b1SNishanth Menon 		u32 data_trail = *word_data;
301aace66b1SNishanth Menon 
302aace66b1SNishanth Menon 		/* Ensure all unused data is 0 */
303aace66b1SNishanth Menon 		data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
304aace66b1SNishanth Menon 		writel(data_trail, data_reg);
305aace66b1SNishanth Menon 		data_reg++;
306aace66b1SNishanth Menon 	}
307aace66b1SNishanth Menon 	/*
308aace66b1SNishanth Menon 	 * 'data_reg' indicates next register to write. If we did not already
309aace66b1SNishanth Menon 	 * write on tx complete reg(last reg), we must do so for transmit
310aace66b1SNishanth Menon 	 */
311aace66b1SNishanth Menon 	if (data_reg <= qinst->queue_buff_end)
312aace66b1SNishanth Menon 		writel(0, qinst->queue_buff_end);
313aace66b1SNishanth Menon 
314aace66b1SNishanth Menon 	return 0;
315aace66b1SNishanth Menon }
316aace66b1SNishanth Menon 
317aace66b1SNishanth Menon /**
318aace66b1SNishanth Menon  * ti_msgmgr_queue_startup() - Startup queue
319aace66b1SNishanth Menon  * @chan:	Channel pointer
320aace66b1SNishanth Menon  *
321aace66b1SNishanth Menon  * Return: 0 if all goes good, else return corresponding error message
322aace66b1SNishanth Menon  */
323aace66b1SNishanth Menon static int ti_msgmgr_queue_startup(struct mbox_chan *chan)
324aace66b1SNishanth Menon {
325aace66b1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
326aace66b1SNishanth Menon 	struct device *dev = chan->mbox->dev;
327aace66b1SNishanth Menon 	int ret;
328aace66b1SNishanth Menon 
329aace66b1SNishanth Menon 	if (!qinst->is_tx) {
330aace66b1SNishanth Menon 		/*
331aace66b1SNishanth Menon 		 * With the expectation that the IRQ might be shared in SoC
332aace66b1SNishanth Menon 		 */
333aace66b1SNishanth Menon 		ret = request_irq(qinst->irq, ti_msgmgr_queue_rx_interrupt,
334aace66b1SNishanth Menon 				  IRQF_SHARED, qinst->name, chan);
335aace66b1SNishanth Menon 		if (ret) {
336aace66b1SNishanth Menon 			dev_err(dev, "Unable to get IRQ %d on %s(res=%d)\n",
337aace66b1SNishanth Menon 				qinst->irq, qinst->name, ret);
338aace66b1SNishanth Menon 			return ret;
339aace66b1SNishanth Menon 		}
340aace66b1SNishanth Menon 	}
341aace66b1SNishanth Menon 
342aace66b1SNishanth Menon 	return 0;
343aace66b1SNishanth Menon }
344aace66b1SNishanth Menon 
345aace66b1SNishanth Menon /**
346aace66b1SNishanth Menon  * ti_msgmgr_queue_shutdown() - Shutdown the queue
347aace66b1SNishanth Menon  * @chan:	Channel pointer
348aace66b1SNishanth Menon  */
349aace66b1SNishanth Menon static void ti_msgmgr_queue_shutdown(struct mbox_chan *chan)
350aace66b1SNishanth Menon {
351aace66b1SNishanth Menon 	struct ti_queue_inst *qinst = chan->con_priv;
352aace66b1SNishanth Menon 
353aace66b1SNishanth Menon 	if (!qinst->is_tx)
354aace66b1SNishanth Menon 		free_irq(qinst->irq, chan);
355aace66b1SNishanth Menon }
356aace66b1SNishanth Menon 
357aace66b1SNishanth Menon /**
358aace66b1SNishanth Menon  * ti_msgmgr_of_xlate() - Translation of phandle to queue
359aace66b1SNishanth Menon  * @mbox:	Mailbox controller
360aace66b1SNishanth Menon  * @p:		phandle pointer
361aace66b1SNishanth Menon  *
362aace66b1SNishanth Menon  * Return: Mailbox channel corresponding to the queue, else return error
363aace66b1SNishanth Menon  * pointer.
364aace66b1SNishanth Menon  */
365aace66b1SNishanth Menon static struct mbox_chan *ti_msgmgr_of_xlate(struct mbox_controller *mbox,
366aace66b1SNishanth Menon 					    const struct of_phandle_args *p)
367aace66b1SNishanth Menon {
368aace66b1SNishanth Menon 	struct ti_msgmgr_inst *inst;
369aace66b1SNishanth Menon 	int req_qid, req_pid;
370aace66b1SNishanth Menon 	struct ti_queue_inst *qinst;
371aace66b1SNishanth Menon 	int i;
372aace66b1SNishanth Menon 
373aace66b1SNishanth Menon 	inst = container_of(mbox, struct ti_msgmgr_inst, mbox);
374aace66b1SNishanth Menon 	if (WARN_ON(!inst))
375aace66b1SNishanth Menon 		return ERR_PTR(-EINVAL);
376aace66b1SNishanth Menon 
377aace66b1SNishanth Menon 	/* #mbox-cells is 2 */
378aace66b1SNishanth Menon 	if (p->args_count != 2) {
379aace66b1SNishanth Menon 		dev_err(inst->dev, "Invalid arguments in dt[%d] instead of 2\n",
380aace66b1SNishanth Menon 			p->args_count);
381aace66b1SNishanth Menon 		return ERR_PTR(-EINVAL);
382aace66b1SNishanth Menon 	}
383aace66b1SNishanth Menon 	req_qid = p->args[0];
384aace66b1SNishanth Menon 	req_pid = p->args[1];
385aace66b1SNishanth Menon 
386aace66b1SNishanth Menon 	for (qinst = inst->qinsts, i = 0; i < inst->num_valid_queues;
387aace66b1SNishanth Menon 	     i++, qinst++) {
388aace66b1SNishanth Menon 		if (req_qid == qinst->queue_id && req_pid == qinst->proxy_id)
389aace66b1SNishanth Menon 			return qinst->chan;
390aace66b1SNishanth Menon 	}
391aace66b1SNishanth Menon 
392aace66b1SNishanth Menon 	dev_err(inst->dev, "Queue ID %d, Proxy ID %d is wrong on %s\n",
393aace66b1SNishanth Menon 		req_qid, req_pid, p->np->name);
394aace66b1SNishanth Menon 	return ERR_PTR(-ENOENT);
395aace66b1SNishanth Menon }
396aace66b1SNishanth Menon 
397aace66b1SNishanth Menon /**
398aace66b1SNishanth Menon  * ti_msgmgr_queue_setup() - Setup data structures for each queue instance
399aace66b1SNishanth Menon  * @idx:	index of the queue
400aace66b1SNishanth Menon  * @dev:	pointer to the message manager device
401aace66b1SNishanth Menon  * @np:		pointer to the of node
402aace66b1SNishanth Menon  * @inst:	Queue instance pointer
403aace66b1SNishanth Menon  * @d:		Message Manager instance description data
404aace66b1SNishanth Menon  * @qd:		Queue description data
405aace66b1SNishanth Menon  * @qinst:	Queue instance pointer
406aace66b1SNishanth Menon  * @chan:	pointer to mailbox channel
407aace66b1SNishanth Menon  *
408aace66b1SNishanth Menon  * Return: 0 if all went well, else return corresponding error
409aace66b1SNishanth Menon  */
410aace66b1SNishanth Menon static int ti_msgmgr_queue_setup(int idx, struct device *dev,
411aace66b1SNishanth Menon 				 struct device_node *np,
412aace66b1SNishanth Menon 				 struct ti_msgmgr_inst *inst,
413aace66b1SNishanth Menon 				 const struct ti_msgmgr_desc *d,
414aace66b1SNishanth Menon 				 const struct ti_msgmgr_valid_queue_desc *qd,
415aace66b1SNishanth Menon 				 struct ti_queue_inst *qinst,
416aace66b1SNishanth Menon 				 struct mbox_chan *chan)
417aace66b1SNishanth Menon {
418aace66b1SNishanth Menon 	qinst->proxy_id = qd->proxy_id;
419aace66b1SNishanth Menon 	qinst->queue_id = qd->queue_id;
420aace66b1SNishanth Menon 
421aace66b1SNishanth Menon 	if (qinst->queue_id > d->queue_count) {
422aace66b1SNishanth Menon 		dev_err(dev, "Queue Data [idx=%d] queuid %d > %d\n",
423aace66b1SNishanth Menon 			idx, qinst->queue_id, d->queue_count);
424aace66b1SNishanth Menon 		return -ERANGE;
425aace66b1SNishanth Menon 	}
426aace66b1SNishanth Menon 
427aace66b1SNishanth Menon 	qinst->is_tx = qd->is_tx;
428aace66b1SNishanth Menon 	snprintf(qinst->name, sizeof(qinst->name), "%s %s_%03d_%03d",
429aace66b1SNishanth Menon 		 dev_name(dev), qinst->is_tx ? "tx" : "rx", qinst->queue_id,
430aace66b1SNishanth Menon 		 qinst->proxy_id);
431aace66b1SNishanth Menon 
432aace66b1SNishanth Menon 	if (!qinst->is_tx) {
433aace66b1SNishanth Menon 		char of_rx_irq_name[7];
434aace66b1SNishanth Menon 
435aace66b1SNishanth Menon 		snprintf(of_rx_irq_name, sizeof(of_rx_irq_name),
436aace66b1SNishanth Menon 			 "rx_%03d", qinst->queue_id);
437aace66b1SNishanth Menon 
438aace66b1SNishanth Menon 		qinst->irq = of_irq_get_byname(np, of_rx_irq_name);
439aace66b1SNishanth Menon 		if (qinst->irq < 0) {
440aace66b1SNishanth Menon 			dev_crit(dev,
441aace66b1SNishanth Menon 				 "[%d]QID %d PID %d:No IRQ[%s]: %d\n",
442aace66b1SNishanth Menon 				 idx, qinst->queue_id, qinst->proxy_id,
443aace66b1SNishanth Menon 				 of_rx_irq_name, qinst->irq);
444aace66b1SNishanth Menon 			return qinst->irq;
445aace66b1SNishanth Menon 		}
446aace66b1SNishanth Menon 		/* Allocate usage buffer for rx */
447aace66b1SNishanth Menon 		qinst->rx_buff = devm_kzalloc(dev,
448aace66b1SNishanth Menon 					      d->max_message_size, GFP_KERNEL);
449aace66b1SNishanth Menon 		if (!qinst->rx_buff)
450aace66b1SNishanth Menon 			return -ENOMEM;
451aace66b1SNishanth Menon 	}
452aace66b1SNishanth Menon 
453aace66b1SNishanth Menon 	qinst->queue_buff_start = inst->queue_proxy_region +
454aace66b1SNishanth Menon 	    Q_DATA_OFFSET(qinst->proxy_id, qinst->queue_id, d->data_first_reg);
455aace66b1SNishanth Menon 	qinst->queue_buff_end = inst->queue_proxy_region +
456aace66b1SNishanth Menon 	    Q_DATA_OFFSET(qinst->proxy_id, qinst->queue_id, d->data_last_reg);
457aace66b1SNishanth Menon 	qinst->queue_state = inst->queue_state_debug_region +
458aace66b1SNishanth Menon 	    Q_STATE_OFFSET(qinst->queue_id);
459aace66b1SNishanth Menon 	qinst->chan = chan;
460aace66b1SNishanth Menon 
461aace66b1SNishanth Menon 	chan->con_priv = qinst;
462aace66b1SNishanth Menon 
463aace66b1SNishanth Menon 	dev_dbg(dev, "[%d] qidx=%d pidx=%d irq=%d q_s=%p q_e = %p\n",
464aace66b1SNishanth Menon 		idx, qinst->queue_id, qinst->proxy_id, qinst->irq,
465aace66b1SNishanth Menon 		qinst->queue_buff_start, qinst->queue_buff_end);
466aace66b1SNishanth Menon 	return 0;
467aace66b1SNishanth Menon }
468aace66b1SNishanth Menon 
469aace66b1SNishanth Menon /* Queue operations */
470aace66b1SNishanth Menon static const struct mbox_chan_ops ti_msgmgr_chan_ops = {
471aace66b1SNishanth Menon 	.startup = ti_msgmgr_queue_startup,
472aace66b1SNishanth Menon 	.shutdown = ti_msgmgr_queue_shutdown,
473aace66b1SNishanth Menon 	.peek_data = ti_msgmgr_queue_peek_data,
474aace66b1SNishanth Menon 	.last_tx_done = ti_msgmgr_last_tx_done,
475aace66b1SNishanth Menon 	.send_data = ti_msgmgr_send_data,
476aace66b1SNishanth Menon };
477aace66b1SNishanth Menon 
478aace66b1SNishanth Menon /* Keystone K2G SoC integration details */
479aace66b1SNishanth Menon static const struct ti_msgmgr_valid_queue_desc k2g_valid_queues[] = {
480aace66b1SNishanth Menon 	{.queue_id = 0, .proxy_id = 0, .is_tx = true,},
481aace66b1SNishanth Menon 	{.queue_id = 1, .proxy_id = 0, .is_tx = true,},
482aace66b1SNishanth Menon 	{.queue_id = 2, .proxy_id = 0, .is_tx = true,},
483aace66b1SNishanth Menon 	{.queue_id = 3, .proxy_id = 0, .is_tx = true,},
484aace66b1SNishanth Menon 	{.queue_id = 5, .proxy_id = 2, .is_tx = false,},
485aace66b1SNishanth Menon 	{.queue_id = 56, .proxy_id = 1, .is_tx = true,},
486aace66b1SNishanth Menon 	{.queue_id = 57, .proxy_id = 2, .is_tx = false,},
487aace66b1SNishanth Menon 	{.queue_id = 58, .proxy_id = 3, .is_tx = true,},
488aace66b1SNishanth Menon 	{.queue_id = 59, .proxy_id = 4, .is_tx = true,},
489aace66b1SNishanth Menon 	{.queue_id = 60, .proxy_id = 5, .is_tx = true,},
490aace66b1SNishanth Menon 	{.queue_id = 61, .proxy_id = 6, .is_tx = true,},
491aace66b1SNishanth Menon };
492aace66b1SNishanth Menon 
493aace66b1SNishanth Menon static const struct ti_msgmgr_desc k2g_desc = {
494aace66b1SNishanth Menon 	.queue_count = 64,
495aace66b1SNishanth Menon 	.max_message_size = 64,
496aace66b1SNishanth Menon 	.max_messages = 128,
497aace66b1SNishanth Menon 	.q_slices = 1,
498aace66b1SNishanth Menon 	.q_proxies = 1,
499aace66b1SNishanth Menon 	.data_first_reg = 16,
500aace66b1SNishanth Menon 	.data_last_reg = 31,
501aace66b1SNishanth Menon 	.tx_polled = false,
502aace66b1SNishanth Menon 	.valid_queues = k2g_valid_queues,
503aace66b1SNishanth Menon 	.num_valid_queues = ARRAY_SIZE(k2g_valid_queues),
504aace66b1SNishanth Menon };
505aace66b1SNishanth Menon 
506aace66b1SNishanth Menon static const struct of_device_id ti_msgmgr_of_match[] = {
507aace66b1SNishanth Menon 	{.compatible = "ti,k2g-message-manager", .data = &k2g_desc},
508aace66b1SNishanth Menon 	{ /* Sentinel */ }
509aace66b1SNishanth Menon };
510aace66b1SNishanth Menon MODULE_DEVICE_TABLE(of, ti_msgmgr_of_match);
511aace66b1SNishanth Menon 
512aace66b1SNishanth Menon static int ti_msgmgr_probe(struct platform_device *pdev)
513aace66b1SNishanth Menon {
514aace66b1SNishanth Menon 	struct device *dev = &pdev->dev;
515aace66b1SNishanth Menon 	const struct of_device_id *of_id;
516aace66b1SNishanth Menon 	struct device_node *np;
517aace66b1SNishanth Menon 	struct resource *res;
518aace66b1SNishanth Menon 	const struct ti_msgmgr_desc *desc;
519aace66b1SNishanth Menon 	struct ti_msgmgr_inst *inst;
520aace66b1SNishanth Menon 	struct ti_queue_inst *qinst;
521aace66b1SNishanth Menon 	struct mbox_controller *mbox;
522aace66b1SNishanth Menon 	struct mbox_chan *chans;
523aace66b1SNishanth Menon 	int queue_count;
524aace66b1SNishanth Menon 	int i;
525aace66b1SNishanth Menon 	int ret = -EINVAL;
526aace66b1SNishanth Menon 	const struct ti_msgmgr_valid_queue_desc *queue_desc;
527aace66b1SNishanth Menon 
528aace66b1SNishanth Menon 	if (!dev->of_node) {
529aace66b1SNishanth Menon 		dev_err(dev, "no OF information\n");
530aace66b1SNishanth Menon 		return -EINVAL;
531aace66b1SNishanth Menon 	}
532aace66b1SNishanth Menon 	np = dev->of_node;
533aace66b1SNishanth Menon 
534aace66b1SNishanth Menon 	of_id = of_match_device(ti_msgmgr_of_match, dev);
535aace66b1SNishanth Menon 	if (!of_id) {
536aace66b1SNishanth Menon 		dev_err(dev, "OF data missing\n");
537aace66b1SNishanth Menon 		return -EINVAL;
538aace66b1SNishanth Menon 	}
539aace66b1SNishanth Menon 	desc = of_id->data;
540aace66b1SNishanth Menon 
541aace66b1SNishanth Menon 	inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
542aace66b1SNishanth Menon 	if (!inst)
543aace66b1SNishanth Menon 		return -ENOMEM;
544aace66b1SNishanth Menon 
545aace66b1SNishanth Menon 	inst->dev = dev;
546aace66b1SNishanth Menon 	inst->desc = desc;
547aace66b1SNishanth Menon 
548aace66b1SNishanth Menon 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
549aace66b1SNishanth Menon 					   "queue_proxy_region");
550aace66b1SNishanth Menon 	inst->queue_proxy_region = devm_ioremap_resource(dev, res);
551aace66b1SNishanth Menon 	if (IS_ERR(inst->queue_proxy_region))
552aace66b1SNishanth Menon 		return PTR_ERR(inst->queue_proxy_region);
553aace66b1SNishanth Menon 
554aace66b1SNishanth Menon 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
555aace66b1SNishanth Menon 					   "queue_state_debug_region");
556aace66b1SNishanth Menon 	inst->queue_state_debug_region = devm_ioremap_resource(dev, res);
557aace66b1SNishanth Menon 	if (IS_ERR(inst->queue_state_debug_region))
558aace66b1SNishanth Menon 		return PTR_ERR(inst->queue_state_debug_region);
559aace66b1SNishanth Menon 
560aace66b1SNishanth Menon 	dev_dbg(dev, "proxy region=%p, queue_state=%p\n",
561aace66b1SNishanth Menon 		inst->queue_proxy_region, inst->queue_state_debug_region);
562aace66b1SNishanth Menon 
563aace66b1SNishanth Menon 	queue_count = desc->num_valid_queues;
564aace66b1SNishanth Menon 	if (!queue_count || queue_count > desc->queue_count) {
565aace66b1SNishanth Menon 		dev_crit(dev, "Invalid Number of queues %d. Max %d\n",
566aace66b1SNishanth Menon 			 queue_count, desc->queue_count);
567aace66b1SNishanth Menon 		return -ERANGE;
568aace66b1SNishanth Menon 	}
569aace66b1SNishanth Menon 	inst->num_valid_queues = queue_count;
570aace66b1SNishanth Menon 
571aace66b1SNishanth Menon 	qinst = devm_kzalloc(dev, sizeof(*qinst) * queue_count, GFP_KERNEL);
572aace66b1SNishanth Menon 	if (!qinst)
573aace66b1SNishanth Menon 		return -ENOMEM;
574aace66b1SNishanth Menon 	inst->qinsts = qinst;
575aace66b1SNishanth Menon 
576aace66b1SNishanth Menon 	chans = devm_kzalloc(dev, sizeof(*chans) * queue_count, GFP_KERNEL);
577aace66b1SNishanth Menon 	if (!chans)
578aace66b1SNishanth Menon 		return -ENOMEM;
579aace66b1SNishanth Menon 	inst->chans = chans;
580aace66b1SNishanth Menon 
581aace66b1SNishanth Menon 	for (i = 0, queue_desc = desc->valid_queues;
582aace66b1SNishanth Menon 	     i < queue_count; i++, qinst++, chans++, queue_desc++) {
583aace66b1SNishanth Menon 		ret = ti_msgmgr_queue_setup(i, dev, np, inst,
584aace66b1SNishanth Menon 					    desc, queue_desc, qinst, chans);
585aace66b1SNishanth Menon 		if (ret)
586aace66b1SNishanth Menon 			return ret;
587aace66b1SNishanth Menon 	}
588aace66b1SNishanth Menon 
589aace66b1SNishanth Menon 	mbox = &inst->mbox;
590aace66b1SNishanth Menon 	mbox->dev = dev;
591aace66b1SNishanth Menon 	mbox->ops = &ti_msgmgr_chan_ops;
592aace66b1SNishanth Menon 	mbox->chans = inst->chans;
593aace66b1SNishanth Menon 	mbox->num_chans = inst->num_valid_queues;
594aace66b1SNishanth Menon 	mbox->txdone_irq = false;
595aace66b1SNishanth Menon 	mbox->txdone_poll = desc->tx_polled;
596aace66b1SNishanth Menon 	if (desc->tx_polled)
597aace66b1SNishanth Menon 		mbox->txpoll_period = desc->tx_poll_timeout_ms;
598aace66b1SNishanth Menon 	mbox->of_xlate = ti_msgmgr_of_xlate;
599aace66b1SNishanth Menon 
600aace66b1SNishanth Menon 	platform_set_drvdata(pdev, inst);
601aace66b1SNishanth Menon 	ret = mbox_controller_register(mbox);
602aace66b1SNishanth Menon 	if (ret)
603aace66b1SNishanth Menon 		dev_err(dev, "Failed to register mbox_controller(%d)\n", ret);
604aace66b1SNishanth Menon 
605aace66b1SNishanth Menon 	return ret;
606aace66b1SNishanth Menon }
607aace66b1SNishanth Menon 
608aace66b1SNishanth Menon static int ti_msgmgr_remove(struct platform_device *pdev)
609aace66b1SNishanth Menon {
610aace66b1SNishanth Menon 	struct ti_msgmgr_inst *inst;
611aace66b1SNishanth Menon 
612aace66b1SNishanth Menon 	inst = platform_get_drvdata(pdev);
613aace66b1SNishanth Menon 	mbox_controller_unregister(&inst->mbox);
614aace66b1SNishanth Menon 
615aace66b1SNishanth Menon 	return 0;
616aace66b1SNishanth Menon }
617aace66b1SNishanth Menon 
618aace66b1SNishanth Menon static struct platform_driver ti_msgmgr_driver = {
619aace66b1SNishanth Menon 	.probe = ti_msgmgr_probe,
620aace66b1SNishanth Menon 	.remove = ti_msgmgr_remove,
621aace66b1SNishanth Menon 	.driver = {
622aace66b1SNishanth Menon 		   .name = "ti-msgmgr",
623aace66b1SNishanth Menon 		   .of_match_table = of_match_ptr(ti_msgmgr_of_match),
624aace66b1SNishanth Menon 	},
625aace66b1SNishanth Menon };
626aace66b1SNishanth Menon module_platform_driver(ti_msgmgr_driver);
627aace66b1SNishanth Menon 
628aace66b1SNishanth Menon MODULE_LICENSE("GPL v2");
629aace66b1SNishanth Menon MODULE_DESCRIPTION("TI message manager driver");
630aace66b1SNishanth Menon MODULE_AUTHOR("Nishanth Menon");
631aace66b1SNishanth Menon MODULE_ALIAS("platform:ti-msgmgr");
632