emif.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * EMIF driver
  4. *
  5. * Copyright (C) 2012 Texas Instruments, Inc.
  6. *
  7. * Aneesh V <aneesh@ti.com>
  8. * Santosh Shilimkar <santosh.shilimkar@ti.com>
  9. */
  10. #include <linux/cleanup.h>
  11. #include <linux/err.h>
  12. #include <linux/kernel.h>
  13. #include <linux/reboot.h>
  14. #include <linux/platform_data/emif_plat.h>
  15. #include <linux/io.h>
  16. #include <linux/device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/slab.h>
  20. #include <linux/of.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/module.h>
  24. #include <linux/list.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/pm.h>
  27. #include "emif.h"
  28. #include "jedec_ddr.h"
  29. #include "of_memory.h"
  30. /**
  31. * struct emif_data - Per device static data for driver's use
  32. * @duplicate: Whether the DDR devices attached to this EMIF
  33. * instance are exactly same as that on EMIF1. In
  34. * this case we can save some memory and processing
  35. * @temperature_level: Maximum temperature of LPDDR2 devices attached
  36. * to this EMIF - read from MR4 register. If there
  37. * are two devices attached to this EMIF, this
  38. * value is the maximum of the two temperature
  39. * levels.
  40. * @lpmode: Chosen low power mode
  41. * @node: node in the device list
  42. * @base: base address of memory-mapped IO registers.
  43. * @dev: device pointer.
  44. * @regs_cache: An array of 'struct emif_regs' that stores
  45. * calculated register values for different
  46. * frequencies, to avoid re-calculating them on
  47. * each DVFS transition.
  48. * @curr_regs: The set of register values used in the last
  49. * frequency change (i.e. corresponding to the
  50. * frequency in effect at the moment)
  51. * @plat_data: Pointer to saved platform data.
  52. * @debugfs_root: dentry to the root folder for EMIF in debugfs
  53. * @np_ddr: Pointer to ddr device tree node
  54. */
  55. struct emif_data {
  56. u8 duplicate;
  57. u8 temperature_level;
  58. u8 lpmode;
  59. struct list_head node;
  60. void __iomem *base;
  61. struct device *dev;
  62. struct emif_regs *regs_cache[EMIF_MAX_NUM_FREQUENCIES];
  63. struct emif_regs *curr_regs;
  64. struct emif_platform_data *plat_data;
  65. struct dentry *debugfs_root;
  66. struct device_node *np_ddr;
  67. };
  68. static struct emif_data *emif1;
  69. static DEFINE_SPINLOCK(emif_lock);
  70. static LIST_HEAD(device_list);
  71. static void do_emif_regdump_show(struct seq_file *s, struct emif_data *emif,
  72. struct emif_regs *regs)
  73. {
  74. u32 type = emif->plat_data->device_info->type;
  75. u32 ip_rev = emif->plat_data->ip_rev;
  76. seq_printf(s, "EMIF register cache dump for %dMHz\n",
  77. regs->freq/1000000);
  78. seq_printf(s, "ref_ctrl_shdw\t: 0x%08x\n", regs->ref_ctrl_shdw);
  79. seq_printf(s, "sdram_tim1_shdw\t: 0x%08x\n", regs->sdram_tim1_shdw);
  80. seq_printf(s, "sdram_tim2_shdw\t: 0x%08x\n", regs->sdram_tim2_shdw);
  81. seq_printf(s, "sdram_tim3_shdw\t: 0x%08x\n", regs->sdram_tim3_shdw);
  82. if (ip_rev == EMIF_4D) {
  83. seq_printf(s, "read_idle_ctrl_shdw_normal\t: 0x%08x\n",
  84. regs->read_idle_ctrl_shdw_normal);
  85. seq_printf(s, "read_idle_ctrl_shdw_volt_ramp\t: 0x%08x\n",
  86. regs->read_idle_ctrl_shdw_volt_ramp);
  87. } else if (ip_rev == EMIF_4D5) {
  88. seq_printf(s, "dll_calib_ctrl_shdw_normal\t: 0x%08x\n",
  89. regs->dll_calib_ctrl_shdw_normal);
  90. seq_printf(s, "dll_calib_ctrl_shdw_volt_ramp\t: 0x%08x\n",
  91. regs->dll_calib_ctrl_shdw_volt_ramp);
  92. }
  93. if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4) {
  94. seq_printf(s, "ref_ctrl_shdw_derated\t: 0x%08x\n",
  95. regs->ref_ctrl_shdw_derated);
  96. seq_printf(s, "sdram_tim1_shdw_derated\t: 0x%08x\n",
  97. regs->sdram_tim1_shdw_derated);
  98. seq_printf(s, "sdram_tim3_shdw_derated\t: 0x%08x\n",
  99. regs->sdram_tim3_shdw_derated);
  100. }
  101. }
  102. static int emif_regdump_show(struct seq_file *s, void *unused)
  103. {
  104. struct emif_data *emif = s->private;
  105. struct emif_regs **regs_cache;
  106. int i;
  107. if (emif->duplicate)
  108. regs_cache = emif1->regs_cache;
  109. else
  110. regs_cache = emif->regs_cache;
  111. for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) {
  112. do_emif_regdump_show(s, emif, regs_cache[i]);
  113. seq_putc(s, '\n');
  114. }
  115. return 0;
  116. }
  117. DEFINE_SHOW_ATTRIBUTE(emif_regdump);
  118. static int emif_mr4_show(struct seq_file *s, void *unused)
  119. {
  120. struct emif_data *emif = s->private;
  121. seq_printf(s, "MR4=%d\n", emif->temperature_level);
  122. return 0;
  123. }
  124. DEFINE_SHOW_ATTRIBUTE(emif_mr4);
  125. static void emif_debugfs_init(struct emif_data *emif)
  126. {
  127. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  128. emif->debugfs_root = debugfs_create_dir(dev_name(emif->dev), NULL);
  129. debugfs_create_file("regcache_dump", S_IRUGO, emif->debugfs_root, emif,
  130. &emif_regdump_fops);
  131. debugfs_create_file("mr4", S_IRUGO, emif->debugfs_root, emif,
  132. &emif_mr4_fops);
  133. }
  134. }
  135. static void emif_debugfs_exit(struct emif_data *emif)
  136. {
  137. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  138. debugfs_remove_recursive(emif->debugfs_root);
  139. emif->debugfs_root = NULL;
  140. }
  141. }
  142. /*
  143. * Get bus width used by EMIF. Note that this may be different from the
  144. * bus width of the DDR devices used. For instance two 16-bit DDR devices
  145. * may be connected to a given CS of EMIF. In this case bus width as far
  146. * as EMIF is concerned is 32, where as the DDR bus width is 16 bits.
  147. */
  148. static u32 get_emif_bus_width(struct emif_data *emif)
  149. {
  150. u32 width;
  151. void __iomem *base = emif->base;
  152. width = (readl(base + EMIF_SDRAM_CONFIG) & NARROW_MODE_MASK)
  153. >> NARROW_MODE_SHIFT;
  154. width = width == 0 ? 32 : 16;
  155. return width;
  156. }
  157. static void set_lpmode(struct emif_data *emif, u8 lpmode)
  158. {
  159. u32 temp;
  160. void __iomem *base = emif->base;
  161. /*
  162. * Workaround for errata i743 - LPDDR2 Power-Down State is Not
  163. * Efficient
  164. *
  165. * i743 DESCRIPTION:
  166. * The EMIF supports power-down state for low power. The EMIF
  167. * automatically puts the SDRAM into power-down after the memory is
  168. * not accessed for a defined number of cycles and the
  169. * EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field is set to 0x4.
  170. * As the EMIF supports automatic output impedance calibration, a ZQ
  171. * calibration long command is issued every time it exits active
  172. * power-down and precharge power-down modes. The EMIF waits and
  173. * blocks any other command during this calibration.
  174. * The EMIF does not allow selective disabling of ZQ calibration upon
  175. * exit of power-down mode. Due to very short periods of power-down
  176. * cycles, ZQ calibration overhead creates bandwidth issues and
  177. * increases overall system power consumption. On the other hand,
  178. * issuing ZQ calibration long commands when exiting self-refresh is
  179. * still required.
  180. *
  181. * WORKAROUND
  182. * Because there is no power consumption benefit of the power-down due
  183. * to the calibration and there is a performance risk, the guideline
  184. * is to not allow power-down state and, therefore, to not have set
  185. * the EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field to 0x4.
  186. */
  187. if ((emif->plat_data->ip_rev == EMIF_4D) &&
  188. (lpmode == EMIF_LP_MODE_PWR_DN)) {
  189. WARN_ONCE(1,
  190. "REG_LP_MODE = LP_MODE_PWR_DN(4) is prohibited by erratum i743 switch to LP_MODE_SELF_REFRESH(2)\n");
  191. /* rollback LP_MODE to Self-refresh mode */
  192. lpmode = EMIF_LP_MODE_SELF_REFRESH;
  193. }
  194. temp = readl(base + EMIF_POWER_MANAGEMENT_CONTROL);
  195. temp &= ~LP_MODE_MASK;
  196. temp |= (lpmode << LP_MODE_SHIFT);
  197. writel(temp, base + EMIF_POWER_MANAGEMENT_CONTROL);
  198. }
  199. static void do_freq_update(void)
  200. {
  201. struct emif_data *emif;
  202. /*
  203. * Workaround for errata i728: Disable LPMODE during FREQ_UPDATE
  204. *
  205. * i728 DESCRIPTION:
  206. * The EMIF automatically puts the SDRAM into self-refresh mode
  207. * after the EMIF has not performed accesses during
  208. * EMIF_PWR_MGMT_CTRL[7:4] REG_SR_TIM number of DDR clock cycles
  209. * and the EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field is set
  210. * to 0x2. If during a small window the following three events
  211. * occur:
  212. * - The SR_TIMING counter expires
  213. * - And frequency change is requested
  214. * - And OCP access is requested
  215. * Then it causes instable clock on the DDR interface.
  216. *
  217. * WORKAROUND
  218. * To avoid the occurrence of the three events, the workaround
  219. * is to disable the self-refresh when requesting a frequency
  220. * change. Before requesting a frequency change the software must
  221. * program EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x0. When the
  222. * frequency change has been done, the software can reprogram
  223. * EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x2
  224. */
  225. list_for_each_entry(emif, &device_list, node) {
  226. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  227. set_lpmode(emif, EMIF_LP_MODE_DISABLE);
  228. }
  229. /*
  230. * TODO: Do FREQ_UPDATE here when an API
  231. * is available for this as part of the new
  232. * clock framework
  233. */
  234. list_for_each_entry(emif, &device_list, node) {
  235. if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
  236. set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH);
  237. }
  238. }
  239. /* Find addressing table entry based on the device's type and density */
  240. static const struct lpddr2_addressing *get_addressing_table(
  241. const struct ddr_device_info *device_info)
  242. {
  243. u32 index, type, density;
  244. type = device_info->type;
  245. density = device_info->density;
  246. switch (type) {
  247. case DDR_TYPE_LPDDR2_S4:
  248. index = density - 1;
  249. break;
  250. case DDR_TYPE_LPDDR2_S2:
  251. switch (density) {
  252. case DDR_DENSITY_1Gb:
  253. case DDR_DENSITY_2Gb:
  254. index = density + 3;
  255. break;
  256. default:
  257. index = density - 1;
  258. }
  259. break;
  260. default:
  261. return NULL;
  262. }
  263. return &lpddr2_jedec_addressing_table[index];
  264. }
  265. static u32 get_zq_config_reg(const struct lpddr2_addressing *addressing,
  266. bool cs1_used, bool cal_resistors_per_cs)
  267. {
  268. u32 zq = 0, val = 0;
  269. val = EMIF_ZQCS_INTERVAL_US * 1000 / addressing->tREFI_ns;
  270. zq |= val << ZQ_REFINTERVAL_SHIFT;
  271. val = DIV_ROUND_UP(T_ZQCL_DEFAULT_NS, T_ZQCS_DEFAULT_NS) - 1;
  272. zq |= val << ZQ_ZQCL_MULT_SHIFT;
  273. val = DIV_ROUND_UP(T_ZQINIT_DEFAULT_NS, T_ZQCL_DEFAULT_NS) - 1;
  274. zq |= val << ZQ_ZQINIT_MULT_SHIFT;
  275. zq |= ZQ_SFEXITEN_ENABLE << ZQ_SFEXITEN_SHIFT;
  276. if (cal_resistors_per_cs)
  277. zq |= ZQ_DUALCALEN_ENABLE << ZQ_DUALCALEN_SHIFT;
  278. else
  279. zq |= ZQ_DUALCALEN_DISABLE << ZQ_DUALCALEN_SHIFT;
  280. zq |= ZQ_CS0EN_MASK; /* CS0 is used for sure */
  281. val = cs1_used ? 1 : 0;
  282. zq |= val << ZQ_CS1EN_SHIFT;
  283. return zq;
  284. }
  285. static u32 get_temp_alert_config(const struct lpddr2_addressing *addressing,
  286. const struct emif_custom_configs *custom_configs, bool cs1_used,
  287. u32 sdram_io_width, u32 emif_bus_width)
  288. {
  289. u32 alert = 0, interval, devcnt;
  290. if (custom_configs && (custom_configs->mask &
  291. EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL))
  292. interval = custom_configs->temp_alert_poll_interval_ms;
  293. else
  294. interval = TEMP_ALERT_POLL_INTERVAL_DEFAULT_MS;
  295. interval *= 1000000; /* Convert to ns */
  296. interval /= addressing->tREFI_ns; /* Convert to refresh cycles */
  297. alert |= (interval << TA_REFINTERVAL_SHIFT);
  298. /*
  299. * sdram_io_width is in 'log2(x) - 1' form. Convert emif_bus_width
  300. * also to this form and subtract to get TA_DEVCNT, which is
  301. * in log2(x) form.
  302. */
  303. emif_bus_width = __fls(emif_bus_width) - 1;
  304. devcnt = emif_bus_width - sdram_io_width;
  305. alert |= devcnt << TA_DEVCNT_SHIFT;
  306. /* DEVWDT is in 'log2(x) - 3' form */
  307. alert |= (sdram_io_width - 2) << TA_DEVWDT_SHIFT;
  308. alert |= 1 << TA_SFEXITEN_SHIFT;
  309. alert |= 1 << TA_CS0EN_SHIFT;
  310. alert |= (cs1_used ? 1 : 0) << TA_CS1EN_SHIFT;
  311. return alert;
  312. }
  313. static u32 get_pwr_mgmt_ctrl(u32 freq, struct emif_data *emif, u32 ip_rev)
  314. {
  315. u32 pwr_mgmt_ctrl = 0, timeout;
  316. u32 lpmode = EMIF_LP_MODE_SELF_REFRESH;
  317. u32 timeout_perf = EMIF_LP_MODE_TIMEOUT_PERFORMANCE;
  318. u32 timeout_pwr = EMIF_LP_MODE_TIMEOUT_POWER;
  319. u32 freq_threshold = EMIF_LP_MODE_FREQ_THRESHOLD;
  320. u32 mask;
  321. u8 shift;
  322. struct emif_custom_configs *cust_cfgs = emif->plat_data->custom_configs;
  323. if (cust_cfgs && (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE)) {
  324. lpmode = cust_cfgs->lpmode;
  325. timeout_perf = cust_cfgs->lpmode_timeout_performance;
  326. timeout_pwr = cust_cfgs->lpmode_timeout_power;
  327. freq_threshold = cust_cfgs->lpmode_freq_threshold;
  328. }
  329. /* Timeout based on DDR frequency */
  330. timeout = freq >= freq_threshold ? timeout_perf : timeout_pwr;
  331. /*
  332. * The value to be set in register is "log2(timeout) - 3"
  333. * if timeout < 16 load 0 in register
  334. * if timeout is not a power of 2, round to next highest power of 2
  335. */
  336. if (timeout < 16) {
  337. timeout = 0;
  338. } else {
  339. if (timeout & (timeout - 1))
  340. timeout <<= 1;
  341. timeout = __fls(timeout) - 3;
  342. }
  343. switch (lpmode) {
  344. case EMIF_LP_MODE_CLOCK_STOP:
  345. shift = CS_TIM_SHIFT;
  346. mask = CS_TIM_MASK;
  347. break;
  348. case EMIF_LP_MODE_SELF_REFRESH:
  349. /* Workaround for errata i735 */
  350. if (timeout < 6)
  351. timeout = 6;
  352. shift = SR_TIM_SHIFT;
  353. mask = SR_TIM_MASK;
  354. break;
  355. case EMIF_LP_MODE_PWR_DN:
  356. shift = PD_TIM_SHIFT;
  357. mask = PD_TIM_MASK;
  358. break;
  359. case EMIF_LP_MODE_DISABLE:
  360. default:
  361. mask = 0;
  362. shift = 0;
  363. break;
  364. }
  365. /* Round to maximum in case of overflow, BUT warn! */
  366. if (lpmode != EMIF_LP_MODE_DISABLE && timeout > mask >> shift) {
  367. pr_err("TIMEOUT Overflow - lpmode=%d perf=%d pwr=%d freq=%d\n",
  368. lpmode,
  369. timeout_perf,
  370. timeout_pwr,
  371. freq_threshold);
  372. WARN(1, "timeout=0x%02x greater than 0x%02x. Using max\n",
  373. timeout, mask >> shift);
  374. timeout = mask >> shift;
  375. }
  376. /* Setup required timing */
  377. pwr_mgmt_ctrl = (timeout << shift) & mask;
  378. /* setup a default mask for rest of the modes */
  379. pwr_mgmt_ctrl |= (SR_TIM_MASK | CS_TIM_MASK | PD_TIM_MASK) &
  380. ~mask;
  381. /* No CS_TIM in EMIF_4D5 */
  382. if (ip_rev == EMIF_4D5)
  383. pwr_mgmt_ctrl &= ~CS_TIM_MASK;
  384. pwr_mgmt_ctrl |= lpmode << LP_MODE_SHIFT;
  385. return pwr_mgmt_ctrl;
  386. }
  387. /*
  388. * Get the temperature level of the EMIF instance:
  389. * Reads the MR4 register of attached SDRAM parts to find out the temperature
  390. * level. If there are two parts attached(one on each CS), then the temperature
  391. * level for the EMIF instance is the higher of the two temperatures.
  392. */
  393. static void get_temperature_level(struct emif_data *emif)
  394. {
  395. u32 temp, temperature_level;
  396. void __iomem *base;
  397. base = emif->base;
  398. /* Read mode register 4 */
  399. writel(DDR_MR4, base + EMIF_LPDDR2_MODE_REG_CONFIG);
  400. temperature_level = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
  401. temperature_level = (temperature_level & MR4_SDRAM_REF_RATE_MASK) >>
  402. MR4_SDRAM_REF_RATE_SHIFT;
  403. if (emif->plat_data->device_info->cs1_used) {
  404. writel(DDR_MR4 | CS_MASK, base + EMIF_LPDDR2_MODE_REG_CONFIG);
  405. temp = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
  406. temp = (temp & MR4_SDRAM_REF_RATE_MASK)
  407. >> MR4_SDRAM_REF_RATE_SHIFT;
  408. temperature_level = max(temp, temperature_level);
  409. }
  410. /* treat everything less than nominal(3) in MR4 as nominal */
  411. if (unlikely(temperature_level < SDRAM_TEMP_NOMINAL))
  412. temperature_level = SDRAM_TEMP_NOMINAL;
  413. /* if we get reserved value in MR4 persist with the existing value */
  414. if (likely(temperature_level != SDRAM_TEMP_RESERVED_4))
  415. emif->temperature_level = temperature_level;
  416. }
  417. /*
  418. * setup_temperature_sensitive_regs() - set the timings for temperature
  419. * sensitive registers. This happens once at initialisation time based
  420. * on the temperature at boot time and subsequently based on the temperature
  421. * alert interrupt. Temperature alert can happen when the temperature
  422. * increases or drops. So this function can have the effect of either
  423. * derating the timings or going back to nominal values.
  424. */
  425. static void setup_temperature_sensitive_regs(struct emif_data *emif,
  426. struct emif_regs *regs)
  427. {
  428. u32 tim1, tim3, ref_ctrl, type;
  429. void __iomem *base = emif->base;
  430. u32 temperature;
  431. type = emif->plat_data->device_info->type;
  432. tim1 = regs->sdram_tim1_shdw;
  433. tim3 = regs->sdram_tim3_shdw;
  434. ref_ctrl = regs->ref_ctrl_shdw;
  435. /* No de-rating for non-lpddr2 devices */
  436. if (type != DDR_TYPE_LPDDR2_S2 && type != DDR_TYPE_LPDDR2_S4)
  437. goto out;
  438. temperature = emif->temperature_level;
  439. if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH) {
  440. ref_ctrl = regs->ref_ctrl_shdw_derated;
  441. } else if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH_AND_TIMINGS) {
  442. tim1 = regs->sdram_tim1_shdw_derated;
  443. tim3 = regs->sdram_tim3_shdw_derated;
  444. ref_ctrl = regs->ref_ctrl_shdw_derated;
  445. }
  446. out:
  447. writel(tim1, base + EMIF_SDRAM_TIMING_1_SHDW);
  448. writel(tim3, base + EMIF_SDRAM_TIMING_3_SHDW);
  449. writel(ref_ctrl, base + EMIF_SDRAM_REFRESH_CTRL_SHDW);
  450. }
  451. static irqreturn_t handle_temp_alert(void __iomem *base, struct emif_data *emif)
  452. {
  453. u32 old_temp_level;
  454. irqreturn_t ret;
  455. struct emif_custom_configs *custom_configs;
  456. guard(spinlock_irqsave)(&emif_lock);
  457. old_temp_level = emif->temperature_level;
  458. get_temperature_level(emif);
  459. if (unlikely(emif->temperature_level == old_temp_level)) {
  460. return IRQ_HANDLED;
  461. } else if (!emif->curr_regs) {
  462. dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
  463. return IRQ_HANDLED;
  464. }
  465. custom_configs = emif->plat_data->custom_configs;
  466. /*
  467. * IF we detect higher than "nominal rating" from DDR sensor
  468. * on an unsupported DDR part, shutdown system
  469. */
  470. if (custom_configs && !(custom_configs->mask &
  471. EMIF_CUSTOM_CONFIG_EXTENDED_TEMP_PART)) {
  472. if (emif->temperature_level >= SDRAM_TEMP_HIGH_DERATE_REFRESH) {
  473. dev_err(emif->dev,
  474. "%s:NOT Extended temperature capable memory. Converting MR4=0x%02x as shutdown event\n",
  475. __func__, emif->temperature_level);
  476. /*
  477. * Temperature far too high - do kernel_power_off()
  478. * from thread context
  479. */
  480. emif->temperature_level = SDRAM_TEMP_VERY_HIGH_SHUTDOWN;
  481. return IRQ_WAKE_THREAD;
  482. }
  483. }
  484. if (emif->temperature_level < old_temp_level ||
  485. emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
  486. /*
  487. * Temperature coming down - defer handling to thread OR
  488. * Temperature far too high - do kernel_power_off() from
  489. * thread context
  490. */
  491. ret = IRQ_WAKE_THREAD;
  492. } else {
  493. /* Temperature is going up - handle immediately */
  494. setup_temperature_sensitive_regs(emif, emif->curr_regs);
  495. do_freq_update();
  496. ret = IRQ_HANDLED;
  497. }
  498. return ret;
  499. }
  500. static irqreturn_t emif_interrupt_handler(int irq, void *dev_id)
  501. {
  502. u32 interrupts;
  503. struct emif_data *emif = dev_id;
  504. void __iomem *base = emif->base;
  505. struct device *dev = emif->dev;
  506. irqreturn_t ret = IRQ_HANDLED;
  507. /* Save the status and clear it */
  508. interrupts = readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  509. writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  510. /*
  511. * Handle temperature alert
  512. * Temperature alert should be same for all ports
  513. * So, it's enough to process it only for one of the ports
  514. */
  515. if (interrupts & TA_SYS_MASK)
  516. ret = handle_temp_alert(base, emif);
  517. if (interrupts & ERR_SYS_MASK)
  518. dev_err(dev, "Access error from SYS port - %x\n", interrupts);
  519. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
  520. /* Save the status and clear it */
  521. interrupts = readl(base + EMIF_LL_OCP_INTERRUPT_STATUS);
  522. writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_STATUS);
  523. if (interrupts & ERR_LL_MASK)
  524. dev_err(dev, "Access error from LL port - %x\n",
  525. interrupts);
  526. }
  527. return ret;
  528. }
  529. static irqreturn_t emif_threaded_isr(int irq, void *dev_id)
  530. {
  531. struct emif_data *emif = dev_id;
  532. unsigned long irq_state;
  533. if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
  534. dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
  535. /* If we have Power OFF ability, use it, else try restarting */
  536. if (kernel_can_power_off()) {
  537. kernel_power_off();
  538. } else {
  539. WARN(1, "FIXME: NO pm_power_off!!! trying restart\n");
  540. kernel_restart("SDRAM Over-temp Emergency restart");
  541. }
  542. return IRQ_HANDLED;
  543. }
  544. spin_lock_irqsave(&emif_lock, irq_state);
  545. if (emif->curr_regs) {
  546. setup_temperature_sensitive_regs(emif, emif->curr_regs);
  547. do_freq_update();
  548. } else {
  549. dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
  550. }
  551. spin_unlock_irqrestore(&emif_lock, irq_state);
  552. return IRQ_HANDLED;
  553. }
  554. static void clear_all_interrupts(struct emif_data *emif)
  555. {
  556. void __iomem *base = emif->base;
  557. writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS),
  558. base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
  559. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
  560. writel(readl(base + EMIF_LL_OCP_INTERRUPT_STATUS),
  561. base + EMIF_LL_OCP_INTERRUPT_STATUS);
  562. }
  563. static void disable_and_clear_all_interrupts(struct emif_data *emif)
  564. {
  565. void __iomem *base = emif->base;
  566. /* Disable all interrupts */
  567. writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET),
  568. base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_CLEAR);
  569. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
  570. writel(readl(base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET),
  571. base + EMIF_LL_OCP_INTERRUPT_ENABLE_CLEAR);
  572. /* Clear all interrupts */
  573. clear_all_interrupts(emif);
  574. }
  575. static int setup_interrupts(struct emif_data *emif, u32 irq)
  576. {
  577. u32 interrupts, type;
  578. void __iomem *base = emif->base;
  579. type = emif->plat_data->device_info->type;
  580. clear_all_interrupts(emif);
  581. /* Enable interrupts for SYS interface */
  582. interrupts = EN_ERR_SYS_MASK;
  583. if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4)
  584. interrupts |= EN_TA_SYS_MASK;
  585. writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET);
  586. /* Enable interrupts for LL interface */
  587. if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
  588. /* TA need not be enabled for LL */
  589. interrupts = EN_ERR_LL_MASK;
  590. writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET);
  591. }
  592. /* setup IRQ handlers */
  593. return devm_request_threaded_irq(emif->dev, irq,
  594. emif_interrupt_handler,
  595. emif_threaded_isr,
  596. 0, dev_name(emif->dev),
  597. emif);
  598. }
  599. static void emif_onetime_settings(struct emif_data *emif)
  600. {
  601. u32 pwr_mgmt_ctrl, zq, temp_alert_cfg;
  602. void __iomem *base = emif->base;
  603. const struct lpddr2_addressing *addressing;
  604. const struct ddr_device_info *device_info;
  605. device_info = emif->plat_data->device_info;
  606. addressing = get_addressing_table(device_info);
  607. /*
  608. * Init power management settings
  609. * We don't know the frequency yet. Use a high frequency
  610. * value for a conservative timeout setting
  611. */
  612. pwr_mgmt_ctrl = get_pwr_mgmt_ctrl(1000000000, emif,
  613. emif->plat_data->ip_rev);
  614. emif->lpmode = (pwr_mgmt_ctrl & LP_MODE_MASK) >> LP_MODE_SHIFT;
  615. writel(pwr_mgmt_ctrl, base + EMIF_POWER_MANAGEMENT_CONTROL);
  616. /* Init ZQ calibration settings */
  617. zq = get_zq_config_reg(addressing, device_info->cs1_used,
  618. device_info->cal_resistors_per_cs);
  619. writel(zq, base + EMIF_SDRAM_OUTPUT_IMPEDANCE_CALIBRATION_CONFIG);
  620. /* Check temperature level temperature level*/
  621. get_temperature_level(emif);
  622. if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN)
  623. dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
  624. /* Init temperature polling */
  625. temp_alert_cfg = get_temp_alert_config(addressing,
  626. emif->plat_data->custom_configs, device_info->cs1_used,
  627. device_info->io_width, get_emif_bus_width(emif));
  628. writel(temp_alert_cfg, base + EMIF_TEMPERATURE_ALERT_CONFIG);
  629. /*
  630. * Program external PHY control registers that are not frequency
  631. * dependent
  632. */
  633. if (emif->plat_data->phy_type != EMIF_PHY_TYPE_INTELLIPHY)
  634. return;
  635. writel(EMIF_EXT_PHY_CTRL_1_VAL, base + EMIF_EXT_PHY_CTRL_1_SHDW);
  636. writel(EMIF_EXT_PHY_CTRL_5_VAL, base + EMIF_EXT_PHY_CTRL_5_SHDW);
  637. writel(EMIF_EXT_PHY_CTRL_6_VAL, base + EMIF_EXT_PHY_CTRL_6_SHDW);
  638. writel(EMIF_EXT_PHY_CTRL_7_VAL, base + EMIF_EXT_PHY_CTRL_7_SHDW);
  639. writel(EMIF_EXT_PHY_CTRL_8_VAL, base + EMIF_EXT_PHY_CTRL_8_SHDW);
  640. writel(EMIF_EXT_PHY_CTRL_9_VAL, base + EMIF_EXT_PHY_CTRL_9_SHDW);
  641. writel(EMIF_EXT_PHY_CTRL_10_VAL, base + EMIF_EXT_PHY_CTRL_10_SHDW);
  642. writel(EMIF_EXT_PHY_CTRL_11_VAL, base + EMIF_EXT_PHY_CTRL_11_SHDW);
  643. writel(EMIF_EXT_PHY_CTRL_12_VAL, base + EMIF_EXT_PHY_CTRL_12_SHDW);
  644. writel(EMIF_EXT_PHY_CTRL_13_VAL, base + EMIF_EXT_PHY_CTRL_13_SHDW);
  645. writel(EMIF_EXT_PHY_CTRL_14_VAL, base + EMIF_EXT_PHY_CTRL_14_SHDW);
  646. writel(EMIF_EXT_PHY_CTRL_15_VAL, base + EMIF_EXT_PHY_CTRL_15_SHDW);
  647. writel(EMIF_EXT_PHY_CTRL_16_VAL, base + EMIF_EXT_PHY_CTRL_16_SHDW);
  648. writel(EMIF_EXT_PHY_CTRL_17_VAL, base + EMIF_EXT_PHY_CTRL_17_SHDW);
  649. writel(EMIF_EXT_PHY_CTRL_18_VAL, base + EMIF_EXT_PHY_CTRL_18_SHDW);
  650. writel(EMIF_EXT_PHY_CTRL_19_VAL, base + EMIF_EXT_PHY_CTRL_19_SHDW);
  651. writel(EMIF_EXT_PHY_CTRL_20_VAL, base + EMIF_EXT_PHY_CTRL_20_SHDW);
  652. writel(EMIF_EXT_PHY_CTRL_21_VAL, base + EMIF_EXT_PHY_CTRL_21_SHDW);
  653. writel(EMIF_EXT_PHY_CTRL_22_VAL, base + EMIF_EXT_PHY_CTRL_22_SHDW);
  654. writel(EMIF_EXT_PHY_CTRL_23_VAL, base + EMIF_EXT_PHY_CTRL_23_SHDW);
  655. writel(EMIF_EXT_PHY_CTRL_24_VAL, base + EMIF_EXT_PHY_CTRL_24_SHDW);
  656. }
  657. static void get_default_timings(struct emif_data *emif)
  658. {
  659. struct emif_platform_data *pd = emif->plat_data;
  660. pd->timings = lpddr2_jedec_timings;
  661. pd->timings_arr_size = ARRAY_SIZE(lpddr2_jedec_timings);
  662. dev_warn(emif->dev, "%s: using default timings\n", __func__);
  663. }
  664. static int is_dev_data_valid(u32 type, u32 density, u32 io_width, u32 phy_type,
  665. u32 ip_rev, struct device *dev)
  666. {
  667. int valid;
  668. valid = (type == DDR_TYPE_LPDDR2_S4 ||
  669. type == DDR_TYPE_LPDDR2_S2)
  670. && (density >= DDR_DENSITY_64Mb
  671. && density <= DDR_DENSITY_8Gb)
  672. && (io_width >= DDR_IO_WIDTH_8
  673. && io_width <= DDR_IO_WIDTH_32);
  674. /* Combinations of EMIF and PHY revisions that we support today */
  675. switch (ip_rev) {
  676. case EMIF_4D:
  677. valid = valid && (phy_type == EMIF_PHY_TYPE_ATTILAPHY);
  678. break;
  679. case EMIF_4D5:
  680. valid = valid && (phy_type == EMIF_PHY_TYPE_INTELLIPHY);
  681. break;
  682. default:
  683. valid = 0;
  684. }
  685. if (!valid)
  686. dev_err(dev, "%s: invalid DDR details\n", __func__);
  687. return valid;
  688. }
  689. static int is_custom_config_valid(struct emif_custom_configs *cust_cfgs,
  690. struct device *dev)
  691. {
  692. int valid = 1;
  693. if ((cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE) &&
  694. (cust_cfgs->lpmode != EMIF_LP_MODE_DISABLE))
  695. valid = cust_cfgs->lpmode_freq_threshold &&
  696. cust_cfgs->lpmode_timeout_performance &&
  697. cust_cfgs->lpmode_timeout_power;
  698. if (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL)
  699. valid = valid && cust_cfgs->temp_alert_poll_interval_ms;
  700. if (!valid)
  701. dev_warn(dev, "%s: invalid custom configs\n", __func__);
  702. return valid;
  703. }
  704. static void of_get_custom_configs(struct device_node *np_emif,
  705. struct emif_data *emif)
  706. {
  707. struct emif_custom_configs *cust_cfgs = NULL;
  708. int len;
  709. const __be32 *lpmode, *poll_intvl;
  710. lpmode = of_get_property(np_emif, "low-power-mode", &len);
  711. poll_intvl = of_get_property(np_emif, "temp-alert-poll-interval", &len);
  712. if (lpmode || poll_intvl)
  713. cust_cfgs = devm_kzalloc(emif->dev, sizeof(*cust_cfgs),
  714. GFP_KERNEL);
  715. if (!cust_cfgs)
  716. return;
  717. if (lpmode) {
  718. cust_cfgs->mask |= EMIF_CUSTOM_CONFIG_LPMODE;
  719. cust_cfgs->lpmode = be32_to_cpup(lpmode);
  720. of_property_read_u32(np_emif,
  721. "low-power-mode-timeout-performance",
  722. &cust_cfgs->lpmode_timeout_performance);
  723. of_property_read_u32(np_emif,
  724. "low-power-mode-timeout-power",
  725. &cust_cfgs->lpmode_timeout_power);
  726. of_property_read_u32(np_emif,
  727. "low-power-mode-freq-threshold",
  728. &cust_cfgs->lpmode_freq_threshold);
  729. }
  730. if (poll_intvl) {
  731. cust_cfgs->mask |=
  732. EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL;
  733. cust_cfgs->temp_alert_poll_interval_ms =
  734. be32_to_cpup(poll_intvl);
  735. }
  736. if (of_property_read_bool(np_emif, "extended-temp-part"))
  737. cust_cfgs->mask |= EMIF_CUSTOM_CONFIG_EXTENDED_TEMP_PART;
  738. if (!is_custom_config_valid(cust_cfgs, emif->dev)) {
  739. devm_kfree(emif->dev, cust_cfgs);
  740. return;
  741. }
  742. emif->plat_data->custom_configs = cust_cfgs;
  743. }
  744. static void of_get_ddr_info(struct device_node *np_emif,
  745. struct device_node *np_ddr,
  746. struct ddr_device_info *dev_info)
  747. {
  748. u32 density = 0, io_width = 0;
  749. dev_info->cs1_used = of_property_read_bool(np_emif, "cs1-used");
  750. dev_info->cal_resistors_per_cs = of_property_read_bool(np_emif, "cal-resistor-per-cs");
  751. if (of_device_is_compatible(np_ddr, "jedec,lpddr2-s4"))
  752. dev_info->type = DDR_TYPE_LPDDR2_S4;
  753. else if (of_device_is_compatible(np_ddr, "jedec,lpddr2-s2"))
  754. dev_info->type = DDR_TYPE_LPDDR2_S2;
  755. of_property_read_u32(np_ddr, "density", &density);
  756. of_property_read_u32(np_ddr, "io-width", &io_width);
  757. /* Convert from density in Mb to the density encoding in jedc_ddr.h */
  758. if (density & (density - 1))
  759. dev_info->density = 0;
  760. else
  761. dev_info->density = __fls(density) - 5;
  762. /* Convert from io_width in bits to io_width encoding in jedc_ddr.h */
  763. if (io_width & (io_width - 1))
  764. dev_info->io_width = 0;
  765. else
  766. dev_info->io_width = __fls(io_width) - 1;
  767. }
  768. static struct emif_data *of_get_memory_device_details(
  769. struct device_node *np_emif, struct device *dev)
  770. {
  771. struct emif_data *emif = NULL;
  772. struct ddr_device_info *dev_info = NULL;
  773. struct emif_platform_data *pd = NULL;
  774. struct device_node *np_ddr;
  775. np_ddr = of_parse_phandle(np_emif, "device-handle", 0);
  776. if (!np_ddr)
  777. goto error;
  778. emif = devm_kzalloc(dev, sizeof(struct emif_data), GFP_KERNEL);
  779. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  780. dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
  781. if (!emif || !pd || !dev_info) {
  782. dev_err(dev, "%s: Out of memory!!\n",
  783. __func__);
  784. goto error;
  785. }
  786. emif->plat_data = pd;
  787. pd->device_info = dev_info;
  788. emif->dev = dev;
  789. emif->np_ddr = np_ddr;
  790. emif->temperature_level = SDRAM_TEMP_NOMINAL;
  791. if (of_device_is_compatible(np_emif, "ti,emif-4d"))
  792. emif->plat_data->ip_rev = EMIF_4D;
  793. else if (of_device_is_compatible(np_emif, "ti,emif-4d5"))
  794. emif->plat_data->ip_rev = EMIF_4D5;
  795. of_property_read_u32(np_emif, "phy-type", &pd->phy_type);
  796. if (of_property_read_bool(np_emif, "hw-caps-ll-interface"))
  797. pd->hw_caps |= EMIF_HW_CAPS_LL_INTERFACE;
  798. of_get_ddr_info(np_emif, np_ddr, dev_info);
  799. if (!is_dev_data_valid(pd->device_info->type, pd->device_info->density,
  800. pd->device_info->io_width, pd->phy_type, pd->ip_rev,
  801. emif->dev)) {
  802. dev_err(dev, "%s: invalid device data!!\n", __func__);
  803. goto error;
  804. }
  805. /*
  806. * For EMIF instances other than EMIF1 see if the devices connected
  807. * are exactly same as on EMIF1(which is typically the case). If so,
  808. * mark it as a duplicate of EMIF1. This will save some memory and
  809. * computation.
  810. */
  811. if (emif1 && emif1->np_ddr == np_ddr) {
  812. emif->duplicate = true;
  813. goto out;
  814. } else if (emif1) {
  815. dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
  816. __func__);
  817. }
  818. of_get_custom_configs(np_emif, emif);
  819. emif->plat_data->timings = of_get_ddr_timings(np_ddr, emif->dev,
  820. emif->plat_data->device_info->type,
  821. &emif->plat_data->timings_arr_size);
  822. emif->plat_data->min_tck = of_get_min_tck(np_ddr, emif->dev);
  823. goto out;
  824. error:
  825. return NULL;
  826. out:
  827. return emif;
  828. }
  829. static struct emif_data *get_device_details(
  830. struct platform_device *pdev)
  831. {
  832. u32 size;
  833. struct emif_data *emif = NULL;
  834. struct ddr_device_info *dev_info;
  835. struct emif_custom_configs *cust_cfgs;
  836. struct emif_platform_data *pd;
  837. struct device *dev;
  838. void *temp;
  839. pd = pdev->dev.platform_data;
  840. dev = &pdev->dev;
  841. if (!(pd && pd->device_info && is_dev_data_valid(pd->device_info->type,
  842. pd->device_info->density, pd->device_info->io_width,
  843. pd->phy_type, pd->ip_rev, dev))) {
  844. dev_err(dev, "%s: invalid device data\n", __func__);
  845. goto error;
  846. }
  847. emif = devm_kzalloc(dev, sizeof(*emif), GFP_KERNEL);
  848. temp = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  849. dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
  850. if (!emif || !temp || !dev_info)
  851. goto error;
  852. memcpy(temp, pd, sizeof(*pd));
  853. pd = temp;
  854. memcpy(dev_info, pd->device_info, sizeof(*dev_info));
  855. pd->device_info = dev_info;
  856. emif->plat_data = pd;
  857. emif->dev = dev;
  858. emif->temperature_level = SDRAM_TEMP_NOMINAL;
  859. /*
  860. * For EMIF instances other than EMIF1 see if the devices connected
  861. * are exactly same as on EMIF1(which is typically the case). If so,
  862. * mark it as a duplicate of EMIF1 and skip copying timings data.
  863. * This will save some memory and some computation later.
  864. */
  865. emif->duplicate = emif1 && (memcmp(dev_info,
  866. emif1->plat_data->device_info,
  867. sizeof(struct ddr_device_info)) == 0);
  868. if (emif->duplicate) {
  869. pd->timings = NULL;
  870. pd->min_tck = NULL;
  871. goto out;
  872. } else if (emif1) {
  873. dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
  874. __func__);
  875. }
  876. /*
  877. * Copy custom configs - ignore allocation error, if any, as
  878. * custom_configs is not very critical
  879. */
  880. cust_cfgs = pd->custom_configs;
  881. if (cust_cfgs && is_custom_config_valid(cust_cfgs, dev)) {
  882. temp = devm_kzalloc(dev, sizeof(*cust_cfgs), GFP_KERNEL);
  883. if (temp)
  884. memcpy(temp, cust_cfgs, sizeof(*cust_cfgs));
  885. pd->custom_configs = temp;
  886. }
  887. /*
  888. * Copy timings and min-tck values from platform data. If it is not
  889. * available or if memory allocation fails, use JEDEC defaults
  890. */
  891. size = sizeof(struct lpddr2_timings) * pd->timings_arr_size;
  892. if (pd->timings) {
  893. temp = devm_kzalloc(dev, size, GFP_KERNEL);
  894. if (temp) {
  895. memcpy(temp, pd->timings, size);
  896. pd->timings = temp;
  897. } else {
  898. get_default_timings(emif);
  899. }
  900. } else {
  901. get_default_timings(emif);
  902. }
  903. if (pd->min_tck) {
  904. temp = devm_kzalloc(dev, sizeof(*pd->min_tck), GFP_KERNEL);
  905. if (temp) {
  906. memcpy(temp, pd->min_tck, sizeof(*pd->min_tck));
  907. pd->min_tck = temp;
  908. } else {
  909. pd->min_tck = &lpddr2_jedec_min_tck;
  910. }
  911. } else {
  912. pd->min_tck = &lpddr2_jedec_min_tck;
  913. }
  914. out:
  915. return emif;
  916. error:
  917. return NULL;
  918. }
  919. static int emif_probe(struct platform_device *pdev)
  920. {
  921. struct emif_data *emif;
  922. int irq, ret;
  923. if (pdev->dev.of_node)
  924. emif = of_get_memory_device_details(pdev->dev.of_node, &pdev->dev);
  925. else
  926. emif = get_device_details(pdev);
  927. if (!emif) {
  928. pr_err("%s: error getting device data\n", __func__);
  929. goto error;
  930. }
  931. list_add(&emif->node, &device_list);
  932. /* Save pointers to each other in emif and device structures */
  933. emif->dev = &pdev->dev;
  934. platform_set_drvdata(pdev, emif);
  935. emif->base = devm_platform_ioremap_resource(pdev, 0);
  936. if (IS_ERR(emif->base))
  937. goto error;
  938. irq = platform_get_irq(pdev, 0);
  939. if (irq < 0)
  940. goto error;
  941. emif_onetime_settings(emif);
  942. emif_debugfs_init(emif);
  943. disable_and_clear_all_interrupts(emif);
  944. ret = setup_interrupts(emif, irq);
  945. if (ret)
  946. goto error;
  947. /* One-time actions taken on probing the first device */
  948. if (!emif1) {
  949. emif1 = emif;
  950. /*
  951. * TODO: register notifiers for frequency and voltage
  952. * change here once the respective frameworks are
  953. * available
  954. */
  955. }
  956. dev_info(&pdev->dev, "%s: device configured with addr = %p and IRQ%d\n",
  957. __func__, emif->base, irq);
  958. return 0;
  959. error:
  960. return -ENODEV;
  961. }
  962. static void emif_remove(struct platform_device *pdev)
  963. {
  964. struct emif_data *emif = platform_get_drvdata(pdev);
  965. emif_debugfs_exit(emif);
  966. }
  967. static void emif_shutdown(struct platform_device *pdev)
  968. {
  969. struct emif_data *emif = platform_get_drvdata(pdev);
  970. disable_and_clear_all_interrupts(emif);
  971. }
  972. #if defined(CONFIG_OF)
  973. static const struct of_device_id emif_of_match[] = {
  974. { .compatible = "ti,emif-4d" },
  975. { .compatible = "ti,emif-4d5" },
  976. {},
  977. };
  978. MODULE_DEVICE_TABLE(of, emif_of_match);
  979. #endif
  980. static struct platform_driver emif_driver = {
  981. .probe = emif_probe,
  982. .remove = emif_remove,
  983. .shutdown = emif_shutdown,
  984. .driver = {
  985. .name = "emif",
  986. .of_match_table = of_match_ptr(emif_of_match),
  987. },
  988. };
  989. module_platform_driver(emif_driver);
  990. MODULE_DESCRIPTION("TI EMIF SDRAM Controller Driver");
  991. MODULE_LICENSE("GPL");
  992. MODULE_ALIAS("platform:emif");
  993. MODULE_AUTHOR("Texas Instruments Inc");