jmb38x_ms.c 24 KB

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