clock.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * linux/arch/arm/mach-at91/clock.c
  3. *
  4. * Copyright (C) 2005 David Brownell
  5. * Copyright (C) 2005 Ivan Kokshaysky
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/fs.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/list.h>
  19. #include <linux/errno.h>
  20. #include <linux/err.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/delay.h>
  23. #include <linux/clk.h>
  24. #include <linux/io.h>
  25. #include <mach/hardware.h>
  26. #include <mach/at91_pmc.h>
  27. #include <mach/cpu.h>
  28. #include "clock.h"
  29. /*
  30. * There's a lot more which can be done with clocks, including cpufreq
  31. * integration, slow clock mode support (for system suspend), letting
  32. * PLLB be used at other rates (on boards that don't need USB), etc.
  33. */
  34. #define clk_is_primary(x) ((x)->type & CLK_TYPE_PRIMARY)
  35. #define clk_is_programmable(x) ((x)->type & CLK_TYPE_PROGRAMMABLE)
  36. #define clk_is_peripheral(x) ((x)->type & CLK_TYPE_PERIPHERAL)
  37. #define clk_is_sys(x) ((x)->type & CLK_TYPE_SYSTEM)
  38. static LIST_HEAD(clocks);
  39. static DEFINE_SPINLOCK(clk_lock);
  40. static u32 at91_pllb_usb_init;
  41. /*
  42. * Four primary clock sources: two crystal oscillators (32K, main), and
  43. * two PLLs. PLLA usually runs the master clock; and PLLB must run at
  44. * 48 MHz (unless no USB function clocks are needed). The main clock and
  45. * both PLLs are turned off to run in "slow clock mode" (system suspend).
  46. */
  47. static struct clk clk32k = {
  48. .name = "clk32k",
  49. .rate_hz = AT91_SLOW_CLOCK,
  50. .users = 1, /* always on */
  51. .id = 0,
  52. .type = CLK_TYPE_PRIMARY,
  53. };
  54. static struct clk main_clk = {
  55. .name = "main",
  56. .pmc_mask = AT91_PMC_MOSCS, /* in PMC_SR */
  57. .id = 1,
  58. .type = CLK_TYPE_PRIMARY,
  59. };
  60. static struct clk plla = {
  61. .name = "plla",
  62. .parent = &main_clk,
  63. .pmc_mask = AT91_PMC_LOCKA, /* in PMC_SR */
  64. .id = 2,
  65. .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
  66. };
  67. static void pllb_mode(struct clk *clk, int is_on)
  68. {
  69. u32 value;
  70. if (is_on) {
  71. is_on = AT91_PMC_LOCKB;
  72. value = at91_pllb_usb_init;
  73. } else
  74. value = 0;
  75. // REVISIT: Add work-around for AT91RM9200 Errata #26 ?
  76. at91_sys_write(AT91_CKGR_PLLBR, value);
  77. do {
  78. cpu_relax();
  79. } while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on);
  80. }
  81. static struct clk pllb = {
  82. .name = "pllb",
  83. .parent = &main_clk,
  84. .pmc_mask = AT91_PMC_LOCKB, /* in PMC_SR */
  85. .mode = pllb_mode,
  86. .id = 3,
  87. .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
  88. };
  89. static void pmc_sys_mode(struct clk *clk, int is_on)
  90. {
  91. if (is_on)
  92. at91_sys_write(AT91_PMC_SCER, clk->pmc_mask);
  93. else
  94. at91_sys_write(AT91_PMC_SCDR, clk->pmc_mask);
  95. }
  96. static void pmc_uckr_mode(struct clk *clk, int is_on)
  97. {
  98. unsigned int uckr = at91_sys_read(AT91_CKGR_UCKR);
  99. if (is_on) {
  100. is_on = AT91_PMC_LOCKU;
  101. at91_sys_write(AT91_CKGR_UCKR, uckr | clk->pmc_mask);
  102. } else
  103. at91_sys_write(AT91_CKGR_UCKR, uckr & ~(clk->pmc_mask));
  104. do {
  105. cpu_relax();
  106. } while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKU) != is_on);
  107. }
  108. /* USB function clocks (PLLB must be 48 MHz) */
  109. static struct clk udpck = {
  110. .name = "udpck",
  111. .parent = &pllb,
  112. .mode = pmc_sys_mode,
  113. };
  114. static struct clk utmi_clk = {
  115. .name = "utmi_clk",
  116. .parent = &main_clk,
  117. .pmc_mask = AT91_PMC_UPLLEN, /* in CKGR_UCKR */
  118. .mode = pmc_uckr_mode,
  119. .type = CLK_TYPE_PLL,
  120. };
  121. static struct clk uhpck = {
  122. .name = "uhpck",
  123. .parent = &pllb,
  124. .mode = pmc_sys_mode,
  125. };
  126. /*
  127. * The master clock is divided from the CPU clock (by 1-4). It's used for
  128. * memory, interfaces to on-chip peripherals, the AIC, and sometimes more
  129. * (e.g baud rate generation). It's sourced from one of the primary clocks.
  130. */
  131. static struct clk mck = {
  132. .name = "mck",
  133. .pmc_mask = AT91_PMC_MCKRDY, /* in PMC_SR */
  134. };
  135. static void pmc_periph_mode(struct clk *clk, int is_on)
  136. {
  137. if (is_on)
  138. at91_sys_write(AT91_PMC_PCER, clk->pmc_mask);
  139. else
  140. at91_sys_write(AT91_PMC_PCDR, clk->pmc_mask);
  141. }
  142. static struct clk __init *at91_css_to_clk(unsigned long css)
  143. {
  144. switch (css) {
  145. case AT91_PMC_CSS_SLOW:
  146. return &clk32k;
  147. case AT91_PMC_CSS_MAIN:
  148. return &main_clk;
  149. case AT91_PMC_CSS_PLLA:
  150. return &plla;
  151. case AT91_PMC_CSS_PLLB:
  152. return &pllb;
  153. }
  154. return NULL;
  155. }
  156. /*
  157. * Associate a particular clock with a function (eg, "uart") and device.
  158. * The drivers can then request the same 'function' with several different
  159. * devices and not care about which clock name to use.
  160. */
  161. void __init at91_clock_associate(const char *id, struct device *dev, const char *func)
  162. {
  163. struct clk *clk = clk_get(NULL, id);
  164. if (!dev || !clk || !IS_ERR(clk_get(dev, func)))
  165. return;
  166. clk->function = func;
  167. clk->dev = dev;
  168. }
  169. /* clocks cannot be de-registered no refcounting necessary */
  170. struct clk *clk_get(struct device *dev, const char *id)
  171. {
  172. struct clk *clk;
  173. list_for_each_entry(clk, &clocks, node) {
  174. if (strcmp(id, clk->name) == 0)
  175. return clk;
  176. if (clk->function && (dev == clk->dev) && strcmp(id, clk->function) == 0)
  177. return clk;
  178. }
  179. return ERR_PTR(-ENOENT);
  180. }
  181. EXPORT_SYMBOL(clk_get);
  182. void clk_put(struct clk *clk)
  183. {
  184. }
  185. EXPORT_SYMBOL(clk_put);
  186. static void __clk_enable(struct clk *clk)
  187. {
  188. if (clk->parent)
  189. __clk_enable(clk->parent);
  190. if (clk->users++ == 0 && clk->mode)
  191. clk->mode(clk, 1);
  192. }
  193. int clk_enable(struct clk *clk)
  194. {
  195. unsigned long flags;
  196. spin_lock_irqsave(&clk_lock, flags);
  197. __clk_enable(clk);
  198. spin_unlock_irqrestore(&clk_lock, flags);
  199. return 0;
  200. }
  201. EXPORT_SYMBOL(clk_enable);
  202. static void __clk_disable(struct clk *clk)
  203. {
  204. BUG_ON(clk->users == 0);
  205. if (--clk->users == 0 && clk->mode)
  206. clk->mode(clk, 0);
  207. if (clk->parent)
  208. __clk_disable(clk->parent);
  209. }
  210. void clk_disable(struct clk *clk)
  211. {
  212. unsigned long flags;
  213. spin_lock_irqsave(&clk_lock, flags);
  214. __clk_disable(clk);
  215. spin_unlock_irqrestore(&clk_lock, flags);
  216. }
  217. EXPORT_SYMBOL(clk_disable);
  218. unsigned long clk_get_rate(struct clk *clk)
  219. {
  220. unsigned long flags;
  221. unsigned long rate;
  222. spin_lock_irqsave(&clk_lock, flags);
  223. for (;;) {
  224. rate = clk->rate_hz;
  225. if (rate || !clk->parent)
  226. break;
  227. clk = clk->parent;
  228. }
  229. spin_unlock_irqrestore(&clk_lock, flags);
  230. return rate;
  231. }
  232. EXPORT_SYMBOL(clk_get_rate);
  233. /*------------------------------------------------------------------------*/
  234. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  235. /*
  236. * For now, only the programmable clocks support reparenting (MCK could
  237. * do this too, with care) or rate changing (the PLLs could do this too,
  238. * ditto MCK but that's more for cpufreq). Drivers may reparent to get
  239. * a better rate match; we don't.
  240. */
  241. long clk_round_rate(struct clk *clk, unsigned long rate)
  242. {
  243. unsigned long flags;
  244. unsigned prescale;
  245. unsigned long actual;
  246. if (!clk_is_programmable(clk))
  247. return -EINVAL;
  248. spin_lock_irqsave(&clk_lock, flags);
  249. actual = clk->parent->rate_hz;
  250. for (prescale = 0; prescale < 7; prescale++) {
  251. if (actual && actual <= rate)
  252. break;
  253. actual >>= 1;
  254. }
  255. spin_unlock_irqrestore(&clk_lock, flags);
  256. return (prescale < 7) ? actual : -ENOENT;
  257. }
  258. EXPORT_SYMBOL(clk_round_rate);
  259. int clk_set_rate(struct clk *clk, unsigned long rate)
  260. {
  261. unsigned long flags;
  262. unsigned prescale;
  263. unsigned long actual;
  264. if (!clk_is_programmable(clk))
  265. return -EINVAL;
  266. if (clk->users)
  267. return -EBUSY;
  268. spin_lock_irqsave(&clk_lock, flags);
  269. actual = clk->parent->rate_hz;
  270. for (prescale = 0; prescale < 7; prescale++) {
  271. if (actual && actual <= rate) {
  272. u32 pckr;
  273. pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
  274. pckr &= AT91_PMC_CSS_PLLB; /* clock selection */
  275. pckr |= prescale << 2;
  276. at91_sys_write(AT91_PMC_PCKR(clk->id), pckr);
  277. clk->rate_hz = actual;
  278. break;
  279. }
  280. actual >>= 1;
  281. }
  282. spin_unlock_irqrestore(&clk_lock, flags);
  283. return (prescale < 7) ? actual : -ENOENT;
  284. }
  285. EXPORT_SYMBOL(clk_set_rate);
  286. struct clk *clk_get_parent(struct clk *clk)
  287. {
  288. return clk->parent;
  289. }
  290. EXPORT_SYMBOL(clk_get_parent);
  291. int clk_set_parent(struct clk *clk, struct clk *parent)
  292. {
  293. unsigned long flags;
  294. if (clk->users)
  295. return -EBUSY;
  296. if (!clk_is_primary(parent) || !clk_is_programmable(clk))
  297. return -EINVAL;
  298. spin_lock_irqsave(&clk_lock, flags);
  299. clk->rate_hz = parent->rate_hz;
  300. clk->parent = parent;
  301. at91_sys_write(AT91_PMC_PCKR(clk->id), parent->id);
  302. spin_unlock_irqrestore(&clk_lock, flags);
  303. return 0;
  304. }
  305. EXPORT_SYMBOL(clk_set_parent);
  306. /* establish PCK0..PCK3 parentage and rate */
  307. static void __init init_programmable_clock(struct clk *clk)
  308. {
  309. struct clk *parent;
  310. u32 pckr;
  311. pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
  312. parent = at91_css_to_clk(pckr & AT91_PMC_CSS);
  313. clk->parent = parent;
  314. clk->rate_hz = parent->rate_hz / (1 << ((pckr & AT91_PMC_PRES) >> 2));
  315. }
  316. #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  317. /*------------------------------------------------------------------------*/
  318. #ifdef CONFIG_DEBUG_FS
  319. static int at91_clk_show(struct seq_file *s, void *unused)
  320. {
  321. u32 scsr, pcsr, uckr = 0, sr;
  322. struct clk *clk;
  323. seq_printf(s, "SCSR = %8x\n", scsr = at91_sys_read(AT91_PMC_SCSR));
  324. seq_printf(s, "PCSR = %8x\n", pcsr = at91_sys_read(AT91_PMC_PCSR));
  325. seq_printf(s, "MOR = %8x\n", at91_sys_read(AT91_CKGR_MOR));
  326. seq_printf(s, "MCFR = %8x\n", at91_sys_read(AT91_CKGR_MCFR));
  327. seq_printf(s, "PLLA = %8x\n", at91_sys_read(AT91_CKGR_PLLAR));
  328. if (!cpu_is_at91sam9rl())
  329. seq_printf(s, "PLLB = %8x\n", at91_sys_read(AT91_CKGR_PLLBR));
  330. if (cpu_is_at91cap9() || cpu_is_at91sam9rl())
  331. seq_printf(s, "UCKR = %8x\n", uckr = at91_sys_read(AT91_CKGR_UCKR));
  332. seq_printf(s, "MCKR = %8x\n", at91_sys_read(AT91_PMC_MCKR));
  333. seq_printf(s, "SR = %8x\n", sr = at91_sys_read(AT91_PMC_SR));
  334. seq_printf(s, "\n");
  335. list_for_each_entry(clk, &clocks, node) {
  336. char *state;
  337. if (clk->mode == pmc_sys_mode)
  338. state = (scsr & clk->pmc_mask) ? "on" : "off";
  339. else if (clk->mode == pmc_periph_mode)
  340. state = (pcsr & clk->pmc_mask) ? "on" : "off";
  341. else if (clk->mode == pmc_uckr_mode)
  342. state = (uckr & clk->pmc_mask) ? "on" : "off";
  343. else if (clk->pmc_mask)
  344. state = (sr & clk->pmc_mask) ? "on" : "off";
  345. else if (clk == &clk32k || clk == &main_clk)
  346. state = "on";
  347. else
  348. state = "";
  349. seq_printf(s, "%-10s users=%2d %-3s %9ld Hz %s\n",
  350. clk->name, clk->users, state, clk_get_rate(clk),
  351. clk->parent ? clk->parent->name : "");
  352. }
  353. return 0;
  354. }
  355. static int at91_clk_open(struct inode *inode, struct file *file)
  356. {
  357. return single_open(file, at91_clk_show, NULL);
  358. }
  359. static const struct file_operations at91_clk_operations = {
  360. .open = at91_clk_open,
  361. .read = seq_read,
  362. .llseek = seq_lseek,
  363. .release = single_release,
  364. };
  365. static int __init at91_clk_debugfs_init(void)
  366. {
  367. /* /sys/kernel/debug/at91_clk */
  368. (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
  369. return 0;
  370. }
  371. postcore_initcall(at91_clk_debugfs_init);
  372. #endif
  373. /*------------------------------------------------------------------------*/
  374. /* Register a new clock */
  375. int __init clk_register(struct clk *clk)
  376. {
  377. if (clk_is_peripheral(clk)) {
  378. clk->parent = &mck;
  379. clk->mode = pmc_periph_mode;
  380. list_add_tail(&clk->node, &clocks);
  381. }
  382. else if (clk_is_sys(clk)) {
  383. clk->parent = &mck;
  384. clk->mode = pmc_sys_mode;
  385. list_add_tail(&clk->node, &clocks);
  386. }
  387. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  388. else if (clk_is_programmable(clk)) {
  389. clk->mode = pmc_sys_mode;
  390. init_programmable_clock(clk);
  391. list_add_tail(&clk->node, &clocks);
  392. }
  393. #endif
  394. return 0;
  395. }
  396. /*------------------------------------------------------------------------*/
  397. static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
  398. {
  399. unsigned mul, div;
  400. div = reg & 0xff;
  401. mul = (reg >> 16) & 0x7ff;
  402. if (div && mul) {
  403. freq /= div;
  404. freq *= mul + 1;
  405. } else
  406. freq = 0;
  407. return freq;
  408. }
  409. static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
  410. {
  411. if (pll == &pllb && (reg & AT91_PMC_USB96M))
  412. return freq / 2;
  413. else
  414. return freq;
  415. }
  416. static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
  417. {
  418. unsigned i, div = 0, mul = 0, diff = 1 << 30;
  419. unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  420. /* PLL output max 240 MHz (or 180 MHz per errata) */
  421. if (out_freq > 240000000)
  422. goto fail;
  423. for (i = 1; i < 256; i++) {
  424. int diff1;
  425. unsigned input, mul1;
  426. /*
  427. * PLL input between 1MHz and 32MHz per spec, but lower
  428. * frequences seem necessary in some cases so allow 100K.
  429. * Warning: some newer products need 2MHz min.
  430. */
  431. input = main_freq / i;
  432. if (cpu_is_at91sam9g20() && input < 2000000)
  433. continue;
  434. if (input < 100000)
  435. continue;
  436. if (input > 32000000)
  437. continue;
  438. mul1 = out_freq / input;
  439. if (cpu_is_at91sam9g20() && mul > 63)
  440. continue;
  441. if (mul1 > 2048)
  442. continue;
  443. if (mul1 < 2)
  444. goto fail;
  445. diff1 = out_freq - input * mul1;
  446. if (diff1 < 0)
  447. diff1 = -diff1;
  448. if (diff > diff1) {
  449. diff = diff1;
  450. div = i;
  451. mul = mul1;
  452. if (diff == 0)
  453. break;
  454. }
  455. }
  456. if (i == 256 && diff > (out_freq >> 5))
  457. goto fail;
  458. return ret | ((mul - 1) << 16) | div;
  459. fail:
  460. return 0;
  461. }
  462. static struct clk *const standard_pmc_clocks[] __initdata = {
  463. /* four primary clocks */
  464. &clk32k,
  465. &main_clk,
  466. &plla,
  467. &pllb,
  468. /* PLLB children (USB) */
  469. &udpck,
  470. &uhpck,
  471. /* MCK */
  472. &mck
  473. };
  474. int __init at91_clock_init(unsigned long main_clock)
  475. {
  476. unsigned tmp, freq, mckr;
  477. int i;
  478. /*
  479. * When the bootloader initialized the main oscillator correctly,
  480. * there's no problem using the cycle counter. But if it didn't,
  481. * or when using oscillator bypass mode, we must be told the speed
  482. * of the main clock.
  483. */
  484. if (!main_clock) {
  485. do {
  486. tmp = at91_sys_read(AT91_CKGR_MCFR);
  487. } while (!(tmp & AT91_PMC_MAINRDY));
  488. main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
  489. }
  490. main_clk.rate_hz = main_clock;
  491. /* report if PLLA is more than mildly overclocked */
  492. plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR));
  493. if ((!cpu_is_at91sam9g20() && plla.rate_hz > 209000000)
  494. || (cpu_is_at91sam9g20() && plla.rate_hz > 800000000))
  495. pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
  496. /*
  497. * USB clock init: choose 48 MHz PLLB value,
  498. * disable 48MHz clock during usb peripheral suspend.
  499. *
  500. * REVISIT: assumes MCK doesn't derive from PLLB!
  501. */
  502. at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M;
  503. pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
  504. if (cpu_is_at91rm9200()) {
  505. uhpck.pmc_mask = AT91RM9200_PMC_UHP;
  506. udpck.pmc_mask = AT91RM9200_PMC_UDP;
  507. at91_sys_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP);
  508. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) {
  509. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  510. udpck.pmc_mask = AT91SAM926x_PMC_UDP;
  511. } else if (cpu_is_at91cap9()) {
  512. uhpck.pmc_mask = AT91CAP9_PMC_UHP;
  513. }
  514. at91_sys_write(AT91_CKGR_PLLBR, 0);
  515. udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  516. uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  517. /*
  518. * USB HS clock init
  519. */
  520. if (cpu_is_at91cap9() || cpu_is_at91sam9rl()) {
  521. /*
  522. * multiplier is hard-wired to 40
  523. * (obtain the USB High Speed 480 MHz when input is 12 MHz)
  524. */
  525. utmi_clk.rate_hz = 40 * utmi_clk.parent->rate_hz;
  526. }
  527. /*
  528. * MCK and CPU derive from one of those primary clocks.
  529. * For now, assume this parentage won't change.
  530. */
  531. mckr = at91_sys_read(AT91_PMC_MCKR);
  532. mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
  533. freq = mck.parent->rate_hz;
  534. freq /= (1 << ((mckr & AT91_PMC_PRES) >> 2)); /* prescale */
  535. if (cpu_is_at91rm9200())
  536. mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  537. else if (cpu_is_at91sam9g20()) {
  538. mck.rate_hz = (mckr & AT91_PMC_MDIV) ?
  539. freq / ((mckr & AT91_PMC_MDIV) >> 7) : freq; /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
  540. if (mckr & AT91_PMC_PDIV)
  541. freq /= 2; /* processor clock division */
  542. } else
  543. mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  544. /* Register the PMC's standard clocks */
  545. for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
  546. list_add_tail(&standard_pmc_clocks[i]->node, &clocks);
  547. if (cpu_is_at91cap9() || cpu_is_at91sam9rl())
  548. list_add_tail(&utmi_clk.node, &clocks);
  549. /* MCK and CPU clock are "always on" */
  550. clk_enable(&mck);
  551. printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
  552. freq / 1000000, (unsigned) mck.rate_hz / 1000000,
  553. (unsigned) main_clock / 1000000,
  554. ((unsigned) main_clock % 1000000) / 1000);
  555. return 0;
  556. }
  557. /*
  558. * Several unused clocks may be active. Turn them off.
  559. */
  560. static int __init at91_clock_reset(void)
  561. {
  562. unsigned long pcdr = 0;
  563. unsigned long scdr = 0;
  564. struct clk *clk;
  565. list_for_each_entry(clk, &clocks, node) {
  566. if (clk->users > 0)
  567. continue;
  568. if (clk->mode == pmc_periph_mode)
  569. pcdr |= clk->pmc_mask;
  570. if (clk->mode == pmc_sys_mode)
  571. scdr |= clk->pmc_mask;
  572. pr_debug("Clocks: disable unused %s\n", clk->name);
  573. }
  574. at91_sys_write(AT91_PMC_PCDR, pcdr);
  575. at91_sys_write(AT91_PMC_SCDR, scdr);
  576. return 0;
  577. }
  578. late_initcall(at91_clock_reset);