arch/sparc/kernel/sys_sparc_64.c: s/COLOUR/COLOR/
[linux-2.6.git] / drivers / memory / emif.c
blob06d31c99e6ac22e166027a5a3666ca810931581b
1 /*
2 * EMIF driver
4 * Copyright (C) 2012 Texas Instruments, Inc.
6 * Aneesh V <aneesh@ti.com>
7 * Santosh Shilimkar <santosh.shilimkar@ti.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/reboot.h>
15 #include <linux/platform_data/emif_plat.h>
16 #include <linux/io.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/interrupt.h>
20 #include <linux/slab.h>
21 #include <linux/of.h>
22 #include <linux/debugfs.h>
23 #include <linux/seq_file.h>
24 #include <linux/module.h>
25 #include <linux/list.h>
26 #include <linux/spinlock.h>
27 #include <memory/jedec_ddr.h>
28 #include "emif.h"
29 #include "of_memory.h"
31 /**
32 * struct emif_data - Per device static data for driver's use
33 * @duplicate: Whether the DDR devices attached to this EMIF
34 * instance are exactly same as that on EMIF1. In
35 * this case we can save some memory and processing
36 * @temperature_level: Maximum temperature of LPDDR2 devices attached
37 * to this EMIF - read from MR4 register. If there
38 * are two devices attached to this EMIF, this
39 * value is the maximum of the two temperature
40 * levels.
41 * @node: node in the device list
42 * @base: base address of memory-mapped IO registers.
43 * @dev: device pointer.
44 * @addressing table with addressing information from the spec
45 * @regs_cache: An array of 'struct emif_regs' that stores
46 * calculated register values for different
47 * frequencies, to avoid re-calculating them on
48 * each DVFS transition.
49 * @curr_regs: The set of register values used in the last
50 * frequency change (i.e. corresponding to the
51 * frequency in effect at the moment)
52 * @plat_data: Pointer to saved platform data.
53 * @debugfs_root: dentry to the root folder for EMIF in debugfs
54 * @np_ddr: Pointer to ddr device tree node
56 struct emif_data {
57 u8 duplicate;
58 u8 temperature_level;
59 u8 lpmode;
60 struct list_head node;
61 unsigned long irq_state;
62 void __iomem *base;
63 struct device *dev;
64 const struct lpddr2_addressing *addressing;
65 struct emif_regs *regs_cache[EMIF_MAX_NUM_FREQUENCIES];
66 struct emif_regs *curr_regs;
67 struct emif_platform_data *plat_data;
68 struct dentry *debugfs_root;
69 struct device_node *np_ddr;
72 static struct emif_data *emif1;
73 static spinlock_t emif_lock;
74 static unsigned long irq_state;
75 static u32 t_ck; /* DDR clock period in ps */
76 static LIST_HEAD(device_list);
78 #ifdef CONFIG_DEBUG_FS
79 static void do_emif_regdump_show(struct seq_file *s, struct emif_data *emif,
80 struct emif_regs *regs)
82 u32 type = emif->plat_data->device_info->type;
83 u32 ip_rev = emif->plat_data->ip_rev;
85 seq_printf(s, "EMIF register cache dump for %dMHz\n",
86 regs->freq/1000000);
88 seq_printf(s, "ref_ctrl_shdw\t: 0x%08x\n", regs->ref_ctrl_shdw);
89 seq_printf(s, "sdram_tim1_shdw\t: 0x%08x\n", regs->sdram_tim1_shdw);
90 seq_printf(s, "sdram_tim2_shdw\t: 0x%08x\n", regs->sdram_tim2_shdw);
91 seq_printf(s, "sdram_tim3_shdw\t: 0x%08x\n", regs->sdram_tim3_shdw);
93 if (ip_rev == EMIF_4D) {
94 seq_printf(s, "read_idle_ctrl_shdw_normal\t: 0x%08x\n",
95 regs->read_idle_ctrl_shdw_normal);
96 seq_printf(s, "read_idle_ctrl_shdw_volt_ramp\t: 0x%08x\n",
97 regs->read_idle_ctrl_shdw_volt_ramp);
98 } else if (ip_rev == EMIF_4D5) {
99 seq_printf(s, "dll_calib_ctrl_shdw_normal\t: 0x%08x\n",
100 regs->dll_calib_ctrl_shdw_normal);
101 seq_printf(s, "dll_calib_ctrl_shdw_volt_ramp\t: 0x%08x\n",
102 regs->dll_calib_ctrl_shdw_volt_ramp);
105 if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4) {
106 seq_printf(s, "ref_ctrl_shdw_derated\t: 0x%08x\n",
107 regs->ref_ctrl_shdw_derated);
108 seq_printf(s, "sdram_tim1_shdw_derated\t: 0x%08x\n",
109 regs->sdram_tim1_shdw_derated);
110 seq_printf(s, "sdram_tim3_shdw_derated\t: 0x%08x\n",
111 regs->sdram_tim3_shdw_derated);
115 static int emif_regdump_show(struct seq_file *s, void *unused)
117 struct emif_data *emif = s->private;
118 struct emif_regs **regs_cache;
119 int i;
121 if (emif->duplicate)
122 regs_cache = emif1->regs_cache;
123 else
124 regs_cache = emif->regs_cache;
126 for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) {
127 do_emif_regdump_show(s, emif, regs_cache[i]);
128 seq_printf(s, "\n");
131 return 0;
134 static int emif_regdump_open(struct inode *inode, struct file *file)
136 return single_open(file, emif_regdump_show, inode->i_private);
139 static const struct file_operations emif_regdump_fops = {
140 .open = emif_regdump_open,
141 .read = seq_read,
142 .release = single_release,
145 static int emif_mr4_show(struct seq_file *s, void *unused)
147 struct emif_data *emif = s->private;
149 seq_printf(s, "MR4=%d\n", emif->temperature_level);
150 return 0;
153 static int emif_mr4_open(struct inode *inode, struct file *file)
155 return single_open(file, emif_mr4_show, inode->i_private);
158 static const struct file_operations emif_mr4_fops = {
159 .open = emif_mr4_open,
160 .read = seq_read,
161 .release = single_release,
164 static int __init_or_module emif_debugfs_init(struct emif_data *emif)
166 struct dentry *dentry;
167 int ret;
169 dentry = debugfs_create_dir(dev_name(emif->dev), NULL);
170 if (!dentry) {
171 ret = -ENOMEM;
172 goto err0;
174 emif->debugfs_root = dentry;
176 dentry = debugfs_create_file("regcache_dump", S_IRUGO,
177 emif->debugfs_root, emif, &emif_regdump_fops);
178 if (!dentry) {
179 ret = -ENOMEM;
180 goto err1;
183 dentry = debugfs_create_file("mr4", S_IRUGO,
184 emif->debugfs_root, emif, &emif_mr4_fops);
185 if (!dentry) {
186 ret = -ENOMEM;
187 goto err1;
190 return 0;
191 err1:
192 debugfs_remove_recursive(emif->debugfs_root);
193 err0:
194 return ret;
197 static void __exit emif_debugfs_exit(struct emif_data *emif)
199 debugfs_remove_recursive(emif->debugfs_root);
200 emif->debugfs_root = NULL;
202 #else
203 static inline int __init_or_module emif_debugfs_init(struct emif_data *emif)
205 return 0;
208 static inline void __exit emif_debugfs_exit(struct emif_data *emif)
211 #endif
214 * Calculate the period of DDR clock from frequency value
216 static void set_ddr_clk_period(u32 freq)
218 /* Divide 10^12 by frequency to get period in ps */
219 t_ck = (u32)DIV_ROUND_UP_ULL(1000000000000ull, freq);
223 * Get bus width used by EMIF. Note that this may be different from the
224 * bus width of the DDR devices used. For instance two 16-bit DDR devices
225 * may be connected to a given CS of EMIF. In this case bus width as far
226 * as EMIF is concerned is 32, where as the DDR bus width is 16 bits.
228 static u32 get_emif_bus_width(struct emif_data *emif)
230 u32 width;
231 void __iomem *base = emif->base;
233 width = (readl(base + EMIF_SDRAM_CONFIG) & NARROW_MODE_MASK)
234 >> NARROW_MODE_SHIFT;
235 width = width == 0 ? 32 : 16;
237 return width;
241 * Get the CL from SDRAM_CONFIG register
243 static u32 get_cl(struct emif_data *emif)
245 u32 cl;
246 void __iomem *base = emif->base;
248 cl = (readl(base + EMIF_SDRAM_CONFIG) & CL_MASK) >> CL_SHIFT;
250 return cl;
253 static void set_lpmode(struct emif_data *emif, u8 lpmode)
255 u32 temp;
256 void __iomem *base = emif->base;
258 temp = readl(base + EMIF_POWER_MANAGEMENT_CONTROL);
259 temp &= ~LP_MODE_MASK;
260 temp |= (lpmode << LP_MODE_SHIFT);
261 writel(temp, base + EMIF_POWER_MANAGEMENT_CONTROL);
264 static void do_freq_update(void)
266 struct emif_data *emif;
269 * Workaround for errata i728: Disable LPMODE during FREQ_UPDATE
271 * i728 DESCRIPTION:
272 * The EMIF automatically puts the SDRAM into self-refresh mode
273 * after the EMIF has not performed accesses during
274 * EMIF_PWR_MGMT_CTRL[7:4] REG_SR_TIM number of DDR clock cycles
275 * and the EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field is set
276 * to 0x2. If during a small window the following three events
277 * occur:
278 * - The SR_TIMING counter expires
279 * - And frequency change is requested
280 * - And OCP access is requested
281 * Then it causes instable clock on the DDR interface.
283 * WORKAROUND
284 * To avoid the occurrence of the three events, the workaround
285 * is to disable the self-refresh when requesting a frequency
286 * change. Before requesting a frequency change the software must
287 * program EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x0. When the
288 * frequency change has been done, the software can reprogram
289 * EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x2
291 list_for_each_entry(emif, &device_list, node) {
292 if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
293 set_lpmode(emif, EMIF_LP_MODE_DISABLE);
297 * TODO: Do FREQ_UPDATE here when an API
298 * is available for this as part of the new
299 * clock framework
302 list_for_each_entry(emif, &device_list, node) {
303 if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
304 set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH);
308 /* Find addressing table entry based on the device's type and density */
309 static const struct lpddr2_addressing *get_addressing_table(
310 const struct ddr_device_info *device_info)
312 u32 index, type, density;
314 type = device_info->type;
315 density = device_info->density;
317 switch (type) {
318 case DDR_TYPE_LPDDR2_S4:
319 index = density - 1;
320 break;
321 case DDR_TYPE_LPDDR2_S2:
322 switch (density) {
323 case DDR_DENSITY_1Gb:
324 case DDR_DENSITY_2Gb:
325 index = density + 3;
326 break;
327 default:
328 index = density - 1;
330 break;
331 default:
332 return NULL;
335 return &lpddr2_jedec_addressing_table[index];
339 * Find the the right timing table from the array of timing
340 * tables of the device using DDR clock frequency
342 static const struct lpddr2_timings *get_timings_table(struct emif_data *emif,
343 u32 freq)
345 u32 i, min, max, freq_nearest;
346 const struct lpddr2_timings *timings = NULL;
347 const struct lpddr2_timings *timings_arr = emif->plat_data->timings;
348 struct device *dev = emif->dev;
350 /* Start with a very high frequency - 1GHz */
351 freq_nearest = 1000000000;
354 * Find the timings table such that:
355 * 1. the frequency range covers the required frequency(safe) AND
356 * 2. the max_freq is closest to the required frequency(optimal)
358 for (i = 0; i < emif->plat_data->timings_arr_size; i++) {
359 max = timings_arr[i].max_freq;
360 min = timings_arr[i].min_freq;
361 if ((freq >= min) && (freq <= max) && (max < freq_nearest)) {
362 freq_nearest = max;
363 timings = &timings_arr[i];
367 if (!timings)
368 dev_err(dev, "%s: couldn't find timings for - %dHz\n",
369 __func__, freq);
371 dev_dbg(dev, "%s: timings table: freq %d, speed bin freq %d\n",
372 __func__, freq, freq_nearest);
374 return timings;
377 static u32 get_sdram_ref_ctrl_shdw(u32 freq,
378 const struct lpddr2_addressing *addressing)
380 u32 ref_ctrl_shdw = 0, val = 0, freq_khz, t_refi;
382 /* Scale down frequency and t_refi to avoid overflow */
383 freq_khz = freq / 1000;
384 t_refi = addressing->tREFI_ns / 100;
387 * refresh rate to be set is 'tREFI(in us) * freq in MHz
388 * division by 10000 to account for change in units
390 val = t_refi * freq_khz / 10000;
391 ref_ctrl_shdw |= val << REFRESH_RATE_SHIFT;
393 return ref_ctrl_shdw;
396 static u32 get_sdram_tim_1_shdw(const struct lpddr2_timings *timings,
397 const struct lpddr2_min_tck *min_tck,
398 const struct lpddr2_addressing *addressing)
400 u32 tim1 = 0, val = 0;
402 val = max(min_tck->tWTR, DIV_ROUND_UP(timings->tWTR, t_ck)) - 1;
403 tim1 |= val << T_WTR_SHIFT;
405 if (addressing->num_banks == B8)
406 val = DIV_ROUND_UP(timings->tFAW, t_ck*4);
407 else
408 val = max(min_tck->tRRD, DIV_ROUND_UP(timings->tRRD, t_ck));
409 tim1 |= (val - 1) << T_RRD_SHIFT;
411 val = DIV_ROUND_UP(timings->tRAS_min + timings->tRPab, t_ck) - 1;
412 tim1 |= val << T_RC_SHIFT;
414 val = max(min_tck->tRASmin, DIV_ROUND_UP(timings->tRAS_min, t_ck));
415 tim1 |= (val - 1) << T_RAS_SHIFT;
417 val = max(min_tck->tWR, DIV_ROUND_UP(timings->tWR, t_ck)) - 1;
418 tim1 |= val << T_WR_SHIFT;
420 val = max(min_tck->tRCD, DIV_ROUND_UP(timings->tRCD, t_ck)) - 1;
421 tim1 |= val << T_RCD_SHIFT;
423 val = max(min_tck->tRPab, DIV_ROUND_UP(timings->tRPab, t_ck)) - 1;
424 tim1 |= val << T_RP_SHIFT;
426 return tim1;
429 static u32 get_sdram_tim_1_shdw_derated(const struct lpddr2_timings *timings,
430 const struct lpddr2_min_tck *min_tck,
431 const struct lpddr2_addressing *addressing)
433 u32 tim1 = 0, val = 0;
435 val = max(min_tck->tWTR, DIV_ROUND_UP(timings->tWTR, t_ck)) - 1;
436 tim1 = val << T_WTR_SHIFT;
439 * tFAW is approximately 4 times tRRD. So add 1875*4 = 7500ps
440 * to tFAW for de-rating
442 if (addressing->num_banks == B8) {
443 val = DIV_ROUND_UP(timings->tFAW + 7500, 4 * t_ck) - 1;
444 } else {
445 val = DIV_ROUND_UP(timings->tRRD + 1875, t_ck);
446 val = max(min_tck->tRRD, val) - 1;
448 tim1 |= val << T_RRD_SHIFT;
450 val = DIV_ROUND_UP(timings->tRAS_min + timings->tRPab + 1875, t_ck);
451 tim1 |= (val - 1) << T_RC_SHIFT;
453 val = DIV_ROUND_UP(timings->tRAS_min + 1875, t_ck);
454 val = max(min_tck->tRASmin, val) - 1;
455 tim1 |= val << T_RAS_SHIFT;
457 val = max(min_tck->tWR, DIV_ROUND_UP(timings->tWR, t_ck)) - 1;
458 tim1 |= val << T_WR_SHIFT;
460 val = max(min_tck->tRCD, DIV_ROUND_UP(timings->tRCD + 1875, t_ck));
461 tim1 |= (val - 1) << T_RCD_SHIFT;
463 val = max(min_tck->tRPab, DIV_ROUND_UP(timings->tRPab + 1875, t_ck));
464 tim1 |= (val - 1) << T_RP_SHIFT;
466 return tim1;
469 static u32 get_sdram_tim_2_shdw(const struct lpddr2_timings *timings,
470 const struct lpddr2_min_tck *min_tck,
471 const struct lpddr2_addressing *addressing,
472 u32 type)
474 u32 tim2 = 0, val = 0;
476 val = min_tck->tCKE - 1;
477 tim2 |= val << T_CKE_SHIFT;
479 val = max(min_tck->tRTP, DIV_ROUND_UP(timings->tRTP, t_ck)) - 1;
480 tim2 |= val << T_RTP_SHIFT;
482 /* tXSNR = tRFCab_ps + 10 ns(tRFCab_ps for LPDDR2). */
483 val = DIV_ROUND_UP(addressing->tRFCab_ps + 10000, t_ck) - 1;
484 tim2 |= val << T_XSNR_SHIFT;
486 /* XSRD same as XSNR for LPDDR2 */
487 tim2 |= val << T_XSRD_SHIFT;
489 val = max(min_tck->tXP, DIV_ROUND_UP(timings->tXP, t_ck)) - 1;
490 tim2 |= val << T_XP_SHIFT;
492 return tim2;
495 static u32 get_sdram_tim_3_shdw(const struct lpddr2_timings *timings,
496 const struct lpddr2_min_tck *min_tck,
497 const struct lpddr2_addressing *addressing,
498 u32 type, u32 ip_rev, u32 derated)
500 u32 tim3 = 0, val = 0, t_dqsck;
502 val = timings->tRAS_max_ns / addressing->tREFI_ns - 1;
503 val = val > 0xF ? 0xF : val;
504 tim3 |= val << T_RAS_MAX_SHIFT;
506 val = DIV_ROUND_UP(addressing->tRFCab_ps, t_ck) - 1;
507 tim3 |= val << T_RFC_SHIFT;
509 t_dqsck = (derated == EMIF_DERATED_TIMINGS) ?
510 timings->tDQSCK_max_derated : timings->tDQSCK_max;
511 if (ip_rev == EMIF_4D5)
512 val = DIV_ROUND_UP(t_dqsck + 1000, t_ck) - 1;
513 else
514 val = DIV_ROUND_UP(t_dqsck, t_ck) - 1;
516 tim3 |= val << T_TDQSCKMAX_SHIFT;
518 val = DIV_ROUND_UP(timings->tZQCS, t_ck) - 1;
519 tim3 |= val << ZQ_ZQCS_SHIFT;
521 val = DIV_ROUND_UP(timings->tCKESR, t_ck);
522 val = max(min_tck->tCKESR, val) - 1;
523 tim3 |= val << T_CKESR_SHIFT;
525 if (ip_rev == EMIF_4D5) {
526 tim3 |= (EMIF_T_CSTA - 1) << T_CSTA_SHIFT;
528 val = DIV_ROUND_UP(EMIF_T_PDLL_UL, 128) - 1;
529 tim3 |= val << T_PDLL_UL_SHIFT;
532 return tim3;
535 static u32 get_zq_config_reg(const struct lpddr2_addressing *addressing,
536 bool cs1_used, bool cal_resistors_per_cs)
538 u32 zq = 0, val = 0;
540 val = EMIF_ZQCS_INTERVAL_US * 1000 / addressing->tREFI_ns;
541 zq |= val << ZQ_REFINTERVAL_SHIFT;
543 val = DIV_ROUND_UP(T_ZQCL_DEFAULT_NS, T_ZQCS_DEFAULT_NS) - 1;
544 zq |= val << ZQ_ZQCL_MULT_SHIFT;
546 val = DIV_ROUND_UP(T_ZQINIT_DEFAULT_NS, T_ZQCL_DEFAULT_NS) - 1;
547 zq |= val << ZQ_ZQINIT_MULT_SHIFT;
549 zq |= ZQ_SFEXITEN_ENABLE << ZQ_SFEXITEN_SHIFT;
551 if (cal_resistors_per_cs)
552 zq |= ZQ_DUALCALEN_ENABLE << ZQ_DUALCALEN_SHIFT;
553 else
554 zq |= ZQ_DUALCALEN_DISABLE << ZQ_DUALCALEN_SHIFT;
556 zq |= ZQ_CS0EN_MASK; /* CS0 is used for sure */
558 val = cs1_used ? 1 : 0;
559 zq |= val << ZQ_CS1EN_SHIFT;
561 return zq;
564 static u32 get_temp_alert_config(const struct lpddr2_addressing *addressing,
565 const struct emif_custom_configs *custom_configs, bool cs1_used,
566 u32 sdram_io_width, u32 emif_bus_width)
568 u32 alert = 0, interval, devcnt;
570 if (custom_configs && (custom_configs->mask &
571 EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL))
572 interval = custom_configs->temp_alert_poll_interval_ms;
573 else
574 interval = TEMP_ALERT_POLL_INTERVAL_DEFAULT_MS;
576 interval *= 1000000; /* Convert to ns */
577 interval /= addressing->tREFI_ns; /* Convert to refresh cycles */
578 alert |= (interval << TA_REFINTERVAL_SHIFT);
581 * sdram_io_width is in 'log2(x) - 1' form. Convert emif_bus_width
582 * also to this form and subtract to get TA_DEVCNT, which is
583 * in log2(x) form.
585 emif_bus_width = __fls(emif_bus_width) - 1;
586 devcnt = emif_bus_width - sdram_io_width;
587 alert |= devcnt << TA_DEVCNT_SHIFT;
589 /* DEVWDT is in 'log2(x) - 3' form */
590 alert |= (sdram_io_width - 2) << TA_DEVWDT_SHIFT;
592 alert |= 1 << TA_SFEXITEN_SHIFT;
593 alert |= 1 << TA_CS0EN_SHIFT;
594 alert |= (cs1_used ? 1 : 0) << TA_CS1EN_SHIFT;
596 return alert;
599 static u32 get_read_idle_ctrl_shdw(u8 volt_ramp)
601 u32 idle = 0, val = 0;
604 * Maximum value in normal conditions and increased frequency
605 * when voltage is ramping
607 if (volt_ramp)
608 val = READ_IDLE_INTERVAL_DVFS / t_ck / 64 - 1;
609 else
610 val = 0x1FF;
613 * READ_IDLE_CTRL register in EMIF4D has same offset and fields
614 * as DLL_CALIB_CTRL in EMIF4D5, so use the same shifts
616 idle |= val << DLL_CALIB_INTERVAL_SHIFT;
617 idle |= EMIF_READ_IDLE_LEN_VAL << ACK_WAIT_SHIFT;
619 return idle;
622 static u32 get_dll_calib_ctrl_shdw(u8 volt_ramp)
624 u32 calib = 0, val = 0;
626 if (volt_ramp == DDR_VOLTAGE_RAMPING)
627 val = DLL_CALIB_INTERVAL_DVFS / t_ck / 16 - 1;
628 else
629 val = 0; /* Disabled when voltage is stable */
631 calib |= val << DLL_CALIB_INTERVAL_SHIFT;
632 calib |= DLL_CALIB_ACK_WAIT_VAL << ACK_WAIT_SHIFT;
634 return calib;
637 static u32 get_ddr_phy_ctrl_1_attilaphy_4d(const struct lpddr2_timings *timings,
638 u32 freq, u8 RL)
640 u32 phy = EMIF_DDR_PHY_CTRL_1_BASE_VAL_ATTILAPHY, val = 0;
642 val = RL + DIV_ROUND_UP(timings->tDQSCK_max, t_ck) - 1;
643 phy |= val << READ_LATENCY_SHIFT_4D;
645 if (freq <= 100000000)
646 val = EMIF_DLL_SLAVE_DLY_CTRL_100_MHZ_AND_LESS_ATTILAPHY;
647 else if (freq <= 200000000)
648 val = EMIF_DLL_SLAVE_DLY_CTRL_200_MHZ_ATTILAPHY;
649 else
650 val = EMIF_DLL_SLAVE_DLY_CTRL_400_MHZ_ATTILAPHY;
652 phy |= val << DLL_SLAVE_DLY_CTRL_SHIFT_4D;
654 return phy;
657 static u32 get_phy_ctrl_1_intelliphy_4d5(u32 freq, u8 cl)
659 u32 phy = EMIF_DDR_PHY_CTRL_1_BASE_VAL_INTELLIPHY, half_delay;
662 * DLL operates at 266 MHz. If DDR frequency is near 266 MHz,
663 * half-delay is not needed else set half-delay
665 if (freq >= 265000000 && freq < 267000000)
666 half_delay = 0;
667 else
668 half_delay = 1;
670 phy |= half_delay << DLL_HALF_DELAY_SHIFT_4D5;
671 phy |= ((cl + DIV_ROUND_UP(EMIF_PHY_TOTAL_READ_LATENCY_INTELLIPHY_PS,
672 t_ck) - 1) << READ_LATENCY_SHIFT_4D5);
674 return phy;
677 static u32 get_ext_phy_ctrl_2_intelliphy_4d5(void)
679 u32 fifo_we_slave_ratio;
681 fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
682 EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
684 return fifo_we_slave_ratio | fifo_we_slave_ratio << 11 |
685 fifo_we_slave_ratio << 22;
688 static u32 get_ext_phy_ctrl_3_intelliphy_4d5(void)
690 u32 fifo_we_slave_ratio;
692 fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
693 EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
695 return fifo_we_slave_ratio >> 10 | fifo_we_slave_ratio << 1 |
696 fifo_we_slave_ratio << 12 | fifo_we_slave_ratio << 23;
699 static u32 get_ext_phy_ctrl_4_intelliphy_4d5(void)
701 u32 fifo_we_slave_ratio;
703 fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
704 EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
706 return fifo_we_slave_ratio >> 9 | fifo_we_slave_ratio << 2 |
707 fifo_we_slave_ratio << 13;
710 static u32 get_pwr_mgmt_ctrl(u32 freq, struct emif_data *emif, u32 ip_rev)
712 u32 pwr_mgmt_ctrl = 0, timeout;
713 u32 lpmode = EMIF_LP_MODE_SELF_REFRESH;
714 u32 timeout_perf = EMIF_LP_MODE_TIMEOUT_PERFORMANCE;
715 u32 timeout_pwr = EMIF_LP_MODE_TIMEOUT_POWER;
716 u32 freq_threshold = EMIF_LP_MODE_FREQ_THRESHOLD;
718 struct emif_custom_configs *cust_cfgs = emif->plat_data->custom_configs;
720 if (cust_cfgs && (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE)) {
721 lpmode = cust_cfgs->lpmode;
722 timeout_perf = cust_cfgs->lpmode_timeout_performance;
723 timeout_pwr = cust_cfgs->lpmode_timeout_power;
724 freq_threshold = cust_cfgs->lpmode_freq_threshold;
727 /* Timeout based on DDR frequency */
728 timeout = freq >= freq_threshold ? timeout_perf : timeout_pwr;
730 /* The value to be set in register is "log2(timeout) - 3" */
731 if (timeout < 16) {
732 timeout = 0;
733 } else {
734 timeout = __fls(timeout) - 3;
735 if (timeout & (timeout - 1))
736 timeout++;
739 switch (lpmode) {
740 case EMIF_LP_MODE_CLOCK_STOP:
741 pwr_mgmt_ctrl = (timeout << CS_TIM_SHIFT) |
742 SR_TIM_MASK | PD_TIM_MASK;
743 break;
744 case EMIF_LP_MODE_SELF_REFRESH:
745 /* Workaround for errata i735 */
746 if (timeout < 6)
747 timeout = 6;
749 pwr_mgmt_ctrl = (timeout << SR_TIM_SHIFT) |
750 CS_TIM_MASK | PD_TIM_MASK;
751 break;
752 case EMIF_LP_MODE_PWR_DN:
753 pwr_mgmt_ctrl = (timeout << PD_TIM_SHIFT) |
754 CS_TIM_MASK | SR_TIM_MASK;
755 break;
756 case EMIF_LP_MODE_DISABLE:
757 default:
758 pwr_mgmt_ctrl = CS_TIM_MASK |
759 PD_TIM_MASK | SR_TIM_MASK;
762 /* No CS_TIM in EMIF_4D5 */
763 if (ip_rev == EMIF_4D5)
764 pwr_mgmt_ctrl &= ~CS_TIM_MASK;
766 pwr_mgmt_ctrl |= lpmode << LP_MODE_SHIFT;
768 return pwr_mgmt_ctrl;
772 * Get the temperature level of the EMIF instance:
773 * Reads the MR4 register of attached SDRAM parts to find out the temperature
774 * level. If there are two parts attached(one on each CS), then the temperature
775 * level for the EMIF instance is the higher of the two temperatures.
777 static void get_temperature_level(struct emif_data *emif)
779 u32 temp, temperature_level;
780 void __iomem *base;
782 base = emif->base;
784 /* Read mode register 4 */
785 writel(DDR_MR4, base + EMIF_LPDDR2_MODE_REG_CONFIG);
786 temperature_level = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
787 temperature_level = (temperature_level & MR4_SDRAM_REF_RATE_MASK) >>
788 MR4_SDRAM_REF_RATE_SHIFT;
790 if (emif->plat_data->device_info->cs1_used) {
791 writel(DDR_MR4 | CS_MASK, base + EMIF_LPDDR2_MODE_REG_CONFIG);
792 temp = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
793 temp = (temp & MR4_SDRAM_REF_RATE_MASK)
794 >> MR4_SDRAM_REF_RATE_SHIFT;
795 temperature_level = max(temp, temperature_level);
798 /* treat everything less than nominal(3) in MR4 as nominal */
799 if (unlikely(temperature_level < SDRAM_TEMP_NOMINAL))
800 temperature_level = SDRAM_TEMP_NOMINAL;
802 /* if we get reserved value in MR4 persist with the existing value */
803 if (likely(temperature_level != SDRAM_TEMP_RESERVED_4))
804 emif->temperature_level = temperature_level;
808 * Program EMIF shadow registers that are not dependent on temperature
809 * or voltage
811 static void setup_registers(struct emif_data *emif, struct emif_regs *regs)
813 void __iomem *base = emif->base;
815 writel(regs->sdram_tim2_shdw, base + EMIF_SDRAM_TIMING_2_SHDW);
816 writel(regs->phy_ctrl_1_shdw, base + EMIF_DDR_PHY_CTRL_1_SHDW);
818 /* Settings specific for EMIF4D5 */
819 if (emif->plat_data->ip_rev != EMIF_4D5)
820 return;
821 writel(regs->ext_phy_ctrl_2_shdw, base + EMIF_EXT_PHY_CTRL_2_SHDW);
822 writel(regs->ext_phy_ctrl_3_shdw, base + EMIF_EXT_PHY_CTRL_3_SHDW);
823 writel(regs->ext_phy_ctrl_4_shdw, base + EMIF_EXT_PHY_CTRL_4_SHDW);
827 * When voltage ramps dll calibration and forced read idle should
828 * happen more often
830 static void setup_volt_sensitive_regs(struct emif_data *emif,
831 struct emif_regs *regs, u32 volt_state)
833 u32 calib_ctrl;
834 void __iomem *base = emif->base;
837 * EMIF_READ_IDLE_CTRL in EMIF4D refers to the same register as
838 * EMIF_DLL_CALIB_CTRL in EMIF4D5 and dll_calib_ctrl_shadow_*
839 * is an alias of the respective read_idle_ctrl_shdw_* (members of
840 * a union). So, the below code takes care of both cases
842 if (volt_state == DDR_VOLTAGE_RAMPING)
843 calib_ctrl = regs->dll_calib_ctrl_shdw_volt_ramp;
844 else
845 calib_ctrl = regs->dll_calib_ctrl_shdw_normal;
847 writel(calib_ctrl, base + EMIF_DLL_CALIB_CTRL_SHDW);
851 * setup_temperature_sensitive_regs() - set the timings for temperature
852 * sensitive registers. This happens once at initialisation time based
853 * on the temperature at boot time and subsequently based on the temperature
854 * alert interrupt. Temperature alert can happen when the temperature
855 * increases or drops. So this function can have the effect of either
856 * derating the timings or going back to nominal values.
858 static void setup_temperature_sensitive_regs(struct emif_data *emif,
859 struct emif_regs *regs)
861 u32 tim1, tim3, ref_ctrl, type;
862 void __iomem *base = emif->base;
863 u32 temperature;
865 type = emif->plat_data->device_info->type;
867 tim1 = regs->sdram_tim1_shdw;
868 tim3 = regs->sdram_tim3_shdw;
869 ref_ctrl = regs->ref_ctrl_shdw;
871 /* No de-rating for non-lpddr2 devices */
872 if (type != DDR_TYPE_LPDDR2_S2 && type != DDR_TYPE_LPDDR2_S4)
873 goto out;
875 temperature = emif->temperature_level;
876 if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH) {
877 ref_ctrl = regs->ref_ctrl_shdw_derated;
878 } else if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH_AND_TIMINGS) {
879 tim1 = regs->sdram_tim1_shdw_derated;
880 tim3 = regs->sdram_tim3_shdw_derated;
881 ref_ctrl = regs->ref_ctrl_shdw_derated;
884 out:
885 writel(tim1, base + EMIF_SDRAM_TIMING_1_SHDW);
886 writel(tim3, base + EMIF_SDRAM_TIMING_3_SHDW);
887 writel(ref_ctrl, base + EMIF_SDRAM_REFRESH_CTRL_SHDW);
890 static irqreturn_t handle_temp_alert(void __iomem *base, struct emif_data *emif)
892 u32 old_temp_level;
893 irqreturn_t ret = IRQ_HANDLED;
895 spin_lock_irqsave(&emif_lock, irq_state);
896 old_temp_level = emif->temperature_level;
897 get_temperature_level(emif);
899 if (unlikely(emif->temperature_level == old_temp_level)) {
900 goto out;
901 } else if (!emif->curr_regs) {
902 dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
903 goto out;
906 if (emif->temperature_level < old_temp_level ||
907 emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
909 * Temperature coming down - defer handling to thread OR
910 * Temperature far too high - do kernel_power_off() from
911 * thread context
913 ret = IRQ_WAKE_THREAD;
914 } else {
915 /* Temperature is going up - handle immediately */
916 setup_temperature_sensitive_regs(emif, emif->curr_regs);
917 do_freq_update();
920 out:
921 spin_unlock_irqrestore(&emif_lock, irq_state);
922 return ret;
925 static irqreturn_t emif_interrupt_handler(int irq, void *dev_id)
927 u32 interrupts;
928 struct emif_data *emif = dev_id;
929 void __iomem *base = emif->base;
930 struct device *dev = emif->dev;
931 irqreturn_t ret = IRQ_HANDLED;
933 /* Save the status and clear it */
934 interrupts = readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
935 writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
938 * Handle temperature alert
939 * Temperature alert should be same for all ports
940 * So, it's enough to process it only for one of the ports
942 if (interrupts & TA_SYS_MASK)
943 ret = handle_temp_alert(base, emif);
945 if (interrupts & ERR_SYS_MASK)
946 dev_err(dev, "Access error from SYS port - %x\n", interrupts);
948 if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
949 /* Save the status and clear it */
950 interrupts = readl(base + EMIF_LL_OCP_INTERRUPT_STATUS);
951 writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_STATUS);
953 if (interrupts & ERR_LL_MASK)
954 dev_err(dev, "Access error from LL port - %x\n",
955 interrupts);
958 return ret;
961 static irqreturn_t emif_threaded_isr(int irq, void *dev_id)
963 struct emif_data *emif = dev_id;
965 if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
966 dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
967 kernel_power_off();
968 return IRQ_HANDLED;
971 spin_lock_irqsave(&emif_lock, irq_state);
973 if (emif->curr_regs) {
974 setup_temperature_sensitive_regs(emif, emif->curr_regs);
975 do_freq_update();
976 } else {
977 dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
980 spin_unlock_irqrestore(&emif_lock, irq_state);
982 return IRQ_HANDLED;
985 static void clear_all_interrupts(struct emif_data *emif)
987 void __iomem *base = emif->base;
989 writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS),
990 base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
991 if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
992 writel(readl(base + EMIF_LL_OCP_INTERRUPT_STATUS),
993 base + EMIF_LL_OCP_INTERRUPT_STATUS);
996 static void disable_and_clear_all_interrupts(struct emif_data *emif)
998 void __iomem *base = emif->base;
1000 /* Disable all interrupts */
1001 writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET),
1002 base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_CLEAR);
1003 if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
1004 writel(readl(base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET),
1005 base + EMIF_LL_OCP_INTERRUPT_ENABLE_CLEAR);
1007 /* Clear all interrupts */
1008 clear_all_interrupts(emif);
1011 static int __init_or_module setup_interrupts(struct emif_data *emif, u32 irq)
1013 u32 interrupts, type;
1014 void __iomem *base = emif->base;
1016 type = emif->plat_data->device_info->type;
1018 clear_all_interrupts(emif);
1020 /* Enable interrupts for SYS interface */
1021 interrupts = EN_ERR_SYS_MASK;
1022 if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4)
1023 interrupts |= EN_TA_SYS_MASK;
1024 writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET);
1026 /* Enable interrupts for LL interface */
1027 if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
1028 /* TA need not be enabled for LL */
1029 interrupts = EN_ERR_LL_MASK;
1030 writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET);
1033 /* setup IRQ handlers */
1034 return devm_request_threaded_irq(emif->dev, irq,
1035 emif_interrupt_handler,
1036 emif_threaded_isr,
1037 0, dev_name(emif->dev),
1038 emif);
1042 static void __init_or_module emif_onetime_settings(struct emif_data *emif)
1044 u32 pwr_mgmt_ctrl, zq, temp_alert_cfg;
1045 void __iomem *base = emif->base;
1046 const struct lpddr2_addressing *addressing;
1047 const struct ddr_device_info *device_info;
1049 device_info = emif->plat_data->device_info;
1050 addressing = get_addressing_table(device_info);
1053 * Init power management settings
1054 * We don't know the frequency yet. Use a high frequency
1055 * value for a conservative timeout setting
1057 pwr_mgmt_ctrl = get_pwr_mgmt_ctrl(1000000000, emif,
1058 emif->plat_data->ip_rev);
1059 emif->lpmode = (pwr_mgmt_ctrl & LP_MODE_MASK) >> LP_MODE_SHIFT;
1060 writel(pwr_mgmt_ctrl, base + EMIF_POWER_MANAGEMENT_CONTROL);
1062 /* Init ZQ calibration settings */
1063 zq = get_zq_config_reg(addressing, device_info->cs1_used,
1064 device_info->cal_resistors_per_cs);
1065 writel(zq, base + EMIF_SDRAM_OUTPUT_IMPEDANCE_CALIBRATION_CONFIG);
1067 /* Check temperature level temperature level*/
1068 get_temperature_level(emif);
1069 if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN)
1070 dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
1072 /* Init temperature polling */
1073 temp_alert_cfg = get_temp_alert_config(addressing,
1074 emif->plat_data->custom_configs, device_info->cs1_used,
1075 device_info->io_width, get_emif_bus_width(emif));
1076 writel(temp_alert_cfg, base + EMIF_TEMPERATURE_ALERT_CONFIG);
1079 * Program external PHY control registers that are not frequency
1080 * dependent
1082 if (emif->plat_data->phy_type != EMIF_PHY_TYPE_INTELLIPHY)
1083 return;
1084 writel(EMIF_EXT_PHY_CTRL_1_VAL, base + EMIF_EXT_PHY_CTRL_1_SHDW);
1085 writel(EMIF_EXT_PHY_CTRL_5_VAL, base + EMIF_EXT_PHY_CTRL_5_SHDW);
1086 writel(EMIF_EXT_PHY_CTRL_6_VAL, base + EMIF_EXT_PHY_CTRL_6_SHDW);
1087 writel(EMIF_EXT_PHY_CTRL_7_VAL, base + EMIF_EXT_PHY_CTRL_7_SHDW);
1088 writel(EMIF_EXT_PHY_CTRL_8_VAL, base + EMIF_EXT_PHY_CTRL_8_SHDW);
1089 writel(EMIF_EXT_PHY_CTRL_9_VAL, base + EMIF_EXT_PHY_CTRL_9_SHDW);
1090 writel(EMIF_EXT_PHY_CTRL_10_VAL, base + EMIF_EXT_PHY_CTRL_10_SHDW);
1091 writel(EMIF_EXT_PHY_CTRL_11_VAL, base + EMIF_EXT_PHY_CTRL_11_SHDW);
1092 writel(EMIF_EXT_PHY_CTRL_12_VAL, base + EMIF_EXT_PHY_CTRL_12_SHDW);
1093 writel(EMIF_EXT_PHY_CTRL_13_VAL, base + EMIF_EXT_PHY_CTRL_13_SHDW);
1094 writel(EMIF_EXT_PHY_CTRL_14_VAL, base + EMIF_EXT_PHY_CTRL_14_SHDW);
1095 writel(EMIF_EXT_PHY_CTRL_15_VAL, base + EMIF_EXT_PHY_CTRL_15_SHDW);
1096 writel(EMIF_EXT_PHY_CTRL_16_VAL, base + EMIF_EXT_PHY_CTRL_16_SHDW);
1097 writel(EMIF_EXT_PHY_CTRL_17_VAL, base + EMIF_EXT_PHY_CTRL_17_SHDW);
1098 writel(EMIF_EXT_PHY_CTRL_18_VAL, base + EMIF_EXT_PHY_CTRL_18_SHDW);
1099 writel(EMIF_EXT_PHY_CTRL_19_VAL, base + EMIF_EXT_PHY_CTRL_19_SHDW);
1100 writel(EMIF_EXT_PHY_CTRL_20_VAL, base + EMIF_EXT_PHY_CTRL_20_SHDW);
1101 writel(EMIF_EXT_PHY_CTRL_21_VAL, base + EMIF_EXT_PHY_CTRL_21_SHDW);
1102 writel(EMIF_EXT_PHY_CTRL_22_VAL, base + EMIF_EXT_PHY_CTRL_22_SHDW);
1103 writel(EMIF_EXT_PHY_CTRL_23_VAL, base + EMIF_EXT_PHY_CTRL_23_SHDW);
1104 writel(EMIF_EXT_PHY_CTRL_24_VAL, base + EMIF_EXT_PHY_CTRL_24_SHDW);
1107 static void get_default_timings(struct emif_data *emif)
1109 struct emif_platform_data *pd = emif->plat_data;
1111 pd->timings = lpddr2_jedec_timings;
1112 pd->timings_arr_size = ARRAY_SIZE(lpddr2_jedec_timings);
1114 dev_warn(emif->dev, "%s: using default timings\n", __func__);
1117 static int is_dev_data_valid(u32 type, u32 density, u32 io_width, u32 phy_type,
1118 u32 ip_rev, struct device *dev)
1120 int valid;
1122 valid = (type == DDR_TYPE_LPDDR2_S4 ||
1123 type == DDR_TYPE_LPDDR2_S2)
1124 && (density >= DDR_DENSITY_64Mb
1125 && density <= DDR_DENSITY_8Gb)
1126 && (io_width >= DDR_IO_WIDTH_8
1127 && io_width <= DDR_IO_WIDTH_32);
1129 /* Combinations of EMIF and PHY revisions that we support today */
1130 switch (ip_rev) {
1131 case EMIF_4D:
1132 valid = valid && (phy_type == EMIF_PHY_TYPE_ATTILAPHY);
1133 break;
1134 case EMIF_4D5:
1135 valid = valid && (phy_type == EMIF_PHY_TYPE_INTELLIPHY);
1136 break;
1137 default:
1138 valid = 0;
1141 if (!valid)
1142 dev_err(dev, "%s: invalid DDR details\n", __func__);
1143 return valid;
1146 static int is_custom_config_valid(struct emif_custom_configs *cust_cfgs,
1147 struct device *dev)
1149 int valid = 1;
1151 if ((cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE) &&
1152 (cust_cfgs->lpmode != EMIF_LP_MODE_DISABLE))
1153 valid = cust_cfgs->lpmode_freq_threshold &&
1154 cust_cfgs->lpmode_timeout_performance &&
1155 cust_cfgs->lpmode_timeout_power;
1157 if (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL)
1158 valid = valid && cust_cfgs->temp_alert_poll_interval_ms;
1160 if (!valid)
1161 dev_warn(dev, "%s: invalid custom configs\n", __func__);
1163 return valid;
1166 #if defined(CONFIG_OF)
1167 static void __init_or_module of_get_custom_configs(struct device_node *np_emif,
1168 struct emif_data *emif)
1170 struct emif_custom_configs *cust_cfgs = NULL;
1171 int len;
1172 const int *lpmode, *poll_intvl;
1174 lpmode = of_get_property(np_emif, "low-power-mode", &len);
1175 poll_intvl = of_get_property(np_emif, "temp-alert-poll-interval", &len);
1177 if (lpmode || poll_intvl)
1178 cust_cfgs = devm_kzalloc(emif->dev, sizeof(*cust_cfgs),
1179 GFP_KERNEL);
1181 if (!cust_cfgs)
1182 return;
1184 if (lpmode) {
1185 cust_cfgs->mask |= EMIF_CUSTOM_CONFIG_LPMODE;
1186 cust_cfgs->lpmode = *lpmode;
1187 of_property_read_u32(np_emif,
1188 "low-power-mode-timeout-performance",
1189 &cust_cfgs->lpmode_timeout_performance);
1190 of_property_read_u32(np_emif,
1191 "low-power-mode-timeout-power",
1192 &cust_cfgs->lpmode_timeout_power);
1193 of_property_read_u32(np_emif,
1194 "low-power-mode-freq-threshold",
1195 &cust_cfgs->lpmode_freq_threshold);
1198 if (poll_intvl) {
1199 cust_cfgs->mask |=
1200 EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL;
1201 cust_cfgs->temp_alert_poll_interval_ms = *poll_intvl;
1204 if (!is_custom_config_valid(cust_cfgs, emif->dev)) {
1205 devm_kfree(emif->dev, cust_cfgs);
1206 return;
1209 emif->plat_data->custom_configs = cust_cfgs;
1212 static void __init_or_module of_get_ddr_info(struct device_node *np_emif,
1213 struct device_node *np_ddr,
1214 struct ddr_device_info *dev_info)
1216 u32 density = 0, io_width = 0;
1217 int len;
1219 if (of_find_property(np_emif, "cs1-used", &len))
1220 dev_info->cs1_used = true;
1222 if (of_find_property(np_emif, "cal-resistor-per-cs", &len))
1223 dev_info->cal_resistors_per_cs = true;
1225 if (of_device_is_compatible(np_ddr , "jedec,lpddr2-s4"))
1226 dev_info->type = DDR_TYPE_LPDDR2_S4;
1227 else if (of_device_is_compatible(np_ddr , "jedec,lpddr2-s2"))
1228 dev_info->type = DDR_TYPE_LPDDR2_S2;
1230 of_property_read_u32(np_ddr, "density", &density);
1231 of_property_read_u32(np_ddr, "io-width", &io_width);
1233 /* Convert from density in Mb to the density encoding in jedc_ddr.h */
1234 if (density & (density - 1))
1235 dev_info->density = 0;
1236 else
1237 dev_info->density = __fls(density) - 5;
1239 /* Convert from io_width in bits to io_width encoding in jedc_ddr.h */
1240 if (io_width & (io_width - 1))
1241 dev_info->io_width = 0;
1242 else
1243 dev_info->io_width = __fls(io_width) - 1;
1246 static struct emif_data * __init_or_module of_get_memory_device_details(
1247 struct device_node *np_emif, struct device *dev)
1249 struct emif_data *emif = NULL;
1250 struct ddr_device_info *dev_info = NULL;
1251 struct emif_platform_data *pd = NULL;
1252 struct device_node *np_ddr;
1253 int len;
1255 np_ddr = of_parse_phandle(np_emif, "device-handle", 0);
1256 if (!np_ddr)
1257 goto error;
1258 emif = devm_kzalloc(dev, sizeof(struct emif_data), GFP_KERNEL);
1259 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
1260 dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
1262 if (!emif || !pd || !dev_info) {
1263 dev_err(dev, "%s: Out of memory!!\n",
1264 __func__);
1265 goto error;
1268 emif->plat_data = pd;
1269 pd->device_info = dev_info;
1270 emif->dev = dev;
1271 emif->np_ddr = np_ddr;
1272 emif->temperature_level = SDRAM_TEMP_NOMINAL;
1274 if (of_device_is_compatible(np_emif, "ti,emif-4d"))
1275 emif->plat_data->ip_rev = EMIF_4D;
1276 else if (of_device_is_compatible(np_emif, "ti,emif-4d5"))
1277 emif->plat_data->ip_rev = EMIF_4D5;
1279 of_property_read_u32(np_emif, "phy-type", &pd->phy_type);
1281 if (of_find_property(np_emif, "hw-caps-ll-interface", &len))
1282 pd->hw_caps |= EMIF_HW_CAPS_LL_INTERFACE;
1284 of_get_ddr_info(np_emif, np_ddr, dev_info);
1285 if (!is_dev_data_valid(pd->device_info->type, pd->device_info->density,
1286 pd->device_info->io_width, pd->phy_type, pd->ip_rev,
1287 emif->dev)) {
1288 dev_err(dev, "%s: invalid device data!!\n", __func__);
1289 goto error;
1292 * For EMIF instances other than EMIF1 see if the devices connected
1293 * are exactly same as on EMIF1(which is typically the case). If so,
1294 * mark it as a duplicate of EMIF1. This will save some memory and
1295 * computation.
1297 if (emif1 && emif1->np_ddr == np_ddr) {
1298 emif->duplicate = true;
1299 goto out;
1300 } else if (emif1) {
1301 dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
1302 __func__);
1305 of_get_custom_configs(np_emif, emif);
1306 emif->plat_data->timings = of_get_ddr_timings(np_ddr, emif->dev,
1307 emif->plat_data->device_info->type,
1308 &emif->plat_data->timings_arr_size);
1310 emif->plat_data->min_tck = of_get_min_tck(np_ddr, emif->dev);
1311 goto out;
1313 error:
1314 return NULL;
1315 out:
1316 return emif;
1319 #else
1321 static struct emif_data * __init_or_module of_get_memory_device_details(
1322 struct device_node *np_emif, struct device *dev)
1324 return NULL;
1326 #endif
1328 static struct emif_data *__init_or_module get_device_details(
1329 struct platform_device *pdev)
1331 u32 size;
1332 struct emif_data *emif = NULL;
1333 struct ddr_device_info *dev_info;
1334 struct emif_custom_configs *cust_cfgs;
1335 struct emif_platform_data *pd;
1336 struct device *dev;
1337 void *temp;
1339 pd = pdev->dev.platform_data;
1340 dev = &pdev->dev;
1342 if (!(pd && pd->device_info && is_dev_data_valid(pd->device_info->type,
1343 pd->device_info->density, pd->device_info->io_width,
1344 pd->phy_type, pd->ip_rev, dev))) {
1345 dev_err(dev, "%s: invalid device data\n", __func__);
1346 goto error;
1349 emif = devm_kzalloc(dev, sizeof(*emif), GFP_KERNEL);
1350 temp = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
1351 dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
1353 if (!emif || !pd || !dev_info) {
1354 dev_err(dev, "%s:%d: allocation error\n", __func__, __LINE__);
1355 goto error;
1358 memcpy(temp, pd, sizeof(*pd));
1359 pd = temp;
1360 memcpy(dev_info, pd->device_info, sizeof(*dev_info));
1362 pd->device_info = dev_info;
1363 emif->plat_data = pd;
1364 emif->dev = dev;
1365 emif->temperature_level = SDRAM_TEMP_NOMINAL;
1368 * For EMIF instances other than EMIF1 see if the devices connected
1369 * are exactly same as on EMIF1(which is typically the case). If so,
1370 * mark it as a duplicate of EMIF1 and skip copying timings data.
1371 * This will save some memory and some computation later.
1373 emif->duplicate = emif1 && (memcmp(dev_info,
1374 emif1->plat_data->device_info,
1375 sizeof(struct ddr_device_info)) == 0);
1377 if (emif->duplicate) {
1378 pd->timings = NULL;
1379 pd->min_tck = NULL;
1380 goto out;
1381 } else if (emif1) {
1382 dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
1383 __func__);
1387 * Copy custom configs - ignore allocation error, if any, as
1388 * custom_configs is not very critical
1390 cust_cfgs = pd->custom_configs;
1391 if (cust_cfgs && is_custom_config_valid(cust_cfgs, dev)) {
1392 temp = devm_kzalloc(dev, sizeof(*cust_cfgs), GFP_KERNEL);
1393 if (temp)
1394 memcpy(temp, cust_cfgs, sizeof(*cust_cfgs));
1395 else
1396 dev_warn(dev, "%s:%d: allocation error\n", __func__,
1397 __LINE__);
1398 pd->custom_configs = temp;
1402 * Copy timings and min-tck values from platform data. If it is not
1403 * available or if memory allocation fails, use JEDEC defaults
1405 size = sizeof(struct lpddr2_timings) * pd->timings_arr_size;
1406 if (pd->timings) {
1407 temp = devm_kzalloc(dev, size, GFP_KERNEL);
1408 if (temp) {
1409 memcpy(temp, pd->timings, sizeof(*pd->timings));
1410 pd->timings = temp;
1411 } else {
1412 dev_warn(dev, "%s:%d: allocation error\n", __func__,
1413 __LINE__);
1414 get_default_timings(emif);
1416 } else {
1417 get_default_timings(emif);
1420 if (pd->min_tck) {
1421 temp = devm_kzalloc(dev, sizeof(*pd->min_tck), GFP_KERNEL);
1422 if (temp) {
1423 memcpy(temp, pd->min_tck, sizeof(*pd->min_tck));
1424 pd->min_tck = temp;
1425 } else {
1426 dev_warn(dev, "%s:%d: allocation error\n", __func__,
1427 __LINE__);
1428 pd->min_tck = &lpddr2_jedec_min_tck;
1430 } else {
1431 pd->min_tck = &lpddr2_jedec_min_tck;
1434 out:
1435 return emif;
1437 error:
1438 return NULL;
1441 static int __init_or_module emif_probe(struct platform_device *pdev)
1443 struct emif_data *emif;
1444 struct resource *res;
1445 int irq;
1447 if (pdev->dev.of_node)
1448 emif = of_get_memory_device_details(pdev->dev.of_node, &pdev->dev);
1449 else
1450 emif = get_device_details(pdev);
1452 if (!emif) {
1453 pr_err("%s: error getting device data\n", __func__);
1454 goto error;
1457 list_add(&emif->node, &device_list);
1458 emif->addressing = get_addressing_table(emif->plat_data->device_info);
1460 /* Save pointers to each other in emif and device structures */
1461 emif->dev = &pdev->dev;
1462 platform_set_drvdata(pdev, emif);
1464 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1465 if (!res) {
1466 dev_err(emif->dev, "%s: error getting memory resource\n",
1467 __func__);
1468 goto error;
1471 emif->base = devm_request_and_ioremap(emif->dev, res);
1472 if (!emif->base) {
1473 dev_err(emif->dev, "%s: devm_request_and_ioremap() failed\n",
1474 __func__);
1475 goto error;
1478 irq = platform_get_irq(pdev, 0);
1479 if (irq < 0) {
1480 dev_err(emif->dev, "%s: error getting IRQ resource - %d\n",
1481 __func__, irq);
1482 goto error;
1485 emif_onetime_settings(emif);
1486 emif_debugfs_init(emif);
1487 disable_and_clear_all_interrupts(emif);
1488 setup_interrupts(emif, irq);
1490 /* One-time actions taken on probing the first device */
1491 if (!emif1) {
1492 emif1 = emif;
1493 spin_lock_init(&emif_lock);
1496 * TODO: register notifiers for frequency and voltage
1497 * change here once the respective frameworks are
1498 * available
1502 dev_info(&pdev->dev, "%s: device configured with addr = %p and IRQ%d\n",
1503 __func__, emif->base, irq);
1505 return 0;
1506 error:
1507 return -ENODEV;
1510 static int __exit emif_remove(struct platform_device *pdev)
1512 struct emif_data *emif = platform_get_drvdata(pdev);
1514 emif_debugfs_exit(emif);
1516 return 0;
1519 static void emif_shutdown(struct platform_device *pdev)
1521 struct emif_data *emif = platform_get_drvdata(pdev);
1523 disable_and_clear_all_interrupts(emif);
1526 static int get_emif_reg_values(struct emif_data *emif, u32 freq,
1527 struct emif_regs *regs)
1529 u32 cs1_used, ip_rev, phy_type;
1530 u32 cl, type;
1531 const struct lpddr2_timings *timings;
1532 const struct lpddr2_min_tck *min_tck;
1533 const struct ddr_device_info *device_info;
1534 const struct lpddr2_addressing *addressing;
1535 struct emif_data *emif_for_calc;
1536 struct device *dev;
1537 const struct emif_custom_configs *custom_configs;
1539 dev = emif->dev;
1541 * If the devices on this EMIF instance is duplicate of EMIF1,
1542 * use EMIF1 details for the calculation
1544 emif_for_calc = emif->duplicate ? emif1 : emif;
1545 timings = get_timings_table(emif_for_calc, freq);
1546 addressing = emif_for_calc->addressing;
1547 if (!timings || !addressing) {
1548 dev_err(dev, "%s: not enough data available for %dHz",
1549 __func__, freq);
1550 return -1;
1553 device_info = emif_for_calc->plat_data->device_info;
1554 type = device_info->type;
1555 cs1_used = device_info->cs1_used;
1556 ip_rev = emif_for_calc->plat_data->ip_rev;
1557 phy_type = emif_for_calc->plat_data->phy_type;
1559 min_tck = emif_for_calc->plat_data->min_tck;
1560 custom_configs = emif_for_calc->plat_data->custom_configs;
1562 set_ddr_clk_period(freq);
1564 regs->ref_ctrl_shdw = get_sdram_ref_ctrl_shdw(freq, addressing);
1565 regs->sdram_tim1_shdw = get_sdram_tim_1_shdw(timings, min_tck,
1566 addressing);
1567 regs->sdram_tim2_shdw = get_sdram_tim_2_shdw(timings, min_tck,
1568 addressing, type);
1569 regs->sdram_tim3_shdw = get_sdram_tim_3_shdw(timings, min_tck,
1570 addressing, type, ip_rev, EMIF_NORMAL_TIMINGS);
1572 cl = get_cl(emif);
1574 if (phy_type == EMIF_PHY_TYPE_ATTILAPHY && ip_rev == EMIF_4D) {
1575 regs->phy_ctrl_1_shdw = get_ddr_phy_ctrl_1_attilaphy_4d(
1576 timings, freq, cl);
1577 } else if (phy_type == EMIF_PHY_TYPE_INTELLIPHY && ip_rev == EMIF_4D5) {
1578 regs->phy_ctrl_1_shdw = get_phy_ctrl_1_intelliphy_4d5(freq, cl);
1579 regs->ext_phy_ctrl_2_shdw = get_ext_phy_ctrl_2_intelliphy_4d5();
1580 regs->ext_phy_ctrl_3_shdw = get_ext_phy_ctrl_3_intelliphy_4d5();
1581 regs->ext_phy_ctrl_4_shdw = get_ext_phy_ctrl_4_intelliphy_4d5();
1582 } else {
1583 return -1;
1586 /* Only timeout values in pwr_mgmt_ctrl_shdw register */
1587 regs->pwr_mgmt_ctrl_shdw =
1588 get_pwr_mgmt_ctrl(freq, emif_for_calc, ip_rev) &
1589 (CS_TIM_MASK | SR_TIM_MASK | PD_TIM_MASK);
1591 if (ip_rev & EMIF_4D) {
1592 regs->read_idle_ctrl_shdw_normal =
1593 get_read_idle_ctrl_shdw(DDR_VOLTAGE_STABLE);
1595 regs->read_idle_ctrl_shdw_volt_ramp =
1596 get_read_idle_ctrl_shdw(DDR_VOLTAGE_RAMPING);
1597 } else if (ip_rev & EMIF_4D5) {
1598 regs->dll_calib_ctrl_shdw_normal =
1599 get_dll_calib_ctrl_shdw(DDR_VOLTAGE_STABLE);
1601 regs->dll_calib_ctrl_shdw_volt_ramp =
1602 get_dll_calib_ctrl_shdw(DDR_VOLTAGE_RAMPING);
1605 if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4) {
1606 regs->ref_ctrl_shdw_derated = get_sdram_ref_ctrl_shdw(freq / 4,
1607 addressing);
1609 regs->sdram_tim1_shdw_derated =
1610 get_sdram_tim_1_shdw_derated(timings, min_tck,
1611 addressing);
1613 regs->sdram_tim3_shdw_derated = get_sdram_tim_3_shdw(timings,
1614 min_tck, addressing, type, ip_rev,
1615 EMIF_DERATED_TIMINGS);
1618 regs->freq = freq;
1620 return 0;
1624 * get_regs() - gets the cached emif_regs structure for a given EMIF instance
1625 * given frequency(freq):
1627 * As an optimisation, every EMIF instance other than EMIF1 shares the
1628 * register cache with EMIF1 if the devices connected on this instance
1629 * are same as that on EMIF1(indicated by the duplicate flag)
1631 * If we do not have an entry corresponding to the frequency given, we
1632 * allocate a new entry and calculate the values
1634 * Upon finding the right reg dump, save it in curr_regs. It can be
1635 * directly used for thermal de-rating and voltage ramping changes.
1637 static struct emif_regs *get_regs(struct emif_data *emif, u32 freq)
1639 int i;
1640 struct emif_regs **regs_cache;
1641 struct emif_regs *regs = NULL;
1642 struct device *dev;
1644 dev = emif->dev;
1645 if (emif->curr_regs && emif->curr_regs->freq == freq) {
1646 dev_dbg(dev, "%s: using curr_regs - %u Hz", __func__, freq);
1647 return emif->curr_regs;
1650 if (emif->duplicate)
1651 regs_cache = emif1->regs_cache;
1652 else
1653 regs_cache = emif->regs_cache;
1655 for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) {
1656 if (regs_cache[i]->freq == freq) {
1657 regs = regs_cache[i];
1658 dev_dbg(dev,
1659 "%s: reg dump found in reg cache for %u Hz\n",
1660 __func__, freq);
1661 break;
1666 * If we don't have an entry for this frequency in the cache create one
1667 * and calculate the values
1669 if (!regs) {
1670 regs = devm_kzalloc(emif->dev, sizeof(*regs), GFP_ATOMIC);
1671 if (!regs)
1672 return NULL;
1674 if (get_emif_reg_values(emif, freq, regs)) {
1675 devm_kfree(emif->dev, regs);
1676 return NULL;
1680 * Now look for an un-used entry in the cache and save the
1681 * newly created struct. If there are no free entries
1682 * over-write the last entry
1684 for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++)
1687 if (i >= EMIF_MAX_NUM_FREQUENCIES) {
1688 dev_warn(dev, "%s: regs_cache full - reusing a slot!!\n",
1689 __func__);
1690 i = EMIF_MAX_NUM_FREQUENCIES - 1;
1691 devm_kfree(emif->dev, regs_cache[i]);
1693 regs_cache[i] = regs;
1696 return regs;
1699 static void do_volt_notify_handling(struct emif_data *emif, u32 volt_state)
1701 dev_dbg(emif->dev, "%s: voltage notification : %d", __func__,
1702 volt_state);
1704 if (!emif->curr_regs) {
1705 dev_err(emif->dev,
1706 "%s: volt-notify before registers are ready: %d\n",
1707 __func__, volt_state);
1708 return;
1711 setup_volt_sensitive_regs(emif, emif->curr_regs, volt_state);
1715 * TODO: voltage notify handling should be hooked up to
1716 * regulator framework as soon as the necessary support
1717 * is available in mainline kernel. This function is un-used
1718 * right now.
1720 static void __attribute__((unused)) volt_notify_handling(u32 volt_state)
1722 struct emif_data *emif;
1724 spin_lock_irqsave(&emif_lock, irq_state);
1726 list_for_each_entry(emif, &device_list, node)
1727 do_volt_notify_handling(emif, volt_state);
1728 do_freq_update();
1730 spin_unlock_irqrestore(&emif_lock, irq_state);
1733 static void do_freq_pre_notify_handling(struct emif_data *emif, u32 new_freq)
1735 struct emif_regs *regs;
1737 regs = get_regs(emif, new_freq);
1738 if (!regs)
1739 return;
1741 emif->curr_regs = regs;
1744 * Update the shadow registers:
1745 * Temperature and voltage-ramp sensitive settings are also configured
1746 * in terms of DDR cycles. So, we need to update them too when there
1747 * is a freq change
1749 dev_dbg(emif->dev, "%s: setting up shadow registers for %uHz",
1750 __func__, new_freq);
1751 setup_registers(emif, regs);
1752 setup_temperature_sensitive_regs(emif, regs);
1753 setup_volt_sensitive_regs(emif, regs, DDR_VOLTAGE_STABLE);
1756 * Part of workaround for errata i728. See do_freq_update()
1757 * for more details
1759 if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
1760 set_lpmode(emif, EMIF_LP_MODE_DISABLE);
1764 * TODO: frequency notify handling should be hooked up to
1765 * clock framework as soon as the necessary support is
1766 * available in mainline kernel. This function is un-used
1767 * right now.
1769 static void __attribute__((unused)) freq_pre_notify_handling(u32 new_freq)
1771 struct emif_data *emif;
1774 * NOTE: we are taking the spin-lock here and releases it
1775 * only in post-notifier. This doesn't look good and
1776 * Sparse complains about it, but this seems to be
1777 * un-avoidable. We need to lock a sequence of events
1778 * that is split between EMIF and clock framework.
1780 * 1. EMIF driver updates EMIF timings in shadow registers in the
1781 * frequency pre-notify callback from clock framework
1782 * 2. clock framework sets up the registers for the new frequency
1783 * 3. clock framework initiates a hw-sequence that updates
1784 * the frequency EMIF timings synchronously.
1786 * All these 3 steps should be performed as an atomic operation
1787 * vis-a-vis similar sequence in the EMIF interrupt handler
1788 * for temperature events. Otherwise, there could be race
1789 * conditions that could result in incorrect EMIF timings for
1790 * a given frequency
1792 spin_lock_irqsave(&emif_lock, irq_state);
1794 list_for_each_entry(emif, &device_list, node)
1795 do_freq_pre_notify_handling(emif, new_freq);
1798 static void do_freq_post_notify_handling(struct emif_data *emif)
1801 * Part of workaround for errata i728. See do_freq_update()
1802 * for more details
1804 if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
1805 set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH);
1809 * TODO: frequency notify handling should be hooked up to
1810 * clock framework as soon as the necessary support is
1811 * available in mainline kernel. This function is un-used
1812 * right now.
1814 static void __attribute__((unused)) freq_post_notify_handling(void)
1816 struct emif_data *emif;
1818 list_for_each_entry(emif, &device_list, node)
1819 do_freq_post_notify_handling(emif);
1822 * Lock is done in pre-notify handler. See freq_pre_notify_handling()
1823 * for more details
1825 spin_unlock_irqrestore(&emif_lock, irq_state);
1828 #if defined(CONFIG_OF)
1829 static const struct of_device_id emif_of_match[] = {
1830 { .compatible = "ti,emif-4d" },
1831 { .compatible = "ti,emif-4d5" },
1834 MODULE_DEVICE_TABLE(of, emif_of_match);
1835 #endif
1837 static struct platform_driver emif_driver = {
1838 .remove = __exit_p(emif_remove),
1839 .shutdown = emif_shutdown,
1840 .driver = {
1841 .name = "emif",
1842 .of_match_table = of_match_ptr(emif_of_match),
1846 static int __init_or_module emif_register(void)
1848 return platform_driver_probe(&emif_driver, emif_probe);
1851 static void __exit emif_unregister(void)
1853 platform_driver_unregister(&emif_driver);
1856 module_init(emif_register);
1857 module_exit(emif_unregister);
1858 MODULE_DESCRIPTION("TI EMIF SDRAM Controller Driver");
1859 MODULE_LICENSE("GPL");
1860 MODULE_ALIAS("platform:emif");
1861 MODULE_AUTHOR("Texas Instruments Inc");