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 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/jiffies.h>
13 #include <linux/workqueue.h>
14 #include <linux/interrupt.h>
15 #include <linux/pci.h>
16 #include <linux/pci_ids.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <asm/byteorder.h>
20 #include <linux/sched.h>
21 #include "sm_common.h"
25 static bool r852_enable_dma
= 1;
26 module_param(r852_enable_dma
, bool, S_IRUGO
);
27 MODULE_PARM_DESC(r852_enable_dma
, "Enable usage of the DMA (default)");
30 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
31 MODULE_PARM_DESC(debug
, "Debug level (0-2)");
34 static inline uint8_t r852_read_reg(struct r852_device
*dev
, int address
)
36 uint8_t reg
= readb(dev
->mmio
+ address
);
41 static inline void r852_write_reg(struct r852_device
*dev
,
42 int address
, uint8_t value
)
44 writeb(value
, dev
->mmio
+ address
);
49 /* read dword sized register */
50 static inline uint32_t r852_read_reg_dword(struct r852_device
*dev
, int address
)
52 uint32_t reg
= le32_to_cpu(readl(dev
->mmio
+ address
));
56 /* write dword sized register */
57 static inline void r852_write_reg_dword(struct r852_device
*dev
,
58 int address
, uint32_t value
)
60 writel(cpu_to_le32(value
), dev
->mmio
+ address
);
64 /* returns pointer to our private structure */
65 static inline struct r852_device
*r852_get_dev(struct mtd_info
*mtd
)
67 struct nand_chip
*chip
= mtd
->priv
;
72 /* check if controller supports dma */
73 static void r852_dma_test(struct r852_device
*dev
)
75 dev
->dma_usable
= (r852_read_reg(dev
, R852_DMA_CAP
) &
76 (R852_DMA1
| R852_DMA2
)) == (R852_DMA1
| R852_DMA2
);
79 message("Non dma capable device detected, dma disabled");
81 if (!r852_enable_dma
) {
82 message("disabling dma on user request");
88 * Enable dma. Enables ether first or second stage of the DMA,
89 * Expects dev->dma_dir and dev->dma_state be set
91 static void r852_dma_enable(struct r852_device
*dev
)
93 uint8_t dma_reg
, dma_irq_reg
;
95 /* Set up dma settings */
96 dma_reg
= r852_read_reg_dword(dev
, R852_DMA_SETTINGS
);
97 dma_reg
&= ~(R852_DMA_READ
| R852_DMA_INTERNAL
| R852_DMA_MEMORY
);
100 dma_reg
|= R852_DMA_READ
;
102 if (dev
->dma_state
== DMA_INTERNAL
) {
103 dma_reg
|= R852_DMA_INTERNAL
;
104 /* Precaution to make sure HW doesn't write */
105 /* to random kernel memory */
106 r852_write_reg_dword(dev
, R852_DMA_ADDR
,
107 cpu_to_le32(dev
->phys_bounce_buffer
));
109 dma_reg
|= R852_DMA_MEMORY
;
110 r852_write_reg_dword(dev
, R852_DMA_ADDR
,
111 cpu_to_le32(dev
->phys_dma_addr
));
114 /* Precaution: make sure write reached the device */
115 r852_read_reg_dword(dev
, R852_DMA_ADDR
);
117 r852_write_reg_dword(dev
, R852_DMA_SETTINGS
, dma_reg
);
120 dma_irq_reg
= r852_read_reg_dword(dev
, R852_DMA_IRQ_ENABLE
);
121 r852_write_reg_dword(dev
, R852_DMA_IRQ_ENABLE
,
123 R852_DMA_IRQ_INTERNAL
|
125 R852_DMA_IRQ_MEMORY
);
129 * Disable dma, called from the interrupt handler, which specifies
130 * success of the operation via 'error' argument
132 static void r852_dma_done(struct r852_device
*dev
, int error
)
134 WARN_ON(dev
->dma_stage
== 0);
136 r852_write_reg_dword(dev
, R852_DMA_IRQ_STA
,
137 r852_read_reg_dword(dev
, R852_DMA_IRQ_STA
));
139 r852_write_reg_dword(dev
, R852_DMA_SETTINGS
, 0);
140 r852_write_reg_dword(dev
, R852_DMA_IRQ_ENABLE
, 0);
142 /* Precaution to make sure HW doesn't write to random kernel memory */
143 r852_write_reg_dword(dev
, R852_DMA_ADDR
,
144 cpu_to_le32(dev
->phys_bounce_buffer
));
145 r852_read_reg_dword(dev
, R852_DMA_ADDR
);
147 dev
->dma_error
= error
;
150 if (dev
->phys_dma_addr
&& dev
->phys_dma_addr
!= dev
->phys_bounce_buffer
)
151 pci_unmap_single(dev
->pci_dev
, dev
->phys_dma_addr
, R852_DMA_LEN
,
152 dev
->dma_dir
? PCI_DMA_FROMDEVICE
: PCI_DMA_TODEVICE
);
156 * Wait, till dma is done, which includes both phases of it
158 static int r852_dma_wait(struct r852_device
*dev
)
160 long timeout
= wait_for_completion_timeout(&dev
->dma_done
,
161 msecs_to_jiffies(1000));
163 dbg("timeout waiting for DMA interrupt");
171 * Read/Write one page using dma. Only pages can be read (512 bytes)
173 static void r852_do_dma(struct r852_device
*dev
, uint8_t *buf
, int do_read
)
181 /* Set dma direction */
182 dev
->dma_dir
= do_read
;
184 reinit_completion(&dev
->dma_done
);
186 dbg_verbose("doing dma %s ", do_read
? "read" : "write");
188 /* Set initial dma state: for reading first fill on board buffer,
189 from device, for writes first fill the buffer from memory*/
190 dev
->dma_state
= do_read
? DMA_INTERNAL
: DMA_MEMORY
;
192 /* if incoming buffer is not page aligned, we should do bounce */
193 if ((unsigned long)buf
& (R852_DMA_LEN
-1))
197 dev
->phys_dma_addr
= pci_map_single(dev
->pci_dev
, (void *)buf
,
199 (do_read
? PCI_DMA_FROMDEVICE
: PCI_DMA_TODEVICE
));
201 if (pci_dma_mapping_error(dev
->pci_dev
, dev
->phys_dma_addr
))
206 dbg_verbose("dma: using bounce buffer");
207 dev
->phys_dma_addr
= dev
->phys_bounce_buffer
;
209 memcpy(dev
->bounce_buffer
, buf
, R852_DMA_LEN
);
213 spin_lock_irqsave(&dev
->irqlock
, flags
);
214 r852_dma_enable(dev
);
215 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
217 /* Wait till complete */
218 error
= r852_dma_wait(dev
);
221 r852_dma_done(dev
, error
);
225 if (do_read
&& bounce
)
226 memcpy((void *)buf
, dev
->bounce_buffer
, R852_DMA_LEN
);
230 * Program data lines of the nand chip to send data to it
232 static void r852_write_buf(struct mtd_info
*mtd
, const uint8_t *buf
, int len
)
234 struct r852_device
*dev
= r852_get_dev(mtd
);
237 /* Don't allow any access to hardware if we suspect card removal */
238 if (dev
->card_unstable
)
241 /* Special case for whole sector read */
242 if (len
== R852_DMA_LEN
&& dev
->dma_usable
) {
243 r852_do_dma(dev
, (uint8_t *)buf
, 0);
247 /* write DWORD chinks - faster */
249 reg
= buf
[0] | buf
[1] << 8 | buf
[2] << 16 | buf
[3] << 24;
250 r852_write_reg_dword(dev
, R852_DATALINE
, reg
);
258 r852_write_reg(dev
, R852_DATALINE
, *buf
++);
264 * Read data lines of the nand chip to retrieve data
266 static void r852_read_buf(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
268 struct r852_device
*dev
= r852_get_dev(mtd
);
271 if (dev
->card_unstable
) {
272 /* since we can't signal error here, at least, return
273 predictable buffer */
278 /* special case for whole sector read */
279 if (len
== R852_DMA_LEN
&& dev
->dma_usable
) {
280 r852_do_dma(dev
, buf
, 1);
284 /* read in dword sized chunks */
287 reg
= r852_read_reg_dword(dev
, R852_DATALINE
);
289 *buf
++ = (reg
>> 8) & 0xFF;
290 *buf
++ = (reg
>> 16) & 0xFF;
291 *buf
++ = (reg
>> 24) & 0xFF;
295 /* read the reset by bytes */
297 *buf
++ = r852_read_reg(dev
, R852_DATALINE
);
301 * Read one byte from nand chip
303 static uint8_t r852_read_byte(struct mtd_info
*mtd
)
305 struct r852_device
*dev
= r852_get_dev(mtd
);
307 /* Same problem as in r852_read_buf.... */
308 if (dev
->card_unstable
)
311 return r852_read_reg(dev
, R852_DATALINE
);
315 * Control several chip lines & send commands
317 static void r852_cmdctl(struct mtd_info
*mtd
, int dat
, unsigned int ctrl
)
319 struct r852_device
*dev
= r852_get_dev(mtd
);
321 if (dev
->card_unstable
)
324 if (ctrl
& NAND_CTRL_CHANGE
) {
326 dev
->ctlreg
&= ~(R852_CTL_DATA
| R852_CTL_COMMAND
|
327 R852_CTL_ON
| R852_CTL_CARDENABLE
);
330 dev
->ctlreg
|= R852_CTL_DATA
;
333 dev
->ctlreg
|= R852_CTL_COMMAND
;
336 dev
->ctlreg
|= (R852_CTL_CARDENABLE
| R852_CTL_ON
);
338 dev
->ctlreg
&= ~R852_CTL_WRITE
;
340 /* when write is stareted, enable write access */
341 if (dat
== NAND_CMD_ERASE1
)
342 dev
->ctlreg
|= R852_CTL_WRITE
;
344 r852_write_reg(dev
, R852_CTL
, dev
->ctlreg
);
347 /* HACK: NAND_CMD_SEQIN is called without NAND_CTRL_CHANGE, but we need
349 if (dat
== NAND_CMD_SEQIN
&& (dev
->ctlreg
& R852_CTL_COMMAND
)) {
350 dev
->ctlreg
|= R852_CTL_WRITE
;
351 r852_write_reg(dev
, R852_CTL
, dev
->ctlreg
);
354 if (dat
!= NAND_CMD_NONE
)
355 r852_write_reg(dev
, R852_DATALINE
, dat
);
359 * Wait till card is ready.
360 * based on nand_wait, but returns errors on DMA error
362 static int r852_wait(struct mtd_info
*mtd
, struct nand_chip
*chip
)
364 struct r852_device
*dev
= chip
->priv
;
366 unsigned long timeout
;
369 timeout
= jiffies
+ (chip
->state
== FL_ERASING
?
370 msecs_to_jiffies(400) : msecs_to_jiffies(20));
372 while (time_before(jiffies
, timeout
))
373 if (chip
->dev_ready(mtd
))
376 chip
->cmdfunc(mtd
, NAND_CMD_STATUS
, -1, -1);
377 status
= (int)chip
->read_byte(mtd
);
379 /* Unfortunelly, no way to send detailed error status... */
380 if (dev
->dma_error
) {
381 status
|= NAND_STATUS_FAIL
;
388 * Check if card is ready
391 static int r852_ready(struct mtd_info
*mtd
)
393 struct r852_device
*dev
= r852_get_dev(mtd
);
394 return !(r852_read_reg(dev
, R852_CARD_STA
) & R852_CARD_STA_BUSY
);
399 * Set ECC engine mode
402 static void r852_ecc_hwctl(struct mtd_info
*mtd
, int mode
)
404 struct r852_device
*dev
= r852_get_dev(mtd
);
406 if (dev
->card_unstable
)
412 /* enable ecc generation/check*/
413 dev
->ctlreg
|= R852_CTL_ECC_ENABLE
;
415 /* flush ecc buffer */
416 r852_write_reg(dev
, R852_CTL
,
417 dev
->ctlreg
| R852_CTL_ECC_ACCESS
);
419 r852_read_reg_dword(dev
, R852_DATALINE
);
420 r852_write_reg(dev
, R852_CTL
, dev
->ctlreg
);
423 case NAND_ECC_READSYN
:
424 /* disable ecc generation */
425 dev
->ctlreg
&= ~R852_CTL_ECC_ENABLE
;
426 r852_write_reg(dev
, R852_CTL
, dev
->ctlreg
);
431 * Calculate ECC, only used for writes
434 static int r852_ecc_calculate(struct mtd_info
*mtd
, const uint8_t *dat
,
437 struct r852_device
*dev
= r852_get_dev(mtd
);
438 struct sm_oob
*oob
= (struct sm_oob
*)ecc_code
;
441 if (dev
->card_unstable
)
444 dev
->ctlreg
&= ~R852_CTL_ECC_ENABLE
;
445 r852_write_reg(dev
, R852_CTL
, dev
->ctlreg
| R852_CTL_ECC_ACCESS
);
447 ecc1
= r852_read_reg_dword(dev
, R852_DATALINE
);
448 ecc2
= r852_read_reg_dword(dev
, R852_DATALINE
);
450 oob
->ecc1
[0] = (ecc1
) & 0xFF;
451 oob
->ecc1
[1] = (ecc1
>> 8) & 0xFF;
452 oob
->ecc1
[2] = (ecc1
>> 16) & 0xFF;
454 oob
->ecc2
[0] = (ecc2
) & 0xFF;
455 oob
->ecc2
[1] = (ecc2
>> 8) & 0xFF;
456 oob
->ecc2
[2] = (ecc2
>> 16) & 0xFF;
458 r852_write_reg(dev
, R852_CTL
, dev
->ctlreg
);
463 * Correct the data using ECC, hw did almost everything for us
466 static int r852_ecc_correct(struct mtd_info
*mtd
, uint8_t *dat
,
467 uint8_t *read_ecc
, uint8_t *calc_ecc
)
470 uint8_t ecc_status
, err_byte
;
473 struct r852_device
*dev
= r852_get_dev(mtd
);
475 if (dev
->card_unstable
)
478 if (dev
->dma_error
) {
483 r852_write_reg(dev
, R852_CTL
, dev
->ctlreg
| R852_CTL_ECC_ACCESS
);
484 ecc_reg
= r852_read_reg_dword(dev
, R852_DATALINE
);
485 r852_write_reg(dev
, R852_CTL
, dev
->ctlreg
);
487 for (i
= 0 ; i
<= 1 ; i
++) {
489 ecc_status
= (ecc_reg
>> 8) & 0xFF;
491 /* ecc uncorrectable error */
492 if (ecc_status
& R852_ECC_FAIL
) {
493 dbg("ecc: unrecoverable error, in half %d", i
);
498 /* correctable error */
499 if (ecc_status
& R852_ECC_CORRECTABLE
) {
501 err_byte
= ecc_reg
& 0xFF;
502 dbg("ecc: recoverable error, "
503 "in half %d, byte %d, bit %d", i
,
504 err_byte
, ecc_status
& R852_ECC_ERR_BIT_MSK
);
507 1 << (ecc_status
& R852_ECC_ERR_BIT_MSK
);
519 * This is copy of nand_read_oob_std
520 * nand_read_oob_syndrome assumes we can send column address - we can't
522 static int r852_read_oob(struct mtd_info
*mtd
, struct nand_chip
*chip
,
525 chip
->cmdfunc(mtd
, NAND_CMD_READOOB
, 0, page
);
526 chip
->read_buf(mtd
, 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 dev
->mtd
= kzalloc(sizeof(struct mtd_info
), GFP_KERNEL
);
642 WARN_ON(dev
->card_registred
);
644 dev
->mtd
->owner
= THIS_MODULE
;
645 dev
->mtd
->priv
= dev
->chip
;
646 dev
->mtd
->dev
.parent
= &dev
->pci_dev
->dev
;
649 dev
->chip
->options
|= NAND_ROM
;
651 r852_engine_enable(dev
);
653 if (sm_register_device(dev
->mtd
, dev
->sm
))
656 if (device_create_file(&dev
->mtd
->dev
, &dev_attr_media_type
))
657 message("can't create media type sysfs attribute");
659 dev
->card_registred
= 1;
664 /* Force card redetect */
665 dev
->card_detected
= 0;
670 * Unregister the card
673 static void r852_unregister_nand_device(struct r852_device
*dev
)
675 if (!dev
->card_registred
)
678 device_remove_file(&dev
->mtd
->dev
, &dev_attr_media_type
);
679 nand_release(dev
->mtd
);
680 r852_engine_disable(dev
);
681 dev
->card_registred
= 0;
686 /* Card state updater */
687 static void r852_card_detect_work(struct work_struct
*work
)
689 struct r852_device
*dev
=
690 container_of(work
, struct r852_device
, card_detect_work
.work
);
692 r852_card_update_present(dev
);
693 r852_update_card_detect(dev
);
694 dev
->card_unstable
= 0;
697 if (dev
->card_detected
== dev
->card_registred
)
700 /* Read media properties */
701 r852_update_media_status(dev
);
703 /* Register the card */
704 if (dev
->card_detected
)
705 r852_register_nand_device(dev
);
707 r852_unregister_nand_device(dev
);
709 r852_update_card_detect(dev
);
712 /* Ack + disable IRQ generation */
713 static void r852_disable_irqs(struct r852_device
*dev
)
716 reg
= r852_read_reg(dev
, R852_CARD_IRQ_ENABLE
);
717 r852_write_reg(dev
, R852_CARD_IRQ_ENABLE
, reg
& ~R852_CARD_IRQ_MASK
);
719 reg
= r852_read_reg_dword(dev
, R852_DMA_IRQ_ENABLE
);
720 r852_write_reg_dword(dev
, R852_DMA_IRQ_ENABLE
,
721 reg
& ~R852_DMA_IRQ_MASK
);
723 r852_write_reg(dev
, R852_CARD_IRQ_STA
, R852_CARD_IRQ_MASK
);
724 r852_write_reg_dword(dev
, R852_DMA_IRQ_STA
, R852_DMA_IRQ_MASK
);
727 /* Interrupt handler */
728 static irqreturn_t
r852_irq(int irq
, void *data
)
730 struct r852_device
*dev
= (struct r852_device
*)data
;
732 uint8_t card_status
, dma_status
;
734 irqreturn_t ret
= IRQ_NONE
;
736 spin_lock_irqsave(&dev
->irqlock
, flags
);
738 /* handle card detection interrupts first */
739 card_status
= r852_read_reg(dev
, R852_CARD_IRQ_STA
);
740 r852_write_reg(dev
, R852_CARD_IRQ_STA
, card_status
);
742 if (card_status
& (R852_CARD_IRQ_INSERT
|R852_CARD_IRQ_REMOVE
)) {
745 dev
->card_detected
= !!(card_status
& R852_CARD_IRQ_INSERT
);
747 /* we shouldn't receive any interrupts if we wait for card
749 WARN_ON(dev
->card_unstable
);
751 /* disable irqs while card is unstable */
752 /* this will timeout DMA if active, but better that garbage */
753 r852_disable_irqs(dev
);
755 if (dev
->card_unstable
)
758 /* let, card state to settle a bit, and then do the work */
759 dev
->card_unstable
= 1;
760 queue_delayed_work(dev
->card_workqueue
,
761 &dev
->card_detect_work
, msecs_to_jiffies(100));
766 /* Handle dma interrupts */
767 dma_status
= r852_read_reg_dword(dev
, R852_DMA_IRQ_STA
);
768 r852_write_reg_dword(dev
, R852_DMA_IRQ_STA
, dma_status
);
770 if (dma_status
& R852_DMA_IRQ_MASK
) {
774 if (dma_status
& R852_DMA_IRQ_ERROR
) {
775 dbg("received dma error IRQ");
776 r852_dma_done(dev
, -EIO
);
777 complete(&dev
->dma_done
);
781 /* received DMA interrupt out of nowhere? */
782 WARN_ON_ONCE(dev
->dma_stage
== 0);
784 if (dev
->dma_stage
== 0)
787 /* done device access */
788 if (dev
->dma_state
== DMA_INTERNAL
&&
789 (dma_status
& R852_DMA_IRQ_INTERNAL
)) {
791 dev
->dma_state
= DMA_MEMORY
;
795 /* done memory DMA */
796 if (dev
->dma_state
== DMA_MEMORY
&&
797 (dma_status
& R852_DMA_IRQ_MEMORY
)) {
798 dev
->dma_state
= DMA_INTERNAL
;
802 /* Enable 2nd half of dma dance */
803 if (dev
->dma_stage
== 2)
804 r852_dma_enable(dev
);
807 if (dev
->dma_stage
== 3) {
808 r852_dma_done(dev
, 0);
809 complete(&dev
->dma_done
);
814 /* Handle unknown interrupts */
816 dbg("bad dma IRQ status = %x", dma_status
);
818 if (card_status
& ~R852_CARD_STA_CD
)
819 dbg("strange card status = %x", card_status
);
822 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
826 static int r852_probe(struct pci_dev
*pci_dev
, const struct pci_device_id
*id
)
829 struct nand_chip
*chip
;
830 struct r852_device
*dev
;
832 /* pci initialization */
833 error
= pci_enable_device(pci_dev
);
838 pci_set_master(pci_dev
);
840 error
= pci_set_dma_mask(pci_dev
, DMA_BIT_MASK(32));
844 error
= pci_request_regions(pci_dev
, DRV_NAME
);
851 /* init nand chip, but register it only on card insert */
852 chip
= kzalloc(sizeof(struct nand_chip
), GFP_KERNEL
);
858 chip
->cmd_ctrl
= r852_cmdctl
;
859 chip
->waitfunc
= r852_wait
;
860 chip
->dev_ready
= r852_ready
;
863 chip
->read_byte
= r852_read_byte
;
864 chip
->read_buf
= r852_read_buf
;
865 chip
->write_buf
= r852_write_buf
;
868 chip
->ecc
.mode
= NAND_ECC_HW_SYNDROME
;
869 chip
->ecc
.size
= R852_DMA_LEN
;
870 chip
->ecc
.bytes
= SM_OOB_SIZE
;
871 chip
->ecc
.strength
= 2;
872 chip
->ecc
.hwctl
= r852_ecc_hwctl
;
873 chip
->ecc
.calculate
= r852_ecc_calculate
;
874 chip
->ecc
.correct
= r852_ecc_correct
;
877 chip
->ecc
.read_oob
= r852_read_oob
;
879 /* init our device structure */
880 dev
= kzalloc(sizeof(struct r852_device
), GFP_KERNEL
);
887 dev
->pci_dev
= pci_dev
;
888 pci_set_drvdata(pci_dev
, dev
);
890 dev
->bounce_buffer
= pci_alloc_consistent(pci_dev
, R852_DMA_LEN
,
891 &dev
->phys_bounce_buffer
);
893 if (!dev
->bounce_buffer
)
898 dev
->mmio
= pci_ioremap_bar(pci_dev
, 0);
904 dev
->tmp_buffer
= kzalloc(SM_SECTOR_SIZE
, GFP_KERNEL
);
906 if (!dev
->tmp_buffer
)
909 init_completion(&dev
->dma_done
);
911 dev
->card_workqueue
= create_freezable_workqueue(DRV_NAME
);
913 if (!dev
->card_workqueue
)
916 INIT_DELAYED_WORK(&dev
->card_detect_work
, r852_card_detect_work
);
918 /* shutdown everything - precation */
919 r852_engine_disable(dev
);
920 r852_disable_irqs(dev
);
924 dev
->irq
= pci_dev
->irq
;
925 spin_lock_init(&dev
->irqlock
);
927 dev
->card_detected
= 0;
928 r852_card_update_present(dev
);
930 /*register irq handler*/
932 if (request_irq(pci_dev
->irq
, &r852_irq
, IRQF_SHARED
,
936 /* kick initial present test */
937 queue_delayed_work(dev
->card_workqueue
,
938 &dev
->card_detect_work
, 0);
941 printk(KERN_NOTICE DRV_NAME
": driver loaded successfully\n");
945 destroy_workqueue(dev
->card_workqueue
);
947 kfree(dev
->tmp_buffer
);
949 pci_iounmap(pci_dev
, dev
->mmio
);
951 pci_free_consistent(pci_dev
, R852_DMA_LEN
,
952 dev
->bounce_buffer
, dev
->phys_bounce_buffer
);
958 pci_release_regions(pci_dev
);
961 pci_disable_device(pci_dev
);
966 static void r852_remove(struct pci_dev
*pci_dev
)
968 struct r852_device
*dev
= pci_get_drvdata(pci_dev
);
970 /* Stop detect workqueue -
971 we are going to unregister the device anyway*/
972 cancel_delayed_work_sync(&dev
->card_detect_work
);
973 destroy_workqueue(dev
->card_workqueue
);
975 /* Unregister the device, this might make more IO */
976 r852_unregister_nand_device(dev
);
978 /* Stop interrupts */
979 r852_disable_irqs(dev
);
980 synchronize_irq(dev
->irq
);
981 free_irq(dev
->irq
, dev
);
984 kfree(dev
->tmp_buffer
);
985 pci_iounmap(pci_dev
, dev
->mmio
);
986 pci_free_consistent(pci_dev
, R852_DMA_LEN
,
987 dev
->bounce_buffer
, dev
->phys_bounce_buffer
);
992 /* Shutdown the PCI device */
993 pci_release_regions(pci_dev
);
994 pci_disable_device(pci_dev
);
997 static void r852_shutdown(struct pci_dev
*pci_dev
)
999 struct r852_device
*dev
= pci_get_drvdata(pci_dev
);
1001 cancel_delayed_work_sync(&dev
->card_detect_work
);
1002 r852_disable_irqs(dev
);
1003 synchronize_irq(dev
->irq
);
1004 pci_disable_device(pci_dev
);
1007 #ifdef CONFIG_PM_SLEEP
1008 static int r852_suspend(struct device
*device
)
1010 struct r852_device
*dev
= pci_get_drvdata(to_pci_dev(device
));
1012 if (dev
->ctlreg
& R852_CTL_CARDENABLE
)
1015 /* First make sure the detect work is gone */
1016 cancel_delayed_work_sync(&dev
->card_detect_work
);
1018 /* Turn off the interrupts and stop the device */
1019 r852_disable_irqs(dev
);
1020 r852_engine_disable(dev
);
1022 /* If card was pulled off just during the suspend, which is very
1023 unlikely, we will remove it on resume, it too late now
1025 dev
->card_unstable
= 0;
1029 static int r852_resume(struct device
*device
)
1031 struct r852_device
*dev
= pci_get_drvdata(to_pci_dev(device
));
1033 r852_disable_irqs(dev
);
1034 r852_card_update_present(dev
);
1035 r852_engine_disable(dev
);
1038 /* If card status changed, just do the work */
1039 if (dev
->card_detected
!= dev
->card_registred
) {
1040 dbg("card was %s during low power state",
1041 dev
->card_detected
? "added" : "removed");
1043 queue_delayed_work(dev
->card_workqueue
,
1044 &dev
->card_detect_work
, msecs_to_jiffies(1000));
1048 /* Otherwise, initialize the card */
1049 if (dev
->card_registred
) {
1050 r852_engine_enable(dev
);
1051 dev
->chip
->select_chip(dev
->mtd
, 0);
1052 dev
->chip
->cmdfunc(dev
->mtd
, NAND_CMD_RESET
, -1, -1);
1053 dev
->chip
->select_chip(dev
->mtd
, -1);
1056 /* Program card detection IRQ */
1057 r852_update_card_detect(dev
);
1062 static const struct pci_device_id r852_pci_id_tbl
[] = {
1064 { PCI_VDEVICE(RICOH
, 0x0852), },
1068 MODULE_DEVICE_TABLE(pci
, r852_pci_id_tbl
);
1070 static SIMPLE_DEV_PM_OPS(r852_pm_ops
, r852_suspend
, r852_resume
);
1072 static struct pci_driver r852_pci_driver
= {
1074 .id_table
= r852_pci_id_tbl
,
1075 .probe
= r852_probe
,
1076 .remove
= r852_remove
,
1077 .shutdown
= r852_shutdown
,
1078 .driver
.pm
= &r852_pm_ops
,
1081 module_pci_driver(r852_pci_driver
);
1083 MODULE_LICENSE("GPL");
1084 MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
1085 MODULE_DESCRIPTION("Ricoh 85xx xD/smartmedia card reader driver");