1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - eeprom access
6 * Copyright (C) 2018, Intel Corporation
9 #include <linux/crc32.h>
10 #include <linux/property.h>
11 #include <linux/slab.h>
15 * tb_eeprom_ctl_write() - write control word
17 static int tb_eeprom_ctl_write(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
19 return tb_sw_write(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
23 * tb_eeprom_ctl_write() - read control word
25 static int tb_eeprom_ctl_read(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
27 return tb_sw_read(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
30 enum tb_eeprom_transfer {
36 * tb_eeprom_active - enable rom access
38 * WARNING: Always disable access after usage. Otherwise the controller will
41 static int tb_eeprom_active(struct tb_switch *sw, bool enable)
43 struct tb_eeprom_ctl ctl;
44 int res = tb_eeprom_ctl_read(sw, &ctl);
49 res = tb_eeprom_ctl_write(sw, &ctl);
53 return tb_eeprom_ctl_write(sw, &ctl);
56 res = tb_eeprom_ctl_write(sw, &ctl);
60 return tb_eeprom_ctl_write(sw, &ctl);
65 * tb_eeprom_transfer - transfer one bit
67 * If TB_EEPROM_IN is passed, then the bit can be retrieved from ctl->data_in.
68 * If TB_EEPROM_OUT is passed, then ctl->data_out will be written.
70 static int tb_eeprom_transfer(struct tb_switch *sw, struct tb_eeprom_ctl *ctl,
71 enum tb_eeprom_transfer direction)
74 if (direction == TB_EEPROM_OUT) {
75 res = tb_eeprom_ctl_write(sw, ctl);
80 res = tb_eeprom_ctl_write(sw, ctl);
83 if (direction == TB_EEPROM_IN) {
84 res = tb_eeprom_ctl_read(sw, ctl);
89 return tb_eeprom_ctl_write(sw, ctl);
93 * tb_eeprom_out - write one byte to the bus
95 static int tb_eeprom_out(struct tb_switch *sw, u8 val)
97 struct tb_eeprom_ctl ctl;
99 int res = tb_eeprom_ctl_read(sw, &ctl);
102 for (i = 0; i < 8; i++) {
103 ctl.data_out = val & 0x80;
104 res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_OUT);
113 * tb_eeprom_in - read one byte from the bus
115 static int tb_eeprom_in(struct tb_switch *sw, u8 *val)
117 struct tb_eeprom_ctl ctl;
119 int res = tb_eeprom_ctl_read(sw, &ctl);
123 for (i = 0; i < 8; i++) {
125 res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_IN);
134 * tb_eeprom_read_n - read count bytes from offset into val
136 static int tb_eeprom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
140 res = tb_eeprom_active(sw, true);
143 res = tb_eeprom_out(sw, 3);
146 res = tb_eeprom_out(sw, offset >> 8);
149 res = tb_eeprom_out(sw, offset);
152 for (i = 0; i < count; i++) {
153 res = tb_eeprom_in(sw, val + i);
157 return tb_eeprom_active(sw, false);
160 static u8 tb_crc8(u8 *data, int len)
164 for (i = 0; i < len; i++) {
166 for (j = 0; j < 8; j++)
167 val = (val << 1) ^ ((val & 0x80) ? 7 : 0);
172 static u32 tb_crc32(void *data, size_t len)
174 return ~__crc32c_le(~0, data, len);
177 #define TB_DROM_DATA_START 13
178 struct tb_drom_header {
180 u8 uid_crc8; /* checksum for uid */
184 u32 data_crc32; /* checksum for data_len bytes starting at byte 13 */
186 u8 device_rom_revision; /* should be <= 1 */
196 enum tb_drom_entry_type {
197 /* force unsigned to prevent "one-bit signed bitfield" warning */
198 TB_DROM_ENTRY_GENERIC = 0U,
202 struct tb_drom_entry_header {
205 bool port_disabled:1; /* only valid if type is TB_DROM_ENTRY_PORT */
206 enum tb_drom_entry_type type:1;
209 struct tb_drom_entry_generic {
210 struct tb_drom_entry_header header;
214 struct tb_drom_entry_port {
216 struct tb_drom_entry_header header;
218 u8 dual_link_port_rid:4;
221 bool has_dual_link_port:1;
224 u8 dual_link_port_nr:6;
227 /* BYTES 4 - 5 TODO decode */
232 /* BYTES 6-7, TODO: verify (find hardware that has these set) */
235 bool has_peer_port:1;
242 * tb_eeprom_get_drom_offset - get drom offset within eeprom
244 static int tb_eeprom_get_drom_offset(struct tb_switch *sw, u16 *offset)
246 struct tb_cap_plug_events cap;
248 if (!sw->cap_plug_events) {
249 tb_sw_warn(sw, "no TB_CAP_PLUG_EVENTS, cannot read eeprom\n");
252 res = tb_sw_read(sw, &cap, TB_CFG_SWITCH, sw->cap_plug_events,
257 if (!cap.eeprom_ctl.present || cap.eeprom_ctl.not_present) {
258 tb_sw_warn(sw, "no NVM\n");
262 if (cap.drom_offset > 0xffff) {
263 tb_sw_warn(sw, "drom offset is larger than 0xffff: %#x\n",
267 *offset = cap.drom_offset;
272 * tb_drom_read_uid_only - read uid directly from drom
274 * Does not use the cached copy in sw->drom. Used during resume to check switch
277 int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid)
282 int res = tb_eeprom_get_drom_offset(sw, &drom_offset);
286 if (drom_offset == 0)
290 res = tb_eeprom_read_n(sw, drom_offset, data, 9);
294 crc = tb_crc8(data + 1, 8);
295 if (crc != data[0]) {
296 tb_sw_warn(sw, "uid crc8 mismatch (expected: %#x, got: %#x)\n",
301 *uid = *(u64 *)(data+1);
305 static int tb_drom_parse_entry_generic(struct tb_switch *sw,
306 struct tb_drom_entry_header *header)
308 const struct tb_drom_entry_generic *entry =
309 (const struct tb_drom_entry_generic *)header;
311 switch (header->index) {
313 /* Length includes 2 bytes header so remove it before copy */
314 sw->vendor_name = kstrndup(entry->data,
315 header->len - sizeof(*header), GFP_KERNEL);
316 if (!sw->vendor_name)
321 sw->device_name = kstrndup(entry->data,
322 header->len - sizeof(*header), GFP_KERNEL);
323 if (!sw->device_name)
331 static int tb_drom_parse_entry_port(struct tb_switch *sw,
332 struct tb_drom_entry_header *header)
334 struct tb_port *port;
336 enum tb_port_type type;
339 * Some DROMs list more ports than the controller actually has
340 * so we skip those but allow the parser to continue.
342 if (header->index > sw->config.max_port_number) {
343 dev_info_once(&sw->dev, "ignoring unnecessary extra entries in DROM\n");
347 port = &sw->ports[header->index];
348 port->disabled = header->port_disabled;
352 res = tb_port_read(port, &type, TB_CFG_PORT, 2, 1);
357 if (type == TB_TYPE_PORT) {
358 struct tb_drom_entry_port *entry = (void *) header;
359 if (header->len != sizeof(*entry)) {
361 "port entry has size %#x (expected %#zx)\n",
362 header->len, sizeof(struct tb_drom_entry_port));
365 port->link_nr = entry->link_nr;
366 if (entry->has_dual_link_port)
367 port->dual_link_port =
368 &port->sw->ports[entry->dual_link_port_nr];
374 * tb_drom_parse_entries - parse the linked list of drom entries
376 * Drom must have been copied to sw->drom.
378 static int tb_drom_parse_entries(struct tb_switch *sw)
380 struct tb_drom_header *header = (void *) sw->drom;
381 u16 pos = sizeof(*header);
382 u16 drom_size = header->data_len + TB_DROM_DATA_START;
385 while (pos < drom_size) {
386 struct tb_drom_entry_header *entry = (void *) (sw->drom + pos);
387 if (pos + 1 == drom_size || pos + entry->len > drom_size
389 tb_sw_warn(sw, "drom buffer overrun, aborting\n");
393 switch (entry->type) {
394 case TB_DROM_ENTRY_GENERIC:
395 res = tb_drom_parse_entry_generic(sw, entry);
397 case TB_DROM_ENTRY_PORT:
398 res = tb_drom_parse_entry_port(sw, entry);
410 * tb_drom_copy_efi - copy drom supplied by EFI to sw->drom if present
412 static int tb_drom_copy_efi(struct tb_switch *sw, u16 *size)
414 struct device *dev = &sw->tb->nhi->pdev->dev;
417 len = device_property_read_u8_array(dev, "ThunderboltDROM", NULL, 0);
418 if (len < 0 || len < sizeof(struct tb_drom_header))
421 sw->drom = kmalloc(len, GFP_KERNEL);
425 res = device_property_read_u8_array(dev, "ThunderboltDROM", sw->drom,
430 *size = ((struct tb_drom_header *)sw->drom)->data_len +
443 static int tb_drom_copy_nvm(struct tb_switch *sw, u16 *size)
451 ret = tb_sw_read(sw, &drom_offset, TB_CFG_SWITCH,
452 sw->cap_plug_events + 12, 1);
459 ret = dma_port_flash_read(sw->dma_port, drom_offset + 14, size,
464 /* Size includes CRC8 + UID + CRC32 */
466 sw->drom = kzalloc(*size, GFP_KERNEL);
470 ret = dma_port_flash_read(sw->dma_port, drom_offset, sw->drom, *size);
475 * Read UID from the minimal DROM because the one in NVM is just
478 tb_drom_read_uid_only(sw, &sw->uid);
488 * tb_drom_read - copy drom to sw->drom and parse it
490 int tb_drom_read(struct tb_switch *sw)
495 struct tb_drom_header *header;
500 if (tb_route(sw) == 0) {
502 * Apple's NHI EFI driver supplies a DROM for the root switch
503 * in a device property. Use it if available.
505 if (tb_drom_copy_efi(sw, &size) == 0)
508 /* Non-Apple hardware has the DROM as part of NVM */
509 if (tb_drom_copy_nvm(sw, &size) == 0)
513 * The root switch contains only a dummy drom (header only,
514 * no entries). Hardcode the configuration here.
516 tb_drom_read_uid_only(sw, &sw->uid);
518 sw->ports[1].link_nr = 0;
519 sw->ports[2].link_nr = 1;
520 sw->ports[1].dual_link_port = &sw->ports[2];
521 sw->ports[2].dual_link_port = &sw->ports[1];
523 sw->ports[3].link_nr = 0;
524 sw->ports[4].link_nr = 1;
525 sw->ports[3].dual_link_port = &sw->ports[4];
526 sw->ports[4].dual_link_port = &sw->ports[3];
528 /* Port 5 is inaccessible on this gen 1 controller */
529 if (sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE)
530 sw->ports[5].disabled = true;
535 res = tb_eeprom_get_drom_offset(sw, &drom_offset);
539 res = tb_eeprom_read_n(sw, drom_offset + 14, (u8 *) &size, 2);
543 size += TB_DROM_DATA_START;
544 tb_sw_dbg(sw, "reading drom (length: %#x)\n", size);
545 if (size < sizeof(*header)) {
546 tb_sw_warn(sw, "drom too small, aborting\n");
550 sw->drom = kzalloc(size, GFP_KERNEL);
553 res = tb_eeprom_read_n(sw, drom_offset, sw->drom, size);
558 header = (void *) sw->drom;
560 if (header->data_len + TB_DROM_DATA_START != size) {
561 tb_sw_warn(sw, "drom size mismatch, aborting\n");
565 crc = tb_crc8((u8 *) &header->uid, 8);
566 if (crc != header->uid_crc8) {
568 "drom uid crc8 mismatch (expected: %#x, got: %#x), aborting\n",
569 header->uid_crc8, crc);
573 sw->uid = header->uid;
574 sw->vendor = header->vendor_id;
575 sw->device = header->model_id;
577 crc = tb_crc32(sw->drom + TB_DROM_DATA_START, header->data_len);
578 if (crc != header->data_crc32) {
580 "drom data crc32 mismatch (expected: %#x, got: %#x), continuing\n",
581 header->data_crc32, crc);
584 if (header->device_rom_revision > 2)
585 tb_sw_warn(sw, "drom device_rom_revision %#x unknown\n",
586 header->device_rom_revision);
588 return tb_drom_parse_entries(sw);