blob: a8dcf3515573b87355f20c52b4e0d6813de3a3e9 [file] [log] [blame]
Aneesh V7ec94452012-04-27 17:54:05 +05301/*
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/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/seq_file.h>
22#include <linux/module.h>
23#include <linux/list.h>
Aneesh Va93de282012-04-27 17:54:06 +053024#include <linux/spinlock.h>
Aneesh V7ec94452012-04-27 17:54:05 +053025#include <memory/jedec_ddr.h>
26#include "emif.h"
27
28/**
29 * struct emif_data - Per device static data for driver's use
30 * @duplicate: Whether the DDR devices attached to this EMIF
31 * instance are exactly same as that on EMIF1. In
32 * this case we can save some memory and processing
33 * @temperature_level: Maximum temperature of LPDDR2 devices attached
34 * to this EMIF - read from MR4 register. If there
35 * are two devices attached to this EMIF, this
36 * value is the maximum of the two temperature
37 * levels.
38 * @node: node in the device list
39 * @base: base address of memory-mapped IO registers.
40 * @dev: device pointer.
Aneesh Va93de282012-04-27 17:54:06 +053041 * @addressing table with addressing information from the spec
42 * @regs_cache: An array of 'struct emif_regs' that stores
43 * calculated register values for different
44 * frequencies, to avoid re-calculating them on
45 * each DVFS transition.
46 * @curr_regs: The set of register values used in the last
47 * frequency change (i.e. corresponding to the
48 * frequency in effect at the moment)
Aneesh V7ec94452012-04-27 17:54:05 +053049 * @plat_data: Pointer to saved platform data.
50 */
51struct emif_data {
52 u8 duplicate;
53 u8 temperature_level;
Aneesh Va93de282012-04-27 17:54:06 +053054 u8 lpmode;
Aneesh V7ec94452012-04-27 17:54:05 +053055 struct list_head node;
Aneesh Va93de282012-04-27 17:54:06 +053056 unsigned long irq_state;
Aneesh V7ec94452012-04-27 17:54:05 +053057 void __iomem *base;
58 struct device *dev;
Aneesh Va93de282012-04-27 17:54:06 +053059 const struct lpddr2_addressing *addressing;
60 struct emif_regs *regs_cache[EMIF_MAX_NUM_FREQUENCIES];
61 struct emif_regs *curr_regs;
Aneesh V7ec94452012-04-27 17:54:05 +053062 struct emif_platform_data *plat_data;
63};
64
65static struct emif_data *emif1;
Aneesh Va93de282012-04-27 17:54:06 +053066static spinlock_t emif_lock;
67static unsigned long irq_state;
68static u32 t_ck; /* DDR clock period in ps */
Aneesh V7ec94452012-04-27 17:54:05 +053069static LIST_HEAD(device_list);
70
Aneesh Va93de282012-04-27 17:54:06 +053071/*
72 * Calculate the period of DDR clock from frequency value
73 */
74static void set_ddr_clk_period(u32 freq)
75{
76 /* Divide 10^12 by frequency to get period in ps */
77 t_ck = (u32)DIV_ROUND_UP_ULL(1000000000000ull, freq);
78}
79
80/*
81 * Get the CL from SDRAM_CONFIG register
82 */
83static u32 get_cl(struct emif_data *emif)
84{
85 u32 cl;
86 void __iomem *base = emif->base;
87
88 cl = (readl(base + EMIF_SDRAM_CONFIG) & CL_MASK) >> CL_SHIFT;
89
90 return cl;
91}
92
93static void set_lpmode(struct emif_data *emif, u8 lpmode)
94{
95 u32 temp;
96 void __iomem *base = emif->base;
97
98 temp = readl(base + EMIF_POWER_MANAGEMENT_CONTROL);
99 temp &= ~LP_MODE_MASK;
100 temp |= (lpmode << LP_MODE_SHIFT);
101 writel(temp, base + EMIF_POWER_MANAGEMENT_CONTROL);
102}
103
104static void do_freq_update(void)
105{
106 struct emif_data *emif;
107
108 /*
109 * Workaround for errata i728: Disable LPMODE during FREQ_UPDATE
110 *
111 * i728 DESCRIPTION:
112 * The EMIF automatically puts the SDRAM into self-refresh mode
113 * after the EMIF has not performed accesses during
114 * EMIF_PWR_MGMT_CTRL[7:4] REG_SR_TIM number of DDR clock cycles
115 * and the EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field is set
116 * to 0x2. If during a small window the following three events
117 * occur:
118 * - The SR_TIMING counter expires
119 * - And frequency change is requested
120 * - And OCP access is requested
121 * Then it causes instable clock on the DDR interface.
122 *
123 * WORKAROUND
124 * To avoid the occurrence of the three events, the workaround
125 * is to disable the self-refresh when requesting a frequency
126 * change. Before requesting a frequency change the software must
127 * program EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x0. When the
128 * frequency change has been done, the software can reprogram
129 * EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x2
130 */
131 list_for_each_entry(emif, &device_list, node) {
132 if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
133 set_lpmode(emif, EMIF_LP_MODE_DISABLE);
134 }
135
136 /*
137 * TODO: Do FREQ_UPDATE here when an API
138 * is available for this as part of the new
139 * clock framework
140 */
141
142 list_for_each_entry(emif, &device_list, node) {
143 if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
144 set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH);
145 }
146}
147
148/* Find addressing table entry based on the device's type and density */
149static const struct lpddr2_addressing *get_addressing_table(
150 const struct ddr_device_info *device_info)
151{
152 u32 index, type, density;
153
154 type = device_info->type;
155 density = device_info->density;
156
157 switch (type) {
158 case DDR_TYPE_LPDDR2_S4:
159 index = density - 1;
160 break;
161 case DDR_TYPE_LPDDR2_S2:
162 switch (density) {
163 case DDR_DENSITY_1Gb:
164 case DDR_DENSITY_2Gb:
165 index = density + 3;
166 break;
167 default:
168 index = density - 1;
169 }
170 break;
171 default:
172 return NULL;
173 }
174
175 return &lpddr2_jedec_addressing_table[index];
176}
177
178/*
179 * Find the the right timing table from the array of timing
180 * tables of the device using DDR clock frequency
181 */
182static const struct lpddr2_timings *get_timings_table(struct emif_data *emif,
183 u32 freq)
184{
185 u32 i, min, max, freq_nearest;
186 const struct lpddr2_timings *timings = NULL;
187 const struct lpddr2_timings *timings_arr = emif->plat_data->timings;
188 struct device *dev = emif->dev;
189
190 /* Start with a very high frequency - 1GHz */
191 freq_nearest = 1000000000;
192
193 /*
194 * Find the timings table such that:
195 * 1. the frequency range covers the required frequency(safe) AND
196 * 2. the max_freq is closest to the required frequency(optimal)
197 */
198 for (i = 0; i < emif->plat_data->timings_arr_size; i++) {
199 max = timings_arr[i].max_freq;
200 min = timings_arr[i].min_freq;
201 if ((freq >= min) && (freq <= max) && (max < freq_nearest)) {
202 freq_nearest = max;
203 timings = &timings_arr[i];
204 }
205 }
206
207 if (!timings)
208 dev_err(dev, "%s: couldn't find timings for - %dHz\n",
209 __func__, freq);
210
211 dev_dbg(dev, "%s: timings table: freq %d, speed bin freq %d\n",
212 __func__, freq, freq_nearest);
213
214 return timings;
215}
216
217static u32 get_sdram_ref_ctrl_shdw(u32 freq,
218 const struct lpddr2_addressing *addressing)
219{
220 u32 ref_ctrl_shdw = 0, val = 0, freq_khz, t_refi;
221
222 /* Scale down frequency and t_refi to avoid overflow */
223 freq_khz = freq / 1000;
224 t_refi = addressing->tREFI_ns / 100;
225
226 /*
227 * refresh rate to be set is 'tREFI(in us) * freq in MHz
228 * division by 10000 to account for change in units
229 */
230 val = t_refi * freq_khz / 10000;
231 ref_ctrl_shdw |= val << REFRESH_RATE_SHIFT;
232
233 return ref_ctrl_shdw;
234}
235
236static u32 get_sdram_tim_1_shdw(const struct lpddr2_timings *timings,
237 const struct lpddr2_min_tck *min_tck,
238 const struct lpddr2_addressing *addressing)
239{
240 u32 tim1 = 0, val = 0;
241
242 val = max(min_tck->tWTR, DIV_ROUND_UP(timings->tWTR, t_ck)) - 1;
243 tim1 |= val << T_WTR_SHIFT;
244
245 if (addressing->num_banks == B8)
246 val = DIV_ROUND_UP(timings->tFAW, t_ck*4);
247 else
248 val = max(min_tck->tRRD, DIV_ROUND_UP(timings->tRRD, t_ck));
249 tim1 |= (val - 1) << T_RRD_SHIFT;
250
251 val = DIV_ROUND_UP(timings->tRAS_min + timings->tRPab, t_ck) - 1;
252 tim1 |= val << T_RC_SHIFT;
253
254 val = max(min_tck->tRASmin, DIV_ROUND_UP(timings->tRAS_min, t_ck));
255 tim1 |= (val - 1) << T_RAS_SHIFT;
256
257 val = max(min_tck->tWR, DIV_ROUND_UP(timings->tWR, t_ck)) - 1;
258 tim1 |= val << T_WR_SHIFT;
259
260 val = max(min_tck->tRCD, DIV_ROUND_UP(timings->tRCD, t_ck)) - 1;
261 tim1 |= val << T_RCD_SHIFT;
262
263 val = max(min_tck->tRPab, DIV_ROUND_UP(timings->tRPab, t_ck)) - 1;
264 tim1 |= val << T_RP_SHIFT;
265
266 return tim1;
267}
268
269static u32 get_sdram_tim_1_shdw_derated(const struct lpddr2_timings *timings,
270 const struct lpddr2_min_tck *min_tck,
271 const struct lpddr2_addressing *addressing)
272{
273 u32 tim1 = 0, val = 0;
274
275 val = max(min_tck->tWTR, DIV_ROUND_UP(timings->tWTR, t_ck)) - 1;
276 tim1 = val << T_WTR_SHIFT;
277
278 /*
279 * tFAW is approximately 4 times tRRD. So add 1875*4 = 7500ps
280 * to tFAW for de-rating
281 */
282 if (addressing->num_banks == B8) {
283 val = DIV_ROUND_UP(timings->tFAW + 7500, 4 * t_ck) - 1;
284 } else {
285 val = DIV_ROUND_UP(timings->tRRD + 1875, t_ck);
286 val = max(min_tck->tRRD, val) - 1;
287 }
288 tim1 |= val << T_RRD_SHIFT;
289
290 val = DIV_ROUND_UP(timings->tRAS_min + timings->tRPab + 1875, t_ck);
291 tim1 |= (val - 1) << T_RC_SHIFT;
292
293 val = DIV_ROUND_UP(timings->tRAS_min + 1875, t_ck);
294 val = max(min_tck->tRASmin, val) - 1;
295 tim1 |= val << T_RAS_SHIFT;
296
297 val = max(min_tck->tWR, DIV_ROUND_UP(timings->tWR, t_ck)) - 1;
298 tim1 |= val << T_WR_SHIFT;
299
300 val = max(min_tck->tRCD, DIV_ROUND_UP(timings->tRCD + 1875, t_ck));
301 tim1 |= (val - 1) << T_RCD_SHIFT;
302
303 val = max(min_tck->tRPab, DIV_ROUND_UP(timings->tRPab + 1875, t_ck));
304 tim1 |= (val - 1) << T_RP_SHIFT;
305
306 return tim1;
307}
308
309static u32 get_sdram_tim_2_shdw(const struct lpddr2_timings *timings,
310 const struct lpddr2_min_tck *min_tck,
311 const struct lpddr2_addressing *addressing,
312 u32 type)
313{
314 u32 tim2 = 0, val = 0;
315
316 val = min_tck->tCKE - 1;
317 tim2 |= val << T_CKE_SHIFT;
318
319 val = max(min_tck->tRTP, DIV_ROUND_UP(timings->tRTP, t_ck)) - 1;
320 tim2 |= val << T_RTP_SHIFT;
321
322 /* tXSNR = tRFCab_ps + 10 ns(tRFCab_ps for LPDDR2). */
323 val = DIV_ROUND_UP(addressing->tRFCab_ps + 10000, t_ck) - 1;
324 tim2 |= val << T_XSNR_SHIFT;
325
326 /* XSRD same as XSNR for LPDDR2 */
327 tim2 |= val << T_XSRD_SHIFT;
328
329 val = max(min_tck->tXP, DIV_ROUND_UP(timings->tXP, t_ck)) - 1;
330 tim2 |= val << T_XP_SHIFT;
331
332 return tim2;
333}
334
335static u32 get_sdram_tim_3_shdw(const struct lpddr2_timings *timings,
336 const struct lpddr2_min_tck *min_tck,
337 const struct lpddr2_addressing *addressing,
338 u32 type, u32 ip_rev, u32 derated)
339{
340 u32 tim3 = 0, val = 0, t_dqsck;
341
342 val = timings->tRAS_max_ns / addressing->tREFI_ns - 1;
343 val = val > 0xF ? 0xF : val;
344 tim3 |= val << T_RAS_MAX_SHIFT;
345
346 val = DIV_ROUND_UP(addressing->tRFCab_ps, t_ck) - 1;
347 tim3 |= val << T_RFC_SHIFT;
348
349 t_dqsck = (derated == EMIF_DERATED_TIMINGS) ?
350 timings->tDQSCK_max_derated : timings->tDQSCK_max;
351 if (ip_rev == EMIF_4D5)
352 val = DIV_ROUND_UP(t_dqsck + 1000, t_ck) - 1;
353 else
354 val = DIV_ROUND_UP(t_dqsck, t_ck) - 1;
355
356 tim3 |= val << T_TDQSCKMAX_SHIFT;
357
358 val = DIV_ROUND_UP(timings->tZQCS, t_ck) - 1;
359 tim3 |= val << ZQ_ZQCS_SHIFT;
360
361 val = DIV_ROUND_UP(timings->tCKESR, t_ck);
362 val = max(min_tck->tCKESR, val) - 1;
363 tim3 |= val << T_CKESR_SHIFT;
364
365 if (ip_rev == EMIF_4D5) {
366 tim3 |= (EMIF_T_CSTA - 1) << T_CSTA_SHIFT;
367
368 val = DIV_ROUND_UP(EMIF_T_PDLL_UL, 128) - 1;
369 tim3 |= val << T_PDLL_UL_SHIFT;
370 }
371
372 return tim3;
373}
374
375static u32 get_read_idle_ctrl_shdw(u8 volt_ramp)
376{
377 u32 idle = 0, val = 0;
378
379 /*
380 * Maximum value in normal conditions and increased frequency
381 * when voltage is ramping
382 */
383 if (volt_ramp)
384 val = READ_IDLE_INTERVAL_DVFS / t_ck / 64 - 1;
385 else
386 val = 0x1FF;
387
388 /*
389 * READ_IDLE_CTRL register in EMIF4D has same offset and fields
390 * as DLL_CALIB_CTRL in EMIF4D5, so use the same shifts
391 */
392 idle |= val << DLL_CALIB_INTERVAL_SHIFT;
393 idle |= EMIF_READ_IDLE_LEN_VAL << ACK_WAIT_SHIFT;
394
395 return idle;
396}
397
398static u32 get_dll_calib_ctrl_shdw(u8 volt_ramp)
399{
400 u32 calib = 0, val = 0;
401
402 if (volt_ramp == DDR_VOLTAGE_RAMPING)
403 val = DLL_CALIB_INTERVAL_DVFS / t_ck / 16 - 1;
404 else
405 val = 0; /* Disabled when voltage is stable */
406
407 calib |= val << DLL_CALIB_INTERVAL_SHIFT;
408 calib |= DLL_CALIB_ACK_WAIT_VAL << ACK_WAIT_SHIFT;
409
410 return calib;
411}
412
413static u32 get_ddr_phy_ctrl_1_attilaphy_4d(const struct lpddr2_timings *timings,
414 u32 freq, u8 RL)
415{
416 u32 phy = EMIF_DDR_PHY_CTRL_1_BASE_VAL_ATTILAPHY, val = 0;
417
418 val = RL + DIV_ROUND_UP(timings->tDQSCK_max, t_ck) - 1;
419 phy |= val << READ_LATENCY_SHIFT_4D;
420
421 if (freq <= 100000000)
422 val = EMIF_DLL_SLAVE_DLY_CTRL_100_MHZ_AND_LESS_ATTILAPHY;
423 else if (freq <= 200000000)
424 val = EMIF_DLL_SLAVE_DLY_CTRL_200_MHZ_ATTILAPHY;
425 else
426 val = EMIF_DLL_SLAVE_DLY_CTRL_400_MHZ_ATTILAPHY;
427
428 phy |= val << DLL_SLAVE_DLY_CTRL_SHIFT_4D;
429
430 return phy;
431}
432
433static u32 get_phy_ctrl_1_intelliphy_4d5(u32 freq, u8 cl)
434{
435 u32 phy = EMIF_DDR_PHY_CTRL_1_BASE_VAL_INTELLIPHY, half_delay;
436
437 /*
438 * DLL operates at 266 MHz. If DDR frequency is near 266 MHz,
439 * half-delay is not needed else set half-delay
440 */
441 if (freq >= 265000000 && freq < 267000000)
442 half_delay = 0;
443 else
444 half_delay = 1;
445
446 phy |= half_delay << DLL_HALF_DELAY_SHIFT_4D5;
447 phy |= ((cl + DIV_ROUND_UP(EMIF_PHY_TOTAL_READ_LATENCY_INTELLIPHY_PS,
448 t_ck) - 1) << READ_LATENCY_SHIFT_4D5);
449
450 return phy;
451}
452
453static u32 get_ext_phy_ctrl_2_intelliphy_4d5(void)
454{
455 u32 fifo_we_slave_ratio;
456
457 fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
458 EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
459
460 return fifo_we_slave_ratio | fifo_we_slave_ratio << 11 |
461 fifo_we_slave_ratio << 22;
462}
463
464static u32 get_ext_phy_ctrl_3_intelliphy_4d5(void)
465{
466 u32 fifo_we_slave_ratio;
467
468 fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
469 EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
470
471 return fifo_we_slave_ratio >> 10 | fifo_we_slave_ratio << 1 |
472 fifo_we_slave_ratio << 12 | fifo_we_slave_ratio << 23;
473}
474
475static u32 get_ext_phy_ctrl_4_intelliphy_4d5(void)
476{
477 u32 fifo_we_slave_ratio;
478
479 fifo_we_slave_ratio = DIV_ROUND_CLOSEST(
480 EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck);
481
482 return fifo_we_slave_ratio >> 9 | fifo_we_slave_ratio << 2 |
483 fifo_we_slave_ratio << 13;
484}
485
486static u32 get_pwr_mgmt_ctrl(u32 freq, struct emif_data *emif, u32 ip_rev)
487{
488 u32 pwr_mgmt_ctrl = 0, timeout;
489 u32 lpmode = EMIF_LP_MODE_SELF_REFRESH;
490 u32 timeout_perf = EMIF_LP_MODE_TIMEOUT_PERFORMANCE;
491 u32 timeout_pwr = EMIF_LP_MODE_TIMEOUT_POWER;
492 u32 freq_threshold = EMIF_LP_MODE_FREQ_THRESHOLD;
493
494 struct emif_custom_configs *cust_cfgs = emif->plat_data->custom_configs;
495
496 if (cust_cfgs && (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE)) {
497 lpmode = cust_cfgs->lpmode;
498 timeout_perf = cust_cfgs->lpmode_timeout_performance;
499 timeout_pwr = cust_cfgs->lpmode_timeout_power;
500 freq_threshold = cust_cfgs->lpmode_freq_threshold;
501 }
502
503 /* Timeout based on DDR frequency */
504 timeout = freq >= freq_threshold ? timeout_perf : timeout_pwr;
505
506 /* The value to be set in register is "log2(timeout) - 3" */
507 if (timeout < 16) {
508 timeout = 0;
509 } else {
510 timeout = __fls(timeout) - 3;
511 if (timeout & (timeout - 1))
512 timeout++;
513 }
514
515 switch (lpmode) {
516 case EMIF_LP_MODE_CLOCK_STOP:
517 pwr_mgmt_ctrl = (timeout << CS_TIM_SHIFT) |
518 SR_TIM_MASK | PD_TIM_MASK;
519 break;
520 case EMIF_LP_MODE_SELF_REFRESH:
521 /* Workaround for errata i735 */
522 if (timeout < 6)
523 timeout = 6;
524
525 pwr_mgmt_ctrl = (timeout << SR_TIM_SHIFT) |
526 CS_TIM_MASK | PD_TIM_MASK;
527 break;
528 case EMIF_LP_MODE_PWR_DN:
529 pwr_mgmt_ctrl = (timeout << PD_TIM_SHIFT) |
530 CS_TIM_MASK | SR_TIM_MASK;
531 break;
532 case EMIF_LP_MODE_DISABLE:
533 default:
534 pwr_mgmt_ctrl = CS_TIM_MASK |
535 PD_TIM_MASK | SR_TIM_MASK;
536 }
537
538 /* No CS_TIM in EMIF_4D5 */
539 if (ip_rev == EMIF_4D5)
540 pwr_mgmt_ctrl &= ~CS_TIM_MASK;
541
542 pwr_mgmt_ctrl |= lpmode << LP_MODE_SHIFT;
543
544 return pwr_mgmt_ctrl;
545}
546
547/*
Aneesh V68b4aee2012-04-27 17:54:07 +0530548 * Get the temperature level of the EMIF instance:
549 * Reads the MR4 register of attached SDRAM parts to find out the temperature
550 * level. If there are two parts attached(one on each CS), then the temperature
551 * level for the EMIF instance is the higher of the two temperatures.
552 */
553static void get_temperature_level(struct emif_data *emif)
554{
555 u32 temp, temperature_level;
556 void __iomem *base;
557
558 base = emif->base;
559
560 /* Read mode register 4 */
561 writel(DDR_MR4, base + EMIF_LPDDR2_MODE_REG_CONFIG);
562 temperature_level = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
563 temperature_level = (temperature_level & MR4_SDRAM_REF_RATE_MASK) >>
564 MR4_SDRAM_REF_RATE_SHIFT;
565
566 if (emif->plat_data->device_info->cs1_used) {
567 writel(DDR_MR4 | CS_MASK, base + EMIF_LPDDR2_MODE_REG_CONFIG);
568 temp = readl(base + EMIF_LPDDR2_MODE_REG_DATA);
569 temp = (temp & MR4_SDRAM_REF_RATE_MASK)
570 >> MR4_SDRAM_REF_RATE_SHIFT;
571 temperature_level = max(temp, temperature_level);
572 }
573
574 /* treat everything less than nominal(3) in MR4 as nominal */
575 if (unlikely(temperature_level < SDRAM_TEMP_NOMINAL))
576 temperature_level = SDRAM_TEMP_NOMINAL;
577
578 /* if we get reserved value in MR4 persist with the existing value */
579 if (likely(temperature_level != SDRAM_TEMP_RESERVED_4))
580 emif->temperature_level = temperature_level;
581}
582
583/*
Aneesh Va93de282012-04-27 17:54:06 +0530584 * Program EMIF shadow registers that are not dependent on temperature
585 * or voltage
586 */
587static void setup_registers(struct emif_data *emif, struct emif_regs *regs)
588{
589 void __iomem *base = emif->base;
590
591 writel(regs->sdram_tim2_shdw, base + EMIF_SDRAM_TIMING_2_SHDW);
592 writel(regs->phy_ctrl_1_shdw, base + EMIF_DDR_PHY_CTRL_1_SHDW);
593
594 /* Settings specific for EMIF4D5 */
595 if (emif->plat_data->ip_rev != EMIF_4D5)
596 return;
597 writel(regs->ext_phy_ctrl_2_shdw, base + EMIF_EXT_PHY_CTRL_2_SHDW);
598 writel(regs->ext_phy_ctrl_3_shdw, base + EMIF_EXT_PHY_CTRL_3_SHDW);
599 writel(regs->ext_phy_ctrl_4_shdw, base + EMIF_EXT_PHY_CTRL_4_SHDW);
600}
601
602/*
603 * When voltage ramps dll calibration and forced read idle should
604 * happen more often
605 */
606static void setup_volt_sensitive_regs(struct emif_data *emif,
607 struct emif_regs *regs, u32 volt_state)
608{
609 u32 calib_ctrl;
610 void __iomem *base = emif->base;
611
612 /*
613 * EMIF_READ_IDLE_CTRL in EMIF4D refers to the same register as
614 * EMIF_DLL_CALIB_CTRL in EMIF4D5 and dll_calib_ctrl_shadow_*
615 * is an alias of the respective read_idle_ctrl_shdw_* (members of
616 * a union). So, the below code takes care of both cases
617 */
618 if (volt_state == DDR_VOLTAGE_RAMPING)
619 calib_ctrl = regs->dll_calib_ctrl_shdw_volt_ramp;
620 else
621 calib_ctrl = regs->dll_calib_ctrl_shdw_normal;
622
623 writel(calib_ctrl, base + EMIF_DLL_CALIB_CTRL_SHDW);
624}
625
626/*
627 * setup_temperature_sensitive_regs() - set the timings for temperature
628 * sensitive registers. This happens once at initialisation time based
629 * on the temperature at boot time and subsequently based on the temperature
630 * alert interrupt. Temperature alert can happen when the temperature
631 * increases or drops. So this function can have the effect of either
632 * derating the timings or going back to nominal values.
633 */
634static void setup_temperature_sensitive_regs(struct emif_data *emif,
635 struct emif_regs *regs)
636{
637 u32 tim1, tim3, ref_ctrl, type;
638 void __iomem *base = emif->base;
639 u32 temperature;
640
641 type = emif->plat_data->device_info->type;
642
643 tim1 = regs->sdram_tim1_shdw;
644 tim3 = regs->sdram_tim3_shdw;
645 ref_ctrl = regs->ref_ctrl_shdw;
646
647 /* No de-rating for non-lpddr2 devices */
648 if (type != DDR_TYPE_LPDDR2_S2 && type != DDR_TYPE_LPDDR2_S4)
649 goto out;
650
651 temperature = emif->temperature_level;
652 if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH) {
653 ref_ctrl = regs->ref_ctrl_shdw_derated;
654 } else if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH_AND_TIMINGS) {
655 tim1 = regs->sdram_tim1_shdw_derated;
656 tim3 = regs->sdram_tim3_shdw_derated;
657 ref_ctrl = regs->ref_ctrl_shdw_derated;
658 }
659
660out:
661 writel(tim1, base + EMIF_SDRAM_TIMING_1_SHDW);
662 writel(tim3, base + EMIF_SDRAM_TIMING_3_SHDW);
663 writel(ref_ctrl, base + EMIF_SDRAM_REFRESH_CTRL_SHDW);
664}
665
Aneesh V68b4aee2012-04-27 17:54:07 +0530666static irqreturn_t handle_temp_alert(void __iomem *base, struct emif_data *emif)
667{
668 u32 old_temp_level;
669 irqreturn_t ret = IRQ_HANDLED;
670
671 spin_lock_irqsave(&emif_lock, irq_state);
672 old_temp_level = emif->temperature_level;
673 get_temperature_level(emif);
674
675 if (unlikely(emif->temperature_level == old_temp_level)) {
676 goto out;
677 } else if (!emif->curr_regs) {
678 dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
679 goto out;
680 }
681
682 if (emif->temperature_level < old_temp_level ||
683 emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
684 /*
685 * Temperature coming down - defer handling to thread OR
686 * Temperature far too high - do kernel_power_off() from
687 * thread context
688 */
689 ret = IRQ_WAKE_THREAD;
690 } else {
691 /* Temperature is going up - handle immediately */
692 setup_temperature_sensitive_regs(emif, emif->curr_regs);
693 do_freq_update();
694 }
695
696out:
697 spin_unlock_irqrestore(&emif_lock, irq_state);
698 return ret;
699}
700
701static irqreturn_t emif_interrupt_handler(int irq, void *dev_id)
702{
703 u32 interrupts;
704 struct emif_data *emif = dev_id;
705 void __iomem *base = emif->base;
706 struct device *dev = emif->dev;
707 irqreturn_t ret = IRQ_HANDLED;
708
709 /* Save the status and clear it */
710 interrupts = readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
711 writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
712
713 /*
714 * Handle temperature alert
715 * Temperature alert should be same for all ports
716 * So, it's enough to process it only for one of the ports
717 */
718 if (interrupts & TA_SYS_MASK)
719 ret = handle_temp_alert(base, emif);
720
721 if (interrupts & ERR_SYS_MASK)
722 dev_err(dev, "Access error from SYS port - %x\n", interrupts);
723
724 if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
725 /* Save the status and clear it */
726 interrupts = readl(base + EMIF_LL_OCP_INTERRUPT_STATUS);
727 writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_STATUS);
728
729 if (interrupts & ERR_LL_MASK)
730 dev_err(dev, "Access error from LL port - %x\n",
731 interrupts);
732 }
733
734 return ret;
735}
736
737static irqreturn_t emif_threaded_isr(int irq, void *dev_id)
738{
739 struct emif_data *emif = dev_id;
740
741 if (emif->temperature_level == SDRAM_TEMP_VERY_HIGH_SHUTDOWN) {
742 dev_emerg(emif->dev, "SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
743 kernel_power_off();
744 return IRQ_HANDLED;
745 }
746
747 spin_lock_irqsave(&emif_lock, irq_state);
748
749 if (emif->curr_regs) {
750 setup_temperature_sensitive_regs(emif, emif->curr_regs);
751 do_freq_update();
752 } else {
753 dev_err(emif->dev, "temperature alert before registers are calculated, not de-rating timings\n");
754 }
755
756 spin_unlock_irqrestore(&emif_lock, irq_state);
757
758 return IRQ_HANDLED;
759}
760
761static void clear_all_interrupts(struct emif_data *emif)
762{
763 void __iomem *base = emif->base;
764
765 writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS),
766 base + EMIF_SYSTEM_OCP_INTERRUPT_STATUS);
767 if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
768 writel(readl(base + EMIF_LL_OCP_INTERRUPT_STATUS),
769 base + EMIF_LL_OCP_INTERRUPT_STATUS);
770}
771
772static void disable_and_clear_all_interrupts(struct emif_data *emif)
773{
774 void __iomem *base = emif->base;
775
776 /* Disable all interrupts */
777 writel(readl(base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET),
778 base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_CLEAR);
779 if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE)
780 writel(readl(base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET),
781 base + EMIF_LL_OCP_INTERRUPT_ENABLE_CLEAR);
782
783 /* Clear all interrupts */
784 clear_all_interrupts(emif);
785}
786
787static int __init_or_module setup_interrupts(struct emif_data *emif, u32 irq)
788{
789 u32 interrupts, type;
790 void __iomem *base = emif->base;
791
792 type = emif->plat_data->device_info->type;
793
794 clear_all_interrupts(emif);
795
796 /* Enable interrupts for SYS interface */
797 interrupts = EN_ERR_SYS_MASK;
798 if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4)
799 interrupts |= EN_TA_SYS_MASK;
800 writel(interrupts, base + EMIF_SYSTEM_OCP_INTERRUPT_ENABLE_SET);
801
802 /* Enable interrupts for LL interface */
803 if (emif->plat_data->hw_caps & EMIF_HW_CAPS_LL_INTERFACE) {
804 /* TA need not be enabled for LL */
805 interrupts = EN_ERR_LL_MASK;
806 writel(interrupts, base + EMIF_LL_OCP_INTERRUPT_ENABLE_SET);
807 }
808
809 /* setup IRQ handlers */
810 return devm_request_threaded_irq(emif->dev, irq,
811 emif_interrupt_handler,
812 emif_threaded_isr,
813 0, dev_name(emif->dev),
814 emif);
815
816}
817
Aneesh V7ec94452012-04-27 17:54:05 +0530818static void get_default_timings(struct emif_data *emif)
819{
820 struct emif_platform_data *pd = emif->plat_data;
821
822 pd->timings = lpddr2_jedec_timings;
823 pd->timings_arr_size = ARRAY_SIZE(lpddr2_jedec_timings);
824
825 dev_warn(emif->dev, "%s: using default timings\n", __func__);
826}
827
828static int is_dev_data_valid(u32 type, u32 density, u32 io_width, u32 phy_type,
829 u32 ip_rev, struct device *dev)
830{
831 int valid;
832
833 valid = (type == DDR_TYPE_LPDDR2_S4 ||
834 type == DDR_TYPE_LPDDR2_S2)
835 && (density >= DDR_DENSITY_64Mb
836 && density <= DDR_DENSITY_8Gb)
837 && (io_width >= DDR_IO_WIDTH_8
838 && io_width <= DDR_IO_WIDTH_32);
839
840 /* Combinations of EMIF and PHY revisions that we support today */
841 switch (ip_rev) {
842 case EMIF_4D:
843 valid = valid && (phy_type == EMIF_PHY_TYPE_ATTILAPHY);
844 break;
845 case EMIF_4D5:
846 valid = valid && (phy_type == EMIF_PHY_TYPE_INTELLIPHY);
847 break;
848 default:
849 valid = 0;
850 }
851
852 if (!valid)
853 dev_err(dev, "%s: invalid DDR details\n", __func__);
854 return valid;
855}
856
857static int is_custom_config_valid(struct emif_custom_configs *cust_cfgs,
858 struct device *dev)
859{
860 int valid = 1;
861
862 if ((cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE) &&
863 (cust_cfgs->lpmode != EMIF_LP_MODE_DISABLE))
864 valid = cust_cfgs->lpmode_freq_threshold &&
865 cust_cfgs->lpmode_timeout_performance &&
866 cust_cfgs->lpmode_timeout_power;
867
868 if (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL)
869 valid = valid && cust_cfgs->temp_alert_poll_interval_ms;
870
871 if (!valid)
872 dev_warn(dev, "%s: invalid custom configs\n", __func__);
873
874 return valid;
875}
876
877static struct emif_data *__init_or_module get_device_details(
878 struct platform_device *pdev)
879{
880 u32 size;
881 struct emif_data *emif = NULL;
882 struct ddr_device_info *dev_info;
883 struct emif_custom_configs *cust_cfgs;
884 struct emif_platform_data *pd;
885 struct device *dev;
886 void *temp;
887
888 pd = pdev->dev.platform_data;
889 dev = &pdev->dev;
890
891 if (!(pd && pd->device_info && is_dev_data_valid(pd->device_info->type,
892 pd->device_info->density, pd->device_info->io_width,
893 pd->phy_type, pd->ip_rev, dev))) {
894 dev_err(dev, "%s: invalid device data\n", __func__);
895 goto error;
896 }
897
898 emif = devm_kzalloc(dev, sizeof(*emif), GFP_KERNEL);
899 temp = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
900 dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
901
902 if (!emif || !pd || !dev_info) {
903 dev_err(dev, "%s:%d: allocation error\n", __func__, __LINE__);
904 goto error;
905 }
906
907 memcpy(temp, pd, sizeof(*pd));
908 pd = temp;
909 memcpy(dev_info, pd->device_info, sizeof(*dev_info));
910
911 pd->device_info = dev_info;
912 emif->plat_data = pd;
913 emif->dev = dev;
914 emif->temperature_level = SDRAM_TEMP_NOMINAL;
915
916 /*
917 * For EMIF instances other than EMIF1 see if the devices connected
918 * are exactly same as on EMIF1(which is typically the case). If so,
919 * mark it as a duplicate of EMIF1 and skip copying timings data.
920 * This will save some memory and some computation later.
921 */
922 emif->duplicate = emif1 && (memcmp(dev_info,
923 emif1->plat_data->device_info,
924 sizeof(struct ddr_device_info)) == 0);
925
926 if (emif->duplicate) {
927 pd->timings = NULL;
928 pd->min_tck = NULL;
929 goto out;
930 } else if (emif1) {
931 dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
932 __func__);
933 }
934
935 /*
936 * Copy custom configs - ignore allocation error, if any, as
937 * custom_configs is not very critical
938 */
939 cust_cfgs = pd->custom_configs;
940 if (cust_cfgs && is_custom_config_valid(cust_cfgs, dev)) {
941 temp = devm_kzalloc(dev, sizeof(*cust_cfgs), GFP_KERNEL);
942 if (temp)
943 memcpy(temp, cust_cfgs, sizeof(*cust_cfgs));
944 else
945 dev_warn(dev, "%s:%d: allocation error\n", __func__,
946 __LINE__);
947 pd->custom_configs = temp;
948 }
949
950 /*
951 * Copy timings and min-tck values from platform data. If it is not
952 * available or if memory allocation fails, use JEDEC defaults
953 */
954 size = sizeof(struct lpddr2_timings) * pd->timings_arr_size;
955 if (pd->timings) {
956 temp = devm_kzalloc(dev, size, GFP_KERNEL);
957 if (temp) {
958 memcpy(temp, pd->timings, sizeof(*pd->timings));
959 pd->timings = temp;
960 } else {
961 dev_warn(dev, "%s:%d: allocation error\n", __func__,
962 __LINE__);
963 get_default_timings(emif);
964 }
965 } else {
966 get_default_timings(emif);
967 }
968
969 if (pd->min_tck) {
970 temp = devm_kzalloc(dev, sizeof(*pd->min_tck), GFP_KERNEL);
971 if (temp) {
972 memcpy(temp, pd->min_tck, sizeof(*pd->min_tck));
973 pd->min_tck = temp;
974 } else {
975 dev_warn(dev, "%s:%d: allocation error\n", __func__,
976 __LINE__);
977 pd->min_tck = &lpddr2_jedec_min_tck;
978 }
979 } else {
980 pd->min_tck = &lpddr2_jedec_min_tck;
981 }
982
983out:
984 return emif;
985
986error:
987 return NULL;
988}
989
990static int __init_or_module emif_probe(struct platform_device *pdev)
991{
992 struct emif_data *emif;
993 struct resource *res;
Aneesh V68b4aee2012-04-27 17:54:07 +0530994 int irq;
Aneesh V7ec94452012-04-27 17:54:05 +0530995
996 emif = get_device_details(pdev);
997 if (!emif) {
998 pr_err("%s: error getting device data\n", __func__);
999 goto error;
1000 }
1001
Aneesh V7ec94452012-04-27 17:54:05 +05301002 list_add(&emif->node, &device_list);
Aneesh Va93de282012-04-27 17:54:06 +05301003 emif->addressing = get_addressing_table(emif->plat_data->device_info);
Aneesh V7ec94452012-04-27 17:54:05 +05301004
1005 /* Save pointers to each other in emif and device structures */
1006 emif->dev = &pdev->dev;
1007 platform_set_drvdata(pdev, emif);
1008
1009 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1010 if (!res) {
1011 dev_err(emif->dev, "%s: error getting memory resource\n",
1012 __func__);
1013 goto error;
1014 }
1015
1016 emif->base = devm_request_and_ioremap(emif->dev, res);
1017 if (!emif->base) {
1018 dev_err(emif->dev, "%s: devm_request_and_ioremap() failed\n",
1019 __func__);
1020 goto error;
1021 }
1022
Aneesh V68b4aee2012-04-27 17:54:07 +05301023 irq = platform_get_irq(pdev, 0);
1024 if (irq < 0) {
1025 dev_err(emif->dev, "%s: error getting IRQ resource - %d\n",
1026 __func__, irq);
1027 goto error;
1028 }
1029
1030 disable_and_clear_all_interrupts(emif);
1031 setup_interrupts(emif, irq);
1032
Aneesh Va93de282012-04-27 17:54:06 +05301033 /* One-time actions taken on probing the first device */
1034 if (!emif1) {
1035 emif1 = emif;
1036 spin_lock_init(&emif_lock);
1037
1038 /*
1039 * TODO: register notifiers for frequency and voltage
1040 * change here once the respective frameworks are
1041 * available
1042 */
1043 }
1044
Aneesh V68b4aee2012-04-27 17:54:07 +05301045 dev_info(&pdev->dev, "%s: device configured with addr = %p and IRQ%d\n",
1046 __func__, emif->base, irq);
Aneesh V7ec94452012-04-27 17:54:05 +05301047
1048 return 0;
1049error:
1050 return -ENODEV;
1051}
1052
Aneesh V68b4aee2012-04-27 17:54:07 +05301053static void emif_shutdown(struct platform_device *pdev)
1054{
1055 struct emif_data *emif = platform_get_drvdata(pdev);
1056
1057 disable_and_clear_all_interrupts(emif);
1058}
1059
Aneesh Va93de282012-04-27 17:54:06 +05301060static int get_emif_reg_values(struct emif_data *emif, u32 freq,
1061 struct emif_regs *regs)
1062{
1063 u32 cs1_used, ip_rev, phy_type;
1064 u32 cl, type;
1065 const struct lpddr2_timings *timings;
1066 const struct lpddr2_min_tck *min_tck;
1067 const struct ddr_device_info *device_info;
1068 const struct lpddr2_addressing *addressing;
1069 struct emif_data *emif_for_calc;
1070 struct device *dev;
1071 const struct emif_custom_configs *custom_configs;
1072
1073 dev = emif->dev;
1074 /*
1075 * If the devices on this EMIF instance is duplicate of EMIF1,
1076 * use EMIF1 details for the calculation
1077 */
1078 emif_for_calc = emif->duplicate ? emif1 : emif;
1079 timings = get_timings_table(emif_for_calc, freq);
1080 addressing = emif_for_calc->addressing;
1081 if (!timings || !addressing) {
1082 dev_err(dev, "%s: not enough data available for %dHz",
1083 __func__, freq);
1084 return -1;
1085 }
1086
1087 device_info = emif_for_calc->plat_data->device_info;
1088 type = device_info->type;
1089 cs1_used = device_info->cs1_used;
1090 ip_rev = emif_for_calc->plat_data->ip_rev;
1091 phy_type = emif_for_calc->plat_data->phy_type;
1092
1093 min_tck = emif_for_calc->plat_data->min_tck;
1094 custom_configs = emif_for_calc->plat_data->custom_configs;
1095
1096 set_ddr_clk_period(freq);
1097
1098 regs->ref_ctrl_shdw = get_sdram_ref_ctrl_shdw(freq, addressing);
1099 regs->sdram_tim1_shdw = get_sdram_tim_1_shdw(timings, min_tck,
1100 addressing);
1101 regs->sdram_tim2_shdw = get_sdram_tim_2_shdw(timings, min_tck,
1102 addressing, type);
1103 regs->sdram_tim3_shdw = get_sdram_tim_3_shdw(timings, min_tck,
1104 addressing, type, ip_rev, EMIF_NORMAL_TIMINGS);
1105
1106 cl = get_cl(emif);
1107
1108 if (phy_type == EMIF_PHY_TYPE_ATTILAPHY && ip_rev == EMIF_4D) {
1109 regs->phy_ctrl_1_shdw = get_ddr_phy_ctrl_1_attilaphy_4d(
1110 timings, freq, cl);
1111 } else if (phy_type == EMIF_PHY_TYPE_INTELLIPHY && ip_rev == EMIF_4D5) {
1112 regs->phy_ctrl_1_shdw = get_phy_ctrl_1_intelliphy_4d5(freq, cl);
1113 regs->ext_phy_ctrl_2_shdw = get_ext_phy_ctrl_2_intelliphy_4d5();
1114 regs->ext_phy_ctrl_3_shdw = get_ext_phy_ctrl_3_intelliphy_4d5();
1115 regs->ext_phy_ctrl_4_shdw = get_ext_phy_ctrl_4_intelliphy_4d5();
1116 } else {
1117 return -1;
1118 }
1119
1120 /* Only timeout values in pwr_mgmt_ctrl_shdw register */
1121 regs->pwr_mgmt_ctrl_shdw =
1122 get_pwr_mgmt_ctrl(freq, emif_for_calc, ip_rev) &
1123 (CS_TIM_MASK | SR_TIM_MASK | PD_TIM_MASK);
1124
1125 if (ip_rev & EMIF_4D) {
1126 regs->read_idle_ctrl_shdw_normal =
1127 get_read_idle_ctrl_shdw(DDR_VOLTAGE_STABLE);
1128
1129 regs->read_idle_ctrl_shdw_volt_ramp =
1130 get_read_idle_ctrl_shdw(DDR_VOLTAGE_RAMPING);
1131 } else if (ip_rev & EMIF_4D5) {
1132 regs->dll_calib_ctrl_shdw_normal =
1133 get_dll_calib_ctrl_shdw(DDR_VOLTAGE_STABLE);
1134
1135 regs->dll_calib_ctrl_shdw_volt_ramp =
1136 get_dll_calib_ctrl_shdw(DDR_VOLTAGE_RAMPING);
1137 }
1138
1139 if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4) {
1140 regs->ref_ctrl_shdw_derated = get_sdram_ref_ctrl_shdw(freq / 4,
1141 addressing);
1142
1143 regs->sdram_tim1_shdw_derated =
1144 get_sdram_tim_1_shdw_derated(timings, min_tck,
1145 addressing);
1146
1147 regs->sdram_tim3_shdw_derated = get_sdram_tim_3_shdw(timings,
1148 min_tck, addressing, type, ip_rev,
1149 EMIF_DERATED_TIMINGS);
1150 }
1151
1152 regs->freq = freq;
1153
1154 return 0;
1155}
1156
1157/*
1158 * get_regs() - gets the cached emif_regs structure for a given EMIF instance
1159 * given frequency(freq):
1160 *
1161 * As an optimisation, every EMIF instance other than EMIF1 shares the
1162 * register cache with EMIF1 if the devices connected on this instance
1163 * are same as that on EMIF1(indicated by the duplicate flag)
1164 *
1165 * If we do not have an entry corresponding to the frequency given, we
1166 * allocate a new entry and calculate the values
1167 *
1168 * Upon finding the right reg dump, save it in curr_regs. It can be
1169 * directly used for thermal de-rating and voltage ramping changes.
1170 */
1171static struct emif_regs *get_regs(struct emif_data *emif, u32 freq)
1172{
1173 int i;
1174 struct emif_regs **regs_cache;
1175 struct emif_regs *regs = NULL;
1176 struct device *dev;
1177
1178 dev = emif->dev;
1179 if (emif->curr_regs && emif->curr_regs->freq == freq) {
1180 dev_dbg(dev, "%s: using curr_regs - %u Hz", __func__, freq);
1181 return emif->curr_regs;
1182 }
1183
1184 if (emif->duplicate)
1185 regs_cache = emif1->regs_cache;
1186 else
1187 regs_cache = emif->regs_cache;
1188
1189 for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) {
1190 if (regs_cache[i]->freq == freq) {
1191 regs = regs_cache[i];
1192 dev_dbg(dev,
1193 "%s: reg dump found in reg cache for %u Hz\n",
1194 __func__, freq);
1195 break;
1196 }
1197 }
1198
1199 /*
1200 * If we don't have an entry for this frequency in the cache create one
1201 * and calculate the values
1202 */
1203 if (!regs) {
1204 regs = devm_kzalloc(emif->dev, sizeof(*regs), GFP_ATOMIC);
1205 if (!regs)
1206 return NULL;
1207
1208 if (get_emif_reg_values(emif, freq, regs)) {
1209 devm_kfree(emif->dev, regs);
1210 return NULL;
1211 }
1212
1213 /*
1214 * Now look for an un-used entry in the cache and save the
1215 * newly created struct. If there are no free entries
1216 * over-write the last entry
1217 */
1218 for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++)
1219 ;
1220
1221 if (i >= EMIF_MAX_NUM_FREQUENCIES) {
1222 dev_warn(dev, "%s: regs_cache full - reusing a slot!!\n",
1223 __func__);
1224 i = EMIF_MAX_NUM_FREQUENCIES - 1;
1225 devm_kfree(emif->dev, regs_cache[i]);
1226 }
1227 regs_cache[i] = regs;
1228 }
1229
1230 return regs;
1231}
1232
1233static void do_volt_notify_handling(struct emif_data *emif, u32 volt_state)
1234{
1235 dev_dbg(emif->dev, "%s: voltage notification : %d", __func__,
1236 volt_state);
1237
1238 if (!emif->curr_regs) {
1239 dev_err(emif->dev,
1240 "%s: volt-notify before registers are ready: %d\n",
1241 __func__, volt_state);
1242 return;
1243 }
1244
1245 setup_volt_sensitive_regs(emif, emif->curr_regs, volt_state);
1246}
1247
1248/*
1249 * TODO: voltage notify handling should be hooked up to
1250 * regulator framework as soon as the necessary support
1251 * is available in mainline kernel. This function is un-used
1252 * right now.
1253 */
1254static void __attribute__((unused)) volt_notify_handling(u32 volt_state)
1255{
1256 struct emif_data *emif;
1257
1258 spin_lock_irqsave(&emif_lock, irq_state);
1259
1260 list_for_each_entry(emif, &device_list, node)
1261 do_volt_notify_handling(emif, volt_state);
1262 do_freq_update();
1263
1264 spin_unlock_irqrestore(&emif_lock, irq_state);
1265}
1266
1267static void do_freq_pre_notify_handling(struct emif_data *emif, u32 new_freq)
1268{
1269 struct emif_regs *regs;
1270
1271 regs = get_regs(emif, new_freq);
1272 if (!regs)
1273 return;
1274
1275 emif->curr_regs = regs;
1276
1277 /*
1278 * Update the shadow registers:
1279 * Temperature and voltage-ramp sensitive settings are also configured
1280 * in terms of DDR cycles. So, we need to update them too when there
1281 * is a freq change
1282 */
1283 dev_dbg(emif->dev, "%s: setting up shadow registers for %uHz",
1284 __func__, new_freq);
1285 setup_registers(emif, regs);
1286 setup_temperature_sensitive_regs(emif, regs);
1287 setup_volt_sensitive_regs(emif, regs, DDR_VOLTAGE_STABLE);
1288
1289 /*
1290 * Part of workaround for errata i728. See do_freq_update()
1291 * for more details
1292 */
1293 if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
1294 set_lpmode(emif, EMIF_LP_MODE_DISABLE);
1295}
1296
1297/*
1298 * TODO: frequency notify handling should be hooked up to
1299 * clock framework as soon as the necessary support is
1300 * available in mainline kernel. This function is un-used
1301 * right now.
1302 */
1303static void __attribute__((unused)) freq_pre_notify_handling(u32 new_freq)
1304{
1305 struct emif_data *emif;
1306
1307 /*
1308 * NOTE: we are taking the spin-lock here and releases it
1309 * only in post-notifier. This doesn't look good and
1310 * Sparse complains about it, but this seems to be
1311 * un-avoidable. We need to lock a sequence of events
1312 * that is split between EMIF and clock framework.
1313 *
1314 * 1. EMIF driver updates EMIF timings in shadow registers in the
1315 * frequency pre-notify callback from clock framework
1316 * 2. clock framework sets up the registers for the new frequency
1317 * 3. clock framework initiates a hw-sequence that updates
1318 * the frequency EMIF timings synchronously.
1319 *
1320 * All these 3 steps should be performed as an atomic operation
1321 * vis-a-vis similar sequence in the EMIF interrupt handler
1322 * for temperature events. Otherwise, there could be race
1323 * conditions that could result in incorrect EMIF timings for
1324 * a given frequency
1325 */
1326 spin_lock_irqsave(&emif_lock, irq_state);
1327
1328 list_for_each_entry(emif, &device_list, node)
1329 do_freq_pre_notify_handling(emif, new_freq);
1330}
1331
1332static void do_freq_post_notify_handling(struct emif_data *emif)
1333{
1334 /*
1335 * Part of workaround for errata i728. See do_freq_update()
1336 * for more details
1337 */
1338 if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH)
1339 set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH);
1340}
1341
1342/*
1343 * TODO: frequency notify handling should be hooked up to
1344 * clock framework as soon as the necessary support is
1345 * available in mainline kernel. This function is un-used
1346 * right now.
1347 */
1348static void __attribute__((unused)) freq_post_notify_handling(void)
1349{
1350 struct emif_data *emif;
1351
1352 list_for_each_entry(emif, &device_list, node)
1353 do_freq_post_notify_handling(emif);
1354
1355 /*
1356 * Lock is done in pre-notify handler. See freq_pre_notify_handling()
1357 * for more details
1358 */
1359 spin_unlock_irqrestore(&emif_lock, irq_state);
1360}
1361
Aneesh V7ec94452012-04-27 17:54:05 +05301362static struct platform_driver emif_driver = {
Aneesh V68b4aee2012-04-27 17:54:07 +05301363 .shutdown = emif_shutdown,
Aneesh V7ec94452012-04-27 17:54:05 +05301364 .driver = {
1365 .name = "emif",
1366 },
1367};
1368
1369static int __init_or_module emif_register(void)
1370{
1371 return platform_driver_probe(&emif_driver, emif_probe);
1372}
1373
1374static void __exit emif_unregister(void)
1375{
1376 platform_driver_unregister(&emif_driver);
1377}
1378
1379module_init(emif_register);
1380module_exit(emif_unregister);
1381MODULE_DESCRIPTION("TI EMIF SDRAM Controller Driver");
1382MODULE_LICENSE("GPL");
1383MODULE_ALIAS("platform:emif");
1384MODULE_AUTHOR("Texas Instruments Inc");