2 * jmb38x_ms.c - JMicron jmb38x MemoryStick card reader
4 * Copyright (C) 2008 Alex Dubov <oakad@yahoo.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/spinlock.h>
13 #include <linux/interrupt.h>
14 #include <linux/pci.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/delay.h>
17 #include <linux/highmem.h>
18 #include <linux/memstick.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
22 #define DRIVER_NAME "jmb38x_ms"
25 module_param(no_dma
, bool, 0644);
38 INT_STATUS_ENABLE
= 0x28,
39 INT_SIGNAL_ENABLE
= 0x2c,
42 PAD_OUTPUT_ENABLE
= 0x38,
51 struct jmb38x_ms_host
{
52 struct jmb38x_ms
*chip
;
55 struct tasklet_struct notify
;
59 unsigned int block_pos
;
60 unsigned long timeout_jiffies
;
61 struct timer_list timer
;
62 struct memstick_request
*req
;
63 unsigned char cmd_flags
;
66 unsigned int io_word
[2];
72 struct memstick_host
*hosts
[];
75 #define BLOCK_COUNT_MASK 0xffff0000
76 #define BLOCK_SIZE_MASK 0x00000fff
78 #define DMA_CONTROL_ENABLE 0x00000001
80 #define TPC_DATA_SEL 0x00008000
81 #define TPC_DIR 0x00004000
82 #define TPC_WAIT_INT 0x00002000
83 #define TPC_GET_INT 0x00000800
84 #define TPC_CODE_SZ_MASK 0x00000700
85 #define TPC_DATA_SZ_MASK 0x00000007
87 #define HOST_CONTROL_TDELAY_EN 0x00040000
88 #define HOST_CONTROL_HW_OC_P 0x00010000
89 #define HOST_CONTROL_RESET_REQ 0x00008000
90 #define HOST_CONTROL_REI 0x00004000
91 #define HOST_CONTROL_LED 0x00000400
92 #define HOST_CONTROL_FAST_CLK 0x00000200
93 #define HOST_CONTROL_RESET 0x00000100
94 #define HOST_CONTROL_POWER_EN 0x00000080
95 #define HOST_CONTROL_CLOCK_EN 0x00000040
96 #define HOST_CONTROL_REO 0x00000008
97 #define HOST_CONTROL_IF_SHIFT 4
99 #define HOST_CONTROL_IF_SERIAL 0x0
100 #define HOST_CONTROL_IF_PAR4 0x1
101 #define HOST_CONTROL_IF_PAR8 0x3
103 #define STATUS_BUSY 0x00080000
104 #define STATUS_MS_DAT7 0x00040000
105 #define STATUS_MS_DAT6 0x00020000
106 #define STATUS_MS_DAT5 0x00010000
107 #define STATUS_MS_DAT4 0x00008000
108 #define STATUS_MS_DAT3 0x00004000
109 #define STATUS_MS_DAT2 0x00002000
110 #define STATUS_MS_DAT1 0x00001000
111 #define STATUS_MS_DAT0 0x00000800
112 #define STATUS_HAS_MEDIA 0x00000400
113 #define STATUS_FIFO_EMPTY 0x00000200
114 #define STATUS_FIFO_FULL 0x00000100
115 #define STATUS_MS_CED 0x00000080
116 #define STATUS_MS_ERR 0x00000040
117 #define STATUS_MS_BRQ 0x00000020
118 #define STATUS_MS_CNK 0x00000001
120 #define INT_STATUS_TPC_ERR 0x00080000
121 #define INT_STATUS_CRC_ERR 0x00040000
122 #define INT_STATUS_TIMER_TO 0x00020000
123 #define INT_STATUS_HSK_TO 0x00010000
124 #define INT_STATUS_ANY_ERR 0x00008000
125 #define INT_STATUS_FIFO_WRDY 0x00000080
126 #define INT_STATUS_FIFO_RRDY 0x00000040
127 #define INT_STATUS_MEDIA_OUT 0x00000010
128 #define INT_STATUS_MEDIA_IN 0x00000008
129 #define INT_STATUS_DMA_BOUNDARY 0x00000004
130 #define INT_STATUS_EOTRAN 0x00000002
131 #define INT_STATUS_EOTPC 0x00000001
133 #define INT_STATUS_ALL 0x000f801f
135 #define PAD_OUTPUT_ENABLE_MS 0x0F3F
137 #define PAD_PU_PD_OFF 0x7FFF0000
138 #define PAD_PU_PD_ON_MS_SOCK0 0x5f8f0000
139 #define PAD_PU_PD_ON_MS_SOCK1 0x0f0f0000
141 #define CLOCK_CONTROL_BY_MMIO 0x00000008
142 #define CLOCK_CONTROL_40MHZ 0x00000001
143 #define CLOCK_CONTROL_50MHZ 0x00000002
144 #define CLOCK_CONTROL_60MHZ 0x00000010
145 #define CLOCK_CONTROL_62_5MHZ 0x00000004
146 #define CLOCK_CONTROL_OFF 0x00000000
148 #define PCI_CTL_CLOCK_DLY_ADDR 0x000000b0
157 static unsigned int jmb38x_ms_read_data(struct jmb38x_ms_host
*host
,
158 unsigned char *buf
, unsigned int length
)
160 unsigned int off
= 0;
162 while (host
->io_pos
&& length
) {
163 buf
[off
++] = host
->io_word
[0] & 0xff;
164 host
->io_word
[0] >>= 8;
172 while (!(STATUS_FIFO_EMPTY
& readl(host
->addr
+ STATUS
))) {
175 *(unsigned int *)(buf
+ off
) = __raw_readl(host
->addr
+ DATA
);
181 && !(STATUS_FIFO_EMPTY
& readl(host
->addr
+ STATUS
))) {
182 host
->io_word
[0] = readl(host
->addr
+ DATA
);
183 for (host
->io_pos
= 4; host
->io_pos
; --host
->io_pos
) {
184 buf
[off
++] = host
->io_word
[0] & 0xff;
185 host
->io_word
[0] >>= 8;
195 static unsigned int jmb38x_ms_read_reg_data(struct jmb38x_ms_host
*host
,
199 unsigned int off
= 0;
201 while (host
->io_pos
> 4 && length
) {
202 buf
[off
++] = host
->io_word
[0] & 0xff;
203 host
->io_word
[0] >>= 8;
211 while (host
->io_pos
&& length
) {
212 buf
[off
++] = host
->io_word
[1] & 0xff;
213 host
->io_word
[1] >>= 8;
221 static unsigned int jmb38x_ms_write_data(struct jmb38x_ms_host
*host
,
225 unsigned int off
= 0;
228 while (host
->io_pos
< 4 && length
) {
229 host
->io_word
[0] |= buf
[off
++] << (host
->io_pos
* 8);
235 if (host
->io_pos
== 4
236 && !(STATUS_FIFO_FULL
& readl(host
->addr
+ STATUS
))) {
237 writel(host
->io_word
[0], host
->addr
+ DATA
);
239 host
->io_word
[0] = 0;
240 } else if (host
->io_pos
) {
247 while (!(STATUS_FIFO_FULL
& readl(host
->addr
+ STATUS
))) {
251 __raw_writel(*(unsigned int *)(buf
+ off
),
259 host
->io_word
[0] |= buf
[off
+ 2] << 16;
262 host
->io_word
[0] |= buf
[off
+ 1] << 8;
265 host
->io_word
[0] |= buf
[off
];
274 static unsigned int jmb38x_ms_write_reg_data(struct jmb38x_ms_host
*host
,
278 unsigned int off
= 0;
280 while (host
->io_pos
< 4 && length
) {
281 host
->io_word
[0] &= ~(0xff << (host
->io_pos
* 8));
282 host
->io_word
[0] |= buf
[off
++] << (host
->io_pos
* 8);
290 while (host
->io_pos
< 8 && length
) {
291 host
->io_word
[1] &= ~(0xff << (host
->io_pos
* 8));
292 host
->io_word
[1] |= buf
[off
++] << (host
->io_pos
* 8);
300 static int jmb38x_ms_transfer_data(struct jmb38x_ms_host
*host
)
304 unsigned int t_size
, p_cnt
;
307 unsigned long flags
= 0;
309 if (host
->req
->long_data
) {
310 length
= host
->req
->sg
.length
- host
->block_pos
;
311 off
= host
->req
->sg
.offset
+ host
->block_pos
;
313 length
= host
->req
->data_len
- host
->block_pos
;
318 unsigned int uninitialized_var(p_off
);
320 if (host
->req
->long_data
) {
321 pg
= nth_page(sg_page(&host
->req
->sg
),
323 p_off
= offset_in_page(off
);
324 p_cnt
= PAGE_SIZE
- p_off
;
325 p_cnt
= min(p_cnt
, length
);
327 local_irq_save(flags
);
328 buf
= kmap_atomic(pg
) + p_off
;
330 buf
= host
->req
->data
+ host
->block_pos
;
331 p_cnt
= host
->req
->data_len
- host
->block_pos
;
334 if (host
->req
->data_dir
== WRITE
)
335 t_size
= !(host
->cmd_flags
& REG_DATA
)
336 ? jmb38x_ms_write_data(host
, buf
, p_cnt
)
337 : jmb38x_ms_write_reg_data(host
, buf
, p_cnt
);
339 t_size
= !(host
->cmd_flags
& REG_DATA
)
340 ? jmb38x_ms_read_data(host
, buf
, p_cnt
)
341 : jmb38x_ms_read_reg_data(host
, buf
, p_cnt
);
343 if (host
->req
->long_data
) {
344 kunmap_atomic(buf
- p_off
);
345 local_irq_restore(flags
);
350 host
->block_pos
+= t_size
;
355 if (!length
&& host
->req
->data_dir
== WRITE
) {
356 if (host
->cmd_flags
& REG_DATA
) {
357 writel(host
->io_word
[0], host
->addr
+ TPC_P0
);
358 writel(host
->io_word
[1], host
->addr
+ TPC_P1
);
359 } else if (host
->io_pos
) {
360 writel(host
->io_word
[0], host
->addr
+ DATA
);
367 static int jmb38x_ms_issue_cmd(struct memstick_host
*msh
)
369 struct jmb38x_ms_host
*host
= memstick_priv(msh
);
371 unsigned int data_len
, cmd
, t_val
;
373 if (!(STATUS_HAS_MEDIA
& readl(host
->addr
+ STATUS
))) {
374 dev_dbg(&msh
->dev
, "no media status\n");
375 host
->req
->error
= -ETIME
;
376 return host
->req
->error
;
379 dev_dbg(&msh
->dev
, "control %08x\n", readl(host
->addr
+ HOST_CONTROL
));
380 dev_dbg(&msh
->dev
, "status %08x\n", readl(host
->addr
+ INT_STATUS
));
381 dev_dbg(&msh
->dev
, "hstatus %08x\n", readl(host
->addr
+ STATUS
));
386 host
->io_word
[0] = 0;
387 host
->io_word
[1] = 0;
389 cmd
= host
->req
->tpc
<< 16;
392 if (host
->req
->data_dir
== READ
)
395 if (host
->req
->need_card_int
) {
396 if (host
->ifmode
== MEMSTICK_SERIAL
)
402 data
= host
->req
->data
;
405 host
->cmd_flags
|= DMA_DATA
;
407 if (host
->req
->long_data
) {
408 data_len
= host
->req
->sg
.length
;
410 data_len
= host
->req
->data_len
;
411 host
->cmd_flags
&= ~DMA_DATA
;
415 cmd
&= ~(TPC_DATA_SEL
| 0xf);
416 host
->cmd_flags
|= REG_DATA
;
417 cmd
|= data_len
& 0xf;
418 host
->cmd_flags
&= ~DMA_DATA
;
421 if (host
->cmd_flags
& DMA_DATA
) {
422 if (1 != dma_map_sg(&host
->chip
->pdev
->dev
, &host
->req
->sg
, 1,
423 host
->req
->data_dir
== READ
426 host
->req
->error
= -ENOMEM
;
427 return host
->req
->error
;
429 data_len
= sg_dma_len(&host
->req
->sg
);
430 writel(sg_dma_address(&host
->req
->sg
),
431 host
->addr
+ DMA_ADDRESS
);
432 writel(((1 << 16) & BLOCK_COUNT_MASK
)
433 | (data_len
& BLOCK_SIZE_MASK
),
435 writel(DMA_CONTROL_ENABLE
, host
->addr
+ DMA_CONTROL
);
436 } else if (!(host
->cmd_flags
& REG_DATA
)) {
437 writel(((1 << 16) & BLOCK_COUNT_MASK
)
438 | (data_len
& BLOCK_SIZE_MASK
),
440 t_val
= readl(host
->addr
+ INT_STATUS_ENABLE
);
441 t_val
|= host
->req
->data_dir
== READ
442 ? INT_STATUS_FIFO_RRDY
443 : INT_STATUS_FIFO_WRDY
;
445 writel(t_val
, host
->addr
+ INT_STATUS_ENABLE
);
446 writel(t_val
, host
->addr
+ INT_SIGNAL_ENABLE
);
448 cmd
&= ~(TPC_DATA_SEL
| 0xf);
449 host
->cmd_flags
|= REG_DATA
;
450 cmd
|= data_len
& 0xf;
452 if (host
->req
->data_dir
== WRITE
) {
453 jmb38x_ms_transfer_data(host
);
454 writel(host
->io_word
[0], host
->addr
+ TPC_P0
);
455 writel(host
->io_word
[1], host
->addr
+ TPC_P1
);
459 mod_timer(&host
->timer
, jiffies
+ host
->timeout_jiffies
);
460 writel(HOST_CONTROL_LED
| readl(host
->addr
+ HOST_CONTROL
),
461 host
->addr
+ HOST_CONTROL
);
462 host
->req
->error
= 0;
464 writel(cmd
, host
->addr
+ TPC
);
465 dev_dbg(&msh
->dev
, "executing TPC %08x, len %x\n", cmd
, data_len
);
470 static void jmb38x_ms_complete_cmd(struct memstick_host
*msh
, int last
)
472 struct jmb38x_ms_host
*host
= memstick_priv(msh
);
473 unsigned int t_val
= 0;
476 del_timer(&host
->timer
);
478 dev_dbg(&msh
->dev
, "c control %08x\n",
479 readl(host
->addr
+ HOST_CONTROL
));
480 dev_dbg(&msh
->dev
, "c status %08x\n",
481 readl(host
->addr
+ INT_STATUS
));
482 dev_dbg(&msh
->dev
, "c hstatus %08x\n", readl(host
->addr
+ STATUS
));
484 host
->req
->int_reg
= readl(host
->addr
+ STATUS
) & 0xff;
486 writel(0, host
->addr
+ BLOCK
);
487 writel(0, host
->addr
+ DMA_CONTROL
);
489 if (host
->cmd_flags
& DMA_DATA
) {
490 dma_unmap_sg(&host
->chip
->pdev
->dev
, &host
->req
->sg
, 1,
491 host
->req
->data_dir
== READ
492 ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
494 t_val
= readl(host
->addr
+ INT_STATUS_ENABLE
);
495 if (host
->req
->data_dir
== READ
)
496 t_val
&= ~INT_STATUS_FIFO_RRDY
;
498 t_val
&= ~INT_STATUS_FIFO_WRDY
;
500 writel(t_val
, host
->addr
+ INT_STATUS_ENABLE
);
501 writel(t_val
, host
->addr
+ INT_SIGNAL_ENABLE
);
504 writel((~HOST_CONTROL_LED
) & readl(host
->addr
+ HOST_CONTROL
),
505 host
->addr
+ HOST_CONTROL
);
509 rc
= memstick_next_req(msh
, &host
->req
);
510 } while (!rc
&& jmb38x_ms_issue_cmd(msh
));
513 rc
= memstick_next_req(msh
, &host
->req
);
515 host
->req
->error
= -ETIME
;
520 static irqreturn_t
jmb38x_ms_isr(int irq
, void *dev_id
)
522 struct memstick_host
*msh
= dev_id
;
523 struct jmb38x_ms_host
*host
= memstick_priv(msh
);
524 unsigned int irq_status
;
526 spin_lock(&host
->lock
);
527 irq_status
= readl(host
->addr
+ INT_STATUS
);
528 dev_dbg(&host
->chip
->pdev
->dev
, "irq_status = %08x\n", irq_status
);
529 if (irq_status
== 0 || irq_status
== (~0)) {
530 spin_unlock(&host
->lock
);
535 if (irq_status
& INT_STATUS_ANY_ERR
) {
536 if (irq_status
& INT_STATUS_CRC_ERR
)
537 host
->req
->error
= -EILSEQ
;
538 else if (irq_status
& INT_STATUS_TPC_ERR
) {
539 dev_dbg(&host
->chip
->pdev
->dev
, "TPC_ERR\n");
540 jmb38x_ms_complete_cmd(msh
, 0);
542 host
->req
->error
= -ETIME
;
544 if (host
->cmd_flags
& DMA_DATA
) {
545 if (irq_status
& INT_STATUS_EOTRAN
)
546 host
->cmd_flags
|= FIFO_READY
;
548 if (irq_status
& (INT_STATUS_FIFO_RRDY
549 | INT_STATUS_FIFO_WRDY
))
550 jmb38x_ms_transfer_data(host
);
552 if (irq_status
& INT_STATUS_EOTRAN
) {
553 jmb38x_ms_transfer_data(host
);
554 host
->cmd_flags
|= FIFO_READY
;
558 if (irq_status
& INT_STATUS_EOTPC
) {
559 host
->cmd_flags
|= CMD_READY
;
560 if (host
->cmd_flags
& REG_DATA
) {
561 if (host
->req
->data_dir
== READ
) {
570 jmb38x_ms_transfer_data(host
);
572 host
->cmd_flags
|= FIFO_READY
;
578 if (irq_status
& (INT_STATUS_MEDIA_IN
| INT_STATUS_MEDIA_OUT
)) {
579 dev_dbg(&host
->chip
->pdev
->dev
, "media changed\n");
580 memstick_detect_change(msh
);
583 writel(irq_status
, host
->addr
+ INT_STATUS
);
586 && (((host
->cmd_flags
& CMD_READY
)
587 && (host
->cmd_flags
& FIFO_READY
))
588 || host
->req
->error
))
589 jmb38x_ms_complete_cmd(msh
, 0);
591 spin_unlock(&host
->lock
);
595 static void jmb38x_ms_abort(unsigned long data
)
597 struct memstick_host
*msh
= (struct memstick_host
*)data
;
598 struct jmb38x_ms_host
*host
= memstick_priv(msh
);
601 dev_dbg(&host
->chip
->pdev
->dev
, "abort\n");
602 spin_lock_irqsave(&host
->lock
, flags
);
604 host
->req
->error
= -ETIME
;
605 jmb38x_ms_complete_cmd(msh
, 0);
607 spin_unlock_irqrestore(&host
->lock
, flags
);
610 static void jmb38x_ms_req_tasklet(unsigned long data
)
612 struct memstick_host
*msh
= (struct memstick_host
*)data
;
613 struct jmb38x_ms_host
*host
= memstick_priv(msh
);
617 spin_lock_irqsave(&host
->lock
, flags
);
620 rc
= memstick_next_req(msh
, &host
->req
);
621 dev_dbg(&host
->chip
->pdev
->dev
, "tasklet req %d\n", rc
);
622 } while (!rc
&& jmb38x_ms_issue_cmd(msh
));
624 spin_unlock_irqrestore(&host
->lock
, flags
);
627 static void jmb38x_ms_dummy_submit(struct memstick_host
*msh
)
632 static void jmb38x_ms_submit_req(struct memstick_host
*msh
)
634 struct jmb38x_ms_host
*host
= memstick_priv(msh
);
636 tasklet_schedule(&host
->notify
);
639 static int jmb38x_ms_reset(struct jmb38x_ms_host
*host
)
643 writel(HOST_CONTROL_RESET_REQ
| HOST_CONTROL_CLOCK_EN
644 | readl(host
->addr
+ HOST_CONTROL
),
645 host
->addr
+ HOST_CONTROL
);
648 for (cnt
= 0; cnt
< 20; ++cnt
) {
649 if (!(HOST_CONTROL_RESET_REQ
650 & readl(host
->addr
+ HOST_CONTROL
)))
655 dev_dbg(&host
->chip
->pdev
->dev
, "reset_req timeout\n");
658 writel(HOST_CONTROL_RESET
| HOST_CONTROL_CLOCK_EN
659 | readl(host
->addr
+ HOST_CONTROL
),
660 host
->addr
+ HOST_CONTROL
);
663 for (cnt
= 0; cnt
< 20; ++cnt
) {
664 if (!(HOST_CONTROL_RESET
665 & readl(host
->addr
+ HOST_CONTROL
)))
670 dev_dbg(&host
->chip
->pdev
->dev
, "reset timeout\n");
675 writel(INT_STATUS_ALL
, host
->addr
+ INT_SIGNAL_ENABLE
);
676 writel(INT_STATUS_ALL
, host
->addr
+ INT_STATUS_ENABLE
);
680 static int jmb38x_ms_set_param(struct memstick_host
*msh
,
681 enum memstick_param param
,
684 struct jmb38x_ms_host
*host
= memstick_priv(msh
);
685 unsigned int host_ctl
= readl(host
->addr
+ HOST_CONTROL
);
686 unsigned int clock_ctl
= CLOCK_CONTROL_BY_MMIO
, clock_delay
= 0;
691 if (value
== MEMSTICK_POWER_ON
) {
692 rc
= jmb38x_ms_reset(host
);
697 host_ctl
|= HOST_CONTROL_POWER_EN
698 | HOST_CONTROL_CLOCK_EN
;
699 writel(host_ctl
, host
->addr
+ HOST_CONTROL
);
701 writel(host
->id
? PAD_PU_PD_ON_MS_SOCK1
702 : PAD_PU_PD_ON_MS_SOCK0
,
703 host
->addr
+ PAD_PU_PD
);
705 writel(PAD_OUTPUT_ENABLE_MS
,
706 host
->addr
+ PAD_OUTPUT_ENABLE
);
709 dev_dbg(&host
->chip
->pdev
->dev
, "power on\n");
710 } else if (value
== MEMSTICK_POWER_OFF
) {
711 host_ctl
&= ~(HOST_CONTROL_POWER_EN
712 | HOST_CONTROL_CLOCK_EN
);
713 writel(host_ctl
, host
->addr
+ HOST_CONTROL
);
714 writel(0, host
->addr
+ PAD_OUTPUT_ENABLE
);
715 writel(PAD_PU_PD_OFF
, host
->addr
+ PAD_PU_PD
);
716 dev_dbg(&host
->chip
->pdev
->dev
, "power off\n");
720 case MEMSTICK_INTERFACE
:
721 dev_dbg(&host
->chip
->pdev
->dev
,
722 "Set Host Interface Mode to %d\n", value
);
723 host_ctl
&= ~(HOST_CONTROL_FAST_CLK
| HOST_CONTROL_REI
|
725 host_ctl
|= HOST_CONTROL_TDELAY_EN
| HOST_CONTROL_HW_OC_P
;
726 host_ctl
&= ~(3 << HOST_CONTROL_IF_SHIFT
);
728 if (value
== MEMSTICK_SERIAL
) {
729 host_ctl
|= HOST_CONTROL_IF_SERIAL
730 << HOST_CONTROL_IF_SHIFT
;
731 host_ctl
|= HOST_CONTROL_REI
;
732 clock_ctl
|= CLOCK_CONTROL_40MHZ
;
734 } else if (value
== MEMSTICK_PAR4
) {
735 host_ctl
|= HOST_CONTROL_FAST_CLK
;
736 host_ctl
|= HOST_CONTROL_IF_PAR4
737 << HOST_CONTROL_IF_SHIFT
;
738 host_ctl
|= HOST_CONTROL_REO
;
739 clock_ctl
|= CLOCK_CONTROL_40MHZ
;
741 } else if (value
== MEMSTICK_PAR8
) {
742 host_ctl
|= HOST_CONTROL_FAST_CLK
;
743 host_ctl
|= HOST_CONTROL_IF_PAR8
744 << HOST_CONTROL_IF_SHIFT
;
745 clock_ctl
|= CLOCK_CONTROL_50MHZ
;
750 writel(host_ctl
, host
->addr
+ HOST_CONTROL
);
751 writel(CLOCK_CONTROL_OFF
, host
->addr
+ CLOCK_CONTROL
);
752 writel(clock_ctl
, host
->addr
+ CLOCK_CONTROL
);
753 pci_write_config_byte(host
->chip
->pdev
,
754 PCI_CTL_CLOCK_DLY_ADDR
+ 1,
756 host
->ifmode
= value
;
762 #define PCI_PMOS0_CONTROL 0xae
763 #define PMOS0_ENABLE 0x01
764 #define PMOS0_OVERCURRENT_LEVEL_2_4V 0x06
765 #define PMOS0_EN_OVERCURRENT_DEBOUNCE 0x40
766 #define PMOS0_SW_LED_POLARITY_ENABLE 0x80
767 #define PMOS0_ACTIVE_BITS (PMOS0_ENABLE | PMOS0_EN_OVERCURRENT_DEBOUNCE | \
768 PMOS0_OVERCURRENT_LEVEL_2_4V)
769 #define PCI_PMOS1_CONTROL 0xbd
770 #define PMOS1_ACTIVE_BITS 0x4a
771 #define PCI_CLOCK_CTL 0xb9
773 static int jmb38x_ms_pmos(struct pci_dev
*pdev
, int flag
)
777 pci_read_config_byte(pdev
, PCI_PMOS0_CONTROL
, &val
);
779 val
|= PMOS0_ACTIVE_BITS
;
781 val
&= ~PMOS0_ACTIVE_BITS
;
782 pci_write_config_byte(pdev
, PCI_PMOS0_CONTROL
, val
);
783 dev_dbg(&pdev
->dev
, "JMB38x: set PMOS0 val 0x%x\n", val
);
785 if (pci_resource_flags(pdev
, 1)) {
786 pci_read_config_byte(pdev
, PCI_PMOS1_CONTROL
, &val
);
788 val
|= PMOS1_ACTIVE_BITS
;
790 val
&= ~PMOS1_ACTIVE_BITS
;
791 pci_write_config_byte(pdev
, PCI_PMOS1_CONTROL
, val
);
792 dev_dbg(&pdev
->dev
, "JMB38x: set PMOS1 val 0x%x\n", val
);
795 pci_read_config_byte(pdev
, PCI_CLOCK_CTL
, &val
);
796 pci_write_config_byte(pdev
, PCI_CLOCK_CTL
, val
& ~0x0f);
797 pci_write_config_byte(pdev
, PCI_CLOCK_CTL
, val
| 0x01);
798 dev_dbg(&pdev
->dev
, "Clock Control by PCI config is disabled!\n");
805 static int jmb38x_ms_suspend(struct pci_dev
*dev
, pm_message_t state
)
807 struct jmb38x_ms
*jm
= pci_get_drvdata(dev
);
810 for (cnt
= 0; cnt
< jm
->host_cnt
; ++cnt
) {
813 memstick_suspend_host(jm
->hosts
[cnt
]);
817 pci_enable_wake(dev
, pci_choose_state(dev
, state
), 0);
818 pci_disable_device(dev
);
819 pci_set_power_state(dev
, pci_choose_state(dev
, state
));
823 static int jmb38x_ms_resume(struct pci_dev
*dev
)
825 struct jmb38x_ms
*jm
= pci_get_drvdata(dev
);
828 pci_set_power_state(dev
, PCI_D0
);
829 pci_restore_state(dev
);
830 rc
= pci_enable_device(dev
);
835 jmb38x_ms_pmos(dev
, 1);
837 for (rc
= 0; rc
< jm
->host_cnt
; ++rc
) {
840 memstick_resume_host(jm
->hosts
[rc
]);
841 memstick_detect_change(jm
->hosts
[rc
]);
849 #define jmb38x_ms_suspend NULL
850 #define jmb38x_ms_resume NULL
852 #endif /* CONFIG_PM */
854 static int jmb38x_ms_count_slots(struct pci_dev
*pdev
)
858 for (cnt
= 0; cnt
< PCI_ROM_RESOURCE
; ++cnt
) {
859 if (!(IORESOURCE_MEM
& pci_resource_flags(pdev
, cnt
)))
862 if (256 != pci_resource_len(pdev
, cnt
))
870 static struct memstick_host
*jmb38x_ms_alloc_host(struct jmb38x_ms
*jm
, int cnt
)
872 struct memstick_host
*msh
;
873 struct jmb38x_ms_host
*host
;
875 msh
= memstick_alloc_host(sizeof(struct jmb38x_ms_host
),
880 host
= memstick_priv(msh
);
882 host
->addr
= ioremap(pci_resource_start(jm
->pdev
, cnt
),
883 pci_resource_len(jm
->pdev
, cnt
));
887 spin_lock_init(&host
->lock
);
889 snprintf(host
->host_id
, sizeof(host
->host_id
), DRIVER_NAME
":slot%d",
891 host
->irq
= jm
->pdev
->irq
;
892 host
->timeout_jiffies
= msecs_to_jiffies(1000);
894 tasklet_init(&host
->notify
, jmb38x_ms_req_tasklet
, (unsigned long)msh
);
895 msh
->request
= jmb38x_ms_submit_req
;
896 msh
->set_param
= jmb38x_ms_set_param
;
898 msh
->caps
= MEMSTICK_CAP_PAR4
| MEMSTICK_CAP_PAR8
;
900 setup_timer(&host
->timer
, jmb38x_ms_abort
, (unsigned long)msh
);
902 if (!request_irq(host
->irq
, jmb38x_ms_isr
, IRQF_SHARED
, host
->host_id
,
912 static void jmb38x_ms_free_host(struct memstick_host
*msh
)
914 struct jmb38x_ms_host
*host
= memstick_priv(msh
);
916 free_irq(host
->irq
, msh
);
918 memstick_free_host(msh
);
921 static int jmb38x_ms_probe(struct pci_dev
*pdev
,
922 const struct pci_device_id
*dev_id
)
924 struct jmb38x_ms
*jm
;
925 int pci_dev_busy
= 0;
928 rc
= dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(32));
932 rc
= pci_enable_device(pdev
);
936 pci_set_master(pdev
);
938 rc
= pci_request_regions(pdev
, DRIVER_NAME
);
944 jmb38x_ms_pmos(pdev
, 1);
946 cnt
= jmb38x_ms_count_slots(pdev
);
953 jm
= kzalloc(sizeof(struct jmb38x_ms
)
954 + cnt
* sizeof(struct memstick_host
*), GFP_KERNEL
);
962 pci_set_drvdata(pdev
, jm
);
964 for (cnt
= 0; cnt
< jm
->host_cnt
; ++cnt
) {
965 jm
->hosts
[cnt
] = jmb38x_ms_alloc_host(jm
, cnt
);
969 rc
= memstick_add_host(jm
->hosts
[cnt
]);
972 jmb38x_ms_free_host(jm
->hosts
[cnt
]);
973 jm
->hosts
[cnt
] = NULL
;
983 pci_set_drvdata(pdev
, NULL
);
986 pci_release_regions(pdev
);
989 pci_disable_device(pdev
);
993 static void jmb38x_ms_remove(struct pci_dev
*dev
)
995 struct jmb38x_ms
*jm
= pci_get_drvdata(dev
);
996 struct jmb38x_ms_host
*host
;
1000 for (cnt
= 0; cnt
< jm
->host_cnt
; ++cnt
) {
1001 if (!jm
->hosts
[cnt
])
1004 host
= memstick_priv(jm
->hosts
[cnt
]);
1006 jm
->hosts
[cnt
]->request
= jmb38x_ms_dummy_submit
;
1007 tasklet_kill(&host
->notify
);
1008 writel(0, host
->addr
+ INT_SIGNAL_ENABLE
);
1009 writel(0, host
->addr
+ INT_STATUS_ENABLE
);
1011 dev_dbg(&jm
->pdev
->dev
, "interrupts off\n");
1012 spin_lock_irqsave(&host
->lock
, flags
);
1014 host
->req
->error
= -ETIME
;
1015 jmb38x_ms_complete_cmd(jm
->hosts
[cnt
], 1);
1017 spin_unlock_irqrestore(&host
->lock
, flags
);
1019 memstick_remove_host(jm
->hosts
[cnt
]);
1020 dev_dbg(&jm
->pdev
->dev
, "host removed\n");
1022 jmb38x_ms_free_host(jm
->hosts
[cnt
]);
1025 jmb38x_ms_pmos(dev
, 0);
1027 pci_set_drvdata(dev
, NULL
);
1028 pci_release_regions(dev
);
1029 pci_disable_device(dev
);
1033 static struct pci_device_id jmb38x_ms_id_tbl
[] = {
1034 { PCI_VDEVICE(JMICRON
, PCI_DEVICE_ID_JMICRON_JMB38X_MS
) },
1035 { PCI_VDEVICE(JMICRON
, PCI_DEVICE_ID_JMICRON_JMB385_MS
) },
1036 { PCI_VDEVICE(JMICRON
, PCI_DEVICE_ID_JMICRON_JMB390_MS
) },
1040 static struct pci_driver jmb38x_ms_driver
= {
1041 .name
= DRIVER_NAME
,
1042 .id_table
= jmb38x_ms_id_tbl
,
1043 .probe
= jmb38x_ms_probe
,
1044 .remove
= jmb38x_ms_remove
,
1045 .suspend
= jmb38x_ms_suspend
,
1046 .resume
= jmb38x_ms_resume
1049 module_pci_driver(jmb38x_ms_driver
);
1051 MODULE_AUTHOR("Alex Dubov");
1052 MODULE_DESCRIPTION("JMicron jmb38x MemoryStick driver");
1053 MODULE_LICENSE("GPL");
1054 MODULE_DEVICE_TABLE(pci
, jmb38x_ms_id_tbl
);