1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020-2021 NXP
4  */
5 
6 #include <linux/init.h>
7 #include <linux/interconnect.h>
8 #include <linux/ioctl.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/of_address.h>
14 #include <linux/platform_device.h>
15 #include "vpu.h"
16 #include "vpu_mbox.h"
17 #include "vpu_msgs.h"
18 
19 static void vpu_mbox_rx_callback(struct mbox_client *cl, void *msg)
20 {
21 	struct vpu_mbox *rx = container_of(cl, struct vpu_mbox, cl);
22 	struct vpu_core *core = container_of(rx, struct vpu_core, rx);
23 
24 	vpu_isr(core, *(u32 *)msg);
25 }
26 
27 static int vpu_mbox_request_channel(struct device *dev, struct vpu_mbox *mbox)
28 {
29 	struct mbox_chan *ch;
30 	struct mbox_client *cl;
31 
32 	if (!dev || !mbox)
33 		return -EINVAL;
34 	if (mbox->ch)
35 		return 0;
36 
37 	cl = &mbox->cl;
38 	cl->dev = dev;
39 	if (mbox->block) {
40 		cl->tx_block = true;
41 		cl->tx_tout = 1000;
42 	} else {
43 		cl->tx_block = false;
44 	}
45 	cl->knows_txdone = false;
46 	cl->rx_callback = vpu_mbox_rx_callback;
47 
48 	ch = mbox_request_channel_byname(cl, mbox->name);
49 	if (IS_ERR(ch))
50 		return dev_err_probe(dev, PTR_ERR(ch),
51 				     "Failed to request mbox chan %s\n",
52 				     mbox->name);
53 
54 	mbox->ch = ch;
55 	return 0;
56 }
57 
58 int vpu_mbox_init(struct vpu_core *core)
59 {
60 	scnprintf(core->tx_type.name, sizeof(core->tx_type.name) - 1, "tx0");
61 	core->tx_type.block = true;
62 
63 	scnprintf(core->tx_data.name, sizeof(core->tx_data.name) - 1, "tx1");
64 	core->tx_data.block = false;
65 
66 	scnprintf(core->rx.name, sizeof(core->rx.name) - 1, "rx");
67 	core->rx.block = true;
68 
69 	return 0;
70 }
71 
72 int vpu_mbox_request(struct vpu_core *core)
73 {
74 	int ret;
75 
76 	ret = vpu_mbox_request_channel(core->dev, &core->tx_type);
77 	if (ret)
78 		goto error;
79 	ret = vpu_mbox_request_channel(core->dev, &core->tx_data);
80 	if (ret)
81 		goto error;
82 	ret = vpu_mbox_request_channel(core->dev, &core->rx);
83 	if (ret)
84 		goto error;
85 
86 	dev_dbg(core->dev, "%s request mbox\n", vpu_core_type_desc(core->type));
87 	return 0;
88 error:
89 	vpu_mbox_free(core);
90 	return ret;
91 }
92 
93 void vpu_mbox_free(struct vpu_core *core)
94 {
95 	mbox_free_channel(core->tx_type.ch);
96 	mbox_free_channel(core->tx_data.ch);
97 	mbox_free_channel(core->rx.ch);
98 	core->tx_type.ch = NULL;
99 	core->tx_data.ch = NULL;
100 	core->rx.ch = NULL;
101 	dev_dbg(core->dev, "%s free mbox\n", vpu_core_type_desc(core->type));
102 }
103 
104 void vpu_mbox_send_type(struct vpu_core *core, u32 type)
105 {
106 	mbox_send_message(core->tx_type.ch, &type);
107 }
108 
109 void vpu_mbox_send_msg(struct vpu_core *core, u32 type, u32 data)
110 {
111 	mbox_send_message(core->tx_data.ch, &data);
112 	mbox_send_message(core->tx_type.ch, &type);
113 }
114 
115 void vpu_mbox_enable_rx(struct vpu_dev *dev)
116 {
117 }
118