blob: 8577de4ebb0efa11d9103105c87be938f3ce1160 [file] [log] [blame]
Alex Dubovbaf85322008-02-09 10:20:54 -08001/*
2 * TI FlashMedia driver
3 *
4 * Copyright (C) 2007 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 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
11 * that made this driver possible.
12 *
13 */
14
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>
20#include <asm/io.h>
21
22#define DRIVER_NAME "tifm_ms"
Alex Dubovbaf85322008-02-09 10:20:54 -080023
24static int no_dma;
25module_param(no_dma, bool, 0644);
26
Alex Dubov92b22d92008-03-10 11:43:40 -070027/*
28 * Some control bits of TIFM appear to conform to Sony's reference design,
29 * so I'm just assuming they all are.
30 */
Alex Dubovbaf85322008-02-09 10:20:54 -080031
Alex Dubov92b22d92008-03-10 11:43:40 -070032#define TIFM_MS_STAT_DRQ 0x04000
33#define TIFM_MS_STAT_MSINT 0x02000
34#define TIFM_MS_STAT_RDY 0x01000
35#define TIFM_MS_STAT_CRC 0x00200
36#define TIFM_MS_STAT_TOE 0x00100
37#define TIFM_MS_STAT_EMP 0x00020
38#define TIFM_MS_STAT_FUL 0x00010
39#define TIFM_MS_STAT_CED 0x00008
40#define TIFM_MS_STAT_ERR 0x00004
41#define TIFM_MS_STAT_BRQ 0x00002
42#define TIFM_MS_STAT_CNK 0x00001
Alex Dubovbaf85322008-02-09 10:20:54 -080043
Alex Dubov92b22d92008-03-10 11:43:40 -070044#define TIFM_MS_SYS_DMA 0x10000
45#define TIFM_MS_SYS_RESET 0x08000
46#define TIFM_MS_SYS_SRAC 0x04000
47#define TIFM_MS_SYS_INTEN 0x02000
48#define TIFM_MS_SYS_NOCRC 0x01000
49#define TIFM_MS_SYS_INTCLR 0x00800
50#define TIFM_MS_SYS_MSIEN 0x00400
51#define TIFM_MS_SYS_FCLR 0x00200
52#define TIFM_MS_SYS_FDIR 0x00100
53#define TIFM_MS_SYS_DAM 0x00080
54#define TIFM_MS_SYS_DRM 0x00040
55#define TIFM_MS_SYS_DRQSL 0x00020
56#define TIFM_MS_SYS_REI 0x00010
57#define TIFM_MS_SYS_REO 0x00008
58#define TIFM_MS_SYS_BSY_MASK 0x00007
59
60#define TIFM_MS_SYS_FIFO (TIFM_MS_SYS_INTEN | TIFM_MS_SYS_MSIEN \
61 | TIFM_MS_SYS_FCLR | TIFM_MS_SYS_BSY_MASK)
Alex Dubovbaf85322008-02-09 10:20:54 -080062
63/* Hardware flags */
64enum {
Alex Dubov92b22d92008-03-10 11:43:40 -070065 CMD_READY = 0x01,
66 FIFO_READY = 0x02,
67 CARD_INT = 0x04
Alex Dubovbaf85322008-02-09 10:20:54 -080068};
69
70struct tifm_ms {
71 struct tifm_dev *dev;
Alex Dubov92b22d92008-03-10 11:43:40 -070072 struct timer_list timer;
73 struct memstick_request *req;
Alex Dubovbaf85322008-02-09 10:20:54 -080074 unsigned int mode_mask;
75 unsigned int block_pos;
76 unsigned long timeout_jiffies;
Alex Dubov92b22d92008-03-10 11:43:40 -070077 unsigned char eject:1,
78 use_dma:1;
79 unsigned char cmd_flags;
80 unsigned char io_pos;
Alex Dubovbaf85322008-02-09 10:20:54 -080081 unsigned int io_word;
82};
83
Alex Dubov92b22d92008-03-10 11:43:40 -070084static unsigned int tifm_ms_read_data(struct tifm_ms *host,
85 unsigned char *buf, unsigned int length)
Alex Dubovbaf85322008-02-09 10:20:54 -080086{
87 struct tifm_dev *sock = host->dev;
Alex Dubov92b22d92008-03-10 11:43:40 -070088 unsigned int off = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -080089
Alex Dubov92b22d92008-03-10 11:43:40 -070090 while (host->io_pos && length) {
91 buf[off++] = host->io_word & 0xff;
92 host->io_word >>= 8;
93 length--;
94 host->io_pos--;
95 }
96
97 if (!length)
98 return off;
99
100 while (!(TIFM_MS_STAT_EMP & readl(sock->addr + SOCK_MS_STATUS))) {
101 if (length < 4)
102 break;
103 *(unsigned int *)(buf + off) = __raw_readl(sock->addr
104 + SOCK_MS_DATA);
105 length -= 4;
106 off += 4;
107 }
108
109 if (length
110 && !(TIFM_MS_STAT_EMP & readl(sock->addr + SOCK_MS_STATUS))) {
111 host->io_word = readl(sock->addr + SOCK_MS_DATA);
112 for (host->io_pos = 4; host->io_pos; --host->io_pos) {
Alex Dubovbaf85322008-02-09 10:20:54 -0800113 buf[off++] = host->io_word & 0xff;
114 host->io_word >>= 8;
115 length--;
Alex Dubov92b22d92008-03-10 11:43:40 -0700116 if (!length)
117 break;
Alex Dubovbaf85322008-02-09 10:20:54 -0800118 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800119 }
120
Alex Dubov92b22d92008-03-10 11:43:40 -0700121 return off;
Alex Dubovbaf85322008-02-09 10:20:54 -0800122}
123
Alex Dubov92b22d92008-03-10 11:43:40 -0700124static unsigned int tifm_ms_write_data(struct tifm_ms *host,
125 unsigned char *buf, unsigned int length)
Alex Dubovbaf85322008-02-09 10:20:54 -0800126{
127 struct tifm_dev *sock = host->dev;
Alex Dubov92b22d92008-03-10 11:43:40 -0700128 unsigned int off = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800129
Alex Dubov92b22d92008-03-10 11:43:40 -0700130 if (host->io_pos) {
131 while (host->io_pos < 4 && length) {
132 host->io_word |= buf[off++] << (host->io_pos * 8);
133 host->io_pos++;
Alex Dubovbaf85322008-02-09 10:20:54 -0800134 length--;
Alex Dubovbaf85322008-02-09 10:20:54 -0800135 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800136 }
137
Alex Dubov92b22d92008-03-10 11:43:40 -0700138 if (host->io_pos == 4
139 && !(TIFM_MS_STAT_FUL & readl(sock->addr + SOCK_MS_STATUS))) {
140 writel(TIFM_MS_SYS_FDIR | readl(sock->addr + SOCK_MS_SYSTEM),
141 sock->addr + SOCK_MS_SYSTEM);
142 writel(host->io_word, sock->addr + SOCK_MS_DATA);
143 host->io_pos = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800144 host->io_word = 0;
Alex Dubov92b22d92008-03-10 11:43:40 -0700145 } else if (host->io_pos) {
146 return off;
Alex Dubovbaf85322008-02-09 10:20:54 -0800147 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800148
149 if (!length)
Alex Dubov92b22d92008-03-10 11:43:40 -0700150 return off;
Alex Dubovbaf85322008-02-09 10:20:54 -0800151
Alex Dubov92b22d92008-03-10 11:43:40 -0700152 while (!(TIFM_MS_STAT_FUL & readl(sock->addr + SOCK_MS_STATUS))) {
153 if (length < 4)
154 break;
155 writel(TIFM_MS_SYS_FDIR | readl(sock->addr + SOCK_MS_SYSTEM),
156 sock->addr + SOCK_MS_SYSTEM);
157 __raw_writel(*(unsigned int *)(buf + off),
158 sock->addr + SOCK_MS_DATA);
159 length -= 4;
160 off += 4;
Alex Dubovbaf85322008-02-09 10:20:54 -0800161 }
162
Alex Dubov92b22d92008-03-10 11:43:40 -0700163 switch (length) {
164 case 3:
165 host->io_word |= buf[off + 2] << 16;
166 host->io_pos++;
167 case 2:
168 host->io_word |= buf[off + 1] << 8;
169 host->io_pos++;
170 case 1:
171 host->io_word |= buf[off];
172 host->io_pos++;
173 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800174
Alex Dubov92b22d92008-03-10 11:43:40 -0700175 off += host->io_pos;
Alex Dubovbaf85322008-02-09 10:20:54 -0800176
Alex Dubov92b22d92008-03-10 11:43:40 -0700177 return off;
178}
179
180static unsigned int tifm_ms_transfer_data(struct tifm_ms *host)
181{
182 struct tifm_dev *sock = host->dev;
183 unsigned int length;
184 unsigned int off;
Andrew Mortond250dad2008-03-28 14:16:09 -0700185 unsigned int t_size, p_cnt;
Alex Dubov92b22d92008-03-10 11:43:40 -0700186 unsigned char *buf;
187 struct page *pg;
188 unsigned long flags = 0;
189
190 if (host->req->long_data) {
191 length = host->req->sg.length - host->block_pos;
192 off = host->req->sg.offset + host->block_pos;
193 } else {
194 length = host->req->data_len - host->block_pos;
195 off = 0;
196 }
197 dev_dbg(&sock->dev, "fifo data transfer, %d, %d\n", length,
198 host->block_pos);
199
200 while (length) {
Andrew Mortond250dad2008-03-28 14:16:09 -0700201 unsigned int uninitialized_var(p_off);
202
Alex Dubov92b22d92008-03-10 11:43:40 -0700203 if (host->req->long_data) {
204 pg = nth_page(sg_page(&host->req->sg),
205 off >> PAGE_SHIFT);
206 p_off = offset_in_page(off);
207 p_cnt = PAGE_SIZE - p_off;
208 p_cnt = min(p_cnt, length);
209
210 local_irq_save(flags);
211 buf = kmap_atomic(pg, KM_BIO_SRC_IRQ) + p_off;
212 } else {
213 buf = host->req->data + host->block_pos;
214 p_cnt = host->req->data_len - host->block_pos;
215 }
216
217 t_size = host->req->data_dir == WRITE
218 ? tifm_ms_write_data(host, buf, p_cnt)
219 : tifm_ms_read_data(host, buf, p_cnt);
220
221 if (host->req->long_data) {
222 kunmap_atomic(buf - p_off, KM_BIO_SRC_IRQ);
223 local_irq_restore(flags);
224 }
225
226 if (!t_size)
227 break;
228 host->block_pos += t_size;
229 length -= t_size;
230 off += t_size;
231 }
232
233 dev_dbg(&sock->dev, "fifo data transfer, %d remaining\n", length);
234 if (!length && (host->req->data_dir == WRITE)) {
235 if (host->io_pos) {
236 writel(TIFM_MS_SYS_FDIR
237 | readl(sock->addr + SOCK_MS_SYSTEM),
238 sock->addr + SOCK_MS_SYSTEM);
239 writel(host->io_word, sock->addr + SOCK_MS_DATA);
240 }
241 writel(TIFM_MS_SYS_FDIR
242 | readl(sock->addr + SOCK_MS_SYSTEM),
243 sock->addr + SOCK_MS_SYSTEM);
244 writel(0, sock->addr + SOCK_MS_DATA);
245 } else {
246 readl(sock->addr + SOCK_MS_DATA);
247 }
248
249 return length;
Alex Dubovbaf85322008-02-09 10:20:54 -0800250}
251
252static int tifm_ms_issue_cmd(struct tifm_ms *host)
253{
254 struct tifm_dev *sock = host->dev;
255 unsigned char *data;
Alex Dubov92b22d92008-03-10 11:43:40 -0700256 unsigned int data_len, cmd, sys_param;
Alex Dubovbaf85322008-02-09 10:20:54 -0800257
258 host->cmd_flags = 0;
Alex Dubov92b22d92008-03-10 11:43:40 -0700259 host->block_pos = 0;
260 host->io_pos = 0;
261 host->io_word = 0;
262 host->cmd_flags = 0;
263
264 data = host->req->data;
265
266 host->use_dma = !no_dma;
Alex Dubovbaf85322008-02-09 10:20:54 -0800267
Alex Dubove1f19992008-03-10 11:43:37 -0700268 if (host->req->long_data) {
Alex Dubov92b22d92008-03-10 11:43:40 -0700269 data_len = host->req->sg.length;
270 if (!is_power_of_2(data_len))
271 host->use_dma = 0;
272 } else {
273 data_len = host->req->data_len;
274 host->use_dma = 0;
275 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800276
Alex Dubov92b22d92008-03-10 11:43:40 -0700277 writel(TIFM_FIFO_INT_SETALL,
278 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
279 writel(TIFM_FIFO_ENABLE,
280 sock->addr + SOCK_FIFO_CONTROL);
281
282 if (host->use_dma) {
283 if (1 != tifm_map_sg(sock, &host->req->sg, 1,
284 host->req->data_dir == READ
285 ? PCI_DMA_FROMDEVICE
286 : PCI_DMA_TODEVICE)) {
287 host->req->error = -ENOMEM;
288 return host->req->error;
289 }
290 data_len = sg_dma_len(&host->req->sg);
291
292 writel(ilog2(data_len) - 2,
293 sock->addr + SOCK_FIFO_PAGE_SIZE);
294 writel(TIFM_FIFO_INTMASK,
295 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
296 sys_param = TIFM_DMA_EN | (1 << 8);
297 if (host->req->data_dir == WRITE)
298 sys_param |= TIFM_DMA_TX;
299
Alex Dubovbaf85322008-02-09 10:20:54 -0800300 writel(TIFM_FIFO_INTMASK,
301 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
302
Alex Dubov92b22d92008-03-10 11:43:40 -0700303 writel(sg_dma_address(&host->req->sg),
304 sock->addr + SOCK_DMA_ADDRESS);
305 writel(sys_param, sock->addr + SOCK_DMA_CONTROL);
Alex Dubove1f19992008-03-10 11:43:37 -0700306 } else {
Alex Dubov92b22d92008-03-10 11:43:40 -0700307 writel(host->mode_mask | TIFM_MS_SYS_FIFO,
308 sock->addr + SOCK_MS_SYSTEM);
Alex Dubovbaf85322008-02-09 10:20:54 -0800309
Alex Dubov92b22d92008-03-10 11:43:40 -0700310 writel(TIFM_FIFO_MORE,
311 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_SET);
Alex Dubove1f19992008-03-10 11:43:37 -0700312 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800313
314 mod_timer(&host->timer, jiffies + host->timeout_jiffies);
315 writel(TIFM_CTRL_LED | readl(sock->addr + SOCK_CONTROL),
316 sock->addr + SOCK_CONTROL);
317 host->req->error = 0;
318
Alex Dubov92b22d92008-03-10 11:43:40 -0700319 sys_param = readl(sock->addr + SOCK_MS_SYSTEM);
320 sys_param |= TIFM_MS_SYS_INTCLR;
321
322 if (host->use_dma)
323 sys_param |= TIFM_MS_SYS_DMA;
324 else
325 sys_param &= ~TIFM_MS_SYS_DMA;
326
327 writel(sys_param, sock->addr + SOCK_MS_SYSTEM);
328
Alex Dubovbaf85322008-02-09 10:20:54 -0800329 cmd = (host->req->tpc & 0xf) << 12;
330 cmd |= data_len;
331 writel(cmd, sock->addr + SOCK_MS_COMMAND);
332
Alex Dubov92b22d92008-03-10 11:43:40 -0700333 dev_dbg(&sock->dev, "executing TPC %x, %x\n", cmd, sys_param);
Alex Dubovbaf85322008-02-09 10:20:54 -0800334 return 0;
335}
336
337static void tifm_ms_complete_cmd(struct tifm_ms *host)
338{
339 struct tifm_dev *sock = host->dev;
340 struct memstick_host *msh = tifm_get_drvdata(sock);
Alex Dubovbaf85322008-02-09 10:20:54 -0800341 int rc;
342
343 del_timer(&host->timer);
Alex Dubovbaf85322008-02-09 10:20:54 -0800344
Alex Dubovead70772008-03-19 17:01:06 -0700345 host->req->int_reg = readl(sock->addr + SOCK_MS_STATUS) & 0xff;
346 host->req->int_reg = (host->req->int_reg & 1)
347 | ((host->req->int_reg << 4) & 0xe0);
348
349 writel(TIFM_FIFO_INT_SETALL,
350 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
351 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
352
353 if (host->use_dma) {
Alex Dubov92b22d92008-03-10 11:43:40 -0700354 tifm_unmap_sg(sock, &host->req->sg, 1,
355 host->req->data_dir == READ
356 ? PCI_DMA_FROMDEVICE
357 : PCI_DMA_TODEVICE);
Alex Dubovead70772008-03-19 17:01:06 -0700358 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800359
360 writel((~TIFM_CTRL_LED) & readl(sock->addr + SOCK_CONTROL),
361 sock->addr + SOCK_CONTROL);
362
Alex Dubov92b22d92008-03-10 11:43:40 -0700363 dev_dbg(&sock->dev, "TPC complete\n");
Alex Dubovbaf85322008-02-09 10:20:54 -0800364 do {
365 rc = memstick_next_req(msh, &host->req);
366 } while (!rc && tifm_ms_issue_cmd(host));
367}
368
369static int tifm_ms_check_status(struct tifm_ms *host)
370{
371 if (!host->req->error) {
372 if (!(host->cmd_flags & CMD_READY))
373 return 1;
Alex Dubov92b22d92008-03-10 11:43:40 -0700374 if (!(host->cmd_flags & FIFO_READY))
Alex Dubovbaf85322008-02-09 10:20:54 -0800375 return 1;
376 if (host->req->need_card_int
Alex Dubov92b22d92008-03-10 11:43:40 -0700377 && !(host->cmd_flags & CARD_INT))
Alex Dubovbaf85322008-02-09 10:20:54 -0800378 return 1;
379 }
380 return 0;
381}
382
383/* Called from interrupt handler */
384static void tifm_ms_data_event(struct tifm_dev *sock)
385{
386 struct tifm_ms *host;
Alex Dubov92b22d92008-03-10 11:43:40 -0700387 unsigned int fifo_status = 0, host_status = 0;
Alex Dubovbaf85322008-02-09 10:20:54 -0800388 int rc = 1;
389
390 spin_lock(&sock->lock);
391 host = memstick_priv((struct memstick_host *)tifm_get_drvdata(sock));
392 fifo_status = readl(sock->addr + SOCK_DMA_FIFO_STATUS);
Alex Dubov92b22d92008-03-10 11:43:40 -0700393 host_status = readl(sock->addr + SOCK_MS_STATUS);
394 dev_dbg(&sock->dev,
395 "data event: fifo_status %x, host_status %x, flags %x\n",
396 fifo_status, host_status, host->cmd_flags);
Alex Dubovbaf85322008-02-09 10:20:54 -0800397
398 if (host->req) {
Alex Dubov92b22d92008-03-10 11:43:40 -0700399 if (host->use_dma && (fifo_status & 1)) {
400 host->cmd_flags |= FIFO_READY;
401 rc = tifm_ms_check_status(host);
402 }
403 if (!host->use_dma && (fifo_status & TIFM_FIFO_MORE)) {
404 if (!tifm_ms_transfer_data(host)) {
Alex Dubovbaf85322008-02-09 10:20:54 -0800405 host->cmd_flags |= FIFO_READY;
406 rc = tifm_ms_check_status(host);
407 }
408 }
409 }
410
411 writel(fifo_status, sock->addr + SOCK_DMA_FIFO_STATUS);
412 if (!rc)
413 tifm_ms_complete_cmd(host);
414
415 spin_unlock(&sock->lock);
416}
417
418
419/* Called from interrupt handler */
420static void tifm_ms_card_event(struct tifm_dev *sock)
421{
422 struct tifm_ms *host;
423 unsigned int host_status = 0;
424 int rc = 1;
425
426 spin_lock(&sock->lock);
427 host = memstick_priv((struct memstick_host *)tifm_get_drvdata(sock));
428 host_status = readl(sock->addr + SOCK_MS_STATUS);
429 dev_dbg(&sock->dev, "host event: host_status %x, flags %x\n",
430 host_status, host->cmd_flags);
431
432 if (host->req) {
Alex Dubov92b22d92008-03-10 11:43:40 -0700433 if (host_status & TIFM_MS_STAT_TOE)
Alex Dubovbaf85322008-02-09 10:20:54 -0800434 host->req->error = -ETIME;
Alex Dubov92b22d92008-03-10 11:43:40 -0700435 else if (host_status & TIFM_MS_STAT_CRC)
Alex Dubovbaf85322008-02-09 10:20:54 -0800436 host->req->error = -EILSEQ;
437
Alex Dubov92b22d92008-03-10 11:43:40 -0700438 if (host_status & TIFM_MS_STAT_RDY)
Alex Dubovbaf85322008-02-09 10:20:54 -0800439 host->cmd_flags |= CMD_READY;
Alex Dubov92b22d92008-03-10 11:43:40 -0700440
441 if (host_status & TIFM_MS_STAT_MSINT)
442 host->cmd_flags |= CARD_INT;
Alex Dubovbaf85322008-02-09 10:20:54 -0800443
444 rc = tifm_ms_check_status(host);
445
446 }
447
Alex Dubov92b22d92008-03-10 11:43:40 -0700448 writel(TIFM_MS_SYS_INTCLR | readl(sock->addr + SOCK_MS_SYSTEM),
Alex Dubovbaf85322008-02-09 10:20:54 -0800449 sock->addr + SOCK_MS_SYSTEM);
450
451 if (!rc)
452 tifm_ms_complete_cmd(host);
453
454 spin_unlock(&sock->lock);
455 return;
456}
457
458static void tifm_ms_request(struct memstick_host *msh)
459{
460 struct tifm_ms *host = memstick_priv(msh);
461 struct tifm_dev *sock = host->dev;
462 unsigned long flags;
463 int rc;
464
465 spin_lock_irqsave(&sock->lock, flags);
466 if (host->req) {
467 printk(KERN_ERR "%s : unfinished request detected\n",
468 sock->dev.bus_id);
469 spin_unlock_irqrestore(&sock->lock, flags);
470 tifm_eject(host->dev);
471 return;
472 }
473
474 if (host->eject) {
475 do {
476 rc = memstick_next_req(msh, &host->req);
477 if (!rc)
478 host->req->error = -ETIME;
479 } while (!rc);
480 spin_unlock_irqrestore(&sock->lock, flags);
481 return;
482 }
483
484 do {
485 rc = memstick_next_req(msh, &host->req);
486 } while (!rc && tifm_ms_issue_cmd(host));
487
488 spin_unlock_irqrestore(&sock->lock, flags);
489 return;
490}
491
492static void tifm_ms_set_param(struct memstick_host *msh,
493 enum memstick_param param,
494 int value)
495{
496 struct tifm_ms *host = memstick_priv(msh);
497 struct tifm_dev *sock = host->dev;
498 unsigned long flags;
499
500 spin_lock_irqsave(&sock->lock, flags);
501
502 switch (param) {
503 case MEMSTICK_POWER:
Alex Duboveebbe9c2008-03-10 11:43:40 -0700504 /* also affected by media detection mechanism */
505 if (value == MEMSTICK_POWER_ON) {
506 host->mode_mask = TIFM_MS_SYS_SRAC | TIFM_MS_SYS_REI;
507 writel(TIFM_MS_SYS_RESET, sock->addr + SOCK_MS_SYSTEM);
508 writel(TIFM_MS_SYS_FCLR | TIFM_MS_SYS_INTCLR,
509 sock->addr + SOCK_MS_SYSTEM);
510 writel(0xffffffff, sock->addr + SOCK_MS_STATUS);
511 } else if (value == MEMSTICK_POWER_OFF) {
512 writel(TIFM_MS_SYS_FCLR | TIFM_MS_SYS_INTCLR,
513 sock->addr + SOCK_MS_SYSTEM);
514 writel(0xffffffff, sock->addr + SOCK_MS_STATUS);
515 }
Alex Dubovbaf85322008-02-09 10:20:54 -0800516 break;
517 case MEMSTICK_INTERFACE:
518 if (value == MEMSTICK_SERIAL) {
Alex Dubov92b22d92008-03-10 11:43:40 -0700519 host->mode_mask = TIFM_MS_SYS_SRAC | TIFM_MS_SYS_REI;
Alex Dubovbaf85322008-02-09 10:20:54 -0800520 writel((~TIFM_CTRL_FAST_CLK)
521 & readl(sock->addr + SOCK_CONTROL),
522 sock->addr + SOCK_CONTROL);
Alex Dubove1f19992008-03-10 11:43:37 -0700523 } else if (value == MEMSTICK_PAR4) {
Alex Dubovbaf85322008-02-09 10:20:54 -0800524 host->mode_mask = 0;
525 writel(TIFM_CTRL_FAST_CLK
526 | readl(sock->addr + SOCK_CONTROL),
527 sock->addr + SOCK_CONTROL);
528 }
529 break;
530 };
531
532 spin_unlock_irqrestore(&sock->lock, flags);
533}
534
535static void tifm_ms_abort(unsigned long data)
536{
537 struct tifm_ms *host = (struct tifm_ms *)data;
538
539 dev_dbg(&host->dev->dev, "status %x\n",
540 readl(host->dev->addr + SOCK_MS_STATUS));
541 printk(KERN_ERR
542 "%s : card failed to respond for a long period of time "
543 "(%x, %x)\n",
544 host->dev->dev.bus_id, host->req ? host->req->tpc : 0,
545 host->cmd_flags);
546
547 tifm_eject(host->dev);
548}
549
Alex Dubovbaf85322008-02-09 10:20:54 -0800550static int tifm_ms_probe(struct tifm_dev *sock)
551{
552 struct memstick_host *msh;
553 struct tifm_ms *host;
554 int rc = -EIO;
555
556 if (!(TIFM_SOCK_STATE_OCCUPIED
557 & readl(sock->addr + SOCK_PRESENT_STATE))) {
558 printk(KERN_WARNING "%s : card gone, unexpectedly\n",
559 sock->dev.bus_id);
560 return rc;
561 }
562
563 msh = memstick_alloc_host(sizeof(struct tifm_ms), &sock->dev);
564 if (!msh)
565 return -ENOMEM;
566
567 host = memstick_priv(msh);
568 tifm_set_drvdata(sock, msh);
569 host->dev = sock;
570 host->timeout_jiffies = msecs_to_jiffies(1000);
Alex Dubovbaf85322008-02-09 10:20:54 -0800571
572 setup_timer(&host->timer, tifm_ms_abort, (unsigned long)host);
573
574 msh->request = tifm_ms_request;
575 msh->set_param = tifm_ms_set_param;
576 sock->card_event = tifm_ms_card_event;
577 sock->data_event = tifm_ms_data_event;
Alex Duboveebbe9c2008-03-10 11:43:40 -0700578 if (tifm_has_ms_pif(sock))
579 msh->caps |= MEMSTICK_CAP_PAR4;
Alex Dubovbaf85322008-02-09 10:20:54 -0800580
Alex Duboveebbe9c2008-03-10 11:43:40 -0700581 rc = memstick_add_host(msh);
Alex Dubovbaf85322008-02-09 10:20:54 -0800582 if (!rc)
583 return 0;
584
585 memstick_free_host(msh);
586 return rc;
587}
588
589static void tifm_ms_remove(struct tifm_dev *sock)
590{
591 struct memstick_host *msh = tifm_get_drvdata(sock);
592 struct tifm_ms *host = memstick_priv(msh);
593 int rc = 0;
594 unsigned long flags;
595
596 spin_lock_irqsave(&sock->lock, flags);
597 host->eject = 1;
598 if (host->req) {
599 del_timer(&host->timer);
600 writel(TIFM_FIFO_INT_SETALL,
601 sock->addr + SOCK_DMA_FIFO_INT_ENABLE_CLEAR);
602 writel(TIFM_DMA_RESET, sock->addr + SOCK_DMA_CONTROL);
Alex Dubov92b22d92008-03-10 11:43:40 -0700603 if (host->use_dma)
Alex Dubovbaf85322008-02-09 10:20:54 -0800604 tifm_unmap_sg(sock, &host->req->sg, 1,
605 host->req->data_dir == READ
606 ? PCI_DMA_TODEVICE
607 : PCI_DMA_FROMDEVICE);
608 host->req->error = -ETIME;
609
610 do {
611 rc = memstick_next_req(msh, &host->req);
612 if (!rc)
613 host->req->error = -ETIME;
614 } while (!rc);
615 }
616 spin_unlock_irqrestore(&sock->lock, flags);
617
618 memstick_remove_host(msh);
Alex Dubovbaf85322008-02-09 10:20:54 -0800619 memstick_free_host(msh);
620}
621
622#ifdef CONFIG_PM
623
624static int tifm_ms_suspend(struct tifm_dev *sock, pm_message_t state)
625{
Alex Dubovd114ad52008-03-10 11:43:38 -0700626 struct memstick_host *msh = tifm_get_drvdata(sock);
627
628 memstick_suspend_host(msh);
Alex Dubovbaf85322008-02-09 10:20:54 -0800629 return 0;
630}
631
632static int tifm_ms_resume(struct tifm_dev *sock)
633{
634 struct memstick_host *msh = tifm_get_drvdata(sock);
Alex Dubovbaf85322008-02-09 10:20:54 -0800635
Alex Dubovd114ad52008-03-10 11:43:38 -0700636 memstick_resume_host(msh);
Alex Dubovbaf85322008-02-09 10:20:54 -0800637 return 0;
638}
639
640#else
641
642#define tifm_ms_suspend NULL
643#define tifm_ms_resume NULL
644
645#endif /* CONFIG_PM */
646
647static struct tifm_device_id tifm_ms_id_tbl[] = {
648 { TIFM_TYPE_MS }, { 0 }
649};
650
651static struct tifm_driver tifm_ms_driver = {
652 .driver = {
653 .name = DRIVER_NAME,
654 .owner = THIS_MODULE
655 },
656 .id_table = tifm_ms_id_tbl,
657 .probe = tifm_ms_probe,
658 .remove = tifm_ms_remove,
659 .suspend = tifm_ms_suspend,
660 .resume = tifm_ms_resume
661};
662
663static int __init tifm_ms_init(void)
664{
665 return tifm_register_driver(&tifm_ms_driver);
666}
667
668static void __exit tifm_ms_exit(void)
669{
670 tifm_unregister_driver(&tifm_ms_driver);
671}
672
673MODULE_AUTHOR("Alex Dubov");
674MODULE_DESCRIPTION("TI FlashMedia MemoryStick driver");
675MODULE_LICENSE("GPL");
676MODULE_DEVICE_TABLE(tifm, tifm_ms_id_tbl);
Alex Dubovbaf85322008-02-09 10:20:54 -0800677
678module_init(tifm_ms_init);
679module_exit(tifm_ms_exit);