1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Realtek PCI-Express card reader
5 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
8 * Wei WANG (wei_wang@realsil.com.cn)
9 * Micky Ching (micky_ching@realsil.com.cn)
12 #include <linux/blkdev.h>
13 #include <linux/kthread.h>
14 #include <linux/sched.h>
19 static inline void spi_set_err_code(struct rtsx_chip
*chip
, u8 err_code
)
21 struct spi_info
*spi
= &chip
->spi
;
23 spi
->err_code
= err_code
;
26 static int spi_init(struct rtsx_chip
*chip
)
30 retval
= rtsx_write_register(chip
, SPI_CONTROL
, 0xFF,
31 CS_POLARITY_LOW
| DTO_MSB_FIRST
32 | SPI_MASTER
| SPI_MODE0
| SPI_AUTO
);
35 retval
= rtsx_write_register(chip
, SPI_TCTL
, EDO_TIMING_MASK
,
40 return STATUS_SUCCESS
;
43 static int spi_set_init_para(struct rtsx_chip
*chip
)
45 struct spi_info
*spi
= &chip
->spi
;
48 retval
= rtsx_write_register(chip
, SPI_CLK_DIVIDER1
, 0xFF,
49 (u8
)(spi
->clk_div
>> 8));
52 retval
= rtsx_write_register(chip
, SPI_CLK_DIVIDER0
, 0xFF,
57 retval
= switch_clock(chip
, spi
->spi_clock
);
58 if (retval
!= STATUS_SUCCESS
)
61 retval
= select_card(chip
, SPI_CARD
);
62 if (retval
!= STATUS_SUCCESS
)
65 retval
= rtsx_write_register(chip
, CARD_CLK_EN
, SPI_CLK_EN
,
69 retval
= rtsx_write_register(chip
, CARD_OE
, SPI_OUTPUT_EN
,
76 retval
= spi_init(chip
);
77 if (retval
!= STATUS_SUCCESS
)
80 return STATUS_SUCCESS
;
83 static int sf_polling_status(struct rtsx_chip
*chip
, int msec
)
89 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, SPI_RDSR
);
90 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
91 SPI_TRANSFER0_START
| SPI_POLLING_MODE0
);
92 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
95 retval
= rtsx_send_cmd(chip
, 0, msec
);
97 rtsx_clear_spi_error(chip
);
98 spi_set_err_code(chip
, SPI_BUSY_ERR
);
102 return STATUS_SUCCESS
;
105 static int sf_enable_write(struct rtsx_chip
*chip
, u8 ins
)
107 struct spi_info
*spi
= &chip
->spi
;
111 return STATUS_SUCCESS
;
115 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, ins
);
116 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF,
117 SPI_COMMAND_BIT_8
| SPI_ADDRESS_BIT_24
);
118 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
119 SPI_TRANSFER0_START
| SPI_C_MODE0
);
120 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
123 retval
= rtsx_send_cmd(chip
, 0, 100);
125 rtsx_clear_spi_error(chip
);
126 spi_set_err_code(chip
, SPI_HW_ERR
);
130 return STATUS_SUCCESS
;
133 static int sf_disable_write(struct rtsx_chip
*chip
, u8 ins
)
135 struct spi_info
*spi
= &chip
->spi
;
139 return STATUS_SUCCESS
;
143 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, ins
);
144 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF,
145 SPI_COMMAND_BIT_8
| SPI_ADDRESS_BIT_24
);
146 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
147 SPI_TRANSFER0_START
| SPI_C_MODE0
);
148 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
151 retval
= rtsx_send_cmd(chip
, 0, 100);
153 rtsx_clear_spi_error(chip
);
154 spi_set_err_code(chip
, SPI_HW_ERR
);
158 return STATUS_SUCCESS
;
161 static void sf_program(struct rtsx_chip
*chip
, u8 ins
, u8 addr_mode
, u32 addr
,
164 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, ins
);
165 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF,
166 SPI_COMMAND_BIT_8
| SPI_ADDRESS_BIT_24
);
167 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_LENGTH0
, 0xFF, (u8
)len
);
168 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_LENGTH1
, 0xFF, (u8
)(len
>> 8));
170 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR0
, 0xFF, (u8
)addr
);
171 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR1
, 0xFF,
173 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR2
, 0xFF,
175 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
176 SPI_TRANSFER0_START
| SPI_CADO_MODE0
);
178 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
179 SPI_TRANSFER0_START
| SPI_CDO_MODE0
);
181 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
185 static int sf_erase(struct rtsx_chip
*chip
, u8 ins
, u8 addr_mode
, u32 addr
)
191 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, ins
);
192 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF,
193 SPI_COMMAND_BIT_8
| SPI_ADDRESS_BIT_24
);
195 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR0
, 0xFF, (u8
)addr
);
196 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR1
, 0xFF,
198 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR2
, 0xFF,
200 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
201 SPI_TRANSFER0_START
| SPI_CA_MODE0
);
203 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
204 SPI_TRANSFER0_START
| SPI_C_MODE0
);
206 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
209 retval
= rtsx_send_cmd(chip
, 0, 100);
211 rtsx_clear_spi_error(chip
);
212 spi_set_err_code(chip
, SPI_HW_ERR
);
216 return STATUS_SUCCESS
;
219 static int spi_init_eeprom(struct rtsx_chip
*chip
)
229 retval
= rtsx_write_register(chip
, SPI_CLK_DIVIDER1
, 0xFF, 0x00);
232 retval
= rtsx_write_register(chip
, SPI_CLK_DIVIDER0
, 0xFF, 0x27);
236 retval
= switch_clock(chip
, clk
);
237 if (retval
!= STATUS_SUCCESS
)
240 retval
= select_card(chip
, SPI_CARD
);
241 if (retval
!= STATUS_SUCCESS
)
244 retval
= rtsx_write_register(chip
, CARD_CLK_EN
, SPI_CLK_EN
,
248 retval
= rtsx_write_register(chip
, CARD_OE
, SPI_OUTPUT_EN
,
255 retval
= rtsx_write_register(chip
, SPI_CONTROL
, 0xFF,
256 CS_POLARITY_HIGH
| SPI_EEPROM_AUTO
);
259 retval
= rtsx_write_register(chip
, SPI_TCTL
, EDO_TIMING_MASK
,
264 return STATUS_SUCCESS
;
267 static int spi_eeprom_program_enable(struct rtsx_chip
*chip
)
273 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF, 0x86);
274 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, 0x13);
275 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
276 SPI_TRANSFER0_START
| SPI_CA_MODE0
);
277 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
280 retval
= rtsx_send_cmd(chip
, 0, 100);
284 return STATUS_SUCCESS
;
287 int spi_erase_eeprom_chip(struct rtsx_chip
*chip
)
291 retval
= spi_init_eeprom(chip
);
292 if (retval
!= STATUS_SUCCESS
)
295 retval
= spi_eeprom_program_enable(chip
);
296 if (retval
!= STATUS_SUCCESS
)
301 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_GPIO_DIR
, 0x01, 0);
302 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01, RING_BUFFER
);
303 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, 0x12);
304 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF, 0x84);
305 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
306 SPI_TRANSFER0_START
| SPI_CA_MODE0
);
307 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
310 retval
= rtsx_send_cmd(chip
, 0, 100);
314 retval
= rtsx_write_register(chip
, CARD_GPIO_DIR
, 0x01, 0x01);
318 return STATUS_SUCCESS
;
321 int spi_erase_eeprom_byte(struct rtsx_chip
*chip
, u16 addr
)
325 retval
= spi_init_eeprom(chip
);
326 if (retval
!= STATUS_SUCCESS
)
329 retval
= spi_eeprom_program_enable(chip
);
330 if (retval
!= STATUS_SUCCESS
)
335 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_GPIO_DIR
, 0x01, 0);
336 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01, RING_BUFFER
);
337 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, 0x07);
338 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR0
, 0xFF, (u8
)addr
);
339 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR1
, 0xFF, (u8
)(addr
>> 8));
340 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF, 0x46);
341 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
342 SPI_TRANSFER0_START
| SPI_CA_MODE0
);
343 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
346 retval
= rtsx_send_cmd(chip
, 0, 100);
350 retval
= rtsx_write_register(chip
, CARD_GPIO_DIR
, 0x01, 0x01);
354 return STATUS_SUCCESS
;
357 int spi_read_eeprom(struct rtsx_chip
*chip
, u16 addr
, u8
*val
)
362 retval
= spi_init_eeprom(chip
);
363 if (retval
!= STATUS_SUCCESS
)
368 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_GPIO_DIR
, 0x01, 0);
369 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01, RING_BUFFER
);
370 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, 0x06);
371 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR0
, 0xFF, (u8
)addr
);
372 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR1
, 0xFF, (u8
)(addr
>> 8));
373 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF, 0x46);
374 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_LENGTH0
, 0xFF, 1);
375 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
376 SPI_TRANSFER0_START
| SPI_CADI_MODE0
);
377 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
380 retval
= rtsx_send_cmd(chip
, 0, 100);
385 retval
= rtsx_read_register(chip
, SPI_DATA
, &data
);
392 retval
= rtsx_write_register(chip
, CARD_GPIO_DIR
, 0x01, 0x01);
396 return STATUS_SUCCESS
;
399 int spi_write_eeprom(struct rtsx_chip
*chip
, u16 addr
, u8 val
)
403 retval
= spi_init_eeprom(chip
);
404 if (retval
!= STATUS_SUCCESS
)
407 retval
= spi_eeprom_program_enable(chip
);
408 if (retval
!= STATUS_SUCCESS
)
413 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_GPIO_DIR
, 0x01, 0);
414 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01, RING_BUFFER
);
415 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, 0x05);
416 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR0
, 0xFF, val
);
417 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR1
, 0xFF, (u8
)addr
);
418 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR2
, 0xFF, (u8
)(addr
>> 8));
419 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF, 0x4E);
420 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
421 SPI_TRANSFER0_START
| SPI_CA_MODE0
);
422 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
425 retval
= rtsx_send_cmd(chip
, 0, 100);
429 retval
= rtsx_write_register(chip
, CARD_GPIO_DIR
, 0x01, 0x01);
433 return STATUS_SUCCESS
;
436 int spi_get_status(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
438 struct spi_info
*spi
= &chip
->spi
;
440 dev_dbg(rtsx_dev(chip
), "%s: err_code = 0x%x\n", __func__
,
442 rtsx_stor_set_xfer_buf(&spi
->err_code
,
443 min_t(int, scsi_bufflen(srb
), 1), srb
);
444 scsi_set_resid(srb
, scsi_bufflen(srb
) - 1);
446 return STATUS_SUCCESS
;
449 int spi_set_parameter(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
451 struct spi_info
*spi
= &chip
->spi
;
453 spi_set_err_code(chip
, SPI_NO_ERR
);
456 spi
->spi_clock
= ((u16
)(srb
->cmnd
[8]) << 8) | srb
->cmnd
[9];
458 spi
->spi_clock
= srb
->cmnd
[3];
460 spi
->clk_div
= ((u16
)(srb
->cmnd
[4]) << 8) | srb
->cmnd
[5];
461 spi
->write_en
= srb
->cmnd
[6];
463 dev_dbg(rtsx_dev(chip
), "%s: ", __func__
);
464 dev_dbg(rtsx_dev(chip
), "spi_clock = %d, ", spi
->spi_clock
);
465 dev_dbg(rtsx_dev(chip
), "clk_div = %d, ", spi
->clk_div
);
466 dev_dbg(rtsx_dev(chip
), "write_en = %d\n", spi
->write_en
);
468 return STATUS_SUCCESS
;
471 int spi_read_flash_id(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
477 spi_set_err_code(chip
, SPI_NO_ERR
);
479 len
= ((u16
)(srb
->cmnd
[7]) << 8) | srb
->cmnd
[8];
481 spi_set_err_code(chip
, SPI_INVALID_COMMAND
);
485 retval
= spi_set_init_para(chip
);
486 if (retval
!= STATUS_SUCCESS
) {
487 spi_set_err_code(chip
, SPI_HW_ERR
);
493 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
496 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, srb
->cmnd
[3]);
497 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR2
, 0xFF, srb
->cmnd
[4]);
498 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR1
, 0xFF, srb
->cmnd
[5]);
499 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR0
, 0xFF, srb
->cmnd
[6]);
500 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF,
501 SPI_COMMAND_BIT_8
| SPI_ADDRESS_BIT_24
);
502 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_LENGTH1
, 0xFF, srb
->cmnd
[7]);
503 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_LENGTH0
, 0xFF, srb
->cmnd
[8]);
507 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
,
508 0xFF, SPI_TRANSFER0_START
| SPI_CA_MODE0
);
510 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
,
511 0xFF, SPI_TRANSFER0_START
| SPI_C_MODE0
);
515 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
516 SPI_TRANSFER0_START
| SPI_CADI_MODE0
);
518 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
519 SPI_TRANSFER0_START
| SPI_CDI_MODE0
);
523 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
526 retval
= rtsx_send_cmd(chip
, 0, 100);
528 rtsx_clear_spi_error(chip
);
529 spi_set_err_code(chip
, SPI_HW_ERR
);
534 buf
= kmalloc(len
, GFP_KERNEL
);
538 retval
= rtsx_read_ppbuf(chip
, buf
, len
);
539 if (retval
!= STATUS_SUCCESS
) {
540 spi_set_err_code(chip
, SPI_READ_ERR
);
545 rtsx_stor_set_xfer_buf(buf
, scsi_bufflen(srb
), srb
);
546 scsi_set_resid(srb
, 0);
551 return STATUS_SUCCESS
;
554 int spi_read_flash(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
557 unsigned int index
= 0, offset
= 0;
563 spi_set_err_code(chip
, SPI_NO_ERR
);
566 addr
= ((u32
)(srb
->cmnd
[4]) << 16) | ((u32
)(srb
->cmnd
[5])
567 << 8) | srb
->cmnd
[6];
568 len
= ((u16
)(srb
->cmnd
[7]) << 8) | srb
->cmnd
[8];
569 slow_read
= srb
->cmnd
[9];
571 retval
= spi_set_init_para(chip
);
572 if (retval
!= STATUS_SUCCESS
) {
573 spi_set_err_code(chip
, SPI_HW_ERR
);
577 buf
= kmalloc(SF_PAGE_LEN
, GFP_KERNEL
);
582 u16 pagelen
= SF_PAGE_LEN
- (u8
)addr
;
589 trans_dma_enable(DMA_FROM_DEVICE
, chip
, 256, DMA_256
);
591 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, ins
);
594 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR0
, 0xFF,
596 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR1
, 0xFF,
598 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR2
, 0xFF,
600 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF,
601 SPI_COMMAND_BIT_8
| SPI_ADDRESS_BIT_24
);
603 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR1
, 0xFF,
605 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR2
, 0xFF,
607 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_ADDR3
, 0xFF,
609 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF,
610 SPI_COMMAND_BIT_8
| SPI_ADDRESS_BIT_32
);
613 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_LENGTH1
, 0xFF,
615 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_LENGTH0
, 0xFF,
618 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
619 SPI_TRANSFER0_START
| SPI_CADI_MODE0
);
620 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
,
621 SPI_TRANSFER0_END
, SPI_TRANSFER0_END
);
623 rtsx_send_cmd_no_wait(chip
);
625 retval
= rtsx_transfer_data(chip
, 0, buf
, pagelen
, 0,
626 DMA_FROM_DEVICE
, 10000);
629 rtsx_clear_spi_error(chip
);
630 spi_set_err_code(chip
, SPI_HW_ERR
);
634 rtsx_stor_access_xfer_buf(buf
, pagelen
, srb
, &index
, &offset
,
641 scsi_set_resid(srb
, 0);
644 return STATUS_SUCCESS
;
647 int spi_write_flash(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
650 u8 ins
, program_mode
;
654 unsigned int index
= 0, offset
= 0;
656 spi_set_err_code(chip
, SPI_NO_ERR
);
659 addr
= ((u32
)(srb
->cmnd
[4]) << 16) | ((u32
)(srb
->cmnd
[5])
660 << 8) | srb
->cmnd
[6];
661 len
= ((u16
)(srb
->cmnd
[7]) << 8) | srb
->cmnd
[8];
662 program_mode
= srb
->cmnd
[9];
664 retval
= spi_set_init_para(chip
);
665 if (retval
!= STATUS_SUCCESS
) {
666 spi_set_err_code(chip
, SPI_HW_ERR
);
670 if (program_mode
== BYTE_PROGRAM
) {
671 buf
= kmalloc(4, GFP_KERNEL
);
676 retval
= sf_enable_write(chip
, SPI_WREN
);
677 if (retval
!= STATUS_SUCCESS
) {
682 rtsx_stor_access_xfer_buf(buf
, 1, srb
, &index
, &offset
,
687 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
688 0x01, PINGPONG_BUFFER
);
689 rtsx_add_cmd(chip
, WRITE_REG_CMD
, PPBUF_BASE2
, 0xFF,
691 sf_program(chip
, ins
, 1, addr
, 1);
693 retval
= rtsx_send_cmd(chip
, 0, 100);
696 rtsx_clear_spi_error(chip
);
697 spi_set_err_code(chip
, SPI_HW_ERR
);
701 retval
= sf_polling_status(chip
, 100);
702 if (retval
!= STATUS_SUCCESS
) {
713 } else if (program_mode
== AAI_PROGRAM
) {
716 retval
= sf_enable_write(chip
, SPI_WREN
);
717 if (retval
!= STATUS_SUCCESS
)
720 buf
= kmalloc(4, GFP_KERNEL
);
725 rtsx_stor_access_xfer_buf(buf
, 1, srb
, &index
, &offset
,
730 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
,
731 0x01, PINGPONG_BUFFER
);
732 rtsx_add_cmd(chip
, WRITE_REG_CMD
, PPBUF_BASE2
, 0xFF,
735 sf_program(chip
, ins
, 1, addr
, 1);
738 sf_program(chip
, ins
, 0, 0, 1);
741 retval
= rtsx_send_cmd(chip
, 0, 100);
744 rtsx_clear_spi_error(chip
);
745 spi_set_err_code(chip
, SPI_HW_ERR
);
749 retval
= sf_polling_status(chip
, 100);
750 if (retval
!= STATUS_SUCCESS
) {
760 retval
= sf_disable_write(chip
, SPI_WRDI
);
761 if (retval
!= STATUS_SUCCESS
)
764 retval
= sf_polling_status(chip
, 100);
765 if (retval
!= STATUS_SUCCESS
)
767 } else if (program_mode
== PAGE_PROGRAM
) {
768 buf
= kmalloc(SF_PAGE_LEN
, GFP_KERNEL
);
773 u16 pagelen
= SF_PAGE_LEN
- (u8
)addr
;
778 retval
= sf_enable_write(chip
, SPI_WREN
);
779 if (retval
!= STATUS_SUCCESS
) {
786 trans_dma_enable(DMA_TO_DEVICE
, chip
, 256, DMA_256
);
787 sf_program(chip
, ins
, 1, addr
, pagelen
);
789 rtsx_send_cmd_no_wait(chip
);
791 rtsx_stor_access_xfer_buf(buf
, pagelen
, srb
, &index
,
792 &offset
, FROM_XFER_BUF
);
794 retval
= rtsx_transfer_data(chip
, 0, buf
, pagelen
, 0,
798 rtsx_clear_spi_error(chip
);
799 spi_set_err_code(chip
, SPI_HW_ERR
);
803 retval
= sf_polling_status(chip
, 100);
804 if (retval
!= STATUS_SUCCESS
) {
815 spi_set_err_code(chip
, SPI_INVALID_COMMAND
);
819 return STATUS_SUCCESS
;
822 int spi_erase_flash(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
828 spi_set_err_code(chip
, SPI_NO_ERR
);
831 addr
= ((u32
)(srb
->cmnd
[4]) << 16) | ((u32
)(srb
->cmnd
[5])
832 << 8) | srb
->cmnd
[6];
833 erase_mode
= srb
->cmnd
[9];
835 retval
= spi_set_init_para(chip
);
836 if (retval
!= STATUS_SUCCESS
) {
837 spi_set_err_code(chip
, SPI_HW_ERR
);
841 if (erase_mode
== PAGE_ERASE
) {
842 retval
= sf_enable_write(chip
, SPI_WREN
);
843 if (retval
!= STATUS_SUCCESS
)
846 retval
= sf_erase(chip
, ins
, 1, addr
);
847 if (retval
!= STATUS_SUCCESS
)
849 } else if (erase_mode
== CHIP_ERASE
) {
850 retval
= sf_enable_write(chip
, SPI_WREN
);
851 if (retval
!= STATUS_SUCCESS
)
854 retval
= sf_erase(chip
, ins
, 0, 0);
855 if (retval
!= STATUS_SUCCESS
)
858 spi_set_err_code(chip
, SPI_INVALID_COMMAND
);
862 return STATUS_SUCCESS
;
865 int spi_write_flash_status(struct scsi_cmnd
*srb
, struct rtsx_chip
*chip
)
868 u8 ins
, status
, ewsr
;
871 status
= srb
->cmnd
[4];
874 retval
= spi_set_init_para(chip
);
875 if (retval
!= STATUS_SUCCESS
) {
876 spi_set_err_code(chip
, SPI_HW_ERR
);
880 retval
= sf_enable_write(chip
, ewsr
);
881 if (retval
!= STATUS_SUCCESS
)
886 rtsx_add_cmd(chip
, WRITE_REG_CMD
, CARD_DATA_SOURCE
, 0x01,
889 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_COMMAND
, 0xFF, ins
);
890 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_CA_NUMBER
, 0xFF,
891 SPI_COMMAND_BIT_8
| SPI_ADDRESS_BIT_24
);
892 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_LENGTH1
, 0xFF, 0);
893 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_LENGTH0
, 0xFF, 1);
894 rtsx_add_cmd(chip
, WRITE_REG_CMD
, PPBUF_BASE2
, 0xFF, status
);
895 rtsx_add_cmd(chip
, WRITE_REG_CMD
, SPI_TRANSFER0
, 0xFF,
896 SPI_TRANSFER0_START
| SPI_CDO_MODE0
);
897 rtsx_add_cmd(chip
, CHECK_REG_CMD
, SPI_TRANSFER0
, SPI_TRANSFER0_END
,
900 retval
= rtsx_send_cmd(chip
, 0, 100);
901 if (retval
!= STATUS_SUCCESS
) {
902 rtsx_clear_spi_error(chip
);
903 spi_set_err_code(chip
, SPI_HW_ERR
);
907 return STATUS_SUCCESS
;