clock.c 20 KB

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