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);
52 /* read dword sized register */
53 static inline uint32_t r852_read_reg_dword(struct r852_device *dev, int address)
55 uint32_t reg = le32_to_cpu(readl(dev->mmio + address));
59 /* write dword sized register */
60 static inline void r852_write_reg_dword(struct r852_device *dev,
61 int address, uint32_t value)
63 writel(cpu_to_le32(value), dev->mmio + address);
67 /* returns pointer to our private structure */
68 static inline struct r852_device *r852_get_dev(struct mtd_info *mtd)
70 struct nand_chip *chip = mtd_to_nand(mtd);
71 return nand_get_controller_data(chip);
75 /* check if controller supports dma */
76 static void r852_dma_test(struct r852_device *dev)
78 dev->dma_usable = (r852_read_reg(dev, R852_DMA_CAP) &
79 (R852_DMA1 | R852_DMA2)) == (R852_DMA1 | R852_DMA2);
82 message("Non dma capable device detected, dma disabled");
84 if (!r852_enable_dma) {
85 message("disabling dma on user request");
91 * Enable dma. Enables ether first or second stage of the DMA,
92 * Expects dev->dma_dir and dev->dma_state be set
94 static void r852_dma_enable(struct r852_device *dev)
96 uint8_t dma_reg, dma_irq_reg;
98 /* Set up dma settings */
99 dma_reg = r852_read_reg_dword(dev, R852_DMA_SETTINGS);
100 dma_reg &= ~(R852_DMA_READ | R852_DMA_INTERNAL | R852_DMA_MEMORY);
103 dma_reg |= R852_DMA_READ;
105 if (dev->dma_state == DMA_INTERNAL) {
106 dma_reg |= R852_DMA_INTERNAL;
107 /* Precaution to make sure HW doesn't write */
108 /* to random kernel memory */
109 r852_write_reg_dword(dev, R852_DMA_ADDR,
110 cpu_to_le32(dev->phys_bounce_buffer));
112 dma_reg |= R852_DMA_MEMORY;
113 r852_write_reg_dword(dev, R852_DMA_ADDR,
114 cpu_to_le32(dev->phys_dma_addr));
117 /* Precaution: make sure write reached the device */
118 r852_read_reg_dword(dev, R852_DMA_ADDR);
120 r852_write_reg_dword(dev, R852_DMA_SETTINGS, dma_reg);
123 dma_irq_reg = r852_read_reg_dword(dev, R852_DMA_IRQ_ENABLE);
124 r852_write_reg_dword(dev, R852_DMA_IRQ_ENABLE,
126 R852_DMA_IRQ_INTERNAL |
128 R852_DMA_IRQ_MEMORY);
132 * Disable dma, called from the interrupt handler, which specifies
133 * success of the operation via 'error' argument
135 static void r852_dma_done(struct r852_device *dev, int error)
137 WARN_ON(dev->dma_stage == 0);
139 r852_write_reg_dword(dev, R852_DMA_IRQ_STA,
140 r852_read_reg_dword(dev, R852_DMA_IRQ_STA));
142 r852_write_reg_dword(dev, R852_DMA_SETTINGS, 0);
143 r852_write_reg_dword(dev, R852_DMA_IRQ_ENABLE, 0);
145 /* Precaution to make sure HW doesn't write to random kernel memory */
146 r852_write_reg_dword(dev, R852_DMA_ADDR,
147 cpu_to_le32(dev->phys_bounce_buffer));
148 r852_read_reg_dword(dev, R852_DMA_ADDR);
150 dev->dma_error = error;
153 if (dev->phys_dma_addr && dev->phys_dma_addr != dev->phys_bounce_buffer)
154 dma_unmap_single(&dev->pci_dev->dev, dev->phys_dma_addr,
156 dev->dma_dir ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
160 * Wait, till dma is done, which includes both phases of it
162 static int r852_dma_wait(struct r852_device *dev)
164 long timeout = wait_for_completion_timeout(&dev->dma_done,
165 msecs_to_jiffies(1000));
167 dbg("timeout waiting for DMA interrupt");
175 * Read/Write one page using dma. Only pages can be read (512 bytes)
177 static void r852_do_dma(struct r852_device *dev, uint8_t *buf, int do_read)
185 /* Set dma direction */
186 dev->dma_dir = do_read;
188 reinit_completion(&dev->dma_done);
190 dbg_verbose("doing dma %s ", do_read ? "read" : "write");
192 /* Set initial dma state: for reading first fill on board buffer,
193 from device, for writes first fill the buffer from memory*/
194 dev->dma_state = do_read ? DMA_INTERNAL : DMA_MEMORY;
196 /* if incoming buffer is not page aligned, we should do bounce */
197 if ((unsigned long)buf & (R852_DMA_LEN-1))
201 dev->phys_dma_addr = dma_map_single(&dev->pci_dev->dev, buf,
203 do_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
204 if (dma_mapping_error(&dev->pci_dev->dev, dev->phys_dma_addr))
209 dbg_verbose("dma: using bounce buffer");
210 dev->phys_dma_addr = dev->phys_bounce_buffer;
212 memcpy(dev->bounce_buffer, buf, R852_DMA_LEN);
216 spin_lock_irqsave(&dev->irqlock, flags);
217 r852_dma_enable(dev);
218 spin_unlock_irqrestore(&dev->irqlock, flags);
220 /* Wait till complete */
221 error = r852_dma_wait(dev);
224 r852_dma_done(dev, error);
228 if (do_read && bounce)
229 memcpy((void *)buf, dev->bounce_buffer, R852_DMA_LEN);
233 * Program data lines of the nand chip to send data to it
235 static void r852_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
237 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
240 /* Don't allow any access to hardware if we suspect card removal */
241 if (dev->card_unstable)
244 /* Special case for whole sector read */
245 if (len == R852_DMA_LEN && dev->dma_usable) {
246 r852_do_dma(dev, (uint8_t *)buf, 0);
250 /* write DWORD chinks - faster */
252 reg = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
253 r852_write_reg_dword(dev, R852_DATALINE, reg);
261 r852_write_reg(dev, R852_DATALINE, *buf++);
267 * Read data lines of the nand chip to retrieve data
269 static void r852_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
271 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
274 if (dev->card_unstable) {
275 /* since we can't signal error here, at least, return
276 predictable buffer */
281 /* special case for whole sector read */
282 if (len == R852_DMA_LEN && dev->dma_usable) {
283 r852_do_dma(dev, buf, 1);
287 /* read in dword sized chunks */
290 reg = r852_read_reg_dword(dev, R852_DATALINE);
292 *buf++ = (reg >> 8) & 0xFF;
293 *buf++ = (reg >> 16) & 0xFF;
294 *buf++ = (reg >> 24) & 0xFF;
298 /* read the reset by bytes */
300 *buf++ = r852_read_reg(dev, R852_DATALINE);
304 * Read one byte from nand chip
306 static uint8_t r852_read_byte(struct nand_chip *chip)
308 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
310 /* Same problem as in r852_read_buf.... */
311 if (dev->card_unstable)
314 return r852_read_reg(dev, R852_DATALINE);
318 * Control several chip lines & send commands
320 static void r852_cmdctl(struct nand_chip *chip, int dat, unsigned int ctrl)
322 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
324 if (dev->card_unstable)
327 if (ctrl & NAND_CTRL_CHANGE) {
329 dev->ctlreg &= ~(R852_CTL_DATA | R852_CTL_COMMAND |
330 R852_CTL_ON | R852_CTL_CARDENABLE);
333 dev->ctlreg |= R852_CTL_DATA;
336 dev->ctlreg |= R852_CTL_COMMAND;
339 dev->ctlreg |= (R852_CTL_CARDENABLE | R852_CTL_ON);
341 dev->ctlreg &= ~R852_CTL_WRITE;
343 /* when write is stareted, enable write access */
344 if (dat == NAND_CMD_ERASE1)
345 dev->ctlreg |= R852_CTL_WRITE;
347 r852_write_reg(dev, R852_CTL, dev->ctlreg);
350 /* HACK: NAND_CMD_SEQIN is called without NAND_CTRL_CHANGE, but we need
352 if (dat == NAND_CMD_SEQIN && (dev->ctlreg & R852_CTL_COMMAND)) {
353 dev->ctlreg |= R852_CTL_WRITE;
354 r852_write_reg(dev, R852_CTL, dev->ctlreg);
357 if (dat != NAND_CMD_NONE)
358 r852_write_reg(dev, R852_DATALINE, dat);
362 * Wait till card is ready.
363 * based on nand_wait, but returns errors on DMA error
365 static int r852_wait(struct nand_chip *chip)
367 struct r852_device *dev = nand_get_controller_data(chip);
369 unsigned long timeout;
372 timeout = jiffies + msecs_to_jiffies(400);
374 while (time_before(jiffies, timeout))
375 if (chip->legacy.dev_ready(chip))
378 nand_status_op(chip, &status);
380 /* Unfortunelly, no way to send detailed error status... */
381 if (dev->dma_error) {
382 status |= NAND_STATUS_FAIL;
389 * Check if card is ready
392 static int r852_ready(struct nand_chip *chip)
394 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
395 return !(r852_read_reg(dev, R852_CARD_STA) & R852_CARD_STA_BUSY);
400 * Set ECC engine mode
403 static void r852_ecc_hwctl(struct nand_chip *chip, int mode)
405 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
407 if (dev->card_unstable)
413 /* enable ecc generation/check*/
414 dev->ctlreg |= R852_CTL_ECC_ENABLE;
416 /* flush ecc buffer */
417 r852_write_reg(dev, R852_CTL,
418 dev->ctlreg | R852_CTL_ECC_ACCESS);
420 r852_read_reg_dword(dev, R852_DATALINE);
421 r852_write_reg(dev, R852_CTL, dev->ctlreg);
424 case NAND_ECC_READSYN:
425 /* disable ecc generation */
426 dev->ctlreg &= ~R852_CTL_ECC_ENABLE;
427 r852_write_reg(dev, R852_CTL, dev->ctlreg);
432 * Calculate ECC, only used for writes
435 static int r852_ecc_calculate(struct nand_chip *chip, const uint8_t *dat,
438 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
439 struct sm_oob *oob = (struct sm_oob *)ecc_code;
442 if (dev->card_unstable)
445 dev->ctlreg &= ~R852_CTL_ECC_ENABLE;
446 r852_write_reg(dev, R852_CTL, dev->ctlreg | R852_CTL_ECC_ACCESS);
448 ecc1 = r852_read_reg_dword(dev, R852_DATALINE);
449 ecc2 = r852_read_reg_dword(dev, R852_DATALINE);
451 oob->ecc1[0] = (ecc1) & 0xFF;
452 oob->ecc1[1] = (ecc1 >> 8) & 0xFF;
453 oob->ecc1[2] = (ecc1 >> 16) & 0xFF;
455 oob->ecc2[0] = (ecc2) & 0xFF;
456 oob->ecc2[1] = (ecc2 >> 8) & 0xFF;
457 oob->ecc2[2] = (ecc2 >> 16) & 0xFF;
459 r852_write_reg(dev, R852_CTL, dev->ctlreg);
464 * Correct the data using ECC, hw did almost everything for us
467 static int r852_ecc_correct(struct nand_chip *chip, uint8_t *dat,
468 uint8_t *read_ecc, uint8_t *calc_ecc)
471 uint8_t ecc_status, err_byte;
474 struct r852_device *dev = r852_get_dev(nand_to_mtd(chip));
476 if (dev->card_unstable)
479 if (dev->dma_error) {
484 r852_write_reg(dev, R852_CTL, dev->ctlreg | R852_CTL_ECC_ACCESS);
485 ecc_reg = r852_read_reg_dword(dev, R852_DATALINE);
486 r852_write_reg(dev, R852_CTL, dev->ctlreg);
488 for (i = 0 ; i <= 1 ; i++) {
490 ecc_status = (ecc_reg >> 8) & 0xFF;
492 /* ecc uncorrectable error */
493 if (ecc_status & R852_ECC_FAIL) {
494 dbg("ecc: unrecoverable error, in half %d", i);
499 /* correctable error */
500 if (ecc_status & R852_ECC_CORRECTABLE) {
502 err_byte = ecc_reg & 0xFF;
503 dbg("ecc: recoverable error, "
504 "in half %d, byte %d, bit %d", i,
505 err_byte, ecc_status & R852_ECC_ERR_BIT_MSK);
508 1 << (ecc_status & R852_ECC_ERR_BIT_MSK);
520 * This is copy of nand_read_oob_std
521 * nand_read_oob_syndrome assumes we can send column address - we can't
523 static int r852_read_oob(struct nand_chip *chip, int page)
525 struct mtd_info *mtd = nand_to_mtd(chip);
527 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
531 * Start the nand engine
534 static void r852_engine_enable(struct r852_device *dev)
536 if (r852_read_reg_dword(dev, R852_HW) & R852_HW_UNKNOWN) {
537 r852_write_reg(dev, R852_CTL, R852_CTL_RESET | R852_CTL_ON);
538 r852_write_reg_dword(dev, R852_HW, R852_HW_ENABLED);
540 r852_write_reg_dword(dev, R852_HW, R852_HW_ENABLED);
541 r852_write_reg(dev, R852_CTL, R852_CTL_RESET | R852_CTL_ON);
544 r852_write_reg(dev, R852_CTL, 0);
549 * Stop the nand engine
552 static void r852_engine_disable(struct r852_device *dev)
554 r852_write_reg_dword(dev, R852_HW, 0);
555 r852_write_reg(dev, R852_CTL, R852_CTL_RESET);
559 * Test if card is present
562 static void r852_card_update_present(struct r852_device *dev)
567 spin_lock_irqsave(&dev->irqlock, flags);
568 reg = r852_read_reg(dev, R852_CARD_STA);
569 dev->card_detected = !!(reg & R852_CARD_STA_PRESENT);
570 spin_unlock_irqrestore(&dev->irqlock, flags);
574 * Update card detection IRQ state according to current card state
575 * which is read in r852_card_update_present
577 static void r852_update_card_detect(struct r852_device *dev)
579 int card_detect_reg = r852_read_reg(dev, R852_CARD_IRQ_ENABLE);
580 dev->card_unstable = 0;
582 card_detect_reg &= ~(R852_CARD_IRQ_REMOVE | R852_CARD_IRQ_INSERT);
583 card_detect_reg |= R852_CARD_IRQ_GENABLE;
585 card_detect_reg |= dev->card_detected ?
586 R852_CARD_IRQ_REMOVE : R852_CARD_IRQ_INSERT;
588 r852_write_reg(dev, R852_CARD_IRQ_ENABLE, card_detect_reg);
591 static ssize_t r852_media_type_show(struct device *sys_dev,
592 struct device_attribute *attr, char *buf)
594 struct mtd_info *mtd = container_of(sys_dev, struct mtd_info, dev);
595 struct r852_device *dev = r852_get_dev(mtd);
596 char *data = dev->sm ? "smartmedia" : "xd";
602 static DEVICE_ATTR(media_type, S_IRUGO, r852_media_type_show, NULL);
605 /* Detect properties of card in slot */
606 static void r852_update_media_status(struct r852_device *dev)
612 spin_lock_irqsave(&dev->irqlock, flags);
613 if (!dev->card_detected) {
614 message("card removed");
615 spin_unlock_irqrestore(&dev->irqlock, flags);
619 readonly = r852_read_reg(dev, R852_CARD_STA) & R852_CARD_STA_RO;
620 reg = r852_read_reg(dev, R852_DMA_CAP);
621 dev->sm = (reg & (R852_DMA1 | R852_DMA2)) && (reg & R852_SMBIT);
623 message("detected %s %s card in slot",
624 dev->sm ? "SmartMedia" : "xD",
625 readonly ? "readonly" : "writeable");
627 dev->readonly = readonly;
628 spin_unlock_irqrestore(&dev->irqlock, flags);
632 * Register the nand device
633 * Called when the card is detected
635 static int r852_register_nand_device(struct r852_device *dev)
637 struct mtd_info *mtd = nand_to_mtd(dev->chip);
639 WARN_ON(dev->card_registered);
641 mtd->dev.parent = &dev->pci_dev->dev;
644 dev->chip->options |= NAND_ROM;
646 r852_engine_enable(dev);
648 if (sm_register_device(mtd, dev->sm))
651 if (device_create_file(&mtd->dev, &dev_attr_media_type)) {
652 message("can't create media type sysfs attribute");
656 dev->card_registered = 1;
659 nand_release(dev->chip);
661 /* Force card redetect */
662 dev->card_detected = 0;
667 * Unregister the card
670 static void r852_unregister_nand_device(struct r852_device *dev)
672 struct mtd_info *mtd = nand_to_mtd(dev->chip);
674 if (!dev->card_registered)
677 device_remove_file(&mtd->dev, &dev_attr_media_type);
678 nand_release(dev->chip);
679 r852_engine_disable(dev);
680 dev->card_registered = 0;
683 /* Card state updater */
684 static void r852_card_detect_work(struct work_struct *work)
686 struct r852_device *dev =
687 container_of(work, struct r852_device, card_detect_work.work);
689 r852_card_update_present(dev);
690 r852_update_card_detect(dev);
691 dev->card_unstable = 0;
694 if (dev->card_detected == dev->card_registered)
697 /* Read media properties */
698 r852_update_media_status(dev);
700 /* Register the card */
701 if (dev->card_detected)
702 r852_register_nand_device(dev);
704 r852_unregister_nand_device(dev);
706 r852_update_card_detect(dev);
709 /* Ack + disable IRQ generation */
710 static void r852_disable_irqs(struct r852_device *dev)
713 reg = r852_read_reg(dev, R852_CARD_IRQ_ENABLE);
714 r852_write_reg(dev, R852_CARD_IRQ_ENABLE, reg & ~R852_CARD_IRQ_MASK);
716 reg = r852_read_reg_dword(dev, R852_DMA_IRQ_ENABLE);
717 r852_write_reg_dword(dev, R852_DMA_IRQ_ENABLE,
718 reg & ~R852_DMA_IRQ_MASK);
720 r852_write_reg(dev, R852_CARD_IRQ_STA, R852_CARD_IRQ_MASK);
721 r852_write_reg_dword(dev, R852_DMA_IRQ_STA, R852_DMA_IRQ_MASK);
724 /* Interrupt handler */
725 static irqreturn_t r852_irq(int irq, void *data)
727 struct r852_device *dev = (struct r852_device *)data;
729 uint8_t card_status, dma_status;
731 irqreturn_t ret = IRQ_NONE;
733 spin_lock_irqsave(&dev->irqlock, flags);
735 /* handle card detection interrupts first */
736 card_status = r852_read_reg(dev, R852_CARD_IRQ_STA);
737 r852_write_reg(dev, R852_CARD_IRQ_STA, card_status);
739 if (card_status & (R852_CARD_IRQ_INSERT|R852_CARD_IRQ_REMOVE)) {
742 dev->card_detected = !!(card_status & R852_CARD_IRQ_INSERT);
744 /* we shouldn't receive any interrupts if we wait for card
746 WARN_ON(dev->card_unstable);
748 /* disable irqs while card is unstable */
749 /* this will timeout DMA if active, but better that garbage */
750 r852_disable_irqs(dev);
752 if (dev->card_unstable)
755 /* let, card state to settle a bit, and then do the work */
756 dev->card_unstable = 1;
757 queue_delayed_work(dev->card_workqueue,
758 &dev->card_detect_work, msecs_to_jiffies(100));
763 /* Handle dma interrupts */
764 dma_status = r852_read_reg_dword(dev, R852_DMA_IRQ_STA);
765 r852_write_reg_dword(dev, R852_DMA_IRQ_STA, dma_status);
767 if (dma_status & R852_DMA_IRQ_MASK) {
771 if (dma_status & R852_DMA_IRQ_ERROR) {
772 dbg("received dma error IRQ");
773 r852_dma_done(dev, -EIO);
774 complete(&dev->dma_done);
778 /* received DMA interrupt out of nowhere? */
779 WARN_ON_ONCE(dev->dma_stage == 0);
781 if (dev->dma_stage == 0)
784 /* done device access */
785 if (dev->dma_state == DMA_INTERNAL &&
786 (dma_status & R852_DMA_IRQ_INTERNAL)) {
788 dev->dma_state = DMA_MEMORY;
792 /* done memory DMA */
793 if (dev->dma_state == DMA_MEMORY &&
794 (dma_status & R852_DMA_IRQ_MEMORY)) {
795 dev->dma_state = DMA_INTERNAL;
799 /* Enable 2nd half of dma dance */
800 if (dev->dma_stage == 2)
801 r852_dma_enable(dev);
804 if (dev->dma_stage == 3) {
805 r852_dma_done(dev, 0);
806 complete(&dev->dma_done);
811 /* Handle unknown interrupts */
813 dbg("bad dma IRQ status = %x", dma_status);
815 if (card_status & ~R852_CARD_STA_CD)
816 dbg("strange card status = %x", card_status);
819 spin_unlock_irqrestore(&dev->irqlock, flags);
823 static int r852_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
826 struct nand_chip *chip;
827 struct r852_device *dev;
829 /* pci initialization */
830 error = pci_enable_device(pci_dev);
835 pci_set_master(pci_dev);
837 error = dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32));
841 error = pci_request_regions(pci_dev, DRV_NAME);
848 /* init nand chip, but register it only on card insert */
849 chip = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
855 chip->legacy.cmd_ctrl = r852_cmdctl;
856 chip->legacy.waitfunc = r852_wait;
857 chip->legacy.dev_ready = r852_ready;
860 chip->legacy.read_byte = r852_read_byte;
861 chip->legacy.read_buf = r852_read_buf;
862 chip->legacy.write_buf = r852_write_buf;
865 chip->ecc.mode = NAND_ECC_HW_SYNDROME;
866 chip->ecc.size = R852_DMA_LEN;
867 chip->ecc.bytes = SM_OOB_SIZE;
868 chip->ecc.strength = 2;
869 chip->ecc.hwctl = r852_ecc_hwctl;
870 chip->ecc.calculate = r852_ecc_calculate;
871 chip->ecc.correct = r852_ecc_correct;
874 chip->ecc.read_oob = r852_read_oob;
876 /* init our device structure */
877 dev = kzalloc(sizeof(struct r852_device), GFP_KERNEL);
882 nand_set_controller_data(chip, dev);
884 dev->pci_dev = pci_dev;
885 pci_set_drvdata(pci_dev, dev);
887 dev->bounce_buffer = dma_alloc_coherent(&pci_dev->dev, R852_DMA_LEN,
888 &dev->phys_bounce_buffer, GFP_KERNEL);
890 if (!dev->bounce_buffer)
895 dev->mmio = pci_ioremap_bar(pci_dev, 0);
901 dev->tmp_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL);
903 if (!dev->tmp_buffer)
906 init_completion(&dev->dma_done);
908 dev->card_workqueue = create_freezable_workqueue(DRV_NAME);
910 if (!dev->card_workqueue)
913 INIT_DELAYED_WORK(&dev->card_detect_work, r852_card_detect_work);
915 /* shutdown everything - precation */
916 r852_engine_disable(dev);
917 r852_disable_irqs(dev);
921 dev->irq = pci_dev->irq;
922 spin_lock_init(&dev->irqlock);
924 dev->card_detected = 0;
925 r852_card_update_present(dev);
927 /*register irq handler*/
929 if (request_irq(pci_dev->irq, &r852_irq, IRQF_SHARED,
933 /* kick initial present test */
934 queue_delayed_work(dev->card_workqueue,
935 &dev->card_detect_work, 0);
938 pr_notice("driver loaded successfully\n");
942 destroy_workqueue(dev->card_workqueue);
944 kfree(dev->tmp_buffer);
946 pci_iounmap(pci_dev, dev->mmio);
948 dma_free_coherent(&pci_dev->dev, R852_DMA_LEN, dev->bounce_buffer,
949 dev->phys_bounce_buffer);
955 pci_release_regions(pci_dev);
958 pci_disable_device(pci_dev);
963 static void r852_remove(struct pci_dev *pci_dev)
965 struct r852_device *dev = pci_get_drvdata(pci_dev);
967 /* Stop detect workqueue -
968 we are going to unregister the device anyway*/
969 cancel_delayed_work_sync(&dev->card_detect_work);
970 destroy_workqueue(dev->card_workqueue);
972 /* Unregister the device, this might make more IO */
973 r852_unregister_nand_device(dev);
975 /* Stop interrupts */
976 r852_disable_irqs(dev);
977 free_irq(dev->irq, dev);
980 kfree(dev->tmp_buffer);
981 pci_iounmap(pci_dev, dev->mmio);
982 dma_free_coherent(&pci_dev->dev, R852_DMA_LEN, dev->bounce_buffer,
983 dev->phys_bounce_buffer);
988 /* Shutdown the PCI device */
989 pci_release_regions(pci_dev);
990 pci_disable_device(pci_dev);
993 static void r852_shutdown(struct pci_dev *pci_dev)
995 struct r852_device *dev = pci_get_drvdata(pci_dev);
997 cancel_delayed_work_sync(&dev->card_detect_work);
998 r852_disable_irqs(dev);
999 synchronize_irq(dev->irq);
1000 pci_disable_device(pci_dev);
1003 #ifdef CONFIG_PM_SLEEP
1004 static int r852_suspend(struct device *device)
1006 struct r852_device *dev = pci_get_drvdata(to_pci_dev(device));
1008 if (dev->ctlreg & R852_CTL_CARDENABLE)
1011 /* First make sure the detect work is gone */
1012 cancel_delayed_work_sync(&dev->card_detect_work);
1014 /* Turn off the interrupts and stop the device */
1015 r852_disable_irqs(dev);
1016 r852_engine_disable(dev);
1018 /* If card was pulled off just during the suspend, which is very
1019 unlikely, we will remove it on resume, it too late now
1021 dev->card_unstable = 0;
1025 static int r852_resume(struct device *device)
1027 struct r852_device *dev = pci_get_drvdata(to_pci_dev(device));
1029 r852_disable_irqs(dev);
1030 r852_card_update_present(dev);
1031 r852_engine_disable(dev);
1034 /* If card status changed, just do the work */
1035 if (dev->card_detected != dev->card_registered) {
1036 dbg("card was %s during low power state",
1037 dev->card_detected ? "added" : "removed");
1039 queue_delayed_work(dev->card_workqueue,
1040 &dev->card_detect_work, msecs_to_jiffies(1000));
1044 /* Otherwise, initialize the card */
1045 if (dev->card_registered) {
1046 r852_engine_enable(dev);
1047 nand_select_target(dev->chip, 0);
1048 nand_reset_op(dev->chip);
1049 nand_deselect_target(dev->chip);
1052 /* Program card detection IRQ */
1053 r852_update_card_detect(dev);
1058 static const struct pci_device_id r852_pci_id_tbl[] = {
1060 { PCI_VDEVICE(RICOH, 0x0852), },
1064 MODULE_DEVICE_TABLE(pci, r852_pci_id_tbl);
1066 static SIMPLE_DEV_PM_OPS(r852_pm_ops, r852_suspend, r852_resume);
1068 static struct pci_driver r852_pci_driver = {
1070 .id_table = r852_pci_id_tbl,
1071 .probe = r852_probe,
1072 .remove = r852_remove,
1073 .shutdown = r852_shutdown,
1074 .driver.pm = &r852_pm_ops,
1077 module_pci_driver(r852_pci_driver);
1079 MODULE_LICENSE("GPL");
1080 MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
1081 MODULE_DESCRIPTION("Ricoh 85xx xD/smartmedia card reader driver");