4 * Copyright (C) 2010-2011 ST-Ericsson
6 * Implements u8500 semaphore handling for protocol 1, no interrupts.
8 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
9 * Heavily borrowed from the work of :
10 * Simon Que <sque@ti.com>
11 * Hari Kanigeri <h-kanigeri2@ti.com>
12 * Ohad Ben-Cohen <ohad@wizery.com>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * version 2 as published by the Free Software Foundation.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
24 #include <linux/delay.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/hwspinlock.h>
30 #include <linux/platform_device.h>
32 #include "hwspinlock_internal.h"
35 * Implementation of STE's HSem protocol 1 without interrutps.
36 * The only masterID we allow is '0x01' to force people to use
37 * HSems for synchronisation between processors rather than processes
41 #define U8500_MAX_SEMAPHORE 32 /* a total of 32 semaphore */
42 #define RESET_SEMAPHORE (0) /* free */
45 * CPU ID for master running u8500 kernel.
46 * Hswpinlocks should only be used to synchonise operations
47 * between the Cortex A9 core and the other CPUs. Hence
48 * forcing the masterID to a preset value.
50 #define HSEM_MASTER_ID 0x01
52 #define HSEM_REGISTER_OFFSET 0x08
54 #define HSEM_CTRL_REG 0x00
55 #define HSEM_ICRALL 0x90
56 #define HSEM_PROTOCOL_1 0x01
58 static int u8500_hsem_trylock(struct hwspinlock *lock)
60 void __iomem *lock_addr = lock->priv;
62 writel(HSEM_MASTER_ID, lock_addr);
64 /* get only first 4 bit and compare to masterID.
65 * if equal, we have the semaphore, otherwise
66 * someone else has it.
68 return (HSEM_MASTER_ID == (0x0F & readl(lock_addr)));
71 static void u8500_hsem_unlock(struct hwspinlock *lock)
73 void __iomem *lock_addr = lock->priv;
75 /* release the lock by writing 0 to it */
76 writel(RESET_SEMAPHORE, lock_addr);
80 * u8500: what value is recommended here ?
82 static void u8500_hsem_relax(struct hwspinlock *lock)
87 static const struct hwspinlock_ops u8500_hwspinlock_ops = {
88 .trylock = u8500_hsem_trylock,
89 .unlock = u8500_hsem_unlock,
90 .relax = u8500_hsem_relax,
93 static int __devinit u8500_hsem_probe(struct platform_device *pdev)
95 struct hwspinlock_pdata *pdata = pdev->dev.platform_data;
96 struct hwspinlock_device *bank;
97 struct hwspinlock *hwlock;
99 void __iomem *io_base;
100 int i, ret, num_locks = U8500_MAX_SEMAPHORE;
106 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
110 io_base = ioremap(res->start, resource_size(res));
114 /* make sure protocol 1 is selected */
115 val = readl(io_base + HSEM_CTRL_REG);
116 writel((val & ~HSEM_PROTOCOL_1), io_base + HSEM_CTRL_REG);
118 /* clear all interrupts */
119 writel(0xFFFF, io_base + HSEM_ICRALL);
121 bank = kzalloc(sizeof(*bank) + num_locks * sizeof(*hwlock), GFP_KERNEL);
127 platform_set_drvdata(pdev, bank);
129 for (i = 0, hwlock = &bank->lock[0]; i < num_locks; i++, hwlock++)
130 hwlock->priv = io_base + HSEM_REGISTER_OFFSET + sizeof(u32) * i;
132 /* no pm needed for HSem but required to comply with hwspilock core */
133 pm_runtime_enable(&pdev->dev);
135 ret = hwspin_lock_register(bank, &pdev->dev, &u8500_hwspinlock_ops,
136 pdata->base_id, num_locks);
143 pm_runtime_disable(&pdev->dev);
150 static int __devexit u8500_hsem_remove(struct platform_device *pdev)
152 struct hwspinlock_device *bank = platform_get_drvdata(pdev);
153 void __iomem *io_base = bank->lock[0].priv - HSEM_REGISTER_OFFSET;
156 /* clear all interrupts */
157 writel(0xFFFF, io_base + HSEM_ICRALL);
159 ret = hwspin_lock_unregister(bank);
161 dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
165 pm_runtime_disable(&pdev->dev);
172 static struct platform_driver u8500_hsem_driver = {
173 .probe = u8500_hsem_probe,
174 .remove = __devexit_p(u8500_hsem_remove),
176 .name = "u8500_hsem",
177 .owner = THIS_MODULE,
181 static int __init u8500_hsem_init(void)
183 return platform_driver_register(&u8500_hsem_driver);
185 /* board init code might need to reserve hwspinlocks for predefined purposes */
186 postcore_initcall(u8500_hsem_init);
188 static void __exit u8500_hsem_exit(void)
190 platform_driver_unregister(&u8500_hsem_driver);
192 module_exit(u8500_hsem_exit);
194 MODULE_LICENSE("GPL v2");
195 MODULE_DESCRIPTION("Hardware Spinlock driver for u8500");
196 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");