emif.c 43 KB

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