Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 1 | /* |
John Gregor | 87427da | 2007-06-11 10:21:14 -0700 | [diff] [blame] | 2 | * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved. |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 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 | { |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 98 | u64 out_mask, dir_mask, *gpioval; |
| 99 | unsigned long flags = 0; |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 100 | |
| 101 | gpioval = &dd->ipath_gpio_out; |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 102 | |
| 103 | if (line == i2c_line_scl) { |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 104 | dir_mask = dd->ipath_gpio_scl; |
| 105 | out_mask = (1UL << dd->ipath_gpio_scl_num); |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 106 | } else { |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 107 | dir_mask = dd->ipath_gpio_sda; |
| 108 | out_mask = (1UL << dd->ipath_gpio_sda_num); |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 109 | } |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 110 | |
| 111 | spin_lock_irqsave(&dd->ipath_gpio_lock, flags); |
| 112 | if (new_line_state == i2c_line_high) { |
| 113 | /* tri-state the output rather than force high */ |
| 114 | dd->ipath_extctrl &= ~dir_mask; |
| 115 | } else { |
| 116 | /* config line to be an output */ |
| 117 | dd->ipath_extctrl |= dir_mask; |
| 118 | } |
| 119 | ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, dd->ipath_extctrl); |
| 120 | |
| 121 | /* set output as well (no real verify) */ |
| 122 | if (new_line_state == i2c_line_high) |
| 123 | *gpioval |= out_mask; |
| 124 | else |
| 125 | *gpioval &= ~out_mask; |
| 126 | |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 127 | ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_out, *gpioval); |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 128 | spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags); |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * i2c_gpio_get - get a GPIO line state |
| 135 | * @dd: the infinipath device |
| 136 | * @line: the line to get |
| 137 | * @curr_statep: where to put the line state |
| 138 | * |
| 139 | * Returns 0 if the line was set to the new state successfully, non-zero |
| 140 | * on error. curr_state is not set on error. |
| 141 | */ |
| 142 | static int i2c_gpio_get(struct ipath_devdata *dd, |
| 143 | enum i2c_type line, |
| 144 | enum i2c_state *curr_statep) |
| 145 | { |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 146 | u64 read_val, mask; |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 147 | int ret; |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 148 | unsigned long flags = 0; |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 149 | |
| 150 | /* check args */ |
| 151 | if (curr_statep == NULL) { |
| 152 | ret = 1; |
| 153 | goto bail; |
| 154 | } |
| 155 | |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 156 | /* config line to be an input */ |
| 157 | if (line == i2c_line_scl) |
Bryan O'Sullivan | f62fe77 | 2006-09-28 09:00:11 -0700 | [diff] [blame] | 158 | mask = dd->ipath_gpio_scl; |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 159 | else |
Bryan O'Sullivan | f62fe77 | 2006-09-28 09:00:11 -0700 | [diff] [blame] | 160 | mask = dd->ipath_gpio_sda; |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 161 | |
| 162 | spin_lock_irqsave(&dd->ipath_gpio_lock, flags); |
| 163 | dd->ipath_extctrl &= ~mask; |
| 164 | ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, dd->ipath_extctrl); |
| 165 | /* |
| 166 | * Below is very unlikely to reflect true input state if Output |
| 167 | * Enable actually changed. |
| 168 | */ |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 169 | read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extstatus); |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 170 | spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags); |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 171 | |
| 172 | if (read_val & mask) |
| 173 | *curr_statep = i2c_line_high; |
| 174 | else |
| 175 | *curr_statep = i2c_line_low; |
| 176 | |
| 177 | ret = 0; |
| 178 | |
| 179 | bail: |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * i2c_wait_for_writes - wait for a write |
| 185 | * @dd: the infinipath device |
| 186 | * |
| 187 | * We use this instead of udelay directly, so we can make sure |
| 188 | * that previous register writes have been flushed all the way |
| 189 | * to the chip. Since we are delaying anyway, the cost doesn't |
| 190 | * hurt, and makes the bit twiddling more regular |
| 191 | */ |
| 192 | static void i2c_wait_for_writes(struct ipath_devdata *dd) |
| 193 | { |
| 194 | (void)ipath_read_kreg32(dd, dd->ipath_kregs->kr_scratch); |
Bryan O'Sullivan | 1a4e74a | 2006-09-28 09:00:19 -0700 | [diff] [blame] | 195 | rmb(); |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void scl_out(struct ipath_devdata *dd, u8 bit) |
| 199 | { |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 200 | udelay(1); |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 201 | i2c_gpio_set(dd, i2c_line_scl, bit ? i2c_line_high : i2c_line_low); |
| 202 | |
| 203 | i2c_wait_for_writes(dd); |
| 204 | } |
| 205 | |
| 206 | static void sda_out(struct ipath_devdata *dd, u8 bit) |
| 207 | { |
| 208 | i2c_gpio_set(dd, i2c_line_sda, bit ? i2c_line_high : i2c_line_low); |
| 209 | |
| 210 | i2c_wait_for_writes(dd); |
| 211 | } |
| 212 | |
| 213 | static u8 sda_in(struct ipath_devdata *dd, int wait) |
| 214 | { |
| 215 | enum i2c_state bit; |
| 216 | |
| 217 | if (i2c_gpio_get(dd, i2c_line_sda, &bit)) |
| 218 | ipath_dbg("get bit failed!\n"); |
| 219 | |
| 220 | if (wait) |
| 221 | i2c_wait_for_writes(dd); |
| 222 | |
| 223 | return bit == i2c_line_high ? 1U : 0; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * i2c_ackrcv - see if ack following write is true |
| 228 | * @dd: the infinipath device |
| 229 | */ |
| 230 | static int i2c_ackrcv(struct ipath_devdata *dd) |
| 231 | { |
| 232 | u8 ack_received; |
| 233 | |
| 234 | /* AT ENTRY SCL = LOW */ |
| 235 | /* change direction, ignore data */ |
| 236 | ack_received = sda_in(dd, 1); |
| 237 | scl_out(dd, i2c_line_high); |
| 238 | ack_received = sda_in(dd, 1) == 0; |
| 239 | scl_out(dd, i2c_line_low); |
| 240 | return ack_received; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * wr_byte - write a byte, one bit at a time |
| 245 | * @dd: the infinipath device |
| 246 | * @data: the byte to write |
| 247 | * |
| 248 | * Returns 0 if we got the following ack, otherwise 1 |
| 249 | */ |
| 250 | static int wr_byte(struct ipath_devdata *dd, u8 data) |
| 251 | { |
| 252 | int bit_cntr; |
| 253 | u8 bit; |
| 254 | |
| 255 | for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) { |
| 256 | bit = (data >> bit_cntr) & 1; |
| 257 | sda_out(dd, bit); |
| 258 | scl_out(dd, i2c_line_high); |
| 259 | scl_out(dd, i2c_line_low); |
| 260 | } |
| 261 | return (!i2c_ackrcv(dd)) ? 1 : 0; |
| 262 | } |
| 263 | |
| 264 | static void send_ack(struct ipath_devdata *dd) |
| 265 | { |
| 266 | sda_out(dd, i2c_line_low); |
| 267 | scl_out(dd, i2c_line_high); |
| 268 | scl_out(dd, i2c_line_low); |
| 269 | sda_out(dd, i2c_line_high); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * i2c_startcmd - transmit the start condition, followed by address/cmd |
| 274 | * @dd: the infinipath device |
| 275 | * @offset_dir: direction byte |
| 276 | * |
| 277 | * (both clock/data high, clock high, data low while clock is high) |
| 278 | */ |
| 279 | static int i2c_startcmd(struct ipath_devdata *dd, u8 offset_dir) |
| 280 | { |
| 281 | int res; |
| 282 | |
| 283 | /* issue start sequence */ |
| 284 | sda_out(dd, i2c_line_high); |
| 285 | scl_out(dd, i2c_line_high); |
| 286 | sda_out(dd, i2c_line_low); |
| 287 | scl_out(dd, i2c_line_low); |
| 288 | |
| 289 | /* issue length and direction byte */ |
| 290 | res = wr_byte(dd, offset_dir); |
| 291 | |
| 292 | if (res) |
| 293 | ipath_cdbg(VERBOSE, "No ack to complete start\n"); |
| 294 | |
| 295 | return res; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * stop_cmd - transmit the stop condition |
| 300 | * @dd: the infinipath device |
| 301 | * |
| 302 | * (both clock/data low, clock high, data high while clock is high) |
| 303 | */ |
| 304 | static void stop_cmd(struct ipath_devdata *dd) |
| 305 | { |
| 306 | scl_out(dd, i2c_line_low); |
| 307 | sda_out(dd, i2c_line_low); |
| 308 | scl_out(dd, i2c_line_high); |
| 309 | sda_out(dd, i2c_line_high); |
| 310 | udelay(2); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * eeprom_reset - reset I2C communication |
| 315 | * @dd: the infinipath device |
| 316 | */ |
| 317 | |
| 318 | static int eeprom_reset(struct ipath_devdata *dd) |
| 319 | { |
| 320 | int clock_cycles_left = 9; |
| 321 | u64 *gpioval = &dd->ipath_gpio_out; |
| 322 | int ret; |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 323 | unsigned long flags; |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 324 | |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 325 | spin_lock_irqsave(&dd->ipath_gpio_lock, flags); |
| 326 | /* Make sure shadows are consistent */ |
| 327 | dd->ipath_extctrl = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extctrl); |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 328 | *gpioval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_gpio_out); |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 329 | spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags); |
| 330 | |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 331 | ipath_cdbg(VERBOSE, "Resetting i2c eeprom; initial gpioout reg " |
| 332 | "is %llx\n", (unsigned long long) *gpioval); |
| 333 | |
Michael Albaugh | 17b2eb9 | 2007-05-17 07:05:04 -0700 | [diff] [blame] | 334 | eeprom_init = 1; |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 335 | /* |
| 336 | * This is to get the i2c into a known state, by first going low, |
| 337 | * then tristate sda (and then tristate scl as first thing |
| 338 | * in loop) |
| 339 | */ |
| 340 | scl_out(dd, i2c_line_low); |
| 341 | sda_out(dd, i2c_line_high); |
| 342 | |
| 343 | while (clock_cycles_left--) { |
| 344 | scl_out(dd, i2c_line_high); |
| 345 | |
| 346 | if (sda_in(dd, 0)) { |
| 347 | sda_out(dd, i2c_line_low); |
| 348 | scl_out(dd, i2c_line_low); |
| 349 | ret = 0; |
| 350 | goto bail; |
| 351 | } |
| 352 | |
| 353 | scl_out(dd, i2c_line_low); |
| 354 | } |
| 355 | |
| 356 | ret = 1; |
| 357 | |
| 358 | bail: |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * ipath_eeprom_read - receives bytes from the eeprom via I2C |
| 364 | * @dd: the infinipath device |
| 365 | * @eeprom_offset: address to read from |
| 366 | * @buffer: where to store result |
| 367 | * @len: number of bytes to receive |
| 368 | */ |
| 369 | |
Michael Albaugh | aecd3b5 | 2007-05-17 07:26:28 -0700 | [diff] [blame] | 370 | static int ipath_eeprom_internal_read(struct ipath_devdata *dd, |
| 371 | u8 eeprom_offset, void *buffer, int len) |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 372 | { |
| 373 | /* compiler complains unless initialized */ |
| 374 | u8 single_byte = 0; |
| 375 | int bit_cntr; |
| 376 | int ret; |
| 377 | |
| 378 | if (!eeprom_init) |
| 379 | eeprom_reset(dd); |
| 380 | |
| 381 | eeprom_offset = (eeprom_offset << 1) | READ_CMD; |
| 382 | |
| 383 | if (i2c_startcmd(dd, eeprom_offset)) { |
| 384 | ipath_dbg("Failed startcmd\n"); |
| 385 | stop_cmd(dd); |
| 386 | ret = 1; |
| 387 | goto bail; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * eeprom keeps clocking data out as long as we ack, automatically |
| 392 | * incrementing the address. |
| 393 | */ |
| 394 | while (len-- > 0) { |
| 395 | /* get data */ |
| 396 | single_byte = 0; |
| 397 | for (bit_cntr = 8; bit_cntr; bit_cntr--) { |
| 398 | u8 bit; |
| 399 | scl_out(dd, i2c_line_high); |
| 400 | bit = sda_in(dd, 0); |
| 401 | single_byte |= bit << (bit_cntr - 1); |
| 402 | scl_out(dd, i2c_line_low); |
| 403 | } |
| 404 | |
| 405 | /* send ack if not the last byte */ |
| 406 | if (len) |
| 407 | send_ack(dd); |
| 408 | |
| 409 | *((u8 *) buffer) = single_byte; |
| 410 | buffer++; |
| 411 | } |
| 412 | |
| 413 | stop_cmd(dd); |
| 414 | |
| 415 | ret = 0; |
| 416 | |
| 417 | bail: |
| 418 | return ret; |
| 419 | } |
| 420 | |
Michael Albaugh | aecd3b5 | 2007-05-17 07:26:28 -0700 | [diff] [blame] | 421 | |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 422 | /** |
| 423 | * ipath_eeprom_write - writes data to the eeprom via I2C |
| 424 | * @dd: the infinipath device |
| 425 | * @eeprom_offset: where to place data |
| 426 | * @buffer: data to write |
| 427 | * @len: number of bytes to write |
| 428 | */ |
Michael Albaugh | aecd3b5 | 2007-05-17 07:26:28 -0700 | [diff] [blame] | 429 | int ipath_eeprom_internal_write(struct ipath_devdata *dd, u8 eeprom_offset, |
| 430 | const void *buffer, int len) |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 431 | { |
| 432 | u8 single_byte; |
| 433 | int sub_len; |
| 434 | const u8 *bp = buffer; |
| 435 | int max_wait_time, i; |
| 436 | int ret; |
| 437 | |
| 438 | if (!eeprom_init) |
| 439 | eeprom_reset(dd); |
| 440 | |
| 441 | while (len > 0) { |
| 442 | if (i2c_startcmd(dd, (eeprom_offset << 1) | WRITE_CMD)) { |
| 443 | ipath_dbg("Failed to start cmd offset %u\n", |
| 444 | eeprom_offset); |
| 445 | goto failed_write; |
| 446 | } |
| 447 | |
| 448 | sub_len = min(len, 4); |
| 449 | eeprom_offset += sub_len; |
| 450 | len -= sub_len; |
| 451 | |
| 452 | for (i = 0; i < sub_len; i++) { |
| 453 | if (wr_byte(dd, *bp++)) { |
| 454 | ipath_dbg("no ack after byte %u/%u (%u " |
| 455 | "total remain)\n", i, sub_len, |
| 456 | len + sub_len - i); |
| 457 | goto failed_write; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | stop_cmd(dd); |
| 462 | |
| 463 | /* |
| 464 | * wait for write complete by waiting for a successful |
| 465 | * read (the chip replies with a zero after the write |
| 466 | * cmd completes, and before it writes to the eeprom. |
| 467 | * The startcmd for the read will fail the ack until |
| 468 | * the writes have completed. We do this inline to avoid |
| 469 | * the debug prints that are in the real read routine |
| 470 | * if the startcmd fails. |
| 471 | */ |
| 472 | max_wait_time = 100; |
| 473 | while (i2c_startcmd(dd, READ_CMD)) { |
| 474 | stop_cmd(dd); |
| 475 | if (!--max_wait_time) { |
| 476 | ipath_dbg("Did not get successful read to " |
| 477 | "complete write\n"); |
| 478 | goto failed_write; |
| 479 | } |
| 480 | } |
| 481 | /* now read the zero byte */ |
| 482 | for (i = single_byte = 0; i < 8; i++) { |
| 483 | u8 bit; |
| 484 | scl_out(dd, i2c_line_high); |
| 485 | bit = sda_in(dd, 0); |
| 486 | scl_out(dd, i2c_line_low); |
| 487 | single_byte <<= 1; |
| 488 | single_byte |= bit; |
| 489 | } |
| 490 | stop_cmd(dd); |
| 491 | } |
| 492 | |
| 493 | ret = 0; |
| 494 | goto bail; |
| 495 | |
| 496 | failed_write: |
| 497 | stop_cmd(dd); |
| 498 | ret = 1; |
| 499 | |
| 500 | bail: |
| 501 | return ret; |
| 502 | } |
| 503 | |
Michael Albaugh | aecd3b5 | 2007-05-17 07:26:28 -0700 | [diff] [blame] | 504 | /* |
| 505 | * The public entry-points ipath_eeprom_read() and ipath_eeprom_write() |
| 506 | * are now just wrappers around the internal functions. |
| 507 | */ |
| 508 | int ipath_eeprom_read(struct ipath_devdata *dd, u8 eeprom_offset, |
| 509 | void *buff, int len) |
| 510 | { |
| 511 | int ret; |
| 512 | |
| 513 | ret = down_interruptible(&dd->ipath_eep_sem); |
| 514 | if (!ret) { |
| 515 | ret = ipath_eeprom_internal_read(dd, eeprom_offset, buff, len); |
| 516 | up(&dd->ipath_eep_sem); |
| 517 | } |
| 518 | |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | int ipath_eeprom_write(struct ipath_devdata *dd, u8 eeprom_offset, |
| 523 | const void *buff, int len) |
| 524 | { |
| 525 | int ret; |
| 526 | |
| 527 | ret = down_interruptible(&dd->ipath_eep_sem); |
| 528 | if (!ret) { |
| 529 | ret = ipath_eeprom_internal_write(dd, eeprom_offset, buff, len); |
| 530 | up(&dd->ipath_eep_sem); |
| 531 | } |
| 532 | |
| 533 | return ret; |
| 534 | } |
| 535 | |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 536 | static u8 flash_csum(struct ipath_flash *ifp, int adjust) |
| 537 | { |
| 538 | u8 *ip = (u8 *) ifp; |
| 539 | u8 csum = 0, len; |
| 540 | |
| 541 | for (len = 0; len < ifp->if_length; len++) |
| 542 | csum += *ip++; |
| 543 | csum -= ifp->if_csum; |
| 544 | csum = ~csum; |
| 545 | if (adjust) |
| 546 | ifp->if_csum = csum; |
| 547 | |
| 548 | return csum; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * ipath_get_guid - get the GUID from the i2c device |
| 553 | * @dd: the infinipath device |
| 554 | * |
Bryan O'Sullivan | f2080fa | 2006-05-23 11:32:34 -0700 | [diff] [blame] | 555 | * We have the capability to use the ipath_nguid field, and get |
| 556 | * the guid from the first chip's flash, to use for all of them. |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 557 | */ |
Bryan O'Sullivan | f2080fa | 2006-05-23 11:32:34 -0700 | [diff] [blame] | 558 | void ipath_get_eeprom_info(struct ipath_devdata *dd) |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 559 | { |
| 560 | void *buf; |
| 561 | struct ipath_flash *ifp; |
| 562 | __be64 guid; |
Michael Albaugh | aecd3b5 | 2007-05-17 07:26:28 -0700 | [diff] [blame] | 563 | int len, eep_stat; |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 564 | u8 csum, *bguid; |
| 565 | int t = dd->ipath_unit; |
| 566 | struct ipath_devdata *dd0 = ipath_lookup(0); |
| 567 | |
| 568 | if (t && dd0->ipath_nguid > 1 && t <= dd0->ipath_nguid) { |
| 569 | u8 *bguid, oguid; |
| 570 | dd->ipath_guid = dd0->ipath_guid; |
| 571 | bguid = (u8 *) & dd->ipath_guid; |
| 572 | |
| 573 | oguid = bguid[7]; |
| 574 | bguid[7] += t; |
| 575 | if (oguid > bguid[7]) { |
| 576 | if (bguid[6] == 0xff) { |
| 577 | if (bguid[5] == 0xff) { |
| 578 | ipath_dev_err( |
| 579 | dd, |
| 580 | "Can't set %s GUID from " |
| 581 | "base, wraps to OUI!\n", |
| 582 | ipath_get_unit_name(t)); |
| 583 | dd->ipath_guid = 0; |
| 584 | goto bail; |
| 585 | } |
| 586 | bguid[5]++; |
| 587 | } |
| 588 | bguid[6]++; |
| 589 | } |
| 590 | dd->ipath_nguid = 1; |
| 591 | |
| 592 | ipath_dbg("nguid %u, so adding %u to device 0 guid, " |
| 593 | "for %llx\n", |
| 594 | dd0->ipath_nguid, t, |
| 595 | (unsigned long long) be64_to_cpu(dd->ipath_guid)); |
| 596 | goto bail; |
| 597 | } |
| 598 | |
| 599 | len = offsetof(struct ipath_flash, if_future); |
| 600 | buf = vmalloc(len); |
| 601 | if (!buf) { |
| 602 | ipath_dev_err(dd, "Couldn't allocate memory to read %u " |
| 603 | "bytes from eeprom for GUID\n", len); |
| 604 | goto bail; |
| 605 | } |
| 606 | |
Michael Albaugh | aecd3b5 | 2007-05-17 07:26:28 -0700 | [diff] [blame] | 607 | down(&dd->ipath_eep_sem); |
| 608 | eep_stat = ipath_eeprom_internal_read(dd, 0, buf, len); |
| 609 | up(&dd->ipath_eep_sem); |
| 610 | |
| 611 | if (eep_stat) { |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 612 | ipath_dev_err(dd, "Failed reading GUID from eeprom\n"); |
| 613 | goto done; |
| 614 | } |
| 615 | ifp = (struct ipath_flash *)buf; |
| 616 | |
| 617 | csum = flash_csum(ifp, 0); |
| 618 | if (csum != ifp->if_csum) { |
| 619 | dev_info(&dd->pcidev->dev, "Bad I2C flash checksum: " |
| 620 | "0x%x, not 0x%x\n", csum, ifp->if_csum); |
| 621 | goto done; |
| 622 | } |
| 623 | if (*(__be64 *) ifp->if_guid == 0ULL || |
| 624 | *(__be64 *) ifp->if_guid == __constant_cpu_to_be64(-1LL)) { |
| 625 | ipath_dev_err(dd, "Invalid GUID %llx from flash; " |
| 626 | "ignoring\n", |
| 627 | *(unsigned long long *) ifp->if_guid); |
| 628 | /* don't allow GUID if all 0 or all 1's */ |
| 629 | goto done; |
| 630 | } |
| 631 | |
| 632 | /* complain, but allow it */ |
| 633 | if (*(u64 *) ifp->if_guid == 0x100007511000000ULL) |
| 634 | dev_info(&dd->pcidev->dev, "Warning, GUID %llx is " |
| 635 | "default, probably not correct!\n", |
| 636 | *(unsigned long long *) ifp->if_guid); |
| 637 | |
| 638 | bguid = ifp->if_guid; |
| 639 | if (!bguid[0] && !bguid[1] && !bguid[2]) { |
| 640 | /* original incorrect GUID format in flash; fix in |
| 641 | * core copy, by shifting up 2 octets; don't need to |
| 642 | * change top octet, since both it and shifted are |
| 643 | * 0.. */ |
| 644 | bguid[1] = bguid[3]; |
| 645 | bguid[2] = bguid[4]; |
| 646 | bguid[3] = bguid[4] = 0; |
| 647 | guid = *(__be64 *) ifp->if_guid; |
| 648 | ipath_cdbg(VERBOSE, "Old GUID format in flash, top 3 zero, " |
| 649 | "shifting 2 octets\n"); |
| 650 | } else |
| 651 | guid = *(__be64 *) ifp->if_guid; |
| 652 | dd->ipath_guid = guid; |
| 653 | dd->ipath_nguid = ifp->if_numguid; |
Bryan O'Sullivan | 8307c28 | 2006-07-01 04:36:13 -0700 | [diff] [blame] | 654 | /* |
| 655 | * Things are slightly complicated by the desire to transparently |
| 656 | * support both the Pathscale 10-digit serial number and the QLogic |
| 657 | * 13-character version. |
| 658 | */ |
| 659 | if ((ifp->if_fversion > 1) && ifp->if_sprefix[0] |
| 660 | && ((u8 *)ifp->if_sprefix)[0] != 0xFF) { |
| 661 | /* This board has a Serial-prefix, which is stored |
| 662 | * elsewhere for backward-compatibility. |
| 663 | */ |
| 664 | char *snp = dd->ipath_serial; |
| 665 | int len; |
| 666 | memcpy(snp, ifp->if_sprefix, sizeof ifp->if_sprefix); |
| 667 | snp[sizeof ifp->if_sprefix] = '\0'; |
| 668 | len = strlen(snp); |
| 669 | snp += len; |
| 670 | len = (sizeof dd->ipath_serial) - len; |
| 671 | if (len > sizeof ifp->if_serial) { |
| 672 | len = sizeof ifp->if_serial; |
| 673 | } |
| 674 | memcpy(snp, ifp->if_serial, len); |
| 675 | } else |
| 676 | memcpy(dd->ipath_serial, ifp->if_serial, |
| 677 | sizeof ifp->if_serial); |
Bryan O'Sullivan | 9783ab4 | 2007-03-15 14:45:07 -0700 | [diff] [blame] | 678 | if (!strstr(ifp->if_comment, "Tested successfully")) |
| 679 | ipath_dev_err(dd, "Board SN %s did not pass functional " |
| 680 | "test: %s\n", dd->ipath_serial, |
| 681 | ifp->if_comment); |
Bryan O'Sullivan | 8307c28 | 2006-07-01 04:36:13 -0700 | [diff] [blame] | 682 | |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 683 | ipath_cdbg(VERBOSE, "Initted GUID to %llx from eeprom\n", |
| 684 | (unsigned long long) be64_to_cpu(dd->ipath_guid)); |
| 685 | |
Michael Albaugh | aecd3b5 | 2007-05-17 07:26:28 -0700 | [diff] [blame] | 686 | memcpy(&dd->ipath_eep_st_errs, &ifp->if_errcntp, IPATH_EEP_LOG_CNT); |
| 687 | /* |
| 688 | * Power-on (actually "active") hours are kept as little-endian value |
| 689 | * in EEPROM, but as seconds in a (possibly as small as 24-bit) |
| 690 | * atomic_t while running. |
| 691 | */ |
| 692 | atomic_set(&dd->ipath_active_time, 0); |
| 693 | dd->ipath_eep_hrs = ifp->if_powerhour[0] | (ifp->if_powerhour[1] << 8); |
| 694 | |
Bryan O'Sullivan | 108ecf0 | 2006-03-29 15:23:29 -0800 | [diff] [blame] | 695 | done: |
| 696 | vfree(buf); |
| 697 | |
| 698 | bail:; |
| 699 | } |
Michael Albaugh | aecd3b5 | 2007-05-17 07:26:28 -0700 | [diff] [blame] | 700 | |
| 701 | /** |
| 702 | * ipath_update_eeprom_log - copy active-time and error counters to eeprom |
| 703 | * @dd: the infinipath device |
| 704 | * |
| 705 | * Although the time is kept as seconds in the ipath_devdata struct, it is |
| 706 | * rounded to hours for re-write, as we have only 16 bits in EEPROM. |
| 707 | * First-cut code reads whole (expected) struct ipath_flash, modifies, |
| 708 | * re-writes. Future direction: read/write only what we need, assuming |
| 709 | * that the EEPROM had to have been "good enough" for driver init, and |
| 710 | * if not, we aren't making it worse. |
| 711 | * |
| 712 | */ |
| 713 | |
| 714 | int ipath_update_eeprom_log(struct ipath_devdata *dd) |
| 715 | { |
| 716 | void *buf; |
| 717 | struct ipath_flash *ifp; |
| 718 | int len, hi_water; |
| 719 | uint32_t new_time, new_hrs; |
| 720 | u8 csum; |
| 721 | int ret, idx; |
| 722 | unsigned long flags; |
| 723 | |
| 724 | /* first, check if we actually need to do anything. */ |
| 725 | ret = 0; |
| 726 | for (idx = 0; idx < IPATH_EEP_LOG_CNT; ++idx) { |
| 727 | if (dd->ipath_eep_st_new_errs[idx]) { |
| 728 | ret = 1; |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | new_time = atomic_read(&dd->ipath_active_time); |
| 733 | |
| 734 | if (ret == 0 && new_time < 3600) |
| 735 | return 0; |
| 736 | |
| 737 | /* |
| 738 | * The quick-check above determined that there is something worthy |
| 739 | * of logging, so get current contents and do a more detailed idea. |
| 740 | */ |
| 741 | len = offsetof(struct ipath_flash, if_future); |
| 742 | buf = vmalloc(len); |
| 743 | ret = 1; |
| 744 | if (!buf) { |
| 745 | ipath_dev_err(dd, "Couldn't allocate memory to read %u " |
| 746 | "bytes from eeprom for logging\n", len); |
| 747 | goto bail; |
| 748 | } |
| 749 | |
| 750 | /* Grab semaphore and read current EEPROM. If we get an |
| 751 | * error, let go, but if not, keep it until we finish write. |
| 752 | */ |
| 753 | ret = down_interruptible(&dd->ipath_eep_sem); |
| 754 | if (ret) { |
| 755 | ipath_dev_err(dd, "Unable to acquire EEPROM for logging\n"); |
| 756 | goto free_bail; |
| 757 | } |
| 758 | ret = ipath_eeprom_internal_read(dd, 0, buf, len); |
| 759 | if (ret) { |
| 760 | up(&dd->ipath_eep_sem); |
| 761 | ipath_dev_err(dd, "Unable read EEPROM for logging\n"); |
| 762 | goto free_bail; |
| 763 | } |
| 764 | ifp = (struct ipath_flash *)buf; |
| 765 | |
| 766 | csum = flash_csum(ifp, 0); |
| 767 | if (csum != ifp->if_csum) { |
| 768 | up(&dd->ipath_eep_sem); |
| 769 | ipath_dev_err(dd, "EEPROM cks err (0x%02X, S/B 0x%02X)\n", |
| 770 | csum, ifp->if_csum); |
| 771 | ret = 1; |
| 772 | goto free_bail; |
| 773 | } |
| 774 | hi_water = 0; |
| 775 | spin_lock_irqsave(&dd->ipath_eep_st_lock, flags); |
| 776 | for (idx = 0; idx < IPATH_EEP_LOG_CNT; ++idx) { |
| 777 | int new_val = dd->ipath_eep_st_new_errs[idx]; |
| 778 | if (new_val) { |
| 779 | /* |
| 780 | * If we have seen any errors, add to EEPROM values |
| 781 | * We need to saturate at 0xFF (255) and we also |
| 782 | * would need to adjust the checksum if we were |
| 783 | * trying to minimize EEPROM traffic |
| 784 | * Note that we add to actual current count in EEPROM, |
| 785 | * in case it was altered while we were running. |
| 786 | */ |
| 787 | new_val += ifp->if_errcntp[idx]; |
| 788 | if (new_val > 0xFF) |
| 789 | new_val = 0xFF; |
| 790 | if (ifp->if_errcntp[idx] != new_val) { |
| 791 | ifp->if_errcntp[idx] = new_val; |
| 792 | hi_water = offsetof(struct ipath_flash, |
| 793 | if_errcntp) + idx; |
| 794 | } |
| 795 | /* |
| 796 | * update our shadow (used to minimize EEPROM |
| 797 | * traffic), to match what we are about to write. |
| 798 | */ |
| 799 | dd->ipath_eep_st_errs[idx] = new_val; |
| 800 | dd->ipath_eep_st_new_errs[idx] = 0; |
| 801 | } |
| 802 | } |
| 803 | /* |
| 804 | * now update active-time. We would like to round to the nearest hour |
| 805 | * but unless atomic_t are sure to be proper signed ints we cannot, |
| 806 | * because we need to account for what we "transfer" to EEPROM and |
| 807 | * if we log an hour at 31 minutes, then we would need to set |
| 808 | * active_time to -29 to accurately count the _next_ hour. |
| 809 | */ |
| 810 | if (new_time > 3600) { |
| 811 | new_hrs = new_time / 3600; |
| 812 | atomic_sub((new_hrs * 3600), &dd->ipath_active_time); |
| 813 | new_hrs += dd->ipath_eep_hrs; |
| 814 | if (new_hrs > 0xFFFF) |
| 815 | new_hrs = 0xFFFF; |
| 816 | dd->ipath_eep_hrs = new_hrs; |
| 817 | if ((new_hrs & 0xFF) != ifp->if_powerhour[0]) { |
| 818 | ifp->if_powerhour[0] = new_hrs & 0xFF; |
| 819 | hi_water = offsetof(struct ipath_flash, if_powerhour); |
| 820 | } |
| 821 | if ((new_hrs >> 8) != ifp->if_powerhour[1]) { |
| 822 | ifp->if_powerhour[1] = new_hrs >> 8; |
| 823 | hi_water = offsetof(struct ipath_flash, if_powerhour) |
| 824 | + 1; |
| 825 | } |
| 826 | } |
| 827 | /* |
| 828 | * There is a tiny possibility that we could somehow fail to write |
| 829 | * the EEPROM after updating our shadows, but problems from holding |
| 830 | * the spinlock too long are a much bigger issue. |
| 831 | */ |
| 832 | spin_unlock_irqrestore(&dd->ipath_eep_st_lock, flags); |
| 833 | if (hi_water) { |
| 834 | /* we made some change to the data, uopdate cksum and write */ |
| 835 | csum = flash_csum(ifp, 1); |
| 836 | ret = ipath_eeprom_internal_write(dd, 0, buf, hi_water + 1); |
| 837 | } |
| 838 | up(&dd->ipath_eep_sem); |
| 839 | if (ret) |
| 840 | ipath_dev_err(dd, "Failed updating EEPROM\n"); |
| 841 | |
| 842 | free_bail: |
| 843 | vfree(buf); |
| 844 | bail: |
| 845 | return ret; |
| 846 | |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * ipath_inc_eeprom_err - increment one of the four error counters |
| 851 | * that are logged to EEPROM. |
| 852 | * @dd: the infinipath device |
| 853 | * @eidx: 0..3, the counter to increment |
| 854 | * @incr: how much to add |
| 855 | * |
| 856 | * Each counter is 8-bits, and saturates at 255 (0xFF). They |
| 857 | * are copied to the EEPROM (aka flash) whenever ipath_update_eeprom_log() |
| 858 | * is called, but it can only be called in a context that allows sleep. |
| 859 | * This function can be called even at interrupt level. |
| 860 | */ |
| 861 | |
| 862 | void ipath_inc_eeprom_err(struct ipath_devdata *dd, u32 eidx, u32 incr) |
| 863 | { |
| 864 | uint new_val; |
| 865 | unsigned long flags; |
| 866 | |
| 867 | spin_lock_irqsave(&dd->ipath_eep_st_lock, flags); |
| 868 | new_val = dd->ipath_eep_st_new_errs[eidx] + incr; |
| 869 | if (new_val > 255) |
| 870 | new_val = 255; |
| 871 | dd->ipath_eep_st_new_errs[eidx] = new_val; |
| 872 | spin_unlock_irqrestore(&dd->ipath_eep_st_lock, flags); |
| 873 | return; |
| 874 | } |