eeprom.c 11 KB

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