clock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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/io.h>
  25. #include <asm/mach-types.h>
  26. #include <asm/hardware.h>
  27. #include <asm/arch/at91_pmc.h>
  28. #include <asm/arch/cpu.h>
  29. #include "clock.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. static LIST_HEAD(clocks);
  40. static DEFINE_SPINLOCK(clk_lock);
  41. static u32 at91_pllb_usb_init;
  42. /*
  43. * Four primary clock sources: two crystal oscillators (32K, main), and
  44. * two PLLs. PLLA usually runs the master clock; and PLLB must run at
  45. * 48 MHz (unless no USB function clocks are needed). The main clock and
  46. * both PLLs are turned off to run in "slow clock mode" (system suspend).
  47. */
  48. static struct clk clk32k = {
  49. .name = "clk32k",
  50. .rate_hz = AT91_SLOW_CLOCK,
  51. .users = 1, /* always on */
  52. .id = 0,
  53. .type = CLK_TYPE_PRIMARY,
  54. };
  55. static struct clk main_clk = {
  56. .name = "main",
  57. .pmc_mask = AT91_PMC_MOSCS, /* in PMC_SR */
  58. .id = 1,
  59. .type = CLK_TYPE_PRIMARY,
  60. };
  61. static struct clk plla = {
  62. .name = "plla",
  63. .parent = &main_clk,
  64. .pmc_mask = AT91_PMC_LOCKA, /* in PMC_SR */
  65. .id = 2,
  66. .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
  67. };
  68. static void pllb_mode(struct clk *clk, int is_on)
  69. {
  70. u32 value;
  71. if (is_on) {
  72. is_on = AT91_PMC_LOCKB;
  73. value = at91_pllb_usb_init;
  74. } else
  75. value = 0;
  76. // REVISIT: Add work-around for AT91RM9200 Errata #26 ?
  77. at91_sys_write(AT91_CKGR_PLLBR, value);
  78. do {
  79. cpu_relax();
  80. } while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on);
  81. }
  82. static struct clk pllb = {
  83. .name = "pllb",
  84. .parent = &main_clk,
  85. .pmc_mask = AT91_PMC_LOCKB, /* in PMC_SR */
  86. .mode = pllb_mode,
  87. .id = 3,
  88. .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
  89. };
  90. static void pmc_sys_mode(struct clk *clk, int is_on)
  91. {
  92. if (is_on)
  93. at91_sys_write(AT91_PMC_SCER, clk->pmc_mask);
  94. else
  95. at91_sys_write(AT91_PMC_SCDR, clk->pmc_mask);
  96. }
  97. /* USB function clocks (PLLB must be 48 MHz) */
  98. static struct clk udpck = {
  99. .name = "udpck",
  100. .parent = &pllb,
  101. .mode = pmc_sys_mode,
  102. };
  103. static struct clk uhpck = {
  104. .name = "uhpck",
  105. .parent = &pllb,
  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 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 & AT91_PMC_PRES) >> 2));
  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. seq_printf(s, "SCSR = %8x\n", scsr = at91_sys_read(AT91_PMC_SCSR));
  306. seq_printf(s, "PCSR = %8x\n", pcsr = at91_sys_read(AT91_PMC_PCSR));
  307. seq_printf(s, "MOR = %8x\n", at91_sys_read(AT91_CKGR_MOR));
  308. seq_printf(s, "MCFR = %8x\n", at91_sys_read(AT91_CKGR_MCFR));
  309. seq_printf(s, "PLLA = %8x\n", at91_sys_read(AT91_CKGR_PLLAR));
  310. seq_printf(s, "PLLB = %8x\n", at91_sys_read(AT91_CKGR_PLLBR));
  311. seq_printf(s, "MCKR = %8x\n", at91_sys_read(AT91_PMC_MCKR));
  312. seq_printf(s, "SR = %8x\n", sr = at91_sys_read(AT91_PMC_SR));
  313. seq_printf(s, "\n");
  314. list_for_each_entry(clk, &clocks, node) {
  315. char *state;
  316. if (clk->mode == pmc_sys_mode)
  317. state = (scsr & clk->pmc_mask) ? "on" : "off";
  318. else if (clk->mode == pmc_periph_mode)
  319. state = (pcsr & clk->pmc_mask) ? "on" : "off";
  320. else if (clk->pmc_mask)
  321. state = (sr & clk->pmc_mask) ? "on" : "off";
  322. else if (clk == &clk32k || clk == &main_clk)
  323. state = "on";
  324. else
  325. state = "";
  326. seq_printf(s, "%-10s users=%2d %-3s %9ld Hz %s\n",
  327. clk->name, clk->users, state, clk_get_rate(clk),
  328. clk->parent ? clk->parent->name : "");
  329. }
  330. return 0;
  331. }
  332. static int at91_clk_open(struct inode *inode, struct file *file)
  333. {
  334. return single_open(file, at91_clk_show, NULL);
  335. }
  336. static const struct file_operations at91_clk_operations = {
  337. .open = at91_clk_open,
  338. .read = seq_read,
  339. .llseek = seq_lseek,
  340. .release = single_release,
  341. };
  342. static int __init at91_clk_debugfs_init(void)
  343. {
  344. /* /sys/kernel/debug/at91_clk */
  345. (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
  346. return 0;
  347. }
  348. postcore_initcall(at91_clk_debugfs_init);
  349. #endif
  350. /*------------------------------------------------------------------------*/
  351. /* Register a new clock */
  352. int __init clk_register(struct clk *clk)
  353. {
  354. if (clk_is_peripheral(clk)) {
  355. clk->parent = &mck;
  356. clk->mode = pmc_periph_mode;
  357. list_add_tail(&clk->node, &clocks);
  358. }
  359. else if (clk_is_sys(clk)) {
  360. clk->parent = &mck;
  361. clk->mode = pmc_sys_mode;
  362. list_add_tail(&clk->node, &clocks);
  363. }
  364. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  365. else if (clk_is_programmable(clk)) {
  366. clk->mode = pmc_sys_mode;
  367. init_programmable_clock(clk);
  368. list_add_tail(&clk->node, &clocks);
  369. }
  370. #endif
  371. return 0;
  372. }
  373. /*------------------------------------------------------------------------*/
  374. static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
  375. {
  376. unsigned mul, div;
  377. div = reg & 0xff;
  378. mul = (reg >> 16) & 0x7ff;
  379. if (div && mul) {
  380. freq /= div;
  381. freq *= mul + 1;
  382. } else
  383. freq = 0;
  384. return freq;
  385. }
  386. static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
  387. {
  388. if (pll == &pllb && (reg & AT91_PMC_USB96M))
  389. return freq / 2;
  390. else
  391. return freq;
  392. }
  393. static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
  394. {
  395. unsigned i, div = 0, mul = 0, diff = 1 << 30;
  396. unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  397. /* PLL output max 240 MHz (or 180 MHz per errata) */
  398. if (out_freq > 240000000)
  399. goto fail;
  400. for (i = 1; i < 256; i++) {
  401. int diff1;
  402. unsigned input, mul1;
  403. /*
  404. * PLL input between 1MHz and 32MHz per spec, but lower
  405. * frequences seem necessary in some cases so allow 100K.
  406. */
  407. input = main_freq / i;
  408. if (input < 100000)
  409. continue;
  410. if (input > 32000000)
  411. continue;
  412. mul1 = out_freq / input;
  413. if (mul1 > 2048)
  414. continue;
  415. if (mul1 < 2)
  416. goto fail;
  417. diff1 = out_freq - input * mul1;
  418. if (diff1 < 0)
  419. diff1 = -diff1;
  420. if (diff > diff1) {
  421. diff = diff1;
  422. div = i;
  423. mul = mul1;
  424. if (diff == 0)
  425. break;
  426. }
  427. }
  428. if (i == 256 && diff > (out_freq >> 5))
  429. goto fail;
  430. return ret | ((mul - 1) << 16) | div;
  431. fail:
  432. return 0;
  433. }
  434. static struct clk *const standard_pmc_clocks[] __initdata = {
  435. /* four primary clocks */
  436. &clk32k,
  437. &main_clk,
  438. &plla,
  439. &pllb,
  440. /* PLLB children (USB) */
  441. &udpck,
  442. &uhpck,
  443. /* MCK */
  444. &mck
  445. };
  446. int __init at91_clock_init(unsigned long main_clock)
  447. {
  448. unsigned tmp, freq, mckr;
  449. int i;
  450. /*
  451. * When the bootloader initialized the main oscillator correctly,
  452. * there's no problem using the cycle counter. But if it didn't,
  453. * or when using oscillator bypass mode, we must be told the speed
  454. * of the main clock.
  455. */
  456. if (!main_clock) {
  457. do {
  458. tmp = at91_sys_read(AT91_CKGR_MCFR);
  459. } while (!(tmp & AT91_PMC_MAINRDY));
  460. main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
  461. }
  462. main_clk.rate_hz = main_clock;
  463. /* report if PLLA is more than mildly overclocked */
  464. plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR));
  465. if (plla.rate_hz > 209000000)
  466. pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
  467. /*
  468. * USB clock init: choose 48 MHz PLLB value,
  469. * disable 48MHz clock during usb peripheral suspend.
  470. *
  471. * REVISIT: assumes MCK doesn't derive from PLLB!
  472. */
  473. at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M;
  474. pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
  475. if (cpu_is_at91rm9200()) {
  476. uhpck.pmc_mask = AT91RM9200_PMC_UHP;
  477. udpck.pmc_mask = AT91RM9200_PMC_UDP;
  478. at91_sys_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP);
  479. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() || cpu_is_at91sam9263()) {
  480. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  481. udpck.pmc_mask = AT91SAM926x_PMC_UDP;
  482. } else if (cpu_is_at91cap9()) {
  483. uhpck.pmc_mask = AT91CAP9_PMC_UHP;
  484. }
  485. at91_sys_write(AT91_CKGR_PLLBR, 0);
  486. udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  487. uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init);
  488. /*
  489. * MCK and CPU derive from one of those primary clocks.
  490. * For now, assume this parentage won't change.
  491. */
  492. mckr = at91_sys_read(AT91_PMC_MCKR);
  493. mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
  494. freq = mck.parent->rate_hz;
  495. freq /= (1 << ((mckr & AT91_PMC_PRES) >> 2)); /* prescale */
  496. if (cpu_is_at91rm9200())
  497. mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  498. else
  499. mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  500. /* Register the PMC's standard clocks */
  501. for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
  502. list_add_tail(&standard_pmc_clocks[i]->node, &clocks);
  503. /* MCK and CPU clock are "always on" */
  504. clk_enable(&mck);
  505. printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
  506. freq / 1000000, (unsigned) mck.rate_hz / 1000000,
  507. (unsigned) main_clock / 1000000,
  508. ((unsigned) main_clock % 1000000) / 1000);
  509. return 0;
  510. }
  511. /*
  512. * Several unused clocks may be active. Turn them off.
  513. */
  514. static int __init at91_clock_reset(void)
  515. {
  516. unsigned long pcdr = 0;
  517. unsigned long scdr = 0;
  518. struct clk *clk;
  519. list_for_each_entry(clk, &clocks, node) {
  520. if (clk->users > 0)
  521. continue;
  522. if (clk->mode == pmc_periph_mode)
  523. pcdr |= clk->pmc_mask;
  524. if (clk->mode == pmc_sys_mode)
  525. scdr |= clk->pmc_mask;
  526. pr_debug("Clocks: disable unused %s\n", clk->name);
  527. }
  528. at91_sys_write(AT91_PMC_PCDR, pcdr);
  529. at91_sys_write(AT91_PMC_SCDR, scdr);
  530. return 0;
  531. }
  532. late_initcall(at91_clock_reset);