emif.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/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>
  24. #include <memory/jedec_ddr.h>
  25. #include "emif.h"
  26. /**
  27. * struct emif_data - Per device static data for driver's use
  28. * @duplicate: Whether the DDR devices attached to this EMIF
  29. * instance are exactly same as that on EMIF1. In
  30. * this case we can save some memory and processing
  31. * @temperature_level: Maximum temperature of LPDDR2 devices attached
  32. * to this EMIF - read from MR4 register. If there
  33. * are two devices attached to this EMIF, this
  34. * value is the maximum of the two temperature
  35. * levels.
  36. * @node: node in the device list
  37. * @base: base address of memory-mapped IO registers.
  38. * @dev: device pointer.
  39. * @plat_data: Pointer to saved platform data.
  40. */
  41. struct emif_data {
  42. u8 duplicate;
  43. u8 temperature_level;
  44. struct list_head node;
  45. void __iomem *base;
  46. struct device *dev;
  47. struct emif_platform_data *plat_data;
  48. };
  49. static struct emif_data *emif1;
  50. static LIST_HEAD(device_list);
  51. static void get_default_timings(struct emif_data *emif)
  52. {
  53. struct emif_platform_data *pd = emif->plat_data;
  54. pd->timings = lpddr2_jedec_timings;
  55. pd->timings_arr_size = ARRAY_SIZE(lpddr2_jedec_timings);
  56. dev_warn(emif->dev, "%s: using default timings\n", __func__);
  57. }
  58. static int is_dev_data_valid(u32 type, u32 density, u32 io_width, u32 phy_type,
  59. u32 ip_rev, struct device *dev)
  60. {
  61. int valid;
  62. valid = (type == DDR_TYPE_LPDDR2_S4 ||
  63. type == DDR_TYPE_LPDDR2_S2)
  64. && (density >= DDR_DENSITY_64Mb
  65. && density <= DDR_DENSITY_8Gb)
  66. && (io_width >= DDR_IO_WIDTH_8
  67. && io_width <= DDR_IO_WIDTH_32);
  68. /* Combinations of EMIF and PHY revisions that we support today */
  69. switch (ip_rev) {
  70. case EMIF_4D:
  71. valid = valid && (phy_type == EMIF_PHY_TYPE_ATTILAPHY);
  72. break;
  73. case EMIF_4D5:
  74. valid = valid && (phy_type == EMIF_PHY_TYPE_INTELLIPHY);
  75. break;
  76. default:
  77. valid = 0;
  78. }
  79. if (!valid)
  80. dev_err(dev, "%s: invalid DDR details\n", __func__);
  81. return valid;
  82. }
  83. static int is_custom_config_valid(struct emif_custom_configs *cust_cfgs,
  84. struct device *dev)
  85. {
  86. int valid = 1;
  87. if ((cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE) &&
  88. (cust_cfgs->lpmode != EMIF_LP_MODE_DISABLE))
  89. valid = cust_cfgs->lpmode_freq_threshold &&
  90. cust_cfgs->lpmode_timeout_performance &&
  91. cust_cfgs->lpmode_timeout_power;
  92. if (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL)
  93. valid = valid && cust_cfgs->temp_alert_poll_interval_ms;
  94. if (!valid)
  95. dev_warn(dev, "%s: invalid custom configs\n", __func__);
  96. return valid;
  97. }
  98. static struct emif_data *__init_or_module get_device_details(
  99. struct platform_device *pdev)
  100. {
  101. u32 size;
  102. struct emif_data *emif = NULL;
  103. struct ddr_device_info *dev_info;
  104. struct emif_custom_configs *cust_cfgs;
  105. struct emif_platform_data *pd;
  106. struct device *dev;
  107. void *temp;
  108. pd = pdev->dev.platform_data;
  109. dev = &pdev->dev;
  110. if (!(pd && pd->device_info && is_dev_data_valid(pd->device_info->type,
  111. pd->device_info->density, pd->device_info->io_width,
  112. pd->phy_type, pd->ip_rev, dev))) {
  113. dev_err(dev, "%s: invalid device data\n", __func__);
  114. goto error;
  115. }
  116. emif = devm_kzalloc(dev, sizeof(*emif), GFP_KERNEL);
  117. temp = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  118. dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
  119. if (!emif || !pd || !dev_info) {
  120. dev_err(dev, "%s:%d: allocation error\n", __func__, __LINE__);
  121. goto error;
  122. }
  123. memcpy(temp, pd, sizeof(*pd));
  124. pd = temp;
  125. memcpy(dev_info, pd->device_info, sizeof(*dev_info));
  126. pd->device_info = dev_info;
  127. emif->plat_data = pd;
  128. emif->dev = dev;
  129. emif->temperature_level = SDRAM_TEMP_NOMINAL;
  130. /*
  131. * For EMIF instances other than EMIF1 see if the devices connected
  132. * are exactly same as on EMIF1(which is typically the case). If so,
  133. * mark it as a duplicate of EMIF1 and skip copying timings data.
  134. * This will save some memory and some computation later.
  135. */
  136. emif->duplicate = emif1 && (memcmp(dev_info,
  137. emif1->plat_data->device_info,
  138. sizeof(struct ddr_device_info)) == 0);
  139. if (emif->duplicate) {
  140. pd->timings = NULL;
  141. pd->min_tck = NULL;
  142. goto out;
  143. } else if (emif1) {
  144. dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
  145. __func__);
  146. }
  147. /*
  148. * Copy custom configs - ignore allocation error, if any, as
  149. * custom_configs is not very critical
  150. */
  151. cust_cfgs = pd->custom_configs;
  152. if (cust_cfgs && is_custom_config_valid(cust_cfgs, dev)) {
  153. temp = devm_kzalloc(dev, sizeof(*cust_cfgs), GFP_KERNEL);
  154. if (temp)
  155. memcpy(temp, cust_cfgs, sizeof(*cust_cfgs));
  156. else
  157. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  158. __LINE__);
  159. pd->custom_configs = temp;
  160. }
  161. /*
  162. * Copy timings and min-tck values from platform data. If it is not
  163. * available or if memory allocation fails, use JEDEC defaults
  164. */
  165. size = sizeof(struct lpddr2_timings) * pd->timings_arr_size;
  166. if (pd->timings) {
  167. temp = devm_kzalloc(dev, size, GFP_KERNEL);
  168. if (temp) {
  169. memcpy(temp, pd->timings, sizeof(*pd->timings));
  170. pd->timings = temp;
  171. } else {
  172. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  173. __LINE__);
  174. get_default_timings(emif);
  175. }
  176. } else {
  177. get_default_timings(emif);
  178. }
  179. if (pd->min_tck) {
  180. temp = devm_kzalloc(dev, sizeof(*pd->min_tck), GFP_KERNEL);
  181. if (temp) {
  182. memcpy(temp, pd->min_tck, sizeof(*pd->min_tck));
  183. pd->min_tck = temp;
  184. } else {
  185. dev_warn(dev, "%s:%d: allocation error\n", __func__,
  186. __LINE__);
  187. pd->min_tck = &lpddr2_jedec_min_tck;
  188. }
  189. } else {
  190. pd->min_tck = &lpddr2_jedec_min_tck;
  191. }
  192. out:
  193. return emif;
  194. error:
  195. return NULL;
  196. }
  197. static int __init_or_module emif_probe(struct platform_device *pdev)
  198. {
  199. struct emif_data *emif;
  200. struct resource *res;
  201. emif = get_device_details(pdev);
  202. if (!emif) {
  203. pr_err("%s: error getting device data\n", __func__);
  204. goto error;
  205. }
  206. if (!emif1)
  207. emif1 = emif;
  208. list_add(&emif->node, &device_list);
  209. /* Save pointers to each other in emif and device structures */
  210. emif->dev = &pdev->dev;
  211. platform_set_drvdata(pdev, emif);
  212. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  213. if (!res) {
  214. dev_err(emif->dev, "%s: error getting memory resource\n",
  215. __func__);
  216. goto error;
  217. }
  218. emif->base = devm_request_and_ioremap(emif->dev, res);
  219. if (!emif->base) {
  220. dev_err(emif->dev, "%s: devm_request_and_ioremap() failed\n",
  221. __func__);
  222. goto error;
  223. }
  224. dev_info(&pdev->dev, "%s: device configured with addr = %p\n",
  225. __func__, emif->base);
  226. return 0;
  227. error:
  228. return -ENODEV;
  229. }
  230. static struct platform_driver emif_driver = {
  231. .driver = {
  232. .name = "emif",
  233. },
  234. };
  235. static int __init_or_module emif_register(void)
  236. {
  237. return platform_driver_probe(&emif_driver, emif_probe);
  238. }
  239. static void __exit emif_unregister(void)
  240. {
  241. platform_driver_unregister(&emif_driver);
  242. }
  243. module_init(emif_register);
  244. module_exit(emif_unregister);
  245. MODULE_DESCRIPTION("TI EMIF SDRAM Controller Driver");
  246. MODULE_LICENSE("GPL");
  247. MODULE_ALIAS("platform:emif");
  248. MODULE_AUTHOR("Texas Instruments Inc");