192634125SMaxim Levitsky /* 292634125SMaxim Levitsky * Copyright (C) 2010 - Maxim Levitsky 392634125SMaxim Levitsky * driver for Ricoh memstick readers 492634125SMaxim Levitsky * 592634125SMaxim Levitsky * This program is free software; you can redistribute it and/or modify 692634125SMaxim Levitsky * it under the terms of the GNU General Public License version 2 as 792634125SMaxim Levitsky * published by the Free Software Foundation. 892634125SMaxim Levitsky */ 992634125SMaxim Levitsky 1092634125SMaxim Levitsky #include <linux/kernel.h> 1192634125SMaxim Levitsky #include <linux/module.h> 1292634125SMaxim Levitsky #include <linux/freezer.h> 1392634125SMaxim Levitsky #include <linux/jiffies.h> 1492634125SMaxim Levitsky #include <linux/interrupt.h> 1592634125SMaxim Levitsky #include <linux/pci.h> 1692634125SMaxim Levitsky #include <linux/pci_ids.h> 1792634125SMaxim Levitsky #include <linux/delay.h> 1892634125SMaxim Levitsky #include <linux/slab.h> 1992634125SMaxim Levitsky #include <linux/kthread.h> 2092634125SMaxim Levitsky #include <linux/sched.h> 2192634125SMaxim Levitsky #include <linux/highmem.h> 2292634125SMaxim Levitsky #include <asm/byteorder.h> 2392634125SMaxim Levitsky #include <linux/swab.h> 2492634125SMaxim Levitsky #include "r592.h" 2592634125SMaxim Levitsky 2692634125SMaxim Levitsky static int enable_dma = 1; 2792634125SMaxim Levitsky static int debug; 2892634125SMaxim Levitsky 2992634125SMaxim Levitsky static const char *tpc_names[] = { 3092634125SMaxim Levitsky "MS_TPC_READ_MG_STATUS", 3192634125SMaxim Levitsky "MS_TPC_READ_LONG_DATA", 3292634125SMaxim Levitsky "MS_TPC_READ_SHORT_DATA", 3392634125SMaxim Levitsky "MS_TPC_READ_REG", 3492634125SMaxim Levitsky "MS_TPC_READ_QUAD_DATA", 3592634125SMaxim Levitsky "INVALID", 3692634125SMaxim Levitsky "MS_TPC_GET_INT", 3792634125SMaxim Levitsky "MS_TPC_SET_RW_REG_ADRS", 3892634125SMaxim Levitsky "MS_TPC_EX_SET_CMD", 3992634125SMaxim Levitsky "MS_TPC_WRITE_QUAD_DATA", 4092634125SMaxim Levitsky "MS_TPC_WRITE_REG", 4192634125SMaxim Levitsky "MS_TPC_WRITE_SHORT_DATA", 4292634125SMaxim Levitsky "MS_TPC_WRITE_LONG_DATA", 4392634125SMaxim Levitsky "MS_TPC_SET_CMD", 4492634125SMaxim Levitsky }; 4592634125SMaxim Levitsky 4692634125SMaxim Levitsky /** 4792634125SMaxim Levitsky * memstick_debug_get_tpc_name - debug helper that returns string for 4892634125SMaxim Levitsky * a TPC number 4992634125SMaxim Levitsky */ 5092634125SMaxim Levitsky const char *memstick_debug_get_tpc_name(int tpc) 5192634125SMaxim Levitsky { 5292634125SMaxim Levitsky return tpc_names[tpc-1]; 5392634125SMaxim Levitsky } 5492634125SMaxim Levitsky EXPORT_SYMBOL(memstick_debug_get_tpc_name); 5592634125SMaxim Levitsky 5692634125SMaxim Levitsky 5792634125SMaxim Levitsky /* Read a register*/ 5892634125SMaxim Levitsky static inline u32 r592_read_reg(struct r592_device *dev, int address) 5992634125SMaxim Levitsky { 6092634125SMaxim Levitsky u32 value = readl(dev->mmio + address); 6192634125SMaxim Levitsky dbg_reg("reg #%02d == 0x%08x", address, value); 6292634125SMaxim Levitsky return value; 6392634125SMaxim Levitsky } 6492634125SMaxim Levitsky 6592634125SMaxim Levitsky /* Write a register */ 6692634125SMaxim Levitsky static inline void r592_write_reg(struct r592_device *dev, 6792634125SMaxim Levitsky int address, u32 value) 6892634125SMaxim Levitsky { 6992634125SMaxim Levitsky dbg_reg("reg #%02d <- 0x%08x", address, value); 7092634125SMaxim Levitsky writel(value, dev->mmio + address); 7192634125SMaxim Levitsky } 7292634125SMaxim Levitsky 7392634125SMaxim Levitsky /* Reads a big endian DWORD register */ 7492634125SMaxim Levitsky static inline u32 r592_read_reg_raw_be(struct r592_device *dev, int address) 7592634125SMaxim Levitsky { 7692634125SMaxim Levitsky u32 value = __raw_readl(dev->mmio + address); 7792634125SMaxim Levitsky dbg_reg("reg #%02d == 0x%08x", address, value); 7892634125SMaxim Levitsky return be32_to_cpu(value); 7992634125SMaxim Levitsky } 8092634125SMaxim Levitsky 8192634125SMaxim Levitsky /* Writes a big endian DWORD register */ 8292634125SMaxim Levitsky static inline void r592_write_reg_raw_be(struct r592_device *dev, 8392634125SMaxim Levitsky int address, u32 value) 8492634125SMaxim Levitsky { 8592634125SMaxim Levitsky dbg_reg("reg #%02d <- 0x%08x", address, value); 8692634125SMaxim Levitsky __raw_writel(cpu_to_be32(value), dev->mmio + address); 8792634125SMaxim Levitsky } 8892634125SMaxim Levitsky 8992634125SMaxim Levitsky /* Set specific bits in a register (little endian) */ 9092634125SMaxim Levitsky static inline void r592_set_reg_mask(struct r592_device *dev, 9192634125SMaxim Levitsky int address, u32 mask) 9292634125SMaxim Levitsky { 9392634125SMaxim Levitsky u32 reg = readl(dev->mmio + address); 9492634125SMaxim Levitsky dbg_reg("reg #%02d |= 0x%08x (old =0x%08x)", address, mask, reg); 9592634125SMaxim Levitsky writel(reg | mask , dev->mmio + address); 9692634125SMaxim Levitsky } 9792634125SMaxim Levitsky 9892634125SMaxim Levitsky /* Clear specific bits in a register (little endian) */ 9992634125SMaxim Levitsky static inline void r592_clear_reg_mask(struct r592_device *dev, 10092634125SMaxim Levitsky int address, u32 mask) 10192634125SMaxim Levitsky { 10292634125SMaxim Levitsky u32 reg = readl(dev->mmio + address); 10392634125SMaxim Levitsky dbg_reg("reg #%02d &= 0x%08x (old = 0x%08x, mask = 0x%08x)", 10492634125SMaxim Levitsky address, ~mask, reg, mask); 10592634125SMaxim Levitsky writel(reg & ~mask, dev->mmio + address); 10692634125SMaxim Levitsky } 10792634125SMaxim Levitsky 10892634125SMaxim Levitsky 10992634125SMaxim Levitsky /* Wait for status bits while checking for errors */ 11092634125SMaxim Levitsky static int r592_wait_status(struct r592_device *dev, u32 mask, u32 wanted_mask) 11192634125SMaxim Levitsky { 11292634125SMaxim Levitsky unsigned long timeout = jiffies + msecs_to_jiffies(1000); 11392634125SMaxim Levitsky u32 reg = r592_read_reg(dev, R592_STATUS); 11492634125SMaxim Levitsky 11592634125SMaxim Levitsky if ((reg & mask) == wanted_mask) 11692634125SMaxim Levitsky return 0; 11792634125SMaxim Levitsky 11892634125SMaxim Levitsky while (time_before(jiffies, timeout)) { 11992634125SMaxim Levitsky 12092634125SMaxim Levitsky reg = r592_read_reg(dev, R592_STATUS); 12192634125SMaxim Levitsky 12292634125SMaxim Levitsky if ((reg & mask) == wanted_mask) 12392634125SMaxim Levitsky return 0; 12492634125SMaxim Levitsky 12592634125SMaxim Levitsky if (reg & (R592_STATUS_SEND_ERR | R592_STATUS_RECV_ERR)) 12692634125SMaxim Levitsky return -EIO; 12792634125SMaxim Levitsky 12892634125SMaxim Levitsky cpu_relax(); 12992634125SMaxim Levitsky } 13092634125SMaxim Levitsky return -ETIME; 13192634125SMaxim Levitsky } 13292634125SMaxim Levitsky 13392634125SMaxim Levitsky 13492634125SMaxim Levitsky /* Enable/disable device */ 13592634125SMaxim Levitsky static int r592_enable_device(struct r592_device *dev, bool enable) 13692634125SMaxim Levitsky { 13792634125SMaxim Levitsky dbg("%sabling the device", enable ? "en" : "dis"); 13892634125SMaxim Levitsky 13992634125SMaxim Levitsky if (enable) { 14092634125SMaxim Levitsky 14192634125SMaxim Levitsky /* Power up the card */ 14292634125SMaxim Levitsky r592_write_reg(dev, R592_POWER, R592_POWER_0 | R592_POWER_1); 14392634125SMaxim Levitsky 14492634125SMaxim Levitsky /* Perform a reset */ 14592634125SMaxim Levitsky r592_set_reg_mask(dev, R592_IO, R592_IO_RESET); 14692634125SMaxim Levitsky 14792634125SMaxim Levitsky msleep(100); 14892634125SMaxim Levitsky } else 14992634125SMaxim Levitsky /* Power down the card */ 15092634125SMaxim Levitsky r592_write_reg(dev, R592_POWER, 0); 15192634125SMaxim Levitsky 15292634125SMaxim Levitsky return 0; 15392634125SMaxim Levitsky } 15492634125SMaxim Levitsky 15592634125SMaxim Levitsky /* Set serial/parallel mode */ 15692634125SMaxim Levitsky static int r592_set_mode(struct r592_device *dev, bool parallel_mode) 15792634125SMaxim Levitsky { 15892634125SMaxim Levitsky if (!parallel_mode) { 15992634125SMaxim Levitsky dbg("switching to serial mode"); 16092634125SMaxim Levitsky 16192634125SMaxim Levitsky /* Set serial mode */ 16292634125SMaxim Levitsky r592_write_reg(dev, R592_IO_MODE, R592_IO_MODE_SERIAL); 16392634125SMaxim Levitsky 16492634125SMaxim Levitsky r592_clear_reg_mask(dev, R592_POWER, R592_POWER_20); 16592634125SMaxim Levitsky 16692634125SMaxim Levitsky } else { 16792634125SMaxim Levitsky dbg("switching to parallel mode"); 16892634125SMaxim Levitsky 16992634125SMaxim Levitsky /* This setting should be set _before_ switch TPC */ 17092634125SMaxim Levitsky r592_set_reg_mask(dev, R592_POWER, R592_POWER_20); 17192634125SMaxim Levitsky 17292634125SMaxim Levitsky r592_clear_reg_mask(dev, R592_IO, 17392634125SMaxim Levitsky R592_IO_SERIAL1 | R592_IO_SERIAL2); 17492634125SMaxim Levitsky 17592634125SMaxim Levitsky /* Set the parallel mode now */ 17692634125SMaxim Levitsky r592_write_reg(dev, R592_IO_MODE, R592_IO_MODE_PARALLEL); 17792634125SMaxim Levitsky } 17892634125SMaxim Levitsky 17992634125SMaxim Levitsky dev->parallel_mode = parallel_mode; 18092634125SMaxim Levitsky return 0; 18192634125SMaxim Levitsky } 18292634125SMaxim Levitsky 18392634125SMaxim Levitsky /* Perform a controller reset without powering down the card */ 18492634125SMaxim Levitsky static void r592_host_reset(struct r592_device *dev) 18592634125SMaxim Levitsky { 18692634125SMaxim Levitsky r592_set_reg_mask(dev, R592_IO, R592_IO_RESET); 18792634125SMaxim Levitsky msleep(100); 18892634125SMaxim Levitsky r592_set_mode(dev, dev->parallel_mode); 18992634125SMaxim Levitsky } 19092634125SMaxim Levitsky 19192634125SMaxim Levitsky /* Disable all hardware interrupts */ 19292634125SMaxim Levitsky static void r592_clear_interrupts(struct r592_device *dev) 19392634125SMaxim Levitsky { 19492634125SMaxim Levitsky /* Disable & ACK all interrupts */ 19592634125SMaxim Levitsky r592_clear_reg_mask(dev, R592_REG_MSC, IRQ_ALL_ACK_MASK); 19692634125SMaxim Levitsky r592_clear_reg_mask(dev, R592_REG_MSC, IRQ_ALL_EN_MASK); 19792634125SMaxim Levitsky } 19892634125SMaxim Levitsky 19992634125SMaxim Levitsky /* Tests if there is an CRC error */ 20092634125SMaxim Levitsky static int r592_test_io_error(struct r592_device *dev) 20192634125SMaxim Levitsky { 20292634125SMaxim Levitsky if (!(r592_read_reg(dev, R592_STATUS) & 20392634125SMaxim Levitsky (R592_STATUS_SEND_ERR | R592_STATUS_RECV_ERR))) 20492634125SMaxim Levitsky return 0; 20592634125SMaxim Levitsky 20692634125SMaxim Levitsky return -EIO; 20792634125SMaxim Levitsky } 20892634125SMaxim Levitsky 20992634125SMaxim Levitsky /* Ensure that FIFO is ready for use */ 21092634125SMaxim Levitsky static int r592_test_fifo_empty(struct r592_device *dev) 21192634125SMaxim Levitsky { 21292634125SMaxim Levitsky if (r592_read_reg(dev, R592_REG_MSC) & R592_REG_MSC_FIFO_EMPTY) 21392634125SMaxim Levitsky return 0; 21492634125SMaxim Levitsky 21592634125SMaxim Levitsky dbg("FIFO not ready, trying to reset the device"); 21692634125SMaxim Levitsky r592_host_reset(dev); 21792634125SMaxim Levitsky 21892634125SMaxim Levitsky if (r592_read_reg(dev, R592_REG_MSC) & R592_REG_MSC_FIFO_EMPTY) 21992634125SMaxim Levitsky return 0; 22092634125SMaxim Levitsky 22192634125SMaxim Levitsky message("FIFO still not ready, giving up"); 22292634125SMaxim Levitsky return -EIO; 22392634125SMaxim Levitsky } 22492634125SMaxim Levitsky 22592634125SMaxim Levitsky /* Activates the DMA transfer from to FIFO */ 22692634125SMaxim Levitsky static void r592_start_dma(struct r592_device *dev, bool is_write) 22792634125SMaxim Levitsky { 22892634125SMaxim Levitsky unsigned long flags; 22992634125SMaxim Levitsky u32 reg; 23092634125SMaxim Levitsky spin_lock_irqsave(&dev->irq_lock, flags); 23192634125SMaxim Levitsky 23292634125SMaxim Levitsky /* Ack interrupts (just in case) + enable them */ 23392634125SMaxim Levitsky r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_ACK_MASK); 23492634125SMaxim Levitsky r592_set_reg_mask(dev, R592_REG_MSC, DMA_IRQ_EN_MASK); 23592634125SMaxim Levitsky 23692634125SMaxim Levitsky /* Set DMA address */ 23792634125SMaxim Levitsky r592_write_reg(dev, R592_FIFO_DMA, sg_dma_address(&dev->req->sg)); 23892634125SMaxim Levitsky 23992634125SMaxim Levitsky /* Enable the DMA */ 24092634125SMaxim Levitsky reg = r592_read_reg(dev, R592_FIFO_DMA_SETTINGS); 24192634125SMaxim Levitsky reg |= R592_FIFO_DMA_SETTINGS_EN; 24292634125SMaxim Levitsky 24392634125SMaxim Levitsky if (!is_write) 24492634125SMaxim Levitsky reg |= R592_FIFO_DMA_SETTINGS_DIR; 24592634125SMaxim Levitsky else 24692634125SMaxim Levitsky reg &= ~R592_FIFO_DMA_SETTINGS_DIR; 24792634125SMaxim Levitsky r592_write_reg(dev, R592_FIFO_DMA_SETTINGS, reg); 24892634125SMaxim Levitsky 24992634125SMaxim Levitsky spin_unlock_irqrestore(&dev->irq_lock, flags); 25092634125SMaxim Levitsky } 25192634125SMaxim Levitsky 25292634125SMaxim Levitsky /* Cleanups DMA related settings */ 25392634125SMaxim Levitsky static void r592_stop_dma(struct r592_device *dev, int error) 25492634125SMaxim Levitsky { 25592634125SMaxim Levitsky r592_clear_reg_mask(dev, R592_FIFO_DMA_SETTINGS, 25692634125SMaxim Levitsky R592_FIFO_DMA_SETTINGS_EN); 25792634125SMaxim Levitsky 25892634125SMaxim Levitsky /* This is only a precation */ 25992634125SMaxim Levitsky r592_write_reg(dev, R592_FIFO_DMA, 26092634125SMaxim Levitsky dev->dummy_dma_page_physical_address); 26192634125SMaxim Levitsky 26292634125SMaxim Levitsky r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_EN_MASK); 26392634125SMaxim Levitsky r592_clear_reg_mask(dev, R592_REG_MSC, DMA_IRQ_ACK_MASK); 26492634125SMaxim Levitsky dev->dma_error = error; 26592634125SMaxim Levitsky } 26692634125SMaxim Levitsky 26792634125SMaxim Levitsky /* Test if hardware supports DMA */ 26892634125SMaxim Levitsky static void r592_check_dma(struct r592_device *dev) 26992634125SMaxim Levitsky { 27092634125SMaxim Levitsky dev->dma_capable = enable_dma && 27192634125SMaxim Levitsky (r592_read_reg(dev, R592_FIFO_DMA_SETTINGS) & 27292634125SMaxim Levitsky R592_FIFO_DMA_SETTINGS_CAP); 27392634125SMaxim Levitsky } 27492634125SMaxim Levitsky 27592634125SMaxim Levitsky /* Transfers fifo contents in/out using DMA */ 27692634125SMaxim Levitsky static int r592_transfer_fifo_dma(struct r592_device *dev) 27792634125SMaxim Levitsky { 27892634125SMaxim Levitsky int len, sg_count; 27992634125SMaxim Levitsky bool is_write; 28092634125SMaxim Levitsky 28192634125SMaxim Levitsky if (!dev->dma_capable || !dev->req->long_data) 28292634125SMaxim Levitsky return -EINVAL; 28392634125SMaxim Levitsky 28492634125SMaxim Levitsky len = dev->req->sg.length; 28592634125SMaxim Levitsky is_write = dev->req->data_dir == WRITE; 28692634125SMaxim Levitsky 28792634125SMaxim Levitsky if (len != R592_LFIFO_SIZE) 28892634125SMaxim Levitsky return -EINVAL; 28992634125SMaxim Levitsky 29092634125SMaxim Levitsky dbg_verbose("doing dma transfer"); 29192634125SMaxim Levitsky 29292634125SMaxim Levitsky dev->dma_error = 0; 29392634125SMaxim Levitsky INIT_COMPLETION(dev->dma_done); 29492634125SMaxim Levitsky 29592634125SMaxim Levitsky /* TODO: hidden assumption about nenth beeing always 1 */ 29692634125SMaxim Levitsky sg_count = dma_map_sg(&dev->pci_dev->dev, &dev->req->sg, 1, is_write ? 29792634125SMaxim Levitsky PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); 29892634125SMaxim Levitsky 29992634125SMaxim Levitsky if (sg_count != 1 || 30092634125SMaxim Levitsky (sg_dma_len(&dev->req->sg) < dev->req->sg.length)) { 30192634125SMaxim Levitsky message("problem in dma_map_sg"); 30292634125SMaxim Levitsky return -EIO; 30392634125SMaxim Levitsky } 30492634125SMaxim Levitsky 30592634125SMaxim Levitsky r592_start_dma(dev, is_write); 30692634125SMaxim Levitsky 30792634125SMaxim Levitsky /* Wait for DMA completion */ 30892634125SMaxim Levitsky if (!wait_for_completion_timeout( 30992634125SMaxim Levitsky &dev->dma_done, msecs_to_jiffies(1000))) { 31092634125SMaxim Levitsky message("DMA timeout"); 31192634125SMaxim Levitsky r592_stop_dma(dev, -ETIMEDOUT); 31292634125SMaxim Levitsky } 31392634125SMaxim Levitsky 31492634125SMaxim Levitsky dma_unmap_sg(&dev->pci_dev->dev, &dev->req->sg, 1, is_write ? 31592634125SMaxim Levitsky PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE); 31692634125SMaxim Levitsky 31792634125SMaxim Levitsky 31892634125SMaxim Levitsky return dev->dma_error; 31992634125SMaxim Levitsky } 32092634125SMaxim Levitsky 32192634125SMaxim Levitsky /* 32292634125SMaxim Levitsky * Writes the FIFO in 4 byte chunks. 32392634125SMaxim Levitsky * If length isn't 4 byte aligned, rest of the data if put to a fifo 32492634125SMaxim Levitsky * to be written later 32592634125SMaxim Levitsky * Use r592_flush_fifo_write to flush that fifo when writing for the 32692634125SMaxim Levitsky * last time 32792634125SMaxim Levitsky */ 32892634125SMaxim Levitsky static void r592_write_fifo_pio(struct r592_device *dev, 32992634125SMaxim Levitsky unsigned char *buffer, int len) 33092634125SMaxim Levitsky { 33192634125SMaxim Levitsky /* flush spill from former write */ 33292634125SMaxim Levitsky if (!kfifo_is_empty(&dev->pio_fifo)) { 33392634125SMaxim Levitsky 33492634125SMaxim Levitsky u8 tmp[4] = {0}; 33592634125SMaxim Levitsky int copy_len = kfifo_in(&dev->pio_fifo, buffer, len); 33692634125SMaxim Levitsky 33792634125SMaxim Levitsky if (!kfifo_is_full(&dev->pio_fifo)) 33892634125SMaxim Levitsky return; 33992634125SMaxim Levitsky len -= copy_len; 34092634125SMaxim Levitsky buffer += copy_len; 34192634125SMaxim Levitsky 34292634125SMaxim Levitsky copy_len = kfifo_out(&dev->pio_fifo, tmp, 4); 34392634125SMaxim Levitsky WARN_ON(copy_len != 4); 34492634125SMaxim Levitsky r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)tmp); 34592634125SMaxim Levitsky } 34692634125SMaxim Levitsky 34792634125SMaxim Levitsky WARN_ON(!kfifo_is_empty(&dev->pio_fifo)); 34892634125SMaxim Levitsky 34992634125SMaxim Levitsky /* write full dwords */ 35092634125SMaxim Levitsky while (len >= 4) { 35192634125SMaxim Levitsky r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)buffer); 35292634125SMaxim Levitsky buffer += 4; 35392634125SMaxim Levitsky len -= 4; 35492634125SMaxim Levitsky } 35592634125SMaxim Levitsky 35692634125SMaxim Levitsky /* put remaining bytes to the spill */ 35792634125SMaxim Levitsky if (len) 35892634125SMaxim Levitsky kfifo_in(&dev->pio_fifo, buffer, len); 35992634125SMaxim Levitsky } 36092634125SMaxim Levitsky 36192634125SMaxim Levitsky /* Flushes the temporary FIFO used to make aligned DWORD writes */ 36292634125SMaxim Levitsky static void r592_flush_fifo_write(struct r592_device *dev) 36392634125SMaxim Levitsky { 36492634125SMaxim Levitsky u8 buffer[4] = { 0 }; 36592634125SMaxim Levitsky int len; 36692634125SMaxim Levitsky 36792634125SMaxim Levitsky if (kfifo_is_empty(&dev->pio_fifo)) 36892634125SMaxim Levitsky return; 36992634125SMaxim Levitsky 37092634125SMaxim Levitsky len = kfifo_out(&dev->pio_fifo, buffer, 4); 37192634125SMaxim Levitsky r592_write_reg_raw_be(dev, R592_FIFO_PIO, *(u32 *)buffer); 37292634125SMaxim Levitsky } 37392634125SMaxim Levitsky 37492634125SMaxim Levitsky /* 37592634125SMaxim Levitsky * Read a fifo in 4 bytes chunks. 37692634125SMaxim Levitsky * If input doesn't fit the buffer, it places bytes of last dword in spill 37792634125SMaxim Levitsky * buffer, so that they don't get lost on last read, just throw these away. 37892634125SMaxim Levitsky */ 37992634125SMaxim Levitsky static void r592_read_fifo_pio(struct r592_device *dev, 38092634125SMaxim Levitsky unsigned char *buffer, int len) 38192634125SMaxim Levitsky { 38292634125SMaxim Levitsky u8 tmp[4]; 38392634125SMaxim Levitsky 38492634125SMaxim Levitsky /* Read from last spill */ 38592634125SMaxim Levitsky if (!kfifo_is_empty(&dev->pio_fifo)) { 38692634125SMaxim Levitsky int bytes_copied = 38792634125SMaxim Levitsky kfifo_out(&dev->pio_fifo, buffer, min(4, len)); 38892634125SMaxim Levitsky buffer += bytes_copied; 38992634125SMaxim Levitsky len -= bytes_copied; 39092634125SMaxim Levitsky 39192634125SMaxim Levitsky if (!kfifo_is_empty(&dev->pio_fifo)) 39292634125SMaxim Levitsky return; 39392634125SMaxim Levitsky } 39492634125SMaxim Levitsky 39592634125SMaxim Levitsky /* Reads dwords from FIFO */ 39692634125SMaxim Levitsky while (len >= 4) { 39792634125SMaxim Levitsky *(u32 *)buffer = r592_read_reg_raw_be(dev, R592_FIFO_PIO); 39892634125SMaxim Levitsky buffer += 4; 39992634125SMaxim Levitsky len -= 4; 40092634125SMaxim Levitsky } 40192634125SMaxim Levitsky 40292634125SMaxim Levitsky if (len) { 40392634125SMaxim Levitsky *(u32 *)tmp = r592_read_reg_raw_be(dev, R592_FIFO_PIO); 40492634125SMaxim Levitsky kfifo_in(&dev->pio_fifo, tmp, 4); 40592634125SMaxim Levitsky len -= kfifo_out(&dev->pio_fifo, buffer, len); 40692634125SMaxim Levitsky } 40792634125SMaxim Levitsky 40892634125SMaxim Levitsky WARN_ON(len); 40992634125SMaxim Levitsky return; 41092634125SMaxim Levitsky } 41192634125SMaxim Levitsky 41292634125SMaxim Levitsky /* Transfers actual data using PIO. */ 41392634125SMaxim Levitsky static int r592_transfer_fifo_pio(struct r592_device *dev) 41492634125SMaxim Levitsky { 41592634125SMaxim Levitsky unsigned long flags; 41692634125SMaxim Levitsky 41792634125SMaxim Levitsky bool is_write = dev->req->tpc >= MS_TPC_SET_RW_REG_ADRS; 41892634125SMaxim Levitsky struct sg_mapping_iter miter; 41992634125SMaxim Levitsky 42092634125SMaxim Levitsky kfifo_reset(&dev->pio_fifo); 42192634125SMaxim Levitsky 42292634125SMaxim Levitsky if (!dev->req->long_data) { 42392634125SMaxim Levitsky if (is_write) { 42492634125SMaxim Levitsky r592_write_fifo_pio(dev, dev->req->data, 42592634125SMaxim Levitsky dev->req->data_len); 42692634125SMaxim Levitsky r592_flush_fifo_write(dev); 42792634125SMaxim Levitsky } else 42892634125SMaxim Levitsky r592_read_fifo_pio(dev, dev->req->data, 42992634125SMaxim Levitsky dev->req->data_len); 43092634125SMaxim Levitsky return 0; 43192634125SMaxim Levitsky } 43292634125SMaxim Levitsky 43392634125SMaxim Levitsky local_irq_save(flags); 43492634125SMaxim Levitsky sg_miter_start(&miter, &dev->req->sg, 1, SG_MITER_ATOMIC | 43592634125SMaxim Levitsky (is_write ? SG_MITER_FROM_SG : SG_MITER_TO_SG)); 43692634125SMaxim Levitsky 43792634125SMaxim Levitsky /* Do the transfer fifo<->memory*/ 43892634125SMaxim Levitsky while (sg_miter_next(&miter)) 43992634125SMaxim Levitsky if (is_write) 44092634125SMaxim Levitsky r592_write_fifo_pio(dev, miter.addr, miter.length); 44192634125SMaxim Levitsky else 44292634125SMaxim Levitsky r592_read_fifo_pio(dev, miter.addr, miter.length); 44392634125SMaxim Levitsky 44492634125SMaxim Levitsky 44592634125SMaxim Levitsky /* Write last few non aligned bytes*/ 44692634125SMaxim Levitsky if (is_write) 44792634125SMaxim Levitsky r592_flush_fifo_write(dev); 44892634125SMaxim Levitsky 44992634125SMaxim Levitsky sg_miter_stop(&miter); 45092634125SMaxim Levitsky local_irq_restore(flags); 45192634125SMaxim Levitsky return 0; 45292634125SMaxim Levitsky } 45392634125SMaxim Levitsky 45492634125SMaxim Levitsky /* Executes one TPC (data is read/written from small or large fifo) */ 45592634125SMaxim Levitsky static void r592_execute_tpc(struct r592_device *dev) 45692634125SMaxim Levitsky { 45792634125SMaxim Levitsky bool is_write = dev->req->tpc >= MS_TPC_SET_RW_REG_ADRS; 45892634125SMaxim Levitsky int len, error; 45992634125SMaxim Levitsky u32 status, reg; 46092634125SMaxim Levitsky 46192634125SMaxim Levitsky if (!dev->req) { 46292634125SMaxim Levitsky message("BUG: tpc execution without request!"); 46392634125SMaxim Levitsky return; 46492634125SMaxim Levitsky } 46592634125SMaxim Levitsky 46692634125SMaxim Levitsky len = dev->req->long_data ? 46792634125SMaxim Levitsky dev->req->sg.length : dev->req->data_len; 46892634125SMaxim Levitsky 46992634125SMaxim Levitsky /* Ensure that FIFO can hold the input data */ 47092634125SMaxim Levitsky if (len > R592_LFIFO_SIZE) { 47192634125SMaxim Levitsky message("IO: hardware doesn't support TPCs longer that 512"); 47292634125SMaxim Levitsky error = -ENOSYS; 47392634125SMaxim Levitsky goto out; 47492634125SMaxim Levitsky } 47592634125SMaxim Levitsky 47692634125SMaxim Levitsky if (!(r592_read_reg(dev, R592_REG_MSC) & R592_REG_MSC_PRSNT)) { 47792634125SMaxim Levitsky dbg("IO: refusing to send TPC because card is absent"); 47892634125SMaxim Levitsky error = -ENODEV; 47992634125SMaxim Levitsky goto out; 48092634125SMaxim Levitsky } 48192634125SMaxim Levitsky 48292634125SMaxim Levitsky dbg("IO: executing %s LEN=%d", 48392634125SMaxim Levitsky memstick_debug_get_tpc_name(dev->req->tpc), len); 48492634125SMaxim Levitsky 48592634125SMaxim Levitsky /* Set IO direction */ 48692634125SMaxim Levitsky if (is_write) 48792634125SMaxim Levitsky r592_set_reg_mask(dev, R592_IO, R592_IO_DIRECTION); 48892634125SMaxim Levitsky else 48992634125SMaxim Levitsky r592_clear_reg_mask(dev, R592_IO, R592_IO_DIRECTION); 49092634125SMaxim Levitsky 49192634125SMaxim Levitsky 49292634125SMaxim Levitsky error = r592_test_fifo_empty(dev); 49392634125SMaxim Levitsky if (error) 49492634125SMaxim Levitsky goto out; 49592634125SMaxim Levitsky 49692634125SMaxim Levitsky /* Transfer write data */ 49792634125SMaxim Levitsky if (is_write) { 49892634125SMaxim Levitsky error = r592_transfer_fifo_dma(dev); 49992634125SMaxim Levitsky if (error == -EINVAL) 50092634125SMaxim Levitsky error = r592_transfer_fifo_pio(dev); 50192634125SMaxim Levitsky } 50292634125SMaxim Levitsky 50392634125SMaxim Levitsky if (error) 50492634125SMaxim Levitsky goto out; 50592634125SMaxim Levitsky 50692634125SMaxim Levitsky /* Trigger the TPC */ 50792634125SMaxim Levitsky reg = (len << R592_TPC_EXEC_LEN_SHIFT) | 50892634125SMaxim Levitsky (dev->req->tpc << R592_TPC_EXEC_TPC_SHIFT) | 50992634125SMaxim Levitsky R592_TPC_EXEC_BIG_FIFO; 51092634125SMaxim Levitsky 51192634125SMaxim Levitsky r592_write_reg(dev, R592_TPC_EXEC, reg); 51292634125SMaxim Levitsky 51392634125SMaxim Levitsky /* Wait for TPC completion */ 51492634125SMaxim Levitsky status = R592_STATUS_RDY; 51592634125SMaxim Levitsky if (dev->req->need_card_int) 51692634125SMaxim Levitsky status |= R592_STATUS_CED; 51792634125SMaxim Levitsky 51892634125SMaxim Levitsky error = r592_wait_status(dev, status, status); 51992634125SMaxim Levitsky if (error) { 52092634125SMaxim Levitsky message("card didn't respond"); 52192634125SMaxim Levitsky goto out; 52292634125SMaxim Levitsky } 52392634125SMaxim Levitsky 52492634125SMaxim Levitsky /* Test IO errors */ 52592634125SMaxim Levitsky error = r592_test_io_error(dev); 52692634125SMaxim Levitsky if (error) { 52792634125SMaxim Levitsky dbg("IO error"); 52892634125SMaxim Levitsky goto out; 52992634125SMaxim Levitsky } 53092634125SMaxim Levitsky 53192634125SMaxim Levitsky /* Read data from FIFO */ 53292634125SMaxim Levitsky if (!is_write) { 53392634125SMaxim Levitsky error = r592_transfer_fifo_dma(dev); 53492634125SMaxim Levitsky if (error == -EINVAL) 53592634125SMaxim Levitsky error = r592_transfer_fifo_pio(dev); 53692634125SMaxim Levitsky } 53792634125SMaxim Levitsky 53892634125SMaxim Levitsky /* read INT reg. This can be shortened with shifts, but that way 53992634125SMaxim Levitsky its more readable */ 54092634125SMaxim Levitsky if (dev->parallel_mode && dev->req->need_card_int) { 54192634125SMaxim Levitsky 54292634125SMaxim Levitsky dev->req->int_reg = 0; 54392634125SMaxim Levitsky status = r592_read_reg(dev, R592_STATUS); 54492634125SMaxim Levitsky 54592634125SMaxim Levitsky if (status & R592_STATUS_P_CMDNACK) 54692634125SMaxim Levitsky dev->req->int_reg |= MEMSTICK_INT_CMDNAK; 54792634125SMaxim Levitsky if (status & R592_STATUS_P_BREQ) 54892634125SMaxim Levitsky dev->req->int_reg |= MEMSTICK_INT_BREQ; 54992634125SMaxim Levitsky if (status & R592_STATUS_P_INTERR) 55092634125SMaxim Levitsky dev->req->int_reg |= MEMSTICK_INT_ERR; 55192634125SMaxim Levitsky if (status & R592_STATUS_P_CED) 55292634125SMaxim Levitsky dev->req->int_reg |= MEMSTICK_INT_CED; 55392634125SMaxim Levitsky } 55492634125SMaxim Levitsky 55592634125SMaxim Levitsky if (error) 55692634125SMaxim Levitsky dbg("FIFO read error"); 55792634125SMaxim Levitsky out: 55892634125SMaxim Levitsky dev->req->error = error; 55992634125SMaxim Levitsky r592_clear_reg_mask(dev, R592_REG_MSC, R592_REG_MSC_LED); 56092634125SMaxim Levitsky return; 56192634125SMaxim Levitsky } 56292634125SMaxim Levitsky 56392634125SMaxim Levitsky /* Main request processing thread */ 56492634125SMaxim Levitsky static int r592_process_thread(void *data) 56592634125SMaxim Levitsky { 56692634125SMaxim Levitsky int error; 56792634125SMaxim Levitsky struct r592_device *dev = (struct r592_device *)data; 56892634125SMaxim Levitsky unsigned long flags; 56992634125SMaxim Levitsky 57092634125SMaxim Levitsky while (!kthread_should_stop()) { 57192634125SMaxim Levitsky spin_lock_irqsave(&dev->io_thread_lock, flags); 57292634125SMaxim Levitsky set_current_state(TASK_INTERRUPTIBLE); 57392634125SMaxim Levitsky error = memstick_next_req(dev->host, &dev->req); 57492634125SMaxim Levitsky spin_unlock_irqrestore(&dev->io_thread_lock, flags); 57592634125SMaxim Levitsky 57692634125SMaxim Levitsky if (error) { 57792634125SMaxim Levitsky if (error == -ENXIO || error == -EAGAIN) { 57892634125SMaxim Levitsky dbg_verbose("IO: done IO, sleeping"); 57992634125SMaxim Levitsky } else { 58092634125SMaxim Levitsky dbg("IO: unknown error from " 58192634125SMaxim Levitsky "memstick_next_req %d", error); 58292634125SMaxim Levitsky } 58392634125SMaxim Levitsky 58492634125SMaxim Levitsky if (kthread_should_stop()) 58592634125SMaxim Levitsky set_current_state(TASK_RUNNING); 58692634125SMaxim Levitsky 58792634125SMaxim Levitsky schedule(); 58892634125SMaxim Levitsky } else { 58992634125SMaxim Levitsky set_current_state(TASK_RUNNING); 59092634125SMaxim Levitsky r592_execute_tpc(dev); 59192634125SMaxim Levitsky } 59292634125SMaxim Levitsky } 59392634125SMaxim Levitsky return 0; 59492634125SMaxim Levitsky } 59592634125SMaxim Levitsky 59692634125SMaxim Levitsky /* Reprogram chip to detect change in card state */ 59792634125SMaxim Levitsky /* eg, if card is detected, arm it to detect removal, and vice versa */ 59892634125SMaxim Levitsky static void r592_update_card_detect(struct r592_device *dev) 59992634125SMaxim Levitsky { 60092634125SMaxim Levitsky u32 reg = r592_read_reg(dev, R592_REG_MSC); 60192634125SMaxim Levitsky bool card_detected = reg & R592_REG_MSC_PRSNT; 60292634125SMaxim Levitsky 60392634125SMaxim Levitsky dbg("update card detect. card state: %s", card_detected ? 60492634125SMaxim Levitsky "present" : "absent"); 60592634125SMaxim Levitsky 60692634125SMaxim Levitsky reg &= ~((R592_REG_MSC_IRQ_REMOVE | R592_REG_MSC_IRQ_INSERT) << 16); 60792634125SMaxim Levitsky 60892634125SMaxim Levitsky if (card_detected) 60992634125SMaxim Levitsky reg |= (R592_REG_MSC_IRQ_REMOVE << 16); 61092634125SMaxim Levitsky else 61192634125SMaxim Levitsky reg |= (R592_REG_MSC_IRQ_INSERT << 16); 61292634125SMaxim Levitsky 61392634125SMaxim Levitsky r592_write_reg(dev, R592_REG_MSC, reg); 61492634125SMaxim Levitsky } 61592634125SMaxim Levitsky 61692634125SMaxim Levitsky /* Timer routine that fires 1 second after last card detection event, */ 61792634125SMaxim Levitsky static void r592_detect_timer(long unsigned int data) 61892634125SMaxim Levitsky { 61992634125SMaxim Levitsky struct r592_device *dev = (struct r592_device *)data; 62092634125SMaxim Levitsky r592_update_card_detect(dev); 62192634125SMaxim Levitsky memstick_detect_change(dev->host); 62292634125SMaxim Levitsky } 62392634125SMaxim Levitsky 62492634125SMaxim Levitsky /* Interrupt handler */ 62592634125SMaxim Levitsky static irqreturn_t r592_irq(int irq, void *data) 62692634125SMaxim Levitsky { 62792634125SMaxim Levitsky struct r592_device *dev = (struct r592_device *)data; 62892634125SMaxim Levitsky irqreturn_t ret = IRQ_NONE; 62992634125SMaxim Levitsky u32 reg; 63092634125SMaxim Levitsky u16 irq_enable, irq_status; 63192634125SMaxim Levitsky unsigned long flags; 63292634125SMaxim Levitsky int error; 63392634125SMaxim Levitsky 63492634125SMaxim Levitsky spin_lock_irqsave(&dev->irq_lock, flags); 63592634125SMaxim Levitsky 63692634125SMaxim Levitsky reg = r592_read_reg(dev, R592_REG_MSC); 63792634125SMaxim Levitsky irq_enable = reg >> 16; 63892634125SMaxim Levitsky irq_status = reg & 0xFFFF; 63992634125SMaxim Levitsky 64092634125SMaxim Levitsky /* Ack the interrupts */ 64192634125SMaxim Levitsky reg &= ~irq_status; 64292634125SMaxim Levitsky r592_write_reg(dev, R592_REG_MSC, reg); 64392634125SMaxim Levitsky 64492634125SMaxim Levitsky /* Get the IRQ status minus bits that aren't enabled */ 64592634125SMaxim Levitsky irq_status &= (irq_enable); 64692634125SMaxim Levitsky 64792634125SMaxim Levitsky /* Due to limitation of memstick core, we don't look at bits that 64892634125SMaxim Levitsky indicate that card was removed/inserted and/or present */ 64992634125SMaxim Levitsky if (irq_status & (R592_REG_MSC_IRQ_INSERT | R592_REG_MSC_IRQ_REMOVE)) { 65092634125SMaxim Levitsky 65192634125SMaxim Levitsky bool card_was_added = irq_status & R592_REG_MSC_IRQ_INSERT; 65292634125SMaxim Levitsky ret = IRQ_HANDLED; 65392634125SMaxim Levitsky 65492634125SMaxim Levitsky message("IRQ: card %s", card_was_added ? "added" : "removed"); 65592634125SMaxim Levitsky 65692634125SMaxim Levitsky mod_timer(&dev->detect_timer, 65792634125SMaxim Levitsky jiffies + msecs_to_jiffies(card_was_added ? 500 : 50)); 65892634125SMaxim Levitsky } 65992634125SMaxim Levitsky 66092634125SMaxim Levitsky if (irq_status & 66192634125SMaxim Levitsky (R592_REG_MSC_FIFO_DMA_DONE | R592_REG_MSC_FIFO_DMA_ERR)) { 66292634125SMaxim Levitsky ret = IRQ_HANDLED; 66392634125SMaxim Levitsky 66492634125SMaxim Levitsky if (irq_status & R592_REG_MSC_FIFO_DMA_ERR) { 66592634125SMaxim Levitsky message("IRQ: DMA error"); 66692634125SMaxim Levitsky error = -EIO; 66792634125SMaxim Levitsky } else { 66892634125SMaxim Levitsky dbg_verbose("IRQ: dma done"); 66992634125SMaxim Levitsky error = 0; 67092634125SMaxim Levitsky } 67192634125SMaxim Levitsky 67292634125SMaxim Levitsky r592_stop_dma(dev, error); 67392634125SMaxim Levitsky complete(&dev->dma_done); 67492634125SMaxim Levitsky } 67592634125SMaxim Levitsky 67692634125SMaxim Levitsky spin_unlock_irqrestore(&dev->irq_lock, flags); 67792634125SMaxim Levitsky return ret; 67892634125SMaxim Levitsky } 67992634125SMaxim Levitsky 68092634125SMaxim Levitsky /* External inteface: set settings */ 68192634125SMaxim Levitsky static int r592_set_param(struct memstick_host *host, 68292634125SMaxim Levitsky enum memstick_param param, int value) 68392634125SMaxim Levitsky { 68492634125SMaxim Levitsky struct r592_device *dev = memstick_priv(host); 68592634125SMaxim Levitsky 68692634125SMaxim Levitsky switch (param) { 68792634125SMaxim Levitsky case MEMSTICK_POWER: 68892634125SMaxim Levitsky switch (value) { 68992634125SMaxim Levitsky case MEMSTICK_POWER_ON: 69092634125SMaxim Levitsky return r592_enable_device(dev, true); 69192634125SMaxim Levitsky case MEMSTICK_POWER_OFF: 69292634125SMaxim Levitsky return r592_enable_device(dev, false); 69392634125SMaxim Levitsky default: 69492634125SMaxim Levitsky return -EINVAL; 69592634125SMaxim Levitsky } 69692634125SMaxim Levitsky case MEMSTICK_INTERFACE: 69792634125SMaxim Levitsky switch (value) { 69892634125SMaxim Levitsky case MEMSTICK_SERIAL: 69992634125SMaxim Levitsky return r592_set_mode(dev, 0); 70092634125SMaxim Levitsky case MEMSTICK_PAR4: 70192634125SMaxim Levitsky return r592_set_mode(dev, 1); 70292634125SMaxim Levitsky default: 70392634125SMaxim Levitsky return -EINVAL; 70492634125SMaxim Levitsky } 70592634125SMaxim Levitsky default: 70692634125SMaxim Levitsky return -EINVAL; 70792634125SMaxim Levitsky } 70892634125SMaxim Levitsky } 70992634125SMaxim Levitsky 71092634125SMaxim Levitsky /* External interface: submit requests */ 71192634125SMaxim Levitsky static void r592_submit_req(struct memstick_host *host) 71292634125SMaxim Levitsky { 71392634125SMaxim Levitsky struct r592_device *dev = memstick_priv(host); 71492634125SMaxim Levitsky unsigned long flags; 71592634125SMaxim Levitsky 71692634125SMaxim Levitsky if (dev->req) 71792634125SMaxim Levitsky return; 71892634125SMaxim Levitsky 71992634125SMaxim Levitsky spin_lock_irqsave(&dev->io_thread_lock, flags); 72092634125SMaxim Levitsky if (wake_up_process(dev->io_thread)) 72192634125SMaxim Levitsky dbg_verbose("IO thread woken to process requests"); 72292634125SMaxim Levitsky spin_unlock_irqrestore(&dev->io_thread_lock, flags); 72392634125SMaxim Levitsky } 72492634125SMaxim Levitsky 72592634125SMaxim Levitsky static const struct pci_device_id r592_pci_id_tbl[] = { 72692634125SMaxim Levitsky 72792634125SMaxim Levitsky { PCI_VDEVICE(RICOH, 0x0592), }, 72892634125SMaxim Levitsky { }, 72992634125SMaxim Levitsky }; 73092634125SMaxim Levitsky 73192634125SMaxim Levitsky /* Main entry */ 73292634125SMaxim Levitsky static int r592_probe(struct pci_dev *pdev, const struct pci_device_id *id) 73392634125SMaxim Levitsky { 73492634125SMaxim Levitsky int error = -ENOMEM; 73592634125SMaxim Levitsky struct memstick_host *host; 73692634125SMaxim Levitsky struct r592_device *dev; 73792634125SMaxim Levitsky 73892634125SMaxim Levitsky /* Allocate memory */ 73992634125SMaxim Levitsky host = memstick_alloc_host(sizeof(struct r592_device), &pdev->dev); 74092634125SMaxim Levitsky if (!host) 74192634125SMaxim Levitsky goto error1; 74292634125SMaxim Levitsky 74392634125SMaxim Levitsky dev = memstick_priv(host); 74492634125SMaxim Levitsky dev->host = host; 74592634125SMaxim Levitsky dev->pci_dev = pdev; 74692634125SMaxim Levitsky pci_set_drvdata(pdev, dev); 74792634125SMaxim Levitsky 74892634125SMaxim Levitsky /* pci initialization */ 74992634125SMaxim Levitsky error = pci_enable_device(pdev); 75092634125SMaxim Levitsky if (error) 75192634125SMaxim Levitsky goto error2; 75292634125SMaxim Levitsky 75392634125SMaxim Levitsky pci_set_master(pdev); 75492634125SMaxim Levitsky error = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 75592634125SMaxim Levitsky if (error) 75692634125SMaxim Levitsky goto error3; 75792634125SMaxim Levitsky 75892634125SMaxim Levitsky error = pci_request_regions(pdev, DRV_NAME); 75992634125SMaxim Levitsky if (error) 76092634125SMaxim Levitsky goto error3; 76192634125SMaxim Levitsky 76292634125SMaxim Levitsky dev->mmio = pci_ioremap_bar(pdev, 0); 76392634125SMaxim Levitsky if (!dev->mmio) 76492634125SMaxim Levitsky goto error4; 76592634125SMaxim Levitsky 76692634125SMaxim Levitsky dev->irq = pdev->irq; 76792634125SMaxim Levitsky spin_lock_init(&dev->irq_lock); 76892634125SMaxim Levitsky spin_lock_init(&dev->io_thread_lock); 76992634125SMaxim Levitsky init_completion(&dev->dma_done); 77092634125SMaxim Levitsky INIT_KFIFO(dev->pio_fifo); 77192634125SMaxim Levitsky setup_timer(&dev->detect_timer, 77292634125SMaxim Levitsky r592_detect_timer, (long unsigned int)dev); 77392634125SMaxim Levitsky 77492634125SMaxim Levitsky /* Host initialization */ 77592634125SMaxim Levitsky host->caps = MEMSTICK_CAP_PAR4; 77692634125SMaxim Levitsky host->request = r592_submit_req; 77792634125SMaxim Levitsky host->set_param = r592_set_param; 77892634125SMaxim Levitsky r592_check_dma(dev); 77992634125SMaxim Levitsky 78092634125SMaxim Levitsky dev->io_thread = kthread_run(r592_process_thread, dev, "r592_io"); 78192634125SMaxim Levitsky if (IS_ERR(dev->io_thread)) { 78292634125SMaxim Levitsky error = PTR_ERR(dev->io_thread); 78392634125SMaxim Levitsky goto error5; 78492634125SMaxim Levitsky } 78592634125SMaxim Levitsky 78692634125SMaxim Levitsky /* This is just a precation, so don't fail */ 78792634125SMaxim Levitsky dev->dummy_dma_page = pci_alloc_consistent(pdev, PAGE_SIZE, 78892634125SMaxim Levitsky &dev->dummy_dma_page_physical_address); 78992634125SMaxim Levitsky r592_stop_dma(dev , 0); 79092634125SMaxim Levitsky 79192634125SMaxim Levitsky if (request_irq(dev->irq, &r592_irq, IRQF_SHARED, 79292634125SMaxim Levitsky DRV_NAME, dev)) 79392634125SMaxim Levitsky goto error6; 79492634125SMaxim Levitsky 79592634125SMaxim Levitsky r592_update_card_detect(dev); 79692634125SMaxim Levitsky if (memstick_add_host(host)) 79792634125SMaxim Levitsky goto error7; 79892634125SMaxim Levitsky 79992634125SMaxim Levitsky message("driver succesfully loaded"); 80092634125SMaxim Levitsky return 0; 80192634125SMaxim Levitsky error7: 80292634125SMaxim Levitsky free_irq(dev->irq, dev); 80392634125SMaxim Levitsky error6: 80492634125SMaxim Levitsky if (dev->dummy_dma_page) 80592634125SMaxim Levitsky pci_free_consistent(pdev, PAGE_SIZE, dev->dummy_dma_page, 80692634125SMaxim Levitsky dev->dummy_dma_page_physical_address); 80792634125SMaxim Levitsky 80892634125SMaxim Levitsky kthread_stop(dev->io_thread); 80992634125SMaxim Levitsky error5: 81092634125SMaxim Levitsky iounmap(dev->mmio); 81192634125SMaxim Levitsky error4: 81292634125SMaxim Levitsky pci_release_regions(pdev); 81392634125SMaxim Levitsky error3: 81492634125SMaxim Levitsky pci_disable_device(pdev); 81592634125SMaxim Levitsky error2: 81692634125SMaxim Levitsky memstick_free_host(host); 81792634125SMaxim Levitsky error1: 81892634125SMaxim Levitsky return error; 81992634125SMaxim Levitsky } 82092634125SMaxim Levitsky 82192634125SMaxim Levitsky static void r592_remove(struct pci_dev *pdev) 82292634125SMaxim Levitsky { 82392634125SMaxim Levitsky int error = 0; 82492634125SMaxim Levitsky struct r592_device *dev = pci_get_drvdata(pdev); 82592634125SMaxim Levitsky 82692634125SMaxim Levitsky /* Stop the processing thread. 82792634125SMaxim Levitsky That ensures that we won't take any more requests */ 82892634125SMaxim Levitsky kthread_stop(dev->io_thread); 82992634125SMaxim Levitsky 83092634125SMaxim Levitsky r592_enable_device(dev, false); 83192634125SMaxim Levitsky 83292634125SMaxim Levitsky while (!error && dev->req) { 83392634125SMaxim Levitsky dev->req->error = -ETIME; 83492634125SMaxim Levitsky error = memstick_next_req(dev->host, &dev->req); 83592634125SMaxim Levitsky } 83692634125SMaxim Levitsky memstick_remove_host(dev->host); 83792634125SMaxim Levitsky 83892634125SMaxim Levitsky free_irq(dev->irq, dev); 83992634125SMaxim Levitsky iounmap(dev->mmio); 84092634125SMaxim Levitsky pci_release_regions(pdev); 84192634125SMaxim Levitsky pci_disable_device(pdev); 84292634125SMaxim Levitsky memstick_free_host(dev->host); 84392634125SMaxim Levitsky 84492634125SMaxim Levitsky if (dev->dummy_dma_page) 84592634125SMaxim Levitsky pci_free_consistent(pdev, PAGE_SIZE, dev->dummy_dma_page, 84692634125SMaxim Levitsky dev->dummy_dma_page_physical_address); 84792634125SMaxim Levitsky } 84892634125SMaxim Levitsky 84992634125SMaxim Levitsky #ifdef CONFIG_PM 85092634125SMaxim Levitsky static int r592_suspend(struct device *core_dev) 85192634125SMaxim Levitsky { 85292634125SMaxim Levitsky struct pci_dev *pdev = to_pci_dev(core_dev); 85392634125SMaxim Levitsky struct r592_device *dev = pci_get_drvdata(pdev); 85492634125SMaxim Levitsky 85592634125SMaxim Levitsky r592_clear_interrupts(dev); 85692634125SMaxim Levitsky memstick_suspend_host(dev->host); 85792634125SMaxim Levitsky del_timer_sync(&dev->detect_timer); 85892634125SMaxim Levitsky return 0; 85992634125SMaxim Levitsky } 86092634125SMaxim Levitsky 86192634125SMaxim Levitsky static int r592_resume(struct device *core_dev) 86292634125SMaxim Levitsky { 86392634125SMaxim Levitsky struct pci_dev *pdev = to_pci_dev(core_dev); 86492634125SMaxim Levitsky struct r592_device *dev = pci_get_drvdata(pdev); 86592634125SMaxim Levitsky 86692634125SMaxim Levitsky r592_clear_interrupts(dev); 86792634125SMaxim Levitsky r592_enable_device(dev, false); 86892634125SMaxim Levitsky memstick_resume_host(dev->host); 86992634125SMaxim Levitsky r592_update_card_detect(dev); 87092634125SMaxim Levitsky return 0; 87192634125SMaxim Levitsky } 87292634125SMaxim Levitsky 87392634125SMaxim Levitsky SIMPLE_DEV_PM_OPS(r592_pm_ops, r592_suspend, r592_resume); 87492634125SMaxim Levitsky #endif 87592634125SMaxim Levitsky 87692634125SMaxim Levitsky MODULE_DEVICE_TABLE(pci, r592_pci_id_tbl); 87792634125SMaxim Levitsky 87892634125SMaxim Levitsky static struct pci_driver r852_pci_driver = { 87992634125SMaxim Levitsky .name = DRV_NAME, 88092634125SMaxim Levitsky .id_table = r592_pci_id_tbl, 88192634125SMaxim Levitsky .probe = r592_probe, 88292634125SMaxim Levitsky .remove = r592_remove, 88392634125SMaxim Levitsky #ifdef CONFIG_PM 88492634125SMaxim Levitsky .driver.pm = &r592_pm_ops, 88592634125SMaxim Levitsky #endif 88692634125SMaxim Levitsky }; 88792634125SMaxim Levitsky 88892634125SMaxim Levitsky static __init int r592_module_init(void) 88992634125SMaxim Levitsky { 89092634125SMaxim Levitsky return pci_register_driver(&r852_pci_driver); 89192634125SMaxim Levitsky } 89292634125SMaxim Levitsky 89392634125SMaxim Levitsky static void __exit r592_module_exit(void) 89492634125SMaxim Levitsky { 89592634125SMaxim Levitsky pci_unregister_driver(&r852_pci_driver); 89692634125SMaxim Levitsky } 89792634125SMaxim Levitsky 89892634125SMaxim Levitsky module_init(r592_module_init); 89992634125SMaxim Levitsky module_exit(r592_module_exit); 90092634125SMaxim Levitsky 90192634125SMaxim Levitsky module_param(enable_dma, bool, S_IRUGO); 90292634125SMaxim Levitsky MODULE_PARM_DESC(enable_dma, "Enable usage of the DMA (default)"); 90392634125SMaxim Levitsky module_param(debug, int, S_IRUGO | S_IWUSR); 90492634125SMaxim Levitsky MODULE_PARM_DESC(debug, "Debug level (0-3)"); 90592634125SMaxim Levitsky 90692634125SMaxim Levitsky MODULE_LICENSE("GPL"); 90792634125SMaxim Levitsky MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>"); 90892634125SMaxim Levitsky MODULE_DESCRIPTION("Ricoh R5C592 Memstick/Memstick PRO card reader driver"); 909