Path: blob/master/drivers/hwspinlock/omap_hwspinlock.c
15109 views
/*1* OMAP hardware spinlock driver2*3* Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com4*5* Contact: Simon Que <[email protected]>6* Hari Kanigeri <[email protected]>7* Ohad Ben-Cohen <[email protected]>8*9* This program is free software; you can redistribute it and/or10* modify it under the terms of the GNU General Public License11* version 2 as published by the Free Software Foundation.12*13* This program is distributed in the hope that it will be useful, but14* WITHOUT ANY WARRANTY; without even the implied warranty of15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU16* General Public License for more details.17*/1819#include <linux/kernel.h>20#include <linux/module.h>21#include <linux/device.h>22#include <linux/delay.h>23#include <linux/io.h>24#include <linux/bitops.h>25#include <linux/pm_runtime.h>26#include <linux/slab.h>27#include <linux/spinlock.h>28#include <linux/hwspinlock.h>29#include <linux/platform_device.h>3031#include "hwspinlock_internal.h"3233/* Spinlock register offsets */34#define SYSSTATUS_OFFSET 0x001435#define LOCK_BASE_OFFSET 0x08003637#define SPINLOCK_NUMLOCKS_BIT_OFFSET (24)3839/* Possible values of SPINLOCK_LOCK_REG */40#define SPINLOCK_NOTTAKEN (0) /* free */41#define SPINLOCK_TAKEN (1) /* locked */4243#define to_omap_hwspinlock(lock) \44container_of(lock, struct omap_hwspinlock, lock)4546struct omap_hwspinlock {47struct hwspinlock lock;48void __iomem *addr;49};5051struct omap_hwspinlock_state {52int num_locks; /* Total number of locks in system */53void __iomem *io_base; /* Mapped base address */54};5556static int omap_hwspinlock_trylock(struct hwspinlock *lock)57{58struct omap_hwspinlock *omap_lock = to_omap_hwspinlock(lock);5960/* attempt to acquire the lock by reading its value */61return (SPINLOCK_NOTTAKEN == readl(omap_lock->addr));62}6364static void omap_hwspinlock_unlock(struct hwspinlock *lock)65{66struct omap_hwspinlock *omap_lock = to_omap_hwspinlock(lock);6768/* release the lock by writing 0 to it */69writel(SPINLOCK_NOTTAKEN, omap_lock->addr);70}7172/*73* relax the OMAP interconnect while spinning on it.74*75* The specs recommended that the retry delay time will be76* just over half of the time that a requester would be77* expected to hold the lock.78*79* The number below is taken from an hardware specs example,80* obviously it is somewhat arbitrary.81*/82static void omap_hwspinlock_relax(struct hwspinlock *lock)83{84ndelay(50);85}8687static const struct hwspinlock_ops omap_hwspinlock_ops = {88.trylock = omap_hwspinlock_trylock,89.unlock = omap_hwspinlock_unlock,90.relax = omap_hwspinlock_relax,91};9293static int __devinit omap_hwspinlock_probe(struct platform_device *pdev)94{95struct omap_hwspinlock *omap_lock;96struct omap_hwspinlock_state *state;97struct hwspinlock *lock;98struct resource *res;99void __iomem *io_base;100int i, ret;101102res = platform_get_resource(pdev, IORESOURCE_MEM, 0);103if (!res)104return -ENODEV;105106state = kzalloc(sizeof(*state), GFP_KERNEL);107if (!state)108return -ENOMEM;109110io_base = ioremap(res->start, resource_size(res));111if (!io_base) {112ret = -ENOMEM;113goto free_state;114}115116/* Determine number of locks */117i = readl(io_base + SYSSTATUS_OFFSET);118i >>= SPINLOCK_NUMLOCKS_BIT_OFFSET;119120/* one of the four lsb's must be set, and nothing else */121if (hweight_long(i & 0xf) != 1 || i > 8) {122ret = -EINVAL;123goto iounmap_base;124}125126state->num_locks = i * 32;127state->io_base = io_base;128129platform_set_drvdata(pdev, state);130131/*132* runtime PM will make sure the clock of this module is133* enabled iff at least one lock is requested134*/135pm_runtime_enable(&pdev->dev);136137for (i = 0; i < state->num_locks; i++) {138omap_lock = kzalloc(sizeof(*omap_lock), GFP_KERNEL);139if (!omap_lock) {140ret = -ENOMEM;141goto free_locks;142}143144omap_lock->lock.dev = &pdev->dev;145omap_lock->lock.owner = THIS_MODULE;146omap_lock->lock.id = i;147omap_lock->lock.ops = &omap_hwspinlock_ops;148omap_lock->addr = io_base + LOCK_BASE_OFFSET + sizeof(u32) * i;149150ret = hwspin_lock_register(&omap_lock->lock);151if (ret) {152kfree(omap_lock);153goto free_locks;154}155}156157return 0;158159free_locks:160while (--i >= 0) {161lock = hwspin_lock_unregister(i);162/* this should't happen, but let's give our best effort */163if (!lock) {164dev_err(&pdev->dev, "%s: cleanups failed\n", __func__);165continue;166}167omap_lock = to_omap_hwspinlock(lock);168kfree(omap_lock);169}170pm_runtime_disable(&pdev->dev);171iounmap_base:172iounmap(io_base);173free_state:174kfree(state);175return ret;176}177178static int omap_hwspinlock_remove(struct platform_device *pdev)179{180struct omap_hwspinlock_state *state = platform_get_drvdata(pdev);181struct hwspinlock *lock;182struct omap_hwspinlock *omap_lock;183int i;184185for (i = 0; i < state->num_locks; i++) {186lock = hwspin_lock_unregister(i);187/* this shouldn't happen at this point. if it does, at least188* don't continue with the remove */189if (!lock) {190dev_err(&pdev->dev, "%s: failed on %d\n", __func__, i);191return -EBUSY;192}193194omap_lock = to_omap_hwspinlock(lock);195kfree(omap_lock);196}197198pm_runtime_disable(&pdev->dev);199iounmap(state->io_base);200kfree(state);201202return 0;203}204205static struct platform_driver omap_hwspinlock_driver = {206.probe = omap_hwspinlock_probe,207.remove = omap_hwspinlock_remove,208.driver = {209.name = "omap_hwspinlock",210},211};212213static int __init omap_hwspinlock_init(void)214{215return platform_driver_register(&omap_hwspinlock_driver);216}217/* board init code might need to reserve hwspinlocks for predefined purposes */218postcore_initcall(omap_hwspinlock_init);219220static void __exit omap_hwspinlock_exit(void)221{222platform_driver_unregister(&omap_hwspinlock_driver);223}224module_exit(omap_hwspinlock_exit);225226MODULE_LICENSE("GPL v2");227MODULE_DESCRIPTION("Hardware spinlock driver for OMAP");228MODULE_AUTHOR("Simon Que <[email protected]>");229MODULE_AUTHOR("Hari Kanigeri <[email protected]>");230MODULE_AUTHOR("Ohad Ben-Cohen <[email protected]>");231232233