emif.c 52 KB

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