summaryrefslogtreecommitdiff
path: root/drivers/hwspinlock/sprd_hwspinlock.c
blob: b8b9590604505f6dd41874f1a1aeeb7f0638dc1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
 * SCI hardware spinlock driver
 *
 * Copyright (C) 2012 Spreadtrum  - http://www.spreadtrum.com
 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
 *
 * Contact: steve.zhan <steve.zhan@spreadtrum.com>
 *        
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/bitops.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/hwspinlock.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_device.h>

#include <soc/sprd/sci.h>
#include <soc/sprd/sci_glb_regs.h>
#include <soc/sprd/arch_lock.h>
#include "hwspinlock_internal.h"

/* hwspinlock reg */
#define HWSPINLOCK_CLEAR			(0x0)
#define HWSPINLOCK_RECCTRL			(0x4)
#define HWSPINLOCK_TTLSTS			(0x8)
#define HWSPINLOCK_CLEAREN			(0xc)
#define HWSPINLOCK_FLAG0			(0x10)
#define HWSPINLOCK_FLAG1			(0x14)
#define HWSPINLOCK_FLAG2			(0x18)
#define HWSPINLOCK_FLAG3			(0x1c)
#define HWSPINLOCK_MASTERID(_X_)	(0x80 + 0x4*(_X_))
#define HWSPINLOCK_TOKEN_V0(_X_)	(0x80 + 0x4*(_X_))
#define HWSPINLOCK_TOKEN_V1(_X_)	(0x800 + 0x4*(_X_))
#define HWSPINLOCK_VERID			(0xFFC)

/* RECCTRL */
#define	HWSPINLOCK_ID				BIT(0)
#define	HWSPINLOCK_USER_BITS		BIT(1)

/* first processor */
#define THIS_PROCESSOR_KEY			(HWSPINLOCK_WRITE_KEY)

/* spinlock enable */
#define HWSPINLOCK_ENABLE_CLEAR		(0x454e434c)

/* hwspinlock controller */
#define	AON_HWSPLOCK				(1)
#define	AP_HWSPLOCK					(0)

struct sprd_hwspinlock_dev {
	unsigned int hwspinlock_vid;
	unsigned long hwspinlock_base;
	unsigned int hwspinlock_cnt;
	struct hwspinlock_device bank;
};

/* array[0] stands for lock0 */
unsigned char hwlocks_implemented[HWSPINLOCK_ID_TOTAL_NUMS];
unsigned char local_hwlocks_status[HWSPINLOCK_ID_TOTAL_NUMS];
struct hwspinlock *hwlocks[HWSPINLOCK_ID_TOTAL_NUMS];
int hwspinlock_vid_early;

static struct sprd_hwspinlock_dev *hwlock_to_sprdlock_dev(struct hwspinlock *hwlock)
{
	struct hwspinlock_device *hwbank;
	int lock_id;
	
	if(!hwlock)
		return NULL;

	lock_id = hwlock_to_id(hwlock);
	hwbank = container_of(hwlock,struct hwspinlock_device,lock[lock_id]);
	if(hwbank)
		return container_of(hwbank,struct sprd_hwspinlock_dev,bank);
	else
		return NULL;
}

#ifdef	CONFIG_ARCH_WHALE
int sprd_hwlock_init(void)
{
	return 0;
}
#else
int sprd_hwlock_init(void)
{
	writel_relaxed(BIT_SPINLOCK_EB, (void __iomem *)(REG_GLB_SET(REG_AP_AHB_AHB_EB)));
	writel_relaxed(BIT_SPLK_EB, (void __iomem *)(REG_GLB_SET(REG_AON_APB_APB_EB0)));
	return 0;
}
#endif

static void inline sprd_hwlocks_implemented(int hwlock_id)
{
	if (hwlock_id == AON_HWSPLOCK){
		hwlocks_implemented[HWLOCK_ADI] = 1;
		hwlocks_implemented[HWLOCK_GLB] = 1;
		hwlocks_implemented[HWLOCK_AGPIO] = 1;
		hwlocks_implemented[HWLOCK_AEIC] = 1;
		hwlocks_implemented[HWLOCK_ADC] = 1;
		hwlocks_implemented[HWLOCK_EFUSE] = 1;
	}else if (hwlock_id == AP_HWSPLOCK){
		/* Caution: ap hwspinlock id need add 31 */
		/* hwlocks_implemented[HWLOCK_ADI+31] = 1; */
	}
}

int sprd_record_hwlock_sts(unsigned int lock_id, unsigned int sts)
{
	if(lock_id >= HWSPINLOCK_ID_TOTAL_NUMS){
		printk(KERN_ERR "Hwspinlock id is wrong!\n");
		return -ENXIO;
	}

	local_hwlocks_status[lock_id] = sts;
	return sts;
}

int sprd_hwspinlock_isbusy(unsigned int lock_id)
{
	unsigned int status = 0;
	struct sprd_hwspinlock_dev *sprd_hwlock;

	if (!hwlocks[lock_id]){
		printk(KERN_ERR "This hwspinlock is not register!\n");
		return -ENODEV;
	}

	sprd_hwlock = hwlock_to_sprdlock_dev(hwlocks[lock_id]);
	if (!sprd_hwlock){
		printk(KERN_ERR "Can't find hwspinlock device!\n");
		return -ENODEV;
	}

	status = readl_relaxed((void __iomem *)(sprd_hwlock->hwspinlock_base + HWSPINLOCK_TTLSTS));
	if (status < 0)
		return -EIO;
	return ((status & (1 << lock_id)) ? 1 : 0);
}

int sprd_hwspinlocks_isbusy(void)
{
	int id;

	for(id = 0; id < HWSPINLOCK_ID_TOTAL_NUMS; id++){
		if(sprd_hwspinlock_isbusy(id) > 0)
			return 1;
	}
	return 0;
}

int sprd_hwspinlock_master_id(unsigned int lock_id)
{
	unsigned int master_id = 0;
	struct sprd_hwspinlock_dev *sprd_hwlock;

	if(!hwlocks[lock_id]){
		printk(KERN_ERR "This hwspinlock is not register!\n");
		return -ENODEV;
	}

	sprd_hwlock = hwlock_to_sprdlock_dev(hwlocks[lock_id]);
	if (!sprd_hwlock){
		printk(KERN_ERR "Can't find hwspinlock device!\n");
		return -ENODEV;
	}
	
	master_id = readl_relaxed((void __iomem *)(sprd_hwlock->hwspinlock_base + HWSPINLOCK_MASTERID(lock_id)));
	return master_id;
}

void hwspinlock_clear(unsigned int lock_id)
{
	struct sprd_hwspinlock_dev *sprd_hwlock;

	if(!hwlocks[lock_id]){
		printk(KERN_ERR "This hwspinlock is not register!\n");
		return;
	}

	sprd_hwlock = hwlock_to_sprdlock_dev(hwlocks[lock_id]);
	if (!sprd_hwlock){
		printk(KERN_ERR "Can't find hwspinlock device!\n");
		return;
	}
	/*setting the abnormal clear bit to 1 makes the corresponding
	 *lock to Not Taken state
	 */
	writel_relaxed(HWSPINLOCK_ENABLE_CLEAR,(void __iomem *)(sprd_hwlock->hwspinlock_base + HWSPINLOCK_CLEAREN));
	writel_relaxed((1 << lock_id), (void __iomem *)(sprd_hwlock->hwspinlock_base + HWSPINLOCK_CLEAR));
	writel_relaxed(~HWSPINLOCK_ENABLE_CLEAR, (void __iomem *)(sprd_hwlock->hwspinlock_base + HWSPINLOCK_CLEAREN));
}

void hwspinlock_clear_all(void)
{
	unsigned int lock_id = 0;
	do {
		hwspinlock_clear(lock_id);
	} while (lock_id++ < HWSPINLOCK_ID_TOTAL_NUMS);
}

static int sprd_check_hwspinlock_vid(struct hwspinlock *lock)
{
	struct sprd_hwspinlock_dev *sprd_hwlock;

	sprd_hwlock = hwlock_to_sprdlock_dev(lock);
	if (!sprd_hwlock){
		printk(KERN_ERR "%s:Can't find hwspinlock device!\n",__func__);
		return -ENODEV;
	}

	/* for sharkl/sharkl64/whale project */
	if (sprd_hwlock->hwspinlock_vid == 0x100 || sprd_hwlock->hwspinlock_vid == 0x200
		|| sprd_hwlock->hwspinlock_vid == 0x300){
		return 1;
	} else {
		printk(KERN_ERR "Warning:hwlock version id is 0x%x!\n",sprd_hwlock->hwspinlock_vid);
		return 0;
	}
}

static unsigned long sprd_hwspinlock_addr(unsigned int lock_id)
{
	unsigned long addr;
	struct hwspinlock *lock;

	if(lock_id >= HWSPINLOCK_ID_TOTAL_NUMS){
		printk(KERN_ERR "Hwspinlock id is wrong!\n");
		return 0;
	}

	lock = hwlocks[lock_id];
	if (!lock){
		printk(KERN_ERR "Call %s: can't find hwspinlock!\n",__func__);
		return 0;
	}
	else
		addr = (unsigned long)lock->priv;

	return addr;
}

static int sprd_init_hwlocks(int hwlock_id)
{
	struct hwspinlock **plock;
	int i;

	sprd_hwlocks_implemented(hwlock_id);

	if (hwlock_id == AON_HWSPLOCK)
		i = 0;
	else if (hwlock_id == AP_HWSPLOCK)
		i = 31;
	else
		i = HWSPINLOCK_ID_TOTAL_NUMS;

	for (; i < HWSPINLOCK_ID_TOTAL_NUMS; ++i) {
		if (hwlocks_implemented[i]) {
			plock = &hwlocks[i];
			*plock = hwspin_lock_request_specific(i);
			if (WARN_ON(IS_ERR_OR_NULL(*plock)))
				*plock = NULL;
			else
				pr_info("early alloc hwspinlock id %d\n",
					hwspin_lock_get_id(*plock));
		}
	}
	return 0;
}

static unsigned int do_lock_key(struct hwspinlock *lock)
{
	unsigned int key = THIS_PROCESSOR_KEY;
	return key;
}

static int __hwspinlock_trylock(struct hwspinlock *lock)
{
	void __iomem *addr = lock->priv;

	if (sprd_check_hwspinlock_vid(lock)) {
		if (!readl_relaxed(addr))
			goto __locked;
	} else {
		unsigned int key = do_lock_key(lock);
		if (!(key ^ HWSPINLOCK_NOTTAKEN_V0))
			BUG_ON(1);
		if (HWSPINLOCK_NOTTAKEN_V0 == readl_relaxed(addr)) {
			writel_relaxed(key, addr);
			if (key == readl_relaxed(addr))
				goto __locked;
		}
	}

	printk(KERN_ERR "Hwspinlock [%d] lock failed!\n",hwlock_to_id(lock));
	return 0;

__locked:
	sprd_record_hwlock_sts(hwlock_to_id(lock), 1);
	return 1;
}

static void __hwspinlock_unlock(struct hwspinlock *lock)
{
	void __iomem *lock_addr = lock->priv;
	int unlock_key;

	if (sprd_check_hwspinlock_vid(lock)) {
		unlock_key = HWSPINLOCK_NOTTAKEN_V1;
	} else {
		unlock_key = HWSPINLOCK_NOTTAKEN_V0;
		if (!(readl_relaxed(lock_addr) ^ unlock_key))
			BUG_ON(1);
	}

	writel_relaxed(unlock_key, lock_addr);
	sprd_record_hwlock_sts(hwlock_to_id(lock), 0);
}

/*
 * relax the  interconnect while spinning on it.
 *
 * The specs recommended that the retry delay time will be
 * just over half of the time that a requester would be
 * expected to hold the lock.
 *
 * The number below is taken from an hardware specs example,
 * obviously it is somewhat arbitrary.
 */
static void __hwspinlock_relax(struct hwspinlock *lock)
{
	ndelay(10);
}

static const struct hwspinlock_ops sprd_hwspinlock_ops = {
	.trylock = __hwspinlock_trylock,
	.unlock = __hwspinlock_unlock,
	.relax = __hwspinlock_relax,
};

static int sprd_hwspinlock_probe(struct platform_device *pdev)
{
	struct hwspinlock *hwlock;
	struct sprd_hwspinlock_dev *sprd_hwlock;
	static int hwspinlock_exist_cnt = 0;
	u32 num_locks = 0;
	int i, ret, hwlock_id;

	ret = of_alias_get_id(pdev->dev.of_node, "hwspinlock");
	if (IS_ERR_VALUE(ret)) {
		printk(KERN_ERR "Hwspinlock get alias failed!\n");
		return ret;
	}
	hwlock_id = ret;

	ret = of_property_read_u32(pdev->dev.of_node,"hwspinlock_cnt",&num_locks);
	if(ret){
		printk(KERN_ERR "Hwspinlock get count failed!\n");
		num_locks = 32;
	}

	sprd_hwlock = devm_kzalloc(&pdev->dev, sizeof(struct sprd_hwspinlock_dev) 
					+ num_locks * sizeof(*hwlock), GFP_KERNEL);
	if (!sprd_hwlock) {
		printk(KERN_ERR "Hwspinlock device alloc memory failed!\n");
		return -ENOMEM;
	}

	/* intial the hwspinlock */
	sprd_hwlock_init();
	sprd_hwlock->hwspinlock_cnt = num_locks;
	if (hwlock_id == 1)
		sprd_hwlock->hwspinlock_base = SPRD_HWSPINLOCK1_BASE;
	else if (hwlock_id == 0)
		sprd_hwlock->hwspinlock_base = SPRD_HWSPINLOCK0_BASE;
	else {
		printk(KERN_ERR "Hwspinlock id is wrong!\n");
		return -ENODEV;
	}
	sprd_hwlock->hwspinlock_vid = readl_relaxed((void __iomem *)(sprd_hwlock->hwspinlock_base + HWSPINLOCK_VERID));

	for (i = 0, hwlock = &sprd_hwlock->bank.lock[0]; i < num_locks; i++, hwlock++) {
		if (sprd_hwlock->hwspinlock_vid == 0x100 || sprd_hwlock->hwspinlock_vid == 0x200
			|| sprd_hwlock->hwspinlock_vid == 0x300)
			hwlock->priv = (void __iomem *)(sprd_hwlock->hwspinlock_base + HWSPINLOCK_TOKEN_V1(i));
		else
			hwlock->priv = (void __iomem *)(sprd_hwlock->hwspinlock_base + HWSPINLOCK_TOKEN_V0(i));
	}

	/*
	 * runtime PM will make sure the clock of this module is
	 * enabled if at least one lock is requested
	 */
	platform_set_drvdata(pdev, sprd_hwlock);
	pm_runtime_enable(&pdev->dev);

	ret = hwspin_lock_register(&sprd_hwlock->bank, &pdev->dev, &sprd_hwspinlock_ops,
				   hwspinlock_exist_cnt, num_locks);
	if (ret){
		printk(KERN_ERR "Hwspinlock register failed!\n");
		goto reg_fail;
	}

	hwspinlock_exist_cnt += num_locks;
	/* inital the hwlocks */
	sprd_init_hwlocks(hwlock_id);
#ifdef	CONFIG_ARCH_WHALE
	/* record master user bits */
	writel_relaxed(HWSPINLOCK_USER_BITS, (void __iomem *)(sprd_hwlock->hwspinlock_base + HWSPINLOCK_RECCTRL));
#endif

	printk("sprd hwspinlock probe ok: hwspinlock name = %s, hwspinlock_vid = 0x%x,"
			"hwspinlock_exist_cnt = %d\n", pdev->name,sprd_hwlock->hwspinlock_vid,hwspinlock_exist_cnt); 

	return 0;

reg_fail:
	pm_runtime_disable(&pdev->dev);
	return ret;
}

static int sprd_hwspinlock_remove(struct platform_device *pdev)
{
	struct sprd_hwspinlock_dev *sprd_hwlock = platform_get_drvdata(pdev);
	int ret;

	ret = hwspin_lock_unregister(&sprd_hwlock->bank);
	if (ret) {
		dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
		return ret;
	}

	pm_runtime_disable(&pdev->dev);
	return 0;
}

static const struct of_device_id sprd_hwspinlock_of_match[] = {
	{ .compatible = "sprd,sprd-hwspinlock", },
	{ /* sentinel */ }
};

static struct platform_driver sprd_hwspinlock_driver = {
	.probe = sprd_hwspinlock_probe,
	.remove = sprd_hwspinlock_remove,
	.driver = {
		.name = "sprd_hwspinlock",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(sprd_hwspinlock_of_match),
	},
};

static int __init sprd_hwspinlock_init(void)
{
	return platform_driver_register(&sprd_hwspinlock_driver);
}

/* board init code might need to reserve hwspinlocks for predefined purposes */
postcore_initcall(sprd_hwspinlock_init);

static void __exit sprd_hwspinlock_exit(void)
{
	platform_driver_unregister(&sprd_hwspinlock_driver);
}

module_exit(sprd_hwspinlock_exit);

static int remove_node(struct device_node *np)
{
	struct property *ps;

	ps = kzalloc(sizeof(*ps), GFP_KERNEL);
	if (!ps)
		return -1;

	ps->value = kstrdup("no", GFP_KERNEL);
	ps->length = strlen(ps->value);
	ps->name = kstrdup("status", GFP_KERNEL);
	of_update_property(np, ps);
	printk("After register,hwspinlock1 node available=%d\n",of_device_is_available(np));

	return 0;
}

int __init early_hwspinlocks_init(void)
{
	struct device_node *np;
	struct platform_device *pdev;
	int ret;

	np = of_find_node_by_name(NULL, "hwspinlock1");
	if (!np) {
		pr_warn("Can't get the hwspinlock1 node!\n");
		return -ENODEV;
	}

	pdev = of_platform_device_create(np, 0, NULL);
	if (!pdev) {
		pr_warn("register hwspinlock1 failed!\n");
		return -ENODEV;
	}

	pr_info("SPRD register hwspinlock1 ok,hwspinlock1's name is %s!\n",pdev->name);
	ret = remove_node(np);
	return ret;
}

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Hardware spinlock driver for Spreadtrum");
MODULE_AUTHOR("baolin.wang <baolin.wang@spreadtrum.com>");