ql4_nvram.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2003-2006 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include "ql4_def.h"
  8. static inline void eeprom_cmd(uint32_t cmd, struct scsi_qla_host *ha)
  9. {
  10. writel(cmd, isp_nvram(ha));
  11. readl(isp_nvram(ha));
  12. udelay(1);
  13. }
  14. static inline int eeprom_size(struct scsi_qla_host *ha)
  15. {
  16. return is_qla4010(ha) ? FM93C66A_SIZE_16 : FM93C86A_SIZE_16;
  17. }
  18. static inline int eeprom_no_addr_bits(struct scsi_qla_host *ha)
  19. {
  20. return is_qla4010(ha) ? FM93C56A_NO_ADDR_BITS_16 :
  21. FM93C86A_NO_ADDR_BITS_16 ;
  22. }
  23. static inline int eeprom_no_data_bits(struct scsi_qla_host *ha)
  24. {
  25. return FM93C56A_DATA_BITS_16;
  26. }
  27. static int fm93c56a_select(struct scsi_qla_host * ha)
  28. {
  29. DEBUG5(printk(KERN_ERR "fm93c56a_select:\n"));
  30. ha->eeprom_cmd_data = AUBURN_EEPROM_CS_1 | 0x000f0000;
  31. eeprom_cmd(ha->eeprom_cmd_data, ha);
  32. return 1;
  33. }
  34. static int fm93c56a_cmd(struct scsi_qla_host * ha, int cmd, int addr)
  35. {
  36. int i;
  37. int mask;
  38. int dataBit;
  39. int previousBit;
  40. /* Clock in a zero, then do the start bit. */
  41. eeprom_cmd(ha->eeprom_cmd_data | AUBURN_EEPROM_DO_1, ha);
  42. eeprom_cmd(ha->eeprom_cmd_data | AUBURN_EEPROM_DO_1 |
  43. AUBURN_EEPROM_CLK_RISE, ha);
  44. eeprom_cmd(ha->eeprom_cmd_data | AUBURN_EEPROM_DO_1 |
  45. AUBURN_EEPROM_CLK_FALL, ha);
  46. mask = 1 << (FM93C56A_CMD_BITS - 1);
  47. /* Force the previous data bit to be different. */
  48. previousBit = 0xffff;
  49. for (i = 0; i < FM93C56A_CMD_BITS; i++) {
  50. dataBit =
  51. (cmd & mask) ? AUBURN_EEPROM_DO_1 : AUBURN_EEPROM_DO_0;
  52. if (previousBit != dataBit) {
  53. /*
  54. * If the bit changed, then change the DO state to
  55. * match.
  56. */
  57. eeprom_cmd(ha->eeprom_cmd_data | dataBit, ha);
  58. previousBit = dataBit;
  59. }
  60. eeprom_cmd(ha->eeprom_cmd_data | dataBit |
  61. AUBURN_EEPROM_CLK_RISE, ha);
  62. eeprom_cmd(ha->eeprom_cmd_data | dataBit |
  63. AUBURN_EEPROM_CLK_FALL, ha);
  64. cmd = cmd << 1;
  65. }
  66. mask = 1 << (eeprom_no_addr_bits(ha) - 1);
  67. /* Force the previous data bit to be different. */
  68. previousBit = 0xffff;
  69. for (i = 0; i < eeprom_no_addr_bits(ha); i++) {
  70. dataBit = addr & mask ? AUBURN_EEPROM_DO_1 :
  71. AUBURN_EEPROM_DO_0;
  72. if (previousBit != dataBit) {
  73. /*
  74. * If the bit changed, then change the DO state to
  75. * match.
  76. */
  77. eeprom_cmd(ha->eeprom_cmd_data | dataBit, ha);
  78. previousBit = dataBit;
  79. }
  80. eeprom_cmd(ha->eeprom_cmd_data | dataBit |
  81. AUBURN_EEPROM_CLK_RISE, ha);
  82. eeprom_cmd(ha->eeprom_cmd_data | dataBit |
  83. AUBURN_EEPROM_CLK_FALL, ha);
  84. addr = addr << 1;
  85. }
  86. return 1;
  87. }
  88. static int fm93c56a_deselect(struct scsi_qla_host * ha)
  89. {
  90. ha->eeprom_cmd_data = AUBURN_EEPROM_CS_0 | 0x000f0000;
  91. eeprom_cmd(ha->eeprom_cmd_data, ha);
  92. return 1;
  93. }
  94. static int fm93c56a_datain(struct scsi_qla_host * ha, unsigned short *value)
  95. {
  96. int i;
  97. int data = 0;
  98. int dataBit;
  99. /* Read the data bits
  100. * The first bit is a dummy. Clock right over it. */
  101. for (i = 0; i < eeprom_no_data_bits(ha); i++) {
  102. eeprom_cmd(ha->eeprom_cmd_data |
  103. AUBURN_EEPROM_CLK_RISE, ha);
  104. eeprom_cmd(ha->eeprom_cmd_data |
  105. AUBURN_EEPROM_CLK_FALL, ha);
  106. dataBit = (readw(isp_nvram(ha)) & AUBURN_EEPROM_DI_1) ? 1 : 0;
  107. data = (data << 1) | dataBit;
  108. }
  109. *value = data;
  110. return 1;
  111. }
  112. static int eeprom_readword(int eepromAddr, u16 * value,
  113. struct scsi_qla_host * ha)
  114. {
  115. fm93c56a_select(ha);
  116. fm93c56a_cmd(ha, FM93C56A_READ, eepromAddr);
  117. fm93c56a_datain(ha, value);
  118. fm93c56a_deselect(ha);
  119. return 1;
  120. }
  121. /* Hardware_lock must be set before calling */
  122. u16 rd_nvram_word(struct scsi_qla_host * ha, int offset)
  123. {
  124. u16 val;
  125. /* NOTE: NVRAM uses half-word addresses */
  126. eeprom_readword(offset, &val, ha);
  127. return val;
  128. }
  129. int qla4xxx_is_nvram_configuration_valid(struct scsi_qla_host * ha)
  130. {
  131. int status = QLA_ERROR;
  132. uint16_t checksum = 0;
  133. uint32_t index;
  134. unsigned long flags;
  135. spin_lock_irqsave(&ha->hardware_lock, flags);
  136. for (index = 0; index < eeprom_size(ha); index++)
  137. checksum += rd_nvram_word(ha, index);
  138. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  139. if (checksum == 0)
  140. status = QLA_SUCCESS;
  141. return status;
  142. }
  143. /*************************************************************************
  144. *
  145. * Hardware Semaphore routines
  146. *
  147. *************************************************************************/
  148. int ql4xxx_sem_spinlock(struct scsi_qla_host * ha, u32 sem_mask, u32 sem_bits)
  149. {
  150. uint32_t value;
  151. unsigned long flags;
  152. unsigned int seconds = 30;
  153. DEBUG2(printk("scsi%ld : Trying to get SEM lock - mask= 0x%x, code = "
  154. "0x%x\n", ha->host_no, sem_mask, sem_bits));
  155. do {
  156. spin_lock_irqsave(&ha->hardware_lock, flags);
  157. writel((sem_mask | sem_bits), isp_semaphore(ha));
  158. value = readw(isp_semaphore(ha));
  159. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  160. if ((value & (sem_mask >> 16)) == sem_bits) {
  161. DEBUG2(printk("scsi%ld : Got SEM LOCK - mask= 0x%x, "
  162. "code = 0x%x\n", ha->host_no,
  163. sem_mask, sem_bits));
  164. return QLA_SUCCESS;
  165. }
  166. ssleep(1);
  167. } while (--seconds);
  168. return QLA_ERROR;
  169. }
  170. void ql4xxx_sem_unlock(struct scsi_qla_host * ha, u32 sem_mask)
  171. {
  172. unsigned long flags;
  173. spin_lock_irqsave(&ha->hardware_lock, flags);
  174. writel(sem_mask, isp_semaphore(ha));
  175. readl(isp_semaphore(ha));
  176. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  177. DEBUG2(printk("scsi%ld : UNLOCK SEM - mask= 0x%x\n", ha->host_no,
  178. sem_mask));
  179. }
  180. int ql4xxx_sem_lock(struct scsi_qla_host * ha, u32 sem_mask, u32 sem_bits)
  181. {
  182. uint32_t value;
  183. unsigned long flags;
  184. spin_lock_irqsave(&ha->hardware_lock, flags);
  185. writel((sem_mask | sem_bits), isp_semaphore(ha));
  186. value = readw(isp_semaphore(ha));
  187. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  188. if ((value & (sem_mask >> 16)) == sem_bits) {
  189. DEBUG2(printk("scsi%ld : Got SEM LOCK - mask= 0x%x, code = "
  190. "0x%x, sema code=0x%x\n", ha->host_no,
  191. sem_mask, sem_bits, value));
  192. return 1;
  193. }
  194. return 0;
  195. }