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 ||
  242. (sg_dma_len(&dev->req->sg) < dev->req->sg.length)) {
  243. message("problem in dma_map_sg");
  244. return -EIO;
  245. }
  246. r592_start_dma(dev, is_write);
  247. /* Wait for DMA completion */
  248. if (!wait_for_completion_timeout(
  249. &dev->dma_done, msecs_to_jiffies(1000))) {
  250. message("DMA timeout");
  251. r592_stop_dma(dev, -ETIMEDOUT);
  252. }
  253. dma_unmap_sg(&dev->pci_dev->dev, &dev->req->sg, 1, is_write ?
  254. PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
  255. return dev->dma_error;
  256. }
  257. /*
  258. * Writes the FIFO in 4 byte chunks.
  259. * If length isn't 4 byte aligned, rest of the data if put to a fifo
  260. * to be written later
  261. * Use r592_flush_fifo_write to flush that fifo when writing for the
  262. * last time
  263. */
  264. static void r592_write_fifo_pio(struct r592_device *dev,
  265. unsigned char *buffer, int len)
  266. {
  267. /* flush spill from former write */
  268. if (!kfifo_is_empty(&dev->pio_fifo)) {
  269. u8 tmp[4] = {0};
  270. int copy_len = kfifo_in(&dev->pio_fifo, buffer, len);
  271. if (!kfifo_is_full(&dev->pio_fifo))
  272. return;
  273. len -= copy_len;
  274. buffer += copy_len;
  275. copy_len = kfifo_out(&dev->pio_fifo, tmp, 4);
  276. WARN_ON(copy_len != 4);
  277. r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)tmp);
  278. }
  279. WARN_ON(!kfifo_is_empty(&dev->pio_fifo));
  280. /* write full dwords */
  281. while (len >= 4) {
  282. r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)buffer);
  283. buffer += 4;
  284. len -= 4;
  285. }
  286. /* put remaining bytes to the spill */
  287. if (len)
  288. kfifo_in(&dev->pio_fifo, buffer, len);
  289. }
  290. /* Flushes the temporary FIFO used to make aligned DWORD writes */
  291. static void r592_flush_fifo_write(struct r592_device *dev)
  292. {
  293. u8 buffer[4] = { 0 };
  294. int len;
  295. if (kfifo_is_empty(&dev->pio_fifo))
  296. return;
  297. len = kfifo_out(&dev->pio_fifo, buffer, 4);
  298. r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)buffer);
  299. }
  300. /*
  301. * Read a fifo in 4 bytes chunks.
  302. * If input doesn't fit the buffer, it places bytes of last dword in spill
  303. * buffer, so that they don't get lost on last read, just throw these away.
  304. */
  305. static void r592_read_fifo_pio(struct r592_device *dev,
  306. unsigned char *buffer, int len)
  307. {
  308. u8 tmp[4];
  309. /* Read from last spill */
  310. if (!kfifo_is_empty(&dev->pio_fifo)) {
  311. int bytes_copied =
  312. kfifo_out(&dev->pio_fifo, buffer, min(4, len));
  313. buffer += bytes_copied;
  314. len -= bytes_copied;
  315. if (!kfifo_is_empty(&dev->pio_fifo))
  316. return;
  317. }
  318. /* Reads dwords from FIFO */
  319. while (len >= 4) {
  320. *(u32 *)buffer = r592_read_reg_raw_be(dev, R592_FIFO_PIO);
  321. buffer += 4;
  322. len -= 4;
  323. }
  324. if (len) {
  325. *(u32 *)tmp = r592_read_reg_raw_be(dev, R592_FIFO_PIO);
  326. kfifo_in(&dev->pio_fifo, tmp, 4);
  327. len -= kfifo_out(&dev->pio_fifo, buffer, len);
  328. }
  329. WARN_ON(len);
  330. return;
  331. }
  332. /* Transfers actual data using PIO. */
  333. static int r592_transfer_fifo_pio(struct r592_device *dev)
  334. {
  335. unsigned long flags;
  336. bool is_write = dev->req->tpc >= MS_TPC_SET_RW_REG_ADRS;
  337. struct sg_mapping_iter miter;
  338. kfifo_reset(&dev->pio_fifo);
  339. if (!dev->req->long_data) {
  340. if (is_write) {
  341. r592_write_fifo_pio(dev, dev->req->data,
  342. dev->req->data_len);
  343. r592_flush_fifo_write(dev);
  344. } else
  345. r592_read_fifo_pio(dev, dev->req->data,
  346. dev->req->data_len);
  347. return 0;
  348. }
  349. local_irq_save(flags);
  350. sg_miter_start(&miter, &dev->req->sg, 1, SG_MITER_ATOMIC |
  351. (is_write ? SG_MITER_FROM_SG : SG_MITER_TO_SG));
  352. /* Do the transfer fifo<->memory*/
  353. while (sg_miter_next(&miter))
  354. if (is_write)
  355. r592_write_fifo_pio(dev, miter.addr, miter.length);
  356. else
  357. r592_read_fifo_pio(dev, miter.addr, miter.length);
  358. /* Write last few non aligned bytes*/
  359. if (is_write)
  360. r592_flush_fifo_write(dev);
  361. sg_miter_stop(&miter);
  362. local_irq_restore(flags);
  363. return 0;
  364. }
  365. /* Executes one TPC (data is read/written from small or large fifo) */
  366. static void r592_execute_tpc(struct r592_device *dev)
  367. {
  368. bool is_write;
  369. int len, error;
  370. u32 status, reg;
  371. if (!dev->req) {
  372. message("BUG: tpc execution without request!");
  373. return;
  374. }
  375. is_write = dev->req->tpc >= MS_TPC_SET_RW_REG_ADRS;
  376. len = dev->req->long_data ?
  377. dev->req->sg.length : dev->req->data_len;
  378. /* Ensure that FIFO can hold the input data */
  379. if (len > R592_LFIFO_SIZE) {
  380. message("IO: hardware doesn't support TPCs longer that 512");
  381. error = -ENOSYS;
  382. goto out;
  383. }
  384. if (!(r592_read_reg(dev, R592_REG_MSC) & R592_REG_MSC_PRSNT)) {
  385. dbg("IO: refusing to send TPC because card is absent");
  386. error = -ENODEV;
  387. goto out;
  388. }
  389. dbg("IO: executing %s LEN=%d",
  390. memstick_debug_get_tpc_name(dev->req->tpc), len);
  391. /* Set IO direction */
  392. if (is_write)
  393. r592_set_reg_mask(dev, R592_IO, R592_IO_DIRECTION);
  394. else
  395. r592_clear_reg_mask(dev, R592_IO, R592_IO_DIRECTION);
  396. error = r592_test_fifo_empty(dev);
  397. if (error)
  398. goto out;
  399. /* Transfer write data */
  400. if (is_write) {
  401. error = r592_transfer_fifo_dma(dev);
  402. if (error == -EINVAL)
  403. error = r592_transfer_fifo_pio(dev);
  404. }
  405. if (error)
  406. goto out;
  407. /* Trigger the TPC */
  408. reg = (len << R592_TPC_EXEC_LEN_SHIFT) |
  409. (dev->req->tpc << R592_TPC_EXEC_TPC_SHIFT) |
  410. R592_TPC_EXEC_BIG_FIFO;
  411. r592_write_reg(dev, R592_TPC_EXEC, reg);
  412. /* Wait for TPC completion */
  413. status = R592_STATUS_RDY;
  414. if (dev->req->need_card_int)
  415. status |= R592_STATUS_CED;
  416. error = r592_wait_status(dev, status, status);
  417. if (error) {
  418. message("card didn't respond");
  419. goto out;
  420. }
  421. /* Test IO errors */
  422. error = r592_test_io_error(dev);
  423. if (error) {
  424. dbg("IO error");
  425. goto out;
  426. }
  427. /* Read data from FIFO */
  428. if (!is_write) {
  429. error = r592_transfer_fifo_dma(dev);
  430. if (error == -EINVAL)
  431. error = r592_transfer_fifo_pio(dev);
  432. }
  433. /* read INT reg. This can be shortened with shifts, but that way
  434. its more readable */
  435. if (dev->parallel_mode && dev->req->need_card_int) {
  436. dev->req->int_reg = 0;
  437. status = r592_read_reg(dev, R592_STATUS);
  438. if (status & R592_STATUS_P_CMDNACK)
  439. dev->req->int_reg |= MEMSTICK_INT_CMDNAK;
  440. if (status & R592_STATUS_P_BREQ)
  441. dev->req->int_reg |= MEMSTICK_INT_BREQ;
  442. if (status & R592_STATUS_P_INTERR)
  443. dev->req->int_reg |= MEMSTICK_INT_ERR;
  444. if (status & R592_STATUS_P_CED)
  445. dev->req->int_reg |= MEMSTICK_INT_CED;
  446. }
  447. if (error)
  448. dbg("FIFO read error");
  449. out:
  450. dev->req->error = error;
  451. r592_clear_reg_mask(dev, R592_REG_MSC, R592_REG_MSC_LED);
  452. return;
  453. }
  454. /* Main request processing thread */
  455. static int r592_process_thread(void *data)
  456. {
  457. int error;
  458. struct r592_device *dev = (struct r592_device *)data;
  459. unsigned long flags;
  460. while (!kthread_should_stop()) {
  461. spin_lock_irqsave(&dev->io_thread_lock, flags);
  462. set_current_state(TASK_INTERRUPTIBLE);
  463. error = memstick_next_req(dev->host, &dev->req);
  464. spin_unlock_irqrestore(&dev->io_thread_lock, flags);
  465. if (error) {
  466. if (error == -ENXIO || error == -EAGAIN) {
  467. dbg_verbose("IO: done IO, sleeping");
  468. } else {
  469. dbg("IO: unknown error from "
  470. "memstick_next_req %d", error);
  471. }
  472. if (kthread_should_stop())
  473. set_current_state(TASK_RUNNING);
  474. schedule();
  475. } else {
  476. set_current_state(TASK_RUNNING);
  477. r592_execute_tpc(dev);
  478. }
  479. }
  480. return 0;
  481. }
  482. /* Reprogram chip to detect change in card state */
  483. /* eg, if card is detected, arm it to detect removal, and vice versa */
  484. static void r592_update_card_detect(struct r592_device *dev)
  485. {
  486. u32 reg = r592_read_reg(dev, R592_REG_MSC);
  487. bool card_detected = reg & R592_REG_MSC_PRSNT;
  488. dbg("update card detect. card state: %s", card_detected ?
  489. "present" : "absent");
  490. reg &= ~((R592_REG_MSC_IRQ_REMOVE | R592_REG_MSC_IRQ_INSERT) << 16);
  491. if (card_detected)
  492. reg |= (R592_REG_MSC_IRQ_REMOVE << 16);
  493. else
  494. reg |= (R592_REG_MSC_IRQ_INSERT << 16);
  495. r592_write_reg(dev, R592_REG_MSC, reg);
  496. }
  497. /* Timer routine that fires 1 second after last card detection event, */
  498. static void r592_detect_timer(long unsigned int data)
  499. {
  500. struct r592_device *dev = (struct r592_device *)data;
  501. r592_update_card_detect(dev);
  502. memstick_detect_change(dev->host);
  503. }
  504. /* Interrupt handler */
  505. static irqreturn_t r592_irq(int irq, void *data)
  506. {
  507. struct r592_device *dev = (struct r592_device *)data;
  508. irqreturn_t ret = IRQ_NONE;
  509. u32 reg;
  510. u16 irq_enable, irq_status;
  511. unsigned long flags;
  512. int error;
  513. spin_lock_irqsave(&dev->irq_lock, flags);
  514. reg = r592_read_reg(dev, R592_REG_MSC);
  515. irq_enable = reg >> 16;
  516. irq_status = reg & 0xFFFF;
  517. /* Ack the interrupts */
  518. reg &= ~irq_status;
  519. r592_write_reg(dev, R592_REG_MSC, reg);
  520. /* Get the IRQ status minus bits that aren't enabled */
  521. irq_status &= (irq_enable);
  522. /* Due to limitation of memstick core, we don't look at bits that
  523. indicate that card was removed/inserted and/or present */
  524. if (irq_status & (R592_REG_MSC_IRQ_INSERT | R592_REG_MSC_IRQ_REMOVE)) {
  525. bool card_was_added = irq_status & R592_REG_MSC_IRQ_INSERT;
  526. ret = IRQ_HANDLED;
  527. message("IRQ: card %s", card_was_added ? "added" : "removed");
  528. mod_timer(&dev->detect_timer,
  529. jiffies + msecs_to_jiffies(card_was_added ? 500 : 50));
  530. }
  531. if (irq_status &
  532. (R592_REG_MSC_FIFO_DMA_DONE | R592_REG_MSC_FIFO_DMA_ERR)) {
  533. ret = IRQ_HANDLED;
  534. if (irq_status & R592_REG_MSC_FIFO_DMA_ERR) {
  535. message("IRQ: DMA error");
  536. error = -EIO;
  537. } else {
  538. dbg_verbose("IRQ: dma done");
  539. error = 0;
  540. }
  541. r592_stop_dma(dev, error);
  542. complete(&dev->dma_done);
  543. }
  544. spin_unlock_irqrestore(&dev->irq_lock, flags);
  545. return ret;
  546. }
  547. /* External inteface: set settings */
  548. static int r592_set_param(struct memstick_host *host,
  549. enum memstick_param param, int value)
  550. {
  551. struct r592_device *dev = memstick_priv(host);
  552. switch (param) {
  553. case MEMSTICK_POWER:
  554. switch (value) {
  555. case MEMSTICK_POWER_ON:
  556. return r592_enable_device(dev, true);
  557. case MEMSTICK_POWER_OFF:
  558. return r592_enable_device(dev, false);
  559. default:
  560. return -EINVAL;
  561. }
  562. case MEMSTICK_INTERFACE:
  563. switch (value) {
  564. case MEMSTICK_SERIAL:
  565. return r592_set_mode(dev, 0);
  566. case MEMSTICK_PAR4:
  567. return r592_set_mode(dev, 1);
  568. default:
  569. return -EINVAL;
  570. }
  571. default:
  572. return -EINVAL;
  573. }
  574. }
  575. /* External interface: submit requests */
  576. static void r592_submit_req(struct memstick_host *host)
  577. {
  578. struct r592_device *dev = memstick_priv(host);
  579. unsigned long flags;
  580. if (dev->req)
  581. return;
  582. spin_lock_irqsave(&dev->io_thread_lock, flags);
  583. if (wake_up_process(dev->io_thread))
  584. dbg_verbose("IO thread woken to process requests");
  585. spin_unlock_irqrestore(&dev->io_thread_lock, flags);
  586. }
  587. static const struct pci_device_id r592_pci_id_tbl[] = {
  588. { PCI_VDEVICE(RICOH, 0x0592), },
  589. { },
  590. };
  591. /* Main entry */
  592. static int r592_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  593. {
  594. int error = -ENOMEM;
  595. struct memstick_host *host;
  596. struct r592_device *dev;
  597. /* Allocate memory */
  598. host = memstick_alloc_host(sizeof(struct r592_device), &pdev->dev);
  599. if (!host)
  600. goto error1;
  601. dev = memstick_priv(host);
  602. dev->host = host;
  603. dev->pci_dev = pdev;
  604. pci_set_drvdata(pdev, dev);
  605. /* pci initialization */
  606. error = pci_enable_device(pdev);
  607. if (error)
  608. goto error2;
  609. pci_set_master(pdev);
  610. error = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  611. if (error)
  612. goto error3;
  613. error = pci_request_regions(pdev, DRV_NAME);
  614. if (error)
  615. goto error3;
  616. dev->mmio = pci_ioremap_bar(pdev, 0);
  617. if (!dev->mmio)
  618. goto error4;
  619. dev->irq = pdev->irq;
  620. spin_lock_init(&dev->irq_lock);
  621. spin_lock_init(&dev->io_thread_lock);
  622. init_completion(&dev->dma_done);
  623. INIT_KFIFO(dev->pio_fifo);
  624. setup_timer(&dev->detect_timer,
  625. r592_detect_timer, (long unsigned int)dev);
  626. /* Host initialization */
  627. host->caps = MEMSTICK_CAP_PAR4;
  628. host->request = r592_submit_req;
  629. host->set_param = r592_set_param;
  630. r592_check_dma(dev);
  631. dev->io_thread = kthread_run(r592_process_thread, dev, "r592_io");
  632. if (IS_ERR(dev->io_thread)) {
  633. error = PTR_ERR(dev->io_thread);
  634. goto error5;
  635. }
  636. /* This is just a precation, so don't fail */
  637. dev->dummy_dma_page = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  638. &dev->dummy_dma_page_physical_address, GFP_KERNEL);
  639. r592_stop_dma(dev , 0);
  640. if (request_irq(dev->irq, &r592_irq, IRQF_SHARED,
  641. DRV_NAME, dev))
  642. goto error6;
  643. r592_update_card_detect(dev);
  644. if (memstick_add_host(host))
  645. goto error7;
  646. message("driver successfully loaded");
  647. return 0;
  648. error7:
  649. free_irq(dev->irq, dev);
  650. error6:
  651. if (dev->dummy_dma_page)
  652. dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->dummy_dma_page,
  653. dev->dummy_dma_page_physical_address);
  654. kthread_stop(dev->io_thread);
  655. error5:
  656. iounmap(dev->mmio);
  657. error4:
  658. pci_release_regions(pdev);
  659. error3:
  660. pci_disable_device(pdev);
  661. error2:
  662. memstick_free_host(host);
  663. error1:
  664. return error;
  665. }
  666. static void r592_remove(struct pci_dev *pdev)
  667. {
  668. int error = 0;
  669. struct r592_device *dev = pci_get_drvdata(pdev);
  670. /* Stop the processing thread.
  671. That ensures that we won't take any more requests */
  672. kthread_stop(dev->io_thread);
  673. r592_enable_device(dev, false);
  674. while (!error && dev->req) {
  675. dev->req->error = -ETIME;
  676. error = memstick_next_req(dev->host, &dev->req);
  677. }
  678. memstick_remove_host(dev->host);
  679. free_irq(dev->irq, dev);
  680. iounmap(dev->mmio);
  681. pci_release_regions(pdev);
  682. pci_disable_device(pdev);
  683. memstick_free_host(dev->host);
  684. if (dev->dummy_dma_page)
  685. dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->dummy_dma_page,
  686. dev->dummy_dma_page_physical_address);
  687. }
  688. #ifdef CONFIG_PM_SLEEP
  689. static int r592_suspend(struct device *core_dev)
  690. {
  691. struct pci_dev *pdev = to_pci_dev(core_dev);
  692. struct r592_device *dev = pci_get_drvdata(pdev);
  693. r592_clear_interrupts(dev);
  694. memstick_suspend_host(dev->host);
  695. del_timer_sync(&dev->detect_timer);
  696. return 0;
  697. }
  698. static int r592_resume(struct device *core_dev)
  699. {
  700. struct pci_dev *pdev = to_pci_dev(core_dev);
  701. struct r592_device *dev = pci_get_drvdata(pdev);
  702. r592_clear_interrupts(dev);
  703. r592_enable_device(dev, false);
  704. memstick_resume_host(dev->host);
  705. r592_update_card_detect(dev);
  706. return 0;
  707. }
  708. #endif
  709. static SIMPLE_DEV_PM_OPS(r592_pm_ops, r592_suspend, r592_resume);
  710. MODULE_DEVICE_TABLE(pci, r592_pci_id_tbl);
  711. static struct pci_driver r852_pci_driver = {
  712. .name = DRV_NAME,
  713. .id_table = r592_pci_id_tbl,
  714. .probe = r592_probe,
  715. .remove = r592_remove,
  716. .driver.pm = &r592_pm_ops,
  717. };
  718. module_pci_driver(r852_pci_driver);
  719. module_param_named(enable_dma, r592_enable_dma, bool, S_IRUGO);
  720. MODULE_PARM_DESC(enable_dma, "Enable usage of the DMA (default)");
  721. module_param(debug, int, S_IRUGO | S_IWUSR);
  722. MODULE_PARM_DESC(debug, "Debug level (0-3)");
  723. MODULE_LICENSE("GPL");
  724. MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
  725. MODULE_DESCRIPTION("Ricoh R5C592 Memstick/Memstick PRO card reader driver");