clock.c 18 KB

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