clock.c 17 KB

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