2 * Copyright (C) 2007,2008 Freescale Semiconductor, Inc. All rights reserved.
4 * Author: John Rigby <jrigby@freescale.com>
6 * Implements the clk api defined in include/linux/clk.h
8 * Original based on linux/arch/arm/mach-integrator/clock.c
10 * Copyright (C) 2004 ARM Limited.
11 * Written by Deep Blue Solutions Limited.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/errno.h>
20 #include <linux/err.h>
21 #include <linux/string.h>
22 #include <linux/clk.h>
23 #include <linux/mutex.h>
26 #include <linux/of_platform.h>
27 #include <asm/mpc5xxx.h>
28 #include <asm/clk_interface.h>
32 static int clocks_initialized;
34 #define CLK_HAS_RATE 0x1 /* has rate in MHz */
35 #define CLK_HAS_CTRL 0x2 /* has control reg and bit */
38 struct list_head node;
44 void (*calc) (struct clk *);
46 int reg, bit; /* CLK_HAS_CTRL */
47 int div_shift; /* only used by generic_div_clk_calc */
50 static LIST_HEAD(clocks);
51 static DEFINE_MUTEX(clocks_mutex);
53 static struct clk *mpc5121_clk_get(struct device *dev, const char *id)
55 struct clk *p, *clk = ERR_PTR(-ENOENT);
59 if (dev == NULL || id == NULL)
62 mutex_lock(&clocks_mutex);
63 list_for_each_entry(p, &clocks, node) {
66 if (strcmp(id, p->name) == 0)
68 if ((dev_match || id_match) && try_module_get(p->owner)) {
73 mutex_unlock(&clocks_mutex);
79 static void dump_clocks(void)
83 mutex_lock(&clocks_mutex);
84 printk(KERN_INFO "CLOCKS:\n");
85 list_for_each_entry(p, &clocks, node) {
86 pr_info(" %s=%ld", p->name, p->rate);
88 pr_cont(" %s=%ld", p->parent->name,
90 if (p->flags & CLK_HAS_CTRL)
91 pr_cont(" reg/bit=%d/%d", p->reg, p->bit);
94 mutex_unlock(&clocks_mutex);
96 #define DEBUG_CLK_DUMP() dump_clocks()
98 #define DEBUG_CLK_DUMP()
102 static void mpc5121_clk_put(struct clk *clk)
104 module_put(clk->owner);
109 struct mpc512x_clockctl {
110 u32 spmr; /* System PLL Mode Reg */
111 u32 sccr[2]; /* System Clk Ctrl Reg 1 & 2 */
112 u32 scfr1; /* System Clk Freq Reg 1 */
113 u32 scfr2; /* System Clk Freq Reg 2 */
115 u32 bcr; /* Bread Crumb Reg */
116 u32 pccr[NRPSC]; /* PSC Clk Ctrl Reg 0-11 */
117 u32 spccr; /* SPDIF Clk Ctrl Reg */
118 u32 cccr; /* CFM Clk Ctrl Reg */
119 u32 dccr; /* DIU Clk Cnfg Reg */
122 struct mpc512x_clockctl __iomem *clockctl;
124 static int mpc5121_clk_enable(struct clk *clk)
128 if (clk->flags & CLK_HAS_CTRL) {
129 mask = in_be32(&clockctl->sccr[clk->reg]);
130 mask |= 1 << clk->bit;
131 out_be32(&clockctl->sccr[clk->reg], mask);
136 static void mpc5121_clk_disable(struct clk *clk)
140 if (clk->flags & CLK_HAS_CTRL) {
141 mask = in_be32(&clockctl->sccr[clk->reg]);
142 mask &= ~(1 << clk->bit);
143 out_be32(&clockctl->sccr[clk->reg], mask);
147 static unsigned long mpc5121_clk_get_rate(struct clk *clk)
149 if (clk->flags & CLK_HAS_RATE)
155 static long mpc5121_clk_round_rate(struct clk *clk, unsigned long rate)
160 static int mpc5121_clk_set_rate(struct clk *clk, unsigned long rate)
165 static int clk_register(struct clk *clk)
167 mutex_lock(&clocks_mutex);
168 list_add(&clk->node, &clocks);
169 mutex_unlock(&clocks_mutex);
173 static unsigned long spmf_mult(void)
176 * Convert spmf to multiplier
178 static int spmf_to_mult[] = {
184 int spmf = (clockctl->spmr >> 24) & 0xf;
185 return spmf_to_mult[spmf];
188 static unsigned long sysdiv_div_x_2(void)
191 * Convert sysdiv to divisor x 2
192 * Some divisors have fractional parts so
193 * multiply by 2 then divide by this value
195 static int sysdiv_to_div_x_2[] = {
206 int sysdiv = (clockctl->scfr2 >> 26) & 0x3f;
207 return sysdiv_to_div_x_2[sysdiv];
210 static unsigned long ref_to_sys(unsigned long rate)
214 rate /= sysdiv_div_x_2();
219 static unsigned long sys_to_ref(unsigned long rate)
221 rate *= sysdiv_div_x_2();
228 static long ips_to_ref(unsigned long rate)
230 int ips_div = (clockctl->scfr1 >> 23) & 0x7;
232 rate *= ips_div; /* csb_clk = ips_clk * ips_div */
233 rate *= 2; /* sys_clk = csb_clk * 2 */
234 return sys_to_ref(rate);
237 static unsigned long devtree_getfreq(char *clockname)
239 struct device_node *np;
240 const unsigned int *prop;
241 unsigned int val = 0;
243 np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-immr");
245 prop = of_get_property(np, clockname, NULL);
253 static void ref_clk_calc(struct clk *clk)
257 rate = devtree_getfreq("bus-frequency");
259 printk(KERN_ERR "No bus-frequency in dev tree\n");
263 clk->rate = ips_to_ref(rate);
266 static struct clk ref_clk = {
268 .calc = ref_clk_calc,
272 static void sys_clk_calc(struct clk *clk)
274 clk->rate = ref_to_sys(ref_clk.rate);
277 static struct clk sys_clk = {
279 .calc = sys_clk_calc,
282 static void diu_clk_calc(struct clk *clk)
284 int diudiv_x_2 = clockctl->scfr1 & 0xff;
295 static void half_clk_calc(struct clk *clk)
297 clk->rate = clk->parent->rate / 2;
300 static void generic_div_clk_calc(struct clk *clk)
302 int div = (clockctl->scfr1 >> clk->div_shift) & 0x7;
304 clk->rate = clk->parent->rate / div;
307 static void unity_clk_calc(struct clk *clk)
309 clk->rate = clk->parent->rate;
312 static struct clk csb_clk = {
314 .calc = half_clk_calc,
318 static void e300_clk_calc(struct clk *clk)
320 int spmf = (clockctl->spmr >> 16) & 0xf;
321 int ratex2 = clk->parent->rate * spmf;
323 clk->rate = ratex2 / 2;
326 static struct clk e300_clk = {
328 .calc = e300_clk_calc,
332 static struct clk ips_clk = {
334 .calc = generic_div_clk_calc,
340 * Clocks controlled by SCCR1 (.reg = 0)
342 static struct clk lpc_clk = {
344 .flags = CLK_HAS_CTRL,
347 .calc = generic_div_clk_calc,
352 static struct clk nfc_clk = {
354 .flags = CLK_HAS_CTRL,
357 .calc = generic_div_clk_calc,
362 static struct clk pata_clk = {
364 .flags = CLK_HAS_CTRL,
367 .calc = unity_clk_calc,
372 * PSC clocks (bits 27 - 16)
373 * are setup elsewhere
376 static struct clk sata_clk = {
378 .flags = CLK_HAS_CTRL,
381 .calc = unity_clk_calc,
385 static struct clk fec_clk = {
387 .flags = CLK_HAS_CTRL,
390 .calc = unity_clk_calc,
394 static struct clk pci_clk = {
396 .flags = CLK_HAS_CTRL,
399 .calc = generic_div_clk_calc,
405 * Clocks controlled by SCCR2 (.reg = 1)
407 static struct clk diu_clk = {
409 .flags = CLK_HAS_CTRL,
412 .calc = diu_clk_calc,
415 static struct clk axe_clk = {
417 .flags = CLK_HAS_CTRL,
420 .calc = unity_clk_calc,
424 static struct clk usb1_clk = {
426 .flags = CLK_HAS_CTRL,
429 .calc = unity_clk_calc,
433 static struct clk usb2_clk = {
435 .flags = CLK_HAS_CTRL,
438 .calc = unity_clk_calc,
442 static struct clk i2c_clk = {
444 .flags = CLK_HAS_CTRL,
447 .calc = unity_clk_calc,
451 static struct clk mscan_clk = {
453 .flags = CLK_HAS_CTRL,
456 .calc = unity_clk_calc,
460 static struct clk sdhc_clk = {
462 .flags = CLK_HAS_CTRL,
465 .calc = unity_clk_calc,
469 static struct clk mbx_bus_clk = {
470 .name = "mbx_bus_clk",
471 .flags = CLK_HAS_CTRL,
474 .calc = half_clk_calc,
478 static struct clk mbx_clk = {
480 .flags = CLK_HAS_CTRL,
483 .calc = unity_clk_calc,
487 static struct clk mbx_3d_clk = {
488 .name = "mbx_3d_clk",
489 .flags = CLK_HAS_CTRL,
492 .calc = generic_div_clk_calc,
493 .parent = &mbx_bus_clk,
497 static void psc_mclk_in_calc(struct clk *clk)
499 clk->rate = devtree_getfreq("psc_mclk_in");
501 clk->rate = 25000000;
504 static struct clk psc_mclk_in = {
505 .name = "psc_mclk_in",
506 .calc = psc_mclk_in_calc,
509 static struct clk spdif_txclk = {
510 .name = "spdif_txclk",
511 .flags = CLK_HAS_CTRL,
516 static struct clk spdif_rxclk = {
517 .name = "spdif_rxclk",
518 .flags = CLK_HAS_CTRL,
523 static void ac97_clk_calc(struct clk *clk)
525 /* ac97 bit clock is always 24.567 MHz */
526 clk->rate = 24567000;
529 static struct clk ac97_clk = {
530 .name = "ac97_clk_in",
531 .calc = ac97_clk_calc,
534 struct clk *rate_clks[] = {
563 static void rate_clk_init(struct clk *clk)
567 clk->flags |= CLK_HAS_RATE;
571 "Could not initialize clk %s without a calc routine\n",
576 static void rate_clks_init(void)
578 struct clk **cpp, *clk;
581 while ((clk = *cpp++))
586 * There are two clk enable registers with 32 enable bits each
587 * psc clocks and device clocks are all stored in dev_clks
589 struct clk dev_clks[2][32];
592 * Given a psc number return the dev_clk
595 static struct clk *psc_dev_clk(int pscnum)
603 clk = &dev_clks[reg][bit];
610 * PSC clock rate calculation
612 static void psc_calc_rate(struct clk *clk, int pscnum, struct device_node *np)
614 unsigned long mclk_src = sys_clk.rate;
615 unsigned long mclk_div;
618 * Can only change value of mclk divider
619 * when the divider is disabled.
621 * Zero is not a valid divider so minimum
624 * disable/set divider/enable
626 out_be32(&clockctl->pccr[pscnum], 0);
627 out_be32(&clockctl->pccr[pscnum], 0x00020000);
628 out_be32(&clockctl->pccr[pscnum], 0x00030000);
630 if (clockctl->pccr[pscnum] & 0x80) {
631 clk->rate = spdif_rxclk.rate;
635 switch ((clockctl->pccr[pscnum] >> 14) & 0x3) {
637 mclk_src = sys_clk.rate;
640 mclk_src = ref_clk.rate;
643 mclk_src = psc_mclk_in.rate;
646 mclk_src = spdif_txclk.rate;
650 mclk_div = ((clockctl->pccr[pscnum] >> 17) & 0x7fff) + 1;
651 clk->rate = mclk_src / mclk_div;
655 * Find all psc nodes in device tree and assign a clock
656 * with name "psc%d_mclk" and dev pointing at the device
657 * returned from of_find_device_by_node
659 static void psc_clks_init(void)
661 struct device_node *np;
662 const u32 *cell_index;
663 struct of_device *ofdev;
665 for_each_compatible_node(np, NULL, "fsl,mpc5121-psc") {
666 cell_index = of_get_property(np, "cell-index", NULL);
668 int pscnum = *cell_index;
669 struct clk *clk = psc_dev_clk(pscnum);
671 clk->flags = CLK_HAS_RATE | CLK_HAS_CTRL;
672 ofdev = of_find_device_by_node(np);
673 clk->dev = &ofdev->dev;
675 * AC97 is special rate clock does
676 * not go through normal path
678 if (strcmp("ac97", np->name) == 0)
679 clk->rate = ac97_clk.rate;
681 psc_calc_rate(clk, pscnum, np);
682 sprintf(clk->name, "psc%d_mclk", pscnum);
689 static struct clk_interface mpc5121_clk_functions = {
690 .clk_get = mpc5121_clk_get,
691 .clk_enable = mpc5121_clk_enable,
692 .clk_disable = mpc5121_clk_disable,
693 .clk_get_rate = mpc5121_clk_get_rate,
694 .clk_put = mpc5121_clk_put,
695 .clk_round_rate = mpc5121_clk_round_rate,
696 .clk_set_rate = mpc5121_clk_set_rate,
697 .clk_set_parent = NULL,
698 .clk_get_parent = NULL,
702 mpc5121_clk_init(void)
704 struct device_node *np;
706 np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock");
708 clockctl = of_iomap(np, 0);
713 printk(KERN_ERR "Could not map clock control registers\n");
720 /* leave clockctl mapped forever */
721 /*iounmap(clockctl); */
723 clocks_initialized++;
724 clk_functions = mpc5121_clk_functions;
729 arch_initcall(mpc5121_clk_init);