clock.c 19 KB

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