r852.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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_to_nand(mtd);
  58. return nand_get_controller_data(chip);
  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 = nand_get_controller_data(chip);
  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 -EIO;
  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 = -EBADMSG;
  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. struct mtd_info *mtd = nand_to_mtd(dev->chip);
  510. WARN_ON(dev->card_registred);
  511. mtd->dev.parent = &dev->pci_dev->dev;
  512. if (dev->readonly)
  513. dev->chip->options |= NAND_ROM;
  514. r852_engine_enable(dev);
  515. if (sm_register_device(mtd, dev->sm))
  516. goto error1;
  517. if (device_create_file(&mtd->dev, &dev_attr_media_type)) {
  518. message("can't create media type sysfs attribute");
  519. goto error3;
  520. }
  521. dev->card_registred = 1;
  522. return 0;
  523. error3:
  524. nand_release(dev->chip);
  525. error1:
  526. /* Force card redetect */
  527. dev->card_detected = 0;
  528. return -1;
  529. }
  530. /*
  531. * Unregister the card
  532. */
  533. static void r852_unregister_nand_device(struct r852_device *dev)
  534. {
  535. struct mtd_info *mtd = nand_to_mtd(dev->chip);
  536. if (!dev->card_registred)
  537. return;
  538. device_remove_file(&mtd->dev, &dev_attr_media_type);
  539. nand_release(dev->chip);
  540. r852_engine_disable(dev);
  541. dev->card_registred = 0;
  542. }
  543. /* Card state updater */
  544. static void r852_card_detect_work(struct work_struct *work)
  545. {
  546. struct r852_device *dev =
  547. container_of(work, struct r852_device, card_detect_work.work);
  548. r852_card_update_present(dev);
  549. r852_update_card_detect(dev);
  550. dev->card_unstable = 0;
  551. /* False alarm */
  552. if (dev->card_detected == dev->card_registred)
  553. goto exit;
  554. /* Read media properties */
  555. r852_update_media_status(dev);
  556. /* Register the card */
  557. if (dev->card_detected)
  558. r852_register_nand_device(dev);
  559. else
  560. r852_unregister_nand_device(dev);
  561. exit:
  562. r852_update_card_detect(dev);
  563. }
  564. /* Ack + disable IRQ generation */
  565. static void r852_disable_irqs(struct r852_device *dev)
  566. {
  567. uint8_t reg;
  568. reg = r852_read_reg(dev, R852_CARD_IRQ_ENABLE);
  569. r852_write_reg(dev, R852_CARD_IRQ_ENABLE, reg & ~R852_CARD_IRQ_MASK);
  570. reg = r852_read_reg_dword(dev, R852_DMA_IRQ_ENABLE);
  571. r852_write_reg_dword(dev, R852_DMA_IRQ_ENABLE,
  572. reg & ~R852_DMA_IRQ_MASK);
  573. r852_write_reg(dev, R852_CARD_IRQ_STA, R852_CARD_IRQ_MASK);
  574. r852_write_reg_dword(dev, R852_DMA_IRQ_STA, R852_DMA_IRQ_MASK);
  575. }
  576. /* Interrupt handler */
  577. static irqreturn_t r852_irq(int irq, void *data)
  578. {
  579. struct r852_device *dev = (struct r852_device *)data;
  580. uint8_t card_status, dma_status;
  581. unsigned long flags;
  582. irqreturn_t ret = IRQ_NONE;
  583. spin_lock_irqsave(&dev->irqlock, flags);
  584. /* handle card detection interrupts first */
  585. card_status = r852_read_reg(dev, R852_CARD_IRQ_STA);
  586. r852_write_reg(dev, R852_CARD_IRQ_STA, card_status);
  587. if (card_status & (R852_CARD_IRQ_INSERT|R852_CARD_IRQ_REMOVE)) {
  588. ret = IRQ_HANDLED;
  589. dev->card_detected = !!(card_status & R852_CARD_IRQ_INSERT);
  590. /* we shouldn't receive any interrupts if we wait for card
  591. to settle */
  592. WARN_ON(dev->card_unstable);
  593. /* disable irqs while card is unstable */
  594. /* this will timeout DMA if active, but better that garbage */
  595. r852_disable_irqs(dev);
  596. if (dev->card_unstable)
  597. goto out;
  598. /* let, card state to settle a bit, and then do the work */
  599. dev->card_unstable = 1;
  600. queue_delayed_work(dev->card_workqueue,
  601. &dev->card_detect_work, msecs_to_jiffies(100));
  602. goto out;
  603. }
  604. /* Handle dma interrupts */
  605. dma_status = r852_read_reg_dword(dev, R852_DMA_IRQ_STA);
  606. r852_write_reg_dword(dev, R852_DMA_IRQ_STA, dma_status);
  607. if (dma_status & R852_DMA_IRQ_MASK) {
  608. ret = IRQ_HANDLED;
  609. if (dma_status & R852_DMA_IRQ_ERROR) {
  610. dbg("received dma error IRQ");
  611. r852_dma_done(dev, -EIO);
  612. complete(&dev->dma_done);
  613. goto out;
  614. }
  615. /* received DMA interrupt out of nowhere? */
  616. WARN_ON_ONCE(dev->dma_stage == 0);
  617. if (dev->dma_stage == 0)
  618. goto out;
  619. /* done device access */
  620. if (dev->dma_state == DMA_INTERNAL &&
  621. (dma_status & R852_DMA_IRQ_INTERNAL)) {
  622. dev->dma_state = DMA_MEMORY;
  623. dev->dma_stage++;
  624. }
  625. /* done memory DMA */
  626. if (dev->dma_state == DMA_MEMORY &&
  627. (dma_status & R852_DMA_IRQ_MEMORY)) {
  628. dev->dma_state = DMA_INTERNAL;
  629. dev->dma_stage++;
  630. }
  631. /* Enable 2nd half of dma dance */
  632. if (dev->dma_stage == 2)
  633. r852_dma_enable(dev);
  634. /* Operation done */
  635. if (dev->dma_stage == 3) {
  636. r852_dma_done(dev, 0);
  637. complete(&dev->dma_done);
  638. }
  639. goto out;
  640. }
  641. /* Handle unknown interrupts */
  642. if (dma_status)
  643. dbg("bad dma IRQ status = %x", dma_status);
  644. if (card_status & ~R852_CARD_STA_CD)
  645. dbg("strange card status = %x", card_status);
  646. out:
  647. spin_unlock_irqrestore(&dev->irqlock, flags);
  648. return ret;
  649. }
  650. static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
  651. {
  652. int error;
  653. struct nand_chip *chip;
  654. struct r852_device *dev;
  655. /* pci initialization */
  656. error = pci_enable_device(pci_dev);
  657. if (error)
  658. goto error1;
  659. pci_set_master(pci_dev);
  660. error = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
  661. if (error)
  662. goto error2;
  663. error = pci_request_regions(pci_dev, DRV_NAME);
  664. if (error)
  665. goto error3;
  666. error = -ENOMEM;
  667. /* init nand chip, but register it only on card insert */
  668. chip = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
  669. if (!chip)
  670. goto error4;
  671. /* commands */
  672. chip->cmd_ctrl = r852_cmdctl;
  673. chip->waitfunc = r852_wait;
  674. chip->dev_ready = r852_ready;
  675. /* I/O */
  676. chip->read_byte = r852_read_byte;
  677. chip->read_buf = r852_read_buf;
  678. chip->write_buf = r852_write_buf;
  679. /* ecc */
  680. chip->ecc.mode = NAND_ECC_HW_SYNDROME;
  681. chip->ecc.size = R852_DMA_LEN;
  682. chip->ecc.bytes = SM_OOB_SIZE;
  683. chip->ecc.strength = 2;
  684. chip->ecc.hwctl = r852_ecc_hwctl;
  685. chip->ecc.calculate = r852_ecc_calculate;
  686. chip->ecc.correct = r852_ecc_correct;
  687. /* TODO: hack */
  688. chip->ecc.read_oob = r852_read_oob;
  689. /* init our device structure */
  690. dev = kzalloc(sizeof(struct r852_device), GFP_KERNEL);
  691. if (!dev)
  692. goto error5;
  693. nand_set_controller_data(chip, dev);
  694. dev->chip = chip;
  695. dev->pci_dev = pci_dev;
  696. pci_set_drvdata(pci_dev, dev);
  697. dev->bounce_buffer = pci_alloc_consistent(pci_dev, R852_DMA_LEN,
  698. &dev->phys_bounce_buffer);
  699. if (!dev->bounce_buffer)
  700. goto error6;
  701. error = -ENODEV;
  702. dev->mmio = pci_ioremap_bar(pci_dev, 0);
  703. if (!dev->mmio)
  704. goto error7;
  705. error = -ENOMEM;
  706. dev->tmp_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL);
  707. if (!dev->tmp_buffer)
  708. goto error8;
  709. init_completion(&dev->dma_done);
  710. dev->card_workqueue = create_freezable_workqueue(DRV_NAME);
  711. if (!dev->card_workqueue)
  712. goto error9;
  713. INIT_DELAYED_WORK(&dev->card_detect_work, r852_card_detect_work);
  714. /* shutdown everything - precation */
  715. r852_engine_disable(dev);
  716. r852_disable_irqs(dev);
  717. r852_dma_test(dev);
  718. dev->irq = pci_dev->irq;
  719. spin_lock_init(&dev->irqlock);
  720. dev->card_detected = 0;
  721. r852_card_update_present(dev);
  722. /*register irq handler*/
  723. error = -ENODEV;
  724. if (request_irq(pci_dev->irq, &r852_irq, IRQF_SHARED,
  725. DRV_NAME, dev))
  726. goto error10;
  727. /* kick initial present test */
  728. queue_delayed_work(dev->card_workqueue,
  729. &dev->card_detect_work, 0);
  730. printk(KERN_NOTICE DRV_NAME ": driver loaded successfully\n");
  731. return 0;
  732. error10:
  733. destroy_workqueue(dev->card_workqueue);
  734. error9:
  735. kfree(dev->tmp_buffer);
  736. error8:
  737. pci_iounmap(pci_dev, dev->mmio);
  738. error7:
  739. pci_free_consistent(pci_dev, R852_DMA_LEN,
  740. dev->bounce_buffer, dev->phys_bounce_buffer);
  741. error6:
  742. kfree(dev);
  743. error5:
  744. kfree(chip);
  745. error4:
  746. pci_release_regions(pci_dev);
  747. error3:
  748. error2:
  749. pci_disable_device(pci_dev);
  750. error1:
  751. return error;
  752. }
  753. static void r852_remove(struct pci_dev *pci_dev)
  754. {
  755. struct r852_device *dev = pci_get_drvdata(pci_dev);
  756. /* Stop detect workqueue -
  757. we are going to unregister the device anyway*/
  758. cancel_delayed_work_sync(&dev->card_detect_work);
  759. destroy_workqueue(dev->card_workqueue);
  760. /* Unregister the device, this might make more IO */
  761. r852_unregister_nand_device(dev);
  762. /* Stop interrupts */
  763. r852_disable_irqs(dev);
  764. free_irq(dev->irq, dev);
  765. /* Cleanup */
  766. kfree(dev->tmp_buffer);
  767. pci_iounmap(pci_dev, dev->mmio);
  768. pci_free_consistent(pci_dev, R852_DMA_LEN,
  769. dev->bounce_buffer, dev->phys_bounce_buffer);
  770. kfree(dev->chip);
  771. kfree(dev);
  772. /* Shutdown the PCI device */
  773. pci_release_regions(pci_dev);
  774. pci_disable_device(pci_dev);
  775. }
  776. static void r852_shutdown(struct pci_dev *pci_dev)
  777. {
  778. struct r852_device *dev = pci_get_drvdata(pci_dev);
  779. cancel_delayed_work_sync(&dev->card_detect_work);
  780. r852_disable_irqs(dev);
  781. synchronize_irq(dev->irq);
  782. pci_disable_device(pci_dev);
  783. }
  784. #ifdef CONFIG_PM_SLEEP
  785. static int r852_suspend(struct device *device)
  786. {
  787. struct r852_device *dev = pci_get_drvdata(to_pci_dev(device));
  788. if (dev->ctlreg & R852_CTL_CARDENABLE)
  789. return -EBUSY;
  790. /* First make sure the detect work is gone */
  791. cancel_delayed_work_sync(&dev->card_detect_work);
  792. /* Turn off the interrupts and stop the device */
  793. r852_disable_irqs(dev);
  794. r852_engine_disable(dev);
  795. /* If card was pulled off just during the suspend, which is very
  796. unlikely, we will remove it on resume, it too late now
  797. anyway... */
  798. dev->card_unstable = 0;
  799. return 0;
  800. }
  801. static int r852_resume(struct device *device)
  802. {
  803. struct r852_device *dev = pci_get_drvdata(to_pci_dev(device));
  804. struct mtd_info *mtd = nand_to_mtd(dev->chip);
  805. r852_disable_irqs(dev);
  806. r852_card_update_present(dev);
  807. r852_engine_disable(dev);
  808. /* If card status changed, just do the work */
  809. if (dev->card_detected != dev->card_registred) {
  810. dbg("card was %s during low power state",
  811. dev->card_detected ? "added" : "removed");
  812. queue_delayed_work(dev->card_workqueue,
  813. &dev->card_detect_work, msecs_to_jiffies(1000));
  814. return 0;
  815. }
  816. /* Otherwise, initialize the card */
  817. if (dev->card_registred) {
  818. r852_engine_enable(dev);
  819. dev->chip->select_chip(mtd, 0);
  820. dev->chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  821. dev->chip->select_chip(mtd, -1);
  822. }
  823. /* Program card detection IRQ */
  824. r852_update_card_detect(dev);
  825. return 0;
  826. }
  827. #endif
  828. static const struct pci_device_id r852_pci_id_tbl[] = {
  829. { PCI_VDEVICE(RICOH, 0x0852), },
  830. { },
  831. };
  832. MODULE_DEVICE_TABLE(pci, r852_pci_id_tbl);
  833. static SIMPLE_DEV_PM_OPS(r852_pm_ops, r852_suspend, r852_resume);
  834. static struct pci_driver r852_pci_driver = {
  835. .name = DRV_NAME,
  836. .id_table = r852_pci_id_tbl,
  837. .probe = r852_probe,
  838. .remove = r852_remove,
  839. .shutdown = r852_shutdown,
  840. .driver.pm = &r852_pm_ops,
  841. };
  842. module_pci_driver(r852_pci_driver);
  843. MODULE_LICENSE("GPL");
  844. MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
  845. MODULE_DESCRIPTION("Ricoh 85xx xD/smartmedia card reader driver");