Merge tag 'efi-2019-10-rc1-2' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / drivers / clk / clk_stm32mp1.c
index 24859fd054eec8812726eed3a98472fef97d9a1d..5806d48696f45061b600c5e648f3468953d2d946 100644 (file)
@@ -90,6 +90,7 @@
 #define RCC_PLL4CSGR           0x8A4
 #define RCC_I2C12CKSELR                0x8C0
 #define RCC_I2C35CKSELR                0x8C4
+#define RCC_SPI2S1CKSELR       0x8D8
 #define RCC_UART6CKSELR                0x8E4
 #define RCC_UART24CKSELR       0x8E8
 #define RCC_UART35CKSELR       0x8EC
@@ -298,6 +299,8 @@ enum stm32mp1_parent_sel {
        _STGEN_SEL,
        _DSI_SEL,
        _ADC12_SEL,
+       _SPI1_SEL,
+       _RTC_SEL,
        _PARENT_SEL_NB,
        _UNKNOWN_SEL = 0xff,
 };
@@ -519,6 +522,7 @@ static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
        STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL),
        STM32MP1_CLK_SET_CLR(RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL),
 
+       STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 8, SPI1_K, _SPI1_SEL),
        STM32MP1_CLK_SET_CLR(RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL),
 
        STM32MP1_CLK_SET_CLR_F(RCC_MP_APB3ENSETR, 13, VREF, _PCLK3),
@@ -531,6 +535,7 @@ static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
        STM32MP1_CLK_SET_CLR(RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL),
 
        STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL),
+       STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5),
        STM32MP1_CLK_SET_CLR(RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL),
 
        STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB2ENSETR, 5, ADC12, _HCLK2),
@@ -555,7 +560,7 @@ static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
 
        STM32MP1_CLK_SET_CLR(RCC_MP_AHB5ENSETR, 0, GPIOZ, _UNKNOWN_SEL),
 
-       STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 7, ETHCK, _ETH_SEL),
+       STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 7, ETHCK_K, _ETH_SEL),
        STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 8, ETHTX, _UNKNOWN_SEL),
        STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 9, ETHRX, _UNKNOWN_SEL),
        STM32MP1_CLK_SET_CLR_F(RCC_MP_AHB6ENSETR, 10, ETHMAC, _ACLK),
@@ -566,6 +571,8 @@ static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = {
        STM32MP1_CLK_SET_CLR(RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL),
 
        STM32MP1_CLK(RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL),
+
+       STM32MP1_CLK(RCC_BDCR, 20, RTC, _RTC_SEL),
 };
 
 static const u8 i2c12_parents[] = {_PCLK1, _PLL4_R, _HSI_KER, _CSI_KER};
@@ -589,6 +596,9 @@ static const u8 usbo_parents[] = {_PLL4_R, _USB_PHY_48};
 static const u8 stgen_parents[] = {_HSI_KER, _HSE_KER};
 static const u8 dsi_parents[] = {_DSI_PHY, _PLL4_P};
 static const u8 adc_parents[] = {_PLL4_R, _CK_PER, _PLL3_Q};
+static const u8 spi_parents[] = {_PLL4_P, _PLL3_Q, _I2S_CKIN, _CK_PER,
+                                _PLL3_R};
+static const u8 rtc_parents[] = {_UNKNOWN_ID, _LSE, _LSI, _HSE};
 
 static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
        STM32MP1_CLK_PARENT(_I2C12_SEL, RCC_I2C12CKSELR, 0, 0x7, i2c12_parents),
@@ -613,6 +623,10 @@ static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = {
        STM32MP1_CLK_PARENT(_STGEN_SEL, RCC_STGENCKSELR, 0, 0x3, stgen_parents),
        STM32MP1_CLK_PARENT(_DSI_SEL, RCC_DSICKSELR, 0, 0x1, dsi_parents),
        STM32MP1_CLK_PARENT(_ADC12_SEL, RCC_ADCCKSELR, 0, 0x1, adc_parents),
+       STM32MP1_CLK_PARENT(_SPI1_SEL, RCC_SPI2S1CKSELR, 0, 0x7, spi_parents),
+       STM32MP1_CLK_PARENT(_RTC_SEL, RCC_BDCR, RCC_BDCR_RTCSRC_SHIFT,
+                           (RCC_BDCR_RTCSRC_MASK >> RCC_BDCR_RTCSRC_SHIFT),
+                           rtc_parents),
 };
 
 #ifdef STM32MP1_CLOCK_TREE_INIT
@@ -727,6 +741,8 @@ char * const stm32mp1_clk_parent_sel_name[_PARENT_SEL_NB] = {
        [_STGEN_SEL] = "STGEN",
        [_DSI_SEL] = "DSI",
        [_ADC12_SEL] = "ADC12",
+       [_SPI1_SEL] = "SPI1",
+       [_RTC_SEL] = "RTC",
 };
 
 static const struct stm32mp1_clk_data stm32mp1_data = {
@@ -798,10 +814,11 @@ static int stm32mp1_clk_get_parent(struct stm32mp1_clk_priv *priv,
        const struct stm32mp1_clk_sel *sel = priv->data->sel;
        int i;
        int s, p;
+       unsigned int idx;
 
-       for (i = 0; i < ARRAY_SIZE(stm32mp1_clks); i++)
-               if (stm32mp1_clks[i][0] == id)
-                       return stm32mp1_clks[i][1];
+       for (idx = 0; idx < ARRAY_SIZE(stm32mp1_clks); idx++)
+               if (stm32mp1_clks[idx][0] == id)
+                       return stm32mp1_clks[idx][1];
 
        i = stm32mp1_clk_get_id(priv, id);
        if (i < 0)
@@ -1448,6 +1465,71 @@ static void pll_csg(struct stm32mp1_clk_priv *priv, int pll_id, u32 *csg)
        setbits_le32(priv->base + pll[pll_id].pllxcr, RCC_PLLNCR_SSCG_CTRL);
 }
 
+static  __maybe_unused int pll_set_rate(struct udevice *dev,
+                                       int pll_id,
+                                       int div_id,
+                                       unsigned long clk_rate)
+{
+       struct stm32mp1_clk_priv *priv = dev_get_priv(dev);
+       unsigned int pllcfg[PLLCFG_NB];
+       ofnode plloff;
+       char name[12];
+       const struct stm32mp1_clk_pll *pll = priv->data->pll;
+       enum stm32mp1_plltype type = pll[pll_id].plltype;
+       int divm, divn, divy;
+       int ret;
+       ulong fck_ref;
+       u32 fracv;
+       u64 value;
+
+       if (div_id > _DIV_NB)
+               return -EINVAL;
+
+       sprintf(name, "st,pll@%d", pll_id);
+       plloff = dev_read_subnode(dev, name);
+       if (!ofnode_valid(plloff))
+               return -FDT_ERR_NOTFOUND;
+
+       ret = ofnode_read_u32_array(plloff, "cfg",
+                                   pllcfg, PLLCFG_NB);
+       if (ret < 0)
+               return -FDT_ERR_NOTFOUND;
+
+       fck_ref = pll_get_fref_ck(priv, pll_id);
+
+       divm = pllcfg[PLLCFG_M];
+       /* select output divider = 0: for _DIV_P, 1:_DIV_Q 2:_DIV_R */
+       divy = pllcfg[PLLCFG_P + div_id];
+
+       /* For: PLL1 & PLL2 => VCO is * 2 but ck_pll_y is also / 2
+        * So same final result than PLL2 et 4
+        * with FRACV
+        * Fck_pll_y = Fck_ref * ((DIVN + 1) + FRACV / 2^13)
+        *             / (DIVy + 1) * (DIVM + 1)
+        * value = (DIVN + 1) * 2^13 + FRACV / 2^13
+        *       = Fck_pll_y (DIVy + 1) * (DIVM + 1) * 2^13 / Fck_ref
+        */
+       value = ((u64)clk_rate * (divy + 1) * (divm + 1)) << 13;
+       value = lldiv(value, fck_ref);
+
+       divn = (value >> 13) - 1;
+       if (divn < DIVN_MIN ||
+           divn > stm32mp1_pll[type].divn_max) {
+               pr_err("divn invalid = %d", divn);
+               return -EINVAL;
+       }
+       fracv = value - ((divn + 1) << 13);
+       pllcfg[PLLCFG_N] = divn;
+
+       /* reconfigure PLL */
+       pll_stop(priv, pll_id);
+       pll_config(priv, pll_id, pllcfg, fracv);
+       pll_start(priv, pll_id);
+       pll_output(priv, pll_id, pllcfg[PLLCFG_O]);
+
+       return 0;
+}
+
 static int set_clksrc(struct stm32mp1_clk_priv *priv, unsigned int clksrc)
 {
        u32 address = priv->base + (clksrc >> 4);
@@ -1470,8 +1552,7 @@ static void stgen_config(struct stm32mp1_clk_priv *priv)
        u32 stgenc, cntfid0;
        ulong rate;
 
-       stgenc = (u32)syscon_get_first_range(STM32MP_SYSCON_STGEN);
-
+       stgenc = STM32_STGEN_BASE;
        cntfid0 = readl(stgenc + STGENC_CNTFID0);
        p = stm32mp1_clk_get_parent(priv, STGEN_K);
        rate = stm32mp1_clk_get(priv, p);
@@ -1820,6 +1901,11 @@ static ulong stm32mp1_clk_set_rate(struct clk *clk, unsigned long clk_rate)
        int p;
 
        switch (clk->id) {
+#if defined(STM32MP1_CLOCK_TREE_INIT) && \
+       defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
+       case DDRPHYC:
+               break;
+#endif
        case LTDC_PX:
        case DSI_PX:
                break;
@@ -1833,6 +1919,19 @@ static ulong stm32mp1_clk_set_rate(struct clk *clk, unsigned long clk_rate)
                return -EINVAL;
 
        switch (p) {
+#if defined(STM32MP1_CLOCK_TREE_INIT) && \
+       defined(CONFIG_STM32MP1_DDR_INTERACTIVE)
+       case _PLL2_R: /* DDRPHYC */
+       {
+               /* only for change DDR clock in interactive mode */
+               ulong result;
+
+               set_clksrc(priv, CLK_AXI_HSI);
+               result = pll_set_rate(clk->dev,  _PLL2, _DIV_R, clk_rate);
+               set_clksrc(priv, CLK_AXI_PLL2P);
+               return result;
+       }
+#endif
        case _PLL4_Q:
                /* for LTDC_PX and DSI_PX case */
                return pll_set_output_rate(clk->dev, _PLL4, _DIV_Q, clk_rate);