1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI AM33XX SRAM EMIF Driver
5 * Copyright (C) 2016-2017 Texas Instruments Inc.
10 #include <linux/genalloc.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/sram.h>
18 #include <linux/ti-emif-sram.h>
22 #define TI_EMIF_SRAM_SYMBOL_OFFSET(sym) ((unsigned long)(sym) - \
23 (unsigned long)&ti_emif_sram)
25 #define EMIF_POWER_MGMT_WAIT_SELF_REFRESH_8192_CYCLES 0x00a0
28 phys_addr_t ti_emif_sram_phys
;
29 phys_addr_t ti_emif_sram_data_phys
;
30 unsigned long ti_emif_sram_virt
;
31 unsigned long ti_emif_sram_data_virt
;
32 struct gen_pool
*sram_pool_code
;
33 struct gen_pool
*sram_pool_data
;
34 struct ti_emif_pm_data pm_data
;
35 struct ti_emif_pm_functions pm_functions
;
38 static struct ti_emif_data
*emif_instance
;
40 static u32
sram_suspend_address(struct ti_emif_data
*emif_data
,
43 return (emif_data
->ti_emif_sram_virt
+
44 TI_EMIF_SRAM_SYMBOL_OFFSET(addr
));
47 static phys_addr_t
sram_resume_address(struct ti_emif_data
*emif_data
,
50 return ((unsigned long)emif_data
->ti_emif_sram_phys
+
51 TI_EMIF_SRAM_SYMBOL_OFFSET(addr
));
54 static void ti_emif_free_sram(struct ti_emif_data
*emif_data
)
56 gen_pool_free(emif_data
->sram_pool_code
, emif_data
->ti_emif_sram_virt
,
58 gen_pool_free(emif_data
->sram_pool_data
,
59 emif_data
->ti_emif_sram_data_virt
,
60 sizeof(struct emif_regs_amx3
));
63 static int ti_emif_alloc_sram(struct device
*dev
,
64 struct ti_emif_data
*emif_data
)
66 struct device_node
*np
= dev
->of_node
;
69 emif_data
->sram_pool_code
= of_gen_pool_get(np
, "sram", 0);
70 if (!emif_data
->sram_pool_code
) {
71 dev_err(dev
, "Unable to get sram pool for ocmcram code\n");
75 emif_data
->ti_emif_sram_virt
=
76 gen_pool_alloc(emif_data
->sram_pool_code
,
78 if (!emif_data
->ti_emif_sram_virt
) {
79 dev_err(dev
, "Unable to allocate code memory from ocmcram\n");
83 /* Save physical address to calculate resume offset during pm init */
84 emif_data
->ti_emif_sram_phys
=
85 gen_pool_virt_to_phys(emif_data
->sram_pool_code
,
86 emif_data
->ti_emif_sram_virt
);
88 /* Get sram pool for data section and allocate space */
89 emif_data
->sram_pool_data
= of_gen_pool_get(np
, "sram", 1);
90 if (!emif_data
->sram_pool_data
) {
91 dev_err(dev
, "Unable to get sram pool for ocmcram data\n");
93 goto err_free_sram_code
;
96 emif_data
->ti_emif_sram_data_virt
=
97 gen_pool_alloc(emif_data
->sram_pool_data
,
98 sizeof(struct emif_regs_amx3
));
99 if (!emif_data
->ti_emif_sram_data_virt
) {
100 dev_err(dev
, "Unable to allocate data memory from ocmcram\n");
102 goto err_free_sram_code
;
105 /* Save physical address to calculate resume offset during pm init */
106 emif_data
->ti_emif_sram_data_phys
=
107 gen_pool_virt_to_phys(emif_data
->sram_pool_data
,
108 emif_data
->ti_emif_sram_data_virt
);
110 * These functions are called during suspend path while MMU is
111 * still on so add virtual base to offset for absolute address
113 emif_data
->pm_functions
.save_context
=
114 sram_suspend_address(emif_data
,
115 (unsigned long)ti_emif_save_context
);
116 emif_data
->pm_functions
.enter_sr
=
117 sram_suspend_address(emif_data
,
118 (unsigned long)ti_emif_enter_sr
);
119 emif_data
->pm_functions
.abort_sr
=
120 sram_suspend_address(emif_data
,
121 (unsigned long)ti_emif_abort_sr
);
124 * These are called during resume path when MMU is not enabled
125 * so physical address is used instead
127 emif_data
->pm_functions
.restore_context
=
128 sram_resume_address(emif_data
,
129 (unsigned long)ti_emif_restore_context
);
130 emif_data
->pm_functions
.exit_sr
=
131 sram_resume_address(emif_data
,
132 (unsigned long)ti_emif_exit_sr
);
133 emif_data
->pm_functions
.run_hw_leveling
=
134 sram_resume_address(emif_data
,
135 (unsigned long)ti_emif_run_hw_leveling
);
137 emif_data
->pm_data
.regs_virt
=
138 (struct emif_regs_amx3
*)emif_data
->ti_emif_sram_data_virt
;
139 emif_data
->pm_data
.regs_phys
= emif_data
->ti_emif_sram_data_phys
;
144 gen_pool_free(emif_data
->sram_pool_code
, emif_data
->ti_emif_sram_virt
,
149 static int ti_emif_push_sram(struct device
*dev
, struct ti_emif_data
*emif_data
)
154 copy_addr
= sram_exec_copy(emif_data
->sram_pool_code
,
155 (void *)emif_data
->ti_emif_sram_virt
,
156 &ti_emif_sram
, ti_emif_sram_sz
);
158 dev_err(dev
, "Cannot copy emif code to sram\n");
162 data_addr
= sram_suspend_address(emif_data
,
163 (unsigned long)&ti_emif_pm_sram_data
);
164 copy_addr
= sram_exec_copy(emif_data
->sram_pool_code
,
167 sizeof(emif_data
->pm_data
));
169 dev_err(dev
, "Cannot copy emif data to code sram\n");
177 * Due to Usage Note 3.1.2 "DDR3: JEDEC Compliance for Maximum
178 * Self-Refresh Command Limit" found in AM335x Silicon Errata
179 * (Document SPRZ360F Revised November 2013) we must configure
180 * the self refresh delay timer to 0xA (8192 cycles) to avoid
181 * generating too many refresh command from the EMIF.
183 static void ti_emif_configure_sr_delay(struct ti_emif_data
*emif_data
)
185 writel(EMIF_POWER_MGMT_WAIT_SELF_REFRESH_8192_CYCLES
,
186 (emif_data
->pm_data
.ti_emif_base_addr_virt
+
187 EMIF_POWER_MANAGEMENT_CONTROL
));
189 writel(EMIF_POWER_MGMT_WAIT_SELF_REFRESH_8192_CYCLES
,
190 (emif_data
->pm_data
.ti_emif_base_addr_virt
+
191 EMIF_POWER_MANAGEMENT_CTRL_SHDW
));
195 * ti_emif_copy_pm_function_table - copy mapping of pm funcs in sram
196 * @sram_pool: pointer to struct gen_pool where dst resides
197 * @dst: void * to address that table should be copied
199 * Returns 0 if success other error code if table is not available
201 int ti_emif_copy_pm_function_table(struct gen_pool
*sram_pool
, void *dst
)
208 copy_addr
= sram_exec_copy(sram_pool
, dst
,
209 &emif_instance
->pm_functions
,
210 sizeof(emif_instance
->pm_functions
));
216 EXPORT_SYMBOL_GPL(ti_emif_copy_pm_function_table
);
219 * ti_emif_get_mem_type - return type for memory type in use
221 * Returns memory type value read from EMIF or error code if fails
223 int ti_emif_get_mem_type(void)
230 temp
= readl(emif_instance
->pm_data
.ti_emif_base_addr_virt
+
233 temp
= (temp
& SDRAM_TYPE_MASK
) >> SDRAM_TYPE_SHIFT
;
236 EXPORT_SYMBOL_GPL(ti_emif_get_mem_type
);
238 static const struct of_device_id ti_emif_of_match
[] = {
239 { .compatible
= "ti,emif-am3352", .data
=
240 (void *)EMIF_SRAM_AM33_REG_LAYOUT
, },
241 { .compatible
= "ti,emif-am4372", .data
=
242 (void *)EMIF_SRAM_AM43_REG_LAYOUT
, },
245 MODULE_DEVICE_TABLE(of
, ti_emif_of_match
);
247 #ifdef CONFIG_PM_SLEEP
248 static int ti_emif_resume(struct device
*dev
)
251 __raw_readl((void *)emif_instance
->ti_emif_sram_virt
);
254 * Check to see if what we are copying is already present in the
255 * first byte at the destination, only copy if it is not which
256 * indicates we have lost context and sram no longer contains
259 if (tmp
!= ti_emif_sram
)
260 ti_emif_push_sram(dev
, emif_instance
);
265 static int ti_emif_suspend(struct device
*dev
)
268 * The contents will be present in DDR hence no need to
273 #endif /* CONFIG_PM_SLEEP */
275 static int ti_emif_probe(struct platform_device
*pdev
)
278 struct resource
*res
;
279 struct device
*dev
= &pdev
->dev
;
280 const struct of_device_id
*match
;
281 struct ti_emif_data
*emif_data
;
283 emif_data
= devm_kzalloc(dev
, sizeof(*emif_data
), GFP_KERNEL
);
287 match
= of_match_device(ti_emif_of_match
, &pdev
->dev
);
291 emif_data
->pm_data
.ti_emif_sram_config
= (unsigned long)match
->data
;
293 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
294 emif_data
->pm_data
.ti_emif_base_addr_virt
= devm_ioremap_resource(dev
,
296 if (IS_ERR(emif_data
->pm_data
.ti_emif_base_addr_virt
)) {
297 ret
= PTR_ERR(emif_data
->pm_data
.ti_emif_base_addr_virt
);
301 emif_data
->pm_data
.ti_emif_base_addr_phys
= res
->start
;
303 ti_emif_configure_sr_delay(emif_data
);
305 ret
= ti_emif_alloc_sram(dev
, emif_data
);
309 ret
= ti_emif_push_sram(dev
, emif_data
);
313 emif_instance
= emif_data
;
318 ti_emif_free_sram(emif_data
);
323 static int ti_emif_remove(struct platform_device
*pdev
)
325 struct ti_emif_data
*emif_data
= emif_instance
;
327 emif_instance
= NULL
;
329 ti_emif_free_sram(emif_data
);
334 static const struct dev_pm_ops ti_emif_pm_ops
= {
335 SET_SYSTEM_SLEEP_PM_OPS(ti_emif_suspend
, ti_emif_resume
)
338 static struct platform_driver ti_emif_driver
= {
339 .probe
= ti_emif_probe
,
340 .remove
= ti_emif_remove
,
342 .name
= KBUILD_MODNAME
,
343 .of_match_table
= of_match_ptr(ti_emif_of_match
),
344 .pm
= &ti_emif_pm_ops
,
347 module_platform_driver(ti_emif_driver
);
349 MODULE_AUTHOR("Dave Gerlach <d-gerlach@ti.com>");
350 MODULE_DESCRIPTION("Texas Instruments SRAM EMIF driver");
351 MODULE_LICENSE("GPL v2");