clock.c 21 KB

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