18e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2ad3a212cSNeil Armstrong /* 3ad3a212cSNeil Armstrong * Copyright (C) 2016 BayLibre SAS. 4ad3a212cSNeil Armstrong * Author: Neil Armstrong <narmstrong@baylibre.com> 5ad3a212cSNeil Armstrong * Synchronised with arm_mhu.c from : 6ad3a212cSNeil Armstrong * Copyright (C) 2013-2015 Fujitsu Semiconductor Ltd. 7ad3a212cSNeil Armstrong * Copyright (C) 2015 Linaro Ltd. 8ad3a212cSNeil Armstrong * Author: Jassi Brar <jaswinder.singh@linaro.org> 9ad3a212cSNeil Armstrong */ 10ad3a212cSNeil Armstrong 11ad3a212cSNeil Armstrong #include <linux/interrupt.h> 12ad3a212cSNeil Armstrong #include <linux/spinlock.h> 13ad3a212cSNeil Armstrong #include <linux/mutex.h> 14ad3a212cSNeil Armstrong #include <linux/delay.h> 15ad3a212cSNeil Armstrong #include <linux/slab.h> 16ad3a212cSNeil Armstrong #include <linux/err.h> 17ad3a212cSNeil Armstrong #include <linux/io.h> 18ad3a212cSNeil Armstrong #include <linux/module.h> 19ad3a212cSNeil Armstrong #include <linux/platform_device.h> 20ad3a212cSNeil Armstrong #include <linux/mailbox_controller.h> 21ad3a212cSNeil Armstrong 22ad3a212cSNeil Armstrong #define INTR_SET_OFS 0x0 23ad3a212cSNeil Armstrong #define INTR_STAT_OFS 0x4 24ad3a212cSNeil Armstrong #define INTR_CLR_OFS 0x8 25ad3a212cSNeil Armstrong 26ad3a212cSNeil Armstrong #define MHU_SEC_OFFSET 0x0 27ad3a212cSNeil Armstrong #define MHU_LP_OFFSET 0xc 28ad3a212cSNeil Armstrong #define MHU_HP_OFFSET 0x18 29ad3a212cSNeil Armstrong #define TX_REG_OFFSET 0x24 30ad3a212cSNeil Armstrong 31ad3a212cSNeil Armstrong #define MHU_CHANS 3 32ad3a212cSNeil Armstrong 33ad3a212cSNeil Armstrong struct platform_mhu_link { 34ad3a212cSNeil Armstrong int irq; 35ad3a212cSNeil Armstrong void __iomem *tx_reg; 36ad3a212cSNeil Armstrong void __iomem *rx_reg; 37ad3a212cSNeil Armstrong }; 38ad3a212cSNeil Armstrong 39ad3a212cSNeil Armstrong struct platform_mhu { 40ad3a212cSNeil Armstrong void __iomem *base; 41ad3a212cSNeil Armstrong struct platform_mhu_link mlink[MHU_CHANS]; 42ad3a212cSNeil Armstrong struct mbox_chan chan[MHU_CHANS]; 43ad3a212cSNeil Armstrong struct mbox_controller mbox; 44ad3a212cSNeil Armstrong }; 45ad3a212cSNeil Armstrong 46ad3a212cSNeil Armstrong static irqreturn_t platform_mhu_rx_interrupt(int irq, void *p) 47ad3a212cSNeil Armstrong { 48ad3a212cSNeil Armstrong struct mbox_chan *chan = p; 49ad3a212cSNeil Armstrong struct platform_mhu_link *mlink = chan->con_priv; 50ad3a212cSNeil Armstrong u32 val; 51ad3a212cSNeil Armstrong 52ad3a212cSNeil Armstrong val = readl_relaxed(mlink->rx_reg + INTR_STAT_OFS); 53ad3a212cSNeil Armstrong if (!val) 54ad3a212cSNeil Armstrong return IRQ_NONE; 55ad3a212cSNeil Armstrong 56ad3a212cSNeil Armstrong mbox_chan_received_data(chan, (void *)&val); 57ad3a212cSNeil Armstrong 58ad3a212cSNeil Armstrong writel_relaxed(val, mlink->rx_reg + INTR_CLR_OFS); 59ad3a212cSNeil Armstrong 60ad3a212cSNeil Armstrong return IRQ_HANDLED; 61ad3a212cSNeil Armstrong } 62ad3a212cSNeil Armstrong 63ad3a212cSNeil Armstrong static bool platform_mhu_last_tx_done(struct mbox_chan *chan) 64ad3a212cSNeil Armstrong { 65ad3a212cSNeil Armstrong struct platform_mhu_link *mlink = chan->con_priv; 66ad3a212cSNeil Armstrong u32 val = readl_relaxed(mlink->tx_reg + INTR_STAT_OFS); 67ad3a212cSNeil Armstrong 68ad3a212cSNeil Armstrong return (val == 0); 69ad3a212cSNeil Armstrong } 70ad3a212cSNeil Armstrong 71ad3a212cSNeil Armstrong static int platform_mhu_send_data(struct mbox_chan *chan, void *data) 72ad3a212cSNeil Armstrong { 73ad3a212cSNeil Armstrong struct platform_mhu_link *mlink = chan->con_priv; 74ad3a212cSNeil Armstrong u32 *arg = data; 75ad3a212cSNeil Armstrong 76ad3a212cSNeil Armstrong writel_relaxed(*arg, mlink->tx_reg + INTR_SET_OFS); 77ad3a212cSNeil Armstrong 78ad3a212cSNeil Armstrong return 0; 79ad3a212cSNeil Armstrong } 80ad3a212cSNeil Armstrong 81ad3a212cSNeil Armstrong static int platform_mhu_startup(struct mbox_chan *chan) 82ad3a212cSNeil Armstrong { 83ad3a212cSNeil Armstrong struct platform_mhu_link *mlink = chan->con_priv; 84ad3a212cSNeil Armstrong u32 val; 85ad3a212cSNeil Armstrong int ret; 86ad3a212cSNeil Armstrong 87ad3a212cSNeil Armstrong val = readl_relaxed(mlink->tx_reg + INTR_STAT_OFS); 88ad3a212cSNeil Armstrong writel_relaxed(val, mlink->tx_reg + INTR_CLR_OFS); 89ad3a212cSNeil Armstrong 90ad3a212cSNeil Armstrong ret = request_irq(mlink->irq, platform_mhu_rx_interrupt, 91ad3a212cSNeil Armstrong IRQF_SHARED, "platform_mhu_link", chan); 92ad3a212cSNeil Armstrong if (ret) { 93ad3a212cSNeil Armstrong dev_err(chan->mbox->dev, 94ad3a212cSNeil Armstrong "Unable to acquire IRQ %d\n", mlink->irq); 95ad3a212cSNeil Armstrong return ret; 96ad3a212cSNeil Armstrong } 97ad3a212cSNeil Armstrong 98ad3a212cSNeil Armstrong return 0; 99ad3a212cSNeil Armstrong } 100ad3a212cSNeil Armstrong 101ad3a212cSNeil Armstrong static void platform_mhu_shutdown(struct mbox_chan *chan) 102ad3a212cSNeil Armstrong { 103ad3a212cSNeil Armstrong struct platform_mhu_link *mlink = chan->con_priv; 104ad3a212cSNeil Armstrong 105ad3a212cSNeil Armstrong free_irq(mlink->irq, chan); 106ad3a212cSNeil Armstrong } 107ad3a212cSNeil Armstrong 108ad3a212cSNeil Armstrong static const struct mbox_chan_ops platform_mhu_ops = { 109ad3a212cSNeil Armstrong .send_data = platform_mhu_send_data, 110ad3a212cSNeil Armstrong .startup = platform_mhu_startup, 111ad3a212cSNeil Armstrong .shutdown = platform_mhu_shutdown, 112ad3a212cSNeil Armstrong .last_tx_done = platform_mhu_last_tx_done, 113ad3a212cSNeil Armstrong }; 114ad3a212cSNeil Armstrong 115ad3a212cSNeil Armstrong static int platform_mhu_probe(struct platform_device *pdev) 116ad3a212cSNeil Armstrong { 117ad3a212cSNeil Armstrong int i, err; 118ad3a212cSNeil Armstrong struct platform_mhu *mhu; 119ad3a212cSNeil Armstrong struct device *dev = &pdev->dev; 120ad3a212cSNeil Armstrong int platform_mhu_reg[MHU_CHANS] = { 121ad3a212cSNeil Armstrong MHU_SEC_OFFSET, MHU_LP_OFFSET, MHU_HP_OFFSET 122ad3a212cSNeil Armstrong }; 123ad3a212cSNeil Armstrong 124ad3a212cSNeil Armstrong /* Allocate memory for device */ 125ad3a212cSNeil Armstrong mhu = devm_kzalloc(dev, sizeof(*mhu), GFP_KERNEL); 126ad3a212cSNeil Armstrong if (!mhu) 127ad3a212cSNeil Armstrong return -ENOMEM; 128ad3a212cSNeil Armstrong 129*b5e3a1feSCai Huoqing mhu->base = devm_platform_ioremap_resource(pdev, 0); 130ad3a212cSNeil Armstrong if (IS_ERR(mhu->base)) { 131ad3a212cSNeil Armstrong dev_err(dev, "ioremap failed\n"); 132ad3a212cSNeil Armstrong return PTR_ERR(mhu->base); 133ad3a212cSNeil Armstrong } 134ad3a212cSNeil Armstrong 135ad3a212cSNeil Armstrong for (i = 0; i < MHU_CHANS; i++) { 136ad3a212cSNeil Armstrong mhu->chan[i].con_priv = &mhu->mlink[i]; 137ad3a212cSNeil Armstrong mhu->mlink[i].irq = platform_get_irq(pdev, i); 138ad3a212cSNeil Armstrong if (mhu->mlink[i].irq < 0) { 139ad3a212cSNeil Armstrong dev_err(dev, "failed to get irq%d\n", i); 140ad3a212cSNeil Armstrong return mhu->mlink[i].irq; 141ad3a212cSNeil Armstrong } 142ad3a212cSNeil Armstrong mhu->mlink[i].rx_reg = mhu->base + platform_mhu_reg[i]; 143ad3a212cSNeil Armstrong mhu->mlink[i].tx_reg = mhu->mlink[i].rx_reg + TX_REG_OFFSET; 144ad3a212cSNeil Armstrong } 145ad3a212cSNeil Armstrong 146ad3a212cSNeil Armstrong mhu->mbox.dev = dev; 147ad3a212cSNeil Armstrong mhu->mbox.chans = &mhu->chan[0]; 148ad3a212cSNeil Armstrong mhu->mbox.num_chans = MHU_CHANS; 149ad3a212cSNeil Armstrong mhu->mbox.ops = &platform_mhu_ops; 150ad3a212cSNeil Armstrong mhu->mbox.txdone_irq = false; 151ad3a212cSNeil Armstrong mhu->mbox.txdone_poll = true; 152ad3a212cSNeil Armstrong mhu->mbox.txpoll_period = 1; 153ad3a212cSNeil Armstrong 154ad3a212cSNeil Armstrong platform_set_drvdata(pdev, mhu); 155ad3a212cSNeil Armstrong 15685a55524SThierry Reding err = devm_mbox_controller_register(dev, &mhu->mbox); 157ad3a212cSNeil Armstrong if (err) { 158ad3a212cSNeil Armstrong dev_err(dev, "Failed to register mailboxes %d\n", err); 159ad3a212cSNeil Armstrong return err; 160ad3a212cSNeil Armstrong } 161ad3a212cSNeil Armstrong 162ad3a212cSNeil Armstrong dev_info(dev, "Platform MHU Mailbox registered\n"); 163ad3a212cSNeil Armstrong return 0; 164ad3a212cSNeil Armstrong } 165ad3a212cSNeil Armstrong 166ad3a212cSNeil Armstrong static const struct of_device_id platform_mhu_dt_ids[] = { 167ad3a212cSNeil Armstrong { .compatible = "amlogic,meson-gxbb-mhu", }, 168ad3a212cSNeil Armstrong { /* sentinel */ }, 169ad3a212cSNeil Armstrong }; 170ad3a212cSNeil Armstrong MODULE_DEVICE_TABLE(of, platform_mhu_dt_ids); 171ad3a212cSNeil Armstrong 172ad3a212cSNeil Armstrong static struct platform_driver platform_mhu_driver = { 173ad3a212cSNeil Armstrong .probe = platform_mhu_probe, 174ad3a212cSNeil Armstrong .driver = { 175ad3a212cSNeil Armstrong .name = "platform-mhu", 176ad3a212cSNeil Armstrong .of_match_table = platform_mhu_dt_ids, 177ad3a212cSNeil Armstrong }, 178ad3a212cSNeil Armstrong }; 179ad3a212cSNeil Armstrong 180ad3a212cSNeil Armstrong module_platform_driver(platform_mhu_driver); 181ad3a212cSNeil Armstrong 182ad3a212cSNeil Armstrong MODULE_LICENSE("GPL v2"); 183ad3a212cSNeil Armstrong MODULE_ALIAS("platform:platform-mhu"); 184ad3a212cSNeil Armstrong MODULE_DESCRIPTION("Platform MHU Driver"); 185ad3a212cSNeil Armstrong MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>"); 186