clock.c 21 KB

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