1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27ec94453SAneesh V /*
37ec94453SAneesh V * EMIF driver
47ec94453SAneesh V *
57ec94453SAneesh V * Copyright (C) 2012 Texas Instruments, Inc.
67ec94453SAneesh V *
77ec94453SAneesh V * Aneesh V <aneesh@ti.com>
87ec94453SAneesh V * Santosh Shilimkar <santosh.shilimkar@ti.com>
97ec94453SAneesh V */
1006303c2eSThierry Reding #include <linux/err.h>
117ec94453SAneesh V #include <linux/kernel.h>
127ec94453SAneesh V #include <linux/reboot.h>
137ec94453SAneesh V #include <linux/platform_data/emif_plat.h>
147ec94453SAneesh V #include <linux/io.h>
157ec94453SAneesh V #include <linux/device.h>
167ec94453SAneesh V #include <linux/platform_device.h>
177ec94453SAneesh V #include <linux/interrupt.h>
187ec94453SAneesh V #include <linux/slab.h>
19e6b42eb6SAneesh V #include <linux/of.h>
20aac10aaaSAneesh V #include <linux/debugfs.h>
217ec94453SAneesh V #include <linux/seq_file.h>
227ec94453SAneesh V #include <linux/module.h>
237ec94453SAneesh V #include <linux/list.h>
24a93de288SAneesh V #include <linux/spinlock.h>
252553e32aSNishanth Menon #include <linux/pm.h>
265ec47cdaSMasahiro Yamada
277ec94453SAneesh V #include "emif.h"
285ec47cdaSMasahiro Yamada #include "jedec_ddr.h"
29e6b42eb6SAneesh V #include "of_memory.h"
307ec94453SAneesh V
317ec94453SAneesh V /**
327ec94453SAneesh V * struct emif_data - Per device static data for driver's use
337ec94453SAneesh V * @duplicate: Whether the DDR devices attached to this EMIF
347ec94453SAneesh V * instance are exactly same as that on EMIF1. In
357ec94453SAneesh V * this case we can save some memory and processing
367ec94453SAneesh V * @temperature_level: Maximum temperature of LPDDR2 devices attached
377ec94453SAneesh V * to this EMIF - read from MR4 register. If there
387ec94453SAneesh V * are two devices attached to this EMIF, this
397ec94453SAneesh V * value is the maximum of the two temperature
407ec94453SAneesh V * levels.
417ec94453SAneesh V * @node: node in the device list
427ec94453SAneesh V * @base: base address of memory-mapped IO registers.
437ec94453SAneesh V * @dev: device pointer.
44a93de288SAneesh V * @regs_cache: An array of 'struct emif_regs' that stores
45a93de288SAneesh V * calculated register values for different
46a93de288SAneesh V * frequencies, to avoid re-calculating them on
47a93de288SAneesh V * each DVFS transition.
48a93de288SAneesh V * @curr_regs: The set of register values used in the last
49a93de288SAneesh V * frequency change (i.e. corresponding to the
50a93de288SAneesh V * frequency in effect at the moment)
517ec94453SAneesh V * @plat_data: Pointer to saved platform data.
52aac10aaaSAneesh V * @debugfs_root: dentry to the root folder for EMIF in debugfs
53e6b42eb6SAneesh V * @np_ddr: Pointer to ddr device tree node
547ec94453SAneesh V */
557ec94453SAneesh V struct emif_data {
567ec94453SAneesh V u8 duplicate;
577ec94453SAneesh V u8 temperature_level;
58a93de288SAneesh V u8 lpmode;
597ec94453SAneesh V struct list_head node;
60a93de288SAneesh V unsigned long irq_state;
617ec94453SAneesh V void __iomem *base;
627ec94453SAneesh V struct device *dev;
63a93de288SAneesh V struct emif_regs *regs_cache[EMIF_MAX_NUM_FREQUENCIES];
64a93de288SAneesh V struct emif_regs *curr_regs;
657ec94453SAneesh V struct emif_platform_data *plat_data;
66aac10aaaSAneesh V struct dentry *debugfs_root;
67e6b42eb6SAneesh V struct device_node *np_ddr;
687ec94453SAneesh V };
697ec94453SAneesh V
707ec94453SAneesh V static struct emif_data *emif1;
71bd96a89cSZheng Yongjun static DEFINE_SPINLOCK(emif_lock);
72a93de288SAneesh V static unsigned long irq_state;
737ec94453SAneesh V static LIST_HEAD(device_list);
747ec94453SAneesh V
75e5445ee6SAxel Lin #ifdef CONFIG_DEBUG_FS
do_emif_regdump_show(struct seq_file * s,struct emif_data * emif,struct emif_regs * regs)76aac10aaaSAneesh V static void do_emif_regdump_show(struct seq_file *s, struct emif_data *emif,
77aac10aaaSAneesh V struct emif_regs *regs)
78aac10aaaSAneesh V {
79aac10aaaSAneesh V u32 type = emif->plat_data->device_info->type;
80aac10aaaSAneesh V u32 ip_rev = emif->plat_data->ip_rev;
81aac10aaaSAneesh V
82aac10aaaSAneesh V seq_printf(s, "EMIF register cache dump for %dMHz\n",
83aac10aaaSAneesh V regs->freq/1000000);
84aac10aaaSAneesh V
85aac10aaaSAneesh V seq_printf(s, "ref_ctrl_shdw\t: 0x%08x\n", regs->ref_ctrl_shdw);
86aac10aaaSAneesh V seq_printf(s, "sdram_tim1_shdw\t: 0x%08x\n", regs->sdram_tim1_shdw);
87aac10aaaSAneesh V seq_printf(s, "sdram_tim2_shdw\t: 0x%08x\n", regs->sdram_tim2_shdw);
88aac10aaaSAneesh V seq_printf(s, "sdram_tim3_shdw\t: 0x%08x\n", regs->sdram_tim3_shdw);
89aac10aaaSAneesh V
90aac10aaaSAneesh V if (ip_rev == EMIF_4D) {
91aac10aaaSAneesh V seq_printf(s, "read_idle_ctrl_shdw_normal\t: 0x%08x\n",
92aac10aaaSAneesh V regs->read_idle_ctrl_shdw_normal);
93aac10aaaSAneesh V seq_printf(s, "read_idle_ctrl_shdw_volt_ramp\t: 0x%08x\n",
94aac10aaaSAneesh V regs->read_idle_ctrl_shdw_volt_ramp);
95aac10aaaSAneesh V } else if (ip_rev == EMIF_4D5) {
96aac10aaaSAneesh V seq_printf(s, "dll_calib_ctrl_shdw_normal\t: 0x%08x\n",
97aac10aaaSAneesh V regs->dll_calib_ctrl_shdw_normal);
98aac10aaaSAneesh V seq_printf(s, "dll_calib_ctrl_shdw_volt_ramp\t: 0x%08x\n",
99aac10aaaSAneesh V regs->dll_calib_ctrl_shdw_volt_ramp);
100aac10aaaSAneesh V }
101aac10aaaSAneesh V
102aac10aaaSAneesh V if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4) {
103aac10aaaSAneesh V seq_printf(s, "ref_ctrl_shdw_derated\t: 0x%08x\n",
104aac10aaaSAneesh V regs->ref_ctrl_shdw_derated);
105aac10aaaSAneesh V seq_printf(s, "sdram_tim1_shdw_derated\t: 0x%08x\n",
106aac10aaaSAneesh V regs->sdram_tim1_shdw_derated);
107aac10aaaSAneesh V seq_printf(s, "sdram_tim3_shdw_derated\t: 0x%08x\n",
108aac10aaaSAneesh V regs->sdram_tim3_shdw_derated);
109aac10aaaSAneesh V }
110aac10aaaSAneesh V }
111aac10aaaSAneesh V
emif_regdump_show(struct seq_file * s,void * unused)112aac10aaaSAneesh V static int emif_regdump_show(struct seq_file *s, void *unused)
113aac10aaaSAneesh V {
114aac10aaaSAneesh V struct emif_data *emif = s->private;
115aac10aaaSAneesh V struct emif_regs **regs_cache;
116aac10aaaSAneesh V int i;
117aac10aaaSAneesh V
118aac10aaaSAneesh V if (emif->duplicate)
119aac10aaaSAneesh V regs_cache = emif1->regs_cache;
120aac10aaaSAneesh V else
121aac10aaaSAneesh V regs_cache = emif->regs_cache;
122aac10aaaSAneesh V
123aac10aaaSAneesh V for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) {
124aac10aaaSAneesh V do_emif_regdump_show(s, emif, regs_cache[i]);
125d363a88bSSF Markus Elfring seq_putc(s, '\n');
126aac10aaaSAneesh V }
127aac10aaaSAneesh V
128aac10aaaSAneesh V return 0;
129aac10aaaSAneesh V }
130aac10aaaSAneesh V
13194ca8573SQinglang Miao DEFINE_SHOW_ATTRIBUTE(emif_regdump);
132aac10aaaSAneesh V
emif_mr4_show(struct seq_file * s,void * unused)133aac10aaaSAneesh V static int emif_mr4_show(struct seq_file *s, void *unused)
134aac10aaaSAneesh V {
135aac10aaaSAneesh V struct emif_data *emif = s->private;
136aac10aaaSAneesh V
137aac10aaaSAneesh V seq_printf(s, "MR4=%d\n", emif->temperature_level);
138aac10aaaSAneesh V return 0;
139aac10aaaSAneesh V }
140aac10aaaSAneesh V
14194ca8573SQinglang Miao DEFINE_SHOW_ATTRIBUTE(emif_mr4);
142aac10aaaSAneesh V
emif_debugfs_init(struct emif_data * emif)143aac10aaaSAneesh V static int __init_or_module emif_debugfs_init(struct emif_data *emif)
144aac10aaaSAneesh V {
145fd227816SDan Carpenter emif->debugfs_root = debugfs_create_dir(dev_name(emif->dev), NULL);
146fd227816SDan Carpenter debugfs_create_file("regcache_dump", S_IRUGO, emif->debugfs_root, emif,
147fd227816SDan Carpenter &emif_regdump_fops);
148fd227816SDan Carpenter debugfs_create_file("mr4", S_IRUGO, emif->debugfs_root, emif,
149fd227816SDan Carpenter &emif_mr4_fops);
150aac10aaaSAneesh V return 0;
151aac10aaaSAneesh V }
152aac10aaaSAneesh V
emif_debugfs_exit(struct emif_data * emif)153aac10aaaSAneesh V static void __exit emif_debugfs_exit(struct emif_data *emif)
154aac10aaaSAneesh V {
155aac10aaaSAneesh V debugfs_remove_recursive(emif->debugfs_root);
156aac10aaaSAneesh V emif->debugfs_root = NULL;
157aac10aaaSAneesh V }
158e5445ee6SAxel Lin #else
emif_debugfs_init(struct emif_data * emif)159e5445ee6SAxel Lin static inline int __init_or_module emif_debugfs_init(struct emif_data *emif)
160e5445ee6SAxel Lin {
161e5445ee6SAxel Lin return 0;
162e5445ee6SAxel Lin }
163e5445ee6SAxel Lin
emif_debugfs_exit(struct emif_data * emif)164e5445ee6SAxel Lin static inline void __exit emif_debugfs_exit(struct emif_data *emif)
165e5445ee6SAxel Lin {
166e5445ee6SAxel Lin }
167e5445ee6SAxel Lin #endif
168aac10aaaSAneesh V
169a93de288SAneesh V /*
17098231c4fSAneesh V * Get bus width used by EMIF. Note that this may be different from the
17198231c4fSAneesh V * bus width of the DDR devices used. For instance two 16-bit DDR devices
17298231c4fSAneesh V * may be connected to a given CS of EMIF. In this case bus width as far
17398231c4fSAneesh V * as EMIF is concerned is 32, where as the DDR bus width is 16 bits.
17498231c4fSAneesh V */
get_emif_bus_width(struct emif_data * emif)17598231c4fSAneesh V static u32 get_emif_bus_width(struct emif_data *emif)
17698231c4fSAneesh V {
17798231c4fSAneesh V u32 width;
17898231c4fSAneesh V void __iomem *base = emif->base;
17998231c4fSAneesh V
18098231c4fSAneesh V width = (readl(base + EMIF_SDRAM_CONFIG) & NARROW_MODE_MASK)
18198231c4fSAneesh V >> NARROW_MODE_SHIFT;
18298231c4fSAneesh V width = width == 0 ? 32 : 16;
18398231c4fSAneesh V
18498231c4fSAneesh V return width;
18598231c4fSAneesh V }
18698231c4fSAneesh V
set_lpmode(struct emif_data * emif,u8 lpmode)187a93de288SAneesh V static void set_lpmode(struct emif_data *emif, u8 lpmode)
188a93de288SAneesh V {
189a93de288SAneesh V u32 temp;
190a93de288SAneesh V void __iomem *base = emif->base;
191a93de288SAneesh V
192f02503b2SGrygorii Strashko /*
193f02503b2SGrygorii Strashko * Workaround for errata i743 - LPDDR2 Power-Down State is Not
194f02503b2SGrygorii Strashko * Efficient
195f02503b2SGrygorii Strashko *
196f02503b2SGrygorii Strashko * i743 DESCRIPTION:
197f02503b2SGrygorii Strashko * The EMIF supports power-down state for low power. The EMIF
198f02503b2SGrygorii Strashko * automatically puts the SDRAM into power-down after the memory is
199f02503b2SGrygorii Strashko * not accessed for a defined number of cycles and the
200f02503b2SGrygorii Strashko * EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field is set to 0x4.
201f02503b2SGrygorii Strashko * As the EMIF supports automatic output impedance calibration, a ZQ
202f02503b2SGrygorii Strashko * calibration long command is issued every time it exits active
203f02503b2SGrygorii Strashko * power-down and precharge power-down modes. The EMIF waits and
204f02503b2SGrygorii Strashko * blocks any other command during this calibration.
205f02503b2SGrygorii Strashko * The EMIF does not allow selective disabling of ZQ calibration upon
206f02503b2SGrygorii Strashko * exit of power-down mode. Due to very short periods of power-down
207f02503b2SGrygorii Strashko * cycles, ZQ calibration overhead creates bandwidth issues and
208f02503b2SGrygorii Strashko * increases overall system power consumption. On the other hand,
209f02503b2SGrygorii Strashko * issuing ZQ calibration long commands when exiting self-refresh is
210f02503b2SGrygorii Strashko * still required.
211f02503b2SGrygorii Strashko *
212f02503b2SGrygorii Strashko * WORKAROUND
213f02503b2SGrygorii Strashko * Because there is no power consumption benefit of the power-down due
214f02503b2SGrygorii Strashko * to the calibration and there is a performance risk, the guideline
215f02503b2SGrygorii Strashko * is to not allow power-down state and, therefore, to not have set
216f02503b2SGrygorii Strashko * the EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field to 0x4.
217f02503b2SGrygorii Strashko */
218f02503b2SGrygorii Strashko if ((emif->plat_data->ip_rev == EMIF_4D) &&
219ec6652bdSKrzysztof Kozlowski (lpmode == EMIF_LP_MODE_PWR_DN)) {
220f02503b2SGrygorii Strashko WARN_ONCE(1,
22128dc76f1SKrzysztof Kozlowski "REG_LP_MODE = LP_MODE_PWR_DN(4) is prohibited by erratum i743 switch to LP_MODE_SELF_REFRESH(2)\n");
222f02503b2SGrygorii Strashko /* rollback LP_MODE to Self-refresh mode */
223f02503b2SGrygorii Strashko lpmode = EMIF_LP_MODE_SELF_REFRESH;
224f02503b2SGrygorii Strashko }
225f02503b2SGrygorii Strashko
226a93de288SAneesh V temp = readl(base + EMIF_POWER_MANAGEMENT_CONTROL);
227a93de288SAneesh V temp &= ~LP_MODE_MASK;
228a93de288SAneesh V temp |= (lpmode << LP_MODE_SHIFT);
229a93de288SAneesh V writel(temp, base + EMIF_POWER_MANAGEMENT_CONTROL);
230a93de288SAneesh V }
231a93de288SAneesh V
do_freq_update(void)232a93de288SAneesh V static void do_freq_update(void)
233a93de288SAneesh V {
234a93de288SAneesh V struct emif_data *emif;
235a93de288SAneesh V
236a93de288SAneesh V /*
237a93de288SAneesh V * Workaround for errata i728: Disable LPMODE during FREQ_UPDATE
238a93de288SAneesh V *
239a93de288SAneesh V * i728 DESCRIPTION:
240a93de288SAneesh V * The EMIF automatically puts the SDRAM into self-refresh mode
241a93de288SAneesh V * after the EMIF has not performed accesses during
242a93de288SAneesh V * EMIF_PWR_MGMT_CTRL[7:4] REG_SR_TIM number of DDR clock cycles
243a93de288SAneesh V * and the EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field is set
244a93de288SAneesh V * to 0x2. If during a small window the following three events
245a93de288SAneesh V * occur:
246a93de288SAneesh V * - The SR_TIMING counter expires
247a93de288SAneesh V * - And frequency change is requested
248a93de288SAneesh V * - And OCP access is requested
249a93de288SAneesh V * Then it causes instable clock on the DDR interface.
250a93de288SAneesh V *
251a93de288SAneesh V * WORKAROUND
252a93de288SAneesh V * To avoid the occurrence of the three events, the workaround
253a93de288SAneesh V * is to disable the self-refresh when requesting a frequency
254a93de288SAneesh V * change. Before requesting a frequency change the software must
255a93de288SAneesh V * program EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x0. When the
256a93de288SAneesh V * frequency change has been done, the software can reprogram
257a93de288SAneesh V * EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x2
258a93de288SAneesh V */
259a93de288SAneesh V list_for_each_entry(emif, &device_list, node) {
260a93de288SAneesh V if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
261a93de288SAneesh V set_lpmode(emif, EMIF_LP_MODE_DISABLE);
262a93de288SAneesh V }
263a93de288SAneesh V
264a93de288SAneesh V /*
265a93de288SAneesh V * TODO: Do FREQ_UPDATE here when an API
266a93de288SAneesh V * is available for this as part of the new
267a93de288SAneesh V * clock framework
268a93de288SAneesh V */
269a93de288SAneesh V
270a93de288SAneesh V list_for_each_entry(emif, &device_list, node) {
271a93de288SAneesh V if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
272a93de288SAneesh V set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH);
273a93de288SAneesh V }
274a93de288SAneesh V }
275a93de288SAneesh V
276a93de288SAneesh V /* Find addressing table entry based on the device's type and density */
get_addressing_table(const struct ddr_device_info * device_info)277a93de288SAneesh V static const struct lpddr2_addressing *get_addressing_table(
278a93de288SAneesh V const struct ddr_device_info *device_info)
279a93de288SAneesh V {
280a93de288SAneesh V u32 index, type, density;
281a93de288SAneesh V
282a93de288SAneesh V type = device_info->type;
283a93de288SAneesh V density = device_info->density;
284a93de288SAneesh V
285a93de288SAneesh V switch (type) {
286a93de288SAneesh V case DDR_TYPE_LPDDR2_S4:
287a93de288SAneesh V index = density - 1;
288a93de288SAneesh V break;
289a93de288SAneesh V case DDR_TYPE_LPDDR2_S2:
290a93de288SAneesh V switch (density) {
291a93de288SAneesh V case DDR_DENSITY_1Gb:
292a93de288SAneesh V case DDR_DENSITY_2Gb:
293a93de288SAneesh V index = density + 3;
294a93de288SAneesh V break;
295a93de288SAneesh V default:
296a93de288SAneesh V index = density - 1;
297a93de288SAneesh V }
298a93de288SAneesh V break;
299a93de288SAneesh V default:
300a93de288SAneesh V return NULL;
301a93de288SAneesh V }
302a93de288SAneesh V
303a93de288SAneesh V return &lpddr2_jedec_addressing_table[index];
304a93de288SAneesh V }
305a93de288SAneesh V
get_zq_config_reg(const struct lpddr2_addressing * addressing,bool cs1_used,bool cal_resistors_per_cs)30698231c4fSAneesh V static u32 get_zq_config_reg(const struct lpddr2_addressing *addressing,
30798231c4fSAneesh V bool cs1_used, bool cal_resistors_per_cs)
30898231c4fSAneesh V {
30998231c4fSAneesh V u32 zq = 0, val = 0;
31098231c4fSAneesh V
31198231c4fSAneesh V val = EMIF_ZQCS_INTERVAL_US * 1000 / addressing->tREFI_ns;
31298231c4fSAneesh V zq |= val << ZQ_REFINTERVAL_SHIFT;
31398231c4fSAneesh V
31498231c4fSAneesh V val = DIV_ROUND_UP(T_ZQCL_DEFAULT_NS, T_ZQCS_DEFAULT_NS) - 1;
31598231c4fSAneesh V zq |= val << ZQ_ZQCL_MULT_SHIFT;
31698231c4fSAneesh V
31798231c4fSAneesh V val = DIV_ROUND_UP(T_ZQINIT_DEFAULT_NS, T_ZQCL_DEFAULT_NS) - 1;
31898231c4fSAneesh V zq |= val << ZQ_ZQINIT_MULT_SHIFT;
31998231c4fSAneesh V
32098231c4fSAneesh V zq |= ZQ_SFEXITEN_ENABLE << ZQ_SFEXITEN_SHIFT;
32198231c4fSAneesh V
32298231c4fSAneesh V if (cal_resistors_per_cs)
32398231c4fSAneesh V zq |= ZQ_DUALCALEN_ENABLE << ZQ_DUALCALEN_SHIFT;
32498231c4fSAneesh V else
32598231c4fSAneesh V zq |= ZQ_DUALCALEN_DISABLE << ZQ_DUALCALEN_SHIFT;
32698231c4fSAneesh V
32798231c4fSAneesh V zq |= ZQ_CS0EN_MASK; /* CS0 is used for sure */
32898231c4fSAneesh V
32998231c4fSAneesh V val = cs1_used ? 1 : 0;
33098231c4fSAneesh V zq |= val << ZQ_CS1EN_SHIFT;
33198231c4fSAneesh V
33298231c4fSAneesh V return zq;
33398231c4fSAneesh V }
33498231c4fSAneesh V
get_temp_alert_config(const struct lpddr2_addressing * addressing,const struct emif_custom_configs * custom_configs,bool cs1_used,u32 sdram_io_width,u32 emif_bus_width)33598231c4fSAneesh V static u32 get_temp_alert_config(const struct lpddr2_addressing *addressing,
33698231c4fSAneesh V const struct emif_custom_configs *custom_configs, bool cs1_used,
33798231c4fSAneesh V u32 sdram_io_width, u32 emif_bus_width)
33898231c4fSAneesh V {
33998231c4fSAneesh V u32 alert = 0, interval, devcnt;
34098231c4fSAneesh V
34198231c4fSAneesh V if (custom_configs && (custom_configs->mask &
34298231c4fSAneesh V EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL))
34398231c4fSAneesh V interval = custom_configs->temp_alert_poll_interval_ms;
34498231c4fSAneesh V else
34598231c4fSAneesh V interval = TEMP_ALERT_POLL_INTERVAL_DEFAULT_MS;
34698231c4fSAneesh V
34798231c4fSAneesh V interval *= 1000000; /* Convert to ns */
34898231c4fSAneesh V interval /= addressing->tREFI_ns; /* Convert to refresh cycles */
34998231c4fSAneesh V alert |= (interval << TA_REFINTERVAL_SHIFT);
35098231c4fSAneesh V
35198231c4fSAneesh V /*
35298231c4fSAneesh V * sdram_io_width is in 'log2(x) - 1' form. Convert emif_bus_width
35398231c4fSAneesh V * also to this form and subtract to get TA_DEVCNT, which is
35498231c4fSAneesh V * in log2(x) form.
35598231c4fSAneesh V */
35698231c4fSAneesh V emif_bus_width = __fls(emif_bus_width) - 1;
35798231c4fSAneesh V devcnt = emif_bus_width - sdram_io_width;
35898231c4fSAneesh V alert |= devcnt << TA_DEVCNT_SHIFT;
35998231c4fSAneesh V
36098231c4fSAneesh V /* DEVWDT is in 'log2(x) - 3' form */
36198231c4fSAneesh V alert |= (sdram_io_width - 2) << TA_DEVWDT_SHIFT;
36298231c4fSAneesh V
36398231c4fSAneesh V alert |= 1 << TA_SFEXITEN_SHIFT;
36498231c4fSAneesh V alert |= 1 << TA_CS0EN_SHIFT;
36598231c4fSAneesh V alert |= (cs1_used ? 1 : 0) << TA_CS1EN_SHIFT;
36698231c4fSAneesh V
36798231c4fSAneesh V return alert;
36898231c4fSAneesh V }
36998231c4fSAneesh V
get_pwr_mgmt_ctrl(u32 freq,struct emif_data * emif,u32 ip_rev)370a93de288SAneesh V static u32 get_pwr_mgmt_ctrl(u32 freq, struct emif_data *emif, u32 ip_rev)
371a93de288SAneesh V {
372a93de288SAneesh V u32 pwr_mgmt_ctrl = 0, timeout;
373a93de288SAneesh V u32 lpmode = EMIF_LP_MODE_SELF_REFRESH;
374a93de288SAneesh V u32 timeout_perf = EMIF_LP_MODE_TIMEOUT_PERFORMANCE;
375a93de288SAneesh V u32 timeout_pwr = EMIF_LP_MODE_TIMEOUT_POWER;
376a93de288SAneesh V u32 freq_threshold = EMIF_LP_MODE_FREQ_THRESHOLD;
37725aaacd2SNishanth Menon u32 mask;
37825aaacd2SNishanth Menon u8 shift;
379a93de288SAneesh V
380a93de288SAneesh V struct emif_custom_configs *cust_cfgs = emif->plat_data->custom_configs;
381a93de288SAneesh V
382a93de288SAneesh V if (cust_cfgs && (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE)) {
383a93de288SAneesh V lpmode = cust_cfgs->lpmode;
384a93de288SAneesh V timeout_perf = cust_cfgs->lpmode_timeout_performance;
385a93de288SAneesh V timeout_pwr = cust_cfgs->lpmode_timeout_power;
386a93de288SAneesh V freq_threshold = cust_cfgs->lpmode_freq_threshold;
387a93de288SAneesh V }
388a93de288SAneesh V
389a93de288SAneesh V /* Timeout based on DDR frequency */
390a93de288SAneesh V timeout = freq >= freq_threshold ? timeout_perf : timeout_pwr;
391a93de288SAneesh V
3920a5f19cfSLokesh Vutla /*
3930a5f19cfSLokesh Vutla * The value to be set in register is "log2(timeout) - 3"
3940a5f19cfSLokesh Vutla * if timeout < 16 load 0 in register
3950a5f19cfSLokesh Vutla * if timeout is not a power of 2, round to next highest power of 2
3960a5f19cfSLokesh Vutla */
397a93de288SAneesh V if (timeout < 16) {
398a93de288SAneesh V timeout = 0;
399a93de288SAneesh V } else {
400a93de288SAneesh V if (timeout & (timeout - 1))
4010a5f19cfSLokesh Vutla timeout <<= 1;
4020a5f19cfSLokesh Vutla timeout = __fls(timeout) - 3;
403a93de288SAneesh V }
404a93de288SAneesh V
405a93de288SAneesh V switch (lpmode) {
406a93de288SAneesh V case EMIF_LP_MODE_CLOCK_STOP:
40725aaacd2SNishanth Menon shift = CS_TIM_SHIFT;
40825aaacd2SNishanth Menon mask = CS_TIM_MASK;
409a93de288SAneesh V break;
410a93de288SAneesh V case EMIF_LP_MODE_SELF_REFRESH:
411a93de288SAneesh V /* Workaround for errata i735 */
412a93de288SAneesh V if (timeout < 6)
413a93de288SAneesh V timeout = 6;
414a93de288SAneesh V
41525aaacd2SNishanth Menon shift = SR_TIM_SHIFT;
41625aaacd2SNishanth Menon mask = SR_TIM_MASK;
417a93de288SAneesh V break;
418a93de288SAneesh V case EMIF_LP_MODE_PWR_DN:
41925aaacd2SNishanth Menon shift = PD_TIM_SHIFT;
42025aaacd2SNishanth Menon mask = PD_TIM_MASK;
421a93de288SAneesh V break;
422a93de288SAneesh V case EMIF_LP_MODE_DISABLE:
423a93de288SAneesh V default:
42425aaacd2SNishanth Menon mask = 0;
42525aaacd2SNishanth Menon shift = 0;
42625aaacd2SNishanth Menon break;
427a93de288SAneesh V }
42825aaacd2SNishanth Menon /* Round to maximum in case of overflow, BUT warn! */
42925aaacd2SNishanth Menon if (lpmode != EMIF_LP_MODE_DISABLE && timeout > mask >> shift) {
43025aaacd2SNishanth Menon pr_err("TIMEOUT Overflow - lpmode=%d perf=%d pwr=%d freq=%d\n",
43125aaacd2SNishanth Menon lpmode,
43225aaacd2SNishanth Menon timeout_perf,
43325aaacd2SNishanth Menon timeout_pwr,
43425aaacd2SNishanth Menon freq_threshold);
43525aaacd2SNishanth Menon WARN(1, "timeout=0x%02x greater than 0x%02x. Using max\n",
43625aaacd2SNishanth Menon timeout, mask >> shift);
43725aaacd2SNishanth Menon timeout = mask >> shift;
43825aaacd2SNishanth Menon }
43925aaacd2SNishanth Menon
44025aaacd2SNishanth Menon /* Setup required timing */
44125aaacd2SNishanth Menon pwr_mgmt_ctrl = (timeout << shift) & mask;
44225aaacd2SNishanth Menon /* setup a default mask for rest of the modes */
44325aaacd2SNishanth Menon pwr_mgmt_ctrl |= (SR_TIM_MASK | CS_TIM_MASK | PD_TIM_MASK) &
44425aaacd2SNishanth Menon ~mask;
445a93de288SAneesh V
446a93de288SAneesh V /* No CS_TIM in EMIF_4D5 */
447a93de288SAneesh V if (ip_rev == EMIF_4D5)
448a93de288SAneesh V pwr_mgmt_ctrl &= ~CS_TIM_MASK;
449a93de288SAneesh V
450a93de288SAneesh V pwr_mgmt_ctrl |= lpmode << LP_MODE_SHIFT;
451a93de288SAneesh V
452a93de288SAneesh V return pwr_mgmt_ctrl;
453a93de288SAneesh V }
454a93de288SAneesh V
455a93de288SAneesh V /*
45668b4aee3SAneesh V * Get the temperature level of the EMIF instance:
45768b4aee3SAneesh V * Reads the MR4 register of attached SDRAM parts to find out the temperature
45868b4aee3SAneesh V * level. If there are two parts attached(one on each CS), then the temperature
45968b4aee3SAneesh V * level for the EMIF instance is the higher of the two temperatures.
46068b4aee3SAneesh V */
get_temperature_level(struct emif_data * emif)46168b4aee3SAneesh V static void get_temperature_level(struct emif_data *emif)
46268b4aee3SAneesh V {
46368b4aee3SAneesh V u32 temp, temperature_level;
46468b4aee3SAneesh V void __iomem *base;
46568b4aee3SAneesh V
46668b4aee3SAneesh V base = emif->base;
46768b4aee3SAneesh V
46868b4aee3SAneesh V /* Read mode register 4 */
46968b4aee3SAneesh V writel(DDR_MR4, base + EMIF_LPDDR2_MODE_REG_CONFIG);
47068b4aee3SAneesh V temperature_level = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
47168b4aee3SAneesh V temperature_level = (temperature_level & MR4_SDRAM_REF_RATE_MASK) >>
47268b4aee3SAneesh V MR4_SDRAM_REF_RATE_SHIFT;
47368b4aee3SAneesh V
47468b4aee3SAneesh V if (emif->plat_data->device_info->cs1_used) {
47568b4aee3SAneesh V writel(DDR_MR4 | CS_MASK, base + EMIF_LPDDR2_MODE_REG_CONFIG);
47668b4aee3SAneesh V temp = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
47768b4aee3SAneesh V temp = (temp & MR4_SDRAM_REF_RATE_MASK)
47868b4aee3SAneesh V >> MR4_SDRAM_REF_RATE_SHIFT;
47968b4aee3SAneesh V temperature_level = max(temp, temperature_level);
48068b4aee3SAneesh V }
48168b4aee3SAneesh V
48268b4aee3SAneesh V /* treat everything less than nominal(3) in MR4 as nominal */
48368b4aee3SAneesh V if (unlikely(temperature_level < SDRAM_TEMP_NOMINAL))
48468b4aee3SAneesh V temperature_level = SDRAM_TEMP_NOMINAL;
48568b4aee3SAneesh V
48668b4aee3SAneesh V /* if we get reserved value in MR4 persist with the existing value */
48768b4aee3SAneesh V if (likely(temperature_level != SDRAM_TEMP_RESERVED_4))
48868b4aee3SAneesh V emif->temperature_level = temperature_level;
48968b4aee3SAneesh V }
49068b4aee3SAneesh V
49168b4aee3SAneesh V /*
492a93de288SAneesh V * setup_temperature_sensitive_regs() - set the timings for temperature
493a93de288SAneesh V * sensitive registers. This happens once at initialisation time based
494a93de288SAneesh V * on the temperature at boot time and subsequently based on the temperature
495a93de288SAneesh V * alert interrupt. Temperature alert can happen when the temperature
496a93de288SAneesh V * increases or drops. So this function can have the effect of either
497a93de288SAneesh V * derating the timings or going back to nominal values.
498a93de288SAneesh V */
setup_temperature_sensitive_regs(struct emif_data * emif,struct emif_regs * regs)499a93de288SAneesh V static void setup_temperature_sensitive_regs(struct emif_data *emif,
500a93de288SAneesh V struct emif_regs *regs)
501a93de288SAneesh V {
502a93de288SAneesh V u32 tim1, tim3, ref_ctrl, type;
503a93de288SAneesh V void __iomem *base = emif->base;
504a93de288SAneesh V u32 temperature;
505a93de288SAneesh V
506a93de288SAneesh V type = emif->plat_data->device_info->type;
507a93de288SAneesh V
508a93de288SAneesh V tim1 = regs->sdram_tim1_shdw;
509a93de288SAneesh V tim3 = regs->sdram_tim3_shdw;
510a93de288SAneesh V ref_ctrl = regs->ref_ctrl_shdw;
511a93de288SAneesh V
512a93de288SAneesh V /* No de-rating for non-lpddr2 devices */
513a93de288SAneesh V if (type != DDR_TYPE_LPDDR2_S2 && type != DDR_TYPE_LPDDR2_S4)
514a93de288SAneesh V goto out;
515a93de288SAneesh V
516a93de288SAneesh V temperature = emif->temperature_level;
517a93de288SAneesh V if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH) {
518a93de288SAneesh V ref_ctrl = regs->ref_ctrl_shdw_derated;
519a93de288SAneesh V } else if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH_AND_TIMINGS) {
520a93de288SAneesh V tim1 = regs->sdram_tim1_shdw_derated;
521a93de288SAneesh V tim3 = regs->sdram_tim3_shdw_derated;
522a93de288SAneesh V ref_ctrl = regs->ref_ctrl_shdw_derated;
523a93de288SAneesh V }
524a93de288SAneesh V
525a93de288SAneesh V out:
526a93de288SAneesh V writel(tim1, base + EMIF_SDRAM_TIMING_1_SHDW);
527a93de288SAneesh V writel(tim3, base + EMIF_SDRAM_TIMING_3_SHDW);
528a93de288SAneesh V writel(ref_ctrl, base + EMIF_SDRAM_REFRESH_CTRL_SHDW);
529a93de288SAneesh V }
530a93de288SAneesh V
handle_temp_alert(void __iomem * base,struct emif_data * emif)53168b4aee3SAneesh V static irqreturn_t handle_temp_alert(void __iomem *base, struct emif_data *emif)
53268b4aee3SAneesh V {
53368b4aee3SAneesh V u32 old_temp_level;
53468b4aee3SAneesh V irqreturn_t ret = IRQ_HANDLED;
535f91a595dSNishanth Menon struct emif_custom_configs *custom_configs;
53668b4aee3SAneesh V
53768b4aee3SAneesh V spin_lock_irqsave(&emif_lock, irq_state);
53868b4aee3SAneesh V old_temp_level = emif->temperature_level;
53968b4aee3SAneesh V get_temperature_level(emif);
54068b4aee3SAneesh V
54168b4aee3SAneesh V if (unlikely(emif->temperature_level == old_temp_level)) {
54268b4aee3SAneesh V goto out;
54368b4aee3SAneesh V } else if (!emif->curr_regs) {
54468b4aee3SAneesh V dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
54568b4aee3SAneesh V goto out;
54668b4aee3SAneesh V }
54768b4aee3SAneesh V
548f91a595dSNishanth Menon custom_configs = emif->plat_data->custom_configs;
549f91a595dSNishanth Menon
550f91a595dSNishanth Menon /*
551f91a595dSNishanth Menon * IF we detect higher than "nominal rating" from DDR sensor
552f91a595dSNishanth Menon * on an unsupported DDR part, shutdown system
553f91a595dSNishanth Menon */
554f91a595dSNishanth Menon if (custom_configs && !(custom_configs->mask &
555f91a595dSNishanth Menon EMIF_CUSTOM_CONFIG_EXTENDED_TEMP_PART)) {
556f91a595dSNishanth Menon if (emif->temperature_level >= SDRAM_TEMP_HIGH_DERATE_REFRESH) {
557f91a595dSNishanth Menon dev_err(emif->dev,
55828dc76f1SKrzysztof Kozlowski "%s:NOT Extended temperature capable memory. Converting MR4=0x%02x as shutdown event\n",
559f91a595dSNishanth Menon __func__, emif->temperature_level);
560f91a595dSNishanth Menon /*
561f91a595dSNishanth Menon * Temperature far too high - do kernel_power_off()
562f91a595dSNishanth Menon * from thread context
563f91a595dSNishanth Menon */
564f91a595dSNishanth Menon emif->temperature_level = SDRAM_TEMP_VERY_HIGH_SHUTDOWN;
565f91a595dSNishanth Menon ret = IRQ_WAKE_THREAD;
566f91a595dSNishanth Menon goto out;
567f91a595dSNishanth Menon }
568f91a595dSNishanth Menon }
569f91a595dSNishanth Menon
57068b4aee3SAneesh V if (emif->temperature_level < old_temp_level ||
57168b4aee3SAneesh V emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
57268b4aee3SAneesh V /*
57368b4aee3SAneesh V * Temperature coming down - defer handling to thread OR
57468b4aee3SAneesh V * Temperature far too high - do kernel_power_off() from
57568b4aee3SAneesh V * thread context
57668b4aee3SAneesh V */
57768b4aee3SAneesh V ret = IRQ_WAKE_THREAD;
57868b4aee3SAneesh V } else {
57968b4aee3SAneesh V /* Temperature is going up - handle immediately */
58068b4aee3SAneesh V setup_temperature_sensitive_regs(emif, emif->curr_regs);
58168b4aee3SAneesh V do_freq_update();
58268b4aee3SAneesh V }
58368b4aee3SAneesh V
58468b4aee3SAneesh V out:
58568b4aee3SAneesh V spin_unlock_irqrestore(&emif_lock, irq_state);
58668b4aee3SAneesh V return ret;
58768b4aee3SAneesh V }
58868b4aee3SAneesh V
emif_interrupt_handler(int irq,void * dev_id)58968b4aee3SAneesh V static irqreturn_t emif_interrupt_handler(int irq, void *dev_id)
59068b4aee3SAneesh V {
59168b4aee3SAneesh V u32 interrupts;
59268b4aee3SAneesh V struct emif_data *emif = dev_id;
59368b4aee3SAneesh V void __iomem *base = emif->base;
59468b4aee3SAneesh V struct device *dev = emif->dev;
59568b4aee3SAneesh V irqreturn_t ret = IRQ_HANDLED;
59668b4aee3SAneesh V
59768b4aee3SAneesh V /* Save the status and clear it */
59868b4aee3SAneesh V interrupts = readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
59968b4aee3SAneesh V writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
60068b4aee3SAneesh V
60168b4aee3SAneesh V /*
60268b4aee3SAneesh V * Handle temperature alert
60368b4aee3SAneesh V * Temperature alert should be same for all ports
60468b4aee3SAneesh V * So, it's enough to process it only for one of the ports
60568b4aee3SAneesh V */
60668b4aee3SAneesh V if (interrupts & TA_SYS_MASK)
60768b4aee3SAneesh V ret = handle_temp_alert(base, emif);
60868b4aee3SAneesh V
60968b4aee3SAneesh V if (interrupts & ERR_SYS_MASK)
61068b4aee3SAneesh V dev_err(dev, "Access error from SYS port - %x\n", interrupts);
61168b4aee3SAneesh V
61268b4aee3SAneesh V if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
61368b4aee3SAneesh V /* Save the status and clear it */
61468b4aee3SAneesh V interrupts = readl(base + EMIF_LL_OCP_INTERRUPT_STATUS);
61568b4aee3SAneesh V writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_STATUS);
61668b4aee3SAneesh V
61768b4aee3SAneesh V if (interrupts & ERR_LL_MASK)
61868b4aee3SAneesh V dev_err(dev, "Access error from LL port - %x\n",
61968b4aee3SAneesh V interrupts);
62068b4aee3SAneesh V }
62168b4aee3SAneesh V
62268b4aee3SAneesh V return ret;
62368b4aee3SAneesh V }
62468b4aee3SAneesh V
emif_threaded_isr(int irq,void * dev_id)62568b4aee3SAneesh V static irqreturn_t emif_threaded_isr(int irq, void *dev_id)
62668b4aee3SAneesh V {
62768b4aee3SAneesh V struct emif_data *emif = dev_id;
62868b4aee3SAneesh V
62968b4aee3SAneesh V if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
63068b4aee3SAneesh V dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
6312553e32aSNishanth Menon
6322553e32aSNishanth Menon /* If we have Power OFF ability, use it, else try restarting */
633*bf8d73b9SDmitry Osipenko if (kernel_can_power_off()) {
63468b4aee3SAneesh V kernel_power_off();
6352553e32aSNishanth Menon } else {
6362553e32aSNishanth Menon WARN(1, "FIXME: NO pm_power_off!!! trying restart\n");
6372553e32aSNishanth Menon kernel_restart("SDRAM Over-temp Emergency restart");
6382553e32aSNishanth Menon }
63968b4aee3SAneesh V return IRQ_HANDLED;
64068b4aee3SAneesh V }
64168b4aee3SAneesh V
64268b4aee3SAneesh V spin_lock_irqsave(&emif_lock, irq_state);
64368b4aee3SAneesh V
64468b4aee3SAneesh V if (emif->curr_regs) {
64568b4aee3SAneesh V setup_temperature_sensitive_regs(emif, emif->curr_regs);
64668b4aee3SAneesh V do_freq_update();
64768b4aee3SAneesh V } else {
64868b4aee3SAneesh V dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
64968b4aee3SAneesh V }
65068b4aee3SAneesh V
65168b4aee3SAneesh V spin_unlock_irqrestore(&emif_lock, irq_state);
65268b4aee3SAneesh V
65368b4aee3SAneesh V return IRQ_HANDLED;
65468b4aee3SAneesh V }
65568b4aee3SAneesh V
clear_all_interrupts(struct emif_data * emif)65668b4aee3SAneesh V static void clear_all_interrupts(struct emif_data *emif)
65768b4aee3SAneesh V {
65868b4aee3SAneesh V void __iomem *base = emif->base;
65968b4aee3SAneesh V
66068b4aee3SAneesh V writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS),
66168b4aee3SAneesh V base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
66268b4aee3SAneesh V if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
66368b4aee3SAneesh V writel(readl(base + EMIF_LL_OCP_INTERRUPT_STATUS),
66468b4aee3SAneesh V base + EMIF_LL_OCP_INTERRUPT_STATUS);
66568b4aee3SAneesh V }
66668b4aee3SAneesh V
disable_and_clear_all_interrupts(struct emif_data * emif)66768b4aee3SAneesh V static void disable_and_clear_all_interrupts(struct emif_data *emif)
66868b4aee3SAneesh V {
66968b4aee3SAneesh V void __iomem *base = emif->base;
67068b4aee3SAneesh V
67168b4aee3SAneesh V /* Disable all interrupts */
67268b4aee3SAneesh V writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET),
67368b4aee3SAneesh V base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_CLEAR);
67468b4aee3SAneesh V if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
67568b4aee3SAneesh V writel(readl(base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET),
67668b4aee3SAneesh V base + EMIF_LL_OCP_INTERRUPT_ENABLE_CLEAR);
67768b4aee3SAneesh V
67868b4aee3SAneesh V /* Clear all interrupts */
67968b4aee3SAneesh V clear_all_interrupts(emif);
68068b4aee3SAneesh V }
68168b4aee3SAneesh V
setup_interrupts(struct emif_data * emif,u32 irq)68268b4aee3SAneesh V static int __init_or_module setup_interrupts(struct emif_data *emif, u32 irq)
68368b4aee3SAneesh V {
68468b4aee3SAneesh V u32 interrupts, type;
68568b4aee3SAneesh V void __iomem *base = emif->base;
68668b4aee3SAneesh V
68768b4aee3SAneesh V type = emif->plat_data->device_info->type;
68868b4aee3SAneesh V
68968b4aee3SAneesh V clear_all_interrupts(emif);
69068b4aee3SAneesh V
69168b4aee3SAneesh V /* Enable interrupts for SYS interface */
69268b4aee3SAneesh V interrupts = EN_ERR_SYS_MASK;
69368b4aee3SAneesh V if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4)
69468b4aee3SAneesh V interrupts |= EN_TA_SYS_MASK;
69568b4aee3SAneesh V writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET);
69668b4aee3SAneesh V
69768b4aee3SAneesh V /* Enable interrupts for LL interface */
69868b4aee3SAneesh V if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
69968b4aee3SAneesh V /* TA need not be enabled for LL */
70068b4aee3SAneesh V interrupts = EN_ERR_LL_MASK;
70168b4aee3SAneesh V writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET);
70268b4aee3SAneesh V }
70368b4aee3SAneesh V
70468b4aee3SAneesh V /* setup IRQ handlers */
70568b4aee3SAneesh V return devm_request_threaded_irq(emif->dev, irq,
70668b4aee3SAneesh V emif_interrupt_handler,
70768b4aee3SAneesh V emif_threaded_isr,
70868b4aee3SAneesh V 0, dev_name(emif->dev),
70968b4aee3SAneesh V emif);
71068b4aee3SAneesh V
71168b4aee3SAneesh V }
71268b4aee3SAneesh V
emif_onetime_settings(struct emif_data * emif)71398231c4fSAneesh V static void __init_or_module emif_onetime_settings(struct emif_data *emif)
71498231c4fSAneesh V {
71598231c4fSAneesh V u32 pwr_mgmt_ctrl, zq, temp_alert_cfg;
71698231c4fSAneesh V void __iomem *base = emif->base;
71798231c4fSAneesh V const struct lpddr2_addressing *addressing;
71898231c4fSAneesh V const struct ddr_device_info *device_info;
71998231c4fSAneesh V
72098231c4fSAneesh V device_info = emif->plat_data->device_info;
72198231c4fSAneesh V addressing = get_addressing_table(device_info);
72298231c4fSAneesh V
72398231c4fSAneesh V /*
72498231c4fSAneesh V * Init power management settings
72598231c4fSAneesh V * We don't know the frequency yet. Use a high frequency
72698231c4fSAneesh V * value for a conservative timeout setting
72798231c4fSAneesh V */
72898231c4fSAneesh V pwr_mgmt_ctrl = get_pwr_mgmt_ctrl(1000000000, emif,
72998231c4fSAneesh V emif->plat_data->ip_rev);
73098231c4fSAneesh V emif->lpmode = (pwr_mgmt_ctrl & LP_MODE_MASK) >> LP_MODE_SHIFT;
73198231c4fSAneesh V writel(pwr_mgmt_ctrl, base + EMIF_POWER_MANAGEMENT_CONTROL);
73298231c4fSAneesh V
73398231c4fSAneesh V /* Init ZQ calibration settings */
73498231c4fSAneesh V zq = get_zq_config_reg(addressing, device_info->cs1_used,
73598231c4fSAneesh V device_info->cal_resistors_per_cs);
73698231c4fSAneesh V writel(zq, base + EMIF_SDRAM_OUTPUT_IMPEDANCE_CALIBRATION_CONFIG);
73798231c4fSAneesh V
73898231c4fSAneesh V /* Check temperature level temperature level*/
73998231c4fSAneesh V get_temperature_level(emif);
74098231c4fSAneesh V if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN)
74198231c4fSAneesh V dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
74298231c4fSAneesh V
74398231c4fSAneesh V /* Init temperature polling */
74498231c4fSAneesh V temp_alert_cfg = get_temp_alert_config(addressing,
74598231c4fSAneesh V emif->plat_data->custom_configs, device_info->cs1_used,
74698231c4fSAneesh V device_info->io_width, get_emif_bus_width(emif));
74798231c4fSAneesh V writel(temp_alert_cfg, base + EMIF_TEMPERATURE_ALERT_CONFIG);
74898231c4fSAneesh V
74998231c4fSAneesh V /*
75098231c4fSAneesh V * Program external PHY control registers that are not frequency
75198231c4fSAneesh V * dependent
75298231c4fSAneesh V */
75398231c4fSAneesh V if (emif->plat_data->phy_type != EMIF_PHY_TYPE_INTELLIPHY)
75498231c4fSAneesh V return;
75598231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_1_VAL, base + EMIF_EXT_PHY_CTRL_1_SHDW);
75698231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_5_VAL, base + EMIF_EXT_PHY_CTRL_5_SHDW);
75798231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_6_VAL, base + EMIF_EXT_PHY_CTRL_6_SHDW);
75898231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_7_VAL, base + EMIF_EXT_PHY_CTRL_7_SHDW);
75998231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_8_VAL, base + EMIF_EXT_PHY_CTRL_8_SHDW);
76098231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_9_VAL, base + EMIF_EXT_PHY_CTRL_9_SHDW);
76198231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_10_VAL, base + EMIF_EXT_PHY_CTRL_10_SHDW);
76298231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_11_VAL, base + EMIF_EXT_PHY_CTRL_11_SHDW);
76398231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_12_VAL, base + EMIF_EXT_PHY_CTRL_12_SHDW);
76498231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_13_VAL, base + EMIF_EXT_PHY_CTRL_13_SHDW);
76598231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_14_VAL, base + EMIF_EXT_PHY_CTRL_14_SHDW);
76698231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_15_VAL, base + EMIF_EXT_PHY_CTRL_15_SHDW);
76798231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_16_VAL, base + EMIF_EXT_PHY_CTRL_16_SHDW);
76898231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_17_VAL, base + EMIF_EXT_PHY_CTRL_17_SHDW);
76998231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_18_VAL, base + EMIF_EXT_PHY_CTRL_18_SHDW);
77098231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_19_VAL, base + EMIF_EXT_PHY_CTRL_19_SHDW);
77198231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_20_VAL, base + EMIF_EXT_PHY_CTRL_20_SHDW);
77298231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_21_VAL, base + EMIF_EXT_PHY_CTRL_21_SHDW);
77398231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_22_VAL, base + EMIF_EXT_PHY_CTRL_22_SHDW);
77498231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_23_VAL, base + EMIF_EXT_PHY_CTRL_23_SHDW);
77598231c4fSAneesh V writel(EMIF_EXT_PHY_CTRL_24_VAL, base + EMIF_EXT_PHY_CTRL_24_SHDW);
77698231c4fSAneesh V }
77798231c4fSAneesh V
get_default_timings(struct emif_data * emif)7787ec94453SAneesh V static void get_default_timings(struct emif_data *emif)
7797ec94453SAneesh V {
7807ec94453SAneesh V struct emif_platform_data *pd = emif->plat_data;
7817ec94453SAneesh V
7827ec94453SAneesh V pd->timings = lpddr2_jedec_timings;
7837ec94453SAneesh V pd->timings_arr_size = ARRAY_SIZE(lpddr2_jedec_timings);
7847ec94453SAneesh V
7857ec94453SAneesh V dev_warn(emif->dev, "%s: using default timings\n", __func__);
7867ec94453SAneesh V }
7877ec94453SAneesh V
is_dev_data_valid(u32 type,u32 density,u32 io_width,u32 phy_type,u32 ip_rev,struct device * dev)7887ec94453SAneesh V static int is_dev_data_valid(u32 type, u32 density, u32 io_width, u32 phy_type,
7897ec94453SAneesh V u32 ip_rev, struct device *dev)
7907ec94453SAneesh V {
7917ec94453SAneesh V int valid;
7927ec94453SAneesh V
7937ec94453SAneesh V valid = (type == DDR_TYPE_LPDDR2_S4 ||
7947ec94453SAneesh V type == DDR_TYPE_LPDDR2_S2)
7957ec94453SAneesh V && (density >= DDR_DENSITY_64Mb
7967ec94453SAneesh V && density <= DDR_DENSITY_8Gb)
7977ec94453SAneesh V && (io_width >= DDR_IO_WIDTH_8
7987ec94453SAneesh V && io_width <= DDR_IO_WIDTH_32);
7997ec94453SAneesh V
8007ec94453SAneesh V /* Combinations of EMIF and PHY revisions that we support today */
8017ec94453SAneesh V switch (ip_rev) {
8027ec94453SAneesh V case EMIF_4D:
8037ec94453SAneesh V valid = valid && (phy_type == EMIF_PHY_TYPE_ATTILAPHY);
8047ec94453SAneesh V break;
8057ec94453SAneesh V case EMIF_4D5:
8067ec94453SAneesh V valid = valid && (phy_type == EMIF_PHY_TYPE_INTELLIPHY);
8077ec94453SAneesh V break;
8087ec94453SAneesh V default:
8097ec94453SAneesh V valid = 0;
8107ec94453SAneesh V }
8117ec94453SAneesh V
8127ec94453SAneesh V if (!valid)
8137ec94453SAneesh V dev_err(dev, "%s: invalid DDR details\n", __func__);
8147ec94453SAneesh V return valid;
8157ec94453SAneesh V }
8167ec94453SAneesh V
is_custom_config_valid(struct emif_custom_configs * cust_cfgs,struct device * dev)8177ec94453SAneesh V static int is_custom_config_valid(struct emif_custom_configs *cust_cfgs,
8187ec94453SAneesh V struct device *dev)
8197ec94453SAneesh V {
8207ec94453SAneesh V int valid = 1;
8217ec94453SAneesh V
8227ec94453SAneesh V if ((cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE) &&
8237ec94453SAneesh V (cust_cfgs->lpmode != EMIF_LP_MODE_DISABLE))
8247ec94453SAneesh V valid = cust_cfgs->lpmode_freq_threshold &&
8257ec94453SAneesh V cust_cfgs->lpmode_timeout_performance &&
8267ec94453SAneesh V cust_cfgs->lpmode_timeout_power;
8277ec94453SAneesh V
8287ec94453SAneesh V if (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL)
8297ec94453SAneesh V valid = valid && cust_cfgs->temp_alert_poll_interval_ms;
8307ec94453SAneesh V
8317ec94453SAneesh V if (!valid)
8327ec94453SAneesh V dev_warn(dev, "%s: invalid custom configs\n", __func__);
8337ec94453SAneesh V
8347ec94453SAneesh V return valid;
8357ec94453SAneesh V }
8367ec94453SAneesh V
837e6b42eb6SAneesh V #if defined(CONFIG_OF)
of_get_custom_configs(struct device_node * np_emif,struct emif_data * emif)838e6b42eb6SAneesh V static void __init_or_module of_get_custom_configs(struct device_node *np_emif,
839e6b42eb6SAneesh V struct emif_data *emif)
840e6b42eb6SAneesh V {
841e6b42eb6SAneesh V struct emif_custom_configs *cust_cfgs = NULL;
842e6b42eb6SAneesh V int len;
843f57f27bcSLokesh Vutla const __be32 *lpmode, *poll_intvl;
844e6b42eb6SAneesh V
845e6b42eb6SAneesh V lpmode = of_get_property(np_emif, "low-power-mode", &len);
846e6b42eb6SAneesh V poll_intvl = of_get_property(np_emif, "temp-alert-poll-interval", &len);
847e6b42eb6SAneesh V
848e6b42eb6SAneesh V if (lpmode || poll_intvl)
849e6b42eb6SAneesh V cust_cfgs = devm_kzalloc(emif->dev, sizeof(*cust_cfgs),
850e6b42eb6SAneesh V GFP_KERNEL);
851e6b42eb6SAneesh V
852e6b42eb6SAneesh V if (!cust_cfgs)
853e6b42eb6SAneesh V return;
854e6b42eb6SAneesh V
855e6b42eb6SAneesh V if (lpmode) {
856e6b42eb6SAneesh V cust_cfgs->mask |= EMIF_CUSTOM_CONFIG_LPMODE;
857f57f27bcSLokesh Vutla cust_cfgs->lpmode = be32_to_cpup(lpmode);
858e6b42eb6SAneesh V of_property_read_u32(np_emif,
859e6b42eb6SAneesh V "low-power-mode-timeout-performance",
860e6b42eb6SAneesh V &cust_cfgs->lpmode_timeout_performance);
861e6b42eb6SAneesh V of_property_read_u32(np_emif,
862e6b42eb6SAneesh V "low-power-mode-timeout-power",
863e6b42eb6SAneesh V &cust_cfgs->lpmode_timeout_power);
864e6b42eb6SAneesh V of_property_read_u32(np_emif,
865e6b42eb6SAneesh V "low-power-mode-freq-threshold",
866e6b42eb6SAneesh V &cust_cfgs->lpmode_freq_threshold);
867e6b42eb6SAneesh V }
868e6b42eb6SAneesh V
869e6b42eb6SAneesh V if (poll_intvl) {
870e6b42eb6SAneesh V cust_cfgs->mask |=
871e6b42eb6SAneesh V EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL;
872f57f27bcSLokesh Vutla cust_cfgs->temp_alert_poll_interval_ms =
873f57f27bcSLokesh Vutla be32_to_cpup(poll_intvl);
874e6b42eb6SAneesh V }
875e6b42eb6SAneesh V
876f91a595dSNishanth Menon if (of_find_property(np_emif, "extended-temp-part", &len))
877f91a595dSNishanth Menon cust_cfgs->mask |= EMIF_CUSTOM_CONFIG_EXTENDED_TEMP_PART;
878f91a595dSNishanth Menon
879e6b42eb6SAneesh V if (!is_custom_config_valid(cust_cfgs, emif->dev)) {
880e6b42eb6SAneesh V devm_kfree(emif->dev, cust_cfgs);
881e6b42eb6SAneesh V return;
882e6b42eb6SAneesh V }
883e6b42eb6SAneesh V
884e6b42eb6SAneesh V emif->plat_data->custom_configs = cust_cfgs;
885e6b42eb6SAneesh V }
886e6b42eb6SAneesh V
of_get_ddr_info(struct device_node * np_emif,struct device_node * np_ddr,struct ddr_device_info * dev_info)887e6b42eb6SAneesh V static void __init_or_module of_get_ddr_info(struct device_node *np_emif,
888e6b42eb6SAneesh V struct device_node *np_ddr,
889e6b42eb6SAneesh V struct ddr_device_info *dev_info)
890e6b42eb6SAneesh V {
891e6b42eb6SAneesh V u32 density = 0, io_width = 0;
892e6b42eb6SAneesh V int len;
893e6b42eb6SAneesh V
894e6b42eb6SAneesh V if (of_find_property(np_emif, "cs1-used", &len))
895e6b42eb6SAneesh V dev_info->cs1_used = true;
896e6b42eb6SAneesh V
897e6b42eb6SAneesh V if (of_find_property(np_emif, "cal-resistor-per-cs", &len))
898e6b42eb6SAneesh V dev_info->cal_resistors_per_cs = true;
899e6b42eb6SAneesh V
900e6b42eb6SAneesh V if (of_device_is_compatible(np_ddr, "jedec,lpddr2-s4"))
901e6b42eb6SAneesh V dev_info->type = DDR_TYPE_LPDDR2_S4;
902e6b42eb6SAneesh V else if (of_device_is_compatible(np_ddr, "jedec,lpddr2-s2"))
903e6b42eb6SAneesh V dev_info->type = DDR_TYPE_LPDDR2_S2;
904e6b42eb6SAneesh V
905e6b42eb6SAneesh V of_property_read_u32(np_ddr, "density", &density);
906e6b42eb6SAneesh V of_property_read_u32(np_ddr, "io-width", &io_width);
907e6b42eb6SAneesh V
908e6b42eb6SAneesh V /* Convert from density in Mb to the density encoding in jedc_ddr.h */
909e6b42eb6SAneesh V if (density & (density - 1))
910e6b42eb6SAneesh V dev_info->density = 0;
911e6b42eb6SAneesh V else
912e6b42eb6SAneesh V dev_info->density = __fls(density) - 5;
913e6b42eb6SAneesh V
914e6b42eb6SAneesh V /* Convert from io_width in bits to io_width encoding in jedc_ddr.h */
915e6b42eb6SAneesh V if (io_width & (io_width - 1))
916e6b42eb6SAneesh V dev_info->io_width = 0;
917e6b42eb6SAneesh V else
918e6b42eb6SAneesh V dev_info->io_width = __fls(io_width) - 1;
919e6b42eb6SAneesh V }
920e6b42eb6SAneesh V
of_get_memory_device_details(struct device_node * np_emif,struct device * dev)921e6b42eb6SAneesh V static struct emif_data * __init_or_module of_get_memory_device_details(
922e6b42eb6SAneesh V struct device_node *np_emif, struct device *dev)
923e6b42eb6SAneesh V {
924e6b42eb6SAneesh V struct emif_data *emif = NULL;
925e6b42eb6SAneesh V struct ddr_device_info *dev_info = NULL;
926e6b42eb6SAneesh V struct emif_platform_data *pd = NULL;
927e6b42eb6SAneesh V struct device_node *np_ddr;
928e6b42eb6SAneesh V int len;
929e6b42eb6SAneesh V
930e6b42eb6SAneesh V np_ddr = of_parse_phandle(np_emif, "device-handle", 0);
931e6b42eb6SAneesh V if (!np_ddr)
932e6b42eb6SAneesh V goto error;
933e6b42eb6SAneesh V emif = devm_kzalloc(dev, sizeof(struct emif_data), GFP_KERNEL);
934e6b42eb6SAneesh V pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
935e6b42eb6SAneesh V dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
936e6b42eb6SAneesh V
937e6b42eb6SAneesh V if (!emif || !pd || !dev_info) {
938e6b42eb6SAneesh V dev_err(dev, "%s: Out of memory!!\n",
939e6b42eb6SAneesh V __func__);
940e6b42eb6SAneesh V goto error;
941e6b42eb6SAneesh V }
942e6b42eb6SAneesh V
943e6b42eb6SAneesh V emif->plat_data = pd;
944e6b42eb6SAneesh V pd->device_info = dev_info;
945e6b42eb6SAneesh V emif->dev = dev;
946e6b42eb6SAneesh V emif->np_ddr = np_ddr;
947e6b42eb6SAneesh V emif->temperature_level = SDRAM_TEMP_NOMINAL;
948e6b42eb6SAneesh V
949e6b42eb6SAneesh V if (of_device_is_compatible(np_emif, "ti,emif-4d"))
950e6b42eb6SAneesh V emif->plat_data->ip_rev = EMIF_4D;
951e6b42eb6SAneesh V else if (of_device_is_compatible(np_emif, "ti,emif-4d5"))
952e6b42eb6SAneesh V emif->plat_data->ip_rev = EMIF_4D5;
953e6b42eb6SAneesh V
954e6b42eb6SAneesh V of_property_read_u32(np_emif, "phy-type", &pd->phy_type);
955e6b42eb6SAneesh V
956e6b42eb6SAneesh V if (of_find_property(np_emif, "hw-caps-ll-interface", &len))
957e6b42eb6SAneesh V pd->hw_caps |= EMIF_HW_CAPS_LL_INTERFACE;
958e6b42eb6SAneesh V
959e6b42eb6SAneesh V of_get_ddr_info(np_emif, np_ddr, dev_info);
960e6b42eb6SAneesh V if (!is_dev_data_valid(pd->device_info->type, pd->device_info->density,
961e6b42eb6SAneesh V pd->device_info->io_width, pd->phy_type, pd->ip_rev,
962e6b42eb6SAneesh V emif->dev)) {
963e6b42eb6SAneesh V dev_err(dev, "%s: invalid device data!!\n", __func__);
964e6b42eb6SAneesh V goto error;
965e6b42eb6SAneesh V }
966e6b42eb6SAneesh V /*
967e6b42eb6SAneesh V * For EMIF instances other than EMIF1 see if the devices connected
968e6b42eb6SAneesh V * are exactly same as on EMIF1(which is typically the case). If so,
969e6b42eb6SAneesh V * mark it as a duplicate of EMIF1. This will save some memory and
970e6b42eb6SAneesh V * computation.
971e6b42eb6SAneesh V */
972e6b42eb6SAneesh V if (emif1 && emif1->np_ddr == np_ddr) {
973e6b42eb6SAneesh V emif->duplicate = true;
974e6b42eb6SAneesh V goto out;
975e6b42eb6SAneesh V } else if (emif1) {
976e6b42eb6SAneesh V dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
977e6b42eb6SAneesh V __func__);
978e6b42eb6SAneesh V }
979e6b42eb6SAneesh V
980e6b42eb6SAneesh V of_get_custom_configs(np_emif, emif);
981e6b42eb6SAneesh V emif->plat_data->timings = of_get_ddr_timings(np_ddr, emif->dev,
982e6b42eb6SAneesh V emif->plat_data->device_info->type,
983e6b42eb6SAneesh V &emif->plat_data->timings_arr_size);
984e6b42eb6SAneesh V
985e6b42eb6SAneesh V emif->plat_data->min_tck = of_get_min_tck(np_ddr, emif->dev);
986e6b42eb6SAneesh V goto out;
987e6b42eb6SAneesh V
988e6b42eb6SAneesh V error:
989e6b42eb6SAneesh V return NULL;
990e6b42eb6SAneesh V out:
991e6b42eb6SAneesh V return emif;
992e6b42eb6SAneesh V }
993e6b42eb6SAneesh V
994e6b42eb6SAneesh V #else
995e6b42eb6SAneesh V
of_get_memory_device_details(struct device_node * np_emif,struct device * dev)996e6b42eb6SAneesh V static struct emif_data * __init_or_module of_get_memory_device_details(
997e6b42eb6SAneesh V struct device_node *np_emif, struct device *dev)
998e6b42eb6SAneesh V {
999e6b42eb6SAneesh V return NULL;
1000e6b42eb6SAneesh V }
1001e6b42eb6SAneesh V #endif
1002e6b42eb6SAneesh V
get_device_details(struct platform_device * pdev)10037ec94453SAneesh V static struct emif_data *__init_or_module get_device_details(
10047ec94453SAneesh V struct platform_device *pdev)
10057ec94453SAneesh V {
10067ec94453SAneesh V u32 size;
10077ec94453SAneesh V struct emif_data *emif = NULL;
10087ec94453SAneesh V struct ddr_device_info *dev_info;
10097ec94453SAneesh V struct emif_custom_configs *cust_cfgs;
10107ec94453SAneesh V struct emif_platform_data *pd;
10117ec94453SAneesh V struct device *dev;
10127ec94453SAneesh V void *temp;
10137ec94453SAneesh V
10147ec94453SAneesh V pd = pdev->dev.platform_data;
10157ec94453SAneesh V dev = &pdev->dev;
10167ec94453SAneesh V
10177ec94453SAneesh V if (!(pd && pd->device_info && is_dev_data_valid(pd->device_info->type,
10187ec94453SAneesh V pd->device_info->density, pd->device_info->io_width,
10197ec94453SAneesh V pd->phy_type, pd->ip_rev, dev))) {
10207ec94453SAneesh V dev_err(dev, "%s: invalid device data\n", __func__);
10217ec94453SAneesh V goto error;
10227ec94453SAneesh V }
10237ec94453SAneesh V
10247ec94453SAneesh V emif = devm_kzalloc(dev, sizeof(*emif), GFP_KERNEL);
10257ec94453SAneesh V temp = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
10267ec94453SAneesh V dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
10277ec94453SAneesh V
10286ded3d74SKrzysztof Kozlowski if (!emif || !temp || !dev_info)
10297ec94453SAneesh V goto error;
10307ec94453SAneesh V
10317ec94453SAneesh V memcpy(temp, pd, sizeof(*pd));
10327ec94453SAneesh V pd = temp;
10337ec94453SAneesh V memcpy(dev_info, pd->device_info, sizeof(*dev_info));
10347ec94453SAneesh V
10357ec94453SAneesh V pd->device_info = dev_info;
10367ec94453SAneesh V emif->plat_data = pd;
10377ec94453SAneesh V emif->dev = dev;
10387ec94453SAneesh V emif->temperature_level = SDRAM_TEMP_NOMINAL;
10397ec94453SAneesh V
10407ec94453SAneesh V /*
10417ec94453SAneesh V * For EMIF instances other than EMIF1 see if the devices connected
10427ec94453SAneesh V * are exactly same as on EMIF1(which is typically the case). If so,
10437ec94453SAneesh V * mark it as a duplicate of EMIF1 and skip copying timings data.
10447ec94453SAneesh V * This will save some memory and some computation later.
10457ec94453SAneesh V */
10467ec94453SAneesh V emif->duplicate = emif1 && (memcmp(dev_info,
10477ec94453SAneesh V emif1->plat_data->device_info,
10487ec94453SAneesh V sizeof(struct ddr_device_info)) == 0);
10497ec94453SAneesh V
10507ec94453SAneesh V if (emif->duplicate) {
10517ec94453SAneesh V pd->timings = NULL;
10527ec94453SAneesh V pd->min_tck = NULL;
10537ec94453SAneesh V goto out;
10547ec94453SAneesh V } else if (emif1) {
10557ec94453SAneesh V dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
10567ec94453SAneesh V __func__);
10577ec94453SAneesh V }
10587ec94453SAneesh V
10597ec94453SAneesh V /*
10607ec94453SAneesh V * Copy custom configs - ignore allocation error, if any, as
10617ec94453SAneesh V * custom_configs is not very critical
10627ec94453SAneesh V */
10637ec94453SAneesh V cust_cfgs = pd->custom_configs;
10647ec94453SAneesh V if (cust_cfgs && is_custom_config_valid(cust_cfgs, dev)) {
10657ec94453SAneesh V temp = devm_kzalloc(dev, sizeof(*cust_cfgs), GFP_KERNEL);
10667ec94453SAneesh V if (temp)
10677ec94453SAneesh V memcpy(temp, cust_cfgs, sizeof(*cust_cfgs));
10687ec94453SAneesh V pd->custom_configs = temp;
10697ec94453SAneesh V }
10707ec94453SAneesh V
10717ec94453SAneesh V /*
10727ec94453SAneesh V * Copy timings and min-tck values from platform data. If it is not
10737ec94453SAneesh V * available or if memory allocation fails, use JEDEC defaults
10747ec94453SAneesh V */
10757ec94453SAneesh V size = sizeof(struct lpddr2_timings) * pd->timings_arr_size;
10767ec94453SAneesh V if (pd->timings) {
10777ec94453SAneesh V temp = devm_kzalloc(dev, size, GFP_KERNEL);
10787ec94453SAneesh V if (temp) {
107936caf3e5SOleksandr Dmytryshyn memcpy(temp, pd->timings, size);
10807ec94453SAneesh V pd->timings = temp;
10817ec94453SAneesh V } else {
10827ec94453SAneesh V get_default_timings(emif);
10837ec94453SAneesh V }
10847ec94453SAneesh V } else {
10857ec94453SAneesh V get_default_timings(emif);
10867ec94453SAneesh V }
10877ec94453SAneesh V
10887ec94453SAneesh V if (pd->min_tck) {
10897ec94453SAneesh V temp = devm_kzalloc(dev, sizeof(*pd->min_tck), GFP_KERNEL);
10907ec94453SAneesh V if (temp) {
10917ec94453SAneesh V memcpy(temp, pd->min_tck, sizeof(*pd->min_tck));
10927ec94453SAneesh V pd->min_tck = temp;
10937ec94453SAneesh V } else {
10947ec94453SAneesh V pd->min_tck = &lpddr2_jedec_min_tck;
10957ec94453SAneesh V }
10967ec94453SAneesh V } else {
10977ec94453SAneesh V pd->min_tck = &lpddr2_jedec_min_tck;
10987ec94453SAneesh V }
10997ec94453SAneesh V
11007ec94453SAneesh V out:
11017ec94453SAneesh V return emif;
11027ec94453SAneesh V
11037ec94453SAneesh V error:
11047ec94453SAneesh V return NULL;
11057ec94453SAneesh V }
11067ec94453SAneesh V
emif_probe(struct platform_device * pdev)11077ec94453SAneesh V static int __init_or_module emif_probe(struct platform_device *pdev)
11087ec94453SAneesh V {
11097ec94453SAneesh V struct emif_data *emif;
1110fd7bd80bSJiasheng Jiang int irq, ret;
11117ec94453SAneesh V
1112e6b42eb6SAneesh V if (pdev->dev.of_node)
1113e6b42eb6SAneesh V emif = of_get_memory_device_details(pdev->dev.of_node, &pdev->dev);
1114e6b42eb6SAneesh V else
11157ec94453SAneesh V emif = get_device_details(pdev);
1116e6b42eb6SAneesh V
11177ec94453SAneesh V if (!emif) {
11187ec94453SAneesh V pr_err("%s: error getting device data\n", __func__);
11197ec94453SAneesh V goto error;
11207ec94453SAneesh V }
11217ec94453SAneesh V
11227ec94453SAneesh V list_add(&emif->node, &device_list);
11237ec94453SAneesh V
11247ec94453SAneesh V /* Save pointers to each other in emif and device structures */
11257ec94453SAneesh V emif->dev = &pdev->dev;
11267ec94453SAneesh V platform_set_drvdata(pdev, emif);
11277ec94453SAneesh V
1128734058b1SKrzysztof Kozlowski emif->base = devm_platform_ioremap_resource(pdev, 0);
112906303c2eSThierry Reding if (IS_ERR(emif->base))
11307ec94453SAneesh V goto error;
11317ec94453SAneesh V
113268b4aee3SAneesh V irq = platform_get_irq(pdev, 0);
1133e79f3849SKrzysztof Kozlowski if (irq < 0)
113468b4aee3SAneesh V goto error;
113568b4aee3SAneesh V
113698231c4fSAneesh V emif_onetime_settings(emif);
1137aac10aaaSAneesh V emif_debugfs_init(emif);
113868b4aee3SAneesh V disable_and_clear_all_interrupts(emif);
1139fd7bd80bSJiasheng Jiang ret = setup_interrupts(emif, irq);
1140fd7bd80bSJiasheng Jiang if (ret)
1141fd7bd80bSJiasheng Jiang goto error;
114268b4aee3SAneesh V
1143a93de288SAneesh V /* One-time actions taken on probing the first device */
1144a93de288SAneesh V if (!emif1) {
1145a93de288SAneesh V emif1 = emif;
1146a93de288SAneesh V
1147a93de288SAneesh V /*
1148a93de288SAneesh V * TODO: register notifiers for frequency and voltage
1149a93de288SAneesh V * change here once the respective frameworks are
1150a93de288SAneesh V * available
1151a93de288SAneesh V */
1152a93de288SAneesh V }
1153a93de288SAneesh V
115468b4aee3SAneesh V dev_info(&pdev->dev, "%s: device configured with addr = %p and IRQ%d\n",
115568b4aee3SAneesh V __func__, emif->base, irq);
11567ec94453SAneesh V
11577ec94453SAneesh V return 0;
11587ec94453SAneesh V error:
11597ec94453SAneesh V return -ENODEV;
11607ec94453SAneesh V }
11617ec94453SAneesh V
emif_remove(struct platform_device * pdev)1162aac10aaaSAneesh V static int __exit emif_remove(struct platform_device *pdev)
1163aac10aaaSAneesh V {
1164aac10aaaSAneesh V struct emif_data *emif = platform_get_drvdata(pdev);
1165aac10aaaSAneesh V
1166aac10aaaSAneesh V emif_debugfs_exit(emif);
1167aac10aaaSAneesh V
1168aac10aaaSAneesh V return 0;
1169aac10aaaSAneesh V }
1170aac10aaaSAneesh V
emif_shutdown(struct platform_device * pdev)117168b4aee3SAneesh V static void emif_shutdown(struct platform_device *pdev)
117268b4aee3SAneesh V {
117368b4aee3SAneesh V struct emif_data *emif = platform_get_drvdata(pdev);
117468b4aee3SAneesh V
117568b4aee3SAneesh V disable_and_clear_all_interrupts(emif);
117668b4aee3SAneesh V }
117768b4aee3SAneesh V
1178e6b42eb6SAneesh V #if defined(CONFIG_OF)
1179e6b42eb6SAneesh V static const struct of_device_id emif_of_match[] = {
1180e6b42eb6SAneesh V { .compatible = "ti,emif-4d" },
1181e6b42eb6SAneesh V { .compatible = "ti,emif-4d5" },
1182e6b42eb6SAneesh V {},
1183e6b42eb6SAneesh V };
1184e6b42eb6SAneesh V MODULE_DEVICE_TABLE(of, emif_of_match);
1185e6b42eb6SAneesh V #endif
1186e6b42eb6SAneesh V
11877ec94453SAneesh V static struct platform_driver emif_driver = {
1188aac10aaaSAneesh V .remove = __exit_p(emif_remove),
118968b4aee3SAneesh V .shutdown = emif_shutdown,
11907ec94453SAneesh V .driver = {
11917ec94453SAneesh V .name = "emif",
1192e6b42eb6SAneesh V .of_match_table = of_match_ptr(emif_of_match),
11937ec94453SAneesh V },
11947ec94453SAneesh V };
11957ec94453SAneesh V
11967a4541a6SFabio Porcedda module_platform_driver_probe(emif_driver, emif_probe);
11977ec94453SAneesh V
11987ec94453SAneesh V MODULE_DESCRIPTION("TI EMIF SDRAM Controller Driver");
11997ec94453SAneesh V MODULE_LICENSE("GPL");
12007ec94453SAneesh V MODULE_ALIAS("platform:emif");
12017ec94453SAneesh V MODULE_AUTHOR("Texas Instruments Inc");
1202