emif.c 52 KB

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