exynos4_bus.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /* drivers/devfreq/exynos4210_memorybus.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com/
  5. * MyungJoo Ham <myungjoo.ham@samsung.com>
  6. *
  7. * EXYNOS4 - Memory/Bus clock frequency scaling support in DEVFREQ framework
  8. * This version supports EXYNOS4210 only. This changes bus frequencies
  9. * and vddint voltages. Exynos4412/4212 should be able to be supported
  10. * with minor modifications.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/io.h>
  18. #include <linux/slab.h>
  19. #include <linux/mutex.h>
  20. #include <linux/suspend.h>
  21. #include <linux/opp.h>
  22. #include <linux/devfreq.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/module.h>
  26. /* Exynos4 ASV has been in the mailing list, but not upstreamed, yet. */
  27. #ifdef CONFIG_EXYNOS_ASV
  28. extern unsigned int exynos_result_of_asv;
  29. #endif
  30. #include <mach/regs-clock.h>
  31. #include <plat/map-s5p.h>
  32. #define MAX_SAFEVOLT 1200000 /* 1.2V */
  33. enum exynos4_busf_type {
  34. TYPE_BUSF_EXYNOS4210,
  35. TYPE_BUSF_EXYNOS4x12,
  36. };
  37. /* Assume that the bus is saturated if the utilization is 40% */
  38. #define BUS_SATURATION_RATIO 40
  39. enum ppmu_counter {
  40. PPMU_PMNCNT0 = 0,
  41. PPMU_PMCCNT1,
  42. PPMU_PMNCNT2,
  43. PPMU_PMNCNT3,
  44. PPMU_PMNCNT_MAX,
  45. };
  46. struct exynos4_ppmu {
  47. void __iomem *hw_base;
  48. unsigned int ccnt;
  49. unsigned int event;
  50. unsigned int count[PPMU_PMNCNT_MAX];
  51. bool ccnt_overflow;
  52. bool count_overflow[PPMU_PMNCNT_MAX];
  53. };
  54. enum busclk_level_idx {
  55. LV_0 = 0,
  56. LV_1,
  57. LV_2,
  58. LV_3,
  59. LV_4,
  60. _LV_END
  61. };
  62. #define EX4210_LV_MAX LV_2
  63. #define EX4x12_LV_MAX LV_4
  64. #define EX4210_LV_NUM (LV_2 + 1)
  65. #define EX4x12_LV_NUM (LV_4 + 1)
  66. struct busfreq_data {
  67. enum exynos4_busf_type type;
  68. struct device *dev;
  69. struct devfreq *devfreq;
  70. bool disabled;
  71. struct regulator *vdd_int;
  72. struct regulator *vdd_mif; /* Exynos4412/4212 only */
  73. struct opp *curr_opp;
  74. struct exynos4_ppmu dmc[2];
  75. struct notifier_block pm_notifier;
  76. struct mutex lock;
  77. /* Dividers calculated at boot/probe-time */
  78. unsigned int dmc_divtable[_LV_END]; /* DMC0 */
  79. unsigned int top_divtable[_LV_END];
  80. };
  81. struct bus_opp_table {
  82. unsigned int idx;
  83. unsigned long clk;
  84. unsigned long volt;
  85. };
  86. /* 4210 controls clock of mif and voltage of int */
  87. static struct bus_opp_table exynos4210_busclk_table[] = {
  88. {LV_0, 400000, 1150000},
  89. {LV_1, 267000, 1050000},
  90. {LV_2, 133000, 1025000},
  91. {0, 0, 0},
  92. };
  93. /*
  94. * MIF is the main control knob clock for exynox4x12 MIF/INT
  95. * clock and voltage of both mif/int are controlled.
  96. */
  97. static struct bus_opp_table exynos4x12_mifclk_table[] = {
  98. {LV_0, 400000, 1100000},
  99. {LV_1, 267000, 1000000},
  100. {LV_2, 160000, 950000},
  101. {LV_3, 133000, 950000},
  102. {LV_4, 100000, 950000},
  103. {0, 0, 0},
  104. };
  105. /*
  106. * INT is not the control knob of 4x12. LV_x is not meant to represent
  107. * the current performance. (MIF does)
  108. */
  109. static struct bus_opp_table exynos4x12_intclk_table[] = {
  110. {LV_0, 200000, 1000000},
  111. {LV_1, 160000, 950000},
  112. {LV_2, 133000, 925000},
  113. {LV_3, 100000, 900000},
  114. {0, 0, 0},
  115. };
  116. /* TODO: asv volt definitions are "__initdata"? */
  117. /* Some chips have different operating voltages */
  118. static unsigned int exynos4210_asv_volt[][EX4210_LV_NUM] = {
  119. {1150000, 1050000, 1050000},
  120. {1125000, 1025000, 1025000},
  121. {1100000, 1000000, 1000000},
  122. {1075000, 975000, 975000},
  123. {1050000, 950000, 950000},
  124. };
  125. static unsigned int exynos4x12_mif_step_50[][EX4x12_LV_NUM] = {
  126. /* 400 267 160 133 100 */
  127. {1050000, 950000, 900000, 900000, 900000}, /* ASV0 */
  128. {1050000, 950000, 900000, 900000, 900000}, /* ASV1 */
  129. {1050000, 950000, 900000, 900000, 900000}, /* ASV2 */
  130. {1050000, 900000, 900000, 900000, 900000}, /* ASV3 */
  131. {1050000, 900000, 900000, 900000, 850000}, /* ASV4 */
  132. {1050000, 900000, 900000, 850000, 850000}, /* ASV5 */
  133. {1050000, 900000, 850000, 850000, 850000}, /* ASV6 */
  134. {1050000, 900000, 850000, 850000, 850000}, /* ASV7 */
  135. {1050000, 900000, 850000, 850000, 850000}, /* ASV8 */
  136. };
  137. static unsigned int exynos4x12_int_volt[][EX4x12_LV_NUM] = {
  138. /* 200 160 133 100 */
  139. {1000000, 950000, 925000, 900000}, /* ASV0 */
  140. {975000, 925000, 925000, 900000}, /* ASV1 */
  141. {950000, 925000, 900000, 875000}, /* ASV2 */
  142. {950000, 900000, 900000, 875000}, /* ASV3 */
  143. {925000, 875000, 875000, 875000}, /* ASV4 */
  144. {900000, 850000, 850000, 850000}, /* ASV5 */
  145. {900000, 850000, 850000, 850000}, /* ASV6 */
  146. {900000, 850000, 850000, 850000}, /* ASV7 */
  147. {900000, 850000, 850000, 850000}, /* ASV8 */
  148. };
  149. /*** Clock Divider Data for Exynos4210 ***/
  150. static unsigned int exynos4210_clkdiv_dmc0[][8] = {
  151. /*
  152. * Clock divider value for following
  153. * { DIVACP, DIVACP_PCLK, DIVDPHY, DIVDMC, DIVDMCD
  154. * DIVDMCP, DIVCOPY2, DIVCORE_TIMERS }
  155. */
  156. /* DMC L0: 400MHz */
  157. { 3, 1, 1, 1, 1, 1, 3, 1 },
  158. /* DMC L1: 266.7MHz */
  159. { 4, 1, 1, 2, 1, 1, 3, 1 },
  160. /* DMC L2: 133MHz */
  161. { 5, 1, 1, 5, 1, 1, 3, 1 },
  162. };
  163. static unsigned int exynos4210_clkdiv_top[][5] = {
  164. /*
  165. * Clock divider value for following
  166. * { DIVACLK200, DIVACLK100, DIVACLK160, DIVACLK133, DIVONENAND }
  167. */
  168. /* ACLK200 L0: 200MHz */
  169. { 3, 7, 4, 5, 1 },
  170. /* ACLK200 L1: 160MHz */
  171. { 4, 7, 5, 6, 1 },
  172. /* ACLK200 L2: 133MHz */
  173. { 5, 7, 7, 7, 1 },
  174. };
  175. static unsigned int exynos4210_clkdiv_lr_bus[][2] = {
  176. /*
  177. * Clock divider value for following
  178. * { DIVGDL/R, DIVGPL/R }
  179. */
  180. /* ACLK_GDL/R L1: 200MHz */
  181. { 3, 1 },
  182. /* ACLK_GDL/R L2: 160MHz */
  183. { 4, 1 },
  184. /* ACLK_GDL/R L3: 133MHz */
  185. { 5, 1 },
  186. };
  187. /*** Clock Divider Data for Exynos4212/4412 ***/
  188. static unsigned int exynos4x12_clkdiv_dmc0[][6] = {
  189. /*
  190. * Clock divider value for following
  191. * { DIVACP, DIVACP_PCLK, DIVDPHY, DIVDMC, DIVDMCD
  192. * DIVDMCP}
  193. */
  194. /* DMC L0: 400MHz */
  195. {3, 1, 1, 1, 1, 1},
  196. /* DMC L1: 266.7MHz */
  197. {4, 1, 1, 2, 1, 1},
  198. /* DMC L2: 160MHz */
  199. {5, 1, 1, 4, 1, 1},
  200. /* DMC L3: 133MHz */
  201. {5, 1, 1, 5, 1, 1},
  202. /* DMC L4: 100MHz */
  203. {7, 1, 1, 7, 1, 1},
  204. };
  205. static unsigned int exynos4x12_clkdiv_dmc1[][6] = {
  206. /*
  207. * Clock divider value for following
  208. * { G2DACP, DIVC2C, DIVC2C_ACLK }
  209. */
  210. /* DMC L0: 400MHz */
  211. {3, 1, 1},
  212. /* DMC L1: 266.7MHz */
  213. {4, 2, 1},
  214. /* DMC L2: 160MHz */
  215. {5, 4, 1},
  216. /* DMC L3: 133MHz */
  217. {5, 5, 1},
  218. /* DMC L4: 100MHz */
  219. {7, 7, 1},
  220. };
  221. static unsigned int exynos4x12_clkdiv_top[][5] = {
  222. /*
  223. * Clock divider value for following
  224. * { DIVACLK266_GPS, DIVACLK100, DIVACLK160,
  225. DIVACLK133, DIVONENAND }
  226. */
  227. /* ACLK_GDL/R L0: 200MHz */
  228. {2, 7, 4, 5, 1},
  229. /* ACLK_GDL/R L1: 200MHz */
  230. {2, 7, 4, 5, 1},
  231. /* ACLK_GDL/R L2: 160MHz */
  232. {4, 7, 5, 7, 1},
  233. /* ACLK_GDL/R L3: 133MHz */
  234. {4, 7, 5, 7, 1},
  235. /* ACLK_GDL/R L4: 100MHz */
  236. {7, 7, 7, 7, 1},
  237. };
  238. static unsigned int exynos4x12_clkdiv_lr_bus[][2] = {
  239. /*
  240. * Clock divider value for following
  241. * { DIVGDL/R, DIVGPL/R }
  242. */
  243. /* ACLK_GDL/R L0: 200MHz */
  244. {3, 1},
  245. /* ACLK_GDL/R L1: 200MHz */
  246. {3, 1},
  247. /* ACLK_GDL/R L2: 160MHz */
  248. {4, 1},
  249. /* ACLK_GDL/R L3: 133MHz */
  250. {5, 1},
  251. /* ACLK_GDL/R L4: 100MHz */
  252. {7, 1},
  253. };
  254. static unsigned int exynos4x12_clkdiv_sclkip[][3] = {
  255. /*
  256. * Clock divider value for following
  257. * { DIVMFC, DIVJPEG, DIVFIMC0~3}
  258. */
  259. /* SCLK_MFC: 200MHz */
  260. {3, 3, 4},
  261. /* SCLK_MFC: 200MHz */
  262. {3, 3, 4},
  263. /* SCLK_MFC: 160MHz */
  264. {4, 4, 5},
  265. /* SCLK_MFC: 133MHz */
  266. {5, 5, 5},
  267. /* SCLK_MFC: 100MHz */
  268. {7, 7, 7},
  269. };
  270. static int exynos4210_set_busclk(struct busfreq_data *data, struct opp *opp)
  271. {
  272. unsigned int index;
  273. unsigned int tmp;
  274. for (index = LV_0; index < EX4210_LV_NUM; index++)
  275. if (opp_get_freq(opp) == exynos4210_busclk_table[index].clk)
  276. break;
  277. if (index == EX4210_LV_NUM)
  278. return -EINVAL;
  279. /* Change Divider - DMC0 */
  280. tmp = data->dmc_divtable[index];
  281. __raw_writel(tmp, S5P_CLKDIV_DMC0);
  282. do {
  283. tmp = __raw_readl(S5P_CLKDIV_STAT_DMC0);
  284. } while (tmp & 0x11111111);
  285. /* Change Divider - TOP */
  286. tmp = data->top_divtable[index];
  287. __raw_writel(tmp, S5P_CLKDIV_TOP);
  288. do {
  289. tmp = __raw_readl(S5P_CLKDIV_STAT_TOP);
  290. } while (tmp & 0x11111);
  291. /* Change Divider - LEFTBUS */
  292. tmp = __raw_readl(S5P_CLKDIV_LEFTBUS);
  293. tmp &= ~(S5P_CLKDIV_BUS_GDLR_MASK | S5P_CLKDIV_BUS_GPLR_MASK);
  294. tmp |= ((exynos4210_clkdiv_lr_bus[index][0] <<
  295. S5P_CLKDIV_BUS_GDLR_SHIFT) |
  296. (exynos4210_clkdiv_lr_bus[index][1] <<
  297. S5P_CLKDIV_BUS_GPLR_SHIFT));
  298. __raw_writel(tmp, S5P_CLKDIV_LEFTBUS);
  299. do {
  300. tmp = __raw_readl(S5P_CLKDIV_STAT_LEFTBUS);
  301. } while (tmp & 0x11);
  302. /* Change Divider - RIGHTBUS */
  303. tmp = __raw_readl(S5P_CLKDIV_RIGHTBUS);
  304. tmp &= ~(S5P_CLKDIV_BUS_GDLR_MASK | S5P_CLKDIV_BUS_GPLR_MASK);
  305. tmp |= ((exynos4210_clkdiv_lr_bus[index][0] <<
  306. S5P_CLKDIV_BUS_GDLR_SHIFT) |
  307. (exynos4210_clkdiv_lr_bus[index][1] <<
  308. S5P_CLKDIV_BUS_GPLR_SHIFT));
  309. __raw_writel(tmp, S5P_CLKDIV_RIGHTBUS);
  310. do {
  311. tmp = __raw_readl(S5P_CLKDIV_STAT_RIGHTBUS);
  312. } while (tmp & 0x11);
  313. return 0;
  314. }
  315. static int exynos4x12_set_busclk(struct busfreq_data *data, struct opp *opp)
  316. {
  317. unsigned int index;
  318. unsigned int tmp;
  319. for (index = LV_0; index < EX4x12_LV_NUM; index++)
  320. if (opp_get_freq(opp) == exynos4x12_mifclk_table[index].clk)
  321. break;
  322. if (index == EX4x12_LV_NUM)
  323. return -EINVAL;
  324. /* Change Divider - DMC0 */
  325. tmp = data->dmc_divtable[index];
  326. __raw_writel(tmp, S5P_CLKDIV_DMC0);
  327. do {
  328. tmp = __raw_readl(S5P_CLKDIV_STAT_DMC0);
  329. } while (tmp & 0x11111111);
  330. /* Change Divider - DMC1 */
  331. tmp = __raw_readl(S5P_CLKDIV_DMC1);
  332. tmp &= ~(S5P_CLKDIV_DMC1_G2D_ACP_MASK |
  333. S5P_CLKDIV_DMC1_C2C_MASK |
  334. S5P_CLKDIV_DMC1_C2CACLK_MASK);
  335. tmp |= ((exynos4x12_clkdiv_dmc1[index][0] <<
  336. S5P_CLKDIV_DMC1_G2D_ACP_SHIFT) |
  337. (exynos4x12_clkdiv_dmc1[index][1] <<
  338. S5P_CLKDIV_DMC1_C2C_SHIFT) |
  339. (exynos4x12_clkdiv_dmc1[index][2] <<
  340. S5P_CLKDIV_DMC1_C2CACLK_SHIFT));
  341. __raw_writel(tmp, S5P_CLKDIV_DMC1);
  342. do {
  343. tmp = __raw_readl(S5P_CLKDIV_STAT_DMC1);
  344. } while (tmp & 0x111111);
  345. /* Change Divider - TOP */
  346. tmp = __raw_readl(S5P_CLKDIV_TOP);
  347. tmp &= ~(S5P_CLKDIV_TOP_ACLK266_GPS_MASK |
  348. S5P_CLKDIV_TOP_ACLK100_MASK |
  349. S5P_CLKDIV_TOP_ACLK160_MASK |
  350. S5P_CLKDIV_TOP_ACLK133_MASK |
  351. S5P_CLKDIV_TOP_ONENAND_MASK);
  352. tmp |= ((exynos4x12_clkdiv_top[index][0] <<
  353. S5P_CLKDIV_TOP_ACLK266_GPS_SHIFT) |
  354. (exynos4x12_clkdiv_top[index][1] <<
  355. S5P_CLKDIV_TOP_ACLK100_SHIFT) |
  356. (exynos4x12_clkdiv_top[index][2] <<
  357. S5P_CLKDIV_TOP_ACLK160_SHIFT) |
  358. (exynos4x12_clkdiv_top[index][3] <<
  359. S5P_CLKDIV_TOP_ACLK133_SHIFT) |
  360. (exynos4x12_clkdiv_top[index][4] <<
  361. S5P_CLKDIV_TOP_ONENAND_SHIFT));
  362. __raw_writel(tmp, S5P_CLKDIV_TOP);
  363. do {
  364. tmp = __raw_readl(S5P_CLKDIV_STAT_TOP);
  365. } while (tmp & 0x11111);
  366. /* Change Divider - LEFTBUS */
  367. tmp = __raw_readl(S5P_CLKDIV_LEFTBUS);
  368. tmp &= ~(S5P_CLKDIV_BUS_GDLR_MASK | S5P_CLKDIV_BUS_GPLR_MASK);
  369. tmp |= ((exynos4x12_clkdiv_lr_bus[index][0] <<
  370. S5P_CLKDIV_BUS_GDLR_SHIFT) |
  371. (exynos4x12_clkdiv_lr_bus[index][1] <<
  372. S5P_CLKDIV_BUS_GPLR_SHIFT));
  373. __raw_writel(tmp, S5P_CLKDIV_LEFTBUS);
  374. do {
  375. tmp = __raw_readl(S5P_CLKDIV_STAT_LEFTBUS);
  376. } while (tmp & 0x11);
  377. /* Change Divider - RIGHTBUS */
  378. tmp = __raw_readl(S5P_CLKDIV_RIGHTBUS);
  379. tmp &= ~(S5P_CLKDIV_BUS_GDLR_MASK | S5P_CLKDIV_BUS_GPLR_MASK);
  380. tmp |= ((exynos4x12_clkdiv_lr_bus[index][0] <<
  381. S5P_CLKDIV_BUS_GDLR_SHIFT) |
  382. (exynos4x12_clkdiv_lr_bus[index][1] <<
  383. S5P_CLKDIV_BUS_GPLR_SHIFT));
  384. __raw_writel(tmp, S5P_CLKDIV_RIGHTBUS);
  385. do {
  386. tmp = __raw_readl(S5P_CLKDIV_STAT_RIGHTBUS);
  387. } while (tmp & 0x11);
  388. /* Change Divider - MFC */
  389. tmp = __raw_readl(S5P_CLKDIV_MFC);
  390. tmp &= ~(S5P_CLKDIV_MFC_MASK);
  391. tmp |= ((exynos4x12_clkdiv_sclkip[index][0] <<
  392. S5P_CLKDIV_MFC_SHIFT));
  393. __raw_writel(tmp, S5P_CLKDIV_MFC);
  394. do {
  395. tmp = __raw_readl(S5P_CLKDIV_STAT_MFC);
  396. } while (tmp & 0x1);
  397. /* Change Divider - JPEG */
  398. tmp = __raw_readl(S5P_CLKDIV_CAM1);
  399. tmp &= ~(S5P_CLKDIV_CAM1_JPEG_MASK);
  400. tmp |= ((exynos4x12_clkdiv_sclkip[index][1] <<
  401. S5P_CLKDIV_CAM1_JPEG_SHIFT));
  402. __raw_writel(tmp, S5P_CLKDIV_CAM1);
  403. do {
  404. tmp = __raw_readl(S5P_CLKDIV_STAT_CAM1);
  405. } while (tmp & 0x1);
  406. /* Change Divider - FIMC0~3 */
  407. tmp = __raw_readl(S5P_CLKDIV_CAM);
  408. tmp &= ~(S5P_CLKDIV_CAM_FIMC0_MASK | S5P_CLKDIV_CAM_FIMC1_MASK |
  409. S5P_CLKDIV_CAM_FIMC2_MASK | S5P_CLKDIV_CAM_FIMC3_MASK);
  410. tmp |= ((exynos4x12_clkdiv_sclkip[index][2] <<
  411. S5P_CLKDIV_CAM_FIMC0_SHIFT) |
  412. (exynos4x12_clkdiv_sclkip[index][2] <<
  413. S5P_CLKDIV_CAM_FIMC1_SHIFT) |
  414. (exynos4x12_clkdiv_sclkip[index][2] <<
  415. S5P_CLKDIV_CAM_FIMC2_SHIFT) |
  416. (exynos4x12_clkdiv_sclkip[index][2] <<
  417. S5P_CLKDIV_CAM_FIMC3_SHIFT));
  418. __raw_writel(tmp, S5P_CLKDIV_CAM);
  419. do {
  420. tmp = __raw_readl(S5P_CLKDIV_STAT_CAM1);
  421. } while (tmp & 0x1111);
  422. return 0;
  423. }
  424. static void busfreq_mon_reset(struct busfreq_data *data)
  425. {
  426. unsigned int i;
  427. for (i = 0; i < 2; i++) {
  428. void __iomem *ppmu_base = data->dmc[i].hw_base;
  429. /* Reset PPMU */
  430. __raw_writel(0x8000000f, ppmu_base + 0xf010);
  431. __raw_writel(0x8000000f, ppmu_base + 0xf050);
  432. __raw_writel(0x6, ppmu_base + 0xf000);
  433. __raw_writel(0x0, ppmu_base + 0xf100);
  434. /* Set PPMU Event */
  435. data->dmc[i].event = 0x6;
  436. __raw_writel(((data->dmc[i].event << 12) | 0x1),
  437. ppmu_base + 0xfc);
  438. /* Start PPMU */
  439. __raw_writel(0x1, ppmu_base + 0xf000);
  440. }
  441. }
  442. static void exynos4_read_ppmu(struct busfreq_data *data)
  443. {
  444. int i, j;
  445. for (i = 0; i < 2; i++) {
  446. void __iomem *ppmu_base = data->dmc[i].hw_base;
  447. u32 overflow;
  448. /* Stop PPMU */
  449. __raw_writel(0x0, ppmu_base + 0xf000);
  450. /* Update local data from PPMU */
  451. overflow = __raw_readl(ppmu_base + 0xf050);
  452. data->dmc[i].ccnt = __raw_readl(ppmu_base + 0xf100);
  453. data->dmc[i].ccnt_overflow = overflow & (1 << 31);
  454. for (j = 0; j < PPMU_PMNCNT_MAX; j++) {
  455. data->dmc[i].count[j] = __raw_readl(
  456. ppmu_base + (0xf110 + (0x10 * j)));
  457. data->dmc[i].count_overflow[j] = overflow & (1 << j);
  458. }
  459. }
  460. busfreq_mon_reset(data);
  461. }
  462. static int exynos4x12_get_intspec(unsigned long mifclk)
  463. {
  464. int i = 0;
  465. while (exynos4x12_intclk_table[i].clk) {
  466. if (exynos4x12_intclk_table[i].clk <= mifclk)
  467. return i;
  468. i++;
  469. }
  470. return -EINVAL;
  471. }
  472. static int exynos4_bus_setvolt(struct busfreq_data *data, struct opp *opp,
  473. struct opp *oldopp)
  474. {
  475. int err = 0, tmp;
  476. unsigned long volt = opp_get_voltage(opp);
  477. switch (data->type) {
  478. case TYPE_BUSF_EXYNOS4210:
  479. /* OPP represents DMC clock + INT voltage */
  480. err = regulator_set_voltage(data->vdd_int, volt,
  481. MAX_SAFEVOLT);
  482. break;
  483. case TYPE_BUSF_EXYNOS4x12:
  484. /* OPP represents MIF clock + MIF voltage */
  485. err = regulator_set_voltage(data->vdd_mif, volt,
  486. MAX_SAFEVOLT);
  487. if (err)
  488. break;
  489. tmp = exynos4x12_get_intspec(opp_get_freq(opp));
  490. if (tmp < 0) {
  491. err = tmp;
  492. regulator_set_voltage(data->vdd_mif,
  493. opp_get_voltage(oldopp),
  494. MAX_SAFEVOLT);
  495. break;
  496. }
  497. err = regulator_set_voltage(data->vdd_int,
  498. exynos4x12_intclk_table[tmp].volt,
  499. MAX_SAFEVOLT);
  500. /* Try to recover */
  501. if (err)
  502. regulator_set_voltage(data->vdd_mif,
  503. opp_get_voltage(oldopp),
  504. MAX_SAFEVOLT);
  505. break;
  506. default:
  507. err = -EINVAL;
  508. }
  509. return err;
  510. }
  511. static int exynos4_bus_target(struct device *dev, unsigned long *_freq)
  512. {
  513. int err = 0;
  514. struct busfreq_data *data = dev_get_drvdata(dev);
  515. struct opp *opp = devfreq_recommended_opp(dev, _freq);
  516. unsigned long old_freq = opp_get_freq(data->curr_opp);
  517. unsigned long freq = opp_get_freq(opp);
  518. if (old_freq == freq)
  519. return 0;
  520. dev_dbg(dev, "targetting %lukHz %luuV\n", freq, opp_get_voltage(opp));
  521. mutex_lock(&data->lock);
  522. if (data->disabled)
  523. goto out;
  524. if (old_freq < freq)
  525. err = exynos4_bus_setvolt(data, opp, data->curr_opp);
  526. if (err)
  527. goto out;
  528. if (old_freq != freq) {
  529. switch (data->type) {
  530. case TYPE_BUSF_EXYNOS4210:
  531. err = exynos4210_set_busclk(data, opp);
  532. break;
  533. case TYPE_BUSF_EXYNOS4x12:
  534. err = exynos4x12_set_busclk(data, opp);
  535. break;
  536. default:
  537. err = -EINVAL;
  538. }
  539. }
  540. if (err)
  541. goto out;
  542. if (old_freq > freq)
  543. err = exynos4_bus_setvolt(data, opp, data->curr_opp);
  544. if (err)
  545. goto out;
  546. data->curr_opp = opp;
  547. out:
  548. mutex_unlock(&data->lock);
  549. return err;
  550. }
  551. static int exynos4_get_busier_dmc(struct busfreq_data *data)
  552. {
  553. u64 p0 = data->dmc[0].count[0];
  554. u64 p1 = data->dmc[1].count[0];
  555. p0 *= data->dmc[1].ccnt;
  556. p1 *= data->dmc[0].ccnt;
  557. if (data->dmc[1].ccnt == 0)
  558. return 0;
  559. if (p0 > p1)
  560. return 0;
  561. return 1;
  562. }
  563. static int exynos4_bus_get_dev_status(struct device *dev,
  564. struct devfreq_dev_status *stat)
  565. {
  566. struct busfreq_data *data = dev_get_drvdata(dev);
  567. int busier_dmc;
  568. int cycles_x2 = 2; /* 2 x cycles */
  569. void __iomem *addr;
  570. u32 timing;
  571. u32 memctrl;
  572. exynos4_read_ppmu(data);
  573. busier_dmc = exynos4_get_busier_dmc(data);
  574. stat->current_frequency = opp_get_freq(data->curr_opp);
  575. if (busier_dmc)
  576. addr = S5P_VA_DMC1;
  577. else
  578. addr = S5P_VA_DMC0;
  579. memctrl = __raw_readl(addr + 0x04); /* one of DDR2/3/LPDDR2 */
  580. timing = __raw_readl(addr + 0x38); /* CL or WL/RL values */
  581. switch ((memctrl >> 8) & 0xf) {
  582. case 0x4: /* DDR2 */
  583. cycles_x2 = ((timing >> 16) & 0xf) * 2;
  584. break;
  585. case 0x5: /* LPDDR2 */
  586. case 0x6: /* DDR3 */
  587. cycles_x2 = ((timing >> 8) & 0xf) + ((timing >> 0) & 0xf);
  588. break;
  589. default:
  590. pr_err("%s: Unknown Memory Type(%d).\n", __func__,
  591. (memctrl >> 8) & 0xf);
  592. return -EINVAL;
  593. }
  594. /* Number of cycles spent on memory access */
  595. stat->busy_time = data->dmc[busier_dmc].count[0] / 2 * (cycles_x2 + 2);
  596. stat->busy_time *= 100 / BUS_SATURATION_RATIO;
  597. stat->total_time = data->dmc[busier_dmc].ccnt;
  598. /* If the counters have overflown, retry */
  599. if (data->dmc[busier_dmc].ccnt_overflow ||
  600. data->dmc[busier_dmc].count_overflow[0])
  601. return -EAGAIN;
  602. return 0;
  603. }
  604. static void exynos4_bus_exit(struct device *dev)
  605. {
  606. struct busfreq_data *data = dev_get_drvdata(dev);
  607. devfreq_unregister_opp_notifier(dev, data->devfreq);
  608. }
  609. static struct devfreq_dev_profile exynos4_devfreq_profile = {
  610. .initial_freq = 400000,
  611. .polling_ms = 50,
  612. .target = exynos4_bus_target,
  613. .get_dev_status = exynos4_bus_get_dev_status,
  614. .exit = exynos4_bus_exit,
  615. };
  616. static int exynos4210_init_tables(struct busfreq_data *data)
  617. {
  618. u32 tmp;
  619. int mgrp;
  620. int i, err = 0;
  621. tmp = __raw_readl(S5P_CLKDIV_DMC0);
  622. for (i = LV_0; i < EX4210_LV_NUM; i++) {
  623. tmp &= ~(S5P_CLKDIV_DMC0_ACP_MASK |
  624. S5P_CLKDIV_DMC0_ACPPCLK_MASK |
  625. S5P_CLKDIV_DMC0_DPHY_MASK |
  626. S5P_CLKDIV_DMC0_DMC_MASK |
  627. S5P_CLKDIV_DMC0_DMCD_MASK |
  628. S5P_CLKDIV_DMC0_DMCP_MASK |
  629. S5P_CLKDIV_DMC0_COPY2_MASK |
  630. S5P_CLKDIV_DMC0_CORETI_MASK);
  631. tmp |= ((exynos4210_clkdiv_dmc0[i][0] <<
  632. S5P_CLKDIV_DMC0_ACP_SHIFT) |
  633. (exynos4210_clkdiv_dmc0[i][1] <<
  634. S5P_CLKDIV_DMC0_ACPPCLK_SHIFT) |
  635. (exynos4210_clkdiv_dmc0[i][2] <<
  636. S5P_CLKDIV_DMC0_DPHY_SHIFT) |
  637. (exynos4210_clkdiv_dmc0[i][3] <<
  638. S5P_CLKDIV_DMC0_DMC_SHIFT) |
  639. (exynos4210_clkdiv_dmc0[i][4] <<
  640. S5P_CLKDIV_DMC0_DMCD_SHIFT) |
  641. (exynos4210_clkdiv_dmc0[i][5] <<
  642. S5P_CLKDIV_DMC0_DMCP_SHIFT) |
  643. (exynos4210_clkdiv_dmc0[i][6] <<
  644. S5P_CLKDIV_DMC0_COPY2_SHIFT) |
  645. (exynos4210_clkdiv_dmc0[i][7] <<
  646. S5P_CLKDIV_DMC0_CORETI_SHIFT));
  647. data->dmc_divtable[i] = tmp;
  648. }
  649. tmp = __raw_readl(S5P_CLKDIV_TOP);
  650. for (i = LV_0; i < EX4210_LV_NUM; i++) {
  651. tmp &= ~(S5P_CLKDIV_TOP_ACLK200_MASK |
  652. S5P_CLKDIV_TOP_ACLK100_MASK |
  653. S5P_CLKDIV_TOP_ACLK160_MASK |
  654. S5P_CLKDIV_TOP_ACLK133_MASK |
  655. S5P_CLKDIV_TOP_ONENAND_MASK);
  656. tmp |= ((exynos4210_clkdiv_top[i][0] <<
  657. S5P_CLKDIV_TOP_ACLK200_SHIFT) |
  658. (exynos4210_clkdiv_top[i][1] <<
  659. S5P_CLKDIV_TOP_ACLK100_SHIFT) |
  660. (exynos4210_clkdiv_top[i][2] <<
  661. S5P_CLKDIV_TOP_ACLK160_SHIFT) |
  662. (exynos4210_clkdiv_top[i][3] <<
  663. S5P_CLKDIV_TOP_ACLK133_SHIFT) |
  664. (exynos4210_clkdiv_top[i][4] <<
  665. S5P_CLKDIV_TOP_ONENAND_SHIFT));
  666. data->top_divtable[i] = tmp;
  667. }
  668. #ifdef CONFIG_EXYNOS_ASV
  669. tmp = exynos4_result_of_asv;
  670. #else
  671. tmp = 0; /* Max voltages for the reliability of the unknown */
  672. #endif
  673. pr_debug("ASV Group of Exynos4 is %d\n", tmp);
  674. /* Use merged grouping for voltage */
  675. switch (tmp) {
  676. case 0:
  677. mgrp = 0;
  678. break;
  679. case 1:
  680. case 2:
  681. mgrp = 1;
  682. break;
  683. case 3:
  684. case 4:
  685. mgrp = 2;
  686. break;
  687. case 5:
  688. case 6:
  689. mgrp = 3;
  690. break;
  691. case 7:
  692. mgrp = 4;
  693. break;
  694. default:
  695. pr_warn("Unknown ASV Group. Use max voltage.\n");
  696. mgrp = 0;
  697. }
  698. for (i = LV_0; i < EX4210_LV_NUM; i++)
  699. exynos4210_busclk_table[i].volt = exynos4210_asv_volt[mgrp][i];
  700. for (i = LV_0; i < EX4210_LV_NUM; i++) {
  701. err = opp_add(data->dev, exynos4210_busclk_table[i].clk,
  702. exynos4210_busclk_table[i].volt);
  703. if (err) {
  704. dev_err(data->dev, "Cannot add opp entries.\n");
  705. return err;
  706. }
  707. }
  708. return 0;
  709. }
  710. static int exynos4x12_init_tables(struct busfreq_data *data)
  711. {
  712. unsigned int i;
  713. unsigned int tmp;
  714. int ret;
  715. /* Enable pause function for DREX2 DVFS */
  716. tmp = __raw_readl(S5P_DMC_PAUSE_CTRL);
  717. tmp |= DMC_PAUSE_ENABLE;
  718. __raw_writel(tmp, S5P_DMC_PAUSE_CTRL);
  719. tmp = __raw_readl(S5P_CLKDIV_DMC0);
  720. for (i = 0; i < EX4x12_LV_NUM; i++) {
  721. tmp &= ~(S5P_CLKDIV_DMC0_ACP_MASK |
  722. S5P_CLKDIV_DMC0_ACPPCLK_MASK |
  723. S5P_CLKDIV_DMC0_DPHY_MASK |
  724. S5P_CLKDIV_DMC0_DMC_MASK |
  725. S5P_CLKDIV_DMC0_DMCD_MASK |
  726. S5P_CLKDIV_DMC0_DMCP_MASK);
  727. tmp |= ((exynos4x12_clkdiv_dmc0[i][0] <<
  728. S5P_CLKDIV_DMC0_ACP_SHIFT) |
  729. (exynos4x12_clkdiv_dmc0[i][1] <<
  730. S5P_CLKDIV_DMC0_ACPPCLK_SHIFT) |
  731. (exynos4x12_clkdiv_dmc0[i][2] <<
  732. S5P_CLKDIV_DMC0_DPHY_SHIFT) |
  733. (exynos4x12_clkdiv_dmc0[i][3] <<
  734. S5P_CLKDIV_DMC0_DMC_SHIFT) |
  735. (exynos4x12_clkdiv_dmc0[i][4] <<
  736. S5P_CLKDIV_DMC0_DMCD_SHIFT) |
  737. (exynos4x12_clkdiv_dmc0[i][5] <<
  738. S5P_CLKDIV_DMC0_DMCP_SHIFT));
  739. data->dmc_divtable[i] = tmp;
  740. }
  741. #ifdef CONFIG_EXYNOS_ASV
  742. tmp = exynos4_result_of_asv;
  743. #else
  744. tmp = 0; /* Max voltages for the reliability of the unknown */
  745. #endif
  746. if (tmp > 8)
  747. tmp = 0;
  748. pr_debug("ASV Group of Exynos4x12 is %d\n", tmp);
  749. for (i = 0; i < EX4x12_LV_NUM; i++) {
  750. exynos4x12_mifclk_table[i].volt =
  751. exynos4x12_mif_step_50[tmp][i];
  752. exynos4x12_intclk_table[i].volt =
  753. exynos4x12_int_volt[tmp][i];
  754. }
  755. for (i = 0; i < EX4x12_LV_NUM; i++) {
  756. ret = opp_add(data->dev, exynos4x12_mifclk_table[i].clk,
  757. exynos4x12_mifclk_table[i].volt);
  758. if (ret) {
  759. dev_err(data->dev, "Fail to add opp entries.\n");
  760. return ret;
  761. }
  762. }
  763. return 0;
  764. }
  765. static int exynos4_busfreq_pm_notifier_event(struct notifier_block *this,
  766. unsigned long event, void *ptr)
  767. {
  768. struct busfreq_data *data = container_of(this, struct busfreq_data,
  769. pm_notifier);
  770. struct opp *opp;
  771. unsigned long maxfreq = ULONG_MAX;
  772. int err = 0;
  773. switch (event) {
  774. case PM_SUSPEND_PREPARE:
  775. /* Set Fastest and Deactivate DVFS */
  776. mutex_lock(&data->lock);
  777. data->disabled = true;
  778. opp = opp_find_freq_floor(data->dev, &maxfreq);
  779. err = exynos4_bus_setvolt(data, opp, data->curr_opp);
  780. if (err)
  781. goto unlock;
  782. switch (data->type) {
  783. case TYPE_BUSF_EXYNOS4210:
  784. err = exynos4210_set_busclk(data, opp);
  785. break;
  786. case TYPE_BUSF_EXYNOS4x12:
  787. err = exynos4x12_set_busclk(data, opp);
  788. break;
  789. default:
  790. err = -EINVAL;
  791. }
  792. if (err)
  793. goto unlock;
  794. data->curr_opp = opp;
  795. unlock:
  796. mutex_unlock(&data->lock);
  797. if (err)
  798. return err;
  799. return NOTIFY_OK;
  800. case PM_POST_RESTORE:
  801. case PM_POST_SUSPEND:
  802. /* Reactivate */
  803. mutex_lock(&data->lock);
  804. data->disabled = false;
  805. mutex_unlock(&data->lock);
  806. return NOTIFY_OK;
  807. }
  808. return NOTIFY_DONE;
  809. }
  810. static __devinit int exynos4_busfreq_probe(struct platform_device *pdev)
  811. {
  812. struct busfreq_data *data;
  813. struct opp *opp;
  814. struct device *dev = &pdev->dev;
  815. int err = 0;
  816. data = kzalloc(sizeof(struct busfreq_data), GFP_KERNEL);
  817. if (data == NULL) {
  818. dev_err(dev, "Cannot allocate memory.\n");
  819. return -ENOMEM;
  820. }
  821. data->type = pdev->id_entry->driver_data;
  822. data->dmc[0].hw_base = S5P_VA_DMC0;
  823. data->dmc[1].hw_base = S5P_VA_DMC1;
  824. data->pm_notifier.notifier_call = exynos4_busfreq_pm_notifier_event;
  825. data->dev = dev;
  826. mutex_init(&data->lock);
  827. switch (data->type) {
  828. case TYPE_BUSF_EXYNOS4210:
  829. err = exynos4210_init_tables(data);
  830. break;
  831. case TYPE_BUSF_EXYNOS4x12:
  832. err = exynos4x12_init_tables(data);
  833. break;
  834. default:
  835. dev_err(dev, "Cannot determine the device id %d\n", data->type);
  836. err = -EINVAL;
  837. }
  838. if (err)
  839. goto err_regulator;
  840. data->vdd_int = regulator_get(dev, "vdd_int");
  841. if (IS_ERR(data->vdd_int)) {
  842. dev_err(dev, "Cannot get the regulator \"vdd_int\"\n");
  843. err = PTR_ERR(data->vdd_int);
  844. goto err_regulator;
  845. }
  846. if (data->type == TYPE_BUSF_EXYNOS4x12) {
  847. data->vdd_mif = regulator_get(dev, "vdd_mif");
  848. if (IS_ERR(data->vdd_mif)) {
  849. dev_err(dev, "Cannot get the regulator \"vdd_mif\"\n");
  850. err = PTR_ERR(data->vdd_mif);
  851. regulator_put(data->vdd_int);
  852. goto err_regulator;
  853. }
  854. }
  855. opp = opp_find_freq_floor(dev, &exynos4_devfreq_profile.initial_freq);
  856. if (IS_ERR(opp)) {
  857. dev_err(dev, "Invalid initial frequency %lu kHz.\n",
  858. exynos4_devfreq_profile.initial_freq);
  859. err = PTR_ERR(opp);
  860. goto err_opp_add;
  861. }
  862. data->curr_opp = opp;
  863. platform_set_drvdata(pdev, data);
  864. busfreq_mon_reset(data);
  865. data->devfreq = devfreq_add_device(dev, &exynos4_devfreq_profile,
  866. &devfreq_simple_ondemand, NULL);
  867. if (IS_ERR(data->devfreq)) {
  868. err = PTR_ERR(data->devfreq);
  869. goto err_opp_add;
  870. }
  871. devfreq_register_opp_notifier(dev, data->devfreq);
  872. err = register_pm_notifier(&data->pm_notifier);
  873. if (err) {
  874. dev_err(dev, "Failed to setup pm notifier\n");
  875. goto err_devfreq_add;
  876. }
  877. return 0;
  878. err_devfreq_add:
  879. devfreq_remove_device(data->devfreq);
  880. err_opp_add:
  881. if (data->vdd_mif)
  882. regulator_put(data->vdd_mif);
  883. regulator_put(data->vdd_int);
  884. err_regulator:
  885. kfree(data);
  886. return err;
  887. }
  888. static __devexit int exynos4_busfreq_remove(struct platform_device *pdev)
  889. {
  890. struct busfreq_data *data = platform_get_drvdata(pdev);
  891. unregister_pm_notifier(&data->pm_notifier);
  892. devfreq_remove_device(data->devfreq);
  893. regulator_put(data->vdd_int);
  894. if (data->vdd_mif)
  895. regulator_put(data->vdd_mif);
  896. kfree(data);
  897. return 0;
  898. }
  899. static int exynos4_busfreq_resume(struct device *dev)
  900. {
  901. struct busfreq_data *data = dev_get_drvdata(dev);
  902. busfreq_mon_reset(data);
  903. return 0;
  904. }
  905. static const struct dev_pm_ops exynos4_busfreq_pm = {
  906. .resume = exynos4_busfreq_resume,
  907. };
  908. static const struct platform_device_id exynos4_busfreq_id[] = {
  909. { "exynos4210-busfreq", TYPE_BUSF_EXYNOS4210 },
  910. { "exynos4412-busfreq", TYPE_BUSF_EXYNOS4x12 },
  911. { "exynos4212-busfreq", TYPE_BUSF_EXYNOS4x12 },
  912. { },
  913. };
  914. static struct platform_driver exynos4_busfreq_driver = {
  915. .probe = exynos4_busfreq_probe,
  916. .remove = __devexit_p(exynos4_busfreq_remove),
  917. .id_table = exynos4_busfreq_id,
  918. .driver = {
  919. .name = "exynos4-busfreq",
  920. .owner = THIS_MODULE,
  921. .pm = &exynos4_busfreq_pm,
  922. },
  923. };
  924. static int __init exynos4_busfreq_init(void)
  925. {
  926. return platform_driver_register(&exynos4_busfreq_driver);
  927. }
  928. late_initcall(exynos4_busfreq_init);
  929. static void __exit exynos4_busfreq_exit(void)
  930. {
  931. platform_driver_unregister(&exynos4_busfreq_driver);
  932. }
  933. module_exit(exynos4_busfreq_exit);
  934. MODULE_LICENSE("GPL");
  935. MODULE_DESCRIPTION("EXYNOS4 busfreq driver with devfreq framework");
  936. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  937. MODULE_ALIAS("exynos4-busfreq");