emif.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  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. /*
  599. * The value to be set in register is "log2(timeout) - 3"
  600. * if timeout < 16 load 0 in register
  601. * if timeout is not a power of 2, round to next highest power of 2
  602. */
  603. if (timeout < 16) {
  604. timeout = 0;
  605. } else {
  606. if (timeout & (timeout - 1))
  607. timeout <<= 1;
  608. timeout = __fls(timeout) - 3;
  609. }
  610. switch (lpmode) {
  611. case EMIF_LP_MODE_CLOCK_STOP:
  612. pwr_mgmt_ctrl = (timeout << CS_TIM_SHIFT) |
  613. SR_TIM_MASK | PD_TIM_MASK;
  614. break;
  615. case EMIF_LP_MODE_SELF_REFRESH:
  616. /* Workaround for errata i735 */
  617. if (timeout < 6)
  618. timeout = 6;
  619. pwr_mgmt_ctrl = (timeout << SR_TIM_SHIFT) |
  620. CS_TIM_MASK | PD_TIM_MASK;
  621. break;
  622. case EMIF_LP_MODE_PWR_DN:
  623. pwr_mgmt_ctrl = (timeout << PD_TIM_SHIFT) |
  624. CS_TIM_MASK | SR_TIM_MASK;
  625. break;
  626. case EMIF_LP_MODE_DISABLE:
  627. default:
  628. pwr_mgmt_ctrl = CS_TIM_MASK |
  629. PD_TIM_MASK | SR_TIM_MASK;
  630. }
  631. /* No CS_TIM in EMIF_4D5 */
  632. if (ip_rev == EMIF_4D5)
  633. pwr_mgmt_ctrl &= ~CS_TIM_MASK;
  634. pwr_mgmt_ctrl |= lpmode << LP_MODE_SHIFT;
  635. return pwr_mgmt_ctrl;
  636. }
  637. /*
  638. * Get the temperature level of the EMIF instance:
  639. * Reads the MR4 register of attached SDRAM parts to find out the temperature
  640. * level. If there are two parts attached(one on each CS), then the temperature
  641. * level for the EMIF instance is the higher of the two temperatures.
  642. */
  643. static void get_temperature_level(struct emif_data *emif)
  644. {
  645. u32 temp, temperature_level;
  646. void __iomem *base;
  647. base = emif->base;
  648. /* Read mode register 4 */
  649. writel(DDR_MR4, base + EMIF_LPDDR2_MODE_REG_CONFIG);
  650. temperature_level = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
  651. temperature_level = (temperature_level & MR4_SDRAM_REF_RATE_MASK) >>
  652. MR4_SDRAM_REF_RATE_SHIFT;
  653. if (emif->plat_data->device_info->cs1_used) {
  654. writel(DDR_MR4 | CS_MASK, base + EMIF_LPDDR2_MODE_REG_CONFIG);
  655. temp = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
  656. temp = (temp & MR4_SDRAM_REF_RATE_MASK)
  657. >> MR4_SDRAM_REF_RATE_SHIFT;
  658. temperature_level = max(temp, temperature_level);
  659. }
  660. /* treat everything less than nominal(3) in MR4 as nominal */
  661. if (unlikely(temperature_level < SDRAM_TEMP_NOMINAL))
  662. temperature_level = SDRAM_TEMP_NOMINAL;
  663. /* if we get reserved value in MR4 persist with the existing value */
  664. if (likely(temperature_level != SDRAM_TEMP_RESERVED_4))
  665. emif->temperature_level = temperature_level;
  666. }
  667. /*
  668. * Program EMIF shadow registers that are not dependent on temperature
  669. * or voltage
  670. */
  671. static void setup_registers(struct emif_data *emif, struct emif_regs *regs)
  672. {
  673. void __iomem *base = emif->base;
  674. writel(regs->sdram_tim2_shdw, base + EMIF_SDRAM_TIMING_2_SHDW);
  675. writel(regs->phy_ctrl_1_shdw, base + EMIF_DDR_PHY_CTRL_1_SHDW);
  676. writel(regs->pwr_mgmt_ctrl_shdw,
  677. base + EMIF_POWER_MANAGEMENT_CTRL_SHDW);
  678. /* Settings specific for EMIF4D5 */
  679. if (emif->plat_data->ip_rev != EMIF_4D5)
  680. return;
  681. writel(regs->ext_phy_ctrl_2_shdw, base + EMIF_EXT_PHY_CTRL_2_SHDW);
  682. writel(regs->ext_phy_ctrl_3_shdw, base + EMIF_EXT_PHY_CTRL_3_SHDW);
  683. writel(regs->ext_phy_ctrl_4_shdw, base + EMIF_EXT_PHY_CTRL_4_SHDW);
  684. }
  685. /*
  686. * When voltage ramps dll calibration and forced read idle should
  687. * happen more often
  688. */
  689. static void setup_volt_sensitive_regs(struct emif_data *emif,
  690. struct emif_regs *regs, u32 volt_state)
  691. {
  692. u32 calib_ctrl;
  693. void __iomem *base = emif->base;
  694. /*
  695. * EMIF_READ_IDLE_CTRL in EMIF4D refers to the same register as
  696. * EMIF_DLL_CALIB_CTRL in EMIF4D5 and dll_calib_ctrl_shadow_*
  697. * is an alias of the respective read_idle_ctrl_shdw_* (members of
  698. * a union). So, the below code takes care of both cases
  699. */
  700. if (volt_state == DDR_VOLTAGE_RAMPING)
  701. calib_ctrl = regs->dll_calib_ctrl_shdw_volt_ramp;
  702. else
  703. calib_ctrl = regs->dll_calib_ctrl_shdw_normal;
  704. writel(calib_ctrl, base + EMIF_DLL_CALIB_CTRL_SHDW);
  705. }
  706. /*
  707. * setup_temperature_sensitive_regs() - set the timings for temperature
  708. * sensitive registers. This happens once at initialisation time based
  709. * on the temperature at boot time and subsequently based on the temperature
  710. * alert interrupt. Temperature alert can happen when the temperature
  711. * increases or drops. So this function can have the effect of either
  712. * derating the timings or going back to nominal values.
  713. */
  714. static void setup_temperature_sensitive_regs(struct emif_data *emif,
  715. struct emif_regs *regs)
  716. {
  717. u32 tim1, tim3, ref_ctrl, type;
  718. void __iomem *base = emif->base;
  719. u32 temperature;
  720. type = emif->plat_data->device_info->type;
  721. tim1 = regs->sdram_tim1_shdw;
  722. tim3 = regs->sdram_tim3_shdw;
  723. ref_ctrl = regs->ref_ctrl_shdw;
  724. /* No de-rating for non-lpddr2 devices */
  725. if (type != DDR_TYPE_LPDDR2_S2 && type != DDR_TYPE_LPDDR2_S4)
  726. goto out;
  727. temperature = emif->temperature_level;
  728. if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH) {
  729. ref_ctrl = regs->ref_ctrl_shdw_derated;
  730. } else if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH_AND_TIMINGS) {
  731. tim1 = regs->sdram_tim1_shdw_derated;
  732. tim3 = regs->sdram_tim3_shdw_derated;
  733. ref_ctrl = regs->ref_ctrl_shdw_derated;
  734. }
  735. out:
  736. writel(tim1, base + EMIF_SDRAM_TIMING_1_SHDW);
  737. writel(tim3, base + EMIF_SDRAM_TIMING_3_SHDW);
  738. writel(ref_ctrl, base + EMIF_SDRAM_REFRESH_CTRL_SHDW);
  739. }
  740. static irqreturn_t handle_temp_alert(void __iomem *base, struct emif_data *emif)
  741. {
  742. u32 old_temp_level;
  743. irqreturn_t ret = IRQ_HANDLED;
  744. spin_lock_irqsave(&emif_lock, irq_state);
  745. old_temp_level = emif->temperature_level;
  746. get_temperature_level(emif);
  747. if (unlikely(emif->temperature_level == old_temp_level)) {
  748. goto out;
  749. } else if (!emif->curr_regs) {
  750. dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
  751. goto out;
  752. }
  753. if (emif->temperature_level < old_temp_level ||
  754. emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
  755. /*
  756. * Temperature coming down - defer handling to thread OR
  757. * Temperature far too high - do kernel_power_off() from
  758. * thread context
  759. */
  760. ret = IRQ_WAKE_THREAD;
  761. } else {
  762. /* Temperature is going up - handle immediately */
  763. setup_temperature_sensitive_regs(emif, emif->curr_regs);
  764. do_freq_update();
  765. }
  766. out:
  767. spin_unlock_irqrestore(&emif_lock, irq_state);
  768. return ret;
  769. }
  770. static irqreturn_t emif_interrupt_handler(int irq, void *dev_id)
  771. {
  772. u32 interrupts;
  773. struct emif_data *emif = dev_id;
  774. void __iomem *base = emif->base;
  775. struct device *dev = emif->dev;
  776. irqreturn_t ret = IRQ_HANDLED;
  777. /* Save the status and clear it */
  778. interrupts = readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  779. writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  780. /*
  781. * Handle temperature alert
  782. * Temperature alert should be same for all ports
  783. * So, it's enough to process it only for one of the ports
  784. */
  785. if (interrupts & TA_SYS_MASK)
  786. ret = handle_temp_alert(base, emif);
  787. if (interrupts & ERR_SYS_MASK)
  788. dev_err(dev, "Access error from SYS port - %x\n", interrupts);
  789. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
  790. /* Save the status and clear it */
  791. interrupts = readl(base + EMIF_LL_OCP_INTERRUPT_STATUS);
  792. writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_STATUS);
  793. if (interrupts & ERR_LL_MASK)
  794. dev_err(dev, "Access error from LL port - %x\n",
  795. interrupts);
  796. }
  797. return ret;
  798. }
  799. static irqreturn_t emif_threaded_isr(int irq, void *dev_id)
  800. {
  801. struct emif_data *emif = dev_id;
  802. if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
  803. dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
  804. kernel_power_off();
  805. return IRQ_HANDLED;
  806. }
  807. spin_lock_irqsave(&emif_lock, irq_state);
  808. if (emif->curr_regs) {
  809. setup_temperature_sensitive_regs(emif, emif->curr_regs);
  810. do_freq_update();
  811. } else {
  812. dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
  813. }
  814. spin_unlock_irqrestore(&emif_lock, irq_state);
  815. return IRQ_HANDLED;
  816. }
  817. static void clear_all_interrupts(struct emif_data *emif)
  818. {
  819. void __iomem *base = emif->base;
  820. writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS),
  821. base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  822. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
  823. writel(readl(base + EMIF_LL_OCP_INTERRUPT_STATUS),
  824. base + EMIF_LL_OCP_INTERRUPT_STATUS);
  825. }
  826. static void disable_and_clear_all_interrupts(struct emif_data *emif)
  827. {
  828. void __iomem *base = emif->base;
  829. /* Disable all interrupts */
  830. writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET),
  831. base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_CLEAR);
  832. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
  833. writel(readl(base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET),
  834. base + EMIF_LL_OCP_INTERRUPT_ENABLE_CLEAR);
  835. /* Clear all interrupts */
  836. clear_all_interrupts(emif);
  837. }
  838. static int __init_or_module setup_interrupts(struct emif_data *emif, u32 irq)
  839. {
  840. u32 interrupts, type;
  841. void __iomem *base = emif->base;
  842. type = emif->plat_data->device_info->type;
  843. clear_all_interrupts(emif);
  844. /* Enable interrupts for SYS interface */
  845. interrupts = EN_ERR_SYS_MASK;
  846. if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4)
  847. interrupts |= EN_TA_SYS_MASK;
  848. writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET);
  849. /* Enable interrupts for LL interface */
  850. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
  851. /* TA need not be enabled for LL */
  852. interrupts = EN_ERR_LL_MASK;
  853. writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET);
  854. }
  855. /* setup IRQ handlers */
  856. return devm_request_threaded_irq(emif->dev, irq,
  857. emif_interrupt_handler,
  858. emif_threaded_isr,
  859. 0, dev_name(emif->dev),
  860. emif);
  861. }
  862. static void __init_or_module emif_onetime_settings(struct emif_data *emif)
  863. {
  864. u32 pwr_mgmt_ctrl, zq, temp_alert_cfg;
  865. void __iomem *base = emif->base;
  866. const struct lpddr2_addressing *addressing;
  867. const struct ddr_device_info *device_info;
  868. device_info = emif->plat_data->device_info;
  869. addressing = get_addressing_table(device_info);
  870. /*
  871. * Init power management settings
  872. * We don't know the frequency yet. Use a high frequency
  873. * value for a conservative timeout setting
  874. */
  875. pwr_mgmt_ctrl = get_pwr_mgmt_ctrl(1000000000, emif,
  876. emif->plat_data->ip_rev);
  877. emif->lpmode = (pwr_mgmt_ctrl & LP_MODE_MASK) >> LP_MODE_SHIFT;
  878. writel(pwr_mgmt_ctrl, base + EMIF_POWER_MANAGEMENT_CONTROL);
  879. /* Init ZQ calibration settings */
  880. zq = get_zq_config_reg(addressing, device_info->cs1_used,
  881. device_info->cal_resistors_per_cs);
  882. writel(zq, base + EMIF_SDRAM_OUTPUT_IMPEDANCE_CALIBRATION_CONFIG);
  883. /* Check temperature level temperature level*/
  884. get_temperature_level(emif);
  885. if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN)
  886. dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
  887. /* Init temperature polling */
  888. temp_alert_cfg = get_temp_alert_config(addressing,
  889. emif->plat_data->custom_configs, device_info->cs1_used,
  890. device_info->io_width, get_emif_bus_width(emif));
  891. writel(temp_alert_cfg, base + EMIF_TEMPERATURE_ALERT_CONFIG);
  892. /*
  893. * Program external PHY control registers that are not frequency
  894. * dependent
  895. */
  896. if (emif->plat_data->phy_type != EMIF_PHY_TYPE_INTELLIPHY)
  897. return;
  898. writel(EMIF_EXT_PHY_CTRL_1_VAL, base + EMIF_EXT_PHY_CTRL_1_SHDW);
  899. writel(EMIF_EXT_PHY_CTRL_5_VAL, base + EMIF_EXT_PHY_CTRL_5_SHDW);
  900. writel(EMIF_EXT_PHY_CTRL_6_VAL, base + EMIF_EXT_PHY_CTRL_6_SHDW);
  901. writel(EMIF_EXT_PHY_CTRL_7_VAL, base + EMIF_EXT_PHY_CTRL_7_SHDW);
  902. writel(EMIF_EXT_PHY_CTRL_8_VAL, base + EMIF_EXT_PHY_CTRL_8_SHDW);
  903. writel(EMIF_EXT_PHY_CTRL_9_VAL, base + EMIF_EXT_PHY_CTRL_9_SHDW);
  904. writel(EMIF_EXT_PHY_CTRL_10_VAL, base + EMIF_EXT_PHY_CTRL_10_SHDW);
  905. writel(EMIF_EXT_PHY_CTRL_11_VAL, base + EMIF_EXT_PHY_CTRL_11_SHDW);
  906. writel(EMIF_EXT_PHY_CTRL_12_VAL, base + EMIF_EXT_PHY_CTRL_12_SHDW);
  907. writel(EMIF_EXT_PHY_CTRL_13_VAL, base + EMIF_EXT_PHY_CTRL_13_SHDW);
  908. writel(EMIF_EXT_PHY_CTRL_14_VAL, base + EMIF_EXT_PHY_CTRL_14_SHDW);
  909. writel(EMIF_EXT_PHY_CTRL_15_VAL, base + EMIF_EXT_PHY_CTRL_15_SHDW);
  910. writel(EMIF_EXT_PHY_CTRL_16_VAL, base + EMIF_EXT_PHY_CTRL_16_SHDW);
  911. writel(EMIF_EXT_PHY_CTRL_17_VAL, base + EMIF_EXT_PHY_CTRL_17_SHDW);
  912. writel(EMIF_EXT_PHY_CTRL_18_VAL, base + EMIF_EXT_PHY_CTRL_18_SHDW);
  913. writel(EMIF_EXT_PHY_CTRL_19_VAL, base + EMIF_EXT_PHY_CTRL_19_SHDW);
  914. writel(EMIF_EXT_PHY_CTRL_20_VAL, base + EMIF_EXT_PHY_CTRL_20_SHDW);
  915. writel(EMIF_EXT_PHY_CTRL_21_VAL, base + EMIF_EXT_PHY_CTRL_21_SHDW);
  916. writel(EMIF_EXT_PHY_CTRL_22_VAL, base + EMIF_EXT_PHY_CTRL_22_SHDW);
  917. writel(EMIF_EXT_PHY_CTRL_23_VAL, base + EMIF_EXT_PHY_CTRL_23_SHDW);
  918. writel(EMIF_EXT_PHY_CTRL_24_VAL, base + EMIF_EXT_PHY_CTRL_24_SHDW);
  919. }
  920. static void get_default_timings(struct emif_data *emif)
  921. {
  922. struct emif_platform_data *pd = emif->plat_data;
  923. pd->timings = lpddr2_jedec_timings;
  924. pd->timings_arr_size = ARRAY_SIZE(lpddr2_jedec_timings);
  925. dev_warn(emif->dev, "%s: using default timings\n", __func__);
  926. }
  927. static int is_dev_data_valid(u32 type, u32 density, u32 io_width, u32 phy_type,
  928. u32 ip_rev, struct device *dev)
  929. {
  930. int valid;
  931. valid = (type == DDR_TYPE_LPDDR2_S4 ||
  932. type == DDR_TYPE_LPDDR2_S2)
  933. && (density >= DDR_DENSITY_64Mb
  934. && density <= DDR_DENSITY_8Gb)
  935. && (io_width >= DDR_IO_WIDTH_8
  936. && io_width <= DDR_IO_WIDTH_32);
  937. /* Combinations of EMIF and PHY revisions that we support today */
  938. switch (ip_rev) {
  939. case EMIF_4D:
  940. valid = valid && (phy_type == EMIF_PHY_TYPE_ATTILAPHY);
  941. break;
  942. case EMIF_4D5:
  943. valid = valid && (phy_type == EMIF_PHY_TYPE_INTELLIPHY);
  944. break;
  945. default:
  946. valid = 0;
  947. }
  948. if (!valid)
  949. dev_err(dev, "%s: invalid DDR details\n", __func__);
  950. return valid;
  951. }
  952. static int is_custom_config_valid(struct emif_custom_configs *cust_cfgs,
  953. struct device *dev)
  954. {
  955. int valid = 1;
  956. if ((cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE) &&
  957. (cust_cfgs->lpmode != EMIF_LP_MODE_DISABLE))
  958. valid = cust_cfgs->lpmode_freq_threshold &&
  959. cust_cfgs->lpmode_timeout_performance &&
  960. cust_cfgs->lpmode_timeout_power;
  961. if (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL)
  962. valid = valid && cust_cfgs->temp_alert_poll_interval_ms;
  963. if (!valid)
  964. dev_warn(dev, "%s: invalid custom configs\n", __func__);
  965. return valid;
  966. }
  967. #if defined(CONFIG_OF)
  968. static void __init_or_module of_get_custom_configs(struct device_node *np_emif,
  969. struct emif_data *emif)
  970. {
  971. struct emif_custom_configs *cust_cfgs = NULL;
  972. int len;
  973. const int *lpmode, *poll_intvl;
  974. lpmode = of_get_property(np_emif, "low-power-mode", &len);
  975. poll_intvl = of_get_property(np_emif, "temp-alert-poll-interval", &len);
  976. if (lpmode || poll_intvl)
  977. cust_cfgs = devm_kzalloc(emif->dev, sizeof(*cust_cfgs),
  978. GFP_KERNEL);
  979. if (!cust_cfgs)
  980. return;
  981. if (lpmode) {
  982. cust_cfgs->mask |= EMIF_CUSTOM_CONFIG_LPMODE;
  983. cust_cfgs->lpmode = *lpmode;
  984. of_property_read_u32(np_emif,
  985. "low-power-mode-timeout-performance",
  986. &cust_cfgs->lpmode_timeout_performance);
  987. of_property_read_u32(np_emif,
  988. "low-power-mode-timeout-power",
  989. &cust_cfgs->lpmode_timeout_power);
  990. of_property_read_u32(np_emif,
  991. "low-power-mode-freq-threshold",
  992. &cust_cfgs->lpmode_freq_threshold);
  993. }
  994. if (poll_intvl) {
  995. cust_cfgs->mask |=
  996. EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL;
  997. cust_cfgs->temp_alert_poll_interval_ms = *poll_intvl;
  998. }
  999. if (!is_custom_config_valid(cust_cfgs, emif->dev)) {
  1000. devm_kfree(emif->dev, cust_cfgs);
  1001. return;
  1002. }
  1003. emif->plat_data->custom_configs = cust_cfgs;
  1004. }
  1005. static void __init_or_module of_get_ddr_info(struct device_node *np_emif,
  1006. struct device_node *np_ddr,
  1007. struct ddr_device_info *dev_info)
  1008. {
  1009. u32 density = 0, io_width = 0;
  1010. int len;
  1011. if (of_find_property(np_emif, "cs1-used", &len))
  1012. dev_info->cs1_used = true;
  1013. if (of_find_property(np_emif, "cal-resistor-per-cs", &len))
  1014. dev_info->cal_resistors_per_cs = true;
  1015. if (of_device_is_compatible(np_ddr , "jedec,lpddr2-s4"))
  1016. dev_info->type = DDR_TYPE_LPDDR2_S4;
  1017. else if (of_device_is_compatible(np_ddr , "jedec,lpddr2-s2"))
  1018. dev_info->type = DDR_TYPE_LPDDR2_S2;
  1019. of_property_read_u32(np_ddr, "density", &density);
  1020. of_property_read_u32(np_ddr, "io-width", &io_width);
  1021. /* Convert from density in Mb to the density encoding in jedc_ddr.h */
  1022. if (density & (density - 1))
  1023. dev_info->density = 0;
  1024. else
  1025. dev_info->density = __fls(density) - 5;
  1026. /* Convert from io_width in bits to io_width encoding in jedc_ddr.h */
  1027. if (io_width & (io_width - 1))
  1028. dev_info->io_width = 0;
  1029. else
  1030. dev_info->io_width = __fls(io_width) - 1;
  1031. }
  1032. static struct emif_data * __init_or_module of_get_memory_device_details(
  1033. struct device_node *np_emif, struct device *dev)
  1034. {
  1035. struct emif_data *emif = NULL;
  1036. struct ddr_device_info *dev_info = NULL;
  1037. struct emif_platform_data *pd = NULL;
  1038. struct device_node *np_ddr;
  1039. int len;
  1040. np_ddr = of_parse_phandle(np_emif, "device-handle", 0);
  1041. if (!np_ddr)
  1042. goto error;
  1043. emif = devm_kzalloc(dev, sizeof(struct emif_data), GFP_KERNEL);
  1044. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  1045. dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
  1046. if (!emif || !pd || !dev_info) {
  1047. dev_err(dev, "%s: Out of memory!!\n",
  1048. __func__);
  1049. goto error;
  1050. }
  1051. emif->plat_data = pd;
  1052. pd->device_info = dev_info;
  1053. emif->dev = dev;
  1054. emif->np_ddr = np_ddr;
  1055. emif->temperature_level = SDRAM_TEMP_NOMINAL;
  1056. if (of_device_is_compatible(np_emif, "ti,emif-4d"))
  1057. emif->plat_data->ip_rev = EMIF_4D;
  1058. else if (of_device_is_compatible(np_emif, "ti,emif-4d5"))
  1059. emif->plat_data->ip_rev = EMIF_4D5;
  1060. of_property_read_u32(np_emif, "phy-type", &pd->phy_type);
  1061. if (of_find_property(np_emif, "hw-caps-ll-interface", &len))
  1062. pd->hw_caps |= EMIF_HW_CAPS_LL_INTERFACE;
  1063. of_get_ddr_info(np_emif, np_ddr, dev_info);
  1064. if (!is_dev_data_valid(pd->device_info->type, pd->device_info->density,
  1065. pd->device_info->io_width, pd->phy_type, pd->ip_rev,
  1066. emif->dev)) {
  1067. dev_err(dev, "%s: invalid device data!!\n", __func__);
  1068. goto error;
  1069. }
  1070. /*
  1071. * For EMIF instances other than EMIF1 see if the devices connected
  1072. * are exactly same as on EMIF1(which is typically the case). If so,
  1073. * mark it as a duplicate of EMIF1. This will save some memory and
  1074. * computation.
  1075. */
  1076. if (emif1 && emif1->np_ddr == np_ddr) {
  1077. emif->duplicate = true;
  1078. goto out;
  1079. } else if (emif1) {
  1080. dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
  1081. __func__);
  1082. }
  1083. of_get_custom_configs(np_emif, emif);
  1084. emif->plat_data->timings = of_get_ddr_timings(np_ddr, emif->dev,
  1085. emif->plat_data->device_info->type,
  1086. &emif->plat_data->timings_arr_size);
  1087. emif->plat_data->min_tck = of_get_min_tck(np_ddr, emif->dev);
  1088. goto out;
  1089. error:
  1090. return NULL;
  1091. out:
  1092. return emif;
  1093. }
  1094. #else
  1095. static struct emif_data * __init_or_module of_get_memory_device_details(
  1096. struct device_node *np_emif, struct device *dev)
  1097. {
  1098. return NULL;
  1099. }
  1100. #endif
  1101. static struct emif_data *__init_or_module get_device_details(
  1102. struct platform_device *pdev)
  1103. {
  1104. u32 size;
  1105. struct emif_data *emif = NULL;
  1106. struct ddr_device_info *dev_info;
  1107. struct emif_custom_configs *cust_cfgs;
  1108. struct emif_platform_data *pd;
  1109. struct device *dev;
  1110. void *temp;
  1111. pd = pdev->dev.platform_data;
  1112. dev = &pdev->dev;
  1113. if (!(pd && pd->device_info && is_dev_data_valid(pd->device_info->type,
  1114. pd->device_info->density, pd->device_info->io_width,
  1115. pd->phy_type, pd->ip_rev, dev))) {
  1116. dev_err(dev, "%s: invalid device data\n", __func__);
  1117. goto error;
  1118. }
  1119. emif = devm_kzalloc(dev, sizeof(*emif), GFP_KERNEL);
  1120. temp = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  1121. dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
  1122. if (!emif || !pd || !dev_info) {
  1123. dev_err(dev, "%s:%d: allocation error\n", __func__, __LINE__);
  1124. goto error;
  1125. }
  1126. memcpy(temp, pd, sizeof(*pd));
  1127. pd = temp;
  1128. memcpy(dev_info, pd->device_info, sizeof(*dev_info));
  1129. pd->device_info = dev_info;
  1130. emif->plat_data = pd;
  1131. emif->dev = dev;
  1132. emif->temperature_level = SDRAM_TEMP_NOMINAL;
  1133. /*
  1134. * For EMIF instances other than EMIF1 see if the devices connected
  1135. * are exactly same as on EMIF1(which is typically the case). If so,
  1136. * mark it as a duplicate of EMIF1 and skip copying timings data.
  1137. * This will save some memory and some computation later.
  1138. */
  1139. emif->duplicate = emif1 && (memcmp(dev_info,
  1140. emif1->plat_data->device_info,
  1141. sizeof(struct ddr_device_info)) == 0);
  1142. if (emif->duplicate) {
  1143. pd->timings = NULL;
  1144. pd->min_tck = NULL;
  1145. goto out;
  1146. } else if (emif1) {
  1147. dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
  1148. __func__);
  1149. }
  1150. /*
  1151. * Copy custom configs - ignore allocation error, if any, as
  1152. * custom_configs is not very critical
  1153. */
  1154. cust_cfgs = pd->custom_configs;
  1155. if (cust_cfgs && is_custom_config_valid(cust_cfgs, dev)) {
  1156. temp = devm_kzalloc(dev, sizeof(*cust_cfgs), GFP_KERNEL);
  1157. if (temp)
  1158. memcpy(temp, cust_cfgs, sizeof(*cust_cfgs));
  1159. else
  1160. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  1161. __LINE__);
  1162. pd->custom_configs = temp;
  1163. }
  1164. /*
  1165. * Copy timings and min-tck values from platform data. If it is not
  1166. * available or if memory allocation fails, use JEDEC defaults
  1167. */
  1168. size = sizeof(struct lpddr2_timings) * pd->timings_arr_size;
  1169. if (pd->timings) {
  1170. temp = devm_kzalloc(dev, size, GFP_KERNEL);
  1171. if (temp) {
  1172. memcpy(temp, pd->timings, sizeof(*pd->timings));
  1173. pd->timings = temp;
  1174. } else {
  1175. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  1176. __LINE__);
  1177. get_default_timings(emif);
  1178. }
  1179. } else {
  1180. get_default_timings(emif);
  1181. }
  1182. if (pd->min_tck) {
  1183. temp = devm_kzalloc(dev, sizeof(*pd->min_tck), GFP_KERNEL);
  1184. if (temp) {
  1185. memcpy(temp, pd->min_tck, sizeof(*pd->min_tck));
  1186. pd->min_tck = temp;
  1187. } else {
  1188. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  1189. __LINE__);
  1190. pd->min_tck = &lpddr2_jedec_min_tck;
  1191. }
  1192. } else {
  1193. pd->min_tck = &lpddr2_jedec_min_tck;
  1194. }
  1195. out:
  1196. return emif;
  1197. error:
  1198. return NULL;
  1199. }
  1200. static int __init_or_module emif_probe(struct platform_device *pdev)
  1201. {
  1202. struct emif_data *emif;
  1203. struct resource *res;
  1204. int irq;
  1205. if (pdev->dev.of_node)
  1206. emif = of_get_memory_device_details(pdev->dev.of_node, &pdev->dev);
  1207. else
  1208. emif = get_device_details(pdev);
  1209. if (!emif) {
  1210. pr_err("%s: error getting device data\n", __func__);
  1211. goto error;
  1212. }
  1213. list_add(&emif->node, &device_list);
  1214. emif->addressing = get_addressing_table(emif->plat_data->device_info);
  1215. /* Save pointers to each other in emif and device structures */
  1216. emif->dev = &pdev->dev;
  1217. platform_set_drvdata(pdev, emif);
  1218. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1219. if (!res) {
  1220. dev_err(emif->dev, "%s: error getting memory resource\n",
  1221. __func__);
  1222. goto error;
  1223. }
  1224. emif->base = devm_ioremap_resource(emif->dev, res);
  1225. if (IS_ERR(emif->base))
  1226. goto error;
  1227. irq = platform_get_irq(pdev, 0);
  1228. if (irq < 0) {
  1229. dev_err(emif->dev, "%s: error getting IRQ resource - %d\n",
  1230. __func__, irq);
  1231. goto error;
  1232. }
  1233. emif_onetime_settings(emif);
  1234. emif_debugfs_init(emif);
  1235. disable_and_clear_all_interrupts(emif);
  1236. setup_interrupts(emif, irq);
  1237. /* One-time actions taken on probing the first device */
  1238. if (!emif1) {
  1239. emif1 = emif;
  1240. spin_lock_init(&emif_lock);
  1241. /*
  1242. * TODO: register notifiers for frequency and voltage
  1243. * change here once the respective frameworks are
  1244. * available
  1245. */
  1246. }
  1247. dev_info(&pdev->dev, "%s: device configured with addr = %p and IRQ%d\n",
  1248. __func__, emif->base, irq);
  1249. return 0;
  1250. error:
  1251. return -ENODEV;
  1252. }
  1253. static int __exit emif_remove(struct platform_device *pdev)
  1254. {
  1255. struct emif_data *emif = platform_get_drvdata(pdev);
  1256. emif_debugfs_exit(emif);
  1257. return 0;
  1258. }
  1259. static void emif_shutdown(struct platform_device *pdev)
  1260. {
  1261. struct emif_data *emif = platform_get_drvdata(pdev);
  1262. disable_and_clear_all_interrupts(emif);
  1263. }
  1264. static int get_emif_reg_values(struct emif_data *emif, u32 freq,
  1265. struct emif_regs *regs)
  1266. {
  1267. u32 cs1_used, ip_rev, phy_type;
  1268. u32 cl, type;
  1269. const struct lpddr2_timings *timings;
  1270. const struct lpddr2_min_tck *min_tck;
  1271. const struct ddr_device_info *device_info;
  1272. const struct lpddr2_addressing *addressing;
  1273. struct emif_data *emif_for_calc;
  1274. struct device *dev;
  1275. const struct emif_custom_configs *custom_configs;
  1276. dev = emif->dev;
  1277. /*
  1278. * If the devices on this EMIF instance is duplicate of EMIF1,
  1279. * use EMIF1 details for the calculation
  1280. */
  1281. emif_for_calc = emif->duplicate ? emif1 : emif;
  1282. timings = get_timings_table(emif_for_calc, freq);
  1283. addressing = emif_for_calc->addressing;
  1284. if (!timings || !addressing) {
  1285. dev_err(dev, "%s: not enough data available for %dHz",
  1286. __func__, freq);
  1287. return -1;
  1288. }
  1289. device_info = emif_for_calc->plat_data->device_info;
  1290. type = device_info->type;
  1291. cs1_used = device_info->cs1_used;
  1292. ip_rev = emif_for_calc->plat_data->ip_rev;
  1293. phy_type = emif_for_calc->plat_data->phy_type;
  1294. min_tck = emif_for_calc->plat_data->min_tck;
  1295. custom_configs = emif_for_calc->plat_data->custom_configs;
  1296. set_ddr_clk_period(freq);
  1297. regs->ref_ctrl_shdw = get_sdram_ref_ctrl_shdw(freq, addressing);
  1298. regs->sdram_tim1_shdw = get_sdram_tim_1_shdw(timings, min_tck,
  1299. addressing);
  1300. regs->sdram_tim2_shdw = get_sdram_tim_2_shdw(timings, min_tck,
  1301. addressing, type);
  1302. regs->sdram_tim3_shdw = get_sdram_tim_3_shdw(timings, min_tck,
  1303. addressing, type, ip_rev, EMIF_NORMAL_TIMINGS);
  1304. cl = get_cl(emif);
  1305. if (phy_type == EMIF_PHY_TYPE_ATTILAPHY && ip_rev == EMIF_4D) {
  1306. regs->phy_ctrl_1_shdw = get_ddr_phy_ctrl_1_attilaphy_4d(
  1307. timings, freq, cl);
  1308. } else if (phy_type == EMIF_PHY_TYPE_INTELLIPHY && ip_rev == EMIF_4D5) {
  1309. regs->phy_ctrl_1_shdw = get_phy_ctrl_1_intelliphy_4d5(freq, cl);
  1310. regs->ext_phy_ctrl_2_shdw = get_ext_phy_ctrl_2_intelliphy_4d5();
  1311. regs->ext_phy_ctrl_3_shdw = get_ext_phy_ctrl_3_intelliphy_4d5();
  1312. regs->ext_phy_ctrl_4_shdw = get_ext_phy_ctrl_4_intelliphy_4d5();
  1313. } else {
  1314. return -1;
  1315. }
  1316. /* Only timeout values in pwr_mgmt_ctrl_shdw register */
  1317. regs->pwr_mgmt_ctrl_shdw =
  1318. get_pwr_mgmt_ctrl(freq, emif_for_calc, ip_rev) &
  1319. (CS_TIM_MASK | SR_TIM_MASK | PD_TIM_MASK);
  1320. if (ip_rev & EMIF_4D) {
  1321. regs->read_idle_ctrl_shdw_normal =
  1322. get_read_idle_ctrl_shdw(DDR_VOLTAGE_STABLE);
  1323. regs->read_idle_ctrl_shdw_volt_ramp =
  1324. get_read_idle_ctrl_shdw(DDR_VOLTAGE_RAMPING);
  1325. } else if (ip_rev & EMIF_4D5) {
  1326. regs->dll_calib_ctrl_shdw_normal =
  1327. get_dll_calib_ctrl_shdw(DDR_VOLTAGE_STABLE);
  1328. regs->dll_calib_ctrl_shdw_volt_ramp =
  1329. get_dll_calib_ctrl_shdw(DDR_VOLTAGE_RAMPING);
  1330. }
  1331. if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4) {
  1332. regs->ref_ctrl_shdw_derated = get_sdram_ref_ctrl_shdw(freq / 4,
  1333. addressing);
  1334. regs->sdram_tim1_shdw_derated =
  1335. get_sdram_tim_1_shdw_derated(timings, min_tck,
  1336. addressing);
  1337. regs->sdram_tim3_shdw_derated = get_sdram_tim_3_shdw(timings,
  1338. min_tck, addressing, type, ip_rev,
  1339. EMIF_DERATED_TIMINGS);
  1340. }
  1341. regs->freq = freq;
  1342. return 0;
  1343. }
  1344. /*
  1345. * get_regs() - gets the cached emif_regs structure for a given EMIF instance
  1346. * given frequency(freq):
  1347. *
  1348. * As an optimisation, every EMIF instance other than EMIF1 shares the
  1349. * register cache with EMIF1 if the devices connected on this instance
  1350. * are same as that on EMIF1(indicated by the duplicate flag)
  1351. *
  1352. * If we do not have an entry corresponding to the frequency given, we
  1353. * allocate a new entry and calculate the values
  1354. *
  1355. * Upon finding the right reg dump, save it in curr_regs. It can be
  1356. * directly used for thermal de-rating and voltage ramping changes.
  1357. */
  1358. static struct emif_regs *get_regs(struct emif_data *emif, u32 freq)
  1359. {
  1360. int i;
  1361. struct emif_regs **regs_cache;
  1362. struct emif_regs *regs = NULL;
  1363. struct device *dev;
  1364. dev = emif->dev;
  1365. if (emif->curr_regs && emif->curr_regs->freq == freq) {
  1366. dev_dbg(dev, "%s: using curr_regs - %u Hz", __func__, freq);
  1367. return emif->curr_regs;
  1368. }
  1369. if (emif->duplicate)
  1370. regs_cache = emif1->regs_cache;
  1371. else
  1372. regs_cache = emif->regs_cache;
  1373. for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) {
  1374. if (regs_cache[i]->freq == freq) {
  1375. regs = regs_cache[i];
  1376. dev_dbg(dev,
  1377. "%s: reg dump found in reg cache for %u Hz\n",
  1378. __func__, freq);
  1379. break;
  1380. }
  1381. }
  1382. /*
  1383. * If we don't have an entry for this frequency in the cache create one
  1384. * and calculate the values
  1385. */
  1386. if (!regs) {
  1387. regs = devm_kzalloc(emif->dev, sizeof(*regs), GFP_ATOMIC);
  1388. if (!regs)
  1389. return NULL;
  1390. if (get_emif_reg_values(emif, freq, regs)) {
  1391. devm_kfree(emif->dev, regs);
  1392. return NULL;
  1393. }
  1394. /*
  1395. * Now look for an un-used entry in the cache and save the
  1396. * newly created struct. If there are no free entries
  1397. * over-write the last entry
  1398. */
  1399. for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++)
  1400. ;
  1401. if (i >= EMIF_MAX_NUM_FREQUENCIES) {
  1402. dev_warn(dev, "%s: regs_cache full - reusing a slot!!\n",
  1403. __func__);
  1404. i = EMIF_MAX_NUM_FREQUENCIES - 1;
  1405. devm_kfree(emif->dev, regs_cache[i]);
  1406. }
  1407. regs_cache[i] = regs;
  1408. }
  1409. return regs;
  1410. }
  1411. static void do_volt_notify_handling(struct emif_data *emif, u32 volt_state)
  1412. {
  1413. dev_dbg(emif->dev, "%s: voltage notification : %d", __func__,
  1414. volt_state);
  1415. if (!emif->curr_regs) {
  1416. dev_err(emif->dev,
  1417. "%s: volt-notify before registers are ready: %d\n",
  1418. __func__, volt_state);
  1419. return;
  1420. }
  1421. setup_volt_sensitive_regs(emif, emif->curr_regs, volt_state);
  1422. }
  1423. /*
  1424. * TODO: voltage notify handling should be hooked up to
  1425. * regulator framework as soon as the necessary support
  1426. * is available in mainline kernel. This function is un-used
  1427. * right now.
  1428. */
  1429. static void __attribute__((unused)) volt_notify_handling(u32 volt_state)
  1430. {
  1431. struct emif_data *emif;
  1432. spin_lock_irqsave(&emif_lock, irq_state);
  1433. list_for_each_entry(emif, &device_list, node)
  1434. do_volt_notify_handling(emif, volt_state);
  1435. do_freq_update();
  1436. spin_unlock_irqrestore(&emif_lock, irq_state);
  1437. }
  1438. static void do_freq_pre_notify_handling(struct emif_data *emif, u32 new_freq)
  1439. {
  1440. struct emif_regs *regs;
  1441. regs = get_regs(emif, new_freq);
  1442. if (!regs)
  1443. return;
  1444. emif->curr_regs = regs;
  1445. /*
  1446. * Update the shadow registers:
  1447. * Temperature and voltage-ramp sensitive settings are also configured
  1448. * in terms of DDR cycles. So, we need to update them too when there
  1449. * is a freq change
  1450. */
  1451. dev_dbg(emif->dev, "%s: setting up shadow registers for %uHz",
  1452. __func__, new_freq);
  1453. setup_registers(emif, regs);
  1454. setup_temperature_sensitive_regs(emif, regs);
  1455. setup_volt_sensitive_regs(emif, regs, DDR_VOLTAGE_STABLE);
  1456. /*
  1457. * Part of workaround for errata i728. See do_freq_update()
  1458. * for more details
  1459. */
  1460. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  1461. set_lpmode(emif, EMIF_LP_MODE_DISABLE);
  1462. }
  1463. /*
  1464. * TODO: frequency notify handling should be hooked up to
  1465. * clock framework as soon as the necessary support is
  1466. * available in mainline kernel. This function is un-used
  1467. * right now.
  1468. */
  1469. static void __attribute__((unused)) freq_pre_notify_handling(u32 new_freq)
  1470. {
  1471. struct emif_data *emif;
  1472. /*
  1473. * NOTE: we are taking the spin-lock here and releases it
  1474. * only in post-notifier. This doesn't look good and
  1475. * Sparse complains about it, but this seems to be
  1476. * un-avoidable. We need to lock a sequence of events
  1477. * that is split between EMIF and clock framework.
  1478. *
  1479. * 1. EMIF driver updates EMIF timings in shadow registers in the
  1480. * frequency pre-notify callback from clock framework
  1481. * 2. clock framework sets up the registers for the new frequency
  1482. * 3. clock framework initiates a hw-sequence that updates
  1483. * the frequency EMIF timings synchronously.
  1484. *
  1485. * All these 3 steps should be performed as an atomic operation
  1486. * vis-a-vis similar sequence in the EMIF interrupt handler
  1487. * for temperature events. Otherwise, there could be race
  1488. * conditions that could result in incorrect EMIF timings for
  1489. * a given frequency
  1490. */
  1491. spin_lock_irqsave(&emif_lock, irq_state);
  1492. list_for_each_entry(emif, &device_list, node)
  1493. do_freq_pre_notify_handling(emif, new_freq);
  1494. }
  1495. static void do_freq_post_notify_handling(struct emif_data *emif)
  1496. {
  1497. /*
  1498. * Part of workaround for errata i728. See do_freq_update()
  1499. * for more details
  1500. */
  1501. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  1502. set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH);
  1503. }
  1504. /*
  1505. * TODO: frequency notify handling should be hooked up to
  1506. * clock framework as soon as the necessary support is
  1507. * available in mainline kernel. This function is un-used
  1508. * right now.
  1509. */
  1510. static void __attribute__((unused)) freq_post_notify_handling(void)
  1511. {
  1512. struct emif_data *emif;
  1513. list_for_each_entry(emif, &device_list, node)
  1514. do_freq_post_notify_handling(emif);
  1515. /*
  1516. * Lock is done in pre-notify handler. See freq_pre_notify_handling()
  1517. * for more details
  1518. */
  1519. spin_unlock_irqrestore(&emif_lock, irq_state);
  1520. }
  1521. #if defined(CONFIG_OF)
  1522. static const struct of_device_id emif_of_match[] = {
  1523. { .compatible = "ti,emif-4d" },
  1524. { .compatible = "ti,emif-4d5" },
  1525. {},
  1526. };
  1527. MODULE_DEVICE_TABLE(of, emif_of_match);
  1528. #endif
  1529. static struct platform_driver emif_driver = {
  1530. .remove = __exit_p(emif_remove),
  1531. .shutdown = emif_shutdown,
  1532. .driver = {
  1533. .name = "emif",
  1534. .of_match_table = of_match_ptr(emif_of_match),
  1535. },
  1536. };
  1537. module_platform_driver_probe(emif_driver, emif_probe);
  1538. MODULE_DESCRIPTION("TI EMIF SDRAM Controller Driver");
  1539. MODULE_LICENSE("GPL");
  1540. MODULE_ALIAS("platform:emif");
  1541. MODULE_AUTHOR("Texas Instruments Inc");