cpqfcTSi2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* Copyright(c) 2000, Compaq Computer Corporation
  2. * Fibre Channel Host Bus Adapter
  3. * 64-bit, 66MHz PCI
  4. * Originally developed and tested on:
  5. * (front): [chip] Tachyon TS HPFC-5166A/1.2 L2C1090 ...
  6. * SP# P225CXCBFIEL6T, Rev XC
  7. * SP# 161290-001, Rev XD
  8. * (back): Board No. 010008-001 A/W Rev X5, FAB REV X5
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2, or (at your option) any
  13. * later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. * Written by Don Zimmerman
  20. */
  21. // These functions control the NVRAM I2C hardware on
  22. // non-intelligent Fibre Host Adapters.
  23. // The primary purpose is to read the HBA's NVRAM to get adapter's
  24. // manufactured WWN to copy into Tachyon chip registers
  25. // Orignal source author unknown
  26. #include <linux/types.h>
  27. enum boolean { FALSE, TRUE } ;
  28. #ifndef UCHAR
  29. typedef __u8 UCHAR;
  30. #endif
  31. #ifndef BOOLEAN
  32. typedef __u8 BOOLEAN;
  33. #endif
  34. #ifndef USHORT
  35. typedef __u16 USHORT;
  36. #endif
  37. #ifndef ULONG
  38. typedef __u32 ULONG;
  39. #endif
  40. #include <linux/string.h>
  41. #include <linux/pci.h>
  42. #include <linux/delay.h>
  43. #include <linux/sched.h>
  44. #include <asm/io.h> // struct pt_regs for IRQ handler & Port I/O
  45. #include "cpqfcTSchip.h"
  46. static void tl_i2c_tx_byte( void* GPIOout, UCHAR data );
  47. /*static BOOLEAN tl_write_i2c_page_portion( void* GPIOin, void* GPIOout,
  48. USHORT startOffset, // e.g. 0x2f for WWN start
  49. USHORT count,
  50. UCHAR *buf );
  51. */
  52. //
  53. // Tachlite GPIO2, GPIO3 (I2C) DEFINES
  54. // The NVRAM chip NM24C03 defines SCL (serial clock) and SDA (serial data)
  55. // GPIO2 drives SDA, and GPIO3 drives SCL
  56. //
  57. // Since Tachlite inverts the state of the GPIO 0-3 outputs, SET writes 0
  58. // and clear writes 1. The input lines (read in TL status) is NOT inverted
  59. // This really helps confuse the code and debugging.
  60. #define SET_DATA_HI 0x0
  61. #define SET_DATA_LO 0x8
  62. #define SET_CLOCK_HI 0x0
  63. #define SET_CLOCK_LO 0x4
  64. #define SENSE_DATA_HI 0x8
  65. #define SENSE_DATA_LO 0x0
  66. #define SENSE_CLOCK_HI 0x4
  67. #define SENSE_CLOCK_LO 0x0
  68. #define SLAVE_READ_ADDRESS 0xA1
  69. #define SLAVE_WRITE_ADDRESS 0xA0
  70. static void i2c_delay(ULONG mstime);
  71. static void tl_i2c_clock_pulse( UCHAR , void* GPIOout);
  72. static UCHAR tl_read_i2c_data( void* );
  73. //-----------------------------------------------------------------------------
  74. //
  75. // Name: I2C_RX_ACK
  76. //
  77. // This routine receives an acknowledge over the I2C bus.
  78. //
  79. //-----------------------------------------------------------------------------
  80. static unsigned short tl_i2c_rx_ack( void* GPIOin, void* GPIOout )
  81. {
  82. unsigned long value;
  83. // do clock pulse, let data line float high
  84. tl_i2c_clock_pulse( SET_DATA_HI, GPIOout );
  85. // slave must drive data low for acknowledge
  86. value = tl_read_i2c_data( GPIOin);
  87. if (value & SENSE_DATA_HI )
  88. return( FALSE );
  89. return( TRUE );
  90. }
  91. //-----------------------------------------------------------------------------
  92. //
  93. // Name: READ_I2C_REG
  94. //
  95. // This routine reads the I2C control register using the global
  96. // IO address stored in gpioreg.
  97. //
  98. //-----------------------------------------------------------------------------
  99. static UCHAR tl_read_i2c_data( void* gpioreg )
  100. {
  101. return( (UCHAR)(readl( gpioreg ) & 0x08L) ); // GPIO3
  102. }
  103. //-----------------------------------------------------------------------------
  104. //
  105. // Name: WRITE_I2C_REG
  106. //
  107. // This routine writes the I2C control register using the global
  108. // IO address stored in gpioreg.
  109. // In Tachlite, we don't want to modify other bits in TL Control reg.
  110. //
  111. //-----------------------------------------------------------------------------
  112. static void tl_write_i2c_reg( void* gpioregOUT, UCHAR value )
  113. {
  114. ULONG temp;
  115. // First read the register and clear out the old bits
  116. temp = readl( gpioregOUT ) & 0xfffffff3L;
  117. // Now or in the new data and send it back out
  118. writel( temp | value, gpioregOUT);
  119. }
  120. //-----------------------------------------------------------------------------
  121. //
  122. // Name: I2C_TX_START
  123. //
  124. // This routine transmits a start condition over the I2C bus.
  125. // 1. Set SCL (clock, GPIO2) HIGH, set SDA (data, GPIO3) HIGH,
  126. // wait 5us to stabilize.
  127. // 2. With SCL still HIGH, drive SDA low. The low transition marks
  128. // the start condition to NM24Cxx (the chip)
  129. // NOTE! In TL control reg., output 1 means chip sees LOW
  130. //
  131. //-----------------------------------------------------------------------------
  132. static unsigned short tl_i2c_tx_start( void* GPIOin, void* GPIOout )
  133. {
  134. unsigned short i;
  135. ULONG value;
  136. if ( !(tl_read_i2c_data(GPIOin) & SENSE_DATA_HI))
  137. {
  138. // start with clock high, let data float high
  139. tl_write_i2c_reg( GPIOout, SET_DATA_HI | SET_CLOCK_HI );
  140. // keep sending clock pulses if slave is driving data line
  141. for (i = 0; i < 10; i++)
  142. {
  143. tl_i2c_clock_pulse( SET_DATA_HI, GPIOout );
  144. if ( tl_read_i2c_data(GPIOin) & SENSE_DATA_HI )
  145. break;
  146. }
  147. // if he's still driving data low after 10 clocks, abort
  148. value = tl_read_i2c_data( GPIOin ); // read status
  149. if (!(value & 0x08) )
  150. return( FALSE );
  151. }
  152. // To START, bring data low while clock high
  153. tl_write_i2c_reg( GPIOout, SET_CLOCK_HI | SET_DATA_LO );
  154. i2c_delay(0);
  155. return( TRUE ); // TX start successful
  156. }
  157. //-----------------------------------------------------------------------------
  158. //
  159. // Name: I2C_TX_STOP
  160. //
  161. // This routine transmits a stop condition over the I2C bus.
  162. //
  163. //-----------------------------------------------------------------------------
  164. static unsigned short tl_i2c_tx_stop( void* GPIOin, void* GPIOout )
  165. {
  166. int i;
  167. for (i = 0; i < 10; i++)
  168. {
  169. // Send clock pulse, drive data line low
  170. tl_i2c_clock_pulse( SET_DATA_LO, GPIOout );
  171. // To STOP, bring data high while clock high
  172. tl_write_i2c_reg( GPIOout, SET_DATA_HI | SET_CLOCK_HI );
  173. // Give the data line time to float high
  174. i2c_delay(0);
  175. // If slave is driving data line low, there's a problem; retry
  176. if ( tl_read_i2c_data(GPIOin) & SENSE_DATA_HI )
  177. return( TRUE ); // TX STOP successful!
  178. }
  179. return( FALSE ); // error
  180. }
  181. //-----------------------------------------------------------------------------
  182. //
  183. // Name: I2C_TX_uchar
  184. //
  185. // This routine transmits a byte across the I2C bus.
  186. //
  187. //-----------------------------------------------------------------------------
  188. static void tl_i2c_tx_byte( void* GPIOout, UCHAR data )
  189. {
  190. UCHAR bit;
  191. for (bit = 0x80; bit; bit >>= 1)
  192. {
  193. if( data & bit )
  194. tl_i2c_clock_pulse( (UCHAR)SET_DATA_HI, GPIOout);
  195. else
  196. tl_i2c_clock_pulse( (UCHAR)SET_DATA_LO, GPIOout);
  197. }
  198. }
  199. //-----------------------------------------------------------------------------
  200. //
  201. // Name: I2C_RX_uchar
  202. //
  203. // This routine receives a byte across the I2C bus.
  204. //
  205. //-----------------------------------------------------------------------------
  206. static UCHAR tl_i2c_rx_byte( void* GPIOin, void* GPIOout )
  207. {
  208. UCHAR bit;
  209. UCHAR data = 0;
  210. for (bit = 0x80; bit; bit >>= 1) {
  211. // do clock pulse, let data line float high
  212. tl_i2c_clock_pulse( SET_DATA_HI, GPIOout );
  213. // read data line
  214. if ( tl_read_i2c_data( GPIOin) & 0x08 )
  215. data |= bit;
  216. }
  217. return (data);
  218. }
  219. //*****************************************************************************
  220. //*****************************************************************************
  221. // Function: read_i2c_nvram
  222. // Arguments: UCHAR count number of bytes to read
  223. // UCHAR *buf area to store the bytes read
  224. // Returns: 0 - failed
  225. // 1 - success
  226. //*****************************************************************************
  227. //*****************************************************************************
  228. unsigned long cpqfcTS_ReadNVRAM( void* GPIOin, void* GPIOout , USHORT count,
  229. UCHAR *buf )
  230. {
  231. unsigned short i;
  232. if( !( tl_i2c_tx_start(GPIOin, GPIOout) ))
  233. return FALSE;
  234. // Select the NVRAM for "dummy" write, to set the address
  235. tl_i2c_tx_byte( GPIOout , SLAVE_WRITE_ADDRESS );
  236. if ( !tl_i2c_rx_ack(GPIOin, GPIOout ) )
  237. return( FALSE );
  238. // Now send the address where we want to start reading
  239. tl_i2c_tx_byte( GPIOout , 0 );
  240. if ( !tl_i2c_rx_ack(GPIOin, GPIOout ) )
  241. return( FALSE );
  242. // Send a repeated start condition and select the
  243. // slave for reading now.
  244. if( tl_i2c_tx_start(GPIOin, GPIOout) )
  245. tl_i2c_tx_byte( GPIOout, SLAVE_READ_ADDRESS );
  246. if ( !tl_i2c_rx_ack(GPIOin, GPIOout) )
  247. return( FALSE );
  248. // this loop will now read out the data and store it
  249. // in the buffer pointed to by buf
  250. for ( i=0; i<count; i++)
  251. {
  252. *buf++ = tl_i2c_rx_byte(GPIOin, GPIOout);
  253. // Send ACK by holding data line low for 1 clock
  254. if ( i < (count-1) )
  255. tl_i2c_clock_pulse( 0x08, GPIOout );
  256. else {
  257. // Don't send ack for final byte
  258. tl_i2c_clock_pulse( SET_DATA_HI, GPIOout );
  259. }
  260. }
  261. tl_i2c_tx_stop(GPIOin, GPIOout);
  262. return( TRUE );
  263. }
  264. //****************************************************************
  265. //
  266. //
  267. //
  268. // routines to set and clear the data and clock bits
  269. //
  270. //
  271. //
  272. //****************************************************************
  273. static void tl_set_clock(void* gpioreg)
  274. {
  275. ULONG ret_val;
  276. ret_val = readl( gpioreg );
  277. ret_val &= 0xffffffFBL; // clear GPIO2 (SCL)
  278. writel( ret_val, gpioreg);
  279. }
  280. static void tl_clr_clock(void* gpioreg)
  281. {
  282. ULONG ret_val;
  283. ret_val = readl( gpioreg );
  284. ret_val |= SET_CLOCK_LO;
  285. writel( ret_val, gpioreg);
  286. }
  287. //*****************************************************************
  288. //
  289. //
  290. // This routine will advance the clock by one period
  291. //
  292. //
  293. //*****************************************************************
  294. static void tl_i2c_clock_pulse( UCHAR value, void* GPIOout )
  295. {
  296. ULONG ret_val;
  297. // clear the clock bit
  298. tl_clr_clock( GPIOout );
  299. i2c_delay(0);
  300. // read the port to preserve non-I2C bits
  301. ret_val = readl( GPIOout );
  302. // clear the data & clock bits
  303. ret_val &= 0xFFFFFFf3;
  304. // write the value passed in...
  305. // data can only change while clock is LOW!
  306. ret_val |= value; // the data
  307. ret_val |= SET_CLOCK_LO; // the clock
  308. writel( ret_val, GPIOout );
  309. i2c_delay(0);
  310. //set clock bit
  311. tl_set_clock( GPIOout);
  312. }
  313. //*****************************************************************
  314. //
  315. //
  316. // This routine returns the 64-bit WWN
  317. //
  318. //
  319. //*****************************************************************
  320. int cpqfcTS_GetNVRAM_data( UCHAR *wwnbuf, UCHAR *buf )
  321. {
  322. ULONG len;
  323. ULONG sub_len;
  324. ULONG ptr_inc;
  325. ULONG i;
  326. ULONG j;
  327. UCHAR *data_ptr;
  328. UCHAR z;
  329. UCHAR name;
  330. UCHAR sub_name;
  331. UCHAR done;
  332. int iReturn=0; // def. 0 offset is failure to find WWN field
  333. data_ptr = (UCHAR *)buf;
  334. done = FALSE;
  335. i = 0;
  336. while ( (i < 128) && (!done) )
  337. {
  338. z = data_ptr[i];\
  339. if ( !(z & 0x80) )
  340. {
  341. len = 1 + (z & 0x07);
  342. name = (z & 0x78) >> 3;
  343. if (name == 0x0F)
  344. done = TRUE;
  345. }
  346. else
  347. {
  348. name = z & 0x7F;
  349. len = 3 + data_ptr[i+1] + (data_ptr[i+2] << 8);
  350. switch (name)
  351. {
  352. case 0x0D:
  353. //
  354. j = i + 3;
  355. //
  356. if ( data_ptr[j] == 0x3b ) {
  357. len = 6;
  358. break;
  359. }
  360. while ( j<(i+len) ) {
  361. sub_name = (data_ptr[j] & 0x3f);
  362. sub_len = data_ptr[j+1] +
  363. (data_ptr[j+2] << 8);
  364. ptr_inc = sub_len + 3;
  365. switch (sub_name)
  366. {
  367. case 0x3C:
  368. memcpy( wwnbuf, &data_ptr[j+3], 8);
  369. iReturn = j+3;
  370. break;
  371. default:
  372. break;
  373. }
  374. j += ptr_inc;
  375. }
  376. break;
  377. default:
  378. break;
  379. }
  380. }
  381. //
  382. i += len;
  383. } // end while
  384. return iReturn;
  385. }
  386. // define a short 5 micro sec delay, and longer (ms) delay
  387. static void i2c_delay(ULONG mstime)
  388. {
  389. ULONG i;
  390. // NOTE: we only expect to use these delays when reading
  391. // our adapter's NVRAM, which happens only during adapter reset.
  392. // Delay technique from "Linux Device Drivers", A. Rubini
  393. // (1st Ed.) pg 137.
  394. // printk(" delay %lx ", mstime);
  395. if( mstime ) // ms delay?
  396. {
  397. // delay technique
  398. for( i=0; i < mstime; i++)
  399. udelay(1000); // 1ms per loop
  400. }
  401. else // 5 micro sec delay
  402. udelay( 5 ); // micro secs
  403. // printk("done\n");
  404. }