emif.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. /*
  2. * EMIF driver
  3. *
  4. * Copyright (C) 2012 Texas Instruments, Inc.
  5. *
  6. * Aneesh V <aneesh@ti.com>
  7. * Santosh Shilimkar <santosh.shilimkar@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/reboot.h>
  15. #include <linux/platform_data/emif_plat.h>
  16. #include <linux/io.h>
  17. #include <linux/device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/slab.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/module.h>
  23. #include <linux/list.h>
  24. #include <linux/spinlock.h>
  25. #include <memory/jedec_ddr.h>
  26. #include "emif.h"
  27. /**
  28. * struct emif_data - Per device static data for driver's use
  29. * @duplicate: Whether the DDR devices attached to this EMIF
  30. * instance are exactly same as that on EMIF1. In
  31. * this case we can save some memory and processing
  32. * @temperature_level: Maximum temperature of LPDDR2 devices attached
  33. * to this EMIF - read from MR4 register. If there
  34. * are two devices attached to this EMIF, this
  35. * value is the maximum of the two temperature
  36. * levels.
  37. * @node: node in the device list
  38. * @base: base address of memory-mapped IO registers.
  39. * @dev: device pointer.
  40. * @addressing table with addressing information from the spec
  41. * @regs_cache: An array of 'struct emif_regs' that stores
  42. * calculated register values for different
  43. * frequencies, to avoid re-calculating them on
  44. * each DVFS transition.
  45. * @curr_regs: The set of register values used in the last
  46. * frequency change (i.e. corresponding to the
  47. * frequency in effect at the moment)
  48. * @plat_data: Pointer to saved platform data.
  49. */
  50. struct emif_data {
  51. u8 duplicate;
  52. u8 temperature_level;
  53. u8 lpmode;
  54. struct list_head node;
  55. unsigned long irq_state;
  56. void __iomem *base;
  57. struct device *dev;
  58. const struct lpddr2_addressing *addressing;
  59. struct emif_regs *regs_cache[EMIF_MAX_NUM_FREQUENCIES];
  60. struct emif_regs *curr_regs;
  61. struct emif_platform_data *plat_data;
  62. };
  63. static struct emif_data *emif1;
  64. static spinlock_t emif_lock;
  65. static unsigned long irq_state;
  66. static u32 t_ck; /* DDR clock period in ps */
  67. static LIST_HEAD(device_list);
  68. /*
  69. * Calculate the period of DDR clock from frequency value
  70. */
  71. static void set_ddr_clk_period(u32 freq)
  72. {
  73. /* Divide 10^12 by frequency to get period in ps */
  74. t_ck = (u32)DIV_ROUND_UP_ULL(1000000000000ull, freq);
  75. }
  76. /*
  77. * Get the CL from SDRAM_CONFIG register
  78. */
  79. static u32 get_cl(struct emif_data *emif)
  80. {
  81. u32 cl;
  82. void __iomem *base = emif->base;
  83. cl = (readl(base + EMIF_SDRAM_CONFIG) & CL_MASK) >> CL_SHIFT;
  84. return cl;
  85. }
  86. static void set_lpmode(struct emif_data *emif, u8 lpmode)
  87. {
  88. u32 temp;
  89. void __iomem *base = emif->base;
  90. temp = readl(base + EMIF_POWER_MANAGEMENT_CONTROL);
  91. temp &= ~LP_MODE_MASK;
  92. temp |= (lpmode << LP_MODE_SHIFT);
  93. writel(temp, base + EMIF_POWER_MANAGEMENT_CONTROL);
  94. }
  95. static void do_freq_update(void)
  96. {
  97. struct emif_data *emif;
  98. /*
  99. * Workaround for errata i728: Disable LPMODE during FREQ_UPDATE
  100. *
  101. * i728 DESCRIPTION:
  102. * The EMIF automatically puts the SDRAM into self-refresh mode
  103. * after the EMIF has not performed accesses during
  104. * EMIF_PWR_MGMT_CTRL[7:4] REG_SR_TIM number of DDR clock cycles
  105. * and the EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field is set
  106. * to 0x2. If during a small window the following three events
  107. * occur:
  108. * - The SR_TIMING counter expires
  109. * - And frequency change is requested
  110. * - And OCP access is requested
  111. * Then it causes instable clock on the DDR interface.
  112. *
  113. * WORKAROUND
  114. * To avoid the occurrence of the three events, the workaround
  115. * is to disable the self-refresh when requesting a frequency
  116. * change. Before requesting a frequency change the software must
  117. * program EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x0. When the
  118. * frequency change has been done, the software can reprogram
  119. * EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x2
  120. */
  121. list_for_each_entry(emif, &device_list, node) {
  122. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  123. set_lpmode(emif, EMIF_LP_MODE_DISABLE);
  124. }
  125. /*
  126. * TODO: Do FREQ_UPDATE here when an API
  127. * is available for this as part of the new
  128. * clock framework
  129. */
  130. list_for_each_entry(emif, &device_list, node) {
  131. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  132. set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH);
  133. }
  134. }
  135. /* Find addressing table entry based on the device's type and density */
  136. static const struct lpddr2_addressing *get_addressing_table(
  137. const struct ddr_device_info *device_info)
  138. {
  139. u32 index, type, density;
  140. type = device_info->type;
  141. density = device_info->density;
  142. switch (type) {
  143. case DDR_TYPE_LPDDR2_S4:
  144. index = density - 1;
  145. break;
  146. case DDR_TYPE_LPDDR2_S2:
  147. switch (density) {
  148. case DDR_DENSITY_1Gb:
  149. case DDR_DENSITY_2Gb:
  150. index = density + 3;
  151. break;
  152. default:
  153. index = density - 1;
  154. }
  155. break;
  156. default:
  157. return NULL;
  158. }
  159. return &lpddr2_jedec_addressing_table[index];
  160. }
  161. /*
  162. * Find the the right timing table from the array of timing
  163. * tables of the device using DDR clock frequency
  164. */
  165. static const struct lpddr2_timings *get_timings_table(struct emif_data *emif,
  166. u32 freq)
  167. {
  168. u32 i, min, max, freq_nearest;
  169. const struct lpddr2_timings *timings = NULL;
  170. const struct lpddr2_timings *timings_arr = emif->plat_data->timings;
  171. struct device *dev = emif->dev;
  172. /* Start with a very high frequency - 1GHz */
  173. freq_nearest = 1000000000;
  174. /*
  175. * Find the timings table such that:
  176. * 1. the frequency range covers the required frequency(safe) AND
  177. * 2. the max_freq is closest to the required frequency(optimal)
  178. */
  179. for (i = 0; i < emif->plat_data->timings_arr_size; i++) {
  180. max = timings_arr[i].max_freq;
  181. min = timings_arr[i].min_freq;
  182. if ((freq >= min) && (freq <= max) && (max < freq_nearest)) {
  183. freq_nearest = max;
  184. timings = &timings_arr[i];
  185. }
  186. }
  187. if (!timings)
  188. dev_err(dev, "%s: couldn't find timings for - %dHz\n",
  189. __func__, freq);
  190. dev_dbg(dev, "%s: timings table: freq %d, speed bin freq %d\n",
  191. __func__, freq, freq_nearest);
  192. return timings;
  193. }
  194. static u32 get_sdram_ref_ctrl_shdw(u32 freq,
  195. const struct lpddr2_addressing *addressing)
  196. {
  197. u32 ref_ctrl_shdw = 0, val = 0, freq_khz, t_refi;
  198. /* Scale down frequency and t_refi to avoid overflow */
  199. freq_khz = freq / 1000;
  200. t_refi = addressing->tREFI_ns / 100;
  201. /*
  202. * refresh rate to be set is 'tREFI(in us) * freq in MHz
  203. * division by 10000 to account for change in units
  204. */
  205. val = t_refi * freq_khz / 10000;
  206. ref_ctrl_shdw |= val << REFRESH_RATE_SHIFT;
  207. return ref_ctrl_shdw;
  208. }
  209. static u32 get_sdram_tim_1_shdw(const struct lpddr2_timings *timings,
  210. const struct lpddr2_min_tck *min_tck,
  211. const struct lpddr2_addressing *addressing)
  212. {
  213. u32 tim1 = 0, val = 0;
  214. val = max(min_tck->tWTR, DIV_ROUND_UP(timings->tWTR, t_ck)) - 1;
  215. tim1 |= val << T_WTR_SHIFT;
  216. if (addressing->num_banks == B8)
  217. val = DIV_ROUND_UP(timings->tFAW, t_ck*4);
  218. else
  219. val = max(min_tck->tRRD, DIV_ROUND_UP(timings->tRRD, t_ck));
  220. tim1 |= (val - 1) << T_RRD_SHIFT;
  221. val = DIV_ROUND_UP(timings->tRAS_min + timings->tRPab, t_ck) - 1;
  222. tim1 |= val << T_RC_SHIFT;
  223. val = max(min_tck->tRASmin, DIV_ROUND_UP(timings->tRAS_min, t_ck));
  224. tim1 |= (val - 1) << T_RAS_SHIFT;
  225. val = max(min_tck->tWR, DIV_ROUND_UP(timings->tWR, t_ck)) - 1;
  226. tim1 |= val << T_WR_SHIFT;
  227. val = max(min_tck->tRCD, DIV_ROUND_UP(timings->tRCD, t_ck)) - 1;
  228. tim1 |= val << T_RCD_SHIFT;
  229. val = max(min_tck->tRPab, DIV_ROUND_UP(timings->tRPab, t_ck)) - 1;
  230. tim1 |= val << T_RP_SHIFT;
  231. return tim1;
  232. }
  233. static u32 get_sdram_tim_1_shdw_derated(const struct lpddr2_timings *timings,
  234. const struct lpddr2_min_tck *min_tck,
  235. const struct lpddr2_addressing *addressing)
  236. {
  237. u32 tim1 = 0, val = 0;
  238. val = max(min_tck->tWTR, DIV_ROUND_UP(timings->tWTR, t_ck)) - 1;
  239. tim1 = val << T_WTR_SHIFT;
  240. /*
  241. * tFAW is approximately 4 times tRRD. So add 1875*4 = 7500ps
  242. * to tFAW for de-rating
  243. */
  244. if (addressing->num_banks == B8) {
  245. val = DIV_ROUND_UP(timings->tFAW + 7500, 4 * t_ck) - 1;
  246. } else {
  247. val = DIV_ROUND_UP(timings->tRRD + 1875, t_ck);
  248. val = max(min_tck->tRRD, val) - 1;
  249. }
  250. tim1 |= val << T_RRD_SHIFT;
  251. val = DIV_ROUND_UP(timings->tRAS_min + timings->tRPab + 1875, t_ck);
  252. tim1 |= (val - 1) << T_RC_SHIFT;
  253. val = DIV_ROUND_UP(timings->tRAS_min + 1875, t_ck);
  254. val = max(min_tck->tRASmin, val) - 1;
  255. tim1 |= val << T_RAS_SHIFT;
  256. val = max(min_tck->tWR, DIV_ROUND_UP(timings->tWR, t_ck)) - 1;
  257. tim1 |= val << T_WR_SHIFT;
  258. val = max(min_tck->tRCD, DIV_ROUND_UP(timings->tRCD + 1875, t_ck));
  259. tim1 |= (val - 1) << T_RCD_SHIFT;
  260. val = max(min_tck->tRPab, DIV_ROUND_UP(timings->tRPab + 1875, t_ck));
  261. tim1 |= (val - 1) << T_RP_SHIFT;
  262. return tim1;
  263. }
  264. static u32 get_sdram_tim_2_shdw(const struct lpddr2_timings *timings,
  265. const struct lpddr2_min_tck *min_tck,
  266. const struct lpddr2_addressing *addressing,
  267. u32 type)
  268. {
  269. u32 tim2 = 0, val = 0;
  270. val = min_tck->tCKE - 1;
  271. tim2 |= val << T_CKE_SHIFT;
  272. val = max(min_tck->tRTP, DIV_ROUND_UP(timings->tRTP, t_ck)) - 1;
  273. tim2 |= val << T_RTP_SHIFT;
  274. /* tXSNR = tRFCab_ps + 10 ns(tRFCab_ps for LPDDR2). */
  275. val = DIV_ROUND_UP(addressing->tRFCab_ps + 10000, t_ck) - 1;
  276. tim2 |= val << T_XSNR_SHIFT;
  277. /* XSRD same as XSNR for LPDDR2 */
  278. tim2 |= val << T_XSRD_SHIFT;
  279. val = max(min_tck->tXP, DIV_ROUND_UP(timings->tXP, t_ck)) - 1;
  280. tim2 |= val << T_XP_SHIFT;
  281. return tim2;
  282. }
  283. static u32 get_sdram_tim_3_shdw(const struct lpddr2_timings *timings,
  284. const struct lpddr2_min_tck *min_tck,
  285. const struct lpddr2_addressing *addressing,
  286. u32 type, u32 ip_rev, u32 derated)
  287. {
  288. u32 tim3 = 0, val = 0, t_dqsck;
  289. val = timings->tRAS_max_ns / addressing->tREFI_ns - 1;
  290. val = val > 0xF ? 0xF : val;
  291. tim3 |= val << T_RAS_MAX_SHIFT;
  292. val = DIV_ROUND_UP(addressing->tRFCab_ps, t_ck) - 1;
  293. tim3 |= val << T_RFC_SHIFT;
  294. t_dqsck = (derated == EMIF_DERATED_TIMINGS) ?
  295. timings->tDQSCK_max_derated : timings->tDQSCK_max;
  296. if (ip_rev == EMIF_4D5)
  297. val = DIV_ROUND_UP(t_dqsck + 1000, t_ck) - 1;
  298. else
  299. val = DIV_ROUND_UP(t_dqsck, t_ck) - 1;
  300. tim3 |= val << T_TDQSCKMAX_SHIFT;
  301. val = DIV_ROUND_UP(timings->tZQCS, t_ck) - 1;
  302. tim3 |= val << ZQ_ZQCS_SHIFT;
  303. val = DIV_ROUND_UP(timings->tCKESR, t_ck);
  304. val = max(min_tck->tCKESR, val) - 1;
  305. tim3 |= val << T_CKESR_SHIFT;
  306. if (ip_rev == EMIF_4D5) {
  307. tim3 |= (EMIF_T_CSTA - 1) << T_CSTA_SHIFT;
  308. val = DIV_ROUND_UP(EMIF_T_PDLL_UL, 128) - 1;
  309. tim3 |= val << T_PDLL_UL_SHIFT;
  310. }
  311. return tim3;
  312. }
  313. static u32 get_read_idle_ctrl_shdw(u8 volt_ramp)
  314. {
  315. u32 idle = 0, val = 0;
  316. /*
  317. * Maximum value in normal conditions and increased frequency
  318. * when voltage is ramping
  319. */
  320. if (volt_ramp)
  321. val = READ_IDLE_INTERVAL_DVFS / t_ck / 64 - 1;
  322. else
  323. val = 0x1FF;
  324. /*
  325. * READ_IDLE_CTRL register in EMIF4D has same offset and fields
  326. * as DLL_CALIB_CTRL in EMIF4D5, so use the same shifts
  327. */
  328. idle |= val << DLL_CALIB_INTERVAL_SHIFT;
  329. idle |= EMIF_READ_IDLE_LEN_VAL << ACK_WAIT_SHIFT;
  330. return idle;
  331. }
  332. static u32 get_dll_calib_ctrl_shdw(u8 volt_ramp)
  333. {
  334. u32 calib = 0, val = 0;
  335. if (volt_ramp == DDR_VOLTAGE_RAMPING)
  336. val = DLL_CALIB_INTERVAL_DVFS / t_ck / 16 - 1;
  337. else
  338. val = 0; /* Disabled when voltage is stable */
  339. calib |= val << DLL_CALIB_INTERVAL_SHIFT;
  340. calib |= DLL_CALIB_ACK_WAIT_VAL << ACK_WAIT_SHIFT;
  341. return calib;
  342. }
  343. static u32 get_ddr_phy_ctrl_1_attilaphy_4d(const struct lpddr2_timings *timings,
  344. u32 freq, u8 RL)
  345. {
  346. u32 phy = EMIF_DDR_PHY_CTRL_1_BASE_VAL_ATTILAPHY, val = 0;
  347. val = RL + DIV_ROUND_UP(timings->tDQSCK_max, t_ck) - 1;
  348. phy |= val << READ_LATENCY_SHIFT_4D;
  349. if (freq <= 100000000)
  350. val = EMIF_DLL_SLAVE_DLY_CTRL_100_MHZ_AND_LESS_ATTILAPHY;
  351. else if (freq <= 200000000)
  352. val = EMIF_DLL_SLAVE_DLY_CTRL_200_MHZ_ATTILAPHY;
  353. else
  354. val = EMIF_DLL_SLAVE_DLY_CTRL_400_MHZ_ATTILAPHY;
  355. phy |= val << DLL_SLAVE_DLY_CTRL_SHIFT_4D;
  356. return phy;
  357. }
  358. static u32 get_phy_ctrl_1_intelliphy_4d5(u32 freq, u8 cl)
  359. {
  360. u32 phy = EMIF_DDR_PHY_CTRL_1_BASE_VAL_INTELLIPHY, half_delay;
  361. /*
  362. * DLL operates at 266 MHz. If DDR frequency is near 266 MHz,
  363. * half-delay is not needed else set half-delay
  364. */
  365. if (freq >= 265000000 && freq < 267000000)
  366. half_delay = 0;
  367. else
  368. half_delay = 1;
  369. phy |= half_delay << DLL_HALF_DELAY_SHIFT_4D5;
  370. phy |= ((cl + DIV_ROUND_UP(EMIF_PHY_TOTAL_READ_LATENCY_INTELLIPHY_PS,
  371. t_ck) - 1) << READ_LATENCY_SHIFT_4D5);
  372. return phy;
  373. }
  374. static u32 get_ext_phy_ctrl_2_intelliphy_4d5(void)
  375. {
  376. u32 fifo_we_slave_ratio;
  377. fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
  378. EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
  379. return fifo_we_slave_ratio | fifo_we_slave_ratio << 11 |
  380. fifo_we_slave_ratio << 22;
  381. }
  382. static u32 get_ext_phy_ctrl_3_intelliphy_4d5(void)
  383. {
  384. u32 fifo_we_slave_ratio;
  385. fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
  386. EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
  387. return fifo_we_slave_ratio >> 10 | fifo_we_slave_ratio << 1 |
  388. fifo_we_slave_ratio << 12 | fifo_we_slave_ratio << 23;
  389. }
  390. static u32 get_ext_phy_ctrl_4_intelliphy_4d5(void)
  391. {
  392. u32 fifo_we_slave_ratio;
  393. fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
  394. EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
  395. return fifo_we_slave_ratio >> 9 | fifo_we_slave_ratio << 2 |
  396. fifo_we_slave_ratio << 13;
  397. }
  398. static u32 get_pwr_mgmt_ctrl(u32 freq, struct emif_data *emif, u32 ip_rev)
  399. {
  400. u32 pwr_mgmt_ctrl = 0, timeout;
  401. u32 lpmode = EMIF_LP_MODE_SELF_REFRESH;
  402. u32 timeout_perf = EMIF_LP_MODE_TIMEOUT_PERFORMANCE;
  403. u32 timeout_pwr = EMIF_LP_MODE_TIMEOUT_POWER;
  404. u32 freq_threshold = EMIF_LP_MODE_FREQ_THRESHOLD;
  405. struct emif_custom_configs *cust_cfgs = emif->plat_data->custom_configs;
  406. if (cust_cfgs && (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE)) {
  407. lpmode = cust_cfgs->lpmode;
  408. timeout_perf = cust_cfgs->lpmode_timeout_performance;
  409. timeout_pwr = cust_cfgs->lpmode_timeout_power;
  410. freq_threshold = cust_cfgs->lpmode_freq_threshold;
  411. }
  412. /* Timeout based on DDR frequency */
  413. timeout = freq >= freq_threshold ? timeout_perf : timeout_pwr;
  414. /* The value to be set in register is "log2(timeout) - 3" */
  415. if (timeout < 16) {
  416. timeout = 0;
  417. } else {
  418. timeout = __fls(timeout) - 3;
  419. if (timeout & (timeout - 1))
  420. timeout++;
  421. }
  422. switch (lpmode) {
  423. case EMIF_LP_MODE_CLOCK_STOP:
  424. pwr_mgmt_ctrl = (timeout << CS_TIM_SHIFT) |
  425. SR_TIM_MASK | PD_TIM_MASK;
  426. break;
  427. case EMIF_LP_MODE_SELF_REFRESH:
  428. /* Workaround for errata i735 */
  429. if (timeout < 6)
  430. timeout = 6;
  431. pwr_mgmt_ctrl = (timeout << SR_TIM_SHIFT) |
  432. CS_TIM_MASK | PD_TIM_MASK;
  433. break;
  434. case EMIF_LP_MODE_PWR_DN:
  435. pwr_mgmt_ctrl = (timeout << PD_TIM_SHIFT) |
  436. CS_TIM_MASK | SR_TIM_MASK;
  437. break;
  438. case EMIF_LP_MODE_DISABLE:
  439. default:
  440. pwr_mgmt_ctrl = CS_TIM_MASK |
  441. PD_TIM_MASK | SR_TIM_MASK;
  442. }
  443. /* No CS_TIM in EMIF_4D5 */
  444. if (ip_rev == EMIF_4D5)
  445. pwr_mgmt_ctrl &= ~CS_TIM_MASK;
  446. pwr_mgmt_ctrl |= lpmode << LP_MODE_SHIFT;
  447. return pwr_mgmt_ctrl;
  448. }
  449. /*
  450. * Get the temperature level of the EMIF instance:
  451. * Reads the MR4 register of attached SDRAM parts to find out the temperature
  452. * level. If there are two parts attached(one on each CS), then the temperature
  453. * level for the EMIF instance is the higher of the two temperatures.
  454. */
  455. static void get_temperature_level(struct emif_data *emif)
  456. {
  457. u32 temp, temperature_level;
  458. void __iomem *base;
  459. base = emif->base;
  460. /* Read mode register 4 */
  461. writel(DDR_MR4, base + EMIF_LPDDR2_MODE_REG_CONFIG);
  462. temperature_level = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
  463. temperature_level = (temperature_level & MR4_SDRAM_REF_RATE_MASK) >>
  464. MR4_SDRAM_REF_RATE_SHIFT;
  465. if (emif->plat_data->device_info->cs1_used) {
  466. writel(DDR_MR4 | CS_MASK, base + EMIF_LPDDR2_MODE_REG_CONFIG);
  467. temp = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
  468. temp = (temp & MR4_SDRAM_REF_RATE_MASK)
  469. >> MR4_SDRAM_REF_RATE_SHIFT;
  470. temperature_level = max(temp, temperature_level);
  471. }
  472. /* treat everything less than nominal(3) in MR4 as nominal */
  473. if (unlikely(temperature_level < SDRAM_TEMP_NOMINAL))
  474. temperature_level = SDRAM_TEMP_NOMINAL;
  475. /* if we get reserved value in MR4 persist with the existing value */
  476. if (likely(temperature_level != SDRAM_TEMP_RESERVED_4))
  477. emif->temperature_level = temperature_level;
  478. }
  479. /*
  480. * Program EMIF shadow registers that are not dependent on temperature
  481. * or voltage
  482. */
  483. static void setup_registers(struct emif_data *emif, struct emif_regs *regs)
  484. {
  485. void __iomem *base = emif->base;
  486. writel(regs->sdram_tim2_shdw, base + EMIF_SDRAM_TIMING_2_SHDW);
  487. writel(regs->phy_ctrl_1_shdw, base + EMIF_DDR_PHY_CTRL_1_SHDW);
  488. /* Settings specific for EMIF4D5 */
  489. if (emif->plat_data->ip_rev != EMIF_4D5)
  490. return;
  491. writel(regs->ext_phy_ctrl_2_shdw, base + EMIF_EXT_PHY_CTRL_2_SHDW);
  492. writel(regs->ext_phy_ctrl_3_shdw, base + EMIF_EXT_PHY_CTRL_3_SHDW);
  493. writel(regs->ext_phy_ctrl_4_shdw, base + EMIF_EXT_PHY_CTRL_4_SHDW);
  494. }
  495. /*
  496. * When voltage ramps dll calibration and forced read idle should
  497. * happen more often
  498. */
  499. static void setup_volt_sensitive_regs(struct emif_data *emif,
  500. struct emif_regs *regs, u32 volt_state)
  501. {
  502. u32 calib_ctrl;
  503. void __iomem *base = emif->base;
  504. /*
  505. * EMIF_READ_IDLE_CTRL in EMIF4D refers to the same register as
  506. * EMIF_DLL_CALIB_CTRL in EMIF4D5 and dll_calib_ctrl_shadow_*
  507. * is an alias of the respective read_idle_ctrl_shdw_* (members of
  508. * a union). So, the below code takes care of both cases
  509. */
  510. if (volt_state == DDR_VOLTAGE_RAMPING)
  511. calib_ctrl = regs->dll_calib_ctrl_shdw_volt_ramp;
  512. else
  513. calib_ctrl = regs->dll_calib_ctrl_shdw_normal;
  514. writel(calib_ctrl, base + EMIF_DLL_CALIB_CTRL_SHDW);
  515. }
  516. /*
  517. * setup_temperature_sensitive_regs() - set the timings for temperature
  518. * sensitive registers. This happens once at initialisation time based
  519. * on the temperature at boot time and subsequently based on the temperature
  520. * alert interrupt. Temperature alert can happen when the temperature
  521. * increases or drops. So this function can have the effect of either
  522. * derating the timings or going back to nominal values.
  523. */
  524. static void setup_temperature_sensitive_regs(struct emif_data *emif,
  525. struct emif_regs *regs)
  526. {
  527. u32 tim1, tim3, ref_ctrl, type;
  528. void __iomem *base = emif->base;
  529. u32 temperature;
  530. type = emif->plat_data->device_info->type;
  531. tim1 = regs->sdram_tim1_shdw;
  532. tim3 = regs->sdram_tim3_shdw;
  533. ref_ctrl = regs->ref_ctrl_shdw;
  534. /* No de-rating for non-lpddr2 devices */
  535. if (type != DDR_TYPE_LPDDR2_S2 && type != DDR_TYPE_LPDDR2_S4)
  536. goto out;
  537. temperature = emif->temperature_level;
  538. if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH) {
  539. ref_ctrl = regs->ref_ctrl_shdw_derated;
  540. } else if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH_AND_TIMINGS) {
  541. tim1 = regs->sdram_tim1_shdw_derated;
  542. tim3 = regs->sdram_tim3_shdw_derated;
  543. ref_ctrl = regs->ref_ctrl_shdw_derated;
  544. }
  545. out:
  546. writel(tim1, base + EMIF_SDRAM_TIMING_1_SHDW);
  547. writel(tim3, base + EMIF_SDRAM_TIMING_3_SHDW);
  548. writel(ref_ctrl, base + EMIF_SDRAM_REFRESH_CTRL_SHDW);
  549. }
  550. static irqreturn_t handle_temp_alert(void __iomem *base, struct emif_data *emif)
  551. {
  552. u32 old_temp_level;
  553. irqreturn_t ret = IRQ_HANDLED;
  554. spin_lock_irqsave(&emif_lock, irq_state);
  555. old_temp_level = emif->temperature_level;
  556. get_temperature_level(emif);
  557. if (unlikely(emif->temperature_level == old_temp_level)) {
  558. goto out;
  559. } else if (!emif->curr_regs) {
  560. dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
  561. goto out;
  562. }
  563. if (emif->temperature_level < old_temp_level ||
  564. emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
  565. /*
  566. * Temperature coming down - defer handling to thread OR
  567. * Temperature far too high - do kernel_power_off() from
  568. * thread context
  569. */
  570. ret = IRQ_WAKE_THREAD;
  571. } else {
  572. /* Temperature is going up - handle immediately */
  573. setup_temperature_sensitive_regs(emif, emif->curr_regs);
  574. do_freq_update();
  575. }
  576. out:
  577. spin_unlock_irqrestore(&emif_lock, irq_state);
  578. return ret;
  579. }
  580. static irqreturn_t emif_interrupt_handler(int irq, void *dev_id)
  581. {
  582. u32 interrupts;
  583. struct emif_data *emif = dev_id;
  584. void __iomem *base = emif->base;
  585. struct device *dev = emif->dev;
  586. irqreturn_t ret = IRQ_HANDLED;
  587. /* Save the status and clear it */
  588. interrupts = readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  589. writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  590. /*
  591. * Handle temperature alert
  592. * Temperature alert should be same for all ports
  593. * So, it's enough to process it only for one of the ports
  594. */
  595. if (interrupts & TA_SYS_MASK)
  596. ret = handle_temp_alert(base, emif);
  597. if (interrupts & ERR_SYS_MASK)
  598. dev_err(dev, "Access error from SYS port - %x\n", interrupts);
  599. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
  600. /* Save the status and clear it */
  601. interrupts = readl(base + EMIF_LL_OCP_INTERRUPT_STATUS);
  602. writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_STATUS);
  603. if (interrupts & ERR_LL_MASK)
  604. dev_err(dev, "Access error from LL port - %x\n",
  605. interrupts);
  606. }
  607. return ret;
  608. }
  609. static irqreturn_t emif_threaded_isr(int irq, void *dev_id)
  610. {
  611. struct emif_data *emif = dev_id;
  612. if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
  613. dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
  614. kernel_power_off();
  615. return IRQ_HANDLED;
  616. }
  617. spin_lock_irqsave(&emif_lock, irq_state);
  618. if (emif->curr_regs) {
  619. setup_temperature_sensitive_regs(emif, emif->curr_regs);
  620. do_freq_update();
  621. } else {
  622. dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
  623. }
  624. spin_unlock_irqrestore(&emif_lock, irq_state);
  625. return IRQ_HANDLED;
  626. }
  627. static void clear_all_interrupts(struct emif_data *emif)
  628. {
  629. void __iomem *base = emif->base;
  630. writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS),
  631. base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  632. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
  633. writel(readl(base + EMIF_LL_OCP_INTERRUPT_STATUS),
  634. base + EMIF_LL_OCP_INTERRUPT_STATUS);
  635. }
  636. static void disable_and_clear_all_interrupts(struct emif_data *emif)
  637. {
  638. void __iomem *base = emif->base;
  639. /* Disable all interrupts */
  640. writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET),
  641. base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_CLEAR);
  642. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
  643. writel(readl(base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET),
  644. base + EMIF_LL_OCP_INTERRUPT_ENABLE_CLEAR);
  645. /* Clear all interrupts */
  646. clear_all_interrupts(emif);
  647. }
  648. static int __init_or_module setup_interrupts(struct emif_data *emif, u32 irq)
  649. {
  650. u32 interrupts, type;
  651. void __iomem *base = emif->base;
  652. type = emif->plat_data->device_info->type;
  653. clear_all_interrupts(emif);
  654. /* Enable interrupts for SYS interface */
  655. interrupts = EN_ERR_SYS_MASK;
  656. if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4)
  657. interrupts |= EN_TA_SYS_MASK;
  658. writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET);
  659. /* Enable interrupts for LL interface */
  660. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
  661. /* TA need not be enabled for LL */
  662. interrupts = EN_ERR_LL_MASK;
  663. writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET);
  664. }
  665. /* setup IRQ handlers */
  666. return devm_request_threaded_irq(emif->dev, irq,
  667. emif_interrupt_handler,
  668. emif_threaded_isr,
  669. 0, dev_name(emif->dev),
  670. emif);
  671. }
  672. static void get_default_timings(struct emif_data *emif)
  673. {
  674. struct emif_platform_data *pd = emif->plat_data;
  675. pd->timings = lpddr2_jedec_timings;
  676. pd->timings_arr_size = ARRAY_SIZE(lpddr2_jedec_timings);
  677. dev_warn(emif->dev, "%s: using default timings\n", __func__);
  678. }
  679. static int is_dev_data_valid(u32 type, u32 density, u32 io_width, u32 phy_type,
  680. u32 ip_rev, struct device *dev)
  681. {
  682. int valid;
  683. valid = (type == DDR_TYPE_LPDDR2_S4 ||
  684. type == DDR_TYPE_LPDDR2_S2)
  685. && (density >= DDR_DENSITY_64Mb
  686. && density <= DDR_DENSITY_8Gb)
  687. && (io_width >= DDR_IO_WIDTH_8
  688. && io_width <= DDR_IO_WIDTH_32);
  689. /* Combinations of EMIF and PHY revisions that we support today */
  690. switch (ip_rev) {
  691. case EMIF_4D:
  692. valid = valid && (phy_type == EMIF_PHY_TYPE_ATTILAPHY);
  693. break;
  694. case EMIF_4D5:
  695. valid = valid && (phy_type == EMIF_PHY_TYPE_INTELLIPHY);
  696. break;
  697. default:
  698. valid = 0;
  699. }
  700. if (!valid)
  701. dev_err(dev, "%s: invalid DDR details\n", __func__);
  702. return valid;
  703. }
  704. static int is_custom_config_valid(struct emif_custom_configs *cust_cfgs,
  705. struct device *dev)
  706. {
  707. int valid = 1;
  708. if ((cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE) &&
  709. (cust_cfgs->lpmode != EMIF_LP_MODE_DISABLE))
  710. valid = cust_cfgs->lpmode_freq_threshold &&
  711. cust_cfgs->lpmode_timeout_performance &&
  712. cust_cfgs->lpmode_timeout_power;
  713. if (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL)
  714. valid = valid && cust_cfgs->temp_alert_poll_interval_ms;
  715. if (!valid)
  716. dev_warn(dev, "%s: invalid custom configs\n", __func__);
  717. return valid;
  718. }
  719. static struct emif_data *__init_or_module get_device_details(
  720. struct platform_device *pdev)
  721. {
  722. u32 size;
  723. struct emif_data *emif = NULL;
  724. struct ddr_device_info *dev_info;
  725. struct emif_custom_configs *cust_cfgs;
  726. struct emif_platform_data *pd;
  727. struct device *dev;
  728. void *temp;
  729. pd = pdev->dev.platform_data;
  730. dev = &pdev->dev;
  731. if (!(pd && pd->device_info && is_dev_data_valid(pd->device_info->type,
  732. pd->device_info->density, pd->device_info->io_width,
  733. pd->phy_type, pd->ip_rev, dev))) {
  734. dev_err(dev, "%s: invalid device data\n", __func__);
  735. goto error;
  736. }
  737. emif = devm_kzalloc(dev, sizeof(*emif), GFP_KERNEL);
  738. temp = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  739. dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
  740. if (!emif || !pd || !dev_info) {
  741. dev_err(dev, "%s:%d: allocation error\n", __func__, __LINE__);
  742. goto error;
  743. }
  744. memcpy(temp, pd, sizeof(*pd));
  745. pd = temp;
  746. memcpy(dev_info, pd->device_info, sizeof(*dev_info));
  747. pd->device_info = dev_info;
  748. emif->plat_data = pd;
  749. emif->dev = dev;
  750. emif->temperature_level = SDRAM_TEMP_NOMINAL;
  751. /*
  752. * For EMIF instances other than EMIF1 see if the devices connected
  753. * are exactly same as on EMIF1(which is typically the case). If so,
  754. * mark it as a duplicate of EMIF1 and skip copying timings data.
  755. * This will save some memory and some computation later.
  756. */
  757. emif->duplicate = emif1 && (memcmp(dev_info,
  758. emif1->plat_data->device_info,
  759. sizeof(struct ddr_device_info)) == 0);
  760. if (emif->duplicate) {
  761. pd->timings = NULL;
  762. pd->min_tck = NULL;
  763. goto out;
  764. } else if (emif1) {
  765. dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
  766. __func__);
  767. }
  768. /*
  769. * Copy custom configs - ignore allocation error, if any, as
  770. * custom_configs is not very critical
  771. */
  772. cust_cfgs = pd->custom_configs;
  773. if (cust_cfgs && is_custom_config_valid(cust_cfgs, dev)) {
  774. temp = devm_kzalloc(dev, sizeof(*cust_cfgs), GFP_KERNEL);
  775. if (temp)
  776. memcpy(temp, cust_cfgs, sizeof(*cust_cfgs));
  777. else
  778. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  779. __LINE__);
  780. pd->custom_configs = temp;
  781. }
  782. /*
  783. * Copy timings and min-tck values from platform data. If it is not
  784. * available or if memory allocation fails, use JEDEC defaults
  785. */
  786. size = sizeof(struct lpddr2_timings) * pd->timings_arr_size;
  787. if (pd->timings) {
  788. temp = devm_kzalloc(dev, size, GFP_KERNEL);
  789. if (temp) {
  790. memcpy(temp, pd->timings, sizeof(*pd->timings));
  791. pd->timings = temp;
  792. } else {
  793. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  794. __LINE__);
  795. get_default_timings(emif);
  796. }
  797. } else {
  798. get_default_timings(emif);
  799. }
  800. if (pd->min_tck) {
  801. temp = devm_kzalloc(dev, sizeof(*pd->min_tck), GFP_KERNEL);
  802. if (temp) {
  803. memcpy(temp, pd->min_tck, sizeof(*pd->min_tck));
  804. pd->min_tck = temp;
  805. } else {
  806. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  807. __LINE__);
  808. pd->min_tck = &lpddr2_jedec_min_tck;
  809. }
  810. } else {
  811. pd->min_tck = &lpddr2_jedec_min_tck;
  812. }
  813. out:
  814. return emif;
  815. error:
  816. return NULL;
  817. }
  818. static int __init_or_module emif_probe(struct platform_device *pdev)
  819. {
  820. struct emif_data *emif;
  821. struct resource *res;
  822. int irq;
  823. emif = get_device_details(pdev);
  824. if (!emif) {
  825. pr_err("%s: error getting device data\n", __func__);
  826. goto error;
  827. }
  828. list_add(&emif->node, &device_list);
  829. emif->addressing = get_addressing_table(emif->plat_data->device_info);
  830. /* Save pointers to each other in emif and device structures */
  831. emif->dev = &pdev->dev;
  832. platform_set_drvdata(pdev, emif);
  833. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  834. if (!res) {
  835. dev_err(emif->dev, "%s: error getting memory resource\n",
  836. __func__);
  837. goto error;
  838. }
  839. emif->base = devm_request_and_ioremap(emif->dev, res);
  840. if (!emif->base) {
  841. dev_err(emif->dev, "%s: devm_request_and_ioremap() failed\n",
  842. __func__);
  843. goto error;
  844. }
  845. irq = platform_get_irq(pdev, 0);
  846. if (irq < 0) {
  847. dev_err(emif->dev, "%s: error getting IRQ resource - %d\n",
  848. __func__, irq);
  849. goto error;
  850. }
  851. disable_and_clear_all_interrupts(emif);
  852. setup_interrupts(emif, irq);
  853. /* One-time actions taken on probing the first device */
  854. if (!emif1) {
  855. emif1 = emif;
  856. spin_lock_init(&emif_lock);
  857. /*
  858. * TODO: register notifiers for frequency and voltage
  859. * change here once the respective frameworks are
  860. * available
  861. */
  862. }
  863. dev_info(&pdev->dev, "%s: device configured with addr = %p and IRQ%d\n",
  864. __func__, emif->base, irq);
  865. return 0;
  866. error:
  867. return -ENODEV;
  868. }
  869. static void emif_shutdown(struct platform_device *pdev)
  870. {
  871. struct emif_data *emif = platform_get_drvdata(pdev);
  872. disable_and_clear_all_interrupts(emif);
  873. }
  874. static int get_emif_reg_values(struct emif_data *emif, u32 freq,
  875. struct emif_regs *regs)
  876. {
  877. u32 cs1_used, ip_rev, phy_type;
  878. u32 cl, type;
  879. const struct lpddr2_timings *timings;
  880. const struct lpddr2_min_tck *min_tck;
  881. const struct ddr_device_info *device_info;
  882. const struct lpddr2_addressing *addressing;
  883. struct emif_data *emif_for_calc;
  884. struct device *dev;
  885. const struct emif_custom_configs *custom_configs;
  886. dev = emif->dev;
  887. /*
  888. * If the devices on this EMIF instance is duplicate of EMIF1,
  889. * use EMIF1 details for the calculation
  890. */
  891. emif_for_calc = emif->duplicate ? emif1 : emif;
  892. timings = get_timings_table(emif_for_calc, freq);
  893. addressing = emif_for_calc->addressing;
  894. if (!timings || !addressing) {
  895. dev_err(dev, "%s: not enough data available for %dHz",
  896. __func__, freq);
  897. return -1;
  898. }
  899. device_info = emif_for_calc->plat_data->device_info;
  900. type = device_info->type;
  901. cs1_used = device_info->cs1_used;
  902. ip_rev = emif_for_calc->plat_data->ip_rev;
  903. phy_type = emif_for_calc->plat_data->phy_type;
  904. min_tck = emif_for_calc->plat_data->min_tck;
  905. custom_configs = emif_for_calc->plat_data->custom_configs;
  906. set_ddr_clk_period(freq);
  907. regs->ref_ctrl_shdw = get_sdram_ref_ctrl_shdw(freq, addressing);
  908. regs->sdram_tim1_shdw = get_sdram_tim_1_shdw(timings, min_tck,
  909. addressing);
  910. regs->sdram_tim2_shdw = get_sdram_tim_2_shdw(timings, min_tck,
  911. addressing, type);
  912. regs->sdram_tim3_shdw = get_sdram_tim_3_shdw(timings, min_tck,
  913. addressing, type, ip_rev, EMIF_NORMAL_TIMINGS);
  914. cl = get_cl(emif);
  915. if (phy_type == EMIF_PHY_TYPE_ATTILAPHY && ip_rev == EMIF_4D) {
  916. regs->phy_ctrl_1_shdw = get_ddr_phy_ctrl_1_attilaphy_4d(
  917. timings, freq, cl);
  918. } else if (phy_type == EMIF_PHY_TYPE_INTELLIPHY && ip_rev == EMIF_4D5) {
  919. regs->phy_ctrl_1_shdw = get_phy_ctrl_1_intelliphy_4d5(freq, cl);
  920. regs->ext_phy_ctrl_2_shdw = get_ext_phy_ctrl_2_intelliphy_4d5();
  921. regs->ext_phy_ctrl_3_shdw = get_ext_phy_ctrl_3_intelliphy_4d5();
  922. regs->ext_phy_ctrl_4_shdw = get_ext_phy_ctrl_4_intelliphy_4d5();
  923. } else {
  924. return -1;
  925. }
  926. /* Only timeout values in pwr_mgmt_ctrl_shdw register */
  927. regs->pwr_mgmt_ctrl_shdw =
  928. get_pwr_mgmt_ctrl(freq, emif_for_calc, ip_rev) &
  929. (CS_TIM_MASK | SR_TIM_MASK | PD_TIM_MASK);
  930. if (ip_rev & EMIF_4D) {
  931. regs->read_idle_ctrl_shdw_normal =
  932. get_read_idle_ctrl_shdw(DDR_VOLTAGE_STABLE);
  933. regs->read_idle_ctrl_shdw_volt_ramp =
  934. get_read_idle_ctrl_shdw(DDR_VOLTAGE_RAMPING);
  935. } else if (ip_rev & EMIF_4D5) {
  936. regs->dll_calib_ctrl_shdw_normal =
  937. get_dll_calib_ctrl_shdw(DDR_VOLTAGE_STABLE);
  938. regs->dll_calib_ctrl_shdw_volt_ramp =
  939. get_dll_calib_ctrl_shdw(DDR_VOLTAGE_RAMPING);
  940. }
  941. if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4) {
  942. regs->ref_ctrl_shdw_derated = get_sdram_ref_ctrl_shdw(freq / 4,
  943. addressing);
  944. regs->sdram_tim1_shdw_derated =
  945. get_sdram_tim_1_shdw_derated(timings, min_tck,
  946. addressing);
  947. regs->sdram_tim3_shdw_derated = get_sdram_tim_3_shdw(timings,
  948. min_tck, addressing, type, ip_rev,
  949. EMIF_DERATED_TIMINGS);
  950. }
  951. regs->freq = freq;
  952. return 0;
  953. }
  954. /*
  955. * get_regs() - gets the cached emif_regs structure for a given EMIF instance
  956. * given frequency(freq):
  957. *
  958. * As an optimisation, every EMIF instance other than EMIF1 shares the
  959. * register cache with EMIF1 if the devices connected on this instance
  960. * are same as that on EMIF1(indicated by the duplicate flag)
  961. *
  962. * If we do not have an entry corresponding to the frequency given, we
  963. * allocate a new entry and calculate the values
  964. *
  965. * Upon finding the right reg dump, save it in curr_regs. It can be
  966. * directly used for thermal de-rating and voltage ramping changes.
  967. */
  968. static struct emif_regs *get_regs(struct emif_data *emif, u32 freq)
  969. {
  970. int i;
  971. struct emif_regs **regs_cache;
  972. struct emif_regs *regs = NULL;
  973. struct device *dev;
  974. dev = emif->dev;
  975. if (emif->curr_regs && emif->curr_regs->freq == freq) {
  976. dev_dbg(dev, "%s: using curr_regs - %u Hz", __func__, freq);
  977. return emif->curr_regs;
  978. }
  979. if (emif->duplicate)
  980. regs_cache = emif1->regs_cache;
  981. else
  982. regs_cache = emif->regs_cache;
  983. for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) {
  984. if (regs_cache[i]->freq == freq) {
  985. regs = regs_cache[i];
  986. dev_dbg(dev,
  987. "%s: reg dump found in reg cache for %u Hz\n",
  988. __func__, freq);
  989. break;
  990. }
  991. }
  992. /*
  993. * If we don't have an entry for this frequency in the cache create one
  994. * and calculate the values
  995. */
  996. if (!regs) {
  997. regs = devm_kzalloc(emif->dev, sizeof(*regs), GFP_ATOMIC);
  998. if (!regs)
  999. return NULL;
  1000. if (get_emif_reg_values(emif, freq, regs)) {
  1001. devm_kfree(emif->dev, regs);
  1002. return NULL;
  1003. }
  1004. /*
  1005. * Now look for an un-used entry in the cache and save the
  1006. * newly created struct. If there are no free entries
  1007. * over-write the last entry
  1008. */
  1009. for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++)
  1010. ;
  1011. if (i >= EMIF_MAX_NUM_FREQUENCIES) {
  1012. dev_warn(dev, "%s: regs_cache full - reusing a slot!!\n",
  1013. __func__);
  1014. i = EMIF_MAX_NUM_FREQUENCIES - 1;
  1015. devm_kfree(emif->dev, regs_cache[i]);
  1016. }
  1017. regs_cache[i] = regs;
  1018. }
  1019. return regs;
  1020. }
  1021. static void do_volt_notify_handling(struct emif_data *emif, u32 volt_state)
  1022. {
  1023. dev_dbg(emif->dev, "%s: voltage notification : %d", __func__,
  1024. volt_state);
  1025. if (!emif->curr_regs) {
  1026. dev_err(emif->dev,
  1027. "%s: volt-notify before registers are ready: %d\n",
  1028. __func__, volt_state);
  1029. return;
  1030. }
  1031. setup_volt_sensitive_regs(emif, emif->curr_regs, volt_state);
  1032. }
  1033. /*
  1034. * TODO: voltage notify handling should be hooked up to
  1035. * regulator framework as soon as the necessary support
  1036. * is available in mainline kernel. This function is un-used
  1037. * right now.
  1038. */
  1039. static void __attribute__((unused)) volt_notify_handling(u32 volt_state)
  1040. {
  1041. struct emif_data *emif;
  1042. spin_lock_irqsave(&emif_lock, irq_state);
  1043. list_for_each_entry(emif, &device_list, node)
  1044. do_volt_notify_handling(emif, volt_state);
  1045. do_freq_update();
  1046. spin_unlock_irqrestore(&emif_lock, irq_state);
  1047. }
  1048. static void do_freq_pre_notify_handling(struct emif_data *emif, u32 new_freq)
  1049. {
  1050. struct emif_regs *regs;
  1051. regs = get_regs(emif, new_freq);
  1052. if (!regs)
  1053. return;
  1054. emif->curr_regs = regs;
  1055. /*
  1056. * Update the shadow registers:
  1057. * Temperature and voltage-ramp sensitive settings are also configured
  1058. * in terms of DDR cycles. So, we need to update them too when there
  1059. * is a freq change
  1060. */
  1061. dev_dbg(emif->dev, "%s: setting up shadow registers for %uHz",
  1062. __func__, new_freq);
  1063. setup_registers(emif, regs);
  1064. setup_temperature_sensitive_regs(emif, regs);
  1065. setup_volt_sensitive_regs(emif, regs, DDR_VOLTAGE_STABLE);
  1066. /*
  1067. * Part of workaround for errata i728. See do_freq_update()
  1068. * for more details
  1069. */
  1070. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  1071. set_lpmode(emif, EMIF_LP_MODE_DISABLE);
  1072. }
  1073. /*
  1074. * TODO: frequency notify handling should be hooked up to
  1075. * clock framework as soon as the necessary support is
  1076. * available in mainline kernel. This function is un-used
  1077. * right now.
  1078. */
  1079. static void __attribute__((unused)) freq_pre_notify_handling(u32 new_freq)
  1080. {
  1081. struct emif_data *emif;
  1082. /*
  1083. * NOTE: we are taking the spin-lock here and releases it
  1084. * only in post-notifier. This doesn't look good and
  1085. * Sparse complains about it, but this seems to be
  1086. * un-avoidable. We need to lock a sequence of events
  1087. * that is split between EMIF and clock framework.
  1088. *
  1089. * 1. EMIF driver updates EMIF timings in shadow registers in the
  1090. * frequency pre-notify callback from clock framework
  1091. * 2. clock framework sets up the registers for the new frequency
  1092. * 3. clock framework initiates a hw-sequence that updates
  1093. * the frequency EMIF timings synchronously.
  1094. *
  1095. * All these 3 steps should be performed as an atomic operation
  1096. * vis-a-vis similar sequence in the EMIF interrupt handler
  1097. * for temperature events. Otherwise, there could be race
  1098. * conditions that could result in incorrect EMIF timings for
  1099. * a given frequency
  1100. */
  1101. spin_lock_irqsave(&emif_lock, irq_state);
  1102. list_for_each_entry(emif, &device_list, node)
  1103. do_freq_pre_notify_handling(emif, new_freq);
  1104. }
  1105. static void do_freq_post_notify_handling(struct emif_data *emif)
  1106. {
  1107. /*
  1108. * Part of workaround for errata i728. See do_freq_update()
  1109. * for more details
  1110. */
  1111. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  1112. set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH);
  1113. }
  1114. /*
  1115. * TODO: frequency notify handling should be hooked up to
  1116. * clock framework as soon as the necessary support is
  1117. * available in mainline kernel. This function is un-used
  1118. * right now.
  1119. */
  1120. static void __attribute__((unused)) freq_post_notify_handling(void)
  1121. {
  1122. struct emif_data *emif;
  1123. list_for_each_entry(emif, &device_list, node)
  1124. do_freq_post_notify_handling(emif);
  1125. /*
  1126. * Lock is done in pre-notify handler. See freq_pre_notify_handling()
  1127. * for more details
  1128. */
  1129. spin_unlock_irqrestore(&emif_lock, irq_state);
  1130. }
  1131. static struct platform_driver emif_driver = {
  1132. .shutdown = emif_shutdown,
  1133. .driver = {
  1134. .name = "emif",
  1135. },
  1136. };
  1137. static int __init_or_module emif_register(void)
  1138. {
  1139. return platform_driver_probe(&emif_driver, emif_probe);
  1140. }
  1141. static void __exit emif_unregister(void)
  1142. {
  1143. platform_driver_unregister(&emif_driver);
  1144. }
  1145. module_init(emif_register);
  1146. module_exit(emif_unregister);
  1147. MODULE_DESCRIPTION("TI EMIF SDRAM Controller Driver");
  1148. MODULE_LICENSE("GPL");
  1149. MODULE_ALIAS("platform:emif");
  1150. MODULE_AUTHOR("Texas Instruments Inc");