clock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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/arch/hardware.h>
  28. #include <asm/arch/board.h> /* for master clock global */
  29. #include "generic.h"
  30. #undef DEBUG
  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. struct clk {
  37. const char *name;
  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 = 1 << 0, /* in PMC_SR */
  66. .users = 1,
  67. .id = 1,
  68. .primary = 1,
  69. };
  70. static struct clk plla = {
  71. .name = "plla",
  72. .parent = &main_clk,
  73. .pmc_mask = 1 << 1, /* in PMC_SR */
  74. .id = 2,
  75. .primary = 1,
  76. .pll = 1,
  77. };
  78. static void pllb_mode(struct clk *clk, int is_on)
  79. {
  80. u32 value;
  81. if (is_on) {
  82. is_on = AT91_PMC_LOCKB;
  83. value = at91_pllb_usb_init;
  84. } else
  85. value = 0;
  86. at91_sys_write(AT91_CKGR_PLLBR, value);
  87. do {
  88. cpu_relax();
  89. } while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on);
  90. }
  91. static struct clk pllb = {
  92. .name = "pllb",
  93. .parent = &main_clk,
  94. .pmc_mask = 1 << 2, /* in PMC_SR */
  95. .mode = pllb_mode,
  96. .id = 3,
  97. .primary = 1,
  98. .pll = 1,
  99. };
  100. static void pmc_sys_mode(struct clk *clk, int is_on)
  101. {
  102. if (is_on)
  103. at91_sys_write(AT91_PMC_SCER, clk->pmc_mask);
  104. else
  105. at91_sys_write(AT91_PMC_SCDR, clk->pmc_mask);
  106. }
  107. /* USB function clocks (PLLB must be 48 MHz) */
  108. static struct clk udpck = {
  109. .name = "udpck",
  110. .parent = &pllb,
  111. .pmc_mask = AT91_PMC_UDP,
  112. .mode = pmc_sys_mode,
  113. };
  114. static struct clk uhpck = {
  115. .name = "uhpck",
  116. .parent = &pllb,
  117. .pmc_mask = AT91_PMC_UHP,
  118. .mode = pmc_sys_mode,
  119. };
  120. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  121. /*
  122. * The four programmable clocks can be parented by any primary clock.
  123. * You must configure pin multiplexing to bring these signals out.
  124. */
  125. static struct clk pck0 = {
  126. .name = "pck0",
  127. .pmc_mask = AT91_PMC_PCK0,
  128. .mode = pmc_sys_mode,
  129. .programmable = 1,
  130. .id = 0,
  131. };
  132. static struct clk pck1 = {
  133. .name = "pck1",
  134. .pmc_mask = AT91_PMC_PCK1,
  135. .mode = pmc_sys_mode,
  136. .programmable = 1,
  137. .id = 1,
  138. };
  139. static struct clk pck2 = {
  140. .name = "pck2",
  141. .pmc_mask = AT91_PMC_PCK2,
  142. .mode = pmc_sys_mode,
  143. .programmable = 1,
  144. .id = 2,
  145. };
  146. static struct clk pck3 = {
  147. .name = "pck3",
  148. .pmc_mask = AT91_PMC_PCK3,
  149. .mode = pmc_sys_mode,
  150. .programmable = 1,
  151. .id = 3,
  152. };
  153. #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  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. static struct clk mck = {
  160. .name = "mck",
  161. .pmc_mask = 1 << 3, /* in PMC_SR */
  162. .users = 1, /* (must be) always on */
  163. };
  164. static void pmc_periph_mode(struct clk *clk, int is_on)
  165. {
  166. if (is_on)
  167. at91_sys_write(AT91_PMC_PCER, clk->pmc_mask);
  168. else
  169. at91_sys_write(AT91_PMC_PCDR, clk->pmc_mask);
  170. }
  171. static struct clk udc_clk = {
  172. .name = "udc_clk",
  173. .parent = &mck,
  174. .pmc_mask = 1 << AT91_ID_UDP,
  175. .mode = pmc_periph_mode,
  176. };
  177. static struct clk ohci_clk = {
  178. .name = "ohci_clk",
  179. .parent = &mck,
  180. .pmc_mask = 1 << AT91_ID_UHP,
  181. .mode = pmc_periph_mode,
  182. };
  183. static struct clk *const clock_list[] = {
  184. /* four primary clocks -- MUST BE FIRST! */
  185. &clk32k,
  186. &main_clk,
  187. &plla,
  188. &pllb,
  189. /* PLLB children (USB) */
  190. &udpck,
  191. &uhpck,
  192. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  193. /* programmable clocks */
  194. &pck0,
  195. &pck1,
  196. &pck2,
  197. &pck3,
  198. #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  199. /* MCK and peripherals */
  200. &mck,
  201. // usart0..usart3
  202. // mmc
  203. &udc_clk,
  204. // i2c
  205. // spi
  206. // ssc0..ssc2
  207. // tc0..tc5
  208. &ohci_clk,
  209. // ether
  210. };
  211. /* clocks are all static for now; no refcounting necessary */
  212. struct clk *clk_get(struct device *dev, const char *id)
  213. {
  214. int i;
  215. for (i = 0; i < ARRAY_SIZE(clock_list); i++) {
  216. if (strcmp(id, clock_list[i]->name) == 0)
  217. return clock_list[i];
  218. }
  219. return ERR_PTR(-ENOENT);
  220. }
  221. EXPORT_SYMBOL(clk_get);
  222. void clk_put(struct clk *clk)
  223. {
  224. }
  225. EXPORT_SYMBOL(clk_put);
  226. static void __clk_enable(struct clk *clk)
  227. {
  228. if (clk->parent)
  229. __clk_enable(clk->parent);
  230. if (clk->users++ == 0 && clk->mode)
  231. clk->mode(clk, 1);
  232. }
  233. int clk_enable(struct clk *clk)
  234. {
  235. unsigned long flags;
  236. spin_lock_irqsave(&clk_lock, flags);
  237. __clk_enable(clk);
  238. spin_unlock_irqrestore(&clk_lock, flags);
  239. return 0;
  240. }
  241. EXPORT_SYMBOL(clk_enable);
  242. static void __clk_disable(struct clk *clk)
  243. {
  244. BUG_ON(clk->users == 0);
  245. if (--clk->users == 0 && clk->mode)
  246. clk->mode(clk, 0);
  247. if (clk->parent)
  248. __clk_disable(clk->parent);
  249. }
  250. void clk_disable(struct clk *clk)
  251. {
  252. unsigned long flags;
  253. spin_lock_irqsave(&clk_lock, flags);
  254. __clk_disable(clk);
  255. spin_unlock_irqrestore(&clk_lock, flags);
  256. }
  257. EXPORT_SYMBOL(clk_disable);
  258. unsigned long clk_get_rate(struct clk *clk)
  259. {
  260. unsigned long flags;
  261. unsigned long rate;
  262. spin_lock_irqsave(&clk_lock, flags);
  263. for (;;) {
  264. rate = clk->rate_hz;
  265. if (rate || !clk->parent)
  266. break;
  267. clk = clk->parent;
  268. }
  269. spin_unlock_irqrestore(&clk_lock, flags);
  270. return rate;
  271. }
  272. EXPORT_SYMBOL(clk_get_rate);
  273. /*------------------------------------------------------------------------*/
  274. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  275. /*
  276. * For now, only the programmable clocks support reparenting (MCK could
  277. * do this too, with care) or rate changing (the PLLs could do this too,
  278. * ditto MCK but that's more for cpufreq). Drivers may reparent to get
  279. * a better rate match; we don't.
  280. */
  281. long clk_round_rate(struct clk *clk, unsigned long rate)
  282. {
  283. unsigned long flags;
  284. unsigned prescale;
  285. unsigned long actual;
  286. if (!clk->programmable)
  287. return -EINVAL;
  288. spin_lock_irqsave(&clk_lock, flags);
  289. actual = clk->parent->rate_hz;
  290. for (prescale = 0; prescale < 7; prescale++) {
  291. if (actual && actual <= rate)
  292. break;
  293. actual >>= 1;
  294. }
  295. spin_unlock_irqrestore(&clk_lock, flags);
  296. return (prescale < 7) ? actual : -ENOENT;
  297. }
  298. EXPORT_SYMBOL(clk_round_rate);
  299. int clk_set_rate(struct clk *clk, unsigned long rate)
  300. {
  301. unsigned long flags;
  302. unsigned prescale;
  303. unsigned long actual;
  304. if (!clk->programmable)
  305. return -EINVAL;
  306. if (clk->users)
  307. return -EBUSY;
  308. spin_lock_irqsave(&clk_lock, flags);
  309. actual = clk->parent->rate_hz;
  310. for (prescale = 0; prescale < 7; prescale++) {
  311. if (actual && actual <= rate) {
  312. u32 pckr;
  313. pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
  314. pckr &= 0x03;
  315. pckr |= prescale << 2;
  316. at91_sys_write(AT91_PMC_PCKR(clk->id), pckr);
  317. clk->rate_hz = actual;
  318. break;
  319. }
  320. actual >>= 1;
  321. }
  322. spin_unlock_irqrestore(&clk_lock, flags);
  323. return (prescale < 7) ? actual : -ENOENT;
  324. }
  325. EXPORT_SYMBOL(clk_set_rate);
  326. struct clk *clk_get_parent(struct clk *clk)
  327. {
  328. return clk->parent;
  329. }
  330. EXPORT_SYMBOL(clk_get_parent);
  331. int clk_set_parent(struct clk *clk, struct clk *parent)
  332. {
  333. unsigned long flags;
  334. if (clk->users)
  335. return -EBUSY;
  336. if (!parent->primary || !clk->programmable)
  337. return -EINVAL;
  338. spin_lock_irqsave(&clk_lock, flags);
  339. clk->rate_hz = parent->rate_hz;
  340. clk->parent = parent;
  341. at91_sys_write(AT91_PMC_PCKR(clk->id), parent->id);
  342. spin_unlock_irqrestore(&clk_lock, flags);
  343. return 0;
  344. }
  345. EXPORT_SYMBOL(clk_set_parent);
  346. #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  347. /*------------------------------------------------------------------------*/
  348. #ifdef CONFIG_DEBUG_FS
  349. static int at91_clk_show(struct seq_file *s, void *unused)
  350. {
  351. u32 scsr, pcsr, sr;
  352. unsigned i;
  353. seq_printf(s, "SCSR = %8x\n", scsr = at91_sys_read(AT91_PMC_SCSR));
  354. seq_printf(s, "PCSR = %8x\n", pcsr = at91_sys_read(AT91_PMC_PCSR));
  355. seq_printf(s, "MOR = %8x\n", at91_sys_read(AT91_CKGR_MOR));
  356. seq_printf(s, "MCFR = %8x\n", at91_sys_read(AT91_CKGR_MCFR));
  357. seq_printf(s, "PLLA = %8x\n", at91_sys_read(AT91_CKGR_PLLAR));
  358. seq_printf(s, "PLLB = %8x\n", at91_sys_read(AT91_CKGR_PLLBR));
  359. seq_printf(s, "MCKR = %8x\n", at91_sys_read(AT91_PMC_MCKR));
  360. for (i = 0; i < 4; i++)
  361. seq_printf(s, "PCK%d = %8x\n", i, at91_sys_read(AT91_PMC_PCKR(i)));
  362. seq_printf(s, "SR = %8x\n", sr = at91_sys_read(AT91_PMC_SR));
  363. seq_printf(s, "\n");
  364. for (i = 0; i < ARRAY_SIZE(clock_list); i++) {
  365. char *state;
  366. struct clk *clk = clock_list[i];
  367. if (clk->mode == pmc_sys_mode)
  368. state = (scsr & clk->pmc_mask) ? "on" : "off";
  369. else if (clk->mode == pmc_periph_mode)
  370. state = (pcsr & clk->pmc_mask) ? "on" : "off";
  371. else if (clk->pmc_mask)
  372. state = (sr & clk->pmc_mask) ? "on" : "off";
  373. else if (clk == &clk32k || clk == &main_clk)
  374. state = "on";
  375. else
  376. state = "";
  377. seq_printf(s, "%-10s users=%d %-3s %9ld Hz %s\n",
  378. clk->name, clk->users, state, clk_get_rate(clk),
  379. clk->parent ? clk->parent->name : "");
  380. }
  381. return 0;
  382. }
  383. static int at91_clk_open(struct inode *inode, struct file *file)
  384. {
  385. return single_open(file, at91_clk_show, NULL);
  386. }
  387. static struct file_operations at91_clk_operations = {
  388. .open = at91_clk_open,
  389. .read = seq_read,
  390. .llseek = seq_lseek,
  391. .release = single_release,
  392. };
  393. static int __init at91_clk_debugfs_init(void)
  394. {
  395. /* /sys/kernel/debug/at91_clk */
  396. (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
  397. return 0;
  398. }
  399. postcore_initcall(at91_clk_debugfs_init);
  400. #endif
  401. /*------------------------------------------------------------------------*/
  402. static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
  403. {
  404. unsigned mul, div;
  405. div = reg & 0xff;
  406. mul = (reg >> 16) & 0x7ff;
  407. if (div && mul) {
  408. freq /= div;
  409. freq *= mul + 1;
  410. } else
  411. freq = 0;
  412. if (pll == &pllb && (reg & (1 << 28)))
  413. freq /= 2;
  414. return freq;
  415. }
  416. static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
  417. {
  418. unsigned i, div = 0, mul = 0, diff = 1 << 30;
  419. unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  420. /* PLL output max 240 MHz (or 180 MHz per errata) */
  421. if (out_freq > 240000000)
  422. goto fail;
  423. for (i = 1; i < 256; i++) {
  424. int diff1;
  425. unsigned input, mul1;
  426. /*
  427. * PLL input between 1MHz and 32MHz per spec, but lower
  428. * frequences seem necessary in some cases so allow 100K.
  429. */
  430. input = main_freq / i;
  431. if (input < 100000)
  432. continue;
  433. if (input > 32000000)
  434. continue;
  435. mul1 = out_freq / input;
  436. if (mul1 > 2048)
  437. continue;
  438. if (mul1 < 2)
  439. goto fail;
  440. diff1 = out_freq - input * mul1;
  441. if (diff1 < 0)
  442. diff1 = -diff1;
  443. if (diff > diff1) {
  444. diff = diff1;
  445. div = i;
  446. mul = mul1;
  447. if (diff == 0)
  448. break;
  449. }
  450. }
  451. if (i == 256 && diff > (out_freq >> 5))
  452. goto fail;
  453. return ret | ((mul - 1) << 16) | div;
  454. fail:
  455. return 0;
  456. }
  457. int __init at91_clock_init(unsigned long main_clock)
  458. {
  459. unsigned tmp, freq, mckr;
  460. spin_lock_init(&clk_lock);
  461. /*
  462. * When the bootloader initialized the main oscillator correctly,
  463. * there's no problem using the cycle counter. But if it didn't,
  464. * or when using oscillator bypass mode, we must be told the speed
  465. * of the main clock.
  466. */
  467. if (!main_clock) {
  468. do {
  469. tmp = at91_sys_read(AT91_CKGR_MCFR);
  470. } while (!(tmp & 0x10000));
  471. main_clock = (tmp & 0xffff) * (AT91_SLOW_CLOCK / 16);
  472. }
  473. main_clk.rate_hz = main_clock;
  474. /* report if PLLA is more than mildly overclocked */
  475. plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR));
  476. if (plla.rate_hz > 209000000)
  477. pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
  478. /*
  479. * USB clock init: choose 48 MHz PLLB value, turn all clocks off,
  480. * disable 48MHz clock during usb peripheral suspend.
  481. *
  482. * REVISIT: assumes MCK doesn't derive from PLLB!
  483. */
  484. at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | 0x10000000;
  485. pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
  486. at91_sys_write(AT91_PMC_PCDR, (1 << AT91_ID_UHP) | (1 << AT91_ID_UDP));
  487. at91_sys_write(AT91_PMC_SCDR, AT91_PMC_UHP | AT91_PMC_UDP);
  488. at91_sys_write(AT91_CKGR_PLLBR, 0);
  489. at91_sys_write(AT91_PMC_SCER, AT91_PMC_MCKUDP);
  490. /*
  491. * MCK and CPU derive from one of those primary clocks.
  492. * For now, assume this parentage won't change.
  493. */
  494. mckr = at91_sys_read(AT91_PMC_MCKR);
  495. mck.parent = clock_list[mckr & AT91_PMC_CSS];
  496. mck.parent->users++;
  497. freq = mck.parent->rate_hz;
  498. freq /= (1 << ((mckr >> 2) & 3)); /* prescale */
  499. mck.rate_hz = freq / (1 + ((mckr >> 8) & 3)); /* mdiv */
  500. printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
  501. freq / 1000000, (unsigned) mck.rate_hz / 1000000,
  502. (unsigned) main_clock / 1000000,
  503. ((unsigned) main_clock % 1000000) / 1000);
  504. /* FIXME get rid of master_clock global */
  505. at91_master_clock = mck.rate_hz;
  506. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  507. /* establish PCK0..PCK3 parentage */
  508. for (tmp = 0; tmp < ARRAY_SIZE(clock_list); tmp++) {
  509. struct clk *clk = clock_list[tmp], *parent;
  510. u32 pckr;
  511. if (!clk->programmable)
  512. continue;
  513. pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
  514. parent = clock_list[pckr & 3];
  515. clk->parent = parent;
  516. clk->rate_hz = parent->rate_hz / (1 << ((pckr >> 2) & 3));
  517. }
  518. #else
  519. /* disable unused clocks */
  520. at91_sys_write(AT91_PMC_SCDR, AT91_PMC_PCK0 | AT91_PMC_PCK1 | AT91_PMC_PCK2 | AT91_PMC_PCK3);
  521. #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  522. /* FIXME several unused clocks may still be active... provide
  523. * a CONFIG option to turn off all unused clocks at some point
  524. * before driver init starts.
  525. */
  526. return 0;
  527. }