1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip PolarFire SoC (MPFS) system controller driver
5 * Copyright (c) 2020-2021 Microchip Corporation. All rights reserved.
11 #include <linux/slab.h>
12 #include <linux/kref.h>
13 #include <linux/module.h>
14 #include <linux/jiffies.h>
15 #include <linux/interrupt.h>
16 #include <linux/of_platform.h>
17 #include <linux/mailbox_client.h>
18 #include <linux/platform_device.h>
19 #include <soc/microchip/mpfs.h>
22 * This timeout must be long, as some services (example: image authentication)
23 * take significant time to complete
25 #define MPFS_SYS_CTRL_TIMEOUT_MS 30000
27 static DEFINE_MUTEX(transaction_lock);
29 struct mpfs_sys_controller {
30 struct mbox_client client;
31 struct mbox_chan *chan;
33 struct kref consumers;
36 int mpfs_blocking_transaction(struct mpfs_sys_controller *sys_controller, struct mpfs_mss_msg *msg)
38 unsigned long timeout = msecs_to_jiffies(MPFS_SYS_CTRL_TIMEOUT_MS);
41 ret = mutex_lock_interruptible(&transaction_lock);
45 reinit_completion(&sys_controller->c);
47 ret = mbox_send_message(sys_controller->chan, msg);
49 dev_warn(sys_controller->client.dev, "MPFS sys controller service timeout\n");
54 * Unfortunately, the system controller will only deliver an interrupt
55 * if a service succeeds. mbox_send_message() will block until the busy
56 * flag is gone. If the busy flag is gone but no interrupt has arrived
57 * to trigger the rx callback then the service can be deemed to have
59 * The caller can then interrogate msg::response::resp_status to
60 * determine the cause of the failure.
61 * mbox_send_message() returns positive integers in the success path, so
62 * ret needs to be cleared if we do get an interrupt.
64 if (!wait_for_completion_timeout(&sys_controller->c, timeout)) {
66 dev_warn(sys_controller->client.dev, "MPFS sys controller service failed\n");
72 mutex_unlock(&transaction_lock);
76 EXPORT_SYMBOL(mpfs_blocking_transaction);
78 static void mpfs_sys_controller_rx_callback(struct mbox_client *client, void *msg)
80 struct mpfs_sys_controller *sys_controller =
81 container_of(client, struct mpfs_sys_controller, client);
83 complete(&sys_controller->c);
86 static void mpfs_sys_controller_delete(struct kref *kref)
88 struct mpfs_sys_controller *sys_controller =
89 container_of(kref, struct mpfs_sys_controller, consumers);
91 mbox_free_channel(sys_controller->chan);
92 kfree(sys_controller);
95 static void mpfs_sys_controller_put(void *data)
97 struct mpfs_sys_controller *sys_controller = data;
99 kref_put(&sys_controller->consumers, mpfs_sys_controller_delete);
102 static struct platform_device subdevs[] = {
108 .name = "mpfs-generic-service",
113 static int mpfs_sys_controller_probe(struct platform_device *pdev)
115 struct device *dev = &pdev->dev;
116 struct mpfs_sys_controller *sys_controller;
119 sys_controller = kzalloc(sizeof(*sys_controller), GFP_KERNEL);
123 sys_controller->client.dev = dev;
124 sys_controller->client.rx_callback = mpfs_sys_controller_rx_callback;
125 sys_controller->client.tx_block = 1U;
126 sys_controller->client.tx_tout = msecs_to_jiffies(MPFS_SYS_CTRL_TIMEOUT_MS);
128 sys_controller->chan = mbox_request_channel(&sys_controller->client, 0);
129 if (IS_ERR(sys_controller->chan)) {
130 ret = dev_err_probe(dev, PTR_ERR(sys_controller->chan),
131 "Failed to get mbox channel\n");
132 kfree(sys_controller);
136 init_completion(&sys_controller->c);
137 kref_init(&sys_controller->consumers);
139 platform_set_drvdata(pdev, sys_controller);
141 dev_info(&pdev->dev, "Registered MPFS system controller\n");
143 for (i = 0; i < ARRAY_SIZE(subdevs); i++) {
144 subdevs[i].dev.parent = dev;
145 if (platform_device_register(&subdevs[i]))
146 dev_warn(dev, "Error registering sub device %s\n", subdevs[i].name);
152 static int mpfs_sys_controller_remove(struct platform_device *pdev)
154 struct mpfs_sys_controller *sys_controller = platform_get_drvdata(pdev);
156 mpfs_sys_controller_put(sys_controller);
161 static const struct of_device_id mpfs_sys_controller_of_match[] = {
162 {.compatible = "microchip,mpfs-sys-controller", },
165 MODULE_DEVICE_TABLE(of, mpfs_sys_controller_of_match);
167 struct mpfs_sys_controller *mpfs_sys_controller_get(struct device *dev)
169 const struct of_device_id *match;
170 struct mpfs_sys_controller *sys_controller;
176 match = of_match_node(mpfs_sys_controller_of_match, dev->parent->of_node);
177 of_node_put(dev->parent->of_node);
181 sys_controller = dev_get_drvdata(dev->parent);
185 if (!kref_get_unless_zero(&sys_controller->consumers))
188 ret = devm_add_action_or_reset(dev, mpfs_sys_controller_put, sys_controller);
192 return sys_controller;
195 dev_dbg(dev, "Parent device was not an MPFS system controller\n");
196 return ERR_PTR(-ENODEV);
199 dev_dbg(dev, "MPFS system controller found but could not register as a sub device\n");
200 return ERR_PTR(-EPROBE_DEFER);
202 EXPORT_SYMBOL(mpfs_sys_controller_get);
204 static struct platform_driver mpfs_sys_controller_driver = {
206 .name = "mpfs-sys-controller",
207 .of_match_table = mpfs_sys_controller_of_match,
209 .probe = mpfs_sys_controller_probe,
210 .remove = mpfs_sys_controller_remove,
212 module_platform_driver(mpfs_sys_controller_driver);
214 MODULE_LICENSE("GPL v2");
216 MODULE_DESCRIPTION("MPFS system controller driver");