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; 44*f48d947aSStephen Boyd struct clk_hw_onecell_data clk_data; 45631c5347SAlexander Shiyan }; 46631c5347SAlexander Shiyan 47631c5347SAlexander Shiyan static struct clps711x_clk * __init _clps711x_clk_init(void __iomem *base, 48631c5347SAlexander Shiyan u32 fref) 49631c5347SAlexander Shiyan { 50631c5347SAlexander Shiyan u32 tmp, f_cpu, f_pll, f_bus, f_tim, f_pwm, f_spi; 51631c5347SAlexander Shiyan struct clps711x_clk *clps711x_clk; 52631c5347SAlexander Shiyan unsigned i; 53631c5347SAlexander Shiyan 54631c5347SAlexander Shiyan if (!base) 55631c5347SAlexander Shiyan return ERR_PTR(-ENOMEM); 56631c5347SAlexander Shiyan 57*f48d947aSStephen Boyd clps711x_clk = kzalloc(sizeof(*clps711x_clk) + 58*f48d947aSStephen Boyd sizeof(*clps711x_clk->clk_data.hws) * CLPS711X_CLK_MAX, 59*f48d947aSStephen Boyd GFP_KERNEL); 60631c5347SAlexander Shiyan if (!clps711x_clk) 61631c5347SAlexander Shiyan return ERR_PTR(-ENOMEM); 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 110*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_DUMMY] = 111*f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "dummy", NULL, 0, 0); 112*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_CPU] = 113*f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "cpu", NULL, 0, f_cpu); 114*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_BUS] = 115*f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "bus", NULL, 0, f_bus); 116*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_PLL] = 117*f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "pll", NULL, 0, f_pll); 118*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMERREF] = 119*f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "timer_ref", NULL, 0, f_tim); 120*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMER1] = 121*f48d947aSStephen 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); 124*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMER2] = 125*f48d947aSStephen 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); 128*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_PWM] = 129*f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "pwm", NULL, 0, f_pwm); 130*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_SPIREF] = 131*f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "spi_ref", NULL, 0, f_spi); 132*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_SPI] = 133*f48d947aSStephen 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); 136*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_UART] = 137*f48d947aSStephen Boyd clk_hw_register_fixed_factor(NULL, "uart", "bus", 0, 1, 10); 138*f48d947aSStephen Boyd clps711x_clk->clk_data.hws[CLPS711X_CLK_TICK] = 139*f48d947aSStephen Boyd clk_hw_register_fixed_rate(NULL, "tick", NULL, 0, 64); 140631c5347SAlexander Shiyan for (i = 0; i < CLPS711X_CLK_MAX; i++) 141*f48d947aSStephen Boyd if (IS_ERR(clps711x_clk->clk_data.hws[i])) 142631c5347SAlexander Shiyan pr_err("clk %i: register failed with %ld\n", 143*f48d947aSStephen Boyd i, PTR_ERR(clps711x_clk->clk_data.hws[i])); 144631c5347SAlexander Shiyan 145631c5347SAlexander Shiyan return clps711x_clk; 146631c5347SAlexander Shiyan } 147631c5347SAlexander Shiyan 148631c5347SAlexander Shiyan void __init clps711x_clk_init(void __iomem *base) 149631c5347SAlexander Shiyan { 150631c5347SAlexander Shiyan struct clps711x_clk *clps711x_clk; 151631c5347SAlexander Shiyan 152631c5347SAlexander Shiyan clps711x_clk = _clps711x_clk_init(base, 73728000); 153631c5347SAlexander Shiyan 154631c5347SAlexander Shiyan BUG_ON(IS_ERR(clps711x_clk)); 155631c5347SAlexander Shiyan 156631c5347SAlexander Shiyan /* Clocksource */ 157*f48d947aSStephen Boyd clk_hw_register_clkdev(clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMER1], 158631c5347SAlexander Shiyan NULL, "clps711x-timer.0"); 159*f48d947aSStephen Boyd clk_hw_register_clkdev(clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMER2], 160631c5347SAlexander Shiyan NULL, "clps711x-timer.1"); 161631c5347SAlexander Shiyan 162631c5347SAlexander Shiyan /* Drivers */ 163*f48d947aSStephen Boyd clk_hw_register_clkdev(clps711x_clk->clk_data.hws[CLPS711X_CLK_PWM], 164631c5347SAlexander Shiyan NULL, "clps711x-pwm"); 165*f48d947aSStephen Boyd clk_hw_register_clkdev(clps711x_clk->clk_data.hws[CLPS711X_CLK_UART], 166631c5347SAlexander Shiyan NULL, "clps711x-uart.0"); 167*f48d947aSStephen Boyd clk_hw_register_clkdev(clps711x_clk->clk_data.hws[CLPS711X_CLK_UART], 168631c5347SAlexander Shiyan NULL, "clps711x-uart.1"); 169631c5347SAlexander Shiyan } 170631c5347SAlexander Shiyan 171631c5347SAlexander Shiyan #ifdef CONFIG_OF 172631c5347SAlexander Shiyan static void __init clps711x_clk_init_dt(struct device_node *np) 173631c5347SAlexander Shiyan { 174631c5347SAlexander Shiyan void __iomem *base = of_iomap(np, 0); 175631c5347SAlexander Shiyan struct clps711x_clk *clps711x_clk; 176631c5347SAlexander Shiyan u32 fref = 0; 177631c5347SAlexander Shiyan 178631c5347SAlexander Shiyan WARN_ON(of_property_read_u32(np, "startup-frequency", &fref)); 179631c5347SAlexander Shiyan 180631c5347SAlexander Shiyan clps711x_clk = _clps711x_clk_init(base, fref); 181631c5347SAlexander Shiyan BUG_ON(IS_ERR(clps711x_clk)); 182631c5347SAlexander Shiyan 183*f48d947aSStephen Boyd clps711x_clk->clk_data.num = CLPS711X_CLK_MAX; 184*f48d947aSStephen Boyd of_clk_add_hw_provider(np, of_clk_hw_onecell_get, 185631c5347SAlexander Shiyan &clps711x_clk->clk_data); 186631c5347SAlexander Shiyan } 187893b7798SAlexander Shiyan CLK_OF_DECLARE(clps711x, "cirrus,ep7209-clk", clps711x_clk_init_dt); 188631c5347SAlexander Shiyan #endif 189