clock.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. static 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. static 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. /*
  185. * Associate a particular clock with a function (eg, "uart") and device.
  186. * The drivers can then request the same 'function' with several different
  187. * devices and not care about which clock name to use.
  188. */
  189. void __init at91_clock_associate(const char *id, struct device *dev, const char *func)
  190. {
  191. struct clk *clk = clk_get(NULL, id);
  192. if (!dev || !clk || !IS_ERR(clk_get(dev, func)))
  193. return;
  194. clk->function = func;
  195. clk->dev = dev;
  196. }
  197. /* clocks cannot be de-registered no refcounting necessary */
  198. struct clk *clk_get(struct device *dev, const char *id)
  199. {
  200. struct clk *clk;
  201. list_for_each_entry(clk, &clocks, node) {
  202. if (strcmp(id, clk->name) == 0)
  203. return clk;
  204. if (clk->function && (dev == clk->dev) && strcmp(id, clk->function) == 0)
  205. return clk;
  206. }
  207. return ERR_PTR(-ENOENT);
  208. }
  209. EXPORT_SYMBOL(clk_get);
  210. void clk_put(struct clk *clk)
  211. {
  212. }
  213. EXPORT_SYMBOL(clk_put);
  214. static void __clk_enable(struct clk *clk)
  215. {
  216. if (clk->parent)
  217. __clk_enable(clk->parent);
  218. if (clk->users++ == 0 && clk->mode)
  219. clk->mode(clk, 1);
  220. }
  221. int clk_enable(struct clk *clk)
  222. {
  223. unsigned long flags;
  224. spin_lock_irqsave(&clk_lock, flags);
  225. __clk_enable(clk);
  226. spin_unlock_irqrestore(&clk_lock, flags);
  227. return 0;
  228. }
  229. EXPORT_SYMBOL(clk_enable);
  230. static void __clk_disable(struct clk *clk)
  231. {
  232. BUG_ON(clk->users == 0);
  233. if (--clk->users == 0 && clk->mode)
  234. clk->mode(clk, 0);
  235. if (clk->parent)
  236. __clk_disable(clk->parent);
  237. }
  238. void clk_disable(struct clk *clk)
  239. {
  240. unsigned long flags;
  241. spin_lock_irqsave(&clk_lock, flags);
  242. __clk_disable(clk);
  243. spin_unlock_irqrestore(&clk_lock, flags);
  244. }
  245. EXPORT_SYMBOL(clk_disable);
  246. unsigned long clk_get_rate(struct clk *clk)
  247. {
  248. unsigned long flags;
  249. unsigned long rate;
  250. spin_lock_irqsave(&clk_lock, flags);
  251. for (;;) {
  252. rate = clk->rate_hz;
  253. if (rate || !clk->parent)
  254. break;
  255. clk = clk->parent;
  256. }
  257. spin_unlock_irqrestore(&clk_lock, flags);
  258. return rate;
  259. }
  260. EXPORT_SYMBOL(clk_get_rate);
  261. /*------------------------------------------------------------------------*/
  262. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  263. /*
  264. * For now, only the programmable clocks support reparenting (MCK could
  265. * do this too, with care) or rate changing (the PLLs could do this too,
  266. * ditto MCK but that's more for cpufreq). Drivers may reparent to get
  267. * a better rate match; we don't.
  268. */
  269. long clk_round_rate(struct clk *clk, unsigned long rate)
  270. {
  271. unsigned long flags;
  272. unsigned prescale;
  273. unsigned long actual;
  274. unsigned long prev = ULONG_MAX;
  275. if (!clk_is_programmable(clk))
  276. return -EINVAL;
  277. spin_lock_irqsave(&clk_lock, flags);
  278. actual = clk->parent->rate_hz;
  279. for (prescale = 0; prescale < 7; prescale++) {
  280. if (actual > rate)
  281. prev = actual;
  282. if (actual && actual <= rate) {
  283. if ((prev - rate) < (rate - actual)) {
  284. actual = prev;
  285. prescale--;
  286. }
  287. break;
  288. }
  289. actual >>= 1;
  290. }
  291. spin_unlock_irqrestore(&clk_lock, flags);
  292. return (prescale < 7) ? actual : -ENOENT;
  293. }
  294. EXPORT_SYMBOL(clk_round_rate);
  295. int clk_set_rate(struct clk *clk, unsigned long rate)
  296. {
  297. unsigned long flags;
  298. unsigned prescale;
  299. unsigned long actual;
  300. if (!clk_is_programmable(clk))
  301. return -EINVAL;
  302. if (clk->users)
  303. return -EBUSY;
  304. spin_lock_irqsave(&clk_lock, flags);
  305. actual = clk->parent->rate_hz;
  306. for (prescale = 0; prescale < 7; prescale++) {
  307. if (actual && actual <= rate) {
  308. u32 pckr;
  309. pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
  310. pckr &= AT91_PMC_CSS; /* clock selection */
  311. pckr |= prescale << 2;
  312. at91_sys_write(AT91_PMC_PCKR(clk->id), pckr);
  313. clk->rate_hz = actual;
  314. break;
  315. }
  316. actual >>= 1;
  317. }
  318. spin_unlock_irqrestore(&clk_lock, flags);
  319. return (prescale < 7) ? actual : -ENOENT;
  320. }
  321. EXPORT_SYMBOL(clk_set_rate);
  322. struct clk *clk_get_parent(struct clk *clk)
  323. {
  324. return clk->parent;
  325. }
  326. EXPORT_SYMBOL(clk_get_parent);
  327. int clk_set_parent(struct clk *clk, struct clk *parent)
  328. {
  329. unsigned long flags;
  330. if (clk->users)
  331. return -EBUSY;
  332. if (!clk_is_primary(parent) || !clk_is_programmable(clk))
  333. return -EINVAL;
  334. if (cpu_is_at91sam9rl() && parent->id == AT91_PMC_CSS_PLLB)
  335. return -EINVAL;
  336. spin_lock_irqsave(&clk_lock, flags);
  337. clk->rate_hz = parent->rate_hz;
  338. clk->parent = parent;
  339. at91_sys_write(AT91_PMC_PCKR(clk->id), parent->id);
  340. spin_unlock_irqrestore(&clk_lock, flags);
  341. return 0;
  342. }
  343. EXPORT_SYMBOL(clk_set_parent);
  344. /* establish PCK0..PCKN parentage and rate */
  345. static void __init init_programmable_clock(struct clk *clk)
  346. {
  347. struct clk *parent;
  348. u32 pckr;
  349. pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
  350. parent = at91_css_to_clk(pckr & AT91_PMC_CSS);
  351. clk->parent = parent;
  352. clk->rate_hz = parent->rate_hz / (1 << ((pckr & AT91_PMC_PRES) >> 2));
  353. }
  354. #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  355. /*------------------------------------------------------------------------*/
  356. #ifdef CONFIG_DEBUG_FS
  357. static int at91_clk_show(struct seq_file *s, void *unused)
  358. {
  359. u32 scsr, pcsr, uckr = 0, sr;
  360. struct clk *clk;
  361. seq_printf(s, "SCSR = %8x\n", scsr = at91_sys_read(AT91_PMC_SCSR));
  362. seq_printf(s, "PCSR = %8x\n", pcsr = at91_sys_read(AT91_PMC_PCSR));
  363. seq_printf(s, "MOR = %8x\n", at91_sys_read(AT91_CKGR_MOR));
  364. seq_printf(s, "MCFR = %8x\n", at91_sys_read(AT91_CKGR_MCFR));
  365. seq_printf(s, "PLLA = %8x\n", at91_sys_read(AT91_CKGR_PLLAR));
  366. if (cpu_has_pllb())
  367. seq_printf(s, "PLLB = %8x\n", at91_sys_read(AT91_CKGR_PLLBR));
  368. if (cpu_has_utmi())
  369. seq_printf(s, "UCKR = %8x\n", uckr = at91_sys_read(AT91_CKGR_UCKR));
  370. seq_printf(s, "MCKR = %8x\n", at91_sys_read(AT91_PMC_MCKR));
  371. if (cpu_has_upll())
  372. seq_printf(s, "USB = %8x\n", at91_sys_read(AT91_PMC_USB));
  373. seq_printf(s, "SR = %8x\n", sr = at91_sys_read(AT91_PMC_SR));
  374. seq_printf(s, "\n");
  375. list_for_each_entry(clk, &clocks, node) {
  376. char *state;
  377. if (clk->mode == pmc_sys_mode)
  378. state = (scsr & clk->pmc_mask) ? "on" : "off";
  379. else if (clk->mode == pmc_periph_mode)
  380. state = (pcsr & clk->pmc_mask) ? "on" : "off";
  381. else if (clk->mode == pmc_uckr_mode)
  382. state = (uckr & clk->pmc_mask) ? "on" : "off";
  383. else if (clk->pmc_mask)
  384. state = (sr & clk->pmc_mask) ? "on" : "off";
  385. else if (clk == &clk32k || clk == &main_clk)
  386. state = "on";
  387. else
  388. state = "";
  389. seq_printf(s, "%-10s users=%2d %-3s %9ld Hz %s\n",
  390. clk->name, clk->users, state, clk_get_rate(clk),
  391. clk->parent ? clk->parent->name : "");
  392. }
  393. return 0;
  394. }
  395. static int at91_clk_open(struct inode *inode, struct file *file)
  396. {
  397. return single_open(file, at91_clk_show, NULL);
  398. }
  399. static const struct file_operations at91_clk_operations = {
  400. .open = at91_clk_open,
  401. .read = seq_read,
  402. .llseek = seq_lseek,
  403. .release = single_release,
  404. };
  405. static int __init at91_clk_debugfs_init(void)
  406. {
  407. /* /sys/kernel/debug/at91_clk */
  408. (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
  409. return 0;
  410. }
  411. postcore_initcall(at91_clk_debugfs_init);
  412. #endif
  413. /*------------------------------------------------------------------------*/
  414. /* Register a new clock */
  415. int __init clk_register(struct clk *clk)
  416. {
  417. if (clk_is_peripheral(clk)) {
  418. clk->parent = &mck;
  419. clk->mode = pmc_periph_mode;
  420. list_add_tail(&clk->node, &clocks);
  421. }
  422. else if (clk_is_sys(clk)) {
  423. clk->parent = &mck;
  424. clk->mode = pmc_sys_mode;
  425. list_add_tail(&clk->node, &clocks);
  426. }
  427. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  428. else if (clk_is_programmable(clk)) {
  429. clk->mode = pmc_sys_mode;
  430. init_programmable_clock(clk);
  431. list_add_tail(&clk->node, &clocks);
  432. }
  433. #endif
  434. return 0;
  435. }
  436. /*------------------------------------------------------------------------*/
  437. static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
  438. {
  439. unsigned mul, div;
  440. div = reg & 0xff;
  441. mul = (reg >> 16) & 0x7ff;
  442. if (div && mul) {
  443. freq /= div;
  444. freq *= mul + 1;
  445. } else
  446. freq = 0;
  447. return freq;
  448. }
  449. static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
  450. {
  451. if (pll == &pllb && (reg & AT91_PMC_USB96M))
  452. return freq / 2;
  453. else
  454. return freq;
  455. }
  456. static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
  457. {
  458. unsigned i, div = 0, mul = 0, diff = 1 << 30;
  459. unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  460. /* PLL output max 240 MHz (or 180 MHz per errata) */
  461. if (out_freq > 240000000)
  462. goto fail;
  463. for (i = 1; i < 256; i++) {
  464. int diff1;
  465. unsigned input, mul1;
  466. /*
  467. * PLL input between 1MHz and 32MHz per spec, but lower
  468. * frequences seem necessary in some cases so allow 100K.
  469. * Warning: some newer products need 2MHz min.
  470. */
  471. input = main_freq / i;
  472. if (cpu_is_at91sam9g20() && input < 2000000)
  473. continue;
  474. if (input < 100000)
  475. continue;
  476. if (input > 32000000)
  477. continue;
  478. mul1 = out_freq / input;
  479. if (cpu_is_at91sam9g20() && mul > 63)
  480. continue;
  481. if (mul1 > 2048)
  482. continue;
  483. if (mul1 < 2)
  484. goto fail;
  485. diff1 = out_freq - input * mul1;
  486. if (diff1 < 0)
  487. diff1 = -diff1;
  488. if (diff > diff1) {
  489. diff = diff1;
  490. div = i;
  491. mul = mul1;
  492. if (diff == 0)
  493. break;
  494. }
  495. }
  496. if (i == 256 && diff > (out_freq >> 5))
  497. goto fail;
  498. return ret | ((mul - 1) << 16) | div;
  499. fail:
  500. return 0;
  501. }
  502. static struct clk *const standard_pmc_clocks[] __initdata = {
  503. /* four primary clocks */
  504. &clk32k,
  505. &main_clk,
  506. &plla,
  507. /* MCK */
  508. &mck
  509. };
  510. /* PLLB generated USB full speed clock init */
  511. static void __init at91_pllb_usbfs_clock_init(unsigned long main_clock)
  512. {
  513. /*
  514. * USB clock init: choose 48 MHz PLLB value,
  515. * disable 48MHz clock during usb peripheral suspend.
  516. *
  517. * REVISIT: assumes MCK doesn't derive from PLLB!
  518. */
  519. uhpck.parent = &pllb;
  520. at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M;
  521. pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
  522. if (cpu_is_at91rm9200()) {
  523. uhpck.pmc_mask = AT91RM9200_PMC_UHP;
  524. udpck.pmc_mask = AT91RM9200_PMC_UDP;
  525. at91_sys_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP);
  526. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() ||
  527. cpu_is_at91sam9263() || cpu_is_at91sam9g20() ||
  528. cpu_is_at91sam9g10() || cpu_is_at572d940hf()) {
  529. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  530. udpck.pmc_mask = AT91SAM926x_PMC_UDP;
  531. } else if (cpu_is_at91cap9()) {
  532. uhpck.pmc_mask = AT91CAP9_PMC_UHP;
  533. }
  534. at91_sys_write(AT91_CKGR_PLLBR, 0);
  535. udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  536. uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  537. }
  538. /* UPLL generated USB full speed clock init */
  539. static void __init at91_upll_usbfs_clock_init(unsigned long main_clock)
  540. {
  541. /*
  542. * USB clock init: choose 480 MHz from UPLL,
  543. */
  544. unsigned int usbr = AT91_PMC_USBS_UPLL;
  545. /* Setup divider by 10 to reach 48 MHz */
  546. usbr |= ((10 - 1) << 8) & AT91_PMC_OHCIUSBDIV;
  547. at91_sys_write(AT91_PMC_USB, usbr);
  548. /* Now set uhpck values */
  549. uhpck.parent = &utmi_clk;
  550. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  551. uhpck.rate_hz = utmi_clk.parent->rate_hz;
  552. uhpck.rate_hz /= 1 + ((at91_sys_read(AT91_PMC_USB) & AT91_PMC_OHCIUSBDIV) >> 8);
  553. }
  554. int __init at91_clock_init(unsigned long main_clock)
  555. {
  556. unsigned tmp, freq, mckr;
  557. int i;
  558. int pll_overclock = false;
  559. /*
  560. * When the bootloader initialized the main oscillator correctly,
  561. * there's no problem using the cycle counter. But if it didn't,
  562. * or when using oscillator bypass mode, we must be told the speed
  563. * of the main clock.
  564. */
  565. if (!main_clock) {
  566. do {
  567. tmp = at91_sys_read(AT91_CKGR_MCFR);
  568. } while (!(tmp & AT91_PMC_MAINRDY));
  569. main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
  570. }
  571. main_clk.rate_hz = main_clock;
  572. /* report if PLLA is more than mildly overclocked */
  573. plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR));
  574. if (cpu_has_300M_plla()) {
  575. if (plla.rate_hz > 300000000)
  576. pll_overclock = true;
  577. } else if (cpu_has_800M_plla()) {
  578. if (plla.rate_hz > 800000000)
  579. pll_overclock = true;
  580. } else {
  581. if (plla.rate_hz > 209000000)
  582. pll_overclock = true;
  583. }
  584. if (pll_overclock)
  585. pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
  586. if (cpu_is_at91sam9g45()) {
  587. mckr = at91_sys_read(AT91_PMC_MCKR);
  588. plla.rate_hz /= (1 << ((mckr & AT91_PMC_PLLADIV2) >> 12)); /* plla divisor by 2 */
  589. }
  590. if (!cpu_has_pllb() && cpu_has_upll()) {
  591. /* setup UTMI clock as the fourth primary clock
  592. * (instead of pllb) */
  593. utmi_clk.type |= CLK_TYPE_PRIMARY;
  594. utmi_clk.id = 3;
  595. }
  596. /*
  597. * USB HS clock init
  598. */
  599. if (cpu_has_utmi()) {
  600. /*
  601. * multiplier is hard-wired to 40
  602. * (obtain the USB High Speed 480 MHz when input is 12 MHz)
  603. */
  604. utmi_clk.rate_hz = 40 * utmi_clk.parent->rate_hz;
  605. }
  606. /*
  607. * USB FS clock init
  608. */
  609. if (cpu_has_pllb())
  610. at91_pllb_usbfs_clock_init(main_clock);
  611. if (cpu_has_upll())
  612. /* assumes that we choose UPLL for USB and not PLLA */
  613. at91_upll_usbfs_clock_init(main_clock);
  614. /*
  615. * MCK and CPU derive from one of those primary clocks.
  616. * For now, assume this parentage won't change.
  617. */
  618. mckr = at91_sys_read(AT91_PMC_MCKR);
  619. mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
  620. freq = mck.parent->rate_hz;
  621. freq /= (1 << ((mckr & AT91_PMC_PRES) >> 2)); /* prescale */
  622. if (cpu_is_at91rm9200()) {
  623. mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  624. } else if (cpu_is_at91sam9g20()) {
  625. mck.rate_hz = (mckr & AT91_PMC_MDIV) ?
  626. freq / ((mckr & AT91_PMC_MDIV) >> 7) : freq; /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
  627. if (mckr & AT91_PMC_PDIV)
  628. freq /= 2; /* processor clock division */
  629. } else if (cpu_is_at91sam9g45()) {
  630. mck.rate_hz = (mckr & AT91_PMC_MDIV) == AT91SAM9_PMC_MDIV_3 ?
  631. freq / 3 : freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  632. } else {
  633. mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  634. }
  635. /* Register the PMC's standard clocks */
  636. for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
  637. list_add_tail(&standard_pmc_clocks[i]->node, &clocks);
  638. if (cpu_has_pllb())
  639. list_add_tail(&pllb.node, &clocks);
  640. if (cpu_has_uhp())
  641. list_add_tail(&uhpck.node, &clocks);
  642. if (cpu_has_udpfs())
  643. list_add_tail(&udpck.node, &clocks);
  644. if (cpu_has_utmi())
  645. list_add_tail(&utmi_clk.node, &clocks);
  646. /* MCK and CPU clock are "always on" */
  647. clk_enable(&mck);
  648. printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
  649. freq / 1000000, (unsigned) mck.rate_hz / 1000000,
  650. (unsigned) main_clock / 1000000,
  651. ((unsigned) main_clock % 1000000) / 1000);
  652. return 0;
  653. }
  654. /*
  655. * Several unused clocks may be active. Turn them off.
  656. */
  657. static int __init at91_clock_reset(void)
  658. {
  659. unsigned long pcdr = 0;
  660. unsigned long scdr = 0;
  661. struct clk *clk;
  662. list_for_each_entry(clk, &clocks, node) {
  663. if (clk->users > 0)
  664. continue;
  665. if (clk->mode == pmc_periph_mode)
  666. pcdr |= clk->pmc_mask;
  667. if (clk->mode == pmc_sys_mode)
  668. scdr |= clk->pmc_mask;
  669. pr_debug("Clocks: disable unused %s\n", clk->name);
  670. }
  671. at91_sys_write(AT91_PMC_PCDR, pcdr);
  672. at91_sys_write(AT91_PMC_SCDR, scdr);
  673. return 0;
  674. }
  675. late_initcall(at91_clock_reset);