2 * Copyright (C) 2015 ST Microelectronics
4 * Author: Lee Jones <lee.jones@linaro.org>
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.
12 #include <linux/debugfs.h>
13 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/mailbox_client.h>
17 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
23 #define MBOX_MAX_SIG_LEN 8
24 #define MBOX_MAX_MSG_LEN 128
25 #define MBOX_BYTES_PER_LINE 16
26 #define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2)
27 #define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \
28 (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
30 static struct dentry *root_debugfs_dir;
32 struct mbox_test_device {
34 void __iomem *tx_mmio;
35 void __iomem *rx_mmio;
36 struct mbox_chan *tx_channel;
37 struct mbox_chan *rx_channel;
44 static ssize_t mbox_test_signal_write(struct file *filp,
45 const char __user *userbuf,
46 size_t count, loff_t *ppos)
48 struct mbox_test_device *tdev = filp->private_data;
50 if (!tdev->tx_channel) {
51 dev_err(tdev->dev, "Channel cannot do Tx\n");
55 if (count > MBOX_MAX_SIG_LEN) {
57 "Signal length %zd greater than max allowed %d\n",
58 count, MBOX_MAX_SIG_LEN);
62 /* Only allocate memory if we need to */
64 tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
69 if (copy_from_user(tdev->signal, userbuf, count)) {
78 static const struct file_operations mbox_test_signal_ops = {
79 .write = mbox_test_signal_write,
81 .llseek = generic_file_llseek,
84 static ssize_t mbox_test_message_write(struct file *filp,
85 const char __user *userbuf,
86 size_t count, loff_t *ppos)
88 struct mbox_test_device *tdev = filp->private_data;
92 if (!tdev->tx_channel) {
93 dev_err(tdev->dev, "Channel cannot do Tx\n");
97 if (count > MBOX_MAX_MSG_LEN) {
99 "Message length %zd greater than max allowed %d\n",
100 count, MBOX_MAX_MSG_LEN);
104 tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
108 ret = copy_from_user(tdev->message, userbuf, count);
115 * A separate signal is only of use if there is
116 * MMIO to subsequently pass the message through
118 if (tdev->tx_mmio && tdev->signal) {
119 print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
120 tdev->signal, MBOX_MAX_SIG_LEN);
124 data = tdev->message;
126 print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
127 tdev->message, MBOX_MAX_MSG_LEN);
129 ret = mbox_send_message(tdev->tx_channel, data);
131 dev_err(tdev->dev, "Failed to send message via mailbox\n");
135 kfree(tdev->message);
138 return ret < 0 ? ret : count;
141 static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
142 size_t count, loff_t *ppos)
144 struct mbox_test_device *tdev = filp->private_data;
150 touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
154 if (!tdev->rx_channel) {
155 ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
156 ret = simple_read_from_buffer(userbuf, count, ppos,
161 if (tdev->rx_buffer[0] == '\0') {
162 ret = snprintf(touser, 9, "<EMPTY>\n");
163 ret = simple_read_from_buffer(userbuf, count, ppos,
168 spin_lock_irqsave(&tdev->lock, flags);
170 ptr = tdev->rx_buffer;
171 while (l < MBOX_HEXDUMP_MAX_LEN) {
172 hex_dump_to_buffer(ptr,
174 MBOX_BYTES_PER_LINE, 1, touser + l,
175 MBOX_HEXDUMP_LINE_LEN, true);
177 ptr += MBOX_BYTES_PER_LINE;
178 l += MBOX_HEXDUMP_LINE_LEN;
179 *(touser + (l - 1)) = '\n';
181 *(touser + l) = '\0';
183 memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
185 spin_unlock_irqrestore(&tdev->lock, flags);
187 ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
193 static const struct file_operations mbox_test_message_ops = {
194 .write = mbox_test_message_write,
195 .read = mbox_test_message_read,
197 .llseek = generic_file_llseek,
200 static int mbox_test_add_debugfs(struct platform_device *pdev,
201 struct mbox_test_device *tdev)
203 if (!debugfs_initialized())
206 root_debugfs_dir = debugfs_create_dir("mailbox", NULL);
207 if (!root_debugfs_dir) {
208 dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
212 debugfs_create_file("message", 0600, root_debugfs_dir,
213 tdev, &mbox_test_message_ops);
215 debugfs_create_file("signal", 0200, root_debugfs_dir,
216 tdev, &mbox_test_signal_ops);
221 static void mbox_test_receive_message(struct mbox_client *client, void *message)
223 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
226 spin_lock_irqsave(&tdev->lock, flags);
228 memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN);
229 print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS,
230 tdev->rx_buffer, MBOX_MAX_MSG_LEN);
231 } else if (message) {
232 print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS,
233 message, MBOX_MAX_MSG_LEN);
234 memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
236 spin_unlock_irqrestore(&tdev->lock, flags);
239 static void mbox_test_prepare_message(struct mbox_client *client, void *message)
241 struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
245 memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
247 memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
251 static void mbox_test_message_sent(struct mbox_client *client,
252 void *message, int r)
255 dev_warn(client->dev,
256 "Client: Message could not be sent: %d\n", r);
258 dev_info(client->dev,
259 "Client: Message sent\n");
262 static struct mbox_chan *
263 mbox_test_request_channel(struct platform_device *pdev, const char *name)
265 struct mbox_client *client;
266 struct mbox_chan *channel;
268 client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
270 return ERR_PTR(-ENOMEM);
272 client->dev = &pdev->dev;
273 client->rx_callback = mbox_test_receive_message;
274 client->tx_prepare = mbox_test_prepare_message;
275 client->tx_done = mbox_test_message_sent;
276 client->tx_block = true;
277 client->knows_txdone = false;
278 client->tx_tout = 500;
280 channel = mbox_request_channel_byname(client, name);
281 if (IS_ERR(channel)) {
282 dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
289 static int mbox_test_probe(struct platform_device *pdev)
291 struct mbox_test_device *tdev;
292 struct resource *res;
295 tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
299 /* It's okay for MMIO to be NULL */
300 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
301 tdev->tx_mmio = devm_ioremap_resource(&pdev->dev, res);
302 if (IS_ERR(tdev->tx_mmio))
303 tdev->tx_mmio = NULL;
305 /* If specified, second reg entry is Rx MMIO */
306 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
307 tdev->rx_mmio = devm_ioremap_resource(&pdev->dev, res);
308 if (IS_ERR(tdev->rx_mmio))
309 tdev->rx_mmio = tdev->tx_mmio;
311 tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
312 tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
314 if (!tdev->tx_channel && !tdev->rx_channel)
315 return -EPROBE_DEFER;
317 /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
318 if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
319 tdev->rx_channel = tdev->tx_channel;
321 tdev->dev = &pdev->dev;
322 platform_set_drvdata(pdev, tdev);
324 spin_lock_init(&tdev->lock);
326 if (tdev->rx_channel) {
327 tdev->rx_buffer = devm_kzalloc(&pdev->dev,
328 MBOX_MAX_MSG_LEN, GFP_KERNEL);
329 if (!tdev->rx_buffer)
333 ret = mbox_test_add_debugfs(pdev, tdev);
337 dev_info(&pdev->dev, "Successfully registered\n");
342 static int mbox_test_remove(struct platform_device *pdev)
344 struct mbox_test_device *tdev = platform_get_drvdata(pdev);
346 debugfs_remove_recursive(root_debugfs_dir);
348 if (tdev->tx_channel)
349 mbox_free_channel(tdev->tx_channel);
350 if (tdev->rx_channel)
351 mbox_free_channel(tdev->rx_channel);
356 static const struct of_device_id mbox_test_match[] = {
357 { .compatible = "mailbox-test" },
361 static struct platform_driver mbox_test_driver = {
363 .name = "mailbox_test",
364 .of_match_table = mbox_test_match,
366 .probe = mbox_test_probe,
367 .remove = mbox_test_remove,
369 module_platform_driver(mbox_test_driver);
371 MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
372 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
373 MODULE_LICENSE("GPL v2");