1 // SPDX-License-Identifier: GPL-2.0-only
3 * Device State Control Registers driver
5 * Copyright (C) 2011 Texas Instruments Incorporated
6 * Author: Mark Salter <msalter@redhat.com>
10 * The Device State Control Registers (DSCR) provide SoC level control over
11 * a number of peripherals. Details vary considerably among the various SoC
12 * parts. In general, the DSCR block will provide one or more configuration
13 * registers often protected by a lock register. One or more key values must
14 * be written to a lock register in order to unlock the configuration register.
15 * The configuration register may be used to enable (and disable in some
16 * cases) SoC pin drivers, peripheral clock sources (internal or pin), etc.
17 * In some cases, a configuration register is write once or the individual
18 * bits are write once. That is, you may be able to enable a device, but
19 * will not be able to disable it.
21 * In addition to device configuration, the DSCR block may provide registers
22 * which are used to reset SoC peripherals, provide device ID information,
23 * provide MAC addresses, and other miscellaneous functions.
27 #include <linux/of_address.h>
28 #include <linux/of_platform.h>
29 #include <linux/module.h>
31 #include <linux/delay.h>
35 #define MAX_DEVSTATE_IDS 32
36 #define MAX_DEVCTL_REGS 8
37 #define MAX_DEVSTAT_REGS 8
38 #define MAX_LOCKED_REGS 4
39 #define MAX_SOC_EMACS 2
41 struct rmii_reset_reg
{
47 * Some registerd may be locked. In order to write to these
48 * registers, the key value must first be written to the lockreg.
51 u32 reg
; /* offset from base */
52 u32 lockreg
; /* offset from base */
53 u32 key
; /* unlock key */
57 * This describes a contiguous area of like control bits used to enable/disable
58 * SoC devices. Each controllable device is given an ID which is used by the
59 * individual device drivers to control the device state. These IDs start at
60 * zero and are assigned sequentially to the control bitfield ranges described
63 struct devstate_ctl_reg
{
64 u32 reg
; /* register holding the control bits */
65 u8 start_id
; /* start id of this range */
66 u8 num_ids
; /* number of devices in this range */
67 u8 enable_only
; /* bits are write-once to enable only */
68 u8 enable
; /* value used to enable device */
69 u8 disable
; /* value used to disable device */
70 u8 shift
; /* starting (rightmost) bit in range */
71 u8 nbits
; /* number of bits per device */
76 * This describes a region of status bits indicating the state of
77 * various devices. This is used internally to wait for status
78 * change completion when enabling/disabling a device. Status is
79 * optional and not all device controls will have a corresponding
82 struct devstate_stat_reg
{
83 u32 reg
; /* register holding the status bits */
84 u8 start_id
; /* start id of this range */
85 u8 num_ids
; /* number of devices in this range */
86 u8 enable
; /* value indicating enabled state */
87 u8 disable
; /* value indicating disabled state */
88 u8 shift
; /* starting (rightmost) bit in range */
89 u8 nbits
; /* number of bits per device */
92 struct devstate_info
{
93 struct devstate_ctl_reg
*ctl
;
94 struct devstate_stat_reg
*stat
;
97 /* These are callbacks to SOC-specific code. */
99 void (*init
)(struct device_node
*node
);
107 struct locked_reg locked
[MAX_LOCKED_REGS
];
108 struct devstate_info devstate_info
[MAX_DEVSTATE_IDS
];
109 struct rmii_reset_reg rmii_resets
[MAX_SOC_EMACS
];
110 struct devstate_ctl_reg devctl
[MAX_DEVCTL_REGS
];
111 struct devstate_stat_reg devstat
[MAX_DEVSTAT_REGS
];
114 static struct dscr_regs dscr
;
116 static struct locked_reg
*find_locked_reg(u32 reg
)
120 for (i
= 0; i
< MAX_LOCKED_REGS
; i
++)
121 if (dscr
.locked
[i
].key
&& reg
== dscr
.locked
[i
].reg
)
122 return &dscr
.locked
[i
];
127 * Write to a register with one lock
129 static void dscr_write_locked1(u32 reg
, u32 val
,
132 void __iomem
*reg_addr
= dscr
.base
+ reg
;
133 void __iomem
*lock_addr
= dscr
.base
+ lock
;
136 * For some registers, the lock is relocked after a short number
137 * of cycles. We have to put the lock write and register write in
138 * the same fetch packet to meet this timing. The .align ensures
139 * the two stw instructions are in the same fetch packet.
141 asm volatile ("b .s2 0f\n"
148 : "a"(reg_addr
), "b"(val
), "a"(lock_addr
), "b"(key
)
151 /* in case the hw doesn't reset the lock */
152 soc_writel(0, lock_addr
);
156 * Write to a register protected by two lock registers
158 static void dscr_write_locked2(u32 reg
, u32 val
,
162 soc_writel(key0
, dscr
.base
+ lock0
);
163 soc_writel(key1
, dscr
.base
+ lock1
);
164 soc_writel(val
, dscr
.base
+ reg
);
165 soc_writel(0, dscr
.base
+ lock0
);
166 soc_writel(0, dscr
.base
+ lock1
);
169 static void dscr_write(u32 reg
, u32 val
)
171 struct locked_reg
*lock
;
173 lock
= find_locked_reg(reg
);
175 dscr_write_locked1(reg
, val
, lock
->lockreg
, lock
->key
);
176 else if (dscr
.kick_key
[0])
177 dscr_write_locked2(reg
, val
, dscr
.kick_reg
[0], dscr
.kick_key
[0],
178 dscr
.kick_reg
[1], dscr
.kick_key
[1]);
180 soc_writel(val
, dscr
.base
+ reg
);
185 * Drivers can use this interface to enable/disable SoC IP blocks.
187 void dscr_set_devstate(int id
, enum dscr_devstate_t state
)
189 struct devstate_ctl_reg
*ctl
;
190 struct devstate_stat_reg
*stat
;
191 struct devstate_info
*info
;
193 int ctl_shift
, ctl_mask
;
199 if (id
< 0 || id
>= MAX_DEVSTATE_IDS
)
202 info
= &dscr
.devstate_info
[id
];
209 ctl_shift
= ctl
->shift
+ ctl
->nbits
* (id
- ctl
->start_id
);
210 ctl_mask
= ((1 << ctl
->nbits
) - 1) << ctl_shift
;
213 case DSCR_DEVSTATE_ENABLED
:
214 ctl_val
= ctl
->enable
<< ctl_shift
;
216 case DSCR_DEVSTATE_DISABLED
:
217 if (ctl
->enable_only
)
219 ctl_val
= ctl
->disable
<< ctl_shift
;
225 spin_lock_irqsave(&dscr
.lock
, flags
);
227 val
= soc_readl(dscr
.base
+ ctl
->reg
);
231 dscr_write(ctl
->reg
, val
);
233 spin_unlock_irqrestore(&dscr
.lock
, flags
);
238 ctl_shift
= stat
->shift
+ stat
->nbits
* (id
- stat
->start_id
);
240 if (state
== DSCR_DEVSTATE_ENABLED
)
241 ctl_val
= stat
->enable
;
243 ctl_val
= stat
->disable
;
246 val
= soc_readl(dscr
.base
+ stat
->reg
);
248 val
&= ((1 << stat
->nbits
) - 1);
249 } while (val
!= ctl_val
);
251 EXPORT_SYMBOL(dscr_set_devstate
);
254 * Drivers can use this to reset RMII module.
256 void dscr_rmii_reset(int id
, int assert)
258 struct rmii_reset_reg
*r
;
262 if (id
< 0 || id
>= MAX_SOC_EMACS
)
265 r
= &dscr
.rmii_resets
[id
];
269 spin_lock_irqsave(&dscr
.lock
, flags
);
271 val
= soc_readl(dscr
.base
+ r
->reg
);
273 dscr_write(r
->reg
, val
| r
->mask
);
275 dscr_write(r
->reg
, val
& ~(r
->mask
));
277 spin_unlock_irqrestore(&dscr
.lock
, flags
);
279 EXPORT_SYMBOL(dscr_rmii_reset
);
281 static void __init
dscr_parse_devstat(struct device_node
*node
,
287 err
= of_property_read_u32_array(node
, "ti,dscr-devstat", &val
, 1);
289 c6x_devstat
= soc_readl(base
+ val
);
290 printk(KERN_INFO
"DEVSTAT: %08x\n", c6x_devstat
);
293 static void __init
dscr_parse_silicon_rev(struct device_node
*node
,
299 err
= of_property_read_u32_array(node
, "ti,dscr-silicon-rev", vals
, 3);
301 c6x_silicon_rev
= soc_readl(base
+ vals
[0]);
302 c6x_silicon_rev
>>= vals
[1];
303 c6x_silicon_rev
&= vals
[2];
308 * Some SoCs will have a pair of fuse registers which hold
309 * an ethernet MAC address. The "ti,dscr-mac-fuse-regs"
310 * property is a mapping from fuse register bytes to MAC
311 * address bytes. The expected format is:
313 * ti,dscr-mac-fuse-regs = <reg0 b3 b2 b1 b0
316 * reg0 and reg1 are the offsets of the two fuse registers.
317 * b3-b0 positionally represent bytes within the fuse register.
318 * b3 is the most significant byte and b0 is the least.
319 * Allowable values for b3-b0 are:
321 * 0 = fuse register byte not used in MAC address
322 * 1-6 = index+1 into c6x_fuse_mac[]
324 static void __init
dscr_parse_mac_fuse(struct device_node
*node
,
330 err
= of_property_read_u32_array(node
, "ti,dscr-mac-fuse-regs",
335 for (f
= 0; f
< 2; f
++) {
336 fuse
= soc_readl(base
+ vals
[f
* 5]);
337 for (j
= (f
* 5) + 1, i
= 24; i
>= 0; i
-= 8, j
++)
338 if (vals
[j
] && vals
[j
] <= 6)
339 c6x_fuse_mac
[vals
[j
] - 1] = fuse
>> i
;
343 static void __init
dscr_parse_rmii_resets(struct device_node
*node
,
349 /* look for RMII reset registers */
350 p
= of_get_property(node
, "ti,dscr-rmii-resets", &size
);
352 /* parse all the reg/mask pairs we can handle */
353 size
/= (sizeof(*p
) * 2);
354 if (size
> MAX_SOC_EMACS
)
355 size
= MAX_SOC_EMACS
;
357 for (i
= 0; i
< size
; i
++) {
358 dscr
.rmii_resets
[i
].reg
= be32_to_cpup(p
++);
359 dscr
.rmii_resets
[i
].mask
= be32_to_cpup(p
++);
365 static void __init
dscr_parse_privperm(struct device_node
*node
,
371 err
= of_property_read_u32_array(node
, "ti,dscr-privperm", vals
, 2);
374 dscr_write(vals
[0], vals
[1]);
378 * SoCs may have "locked" DSCR registers which can only be written
379 * to only after writing a key value to a lock registers. These
380 * regisers can be described with the "ti,dscr-locked-regs" property.
381 * This property provides a list of register descriptions with each
382 * description consisting of three values.
384 * ti,dscr-locked-regs = <reg0 lockreg0 key0
386 * regN lockregN keyN>;
388 * reg is the offset of the locked register
389 * lockreg is the offset of the lock register
390 * key is the unlock key written to lockreg
393 static void __init
dscr_parse_locked_regs(struct device_node
*node
,
396 struct locked_reg
*r
;
400 p
= of_get_property(node
, "ti,dscr-locked-regs", &size
);
402 /* parse all the register descriptions we can handle */
403 size
/= (sizeof(*p
) * 3);
404 if (size
> MAX_LOCKED_REGS
)
405 size
= MAX_LOCKED_REGS
;
407 for (i
= 0; i
< size
; i
++) {
410 r
->reg
= be32_to_cpup(p
++);
411 r
->lockreg
= be32_to_cpup(p
++);
412 r
->key
= be32_to_cpup(p
++);
418 * SoCs may have DSCR registers which are only write enabled after
419 * writing specific key values to two registers. The two key registers
420 * and the key values can be parsed from a "ti,dscr-kick-regs"
421 * propety with the following layout:
423 * ti,dscr-kick-regs = <kickreg0 key0 kickreg1 key1>
425 * kickreg is the offset of the "kick" register
426 * key is the value which unlocks writing for protected regs
428 static void __init
dscr_parse_kick_regs(struct device_node
*node
,
434 err
= of_property_read_u32_array(node
, "ti,dscr-kick-regs", vals
, 4);
436 dscr
.kick_reg
[0] = vals
[0];
437 dscr
.kick_key
[0] = vals
[1];
438 dscr
.kick_reg
[1] = vals
[2];
439 dscr
.kick_key
[1] = vals
[3];
445 * SoCs may provide controls to enable/disable individual IP blocks. These
446 * controls in the DSCR usually control pin drivers but also may control
447 * clocking and or resets. The device tree is used to describe the bitfields
448 * in registers used to control device state. The number of bits and their
449 * values may vary even within the same register.
451 * The layout of these bitfields is described by the ti,dscr-devstate-ctl-regs
452 * property. This property is a list where each element describes a contiguous
453 * range of control fields with like properties. Each element of the list
454 * consists of 7 cells with the following values:
456 * start_id num_ids reg enable disable start_bit nbits
458 * start_id is device id for the first device control in the range
459 * num_ids is the number of device controls in the range
460 * reg is the offset of the register holding the control bits
461 * enable is the value to enable a device
462 * disable is the value to disable a device (0xffffffff if cannot disable)
463 * start_bit is the bit number of the first bit in the range
464 * nbits is the number of bits per device control
466 static void __init
dscr_parse_devstate_ctl_regs(struct device_node
*node
,
469 struct devstate_ctl_reg
*r
;
473 p
= of_get_property(node
, "ti,dscr-devstate-ctl-regs", &size
);
475 /* parse all the ranges we can handle */
476 size
/= (sizeof(*p
) * 7);
477 if (size
> MAX_DEVCTL_REGS
)
478 size
= MAX_DEVCTL_REGS
;
480 for (i
= 0; i
< size
; i
++) {
483 r
->start_id
= be32_to_cpup(p
++);
484 r
->num_ids
= be32_to_cpup(p
++);
485 r
->reg
= be32_to_cpup(p
++);
486 r
->enable
= be32_to_cpup(p
++);
487 r
->disable
= be32_to_cpup(p
++);
488 if (r
->disable
== 0xffffffff)
490 r
->shift
= be32_to_cpup(p
++);
491 r
->nbits
= be32_to_cpup(p
++);
493 for (j
= r
->start_id
;
494 j
< (r
->start_id
+ r
->num_ids
);
496 dscr
.devstate_info
[j
].ctl
= r
;
502 * SoCs may provide status registers indicating the state (enabled/disabled) of
503 * devices on the SoC. The device tree is used to describe the bitfields in
504 * registers used to provide device status. The number of bits and their
505 * values used to provide status may vary even within the same register.
507 * The layout of these bitfields is described by the ti,dscr-devstate-stat-regs
508 * property. This property is a list where each element describes a contiguous
509 * range of status fields with like properties. Each element of the list
510 * consists of 7 cells with the following values:
512 * start_id num_ids reg enable disable start_bit nbits
514 * start_id is device id for the first device status in the range
515 * num_ids is the number of devices covered by the range
516 * reg is the offset of the register holding the status bits
517 * enable is the value indicating device is enabled
518 * disable is the value indicating device is disabled
519 * start_bit is the bit number of the first bit in the range
520 * nbits is the number of bits per device status
522 static void __init
dscr_parse_devstate_stat_regs(struct device_node
*node
,
525 struct devstate_stat_reg
*r
;
529 p
= of_get_property(node
, "ti,dscr-devstate-stat-regs", &size
);
531 /* parse all the ranges we can handle */
532 size
/= (sizeof(*p
) * 7);
533 if (size
> MAX_DEVSTAT_REGS
)
534 size
= MAX_DEVSTAT_REGS
;
536 for (i
= 0; i
< size
; i
++) {
537 r
= &dscr
.devstat
[i
];
539 r
->start_id
= be32_to_cpup(p
++);
540 r
->num_ids
= be32_to_cpup(p
++);
541 r
->reg
= be32_to_cpup(p
++);
542 r
->enable
= be32_to_cpup(p
++);
543 r
->disable
= be32_to_cpup(p
++);
544 r
->shift
= be32_to_cpup(p
++);
545 r
->nbits
= be32_to_cpup(p
++);
547 for (j
= r
->start_id
;
548 j
< (r
->start_id
+ r
->num_ids
);
550 dscr
.devstate_info
[j
].stat
= r
;
555 static struct of_device_id dscr_ids
[] __initdata
= {
556 { .compatible
= "ti,c64x+dscr" },
561 * Probe for DSCR area.
563 * This has to be done early on in case timer or interrupt controller
564 * needs something. e.g. On C6455 SoC, timer must be enabled through
565 * DSCR before it is functional.
567 void __init
dscr_probe(void)
569 struct device_node
*node
;
572 spin_lock_init(&dscr
.lock
);
574 node
= of_find_matching_node(NULL
, dscr_ids
);
578 base
= of_iomap(node
, 0);
586 dscr_parse_devstat(node
, base
);
587 dscr_parse_silicon_rev(node
, base
);
588 dscr_parse_mac_fuse(node
, base
);
589 dscr_parse_rmii_resets(node
, base
);
590 dscr_parse_locked_regs(node
, base
);
591 dscr_parse_kick_regs(node
, base
);
592 dscr_parse_devstate_ctl_regs(node
, base
);
593 dscr_parse_devstate_stat_regs(node
, base
);
594 dscr_parse_privperm(node
, base
);