1*04dc82e1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2ce6e1188SPurna Chandra Mandal /* 3ce6e1188SPurna Chandra Mandal * Purna Chandra Mandal,<purna.mandal@microchip.com> 4ce6e1188SPurna Chandra Mandal * Copyright (C) 2015 Microchip Technology Inc. All rights reserved. 5ce6e1188SPurna Chandra Mandal */ 6ce6e1188SPurna Chandra Mandal #include <dt-bindings/clock/microchip,pic32-clock.h> 7ce6e1188SPurna Chandra Mandal #include <linux/clk.h> 8ce6e1188SPurna Chandra Mandal #include <linux/clk-provider.h> 9ce6e1188SPurna Chandra Mandal #include <linux/clkdev.h> 1062e59c4eSStephen Boyd #include <linux/io.h> 11ce6e1188SPurna Chandra Mandal #include <linux/module.h> 12ce6e1188SPurna Chandra Mandal #include <linux/of_address.h> 13ce6e1188SPurna Chandra Mandal #include <linux/of_platform.h> 14ce6e1188SPurna Chandra Mandal #include <linux/platform_device.h> 15ce6e1188SPurna Chandra Mandal #include <asm/traps.h> 16ce6e1188SPurna Chandra Mandal 17ce6e1188SPurna Chandra Mandal #include "clk-core.h" 18ce6e1188SPurna Chandra Mandal 19ce6e1188SPurna Chandra Mandal /* FRC Postscaler */ 20ce6e1188SPurna Chandra Mandal #define OSC_FRCDIV_MASK 0x07 21ce6e1188SPurna Chandra Mandal #define OSC_FRCDIV_SHIFT 24 22ce6e1188SPurna Chandra Mandal 23ce6e1188SPurna Chandra Mandal /* SPLL fields */ 24ce6e1188SPurna Chandra Mandal #define PLL_ICLK_MASK 0x01 25ce6e1188SPurna Chandra Mandal #define PLL_ICLK_SHIFT 7 26ce6e1188SPurna Chandra Mandal 27ce6e1188SPurna Chandra Mandal #define DECLARE_PERIPHERAL_CLOCK(__clk_name, __reg, __flags) \ 28ce6e1188SPurna Chandra Mandal { \ 29ce6e1188SPurna Chandra Mandal .ctrl_reg = (__reg), \ 30ce6e1188SPurna Chandra Mandal .init_data = { \ 31ce6e1188SPurna Chandra Mandal .name = (__clk_name), \ 32ce6e1188SPurna Chandra Mandal .parent_names = (const char *[]) { \ 33ce6e1188SPurna Chandra Mandal "sys_clk" \ 34ce6e1188SPurna Chandra Mandal }, \ 35ce6e1188SPurna Chandra Mandal .num_parents = 1, \ 36ce6e1188SPurna Chandra Mandal .ops = &pic32_pbclk_ops, \ 37ce6e1188SPurna Chandra Mandal .flags = (__flags), \ 38ce6e1188SPurna Chandra Mandal }, \ 39ce6e1188SPurna Chandra Mandal } 40ce6e1188SPurna Chandra Mandal 41ce6e1188SPurna Chandra Mandal #define DECLARE_REFO_CLOCK(__clkid, __reg) \ 42ce6e1188SPurna Chandra Mandal { \ 43ce6e1188SPurna Chandra Mandal .ctrl_reg = (__reg), \ 44ce6e1188SPurna Chandra Mandal .init_data = { \ 45ce6e1188SPurna Chandra Mandal .name = "refo" #__clkid "_clk", \ 46ce6e1188SPurna Chandra Mandal .parent_names = (const char *[]) { \ 47ce6e1188SPurna Chandra Mandal "sys_clk", "pb1_clk", "posc_clk", \ 48ce6e1188SPurna Chandra Mandal "frc_clk", "lprc_clk", "sosc_clk", \ 49ce6e1188SPurna Chandra Mandal "sys_pll", "refi" #__clkid "_clk", \ 50ce6e1188SPurna Chandra Mandal "bfrc_clk", \ 51ce6e1188SPurna Chandra Mandal }, \ 52ce6e1188SPurna Chandra Mandal .num_parents = 9, \ 53ce6e1188SPurna Chandra Mandal .flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE,\ 54ce6e1188SPurna Chandra Mandal .ops = &pic32_roclk_ops, \ 55ce6e1188SPurna Chandra Mandal }, \ 56ce6e1188SPurna Chandra Mandal .parent_map = (const u32[]) { \ 57ce6e1188SPurna Chandra Mandal 0, 1, 2, 3, 4, 5, 7, 8, 9 \ 58ce6e1188SPurna Chandra Mandal }, \ 59ce6e1188SPurna Chandra Mandal } 60ce6e1188SPurna Chandra Mandal 61ce6e1188SPurna Chandra Mandal static const struct pic32_ref_osc_data ref_clks[] = { 62ce6e1188SPurna Chandra Mandal DECLARE_REFO_CLOCK(1, 0x80), 63ce6e1188SPurna Chandra Mandal DECLARE_REFO_CLOCK(2, 0xa0), 64ce6e1188SPurna Chandra Mandal DECLARE_REFO_CLOCK(3, 0xc0), 65ce6e1188SPurna Chandra Mandal DECLARE_REFO_CLOCK(4, 0xe0), 66ce6e1188SPurna Chandra Mandal DECLARE_REFO_CLOCK(5, 0x100), 67ce6e1188SPurna Chandra Mandal }; 68ce6e1188SPurna Chandra Mandal 69ce6e1188SPurna Chandra Mandal static const struct pic32_periph_clk_data periph_clocks[] = { 70ce6e1188SPurna Chandra Mandal DECLARE_PERIPHERAL_CLOCK("pb1_clk", 0x140, 0), 71ce6e1188SPurna Chandra Mandal DECLARE_PERIPHERAL_CLOCK("pb2_clk", 0x150, CLK_IGNORE_UNUSED), 72ce6e1188SPurna Chandra Mandal DECLARE_PERIPHERAL_CLOCK("pb3_clk", 0x160, 0), 73ce6e1188SPurna Chandra Mandal DECLARE_PERIPHERAL_CLOCK("pb4_clk", 0x170, 0), 74ce6e1188SPurna Chandra Mandal DECLARE_PERIPHERAL_CLOCK("pb5_clk", 0x180, 0), 75ce6e1188SPurna Chandra Mandal DECLARE_PERIPHERAL_CLOCK("pb6_clk", 0x190, 0), 76ce6e1188SPurna Chandra Mandal DECLARE_PERIPHERAL_CLOCK("cpu_clk", 0x1a0, CLK_IGNORE_UNUSED), 77ce6e1188SPurna Chandra Mandal }; 78ce6e1188SPurna Chandra Mandal 79ce6e1188SPurna Chandra Mandal static const struct pic32_sys_clk_data sys_mux_clk = { 80ce6e1188SPurna Chandra Mandal .slew_reg = 0x1c0, 81ce6e1188SPurna Chandra Mandal .slew_div = 2, /* step of div_4 -> div_2 -> no_div */ 82ce6e1188SPurna Chandra Mandal .init_data = { 83ce6e1188SPurna Chandra Mandal .name = "sys_clk", 84ce6e1188SPurna Chandra Mandal .parent_names = (const char *[]) { 85ce6e1188SPurna Chandra Mandal "frcdiv_clk", "sys_pll", "posc_clk", 86ce6e1188SPurna Chandra Mandal "sosc_clk", "lprc_clk", "frcdiv_clk", 87ce6e1188SPurna Chandra Mandal }, 88ce6e1188SPurna Chandra Mandal .num_parents = 6, 89ce6e1188SPurna Chandra Mandal .ops = &pic32_sclk_ops, 90ce6e1188SPurna Chandra Mandal }, 91ce6e1188SPurna Chandra Mandal .parent_map = (const u32[]) { 92ce6e1188SPurna Chandra Mandal 0, 1, 2, 4, 5, 7, 93ce6e1188SPurna Chandra Mandal }, 94ce6e1188SPurna Chandra Mandal }; 95ce6e1188SPurna Chandra Mandal 96ce6e1188SPurna Chandra Mandal static const struct pic32_sys_pll_data sys_pll = { 97ce6e1188SPurna Chandra Mandal .ctrl_reg = 0x020, 98ce6e1188SPurna Chandra Mandal .status_reg = 0x1d0, 99ce6e1188SPurna Chandra Mandal .lock_mask = BIT(7), 100ce6e1188SPurna Chandra Mandal .init_data = { 101ce6e1188SPurna Chandra Mandal .name = "sys_pll", 102ce6e1188SPurna Chandra Mandal .parent_names = (const char *[]) { 103ce6e1188SPurna Chandra Mandal "spll_mux_clk" 104ce6e1188SPurna Chandra Mandal }, 105ce6e1188SPurna Chandra Mandal .num_parents = 1, 106ce6e1188SPurna Chandra Mandal .ops = &pic32_spll_ops, 107ce6e1188SPurna Chandra Mandal }, 108ce6e1188SPurna Chandra Mandal }; 109ce6e1188SPurna Chandra Mandal 110ce6e1188SPurna Chandra Mandal static const struct pic32_sec_osc_data sosc_clk = { 111ce6e1188SPurna Chandra Mandal .status_reg = 0x1d0, 112ce6e1188SPurna Chandra Mandal .enable_mask = BIT(1), 113ce6e1188SPurna Chandra Mandal .status_mask = BIT(4), 114a38c9410SPurna Chandra Mandal .fixed_rate = 32768, 115ce6e1188SPurna Chandra Mandal .init_data = { 116ce6e1188SPurna Chandra Mandal .name = "sosc_clk", 117ce6e1188SPurna Chandra Mandal .parent_names = NULL, 118ce6e1188SPurna Chandra Mandal .ops = &pic32_sosc_ops, 119ce6e1188SPurna Chandra Mandal }, 120ce6e1188SPurna Chandra Mandal }; 121ce6e1188SPurna Chandra Mandal 122ce6e1188SPurna Chandra Mandal static int pic32mzda_critical_clks[] = { 123ce6e1188SPurna Chandra Mandal PB2CLK, PB7CLK 124ce6e1188SPurna Chandra Mandal }; 125ce6e1188SPurna Chandra Mandal 126ce6e1188SPurna Chandra Mandal /* PIC32MZDA clock data */ 127ce6e1188SPurna Chandra Mandal struct pic32mzda_clk_data { 128ce6e1188SPurna Chandra Mandal struct clk *clks[MAXCLKS]; 129ce6e1188SPurna Chandra Mandal struct pic32_clk_common core; 130ce6e1188SPurna Chandra Mandal struct clk_onecell_data onecell_data; 131ce6e1188SPurna Chandra Mandal struct notifier_block failsafe_notifier; 132ce6e1188SPurna Chandra Mandal }; 133ce6e1188SPurna Chandra Mandal 134ce6e1188SPurna Chandra Mandal static int pic32_fscm_nmi(struct notifier_block *nb, 135ce6e1188SPurna Chandra Mandal unsigned long action, void *data) 136ce6e1188SPurna Chandra Mandal { 137ce6e1188SPurna Chandra Mandal struct pic32mzda_clk_data *cd; 138ce6e1188SPurna Chandra Mandal 139ce6e1188SPurna Chandra Mandal cd = container_of(nb, struct pic32mzda_clk_data, failsafe_notifier); 140ce6e1188SPurna Chandra Mandal 141ce6e1188SPurna Chandra Mandal /* SYSCLK is now running from BFRCCLK. Report clock failure. */ 142ce6e1188SPurna Chandra Mandal if (readl(cd->core.iobase) & BIT(2)) 143ce6e1188SPurna Chandra Mandal pr_alert("pic32-clk: FSCM detected clk failure.\n"); 144ce6e1188SPurna Chandra Mandal 145ce6e1188SPurna Chandra Mandal /* TODO: detect reason of failure and recover accordingly */ 146ce6e1188SPurna Chandra Mandal 147ce6e1188SPurna Chandra Mandal return NOTIFY_OK; 148ce6e1188SPurna Chandra Mandal } 149ce6e1188SPurna Chandra Mandal 150ce6e1188SPurna Chandra Mandal static int pic32mzda_clk_probe(struct platform_device *pdev) 151ce6e1188SPurna Chandra Mandal { 152ce6e1188SPurna Chandra Mandal const char *const pll_mux_parents[] = {"posc_clk", "frc_clk"}; 153ce6e1188SPurna Chandra Mandal struct device_node *np = pdev->dev.of_node; 154ce6e1188SPurna Chandra Mandal struct pic32mzda_clk_data *cd; 155ce6e1188SPurna Chandra Mandal struct pic32_clk_common *core; 156ce6e1188SPurna Chandra Mandal struct clk *pll_mux_clk, *clk; 157ce6e1188SPurna Chandra Mandal struct clk **clks; 158ce6e1188SPurna Chandra Mandal int nr_clks, i, ret; 159ce6e1188SPurna Chandra Mandal 160ce6e1188SPurna Chandra Mandal cd = devm_kzalloc(&pdev->dev, sizeof(*cd), GFP_KERNEL); 161ce6e1188SPurna Chandra Mandal if (!cd) 162ce6e1188SPurna Chandra Mandal return -ENOMEM; 163ce6e1188SPurna Chandra Mandal 164ce6e1188SPurna Chandra Mandal core = &cd->core; 165ce6e1188SPurna Chandra Mandal core->iobase = of_io_request_and_map(np, 0, of_node_full_name(np)); 166ce6e1188SPurna Chandra Mandal if (IS_ERR(core->iobase)) { 167ce6e1188SPurna Chandra Mandal dev_err(&pdev->dev, "pic32-clk: failed to map registers\n"); 168ce6e1188SPurna Chandra Mandal return PTR_ERR(core->iobase); 169ce6e1188SPurna Chandra Mandal } 170ce6e1188SPurna Chandra Mandal 171ce6e1188SPurna Chandra Mandal spin_lock_init(&core->reg_lock); 172ce6e1188SPurna Chandra Mandal core->dev = &pdev->dev; 173ce6e1188SPurna Chandra Mandal clks = &cd->clks[0]; 174ce6e1188SPurna Chandra Mandal 175ce6e1188SPurna Chandra Mandal /* register fixed rate clocks */ 176ce6e1188SPurna Chandra Mandal clks[POSCCLK] = clk_register_fixed_rate(&pdev->dev, "posc_clk", NULL, 1773c7f4f54SStephen Boyd 0, 24000000); 178ce6e1188SPurna Chandra Mandal clks[FRCCLK] = clk_register_fixed_rate(&pdev->dev, "frc_clk", NULL, 1793c7f4f54SStephen Boyd 0, 8000000); 180ce6e1188SPurna Chandra Mandal clks[BFRCCLK] = clk_register_fixed_rate(&pdev->dev, "bfrc_clk", NULL, 1813c7f4f54SStephen Boyd 0, 8000000); 182ce6e1188SPurna Chandra Mandal clks[LPRCCLK] = clk_register_fixed_rate(&pdev->dev, "lprc_clk", NULL, 1833c7f4f54SStephen Boyd 0, 32000); 184ce6e1188SPurna Chandra Mandal clks[UPLLCLK] = clk_register_fixed_rate(&pdev->dev, "usbphy_clk", NULL, 1853c7f4f54SStephen Boyd 0, 24000000); 186ce6e1188SPurna Chandra Mandal /* fixed rate (optional) clock */ 187ce6e1188SPurna Chandra Mandal if (of_find_property(np, "microchip,pic32mzda-sosc", NULL)) { 188ce6e1188SPurna Chandra Mandal pr_info("pic32-clk: dt requests SOSC.\n"); 189ce6e1188SPurna Chandra Mandal clks[SOSCCLK] = pic32_sosc_clk_register(&sosc_clk, core); 190ce6e1188SPurna Chandra Mandal } 191ce6e1188SPurna Chandra Mandal /* divider clock */ 192ce6e1188SPurna Chandra Mandal clks[FRCDIVCLK] = clk_register_divider(&pdev->dev, "frcdiv_clk", 193ce6e1188SPurna Chandra Mandal "frc_clk", 0, 194ce6e1188SPurna Chandra Mandal core->iobase, 195ce6e1188SPurna Chandra Mandal OSC_FRCDIV_SHIFT, 196ce6e1188SPurna Chandra Mandal OSC_FRCDIV_MASK, 197ce6e1188SPurna Chandra Mandal CLK_DIVIDER_POWER_OF_TWO, 198ce6e1188SPurna Chandra Mandal &core->reg_lock); 199ce6e1188SPurna Chandra Mandal /* PLL ICLK mux */ 200ce6e1188SPurna Chandra Mandal pll_mux_clk = clk_register_mux(&pdev->dev, "spll_mux_clk", 201ce6e1188SPurna Chandra Mandal pll_mux_parents, 2, 0, 202ce6e1188SPurna Chandra Mandal core->iobase + 0x020, 203ce6e1188SPurna Chandra Mandal PLL_ICLK_SHIFT, 1, 0, &core->reg_lock); 204ce6e1188SPurna Chandra Mandal if (IS_ERR(pll_mux_clk)) 205ce6e1188SPurna Chandra Mandal pr_err("spll_mux_clk: clk register failed\n"); 206ce6e1188SPurna Chandra Mandal 207ce6e1188SPurna Chandra Mandal /* PLL */ 208ce6e1188SPurna Chandra Mandal clks[PLLCLK] = pic32_spll_clk_register(&sys_pll, core); 209ce6e1188SPurna Chandra Mandal /* SYSTEM clock */ 210ce6e1188SPurna Chandra Mandal clks[SCLK] = pic32_sys_clk_register(&sys_mux_clk, core); 211ce6e1188SPurna Chandra Mandal /* Peripheral bus clocks */ 212ce6e1188SPurna Chandra Mandal for (nr_clks = PB1CLK, i = 0; nr_clks <= PB7CLK; i++, nr_clks++) 213ce6e1188SPurna Chandra Mandal clks[nr_clks] = pic32_periph_clk_register(&periph_clocks[i], 214ce6e1188SPurna Chandra Mandal core); 215ce6e1188SPurna Chandra Mandal /* Reference oscillator clock */ 216ce6e1188SPurna Chandra Mandal for (nr_clks = REF1CLK, i = 0; nr_clks <= REF5CLK; i++, nr_clks++) 217ce6e1188SPurna Chandra Mandal clks[nr_clks] = pic32_refo_clk_register(&ref_clks[i], core); 218ce6e1188SPurna Chandra Mandal 219ce6e1188SPurna Chandra Mandal /* register clkdev */ 220ce6e1188SPurna Chandra Mandal for (i = 0; i < MAXCLKS; i++) { 221ce6e1188SPurna Chandra Mandal if (IS_ERR(clks[i])) 222ce6e1188SPurna Chandra Mandal continue; 223ce6e1188SPurna Chandra Mandal clk_register_clkdev(clks[i], NULL, __clk_get_name(clks[i])); 224ce6e1188SPurna Chandra Mandal } 225ce6e1188SPurna Chandra Mandal 226ce6e1188SPurna Chandra Mandal /* register clock provider */ 227ce6e1188SPurna Chandra Mandal cd->onecell_data.clks = clks; 228ce6e1188SPurna Chandra Mandal cd->onecell_data.clk_num = MAXCLKS; 229ce6e1188SPurna Chandra Mandal ret = of_clk_add_provider(np, of_clk_src_onecell_get, 230ce6e1188SPurna Chandra Mandal &cd->onecell_data); 231ce6e1188SPurna Chandra Mandal if (ret) 232ce6e1188SPurna Chandra Mandal return ret; 233ce6e1188SPurna Chandra Mandal 234ce6e1188SPurna Chandra Mandal /* force enable critical clocks */ 235ce6e1188SPurna Chandra Mandal for (i = 0; i < ARRAY_SIZE(pic32mzda_critical_clks); i++) { 236ce6e1188SPurna Chandra Mandal clk = clks[pic32mzda_critical_clks[i]]; 237ce6e1188SPurna Chandra Mandal if (clk_prepare_enable(clk)) 238ce6e1188SPurna Chandra Mandal dev_err(&pdev->dev, "clk_prepare_enable(%s) failed\n", 239ce6e1188SPurna Chandra Mandal __clk_get_name(clk)); 240ce6e1188SPurna Chandra Mandal } 241ce6e1188SPurna Chandra Mandal 242ce6e1188SPurna Chandra Mandal /* register NMI for failsafe clock monitor */ 243ce6e1188SPurna Chandra Mandal cd->failsafe_notifier.notifier_call = pic32_fscm_nmi; 244ce6e1188SPurna Chandra Mandal return register_nmi_notifier(&cd->failsafe_notifier); 245ce6e1188SPurna Chandra Mandal } 246ce6e1188SPurna Chandra Mandal 247ce6e1188SPurna Chandra Mandal static const struct of_device_id pic32mzda_clk_match_table[] = { 248ce6e1188SPurna Chandra Mandal { .compatible = "microchip,pic32mzda-clk", }, 249ce6e1188SPurna Chandra Mandal { } 250ce6e1188SPurna Chandra Mandal }; 251ce6e1188SPurna Chandra Mandal MODULE_DEVICE_TABLE(of, pic32mzda_clk_match_table); 252ce6e1188SPurna Chandra Mandal 253ce6e1188SPurna Chandra Mandal static struct platform_driver pic32mzda_clk_driver = { 254ce6e1188SPurna Chandra Mandal .probe = pic32mzda_clk_probe, 255ce6e1188SPurna Chandra Mandal .driver = { 256ce6e1188SPurna Chandra Mandal .name = "clk-pic32mzda", 257ce6e1188SPurna Chandra Mandal .of_match_table = pic32mzda_clk_match_table, 258ce6e1188SPurna Chandra Mandal }, 259ce6e1188SPurna Chandra Mandal }; 260ce6e1188SPurna Chandra Mandal 261ce6e1188SPurna Chandra Mandal static int __init microchip_pic32mzda_clk_init(void) 262ce6e1188SPurna Chandra Mandal { 263ce6e1188SPurna Chandra Mandal return platform_driver_register(&pic32mzda_clk_driver); 264ce6e1188SPurna Chandra Mandal } 265ce6e1188SPurna Chandra Mandal core_initcall(microchip_pic32mzda_clk_init); 266ce6e1188SPurna Chandra Mandal 267ce6e1188SPurna Chandra Mandal MODULE_DESCRIPTION("Microchip PIC32MZDA Clock Driver"); 268ce6e1188SPurna Chandra Mandal MODULE_LICENSE("GPL v2"); 269ce6e1188SPurna Chandra Mandal MODULE_ALIAS("platform:clk-pic32mzda"); 270