eeprom.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Thunderbolt Cactus Ridge driver - eeprom access
  3. *
  4. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  5. */
  6. #include <linux/crc32.h>
  7. #include <linux/slab.h>
  8. #include "tb.h"
  9. /**
  10. * tb_eeprom_ctl_write() - write control word
  11. */
  12. static int tb_eeprom_ctl_write(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
  13. {
  14. return tb_sw_write(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
  15. }
  16. /**
  17. * tb_eeprom_ctl_write() - read control word
  18. */
  19. static int tb_eeprom_ctl_read(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
  20. {
  21. return tb_sw_read(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
  22. }
  23. enum tb_eeprom_transfer {
  24. TB_EEPROM_IN,
  25. TB_EEPROM_OUT,
  26. };
  27. /**
  28. * tb_eeprom_active - enable rom access
  29. *
  30. * WARNING: Always disable access after usage. Otherwise the controller will
  31. * fail to reprobe.
  32. */
  33. static int tb_eeprom_active(struct tb_switch *sw, bool enable)
  34. {
  35. struct tb_eeprom_ctl ctl;
  36. int res = tb_eeprom_ctl_read(sw, &ctl);
  37. if (res)
  38. return res;
  39. if (enable) {
  40. ctl.access_high = 1;
  41. res = tb_eeprom_ctl_write(sw, &ctl);
  42. if (res)
  43. return res;
  44. ctl.access_low = 0;
  45. return tb_eeprom_ctl_write(sw, &ctl);
  46. } else {
  47. ctl.access_low = 1;
  48. res = tb_eeprom_ctl_write(sw, &ctl);
  49. if (res)
  50. return res;
  51. ctl.access_high = 0;
  52. return tb_eeprom_ctl_write(sw, &ctl);
  53. }
  54. }
  55. /**
  56. * tb_eeprom_transfer - transfer one bit
  57. *
  58. * If TB_EEPROM_IN is passed, then the bit can be retrieved from ctl->data_in.
  59. * If TB_EEPROM_OUT is passed, then ctl->data_out will be written.
  60. */
  61. static int tb_eeprom_transfer(struct tb_switch *sw, struct tb_eeprom_ctl *ctl,
  62. enum tb_eeprom_transfer direction)
  63. {
  64. int res;
  65. if (direction == TB_EEPROM_OUT) {
  66. res = tb_eeprom_ctl_write(sw, ctl);
  67. if (res)
  68. return res;
  69. }
  70. ctl->clock = 1;
  71. res = tb_eeprom_ctl_write(sw, ctl);
  72. if (res)
  73. return res;
  74. if (direction == TB_EEPROM_IN) {
  75. res = tb_eeprom_ctl_read(sw, ctl);
  76. if (res)
  77. return res;
  78. }
  79. ctl->clock = 0;
  80. return tb_eeprom_ctl_write(sw, ctl);
  81. }
  82. /**
  83. * tb_eeprom_out - write one byte to the bus
  84. */
  85. static int tb_eeprom_out(struct tb_switch *sw, u8 val)
  86. {
  87. struct tb_eeprom_ctl ctl;
  88. int i;
  89. int res = tb_eeprom_ctl_read(sw, &ctl);
  90. if (res)
  91. return res;
  92. for (i = 0; i < 8; i++) {
  93. ctl.data_out = val & 0x80;
  94. res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_OUT);
  95. if (res)
  96. return res;
  97. val <<= 1;
  98. }
  99. return 0;
  100. }
  101. /**
  102. * tb_eeprom_in - read one byte from the bus
  103. */
  104. static int tb_eeprom_in(struct tb_switch *sw, u8 *val)
  105. {
  106. struct tb_eeprom_ctl ctl;
  107. int i;
  108. int res = tb_eeprom_ctl_read(sw, &ctl);
  109. if (res)
  110. return res;
  111. *val = 0;
  112. for (i = 0; i < 8; i++) {
  113. *val <<= 1;
  114. res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_IN);
  115. if (res)
  116. return res;
  117. *val |= ctl.data_in;
  118. }
  119. return 0;
  120. }
  121. /**
  122. * tb_eeprom_read_n - read count bytes from offset into val
  123. */
  124. static int tb_eeprom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
  125. size_t count)
  126. {
  127. int i, res;
  128. res = tb_eeprom_active(sw, true);
  129. if (res)
  130. return res;
  131. res = tb_eeprom_out(sw, 3);
  132. if (res)
  133. return res;
  134. res = tb_eeprom_out(sw, offset >> 8);
  135. if (res)
  136. return res;
  137. res = tb_eeprom_out(sw, offset);
  138. if (res)
  139. return res;
  140. for (i = 0; i < count; i++) {
  141. res = tb_eeprom_in(sw, val + i);
  142. if (res)
  143. return res;
  144. }
  145. return tb_eeprom_active(sw, false);
  146. }
  147. static u8 tb_crc8(u8 *data, int len)
  148. {
  149. int i, j;
  150. u8 val = 0xff;
  151. for (i = 0; i < len; i++) {
  152. val ^= data[i];
  153. for (j = 0; j < 8; j++)
  154. val = (val << 1) ^ ((val & 0x80) ? 7 : 0);
  155. }
  156. return val;
  157. }
  158. static u32 tb_crc32(void *data, size_t len)
  159. {
  160. return ~__crc32c_le(~0, data, len);
  161. }
  162. #define TB_DROM_DATA_START 13
  163. struct tb_drom_header {
  164. /* BYTE 0 */
  165. u8 uid_crc8; /* checksum for uid */
  166. /* BYTES 1-8 */
  167. u64 uid;
  168. /* BYTES 9-12 */
  169. u32 data_crc32; /* checksum for data_len bytes starting at byte 13 */
  170. /* BYTE 13 */
  171. u8 device_rom_revision; /* should be <= 1 */
  172. u16 data_len:10;
  173. u8 __unknown1:6;
  174. /* BYTES 16-21 */
  175. u16 vendor_id;
  176. u16 model_id;
  177. u8 model_rev;
  178. u8 eeprom_rev;
  179. } __packed;
  180. enum tb_drom_entry_type {
  181. /* force unsigned to prevent "one-bit signed bitfield" warning */
  182. TB_DROM_ENTRY_GENERIC = 0U,
  183. TB_DROM_ENTRY_PORT,
  184. };
  185. struct tb_drom_entry_header {
  186. u8 len;
  187. u8 index:6;
  188. bool port_disabled:1; /* only valid if type is TB_DROM_ENTRY_PORT */
  189. enum tb_drom_entry_type type:1;
  190. } __packed;
  191. struct tb_drom_entry_port {
  192. /* BYTES 0-1 */
  193. struct tb_drom_entry_header header;
  194. /* BYTE 2 */
  195. u8 dual_link_port_rid:4;
  196. u8 link_nr:1;
  197. u8 unknown1:2;
  198. bool has_dual_link_port:1;
  199. /* BYTE 3 */
  200. u8 dual_link_port_nr:6;
  201. u8 unknown2:2;
  202. /* BYTES 4 - 5 TODO decode */
  203. u8 micro2:4;
  204. u8 micro1:4;
  205. u8 micro3;
  206. /* BYTES 5-6, TODO: verify (find hardware that has these set) */
  207. u8 peer_port_rid:4;
  208. u8 unknown3:3;
  209. bool has_peer_port:1;
  210. u8 peer_port_nr:6;
  211. u8 unknown4:2;
  212. } __packed;
  213. /**
  214. * tb_eeprom_get_drom_offset - get drom offset within eeprom
  215. */
  216. static int tb_eeprom_get_drom_offset(struct tb_switch *sw, u16 *offset)
  217. {
  218. struct tb_cap_plug_events cap;
  219. int res;
  220. if (!sw->cap_plug_events) {
  221. tb_sw_warn(sw, "no TB_CAP_PLUG_EVENTS, cannot read eeprom\n");
  222. return -ENOSYS;
  223. }
  224. res = tb_sw_read(sw, &cap, TB_CFG_SWITCH, sw->cap_plug_events,
  225. sizeof(cap) / 4);
  226. if (res)
  227. return res;
  228. if (!cap.eeprom_ctl.present || cap.eeprom_ctl.not_present) {
  229. tb_sw_warn(sw, "no NVM\n");
  230. return -ENOSYS;
  231. }
  232. if (cap.drom_offset > 0xffff) {
  233. tb_sw_warn(sw, "drom offset is larger than 0xffff: %#x\n",
  234. cap.drom_offset);
  235. return -ENXIO;
  236. }
  237. *offset = cap.drom_offset;
  238. return 0;
  239. }
  240. /**
  241. * tb_drom_read_uid_only - read uid directly from drom
  242. *
  243. * Does not use the cached copy in sw->drom. Used during resume to check switch
  244. * identity.
  245. */
  246. int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid)
  247. {
  248. u8 data[9];
  249. u16 drom_offset;
  250. u8 crc;
  251. int res = tb_eeprom_get_drom_offset(sw, &drom_offset);
  252. if (res)
  253. return res;
  254. /* read uid */
  255. res = tb_eeprom_read_n(sw, drom_offset, data, 9);
  256. if (res)
  257. return res;
  258. crc = tb_crc8(data + 1, 8);
  259. if (crc != data[0]) {
  260. tb_sw_warn(sw, "uid crc8 missmatch (expected: %#x, got: %#x)\n",
  261. data[0], crc);
  262. return -EIO;
  263. }
  264. *uid = *(u64 *)(data+1);
  265. return 0;
  266. }
  267. static void tb_drom_parse_port_entry(struct tb_port *port,
  268. struct tb_drom_entry_port *entry)
  269. {
  270. port->link_nr = entry->link_nr;
  271. if (entry->has_dual_link_port)
  272. port->dual_link_port =
  273. &port->sw->ports[entry->dual_link_port_nr];
  274. }
  275. static int tb_drom_parse_entry(struct tb_switch *sw,
  276. struct tb_drom_entry_header *header)
  277. {
  278. struct tb_port *port;
  279. int res;
  280. enum tb_port_type type;
  281. if (header->type != TB_DROM_ENTRY_PORT)
  282. return 0;
  283. port = &sw->ports[header->index];
  284. port->disabled = header->port_disabled;
  285. if (port->disabled)
  286. return 0;
  287. res = tb_port_read(port, &type, TB_CFG_PORT, 2, 1);
  288. if (res)
  289. return res;
  290. type &= 0xffffff;
  291. if (type == TB_TYPE_PORT) {
  292. struct tb_drom_entry_port *entry = (void *) header;
  293. if (header->len != sizeof(*entry)) {
  294. tb_sw_warn(sw,
  295. "port entry has size %#x (expected %#zx)\n",
  296. header->len, sizeof(struct tb_drom_entry_port));
  297. return -EIO;
  298. }
  299. tb_drom_parse_port_entry(port, entry);
  300. }
  301. return 0;
  302. }
  303. /**
  304. * tb_drom_parse_entries - parse the linked list of drom entries
  305. *
  306. * Drom must have been copied to sw->drom.
  307. */
  308. static int tb_drom_parse_entries(struct tb_switch *sw)
  309. {
  310. struct tb_drom_header *header = (void *) sw->drom;
  311. u16 pos = sizeof(*header);
  312. u16 drom_size = header->data_len + TB_DROM_DATA_START;
  313. while (pos < drom_size) {
  314. struct tb_drom_entry_header *entry = (void *) (sw->drom + pos);
  315. if (pos + 1 == drom_size || pos + entry->len > drom_size
  316. || !entry->len) {
  317. tb_sw_warn(sw, "drom buffer overrun, aborting\n");
  318. return -EIO;
  319. }
  320. tb_drom_parse_entry(sw, entry);
  321. pos += entry->len;
  322. }
  323. return 0;
  324. }
  325. /**
  326. * tb_drom_read - copy drom to sw->drom and parse it
  327. */
  328. int tb_drom_read(struct tb_switch *sw)
  329. {
  330. u16 drom_offset;
  331. u16 size;
  332. u32 crc;
  333. struct tb_drom_header *header;
  334. int res;
  335. if (sw->drom)
  336. return 0;
  337. if (tb_route(sw) == 0) {
  338. /*
  339. * The root switch contains only a dummy drom (header only,
  340. * no entries). Hardcode the configuration here.
  341. */
  342. tb_drom_read_uid_only(sw, &sw->uid);
  343. sw->ports[1].link_nr = 0;
  344. sw->ports[2].link_nr = 1;
  345. sw->ports[1].dual_link_port = &sw->ports[2];
  346. sw->ports[2].dual_link_port = &sw->ports[1];
  347. sw->ports[3].link_nr = 0;
  348. sw->ports[4].link_nr = 1;
  349. sw->ports[3].dual_link_port = &sw->ports[4];
  350. sw->ports[4].dual_link_port = &sw->ports[3];
  351. return 0;
  352. }
  353. res = tb_eeprom_get_drom_offset(sw, &drom_offset);
  354. if (res)
  355. return res;
  356. res = tb_eeprom_read_n(sw, drom_offset + 14, (u8 *) &size, 2);
  357. if (res)
  358. return res;
  359. size &= 0x3ff;
  360. size += TB_DROM_DATA_START;
  361. tb_sw_info(sw, "reading drom (length: %#x)\n", size);
  362. if (size < sizeof(*header)) {
  363. tb_sw_warn(sw, "drom too small, aborting\n");
  364. return -EIO;
  365. }
  366. sw->drom = kzalloc(size, GFP_KERNEL);
  367. if (!sw->drom)
  368. return -ENOMEM;
  369. res = tb_eeprom_read_n(sw, drom_offset, sw->drom, size);
  370. if (res)
  371. goto err;
  372. header = (void *) sw->drom;
  373. if (header->data_len + TB_DROM_DATA_START != size) {
  374. tb_sw_warn(sw, "drom size mismatch, aborting\n");
  375. goto err;
  376. }
  377. crc = tb_crc8((u8 *) &header->uid, 8);
  378. if (crc != header->uid_crc8) {
  379. tb_sw_warn(sw,
  380. "drom uid crc8 mismatch (expected: %#x, got: %#x), aborting\n",
  381. header->uid_crc8, crc);
  382. goto err;
  383. }
  384. sw->uid = header->uid;
  385. crc = tb_crc32(sw->drom + TB_DROM_DATA_START, header->data_len);
  386. if (crc != header->data_crc32) {
  387. tb_sw_warn(sw,
  388. "drom data crc32 mismatch (expected: %#x, got: %#x), aborting\n",
  389. header->data_crc32, crc);
  390. goto err;
  391. }
  392. if (header->device_rom_revision > 1)
  393. tb_sw_warn(sw, "drom device_rom_revision %#x unknown\n",
  394. header->device_rom_revision);
  395. return tb_drom_parse_entries(sw);
  396. err:
  397. kfree(sw->drom);
  398. sw->drom = NULL;
  399. return -EIO;
  400. }