RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / scsi / qla4xxx / ql4_nvram.c
blob58afd135aa1de2a32bd565940e371d309200fe3f
1 /*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
8 #include "ql4_def.h"
10 static inline void eeprom_cmd(uint32_t cmd, struct scsi_qla_host *ha)
12 writel(cmd, isp_nvram(ha));
13 readl(isp_nvram(ha));
14 udelay(1);
17 static inline int eeprom_size(struct scsi_qla_host *ha)
19 return is_qla4010(ha) ? FM93C66A_SIZE_16 : FM93C86A_SIZE_16;
22 static inline int eeprom_no_addr_bits(struct scsi_qla_host *ha)
24 return is_qla4010(ha) ? FM93C56A_NO_ADDR_BITS_16 :
25 FM93C86A_NO_ADDR_BITS_16 ;
28 static inline int eeprom_no_data_bits(struct scsi_qla_host *ha)
30 return FM93C56A_DATA_BITS_16;
33 static int fm93c56a_select(struct scsi_qla_host * ha)
35 DEBUG5(printk(KERN_ERR "fm93c56a_select:\n"));
37 ha->eeprom_cmd_data = AUBURN_EEPROM_CS_1 | 0x000f0000;
38 eeprom_cmd(ha->eeprom_cmd_data, ha);
39 return 1;
42 static int fm93c56a_cmd(struct scsi_qla_host * ha, int cmd, int addr)
44 int i;
45 int mask;
46 int dataBit;
47 int previousBit;
49 /* Clock in a zero, then do the start bit. */
50 eeprom_cmd(ha->eeprom_cmd_data | AUBURN_EEPROM_DO_1, ha);
52 eeprom_cmd(ha->eeprom_cmd_data | AUBURN_EEPROM_DO_1 |
53 AUBURN_EEPROM_CLK_RISE, ha);
54 eeprom_cmd(ha->eeprom_cmd_data | AUBURN_EEPROM_DO_1 |
55 AUBURN_EEPROM_CLK_FALL, ha);
57 mask = 1 << (FM93C56A_CMD_BITS - 1);
59 /* Force the previous data bit to be different. */
60 previousBit = 0xffff;
61 for (i = 0; i < FM93C56A_CMD_BITS; i++) {
62 dataBit =
63 (cmd & mask) ? AUBURN_EEPROM_DO_1 : AUBURN_EEPROM_DO_0;
64 if (previousBit != dataBit) {
67 * If the bit changed, then change the DO state to
68 * match.
70 eeprom_cmd(ha->eeprom_cmd_data | dataBit, ha);
71 previousBit = dataBit;
73 eeprom_cmd(ha->eeprom_cmd_data | dataBit |
74 AUBURN_EEPROM_CLK_RISE, ha);
75 eeprom_cmd(ha->eeprom_cmd_data | dataBit |
76 AUBURN_EEPROM_CLK_FALL, ha);
78 cmd = cmd << 1;
80 mask = 1 << (eeprom_no_addr_bits(ha) - 1);
82 /* Force the previous data bit to be different. */
83 previousBit = 0xffff;
84 for (i = 0; i < eeprom_no_addr_bits(ha); i++) {
85 dataBit = addr & mask ? AUBURN_EEPROM_DO_1 :
86 AUBURN_EEPROM_DO_0;
87 if (previousBit != dataBit) {
89 * If the bit changed, then change the DO state to
90 * match.
92 eeprom_cmd(ha->eeprom_cmd_data | dataBit, ha);
94 previousBit = dataBit;
96 eeprom_cmd(ha->eeprom_cmd_data | dataBit |
97 AUBURN_EEPROM_CLK_RISE, ha);
98 eeprom_cmd(ha->eeprom_cmd_data | dataBit |
99 AUBURN_EEPROM_CLK_FALL, ha);
101 addr = addr << 1;
103 return 1;
106 static int fm93c56a_deselect(struct scsi_qla_host * ha)
108 ha->eeprom_cmd_data = AUBURN_EEPROM_CS_0 | 0x000f0000;
109 eeprom_cmd(ha->eeprom_cmd_data, ha);
110 return 1;
113 static int fm93c56a_datain(struct scsi_qla_host * ha, unsigned short *value)
115 int i;
116 int data = 0;
117 int dataBit;
119 /* Read the data bits
120 * The first bit is a dummy. Clock right over it. */
121 for (i = 0; i < eeprom_no_data_bits(ha); i++) {
122 eeprom_cmd(ha->eeprom_cmd_data |
123 AUBURN_EEPROM_CLK_RISE, ha);
124 eeprom_cmd(ha->eeprom_cmd_data |
125 AUBURN_EEPROM_CLK_FALL, ha);
127 dataBit = (readw(isp_nvram(ha)) & AUBURN_EEPROM_DI_1) ? 1 : 0;
129 data = (data << 1) | dataBit;
132 *value = data;
133 return 1;
136 static int eeprom_readword(int eepromAddr, u16 * value,
137 struct scsi_qla_host * ha)
139 fm93c56a_select(ha);
140 fm93c56a_cmd(ha, FM93C56A_READ, eepromAddr);
141 fm93c56a_datain(ha, value);
142 fm93c56a_deselect(ha);
143 return 1;
146 /* Hardware_lock must be set before calling */
147 u16 rd_nvram_word(struct scsi_qla_host * ha, int offset)
149 u16 val;
151 /* NOTE: NVRAM uses half-word addresses */
152 eeprom_readword(offset, &val, ha);
153 return val;
156 int qla4xxx_is_nvram_configuration_valid(struct scsi_qla_host * ha)
158 int status = QLA_ERROR;
159 uint16_t checksum = 0;
160 uint32_t index;
161 unsigned long flags;
163 spin_lock_irqsave(&ha->hardware_lock, flags);
164 for (index = 0; index < eeprom_size(ha); index++)
165 checksum += rd_nvram_word(ha, index);
166 spin_unlock_irqrestore(&ha->hardware_lock, flags);
168 if (checksum == 0)
169 status = QLA_SUCCESS;
171 return status;
174 /*************************************************************************
176 * Hardware Semaphore routines
178 *************************************************************************/
179 int ql4xxx_sem_spinlock(struct scsi_qla_host * ha, u32 sem_mask, u32 sem_bits)
181 uint32_t value;
182 unsigned long flags;
183 unsigned int seconds = 30;
185 DEBUG2(printk("scsi%ld : Trying to get SEM lock - mask= 0x%x, code = "
186 "0x%x\n", ha->host_no, sem_mask, sem_bits));
187 do {
188 spin_lock_irqsave(&ha->hardware_lock, flags);
189 writel((sem_mask | sem_bits), isp_semaphore(ha));
190 value = readw(isp_semaphore(ha));
191 spin_unlock_irqrestore(&ha->hardware_lock, flags);
192 if ((value & (sem_mask >> 16)) == sem_bits) {
193 DEBUG2(printk("scsi%ld : Got SEM LOCK - mask= 0x%x, "
194 "code = 0x%x\n", ha->host_no,
195 sem_mask, sem_bits));
196 return QLA_SUCCESS;
198 ssleep(1);
199 } while (--seconds);
200 return QLA_ERROR;
203 void ql4xxx_sem_unlock(struct scsi_qla_host * ha, u32 sem_mask)
205 unsigned long flags;
207 spin_lock_irqsave(&ha->hardware_lock, flags);
208 writel(sem_mask, isp_semaphore(ha));
209 readl(isp_semaphore(ha));
210 spin_unlock_irqrestore(&ha->hardware_lock, flags);
212 DEBUG2(printk("scsi%ld : UNLOCK SEM - mask= 0x%x\n", ha->host_no,
213 sem_mask));
216 int ql4xxx_sem_lock(struct scsi_qla_host * ha, u32 sem_mask, u32 sem_bits)
218 uint32_t value;
219 unsigned long flags;
221 spin_lock_irqsave(&ha->hardware_lock, flags);
222 writel((sem_mask | sem_bits), isp_semaphore(ha));
223 value = readw(isp_semaphore(ha));
224 spin_unlock_irqrestore(&ha->hardware_lock, flags);
225 if ((value & (sem_mask >> 16)) == sem_bits) {
226 DEBUG2(printk("scsi%ld : Got SEM LOCK - mask= 0x%x, code = "
227 "0x%x, sema code=0x%x\n", ha->host_no,
228 sem_mask, sem_bits, value));
229 return 1;
231 return 0;