ipath_eeprom.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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 = dd->ipath_gpio_scl;
  94. else
  95. mask = dd->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 <<= dd->ipath_gpio_scl_num;
  110. *gpioval = *gpioval & ~(1UL << dd->ipath_gpio_scl_num);
  111. *gpioval |= write_val;
  112. } else {
  113. write_val <<= dd->ipath_gpio_sda_num;
  114. *gpioval = *gpioval & ~(1UL << dd->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 = dd->ipath_gpio_scl;
  144. else
  145. mask = dd->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. rmb();
  170. }
  171. static void scl_out(struct ipath_devdata *dd, u8 bit)
  172. {
  173. i2c_gpio_set(dd, i2c_line_scl, bit ? i2c_line_high : i2c_line_low);
  174. i2c_wait_for_writes(dd);
  175. }
  176. static void sda_out(struct ipath_devdata *dd, u8 bit)
  177. {
  178. i2c_gpio_set(dd, i2c_line_sda, bit ? i2c_line_high : i2c_line_low);
  179. i2c_wait_for_writes(dd);
  180. }
  181. static u8 sda_in(struct ipath_devdata *dd, int wait)
  182. {
  183. enum i2c_state bit;
  184. if (i2c_gpio_get(dd, i2c_line_sda, &bit))
  185. ipath_dbg("get bit failed!\n");
  186. if (wait)
  187. i2c_wait_for_writes(dd);
  188. return bit == i2c_line_high ? 1U : 0;
  189. }
  190. /**
  191. * i2c_ackrcv - see if ack following write is true
  192. * @dd: the infinipath device
  193. */
  194. static int i2c_ackrcv(struct ipath_devdata *dd)
  195. {
  196. u8 ack_received;
  197. /* AT ENTRY SCL = LOW */
  198. /* change direction, ignore data */
  199. ack_received = sda_in(dd, 1);
  200. scl_out(dd, i2c_line_high);
  201. ack_received = sda_in(dd, 1) == 0;
  202. scl_out(dd, i2c_line_low);
  203. return ack_received;
  204. }
  205. /**
  206. * wr_byte - write a byte, one bit at a time
  207. * @dd: the infinipath device
  208. * @data: the byte to write
  209. *
  210. * Returns 0 if we got the following ack, otherwise 1
  211. */
  212. static int wr_byte(struct ipath_devdata *dd, u8 data)
  213. {
  214. int bit_cntr;
  215. u8 bit;
  216. for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {
  217. bit = (data >> bit_cntr) & 1;
  218. sda_out(dd, bit);
  219. scl_out(dd, i2c_line_high);
  220. scl_out(dd, i2c_line_low);
  221. }
  222. return (!i2c_ackrcv(dd)) ? 1 : 0;
  223. }
  224. static void send_ack(struct ipath_devdata *dd)
  225. {
  226. sda_out(dd, i2c_line_low);
  227. scl_out(dd, i2c_line_high);
  228. scl_out(dd, i2c_line_low);
  229. sda_out(dd, i2c_line_high);
  230. }
  231. /**
  232. * i2c_startcmd - transmit the start condition, followed by address/cmd
  233. * @dd: the infinipath device
  234. * @offset_dir: direction byte
  235. *
  236. * (both clock/data high, clock high, data low while clock is high)
  237. */
  238. static int i2c_startcmd(struct ipath_devdata *dd, u8 offset_dir)
  239. {
  240. int res;
  241. /* issue start sequence */
  242. sda_out(dd, i2c_line_high);
  243. scl_out(dd, i2c_line_high);
  244. sda_out(dd, i2c_line_low);
  245. scl_out(dd, i2c_line_low);
  246. /* issue length and direction byte */
  247. res = wr_byte(dd, offset_dir);
  248. if (res)
  249. ipath_cdbg(VERBOSE, "No ack to complete start\n");
  250. return res;
  251. }
  252. /**
  253. * stop_cmd - transmit the stop condition
  254. * @dd: the infinipath device
  255. *
  256. * (both clock/data low, clock high, data high while clock is high)
  257. */
  258. static void stop_cmd(struct ipath_devdata *dd)
  259. {
  260. scl_out(dd, i2c_line_low);
  261. sda_out(dd, i2c_line_low);
  262. scl_out(dd, i2c_line_high);
  263. sda_out(dd, i2c_line_high);
  264. udelay(2);
  265. }
  266. /**
  267. * eeprom_reset - reset I2C communication
  268. * @dd: the infinipath device
  269. */
  270. static int eeprom_reset(struct ipath_devdata *dd)
  271. {
  272. int clock_cycles_left = 9;
  273. u64 *gpioval = &dd->ipath_gpio_out;
  274. int ret;
  275. eeprom_init = 1;
  276. *gpioval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_gpio_out);
  277. ipath_cdbg(VERBOSE, "Resetting i2c eeprom; initial gpioout reg "
  278. "is %llx\n", (unsigned long long) *gpioval);
  279. /*
  280. * This is to get the i2c into a known state, by first going low,
  281. * then tristate sda (and then tristate scl as first thing
  282. * in loop)
  283. */
  284. scl_out(dd, i2c_line_low);
  285. sda_out(dd, i2c_line_high);
  286. while (clock_cycles_left--) {
  287. scl_out(dd, i2c_line_high);
  288. if (sda_in(dd, 0)) {
  289. sda_out(dd, i2c_line_low);
  290. scl_out(dd, i2c_line_low);
  291. ret = 0;
  292. goto bail;
  293. }
  294. scl_out(dd, i2c_line_low);
  295. }
  296. ret = 1;
  297. bail:
  298. return ret;
  299. }
  300. /**
  301. * ipath_eeprom_read - receives bytes from the eeprom via I2C
  302. * @dd: the infinipath device
  303. * @eeprom_offset: address to read from
  304. * @buffer: where to store result
  305. * @len: number of bytes to receive
  306. */
  307. int ipath_eeprom_read(struct ipath_devdata *dd, u8 eeprom_offset,
  308. void *buffer, int len)
  309. {
  310. /* compiler complains unless initialized */
  311. u8 single_byte = 0;
  312. int bit_cntr;
  313. int ret;
  314. if (!eeprom_init)
  315. eeprom_reset(dd);
  316. eeprom_offset = (eeprom_offset << 1) | READ_CMD;
  317. if (i2c_startcmd(dd, eeprom_offset)) {
  318. ipath_dbg("Failed startcmd\n");
  319. stop_cmd(dd);
  320. ret = 1;
  321. goto bail;
  322. }
  323. /*
  324. * eeprom keeps clocking data out as long as we ack, automatically
  325. * incrementing the address.
  326. */
  327. while (len-- > 0) {
  328. /* get data */
  329. single_byte = 0;
  330. for (bit_cntr = 8; bit_cntr; bit_cntr--) {
  331. u8 bit;
  332. scl_out(dd, i2c_line_high);
  333. bit = sda_in(dd, 0);
  334. single_byte |= bit << (bit_cntr - 1);
  335. scl_out(dd, i2c_line_low);
  336. }
  337. /* send ack if not the last byte */
  338. if (len)
  339. send_ack(dd);
  340. *((u8 *) buffer) = single_byte;
  341. buffer++;
  342. }
  343. stop_cmd(dd);
  344. ret = 0;
  345. bail:
  346. return ret;
  347. }
  348. /**
  349. * ipath_eeprom_write - writes data to the eeprom via I2C
  350. * @dd: the infinipath device
  351. * @eeprom_offset: where to place data
  352. * @buffer: data to write
  353. * @len: number of bytes to write
  354. */
  355. int ipath_eeprom_write(struct ipath_devdata *dd, u8 eeprom_offset,
  356. const void *buffer, int len)
  357. {
  358. u8 single_byte;
  359. int sub_len;
  360. const u8 *bp = buffer;
  361. int max_wait_time, i;
  362. int ret;
  363. if (!eeprom_init)
  364. eeprom_reset(dd);
  365. while (len > 0) {
  366. if (i2c_startcmd(dd, (eeprom_offset << 1) | WRITE_CMD)) {
  367. ipath_dbg("Failed to start cmd offset %u\n",
  368. eeprom_offset);
  369. goto failed_write;
  370. }
  371. sub_len = min(len, 4);
  372. eeprom_offset += sub_len;
  373. len -= sub_len;
  374. for (i = 0; i < sub_len; i++) {
  375. if (wr_byte(dd, *bp++)) {
  376. ipath_dbg("no ack after byte %u/%u (%u "
  377. "total remain)\n", i, sub_len,
  378. len + sub_len - i);
  379. goto failed_write;
  380. }
  381. }
  382. stop_cmd(dd);
  383. /*
  384. * wait for write complete by waiting for a successful
  385. * read (the chip replies with a zero after the write
  386. * cmd completes, and before it writes to the eeprom.
  387. * The startcmd for the read will fail the ack until
  388. * the writes have completed. We do this inline to avoid
  389. * the debug prints that are in the real read routine
  390. * if the startcmd fails.
  391. */
  392. max_wait_time = 100;
  393. while (i2c_startcmd(dd, READ_CMD)) {
  394. stop_cmd(dd);
  395. if (!--max_wait_time) {
  396. ipath_dbg("Did not get successful read to "
  397. "complete write\n");
  398. goto failed_write;
  399. }
  400. }
  401. /* now read the zero byte */
  402. for (i = single_byte = 0; i < 8; i++) {
  403. u8 bit;
  404. scl_out(dd, i2c_line_high);
  405. bit = sda_in(dd, 0);
  406. scl_out(dd, i2c_line_low);
  407. single_byte <<= 1;
  408. single_byte |= bit;
  409. }
  410. stop_cmd(dd);
  411. }
  412. ret = 0;
  413. goto bail;
  414. failed_write:
  415. stop_cmd(dd);
  416. ret = 1;
  417. bail:
  418. return ret;
  419. }
  420. static u8 flash_csum(struct ipath_flash *ifp, int adjust)
  421. {
  422. u8 *ip = (u8 *) ifp;
  423. u8 csum = 0, len;
  424. for (len = 0; len < ifp->if_length; len++)
  425. csum += *ip++;
  426. csum -= ifp->if_csum;
  427. csum = ~csum;
  428. if (adjust)
  429. ifp->if_csum = csum;
  430. return csum;
  431. }
  432. /**
  433. * ipath_get_guid - get the GUID from the i2c device
  434. * @dd: the infinipath device
  435. *
  436. * We have the capability to use the ipath_nguid field, and get
  437. * the guid from the first chip's flash, to use for all of them.
  438. */
  439. void ipath_get_eeprom_info(struct ipath_devdata *dd)
  440. {
  441. void *buf;
  442. struct ipath_flash *ifp;
  443. __be64 guid;
  444. int len;
  445. u8 csum, *bguid;
  446. int t = dd->ipath_unit;
  447. struct ipath_devdata *dd0 = ipath_lookup(0);
  448. if (t && dd0->ipath_nguid > 1 && t <= dd0->ipath_nguid) {
  449. u8 *bguid, oguid;
  450. dd->ipath_guid = dd0->ipath_guid;
  451. bguid = (u8 *) & dd->ipath_guid;
  452. oguid = bguid[7];
  453. bguid[7] += t;
  454. if (oguid > bguid[7]) {
  455. if (bguid[6] == 0xff) {
  456. if (bguid[5] == 0xff) {
  457. ipath_dev_err(
  458. dd,
  459. "Can't set %s GUID from "
  460. "base, wraps to OUI!\n",
  461. ipath_get_unit_name(t));
  462. dd->ipath_guid = 0;
  463. goto bail;
  464. }
  465. bguid[5]++;
  466. }
  467. bguid[6]++;
  468. }
  469. dd->ipath_nguid = 1;
  470. ipath_dbg("nguid %u, so adding %u to device 0 guid, "
  471. "for %llx\n",
  472. dd0->ipath_nguid, t,
  473. (unsigned long long) be64_to_cpu(dd->ipath_guid));
  474. goto bail;
  475. }
  476. len = offsetof(struct ipath_flash, if_future);
  477. buf = vmalloc(len);
  478. if (!buf) {
  479. ipath_dev_err(dd, "Couldn't allocate memory to read %u "
  480. "bytes from eeprom for GUID\n", len);
  481. goto bail;
  482. }
  483. if (ipath_eeprom_read(dd, 0, buf, len)) {
  484. ipath_dev_err(dd, "Failed reading GUID from eeprom\n");
  485. goto done;
  486. }
  487. ifp = (struct ipath_flash *)buf;
  488. csum = flash_csum(ifp, 0);
  489. if (csum != ifp->if_csum) {
  490. dev_info(&dd->pcidev->dev, "Bad I2C flash checksum: "
  491. "0x%x, not 0x%x\n", csum, ifp->if_csum);
  492. goto done;
  493. }
  494. if (*(__be64 *) ifp->if_guid == 0ULL ||
  495. *(__be64 *) ifp->if_guid == __constant_cpu_to_be64(-1LL)) {
  496. ipath_dev_err(dd, "Invalid GUID %llx from flash; "
  497. "ignoring\n",
  498. *(unsigned long long *) ifp->if_guid);
  499. /* don't allow GUID if all 0 or all 1's */
  500. goto done;
  501. }
  502. /* complain, but allow it */
  503. if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
  504. dev_info(&dd->pcidev->dev, "Warning, GUID %llx is "
  505. "default, probably not correct!\n",
  506. *(unsigned long long *) ifp->if_guid);
  507. bguid = ifp->if_guid;
  508. if (!bguid[0] && !bguid[1] && !bguid[2]) {
  509. /* original incorrect GUID format in flash; fix in
  510. * core copy, by shifting up 2 octets; don't need to
  511. * change top octet, since both it and shifted are
  512. * 0.. */
  513. bguid[1] = bguid[3];
  514. bguid[2] = bguid[4];
  515. bguid[3] = bguid[4] = 0;
  516. guid = *(__be64 *) ifp->if_guid;
  517. ipath_cdbg(VERBOSE, "Old GUID format in flash, top 3 zero, "
  518. "shifting 2 octets\n");
  519. } else
  520. guid = *(__be64 *) ifp->if_guid;
  521. dd->ipath_guid = guid;
  522. dd->ipath_nguid = ifp->if_numguid;
  523. /*
  524. * Things are slightly complicated by the desire to transparently
  525. * support both the Pathscale 10-digit serial number and the QLogic
  526. * 13-character version.
  527. */
  528. if ((ifp->if_fversion > 1) && ifp->if_sprefix[0]
  529. && ((u8 *)ifp->if_sprefix)[0] != 0xFF) {
  530. /* This board has a Serial-prefix, which is stored
  531. * elsewhere for backward-compatibility.
  532. */
  533. char *snp = dd->ipath_serial;
  534. int len;
  535. memcpy(snp, ifp->if_sprefix, sizeof ifp->if_sprefix);
  536. snp[sizeof ifp->if_sprefix] = '\0';
  537. len = strlen(snp);
  538. snp += len;
  539. len = (sizeof dd->ipath_serial) - len;
  540. if (len > sizeof ifp->if_serial) {
  541. len = sizeof ifp->if_serial;
  542. }
  543. memcpy(snp, ifp->if_serial, len);
  544. } else
  545. memcpy(dd->ipath_serial, ifp->if_serial,
  546. sizeof ifp->if_serial);
  547. ipath_cdbg(VERBOSE, "Initted GUID to %llx from eeprom\n",
  548. (unsigned long long) be64_to_cpu(dd->ipath_guid));
  549. done:
  550. vfree(buf);
  551. bail:;
  552. }