clock.c 21 KB

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