ASoC: es8316: Add jack-detect support
[linux/fpc-iii.git] / drivers / memory / ti-emif-pm.c
blob2250d03ea17f63f5ad79b8e860aeb6c1383a6047
1 /*
2 * TI AM33XX SRAM EMIF Driver
4 * Copyright (C) 2016-2017 Texas Instruments Inc.
5 * Dave Gerlach
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/err.h>
18 #include <linux/genalloc.h>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_platform.h>
24 #include <linux/platform_device.h>
25 #include <linux/sram.h>
26 #include <linux/ti-emif-sram.h>
28 #include "emif.h"
30 #define TI_EMIF_SRAM_SYMBOL_OFFSET(sym) ((unsigned long)(sym) - \
31 (unsigned long)&ti_emif_sram)
33 #define EMIF_POWER_MGMT_WAIT_SELF_REFRESH_8192_CYCLES 0x00a0
35 struct ti_emif_data {
36 phys_addr_t ti_emif_sram_phys;
37 phys_addr_t ti_emif_sram_data_phys;
38 unsigned long ti_emif_sram_virt;
39 unsigned long ti_emif_sram_data_virt;
40 struct gen_pool *sram_pool_code;
41 struct gen_pool *sram_pool_data;
42 struct ti_emif_pm_data pm_data;
43 struct ti_emif_pm_functions pm_functions;
46 static struct ti_emif_data *emif_instance;
48 static u32 sram_suspend_address(struct ti_emif_data *emif_data,
49 unsigned long addr)
51 return (emif_data->ti_emif_sram_virt +
52 TI_EMIF_SRAM_SYMBOL_OFFSET(addr));
55 static phys_addr_t sram_resume_address(struct ti_emif_data *emif_data,
56 unsigned long addr)
58 return ((unsigned long)emif_data->ti_emif_sram_phys +
59 TI_EMIF_SRAM_SYMBOL_OFFSET(addr));
62 static void ti_emif_free_sram(struct ti_emif_data *emif_data)
64 gen_pool_free(emif_data->sram_pool_code, emif_data->ti_emif_sram_virt,
65 ti_emif_sram_sz);
66 gen_pool_free(emif_data->sram_pool_data,
67 emif_data->ti_emif_sram_data_virt,
68 sizeof(struct emif_regs_amx3));
71 static int ti_emif_alloc_sram(struct device *dev,
72 struct ti_emif_data *emif_data)
74 struct device_node *np = dev->of_node;
75 int ret;
77 emif_data->sram_pool_code = of_gen_pool_get(np, "sram", 0);
78 if (!emif_data->sram_pool_code) {
79 dev_err(dev, "Unable to get sram pool for ocmcram code\n");
80 return -ENODEV;
83 emif_data->ti_emif_sram_virt =
84 gen_pool_alloc(emif_data->sram_pool_code,
85 ti_emif_sram_sz);
86 if (!emif_data->ti_emif_sram_virt) {
87 dev_err(dev, "Unable to allocate code memory from ocmcram\n");
88 return -ENOMEM;
91 /* Save physical address to calculate resume offset during pm init */
92 emif_data->ti_emif_sram_phys =
93 gen_pool_virt_to_phys(emif_data->sram_pool_code,
94 emif_data->ti_emif_sram_virt);
96 /* Get sram pool for data section and allocate space */
97 emif_data->sram_pool_data = of_gen_pool_get(np, "sram", 1);
98 if (!emif_data->sram_pool_data) {
99 dev_err(dev, "Unable to get sram pool for ocmcram data\n");
100 ret = -ENODEV;
101 goto err_free_sram_code;
104 emif_data->ti_emif_sram_data_virt =
105 gen_pool_alloc(emif_data->sram_pool_data,
106 sizeof(struct emif_regs_amx3));
107 if (!emif_data->ti_emif_sram_data_virt) {
108 dev_err(dev, "Unable to allocate data memory from ocmcram\n");
109 ret = -ENOMEM;
110 goto err_free_sram_code;
113 /* Save physical address to calculate resume offset during pm init */
114 emif_data->ti_emif_sram_data_phys =
115 gen_pool_virt_to_phys(emif_data->sram_pool_data,
116 emif_data->ti_emif_sram_data_virt);
118 * These functions are called during suspend path while MMU is
119 * still on so add virtual base to offset for absolute address
121 emif_data->pm_functions.save_context =
122 sram_suspend_address(emif_data,
123 (unsigned long)ti_emif_save_context);
124 emif_data->pm_functions.enter_sr =
125 sram_suspend_address(emif_data,
126 (unsigned long)ti_emif_enter_sr);
127 emif_data->pm_functions.abort_sr =
128 sram_suspend_address(emif_data,
129 (unsigned long)ti_emif_abort_sr);
132 * These are called during resume path when MMU is not enabled
133 * so physical address is used instead
135 emif_data->pm_functions.restore_context =
136 sram_resume_address(emif_data,
137 (unsigned long)ti_emif_restore_context);
138 emif_data->pm_functions.exit_sr =
139 sram_resume_address(emif_data,
140 (unsigned long)ti_emif_exit_sr);
142 emif_data->pm_data.regs_virt =
143 (struct emif_regs_amx3 *)emif_data->ti_emif_sram_data_virt;
144 emif_data->pm_data.regs_phys = emif_data->ti_emif_sram_data_phys;
146 return 0;
148 err_free_sram_code:
149 gen_pool_free(emif_data->sram_pool_code, emif_data->ti_emif_sram_virt,
150 ti_emif_sram_sz);
151 return ret;
154 static int ti_emif_push_sram(struct device *dev, struct ti_emif_data *emif_data)
156 void *copy_addr;
157 u32 data_addr;
159 copy_addr = sram_exec_copy(emif_data->sram_pool_code,
160 (void *)emif_data->ti_emif_sram_virt,
161 &ti_emif_sram, ti_emif_sram_sz);
162 if (!copy_addr) {
163 dev_err(dev, "Cannot copy emif code to sram\n");
164 return -ENODEV;
167 data_addr = sram_suspend_address(emif_data,
168 (unsigned long)&ti_emif_pm_sram_data);
169 copy_addr = sram_exec_copy(emif_data->sram_pool_code,
170 (void *)data_addr,
171 &emif_data->pm_data,
172 sizeof(emif_data->pm_data));
173 if (!copy_addr) {
174 dev_err(dev, "Cannot copy emif data to code sram\n");
175 return -ENODEV;
178 return 0;
182 * Due to Usage Note 3.1.2 "DDR3: JEDEC Compliance for Maximum
183 * Self-Refresh Command Limit" found in AM335x Silicon Errata
184 * (Document SPRZ360F Revised November 2013) we must configure
185 * the self refresh delay timer to 0xA (8192 cycles) to avoid
186 * generating too many refresh command from the EMIF.
188 static void ti_emif_configure_sr_delay(struct ti_emif_data *emif_data)
190 writel(EMIF_POWER_MGMT_WAIT_SELF_REFRESH_8192_CYCLES,
191 (emif_data->pm_data.ti_emif_base_addr_virt +
192 EMIF_POWER_MANAGEMENT_CONTROL));
194 writel(EMIF_POWER_MGMT_WAIT_SELF_REFRESH_8192_CYCLES,
195 (emif_data->pm_data.ti_emif_base_addr_virt +
196 EMIF_POWER_MANAGEMENT_CTRL_SHDW));
200 * ti_emif_copy_pm_function_table - copy mapping of pm funcs in sram
201 * @sram_pool: pointer to struct gen_pool where dst resides
202 * @dst: void * to address that table should be copied
204 * Returns 0 if success other error code if table is not available
206 int ti_emif_copy_pm_function_table(struct gen_pool *sram_pool, void *dst)
208 void *copy_addr;
210 if (!emif_instance)
211 return -ENODEV;
213 copy_addr = sram_exec_copy(sram_pool, dst,
214 &emif_instance->pm_functions,
215 sizeof(emif_instance->pm_functions));
216 if (!copy_addr)
217 return -ENODEV;
219 return 0;
221 EXPORT_SYMBOL_GPL(ti_emif_copy_pm_function_table);
224 * ti_emif_get_mem_type - return type for memory type in use
226 * Returns memory type value read from EMIF or error code if fails
228 int ti_emif_get_mem_type(void)
230 unsigned long temp;
232 if (!emif_instance)
233 return -ENODEV;
235 temp = readl(emif_instance->pm_data.ti_emif_base_addr_virt +
236 EMIF_SDRAM_CONFIG);
238 temp = (temp & SDRAM_TYPE_MASK) >> SDRAM_TYPE_SHIFT;
239 return temp;
241 EXPORT_SYMBOL_GPL(ti_emif_get_mem_type);
243 static const struct of_device_id ti_emif_of_match[] = {
244 { .compatible = "ti,emif-am3352", .data =
245 (void *)EMIF_SRAM_AM33_REG_LAYOUT, },
246 { .compatible = "ti,emif-am4372", .data =
247 (void *)EMIF_SRAM_AM43_REG_LAYOUT, },
250 MODULE_DEVICE_TABLE(of, ti_emif_of_match);
252 #ifdef CONFIG_PM_SLEEP
253 static int ti_emif_resume(struct device *dev)
255 unsigned long tmp =
256 __raw_readl((void *)emif_instance->ti_emif_sram_virt);
259 * Check to see if what we are copying is already present in the
260 * first byte at the destination, only copy if it is not which
261 * indicates we have lost context and sram no longer contains
262 * the PM code
264 if (tmp != ti_emif_sram)
265 ti_emif_push_sram(dev, emif_instance);
267 return 0;
270 static int ti_emif_suspend(struct device *dev)
273 * The contents will be present in DDR hence no need to
274 * explicitly save
276 return 0;
278 #endif /* CONFIG_PM_SLEEP */
280 static int ti_emif_probe(struct platform_device *pdev)
282 int ret;
283 struct resource *res;
284 struct device *dev = &pdev->dev;
285 const struct of_device_id *match;
286 struct ti_emif_data *emif_data;
288 emif_data = devm_kzalloc(dev, sizeof(*emif_data), GFP_KERNEL);
289 if (!emif_data)
290 return -ENOMEM;
292 match = of_match_device(ti_emif_of_match, &pdev->dev);
293 if (!match)
294 return -ENODEV;
296 emif_data->pm_data.ti_emif_sram_config = (unsigned long)match->data;
298 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
299 emif_data->pm_data.ti_emif_base_addr_virt = devm_ioremap_resource(dev,
300 res);
301 if (IS_ERR(emif_data->pm_data.ti_emif_base_addr_virt)) {
302 ret = PTR_ERR(emif_data->pm_data.ti_emif_base_addr_virt);
303 return ret;
306 emif_data->pm_data.ti_emif_base_addr_phys = res->start;
308 ti_emif_configure_sr_delay(emif_data);
310 ret = ti_emif_alloc_sram(dev, emif_data);
311 if (ret)
312 return ret;
314 ret = ti_emif_push_sram(dev, emif_data);
315 if (ret)
316 goto fail_free_sram;
318 emif_instance = emif_data;
320 return 0;
322 fail_free_sram:
323 ti_emif_free_sram(emif_data);
325 return ret;
328 static int ti_emif_remove(struct platform_device *pdev)
330 struct ti_emif_data *emif_data = emif_instance;
332 emif_instance = NULL;
334 ti_emif_free_sram(emif_data);
336 return 0;
339 static const struct dev_pm_ops ti_emif_pm_ops = {
340 SET_SYSTEM_SLEEP_PM_OPS(ti_emif_suspend, ti_emif_resume)
343 static struct platform_driver ti_emif_driver = {
344 .probe = ti_emif_probe,
345 .remove = ti_emif_remove,
346 .driver = {
347 .name = KBUILD_MODNAME,
348 .of_match_table = of_match_ptr(ti_emif_of_match),
349 .pm = &ti_emif_pm_ops,
352 module_platform_driver(ti_emif_driver);
354 MODULE_AUTHOR("Dave Gerlach <d-gerlach@ti.com>");
355 MODULE_DESCRIPTION("Texas Instruments SRAM EMIF driver");
356 MODULE_LICENSE("GPL v2");