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