i2c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2012 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
  23. */
  24. #include <subdev/bios.h>
  25. #include <subdev/bios/dcb.h>
  26. #include <subdev/bios/i2c.h>
  27. u16
  28. dcb_i2c_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
  29. {
  30. u16 i2c = 0x0000;
  31. u16 dcb = dcb_table(bios, ver, hdr, cnt, len);
  32. if (dcb) {
  33. if (*ver >= 0x15)
  34. i2c = nvbios_rd16(bios, dcb + 2);
  35. if (*ver >= 0x30)
  36. i2c = nvbios_rd16(bios, dcb + 4);
  37. }
  38. if (i2c && *ver >= 0x42) {
  39. nvkm_warn(&bios->subdev, "ccb %02x not supported\n", *ver);
  40. return 0x0000;
  41. }
  42. if (i2c && *ver >= 0x30) {
  43. *ver = nvbios_rd08(bios, i2c + 0);
  44. *hdr = nvbios_rd08(bios, i2c + 1);
  45. *cnt = nvbios_rd08(bios, i2c + 2);
  46. *len = nvbios_rd08(bios, i2c + 3);
  47. } else {
  48. *ver = *ver; /* use DCB version */
  49. *hdr = 0;
  50. *cnt = 16;
  51. *len = 4;
  52. }
  53. return i2c;
  54. }
  55. u16
  56. dcb_i2c_entry(struct nvkm_bios *bios, u8 idx, u8 *ver, u8 *len)
  57. {
  58. u8 hdr, cnt;
  59. u16 i2c = dcb_i2c_table(bios, ver, &hdr, &cnt, len);
  60. if (i2c && idx < cnt)
  61. return i2c + hdr + (idx * *len);
  62. return 0x0000;
  63. }
  64. int
  65. dcb_i2c_parse(struct nvkm_bios *bios, u8 idx, struct dcb_i2c_entry *info)
  66. {
  67. struct nvkm_subdev *subdev = &bios->subdev;
  68. u8 ver, len;
  69. u16 ent = dcb_i2c_entry(bios, idx, &ver, &len);
  70. if (ent) {
  71. if (ver >= 0x41) {
  72. u32 ent_value = nvbios_rd32(bios, ent);
  73. u8 i2c_port = (ent_value >> 0) & 0x1f;
  74. u8 dpaux_port = (ent_value >> 5) & 0x1f;
  75. /* value 0x1f means unused according to DCB 4.x spec */
  76. if (i2c_port == 0x1f && dpaux_port == 0x1f)
  77. info->type = DCB_I2C_UNUSED;
  78. else
  79. info->type = DCB_I2C_PMGR;
  80. } else
  81. if (ver >= 0x30) {
  82. info->type = nvbios_rd08(bios, ent + 0x03);
  83. } else {
  84. info->type = nvbios_rd08(bios, ent + 0x03) & 0x07;
  85. if (info->type == 0x07)
  86. info->type = DCB_I2C_UNUSED;
  87. }
  88. info->drive = DCB_I2C_UNUSED;
  89. info->sense = DCB_I2C_UNUSED;
  90. info->share = DCB_I2C_UNUSED;
  91. info->auxch = DCB_I2C_UNUSED;
  92. switch (info->type) {
  93. case DCB_I2C_NV04_BIT:
  94. info->drive = nvbios_rd08(bios, ent + 0);
  95. info->sense = nvbios_rd08(bios, ent + 1);
  96. return 0;
  97. case DCB_I2C_NV4E_BIT:
  98. info->drive = nvbios_rd08(bios, ent + 1);
  99. return 0;
  100. case DCB_I2C_NVIO_BIT:
  101. info->drive = nvbios_rd08(bios, ent + 0) & 0x0f;
  102. if (nvbios_rd08(bios, ent + 1) & 0x01)
  103. info->share = nvbios_rd08(bios, ent + 1) >> 1;
  104. return 0;
  105. case DCB_I2C_NVIO_AUX:
  106. info->auxch = nvbios_rd08(bios, ent + 0) & 0x0f;
  107. if (nvbios_rd08(bios, ent + 1) & 0x01)
  108. info->share = info->auxch;
  109. return 0;
  110. case DCB_I2C_PMGR:
  111. info->drive = (nvbios_rd16(bios, ent + 0) & 0x01f) >> 0;
  112. if (info->drive == 0x1f)
  113. info->drive = DCB_I2C_UNUSED;
  114. info->auxch = (nvbios_rd16(bios, ent + 0) & 0x3e0) >> 5;
  115. if (info->auxch == 0x1f)
  116. info->auxch = DCB_I2C_UNUSED;
  117. info->share = info->auxch;
  118. return 0;
  119. case DCB_I2C_UNUSED:
  120. return 0;
  121. default:
  122. nvkm_warn(subdev, "unknown i2c type %d\n", info->type);
  123. info->type = DCB_I2C_UNUSED;
  124. return 0;
  125. }
  126. }
  127. if (bios->bmp_offset && idx < 2) {
  128. /* BMP (from v4.0 has i2c info in the structure, it's in a
  129. * fixed location on earlier VBIOS
  130. */
  131. if (nvbios_rd08(bios, bios->bmp_offset + 5) < 4)
  132. ent = 0x0048;
  133. else
  134. ent = 0x0036 + bios->bmp_offset;
  135. if (idx == 0) {
  136. info->drive = nvbios_rd08(bios, ent + 4);
  137. if (!info->drive) info->drive = 0x3f;
  138. info->sense = nvbios_rd08(bios, ent + 5);
  139. if (!info->sense) info->sense = 0x3e;
  140. } else
  141. if (idx == 1) {
  142. info->drive = nvbios_rd08(bios, ent + 6);
  143. if (!info->drive) info->drive = 0x37;
  144. info->sense = nvbios_rd08(bios, ent + 7);
  145. if (!info->sense) info->sense = 0x36;
  146. }
  147. info->type = DCB_I2C_NV04_BIT;
  148. info->share = DCB_I2C_UNUSED;
  149. return 0;
  150. }
  151. return -ENOENT;
  152. }