111f68120SShawn Guo /* 211f68120SShawn Guo * Copyright (C) 2012 Sascha Hauer <kernel@pengutronix.de> 311f68120SShawn Guo * 411f68120SShawn Guo * This program is free software; you can redistribute it and/or 511f68120SShawn Guo * modify it under the terms of the GNU General Public License 611f68120SShawn Guo * as published by the Free Software Foundation; either version 2 711f68120SShawn Guo * of the License, or (at your option) any later version. 811f68120SShawn Guo * This program is distributed in the hope that it will be useful, 911f68120SShawn Guo * but WITHOUT ANY WARRANTY; without even the implied warranty of 1011f68120SShawn Guo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1111f68120SShawn Guo * GNU General Public License for more details. 1211f68120SShawn Guo * 1311f68120SShawn Guo * You should have received a copy of the GNU General Public License 1411f68120SShawn Guo * along with this program; if not, write to the Free Software 1511f68120SShawn Guo * Foundation. 1611f68120SShawn Guo */ 1711f68120SShawn Guo 1811f68120SShawn Guo #include <linux/module.h> 1911f68120SShawn Guo #include <linux/clk.h> 2011f68120SShawn Guo #include <linux/clkdev.h> 2111f68120SShawn Guo #include <linux/io.h> 2211f68120SShawn Guo #include <linux/err.h> 2311f68120SShawn Guo #include <linux/of.h> 2411f68120SShawn Guo #include <soc/imx/revision.h> 250931aff7SShawn Guo #include <soc/imx/timer.h> 2611f68120SShawn Guo #include <asm/irq.h> 2711f68120SShawn Guo 2811f68120SShawn Guo #include "clk.h" 2911f68120SShawn Guo 3011f68120SShawn Guo #define MX31_CCM_BASE_ADDR 0x53f80000 3111f68120SShawn Guo #define MX31_GPT1_BASE_ADDR 0x53f90000 3211f68120SShawn Guo #define MX31_INT_GPT (NR_IRQS_LEGACY + 29) 3311f68120SShawn Guo 3411f68120SShawn Guo #define MXC_CCM_CCMR 0x00 3511f68120SShawn Guo #define MXC_CCM_PDR0 0x04 3611f68120SShawn Guo #define MXC_CCM_PDR1 0x08 3711f68120SShawn Guo #define MXC_CCM_MPCTL 0x10 3811f68120SShawn Guo #define MXC_CCM_UPCTL 0x14 3911f68120SShawn Guo #define MXC_CCM_SRPCTL 0x18 4011f68120SShawn Guo #define MXC_CCM_CGR0 0x20 4111f68120SShawn Guo #define MXC_CCM_CGR1 0x24 4211f68120SShawn Guo #define MXC_CCM_CGR2 0x28 4311f68120SShawn Guo #define MXC_CCM_PMCR0 0x5c 4411f68120SShawn Guo 4511f68120SShawn Guo static const char *mcu_main_sel[] = { "spll", "mpll", }; 4611f68120SShawn Guo static const char *per_sel[] = { "per_div", "ipg", }; 4711f68120SShawn Guo static const char *csi_sel[] = { "upll", "spll", }; 4811f68120SShawn Guo static const char *fir_sel[] = { "mcu_main", "upll", "spll" }; 4911f68120SShawn Guo 5011f68120SShawn Guo enum mx31_clks { 5111f68120SShawn Guo dummy, ckih, ckil, mpll, spll, upll, mcu_main, hsp, ahb, nfc, ipg, 5211f68120SShawn Guo per_div, per, csi, fir, csi_div, usb_div_pre, usb_div_post, fir_div_pre, 5311f68120SShawn Guo fir_div_post, sdhc1_gate, sdhc2_gate, gpt_gate, epit1_gate, epit2_gate, 5411f68120SShawn Guo iim_gate, ata_gate, sdma_gate, cspi3_gate, rng_gate, uart1_gate, 5511f68120SShawn Guo uart2_gate, ssi1_gate, i2c1_gate, i2c2_gate, i2c3_gate, hantro_gate, 5611f68120SShawn Guo mstick1_gate, mstick2_gate, csi_gate, rtc_gate, wdog_gate, pwm_gate, 5711f68120SShawn Guo sim_gate, ect_gate, usb_gate, kpp_gate, ipu_gate, uart3_gate, 5811f68120SShawn Guo uart4_gate, uart5_gate, owire_gate, ssi2_gate, cspi1_gate, cspi2_gate, 5911f68120SShawn Guo gacc_gate, emi_gate, rtic_gate, firi_gate, clk_max 6011f68120SShawn Guo }; 6111f68120SShawn Guo 6211f68120SShawn Guo static struct clk *clk[clk_max]; 6311f68120SShawn Guo static struct clk_onecell_data clk_data; 6411f68120SShawn Guo 655c678cddSLucas Stach static struct clk ** const uart_clks[] __initconst = { 665c678cddSLucas Stach &clk[ipg], 675c678cddSLucas Stach &clk[uart1_gate], 685c678cddSLucas Stach &clk[uart2_gate], 695c678cddSLucas Stach &clk[uart3_gate], 705c678cddSLucas Stach &clk[uart4_gate], 715c678cddSLucas Stach &clk[uart5_gate], 725c678cddSLucas Stach NULL 735c678cddSLucas Stach }; 745c678cddSLucas Stach 75d9388c84SAlexander Stein static void __init _mx31_clocks_init(unsigned long fref) 7611f68120SShawn Guo { 7711f68120SShawn Guo void __iomem *base; 7811f68120SShawn Guo struct device_node *np; 7911f68120SShawn Guo 8011f68120SShawn Guo base = ioremap(MX31_CCM_BASE_ADDR, SZ_4K); 8111f68120SShawn Guo BUG_ON(!base); 8211f68120SShawn Guo 8311f68120SShawn Guo clk[dummy] = imx_clk_fixed("dummy", 0); 8411f68120SShawn Guo clk[ckih] = imx_clk_fixed("ckih", fref); 8511f68120SShawn Guo clk[ckil] = imx_clk_fixed("ckil", 32768); 8611f68120SShawn Guo clk[mpll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "mpll", "ckih", base + MXC_CCM_MPCTL); 8711f68120SShawn Guo clk[spll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "spll", "ckih", base + MXC_CCM_SRPCTL); 8811f68120SShawn Guo clk[upll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "upll", "ckih", base + MXC_CCM_UPCTL); 8911f68120SShawn Guo clk[mcu_main] = imx_clk_mux("mcu_main", base + MXC_CCM_PMCR0, 31, 1, mcu_main_sel, ARRAY_SIZE(mcu_main_sel)); 9011f68120SShawn Guo clk[hsp] = imx_clk_divider("hsp", "mcu_main", base + MXC_CCM_PDR0, 11, 3); 9111f68120SShawn Guo clk[ahb] = imx_clk_divider("ahb", "mcu_main", base + MXC_CCM_PDR0, 3, 3); 9211f68120SShawn Guo clk[nfc] = imx_clk_divider("nfc", "ahb", base + MXC_CCM_PDR0, 8, 3); 9311f68120SShawn Guo clk[ipg] = imx_clk_divider("ipg", "ahb", base + MXC_CCM_PDR0, 6, 2); 9411f68120SShawn Guo clk[per_div] = imx_clk_divider("per_div", "upll", base + MXC_CCM_PDR0, 16, 5); 9511f68120SShawn Guo clk[per] = imx_clk_mux("per", base + MXC_CCM_CCMR, 24, 1, per_sel, ARRAY_SIZE(per_sel)); 9611f68120SShawn Guo clk[csi] = imx_clk_mux("csi_sel", base + MXC_CCM_CCMR, 25, 1, csi_sel, ARRAY_SIZE(csi_sel)); 9711f68120SShawn Guo clk[fir] = imx_clk_mux("fir_sel", base + MXC_CCM_CCMR, 11, 2, fir_sel, ARRAY_SIZE(fir_sel)); 9811f68120SShawn Guo clk[csi_div] = imx_clk_divider("csi_div", "csi_sel", base + MXC_CCM_PDR0, 23, 9); 9911f68120SShawn Guo clk[usb_div_pre] = imx_clk_divider("usb_div_pre", "upll", base + MXC_CCM_PDR1, 30, 2); 10011f68120SShawn Guo clk[usb_div_post] = imx_clk_divider("usb_div_post", "usb_div_pre", base + MXC_CCM_PDR1, 27, 3); 10111f68120SShawn Guo clk[fir_div_pre] = imx_clk_divider("fir_div_pre", "fir_sel", base + MXC_CCM_PDR1, 24, 3); 10211f68120SShawn Guo clk[fir_div_post] = imx_clk_divider("fir_div_post", "fir_div_pre", base + MXC_CCM_PDR1, 23, 6); 10311f68120SShawn Guo clk[sdhc1_gate] = imx_clk_gate2("sdhc1_gate", "per", base + MXC_CCM_CGR0, 0); 10411f68120SShawn Guo clk[sdhc2_gate] = imx_clk_gate2("sdhc2_gate", "per", base + MXC_CCM_CGR0, 2); 10511f68120SShawn Guo clk[gpt_gate] = imx_clk_gate2("gpt_gate", "per", base + MXC_CCM_CGR0, 4); 10611f68120SShawn Guo clk[epit1_gate] = imx_clk_gate2("epit1_gate", "per", base + MXC_CCM_CGR0, 6); 10711f68120SShawn Guo clk[epit2_gate] = imx_clk_gate2("epit2_gate", "per", base + MXC_CCM_CGR0, 8); 10811f68120SShawn Guo clk[iim_gate] = imx_clk_gate2("iim_gate", "ipg", base + MXC_CCM_CGR0, 10); 10911f68120SShawn Guo clk[ata_gate] = imx_clk_gate2("ata_gate", "ipg", base + MXC_CCM_CGR0, 12); 11011f68120SShawn Guo clk[sdma_gate] = imx_clk_gate2("sdma_gate", "ahb", base + MXC_CCM_CGR0, 14); 11111f68120SShawn Guo clk[cspi3_gate] = imx_clk_gate2("cspi3_gate", "ipg", base + MXC_CCM_CGR0, 16); 11211f68120SShawn Guo clk[rng_gate] = imx_clk_gate2("rng_gate", "ipg", base + MXC_CCM_CGR0, 18); 11311f68120SShawn Guo clk[uart1_gate] = imx_clk_gate2("uart1_gate", "per", base + MXC_CCM_CGR0, 20); 11411f68120SShawn Guo clk[uart2_gate] = imx_clk_gate2("uart2_gate", "per", base + MXC_CCM_CGR0, 22); 11511f68120SShawn Guo clk[ssi1_gate] = imx_clk_gate2("ssi1_gate", "spll", base + MXC_CCM_CGR0, 24); 11611f68120SShawn Guo clk[i2c1_gate] = imx_clk_gate2("i2c1_gate", "per", base + MXC_CCM_CGR0, 26); 11711f68120SShawn Guo clk[i2c2_gate] = imx_clk_gate2("i2c2_gate", "per", base + MXC_CCM_CGR0, 28); 11811f68120SShawn Guo clk[i2c3_gate] = imx_clk_gate2("i2c3_gate", "per", base + MXC_CCM_CGR0, 30); 11911f68120SShawn Guo clk[hantro_gate] = imx_clk_gate2("hantro_gate", "per", base + MXC_CCM_CGR1, 0); 12011f68120SShawn Guo clk[mstick1_gate] = imx_clk_gate2("mstick1_gate", "per", base + MXC_CCM_CGR1, 2); 12111f68120SShawn Guo clk[mstick2_gate] = imx_clk_gate2("mstick2_gate", "per", base + MXC_CCM_CGR1, 4); 12211f68120SShawn Guo clk[csi_gate] = imx_clk_gate2("csi_gate", "csi_div", base + MXC_CCM_CGR1, 6); 12311f68120SShawn Guo clk[rtc_gate] = imx_clk_gate2("rtc_gate", "ipg", base + MXC_CCM_CGR1, 8); 12411f68120SShawn Guo clk[wdog_gate] = imx_clk_gate2("wdog_gate", "ipg", base + MXC_CCM_CGR1, 10); 12511f68120SShawn Guo clk[pwm_gate] = imx_clk_gate2("pwm_gate", "per", base + MXC_CCM_CGR1, 12); 12611f68120SShawn Guo clk[sim_gate] = imx_clk_gate2("sim_gate", "per", base + MXC_CCM_CGR1, 14); 12711f68120SShawn Guo clk[ect_gate] = imx_clk_gate2("ect_gate", "per", base + MXC_CCM_CGR1, 16); 12811f68120SShawn Guo clk[usb_gate] = imx_clk_gate2("usb_gate", "ahb", base + MXC_CCM_CGR1, 18); 12911f68120SShawn Guo clk[kpp_gate] = imx_clk_gate2("kpp_gate", "ipg", base + MXC_CCM_CGR1, 20); 13011f68120SShawn Guo clk[ipu_gate] = imx_clk_gate2("ipu_gate", "hsp", base + MXC_CCM_CGR1, 22); 13111f68120SShawn Guo clk[uart3_gate] = imx_clk_gate2("uart3_gate", "per", base + MXC_CCM_CGR1, 24); 13211f68120SShawn Guo clk[uart4_gate] = imx_clk_gate2("uart4_gate", "per", base + MXC_CCM_CGR1, 26); 13311f68120SShawn Guo clk[uart5_gate] = imx_clk_gate2("uart5_gate", "per", base + MXC_CCM_CGR1, 28); 13411f68120SShawn Guo clk[owire_gate] = imx_clk_gate2("owire_gate", "per", base + MXC_CCM_CGR1, 30); 13511f68120SShawn Guo clk[ssi2_gate] = imx_clk_gate2("ssi2_gate", "spll", base + MXC_CCM_CGR2, 0); 13611f68120SShawn Guo clk[cspi1_gate] = imx_clk_gate2("cspi1_gate", "ipg", base + MXC_CCM_CGR2, 2); 13711f68120SShawn Guo clk[cspi2_gate] = imx_clk_gate2("cspi2_gate", "ipg", base + MXC_CCM_CGR2, 4); 13811f68120SShawn Guo clk[gacc_gate] = imx_clk_gate2("gacc_gate", "per", base + MXC_CCM_CGR2, 6); 13911f68120SShawn Guo clk[emi_gate] = imx_clk_gate2("emi_gate", "ahb", base + MXC_CCM_CGR2, 8); 14011f68120SShawn Guo clk[rtic_gate] = imx_clk_gate2("rtic_gate", "ahb", base + MXC_CCM_CGR2, 10); 14111f68120SShawn Guo clk[firi_gate] = imx_clk_gate2("firi_gate", "upll", base+MXC_CCM_CGR2, 12); 14211f68120SShawn Guo 14311f68120SShawn Guo imx_check_clocks(clk, ARRAY_SIZE(clk)); 14411f68120SShawn Guo 145d9388c84SAlexander Stein clk_set_parent(clk[csi], clk[upll]); 146d9388c84SAlexander Stein clk_prepare_enable(clk[emi_gate]); 147d9388c84SAlexander Stein clk_prepare_enable(clk[iim_gate]); 148d9388c84SAlexander Stein mx31_revision(); 149d9388c84SAlexander Stein clk_disable_unprepare(clk[iim_gate]); 150d9388c84SAlexander Stein 15111f68120SShawn Guo np = of_find_compatible_node(NULL, NULL, "fsl,imx31-ccm"); 15211f68120SShawn Guo 15311f68120SShawn Guo if (np) { 15411f68120SShawn Guo clk_data.clks = clk; 15511f68120SShawn Guo clk_data.clk_num = ARRAY_SIZE(clk); 15611f68120SShawn Guo of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data); 15711f68120SShawn Guo } 158d9388c84SAlexander Stein } 159d9388c84SAlexander Stein 160d9388c84SAlexander Stein int __init mx31_clocks_init(void) 161d9388c84SAlexander Stein { 162d9388c84SAlexander Stein u32 fref = 26000000; /* default */ 163d9388c84SAlexander Stein 164d9388c84SAlexander Stein _mx31_clocks_init(fref); 16511f68120SShawn Guo 16611f68120SShawn Guo clk_register_clkdev(clk[gpt_gate], "per", "imx-gpt.0"); 16711f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx-gpt.0"); 16811f68120SShawn Guo clk_register_clkdev(clk[cspi1_gate], NULL, "imx31-cspi.0"); 16911f68120SShawn Guo clk_register_clkdev(clk[cspi2_gate], NULL, "imx31-cspi.1"); 17011f68120SShawn Guo clk_register_clkdev(clk[cspi3_gate], NULL, "imx31-cspi.2"); 17111f68120SShawn Guo clk_register_clkdev(clk[pwm_gate], "pwm", NULL); 17211f68120SShawn Guo clk_register_clkdev(clk[wdog_gate], NULL, "imx2-wdt.0"); 17300a48fe3SPhilippe Reynes clk_register_clkdev(clk[ckil], "ref", "imx21-rtc"); 17400a48fe3SPhilippe Reynes clk_register_clkdev(clk[rtc_gate], "ipg", "imx21-rtc"); 17511f68120SShawn Guo clk_register_clkdev(clk[epit1_gate], "epit", NULL); 17611f68120SShawn Guo clk_register_clkdev(clk[epit2_gate], "epit", NULL); 17711f68120SShawn Guo clk_register_clkdev(clk[nfc], NULL, "imx27-nand.0"); 17811f68120SShawn Guo clk_register_clkdev(clk[ipu_gate], NULL, "ipu-core"); 17911f68120SShawn Guo clk_register_clkdev(clk[ipu_gate], NULL, "mx3_sdc_fb"); 18011f68120SShawn Guo clk_register_clkdev(clk[kpp_gate], NULL, "imx-keypad"); 18111f68120SShawn Guo clk_register_clkdev(clk[usb_div_post], "per", "mxc-ehci.0"); 18211f68120SShawn Guo clk_register_clkdev(clk[usb_gate], "ahb", "mxc-ehci.0"); 18311f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "mxc-ehci.0"); 18411f68120SShawn Guo clk_register_clkdev(clk[usb_div_post], "per", "mxc-ehci.1"); 18511f68120SShawn Guo clk_register_clkdev(clk[usb_gate], "ahb", "mxc-ehci.1"); 18611f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "mxc-ehci.1"); 18711f68120SShawn Guo clk_register_clkdev(clk[usb_div_post], "per", "mxc-ehci.2"); 18811f68120SShawn Guo clk_register_clkdev(clk[usb_gate], "ahb", "mxc-ehci.2"); 18911f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "mxc-ehci.2"); 19011f68120SShawn Guo clk_register_clkdev(clk[usb_div_post], "per", "imx-udc-mx27"); 19111f68120SShawn Guo clk_register_clkdev(clk[usb_gate], "ahb", "imx-udc-mx27"); 19211f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx-udc-mx27"); 19311f68120SShawn Guo clk_register_clkdev(clk[csi_gate], NULL, "mx3-camera.0"); 19411f68120SShawn Guo /* i.mx31 has the i.mx21 type uart */ 19511f68120SShawn Guo clk_register_clkdev(clk[uart1_gate], "per", "imx21-uart.0"); 19611f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx21-uart.0"); 19711f68120SShawn Guo clk_register_clkdev(clk[uart2_gate], "per", "imx21-uart.1"); 19811f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx21-uart.1"); 19911f68120SShawn Guo clk_register_clkdev(clk[uart3_gate], "per", "imx21-uart.2"); 20011f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx21-uart.2"); 20111f68120SShawn Guo clk_register_clkdev(clk[uart4_gate], "per", "imx21-uart.3"); 20211f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx21-uart.3"); 20311f68120SShawn Guo clk_register_clkdev(clk[uart5_gate], "per", "imx21-uart.4"); 20411f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx21-uart.4"); 20511f68120SShawn Guo clk_register_clkdev(clk[i2c1_gate], NULL, "imx21-i2c.0"); 20611f68120SShawn Guo clk_register_clkdev(clk[i2c2_gate], NULL, "imx21-i2c.1"); 20711f68120SShawn Guo clk_register_clkdev(clk[i2c3_gate], NULL, "imx21-i2c.2"); 20811f68120SShawn Guo clk_register_clkdev(clk[owire_gate], NULL, "mxc_w1.0"); 20911f68120SShawn Guo clk_register_clkdev(clk[sdhc1_gate], NULL, "imx31-mmc.0"); 21011f68120SShawn Guo clk_register_clkdev(clk[sdhc2_gate], NULL, "imx31-mmc.1"); 21111f68120SShawn Guo clk_register_clkdev(clk[ssi1_gate], NULL, "imx-ssi.0"); 21211f68120SShawn Guo clk_register_clkdev(clk[ssi2_gate], NULL, "imx-ssi.1"); 21311f68120SShawn Guo clk_register_clkdev(clk[firi_gate], "firi", NULL); 21411f68120SShawn Guo clk_register_clkdev(clk[ata_gate], NULL, "pata_imx"); 21511f68120SShawn Guo clk_register_clkdev(clk[rtic_gate], "rtic", NULL); 21611f68120SShawn Guo clk_register_clkdev(clk[rng_gate], NULL, "mxc_rnga"); 21711f68120SShawn Guo clk_register_clkdev(clk[sdma_gate], NULL, "imx31-sdma"); 21811f68120SShawn Guo clk_register_clkdev(clk[iim_gate], "iim", NULL); 21911f68120SShawn Guo 22011f68120SShawn Guo 2215c678cddSLucas Stach imx_register_uart_clocks(uart_clks); 2220931aff7SShawn Guo mxc_timer_init(MX31_GPT1_BASE_ADDR, MX31_INT_GPT, GPT_TYPE_IMX31); 22311f68120SShawn Guo 22411f68120SShawn Guo return 0; 22511f68120SShawn Guo } 22611f68120SShawn Guo 22711f68120SShawn Guo int __init mx31_clocks_init_dt(void) 22811f68120SShawn Guo { 22911f68120SShawn Guo struct device_node *np; 23011f68120SShawn Guo u32 fref = 26000000; /* default */ 23111f68120SShawn Guo 23211f68120SShawn Guo for_each_compatible_node(np, NULL, "fixed-clock") { 23311f68120SShawn Guo if (!of_device_is_compatible(np, "fsl,imx-osc26m")) 23411f68120SShawn Guo continue; 23511f68120SShawn Guo 236*489e5d41SJulia Lawall if (!of_property_read_u32(np, "clock-frequency", &fref)) { 237*489e5d41SJulia Lawall of_node_put(np); 23811f68120SShawn Guo break; 23911f68120SShawn Guo } 240*489e5d41SJulia Lawall } 24111f68120SShawn Guo 242d9388c84SAlexander Stein _mx31_clocks_init(fref); 243d9388c84SAlexander Stein 244d9388c84SAlexander Stein return 0; 24511f68120SShawn Guo } 246