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