clock.c 19 KB

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