ql4_nvram.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 int eeprom_size(struct scsi_qla_host *ha)
  9. {
  10. return is_qla4022(ha) ? FM93C86A_SIZE_16 : FM93C66A_SIZE_16;
  11. }
  12. static inline int eeprom_no_addr_bits(struct scsi_qla_host *ha)
  13. {
  14. return is_qla4022(ha) ? FM93C86A_NO_ADDR_BITS_16 :
  15. FM93C56A_NO_ADDR_BITS_16;
  16. }
  17. static inline int eeprom_no_data_bits(struct scsi_qla_host *ha)
  18. {
  19. return FM93C56A_DATA_BITS_16;
  20. }
  21. static int fm93c56a_select(struct scsi_qla_host * ha)
  22. {
  23. DEBUG5(printk(KERN_ERR "fm93c56a_select:\n"));
  24. ha->eeprom_cmd_data = AUBURN_EEPROM_CS_1 | 0x000f0000;
  25. writel(ha->eeprom_cmd_data, isp_nvram(ha));
  26. readl(isp_nvram(ha));
  27. return 1;
  28. }
  29. static int fm93c56a_cmd(struct scsi_qla_host * ha, int cmd, int addr)
  30. {
  31. int i;
  32. int mask;
  33. int dataBit;
  34. int previousBit;
  35. /* Clock in a zero, then do the start bit. */
  36. writel(ha->eeprom_cmd_data | AUBURN_EEPROM_DO_1, isp_nvram(ha));
  37. writel(ha->eeprom_cmd_data | AUBURN_EEPROM_DO_1 |
  38. AUBURN_EEPROM_CLK_RISE, isp_nvram(ha));
  39. writel(ha->eeprom_cmd_data | AUBURN_EEPROM_DO_1 |
  40. AUBURN_EEPROM_CLK_FALL, isp_nvram(ha));
  41. readl(isp_nvram(ha));
  42. mask = 1 << (FM93C56A_CMD_BITS - 1);
  43. /* Force the previous data bit to be different. */
  44. previousBit = 0xffff;
  45. for (i = 0; i < FM93C56A_CMD_BITS; i++) {
  46. dataBit =
  47. (cmd & mask) ? AUBURN_EEPROM_DO_1 : AUBURN_EEPROM_DO_0;
  48. if (previousBit != dataBit) {
  49. /*
  50. * If the bit changed, then change the DO state to
  51. * match.
  52. */
  53. writel(ha->eeprom_cmd_data | dataBit, isp_nvram(ha));
  54. previousBit = dataBit;
  55. }
  56. writel(ha->eeprom_cmd_data | dataBit |
  57. AUBURN_EEPROM_CLK_RISE, isp_nvram(ha));
  58. writel(ha->eeprom_cmd_data | dataBit |
  59. AUBURN_EEPROM_CLK_FALL, isp_nvram(ha));
  60. readl(isp_nvram(ha));
  61. cmd = cmd << 1;
  62. }
  63. mask = 1 << (eeprom_no_addr_bits(ha) - 1);
  64. /* Force the previous data bit to be different. */
  65. previousBit = 0xffff;
  66. for (i = 0; i < eeprom_no_addr_bits(ha); i++) {
  67. dataBit = addr & mask ? AUBURN_EEPROM_DO_1 :
  68. AUBURN_EEPROM_DO_0;
  69. if (previousBit != dataBit) {
  70. /*
  71. * If the bit changed, then change the DO state to
  72. * match.
  73. */
  74. writel(ha->eeprom_cmd_data | dataBit, isp_nvram(ha));
  75. previousBit = dataBit;
  76. }
  77. writel(ha->eeprom_cmd_data | dataBit |
  78. AUBURN_EEPROM_CLK_RISE, isp_nvram(ha));
  79. writel(ha->eeprom_cmd_data | dataBit |
  80. AUBURN_EEPROM_CLK_FALL, isp_nvram(ha));
  81. readl(isp_nvram(ha));
  82. addr = addr << 1;
  83. }
  84. return 1;
  85. }
  86. static int fm93c56a_deselect(struct scsi_qla_host * ha)
  87. {
  88. ha->eeprom_cmd_data = AUBURN_EEPROM_CS_0 | 0x000f0000;
  89. writel(ha->eeprom_cmd_data, isp_nvram(ha));
  90. readl(isp_nvram(ha));
  91. return 1;
  92. }
  93. static int fm93c56a_datain(struct scsi_qla_host * ha, unsigned short *value)
  94. {
  95. int i;
  96. int data = 0;
  97. int dataBit;
  98. /* Read the data bits
  99. * The first bit is a dummy. Clock right over it. */
  100. for (i = 0; i < eeprom_no_data_bits(ha); i++) {
  101. writel(ha->eeprom_cmd_data |
  102. AUBURN_EEPROM_CLK_RISE, isp_nvram(ha));
  103. writel(ha->eeprom_cmd_data |
  104. AUBURN_EEPROM_CLK_FALL, isp_nvram(ha));
  105. dataBit =
  106. (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. }