clock.c 21 KB

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