emif.c 53 KB

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