2 * Copyright © 2009 - Maxim Levitsky
3 * driver for Ricoh xD readers
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.
10 #define DRV_NAME "r852"
11 #define pr_fmt(fmt) DRV_NAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/jiffies.h>
16 #include <linux/workqueue.h>
17 #include <linux/interrupt.h>
18 #include <linux/pci.h>
19 #include <linux/pci_ids.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <asm/byteorder.h>
23 #include <linux/sched.h>
24 #include "sm_common.h"
28 static bool r852_enable_dma = 1;
29 module_param(r852_enable_dma, bool, S_IRUGO);
30 MODULE_PARM_DESC(r852_enable_dma, "Enable usage of the DMA (default)");
33 module_param(debug, int, S_IRUGO | S_IWUSR);
34 MODULE_PARM_DESC(debug, "Debug level (0-2)");
37 static inline uint8_t r852_read_reg(struct r852_device *dev, int address)
39 uint8_t reg = readb(dev->mmio + address);
44 static inline void r852_write_reg(struct r852_device *dev,
45 int address, uint8_t value)
47 writeb(value, dev->mmio + address);
51 /* read dword sized register */
52 static inline uint32_t r852_read_reg_dword(struct r852_device *dev, int address)
54 uint32_t reg = le32_to_cpu(readl(dev->mmio + address));
58 /* write dword sized register */
59 static inline void r852_write_reg_dword(struct r852_device *dev,
60 int address, uint32_t value)
62 writel(cpu_to_le32(value), dev->mmio + address);
65 /* returns pointer to our private structure */
66 static inline struct r852_device *r852_get_dev(struct mtd_info *mtd)
68 struct nand_chip *chip = mtd_to_nand(mtd);
69 return nand_get_controller_data(chip);
73 /* check if controller supports dma */
74 static void r852_dma_test(struct r852_device *dev)
76 dev->dma_usable = (r852_read_reg(dev, R852_DMA_CAP) &
77 (R852_DMA1 | R852_DMA2)) == (R852_DMA1 | R852_DMA2);
80 message("Non dma capable device detected, dma disabled");
82 if (!r852_enable_dma) {
83 message("disabling dma on user request");
89 * Enable dma. Enables ether first or second stage of the DMA,
90 * Expects dev->dma_dir and dev->dma_state be set
92 static void r852_dma_enable(struct r852_device *dev)
94 uint8_t dma_reg, dma_irq_reg;
96 /* Set up dma settings */
97 dma_reg = r852_read_reg_dword(dev, R852_DMA_SETTINGS);
98 dma_reg &= ~(R852_DMA_READ | R852_DMA_INTERNAL | R852_DMA_MEMORY);
101 dma_reg |= R852_DMA_READ;
103 if (dev->dma_state == DMA_INTERNAL) {
104 dma_reg |= R852_DMA_INTERNAL;
105 /* Precaution to make sure HW doesn't write */
106 /* to random kernel memory */
107 r852_write_reg_dword(dev, R852_DMA_ADDR,
108 cpu_to_le32(dev->phys_bounce_buffer));
110 dma_reg |= R852_DMA_MEMORY;
111 r852_write_reg_dword(dev, R852_DMA_ADDR,
112 cpu_to_le32(dev->phys_dma_addr));
115 /* Precaution: make sure write reached the device */
116 r852_read_reg_dword(dev, R852_DMA_ADDR);
118 r852_write_reg_dword(dev, R852_DMA_SETTINGS, dma_reg);
121 dma_irq_reg = r852_read_reg_dword(dev, R852_DMA_IRQ_ENABLE);
122 r852_write_reg_dword(dev, R852_DMA_IRQ_ENABLE,
124 R852_DMA_IRQ_INTERNAL |
126 R852_DMA_IRQ_MEMORY);
130 * Disable dma, called from the interrupt handler, which specifies
131 * success of the operation via 'error' argument
133 static void r852_dma_done(struct r852_device *dev, int error)
135 WARN_ON(dev->dma_stage == 0);
137 r852_write_reg_dword(dev, R852_DMA_IRQ_STA,
138 r852_read_reg_dword(dev, R852_DMA_IRQ_STA));
140 r852_write_reg_dword(dev, R852_DMA_SETTINGS, 0);
141 r852_write_reg_dword(dev, R852_DMA_IRQ_ENABLE, 0);
143 /* Precaution to make sure HW doesn't write to random kernel memory */
144 r852_write_reg_dword(dev, R852_DMA_ADDR,
145 cpu_to_le32(dev->phys_bounce_buffer));
146 r852_read_reg_dword(dev, R852_DMA_ADDR);
148 dev->dma_error = error;
151 if (dev->phys_dma_addr && dev->phys_dma_addr != dev->phys_bounce_buffer)
152 dma_unmap_single(&dev->pci_dev->dev, dev->phys_dma_addr,
154 dev->dma_dir ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
158 * Wait, till dma is done, which includes both phases of it
160 static int r852_dma_wait(struct r852_device *dev)
162 long timeout = wait_for_completion_timeout(&dev->dma_done,
163 msecs_to_jiffies(1000));
165 dbg("timeout waiting for DMA interrupt");
173 * Read/Write one page using dma. Only pages can be read (512 bytes)
175 static void r852_do_dma(struct r852_device *dev, uint8_t *buf, int do_read)
183 /* Set dma direction */
184 dev->dma_dir = do_read;
186 reinit_completion(&dev->dma_done);
188 dbg_verbose("doing dma %s ", do_read ? "read" : "write");
190 /* Set initial dma state: for reading first fill on board buffer,
191 from device, for writes first fill the buffer from memory*/
192 dev->dma_state = do_read ? DMA_INTERNAL : DMA_MEMORY;
194 /* if incoming buffer is not page aligned, we should do bounce */
195 if ((unsigned long)buf & (R852_DMA_LEN-1))
199 dev->phys_dma_addr = dma_map_single(&dev->pci_dev->dev, buf,
201 do_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
202 if (dma_mapping_error(&dev->pci_dev->dev, dev->phys_dma_addr))
207 dbg_verbose("dma: using bounce buffer");
208 dev->phys_dma_addr = dev->phys_bounce_buffer;
210 memcpy(dev->bounce_buffer, buf, R852_DMA_LEN);
214 spin_lock_irqsave(&dev->irqlock, flags);
215 r852_dma_enable(dev);
216 spin_unlock_irqrestore(&dev->irqlock, flags);
218 /* Wait till complete */
219 error = r852_dma_wait(dev);
222 r852_dma_done(dev, error);
226 if (do_read && bounce)
227 memcpy((void *)buf, dev->bounce_buffer, R852_DMA_LEN);
231 * Program data lines of the nand chip to send data to it
233 static void r852_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
235 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
238 /* Don't allow any access to hardware if we suspect card removal */
239 if (dev->card_unstable)
242 /* Special case for whole sector read */
243 if (len == R852_DMA_LEN && dev->dma_usable) {
244 r852_do_dma(dev, (uint8_t *)buf, 0);
248 /* write DWORD chinks - faster */
250 reg = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
251 r852_write_reg_dword(dev, R852_DATALINE, reg);
259 r852_write_reg(dev, R852_DATALINE, *buf++);
265 * Read data lines of the nand chip to retrieve data
267 static void r852_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
269 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
272 if (dev->card_unstable) {
273 /* since we can't signal error here, at least, return
274 predictable buffer */
279 /* special case for whole sector read */
280 if (len == R852_DMA_LEN && dev->dma_usable) {
281 r852_do_dma(dev, buf, 1);
285 /* read in dword sized chunks */
288 reg = r852_read_reg_dword(dev, R852_DATALINE);
290 *buf++ = (reg >> 8) & 0xFF;
291 *buf++ = (reg >> 16) & 0xFF;
292 *buf++ = (reg >> 24) & 0xFF;
296 /* read the reset by bytes */
298 *buf++ = r852_read_reg(dev, R852_DATALINE);
302 * Read one byte from nand chip
304 static uint8_t r852_read_byte(struct nand_chip *chip)
306 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
308 /* Same problem as in r852_read_buf.... */
309 if (dev->card_unstable)
312 return r852_read_reg(dev, R852_DATALINE);
316 * Control several chip lines & send commands
318 static void r852_cmdctl(struct nand_chip *chip, int dat, unsigned int ctrl)
320 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
322 if (dev->card_unstable)
325 if (ctrl & NAND_CTRL_CHANGE) {
327 dev->ctlreg &= ~(R852_CTL_DATA | R852_CTL_COMMAND |
328 R852_CTL_ON | R852_CTL_CARDENABLE);
331 dev->ctlreg |= R852_CTL_DATA;
334 dev->ctlreg |= R852_CTL_COMMAND;
337 dev->ctlreg |= (R852_CTL_CARDENABLE | R852_CTL_ON);
339 dev->ctlreg &= ~R852_CTL_WRITE;
341 /* when write is stareted, enable write access */
342 if (dat == NAND_CMD_ERASE1)
343 dev->ctlreg |= R852_CTL_WRITE;
345 r852_write_reg(dev, R852_CTL, dev->ctlreg);
348 /* HACK: NAND_CMD_SEQIN is called without NAND_CTRL_CHANGE, but we need
350 if (dat == NAND_CMD_SEQIN && (dev->ctlreg & R852_CTL_COMMAND)) {
351 dev->ctlreg |= R852_CTL_WRITE;
352 r852_write_reg(dev, R852_CTL, dev->ctlreg);
355 if (dat != NAND_CMD_NONE)
356 r852_write_reg(dev, R852_DATALINE, dat);
360 * Wait till card is ready.
361 * based on nand_wait, but returns errors on DMA error
363 static int r852_wait(struct nand_chip *chip)
365 struct r852_device *dev = nand_get_controller_data(chip);
367 unsigned long timeout;
370 timeout = jiffies + msecs_to_jiffies(400);
372 while (time_before(jiffies, timeout))
373 if (chip->legacy.dev_ready(chip))
376 nand_status_op(chip, &status);
378 /* Unfortunelly, no way to send detailed error status... */
379 if (dev->dma_error) {
380 status |= NAND_STATUS_FAIL;
387 * Check if card is ready
390 static int r852_ready(struct nand_chip *chip)
392 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
393 return !(r852_read_reg(dev, R852_CARD_STA) & R852_CARD_STA_BUSY);
398 * Set ECC engine mode
401 static void r852_ecc_hwctl(struct nand_chip *chip, int mode)
403 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
405 if (dev->card_unstable)
411 /* enable ecc generation/check*/
412 dev->ctlreg |= R852_CTL_ECC_ENABLE;
414 /* flush ecc buffer */
415 r852_write_reg(dev, R852_CTL,
416 dev->ctlreg | R852_CTL_ECC_ACCESS);
418 r852_read_reg_dword(dev, R852_DATALINE);
419 r852_write_reg(dev, R852_CTL, dev->ctlreg);
422 case NAND_ECC_READSYN:
423 /* disable ecc generation */
424 dev->ctlreg &= ~R852_CTL_ECC_ENABLE;
425 r852_write_reg(dev, R852_CTL, dev->ctlreg);
430 * Calculate ECC, only used for writes
433 static int r852_ecc_calculate(struct nand_chip *chip, const uint8_t *dat,
436 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
437 struct sm_oob *oob = (struct sm_oob *)ecc_code;
440 if (dev->card_unstable)
443 dev->ctlreg &= ~R852_CTL_ECC_ENABLE;
444 r852_write_reg(dev, R852_CTL, dev->ctlreg | R852_CTL_ECC_ACCESS);
446 ecc1 = r852_read_reg_dword(dev, R852_DATALINE);
447 ecc2 = r852_read_reg_dword(dev, R852_DATALINE);
449 oob->ecc1[0] = (ecc1) & 0xFF;
450 oob->ecc1[1] = (ecc1 >> 8) & 0xFF;
451 oob->ecc1[2] = (ecc1 >> 16) & 0xFF;
453 oob->ecc2[0] = (ecc2) & 0xFF;
454 oob->ecc2[1] = (ecc2 >> 8) & 0xFF;
455 oob->ecc2[2] = (ecc2 >> 16) & 0xFF;
457 r852_write_reg(dev, R852_CTL, dev->ctlreg);
462 * Correct the data using ECC, hw did almost everything for us
465 static int r852_ecc_correct(struct nand_chip *chip, uint8_t *dat,
466 uint8_t *read_ecc, uint8_t *calc_ecc)
469 uint8_t ecc_status, err_byte;
472 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
474 if (dev->card_unstable)
477 if (dev->dma_error) {
482 r852_write_reg(dev, R852_CTL, dev->ctlreg | R852_CTL_ECC_ACCESS);
483 ecc_reg = r852_read_reg_dword(dev, R852_DATALINE);
484 r852_write_reg(dev, R852_CTL, dev->ctlreg);
486 for (i = 0 ; i <= 1 ; i++) {
488 ecc_status = (ecc_reg >> 8) & 0xFF;
490 /* ecc uncorrectable error */
491 if (ecc_status & R852_ECC_FAIL) {
492 dbg("ecc: unrecoverable error, in half %d", i);
497 /* correctable error */
498 if (ecc_status & R852_ECC_CORRECTABLE) {
500 err_byte = ecc_reg & 0xFF;
501 dbg("ecc: recoverable error, "
502 "in half %d, byte %d, bit %d", i,
503 err_byte, ecc_status & R852_ECC_ERR_BIT_MSK);
506 1 << (ecc_status & R852_ECC_ERR_BIT_MSK);
518 * This is copy of nand_read_oob_std
519 * nand_read_oob_syndrome assumes we can send column address - we can't
521 static int r852_read_oob(struct nand_chip *chip, int page)
523 struct mtd_info *mtd = nand_to_mtd(chip);
525 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
529 * Start the nand engine
532 static void r852_engine_enable(struct r852_device *dev)
534 if (r852_read_reg_dword(dev, R852_HW) & R852_HW_UNKNOWN) {
535 r852_write_reg(dev, R852_CTL, R852_CTL_RESET | R852_CTL_ON);
536 r852_write_reg_dword(dev, R852_HW, R852_HW_ENABLED);
538 r852_write_reg_dword(dev, R852_HW, R852_HW_ENABLED);
539 r852_write_reg(dev, R852_CTL, R852_CTL_RESET | R852_CTL_ON);
542 r852_write_reg(dev, R852_CTL, 0);
547 * Stop the nand engine
550 static void r852_engine_disable(struct r852_device *dev)
552 r852_write_reg_dword(dev, R852_HW, 0);
553 r852_write_reg(dev, R852_CTL, R852_CTL_RESET);
557 * Test if card is present
560 static void r852_card_update_present(struct r852_device *dev)
565 spin_lock_irqsave(&dev->irqlock, flags);
566 reg = r852_read_reg(dev, R852_CARD_STA);
567 dev->card_detected = !!(reg & R852_CARD_STA_PRESENT);
568 spin_unlock_irqrestore(&dev->irqlock, flags);
572 * Update card detection IRQ state according to current card state
573 * which is read in r852_card_update_present
575 static void r852_update_card_detect(struct r852_device *dev)
577 int card_detect_reg = r852_read_reg(dev, R852_CARD_IRQ_ENABLE);
578 dev->card_unstable = 0;
580 card_detect_reg &= ~(R852_CARD_IRQ_REMOVE | R852_CARD_IRQ_INSERT);
581 card_detect_reg |= R852_CARD_IRQ_GENABLE;
583 card_detect_reg |= dev->card_detected ?
584 R852_CARD_IRQ_REMOVE : R852_CARD_IRQ_INSERT;
586 r852_write_reg(dev, R852_CARD_IRQ_ENABLE, card_detect_reg);
589 static ssize_t r852_media_type_show(struct device *sys_dev,
590 struct device_attribute *attr, char *buf)
592 struct mtd_info *mtd = container_of(sys_dev, struct mtd_info, dev);
593 struct r852_device *dev = r852_get_dev(mtd);
594 char *data = dev->sm ? "smartmedia" : "xd";
600 static DEVICE_ATTR(media_type, S_IRUGO, r852_media_type_show, NULL);
603 /* Detect properties of card in slot */
604 static void r852_update_media_status(struct r852_device *dev)
610 spin_lock_irqsave(&dev->irqlock, flags);
611 if (!dev->card_detected) {
612 message("card removed");
613 spin_unlock_irqrestore(&dev->irqlock, flags);
617 readonly = r852_read_reg(dev, R852_CARD_STA) & R852_CARD_STA_RO;
618 reg = r852_read_reg(dev, R852_DMA_CAP);
619 dev->sm = (reg & (R852_DMA1 | R852_DMA2)) && (reg & R852_SMBIT);
621 message("detected %s %s card in slot",
622 dev->sm ? "SmartMedia" : "xD",
623 readonly ? "readonly" : "writeable");
625 dev->readonly = readonly;
626 spin_unlock_irqrestore(&dev->irqlock, flags);
630 * Register the nand device
631 * Called when the card is detected
633 static int r852_register_nand_device(struct r852_device *dev)
635 struct mtd_info *mtd = nand_to_mtd(dev->chip);
637 WARN_ON(dev->card_registered);
639 mtd->dev.parent = &dev->pci_dev->dev;
642 dev->chip->options |= NAND_ROM;
644 r852_engine_enable(dev);
646 if (sm_register_device(mtd, dev->sm))
649 if (device_create_file(&mtd->dev, &dev_attr_media_type)) {
650 message("can't create media type sysfs attribute");
654 dev->card_registered = 1;
657 nand_release(dev->chip);
659 /* Force card redetect */
660 dev->card_detected = 0;
665 * Unregister the card
668 static void r852_unregister_nand_device(struct r852_device *dev)
670 struct mtd_info *mtd = nand_to_mtd(dev->chip);
672 if (!dev->card_registered)
675 device_remove_file(&mtd->dev, &dev_attr_media_type);
676 nand_release(dev->chip);
677 r852_engine_disable(dev);
678 dev->card_registered = 0;
681 /* Card state updater */
682 static void r852_card_detect_work(struct work_struct *work)
684 struct r852_device *dev =
685 container_of(work, struct r852_device, card_detect_work.work);
687 r852_card_update_present(dev);
688 r852_update_card_detect(dev);
689 dev->card_unstable = 0;
692 if (dev->card_detected == dev->card_registered)
695 /* Read media properties */
696 r852_update_media_status(dev);
698 /* Register the card */
699 if (dev->card_detected)
700 r852_register_nand_device(dev);
702 r852_unregister_nand_device(dev);
704 r852_update_card_detect(dev);
707 /* Ack + disable IRQ generation */
708 static void r852_disable_irqs(struct r852_device *dev)
711 reg = r852_read_reg(dev, R852_CARD_IRQ_ENABLE);
712 r852_write_reg(dev, R852_CARD_IRQ_ENABLE, reg & ~R852_CARD_IRQ_MASK);
714 reg = r852_read_reg_dword(dev, R852_DMA_IRQ_ENABLE);
715 r852_write_reg_dword(dev, R852_DMA_IRQ_ENABLE,
716 reg & ~R852_DMA_IRQ_MASK);
718 r852_write_reg(dev, R852_CARD_IRQ_STA, R852_CARD_IRQ_MASK);
719 r852_write_reg_dword(dev, R852_DMA_IRQ_STA, R852_DMA_IRQ_MASK);
722 /* Interrupt handler */
723 static irqreturn_t r852_irq(int irq, void *data)
725 struct r852_device *dev = (struct r852_device *)data;
727 uint8_t card_status, dma_status;
729 irqreturn_t ret = IRQ_NONE;
731 spin_lock_irqsave(&dev->irqlock, flags);
733 /* handle card detection interrupts first */
734 card_status = r852_read_reg(dev, R852_CARD_IRQ_STA);
735 r852_write_reg(dev, R852_CARD_IRQ_STA, card_status);
737 if (card_status & (R852_CARD_IRQ_INSERT|R852_CARD_IRQ_REMOVE)) {
740 dev->card_detected = !!(card_status & R852_CARD_IRQ_INSERT);
742 /* we shouldn't receive any interrupts if we wait for card
744 WARN_ON(dev->card_unstable);
746 /* disable irqs while card is unstable */
747 /* this will timeout DMA if active, but better that garbage */
748 r852_disable_irqs(dev);
750 if (dev->card_unstable)
753 /* let, card state to settle a bit, and then do the work */
754 dev->card_unstable = 1;
755 queue_delayed_work(dev->card_workqueue,
756 &dev->card_detect_work, msecs_to_jiffies(100));
761 /* Handle dma interrupts */
762 dma_status = r852_read_reg_dword(dev, R852_DMA_IRQ_STA);
763 r852_write_reg_dword(dev, R852_DMA_IRQ_STA, dma_status);
765 if (dma_status & R852_DMA_IRQ_MASK) {
769 if (dma_status & R852_DMA_IRQ_ERROR) {
770 dbg("received dma error IRQ");
771 r852_dma_done(dev, -EIO);
772 complete(&dev->dma_done);
776 /* received DMA interrupt out of nowhere? */
777 WARN_ON_ONCE(dev->dma_stage == 0);
779 if (dev->dma_stage == 0)
782 /* done device access */
783 if (dev->dma_state == DMA_INTERNAL &&
784 (dma_status & R852_DMA_IRQ_INTERNAL)) {
786 dev->dma_state = DMA_MEMORY;
790 /* done memory DMA */
791 if (dev->dma_state == DMA_MEMORY &&
792 (dma_status & R852_DMA_IRQ_MEMORY)) {
793 dev->dma_state = DMA_INTERNAL;
797 /* Enable 2nd half of dma dance */
798 if (dev->dma_stage == 2)
799 r852_dma_enable(dev);
802 if (dev->dma_stage == 3) {
803 r852_dma_done(dev, 0);
804 complete(&dev->dma_done);
809 /* Handle unknown interrupts */
811 dbg("bad dma IRQ status = %x", dma_status);
813 if (card_status & ~R852_CARD_STA_CD)
814 dbg("strange card status = %x", card_status);
817 spin_unlock_irqrestore(&dev->irqlock, flags);
821 static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
824 struct nand_chip *chip;
825 struct r852_device *dev;
827 /* pci initialization */
828 error = pci_enable_device(pci_dev);
833 pci_set_master(pci_dev);
835 error = dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32));
839 error = pci_request_regions(pci_dev, DRV_NAME);
846 /* init nand chip, but register it only on card insert */
847 chip = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
853 chip->legacy.cmd_ctrl = r852_cmdctl;
854 chip->legacy.waitfunc = r852_wait;
855 chip->legacy.dev_ready = r852_ready;
858 chip->legacy.read_byte = r852_read_byte;
859 chip->legacy.read_buf = r852_read_buf;
860 chip->legacy.write_buf = r852_write_buf;
863 chip->ecc.mode = NAND_ECC_HW_SYNDROME;
864 chip->ecc.size = R852_DMA_LEN;
865 chip->ecc.bytes = SM_OOB_SIZE;
866 chip->ecc.strength = 2;
867 chip->ecc.hwctl = r852_ecc_hwctl;
868 chip->ecc.calculate = r852_ecc_calculate;
869 chip->ecc.correct = r852_ecc_correct;
872 chip->ecc.read_oob = r852_read_oob;
874 /* init our device structure */
875 dev = kzalloc(sizeof(struct r852_device), GFP_KERNEL);
880 nand_set_controller_data(chip, dev);
882 dev->pci_dev = pci_dev;
883 pci_set_drvdata(pci_dev, dev);
885 dev->bounce_buffer = dma_alloc_coherent(&pci_dev->dev, R852_DMA_LEN,
886 &dev->phys_bounce_buffer, GFP_KERNEL);
888 if (!dev->bounce_buffer)
893 dev->mmio = pci_ioremap_bar(pci_dev, 0);
899 dev->tmp_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL);
901 if (!dev->tmp_buffer)
904 init_completion(&dev->dma_done);
906 dev->card_workqueue = create_freezable_workqueue(DRV_NAME);
908 if (!dev->card_workqueue)
911 INIT_DELAYED_WORK(&dev->card_detect_work, r852_card_detect_work);
913 /* shutdown everything - precation */
914 r852_engine_disable(dev);
915 r852_disable_irqs(dev);
919 dev->irq = pci_dev->irq;
920 spin_lock_init(&dev->irqlock);
922 dev->card_detected = 0;
923 r852_card_update_present(dev);
925 /*register irq handler*/
927 if (request_irq(pci_dev->irq, &r852_irq, IRQF_SHARED,
931 /* kick initial present test */
932 queue_delayed_work(dev->card_workqueue,
933 &dev->card_detect_work, 0);
936 pr_notice("driver loaded successfully\n");
940 destroy_workqueue(dev->card_workqueue);
942 kfree(dev->tmp_buffer);
944 pci_iounmap(pci_dev, dev->mmio);
946 dma_free_coherent(&pci_dev->dev, R852_DMA_LEN, dev->bounce_buffer,
947 dev->phys_bounce_buffer);
953 pci_release_regions(pci_dev);
956 pci_disable_device(pci_dev);
961 static void r852_remove(struct pci_dev *pci_dev)
963 struct r852_device *dev = pci_get_drvdata(pci_dev);
965 /* Stop detect workqueue -
966 we are going to unregister the device anyway*/
967 cancel_delayed_work_sync(&dev->card_detect_work);
968 destroy_workqueue(dev->card_workqueue);
970 /* Unregister the device, this might make more IO */
971 r852_unregister_nand_device(dev);
973 /* Stop interrupts */
974 r852_disable_irqs(dev);
975 free_irq(dev->irq, dev);
978 kfree(dev->tmp_buffer);
979 pci_iounmap(pci_dev, dev->mmio);
980 dma_free_coherent(&pci_dev->dev, R852_DMA_LEN, dev->bounce_buffer,
981 dev->phys_bounce_buffer);
986 /* Shutdown the PCI device */
987 pci_release_regions(pci_dev);
988 pci_disable_device(pci_dev);
991 static void r852_shutdown(struct pci_dev *pci_dev)
993 struct r852_device *dev = pci_get_drvdata(pci_dev);
995 cancel_delayed_work_sync(&dev->card_detect_work);
996 r852_disable_irqs(dev);
997 synchronize_irq(dev->irq);
998 pci_disable_device(pci_dev);
1001 #ifdef CONFIG_PM_SLEEP
1002 static int r852_suspend(struct device *device)
1004 struct r852_device *dev = pci_get_drvdata(to_pci_dev(device));
1006 if (dev->ctlreg & R852_CTL_CARDENABLE)
1009 /* First make sure the detect work is gone */
1010 cancel_delayed_work_sync(&dev->card_detect_work);
1012 /* Turn off the interrupts and stop the device */
1013 r852_disable_irqs(dev);
1014 r852_engine_disable(dev);
1016 /* If card was pulled off just during the suspend, which is very
1017 unlikely, we will remove it on resume, it too late now
1019 dev->card_unstable = 0;
1023 static int r852_resume(struct device *device)
1025 struct r852_device *dev = pci_get_drvdata(to_pci_dev(device));
1027 r852_disable_irqs(dev);
1028 r852_card_update_present(dev);
1029 r852_engine_disable(dev);
1032 /* If card status changed, just do the work */
1033 if (dev->card_detected != dev->card_registered) {
1034 dbg("card was %s during low power state",
1035 dev->card_detected ? "added" : "removed");
1037 queue_delayed_work(dev->card_workqueue,
1038 &dev->card_detect_work, msecs_to_jiffies(1000));
1042 /* Otherwise, initialize the card */
1043 if (dev->card_registered) {
1044 r852_engine_enable(dev);
1045 nand_select_target(dev->chip, 0);
1046 nand_reset_op(dev->chip);
1047 nand_deselect_target(dev->chip);
1050 /* Program card detection IRQ */
1051 r852_update_card_detect(dev);
1056 static const struct pci_device_id r852_pci_id_tbl[] = {
1058 { PCI_VDEVICE(RICOH, 0x0852), },
1062 MODULE_DEVICE_TABLE(pci, r852_pci_id_tbl);
1064 static SIMPLE_DEV_PM_OPS(r852_pm_ops, r852_suspend, r852_resume);
1066 static struct pci_driver r852_pci_driver = {
1068 .id_table = r852_pci_id_tbl,
1069 .probe = r852_probe,
1070 .remove = r852_remove,
1071 .shutdown = r852_shutdown,
1072 .driver.pm = &r852_pm_ops,
1075 module_pci_driver(r852_pci_driver);
1077 MODULE_LICENSE("GPL");
1078 MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
1079 MODULE_DESCRIPTION("Ricoh 85xx xD/smartmedia card reader driver");