blob: d7dfdd231669df49948eb355380cb46d949e9ec2 [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
Dean Luickcfe3e652016-02-03 14:36:14 -08008 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * BSD LICENSE
20 *
Dean Luickcfe3e652016-02-03 14:36:14 -080021 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -040022 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 *
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
32 * distribution.
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 *
49 */
50
51#include <linux/delay.h>
52#include <linux/pci.h>
53#include <linux/vmalloc.h>
54
55#include "hfi.h"
56#include "twsi.h"
57
58/*
59 * "Two Wire Serial Interface" support.
60 *
61 * Originally written for a not-quite-i2c serial eeprom, which is
62 * still used on some supported boards. Later boards have added a
63 * variety of other uses, most board-specific, so the bit-boffing
64 * part has been split off to this file, while the other parts
65 * have been moved to chip-specific files.
66 *
67 * We have also dropped all pretense of fully generic (e.g. pretend
68 * we don't know whether '1' is the higher voltage) interface, as
69 * the restrictions of the generic i2c interface (e.g. no access from
70 * driver itself) make it unsuitable for this use.
71 */
72
73#define READ_CMD 1
74#define WRITE_CMD 0
75
76/**
77 * i2c_wait_for_writes - wait for a write
78 * @dd: the hfi1_ib device
79 *
80 * We use this instead of udelay directly, so we can make sure
81 * that previous register writes have been flushed all the way
82 * to the chip. Since we are delaying anyway, the cost doesn't
83 * hurt, and makes the bit twiddling more regular
84 */
85static void i2c_wait_for_writes(struct hfi1_devdata *dd, u32 target)
86{
87 /*
88 * implicit read of EXTStatus is as good as explicit
89 * read of scratch, if all we want to do is flush
90 * writes.
91 */
92 hfi1_gpio_mod(dd, target, 0, 0, 0);
93 rmb(); /* inlined, so prevent compiler reordering */
94}
95
96/*
97 * QSFP modules are allowed to hold SCL low for 500uSec. Allow twice that
98 * for "almost compliant" modules
99 */
100#define SCL_WAIT_USEC 1000
101
102/* BUF_WAIT is time bus must be free between STOP or ACK and to next START.
103 * Should be 20, but some chips need more.
104 */
105#define TWSI_BUF_WAIT_USEC 60
106
107static void scl_out(struct hfi1_devdata *dd, u32 target, u8 bit)
108{
109 u32 mask;
110
111 udelay(1);
112
113 mask = QSFP_HFI0_I2CCLK;
114
115 /* SCL is meant to be bare-drain, so never set "OUT", just DIR */
116 hfi1_gpio_mod(dd, target, 0, bit ? 0 : mask, mask);
117
118 /*
119 * Allow for slow slaves by simple
120 * delay for falling edge, sampling on rise.
121 */
122 if (!bit)
123 udelay(2);
124 else {
125 int rise_usec;
126
127 for (rise_usec = SCL_WAIT_USEC; rise_usec > 0; rise_usec -= 2) {
128 if (mask & hfi1_gpio_mod(dd, target, 0, 0, 0))
129 break;
130 udelay(2);
131 }
132 if (rise_usec <= 0)
133 dd_dev_err(dd, "SCL interface stuck low > %d uSec\n",
134 SCL_WAIT_USEC);
135 }
136 i2c_wait_for_writes(dd, target);
137}
138
Dean Luickcfe3e652016-02-03 14:36:14 -0800139static u8 scl_in(struct hfi1_devdata *dd, u32 target, int wait)
140{
141 u32 read_val, mask;
142
143 mask = QSFP_HFI0_I2CCLK;
144 /* SCL is meant to be bare-drain, so never set "OUT", just DIR */
145 hfi1_gpio_mod(dd, target, 0, 0, mask);
146 read_val = hfi1_gpio_mod(dd, target, 0, 0, 0);
147 if (wait)
148 i2c_wait_for_writes(dd, target);
149 return (read_val & mask) >> GPIO_SCL_NUM;
150}
151
Mike Marciniszyn77241052015-07-30 15:17:43 -0400152static void sda_out(struct hfi1_devdata *dd, u32 target, u8 bit)
153{
154 u32 mask;
155
156 mask = QSFP_HFI0_I2CDAT;
157
158 /* SDA is meant to be bare-drain, so never set "OUT", just DIR */
159 hfi1_gpio_mod(dd, target, 0, bit ? 0 : mask, mask);
160
161 i2c_wait_for_writes(dd, target);
162 udelay(2);
163}
164
165static u8 sda_in(struct hfi1_devdata *dd, u32 target, int wait)
166{
167 u32 read_val, mask;
168
169 mask = QSFP_HFI0_I2CDAT;
170 /* SDA is meant to be bare-drain, so never set "OUT", just DIR */
171 hfi1_gpio_mod(dd, target, 0, 0, mask);
172 read_val = hfi1_gpio_mod(dd, target, 0, 0, 0);
173 if (wait)
174 i2c_wait_for_writes(dd, target);
175 return (read_val & mask) >> GPIO_SDA_NUM;
176}
177
178/**
179 * i2c_ackrcv - see if ack following write is true
180 * @dd: the hfi1_ib device
181 */
182static int i2c_ackrcv(struct hfi1_devdata *dd, u32 target)
183{
184 u8 ack_received;
185
186 /* AT ENTRY SCL = LOW */
187 /* change direction, ignore data */
188 ack_received = sda_in(dd, target, 1);
189 scl_out(dd, target, 1);
190 ack_received = sda_in(dd, target, 1) == 0;
191 scl_out(dd, target, 0);
192 return ack_received;
193}
194
195static void stop_cmd(struct hfi1_devdata *dd, u32 target);
196
197/**
198 * rd_byte - read a byte, sending STOP on last, else ACK
199 * @dd: the hfi1_ib device
200 *
201 * Returns byte shifted out of device
202 */
203static int rd_byte(struct hfi1_devdata *dd, u32 target, int last)
204{
205 int bit_cntr, data;
206
207 data = 0;
208
209 for (bit_cntr = 7; bit_cntr >= 0; --bit_cntr) {
210 data <<= 1;
211 scl_out(dd, target, 1);
212 data |= sda_in(dd, target, 0);
213 scl_out(dd, target, 0);
214 }
215 if (last) {
216 scl_out(dd, target, 1);
217 stop_cmd(dd, target);
218 } else {
219 sda_out(dd, target, 0);
220 scl_out(dd, target, 1);
221 scl_out(dd, target, 0);
222 sda_out(dd, target, 1);
223 }
224 return data;
225}
226
227/**
228 * wr_byte - write a byte, one bit at a time
229 * @dd: the hfi1_ib device
230 * @data: the byte to write
231 *
232 * Returns 0 if we got the following ack, otherwise 1
233 */
234static int wr_byte(struct hfi1_devdata *dd, u32 target, u8 data)
235{
236 int bit_cntr;
237 u8 bit;
238
239 for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {
240 bit = (data >> bit_cntr) & 1;
241 sda_out(dd, target, bit);
242 scl_out(dd, target, 1);
243 scl_out(dd, target, 0);
244 }
245 return (!i2c_ackrcv(dd, target)) ? 1 : 0;
246}
247
248/*
249 * issue TWSI start sequence:
250 * (both clock/data high, clock high, data low while clock is high)
251 */
252static void start_seq(struct hfi1_devdata *dd, u32 target)
253{
254 sda_out(dd, target, 1);
255 scl_out(dd, target, 1);
256 sda_out(dd, target, 0);
257 udelay(1);
258 scl_out(dd, target, 0);
259}
260
261/**
262 * stop_seq - transmit the stop sequence
263 * @dd: the hfi1_ib device
264 *
265 * (both clock/data low, clock high, data high while clock is high)
266 */
267static void stop_seq(struct hfi1_devdata *dd, u32 target)
268{
269 scl_out(dd, target, 0);
270 sda_out(dd, target, 0);
271 scl_out(dd, target, 1);
272 sda_out(dd, target, 1);
273}
274
275/**
276 * stop_cmd - transmit the stop condition
277 * @dd: the hfi1_ib device
278 *
279 * (both clock/data low, clock high, data high while clock is high)
280 */
281static void stop_cmd(struct hfi1_devdata *dd, u32 target)
282{
283 stop_seq(dd, target);
284 udelay(TWSI_BUF_WAIT_USEC);
285}
286
287/**
288 * hfi1_twsi_reset - reset I2C communication
289 * @dd: the hfi1_ib device
Dean Luickcfe3e652016-02-03 14:36:14 -0800290 * returns 0 if ok, -EIO on error
Mike Marciniszyn77241052015-07-30 15:17:43 -0400291 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400292int hfi1_twsi_reset(struct hfi1_devdata *dd, u32 target)
293{
294 int clock_cycles_left = 9;
Dean Luickcfe3e652016-02-03 14:36:14 -0800295 u32 mask;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400296
297 /* Both SCL and SDA should be high. If not, there
298 * is something wrong.
299 */
300 mask = QSFP_HFI0_I2CCLK | QSFP_HFI0_I2CDAT;
301
302 /*
303 * Force pins to desired innocuous state.
304 * This is the default power-on state with out=0 and dir=0,
305 * So tri-stated and should be floating high (barring HW problems)
306 */
307 hfi1_gpio_mod(dd, target, 0, 0, mask);
308
Dean Luickcfe3e652016-02-03 14:36:14 -0800309 /* Check if SCL is low, if it is low then we have a slave device
310 * misbehaving and there is not much we can do.
311 */
312 if (!scl_in(dd, target, 0))
313 return -EIO;
314
315 /* Check if SDA is low, if it is low then we have to clock SDA
316 * up to 9 times for the device to release the bus
Mike Marciniszyn77241052015-07-30 15:17:43 -0400317 */
318 while (clock_cycles_left--) {
Dean Luickcfe3e652016-02-03 14:36:14 -0800319 if (sda_in(dd, target, 0))
320 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400321 scl_out(dd, target, 0);
322 scl_out(dd, target, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400323 }
324
Dean Luickcfe3e652016-02-03 14:36:14 -0800325 return -EIO;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400326}
327
328#define HFI1_TWSI_START 0x100
329#define HFI1_TWSI_STOP 0x200
330
331/* Write byte to TWSI, optionally prefixed with START or suffixed with
332 * STOP.
333 * returns 0 if OK (ACK received), else != 0
334 */
335static int twsi_wr(struct hfi1_devdata *dd, u32 target, int data, int flags)
336{
337 int ret = 1;
338
339 if (flags & HFI1_TWSI_START)
340 start_seq(dd, target);
341
342 /* Leaves SCL low (from i2c_ackrcv()) */
343 ret = wr_byte(dd, target, data);
344
345 if (flags & HFI1_TWSI_STOP)
346 stop_cmd(dd, target);
347 return ret;
348}
349
350/* Added functionality for IBA7220-based cards */
351#define HFI1_TEMP_DEV 0x98
352
353/*
354 * hfi1_twsi_blk_rd
355 * General interface for data transfer from twsi devices.
356 * One vestige of its former role is that it recognizes a device
357 * HFI1_TWSI_NO_DEV and does the correct operation for the legacy part,
358 * which responded to all TWSI device codes, interpreting them as
359 * address within device. On all other devices found on board handled by
Dean Luickf1bf2962016-02-03 14:34:15 -0800360 * this driver, the device is followed by a N-byte "address" which selects
Mike Marciniszyn77241052015-07-30 15:17:43 -0400361 * the "register" or "offset" within the device from which data should
362 * be read.
363 */
364int hfi1_twsi_blk_rd(struct hfi1_devdata *dd, u32 target, int dev, int addr,
365 void *buffer, int len)
366{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400367 u8 *bp = buffer;
Dean Luickf1bf2962016-02-03 14:34:15 -0800368 int ret = 1;
369 int i;
370 int offset_size;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400371
Dean Luickf1bf2962016-02-03 14:34:15 -0800372 /* obtain the offset size, strip it from the device address */
373 offset_size = (dev >> 8) & 0xff;
374 dev &= 0xff;
375
376 /* allow at most a 2 byte offset */
377 if (offset_size > 2)
378 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400379
380 if (dev == HFI1_TWSI_NO_DEV) {
381 /* legacy not-really-I2C */
382 addr = (addr << 1) | READ_CMD;
383 ret = twsi_wr(dd, target, addr, HFI1_TWSI_START);
384 } else {
385 /* Actual I2C */
Dean Luickf1bf2962016-02-03 14:34:15 -0800386 if (offset_size) {
387 ret = twsi_wr(dd, target,
388 dev | WRITE_CMD, HFI1_TWSI_START);
389 if (ret) {
390 stop_cmd(dd, target);
391 goto bail;
392 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400393
Dean Luickf1bf2962016-02-03 14:34:15 -0800394 for (i = 0; i < offset_size; i++) {
395 ret = twsi_wr(dd, target,
396 (addr >> (i * 8)) & 0xff, 0);
397 udelay(TWSI_BUF_WAIT_USEC);
398 if (ret) {
399 dd_dev_err(dd, "Failed to write byte %d of offset 0x%04X\n",
400 i, addr);
401 goto bail;
402 }
403 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400404 }
405 ret = twsi_wr(dd, target, dev | READ_CMD, HFI1_TWSI_START);
406 }
407 if (ret) {
408 stop_cmd(dd, target);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400409 goto bail;
410 }
411
412 /*
413 * block devices keeps clocking data out as long as we ack,
414 * automatically incrementing the address. Some have "pages"
415 * whose boundaries will not be crossed, but the handling
416 * of these is left to the caller, who is in a better
417 * position to know.
418 */
419 while (len-- > 0) {
420 /*
421 * Get and store data, sending ACK if length remaining,
422 * else STOP
423 */
424 *bp++ = rd_byte(dd, target, !len);
425 }
426
427 ret = 0;
428
429bail:
430 return ret;
431}
432
433/*
434 * hfi1_twsi_blk_wr
435 * General interface for data transfer to twsi devices.
436 * One vestige of its former role is that it recognizes a device
437 * HFI1_TWSI_NO_DEV and does the correct operation for the legacy part,
438 * which responded to all TWSI device codes, interpreting them as
439 * address within device. On all other devices found on board handled by
Dean Luickf1bf2962016-02-03 14:34:15 -0800440 * this driver, the device is followed by a N-byte "address" which selects
Mike Marciniszyn77241052015-07-30 15:17:43 -0400441 * the "register" or "offset" within the device to which data should
442 * be written.
443 */
444int hfi1_twsi_blk_wr(struct hfi1_devdata *dd, u32 target, int dev, int addr,
445 const void *buffer, int len)
446{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400447 const u8 *bp = buffer;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400448 int ret = 1;
Dean Luickf1bf2962016-02-03 14:34:15 -0800449 int i;
450 int offset_size;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400451
Dean Luickf1bf2962016-02-03 14:34:15 -0800452 /* obtain the offset size, strip it from the device address */
453 offset_size = (dev >> 8) & 0xff;
454 dev &= 0xff;
455
456 /* allow at most a 2 byte offset */
457 if (offset_size > 2)
458 goto bail;
459
460 if (dev == HFI1_TWSI_NO_DEV) {
461 if (twsi_wr(dd, target, (addr << 1) | WRITE_CMD,
462 HFI1_TWSI_START)) {
463 goto failed_write;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400464 }
Dean Luickf1bf2962016-02-03 14:34:15 -0800465 } else {
466 /* Real I2C */
467 if (twsi_wr(dd, target, dev | WRITE_CMD, HFI1_TWSI_START))
468 goto failed_write;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400469 }
470
Dean Luickf1bf2962016-02-03 14:34:15 -0800471 for (i = 0; i < offset_size; i++) {
472 ret = twsi_wr(dd, target, (addr >> (i * 8)) & 0xff, 0);
473 udelay(TWSI_BUF_WAIT_USEC);
474 if (ret) {
475 dd_dev_err(dd, "Failed to write byte %d of offset 0x%04X\n",
476 i, addr);
477 goto bail;
478 }
479 }
480
481 for (i = 0; i < len; i++)
482 if (twsi_wr(dd, target, *bp++, 0))
483 goto failed_write;
484
Mike Marciniszyn77241052015-07-30 15:17:43 -0400485 ret = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400486
487failed_write:
488 stop_cmd(dd, target);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400489
490bail:
491 return ret;
492}