Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Clock drivers for Qualcomm APQ8016, APQ8096 |
| 4 | * |
| 5 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
| 6 | * |
| 7 | * Based on Little Kernel driver, simplified |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <clk-uclass.h> |
| 12 | #include <dm.h> |
| 13 | #include <errno.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <linux/bitops.h> |
| 16 | #include "clock-snapdragon.h" |
| 17 | |
| 18 | /* CBCR register fields */ |
| 19 | #define CBCR_BRANCH_ENABLE_BIT BIT(0) |
| 20 | #define CBCR_BRANCH_OFF_BIT BIT(31) |
| 21 | |
| 22 | extern ulong msm_set_rate(struct clk *clk, ulong rate); |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 23 | extern int msm_enable(struct clk *clk); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 24 | |
| 25 | /* Enable clock controlled by CBC soft macro */ |
| 26 | void clk_enable_cbc(phys_addr_t cbcr) |
| 27 | { |
| 28 | setbits_le32(cbcr, CBCR_BRANCH_ENABLE_BIT); |
| 29 | |
| 30 | while (readl(cbcr) & CBCR_BRANCH_OFF_BIT) |
| 31 | ; |
| 32 | } |
| 33 | |
Ramon Fried | ae29977 | 2018-05-16 12:13:39 +0300 | [diff] [blame] | 34 | void clk_enable_gpll0(phys_addr_t base, const struct pll_vote_clk *gpll0) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 35 | { |
| 36 | if (readl(base + gpll0->status) & gpll0->status_bit) |
| 37 | return; /* clock already enabled */ |
| 38 | |
| 39 | setbits_le32(base + gpll0->ena_vote, gpll0->vote_bit); |
| 40 | |
| 41 | while ((readl(base + gpll0->status) & gpll0->status_bit) == 0) |
| 42 | ; |
| 43 | } |
| 44 | |
Ramon Fried | ae29977 | 2018-05-16 12:13:39 +0300 | [diff] [blame] | 45 | #define BRANCH_ON_VAL (0) |
| 46 | #define BRANCH_NOC_FSM_ON_VAL BIT(29) |
| 47 | #define BRANCH_CHECK_MASK GENMASK(31, 28) |
| 48 | |
| 49 | void clk_enable_vote_clk(phys_addr_t base, const struct vote_clk *vclk) |
| 50 | { |
| 51 | u32 val; |
| 52 | |
| 53 | setbits_le32(base + vclk->ena_vote, vclk->vote_bit); |
| 54 | do { |
| 55 | val = readl(base + vclk->cbcr_reg); |
| 56 | val &= BRANCH_CHECK_MASK; |
| 57 | } while ((val != BRANCH_ON_VAL) && (val != BRANCH_NOC_FSM_ON_VAL)); |
| 58 | } |
| 59 | |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 60 | #define APPS_CMD_RCGR_UPDATE BIT(0) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 61 | |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 62 | /* Update clock command via CMD_RCGR */ |
| 63 | void clk_bcr_update(phys_addr_t apps_cmd_rcgr) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 64 | { |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 65 | setbits_le32(apps_cmd_rcgr, APPS_CMD_RCGR_UPDATE); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 66 | |
| 67 | /* Wait for frequency to be updated. */ |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 68 | while (readl(apps_cmd_rcgr) & APPS_CMD_RCGR_UPDATE) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 69 | ; |
| 70 | } |
| 71 | |
| 72 | #define CFG_MODE_DUAL_EDGE (0x2 << 12) /* Counter mode */ |
| 73 | |
| 74 | #define CFG_MASK 0x3FFF |
| 75 | |
| 76 | #define CFG_DIVIDER_MASK 0x1F |
| 77 | |
| 78 | /* root set rate for clocks with half integer and MND divider */ |
| 79 | void clk_rcg_set_rate_mnd(phys_addr_t base, const struct bcr_regs *regs, |
| 80 | int div, int m, int n, int source) |
| 81 | { |
| 82 | u32 cfg; |
| 83 | /* M value for MND divider. */ |
| 84 | u32 m_val = m; |
| 85 | /* NOT(N-M) value for MND divider. */ |
| 86 | u32 n_val = ~((n) - (m)) * !!(n); |
| 87 | /* NOT 2D value for MND divider. */ |
| 88 | u32 d_val = ~(n); |
| 89 | |
| 90 | /* Program MND values */ |
| 91 | writel(m_val, base + regs->M); |
| 92 | writel(n_val, base + regs->N); |
| 93 | writel(d_val, base + regs->D); |
| 94 | |
| 95 | /* setup src select and divider */ |
| 96 | cfg = readl(base + regs->cfg_rcgr); |
| 97 | cfg &= ~CFG_MASK; |
| 98 | cfg |= source & CFG_CLK_SRC_MASK; /* Select clock source */ |
| 99 | |
| 100 | /* Set the divider; HW permits fraction dividers (+0.5), but |
| 101 | for simplicity, we will support integers only */ |
| 102 | if (div) |
| 103 | cfg |= (2 * div - 1) & CFG_DIVIDER_MASK; |
| 104 | |
| 105 | if (n_val) |
| 106 | cfg |= CFG_MODE_DUAL_EDGE; |
| 107 | |
| 108 | writel(cfg, base + regs->cfg_rcgr); /* Write new clock configuration */ |
| 109 | |
| 110 | /* Inform h/w to start using the new config. */ |
| 111 | clk_bcr_update(base + regs->cmd_rcgr); |
| 112 | } |
| 113 | |
| 114 | static int msm_clk_probe(struct udevice *dev) |
| 115 | { |
| 116 | struct msm_clk_priv *priv = dev_get_priv(dev); |
| 117 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 118 | priv->base = dev_read_addr(dev); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 119 | if (priv->base == FDT_ADDR_T_NONE) |
| 120 | return -EINVAL; |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static ulong msm_clk_set_rate(struct clk *clk, ulong rate) |
| 126 | { |
| 127 | return msm_set_rate(clk, rate); |
| 128 | } |
| 129 | |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 130 | static int msm_clk_enable(struct clk *clk) |
| 131 | { |
| 132 | return msm_enable(clk); |
| 133 | } |
| 134 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 135 | static struct clk_ops msm_clk_ops = { |
| 136 | .set_rate = msm_clk_set_rate, |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 137 | .enable = msm_clk_enable, |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static const struct udevice_id msm_clk_ids[] = { |
| 141 | { .compatible = "qcom,gcc-msm8916" }, |
| 142 | { .compatible = "qcom,gcc-apq8016" }, |
| 143 | { .compatible = "qcom,gcc-msm8996" }, |
| 144 | { .compatible = "qcom,gcc-apq8096" }, |
Dzmitry Sankouski | 038f2b9 | 2021-10-17 13:44:30 +0300 | [diff] [blame] | 145 | { .compatible = "qcom,gcc-sdm845" }, |
Sumit Garg | e6a488b | 2022-07-12 12:42:11 +0530 | [diff] [blame] | 146 | { .compatible = "qcom,gcc-qcs404" }, |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 147 | { } |
| 148 | }; |
| 149 | |
| 150 | U_BOOT_DRIVER(clk_msm) = { |
| 151 | .name = "clk_msm", |
| 152 | .id = UCLASS_CLK, |
| 153 | .of_match = msm_clk_ids, |
| 154 | .ops = &msm_clk_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 155 | .priv_auto = sizeof(struct msm_clk_priv), |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 156 | .probe = msm_clk_probe, |
| 157 | }; |