clock.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * linux/arch/arm/mach-at91rm9200/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 <asm/semaphore.h>
  25. #include <asm/io.h>
  26. #include <asm/mach-types.h>
  27. #include <asm/hardware.h>
  28. #include "generic.h"
  29. /*
  30. * There's a lot more which can be done with clocks, including cpufreq
  31. * integration, slow clock mode support (for system suspend), letting
  32. * PLLB be used at other rates (on boards that don't need USB), etc.
  33. */
  34. struct clk {
  35. const char *name; /* unique clock name */
  36. const char *function; /* function of the clock */
  37. struct device *dev; /* device associated with function */
  38. unsigned long rate_hz;
  39. struct clk *parent;
  40. u32 pmc_mask;
  41. void (*mode)(struct clk *, int);
  42. unsigned id:2; /* PCK0..3, or 32k/main/a/b */
  43. unsigned primary:1;
  44. unsigned pll:1;
  45. unsigned programmable:1;
  46. u16 users;
  47. };
  48. static spinlock_t clk_lock;
  49. static u32 at91_pllb_usb_init;
  50. /*
  51. * Four primary clock sources: two crystal oscillators (32K, main), and
  52. * two PLLs. PLLA usually runs the master clock; and PLLB must run at
  53. * 48 MHz (unless no USB function clocks are needed). The main clock and
  54. * both PLLs are turned off to run in "slow clock mode" (system suspend).
  55. */
  56. static struct clk clk32k = {
  57. .name = "clk32k",
  58. .rate_hz = AT91_SLOW_CLOCK,
  59. .users = 1, /* always on */
  60. .id = 0,
  61. .primary = 1,
  62. };
  63. static struct clk main_clk = {
  64. .name = "main",
  65. .pmc_mask = AT91_PMC_MOSCS, /* in PMC_SR */
  66. .id = 1,
  67. .primary = 1,
  68. };
  69. static struct clk plla = {
  70. .name = "plla",
  71. .parent = &main_clk,
  72. .pmc_mask = AT91_PMC_LOCKA, /* in PMC_SR */
  73. .id = 2,
  74. .primary = 1,
  75. .pll = 1,
  76. };
  77. static void pllb_mode(struct clk *clk, int is_on)
  78. {
  79. u32 value;
  80. if (is_on) {
  81. is_on = AT91_PMC_LOCKB;
  82. value = at91_pllb_usb_init;
  83. } else
  84. value = 0;
  85. at91_sys_write(AT91_CKGR_PLLBR, value);
  86. do {
  87. cpu_relax();
  88. } while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on);
  89. }
  90. static struct clk pllb = {
  91. .name = "pllb",
  92. .parent = &main_clk,
  93. .pmc_mask = AT91_PMC_LOCKB, /* in PMC_SR */
  94. .mode = pllb_mode,
  95. .id = 3,
  96. .primary = 1,
  97. .pll = 1,
  98. };
  99. static void pmc_sys_mode(struct clk *clk, int is_on)
  100. {
  101. if (is_on)
  102. at91_sys_write(AT91_PMC_SCER, clk->pmc_mask);
  103. else
  104. at91_sys_write(AT91_PMC_SCDR, clk->pmc_mask);
  105. }
  106. /* USB function clocks (PLLB must be 48 MHz) */
  107. static struct clk udpck = {
  108. .name = "udpck",
  109. .parent = &pllb,
  110. .pmc_mask = AT91_PMC_UDP,
  111. .mode = pmc_sys_mode,
  112. };
  113. static struct clk uhpck = {
  114. .name = "uhpck",
  115. .parent = &pllb,
  116. .pmc_mask = AT91_PMC_UHP,
  117. .mode = pmc_sys_mode,
  118. };
  119. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  120. /*
  121. * The four programmable clocks can be parented by any primary clock.
  122. * You must configure pin multiplexing to bring these signals out.
  123. */
  124. static struct clk pck0 = {
  125. .name = "pck0",
  126. .pmc_mask = AT91_PMC_PCK0,
  127. .mode = pmc_sys_mode,
  128. .programmable = 1,
  129. .id = 0,
  130. };
  131. static struct clk pck1 = {
  132. .name = "pck1",
  133. .pmc_mask = AT91_PMC_PCK1,
  134. .mode = pmc_sys_mode,
  135. .programmable = 1,
  136. .id = 1,
  137. };
  138. static struct clk pck2 = {
  139. .name = "pck2",
  140. .pmc_mask = AT91_PMC_PCK2,
  141. .mode = pmc_sys_mode,
  142. .programmable = 1,
  143. .id = 2,
  144. };
  145. static struct clk pck3 = {
  146. .name = "pck3",
  147. .pmc_mask = AT91_PMC_PCK3,
  148. .mode = pmc_sys_mode,
  149. .programmable = 1,
  150. .id = 3,
  151. };
  152. #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  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. static 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 udc_clk = {
  170. .name = "udc_clk",
  171. .parent = &mck,
  172. .pmc_mask = 1 << AT91_ID_UDP,
  173. .mode = pmc_periph_mode,
  174. };
  175. static struct clk ohci_clk = {
  176. .name = "ohci_clk",
  177. .parent = &mck,
  178. .pmc_mask = 1 << AT91_ID_UHP,
  179. .mode = pmc_periph_mode,
  180. };
  181. static struct clk ether_clk = {
  182. .name = "ether_clk",
  183. .parent = &mck,
  184. .pmc_mask = 1 << AT91_ID_EMAC,
  185. .mode = pmc_periph_mode,
  186. };
  187. static struct clk mmc_clk = {
  188. .name = "mci_clk",
  189. .parent = &mck,
  190. .pmc_mask = 1 << AT91_ID_MCI,
  191. .mode = pmc_periph_mode,
  192. };
  193. static struct clk twi_clk = {
  194. .name = "twi_clk",
  195. .parent = &mck,
  196. .pmc_mask = 1 << AT91_ID_TWI,
  197. .mode = pmc_periph_mode,
  198. };
  199. static struct clk usart0_clk = {
  200. .name = "usart0_clk",
  201. .parent = &mck,
  202. .pmc_mask = 1 << AT91_ID_US0,
  203. .mode = pmc_periph_mode,
  204. };
  205. static struct clk usart1_clk = {
  206. .name = "usart1_clk",
  207. .parent = &mck,
  208. .pmc_mask = 1 << AT91_ID_US1,
  209. .mode = pmc_periph_mode,
  210. };
  211. static struct clk usart2_clk = {
  212. .name = "usart2_clk",
  213. .parent = &mck,
  214. .pmc_mask = 1 << AT91_ID_US2,
  215. .mode = pmc_periph_mode,
  216. };
  217. static struct clk usart3_clk = {
  218. .name = "usart3_clk",
  219. .parent = &mck,
  220. .pmc_mask = 1 << AT91_ID_US3,
  221. .mode = pmc_periph_mode,
  222. };
  223. static struct clk spi_clk = {
  224. .name = "spi0_clk",
  225. .parent = &mck,
  226. .pmc_mask = 1 << AT91_ID_SPI,
  227. .mode = pmc_periph_mode,
  228. };
  229. static struct clk pioA_clk = {
  230. .name = "pioA_clk",
  231. .parent = &mck,
  232. .pmc_mask = 1 << AT91_ID_PIOA,
  233. .mode = pmc_periph_mode,
  234. };
  235. static struct clk pioB_clk = {
  236. .name = "pioB_clk",
  237. .parent = &mck,
  238. .pmc_mask = 1 << AT91_ID_PIOB,
  239. .mode = pmc_periph_mode,
  240. };
  241. static struct clk pioC_clk = {
  242. .name = "pioC_clk",
  243. .parent = &mck,
  244. .pmc_mask = 1 << AT91_ID_PIOC,
  245. .mode = pmc_periph_mode,
  246. };
  247. static struct clk pioD_clk = {
  248. .name = "pioD_clk",
  249. .parent = &mck,
  250. .pmc_mask = 1 << AT91_ID_PIOD,
  251. .mode = pmc_periph_mode,
  252. };
  253. static struct clk *const clock_list[] = {
  254. /* four primary clocks -- MUST BE FIRST! */
  255. &clk32k,
  256. &main_clk,
  257. &plla,
  258. &pllb,
  259. /* PLLB children (USB) */
  260. &udpck,
  261. &uhpck,
  262. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  263. /* programmable clocks */
  264. &pck0,
  265. &pck1,
  266. &pck2,
  267. &pck3,
  268. #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  269. /* MCK and peripherals */
  270. &mck,
  271. &usart0_clk,
  272. &usart1_clk,
  273. &usart2_clk,
  274. &usart3_clk,
  275. &mmc_clk,
  276. &udc_clk,
  277. &twi_clk,
  278. &spi_clk,
  279. &pioA_clk,
  280. &pioB_clk,
  281. &pioC_clk,
  282. &pioD_clk,
  283. // ssc0..ssc2
  284. // tc0..tc5
  285. // irq0..irq6
  286. &ohci_clk,
  287. &ether_clk,
  288. };
  289. /*
  290. * Associate a particular clock with a function (eg, "uart") and device.
  291. * The drivers can then request the same 'function' with several different
  292. * devices and not care about which clock name to use.
  293. */
  294. void __init at91_clock_associate(const char *id, struct device *dev, const char *func)
  295. {
  296. struct clk *clk = clk_get(NULL, id);
  297. if (!dev || !clk || !IS_ERR(clk_get(dev, func)))
  298. return;
  299. clk->function = func;
  300. clk->dev = dev;
  301. }
  302. /* clocks are all static for now; no refcounting necessary */
  303. struct clk *clk_get(struct device *dev, const char *id)
  304. {
  305. int i;
  306. for (i = 0; i < ARRAY_SIZE(clock_list); i++) {
  307. struct clk *clk = clock_list[i];
  308. if (strcmp(id, clk->name) == 0)
  309. return clk;
  310. if (clk->function && (dev == clk->dev) && strcmp(id, clk->function) == 0)
  311. return clk;
  312. }
  313. return ERR_PTR(-ENOENT);
  314. }
  315. EXPORT_SYMBOL(clk_get);
  316. void clk_put(struct clk *clk)
  317. {
  318. }
  319. EXPORT_SYMBOL(clk_put);
  320. static void __clk_enable(struct clk *clk)
  321. {
  322. if (clk->parent)
  323. __clk_enable(clk->parent);
  324. if (clk->users++ == 0 && clk->mode)
  325. clk->mode(clk, 1);
  326. }
  327. int clk_enable(struct clk *clk)
  328. {
  329. unsigned long flags;
  330. spin_lock_irqsave(&clk_lock, flags);
  331. __clk_enable(clk);
  332. spin_unlock_irqrestore(&clk_lock, flags);
  333. return 0;
  334. }
  335. EXPORT_SYMBOL(clk_enable);
  336. static void __clk_disable(struct clk *clk)
  337. {
  338. BUG_ON(clk->users == 0);
  339. if (--clk->users == 0 && clk->mode)
  340. clk->mode(clk, 0);
  341. if (clk->parent)
  342. __clk_disable(clk->parent);
  343. }
  344. void clk_disable(struct clk *clk)
  345. {
  346. unsigned long flags;
  347. spin_lock_irqsave(&clk_lock, flags);
  348. __clk_disable(clk);
  349. spin_unlock_irqrestore(&clk_lock, flags);
  350. }
  351. EXPORT_SYMBOL(clk_disable);
  352. unsigned long clk_get_rate(struct clk *clk)
  353. {
  354. unsigned long flags;
  355. unsigned long rate;
  356. spin_lock_irqsave(&clk_lock, flags);
  357. for (;;) {
  358. rate = clk->rate_hz;
  359. if (rate || !clk->parent)
  360. break;
  361. clk = clk->parent;
  362. }
  363. spin_unlock_irqrestore(&clk_lock, flags);
  364. return rate;
  365. }
  366. EXPORT_SYMBOL(clk_get_rate);
  367. /*------------------------------------------------------------------------*/
  368. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  369. /*
  370. * For now, only the programmable clocks support reparenting (MCK could
  371. * do this too, with care) or rate changing (the PLLs could do this too,
  372. * ditto MCK but that's more for cpufreq). Drivers may reparent to get
  373. * a better rate match; we don't.
  374. */
  375. long clk_round_rate(struct clk *clk, unsigned long rate)
  376. {
  377. unsigned long flags;
  378. unsigned prescale;
  379. unsigned long actual;
  380. if (!clk->programmable)
  381. return -EINVAL;
  382. spin_lock_irqsave(&clk_lock, flags);
  383. actual = clk->parent->rate_hz;
  384. for (prescale = 0; prescale < 7; prescale++) {
  385. if (actual && actual <= rate)
  386. break;
  387. actual >>= 1;
  388. }
  389. spin_unlock_irqrestore(&clk_lock, flags);
  390. return (prescale < 7) ? actual : -ENOENT;
  391. }
  392. EXPORT_SYMBOL(clk_round_rate);
  393. int clk_set_rate(struct clk *clk, unsigned long rate)
  394. {
  395. unsigned long flags;
  396. unsigned prescale;
  397. unsigned long actual;
  398. if (!clk->programmable)
  399. return -EINVAL;
  400. if (clk->users)
  401. return -EBUSY;
  402. spin_lock_irqsave(&clk_lock, flags);
  403. actual = clk->parent->rate_hz;
  404. for (prescale = 0; prescale < 7; prescale++) {
  405. if (actual && actual <= rate) {
  406. u32 pckr;
  407. pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
  408. pckr &= AT91_PMC_CSS_PLLB; /* clock selection */
  409. pckr |= prescale << 2;
  410. at91_sys_write(AT91_PMC_PCKR(clk->id), pckr);
  411. clk->rate_hz = actual;
  412. break;
  413. }
  414. actual >>= 1;
  415. }
  416. spin_unlock_irqrestore(&clk_lock, flags);
  417. return (prescale < 7) ? actual : -ENOENT;
  418. }
  419. EXPORT_SYMBOL(clk_set_rate);
  420. struct clk *clk_get_parent(struct clk *clk)
  421. {
  422. return clk->parent;
  423. }
  424. EXPORT_SYMBOL(clk_get_parent);
  425. int clk_set_parent(struct clk *clk, struct clk *parent)
  426. {
  427. unsigned long flags;
  428. if (clk->users)
  429. return -EBUSY;
  430. if (!parent->primary || !clk->programmable)
  431. return -EINVAL;
  432. spin_lock_irqsave(&clk_lock, flags);
  433. clk->rate_hz = parent->rate_hz;
  434. clk->parent = parent;
  435. at91_sys_write(AT91_PMC_PCKR(clk->id), parent->id);
  436. spin_unlock_irqrestore(&clk_lock, flags);
  437. return 0;
  438. }
  439. EXPORT_SYMBOL(clk_set_parent);
  440. #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  441. /*------------------------------------------------------------------------*/
  442. #ifdef CONFIG_DEBUG_FS
  443. static int at91_clk_show(struct seq_file *s, void *unused)
  444. {
  445. u32 scsr, pcsr, sr;
  446. unsigned i;
  447. seq_printf(s, "SCSR = %8x\n", scsr = at91_sys_read(AT91_PMC_SCSR));
  448. seq_printf(s, "PCSR = %8x\n", pcsr = at91_sys_read(AT91_PMC_PCSR));
  449. seq_printf(s, "MOR = %8x\n", at91_sys_read(AT91_CKGR_MOR));
  450. seq_printf(s, "MCFR = %8x\n", at91_sys_read(AT91_CKGR_MCFR));
  451. seq_printf(s, "PLLA = %8x\n", at91_sys_read(AT91_CKGR_PLLAR));
  452. seq_printf(s, "PLLB = %8x\n", at91_sys_read(AT91_CKGR_PLLBR));
  453. seq_printf(s, "MCKR = %8x\n", at91_sys_read(AT91_PMC_MCKR));
  454. for (i = 0; i < 4; i++)
  455. seq_printf(s, "PCK%d = %8x\n", i, at91_sys_read(AT91_PMC_PCKR(i)));
  456. seq_printf(s, "SR = %8x\n", sr = at91_sys_read(AT91_PMC_SR));
  457. seq_printf(s, "\n");
  458. for (i = 0; i < ARRAY_SIZE(clock_list); i++) {
  459. char *state;
  460. struct clk *clk = clock_list[i];
  461. if (clk->mode == pmc_sys_mode)
  462. state = (scsr & clk->pmc_mask) ? "on" : "off";
  463. else if (clk->mode == pmc_periph_mode)
  464. state = (pcsr & clk->pmc_mask) ? "on" : "off";
  465. else if (clk->pmc_mask)
  466. state = (sr & clk->pmc_mask) ? "on" : "off";
  467. else if (clk == &clk32k || clk == &main_clk)
  468. state = "on";
  469. else
  470. state = "";
  471. seq_printf(s, "%-10s users=%2d %-3s %9ld Hz %s\n",
  472. clk->name, clk->users, state, clk_get_rate(clk),
  473. clk->parent ? clk->parent->name : "");
  474. }
  475. return 0;
  476. }
  477. static int at91_clk_open(struct inode *inode, struct file *file)
  478. {
  479. return single_open(file, at91_clk_show, NULL);
  480. }
  481. static struct file_operations at91_clk_operations = {
  482. .open = at91_clk_open,
  483. .read = seq_read,
  484. .llseek = seq_lseek,
  485. .release = single_release,
  486. };
  487. static int __init at91_clk_debugfs_init(void)
  488. {
  489. /* /sys/kernel/debug/at91_clk */
  490. (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
  491. return 0;
  492. }
  493. postcore_initcall(at91_clk_debugfs_init);
  494. #endif
  495. /*------------------------------------------------------------------------*/
  496. static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
  497. {
  498. unsigned mul, div;
  499. div = reg & 0xff;
  500. mul = (reg >> 16) & 0x7ff;
  501. if (div && mul) {
  502. freq /= div;
  503. freq *= mul + 1;
  504. } else
  505. freq = 0;
  506. return freq;
  507. }
  508. static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
  509. {
  510. if (pll == &pllb && (reg & AT91_PMC_USB96M))
  511. return freq / 2;
  512. else
  513. return freq;
  514. }
  515. static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
  516. {
  517. unsigned i, div = 0, mul = 0, diff = 1 << 30;
  518. unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  519. /* PLL output max 240 MHz (or 180 MHz per errata) */
  520. if (out_freq > 240000000)
  521. goto fail;
  522. for (i = 1; i < 256; i++) {
  523. int diff1;
  524. unsigned input, mul1;
  525. /*
  526. * PLL input between 1MHz and 32MHz per spec, but lower
  527. * frequences seem necessary in some cases so allow 100K.
  528. */
  529. input = main_freq / i;
  530. if (input < 100000)
  531. continue;
  532. if (input > 32000000)
  533. continue;
  534. mul1 = out_freq / input;
  535. if (mul1 > 2048)
  536. continue;
  537. if (mul1 < 2)
  538. goto fail;
  539. diff1 = out_freq - input * mul1;
  540. if (diff1 < 0)
  541. diff1 = -diff1;
  542. if (diff > diff1) {
  543. diff = diff1;
  544. div = i;
  545. mul = mul1;
  546. if (diff == 0)
  547. break;
  548. }
  549. }
  550. if (i == 256 && diff > (out_freq >> 5))
  551. goto fail;
  552. return ret | ((mul - 1) << 16) | div;
  553. fail:
  554. return 0;
  555. }
  556. /*
  557. * Several unused clocks may be active. Turn them off.
  558. */
  559. static void at91_periphclk_reset(void)
  560. {
  561. unsigned long reg;
  562. int i;
  563. reg = at91_sys_read(AT91_PMC_PCSR);
  564. for (i = 0; i < ARRAY_SIZE(clock_list); i++) {
  565. struct clk *clk = clock_list[i];
  566. if (clk->mode != pmc_periph_mode)
  567. continue;
  568. if (clk->users > 0)
  569. reg &= ~clk->pmc_mask;
  570. }
  571. at91_sys_write(AT91_PMC_PCDR, reg);
  572. }
  573. int __init at91_clock_init(unsigned long main_clock)
  574. {
  575. unsigned tmp, freq, mckr;
  576. spin_lock_init(&clk_lock);
  577. /*
  578. * When the bootloader initialized the main oscillator correctly,
  579. * there's no problem using the cycle counter. But if it didn't,
  580. * or when using oscillator bypass mode, we must be told the speed
  581. * of the main clock.
  582. */
  583. if (!main_clock) {
  584. do {
  585. tmp = at91_sys_read(AT91_CKGR_MCFR);
  586. } while (!(tmp & AT91_PMC_MAINRDY));
  587. main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
  588. }
  589. main_clk.rate_hz = main_clock;
  590. /* report if PLLA is more than mildly overclocked */
  591. plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR));
  592. if (plla.rate_hz > 209000000)
  593. pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
  594. /*
  595. * USB clock init: choose 48 MHz PLLB value, turn all clocks off,
  596. * disable 48MHz clock during usb peripheral suspend.
  597. *
  598. * REVISIT: assumes MCK doesn't derive from PLLB!
  599. */
  600. at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M;
  601. pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
  602. at91_sys_write(AT91_PMC_SCDR, AT91_PMC_UHP | AT91_PMC_UDP);
  603. at91_sys_write(AT91_CKGR_PLLBR, 0);
  604. at91_sys_write(AT91_PMC_SCER, AT91_PMC_MCKUDP);
  605. udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  606. uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  607. /*
  608. * MCK and CPU derive from one of those primary clocks.
  609. * For now, assume this parentage won't change.
  610. */
  611. mckr = at91_sys_read(AT91_PMC_MCKR);
  612. mck.parent = clock_list[mckr & AT91_PMC_CSS];
  613. freq = mck.parent->rate_hz;
  614. freq /= (1 << ((mckr >> 2) & 3)); /* prescale */
  615. mck.rate_hz = freq / (1 + ((mckr >> 8) & 3)); /* mdiv */
  616. /* MCK and CPU clock are "always on" */
  617. clk_enable(&mck);
  618. printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
  619. freq / 1000000, (unsigned) mck.rate_hz / 1000000,
  620. (unsigned) main_clock / 1000000,
  621. ((unsigned) main_clock % 1000000) / 1000);
  622. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  623. /* establish PCK0..PCK3 parentage */
  624. for (tmp = 0; tmp < ARRAY_SIZE(clock_list); tmp++) {
  625. struct clk *clk = clock_list[tmp], *parent;
  626. u32 pckr;
  627. if (!clk->programmable)
  628. continue;
  629. pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
  630. parent = clock_list[pckr & AT91_PMC_CSS];
  631. clk->parent = parent;
  632. clk->rate_hz = parent->rate_hz / (1 << ((pckr >> 2) & 3));
  633. if (clk->users == 0) {
  634. /* not being used, so switch it off */
  635. at91_sys_write(AT91_PMC_SCDR, clk->pmc_mask);
  636. }
  637. }
  638. #else
  639. /* disable all programmable clocks */
  640. at91_sys_write(AT91_PMC_SCDR, AT91_PMC_PCK0 | AT91_PMC_PCK1 | AT91_PMC_PCK2 | AT91_PMC_PCK3);
  641. #endif
  642. /* enable the PIO clocks */
  643. clk_enable(&pioA_clk);
  644. clk_enable(&pioB_clk);
  645. clk_enable(&pioC_clk);
  646. clk_enable(&pioD_clk);
  647. /* disable all other unused peripheral clocks */
  648. at91_periphclk_reset();
  649. return 0;
  650. }