aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/imx_mcc/imx_mcc_demo.c
blob: 6882d81dee8cf473e662088434fee88ce2258171 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * Copyright (C) 2014 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#include <linux/delay.h>
#include <linux/err.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/suspend.h>

#include <linux/imx_sema4.h>
#include <linux/mcc_common.h>
#include <linux/mcc_api.h>
#include <linux/mcc_linux.h>
#include <linux/mcc_imx6sx.h>

enum {
	MCC_NODE_A9 = 0,
	MCC_NODE_M4 = 0,

	MCC_A9_PORT = 1,
	MCC_M4_PORT = 2,
};

/* mcc pingpong demo */
MCC_ENDPOINT mcc_endpoint_a9_pingpong = {0, MCC_NODE_A9, MCC_A9_PORT};
MCC_ENDPOINT mcc_endpoint_m4_pingpong = {1, MCC_NODE_M4, MCC_M4_PORT};
/* mcc can demo */
MCC_ENDPOINT mcc_endpoint_a9_can = {0, MCC_NODE_A9, MCC_A9_PORT};

struct mcc_pp_msg {
	unsigned int data;
};

/* Set the max len of the can msg to be 1000 bytes */
struct mcc_can_msg {
	char data[MCC_ATTR_BUFFER_SIZE_IN_BYTES - 24];
};

static ssize_t imx_mcc_can_demo_en(struct device *dev,
		struct device_attribute *attr,
		const char *buf, size_t count)
{
	u32 can_demo_en;
	int i = 0, ret = 0;
	struct mcc_can_msg msg;
	MCC_MEM_SIZE num_of_received_bytes;
	MCC_INFO_STRUCT mcc_info;

	sscanf(buf, "%d\n", &can_demo_en);

	if (can_demo_en) {
		pr_info("imx mcc can communication demo begin.\n");
		ret = mcc_get_info(MCC_NODE_A9, &mcc_info);
		if (ret) {
			pr_err("failed to get mcc info.\n");
			return -EINVAL;
		}

		ret = mcc_create_endpoint(&mcc_endpoint_a9_can, MCC_A9_PORT);
		if (ret) {
			pr_err("failed to create a9 mcc ep.\n");
			return -EINVAL;
		}

		pr_info("\nA9 mcc prepares run, MCC version is %s\n",
				mcc_info.version_string);

		while (i < 0x10000) {
			i++;
			/*
			 * wait for the "sleep" msg from the remote ep.
			 */
			ret = mcc_recv_copy(&mcc_endpoint_a9_can, &msg,
					sizeof(struct mcc_can_msg),
					&num_of_received_bytes, 0xffffffff);
			pr_info("%s", msg.data);
		}

		ret = mcc_destroy_endpoint(&mcc_endpoint_a9_can);
		if (ret) {
			pr_err("failed to destory a9 mcc ep.\n");
			return -EINVAL;
		} else {
			pr_info("destory a9 mcc ep.\n");
		}
	}

	pr_info("imx mcc demo end after %08d times recv tests.\n", i);
	return count;
}

static ssize_t imx_mcc_pingpong_en(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	u32 pingpong_en;
	int i = 0, ret = 0;
	struct timeval tv1, tv2, tv3;
	u32 tv_count1 = 0, tv_count2 = 0;
	struct mcc_pp_msg msg;
	MCC_MEM_SIZE num_of_received_bytes;
	MCC_INFO_STRUCT mcc_info;

	sscanf(buf, "%d\n", &pingpong_en);
	if (pingpong_en) {
		pr_info("imx mcc pingpong demo begin.\n");
		ret = mcc_get_info(MCC_NODE_A9, &mcc_info);
		if (ret) {
			pr_err("failed to get mcc info.\n");
			return -EINVAL;
		}

		ret = mcc_create_endpoint(&mcc_endpoint_a9_pingpong,
				MCC_A9_PORT);
		if (ret) {
			pr_err("failed to create a9 mcc ep.\n");
			return -EINVAL;
		}

		pr_info("\nA9 mcc prepares run, MCC version is %s\n",
				mcc_info.version_string);
		msg.data = 1;
		while (i < 0x50000) {
			i++;
			i++;
			/*
			 * wait until the remote endpoint is created by
			 * the other core
			 */
			if (pingpong_en > 1)
				do_gettimeofday(&tv1);

			ret = mcc_send(&mcc_endpoint_m4_pingpong, &msg,
					sizeof(struct mcc_pp_msg),
					0xffffffff);

			if (pingpong_en > 1) {
				do_gettimeofday(&tv2);
				tv_count1 = (tv2.tv_sec - tv1.tv_sec)
					* USEC_PER_SEC
					+ tv2.tv_usec - tv1.tv_usec;
			}
			while (MCC_ERR_ENDPOINT == ret) {
				pr_err("\n send err ret %d, re-send\n", ret);
				ret = mcc_send(&mcc_endpoint_m4_pingpong, &msg,
						sizeof(struct mcc_pp_msg),
						0xffffffff);
				msleep(5000);
			}

			if (pingpong_en > 1)
				do_gettimeofday(&tv2);

			ret = mcc_recv_copy(&mcc_endpoint_a9_pingpong, &msg,
					sizeof(struct mcc_pp_msg),
					&num_of_received_bytes, 0xffffffff);

			if (pingpong_en > 1) {
				do_gettimeofday(&tv3);
				tv_count2 = (tv3.tv_sec - tv2.tv_sec)
					* USEC_PER_SEC
					+ tv3.tv_usec - tv2.tv_usec;
				pr_info("imx mcc: Data transfer speed tests"
						"in pingpong. a9 -> m4:%08dus."
						"a9 <- m4:%08dus.\n",
						tv_count1, tv_count2);
			}

			if (MCC_SUCCESS != ret) {
				pr_err("A9 Main task receive error: %d\n", ret);
			} else {
				pr_info("%08x Main task received a msg\n", i);
				pr_info("Message: Size=0x%08x, data = 0x%08x\n",
					num_of_received_bytes, msg.data);
				msg.data++;
			}
		}
		ret = mcc_destroy_endpoint(&mcc_endpoint_a9_pingpong);
		if (ret) {
			pr_err("failed to destory a9 mcc ep.\n");
			return ret;
		} else {
			pr_info("destory a9 mcc ep.\n");
		}
		pr_info("imx mcc demo end after %08d times tests.\n", i/2);
	}

	if (ret)
		return ret;
	else
		return count;
}

static DEVICE_ATTR(pingpong_en, S_IWUGO, NULL, imx_mcc_pingpong_en);
static DEVICE_ATTR(can_demo_en, S_IWUGO, NULL, imx_mcc_can_demo_en);

static struct attribute *imx_mcc_attrs[] = {
	&dev_attr_pingpong_en.attr,
	&dev_attr_can_demo_en.attr,
	NULL
};

static struct attribute_group imx_mcc_attrgroup = {
	.attrs	= imx_mcc_attrs,
};

static int imx_mcc_demo_probe(struct platform_device *pdev)
{
	int ret = 0;
	MCC_INFO_STRUCT mcc_info;

	ret = mcc_initialize(MCC_NODE_A9);
	if (ret) {
		pr_err("failed to initialize mcc.\n");
		ret = -EINVAL;
		return ret;
	}

	ret = mcc_get_info(MCC_NODE_A9, &mcc_info);
	if (ret) {
		pr_err("failed to get mcc info.\n");
		ret = -EINVAL;
		goto out_node;
	}
	pr_info("\nA9 mcc prepares run, MCC version is %s\n",
			mcc_info.version_string);

	if (strcmp(mcc_info.version_string, MCC_VERSION_STRING) != 0) {
		pr_err("\nError, different versions of the MCC library");
		pr_err("is used on each core!\n");
		pr_err("\nApplication is stopped now.\n");
		ret = -EINVAL;
		goto out_node;
	}

	/* add attributes for device. */
	ret = sysfs_create_group(&pdev->dev.kobj, &imx_mcc_attrgroup);
	if (ret)
		goto out_node;

	return ret;

out_node:
	mcc_destroy(MCC_NODE_A9);

	return ret;
}

static int imx_mcc_demo_remove(struct platform_device *pdev)
{
	return 0;
}

static const struct of_device_id imx_mcc_demo_dt_ids[] = {
	{ .compatible = "fsl,imx6sx-mcc-demo", },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, imx_mcc_demo_dt_ids);

static struct platform_driver imx_mcc_demo_driver = {
	.driver = {
		   .owner = THIS_MODULE,
		   .name = "imx6sx-mcc-demo",
		   .of_match_table = imx_mcc_demo_dt_ids,
		   },
	.probe = imx_mcc_demo_probe,
	.remove = imx_mcc_demo_remove,
};

static int __init imx_mcc_demo_init(void)
{
	int ret;

	ret = platform_driver_register(&imx_mcc_demo_driver);
	if (ret)
		pr_err("failed to register imx mcc demo driver.\n");
	else
		pr_info("imx mcc demo is registered.\n");
	return ret;
}
late_initcall(imx_mcc_demo_init);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("IMX MCC demo driver");
MODULE_LICENSE("GPL");