emif.c 52 KB

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