]> err.no Git - linux-2.6/blob - drivers/memstick/host/jmb38x_ms.c
memstick: automatically retrieve "INT" value from command response
[linux-2.6] / drivers / memstick / host / jmb38x_ms.c
1 /*
2  *  jmb38x_ms.c - JMicron jmb38x MemoryStick card reader
3  *
4  *  Copyright (C) 2008 Alex Dubov <oakad@yahoo.com>
5  *
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.
9  *
10  */
11
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
20 #define DRIVER_NAME "jmb38x_ms"
21
22 static int no_dma;
23 module_param(no_dma, bool, 0644);
24
25 enum {
26         DMA_ADDRESS       = 0x00,
27         BLOCK             = 0x04,
28         DMA_CONTROL       = 0x08,
29         TPC_P0            = 0x0c,
30         TPC_P1            = 0x10,
31         TPC               = 0x14,
32         HOST_CONTROL      = 0x18,
33         DATA              = 0x1c,
34         STATUS            = 0x20,
35         INT_STATUS        = 0x24,
36         INT_STATUS_ENABLE = 0x28,
37         INT_SIGNAL_ENABLE = 0x2c,
38         TIMER             = 0x30,
39         TIMER_CONTROL     = 0x34,
40         PAD_OUTPUT_ENABLE = 0x38,
41         PAD_PU_PD         = 0x3c,
42         CLOCK_DELAY       = 0x40,
43         ADMA_ADDRESS      = 0x44,
44         CLOCK_CONTROL     = 0x48,
45         LED_CONTROL       = 0x4c,
46         VERSION           = 0x50
47 };
48
49 struct jmb38x_ms_host {
50         struct jmb38x_ms        *chip;
51         void __iomem            *addr;
52         spinlock_t              lock;
53         int                     id;
54         char                    host_id[DEVICE_ID_SIZE];
55         int                     irq;
56         unsigned int            block_pos;
57         unsigned long           timeout_jiffies;
58         struct timer_list       timer;
59         struct memstick_request *req;
60         unsigned char           cmd_flags;
61         unsigned char           io_pos;
62         unsigned int            io_word[2];
63 };
64
65 struct jmb38x_ms {
66         struct pci_dev        *pdev;
67         int                   host_cnt;
68         struct memstick_host  *hosts[];
69 };
70
71 #define BLOCK_COUNT_MASK       0xffff0000
72 #define BLOCK_SIZE_MASK        0x00000fff
73
74 #define DMA_CONTROL_ENABLE     0x00000001
75
76 #define TPC_DATA_SEL           0x00008000
77 #define TPC_DIR                0x00004000
78 #define TPC_WAIT_INT           0x00002000
79 #define TPC_GET_INT            0x00000800
80 #define TPC_CODE_SZ_MASK       0x00000700
81 #define TPC_DATA_SZ_MASK       0x00000007
82
83 #define HOST_CONTROL_RESET_REQ 0x00008000
84 #define HOST_CONTROL_REI       0x00004000
85 #define HOST_CONTROL_LED       0x00000400
86 #define HOST_CONTROL_FAST_CLK  0x00000200
87 #define HOST_CONTROL_RESET     0x00000100
88 #define HOST_CONTROL_POWER_EN  0x00000080
89 #define HOST_CONTROL_CLOCK_EN  0x00000040
90 #define HOST_CONTROL_IF_SHIFT  4
91
92 #define HOST_CONTROL_IF_SERIAL 0x0
93 #define HOST_CONTROL_IF_PAR4   0x1
94 #define HOST_CONTROL_IF_PAR8   0x3
95
96 #define STATUS_BUSY             0x00080000
97 #define STATUS_MS_DAT7          0x00040000
98 #define STATUS_MS_DAT6          0x00020000
99 #define STATUS_MS_DAT5          0x00010000
100 #define STATUS_MS_DAT4          0x00008000
101 #define STATUS_MS_DAT3          0x00004000
102 #define STATUS_MS_DAT2          0x00002000
103 #define STATUS_MS_DAT1          0x00001000
104 #define STATUS_MS_DAT0          0x00000800
105 #define STATUS_HAS_MEDIA        0x00000400
106 #define STATUS_FIFO_EMPTY       0x00000200
107 #define STATUS_FIFO_FULL        0x00000100
108 #define STATUS_MS_CED           0x00000080
109 #define STATUS_MS_ERR           0x00000040
110 #define STATUS_MS_BRQ           0x00000020
111 #define STATUS_MS_CNK           0x00000001
112
113 #define INT_STATUS_TPC_ERR      0x00080000
114 #define INT_STATUS_CRC_ERR      0x00040000
115 #define INT_STATUS_TIMER_TO     0x00020000
116 #define INT_STATUS_HSK_TO       0x00010000
117 #define INT_STATUS_ANY_ERR      0x00008000
118 #define INT_STATUS_FIFO_WRDY    0x00000080
119 #define INT_STATUS_FIFO_RRDY    0x00000040
120 #define INT_STATUS_MEDIA_OUT    0x00000010
121 #define INT_STATUS_MEDIA_IN     0x00000008
122 #define INT_STATUS_DMA_BOUNDARY 0x00000004
123 #define INT_STATUS_EOTRAN       0x00000002
124 #define INT_STATUS_EOTPC        0x00000001
125
126 #define INT_STATUS_ALL          0x000f801f
127
128 #define PAD_OUTPUT_ENABLE_MS  0x0F3F
129
130 #define PAD_PU_PD_OFF         0x7FFF0000
131 #define PAD_PU_PD_ON_MS_SOCK0 0x5f8f0000
132 #define PAD_PU_PD_ON_MS_SOCK1 0x0f0f0000
133
134 enum {
135         CMD_READY    = 0x01,
136         FIFO_READY   = 0x02,
137         REG_DATA     = 0x04,
138         DMA_DATA     = 0x08
139 };
140
141 static unsigned int jmb38x_ms_read_data(struct jmb38x_ms_host *host,
142                                         unsigned char *buf, unsigned int length)
143 {
144         unsigned int off = 0;
145
146         while (host->io_pos && length) {
147                 buf[off++] = host->io_word[0] & 0xff;
148                 host->io_word[0] >>= 8;
149                 length--;
150                 host->io_pos--;
151         }
152
153         if (!length)
154                 return off;
155
156         while (!(STATUS_FIFO_EMPTY & readl(host->addr + STATUS))) {
157                 if (length < 4)
158                         break;
159                 *(unsigned int *)(buf + off) = __raw_readl(host->addr + DATA);
160                 length -= 4;
161                 off += 4;
162         }
163
164         if (length
165             && !(STATUS_FIFO_EMPTY & readl(host->addr + STATUS))) {
166                 host->io_word[0] = readl(host->addr + DATA);
167                 for (host->io_pos = 4; host->io_pos; --host->io_pos) {
168                         buf[off++] = host->io_word[0] & 0xff;
169                         host->io_word[0] >>= 8;
170                         length--;
171                         if (!length)
172                                 break;
173                 }
174         }
175
176         return off;
177 }
178
179 static unsigned int jmb38x_ms_read_reg_data(struct jmb38x_ms_host *host,
180                                             unsigned char *buf,
181                                             unsigned int length)
182 {
183         unsigned int off = 0;
184
185         while (host->io_pos > 4 && length) {
186                 buf[off++] = host->io_word[0] & 0xff;
187                 host->io_word[0] >>= 8;
188                 length--;
189                 host->io_pos--;
190         }
191
192         if (!length)
193                 return off;
194
195         while (host->io_pos && length) {
196                 buf[off++] = host->io_word[1] & 0xff;
197                 host->io_word[1] >>= 8;
198                 length--;
199                 host->io_pos--;
200         }
201
202         return off;
203 }
204
205 static unsigned int jmb38x_ms_write_data(struct jmb38x_ms_host *host,
206                                          unsigned char *buf,
207                                          unsigned int length)
208 {
209         unsigned int off = 0;
210
211         if (host->io_pos) {
212                 while (host->io_pos < 4 && length) {
213                         host->io_word[0] |=  buf[off++] << (host->io_pos * 8);
214                         host->io_pos++;
215                         length--;
216                 }
217         }
218
219         if (host->io_pos == 4
220             && !(STATUS_FIFO_FULL & readl(host->addr + STATUS))) {
221                 writel(host->io_word[0], host->addr + DATA);
222                 host->io_pos = 0;
223                 host->io_word[0] = 0;
224         } else if (host->io_pos) {
225                 return off;
226         }
227
228         if (!length)
229                 return off;
230
231         while (!(STATUS_FIFO_FULL & readl(host->addr + STATUS))) {
232                 if (length < 4)
233                         break;
234
235                 __raw_writel(*(unsigned int *)(buf + off),
236                              host->addr + DATA);
237                 length -= 4;
238                 off += 4;
239         }
240
241         switch (length) {
242         case 3:
243                 host->io_word[0] |= buf[off + 2] << 16;
244                 host->io_pos++;
245         case 2:
246                 host->io_word[0] |= buf[off + 1] << 8;
247                 host->io_pos++;
248         case 1:
249                 host->io_word[0] |= buf[off];
250                 host->io_pos++;
251         }
252
253         off += host->io_pos;
254
255         return off;
256 }
257
258 static unsigned int jmb38x_ms_write_reg_data(struct jmb38x_ms_host *host,
259                                              unsigned char *buf,
260                                              unsigned int length)
261 {
262         unsigned int off = 0;
263
264         while (host->io_pos < 4 && length) {
265                 host->io_word[0] &= ~(0xff << (host->io_pos * 8));
266                 host->io_word[0] |=  buf[off++] << (host->io_pos * 8);
267                 host->io_pos++;
268                 length--;
269         }
270
271         if (!length)
272                 return off;
273
274         while (host->io_pos < 8 && length) {
275                 host->io_word[1] &= ~(0xff << (host->io_pos * 8));
276                 host->io_word[1] |=  buf[off++] << (host->io_pos * 8);
277                 host->io_pos++;
278                 length--;
279         }
280
281         return off;
282 }
283
284 static int jmb38x_ms_transfer_data(struct jmb38x_ms_host *host)
285 {
286         unsigned int length;
287         unsigned int off;
288         unsigned int t_size, p_cnt;
289         unsigned char *buf;
290         struct page *pg;
291         unsigned long flags = 0;
292
293         if (host->req->long_data) {
294                 length = host->req->sg.length - host->block_pos;
295                 off = host->req->sg.offset + host->block_pos;
296         } else {
297                 length = host->req->data_len - host->block_pos;
298                 off = 0;
299         }
300
301         while (length) {
302                 unsigned int uninitialized_var(p_off);
303
304                 if (host->req->long_data) {
305                         pg = nth_page(sg_page(&host->req->sg),
306                                       off >> PAGE_SHIFT);
307                         p_off = offset_in_page(off);
308                         p_cnt = PAGE_SIZE - p_off;
309                         p_cnt = min(p_cnt, length);
310
311                         local_irq_save(flags);
312                         buf = kmap_atomic(pg, KM_BIO_SRC_IRQ) + p_off;
313                 } else {
314                         buf = host->req->data + host->block_pos;
315                         p_cnt = host->req->data_len - host->block_pos;
316                 }
317
318                 if (host->req->data_dir == WRITE)
319                         t_size = !(host->cmd_flags & REG_DATA)
320                                  ? jmb38x_ms_write_data(host, buf, p_cnt)
321                                  : jmb38x_ms_write_reg_data(host, buf, p_cnt);
322                 else
323                         t_size = !(host->cmd_flags & REG_DATA)
324                                  ? jmb38x_ms_read_data(host, buf, p_cnt)
325                                  : jmb38x_ms_read_reg_data(host, buf, p_cnt);
326
327                 if (host->req->long_data) {
328                         kunmap_atomic(buf - p_off, KM_BIO_SRC_IRQ);
329                         local_irq_restore(flags);
330                 }
331
332                 if (!t_size)
333                         break;
334                 host->block_pos += t_size;
335                 length -= t_size;
336                 off += t_size;
337         }
338
339         if (!length && host->req->data_dir == WRITE) {
340                 if (host->cmd_flags & REG_DATA) {
341                         writel(host->io_word[0], host->addr + TPC_P0);
342                         writel(host->io_word[1], host->addr + TPC_P1);
343                 } else if (host->io_pos) {
344                         writel(host->io_word[0], host->addr + DATA);
345                 }
346         }
347
348         return length;
349 }
350
351 static int jmb38x_ms_issue_cmd(struct memstick_host *msh)
352 {
353         struct jmb38x_ms_host *host = memstick_priv(msh);
354         unsigned char *data;
355         unsigned int data_len, cmd, t_val;
356
357         if (!(STATUS_HAS_MEDIA & readl(host->addr + STATUS))) {
358                 dev_dbg(msh->cdev.dev, "no media status\n");
359                 host->req->error = -ETIME;
360                 return host->req->error;
361         }
362
363         dev_dbg(msh->cdev.dev, "control %08x\n",
364                 readl(host->addr + HOST_CONTROL));
365         dev_dbg(msh->cdev.dev, "status %08x\n", readl(host->addr + INT_STATUS));
366         dev_dbg(msh->cdev.dev, "hstatus %08x\n", readl(host->addr + STATUS));
367
368         host->cmd_flags = 0;
369         host->block_pos = 0;
370         host->io_pos = 0;
371         host->io_word[0] = 0;
372         host->io_word[1] = 0;
373
374         cmd = host->req->tpc << 16;
375         cmd |= TPC_DATA_SEL;
376
377         if (host->req->data_dir == READ)
378                 cmd |= TPC_DIR;
379         if (host->req->need_card_int)
380                 cmd |= TPC_WAIT_INT;
381
382         data = host->req->data;
383
384         if (!no_dma)
385                 host->cmd_flags |= DMA_DATA;
386
387         if (host->req->long_data) {
388                 data_len = host->req->sg.length;
389         } else {
390                 data_len = host->req->data_len;
391                 host->cmd_flags &= ~DMA_DATA;
392         }
393
394         if (data_len <= 8) {
395                 cmd &= ~(TPC_DATA_SEL | 0xf);
396                 host->cmd_flags |= REG_DATA;
397                 cmd |= data_len & 0xf;
398                 host->cmd_flags &= ~DMA_DATA;
399         }
400
401         if (host->cmd_flags & DMA_DATA) {
402                 if (1 != pci_map_sg(host->chip->pdev, &host->req->sg, 1,
403                                     host->req->data_dir == READ
404                                     ? PCI_DMA_FROMDEVICE
405                                     : PCI_DMA_TODEVICE)) {
406                         host->req->error = -ENOMEM;
407                         return host->req->error;
408                 }
409                 data_len = sg_dma_len(&host->req->sg);
410                 writel(sg_dma_address(&host->req->sg),
411                        host->addr + DMA_ADDRESS);
412                 writel(((1 << 16) & BLOCK_COUNT_MASK)
413                        | (data_len & BLOCK_SIZE_MASK),
414                        host->addr + BLOCK);
415                 writel(DMA_CONTROL_ENABLE, host->addr + DMA_CONTROL);
416         } else if (!(host->cmd_flags & REG_DATA)) {
417                 writel(((1 << 16) & BLOCK_COUNT_MASK)
418                        | (data_len & BLOCK_SIZE_MASK),
419                        host->addr + BLOCK);
420                         t_val = readl(host->addr + INT_STATUS_ENABLE);
421                         t_val |= host->req->data_dir == READ
422                                  ? INT_STATUS_FIFO_RRDY
423                                  : INT_STATUS_FIFO_WRDY;
424
425                         writel(t_val, host->addr + INT_STATUS_ENABLE);
426                         writel(t_val, host->addr + INT_SIGNAL_ENABLE);
427         } else {
428                 cmd &= ~(TPC_DATA_SEL | 0xf);
429                 host->cmd_flags |= REG_DATA;
430                 cmd |= data_len & 0xf;
431
432                 if (host->req->data_dir == WRITE) {
433                         jmb38x_ms_transfer_data(host);
434                         writel(host->io_word[0], host->addr + TPC_P0);
435                         writel(host->io_word[1], host->addr + TPC_P1);
436                 }
437         }
438
439         mod_timer(&host->timer, jiffies + host->timeout_jiffies);
440         writel(HOST_CONTROL_LED | readl(host->addr + HOST_CONTROL),
441                host->addr + HOST_CONTROL);
442         host->req->error = 0;
443
444         writel(cmd, host->addr + TPC);
445         dev_dbg(msh->cdev.dev, "executing TPC %08x, len %x\n", cmd, data_len);
446
447         return 0;
448 }
449
450 static void jmb38x_ms_complete_cmd(struct memstick_host *msh, int last)
451 {
452         struct jmb38x_ms_host *host = memstick_priv(msh);
453         unsigned int t_val = 0;
454         int rc;
455
456         del_timer(&host->timer);
457
458         dev_dbg(msh->cdev.dev, "c control %08x\n",
459                 readl(host->addr + HOST_CONTROL));
460         dev_dbg(msh->cdev.dev, "c status %08x\n",
461                 readl(host->addr + INT_STATUS));
462         dev_dbg(msh->cdev.dev, "c hstatus %08x\n", readl(host->addr + STATUS));
463
464         host->req->int_reg = readl(host->addr + STATUS) & 0xff;
465
466         writel(0, host->addr + BLOCK);
467         writel(0, host->addr + DMA_CONTROL);
468
469         if (host->cmd_flags & DMA_DATA) {
470                 pci_unmap_sg(host->chip->pdev, &host->req->sg, 1,
471                              host->req->data_dir == READ
472                              ? PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
473         } else {
474                 t_val = readl(host->addr + INT_STATUS_ENABLE);
475                 if (host->req->data_dir == READ)
476                         t_val &= ~INT_STATUS_FIFO_RRDY;
477                 else
478                         t_val &= ~INT_STATUS_FIFO_WRDY;
479
480                 writel(t_val, host->addr + INT_STATUS_ENABLE);
481                 writel(t_val, host->addr + INT_SIGNAL_ENABLE);
482         }
483
484         writel((~HOST_CONTROL_LED) & readl(host->addr + HOST_CONTROL),
485                host->addr + HOST_CONTROL);
486
487         if (!last) {
488                 do {
489                         rc = memstick_next_req(msh, &host->req);
490                 } while (!rc && jmb38x_ms_issue_cmd(msh));
491         } else {
492                 do {
493                         rc = memstick_next_req(msh, &host->req);
494                         if (!rc)
495                                 host->req->error = -ETIME;
496                 } while (!rc);
497         }
498 }
499
500 static irqreturn_t jmb38x_ms_isr(int irq, void *dev_id)
501 {
502         struct memstick_host *msh = dev_id;
503         struct jmb38x_ms_host *host = memstick_priv(msh);
504         unsigned int irq_status;
505
506         spin_lock(&host->lock);
507         irq_status = readl(host->addr + INT_STATUS);
508         dev_dbg(&host->chip->pdev->dev, "irq_status = %08x\n", irq_status);
509         if (irq_status == 0 || irq_status == (~0)) {
510                 spin_unlock(&host->lock);
511                 return IRQ_NONE;
512         }
513
514         if (host->req) {
515                 if (irq_status & INT_STATUS_ANY_ERR) {
516                         if (irq_status & INT_STATUS_CRC_ERR)
517                                 host->req->error = -EILSEQ;
518                         else
519                                 host->req->error = -ETIME;
520                 } else {
521                         if (host->cmd_flags & DMA_DATA) {
522                                 if (irq_status & INT_STATUS_EOTRAN)
523                                         host->cmd_flags |= FIFO_READY;
524                         } else {
525                                 if (irq_status & (INT_STATUS_FIFO_RRDY
526                                                   | INT_STATUS_FIFO_WRDY))
527                                         jmb38x_ms_transfer_data(host);
528
529                                 if (irq_status & INT_STATUS_EOTRAN) {
530                                         jmb38x_ms_transfer_data(host);
531                                         host->cmd_flags |= FIFO_READY;
532                                 }
533                         }
534
535                         if (irq_status & INT_STATUS_EOTPC) {
536                                 host->cmd_flags |= CMD_READY;
537                                 if (host->cmd_flags & REG_DATA) {
538                                         if (host->req->data_dir == READ) {
539                                                 host->io_word[0]
540                                                         = readl(host->addr
541                                                                 + TPC_P0);
542                                                 host->io_word[1]
543                                                         = readl(host->addr
544                                                                 + TPC_P1);
545                                                 host->io_pos = 8;
546
547                                                 jmb38x_ms_transfer_data(host);
548                                         }
549                                         host->cmd_flags |= FIFO_READY;
550                                 }
551                         }
552                 }
553         }
554
555         if (irq_status & (INT_STATUS_MEDIA_IN | INT_STATUS_MEDIA_OUT)) {
556                 dev_dbg(&host->chip->pdev->dev, "media changed\n");
557                 memstick_detect_change(msh);
558         }
559
560         writel(irq_status, host->addr + INT_STATUS);
561
562         if (host->req
563             && (((host->cmd_flags & CMD_READY)
564                  && (host->cmd_flags & FIFO_READY))
565                 || host->req->error))
566                 jmb38x_ms_complete_cmd(msh, 0);
567
568         spin_unlock(&host->lock);
569         return IRQ_HANDLED;
570 }
571
572 static void jmb38x_ms_abort(unsigned long data)
573 {
574         struct memstick_host *msh = (struct memstick_host *)data;
575         struct jmb38x_ms_host *host = memstick_priv(msh);
576         unsigned long flags;
577
578         dev_dbg(&host->chip->pdev->dev, "abort\n");
579         spin_lock_irqsave(&host->lock, flags);
580         if (host->req) {
581                 host->req->error = -ETIME;
582                 jmb38x_ms_complete_cmd(msh, 0);
583         }
584         spin_unlock_irqrestore(&host->lock, flags);
585 }
586
587 static void jmb38x_ms_request(struct memstick_host *msh)
588 {
589         struct jmb38x_ms_host *host = memstick_priv(msh);
590         unsigned long flags;
591         int rc;
592
593         spin_lock_irqsave(&host->lock, flags);
594         if (host->req) {
595                 spin_unlock_irqrestore(&host->lock, flags);
596                 BUG();
597                 return;
598         }
599
600         do {
601                 rc = memstick_next_req(msh, &host->req);
602         } while (!rc && jmb38x_ms_issue_cmd(msh));
603         spin_unlock_irqrestore(&host->lock, flags);
604 }
605
606 static void jmb38x_ms_reset(struct jmb38x_ms_host *host)
607 {
608         unsigned int host_ctl = readl(host->addr + HOST_CONTROL);
609
610         writel(host_ctl | HOST_CONTROL_RESET_REQ | HOST_CONTROL_RESET,
611                host->addr + HOST_CONTROL);
612
613         while (HOST_CONTROL_RESET_REQ
614                & (host_ctl = readl(host->addr + HOST_CONTROL))) {
615                 ndelay(100);
616                 dev_dbg(&host->chip->pdev->dev, "reset\n");
617         }
618
619         writel(INT_STATUS_ALL, host->addr + INT_STATUS_ENABLE);
620         writel(INT_STATUS_ALL, host->addr + INT_SIGNAL_ENABLE);
621
622         dev_dbg(&host->chip->pdev->dev, "reset\n");
623 }
624
625 static void jmb38x_ms_set_param(struct memstick_host *msh,
626                                 enum memstick_param param,
627                                 int value)
628 {
629         struct jmb38x_ms_host *host = memstick_priv(msh);
630         unsigned int host_ctl;
631         unsigned long flags;
632
633         spin_lock_irqsave(&host->lock, flags);
634
635         switch (param) {
636         case MEMSTICK_POWER:
637                 if (value == MEMSTICK_POWER_ON) {
638                         jmb38x_ms_reset(host);
639
640                         writel(host->id ? PAD_PU_PD_ON_MS_SOCK1
641                                           : PAD_PU_PD_ON_MS_SOCK0,
642                                host->addr + PAD_PU_PD);
643
644                         writel(PAD_OUTPUT_ENABLE_MS,
645                                host->addr + PAD_OUTPUT_ENABLE);
646
647                         host_ctl = readl(host->addr + HOST_CONTROL);
648                         host_ctl |= 7;
649                         writel(host_ctl | (HOST_CONTROL_POWER_EN
650                                            | HOST_CONTROL_CLOCK_EN),
651                                host->addr + HOST_CONTROL);
652
653                         dev_dbg(&host->chip->pdev->dev, "power on\n");
654                 } else if (value == MEMSTICK_POWER_OFF) {
655                         writel(readl(host->addr + HOST_CONTROL)
656                                & ~(HOST_CONTROL_POWER_EN
657                                    | HOST_CONTROL_CLOCK_EN),
658                                host->addr +  HOST_CONTROL);
659                         writel(0, host->addr + PAD_OUTPUT_ENABLE);
660                         writel(PAD_PU_PD_OFF, host->addr + PAD_PU_PD);
661                         dev_dbg(&host->chip->pdev->dev, "power off\n");
662                 }
663                 break;
664         case MEMSTICK_INTERFACE:
665                 /* jmb38x_ms_reset(host); */
666
667                 host_ctl = readl(host->addr + HOST_CONTROL);
668                 host_ctl &= ~(3 << HOST_CONTROL_IF_SHIFT);
669                 /* host_ctl |= 7; */
670
671                 if (value == MEMSTICK_SERIAL) {
672                         host_ctl &= ~HOST_CONTROL_FAST_CLK;
673                         host_ctl |= HOST_CONTROL_IF_SERIAL
674                                     << HOST_CONTROL_IF_SHIFT;
675                         host_ctl |= HOST_CONTROL_REI;
676                         writel(0, host->addr + CLOCK_DELAY);
677                 } else if (value == MEMSTICK_PAR4) {
678                         host_ctl |= HOST_CONTROL_FAST_CLK;
679                         host_ctl |= HOST_CONTROL_IF_PAR4
680                                     << HOST_CONTROL_IF_SHIFT;
681                         host_ctl &= ~HOST_CONTROL_REI;
682                         writel(4, host->addr + CLOCK_DELAY);
683                 } else if (value == MEMSTICK_PAR8) {
684                         host_ctl |= HOST_CONTROL_FAST_CLK;
685                         host_ctl |= HOST_CONTROL_IF_PAR8
686                                     << HOST_CONTROL_IF_SHIFT;
687                         host_ctl &= ~HOST_CONTROL_REI;
688                         writel(4, host->addr + CLOCK_DELAY);
689                 }
690                 writel(host_ctl, host->addr + HOST_CONTROL);
691                 break;
692         };
693
694         spin_unlock_irqrestore(&host->lock, flags);
695 }
696
697 #ifdef CONFIG_PM
698
699 static int jmb38x_ms_suspend(struct pci_dev *dev, pm_message_t state)
700 {
701         struct jmb38x_ms *jm = pci_get_drvdata(dev);
702         int cnt;
703
704         for (cnt = 0; cnt < jm->host_cnt; ++cnt) {
705                 if (!jm->hosts[cnt])
706                         break;
707                 memstick_suspend_host(jm->hosts[cnt]);
708         }
709
710         pci_save_state(dev);
711         pci_enable_wake(dev, pci_choose_state(dev, state), 0);
712         pci_disable_device(dev);
713         pci_set_power_state(dev, pci_choose_state(dev, state));
714         return 0;
715 }
716
717 static int jmb38x_ms_resume(struct pci_dev *dev)
718 {
719         struct jmb38x_ms *jm = pci_get_drvdata(dev);
720         int rc;
721
722         pci_set_power_state(dev, PCI_D0);
723         pci_restore_state(dev);
724         rc = pci_enable_device(dev);
725         if (rc)
726                 return rc;
727         pci_set_master(dev);
728
729         pci_read_config_dword(dev, 0xac, &rc);
730         pci_write_config_dword(dev, 0xac, rc | 0x00470000);
731
732         for (rc = 0; rc < jm->host_cnt; ++rc) {
733                 if (!jm->hosts[rc])
734                         break;
735                 memstick_resume_host(jm->hosts[rc]);
736                 memstick_detect_change(jm->hosts[rc]);
737         }
738
739         return 0;
740 }
741
742 #else
743
744 #define jmb38x_ms_suspend NULL
745 #define jmb38x_ms_resume NULL
746
747 #endif /* CONFIG_PM */
748
749 static int jmb38x_ms_count_slots(struct pci_dev *pdev)
750 {
751         int cnt, rc = 0;
752
753         for (cnt = 0; cnt < PCI_ROM_RESOURCE; ++cnt) {
754                 if (!(IORESOURCE_MEM & pci_resource_flags(pdev, cnt)))
755                         break;
756
757                 if (256 != pci_resource_len(pdev, cnt))
758                         break;
759
760                 ++rc;
761         }
762         return rc;
763 }
764
765 static struct memstick_host *jmb38x_ms_alloc_host(struct jmb38x_ms *jm, int cnt)
766 {
767         struct memstick_host *msh;
768         struct jmb38x_ms_host *host;
769
770         msh = memstick_alloc_host(sizeof(struct jmb38x_ms_host),
771                                   &jm->pdev->dev);
772         if (!msh)
773                 return NULL;
774
775         host = memstick_priv(msh);
776         host->chip = jm;
777         host->addr = ioremap(pci_resource_start(jm->pdev, cnt),
778                              pci_resource_len(jm->pdev, cnt));
779         if (!host->addr)
780                 goto err_out_free;
781
782         spin_lock_init(&host->lock);
783         host->id = cnt;
784         snprintf(host->host_id, DEVICE_ID_SIZE, DRIVER_NAME ":slot%d",
785                  host->id);
786         host->irq = jm->pdev->irq;
787         host->timeout_jiffies = msecs_to_jiffies(1000);
788         msh->request = jmb38x_ms_request;
789         msh->set_param = jmb38x_ms_set_param;
790
791         msh->caps = MEMSTICK_CAP_PAR4 | MEMSTICK_CAP_PAR8;
792
793         setup_timer(&host->timer, jmb38x_ms_abort, (unsigned long)msh);
794
795         if (!request_irq(host->irq, jmb38x_ms_isr, IRQF_SHARED, host->host_id,
796                          msh))
797                 return msh;
798
799         iounmap(host->addr);
800 err_out_free:
801         kfree(msh);
802         return NULL;
803 }
804
805 static void jmb38x_ms_free_host(struct memstick_host *msh)
806 {
807         struct jmb38x_ms_host *host = memstick_priv(msh);
808
809         free_irq(host->irq, msh);
810         iounmap(host->addr);
811         memstick_free_host(msh);
812 }
813
814 static int jmb38x_ms_probe(struct pci_dev *pdev,
815                            const struct pci_device_id *dev_id)
816 {
817         struct jmb38x_ms *jm;
818         int pci_dev_busy = 0;
819         int rc, cnt;
820
821         rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
822         if (rc)
823                 return rc;
824
825         rc = pci_enable_device(pdev);
826         if (rc)
827                 return rc;
828
829         pci_set_master(pdev);
830
831         rc = pci_request_regions(pdev, DRIVER_NAME);
832         if (rc) {
833                 pci_dev_busy = 1;
834                 goto err_out;
835         }
836
837         pci_read_config_dword(pdev, 0xac, &rc);
838         pci_write_config_dword(pdev, 0xac, rc | 0x00470000);
839
840         cnt = jmb38x_ms_count_slots(pdev);
841         if (!cnt) {
842                 rc = -ENODEV;
843                 pci_dev_busy = 1;
844                 goto err_out;
845         }
846
847         jm = kzalloc(sizeof(struct jmb38x_ms)
848                      + cnt * sizeof(struct memstick_host *), GFP_KERNEL);
849         if (!jm) {
850                 rc = -ENOMEM;
851                 goto err_out_int;
852         }
853
854         jm->pdev = pdev;
855         jm->host_cnt = cnt;
856         pci_set_drvdata(pdev, jm);
857
858         for (cnt = 0; cnt < jm->host_cnt; ++cnt) {
859                 jm->hosts[cnt] = jmb38x_ms_alloc_host(jm, cnt);
860                 if (!jm->hosts[cnt])
861                         break;
862
863                 rc = memstick_add_host(jm->hosts[cnt]);
864
865                 if (rc) {
866                         jmb38x_ms_free_host(jm->hosts[cnt]);
867                         jm->hosts[cnt] = NULL;
868                         break;
869                 }
870         }
871
872         if (cnt)
873                 return 0;
874
875         rc = -ENODEV;
876
877         pci_set_drvdata(pdev, NULL);
878         kfree(jm);
879 err_out_int:
880         pci_release_regions(pdev);
881 err_out:
882         if (!pci_dev_busy)
883                 pci_disable_device(pdev);
884         return rc;
885 }
886
887 static void jmb38x_ms_remove(struct pci_dev *dev)
888 {
889         struct jmb38x_ms *jm = pci_get_drvdata(dev);
890         struct jmb38x_ms_host *host;
891         int cnt;
892         unsigned long flags;
893
894         for (cnt = 0; cnt < jm->host_cnt; ++cnt) {
895                 if (!jm->hosts[cnt])
896                         break;
897
898                 host = memstick_priv(jm->hosts[cnt]);
899
900                 writel(0, host->addr + INT_SIGNAL_ENABLE);
901                 writel(0, host->addr + INT_STATUS_ENABLE);
902                 mmiowb();
903                 dev_dbg(&jm->pdev->dev, "interrupts off\n");
904                 spin_lock_irqsave(&host->lock, flags);
905                 if (host->req) {
906                         host->req->error = -ETIME;
907                         jmb38x_ms_complete_cmd(jm->hosts[cnt], 1);
908                 }
909                 spin_unlock_irqrestore(&host->lock, flags);
910
911                 memstick_remove_host(jm->hosts[cnt]);
912                 dev_dbg(&jm->pdev->dev, "host removed\n");
913
914                 jmb38x_ms_free_host(jm->hosts[cnt]);
915         }
916
917         pci_set_drvdata(dev, NULL);
918         pci_release_regions(dev);
919         pci_disable_device(dev);
920         kfree(jm);
921 }
922
923 static struct pci_device_id jmb38x_ms_id_tbl [] = {
924         { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB38X_MS, PCI_ANY_ID,
925           PCI_ANY_ID, 0, 0, 0 },
926         { }
927 };
928
929 static struct pci_driver jmb38x_ms_driver = {
930         .name = DRIVER_NAME,
931         .id_table = jmb38x_ms_id_tbl,
932         .probe = jmb38x_ms_probe,
933         .remove = jmb38x_ms_remove,
934         .suspend = jmb38x_ms_suspend,
935         .resume = jmb38x_ms_resume
936 };
937
938 static int __init jmb38x_ms_init(void)
939 {
940         return pci_register_driver(&jmb38x_ms_driver);
941 }
942
943 static void __exit jmb38x_ms_exit(void)
944 {
945         pci_unregister_driver(&jmb38x_ms_driver);
946 }
947
948 MODULE_AUTHOR("Alex Dubov");
949 MODULE_DESCRIPTION("JMicron jmb38x MemoryStick driver");
950 MODULE_LICENSE("GPL");
951 MODULE_DEVICE_TABLE(pci, jmb38x_ms_id_tbl);
952
953 module_init(jmb38x_ms_init);
954 module_exit(jmb38x_ms_exit);