jmb38x_ms.c 24 KB

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