mt76x2: apply coverage class on slot time too
[linux/fpc-iii.git] / drivers / staging / rts5208 / spi.c
blobb5646b62ec9ee16bd85d2d1508fbe7ded221e9fb
1 /* Driver for Realtek PCI-Express card reader
3 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
8 * later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
18 * Author:
19 * Wei WANG (wei_wang@realsil.com.cn)
20 * Micky Ching (micky_ching@realsil.com.cn)
23 #include <linux/blkdev.h>
24 #include <linux/kthread.h>
25 #include <linux/sched.h>
27 #include "rtsx.h"
28 #include "spi.h"
30 static inline void spi_set_err_code(struct rtsx_chip *chip, u8 err_code)
32 struct spi_info *spi = &chip->spi;
34 spi->err_code = err_code;
37 static int spi_init(struct rtsx_chip *chip)
39 int retval;
41 retval = rtsx_write_register(chip, SPI_CONTROL, 0xFF,
42 CS_POLARITY_LOW | DTO_MSB_FIRST
43 | SPI_MASTER | SPI_MODE0 | SPI_AUTO);
44 if (retval) {
45 rtsx_trace(chip);
46 return retval;
48 retval = rtsx_write_register(chip, SPI_TCTL, EDO_TIMING_MASK,
49 SAMPLE_DELAY_HALF);
50 if (retval) {
51 rtsx_trace(chip);
52 return retval;
55 return STATUS_SUCCESS;
58 static int spi_set_init_para(struct rtsx_chip *chip)
60 struct spi_info *spi = &chip->spi;
61 int retval;
63 retval = rtsx_write_register(chip, SPI_CLK_DIVIDER1, 0xFF,
64 (u8)(spi->clk_div >> 8));
65 if (retval) {
66 rtsx_trace(chip);
67 return retval;
69 retval = rtsx_write_register(chip, SPI_CLK_DIVIDER0, 0xFF,
70 (u8)(spi->clk_div));
71 if (retval) {
72 rtsx_trace(chip);
73 return retval;
76 retval = switch_clock(chip, spi->spi_clock);
77 if (retval != STATUS_SUCCESS) {
78 rtsx_trace(chip);
79 return STATUS_FAIL;
82 retval = select_card(chip, SPI_CARD);
83 if (retval != STATUS_SUCCESS) {
84 rtsx_trace(chip);
85 return STATUS_FAIL;
88 retval = rtsx_write_register(chip, CARD_CLK_EN, SPI_CLK_EN,
89 SPI_CLK_EN);
90 if (retval) {
91 rtsx_trace(chip);
92 return retval;
94 retval = rtsx_write_register(chip, CARD_OE, SPI_OUTPUT_EN,
95 SPI_OUTPUT_EN);
96 if (retval) {
97 rtsx_trace(chip);
98 return retval;
101 wait_timeout(10);
103 retval = spi_init(chip);
104 if (retval != STATUS_SUCCESS) {
105 rtsx_trace(chip);
106 return STATUS_FAIL;
109 return STATUS_SUCCESS;
112 static int sf_polling_status(struct rtsx_chip *chip, int msec)
114 int retval;
116 rtsx_init_cmd(chip);
118 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, SPI_RDSR);
119 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
120 SPI_TRANSFER0_START | SPI_POLLING_MODE0);
121 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
122 SPI_TRANSFER0_END);
124 retval = rtsx_send_cmd(chip, 0, msec);
125 if (retval < 0) {
126 rtsx_clear_spi_error(chip);
127 spi_set_err_code(chip, SPI_BUSY_ERR);
128 rtsx_trace(chip);
129 return STATUS_FAIL;
132 return STATUS_SUCCESS;
135 static int sf_enable_write(struct rtsx_chip *chip, u8 ins)
137 struct spi_info *spi = &chip->spi;
138 int retval;
140 if (!spi->write_en)
141 return STATUS_SUCCESS;
143 rtsx_init_cmd(chip);
145 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, ins);
146 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF,
147 SPI_COMMAND_BIT_8 | SPI_ADDRESS_BIT_24);
148 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
149 SPI_TRANSFER0_START | SPI_C_MODE0);
150 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
151 SPI_TRANSFER0_END);
153 retval = rtsx_send_cmd(chip, 0, 100);
154 if (retval < 0) {
155 rtsx_clear_spi_error(chip);
156 spi_set_err_code(chip, SPI_HW_ERR);
157 rtsx_trace(chip);
158 return STATUS_FAIL;
161 return STATUS_SUCCESS;
164 static int sf_disable_write(struct rtsx_chip *chip, u8 ins)
166 struct spi_info *spi = &chip->spi;
167 int retval;
169 if (!spi->write_en)
170 return STATUS_SUCCESS;
172 rtsx_init_cmd(chip);
174 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, ins);
175 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF,
176 SPI_COMMAND_BIT_8 | SPI_ADDRESS_BIT_24);
177 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
178 SPI_TRANSFER0_START | SPI_C_MODE0);
179 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
180 SPI_TRANSFER0_END);
182 retval = rtsx_send_cmd(chip, 0, 100);
183 if (retval < 0) {
184 rtsx_clear_spi_error(chip);
185 spi_set_err_code(chip, SPI_HW_ERR);
186 rtsx_trace(chip);
187 return STATUS_FAIL;
190 return STATUS_SUCCESS;
193 static void sf_program(struct rtsx_chip *chip, u8 ins, u8 addr_mode, u32 addr,
194 u16 len)
196 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, ins);
197 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF,
198 SPI_COMMAND_BIT_8 | SPI_ADDRESS_BIT_24);
199 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_LENGTH0, 0xFF, (u8)len);
200 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_LENGTH1, 0xFF, (u8)(len >> 8));
201 if (addr_mode) {
202 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR0, 0xFF, (u8)addr);
203 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR1, 0xFF,
204 (u8)(addr >> 8));
205 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR2, 0xFF,
206 (u8)(addr >> 16));
207 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
208 SPI_TRANSFER0_START | SPI_CADO_MODE0);
209 } else {
210 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
211 SPI_TRANSFER0_START | SPI_CDO_MODE0);
213 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
214 SPI_TRANSFER0_END);
217 static int sf_erase(struct rtsx_chip *chip, u8 ins, u8 addr_mode, u32 addr)
219 int retval;
221 rtsx_init_cmd(chip);
223 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, ins);
224 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF,
225 SPI_COMMAND_BIT_8 | SPI_ADDRESS_BIT_24);
226 if (addr_mode) {
227 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR0, 0xFF, (u8)addr);
228 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR1, 0xFF,
229 (u8)(addr >> 8));
230 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR2, 0xFF,
231 (u8)(addr >> 16));
232 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
233 SPI_TRANSFER0_START | SPI_CA_MODE0);
234 } else {
235 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
236 SPI_TRANSFER0_START | SPI_C_MODE0);
238 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
239 SPI_TRANSFER0_END);
241 retval = rtsx_send_cmd(chip, 0, 100);
242 if (retval < 0) {
243 rtsx_clear_spi_error(chip);
244 spi_set_err_code(chip, SPI_HW_ERR);
245 rtsx_trace(chip);
246 return STATUS_FAIL;
249 return STATUS_SUCCESS;
252 static int spi_init_eeprom(struct rtsx_chip *chip)
254 int retval;
255 int clk;
257 if (chip->asic_code)
258 clk = 30;
259 else
260 clk = CLK_30;
262 retval = rtsx_write_register(chip, SPI_CLK_DIVIDER1, 0xFF, 0x00);
263 if (retval) {
264 rtsx_trace(chip);
265 return retval;
267 retval = rtsx_write_register(chip, SPI_CLK_DIVIDER0, 0xFF, 0x27);
268 if (retval) {
269 rtsx_trace(chip);
270 return retval;
273 retval = switch_clock(chip, clk);
274 if (retval != STATUS_SUCCESS) {
275 rtsx_trace(chip);
276 return STATUS_FAIL;
279 retval = select_card(chip, SPI_CARD);
280 if (retval != STATUS_SUCCESS) {
281 rtsx_trace(chip);
282 return STATUS_FAIL;
285 retval = rtsx_write_register(chip, CARD_CLK_EN, SPI_CLK_EN,
286 SPI_CLK_EN);
287 if (retval) {
288 rtsx_trace(chip);
289 return retval;
291 retval = rtsx_write_register(chip, CARD_OE, SPI_OUTPUT_EN,
292 SPI_OUTPUT_EN);
293 if (retval) {
294 rtsx_trace(chip);
295 return retval;
298 wait_timeout(10);
300 retval = rtsx_write_register(chip, SPI_CONTROL, 0xFF,
301 CS_POLARITY_HIGH | SPI_EEPROM_AUTO);
302 if (retval) {
303 rtsx_trace(chip);
304 return retval;
306 retval = rtsx_write_register(chip, SPI_TCTL, EDO_TIMING_MASK,
307 SAMPLE_DELAY_HALF);
308 if (retval) {
309 rtsx_trace(chip);
310 return retval;
313 return STATUS_SUCCESS;
316 static int spi_eeprom_program_enable(struct rtsx_chip *chip)
318 int retval;
320 rtsx_init_cmd(chip);
322 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF, 0x86);
323 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, 0x13);
324 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
325 SPI_TRANSFER0_START | SPI_CA_MODE0);
326 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
327 SPI_TRANSFER0_END);
329 retval = rtsx_send_cmd(chip, 0, 100);
330 if (retval < 0) {
331 rtsx_trace(chip);
332 return STATUS_FAIL;
335 return STATUS_SUCCESS;
338 int spi_erase_eeprom_chip(struct rtsx_chip *chip)
340 int retval;
342 retval = spi_init_eeprom(chip);
343 if (retval != STATUS_SUCCESS) {
344 rtsx_trace(chip);
345 return STATUS_FAIL;
348 retval = spi_eeprom_program_enable(chip);
349 if (retval != STATUS_SUCCESS) {
350 rtsx_trace(chip);
351 return STATUS_FAIL;
354 rtsx_init_cmd(chip);
356 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_GPIO_DIR, 0x01, 0);
357 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE, 0x01, RING_BUFFER);
358 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, 0x12);
359 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF, 0x84);
360 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
361 SPI_TRANSFER0_START | SPI_CA_MODE0);
362 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
363 SPI_TRANSFER0_END);
365 retval = rtsx_send_cmd(chip, 0, 100);
366 if (retval < 0) {
367 rtsx_trace(chip);
368 return STATUS_FAIL;
371 retval = rtsx_write_register(chip, CARD_GPIO_DIR, 0x01, 0x01);
372 if (retval) {
373 rtsx_trace(chip);
374 return retval;
377 return STATUS_SUCCESS;
380 int spi_erase_eeprom_byte(struct rtsx_chip *chip, u16 addr)
382 int retval;
384 retval = spi_init_eeprom(chip);
385 if (retval != STATUS_SUCCESS) {
386 rtsx_trace(chip);
387 return STATUS_FAIL;
390 retval = spi_eeprom_program_enable(chip);
391 if (retval != STATUS_SUCCESS) {
392 rtsx_trace(chip);
393 return STATUS_FAIL;
396 rtsx_init_cmd(chip);
398 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_GPIO_DIR, 0x01, 0);
399 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE, 0x01, RING_BUFFER);
400 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, 0x07);
401 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR0, 0xFF, (u8)addr);
402 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR1, 0xFF, (u8)(addr >> 8));
403 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF, 0x46);
404 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
405 SPI_TRANSFER0_START | SPI_CA_MODE0);
406 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
407 SPI_TRANSFER0_END);
409 retval = rtsx_send_cmd(chip, 0, 100);
410 if (retval < 0) {
411 rtsx_trace(chip);
412 return STATUS_FAIL;
415 retval = rtsx_write_register(chip, CARD_GPIO_DIR, 0x01, 0x01);
416 if (retval) {
417 rtsx_trace(chip);
418 return retval;
421 return STATUS_SUCCESS;
424 int spi_read_eeprom(struct rtsx_chip *chip, u16 addr, u8 *val)
426 int retval;
427 u8 data;
429 retval = spi_init_eeprom(chip);
430 if (retval != STATUS_SUCCESS) {
431 rtsx_trace(chip);
432 return STATUS_FAIL;
435 rtsx_init_cmd(chip);
437 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_GPIO_DIR, 0x01, 0);
438 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE, 0x01, RING_BUFFER);
439 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, 0x06);
440 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR0, 0xFF, (u8)addr);
441 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR1, 0xFF, (u8)(addr >> 8));
442 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF, 0x46);
443 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_LENGTH0, 0xFF, 1);
444 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
445 SPI_TRANSFER0_START | SPI_CADI_MODE0);
446 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
447 SPI_TRANSFER0_END);
449 retval = rtsx_send_cmd(chip, 0, 100);
450 if (retval < 0) {
451 rtsx_trace(chip);
452 return STATUS_FAIL;
455 wait_timeout(5);
456 retval = rtsx_read_register(chip, SPI_DATA, &data);
457 if (retval) {
458 rtsx_trace(chip);
459 return retval;
462 if (val)
463 *val = data;
465 retval = rtsx_write_register(chip, CARD_GPIO_DIR, 0x01, 0x01);
466 if (retval) {
467 rtsx_trace(chip);
468 return retval;
471 return STATUS_SUCCESS;
474 int spi_write_eeprom(struct rtsx_chip *chip, u16 addr, u8 val)
476 int retval;
478 retval = spi_init_eeprom(chip);
479 if (retval != STATUS_SUCCESS) {
480 rtsx_trace(chip);
481 return STATUS_FAIL;
484 retval = spi_eeprom_program_enable(chip);
485 if (retval != STATUS_SUCCESS) {
486 rtsx_trace(chip);
487 return STATUS_FAIL;
490 rtsx_init_cmd(chip);
492 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_GPIO_DIR, 0x01, 0);
493 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE, 0x01, RING_BUFFER);
494 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, 0x05);
495 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR0, 0xFF, val);
496 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR1, 0xFF, (u8)addr);
497 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR2, 0xFF, (u8)(addr >> 8));
498 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF, 0x4E);
499 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
500 SPI_TRANSFER0_START | SPI_CA_MODE0);
501 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
502 SPI_TRANSFER0_END);
504 retval = rtsx_send_cmd(chip, 0, 100);
505 if (retval < 0) {
506 rtsx_trace(chip);
507 return STATUS_FAIL;
510 retval = rtsx_write_register(chip, CARD_GPIO_DIR, 0x01, 0x01);
511 if (retval) {
512 rtsx_trace(chip);
513 return retval;
516 return STATUS_SUCCESS;
519 int spi_get_status(struct scsi_cmnd *srb, struct rtsx_chip *chip)
521 struct spi_info *spi = &chip->spi;
523 dev_dbg(rtsx_dev(chip), "%s: err_code = 0x%x\n", __func__,
524 spi->err_code);
525 rtsx_stor_set_xfer_buf(&spi->err_code,
526 min_t(int, scsi_bufflen(srb), 1), srb);
527 scsi_set_resid(srb, scsi_bufflen(srb) - 1);
529 return STATUS_SUCCESS;
532 int spi_set_parameter(struct scsi_cmnd *srb, struct rtsx_chip *chip)
534 struct spi_info *spi = &chip->spi;
536 spi_set_err_code(chip, SPI_NO_ERR);
538 if (chip->asic_code)
539 spi->spi_clock = ((u16)(srb->cmnd[8]) << 8) | srb->cmnd[9];
540 else
541 spi->spi_clock = srb->cmnd[3];
543 spi->clk_div = ((u16)(srb->cmnd[4]) << 8) | srb->cmnd[5];
544 spi->write_en = srb->cmnd[6];
546 dev_dbg(rtsx_dev(chip), "%s: ", __func__);
547 dev_dbg(rtsx_dev(chip), "spi_clock = %d, ", spi->spi_clock);
548 dev_dbg(rtsx_dev(chip), "clk_div = %d, ", spi->clk_div);
549 dev_dbg(rtsx_dev(chip), "write_en = %d\n", spi->write_en);
551 return STATUS_SUCCESS;
554 int spi_read_flash_id(struct scsi_cmnd *srb, struct rtsx_chip *chip)
556 int retval;
557 u16 len;
558 u8 *buf;
560 spi_set_err_code(chip, SPI_NO_ERR);
562 len = ((u16)(srb->cmnd[7]) << 8) | srb->cmnd[8];
563 if (len > 512) {
564 spi_set_err_code(chip, SPI_INVALID_COMMAND);
565 rtsx_trace(chip);
566 return STATUS_FAIL;
569 retval = spi_set_init_para(chip);
570 if (retval != STATUS_SUCCESS) {
571 spi_set_err_code(chip, SPI_HW_ERR);
572 rtsx_trace(chip);
573 return STATUS_FAIL;
576 rtsx_init_cmd(chip);
578 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE, 0x01,
579 PINGPONG_BUFFER);
581 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, srb->cmnd[3]);
582 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR2, 0xFF, srb->cmnd[4]);
583 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR1, 0xFF, srb->cmnd[5]);
584 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR0, 0xFF, srb->cmnd[6]);
585 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF,
586 SPI_COMMAND_BIT_8 | SPI_ADDRESS_BIT_24);
587 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_LENGTH1, 0xFF, srb->cmnd[7]);
588 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_LENGTH0, 0xFF, srb->cmnd[8]);
590 if (len == 0) {
591 if (srb->cmnd[9]) {
592 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0,
593 0xFF, SPI_TRANSFER0_START | SPI_CA_MODE0);
594 } else {
595 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0,
596 0xFF, SPI_TRANSFER0_START | SPI_C_MODE0);
598 } else {
599 if (srb->cmnd[9]) {
600 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
601 SPI_TRANSFER0_START | SPI_CADI_MODE0);
602 } else {
603 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
604 SPI_TRANSFER0_START | SPI_CDI_MODE0);
608 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
609 SPI_TRANSFER0_END);
611 retval = rtsx_send_cmd(chip, 0, 100);
612 if (retval < 0) {
613 rtsx_clear_spi_error(chip);
614 spi_set_err_code(chip, SPI_HW_ERR);
615 rtsx_trace(chip);
616 return STATUS_FAIL;
619 if (len) {
620 buf = kmalloc(len, GFP_KERNEL);
621 if (!buf) {
622 rtsx_trace(chip);
623 return STATUS_ERROR;
626 retval = rtsx_read_ppbuf(chip, buf, len);
627 if (retval != STATUS_SUCCESS) {
628 spi_set_err_code(chip, SPI_READ_ERR);
629 kfree(buf);
630 rtsx_trace(chip);
631 return STATUS_FAIL;
634 rtsx_stor_set_xfer_buf(buf, scsi_bufflen(srb), srb);
635 scsi_set_resid(srb, 0);
637 kfree(buf);
640 return STATUS_SUCCESS;
643 int spi_read_flash(struct scsi_cmnd *srb, struct rtsx_chip *chip)
645 int retval;
646 unsigned int index = 0, offset = 0;
647 u8 ins, slow_read;
648 u32 addr;
649 u16 len;
650 u8 *buf;
652 spi_set_err_code(chip, SPI_NO_ERR);
654 ins = srb->cmnd[3];
655 addr = ((u32)(srb->cmnd[4]) << 16) | ((u32)(srb->cmnd[5])
656 << 8) | srb->cmnd[6];
657 len = ((u16)(srb->cmnd[7]) << 8) | srb->cmnd[8];
658 slow_read = srb->cmnd[9];
660 retval = spi_set_init_para(chip);
661 if (retval != STATUS_SUCCESS) {
662 spi_set_err_code(chip, SPI_HW_ERR);
663 rtsx_trace(chip);
664 return STATUS_FAIL;
667 buf = kmalloc(SF_PAGE_LEN, GFP_KERNEL);
668 if (!buf) {
669 rtsx_trace(chip);
670 return STATUS_ERROR;
673 while (len) {
674 u16 pagelen = SF_PAGE_LEN - (u8)addr;
676 if (pagelen > len)
677 pagelen = len;
679 rtsx_init_cmd(chip);
681 trans_dma_enable(DMA_FROM_DEVICE, chip, 256, DMA_256);
683 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, ins);
685 if (slow_read) {
686 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR0, 0xFF,
687 (u8)addr);
688 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR1, 0xFF,
689 (u8)(addr >> 8));
690 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR2, 0xFF,
691 (u8)(addr >> 16));
692 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF,
693 SPI_COMMAND_BIT_8 | SPI_ADDRESS_BIT_24);
694 } else {
695 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR1, 0xFF,
696 (u8)addr);
697 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR2, 0xFF,
698 (u8)(addr >> 8));
699 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_ADDR3, 0xFF,
700 (u8)(addr >> 16));
701 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF,
702 SPI_COMMAND_BIT_8 | SPI_ADDRESS_BIT_32);
705 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_LENGTH1, 0xFF,
706 (u8)(pagelen >> 8));
707 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_LENGTH0, 0xFF,
708 (u8)pagelen);
710 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
711 SPI_TRANSFER0_START | SPI_CADI_MODE0);
712 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0,
713 SPI_TRANSFER0_END, SPI_TRANSFER0_END);
715 rtsx_send_cmd_no_wait(chip);
717 retval = rtsx_transfer_data(chip, 0, buf, pagelen, 0,
718 DMA_FROM_DEVICE, 10000);
719 if (retval < 0) {
720 kfree(buf);
721 rtsx_clear_spi_error(chip);
722 spi_set_err_code(chip, SPI_HW_ERR);
723 rtsx_trace(chip);
724 return STATUS_FAIL;
727 rtsx_stor_access_xfer_buf(buf, pagelen, srb, &index, &offset,
728 TO_XFER_BUF);
730 addr += pagelen;
731 len -= pagelen;
734 scsi_set_resid(srb, 0);
735 kfree(buf);
737 return STATUS_SUCCESS;
740 int spi_write_flash(struct scsi_cmnd *srb, struct rtsx_chip *chip)
742 int retval;
743 u8 ins, program_mode;
744 u32 addr;
745 u16 len;
746 u8 *buf;
747 unsigned int index = 0, offset = 0;
749 spi_set_err_code(chip, SPI_NO_ERR);
751 ins = srb->cmnd[3];
752 addr = ((u32)(srb->cmnd[4]) << 16) | ((u32)(srb->cmnd[5])
753 << 8) | srb->cmnd[6];
754 len = ((u16)(srb->cmnd[7]) << 8) | srb->cmnd[8];
755 program_mode = srb->cmnd[9];
757 retval = spi_set_init_para(chip);
758 if (retval != STATUS_SUCCESS) {
759 spi_set_err_code(chip, SPI_HW_ERR);
760 rtsx_trace(chip);
761 return STATUS_FAIL;
764 if (program_mode == BYTE_PROGRAM) {
765 buf = kmalloc(4, GFP_KERNEL);
766 if (!buf) {
767 rtsx_trace(chip);
768 return STATUS_ERROR;
771 while (len) {
772 retval = sf_enable_write(chip, SPI_WREN);
773 if (retval != STATUS_SUCCESS) {
774 kfree(buf);
775 rtsx_trace(chip);
776 return STATUS_FAIL;
779 rtsx_stor_access_xfer_buf(buf, 1, srb, &index, &offset,
780 FROM_XFER_BUF);
782 rtsx_init_cmd(chip);
784 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE,
785 0x01, PINGPONG_BUFFER);
786 rtsx_add_cmd(chip, WRITE_REG_CMD, PPBUF_BASE2, 0xFF,
787 buf[0]);
788 sf_program(chip, ins, 1, addr, 1);
790 retval = rtsx_send_cmd(chip, 0, 100);
791 if (retval < 0) {
792 kfree(buf);
793 rtsx_clear_spi_error(chip);
794 spi_set_err_code(chip, SPI_HW_ERR);
795 rtsx_trace(chip);
796 return STATUS_FAIL;
799 retval = sf_polling_status(chip, 100);
800 if (retval != STATUS_SUCCESS) {
801 kfree(buf);
802 rtsx_trace(chip);
803 return STATUS_FAIL;
806 addr++;
807 len--;
810 kfree(buf);
812 } else if (program_mode == AAI_PROGRAM) {
813 int first_byte = 1;
815 retval = sf_enable_write(chip, SPI_WREN);
816 if (retval != STATUS_SUCCESS) {
817 rtsx_trace(chip);
818 return STATUS_FAIL;
821 buf = kmalloc(4, GFP_KERNEL);
822 if (!buf) {
823 rtsx_trace(chip);
824 return STATUS_ERROR;
827 while (len) {
828 rtsx_stor_access_xfer_buf(buf, 1, srb, &index, &offset,
829 FROM_XFER_BUF);
831 rtsx_init_cmd(chip);
833 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE,
834 0x01, PINGPONG_BUFFER);
835 rtsx_add_cmd(chip, WRITE_REG_CMD, PPBUF_BASE2, 0xFF,
836 buf[0]);
837 if (first_byte) {
838 sf_program(chip, ins, 1, addr, 1);
839 first_byte = 0;
840 } else {
841 sf_program(chip, ins, 0, 0, 1);
844 retval = rtsx_send_cmd(chip, 0, 100);
845 if (retval < 0) {
846 kfree(buf);
847 rtsx_clear_spi_error(chip);
848 spi_set_err_code(chip, SPI_HW_ERR);
849 rtsx_trace(chip);
850 return STATUS_FAIL;
853 retval = sf_polling_status(chip, 100);
854 if (retval != STATUS_SUCCESS) {
855 kfree(buf);
856 rtsx_trace(chip);
857 return STATUS_FAIL;
860 len--;
863 kfree(buf);
865 retval = sf_disable_write(chip, SPI_WRDI);
866 if (retval != STATUS_SUCCESS) {
867 rtsx_trace(chip);
868 return STATUS_FAIL;
871 retval = sf_polling_status(chip, 100);
872 if (retval != STATUS_SUCCESS) {
873 rtsx_trace(chip);
874 return STATUS_FAIL;
876 } else if (program_mode == PAGE_PROGRAM) {
877 buf = kmalloc(SF_PAGE_LEN, GFP_KERNEL);
878 if (!buf) {
879 rtsx_trace(chip);
880 return STATUS_NOMEM;
883 while (len) {
884 u16 pagelen = SF_PAGE_LEN - (u8)addr;
886 if (pagelen > len)
887 pagelen = len;
889 retval = sf_enable_write(chip, SPI_WREN);
890 if (retval != STATUS_SUCCESS) {
891 kfree(buf);
892 rtsx_trace(chip);
893 return STATUS_FAIL;
896 rtsx_init_cmd(chip);
898 trans_dma_enable(DMA_TO_DEVICE, chip, 256, DMA_256);
899 sf_program(chip, ins, 1, addr, pagelen);
901 rtsx_send_cmd_no_wait(chip);
903 rtsx_stor_access_xfer_buf(buf, pagelen, srb, &index,
904 &offset, FROM_XFER_BUF);
906 retval = rtsx_transfer_data(chip, 0, buf, pagelen, 0,
907 DMA_TO_DEVICE, 100);
908 if (retval < 0) {
909 kfree(buf);
910 rtsx_clear_spi_error(chip);
911 spi_set_err_code(chip, SPI_HW_ERR);
912 rtsx_trace(chip);
913 return STATUS_FAIL;
916 retval = sf_polling_status(chip, 100);
917 if (retval != STATUS_SUCCESS) {
918 kfree(buf);
919 rtsx_trace(chip);
920 return STATUS_FAIL;
923 addr += pagelen;
924 len -= pagelen;
927 kfree(buf);
928 } else {
929 spi_set_err_code(chip, SPI_INVALID_COMMAND);
930 rtsx_trace(chip);
931 return STATUS_FAIL;
934 return STATUS_SUCCESS;
937 int spi_erase_flash(struct scsi_cmnd *srb, struct rtsx_chip *chip)
939 int retval;
940 u8 ins, erase_mode;
941 u32 addr;
943 spi_set_err_code(chip, SPI_NO_ERR);
945 ins = srb->cmnd[3];
946 addr = ((u32)(srb->cmnd[4]) << 16) | ((u32)(srb->cmnd[5])
947 << 8) | srb->cmnd[6];
948 erase_mode = srb->cmnd[9];
950 retval = spi_set_init_para(chip);
951 if (retval != STATUS_SUCCESS) {
952 spi_set_err_code(chip, SPI_HW_ERR);
953 rtsx_trace(chip);
954 return STATUS_FAIL;
957 if (erase_mode == PAGE_ERASE) {
958 retval = sf_enable_write(chip, SPI_WREN);
959 if (retval != STATUS_SUCCESS) {
960 rtsx_trace(chip);
961 return STATUS_FAIL;
964 retval = sf_erase(chip, ins, 1, addr);
965 if (retval != STATUS_SUCCESS) {
966 rtsx_trace(chip);
967 return STATUS_FAIL;
969 } else if (erase_mode == CHIP_ERASE) {
970 retval = sf_enable_write(chip, SPI_WREN);
971 if (retval != STATUS_SUCCESS) {
972 rtsx_trace(chip);
973 return STATUS_FAIL;
976 retval = sf_erase(chip, ins, 0, 0);
977 if (retval != STATUS_SUCCESS) {
978 rtsx_trace(chip);
979 return STATUS_FAIL;
981 } else {
982 spi_set_err_code(chip, SPI_INVALID_COMMAND);
983 rtsx_trace(chip);
984 return STATUS_FAIL;
987 return STATUS_SUCCESS;
990 int spi_write_flash_status(struct scsi_cmnd *srb, struct rtsx_chip *chip)
992 int retval;
993 u8 ins, status, ewsr;
995 ins = srb->cmnd[3];
996 status = srb->cmnd[4];
997 ewsr = srb->cmnd[5];
999 retval = spi_set_init_para(chip);
1000 if (retval != STATUS_SUCCESS) {
1001 spi_set_err_code(chip, SPI_HW_ERR);
1002 rtsx_trace(chip);
1003 return STATUS_FAIL;
1006 retval = sf_enable_write(chip, ewsr);
1007 if (retval != STATUS_SUCCESS) {
1008 rtsx_trace(chip);
1009 return STATUS_FAIL;
1012 rtsx_init_cmd(chip);
1014 rtsx_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE, 0x01,
1015 PINGPONG_BUFFER);
1017 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_COMMAND, 0xFF, ins);
1018 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_CA_NUMBER, 0xFF,
1019 SPI_COMMAND_BIT_8 | SPI_ADDRESS_BIT_24);
1020 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_LENGTH1, 0xFF, 0);
1021 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_LENGTH0, 0xFF, 1);
1022 rtsx_add_cmd(chip, WRITE_REG_CMD, PPBUF_BASE2, 0xFF, status);
1023 rtsx_add_cmd(chip, WRITE_REG_CMD, SPI_TRANSFER0, 0xFF,
1024 SPI_TRANSFER0_START | SPI_CDO_MODE0);
1025 rtsx_add_cmd(chip, CHECK_REG_CMD, SPI_TRANSFER0, SPI_TRANSFER0_END,
1026 SPI_TRANSFER0_END);
1028 retval = rtsx_send_cmd(chip, 0, 100);
1029 if (retval != STATUS_SUCCESS) {
1030 rtsx_clear_spi_error(chip);
1031 spi_set_err_code(chip, SPI_HW_ERR);
1032 rtsx_trace(chip);
1033 return STATUS_FAIL;
1036 return STATUS_SUCCESS;