blob: f82dc89266879b4dca394e574be4dbc382a1f0e1 [file] [log] [blame]
Lee Jones8ea44842015-10-16 08:21:30 +01001/*
2 * Copyright (C) 2015 ST Microelectronics
3 *
4 * Author: Lee Jones <lee.jones@linaro.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/debugfs.h>
13#include <linux/err.h>
Lee Jonesa133f8b2015-10-20 17:11:27 +010014#include <linux/io.h>
Lee Jones8ea44842015-10-16 08:21:30 +010015#include <linux/kernel.h>
16#include <linux/mailbox_client.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/platform_device.h>
20#include <linux/slab.h>
21#include <linux/uaccess.h>
22
23#define MBOX_MAX_SIG_LEN 8
24#define MBOX_MAX_MSG_LEN 128
25#define MBOX_BYTES_PER_LINE 16
Lee Jones6c036632015-10-16 13:32:47 +010026#define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2)
Lee Jones8ea44842015-10-16 08:21:30 +010027#define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \
28 (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
29
30static struct dentry *root_debugfs_dir;
31
32struct mbox_test_device {
33 struct device *dev;
Lee Jonesa133f8b2015-10-20 17:11:27 +010034 void __iomem *mmio;
Lee Jones8ea44842015-10-16 08:21:30 +010035 struct mbox_chan *tx_channel;
36 struct mbox_chan *rx_channel;
37 char *rx_buffer;
38 char *signal;
39 char *message;
40 spinlock_t lock;
41};
42
43static ssize_t mbox_test_signal_write(struct file *filp,
44 const char __user *userbuf,
45 size_t count, loff_t *ppos)
46{
47 struct mbox_test_device *tdev = filp->private_data;
48 int ret;
49
50 if (!tdev->tx_channel) {
51 dev_err(tdev->dev, "Channel cannot do Tx\n");
52 return -EINVAL;
53 }
54
55 if (count > MBOX_MAX_SIG_LEN) {
56 dev_err(tdev->dev,
Lee Jones6c036632015-10-16 13:32:47 +010057 "Signal length %zd greater than max allowed %d\n",
Lee Jones8ea44842015-10-16 08:21:30 +010058 count, MBOX_MAX_SIG_LEN);
59 return -EINVAL;
60 }
61
62 tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
63 if (!tdev->signal)
64 return -ENOMEM;
65
66 ret = copy_from_user(tdev->signal, userbuf, count);
67 if (ret) {
68 kfree(tdev->signal);
69 return -EFAULT;
70 }
71
72 return ret < 0 ? ret : count;
73}
74
75static const struct file_operations mbox_test_signal_ops = {
76 .write = mbox_test_signal_write,
77 .open = simple_open,
78 .llseek = generic_file_llseek,
79};
80
81static ssize_t mbox_test_message_write(struct file *filp,
82 const char __user *userbuf,
83 size_t count, loff_t *ppos)
84{
85 struct mbox_test_device *tdev = filp->private_data;
86 void *data;
87 int ret;
88
89 if (!tdev->tx_channel) {
90 dev_err(tdev->dev, "Channel cannot do Tx\n");
91 return -EINVAL;
92 }
93
94 if (count > MBOX_MAX_MSG_LEN) {
95 dev_err(tdev->dev,
Lee Jones6c036632015-10-16 13:32:47 +010096 "Message length %zd greater than max allowed %d\n",
Lee Jones8ea44842015-10-16 08:21:30 +010097 count, MBOX_MAX_MSG_LEN);
98 return -EINVAL;
99 }
100
101 tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
102 if (!tdev->message)
103 return -ENOMEM;
104
105 ret = copy_from_user(tdev->message, userbuf, count);
106 if (ret) {
107 ret = -EFAULT;
108 goto out;
109 }
110
111 /*
112 * A separate signal is only of use if there is
113 * MMIO to subsequently pass the message through
114 */
115 if (tdev->mmio && tdev->signal) {
116 print_hex_dump(KERN_INFO, "Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
117 MBOX_BYTES_PER_LINE, 1, tdev->signal, MBOX_MAX_SIG_LEN, true);
118
119 data = tdev->signal;
120 } else
121 data = tdev->message;
122
123 print_hex_dump(KERN_INFO, "Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
124 MBOX_BYTES_PER_LINE, 1, tdev->message, MBOX_MAX_MSG_LEN, true);
125
126 ret = mbox_send_message(tdev->tx_channel, data);
127 if (ret < 0)
128 dev_err(tdev->dev, "Failed to send message via mailbox\n");
129
130out:
131 kfree(tdev->signal);
132 kfree(tdev->message);
133
134 return ret < 0 ? ret : count;
135}
136
137static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
138 size_t count, loff_t *ppos)
139{
140 struct mbox_test_device *tdev = filp->private_data;
141 unsigned long flags;
142 char *touser, *ptr;
143 int l = 0;
144 int ret;
145
Dan Carpenterc3ac54a2015-10-22 22:51:27 +0300146 touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
Lee Jones8ea44842015-10-16 08:21:30 +0100147 if (!touser)
148 return -ENOMEM;
149
150 if (!tdev->rx_channel) {
151 ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
152 ret = simple_read_from_buffer(userbuf, count, ppos,
153 touser, ret);
154 goto out;
155 }
156
157 if (tdev->rx_buffer[0] == '\0') {
158 ret = snprintf(touser, 9, "<EMPTY>\n");
159 ret = simple_read_from_buffer(userbuf, count, ppos,
160 touser, ret);
161 goto out;
162 }
163
164 spin_lock_irqsave(&tdev->lock, flags);
165
166 ptr = tdev->rx_buffer;
167 while (l < MBOX_HEXDUMP_MAX_LEN) {
168 hex_dump_to_buffer(ptr,
169 MBOX_BYTES_PER_LINE,
170 MBOX_BYTES_PER_LINE, 1, touser + l,
171 MBOX_HEXDUMP_LINE_LEN, true);
172
173 ptr += MBOX_BYTES_PER_LINE;
174 l += MBOX_HEXDUMP_LINE_LEN;
175 *(touser + (l - 1)) = '\n';
176 }
177 *(touser + l) = '\0';
178
179 memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
180
181 spin_unlock_irqrestore(&tdev->lock, flags);
182
183 ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
184out:
185 kfree(touser);
186 return ret;
187}
188
189static const struct file_operations mbox_test_message_ops = {
190 .write = mbox_test_message_write,
191 .read = mbox_test_message_read,
192 .open = simple_open,
193 .llseek = generic_file_llseek,
194};
195
196static int mbox_test_add_debugfs(struct platform_device *pdev,
197 struct mbox_test_device *tdev)
198{
199 if (!debugfs_initialized())
200 return 0;
201
202 root_debugfs_dir = debugfs_create_dir("mailbox", NULL);
203 if (!root_debugfs_dir) {
204 dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
205 return -EINVAL;
206 }
207
208 debugfs_create_file("message", 0600, root_debugfs_dir,
209 tdev, &mbox_test_message_ops);
210
211 debugfs_create_file("signal", 0200, root_debugfs_dir,
212 tdev, &mbox_test_signal_ops);
213
214 return 0;
215}
216
217static void mbox_test_receive_message(struct mbox_client *client, void *message)
218{
219 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
220 unsigned long flags;
221
222 spin_lock_irqsave(&tdev->lock, flags);
223 if (tdev->mmio) {
224 print_hex_dump(KERN_INFO, "Client: Received [MMIO]: ",
225 DUMP_PREFIX_ADDRESS, MBOX_BYTES_PER_LINE, 1,
Lee Jonesa133f8b2015-10-20 17:11:27 +0100226 __io_virt(tdev->mmio), MBOX_MAX_MSG_LEN, true);
227 memcpy_fromio(tdev->rx_buffer, tdev->mmio, MBOX_MAX_MSG_LEN);
Lee Jones8ea44842015-10-16 08:21:30 +0100228
229 } else if (message) {
230 print_hex_dump(KERN_INFO, "Client: Received [API]: ",
231 DUMP_PREFIX_ADDRESS, MBOX_BYTES_PER_LINE, 1,
232 message, MBOX_MAX_MSG_LEN, true);
233 memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
234 }
235 spin_unlock_irqrestore(&tdev->lock, flags);
236}
237
238static void mbox_test_prepare_message(struct mbox_client *client, void *message)
239{
240 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
241
242 if (tdev->mmio) {
243 if (tdev->signal)
Lee Jonesa133f8b2015-10-20 17:11:27 +0100244 memcpy_toio(tdev->mmio, tdev->message, MBOX_MAX_MSG_LEN);
Lee Jones8ea44842015-10-16 08:21:30 +0100245 else
Lee Jonesa133f8b2015-10-20 17:11:27 +0100246 memcpy_toio(tdev->mmio, message, MBOX_MAX_MSG_LEN);
Lee Jones8ea44842015-10-16 08:21:30 +0100247 }
248}
249
250static void mbox_test_message_sent(struct mbox_client *client,
251 void *message, int r)
252{
253 if (r)
254 dev_warn(client->dev,
255 "Client: Message could not be sent: %d\n", r);
256 else
257 dev_info(client->dev,
258 "Client: Message sent\n");
259}
260
261static struct mbox_chan *
262mbox_test_request_channel(struct platform_device *pdev, const char *name)
263{
264 struct mbox_client *client;
265 struct mbox_chan *channel;
266
267 client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
268 if (!client)
269 return ERR_PTR(-ENOMEM);
270
271 client->dev = &pdev->dev;
272 client->rx_callback = mbox_test_receive_message;
273 client->tx_prepare = mbox_test_prepare_message;
274 client->tx_done = mbox_test_message_sent;
275 client->tx_block = true;
276 client->knows_txdone = false;
277 client->tx_tout = 500;
278
279 channel = mbox_request_channel_byname(client, name);
280 if (IS_ERR(channel)) {
281 dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
282 return NULL;
283 }
284
285 return channel;
286}
287
288static int mbox_test_probe(struct platform_device *pdev)
289{
290 struct mbox_test_device *tdev;
291 struct resource *res;
292 int ret;
293
294 tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
295 if (!tdev)
296 return -ENOMEM;
297
298 /* It's okay for MMIO to be NULL */
299 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
300 tdev->mmio = devm_ioremap_resource(&pdev->dev, res);
301 if (IS_ERR(tdev->mmio))
302 tdev->mmio = NULL;
303
304 tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
305 tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
306
Lee Jones6c036632015-10-16 13:32:47 +0100307 if (!tdev->tx_channel && !tdev->rx_channel)
Lee Jones8ea44842015-10-16 08:21:30 +0100308 return -EPROBE_DEFER;
309
310 tdev->dev = &pdev->dev;
311 platform_set_drvdata(pdev, tdev);
312
313 spin_lock_init(&tdev->lock);
314
315 if (tdev->rx_channel) {
316 tdev->rx_buffer = devm_kzalloc(&pdev->dev,
317 MBOX_MAX_MSG_LEN, GFP_KERNEL);
318 if (!tdev->rx_buffer)
319 return -ENOMEM;
320 }
321
322 ret = mbox_test_add_debugfs(pdev, tdev);
323 if (ret)
324 return ret;
325
326 dev_info(&pdev->dev, "Successfully registered\n");
327
328 return 0;
329}
330
331static int mbox_test_remove(struct platform_device *pdev)
332{
333 struct mbox_test_device *tdev = platform_get_drvdata(pdev);
334
335 debugfs_remove_recursive(root_debugfs_dir);
336
337 if (tdev->tx_channel)
338 mbox_free_channel(tdev->tx_channel);
339 if (tdev->rx_channel)
340 mbox_free_channel(tdev->rx_channel);
341
342 return 0;
343}
344
345static const struct of_device_id mbox_test_match[] = {
346 { .compatible = "mailbox_test" },
347 {},
348};
349
350static struct platform_driver mbox_test_driver = {
351 .driver = {
352 .name = "mailbox_sti_test",
353 .of_match_table = mbox_test_match,
354 },
355 .probe = mbox_test_probe,
356 .remove = mbox_test_remove,
357};
358module_platform_driver(mbox_test_driver);
359
360MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
361MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
362MODULE_LICENSE("GPL v2");