2 * Copyright (c) 2013 Thomas Skibo
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Zynq-700 SLCR driver. Provides hooks for cpu_reset and PL control stuff.
31 * In the future, maybe MIO control, clock control, etc. could go here.
33 * Reference: Zynq-7000 All Programmable SoC Technical Reference Manual.
34 * (v1.4) November 16, 2012. Xilinx doc UG585.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/resource.h>
48 #include <sys/sysctl.h>
51 #include <machine/bus.h>
52 #include <machine/resource.h>
53 #include <machine/stdarg.h>
55 #include <dev/fdt/fdt_common.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
59 #include <arm/xilinx/zy7_slcr.h>
61 struct zy7_slcr_softc {
64 struct resource *mem_res;
67 static struct zy7_slcr_softc *zy7_slcr_softc_p;
68 extern void (*zynq7_cpu_reset);
70 #define ZSLCR_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
71 #define ZSLCR_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
72 #define ZSLCR_LOCK_INIT(sc) \
73 mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev), \
75 #define ZSLCR_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
77 #define RD4(sc, off) (bus_read_4((sc)->mem_res, (off)))
78 #define WR4(sc, off, val) (bus_write_4((sc)->mem_res, (off), (val)))
80 #define ZYNQ_DEFAULT_PS_CLK_FREQUENCY 33333333 /* 33.3 Mhz */
83 SYSCTL_NODE(_hw, OID_AUTO, zynq, CTLFLAG_RD, 0, "Xilinx Zynq-7000");
85 static char zynq_bootmode[64];
86 SYSCTL_STRING(_hw_zynq, OID_AUTO, bootmode, CTLFLAG_RD, zynq_bootmode, 0,
89 static char zynq_pssid[100];
90 SYSCTL_STRING(_hw_zynq, OID_AUTO, pssid, CTLFLAG_RD, zynq_pssid, 0,
93 static uint32_t zynq_reboot_status;
94 SYSCTL_INT(_hw_zynq, OID_AUTO, reboot_status, CTLFLAG_RD, &zynq_reboot_status,
95 0, "Zynq REBOOT_STATUS register");
97 static int ps_clk_frequency;
98 SYSCTL_INT(_hw_zynq, OID_AUTO, ps_clk_frequency, CTLFLAG_RD, &ps_clk_frequency,
99 0, "Zynq PS_CLK Frequency");
101 static int io_pll_frequency;
102 SYSCTL_INT(_hw_zynq, OID_AUTO, io_pll_frequency, CTLFLAG_RD, &io_pll_frequency,
103 0, "Zynq IO PLL Frequency");
105 static int arm_pll_frequency;
106 SYSCTL_INT(_hw_zynq, OID_AUTO, arm_pll_frequency, CTLFLAG_RD,
107 &arm_pll_frequency, 0, "Zynq ARM PLL Frequency");
109 static int ddr_pll_frequency;
110 SYSCTL_INT(_hw_zynq, OID_AUTO, ddr_pll_frequency, CTLFLAG_RD,
111 &ddr_pll_frequency, 0, "Zynq DDR PLL Frequency");
114 zy7_slcr_unlock(struct zy7_slcr_softc *sc)
117 /* Unlock SLCR with magic number. */
118 WR4(sc, ZY7_SLCR_UNLOCK, ZY7_SLCR_UNLOCK_MAGIC);
122 zy7_slcr_lock(struct zy7_slcr_softc *sc)
125 /* Lock SLCR with magic number. */
126 WR4(sc, ZY7_SLCR_LOCK, ZY7_SLCR_LOCK_MAGIC);
131 zy7_slcr_cpu_reset(void)
133 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
135 /* Unlock SLCR registers. */
138 /* This has something to do with a work-around so the fsbl will load
139 * the bitstream after soft-reboot. It's very important.
141 WR4(sc, ZY7_SLCR_REBOOT_STAT,
142 RD4(sc, ZY7_SLCR_REBOOT_STAT) & 0xf0ffffff);
145 WR4(sc, ZY7_SLCR_PSS_RST_CTRL, ZY7_SLCR_PSS_RST_CTRL_SOFT_RESET);
151 /* Assert PL resets and disable level shifters in preparation of programming
152 * the PL (FPGA) section. Called from zy7_devcfg.c.
155 zy7_slcr_preload_pl(void)
157 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
164 /* Unlock SLCR registers. */
167 /* Assert top level output resets. */
168 WR4(sc, ZY7_SLCR_FPGA_RST_CTRL, ZY7_SLCR_FPGA_RST_CTRL_RST_ALL);
170 /* Disable all level shifters. */
171 WR4(sc, ZY7_SLCR_LVL_SHFTR_EN, 0);
173 /* Lock SLCR registers. */
179 /* After PL configuration, enable level shifters and deassert top-level
180 * PL resets. Called from zy7_devcfg.c. Optionally, the level shifters
181 * can be left disabled but that's rare of an FPGA application. That option
182 * is controled by a sysctl in the devcfg driver.
185 zy7_slcr_postload_pl(int en_level_shifters)
187 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
194 /* Unlock SLCR registers. */
197 if (en_level_shifters)
198 /* Enable level shifters. */
199 WR4(sc, ZY7_SLCR_LVL_SHFTR_EN, ZY7_SLCR_LVL_SHFTR_EN_ALL);
201 /* Deassert top level output resets. */
202 WR4(sc, ZY7_SLCR_FPGA_RST_CTRL, 0);
204 /* Lock SLCR registers. */
210 /* Override cgem_set_refclk() in gigabit ethernet driver
211 * (sys/dev/cadence/if_cgem.c). This function is called to
212 * request a change in the gem's reference clock speed.
215 cgem_set_ref_clk(int unit, int frequency)
217 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
223 /* Find suitable divisor pairs. Round result to nearest khz
226 for (div1 = 1; div1 <= ZY7_SLCR_GEM_CLK_CTRL_DIVISOR1_MAX; div1++) {
227 div0 = (io_pll_frequency + div1 * frequency / 2) /
229 if (div0 > 0 && div0 <= ZY7_SLCR_GEM_CLK_CTRL_DIVISOR_MAX &&
230 ((io_pll_frequency / div0 / div1) + 500) / 1000 ==
231 (frequency + 500) / 1000)
235 if (div1 > ZY7_SLCR_GEM_CLK_CTRL_DIVISOR1_MAX)
240 /* Unlock SLCR registers. */
243 /* Modify GEM reference clock. */
244 WR4(sc, unit ? ZY7_SLCR_GEM1_CLK_CTRL : ZY7_SLCR_GEM0_CLK_CTRL,
245 (div1 << ZY7_SLCR_GEM_CLK_CTRL_DIVISOR1_SHIFT) |
246 (div0 << ZY7_SLCR_GEM_CLK_CTRL_DIVISOR_SHIFT) |
247 ZY7_SLCR_GEM_CLK_CTRL_SRCSEL_IO_PLL |
248 ZY7_SLCR_GEM_CLK_CTRL_CLKACT);
250 /* Lock SLCR registers. */
259 zy7_slcr_probe(device_t dev)
262 if (!ofw_bus_status_okay(dev))
265 if (!ofw_bus_is_compatible(dev, "xlnx,zy7_slcr"))
268 device_set_desc(dev, "Zynq-7000 slcr block");
273 zy7_slcr_attach(device_t dev)
275 struct zy7_slcr_softc *sc = device_get_softc(dev);
281 uint32_t arm_pll_ctrl;
282 uint32_t ddr_pll_ctrl;
283 uint32_t io_pll_ctrl;
284 static char *bootdev_names[] = {
285 "JTAG", "Quad-SPI", "NOR", "(3?)",
286 "NAND", "SD Card", "(6?)", "(7?)"
289 /* Allow only one attach. */
290 if (zy7_slcr_softc_p != NULL)
297 /* Get memory resource. */
299 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
301 if (sc->mem_res == NULL) {
302 device_printf(dev, "could not allocate memory resources.\n");
306 /* Hook up cpu_reset. */
307 zy7_slcr_softc_p = sc;
308 zynq7_cpu_reset = zy7_slcr_cpu_reset;
310 /* Read info and set sysctls. */
311 bootmode = RD4(sc, ZY7_SLCR_BOOT_MODE);
312 snprintf(zynq_bootmode, sizeof(zynq_bootmode),
313 "0x%x: boot device: %s", bootmode,
314 bootdev_names[bootmode & ZY7_SLCR_BOOT_MODE_BOOTDEV_MASK]);
316 pss_idcode = RD4(sc, ZY7_SLCR_PSS_IDCODE);
317 snprintf(zynq_pssid, sizeof(zynq_pssid),
318 "0x%x: manufacturer: 0x%x device: 0x%x "
319 "family: 0x%x sub-family: 0x%x rev: 0x%x",
321 (pss_idcode & ZY7_SLCR_PSS_IDCODE_MNFR_ID_MASK) >>
322 ZY7_SLCR_PSS_IDCODE_MNFR_ID_SHIFT,
323 (pss_idcode & ZY7_SLCR_PSS_IDCODE_DEVICE_MASK) >>
324 ZY7_SLCR_PSS_IDCODE_DEVICE_SHIFT,
325 (pss_idcode & ZY7_SLCR_PSS_IDCODE_FAMILY_MASK) >>
326 ZY7_SLCR_PSS_IDCODE_FAMILY_SHIFT,
327 (pss_idcode & ZY7_SLCR_PSS_IDCODE_SUB_FAMILY_MASK) >>
328 ZY7_SLCR_PSS_IDCODE_SUB_FAMILY_SHIFT,
329 (pss_idcode & ZY7_SLCR_PSS_IDCODE_REVISION_MASK) >>
330 ZY7_SLCR_PSS_IDCODE_REVISION_SHIFT);
332 zynq_reboot_status = RD4(sc, ZY7_SLCR_REBOOT_STAT);
334 /* Derive PLL frequencies from PS_CLK. */
335 node = ofw_bus_get_node(dev);
336 if (OF_getprop(node, "clock-frequency", &cell, sizeof(cell)) > 0)
337 ps_clk_frequency = fdt32_to_cpu(cell);
339 ps_clk_frequency = ZYNQ_DEFAULT_PS_CLK_FREQUENCY;
341 arm_pll_ctrl = RD4(sc, ZY7_SLCR_ARM_PLL_CTRL);
342 ddr_pll_ctrl = RD4(sc, ZY7_SLCR_DDR_PLL_CTRL);
343 io_pll_ctrl = RD4(sc, ZY7_SLCR_IO_PLL_CTRL);
345 /* Determine ARM PLL frequency. */
346 if (((arm_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) == 0 &&
347 (arm_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_FORCE) != 0) ||
348 ((arm_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) != 0 &&
349 (bootmode & ZY7_SLCR_BOOT_MODE_PLL_BYPASS) != 0))
350 /* PLL is bypassed. */
351 arm_pll_frequency = ps_clk_frequency;
353 arm_pll_frequency = ps_clk_frequency *
354 ((arm_pll_ctrl & ZY7_SLCR_PLL_CTRL_FDIV_MASK) >>
355 ZY7_SLCR_PLL_CTRL_FDIV_SHIFT);
357 /* Determine DDR PLL frequency. */
358 if (((ddr_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) == 0 &&
359 (ddr_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_FORCE) != 0) ||
360 ((ddr_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) != 0 &&
361 (bootmode & ZY7_SLCR_BOOT_MODE_PLL_BYPASS) != 0))
362 /* PLL is bypassed. */
363 ddr_pll_frequency = ps_clk_frequency;
365 ddr_pll_frequency = ps_clk_frequency *
366 ((ddr_pll_ctrl & ZY7_SLCR_PLL_CTRL_FDIV_MASK) >>
367 ZY7_SLCR_PLL_CTRL_FDIV_SHIFT);
369 /* Determine IO PLL frequency. */
370 if (((io_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) == 0 &&
371 (io_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_FORCE) != 0) ||
372 ((io_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) != 0 &&
373 (bootmode & ZY7_SLCR_BOOT_MODE_PLL_BYPASS) != 0))
374 /* PLL is bypassed. */
375 io_pll_frequency = ps_clk_frequency;
377 io_pll_frequency = ps_clk_frequency *
378 ((io_pll_ctrl & ZY7_SLCR_PLL_CTRL_FDIV_MASK) >>
379 ZY7_SLCR_PLL_CTRL_FDIV_SHIFT);
381 /* Lock SLCR registers. */
388 zy7_slcr_detach(device_t dev)
390 struct zy7_slcr_softc *sc = device_get_softc(dev);
392 bus_generic_detach(dev);
394 /* Release memory resource. */
395 if (sc->mem_res != NULL)
396 bus_release_resource(dev, SYS_RES_MEMORY,
397 rman_get_rid(sc->mem_res), sc->mem_res);
399 zy7_slcr_softc_p = NULL;
400 zynq7_cpu_reset = NULL;
402 ZSLCR_LOCK_DESTROY(sc);
407 static device_method_t zy7_slcr_methods[] = {
409 DEVMETHOD(device_probe, zy7_slcr_probe),
410 DEVMETHOD(device_attach, zy7_slcr_attach),
411 DEVMETHOD(device_detach, zy7_slcr_detach),
416 static driver_t zy7_slcr_driver = {
419 sizeof(struct zy7_slcr_softc),
421 static devclass_t zy7_slcr_devclass;
423 DRIVER_MODULE(zy7_slcr, simplebus, zy7_slcr_driver, zy7_slcr_devclass, 0, 0);
424 MODULE_VERSION(zy7_slcr, 1);