clock.c 24 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 <linux/clk/at91_pmc.h>
  27. #include <mach/hardware.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. /*
  283. * For now, only the programmable clocks support reparenting (MCK could
  284. * do this too, with care) or rate changing (the PLLs could do this too,
  285. * ditto MCK but that's more for cpufreq). Drivers may reparent to get
  286. * a better rate match; we don't.
  287. */
  288. long clk_round_rate(struct clk *clk, unsigned long rate)
  289. {
  290. unsigned long flags;
  291. unsigned prescale;
  292. unsigned long actual;
  293. unsigned long prev = ULONG_MAX;
  294. if (!clk_is_programmable(clk))
  295. return -EINVAL;
  296. spin_lock_irqsave(&clk_lock, flags);
  297. actual = clk->parent->rate_hz;
  298. for (prescale = 0; prescale < 7; prescale++) {
  299. if (actual > rate)
  300. prev = actual;
  301. if (actual && actual <= rate) {
  302. if ((prev - rate) < (rate - actual)) {
  303. actual = prev;
  304. prescale--;
  305. }
  306. break;
  307. }
  308. actual >>= 1;
  309. }
  310. spin_unlock_irqrestore(&clk_lock, flags);
  311. return (prescale < 7) ? actual : -ENOENT;
  312. }
  313. EXPORT_SYMBOL(clk_round_rate);
  314. int clk_set_rate(struct clk *clk, unsigned long rate)
  315. {
  316. unsigned long flags;
  317. unsigned prescale;
  318. unsigned long prescale_offset, css_mask;
  319. unsigned long actual;
  320. if (!clk_is_programmable(clk))
  321. return -EINVAL;
  322. if (clk->users)
  323. return -EBUSY;
  324. if (cpu_has_alt_prescaler()) {
  325. prescale_offset = PMC_ALT_PRES_OFFSET;
  326. css_mask = AT91_PMC_ALT_PCKR_CSS;
  327. } else {
  328. prescale_offset = PMC_PRES_OFFSET;
  329. css_mask = AT91_PMC_CSS;
  330. }
  331. spin_lock_irqsave(&clk_lock, flags);
  332. actual = clk->parent->rate_hz;
  333. for (prescale = 0; prescale < 7; prescale++) {
  334. if (actual && actual <= rate) {
  335. u32 pckr;
  336. pckr = at91_pmc_read(AT91_PMC_PCKR(clk->id));
  337. pckr &= css_mask; /* keep clock selection */
  338. pckr |= prescale << prescale_offset;
  339. at91_pmc_write(AT91_PMC_PCKR(clk->id), pckr);
  340. clk->rate_hz = actual;
  341. break;
  342. }
  343. actual >>= 1;
  344. }
  345. spin_unlock_irqrestore(&clk_lock, flags);
  346. return (prescale < 7) ? actual : -ENOENT;
  347. }
  348. EXPORT_SYMBOL(clk_set_rate);
  349. struct clk *clk_get_parent(struct clk *clk)
  350. {
  351. return clk->parent;
  352. }
  353. EXPORT_SYMBOL(clk_get_parent);
  354. int clk_set_parent(struct clk *clk, struct clk *parent)
  355. {
  356. unsigned long flags;
  357. if (clk->users)
  358. return -EBUSY;
  359. if (!clk_is_primary(parent) || !clk_is_programmable(clk))
  360. return -EINVAL;
  361. if (cpu_is_at91sam9rl() && parent->id == AT91_PMC_CSS_PLLB)
  362. return -EINVAL;
  363. spin_lock_irqsave(&clk_lock, flags);
  364. clk->rate_hz = parent->rate_hz;
  365. clk->parent = parent;
  366. at91_pmc_write(AT91_PMC_PCKR(clk->id), parent->id);
  367. spin_unlock_irqrestore(&clk_lock, flags);
  368. return 0;
  369. }
  370. EXPORT_SYMBOL(clk_set_parent);
  371. /* establish PCK0..PCKN parentage and rate */
  372. static void __init init_programmable_clock(struct clk *clk)
  373. {
  374. struct clk *parent;
  375. u32 pckr;
  376. unsigned int css_mask;
  377. if (cpu_has_alt_prescaler())
  378. css_mask = AT91_PMC_ALT_PCKR_CSS;
  379. else
  380. css_mask = AT91_PMC_CSS;
  381. pckr = at91_pmc_read(AT91_PMC_PCKR(clk->id));
  382. parent = at91_css_to_clk(pckr & css_mask);
  383. clk->parent = parent;
  384. clk->rate_hz = parent->rate_hz / pmc_prescaler_divider(pckr);
  385. }
  386. /*------------------------------------------------------------------------*/
  387. #ifdef CONFIG_DEBUG_FS
  388. static int at91_clk_show(struct seq_file *s, void *unused)
  389. {
  390. u32 scsr, pcsr, pcsr1 = 0, uckr = 0, sr;
  391. struct clk *clk;
  392. scsr = at91_pmc_read(AT91_PMC_SCSR);
  393. pcsr = at91_pmc_read(AT91_PMC_PCSR);
  394. if (cpu_is_sama5d3())
  395. pcsr1 = at91_pmc_read(AT91_PMC_PCSR1);
  396. sr = at91_pmc_read(AT91_PMC_SR);
  397. seq_printf(s, "SCSR = %8x\n", scsr);
  398. seq_printf(s, "PCSR = %8x\n", pcsr);
  399. if (cpu_is_sama5d3())
  400. seq_printf(s, "PCSR1 = %8x\n", pcsr1);
  401. seq_printf(s, "MOR = %8x\n", at91_pmc_read(AT91_CKGR_MOR));
  402. seq_printf(s, "MCFR = %8x\n", at91_pmc_read(AT91_CKGR_MCFR));
  403. seq_printf(s, "PLLA = %8x\n", at91_pmc_read(AT91_CKGR_PLLAR));
  404. if (cpu_has_pllb())
  405. seq_printf(s, "PLLB = %8x\n", at91_pmc_read(AT91_CKGR_PLLBR));
  406. if (cpu_has_utmi()) {
  407. uckr = at91_pmc_read(AT91_CKGR_UCKR);
  408. seq_printf(s, "UCKR = %8x\n", uckr);
  409. }
  410. seq_printf(s, "MCKR = %8x\n", at91_pmc_read(AT91_PMC_MCKR));
  411. if (cpu_has_upll() || cpu_is_at91sam9n12())
  412. seq_printf(s, "USB = %8x\n", at91_pmc_read(AT91_PMC_USB));
  413. seq_printf(s, "SR = %8x\n", sr);
  414. seq_printf(s, "\n");
  415. list_for_each_entry(clk, &clocks, node) {
  416. char *state;
  417. if (clk->mode == pmc_sys_mode) {
  418. state = (scsr & clk->pmc_mask) ? "on" : "off";
  419. } else if (clk->mode == pmc_periph_mode) {
  420. if (cpu_is_sama5d3()) {
  421. u32 pmc_mask = 1 << (clk->pid % 32);
  422. if (clk->pid > 31)
  423. state = (pcsr1 & pmc_mask) ? "on" : "off";
  424. else
  425. state = (pcsr & pmc_mask) ? "on" : "off";
  426. } else {
  427. state = (pcsr & clk->pmc_mask) ? "on" : "off";
  428. }
  429. } else if (clk->mode == pmc_uckr_mode) {
  430. state = (uckr & clk->pmc_mask) ? "on" : "off";
  431. } else if (clk->pmc_mask) {
  432. state = (sr & clk->pmc_mask) ? "on" : "off";
  433. } else if (clk == &clk32k || clk == &main_clk) {
  434. state = "on";
  435. } else {
  436. state = "";
  437. }
  438. seq_printf(s, "%-10s users=%2d %-3s %9lu Hz %s\n",
  439. clk->name, clk->users, state, clk_get_rate(clk),
  440. clk->parent ? clk->parent->name : "");
  441. }
  442. return 0;
  443. }
  444. static int at91_clk_open(struct inode *inode, struct file *file)
  445. {
  446. return single_open(file, at91_clk_show, NULL);
  447. }
  448. static const struct file_operations at91_clk_operations = {
  449. .open = at91_clk_open,
  450. .read = seq_read,
  451. .llseek = seq_lseek,
  452. .release = single_release,
  453. };
  454. static int __init at91_clk_debugfs_init(void)
  455. {
  456. /* /sys/kernel/debug/at91_clk */
  457. (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations);
  458. return 0;
  459. }
  460. postcore_initcall(at91_clk_debugfs_init);
  461. #endif
  462. /*------------------------------------------------------------------------*/
  463. /* Register a new clock */
  464. static void __init at91_clk_add(struct clk *clk)
  465. {
  466. list_add_tail(&clk->node, &clocks);
  467. clk->cl.con_id = clk->name;
  468. clk->cl.clk = clk;
  469. clkdev_add(&clk->cl);
  470. }
  471. int __init clk_register(struct clk *clk)
  472. {
  473. if (clk_is_peripheral(clk)) {
  474. if (!clk->parent)
  475. clk->parent = &mck;
  476. if (cpu_is_sama5d3())
  477. clk->rate_hz = DIV_ROUND_UP(clk->parent->rate_hz,
  478. 1 << clk->div);
  479. clk->mode = pmc_periph_mode;
  480. }
  481. else if (clk_is_sys(clk)) {
  482. clk->parent = &mck;
  483. clk->mode = pmc_sys_mode;
  484. }
  485. else if (clk_is_programmable(clk)) {
  486. clk->mode = pmc_sys_mode;
  487. init_programmable_clock(clk);
  488. }
  489. at91_clk_add(clk);
  490. return 0;
  491. }
  492. /*------------------------------------------------------------------------*/
  493. static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg)
  494. {
  495. unsigned mul, div;
  496. div = reg & 0xff;
  497. if (cpu_is_sama5d3())
  498. mul = AT91_PMC3_MUL_GET(reg);
  499. else
  500. mul = AT91_PMC_MUL_GET(reg);
  501. if (div && mul) {
  502. freq /= div;
  503. freq *= mul + 1;
  504. } else
  505. freq = 0;
  506. return freq;
  507. }
  508. static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg)
  509. {
  510. if (pll == &pllb && (reg & AT91_PMC_USB96M))
  511. return freq / 2;
  512. else if (pll == &utmi_clk || cpu_is_at91sam9n12())
  513. return freq / (1 + ((reg & AT91_PMC_OHCIUSBDIV) >> 8));
  514. else
  515. return freq;
  516. }
  517. static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq)
  518. {
  519. unsigned i, div = 0, mul = 0, diff = 1 << 30;
  520. unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00;
  521. /* PLL output max 240 MHz (or 180 MHz per errata) */
  522. if (out_freq > 240000000)
  523. goto fail;
  524. for (i = 1; i < 256; i++) {
  525. int diff1;
  526. unsigned input, mul1;
  527. /*
  528. * PLL input between 1MHz and 32MHz per spec, but lower
  529. * frequences seem necessary in some cases so allow 100K.
  530. * Warning: some newer products need 2MHz min.
  531. */
  532. input = main_freq / i;
  533. if (cpu_is_at91sam9g20() && input < 2000000)
  534. continue;
  535. if (input < 100000)
  536. continue;
  537. if (input > 32000000)
  538. continue;
  539. mul1 = out_freq / input;
  540. if (cpu_is_at91sam9g20() && mul > 63)
  541. continue;
  542. if (mul1 > 2048)
  543. continue;
  544. if (mul1 < 2)
  545. goto fail;
  546. diff1 = out_freq - input * mul1;
  547. if (diff1 < 0)
  548. diff1 = -diff1;
  549. if (diff > diff1) {
  550. diff = diff1;
  551. div = i;
  552. mul = mul1;
  553. if (diff == 0)
  554. break;
  555. }
  556. }
  557. if (i == 256 && diff > (out_freq >> 5))
  558. goto fail;
  559. return ret | ((mul - 1) << 16) | div;
  560. fail:
  561. return 0;
  562. }
  563. static struct clk *const standard_pmc_clocks[] __initconst = {
  564. /* four primary clocks */
  565. &clk32k,
  566. &main_clk,
  567. &plla,
  568. /* MCK */
  569. &mck
  570. };
  571. /* PLLB generated USB full speed clock init */
  572. static void __init at91_pllb_usbfs_clock_init(unsigned long main_clock)
  573. {
  574. unsigned int reg;
  575. /*
  576. * USB clock init: choose 48 MHz PLLB value,
  577. * disable 48MHz clock during usb peripheral suspend.
  578. *
  579. * REVISIT: assumes MCK doesn't derive from PLLB!
  580. */
  581. uhpck.parent = &pllb;
  582. reg = at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2);
  583. pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init);
  584. if (cpu_is_at91rm9200()) {
  585. reg = at91_pllb_usb_init |= AT91_PMC_USB96M;
  586. uhpck.pmc_mask = AT91RM9200_PMC_UHP;
  587. udpck.pmc_mask = AT91RM9200_PMC_UDP;
  588. at91_pmc_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP);
  589. } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() ||
  590. cpu_is_at91sam9263() || cpu_is_at91sam9g20() ||
  591. cpu_is_at91sam9g10()) {
  592. reg = at91_pllb_usb_init |= AT91_PMC_USB96M;
  593. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  594. udpck.pmc_mask = AT91SAM926x_PMC_UDP;
  595. } else if (cpu_is_at91sam9n12()) {
  596. /* Divider for USB clock is in USB clock register for 9n12 */
  597. reg = AT91_PMC_USBS_PLLB;
  598. /* For PLLB output 96M, set usb divider 2 (USBDIV + 1) */
  599. reg |= AT91_PMC_OHCIUSBDIV_2;
  600. at91_pmc_write(AT91_PMC_USB, reg);
  601. /* Still setup masks */
  602. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  603. udpck.pmc_mask = AT91SAM926x_PMC_UDP;
  604. }
  605. at91_pmc_write(AT91_CKGR_PLLBR, 0);
  606. udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, reg);
  607. uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, reg);
  608. }
  609. /* UPLL generated USB full speed clock init */
  610. static void __init at91_upll_usbfs_clock_init(unsigned long main_clock)
  611. {
  612. /*
  613. * USB clock init: choose 480 MHz from UPLL,
  614. */
  615. unsigned int usbr = AT91_PMC_USBS_UPLL;
  616. /* Setup divider by 10 to reach 48 MHz */
  617. usbr |= ((10 - 1) << 8) & AT91_PMC_OHCIUSBDIV;
  618. at91_pmc_write(AT91_PMC_USB, usbr);
  619. /* Now set uhpck values */
  620. uhpck.parent = &utmi_clk;
  621. uhpck.pmc_mask = AT91SAM926x_PMC_UHP;
  622. uhpck.rate_hz = at91_usb_rate(&utmi_clk, utmi_clk.rate_hz, usbr);
  623. }
  624. static int __init at91_pmc_init(unsigned long main_clock)
  625. {
  626. unsigned tmp, freq, mckr;
  627. int i;
  628. int pll_overclock = false;
  629. /*
  630. * When the bootloader initialized the main oscillator correctly,
  631. * there's no problem using the cycle counter. But if it didn't,
  632. * or when using oscillator bypass mode, we must be told the speed
  633. * of the main clock.
  634. */
  635. if (!main_clock) {
  636. do {
  637. tmp = at91_pmc_read(AT91_CKGR_MCFR);
  638. } while (!(tmp & AT91_PMC_MAINRDY));
  639. main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16);
  640. }
  641. main_clk.rate_hz = main_clock;
  642. /* report if PLLA is more than mildly overclocked */
  643. plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_pmc_read(AT91_CKGR_PLLAR));
  644. if (cpu_has_1056M_plla()) {
  645. if (plla.rate_hz > 1056000000)
  646. pll_overclock = true;
  647. } else if (cpu_has_800M_plla()) {
  648. if (plla.rate_hz > 800000000)
  649. pll_overclock = true;
  650. } else if (cpu_has_300M_plla()) {
  651. if (plla.rate_hz > 300000000)
  652. pll_overclock = true;
  653. } else if (cpu_has_240M_plla()) {
  654. if (plla.rate_hz > 240000000)
  655. pll_overclock = true;
  656. } else if (cpu_has_210M_plla()) {
  657. if (plla.rate_hz > 210000000)
  658. pll_overclock = true;
  659. } else {
  660. if (plla.rate_hz > 209000000)
  661. pll_overclock = true;
  662. }
  663. if (pll_overclock)
  664. pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000);
  665. if (cpu_has_plladiv2()) {
  666. mckr = at91_pmc_read(AT91_PMC_MCKR);
  667. plla.rate_hz /= (1 << ((mckr & AT91_PMC_PLLADIV2) >> 12)); /* plla divisor by 2 */
  668. }
  669. if (!cpu_has_pllb() && cpu_has_upll()) {
  670. /* setup UTMI clock as the fourth primary clock
  671. * (instead of pllb) */
  672. utmi_clk.type |= CLK_TYPE_PRIMARY;
  673. utmi_clk.id = 3;
  674. }
  675. /*
  676. * USB HS clock init
  677. */
  678. if (cpu_has_utmi()) {
  679. /*
  680. * multiplier is hard-wired to 40
  681. * (obtain the USB High Speed 480 MHz when input is 12 MHz)
  682. */
  683. utmi_clk.rate_hz = 40 * utmi_clk.parent->rate_hz;
  684. /* UTMI bias and PLL are managed at the same time */
  685. if (cpu_has_upll())
  686. utmi_clk.pmc_mask |= AT91_PMC_BIASEN;
  687. }
  688. /*
  689. * USB FS clock init
  690. */
  691. if (cpu_has_pllb())
  692. at91_pllb_usbfs_clock_init(main_clock);
  693. if (cpu_has_upll())
  694. /* assumes that we choose UPLL for USB and not PLLA */
  695. at91_upll_usbfs_clock_init(main_clock);
  696. /*
  697. * MCK and CPU derive from one of those primary clocks.
  698. * For now, assume this parentage won't change.
  699. */
  700. mckr = at91_pmc_read(AT91_PMC_MCKR);
  701. mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS);
  702. freq = mck.parent->rate_hz;
  703. freq /= pmc_prescaler_divider(mckr); /* prescale */
  704. if (cpu_is_at91rm9200()) {
  705. mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  706. } else if (cpu_is_at91sam9g20()) {
  707. mck.rate_hz = (mckr & AT91_PMC_MDIV) ?
  708. freq / ((mckr & AT91_PMC_MDIV) >> 7) : freq; /* mdiv ; (x >> 7) = ((x >> 8) * 2) */
  709. if (mckr & AT91_PMC_PDIV)
  710. freq /= 2; /* processor clock division */
  711. } else if (cpu_has_mdiv3()) {
  712. mck.rate_hz = (mckr & AT91_PMC_MDIV) == AT91SAM9_PMC_MDIV_3 ?
  713. freq / 3 : freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  714. } else {
  715. mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */
  716. }
  717. if (cpu_has_alt_prescaler()) {
  718. /* Programmable clocks can use MCK */
  719. mck.type |= CLK_TYPE_PRIMARY;
  720. mck.id = 4;
  721. }
  722. /* Register the PMC's standard clocks */
  723. for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++)
  724. at91_clk_add(standard_pmc_clocks[i]);
  725. if (cpu_has_pllb())
  726. at91_clk_add(&pllb);
  727. if (cpu_has_uhp())
  728. at91_clk_add(&uhpck);
  729. if (cpu_has_udpfs())
  730. at91_clk_add(&udpck);
  731. if (cpu_has_utmi())
  732. at91_clk_add(&utmi_clk);
  733. /* MCK and CPU clock are "always on" */
  734. clk_enable(&mck);
  735. printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n",
  736. freq / 1000000, (unsigned) mck.rate_hz / 1000000,
  737. (unsigned) main_clock / 1000000,
  738. ((unsigned) main_clock % 1000000) / 1000);
  739. return 0;
  740. }
  741. #if defined(CONFIG_OF)
  742. static struct of_device_id pmc_ids[] = {
  743. { .compatible = "atmel,at91rm9200-pmc" },
  744. { .compatible = "atmel,at91sam9260-pmc" },
  745. { .compatible = "atmel,at91sam9g45-pmc" },
  746. { .compatible = "atmel,at91sam9n12-pmc" },
  747. { .compatible = "atmel,at91sam9x5-pmc" },
  748. { .compatible = "atmel,sama5d3-pmc" },
  749. { /*sentinel*/ }
  750. };
  751. static struct of_device_id osc_ids[] = {
  752. { .compatible = "atmel,osc" },
  753. { /*sentinel*/ }
  754. };
  755. int __init at91_dt_clock_init(void)
  756. {
  757. struct device_node *np;
  758. u32 main_clock = 0;
  759. np = of_find_matching_node(NULL, pmc_ids);
  760. if (!np)
  761. panic("unable to find compatible pmc node in dtb\n");
  762. at91_pmc_base = of_iomap(np, 0);
  763. if (!at91_pmc_base)
  764. panic("unable to map pmc cpu registers\n");
  765. of_node_put(np);
  766. /* retrieve the freqency of fixed clocks from device tree */
  767. np = of_find_matching_node(NULL, osc_ids);
  768. if (np) {
  769. u32 rate;
  770. if (!of_property_read_u32(np, "clock-frequency", &rate))
  771. main_clock = rate;
  772. }
  773. of_node_put(np);
  774. return at91_pmc_init(main_clock);
  775. }
  776. #endif
  777. int __init at91_clock_init(unsigned long main_clock)
  778. {
  779. at91_pmc_base = ioremap(AT91_PMC, 256);
  780. if (!at91_pmc_base)
  781. panic("Impossible to ioremap AT91_PMC 0x%x\n", AT91_PMC);
  782. return at91_pmc_init(main_clock);
  783. }
  784. /*
  785. * Several unused clocks may be active. Turn them off.
  786. */
  787. static int __init at91_clock_reset(void)
  788. {
  789. unsigned long pcdr = 0;
  790. unsigned long pcdr1 = 0;
  791. unsigned long scdr = 0;
  792. struct clk *clk;
  793. list_for_each_entry(clk, &clocks, node) {
  794. if (clk->users > 0)
  795. continue;
  796. if (clk->mode == pmc_periph_mode) {
  797. if (cpu_is_sama5d3()) {
  798. u32 pmc_mask = 1 << (clk->pid % 32);
  799. if (clk->pid > 31)
  800. pcdr1 |= pmc_mask;
  801. else
  802. pcdr |= pmc_mask;
  803. } else
  804. pcdr |= clk->pmc_mask;
  805. }
  806. if (clk->mode == pmc_sys_mode)
  807. scdr |= clk->pmc_mask;
  808. pr_debug("Clocks: disable unused %s\n", clk->name);
  809. }
  810. at91_pmc_write(AT91_PMC_SCDR, scdr);
  811. at91_pmc_write(AT91_PMC_PCDR, pcdr);
  812. if (cpu_is_sama5d3())
  813. at91_pmc_write(AT91_PMC_PCDR1, pcdr1);
  814. return 0;
  815. }
  816. late_initcall(at91_clock_reset);
  817. void at91sam9_idle(void)
  818. {
  819. at91_pmc_write(AT91_PMC_SCDR, AT91_PMC_PCK);
  820. cpu_do_idle();
  821. }