1 /******************************************************************************
2 * QLOGIC LINUX SOFTWARE
4 * QLogic ISP2x00 device driver for Linux 2.6.x
5 * Copyright (C) 2003-2004 QLogic Corporation
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 ******************************************************************************/
23 static uint16_t qla2x00_nvram_request(scsi_qla_host_t *, uint32_t);
24 static void qla2x00_nv_deselect(scsi_qla_host_t *);
25 static void qla2x00_nv_write(scsi_qla_host_t *, uint16_t);
27 uint8_t qla2x00_read_flash_byte(scsi_qla_host_t *, uint32_t);
28 static void qla2x00_write_flash_byte(scsi_qla_host_t *, uint32_t, uint8_t);
29 static uint8_t qla2x00_poll_flash(scsi_qla_host_t *ha,
30 uint32_t addr, uint8_t poll_data, uint8_t mid);
31 static uint8_t qla2x00_program_flash_address(scsi_qla_host_t *ha,
32 uint32_t addr, uint8_t data, uint8_t mid);
33 static uint8_t qla2x00_erase_flash_sector(scsi_qla_host_t *ha,
34 uint32_t addr, uint32_t sec_mask, uint8_t mid);
36 uint8_t qla2x00_get_flash_manufacturer(scsi_qla_host_t *ha);
37 uint16_t qla2x00_get_flash_version(scsi_qla_host_t *);
38 uint16_t qla2x00_get_flash_image(scsi_qla_host_t *ha, uint8_t *image);
39 uint16_t qla2x00_set_flash_image(scsi_qla_host_t *ha, uint8_t *image);
43 * NVRAM support routines
47 * qla2x00_lock_nvram_access() -
51 qla2x00_lock_nvram_access(scsi_qla_host_t *ha)
58 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
59 data = RD_REG_WORD(®->nvram);
60 while (data & NVR_BUSY) {
62 data = RD_REG_WORD(®->nvram);
66 WRT_REG_WORD(®->u.isp2300.host_semaphore, 0x1);
68 data = RD_REG_WORD(®->u.isp2300.host_semaphore);
69 while ((data & BIT_0) == 0) {
72 WRT_REG_WORD(®->u.isp2300.host_semaphore, 0x1);
74 data = RD_REG_WORD(®->u.isp2300.host_semaphore);
80 * qla2x00_unlock_nvram_access() -
84 qla2x00_unlock_nvram_access(scsi_qla_host_t *ha)
90 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
91 WRT_REG_WORD(®->u.isp2300.host_semaphore, 0);
95 * qla2x00_get_nvram_word() - Calculates word position in NVRAM and calls the
96 * request routine to get the word from NVRAM.
98 * @addr: Address in NVRAM to read
100 * Returns the word read from nvram @addr.
103 qla2x00_get_nvram_word(scsi_qla_host_t *ha, uint32_t addr)
109 nv_cmd |= NV_READ_OP;
110 data = qla2x00_nvram_request(ha, nv_cmd);
116 * qla2x00_write_nvram_word() - Write NVRAM data.
118 * @addr: Address in NVRAM to write
119 * @data: word to program
122 qla2x00_write_nvram_word(scsi_qla_host_t *ha, uint32_t addr, uint16_t data)
127 device_reg_t *reg = ha->iobase;
129 qla2x00_nv_write(ha, NVR_DATA_OUT);
130 qla2x00_nv_write(ha, 0);
131 qla2x00_nv_write(ha, 0);
133 for (word = 0; word < 8; word++)
134 qla2x00_nv_write(ha, NVR_DATA_OUT);
136 qla2x00_nv_deselect(ha);
139 nv_cmd = (addr << 16) | NV_ERASE_OP;
141 for (count = 0; count < 11; count++) {
143 qla2x00_nv_write(ha, NVR_DATA_OUT);
145 qla2x00_nv_write(ha, 0);
150 qla2x00_nv_deselect(ha);
152 /* Wait for Erase to Finish */
153 WRT_REG_WORD(®->nvram, NVR_SELECT);
156 word = RD_REG_WORD(®->nvram);
157 } while ((word & NVR_DATA_IN) == 0);
159 qla2x00_nv_deselect(ha);
162 nv_cmd = (addr << 16) | NV_WRITE_OP;
165 for (count = 0; count < 27; count++) {
167 qla2x00_nv_write(ha, NVR_DATA_OUT);
169 qla2x00_nv_write(ha, 0);
174 qla2x00_nv_deselect(ha);
176 /* Wait for NVRAM to become ready */
177 WRT_REG_WORD(®->nvram, NVR_SELECT);
180 word = RD_REG_WORD(®->nvram);
181 } while ((word & NVR_DATA_IN) == 0);
183 qla2x00_nv_deselect(ha);
186 qla2x00_nv_write(ha, NVR_DATA_OUT);
187 for (count = 0; count < 10; count++)
188 qla2x00_nv_write(ha, 0);
190 qla2x00_nv_deselect(ha);
194 * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
197 * @nv_cmd: NVRAM command
199 * Bit definitions for NVRAM command:
202 * Bit 25, 24 = opcode
203 * Bit 23-16 = address
204 * Bit 15-0 = write data
206 * Returns the word read from nvram @addr.
209 qla2x00_nvram_request(scsi_qla_host_t *ha, uint32_t nv_cmd)
212 device_reg_t *reg = ha->iobase;
216 /* Send command to NVRAM. */
218 for (cnt = 0; cnt < 11; cnt++) {
220 qla2x00_nv_write(ha, NVR_DATA_OUT);
222 qla2x00_nv_write(ha, 0);
226 /* Read data from NVRAM. */
227 for (cnt = 0; cnt < 16; cnt++) {
228 WRT_REG_WORD(®->nvram, NVR_SELECT | NVR_CLOCK);
231 reg_data = RD_REG_WORD(®->nvram);
232 if (reg_data & NVR_DATA_IN)
234 WRT_REG_WORD(®->nvram, NVR_SELECT);
236 RD_REG_WORD(®->nvram); /* PCI Posting. */
240 WRT_REG_WORD(®->nvram, NVR_DESELECT);
242 RD_REG_WORD(®->nvram); /* PCI Posting. */
248 * qla2x00_nv_write() - Clean NVRAM operations.
252 qla2x00_nv_deselect(scsi_qla_host_t *ha)
254 device_reg_t *reg = ha->iobase;
256 WRT_REG_WORD(®->nvram, NVR_DESELECT);
258 RD_REG_WORD(®->nvram); /* PCI Posting. */
262 * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
264 * @data: Serial interface selector
267 qla2x00_nv_write(scsi_qla_host_t *ha, uint16_t data)
269 device_reg_t *reg = ha->iobase;
271 WRT_REG_WORD(®->nvram, data | NVR_SELECT);
273 RD_REG_WORD(®->nvram); /* PCI Posting. */
274 WRT_REG_WORD(®->nvram, data | NVR_SELECT | NVR_CLOCK);
276 RD_REG_WORD(®->nvram); /* PCI Posting. */
277 WRT_REG_WORD(®->nvram, data | NVR_SELECT);
279 RD_REG_WORD(®->nvram); /* PCI Posting. */
283 * Flash support routines
287 * qla2x00_flash_enable() - Setup flash for reading and writing.
291 qla2x00_flash_enable(scsi_qla_host_t *ha)
294 device_reg_t *reg = ha->iobase;
296 data = RD_REG_WORD(®->ctrl_status);
297 data |= CSR_FLASH_ENABLE;
298 WRT_REG_WORD(®->ctrl_status, data);
299 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
303 * qla2x00_flash_disable() - Disable flash and allow RISC to run.
307 qla2x00_flash_disable(scsi_qla_host_t *ha)
310 device_reg_t *reg = ha->iobase;
312 data = RD_REG_WORD(®->ctrl_status);
313 data &= ~(CSR_FLASH_ENABLE);
314 WRT_REG_WORD(®->ctrl_status, data);
315 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
319 * qla2x00_read_flash_byte() - Reads a byte from flash
321 * @addr: Address in flash to read
323 * A word is read from the chip, but, only the lower byte is valid.
325 * Returns the byte read from flash @addr.
328 qla2x00_read_flash_byte(scsi_qla_host_t *ha, uint32_t addr)
331 uint16_t bank_select;
332 device_reg_t *reg = ha->iobase;
334 /* Setup bit 16 of flash address. */
335 bank_select = RD_REG_WORD(®->ctrl_status);
336 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
337 bank_select |= CSR_FLASH_64K_BANK;
338 WRT_REG_WORD(®->ctrl_status, bank_select);
339 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
340 } else if (((addr & BIT_16) == 0) &&
341 (bank_select & CSR_FLASH_64K_BANK)) {
342 bank_select &= ~(CSR_FLASH_64K_BANK);
343 WRT_REG_WORD(®->ctrl_status, bank_select);
344 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
347 /* The ISP2312 v2 chip cannot access the FLASH registers via MMIO. */
348 if (IS_QLA2312(ha) && ha->product_id[3] == 0x2 && ha->pio_address) {
351 reg = (device_reg_t *)ha->pio_address;
352 outw((uint16_t)addr, (unsigned long)(®->flash_address));
354 data = inw((unsigned long)(®->flash_data));
357 data2 = inw((unsigned long)(®->flash_data));
358 } while (data != data2);
360 WRT_REG_WORD(®->flash_address, (uint16_t)addr);
361 data = qla2x00_debounce_register(®->flash_data);
364 return ((uint8_t)data);
368 * qla2x00_write_flash_byte() - Write a byte to flash
370 * @addr: Address in flash to write
371 * @data: Data to write
374 qla2x00_write_flash_byte(scsi_qla_host_t *ha, uint32_t addr, uint8_t data)
376 uint16_t bank_select;
377 device_reg_t *reg = ha->iobase;
379 /* Setup bit 16 of flash address. */
380 bank_select = RD_REG_WORD(®->ctrl_status);
381 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
382 bank_select |= CSR_FLASH_64K_BANK;
383 WRT_REG_WORD(®->ctrl_status, bank_select);
384 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
385 } else if (((addr & BIT_16) == 0) &&
386 (bank_select & CSR_FLASH_64K_BANK)) {
387 bank_select &= ~(CSR_FLASH_64K_BANK);
388 WRT_REG_WORD(®->ctrl_status, bank_select);
389 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
392 /* The ISP2312 v2 chip cannot access the FLASH registers via MMIO. */
393 if (IS_QLA2312(ha) && ha->product_id[3] == 0x2 && ha->pio_address) {
394 reg = (device_reg_t *)ha->pio_address;
395 outw((uint16_t)addr, (unsigned long)(®->flash_address));
396 outw((uint16_t)data, (unsigned long)(®->flash_data));
398 WRT_REG_WORD(®->flash_address, (uint16_t)addr);
399 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
400 WRT_REG_WORD(®->flash_data, (uint16_t)data);
401 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
406 * qla2x00_poll_flash() - Polls flash for completion.
408 * @addr: Address in flash to poll
409 * @poll_data: Data to be polled
410 * @mid: Flash manufacturer ID
412 * This function polls the device until bit 7 of what is read matches data
413 * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
414 * out (a fatal error). The flash book recommeds reading bit 7 again after
415 * reading bit 5 as a 1.
417 * Returns 0 on success, else non-zero.
420 qla2x00_poll_flash(scsi_qla_host_t *ha,
421 uint32_t addr, uint8_t poll_data, uint8_t mid)
431 /* Wait for 30 seconds for command to finish. */
433 for (cnt = 3000000; cnt; cnt--) {
434 flash_data = qla2x00_read_flash_byte(ha, addr);
435 if ((flash_data & BIT_7) == poll_data) {
440 if (mid != 0x40 && mid != 0xda) {
441 if (flash_data & BIT_5)
453 * qla2x00_program_flash_address() - Programs a flash address
455 * @addr: Address in flash to program
456 * @data: Data to be written in flash
457 * @mid: Flash manufacturer ID
459 * Returns 0 on success, else non-zero.
462 qla2x00_program_flash_address(scsi_qla_host_t *ha,
463 uint32_t addr, uint8_t data, uint8_t mid)
465 /* Write Program Command Sequence */
466 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
467 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
468 qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
469 qla2x00_write_flash_byte(ha, addr, data);
471 /* Wait for write to complete. */
472 return (qla2x00_poll_flash(ha, addr, data, mid));
476 * qla2x00_erase_flash_sector() - Erase a flash sector.
478 * @addr: Flash sector to erase
479 * @sec_mask: Sector address mask
480 * @mid: Flash manufacturer ID
482 * Returns 0 on success, else non-zero.
485 qla2x00_erase_flash_sector(scsi_qla_host_t *ha,
486 uint32_t addr, uint32_t sec_mask, uint8_t mid)
488 /* Individual Sector Erase Command Sequence */
489 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
490 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
491 qla2x00_write_flash_byte(ha, 0x5555, 0x80);
492 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
493 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
496 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
498 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
502 /* Wait for erase to complete. */
503 return (qla2x00_poll_flash(ha, addr, 0x80, mid));
507 * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
510 * Returns the manufacturer's ID read from the flash chip.
513 qla2x00_get_flash_manufacturer(scsi_qla_host_t *ha)
517 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
518 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
519 qla2x00_write_flash_byte(ha, 0x5555, 0x90);
520 manuf_id = qla2x00_read_flash_byte(ha, 0x0001);
526 * qla2x00_get_flash_version() - Read version information from flash.
529 * Returns QLA_SUCCESS on successful retrieval of flash version.
532 qla2x00_get_flash_version(scsi_qla_host_t *ha)
534 uint16_t ret = QLA_SUCCESS;
535 uint32_t loop_cnt = 1; /* this is for error exit only */
538 /* The ISP2312 v2 chip cannot access the FLASH registers via MMIO. */
539 if (IS_QLA2312(ha) && ha->product_id[3] == 0x2 && !ha->pio_address)
540 ret = QLA_FUNCTION_FAILED;
542 qla2x00_flash_enable(ha);
543 do { /* Loop once to provide quick error exit */
544 /* Match signature */
545 if (!(qla2x00_read_flash_byte(ha, 0) == 0x55 &&
546 qla2x00_read_flash_byte(ha, 1) == 0xaa)) {
548 DEBUG2(printk("scsi(%ld): No matching FLASH "
549 "signature.\n", ha->host_no));
550 ret = QLA_FUNCTION_FAILED;
554 pcir_adr = qla2x00_read_flash_byte(ha, 0x18) & 0xff;
556 /* validate signature of PCI data structure */
557 if ((qla2x00_read_flash_byte(ha, pcir_adr)) == 'P' &&
558 (qla2x00_read_flash_byte(ha, pcir_adr + 1)) == 'C' &&
559 (qla2x00_read_flash_byte(ha, pcir_adr + 2)) == 'I' &&
560 (qla2x00_read_flash_byte(ha, pcir_adr + 3)) == 'R') {
564 qla2x00_read_flash_byte(ha, pcir_adr + 0x12);
566 qla2x00_read_flash_byte(ha, pcir_adr + 0x13);
567 DEBUG3(printk("%s(): got %d.%d.\n",
568 __func__, ha->optrom_major, ha->optrom_minor));
571 DEBUG2(printk("%s(): PCI data struct not found. "
573 __func__, pcir_adr));
574 ret = QLA_FUNCTION_FAILED;
578 } while (--loop_cnt);
579 qla2x00_flash_disable(ha);
585 * qla2x00_get_flash_image() - Read image from flash chip.
587 * @image: Buffer to receive flash image
589 * Returns 0 on success, else non-zero.
592 qla2x00_get_flash_image(scsi_qla_host_t *ha, uint8_t *image)
597 device_reg_t *reg = ha->iobase;
599 midpoint = FLASH_IMAGE_SIZE / 2;
601 qla2x00_flash_enable(ha);
602 WRT_REG_WORD(®->nvram, 0);
603 RD_REG_WORD(®->nvram); /* PCI Posting. */
604 for (addr = 0, data = image; addr < FLASH_IMAGE_SIZE; addr++, data++) {
605 if (addr == midpoint)
606 WRT_REG_WORD(®->nvram, NVR_SELECT);
608 *data = qla2x00_read_flash_byte(ha, addr);
610 qla2x00_flash_disable(ha);
616 * qla2x00_set_flash_image() - Write image to flash chip.
618 * @image: Source image to write to flash
620 * Returns 0 on success, else non-zero.
623 qla2x00_set_flash_image(scsi_qla_host_t *ha, uint8_t *image)
633 device_reg_t *reg = ha->iobase;
638 /* Reset ISP chip. */
639 WRT_REG_WORD(®->ctrl_status, CSR_ISP_SOFT_RESET);
640 RD_REG_WORD(®->ctrl_status); /* PCI Posting. */
642 qla2x00_flash_enable(ha);
643 do { /* Loop once to provide quick error exit */
644 /* Structure of flash memory based on manufacturer */
645 mid = qla2x00_get_flash_manufacturer(ha);
650 } else if (mid == 0x40) {
651 // Mostel v29c51001 part
654 } else if (mid == 0xbf) {
658 } else if (mid == 0xda) {
659 // Winbond W29EE011 part
663 if (qla2x00_erase_flash_sector(ha, addr, sec_mask,
674 midpoint = FLASH_IMAGE_SIZE / 2;
675 for (addr = 0; addr < FLASH_IMAGE_SIZE; addr++) {
677 /* Are we at the beginning of a sector? */
678 if (!(addr & rest_addr)) {
679 if (addr == midpoint)
680 WRT_REG_WORD(®->nvram, NVR_SELECT);
683 if (qla2x00_erase_flash_sector(ha, addr,
692 if (sec_number == 1 &&
693 (addr == (rest_addr - 1))) {
696 } else if (sec_number == 3 && (addr & 0x7ffe)) {
702 if (qla2x00_program_flash_address(ha, addr, data,
710 qla2x00_flash_disable(ha);