jmb38x_ms.c 25 KB

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