2 * Thunderbolt Cactus Ridge driver - eeprom access
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
10 * tb_eeprom_ctl_write() - write control word
12 static int tb_eeprom_ctl_write(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
14 return tb_sw_write(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
18 * tb_eeprom_ctl_write() - read control word
20 static int tb_eeprom_ctl_read(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
22 return tb_sw_read(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
25 enum tb_eeprom_transfer {
31 * tb_eeprom_active - enable rom access
33 * WARNING: Always disable access after usage. Otherwise the controller will
36 static int tb_eeprom_active(struct tb_switch *sw, bool enable)
38 struct tb_eeprom_ctl ctl;
39 int res = tb_eeprom_ctl_read(sw, &ctl);
44 res = tb_eeprom_ctl_write(sw, &ctl);
48 return tb_eeprom_ctl_write(sw, &ctl);
51 res = tb_eeprom_ctl_write(sw, &ctl);
55 return tb_eeprom_ctl_write(sw, &ctl);
60 * tb_eeprom_transfer - transfer one bit
62 * If TB_EEPROM_IN is passed, then the bit can be retrieved from ctl->data_in.
63 * If TB_EEPROM_OUT is passed, then ctl->data_out will be written.
65 static int tb_eeprom_transfer(struct tb_switch *sw, struct tb_eeprom_ctl *ctl,
66 enum tb_eeprom_transfer direction)
69 if (direction == TB_EEPROM_OUT) {
70 res = tb_eeprom_ctl_write(sw, ctl);
75 res = tb_eeprom_ctl_write(sw, ctl);
78 if (direction == TB_EEPROM_IN) {
79 res = tb_eeprom_ctl_read(sw, ctl);
84 return tb_eeprom_ctl_write(sw, ctl);
88 * tb_eeprom_out - write one byte to the bus
90 static int tb_eeprom_out(struct tb_switch *sw, u8 val)
92 struct tb_eeprom_ctl ctl;
94 int res = tb_eeprom_ctl_read(sw, &ctl);
97 for (i = 0; i < 8; i++) {
98 ctl.data_out = val & 0x80;
99 res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_OUT);
108 * tb_eeprom_in - read one byte from the bus
110 static int tb_eeprom_in(struct tb_switch *sw, u8 *val)
112 struct tb_eeprom_ctl ctl;
114 int res = tb_eeprom_ctl_read(sw, &ctl);
118 for (i = 0; i < 8; i++) {
120 res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_IN);
129 * tb_eeprom_read_n - read count bytes from offset into val
131 static int tb_eeprom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
135 res = tb_eeprom_active(sw, true);
138 res = tb_eeprom_out(sw, 3);
141 res = tb_eeprom_out(sw, offset >> 8);
144 res = tb_eeprom_out(sw, offset);
147 for (i = 0; i < count; i++) {
148 res = tb_eeprom_in(sw, val + i);
152 return tb_eeprom_active(sw, false);
155 int tb_eeprom_read_uid(struct tb_switch *sw, u64 *uid)
158 struct tb_cap_plug_events cap;
160 if (!sw->cap_plug_events) {
161 tb_sw_warn(sw, "no TB_CAP_PLUG_EVENTS, cannot read eeprom\n");
164 res = tb_sw_read(sw, &cap, TB_CFG_SWITCH, sw->cap_plug_events,
168 if (!cap.eeprom_ctl.present || cap.eeprom_ctl.not_present) {
169 tb_sw_warn(sw, "no NVM\n");
173 if (cap.drom_offset > 0xffff) {
174 tb_sw_warn(sw, "drom offset is larger than 0xffff: %#x\n",
180 res = tb_eeprom_read_n(sw, cap.drom_offset, data, 9);
183 /* TODO: check checksum in data[0] */
184 *uid = *(u64 *)(data+1);