emif.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  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/err.h>
  14. #include <linux/kernel.h>
  15. #include <linux/reboot.h>
  16. #include <linux/platform_data/emif_plat.h>
  17. #include <linux/io.h>
  18. #include <linux/device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/slab.h>
  22. #include <linux/of.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/module.h>
  26. #include <linux/list.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/pm.h>
  29. #include <memory/jedec_ddr.h>
  30. #include "emif.h"
  31. #include "of_memory.h"
  32. /**
  33. * struct emif_data - Per device static data for driver's use
  34. * @duplicate: Whether the DDR devices attached to this EMIF
  35. * instance are exactly same as that on EMIF1. In
  36. * this case we can save some memory and processing
  37. * @temperature_level: Maximum temperature of LPDDR2 devices attached
  38. * to this EMIF - read from MR4 register. If there
  39. * are two devices attached to this EMIF, this
  40. * value is the maximum of the two temperature
  41. * levels.
  42. * @node: node in the device list
  43. * @base: base address of memory-mapped IO registers.
  44. * @dev: device pointer.
  45. * @addressing table with addressing information from the spec
  46. * @regs_cache: An array of 'struct emif_regs' that stores
  47. * calculated register values for different
  48. * frequencies, to avoid re-calculating them on
  49. * each DVFS transition.
  50. * @curr_regs: The set of register values used in the last
  51. * frequency change (i.e. corresponding to the
  52. * frequency in effect at the moment)
  53. * @plat_data: Pointer to saved platform data.
  54. * @debugfs_root: dentry to the root folder for EMIF in debugfs
  55. * @np_ddr: Pointer to ddr device tree node
  56. */
  57. struct emif_data {
  58. u8 duplicate;
  59. u8 temperature_level;
  60. u8 lpmode;
  61. struct list_head node;
  62. unsigned long irq_state;
  63. void __iomem *base;
  64. struct device *dev;
  65. const struct lpddr2_addressing *addressing;
  66. struct emif_regs *regs_cache[EMIF_MAX_NUM_FREQUENCIES];
  67. struct emif_regs *curr_regs;
  68. struct emif_platform_data *plat_data;
  69. struct dentry *debugfs_root;
  70. struct device_node *np_ddr;
  71. };
  72. static struct emif_data *emif1;
  73. static spinlock_t emif_lock;
  74. static unsigned long irq_state;
  75. static u32 t_ck; /* DDR clock period in ps */
  76. static LIST_HEAD(device_list);
  77. #ifdef CONFIG_DEBUG_FS
  78. static void do_emif_regdump_show(struct seq_file *s, struct emif_data *emif,
  79. struct emif_regs *regs)
  80. {
  81. u32 type = emif->plat_data->device_info->type;
  82. u32 ip_rev = emif->plat_data->ip_rev;
  83. seq_printf(s, "EMIF register cache dump for %dMHz\n",
  84. regs->freq/1000000);
  85. seq_printf(s, "ref_ctrl_shdw\t: 0x%08x\n", regs->ref_ctrl_shdw);
  86. seq_printf(s, "sdram_tim1_shdw\t: 0x%08x\n", regs->sdram_tim1_shdw);
  87. seq_printf(s, "sdram_tim2_shdw\t: 0x%08x\n", regs->sdram_tim2_shdw);
  88. seq_printf(s, "sdram_tim3_shdw\t: 0x%08x\n", regs->sdram_tim3_shdw);
  89. if (ip_rev == EMIF_4D) {
  90. seq_printf(s, "read_idle_ctrl_shdw_normal\t: 0x%08x\n",
  91. regs->read_idle_ctrl_shdw_normal);
  92. seq_printf(s, "read_idle_ctrl_shdw_volt_ramp\t: 0x%08x\n",
  93. regs->read_idle_ctrl_shdw_volt_ramp);
  94. } else if (ip_rev == EMIF_4D5) {
  95. seq_printf(s, "dll_calib_ctrl_shdw_normal\t: 0x%08x\n",
  96. regs->dll_calib_ctrl_shdw_normal);
  97. seq_printf(s, "dll_calib_ctrl_shdw_volt_ramp\t: 0x%08x\n",
  98. regs->dll_calib_ctrl_shdw_volt_ramp);
  99. }
  100. if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4) {
  101. seq_printf(s, "ref_ctrl_shdw_derated\t: 0x%08x\n",
  102. regs->ref_ctrl_shdw_derated);
  103. seq_printf(s, "sdram_tim1_shdw_derated\t: 0x%08x\n",
  104. regs->sdram_tim1_shdw_derated);
  105. seq_printf(s, "sdram_tim3_shdw_derated\t: 0x%08x\n",
  106. regs->sdram_tim3_shdw_derated);
  107. }
  108. }
  109. static int emif_regdump_show(struct seq_file *s, void *unused)
  110. {
  111. struct emif_data *emif = s->private;
  112. struct emif_regs **regs_cache;
  113. int i;
  114. if (emif->duplicate)
  115. regs_cache = emif1->regs_cache;
  116. else
  117. regs_cache = emif->regs_cache;
  118. for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) {
  119. do_emif_regdump_show(s, emif, regs_cache[i]);
  120. seq_printf(s, "\n");
  121. }
  122. return 0;
  123. }
  124. static int emif_regdump_open(struct inode *inode, struct file *file)
  125. {
  126. return single_open(file, emif_regdump_show, inode->i_private);
  127. }
  128. static const struct file_operations emif_regdump_fops = {
  129. .open = emif_regdump_open,
  130. .read = seq_read,
  131. .release = single_release,
  132. };
  133. static int emif_mr4_show(struct seq_file *s, void *unused)
  134. {
  135. struct emif_data *emif = s->private;
  136. seq_printf(s, "MR4=%d\n", emif->temperature_level);
  137. return 0;
  138. }
  139. static int emif_mr4_open(struct inode *inode, struct file *file)
  140. {
  141. return single_open(file, emif_mr4_show, inode->i_private);
  142. }
  143. static const struct file_operations emif_mr4_fops = {
  144. .open = emif_mr4_open,
  145. .read = seq_read,
  146. .release = single_release,
  147. };
  148. static int __init_or_module emif_debugfs_init(struct emif_data *emif)
  149. {
  150. struct dentry *dentry;
  151. int ret;
  152. dentry = debugfs_create_dir(dev_name(emif->dev), NULL);
  153. if (!dentry) {
  154. ret = -ENOMEM;
  155. goto err0;
  156. }
  157. emif->debugfs_root = dentry;
  158. dentry = debugfs_create_file("regcache_dump", S_IRUGO,
  159. emif->debugfs_root, emif, &emif_regdump_fops);
  160. if (!dentry) {
  161. ret = -ENOMEM;
  162. goto err1;
  163. }
  164. dentry = debugfs_create_file("mr4", S_IRUGO,
  165. emif->debugfs_root, emif, &emif_mr4_fops);
  166. if (!dentry) {
  167. ret = -ENOMEM;
  168. goto err1;
  169. }
  170. return 0;
  171. err1:
  172. debugfs_remove_recursive(emif->debugfs_root);
  173. err0:
  174. return ret;
  175. }
  176. static void __exit emif_debugfs_exit(struct emif_data *emif)
  177. {
  178. debugfs_remove_recursive(emif->debugfs_root);
  179. emif->debugfs_root = NULL;
  180. }
  181. #else
  182. static inline int __init_or_module emif_debugfs_init(struct emif_data *emif)
  183. {
  184. return 0;
  185. }
  186. static inline void __exit emif_debugfs_exit(struct emif_data *emif)
  187. {
  188. }
  189. #endif
  190. /*
  191. * Calculate the period of DDR clock from frequency value
  192. */
  193. static void set_ddr_clk_period(u32 freq)
  194. {
  195. /* Divide 10^12 by frequency to get period in ps */
  196. t_ck = (u32)DIV_ROUND_UP_ULL(1000000000000ull, freq);
  197. }
  198. /*
  199. * Get bus width used by EMIF. Note that this may be different from the
  200. * bus width of the DDR devices used. For instance two 16-bit DDR devices
  201. * may be connected to a given CS of EMIF. In this case bus width as far
  202. * as EMIF is concerned is 32, where as the DDR bus width is 16 bits.
  203. */
  204. static u32 get_emif_bus_width(struct emif_data *emif)
  205. {
  206. u32 width;
  207. void __iomem *base = emif->base;
  208. width = (readl(base + EMIF_SDRAM_CONFIG) & NARROW_MODE_MASK)
  209. >> NARROW_MODE_SHIFT;
  210. width = width == 0 ? 32 : 16;
  211. return width;
  212. }
  213. /*
  214. * Get the CL from SDRAM_CONFIG register
  215. */
  216. static u32 get_cl(struct emif_data *emif)
  217. {
  218. u32 cl;
  219. void __iomem *base = emif->base;
  220. cl = (readl(base + EMIF_SDRAM_CONFIG) & CL_MASK) >> CL_SHIFT;
  221. return cl;
  222. }
  223. static void set_lpmode(struct emif_data *emif, u8 lpmode)
  224. {
  225. u32 temp;
  226. void __iomem *base = emif->base;
  227. /*
  228. * Workaround for errata i743 - LPDDR2 Power-Down State is Not
  229. * Efficient
  230. *
  231. * i743 DESCRIPTION:
  232. * The EMIF supports power-down state for low power. The EMIF
  233. * automatically puts the SDRAM into power-down after the memory is
  234. * not accessed for a defined number of cycles and the
  235. * EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field is set to 0x4.
  236. * As the EMIF supports automatic output impedance calibration, a ZQ
  237. * calibration long command is issued every time it exits active
  238. * power-down and precharge power-down modes. The EMIF waits and
  239. * blocks any other command during this calibration.
  240. * The EMIF does not allow selective disabling of ZQ calibration upon
  241. * exit of power-down mode. Due to very short periods of power-down
  242. * cycles, ZQ calibration overhead creates bandwidth issues and
  243. * increases overall system power consumption. On the other hand,
  244. * issuing ZQ calibration long commands when exiting self-refresh is
  245. * still required.
  246. *
  247. * WORKAROUND
  248. * Because there is no power consumption benefit of the power-down due
  249. * to the calibration and there is a performance risk, the guideline
  250. * is to not allow power-down state and, therefore, to not have set
  251. * the EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field to 0x4.
  252. */
  253. if ((emif->plat_data->ip_rev == EMIF_4D) &&
  254. (EMIF_LP_MODE_PWR_DN == lpmode)) {
  255. WARN_ONCE(1,
  256. "REG_LP_MODE = LP_MODE_PWR_DN(4) is prohibited by"
  257. "erratum i743 switch to LP_MODE_SELF_REFRESH(2)\n");
  258. /* rollback LP_MODE to Self-refresh mode */
  259. lpmode = EMIF_LP_MODE_SELF_REFRESH;
  260. }
  261. temp = readl(base + EMIF_POWER_MANAGEMENT_CONTROL);
  262. temp &= ~LP_MODE_MASK;
  263. temp |= (lpmode << LP_MODE_SHIFT);
  264. writel(temp, base + EMIF_POWER_MANAGEMENT_CONTROL);
  265. }
  266. static void do_freq_update(void)
  267. {
  268. struct emif_data *emif;
  269. /*
  270. * Workaround for errata i728: Disable LPMODE during FREQ_UPDATE
  271. *
  272. * i728 DESCRIPTION:
  273. * The EMIF automatically puts the SDRAM into self-refresh mode
  274. * after the EMIF has not performed accesses during
  275. * EMIF_PWR_MGMT_CTRL[7:4] REG_SR_TIM number of DDR clock cycles
  276. * and the EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field is set
  277. * to 0x2. If during a small window the following three events
  278. * occur:
  279. * - The SR_TIMING counter expires
  280. * - And frequency change is requested
  281. * - And OCP access is requested
  282. * Then it causes instable clock on the DDR interface.
  283. *
  284. * WORKAROUND
  285. * To avoid the occurrence of the three events, the workaround
  286. * is to disable the self-refresh when requesting a frequency
  287. * change. Before requesting a frequency change the software must
  288. * program EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x0. When the
  289. * frequency change has been done, the software can reprogram
  290. * EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x2
  291. */
  292. list_for_each_entry(emif, &device_list, node) {
  293. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  294. set_lpmode(emif, EMIF_LP_MODE_DISABLE);
  295. }
  296. /*
  297. * TODO: Do FREQ_UPDATE here when an API
  298. * is available for this as part of the new
  299. * clock framework
  300. */
  301. list_for_each_entry(emif, &device_list, node) {
  302. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  303. set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH);
  304. }
  305. }
  306. /* Find addressing table entry based on the device's type and density */
  307. static const struct lpddr2_addressing *get_addressing_table(
  308. const struct ddr_device_info *device_info)
  309. {
  310. u32 index, type, density;
  311. type = device_info->type;
  312. density = device_info->density;
  313. switch (type) {
  314. case DDR_TYPE_LPDDR2_S4:
  315. index = density - 1;
  316. break;
  317. case DDR_TYPE_LPDDR2_S2:
  318. switch (density) {
  319. case DDR_DENSITY_1Gb:
  320. case DDR_DENSITY_2Gb:
  321. index = density + 3;
  322. break;
  323. default:
  324. index = density - 1;
  325. }
  326. break;
  327. default:
  328. return NULL;
  329. }
  330. return &lpddr2_jedec_addressing_table[index];
  331. }
  332. /*
  333. * Find the the right timing table from the array of timing
  334. * tables of the device using DDR clock frequency
  335. */
  336. static const struct lpddr2_timings *get_timings_table(struct emif_data *emif,
  337. u32 freq)
  338. {
  339. u32 i, min, max, freq_nearest;
  340. const struct lpddr2_timings *timings = NULL;
  341. const struct lpddr2_timings *timings_arr = emif->plat_data->timings;
  342. struct device *dev = emif->dev;
  343. /* Start with a very high frequency - 1GHz */
  344. freq_nearest = 1000000000;
  345. /*
  346. * Find the timings table such that:
  347. * 1. the frequency range covers the required frequency(safe) AND
  348. * 2. the max_freq is closest to the required frequency(optimal)
  349. */
  350. for (i = 0; i < emif->plat_data->timings_arr_size; i++) {
  351. max = timings_arr[i].max_freq;
  352. min = timings_arr[i].min_freq;
  353. if ((freq >= min) && (freq <= max) && (max < freq_nearest)) {
  354. freq_nearest = max;
  355. timings = &timings_arr[i];
  356. }
  357. }
  358. if (!timings)
  359. dev_err(dev, "%s: couldn't find timings for - %dHz\n",
  360. __func__, freq);
  361. dev_dbg(dev, "%s: timings table: freq %d, speed bin freq %d\n",
  362. __func__, freq, freq_nearest);
  363. return timings;
  364. }
  365. static u32 get_sdram_ref_ctrl_shdw(u32 freq,
  366. const struct lpddr2_addressing *addressing)
  367. {
  368. u32 ref_ctrl_shdw = 0, val = 0, freq_khz, t_refi;
  369. /* Scale down frequency and t_refi to avoid overflow */
  370. freq_khz = freq / 1000;
  371. t_refi = addressing->tREFI_ns / 100;
  372. /*
  373. * refresh rate to be set is 'tREFI(in us) * freq in MHz
  374. * division by 10000 to account for change in units
  375. */
  376. val = t_refi * freq_khz / 10000;
  377. ref_ctrl_shdw |= val << REFRESH_RATE_SHIFT;
  378. return ref_ctrl_shdw;
  379. }
  380. static u32 get_sdram_tim_1_shdw(const struct lpddr2_timings *timings,
  381. const struct lpddr2_min_tck *min_tck,
  382. const struct lpddr2_addressing *addressing)
  383. {
  384. u32 tim1 = 0, val = 0;
  385. val = max(min_tck->tWTR, DIV_ROUND_UP(timings->tWTR, t_ck)) - 1;
  386. tim1 |= val << T_WTR_SHIFT;
  387. if (addressing->num_banks == B8)
  388. val = DIV_ROUND_UP(timings->tFAW, t_ck*4);
  389. else
  390. val = max(min_tck->tRRD, DIV_ROUND_UP(timings->tRRD, t_ck));
  391. tim1 |= (val - 1) << T_RRD_SHIFT;
  392. val = DIV_ROUND_UP(timings->tRAS_min + timings->tRPab, t_ck) - 1;
  393. tim1 |= val << T_RC_SHIFT;
  394. val = max(min_tck->tRASmin, DIV_ROUND_UP(timings->tRAS_min, t_ck));
  395. tim1 |= (val - 1) << T_RAS_SHIFT;
  396. val = max(min_tck->tWR, DIV_ROUND_UP(timings->tWR, t_ck)) - 1;
  397. tim1 |= val << T_WR_SHIFT;
  398. val = max(min_tck->tRCD, DIV_ROUND_UP(timings->tRCD, t_ck)) - 1;
  399. tim1 |= val << T_RCD_SHIFT;
  400. val = max(min_tck->tRPab, DIV_ROUND_UP(timings->tRPab, t_ck)) - 1;
  401. tim1 |= val << T_RP_SHIFT;
  402. return tim1;
  403. }
  404. static u32 get_sdram_tim_1_shdw_derated(const struct lpddr2_timings *timings,
  405. const struct lpddr2_min_tck *min_tck,
  406. const struct lpddr2_addressing *addressing)
  407. {
  408. u32 tim1 = 0, val = 0;
  409. val = max(min_tck->tWTR, DIV_ROUND_UP(timings->tWTR, t_ck)) - 1;
  410. tim1 = val << T_WTR_SHIFT;
  411. /*
  412. * tFAW is approximately 4 times tRRD. So add 1875*4 = 7500ps
  413. * to tFAW for de-rating
  414. */
  415. if (addressing->num_banks == B8) {
  416. val = DIV_ROUND_UP(timings->tFAW + 7500, 4 * t_ck) - 1;
  417. } else {
  418. val = DIV_ROUND_UP(timings->tRRD + 1875, t_ck);
  419. val = max(min_tck->tRRD, val) - 1;
  420. }
  421. tim1 |= val << T_RRD_SHIFT;
  422. val = DIV_ROUND_UP(timings->tRAS_min + timings->tRPab + 1875, t_ck);
  423. tim1 |= (val - 1) << T_RC_SHIFT;
  424. val = DIV_ROUND_UP(timings->tRAS_min + 1875, t_ck);
  425. val = max(min_tck->tRASmin, val) - 1;
  426. tim1 |= val << T_RAS_SHIFT;
  427. val = max(min_tck->tWR, DIV_ROUND_UP(timings->tWR, t_ck)) - 1;
  428. tim1 |= val << T_WR_SHIFT;
  429. val = max(min_tck->tRCD, DIV_ROUND_UP(timings->tRCD + 1875, t_ck));
  430. tim1 |= (val - 1) << T_RCD_SHIFT;
  431. val = max(min_tck->tRPab, DIV_ROUND_UP(timings->tRPab + 1875, t_ck));
  432. tim1 |= (val - 1) << T_RP_SHIFT;
  433. return tim1;
  434. }
  435. static u32 get_sdram_tim_2_shdw(const struct lpddr2_timings *timings,
  436. const struct lpddr2_min_tck *min_tck,
  437. const struct lpddr2_addressing *addressing,
  438. u32 type)
  439. {
  440. u32 tim2 = 0, val = 0;
  441. val = min_tck->tCKE - 1;
  442. tim2 |= val << T_CKE_SHIFT;
  443. val = max(min_tck->tRTP, DIV_ROUND_UP(timings->tRTP, t_ck)) - 1;
  444. tim2 |= val << T_RTP_SHIFT;
  445. /* tXSNR = tRFCab_ps + 10 ns(tRFCab_ps for LPDDR2). */
  446. val = DIV_ROUND_UP(addressing->tRFCab_ps + 10000, t_ck) - 1;
  447. tim2 |= val << T_XSNR_SHIFT;
  448. /* XSRD same as XSNR for LPDDR2 */
  449. tim2 |= val << T_XSRD_SHIFT;
  450. val = max(min_tck->tXP, DIV_ROUND_UP(timings->tXP, t_ck)) - 1;
  451. tim2 |= val << T_XP_SHIFT;
  452. return tim2;
  453. }
  454. static u32 get_sdram_tim_3_shdw(const struct lpddr2_timings *timings,
  455. const struct lpddr2_min_tck *min_tck,
  456. const struct lpddr2_addressing *addressing,
  457. u32 type, u32 ip_rev, u32 derated)
  458. {
  459. u32 tim3 = 0, val = 0, t_dqsck;
  460. val = timings->tRAS_max_ns / addressing->tREFI_ns - 1;
  461. val = val > 0xF ? 0xF : val;
  462. tim3 |= val << T_RAS_MAX_SHIFT;
  463. val = DIV_ROUND_UP(addressing->tRFCab_ps, t_ck) - 1;
  464. tim3 |= val << T_RFC_SHIFT;
  465. t_dqsck = (derated == EMIF_DERATED_TIMINGS) ?
  466. timings->tDQSCK_max_derated : timings->tDQSCK_max;
  467. if (ip_rev == EMIF_4D5)
  468. val = DIV_ROUND_UP(t_dqsck + 1000, t_ck) - 1;
  469. else
  470. val = DIV_ROUND_UP(t_dqsck, t_ck) - 1;
  471. tim3 |= val << T_TDQSCKMAX_SHIFT;
  472. val = DIV_ROUND_UP(timings->tZQCS, t_ck) - 1;
  473. tim3 |= val << ZQ_ZQCS_SHIFT;
  474. val = DIV_ROUND_UP(timings->tCKESR, t_ck);
  475. val = max(min_tck->tCKESR, val) - 1;
  476. tim3 |= val << T_CKESR_SHIFT;
  477. if (ip_rev == EMIF_4D5) {
  478. tim3 |= (EMIF_T_CSTA - 1) << T_CSTA_SHIFT;
  479. val = DIV_ROUND_UP(EMIF_T_PDLL_UL, 128) - 1;
  480. tim3 |= val << T_PDLL_UL_SHIFT;
  481. }
  482. return tim3;
  483. }
  484. static u32 get_zq_config_reg(const struct lpddr2_addressing *addressing,
  485. bool cs1_used, bool cal_resistors_per_cs)
  486. {
  487. u32 zq = 0, val = 0;
  488. val = EMIF_ZQCS_INTERVAL_US * 1000 / addressing->tREFI_ns;
  489. zq |= val << ZQ_REFINTERVAL_SHIFT;
  490. val = DIV_ROUND_UP(T_ZQCL_DEFAULT_NS, T_ZQCS_DEFAULT_NS) - 1;
  491. zq |= val << ZQ_ZQCL_MULT_SHIFT;
  492. val = DIV_ROUND_UP(T_ZQINIT_DEFAULT_NS, T_ZQCL_DEFAULT_NS) - 1;
  493. zq |= val << ZQ_ZQINIT_MULT_SHIFT;
  494. zq |= ZQ_SFEXITEN_ENABLE << ZQ_SFEXITEN_SHIFT;
  495. if (cal_resistors_per_cs)
  496. zq |= ZQ_DUALCALEN_ENABLE << ZQ_DUALCALEN_SHIFT;
  497. else
  498. zq |= ZQ_DUALCALEN_DISABLE << ZQ_DUALCALEN_SHIFT;
  499. zq |= ZQ_CS0EN_MASK; /* CS0 is used for sure */
  500. val = cs1_used ? 1 : 0;
  501. zq |= val << ZQ_CS1EN_SHIFT;
  502. return zq;
  503. }
  504. static u32 get_temp_alert_config(const struct lpddr2_addressing *addressing,
  505. const struct emif_custom_configs *custom_configs, bool cs1_used,
  506. u32 sdram_io_width, u32 emif_bus_width)
  507. {
  508. u32 alert = 0, interval, devcnt;
  509. if (custom_configs && (custom_configs->mask &
  510. EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL))
  511. interval = custom_configs->temp_alert_poll_interval_ms;
  512. else
  513. interval = TEMP_ALERT_POLL_INTERVAL_DEFAULT_MS;
  514. interval *= 1000000; /* Convert to ns */
  515. interval /= addressing->tREFI_ns; /* Convert to refresh cycles */
  516. alert |= (interval << TA_REFINTERVAL_SHIFT);
  517. /*
  518. * sdram_io_width is in 'log2(x) - 1' form. Convert emif_bus_width
  519. * also to this form and subtract to get TA_DEVCNT, which is
  520. * in log2(x) form.
  521. */
  522. emif_bus_width = __fls(emif_bus_width) - 1;
  523. devcnt = emif_bus_width - sdram_io_width;
  524. alert |= devcnt << TA_DEVCNT_SHIFT;
  525. /* DEVWDT is in 'log2(x) - 3' form */
  526. alert |= (sdram_io_width - 2) << TA_DEVWDT_SHIFT;
  527. alert |= 1 << TA_SFEXITEN_SHIFT;
  528. alert |= 1 << TA_CS0EN_SHIFT;
  529. alert |= (cs1_used ? 1 : 0) << TA_CS1EN_SHIFT;
  530. return alert;
  531. }
  532. static u32 get_read_idle_ctrl_shdw(u8 volt_ramp)
  533. {
  534. u32 idle = 0, val = 0;
  535. /*
  536. * Maximum value in normal conditions and increased frequency
  537. * when voltage is ramping
  538. */
  539. if (volt_ramp)
  540. val = READ_IDLE_INTERVAL_DVFS / t_ck / 64 - 1;
  541. else
  542. val = 0x1FF;
  543. /*
  544. * READ_IDLE_CTRL register in EMIF4D has same offset and fields
  545. * as DLL_CALIB_CTRL in EMIF4D5, so use the same shifts
  546. */
  547. idle |= val << DLL_CALIB_INTERVAL_SHIFT;
  548. idle |= EMIF_READ_IDLE_LEN_VAL << ACK_WAIT_SHIFT;
  549. return idle;
  550. }
  551. static u32 get_dll_calib_ctrl_shdw(u8 volt_ramp)
  552. {
  553. u32 calib = 0, val = 0;
  554. if (volt_ramp == DDR_VOLTAGE_RAMPING)
  555. val = DLL_CALIB_INTERVAL_DVFS / t_ck / 16 - 1;
  556. else
  557. val = 0; /* Disabled when voltage is stable */
  558. calib |= val << DLL_CALIB_INTERVAL_SHIFT;
  559. calib |= DLL_CALIB_ACK_WAIT_VAL << ACK_WAIT_SHIFT;
  560. return calib;
  561. }
  562. static u32 get_ddr_phy_ctrl_1_attilaphy_4d(const struct lpddr2_timings *timings,
  563. u32 freq, u8 RL)
  564. {
  565. u32 phy = EMIF_DDR_PHY_CTRL_1_BASE_VAL_ATTILAPHY, val = 0;
  566. val = RL + DIV_ROUND_UP(timings->tDQSCK_max, t_ck) - 1;
  567. phy |= val << READ_LATENCY_SHIFT_4D;
  568. if (freq <= 100000000)
  569. val = EMIF_DLL_SLAVE_DLY_CTRL_100_MHZ_AND_LESS_ATTILAPHY;
  570. else if (freq <= 200000000)
  571. val = EMIF_DLL_SLAVE_DLY_CTRL_200_MHZ_ATTILAPHY;
  572. else
  573. val = EMIF_DLL_SLAVE_DLY_CTRL_400_MHZ_ATTILAPHY;
  574. phy |= val << DLL_SLAVE_DLY_CTRL_SHIFT_4D;
  575. return phy;
  576. }
  577. static u32 get_phy_ctrl_1_intelliphy_4d5(u32 freq, u8 cl)
  578. {
  579. u32 phy = EMIF_DDR_PHY_CTRL_1_BASE_VAL_INTELLIPHY, half_delay;
  580. /*
  581. * DLL operates at 266 MHz. If DDR frequency is near 266 MHz,
  582. * half-delay is not needed else set half-delay
  583. */
  584. if (freq >= 265000000 && freq < 267000000)
  585. half_delay = 0;
  586. else
  587. half_delay = 1;
  588. phy |= half_delay << DLL_HALF_DELAY_SHIFT_4D5;
  589. phy |= ((cl + DIV_ROUND_UP(EMIF_PHY_TOTAL_READ_LATENCY_INTELLIPHY_PS,
  590. t_ck) - 1) << READ_LATENCY_SHIFT_4D5);
  591. return phy;
  592. }
  593. static u32 get_ext_phy_ctrl_2_intelliphy_4d5(void)
  594. {
  595. u32 fifo_we_slave_ratio;
  596. fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
  597. EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
  598. return fifo_we_slave_ratio | fifo_we_slave_ratio << 11 |
  599. fifo_we_slave_ratio << 22;
  600. }
  601. static u32 get_ext_phy_ctrl_3_intelliphy_4d5(void)
  602. {
  603. u32 fifo_we_slave_ratio;
  604. fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
  605. EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
  606. return fifo_we_slave_ratio >> 10 | fifo_we_slave_ratio << 1 |
  607. fifo_we_slave_ratio << 12 | fifo_we_slave_ratio << 23;
  608. }
  609. static u32 get_ext_phy_ctrl_4_intelliphy_4d5(void)
  610. {
  611. u32 fifo_we_slave_ratio;
  612. fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
  613. EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
  614. return fifo_we_slave_ratio >> 9 | fifo_we_slave_ratio << 2 |
  615. fifo_we_slave_ratio << 13;
  616. }
  617. static u32 get_pwr_mgmt_ctrl(u32 freq, struct emif_data *emif, u32 ip_rev)
  618. {
  619. u32 pwr_mgmt_ctrl = 0, timeout;
  620. u32 lpmode = EMIF_LP_MODE_SELF_REFRESH;
  621. u32 timeout_perf = EMIF_LP_MODE_TIMEOUT_PERFORMANCE;
  622. u32 timeout_pwr = EMIF_LP_MODE_TIMEOUT_POWER;
  623. u32 freq_threshold = EMIF_LP_MODE_FREQ_THRESHOLD;
  624. u32 mask;
  625. u8 shift;
  626. struct emif_custom_configs *cust_cfgs = emif->plat_data->custom_configs;
  627. if (cust_cfgs && (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE)) {
  628. lpmode = cust_cfgs->lpmode;
  629. timeout_perf = cust_cfgs->lpmode_timeout_performance;
  630. timeout_pwr = cust_cfgs->lpmode_timeout_power;
  631. freq_threshold = cust_cfgs->lpmode_freq_threshold;
  632. }
  633. /* Timeout based on DDR frequency */
  634. timeout = freq >= freq_threshold ? timeout_perf : timeout_pwr;
  635. /*
  636. * The value to be set in register is "log2(timeout) - 3"
  637. * if timeout < 16 load 0 in register
  638. * if timeout is not a power of 2, round to next highest power of 2
  639. */
  640. if (timeout < 16) {
  641. timeout = 0;
  642. } else {
  643. if (timeout & (timeout - 1))
  644. timeout <<= 1;
  645. timeout = __fls(timeout) - 3;
  646. }
  647. switch (lpmode) {
  648. case EMIF_LP_MODE_CLOCK_STOP:
  649. shift = CS_TIM_SHIFT;
  650. mask = CS_TIM_MASK;
  651. break;
  652. case EMIF_LP_MODE_SELF_REFRESH:
  653. /* Workaround for errata i735 */
  654. if (timeout < 6)
  655. timeout = 6;
  656. shift = SR_TIM_SHIFT;
  657. mask = SR_TIM_MASK;
  658. break;
  659. case EMIF_LP_MODE_PWR_DN:
  660. shift = PD_TIM_SHIFT;
  661. mask = PD_TIM_MASK;
  662. break;
  663. case EMIF_LP_MODE_DISABLE:
  664. default:
  665. mask = 0;
  666. shift = 0;
  667. break;
  668. }
  669. /* Round to maximum in case of overflow, BUT warn! */
  670. if (lpmode != EMIF_LP_MODE_DISABLE && timeout > mask >> shift) {
  671. pr_err("TIMEOUT Overflow - lpmode=%d perf=%d pwr=%d freq=%d\n",
  672. lpmode,
  673. timeout_perf,
  674. timeout_pwr,
  675. freq_threshold);
  676. WARN(1, "timeout=0x%02x greater than 0x%02x. Using max\n",
  677. timeout, mask >> shift);
  678. timeout = mask >> shift;
  679. }
  680. /* Setup required timing */
  681. pwr_mgmt_ctrl = (timeout << shift) & mask;
  682. /* setup a default mask for rest of the modes */
  683. pwr_mgmt_ctrl |= (SR_TIM_MASK | CS_TIM_MASK | PD_TIM_MASK) &
  684. ~mask;
  685. /* No CS_TIM in EMIF_4D5 */
  686. if (ip_rev == EMIF_4D5)
  687. pwr_mgmt_ctrl &= ~CS_TIM_MASK;
  688. pwr_mgmt_ctrl |= lpmode << LP_MODE_SHIFT;
  689. return pwr_mgmt_ctrl;
  690. }
  691. /*
  692. * Get the temperature level of the EMIF instance:
  693. * Reads the MR4 register of attached SDRAM parts to find out the temperature
  694. * level. If there are two parts attached(one on each CS), then the temperature
  695. * level for the EMIF instance is the higher of the two temperatures.
  696. */
  697. static void get_temperature_level(struct emif_data *emif)
  698. {
  699. u32 temp, temperature_level;
  700. void __iomem *base;
  701. base = emif->base;
  702. /* Read mode register 4 */
  703. writel(DDR_MR4, base + EMIF_LPDDR2_MODE_REG_CONFIG);
  704. temperature_level = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
  705. temperature_level = (temperature_level & MR4_SDRAM_REF_RATE_MASK) >>
  706. MR4_SDRAM_REF_RATE_SHIFT;
  707. if (emif->plat_data->device_info->cs1_used) {
  708. writel(DDR_MR4 | CS_MASK, base + EMIF_LPDDR2_MODE_REG_CONFIG);
  709. temp = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
  710. temp = (temp & MR4_SDRAM_REF_RATE_MASK)
  711. >> MR4_SDRAM_REF_RATE_SHIFT;
  712. temperature_level = max(temp, temperature_level);
  713. }
  714. /* treat everything less than nominal(3) in MR4 as nominal */
  715. if (unlikely(temperature_level < SDRAM_TEMP_NOMINAL))
  716. temperature_level = SDRAM_TEMP_NOMINAL;
  717. /* if we get reserved value in MR4 persist with the existing value */
  718. if (likely(temperature_level != SDRAM_TEMP_RESERVED_4))
  719. emif->temperature_level = temperature_level;
  720. }
  721. /*
  722. * Program EMIF shadow registers that are not dependent on temperature
  723. * or voltage
  724. */
  725. static void setup_registers(struct emif_data *emif, struct emif_regs *regs)
  726. {
  727. void __iomem *base = emif->base;
  728. writel(regs->sdram_tim2_shdw, base + EMIF_SDRAM_TIMING_2_SHDW);
  729. writel(regs->phy_ctrl_1_shdw, base + EMIF_DDR_PHY_CTRL_1_SHDW);
  730. writel(regs->pwr_mgmt_ctrl_shdw,
  731. base + EMIF_POWER_MANAGEMENT_CTRL_SHDW);
  732. /* Settings specific for EMIF4D5 */
  733. if (emif->plat_data->ip_rev != EMIF_4D5)
  734. return;
  735. writel(regs->ext_phy_ctrl_2_shdw, base + EMIF_EXT_PHY_CTRL_2_SHDW);
  736. writel(regs->ext_phy_ctrl_3_shdw, base + EMIF_EXT_PHY_CTRL_3_SHDW);
  737. writel(regs->ext_phy_ctrl_4_shdw, base + EMIF_EXT_PHY_CTRL_4_SHDW);
  738. }
  739. /*
  740. * When voltage ramps dll calibration and forced read idle should
  741. * happen more often
  742. */
  743. static void setup_volt_sensitive_regs(struct emif_data *emif,
  744. struct emif_regs *regs, u32 volt_state)
  745. {
  746. u32 calib_ctrl;
  747. void __iomem *base = emif->base;
  748. /*
  749. * EMIF_READ_IDLE_CTRL in EMIF4D refers to the same register as
  750. * EMIF_DLL_CALIB_CTRL in EMIF4D5 and dll_calib_ctrl_shadow_*
  751. * is an alias of the respective read_idle_ctrl_shdw_* (members of
  752. * a union). So, the below code takes care of both cases
  753. */
  754. if (volt_state == DDR_VOLTAGE_RAMPING)
  755. calib_ctrl = regs->dll_calib_ctrl_shdw_volt_ramp;
  756. else
  757. calib_ctrl = regs->dll_calib_ctrl_shdw_normal;
  758. writel(calib_ctrl, base + EMIF_DLL_CALIB_CTRL_SHDW);
  759. }
  760. /*
  761. * setup_temperature_sensitive_regs() - set the timings for temperature
  762. * sensitive registers. This happens once at initialisation time based
  763. * on the temperature at boot time and subsequently based on the temperature
  764. * alert interrupt. Temperature alert can happen when the temperature
  765. * increases or drops. So this function can have the effect of either
  766. * derating the timings or going back to nominal values.
  767. */
  768. static void setup_temperature_sensitive_regs(struct emif_data *emif,
  769. struct emif_regs *regs)
  770. {
  771. u32 tim1, tim3, ref_ctrl, type;
  772. void __iomem *base = emif->base;
  773. u32 temperature;
  774. type = emif->plat_data->device_info->type;
  775. tim1 = regs->sdram_tim1_shdw;
  776. tim3 = regs->sdram_tim3_shdw;
  777. ref_ctrl = regs->ref_ctrl_shdw;
  778. /* No de-rating for non-lpddr2 devices */
  779. if (type != DDR_TYPE_LPDDR2_S2 && type != DDR_TYPE_LPDDR2_S4)
  780. goto out;
  781. temperature = emif->temperature_level;
  782. if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH) {
  783. ref_ctrl = regs->ref_ctrl_shdw_derated;
  784. } else if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH_AND_TIMINGS) {
  785. tim1 = regs->sdram_tim1_shdw_derated;
  786. tim3 = regs->sdram_tim3_shdw_derated;
  787. ref_ctrl = regs->ref_ctrl_shdw_derated;
  788. }
  789. out:
  790. writel(tim1, base + EMIF_SDRAM_TIMING_1_SHDW);
  791. writel(tim3, base + EMIF_SDRAM_TIMING_3_SHDW);
  792. writel(ref_ctrl, base + EMIF_SDRAM_REFRESH_CTRL_SHDW);
  793. }
  794. static irqreturn_t handle_temp_alert(void __iomem *base, struct emif_data *emif)
  795. {
  796. u32 old_temp_level;
  797. irqreturn_t ret = IRQ_HANDLED;
  798. struct emif_custom_configs *custom_configs;
  799. spin_lock_irqsave(&emif_lock, irq_state);
  800. old_temp_level = emif->temperature_level;
  801. get_temperature_level(emif);
  802. if (unlikely(emif->temperature_level == old_temp_level)) {
  803. goto out;
  804. } else if (!emif->curr_regs) {
  805. dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
  806. goto out;
  807. }
  808. custom_configs = emif->plat_data->custom_configs;
  809. /*
  810. * IF we detect higher than "nominal rating" from DDR sensor
  811. * on an unsupported DDR part, shutdown system
  812. */
  813. if (custom_configs && !(custom_configs->mask &
  814. EMIF_CUSTOM_CONFIG_EXTENDED_TEMP_PART)) {
  815. if (emif->temperature_level >= SDRAM_TEMP_HIGH_DERATE_REFRESH) {
  816. dev_err(emif->dev,
  817. "%s:NOT Extended temperature capable memory."
  818. "Converting MR4=0x%02x as shutdown event\n",
  819. __func__, emif->temperature_level);
  820. /*
  821. * Temperature far too high - do kernel_power_off()
  822. * from thread context
  823. */
  824. emif->temperature_level = SDRAM_TEMP_VERY_HIGH_SHUTDOWN;
  825. ret = IRQ_WAKE_THREAD;
  826. goto out;
  827. }
  828. }
  829. if (emif->temperature_level < old_temp_level ||
  830. emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
  831. /*
  832. * Temperature coming down - defer handling to thread OR
  833. * Temperature far too high - do kernel_power_off() from
  834. * thread context
  835. */
  836. ret = IRQ_WAKE_THREAD;
  837. } else {
  838. /* Temperature is going up - handle immediately */
  839. setup_temperature_sensitive_regs(emif, emif->curr_regs);
  840. do_freq_update();
  841. }
  842. out:
  843. spin_unlock_irqrestore(&emif_lock, irq_state);
  844. return ret;
  845. }
  846. static irqreturn_t emif_interrupt_handler(int irq, void *dev_id)
  847. {
  848. u32 interrupts;
  849. struct emif_data *emif = dev_id;
  850. void __iomem *base = emif->base;
  851. struct device *dev = emif->dev;
  852. irqreturn_t ret = IRQ_HANDLED;
  853. /* Save the status and clear it */
  854. interrupts = readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  855. writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  856. /*
  857. * Handle temperature alert
  858. * Temperature alert should be same for all ports
  859. * So, it's enough to process it only for one of the ports
  860. */
  861. if (interrupts & TA_SYS_MASK)
  862. ret = handle_temp_alert(base, emif);
  863. if (interrupts & ERR_SYS_MASK)
  864. dev_err(dev, "Access error from SYS port - %x\n", interrupts);
  865. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
  866. /* Save the status and clear it */
  867. interrupts = readl(base + EMIF_LL_OCP_INTERRUPT_STATUS);
  868. writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_STATUS);
  869. if (interrupts & ERR_LL_MASK)
  870. dev_err(dev, "Access error from LL port - %x\n",
  871. interrupts);
  872. }
  873. return ret;
  874. }
  875. static irqreturn_t emif_threaded_isr(int irq, void *dev_id)
  876. {
  877. struct emif_data *emif = dev_id;
  878. if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
  879. dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
  880. /* If we have Power OFF ability, use it, else try restarting */
  881. if (pm_power_off) {
  882. kernel_power_off();
  883. } else {
  884. WARN(1, "FIXME: NO pm_power_off!!! trying restart\n");
  885. kernel_restart("SDRAM Over-temp Emergency restart");
  886. }
  887. return IRQ_HANDLED;
  888. }
  889. spin_lock_irqsave(&emif_lock, irq_state);
  890. if (emif->curr_regs) {
  891. setup_temperature_sensitive_regs(emif, emif->curr_regs);
  892. do_freq_update();
  893. } else {
  894. dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
  895. }
  896. spin_unlock_irqrestore(&emif_lock, irq_state);
  897. return IRQ_HANDLED;
  898. }
  899. static void clear_all_interrupts(struct emif_data *emif)
  900. {
  901. void __iomem *base = emif->base;
  902. writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS),
  903. base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  904. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
  905. writel(readl(base + EMIF_LL_OCP_INTERRUPT_STATUS),
  906. base + EMIF_LL_OCP_INTERRUPT_STATUS);
  907. }
  908. static void disable_and_clear_all_interrupts(struct emif_data *emif)
  909. {
  910. void __iomem *base = emif->base;
  911. /* Disable all interrupts */
  912. writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET),
  913. base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_CLEAR);
  914. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
  915. writel(readl(base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET),
  916. base + EMIF_LL_OCP_INTERRUPT_ENABLE_CLEAR);
  917. /* Clear all interrupts */
  918. clear_all_interrupts(emif);
  919. }
  920. static int __init_or_module setup_interrupts(struct emif_data *emif, u32 irq)
  921. {
  922. u32 interrupts, type;
  923. void __iomem *base = emif->base;
  924. type = emif->plat_data->device_info->type;
  925. clear_all_interrupts(emif);
  926. /* Enable interrupts for SYS interface */
  927. interrupts = EN_ERR_SYS_MASK;
  928. if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4)
  929. interrupts |= EN_TA_SYS_MASK;
  930. writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET);
  931. /* Enable interrupts for LL interface */
  932. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
  933. /* TA need not be enabled for LL */
  934. interrupts = EN_ERR_LL_MASK;
  935. writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET);
  936. }
  937. /* setup IRQ handlers */
  938. return devm_request_threaded_irq(emif->dev, irq,
  939. emif_interrupt_handler,
  940. emif_threaded_isr,
  941. 0, dev_name(emif->dev),
  942. emif);
  943. }
  944. static void __init_or_module emif_onetime_settings(struct emif_data *emif)
  945. {
  946. u32 pwr_mgmt_ctrl, zq, temp_alert_cfg;
  947. void __iomem *base = emif->base;
  948. const struct lpddr2_addressing *addressing;
  949. const struct ddr_device_info *device_info;
  950. device_info = emif->plat_data->device_info;
  951. addressing = get_addressing_table(device_info);
  952. /*
  953. * Init power management settings
  954. * We don't know the frequency yet. Use a high frequency
  955. * value for a conservative timeout setting
  956. */
  957. pwr_mgmt_ctrl = get_pwr_mgmt_ctrl(1000000000, emif,
  958. emif->plat_data->ip_rev);
  959. emif->lpmode = (pwr_mgmt_ctrl & LP_MODE_MASK) >> LP_MODE_SHIFT;
  960. writel(pwr_mgmt_ctrl, base + EMIF_POWER_MANAGEMENT_CONTROL);
  961. /* Init ZQ calibration settings */
  962. zq = get_zq_config_reg(addressing, device_info->cs1_used,
  963. device_info->cal_resistors_per_cs);
  964. writel(zq, base + EMIF_SDRAM_OUTPUT_IMPEDANCE_CALIBRATION_CONFIG);
  965. /* Check temperature level temperature level*/
  966. get_temperature_level(emif);
  967. if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN)
  968. dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
  969. /* Init temperature polling */
  970. temp_alert_cfg = get_temp_alert_config(addressing,
  971. emif->plat_data->custom_configs, device_info->cs1_used,
  972. device_info->io_width, get_emif_bus_width(emif));
  973. writel(temp_alert_cfg, base + EMIF_TEMPERATURE_ALERT_CONFIG);
  974. /*
  975. * Program external PHY control registers that are not frequency
  976. * dependent
  977. */
  978. if (emif->plat_data->phy_type != EMIF_PHY_TYPE_INTELLIPHY)
  979. return;
  980. writel(EMIF_EXT_PHY_CTRL_1_VAL, base + EMIF_EXT_PHY_CTRL_1_SHDW);
  981. writel(EMIF_EXT_PHY_CTRL_5_VAL, base + EMIF_EXT_PHY_CTRL_5_SHDW);
  982. writel(EMIF_EXT_PHY_CTRL_6_VAL, base + EMIF_EXT_PHY_CTRL_6_SHDW);
  983. writel(EMIF_EXT_PHY_CTRL_7_VAL, base + EMIF_EXT_PHY_CTRL_7_SHDW);
  984. writel(EMIF_EXT_PHY_CTRL_8_VAL, base + EMIF_EXT_PHY_CTRL_8_SHDW);
  985. writel(EMIF_EXT_PHY_CTRL_9_VAL, base + EMIF_EXT_PHY_CTRL_9_SHDW);
  986. writel(EMIF_EXT_PHY_CTRL_10_VAL, base + EMIF_EXT_PHY_CTRL_10_SHDW);
  987. writel(EMIF_EXT_PHY_CTRL_11_VAL, base + EMIF_EXT_PHY_CTRL_11_SHDW);
  988. writel(EMIF_EXT_PHY_CTRL_12_VAL, base + EMIF_EXT_PHY_CTRL_12_SHDW);
  989. writel(EMIF_EXT_PHY_CTRL_13_VAL, base + EMIF_EXT_PHY_CTRL_13_SHDW);
  990. writel(EMIF_EXT_PHY_CTRL_14_VAL, base + EMIF_EXT_PHY_CTRL_14_SHDW);
  991. writel(EMIF_EXT_PHY_CTRL_15_VAL, base + EMIF_EXT_PHY_CTRL_15_SHDW);
  992. writel(EMIF_EXT_PHY_CTRL_16_VAL, base + EMIF_EXT_PHY_CTRL_16_SHDW);
  993. writel(EMIF_EXT_PHY_CTRL_17_VAL, base + EMIF_EXT_PHY_CTRL_17_SHDW);
  994. writel(EMIF_EXT_PHY_CTRL_18_VAL, base + EMIF_EXT_PHY_CTRL_18_SHDW);
  995. writel(EMIF_EXT_PHY_CTRL_19_VAL, base + EMIF_EXT_PHY_CTRL_19_SHDW);
  996. writel(EMIF_EXT_PHY_CTRL_20_VAL, base + EMIF_EXT_PHY_CTRL_20_SHDW);
  997. writel(EMIF_EXT_PHY_CTRL_21_VAL, base + EMIF_EXT_PHY_CTRL_21_SHDW);
  998. writel(EMIF_EXT_PHY_CTRL_22_VAL, base + EMIF_EXT_PHY_CTRL_22_SHDW);
  999. writel(EMIF_EXT_PHY_CTRL_23_VAL, base + EMIF_EXT_PHY_CTRL_23_SHDW);
  1000. writel(EMIF_EXT_PHY_CTRL_24_VAL, base + EMIF_EXT_PHY_CTRL_24_SHDW);
  1001. }
  1002. static void get_default_timings(struct emif_data *emif)
  1003. {
  1004. struct emif_platform_data *pd = emif->plat_data;
  1005. pd->timings = lpddr2_jedec_timings;
  1006. pd->timings_arr_size = ARRAY_SIZE(lpddr2_jedec_timings);
  1007. dev_warn(emif->dev, "%s: using default timings\n", __func__);
  1008. }
  1009. static int is_dev_data_valid(u32 type, u32 density, u32 io_width, u32 phy_type,
  1010. u32 ip_rev, struct device *dev)
  1011. {
  1012. int valid;
  1013. valid = (type == DDR_TYPE_LPDDR2_S4 ||
  1014. type == DDR_TYPE_LPDDR2_S2)
  1015. && (density >= DDR_DENSITY_64Mb
  1016. && density <= DDR_DENSITY_8Gb)
  1017. && (io_width >= DDR_IO_WIDTH_8
  1018. && io_width <= DDR_IO_WIDTH_32);
  1019. /* Combinations of EMIF and PHY revisions that we support today */
  1020. switch (ip_rev) {
  1021. case EMIF_4D:
  1022. valid = valid && (phy_type == EMIF_PHY_TYPE_ATTILAPHY);
  1023. break;
  1024. case EMIF_4D5:
  1025. valid = valid && (phy_type == EMIF_PHY_TYPE_INTELLIPHY);
  1026. break;
  1027. default:
  1028. valid = 0;
  1029. }
  1030. if (!valid)
  1031. dev_err(dev, "%s: invalid DDR details\n", __func__);
  1032. return valid;
  1033. }
  1034. static int is_custom_config_valid(struct emif_custom_configs *cust_cfgs,
  1035. struct device *dev)
  1036. {
  1037. int valid = 1;
  1038. if ((cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE) &&
  1039. (cust_cfgs->lpmode != EMIF_LP_MODE_DISABLE))
  1040. valid = cust_cfgs->lpmode_freq_threshold &&
  1041. cust_cfgs->lpmode_timeout_performance &&
  1042. cust_cfgs->lpmode_timeout_power;
  1043. if (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL)
  1044. valid = valid && cust_cfgs->temp_alert_poll_interval_ms;
  1045. if (!valid)
  1046. dev_warn(dev, "%s: invalid custom configs\n", __func__);
  1047. return valid;
  1048. }
  1049. #if defined(CONFIG_OF)
  1050. static void __init_or_module of_get_custom_configs(struct device_node *np_emif,
  1051. struct emif_data *emif)
  1052. {
  1053. struct emif_custom_configs *cust_cfgs = NULL;
  1054. int len;
  1055. const __be32 *lpmode, *poll_intvl;
  1056. lpmode = of_get_property(np_emif, "low-power-mode", &len);
  1057. poll_intvl = of_get_property(np_emif, "temp-alert-poll-interval", &len);
  1058. if (lpmode || poll_intvl)
  1059. cust_cfgs = devm_kzalloc(emif->dev, sizeof(*cust_cfgs),
  1060. GFP_KERNEL);
  1061. if (!cust_cfgs)
  1062. return;
  1063. if (lpmode) {
  1064. cust_cfgs->mask |= EMIF_CUSTOM_CONFIG_LPMODE;
  1065. cust_cfgs->lpmode = be32_to_cpup(lpmode);
  1066. of_property_read_u32(np_emif,
  1067. "low-power-mode-timeout-performance",
  1068. &cust_cfgs->lpmode_timeout_performance);
  1069. of_property_read_u32(np_emif,
  1070. "low-power-mode-timeout-power",
  1071. &cust_cfgs->lpmode_timeout_power);
  1072. of_property_read_u32(np_emif,
  1073. "low-power-mode-freq-threshold",
  1074. &cust_cfgs->lpmode_freq_threshold);
  1075. }
  1076. if (poll_intvl) {
  1077. cust_cfgs->mask |=
  1078. EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL;
  1079. cust_cfgs->temp_alert_poll_interval_ms =
  1080. be32_to_cpup(poll_intvl);
  1081. }
  1082. if (of_find_property(np_emif, "extended-temp-part", &len))
  1083. cust_cfgs->mask |= EMIF_CUSTOM_CONFIG_EXTENDED_TEMP_PART;
  1084. if (!is_custom_config_valid(cust_cfgs, emif->dev)) {
  1085. devm_kfree(emif->dev, cust_cfgs);
  1086. return;
  1087. }
  1088. emif->plat_data->custom_configs = cust_cfgs;
  1089. }
  1090. static void __init_or_module of_get_ddr_info(struct device_node *np_emif,
  1091. struct device_node *np_ddr,
  1092. struct ddr_device_info *dev_info)
  1093. {
  1094. u32 density = 0, io_width = 0;
  1095. int len;
  1096. if (of_find_property(np_emif, "cs1-used", &len))
  1097. dev_info->cs1_used = true;
  1098. if (of_find_property(np_emif, "cal-resistor-per-cs", &len))
  1099. dev_info->cal_resistors_per_cs = true;
  1100. if (of_device_is_compatible(np_ddr , "jedec,lpddr2-s4"))
  1101. dev_info->type = DDR_TYPE_LPDDR2_S4;
  1102. else if (of_device_is_compatible(np_ddr , "jedec,lpddr2-s2"))
  1103. dev_info->type = DDR_TYPE_LPDDR2_S2;
  1104. of_property_read_u32(np_ddr, "density", &density);
  1105. of_property_read_u32(np_ddr, "io-width", &io_width);
  1106. /* Convert from density in Mb to the density encoding in jedc_ddr.h */
  1107. if (density & (density - 1))
  1108. dev_info->density = 0;
  1109. else
  1110. dev_info->density = __fls(density) - 5;
  1111. /* Convert from io_width in bits to io_width encoding in jedc_ddr.h */
  1112. if (io_width & (io_width - 1))
  1113. dev_info->io_width = 0;
  1114. else
  1115. dev_info->io_width = __fls(io_width) - 1;
  1116. }
  1117. static struct emif_data * __init_or_module of_get_memory_device_details(
  1118. struct device_node *np_emif, struct device *dev)
  1119. {
  1120. struct emif_data *emif = NULL;
  1121. struct ddr_device_info *dev_info = NULL;
  1122. struct emif_platform_data *pd = NULL;
  1123. struct device_node *np_ddr;
  1124. int len;
  1125. np_ddr = of_parse_phandle(np_emif, "device-handle", 0);
  1126. if (!np_ddr)
  1127. goto error;
  1128. emif = devm_kzalloc(dev, sizeof(struct emif_data), GFP_KERNEL);
  1129. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  1130. dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
  1131. if (!emif || !pd || !dev_info) {
  1132. dev_err(dev, "%s: Out of memory!!\n",
  1133. __func__);
  1134. goto error;
  1135. }
  1136. emif->plat_data = pd;
  1137. pd->device_info = dev_info;
  1138. emif->dev = dev;
  1139. emif->np_ddr = np_ddr;
  1140. emif->temperature_level = SDRAM_TEMP_NOMINAL;
  1141. if (of_device_is_compatible(np_emif, "ti,emif-4d"))
  1142. emif->plat_data->ip_rev = EMIF_4D;
  1143. else if (of_device_is_compatible(np_emif, "ti,emif-4d5"))
  1144. emif->plat_data->ip_rev = EMIF_4D5;
  1145. of_property_read_u32(np_emif, "phy-type", &pd->phy_type);
  1146. if (of_find_property(np_emif, "hw-caps-ll-interface", &len))
  1147. pd->hw_caps |= EMIF_HW_CAPS_LL_INTERFACE;
  1148. of_get_ddr_info(np_emif, np_ddr, dev_info);
  1149. if (!is_dev_data_valid(pd->device_info->type, pd->device_info->density,
  1150. pd->device_info->io_width, pd->phy_type, pd->ip_rev,
  1151. emif->dev)) {
  1152. dev_err(dev, "%s: invalid device data!!\n", __func__);
  1153. goto error;
  1154. }
  1155. /*
  1156. * For EMIF instances other than EMIF1 see if the devices connected
  1157. * are exactly same as on EMIF1(which is typically the case). If so,
  1158. * mark it as a duplicate of EMIF1. This will save some memory and
  1159. * computation.
  1160. */
  1161. if (emif1 && emif1->np_ddr == np_ddr) {
  1162. emif->duplicate = true;
  1163. goto out;
  1164. } else if (emif1) {
  1165. dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
  1166. __func__);
  1167. }
  1168. of_get_custom_configs(np_emif, emif);
  1169. emif->plat_data->timings = of_get_ddr_timings(np_ddr, emif->dev,
  1170. emif->plat_data->device_info->type,
  1171. &emif->plat_data->timings_arr_size);
  1172. emif->plat_data->min_tck = of_get_min_tck(np_ddr, emif->dev);
  1173. goto out;
  1174. error:
  1175. return NULL;
  1176. out:
  1177. return emif;
  1178. }
  1179. #else
  1180. static struct emif_data * __init_or_module of_get_memory_device_details(
  1181. struct device_node *np_emif, struct device *dev)
  1182. {
  1183. return NULL;
  1184. }
  1185. #endif
  1186. static struct emif_data *__init_or_module get_device_details(
  1187. struct platform_device *pdev)
  1188. {
  1189. u32 size;
  1190. struct emif_data *emif = NULL;
  1191. struct ddr_device_info *dev_info;
  1192. struct emif_custom_configs *cust_cfgs;
  1193. struct emif_platform_data *pd;
  1194. struct device *dev;
  1195. void *temp;
  1196. pd = pdev->dev.platform_data;
  1197. dev = &pdev->dev;
  1198. if (!(pd && pd->device_info && is_dev_data_valid(pd->device_info->type,
  1199. pd->device_info->density, pd->device_info->io_width,
  1200. pd->phy_type, pd->ip_rev, dev))) {
  1201. dev_err(dev, "%s: invalid device data\n", __func__);
  1202. goto error;
  1203. }
  1204. emif = devm_kzalloc(dev, sizeof(*emif), GFP_KERNEL);
  1205. temp = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  1206. dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
  1207. if (!emif || !pd || !dev_info) {
  1208. dev_err(dev, "%s:%d: allocation error\n", __func__, __LINE__);
  1209. goto error;
  1210. }
  1211. memcpy(temp, pd, sizeof(*pd));
  1212. pd = temp;
  1213. memcpy(dev_info, pd->device_info, sizeof(*dev_info));
  1214. pd->device_info = dev_info;
  1215. emif->plat_data = pd;
  1216. emif->dev = dev;
  1217. emif->temperature_level = SDRAM_TEMP_NOMINAL;
  1218. /*
  1219. * For EMIF instances other than EMIF1 see if the devices connected
  1220. * are exactly same as on EMIF1(which is typically the case). If so,
  1221. * mark it as a duplicate of EMIF1 and skip copying timings data.
  1222. * This will save some memory and some computation later.
  1223. */
  1224. emif->duplicate = emif1 && (memcmp(dev_info,
  1225. emif1->plat_data->device_info,
  1226. sizeof(struct ddr_device_info)) == 0);
  1227. if (emif->duplicate) {
  1228. pd->timings = NULL;
  1229. pd->min_tck = NULL;
  1230. goto out;
  1231. } else if (emif1) {
  1232. dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
  1233. __func__);
  1234. }
  1235. /*
  1236. * Copy custom configs - ignore allocation error, if any, as
  1237. * custom_configs is not very critical
  1238. */
  1239. cust_cfgs = pd->custom_configs;
  1240. if (cust_cfgs && is_custom_config_valid(cust_cfgs, dev)) {
  1241. temp = devm_kzalloc(dev, sizeof(*cust_cfgs), GFP_KERNEL);
  1242. if (temp)
  1243. memcpy(temp, cust_cfgs, sizeof(*cust_cfgs));
  1244. else
  1245. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  1246. __LINE__);
  1247. pd->custom_configs = temp;
  1248. }
  1249. /*
  1250. * Copy timings and min-tck values from platform data. If it is not
  1251. * available or if memory allocation fails, use JEDEC defaults
  1252. */
  1253. size = sizeof(struct lpddr2_timings) * pd->timings_arr_size;
  1254. if (pd->timings) {
  1255. temp = devm_kzalloc(dev, size, GFP_KERNEL);
  1256. if (temp) {
  1257. memcpy(temp, pd->timings, size);
  1258. pd->timings = temp;
  1259. } else {
  1260. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  1261. __LINE__);
  1262. get_default_timings(emif);
  1263. }
  1264. } else {
  1265. get_default_timings(emif);
  1266. }
  1267. if (pd->min_tck) {
  1268. temp = devm_kzalloc(dev, sizeof(*pd->min_tck), GFP_KERNEL);
  1269. if (temp) {
  1270. memcpy(temp, pd->min_tck, sizeof(*pd->min_tck));
  1271. pd->min_tck = temp;
  1272. } else {
  1273. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  1274. __LINE__);
  1275. pd->min_tck = &lpddr2_jedec_min_tck;
  1276. }
  1277. } else {
  1278. pd->min_tck = &lpddr2_jedec_min_tck;
  1279. }
  1280. out:
  1281. return emif;
  1282. error:
  1283. return NULL;
  1284. }
  1285. static int __init_or_module emif_probe(struct platform_device *pdev)
  1286. {
  1287. struct emif_data *emif;
  1288. struct resource *res;
  1289. int irq;
  1290. if (pdev->dev.of_node)
  1291. emif = of_get_memory_device_details(pdev->dev.of_node, &pdev->dev);
  1292. else
  1293. emif = get_device_details(pdev);
  1294. if (!emif) {
  1295. pr_err("%s: error getting device data\n", __func__);
  1296. goto error;
  1297. }
  1298. list_add(&emif->node, &device_list);
  1299. emif->addressing = get_addressing_table(emif->plat_data->device_info);
  1300. /* Save pointers to each other in emif and device structures */
  1301. emif->dev = &pdev->dev;
  1302. platform_set_drvdata(pdev, emif);
  1303. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1304. emif->base = devm_ioremap_resource(emif->dev, res);
  1305. if (IS_ERR(emif->base))
  1306. goto error;
  1307. irq = platform_get_irq(pdev, 0);
  1308. if (irq < 0) {
  1309. dev_err(emif->dev, "%s: error getting IRQ resource - %d\n",
  1310. __func__, irq);
  1311. goto error;
  1312. }
  1313. emif_onetime_settings(emif);
  1314. emif_debugfs_init(emif);
  1315. disable_and_clear_all_interrupts(emif);
  1316. setup_interrupts(emif, irq);
  1317. /* One-time actions taken on probing the first device */
  1318. if (!emif1) {
  1319. emif1 = emif;
  1320. spin_lock_init(&emif_lock);
  1321. /*
  1322. * TODO: register notifiers for frequency and voltage
  1323. * change here once the respective frameworks are
  1324. * available
  1325. */
  1326. }
  1327. dev_info(&pdev->dev, "%s: device configured with addr = %p and IRQ%d\n",
  1328. __func__, emif->base, irq);
  1329. return 0;
  1330. error:
  1331. return -ENODEV;
  1332. }
  1333. static int __exit emif_remove(struct platform_device *pdev)
  1334. {
  1335. struct emif_data *emif = platform_get_drvdata(pdev);
  1336. emif_debugfs_exit(emif);
  1337. return 0;
  1338. }
  1339. static void emif_shutdown(struct platform_device *pdev)
  1340. {
  1341. struct emif_data *emif = platform_get_drvdata(pdev);
  1342. disable_and_clear_all_interrupts(emif);
  1343. }
  1344. static int get_emif_reg_values(struct emif_data *emif, u32 freq,
  1345. struct emif_regs *regs)
  1346. {
  1347. u32 cs1_used, ip_rev, phy_type;
  1348. u32 cl, type;
  1349. const struct lpddr2_timings *timings;
  1350. const struct lpddr2_min_tck *min_tck;
  1351. const struct ddr_device_info *device_info;
  1352. const struct lpddr2_addressing *addressing;
  1353. struct emif_data *emif_for_calc;
  1354. struct device *dev;
  1355. const struct emif_custom_configs *custom_configs;
  1356. dev = emif->dev;
  1357. /*
  1358. * If the devices on this EMIF instance is duplicate of EMIF1,
  1359. * use EMIF1 details for the calculation
  1360. */
  1361. emif_for_calc = emif->duplicate ? emif1 : emif;
  1362. timings = get_timings_table(emif_for_calc, freq);
  1363. addressing = emif_for_calc->addressing;
  1364. if (!timings || !addressing) {
  1365. dev_err(dev, "%s: not enough data available for %dHz",
  1366. __func__, freq);
  1367. return -1;
  1368. }
  1369. device_info = emif_for_calc->plat_data->device_info;
  1370. type = device_info->type;
  1371. cs1_used = device_info->cs1_used;
  1372. ip_rev = emif_for_calc->plat_data->ip_rev;
  1373. phy_type = emif_for_calc->plat_data->phy_type;
  1374. min_tck = emif_for_calc->plat_data->min_tck;
  1375. custom_configs = emif_for_calc->plat_data->custom_configs;
  1376. set_ddr_clk_period(freq);
  1377. regs->ref_ctrl_shdw = get_sdram_ref_ctrl_shdw(freq, addressing);
  1378. regs->sdram_tim1_shdw = get_sdram_tim_1_shdw(timings, min_tck,
  1379. addressing);
  1380. regs->sdram_tim2_shdw = get_sdram_tim_2_shdw(timings, min_tck,
  1381. addressing, type);
  1382. regs->sdram_tim3_shdw = get_sdram_tim_3_shdw(timings, min_tck,
  1383. addressing, type, ip_rev, EMIF_NORMAL_TIMINGS);
  1384. cl = get_cl(emif);
  1385. if (phy_type == EMIF_PHY_TYPE_ATTILAPHY && ip_rev == EMIF_4D) {
  1386. regs->phy_ctrl_1_shdw = get_ddr_phy_ctrl_1_attilaphy_4d(
  1387. timings, freq, cl);
  1388. } else if (phy_type == EMIF_PHY_TYPE_INTELLIPHY && ip_rev == EMIF_4D5) {
  1389. regs->phy_ctrl_1_shdw = get_phy_ctrl_1_intelliphy_4d5(freq, cl);
  1390. regs->ext_phy_ctrl_2_shdw = get_ext_phy_ctrl_2_intelliphy_4d5();
  1391. regs->ext_phy_ctrl_3_shdw = get_ext_phy_ctrl_3_intelliphy_4d5();
  1392. regs->ext_phy_ctrl_4_shdw = get_ext_phy_ctrl_4_intelliphy_4d5();
  1393. } else {
  1394. return -1;
  1395. }
  1396. /* Only timeout values in pwr_mgmt_ctrl_shdw register */
  1397. regs->pwr_mgmt_ctrl_shdw =
  1398. get_pwr_mgmt_ctrl(freq, emif_for_calc, ip_rev) &
  1399. (CS_TIM_MASK | SR_TIM_MASK | PD_TIM_MASK);
  1400. if (ip_rev & EMIF_4D) {
  1401. regs->read_idle_ctrl_shdw_normal =
  1402. get_read_idle_ctrl_shdw(DDR_VOLTAGE_STABLE);
  1403. regs->read_idle_ctrl_shdw_volt_ramp =
  1404. get_read_idle_ctrl_shdw(DDR_VOLTAGE_RAMPING);
  1405. } else if (ip_rev & EMIF_4D5) {
  1406. regs->dll_calib_ctrl_shdw_normal =
  1407. get_dll_calib_ctrl_shdw(DDR_VOLTAGE_STABLE);
  1408. regs->dll_calib_ctrl_shdw_volt_ramp =
  1409. get_dll_calib_ctrl_shdw(DDR_VOLTAGE_RAMPING);
  1410. }
  1411. if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4) {
  1412. regs->ref_ctrl_shdw_derated = get_sdram_ref_ctrl_shdw(freq / 4,
  1413. addressing);
  1414. regs->sdram_tim1_shdw_derated =
  1415. get_sdram_tim_1_shdw_derated(timings, min_tck,
  1416. addressing);
  1417. regs->sdram_tim3_shdw_derated = get_sdram_tim_3_shdw(timings,
  1418. min_tck, addressing, type, ip_rev,
  1419. EMIF_DERATED_TIMINGS);
  1420. }
  1421. regs->freq = freq;
  1422. return 0;
  1423. }
  1424. /*
  1425. * get_regs() - gets the cached emif_regs structure for a given EMIF instance
  1426. * given frequency(freq):
  1427. *
  1428. * As an optimisation, every EMIF instance other than EMIF1 shares the
  1429. * register cache with EMIF1 if the devices connected on this instance
  1430. * are same as that on EMIF1(indicated by the duplicate flag)
  1431. *
  1432. * If we do not have an entry corresponding to the frequency given, we
  1433. * allocate a new entry and calculate the values
  1434. *
  1435. * Upon finding the right reg dump, save it in curr_regs. It can be
  1436. * directly used for thermal de-rating and voltage ramping changes.
  1437. */
  1438. static struct emif_regs *get_regs(struct emif_data *emif, u32 freq)
  1439. {
  1440. int i;
  1441. struct emif_regs **regs_cache;
  1442. struct emif_regs *regs = NULL;
  1443. struct device *dev;
  1444. dev = emif->dev;
  1445. if (emif->curr_regs && emif->curr_regs->freq == freq) {
  1446. dev_dbg(dev, "%s: using curr_regs - %u Hz", __func__, freq);
  1447. return emif->curr_regs;
  1448. }
  1449. if (emif->duplicate)
  1450. regs_cache = emif1->regs_cache;
  1451. else
  1452. regs_cache = emif->regs_cache;
  1453. for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) {
  1454. if (regs_cache[i]->freq == freq) {
  1455. regs = regs_cache[i];
  1456. dev_dbg(dev,
  1457. "%s: reg dump found in reg cache for %u Hz\n",
  1458. __func__, freq);
  1459. break;
  1460. }
  1461. }
  1462. /*
  1463. * If we don't have an entry for this frequency in the cache create one
  1464. * and calculate the values
  1465. */
  1466. if (!regs) {
  1467. regs = devm_kzalloc(emif->dev, sizeof(*regs), GFP_ATOMIC);
  1468. if (!regs)
  1469. return NULL;
  1470. if (get_emif_reg_values(emif, freq, regs)) {
  1471. devm_kfree(emif->dev, regs);
  1472. return NULL;
  1473. }
  1474. /*
  1475. * Now look for an un-used entry in the cache and save the
  1476. * newly created struct. If there are no free entries
  1477. * over-write the last entry
  1478. */
  1479. for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++)
  1480. ;
  1481. if (i >= EMIF_MAX_NUM_FREQUENCIES) {
  1482. dev_warn(dev, "%s: regs_cache full - reusing a slot!!\n",
  1483. __func__);
  1484. i = EMIF_MAX_NUM_FREQUENCIES - 1;
  1485. devm_kfree(emif->dev, regs_cache[i]);
  1486. }
  1487. regs_cache[i] = regs;
  1488. }
  1489. return regs;
  1490. }
  1491. static void do_volt_notify_handling(struct emif_data *emif, u32 volt_state)
  1492. {
  1493. dev_dbg(emif->dev, "%s: voltage notification : %d", __func__,
  1494. volt_state);
  1495. if (!emif->curr_regs) {
  1496. dev_err(emif->dev,
  1497. "%s: volt-notify before registers are ready: %d\n",
  1498. __func__, volt_state);
  1499. return;
  1500. }
  1501. setup_volt_sensitive_regs(emif, emif->curr_regs, volt_state);
  1502. }
  1503. /*
  1504. * TODO: voltage notify handling should be hooked up to
  1505. * regulator framework as soon as the necessary support
  1506. * is available in mainline kernel. This function is un-used
  1507. * right now.
  1508. */
  1509. static void __attribute__((unused)) volt_notify_handling(u32 volt_state)
  1510. {
  1511. struct emif_data *emif;
  1512. spin_lock_irqsave(&emif_lock, irq_state);
  1513. list_for_each_entry(emif, &device_list, node)
  1514. do_volt_notify_handling(emif, volt_state);
  1515. do_freq_update();
  1516. spin_unlock_irqrestore(&emif_lock, irq_state);
  1517. }
  1518. static void do_freq_pre_notify_handling(struct emif_data *emif, u32 new_freq)
  1519. {
  1520. struct emif_regs *regs;
  1521. regs = get_regs(emif, new_freq);
  1522. if (!regs)
  1523. return;
  1524. emif->curr_regs = regs;
  1525. /*
  1526. * Update the shadow registers:
  1527. * Temperature and voltage-ramp sensitive settings are also configured
  1528. * in terms of DDR cycles. So, we need to update them too when there
  1529. * is a freq change
  1530. */
  1531. dev_dbg(emif->dev, "%s: setting up shadow registers for %uHz",
  1532. __func__, new_freq);
  1533. setup_registers(emif, regs);
  1534. setup_temperature_sensitive_regs(emif, regs);
  1535. setup_volt_sensitive_regs(emif, regs, DDR_VOLTAGE_STABLE);
  1536. /*
  1537. * Part of workaround for errata i728. See do_freq_update()
  1538. * for more details
  1539. */
  1540. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  1541. set_lpmode(emif, EMIF_LP_MODE_DISABLE);
  1542. }
  1543. /*
  1544. * TODO: frequency notify handling should be hooked up to
  1545. * clock framework as soon as the necessary support is
  1546. * available in mainline kernel. This function is un-used
  1547. * right now.
  1548. */
  1549. static void __attribute__((unused)) freq_pre_notify_handling(u32 new_freq)
  1550. {
  1551. struct emif_data *emif;
  1552. /*
  1553. * NOTE: we are taking the spin-lock here and releases it
  1554. * only in post-notifier. This doesn't look good and
  1555. * Sparse complains about it, but this seems to be
  1556. * un-avoidable. We need to lock a sequence of events
  1557. * that is split between EMIF and clock framework.
  1558. *
  1559. * 1. EMIF driver updates EMIF timings in shadow registers in the
  1560. * frequency pre-notify callback from clock framework
  1561. * 2. clock framework sets up the registers for the new frequency
  1562. * 3. clock framework initiates a hw-sequence that updates
  1563. * the frequency EMIF timings synchronously.
  1564. *
  1565. * All these 3 steps should be performed as an atomic operation
  1566. * vis-a-vis similar sequence in the EMIF interrupt handler
  1567. * for temperature events. Otherwise, there could be race
  1568. * conditions that could result in incorrect EMIF timings for
  1569. * a given frequency
  1570. */
  1571. spin_lock_irqsave(&emif_lock, irq_state);
  1572. list_for_each_entry(emif, &device_list, node)
  1573. do_freq_pre_notify_handling(emif, new_freq);
  1574. }
  1575. static void do_freq_post_notify_handling(struct emif_data *emif)
  1576. {
  1577. /*
  1578. * Part of workaround for errata i728. See do_freq_update()
  1579. * for more details
  1580. */
  1581. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  1582. set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH);
  1583. }
  1584. /*
  1585. * TODO: frequency notify handling should be hooked up to
  1586. * clock framework as soon as the necessary support is
  1587. * available in mainline kernel. This function is un-used
  1588. * right now.
  1589. */
  1590. static void __attribute__((unused)) freq_post_notify_handling(void)
  1591. {
  1592. struct emif_data *emif;
  1593. list_for_each_entry(emif, &device_list, node)
  1594. do_freq_post_notify_handling(emif);
  1595. /*
  1596. * Lock is done in pre-notify handler. See freq_pre_notify_handling()
  1597. * for more details
  1598. */
  1599. spin_unlock_irqrestore(&emif_lock, irq_state);
  1600. }
  1601. #if defined(CONFIG_OF)
  1602. static const struct of_device_id emif_of_match[] = {
  1603. { .compatible = "ti,emif-4d" },
  1604. { .compatible = "ti,emif-4d5" },
  1605. {},
  1606. };
  1607. MODULE_DEVICE_TABLE(of, emif_of_match);
  1608. #endif
  1609. static struct platform_driver emif_driver = {
  1610. .remove = __exit_p(emif_remove),
  1611. .shutdown = emif_shutdown,
  1612. .driver = {
  1613. .name = "emif",
  1614. .of_match_table = of_match_ptr(emif_of_match),
  1615. },
  1616. };
  1617. module_platform_driver_probe(emif_driver, emif_probe);
  1618. MODULE_DESCRIPTION("TI EMIF SDRAM Controller Driver");
  1619. MODULE_LICENSE("GPL");
  1620. MODULE_ALIAS("platform:emif");
  1621. MODULE_AUTHOR("Texas Instruments Inc");