jmb38x_ms.c 26 KB

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