4 * Copyright (C) 2007 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.
10 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
11 * that made this driver possible.
15 #include <linux/tifm.h>
16 #include <linux/memstick.h>
17 #include <linux/highmem.h>
18 #include <linux/scatterlist.h>
19 #include <linux/log2.h>
22 #define DRIVER_NAME "tifm_ms"
25 module_param(no_dma, bool, 0644);
27 #define TIFM_MS_TIMEOUT 0x00100
28 #define TIFM_MS_BADCRC 0x00200
29 #define TIFM_MS_EOTPC 0x01000
30 #define TIFM_MS_INT 0x02000
32 /* The meaning of the bit majority in this constant is unknown. */
33 #define TIFM_MS_SERIAL 0x04010
35 #define TIFM_MS_SYS_LATCH 0x00100
36 #define TIFM_MS_SYS_NOT_RDY 0x00800
37 #define TIFM_MS_SYS_DATA 0x10000
49 unsigned short eject:1,
51 unsigned short cmd_flags;
52 unsigned int mode_mask;
53 unsigned int block_pos;
54 unsigned long timeout_jiffies;
56 struct timer_list timer;
57 struct memstick_request *req;
61 static void tifm_ms_read_fifo(struct tifm_ms *host, unsigned int fifo_offset,
62 struct page *pg, unsigned int page_off,
65 struct tifm_dev *sock = host->dev;
66 unsigned int cnt = 0, off = 0;
67 unsigned char *buf = kmap_atomic(pg, KM_BIO_DST_IRQ) + page_off;
69 if (host->cmd_flags & DATA_CARRY) {
70 while ((fifo_offset & 3) && length) {
71 buf[off++] = host->io_word & 0xff;
76 if (!(fifo_offset & 3))
77 host->cmd_flags &= ~DATA_CARRY;
83 host->io_word = readl(sock->addr + SOCK_FIFO_ACCESS
86 while (length && cnt) {
87 buf[off++] = (host->io_word >> 8) & 0xff;
91 fifo_offset += 4 - cnt;
95 host->cmd_flags |= DATA_CARRY;
97 kunmap_atomic(buf - page_off, KM_BIO_DST_IRQ);
100 static void tifm_ms_write_fifo(struct tifm_ms *host, unsigned int fifo_offset,
101 struct page *pg, unsigned int page_off,
104 struct tifm_dev *sock = host->dev;
105 unsigned int cnt = 0, off = 0;
106 unsigned char *buf = kmap_atomic(pg, KM_BIO_SRC_IRQ) + page_off;
108 if (host->cmd_flags & DATA_CARRY) {
109 while (fifo_offset & 3) {
110 host->io_word |= buf[off++] << (8 * (fifo_offset & 3));
114 if (!(fifo_offset & 3)) {
115 writel(host->io_word, sock->addr + SOCK_FIFO_ACCESS
118 host->cmd_flags &= ~DATA_CARRY;
127 while (length && cnt) {
128 host->io_word |= buf[off++] << (4 - cnt);
132 fifo_offset += 4 - cnt;
134 writel(host->io_word, sock->addr + SOCK_FIFO_ACCESS
140 host->cmd_flags |= DATA_CARRY;
142 kunmap_atomic(buf - page_off, KM_BIO_SRC_IRQ);
145 static void tifm_ms_move_block(struct tifm_ms *host, unsigned int length)
148 unsigned int off = host->req->sg.offset + host->block_pos;
149 unsigned int p_off, p_cnt;
153 dev_dbg(&host->dev->dev, "moving block\n");
154 local_irq_save(flags);
157 pg = nth_page(sg_page(&host->req->sg), off >> PAGE_SHIFT);
158 p_off = offset_in_page(off);
159 p_cnt = PAGE_SIZE - p_off;
160 p_cnt = min(p_cnt, t_size);
162 if (host->req->data_dir == WRITE)
163 tifm_ms_write_fifo(host, length - t_size,
166 tifm_ms_read_fifo(host, length - t_size,
171 local_irq_restore(flags);
174 static int tifm_ms_transfer_data(struct tifm_ms *host, int skip)
176 struct tifm_dev *sock = host->dev;
177 unsigned int length = host->req->sg.length - host->block_pos;
182 if (length > TIFM_FIFO_SIZE)
183 length = TIFM_FIFO_SIZE;
186 tifm_ms_move_block(host, length);
187 host->block_pos += length;
190 if ((host->req->data_dir == READ)
191 && (host->block_pos == host->req->sg.length))
194 writel(ilog2(length) - 2, sock->addr + SOCK_FIFO_PAGE_SIZE);
195 if (host->req->data_dir == WRITE)
196 writel((1 << 8) | TIFM_DMA_TX, sock->addr + SOCK_DMA_CONTROL);
198 writel((1 << 8), sock->addr + SOCK_DMA_CONTROL);
203 static int tifm_ms_issue_cmd(struct tifm_ms *host)
205 struct tifm_dev *sock = host->dev;
207 unsigned int data_len = 0, cmd = 0, cmd_mask = 0, cnt, tval = 0;
211 if (host->req->long_data) {
213 if (1 != tifm_map_sg(sock, &host->req->sg, 1,
214 host->req->data_dir == READ
216 : PCI_DMA_TODEVICE)) {
217 host->req->error = -ENOMEM;
218 return host->req->error;
220 data_len = sg_dma_len(&host->req->sg);
222 data_len = host->req->sg.length;
224 writel(TIFM_FIFO_INT_SETALL,
225 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
226 writel(TIFM_FIFO_ENABLE,
227 sock->addr + SOCK_FIFO_CONTROL);
228 writel(TIFM_FIFO_INTMASK,
229 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
232 writel(ilog2(data_len) - 2,
233 sock->addr + SOCK_FIFO_PAGE_SIZE);
234 writel(sg_dma_address(&host->req->sg),
235 sock->addr + SOCK_DMA_ADDRESS);
236 if (host->req->data_dir == WRITE)
237 writel((1 << 8) | TIFM_DMA_TX | TIFM_DMA_EN,
238 sock->addr + SOCK_DMA_CONTROL);
240 writel((1 << 8) | TIFM_DMA_EN,
241 sock->addr + SOCK_DMA_CONTROL);
243 tifm_ms_transfer_data(host,
244 host->req->data_dir == READ);
247 cmd_mask = readl(sock->addr + SOCK_MS_SYSTEM);
248 cmd_mask |= TIFM_MS_SYS_DATA | TIFM_MS_SYS_NOT_RDY;
249 writel(cmd_mask, sock->addr + SOCK_MS_SYSTEM);
251 data = host->req->data;
252 data_len = host->req->data_len;
254 cmd_mask = host->mode_mask | 0x2607; /* unknown constant */
256 if (host->req->data_dir == WRITE) {
257 cmd_mask |= TIFM_MS_SYS_LATCH;
258 writel(cmd_mask, sock->addr + SOCK_MS_SYSTEM);
259 for (cnt = 0; (data_len - cnt) >= 4; cnt += 4) {
260 writel(TIFM_MS_SYS_LATCH
261 | readl(sock->addr + SOCK_MS_SYSTEM),
262 sock->addr + SOCK_MS_SYSTEM);
263 __raw_writel(*(unsigned int *)(data + cnt),
264 sock->addr + SOCK_MS_DATA);
265 dev_dbg(&sock->dev, "writing %x\n",
266 *(int *)(data + cnt));
268 switch (data_len - cnt) {
270 tval |= data[cnt + 2] << 16;
272 tval |= data[cnt + 1] << 8;
275 writel(TIFM_MS_SYS_LATCH
276 | readl(sock->addr + SOCK_MS_SYSTEM),
277 sock->addr + SOCK_MS_SYSTEM);
278 writel(tval, sock->addr + SOCK_MS_DATA);
279 dev_dbg(&sock->dev, "writing %x\n", tval);
282 writel(TIFM_MS_SYS_LATCH
283 | readl(sock->addr + SOCK_MS_SYSTEM),
284 sock->addr + SOCK_MS_SYSTEM);
285 writel(0, sock->addr + SOCK_MS_DATA);
286 dev_dbg(&sock->dev, "writing %x\n", 0);
289 writel(cmd_mask, sock->addr + SOCK_MS_SYSTEM);
291 cmd_mask = readl(sock->addr + SOCK_MS_SYSTEM);
292 cmd_mask &= ~TIFM_MS_SYS_DATA;
293 cmd_mask |= TIFM_MS_SYS_NOT_RDY;
294 dev_dbg(&sock->dev, "mask %x\n", cmd_mask);
295 writel(cmd_mask, sock->addr + SOCK_MS_SYSTEM);
298 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
299 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
300 sock->addr + SOCK_CONTROL);
301 host->req->error = 0;
303 cmd = (host->req->tpc & 0xf) << 12;
305 writel(cmd, sock->addr + SOCK_MS_COMMAND);
307 dev_dbg(&sock->dev, "executing TPC %x, %x\n", cmd, cmd_mask);
311 static void tifm_ms_complete_cmd(struct tifm_ms *host)
313 struct tifm_dev *sock = host->dev;
314 struct memstick_host *msh = tifm_get_drvdata(sock);
315 unsigned int tval = 0, data_len;
319 del_timer(&host->timer);
320 if (host->req->long_data) {
322 tifm_unmap_sg(sock, &host->req->sg, 1,
323 host->req->data_dir == READ
327 writel(~TIFM_MS_SYS_DATA & readl(sock->addr + SOCK_MS_SYSTEM),
328 sock->addr + SOCK_MS_SYSTEM);
330 data = host->req->data;
331 data_len = host->req->data_len;
333 if (host->req->data_dir == READ) {
334 for (rc = 0; (data_len - rc) >= 4; rc += 4)
336 = __raw_readl(sock->addr
340 tval = readl(sock->addr + SOCK_MS_DATA);
341 switch (data_len - rc) {
343 data[rc + 2] = (tval >> 16) & 0xff;
345 data[rc + 1] = (tval >> 8) & 0xff;
347 data[rc] = tval & 0xff;
349 readl(sock->addr + SOCK_MS_DATA);
353 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
354 sock->addr + SOCK_CONTROL);
357 rc = memstick_next_req(msh, &host->req);
358 } while (!rc && tifm_ms_issue_cmd(host));
361 static int tifm_ms_check_status(struct tifm_ms *host)
363 if (!host->req->error) {
364 if (!(host->cmd_flags & CMD_READY))
366 if (host->req->long_data
367 && !(host->cmd_flags & FIFO_READY))
369 if (host->req->need_card_int
370 && !(host->cmd_flags & CARD_READY))
376 /* Called from interrupt handler */
377 static void tifm_ms_data_event(struct tifm_dev *sock)
379 struct tifm_ms *host;
380 unsigned int fifo_status = 0;
383 spin_lock(&sock->lock);
384 host = memstick_priv((struct memstick_host *)tifm_get_drvdata(sock));
385 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
386 dev_dbg(&sock->dev, "data event: fifo_status %x, flags %x\n",
387 fifo_status, host->cmd_flags);
390 if (fifo_status & TIFM_FIFO_READY) {
391 if (!host->no_dma || tifm_ms_transfer_data(host, 0)) {
392 host->cmd_flags |= FIFO_READY;
393 rc = tifm_ms_check_status(host);
398 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
400 tifm_ms_complete_cmd(host);
402 spin_unlock(&sock->lock);
406 /* Called from interrupt handler */
407 static void tifm_ms_card_event(struct tifm_dev *sock)
409 struct tifm_ms *host;
410 unsigned int host_status = 0;
413 spin_lock(&sock->lock);
414 host = memstick_priv((struct memstick_host *)tifm_get_drvdata(sock));
415 host_status = readl(sock->addr + SOCK_MS_STATUS);
416 dev_dbg(&sock->dev, "host event: host_status %x, flags %x\n",
417 host_status, host->cmd_flags);
420 if (host_status & TIFM_MS_TIMEOUT)
421 host->req->error = -ETIME;
422 else if (host_status & TIFM_MS_BADCRC)
423 host->req->error = -EILSEQ;
425 if (host->req->error) {
426 writel(TIFM_FIFO_INT_SETALL,
427 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
428 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
431 if (host_status & TIFM_MS_EOTPC)
432 host->cmd_flags |= CMD_READY;
433 if (host_status & TIFM_MS_INT)
434 host->cmd_flags |= CARD_READY;
436 rc = tifm_ms_check_status(host);
440 writel(TIFM_MS_SYS_NOT_RDY | readl(sock->addr + SOCK_MS_SYSTEM),
441 sock->addr + SOCK_MS_SYSTEM);
442 writel((~TIFM_MS_SYS_DATA) & readl(sock->addr + SOCK_MS_SYSTEM),
443 sock->addr + SOCK_MS_SYSTEM);
446 tifm_ms_complete_cmd(host);
448 spin_unlock(&sock->lock);
452 static void tifm_ms_request(struct memstick_host *msh)
454 struct tifm_ms *host = memstick_priv(msh);
455 struct tifm_dev *sock = host->dev;
459 spin_lock_irqsave(&sock->lock, flags);
461 printk(KERN_ERR "%s : unfinished request detected\n",
463 spin_unlock_irqrestore(&sock->lock, flags);
464 tifm_eject(host->dev);
470 rc = memstick_next_req(msh, &host->req);
472 host->req->error = -ETIME;
474 spin_unlock_irqrestore(&sock->lock, flags);
479 rc = memstick_next_req(msh, &host->req);
480 } while (!rc && tifm_ms_issue_cmd(host));
482 spin_unlock_irqrestore(&sock->lock, flags);
486 static void tifm_ms_set_param(struct memstick_host *msh,
487 enum memstick_param param,
490 struct tifm_ms *host = memstick_priv(msh);
491 struct tifm_dev *sock = host->dev;
494 spin_lock_irqsave(&sock->lock, flags);
498 /* this is set by card detection mechanism */
500 case MEMSTICK_INTERFACE:
501 if (value == MEMSTICK_SERIAL) {
502 host->mode_mask = TIFM_MS_SERIAL;
503 writel((~TIFM_CTRL_FAST_CLK)
504 & readl(sock->addr + SOCK_CONTROL),
505 sock->addr + SOCK_CONTROL);
506 } else if (value == MEMSTICK_PAR4) {
508 writel(TIFM_CTRL_FAST_CLK
509 | readl(sock->addr + SOCK_CONTROL),
510 sock->addr + SOCK_CONTROL);
515 spin_unlock_irqrestore(&sock->lock, flags);
518 static void tifm_ms_abort(unsigned long data)
520 struct tifm_ms *host = (struct tifm_ms *)data;
522 dev_dbg(&host->dev->dev, "status %x\n",
523 readl(host->dev->addr + SOCK_MS_STATUS));
525 "%s : card failed to respond for a long period of time "
527 host->dev->dev.bus_id, host->req ? host->req->tpc : 0,
530 tifm_eject(host->dev);
533 static int tifm_ms_initialize_host(struct tifm_ms *host)
535 struct tifm_dev *sock = host->dev;
536 struct memstick_host *msh = tifm_get_drvdata(sock);
538 host->mode_mask = TIFM_MS_SERIAL;
539 writel(0x8000, sock->addr + SOCK_MS_SYSTEM);
540 writel(0x0200 | TIFM_MS_SYS_NOT_RDY, sock->addr + SOCK_MS_SYSTEM);
541 writel(0xffffffff, sock->addr + SOCK_MS_STATUS);
542 if (tifm_has_ms_pif(sock))
543 msh->caps |= MEMSTICK_CAP_PAR4;
548 static int tifm_ms_probe(struct tifm_dev *sock)
550 struct memstick_host *msh;
551 struct tifm_ms *host;
554 if (!(TIFM_SOCK_STATE_OCCUPIED
555 & readl(sock->addr + SOCK_PRESENT_STATE))) {
556 printk(KERN_WARNING "%s : card gone, unexpectedly\n",
561 msh = memstick_alloc_host(sizeof(struct tifm_ms), &sock->dev);
565 host = memstick_priv(msh);
566 tifm_set_drvdata(sock, msh);
568 host->timeout_jiffies = msecs_to_jiffies(1000);
569 host->no_dma = no_dma;
571 setup_timer(&host->timer, tifm_ms_abort, (unsigned long)host);
573 msh->request = tifm_ms_request;
574 msh->set_param = tifm_ms_set_param;
575 sock->card_event = tifm_ms_card_event;
576 sock->data_event = tifm_ms_data_event;
577 rc = tifm_ms_initialize_host(host);
580 rc = memstick_add_host(msh);
584 memstick_free_host(msh);
588 static void tifm_ms_remove(struct tifm_dev *sock)
590 struct memstick_host *msh = tifm_get_drvdata(sock);
591 struct tifm_ms *host = memstick_priv(msh);
595 spin_lock_irqsave(&sock->lock, flags);
598 del_timer(&host->timer);
599 writel(TIFM_FIFO_INT_SETALL,
600 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
601 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
602 if (host->req->long_data && !host->no_dma)
603 tifm_unmap_sg(sock, &host->req->sg, 1,
604 host->req->data_dir == READ
606 : PCI_DMA_FROMDEVICE);
607 host->req->error = -ETIME;
610 rc = memstick_next_req(msh, &host->req);
612 host->req->error = -ETIME;
615 spin_unlock_irqrestore(&sock->lock, flags);
617 memstick_remove_host(msh);
619 writel(0x0200 | TIFM_MS_SYS_NOT_RDY, sock->addr + SOCK_MS_SYSTEM);
620 writel(0xffffffff, sock->addr + SOCK_MS_STATUS);
622 memstick_free_host(msh);
627 static int tifm_ms_suspend(struct tifm_dev *sock, pm_message_t state)
629 struct memstick_host *msh = tifm_get_drvdata(sock);
631 memstick_suspend_host(msh);
635 static int tifm_ms_resume(struct tifm_dev *sock)
637 struct memstick_host *msh = tifm_get_drvdata(sock);
639 memstick_resume_host(msh);
645 #define tifm_ms_suspend NULL
646 #define tifm_ms_resume NULL
648 #endif /* CONFIG_PM */
650 static struct tifm_device_id tifm_ms_id_tbl[] = {
651 { TIFM_TYPE_MS }, { 0 }
654 static struct tifm_driver tifm_ms_driver = {
659 .id_table = tifm_ms_id_tbl,
660 .probe = tifm_ms_probe,
661 .remove = tifm_ms_remove,
662 .suspend = tifm_ms_suspend,
663 .resume = tifm_ms_resume
666 static int __init tifm_ms_init(void)
668 return tifm_register_driver(&tifm_ms_driver);
671 static void __exit tifm_ms_exit(void)
673 tifm_unregister_driver(&tifm_ms_driver);
676 MODULE_AUTHOR("Alex Dubov");
677 MODULE_DESCRIPTION("TI FlashMedia MemoryStick driver");
678 MODULE_LICENSE("GPL");
679 MODULE_DEVICE_TABLE(tifm, tifm_ms_id_tbl);
681 module_init(tifm_ms_init);
682 module_exit(tifm_ms_exit);