2 * Thunderbolt Cactus Ridge driver - eeprom access
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
7 #include <linux/crc32.h>
8 #include <linux/slab.h>
12 * tb_eeprom_ctl_write() - write control word
14 static int tb_eeprom_ctl_write(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
16 return tb_sw_write(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
20 * tb_eeprom_ctl_write() - read control word
22 static int tb_eeprom_ctl_read(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
24 return tb_sw_read(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
27 enum tb_eeprom_transfer {
33 * tb_eeprom_active - enable rom access
35 * WARNING: Always disable access after usage. Otherwise the controller will
38 static int tb_eeprom_active(struct tb_switch *sw, bool enable)
40 struct tb_eeprom_ctl ctl;
41 int res = tb_eeprom_ctl_read(sw, &ctl);
46 res = tb_eeprom_ctl_write(sw, &ctl);
50 return tb_eeprom_ctl_write(sw, &ctl);
53 res = tb_eeprom_ctl_write(sw, &ctl);
57 return tb_eeprom_ctl_write(sw, &ctl);
62 * tb_eeprom_transfer - transfer one bit
64 * If TB_EEPROM_IN is passed, then the bit can be retrieved from ctl->data_in.
65 * If TB_EEPROM_OUT is passed, then ctl->data_out will be written.
67 static int tb_eeprom_transfer(struct tb_switch *sw, struct tb_eeprom_ctl *ctl,
68 enum tb_eeprom_transfer direction)
71 if (direction == TB_EEPROM_OUT) {
72 res = tb_eeprom_ctl_write(sw, ctl);
77 res = tb_eeprom_ctl_write(sw, ctl);
80 if (direction == TB_EEPROM_IN) {
81 res = tb_eeprom_ctl_read(sw, ctl);
86 return tb_eeprom_ctl_write(sw, ctl);
90 * tb_eeprom_out - write one byte to the bus
92 static int tb_eeprom_out(struct tb_switch *sw, u8 val)
94 struct tb_eeprom_ctl ctl;
96 int res = tb_eeprom_ctl_read(sw, &ctl);
99 for (i = 0; i < 8; i++) {
100 ctl.data_out = val & 0x80;
101 res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_OUT);
110 * tb_eeprom_in - read one byte from the bus
112 static int tb_eeprom_in(struct tb_switch *sw, u8 *val)
114 struct tb_eeprom_ctl ctl;
116 int res = tb_eeprom_ctl_read(sw, &ctl);
120 for (i = 0; i < 8; i++) {
122 res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_IN);
131 * tb_eeprom_read_n - read count bytes from offset into val
133 static int tb_eeprom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
137 res = tb_eeprom_active(sw, true);
140 res = tb_eeprom_out(sw, 3);
143 res = tb_eeprom_out(sw, offset >> 8);
146 res = tb_eeprom_out(sw, offset);
149 for (i = 0; i < count; i++) {
150 res = tb_eeprom_in(sw, val + i);
154 return tb_eeprom_active(sw, false);
157 static u8 tb_crc8(u8 *data, int len)
161 for (i = 0; i < len; i++) {
163 for (j = 0; j < 8; j++)
164 val = (val << 1) ^ ((val & 0x80) ? 7 : 0);
169 static u32 tb_crc32(void *data, size_t len)
171 return ~__crc32c_le(~0, data, len);
174 #define TB_DROM_DATA_START 13
175 struct tb_drom_header {
177 u8 uid_crc8; /* checksum for uid */
181 u32 data_crc32; /* checksum for data_len bytes starting at byte 13 */
183 u8 device_rom_revision; /* should be <= 1 */
193 enum tb_drom_entry_type {
194 /* force unsigned to prevent "one-bit signed bitfield" warning */
195 TB_DROM_ENTRY_GENERIC = 0U,
199 struct tb_drom_entry_header {
202 bool port_disabled:1; /* only valid if type is TB_DROM_ENTRY_PORT */
203 enum tb_drom_entry_type type:1;
206 struct tb_drom_entry_port {
208 struct tb_drom_entry_header header;
210 u8 dual_link_port_rid:4;
213 bool has_dual_link_port:1;
216 u8 dual_link_port_nr:6;
219 /* BYTES 4 - 5 TODO decode */
224 /* BYTES 5-6, TODO: verify (find hardware that has these set) */
227 bool has_peer_port:1;
234 * tb_eeprom_get_drom_offset - get drom offset within eeprom
236 static int tb_eeprom_get_drom_offset(struct tb_switch *sw, u16 *offset)
238 struct tb_cap_plug_events cap;
240 if (!sw->cap_plug_events) {
241 tb_sw_warn(sw, "no TB_CAP_PLUG_EVENTS, cannot read eeprom\n");
244 res = tb_sw_read(sw, &cap, TB_CFG_SWITCH, sw->cap_plug_events,
249 if (!cap.eeprom_ctl.present || cap.eeprom_ctl.not_present) {
250 tb_sw_warn(sw, "no NVM\n");
254 if (cap.drom_offset > 0xffff) {
255 tb_sw_warn(sw, "drom offset is larger than 0xffff: %#x\n",
259 *offset = cap.drom_offset;
264 * tb_drom_read_uid_only - read uid directly from drom
266 * Does not use the cached copy in sw->drom. Used during resume to check switch
269 int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid)
274 int res = tb_eeprom_get_drom_offset(sw, &drom_offset);
279 res = tb_eeprom_read_n(sw, drom_offset, data, 9);
283 crc = tb_crc8(data + 1, 8);
284 if (crc != data[0]) {
285 tb_sw_warn(sw, "uid crc8 missmatch (expected: %#x, got: %#x)\n",
290 *uid = *(u64 *)(data+1);
294 static void tb_drom_parse_port_entry(struct tb_port *port,
295 struct tb_drom_entry_port *entry)
297 port->link_nr = entry->link_nr;
298 if (entry->has_dual_link_port)
299 port->dual_link_port =
300 &port->sw->ports[entry->dual_link_port_nr];
303 static int tb_drom_parse_entry(struct tb_switch *sw,
304 struct tb_drom_entry_header *header)
306 struct tb_port *port;
308 enum tb_port_type type;
310 if (header->type != TB_DROM_ENTRY_PORT)
313 port = &sw->ports[header->index];
314 port->disabled = header->port_disabled;
318 res = tb_port_read(port, &type, TB_CFG_PORT, 2, 1);
323 if (type == TB_TYPE_PORT) {
324 struct tb_drom_entry_port *entry = (void *) header;
325 if (header->len != sizeof(*entry)) {
327 "port entry has size %#x (expected %#zx)\n",
328 header->len, sizeof(struct tb_drom_entry_port));
331 tb_drom_parse_port_entry(port, entry);
337 * tb_drom_parse_entries - parse the linked list of drom entries
339 * Drom must have been copied to sw->drom.
341 static int tb_drom_parse_entries(struct tb_switch *sw)
343 struct tb_drom_header *header = (void *) sw->drom;
344 u16 pos = sizeof(*header);
345 u16 drom_size = header->data_len + TB_DROM_DATA_START;
347 while (pos < drom_size) {
348 struct tb_drom_entry_header *entry = (void *) (sw->drom + pos);
349 if (pos + 1 == drom_size || pos + entry->len > drom_size
351 tb_sw_warn(sw, "drom buffer overrun, aborting\n");
355 tb_drom_parse_entry(sw, entry);
363 * tb_drom_read - copy drom to sw->drom and parse it
365 int tb_drom_read(struct tb_switch *sw)
370 struct tb_drom_header *header;
375 if (tb_route(sw) == 0) {
377 * The root switch contains only a dummy drom (header only,
378 * no entries). Hardcode the configuration here.
380 tb_drom_read_uid_only(sw, &sw->uid);
382 sw->ports[1].link_nr = 0;
383 sw->ports[2].link_nr = 1;
384 sw->ports[1].dual_link_port = &sw->ports[2];
385 sw->ports[2].dual_link_port = &sw->ports[1];
387 sw->ports[3].link_nr = 0;
388 sw->ports[4].link_nr = 1;
389 sw->ports[3].dual_link_port = &sw->ports[4];
390 sw->ports[4].dual_link_port = &sw->ports[3];
394 res = tb_eeprom_get_drom_offset(sw, &drom_offset);
398 res = tb_eeprom_read_n(sw, drom_offset + 14, (u8 *) &size, 2);
402 size += TB_DROM_DATA_START;
403 tb_sw_info(sw, "reading drom (length: %#x)\n", size);
404 if (size < sizeof(*header)) {
405 tb_sw_warn(sw, "drom too small, aborting\n");
409 sw->drom = kzalloc(size, GFP_KERNEL);
412 res = tb_eeprom_read_n(sw, drom_offset, sw->drom, size);
416 header = (void *) sw->drom;
418 if (header->data_len + TB_DROM_DATA_START != size) {
419 tb_sw_warn(sw, "drom size mismatch, aborting\n");
423 crc = tb_crc8((u8 *) &header->uid, 8);
424 if (crc != header->uid_crc8) {
426 "drom uid crc8 mismatch (expected: %#x, got: %#x), aborting\n",
427 header->uid_crc8, crc);
430 sw->uid = header->uid;
432 crc = tb_crc32(sw->drom + TB_DROM_DATA_START, header->data_len);
433 if (crc != header->data_crc32) {
435 "drom data crc32 mismatch (expected: %#x, got: %#x), aborting\n",
436 header->data_crc32, crc);
440 if (header->device_rom_revision > 1)
441 tb_sw_warn(sw, "drom device_rom_revision %#x unknown\n",
442 header->device_rom_revision);
444 return tb_drom_parse_entries(sw);