clock.c 20 KB

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