clock.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. if (!clk->parent)
  419. clk->parent = &mck;
  420. clk->mode = pmc_periph_mode;
  421. list_add_tail(&clk->node, &clocks);
  422. }
  423. else if (clk_is_sys(clk)) {
  424. clk->parent = &mck;
  425. clk->mode = pmc_sys_mode;
  426. list_add_tail(&clk->node, &clocks);
  427. }
  428. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  429. else if (clk_is_programmable(clk)) {
  430. clk->mode = pmc_sys_mode;
  431. init_programmable_clock(clk);
  432. list_add_tail(&clk->node, &clocks);
  433. }
  434. #endif
  435. return 0;
  436. }
  437. /*------------------------------------------------------------------------*/
  438. static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
  439. {
  440. unsigned mul, div;
  441. div = reg & 0xff;
  442. mul = (reg >> 16) & 0x7ff;
  443. if (div && mul) {
  444. freq /= div;
  445. freq *= mul + 1;
  446. } else
  447. freq = 0;
  448. return freq;
  449. }
  450. static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
  451. {
  452. if (pll == &pllb && (reg & AT91_PMC_USB96M))
  453. return freq / 2;
  454. else
  455. return freq;
  456. }
  457. static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
  458. {
  459. unsigned i, div = 0, mul = 0, diff = 1 << 30;
  460. unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  461. /* PLL output max 240 MHz (or 180 MHz per errata) */
  462. if (out_freq > 240000000)
  463. goto fail;
  464. for (i = 1; i < 256; i++) {
  465. int diff1;
  466. unsigned input, mul1;
  467. /*
  468. * PLL input between 1MHz and 32MHz per spec, but lower
  469. * frequences seem necessary in some cases so allow 100K.
  470. * Warning: some newer products need 2MHz min.
  471. */
  472. input = main_freq / i;
  473. if (cpu_is_at91sam9g20() && input < 2000000)
  474. continue;
  475. if (input < 100000)
  476. continue;
  477. if (input > 32000000)
  478. continue;
  479. mul1 = out_freq / input;
  480. if (cpu_is_at91sam9g20() && mul > 63)
  481. continue;
  482. if (mul1 > 2048)
  483. continue;
  484. if (mul1 < 2)
  485. goto fail;
  486. diff1 = out_freq - input * mul1;
  487. if (diff1 < 0)
  488. diff1 = -diff1;
  489. if (diff > diff1) {
  490. diff = diff1;
  491. div = i;
  492. mul = mul1;
  493. if (diff == 0)
  494. break;
  495. }
  496. }
  497. if (i == 256 && diff > (out_freq >> 5))
  498. goto fail;
  499. return ret | ((mul - 1) << 16) | div;
  500. fail:
  501. return 0;
  502. }
  503. static struct clk *const standard_pmc_clocks[] __initdata = {
  504. /* four primary clocks */
  505. &clk32k,
  506. &main_clk,
  507. &plla,
  508. /* MCK */
  509. &mck
  510. };
  511. /* PLLB generated USB full speed clock init */
  512. static void __init at91_pllb_usbfs_clock_init(unsigned long main_clock)
  513. {
  514. /*
  515. * USB clock init: choose 48 MHz PLLB value,
  516. * disable 48MHz clock during usb peripheral suspend.
  517. *
  518. * REVISIT: assumes MCK doesn't derive from PLLB!
  519. */
  520. uhpck.parent = &pllb;
  521. at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M;
  522. pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
  523. if (cpu_is_at91rm9200()) {
  524. uhpck.pmc_mask = AT91RM9200_PMC_UHP;
  525. udpck.pmc_mask = AT91RM9200_PMC_UDP;
  526. at91_sys_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP);
  527. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() ||
  528. cpu_is_at91sam9263() || cpu_is_at91sam9g20() ||
  529. cpu_is_at91sam9g10() || cpu_is_at572d940hf()) {
  530. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  531. udpck.pmc_mask = AT91SAM926x_PMC_UDP;
  532. } else if (cpu_is_at91cap9()) {
  533. uhpck.pmc_mask = AT91CAP9_PMC_UHP;
  534. }
  535. at91_sys_write(AT91_CKGR_PLLBR, 0);
  536. udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  537. uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  538. }
  539. /* UPLL generated USB full speed clock init */
  540. static void __init at91_upll_usbfs_clock_init(unsigned long main_clock)
  541. {
  542. /*
  543. * USB clock init: choose 480 MHz from UPLL,
  544. */
  545. unsigned int usbr = AT91_PMC_USBS_UPLL;
  546. /* Setup divider by 10 to reach 48 MHz */
  547. usbr |= ((10 - 1) << 8) & AT91_PMC_OHCIUSBDIV;
  548. at91_sys_write(AT91_PMC_USB, usbr);
  549. /* Now set uhpck values */
  550. uhpck.parent = &utmi_clk;
  551. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  552. uhpck.rate_hz = utmi_clk.parent->rate_hz;
  553. uhpck.rate_hz /= 1 + ((at91_sys_read(AT91_PMC_USB) & AT91_PMC_OHCIUSBDIV) >> 8);
  554. }
  555. int __init at91_clock_init(unsigned long main_clock)
  556. {
  557. unsigned tmp, freq, mckr;
  558. int i;
  559. int pll_overclock = false;
  560. /*
  561. * When the bootloader initialized the main oscillator correctly,
  562. * there's no problem using the cycle counter. But if it didn't,
  563. * or when using oscillator bypass mode, we must be told the speed
  564. * of the main clock.
  565. */
  566. if (!main_clock) {
  567. do {
  568. tmp = at91_sys_read(AT91_CKGR_MCFR);
  569. } while (!(tmp & AT91_PMC_MAINRDY));
  570. main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
  571. }
  572. main_clk.rate_hz = main_clock;
  573. /* report if PLLA is more than mildly overclocked */
  574. plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR));
  575. if (cpu_has_300M_plla()) {
  576. if (plla.rate_hz > 300000000)
  577. pll_overclock = true;
  578. } else if (cpu_has_800M_plla()) {
  579. if (plla.rate_hz > 800000000)
  580. pll_overclock = true;
  581. } else {
  582. if (plla.rate_hz > 209000000)
  583. pll_overclock = true;
  584. }
  585. if (pll_overclock)
  586. pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
  587. if (cpu_is_at91sam9g45()) {
  588. mckr = at91_sys_read(AT91_PMC_MCKR);
  589. plla.rate_hz /= (1 << ((mckr & AT91_PMC_PLLADIV2) >> 12)); /* plla divisor by 2 */
  590. }
  591. if (!cpu_has_pllb() && cpu_has_upll()) {
  592. /* setup UTMI clock as the fourth primary clock
  593. * (instead of pllb) */
  594. utmi_clk.type |= CLK_TYPE_PRIMARY;
  595. utmi_clk.id = 3;
  596. }
  597. /*
  598. * USB HS clock init
  599. */
  600. if (cpu_has_utmi()) {
  601. /*
  602. * multiplier is hard-wired to 40
  603. * (obtain the USB High Speed 480 MHz when input is 12 MHz)
  604. */
  605. utmi_clk.rate_hz = 40 * utmi_clk.parent->rate_hz;
  606. }
  607. /*
  608. * USB FS clock init
  609. */
  610. if (cpu_has_pllb())
  611. at91_pllb_usbfs_clock_init(main_clock);
  612. if (cpu_has_upll())
  613. /* assumes that we choose UPLL for USB and not PLLA */
  614. at91_upll_usbfs_clock_init(main_clock);
  615. /*
  616. * MCK and CPU derive from one of those primary clocks.
  617. * For now, assume this parentage won't change.
  618. */
  619. mckr = at91_sys_read(AT91_PMC_MCKR);
  620. mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
  621. freq = mck.parent->rate_hz;
  622. freq /= (1 << ((mckr & AT91_PMC_PRES) >> 2)); /* prescale */
  623. if (cpu_is_at91rm9200()) {
  624. mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  625. } else if (cpu_is_at91sam9g20()) {
  626. mck.rate_hz = (mckr & AT91_PMC_MDIV) ?
  627. freq / ((mckr & AT91_PMC_MDIV) >> 7) : freq; /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
  628. if (mckr & AT91_PMC_PDIV)
  629. freq /= 2; /* processor clock division */
  630. } else if (cpu_is_at91sam9g45()) {
  631. mck.rate_hz = (mckr & AT91_PMC_MDIV) == AT91SAM9_PMC_MDIV_3 ?
  632. freq / 3 : freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  633. } else {
  634. mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  635. }
  636. /* Register the PMC's standard clocks */
  637. for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
  638. list_add_tail(&standard_pmc_clocks[i]->node, &clocks);
  639. if (cpu_has_pllb())
  640. list_add_tail(&pllb.node, &clocks);
  641. if (cpu_has_uhp())
  642. list_add_tail(&uhpck.node, &clocks);
  643. if (cpu_has_udpfs())
  644. list_add_tail(&udpck.node, &clocks);
  645. if (cpu_has_utmi())
  646. list_add_tail(&utmi_clk.node, &clocks);
  647. /* MCK and CPU clock are "always on" */
  648. clk_enable(&mck);
  649. printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
  650. freq / 1000000, (unsigned) mck.rate_hz / 1000000,
  651. (unsigned) main_clock / 1000000,
  652. ((unsigned) main_clock % 1000000) / 1000);
  653. return 0;
  654. }
  655. /*
  656. * Several unused clocks may be active. Turn them off.
  657. */
  658. static int __init at91_clock_reset(void)
  659. {
  660. unsigned long pcdr = 0;
  661. unsigned long scdr = 0;
  662. struct clk *clk;
  663. list_for_each_entry(clk, &clocks, node) {
  664. if (clk->users > 0)
  665. continue;
  666. if (clk->mode == pmc_periph_mode)
  667. pcdr |= clk->pmc_mask;
  668. if (clk->mode == pmc_sys_mode)
  669. scdr |= clk->pmc_mask;
  670. pr_debug("Clocks: disable unused %s\n", clk->name);
  671. }
  672. at91_sys_write(AT91_PMC_PCDR, pcdr);
  673. at91_sys_write(AT91_PMC_SCDR, scdr);
  674. return 0;
  675. }
  676. late_initcall(at91_clock_reset);