Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
clock.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007,2008 Freescale Semiconductor, Inc. All rights reserved.
3  *
4  * Author: John Rigby <[email protected]>
5  *
6  * Implements the clk api defined in include/linux/clk.h
7  *
8  * Original based on linux/arch/arm/mach-integrator/clock.c
9  *
10  * Copyright (C) 2004 ARM Limited.
11  * Written by Deep Blue Solutions Limited.
12  *
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.
16  */
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>
25 #include <linux/io.h>
26 
27 #include <linux/of_platform.h>
28 #include <asm/mpc5xxx.h>
29 #include <asm/clk_interface.h>
30 
31 #undef CLK_DEBUG
32 
33 static int clocks_initialized;
34 
35 #define CLK_HAS_RATE 0x1 /* has rate in MHz */
36 #define CLK_HAS_CTRL 0x2 /* has control reg and bit */
37 
38 struct clk {
39  struct list_head node;
40  char name[32];
41  int flags;
42  struct device *dev;
43  unsigned long rate;
44  struct module *owner;
45  void (*calc) (struct clk *);
46  struct clk *parent;
47  int reg, bit; /* CLK_HAS_CTRL */
48  int div_shift; /* only used by generic_div_clk_calc */
49 };
50 
51 static LIST_HEAD(clocks);
52 static DEFINE_MUTEX(clocks_mutex);
53 
54 static struct clk *mpc5121_clk_get(struct device *dev, const char *id)
55 {
56  struct clk *p, *clk = ERR_PTR(-ENOENT);
57  int dev_match;
58  int id_match;
59 
60  if (dev == NULL || id == NULL)
61  return clk;
62 
63  mutex_lock(&clocks_mutex);
64  list_for_each_entry(p, &clocks, node) {
65  dev_match = id_match = 0;
66 
67  if (dev == p->dev)
68  dev_match++;
69  if (strcmp(id, p->name) == 0)
70  id_match++;
71  if ((dev_match || id_match) && try_module_get(p->owner)) {
72  clk = p;
73  break;
74  }
75  }
76  mutex_unlock(&clocks_mutex);
77 
78  return clk;
79 }
80 
81 #ifdef CLK_DEBUG
82 static void dump_clocks(void)
83 {
84  struct clk *p;
85 
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);
90  if (p->parent)
91  pr_cont(" %s=%ld", p->parent->name,
92  p->parent->rate);
93  if (p->flags & CLK_HAS_CTRL)
94  pr_cont(" reg/bit=%d/%d", p->reg, p->bit);
95  pr_cont("\n");
96  }
97  mutex_unlock(&clocks_mutex);
98 }
99 #define DEBUG_CLK_DUMP() dump_clocks()
100 #else
101 #define DEBUG_CLK_DUMP()
102 #endif
103 
104 
105 static void mpc5121_clk_put(struct clk *clk)
106 {
107  module_put(clk->owner);
108 }
109 
110 #define NRPSC 12
111 
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 */
123 };
124 
126 
127 static int mpc5121_clk_enable(struct clk *clk)
128 {
129  unsigned int mask;
130 
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);
135  }
136  return 0;
137 }
138 
139 static void mpc5121_clk_disable(struct clk *clk)
140 {
141  unsigned int mask;
142 
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);
147  }
148 }
149 
150 static unsigned long mpc5121_clk_get_rate(struct clk *clk)
151 {
152  if (clk->flags & CLK_HAS_RATE)
153  return clk->rate;
154  else
155  return 0;
156 }
157 
158 static long mpc5121_clk_round_rate(struct clk *clk, unsigned long rate)
159 {
160  return rate;
161 }
162 
163 static int mpc5121_clk_set_rate(struct clk *clk, unsigned long rate)
164 {
165  return 0;
166 }
167 
168 static int clk_register(struct clk *clk)
169 {
170  mutex_lock(&clocks_mutex);
171  list_add(&clk->node, &clocks);
172  mutex_unlock(&clocks_mutex);
173  return 0;
174 }
175 
176 static unsigned long spmf_mult(void)
177 {
178  /*
179  * Convert spmf to multiplier
180  */
181  static int spmf_to_mult[] = {
182  68, 1, 12, 16,
183  20, 24, 28, 32,
184  36, 40, 44, 48,
185  52, 56, 60, 64
186  };
187  int spmf = (clockctl->spmr >> 24) & 0xf;
188  return spmf_to_mult[spmf];
189 }
190 
191 static unsigned long sysdiv_div_x_2(void)
192 {
193  /*
194  * Convert sysdiv to divisor x 2
195  * Some divisors have fractional parts so
196  * multiply by 2 then divide by this value
197  */
198  static int sysdiv_to_div_x_2[] = {
199  4, 5, 6, 7,
200  8, 9, 10, 14,
201  12, 16, 18, 22,
202  20, 24, 26, 30,
203  28, 32, 34, 38,
204  36, 40, 42, 46,
205  44, 48, 50, 54,
206  52, 56, 58, 62,
207  60, 64, 66,
208  };
209  int sysdiv = (clockctl->scfr2 >> 26) & 0x3f;
210  return sysdiv_to_div_x_2[sysdiv];
211 }
212 
213 static unsigned long ref_to_sys(unsigned long rate)
214 {
215  rate *= spmf_mult();
216  rate *= 2;
217  rate /= sysdiv_div_x_2();
218 
219  return rate;
220 }
221 
222 static unsigned long sys_to_ref(unsigned long rate)
223 {
224  rate *= sysdiv_div_x_2();
225  rate /= 2;
226  rate /= spmf_mult();
227 
228  return rate;
229 }
230 
231 static long ips_to_ref(unsigned long rate)
232 {
233  int ips_div = (clockctl->scfr1 >> 23) & 0x7;
234 
235  rate *= ips_div; /* csb_clk = ips_clk * ips_div */
236  rate *= 2; /* sys_clk = csb_clk * 2 */
237  return sys_to_ref(rate);
238 }
239 
240 static unsigned long devtree_getfreq(char *clockname)
241 {
242  struct device_node *np;
243  const unsigned int *prop;
244  unsigned int val = 0;
245 
246  np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-immr");
247  if (np) {
248  prop = of_get_property(np, clockname, NULL);
249  if (prop)
250  val = *prop;
251  of_node_put(np);
252  }
253  return val;
254 }
255 
256 static void ref_clk_calc(struct clk *clk)
257 {
258  unsigned long rate;
259 
260  rate = devtree_getfreq("bus-frequency");
261  if (rate == 0) {
262  printk(KERN_ERR "No bus-frequency in dev tree\n");
263  clk->rate = 0;
264  return;
265  }
266  clk->rate = ips_to_ref(rate);
267 }
268 
269 static struct clk ref_clk = {
270  .name = "ref_clk",
271  .calc = ref_clk_calc,
272 };
273 
274 
275 static void sys_clk_calc(struct clk *clk)
276 {
277  clk->rate = ref_to_sys(ref_clk.rate);
278 }
279 
280 static struct clk sys_clk = {
281  .name = "sys_clk",
282  .calc = sys_clk_calc,
283 };
284 
285 static void diu_clk_calc(struct clk *clk)
286 {
287  int diudiv_x_2 = clockctl->scfr1 & 0xff;
288  unsigned long rate;
289 
290  rate = sys_clk.rate;
291 
292  rate *= 2;
293  rate /= diudiv_x_2;
294 
295  clk->rate = rate;
296 }
297 
298 static void viu_clk_calc(struct clk *clk)
299 {
300  unsigned long rate;
301 
302  rate = sys_clk.rate;
303  rate /= 2;
304  clk->rate = rate;
305 }
306 
307 static void half_clk_calc(struct clk *clk)
308 {
309  clk->rate = clk->parent->rate / 2;
310 }
311 
312 static void generic_div_clk_calc(struct clk *clk)
313 {
314  int div = (clockctl->scfr1 >> clk->div_shift) & 0x7;
315 
316  clk->rate = clk->parent->rate / div;
317 }
318 
319 static void unity_clk_calc(struct clk *clk)
320 {
321  clk->rate = clk->parent->rate;
322 }
323 
324 static struct clk csb_clk = {
325  .name = "csb_clk",
326  .calc = half_clk_calc,
327  .parent = &sys_clk,
328 };
329 
330 static void e300_clk_calc(struct clk *clk)
331 {
332  int spmf = (clockctl->spmr >> 16) & 0xf;
333  int ratex2 = clk->parent->rate * spmf;
334 
335  clk->rate = ratex2 / 2;
336 }
337 
338 static struct clk e300_clk = {
339  .name = "e300_clk",
340  .calc = e300_clk_calc,
341  .parent = &csb_clk,
342 };
343 
344 static struct clk ips_clk = {
345  .name = "ips_clk",
346  .calc = generic_div_clk_calc,
347  .parent = &csb_clk,
348  .div_shift = 23,
349 };
350 
351 /*
352  * Clocks controlled by SCCR1 (.reg = 0)
353  */
354 static struct clk lpc_clk = {
355  .name = "lpc_clk",
356  .flags = CLK_HAS_CTRL,
357  .reg = 0,
358  .bit = 30,
359  .calc = generic_div_clk_calc,
360  .parent = &ips_clk,
361  .div_shift = 11,
362 };
363 
364 static struct clk nfc_clk = {
365  .name = "nfc_clk",
366  .flags = CLK_HAS_CTRL,
367  .reg = 0,
368  .bit = 29,
369  .calc = generic_div_clk_calc,
370  .parent = &ips_clk,
371  .div_shift = 8,
372 };
373 
374 static struct clk pata_clk = {
375  .name = "pata_clk",
376  .flags = CLK_HAS_CTRL,
377  .reg = 0,
378  .bit = 28,
379  .calc = unity_clk_calc,
380  .parent = &ips_clk,
381 };
382 
383 /*
384  * PSC clocks (bits 27 - 16)
385  * are setup elsewhere
386  */
387 
388 static struct clk sata_clk = {
389  .name = "sata_clk",
390  .flags = CLK_HAS_CTRL,
391  .reg = 0,
392  .bit = 14,
393  .calc = unity_clk_calc,
394  .parent = &ips_clk,
395 };
396 
397 static struct clk fec_clk = {
398  .name = "fec_clk",
399  .flags = CLK_HAS_CTRL,
400  .reg = 0,
401  .bit = 13,
402  .calc = unity_clk_calc,
403  .parent = &ips_clk,
404 };
405 
406 static struct clk pci_clk = {
407  .name = "pci_clk",
408  .flags = CLK_HAS_CTRL,
409  .reg = 0,
410  .bit = 11,
411  .calc = generic_div_clk_calc,
412  .parent = &csb_clk,
413  .div_shift = 20,
414 };
415 
416 /*
417  * Clocks controlled by SCCR2 (.reg = 1)
418  */
419 static struct clk diu_clk = {
420  .name = "diu_clk",
421  .flags = CLK_HAS_CTRL,
422  .reg = 1,
423  .bit = 31,
424  .calc = diu_clk_calc,
425 };
426 
427 static struct clk viu_clk = {
428  .name = "viu_clk",
429  .flags = CLK_HAS_CTRL,
430  .reg = 1,
431  .bit = 18,
432  .calc = viu_clk_calc,
433 };
434 
435 static struct clk axe_clk = {
436  .name = "axe_clk",
437  .flags = CLK_HAS_CTRL,
438  .reg = 1,
439  .bit = 30,
440  .calc = unity_clk_calc,
441  .parent = &csb_clk,
442 };
443 
444 static struct clk usb1_clk = {
445  .name = "usb1_clk",
446  .flags = CLK_HAS_CTRL,
447  .reg = 1,
448  .bit = 28,
449  .calc = unity_clk_calc,
450  .parent = &csb_clk,
451 };
452 
453 static struct clk usb2_clk = {
454  .name = "usb2_clk",
455  .flags = CLK_HAS_CTRL,
456  .reg = 1,
457  .bit = 27,
458  .calc = unity_clk_calc,
459  .parent = &csb_clk,
460 };
461 
462 static struct clk i2c_clk = {
463  .name = "i2c_clk",
464  .flags = CLK_HAS_CTRL,
465  .reg = 1,
466  .bit = 26,
467  .calc = unity_clk_calc,
468  .parent = &ips_clk,
469 };
470 
471 static struct clk mscan_clk = {
472  .name = "mscan_clk",
473  .flags = CLK_HAS_CTRL,
474  .reg = 1,
475  .bit = 25,
476  .calc = unity_clk_calc,
477  .parent = &ips_clk,
478 };
479 
480 static struct clk sdhc_clk = {
481  .name = "sdhc_clk",
482  .flags = CLK_HAS_CTRL,
483  .reg = 1,
484  .bit = 24,
485  .calc = unity_clk_calc,
486  .parent = &ips_clk,
487 };
488 
489 static struct clk mbx_bus_clk = {
490  .name = "mbx_bus_clk",
491  .flags = CLK_HAS_CTRL,
492  .reg = 1,
493  .bit = 22,
494  .calc = half_clk_calc,
495  .parent = &csb_clk,
496 };
497 
498 static struct clk mbx_clk = {
499  .name = "mbx_clk",
500  .flags = CLK_HAS_CTRL,
501  .reg = 1,
502  .bit = 21,
503  .calc = unity_clk_calc,
504  .parent = &csb_clk,
505 };
506 
507 static struct clk mbx_3d_clk = {
508  .name = "mbx_3d_clk",
509  .flags = CLK_HAS_CTRL,
510  .reg = 1,
511  .bit = 20,
512  .calc = generic_div_clk_calc,
513  .parent = &mbx_bus_clk,
514  .div_shift = 14,
515 };
516 
517 static void psc_mclk_in_calc(struct clk *clk)
518 {
519  clk->rate = devtree_getfreq("psc_mclk_in");
520  if (!clk->rate)
521  clk->rate = 25000000;
522 }
523 
524 static struct clk psc_mclk_in = {
525  .name = "psc_mclk_in",
526  .calc = psc_mclk_in_calc,
527 };
528 
529 static struct clk spdif_txclk = {
530  .name = "spdif_txclk",
531  .flags = CLK_HAS_CTRL,
532  .reg = 1,
533  .bit = 23,
534 };
535 
536 static struct clk spdif_rxclk = {
537  .name = "spdif_rxclk",
538  .flags = CLK_HAS_CTRL,
539  .reg = 1,
540  .bit = 23,
541 };
542 
543 static void ac97_clk_calc(struct clk *clk)
544 {
545  /* ac97 bit clock is always 24.567 MHz */
546  clk->rate = 24567000;
547 }
548 
549 static struct clk ac97_clk = {
550  .name = "ac97_clk_in",
551  .calc = ac97_clk_calc,
552 };
553 
554 struct clk *rate_clks[] = {
555  &ref_clk,
556  &sys_clk,
557  &diu_clk,
558  &viu_clk,
559  &csb_clk,
560  &e300_clk,
561  &ips_clk,
562  &fec_clk,
563  &sata_clk,
564  &pata_clk,
565  &nfc_clk,
566  &lpc_clk,
567  &mbx_bus_clk,
568  &mbx_clk,
569  &mbx_3d_clk,
570  &axe_clk,
571  &usb1_clk,
572  &usb2_clk,
573  &i2c_clk,
574  &mscan_clk,
575  &sdhc_clk,
576  &pci_clk,
577  &psc_mclk_in,
578  &spdif_txclk,
579  &spdif_rxclk,
580  &ac97_clk,
581  NULL
582 };
583 
584 static void rate_clk_init(struct clk *clk)
585 {
586  if (clk->calc) {
587  clk->calc(clk);
588  clk->flags |= CLK_HAS_RATE;
589  clk_register(clk);
590  } else {
592  "Could not initialize clk %s without a calc routine\n",
593  clk->name);
594  }
595 }
596 
597 static void rate_clks_init(void)
598 {
599  struct clk **cpp, *clk;
600 
601  cpp = rate_clks;
602  while ((clk = *cpp++))
603  rate_clk_init(clk);
604 }
605 
606 /*
607  * There are two clk enable registers with 32 enable bits each
608  * psc clocks and device clocks are all stored in dev_clks
609  */
610 struct clk dev_clks[2][32];
611 
612 /*
613  * Given a psc number return the dev_clk
614  * associated with it
615  */
616 static struct clk *psc_dev_clk(int pscnum)
617 {
618  int reg, bit;
619  struct clk *clk;
620 
621  reg = 0;
622  bit = 27 - pscnum;
623 
624  clk = &dev_clks[reg][bit];
625  clk->reg = 0;
626  clk->bit = bit;
627  return clk;
628 }
629 
630 /*
631  * PSC clock rate calculation
632  */
633 static void psc_calc_rate(struct clk *clk, int pscnum, struct device_node *np)
634 {
635  unsigned long mclk_src = sys_clk.rate;
636  unsigned long mclk_div;
637 
638  /*
639  * Can only change value of mclk divider
640  * when the divider is disabled.
641  *
642  * Zero is not a valid divider so minimum
643  * divider is 1
644  *
645  * disable/set divider/enable
646  */
647  out_be32(&clockctl->pccr[pscnum], 0);
648  out_be32(&clockctl->pccr[pscnum], 0x00020000);
649  out_be32(&clockctl->pccr[pscnum], 0x00030000);
650 
651  if (clockctl->pccr[pscnum] & 0x80) {
652  clk->rate = spdif_rxclk.rate;
653  return;
654  }
655 
656  switch ((clockctl->pccr[pscnum] >> 14) & 0x3) {
657  case 0:
658  mclk_src = sys_clk.rate;
659  break;
660  case 1:
661  mclk_src = ref_clk.rate;
662  break;
663  case 2:
664  mclk_src = psc_mclk_in.rate;
665  break;
666  case 3:
667  mclk_src = spdif_txclk.rate;
668  break;
669  }
670 
671  mclk_div = ((clockctl->pccr[pscnum] >> 17) & 0x7fff) + 1;
672  clk->rate = mclk_src / mclk_div;
673 }
674 
675 /*
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
679  */
680 static void psc_clks_init(void)
681 {
682  struct device_node *np;
683  const u32 *cell_index;
684  struct platform_device *ofdev;
685 
686  for_each_compatible_node(np, NULL, "fsl,mpc5121-psc") {
687  cell_index = of_get_property(np, "cell-index", NULL);
688  if (cell_index) {
689  int pscnum = *cell_index;
690  struct clk *clk = psc_dev_clk(pscnum);
691 
693  ofdev = of_find_device_by_node(np);
694  clk->dev = &ofdev->dev;
695  /*
696  * AC97 is special rate clock does
697  * not go through normal path
698  */
699  if (strcmp("ac97", np->name) == 0)
700  clk->rate = ac97_clk.rate;
701  else
702  psc_calc_rate(clk, pscnum, np);
703  sprintf(clk->name, "psc%d_mclk", pscnum);
704  clk_register(clk);
705  clk_enable(clk);
706  }
707  }
708 }
709 
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,
720 };
721 
723 {
724  struct device_node *np;
725 
726  np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock");
727  if (np) {
728  clockctl = of_iomap(np, 0);
729  of_node_put(np);
730  }
731 
732  if (!clockctl) {
733  printk(KERN_ERR "Could not map clock control registers\n");
734  return 0;
735  }
736 
737  rate_clks_init();
738  psc_clks_init();
739 
740  /* leave clockctl mapped forever */
741  /*iounmap(clockctl); */
742  DEBUG_CLK_DUMP();
743  clocks_initialized++;
744  clk_functions = mpc5121_clk_functions;
745  return 0;
746 }