1631c5347SAlexander Shiyan /* 2631c5347SAlexander Shiyan * Cirrus Logic CLPS711X CLK driver 3631c5347SAlexander Shiyan * 4631c5347SAlexander Shiyan * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru> 5631c5347SAlexander Shiyan * 6631c5347SAlexander Shiyan * This program is free software; you can redistribute it and/or modify 7631c5347SAlexander Shiyan * it under the terms of the GNU General Public License as published by 8631c5347SAlexander Shiyan * the Free Software Foundation; either version 2 of the License, or 9631c5347SAlexander Shiyan * (at your option) any later version. 10631c5347SAlexander Shiyan */ 11631c5347SAlexander Shiyan 12631c5347SAlexander Shiyan #include <linux/clk-provider.h> 13631c5347SAlexander Shiyan #include <linux/clkdev.h> 14631c5347SAlexander Shiyan #include <linux/io.h> 15631c5347SAlexander Shiyan #include <linux/ioport.h> 16631c5347SAlexander Shiyan #include <linux/of_address.h> 17631c5347SAlexander Shiyan #include <linux/slab.h> 18631c5347SAlexander Shiyan #include <linux/mfd/syscon/clps711x.h> 19631c5347SAlexander Shiyan 20631c5347SAlexander Shiyan #include <dt-bindings/clock/clps711x-clock.h> 21631c5347SAlexander Shiyan 22631c5347SAlexander Shiyan #define CLPS711X_SYSCON1 (0x0100) 23631c5347SAlexander Shiyan #define CLPS711X_SYSCON2 (0x1100) 24631c5347SAlexander Shiyan #define CLPS711X_SYSFLG2 (CLPS711X_SYSCON2 + SYSFLG_OFFSET) 25631c5347SAlexander Shiyan #define CLPS711X_PLLR (0xa5a8) 26631c5347SAlexander Shiyan 27631c5347SAlexander Shiyan #define CLPS711X_EXT_FREQ (13000000) 28631c5347SAlexander Shiyan #define CLPS711X_OSC_FREQ (3686400) 29631c5347SAlexander Shiyan 30631c5347SAlexander Shiyan static const struct clk_div_table spi_div_table[] = { 31631c5347SAlexander Shiyan { .val = 0, .div = 32, }, 32631c5347SAlexander Shiyan { .val = 1, .div = 8, }, 33631c5347SAlexander Shiyan { .val = 2, .div = 2, }, 34631c5347SAlexander Shiyan { .val = 3, .div = 1, }, 35631c5347SAlexander Shiyan }; 36631c5347SAlexander Shiyan 37631c5347SAlexander Shiyan static const struct clk_div_table timer_div_table[] = { 38631c5347SAlexander Shiyan { .val = 0, .div = 256, }, 39631c5347SAlexander Shiyan { .val = 1, .div = 1, }, 40631c5347SAlexander Shiyan }; 41631c5347SAlexander Shiyan 42631c5347SAlexander Shiyan struct clps711x_clk { 43631c5347SAlexander Shiyan spinlock_t lock; 44f48d947aSStephen Boyd struct clk_hw_onecell_data clk_data; 45631c5347SAlexander Shiyan }; 46631c5347SAlexander Shiyan 47*31cc9e09SAlexander Shiyan static void __init clps711x_clk_init_dt(struct device_node *np) 48631c5347SAlexander Shiyan { 49*31cc9e09SAlexander Shiyan u32 tmp, f_cpu, f_pll, f_bus, f_tim, f_pwm, f_spi, fref = 0; 50631c5347SAlexander Shiyan struct clps711x_clk *clps711x_clk; 51*31cc9e09SAlexander Shiyan void __iomem *base; 52631c5347SAlexander Shiyan 53*31cc9e09SAlexander Shiyan WARN_ON(of_property_read_u32(np, "startup-frequency", &fref)); 54*31cc9e09SAlexander Shiyan 55*31cc9e09SAlexander Shiyan base = of_iomap(np, 0); 56*31cc9e09SAlexander Shiyan BUG_ON(!base); 57631c5347SAlexander Shiyan 58acafe7e3SKees Cook clps711x_clk = kzalloc(struct_size(clps711x_clk, clk_data.hws, 59acafe7e3SKees Cook CLPS711X_CLK_MAX), 60f48d947aSStephen Boyd GFP_KERNEL); 61*31cc9e09SAlexander Shiyan BUG_ON(!clps711x_clk); 62631c5347SAlexander Shiyan 63631c5347SAlexander Shiyan spin_lock_init(&clps711x_clk->lock); 64631c5347SAlexander Shiyan 65631c5347SAlexander Shiyan /* Read PLL multiplier value and sanity check */ 66631c5347SAlexander Shiyan tmp = readl(base + CLPS711X_PLLR) >> 24; 67631c5347SAlexander Shiyan if (((tmp >= 10) && (tmp <= 50)) || !fref) 68631c5347SAlexander Shiyan f_pll = DIV_ROUND_UP(CLPS711X_OSC_FREQ * tmp, 2); 69631c5347SAlexander Shiyan else 70631c5347SAlexander Shiyan f_pll = fref; 71631c5347SAlexander Shiyan 72631c5347SAlexander Shiyan tmp = readl(base + CLPS711X_SYSFLG2); 73631c5347SAlexander Shiyan if (tmp & SYSFLG2_CKMODE) { 74631c5347SAlexander Shiyan f_cpu = CLPS711X_EXT_FREQ; 75631c5347SAlexander Shiyan f_bus = CLPS711X_EXT_FREQ; 76631c5347SAlexander Shiyan f_spi = DIV_ROUND_CLOSEST(CLPS711X_EXT_FREQ, 96); 77631c5347SAlexander Shiyan f_pll = 0; 78631c5347SAlexander Shiyan f_pwm = DIV_ROUND_CLOSEST(CLPS711X_EXT_FREQ, 128); 79631c5347SAlexander Shiyan } else { 80631c5347SAlexander Shiyan f_cpu = f_pll; 81631c5347SAlexander Shiyan if (f_cpu > 36864000) 82631c5347SAlexander Shiyan f_bus = DIV_ROUND_UP(f_cpu, 2); 83631c5347SAlexander Shiyan else 84631c5347SAlexander Shiyan f_bus = 36864000 / 2; 85631c5347SAlexander Shiyan f_spi = DIV_ROUND_CLOSEST(f_cpu, 576); 86631c5347SAlexander Shiyan f_pwm = DIV_ROUND_CLOSEST(f_cpu, 768); 87631c5347SAlexander Shiyan } 88631c5347SAlexander Shiyan 89631c5347SAlexander Shiyan if (tmp & SYSFLG2_CKMODE) { 90631c5347SAlexander Shiyan if (readl(base + CLPS711X_SYSCON2) & SYSCON2_OSTB) 91631c5347SAlexander Shiyan f_tim = DIV_ROUND_CLOSEST(CLPS711X_EXT_FREQ, 26); 92631c5347SAlexander Shiyan else 93631c5347SAlexander Shiyan f_tim = DIV_ROUND_CLOSEST(CLPS711X_EXT_FREQ, 24); 94631c5347SAlexander Shiyan } else 95631c5347SAlexander Shiyan f_tim = DIV_ROUND_CLOSEST(f_cpu, 144); 96631c5347SAlexander Shiyan 97631c5347SAlexander Shiyan tmp = readl(base + CLPS711X_SYSCON1); 98631c5347SAlexander Shiyan /* Timer1 in free running mode. 99631c5347SAlexander Shiyan * Counter will wrap around to 0xffff when it underflows 100631c5347SAlexander Shiyan * and will continue to count down. 101631c5347SAlexander Shiyan */ 102631c5347SAlexander Shiyan tmp &= ~(SYSCON1_TC1M | SYSCON1_TC1S); 103631c5347SAlexander Shiyan /* Timer2 in prescale mode. 104631c5347SAlexander Shiyan * Value writen is automatically re-loaded when 105631c5347SAlexander Shiyan * the counter underflows. 106631c5347SAlexander Shiyan */ 107631c5347SAlexander Shiyan tmp |= SYSCON1_TC2M | SYSCON1_TC2S; 108631c5347SAlexander Shiyan writel(tmp, base + CLPS711X_SYSCON1); 109631c5347SAlexander Shiyan 110f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_DUMMY] = 111f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "dummy", NULL, 0, 0); 112f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_CPU] = 113f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "cpu", NULL, 0, f_cpu); 114f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_BUS] = 115f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "bus", NULL, 0, f_bus); 116f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_PLL] = 117f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "pll", NULL, 0, f_pll); 118f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMERREF] = 119f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "timer_ref", NULL, 0, f_tim); 120f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMER1] = 121f48d947aSStephen Boyd clk_hw_register_divider_table(NULL, "timer1", "timer_ref", 0, 122631c5347SAlexander Shiyan base + CLPS711X_SYSCON1, 5, 1, 0, 123631c5347SAlexander Shiyan timer_div_table, &clps711x_clk->lock); 124f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMER2] = 125f48d947aSStephen Boyd clk_hw_register_divider_table(NULL, "timer2", "timer_ref", 0, 126631c5347SAlexander Shiyan base + CLPS711X_SYSCON1, 7, 1, 0, 127631c5347SAlexander Shiyan timer_div_table, &clps711x_clk->lock); 128f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_PWM] = 129f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "pwm", NULL, 0, f_pwm); 130f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_SPIREF] = 131f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "spi_ref", NULL, 0, f_spi); 132f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_SPI] = 133f48d947aSStephen Boyd clk_hw_register_divider_table(NULL, "spi", "spi_ref", 0, 134631c5347SAlexander Shiyan base + CLPS711X_SYSCON1, 16, 2, 0, 135631c5347SAlexander Shiyan spi_div_table, &clps711x_clk->lock); 136f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_UART] = 137f48d947aSStephen Boyd clk_hw_register_fixed_factor(NULL, "uart", "bus", 0, 1, 10); 138f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_TICK] = 139f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "tick", NULL, 0, 64); 140*31cc9e09SAlexander Shiyan for (tmp = 0; tmp < CLPS711X_CLK_MAX; tmp++) 141*31cc9e09SAlexander Shiyan if (IS_ERR(clps711x_clk->clk_data.hws[tmp])) 142631c5347SAlexander Shiyan pr_err("clk %i: register failed with %ld\n", 143*31cc9e09SAlexander Shiyan tmp, PTR_ERR(clps711x_clk->clk_data.hws[tmp])); 144631c5347SAlexander Shiyan 145f48d947aSStephen Boyd clps711x_clk->clk_data.num = CLPS711X_CLK_MAX; 146f48d947aSStephen Boyd of_clk_add_hw_provider(np, of_clk_hw_onecell_get, 147631c5347SAlexander Shiyan &clps711x_clk->clk_data); 148631c5347SAlexander Shiyan } 149893b7798SAlexander Shiyan CLK_OF_DECLARE(clps711x, "cirrus,ep7209-clk", clps711x_clk_init_dt); 150