emif.c 34 KB

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