sddr2.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2014 Roy Spliet
  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: Roy Spliet <rspliet@eclipso.eu>
  23. * Ben Skeggs
  24. */
  25. #include "priv.h"
  26. #include "ram.h"
  27. struct ramxlat {
  28. int id;
  29. u8 enc;
  30. };
  31. static inline int
  32. ramxlat(const struct ramxlat *xlat, int id)
  33. {
  34. while (xlat->id >= 0) {
  35. if (xlat->id == id)
  36. return xlat->enc;
  37. xlat++;
  38. }
  39. return -EINVAL;
  40. }
  41. static const struct ramxlat
  42. ramddr2_cl[] = {
  43. { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 },
  44. /* The following are available in some, but not all DDR2 docs */
  45. { 7, 7 },
  46. { -1 }
  47. };
  48. static const struct ramxlat
  49. ramddr2_wr[] = {
  50. { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 4 }, { 6, 5 },
  51. /* The following are available in some, but not all DDR2 docs */
  52. { 7, 6 },
  53. { -1 }
  54. };
  55. int
  56. nvkm_sddr2_calc(struct nvkm_ram *ram)
  57. {
  58. int CL, WR, DLL = 0, ODT = 0;
  59. switch (ram->next->bios.timing_ver) {
  60. case 0x10:
  61. CL = ram->next->bios.timing_10_CL;
  62. WR = ram->next->bios.timing_10_WR;
  63. DLL = !ram->next->bios.ramcfg_DLLoff;
  64. ODT = ram->next->bios.timing_10_ODT & 3;
  65. break;
  66. case 0x20:
  67. CL = (ram->next->bios.timing[1] & 0x0000001f);
  68. WR = (ram->next->bios.timing[2] & 0x007f0000) >> 16;
  69. break;
  70. default:
  71. return -ENOSYS;
  72. }
  73. if (ram->next->bios.timing_ver == 0x20 ||
  74. ram->next->bios.ramcfg_timing == 0xff) {
  75. ODT = (ram->mr[1] & 0x004) >> 2 |
  76. (ram->mr[1] & 0x040) >> 5;
  77. }
  78. CL = ramxlat(ramddr2_cl, CL);
  79. WR = ramxlat(ramddr2_wr, WR);
  80. if (CL < 0 || WR < 0)
  81. return -EINVAL;
  82. ram->mr[0] &= ~0xf70;
  83. ram->mr[0] |= (WR & 0x07) << 9;
  84. ram->mr[0] |= (CL & 0x07) << 4;
  85. ram->mr[1] &= ~0x045;
  86. ram->mr[1] |= (ODT & 0x1) << 2;
  87. ram->mr[1] |= (ODT & 0x2) << 5;
  88. ram->mr[1] |= !DLL;
  89. return 0;
  90. }