blob: 9d82e67737db5c06e23c3ec726bd71ca11653d22 [file] [log] [blame]
Alex Dubov60fdd932008-03-10 11:43:43 -07001/*
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>
Andrew Mortond3597ea2008-03-19 17:01:03 -070015#include <linux/dma-mapping.h>
Alex Dubov60fdd932008-03-10 11:43:43 -070016#include <linux/delay.h>
17#include <linux/highmem.h>
18#include <linux/memstick.h>
19
20#define DRIVER_NAME "jmb38x_ms"
21
22static int no_dma;
23module_param(no_dma, bool, 0644);
24
25enum {
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
49struct jmb38x_ms_host {
50 struct jmb38x_ms *chip;
51 void __iomem *addr;
52 spinlock_t lock;
53 int id;
Greg Kroah-Hartmanb98cb4b2008-05-02 06:02:41 +020054 char host_id[32];
Alex Dubov60fdd932008-03-10 11:43:43 -070055 int irq;
56 unsigned int block_pos;
57 unsigned long timeout_jiffies;
58 struct timer_list timer;
59 struct memstick_request *req;
Alex Dubov60fdd932008-03-10 11:43:43 -070060 unsigned char cmd_flags;
61 unsigned char io_pos;
62 unsigned int io_word[2];
63};
64
65struct 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
Alex Dubovead70772008-03-19 17:01:06 -070096#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
Alex Dubov60fdd932008-03-10 11:43:43 -0700105#define STATUS_HAS_MEDIA 0x00000400
106#define STATUS_FIFO_EMPTY 0x00000200
107#define STATUS_FIFO_FULL 0x00000100
Alex Dubovead70772008-03-19 17:01:06 -0700108#define STATUS_MS_CED 0x00000080
109#define STATUS_MS_ERR 0x00000040
110#define STATUS_MS_BRQ 0x00000020
111#define STATUS_MS_CNK 0x00000001
Alex Dubov60fdd932008-03-10 11:43:43 -0700112
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
Alex Dubovcf821e82008-03-19 17:01:08 -0700134#define CLOCK_CONTROL_40MHZ 0x00000001
135#define CLOCK_CONTROL_50MHZ 0x00000002
136#define CLOCK_CONTROL_60MHZ 0x00000008
137#define CLOCK_CONTROL_62_5MHZ 0x0000000c
138#define CLOCK_CONTROL_OFF 0x00000000
139
Alex Dubov60fdd932008-03-10 11:43:43 -0700140enum {
141 CMD_READY = 0x01,
142 FIFO_READY = 0x02,
143 REG_DATA = 0x04,
Alex Dubovead70772008-03-19 17:01:06 -0700144 DMA_DATA = 0x08
Alex Dubov60fdd932008-03-10 11:43:43 -0700145};
146
147static unsigned int jmb38x_ms_read_data(struct jmb38x_ms_host *host,
148 unsigned char *buf, unsigned int length)
149{
150 unsigned int off = 0;
151
152 while (host->io_pos && length) {
153 buf[off++] = host->io_word[0] & 0xff;
154 host->io_word[0] >>= 8;
155 length--;
156 host->io_pos--;
157 }
158
159 if (!length)
160 return off;
161
162 while (!(STATUS_FIFO_EMPTY & readl(host->addr + STATUS))) {
163 if (length < 4)
164 break;
165 *(unsigned int *)(buf + off) = __raw_readl(host->addr + DATA);
166 length -= 4;
167 off += 4;
168 }
169
170 if (length
171 && !(STATUS_FIFO_EMPTY & readl(host->addr + STATUS))) {
172 host->io_word[0] = readl(host->addr + DATA);
173 for (host->io_pos = 4; host->io_pos; --host->io_pos) {
174 buf[off++] = host->io_word[0] & 0xff;
175 host->io_word[0] >>= 8;
176 length--;
177 if (!length)
178 break;
179 }
180 }
181
182 return off;
183}
184
185static unsigned int jmb38x_ms_read_reg_data(struct jmb38x_ms_host *host,
186 unsigned char *buf,
187 unsigned int length)
188{
189 unsigned int off = 0;
190
191 while (host->io_pos > 4 && length) {
192 buf[off++] = host->io_word[0] & 0xff;
193 host->io_word[0] >>= 8;
194 length--;
195 host->io_pos--;
196 }
197
198 if (!length)
199 return off;
200
201 while (host->io_pos && length) {
202 buf[off++] = host->io_word[1] & 0xff;
203 host->io_word[1] >>= 8;
204 length--;
205 host->io_pos--;
206 }
207
208 return off;
209}
210
211static unsigned int jmb38x_ms_write_data(struct jmb38x_ms_host *host,
212 unsigned char *buf,
213 unsigned int length)
214{
215 unsigned int off = 0;
216
217 if (host->io_pos) {
218 while (host->io_pos < 4 && length) {
219 host->io_word[0] |= buf[off++] << (host->io_pos * 8);
220 host->io_pos++;
221 length--;
222 }
223 }
224
225 if (host->io_pos == 4
226 && !(STATUS_FIFO_FULL & readl(host->addr + STATUS))) {
227 writel(host->io_word[0], host->addr + DATA);
228 host->io_pos = 0;
229 host->io_word[0] = 0;
230 } else if (host->io_pos) {
231 return off;
232 }
233
234 if (!length)
235 return off;
236
237 while (!(STATUS_FIFO_FULL & readl(host->addr + STATUS))) {
238 if (length < 4)
239 break;
240
241 __raw_writel(*(unsigned int *)(buf + off),
242 host->addr + DATA);
243 length -= 4;
244 off += 4;
245 }
246
247 switch (length) {
248 case 3:
249 host->io_word[0] |= buf[off + 2] << 16;
250 host->io_pos++;
251 case 2:
252 host->io_word[0] |= buf[off + 1] << 8;
253 host->io_pos++;
254 case 1:
255 host->io_word[0] |= buf[off];
256 host->io_pos++;
257 }
258
259 off += host->io_pos;
260
261 return off;
262}
263
264static unsigned int jmb38x_ms_write_reg_data(struct jmb38x_ms_host *host,
265 unsigned char *buf,
266 unsigned int length)
267{
268 unsigned int off = 0;
269
270 while (host->io_pos < 4 && length) {
271 host->io_word[0] &= ~(0xff << (host->io_pos * 8));
272 host->io_word[0] |= buf[off++] << (host->io_pos * 8);
273 host->io_pos++;
274 length--;
275 }
276
277 if (!length)
278 return off;
279
280 while (host->io_pos < 8 && length) {
281 host->io_word[1] &= ~(0xff << (host->io_pos * 8));
282 host->io_word[1] |= buf[off++] << (host->io_pos * 8);
283 host->io_pos++;
284 length--;
285 }
286
287 return off;
288}
289
290static int jmb38x_ms_transfer_data(struct jmb38x_ms_host *host)
291{
292 unsigned int length;
293 unsigned int off;
Andrew Morton81950962008-03-19 17:01:04 -0700294 unsigned int t_size, p_cnt;
Alex Dubov60fdd932008-03-10 11:43:43 -0700295 unsigned char *buf;
296 struct page *pg;
297 unsigned long flags = 0;
298
299 if (host->req->long_data) {
300 length = host->req->sg.length - host->block_pos;
301 off = host->req->sg.offset + host->block_pos;
302 } else {
303 length = host->req->data_len - host->block_pos;
304 off = 0;
305 }
306
307 while (length) {
Andrew Morton81950962008-03-19 17:01:04 -0700308 unsigned int uninitialized_var(p_off);
309
Alex Dubov60fdd932008-03-10 11:43:43 -0700310 if (host->req->long_data) {
311 pg = nth_page(sg_page(&host->req->sg),
312 off >> PAGE_SHIFT);
313 p_off = offset_in_page(off);
314 p_cnt = PAGE_SIZE - p_off;
315 p_cnt = min(p_cnt, length);
316
317 local_irq_save(flags);
318 buf = kmap_atomic(pg, KM_BIO_SRC_IRQ) + p_off;
319 } else {
320 buf = host->req->data + host->block_pos;
321 p_cnt = host->req->data_len - host->block_pos;
322 }
323
324 if (host->req->data_dir == WRITE)
325 t_size = !(host->cmd_flags & REG_DATA)
326 ? jmb38x_ms_write_data(host, buf, p_cnt)
327 : jmb38x_ms_write_reg_data(host, buf, p_cnt);
328 else
329 t_size = !(host->cmd_flags & REG_DATA)
330 ? jmb38x_ms_read_data(host, buf, p_cnt)
331 : jmb38x_ms_read_reg_data(host, buf, p_cnt);
332
333 if (host->req->long_data) {
334 kunmap_atomic(buf - p_off, KM_BIO_SRC_IRQ);
335 local_irq_restore(flags);
336 }
337
338 if (!t_size)
339 break;
340 host->block_pos += t_size;
341 length -= t_size;
342 off += t_size;
343 }
344
345 if (!length && host->req->data_dir == WRITE) {
346 if (host->cmd_flags & REG_DATA) {
347 writel(host->io_word[0], host->addr + TPC_P0);
348 writel(host->io_word[1], host->addr + TPC_P1);
349 } else if (host->io_pos) {
350 writel(host->io_word[0], host->addr + DATA);
351 }
352 }
353
354 return length;
355}
356
357static int jmb38x_ms_issue_cmd(struct memstick_host *msh)
358{
359 struct jmb38x_ms_host *host = memstick_priv(msh);
360 unsigned char *data;
361 unsigned int data_len, cmd, t_val;
362
363 if (!(STATUS_HAS_MEDIA & readl(host->addr + STATUS))) {
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100364 dev_dbg(&msh->dev, "no media status\n");
Alex Dubov60fdd932008-03-10 11:43:43 -0700365 host->req->error = -ETIME;
366 return host->req->error;
367 }
368
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100369 dev_dbg(&msh->dev, "control %08x\n",
Alex Dubov60fdd932008-03-10 11:43:43 -0700370 readl(host->addr + HOST_CONTROL));
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100371 dev_dbg(&msh->dev, "status %08x\n", readl(host->addr + INT_STATUS));
372 dev_dbg(&msh->dev, "hstatus %08x\n", readl(host->addr + STATUS));
Alex Dubov60fdd932008-03-10 11:43:43 -0700373
374 host->cmd_flags = 0;
375 host->block_pos = 0;
376 host->io_pos = 0;
377 host->io_word[0] = 0;
378 host->io_word[1] = 0;
379
380 cmd = host->req->tpc << 16;
381 cmd |= TPC_DATA_SEL;
382
383 if (host->req->data_dir == READ)
384 cmd |= TPC_DIR;
385 if (host->req->need_card_int)
386 cmd |= TPC_WAIT_INT;
Alex Dubov60fdd932008-03-10 11:43:43 -0700387
388 data = host->req->data;
389
Alex Dubovead70772008-03-19 17:01:06 -0700390 if (!no_dma)
391 host->cmd_flags |= DMA_DATA;
Alex Dubov60fdd932008-03-10 11:43:43 -0700392
393 if (host->req->long_data) {
394 data_len = host->req->sg.length;
395 } else {
396 data_len = host->req->data_len;
Alex Dubovead70772008-03-19 17:01:06 -0700397 host->cmd_flags &= ~DMA_DATA;
Alex Dubov60fdd932008-03-10 11:43:43 -0700398 }
399
400 if (data_len <= 8) {
401 cmd &= ~(TPC_DATA_SEL | 0xf);
402 host->cmd_flags |= REG_DATA;
403 cmd |= data_len & 0xf;
Alex Dubovead70772008-03-19 17:01:06 -0700404 host->cmd_flags &= ~DMA_DATA;
Alex Dubov60fdd932008-03-10 11:43:43 -0700405 }
406
Alex Dubovead70772008-03-19 17:01:06 -0700407 if (host->cmd_flags & DMA_DATA) {
Alex Dubov60fdd932008-03-10 11:43:43 -0700408 if (1 != pci_map_sg(host->chip->pdev, &host->req->sg, 1,
409 host->req->data_dir == READ
410 ? PCI_DMA_FROMDEVICE
411 : PCI_DMA_TODEVICE)) {
412 host->req->error = -ENOMEM;
413 return host->req->error;
414 }
415 data_len = sg_dma_len(&host->req->sg);
416 writel(sg_dma_address(&host->req->sg),
417 host->addr + DMA_ADDRESS);
418 writel(((1 << 16) & BLOCK_COUNT_MASK)
419 | (data_len & BLOCK_SIZE_MASK),
420 host->addr + BLOCK);
421 writel(DMA_CONTROL_ENABLE, host->addr + DMA_CONTROL);
422 } else if (!(host->cmd_flags & REG_DATA)) {
423 writel(((1 << 16) & BLOCK_COUNT_MASK)
424 | (data_len & BLOCK_SIZE_MASK),
425 host->addr + BLOCK);
426 t_val = readl(host->addr + INT_STATUS_ENABLE);
427 t_val |= host->req->data_dir == READ
428 ? INT_STATUS_FIFO_RRDY
429 : INT_STATUS_FIFO_WRDY;
430
431 writel(t_val, host->addr + INT_STATUS_ENABLE);
432 writel(t_val, host->addr + INT_SIGNAL_ENABLE);
433 } else {
434 cmd &= ~(TPC_DATA_SEL | 0xf);
435 host->cmd_flags |= REG_DATA;
436 cmd |= data_len & 0xf;
437
438 if (host->req->data_dir == WRITE) {
439 jmb38x_ms_transfer_data(host);
440 writel(host->io_word[0], host->addr + TPC_P0);
441 writel(host->io_word[1], host->addr + TPC_P1);
442 }
443 }
444
445 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
446 writel(HOST_CONTROL_LED | readl(host->addr + HOST_CONTROL),
447 host->addr + HOST_CONTROL);
448 host->req->error = 0;
449
450 writel(cmd, host->addr + TPC);
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100451 dev_dbg(&msh->dev, "executing TPC %08x, len %x\n", cmd, data_len);
Alex Dubov60fdd932008-03-10 11:43:43 -0700452
453 return 0;
454}
455
456static void jmb38x_ms_complete_cmd(struct memstick_host *msh, int last)
457{
458 struct jmb38x_ms_host *host = memstick_priv(msh);
459 unsigned int t_val = 0;
460 int rc;
461
462 del_timer(&host->timer);
463
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100464 dev_dbg(&msh->dev, "c control %08x\n",
Alex Dubov60fdd932008-03-10 11:43:43 -0700465 readl(host->addr + HOST_CONTROL));
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100466 dev_dbg(&msh->dev, "c status %08x\n",
Alex Dubov60fdd932008-03-10 11:43:43 -0700467 readl(host->addr + INT_STATUS));
Greg Kroah-Hartmanc4c66cf2008-03-04 00:13:36 +0100468 dev_dbg(&msh->dev, "c hstatus %08x\n", readl(host->addr + STATUS));
Alex Dubov60fdd932008-03-10 11:43:43 -0700469
Alex Dubovead70772008-03-19 17:01:06 -0700470 host->req->int_reg = readl(host->addr + STATUS) & 0xff;
Alex Dubov60fdd932008-03-10 11:43:43 -0700471
Alex Dubovead70772008-03-19 17:01:06 -0700472 writel(0, host->addr + BLOCK);
473 writel(0, host->addr + DMA_CONTROL);
474
475 if (host->cmd_flags & DMA_DATA) {
Alex Dubov60fdd932008-03-10 11:43:43 -0700476 pci_unmap_sg(host->chip->pdev, &host->req->sg, 1,
477 host->req->data_dir == READ
478 ? PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
479 } else {
480 t_val = readl(host->addr + INT_STATUS_ENABLE);
481 if (host->req->data_dir == READ)
482 t_val &= ~INT_STATUS_FIFO_RRDY;
483 else
484 t_val &= ~INT_STATUS_FIFO_WRDY;
485
486 writel(t_val, host->addr + INT_STATUS_ENABLE);
487 writel(t_val, host->addr + INT_SIGNAL_ENABLE);
488 }
489
490 writel((~HOST_CONTROL_LED) & readl(host->addr + HOST_CONTROL),
491 host->addr + HOST_CONTROL);
492
493 if (!last) {
494 do {
495 rc = memstick_next_req(msh, &host->req);
496 } while (!rc && jmb38x_ms_issue_cmd(msh));
497 } else {
498 do {
499 rc = memstick_next_req(msh, &host->req);
500 if (!rc)
501 host->req->error = -ETIME;
502 } while (!rc);
503 }
504}
505
506static irqreturn_t jmb38x_ms_isr(int irq, void *dev_id)
507{
508 struct memstick_host *msh = dev_id;
509 struct jmb38x_ms_host *host = memstick_priv(msh);
510 unsigned int irq_status;
511
512 spin_lock(&host->lock);
513 irq_status = readl(host->addr + INT_STATUS);
514 dev_dbg(&host->chip->pdev->dev, "irq_status = %08x\n", irq_status);
515 if (irq_status == 0 || irq_status == (~0)) {
516 spin_unlock(&host->lock);
517 return IRQ_NONE;
518 }
519
520 if (host->req) {
521 if (irq_status & INT_STATUS_ANY_ERR) {
522 if (irq_status & INT_STATUS_CRC_ERR)
523 host->req->error = -EILSEQ;
524 else
525 host->req->error = -ETIME;
526 } else {
Alex Dubovead70772008-03-19 17:01:06 -0700527 if (host->cmd_flags & DMA_DATA) {
Alex Dubov60fdd932008-03-10 11:43:43 -0700528 if (irq_status & INT_STATUS_EOTRAN)
529 host->cmd_flags |= FIFO_READY;
530 } else {
531 if (irq_status & (INT_STATUS_FIFO_RRDY
532 | INT_STATUS_FIFO_WRDY))
533 jmb38x_ms_transfer_data(host);
534
535 if (irq_status & INT_STATUS_EOTRAN) {
536 jmb38x_ms_transfer_data(host);
537 host->cmd_flags |= FIFO_READY;
538 }
539 }
540
541 if (irq_status & INT_STATUS_EOTPC) {
542 host->cmd_flags |= CMD_READY;
543 if (host->cmd_flags & REG_DATA) {
544 if (host->req->data_dir == READ) {
545 host->io_word[0]
546 = readl(host->addr
547 + TPC_P0);
548 host->io_word[1]
549 = readl(host->addr
550 + TPC_P1);
551 host->io_pos = 8;
552
553 jmb38x_ms_transfer_data(host);
554 }
555 host->cmd_flags |= FIFO_READY;
556 }
557 }
558 }
559 }
560
561 if (irq_status & (INT_STATUS_MEDIA_IN | INT_STATUS_MEDIA_OUT)) {
562 dev_dbg(&host->chip->pdev->dev, "media changed\n");
563 memstick_detect_change(msh);
564 }
565
566 writel(irq_status, host->addr + INT_STATUS);
567
568 if (host->req
569 && (((host->cmd_flags & CMD_READY)
570 && (host->cmd_flags & FIFO_READY))
571 || host->req->error))
572 jmb38x_ms_complete_cmd(msh, 0);
573
574 spin_unlock(&host->lock);
575 return IRQ_HANDLED;
576}
577
578static void jmb38x_ms_abort(unsigned long data)
579{
580 struct memstick_host *msh = (struct memstick_host *)data;
581 struct jmb38x_ms_host *host = memstick_priv(msh);
582 unsigned long flags;
583
584 dev_dbg(&host->chip->pdev->dev, "abort\n");
585 spin_lock_irqsave(&host->lock, flags);
586 if (host->req) {
587 host->req->error = -ETIME;
588 jmb38x_ms_complete_cmd(msh, 0);
589 }
590 spin_unlock_irqrestore(&host->lock, flags);
591}
592
593static void jmb38x_ms_request(struct memstick_host *msh)
594{
595 struct jmb38x_ms_host *host = memstick_priv(msh);
596 unsigned long flags;
597 int rc;
598
599 spin_lock_irqsave(&host->lock, flags);
600 if (host->req) {
601 spin_unlock_irqrestore(&host->lock, flags);
602 BUG();
603 return;
604 }
605
606 do {
607 rc = memstick_next_req(msh, &host->req);
608 } while (!rc && jmb38x_ms_issue_cmd(msh));
609 spin_unlock_irqrestore(&host->lock, flags);
610}
611
Alex Dubovb7789992008-07-25 19:45:00 -0700612static int jmb38x_ms_reset(struct jmb38x_ms_host *host)
Alex Dubov60fdd932008-03-10 11:43:43 -0700613{
Alex Dubovb7789992008-07-25 19:45:00 -0700614 int cnt;
Alex Dubov60fdd932008-03-10 11:43:43 -0700615
Alex Dubovb7789992008-07-25 19:45:00 -0700616 writel(HOST_CONTROL_RESET_REQ | HOST_CONTROL_CLOCK_EN
617 | readl(host->addr + HOST_CONTROL),
618 host->addr + HOST_CONTROL);
619 mmiowb();
Alex Dubov60fdd932008-03-10 11:43:43 -0700620
Alex Dubovb7789992008-07-25 19:45:00 -0700621 for (cnt = 0; cnt < 20; ++cnt) {
622 if (!(HOST_CONTROL_RESET_REQ
623 & readl(host->addr + HOST_CONTROL)))
624 goto reset_next;
625
Alex Dubovcf821e82008-03-19 17:01:08 -0700626 ndelay(20);
Alex Dubov60fdd932008-03-10 11:43:43 -0700627 }
Alex Dubovb7789992008-07-25 19:45:00 -0700628 dev_dbg(&host->chip->pdev->dev, "reset_req timeout\n");
629 return -EIO;
Alex Dubov60fdd932008-03-10 11:43:43 -0700630
Alex Dubovb7789992008-07-25 19:45:00 -0700631reset_next:
632 writel(HOST_CONTROL_RESET | HOST_CONTROL_CLOCK_EN
633 | readl(host->addr + HOST_CONTROL),
634 host->addr + HOST_CONTROL);
635 mmiowb();
636
637 for (cnt = 0; cnt < 20; ++cnt) {
638 if (!(HOST_CONTROL_RESET
639 & readl(host->addr + HOST_CONTROL)))
640 goto reset_ok;
641
642 ndelay(20);
643 }
644 dev_dbg(&host->chip->pdev->dev, "reset timeout\n");
645 return -EIO;
646
647reset_ok:
Alex Dubovcf821e82008-03-19 17:01:08 -0700648 mmiowb();
Alex Dubov60fdd932008-03-10 11:43:43 -0700649 writel(INT_STATUS_ALL, host->addr + INT_SIGNAL_ENABLE);
Alex Dubovcf821e82008-03-19 17:01:08 -0700650 writel(INT_STATUS_ALL, host->addr + INT_STATUS_ENABLE);
Alex Dubovb7789992008-07-25 19:45:00 -0700651 return 0;
Alex Dubov60fdd932008-03-10 11:43:43 -0700652}
653
Alex Dubovb7789992008-07-25 19:45:00 -0700654static int jmb38x_ms_set_param(struct memstick_host *msh,
655 enum memstick_param param,
656 int value)
Alex Dubov60fdd932008-03-10 11:43:43 -0700657{
658 struct jmb38x_ms_host *host = memstick_priv(msh);
Alex Dubovcf821e82008-03-19 17:01:08 -0700659 unsigned int host_ctl = readl(host->addr + HOST_CONTROL);
660 unsigned int clock_ctl = CLOCK_CONTROL_40MHZ, clock_delay = 0;
Alex Dubovb7789992008-07-25 19:45:00 -0700661 int rc = 0;
Alex Dubov60fdd932008-03-10 11:43:43 -0700662
663 switch (param) {
664 case MEMSTICK_POWER:
665 if (value == MEMSTICK_POWER_ON) {
Alex Dubovb7789992008-07-25 19:45:00 -0700666 rc = jmb38x_ms_reset(host);
667 if (rc)
668 return rc;
669
670 host_ctl = 7;
671 host_ctl |= HOST_CONTROL_POWER_EN
672 | HOST_CONTROL_CLOCK_EN;
673 writel(host_ctl, host->addr + HOST_CONTROL);
Alex Dubov60fdd932008-03-10 11:43:43 -0700674
675 writel(host->id ? PAD_PU_PD_ON_MS_SOCK1
Alex Dubovcf821e82008-03-19 17:01:08 -0700676 : PAD_PU_PD_ON_MS_SOCK0,
Alex Dubov60fdd932008-03-10 11:43:43 -0700677 host->addr + PAD_PU_PD);
678
679 writel(PAD_OUTPUT_ENABLE_MS,
680 host->addr + PAD_OUTPUT_ENABLE);
681
Alex Dubovb7789992008-07-25 19:45:00 -0700682 msleep(10);
Alex Dubov60fdd932008-03-10 11:43:43 -0700683 dev_dbg(&host->chip->pdev->dev, "power on\n");
684 } else if (value == MEMSTICK_POWER_OFF) {
Alex Dubovcf821e82008-03-19 17:01:08 -0700685 host_ctl &= ~(HOST_CONTROL_POWER_EN
686 | HOST_CONTROL_CLOCK_EN);
687 writel(host_ctl, host->addr + HOST_CONTROL);
Alex Dubov60fdd932008-03-10 11:43:43 -0700688 writel(0, host->addr + PAD_OUTPUT_ENABLE);
689 writel(PAD_PU_PD_OFF, host->addr + PAD_PU_PD);
690 dev_dbg(&host->chip->pdev->dev, "power off\n");
Alex Dubovb7789992008-07-25 19:45:00 -0700691 } else
692 return -EINVAL;
Alex Dubov60fdd932008-03-10 11:43:43 -0700693 break;
694 case MEMSTICK_INTERFACE:
Alex Dubov60fdd932008-03-10 11:43:43 -0700695 host_ctl &= ~(3 << HOST_CONTROL_IF_SHIFT);
Alex Dubov60fdd932008-03-10 11:43:43 -0700696
697 if (value == MEMSTICK_SERIAL) {
698 host_ctl &= ~HOST_CONTROL_FAST_CLK;
699 host_ctl |= HOST_CONTROL_IF_SERIAL
700 << HOST_CONTROL_IF_SHIFT;
701 host_ctl |= HOST_CONTROL_REI;
Alex Dubovcf821e82008-03-19 17:01:08 -0700702 clock_ctl = CLOCK_CONTROL_40MHZ;
703 clock_delay = 0;
Alex Dubov60fdd932008-03-10 11:43:43 -0700704 } else if (value == MEMSTICK_PAR4) {
705 host_ctl |= HOST_CONTROL_FAST_CLK;
706 host_ctl |= HOST_CONTROL_IF_PAR4
707 << HOST_CONTROL_IF_SHIFT;
708 host_ctl &= ~HOST_CONTROL_REI;
Alex Dubovcf821e82008-03-19 17:01:08 -0700709 clock_ctl = CLOCK_CONTROL_40MHZ;
710 clock_delay = 4;
Alex Dubov60fdd932008-03-10 11:43:43 -0700711 } else if (value == MEMSTICK_PAR8) {
712 host_ctl |= HOST_CONTROL_FAST_CLK;
713 host_ctl |= HOST_CONTROL_IF_PAR8
714 << HOST_CONTROL_IF_SHIFT;
715 host_ctl &= ~HOST_CONTROL_REI;
Alex Dubovcf821e82008-03-19 17:01:08 -0700716 clock_ctl = CLOCK_CONTROL_60MHZ;
717 clock_delay = 0;
Alex Dubovb7789992008-07-25 19:45:00 -0700718 } else
719 return -EINVAL;
Alex Dubov60fdd932008-03-10 11:43:43 -0700720 writel(host_ctl, host->addr + HOST_CONTROL);
Alex Dubovcf821e82008-03-19 17:01:08 -0700721 writel(clock_ctl, host->addr + CLOCK_CONTROL);
722 writel(clock_delay, host->addr + CLOCK_DELAY);
Alex Dubov60fdd932008-03-10 11:43:43 -0700723 break;
724 };
Alex Dubovb7789992008-07-25 19:45:00 -0700725 return 0;
Alex Dubov60fdd932008-03-10 11:43:43 -0700726}
727
728#ifdef CONFIG_PM
729
730static int jmb38x_ms_suspend(struct pci_dev *dev, pm_message_t state)
731{
732 struct jmb38x_ms *jm = pci_get_drvdata(dev);
733 int cnt;
734
735 for (cnt = 0; cnt < jm->host_cnt; ++cnt) {
736 if (!jm->hosts[cnt])
737 break;
738 memstick_suspend_host(jm->hosts[cnt]);
739 }
740
741 pci_save_state(dev);
742 pci_enable_wake(dev, pci_choose_state(dev, state), 0);
743 pci_disable_device(dev);
744 pci_set_power_state(dev, pci_choose_state(dev, state));
745 return 0;
746}
747
748static int jmb38x_ms_resume(struct pci_dev *dev)
749{
750 struct jmb38x_ms *jm = pci_get_drvdata(dev);
751 int rc;
752
753 pci_set_power_state(dev, PCI_D0);
754 pci_restore_state(dev);
755 rc = pci_enable_device(dev);
756 if (rc)
757 return rc;
758 pci_set_master(dev);
759
760 pci_read_config_dword(dev, 0xac, &rc);
761 pci_write_config_dword(dev, 0xac, rc | 0x00470000);
762
763 for (rc = 0; rc < jm->host_cnt; ++rc) {
764 if (!jm->hosts[rc])
765 break;
766 memstick_resume_host(jm->hosts[rc]);
767 memstick_detect_change(jm->hosts[rc]);
768 }
769
770 return 0;
771}
772
773#else
774
775#define jmb38x_ms_suspend NULL
776#define jmb38x_ms_resume NULL
777
778#endif /* CONFIG_PM */
779
780static int jmb38x_ms_count_slots(struct pci_dev *pdev)
781{
782 int cnt, rc = 0;
783
784 for (cnt = 0; cnt < PCI_ROM_RESOURCE; ++cnt) {
785 if (!(IORESOURCE_MEM & pci_resource_flags(pdev, cnt)))
786 break;
787
788 if (256 != pci_resource_len(pdev, cnt))
789 break;
790
791 ++rc;
792 }
793 return rc;
794}
795
796static struct memstick_host *jmb38x_ms_alloc_host(struct jmb38x_ms *jm, int cnt)
797{
798 struct memstick_host *msh;
799 struct jmb38x_ms_host *host;
800
801 msh = memstick_alloc_host(sizeof(struct jmb38x_ms_host),
802 &jm->pdev->dev);
803 if (!msh)
804 return NULL;
805
806 host = memstick_priv(msh);
807 host->chip = jm;
808 host->addr = ioremap(pci_resource_start(jm->pdev, cnt),
809 pci_resource_len(jm->pdev, cnt));
810 if (!host->addr)
811 goto err_out_free;
812
813 spin_lock_init(&host->lock);
814 host->id = cnt;
Greg Kroah-Hartmanb98cb4b2008-05-02 06:02:41 +0200815 snprintf(host->host_id, sizeof(host->host_id), DRIVER_NAME ":slot%d",
Alex Dubov60fdd932008-03-10 11:43:43 -0700816 host->id);
817 host->irq = jm->pdev->irq;
Alex Dubovead70772008-03-19 17:01:06 -0700818 host->timeout_jiffies = msecs_to_jiffies(1000);
Alex Dubov60fdd932008-03-10 11:43:43 -0700819 msh->request = jmb38x_ms_request;
820 msh->set_param = jmb38x_ms_set_param;
Alex Dubovead70772008-03-19 17:01:06 -0700821
Alex Dubov60fdd932008-03-10 11:43:43 -0700822 msh->caps = MEMSTICK_CAP_PAR4 | MEMSTICK_CAP_PAR8;
823
824 setup_timer(&host->timer, jmb38x_ms_abort, (unsigned long)msh);
825
826 if (!request_irq(host->irq, jmb38x_ms_isr, IRQF_SHARED, host->host_id,
827 msh))
828 return msh;
829
830 iounmap(host->addr);
831err_out_free:
832 kfree(msh);
833 return NULL;
834}
835
836static void jmb38x_ms_free_host(struct memstick_host *msh)
837{
838 struct jmb38x_ms_host *host = memstick_priv(msh);
839
840 free_irq(host->irq, msh);
841 iounmap(host->addr);
842 memstick_free_host(msh);
843}
844
845static int jmb38x_ms_probe(struct pci_dev *pdev,
846 const struct pci_device_id *dev_id)
847{
848 struct jmb38x_ms *jm;
849 int pci_dev_busy = 0;
850 int rc, cnt;
851
852 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
853 if (rc)
854 return rc;
855
856 rc = pci_enable_device(pdev);
857 if (rc)
858 return rc;
859
860 pci_set_master(pdev);
861
862 rc = pci_request_regions(pdev, DRIVER_NAME);
863 if (rc) {
864 pci_dev_busy = 1;
865 goto err_out;
866 }
867
868 pci_read_config_dword(pdev, 0xac, &rc);
869 pci_write_config_dword(pdev, 0xac, rc | 0x00470000);
870
871 cnt = jmb38x_ms_count_slots(pdev);
872 if (!cnt) {
873 rc = -ENODEV;
874 pci_dev_busy = 1;
875 goto err_out;
876 }
877
878 jm = kzalloc(sizeof(struct jmb38x_ms)
879 + cnt * sizeof(struct memstick_host *), GFP_KERNEL);
880 if (!jm) {
881 rc = -ENOMEM;
882 goto err_out_int;
883 }
884
885 jm->pdev = pdev;
886 jm->host_cnt = cnt;
887 pci_set_drvdata(pdev, jm);
888
889 for (cnt = 0; cnt < jm->host_cnt; ++cnt) {
890 jm->hosts[cnt] = jmb38x_ms_alloc_host(jm, cnt);
891 if (!jm->hosts[cnt])
892 break;
893
894 rc = memstick_add_host(jm->hosts[cnt]);
895
896 if (rc) {
897 jmb38x_ms_free_host(jm->hosts[cnt]);
898 jm->hosts[cnt] = NULL;
899 break;
900 }
901 }
902
903 if (cnt)
904 return 0;
905
906 rc = -ENODEV;
907
908 pci_set_drvdata(pdev, NULL);
909 kfree(jm);
910err_out_int:
911 pci_release_regions(pdev);
912err_out:
913 if (!pci_dev_busy)
914 pci_disable_device(pdev);
915 return rc;
916}
917
918static void jmb38x_ms_remove(struct pci_dev *dev)
919{
920 struct jmb38x_ms *jm = pci_get_drvdata(dev);
921 struct jmb38x_ms_host *host;
922 int cnt;
923 unsigned long flags;
924
925 for (cnt = 0; cnt < jm->host_cnt; ++cnt) {
926 if (!jm->hosts[cnt])
927 break;
928
929 host = memstick_priv(jm->hosts[cnt]);
930
931 writel(0, host->addr + INT_SIGNAL_ENABLE);
932 writel(0, host->addr + INT_STATUS_ENABLE);
933 mmiowb();
934 dev_dbg(&jm->pdev->dev, "interrupts off\n");
935 spin_lock_irqsave(&host->lock, flags);
936 if (host->req) {
937 host->req->error = -ETIME;
938 jmb38x_ms_complete_cmd(jm->hosts[cnt], 1);
939 }
940 spin_unlock_irqrestore(&host->lock, flags);
941
942 memstick_remove_host(jm->hosts[cnt]);
943 dev_dbg(&jm->pdev->dev, "host removed\n");
944
945 jmb38x_ms_free_host(jm->hosts[cnt]);
946 }
947
948 pci_set_drvdata(dev, NULL);
949 pci_release_regions(dev);
950 pci_disable_device(dev);
951 kfree(jm);
952}
953
954static struct pci_device_id jmb38x_ms_id_tbl [] = {
955 { PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB38X_MS, PCI_ANY_ID,
956 PCI_ANY_ID, 0, 0, 0 },
957 { }
958};
959
960static struct pci_driver jmb38x_ms_driver = {
961 .name = DRIVER_NAME,
962 .id_table = jmb38x_ms_id_tbl,
963 .probe = jmb38x_ms_probe,
964 .remove = jmb38x_ms_remove,
965 .suspend = jmb38x_ms_suspend,
966 .resume = jmb38x_ms_resume
967};
968
969static int __init jmb38x_ms_init(void)
970{
971 return pci_register_driver(&jmb38x_ms_driver);
972}
973
974static void __exit jmb38x_ms_exit(void)
975{
976 pci_unregister_driver(&jmb38x_ms_driver);
977}
978
979MODULE_AUTHOR("Alex Dubov");
980MODULE_DESCRIPTION("JMicron jmb38x MemoryStick driver");
981MODULE_LICENSE("GPL");
982MODULE_DEVICE_TABLE(pci, jmb38x_ms_id_tbl);
983
984module_init(jmb38x_ms_init);
985module_exit(jmb38x_ms_exit);