162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * OMAP hardware spinlock driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2010-2021 Texas Instruments Incorporated - https://www.ti.com
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Contact: Simon Que <sque@ti.com>
862306a36Sopenharmony_ci *          Hari Kanigeri <h-kanigeri2@ti.com>
962306a36Sopenharmony_ci *          Ohad Ben-Cohen <ohad@wizery.com>
1062306a36Sopenharmony_ci *          Suman Anna <s-anna@ti.com>
1162306a36Sopenharmony_ci */
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/kernel.h>
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include <linux/device.h>
1662306a36Sopenharmony_ci#include <linux/delay.h>
1762306a36Sopenharmony_ci#include <linux/io.h>
1862306a36Sopenharmony_ci#include <linux/bitops.h>
1962306a36Sopenharmony_ci#include <linux/pm_runtime.h>
2062306a36Sopenharmony_ci#include <linux/slab.h>
2162306a36Sopenharmony_ci#include <linux/spinlock.h>
2262306a36Sopenharmony_ci#include <linux/hwspinlock.h>
2362306a36Sopenharmony_ci#include <linux/of.h>
2462306a36Sopenharmony_ci#include <linux/platform_device.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#include "hwspinlock_internal.h"
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/* Spinlock register offsets */
2962306a36Sopenharmony_ci#define SYSSTATUS_OFFSET		0x0014
3062306a36Sopenharmony_ci#define LOCK_BASE_OFFSET		0x0800
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#define SPINLOCK_NUMLOCKS_BIT_OFFSET	(24)
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/* Possible values of SPINLOCK_LOCK_REG */
3562306a36Sopenharmony_ci#define SPINLOCK_NOTTAKEN		(0)	/* free */
3662306a36Sopenharmony_ci#define SPINLOCK_TAKEN			(1)	/* locked */
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic int omap_hwspinlock_trylock(struct hwspinlock *lock)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	void __iomem *lock_addr = lock->priv;
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	/* attempt to acquire the lock by reading its value */
4362306a36Sopenharmony_ci	return (SPINLOCK_NOTTAKEN == readl(lock_addr));
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic void omap_hwspinlock_unlock(struct hwspinlock *lock)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	void __iomem *lock_addr = lock->priv;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	/* release the lock by writing 0 to it */
5162306a36Sopenharmony_ci	writel(SPINLOCK_NOTTAKEN, lock_addr);
5262306a36Sopenharmony_ci}
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/*
5562306a36Sopenharmony_ci * relax the OMAP interconnect while spinning on it.
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * The specs recommended that the retry delay time will be
5862306a36Sopenharmony_ci * just over half of the time that a requester would be
5962306a36Sopenharmony_ci * expected to hold the lock.
6062306a36Sopenharmony_ci *
6162306a36Sopenharmony_ci * The number below is taken from an hardware specs example,
6262306a36Sopenharmony_ci * obviously it is somewhat arbitrary.
6362306a36Sopenharmony_ci */
6462306a36Sopenharmony_cistatic void omap_hwspinlock_relax(struct hwspinlock *lock)
6562306a36Sopenharmony_ci{
6662306a36Sopenharmony_ci	ndelay(50);
6762306a36Sopenharmony_ci}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistatic const struct hwspinlock_ops omap_hwspinlock_ops = {
7062306a36Sopenharmony_ci	.trylock = omap_hwspinlock_trylock,
7162306a36Sopenharmony_ci	.unlock = omap_hwspinlock_unlock,
7262306a36Sopenharmony_ci	.relax = omap_hwspinlock_relax,
7362306a36Sopenharmony_ci};
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cistatic int omap_hwspinlock_probe(struct platform_device *pdev)
7662306a36Sopenharmony_ci{
7762306a36Sopenharmony_ci	struct device_node *node = pdev->dev.of_node;
7862306a36Sopenharmony_ci	struct hwspinlock_device *bank;
7962306a36Sopenharmony_ci	struct hwspinlock *hwlock;
8062306a36Sopenharmony_ci	void __iomem *io_base;
8162306a36Sopenharmony_ci	int num_locks, i, ret;
8262306a36Sopenharmony_ci	/* Only a single hwspinlock block device is supported */
8362306a36Sopenharmony_ci	int base_id = 0;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	if (!node)
8662306a36Sopenharmony_ci		return -ENODEV;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	io_base = devm_platform_ioremap_resource(pdev, 0);
8962306a36Sopenharmony_ci	if (IS_ERR(io_base))
9062306a36Sopenharmony_ci		return PTR_ERR(io_base);
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	/*
9362306a36Sopenharmony_ci	 * make sure the module is enabled and clocked before reading
9462306a36Sopenharmony_ci	 * the module SYSSTATUS register
9562306a36Sopenharmony_ci	 */
9662306a36Sopenharmony_ci	pm_runtime_enable(&pdev->dev);
9762306a36Sopenharmony_ci	ret = pm_runtime_resume_and_get(&pdev->dev);
9862306a36Sopenharmony_ci	if (ret < 0)
9962306a36Sopenharmony_ci		goto runtime_err;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	/* Determine number of locks */
10262306a36Sopenharmony_ci	i = readl(io_base + SYSSTATUS_OFFSET);
10362306a36Sopenharmony_ci	i >>= SPINLOCK_NUMLOCKS_BIT_OFFSET;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	/*
10662306a36Sopenharmony_ci	 * runtime PM will make sure the clock of this module is
10762306a36Sopenharmony_ci	 * enabled again iff at least one lock is requested
10862306a36Sopenharmony_ci	 */
10962306a36Sopenharmony_ci	ret = pm_runtime_put(&pdev->dev);
11062306a36Sopenharmony_ci	if (ret < 0)
11162306a36Sopenharmony_ci		goto runtime_err;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	/* one of the four lsb's must be set, and nothing else */
11462306a36Sopenharmony_ci	if (hweight_long(i & 0xf) != 1 || i > 8) {
11562306a36Sopenharmony_ci		ret = -EINVAL;
11662306a36Sopenharmony_ci		goto runtime_err;
11762306a36Sopenharmony_ci	}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	num_locks = i * 32; /* actual number of locks in this device */
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	bank = devm_kzalloc(&pdev->dev, struct_size(bank, lock, num_locks),
12262306a36Sopenharmony_ci			    GFP_KERNEL);
12362306a36Sopenharmony_ci	if (!bank) {
12462306a36Sopenharmony_ci		ret = -ENOMEM;
12562306a36Sopenharmony_ci		goto runtime_err;
12662306a36Sopenharmony_ci	}
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	platform_set_drvdata(pdev, bank);
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	for (i = 0, hwlock = &bank->lock[0]; i < num_locks; i++, hwlock++)
13162306a36Sopenharmony_ci		hwlock->priv = io_base + LOCK_BASE_OFFSET + sizeof(u32) * i;
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	ret = hwspin_lock_register(bank, &pdev->dev, &omap_hwspinlock_ops,
13462306a36Sopenharmony_ci						base_id, num_locks);
13562306a36Sopenharmony_ci	if (ret)
13662306a36Sopenharmony_ci		goto runtime_err;
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	dev_dbg(&pdev->dev, "Registered %d locks with HwSpinlock core\n",
13962306a36Sopenharmony_ci		num_locks);
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	return 0;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciruntime_err:
14462306a36Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
14562306a36Sopenharmony_ci	return ret;
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_cistatic void omap_hwspinlock_remove(struct platform_device *pdev)
14962306a36Sopenharmony_ci{
15062306a36Sopenharmony_ci	struct hwspinlock_device *bank = platform_get_drvdata(pdev);
15162306a36Sopenharmony_ci	int ret;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	ret = hwspin_lock_unregister(bank);
15462306a36Sopenharmony_ci	if (ret) {
15562306a36Sopenharmony_ci		dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
15662306a36Sopenharmony_ci		return;
15762306a36Sopenharmony_ci	}
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
16062306a36Sopenharmony_ci}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistatic const struct of_device_id omap_hwspinlock_of_match[] = {
16362306a36Sopenharmony_ci	{ .compatible = "ti,omap4-hwspinlock", },
16462306a36Sopenharmony_ci	{ .compatible = "ti,am64-hwspinlock", },
16562306a36Sopenharmony_ci	{ .compatible = "ti,am654-hwspinlock", },
16662306a36Sopenharmony_ci	{ /* end */ },
16762306a36Sopenharmony_ci};
16862306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, omap_hwspinlock_of_match);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cistatic struct platform_driver omap_hwspinlock_driver = {
17162306a36Sopenharmony_ci	.probe		= omap_hwspinlock_probe,
17262306a36Sopenharmony_ci	.remove_new	= omap_hwspinlock_remove,
17362306a36Sopenharmony_ci	.driver		= {
17462306a36Sopenharmony_ci		.name	= "omap_hwspinlock",
17562306a36Sopenharmony_ci		.of_match_table = omap_hwspinlock_of_match,
17662306a36Sopenharmony_ci	},
17762306a36Sopenharmony_ci};
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_cistatic int __init omap_hwspinlock_init(void)
18062306a36Sopenharmony_ci{
18162306a36Sopenharmony_ci	return platform_driver_register(&omap_hwspinlock_driver);
18262306a36Sopenharmony_ci}
18362306a36Sopenharmony_ci/* board init code might need to reserve hwspinlocks for predefined purposes */
18462306a36Sopenharmony_cipostcore_initcall(omap_hwspinlock_init);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_cistatic void __exit omap_hwspinlock_exit(void)
18762306a36Sopenharmony_ci{
18862306a36Sopenharmony_ci	platform_driver_unregister(&omap_hwspinlock_driver);
18962306a36Sopenharmony_ci}
19062306a36Sopenharmony_cimodule_exit(omap_hwspinlock_exit);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
19362306a36Sopenharmony_ciMODULE_DESCRIPTION("Hardware spinlock driver for OMAP");
19462306a36Sopenharmony_ciMODULE_AUTHOR("Simon Que <sque@ti.com>");
19562306a36Sopenharmony_ciMODULE_AUTHOR("Hari Kanigeri <h-kanigeri2@ti.com>");
19662306a36Sopenharmony_ciMODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");
197