1 // SPDX-License-Identifier: GPL-2.0-only
3 * QLogic iSCSI HBA Driver
4 * Copyright (c) 2003-2013 QLogic Corporation
10 #include "ql4_inline.h"
12 static inline void eeprom_cmd(uint32_t cmd
, struct scsi_qla_host
*ha
)
14 writel(cmd
, isp_nvram(ha
));
19 static inline int eeprom_size(struct scsi_qla_host
*ha
)
21 return is_qla4010(ha
) ? FM93C66A_SIZE_16
: FM93C86A_SIZE_16
;
24 static inline int eeprom_no_addr_bits(struct scsi_qla_host
*ha
)
26 return is_qla4010(ha
) ? FM93C56A_NO_ADDR_BITS_16
:
27 FM93C86A_NO_ADDR_BITS_16
;
30 static inline int eeprom_no_data_bits(struct scsi_qla_host
*ha
)
32 return FM93C56A_DATA_BITS_16
;
35 static int fm93c56a_select(struct scsi_qla_host
* ha
)
37 DEBUG5(printk(KERN_ERR
"fm93c56a_select:\n"));
39 ha
->eeprom_cmd_data
= AUBURN_EEPROM_CS_1
| 0x000f0000;
40 eeprom_cmd(ha
->eeprom_cmd_data
, ha
);
44 static int fm93c56a_cmd(struct scsi_qla_host
* ha
, int cmd
, int addr
)
51 /* Clock in a zero, then do the start bit. */
52 eeprom_cmd(ha
->eeprom_cmd_data
| AUBURN_EEPROM_DO_1
, ha
);
54 eeprom_cmd(ha
->eeprom_cmd_data
| AUBURN_EEPROM_DO_1
|
55 AUBURN_EEPROM_CLK_RISE
, ha
);
56 eeprom_cmd(ha
->eeprom_cmd_data
| AUBURN_EEPROM_DO_1
|
57 AUBURN_EEPROM_CLK_FALL
, ha
);
59 mask
= 1 << (FM93C56A_CMD_BITS
- 1);
61 /* Force the previous data bit to be different. */
63 for (i
= 0; i
< FM93C56A_CMD_BITS
; i
++) {
65 (cmd
& mask
) ? AUBURN_EEPROM_DO_1
: AUBURN_EEPROM_DO_0
;
66 if (previousBit
!= dataBit
) {
69 * If the bit changed, then change the DO state to
72 eeprom_cmd(ha
->eeprom_cmd_data
| dataBit
, ha
);
73 previousBit
= dataBit
;
75 eeprom_cmd(ha
->eeprom_cmd_data
| dataBit
|
76 AUBURN_EEPROM_CLK_RISE
, ha
);
77 eeprom_cmd(ha
->eeprom_cmd_data
| dataBit
|
78 AUBURN_EEPROM_CLK_FALL
, ha
);
82 mask
= 1 << (eeprom_no_addr_bits(ha
) - 1);
84 /* Force the previous data bit to be different. */
86 for (i
= 0; i
< eeprom_no_addr_bits(ha
); i
++) {
87 dataBit
= addr
& mask
? AUBURN_EEPROM_DO_1
:
89 if (previousBit
!= dataBit
) {
91 * If the bit changed, then change the DO state to
94 eeprom_cmd(ha
->eeprom_cmd_data
| dataBit
, ha
);
96 previousBit
= dataBit
;
98 eeprom_cmd(ha
->eeprom_cmd_data
| dataBit
|
99 AUBURN_EEPROM_CLK_RISE
, ha
);
100 eeprom_cmd(ha
->eeprom_cmd_data
| dataBit
|
101 AUBURN_EEPROM_CLK_FALL
, ha
);
108 static int fm93c56a_deselect(struct scsi_qla_host
* ha
)
110 ha
->eeprom_cmd_data
= AUBURN_EEPROM_CS_0
| 0x000f0000;
111 eeprom_cmd(ha
->eeprom_cmd_data
, ha
);
115 static int fm93c56a_datain(struct scsi_qla_host
* ha
, unsigned short *value
)
121 /* Read the data bits
122 * The first bit is a dummy. Clock right over it. */
123 for (i
= 0; i
< eeprom_no_data_bits(ha
); i
++) {
124 eeprom_cmd(ha
->eeprom_cmd_data
|
125 AUBURN_EEPROM_CLK_RISE
, ha
);
126 eeprom_cmd(ha
->eeprom_cmd_data
|
127 AUBURN_EEPROM_CLK_FALL
, ha
);
129 dataBit
= (readw(isp_nvram(ha
)) & AUBURN_EEPROM_DI_1
) ? 1 : 0;
131 data
= (data
<< 1) | dataBit
;
138 static int eeprom_readword(int eepromAddr
, u16
* value
,
139 struct scsi_qla_host
* ha
)
142 fm93c56a_cmd(ha
, FM93C56A_READ
, eepromAddr
);
143 fm93c56a_datain(ha
, value
);
144 fm93c56a_deselect(ha
);
148 /* Hardware_lock must be set before calling */
149 u16
rd_nvram_word(struct scsi_qla_host
* ha
, int offset
)
153 /* NOTE: NVRAM uses half-word addresses */
154 eeprom_readword(offset
, &val
, ha
);
158 u8
rd_nvram_byte(struct scsi_qla_host
*ha
, int offset
)
165 index
= (offset
- 1) / 2;
169 val
= le16_to_cpu(rd_nvram_word(ha
, index
));
172 rval
= (u8
)((val
& 0xff00) >> 8);
174 rval
= (u8
)((val
& 0x00ff));
179 int qla4xxx_is_nvram_configuration_valid(struct scsi_qla_host
* ha
)
181 int status
= QLA_ERROR
;
182 uint16_t checksum
= 0;
186 spin_lock_irqsave(&ha
->hardware_lock
, flags
);
187 for (index
= 0; index
< eeprom_size(ha
); index
++)
188 checksum
+= rd_nvram_word(ha
, index
);
189 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
192 status
= QLA_SUCCESS
;
197 /*************************************************************************
199 * Hardware Semaphore routines
201 *************************************************************************/
202 int ql4xxx_sem_spinlock(struct scsi_qla_host
* ha
, u32 sem_mask
, u32 sem_bits
)
206 unsigned int seconds
= 30;
208 DEBUG2(printk("scsi%ld : Trying to get SEM lock - mask= 0x%x, code = "
209 "0x%x\n", ha
->host_no
, sem_mask
, sem_bits
));
211 spin_lock_irqsave(&ha
->hardware_lock
, flags
);
212 writel((sem_mask
| sem_bits
), isp_semaphore(ha
));
213 value
= readw(isp_semaphore(ha
));
214 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
215 if ((value
& (sem_mask
>> 16)) == sem_bits
) {
216 DEBUG2(printk("scsi%ld : Got SEM LOCK - mask= 0x%x, "
217 "code = 0x%x\n", ha
->host_no
,
218 sem_mask
, sem_bits
));
226 void ql4xxx_sem_unlock(struct scsi_qla_host
* ha
, u32 sem_mask
)
230 spin_lock_irqsave(&ha
->hardware_lock
, flags
);
231 writel(sem_mask
, isp_semaphore(ha
));
232 readl(isp_semaphore(ha
));
233 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
235 DEBUG2(printk("scsi%ld : UNLOCK SEM - mask= 0x%x\n", ha
->host_no
,
239 int ql4xxx_sem_lock(struct scsi_qla_host
* ha
, u32 sem_mask
, u32 sem_bits
)
244 spin_lock_irqsave(&ha
->hardware_lock
, flags
);
245 writel((sem_mask
| sem_bits
), isp_semaphore(ha
));
246 value
= readw(isp_semaphore(ha
));
247 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
248 if ((value
& (sem_mask
>> 16)) == sem_bits
) {
249 DEBUG2(printk("scsi%ld : Got SEM LOCK - mask= 0x%x, code = "
250 "0x%x, sema code=0x%x\n", ha
->host_no
,
251 sem_mask
, sem_bits
, value
));