emif.c 52 KB

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