r852.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * Copyright © 2009 - Maxim Levitsky
  3. * driver for Ricoh xD 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/jiffies.h>
  12. #include <linux/workqueue.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 <asm/byteorder.h>
  19. #include <linux/sched.h>
  20. #include "sm_common.h"
  21. #include "r852.h"
  22. static bool r852_enable_dma = 1;
  23. module_param(r852_enable_dma, bool, S_IRUGO);
  24. MODULE_PARM_DESC(r852_enable_dma, "Enable usage of the DMA (default)");
  25. static int debug;
  26. module_param(debug, int, S_IRUGO | S_IWUSR);
  27. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  28. /* read register */
  29. static inline uint8_t r852_read_reg(struct r852_device *dev, int address)
  30. {
  31. uint8_t reg = readb(dev->mmio + address);
  32. return reg;
  33. }
  34. /* write register */
  35. static inline void r852_write_reg(struct r852_device *dev,
  36. int address, uint8_t value)
  37. {
  38. writeb(value, dev->mmio + address);
  39. mmiowb();
  40. }
  41. /* read dword sized register */
  42. static inline uint32_t r852_read_reg_dword(struct r852_device *dev, int address)
  43. {
  44. uint32_t reg = le32_to_cpu(readl(dev->mmio + address));
  45. return reg;
  46. }
  47. /* write dword sized register */
  48. static inline void r852_write_reg_dword(struct r852_device *dev,
  49. int address, uint32_t value)
  50. {
  51. writel(cpu_to_le32(value), dev->mmio + address);
  52. mmiowb();
  53. }
  54. /* returns pointer to our private structure */
  55. static inline struct r852_device *r852_get_dev(struct mtd_info *mtd)
  56. {
  57. struct nand_chip *chip = mtd->priv;
  58. return chip->priv;
  59. }
  60. /* check if controller supports dma */
  61. static void r852_dma_test(struct r852_device *dev)
  62. {
  63. dev->dma_usable = (r852_read_reg(dev, R852_DMA_CAP) &
  64. (R852_DMA1 | R852_DMA2)) == (R852_DMA1 | R852_DMA2);
  65. if (!dev->dma_usable)
  66. message("Non dma capable device detected, dma disabled");
  67. if (!r852_enable_dma) {
  68. message("disabling dma on user request");
  69. dev->dma_usable = 0;
  70. }
  71. }
  72. /*
  73. * Enable dma. Enables ether first or second stage of the DMA,
  74. * Expects dev->dma_dir and dev->dma_state be set
  75. */
  76. static void r852_dma_enable(struct r852_device *dev)
  77. {
  78. uint8_t dma_reg, dma_irq_reg;
  79. /* Set up dma settings */
  80. dma_reg = r852_read_reg_dword(dev, R852_DMA_SETTINGS);
  81. dma_reg &= ~(R852_DMA_READ | R852_DMA_INTERNAL | R852_DMA_MEMORY);
  82. if (dev->dma_dir)
  83. dma_reg |= R852_DMA_READ;
  84. if (dev->dma_state == DMA_INTERNAL) {
  85. dma_reg |= R852_DMA_INTERNAL;
  86. /* Precaution to make sure HW doesn't write */
  87. /* to random kernel memory */
  88. r852_write_reg_dword(dev, R852_DMA_ADDR,
  89. cpu_to_le32(dev->phys_bounce_buffer));
  90. } else {
  91. dma_reg |= R852_DMA_MEMORY;
  92. r852_write_reg_dword(dev, R852_DMA_ADDR,
  93. cpu_to_le32(dev->phys_dma_addr));
  94. }
  95. /* Precaution: make sure write reached the device */
  96. r852_read_reg_dword(dev, R852_DMA_ADDR);
  97. r852_write_reg_dword(dev, R852_DMA_SETTINGS, dma_reg);
  98. /* Set dma irq */
  99. dma_irq_reg = r852_read_reg_dword(dev, R852_DMA_IRQ_ENABLE);
  100. r852_write_reg_dword(dev, R852_DMA_IRQ_ENABLE,
  101. dma_irq_reg |
  102. R852_DMA_IRQ_INTERNAL |
  103. R852_DMA_IRQ_ERROR |
  104. R852_DMA_IRQ_MEMORY);
  105. }
  106. /*
  107. * Disable dma, called from the interrupt handler, which specifies
  108. * success of the operation via 'error' argument
  109. */
  110. static void r852_dma_done(struct r852_device *dev, int error)
  111. {
  112. WARN_ON(dev->dma_stage == 0);
  113. r852_write_reg_dword(dev, R852_DMA_IRQ_STA,
  114. r852_read_reg_dword(dev, R852_DMA_IRQ_STA));
  115. r852_write_reg_dword(dev, R852_DMA_SETTINGS, 0);
  116. r852_write_reg_dword(dev, R852_DMA_IRQ_ENABLE, 0);
  117. /* Precaution to make sure HW doesn't write to random kernel memory */
  118. r852_write_reg_dword(dev, R852_DMA_ADDR,
  119. cpu_to_le32(dev->phys_bounce_buffer));
  120. r852_read_reg_dword(dev, R852_DMA_ADDR);
  121. dev->dma_error = error;
  122. dev->dma_stage = 0;
  123. if (dev->phys_dma_addr && dev->phys_dma_addr != dev->phys_bounce_buffer)
  124. pci_unmap_single(dev->pci_dev, dev->phys_dma_addr, R852_DMA_LEN,
  125. dev->dma_dir ? PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
  126. }
  127. /*
  128. * Wait, till dma is done, which includes both phases of it
  129. */
  130. static int r852_dma_wait(struct r852_device *dev)
  131. {
  132. long timeout = wait_for_completion_timeout(&dev->dma_done,
  133. msecs_to_jiffies(1000));
  134. if (!timeout) {
  135. dbg("timeout waiting for DMA interrupt");
  136. return -ETIMEDOUT;
  137. }
  138. return 0;
  139. }
  140. /*
  141. * Read/Write one page using dma. Only pages can be read (512 bytes)
  142. */
  143. static void r852_do_dma(struct r852_device *dev, uint8_t *buf, int do_read)
  144. {
  145. int bounce = 0;
  146. unsigned long flags;
  147. int error;
  148. dev->dma_error = 0;
  149. /* Set dma direction */
  150. dev->dma_dir = do_read;
  151. dev->dma_stage = 1;
  152. reinit_completion(&dev->dma_done);
  153. dbg_verbose("doing dma %s ", do_read ? "read" : "write");
  154. /* Set initial dma state: for reading first fill on board buffer,
  155. from device, for writes first fill the buffer from memory*/
  156. dev->dma_state = do_read ? DMA_INTERNAL : DMA_MEMORY;
  157. /* if incoming buffer is not page aligned, we should do bounce */
  158. if ((unsigned long)buf & (R852_DMA_LEN-1))
  159. bounce = 1;
  160. if (!bounce) {
  161. dev->phys_dma_addr = pci_map_single(dev->pci_dev, (void *)buf,
  162. R852_DMA_LEN,
  163. (do_read ? PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE));
  164. if (pci_dma_mapping_error(dev->pci_dev, dev->phys_dma_addr))
  165. bounce = 1;
  166. }
  167. if (bounce) {
  168. dbg_verbose("dma: using bounce buffer");
  169. dev->phys_dma_addr = dev->phys_bounce_buffer;
  170. if (!do_read)
  171. memcpy(dev->bounce_buffer, buf, R852_DMA_LEN);
  172. }
  173. /* Enable DMA */
  174. spin_lock_irqsave(&dev->irqlock, flags);
  175. r852_dma_enable(dev);
  176. spin_unlock_irqrestore(&dev->irqlock, flags);
  177. /* Wait till complete */
  178. error = r852_dma_wait(dev);
  179. if (error) {
  180. r852_dma_done(dev, error);
  181. return;
  182. }
  183. if (do_read && bounce)
  184. memcpy((void *)buf, dev->bounce_buffer, R852_DMA_LEN);
  185. }
  186. /*
  187. * Program data lines of the nand chip to send data to it
  188. */
  189. static void r852_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  190. {
  191. struct r852_device *dev = r852_get_dev(mtd);
  192. uint32_t reg;
  193. /* Don't allow any access to hardware if we suspect card removal */
  194. if (dev->card_unstable)
  195. return;
  196. /* Special case for whole sector read */
  197. if (len == R852_DMA_LEN && dev->dma_usable) {
  198. r852_do_dma(dev, (uint8_t *)buf, 0);
  199. return;
  200. }
  201. /* write DWORD chinks - faster */
  202. while (len >= 4) {
  203. reg = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
  204. r852_write_reg_dword(dev, R852_DATALINE, reg);
  205. buf += 4;
  206. len -= 4;
  207. }
  208. /* write rest */
  209. while (len > 0) {
  210. r852_write_reg(dev, R852_DATALINE, *buf++);
  211. len--;
  212. }
  213. }
  214. /*
  215. * Read data lines of the nand chip to retrieve data
  216. */
  217. static void r852_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  218. {
  219. struct r852_device *dev = r852_get_dev(mtd);
  220. uint32_t reg;
  221. if (dev->card_unstable) {
  222. /* since we can't signal error here, at least, return
  223. predictable buffer */
  224. memset(buf, 0, len);
  225. return;
  226. }
  227. /* special case for whole sector read */
  228. if (len == R852_DMA_LEN && dev->dma_usable) {
  229. r852_do_dma(dev, buf, 1);
  230. return;
  231. }
  232. /* read in dword sized chunks */
  233. while (len >= 4) {
  234. reg = r852_read_reg_dword(dev, R852_DATALINE);
  235. *buf++ = reg & 0xFF;
  236. *buf++ = (reg >> 8) & 0xFF;
  237. *buf++ = (reg >> 16) & 0xFF;
  238. *buf++ = (reg >> 24) & 0xFF;
  239. len -= 4;
  240. }
  241. /* read the reset by bytes */
  242. while (len--)
  243. *buf++ = r852_read_reg(dev, R852_DATALINE);
  244. }
  245. /*
  246. * Read one byte from nand chip
  247. */
  248. static uint8_t r852_read_byte(struct mtd_info *mtd)
  249. {
  250. struct r852_device *dev = r852_get_dev(mtd);
  251. /* Same problem as in r852_read_buf.... */
  252. if (dev->card_unstable)
  253. return 0;
  254. return r852_read_reg(dev, R852_DATALINE);
  255. }
  256. /*
  257. * Control several chip lines & send commands
  258. */
  259. static void r852_cmdctl(struct mtd_info *mtd, int dat, unsigned int ctrl)
  260. {
  261. struct r852_device *dev = r852_get_dev(mtd);
  262. if (dev->card_unstable)
  263. return;
  264. if (ctrl & NAND_CTRL_CHANGE) {
  265. dev->ctlreg &= ~(R852_CTL_DATA | R852_CTL_COMMAND |
  266. R852_CTL_ON | R852_CTL_CARDENABLE);
  267. if (ctrl & NAND_ALE)
  268. dev->ctlreg |= R852_CTL_DATA;
  269. if (ctrl & NAND_CLE)
  270. dev->ctlreg |= R852_CTL_COMMAND;
  271. if (ctrl & NAND_NCE)
  272. dev->ctlreg |= (R852_CTL_CARDENABLE | R852_CTL_ON);
  273. else
  274. dev->ctlreg &= ~R852_CTL_WRITE;
  275. /* when write is stareted, enable write access */
  276. if (dat == NAND_CMD_ERASE1)
  277. dev->ctlreg |= R852_CTL_WRITE;
  278. r852_write_reg(dev, R852_CTL, dev->ctlreg);
  279. }
  280. /* HACK: NAND_CMD_SEQIN is called without NAND_CTRL_CHANGE, but we need
  281. to set write mode */
  282. if (dat == NAND_CMD_SEQIN && (dev->ctlreg & R852_CTL_COMMAND)) {
  283. dev->ctlreg |= R852_CTL_WRITE;
  284. r852_write_reg(dev, R852_CTL, dev->ctlreg);
  285. }
  286. if (dat != NAND_CMD_NONE)
  287. r852_write_reg(dev, R852_DATALINE, dat);
  288. }
  289. /*
  290. * Wait till card is ready.
  291. * based on nand_wait, but returns errors on DMA error
  292. */
  293. static int r852_wait(struct mtd_info *mtd, struct nand_chip *chip)
  294. {
  295. struct r852_device *dev = chip->priv;
  296. unsigned long timeout;
  297. int status;
  298. timeout = jiffies + (chip->state == FL_ERASING ?
  299. msecs_to_jiffies(400) : msecs_to_jiffies(20));
  300. while (time_before(jiffies, timeout))
  301. if (chip->dev_ready(mtd))
  302. break;
  303. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  304. status = (int)chip->read_byte(mtd);
  305. /* Unfortunelly, no way to send detailed error status... */
  306. if (dev->dma_error) {
  307. status |= NAND_STATUS_FAIL;
  308. dev->dma_error = 0;
  309. }
  310. return status;
  311. }
  312. /*
  313. * Check if card is ready
  314. */
  315. static int r852_ready(struct mtd_info *mtd)
  316. {
  317. struct r852_device *dev = r852_get_dev(mtd);
  318. return !(r852_read_reg(dev, R852_CARD_STA) & R852_CARD_STA_BUSY);
  319. }
  320. /*
  321. * Set ECC engine mode
  322. */
  323. static void r852_ecc_hwctl(struct mtd_info *mtd, int mode)
  324. {
  325. struct r852_device *dev = r852_get_dev(mtd);
  326. if (dev->card_unstable)
  327. return;
  328. switch (mode) {
  329. case NAND_ECC_READ:
  330. case NAND_ECC_WRITE:
  331. /* enable ecc generation/check*/
  332. dev->ctlreg |= R852_CTL_ECC_ENABLE;
  333. /* flush ecc buffer */
  334. r852_write_reg(dev, R852_CTL,
  335. dev->ctlreg | R852_CTL_ECC_ACCESS);
  336. r852_read_reg_dword(dev, R852_DATALINE);
  337. r852_write_reg(dev, R852_CTL, dev->ctlreg);
  338. return;
  339. case NAND_ECC_READSYN:
  340. /* disable ecc generation */
  341. dev->ctlreg &= ~R852_CTL_ECC_ENABLE;
  342. r852_write_reg(dev, R852_CTL, dev->ctlreg);
  343. }
  344. }
  345. /*
  346. * Calculate ECC, only used for writes
  347. */
  348. static int r852_ecc_calculate(struct mtd_info *mtd, const uint8_t *dat,
  349. uint8_t *ecc_code)
  350. {
  351. struct r852_device *dev = r852_get_dev(mtd);
  352. struct sm_oob *oob = (struct sm_oob *)ecc_code;
  353. uint32_t ecc1, ecc2;
  354. if (dev->card_unstable)
  355. return 0;
  356. dev->ctlreg &= ~R852_CTL_ECC_ENABLE;
  357. r852_write_reg(dev, R852_CTL, dev->ctlreg | R852_CTL_ECC_ACCESS);
  358. ecc1 = r852_read_reg_dword(dev, R852_DATALINE);
  359. ecc2 = r852_read_reg_dword(dev, R852_DATALINE);
  360. oob->ecc1[0] = (ecc1) & 0xFF;
  361. oob->ecc1[1] = (ecc1 >> 8) & 0xFF;
  362. oob->ecc1[2] = (ecc1 >> 16) & 0xFF;
  363. oob->ecc2[0] = (ecc2) & 0xFF;
  364. oob->ecc2[1] = (ecc2 >> 8) & 0xFF;
  365. oob->ecc2[2] = (ecc2 >> 16) & 0xFF;
  366. r852_write_reg(dev, R852_CTL, dev->ctlreg);
  367. return 0;
  368. }
  369. /*
  370. * Correct the data using ECC, hw did almost everything for us
  371. */
  372. static int r852_ecc_correct(struct mtd_info *mtd, uint8_t *dat,
  373. uint8_t *read_ecc, uint8_t *calc_ecc)
  374. {
  375. uint32_t ecc_reg;
  376. uint8_t ecc_status, err_byte;
  377. int i, error = 0;
  378. struct r852_device *dev = r852_get_dev(mtd);
  379. if (dev->card_unstable)
  380. return 0;
  381. if (dev->dma_error) {
  382. dev->dma_error = 0;
  383. return -1;
  384. }
  385. r852_write_reg(dev, R852_CTL, dev->ctlreg | R852_CTL_ECC_ACCESS);
  386. ecc_reg = r852_read_reg_dword(dev, R852_DATALINE);
  387. r852_write_reg(dev, R852_CTL, dev->ctlreg);
  388. for (i = 0 ; i <= 1 ; i++) {
  389. ecc_status = (ecc_reg >> 8) & 0xFF;
  390. /* ecc uncorrectable error */
  391. if (ecc_status & R852_ECC_FAIL) {
  392. dbg("ecc: unrecoverable error, in half %d", i);
  393. error = -1;
  394. goto exit;
  395. }
  396. /* correctable error */
  397. if (ecc_status & R852_ECC_CORRECTABLE) {
  398. err_byte = ecc_reg & 0xFF;
  399. dbg("ecc: recoverable error, "
  400. "in half %d, byte %d, bit %d", i,
  401. err_byte, ecc_status & R852_ECC_ERR_BIT_MSK);
  402. dat[err_byte] ^=
  403. 1 << (ecc_status & R852_ECC_ERR_BIT_MSK);
  404. error++;
  405. }
  406. dat += 256;
  407. ecc_reg >>= 16;
  408. }
  409. exit:
  410. return error;
  411. }
  412. /*
  413. * This is copy of nand_read_oob_std
  414. * nand_read_oob_syndrome assumes we can send column address - we can't
  415. */
  416. static int r852_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  417. int page)
  418. {
  419. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  420. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  421. return 0;
  422. }
  423. /*
  424. * Start the nand engine
  425. */
  426. static void r852_engine_enable(struct r852_device *dev)
  427. {
  428. if (r852_read_reg_dword(dev, R852_HW) & R852_HW_UNKNOWN) {
  429. r852_write_reg(dev, R852_CTL, R852_CTL_RESET | R852_CTL_ON);
  430. r852_write_reg_dword(dev, R852_HW, R852_HW_ENABLED);
  431. } else {
  432. r852_write_reg_dword(dev, R852_HW, R852_HW_ENABLED);
  433. r852_write_reg(dev, R852_CTL, R852_CTL_RESET | R852_CTL_ON);
  434. }
  435. msleep(300);
  436. r852_write_reg(dev, R852_CTL, 0);
  437. }
  438. /*
  439. * Stop the nand engine
  440. */
  441. static void r852_engine_disable(struct r852_device *dev)
  442. {
  443. r852_write_reg_dword(dev, R852_HW, 0);
  444. r852_write_reg(dev, R852_CTL, R852_CTL_RESET);
  445. }
  446. /*
  447. * Test if card is present
  448. */
  449. static void r852_card_update_present(struct r852_device *dev)
  450. {
  451. unsigned long flags;
  452. uint8_t reg;
  453. spin_lock_irqsave(&dev->irqlock, flags);
  454. reg = r852_read_reg(dev, R852_CARD_STA);
  455. dev->card_detected = !!(reg & R852_CARD_STA_PRESENT);
  456. spin_unlock_irqrestore(&dev->irqlock, flags);
  457. }
  458. /*
  459. * Update card detection IRQ state according to current card state
  460. * which is read in r852_card_update_present
  461. */
  462. static void r852_update_card_detect(struct r852_device *dev)
  463. {
  464. int card_detect_reg = r852_read_reg(dev, R852_CARD_IRQ_ENABLE);
  465. dev->card_unstable = 0;
  466. card_detect_reg &= ~(R852_CARD_IRQ_REMOVE | R852_CARD_IRQ_INSERT);
  467. card_detect_reg |= R852_CARD_IRQ_GENABLE;
  468. card_detect_reg |= dev->card_detected ?
  469. R852_CARD_IRQ_REMOVE : R852_CARD_IRQ_INSERT;
  470. r852_write_reg(dev, R852_CARD_IRQ_ENABLE, card_detect_reg);
  471. }
  472. static ssize_t r852_media_type_show(struct device *sys_dev,
  473. struct device_attribute *attr, char *buf)
  474. {
  475. struct mtd_info *mtd = container_of(sys_dev, struct mtd_info, dev);
  476. struct r852_device *dev = r852_get_dev(mtd);
  477. char *data = dev->sm ? "smartmedia" : "xd";
  478. strcpy(buf, data);
  479. return strlen(data);
  480. }
  481. static DEVICE_ATTR(media_type, S_IRUGO, r852_media_type_show, NULL);
  482. /* Detect properties of card in slot */
  483. static void r852_update_media_status(struct r852_device *dev)
  484. {
  485. uint8_t reg;
  486. unsigned long flags;
  487. int readonly;
  488. spin_lock_irqsave(&dev->irqlock, flags);
  489. if (!dev->card_detected) {
  490. message("card removed");
  491. spin_unlock_irqrestore(&dev->irqlock, flags);
  492. return ;
  493. }
  494. readonly = r852_read_reg(dev, R852_CARD_STA) & R852_CARD_STA_RO;
  495. reg = r852_read_reg(dev, R852_DMA_CAP);
  496. dev->sm = (reg & (R852_DMA1 | R852_DMA2)) && (reg & R852_SMBIT);
  497. message("detected %s %s card in slot",
  498. dev->sm ? "SmartMedia" : "xD",
  499. readonly ? "readonly" : "writeable");
  500. dev->readonly = readonly;
  501. spin_unlock_irqrestore(&dev->irqlock, flags);
  502. }
  503. /*
  504. * Register the nand device
  505. * Called when the card is detected
  506. */
  507. static int r852_register_nand_device(struct r852_device *dev)
  508. {
  509. dev->mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
  510. if (!dev->mtd)
  511. goto error1;
  512. WARN_ON(dev->card_registred);
  513. dev->mtd->priv = dev->chip;
  514. dev->mtd->dev.parent = &dev->pci_dev->dev;
  515. if (dev->readonly)
  516. dev->chip->options |= NAND_ROM;
  517. r852_engine_enable(dev);
  518. if (sm_register_device(dev->mtd, dev->sm))
  519. goto error2;
  520. if (device_create_file(&dev->mtd->dev, &dev_attr_media_type)) {
  521. message("can't create media type sysfs attribute");
  522. goto error3;
  523. }
  524. dev->card_registred = 1;
  525. return 0;
  526. error3:
  527. nand_release(dev->mtd);
  528. error2:
  529. kfree(dev->mtd);
  530. error1:
  531. /* Force card redetect */
  532. dev->card_detected = 0;
  533. return -1;
  534. }
  535. /*
  536. * Unregister the card
  537. */
  538. static void r852_unregister_nand_device(struct r852_device *dev)
  539. {
  540. if (!dev->card_registred)
  541. return;
  542. device_remove_file(&dev->mtd->dev, &dev_attr_media_type);
  543. nand_release(dev->mtd);
  544. r852_engine_disable(dev);
  545. dev->card_registred = 0;
  546. kfree(dev->mtd);
  547. dev->mtd = NULL;
  548. }
  549. /* Card state updater */
  550. static void r852_card_detect_work(struct work_struct *work)
  551. {
  552. struct r852_device *dev =
  553. container_of(work, struct r852_device, card_detect_work.work);
  554. r852_card_update_present(dev);
  555. r852_update_card_detect(dev);
  556. dev->card_unstable = 0;
  557. /* False alarm */
  558. if (dev->card_detected == dev->card_registred)
  559. goto exit;
  560. /* Read media properties */
  561. r852_update_media_status(dev);
  562. /* Register the card */
  563. if (dev->card_detected)
  564. r852_register_nand_device(dev);
  565. else
  566. r852_unregister_nand_device(dev);
  567. exit:
  568. r852_update_card_detect(dev);
  569. }
  570. /* Ack + disable IRQ generation */
  571. static void r852_disable_irqs(struct r852_device *dev)
  572. {
  573. uint8_t reg;
  574. reg = r852_read_reg(dev, R852_CARD_IRQ_ENABLE);
  575. r852_write_reg(dev, R852_CARD_IRQ_ENABLE, reg & ~R852_CARD_IRQ_MASK);
  576. reg = r852_read_reg_dword(dev, R852_DMA_IRQ_ENABLE);
  577. r852_write_reg_dword(dev, R852_DMA_IRQ_ENABLE,
  578. reg & ~R852_DMA_IRQ_MASK);
  579. r852_write_reg(dev, R852_CARD_IRQ_STA, R852_CARD_IRQ_MASK);
  580. r852_write_reg_dword(dev, R852_DMA_IRQ_STA, R852_DMA_IRQ_MASK);
  581. }
  582. /* Interrupt handler */
  583. static irqreturn_t r852_irq(int irq, void *data)
  584. {
  585. struct r852_device *dev = (struct r852_device *)data;
  586. uint8_t card_status, dma_status;
  587. unsigned long flags;
  588. irqreturn_t ret = IRQ_NONE;
  589. spin_lock_irqsave(&dev->irqlock, flags);
  590. /* handle card detection interrupts first */
  591. card_status = r852_read_reg(dev, R852_CARD_IRQ_STA);
  592. r852_write_reg(dev, R852_CARD_IRQ_STA, card_status);
  593. if (card_status & (R852_CARD_IRQ_INSERT|R852_CARD_IRQ_REMOVE)) {
  594. ret = IRQ_HANDLED;
  595. dev->card_detected = !!(card_status & R852_CARD_IRQ_INSERT);
  596. /* we shouldn't receive any interrupts if we wait for card
  597. to settle */
  598. WARN_ON(dev->card_unstable);
  599. /* disable irqs while card is unstable */
  600. /* this will timeout DMA if active, but better that garbage */
  601. r852_disable_irqs(dev);
  602. if (dev->card_unstable)
  603. goto out;
  604. /* let, card state to settle a bit, and then do the work */
  605. dev->card_unstable = 1;
  606. queue_delayed_work(dev->card_workqueue,
  607. &dev->card_detect_work, msecs_to_jiffies(100));
  608. goto out;
  609. }
  610. /* Handle dma interrupts */
  611. dma_status = r852_read_reg_dword(dev, R852_DMA_IRQ_STA);
  612. r852_write_reg_dword(dev, R852_DMA_IRQ_STA, dma_status);
  613. if (dma_status & R852_DMA_IRQ_MASK) {
  614. ret = IRQ_HANDLED;
  615. if (dma_status & R852_DMA_IRQ_ERROR) {
  616. dbg("received dma error IRQ");
  617. r852_dma_done(dev, -EIO);
  618. complete(&dev->dma_done);
  619. goto out;
  620. }
  621. /* received DMA interrupt out of nowhere? */
  622. WARN_ON_ONCE(dev->dma_stage == 0);
  623. if (dev->dma_stage == 0)
  624. goto out;
  625. /* done device access */
  626. if (dev->dma_state == DMA_INTERNAL &&
  627. (dma_status & R852_DMA_IRQ_INTERNAL)) {
  628. dev->dma_state = DMA_MEMORY;
  629. dev->dma_stage++;
  630. }
  631. /* done memory DMA */
  632. if (dev->dma_state == DMA_MEMORY &&
  633. (dma_status & R852_DMA_IRQ_MEMORY)) {
  634. dev->dma_state = DMA_INTERNAL;
  635. dev->dma_stage++;
  636. }
  637. /* Enable 2nd half of dma dance */
  638. if (dev->dma_stage == 2)
  639. r852_dma_enable(dev);
  640. /* Operation done */
  641. if (dev->dma_stage == 3) {
  642. r852_dma_done(dev, 0);
  643. complete(&dev->dma_done);
  644. }
  645. goto out;
  646. }
  647. /* Handle unknown interrupts */
  648. if (dma_status)
  649. dbg("bad dma IRQ status = %x", dma_status);
  650. if (card_status & ~R852_CARD_STA_CD)
  651. dbg("strange card status = %x", card_status);
  652. out:
  653. spin_unlock_irqrestore(&dev->irqlock, flags);
  654. return ret;
  655. }
  656. static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
  657. {
  658. int error;
  659. struct nand_chip *chip;
  660. struct r852_device *dev;
  661. /* pci initialization */
  662. error = pci_enable_device(pci_dev);
  663. if (error)
  664. goto error1;
  665. pci_set_master(pci_dev);
  666. error = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
  667. if (error)
  668. goto error2;
  669. error = pci_request_regions(pci_dev, DRV_NAME);
  670. if (error)
  671. goto error3;
  672. error = -ENOMEM;
  673. /* init nand chip, but register it only on card insert */
  674. chip = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
  675. if (!chip)
  676. goto error4;
  677. /* commands */
  678. chip->cmd_ctrl = r852_cmdctl;
  679. chip->waitfunc = r852_wait;
  680. chip->dev_ready = r852_ready;
  681. /* I/O */
  682. chip->read_byte = r852_read_byte;
  683. chip->read_buf = r852_read_buf;
  684. chip->write_buf = r852_write_buf;
  685. /* ecc */
  686. chip->ecc.mode = NAND_ECC_HW_SYNDROME;
  687. chip->ecc.size = R852_DMA_LEN;
  688. chip->ecc.bytes = SM_OOB_SIZE;
  689. chip->ecc.strength = 2;
  690. chip->ecc.hwctl = r852_ecc_hwctl;
  691. chip->ecc.calculate = r852_ecc_calculate;
  692. chip->ecc.correct = r852_ecc_correct;
  693. /* TODO: hack */
  694. chip->ecc.read_oob = r852_read_oob;
  695. /* init our device structure */
  696. dev = kzalloc(sizeof(struct r852_device), GFP_KERNEL);
  697. if (!dev)
  698. goto error5;
  699. chip->priv = dev;
  700. dev->chip = chip;
  701. dev->pci_dev = pci_dev;
  702. pci_set_drvdata(pci_dev, dev);
  703. dev->bounce_buffer = pci_alloc_consistent(pci_dev, R852_DMA_LEN,
  704. &dev->phys_bounce_buffer);
  705. if (!dev->bounce_buffer)
  706. goto error6;
  707. error = -ENODEV;
  708. dev->mmio = pci_ioremap_bar(pci_dev, 0);
  709. if (!dev->mmio)
  710. goto error7;
  711. error = -ENOMEM;
  712. dev->tmp_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL);
  713. if (!dev->tmp_buffer)
  714. goto error8;
  715. init_completion(&dev->dma_done);
  716. dev->card_workqueue = create_freezable_workqueue(DRV_NAME);
  717. if (!dev->card_workqueue)
  718. goto error9;
  719. INIT_DELAYED_WORK(&dev->card_detect_work, r852_card_detect_work);
  720. /* shutdown everything - precation */
  721. r852_engine_disable(dev);
  722. r852_disable_irqs(dev);
  723. r852_dma_test(dev);
  724. dev->irq = pci_dev->irq;
  725. spin_lock_init(&dev->irqlock);
  726. dev->card_detected = 0;
  727. r852_card_update_present(dev);
  728. /*register irq handler*/
  729. error = -ENODEV;
  730. if (request_irq(pci_dev->irq, &r852_irq, IRQF_SHARED,
  731. DRV_NAME, dev))
  732. goto error10;
  733. /* kick initial present test */
  734. queue_delayed_work(dev->card_workqueue,
  735. &dev->card_detect_work, 0);
  736. printk(KERN_NOTICE DRV_NAME ": driver loaded successfully\n");
  737. return 0;
  738. error10:
  739. destroy_workqueue(dev->card_workqueue);
  740. error9:
  741. kfree(dev->tmp_buffer);
  742. error8:
  743. pci_iounmap(pci_dev, dev->mmio);
  744. error7:
  745. pci_free_consistent(pci_dev, R852_DMA_LEN,
  746. dev->bounce_buffer, dev->phys_bounce_buffer);
  747. error6:
  748. kfree(dev);
  749. error5:
  750. kfree(chip);
  751. error4:
  752. pci_release_regions(pci_dev);
  753. error3:
  754. error2:
  755. pci_disable_device(pci_dev);
  756. error1:
  757. return error;
  758. }
  759. static void r852_remove(struct pci_dev *pci_dev)
  760. {
  761. struct r852_device *dev = pci_get_drvdata(pci_dev);
  762. /* Stop detect workqueue -
  763. we are going to unregister the device anyway*/
  764. cancel_delayed_work_sync(&dev->card_detect_work);
  765. destroy_workqueue(dev->card_workqueue);
  766. /* Unregister the device, this might make more IO */
  767. r852_unregister_nand_device(dev);
  768. /* Stop interrupts */
  769. r852_disable_irqs(dev);
  770. synchronize_irq(dev->irq);
  771. free_irq(dev->irq, dev);
  772. /* Cleanup */
  773. kfree(dev->tmp_buffer);
  774. pci_iounmap(pci_dev, dev->mmio);
  775. pci_free_consistent(pci_dev, R852_DMA_LEN,
  776. dev->bounce_buffer, dev->phys_bounce_buffer);
  777. kfree(dev->chip);
  778. kfree(dev);
  779. /* Shutdown the PCI device */
  780. pci_release_regions(pci_dev);
  781. pci_disable_device(pci_dev);
  782. }
  783. static void r852_shutdown(struct pci_dev *pci_dev)
  784. {
  785. struct r852_device *dev = pci_get_drvdata(pci_dev);
  786. cancel_delayed_work_sync(&dev->card_detect_work);
  787. r852_disable_irqs(dev);
  788. synchronize_irq(dev->irq);
  789. pci_disable_device(pci_dev);
  790. }
  791. #ifdef CONFIG_PM_SLEEP
  792. static int r852_suspend(struct device *device)
  793. {
  794. struct r852_device *dev = pci_get_drvdata(to_pci_dev(device));
  795. if (dev->ctlreg & R852_CTL_CARDENABLE)
  796. return -EBUSY;
  797. /* First make sure the detect work is gone */
  798. cancel_delayed_work_sync(&dev->card_detect_work);
  799. /* Turn off the interrupts and stop the device */
  800. r852_disable_irqs(dev);
  801. r852_engine_disable(dev);
  802. /* If card was pulled off just during the suspend, which is very
  803. unlikely, we will remove it on resume, it too late now
  804. anyway... */
  805. dev->card_unstable = 0;
  806. return 0;
  807. }
  808. static int r852_resume(struct device *device)
  809. {
  810. struct r852_device *dev = pci_get_drvdata(to_pci_dev(device));
  811. r852_disable_irqs(dev);
  812. r852_card_update_present(dev);
  813. r852_engine_disable(dev);
  814. /* If card status changed, just do the work */
  815. if (dev->card_detected != dev->card_registred) {
  816. dbg("card was %s during low power state",
  817. dev->card_detected ? "added" : "removed");
  818. queue_delayed_work(dev->card_workqueue,
  819. &dev->card_detect_work, msecs_to_jiffies(1000));
  820. return 0;
  821. }
  822. /* Otherwise, initialize the card */
  823. if (dev->card_registred) {
  824. r852_engine_enable(dev);
  825. dev->chip->select_chip(dev->mtd, 0);
  826. dev->chip->cmdfunc(dev->mtd, NAND_CMD_RESET, -1, -1);
  827. dev->chip->select_chip(dev->mtd, -1);
  828. }
  829. /* Program card detection IRQ */
  830. r852_update_card_detect(dev);
  831. return 0;
  832. }
  833. #endif
  834. static const struct pci_device_id r852_pci_id_tbl[] = {
  835. { PCI_VDEVICE(RICOH, 0x0852), },
  836. { },
  837. };
  838. MODULE_DEVICE_TABLE(pci, r852_pci_id_tbl);
  839. static SIMPLE_DEV_PM_OPS(r852_pm_ops, r852_suspend, r852_resume);
  840. static struct pci_driver r852_pci_driver = {
  841. .name = DRV_NAME,
  842. .id_table = r852_pci_id_tbl,
  843. .probe = r852_probe,
  844. .remove = r852_remove,
  845. .shutdown = r852_shutdown,
  846. .driver.pm = &r852_pm_ops,
  847. };
  848. module_pci_driver(r852_pci_driver);
  849. MODULE_LICENSE("GPL");
  850. MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
  851. MODULE_DESCRIPTION("Ricoh 85xx xD/smartmedia card reader driver");