ipath_eeprom.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /*
  2. * Copyright (c) 2006, 2007, 2008 QLogic Corporation. 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. /* Added functionality for IBA7220-based cards */
  62. #define IPATH_EEPROM_DEV_V1 0xA0
  63. #define IPATH_EEPROM_DEV_V2 0xA2
  64. #define IPATH_TEMP_DEV 0x98
  65. #define IPATH_BAD_DEV (IPATH_EEPROM_DEV_V2+2)
  66. #define IPATH_NO_DEV (0xFF)
  67. /*
  68. * The number of I2C chains is proliferating. Table below brings
  69. * some order to the madness. The basic principle is that the
  70. * table is scanned from the top, and a "probe" is made to the
  71. * device probe_dev. If that succeeds, the chain is considered
  72. * to be of that type, and dd->i2c_chain_type is set to the index+1
  73. * of the entry.
  74. * The +1 is so static initialization can mean "unknown, do probe."
  75. */
  76. static struct i2c_chain_desc {
  77. u8 probe_dev; /* If seen at probe, chain is this type */
  78. u8 eeprom_dev; /* Dev addr (if any) for EEPROM */
  79. u8 temp_dev; /* Dev Addr (if any) for Temp-sense */
  80. } i2c_chains[] = {
  81. { IPATH_BAD_DEV, IPATH_NO_DEV, IPATH_NO_DEV }, /* pre-iba7220 bds */
  82. { IPATH_EEPROM_DEV_V1, IPATH_EEPROM_DEV_V1, IPATH_TEMP_DEV}, /* V1 */
  83. { IPATH_EEPROM_DEV_V2, IPATH_EEPROM_DEV_V2, IPATH_TEMP_DEV}, /* V2 */
  84. { IPATH_NO_DEV }
  85. };
  86. enum i2c_type {
  87. i2c_line_scl = 0,
  88. i2c_line_sda
  89. };
  90. enum i2c_state {
  91. i2c_line_low = 0,
  92. i2c_line_high
  93. };
  94. #define READ_CMD 1
  95. #define WRITE_CMD 0
  96. /**
  97. * i2c_gpio_set - set a GPIO line
  98. * @dd: the infinipath device
  99. * @line: the line to set
  100. * @new_line_state: the state to set
  101. *
  102. * Returns 0 if the line was set to the new state successfully, non-zero
  103. * on error.
  104. */
  105. static int i2c_gpio_set(struct ipath_devdata *dd,
  106. enum i2c_type line,
  107. enum i2c_state new_line_state)
  108. {
  109. u64 out_mask, dir_mask, *gpioval;
  110. unsigned long flags = 0;
  111. gpioval = &dd->ipath_gpio_out;
  112. if (line == i2c_line_scl) {
  113. dir_mask = dd->ipath_gpio_scl;
  114. out_mask = (1UL << dd->ipath_gpio_scl_num);
  115. } else {
  116. dir_mask = dd->ipath_gpio_sda;
  117. out_mask = (1UL << dd->ipath_gpio_sda_num);
  118. }
  119. spin_lock_irqsave(&dd->ipath_gpio_lock, flags);
  120. if (new_line_state == i2c_line_high) {
  121. /* tri-state the output rather than force high */
  122. dd->ipath_extctrl &= ~dir_mask;
  123. } else {
  124. /* config line to be an output */
  125. dd->ipath_extctrl |= dir_mask;
  126. }
  127. ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, dd->ipath_extctrl);
  128. /* set output as well (no real verify) */
  129. if (new_line_state == i2c_line_high)
  130. *gpioval |= out_mask;
  131. else
  132. *gpioval &= ~out_mask;
  133. ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_out, *gpioval);
  134. spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);
  135. return 0;
  136. }
  137. /**
  138. * i2c_gpio_get - get a GPIO line state
  139. * @dd: the infinipath device
  140. * @line: the line to get
  141. * @curr_statep: where to put the line state
  142. *
  143. * Returns 0 if the line was set to the new state successfully, non-zero
  144. * on error. curr_state is not set on error.
  145. */
  146. static int i2c_gpio_get(struct ipath_devdata *dd,
  147. enum i2c_type line,
  148. enum i2c_state *curr_statep)
  149. {
  150. u64 read_val, mask;
  151. int ret;
  152. unsigned long flags = 0;
  153. /* check args */
  154. if (curr_statep == NULL) {
  155. ret = 1;
  156. goto bail;
  157. }
  158. /* config line to be an input */
  159. if (line == i2c_line_scl)
  160. mask = dd->ipath_gpio_scl;
  161. else
  162. mask = dd->ipath_gpio_sda;
  163. spin_lock_irqsave(&dd->ipath_gpio_lock, flags);
  164. dd->ipath_extctrl &= ~mask;
  165. ipath_write_kreg(dd, dd->ipath_kregs->kr_extctrl, dd->ipath_extctrl);
  166. /*
  167. * Below is very unlikely to reflect true input state if Output
  168. * Enable actually changed.
  169. */
  170. read_val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extstatus);
  171. spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);
  172. if (read_val & mask)
  173. *curr_statep = i2c_line_high;
  174. else
  175. *curr_statep = i2c_line_low;
  176. ret = 0;
  177. bail:
  178. return ret;
  179. }
  180. /**
  181. * i2c_wait_for_writes - wait for a write
  182. * @dd: the infinipath device
  183. *
  184. * We use this instead of udelay directly, so we can make sure
  185. * that previous register writes have been flushed all the way
  186. * to the chip. Since we are delaying anyway, the cost doesn't
  187. * hurt, and makes the bit twiddling more regular
  188. */
  189. static void i2c_wait_for_writes(struct ipath_devdata *dd)
  190. {
  191. (void)ipath_read_kreg32(dd, dd->ipath_kregs->kr_scratch);
  192. rmb();
  193. }
  194. static void scl_out(struct ipath_devdata *dd, u8 bit)
  195. {
  196. udelay(1);
  197. i2c_gpio_set(dd, i2c_line_scl, bit ? i2c_line_high : i2c_line_low);
  198. i2c_wait_for_writes(dd);
  199. }
  200. static void sda_out(struct ipath_devdata *dd, u8 bit)
  201. {
  202. i2c_gpio_set(dd, i2c_line_sda, bit ? i2c_line_high : i2c_line_low);
  203. i2c_wait_for_writes(dd);
  204. }
  205. static u8 sda_in(struct ipath_devdata *dd, int wait)
  206. {
  207. enum i2c_state bit;
  208. if (i2c_gpio_get(dd, i2c_line_sda, &bit))
  209. ipath_dbg("get bit failed!\n");
  210. if (wait)
  211. i2c_wait_for_writes(dd);
  212. return bit == i2c_line_high ? 1U : 0;
  213. }
  214. /**
  215. * i2c_ackrcv - see if ack following write is true
  216. * @dd: the infinipath device
  217. */
  218. static int i2c_ackrcv(struct ipath_devdata *dd)
  219. {
  220. u8 ack_received;
  221. /* AT ENTRY SCL = LOW */
  222. /* change direction, ignore data */
  223. ack_received = sda_in(dd, 1);
  224. scl_out(dd, i2c_line_high);
  225. ack_received = sda_in(dd, 1) == 0;
  226. scl_out(dd, i2c_line_low);
  227. return ack_received;
  228. }
  229. /**
  230. * rd_byte - read a byte, leaving ACK, STOP, etc up to caller
  231. * @dd: the infinipath device
  232. *
  233. * Returns byte shifted out of device
  234. */
  235. static int rd_byte(struct ipath_devdata *dd)
  236. {
  237. int bit_cntr, data;
  238. data = 0;
  239. for (bit_cntr = 7; bit_cntr >= 0; --bit_cntr) {
  240. data <<= 1;
  241. scl_out(dd, i2c_line_high);
  242. data |= sda_in(dd, 0);
  243. scl_out(dd, i2c_line_low);
  244. }
  245. return data;
  246. }
  247. /**
  248. * wr_byte - write a byte, one bit at a time
  249. * @dd: the infinipath device
  250. * @data: the byte to write
  251. *
  252. * Returns 0 if we got the following ack, otherwise 1
  253. */
  254. static int wr_byte(struct ipath_devdata *dd, u8 data)
  255. {
  256. int bit_cntr;
  257. u8 bit;
  258. for (bit_cntr = 7; bit_cntr >= 0; bit_cntr--) {
  259. bit = (data >> bit_cntr) & 1;
  260. sda_out(dd, bit);
  261. scl_out(dd, i2c_line_high);
  262. scl_out(dd, i2c_line_low);
  263. }
  264. return (!i2c_ackrcv(dd)) ? 1 : 0;
  265. }
  266. static void send_ack(struct ipath_devdata *dd)
  267. {
  268. sda_out(dd, i2c_line_low);
  269. scl_out(dd, i2c_line_high);
  270. scl_out(dd, i2c_line_low);
  271. sda_out(dd, i2c_line_high);
  272. }
  273. /**
  274. * i2c_startcmd - transmit the start condition, followed by address/cmd
  275. * @dd: the infinipath device
  276. * @offset_dir: direction byte
  277. *
  278. * (both clock/data high, clock high, data low while clock is high)
  279. */
  280. static int i2c_startcmd(struct ipath_devdata *dd, u8 offset_dir)
  281. {
  282. int res;
  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. /* issue length and direction byte */
  289. res = wr_byte(dd, offset_dir);
  290. if (res)
  291. ipath_cdbg(VERBOSE, "No ack to complete start\n");
  292. return res;
  293. }
  294. /**
  295. * stop_cmd - transmit the stop condition
  296. * @dd: the infinipath device
  297. *
  298. * (both clock/data low, clock high, data high while clock is high)
  299. */
  300. static void stop_cmd(struct ipath_devdata *dd)
  301. {
  302. scl_out(dd, i2c_line_low);
  303. sda_out(dd, i2c_line_low);
  304. scl_out(dd, i2c_line_high);
  305. sda_out(dd, i2c_line_high);
  306. udelay(2);
  307. }
  308. /**
  309. * eeprom_reset - reset I2C communication
  310. * @dd: the infinipath device
  311. */
  312. static int eeprom_reset(struct ipath_devdata *dd)
  313. {
  314. int clock_cycles_left = 9;
  315. u64 *gpioval = &dd->ipath_gpio_out;
  316. int ret;
  317. unsigned long flags;
  318. spin_lock_irqsave(&dd->ipath_gpio_lock, flags);
  319. /* Make sure shadows are consistent */
  320. dd->ipath_extctrl = ipath_read_kreg64(dd, dd->ipath_kregs->kr_extctrl);
  321. *gpioval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_gpio_out);
  322. spin_unlock_irqrestore(&dd->ipath_gpio_lock, flags);
  323. ipath_cdbg(VERBOSE, "Resetting i2c eeprom; initial gpioout reg "
  324. "is %llx\n", (unsigned long long) *gpioval);
  325. /*
  326. * This is to get the i2c into a known state, by first going low,
  327. * then tristate sda (and then tristate scl as first thing
  328. * in loop)
  329. */
  330. scl_out(dd, i2c_line_low);
  331. sda_out(dd, i2c_line_high);
  332. /* Clock up to 9 cycles looking for SDA hi, then issue START and STOP */
  333. while (clock_cycles_left--) {
  334. scl_out(dd, i2c_line_high);
  335. /* SDA seen high, issue START by dropping it while SCL high */
  336. if (sda_in(dd, 0)) {
  337. sda_out(dd, i2c_line_low);
  338. scl_out(dd, i2c_line_low);
  339. /* ATMEL spec says must be followed by STOP. */
  340. scl_out(dd, i2c_line_high);
  341. sda_out(dd, i2c_line_high);
  342. ret = 0;
  343. goto bail;
  344. }
  345. scl_out(dd, i2c_line_low);
  346. }
  347. ret = 1;
  348. bail:
  349. return ret;
  350. }
  351. /*
  352. * Probe for I2C device at specified address. Returns 0 for "success"
  353. * to match rest of this file.
  354. * Leave bus in "reasonable" state for further commands.
  355. */
  356. static int i2c_probe(struct ipath_devdata *dd, int devaddr)
  357. {
  358. int ret = 0;
  359. ret = eeprom_reset(dd);
  360. if (ret) {
  361. ipath_dev_err(dd, "Failed reset probing device 0x%02X\n",
  362. devaddr);
  363. return ret;
  364. }
  365. /*
  366. * Reset no longer leaves bus in start condition, so normal
  367. * i2c_startcmd() will do.
  368. */
  369. ret = i2c_startcmd(dd, devaddr | READ_CMD);
  370. if (ret)
  371. ipath_cdbg(VERBOSE, "Failed startcmd for device 0x%02X\n",
  372. devaddr);
  373. else {
  374. /*
  375. * Device did respond. Complete a single-byte read, because some
  376. * devices apparently cannot handle STOP immediately after they
  377. * ACK the start-cmd.
  378. */
  379. int data;
  380. data = rd_byte(dd);
  381. stop_cmd(dd);
  382. ipath_cdbg(VERBOSE, "Response from device 0x%02X\n", devaddr);
  383. }
  384. return ret;
  385. }
  386. /*
  387. * Returns the "i2c type". This is a pointer to a struct that describes
  388. * the I2C chain on this board. To minimize impact on struct ipath_devdata,
  389. * the (small integer) index into the table is actually memoized, rather
  390. * then the pointer.
  391. * Memoization is because the type is determined on the first call per chip.
  392. * An alternative would be to move type determination to early
  393. * init code.
  394. */
  395. static struct i2c_chain_desc *ipath_i2c_type(struct ipath_devdata *dd)
  396. {
  397. int idx;
  398. /* Get memoized index, from previous successful probes */
  399. idx = dd->ipath_i2c_chain_type - 1;
  400. if (idx >= 0 && idx < (ARRAY_SIZE(i2c_chains) - 1))
  401. goto done;
  402. idx = 0;
  403. while (i2c_chains[idx].probe_dev != IPATH_NO_DEV) {
  404. /* if probe succeeds, this is type */
  405. if (!i2c_probe(dd, i2c_chains[idx].probe_dev))
  406. break;
  407. ++idx;
  408. }
  409. /*
  410. * Old EEPROM (first entry) may require a reset after probe,
  411. * rather than being able to "start" after "stop"
  412. */
  413. if (idx == 0)
  414. eeprom_reset(dd);
  415. if (i2c_chains[idx].probe_dev == IPATH_NO_DEV)
  416. idx = -1;
  417. else
  418. dd->ipath_i2c_chain_type = idx + 1;
  419. done:
  420. return (idx >= 0) ? i2c_chains + idx : NULL;
  421. }
  422. static int ipath_eeprom_internal_read(struct ipath_devdata *dd,
  423. u8 eeprom_offset, void *buffer, int len)
  424. {
  425. int ret;
  426. struct i2c_chain_desc *icd;
  427. u8 *bp = buffer;
  428. ret = 1;
  429. icd = ipath_i2c_type(dd);
  430. if (!icd)
  431. goto bail;
  432. if (icd->eeprom_dev == IPATH_NO_DEV) {
  433. /* legacy not-really-I2C */
  434. ipath_cdbg(VERBOSE, "Start command only address\n");
  435. eeprom_offset = (eeprom_offset << 1) | READ_CMD;
  436. ret = i2c_startcmd(dd, eeprom_offset);
  437. } else {
  438. /* Actual I2C */
  439. ipath_cdbg(VERBOSE, "Start command uses devaddr\n");
  440. if (i2c_startcmd(dd, icd->eeprom_dev | WRITE_CMD)) {
  441. ipath_dbg("Failed EEPROM startcmd\n");
  442. stop_cmd(dd);
  443. ret = 1;
  444. goto bail;
  445. }
  446. ret = wr_byte(dd, eeprom_offset);
  447. stop_cmd(dd);
  448. if (ret) {
  449. ipath_dev_err(dd, "Failed to write EEPROM address\n");
  450. ret = 1;
  451. goto bail;
  452. }
  453. ret = i2c_startcmd(dd, icd->eeprom_dev | READ_CMD);
  454. }
  455. if (ret) {
  456. ipath_dbg("Failed startcmd for dev %02X\n", icd->eeprom_dev);
  457. stop_cmd(dd);
  458. ret = 1;
  459. goto bail;
  460. }
  461. /*
  462. * eeprom keeps clocking data out as long as we ack, automatically
  463. * incrementing the address.
  464. */
  465. while (len-- > 0) {
  466. /* get and store data */
  467. *bp++ = rd_byte(dd);
  468. /* send ack if not the last byte */
  469. if (len)
  470. send_ack(dd);
  471. }
  472. stop_cmd(dd);
  473. ret = 0;
  474. bail:
  475. return ret;
  476. }
  477. static int ipath_eeprom_internal_write(struct ipath_devdata *dd, u8 eeprom_offset,
  478. const void *buffer, int len)
  479. {
  480. int sub_len;
  481. const u8 *bp = buffer;
  482. int max_wait_time, i;
  483. int ret;
  484. struct i2c_chain_desc *icd;
  485. ret = 1;
  486. icd = ipath_i2c_type(dd);
  487. if (!icd)
  488. goto bail;
  489. while (len > 0) {
  490. if (icd->eeprom_dev == IPATH_NO_DEV) {
  491. if (i2c_startcmd(dd,
  492. (eeprom_offset << 1) | WRITE_CMD)) {
  493. ipath_dbg("Failed to start cmd offset %u\n",
  494. eeprom_offset);
  495. goto failed_write;
  496. }
  497. } else {
  498. /* Real I2C */
  499. if (i2c_startcmd(dd, icd->eeprom_dev | WRITE_CMD)) {
  500. ipath_dbg("Failed EEPROM startcmd\n");
  501. goto failed_write;
  502. }
  503. ret = wr_byte(dd, eeprom_offset);
  504. if (ret) {
  505. ipath_dev_err(dd, "Failed to write EEPROM "
  506. "address\n");
  507. goto failed_write;
  508. }
  509. }
  510. sub_len = min(len, 4);
  511. eeprom_offset += sub_len;
  512. len -= sub_len;
  513. for (i = 0; i < sub_len; i++) {
  514. if (wr_byte(dd, *bp++)) {
  515. ipath_dbg("no ack after byte %u/%u (%u "
  516. "total remain)\n", i, sub_len,
  517. len + sub_len - i);
  518. goto failed_write;
  519. }
  520. }
  521. stop_cmd(dd);
  522. /*
  523. * wait for write complete by waiting for a successful
  524. * read (the chip replies with a zero after the write
  525. * cmd completes, and before it writes to the eeprom.
  526. * The startcmd for the read will fail the ack until
  527. * the writes have completed. We do this inline to avoid
  528. * the debug prints that are in the real read routine
  529. * if the startcmd fails.
  530. * We also use the proper device address, so it doesn't matter
  531. * whether we have real eeprom_dev. legacy likes any address.
  532. */
  533. max_wait_time = 100;
  534. while (i2c_startcmd(dd, icd->eeprom_dev | READ_CMD)) {
  535. stop_cmd(dd);
  536. if (!--max_wait_time) {
  537. ipath_dbg("Did not get successful read to "
  538. "complete write\n");
  539. goto failed_write;
  540. }
  541. }
  542. /* now read (and ignore) the resulting byte */
  543. rd_byte(dd);
  544. stop_cmd(dd);
  545. }
  546. ret = 0;
  547. goto bail;
  548. failed_write:
  549. stop_cmd(dd);
  550. ret = 1;
  551. bail:
  552. return ret;
  553. }
  554. /**
  555. * ipath_eeprom_read - receives bytes from the eeprom via I2C
  556. * @dd: the infinipath device
  557. * @eeprom_offset: address to read from
  558. * @buffer: where to store result
  559. * @len: number of bytes to receive
  560. */
  561. int ipath_eeprom_read(struct ipath_devdata *dd, u8 eeprom_offset,
  562. void *buff, int len)
  563. {
  564. int ret;
  565. ret = mutex_lock_interruptible(&dd->ipath_eep_lock);
  566. if (!ret) {
  567. ret = ipath_eeprom_internal_read(dd, eeprom_offset, buff, len);
  568. mutex_unlock(&dd->ipath_eep_lock);
  569. }
  570. return ret;
  571. }
  572. /**
  573. * ipath_eeprom_write - writes data to the eeprom via I2C
  574. * @dd: the infinipath device
  575. * @eeprom_offset: where to place data
  576. * @buffer: data to write
  577. * @len: number of bytes to write
  578. */
  579. int ipath_eeprom_write(struct ipath_devdata *dd, u8 eeprom_offset,
  580. const void *buff, int len)
  581. {
  582. int ret;
  583. ret = mutex_lock_interruptible(&dd->ipath_eep_lock);
  584. if (!ret) {
  585. ret = ipath_eeprom_internal_write(dd, eeprom_offset, buff, len);
  586. mutex_unlock(&dd->ipath_eep_lock);
  587. }
  588. return ret;
  589. }
  590. static u8 flash_csum(struct ipath_flash *ifp, int adjust)
  591. {
  592. u8 *ip = (u8 *) ifp;
  593. u8 csum = 0, len;
  594. /*
  595. * Limit length checksummed to max length of actual data.
  596. * Checksum of erased eeprom will still be bad, but we avoid
  597. * reading past the end of the buffer we were passed.
  598. */
  599. len = ifp->if_length;
  600. if (len > sizeof(struct ipath_flash))
  601. len = sizeof(struct ipath_flash);
  602. while (len--)
  603. csum += *ip++;
  604. csum -= ifp->if_csum;
  605. csum = ~csum;
  606. if (adjust)
  607. ifp->if_csum = csum;
  608. return csum;
  609. }
  610. /**
  611. * ipath_get_guid - get the GUID from the i2c device
  612. * @dd: the infinipath device
  613. *
  614. * We have the capability to use the ipath_nguid field, and get
  615. * the guid from the first chip's flash, to use for all of them.
  616. */
  617. void ipath_get_eeprom_info(struct ipath_devdata *dd)
  618. {
  619. void *buf;
  620. struct ipath_flash *ifp;
  621. __be64 guid;
  622. int len, eep_stat;
  623. u8 csum, *bguid;
  624. int t = dd->ipath_unit;
  625. struct ipath_devdata *dd0 = ipath_lookup(0);
  626. if (t && dd0->ipath_nguid > 1 && t <= dd0->ipath_nguid) {
  627. u8 oguid;
  628. dd->ipath_guid = dd0->ipath_guid;
  629. bguid = (u8 *) & dd->ipath_guid;
  630. oguid = bguid[7];
  631. bguid[7] += t;
  632. if (oguid > bguid[7]) {
  633. if (bguid[6] == 0xff) {
  634. if (bguid[5] == 0xff) {
  635. ipath_dev_err(
  636. dd,
  637. "Can't set %s GUID from "
  638. "base, wraps to OUI!\n",
  639. ipath_get_unit_name(t));
  640. dd->ipath_guid = 0;
  641. goto bail;
  642. }
  643. bguid[5]++;
  644. }
  645. bguid[6]++;
  646. }
  647. dd->ipath_nguid = 1;
  648. ipath_dbg("nguid %u, so adding %u to device 0 guid, "
  649. "for %llx\n",
  650. dd0->ipath_nguid, t,
  651. (unsigned long long) be64_to_cpu(dd->ipath_guid));
  652. goto bail;
  653. }
  654. /*
  655. * read full flash, not just currently used part, since it may have
  656. * been written with a newer definition
  657. * */
  658. len = sizeof(struct ipath_flash);
  659. buf = vmalloc(len);
  660. if (!buf) {
  661. ipath_dev_err(dd, "Couldn't allocate memory to read %u "
  662. "bytes from eeprom for GUID\n", len);
  663. goto bail;
  664. }
  665. mutex_lock(&dd->ipath_eep_lock);
  666. eep_stat = ipath_eeprom_internal_read(dd, 0, buf, len);
  667. mutex_unlock(&dd->ipath_eep_lock);
  668. if (eep_stat) {
  669. ipath_dev_err(dd, "Failed reading GUID from eeprom\n");
  670. goto done;
  671. }
  672. ifp = (struct ipath_flash *)buf;
  673. csum = flash_csum(ifp, 0);
  674. if (csum != ifp->if_csum) {
  675. dev_info(&dd->pcidev->dev, "Bad I2C flash checksum: "
  676. "0x%x, not 0x%x\n", csum, ifp->if_csum);
  677. goto done;
  678. }
  679. if (*(__be64 *) ifp->if_guid == cpu_to_be64(0) ||
  680. *(__be64 *) ifp->if_guid == ~cpu_to_be64(0)) {
  681. ipath_dev_err(dd, "Invalid GUID %llx from flash; "
  682. "ignoring\n",
  683. *(unsigned long long *) ifp->if_guid);
  684. /* don't allow GUID if all 0 or all 1's */
  685. goto done;
  686. }
  687. /* complain, but allow it */
  688. if (*(u64 *) ifp->if_guid == 0x100007511000000ULL)
  689. dev_info(&dd->pcidev->dev, "Warning, GUID %llx is "
  690. "default, probably not correct!\n",
  691. *(unsigned long long *) ifp->if_guid);
  692. bguid = ifp->if_guid;
  693. if (!bguid[0] && !bguid[1] && !bguid[2]) {
  694. /* original incorrect GUID format in flash; fix in
  695. * core copy, by shifting up 2 octets; don't need to
  696. * change top octet, since both it and shifted are
  697. * 0.. */
  698. bguid[1] = bguid[3];
  699. bguid[2] = bguid[4];
  700. bguid[3] = bguid[4] = 0;
  701. guid = *(__be64 *) ifp->if_guid;
  702. ipath_cdbg(VERBOSE, "Old GUID format in flash, top 3 zero, "
  703. "shifting 2 octets\n");
  704. } else
  705. guid = *(__be64 *) ifp->if_guid;
  706. dd->ipath_guid = guid;
  707. dd->ipath_nguid = ifp->if_numguid;
  708. /*
  709. * Things are slightly complicated by the desire to transparently
  710. * support both the Pathscale 10-digit serial number and the QLogic
  711. * 13-character version.
  712. */
  713. if ((ifp->if_fversion > 1) && ifp->if_sprefix[0]
  714. && ((u8 *)ifp->if_sprefix)[0] != 0xFF) {
  715. /* This board has a Serial-prefix, which is stored
  716. * elsewhere for backward-compatibility.
  717. */
  718. char *snp = dd->ipath_serial;
  719. memcpy(snp, ifp->if_sprefix, sizeof ifp->if_sprefix);
  720. snp[sizeof ifp->if_sprefix] = '\0';
  721. len = strlen(snp);
  722. snp += len;
  723. len = (sizeof dd->ipath_serial) - len;
  724. if (len > sizeof ifp->if_serial) {
  725. len = sizeof ifp->if_serial;
  726. }
  727. memcpy(snp, ifp->if_serial, len);
  728. } else
  729. memcpy(dd->ipath_serial, ifp->if_serial,
  730. sizeof ifp->if_serial);
  731. if (!strstr(ifp->if_comment, "Tested successfully"))
  732. ipath_dev_err(dd, "Board SN %s did not pass functional "
  733. "test: %s\n", dd->ipath_serial,
  734. ifp->if_comment);
  735. ipath_cdbg(VERBOSE, "Initted GUID to %llx from eeprom\n",
  736. (unsigned long long) be64_to_cpu(dd->ipath_guid));
  737. memcpy(&dd->ipath_eep_st_errs, &ifp->if_errcntp, IPATH_EEP_LOG_CNT);
  738. /*
  739. * Power-on (actually "active") hours are kept as little-endian value
  740. * in EEPROM, but as seconds in a (possibly as small as 24-bit)
  741. * atomic_t while running.
  742. */
  743. atomic_set(&dd->ipath_active_time, 0);
  744. dd->ipath_eep_hrs = ifp->if_powerhour[0] | (ifp->if_powerhour[1] << 8);
  745. done:
  746. vfree(buf);
  747. bail:;
  748. }
  749. /**
  750. * ipath_update_eeprom_log - copy active-time and error counters to eeprom
  751. * @dd: the infinipath device
  752. *
  753. * Although the time is kept as seconds in the ipath_devdata struct, it is
  754. * rounded to hours for re-write, as we have only 16 bits in EEPROM.
  755. * First-cut code reads whole (expected) struct ipath_flash, modifies,
  756. * re-writes. Future direction: read/write only what we need, assuming
  757. * that the EEPROM had to have been "good enough" for driver init, and
  758. * if not, we aren't making it worse.
  759. *
  760. */
  761. int ipath_update_eeprom_log(struct ipath_devdata *dd)
  762. {
  763. void *buf;
  764. struct ipath_flash *ifp;
  765. int len, hi_water;
  766. uint32_t new_time, new_hrs;
  767. u8 csum;
  768. int ret, idx;
  769. unsigned long flags;
  770. /* first, check if we actually need to do anything. */
  771. ret = 0;
  772. for (idx = 0; idx < IPATH_EEP_LOG_CNT; ++idx) {
  773. if (dd->ipath_eep_st_new_errs[idx]) {
  774. ret = 1;
  775. break;
  776. }
  777. }
  778. new_time = atomic_read(&dd->ipath_active_time);
  779. if (ret == 0 && new_time < 3600)
  780. return 0;
  781. /*
  782. * The quick-check above determined that there is something worthy
  783. * of logging, so get current contents and do a more detailed idea.
  784. * read full flash, not just currently used part, since it may have
  785. * been written with a newer definition
  786. */
  787. len = sizeof(struct ipath_flash);
  788. buf = vmalloc(len);
  789. ret = 1;
  790. if (!buf) {
  791. ipath_dev_err(dd, "Couldn't allocate memory to read %u "
  792. "bytes from eeprom for logging\n", len);
  793. goto bail;
  794. }
  795. /* Grab semaphore and read current EEPROM. If we get an
  796. * error, let go, but if not, keep it until we finish write.
  797. */
  798. ret = mutex_lock_interruptible(&dd->ipath_eep_lock);
  799. if (ret) {
  800. ipath_dev_err(dd, "Unable to acquire EEPROM for logging\n");
  801. goto free_bail;
  802. }
  803. ret = ipath_eeprom_internal_read(dd, 0, buf, len);
  804. if (ret) {
  805. mutex_unlock(&dd->ipath_eep_lock);
  806. ipath_dev_err(dd, "Unable read EEPROM for logging\n");
  807. goto free_bail;
  808. }
  809. ifp = (struct ipath_flash *)buf;
  810. csum = flash_csum(ifp, 0);
  811. if (csum != ifp->if_csum) {
  812. mutex_unlock(&dd->ipath_eep_lock);
  813. ipath_dev_err(dd, "EEPROM cks err (0x%02X, S/B 0x%02X)\n",
  814. csum, ifp->if_csum);
  815. ret = 1;
  816. goto free_bail;
  817. }
  818. hi_water = 0;
  819. spin_lock_irqsave(&dd->ipath_eep_st_lock, flags);
  820. for (idx = 0; idx < IPATH_EEP_LOG_CNT; ++idx) {
  821. int new_val = dd->ipath_eep_st_new_errs[idx];
  822. if (new_val) {
  823. /*
  824. * If we have seen any errors, add to EEPROM values
  825. * We need to saturate at 0xFF (255) and we also
  826. * would need to adjust the checksum if we were
  827. * trying to minimize EEPROM traffic
  828. * Note that we add to actual current count in EEPROM,
  829. * in case it was altered while we were running.
  830. */
  831. new_val += ifp->if_errcntp[idx];
  832. if (new_val > 0xFF)
  833. new_val = 0xFF;
  834. if (ifp->if_errcntp[idx] != new_val) {
  835. ifp->if_errcntp[idx] = new_val;
  836. hi_water = offsetof(struct ipath_flash,
  837. if_errcntp) + idx;
  838. }
  839. /*
  840. * update our shadow (used to minimize EEPROM
  841. * traffic), to match what we are about to write.
  842. */
  843. dd->ipath_eep_st_errs[idx] = new_val;
  844. dd->ipath_eep_st_new_errs[idx] = 0;
  845. }
  846. }
  847. /*
  848. * now update active-time. We would like to round to the nearest hour
  849. * but unless atomic_t are sure to be proper signed ints we cannot,
  850. * because we need to account for what we "transfer" to EEPROM and
  851. * if we log an hour at 31 minutes, then we would need to set
  852. * active_time to -29 to accurately count the _next_ hour.
  853. */
  854. if (new_time >= 3600) {
  855. new_hrs = new_time / 3600;
  856. atomic_sub((new_hrs * 3600), &dd->ipath_active_time);
  857. new_hrs += dd->ipath_eep_hrs;
  858. if (new_hrs > 0xFFFF)
  859. new_hrs = 0xFFFF;
  860. dd->ipath_eep_hrs = new_hrs;
  861. if ((new_hrs & 0xFF) != ifp->if_powerhour[0]) {
  862. ifp->if_powerhour[0] = new_hrs & 0xFF;
  863. hi_water = offsetof(struct ipath_flash, if_powerhour);
  864. }
  865. if ((new_hrs >> 8) != ifp->if_powerhour[1]) {
  866. ifp->if_powerhour[1] = new_hrs >> 8;
  867. hi_water = offsetof(struct ipath_flash, if_powerhour)
  868. + 1;
  869. }
  870. }
  871. /*
  872. * There is a tiny possibility that we could somehow fail to write
  873. * the EEPROM after updating our shadows, but problems from holding
  874. * the spinlock too long are a much bigger issue.
  875. */
  876. spin_unlock_irqrestore(&dd->ipath_eep_st_lock, flags);
  877. if (hi_water) {
  878. /* we made some change to the data, uopdate cksum and write */
  879. csum = flash_csum(ifp, 1);
  880. ret = ipath_eeprom_internal_write(dd, 0, buf, hi_water + 1);
  881. }
  882. mutex_unlock(&dd->ipath_eep_lock);
  883. if (ret)
  884. ipath_dev_err(dd, "Failed updating EEPROM\n");
  885. free_bail:
  886. vfree(buf);
  887. bail:
  888. return ret;
  889. }
  890. /**
  891. * ipath_inc_eeprom_err - increment one of the four error counters
  892. * that are logged to EEPROM.
  893. * @dd: the infinipath device
  894. * @eidx: 0..3, the counter to increment
  895. * @incr: how much to add
  896. *
  897. * Each counter is 8-bits, and saturates at 255 (0xFF). They
  898. * are copied to the EEPROM (aka flash) whenever ipath_update_eeprom_log()
  899. * is called, but it can only be called in a context that allows sleep.
  900. * This function can be called even at interrupt level.
  901. */
  902. void ipath_inc_eeprom_err(struct ipath_devdata *dd, u32 eidx, u32 incr)
  903. {
  904. uint new_val;
  905. unsigned long flags;
  906. spin_lock_irqsave(&dd->ipath_eep_st_lock, flags);
  907. new_val = dd->ipath_eep_st_new_errs[eidx] + incr;
  908. if (new_val > 255)
  909. new_val = 255;
  910. dd->ipath_eep_st_new_errs[eidx] = new_val;
  911. spin_unlock_irqrestore(&dd->ipath_eep_st_lock, flags);
  912. return;
  913. }
  914. static int ipath_tempsense_internal_read(struct ipath_devdata *dd, u8 regnum)
  915. {
  916. int ret;
  917. struct i2c_chain_desc *icd;
  918. ret = -ENOENT;
  919. icd = ipath_i2c_type(dd);
  920. if (!icd)
  921. goto bail;
  922. if (icd->temp_dev == IPATH_NO_DEV) {
  923. /* tempsense only exists on new, real-I2C boards */
  924. ret = -ENXIO;
  925. goto bail;
  926. }
  927. if (i2c_startcmd(dd, icd->temp_dev | WRITE_CMD)) {
  928. ipath_dbg("Failed tempsense startcmd\n");
  929. stop_cmd(dd);
  930. ret = -ENXIO;
  931. goto bail;
  932. }
  933. ret = wr_byte(dd, regnum);
  934. stop_cmd(dd);
  935. if (ret) {
  936. ipath_dev_err(dd, "Failed tempsense WR command %02X\n",
  937. regnum);
  938. ret = -ENXIO;
  939. goto bail;
  940. }
  941. if (i2c_startcmd(dd, icd->temp_dev | READ_CMD)) {
  942. ipath_dbg("Failed tempsense RD startcmd\n");
  943. stop_cmd(dd);
  944. ret = -ENXIO;
  945. goto bail;
  946. }
  947. /*
  948. * We can only clock out one byte per command, sensibly
  949. */
  950. ret = rd_byte(dd);
  951. stop_cmd(dd);
  952. bail:
  953. return ret;
  954. }
  955. #define VALID_TS_RD_REG_MASK 0xBF
  956. /**
  957. * ipath_tempsense_read - read register of temp sensor via I2C
  958. * @dd: the infinipath device
  959. * @regnum: register to read from
  960. *
  961. * returns reg contents (0..255) or < 0 for error
  962. */
  963. int ipath_tempsense_read(struct ipath_devdata *dd, u8 regnum)
  964. {
  965. int ret;
  966. if (regnum > 7)
  967. return -EINVAL;
  968. /* return a bogus value for (the one) register we do not have */
  969. if (!((1 << regnum) & VALID_TS_RD_REG_MASK))
  970. return 0;
  971. ret = mutex_lock_interruptible(&dd->ipath_eep_lock);
  972. if (!ret) {
  973. ret = ipath_tempsense_internal_read(dd, regnum);
  974. mutex_unlock(&dd->ipath_eep_lock);
  975. }
  976. /*
  977. * There are three possibilities here:
  978. * ret is actual value (0..255)
  979. * ret is -ENXIO or -EINVAL from code in this file
  980. * ret is -EINTR from mutex_lock_interruptible.
  981. */
  982. return ret;
  983. }
  984. static int ipath_tempsense_internal_write(struct ipath_devdata *dd,
  985. u8 regnum, u8 data)
  986. {
  987. int ret = -ENOENT;
  988. struct i2c_chain_desc *icd;
  989. icd = ipath_i2c_type(dd);
  990. if (!icd)
  991. goto bail;
  992. if (icd->temp_dev == IPATH_NO_DEV) {
  993. /* tempsense only exists on new, real-I2C boards */
  994. ret = -ENXIO;
  995. goto bail;
  996. }
  997. if (i2c_startcmd(dd, icd->temp_dev | WRITE_CMD)) {
  998. ipath_dbg("Failed tempsense startcmd\n");
  999. stop_cmd(dd);
  1000. ret = -ENXIO;
  1001. goto bail;
  1002. }
  1003. ret = wr_byte(dd, regnum);
  1004. if (ret) {
  1005. stop_cmd(dd);
  1006. ipath_dev_err(dd, "Failed to write tempsense command %02X\n",
  1007. regnum);
  1008. ret = -ENXIO;
  1009. goto bail;
  1010. }
  1011. ret = wr_byte(dd, data);
  1012. stop_cmd(dd);
  1013. ret = i2c_startcmd(dd, icd->temp_dev | READ_CMD);
  1014. if (ret) {
  1015. ipath_dev_err(dd, "Failed tempsense data wrt to %02X\n",
  1016. regnum);
  1017. ret = -ENXIO;
  1018. }
  1019. bail:
  1020. return ret;
  1021. }
  1022. #define VALID_TS_WR_REG_MASK ((1 << 9) | (1 << 0xB) | (1 << 0xD))
  1023. /**
  1024. * ipath_tempsense_write - write register of temp sensor via I2C
  1025. * @dd: the infinipath device
  1026. * @regnum: register to write
  1027. * @data: data to write
  1028. *
  1029. * returns 0 for success or < 0 for error
  1030. */
  1031. int ipath_tempsense_write(struct ipath_devdata *dd, u8 regnum, u8 data)
  1032. {
  1033. int ret;
  1034. if (regnum > 15 || !((1 << regnum) & VALID_TS_WR_REG_MASK))
  1035. return -EINVAL;
  1036. ret = mutex_lock_interruptible(&dd->ipath_eep_lock);
  1037. if (!ret) {
  1038. ret = ipath_tempsense_internal_write(dd, regnum, data);
  1039. mutex_unlock(&dd->ipath_eep_lock);
  1040. }
  1041. /*
  1042. * There are three possibilities here:
  1043. * ret is 0 for success
  1044. * ret is -ENXIO or -EINVAL from code in this file
  1045. * ret is -EINTR from mutex_lock_interruptible.
  1046. */
  1047. return ret;
  1048. }