18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * APM X-Gene SLIMpro MailBox Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2015, Applied Micro Circuits Corporation
68c2ecf20Sopenharmony_ci * Author: Feng Kan fkan@apm.com
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <linux/acpi.h>
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/mailbox_controller.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/of.h>
158c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
168c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define MBOX_CON_NAME			"slimpro-mbox"
198c2ecf20Sopenharmony_ci#define MBOX_REG_SET_OFFSET		0x1000
208c2ecf20Sopenharmony_ci#define MBOX_CNT			8
218c2ecf20Sopenharmony_ci#define MBOX_STATUS_AVAIL_MASK		BIT(16)
228c2ecf20Sopenharmony_ci#define MBOX_STATUS_ACK_MASK		BIT(0)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* Configuration and Status Registers */
258c2ecf20Sopenharmony_ci#define REG_DB_IN		0x00
268c2ecf20Sopenharmony_ci#define REG_DB_DIN0		0x04
278c2ecf20Sopenharmony_ci#define REG_DB_DIN1		0x08
288c2ecf20Sopenharmony_ci#define REG_DB_OUT		0x10
298c2ecf20Sopenharmony_ci#define REG_DB_DOUT0		0x14
308c2ecf20Sopenharmony_ci#define REG_DB_DOUT1		0x18
318c2ecf20Sopenharmony_ci#define REG_DB_STAT		0x20
328c2ecf20Sopenharmony_ci#define REG_DB_STATMASK		0x24
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/**
358c2ecf20Sopenharmony_ci * X-Gene SlimPRO mailbox channel information
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * @dev:	Device to which it is attached
388c2ecf20Sopenharmony_ci * @chan:	Pointer to mailbox communication channel
398c2ecf20Sopenharmony_ci * @reg:	Base address to access channel registers
408c2ecf20Sopenharmony_ci * @irq:	Interrupt number of the channel
418c2ecf20Sopenharmony_ci * @rx_msg:	Received message storage
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistruct slimpro_mbox_chan {
448c2ecf20Sopenharmony_ci	struct device		*dev;
458c2ecf20Sopenharmony_ci	struct mbox_chan	*chan;
468c2ecf20Sopenharmony_ci	void __iomem		*reg;
478c2ecf20Sopenharmony_ci	int			irq;
488c2ecf20Sopenharmony_ci	u32			rx_msg[3];
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/**
528c2ecf20Sopenharmony_ci * X-Gene SlimPRO Mailbox controller data
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * X-Gene SlimPRO Mailbox controller has 8 commnunication channels.
558c2ecf20Sopenharmony_ci * Each channel has a separate IRQ number assgined to it.
568c2ecf20Sopenharmony_ci *
578c2ecf20Sopenharmony_ci * @mb_ctrl:	Representation of the commnunication channel controller
588c2ecf20Sopenharmony_ci * @mc:		Array of SlimPRO mailbox channels of the controller
598c2ecf20Sopenharmony_ci * @chans:	Array of mailbox communication channels
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_cistruct slimpro_mbox {
638c2ecf20Sopenharmony_ci	struct mbox_controller		mb_ctrl;
648c2ecf20Sopenharmony_ci	struct slimpro_mbox_chan	mc[MBOX_CNT];
658c2ecf20Sopenharmony_ci	struct mbox_chan		chans[MBOX_CNT];
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic void mb_chan_send_msg(struct slimpro_mbox_chan *mb_chan, u32 *msg)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	writel(msg[1], mb_chan->reg + REG_DB_DOUT0);
718c2ecf20Sopenharmony_ci	writel(msg[2], mb_chan->reg + REG_DB_DOUT1);
728c2ecf20Sopenharmony_ci	writel(msg[0], mb_chan->reg + REG_DB_OUT);
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic void mb_chan_recv_msg(struct slimpro_mbox_chan *mb_chan)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	mb_chan->rx_msg[1] = readl(mb_chan->reg + REG_DB_DIN0);
788c2ecf20Sopenharmony_ci	mb_chan->rx_msg[2] = readl(mb_chan->reg + REG_DB_DIN1);
798c2ecf20Sopenharmony_ci	mb_chan->rx_msg[0] = readl(mb_chan->reg + REG_DB_IN);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic int mb_chan_status_ack(struct slimpro_mbox_chan *mb_chan)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	u32 val = readl(mb_chan->reg + REG_DB_STAT);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	if (val & MBOX_STATUS_ACK_MASK) {
878c2ecf20Sopenharmony_ci		writel(MBOX_STATUS_ACK_MASK, mb_chan->reg + REG_DB_STAT);
888c2ecf20Sopenharmony_ci		return 1;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci	return 0;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int mb_chan_status_avail(struct slimpro_mbox_chan *mb_chan)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	u32 val = readl(mb_chan->reg + REG_DB_STAT);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (val & MBOX_STATUS_AVAIL_MASK) {
988c2ecf20Sopenharmony_ci		mb_chan_recv_msg(mb_chan);
998c2ecf20Sopenharmony_ci		writel(MBOX_STATUS_AVAIL_MASK, mb_chan->reg + REG_DB_STAT);
1008c2ecf20Sopenharmony_ci		return 1;
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci	return 0;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic irqreturn_t slimpro_mbox_irq(int irq, void *id)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct slimpro_mbox_chan *mb_chan = id;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	if (mb_chan_status_ack(mb_chan))
1108c2ecf20Sopenharmony_ci		mbox_chan_txdone(mb_chan->chan, 0);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (mb_chan_status_avail(mb_chan))
1138c2ecf20Sopenharmony_ci		mbox_chan_received_data(mb_chan->chan, mb_chan->rx_msg);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic int slimpro_mbox_send_data(struct mbox_chan *chan, void *msg)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	struct slimpro_mbox_chan *mb_chan = chan->con_priv;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	mb_chan_send_msg(mb_chan, msg);
1238c2ecf20Sopenharmony_ci	return 0;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic int slimpro_mbox_startup(struct mbox_chan *chan)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct slimpro_mbox_chan *mb_chan = chan->con_priv;
1298c2ecf20Sopenharmony_ci	int rc;
1308c2ecf20Sopenharmony_ci	u32 val;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	rc = devm_request_irq(mb_chan->dev, mb_chan->irq, slimpro_mbox_irq, 0,
1338c2ecf20Sopenharmony_ci			      MBOX_CON_NAME, mb_chan);
1348c2ecf20Sopenharmony_ci	if (unlikely(rc)) {
1358c2ecf20Sopenharmony_ci		dev_err(mb_chan->dev, "failed to register mailbox interrupt %d\n",
1368c2ecf20Sopenharmony_ci			mb_chan->irq);
1378c2ecf20Sopenharmony_ci		return rc;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	/* Enable HW interrupt */
1418c2ecf20Sopenharmony_ci	writel(MBOX_STATUS_ACK_MASK | MBOX_STATUS_AVAIL_MASK,
1428c2ecf20Sopenharmony_ci	       mb_chan->reg + REG_DB_STAT);
1438c2ecf20Sopenharmony_ci	/* Unmask doorbell status interrupt */
1448c2ecf20Sopenharmony_ci	val = readl(mb_chan->reg + REG_DB_STATMASK);
1458c2ecf20Sopenharmony_ci	val &= ~(MBOX_STATUS_ACK_MASK | MBOX_STATUS_AVAIL_MASK);
1468c2ecf20Sopenharmony_ci	writel(val, mb_chan->reg + REG_DB_STATMASK);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	return 0;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic void slimpro_mbox_shutdown(struct mbox_chan *chan)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	struct slimpro_mbox_chan *mb_chan = chan->con_priv;
1548c2ecf20Sopenharmony_ci	u32 val;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* Mask doorbell status interrupt */
1578c2ecf20Sopenharmony_ci	val = readl(mb_chan->reg + REG_DB_STATMASK);
1588c2ecf20Sopenharmony_ci	val |= (MBOX_STATUS_ACK_MASK | MBOX_STATUS_AVAIL_MASK);
1598c2ecf20Sopenharmony_ci	writel(val, mb_chan->reg + REG_DB_STATMASK);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	devm_free_irq(mb_chan->dev, mb_chan->irq, mb_chan);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic const struct mbox_chan_ops slimpro_mbox_ops = {
1658c2ecf20Sopenharmony_ci	.send_data = slimpro_mbox_send_data,
1668c2ecf20Sopenharmony_ci	.startup = slimpro_mbox_startup,
1678c2ecf20Sopenharmony_ci	.shutdown = slimpro_mbox_shutdown,
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic int slimpro_mbox_probe(struct platform_device *pdev)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	struct slimpro_mbox *ctx;
1738c2ecf20Sopenharmony_ci	struct resource *regs;
1748c2ecf20Sopenharmony_ci	void __iomem *mb_base;
1758c2ecf20Sopenharmony_ci	int rc;
1768c2ecf20Sopenharmony_ci	int i;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	ctx = devm_kzalloc(&pdev->dev, sizeof(struct slimpro_mbox), GFP_KERNEL);
1798c2ecf20Sopenharmony_ci	if (!ctx)
1808c2ecf20Sopenharmony_ci		return -ENOMEM;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, ctx);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1858c2ecf20Sopenharmony_ci	mb_base = devm_ioremap_resource(&pdev->dev, regs);
1868c2ecf20Sopenharmony_ci	if (IS_ERR(mb_base))
1878c2ecf20Sopenharmony_ci		return PTR_ERR(mb_base);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	/* Setup mailbox links */
1908c2ecf20Sopenharmony_ci	for (i = 0; i < MBOX_CNT; i++) {
1918c2ecf20Sopenharmony_ci		ctx->mc[i].irq = platform_get_irq(pdev, i);
1928c2ecf20Sopenharmony_ci		if (ctx->mc[i].irq < 0) {
1938c2ecf20Sopenharmony_ci			if (i == 0) {
1948c2ecf20Sopenharmony_ci				dev_err(&pdev->dev, "no available IRQ\n");
1958c2ecf20Sopenharmony_ci				return -EINVAL;
1968c2ecf20Sopenharmony_ci			}
1978c2ecf20Sopenharmony_ci			dev_info(&pdev->dev, "no IRQ for channel %d\n", i);
1988c2ecf20Sopenharmony_ci			break;
1998c2ecf20Sopenharmony_ci		}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci		ctx->mc[i].dev = &pdev->dev;
2028c2ecf20Sopenharmony_ci		ctx->mc[i].reg = mb_base + i * MBOX_REG_SET_OFFSET;
2038c2ecf20Sopenharmony_ci		ctx->mc[i].chan = &ctx->chans[i];
2048c2ecf20Sopenharmony_ci		ctx->chans[i].con_priv = &ctx->mc[i];
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/* Setup mailbox controller */
2088c2ecf20Sopenharmony_ci	ctx->mb_ctrl.dev = &pdev->dev;
2098c2ecf20Sopenharmony_ci	ctx->mb_ctrl.chans = ctx->chans;
2108c2ecf20Sopenharmony_ci	ctx->mb_ctrl.txdone_irq = true;
2118c2ecf20Sopenharmony_ci	ctx->mb_ctrl.ops = &slimpro_mbox_ops;
2128c2ecf20Sopenharmony_ci	ctx->mb_ctrl.num_chans = i;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	rc = devm_mbox_controller_register(&pdev->dev, &ctx->mb_ctrl);
2158c2ecf20Sopenharmony_ci	if (rc) {
2168c2ecf20Sopenharmony_ci		dev_err(&pdev->dev,
2178c2ecf20Sopenharmony_ci			"APM X-Gene SLIMpro MailBox register failed:%d\n", rc);
2188c2ecf20Sopenharmony_ci		return rc;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "APM X-Gene SLIMpro MailBox registered\n");
2228c2ecf20Sopenharmony_ci	return 0;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic const struct of_device_id slimpro_of_match[] = {
2268c2ecf20Sopenharmony_ci	{.compatible = "apm,xgene-slimpro-mbox" },
2278c2ecf20Sopenharmony_ci	{ },
2288c2ecf20Sopenharmony_ci};
2298c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, slimpro_of_match);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI
2328c2ecf20Sopenharmony_cistatic const struct acpi_device_id slimpro_acpi_ids[] = {
2338c2ecf20Sopenharmony_ci	{"APMC0D01", 0},
2348c2ecf20Sopenharmony_ci	{}
2358c2ecf20Sopenharmony_ci};
2368c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, slimpro_acpi_ids);
2378c2ecf20Sopenharmony_ci#endif
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic struct platform_driver slimpro_mbox_driver = {
2408c2ecf20Sopenharmony_ci	.probe	= slimpro_mbox_probe,
2418c2ecf20Sopenharmony_ci	.driver	= {
2428c2ecf20Sopenharmony_ci		.name = "xgene-slimpro-mbox",
2438c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(slimpro_of_match),
2448c2ecf20Sopenharmony_ci		.acpi_match_table = ACPI_PTR(slimpro_acpi_ids)
2458c2ecf20Sopenharmony_ci	},
2468c2ecf20Sopenharmony_ci};
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic int __init slimpro_mbox_init(void)
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	return platform_driver_register(&slimpro_mbox_driver);
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic void __exit slimpro_mbox_exit(void)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	platform_driver_unregister(&slimpro_mbox_driver);
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cisubsys_initcall(slimpro_mbox_init);
2598c2ecf20Sopenharmony_cimodule_exit(slimpro_mbox_exit);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("APM X-Gene SLIMpro Mailbox Driver");
2628c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
263