root/drivers/hwspinlock/sprd_hwspinlock.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sprd_hwspinlock_trylock
  2. sprd_hwspinlock_unlock
  3. sprd_hwspinlock_relax
  4. sprd_hwspinlock_probe
  5. sprd_hwspinlock_remove
  6. sprd_hwspinlock_init
  7. sprd_hwspinlock_exit

   1 // SPDX-License-Identifier: GPL-2.0
   2 /*
   3  * Spreadtrum hardware spinlock driver
   4  * Copyright (C) 2017 Spreadtrum  - http://www.spreadtrum.com
   5  */
   6 
   7 #include <linux/bitops.h>
   8 #include <linux/clk.h>
   9 #include <linux/delay.h>
  10 #include <linux/device.h>
  11 #include <linux/hwspinlock.h>
  12 #include <linux/io.h>
  13 #include <linux/kernel.h>
  14 #include <linux/module.h>
  15 #include <linux/of.h>
  16 #include <linux/of_device.h>
  17 #include <linux/platform_device.h>
  18 #include <linux/pm_runtime.h>
  19 #include <linux/slab.h>
  20 
  21 #include "hwspinlock_internal.h"
  22 
  23 /* hwspinlock registers definition */
  24 #define HWSPINLOCK_RECCTRL              0x4
  25 #define HWSPINLOCK_MASTERID(_X_)        (0x80 + 0x4 * (_X_))
  26 #define HWSPINLOCK_TOKEN(_X_)           (0x800 + 0x4 * (_X_))
  27 
  28 /* unlocked value */
  29 #define HWSPINLOCK_NOTTAKEN             0x55aa10c5
  30 /* bits definition of RECCTRL reg */
  31 #define HWSPINLOCK_USER_BITS            0x1
  32 
  33 /* hwspinlock number */
  34 #define SPRD_HWLOCKS_NUM                32
  35 
  36 struct sprd_hwspinlock_dev {
  37         void __iomem *base;
  38         struct clk *clk;
  39         struct hwspinlock_device bank;
  40 };
  41 
  42 /* try to lock the hardware spinlock */
  43 static int sprd_hwspinlock_trylock(struct hwspinlock *lock)
  44 {
  45         struct sprd_hwspinlock_dev *sprd_hwlock =
  46                 dev_get_drvdata(lock->bank->dev);
  47         void __iomem *addr = lock->priv;
  48         int user_id, lock_id;
  49 
  50         if (!readl(addr))
  51                 return 1;
  52 
  53         lock_id = hwlock_to_id(lock);
  54         /* get the hardware spinlock master/user id */
  55         user_id = readl(sprd_hwlock->base + HWSPINLOCK_MASTERID(lock_id));
  56         dev_warn(sprd_hwlock->bank.dev,
  57                  "hwspinlock [%d] lock failed and master/user id = %d!\n",
  58                  lock_id, user_id);
  59         return 0;
  60 }
  61 
  62 /* unlock the hardware spinlock */
  63 static void sprd_hwspinlock_unlock(struct hwspinlock *lock)
  64 {
  65         void __iomem *lock_addr = lock->priv;
  66 
  67         writel(HWSPINLOCK_NOTTAKEN, lock_addr);
  68 }
  69 
  70 /* The specs recommended below number as the retry delay time */
  71 static void sprd_hwspinlock_relax(struct hwspinlock *lock)
  72 {
  73         ndelay(10);
  74 }
  75 
  76 static const struct hwspinlock_ops sprd_hwspinlock_ops = {
  77         .trylock = sprd_hwspinlock_trylock,
  78         .unlock = sprd_hwspinlock_unlock,
  79         .relax = sprd_hwspinlock_relax,
  80 };
  81 
  82 static int sprd_hwspinlock_probe(struct platform_device *pdev)
  83 {
  84         struct sprd_hwspinlock_dev *sprd_hwlock;
  85         struct hwspinlock *lock;
  86         struct resource *res;
  87         int i, ret;
  88 
  89         if (!pdev->dev.of_node)
  90                 return -ENODEV;
  91 
  92         sprd_hwlock = devm_kzalloc(&pdev->dev,
  93                                    sizeof(struct sprd_hwspinlock_dev) +
  94                                    SPRD_HWLOCKS_NUM * sizeof(*lock),
  95                                    GFP_KERNEL);
  96         if (!sprd_hwlock)
  97                 return -ENOMEM;
  98 
  99         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 100         sprd_hwlock->base = devm_ioremap_resource(&pdev->dev, res);
 101         if (IS_ERR(sprd_hwlock->base))
 102                 return PTR_ERR(sprd_hwlock->base);
 103 
 104         sprd_hwlock->clk = devm_clk_get(&pdev->dev, "enable");
 105         if (IS_ERR(sprd_hwlock->clk)) {
 106                 dev_err(&pdev->dev, "get hwspinlock clock failed!\n");
 107                 return PTR_ERR(sprd_hwlock->clk);
 108         }
 109 
 110         clk_prepare_enable(sprd_hwlock->clk);
 111 
 112         /* set the hwspinlock to record user id to identify subsystems */
 113         writel(HWSPINLOCK_USER_BITS, sprd_hwlock->base + HWSPINLOCK_RECCTRL);
 114 
 115         for (i = 0; i < SPRD_HWLOCKS_NUM; i++) {
 116                 lock = &sprd_hwlock->bank.lock[i];
 117                 lock->priv = sprd_hwlock->base + HWSPINLOCK_TOKEN(i);
 118         }
 119 
 120         platform_set_drvdata(pdev, sprd_hwlock);
 121         pm_runtime_enable(&pdev->dev);
 122 
 123         ret = hwspin_lock_register(&sprd_hwlock->bank, &pdev->dev,
 124                                    &sprd_hwspinlock_ops, 0, SPRD_HWLOCKS_NUM);
 125         if (ret) {
 126                 pm_runtime_disable(&pdev->dev);
 127                 clk_disable_unprepare(sprd_hwlock->clk);
 128                 return ret;
 129         }
 130 
 131         return 0;
 132 }
 133 
 134 static int sprd_hwspinlock_remove(struct platform_device *pdev)
 135 {
 136         struct sprd_hwspinlock_dev *sprd_hwlock = platform_get_drvdata(pdev);
 137 
 138         hwspin_lock_unregister(&sprd_hwlock->bank);
 139         pm_runtime_disable(&pdev->dev);
 140         clk_disable_unprepare(sprd_hwlock->clk);
 141         return 0;
 142 }
 143 
 144 static const struct of_device_id sprd_hwspinlock_of_match[] = {
 145         { .compatible = "sprd,hwspinlock-r3p0", },
 146         { /* sentinel */ }
 147 };
 148 MODULE_DEVICE_TABLE(of, sprd_hwspinlock_of_match);
 149 
 150 static struct platform_driver sprd_hwspinlock_driver = {
 151         .probe = sprd_hwspinlock_probe,
 152         .remove = sprd_hwspinlock_remove,
 153         .driver = {
 154                 .name = "sprd_hwspinlock",
 155                 .of_match_table = of_match_ptr(sprd_hwspinlock_of_match),
 156         },
 157 };
 158 
 159 static int __init sprd_hwspinlock_init(void)
 160 {
 161         return platform_driver_register(&sprd_hwspinlock_driver);
 162 }
 163 postcore_initcall(sprd_hwspinlock_init);
 164 
 165 static void __exit sprd_hwspinlock_exit(void)
 166 {
 167         platform_driver_unregister(&sprd_hwspinlock_driver);
 168 }
 169 module_exit(sprd_hwspinlock_exit);
 170 
 171 MODULE_LICENSE("GPL v2");
 172 MODULE_DESCRIPTION("Hardware spinlock driver for Spreadtrum");
 173 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
 174 MODULE_AUTHOR("Lanqing Liu <lanqing.liu@spreadtrum.com>");
 175 MODULE_AUTHOR("Long Cheng <aiden.cheng@spreadtrum.com>");

/* [<][>][^][v][top][bottom][index][help] */