r592.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. * Copyright (C) 2010 - Maxim Levitsky
  3. * driver for Ricoh memstick readers
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/freezer.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/pci.h>
  15. #include <linux/pci_ids.h>
  16. #include <linux/delay.h>
  17. #include <linux/slab.h>
  18. #include <linux/kthread.h>
  19. #include <linux/sched.h>
  20. #include <linux/highmem.h>
  21. #include <asm/byteorder.h>
  22. #include <linux/swab.h>
  23. #include "r592.h"
  24. static bool r592_enable_dma = 1;
  25. static int debug;
  26. static const char *tpc_names[] = {
  27. "MS_TPC_READ_MG_STATUS",
  28. "MS_TPC_READ_LONG_DATA",
  29. "MS_TPC_READ_SHORT_DATA",
  30. "MS_TPC_READ_REG",
  31. "MS_TPC_READ_QUAD_DATA",
  32. "INVALID",
  33. "MS_TPC_GET_INT",
  34. "MS_TPC_SET_RW_REG_ADRS",
  35. "MS_TPC_EX_SET_CMD",
  36. "MS_TPC_WRITE_QUAD_DATA",
  37. "MS_TPC_WRITE_REG",
  38. "MS_TPC_WRITE_SHORT_DATA",
  39. "MS_TPC_WRITE_LONG_DATA",
  40. "MS_TPC_SET_CMD",
  41. };
  42. /**
  43. * memstick_debug_get_tpc_name - debug helper that returns string for
  44. * a TPC number
  45. */
  46. const char *memstick_debug_get_tpc_name(int tpc)
  47. {
  48. return tpc_names[tpc-1];
  49. }
  50. EXPORT_SYMBOL(memstick_debug_get_tpc_name);
  51. /* Read a register*/
  52. static inline u32 r592_read_reg(struct r592_device *dev, int address)
  53. {
  54. u32 value = readl(dev->mmio + address);
  55. dbg_reg("reg #%02d == 0x%08x", address, value);
  56. return value;
  57. }
  58. /* Write a register */
  59. static inline void r592_write_reg(struct r592_device *dev,
  60. int address, u32 value)
  61. {
  62. dbg_reg("reg #%02d <- 0x%08x", address, value);
  63. writel(value, dev->mmio + address);
  64. }
  65. /* Reads a big endian DWORD register */
  66. static inline u32 r592_read_reg_raw_be(struct r592_device *dev, int address)
  67. {
  68. u32 value = __raw_readl(dev->mmio + address);
  69. dbg_reg("reg #%02d == 0x%08x", address, value);
  70. return be32_to_cpu(value);
  71. }
  72. /* Writes a big endian DWORD register */
  73. static inline void r592_write_reg_raw_be(struct r592_device *dev,
  74. int address, u32 value)
  75. {
  76. dbg_reg("reg #%02d <- 0x%08x", address, value);
  77. __raw_writel(cpu_to_be32(value), dev->mmio + address);
  78. }
  79. /* Set specific bits in a register (little endian) */
  80. static inline void r592_set_reg_mask(struct r592_device *dev,
  81. int address, u32 mask)
  82. {
  83. u32 reg = readl(dev->mmio + address);
  84. dbg_reg("reg #%02d |= 0x%08x (old =0x%08x)", address, mask, reg);
  85. writel(reg | mask , dev->mmio + address);
  86. }
  87. /* Clear specific bits in a register (little endian) */
  88. static inline void r592_clear_reg_mask(struct r592_device *dev,
  89. int address, u32 mask)
  90. {
  91. u32 reg = readl(dev->mmio + address);
  92. dbg_reg("reg #%02d &= 0x%08x (old = 0x%08x, mask = 0x%08x)",
  93. address, ~mask, reg, mask);
  94. writel(reg & ~mask, dev->mmio + address);
  95. }
  96. /* Wait for status bits while checking for errors */
  97. static int r592_wait_status(struct r592_device *dev, u32 mask, u32 wanted_mask)
  98. {
  99. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  100. u32 reg = r592_read_reg(dev, R592_STATUS);
  101. if ((reg & mask) == wanted_mask)
  102. return 0;
  103. while (time_before(jiffies, timeout)) {
  104. reg = r592_read_reg(dev, R592_STATUS);
  105. if ((reg & mask) == wanted_mask)
  106. return 0;
  107. if (reg & (R592_STATUS_SEND_ERR | R592_STATUS_RECV_ERR))
  108. return -EIO;
  109. cpu_relax();
  110. }
  111. return -ETIME;
  112. }
  113. /* Enable/disable device */
  114. static int r592_enable_device(struct r592_device *dev, bool enable)
  115. {
  116. dbg("%sabling the device", enable ? "en" : "dis");
  117. if (enable) {
  118. /* Power up the card */
  119. r592_write_reg(dev, R592_POWER, R592_POWER_0 | R592_POWER_1);
  120. /* Perform a reset */
  121. r592_set_reg_mask(dev, R592_IO, R592_IO_RESET);
  122. msleep(100);
  123. } else
  124. /* Power down the card */
  125. r592_write_reg(dev, R592_POWER, 0);
  126. return 0;
  127. }
  128. /* Set serial/parallel mode */
  129. static int r592_set_mode(struct r592_device *dev, bool parallel_mode)
  130. {
  131. if (!parallel_mode) {
  132. dbg("switching to serial mode");
  133. /* Set serial mode */
  134. r592_write_reg(dev, R592_IO_MODE, R592_IO_MODE_SERIAL);
  135. r592_clear_reg_mask(dev, R592_POWER, R592_POWER_20);
  136. } else {
  137. dbg("switching to parallel mode");
  138. /* This setting should be set _before_ switch TPC */
  139. r592_set_reg_mask(dev, R592_POWER, R592_POWER_20);
  140. r592_clear_reg_mask(dev, R592_IO,
  141. R592_IO_SERIAL1 | R592_IO_SERIAL2);
  142. /* Set the parallel mode now */
  143. r592_write_reg(dev, R592_IO_MODE, R592_IO_MODE_PARALLEL);
  144. }
  145. dev->parallel_mode = parallel_mode;
  146. return 0;
  147. }
  148. /* Perform a controller reset without powering down the card */
  149. static void r592_host_reset(struct r592_device *dev)
  150. {
  151. r592_set_reg_mask(dev, R592_IO, R592_IO_RESET);
  152. msleep(100);
  153. r592_set_mode(dev, dev->parallel_mode);
  154. }
  155. #ifdef CONFIG_PM_SLEEP
  156. /* Disable all hardware interrupts */
  157. static void r592_clear_interrupts(struct r592_device *dev)
  158. {
  159. /* Disable & ACK all interrupts */
  160. r592_clear_reg_mask(dev, R592_REG_MSC, IRQ_ALL_ACK_MASK);
  161. r592_clear_reg_mask(dev, R592_REG_MSC, IRQ_ALL_EN_MASK);
  162. }
  163. #endif
  164. /* Tests if there is an CRC error */
  165. static int r592_test_io_error(struct r592_device *dev)
  166. {
  167. if (!(r592_read_reg(dev, R592_STATUS) &
  168. (R592_STATUS_SEND_ERR | R592_STATUS_RECV_ERR)))
  169. return 0;
  170. return -EIO;
  171. }
  172. /* Ensure that FIFO is ready for use */
  173. static int r592_test_fifo_empty(struct r592_device *dev)
  174. {
  175. if (r592_read_reg(dev, R592_REG_MSC) & R592_REG_MSC_FIFO_EMPTY)
  176. return 0;
  177. dbg("FIFO not ready, trying to reset the device");
  178. r592_host_reset(dev);
  179. if (r592_read_reg(dev, R592_REG_MSC) & R592_REG_MSC_FIFO_EMPTY)
  180. return 0;
  181. message("FIFO still not ready, giving up");
  182. return -EIO;
  183. }
  184. /* Activates the DMA transfer from to FIFO */
  185. static void r592_start_dma(struct r592_device *dev, bool is_write)
  186. {
  187. unsigned long flags;
  188. u32 reg;
  189. spin_lock_irqsave(&dev->irq_lock, flags);
  190. /* Ack interrupts (just in case) + enable them */
  191. r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_ACK_MASK);
  192. r592_set_reg_mask(dev, R592_REG_MSC, DMA_IRQ_EN_MASK);
  193. /* Set DMA address */
  194. r592_write_reg(dev, R592_FIFO_DMA, sg_dma_address(&dev->req->sg));
  195. /* Enable the DMA */
  196. reg = r592_read_reg(dev, R592_FIFO_DMA_SETTINGS);
  197. reg |= R592_FIFO_DMA_SETTINGS_EN;
  198. if (!is_write)
  199. reg |= R592_FIFO_DMA_SETTINGS_DIR;
  200. else
  201. reg &= ~R592_FIFO_DMA_SETTINGS_DIR;
  202. r592_write_reg(dev, R592_FIFO_DMA_SETTINGS, reg);
  203. spin_unlock_irqrestore(&dev->irq_lock, flags);
  204. }
  205. /* Cleanups DMA related settings */
  206. static void r592_stop_dma(struct r592_device *dev, int error)
  207. {
  208. r592_clear_reg_mask(dev, R592_FIFO_DMA_SETTINGS,
  209. R592_FIFO_DMA_SETTINGS_EN);
  210. /* This is only a precation */
  211. r592_write_reg(dev, R592_FIFO_DMA,
  212. dev->dummy_dma_page_physical_address);
  213. r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_EN_MASK);
  214. r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_ACK_MASK);
  215. dev->dma_error = error;
  216. }
  217. /* Test if hardware supports DMA */
  218. static void r592_check_dma(struct r592_device *dev)
  219. {
  220. dev->dma_capable = r592_enable_dma &&
  221. (r592_read_reg(dev, R592_FIFO_DMA_SETTINGS) &
  222. R592_FIFO_DMA_SETTINGS_CAP);
  223. }
  224. /* Transfers fifo contents in/out using DMA */
  225. static int r592_transfer_fifo_dma(struct r592_device *dev)
  226. {
  227. int len, sg_count;
  228. bool is_write;
  229. if (!dev->dma_capable || !dev->req->long_data)
  230. return -EINVAL;
  231. len = dev->req->sg.length;
  232. is_write = dev->req->data_dir == WRITE;
  233. if (len != R592_LFIFO_SIZE)
  234. return -EINVAL;
  235. dbg_verbose("doing dma transfer");
  236. dev->dma_error = 0;
  237. reinit_completion(&dev->dma_done);
  238. /* TODO: hidden assumption about nenth beeing always 1 */
  239. sg_count = dma_map_sg(&dev->pci_dev->dev, &dev->req->sg, 1, is_write ?
  240. PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
  241. if (sg_count != 1 || sg_dma_len(&dev->req->sg) < R592_LFIFO_SIZE) {
  242. message("problem in dma_map_sg");
  243. return -EIO;
  244. }
  245. r592_start_dma(dev, is_write);
  246. /* Wait for DMA completion */
  247. if (!wait_for_completion_timeout(
  248. &dev->dma_done, msecs_to_jiffies(1000))) {
  249. message("DMA timeout");
  250. r592_stop_dma(dev, -ETIMEDOUT);
  251. }
  252. dma_unmap_sg(&dev->pci_dev->dev, &dev->req->sg, 1, is_write ?
  253. PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
  254. return dev->dma_error;
  255. }
  256. /*
  257. * Writes the FIFO in 4 byte chunks.
  258. * If length isn't 4 byte aligned, rest of the data if put to a fifo
  259. * to be written later
  260. * Use r592_flush_fifo_write to flush that fifo when writing for the
  261. * last time
  262. */
  263. static void r592_write_fifo_pio(struct r592_device *dev,
  264. unsigned char *buffer, int len)
  265. {
  266. /* flush spill from former write */
  267. if (!kfifo_is_empty(&dev->pio_fifo)) {
  268. u8 tmp[4] = {0};
  269. int copy_len = kfifo_in(&dev->pio_fifo, buffer, len);
  270. if (!kfifo_is_full(&dev->pio_fifo))
  271. return;
  272. len -= copy_len;
  273. buffer += copy_len;
  274. copy_len = kfifo_out(&dev->pio_fifo, tmp, 4);
  275. WARN_ON(copy_len != 4);
  276. r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)tmp);
  277. }
  278. WARN_ON(!kfifo_is_empty(&dev->pio_fifo));
  279. /* write full dwords */
  280. while (len >= 4) {
  281. r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)buffer);
  282. buffer += 4;
  283. len -= 4;
  284. }
  285. /* put remaining bytes to the spill */
  286. if (len)
  287. kfifo_in(&dev->pio_fifo, buffer, len);
  288. }
  289. /* Flushes the temporary FIFO used to make aligned DWORD writes */
  290. static void r592_flush_fifo_write(struct r592_device *dev)
  291. {
  292. u8 buffer[4] = { 0 };
  293. int len;
  294. if (kfifo_is_empty(&dev->pio_fifo))
  295. return;
  296. len = kfifo_out(&dev->pio_fifo, buffer, 4);
  297. r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)buffer);
  298. }
  299. /*
  300. * Read a fifo in 4 bytes chunks.
  301. * If input doesn't fit the buffer, it places bytes of last dword in spill
  302. * buffer, so that they don't get lost on last read, just throw these away.
  303. */
  304. static void r592_read_fifo_pio(struct r592_device *dev,
  305. unsigned char *buffer, int len)
  306. {
  307. u8 tmp[4];
  308. /* Read from last spill */
  309. if (!kfifo_is_empty(&dev->pio_fifo)) {
  310. int bytes_copied =
  311. kfifo_out(&dev->pio_fifo, buffer, min(4, len));
  312. buffer += bytes_copied;
  313. len -= bytes_copied;
  314. if (!kfifo_is_empty(&dev->pio_fifo))
  315. return;
  316. }
  317. /* Reads dwords from FIFO */
  318. while (len >= 4) {
  319. *(u32 *)buffer = r592_read_reg_raw_be(dev, R592_FIFO_PIO);
  320. buffer += 4;
  321. len -= 4;
  322. }
  323. if (len) {
  324. *(u32 *)tmp = r592_read_reg_raw_be(dev, R592_FIFO_PIO);
  325. kfifo_in(&dev->pio_fifo, tmp, 4);
  326. len -= kfifo_out(&dev->pio_fifo, buffer, len);
  327. }
  328. WARN_ON(len);
  329. return;
  330. }
  331. /* Transfers actual data using PIO. */
  332. static int r592_transfer_fifo_pio(struct r592_device *dev)
  333. {
  334. unsigned long flags;
  335. bool is_write = dev->req->tpc >= MS_TPC_SET_RW_REG_ADRS;
  336. struct sg_mapping_iter miter;
  337. kfifo_reset(&dev->pio_fifo);
  338. if (!dev->req->long_data) {
  339. if (is_write) {
  340. r592_write_fifo_pio(dev, dev->req->data,
  341. dev->req->data_len);
  342. r592_flush_fifo_write(dev);
  343. } else
  344. r592_read_fifo_pio(dev, dev->req->data,
  345. dev->req->data_len);
  346. return 0;
  347. }
  348. local_irq_save(flags);
  349. sg_miter_start(&miter, &dev->req->sg, 1, SG_MITER_ATOMIC |
  350. (is_write ? SG_MITER_FROM_SG : SG_MITER_TO_SG));
  351. /* Do the transfer fifo<->memory*/
  352. while (sg_miter_next(&miter))
  353. if (is_write)
  354. r592_write_fifo_pio(dev, miter.addr, miter.length);
  355. else
  356. r592_read_fifo_pio(dev, miter.addr, miter.length);
  357. /* Write last few non aligned bytes*/
  358. if (is_write)
  359. r592_flush_fifo_write(dev);
  360. sg_miter_stop(&miter);
  361. local_irq_restore(flags);
  362. return 0;
  363. }
  364. /* Executes one TPC (data is read/written from small or large fifo) */
  365. static void r592_execute_tpc(struct r592_device *dev)
  366. {
  367. bool is_write;
  368. int len, error;
  369. u32 status, reg;
  370. if (!dev->req) {
  371. message("BUG: tpc execution without request!");
  372. return;
  373. }
  374. is_write = dev->req->tpc >= MS_TPC_SET_RW_REG_ADRS;
  375. len = dev->req->long_data ?
  376. dev->req->sg.length : dev->req->data_len;
  377. /* Ensure that FIFO can hold the input data */
  378. if (len > R592_LFIFO_SIZE) {
  379. message("IO: hardware doesn't support TPCs longer that 512");
  380. error = -ENOSYS;
  381. goto out;
  382. }
  383. if (!(r592_read_reg(dev, R592_REG_MSC) & R592_REG_MSC_PRSNT)) {
  384. dbg("IO: refusing to send TPC because card is absent");
  385. error = -ENODEV;
  386. goto out;
  387. }
  388. dbg("IO: executing %s LEN=%d",
  389. memstick_debug_get_tpc_name(dev->req->tpc), len);
  390. /* Set IO direction */
  391. if (is_write)
  392. r592_set_reg_mask(dev, R592_IO, R592_IO_DIRECTION);
  393. else
  394. r592_clear_reg_mask(dev, R592_IO, R592_IO_DIRECTION);
  395. error = r592_test_fifo_empty(dev);
  396. if (error)
  397. goto out;
  398. /* Transfer write data */
  399. if (is_write) {
  400. error = r592_transfer_fifo_dma(dev);
  401. if (error == -EINVAL)
  402. error = r592_transfer_fifo_pio(dev);
  403. }
  404. if (error)
  405. goto out;
  406. /* Trigger the TPC */
  407. reg = (len << R592_TPC_EXEC_LEN_SHIFT) |
  408. (dev->req->tpc << R592_TPC_EXEC_TPC_SHIFT) |
  409. R592_TPC_EXEC_BIG_FIFO;
  410. r592_write_reg(dev, R592_TPC_EXEC, reg);
  411. /* Wait for TPC completion */
  412. status = R592_STATUS_RDY;
  413. if (dev->req->need_card_int)
  414. status |= R592_STATUS_CED;
  415. error = r592_wait_status(dev, status, status);
  416. if (error) {
  417. message("card didn't respond");
  418. goto out;
  419. }
  420. /* Test IO errors */
  421. error = r592_test_io_error(dev);
  422. if (error) {
  423. dbg("IO error");
  424. goto out;
  425. }
  426. /* Read data from FIFO */
  427. if (!is_write) {
  428. error = r592_transfer_fifo_dma(dev);
  429. if (error == -EINVAL)
  430. error = r592_transfer_fifo_pio(dev);
  431. }
  432. /* read INT reg. This can be shortened with shifts, but that way
  433. its more readable */
  434. if (dev->parallel_mode && dev->req->need_card_int) {
  435. dev->req->int_reg = 0;
  436. status = r592_read_reg(dev, R592_STATUS);
  437. if (status & R592_STATUS_P_CMDNACK)
  438. dev->req->int_reg |= MEMSTICK_INT_CMDNAK;
  439. if (status & R592_STATUS_P_BREQ)
  440. dev->req->int_reg |= MEMSTICK_INT_BREQ;
  441. if (status & R592_STATUS_P_INTERR)
  442. dev->req->int_reg |= MEMSTICK_INT_ERR;
  443. if (status & R592_STATUS_P_CED)
  444. dev->req->int_reg |= MEMSTICK_INT_CED;
  445. }
  446. if (error)
  447. dbg("FIFO read error");
  448. out:
  449. dev->req->error = error;
  450. r592_clear_reg_mask(dev, R592_REG_MSC, R592_REG_MSC_LED);
  451. return;
  452. }
  453. /* Main request processing thread */
  454. static int r592_process_thread(void *data)
  455. {
  456. int error;
  457. struct r592_device *dev = (struct r592_device *)data;
  458. unsigned long flags;
  459. while (!kthread_should_stop()) {
  460. spin_lock_irqsave(&dev->io_thread_lock, flags);
  461. set_current_state(TASK_INTERRUPTIBLE);
  462. error = memstick_next_req(dev->host, &dev->req);
  463. spin_unlock_irqrestore(&dev->io_thread_lock, flags);
  464. if (error) {
  465. if (error == -ENXIO || error == -EAGAIN) {
  466. dbg_verbose("IO: done IO, sleeping");
  467. } else {
  468. dbg("IO: unknown error from "
  469. "memstick_next_req %d", error);
  470. }
  471. if (kthread_should_stop())
  472. set_current_state(TASK_RUNNING);
  473. schedule();
  474. } else {
  475. set_current_state(TASK_RUNNING);
  476. r592_execute_tpc(dev);
  477. }
  478. }
  479. return 0;
  480. }
  481. /* Reprogram chip to detect change in card state */
  482. /* eg, if card is detected, arm it to detect removal, and vice versa */
  483. static void r592_update_card_detect(struct r592_device *dev)
  484. {
  485. u32 reg = r592_read_reg(dev, R592_REG_MSC);
  486. bool card_detected = reg & R592_REG_MSC_PRSNT;
  487. dbg("update card detect. card state: %s", card_detected ?
  488. "present" : "absent");
  489. reg &= ~((R592_REG_MSC_IRQ_REMOVE | R592_REG_MSC_IRQ_INSERT) << 16);
  490. if (card_detected)
  491. reg |= (R592_REG_MSC_IRQ_REMOVE << 16);
  492. else
  493. reg |= (R592_REG_MSC_IRQ_INSERT << 16);
  494. r592_write_reg(dev, R592_REG_MSC, reg);
  495. }
  496. /* Timer routine that fires 1 second after last card detection event, */
  497. static void r592_detect_timer(long unsigned int data)
  498. {
  499. struct r592_device *dev = (struct r592_device *)data;
  500. r592_update_card_detect(dev);
  501. memstick_detect_change(dev->host);
  502. }
  503. /* Interrupt handler */
  504. static irqreturn_t r592_irq(int irq, void *data)
  505. {
  506. struct r592_device *dev = (struct r592_device *)data;
  507. irqreturn_t ret = IRQ_NONE;
  508. u32 reg;
  509. u16 irq_enable, irq_status;
  510. unsigned long flags;
  511. int error;
  512. spin_lock_irqsave(&dev->irq_lock, flags);
  513. reg = r592_read_reg(dev, R592_REG_MSC);
  514. irq_enable = reg >> 16;
  515. irq_status = reg & 0xFFFF;
  516. /* Ack the interrupts */
  517. reg &= ~irq_status;
  518. r592_write_reg(dev, R592_REG_MSC, reg);
  519. /* Get the IRQ status minus bits that aren't enabled */
  520. irq_status &= (irq_enable);
  521. /* Due to limitation of memstick core, we don't look at bits that
  522. indicate that card was removed/inserted and/or present */
  523. if (irq_status & (R592_REG_MSC_IRQ_INSERT | R592_REG_MSC_IRQ_REMOVE)) {
  524. bool card_was_added = irq_status & R592_REG_MSC_IRQ_INSERT;
  525. ret = IRQ_HANDLED;
  526. message("IRQ: card %s", card_was_added ? "added" : "removed");
  527. mod_timer(&dev->detect_timer,
  528. jiffies + msecs_to_jiffies(card_was_added ? 500 : 50));
  529. }
  530. if (irq_status &
  531. (R592_REG_MSC_FIFO_DMA_DONE | R592_REG_MSC_FIFO_DMA_ERR)) {
  532. ret = IRQ_HANDLED;
  533. if (irq_status & R592_REG_MSC_FIFO_DMA_ERR) {
  534. message("IRQ: DMA error");
  535. error = -EIO;
  536. } else {
  537. dbg_verbose("IRQ: dma done");
  538. error = 0;
  539. }
  540. r592_stop_dma(dev, error);
  541. complete(&dev->dma_done);
  542. }
  543. spin_unlock_irqrestore(&dev->irq_lock, flags);
  544. return ret;
  545. }
  546. /* External inteface: set settings */
  547. static int r592_set_param(struct memstick_host *host,
  548. enum memstick_param param, int value)
  549. {
  550. struct r592_device *dev = memstick_priv(host);
  551. switch (param) {
  552. case MEMSTICK_POWER:
  553. switch (value) {
  554. case MEMSTICK_POWER_ON:
  555. return r592_enable_device(dev, true);
  556. case MEMSTICK_POWER_OFF:
  557. return r592_enable_device(dev, false);
  558. default:
  559. return -EINVAL;
  560. }
  561. case MEMSTICK_INTERFACE:
  562. switch (value) {
  563. case MEMSTICK_SERIAL:
  564. return r592_set_mode(dev, 0);
  565. case MEMSTICK_PAR4:
  566. return r592_set_mode(dev, 1);
  567. default:
  568. return -EINVAL;
  569. }
  570. default:
  571. return -EINVAL;
  572. }
  573. }
  574. /* External interface: submit requests */
  575. static void r592_submit_req(struct memstick_host *host)
  576. {
  577. struct r592_device *dev = memstick_priv(host);
  578. unsigned long flags;
  579. if (dev->req)
  580. return;
  581. spin_lock_irqsave(&dev->io_thread_lock, flags);
  582. if (wake_up_process(dev->io_thread))
  583. dbg_verbose("IO thread woken to process requests");
  584. spin_unlock_irqrestore(&dev->io_thread_lock, flags);
  585. }
  586. static const struct pci_device_id r592_pci_id_tbl[] = {
  587. { PCI_VDEVICE(RICOH, 0x0592), },
  588. { },
  589. };
  590. /* Main entry */
  591. static int r592_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  592. {
  593. int error = -ENOMEM;
  594. struct memstick_host *host;
  595. struct r592_device *dev;
  596. /* Allocate memory */
  597. host = memstick_alloc_host(sizeof(struct r592_device), &pdev->dev);
  598. if (!host)
  599. goto error1;
  600. dev = memstick_priv(host);
  601. dev->host = host;
  602. dev->pci_dev = pdev;
  603. pci_set_drvdata(pdev, dev);
  604. /* pci initialization */
  605. error = pci_enable_device(pdev);
  606. if (error)
  607. goto error2;
  608. pci_set_master(pdev);
  609. error = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  610. if (error)
  611. goto error3;
  612. error = pci_request_regions(pdev, DRV_NAME);
  613. if (error)
  614. goto error3;
  615. dev->mmio = pci_ioremap_bar(pdev, 0);
  616. if (!dev->mmio)
  617. goto error4;
  618. dev->irq = pdev->irq;
  619. spin_lock_init(&dev->irq_lock);
  620. spin_lock_init(&dev->io_thread_lock);
  621. init_completion(&dev->dma_done);
  622. INIT_KFIFO(dev->pio_fifo);
  623. setup_timer(&dev->detect_timer,
  624. r592_detect_timer, (long unsigned int)dev);
  625. /* Host initialization */
  626. host->caps = MEMSTICK_CAP_PAR4;
  627. host->request = r592_submit_req;
  628. host->set_param = r592_set_param;
  629. r592_check_dma(dev);
  630. dev->io_thread = kthread_run(r592_process_thread, dev, "r592_io");
  631. if (IS_ERR(dev->io_thread)) {
  632. error = PTR_ERR(dev->io_thread);
  633. goto error5;
  634. }
  635. /* This is just a precation, so don't fail */
  636. dev->dummy_dma_page = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  637. &dev->dummy_dma_page_physical_address, GFP_KERNEL);
  638. r592_stop_dma(dev , 0);
  639. if (request_irq(dev->irq, &r592_irq, IRQF_SHARED,
  640. DRV_NAME, dev))
  641. goto error6;
  642. r592_update_card_detect(dev);
  643. if (memstick_add_host(host))
  644. goto error7;
  645. message("driver successfully loaded");
  646. return 0;
  647. error7:
  648. free_irq(dev->irq, dev);
  649. error6:
  650. if (dev->dummy_dma_page)
  651. dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->dummy_dma_page,
  652. dev->dummy_dma_page_physical_address);
  653. kthread_stop(dev->io_thread);
  654. error5:
  655. iounmap(dev->mmio);
  656. error4:
  657. pci_release_regions(pdev);
  658. error3:
  659. pci_disable_device(pdev);
  660. error2:
  661. memstick_free_host(host);
  662. error1:
  663. return error;
  664. }
  665. static void r592_remove(struct pci_dev *pdev)
  666. {
  667. int error = 0;
  668. struct r592_device *dev = pci_get_drvdata(pdev);
  669. /* Stop the processing thread.
  670. That ensures that we won't take any more requests */
  671. kthread_stop(dev->io_thread);
  672. r592_enable_device(dev, false);
  673. while (!error && dev->req) {
  674. dev->req->error = -ETIME;
  675. error = memstick_next_req(dev->host, &dev->req);
  676. }
  677. memstick_remove_host(dev->host);
  678. free_irq(dev->irq, dev);
  679. iounmap(dev->mmio);
  680. pci_release_regions(pdev);
  681. pci_disable_device(pdev);
  682. memstick_free_host(dev->host);
  683. if (dev->dummy_dma_page)
  684. dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->dummy_dma_page,
  685. dev->dummy_dma_page_physical_address);
  686. }
  687. #ifdef CONFIG_PM_SLEEP
  688. static int r592_suspend(struct device *core_dev)
  689. {
  690. struct pci_dev *pdev = to_pci_dev(core_dev);
  691. struct r592_device *dev = pci_get_drvdata(pdev);
  692. r592_clear_interrupts(dev);
  693. memstick_suspend_host(dev->host);
  694. del_timer_sync(&dev->detect_timer);
  695. return 0;
  696. }
  697. static int r592_resume(struct device *core_dev)
  698. {
  699. struct pci_dev *pdev = to_pci_dev(core_dev);
  700. struct r592_device *dev = pci_get_drvdata(pdev);
  701. r592_clear_interrupts(dev);
  702. r592_enable_device(dev, false);
  703. memstick_resume_host(dev->host);
  704. r592_update_card_detect(dev);
  705. return 0;
  706. }
  707. #endif
  708. static SIMPLE_DEV_PM_OPS(r592_pm_ops, r592_suspend, r592_resume);
  709. MODULE_DEVICE_TABLE(pci, r592_pci_id_tbl);
  710. static struct pci_driver r852_pci_driver = {
  711. .name = DRV_NAME,
  712. .id_table = r592_pci_id_tbl,
  713. .probe = r592_probe,
  714. .remove = r592_remove,
  715. .driver.pm = &r592_pm_ops,
  716. };
  717. module_pci_driver(r852_pci_driver);
  718. module_param_named(enable_dma, r592_enable_dma, bool, S_IRUGO);
  719. MODULE_PARM_DESC(enable_dma, "Enable usage of the DMA (default)");
  720. module_param(debug, int, S_IRUGO | S_IWUSR);
  721. MODULE_PARM_DESC(debug, "Debug level (0-3)");
  722. MODULE_LICENSE("GPL");
  723. MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
  724. MODULE_DESCRIPTION("Ricoh R5C592 Memstick/Memstick PRO card reader driver");