fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / infiniband / hw / ipath / ipath_eeprom.c
1 /*
2  * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3  * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/delay.h>
35 #include <linux/pci.h>
36 #include <linux/vmalloc.h>
37
38 #include "ipath_kernel.h"
39
40 /*
41  * InfiniPath I2C driver for a serial eeprom.  This is not a generic
42  * I2C interface.  For a start, the device we're using (Atmel AT24C11)
43  * doesn't work like a regular I2C device.  It looks like one
44  * electrically, but not logically.  Normal I2C devices have a single
45  * 7-bit or 10-bit I2C address that they respond to.  Valid 7-bit
46  * addresses range from 0x03 to 0x77.  Addresses 0x00 to 0x02 and 0x78
47  * to 0x7F are special reserved addresses (e.g. 0x00 is the "general
48  * call" address.)  The Atmel device, on the other hand, responds to ALL
49  * 7-bit addresses.  It's designed to be the only device on a given I2C
50  * bus.  A 7-bit address corresponds to the memory address within the
51  * Atmel device itself.
52  *
53  * Also, the timing requirements mean more than simple software
54  * bitbanging, with readbacks from chip to ensure timing (simple udelay
55  * is not enough).
56  *
57  * This all means that accessing the device is specialized enough
58  * that using the standard kernel I2C bitbanging interface would be
59  * impossible.  For example, the core I2C eeprom driver expects to find
60  * a device at one or more of a limited set of addresses only.  It doesn't
61  * allow writing to an eeprom.  It also doesn't provide any means of
62  * accessing eeprom contents from within the kernel, only via sysfs.
63  */
64
65 enum i2c_type {
66         i2c_line_scl = 0,
67         i2c_line_sda
68 };
69
70 enum i2c_state {
71         i2c_line_low = 0,
72         i2c_line_high
73 };
74
75 #define READ_CMD 1
76 #define WRITE_CMD 0
77
78 static int eeprom_init;
79
80 /*
81  * The gpioval manipulation really should be protected by spinlocks
82  * or be converted to use atomic operations.
83  */
84
85 /**
86  * i2c_gpio_set - set a GPIO line
87  * @dd: the infinipath device
88  * @line: the line to set
89  * @new_line_state: the state to set
90  *
91  * Returns 0 if the line was set to the new state successfully, non-zero
92  * on error.
93  */
94 static int i2c_gpio_set(struct ipath_devdata *dd,
95                         enum i2c_type line,
96                         enum i2c_state new_line_state)
97 {
98         u64 read_val, write_val, mask, *gpioval;
99
100         gpioval = &dd->ipath_gpio_out;
101         read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extctrl);
102         if (line == i2c_line_scl)
103                 mask = dd->ipath_gpio_scl;
104         else
105                 mask = dd->ipath_gpio_sda;
106
107         if (new_line_state == i2c_line_high)
108                 /* tri-state the output rather than force high */
109                 write_val = read_val & ~mask;
110         else
111                 /* config line to be an output */
112                 write_val = read_val | mask;
113         ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, write_val);
114
115         /* set high and verify */
116         if (new_line_state == i2c_line_high)
117                 write_val = 0x1UL;
118         else
119                 write_val = 0x0UL;
120
121         if (line == i2c_line_scl) {
122                 write_val <<= dd->ipath_gpio_scl_num;
123                 *gpioval = *gpioval & ~(1UL << dd->ipath_gpio_scl_num);
124                 *gpioval |= write_val;
125         } else {
126                 write_val <<= dd->ipath_gpio_sda_num;
127                 *gpioval = *gpioval & ~(1UL << dd->ipath_gpio_sda_num);
128                 *gpioval |= write_val;
129         }
130         ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_out, *gpioval);
131
132         return 0;
133 }
134
135 /**
136  * i2c_gpio_get - get a GPIO line state
137  * @dd: the infinipath device
138  * @line: the line to get
139  * @curr_statep: where to put the line state
140  *
141  * Returns 0 if the line was set to the new state successfully, non-zero
142  * on error.  curr_state is not set on error.
143  */
144 static int i2c_gpio_get(struct ipath_devdata *dd,
145                         enum i2c_type line,
146                         enum i2c_state *curr_statep)
147 {
148         u64 read_val, write_val, mask;
149         int ret;
150
151         /* check args */
152         if (curr_statep == NULL) {
153                 ret = 1;
154                 goto bail;
155         }
156
157         read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extctrl);
158         /* config line to be an input */
159         if (line == i2c_line_scl)
160                 mask = dd->ipath_gpio_scl;
161         else
162                 mask = dd->ipath_gpio_sda;
163         write_val = read_val & ~mask;
164         ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, write_val);
165         read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extstatus);
166
167         if (read_val & mask)
168                 *curr_statep = i2c_line_high;
169         else
170                 *curr_statep = i2c_line_low;
171
172         ret = 0;
173
174 bail:
175         return ret;
176 }
177
178 /**
179  * i2c_wait_for_writes - wait for a write
180  * @dd: the infinipath device
181  *
182  * We use this instead of udelay directly, so we can make sure
183  * that previous register writes have been flushed all the way
184  * to the chip.  Since we are delaying anyway, the cost doesn't
185  * hurt, and makes the bit twiddling more regular
186  */
187 static void i2c_wait_for_writes(struct ipath_devdata *dd)
188 {
189         (void)ipath_read_kreg32(dd, dd->ipath_kregs->kr_scratch);
190         rmb();
191 }
192
193 static void scl_out(struct ipath_devdata *dd, u8 bit)
194 {
195         i2c_gpio_set(dd, i2c_line_scl, bit ? i2c_line_high : i2c_line_low);
196
197         i2c_wait_for_writes(dd);
198 }
199
200 static void sda_out(struct ipath_devdata *dd, u8 bit)
201 {
202         i2c_gpio_set(dd, i2c_line_sda, bit ? i2c_line_high : i2c_line_low);
203
204         i2c_wait_for_writes(dd);
205 }
206
207 static u8 sda_in(struct ipath_devdata *dd, int wait)
208 {
209         enum i2c_state bit;
210
211         if (i2c_gpio_get(dd, i2c_line_sda, &bit))
212                 ipath_dbg("get bit failed!\n");
213
214         if (wait)
215                 i2c_wait_for_writes(dd);
216
217         return bit == i2c_line_high ? 1U : 0;
218 }
219
220 /**
221  * i2c_ackrcv - see if ack following write is true
222  * @dd: the infinipath device
223  */
224 static int i2c_ackrcv(struct ipath_devdata *dd)
225 {
226         u8 ack_received;
227
228         /* AT ENTRY SCL = LOW */
229         /* change direction, ignore data */
230         ack_received = sda_in(dd, 1);
231         scl_out(dd, i2c_line_high);
232         ack_received = sda_in(dd, 1) == 0;
233         scl_out(dd, i2c_line_low);
234         return ack_received;
235 }
236
237 /**
238  * wr_byte - write a byte, one bit at a time
239  * @dd: the infinipath device
240  * @data: the byte to write
241  *
242  * Returns 0 if we got the following ack, otherwise 1
243  */
244 static int wr_byte(struct ipath_devdata *dd, u8 data)
245 {
246         int bit_cntr;
247         u8 bit;
248
249         for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {
250                 bit = (data >> bit_cntr) & 1;
251                 sda_out(dd, bit);
252                 scl_out(dd, i2c_line_high);
253                 scl_out(dd, i2c_line_low);
254         }
255         return (!i2c_ackrcv(dd)) ? 1 : 0;
256 }
257
258 static void send_ack(struct ipath_devdata *dd)
259 {
260         sda_out(dd, i2c_line_low);
261         scl_out(dd, i2c_line_high);
262         scl_out(dd, i2c_line_low);
263         sda_out(dd, i2c_line_high);
264 }
265
266 /**
267  * i2c_startcmd - transmit the start condition, followed by address/cmd
268  * @dd: the infinipath device
269  * @offset_dir: direction byte
270  *
271  *      (both clock/data high, clock high, data low while clock is high)
272  */
273 static int i2c_startcmd(struct ipath_devdata *dd, u8 offset_dir)
274 {
275         int res;
276
277         /* issue start sequence */
278         sda_out(dd, i2c_line_high);
279         scl_out(dd, i2c_line_high);
280         sda_out(dd, i2c_line_low);
281         scl_out(dd, i2c_line_low);
282
283         /* issue length and direction byte */
284         res = wr_byte(dd, offset_dir);
285
286         if (res)
287                 ipath_cdbg(VERBOSE, "No ack to complete start\n");
288
289         return res;
290 }
291
292 /**
293  * stop_cmd - transmit the stop condition
294  * @dd: the infinipath device
295  *
296  * (both clock/data low, clock high, data high while clock is high)
297  */
298 static void stop_cmd(struct ipath_devdata *dd)
299 {
300         scl_out(dd, i2c_line_low);
301         sda_out(dd, i2c_line_low);
302         scl_out(dd, i2c_line_high);
303         sda_out(dd, i2c_line_high);
304         udelay(2);
305 }
306
307 /**
308  * eeprom_reset - reset I2C communication
309  * @dd: the infinipath device
310  */
311
312 static int eeprom_reset(struct ipath_devdata *dd)
313 {
314         int clock_cycles_left = 9;
315         u64 *gpioval = &dd->ipath_gpio_out;
316         int ret;
317
318         eeprom_init = 1;
319         *gpioval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_gpio_out);
320         ipath_cdbg(VERBOSE, "Resetting i2c eeprom; initial gpioout reg "
321                    "is %llx\n", (unsigned long long) *gpioval);
322
323         /*
324          * This is to get the i2c into a known state, by first going low,
325          * then tristate sda (and then tristate scl as first thing
326          * in loop)
327          */
328         scl_out(dd, i2c_line_low);
329         sda_out(dd, i2c_line_high);
330
331         while (clock_cycles_left--) {
332                 scl_out(dd, i2c_line_high);
333
334                 if (sda_in(dd, 0)) {
335                         sda_out(dd, i2c_line_low);
336                         scl_out(dd, i2c_line_low);
337                         ret = 0;
338                         goto bail;
339                 }
340
341                 scl_out(dd, i2c_line_low);
342         }
343
344         ret = 1;
345
346 bail:
347         return ret;
348 }
349
350 /**
351  * ipath_eeprom_read - receives bytes from the eeprom via I2C
352  * @dd: the infinipath device
353  * @eeprom_offset: address to read from
354  * @buffer: where to store result
355  * @len: number of bytes to receive
356  */
357
358 int ipath_eeprom_read(struct ipath_devdata *dd, u8 eeprom_offset,
359                       void *buffer, int len)
360 {
361         /* compiler complains unless initialized */
362         u8 single_byte = 0;
363         int bit_cntr;
364         int ret;
365
366         if (!eeprom_init)
367                 eeprom_reset(dd);
368
369         eeprom_offset = (eeprom_offset << 1) | READ_CMD;
370
371         if (i2c_startcmd(dd, eeprom_offset)) {
372                 ipath_dbg("Failed startcmd\n");
373                 stop_cmd(dd);
374                 ret = 1;
375                 goto bail;
376         }
377
378         /*
379          * eeprom keeps clocking data out as long as we ack, automatically
380          * incrementing the address.
381          */
382         while (len-- > 0) {
383                 /* get data */
384                 single_byte = 0;
385                 for (bit_cntr = 8; bit_cntr; bit_cntr--) {
386                         u8 bit;
387                         scl_out(dd, i2c_line_high);
388                         bit = sda_in(dd, 0);
389                         single_byte |= bit << (bit_cntr - 1);
390                         scl_out(dd, i2c_line_low);
391                 }
392
393                 /* send ack if not the last byte */
394                 if (len)
395                         send_ack(dd);
396
397                 *((u8 *) buffer) = single_byte;
398                 buffer++;
399         }
400
401         stop_cmd(dd);
402
403         ret = 0;
404
405 bail:
406         return ret;
407 }
408
409 /**
410  * ipath_eeprom_write - writes data to the eeprom via I2C
411  * @dd: the infinipath device
412  * @eeprom_offset: where to place data
413  * @buffer: data to write
414  * @len: number of bytes to write
415  */
416 int ipath_eeprom_write(struct ipath_devdata *dd, u8 eeprom_offset,
417                        const void *buffer, int len)
418 {
419         u8 single_byte;
420         int sub_len;
421         const u8 *bp = buffer;
422         int max_wait_time, i;
423         int ret;
424
425         if (!eeprom_init)
426                 eeprom_reset(dd);
427
428         while (len > 0) {
429                 if (i2c_startcmd(dd, (eeprom_offset << 1) | WRITE_CMD)) {
430                         ipath_dbg("Failed to start cmd offset %u\n",
431                                   eeprom_offset);
432                         goto failed_write;
433                 }
434
435                 sub_len = min(len, 4);
436                 eeprom_offset += sub_len;
437                 len -= sub_len;
438
439                 for (i = 0; i < sub_len; i++) {
440                         if (wr_byte(dd, *bp++)) {
441                                 ipath_dbg("no ack after byte %u/%u (%u "
442                                           "total remain)\n", i, sub_len,
443                                           len + sub_len - i);
444                                 goto failed_write;
445                         }
446                 }
447
448                 stop_cmd(dd);
449
450                 /*
451                  * wait for write complete by waiting for a successful
452                  * read (the chip replies with a zero after the write
453                  * cmd completes, and before it writes to the eeprom.
454                  * The startcmd for the read will fail the ack until
455                  * the writes have completed.   We do this inline to avoid
456                  * the debug prints that are in the real read routine
457                  * if the startcmd fails.
458                  */
459                 max_wait_time = 100;
460                 while (i2c_startcmd(dd, READ_CMD)) {
461                         stop_cmd(dd);
462                         if (!--max_wait_time) {
463                                 ipath_dbg("Did not get successful read to "
464                                           "complete write\n");
465                                 goto failed_write;
466                         }
467                 }
468                 /* now read the zero byte */
469                 for (i = single_byte = 0; i < 8; i++) {
470                         u8 bit;
471                         scl_out(dd, i2c_line_high);
472                         bit = sda_in(dd, 0);
473                         scl_out(dd, i2c_line_low);
474                         single_byte <<= 1;
475                         single_byte |= bit;
476                 }
477                 stop_cmd(dd);
478         }
479
480         ret = 0;
481         goto bail;
482
483 failed_write:
484         stop_cmd(dd);
485         ret = 1;
486
487 bail:
488         return ret;
489 }
490
491 static u8 flash_csum(struct ipath_flash *ifp, int adjust)
492 {
493         u8 *ip = (u8 *) ifp;
494         u8 csum = 0, len;
495
496         for (len = 0; len < ifp->if_length; len++)
497                 csum += *ip++;
498         csum -= ifp->if_csum;
499         csum = ~csum;
500         if (adjust)
501                 ifp->if_csum = csum;
502
503         return csum;
504 }
505
506 /**
507  * ipath_get_guid - get the GUID from the i2c device
508  * @dd: the infinipath device
509  *
510  * We have the capability to use the ipath_nguid field, and get
511  * the guid from the first chip's flash, to use for all of them.
512  */
513 void ipath_get_eeprom_info(struct ipath_devdata *dd)
514 {
515         void *buf;
516         struct ipath_flash *ifp;
517         __be64 guid;
518         int len;
519         u8 csum, *bguid;
520         int t = dd->ipath_unit;
521         struct ipath_devdata *dd0 = ipath_lookup(0);
522
523         if (t && dd0->ipath_nguid > 1 && t <= dd0->ipath_nguid) {
524                 u8 *bguid, oguid;
525                 dd->ipath_guid = dd0->ipath_guid;
526                 bguid = (u8 *) & dd->ipath_guid;
527
528                 oguid = bguid[7];
529                 bguid[7] += t;
530                 if (oguid > bguid[7]) {
531                         if (bguid[6] == 0xff) {
532                                 if (bguid[5] == 0xff) {
533                                         ipath_dev_err(
534                                                 dd,
535                                                 "Can't set %s GUID from "
536                                                 "base, wraps to OUI!\n",
537                                                 ipath_get_unit_name(t));
538                                         dd->ipath_guid = 0;
539                                         goto bail;
540                                 }
541                                 bguid[5]++;
542                         }
543                         bguid[6]++;
544                 }
545                 dd->ipath_nguid = 1;
546
547                 ipath_dbg("nguid %u, so adding %u to device 0 guid, "
548                           "for %llx\n",
549                           dd0->ipath_nguid, t,
550                           (unsigned long long) be64_to_cpu(dd->ipath_guid));
551                 goto bail;
552         }
553
554         len = offsetof(struct ipath_flash, if_future);
555         buf = vmalloc(len);
556         if (!buf) {
557                 ipath_dev_err(dd, "Couldn't allocate memory to read %u "
558                               "bytes from eeprom for GUID\n", len);
559                 goto bail;
560         }
561
562         if (ipath_eeprom_read(dd, 0, buf, len)) {
563                 ipath_dev_err(dd, "Failed reading GUID from eeprom\n");
564                 goto done;
565         }
566         ifp = (struct ipath_flash *)buf;
567
568         csum = flash_csum(ifp, 0);
569         if (csum != ifp->if_csum) {
570                 dev_info(&dd->pcidev->dev, "Bad I2C flash checksum: "
571                          "0x%x, not 0x%x\n", csum, ifp->if_csum);
572                 goto done;
573         }
574         if (*(__be64 *) ifp->if_guid == 0ULL ||
575             *(__be64 *) ifp->if_guid == __constant_cpu_to_be64(-1LL)) {
576                 ipath_dev_err(dd, "Invalid GUID %llx from flash; "
577                               "ignoring\n",
578                               *(unsigned long long *) ifp->if_guid);
579                 /* don't allow GUID if all 0 or all 1's */
580                 goto done;
581         }
582
583         /* complain, but allow it */
584         if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
585                 dev_info(&dd->pcidev->dev, "Warning, GUID %llx is "
586                          "default, probably not correct!\n",
587                          *(unsigned long long *) ifp->if_guid);
588
589         bguid = ifp->if_guid;
590         if (!bguid[0] && !bguid[1] && !bguid[2]) {
591                 /* original incorrect GUID format in flash; fix in
592                  * core copy, by shifting up 2 octets; don't need to
593                  * change top octet, since both it and shifted are
594                  * 0.. */
595                 bguid[1] = bguid[3];
596                 bguid[2] = bguid[4];
597                 bguid[3] = bguid[4] = 0;
598                 guid = *(__be64 *) ifp->if_guid;
599                 ipath_cdbg(VERBOSE, "Old GUID format in flash, top 3 zero, "
600                            "shifting 2 octets\n");
601         } else
602                 guid = *(__be64 *) ifp->if_guid;
603         dd->ipath_guid = guid;
604         dd->ipath_nguid = ifp->if_numguid;
605         /*
606          * Things are slightly complicated by the desire to transparently
607          * support both the Pathscale 10-digit serial number and the QLogic
608          * 13-character version.
609          */
610         if ((ifp->if_fversion > 1) && ifp->if_sprefix[0]
611                 && ((u8 *)ifp->if_sprefix)[0] != 0xFF) {
612                 /* This board has a Serial-prefix, which is stored
613                  * elsewhere for backward-compatibility.
614                  */
615                 char *snp = dd->ipath_serial;
616                 int len;
617                 memcpy(snp, ifp->if_sprefix, sizeof ifp->if_sprefix);
618                 snp[sizeof ifp->if_sprefix] = '\0';
619                 len = strlen(snp);
620                 snp += len;
621                 len = (sizeof dd->ipath_serial) - len;
622                 if (len > sizeof ifp->if_serial) {
623                         len = sizeof ifp->if_serial;
624                 }
625                 memcpy(snp, ifp->if_serial, len);
626         } else
627                 memcpy(dd->ipath_serial, ifp->if_serial,
628                        sizeof ifp->if_serial);
629
630         ipath_cdbg(VERBOSE, "Initted GUID to %llx from eeprom\n",
631                    (unsigned long long) be64_to_cpu(dd->ipath_guid));
632
633 done:
634         vfree(buf);
635
636 bail:;
637 }