xref: /linux/arch/arm/mach-zynq/slcr.c (revision 871c6971ec38d485fa601f6d9f60cb8d25a5aae1)
164b889b3SMichal Simek /*
264b889b3SMichal Simek  * Xilinx SLCR driver
364b889b3SMichal Simek  *
464b889b3SMichal Simek  * Copyright (c) 2011-2013 Xilinx Inc.
564b889b3SMichal Simek  *
664b889b3SMichal Simek  * This program is free software; you can redistribute it and/or
764b889b3SMichal Simek  * modify it under the terms of the GNU General Public License
864b889b3SMichal Simek  * as published by the Free Software Foundation; either version
964b889b3SMichal Simek  * 2 of the License, or (at your option) any later version.
1064b889b3SMichal Simek  *
1164b889b3SMichal Simek  * You should have received a copy of the GNU General Public
1264b889b3SMichal Simek  * License along with this program; if not, write to the Free
1364b889b3SMichal Simek  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
1464b889b3SMichal Simek  * 02139, USA.
1564b889b3SMichal Simek  */
1664b889b3SMichal Simek 
1764b889b3SMichal Simek #include <linux/io.h>
18016f4dcaSMichal Simek #include <linux/mfd/syscon.h>
1964b889b3SMichal Simek #include <linux/of_address.h>
20016f4dcaSMichal Simek #include <linux/regmap.h>
2164b889b3SMichal Simek #include <linux/clk/zynq.h>
2264b889b3SMichal Simek #include "common.h"
2364b889b3SMichal Simek 
24b5f177ffSSoren Brinkmann /* register offsets */
25b5f177ffSSoren Brinkmann #define SLCR_UNLOCK_OFFSET		0x8   /* SCLR unlock register */
2696790f0aSMichal Simek #define SLCR_PS_RST_CTRL_OFFSET		0x200 /* PS Software Reset Control */
27b5f177ffSSoren Brinkmann #define SLCR_A9_CPU_RST_CTRL_OFFSET	0x244 /* CPU Software Reset Control */
28b5f177ffSSoren Brinkmann #define SLCR_REBOOT_STATUS_OFFSET	0x258 /* PS Reboot Status */
29aa7eb2bbSMichal Simek 
30b5f177ffSSoren Brinkmann #define SLCR_UNLOCK_MAGIC		0xDF0D
31aa7eb2bbSMichal Simek #define SLCR_A9_CPU_CLKSTOP		0x10
32aa7eb2bbSMichal Simek #define SLCR_A9_CPU_RST			0x1
33aa7eb2bbSMichal Simek 
347b274efeSSteffen Trumtrar static void __iomem *zynq_slcr_base;
35016f4dcaSMichal Simek static struct regmap *zynq_slcr_regmap;
3664b889b3SMichal Simek 
3764b889b3SMichal Simek /**
38*871c6971SMichal Simek  * zynq_slcr_write - Write to a register in SLCR block
39*871c6971SMichal Simek  *
40*871c6971SMichal Simek  * @val:	Value to write to the register
41*871c6971SMichal Simek  * @offset:	Register offset in SLCR block
42*871c6971SMichal Simek  *
43*871c6971SMichal Simek  * Return:	a negative value on error, 0 on success
44*871c6971SMichal Simek  */
45*871c6971SMichal Simek static int zynq_slcr_write(u32 val, u32 offset)
46*871c6971SMichal Simek {
47*871c6971SMichal Simek 	if (!zynq_slcr_regmap) {
48*871c6971SMichal Simek 		writel(val, zynq_slcr_base + offset);
49*871c6971SMichal Simek 		return 0;
50*871c6971SMichal Simek 	}
51*871c6971SMichal Simek 
52*871c6971SMichal Simek 	return regmap_write(zynq_slcr_regmap, offset, val);
53*871c6971SMichal Simek }
54*871c6971SMichal Simek 
55*871c6971SMichal Simek /**
56*871c6971SMichal Simek  * zynq_slcr_read - Read a register in SLCR block
57*871c6971SMichal Simek  *
58*871c6971SMichal Simek  * @val:	Pointer to value to be read from SLCR
59*871c6971SMichal Simek  * @offset:	Register offset in SLCR block
60*871c6971SMichal Simek  *
61*871c6971SMichal Simek  * Return:	a negative value on error, 0 on success
62*871c6971SMichal Simek  */
63*871c6971SMichal Simek static int zynq_slcr_read(u32 *val, u32 offset)
64*871c6971SMichal Simek {
65*871c6971SMichal Simek 	if (zynq_slcr_regmap)
66*871c6971SMichal Simek 		return regmap_read(zynq_slcr_regmap, offset, val);
67*871c6971SMichal Simek 
68*871c6971SMichal Simek 	*val = readl(zynq_slcr_base + offset);
69*871c6971SMichal Simek 
70*871c6971SMichal Simek 	return 0;
71*871c6971SMichal Simek }
72*871c6971SMichal Simek 
73*871c6971SMichal Simek /**
7496790f0aSMichal Simek  * zynq_slcr_system_reset - Reset the entire system.
7596790f0aSMichal Simek  */
7696790f0aSMichal Simek void zynq_slcr_system_reset(void)
7796790f0aSMichal Simek {
7896790f0aSMichal Simek 	u32 reboot;
7996790f0aSMichal Simek 
8096790f0aSMichal Simek 	/*
8196790f0aSMichal Simek 	 * Unlock the SLCR then reset the system.
8296790f0aSMichal Simek 	 * Note that this seems to require raw i/o
8396790f0aSMichal Simek 	 * functions or there's a lockup?
8496790f0aSMichal Simek 	 */
85b5f177ffSSoren Brinkmann 	writel(SLCR_UNLOCK_MAGIC, zynq_slcr_base + SLCR_UNLOCK_OFFSET);
8696790f0aSMichal Simek 
8796790f0aSMichal Simek 	/*
8896790f0aSMichal Simek 	 * Clear 0x0F000000 bits of reboot status register to workaround
8996790f0aSMichal Simek 	 * the FSBL not loading the bitstream after soft-reboot
9096790f0aSMichal Simek 	 * This is a temporary solution until we know more.
9196790f0aSMichal Simek 	 */
92*871c6971SMichal Simek 	zynq_slcr_read(&reboot, SLCR_REBOOT_STATUS_OFFSET);
93*871c6971SMichal Simek 	zynq_slcr_write(reboot & 0xF0FFFFFF, SLCR_REBOOT_STATUS_OFFSET);
94*871c6971SMichal Simek 	zynq_slcr_write(1, SLCR_PS_RST_CTRL_OFFSET);
9596790f0aSMichal Simek }
9696790f0aSMichal Simek 
9796790f0aSMichal Simek /**
98aa7eb2bbSMichal Simek  * zynq_slcr_cpu_start - Start cpu
99aa7eb2bbSMichal Simek  * @cpu:	cpu number
100aa7eb2bbSMichal Simek  */
101aa7eb2bbSMichal Simek void zynq_slcr_cpu_start(int cpu)
102aa7eb2bbSMichal Simek {
103*871c6971SMichal Simek 	u32 reg;
104*871c6971SMichal Simek 
105*871c6971SMichal Simek 	zynq_slcr_read(&reg, SLCR_A9_CPU_RST_CTRL_OFFSET);
1063db9e860SSoren Brinkmann 	reg &= ~(SLCR_A9_CPU_RST << cpu);
107*871c6971SMichal Simek 	zynq_slcr_write(reg, SLCR_A9_CPU_RST_CTRL_OFFSET);
1083db9e860SSoren Brinkmann 	reg &= ~(SLCR_A9_CPU_CLKSTOP << cpu);
109*871c6971SMichal Simek 	zynq_slcr_write(reg, SLCR_A9_CPU_RST_CTRL_OFFSET);
110aa7eb2bbSMichal Simek }
111aa7eb2bbSMichal Simek 
112aa7eb2bbSMichal Simek /**
113aa7eb2bbSMichal Simek  * zynq_slcr_cpu_stop - Stop cpu
114aa7eb2bbSMichal Simek  * @cpu:	cpu number
115aa7eb2bbSMichal Simek  */
116aa7eb2bbSMichal Simek void zynq_slcr_cpu_stop(int cpu)
117aa7eb2bbSMichal Simek {
118*871c6971SMichal Simek 	u32 reg;
119*871c6971SMichal Simek 
120*871c6971SMichal Simek 	zynq_slcr_read(&reg, SLCR_A9_CPU_RST_CTRL_OFFSET);
1213db9e860SSoren Brinkmann 	reg |= (SLCR_A9_CPU_CLKSTOP | SLCR_A9_CPU_RST) << cpu;
122*871c6971SMichal Simek 	zynq_slcr_write(reg, SLCR_A9_CPU_RST_CTRL_OFFSET);
123aa7eb2bbSMichal Simek }
124aa7eb2bbSMichal Simek 
125aa7eb2bbSMichal Simek /**
126016f4dcaSMichal Simek  * zynq_slcr_init - Regular slcr driver init
127016f4dcaSMichal Simek  *
128016f4dcaSMichal Simek  * Return:	0 on success, negative errno otherwise.
12964b889b3SMichal Simek  *
13064b889b3SMichal Simek  * Called early during boot from platform code to remap SLCR area.
13164b889b3SMichal Simek  */
13264b889b3SMichal Simek int __init zynq_slcr_init(void)
13364b889b3SMichal Simek {
134016f4dcaSMichal Simek 	zynq_slcr_regmap = syscon_regmap_lookup_by_compatible("xlnx,zynq-slcr");
135016f4dcaSMichal Simek 	if (IS_ERR(zynq_slcr_regmap)) {
136016f4dcaSMichal Simek 		pr_err("%s: failed to find zynq-slcr\n", __func__);
137016f4dcaSMichal Simek 		return -ENODEV;
138016f4dcaSMichal Simek 	}
139016f4dcaSMichal Simek 
140016f4dcaSMichal Simek 	return 0;
141016f4dcaSMichal Simek }
142016f4dcaSMichal Simek 
143016f4dcaSMichal Simek /**
144016f4dcaSMichal Simek  * zynq_early_slcr_init - Early slcr init function
145016f4dcaSMichal Simek  *
146016f4dcaSMichal Simek  * Return:	0 on success, negative errno otherwise.
147016f4dcaSMichal Simek  *
148016f4dcaSMichal Simek  * Called very early during boot from platform code to unlock SLCR.
149016f4dcaSMichal Simek  */
150016f4dcaSMichal Simek int __init zynq_early_slcr_init(void)
151016f4dcaSMichal Simek {
15264b889b3SMichal Simek 	struct device_node *np;
15364b889b3SMichal Simek 
15464b889b3SMichal Simek 	np = of_find_compatible_node(NULL, NULL, "xlnx,zynq-slcr");
15564b889b3SMichal Simek 	if (!np) {
15664b889b3SMichal Simek 		pr_err("%s: no slcr node found\n", __func__);
15764b889b3SMichal Simek 		BUG();
15864b889b3SMichal Simek 	}
15964b889b3SMichal Simek 
16064b889b3SMichal Simek 	zynq_slcr_base = of_iomap(np, 0);
16164b889b3SMichal Simek 	if (!zynq_slcr_base) {
16264b889b3SMichal Simek 		pr_err("%s: Unable to map I/O memory\n", __func__);
16364b889b3SMichal Simek 		BUG();
16464b889b3SMichal Simek 	}
16564b889b3SMichal Simek 
1665e218280SSteffen Trumtrar 	np->data = (__force void *)zynq_slcr_base;
1675e218280SSteffen Trumtrar 
16864b889b3SMichal Simek 	/* unlock the SLCR so that registers can be changed */
169b5f177ffSSoren Brinkmann 	writel(SLCR_UNLOCK_MAGIC, zynq_slcr_base + SLCR_UNLOCK_OFFSET);
17064b889b3SMichal Simek 
17164b889b3SMichal Simek 	pr_info("%s mapped to %p\n", np->name, zynq_slcr_base);
17264b889b3SMichal Simek 
17364b889b3SMichal Simek 	of_node_put(np);
17464b889b3SMichal Simek 
17564b889b3SMichal Simek 	return 0;
17664b889b3SMichal Simek }
177