12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 20ad6125bSBoris BREZILLON /* 30ad6125bSBoris BREZILLON * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com> 40ad6125bSBoris BREZILLON */ 50ad6125bSBoris BREZILLON 60ad6125bSBoris BREZILLON #include <linux/clk-provider.h> 70ad6125bSBoris BREZILLON #include <linux/clkdev.h> 80ad6125bSBoris BREZILLON #include <linux/clk/at91_pmc.h> 90ad6125bSBoris BREZILLON #include <linux/of.h> 10863a81c3SBoris Brezillon #include <linux/mfd/syscon.h> 11b3b02eacSAlexandre Belloni #include <linux/platform_device.h> 121bdf0232SBoris Brezillon #include <linux/regmap.h> 13b3b02eacSAlexandre Belloni #include <linux/syscore_ops.h> 140ad6125bSBoris BREZILLON 150ad6125bSBoris BREZILLON #include <asm/proc-fns.h> 160ad6125bSBoris BREZILLON 17d387ff54SAlexandre Belloni #include <dt-bindings/clock/at91.h> 18d387ff54SAlexandre Belloni 190ad6125bSBoris BREZILLON #include "pmc.h" 200ad6125bSBoris BREZILLON 21b3b02eacSAlexandre Belloni #define PMC_MAX_IDS 128 2213967beaSRomain Izard #define PMC_MAX_PCKS 8 23b3b02eacSAlexandre Belloni 240ad6125bSBoris BREZILLON int of_at91_get_clk_range(struct device_node *np, const char *propname, 250ad6125bSBoris BREZILLON struct clk_range *range) 260ad6125bSBoris BREZILLON { 270ad6125bSBoris BREZILLON u32 min, max; 280ad6125bSBoris BREZILLON int ret; 290ad6125bSBoris BREZILLON 300ad6125bSBoris BREZILLON ret = of_property_read_u32_index(np, propname, 0, &min); 310ad6125bSBoris BREZILLON if (ret) 320ad6125bSBoris BREZILLON return ret; 330ad6125bSBoris BREZILLON 340ad6125bSBoris BREZILLON ret = of_property_read_u32_index(np, propname, 1, &max); 350ad6125bSBoris BREZILLON if (ret) 360ad6125bSBoris BREZILLON return ret; 370ad6125bSBoris BREZILLON 380ad6125bSBoris BREZILLON if (range) { 390ad6125bSBoris BREZILLON range->min = min; 400ad6125bSBoris BREZILLON range->max = max; 410ad6125bSBoris BREZILLON } 420ad6125bSBoris BREZILLON 430ad6125bSBoris BREZILLON return 0; 440ad6125bSBoris BREZILLON } 450ad6125bSBoris BREZILLON EXPORT_SYMBOL_GPL(of_at91_get_clk_range); 46b3b02eacSAlexandre Belloni 47d387ff54SAlexandre Belloni struct clk_hw *of_clk_hw_pmc_get(struct of_phandle_args *clkspec, void *data) 48d387ff54SAlexandre Belloni { 49d387ff54SAlexandre Belloni unsigned int type = clkspec->args[0]; 50d387ff54SAlexandre Belloni unsigned int idx = clkspec->args[1]; 51d387ff54SAlexandre Belloni struct pmc_data *pmc_data = data; 52d387ff54SAlexandre Belloni 53d387ff54SAlexandre Belloni switch (type) { 54d387ff54SAlexandre Belloni case PMC_TYPE_CORE: 55d387ff54SAlexandre Belloni if (idx < pmc_data->ncore) 56d387ff54SAlexandre Belloni return pmc_data->chws[idx]; 57d387ff54SAlexandre Belloni break; 58d387ff54SAlexandre Belloni case PMC_TYPE_SYSTEM: 59d387ff54SAlexandre Belloni if (idx < pmc_data->nsystem) 60d387ff54SAlexandre Belloni return pmc_data->shws[idx]; 61d387ff54SAlexandre Belloni break; 62d387ff54SAlexandre Belloni case PMC_TYPE_PERIPHERAL: 63d387ff54SAlexandre Belloni if (idx < pmc_data->nperiph) 64d387ff54SAlexandre Belloni return pmc_data->phws[idx]; 65d387ff54SAlexandre Belloni break; 66d387ff54SAlexandre Belloni case PMC_TYPE_GCK: 67d387ff54SAlexandre Belloni if (idx < pmc_data->ngck) 68d387ff54SAlexandre Belloni return pmc_data->ghws[idx]; 69d387ff54SAlexandre Belloni break; 70d387ff54SAlexandre Belloni default: 71d387ff54SAlexandre Belloni break; 72d387ff54SAlexandre Belloni } 73d387ff54SAlexandre Belloni 74d387ff54SAlexandre Belloni pr_err("%s: invalid type (%u) or index (%u)\n", __func__, type, idx); 75d387ff54SAlexandre Belloni 76d387ff54SAlexandre Belloni return ERR_PTR(-EINVAL); 77d387ff54SAlexandre Belloni } 78d387ff54SAlexandre Belloni 79b00cd8e4SAlexandre Belloni void pmc_data_free(struct pmc_data *pmc_data) 80b00cd8e4SAlexandre Belloni { 81b00cd8e4SAlexandre Belloni kfree(pmc_data->chws); 82b00cd8e4SAlexandre Belloni kfree(pmc_data->shws); 83b00cd8e4SAlexandre Belloni kfree(pmc_data->phws); 84b00cd8e4SAlexandre Belloni kfree(pmc_data->ghws); 85b00cd8e4SAlexandre Belloni } 86b00cd8e4SAlexandre Belloni 87b00cd8e4SAlexandre Belloni struct pmc_data *pmc_data_allocate(unsigned int ncore, unsigned int nsystem, 88b00cd8e4SAlexandre Belloni unsigned int nperiph, unsigned int ngck) 89b00cd8e4SAlexandre Belloni { 90b00cd8e4SAlexandre Belloni struct pmc_data *pmc_data = kzalloc(sizeof(*pmc_data), GFP_KERNEL); 91b00cd8e4SAlexandre Belloni 92b00cd8e4SAlexandre Belloni if (!pmc_data) 93b00cd8e4SAlexandre Belloni return NULL; 94b00cd8e4SAlexandre Belloni 95b00cd8e4SAlexandre Belloni pmc_data->ncore = ncore; 96b00cd8e4SAlexandre Belloni pmc_data->chws = kcalloc(ncore, sizeof(struct clk_hw *), GFP_KERNEL); 97b00cd8e4SAlexandre Belloni if (!pmc_data->chws) 98b00cd8e4SAlexandre Belloni goto err; 99b00cd8e4SAlexandre Belloni 100b00cd8e4SAlexandre Belloni pmc_data->nsystem = nsystem; 101b00cd8e4SAlexandre Belloni pmc_data->shws = kcalloc(nsystem, sizeof(struct clk_hw *), GFP_KERNEL); 102b00cd8e4SAlexandre Belloni if (!pmc_data->shws) 103b00cd8e4SAlexandre Belloni goto err; 104b00cd8e4SAlexandre Belloni 105b00cd8e4SAlexandre Belloni pmc_data->nperiph = nperiph; 106b00cd8e4SAlexandre Belloni pmc_data->phws = kcalloc(nperiph, sizeof(struct clk_hw *), GFP_KERNEL); 107b00cd8e4SAlexandre Belloni if (!pmc_data->phws) 108b00cd8e4SAlexandre Belloni goto err; 109b00cd8e4SAlexandre Belloni 110b00cd8e4SAlexandre Belloni pmc_data->ngck = ngck; 111b00cd8e4SAlexandre Belloni pmc_data->ghws = kcalloc(ngck, sizeof(struct clk_hw *), GFP_KERNEL); 112b00cd8e4SAlexandre Belloni if (!pmc_data->ghws) 113b00cd8e4SAlexandre Belloni goto err; 114b00cd8e4SAlexandre Belloni 115b00cd8e4SAlexandre Belloni return pmc_data; 116b00cd8e4SAlexandre Belloni 117b00cd8e4SAlexandre Belloni err: 118b00cd8e4SAlexandre Belloni pmc_data_free(pmc_data); 119b00cd8e4SAlexandre Belloni 120b00cd8e4SAlexandre Belloni return NULL; 121b00cd8e4SAlexandre Belloni } 122b00cd8e4SAlexandre Belloni 123b3b02eacSAlexandre Belloni #ifdef CONFIG_PM 124b3b02eacSAlexandre Belloni static struct regmap *pmcreg; 125b3b02eacSAlexandre Belloni 126b3b02eacSAlexandre Belloni static u8 registered_ids[PMC_MAX_IDS]; 12713967beaSRomain Izard static u8 registered_pcks[PMC_MAX_PCKS]; 128b3b02eacSAlexandre Belloni 129b3b02eacSAlexandre Belloni static struct 130b3b02eacSAlexandre Belloni { 131b3b02eacSAlexandre Belloni u32 scsr; 132b3b02eacSAlexandre Belloni u32 pcsr0; 133b3b02eacSAlexandre Belloni u32 uckr; 134b3b02eacSAlexandre Belloni u32 mor; 135b3b02eacSAlexandre Belloni u32 mcfr; 136b3b02eacSAlexandre Belloni u32 pllar; 137b3b02eacSAlexandre Belloni u32 mckr; 138b3b02eacSAlexandre Belloni u32 usb; 139b3b02eacSAlexandre Belloni u32 imr; 140b3b02eacSAlexandre Belloni u32 pcsr1; 141b3b02eacSAlexandre Belloni u32 pcr[PMC_MAX_IDS]; 142b3b02eacSAlexandre Belloni u32 audio_pll0; 143b3b02eacSAlexandre Belloni u32 audio_pll1; 14413967beaSRomain Izard u32 pckr[PMC_MAX_PCKS]; 145b3b02eacSAlexandre Belloni } pmc_cache; 146b3b02eacSAlexandre Belloni 14713967beaSRomain Izard /* 14813967beaSRomain Izard * As Peripheral ID 0 is invalid on AT91 chips, the identifier is stored 14913967beaSRomain Izard * without alteration in the table, and 0 is for unused clocks. 15013967beaSRomain Izard */ 151b3b02eacSAlexandre Belloni void pmc_register_id(u8 id) 152b3b02eacSAlexandre Belloni { 153b3b02eacSAlexandre Belloni int i; 154b3b02eacSAlexandre Belloni 155b3b02eacSAlexandre Belloni for (i = 0; i < PMC_MAX_IDS; i++) { 156b3b02eacSAlexandre Belloni if (registered_ids[i] == 0) { 157b3b02eacSAlexandre Belloni registered_ids[i] = id; 158b3b02eacSAlexandre Belloni break; 159b3b02eacSAlexandre Belloni } 160b3b02eacSAlexandre Belloni if (registered_ids[i] == id) 161b3b02eacSAlexandre Belloni break; 162b3b02eacSAlexandre Belloni } 163b3b02eacSAlexandre Belloni } 164b3b02eacSAlexandre Belloni 16513967beaSRomain Izard /* 16613967beaSRomain Izard * As Programmable Clock 0 is valid on AT91 chips, there is an offset 16713967beaSRomain Izard * of 1 between the stored value and the real clock ID. 16813967beaSRomain Izard */ 16913967beaSRomain Izard void pmc_register_pck(u8 pck) 17013967beaSRomain Izard { 17113967beaSRomain Izard int i; 17213967beaSRomain Izard 17313967beaSRomain Izard for (i = 0; i < PMC_MAX_PCKS; i++) { 17413967beaSRomain Izard if (registered_pcks[i] == 0) { 17513967beaSRomain Izard registered_pcks[i] = pck + 1; 17613967beaSRomain Izard break; 17713967beaSRomain Izard } 17813967beaSRomain Izard if (registered_pcks[i] == (pck + 1)) 17913967beaSRomain Izard break; 18013967beaSRomain Izard } 18113967beaSRomain Izard } 18213967beaSRomain Izard 183b3b02eacSAlexandre Belloni static int pmc_suspend(void) 184b3b02eacSAlexandre Belloni { 185b3b02eacSAlexandre Belloni int i; 18613967beaSRomain Izard u8 num; 187b3b02eacSAlexandre Belloni 1883c6fad25SRomain Izard regmap_read(pmcreg, AT91_PMC_SCSR, &pmc_cache.scsr); 189b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_PMC_PCSR, &pmc_cache.pcsr0); 190b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_CKGR_UCKR, &pmc_cache.uckr); 191b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_CKGR_MOR, &pmc_cache.mor); 192b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_CKGR_MCFR, &pmc_cache.mcfr); 193b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_CKGR_PLLAR, &pmc_cache.pllar); 194b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_PMC_MCKR, &pmc_cache.mckr); 195b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_PMC_USB, &pmc_cache.usb); 196b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_PMC_IMR, &pmc_cache.imr); 197b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_PMC_PCSR1, &pmc_cache.pcsr1); 198b3b02eacSAlexandre Belloni 199b3b02eacSAlexandre Belloni for (i = 0; registered_ids[i]; i++) { 200b3b02eacSAlexandre Belloni regmap_write(pmcreg, AT91_PMC_PCR, 201b3b02eacSAlexandre Belloni (registered_ids[i] & AT91_PMC_PCR_PID_MASK)); 202b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_PMC_PCR, 203b3b02eacSAlexandre Belloni &pmc_cache.pcr[registered_ids[i]]); 204b3b02eacSAlexandre Belloni } 20513967beaSRomain Izard for (i = 0; registered_pcks[i]; i++) { 20613967beaSRomain Izard num = registered_pcks[i] - 1; 20713967beaSRomain Izard regmap_read(pmcreg, AT91_PMC_PCKR(num), &pmc_cache.pckr[num]); 20813967beaSRomain Izard } 209b3b02eacSAlexandre Belloni 210b3b02eacSAlexandre Belloni return 0; 211b3b02eacSAlexandre Belloni } 212b3b02eacSAlexandre Belloni 213960e1c4dSRomain Izard static bool pmc_ready(unsigned int mask) 214960e1c4dSRomain Izard { 215960e1c4dSRomain Izard unsigned int status; 216960e1c4dSRomain Izard 217960e1c4dSRomain Izard regmap_read(pmcreg, AT91_PMC_SR, &status); 218960e1c4dSRomain Izard 219960e1c4dSRomain Izard return ((status & mask) == mask) ? 1 : 0; 220960e1c4dSRomain Izard } 221960e1c4dSRomain Izard 222b3b02eacSAlexandre Belloni static void pmc_resume(void) 223b3b02eacSAlexandre Belloni { 224960e1c4dSRomain Izard int i; 22513967beaSRomain Izard u8 num; 226b3b02eacSAlexandre Belloni u32 tmp; 227960e1c4dSRomain Izard u32 mask = AT91_PMC_MCKRDY | AT91_PMC_LOCKA; 228b3b02eacSAlexandre Belloni 229b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_PMC_MCKR, &tmp); 230b3b02eacSAlexandre Belloni if (pmc_cache.mckr != tmp) 231b3b02eacSAlexandre Belloni pr_warn("MCKR was not configured properly by the firmware\n"); 232b3b02eacSAlexandre Belloni regmap_read(pmcreg, AT91_CKGR_PLLAR, &tmp); 233b3b02eacSAlexandre Belloni if (pmc_cache.pllar != tmp) 234b3b02eacSAlexandre Belloni pr_warn("PLLAR was not configured properly by the firmware\n"); 235b3b02eacSAlexandre Belloni 2363c6fad25SRomain Izard regmap_write(pmcreg, AT91_PMC_SCER, pmc_cache.scsr); 237b3b02eacSAlexandre Belloni regmap_write(pmcreg, AT91_PMC_PCER, pmc_cache.pcsr0); 238b3b02eacSAlexandre Belloni regmap_write(pmcreg, AT91_CKGR_UCKR, pmc_cache.uckr); 239b3b02eacSAlexandre Belloni regmap_write(pmcreg, AT91_CKGR_MOR, pmc_cache.mor); 240b3b02eacSAlexandre Belloni regmap_write(pmcreg, AT91_CKGR_MCFR, pmc_cache.mcfr); 241b3b02eacSAlexandre Belloni regmap_write(pmcreg, AT91_PMC_USB, pmc_cache.usb); 242b3b02eacSAlexandre Belloni regmap_write(pmcreg, AT91_PMC_IMR, pmc_cache.imr); 243b3b02eacSAlexandre Belloni regmap_write(pmcreg, AT91_PMC_PCER1, pmc_cache.pcsr1); 244b3b02eacSAlexandre Belloni 245b3b02eacSAlexandre Belloni for (i = 0; registered_ids[i]; i++) { 246b3b02eacSAlexandre Belloni regmap_write(pmcreg, AT91_PMC_PCR, 247b3b02eacSAlexandre Belloni pmc_cache.pcr[registered_ids[i]] | 248b3b02eacSAlexandre Belloni AT91_PMC_PCR_CMD); 249b3b02eacSAlexandre Belloni } 25013967beaSRomain Izard for (i = 0; registered_pcks[i]; i++) { 25113967beaSRomain Izard num = registered_pcks[i] - 1; 25213967beaSRomain Izard regmap_write(pmcreg, AT91_PMC_PCKR(num), pmc_cache.pckr[num]); 25313967beaSRomain Izard } 254b3b02eacSAlexandre Belloni 255960e1c4dSRomain Izard if (pmc_cache.uckr & AT91_PMC_UPLLEN) 256960e1c4dSRomain Izard mask |= AT91_PMC_LOCKU; 257960e1c4dSRomain Izard 258960e1c4dSRomain Izard while (!pmc_ready(mask)) 259960e1c4dSRomain Izard cpu_relax(); 260b3b02eacSAlexandre Belloni } 261b3b02eacSAlexandre Belloni 262b3b02eacSAlexandre Belloni static struct syscore_ops pmc_syscore_ops = { 263b3b02eacSAlexandre Belloni .suspend = pmc_suspend, 264b3b02eacSAlexandre Belloni .resume = pmc_resume, 265b3b02eacSAlexandre Belloni }; 266b3b02eacSAlexandre Belloni 267b3b02eacSAlexandre Belloni static const struct of_device_id sama5d2_pmc_dt_ids[] = { 268b3b02eacSAlexandre Belloni { .compatible = "atmel,sama5d2-pmc" }, 269b3b02eacSAlexandre Belloni { /* sentinel */ } 270b3b02eacSAlexandre Belloni }; 271b3b02eacSAlexandre Belloni 272b3b02eacSAlexandre Belloni static int __init pmc_register_ops(void) 273b3b02eacSAlexandre Belloni { 274b3b02eacSAlexandre Belloni struct device_node *np; 275b3b02eacSAlexandre Belloni 276b3b02eacSAlexandre Belloni np = of_find_matching_node(NULL, sama5d2_pmc_dt_ids); 277f6363c43SClaudiu Beznea if (!np) 278f6363c43SClaudiu Beznea return -ENODEV; 279b3b02eacSAlexandre Belloni 2806956eb33SAlexandre Belloni pmcreg = device_node_to_regmap(np); 281b3b02eacSAlexandre Belloni if (IS_ERR(pmcreg)) 282b3b02eacSAlexandre Belloni return PTR_ERR(pmcreg); 283b3b02eacSAlexandre Belloni 284b3b02eacSAlexandre Belloni register_syscore_ops(&pmc_syscore_ops); 285b3b02eacSAlexandre Belloni 286b3b02eacSAlexandre Belloni return 0; 287b3b02eacSAlexandre Belloni } 288b3b02eacSAlexandre Belloni /* This has to happen before arch_initcall because of the tcb_clksrc driver */ 289b3b02eacSAlexandre Belloni postcore_initcall(pmc_register_ops); 290b3b02eacSAlexandre Belloni #endif 291