2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Thomas Skibo
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Zynq-700 SLCR driver. Provides hooks for cpu_reset and PL control stuff.
33 * In the future, maybe MIO control, clock control, etc. could go here.
35 * Reference: Zynq-7000 All Programmable SoC Technical Reference Manual.
36 * (v1.4) November 16, 2012. Xilinx doc UG585.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/resource.h>
50 #include <sys/sysctl.h>
53 #include <machine/bus.h>
54 #include <machine/resource.h>
55 #include <machine/stdarg.h>
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/ofw/ofw_bus_subr.h>
60 #include <arm/xilinx/zy7_slcr.h>
62 struct zy7_slcr_softc {
65 struct resource *mem_res;
68 static struct zy7_slcr_softc *zy7_slcr_softc_p;
69 extern void (*zynq7_cpu_reset);
71 #define ZSLCR_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
72 #define ZSLCR_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
73 #define ZSLCR_LOCK_INIT(sc) \
74 mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev), \
76 #define ZSLCR_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
78 #define RD4(sc, off) (bus_read_4((sc)->mem_res, (off)))
79 #define WR4(sc, off, val) (bus_write_4((sc)->mem_res, (off), (val)))
81 #define ZYNQ_DEFAULT_PS_CLK_FREQUENCY 33333333 /* 33.3 Mhz */
83 SYSCTL_NODE(_hw, OID_AUTO, zynq, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
86 static char zynq_bootmode[64];
87 SYSCTL_STRING(_hw_zynq, OID_AUTO, bootmode, CTLFLAG_RD, zynq_bootmode, 0,
90 static char zynq_pssid[100];
91 SYSCTL_STRING(_hw_zynq, OID_AUTO, pssid, CTLFLAG_RD, zynq_pssid, 0,
94 static uint32_t zynq_reboot_status;
95 SYSCTL_INT(_hw_zynq, OID_AUTO, reboot_status, CTLFLAG_RD, &zynq_reboot_status,
96 0, "Zynq REBOOT_STATUS register");
98 static int ps_clk_frequency;
99 SYSCTL_INT(_hw_zynq, OID_AUTO, ps_clk_frequency, CTLFLAG_RD, &ps_clk_frequency,
100 0, "Zynq PS_CLK Frequency");
102 static int io_pll_frequency;
103 SYSCTL_INT(_hw_zynq, OID_AUTO, io_pll_frequency, CTLFLAG_RD, &io_pll_frequency,
104 0, "Zynq IO PLL Frequency");
106 static int arm_pll_frequency;
107 SYSCTL_INT(_hw_zynq, OID_AUTO, arm_pll_frequency, CTLFLAG_RD,
108 &arm_pll_frequency, 0, "Zynq ARM PLL Frequency");
110 static int ddr_pll_frequency;
111 SYSCTL_INT(_hw_zynq, OID_AUTO, ddr_pll_frequency, CTLFLAG_RD,
112 &ddr_pll_frequency, 0, "Zynq DDR PLL Frequency");
115 zy7_slcr_unlock(struct zy7_slcr_softc *sc)
118 /* Unlock SLCR with magic number. */
119 WR4(sc, ZY7_SLCR_UNLOCK, ZY7_SLCR_UNLOCK_MAGIC);
123 zy7_slcr_lock(struct zy7_slcr_softc *sc)
126 /* Lock SLCR with magic number. */
127 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 controlled 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 * PL clocks management function
262 zy7_pl_fclk_set_source(int unit, int source)
264 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
272 /* Unlock SLCR registers. */
275 /* Modify FPGAx source. */
276 reg = RD4(sc, ZY7_SLCR_FPGA_CLK_CTRL(unit));
277 reg &= ~(ZY7_SLCR_FPGA_CLK_CTRL_SRCSEL_MASK);
278 reg |= (source << ZY7_SLCR_FPGA_CLK_CTRL_SRCSEL_SHIFT);
279 WR4(sc, ZY7_SLCR_FPGA_CLK_CTRL(unit), reg);
281 /* Lock SLCR registers. */
290 zy7_pl_fclk_get_source(int unit)
292 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
301 /* Modify GEM reference clock. */
302 reg = RD4(sc, ZY7_SLCR_FPGA_CLK_CTRL(unit));
303 source = (reg & ZY7_SLCR_FPGA_CLK_CTRL_SRCSEL_MASK) >>
304 ZY7_SLCR_FPGA_CLK_CTRL_SRCSEL_SHIFT;
306 /* ZY7_PL_FCLK_SRC_IO is actually b0x */
307 if ((source & 2) == 0)
308 source = ZY7_PL_FCLK_SRC_IO;
316 zy7_pl_fclk_set_freq(int unit, int frequency)
318 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
327 source = zy7_pl_fclk_get_source(unit);
329 case ZY7_PL_FCLK_SRC_IO:
330 base_frequency = io_pll_frequency;
333 case ZY7_PL_FCLK_SRC_ARM:
334 base_frequency = arm_pll_frequency;
337 case ZY7_PL_FCLK_SRC_DDR:
338 base_frequency = ddr_pll_frequency;
345 /* Find suitable divisor pairs. Round result to nearest khz
348 for (div1 = 1; div1 <= ZY7_SLCR_FPGA_CLK_CTRL_DIVISOR_MAX; div1++) {
349 div0 = (base_frequency + div1 * frequency / 2) /
351 if (div0 > 0 && div0 <= ZY7_SLCR_FPGA_CLK_CTRL_DIVISOR_MAX &&
352 ((base_frequency / div0 / div1) + 500) / 1000 ==
353 (frequency + 500) / 1000)
357 if (div1 > ZY7_SLCR_FPGA_CLK_CTRL_DIVISOR_MAX)
362 /* Unlock SLCR registers. */
365 /* Modify FPGAx reference clock. */
366 reg = RD4(sc, ZY7_SLCR_FPGA_CLK_CTRL(unit));
367 reg &= ~(ZY7_SLCR_FPGA_CLK_CTRL_DIVISOR1_MASK |
368 ZY7_SLCR_FPGA_CLK_CTRL_DIVISOR0_MASK);
369 reg |= (div1 << ZY7_SLCR_FPGA_CLK_CTRL_DIVISOR1_SHIFT) |
370 (div0 << ZY7_SLCR_FPGA_CLK_CTRL_DIVISOR0_SHIFT);
371 WR4(sc, ZY7_SLCR_FPGA_CLK_CTRL(unit), reg);
373 /* Lock SLCR registers. */
378 return (base_frequency / div0 / div1);
382 zy7_pl_fclk_get_freq(int unit)
384 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
394 source = zy7_pl_fclk_get_source(unit);
396 case ZY7_PL_FCLK_SRC_IO:
397 base_frequency = io_pll_frequency;
400 case ZY7_PL_FCLK_SRC_ARM:
401 base_frequency = arm_pll_frequency;
404 case ZY7_PL_FCLK_SRC_DDR:
405 base_frequency = ddr_pll_frequency;
414 /* Modify FPGAx reference clock. */
415 reg = RD4(sc, ZY7_SLCR_FPGA_CLK_CTRL(unit));
416 div1 = (reg & ZY7_SLCR_FPGA_CLK_CTRL_DIVISOR1_MASK) >>
417 ZY7_SLCR_FPGA_CLK_CTRL_DIVISOR1_SHIFT;
418 div0 = (reg & ZY7_SLCR_FPGA_CLK_CTRL_DIVISOR0_MASK) >>
419 ZY7_SLCR_FPGA_CLK_CTRL_DIVISOR0_SHIFT;
429 frequency = (base_frequency / div0 / div1);
431 frequency = (frequency + 500) / 1000;
432 frequency = frequency * 1000;
438 zy7_pl_fclk_enable(int unit)
440 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
447 /* Unlock SLCR registers. */
450 WR4(sc, ZY7_SLCR_FPGA_THR_CTRL(unit), 0);
451 WR4(sc, ZY7_SLCR_FPGA_THR_CNT(unit), 0);
453 /* Lock SLCR registers. */
462 zy7_pl_fclk_disable(int unit)
464 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
471 /* Unlock SLCR registers. */
474 WR4(sc, ZY7_SLCR_FPGA_THR_CTRL(unit), 0);
475 WR4(sc, ZY7_SLCR_FPGA_THR_CNT(unit), 1);
477 /* Lock SLCR registers. */
486 zy7_pl_fclk_enabled(int unit)
488 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
495 reg = RD4(sc, ZY7_SLCR_FPGA_THR_CNT(unit));
502 zy7_pl_level_shifters_enabled(void)
504 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
512 reg = RD4(sc, ZY7_SLCR_LVL_SHFTR_EN);
515 return (reg == ZY7_SLCR_LVL_SHFTR_EN_ALL);
519 zy7_pl_level_shifters_enable(void)
521 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
528 WR4(sc, ZY7_SLCR_LVL_SHFTR_EN, ZY7_SLCR_LVL_SHFTR_EN_ALL);
534 zy7_pl_level_shifters_disable(void)
536 struct zy7_slcr_softc *sc = zy7_slcr_softc_p;
543 WR4(sc, ZY7_SLCR_LVL_SHFTR_EN, 0);
549 zy7_slcr_probe(device_t dev)
552 if (!ofw_bus_status_okay(dev))
555 if (!ofw_bus_is_compatible(dev, "xlnx,zy7_slcr"))
558 device_set_desc(dev, "Zynq-7000 slcr block");
563 zy7_slcr_attach(device_t dev)
565 struct zy7_slcr_softc *sc = device_get_softc(dev);
571 uint32_t arm_pll_ctrl;
572 uint32_t ddr_pll_ctrl;
573 uint32_t io_pll_ctrl;
574 static char *bootdev_names[] = {
575 "JTAG", "Quad-SPI", "NOR", "(3?)",
576 "NAND", "SD Card", "(6?)", "(7?)"
579 /* Allow only one attach. */
580 if (zy7_slcr_softc_p != NULL)
587 /* Get memory resource. */
589 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
591 if (sc->mem_res == NULL) {
592 device_printf(dev, "could not allocate memory resources.\n");
596 /* Hook up cpu_reset. */
597 zy7_slcr_softc_p = sc;
598 zynq7_cpu_reset = zy7_slcr_cpu_reset;
600 /* Read info and set sysctls. */
601 bootmode = RD4(sc, ZY7_SLCR_BOOT_MODE);
602 snprintf(zynq_bootmode, sizeof(zynq_bootmode),
603 "0x%x: boot device: %s", bootmode,
604 bootdev_names[bootmode & ZY7_SLCR_BOOT_MODE_BOOTDEV_MASK]);
606 pss_idcode = RD4(sc, ZY7_SLCR_PSS_IDCODE);
607 snprintf(zynq_pssid, sizeof(zynq_pssid),
608 "0x%x: manufacturer: 0x%x device: 0x%x "
609 "family: 0x%x sub-family: 0x%x rev: 0x%x",
611 (pss_idcode & ZY7_SLCR_PSS_IDCODE_MNFR_ID_MASK) >>
612 ZY7_SLCR_PSS_IDCODE_MNFR_ID_SHIFT,
613 (pss_idcode & ZY7_SLCR_PSS_IDCODE_DEVICE_MASK) >>
614 ZY7_SLCR_PSS_IDCODE_DEVICE_SHIFT,
615 (pss_idcode & ZY7_SLCR_PSS_IDCODE_FAMILY_MASK) >>
616 ZY7_SLCR_PSS_IDCODE_FAMILY_SHIFT,
617 (pss_idcode & ZY7_SLCR_PSS_IDCODE_SUB_FAMILY_MASK) >>
618 ZY7_SLCR_PSS_IDCODE_SUB_FAMILY_SHIFT,
619 (pss_idcode & ZY7_SLCR_PSS_IDCODE_REVISION_MASK) >>
620 ZY7_SLCR_PSS_IDCODE_REVISION_SHIFT);
622 zynq_reboot_status = RD4(sc, ZY7_SLCR_REBOOT_STAT);
624 /* Derive PLL frequencies from PS_CLK. */
625 node = ofw_bus_get_node(dev);
626 if (OF_getencprop(node, "clock-frequency", &cell, sizeof(cell)) > 0)
627 ps_clk_frequency = cell;
629 ps_clk_frequency = ZYNQ_DEFAULT_PS_CLK_FREQUENCY;
631 arm_pll_ctrl = RD4(sc, ZY7_SLCR_ARM_PLL_CTRL);
632 ddr_pll_ctrl = RD4(sc, ZY7_SLCR_DDR_PLL_CTRL);
633 io_pll_ctrl = RD4(sc, ZY7_SLCR_IO_PLL_CTRL);
635 /* Determine ARM PLL frequency. */
636 if (((arm_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) == 0 &&
637 (arm_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_FORCE) != 0) ||
638 ((arm_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) != 0 &&
639 (bootmode & ZY7_SLCR_BOOT_MODE_PLL_BYPASS) != 0))
640 /* PLL is bypassed. */
641 arm_pll_frequency = ps_clk_frequency;
643 arm_pll_frequency = ps_clk_frequency *
644 ((arm_pll_ctrl & ZY7_SLCR_PLL_CTRL_FDIV_MASK) >>
645 ZY7_SLCR_PLL_CTRL_FDIV_SHIFT);
647 /* Determine DDR PLL frequency. */
648 if (((ddr_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) == 0 &&
649 (ddr_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_FORCE) != 0) ||
650 ((ddr_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) != 0 &&
651 (bootmode & ZY7_SLCR_BOOT_MODE_PLL_BYPASS) != 0))
652 /* PLL is bypassed. */
653 ddr_pll_frequency = ps_clk_frequency;
655 ddr_pll_frequency = ps_clk_frequency *
656 ((ddr_pll_ctrl & ZY7_SLCR_PLL_CTRL_FDIV_MASK) >>
657 ZY7_SLCR_PLL_CTRL_FDIV_SHIFT);
659 /* Determine IO PLL frequency. */
660 if (((io_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) == 0 &&
661 (io_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_FORCE) != 0) ||
662 ((io_pll_ctrl & ZY7_SLCR_PLL_CTRL_BYPASS_QUAL) != 0 &&
663 (bootmode & ZY7_SLCR_BOOT_MODE_PLL_BYPASS) != 0))
664 /* PLL is bypassed. */
665 io_pll_frequency = ps_clk_frequency;
667 io_pll_frequency = ps_clk_frequency *
668 ((io_pll_ctrl & ZY7_SLCR_PLL_CTRL_FDIV_MASK) >>
669 ZY7_SLCR_PLL_CTRL_FDIV_SHIFT);
671 /* Lock SLCR registers. */
678 zy7_slcr_detach(device_t dev)
680 struct zy7_slcr_softc *sc = device_get_softc(dev);
682 bus_generic_detach(dev);
684 /* Release memory resource. */
685 if (sc->mem_res != NULL)
686 bus_release_resource(dev, SYS_RES_MEMORY,
687 rman_get_rid(sc->mem_res), sc->mem_res);
689 zy7_slcr_softc_p = NULL;
690 zynq7_cpu_reset = NULL;
692 ZSLCR_LOCK_DESTROY(sc);
697 static device_method_t zy7_slcr_methods[] = {
699 DEVMETHOD(device_probe, zy7_slcr_probe),
700 DEVMETHOD(device_attach, zy7_slcr_attach),
701 DEVMETHOD(device_detach, zy7_slcr_detach),
706 static driver_t zy7_slcr_driver = {
709 sizeof(struct zy7_slcr_softc),
712 DRIVER_MODULE(zy7_slcr, simplebus, zy7_slcr_driver, 0, 0);
713 MODULE_VERSION(zy7_slcr, 1);