ipath_eeprom.c 16 KB

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