2 * Joshua Henderson <joshua.henderson@microchip.com>
3 * Copyright (C) 2015 Microchip Technology Inc. All rights reserved.
5 * This program is free software; you can distribute it and/or modify it
6 * under the terms of the GNU General Public License (Version 2) as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 #include <asm/mach-pic32/pic32.h>
16 #include "pic32mzda.h"
18 /* Oscillators, PLL & clocks */
19 #define ICLK_MASK 0x00000080
20 #define PLLDIV_MASK 0x00000007
21 #define CUROSC_MASK 0x00000007
22 #define PLLMUL_MASK 0x0000007F
23 #define PB_MASK 0x00000007
28 #define FRC_CLK 8000000
30 #define PIC32_POSC_FREQ 24000000
33 #define SPLLCON 0x0020
36 u32 pic32_get_sysclk(void)
51 void __iomem *osc_base = ioremap(PIC32_BASE_OSC, 0x200);
53 osccon = __raw_readl(osc_base + OSCCON);
54 spllcon = __raw_readl(osc_base + SPLLCON);
56 plliclk = (spllcon & ICLK_MASK);
57 pllidiv = ((spllcon >> 8) & PLLDIV_MASK) + 1;
58 pllodiv = ((spllcon >> 24) & PLLDIV_MASK);
59 pllmult = ((spllcon >> 16) & PLLMUL_MASK) + 1;
60 frcdiv = ((osccon >> 24) & PLLDIV_MASK);
62 pllclk = plliclk ? FRC_CLK : PIC32_POSC_FREQ;
63 frcdivn = ((1 << frcdiv) + 1) + (128 * (frcdiv == 7));
68 pllodiv = (1 << pllodiv);
72 curr_osc = (int)((osccon >> 12) & CUROSC_MASK);
77 osc_freq = FRC_CLK / frcdivn;
80 osc_freq = ((pllclk / pllidiv) * pllmult) / pllodiv;
83 osc_freq = PIC32_POSC_FREQ;
94 u32 pic32_get_pbclk(int bus)
97 void __iomem *osc_base = ioremap(PIC32_BASE_OSC, 0x200);
98 u32 pbxdiv = PB1DIV + ((bus - 1) * 0x10);
99 u32 pbdiv = (__raw_readl(osc_base + pbxdiv) & PB_MASK) + 1;
103 clk_freq = pic32_get_sysclk();
105 return clk_freq / pbdiv;