ipath_eeprom.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/delay.h>
  33. #include <linux/pci.h>
  34. #include <linux/vmalloc.h>
  35. #include "ipath_kernel.h"
  36. /*
  37. * InfiniPath I2C driver for a serial eeprom. This is not a generic
  38. * I2C interface. For a start, the device we're using (Atmel AT24C11)
  39. * doesn't work like a regular I2C device. It looks like one
  40. * electrically, but not logically. Normal I2C devices have a single
  41. * 7-bit or 10-bit I2C address that they respond to. Valid 7-bit
  42. * addresses range from 0x03 to 0x77. Addresses 0x00 to 0x02 and 0x78
  43. * to 0x7F are special reserved addresses (e.g. 0x00 is the "general
  44. * call" address.) The Atmel device, on the other hand, responds to ALL
  45. * 7-bit addresses. It's designed to be the only device on a given I2C
  46. * bus. A 7-bit address corresponds to the memory address within the
  47. * Atmel device itself.
  48. *
  49. * Also, the timing requirements mean more than simple software
  50. * bitbanging, with readbacks from chip to ensure timing (simple udelay
  51. * is not enough).
  52. *
  53. * This all means that accessing the device is specialized enough
  54. * that using the standard kernel I2C bitbanging interface would be
  55. * impossible. For example, the core I2C eeprom driver expects to find
  56. * a device at one or more of a limited set of addresses only. It doesn't
  57. * allow writing to an eeprom. It also doesn't provide any means of
  58. * accessing eeprom contents from within the kernel, only via sysfs.
  59. */
  60. enum i2c_type {
  61. i2c_line_scl = 0,
  62. i2c_line_sda
  63. };
  64. enum i2c_state {
  65. i2c_line_low = 0,
  66. i2c_line_high
  67. };
  68. #define READ_CMD 1
  69. #define WRITE_CMD 0
  70. static int eeprom_init;
  71. /*
  72. * The gpioval manipulation really should be protected by spinlocks
  73. * or be converted to use atomic operations.
  74. */
  75. /**
  76. * i2c_gpio_set - set a GPIO line
  77. * @dd: the infinipath device
  78. * @line: the line to set
  79. * @new_line_state: the state to set
  80. *
  81. * Returns 0 if the line was set to the new state successfully, non-zero
  82. * on error.
  83. */
  84. static int i2c_gpio_set(struct ipath_devdata *dd,
  85. enum i2c_type line,
  86. enum i2c_state new_line_state)
  87. {
  88. u64 read_val, write_val, mask, *gpioval;
  89. gpioval = &dd->ipath_gpio_out;
  90. read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extctrl);
  91. if (line == i2c_line_scl)
  92. mask = ipath_gpio_scl;
  93. else
  94. mask = ipath_gpio_sda;
  95. if (new_line_state == i2c_line_high)
  96. /* tri-state the output rather than force high */
  97. write_val = read_val & ~mask;
  98. else
  99. /* config line to be an output */
  100. write_val = read_val | mask;
  101. ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, write_val);
  102. /* set high and verify */
  103. if (new_line_state == i2c_line_high)
  104. write_val = 0x1UL;
  105. else
  106. write_val = 0x0UL;
  107. if (line == i2c_line_scl) {
  108. write_val <<= ipath_gpio_scl_num;
  109. *gpioval = *gpioval & ~(1UL << ipath_gpio_scl_num);
  110. *gpioval |= write_val;
  111. } else {
  112. write_val <<= ipath_gpio_sda_num;
  113. *gpioval = *gpioval & ~(1UL << ipath_gpio_sda_num);
  114. *gpioval |= write_val;
  115. }
  116. ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_out, *gpioval);
  117. return 0;
  118. }
  119. /**
  120. * i2c_gpio_get - get a GPIO line state
  121. * @dd: the infinipath device
  122. * @line: the line to get
  123. * @curr_statep: where to put the line state
  124. *
  125. * Returns 0 if the line was set to the new state successfully, non-zero
  126. * on error. curr_state is not set on error.
  127. */
  128. static int i2c_gpio_get(struct ipath_devdata *dd,
  129. enum i2c_type line,
  130. enum i2c_state *curr_statep)
  131. {
  132. u64 read_val, write_val, mask;
  133. int ret;
  134. /* check args */
  135. if (curr_statep == NULL) {
  136. ret = 1;
  137. goto bail;
  138. }
  139. read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extctrl);
  140. /* config line to be an input */
  141. if (line == i2c_line_scl)
  142. mask = ipath_gpio_scl;
  143. else
  144. mask = ipath_gpio_sda;
  145. write_val = read_val & ~mask;
  146. ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, write_val);
  147. read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extstatus);
  148. if (read_val & mask)
  149. *curr_statep = i2c_line_high;
  150. else
  151. *curr_statep = i2c_line_low;
  152. ret = 0;
  153. bail:
  154. return ret;
  155. }
  156. /**
  157. * i2c_wait_for_writes - wait for a write
  158. * @dd: the infinipath device
  159. *
  160. * We use this instead of udelay directly, so we can make sure
  161. * that previous register writes have been flushed all the way
  162. * to the chip. Since we are delaying anyway, the cost doesn't
  163. * hurt, and makes the bit twiddling more regular
  164. */
  165. static void i2c_wait_for_writes(struct ipath_devdata *dd)
  166. {
  167. (void)ipath_read_kreg32(dd, dd->ipath_kregs->kr_scratch);
  168. }
  169. static void scl_out(struct ipath_devdata *dd, u8 bit)
  170. {
  171. i2c_gpio_set(dd, i2c_line_scl, bit ? i2c_line_high : i2c_line_low);
  172. i2c_wait_for_writes(dd);
  173. }
  174. static void sda_out(struct ipath_devdata *dd, u8 bit)
  175. {
  176. i2c_gpio_set(dd, i2c_line_sda, bit ? i2c_line_high : i2c_line_low);
  177. i2c_wait_for_writes(dd);
  178. }
  179. static u8 sda_in(struct ipath_devdata *dd, int wait)
  180. {
  181. enum i2c_state bit;
  182. if (i2c_gpio_get(dd, i2c_line_sda, &bit))
  183. ipath_dbg("get bit failed!\n");
  184. if (wait)
  185. i2c_wait_for_writes(dd);
  186. return bit == i2c_line_high ? 1U : 0;
  187. }
  188. /**
  189. * i2c_ackrcv - see if ack following write is true
  190. * @dd: the infinipath device
  191. */
  192. static int i2c_ackrcv(struct ipath_devdata *dd)
  193. {
  194. u8 ack_received;
  195. /* AT ENTRY SCL = LOW */
  196. /* change direction, ignore data */
  197. ack_received = sda_in(dd, 1);
  198. scl_out(dd, i2c_line_high);
  199. ack_received = sda_in(dd, 1) == 0;
  200. scl_out(dd, i2c_line_low);
  201. return ack_received;
  202. }
  203. /**
  204. * wr_byte - write a byte, one bit at a time
  205. * @dd: the infinipath device
  206. * @data: the byte to write
  207. *
  208. * Returns 0 if we got the following ack, otherwise 1
  209. */
  210. static int wr_byte(struct ipath_devdata *dd, u8 data)
  211. {
  212. int bit_cntr;
  213. u8 bit;
  214. for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {
  215. bit = (data >> bit_cntr) & 1;
  216. sda_out(dd, bit);
  217. scl_out(dd, i2c_line_high);
  218. scl_out(dd, i2c_line_low);
  219. }
  220. return (!i2c_ackrcv(dd)) ? 1 : 0;
  221. }
  222. static void send_ack(struct ipath_devdata *dd)
  223. {
  224. sda_out(dd, i2c_line_low);
  225. scl_out(dd, i2c_line_high);
  226. scl_out(dd, i2c_line_low);
  227. sda_out(dd, i2c_line_high);
  228. }
  229. /**
  230. * i2c_startcmd - transmit the start condition, followed by address/cmd
  231. * @dd: the infinipath device
  232. * @offset_dir: direction byte
  233. *
  234. * (both clock/data high, clock high, data low while clock is high)
  235. */
  236. static int i2c_startcmd(struct ipath_devdata *dd, u8 offset_dir)
  237. {
  238. int res;
  239. /* issue start sequence */
  240. sda_out(dd, i2c_line_high);
  241. scl_out(dd, i2c_line_high);
  242. sda_out(dd, i2c_line_low);
  243. scl_out(dd, i2c_line_low);
  244. /* issue length and direction byte */
  245. res = wr_byte(dd, offset_dir);
  246. if (res)
  247. ipath_cdbg(VERBOSE, "No ack to complete start\n");
  248. return res;
  249. }
  250. /**
  251. * stop_cmd - transmit the stop condition
  252. * @dd: the infinipath device
  253. *
  254. * (both clock/data low, clock high, data high while clock is high)
  255. */
  256. static void stop_cmd(struct ipath_devdata *dd)
  257. {
  258. scl_out(dd, i2c_line_low);
  259. sda_out(dd, i2c_line_low);
  260. scl_out(dd, i2c_line_high);
  261. sda_out(dd, i2c_line_high);
  262. udelay(2);
  263. }
  264. /**
  265. * eeprom_reset - reset I2C communication
  266. * @dd: the infinipath device
  267. */
  268. static int eeprom_reset(struct ipath_devdata *dd)
  269. {
  270. int clock_cycles_left = 9;
  271. u64 *gpioval = &dd->ipath_gpio_out;
  272. int ret;
  273. eeprom_init = 1;
  274. *gpioval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_gpio_out);
  275. ipath_cdbg(VERBOSE, "Resetting i2c eeprom; initial gpioout reg "
  276. "is %llx\n", (unsigned long long) *gpioval);
  277. /*
  278. * This is to get the i2c into a known state, by first going low,
  279. * then tristate sda (and then tristate scl as first thing
  280. * in loop)
  281. */
  282. scl_out(dd, i2c_line_low);
  283. sda_out(dd, i2c_line_high);
  284. while (clock_cycles_left--) {
  285. scl_out(dd, i2c_line_high);
  286. if (sda_in(dd, 0)) {
  287. sda_out(dd, i2c_line_low);
  288. scl_out(dd, i2c_line_low);
  289. ret = 0;
  290. goto bail;
  291. }
  292. scl_out(dd, i2c_line_low);
  293. }
  294. ret = 1;
  295. bail:
  296. return ret;
  297. }
  298. /**
  299. * ipath_eeprom_read - receives bytes from the eeprom via I2C
  300. * @dd: the infinipath device
  301. * @eeprom_offset: address to read from
  302. * @buffer: where to store result
  303. * @len: number of bytes to receive
  304. */
  305. int ipath_eeprom_read(struct ipath_devdata *dd, u8 eeprom_offset,
  306. void *buffer, int len)
  307. {
  308. /* compiler complains unless initialized */
  309. u8 single_byte = 0;
  310. int bit_cntr;
  311. int ret;
  312. if (!eeprom_init)
  313. eeprom_reset(dd);
  314. eeprom_offset = (eeprom_offset << 1) | READ_CMD;
  315. if (i2c_startcmd(dd, eeprom_offset)) {
  316. ipath_dbg("Failed startcmd\n");
  317. stop_cmd(dd);
  318. ret = 1;
  319. goto bail;
  320. }
  321. /*
  322. * eeprom keeps clocking data out as long as we ack, automatically
  323. * incrementing the address.
  324. */
  325. while (len-- > 0) {
  326. /* get data */
  327. single_byte = 0;
  328. for (bit_cntr = 8; bit_cntr; bit_cntr--) {
  329. u8 bit;
  330. scl_out(dd, i2c_line_high);
  331. bit = sda_in(dd, 0);
  332. single_byte |= bit << (bit_cntr - 1);
  333. scl_out(dd, i2c_line_low);
  334. }
  335. /* send ack if not the last byte */
  336. if (len)
  337. send_ack(dd);
  338. *((u8 *) buffer) = single_byte;
  339. buffer++;
  340. }
  341. stop_cmd(dd);
  342. ret = 0;
  343. bail:
  344. return ret;
  345. }
  346. /**
  347. * ipath_eeprom_write - writes data to the eeprom via I2C
  348. * @dd: the infinipath device
  349. * @eeprom_offset: where to place data
  350. * @buffer: data to write
  351. * @len: number of bytes to write
  352. */
  353. int ipath_eeprom_write(struct ipath_devdata *dd, u8 eeprom_offset,
  354. const void *buffer, int len)
  355. {
  356. u8 single_byte;
  357. int sub_len;
  358. const u8 *bp = buffer;
  359. int max_wait_time, i;
  360. int ret;
  361. if (!eeprom_init)
  362. eeprom_reset(dd);
  363. while (len > 0) {
  364. if (i2c_startcmd(dd, (eeprom_offset << 1) | WRITE_CMD)) {
  365. ipath_dbg("Failed to start cmd offset %u\n",
  366. eeprom_offset);
  367. goto failed_write;
  368. }
  369. sub_len = min(len, 4);
  370. eeprom_offset += sub_len;
  371. len -= sub_len;
  372. for (i = 0; i < sub_len; i++) {
  373. if (wr_byte(dd, *bp++)) {
  374. ipath_dbg("no ack after byte %u/%u (%u "
  375. "total remain)\n", i, sub_len,
  376. len + sub_len - i);
  377. goto failed_write;
  378. }
  379. }
  380. stop_cmd(dd);
  381. /*
  382. * wait for write complete by waiting for a successful
  383. * read (the chip replies with a zero after the write
  384. * cmd completes, and before it writes to the eeprom.
  385. * The startcmd for the read will fail the ack until
  386. * the writes have completed. We do this inline to avoid
  387. * the debug prints that are in the real read routine
  388. * if the startcmd fails.
  389. */
  390. max_wait_time = 100;
  391. while (i2c_startcmd(dd, READ_CMD)) {
  392. stop_cmd(dd);
  393. if (!--max_wait_time) {
  394. ipath_dbg("Did not get successful read to "
  395. "complete write\n");
  396. goto failed_write;
  397. }
  398. }
  399. /* now read the zero byte */
  400. for (i = single_byte = 0; i < 8; i++) {
  401. u8 bit;
  402. scl_out(dd, i2c_line_high);
  403. bit = sda_in(dd, 0);
  404. scl_out(dd, i2c_line_low);
  405. single_byte <<= 1;
  406. single_byte |= bit;
  407. }
  408. stop_cmd(dd);
  409. }
  410. ret = 0;
  411. goto bail;
  412. failed_write:
  413. stop_cmd(dd);
  414. ret = 1;
  415. bail:
  416. return ret;
  417. }
  418. static u8 flash_csum(struct ipath_flash *ifp, int adjust)
  419. {
  420. u8 *ip = (u8 *) ifp;
  421. u8 csum = 0, len;
  422. for (len = 0; len < ifp->if_length; len++)
  423. csum += *ip++;
  424. csum -= ifp->if_csum;
  425. csum = ~csum;
  426. if (adjust)
  427. ifp->if_csum = csum;
  428. return csum;
  429. }
  430. /**
  431. * ipath_get_guid - get the GUID from the i2c device
  432. * @dd: the infinipath device
  433. *
  434. * When we add the multi-chip support, we will probably have to add
  435. * the ability to use the number of guids field, and get the guid from
  436. * the first chip's flash, to use for all of them.
  437. */
  438. void ipath_get_guid(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. memcpy(dd->ipath_serial, ifp->if_serial,
  523. sizeof(ifp->if_serial));
  524. ipath_cdbg(VERBOSE, "Initted GUID to %llx from eeprom\n",
  525. (unsigned long long) be64_to_cpu(dd->ipath_guid));
  526. done:
  527. vfree(buf);
  528. bail:;
  529. }