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