sddr3.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2013 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs <bskeggs@redhat.com>
  23. * Roy Spliet <rspliet@eclipso.eu>
  24. */
  25. #include "priv.h"
  26. struct ramxlat {
  27. int id;
  28. u8 enc;
  29. };
  30. static inline int
  31. ramxlat(const struct ramxlat *xlat, int id)
  32. {
  33. while (xlat->id >= 0) {
  34. if (xlat->id == id)
  35. return xlat->enc;
  36. xlat++;
  37. }
  38. return -EINVAL;
  39. }
  40. static const struct ramxlat
  41. ramddr3_cl[] = {
  42. { 5, 2 }, { 6, 4 }, { 7, 6 }, { 8, 8 }, { 9, 10 }, { 10, 12 },
  43. { 11, 14 },
  44. /* the below are mentioned in some, but not all, ddr3 docs */
  45. { 12, 1 }, { 13, 3 }, { 14, 5 },
  46. { -1 }
  47. };
  48. static const struct ramxlat
  49. ramddr3_wr[] = {
  50. { 5, 1 }, { 6, 2 }, { 7, 3 }, { 8, 4 }, { 10, 5 }, { 12, 6 },
  51. /* the below are mentioned in some, but not all, ddr3 docs */
  52. { 14, 7 }, { 15, 7 }, { 16, 0 },
  53. { -1 }
  54. };
  55. static const struct ramxlat
  56. ramddr3_cwl[] = {
  57. { 5, 0 }, { 6, 1 }, { 7, 2 }, { 8, 3 },
  58. /* the below are mentioned in some, but not all, ddr3 docs */
  59. { 9, 4 }, { 10, 5 },
  60. { -1 }
  61. };
  62. int
  63. nvkm_sddr3_calc(struct nvkm_ram *ram)
  64. {
  65. int CWL, CL, WR, DLL = 0, ODT = 0;
  66. switch (ram->next->bios.timing_ver) {
  67. case 0x10:
  68. if (ram->next->bios.timing_hdr < 0x17) {
  69. /* XXX: NV50: Get CWL from the timing register */
  70. return -ENOSYS;
  71. }
  72. CWL = ram->next->bios.timing_10_CWL;
  73. CL = ram->next->bios.timing_10_CL;
  74. WR = ram->next->bios.timing_10_WR;
  75. DLL = !ram->next->bios.ramcfg_DLLoff;
  76. ODT = ram->next->bios.timing_10_ODT;
  77. break;
  78. case 0x20:
  79. CWL = (ram->next->bios.timing[1] & 0x00000f80) >> 7;
  80. CL = (ram->next->bios.timing[1] & 0x0000001f) >> 0;
  81. WR = (ram->next->bios.timing[2] & 0x007f0000) >> 16;
  82. /* XXX: Get these values from the VBIOS instead */
  83. DLL = !(ram->mr[1] & 0x1);
  84. ODT = (ram->mr[1] & 0x004) >> 2 |
  85. (ram->mr[1] & 0x040) >> 5 |
  86. (ram->mr[1] & 0x200) >> 7;
  87. break;
  88. default:
  89. return -ENOSYS;
  90. }
  91. CWL = ramxlat(ramddr3_cwl, CWL);
  92. CL = ramxlat(ramddr3_cl, CL);
  93. WR = ramxlat(ramddr3_wr, WR);
  94. if (CL < 0 || CWL < 0 || WR < 0)
  95. return -EINVAL;
  96. ram->mr[0] &= ~0xf74;
  97. ram->mr[0] |= (WR & 0x07) << 9;
  98. ram->mr[0] |= (CL & 0x0e) << 3;
  99. ram->mr[0] |= (CL & 0x01) << 2;
  100. ram->mr[1] &= ~0x245;
  101. ram->mr[1] |= (ODT & 0x1) << 2;
  102. ram->mr[1] |= (ODT & 0x2) << 5;
  103. ram->mr[1] |= (ODT & 0x4) << 7;
  104. ram->mr[1] |= !DLL;
  105. ram->mr[2] &= ~0x038;
  106. ram->mr[2] |= (CWL & 0x07) << 3;
  107. return 0;
  108. }