1*11f68120SShawn Guo /* 2*11f68120SShawn Guo * Copyright (C) 2012 Sascha Hauer <kernel@pengutronix.de> 3*11f68120SShawn Guo * 4*11f68120SShawn Guo * This program is free software; you can redistribute it and/or 5*11f68120SShawn Guo * modify it under the terms of the GNU General Public License 6*11f68120SShawn Guo * as published by the Free Software Foundation; either version 2 7*11f68120SShawn Guo * of the License, or (at your option) any later version. 8*11f68120SShawn Guo * This program is distributed in the hope that it will be useful, 9*11f68120SShawn Guo * but WITHOUT ANY WARRANTY; without even the implied warranty of 10*11f68120SShawn Guo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11*11f68120SShawn Guo * GNU General Public License for more details. 12*11f68120SShawn Guo * 13*11f68120SShawn Guo * You should have received a copy of the GNU General Public License 14*11f68120SShawn Guo * along with this program; if not, write to the Free Software 15*11f68120SShawn Guo * Foundation. 16*11f68120SShawn Guo */ 17*11f68120SShawn Guo 18*11f68120SShawn Guo #include <linux/module.h> 19*11f68120SShawn Guo #include <linux/clk.h> 20*11f68120SShawn Guo #include <linux/clkdev.h> 21*11f68120SShawn Guo #include <linux/io.h> 22*11f68120SShawn Guo #include <linux/err.h> 23*11f68120SShawn Guo #include <linux/of.h> 24*11f68120SShawn Guo #include <soc/imx/revision.h> 25*11f68120SShawn Guo #include <asm/irq.h> 26*11f68120SShawn Guo 27*11f68120SShawn Guo #include "clk.h" 28*11f68120SShawn Guo 29*11f68120SShawn Guo #define MX31_CCM_BASE_ADDR 0x53f80000 30*11f68120SShawn Guo #define MX31_GPT1_BASE_ADDR 0x53f90000 31*11f68120SShawn Guo #define MX31_INT_GPT (NR_IRQS_LEGACY + 29) 32*11f68120SShawn Guo 33*11f68120SShawn Guo #define MXC_CCM_CCMR 0x00 34*11f68120SShawn Guo #define MXC_CCM_PDR0 0x04 35*11f68120SShawn Guo #define MXC_CCM_PDR1 0x08 36*11f68120SShawn Guo #define MXC_CCM_MPCTL 0x10 37*11f68120SShawn Guo #define MXC_CCM_UPCTL 0x14 38*11f68120SShawn Guo #define MXC_CCM_SRPCTL 0x18 39*11f68120SShawn Guo #define MXC_CCM_CGR0 0x20 40*11f68120SShawn Guo #define MXC_CCM_CGR1 0x24 41*11f68120SShawn Guo #define MXC_CCM_CGR2 0x28 42*11f68120SShawn Guo #define MXC_CCM_PMCR0 0x5c 43*11f68120SShawn Guo 44*11f68120SShawn Guo static const char *mcu_main_sel[] = { "spll", "mpll", }; 45*11f68120SShawn Guo static const char *per_sel[] = { "per_div", "ipg", }; 46*11f68120SShawn Guo static const char *csi_sel[] = { "upll", "spll", }; 47*11f68120SShawn Guo static const char *fir_sel[] = { "mcu_main", "upll", "spll" }; 48*11f68120SShawn Guo 49*11f68120SShawn Guo enum mx31_clks { 50*11f68120SShawn Guo dummy, ckih, ckil, mpll, spll, upll, mcu_main, hsp, ahb, nfc, ipg, 51*11f68120SShawn Guo per_div, per, csi, fir, csi_div, usb_div_pre, usb_div_post, fir_div_pre, 52*11f68120SShawn Guo fir_div_post, sdhc1_gate, sdhc2_gate, gpt_gate, epit1_gate, epit2_gate, 53*11f68120SShawn Guo iim_gate, ata_gate, sdma_gate, cspi3_gate, rng_gate, uart1_gate, 54*11f68120SShawn Guo uart2_gate, ssi1_gate, i2c1_gate, i2c2_gate, i2c3_gate, hantro_gate, 55*11f68120SShawn Guo mstick1_gate, mstick2_gate, csi_gate, rtc_gate, wdog_gate, pwm_gate, 56*11f68120SShawn Guo sim_gate, ect_gate, usb_gate, kpp_gate, ipu_gate, uart3_gate, 57*11f68120SShawn Guo uart4_gate, uart5_gate, owire_gate, ssi2_gate, cspi1_gate, cspi2_gate, 58*11f68120SShawn Guo gacc_gate, emi_gate, rtic_gate, firi_gate, clk_max 59*11f68120SShawn Guo }; 60*11f68120SShawn Guo 61*11f68120SShawn Guo static struct clk *clk[clk_max]; 62*11f68120SShawn Guo static struct clk_onecell_data clk_data; 63*11f68120SShawn Guo 64*11f68120SShawn Guo int __init mx31_clocks_init(unsigned long fref) 65*11f68120SShawn Guo { 66*11f68120SShawn Guo void __iomem *base; 67*11f68120SShawn Guo struct device_node *np; 68*11f68120SShawn Guo 69*11f68120SShawn Guo base = ioremap(MX31_CCM_BASE_ADDR, SZ_4K); 70*11f68120SShawn Guo BUG_ON(!base); 71*11f68120SShawn Guo 72*11f68120SShawn Guo clk[dummy] = imx_clk_fixed("dummy", 0); 73*11f68120SShawn Guo clk[ckih] = imx_clk_fixed("ckih", fref); 74*11f68120SShawn Guo clk[ckil] = imx_clk_fixed("ckil", 32768); 75*11f68120SShawn Guo clk[mpll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "mpll", "ckih", base + MXC_CCM_MPCTL); 76*11f68120SShawn Guo clk[spll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "spll", "ckih", base + MXC_CCM_SRPCTL); 77*11f68120SShawn Guo clk[upll] = imx_clk_pllv1(IMX_PLLV1_IMX31, "upll", "ckih", base + MXC_CCM_UPCTL); 78*11f68120SShawn Guo clk[mcu_main] = imx_clk_mux("mcu_main", base + MXC_CCM_PMCR0, 31, 1, mcu_main_sel, ARRAY_SIZE(mcu_main_sel)); 79*11f68120SShawn Guo clk[hsp] = imx_clk_divider("hsp", "mcu_main", base + MXC_CCM_PDR0, 11, 3); 80*11f68120SShawn Guo clk[ahb] = imx_clk_divider("ahb", "mcu_main", base + MXC_CCM_PDR0, 3, 3); 81*11f68120SShawn Guo clk[nfc] = imx_clk_divider("nfc", "ahb", base + MXC_CCM_PDR0, 8, 3); 82*11f68120SShawn Guo clk[ipg] = imx_clk_divider("ipg", "ahb", base + MXC_CCM_PDR0, 6, 2); 83*11f68120SShawn Guo clk[per_div] = imx_clk_divider("per_div", "upll", base + MXC_CCM_PDR0, 16, 5); 84*11f68120SShawn Guo clk[per] = imx_clk_mux("per", base + MXC_CCM_CCMR, 24, 1, per_sel, ARRAY_SIZE(per_sel)); 85*11f68120SShawn Guo clk[csi] = imx_clk_mux("csi_sel", base + MXC_CCM_CCMR, 25, 1, csi_sel, ARRAY_SIZE(csi_sel)); 86*11f68120SShawn Guo clk[fir] = imx_clk_mux("fir_sel", base + MXC_CCM_CCMR, 11, 2, fir_sel, ARRAY_SIZE(fir_sel)); 87*11f68120SShawn Guo clk[csi_div] = imx_clk_divider("csi_div", "csi_sel", base + MXC_CCM_PDR0, 23, 9); 88*11f68120SShawn Guo clk[usb_div_pre] = imx_clk_divider("usb_div_pre", "upll", base + MXC_CCM_PDR1, 30, 2); 89*11f68120SShawn Guo clk[usb_div_post] = imx_clk_divider("usb_div_post", "usb_div_pre", base + MXC_CCM_PDR1, 27, 3); 90*11f68120SShawn Guo clk[fir_div_pre] = imx_clk_divider("fir_div_pre", "fir_sel", base + MXC_CCM_PDR1, 24, 3); 91*11f68120SShawn Guo clk[fir_div_post] = imx_clk_divider("fir_div_post", "fir_div_pre", base + MXC_CCM_PDR1, 23, 6); 92*11f68120SShawn Guo clk[sdhc1_gate] = imx_clk_gate2("sdhc1_gate", "per", base + MXC_CCM_CGR0, 0); 93*11f68120SShawn Guo clk[sdhc2_gate] = imx_clk_gate2("sdhc2_gate", "per", base + MXC_CCM_CGR0, 2); 94*11f68120SShawn Guo clk[gpt_gate] = imx_clk_gate2("gpt_gate", "per", base + MXC_CCM_CGR0, 4); 95*11f68120SShawn Guo clk[epit1_gate] = imx_clk_gate2("epit1_gate", "per", base + MXC_CCM_CGR0, 6); 96*11f68120SShawn Guo clk[epit2_gate] = imx_clk_gate2("epit2_gate", "per", base + MXC_CCM_CGR0, 8); 97*11f68120SShawn Guo clk[iim_gate] = imx_clk_gate2("iim_gate", "ipg", base + MXC_CCM_CGR0, 10); 98*11f68120SShawn Guo clk[ata_gate] = imx_clk_gate2("ata_gate", "ipg", base + MXC_CCM_CGR0, 12); 99*11f68120SShawn Guo clk[sdma_gate] = imx_clk_gate2("sdma_gate", "ahb", base + MXC_CCM_CGR0, 14); 100*11f68120SShawn Guo clk[cspi3_gate] = imx_clk_gate2("cspi3_gate", "ipg", base + MXC_CCM_CGR0, 16); 101*11f68120SShawn Guo clk[rng_gate] = imx_clk_gate2("rng_gate", "ipg", base + MXC_CCM_CGR0, 18); 102*11f68120SShawn Guo clk[uart1_gate] = imx_clk_gate2("uart1_gate", "per", base + MXC_CCM_CGR0, 20); 103*11f68120SShawn Guo clk[uart2_gate] = imx_clk_gate2("uart2_gate", "per", base + MXC_CCM_CGR0, 22); 104*11f68120SShawn Guo clk[ssi1_gate] = imx_clk_gate2("ssi1_gate", "spll", base + MXC_CCM_CGR0, 24); 105*11f68120SShawn Guo clk[i2c1_gate] = imx_clk_gate2("i2c1_gate", "per", base + MXC_CCM_CGR0, 26); 106*11f68120SShawn Guo clk[i2c2_gate] = imx_clk_gate2("i2c2_gate", "per", base + MXC_CCM_CGR0, 28); 107*11f68120SShawn Guo clk[i2c3_gate] = imx_clk_gate2("i2c3_gate", "per", base + MXC_CCM_CGR0, 30); 108*11f68120SShawn Guo clk[hantro_gate] = imx_clk_gate2("hantro_gate", "per", base + MXC_CCM_CGR1, 0); 109*11f68120SShawn Guo clk[mstick1_gate] = imx_clk_gate2("mstick1_gate", "per", base + MXC_CCM_CGR1, 2); 110*11f68120SShawn Guo clk[mstick2_gate] = imx_clk_gate2("mstick2_gate", "per", base + MXC_CCM_CGR1, 4); 111*11f68120SShawn Guo clk[csi_gate] = imx_clk_gate2("csi_gate", "csi_div", base + MXC_CCM_CGR1, 6); 112*11f68120SShawn Guo clk[rtc_gate] = imx_clk_gate2("rtc_gate", "ipg", base + MXC_CCM_CGR1, 8); 113*11f68120SShawn Guo clk[wdog_gate] = imx_clk_gate2("wdog_gate", "ipg", base + MXC_CCM_CGR1, 10); 114*11f68120SShawn Guo clk[pwm_gate] = imx_clk_gate2("pwm_gate", "per", base + MXC_CCM_CGR1, 12); 115*11f68120SShawn Guo clk[sim_gate] = imx_clk_gate2("sim_gate", "per", base + MXC_CCM_CGR1, 14); 116*11f68120SShawn Guo clk[ect_gate] = imx_clk_gate2("ect_gate", "per", base + MXC_CCM_CGR1, 16); 117*11f68120SShawn Guo clk[usb_gate] = imx_clk_gate2("usb_gate", "ahb", base + MXC_CCM_CGR1, 18); 118*11f68120SShawn Guo clk[kpp_gate] = imx_clk_gate2("kpp_gate", "ipg", base + MXC_CCM_CGR1, 20); 119*11f68120SShawn Guo clk[ipu_gate] = imx_clk_gate2("ipu_gate", "hsp", base + MXC_CCM_CGR1, 22); 120*11f68120SShawn Guo clk[uart3_gate] = imx_clk_gate2("uart3_gate", "per", base + MXC_CCM_CGR1, 24); 121*11f68120SShawn Guo clk[uart4_gate] = imx_clk_gate2("uart4_gate", "per", base + MXC_CCM_CGR1, 26); 122*11f68120SShawn Guo clk[uart5_gate] = imx_clk_gate2("uart5_gate", "per", base + MXC_CCM_CGR1, 28); 123*11f68120SShawn Guo clk[owire_gate] = imx_clk_gate2("owire_gate", "per", base + MXC_CCM_CGR1, 30); 124*11f68120SShawn Guo clk[ssi2_gate] = imx_clk_gate2("ssi2_gate", "spll", base + MXC_CCM_CGR2, 0); 125*11f68120SShawn Guo clk[cspi1_gate] = imx_clk_gate2("cspi1_gate", "ipg", base + MXC_CCM_CGR2, 2); 126*11f68120SShawn Guo clk[cspi2_gate] = imx_clk_gate2("cspi2_gate", "ipg", base + MXC_CCM_CGR2, 4); 127*11f68120SShawn Guo clk[gacc_gate] = imx_clk_gate2("gacc_gate", "per", base + MXC_CCM_CGR2, 6); 128*11f68120SShawn Guo clk[emi_gate] = imx_clk_gate2("emi_gate", "ahb", base + MXC_CCM_CGR2, 8); 129*11f68120SShawn Guo clk[rtic_gate] = imx_clk_gate2("rtic_gate", "ahb", base + MXC_CCM_CGR2, 10); 130*11f68120SShawn Guo clk[firi_gate] = imx_clk_gate2("firi_gate", "upll", base+MXC_CCM_CGR2, 12); 131*11f68120SShawn Guo 132*11f68120SShawn Guo imx_check_clocks(clk, ARRAY_SIZE(clk)); 133*11f68120SShawn Guo 134*11f68120SShawn Guo np = of_find_compatible_node(NULL, NULL, "fsl,imx31-ccm"); 135*11f68120SShawn Guo 136*11f68120SShawn Guo if (np) { 137*11f68120SShawn Guo clk_data.clks = clk; 138*11f68120SShawn Guo clk_data.clk_num = ARRAY_SIZE(clk); 139*11f68120SShawn Guo of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data); 140*11f68120SShawn Guo } 141*11f68120SShawn Guo 142*11f68120SShawn Guo clk_register_clkdev(clk[gpt_gate], "per", "imx-gpt.0"); 143*11f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx-gpt.0"); 144*11f68120SShawn Guo clk_register_clkdev(clk[cspi1_gate], NULL, "imx31-cspi.0"); 145*11f68120SShawn Guo clk_register_clkdev(clk[cspi2_gate], NULL, "imx31-cspi.1"); 146*11f68120SShawn Guo clk_register_clkdev(clk[cspi3_gate], NULL, "imx31-cspi.2"); 147*11f68120SShawn Guo clk_register_clkdev(clk[pwm_gate], "pwm", NULL); 148*11f68120SShawn Guo clk_register_clkdev(clk[wdog_gate], NULL, "imx2-wdt.0"); 149*11f68120SShawn Guo clk_register_clkdev(clk[rtc_gate], NULL, "imx21-rtc"); 150*11f68120SShawn Guo clk_register_clkdev(clk[epit1_gate], "epit", NULL); 151*11f68120SShawn Guo clk_register_clkdev(clk[epit2_gate], "epit", NULL); 152*11f68120SShawn Guo clk_register_clkdev(clk[nfc], NULL, "imx27-nand.0"); 153*11f68120SShawn Guo clk_register_clkdev(clk[ipu_gate], NULL, "ipu-core"); 154*11f68120SShawn Guo clk_register_clkdev(clk[ipu_gate], NULL, "mx3_sdc_fb"); 155*11f68120SShawn Guo clk_register_clkdev(clk[kpp_gate], NULL, "imx-keypad"); 156*11f68120SShawn Guo clk_register_clkdev(clk[usb_div_post], "per", "mxc-ehci.0"); 157*11f68120SShawn Guo clk_register_clkdev(clk[usb_gate], "ahb", "mxc-ehci.0"); 158*11f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "mxc-ehci.0"); 159*11f68120SShawn Guo clk_register_clkdev(clk[usb_div_post], "per", "mxc-ehci.1"); 160*11f68120SShawn Guo clk_register_clkdev(clk[usb_gate], "ahb", "mxc-ehci.1"); 161*11f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "mxc-ehci.1"); 162*11f68120SShawn Guo clk_register_clkdev(clk[usb_div_post], "per", "mxc-ehci.2"); 163*11f68120SShawn Guo clk_register_clkdev(clk[usb_gate], "ahb", "mxc-ehci.2"); 164*11f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "mxc-ehci.2"); 165*11f68120SShawn Guo clk_register_clkdev(clk[usb_div_post], "per", "imx-udc-mx27"); 166*11f68120SShawn Guo clk_register_clkdev(clk[usb_gate], "ahb", "imx-udc-mx27"); 167*11f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx-udc-mx27"); 168*11f68120SShawn Guo clk_register_clkdev(clk[csi_gate], NULL, "mx3-camera.0"); 169*11f68120SShawn Guo /* i.mx31 has the i.mx21 type uart */ 170*11f68120SShawn Guo clk_register_clkdev(clk[uart1_gate], "per", "imx21-uart.0"); 171*11f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx21-uart.0"); 172*11f68120SShawn Guo clk_register_clkdev(clk[uart2_gate], "per", "imx21-uart.1"); 173*11f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx21-uart.1"); 174*11f68120SShawn Guo clk_register_clkdev(clk[uart3_gate], "per", "imx21-uart.2"); 175*11f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx21-uart.2"); 176*11f68120SShawn Guo clk_register_clkdev(clk[uart4_gate], "per", "imx21-uart.3"); 177*11f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx21-uart.3"); 178*11f68120SShawn Guo clk_register_clkdev(clk[uart5_gate], "per", "imx21-uart.4"); 179*11f68120SShawn Guo clk_register_clkdev(clk[ipg], "ipg", "imx21-uart.4"); 180*11f68120SShawn Guo clk_register_clkdev(clk[i2c1_gate], NULL, "imx21-i2c.0"); 181*11f68120SShawn Guo clk_register_clkdev(clk[i2c2_gate], NULL, "imx21-i2c.1"); 182*11f68120SShawn Guo clk_register_clkdev(clk[i2c3_gate], NULL, "imx21-i2c.2"); 183*11f68120SShawn Guo clk_register_clkdev(clk[owire_gate], NULL, "mxc_w1.0"); 184*11f68120SShawn Guo clk_register_clkdev(clk[sdhc1_gate], NULL, "imx31-mmc.0"); 185*11f68120SShawn Guo clk_register_clkdev(clk[sdhc2_gate], NULL, "imx31-mmc.1"); 186*11f68120SShawn Guo clk_register_clkdev(clk[ssi1_gate], NULL, "imx-ssi.0"); 187*11f68120SShawn Guo clk_register_clkdev(clk[ssi2_gate], NULL, "imx-ssi.1"); 188*11f68120SShawn Guo clk_register_clkdev(clk[firi_gate], "firi", NULL); 189*11f68120SShawn Guo clk_register_clkdev(clk[ata_gate], NULL, "pata_imx"); 190*11f68120SShawn Guo clk_register_clkdev(clk[rtic_gate], "rtic", NULL); 191*11f68120SShawn Guo clk_register_clkdev(clk[rng_gate], NULL, "mxc_rnga"); 192*11f68120SShawn Guo clk_register_clkdev(clk[sdma_gate], NULL, "imx31-sdma"); 193*11f68120SShawn Guo clk_register_clkdev(clk[iim_gate], "iim", NULL); 194*11f68120SShawn Guo 195*11f68120SShawn Guo clk_set_parent(clk[csi], clk[upll]); 196*11f68120SShawn Guo clk_prepare_enable(clk[emi_gate]); 197*11f68120SShawn Guo clk_prepare_enable(clk[iim_gate]); 198*11f68120SShawn Guo mx31_revision(); 199*11f68120SShawn Guo clk_disable_unprepare(clk[iim_gate]); 200*11f68120SShawn Guo 201*11f68120SShawn Guo mxc_timer_init(MX31_GPT1_BASE_ADDR, MX31_INT_GPT); 202*11f68120SShawn Guo 203*11f68120SShawn Guo return 0; 204*11f68120SShawn Guo } 205*11f68120SShawn Guo 206*11f68120SShawn Guo int __init mx31_clocks_init_dt(void) 207*11f68120SShawn Guo { 208*11f68120SShawn Guo struct device_node *np; 209*11f68120SShawn Guo u32 fref = 26000000; /* default */ 210*11f68120SShawn Guo 211*11f68120SShawn Guo for_each_compatible_node(np, NULL, "fixed-clock") { 212*11f68120SShawn Guo if (!of_device_is_compatible(np, "fsl,imx-osc26m")) 213*11f68120SShawn Guo continue; 214*11f68120SShawn Guo 215*11f68120SShawn Guo if (!of_property_read_u32(np, "clock-frequency", &fref)) 216*11f68120SShawn Guo break; 217*11f68120SShawn Guo } 218*11f68120SShawn Guo 219*11f68120SShawn Guo return mx31_clocks_init(fref); 220*11f68120SShawn Guo } 221