13 #include <linux/kernel.h>
14 #include <linux/reboot.h>
17 #include <linux/device.h>
20 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/list.h>
78 #ifdef CONFIG_DEBUG_FS
85 seq_printf(s,
"EMIF register cache dump for %dMHz\n",
94 seq_printf(s,
"read_idle_ctrl_shdw_normal\t: 0x%08x\n",
96 seq_printf(s,
"read_idle_ctrl_shdw_volt_ramp\t: 0x%08x\n",
99 seq_printf(s,
"dll_calib_ctrl_shdw_normal\t: 0x%08x\n",
101 seq_printf(s,
"dll_calib_ctrl_shdw_volt_ramp\t: 0x%08x\n",
106 seq_printf(s,
"ref_ctrl_shdw_derated\t: 0x%08x\n",
108 seq_printf(s,
"sdram_tim1_shdw_derated\t: 0x%08x\n",
110 seq_printf(s,
"sdram_tim3_shdw_derated\t: 0x%08x\n",
127 do_emif_regdump_show(s, emif, regs_cache[i]);
140 .
open = emif_regdump_open,
145 static int emif_mr4_show(
struct seq_file *s,
void *unused)
159 .
open = emif_mr4_open,
216 static void set_ddr_clk_period(
u32 freq)
235 width = width == 0 ? 32 : 16;
253 static void set_lpmode(
struct emif_data *emif,
u8 lpmode)
264 static void do_freq_update(
void)
314 type = device_info->
type;
315 density = device_info->
density;
351 freq_nearest = 1000000000;
358 for (i = 0; i < emif->
plat_data->timings_arr_size; i++) {
361 if ((freq >= min) && (freq <= max) && (max < freq_nearest)) {
363 timings = &timings_arr[
i];
368 dev_err(dev,
"%s: couldn't find timings for - %dHz\n",
371 dev_dbg(dev,
"%s: timings table: freq %d, speed bin freq %d\n",
372 __func__, freq, freq_nearest);
377 static u32 get_sdram_ref_ctrl_shdw(
u32 freq,
380 u32 ref_ctrl_shdw = 0,
val = 0, freq_khz, t_refi;
383 freq_khz = freq / 1000;
384 t_refi = addressing->
tREFI_ns / 100;
390 val = t_refi * freq_khz / 10000;
393 return ref_ctrl_shdw;
451 tim1 |= (
val - 1) << T_RC_SHIFT;
461 tim1 |= (
val - 1) << T_RCD_SHIFT;
500 u32 tim3 = 0,
val = 0, t_dqsck;
536 bool cs1_used,
bool cal_resistors_per_cs)
551 if (cal_resistors_per_cs)
558 val = cs1_used ? 1 : 0;
566 u32 sdram_io_width,
u32 emif_bus_width)
570 if (custom_configs && (custom_configs->
mask &
585 emif_bus_width =
__fls(emif_bus_width) - 1;
586 devcnt = emif_bus_width - sdram_io_width;
599 static u32 get_read_idle_ctrl_shdw(
u8 volt_ramp)
622 static u32 get_dll_calib_ctrl_shdw(
u8 volt_ramp)
645 if (freq <= 100000000)
647 else if (freq <= 200000000)
657 static u32 get_phy_ctrl_1_intelliphy_4d5(
u32 freq,
u8 cl)
665 if (freq >= 265000000 && freq < 267000000)
677 static u32 get_ext_phy_ctrl_2_intelliphy_4d5(
void)
679 u32 fifo_we_slave_ratio;
684 return fifo_we_slave_ratio | fifo_we_slave_ratio << 11 |
685 fifo_we_slave_ratio << 22;
688 static u32 get_ext_phy_ctrl_3_intelliphy_4d5(
void)
690 u32 fifo_we_slave_ratio;
695 return fifo_we_slave_ratio >> 10 | fifo_we_slave_ratio << 1 |
696 fifo_we_slave_ratio << 12 | fifo_we_slave_ratio << 23;
699 static u32 get_ext_phy_ctrl_4_intelliphy_4d5(
void)
701 u32 fifo_we_slave_ratio;
706 return fifo_we_slave_ratio >> 9 | fifo_we_slave_ratio << 2 |
707 fifo_we_slave_ratio << 13;
721 lpmode = cust_cfgs->
lpmode;
728 timeout = freq >= freq_threshold ? timeout_perf : timeout_pwr;
735 if (timeout & (timeout - 1))
768 return pwr_mgmt_ctrl;
777 static void get_temperature_level(
struct emif_data *emif)
790 if (emif->
plat_data->device_info->cs1_used) {
795 temperature_level =
max(temp, temperature_level);
830 static void setup_volt_sensitive_regs(
struct emif_data *emif,
858 static void setup_temperature_sensitive_regs(
struct emif_data *emif,
861 u32 tim1, tim3, ref_ctrl,
type;
865 type = emif->
plat_data->device_info->type;
897 get_temperature_level(emif);
902 dev_err(emif->
dev,
"temperature alert before registers are calculated, not de-rating timings\n");
916 setup_temperature_sensitive_regs(emif, emif->
curr_regs);
921 spin_unlock_irqrestore(&emif_lock,
irq_state);
943 ret = handle_temp_alert(base, emif);
946 dev_err(dev,
"Access error from SYS port - %x\n", interrupts);
954 dev_err(dev,
"Access error from LL port - %x\n",
961 static irqreturn_t emif_threaded_isr(
int irq,
void *dev_id)
966 dev_emerg(emif->
dev,
"SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
974 setup_temperature_sensitive_regs(emif, emif->
curr_regs);
977 dev_err(emif->
dev,
"temperature alert before registers are calculated, not de-rating timings\n");
980 spin_unlock_irqrestore(&emif_lock,
irq_state);
985 static void clear_all_interrupts(
struct emif_data *emif)
996 static void disable_and_clear_all_interrupts(
struct emif_data *emif)
1008 clear_all_interrupts(emif);
1016 type = emif->
plat_data->device_info->type;
1018 clear_all_interrupts(emif);
1035 emif_interrupt_handler,
1037 0, dev_name(emif->
dev),
1044 u32 pwr_mgmt_ctrl, zq, temp_alert_cfg;
1049 device_info = emif->
plat_data->device_info;
1050 addressing = get_addressing_table(device_info);
1057 pwr_mgmt_ctrl = get_pwr_mgmt_ctrl(1000000000, emif,
1063 zq = get_zq_config_reg(addressing, device_info->
cs1_used,
1068 get_temperature_level(emif);
1070 dev_emerg(emif->
dev,
"SDRAM temperature exceeds operating limit.. Needs shut down!!!\n");
1073 temp_alert_cfg = get_temp_alert_config(addressing,
1075 device_info->
io_width, get_emif_bus_width(emif));
1107 static void get_default_timings(
struct emif_data *emif)
1114 dev_warn(emif->
dev,
"%s: using default timings\n", __func__);
1142 dev_err(dev,
"%s: invalid DDR details\n", __func__);
1161 dev_warn(dev,
"%s: invalid custom configs\n", __func__);
1166 #if defined(CONFIG_OF)
1172 const int *
lpmode, *poll_intvl;
1175 poll_intvl =
of_get_property(np_emif,
"temp-alert-poll-interval", &len);
1177 if (lpmode || poll_intvl)
1187 of_property_read_u32(np_emif,
1188 "low-power-mode-timeout-performance",
1190 of_property_read_u32(np_emif,
1191 "low-power-mode-timeout-power",
1193 of_property_read_u32(np_emif,
1194 "low-power-mode-freq-threshold",
1204 if (!is_custom_config_valid(cust_cfgs, emif->
dev)) {
1209 emif->
plat_data->custom_configs = cust_cfgs;
1216 u32 density = 0, io_width = 0;
1230 of_property_read_u32(np_ddr,
"density", &density);
1231 of_property_read_u32(np_ddr,
"io-width", &io_width);
1234 if (density & (density - 1))
1240 if (io_width & (io_width - 1))
1262 if (!emif || !pd || !dev_info) {
1263 dev_err(dev,
"%s: Out of memory!!\n",
1279 of_property_read_u32(np_emif,
"phy-type", &pd->
phy_type);
1284 of_get_ddr_info(np_emif, np_ddr, dev_info);
1288 dev_err(dev,
"%s: invalid device data!!\n", __func__);
1297 if (emif1 && emif1->
np_ddr == np_ddr) {
1301 dev_warn(emif->
dev,
"%s: Non-symmetric DDR geometry\n",
1305 of_get_custom_configs(np_emif, emif);
1339 pd = pdev->
dev.platform_data;
1345 dev_err(dev,
"%s: invalid device data\n", __func__);
1353 if (!emif || !pd || !dev_info) {
1354 dev_err(dev,
"%s:%d: allocation error\n", __func__, __LINE__);
1358 memcpy(temp, pd,
sizeof(*pd));
1382 dev_warn(emif->
dev,
"%s: Non-symmetric DDR geometry\n",
1391 if (cust_cfgs && is_custom_config_valid(cust_cfgs, dev)) {
1394 memcpy(temp, cust_cfgs,
sizeof(*cust_cfgs));
1396 dev_warn(dev,
"%s:%d: allocation error\n", __func__,
1412 dev_warn(dev,
"%s:%d: allocation error\n", __func__,
1414 get_default_timings(emif);
1417 get_default_timings(emif);
1426 dev_warn(dev,
"%s:%d: allocation error\n", __func__,
1447 if (pdev->
dev.of_node)
1448 emif = of_get_memory_device_details(pdev->
dev.of_node, &pdev->
dev);
1450 emif = get_device_details(pdev);
1453 pr_err(
"%s: error getting device data\n", __func__);
1462 platform_set_drvdata(pdev, emif);
1466 dev_err(emif->
dev,
"%s: error getting memory resource\n",
1473 dev_err(emif->
dev,
"%s: devm_request_and_ioremap() failed\n",
1480 dev_err(emif->
dev,
"%s: error getting IRQ resource - %d\n",
1485 emif_onetime_settings(emif);
1486 emif_debugfs_init(emif);
1487 disable_and_clear_all_interrupts(emif);
1488 setup_interrupts(emif, irq);
1502 dev_info(&pdev->
dev,
"%s: device configured with addr = %p and IRQ%d\n",
1503 __func__, emif->
base, irq);
1512 struct emif_data *emif = platform_get_drvdata(pdev);
1514 emif_debugfs_exit(emif);
1521 struct emif_data *emif = platform_get_drvdata(pdev);
1523 disable_and_clear_all_interrupts(emif);
1526 static int get_emif_reg_values(
struct emif_data *emif,
u32 freq,
1529 u32 cs1_used, ip_rev, phy_type;
1544 emif_for_calc = emif->
duplicate ? emif1 : emif;
1545 timings = get_timings_table(emif_for_calc, freq);
1547 if (!timings || !addressing) {
1548 dev_err(dev,
"%s: not enough data available for %dHz",
1553 device_info = emif_for_calc->
plat_data->device_info;
1554 type = device_info->
type;
1556 ip_rev = emif_for_calc->
plat_data->ip_rev;
1557 phy_type = emif_for_calc->
plat_data->phy_type;
1559 min_tck = emif_for_calc->
plat_data->min_tck;
1560 custom_configs = emif_for_calc->
plat_data->custom_configs;
1562 set_ddr_clk_period(freq);
1564 regs->
ref_ctrl_shdw = get_sdram_ref_ctrl_shdw(freq, addressing);
1588 get_pwr_mgmt_ctrl(freq, emif_for_calc, ip_rev) &
1610 get_sdram_tim_1_shdw_derated(timings, min_tck,
1614 min_tck, addressing, type, ip_rev,
1646 dev_dbg(dev,
"%s: using curr_regs - %u Hz", __func__, freq);
1656 if (regs_cache[i]->freq == freq) {
1657 regs = regs_cache[
i];
1659 "%s: reg dump found in reg cache for %u Hz\n",
1674 if (get_emif_reg_values(emif, freq, regs)) {
1688 dev_warn(dev,
"%s: regs_cache full - reusing a slot!!\n",
1693 regs_cache[
i] =
regs;
1699 static void do_volt_notify_handling(
struct emif_data *emif,
u32 volt_state)
1701 dev_dbg(emif->
dev,
"%s: voltage notification : %d", __func__,
1706 "%s: volt-notify before registers are ready: %d\n",
1707 __func__, volt_state);
1711 setup_volt_sensitive_regs(emif, emif->
curr_regs, volt_state);
1727 do_volt_notify_handling(emif, volt_state);
1730 spin_unlock_irqrestore(&emif_lock,
irq_state);
1737 regs = get_regs(emif, new_freq);
1741 emif->curr_regs =
regs;
1749 dev_dbg(emif->dev,
"%s: setting up shadow registers for %uHz",
1750 __func__, new_freq);
1751 setup_registers(emif, regs);
1752 setup_temperature_sensitive_regs(emif, regs);
1795 do_freq_pre_notify_handling(emif, new_freq);
1814 static void __attribute__((unused)) freq_post_notify_handling(
void)
1819 do_freq_post_notify_handling(emif);
1825 spin_unlock_irqrestore(&emif_lock, irq_state);
1828 #if defined(CONFIG_OF)
1831 { .compatible =
"ti,emif-4d5" },
1839 .shutdown = emif_shutdown,
1851 static void __exit emif_unregister(
void)