2 * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * 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 THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER 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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <dev/bhnd/bhnd.h>
33 #include <dev/bhnd/cores/chipc/chipcreg.h>
35 #include <dev/bhnd/cores/chipc/pwrctl/bhnd_pwrctlvar.h>
37 #include <dev/bhnd/cores/pmu/bhnd_pmureg.h>
38 #include <dev/bhnd/cores/pmu/bhnd_pmuvar.h>
40 #include "bcm_machdep.h"
42 static struct bhnd_pmu_query *bcm_get_pmu(struct bcm_platform *bp);
43 static bool bcm_has_pmu(struct bcm_platform *bp);
45 static uint32_t bcm_pmu_read4(bus_size_t reg, void *ctx);
46 static void bcm_pmu_write4(bus_size_t reg, uint32_t val,
48 static uint32_t bcm_pmu_read_chipst(void *ctx);
50 const struct bhnd_pmu_io bcm_pmu_soc_io = {
52 .wr4 = bcm_pmu_write4,
53 .rd_chipst = bcm_pmu_read_chipst
57 * Supported UART clock sources.
60 BCM_UART_RCLK_PLL_T1 = 0, /**< UART uses PLL m2 (mii/uart/mipsref) with no divisor */
61 BCM_UART_RCLK_ALP = 1, /**< UART uses ALP rclk with no divisor */
62 BCM_UART_RCLK_EXT = 2, /**< UART uses 1.8423 MHz external clock */
63 BCM_UART_RCLK_SI = 3, /**< UART uses backplane clock with divisor of two */
64 BCM_UART_RCLK_FIXED = 4, /**< UART uses fixed 88Mhz backplane clock with a divisor of 48 */
68 * UART clock configuration.
70 struct bcm_uart_clkcfg {
71 bcm_uart_clksrc src; /**< clock source */
72 uint32_t div; /**< clock divisor */
73 uint32_t freq; /**< clock frequency (Hz) */
76 #define BCM_UART_RCLK_PLL_T1_DIV 1
77 #define BCM_UART_RCLK_ALP_DIV 1
78 #define BCM_UART_RCLK_EXT_HZ 1842300 /* 1.8423MHz */
79 #define BCM_UART_RCLK_EXT_DIV 1
80 #define BCM_UART_RCLK_FIXED_HZ 88000000 /* 88MHz */
81 #define BCM_UART_RCLK_FIXED_DIV 48
83 /* Fetch PLL type from ChipCommon capability flags */
84 #define BCM_PMU_PLL_TYPE(_bp) \
85 CHIPC_GET_BITS(_bp->cc_caps, CHIPC_CAP_PLL)
88 * Return the PMU instance, or NULL if no PMU.
90 static struct bhnd_pmu_query *
91 bcm_get_pmu(struct bcm_platform *bp)
99 * Return true if a PMU is available, false otherwise.
102 bcm_has_pmu(struct bcm_platform *bp)
104 return (bp->pmu_addr != 0);
108 * Determine the UART clock source for @p bp and return the
109 * corresponding clock configuration, if any.
111 static struct bcm_uart_clkcfg
112 bcm_get_uart_clkcfg(struct bcm_platform *bp)
114 struct bcm_uart_clkcfg cfg;
115 struct bhnd_core_info *cc_id;
119 /* These tests are ordered by precedence. */
121 /* PLL M2 clock source? */
122 if (!bcm_has_pmu(bp) && BCM_PMU_PLL_TYPE(bp) == CHIPC_PLL_TYPE1) {
125 n = BCM_CHIPC_READ_4(bp, CHIPC_CLKC_N);
126 m = BCM_CHIPC_READ_4(bp, CHIPC_CLKC_M2);
128 cfg = (struct bcm_uart_clkcfg) {
129 BCM_UART_RCLK_PLL_T1,
130 BCM_UART_RCLK_PLL_T1_DIV,
131 bhnd_pwrctl_clock_rate(BCM_PMU_PLL_TYPE(bp), n, m)
137 /* ALP clock source? */
138 if (cc_id->hwrev != 15 && cc_id->hwrev >= 11) {
139 cfg = (struct bcm_uart_clkcfg) {
141 BCM_UART_RCLK_ALP_DIV,
147 /* External clock? */
148 if (CHIPC_HWREV_HAS_CORECTRL(cc_id->hwrev)) {
149 uint32_t corectrl, uclksel;
152 /* Fetch UART clock support flag */
153 uclksel = CHIPC_GET_BITS(bp->cc_caps, CHIPC_CAP_UCLKSEL);
155 /* Is UART using internal clock? */
156 corectrl = BCM_CHIPC_READ_4(bp, CHIPC_CORECTRL);
157 uintclk0 = CHIPC_GET_FLAG(corectrl, CHIPC_UARTCLKO);
159 if (uintclk0 && uclksel == CHIPC_CAP_UCLKSEL_UINTCLK) {
160 cfg = (struct bcm_uart_clkcfg) {
162 BCM_UART_RCLK_EXT_DIV,
169 /* UART uses backplane clock? */
170 if (cc_id->hwrev == 15 || (cc_id->hwrev >= 3 && cc_id->hwrev <= 10)) {
171 cfg = (struct bcm_uart_clkcfg) {
173 BCM_CHIPC_READ_4(bp, CHIPC_CLKDIV) & CHIPC_CLKD_UART,
180 /* UART uses fixed clock? */
181 if (cc_id->hwrev <= 2) {
182 cfg = (struct bcm_uart_clkcfg) {
184 BCM_UART_RCLK_FIXED_DIV,
185 BCM_UART_RCLK_FIXED_HZ
191 /* All cases must be accounted for above */
192 panic("unreachable - no clock config");
196 * Return the UART reference clock frequency (in Hz).
199 bcm_get_uart_rclk(struct bcm_platform *bp)
201 struct bcm_uart_clkcfg cfg;
203 cfg = bcm_get_uart_clkcfg(bp);
204 return (cfg.freq / cfg.div);
207 /** ALP clock frequency (in Hz) */
209 bcm_get_alpfreq(struct bcm_platform *bp) {
210 if (!bcm_has_pmu(bp))
211 return (BHND_PMU_ALP_CLOCK);
213 return (bhnd_pmu_alp_clock(bcm_get_pmu(bp)));
216 /** ILP clock frequency (in Hz) */
218 bcm_get_ilpfreq(struct bcm_platform *bp) {
219 if (!bcm_has_pmu(bp))
220 return (BHND_PMU_ILP_CLOCK);
222 return (bhnd_pmu_ilp_clock(bcm_get_pmu(bp)));
225 /** CPU clock frequency (in Hz) */
227 bcm_get_cpufreq(struct bcm_platform *bp)
236 return (bhnd_pmu_cpu_clock(bcm_get_pmu(bp)));
241 pll_type = CHIPC_GET_BITS(bp->cc_caps, CHIPC_CAP_PLL);
242 mreg = bhnd_pwrctl_cpu_clkreg_m(&bp->cid, pll_type, &fixed_hz);
246 n = BCM_CHIPC_READ_4(bp, CHIPC_CLKC_N);
247 m = BCM_CHIPC_READ_4(bp, mreg);
249 return (bhnd_pwrctl_cpu_clock_rate(&bp->cid, pll_type, n, m));
253 /** Backplane clock frequency (in Hz) */
255 bcm_get_sifreq(struct bcm_platform *bp)
264 return (bhnd_pmu_si_clock(bcm_get_pmu(bp)));
269 pll_type = CHIPC_GET_BITS(bp->cc_caps, CHIPC_CAP_PLL);
270 mreg = bhnd_pwrctl_si_clkreg_m(&bp->cid, pll_type, &fixed_hz);
274 n = BCM_CHIPC_READ_4(bp, CHIPC_CLKC_N);
275 m = BCM_CHIPC_READ_4(bp, mreg);
277 return (bhnd_pwrctl_si_clock_rate(&bp->cid, pll_type, n, m));
282 bcm_pmu_read4(bus_size_t reg, void *ctx) {
283 struct bcm_platform *bp = ctx;
284 return (readl(BCM_SOC_ADDR(bp->pmu_addr, reg)));
288 bcm_pmu_write4(bus_size_t reg, uint32_t val, void *ctx) {
289 struct bcm_platform *bp = ctx;
290 writel(BCM_SOC_ADDR(bp->pmu_addr, reg), val);
294 bcm_pmu_read_chipst(void *ctx)
296 struct bcm_platform *bp = ctx;
297 return (readl(BCM_SOC_ADDR(bp->cc_addr, CHIPC_CHIPST)));