17154b046SJesse Taube // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 27154b046SJesse Taube /* 37154b046SJesse Taube * Copyright (C) 2021 47154b046SJesse Taube * Author(s): 57154b046SJesse Taube * Jesse Taube <Mr.Bossman075@gmail.com> 67154b046SJesse Taube * Giulio Benetti <giulio.benetti@benettiengineering.com> 77154b046SJesse Taube */ 87154b046SJesse Taube #include <linux/clk.h> 97154b046SJesse Taube #include <linux/of_address.h> 107154b046SJesse Taube #include <linux/of_irq.h> 117154b046SJesse Taube #include <linux/platform_device.h> 127154b046SJesse Taube #include <dt-bindings/clock/imxrt1050-clock.h> 137154b046SJesse Taube 147154b046SJesse Taube #include "clk.h" 157154b046SJesse Taube 167154b046SJesse Taube static const char * const pll_ref_sels[] = {"osc", "dummy", }; 177154b046SJesse Taube static const char * const per_sels[] = {"ipg_pdof", "osc", }; 187154b046SJesse Taube static const char * const pll1_bypass_sels[] = {"pll1_arm", "pll1_arm_ref_sel", }; 197154b046SJesse Taube static const char * const pll2_bypass_sels[] = {"pll2_sys", "pll2_sys_ref_sel", }; 207154b046SJesse Taube static const char * const pll3_bypass_sels[] = {"pll3_usb_otg", "pll3_usb_otg_ref_sel", }; 217154b046SJesse Taube static const char * const pll5_bypass_sels[] = {"pll5_video", "pll5_video_ref_sel", }; 227154b046SJesse Taube static const char *const pre_periph_sels[] = { 237154b046SJesse Taube "pll2_sys", "pll2_pfd2_396m", "pll2_pfd0_352m", "arm_podf", }; 247154b046SJesse Taube static const char *const periph_sels[] = { "pre_periph_sel", "todo", }; 257154b046SJesse Taube static const char *const usdhc_sels[] = { "pll2_pfd2_396m", "pll2_pfd0_352m", }; 267154b046SJesse Taube static const char *const lpuart_sels[] = { "pll3_80m", "osc", }; 277154b046SJesse Taube static const char *const lcdif_sels[] = { 287154b046SJesse Taube "pll2_sys", "pll3_pfd3_454_74m", "pll5_video", "pll2_pfd0_352m", 297154b046SJesse Taube "pll2_pfd1_594m", "pll3_pfd1_664_62m", }; 307154b046SJesse Taube static const char *const semc_alt_sels[] = { "pll2_pfd2_396m", "pll3_pfd1_664_62m", }; 317154b046SJesse Taube static const char *const semc_sels[] = { "periph_sel", "semc_alt_sel", }; 327154b046SJesse Taube 337154b046SJesse Taube static struct clk_hw **hws; 347154b046SJesse Taube static struct clk_hw_onecell_data *clk_hw_data; 357154b046SJesse Taube 367154b046SJesse Taube static int imxrt1050_clocks_probe(struct platform_device *pdev) 377154b046SJesse Taube { 387154b046SJesse Taube void __iomem *ccm_base; 397154b046SJesse Taube void __iomem *pll_base; 407154b046SJesse Taube struct device *dev = &pdev->dev; 417154b046SJesse Taube struct device_node *np = dev->of_node; 427154b046SJesse Taube struct device_node *anp; 437154b046SJesse Taube int ret; 447154b046SJesse Taube 457154b046SJesse Taube clk_hw_data = kzalloc(struct_size(clk_hw_data, hws, 467154b046SJesse Taube IMXRT1050_CLK_END), GFP_KERNEL); 477154b046SJesse Taube if (WARN_ON(!clk_hw_data)) 487154b046SJesse Taube return -ENOMEM; 497154b046SJesse Taube 507154b046SJesse Taube clk_hw_data->num = IMXRT1050_CLK_END; 517154b046SJesse Taube hws = clk_hw_data->hws; 527154b046SJesse Taube 537154b046SJesse Taube hws[IMXRT1050_CLK_OSC] = imx_obtain_fixed_clk_hw(np, "osc"); 547154b046SJesse Taube 557154b046SJesse Taube anp = of_find_compatible_node(NULL, NULL, "fsl,imxrt-anatop"); 567154b046SJesse Taube pll_base = of_iomap(anp, 0); 577154b046SJesse Taube of_node_put(anp); 587154b046SJesse Taube if (WARN_ON(!pll_base)) 597154b046SJesse Taube return -ENOMEM; 607154b046SJesse Taube 617154b046SJesse Taube /* Anatop clocks */ 627154b046SJesse Taube hws[IMXRT1050_CLK_DUMMY] = imx_clk_hw_fixed("dummy", 0UL); 637154b046SJesse Taube 647154b046SJesse Taube hws[IMXRT1050_CLK_PLL1_REF_SEL] = imx_clk_hw_mux("pll1_arm_ref_sel", 657154b046SJesse Taube pll_base + 0x0, 14, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels)); 667154b046SJesse Taube hws[IMXRT1050_CLK_PLL2_REF_SEL] = imx_clk_hw_mux("pll2_sys_ref_sel", 677154b046SJesse Taube pll_base + 0x30, 14, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels)); 687154b046SJesse Taube hws[IMXRT1050_CLK_PLL3_REF_SEL] = imx_clk_hw_mux("pll3_usb_otg_ref_sel", 697154b046SJesse Taube pll_base + 0x10, 14, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels)); 707154b046SJesse Taube hws[IMXRT1050_CLK_PLL5_REF_SEL] = imx_clk_hw_mux("pll5_video_ref_sel", 717154b046SJesse Taube pll_base + 0xa0, 14, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels)); 727154b046SJesse Taube 737154b046SJesse Taube hws[IMXRT1050_CLK_PLL1_ARM] = imx_clk_hw_pllv3(IMX_PLLV3_SYS, "pll1_arm", 747154b046SJesse Taube "pll1_arm_ref_sel", pll_base + 0x0, 0x7f); 757154b046SJesse Taube hws[IMXRT1050_CLK_PLL2_SYS] = imx_clk_hw_pllv3(IMX_PLLV3_GENERIC, "pll2_sys", 767154b046SJesse Taube "pll2_sys_ref_sel", pll_base + 0x30, 0x1); 777154b046SJesse Taube hws[IMXRT1050_CLK_PLL3_USB_OTG] = imx_clk_hw_pllv3(IMX_PLLV3_USB, "pll3_usb_otg", 787154b046SJesse Taube "pll3_usb_otg_ref_sel", pll_base + 0x10, 0x1); 797154b046SJesse Taube hws[IMXRT1050_CLK_PLL5_VIDEO] = imx_clk_hw_pllv3(IMX_PLLV3_AV, "pll5_video", 807154b046SJesse Taube "pll5_video_ref_sel", pll_base + 0xa0, 0x7f); 817154b046SJesse Taube 827154b046SJesse Taube /* PLL bypass out */ 837154b046SJesse Taube hws[IMXRT1050_CLK_PLL1_BYPASS] = imx_clk_hw_mux_flags("pll1_bypass", pll_base + 0x0, 16, 1, 847154b046SJesse Taube pll1_bypass_sels, ARRAY_SIZE(pll1_bypass_sels), CLK_SET_RATE_PARENT); 857154b046SJesse Taube hws[IMXRT1050_CLK_PLL2_BYPASS] = imx_clk_hw_mux_flags("pll2_bypass", pll_base + 0x30, 16, 1, 867154b046SJesse Taube pll2_bypass_sels, ARRAY_SIZE(pll2_bypass_sels), CLK_SET_RATE_PARENT); 877154b046SJesse Taube hws[IMXRT1050_CLK_PLL3_BYPASS] = imx_clk_hw_mux_flags("pll3_bypass", pll_base + 0x10, 16, 1, 887154b046SJesse Taube pll3_bypass_sels, ARRAY_SIZE(pll3_bypass_sels), CLK_SET_RATE_PARENT); 897154b046SJesse Taube hws[IMXRT1050_CLK_PLL5_BYPASS] = imx_clk_hw_mux_flags("pll5_bypass", pll_base + 0xa0, 16, 1, 907154b046SJesse Taube pll5_bypass_sels, ARRAY_SIZE(pll5_bypass_sels), CLK_SET_RATE_PARENT); 917154b046SJesse Taube 927154b046SJesse Taube hws[IMXRT1050_CLK_VIDEO_POST_DIV_SEL] = imx_clk_hw_divider("video_post_div_sel", 937154b046SJesse Taube "pll5_video", pll_base + 0xa0, 19, 2); 947154b046SJesse Taube hws[IMXRT1050_CLK_VIDEO_DIV] = imx_clk_hw_divider("video_div", 957154b046SJesse Taube "video_post_div_sel", pll_base + 0x170, 30, 2); 967154b046SJesse Taube 977154b046SJesse Taube hws[IMXRT1050_CLK_PLL3_80M] = imx_clk_hw_fixed_factor("pll3_80m", "pll3_usb_otg", 1, 6); 987154b046SJesse Taube 997154b046SJesse Taube hws[IMXRT1050_CLK_PLL2_PFD0_352M] = imx_clk_hw_pfd("pll2_pfd0_352m", "pll2_sys", pll_base + 0x100, 0); 1007154b046SJesse Taube hws[IMXRT1050_CLK_PLL2_PFD1_594M] = imx_clk_hw_pfd("pll2_pfd1_594m", "pll2_sys", pll_base + 0x100, 1); 1017154b046SJesse Taube hws[IMXRT1050_CLK_PLL2_PFD2_396M] = imx_clk_hw_pfd("pll2_pfd2_396m", "pll2_sys", pll_base + 0x100, 2); 1027154b046SJesse Taube hws[IMXRT1050_CLK_PLL3_PFD1_664_62M] = imx_clk_hw_pfd("pll3_pfd1_664_62m", "pll3_usb_otg", pll_base + 0xf0, 1); 1037154b046SJesse Taube hws[IMXRT1050_CLK_PLL3_PFD3_454_74M] = imx_clk_hw_pfd("pll3_pfd3_454_74m", "pll3_usb_otg", pll_base + 0xf0, 3); 1047154b046SJesse Taube 1057154b046SJesse Taube /* CCM clocks */ 1067154b046SJesse Taube ccm_base = devm_platform_ioremap_resource(pdev, 0); 1077154b046SJesse Taube if (WARN_ON(IS_ERR(ccm_base))) 1087154b046SJesse Taube return PTR_ERR(ccm_base); 1097154b046SJesse Taube 1107154b046SJesse Taube hws[IMXRT1050_CLK_ARM_PODF] = imx_clk_hw_divider("arm_podf", "pll1_arm", ccm_base + 0x10, 0, 3); 1117154b046SJesse Taube hws[IMXRT1050_CLK_PRE_PERIPH_SEL] = imx_clk_hw_mux("pre_periph_sel", ccm_base + 0x18, 18, 2, 1127154b046SJesse Taube pre_periph_sels, ARRAY_SIZE(pre_periph_sels)); 1137154b046SJesse Taube hws[IMXRT1050_CLK_PERIPH_SEL] = imx_clk_hw_mux("periph_sel", ccm_base + 0x14, 25, 1, 1147154b046SJesse Taube periph_sels, ARRAY_SIZE(periph_sels)); 1157154b046SJesse Taube hws[IMXRT1050_CLK_USDHC1_SEL] = imx_clk_hw_mux("usdhc1_sel", ccm_base + 0x1c, 16, 1, 1167154b046SJesse Taube usdhc_sels, ARRAY_SIZE(usdhc_sels)); 1177154b046SJesse Taube hws[IMXRT1050_CLK_USDHC2_SEL] = imx_clk_hw_mux("usdhc2_sel", ccm_base + 0x1c, 17, 1, 1187154b046SJesse Taube usdhc_sels, ARRAY_SIZE(usdhc_sels)); 1197154b046SJesse Taube hws[IMXRT1050_CLK_LPUART_SEL] = imx_clk_hw_mux("lpuart_sel", ccm_base + 0x24, 6, 1, 1207154b046SJesse Taube lpuart_sels, ARRAY_SIZE(lpuart_sels)); 1217154b046SJesse Taube hws[IMXRT1050_CLK_LCDIF_SEL] = imx_clk_hw_mux("lcdif_sel", ccm_base + 0x38, 15, 3, 1227154b046SJesse Taube lcdif_sels, ARRAY_SIZE(lcdif_sels)); 1237154b046SJesse Taube hws[IMXRT1050_CLK_PER_CLK_SEL] = imx_clk_hw_mux("per_sel", ccm_base + 0x1C, 6, 1, 1247154b046SJesse Taube per_sels, ARRAY_SIZE(per_sels)); 1257154b046SJesse Taube hws[IMXRT1050_CLK_SEMC_ALT_SEL] = imx_clk_hw_mux("semc_alt_sel", ccm_base + 0x14, 7, 1, 1267154b046SJesse Taube semc_alt_sels, ARRAY_SIZE(semc_alt_sels)); 1277154b046SJesse Taube hws[IMXRT1050_CLK_SEMC_SEL] = imx_clk_hw_mux_flags("semc_sel", ccm_base + 0x14, 6, 1, 1287154b046SJesse Taube semc_sels, ARRAY_SIZE(semc_sels), CLK_IS_CRITICAL); 1297154b046SJesse Taube 1307154b046SJesse Taube hws[IMXRT1050_CLK_AHB_PODF] = imx_clk_hw_divider("ahb", "periph_sel", ccm_base + 0x14, 10, 3); 1317154b046SJesse Taube hws[IMXRT1050_CLK_IPG_PDOF] = imx_clk_hw_divider("ipg", "ahb", ccm_base + 0x14, 8, 2); 1327154b046SJesse Taube hws[IMXRT1050_CLK_PER_PDOF] = imx_clk_hw_divider("per", "per_sel", ccm_base + 0x1C, 0, 5); 1337154b046SJesse Taube 1347154b046SJesse Taube hws[IMXRT1050_CLK_USDHC1_PODF] = imx_clk_hw_divider("usdhc1_podf", "usdhc1_sel", ccm_base + 0x24, 11, 3); 1357154b046SJesse Taube hws[IMXRT1050_CLK_USDHC2_PODF] = imx_clk_hw_divider("usdhc2_podf", "usdhc2_sel", ccm_base + 0x24, 16, 3); 1367154b046SJesse Taube hws[IMXRT1050_CLK_LPUART_PODF] = imx_clk_hw_divider("lpuart_podf", "lpuart_sel", ccm_base + 0x24, 0, 6); 1377154b046SJesse Taube hws[IMXRT1050_CLK_LCDIF_PRED] = imx_clk_hw_divider("lcdif_pred", "lcdif_sel", ccm_base + 0x38, 12, 3); 1387154b046SJesse Taube hws[IMXRT1050_CLK_LCDIF_PODF] = imx_clk_hw_divider("lcdif_podf", "lcdif_pred", ccm_base + 0x18, 23, 3); 1397154b046SJesse Taube 1407154b046SJesse Taube hws[IMXRT1050_CLK_USDHC1] = imx_clk_hw_gate2("usdhc1", "usdhc1_podf", ccm_base + 0x80, 2); 1417154b046SJesse Taube hws[IMXRT1050_CLK_USDHC2] = imx_clk_hw_gate2("usdhc2", "usdhc2_podf", ccm_base + 0x80, 4); 1427154b046SJesse Taube hws[IMXRT1050_CLK_LPUART1] = imx_clk_hw_gate2("lpuart1", "lpuart_podf", ccm_base + 0x7c, 24); 1433095c02fSGiulio Benetti hws[IMXRT1050_CLK_LCDIF_APB] = imx_clk_hw_gate2("lcdif", "lcdif_podf", ccm_base + 0x70, 28); 144*ec689c15SGiulio Benetti hws[IMXRT1050_CLK_LCDIF_PIX] = imx_clk_hw_gate2("lcdif_pix", "lcdif", ccm_base + 0x74, 10); 1457154b046SJesse Taube hws[IMXRT1050_CLK_DMA] = imx_clk_hw_gate("dma", "ipg", ccm_base + 0x7C, 6); 1467154b046SJesse Taube hws[IMXRT1050_CLK_DMA_MUX] = imx_clk_hw_gate("dmamux0", "ipg", ccm_base + 0x7C, 7); 1477154b046SJesse Taube imx_check_clk_hws(hws, IMXRT1050_CLK_END); 1487154b046SJesse Taube 1497154b046SJesse Taube ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_hw_data); 1507154b046SJesse Taube if (ret < 0) { 1517154b046SJesse Taube dev_err(dev, "Failed to register clks for i.MXRT1050.\n"); 1527154b046SJesse Taube imx_unregister_hw_clocks(hws, IMXRT1050_CLK_END); 1537154b046SJesse Taube } 1547154b046SJesse Taube return ret; 1557154b046SJesse Taube } 1567154b046SJesse Taube static const struct of_device_id imxrt1050_clk_of_match[] = { 1577154b046SJesse Taube { .compatible = "fsl,imxrt1050-ccm" }, 1587154b046SJesse Taube { /* Sentinel */ } 1597154b046SJesse Taube }; 1607154b046SJesse Taube MODULE_DEVICE_TABLE(of, imxrt1050_clk_of_match); 1617154b046SJesse Taube 1627154b046SJesse Taube static struct platform_driver imxrt1050_clk_driver = { 1637154b046SJesse Taube .probe = imxrt1050_clocks_probe, 1647154b046SJesse Taube .driver = { 1657154b046SJesse Taube .name = "imxrt1050-ccm", 1667154b046SJesse Taube .of_match_table = imxrt1050_clk_of_match, 1677154b046SJesse Taube }, 1687154b046SJesse Taube }; 1697154b046SJesse Taube module_platform_driver(imxrt1050_clk_driver); 170