clock.c 19 KB

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