aboutsummaryrefslogtreecommitdiff
path: root/arch/c6x/platforms/dscr.c
blob: f848a65ee64687608903b2e7a8eb55fcb719950b (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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
 *  Device State Control Registers driver
 *
 *  Copyright (C) 2011 Texas Instruments Incorporated
 *  Author: Mark Salter <msalter@redhat.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.
 */

/*
 * The Device State Control Registers (DSCR) provide SoC level control over
 * a number of peripherals. Details vary considerably among the various SoC
 * parts. In general, the DSCR block will provide one or more configuration
 * registers often protected by a lock register. One or more key values must
 * be written to a lock register in order to unlock the configuration register.
 * The configuration register may be used to enable (and disable in some
 * cases) SoC pin drivers, peripheral clock sources (internal or pin), etc.
 * In some cases, a configuration register is write once or the individual
 * bits are write once. That is, you may be able to enable a device, but
 * will not be able to disable it.
 *
 * In addition to device configuration, the DSCR block may provide registers
 * which are used to reset SoC peripherals, provide device ID information,
 * provide MAC addresses, and other miscellaneous functions.
 */

#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <asm/soc.h>
#include <asm/dscr.h>

#define MAX_DEVSTATE_IDS   32
#define MAX_DEVCTL_REGS     8
#define MAX_DEVSTAT_REGS    8
#define MAX_LOCKED_REGS     4
#define MAX_SOC_EMACS       2

struct rmii_reset_reg {
	u32 reg;
	u32 mask;
};

/*
 * Some registerd may be locked. In order to write to these
 * registers, the key value must first be written to the lockreg.
 */
struct locked_reg {
	u32 reg;	/* offset from base */
	u32 lockreg;	/* offset from base */
	u32 key;	/* unlock key */
};

/*
 * This describes a contiguous area of like control bits used to enable/disable
 * SoC devices. Each controllable device is given an ID which is used by the
 * individual device drivers to control the device state. These IDs start at
 * zero and are assigned sequentially to the control bitfield ranges described
 * by this structure.
 */
struct devstate_ctl_reg {
	u32 reg;		/* register holding the control bits */
	u8  start_id;		/* start id of this range */
	u8  num_ids;		/* number of devices in this range */
	u8  enable_only;	/* bits are write-once to enable only */
	u8  enable;		/* value used to enable device */
	u8  disable;		/* value used to disable device */
	u8  shift;		/* starting (rightmost) bit in range */
	u8  nbits;		/* number of bits per device */
};


/*
 * This describes a region of status bits indicating the state of
 * various devices. This is used internally to wait for status
 * change completion when enabling/disabling a device. Status is
 * optional and not all device controls will have a corresponding
 * status.
 */
struct devstate_stat_reg {
	u32 reg;		/* register holding the status bits */
	u8  start_id;		/* start id of this range */
	u8  num_ids;		/* number of devices in this range */
	u8  enable;		/* value indicating enabled state */
	u8  disable;		/* value indicating disabled state */
	u8  shift;		/* starting (rightmost) bit in range */
	u8  nbits;		/* number of bits per device */
};

struct devstate_info {
	struct devstate_ctl_reg *ctl;
	struct devstate_stat_reg *stat;
};

/* These are callbacks to SOC-specific code. */
struct dscr_ops {
	void (*init)(struct device_node *node);
};

struct dscr_regs {
	spinlock_t		lock;
	void __iomem		*base;
	u32			kick_reg[2];
	u32			kick_key[2];
	struct locked_reg	locked[MAX_LOCKED_REGS];
	struct devstate_info	devstate_info[MAX_DEVSTATE_IDS];
	struct rmii_reset_reg   rmii_resets[MAX_SOC_EMACS];
	struct devstate_ctl_reg devctl[MAX_DEVCTL_REGS];
	struct devstate_stat_reg devstat[MAX_DEVSTAT_REGS];
};

static struct dscr_regs	dscr;

static struct locked_reg *find_locked_reg(u32 reg)
{
	int i;

	for (i = 0; i < MAX_LOCKED_REGS; i++)
		if (dscr.locked[i].key && reg == dscr.locked[i].reg)
			return &dscr.locked[i];
	return NULL;
}

/*
 * Write to a register with one lock
 */
static void dscr_write_locked1(u32 reg, u32 val,
			       u32 lock, u32 key)
{
	void __iomem *reg_addr = dscr.base + reg;
	void __iomem *lock_addr = dscr.base + lock;

	/*
	 * For some registers, the lock is relocked after a short number
	 * of cycles. We have to put the lock write and register write in
	 * the same fetch packet to meet this timing. The .align ensures
	 * the two stw instructions are in the same fetch packet.
	 */
	asm volatile ("b	.s2	0f\n"
		      "nop	5\n"
		      "    .align 5\n"
		      "0:\n"
		      "stw	.D1T2	%3,*%2\n"
		      "stw	.D1T2	%1,*%0\n"
		      :
		      : "a"(reg_addr), "b"(val), "a"(lock_addr), "b"(key)
		);

	/* in case the hw doesn't reset the lock */
	soc_writel(0, lock_addr);
}

/*
 * Write to a register protected by two lock registers
 */
static void dscr_write_locked2(u32 reg, u32 val,
			       u32 lock0, u32 key0,
			       u32 lock1, u32 key1)
{
	soc_writel(key0, dscr.base + lock0);
	soc_writel(key1, dscr.base + lock1);
	soc_writel(val, dscr.base + reg);
	soc_writel(0, dscr.base + lock0);
	soc_writel(0, dscr.base + lock1);
}

static void dscr_write(u32 reg, u32 val)
{
	struct locked_reg *lock;

	lock = find_locked_reg(reg);
	if (lock)
		dscr_write_locked1(reg, val, lock->lockreg, lock->key);
	else if (dscr.kick_key[0])
		dscr_write_locked2(reg, val, dscr.kick_reg[0], dscr.kick_key[0],
				   dscr.kick_reg[1], dscr.kick_key[1]);
	else
		soc_writel(val, dscr.base + reg);
}


/*
 * Drivers can use this interface to enable/disable SoC IP blocks.
 */
void dscr_set_devstate(int id, enum dscr_devstate_t state)
{
	struct devstate_ctl_reg *ctl;
	struct devstate_stat_reg *stat;
	struct devstate_info *info;
	u32 ctl_val, val;
	int ctl_shift, ctl_mask;
	unsigned long flags;

	if (!dscr.base)
		return;

	if (id < 0 || id >= MAX_DEVSTATE_IDS)
		return;

	info = &dscr.devstate_info[id];
	ctl = info->ctl;
	stat = info->stat;

	if (ctl == NULL)
		return;

	ctl_shift = ctl->shift + ctl->nbits * (id - ctl->start_id);
	ctl_mask = ((1 << ctl->nbits) - 1) << ctl_shift;

	switch (state) {
	case DSCR_DEVSTATE_ENABLED:
		ctl_val = ctl->enable << ctl_shift;
		break;
	case DSCR_DEVSTATE_DISABLED:
		if (ctl->enable_only)
			return;
		ctl_val = ctl->disable << ctl_shift;
		break;
	default:
		return;
	}

	spin_lock_irqsave(&dscr.lock, flags);

	val = soc_readl(dscr.base + ctl->reg);
	val &= ~ctl_mask;
	val |= ctl_val;

	dscr_write(ctl->reg, val);

	spin_unlock_irqrestore(&dscr.lock, flags);

	if (!stat)
		return;

	ctl_shift = stat->shift + stat->nbits * (id - stat->start_id);

	if (state == DSCR_DEVSTATE_ENABLED)
		ctl_val = stat->enable;
	else
		ctl_val = stat->disable;

	do {
		val = soc_readl(dscr.base + stat->reg);
		val >>= ctl_shift;
		val &= ((1 << stat->nbits) - 1);
	} while (val != ctl_val);
}
EXPORT_SYMBOL(dscr_set_devstate);

/*
 * Drivers can use this to reset RMII module.
 */
void dscr_rmii_reset(int id, int assert)
{
	struct rmii_reset_reg *r;
	unsigned