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