clock.c 21 KB

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