jmb38x_ms.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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 char ifmode;
  60. unsigned int io_word[2];
  61. };
  62. struct jmb38x_ms {
  63. struct pci_dev *pdev;
  64. int host_cnt;
  65. struct memstick_host *hosts[];
  66. };
  67. #define BLOCK_COUNT_MASK 0xffff0000
  68. #define BLOCK_SIZE_MASK 0x00000fff
  69. #define DMA_CONTROL_ENABLE 0x00000001
  70. #define TPC_DATA_SEL 0x00008000
  71. #define TPC_DIR 0x00004000
  72. #define TPC_WAIT_INT 0x00002000
  73. #define TPC_GET_INT 0x00000800
  74. #define TPC_CODE_SZ_MASK 0x00000700
  75. #define TPC_DATA_SZ_MASK 0x00000007
  76. #define HOST_CONTROL_TDELAY_EN 0x00040000
  77. #define HOST_CONTROL_HW_OC_P 0x00010000
  78. #define HOST_CONTROL_RESET_REQ 0x00008000
  79. #define HOST_CONTROL_REI 0x00004000
  80. #define HOST_CONTROL_LED 0x00000400
  81. #define HOST_CONTROL_FAST_CLK 0x00000200
  82. #define HOST_CONTROL_RESET 0x00000100
  83. #define HOST_CONTROL_POWER_EN 0x00000080
  84. #define HOST_CONTROL_CLOCK_EN 0x00000040
  85. #define HOST_CONTROL_REO 0x00000008
  86. #define HOST_CONTROL_IF_SHIFT 4
  87. #define HOST_CONTROL_IF_SERIAL 0x0
  88. #define HOST_CONTROL_IF_PAR4 0x1
  89. #define HOST_CONTROL_IF_PAR8 0x3
  90. #define STATUS_BUSY 0x00080000
  91. #define STATUS_MS_DAT7 0x00040000
  92. #define STATUS_MS_DAT6 0x00020000
  93. #define STATUS_MS_DAT5 0x00010000
  94. #define STATUS_MS_DAT4 0x00008000
  95. #define STATUS_MS_DAT3 0x00004000
  96. #define STATUS_MS_DAT2 0x00002000
  97. #define STATUS_MS_DAT1 0x00001000
  98. #define STATUS_MS_DAT0 0x00000800
  99. #define STATUS_HAS_MEDIA 0x00000400
  100. #define STATUS_FIFO_EMPTY 0x00000200
  101. #define STATUS_FIFO_FULL 0x00000100
  102. #define STATUS_MS_CED 0x00000080
  103. #define STATUS_MS_ERR 0x00000040
  104. #define STATUS_MS_BRQ 0x00000020
  105. #define STATUS_MS_CNK 0x00000001
  106. #define INT_STATUS_TPC_ERR 0x00080000
  107. #define INT_STATUS_CRC_ERR 0x00040000
  108. #define INT_STATUS_TIMER_TO 0x00020000
  109. #define INT_STATUS_HSK_TO 0x00010000
  110. #define INT_STATUS_ANY_ERR 0x00008000
  111. #define INT_STATUS_FIFO_WRDY 0x00000080
  112. #define INT_STATUS_FIFO_RRDY 0x00000040
  113. #define INT_STATUS_MEDIA_OUT 0x00000010
  114. #define INT_STATUS_MEDIA_IN 0x00000008
  115. #define INT_STATUS_DMA_BOUNDARY 0x00000004
  116. #define INT_STATUS_EOTRAN 0x00000002
  117. #define INT_STATUS_EOTPC 0x00000001
  118. #define INT_STATUS_ALL 0x000f801f
  119. #define PAD_OUTPUT_ENABLE_MS 0x0F3F
  120. #define PAD_PU_PD_OFF 0x7FFF0000
  121. #define PAD_PU_PD_ON_MS_SOCK0 0x5f8f0000
  122. #define PAD_PU_PD_ON_MS_SOCK1 0x0f0f0000
  123. #define CLOCK_CONTROL_BY_MMIO 0x00000008
  124. #define CLOCK_CONTROL_40MHZ 0x00000001
  125. #define CLOCK_CONTROL_50MHZ 0x00000002
  126. #define CLOCK_CONTROL_60MHZ 0x00000010
  127. #define CLOCK_CONTROL_62_5MHZ 0x00000004
  128. #define CLOCK_CONTROL_OFF 0x00000000
  129. #define PCI_CTL_CLOCK_DLY_ADDR 0x000000b0
  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. if (host->ifmode == MEMSTICK_SERIAL)
  335. cmd |= TPC_GET_INT;
  336. else
  337. cmd |= TPC_WAIT_INT;
  338. }
  339. data = host->req->data;
  340. if (!no_dma)
  341. host->cmd_flags |= DMA_DATA;
  342. if (host->req->long_data) {
  343. data_len = host->req->sg.length;
  344. } else {
  345. data_len = host->req->data_len;
  346. host->cmd_flags &= ~DMA_DATA;
  347. }
  348. if (data_len <= 8) {
  349. cmd &= ~(TPC_DATA_SEL | 0xf);
  350. host->cmd_flags |= REG_DATA;
  351. cmd |= data_len & 0xf;
  352. host->cmd_flags &= ~DMA_DATA;
  353. }
  354. if (host->cmd_flags & DMA_DATA) {
  355. if (1 != pci_map_sg(host->chip->pdev, &host->req->sg, 1,
  356. host->req->data_dir == READ
  357. ? PCI_DMA_FROMDEVICE
  358. : PCI_DMA_TODEVICE)) {
  359. host->req->error = -ENOMEM;
  360. return host->req->error;
  361. }
  362. data_len = sg_dma_len(&host->req->sg);
  363. writel(sg_dma_address(&host->req->sg),
  364. host->addr + DMA_ADDRESS);
  365. writel(((1 << 16) & BLOCK_COUNT_MASK)
  366. | (data_len & BLOCK_SIZE_MASK),
  367. host->addr + BLOCK);
  368. writel(DMA_CONTROL_ENABLE, host->addr + DMA_CONTROL);
  369. } else if (!(host->cmd_flags & REG_DATA)) {
  370. writel(((1 << 16) & BLOCK_COUNT_MASK)
  371. | (data_len & BLOCK_SIZE_MASK),
  372. host->addr + BLOCK);
  373. t_val = readl(host->addr + INT_STATUS_ENABLE);
  374. t_val |= host->req->data_dir == READ
  375. ? INT_STATUS_FIFO_RRDY
  376. : INT_STATUS_FIFO_WRDY;
  377. writel(t_val, host->addr + INT_STATUS_ENABLE);
  378. writel(t_val, host->addr + INT_SIGNAL_ENABLE);
  379. } else {
  380. cmd &= ~(TPC_DATA_SEL | 0xf);
  381. host->cmd_flags |= REG_DATA;
  382. cmd |= data_len & 0xf;
  383. if (host->req->data_dir == WRITE) {
  384. jmb38x_ms_transfer_data(host);
  385. writel(host->io_word[0], host->addr + TPC_P0);
  386. writel(host->io_word[1], host->addr + TPC_P1);
  387. }
  388. }
  389. mod_timer(&host->timer, jiffies + host->timeout_jiffies);
  390. writel(HOST_CONTROL_LED | readl(host->addr + HOST_CONTROL),
  391. host->addr + HOST_CONTROL);
  392. host->req->error = 0;
  393. writel(cmd, host->addr + TPC);
  394. dev_dbg(&msh->dev, "executing TPC %08x, len %x\n", cmd, data_len);
  395. return 0;
  396. }
  397. static void jmb38x_ms_complete_cmd(struct memstick_host *msh, int last)
  398. {
  399. struct jmb38x_ms_host *host = memstick_priv(msh);
  400. unsigned int t_val = 0;
  401. int rc;
  402. del_timer(&host->timer);
  403. dev_dbg(&msh->dev, "c control %08x\n",
  404. readl(host->addr + HOST_CONTROL));
  405. dev_dbg(&msh->dev, "c status %08x\n",
  406. readl(host->addr + INT_STATUS));
  407. dev_dbg(&msh->dev, "c hstatus %08x\n", readl(host->addr + STATUS));
  408. host->req->int_reg = readl(host->addr + STATUS) & 0xff;
  409. writel(0, host->addr + BLOCK);
  410. writel(0, host->addr + DMA_CONTROL);
  411. if (host->cmd_flags & DMA_DATA) {
  412. pci_unmap_sg(host->chip->pdev, &host->req->sg, 1,
  413. host->req->data_dir == READ
  414. ? PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
  415. } else {
  416. t_val = readl(host->addr + INT_STATUS_ENABLE);
  417. if (host->req->data_dir == READ)
  418. t_val &= ~INT_STATUS_FIFO_RRDY;
  419. else
  420. t_val &= ~INT_STATUS_FIFO_WRDY;
  421. writel(t_val, host->addr + INT_STATUS_ENABLE);
  422. writel(t_val, host->addr + INT_SIGNAL_ENABLE);
  423. }
  424. writel((~HOST_CONTROL_LED) & readl(host->addr + HOST_CONTROL),
  425. host->addr + HOST_CONTROL);
  426. if (!last) {
  427. do {
  428. rc = memstick_next_req(msh, &host->req);
  429. } while (!rc && jmb38x_ms_issue_cmd(msh));
  430. } else {
  431. do {
  432. rc = memstick_next_req(msh, &host->req);
  433. if (!rc)
  434. host->req->error = -ETIME;
  435. } while (!rc);
  436. }
  437. }
  438. static irqreturn_t jmb38x_ms_isr(int irq, void *dev_id)
  439. {
  440. struct memstick_host *msh = dev_id;
  441. struct jmb38x_ms_host *host = memstick_priv(msh);
  442. unsigned int irq_status;
  443. spin_lock(&host->lock);
  444. irq_status = readl(host->addr + INT_STATUS);
  445. dev_dbg(&host->chip->pdev->dev, "irq_status = %08x\n", irq_status);
  446. if (irq_status == 0 || irq_status == (~0)) {
  447. spin_unlock(&host->lock);
  448. return IRQ_NONE;
  449. }
  450. if (host->req) {
  451. if (irq_status & INT_STATUS_ANY_ERR) {
  452. if (irq_status & INT_STATUS_CRC_ERR)
  453. host->req->error = -EILSEQ;
  454. else if (irq_status & INT_STATUS_TPC_ERR) {
  455. dev_dbg(&host->chip->pdev->dev, "TPC_ERR\n");
  456. jmb38x_ms_complete_cmd(msh, 0);
  457. } else
  458. host->req->error = -ETIME;
  459. } else {
  460. if (host->cmd_flags & DMA_DATA) {
  461. if (irq_status & INT_STATUS_EOTRAN)
  462. host->cmd_flags |= FIFO_READY;
  463. } else {
  464. if (irq_status & (INT_STATUS_FIFO_RRDY
  465. | INT_STATUS_FIFO_WRDY))
  466. jmb38x_ms_transfer_data(host);
  467. if (irq_status & INT_STATUS_EOTRAN) {
  468. jmb38x_ms_transfer_data(host);
  469. host->cmd_flags |= FIFO_READY;
  470. }
  471. }
  472. if (irq_status & INT_STATUS_EOTPC) {
  473. host->cmd_flags |= CMD_READY;
  474. if (host->cmd_flags & REG_DATA) {
  475. if (host->req->data_dir == READ) {
  476. host->io_word[0]
  477. = readl(host->addr
  478. + TPC_P0);
  479. host->io_word[1]
  480. = readl(host->addr
  481. + TPC_P1);
  482. host->io_pos = 8;
  483. jmb38x_ms_transfer_data(host);
  484. }
  485. host->cmd_flags |= FIFO_READY;
  486. }
  487. }
  488. }
  489. }
  490. if (irq_status & (INT_STATUS_MEDIA_IN | INT_STATUS_MEDIA_OUT)) {
  491. dev_dbg(&host->chip->pdev->dev, "media changed\n");
  492. memstick_detect_change(msh);
  493. }
  494. writel(irq_status, host->addr + INT_STATUS);
  495. if (host->req
  496. && (((host->cmd_flags & CMD_READY)
  497. && (host->cmd_flags & FIFO_READY))
  498. || host->req->error))
  499. jmb38x_ms_complete_cmd(msh, 0);
  500. spin_unlock(&host->lock);
  501. return IRQ_HANDLED;
  502. }
  503. static void jmb38x_ms_abort(unsigned long data)
  504. {
  505. struct memstick_host *msh = (struct memstick_host *)data;
  506. struct jmb38x_ms_host *host = memstick_priv(msh);
  507. unsigned long flags;
  508. dev_dbg(&host->chip->pdev->dev, "abort\n");
  509. spin_lock_irqsave(&host->lock, flags);
  510. if (host->req) {
  511. host->req->error = -ETIME;
  512. jmb38x_ms_complete_cmd(msh, 0);
  513. }
  514. spin_unlock_irqrestore(&host->lock, flags);
  515. }
  516. static void jmb38x_ms_req_tasklet(unsigned long data)
  517. {
  518. struct memstick_host *msh = (struct memstick_host *)data;
  519. struct jmb38x_ms_host *host = memstick_priv(msh);
  520. unsigned long flags;
  521. int rc;
  522. spin_lock_irqsave(&host->lock, flags);
  523. if (!host->req) {
  524. do {
  525. rc = memstick_next_req(msh, &host->req);
  526. dev_dbg(&host->chip->pdev->dev, "tasklet req %d\n", rc);
  527. } while (!rc && jmb38x_ms_issue_cmd(msh));
  528. }
  529. spin_unlock_irqrestore(&host->lock, flags);
  530. }
  531. static void jmb38x_ms_dummy_submit(struct memstick_host *msh)
  532. {
  533. return;
  534. }
  535. static void jmb38x_ms_submit_req(struct memstick_host *msh)
  536. {
  537. struct jmb38x_ms_host *host = memstick_priv(msh);
  538. tasklet_schedule(&host->notify);
  539. }
  540. static int jmb38x_ms_reset(struct jmb38x_ms_host *host)
  541. {
  542. int cnt;
  543. writel(HOST_CONTROL_RESET_REQ | HOST_CONTROL_CLOCK_EN
  544. | readl(host->addr + HOST_CONTROL),
  545. host->addr + HOST_CONTROL);
  546. mmiowb();
  547. for (cnt = 0; cnt < 20; ++cnt) {
  548. if (!(HOST_CONTROL_RESET_REQ
  549. & readl(host->addr + HOST_CONTROL)))
  550. goto reset_next;
  551. ndelay(20);
  552. }
  553. dev_dbg(&host->chip->pdev->dev, "reset_req timeout\n");
  554. reset_next:
  555. writel(HOST_CONTROL_RESET | HOST_CONTROL_CLOCK_EN
  556. | readl(host->addr + HOST_CONTROL),
  557. host->addr + HOST_CONTROL);
  558. mmiowb();
  559. for (cnt = 0; cnt < 20; ++cnt) {
  560. if (!(HOST_CONTROL_RESET
  561. & readl(host->addr + HOST_CONTROL)))
  562. goto reset_ok;
  563. ndelay(20);
  564. }
  565. dev_dbg(&host->chip->pdev->dev, "reset timeout\n");
  566. return -EIO;
  567. reset_ok:
  568. mmiowb();
  569. writel(INT_STATUS_ALL, host->addr + INT_SIGNAL_ENABLE);
  570. writel(INT_STATUS_ALL, host->addr + INT_STATUS_ENABLE);
  571. return 0;
  572. }
  573. static int jmb38x_ms_set_param(struct memstick_host *msh,
  574. enum memstick_param param,
  575. int value)
  576. {
  577. struct jmb38x_ms_host *host = memstick_priv(msh);
  578. unsigned int host_ctl = readl(host->addr + HOST_CONTROL);
  579. unsigned int clock_ctl = CLOCK_CONTROL_BY_MMIO, clock_delay = 0;
  580. int rc = 0;
  581. switch (param) {
  582. case MEMSTICK_POWER:
  583. if (value == MEMSTICK_POWER_ON) {
  584. rc = jmb38x_ms_reset(host);
  585. if (rc)
  586. return rc;
  587. host_ctl = 7;
  588. host_ctl |= HOST_CONTROL_POWER_EN
  589. | HOST_CONTROL_CLOCK_EN;
  590. writel(host_ctl, host->addr + HOST_CONTROL);
  591. writel(host->id ? PAD_PU_PD_ON_MS_SOCK1
  592. : PAD_PU_PD_ON_MS_SOCK0,
  593. host->addr + PAD_PU_PD);
  594. writel(PAD_OUTPUT_ENABLE_MS,
  595. host->addr + PAD_OUTPUT_ENABLE);
  596. msleep(10);
  597. dev_dbg(&host->chip->pdev->dev, "power on\n");
  598. } else if (value == MEMSTICK_POWER_OFF) {
  599. host_ctl &= ~(HOST_CONTROL_POWER_EN
  600. | HOST_CONTROL_CLOCK_EN);
  601. writel(host_ctl, host->addr + HOST_CONTROL);
  602. writel(0, host->addr + PAD_OUTPUT_ENABLE);
  603. writel(PAD_PU_PD_OFF, host->addr + PAD_PU_PD);
  604. dev_dbg(&host->chip->pdev->dev, "power off\n");
  605. } else
  606. return -EINVAL;
  607. break;
  608. case MEMSTICK_INTERFACE:
  609. dev_dbg(&host->chip->pdev->dev,
  610. "Set Host Interface Mode to %d\n", value);
  611. host_ctl &= ~(HOST_CONTROL_FAST_CLK | HOST_CONTROL_REI |
  612. HOST_CONTROL_REO);
  613. host_ctl |= HOST_CONTROL_TDELAY_EN | HOST_CONTROL_HW_OC_P;
  614. host_ctl &= ~(3 << HOST_CONTROL_IF_SHIFT);
  615. if (value == MEMSTICK_SERIAL) {
  616. host_ctl |= HOST_CONTROL_IF_SERIAL
  617. << HOST_CONTROL_IF_SHIFT;
  618. host_ctl |= HOST_CONTROL_REI;
  619. clock_ctl |= CLOCK_CONTROL_40MHZ;
  620. clock_delay = 0;
  621. } else if (value == MEMSTICK_PAR4) {
  622. host_ctl |= HOST_CONTROL_FAST_CLK;
  623. host_ctl |= HOST_CONTROL_IF_PAR4
  624. << HOST_CONTROL_IF_SHIFT;
  625. host_ctl |= HOST_CONTROL_REO;
  626. clock_ctl |= CLOCK_CONTROL_40MHZ;
  627. clock_delay = 4;
  628. } else if (value == MEMSTICK_PAR8) {
  629. host_ctl |= HOST_CONTROL_FAST_CLK;
  630. host_ctl |= HOST_CONTROL_IF_PAR8
  631. << HOST_CONTROL_IF_SHIFT;
  632. clock_ctl |= CLOCK_CONTROL_50MHZ;
  633. clock_delay = 0;
  634. } else
  635. return -EINVAL;
  636. writel(host_ctl, host->addr + HOST_CONTROL);
  637. writel(CLOCK_CONTROL_OFF, host->addr + CLOCK_CONTROL);
  638. writel(clock_ctl, host->addr + CLOCK_CONTROL);
  639. pci_write_config_byte(host->chip->pdev,
  640. PCI_CTL_CLOCK_DLY_ADDR + 1,
  641. clock_delay);
  642. host->ifmode = value;
  643. break;
  644. };
  645. return 0;
  646. }
  647. #define PCI_PMOS0_CONTROL 0xae
  648. #define PMOS0_ENABLE 0x01
  649. #define PMOS0_OVERCURRENT_LEVEL_2_4V 0x06
  650. #define PMOS0_EN_OVERCURRENT_DEBOUNCE 0x40
  651. #define PMOS0_SW_LED_POLARITY_ENABLE 0x80
  652. #define PMOS0_ACTIVE_BITS (PMOS0_ENABLE | PMOS0_EN_OVERCURRENT_DEBOUNCE | \
  653. PMOS0_OVERCURRENT_LEVEL_2_4V)
  654. #define PCI_PMOS1_CONTROL 0xbd
  655. #define PMOS1_ACTIVE_BITS 0x4a
  656. #define PCI_CLOCK_CTL 0xb9
  657. static int jmb38x_ms_pmos(struct pci_dev *pdev, int flag)
  658. {
  659. unsigned char val;
  660. pci_read_config_byte(pdev, PCI_PMOS0_CONTROL, &val);
  661. if (flag)
  662. val |= PMOS0_ACTIVE_BITS;
  663. else
  664. val &= ~PMOS0_ACTIVE_BITS;
  665. pci_write_config_byte(pdev, PCI_PMOS0_CONTROL, val);
  666. dev_dbg(&pdev->dev, "JMB38x: set PMOS0 val 0x%x\n", val);
  667. if (pci_resource_flags(pdev, 1)) {
  668. pci_read_config_byte(pdev, PCI_PMOS1_CONTROL, &val);
  669. if (flag)
  670. val |= PMOS1_ACTIVE_BITS;
  671. else
  672. val &= ~PMOS1_ACTIVE_BITS;
  673. pci_write_config_byte(pdev, PCI_PMOS1_CONTROL, val);
  674. dev_dbg(&pdev->dev, "JMB38x: set PMOS1 val 0x%x\n", val);
  675. }
  676. pci_read_config_byte(pdev, PCI_CLOCK_CTL, &val);
  677. pci_write_config_byte(pdev, PCI_CLOCK_CTL, val & ~0x0f);
  678. pci_write_config_byte(pdev, PCI_CLOCK_CTL, val | 0x01);
  679. dev_dbg(&pdev->dev, "Clock Control by PCI config is disabled!\n");
  680. return 0;
  681. }
  682. #ifdef CONFIG_PM
  683. static int jmb38x_ms_suspend(struct pci_dev *dev, pm_message_t state)
  684. {
  685. struct jmb38x_ms *jm = pci_get_drvdata(dev);
  686. int cnt;
  687. for (cnt = 0; cnt < jm->host_cnt; ++cnt) {
  688. if (!jm->hosts[cnt])
  689. break;
  690. memstick_suspend_host(jm->hosts[cnt]);
  691. }
  692. pci_save_state(dev);
  693. pci_enable_wake(dev, pci_choose_state(dev, state), 0);
  694. pci_disable_device(dev);
  695. pci_set_power_state(dev, pci_choose_state(dev, state));
  696. return 0;
  697. }
  698. static int jmb38x_ms_resume(struct pci_dev *dev)
  699. {
  700. struct jmb38x_ms *jm = pci_get_drvdata(dev);
  701. int rc;
  702. pci_set_power_state(dev, PCI_D0);
  703. pci_restore_state(dev);
  704. rc = pci_enable_device(dev);
  705. if (rc)
  706. return rc;
  707. pci_set_master(dev);
  708. jmb38x_ms_pmos(dev, 1);
  709. for (rc = 0; rc < jm->host_cnt; ++rc) {
  710. if (!jm->hosts[rc])
  711. break;
  712. memstick_resume_host(jm->hosts[rc]);
  713. memstick_detect_change(jm->hosts[rc]);
  714. }
  715. return 0;
  716. }
  717. #else
  718. #define jmb38x_ms_suspend NULL
  719. #define jmb38x_ms_resume NULL
  720. #endif /* CONFIG_PM */
  721. static int jmb38x_ms_count_slots(struct pci_dev *pdev)
  722. {
  723. int cnt, rc = 0;
  724. for (cnt = 0; cnt < PCI_ROM_RESOURCE; ++cnt) {
  725. if (!(IORESOURCE_MEM & pci_resource_flags(pdev, cnt)))
  726. break;
  727. if (256 != pci_resource_len(pdev, cnt))
  728. break;
  729. ++rc;
  730. }
  731. return rc;
  732. }
  733. static struct memstick_host *jmb38x_ms_alloc_host(struct jmb38x_ms *jm, int cnt)
  734. {
  735. struct memstick_host *msh;
  736. struct jmb38x_ms_host *host;
  737. msh = memstick_alloc_host(sizeof(struct jmb38x_ms_host),
  738. &jm->pdev->dev);
  739. if (!msh)
  740. return NULL;
  741. host = memstick_priv(msh);
  742. host->chip = jm;
  743. host->addr = ioremap(pci_resource_start(jm->pdev, cnt),
  744. pci_resource_len(jm->pdev, cnt));
  745. if (!host->addr)
  746. goto err_out_free;
  747. spin_lock_init(&host->lock);
  748. host->id = cnt;
  749. snprintf(host->host_id, sizeof(host->host_id), DRIVER_NAME ":slot%d",
  750. host->id);
  751. host->irq = jm->pdev->irq;
  752. host->timeout_jiffies = msecs_to_jiffies(1000);
  753. tasklet_init(&host->notify, jmb38x_ms_req_tasklet, (unsigned long)msh);
  754. msh->request = jmb38x_ms_submit_req;
  755. msh->set_param = jmb38x_ms_set_param;
  756. msh->caps = MEMSTICK_CAP_PAR4 | MEMSTICK_CAP_PAR8;
  757. setup_timer(&host->timer, jmb38x_ms_abort, (unsigned long)msh);
  758. if (!request_irq(host->irq, jmb38x_ms_isr, IRQF_SHARED, host->host_id,
  759. msh))
  760. return msh;
  761. iounmap(host->addr);
  762. err_out_free:
  763. kfree(msh);
  764. return NULL;
  765. }
  766. static void jmb38x_ms_free_host(struct memstick_host *msh)
  767. {
  768. struct jmb38x_ms_host *host = memstick_priv(msh);
  769. free_irq(host->irq, msh);
  770. iounmap(host->addr);
  771. memstick_free_host(msh);
  772. }
  773. static int jmb38x_ms_probe(struct pci_dev *pdev,
  774. const struct pci_device_id *dev_id)
  775. {
  776. struct jmb38x_ms *jm;
  777. int pci_dev_busy = 0;
  778. int rc, cnt;
  779. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  780. if (rc)
  781. return rc;
  782. rc = pci_enable_device(pdev);
  783. if (rc)
  784. return rc;
  785. pci_set_master(pdev);
  786. rc = pci_request_regions(pdev, DRIVER_NAME);
  787. if (rc) {
  788. pci_dev_busy = 1;
  789. goto err_out;
  790. }
  791. jmb38x_ms_pmos(pdev, 1);
  792. cnt = jmb38x_ms_count_slots(pdev);
  793. if (!cnt) {
  794. rc = -ENODEV;
  795. pci_dev_busy = 1;
  796. goto err_out;
  797. }
  798. jm = kzalloc(sizeof(struct jmb38x_ms)
  799. + cnt * sizeof(struct memstick_host *), GFP_KERNEL);
  800. if (!jm) {
  801. rc = -ENOMEM;
  802. goto err_out_int;
  803. }
  804. jm->pdev = pdev;
  805. jm->host_cnt = cnt;
  806. pci_set_drvdata(pdev, jm);
  807. for (cnt = 0; cnt < jm->host_cnt; ++cnt) {
  808. jm->hosts[cnt] = jmb38x_ms_alloc_host(jm, cnt);
  809. if (!jm->hosts[cnt])
  810. break;
  811. rc = memstick_add_host(jm->hosts[cnt]);
  812. if (rc) {
  813. jmb38x_ms_free_host(jm->hosts[cnt]);
  814. jm->hosts[cnt] = NULL;
  815. break;
  816. }
  817. }
  818. if (cnt)
  819. return 0;
  820. rc = -ENODEV;
  821. pci_set_drvdata(pdev, NULL);
  822. kfree(jm);
  823. err_out_int:
  824. pci_release_regions(pdev);
  825. err_out:
  826. if (!pci_dev_busy)
  827. pci_disable_device(pdev);
  828. return rc;
  829. }
  830. static void jmb38x_ms_remove(struct pci_dev *dev)
  831. {
  832. struct jmb38x_ms *jm = pci_get_drvdata(dev);
  833. struct jmb38x_ms_host *host;
  834. int cnt;
  835. unsigned long flags;
  836. for (cnt = 0; cnt < jm->host_cnt; ++cnt) {
  837. if (!jm->hosts[cnt])
  838. break;
  839. host = memstick_priv(jm->hosts[cnt]);
  840. jm->hosts[cnt]->request = jmb38x_ms_dummy_submit;
  841. tasklet_kill(&host->notify);
  842. writel(0, host->addr + INT_SIGNAL_ENABLE);
  843. writel(0, host->addr + INT_STATUS_ENABLE);
  844. mmiowb();
  845. dev_dbg(&jm->pdev->dev, "interrupts off\n");
  846. spin_lock_irqsave(&host->lock, flags);
  847. if (host->req) {
  848. host->req->error = -ETIME;
  849. jmb38x_ms_complete_cmd(jm->hosts[cnt], 1);
  850. }
  851. spin_unlock_irqrestore(&host->lock, flags);
  852. memstick_remove_host(jm->hosts[cnt]);
  853. dev_dbg(&jm->pdev->dev, "host removed\n");
  854. jmb38x_ms_free_host(jm->hosts[cnt]);
  855. }
  856. jmb38x_ms_pmos(dev, 0);
  857. pci_set_drvdata(dev, NULL);
  858. pci_release_regions(dev);
  859. pci_disable_device(dev);
  860. kfree(jm);
  861. }
  862. static struct pci_device_id jmb38x_ms_id_tbl [] = {
  863. { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMB38X_MS) },
  864. { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMB385_MS) },
  865. { PCI_VDEVICE(JMICRON, PCI_DEVICE_ID_JMICRON_JMB390_MS) },
  866. { }
  867. };
  868. static struct pci_driver jmb38x_ms_driver = {
  869. .name = DRIVER_NAME,
  870. .id_table = jmb38x_ms_id_tbl,
  871. .probe = jmb38x_ms_probe,
  872. .remove = jmb38x_ms_remove,
  873. .suspend = jmb38x_ms_suspend,
  874. .resume = jmb38x_ms_resume
  875. };
  876. static int __init jmb38x_ms_init(void)
  877. {
  878. return pci_register_driver(&jmb38x_ms_driver);
  879. }
  880. static void __exit jmb38x_ms_exit(void)
  881. {
  882. pci_unregister_driver(&jmb38x_ms_driver);
  883. }
  884. MODULE_AUTHOR("Alex Dubov");
  885. MODULE_DESCRIPTION("JMicron jmb38x MemoryStick driver");
  886. MODULE_LICENSE("GPL");
  887. MODULE_DEVICE_TABLE(pci, jmb38x_ms_id_tbl);
  888. module_init(jmb38x_ms_init);
  889. module_exit(jmb38x_ms_exit);