clock.c 15 KB

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