Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Microchip PolarFire SoC (MPFS) system controller driver
0004  *
0005  * Copyright (c) 2020-2021 Microchip Corporation. All rights reserved.
0006  *
0007  * Author: Conor Dooley <conor.dooley@microchip.com>
0008  *
0009  */
0010 
0011 #include <linux/slab.h>
0012 #include <linux/kref.h>
0013 #include <linux/module.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/of_platform.h>
0016 #include <linux/mailbox_client.h>
0017 #include <linux/platform_device.h>
0018 #include <soc/microchip/mpfs.h>
0019 
0020 static DEFINE_MUTEX(transaction_lock);
0021 
0022 struct mpfs_sys_controller {
0023     struct mbox_client client;
0024     struct mbox_chan *chan;
0025     struct completion c;
0026     struct kref consumers;
0027 };
0028 
0029 int mpfs_blocking_transaction(struct mpfs_sys_controller *sys_controller, struct mpfs_mss_msg *msg)
0030 {
0031     int ret, err;
0032 
0033     err = mutex_lock_interruptible(&transaction_lock);
0034     if (err)
0035         return err;
0036 
0037     reinit_completion(&sys_controller->c);
0038 
0039     ret = mbox_send_message(sys_controller->chan, msg);
0040     if (ret >= 0) {
0041         if (wait_for_completion_timeout(&sys_controller->c, HZ)) {
0042             ret = 0;
0043         } else {
0044             ret = -ETIMEDOUT;
0045             dev_warn(sys_controller->client.dev,
0046                  "MPFS sys controller transaction timeout\n");
0047         }
0048     } else {
0049         dev_err(sys_controller->client.dev,
0050             "mpfs sys controller transaction returned %d\n", ret);
0051     }
0052 
0053     mutex_unlock(&transaction_lock);
0054 
0055     return ret;
0056 }
0057 EXPORT_SYMBOL(mpfs_blocking_transaction);
0058 
0059 static void rx_callback(struct mbox_client *client, void *msg)
0060 {
0061     struct mpfs_sys_controller *sys_controller =
0062         container_of(client, struct mpfs_sys_controller, client);
0063 
0064     complete(&sys_controller->c);
0065 }
0066 
0067 static void mpfs_sys_controller_delete(struct kref *kref)
0068 {
0069     struct mpfs_sys_controller *sys_controller = container_of(kref, struct mpfs_sys_controller,
0070                            consumers);
0071 
0072     mbox_free_channel(sys_controller->chan);
0073     kfree(sys_controller);
0074 }
0075 
0076 static void mpfs_sys_controller_put(void *data)
0077 {
0078     struct mpfs_sys_controller *sys_controller = data;
0079 
0080     kref_put(&sys_controller->consumers, mpfs_sys_controller_delete);
0081 }
0082 
0083 static struct platform_device subdevs[] = {
0084     {
0085         .name       = "mpfs-rng",
0086         .id     = -1,
0087     },
0088     {
0089         .name       = "mpfs-generic-service",
0090         .id     = -1,
0091     }
0092 };
0093 
0094 static int mpfs_sys_controller_probe(struct platform_device *pdev)
0095 {
0096     struct device *dev = &pdev->dev;
0097     struct mpfs_sys_controller *sys_controller;
0098     int i, ret;
0099 
0100     sys_controller = kzalloc(sizeof(*sys_controller), GFP_KERNEL);
0101     if (!sys_controller)
0102         return -ENOMEM;
0103 
0104     sys_controller->client.dev = dev;
0105     sys_controller->client.rx_callback = rx_callback;
0106     sys_controller->client.tx_block = 1U;
0107 
0108     sys_controller->chan = mbox_request_channel(&sys_controller->client, 0);
0109     if (IS_ERR(sys_controller->chan)) {
0110         ret = dev_err_probe(dev, PTR_ERR(sys_controller->chan),
0111                     "Failed to get mbox channel\n");
0112         kfree(sys_controller);
0113         return ret;
0114     }
0115 
0116     init_completion(&sys_controller->c);
0117     kref_init(&sys_controller->consumers);
0118 
0119     platform_set_drvdata(pdev, sys_controller);
0120 
0121     dev_info(&pdev->dev, "Registered MPFS system controller\n");
0122 
0123     for (i = 0; i < ARRAY_SIZE(subdevs); i++) {
0124         subdevs[i].dev.parent = dev;
0125         if (platform_device_register(&subdevs[i]))
0126             dev_warn(dev, "Error registering sub device %s\n", subdevs[i].name);
0127     }
0128 
0129     return 0;
0130 }
0131 
0132 static int mpfs_sys_controller_remove(struct platform_device *pdev)
0133 {
0134     struct mpfs_sys_controller *sys_controller = platform_get_drvdata(pdev);
0135 
0136     mpfs_sys_controller_put(sys_controller);
0137 
0138     return 0;
0139 }
0140 
0141 static const struct of_device_id mpfs_sys_controller_of_match[] = {
0142     {.compatible = "microchip,mpfs-sys-controller", },
0143     {},
0144 };
0145 MODULE_DEVICE_TABLE(of, mpfs_sys_controller_of_match);
0146 
0147 struct mpfs_sys_controller *mpfs_sys_controller_get(struct device *dev)
0148 {
0149     const struct of_device_id *match;
0150     struct mpfs_sys_controller *sys_controller;
0151     int ret;
0152 
0153     if (!dev->parent)
0154         goto err_no_device;
0155 
0156     match = of_match_node(mpfs_sys_controller_of_match,  dev->parent->of_node);
0157     of_node_put(dev->parent->of_node);
0158     if (!match)
0159         goto err_no_device;
0160 
0161     sys_controller = dev_get_drvdata(dev->parent);
0162     if (!sys_controller)
0163         goto err_bad_device;
0164 
0165     if (!kref_get_unless_zero(&sys_controller->consumers))
0166         goto err_bad_device;
0167 
0168     ret = devm_add_action_or_reset(dev, mpfs_sys_controller_put, sys_controller);
0169     if (ret)
0170         return ERR_PTR(ret);
0171 
0172     return sys_controller;
0173 
0174 err_no_device:
0175     dev_dbg(dev, "Parent device was not an MPFS system controller\n");
0176     return ERR_PTR(-ENODEV);
0177 
0178 err_bad_device:
0179     dev_dbg(dev, "MPFS system controller found but could not register as a sub device\n");
0180     return ERR_PTR(-EPROBE_DEFER);
0181 }
0182 EXPORT_SYMBOL(mpfs_sys_controller_get);
0183 
0184 static struct platform_driver mpfs_sys_controller_driver = {
0185     .driver = {
0186         .name = "mpfs-sys-controller",
0187         .of_match_table = mpfs_sys_controller_of_match,
0188     },
0189     .probe = mpfs_sys_controller_probe,
0190     .remove = mpfs_sys_controller_remove,
0191 };
0192 module_platform_driver(mpfs_sys_controller_driver);
0193 
0194 MODULE_LICENSE("GPL v2");
0195 MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
0196 MODULE_DESCRIPTION("MPFS system controller driver");