1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
7 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
8 * that made this driver possible.
11 #include <linux/tifm.h>
12 #include <linux/memstick.h>
13 #include <linux/highmem.h>
14 #include <linux/scatterlist.h>
15 #include <linux/log2.h>
16 #include <linux/module.h>
19 #define DRIVER_NAME "tifm_ms"
22 module_param(no_dma
, bool, 0644);
25 * Some control bits of TIFM appear to conform to Sony's reference design,
26 * so I'm just assuming they all are.
29 #define TIFM_MS_STAT_DRQ 0x04000
30 #define TIFM_MS_STAT_MSINT 0x02000
31 #define TIFM_MS_STAT_RDY 0x01000
32 #define TIFM_MS_STAT_CRC 0x00200
33 #define TIFM_MS_STAT_TOE 0x00100
34 #define TIFM_MS_STAT_EMP 0x00020
35 #define TIFM_MS_STAT_FUL 0x00010
36 #define TIFM_MS_STAT_CED 0x00008
37 #define TIFM_MS_STAT_ERR 0x00004
38 #define TIFM_MS_STAT_BRQ 0x00002
39 #define TIFM_MS_STAT_CNK 0x00001
41 #define TIFM_MS_SYS_DMA 0x10000
42 #define TIFM_MS_SYS_RESET 0x08000
43 #define TIFM_MS_SYS_SRAC 0x04000
44 #define TIFM_MS_SYS_INTEN 0x02000
45 #define TIFM_MS_SYS_NOCRC 0x01000
46 #define TIFM_MS_SYS_INTCLR 0x00800
47 #define TIFM_MS_SYS_MSIEN 0x00400
48 #define TIFM_MS_SYS_FCLR 0x00200
49 #define TIFM_MS_SYS_FDIR 0x00100
50 #define TIFM_MS_SYS_DAM 0x00080
51 #define TIFM_MS_SYS_DRM 0x00040
52 #define TIFM_MS_SYS_DRQSL 0x00020
53 #define TIFM_MS_SYS_REI 0x00010
54 #define TIFM_MS_SYS_REO 0x00008
55 #define TIFM_MS_SYS_BSY_MASK 0x00007
57 #define TIFM_MS_SYS_FIFO (TIFM_MS_SYS_INTEN | TIFM_MS_SYS_MSIEN \
58 | TIFM_MS_SYS_FCLR | TIFM_MS_SYS_BSY_MASK)
69 struct timer_list timer
;
70 struct memstick_request
*req
;
71 struct tasklet_struct notify
;
72 unsigned int mode_mask
;
73 unsigned int block_pos
;
74 unsigned long timeout_jiffies
;
75 unsigned char eject
:1,
77 unsigned char cmd_flags
;
82 static unsigned int tifm_ms_read_data(struct tifm_ms
*host
,
83 unsigned char *buf
, unsigned int length
)
85 struct tifm_dev
*sock
= host
->dev
;
88 while (host
->io_pos
&& length
) {
89 buf
[off
++] = host
->io_word
& 0xff;
98 while (!(TIFM_MS_STAT_EMP
& readl(sock
->addr
+ SOCK_MS_STATUS
))) {
101 *(unsigned int *)(buf
+ off
) = __raw_readl(sock
->addr
108 && !(TIFM_MS_STAT_EMP
& readl(sock
->addr
+ SOCK_MS_STATUS
))) {
109 host
->io_word
= readl(sock
->addr
+ SOCK_MS_DATA
);
110 for (host
->io_pos
= 4; host
->io_pos
; --host
->io_pos
) {
111 buf
[off
++] = host
->io_word
& 0xff;
122 static unsigned int tifm_ms_write_data(struct tifm_ms
*host
,
123 unsigned char *buf
, unsigned int length
)
125 struct tifm_dev
*sock
= host
->dev
;
126 unsigned int off
= 0;
129 while (host
->io_pos
< 4 && length
) {
130 host
->io_word
|= buf
[off
++] << (host
->io_pos
* 8);
136 if (host
->io_pos
== 4
137 && !(TIFM_MS_STAT_FUL
& readl(sock
->addr
+ SOCK_MS_STATUS
))) {
138 writel(TIFM_MS_SYS_FDIR
| readl(sock
->addr
+ SOCK_MS_SYSTEM
),
139 sock
->addr
+ SOCK_MS_SYSTEM
);
140 writel(host
->io_word
, sock
->addr
+ SOCK_MS_DATA
);
143 } else if (host
->io_pos
) {
150 while (!(TIFM_MS_STAT_FUL
& readl(sock
->addr
+ SOCK_MS_STATUS
))) {
153 writel(TIFM_MS_SYS_FDIR
| readl(sock
->addr
+ SOCK_MS_SYSTEM
),
154 sock
->addr
+ SOCK_MS_SYSTEM
);
155 __raw_writel(*(unsigned int *)(buf
+ off
),
156 sock
->addr
+ SOCK_MS_DATA
);
163 host
->io_word
|= buf
[off
+ 2] << 16;
167 host
->io_word
|= buf
[off
+ 1] << 8;
171 host
->io_word
|= buf
[off
];
180 static unsigned int tifm_ms_transfer_data(struct tifm_ms
*host
)
182 struct tifm_dev
*sock
= host
->dev
;
185 unsigned int t_size
, p_cnt
;
188 unsigned long flags
= 0;
190 if (host
->req
->long_data
) {
191 length
= host
->req
->sg
.length
- host
->block_pos
;
192 off
= host
->req
->sg
.offset
+ host
->block_pos
;
194 length
= host
->req
->data_len
- host
->block_pos
;
197 dev_dbg(&sock
->dev
, "fifo data transfer, %d, %d\n", length
,
201 unsigned int uninitialized_var(p_off
);
203 if (host
->req
->long_data
) {
204 pg
= nth_page(sg_page(&host
->req
->sg
),
206 p_off
= offset_in_page(off
);
207 p_cnt
= PAGE_SIZE
- p_off
;
208 p_cnt
= min(p_cnt
, length
);
210 local_irq_save(flags
);
211 buf
= kmap_atomic(pg
) + p_off
;
213 buf
= host
->req
->data
+ host
->block_pos
;
214 p_cnt
= host
->req
->data_len
- host
->block_pos
;
217 t_size
= host
->req
->data_dir
== WRITE
218 ? tifm_ms_write_data(host
, buf
, p_cnt
)
219 : tifm_ms_read_data(host
, buf
, p_cnt
);
221 if (host
->req
->long_data
) {
222 kunmap_atomic(buf
- p_off
);
223 local_irq_restore(flags
);
228 host
->block_pos
+= t_size
;
233 dev_dbg(&sock
->dev
, "fifo data transfer, %d remaining\n", length
);
234 if (!length
&& (host
->req
->data_dir
== WRITE
)) {
236 writel(TIFM_MS_SYS_FDIR
237 | readl(sock
->addr
+ SOCK_MS_SYSTEM
),
238 sock
->addr
+ SOCK_MS_SYSTEM
);
239 writel(host
->io_word
, sock
->addr
+ SOCK_MS_DATA
);
241 writel(TIFM_MS_SYS_FDIR
242 | readl(sock
->addr
+ SOCK_MS_SYSTEM
),
243 sock
->addr
+ SOCK_MS_SYSTEM
);
244 writel(0, sock
->addr
+ SOCK_MS_DATA
);
246 readl(sock
->addr
+ SOCK_MS_DATA
);
252 static int tifm_ms_issue_cmd(struct tifm_ms
*host
)
254 struct tifm_dev
*sock
= host
->dev
;
255 unsigned int data_len
, cmd
, sys_param
;
263 host
->use_dma
= !no_dma
;
265 if (host
->req
->long_data
) {
266 data_len
= host
->req
->sg
.length
;
267 if (!is_power_of_2(data_len
))
270 data_len
= host
->req
->data_len
;
274 writel(TIFM_FIFO_INT_SETALL
,
275 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
276 writel(TIFM_FIFO_ENABLE
,
277 sock
->addr
+ SOCK_FIFO_CONTROL
);
280 if (1 != tifm_map_sg(sock
, &host
->req
->sg
, 1,
281 host
->req
->data_dir
== READ
283 : PCI_DMA_TODEVICE
)) {
284 host
->req
->error
= -ENOMEM
;
285 return host
->req
->error
;
287 data_len
= sg_dma_len(&host
->req
->sg
);
289 writel(ilog2(data_len
) - 2,
290 sock
->addr
+ SOCK_FIFO_PAGE_SIZE
);
291 writel(TIFM_FIFO_INTMASK
,
292 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_SET
);
293 sys_param
= TIFM_DMA_EN
| (1 << 8);
294 if (host
->req
->data_dir
== WRITE
)
295 sys_param
|= TIFM_DMA_TX
;
297 writel(TIFM_FIFO_INTMASK
,
298 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_SET
);
300 writel(sg_dma_address(&host
->req
->sg
),
301 sock
->addr
+ SOCK_DMA_ADDRESS
);
302 writel(sys_param
, sock
->addr
+ SOCK_DMA_CONTROL
);
304 writel(host
->mode_mask
| TIFM_MS_SYS_FIFO
,
305 sock
->addr
+ SOCK_MS_SYSTEM
);
307 writel(TIFM_FIFO_MORE
,
308 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_SET
);
311 mod_timer(&host
->timer
, jiffies
+ host
->timeout_jiffies
);
312 writel(TIFM_CTRL_LED
| readl(sock
->addr
+ SOCK_CONTROL
),
313 sock
->addr
+ SOCK_CONTROL
);
314 host
->req
->error
= 0;
316 sys_param
= readl(sock
->addr
+ SOCK_MS_SYSTEM
);
317 sys_param
|= TIFM_MS_SYS_INTCLR
;
320 sys_param
|= TIFM_MS_SYS_DMA
;
322 sys_param
&= ~TIFM_MS_SYS_DMA
;
324 writel(sys_param
, sock
->addr
+ SOCK_MS_SYSTEM
);
326 cmd
= (host
->req
->tpc
& 0xf) << 12;
328 writel(cmd
, sock
->addr
+ SOCK_MS_COMMAND
);
330 dev_dbg(&sock
->dev
, "executing TPC %x, %x\n", cmd
, sys_param
);
334 static void tifm_ms_complete_cmd(struct tifm_ms
*host
)
336 struct tifm_dev
*sock
= host
->dev
;
337 struct memstick_host
*msh
= tifm_get_drvdata(sock
);
340 del_timer(&host
->timer
);
342 host
->req
->int_reg
= readl(sock
->addr
+ SOCK_MS_STATUS
) & 0xff;
343 host
->req
->int_reg
= (host
->req
->int_reg
& 1)
344 | ((host
->req
->int_reg
<< 4) & 0xe0);
346 writel(TIFM_FIFO_INT_SETALL
,
347 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
348 writel(TIFM_DMA_RESET
, sock
->addr
+ SOCK_DMA_CONTROL
);
351 tifm_unmap_sg(sock
, &host
->req
->sg
, 1,
352 host
->req
->data_dir
== READ
357 writel((~TIFM_CTRL_LED
) & readl(sock
->addr
+ SOCK_CONTROL
),
358 sock
->addr
+ SOCK_CONTROL
);
360 dev_dbg(&sock
->dev
, "TPC complete\n");
362 rc
= memstick_next_req(msh
, &host
->req
);
363 } while (!rc
&& tifm_ms_issue_cmd(host
));
366 static int tifm_ms_check_status(struct tifm_ms
*host
)
368 if (!host
->req
->error
) {
369 if (!(host
->cmd_flags
& CMD_READY
))
371 if (!(host
->cmd_flags
& FIFO_READY
))
373 if (host
->req
->need_card_int
374 && !(host
->cmd_flags
& CARD_INT
))
380 /* Called from interrupt handler */
381 static void tifm_ms_data_event(struct tifm_dev
*sock
)
383 struct tifm_ms
*host
;
384 unsigned int fifo_status
= 0, host_status
= 0;
387 spin_lock(&sock
->lock
);
388 host
= memstick_priv((struct memstick_host
*)tifm_get_drvdata(sock
));
389 fifo_status
= readl(sock
->addr
+ SOCK_DMA_FIFO_STATUS
);
390 host_status
= readl(sock
->addr
+ SOCK_MS_STATUS
);
392 "data event: fifo_status %x, host_status %x, flags %x\n",
393 fifo_status
, host_status
, host
->cmd_flags
);
396 if (host
->use_dma
&& (fifo_status
& 1)) {
397 host
->cmd_flags
|= FIFO_READY
;
398 rc
= tifm_ms_check_status(host
);
400 if (!host
->use_dma
&& (fifo_status
& TIFM_FIFO_MORE
)) {
401 if (!tifm_ms_transfer_data(host
)) {
402 host
->cmd_flags
|= FIFO_READY
;
403 rc
= tifm_ms_check_status(host
);
408 writel(fifo_status
, sock
->addr
+ SOCK_DMA_FIFO_STATUS
);
410 tifm_ms_complete_cmd(host
);
412 spin_unlock(&sock
->lock
);
416 /* Called from interrupt handler */
417 static void tifm_ms_card_event(struct tifm_dev
*sock
)
419 struct tifm_ms
*host
;
420 unsigned int host_status
= 0;
423 spin_lock(&sock
->lock
);
424 host
= memstick_priv((struct memstick_host
*)tifm_get_drvdata(sock
));
425 host_status
= readl(sock
->addr
+ SOCK_MS_STATUS
);
426 dev_dbg(&sock
->dev
, "host event: host_status %x, flags %x\n",
427 host_status
, host
->cmd_flags
);
430 if (host_status
& TIFM_MS_STAT_TOE
)
431 host
->req
->error
= -ETIME
;
432 else if (host_status
& TIFM_MS_STAT_CRC
)
433 host
->req
->error
= -EILSEQ
;
435 if (host_status
& TIFM_MS_STAT_RDY
)
436 host
->cmd_flags
|= CMD_READY
;
438 if (host_status
& TIFM_MS_STAT_MSINT
)
439 host
->cmd_flags
|= CARD_INT
;
441 rc
= tifm_ms_check_status(host
);
445 writel(TIFM_MS_SYS_INTCLR
| readl(sock
->addr
+ SOCK_MS_SYSTEM
),
446 sock
->addr
+ SOCK_MS_SYSTEM
);
449 tifm_ms_complete_cmd(host
);
451 spin_unlock(&sock
->lock
);
455 static void tifm_ms_req_tasklet(unsigned long data
)
457 struct memstick_host
*msh
= (struct memstick_host
*)data
;
458 struct tifm_ms
*host
= memstick_priv(msh
);
459 struct tifm_dev
*sock
= host
->dev
;
463 spin_lock_irqsave(&sock
->lock
, flags
);
467 rc
= memstick_next_req(msh
, &host
->req
);
469 host
->req
->error
= -ETIME
;
471 spin_unlock_irqrestore(&sock
->lock
, flags
);
476 rc
= memstick_next_req(msh
, &host
->req
);
477 } while (!rc
&& tifm_ms_issue_cmd(host
));
479 spin_unlock_irqrestore(&sock
->lock
, flags
);
482 static void tifm_ms_dummy_submit(struct memstick_host
*msh
)
487 static void tifm_ms_submit_req(struct memstick_host
*msh
)
489 struct tifm_ms
*host
= memstick_priv(msh
);
491 tasklet_schedule(&host
->notify
);
494 static int tifm_ms_set_param(struct memstick_host
*msh
,
495 enum memstick_param param
,
498 struct tifm_ms
*host
= memstick_priv(msh
);
499 struct tifm_dev
*sock
= host
->dev
;
503 /* also affected by media detection mechanism */
504 if (value
== MEMSTICK_POWER_ON
) {
505 host
->mode_mask
= TIFM_MS_SYS_SRAC
| TIFM_MS_SYS_REI
;
506 writel(TIFM_MS_SYS_RESET
, sock
->addr
+ SOCK_MS_SYSTEM
);
507 writel(TIFM_MS_SYS_FCLR
| TIFM_MS_SYS_INTCLR
,
508 sock
->addr
+ SOCK_MS_SYSTEM
);
509 writel(0xffffffff, sock
->addr
+ SOCK_MS_STATUS
);
510 } else if (value
== MEMSTICK_POWER_OFF
) {
511 writel(TIFM_MS_SYS_FCLR
| TIFM_MS_SYS_INTCLR
,
512 sock
->addr
+ SOCK_MS_SYSTEM
);
513 writel(0xffffffff, sock
->addr
+ SOCK_MS_STATUS
);
517 case MEMSTICK_INTERFACE
:
518 if (value
== MEMSTICK_SERIAL
) {
519 host
->mode_mask
= TIFM_MS_SYS_SRAC
| TIFM_MS_SYS_REI
;
520 writel((~TIFM_CTRL_FAST_CLK
)
521 & readl(sock
->addr
+ SOCK_CONTROL
),
522 sock
->addr
+ SOCK_CONTROL
);
523 } else if (value
== MEMSTICK_PAR4
) {
525 writel(TIFM_CTRL_FAST_CLK
526 | readl(sock
->addr
+ SOCK_CONTROL
),
527 sock
->addr
+ SOCK_CONTROL
);
536 static void tifm_ms_abort(struct timer_list
*t
)
538 struct tifm_ms
*host
= from_timer(host
, t
, timer
);
540 dev_dbg(&host
->dev
->dev
, "status %x\n",
541 readl(host
->dev
->addr
+ SOCK_MS_STATUS
));
543 "%s : card failed to respond for a long period of time "
545 dev_name(&host
->dev
->dev
), host
->req
? host
->req
->tpc
: 0,
548 tifm_eject(host
->dev
);
551 static int tifm_ms_probe(struct tifm_dev
*sock
)
553 struct memstick_host
*msh
;
554 struct tifm_ms
*host
;
557 if (!(TIFM_SOCK_STATE_OCCUPIED
558 & readl(sock
->addr
+ SOCK_PRESENT_STATE
))) {
559 printk(KERN_WARNING
"%s : card gone, unexpectedly\n",
560 dev_name(&sock
->dev
));
564 msh
= memstick_alloc_host(sizeof(struct tifm_ms
), &sock
->dev
);
568 host
= memstick_priv(msh
);
569 tifm_set_drvdata(sock
, msh
);
571 host
->timeout_jiffies
= msecs_to_jiffies(1000);
573 timer_setup(&host
->timer
, tifm_ms_abort
, 0);
574 tasklet_init(&host
->notify
, tifm_ms_req_tasklet
, (unsigned long)msh
);
576 msh
->request
= tifm_ms_submit_req
;
577 msh
->set_param
= tifm_ms_set_param
;
578 sock
->card_event
= tifm_ms_card_event
;
579 sock
->data_event
= tifm_ms_data_event
;
580 if (tifm_has_ms_pif(sock
))
581 msh
->caps
|= MEMSTICK_CAP_PAR4
;
583 rc
= memstick_add_host(msh
);
587 memstick_free_host(msh
);
591 static void tifm_ms_remove(struct tifm_dev
*sock
)
593 struct memstick_host
*msh
= tifm_get_drvdata(sock
);
594 struct tifm_ms
*host
= memstick_priv(msh
);
598 msh
->request
= tifm_ms_dummy_submit
;
599 tasklet_kill(&host
->notify
);
600 spin_lock_irqsave(&sock
->lock
, flags
);
603 del_timer(&host
->timer
);
604 writel(TIFM_FIFO_INT_SETALL
,
605 sock
->addr
+ SOCK_DMA_FIFO_INT_ENABLE_CLEAR
);
606 writel(TIFM_DMA_RESET
, sock
->addr
+ SOCK_DMA_CONTROL
);
608 tifm_unmap_sg(sock
, &host
->req
->sg
, 1,
609 host
->req
->data_dir
== READ
611 : PCI_DMA_FROMDEVICE
);
612 host
->req
->error
= -ETIME
;
615 rc
= memstick_next_req(msh
, &host
->req
);
617 host
->req
->error
= -ETIME
;
620 spin_unlock_irqrestore(&sock
->lock
, flags
);
622 memstick_remove_host(msh
);
623 memstick_free_host(msh
);
628 static int tifm_ms_suspend(struct tifm_dev
*sock
, pm_message_t state
)
630 struct memstick_host
*msh
= tifm_get_drvdata(sock
);
632 memstick_suspend_host(msh
);
636 static int tifm_ms_resume(struct tifm_dev
*sock
)
638 struct memstick_host
*msh
= tifm_get_drvdata(sock
);
640 memstick_resume_host(msh
);
646 #define tifm_ms_suspend NULL
647 #define tifm_ms_resume NULL
649 #endif /* CONFIG_PM */
651 static struct tifm_device_id tifm_ms_id_tbl
[] = {
652 { TIFM_TYPE_MS
}, { 0 }
655 static struct tifm_driver tifm_ms_driver
= {
660 .id_table
= tifm_ms_id_tbl
,
661 .probe
= tifm_ms_probe
,
662 .remove
= tifm_ms_remove
,
663 .suspend
= tifm_ms_suspend
,
664 .resume
= tifm_ms_resume
667 static int __init
tifm_ms_init(void)
669 return tifm_register_driver(&tifm_ms_driver
);
672 static void __exit
tifm_ms_exit(void)
674 tifm_unregister_driver(&tifm_ms_driver
);
677 MODULE_AUTHOR("Alex Dubov");
678 MODULE_DESCRIPTION("TI FlashMedia MemoryStick driver");
679 MODULE_LICENSE("GPL");
680 MODULE_DEVICE_TABLE(tifm
, tifm_ms_id_tbl
);
682 module_init(tifm_ms_init
);
683 module_exit(tifm_ms_exit
);