jmb38x_ms.c 25 KB

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