1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt Cactus Ridge driver - eeprom access
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
8 #include <linux/crc32.h>
9 #include <linux/property.h>
10 #include <linux/slab.h>
14 * tb_eeprom_ctl_write() - write control word
16 static int tb_eeprom_ctl_write(struct tb_switch
*sw
, struct tb_eeprom_ctl
*ctl
)
18 return tb_sw_write(sw
, ctl
, TB_CFG_SWITCH
, sw
->cap_plug_events
+ 4, 1);
22 * tb_eeprom_ctl_write() - read control word
24 static int tb_eeprom_ctl_read(struct tb_switch
*sw
, struct tb_eeprom_ctl
*ctl
)
26 return tb_sw_read(sw
, ctl
, TB_CFG_SWITCH
, sw
->cap_plug_events
+ 4, 1);
29 enum tb_eeprom_transfer
{
35 * tb_eeprom_active - enable rom access
37 * WARNING: Always disable access after usage. Otherwise the controller will
40 static int tb_eeprom_active(struct tb_switch
*sw
, bool enable
)
42 struct tb_eeprom_ctl ctl
;
43 int res
= tb_eeprom_ctl_read(sw
, &ctl
);
48 res
= tb_eeprom_ctl_write(sw
, &ctl
);
52 return tb_eeprom_ctl_write(sw
, &ctl
);
55 res
= tb_eeprom_ctl_write(sw
, &ctl
);
59 return tb_eeprom_ctl_write(sw
, &ctl
);
64 * tb_eeprom_transfer - transfer one bit
66 * If TB_EEPROM_IN is passed, then the bit can be retrieved from ctl->data_in.
67 * If TB_EEPROM_OUT is passed, then ctl->data_out will be written.
69 static int tb_eeprom_transfer(struct tb_switch
*sw
, struct tb_eeprom_ctl
*ctl
,
70 enum tb_eeprom_transfer direction
)
73 if (direction
== TB_EEPROM_OUT
) {
74 res
= tb_eeprom_ctl_write(sw
, ctl
);
79 res
= tb_eeprom_ctl_write(sw
, ctl
);
82 if (direction
== TB_EEPROM_IN
) {
83 res
= tb_eeprom_ctl_read(sw
, ctl
);
88 return tb_eeprom_ctl_write(sw
, ctl
);
92 * tb_eeprom_out - write one byte to the bus
94 static int tb_eeprom_out(struct tb_switch
*sw
, u8 val
)
96 struct tb_eeprom_ctl ctl
;
98 int res
= tb_eeprom_ctl_read(sw
, &ctl
);
101 for (i
= 0; i
< 8; i
++) {
102 ctl
.data_out
= val
& 0x80;
103 res
= tb_eeprom_transfer(sw
, &ctl
, TB_EEPROM_OUT
);
112 * tb_eeprom_in - read one byte from the bus
114 static int tb_eeprom_in(struct tb_switch
*sw
, u8
*val
)
116 struct tb_eeprom_ctl ctl
;
118 int res
= tb_eeprom_ctl_read(sw
, &ctl
);
122 for (i
= 0; i
< 8; i
++) {
124 res
= tb_eeprom_transfer(sw
, &ctl
, TB_EEPROM_IN
);
133 * tb_eeprom_read_n - read count bytes from offset into val
135 static int tb_eeprom_read_n(struct tb_switch
*sw
, u16 offset
, u8
*val
,
139 res
= tb_eeprom_active(sw
, true);
142 res
= tb_eeprom_out(sw
, 3);
145 res
= tb_eeprom_out(sw
, offset
>> 8);
148 res
= tb_eeprom_out(sw
, offset
);
151 for (i
= 0; i
< count
; i
++) {
152 res
= tb_eeprom_in(sw
, val
+ i
);
156 return tb_eeprom_active(sw
, false);
159 static u8
tb_crc8(u8
*data
, int len
)
163 for (i
= 0; i
< len
; i
++) {
165 for (j
= 0; j
< 8; j
++)
166 val
= (val
<< 1) ^ ((val
& 0x80) ? 7 : 0);
171 static u32
tb_crc32(void *data
, size_t len
)
173 return ~__crc32c_le(~0, data
, len
);
176 #define TB_DROM_DATA_START 13
177 struct tb_drom_header
{
179 u8 uid_crc8
; /* checksum for uid */
183 u32 data_crc32
; /* checksum for data_len bytes starting at byte 13 */
185 u8 device_rom_revision
; /* should be <= 1 */
195 enum tb_drom_entry_type
{
196 /* force unsigned to prevent "one-bit signed bitfield" warning */
197 TB_DROM_ENTRY_GENERIC
= 0U,
201 struct tb_drom_entry_header
{
204 bool port_disabled
:1; /* only valid if type is TB_DROM_ENTRY_PORT */
205 enum tb_drom_entry_type type
:1;
208 struct tb_drom_entry_generic
{
209 struct tb_drom_entry_header header
;
213 struct tb_drom_entry_port
{
215 struct tb_drom_entry_header header
;
217 u8 dual_link_port_rid
:4;
220 bool has_dual_link_port
:1;
223 u8 dual_link_port_nr
:6;
226 /* BYTES 4 - 5 TODO decode */
231 /* BYTES 6-7, TODO: verify (find hardware that has these set) */
234 bool has_peer_port
:1;
241 * tb_eeprom_get_drom_offset - get drom offset within eeprom
243 static int tb_eeprom_get_drom_offset(struct tb_switch
*sw
, u16
*offset
)
245 struct tb_cap_plug_events cap
;
247 if (!sw
->cap_plug_events
) {
248 tb_sw_warn(sw
, "no TB_CAP_PLUG_EVENTS, cannot read eeprom\n");
251 res
= tb_sw_read(sw
, &cap
, TB_CFG_SWITCH
, sw
->cap_plug_events
,
256 if (!cap
.eeprom_ctl
.present
|| cap
.eeprom_ctl
.not_present
) {
257 tb_sw_warn(sw
, "no NVM\n");
261 if (cap
.drom_offset
> 0xffff) {
262 tb_sw_warn(sw
, "drom offset is larger than 0xffff: %#x\n",
266 *offset
= cap
.drom_offset
;
271 * tb_drom_read_uid_only - read uid directly from drom
273 * Does not use the cached copy in sw->drom. Used during resume to check switch
276 int tb_drom_read_uid_only(struct tb_switch
*sw
, u64
*uid
)
281 int res
= tb_eeprom_get_drom_offset(sw
, &drom_offset
);
285 if (drom_offset
== 0)
289 res
= tb_eeprom_read_n(sw
, drom_offset
, data
, 9);
293 crc
= tb_crc8(data
+ 1, 8);
294 if (crc
!= data
[0]) {
295 tb_sw_warn(sw
, "uid crc8 mismatch (expected: %#x, got: %#x)\n",
300 *uid
= *(u64
*)(data
+1);
304 static int tb_drom_parse_entry_generic(struct tb_switch
*sw
,
305 struct tb_drom_entry_header
*header
)
307 const struct tb_drom_entry_generic
*entry
=
308 (const struct tb_drom_entry_generic
*)header
;
310 switch (header
->index
) {
312 /* Length includes 2 bytes header so remove it before copy */
313 sw
->vendor_name
= kstrndup(entry
->data
,
314 header
->len
- sizeof(*header
), GFP_KERNEL
);
315 if (!sw
->vendor_name
)
320 sw
->device_name
= kstrndup(entry
->data
,
321 header
->len
- sizeof(*header
), GFP_KERNEL
);
322 if (!sw
->device_name
)
330 static int tb_drom_parse_entry_port(struct tb_switch
*sw
,
331 struct tb_drom_entry_header
*header
)
333 struct tb_port
*port
;
335 enum tb_port_type type
;
338 * Some DROMs list more ports than the controller actually has
339 * so we skip those but allow the parser to continue.
341 if (header
->index
> sw
->config
.max_port_number
) {
342 dev_info_once(&sw
->dev
, "ignoring unnecessary extra entries in DROM\n");
346 port
= &sw
->ports
[header
->index
];
347 port
->disabled
= header
->port_disabled
;
351 res
= tb_port_read(port
, &type
, TB_CFG_PORT
, 2, 1);
356 if (type
== TB_TYPE_PORT
) {
357 struct tb_drom_entry_port
*entry
= (void *) header
;
358 if (header
->len
!= sizeof(*entry
)) {
360 "port entry has size %#x (expected %#zx)\n",
361 header
->len
, sizeof(struct tb_drom_entry_port
));
364 port
->link_nr
= entry
->link_nr
;
365 if (entry
->has_dual_link_port
)
366 port
->dual_link_port
=
367 &port
->sw
->ports
[entry
->dual_link_port_nr
];
373 * tb_drom_parse_entries - parse the linked list of drom entries
375 * Drom must have been copied to sw->drom.
377 static int tb_drom_parse_entries(struct tb_switch
*sw
)
379 struct tb_drom_header
*header
= (void *) sw
->drom
;
380 u16 pos
= sizeof(*header
);
381 u16 drom_size
= header
->data_len
+ TB_DROM_DATA_START
;
384 while (pos
< drom_size
) {
385 struct tb_drom_entry_header
*entry
= (void *) (sw
->drom
+ pos
);
386 if (pos
+ 1 == drom_size
|| pos
+ entry
->len
> drom_size
388 tb_sw_warn(sw
, "drom buffer overrun, aborting\n");
392 switch (entry
->type
) {
393 case TB_DROM_ENTRY_GENERIC
:
394 res
= tb_drom_parse_entry_generic(sw
, entry
);
396 case TB_DROM_ENTRY_PORT
:
397 res
= tb_drom_parse_entry_port(sw
, entry
);
409 * tb_drom_copy_efi - copy drom supplied by EFI to sw->drom if present
411 static int tb_drom_copy_efi(struct tb_switch
*sw
, u16
*size
)
413 struct device
*dev
= &sw
->tb
->nhi
->pdev
->dev
;
416 len
= device_property_read_u8_array(dev
, "ThunderboltDROM", NULL
, 0);
417 if (len
< 0 || len
< sizeof(struct tb_drom_header
))
420 sw
->drom
= kmalloc(len
, GFP_KERNEL
);
424 res
= device_property_read_u8_array(dev
, "ThunderboltDROM", sw
->drom
,
429 *size
= ((struct tb_drom_header
*)sw
->drom
)->data_len
+
442 static int tb_drom_copy_nvm(struct tb_switch
*sw
, u16
*size
)
450 ret
= tb_sw_read(sw
, &drom_offset
, TB_CFG_SWITCH
,
451 sw
->cap_plug_events
+ 12, 1);
458 ret
= dma_port_flash_read(sw
->dma_port
, drom_offset
+ 14, size
,
463 /* Size includes CRC8 + UID + CRC32 */
465 sw
->drom
= kzalloc(*size
, GFP_KERNEL
);
469 ret
= dma_port_flash_read(sw
->dma_port
, drom_offset
, sw
->drom
, *size
);
474 * Read UID from the minimal DROM because the one in NVM is just
477 tb_drom_read_uid_only(sw
, &sw
->uid
);
487 * tb_drom_read - copy drom to sw->drom and parse it
489 int tb_drom_read(struct tb_switch
*sw
)
494 struct tb_drom_header
*header
;
499 if (tb_route(sw
) == 0) {
501 * Apple's NHI EFI driver supplies a DROM for the root switch
502 * in a device property. Use it if available.
504 if (tb_drom_copy_efi(sw
, &size
) == 0)
507 /* Non-Apple hardware has the DROM as part of NVM */
508 if (tb_drom_copy_nvm(sw
, &size
) == 0)
512 * The root switch contains only a dummy drom (header only,
513 * no entries). Hardcode the configuration here.
515 tb_drom_read_uid_only(sw
, &sw
->uid
);
517 sw
->ports
[1].link_nr
= 0;
518 sw
->ports
[2].link_nr
= 1;
519 sw
->ports
[1].dual_link_port
= &sw
->ports
[2];
520 sw
->ports
[2].dual_link_port
= &sw
->ports
[1];
522 sw
->ports
[3].link_nr
= 0;
523 sw
->ports
[4].link_nr
= 1;
524 sw
->ports
[3].dual_link_port
= &sw
->ports
[4];
525 sw
->ports
[4].dual_link_port
= &sw
->ports
[3];
527 /* Port 5 is inaccessible on this gen 1 controller */
528 if (sw
->config
.device_id
== PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
)
529 sw
->ports
[5].disabled
= true;
534 res
= tb_eeprom_get_drom_offset(sw
, &drom_offset
);
538 res
= tb_eeprom_read_n(sw
, drom_offset
+ 14, (u8
*) &size
, 2);
542 size
+= TB_DROM_DATA_START
;
543 tb_sw_info(sw
, "reading drom (length: %#x)\n", size
);
544 if (size
< sizeof(*header
)) {
545 tb_sw_warn(sw
, "drom too small, aborting\n");
549 sw
->drom
= kzalloc(size
, GFP_KERNEL
);
552 res
= tb_eeprom_read_n(sw
, drom_offset
, sw
->drom
, size
);
557 header
= (void *) sw
->drom
;
559 if (header
->data_len
+ TB_DROM_DATA_START
!= size
) {
560 tb_sw_warn(sw
, "drom size mismatch, aborting\n");
564 crc
= tb_crc8((u8
*) &header
->uid
, 8);
565 if (crc
!= header
->uid_crc8
) {
567 "drom uid crc8 mismatch (expected: %#x, got: %#x), aborting\n",
568 header
->uid_crc8
, crc
);
572 sw
->uid
= header
->uid
;
573 sw
->vendor
= header
->vendor_id
;
574 sw
->device
= header
->model_id
;
576 crc
= tb_crc32(sw
->drom
+ TB_DROM_DATA_START
, header
->data_len
);
577 if (crc
!= header
->data_crc32
) {
579 "drom data crc32 mismatch (expected: %#x, got: %#x), continuing\n",
580 header
->data_crc32
, crc
);
583 if (header
->device_rom_revision
> 2)
584 tb_sw_warn(sw
, "drom device_rom_revision %#x unknown\n",
585 header
->device_rom_revision
);
587 return tb_drom_parse_entries(sw
);