emif.c 54 KB

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