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. /**
  19. * of_get_min_tck() - extract min timing values for ddr
  20. * @np: pointer to ddr device tree node
  21. * @device: device requesting for min timing values
  22. *
  23. * Populates the lpddr2_min_tck structure by extracting data
  24. * from device tree node. Returns a pointer to the populated
  25. * structure. If any error in populating the structure, returns
  26. * default min timings provided by JEDEC.
  27. */
  28. const struct lpddr2_min_tck *of_get_min_tck(struct device_node *np,
  29. struct device *dev)
  30. {
  31. int ret = 0;
  32. struct lpddr2_min_tck *min;
  33. min = devm_kzalloc(dev, sizeof(*min), GFP_KERNEL);
  34. if (!min)
  35. goto default_min_tck;
  36. ret |= of_property_read_u32(np, "tRPab-min-tck", &min->tRPab);
  37. ret |= of_property_read_u32(np, "tRCD-min-tck", &min->tRCD);
  38. ret |= of_property_read_u32(np, "tWR-min-tck", &min->tWR);
  39. ret |= of_property_read_u32(np, "tRASmin-min-tck", &min->tRASmin);
  40. ret |= of_property_read_u32(np, "tRRD-min-tck", &min->tRRD);
  41. ret |= of_property_read_u32(np, "tWTR-min-tck", &min->tWTR);
  42. ret |= of_property_read_u32(np, "tXP-min-tck", &min->tXP);
  43. ret |= of_property_read_u32(np, "tRTP-min-tck", &min->tRTP);
  44. ret |= of_property_read_u32(np, "tCKE-min-tck", &min->tCKE);
  45. ret |= of_property_read_u32(np, "tCKESR-min-tck", &min->tCKESR);
  46. ret |= of_property_read_u32(np, "tFAW-min-tck", &min->tFAW);
  47. if (ret) {
  48. devm_kfree(dev, min);
  49. goto default_min_tck;
  50. }
  51. return min;
  52. default_min_tck:
  53. dev_warn(dev, "%s: using default min-tck values\n", __func__);
  54. return &lpddr2_jedec_min_tck;
  55. }
  56. EXPORT_SYMBOL(of_get_min_tck);
  57. static int of_do_get_timings(struct device_node *np,
  58. struct lpddr2_timings *tim)
  59. {
  60. int ret;
  61. ret = of_property_read_u32(np, "max-freq", &tim->max_freq);
  62. ret |= of_property_read_u32(np, "min-freq", &tim->min_freq);
  63. ret |= of_property_read_u32(np, "tRPab", &tim->tRPab);
  64. ret |= of_property_read_u32(np, "tRCD", &tim->tRCD);
  65. ret |= of_property_read_u32(np, "tWR", &tim->tWR);
  66. ret |= of_property_read_u32(np, "tRAS-min", &tim->tRAS_min);
  67. ret |= of_property_read_u32(np, "tRRD", &tim->tRRD);
  68. ret |= of_property_read_u32(np, "tWTR", &tim->tWTR);
  69. ret |= of_property_read_u32(np, "tXP", &tim->tXP);
  70. ret |= of_property_read_u32(np, "tRTP", &tim->tRTP);
  71. ret |= of_property_read_u32(np, "tCKESR", &tim->tCKESR);
  72. ret |= of_property_read_u32(np, "tDQSCK-max", &tim->tDQSCK_max);
  73. ret |= of_property_read_u32(np, "tFAW", &tim->tFAW);
  74. ret |= of_property_read_u32(np, "tZQCS", &tim->tZQCS);
  75. ret |= of_property_read_u32(np, "tZQCL", &tim->tZQCL);
  76. ret |= of_property_read_u32(np, "tZQinit", &tim->tZQinit);
  77. ret |= of_property_read_u32(np, "tRAS-max-ns", &tim->tRAS_max_ns);
  78. ret |= of_property_read_u32(np, "tDQSCK-max-derated",
  79. &tim->tDQSCK_max_derated);
  80. return ret;
  81. }
  82. /**
  83. * of_get_ddr_timings() - extracts the ddr timings and updates no of
  84. * frequencies available.
  85. * @np_ddr: Pointer to ddr device tree node
  86. * @dev: Device requesting for ddr timings
  87. * @device_type: Type of ddr(LPDDR2 S2/S4)
  88. * @nr_frequencies: No of frequencies available for ddr
  89. * (updated by this function)
  90. *
  91. * Populates lpddr2_timings structure by extracting data from device
  92. * tree node. Returns pointer to populated structure. If any error
  93. * while populating, returns default timings provided by JEDEC.
  94. */
  95. const struct lpddr2_timings *of_get_ddr_timings(struct device_node *np_ddr,
  96. struct device *dev, u32 device_type, u32 *nr_frequencies)
  97. {
  98. struct lpddr2_timings *timings = NULL;
  99. u32 arr_sz = 0, i = 0;
  100. struct device_node *np_tim;
  101. char *tim_compat;
  102. switch (device_type) {
  103. case DDR_TYPE_LPDDR2_S2:
  104. case DDR_TYPE_LPDDR2_S4:
  105. tim_compat = "jedec,lpddr2-timings";
  106. break;
  107. default:
  108. dev_warn(dev, "%s: un-supported memory type\n", __func__);
  109. }
  110. for_each_child_of_node(np_ddr, np_tim)
  111. if (of_device_is_compatible(np_tim, tim_compat))
  112. arr_sz++;
  113. if (arr_sz)
  114. timings = devm_kzalloc(dev, sizeof(*timings) * arr_sz,
  115. GFP_KERNEL);
  116. if (!timings)
  117. goto default_timings;
  118. for_each_child_of_node(np_ddr, np_tim) {
  119. if (of_device_is_compatible(np_tim, tim_compat)) {
  120. if (of_do_get_timings(np_tim, &timings[i])) {
  121. devm_kfree(dev, timings);
  122. goto default_timings;
  123. }
  124. i++;
  125. }
  126. }
  127. *nr_frequencies = arr_sz;
  128. return timings;
  129. default_timings:
  130. dev_warn(dev, "%s: using default timings\n", __func__);
  131. *nr_frequencies = ARRAY_SIZE(lpddr2_jedec_timings);
  132. return lpddr2_jedec_timings;
  133. }
  134. EXPORT_SYMBOL(of_get_ddr_timings);