clock.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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 <linux/io.h>
  25. #include <linux/of_address.h>
  26. #include <mach/hardware.h>
  27. #include <mach/at91_pmc.h>
  28. #include <mach/cpu.h>
  29. #include <asm/proc-fns.h>
  30. #include "clock.h"
  31. #include "generic.h"
  32. void __iomem *at91_pmc_base;
  33. EXPORT_SYMBOL_GPL(at91_pmc_base);
  34. /*
  35. * There's a lot more which can be done with clocks, including cpufreq
  36. * integration, slow clock mode support (for system suspend), letting
  37. * PLLB be used at other rates (on boards that don't need USB), etc.
  38. */
  39. #define clk_is_primary(x) ((x)->type & CLK_TYPE_PRIMARY)
  40. #define clk_is_programmable(x) ((x)->type & CLK_TYPE_PROGRAMMABLE)
  41. #define clk_is_peripheral(x) ((x)->type & CLK_TYPE_PERIPHERAL)
  42. #define clk_is_sys(x) ((x)->type & CLK_TYPE_SYSTEM)
  43. /*
  44. * Chips have some kind of clocks : group them by functionality
  45. */
  46. #define cpu_has_utmi() ( cpu_is_at91sam9rl() \
  47. || cpu_is_at91sam9g45() \
  48. || cpu_is_at91sam9x5() \
  49. || cpu_is_sama5d3())
  50. #define cpu_has_1056M_plla() (cpu_is_sama5d3())
  51. #define cpu_has_800M_plla() ( cpu_is_at91sam9g20() \
  52. || cpu_is_at91sam9g45() \
  53. || cpu_is_at91sam9x5() \
  54. || cpu_is_at91sam9n12())
  55. #define cpu_has_300M_plla() (cpu_is_at91sam9g10())
  56. #define cpu_has_240M_plla() (cpu_is_at91sam9261() \
  57. || cpu_is_at91sam9263() \
  58. || cpu_is_at91sam9rl())
  59. #define cpu_has_210M_plla() (cpu_is_at91sam9260())
  60. #define cpu_has_pllb() (!(cpu_is_at91sam9rl() \
  61. || cpu_is_at91sam9g45() \
  62. || cpu_is_at91sam9x5() \
  63. || cpu_is_sama5d3()))
  64. #define cpu_has_upll() (cpu_is_at91sam9g45() \
  65. || cpu_is_at91sam9x5() \
  66. || cpu_is_sama5d3())
  67. /* USB host HS & FS */
  68. #define cpu_has_uhp() (!cpu_is_at91sam9rl())
  69. /* USB device FS only */
  70. #define cpu_has_udpfs() (!(cpu_is_at91sam9rl() \
  71. || cpu_is_at91sam9g45() \
  72. || cpu_is_at91sam9x5() \
  73. || cpu_is_sama5d3()))
  74. #define cpu_has_plladiv2() (cpu_is_at91sam9g45() \
  75. || cpu_is_at91sam9x5() \
  76. || cpu_is_at91sam9n12() \
  77. || cpu_is_sama5d3())
  78. #define cpu_has_mdiv3() (cpu_is_at91sam9g45() \
  79. || cpu_is_at91sam9x5() \
  80. || cpu_is_at91sam9n12() \
  81. || cpu_is_sama5d3())
  82. #define cpu_has_alt_prescaler() (cpu_is_at91sam9x5() \
  83. || cpu_is_at91sam9n12() \
  84. || cpu_is_sama5d3())
  85. static LIST_HEAD(clocks);
  86. static DEFINE_SPINLOCK(clk_lock);
  87. static u32 at91_pllb_usb_init;
  88. /*
  89. * Four primary clock sources: two crystal oscillators (32K, main), and
  90. * two PLLs. PLLA usually runs the master clock; and PLLB must run at
  91. * 48 MHz (unless no USB function clocks are needed). The main clock and
  92. * both PLLs are turned off to run in "slow clock mode" (system suspend).
  93. */
  94. static struct clk clk32k = {
  95. .name = "clk32k",
  96. .rate_hz = AT91_SLOW_CLOCK,
  97. .users = 1, /* always on */
  98. .id = 0,
  99. .type = CLK_TYPE_PRIMARY,
  100. };
  101. static struct clk main_clk = {
  102. .name = "main",
  103. .pmc_mask = AT91_PMC_MOSCS, /* in PMC_SR */
  104. .id = 1,
  105. .type = CLK_TYPE_PRIMARY,
  106. };
  107. static struct clk plla = {
  108. .name = "plla",
  109. .parent = &main_clk,
  110. .pmc_mask = AT91_PMC_LOCKA, /* in PMC_SR */
  111. .id = 2,
  112. .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
  113. };
  114. static void pllb_mode(struct clk *clk, int is_on)
  115. {
  116. u32 value;
  117. if (is_on) {
  118. is_on = AT91_PMC_LOCKB;
  119. value = at91_pllb_usb_init;
  120. } else
  121. value = 0;
  122. // REVISIT: Add work-around for AT91RM9200 Errata #26 ?
  123. at91_pmc_write(AT91_CKGR_PLLBR, value);
  124. do {
  125. cpu_relax();
  126. } while ((at91_pmc_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on);
  127. }
  128. static struct clk pllb = {
  129. .name = "pllb",
  130. .parent = &main_clk,
  131. .pmc_mask = AT91_PMC_LOCKB, /* in PMC_SR */
  132. .mode = pllb_mode,
  133. .id = 3,
  134. .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL,
  135. };
  136. static void pmc_sys_mode(struct clk *clk, int is_on)
  137. {
  138. if (is_on)
  139. at91_pmc_write(AT91_PMC_SCER, clk->pmc_mask);
  140. else
  141. at91_pmc_write(AT91_PMC_SCDR, clk->pmc_mask);
  142. }
  143. static void pmc_uckr_mode(struct clk *clk, int is_on)
  144. {
  145. unsigned int uckr = at91_pmc_read(AT91_CKGR_UCKR);
  146. if (is_on) {
  147. is_on = AT91_PMC_LOCKU;
  148. at91_pmc_write(AT91_CKGR_UCKR, uckr | clk->pmc_mask);
  149. } else
  150. at91_pmc_write(AT91_CKGR_UCKR, uckr & ~(clk->pmc_mask));
  151. do {
  152. cpu_relax();
  153. } while ((at91_pmc_read(AT91_PMC_SR) & AT91_PMC_LOCKU) != is_on);
  154. }
  155. /* USB function clocks (PLLB must be 48 MHz) */
  156. static struct clk udpck = {
  157. .name = "udpck",
  158. .parent = &pllb,
  159. .mode = pmc_sys_mode,
  160. };
  161. struct clk utmi_clk = {
  162. .name = "utmi_clk",
  163. .parent = &main_clk,
  164. .pmc_mask = AT91_PMC_UPLLEN, /* in CKGR_UCKR */
  165. .mode = pmc_uckr_mode,
  166. .type = CLK_TYPE_PLL,
  167. };
  168. static struct clk uhpck = {
  169. .name = "uhpck",
  170. /*.parent = ... we choose parent at runtime */
  171. .mode = pmc_sys_mode,
  172. };
  173. /*
  174. * The master clock is divided from the CPU clock (by 1-4). It's used for
  175. * memory, interfaces to on-chip peripherals, the AIC, and sometimes more
  176. * (e.g baud rate generation). It's sourced from one of the primary clocks.
  177. */
  178. struct clk mck = {
  179. .name = "mck",
  180. .pmc_mask = AT91_PMC_MCKRDY, /* in PMC_SR */
  181. };
  182. static void pmc_periph_mode(struct clk *clk, int is_on)
  183. {
  184. u32 regval = 0;
  185. /*
  186. * With sama5d3 devices, we are managing clock division so we have to
  187. * use the Peripheral Control Register introduced from at91sam9x5
  188. * devices.
  189. */
  190. if (cpu_is_sama5d3()) {
  191. regval |= AT91_PMC_PCR_CMD; /* write command */
  192. regval |= clk->pid & AT91_PMC_PCR_PID; /* peripheral selection */
  193. regval |= AT91_PMC_PCR_DIV(clk->div);
  194. if (is_on)
  195. regval |= AT91_PMC_PCR_EN; /* enable clock */
  196. at91_pmc_write(AT91_PMC_PCR, regval);
  197. } else {
  198. if (is_on)
  199. at91_pmc_write(AT91_PMC_PCER, clk->pmc_mask);
  200. else
  201. at91_pmc_write(AT91_PMC_PCDR, clk->pmc_mask);
  202. }
  203. }
  204. static struct clk __init *at91_css_to_clk(unsigned long css)
  205. {
  206. switch (css) {
  207. case AT91_PMC_CSS_SLOW:
  208. return &clk32k;
  209. case AT91_PMC_CSS_MAIN:
  210. return &main_clk;
  211. case AT91_PMC_CSS_PLLA:
  212. return &plla;
  213. case AT91_PMC_CSS_PLLB:
  214. if (cpu_has_upll())
  215. /* CSS_PLLB == CSS_UPLL */
  216. return &utmi_clk;
  217. else if (cpu_has_pllb())
  218. return &pllb;
  219. break;
  220. /* alternate PMC: can use master clock */
  221. case AT91_PMC_CSS_MASTER:
  222. return &mck;
  223. }
  224. return NULL;
  225. }
  226. static int pmc_prescaler_divider(u32 reg)
  227. {
  228. if (cpu_has_alt_prescaler()) {
  229. return 1 << ((reg & AT91_PMC_ALT_PRES) >> PMC_ALT_PRES_OFFSET);
  230. } else {
  231. return 1 << ((reg & AT91_PMC_PRES) >> PMC_PRES_OFFSET);
  232. }
  233. }
  234. static void __clk_enable(struct clk *clk)
  235. {
  236. if (clk->parent)
  237. __clk_enable(clk->parent);
  238. if (clk->users++ == 0 && clk->mode)
  239. clk->mode(clk, 1);
  240. }
  241. int clk_enable(struct clk *clk)
  242. {
  243. unsigned long flags;
  244. spin_lock_irqsave(&clk_lock, flags);
  245. __clk_enable(clk);
  246. spin_unlock_irqrestore(&clk_lock, flags);
  247. return 0;
  248. }
  249. EXPORT_SYMBOL(clk_enable);
  250. static void __clk_disable(struct clk *clk)
  251. {
  252. BUG_ON(clk->users == 0);
  253. if (--clk->users == 0 && clk->mode)
  254. clk->mode(clk, 0);
  255. if (clk->parent)
  256. __clk_disable(clk->parent);
  257. }
  258. void clk_disable(struct clk *clk)
  259. {
  260. unsigned long flags;
  261. spin_lock_irqsave(&clk_lock, flags);
  262. __clk_disable(clk);
  263. spin_unlock_irqrestore(&clk_lock, flags);
  264. }
  265. EXPORT_SYMBOL(clk_disable);
  266. unsigned long clk_get_rate(struct clk *clk)
  267. {
  268. unsigned long flags;
  269. unsigned long rate;
  270. spin_lock_irqsave(&clk_lock, flags);
  271. for (;;) {
  272. rate = clk->rate_hz;
  273. if (rate || !clk->parent)
  274. break;
  275. clk = clk->parent;
  276. }
  277. spin_unlock_irqrestore(&clk_lock, flags);
  278. return rate;
  279. }
  280. EXPORT_SYMBOL(clk_get_rate);
  281. /*------------------------------------------------------------------------*/
  282. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  283. /*
  284. * For now, only the programmable clocks support reparenting (MCK could
  285. * do this too, with care) or rate changing (the PLLs could do this too,
  286. * ditto MCK but that's more for cpufreq). Drivers may reparent to get
  287. * a better rate match; we don't.
  288. */
  289. long clk_round_rate(struct clk *clk, unsigned long rate)
  290. {
  291. unsigned long flags;
  292. unsigned prescale;
  293. unsigned long actual;
  294. unsigned long prev = ULONG_MAX;
  295. if (!clk_is_programmable(clk))
  296. return -EINVAL;
  297. spin_lock_irqsave(&clk_lock, flags);
  298. actual = clk->parent->rate_hz;
  299. for (prescale = 0; prescale < 7; prescale++) {
  300. if (actual > rate)
  301. prev = actual;
  302. if (actual && actual <= rate) {
  303. if ((prev - rate) < (rate - actual)) {
  304. actual = prev;
  305. prescale--;
  306. }
  307. break;
  308. }
  309. actual >>= 1;
  310. }
  311. spin_unlock_irqrestore(&clk_lock, flags);
  312. return (prescale < 7) ? actual : -ENOENT;
  313. }
  314. EXPORT_SYMBOL(clk_round_rate);
  315. int clk_set_rate(struct clk *clk, unsigned long rate)
  316. {
  317. unsigned long flags;
  318. unsigned prescale;
  319. unsigned long prescale_offset, css_mask;
  320. unsigned long actual;
  321. if (!clk_is_programmable(clk))
  322. return -EINVAL;
  323. if (clk->users)
  324. return -EBUSY;
  325. if (cpu_has_alt_prescaler()) {
  326. prescale_offset = PMC_ALT_PRES_OFFSET;
  327. css_mask = AT91_PMC_ALT_PCKR_CSS;
  328. } else {
  329. prescale_offset = PMC_PRES_OFFSET;
  330. css_mask = AT91_PMC_CSS;
  331. }
  332. spin_lock_irqsave(&clk_lock, flags);
  333. actual = clk->parent->rate_hz;
  334. for (prescale = 0; prescale < 7; prescale++) {
  335. if (actual && actual <= rate) {
  336. u32 pckr;
  337. pckr = at91_pmc_read(AT91_PMC_PCKR(clk->id));
  338. pckr &= css_mask; /* keep clock selection */
  339. pckr |= prescale << prescale_offset;
  340. at91_pmc_write(AT91_PMC_PCKR(clk->id), pckr);
  341. clk->rate_hz = actual;
  342. break;
  343. }
  344. actual >>= 1;
  345. }
  346. spin_unlock_irqrestore(&clk_lock, flags);
  347. return (prescale < 7) ? actual : -ENOENT;
  348. }
  349. EXPORT_SYMBOL(clk_set_rate);
  350. struct clk *clk_get_parent(struct clk *clk)
  351. {
  352. return clk->parent;
  353. }
  354. EXPORT_SYMBOL(clk_get_parent);
  355. int clk_set_parent(struct clk *clk, struct clk *parent)
  356. {
  357. unsigned long flags;
  358. if (clk->users)
  359. return -EBUSY;
  360. if (!clk_is_primary(parent) || !clk_is_programmable(clk))
  361. return -EINVAL;
  362. if (cpu_is_at91sam9rl() && parent->id == AT91_PMC_CSS_PLLB)
  363. return -EINVAL;
  364. spin_lock_irqsave(&clk_lock, flags);
  365. clk->rate_hz = parent->rate_hz;
  366. clk->parent = parent;
  367. at91_pmc_write(AT91_PMC_PCKR(clk->id), parent->id);
  368. spin_unlock_irqrestore(&clk_lock, flags);
  369. return 0;
  370. }
  371. EXPORT_SYMBOL(clk_set_parent);
  372. /* establish PCK0..PCKN parentage and rate */
  373. static void __init init_programmable_clock(struct clk *clk)
  374. {
  375. struct clk *parent;
  376. u32 pckr;
  377. unsigned int css_mask;
  378. if (cpu_has_alt_prescaler())
  379. css_mask = AT91_PMC_ALT_PCKR_CSS;
  380. else
  381. css_mask = AT91_PMC_CSS;
  382. pckr = at91_pmc_read(AT91_PMC_PCKR(clk->id));
  383. parent = at91_css_to_clk(pckr & css_mask);
  384. clk->parent = parent;
  385. clk->rate_hz = parent->rate_hz / pmc_prescaler_divider(pckr);
  386. }
  387. #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */
  388. /*------------------------------------------------------------------------*/
  389. #ifdef CONFIG_DEBUG_FS
  390. static int at91_clk_show(struct seq_file *s, void *unused)
  391. {
  392. u32 scsr, pcsr, pcsr1 = 0, uckr = 0, sr;
  393. struct clk *clk;
  394. scsr = at91_pmc_read(AT91_PMC_SCSR);
  395. pcsr = at91_pmc_read(AT91_PMC_PCSR);
  396. if (cpu_is_sama5d3())
  397. pcsr1 = at91_pmc_read(AT91_PMC_PCSR1);
  398. sr = at91_pmc_read(AT91_PMC_SR);
  399. seq_printf(s, "SCSR = %8x\n", scsr);
  400. seq_printf(s, "PCSR = %8x\n", pcsr);
  401. if (cpu_is_sama5d3())
  402. seq_printf(s, "PCSR1 = %8x\n", pcsr1);
  403. seq_printf(s, "MOR = %8x\n", at91_pmc_read(AT91_CKGR_MOR));
  404. seq_printf(s, "MCFR = %8x\n", at91_pmc_read(AT91_CKGR_MCFR));
  405. seq_printf(s, "PLLA = %8x\n", at91_pmc_read(AT91_CKGR_PLLAR));
  406. if (cpu_has_pllb())
  407. seq_printf(s, "PLLB = %8x\n", at91_pmc_read(AT91_CKGR_PLLBR));
  408. if (cpu_has_utmi()) {
  409. uckr = at91_pmc_read(AT91_CKGR_UCKR);
  410. seq_printf(s, "UCKR = %8x\n", uckr);
  411. }
  412. seq_printf(s, "MCKR = %8x\n", at91_pmc_read(AT91_PMC_MCKR));
  413. if (cpu_has_upll() || cpu_is_at91sam9n12())
  414. seq_printf(s, "USB = %8x\n", at91_pmc_read(AT91_PMC_USB));
  415. seq_printf(s, "SR = %8x\n", sr);
  416. seq_printf(s, "\n");
  417. list_for_each_entry(clk, &clocks, node) {
  418. char *state;
  419. if (clk->mode == pmc_sys_mode) {
  420. state = (scsr & clk->pmc_mask) ? "on" : "off";
  421. } else if (clk->mode == pmc_periph_mode) {
  422. if (cpu_is_sama5d3()) {
  423. u32 pmc_mask = 1 << (clk->pid % 32);
  424. if (clk->pid > 31)
  425. state = (pcsr1 & pmc_mask) ? "on" : "off";
  426. else
  427. state = (pcsr & pmc_mask) ? "on" : "off";
  428. } else {
  429. state = (pcsr & clk->pmc_mask) ? "on" : "off";
  430. }
  431. } else if (clk->mode == pmc_uckr_mode) {
  432. state = (uckr & clk->pmc_mask) ? "on" : "off";
  433. } else if (clk->pmc_mask) {
  434. state = (sr & clk->pmc_mask) ? "on" : "off";
  435. } else if (clk == &clk32k || clk == &main_clk) {
  436. state = "on";
  437. } else {
  438. state = "";
  439. }
  440. seq_printf(s, "%-10s users=%2d %-3s %9lu Hz %s\n",
  441. clk->name, clk->users, state, clk_get_rate(clk),
  442. clk->parent ? clk->parent->name : "");
  443. }
  444. return 0;
  445. }
  446. static int at91_clk_open(struct inode *inode, struct file *file)
  447. {
  448. return single_open(file, at91_clk_show, NULL);
  449. }
  450. static const struct file_operations at91_clk_operations = {
  451. .open = at91_clk_open,
  452. .read = seq_read,
  453. .llseek = seq_lseek,
  454. .release = single_release,
  455. };
  456. static int __init at91_clk_debugfs_init(void)
  457. {
  458. /* /sys/kernel/debug/at91_clk */
  459. (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
  460. return 0;
  461. }
  462. postcore_initcall(at91_clk_debugfs_init);
  463. #endif
  464. /*------------------------------------------------------------------------*/
  465. /* Register a new clock */
  466. static void __init at91_clk_add(struct clk *clk)
  467. {
  468. list_add_tail(&clk->node, &clocks);
  469. clk->cl.con_id = clk->name;
  470. clk->cl.clk = clk;
  471. clkdev_add(&clk->cl);
  472. }
  473. int __init clk_register(struct clk *clk)
  474. {
  475. if (clk_is_peripheral(clk)) {
  476. if (!clk->parent)
  477. clk->parent = &mck;
  478. if (cpu_is_sama5d3())
  479. clk->rate_hz = DIV_ROUND_UP(clk->parent->rate_hz,
  480. 1 << clk->div);
  481. clk->mode = pmc_periph_mode;
  482. }
  483. else if (clk_is_sys(clk)) {
  484. clk->parent = &mck;
  485. clk->mode = pmc_sys_mode;
  486. }
  487. #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS
  488. else if (clk_is_programmable(clk)) {
  489. clk->mode = pmc_sys_mode;
  490. init_programmable_clock(clk);
  491. }
  492. #endif
  493. at91_clk_add(clk);
  494. return 0;
  495. }
  496. /*------------------------------------------------------------------------*/
  497. static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
  498. {
  499. unsigned mul, div;
  500. div = reg & 0xff;
  501. if (cpu_is_sama5d3())
  502. mul = AT91_PMC3_MUL_GET(reg);
  503. else
  504. mul = AT91_PMC_MUL_GET(reg);
  505. if (div && mul) {
  506. freq /= div;
  507. freq *= mul + 1;
  508. } else
  509. freq = 0;
  510. return freq;
  511. }
  512. static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
  513. {
  514. if (pll == &pllb && (reg & AT91_PMC_USB96M))
  515. return freq / 2;
  516. else if (pll == &utmi_clk || cpu_is_at91sam9n12())
  517. return freq / (1 + ((reg & AT91_PMC_OHCIUSBDIV) >> 8));
  518. else
  519. return freq;
  520. }
  521. static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
  522. {
  523. unsigned i, div = 0, mul = 0, diff = 1 << 30;
  524. unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  525. /* PLL output max 240 MHz (or 180 MHz per errata) */
  526. if (out_freq > 240000000)
  527. goto fail;
  528. for (i = 1; i < 256; i++) {
  529. int diff1;
  530. unsigned input, mul1;
  531. /*
  532. * PLL input between 1MHz and 32MHz per spec, but lower
  533. * frequences seem necessary in some cases so allow 100K.
  534. * Warning: some newer products need 2MHz min.
  535. */
  536. input = main_freq / i;
  537. if (cpu_is_at91sam9g20() && input < 2000000)
  538. continue;
  539. if (input < 100000)
  540. continue;
  541. if (input > 32000000)
  542. continue;
  543. mul1 = out_freq / input;
  544. if (cpu_is_at91sam9g20() && mul > 63)
  545. continue;
  546. if (mul1 > 2048)
  547. continue;
  548. if (mul1 < 2)
  549. goto fail;
  550. diff1 = out_freq - input * mul1;
  551. if (diff1 < 0)
  552. diff1 = -diff1;
  553. if (diff > diff1) {
  554. diff = diff1;
  555. div = i;
  556. mul = mul1;
  557. if (diff == 0)
  558. break;
  559. }
  560. }
  561. if (i == 256 && diff > (out_freq >> 5))
  562. goto fail;
  563. return ret | ((mul - 1) << 16) | div;
  564. fail:
  565. return 0;
  566. }
  567. static struct clk *const standard_pmc_clocks[] __initconst = {
  568. /* four primary clocks */
  569. &clk32k,
  570. &main_clk,
  571. &plla,
  572. /* MCK */
  573. &mck
  574. };
  575. /* PLLB generated USB full speed clock init */
  576. static void __init at91_pllb_usbfs_clock_init(unsigned long main_clock)
  577. {
  578. unsigned int reg;
  579. /*
  580. * USB clock init: choose 48 MHz PLLB value,
  581. * disable 48MHz clock during usb peripheral suspend.
  582. *
  583. * REVISIT: assumes MCK doesn't derive from PLLB!
  584. */
  585. uhpck.parent = &pllb;
  586. reg = at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2);
  587. pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
  588. if (cpu_is_at91rm9200()) {
  589. reg = at91_pllb_usb_init |= AT91_PMC_USB96M;
  590. uhpck.pmc_mask = AT91RM9200_PMC_UHP;
  591. udpck.pmc_mask = AT91RM9200_PMC_UDP;
  592. at91_pmc_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP);
  593. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() ||
  594. cpu_is_at91sam9263() || cpu_is_at91sam9g20() ||
  595. cpu_is_at91sam9g10()) {
  596. reg = at91_pllb_usb_init |= AT91_PMC_USB96M;
  597. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  598. udpck.pmc_mask = AT91SAM926x_PMC_UDP;
  599. } else if (cpu_is_at91sam9n12()) {
  600. /* Divider for USB clock is in USB clock register for 9n12 */
  601. reg = AT91_PMC_USBS_PLLB;
  602. /* For PLLB output 96M, set usb divider 2 (USBDIV + 1) */
  603. reg |= AT91_PMC_OHCIUSBDIV_2;
  604. at91_pmc_write(AT91_PMC_USB, reg);
  605. /* Still setup masks */
  606. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  607. udpck.pmc_mask = AT91SAM926x_PMC_UDP;
  608. }
  609. at91_pmc_write(AT91_CKGR_PLLBR, 0);
  610. udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, reg);
  611. uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, reg);
  612. }
  613. /* UPLL generated USB full speed clock init */
  614. static void __init at91_upll_usbfs_clock_init(unsigned long main_clock)
  615. {
  616. /*
  617. * USB clock init: choose 480 MHz from UPLL,
  618. */
  619. unsigned int usbr = AT91_PMC_USBS_UPLL;
  620. /* Setup divider by 10 to reach 48 MHz */
  621. usbr |= ((10 - 1) << 8) & AT91_PMC_OHCIUSBDIV;
  622. at91_pmc_write(AT91_PMC_USB, usbr);
  623. /* Now set uhpck values */
  624. uhpck.parent = &utmi_clk;
  625. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  626. uhpck.rate_hz = at91_usb_rate(&utmi_clk, utmi_clk.rate_hz, usbr);
  627. }
  628. static int __init at91_pmc_init(unsigned long main_clock)
  629. {
  630. unsigned tmp, freq, mckr;
  631. int i;
  632. int pll_overclock = false;
  633. /*
  634. * When the bootloader initialized the main oscillator correctly,
  635. * there's no problem using the cycle counter. But if it didn't,
  636. * or when using oscillator bypass mode, we must be told the speed
  637. * of the main clock.
  638. */
  639. if (!main_clock) {
  640. do {
  641. tmp = at91_pmc_read(AT91_CKGR_MCFR);
  642. } while (!(tmp & AT91_PMC_MAINRDY));
  643. main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
  644. }
  645. main_clk.rate_hz = main_clock;
  646. /* report if PLLA is more than mildly overclocked */
  647. plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_pmc_read(AT91_CKGR_PLLAR));
  648. if (cpu_has_1056M_plla()) {
  649. if (plla.rate_hz > 1056000000)
  650. pll_overclock = true;
  651. } else if (cpu_has_800M_plla()) {
  652. if (plla.rate_hz > 800000000)
  653. pll_overclock = true;
  654. } else if (cpu_has_300M_plla()) {
  655. if (plla.rate_hz > 300000000)
  656. pll_overclock = true;
  657. } else if (cpu_has_240M_plla()) {
  658. if (plla.rate_hz > 240000000)
  659. pll_overclock = true;
  660. } else if (cpu_has_210M_plla()) {
  661. if (plla.rate_hz > 210000000)
  662. pll_overclock = true;
  663. } else {
  664. if (plla.rate_hz > 209000000)
  665. pll_overclock = true;
  666. }
  667. if (pll_overclock)
  668. pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
  669. if (cpu_has_plladiv2()) {
  670. mckr = at91_pmc_read(AT91_PMC_MCKR);
  671. plla.rate_hz /= (1 << ((mckr & AT91_PMC_PLLADIV2) >> 12)); /* plla divisor by 2 */
  672. }
  673. if (!cpu_has_pllb() && cpu_has_upll()) {
  674. /* setup UTMI clock as the fourth primary clock
  675. * (instead of pllb) */
  676. utmi_clk.type |= CLK_TYPE_PRIMARY;
  677. utmi_clk.id = 3;
  678. }
  679. /*
  680. * USB HS clock init
  681. */
  682. if (cpu_has_utmi()) {
  683. /*
  684. * multiplier is hard-wired to 40
  685. * (obtain the USB High Speed 480 MHz when input is 12 MHz)
  686. */
  687. utmi_clk.rate_hz = 40 * utmi_clk.parent->rate_hz;
  688. /* UTMI bias and PLL are managed at the same time */
  689. if (cpu_has_upll())
  690. utmi_clk.pmc_mask |= AT91_PMC_BIASEN;
  691. }
  692. /*
  693. * USB FS clock init
  694. */
  695. if (cpu_has_pllb())
  696. at91_pllb_usbfs_clock_init(main_clock);
  697. if (cpu_has_upll())
  698. /* assumes that we choose UPLL for USB and not PLLA */
  699. at91_upll_usbfs_clock_init(main_clock);
  700. /*
  701. * MCK and CPU derive from one of those primary clocks.
  702. * For now, assume this parentage won't change.
  703. */
  704. mckr = at91_pmc_read(AT91_PMC_MCKR);
  705. mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
  706. freq = mck.parent->rate_hz;
  707. freq /= pmc_prescaler_divider(mckr); /* prescale */
  708. if (cpu_is_at91rm9200()) {
  709. mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  710. } else if (cpu_is_at91sam9g20()) {
  711. mck.rate_hz = (mckr & AT91_PMC_MDIV) ?
  712. freq / ((mckr & AT91_PMC_MDIV) >> 7) : freq; /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
  713. if (mckr & AT91_PMC_PDIV)
  714. freq /= 2; /* processor clock division */
  715. } else if (cpu_has_mdiv3()) {
  716. mck.rate_hz = (mckr & AT91_PMC_MDIV) == AT91SAM9_PMC_MDIV_3 ?
  717. freq / 3 : freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  718. } else {
  719. mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  720. }
  721. if (cpu_has_alt_prescaler()) {
  722. /* Programmable clocks can use MCK */
  723. mck.type |= CLK_TYPE_PRIMARY;
  724. mck.id = 4;
  725. }
  726. /* Register the PMC's standard clocks */
  727. for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
  728. at91_clk_add(standard_pmc_clocks[i]);
  729. if (cpu_has_pllb())
  730. at91_clk_add(&pllb);
  731. if (cpu_has_uhp())
  732. at91_clk_add(&uhpck);
  733. if (cpu_has_udpfs())
  734. at91_clk_add(&udpck);
  735. if (cpu_has_utmi())
  736. at91_clk_add(&utmi_clk);
  737. /* MCK and CPU clock are "always on" */
  738. clk_enable(&mck);
  739. printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
  740. freq / 1000000, (unsigned) mck.rate_hz / 1000000,
  741. (unsigned) main_clock / 1000000,
  742. ((unsigned) main_clock % 1000000) / 1000);
  743. return 0;
  744. }
  745. #if defined(CONFIG_OF)
  746. static struct of_device_id pmc_ids[] = {
  747. { .compatible = "atmel,at91rm9200-pmc" },
  748. { /*sentinel*/ }
  749. };
  750. static struct of_device_id osc_ids[] = {
  751. { .compatible = "atmel,osc" },
  752. { /*sentinel*/ }
  753. };
  754. int __init at91_dt_clock_init(void)
  755. {
  756. struct device_node *np;
  757. u32 main_clock = 0;
  758. np = of_find_matching_node(NULL, pmc_ids);
  759. if (!np)
  760. panic("unable to find compatible pmc node in dtb\n");
  761. at91_pmc_base = of_iomap(np, 0);
  762. if (!at91_pmc_base)
  763. panic("unable to map pmc cpu registers\n");
  764. of_node_put(np);
  765. /* retrieve the freqency of fixed clocks from device tree */
  766. np = of_find_matching_node(NULL, osc_ids);
  767. if (np) {
  768. u32 rate;
  769. if (!of_property_read_u32(np, "clock-frequency", &rate))
  770. main_clock = rate;
  771. }
  772. of_node_put(np);
  773. return at91_pmc_init(main_clock);
  774. }
  775. #endif
  776. int __init at91_clock_init(unsigned long main_clock)
  777. {
  778. at91_pmc_base = ioremap(AT91_PMC, 256);
  779. if (!at91_pmc_base)
  780. panic("Impossible to ioremap AT91_PMC 0x%x\n", AT91_PMC);
  781. return at91_pmc_init(main_clock);
  782. }
  783. /*
  784. * Several unused clocks may be active. Turn them off.
  785. */
  786. static int __init at91_clock_reset(void)
  787. {
  788. unsigned long pcdr = 0;
  789. unsigned long pcdr1 = 0;
  790. unsigned long scdr = 0;
  791. struct clk *clk;
  792. list_for_each_entry(clk, &clocks, node) {
  793. if (clk->users > 0)
  794. continue;
  795. if (clk->mode == pmc_periph_mode) {
  796. if (cpu_is_sama5d3()) {
  797. u32 pmc_mask = 1 << (clk->pid % 32);
  798. if (clk->pid > 31)
  799. pcdr1 |= pmc_mask;
  800. else
  801. pcdr |= pmc_mask;
  802. } else
  803. pcdr |= clk->pmc_mask;
  804. }
  805. if (clk->mode == pmc_sys_mode)
  806. scdr |= clk->pmc_mask;
  807. pr_debug("Clocks: disable unused %s\n", clk->name);
  808. }
  809. at91_pmc_write(AT91_PMC_SCDR, scdr);
  810. if (cpu_is_sama5d3())
  811. at91_pmc_write(AT91_PMC_PCDR1, pcdr1);
  812. return 0;
  813. }
  814. late_initcall(at91_clock_reset);
  815. void at91sam9_idle(void)
  816. {
  817. at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
  818. cpu_do_idle();
  819. }