xref: /openbmc/linux/drivers/mailbox/arm_mhuv2.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
15a6338ccSViresh Kumar // SPDX-License-Identifier: GPL-2.0
25a6338ccSViresh Kumar /*
35a6338ccSViresh Kumar  * ARM Message Handling Unit Version 2 (MHUv2) driver.
45a6338ccSViresh Kumar  *
55a6338ccSViresh Kumar  * Copyright (C) 2020 ARM Ltd.
65a6338ccSViresh Kumar  * Copyright (C) 2020 Linaro Ltd.
75a6338ccSViresh Kumar  *
85a6338ccSViresh Kumar  * An MHUv2 mailbox controller can provide up to 124 channel windows (each 32
95a6338ccSViresh Kumar  * bit long) and the driver allows any combination of both the transport
105a6338ccSViresh Kumar  * protocol modes: data-transfer and doorbell, to be used on those channel
115a6338ccSViresh Kumar  * windows.
125a6338ccSViresh Kumar  *
135a6338ccSViresh Kumar  * The transport protocols should be specified in the device tree entry for the
145a6338ccSViresh Kumar  * device. The transport protocols determine how the underlying hardware
155a6338ccSViresh Kumar  * resources of the device are utilized when transmitting data. Refer to the
165a6338ccSViresh Kumar  * device tree bindings of the ARM MHUv2 controller for more details.
175a6338ccSViresh Kumar  *
185a6338ccSViresh Kumar  * The number of registered mailbox channels is dependent on both the underlying
195a6338ccSViresh Kumar  * hardware - mainly the number of channel windows implemented by the platform,
205a6338ccSViresh Kumar  * as well as the selected transport protocols.
215a6338ccSViresh Kumar  *
225a6338ccSViresh Kumar  * The MHUv2 controller can work both as a sender and receiver, but the driver
235a6338ccSViresh Kumar  * and the DT bindings support unidirectional transfers for better allocation of
245a6338ccSViresh Kumar  * the channels. That is, this driver will be probed for two separate devices
255a6338ccSViresh Kumar  * for each mailbox controller, a sender device and a receiver device.
265a6338ccSViresh Kumar  */
275a6338ccSViresh Kumar 
285a6338ccSViresh Kumar #include <linux/amba/bus.h>
295a6338ccSViresh Kumar #include <linux/interrupt.h>
305a6338ccSViresh Kumar #include <linux/mailbox_controller.h>
315a6338ccSViresh Kumar #include <linux/mailbox/arm_mhuv2_message.h>
325a6338ccSViresh Kumar #include <linux/module.h>
335a6338ccSViresh Kumar #include <linux/of_address.h>
345a6338ccSViresh Kumar #include <linux/spinlock.h>
355a6338ccSViresh Kumar 
365a6338ccSViresh Kumar /* ====== MHUv2 Registers ====== */
375a6338ccSViresh Kumar 
385a6338ccSViresh Kumar /* Maximum number of channel windows */
395a6338ccSViresh Kumar #define MHUV2_CH_WN_MAX			124
405a6338ccSViresh Kumar /* Number of combined interrupt status registers */
415a6338ccSViresh Kumar #define MHUV2_CMB_INT_ST_REG_CNT	4
425a6338ccSViresh Kumar #define MHUV2_STAT_BYTES		(sizeof(u32))
435a6338ccSViresh Kumar #define MHUV2_STAT_BITS			(MHUV2_STAT_BYTES * __CHAR_BIT__)
445a6338ccSViresh Kumar 
455a6338ccSViresh Kumar #define LSB_MASK(n)			((1 << (n * __CHAR_BIT__)) - 1)
465a6338ccSViresh Kumar #define MHUV2_PROTOCOL_PROP		"arm,mhuv2-protocols"
475a6338ccSViresh Kumar 
485a6338ccSViresh Kumar /* Register Message Handling Unit Configuration fields */
495a6338ccSViresh Kumar struct mhu_cfg_t {
505a6338ccSViresh Kumar 	u32 num_ch : 7;
515a6338ccSViresh Kumar 	u32 pad : 25;
525a6338ccSViresh Kumar } __packed;
535a6338ccSViresh Kumar 
545a6338ccSViresh Kumar /* register Interrupt Status fields */
555a6338ccSViresh Kumar struct int_st_t {
565a6338ccSViresh Kumar 	u32 nr2r : 1;
575a6338ccSViresh Kumar 	u32 r2nr : 1;
585a6338ccSViresh Kumar 	u32 pad : 30;
595a6338ccSViresh Kumar } __packed;
605a6338ccSViresh Kumar 
615a6338ccSViresh Kumar /* Register Interrupt Clear fields */
625a6338ccSViresh Kumar struct int_clr_t {
635a6338ccSViresh Kumar 	u32 nr2r : 1;
645a6338ccSViresh Kumar 	u32 r2nr : 1;
655a6338ccSViresh Kumar 	u32 pad : 30;
665a6338ccSViresh Kumar } __packed;
675a6338ccSViresh Kumar 
685a6338ccSViresh Kumar /* Register Interrupt Enable fields */
695a6338ccSViresh Kumar struct int_en_t {
705a6338ccSViresh Kumar 	u32 r2nr : 1;
715a6338ccSViresh Kumar 	u32 nr2r : 1;
725a6338ccSViresh Kumar 	u32 chcomb : 1;
735a6338ccSViresh Kumar 	u32 pad : 29;
745a6338ccSViresh Kumar } __packed;
755a6338ccSViresh Kumar 
765a6338ccSViresh Kumar /* Register Implementer Identification fields */
775a6338ccSViresh Kumar struct iidr_t {
785a6338ccSViresh Kumar 	u32 implementer : 12;
795a6338ccSViresh Kumar 	u32 revision : 4;
805a6338ccSViresh Kumar 	u32 variant : 4;
815a6338ccSViresh Kumar 	u32 product_id : 12;
825a6338ccSViresh Kumar } __packed;
835a6338ccSViresh Kumar 
845a6338ccSViresh Kumar /* Register Architecture Identification Register fields */
855a6338ccSViresh Kumar struct aidr_t {
865a6338ccSViresh Kumar 	u32 arch_minor_rev : 4;
875a6338ccSViresh Kumar 	u32 arch_major_rev : 4;
885a6338ccSViresh Kumar 	u32 pad : 24;
895a6338ccSViresh Kumar } __packed;
905a6338ccSViresh Kumar 
915a6338ccSViresh Kumar /* Sender Channel Window fields */
925a6338ccSViresh Kumar struct mhu2_send_ch_wn_reg {
935a6338ccSViresh Kumar 	u32 stat;
945a6338ccSViresh Kumar 	u8 pad1[0x0C - 0x04];
955a6338ccSViresh Kumar 	u32 stat_set;
965a6338ccSViresh Kumar 	u32 int_st;
975a6338ccSViresh Kumar 	u32 int_clr;
985a6338ccSViresh Kumar 	u32 int_en;
995a6338ccSViresh Kumar 	u8 pad2[0x20 - 0x1C];
1005a6338ccSViresh Kumar } __packed;
1015a6338ccSViresh Kumar 
1025a6338ccSViresh Kumar /* Sender frame register fields */
1035a6338ccSViresh Kumar struct mhu2_send_frame_reg {
1045a6338ccSViresh Kumar 	struct mhu2_send_ch_wn_reg ch_wn[MHUV2_CH_WN_MAX];
1055a6338ccSViresh Kumar 	struct mhu_cfg_t mhu_cfg;
1065a6338ccSViresh Kumar 	u32 resp_cfg;
1075a6338ccSViresh Kumar 	u32 access_request;
1085a6338ccSViresh Kumar 	u32 access_ready;
1095a6338ccSViresh Kumar 	struct int_st_t int_st;
1105a6338ccSViresh Kumar 	struct int_clr_t int_clr;
1115a6338ccSViresh Kumar 	struct int_en_t int_en;
1125a6338ccSViresh Kumar 	u32 reserved0;
1135a6338ccSViresh Kumar 	u32 chcomb_int_st[MHUV2_CMB_INT_ST_REG_CNT];
1145a6338ccSViresh Kumar 	u8 pad[0xFC8 - 0xFB0];
1155a6338ccSViresh Kumar 	struct iidr_t iidr;
1165a6338ccSViresh Kumar 	struct aidr_t aidr;
1175a6338ccSViresh Kumar } __packed;
1185a6338ccSViresh Kumar 
1195a6338ccSViresh Kumar /* Receiver Channel Window fields */
1205a6338ccSViresh Kumar struct mhu2_recv_ch_wn_reg {
1215a6338ccSViresh Kumar 	u32 stat;
1225a6338ccSViresh Kumar 	u32 stat_masked;
1235a6338ccSViresh Kumar 	u32 stat_clear;
1245a6338ccSViresh Kumar 	u8 reserved0[0x10 - 0x0C];
1255a6338ccSViresh Kumar 	u32 mask;
1265a6338ccSViresh Kumar 	u32 mask_set;
1275a6338ccSViresh Kumar 	u32 mask_clear;
1285a6338ccSViresh Kumar 	u8 pad[0x20 - 0x1C];
1295a6338ccSViresh Kumar } __packed;
1305a6338ccSViresh Kumar 
1315a6338ccSViresh Kumar /* Receiver frame register fields */
1325a6338ccSViresh Kumar struct mhu2_recv_frame_reg {
1335a6338ccSViresh Kumar 	struct mhu2_recv_ch_wn_reg ch_wn[MHUV2_CH_WN_MAX];
1345a6338ccSViresh Kumar 	struct mhu_cfg_t mhu_cfg;
1355a6338ccSViresh Kumar 	u8 reserved0[0xF90 - 0xF84];
1365a6338ccSViresh Kumar 	struct int_st_t int_st;
1375a6338ccSViresh Kumar 	struct int_clr_t int_clr;
1385a6338ccSViresh Kumar 	struct int_en_t int_en;
1395a6338ccSViresh Kumar 	u32 pad;
1405a6338ccSViresh Kumar 	u32 chcomb_int_st[MHUV2_CMB_INT_ST_REG_CNT];
1415a6338ccSViresh Kumar 	u8 reserved2[0xFC8 - 0xFB0];
1425a6338ccSViresh Kumar 	struct iidr_t iidr;
1435a6338ccSViresh Kumar 	struct aidr_t aidr;
1445a6338ccSViresh Kumar } __packed;
1455a6338ccSViresh Kumar 
1465a6338ccSViresh Kumar 
1475a6338ccSViresh Kumar /* ====== MHUv2 data structures ====== */
1485a6338ccSViresh Kumar 
1495a6338ccSViresh Kumar enum mhuv2_transport_protocol {
1505a6338ccSViresh Kumar 	DOORBELL = 0,
1515a6338ccSViresh Kumar 	DATA_TRANSFER = 1
1525a6338ccSViresh Kumar };
1535a6338ccSViresh Kumar 
1545a6338ccSViresh Kumar enum mhuv2_frame {
1555a6338ccSViresh Kumar 	RECEIVER_FRAME,
1565a6338ccSViresh Kumar 	SENDER_FRAME
1575a6338ccSViresh Kumar };
1585a6338ccSViresh Kumar 
1595a6338ccSViresh Kumar /**
1605a6338ccSViresh Kumar  * struct mhuv2 - MHUv2 mailbox controller data
1615a6338ccSViresh Kumar  *
1625a6338ccSViresh Kumar  * @mbox:	Mailbox controller belonging to the MHU frame.
163a022c7c9SKrzysztof Kozlowski  * @send:	Base address of the register mapping region.
164a022c7c9SKrzysztof Kozlowski  * @recv:	Base address of the register mapping region.
1655a6338ccSViresh Kumar  * @frame:	Frame type: RECEIVER_FRAME or SENDER_FRAME.
1665a6338ccSViresh Kumar  * @irq:	Interrupt.
1675a6338ccSViresh Kumar  * @windows:	Channel windows implemented by the platform.
1685a6338ccSViresh Kumar  * @minor:	Minor version of the controller.
1695a6338ccSViresh Kumar  * @length:	Length of the protocols array in bytes.
1705a6338ccSViresh Kumar  * @protocols:	Raw protocol information, derived from device tree.
1715a6338ccSViresh Kumar  * @doorbell_pending_lock: spinlock required for correct operation of Tx
1725a6338ccSViresh Kumar  *		interrupt for doorbells.
1735a6338ccSViresh Kumar  */
1745a6338ccSViresh Kumar struct mhuv2 {
1755a6338ccSViresh Kumar 	struct mbox_controller mbox;
1765a6338ccSViresh Kumar 	union {
1775a6338ccSViresh Kumar 		struct mhu2_send_frame_reg __iomem *send;
1785a6338ccSViresh Kumar 		struct mhu2_recv_frame_reg __iomem *recv;
1795a6338ccSViresh Kumar 	};
1805a6338ccSViresh Kumar 	enum mhuv2_frame frame;
1815a6338ccSViresh Kumar 	unsigned int irq;
1825a6338ccSViresh Kumar 	unsigned int windows;
1835a6338ccSViresh Kumar 	unsigned int minor;
1845a6338ccSViresh Kumar 	unsigned int length;
1855a6338ccSViresh Kumar 	u32 *protocols;
1865a6338ccSViresh Kumar 
1875a6338ccSViresh Kumar 	spinlock_t doorbell_pending_lock;
1885a6338ccSViresh Kumar };
1895a6338ccSViresh Kumar 
1905a6338ccSViresh Kumar #define mhu_from_mbox(_mbox) container_of(_mbox, struct mhuv2, mbox)
1915a6338ccSViresh Kumar 
1925a6338ccSViresh Kumar /**
1935a6338ccSViresh Kumar  * struct mhuv2_protocol_ops - MHUv2 operations
1945a6338ccSViresh Kumar  *
1955a6338ccSViresh Kumar  * Each transport protocol must provide an implementation of the operations
1965a6338ccSViresh Kumar  * provided here.
1975a6338ccSViresh Kumar  *
1985a6338ccSViresh Kumar  * @rx_startup: Startup callback for receiver.
1995a6338ccSViresh Kumar  * @rx_shutdown: Shutdown callback for receiver.
2005a6338ccSViresh Kumar  * @read_data: Reads and clears newly available data.
2015a6338ccSViresh Kumar  * @tx_startup: Startup callback for receiver.
2025a6338ccSViresh Kumar  * @tx_shutdown: Shutdown callback for receiver.
2035a6338ccSViresh Kumar  * @last_tx_done: Report back if the last tx is completed or not.
2045a6338ccSViresh Kumar  * @send_data: Send data to the receiver.
2055a6338ccSViresh Kumar  */
2065a6338ccSViresh Kumar struct mhuv2_protocol_ops {
2075a6338ccSViresh Kumar 	int (*rx_startup)(struct mhuv2 *mhu, struct mbox_chan *chan);
2085a6338ccSViresh Kumar 	void (*rx_shutdown)(struct mhuv2 *mhu, struct mbox_chan *chan);
2095a6338ccSViresh Kumar 	void *(*read_data)(struct mhuv2 *mhu, struct mbox_chan *chan);
2105a6338ccSViresh Kumar 
2115a6338ccSViresh Kumar 	void (*tx_startup)(struct mhuv2 *mhu, struct mbox_chan *chan);
2125a6338ccSViresh Kumar 	void (*tx_shutdown)(struct mhuv2 *mhu, struct mbox_chan *chan);
2135a6338ccSViresh Kumar 	int (*last_tx_done)(struct mhuv2 *mhu, struct mbox_chan *chan);
2145a6338ccSViresh Kumar 	int (*send_data)(struct mhuv2 *mhu, struct mbox_chan *chan, void *arg);
2155a6338ccSViresh Kumar };
2165a6338ccSViresh Kumar 
2175a6338ccSViresh Kumar /*
2185a6338ccSViresh Kumar  * MHUv2 mailbox channel's private information
2195a6338ccSViresh Kumar  *
2205a6338ccSViresh Kumar  * @ops:	protocol specific ops for the channel.
2215a6338ccSViresh Kumar  * @ch_wn_idx:	Channel window index allocated to the channel.
2225a6338ccSViresh Kumar  * @windows:	Total number of windows consumed by the channel, only relevant
2235a6338ccSViresh Kumar  *		in DATA_TRANSFER protocol.
2245a6338ccSViresh Kumar  * @doorbell:	Doorbell bit number within the ch_wn_idx window, only relevant
2255a6338ccSViresh Kumar  *		in DOORBELL protocol.
2265a6338ccSViresh Kumar  * @pending:	Flag indicating pending doorbell interrupt, only relevant in
2275a6338ccSViresh Kumar  *		DOORBELL protocol.
2285a6338ccSViresh Kumar  */
2295a6338ccSViresh Kumar struct mhuv2_mbox_chan_priv {
2305a6338ccSViresh Kumar 	const struct mhuv2_protocol_ops *ops;
2315a6338ccSViresh Kumar 	u32 ch_wn_idx;
2325a6338ccSViresh Kumar 	union {
2335a6338ccSViresh Kumar 		u32 windows;
2345a6338ccSViresh Kumar 		struct {
2355a6338ccSViresh Kumar 			u32 doorbell;
2365a6338ccSViresh Kumar 			u32 pending;
2375a6338ccSViresh Kumar 		};
2385a6338ccSViresh Kumar 	};
2395a6338ccSViresh Kumar };
2405a6338ccSViresh Kumar 
2415a6338ccSViresh Kumar /* Macro for reading a bitfield within a physically mapped packed struct */
242af59bcffSViresh Kumar #define readl_relaxed_bitfield(_regptr, _type, _field)			\
2435a6338ccSViresh Kumar 	({								\
2445a6338ccSViresh Kumar 		u32 _regval;						\
2455a6338ccSViresh Kumar 		_regval = readl_relaxed((_regptr));			\
246af59bcffSViresh Kumar 		(*(_type *)(&_regval))._field;				\
2475a6338ccSViresh Kumar 	})
2485a6338ccSViresh Kumar 
2495a6338ccSViresh Kumar /* Macro for writing a bitfield within a physically mapped packed struct */
250af59bcffSViresh Kumar #define writel_relaxed_bitfield(_value, _regptr, _type, _field)		\
2515a6338ccSViresh Kumar 	({								\
2525a6338ccSViresh Kumar 		u32 _regval;						\
2535a6338ccSViresh Kumar 		_regval = readl_relaxed(_regptr);			\
254af59bcffSViresh Kumar 		(*(_type *)(&_regval))._field = _value;			\
2555a6338ccSViresh Kumar 		writel_relaxed(_regval, _regptr);			\
2565a6338ccSViresh Kumar 	})
2575a6338ccSViresh Kumar 
2585a6338ccSViresh Kumar 
2595a6338ccSViresh Kumar /* =================== Doorbell transport protocol operations =============== */
2605a6338ccSViresh Kumar 
mhuv2_doorbell_rx_startup(struct mhuv2 * mhu,struct mbox_chan * chan)2615a6338ccSViresh Kumar static int mhuv2_doorbell_rx_startup(struct mhuv2 *mhu, struct mbox_chan *chan)
2625a6338ccSViresh Kumar {
2635a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
2645a6338ccSViresh Kumar 
2655a6338ccSViresh Kumar 	writel_relaxed(BIT(priv->doorbell),
2665a6338ccSViresh Kumar 		       &mhu->recv->ch_wn[priv->ch_wn_idx].mask_clear);
2675a6338ccSViresh Kumar 	return 0;
2685a6338ccSViresh Kumar }
2695a6338ccSViresh Kumar 
mhuv2_doorbell_rx_shutdown(struct mhuv2 * mhu,struct mbox_chan * chan)2705a6338ccSViresh Kumar static void mhuv2_doorbell_rx_shutdown(struct mhuv2 *mhu,
2715a6338ccSViresh Kumar 				       struct mbox_chan *chan)
2725a6338ccSViresh Kumar {
2735a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
2745a6338ccSViresh Kumar 
2755a6338ccSViresh Kumar 	writel_relaxed(BIT(priv->doorbell),
2765a6338ccSViresh Kumar 		       &mhu->recv->ch_wn[priv->ch_wn_idx].mask_set);
2775a6338ccSViresh Kumar }
2785a6338ccSViresh Kumar 
mhuv2_doorbell_read_data(struct mhuv2 * mhu,struct mbox_chan * chan)2795a6338ccSViresh Kumar static void *mhuv2_doorbell_read_data(struct mhuv2 *mhu, struct mbox_chan *chan)
2805a6338ccSViresh Kumar {
2815a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
2825a6338ccSViresh Kumar 
2835a6338ccSViresh Kumar 	writel_relaxed(BIT(priv->doorbell),
2845a6338ccSViresh Kumar 		       &mhu->recv->ch_wn[priv->ch_wn_idx].stat_clear);
2855a6338ccSViresh Kumar 	return NULL;
2865a6338ccSViresh Kumar }
2875a6338ccSViresh Kumar 
mhuv2_doorbell_last_tx_done(struct mhuv2 * mhu,struct mbox_chan * chan)2885a6338ccSViresh Kumar static int mhuv2_doorbell_last_tx_done(struct mhuv2 *mhu,
2895a6338ccSViresh Kumar 				       struct mbox_chan *chan)
2905a6338ccSViresh Kumar {
2915a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
2925a6338ccSViresh Kumar 
2935a6338ccSViresh Kumar 	return !(readl_relaxed(&mhu->send->ch_wn[priv->ch_wn_idx].stat) &
2945a6338ccSViresh Kumar 		 BIT(priv->doorbell));
2955a6338ccSViresh Kumar }
2965a6338ccSViresh Kumar 
mhuv2_doorbell_send_data(struct mhuv2 * mhu,struct mbox_chan * chan,void * arg)2975a6338ccSViresh Kumar static int mhuv2_doorbell_send_data(struct mhuv2 *mhu, struct mbox_chan *chan,
2985a6338ccSViresh Kumar 				    void *arg)
2995a6338ccSViresh Kumar {
3005a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
3015a6338ccSViresh Kumar 	unsigned long flags;
3025a6338ccSViresh Kumar 
3035a6338ccSViresh Kumar 	spin_lock_irqsave(&mhu->doorbell_pending_lock, flags);
3045a6338ccSViresh Kumar 
3055a6338ccSViresh Kumar 	priv->pending = 1;
3065a6338ccSViresh Kumar 	writel_relaxed(BIT(priv->doorbell),
3075a6338ccSViresh Kumar 		       &mhu->send->ch_wn[priv->ch_wn_idx].stat_set);
3085a6338ccSViresh Kumar 
3095a6338ccSViresh Kumar 	spin_unlock_irqrestore(&mhu->doorbell_pending_lock, flags);
3105a6338ccSViresh Kumar 
3115a6338ccSViresh Kumar 	return 0;
3125a6338ccSViresh Kumar }
3135a6338ccSViresh Kumar 
3145a6338ccSViresh Kumar static const struct mhuv2_protocol_ops mhuv2_doorbell_ops = {
3155a6338ccSViresh Kumar 	.rx_startup = mhuv2_doorbell_rx_startup,
3165a6338ccSViresh Kumar 	.rx_shutdown = mhuv2_doorbell_rx_shutdown,
3175a6338ccSViresh Kumar 	.read_data = mhuv2_doorbell_read_data,
3185a6338ccSViresh Kumar 	.last_tx_done = mhuv2_doorbell_last_tx_done,
3195a6338ccSViresh Kumar 	.send_data = mhuv2_doorbell_send_data,
3205a6338ccSViresh Kumar };
3215a6338ccSViresh Kumar #define IS_PROTOCOL_DOORBELL(_priv) (_priv->ops == &mhuv2_doorbell_ops)
3225a6338ccSViresh Kumar 
3235a6338ccSViresh Kumar /* ============= Data transfer transport protocol operations ================ */
3245a6338ccSViresh Kumar 
mhuv2_data_transfer_rx_startup(struct mhuv2 * mhu,struct mbox_chan * chan)3255a6338ccSViresh Kumar static int mhuv2_data_transfer_rx_startup(struct mhuv2 *mhu,
3265a6338ccSViresh Kumar 					  struct mbox_chan *chan)
3275a6338ccSViresh Kumar {
3285a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
3295a6338ccSViresh Kumar 	int i = priv->ch_wn_idx + priv->windows - 1;
3305a6338ccSViresh Kumar 
3315a6338ccSViresh Kumar 	/*
3325a6338ccSViresh Kumar 	 * The protocol mandates that all but the last status register must be
3335a6338ccSViresh Kumar 	 * masked.
3345a6338ccSViresh Kumar 	 */
3355a6338ccSViresh Kumar 	writel_relaxed(0xFFFFFFFF, &mhu->recv->ch_wn[i].mask_clear);
3365a6338ccSViresh Kumar 	return 0;
3375a6338ccSViresh Kumar }
3385a6338ccSViresh Kumar 
mhuv2_data_transfer_rx_shutdown(struct mhuv2 * mhu,struct mbox_chan * chan)3395a6338ccSViresh Kumar static void mhuv2_data_transfer_rx_shutdown(struct mhuv2 *mhu,
3405a6338ccSViresh Kumar 					    struct mbox_chan *chan)
3415a6338ccSViresh Kumar {
3425a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
3435a6338ccSViresh Kumar 	int i = priv->ch_wn_idx + priv->windows - 1;
3445a6338ccSViresh Kumar 
3455a6338ccSViresh Kumar 	writel_relaxed(0xFFFFFFFF, &mhu->recv->ch_wn[i].mask_set);
3465a6338ccSViresh Kumar }
3475a6338ccSViresh Kumar 
mhuv2_data_transfer_read_data(struct mhuv2 * mhu,struct mbox_chan * chan)3485a6338ccSViresh Kumar static void *mhuv2_data_transfer_read_data(struct mhuv2 *mhu,
3495a6338ccSViresh Kumar 					   struct mbox_chan *chan)
3505a6338ccSViresh Kumar {
3515a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
3525a6338ccSViresh Kumar 	const int windows = priv->windows;
3535a6338ccSViresh Kumar 	struct arm_mhuv2_mbox_msg *msg;
3545a6338ccSViresh Kumar 	u32 *data;
3555a6338ccSViresh Kumar 	int i, idx;
3565a6338ccSViresh Kumar 
3575a6338ccSViresh Kumar 	msg = kzalloc(sizeof(*msg) + windows * MHUV2_STAT_BYTES, GFP_KERNEL);
3585a6338ccSViresh Kumar 	if (!msg)
3595a6338ccSViresh Kumar 		return ERR_PTR(-ENOMEM);
3605a6338ccSViresh Kumar 
3615a6338ccSViresh Kumar 	data = msg->data = msg + 1;
3625a6338ccSViresh Kumar 	msg->len = windows * MHUV2_STAT_BYTES;
3635a6338ccSViresh Kumar 
3645a6338ccSViresh Kumar 	/*
3655a6338ccSViresh Kumar 	 * Messages are expected in order of most significant word to least
3665a6338ccSViresh Kumar 	 * significant word. Refer mhuv2_data_transfer_send_data() for more
3675a6338ccSViresh Kumar 	 * details.
3685a6338ccSViresh Kumar 	 *
3695a6338ccSViresh Kumar 	 * We also need to read the stat register instead of stat_masked, as we
3705a6338ccSViresh Kumar 	 * masked all but the last window.
3715a6338ccSViresh Kumar 	 *
3725a6338ccSViresh Kumar 	 * Last channel window must be cleared as the final operation. Upon
3735a6338ccSViresh Kumar 	 * clearing the last channel window register, which is unmasked in
3745a6338ccSViresh Kumar 	 * data-transfer protocol, the interrupt is de-asserted.
3755a6338ccSViresh Kumar 	 */
3765a6338ccSViresh Kumar 	for (i = 0; i < windows; i++) {
3775a6338ccSViresh Kumar 		idx = priv->ch_wn_idx + i;
3785a6338ccSViresh Kumar 		data[windows - 1 - i] = readl_relaxed(&mhu->recv->ch_wn[idx].stat);
3795a6338ccSViresh Kumar 		writel_relaxed(0xFFFFFFFF, &mhu->recv->ch_wn[idx].stat_clear);
3805a6338ccSViresh Kumar 	}
3815a6338ccSViresh Kumar 
3825a6338ccSViresh Kumar 	return msg;
3835a6338ccSViresh Kumar }
3845a6338ccSViresh Kumar 
mhuv2_data_transfer_tx_startup(struct mhuv2 * mhu,struct mbox_chan * chan)3855a6338ccSViresh Kumar static void mhuv2_data_transfer_tx_startup(struct mhuv2 *mhu,
3865a6338ccSViresh Kumar 					   struct mbox_chan *chan)
3875a6338ccSViresh Kumar {
3885a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
3895a6338ccSViresh Kumar 	int i = priv->ch_wn_idx + priv->windows - 1;
3905a6338ccSViresh Kumar 
3915a6338ccSViresh Kumar 	/* Enable interrupts only for the last window */
3925a6338ccSViresh Kumar 	if (mhu->minor) {
3935a6338ccSViresh Kumar 		writel_relaxed(0x1, &mhu->send->ch_wn[i].int_clr);
3945a6338ccSViresh Kumar 		writel_relaxed(0x1, &mhu->send->ch_wn[i].int_en);
3955a6338ccSViresh Kumar 	}
3965a6338ccSViresh Kumar }
3975a6338ccSViresh Kumar 
mhuv2_data_transfer_tx_shutdown(struct mhuv2 * mhu,struct mbox_chan * chan)3985a6338ccSViresh Kumar static void mhuv2_data_transfer_tx_shutdown(struct mhuv2 *mhu,
3995a6338ccSViresh Kumar 					    struct mbox_chan *chan)
4005a6338ccSViresh Kumar {
4015a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
4025a6338ccSViresh Kumar 	int i = priv->ch_wn_idx + priv->windows - 1;
4035a6338ccSViresh Kumar 
4045a6338ccSViresh Kumar 	if (mhu->minor)
4055a6338ccSViresh Kumar 		writel_relaxed(0x0, &mhu->send->ch_wn[i].int_en);
4065a6338ccSViresh Kumar }
4075a6338ccSViresh Kumar 
mhuv2_data_transfer_last_tx_done(struct mhuv2 * mhu,struct mbox_chan * chan)4085a6338ccSViresh Kumar static int mhuv2_data_transfer_last_tx_done(struct mhuv2 *mhu,
4095a6338ccSViresh Kumar 					    struct mbox_chan *chan)
4105a6338ccSViresh Kumar {
4115a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
4125a6338ccSViresh Kumar 	int i = priv->ch_wn_idx + priv->windows - 1;
4135a6338ccSViresh Kumar 
4145a6338ccSViresh Kumar 	/* Just checking the last channel window should be enough */
4155a6338ccSViresh Kumar 	return !readl_relaxed(&mhu->send->ch_wn[i].stat);
4165a6338ccSViresh Kumar }
4175a6338ccSViresh Kumar 
4185a6338ccSViresh Kumar /*
4195a6338ccSViresh Kumar  * Message will be transmitted from most significant to least significant word.
4205a6338ccSViresh Kumar  * This is to allow for messages shorter than channel windows to still trigger
4215a6338ccSViresh Kumar  * the receiver interrupt which gets activated when the last stat register is
4225a6338ccSViresh Kumar  * written. As an example, a 6-word message is to be written on a 4-channel MHU
4235a6338ccSViresh Kumar  * connection: Registers marked with '*' are masked, and will not generate an
4245a6338ccSViresh Kumar  * interrupt on the receiver side once written.
4255a6338ccSViresh Kumar  *
4265a6338ccSViresh Kumar  * u32 *data =	[0x00000001], [0x00000002], [0x00000003], [0x00000004],
4275a6338ccSViresh Kumar  *		[0x00000005], [0x00000006]
4285a6338ccSViresh Kumar  *
4295a6338ccSViresh Kumar  * ROUND 1:
4305a6338ccSViresh Kumar  * stat reg		To write	Write sequence
4315a6338ccSViresh Kumar  * [ stat 3 ]	<-	[0x00000001]	4 <- triggers interrupt on receiver
4325a6338ccSViresh Kumar  * [ stat 2 ]	<-	[0x00000002]	3
4335a6338ccSViresh Kumar  * [ stat 1 ]	<-	[0x00000003]	2
4345a6338ccSViresh Kumar  * [ stat 0 ]	<-	[0x00000004]	1
4355a6338ccSViresh Kumar  *
4365a6338ccSViresh Kumar  * data += 4 // Increment data pointer by number of stat regs
4375a6338ccSViresh Kumar  *
4385a6338ccSViresh Kumar  * ROUND 2:
4395a6338ccSViresh Kumar  * stat reg		To write	Write sequence
4405a6338ccSViresh Kumar  * [ stat 3 ]	<-	[0x00000005]	2 <- triggers interrupt on receiver
4415a6338ccSViresh Kumar  * [ stat 2 ]	<-	[0x00000006]	1
4425a6338ccSViresh Kumar  * [ stat 1 ]	<-	[0x00000000]
4435a6338ccSViresh Kumar  * [ stat 0 ]	<-	[0x00000000]
4445a6338ccSViresh Kumar  */
mhuv2_data_transfer_send_data(struct mhuv2 * mhu,struct mbox_chan * chan,void * arg)4455a6338ccSViresh Kumar static int mhuv2_data_transfer_send_data(struct mhuv2 *mhu,
4465a6338ccSViresh Kumar 					 struct mbox_chan *chan, void *arg)
4475a6338ccSViresh Kumar {
4485a6338ccSViresh Kumar 	const struct arm_mhuv2_mbox_msg *msg = arg;
4495a6338ccSViresh Kumar 	int bytes_left = msg->len, bytes_to_send, bytes_in_round, i;
4505a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
4515a6338ccSViresh Kumar 	int windows = priv->windows;
4525a6338ccSViresh Kumar 	u32 *data = msg->data, word;
4535a6338ccSViresh Kumar 
4545a6338ccSViresh Kumar 	while (bytes_left) {
4555a6338ccSViresh Kumar 		if (!data[0]) {
4565a6338ccSViresh Kumar 			dev_err(mhu->mbox.dev, "Data aligned at first window can't be zero to guarantee interrupt generation at receiver");
4575a6338ccSViresh Kumar 			return -EINVAL;
4585a6338ccSViresh Kumar 		}
4595a6338ccSViresh Kumar 
4605a6338ccSViresh Kumar 		while(!mhuv2_data_transfer_last_tx_done(mhu, chan))
4615a6338ccSViresh Kumar 			continue;
4625a6338ccSViresh Kumar 
4635a6338ccSViresh Kumar 		bytes_in_round = min(bytes_left, (int)(windows * MHUV2_STAT_BYTES));
4645a6338ccSViresh Kumar 
4655a6338ccSViresh Kumar 		for (i = windows - 1; i >= 0; i--) {
4665a6338ccSViresh Kumar 			/* Data less than windows can transfer ? */
4675a6338ccSViresh Kumar 			if (unlikely(bytes_in_round <= i * MHUV2_STAT_BYTES))
4685a6338ccSViresh Kumar 				continue;
4695a6338ccSViresh Kumar 
4705a6338ccSViresh Kumar 			word = data[i];
4715a6338ccSViresh Kumar 			bytes_to_send = bytes_in_round & (MHUV2_STAT_BYTES - 1);
4725a6338ccSViresh Kumar 			if (unlikely(bytes_to_send))
4735a6338ccSViresh Kumar 				word &= LSB_MASK(bytes_to_send);
4745a6338ccSViresh Kumar 			else
4755a6338ccSViresh Kumar 				bytes_to_send = MHUV2_STAT_BYTES;
4765a6338ccSViresh Kumar 
4775a6338ccSViresh Kumar 			writel_relaxed(word, &mhu->send->ch_wn[priv->ch_wn_idx + windows - 1 - i].stat_set);
4785a6338ccSViresh Kumar 			bytes_left -= bytes_to_send;
4795a6338ccSViresh Kumar 			bytes_in_round -= bytes_to_send;
4805a6338ccSViresh Kumar 		}
4815a6338ccSViresh Kumar 
4825a6338ccSViresh Kumar 		data += windows;
4835a6338ccSViresh Kumar 	}
4845a6338ccSViresh Kumar 
4855a6338ccSViresh Kumar 	return 0;
4865a6338ccSViresh Kumar }
4875a6338ccSViresh Kumar 
4885a6338ccSViresh Kumar static const struct mhuv2_protocol_ops mhuv2_data_transfer_ops = {
4895a6338ccSViresh Kumar 	.rx_startup = mhuv2_data_transfer_rx_startup,
4905a6338ccSViresh Kumar 	.rx_shutdown = mhuv2_data_transfer_rx_shutdown,
4915a6338ccSViresh Kumar 	.read_data = mhuv2_data_transfer_read_data,
4925a6338ccSViresh Kumar 	.tx_startup = mhuv2_data_transfer_tx_startup,
4935a6338ccSViresh Kumar 	.tx_shutdown = mhuv2_data_transfer_tx_shutdown,
4945a6338ccSViresh Kumar 	.last_tx_done = mhuv2_data_transfer_last_tx_done,
4955a6338ccSViresh Kumar 	.send_data = mhuv2_data_transfer_send_data,
4965a6338ccSViresh Kumar };
4975a6338ccSViresh Kumar 
4985a6338ccSViresh Kumar /* Interrupt handlers */
4995a6338ccSViresh Kumar 
get_irq_chan_comb(struct mhuv2 * mhu,u32 __iomem * reg)500af59bcffSViresh Kumar static struct mbox_chan *get_irq_chan_comb(struct mhuv2 *mhu, u32 __iomem *reg)
5015a6338ccSViresh Kumar {
5025a6338ccSViresh Kumar 	struct mbox_chan *chans = mhu->mbox.chans;
503*39e5f390SDan Carpenter 	int channel = 0, i, j, offset = 0, windows, protocol, ch_wn;
5045a6338ccSViresh Kumar 	u32 stat;
5055a6338ccSViresh Kumar 
5065a6338ccSViresh Kumar 	for (i = 0; i < MHUV2_CMB_INT_ST_REG_CNT; i++) {
5075a6338ccSViresh Kumar 		stat = readl_relaxed(reg + i);
5085a6338ccSViresh Kumar 		if (!stat)
5095a6338ccSViresh Kumar 			continue;
5105a6338ccSViresh Kumar 
5115a6338ccSViresh Kumar 		ch_wn = i * MHUV2_STAT_BITS + __builtin_ctz(stat);
5125a6338ccSViresh Kumar 
513*39e5f390SDan Carpenter 		for (j = 0; j < mhu->length; j += 2) {
514*39e5f390SDan Carpenter 			protocol = mhu->protocols[j];
515*39e5f390SDan Carpenter 			windows = mhu->protocols[j + 1];
5165a6338ccSViresh Kumar 
5175a6338ccSViresh Kumar 			if (ch_wn >= offset + windows) {
5185a6338ccSViresh Kumar 				if (protocol == DOORBELL)
5195a6338ccSViresh Kumar 					channel += MHUV2_STAT_BITS * windows;
5205a6338ccSViresh Kumar 				else
5215a6338ccSViresh Kumar 					channel++;
5225a6338ccSViresh Kumar 
5235a6338ccSViresh Kumar 				offset += windows;
5245a6338ccSViresh Kumar 				continue;
5255a6338ccSViresh Kumar 			}
5265a6338ccSViresh Kumar 
5275a6338ccSViresh Kumar 			/* Return first chan of the window in doorbell mode */
5285a6338ccSViresh Kumar 			if (protocol == DOORBELL)
5295a6338ccSViresh Kumar 				channel += MHUV2_STAT_BITS * (ch_wn - offset);
5305a6338ccSViresh Kumar 
5315a6338ccSViresh Kumar 			return &chans[channel];
5325a6338ccSViresh Kumar 		}
5335a6338ccSViresh Kumar 	}
5345a6338ccSViresh Kumar 
5355a6338ccSViresh Kumar 	return ERR_PTR(-EIO);
5365a6338ccSViresh Kumar }
5375a6338ccSViresh Kumar 
mhuv2_sender_interrupt(int irq,void * data)5385a6338ccSViresh Kumar static irqreturn_t mhuv2_sender_interrupt(int irq, void *data)
5395a6338ccSViresh Kumar {
5405a6338ccSViresh Kumar 	struct mhuv2 *mhu = data;
5415a6338ccSViresh Kumar 	struct device *dev = mhu->mbox.dev;
5425a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv;
5435a6338ccSViresh Kumar 	struct mbox_chan *chan;
5445a6338ccSViresh Kumar 	unsigned long flags;
5455a6338ccSViresh Kumar 	int i, found = 0;
5465a6338ccSViresh Kumar 	u32 stat;
5475a6338ccSViresh Kumar 
5485a6338ccSViresh Kumar 	chan = get_irq_chan_comb(mhu, mhu->send->chcomb_int_st);
5495a6338ccSViresh Kumar 	if (IS_ERR(chan)) {
5505a6338ccSViresh Kumar 		dev_warn(dev, "Failed to find channel for the Tx interrupt\n");
5515a6338ccSViresh Kumar 		return IRQ_NONE;
5525a6338ccSViresh Kumar 	}
5535a6338ccSViresh Kumar 	priv = chan->con_priv;
5545a6338ccSViresh Kumar 
5555a6338ccSViresh Kumar 	if (!IS_PROTOCOL_DOORBELL(priv)) {
556a71f8c0aSXiaowu.ding 		for (i = 0; i < priv->windows; i++)
557a71f8c0aSXiaowu.ding 			writel_relaxed(1, &mhu->send->ch_wn[priv->ch_wn_idx + i].int_clr);
5585a6338ccSViresh Kumar 
5595a6338ccSViresh Kumar 		if (chan->cl) {
5605a6338ccSViresh Kumar 			mbox_chan_txdone(chan, 0);
5615a6338ccSViresh Kumar 			return IRQ_HANDLED;
5625a6338ccSViresh Kumar 		}
5635a6338ccSViresh Kumar 
5645a6338ccSViresh Kumar 		dev_warn(dev, "Tx interrupt Received on channel (%u) not currently attached to a mailbox client\n",
5655a6338ccSViresh Kumar 			 priv->ch_wn_idx);
5665a6338ccSViresh Kumar 		return IRQ_NONE;
5675a6338ccSViresh Kumar 	}
5685a6338ccSViresh Kumar 
5695a6338ccSViresh Kumar 	/* Clear the interrupt first, so we don't miss any doorbell later */
5705a6338ccSViresh Kumar 	writel_relaxed(1, &mhu->send->ch_wn[priv->ch_wn_idx].int_clr);
5715a6338ccSViresh Kumar 
5725a6338ccSViresh Kumar 	/*
5735a6338ccSViresh Kumar 	 * In Doorbell mode, make sure no new transitions happen while the
5745a6338ccSViresh Kumar 	 * interrupt handler is trying to find the finished doorbell tx
5755a6338ccSViresh Kumar 	 * operations, else we may think few of the transfers were complete
5765a6338ccSViresh Kumar 	 * before they actually were.
5775a6338ccSViresh Kumar 	 */
5785a6338ccSViresh Kumar 	spin_lock_irqsave(&mhu->doorbell_pending_lock, flags);
5795a6338ccSViresh Kumar 
5805a6338ccSViresh Kumar 	/*
5815a6338ccSViresh Kumar 	 * In case of doorbell mode, the first channel of the window is returned
5825a6338ccSViresh Kumar 	 * by get_irq_chan_comb(). Find all the pending channels here.
5835a6338ccSViresh Kumar 	 */
5845a6338ccSViresh Kumar 	stat = readl_relaxed(&mhu->send->ch_wn[priv->ch_wn_idx].stat);
5855a6338ccSViresh Kumar 
5865a6338ccSViresh Kumar 	for (i = 0; i < MHUV2_STAT_BITS; i++) {
5875a6338ccSViresh Kumar 		priv = chan[i].con_priv;
5885a6338ccSViresh Kumar 
5895a6338ccSViresh Kumar 		/* Find cases where pending was 1, but stat's bit is cleared */
5905a6338ccSViresh Kumar 		if (priv->pending ^ ((stat >> i) & 0x1)) {
5915a6338ccSViresh Kumar 			BUG_ON(!priv->pending);
5925a6338ccSViresh Kumar 
5935a6338ccSViresh Kumar 			if (!chan->cl) {
5945a6338ccSViresh Kumar 				dev_warn(dev, "Tx interrupt received on doorbell (%u : %u) channel not currently attached to a mailbox client\n",
5955a6338ccSViresh Kumar 					 priv->ch_wn_idx, i);
5965a6338ccSViresh Kumar 				continue;
5975a6338ccSViresh Kumar 			}
5985a6338ccSViresh Kumar 
5995a6338ccSViresh Kumar 			mbox_chan_txdone(&chan[i], 0);
6005a6338ccSViresh Kumar 			priv->pending = 0;
6015a6338ccSViresh Kumar 			found++;
6025a6338ccSViresh Kumar 		}
6035a6338ccSViresh Kumar 	}
6045a6338ccSViresh Kumar 
6055a6338ccSViresh Kumar 	spin_unlock_irqrestore(&mhu->doorbell_pending_lock, flags);
6065a6338ccSViresh Kumar 
6075a6338ccSViresh Kumar 	if (!found) {
6085a6338ccSViresh Kumar 		/*
6095a6338ccSViresh Kumar 		 * We may have already processed the doorbell in the previous
6105a6338ccSViresh Kumar 		 * iteration if the interrupt came right after we cleared it but
6115a6338ccSViresh Kumar 		 * before we read the stat register.
6125a6338ccSViresh Kumar 		 */
6135a6338ccSViresh Kumar 		dev_dbg(dev, "Couldn't find the doorbell (%u) for the Tx interrupt interrupt\n",
6145a6338ccSViresh Kumar 			priv->ch_wn_idx);
6155a6338ccSViresh Kumar 		return IRQ_NONE;
6165a6338ccSViresh Kumar 	}
6175a6338ccSViresh Kumar 
6185a6338ccSViresh Kumar 	return IRQ_HANDLED;
6195a6338ccSViresh Kumar }
6205a6338ccSViresh Kumar 
get_irq_chan_comb_rx(struct mhuv2 * mhu)6215a6338ccSViresh Kumar static struct mbox_chan *get_irq_chan_comb_rx(struct mhuv2 *mhu)
6225a6338ccSViresh Kumar {
6235a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv;
6245a6338ccSViresh Kumar 	struct mbox_chan *chan;
6255a6338ccSViresh Kumar 	u32 stat;
6265a6338ccSViresh Kumar 
6275a6338ccSViresh Kumar 	chan = get_irq_chan_comb(mhu, mhu->recv->chcomb_int_st);
6285a6338ccSViresh Kumar 	if (IS_ERR(chan))
6295a6338ccSViresh Kumar 		return chan;
6305a6338ccSViresh Kumar 
6315a6338ccSViresh Kumar 	priv = chan->con_priv;
6325a6338ccSViresh Kumar 	if (!IS_PROTOCOL_DOORBELL(priv))
6335a6338ccSViresh Kumar 		return chan;
6345a6338ccSViresh Kumar 
6355a6338ccSViresh Kumar 	/*
6365a6338ccSViresh Kumar 	 * In case of doorbell mode, the first channel of the window is returned
6375a6338ccSViresh Kumar 	 * by the routine. Find the exact channel here.
6385a6338ccSViresh Kumar 	 */
6395a6338ccSViresh Kumar 	stat = readl_relaxed(&mhu->recv->ch_wn[priv->ch_wn_idx].stat_masked);
6405a6338ccSViresh Kumar 	BUG_ON(!stat);
6415a6338ccSViresh Kumar 
6425a6338ccSViresh Kumar 	return chan + __builtin_ctz(stat);
6435a6338ccSViresh Kumar }
6445a6338ccSViresh Kumar 
get_irq_chan_stat_rx(struct mhuv2 * mhu)6455a6338ccSViresh Kumar static struct mbox_chan *get_irq_chan_stat_rx(struct mhuv2 *mhu)
6465a6338ccSViresh Kumar {
6475a6338ccSViresh Kumar 	struct mbox_chan *chans = mhu->mbox.chans;
6485a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv;
6495a6338ccSViresh Kumar 	u32 stat;
6505a6338ccSViresh Kumar 	int i = 0;
6515a6338ccSViresh Kumar 
6525a6338ccSViresh Kumar 	while (i < mhu->mbox.num_chans) {
6535a6338ccSViresh Kumar 		priv = chans[i].con_priv;
6545a6338ccSViresh Kumar 		stat = readl_relaxed(&mhu->recv->ch_wn[priv->ch_wn_idx].stat_masked);
6555a6338ccSViresh Kumar 
6565a6338ccSViresh Kumar 		if (stat) {
6575a6338ccSViresh Kumar 			if (IS_PROTOCOL_DOORBELL(priv))
6585a6338ccSViresh Kumar 				i += __builtin_ctz(stat);
6595a6338ccSViresh Kumar 			return &chans[i];
6605a6338ccSViresh Kumar 		}
6615a6338ccSViresh Kumar 
6625a6338ccSViresh Kumar 		i += IS_PROTOCOL_DOORBELL(priv) ? MHUV2_STAT_BITS : 1;
6635a6338ccSViresh Kumar 	}
6645a6338ccSViresh Kumar 
6655a6338ccSViresh Kumar 	return ERR_PTR(-EIO);
6665a6338ccSViresh Kumar }
6675a6338ccSViresh Kumar 
get_irq_chan_rx(struct mhuv2 * mhu)6685a6338ccSViresh Kumar static struct mbox_chan *get_irq_chan_rx(struct mhuv2 *mhu)
6695a6338ccSViresh Kumar {
6705a6338ccSViresh Kumar 	if (!mhu->minor)
6715a6338ccSViresh Kumar 		return get_irq_chan_stat_rx(mhu);
6725a6338ccSViresh Kumar 
6735a6338ccSViresh Kumar 	return get_irq_chan_comb_rx(mhu);
6745a6338ccSViresh Kumar }
6755a6338ccSViresh Kumar 
mhuv2_receiver_interrupt(int irq,void * arg)6765a6338ccSViresh Kumar static irqreturn_t mhuv2_receiver_interrupt(int irq, void *arg)
6775a6338ccSViresh Kumar {
6785a6338ccSViresh Kumar 	struct mhuv2 *mhu = arg;
6795a6338ccSViresh Kumar 	struct mbox_chan *chan = get_irq_chan_rx(mhu);
6805a6338ccSViresh Kumar 	struct device *dev = mhu->mbox.dev;
6815a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv;
6825a6338ccSViresh Kumar 	int ret = IRQ_NONE;
6835a6338ccSViresh Kumar 	void *data;
6845a6338ccSViresh Kumar 
6855a6338ccSViresh Kumar 	if (IS_ERR(chan)) {
6865a6338ccSViresh Kumar 		dev_warn(dev, "Failed to find channel for the rx interrupt\n");
6875a6338ccSViresh Kumar 		return IRQ_NONE;
6885a6338ccSViresh Kumar 	}
6895a6338ccSViresh Kumar 	priv = chan->con_priv;
6905a6338ccSViresh Kumar 
6915a6338ccSViresh Kumar 	/* Read and clear the data first */
6925a6338ccSViresh Kumar 	data = priv->ops->read_data(mhu, chan);
6935a6338ccSViresh Kumar 
6945a6338ccSViresh Kumar 	if (!chan->cl) {
6955a6338ccSViresh Kumar 		dev_warn(dev, "Received data on channel (%u) not currently attached to a mailbox client\n",
6965a6338ccSViresh Kumar 			 priv->ch_wn_idx);
6975a6338ccSViresh Kumar 	} else if (IS_ERR(data)) {
6985a6338ccSViresh Kumar 		dev_err(dev, "Failed to read data: %lu\n", PTR_ERR(data));
6995a6338ccSViresh Kumar 	} else {
7005a6338ccSViresh Kumar 		mbox_chan_received_data(chan, data);
7015a6338ccSViresh Kumar 		ret = IRQ_HANDLED;
7025a6338ccSViresh Kumar 	}
7035a6338ccSViresh Kumar 
7046b50df2bSViresh Kumar 	if (!IS_ERR(data))
7055a6338ccSViresh Kumar 		kfree(data);
7066b50df2bSViresh Kumar 
7075a6338ccSViresh Kumar 	return ret;
7085a6338ccSViresh Kumar }
7095a6338ccSViresh Kumar 
7105a6338ccSViresh Kumar /* Sender and receiver ops */
mhuv2_sender_last_tx_done(struct mbox_chan * chan)7115a6338ccSViresh Kumar static bool mhuv2_sender_last_tx_done(struct mbox_chan *chan)
7125a6338ccSViresh Kumar {
7135a6338ccSViresh Kumar 	struct mhuv2 *mhu = mhu_from_mbox(chan->mbox);
7145a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
7155a6338ccSViresh Kumar 
7165a6338ccSViresh Kumar 	return priv->ops->last_tx_done(mhu, chan);
7175a6338ccSViresh Kumar }
7185a6338ccSViresh Kumar 
mhuv2_sender_send_data(struct mbox_chan * chan,void * data)7195a6338ccSViresh Kumar static int mhuv2_sender_send_data(struct mbox_chan *chan, void *data)
7205a6338ccSViresh Kumar {
7215a6338ccSViresh Kumar 	struct mhuv2 *mhu = mhu_from_mbox(chan->mbox);
7225a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
7235a6338ccSViresh Kumar 
7245a6338ccSViresh Kumar 	if (!priv->ops->last_tx_done(mhu, chan))
7255a6338ccSViresh Kumar 		return -EBUSY;
7265a6338ccSViresh Kumar 
7275a6338ccSViresh Kumar 	return priv->ops->send_data(mhu, chan, data);
7285a6338ccSViresh Kumar }
7295a6338ccSViresh Kumar 
mhuv2_sender_startup(struct mbox_chan * chan)7305a6338ccSViresh Kumar static int mhuv2_sender_startup(struct mbox_chan *chan)
7315a6338ccSViresh Kumar {
7325a6338ccSViresh Kumar 	struct mhuv2 *mhu = mhu_from_mbox(chan->mbox);
7335a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
7345a6338ccSViresh Kumar 
7355a6338ccSViresh Kumar 	if (priv->ops->tx_startup)
7365a6338ccSViresh Kumar 		priv->ops->tx_startup(mhu, chan);
7375a6338ccSViresh Kumar 	return 0;
7385a6338ccSViresh Kumar }
7395a6338ccSViresh Kumar 
mhuv2_sender_shutdown(struct mbox_chan * chan)7405a6338ccSViresh Kumar static void mhuv2_sender_shutdown(struct mbox_chan *chan)
7415a6338ccSViresh Kumar {
7425a6338ccSViresh Kumar 	struct mhuv2 *mhu = mhu_from_mbox(chan->mbox);
7435a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
7445a6338ccSViresh Kumar 
7455a6338ccSViresh Kumar 	if (priv->ops->tx_shutdown)
7465a6338ccSViresh Kumar 		priv->ops->tx_shutdown(mhu, chan);
7475a6338ccSViresh Kumar }
7485a6338ccSViresh Kumar 
7495a6338ccSViresh Kumar static const struct mbox_chan_ops mhuv2_sender_ops = {
7505a6338ccSViresh Kumar 	.send_data = mhuv2_sender_send_data,
7515a6338ccSViresh Kumar 	.startup = mhuv2_sender_startup,
7525a6338ccSViresh Kumar 	.shutdown = mhuv2_sender_shutdown,
7535a6338ccSViresh Kumar 	.last_tx_done = mhuv2_sender_last_tx_done,
7545a6338ccSViresh Kumar };
7555a6338ccSViresh Kumar 
mhuv2_receiver_startup(struct mbox_chan * chan)7565a6338ccSViresh Kumar static int mhuv2_receiver_startup(struct mbox_chan *chan)
7575a6338ccSViresh Kumar {
7585a6338ccSViresh Kumar 	struct mhuv2 *mhu = mhu_from_mbox(chan->mbox);
7595a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
7605a6338ccSViresh Kumar 
7615a6338ccSViresh Kumar 	return priv->ops->rx_startup(mhu, chan);
7625a6338ccSViresh Kumar }
7635a6338ccSViresh Kumar 
mhuv2_receiver_shutdown(struct mbox_chan * chan)7645a6338ccSViresh Kumar static void mhuv2_receiver_shutdown(struct mbox_chan *chan)
7655a6338ccSViresh Kumar {
7665a6338ccSViresh Kumar 	struct mhuv2 *mhu = mhu_from_mbox(chan->mbox);
7675a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv = chan->con_priv;
7685a6338ccSViresh Kumar 
7695a6338ccSViresh Kumar 	priv->ops->rx_shutdown(mhu, chan);
7705a6338ccSViresh Kumar }
7715a6338ccSViresh Kumar 
mhuv2_receiver_send_data(struct mbox_chan * chan,void * data)7725a6338ccSViresh Kumar static int mhuv2_receiver_send_data(struct mbox_chan *chan, void *data)
7735a6338ccSViresh Kumar {
7745a6338ccSViresh Kumar 	dev_err(chan->mbox->dev,
7755a6338ccSViresh Kumar 		"Trying to transmit on a receiver MHU frame\n");
7765a6338ccSViresh Kumar 	return -EIO;
7775a6338ccSViresh Kumar }
7785a6338ccSViresh Kumar 
mhuv2_receiver_last_tx_done(struct mbox_chan * chan)7795a6338ccSViresh Kumar static bool mhuv2_receiver_last_tx_done(struct mbox_chan *chan)
7805a6338ccSViresh Kumar {
7815a6338ccSViresh Kumar 	dev_err(chan->mbox->dev, "Trying to Tx poll on a receiver MHU frame\n");
7825a6338ccSViresh Kumar 	return true;
7835a6338ccSViresh Kumar }
7845a6338ccSViresh Kumar 
7855a6338ccSViresh Kumar static const struct mbox_chan_ops mhuv2_receiver_ops = {
7865a6338ccSViresh Kumar 	.send_data = mhuv2_receiver_send_data,
7875a6338ccSViresh Kumar 	.startup = mhuv2_receiver_startup,
7885a6338ccSViresh Kumar 	.shutdown = mhuv2_receiver_shutdown,
7895a6338ccSViresh Kumar 	.last_tx_done = mhuv2_receiver_last_tx_done,
7905a6338ccSViresh Kumar };
7915a6338ccSViresh Kumar 
mhuv2_mbox_of_xlate(struct mbox_controller * mbox,const struct of_phandle_args * pa)7925a6338ccSViresh Kumar static struct mbox_chan *mhuv2_mbox_of_xlate(struct mbox_controller *mbox,
7935a6338ccSViresh Kumar 					     const struct of_phandle_args *pa)
7945a6338ccSViresh Kumar {
7955a6338ccSViresh Kumar 	struct mhuv2 *mhu = mhu_from_mbox(mbox);
7965a6338ccSViresh Kumar 	struct mbox_chan *chans = mbox->chans;
7975a6338ccSViresh Kumar 	int channel = 0, i, offset, doorbell, protocol, windows;
7985a6338ccSViresh Kumar 
7995a6338ccSViresh Kumar 	if (pa->args_count != 2)
8005a6338ccSViresh Kumar 		return ERR_PTR(-EINVAL);
8015a6338ccSViresh Kumar 
8025a6338ccSViresh Kumar 	offset = pa->args[0];
8035a6338ccSViresh Kumar 	doorbell = pa->args[1];
8045a6338ccSViresh Kumar 	if (doorbell >= MHUV2_STAT_BITS)
8055a6338ccSViresh Kumar 		goto out;
8065a6338ccSViresh Kumar 
8075a6338ccSViresh Kumar 	for (i = 0; i < mhu->length; i += 2) {
8085a6338ccSViresh Kumar 		protocol = mhu->protocols[i];
8095a6338ccSViresh Kumar 		windows = mhu->protocols[i + 1];
8105a6338ccSViresh Kumar 
8115a6338ccSViresh Kumar 		if (protocol == DOORBELL) {
8125a6338ccSViresh Kumar 			if (offset < windows)
8135a6338ccSViresh Kumar 				return &chans[channel + MHUV2_STAT_BITS * offset + doorbell];
8145a6338ccSViresh Kumar 
8155a6338ccSViresh Kumar 			channel += MHUV2_STAT_BITS * windows;
8165a6338ccSViresh Kumar 			offset -= windows;
8175a6338ccSViresh Kumar 		} else {
8185a6338ccSViresh Kumar 			if (offset == 0) {
8195a6338ccSViresh Kumar 				if (doorbell)
8205a6338ccSViresh Kumar 					goto out;
8215a6338ccSViresh Kumar 
8225a6338ccSViresh Kumar 				return &chans[channel];
8235a6338ccSViresh Kumar 			}
8245a6338ccSViresh Kumar 
8255a6338ccSViresh Kumar 			channel++;
8265a6338ccSViresh Kumar 			offset--;
8275a6338ccSViresh Kumar 		}
8285a6338ccSViresh Kumar 	}
8295a6338ccSViresh Kumar 
8305a6338ccSViresh Kumar out:
8315a6338ccSViresh Kumar 	dev_err(mbox->dev, "Couldn't xlate to a valid channel (%d: %d)\n",
8325a6338ccSViresh Kumar 		pa->args[0], doorbell);
8335a6338ccSViresh Kumar 	return ERR_PTR(-ENODEV);
8345a6338ccSViresh Kumar }
8355a6338ccSViresh Kumar 
mhuv2_verify_protocol(struct mhuv2 * mhu)8365a6338ccSViresh Kumar static int mhuv2_verify_protocol(struct mhuv2 *mhu)
8375a6338ccSViresh Kumar {
8385a6338ccSViresh Kumar 	struct device *dev = mhu->mbox.dev;
8395a6338ccSViresh Kumar 	int protocol, windows, channels = 0, total_windows = 0, i;
8405a6338ccSViresh Kumar 
8415a6338ccSViresh Kumar 	for (i = 0; i < mhu->length; i += 2) {
8425a6338ccSViresh Kumar 		protocol = mhu->protocols[i];
8435a6338ccSViresh Kumar 		windows = mhu->protocols[i + 1];
8445a6338ccSViresh Kumar 
8455a6338ccSViresh Kumar 		if (!windows) {
8465a6338ccSViresh Kumar 			dev_err(dev, "Window size can't be zero (%d)\n", i);
8475a6338ccSViresh Kumar 			return -EINVAL;
8485a6338ccSViresh Kumar 		}
8495a6338ccSViresh Kumar 		total_windows += windows;
8505a6338ccSViresh Kumar 
8515a6338ccSViresh Kumar 		if (protocol == DOORBELL) {
8525a6338ccSViresh Kumar 			channels += MHUV2_STAT_BITS * windows;
8535a6338ccSViresh Kumar 		} else if (protocol == DATA_TRANSFER) {
8545a6338ccSViresh Kumar 			channels++;
8555a6338ccSViresh Kumar 		} else {
8565a6338ccSViresh Kumar 			dev_err(dev, "Invalid protocol (%d) present in %s property at index %d\n",
8575a6338ccSViresh Kumar 				protocol, MHUV2_PROTOCOL_PROP, i);
8585a6338ccSViresh Kumar 			return -EINVAL;
8595a6338ccSViresh Kumar 		}
8605a6338ccSViresh Kumar 	}
8615a6338ccSViresh Kumar 
8625a6338ccSViresh Kumar 	if (total_windows > mhu->windows) {
8635a6338ccSViresh Kumar 		dev_err(dev, "Channel windows can't be more than what's implemented by the hardware ( %d: %d)\n",
8645a6338ccSViresh Kumar 			total_windows, mhu->windows);
8655a6338ccSViresh Kumar 		return -EINVAL;
8665a6338ccSViresh Kumar 	}
8675a6338ccSViresh Kumar 
8685a6338ccSViresh Kumar 	mhu->mbox.num_chans = channels;
8695a6338ccSViresh Kumar 	return 0;
8705a6338ccSViresh Kumar }
8715a6338ccSViresh Kumar 
mhuv2_allocate_channels(struct mhuv2 * mhu)8725a6338ccSViresh Kumar static int mhuv2_allocate_channels(struct mhuv2 *mhu)
8735a6338ccSViresh Kumar {
8745a6338ccSViresh Kumar 	struct mbox_controller *mbox = &mhu->mbox;
8755a6338ccSViresh Kumar 	struct mhuv2_mbox_chan_priv *priv;
8765a6338ccSViresh Kumar 	struct device *dev = mbox->dev;
8775a6338ccSViresh Kumar 	struct mbox_chan *chans;
8785a6338ccSViresh Kumar 	int protocol, windows = 0, next_window = 0, i, j, k;
8795a6338ccSViresh Kumar 
8805a6338ccSViresh Kumar 	chans = devm_kcalloc(dev, mbox->num_chans, sizeof(*chans), GFP_KERNEL);
8815a6338ccSViresh Kumar 	if (!chans)
8825a6338ccSViresh Kumar 		return -ENOMEM;
8835a6338ccSViresh Kumar 
8845a6338ccSViresh Kumar 	mbox->chans = chans;
8855a6338ccSViresh Kumar 
8865a6338ccSViresh Kumar 	for (i = 0; i < mhu->length; i += 2) {
8875a6338ccSViresh Kumar 		next_window += windows;
8885a6338ccSViresh Kumar 
8895a6338ccSViresh Kumar 		protocol = mhu->protocols[i];
8905a6338ccSViresh Kumar 		windows = mhu->protocols[i + 1];
8915a6338ccSViresh Kumar 
8925a6338ccSViresh Kumar 		if (protocol == DATA_TRANSFER) {
8935a6338ccSViresh Kumar 			priv = devm_kmalloc(dev, sizeof(*priv), GFP_KERNEL);
8945a6338ccSViresh Kumar 			if (!priv)
8955a6338ccSViresh Kumar 				return -ENOMEM;
8965a6338ccSViresh Kumar 
8975a6338ccSViresh Kumar 			priv->ch_wn_idx = next_window;
8985a6338ccSViresh Kumar 			priv->ops = &mhuv2_data_transfer_ops;
8995a6338ccSViresh Kumar 			priv->windows = windows;
9005a6338ccSViresh Kumar 			chans++->con_priv = priv;
9015a6338ccSViresh Kumar 			continue;
9025a6338ccSViresh Kumar 		}
9035a6338ccSViresh Kumar 
9045a6338ccSViresh Kumar 		for (j = 0; j < windows; j++) {
9055a6338ccSViresh Kumar 			for (k = 0; k < MHUV2_STAT_BITS; k++) {
9065a6338ccSViresh Kumar 				priv = devm_kmalloc(dev, sizeof(*priv), GFP_KERNEL);
9075a6338ccSViresh Kumar 				if (!priv)
9085a6338ccSViresh Kumar 					return -ENOMEM;
9095a6338ccSViresh Kumar 
9105a6338ccSViresh Kumar 				priv->ch_wn_idx = next_window + j;
9115a6338ccSViresh Kumar 				priv->ops = &mhuv2_doorbell_ops;
9125a6338ccSViresh Kumar 				priv->doorbell = k;
9135a6338ccSViresh Kumar 				chans++->con_priv = priv;
9145a6338ccSViresh Kumar 			}
9155a6338ccSViresh Kumar 
9165a6338ccSViresh Kumar 			/*
9175a6338ccSViresh Kumar 			 * Permanently enable interrupt as we can't
9185a6338ccSViresh Kumar 			 * control it per doorbell.
9195a6338ccSViresh Kumar 			 */
9205a6338ccSViresh Kumar 			if (mhu->frame == SENDER_FRAME && mhu->minor)
9215a6338ccSViresh Kumar 				writel_relaxed(0x1, &mhu->send->ch_wn[priv->ch_wn_idx].int_en);
9225a6338ccSViresh Kumar 		}
9235a6338ccSViresh Kumar 	}
9245a6338ccSViresh Kumar 
9255a6338ccSViresh Kumar 	/* Make sure we have initialized all channels */
9265a6338ccSViresh Kumar 	BUG_ON(chans - mbox->chans != mbox->num_chans);
9275a6338ccSViresh Kumar 
9285a6338ccSViresh Kumar 	return 0;
9295a6338ccSViresh Kumar }
9305a6338ccSViresh Kumar 
mhuv2_parse_channels(struct mhuv2 * mhu)9315a6338ccSViresh Kumar static int mhuv2_parse_channels(struct mhuv2 *mhu)
9325a6338ccSViresh Kumar {
9335a6338ccSViresh Kumar 	struct device *dev = mhu->mbox.dev;
9345a6338ccSViresh Kumar 	const struct device_node *np = dev->of_node;
9355a6338ccSViresh Kumar 	int ret, count;
9365a6338ccSViresh Kumar 	u32 *protocols;
9375a6338ccSViresh Kumar 
9385a6338ccSViresh Kumar 	count = of_property_count_u32_elems(np, MHUV2_PROTOCOL_PROP);
9395a6338ccSViresh Kumar 	if (count <= 0 || count % 2) {
9405a6338ccSViresh Kumar 		dev_err(dev, "Invalid %s property (%d)\n", MHUV2_PROTOCOL_PROP,
9415a6338ccSViresh Kumar 			count);
9425a6338ccSViresh Kumar 		return -EINVAL;
9435a6338ccSViresh Kumar 	}
9445a6338ccSViresh Kumar 
9455a6338ccSViresh Kumar 	protocols = devm_kmalloc_array(dev, count, sizeof(*protocols), GFP_KERNEL);
9465a6338ccSViresh Kumar 	if (!protocols)
9475a6338ccSViresh Kumar 		return -ENOMEM;
9485a6338ccSViresh Kumar 
9495a6338ccSViresh Kumar 	ret = of_property_read_u32_array(np, MHUV2_PROTOCOL_PROP, protocols, count);
9505a6338ccSViresh Kumar 	if (ret) {
9515a6338ccSViresh Kumar 		dev_err(dev, "Failed to read %s property: %d\n",
9525a6338ccSViresh Kumar 			MHUV2_PROTOCOL_PROP, ret);
9535a6338ccSViresh Kumar 		return ret;
9545a6338ccSViresh Kumar 	}
9555a6338ccSViresh Kumar 
9565a6338ccSViresh Kumar 	mhu->protocols = protocols;
9575a6338ccSViresh Kumar 	mhu->length = count;
9585a6338ccSViresh Kumar 
9595a6338ccSViresh Kumar 	ret = mhuv2_verify_protocol(mhu);
9605a6338ccSViresh Kumar 	if (ret)
9615a6338ccSViresh Kumar 		return ret;
9625a6338ccSViresh Kumar 
9635a6338ccSViresh Kumar 	return mhuv2_allocate_channels(mhu);
9645a6338ccSViresh Kumar }
9655a6338ccSViresh Kumar 
mhuv2_tx_init(struct amba_device * adev,struct mhuv2 * mhu,void __iomem * reg)9665a6338ccSViresh Kumar static int mhuv2_tx_init(struct amba_device *adev, struct mhuv2 *mhu,
9675a6338ccSViresh Kumar 			 void __iomem *reg)
9685a6338ccSViresh Kumar {
9695a6338ccSViresh Kumar 	struct device *dev = mhu->mbox.dev;
9705a6338ccSViresh Kumar 	int ret, i;
9715a6338ccSViresh Kumar 
9725a6338ccSViresh Kumar 	mhu->frame = SENDER_FRAME;
9735a6338ccSViresh Kumar 	mhu->mbox.ops = &mhuv2_sender_ops;
9745a6338ccSViresh Kumar 	mhu->send = reg;
9755a6338ccSViresh Kumar 
976af59bcffSViresh Kumar 	mhu->windows = readl_relaxed_bitfield(&mhu->send->mhu_cfg, struct mhu_cfg_t, num_ch);
977af59bcffSViresh Kumar 	mhu->minor = readl_relaxed_bitfield(&mhu->send->aidr, struct aidr_t, arch_minor_rev);
9785a6338ccSViresh Kumar 
9795a6338ccSViresh Kumar 	spin_lock_init(&mhu->doorbell_pending_lock);
9805a6338ccSViresh Kumar 
9815a6338ccSViresh Kumar 	/*
9825a6338ccSViresh Kumar 	 * For minor version 1 and forward, tx interrupt is provided by
9835a6338ccSViresh Kumar 	 * the controller.
9845a6338ccSViresh Kumar 	 */
9855a6338ccSViresh Kumar 	if (mhu->minor && adev->irq[0]) {
9865a6338ccSViresh Kumar 		ret = devm_request_threaded_irq(dev, adev->irq[0], NULL,
9875a6338ccSViresh Kumar 						mhuv2_sender_interrupt,
9885a6338ccSViresh Kumar 						IRQF_ONESHOT, "mhuv2-tx", mhu);
9895a6338ccSViresh Kumar 		if (ret) {
9905a6338ccSViresh Kumar 			dev_err(dev, "Failed to request tx IRQ, fallback to polling mode: %d\n",
9915a6338ccSViresh Kumar 				ret);
9925a6338ccSViresh Kumar 		} else {
9935a6338ccSViresh Kumar 			mhu->mbox.txdone_irq = true;
9945a6338ccSViresh Kumar 			mhu->mbox.txdone_poll = false;
9955a6338ccSViresh Kumar 			mhu->irq = adev->irq[0];
9965a6338ccSViresh Kumar 
997af59bcffSViresh Kumar 			writel_relaxed_bitfield(1, &mhu->send->int_en, struct int_en_t, chcomb);
9985a6338ccSViresh Kumar 
9995a6338ccSViresh Kumar 			/* Disable all channel interrupts */
10005a6338ccSViresh Kumar 			for (i = 0; i < mhu->windows; i++)
10015a6338ccSViresh Kumar 				writel_relaxed(0x0, &mhu->send->ch_wn[i].int_en);
10025a6338ccSViresh Kumar 
10035a6338ccSViresh Kumar 			goto out;
10045a6338ccSViresh Kumar 		}
10055a6338ccSViresh Kumar 	}
10065a6338ccSViresh Kumar 
10075a6338ccSViresh Kumar 	mhu->mbox.txdone_irq = false;
10085a6338ccSViresh Kumar 	mhu->mbox.txdone_poll = true;
10095a6338ccSViresh Kumar 	mhu->mbox.txpoll_period = 1;
10105a6338ccSViresh Kumar 
10115a6338ccSViresh Kumar out:
10125a6338ccSViresh Kumar 	/* Wait for receiver to be ready */
10135a6338ccSViresh Kumar 	writel_relaxed(0x1, &mhu->send->access_request);
10145a6338ccSViresh Kumar 	while (!readl_relaxed(&mhu->send->access_ready))
10155a6338ccSViresh Kumar 		continue;
10165a6338ccSViresh Kumar 
10175a6338ccSViresh Kumar 	return 0;
10185a6338ccSViresh Kumar }
10195a6338ccSViresh Kumar 
mhuv2_rx_init(struct amba_device * adev,struct mhuv2 * mhu,void __iomem * reg)10205a6338ccSViresh Kumar static int mhuv2_rx_init(struct amba_device *adev, struct mhuv2 *mhu,
10215a6338ccSViresh Kumar 			 void __iomem *reg)
10225a6338ccSViresh Kumar {
10235a6338ccSViresh Kumar 	struct device *dev = mhu->mbox.dev;
10245a6338ccSViresh Kumar 	int ret, i;
10255a6338ccSViresh Kumar 
10265a6338ccSViresh Kumar 	mhu->frame = RECEIVER_FRAME;
10275a6338ccSViresh Kumar 	mhu->mbox.ops = &mhuv2_receiver_ops;
10285a6338ccSViresh Kumar 	mhu->recv = reg;
10295a6338ccSViresh Kumar 
1030af59bcffSViresh Kumar 	mhu->windows = readl_relaxed_bitfield(&mhu->recv->mhu_cfg, struct mhu_cfg_t, num_ch);
1031af59bcffSViresh Kumar 	mhu->minor = readl_relaxed_bitfield(&mhu->recv->aidr, struct aidr_t, arch_minor_rev);
10325a6338ccSViresh Kumar 
10335a6338ccSViresh Kumar 	mhu->irq = adev->irq[0];
10345a6338ccSViresh Kumar 	if (!mhu->irq) {
10355a6338ccSViresh Kumar 		dev_err(dev, "Missing receiver IRQ\n");
10365a6338ccSViresh Kumar 		return -EINVAL;
10375a6338ccSViresh Kumar 	}
10385a6338ccSViresh Kumar 
10395a6338ccSViresh Kumar 	ret = devm_request_threaded_irq(dev, mhu->irq, NULL,
10405a6338ccSViresh Kumar 					mhuv2_receiver_interrupt, IRQF_ONESHOT,
10415a6338ccSViresh Kumar 					"mhuv2-rx", mhu);
10425a6338ccSViresh Kumar 	if (ret) {
10435a6338ccSViresh Kumar 		dev_err(dev, "Failed to request rx IRQ\n");
10445a6338ccSViresh Kumar 		return ret;
10455a6338ccSViresh Kumar 	}
10465a6338ccSViresh Kumar 
10475a6338ccSViresh Kumar 	/* Mask all the channel windows */
10485a6338ccSViresh Kumar 	for (i = 0; i < mhu->windows; i++)
10495a6338ccSViresh Kumar 		writel_relaxed(0xFFFFFFFF, &mhu->recv->ch_wn[i].mask_set);
10505a6338ccSViresh Kumar 
10515a6338ccSViresh Kumar 	if (mhu->minor)
1052af59bcffSViresh Kumar 		writel_relaxed_bitfield(1, &mhu->recv->int_en, struct int_en_t, chcomb);
10535a6338ccSViresh Kumar 
10545a6338ccSViresh Kumar 	return 0;
10555a6338ccSViresh Kumar }
10565a6338ccSViresh Kumar 
mhuv2_probe(struct amba_device * adev,const struct amba_id * id)10575a6338ccSViresh Kumar static int mhuv2_probe(struct amba_device *adev, const struct amba_id *id)
10585a6338ccSViresh Kumar {
10595a6338ccSViresh Kumar 	struct device *dev = &adev->dev;
10605a6338ccSViresh Kumar 	const struct device_node *np = dev->of_node;
10615a6338ccSViresh Kumar 	struct mhuv2 *mhu;
10625a6338ccSViresh Kumar 	void __iomem *reg;
10635a6338ccSViresh Kumar 	int ret = -EINVAL;
10645a6338ccSViresh Kumar 
10655a6338ccSViresh Kumar 	reg = devm_of_iomap(dev, dev->of_node, 0, NULL);
1066165b7643SYang Yingliang 	if (IS_ERR(reg))
1067165b7643SYang Yingliang 		return PTR_ERR(reg);
10685a6338ccSViresh Kumar 
10695a6338ccSViresh Kumar 	mhu = devm_kzalloc(dev, sizeof(*mhu), GFP_KERNEL);
10705a6338ccSViresh Kumar 	if (!mhu)
10715a6338ccSViresh Kumar 		return -ENOMEM;
10725a6338ccSViresh Kumar 
10735a6338ccSViresh Kumar 	mhu->mbox.dev = dev;
10745a6338ccSViresh Kumar 	mhu->mbox.of_xlate = mhuv2_mbox_of_xlate;
10755a6338ccSViresh Kumar 
10765a6338ccSViresh Kumar 	if (of_device_is_compatible(np, "arm,mhuv2-tx"))
10775a6338ccSViresh Kumar 		ret = mhuv2_tx_init(adev, mhu, reg);
10785a6338ccSViresh Kumar 	else if (of_device_is_compatible(np, "arm,mhuv2-rx"))
10795a6338ccSViresh Kumar 		ret = mhuv2_rx_init(adev, mhu, reg);
10805a6338ccSViresh Kumar 	else
10815a6338ccSViresh Kumar 		dev_err(dev, "Invalid compatible property\n");
10825a6338ccSViresh Kumar 
10835a6338ccSViresh Kumar 	if (ret)
10845a6338ccSViresh Kumar 		return ret;
10855a6338ccSViresh Kumar 
10865a6338ccSViresh Kumar 	/* Channel windows can't be 0 */
10875a6338ccSViresh Kumar 	BUG_ON(!mhu->windows);
10885a6338ccSViresh Kumar 
10895a6338ccSViresh Kumar 	ret = mhuv2_parse_channels(mhu);
10905a6338ccSViresh Kumar 	if (ret)
10915a6338ccSViresh Kumar 		return ret;
10925a6338ccSViresh Kumar 
10935a6338ccSViresh Kumar 	amba_set_drvdata(adev, mhu);
10945a6338ccSViresh Kumar 
10955a6338ccSViresh Kumar 	ret = devm_mbox_controller_register(dev, &mhu->mbox);
10965a6338ccSViresh Kumar 	if (ret)
10975a6338ccSViresh Kumar 		dev_err(dev, "failed to register ARM MHUv2 driver %d\n", ret);
10985a6338ccSViresh Kumar 
10995a6338ccSViresh Kumar 	return ret;
11005a6338ccSViresh Kumar }
11015a6338ccSViresh Kumar 
mhuv2_remove(struct amba_device * adev)110209d12157SUwe Kleine-König static void mhuv2_remove(struct amba_device *adev)
11035a6338ccSViresh Kumar {
11045a6338ccSViresh Kumar 	struct mhuv2 *mhu = amba_get_drvdata(adev);
11055a6338ccSViresh Kumar 
11065a6338ccSViresh Kumar 	if (mhu->frame == SENDER_FRAME)
11075a6338ccSViresh Kumar 		writel_relaxed(0x0, &mhu->send->access_request);
11085a6338ccSViresh Kumar }
11095a6338ccSViresh Kumar 
11105a6338ccSViresh Kumar static struct amba_id mhuv2_ids[] = {
11115a6338ccSViresh Kumar 	{
11125a6338ccSViresh Kumar 		/* 2.0 */
11135a6338ccSViresh Kumar 		.id = 0xbb0d1,
11145a6338ccSViresh Kumar 		.mask = 0xfffff,
11155a6338ccSViresh Kumar 	},
11165a6338ccSViresh Kumar 	{
11175a6338ccSViresh Kumar 		/* 2.1 */
11185a6338ccSViresh Kumar 		.id = 0xbb076,
11195a6338ccSViresh Kumar 		.mask = 0xfffff,
11205a6338ccSViresh Kumar 	},
11215a6338ccSViresh Kumar 	{ 0, 0 },
11225a6338ccSViresh Kumar };
11235a6338ccSViresh Kumar MODULE_DEVICE_TABLE(amba, mhuv2_ids);
11245a6338ccSViresh Kumar 
11255a6338ccSViresh Kumar static struct amba_driver mhuv2_driver = {
11265a6338ccSViresh Kumar 	.drv = {
11275a6338ccSViresh Kumar 		.name	= "arm-mhuv2",
11285a6338ccSViresh Kumar 	},
11295a6338ccSViresh Kumar 	.id_table	= mhuv2_ids,
11305a6338ccSViresh Kumar 	.probe		= mhuv2_probe,
11315a6338ccSViresh Kumar 	.remove		= mhuv2_remove,
11325a6338ccSViresh Kumar };
11335a6338ccSViresh Kumar module_amba_driver(mhuv2_driver);
11345a6338ccSViresh Kumar 
11355a6338ccSViresh Kumar MODULE_LICENSE("GPL v2");
11365a6338ccSViresh Kumar MODULE_DESCRIPTION("ARM MHUv2 Driver");
11375a6338ccSViresh Kumar MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
11385a6338ccSViresh Kumar MODULE_AUTHOR("Tushar Khandelwal <tushar.khandelwal@arm.com>");
1139