clock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 "clock.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. #define clk_is_primary(x) ((x)->type & CLK_TYPE_PRIMARY)
  35. #define clk_is_programmable(x) ((x)->type & CLK_TYPE_PROGRAMMABLE)
  36. #define clk_is_peripheral(x) ((x)->type & CLK_TYPE_PERIPHERAL)
  37. static LIST_HEAD(clocks);
  38. static DEFINE_SPINLOCK(clk_lock);
  39. static u32 at91_pllb_usb_init;
  40. /*
  41. * Four primary clock sources: two crystal oscillators (32K, main), and
  42. * two PLLs. PLLA usually runs the master clock; and PLLB must run at
  43. * 48 MHz (unless no USB function clocks are needed). The main clock and
  44. * both PLLs are turned off to run in "slow clock mode" (system suspend).
  45. */
  46. static struct clk clk32k = {
  47. .name = "clk32k",
  48. .rate_hz = AT91_SLOW_CLOCK,
  49. .users = 1, /* always on */
  50. .id = 0,
  51. .type = CLK_TYPE_PRIMARY,
  52. };
  53. static struct clk main_clk = {
  54. .name = "main",
  55. .pmc_mask = AT91_PMC_MOSCS, /* in PMC_SR */
  56. .id = 1,
  57. .type = CLK_TYPE_PRIMARY,
  58. };
  59. static struct clk plla = {
  60. .name = "plla",
  61. .parent = &main_clk,
  62. .pmc_mask = AT91_PMC_LOCKA, /* in PMC_SR */
  63. .id = 2,
  64. .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
  65. };
  66. static void pllb_mode(struct clk *clk, int is_on)
  67. {
  68. u32 value;
  69. if (is_on) {
  70. is_on = AT91_PMC_LOCKB;
  71. value = at91_pllb_usb_init;
  72. } else
  73. value = 0;
  74. // REVISIT: Add work-around for AT91RM9200 Errata #26 ?
  75. at91_sys_write(AT91_CKGR_PLLBR, value);
  76. do {
  77. cpu_relax();
  78. } while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on);
  79. }
  80. static struct clk pllb = {
  81. .name = "pllb",
  82. .parent = &main_clk,
  83. .pmc_mask = AT91_PMC_LOCKB, /* in PMC_SR */
  84. .mode = pllb_mode,
  85. .id = 3,
  86. .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
  87. };
  88. static void pmc_sys_mode(struct clk *clk, int is_on)
  89. {
  90. if (is_on)
  91. at91_sys_write(AT91_PMC_SCER, clk->pmc_mask);
  92. else
  93. at91_sys_write(AT91_PMC_SCDR, clk->pmc_mask);
  94. }
  95. /* USB function clocks (PLLB must be 48 MHz) */
  96. static struct clk udpck = {
  97. .name = "udpck",
  98. .parent = &pllb,
  99. .pmc_mask = AT91_PMC_UDP,
  100. .mode = pmc_sys_mode,
  101. };
  102. static struct clk uhpck = {
  103. .name = "uhpck",
  104. .parent = &pllb,
  105. .pmc_mask = AT91_PMC_UHP,
  106. .mode = pmc_sys_mode,
  107. };
  108. /*
  109. * The master clock is divided from the CPU clock (by 1-4). It's used for
  110. * memory, interfaces to on-chip peripherals, the AIC, and sometimes more
  111. * (e.g baud rate generation). It's sourced from one of the primary clocks.
  112. */
  113. static struct clk mck = {
  114. .name = "mck",
  115. .pmc_mask = AT91_PMC_MCKRDY, /* in PMC_SR */
  116. };
  117. static void pmc_periph_mode(struct clk *clk, int is_on)
  118. {
  119. if (is_on)
  120. at91_sys_write(AT91_PMC_PCER, clk->pmc_mask);
  121. else
  122. at91_sys_write(AT91_PMC_PCDR, clk->pmc_mask);
  123. }
  124. static struct clk __init *at91_css_to_clk(unsigned long css)
  125. {
  126. switch (css) {
  127. case AT91_PMC_CSS_SLOW:
  128. return &clk32k;
  129. case AT91_PMC_CSS_MAIN:
  130. return &main_clk;
  131. case AT91_PMC_CSS_PLLA:
  132. return &plla;
  133. case AT91_PMC_CSS_PLLB:
  134. return &pllb;
  135. }
  136. return NULL;
  137. }
  138. /*
  139. * Associate a particular clock with a function (eg, "uart") and device.
  140. * The drivers can then request the same 'function' with several different
  141. * devices and not care about which clock name to use.
  142. */
  143. void __init at91_clock_associate(const char *id, struct device *dev, const char *func)
  144. {
  145. struct clk *clk = clk_get(NULL, id);
  146. if (!dev || !clk || !IS_ERR(clk_get(dev, func)))
  147. return;
  148. clk->function = func;
  149. clk->dev = dev;
  150. }
  151. /* clocks cannot be de-registered no refcounting necessary */
  152. struct clk *clk_get(struct device *dev, const char *id)
  153. {
  154. struct clk *clk;
  155. list_for_each_entry(clk, &clocks, node) {
  156. if (strcmp(id, clk->name) == 0)
  157. return clk;
  158. if (clk->function && (dev == clk->dev) && strcmp(id, clk->function) == 0)
  159. return clk;
  160. }
  161. return ERR_PTR(-ENOENT);
  162. }
  163. EXPORT_SYMBOL(clk_get);
  164. void clk_put(struct clk *clk)
  165. {
  166. }
  167. EXPORT_SYMBOL(clk_put);
  168. static void __clk_enable(struct clk *clk)
  169. {
  170. if (clk->parent)
  171. __clk_enable(clk->parent);
  172. if (clk->users++ == 0 && clk->mode)
  173. clk->mode(clk, 1);
  174. }
  175. int clk_enable(struct clk *clk)
  176. {
  177. unsigned long flags;
  178. spin_lock_irqsave(&clk_lock, flags);
  179. __clk_enable(clk);
  180. spin_unlock_irqrestore(&clk_lock, flags);
  181. return 0;
  182. }
  183. EXPORT_SYMBOL(clk_enable);
  184. static void __clk_disable(struct clk *clk)
  185. {
  186. BUG_ON(clk->users == 0);
  187. if (--clk->users == 0 && clk->mode)
  188. clk->mode(clk, 0);
  189. if (clk->parent)
  190. __clk_disable(clk->parent);
  191. }
  192. void clk_disable(struct clk *clk)
  193. {
  194. unsigned long flags;
  195. spin_lock_irqsave(&clk_lock, flags);
  196. __clk_disable(clk);
  197. spin_unlock_irqrestore(&clk_lock, flags);
  198. }
  199. EXPORT_SYMBOL(clk_disable);
  200. unsigned long clk_get_rate(struct clk *clk)
  201. {
  202. unsigned long flags;
  203. unsigned long rate;
  204. spin_lock_irqsave(&clk_lock, flags);
  205. for (;;) {
  206. rate = clk->rate_hz;
  207. if (rate || !clk->parent)
  208. break;
  209. clk = clk->parent;
  210. }
  211. spin_unlock_irqrestore(&clk_lock, flags);
  212. return rate;
  213. }
  214. EXPORT_SYMBOL(clk_get_rate);
  215. /*------------------------------------------------------------------------*/
  216. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  217. /*
  218. * For now, only the programmable clocks support reparenting (MCK could
  219. * do this too, with care) or rate changing (the PLLs could do this too,
  220. * ditto MCK but that's more for cpufreq). Drivers may reparent to get
  221. * a better rate match; we don't.
  222. */
  223. long clk_round_rate(struct clk *clk, unsigned long rate)
  224. {
  225. unsigned long flags;
  226. unsigned prescale;
  227. unsigned long actual;
  228. if (!clk_is_programmable(clk))
  229. return -EINVAL;
  230. spin_lock_irqsave(&clk_lock, flags);
  231. actual = clk->parent->rate_hz;
  232. for (prescale = 0; prescale < 7; prescale++) {
  233. if (actual && actual <= rate)
  234. break;
  235. actual >>= 1;
  236. }
  237. spin_unlock_irqrestore(&clk_lock, flags);
  238. return (prescale < 7) ? actual : -ENOENT;
  239. }
  240. EXPORT_SYMBOL(clk_round_rate);
  241. int clk_set_rate(struct clk *clk, unsigned long rate)
  242. {
  243. unsigned long flags;
  244. unsigned prescale;
  245. unsigned long actual;
  246. if (!clk_is_programmable(clk))
  247. return -EINVAL;
  248. if (clk->users)
  249. return -EBUSY;
  250. spin_lock_irqsave(&clk_lock, flags);
  251. actual = clk->parent->rate_hz;
  252. for (prescale = 0; prescale < 7; prescale++) {
  253. if (actual && actual <= rate) {
  254. u32 pckr;
  255. pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
  256. pckr &= AT91_PMC_CSS_PLLB; /* clock selection */
  257. pckr |= prescale << 2;
  258. at91_sys_write(AT91_PMC_PCKR(clk->id), pckr);
  259. clk->rate_hz = actual;
  260. break;
  261. }
  262. actual >>= 1;
  263. }
  264. spin_unlock_irqrestore(&clk_lock, flags);
  265. return (prescale < 7) ? actual : -ENOENT;
  266. }
  267. EXPORT_SYMBOL(clk_set_rate);
  268. struct clk *clk_get_parent(struct clk *clk)
  269. {
  270. return clk->parent;
  271. }
  272. EXPORT_SYMBOL(clk_get_parent);
  273. int clk_set_parent(struct clk *clk, struct clk *parent)
  274. {
  275. unsigned long flags;
  276. if (clk->users)
  277. return -EBUSY;
  278. if (!clk_is_primary(parent) || !clk_is_programmable(clk))
  279. return -EINVAL;
  280. spin_lock_irqsave(&clk_lock, flags);
  281. clk->rate_hz = parent->rate_hz;
  282. clk->parent = parent;
  283. at91_sys_write(AT91_PMC_PCKR(clk->id), parent->id);
  284. spin_unlock_irqrestore(&clk_lock, flags);
  285. return 0;
  286. }
  287. EXPORT_SYMBOL(clk_set_parent);
  288. /* establish PCK0..PCK3 parentage and rate */
  289. static void init_programmable_clock(struct clk *clk)
  290. {
  291. struct clk *parent;
  292. u32 pckr;
  293. pckr = at91_sys_read(AT91_PMC_PCKR(clk->id));
  294. parent = at91_css_to_clk(pckr & AT91_PMC_CSS);
  295. clk->parent = parent;
  296. clk->rate_hz = parent->rate_hz / (1 << ((pckr >> 2) & 3));
  297. }
  298. #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  299. /*------------------------------------------------------------------------*/
  300. #ifdef CONFIG_DEBUG_FS
  301. static int at91_clk_show(struct seq_file *s, void *unused)
  302. {
  303. u32 scsr, pcsr, sr;
  304. struct clk *clk;
  305. unsigned i;
  306. seq_printf(s, "SCSR = %8x\n", scsr = at91_sys_read(AT91_PMC_SCSR));
  307. seq_printf(s, "PCSR = %8x\n", pcsr = at91_sys_read(AT91_PMC_PCSR));
  308. seq_printf(s, "MOR = %8x\n", at91_sys_read(AT91_CKGR_MOR));
  309. seq_printf(s, "MCFR = %8x\n", at91_sys_read(AT91_CKGR_MCFR));
  310. seq_printf(s, "PLLA = %8x\n", at91_sys_read(AT91_CKGR_PLLAR));
  311. seq_printf(s, "PLLB = %8x\n", at91_sys_read(AT91_CKGR_PLLBR));
  312. seq_printf(s, "MCKR = %8x\n", at91_sys_read(AT91_PMC_MCKR));
  313. for (i = 0; i < 4; i++)
  314. seq_printf(s, "PCK%d = %8x\n", i, at91_sys_read(AT91_PMC_PCKR(i)));
  315. seq_printf(s, "SR = %8x\n", sr = at91_sys_read(AT91_PMC_SR));
  316. seq_printf(s, "\n");
  317. list_for_each_entry(clk, &clocks, node) {
  318. char *state;
  319. if (clk->mode == pmc_sys_mode)
  320. state = (scsr & clk->pmc_mask) ? "on" : "off";
  321. else if (clk->mode == pmc_periph_mode)
  322. state = (pcsr & clk->pmc_mask) ? "on" : "off";
  323. else if (clk->pmc_mask)
  324. state = (sr & clk->pmc_mask) ? "on" : "off";
  325. else if (clk == &clk32k || clk == &main_clk)
  326. state = "on";
  327. else
  328. state = "";
  329. seq_printf(s, "%-10s users=%2d %-3s %9ld Hz %s\n",
  330. clk->name, clk->users, state, clk_get_rate(clk),
  331. clk->parent ? clk->parent->name : "");
  332. }
  333. return 0;
  334. }
  335. static int at91_clk_open(struct inode *inode, struct file *file)
  336. {
  337. return single_open(file, at91_clk_show, NULL);
  338. }
  339. static struct file_operations at91_clk_operations = {
  340. .open = at91_clk_open,
  341. .read = seq_read,
  342. .llseek = seq_lseek,
  343. .release = single_release,
  344. };
  345. static int __init at91_clk_debugfs_init(void)
  346. {
  347. /* /sys/kernel/debug/at91_clk */
  348. (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
  349. return 0;
  350. }
  351. postcore_initcall(at91_clk_debugfs_init);
  352. #endif
  353. /*------------------------------------------------------------------------*/
  354. /* Register a new clock */
  355. int __init clk_register(struct clk *clk)
  356. {
  357. if (clk_is_peripheral(clk)) {
  358. clk->parent = &mck;
  359. clk->mode = pmc_periph_mode;
  360. list_add_tail(&clk->node, &clocks);
  361. }
  362. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  363. else if (clk_is_programmable(clk)) {
  364. clk->mode = pmc_sys_mode;
  365. init_programmable_clock(clk);
  366. list_add_tail(&clk->node, &clocks);
  367. }
  368. #endif
  369. return 0;
  370. }
  371. /*------------------------------------------------------------------------*/
  372. static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
  373. {
  374. unsigned mul, div;
  375. div = reg & 0xff;
  376. mul = (reg >> 16) & 0x7ff;
  377. if (div && mul) {
  378. freq /= div;
  379. freq *= mul + 1;
  380. } else
  381. freq = 0;
  382. return freq;
  383. }
  384. static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
  385. {
  386. if (pll == &pllb && (reg & AT91_PMC_USB96M))
  387. return freq / 2;
  388. else
  389. return freq;
  390. }
  391. static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
  392. {
  393. unsigned i, div = 0, mul = 0, diff = 1 << 30;
  394. unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  395. /* PLL output max 240 MHz (or 180 MHz per errata) */
  396. if (out_freq > 240000000)
  397. goto fail;
  398. for (i = 1; i < 256; i++) {
  399. int diff1;
  400. unsigned input, mul1;
  401. /*
  402. * PLL input between 1MHz and 32MHz per spec, but lower
  403. * frequences seem necessary in some cases so allow 100K.
  404. */
  405. input = main_freq / i;
  406. if (input < 100000)
  407. continue;
  408. if (input > 32000000)
  409. continue;
  410. mul1 = out_freq / input;
  411. if (mul1 > 2048)
  412. continue;
  413. if (mul1 < 2)
  414. goto fail;
  415. diff1 = out_freq - input * mul1;
  416. if (diff1 < 0)
  417. diff1 = -diff1;
  418. if (diff > diff1) {
  419. diff = diff1;
  420. div = i;
  421. mul = mul1;
  422. if (diff == 0)
  423. break;
  424. }
  425. }
  426. if (i == 256 && diff > (out_freq >> 5))
  427. goto fail;
  428. return ret | ((mul - 1) << 16) | div;
  429. fail:
  430. return 0;
  431. }
  432. /*
  433. * Several unused clocks may be active. Turn them off.
  434. */
  435. static void __init at91_periphclk_reset(void)
  436. {
  437. unsigned long reg;
  438. struct clk *clk;
  439. reg = at91_sys_read(AT91_PMC_PCSR);
  440. list_for_each_entry(clk, &clocks, node) {
  441. if (clk->mode != pmc_periph_mode)
  442. continue;
  443. if (clk->users > 0)
  444. reg &= ~clk->pmc_mask;
  445. }
  446. at91_sys_write(AT91_PMC_PCDR, reg);
  447. }
  448. static struct clk *const standard_pmc_clocks[] __initdata = {
  449. /* four primary clocks */
  450. &clk32k,
  451. &main_clk,
  452. &plla,
  453. &pllb,
  454. /* PLLB children (USB) */
  455. &udpck,
  456. &uhpck,
  457. /* MCK */
  458. &mck
  459. };
  460. int __init at91_clock_init(unsigned long main_clock)
  461. {
  462. unsigned tmp, freq, mckr;
  463. int i;
  464. /*
  465. * When the bootloader initialized the main oscillator correctly,
  466. * there's no problem using the cycle counter. But if it didn't,
  467. * or when using oscillator bypass mode, we must be told the speed
  468. * of the main clock.
  469. */
  470. if (!main_clock) {
  471. do {
  472. tmp = at91_sys_read(AT91_CKGR_MCFR);
  473. } while (!(tmp & AT91_PMC_MAINRDY));
  474. main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
  475. }
  476. main_clk.rate_hz = main_clock;
  477. /* report if PLLA is more than mildly overclocked */
  478. plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR));
  479. if (plla.rate_hz > 209000000)
  480. pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
  481. /*
  482. * USB clock init: choose 48 MHz PLLB value, turn all clocks off,
  483. * disable 48MHz clock during usb peripheral suspend.
  484. *
  485. * REVISIT: assumes MCK doesn't derive from PLLB!
  486. */
  487. at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M;
  488. pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
  489. at91_sys_write(AT91_PMC_SCDR, AT91_PMC_UHP | AT91_PMC_UDP);
  490. at91_sys_write(AT91_CKGR_PLLBR, 0);
  491. at91_sys_write(AT91_PMC_SCER, AT91_PMC_MCKUDP);
  492. udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  493. uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  494. /*
  495. * MCK and CPU derive from one of those primary clocks.
  496. * For now, assume this parentage won't change.
  497. */
  498. mckr = at91_sys_read(AT91_PMC_MCKR);
  499. mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
  500. freq = mck.parent->rate_hz;
  501. freq /= (1 << ((mckr >> 2) & 3)); /* prescale */
  502. mck.rate_hz = freq / (1 + ((mckr >> 8) & 3)); /* mdiv */
  503. /* Register the PMC's standard clocks */
  504. for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
  505. list_add_tail(&standard_pmc_clocks[i]->node, &clocks);
  506. /* MCK and CPU clock are "always on" */
  507. clk_enable(&mck);
  508. printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
  509. freq / 1000000, (unsigned) mck.rate_hz / 1000000,
  510. (unsigned) main_clock / 1000000,
  511. ((unsigned) main_clock % 1000000) / 1000);
  512. /* disable all programmable clocks */
  513. at91_sys_write(AT91_PMC_SCDR, AT91_PMC_PCK0 | AT91_PMC_PCK1 | AT91_PMC_PCK2 | AT91_PMC_PCK3);
  514. /* disable all other unused peripheral clocks */
  515. at91_periphclk_reset();
  516. return 0;
  517. }