clock.c 20 KB

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