zy7_slcr.c revision 249997
1/*-
2 * Copyright (C) 2013, Thomas Skibo.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 *   notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 *   notice, this list of conditions and the following disclaimer in the
11 *   documentation and/or other materials provided with the distribution.
12 * * The names of contributors may not be used to endorse or promote products
13 *   derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHORS OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25 * DAMAGE.
26 *
27 */
28
29/* Zynq-700 SLCR driver.  Provides hooks for cpu_reset and PL control stuff.
30 * In the future, maybe MIO control, clock control, etc. could go here.
31 *
32 * Reference: Zynq-7000 All Programmable SoC Technical Reference Manual.
33 * (v1.4) November 16, 2012.  Xilinx doc UG585.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/arm/xilinx/zy7_slcr.c 249997 2013-04-27 22:38:29Z wkoszek $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/conf.h>
42#include <sys/kernel.h>
43#include <sys/module.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/resource.h>
47#include <sys/sysctl.h>
48#include <sys/rman.h>
49
50#include <machine/bus.h>
51#include <machine/resource.h>
52#include <machine/stdarg.h>
53
54#include <dev/fdt/fdt_common.h>
55#include <dev/ofw/ofw_bus.h>
56#include <dev/ofw/ofw_bus_subr.h>
57
58#include <arm/xilinx/zy7_slcr.h>
59
60struct zy7_slcr_softc {
61	device_t	dev;
62	struct mtx	sc_mtx;
63	struct resource	*mem_res;
64};
65
66static struct zy7_slcr_softc *zy7_slcr_softc_p;
67extern void (*zynq7_cpu_reset);
68
69#define ZSLCR_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
70#define	ZSLCR_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
71#define ZSLCR_LOCK_INIT(sc) \
72	mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev),	\
73	    "zy7_slcr", MTX_SPIN)
74#define ZSLCR_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
75
76#define RD4(sc, off) 		(bus_read_4((sc)->mem_res, (off)))
77#define WR4(sc, off, val) 	(bus_write_4((sc)->mem_res, (off), (val)))
78
79
80SYSCTL_NODE(_hw, OID_AUTO, zynq, CTLFLAG_RD, 0, "Xilinx Zynq-7000");
81
82static char zynq_bootmode[64];
83SYSCTL_STRING(_hw_zynq, OID_AUTO, bootmode, CTLFLAG_RD, zynq_bootmode, 0,
84	      "Zynq boot mode");
85
86static char zynq_pssid[80];
87SYSCTL_STRING(_hw_zynq, OID_AUTO, pssid, CTLFLAG_RD, zynq_pssid, 0,
88	   "Zynq PSS IDCODE");
89
90static uint32_t zynq_reboot_status;
91SYSCTL_INT(_hw_zynq, OID_AUTO, reboot_status, CTLFLAG_RD, &zynq_reboot_status,
92	   0, "Zynq REBOOT_STATUS register");
93
94static void
95zy7_slcr_unlock(struct zy7_slcr_softc *sc)
96{
97
98	/* Unlock SLCR with magic number. */
99	WR4(sc, ZY7_SLCR_UNLOCK, ZY7_SLCR_UNLOCK_MAGIC);
100}
101
102static void
103zy7_slcr_lock(struct zy7_slcr_softc *sc)
104{
105
106	/* Lock SLCR with magic number. */
107	WR4(sc, ZY7_SLCR_LOCK, ZY7_SLCR_LOCK_MAGIC);
108}
109
110
111static void
112zy7_slcr_cpu_reset(void)
113{
114	struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
115
116	/* Unlock SLCR registers. */
117	zy7_slcr_unlock(sc);
118
119	/* This has something to do with a work-around so the fsbl will load
120	 * the bitstream after soft-reboot.  It's very important.
121	 */
122	WR4(sc, ZY7_SLCR_REBOOT_STAT,
123	    RD4(sc, ZY7_SLCR_REBOOT_STAT) & 0xf0ffffff);
124
125	/* Soft reset */
126	WR4(sc, ZY7_SLCR_PSS_RST_CTRL, ZY7_SLCR_PSS_RST_CTRL_SOFT_RESET);
127
128	for (;;)
129		;
130}
131
132/* Assert PL resets and disable level shifters in preparation of programming
133 * the PL (FPGA) section.  Called from zy7_devcfg.c.
134 */
135void
136zy7_slcr_preload_pl(void)
137{
138	struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
139
140	if (!sc)
141		return;
142
143	ZSLCR_LOCK(sc);
144
145	/* Unlock SLCR registers. */
146	zy7_slcr_unlock(sc);
147
148	/* Assert top level output resets. */
149	WR4(sc, ZY7_SLCR_FPGA_RST_CTRL, ZY7_SLCR_FPGA_RST_CTRL_RST_ALL);
150
151	/* Disable all level shifters. */
152	WR4(sc, ZY7_SLCR_LVL_SHFTR_EN, 0);
153
154	/* Lock SLCR registers. */
155	zy7_slcr_lock(sc);
156
157	ZSLCR_UNLOCK(sc);
158}
159
160/* After PL configuration, enable level shifters and deassert top-level
161 * PL resets.  Called from zy7_devcfg.c.  Optionally, the level shifters
162 * can be left disabled but that's rare of an FPGA application. That option
163 * is controled by a sysctl in the devcfg driver.
164 */
165void
166zy7_slcr_postload_pl(int en_level_shifters)
167{
168	struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
169
170	if (!sc)
171		return;
172
173	ZSLCR_LOCK(sc);
174
175	/* Unlock SLCR registers. */
176	zy7_slcr_unlock(sc);
177
178	if (en_level_shifters)
179		/* Enable level shifters. */
180		WR4(sc, ZY7_SLCR_LVL_SHFTR_EN, ZY7_SLCR_LVL_SHFTR_EN_ALL);
181
182	/* Deassert top level output resets. */
183	WR4(sc, ZY7_SLCR_FPGA_RST_CTRL, 0);
184
185	/* Lock SLCR registers. */
186	zy7_slcr_lock(sc);
187
188	ZSLCR_UNLOCK(sc);
189}
190
191static int
192zy7_slcr_probe(device_t dev)
193{
194	if (!ofw_bus_is_compatible(dev, "xlnx,zy7_slcr"))
195		return (ENXIO);
196
197	device_set_desc(dev, "Zynq-7000 slcr block");
198	return (0);
199}
200
201static int
202zy7_slcr_attach(device_t dev)
203{
204	struct zy7_slcr_softc *sc = device_get_softc(dev);
205	int rid;
206	uint32_t bootmode;
207	uint32_t pss_idcode;
208	static char *bootdev_names[] = {
209		"JTAG", "Quad-SPI", "NOR", "(3?)",
210		"NAND", "SD Card", "(6?)", "(7?)"
211	};
212
213	/* Allow only one attach. */
214	if (zy7_slcr_softc_p != NULL)
215		return (ENXIO);
216
217	sc->dev = dev;
218
219	ZSLCR_LOCK_INIT(sc);
220
221	/* Get memory resource. */
222	rid = 0;
223	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
224					     RF_ACTIVE);
225	if (sc->mem_res == NULL) {
226		device_printf(dev, "could not allocate memory resources.\n");
227		return (ENOMEM);
228	}
229
230	/* Hook up cpu_reset. */
231	zy7_slcr_softc_p = sc;
232	zynq7_cpu_reset = zy7_slcr_cpu_reset;
233
234	/* Read info and set sysctls. */
235	bootmode = RD4(sc, ZY7_SLCR_BOOT_MODE);
236	snprintf(zynq_bootmode, sizeof(zynq_bootmode),
237		 "0x%x: boot device: %s", bootmode,
238		 bootdev_names[bootmode & ZY7_SLCR_BOOT_MODE_BOOTDEV_MASK]);
239
240	pss_idcode = RD4(sc, ZY7_SLCR_PSS_IDCODE);
241	snprintf(zynq_pssid, sizeof(zynq_pssid),
242		 "0x%x: manufacturer: 0x%x device: 0x%x "
243		 "family: 0x%x sub-family: 0x%x rev: 0x%x",
244		 pss_idcode,
245		 (pss_idcode & ZY7_SLCR_PSS_IDCODE_MNFR_ID_MASK) >>
246		 ZY7_SLCR_PSS_IDCODE_MNFR_ID_SHIFT,
247		 (pss_idcode & ZY7_SLCR_PSS_IDCODE_DEVICE_MASK) >>
248		 ZY7_SLCR_PSS_IDCODE_DEVICE_SHIFT,
249		 (pss_idcode & ZY7_SLCR_PSS_IDCODE_FAMILY_MASK) >>
250		 ZY7_SLCR_PSS_IDCODE_FAMILY_SHIFT,
251		 (pss_idcode & ZY7_SLCR_PSS_IDCODE_SUB_FAMILY_MASK) >>
252		 ZY7_SLCR_PSS_IDCODE_SUB_FAMILY_SHIFT,
253		 (pss_idcode & ZY7_SLCR_PSS_IDCODE_REVISION_MASK) >>
254		 ZY7_SLCR_PSS_IDCODE_REVISION_SHIFT);
255
256	zynq_reboot_status = RD4(sc, ZY7_SLCR_REBOOT_STAT);
257
258	/* Lock SLCR registers. */
259	zy7_slcr_lock(sc);
260
261	return (0);
262}
263
264static int
265zy7_slcr_detach(device_t dev)
266{
267	struct zy7_slcr_softc *sc = device_get_softc(dev);
268
269	bus_generic_detach(dev);
270
271	/* Release memory resource. */
272	if (sc->mem_res != NULL)
273		bus_release_resource(dev, SYS_RES_MEMORY,
274			     rman_get_rid(sc->mem_res), sc->mem_res);
275
276	zy7_slcr_softc_p = NULL;
277	zynq7_cpu_reset = NULL;
278
279	ZSLCR_LOCK_DESTROY(sc);
280
281	return (0);
282}
283
284static device_method_t zy7_slcr_methods[] = {
285	/* device_if */
286	DEVMETHOD(device_probe, 	zy7_slcr_probe),
287	DEVMETHOD(device_attach, 	zy7_slcr_attach),
288	DEVMETHOD(device_detach, 	zy7_slcr_detach),
289
290	DEVMETHOD_END
291};
292
293static driver_t zy7_slcr_driver = {
294	"zy7_slcr",
295	zy7_slcr_methods,
296	sizeof(struct zy7_slcr_softc),
297};
298static devclass_t zy7_slcr_devclass;
299
300DRIVER_MODULE(zy7_slcr, simplebus, zy7_slcr_driver, zy7_slcr_devclass, 0, 0);
301MODULE_VERSION(zy7_slcr, 1);
302