2 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/delay.h>
34 #include <linux/pci.h>
35 #include <linux/vmalloc.h>
37 #include "ipath_kernel.h"
40 * InfiniPath I2C driver for a serial eeprom. This is not a generic
41 * I2C interface. For a start, the device we're using (Atmel AT24C11)
42 * doesn't work like a regular I2C device. It looks like one
43 * electrically, but not logically. Normal I2C devices have a single
44 * 7-bit or 10-bit I2C address that they respond to. Valid 7-bit
45 * addresses range from 0x03 to 0x77. Addresses 0x00 to 0x02 and 0x78
46 * to 0x7F are special reserved addresses (e.g. 0x00 is the "general
47 * call" address.) The Atmel device, on the other hand, responds to ALL
48 * 7-bit addresses. It's designed to be the only device on a given I2C
49 * bus. A 7-bit address corresponds to the memory address within the
50 * Atmel device itself.
52 * Also, the timing requirements mean more than simple software
53 * bitbanging, with readbacks from chip to ensure timing (simple udelay
56 * This all means that accessing the device is specialized enough
57 * that using the standard kernel I2C bitbanging interface would be
58 * impossible. For example, the core I2C eeprom driver expects to find
59 * a device at one or more of a limited set of addresses only. It doesn't
60 * allow writing to an eeprom. It also doesn't provide any means of
61 * accessing eeprom contents from within the kernel, only via sysfs.
77 static int eeprom_init;
80 * The gpioval manipulation really should be protected by spinlocks
81 * or be converted to use atomic operations.
85 * i2c_gpio_set - set a GPIO line
86 * @dd: the infinipath device
87 * @line: the line to set
88 * @new_line_state: the state to set
90 * Returns 0 if the line was set to the new state successfully, non-zero
93 static int i2c_gpio_set(struct ipath_devdata *dd,
95 enum i2c_state new_line_state)
97 u64 read_val, write_val, mask, *gpioval;
99 gpioval = &dd->ipath_gpio_out;
100 read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extctrl);
101 if (line == i2c_line_scl)
102 mask = ipath_gpio_scl;
104 mask = ipath_gpio_sda;
106 if (new_line_state == i2c_line_high)
107 /* tri-state the output rather than force high */
108 write_val = read_val & ~mask;
110 /* config line to be an output */
111 write_val = read_val | mask;
112 ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, write_val);
114 /* set high and verify */
115 if (new_line_state == i2c_line_high)
120 if (line == i2c_line_scl) {
121 write_val <<= ipath_gpio_scl_num;
122 *gpioval = *gpioval & ~(1UL << ipath_gpio_scl_num);
123 *gpioval |= write_val;
125 write_val <<= ipath_gpio_sda_num;
126 *gpioval = *gpioval & ~(1UL << ipath_gpio_sda_num);
127 *gpioval |= write_val;
129 ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_out, *gpioval);
135 * i2c_gpio_get - get a GPIO line state
136 * @dd: the infinipath device
137 * @line: the line to get
138 * @curr_statep: where to put the line state
140 * Returns 0 if the line was set to the new state successfully, non-zero
141 * on error. curr_state is not set on error.
143 static int i2c_gpio_get(struct ipath_devdata *dd,
145 enum i2c_state *curr_statep)
147 u64 read_val, write_val, mask;
151 if (curr_statep == NULL) {
156 read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extctrl);
157 /* config line to be an input */
158 if (line == i2c_line_scl)
159 mask = ipath_gpio_scl;
161 mask = ipath_gpio_sda;
162 write_val = read_val & ~mask;
163 ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, write_val);
164 read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extstatus);
167 *curr_statep = i2c_line_high;
169 *curr_statep = i2c_line_low;
178 * i2c_wait_for_writes - wait for a write
179 * @dd: the infinipath device
181 * We use this instead of udelay directly, so we can make sure
182 * that previous register writes have been flushed all the way
183 * to the chip. Since we are delaying anyway, the cost doesn't
184 * hurt, and makes the bit twiddling more regular
186 static void i2c_wait_for_writes(struct ipath_devdata *dd)
188 (void)ipath_read_kreg32(dd, dd->ipath_kregs->kr_scratch);
191 static void scl_out(struct ipath_devdata *dd, u8 bit)
193 i2c_gpio_set(dd, i2c_line_scl, bit ? i2c_line_high : i2c_line_low);
195 i2c_wait_for_writes(dd);
198 static void sda_out(struct ipath_devdata *dd, u8 bit)
200 i2c_gpio_set(dd, i2c_line_sda, bit ? i2c_line_high : i2c_line_low);
202 i2c_wait_for_writes(dd);
205 static u8 sda_in(struct ipath_devdata *dd, int wait)
209 if (i2c_gpio_get(dd, i2c_line_sda, &bit))
210 ipath_dbg("get bit failed!\n");
213 i2c_wait_for_writes(dd);
215 return bit == i2c_line_high ? 1U : 0;
219 * i2c_ackrcv - see if ack following write is true
220 * @dd: the infinipath device
222 static int i2c_ackrcv(struct ipath_devdata *dd)
226 /* AT ENTRY SCL = LOW */
227 /* change direction, ignore data */
228 ack_received = sda_in(dd, 1);
229 scl_out(dd, i2c_line_high);
230 ack_received = sda_in(dd, 1) == 0;
231 scl_out(dd, i2c_line_low);
236 * wr_byte - write a byte, one bit at a time
237 * @dd: the infinipath device
238 * @data: the byte to write
240 * Returns 0 if we got the following ack, otherwise 1
242 static int wr_byte(struct ipath_devdata *dd, u8 data)
247 for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {
248 bit = (data >> bit_cntr) & 1;
250 scl_out(dd, i2c_line_high);
251 scl_out(dd, i2c_line_low);
253 return (!i2c_ackrcv(dd)) ? 1 : 0;
256 static void send_ack(struct ipath_devdata *dd)
258 sda_out(dd, i2c_line_low);
259 scl_out(dd, i2c_line_high);
260 scl_out(dd, i2c_line_low);
261 sda_out(dd, i2c_line_high);
265 * i2c_startcmd - transmit the start condition, followed by address/cmd
266 * @dd: the infinipath device
267 * @offset_dir: direction byte
269 * (both clock/data high, clock high, data low while clock is high)
271 static int i2c_startcmd(struct ipath_devdata *dd, u8 offset_dir)
275 /* issue start sequence */
276 sda_out(dd, i2c_line_high);
277 scl_out(dd, i2c_line_high);
278 sda_out(dd, i2c_line_low);
279 scl_out(dd, i2c_line_low);
281 /* issue length and direction byte */
282 res = wr_byte(dd, offset_dir);
285 ipath_cdbg(VERBOSE, "No ack to complete start\n");
291 * stop_cmd - transmit the stop condition
292 * @dd: the infinipath device
294 * (both clock/data low, clock high, data high while clock is high)
296 static void stop_cmd(struct ipath_devdata *dd)
298 scl_out(dd, i2c_line_low);
299 sda_out(dd, i2c_line_low);
300 scl_out(dd, i2c_line_high);
301 sda_out(dd, i2c_line_high);
306 * eeprom_reset - reset I2C communication
307 * @dd: the infinipath device
310 static int eeprom_reset(struct ipath_devdata *dd)
312 int clock_cycles_left = 9;
313 u64 *gpioval = &dd->ipath_gpio_out;
317 *gpioval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_gpio_out);
318 ipath_cdbg(VERBOSE, "Resetting i2c eeprom; initial gpioout reg "
319 "is %llx\n", (unsigned long long) *gpioval);
322 * This is to get the i2c into a known state, by first going low,
323 * then tristate sda (and then tristate scl as first thing
326 scl_out(dd, i2c_line_low);
327 sda_out(dd, i2c_line_high);
329 while (clock_cycles_left--) {
330 scl_out(dd, i2c_line_high);
333 sda_out(dd, i2c_line_low);
334 scl_out(dd, i2c_line_low);
339 scl_out(dd, i2c_line_low);
349 * ipath_eeprom_read - receives bytes from the eeprom via I2C
350 * @dd: the infinipath device
351 * @eeprom_offset: address to read from
352 * @buffer: where to store result
353 * @len: number of bytes to receive
356 int ipath_eeprom_read(struct ipath_devdata *dd, u8 eeprom_offset,
357 void *buffer, int len)
359 /* compiler complains unless initialized */
367 eeprom_offset = (eeprom_offset << 1) | READ_CMD;
369 if (i2c_startcmd(dd, eeprom_offset)) {
370 ipath_dbg("Failed startcmd\n");
377 * eeprom keeps clocking data out as long as we ack, automatically
378 * incrementing the address.
383 for (bit_cntr = 8; bit_cntr; bit_cntr--) {
385 scl_out(dd, i2c_line_high);
387 single_byte |= bit << (bit_cntr - 1);
388 scl_out(dd, i2c_line_low);
391 /* send ack if not the last byte */
395 *((u8 *) buffer) = single_byte;
408 * ipath_eeprom_write - writes data to the eeprom via I2C
409 * @dd: the infinipath device
410 * @eeprom_offset: where to place data
411 * @buffer: data to write
412 * @len: number of bytes to write
414 int ipath_eeprom_write(struct ipath_devdata *dd, u8 eeprom_offset,
415 const void *buffer, int len)
419 const u8 *bp = buffer;
420 int max_wait_time, i;
427 if (i2c_startcmd(dd, (eeprom_offset << 1) | WRITE_CMD)) {
428 ipath_dbg("Failed to start cmd offset %u\n",
433 sub_len = min(len, 4);
434 eeprom_offset += sub_len;
437 for (i = 0; i < sub_len; i++) {
438 if (wr_byte(dd, *bp++)) {
439 ipath_dbg("no ack after byte %u/%u (%u "
440 "total remain)\n", i, sub_len,
449 * wait for write complete by waiting for a successful
450 * read (the chip replies with a zero after the write
451 * cmd completes, and before it writes to the eeprom.
452 * The startcmd for the read will fail the ack until
453 * the writes have completed. We do this inline to avoid
454 * the debug prints that are in the real read routine
455 * if the startcmd fails.
458 while (i2c_startcmd(dd, READ_CMD)) {
460 if (!--max_wait_time) {
461 ipath_dbg("Did not get successful read to "
466 /* now read the zero byte */
467 for (i = single_byte = 0; i < 8; i++) {
469 scl_out(dd, i2c_line_high);
471 scl_out(dd, i2c_line_low);
489 static u8 flash_csum(struct ipath_flash *ifp, int adjust)
494 for (len = 0; len < ifp->if_length; len++)
496 csum -= ifp->if_csum;
505 * ipath_get_guid - get the GUID from the i2c device
506 * @dd: the infinipath device
508 * We have the capability to use the ipath_nguid field, and get
509 * the guid from the first chip's flash, to use for all of them.
511 void ipath_get_eeprom_info(struct ipath_devdata *dd)
514 struct ipath_flash *ifp;
518 int t = dd->ipath_unit;
519 struct ipath_devdata *dd0 = ipath_lookup(0);
521 if (t && dd0->ipath_nguid > 1 && t <= dd0->ipath_nguid) {
523 dd->ipath_guid = dd0->ipath_guid;
524 bguid = (u8 *) & dd->ipath_guid;
528 if (oguid > bguid[7]) {
529 if (bguid[6] == 0xff) {
530 if (bguid[5] == 0xff) {
533 "Can't set %s GUID from "
534 "base, wraps to OUI!\n",
535 ipath_get_unit_name(t));
545 ipath_dbg("nguid %u, so adding %u to device 0 guid, "
548 (unsigned long long) be64_to_cpu(dd->ipath_guid));
552 len = offsetof(struct ipath_flash, if_future);
555 ipath_dev_err(dd, "Couldn't allocate memory to read %u "
556 "bytes from eeprom for GUID\n", len);
560 if (ipath_eeprom_read(dd, 0, buf, len)) {
561 ipath_dev_err(dd, "Failed reading GUID from eeprom\n");
564 ifp = (struct ipath_flash *)buf;
566 csum = flash_csum(ifp, 0);
567 if (csum != ifp->if_csum) {
568 dev_info(&dd->pcidev->dev, "Bad I2C flash checksum: "
569 "0x%x, not 0x%x\n", csum, ifp->if_csum);
572 if (*(__be64 *) ifp->if_guid == 0ULL ||
573 *(__be64 *) ifp->if_guid == __constant_cpu_to_be64(-1LL)) {
574 ipath_dev_err(dd, "Invalid GUID %llx from flash; "
576 *(unsigned long long *) ifp->if_guid);
577 /* don't allow GUID if all 0 or all 1's */
581 /* complain, but allow it */
582 if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
583 dev_info(&dd->pcidev->dev, "Warning, GUID %llx is "
584 "default, probably not correct!\n",
585 *(unsigned long long *) ifp->if_guid);
587 bguid = ifp->if_guid;
588 if (!bguid[0] && !bguid[1] && !bguid[2]) {
589 /* original incorrect GUID format in flash; fix in
590 * core copy, by shifting up 2 octets; don't need to
591 * change top octet, since both it and shifted are
595 bguid[3] = bguid[4] = 0;
596 guid = *(__be64 *) ifp->if_guid;
597 ipath_cdbg(VERBOSE, "Old GUID format in flash, top 3 zero, "
598 "shifting 2 octets\n");
600 guid = *(__be64 *) ifp->if_guid;
601 dd->ipath_guid = guid;
602 dd->ipath_nguid = ifp->if_numguid;
603 memcpy(dd->ipath_serial, ifp->if_serial,
604 sizeof(ifp->if_serial));
605 ipath_cdbg(VERBOSE, "Initted GUID to %llx from eeprom\n",
606 (unsigned long long) be64_to_cpu(dd->ipath_guid));