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/module.h>
22 #include <linux/string.h>
23 #include <linux/clk.h>
24 #include <linux/mutex.h>
27 #include <linux/of_platform.h>
28 #include <asm/mpc5xxx.h>
29 #include <asm/clk_interface.h>
33 static int clocks_initialized;
35 #define CLK_HAS_RATE 0x1 /* has rate in MHz */
36 #define CLK_HAS_CTRL 0x2 /* has control reg and bit */
39 struct list_head node;
45 void (*calc) (struct clk *);
47 int reg, bit; /* CLK_HAS_CTRL */
48 int div_shift; /* only used by generic_div_clk_calc */
51 static LIST_HEAD(clocks);
52 static DEFINE_MUTEX(clocks_mutex);
54 static struct clk *mpc5121_clk_get(struct device *dev, const char *id)
56 struct clk *p, *clk = ERR_PTR(-ENOENT);
60 if (dev == NULL || id == NULL)
63 mutex_lock(&clocks_mutex);
64 list_for_each_entry(p, &clocks, node) {
65 dev_match = id_match = 0;
69 if (strcmp(id, p->name) == 0)
71 if ((dev_match || id_match) && try_module_get(p->owner)) {
76 mutex_unlock(&clocks_mutex);
82 static void dump_clocks(void)
86 mutex_lock(&clocks_mutex);
87 printk(KERN_INFO "CLOCKS:\n");
88 list_for_each_entry(p, &clocks, node) {
89 pr_info(" %s=%ld", p->name, p->rate);
91 pr_cont(" %s=%ld", p->parent->name,
93 if (p->flags & CLK_HAS_CTRL)
94 pr_cont(" reg/bit=%d/%d", p->reg, p->bit);
97 mutex_unlock(&clocks_mutex);
99 #define DEBUG_CLK_DUMP() dump_clocks()
101 #define DEBUG_CLK_DUMP()
105 static void mpc5121_clk_put(struct clk *clk)
107 module_put(clk->owner);
112 struct mpc512x_clockctl {
113 u32 spmr; /* System PLL Mode Reg */
114 u32 sccr[2]; /* System Clk Ctrl Reg 1 & 2 */
115 u32 scfr1; /* System Clk Freq Reg 1 */
116 u32 scfr2; /* System Clk Freq Reg 2 */
118 u32 bcr; /* Bread Crumb Reg */
119 u32 pccr[NRPSC]; /* PSC Clk Ctrl Reg 0-11 */
120 u32 spccr; /* SPDIF Clk Ctrl Reg */
121 u32 cccr; /* CFM Clk Ctrl Reg */
122 u32 dccr; /* DIU Clk Cnfg Reg */
125 struct mpc512x_clockctl __iomem *clockctl;
127 static int mpc5121_clk_enable(struct clk *clk)
131 if (clk->flags & CLK_HAS_CTRL) {
132 mask = in_be32(&clockctl->sccr[clk->reg]);
133 mask |= 1 << clk->bit;
134 out_be32(&clockctl->sccr[clk->reg], mask);
139 static void mpc5121_clk_disable(struct clk *clk)
143 if (clk->flags & CLK_HAS_CTRL) {
144 mask = in_be32(&clockctl->sccr[clk->reg]);
145 mask &= ~(1 << clk->bit);
146 out_be32(&clockctl->sccr[clk->reg], mask);
150 static unsigned long mpc5121_clk_get_rate(struct clk *clk)
152 if (clk->flags & CLK_HAS_RATE)
158 static long mpc5121_clk_round_rate(struct clk *clk, unsigned long rate)
163 static int mpc5121_clk_set_rate(struct clk *clk, unsigned long rate)
168 static int clk_register(struct clk *clk)
170 mutex_lock(&clocks_mutex);
171 list_add(&clk->node, &clocks);
172 mutex_unlock(&clocks_mutex);
176 static unsigned long spmf_mult(void)
179 * Convert spmf to multiplier
181 static int spmf_to_mult[] = {
187 int spmf = (clockctl->spmr >> 24) & 0xf;
188 return spmf_to_mult[spmf];
191 static unsigned long sysdiv_div_x_2(void)
194 * Convert sysdiv to divisor x 2
195 * Some divisors have fractional parts so
196 * multiply by 2 then divide by this value
198 static int sysdiv_to_div_x_2[] = {
209 int sysdiv = (clockctl->scfr2 >> 26) & 0x3f;
210 return sysdiv_to_div_x_2[sysdiv];
213 static unsigned long ref_to_sys(unsigned long rate)
217 rate /= sysdiv_div_x_2();
222 static unsigned long sys_to_ref(unsigned long rate)
224 rate *= sysdiv_div_x_2();
231 static long ips_to_ref(unsigned long rate)
233 int ips_div = (clockctl->scfr1 >> 23) & 0x7;
235 rate *= ips_div; /* csb_clk = ips_clk * ips_div */
236 rate *= 2; /* sys_clk = csb_clk * 2 */
237 return sys_to_ref(rate);
240 static unsigned long devtree_getfreq(char *clockname)
242 struct device_node *np;
243 const unsigned int *prop;
244 unsigned int val = 0;
246 np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-immr");
248 prop = of_get_property(np, clockname, NULL);
256 static void ref_clk_calc(struct clk *clk)
260 rate = devtree_getfreq("bus-frequency");
262 printk(KERN_ERR "No bus-frequency in dev tree\n");
266 clk->rate = ips_to_ref(rate);
269 static struct clk ref_clk = {
271 .calc = ref_clk_calc,
275 static void sys_clk_calc(struct clk *clk)
277 clk->rate = ref_to_sys(ref_clk.rate);
280 static struct clk sys_clk = {
282 .calc = sys_clk_calc,
285 static void diu_clk_calc(struct clk *clk)
287 int diudiv_x_2 = clockctl->scfr1 & 0xff;
298 static void viu_clk_calc(struct clk *clk)
307 static void half_clk_calc(struct clk *clk)
309 clk->rate = clk->parent->rate / 2;
312 static void generic_div_clk_calc(struct clk *clk)
314 int div = (clockctl->scfr1 >> clk->div_shift) & 0x7;
316 clk->rate = clk->parent->rate / div;
319 static void unity_clk_calc(struct clk *clk)
321 clk->rate = clk->parent->rate;
324 static struct clk csb_clk = {
326 .calc = half_clk_calc,
330 static void e300_clk_calc(struct clk *clk)
332 int spmf = (clockctl->spmr >> 16) & 0xf;
333 int ratex2 = clk->parent->rate * spmf;
335 clk->rate = ratex2 / 2;
338 static struct clk e300_clk = {
340 .calc = e300_clk_calc,
344 static struct clk ips_clk = {
346 .calc = generic_div_clk_calc,
352 * Clocks controlled by SCCR1 (.reg = 0)
354 static struct clk lpc_clk = {
356 .flags = CLK_HAS_CTRL,
359 .calc = generic_div_clk_calc,
364 static struct clk nfc_clk = {
366 .flags = CLK_HAS_CTRL,
369 .calc = generic_div_clk_calc,
374 static struct clk pata_clk = {
376 .flags = CLK_HAS_CTRL,
379 .calc = unity_clk_calc,
384 * PSC clocks (bits 27 - 16)
385 * are setup elsewhere
388 static struct clk sata_clk = {
390 .flags = CLK_HAS_CTRL,
393 .calc = unity_clk_calc,
397 static struct clk fec_clk = {
399 .flags = CLK_HAS_CTRL,
402 .calc = unity_clk_calc,
406 static struct clk pci_clk = {
408 .flags = CLK_HAS_CTRL,
411 .calc = generic_div_clk_calc,
417 * Clocks controlled by SCCR2 (.reg = 1)
419 static struct clk diu_clk = {
421 .flags = CLK_HAS_CTRL,
424 .calc = diu_clk_calc,
427 static struct clk viu_clk = {
429 .flags = CLK_HAS_CTRL,
432 .calc = viu_clk_calc,
435 static struct clk axe_clk = {
437 .flags = CLK_HAS_CTRL,
440 .calc = unity_clk_calc,
444 static struct clk usb1_clk = {
446 .flags = CLK_HAS_CTRL,
449 .calc = unity_clk_calc,
453 static struct clk usb2_clk = {
455 .flags = CLK_HAS_CTRL,
458 .calc = unity_clk_calc,
462 static struct clk i2c_clk = {
464 .flags = CLK_HAS_CTRL,
467 .calc = unity_clk_calc,
471 static struct clk mscan_clk = {
473 .flags = CLK_HAS_CTRL,
476 .calc = unity_clk_calc,
480 static struct clk sdhc_clk = {
482 .flags = CLK_HAS_CTRL,
485 .calc = unity_clk_calc,
489 static struct clk mbx_bus_clk = {
490 .name = "mbx_bus_clk",
491 .flags = CLK_HAS_CTRL,
494 .calc = half_clk_calc,
498 static struct clk mbx_clk = {
500 .flags = CLK_HAS_CTRL,
503 .calc = unity_clk_calc,
507 static struct clk mbx_3d_clk = {
508 .name = "mbx_3d_clk",
509 .flags = CLK_HAS_CTRL,
512 .calc = generic_div_clk_calc,
513 .parent = &mbx_bus_clk,
517 static void psc_mclk_in_calc(struct clk *clk)
519 clk->rate = devtree_getfreq("psc_mclk_in");
521 clk->rate = 25000000;
524 static struct clk psc_mclk_in = {
525 .name = "psc_mclk_in",
526 .calc = psc_mclk_in_calc,
529 static struct clk spdif_txclk = {
530 .name = "spdif_txclk",
531 .flags = CLK_HAS_CTRL,
536 static struct clk spdif_rxclk = {
537 .name = "spdif_rxclk",
538 .flags = CLK_HAS_CTRL,
543 static void ac97_clk_calc(struct clk *clk)
545 /* ac97 bit clock is always 24.567 MHz */
546 clk->rate = 24567000;
549 static struct clk ac97_clk = {
550 .name = "ac97_clk_in",
551 .calc = ac97_clk_calc,
554 struct clk *rate_clks[] = {
584 static void rate_clk_init(struct clk *clk)
588 clk->flags |= CLK_HAS_RATE;
592 "Could not initialize clk %s without a calc routine\n",
597 static void rate_clks_init(void)
599 struct clk **cpp, *clk;
602 while ((clk = *cpp++))
607 * There are two clk enable registers with 32 enable bits each
608 * psc clocks and device clocks are all stored in dev_clks
610 struct clk dev_clks[2][32];
613 * Given a psc number return the dev_clk
616 static struct clk *psc_dev_clk(int pscnum)
624 clk = &dev_clks[reg][bit];
631 * PSC clock rate calculation
633 static void psc_calc_rate(struct clk *clk, int pscnum, struct device_node *np)
635 unsigned long mclk_src = sys_clk.rate;
636 unsigned long mclk_div;
639 * Can only change value of mclk divider
640 * when the divider is disabled.
642 * Zero is not a valid divider so minimum
645 * disable/set divider/enable
647 out_be32(&clockctl->pccr[pscnum], 0);
648 out_be32(&clockctl->pccr[pscnum], 0x00020000);
649 out_be32(&clockctl->pccr[pscnum], 0x00030000);
651 if (clockctl->pccr[pscnum] & 0x80) {
652 clk->rate = spdif_rxclk.rate;
656 switch ((clockctl->pccr[pscnum] >> 14) & 0x3) {
658 mclk_src = sys_clk.rate;
661 mclk_src = ref_clk.rate;
664 mclk_src = psc_mclk_in.rate;
667 mclk_src = spdif_txclk.rate;
671 mclk_div = ((clockctl->pccr[pscnum] >> 17) & 0x7fff) + 1;
672 clk->rate = mclk_src / mclk_div;
676 * Find all psc nodes in device tree and assign a clock
677 * with name "psc%d_mclk" and dev pointing at the device
678 * returned from of_find_device_by_node
680 static void psc_clks_init(void)
682 struct device_node *np;
683 const u32 *cell_index;
684 struct platform_device *ofdev;
686 for_each_compatible_node(np, NULL, "fsl,mpc5121-psc") {
687 cell_index = of_get_property(np, "cell-index", NULL);
689 int pscnum = *cell_index;
690 struct clk *clk = psc_dev_clk(pscnum);
692 clk->flags = CLK_HAS_RATE | CLK_HAS_CTRL;
693 ofdev = of_find_device_by_node(np);
694 clk->dev = &ofdev->dev;
696 * AC97 is special rate clock does
697 * not go through normal path
699 if (strcmp("ac97", np->name) == 0)
700 clk->rate = ac97_clk.rate;
702 psc_calc_rate(clk, pscnum, np);
703 sprintf(clk->name, "psc%d_mclk", pscnum);
710 static struct clk_interface mpc5121_clk_functions = {
711 .clk_get = mpc5121_clk_get,
712 .clk_enable = mpc5121_clk_enable,
713 .clk_disable = mpc5121_clk_disable,
714 .clk_get_rate = mpc5121_clk_get_rate,
715 .clk_put = mpc5121_clk_put,
716 .clk_round_rate = mpc5121_clk_round_rate,
717 .clk_set_rate = mpc5121_clk_set_rate,
718 .clk_set_parent = NULL,
719 .clk_get_parent = NULL,
722 int __init mpc5121_clk_init(void)
724 struct device_node *np;
726 np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock");
728 clockctl = of_iomap(np, 0);
733 printk(KERN_ERR "Could not map clock control registers\n");
740 /* leave clockctl mapped forever */
741 /*iounmap(clockctl); */
743 clocks_initialized++;
744 clk_functions = mpc5121_clk_functions;