of_memory.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * OpenFirmware helpers for memory drivers
  3. *
  4. * Copyright (C) 2012 Texas Instruments, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/list.h>
  14. #include <linux/of.h>
  15. #include <linux/gfp.h>
  16. #include <memory/jedec_ddr.h>
  17. #include <linux/export.h>
  18. #include "of_memory.h"
  19. /**
  20. * of_get_min_tck() - extract min timing values for ddr
  21. * @np: pointer to ddr device tree node
  22. * @device: device requesting for min timing values
  23. *
  24. * Populates the lpddr2_min_tck structure by extracting data
  25. * from device tree node. Returns a pointer to the populated
  26. * structure. If any error in populating the structure, returns
  27. * default min timings provided by JEDEC.
  28. */
  29. const struct lpddr2_min_tck *of_get_min_tck(struct device_node *np,
  30. struct device *dev)
  31. {
  32. int ret = 0;
  33. struct lpddr2_min_tck *min;
  34. min = devm_kzalloc(dev, sizeof(*min), GFP_KERNEL);
  35. if (!min)
  36. goto default_min_tck;
  37. ret |= of_property_read_u32(np, "tRPab-min-tck", &min->tRPab);
  38. ret |= of_property_read_u32(np, "tRCD-min-tck", &min->tRCD);
  39. ret |= of_property_read_u32(np, "tWR-min-tck", &min->tWR);
  40. ret |= of_property_read_u32(np, "tRASmin-min-tck", &min->tRASmin);
  41. ret |= of_property_read_u32(np, "tRRD-min-tck", &min->tRRD);
  42. ret |= of_property_read_u32(np, "tWTR-min-tck", &min->tWTR);
  43. ret |= of_property_read_u32(np, "tXP-min-tck", &min->tXP);
  44. ret |= of_property_read_u32(np, "tRTP-min-tck", &min->tRTP);
  45. ret |= of_property_read_u32(np, "tCKE-min-tck", &min->tCKE);
  46. ret |= of_property_read_u32(np, "tCKESR-min-tck", &min->tCKESR);
  47. ret |= of_property_read_u32(np, "tFAW-min-tck", &min->tFAW);
  48. if (ret) {
  49. devm_kfree(dev, min);
  50. goto default_min_tck;
  51. }
  52. return min;
  53. default_min_tck:
  54. dev_warn(dev, "%s: using default min-tck values\n", __func__);
  55. return &lpddr2_jedec_min_tck;
  56. }
  57. EXPORT_SYMBOL(of_get_min_tck);
  58. static int of_do_get_timings(struct device_node *np,
  59. struct lpddr2_timings *tim)
  60. {
  61. int ret;
  62. ret = of_property_read_u32(np, "max-freq", &tim->max_freq);
  63. ret |= of_property_read_u32(np, "min-freq", &tim->min_freq);
  64. ret |= of_property_read_u32(np, "tRPab", &tim->tRPab);
  65. ret |= of_property_read_u32(np, "tRCD", &tim->tRCD);
  66. ret |= of_property_read_u32(np, "tWR", &tim->tWR);
  67. ret |= of_property_read_u32(np, "tRAS-min", &tim->tRAS_min);
  68. ret |= of_property_read_u32(np, "tRRD", &tim->tRRD);
  69. ret |= of_property_read_u32(np, "tWTR", &tim->tWTR);
  70. ret |= of_property_read_u32(np, "tXP", &tim->tXP);
  71. ret |= of_property_read_u32(np, "tRTP", &tim->tRTP);
  72. ret |= of_property_read_u32(np, "tCKESR", &tim->tCKESR);
  73. ret |= of_property_read_u32(np, "tDQSCK-max", &tim->tDQSCK_max);
  74. ret |= of_property_read_u32(np, "tFAW", &tim->tFAW);
  75. ret |= of_property_read_u32(np, "tZQCS", &tim->tZQCS);
  76. ret |= of_property_read_u32(np, "tZQCL", &tim->tZQCL);
  77. ret |= of_property_read_u32(np, "tZQinit", &tim->tZQinit);
  78. ret |= of_property_read_u32(np, "tRAS-max-ns", &tim->tRAS_max_ns);
  79. ret |= of_property_read_u32(np, "tDQSCK-max-derated",
  80. &tim->tDQSCK_max_derated);
  81. return ret;
  82. }
  83. /**
  84. * of_get_ddr_timings() - extracts the ddr timings and updates no of
  85. * frequencies available.
  86. * @np_ddr: Pointer to ddr device tree node
  87. * @dev: Device requesting for ddr timings
  88. * @device_type: Type of ddr(LPDDR2 S2/S4)
  89. * @nr_frequencies: No of frequencies available for ddr
  90. * (updated by this function)
  91. *
  92. * Populates lpddr2_timings structure by extracting data from device
  93. * tree node. Returns pointer to populated structure. If any error
  94. * while populating, returns default timings provided by JEDEC.
  95. */
  96. const struct lpddr2_timings *of_get_ddr_timings(struct device_node *np_ddr,
  97. struct device *dev, u32 device_type, u32 *nr_frequencies)
  98. {
  99. struct lpddr2_timings *timings = NULL;
  100. u32 arr_sz = 0, i = 0;
  101. struct device_node *np_tim;
  102. char *tim_compat = NULL;
  103. switch (device_type) {
  104. case DDR_TYPE_LPDDR2_S2:
  105. case DDR_TYPE_LPDDR2_S4:
  106. tim_compat = "jedec,lpddr2-timings";
  107. break;
  108. default:
  109. dev_warn(dev, "%s: un-supported memory type\n", __func__);
  110. }
  111. for_each_child_of_node(np_ddr, np_tim)
  112. if (of_device_is_compatible(np_tim, tim_compat))
  113. arr_sz++;
  114. if (arr_sz)
  115. timings = devm_kzalloc(dev, sizeof(*timings) * arr_sz,
  116. GFP_KERNEL);
  117. if (!timings)
  118. goto default_timings;
  119. for_each_child_of_node(np_ddr, np_tim) {
  120. if (of_device_is_compatible(np_tim, tim_compat)) {
  121. if (of_do_get_timings(np_tim, &timings[i])) {
  122. devm_kfree(dev, timings);
  123. goto default_timings;
  124. }
  125. i++;
  126. }
  127. }
  128. *nr_frequencies = arr_sz;
  129. return timings;
  130. default_timings:
  131. dev_warn(dev, "%s: using default timings\n", __func__);
  132. *nr_frequencies = ARRAY_SIZE(lpddr2_jedec_timings);
  133. return lpddr2_jedec_timings;
  134. }
  135. EXPORT_SYMBOL(of_get_ddr_timings);